diff options
| author | John Klug <john.klug@multitech.com> | 2018-05-23 10:14:11 -0500 |
|---|---|---|
| committer | John Klug <john.klug@multitech.com> | 2018-05-23 10:14:11 -0500 |
| commit | c31d619c83ba2a6c7c803082e804d18da657a389 (patch) | |
| tree | f1b751dd1f3ae58a6e722b201b0e64b759ae03b4 /io-module | |
| parent | 27a24df1a2c713b070559fc5404258d7b691c833 (diff) | |
| download | mts-io-2.0.5.tar.gz mts-io-2.0.5.tar.bz2 mts-io-2.0.5.zip | |
Merge in global logic mts-io hotspot code.2.0.5
Diffstat (limited to 'io-module')
| -rw-r--r-- | io-module/Makefile | 2 | ||||
| -rw-r--r-- | io-module/buttons.c | 281 | ||||
| -rw-r--r-- | io-module/buttons.h | 84 | ||||
| -rw-r--r-- | io-module/gpio.c | 2 | ||||
| -rw-r--r-- | io-module/mths.c | 1016 | ||||
| -rw-r--r-- | io-module/mts-io.c (renamed from io-module/mts_io.c) | 43 | ||||
| -rw-r--r-- | io-module/mts_io.h | 10 |
7 files changed, 1086 insertions, 352 deletions
diff --git a/io-module/Makefile b/io-module/Makefile index 5d036f4..7e6693d 100644 --- a/io-module/Makefile +++ b/io-module/Makefile @@ -1,5 +1,7 @@ obj-m := mts_io.o +mts_io-objs := buttons.o mts-io.o + clean: rm -f *.ko *.o diff --git a/io-module/buttons.c b/io-module/buttons.c new file mode 100644 index 0000000..e7ed9ac --- /dev/null +++ b/io-module/buttons.c @@ -0,0 +1,281 @@ + +#include "buttons.h" + +extern ssize_t mts_attr_show_gpio_pin(struct device *dev, + struct device_attribute *attr, + char *buf); +extern struct gpio_pin *gpio_pin_by_attr_name(const char *name); + +static button_info_pt *buttons = NULL; + +struct gpio_pin *gpio_pin_by_button_name(const char *button_name) +{ + button_info_pt *pbutton; + + for (pbutton = buttons; *pbutton != NULL; pbutton++) { + if (!strcmp(pbutton[0]->name, button_name)) { + return gpio_pin_by_attr_name(pbutton[0]->label_pin); + } + } + + log_error("Button named %s not found", button_name); + return NULL; +} + +static button_info_t *button_by_monitor_name(const char *label_monitor) +{ + button_info_pt *pbutton; + + for (pbutton = buttons; *pbutton != NULL; pbutton++) { + if (!strcmp(pbutton[0]->label_monitor, label_monitor)) { + return pbutton[0]; + } + } + + log_error("Button with %s monitor label is not found", label_monitor); + return NULL; +} + +static button_info_t *button_by_monitor_intervals_name(const char *label_monitor_intervals) +{ + button_info_pt *pbutton; + + for (pbutton = buttons; *pbutton != NULL; pbutton++) { + if (!strcmp(pbutton[0]->label_monitor_intervals, label_monitor_intervals)) { + return pbutton[0]; + } + } + + log_error("Button with %s monitor intervals label is not found", label_monitor_intervals); + return NULL; +} + +ssize_t mts_attr_show_button_monitor_intervals(struct device *dev, struct device_attribute *attr, char *buf) +{ + int ret; + + button_info_t *button = button_by_monitor_intervals_name(attr->attr.name); + + if (!button) { + return -ENODEV; + } + + mutex_lock(&mts_io_mutex); + + ret = sprintf(buf, "%d %d\n", button->short_interval / BUTTON_CHECK_PER_SEC, button->long_interval / BUTTON_CHECK_PER_SEC); + + mutex_unlock(&mts_io_mutex); + + return ret; +} + +ssize_t mts_attr_store_button_monitor_intervals(struct device *dev, struct device_attribute *attr, char *buf, size_t count) +{ + int short_int; + int long_int; + button_info_t *button = button_by_monitor_intervals_name(attr->attr.name); + + if (sscanf(buf, "%i %i", &short_int, &long_int) != 2) { + return -EINVAL; + } + + if (!button) { + return -ENODEV; + } + + mutex_lock(&mts_io_mutex); + + button->short_interval = short_int * BUTTON_CHECK_PER_SEC; + button->long_interval = long_int * BUTTON_CHECK_PER_SEC; + + mutex_unlock(&mts_io_mutex); + + return count; +} + +ssize_t mts_attr_show_button_monitor(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int ret; + + button_info_t *button = button_by_monitor_name(attr->attr.name); + + if (!button) { + return -ENODEV; + } + + mutex_lock(&mts_io_mutex); + + ret = sprintf(buf, "%d %d %d %d\n", button->pid, button->short_signal, button->long_signal, button->extra_long_signal); + + mutex_unlock(&mts_io_mutex); + + return ret; +} + +ssize_t mts_attr_store_button_monitor(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + pid_t pid; + int short_signal; + int long_signal; + int extra_long_signal; + int result = sscanf(buf, "%i %i %i %i", &pid, &short_signal, &long_signal, &extra_long_signal); + + button_info_t *button = button_by_monitor_name(attr->attr.name); + + if (!button) { + return -ENODEV; + } + + if (result < 3 || result > 4) { + return -EINVAL; + } + + if(result == 3) { + mutex_lock(&mts_io_mutex); + + button->pid = pid; + button->short_signal = short_signal; + button->long_signal = long_signal; + + mutex_unlock(&mts_io_mutex); + } else { + mutex_lock(&mts_io_mutex); + + button->pid = pid; + button->short_signal = short_signal; + button->long_signal = long_signal; + button->extra_long_signal = extra_long_signal; + + mutex_unlock(&mts_io_mutex); + } + + return count; +} + + +static void button_worker(struct work_struct *ignored); + +static DECLARE_DELAYED_WORK(button_work, button_worker); + +static void button_worker(struct work_struct *ignored) +{ + struct gpio_pin *pin; + struct pid *vpid; + int button_pressed = 0; + + button_info_pt *pbutton; + + mutex_lock(&mts_io_mutex); + + for (pbutton = buttons; *pbutton != NULL; pbutton++) { + button_pressed = 0; + vpid = NULL; + + pin = gpio_pin_by_button_name(pbutton[0]->name); + + if (pin) { + button_pressed = !gpio_get_value(pin->pin.gpio); + } + + if (pbutton[0]->pid > 0) { + vpid = find_vpid(pbutton[0]->pid); + } + + if (vpid) { + if (button_pressed) { + pbutton[0]->pressed_count++; + } + else { + // The button is not pressed + if (pbutton[0]->pressed_count > 0 && pbutton[0]->pressed_count < pbutton[0]->short_interval) { + log_debug("Button %s short signal", pbutton[0]->name); + kill_pid(vpid, pbutton[0]->short_signal, 1); + } else if (pbutton[0]->pressed_count >= pbutton[0]->short_interval && pbutton[0]->pressed_count < pbutton[0]->long_interval) { + log_debug("Button %s long signal", pbutton[0]->name); + kill_pid(vpid, pbutton[0]->long_signal, 1); + } + + pbutton[0]->pressed_count = 0; + pbutton[0]->sent_extra_long = false; + } + if (pbutton[0]->pressed_count >= pbutton[0]->long_interval && ! pbutton[0]->sent_extra_long) { + log_debug("Button %s extra long signal", pbutton[0]->name); + kill_pid(vpid, pbutton[0]->extra_long_signal, 1); + pbutton[0]->sent_extra_long = true; + } + } else { + pbutton[0]->pressed_count = 0; + } + } + + mutex_unlock(&mts_io_mutex); + + schedule_delayed_work(&button_work, BUTTON_INTERVAL); +} + +int set_buttons (button_info_pt* platform_buttons) { + if (platform_buttons == NULL) { + log_error("Null pointer error"); + return -EINVAL; + } + mutex_lock(&mts_io_mutex); + if (buttons != NULL) { + log_warning("Buttons structure was initialized more then once"); + } + buttons = platform_buttons; + mutex_unlock(&mts_io_mutex); + return 0; +} + +void init_buttons(void) { + if (buttons == NULL) { + log_error("Button structure hasn't been set yet"); + return; + } + button_worker(NULL); +} + +void cleanup_buttons(void) { + cancel_delayed_work_sync(&button_work); +} + +// Reset button is common for all devices + +button_info_t reset_button = { + .name = "Reset Button", + .label_pin = "reset", + .label_monitor = "reset-monitor", + .label_monitor_intervals = "reset-monitor-intervals", + + /* Signals */ + .short_signal = SIGUSR1, + .long_signal = SIGUSR2, + .short_signal = SIGHUP, + + /* Intervals */ + .short_interval = BUTTON_HOLD_COUNT, + .long_interval = BUTTON_LONG_HOLD_COUNT, +}; + +DEVICE_ATTR_MTS(dev_attr_reset_monitor_intervals, + reset_button.label_monitor_intervals, + mts_attr_show_button_monitor_intervals, + mts_attr_store_button_monitor_intervals); + +DEVICE_ATTR_MTS(dev_attr_reset_monitor, + reset_button.label_monitor, + mts_attr_show_button_monitor, + mts_attr_store_button_monitor); + +DEVICE_ATTR_RO_MTS(dev_attr_reset, + reset_button.label_pin, + mts_attr_show_gpio_pin); + + +button_info_pt default_buttons[] = { + &reset_button, + NULL, +}; diff --git a/io-module/buttons.h b/io-module/buttons.h new file mode 100644 index 0000000..d324929 --- /dev/null +++ b/io-module/buttons.h @@ -0,0 +1,84 @@ +/* + * buttons.h + * + * Created on: Apr 27, 2018 + * Author: leonid + */ + +#ifndef IO_MODULE_BUTTONS_H_ +#define IO_MODULE_BUTTONS_H_ + +#include <linux/delay.h> +#include <linux/ioctl.h> +#include <linux/input.h> +#include <linux/cdev.h> +#include <linux/clk.h> +#include <linux/sched.h> +#include <linux/reboot.h> +#include <linux/uaccess.h> +#include <linux/gpio.h> +#include <linux/sched.h> +#include <linux/workqueue.h> +#include <linux/platform_device.h> +#include <linux/device.h> +#include <linux/bitops.h> +#include <linux/spi/spi.h> +#include <linux/i2c/at24.h> +#include <linux/kmod.h> +#include <linux/ctype.h> +#include <linux/io.h> +#include <linux/module.h> + +#include "mts_io.h" + +#define NEW_BUTTON_INTERFACE + +#define BUTTON_CHECK_PER_SEC 8 +#define BUTTON_INTERVAL (HZ / BUTTON_CHECK_PER_SEC) +#define BUTTON_HOLD_COUNT (BUTTON_CHECK_PER_SEC * 3) +#define BUTTON_LONG_HOLD_COUNT (BUTTON_CHECK_PER_SEC * 30) + +typedef struct button_info { + char name[32]; + char label_pin[32]; + char label_monitor[32]; + char label_monitor_intervals[32]; + + /* PID to notify */ + pid_t pid; + + /* Unix signals */ + int short_signal; + int long_signal; + int extra_long_signal; + + /* Monitor intervals */ + int short_interval; + int long_interval; + + /* Internal fields */ + int pressed_count; + bool sent_extra_long; +} button_info_t, *button_info_pt; + +extern void init_buttons(void); +extern void cleanup_buttons(void); +extern int set_buttons (button_info_pt* platform_buttons); + +extern ssize_t mts_attr_show_button_monitor_intervals(struct device *dev, struct device_attribute *attr, char *buf); +extern ssize_t mts_attr_store_button_monitor_intervals(struct device *dev, struct device_attribute *attr, char *buf, size_t count); +extern ssize_t mts_attr_show_button_monitor(struct device *dev, + struct device_attribute *attr, + char *buf); +extern ssize_t mts_attr_store_button_monitor(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count); + +// Reset button is common for all devices. It is defined in buttons.c + +extern button_info_t reset_button; +extern struct device_attribute dev_attr_reset_monitor_intervals; +extern struct device_attribute dev_attr_reset_monitor; +extern struct device_attribute dev_attr_reset; +extern button_info_pt default_buttons[]; + +#endif /* IO_MODULE_BUTTONS_H_ */ diff --git a/io-module/gpio.c b/io-module/gpio.c index 9e643a4..a01e979 100644 --- a/io-module/gpio.c +++ b/io-module/gpio.c @@ -43,7 +43,7 @@ struct gpio_pin *gpio_pin_by_attr_name(const char *name) { return NULL; } -static ssize_t mts_attr_show_gpio_pin(struct device *dev, +ssize_t mts_attr_show_gpio_pin(struct device *dev, struct device_attribute *attr, char *buf) { diff --git a/io-module/mths.c b/io-module/mths.c index b323e2b..1fef07f 100644 --- a/io-module/mths.c +++ b/io-module/mths.c @@ -1,372 +1,718 @@ +/* + * mths.c + * + * Created on: Apr 26, 2018 + * Author: leonid + */ + +#include "buttons.h" + static struct gpio_pin gpio_pins_mths_0_0[] = { - { - .name = "RADIO_POWER_MONITOR", - .pin = { - .gpio = AT91_PIN_PA23, - .flags = GPIOF_IN, - .label = "radio-power-monitor", - }, - .active_low = 0, - }, - { - .name = "RADIO_RESET", - .pin = { - .gpio = AT91_PIN_PA22, - .flags = GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN, - .label = "radio-reset", - }, - .active_low = 0, - }, - { - .name = "RADIO_POWER", - .pin = { - .gpio = AT91_PIN_PA21, - .flags = GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN, - .label = "radio-power", - }, - .active_low = 0, - }, - { - .name = "DEVICE_RESET", - .pin = { - .gpio = AT91_PIN_PC4, - .flags = GPIOF_IN, - .label = "reset", - }, - .active_low = 1, - }, - { - .name = "LS_LED", - .pin = { - .gpio = AT91_PIN_PC4, + { + .name = "RADIO_POWER_MONITOR", + .pin = { + .gpio = AT91_PIN_PA23, + .flags = GPIOF_IN, + .label = "radio-power-monitor", + }, + .active_low = 0, + }, + { + .name = "RADIO_RESET", + .pin = { + .gpio = AT91_PIN_PA22, + .flags = GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN, + .label = "radio-reset", + }, + .active_low = 0, + }, + { + .name = "RADIO_POWER", + .pin = { + .gpio = AT91_PIN_PA21, + .flags = GPIOF_OUT_INIT_HIGH | GPIOF_OPEN_DRAIN, + .label = "radio-power", + }, + .active_low = 0, + }, + { + .name = "DEVICE_RESET", + .pin = { + .gpio = AT91_PIN_PC4, + .flags = GPIOF_IN, + .label = "reset", + }, + .active_low = 1, + }, + { + .name = "WPS_BUTTON", + .pin = { + .gpio = AT91_PIN_PA13, + .flags = GPIOF_IN, + .label = "wps_button", + }, + .active_low = 1, + }, + { + .name = "BT_BUTTON", + .pin = { + //TODO return back .gpio = AT91_PIN_PC4, + .gpio = AT91_PIN_PA12, + .flags = GPIOF_IN, + .label = "bt_button", + }, + .active_low = 1, + }, + { + .name = "LS_LED", + .pin = { + .gpio = AT91_PIN_PC4, #if LED_LS_CONTROLLABLE - .flags = GPIOF_OUT_INIT_HIGH, + .flags = GPIOF_OUT_INIT_HIGH, #else - .flags = GPIOF_IN, + .flags = GPIOF_IN, #endif - .label = "led-ls", - }, - .active_low = 1, - }, - { - .name = "STATUS_LED", - .pin = { - .gpio = AT91_PIN_PC16, - .flags = GPIOF_OUT_INIT_LOW, - .label = "led-status", - }, - .active_low = 1, - }, - { - .name = "LED3", - .pin = { - .gpio = AT91_PIN_PC15, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-wifi", - }, - .active_low = 1, - }, - { - .name = "LED3", - .pin = { - .gpio = AT91_PIN_PC15, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-b", - }, - .active_low = 1, - }, - { - .name = "LED4", - .pin = { - .gpio = AT91_PIN_PC20, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-bt", - }, - .active_low = 1, + .label = "led-ls", + }, + .active_low = 1, + }, + { + .name = "LED3", + .pin = { + .gpio = AT91_PIN_PC15, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-wifi", + }, + .active_low = 1, + }, + { + .name = "LED3", + .pin = { + .gpio = AT91_PIN_PC15, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-b", + }, + .active_low = 1, + }, + { + .name = "LED4", + .pin = { + .gpio = AT91_PIN_PC20, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-bt", + }, + .active_low = 1, + }, + { + .name = "LED4", + .pin = { + .gpio = AT91_PIN_PC20, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-cd", + }, + .active_low = 1, + }, + { + .name = "LED5", + .pin = { + .gpio = AT91_PIN_PC16, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-link-status", }, - { - .name = "LED4", - .pin = { - .gpio = AT91_PIN_PC20, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-c", - }, - .active_low = 1, - }, - { - .name = "LED5", - .pin = { - .gpio = AT91_PIN_PC16, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-link-status", - }, - .active_low = 1, - }, - { - .name = "LED5", - .pin = { - .gpio = AT91_PIN_PC16, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-d", - }, - .active_low = 1, - }, - { - .name = "LED6", - .pin = { - .gpio = AT91_PIN_PC19, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-sig1", - }, - .active_low = 1, - }, - { - .name = "LED6", - .pin = { - .gpio = AT91_PIN_PC19, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-e", - }, - .active_low = 1, - }, - { - .name = "LED7", - .pin = { - .gpio = AT91_PIN_PC18, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-sig2", - }, - .active_low = 1, - }, - { - .name = "LED7", - .pin = { - .gpio = AT91_PIN_PC18, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-f", - }, - .active_low = 1, - }, - { - .name = "LED8", - .pin = { - .gpio = AT91_PIN_PC17, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-sig3", - }, - .active_low = 1, - }, - { - .name = "LED8", - .pin = { - .gpio = AT91_PIN_PC17, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "led-g", - }, - .active_low = 1, + .active_low = 1, + }, + { + .name = "LED5", + .pin = { + .gpio = AT91_PIN_PC16, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-d", }, + .active_low = 1, + }, + { + .name = "LED6", + .pin = { + .gpio = AT91_PIN_PC19, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-sig1", + }, + .active_low = 1, + }, + { + .name = "LED6", + .pin = { + .gpio = AT91_PIN_PC19, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-d", + }, + .active_low = 1, + }, + { + .name = "LED7", + .pin = { + .gpio = AT91_PIN_PC18, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-sig2", + }, + .active_low = 1, + }, + { + .name = "LED7", + .pin = { + .gpio = AT91_PIN_PC18, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-e", + }, + .active_low = 1, + }, + { + .name = "LED8", + .pin = { + .gpio = AT91_PIN_PC17, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-sig3", + }, + .active_low = 1, + }, + { + .name = "LED8", + .pin = { + .gpio = AT91_PIN_PC17, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "led-f", + }, + .active_low = 1, + }, + { + .name = "RI_B", + .pin = { + .gpio = AT91_PIN_PC25, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "extserial-ri", + }, + .active_low = 1, + }, + { + .name = "DTR_B", + .pin = { + .gpio = AT91_PIN_PC26, + .flags = GPIOF_IN, + .label = "extserial-dtr", + }, + .active_low = 1, + }, + { + .name = "DSR_B", + .pin = { + .gpio = AT91_PIN_PC27, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "extserial-dsr", + }, + .active_low = 1, + }, + { + .name = "DCD_B", + .pin = { + .gpio = AT91_PIN_PC28, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "extserial-dcd", + }, + .active_low = 1, + }, + + // The difference between MTRv1_0_0 and MTRv1_0_1 starts here + { + .name = "WIFI_BT_ULPWKUP", + .pin = { + .gpio = AT91_PIN_PB9, + .flags = GPIOF_IN, + .label = "wifi-bt-ulpwkup", + } + }, + { + .name = "WIFI_BT_LPWKUP", + .pin = { + .gpio = AT91_PIN_PB11, + .flags = GPIOF_IN, + .label = "wifi-bt-lpwkup", + } + }, + { + .name = "WIFI_BT_INT", + .pin = { + .gpio = AT91_PIN_PB13, + .flags = GPIOF_IN, + .label = "wifi-bt-int", + } + }, + { + .name = "WIFI_BT_RESET", + .pin = { + .gpio = AT91_PIN_PB10, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "wifi-bt-reset", + } + }, + { + .name = "WIFI_BT_LPMODE", + .pin = { + .gpio = AT91_PIN_PB12, + .flags = GPIOF_IN, + .label = "wifi-bt-lpmode", + } + }, + { + .name = "GNSS_RESET", + .pin = { + .gpio = AT91_PIN_PC6, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "gnss-reset", + } + }, + { + .name = "USBHUB_RESET", + .pin = { + .gpio = AT91_PIN_PB6, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "usbhub-reset", + } + }, + { + .name = "GNSS_INT", + .pin = { + .gpio = AT91_PIN_PC7, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "gnss-int", + } + }, + + + { }, +}; - // The difference between MTRv1_0_0 and MTRv1_0_1 starts here - { - .name = "WIFI_BT_ULPWKUP", - .pin = { - .gpio = AT91_PIN_PB9, - .flags = GPIOF_IN, - .label = "wifi-bt-ulpwkup", - } - }, - { - .name = "WIFI_BT_LPWKUP", - .pin = { - .gpio = AT91_PIN_PB11, - .flags = GPIOF_IN, - .label = "wifi-bt-lpwkup", - } - }, - { - .name = "WIFI_BT_INT", // WLAN_IRQ on schematic - .pin = { - .gpio = AT91_PIN_PB13, - .flags = GPIOF_IN, - .label = "wifi-bt-int", - } - }, - { - .name = "WIFI_BT_RESET", - .pin = { - .gpio = AT91_PIN_PB10, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "wifi-bt-reset", - } - }, - { - .name = "WIFI_BT_LPMODE", - .pin = { - .gpio = AT91_PIN_PB12, - .flags = GPIOF_IN, - .label = "wifi-bt-lpmode", - } - }, - { - .name = "GNSS_RESET", - .pin = { - .gpio = AT91_PIN_PC6, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "gnss-reset", - } - }, - { - .name = "USBHUB_RESET", - .pin = { - .gpio = AT91_PIN_PB6, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "usbhub-reset", - } - }, - { - .name = "GNSS_INT", - .pin = { - .gpio = AT91_PIN_PC7, - .flags = GPIOF_OUT_INIT_HIGH, - .label = "gnss-int", - } - }, - { - .name = "WIFI_BUTTON", - .pin = { - .gpio = AT91_PIN_PA13, - .flags = GPIOF_IN, - .label = "wifi-button", - }, - .active_low = 0, - }, - { - .name = "WIFI_BUTTON", - .pin = { - .gpio = AT91_PIN_PA12, - .flags = GPIOF_IN, - .label = "wifi-button", - }, - .active_low = 0, - }, - { }, +button_info_t mths_wps_button = { + .name = "User Push Button", + .label_pin = "wps_button", + .label_monitor = "wps_button-monitor", + .label_monitor_intervals = "wps_button-monitor-intervals", + + /* Signals */ + .short_signal = SIGUSR1, + .long_signal = SIGUSR2, + .short_signal = SIGHUP, + + /* Intervals */ + .short_interval = BUTTON_HOLD_COUNT, + .long_interval = BUTTON_LONG_HOLD_COUNT, +}; + +button_info_t mths_bt_button = { + .name = "User Push Button", + .label_pin = "bt_button", + .label_monitor = "bt_button-monitor", + .label_monitor_intervals = "bt_button-monitor-intervals", + + /* Signals */ + .short_signal = SIGUSR1, + .long_signal = SIGUSR2, + .short_signal = SIGHUP, + + /* Intervals */ + .short_interval = BUTTON_HOLD_COUNT, + .long_interval = BUTTON_LONG_HOLD_COUNT, +}; + +static button_info_pt mths_buttons_0_0[] = { + &reset_button, + &mths_wps_button, + &mths_bt_button, + NULL, }; +static DEVICE_ATTR_MTS(dev_attr_wps_button_monitor_intervals_mths, + mths_wps_button.label_monitor_intervals, + mts_attr_show_button_monitor_intervals, + mts_attr_store_button_monitor_intervals); +static DEVICE_ATTR_MTS(dev_attr_wps_button_monitor_mths, + mths_wps_button.label_monitor, + mts_attr_show_button_monitor, + mts_attr_store_button_monitor); -static DEVICE_ATTR_MTS(dev_attr_radio_power_hs, "radio-power", - mts_attr_show_radio_power, mts_attr_store_radio_power_mtr); +static DEVICE_ATTR_RO_MTS(dev_attr_wps_button_pin_mths, + mths_wps_button.label_pin, + mts_attr_show_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_radio_reset_hs, "radio-reset", - mts_attr_show_gpio_pin, mts_attr_store_radio_reset_mtr); -static DEVICE_ATTR_MTS(dev_attr_eth_reset_hs, "eth-reset", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_bt_button_monitor_intervals_mths, + mths_bt_button.label_monitor_intervals, + mts_attr_show_button_monitor_intervals, + mts_attr_store_button_monitor_intervals); -static DEVICE_ATTR_MTS(dev_attr_bt_enabled_hs, "bt-enabled", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_bt_button_monitor_mths, + mths_bt_button.label_monitor, + mts_attr_show_button_monitor, + mts_attr_store_button_monitor); -static DEVICE_ATTR_MTS(dev_attr_wlan_enabled_hs, "wlan-enabled", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_RO_MTS(dev_attr_bt_button_pin_mths, + mths_bt_button.label_pin, + mts_attr_show_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_wifi_gpio_hs, "led-wifi", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_f_gpio_hs, "led-f", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +/* radio control (power/reset) for mtr */ +static int radio_off_mths(void) +{ + int value; + struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor"); + struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-power"); + struct gpio_pin *rst_pin = gpio_pin_by_attr_name("radio-reset"); -static DEVICE_ATTR_MTS(dev_attr_extserial_ri_gpio_hs, "extserial-ri", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + if (!onoff_pin || !pwrmon_pin || !rst_pin) { + return -ENODEV; + } -static DEVICE_ATTR_RO_MTS(dev_attr_extserial_dtr_hs, "extserial-dtr", - mts_attr_show_gpio_pin); + value = gpio_get_value(pwrmon_pin->pin.gpio); + if(value == 0) { + log_error("radio is already off"); + return -EINVAL; + } -static DEVICE_ATTR_MTS(dev_attr_extserial_dsr_gpio_hs, "extserial-dsr", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + // drive on/off pin low for at least 3 sec + log_info("shutting down radio"); + gpio_set_value(onoff_pin->pin.gpio, 0); -static DEVICE_ATTR_MTS(dev_attr_extserial_dcd_gpio_hs, "extserial-dcd", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + msleep(3500); -static DEVICE_ATTR_RO_MTS(dev_attr_wifi_mac_hs, "mac-wifi", - mts_attr_show_product_info); + // set on/off pin high + gpio_set_value(onoff_pin->pin.gpio, 1); -static DEVICE_ATTR_MTS(dev_attr_wifi_bt_lpwkup_hs, "wifi-bt-lpwkup", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + msleep(200); -static DEVICE_ATTR_MTS(dev_attr_wifi_bt_ulpwkup_hs, "wifi-bt-ulpwkup", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + // check that power is low + value = gpio_get_value(pwrmon_pin->pin.gpio); + if(value != 0) { + log_warning("radio is still on. performing radio reset."); + //Perform Hard Reset + gpio_set_value(rst_pin->pin.gpio, 0); -static DEVICE_ATTR_MTS(dev_attr_wifi_bt_reset_hs, "wifi-bt-reset", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + msleep(500); + + // set pin high + gpio_set_value(rst_pin->pin.gpio, 1); + } else { + log_info("radio has been shut down"); + } + + return 0; +} + +static int radio_on_mths(void) +{ + int value; + struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor"); + struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-power"); + struct gpio_pin *rst_pin = gpio_pin_by_attr_name("radio-reset"); + + if (!onoff_pin || !pwrmon_pin || !rst_pin) { + return -ENODEV; + } + + value = gpio_get_value(pwrmon_pin->pin.gpio); + if(value != 0) { + log_error("radio is already on"); + return -EINVAL; + } + + // drive on/off pin low for at least 5 sec + log_info("turning on radio"); + gpio_set_value(onoff_pin->pin.gpio, 0); + + msleep(5500); + + // set on/off pin high + gpio_set_value(onoff_pin->pin.gpio, 1); + + msleep(200); + + // check that power is high + value = gpio_get_value(pwrmon_pin->pin.gpio); + if(value == 0) { + log_warning("radio is still off. performing radio reset"); + //Perform Hard Reset + gpio_set_value(rst_pin->pin.gpio, 0); + + msleep(500); + + // set pin high + gpio_set_value(rst_pin->pin.gpio, 1); + } else { + log_info("radio has been turned on"); + } + + return 0; +} + +static int radio_reset_mths(void) +{ + struct gpio_pin *onoff_pin = gpio_pin_by_attr_name("radio-power"); + struct gpio_pin *rst_pin = gpio_pin_by_attr_name("radio-reset"); + + if (!rst_pin || !onoff_pin) { + return -ENODEV; + } + + // drive reset pin low for 500ms + gpio_set_value(rst_pin->pin.gpio, 0); + + msleep(500); + + // set pin high + gpio_set_value(rst_pin->pin.gpio, 1); + + // wait for 2 sec before toggling on/off pin + msleep(2000); + + // drive on/off pin low for 6 sec + gpio_set_value(onoff_pin->pin.gpio, 0); + + msleep(6000); + + // set on/off pin high + gpio_set_value(onoff_pin->pin.gpio, 1); + return 0; +} + +static ssize_t mts_attr_store_radio_power_mths(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + int value; + int err; + + if (sscanf(buf, "%i", &value) != 1) { + return -EINVAL; + } + if (value == 0) { + mutex_lock(&mts_io_mutex); + err = radio_off_mths(); + mutex_unlock(&mts_io_mutex); + } else { + mutex_lock(&mts_io_mutex); + err = radio_on_mths(); + mutex_unlock(&mts_io_mutex); + } -static DEVICE_ATTR_RO_MTS(dev_attr_wifi_bt_lpmode_hs, "wifi-bt-lpmode", - mts_attr_show_gpio_pin); + if (err) { + return err; + } -static DEVICE_ATTR_RO_MTS(dev_attr_wifi_bt_int_hs, "wifi-bt-int", - mts_attr_show_gpio_pin); + return count; +} -static DEVICE_ATTR_MTS(dev_attr_gnss_reset_hs, "gnss-reset", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static ssize_t mts_attr_store_radio_reset_mths(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + int value; /* 0 = normal reset; -1 = forced reset */ + int err; + + if (sscanf(buf, "%i", & |
