From c31d619c83ba2a6c7c803082e804d18da657a389 Mon Sep 17 00:00:00 2001 From: John Klug Date: Wed, 23 May 2018 10:14:11 -0500 Subject: Merge in global logic mts-io hotspot code. --- io-module/Makefile | 2 + io-module/buttons.c | 281 ++++++++++++ io-module/buttons.h | 84 ++++ io-module/gpio.c | 2 +- io-module/mths.c | 1016 +++++++++++++++++++++++++++-------------- io-module/mts-io.c | 1256 +++++++++++++++++++++++++++++++++++++++++++++++++++ io-module/mts_io.c | 1243 -------------------------------------------------- io-module/mts_io.h | 10 +- 8 files changed, 2314 insertions(+), 1580 deletions(-) create mode 100644 io-module/buttons.c create mode 100644 io-module/buttons.h create mode 100644 io-module/mts-io.c delete mode 100644 io-module/mts_io.c 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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", &value) != 1) { + return -EINVAL; + } + if (value != 0 && value != -1) { + return -EINVAL; + } + + /* check reset timings is enabled */ + if (value != -1 && NULL != timings_data) { + /* check reset timer is started */ + if (radio_reset_timer_is_start == 1) { + log_info("radio reset timer is running. \n"); + return count; + } + + /* check reset timer available is started */ + if (radio_reset_available_timer_is_start == 1) { + del_timer(&radio_reset_available_timer); + radio_reset_available_timer_is_start = 0; + } + + /* reset timer not started, start it */ + mod_timer(&radio_reset_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000)); + //log_info("radio reset timer is start = [%d]\n", time_now_secs()); + /* save timings_data_stop_seconds */ + timings_data_stop_seconds = timings_data[timings_data_index] + time_now_secs(); + radio_reset_timer_is_start = 1; + } + + log_info("radio is reset\n"); + + mutex_lock(&mts_io_mutex); + + err = radio_reset_mths(); + + mutex_unlock(&mts_io_mutex); + + if (err) { + return err; + } + + return count; +} + +static ssize_t mts_attr_show_radio_power_mths(struct device *dev, + struct device_attribute *attr, char *buf) +{ + int value; + struct gpio_pin *pwrmon_pin = gpio_pin_by_attr_name("radio-power-monitor"); + + if ( !pwrmon_pin ) { + return -ENODEV; + } + + value = gpio_get_value(pwrmon_pin->pin.gpio); + return sprintf(buf, "%d\n", value); +} + +static DEVICE_ATTR_MTS(dev_attr_radio_power_mths, "radio-power", + mts_attr_show_radio_power_mths, mts_attr_store_radio_power_mths); + +static DEVICE_ATTR_MTS(dev_attr_radio_reset_mths, "radio-reset", + mts_attr_show_gpio_pin, mts_attr_store_radio_reset_mths); + + +static DEVICE_ATTR_MTS(dev_attr_led_wifi_gpio_mths, "led-wifi", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_led_f_gpio_mths, "led-f", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_extserial_ri_gpio_mths, "extserial-ri", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_RO_MTS(dev_attr_extserial_dtr_mths, "extserial-dtr", + mts_attr_show_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_extserial_dsr_gpio_mths, "extserial-dsr", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_extserial_dcd_gpio_mths, "extserial-dcd", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_RO_MTS(dev_attr_wifi_mac_mths, "mac-wifi", + mts_attr_show_product_info); + +static DEVICE_ATTR_MTS(dev_attr_wifi_bt_lpwkup_mths, "wifi-bt-lpwkup", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_wifi_bt_ulpwkup_mths, "wifi-bt-ulpwkup", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_wifi_bt_reset_mths, "wifi-bt-reset", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_RO_MTS(dev_attr_wifi_bt_lpmode_mths, "wifi-bt-lpmode", + mts_attr_show_gpio_pin); + +static DEVICE_ATTR_RO_MTS(dev_attr_wifi_bt_int_mths, "wifi-bt-int", + mts_attr_show_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_gnss_reset_mths, "gnss-reset", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_usbhub_reset_mths, "usbhub-reset", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_RO_MTS(dev_attr_gnss_int_mths, "gnss-int", + mts_attr_show_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_usbhub_reset_hs, "usbhub-reset", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_RO_MTS(dev_attr_gnss_int_hs, "gnss-int", - mts_attr_show_gpio_pin); static struct attribute *mths_0_0_platform_attributes[] = { - &dev_attr_vendor_id.attr, - &dev_attr_product_id.attr, - &dev_attr_device_id.attr, - &dev_attr_uuid.attr, - &dev_attr_hw_version.attr, - &dev_attr_imei.attr, - &dev_attr_eth_mac.attr, - &dev_attr_has_radio.attr, - &dev_attr_wifi_mac_hs.attr, - - &dev_attr_reset.attr, - &dev_attr_reset_monitor.attr, - &dev_attr_reset_monitor_intervals.attr, - &dev_attr_radio_power_hs.attr, - &dev_attr_radio_reset_hs.attr, - - &dev_attr_radio_reset_backoffs.attr, - &dev_attr_radio_reset_backoff_index.attr, - &dev_attr_radio_reset_backoff_seconds.attr, - - &dev_attr_extserial_ri_gpio_hs.attr, - &dev_attr_extserial_dtr_hs.attr, - &dev_attr_extserial_dsr_gpio_hs.attr, - &dev_attr_extserial_dcd_gpio_hs.attr, - - &dev_attr_eth_reset_hs.attr, - &dev_attr_wifi_bt_lpwkup_hs.attr, - &dev_attr_wifi_bt_ulpwkup_hs.attr, - &dev_attr_wifi_bt_reset_hs.attr, - &dev_attr_wifi_bt_lpmode_hs.attr, - &dev_attr_wifi_bt_int_hs.attr, - &dev_attr_gnss_reset_hs.attr, - &dev_attr_usbhub_reset_hs.attr, - &dev_attr_gnss_int_hs.attr, - - &dev_attr_led_status.attr, - &dev_attr_led_cd_gpio.attr, - &dev_attr_led_sig1_gpio.attr, - &dev_attr_led_sig2_gpio.attr, - &dev_attr_led_sig3_gpio.attr, - &dev_attr_led_wifi_gpio_hs.attr, - - &dev_attr_led_b_gpio.attr, - &dev_attr_led_c_gpio.attr, - &dev_attr_led_d_gpio.attr, - &dev_attr_led_e_gpio.attr, - &dev_attr_led_f_gpio_hs.attr, - - NULL, + &dev_attr_vendor_id.attr, + &dev_attr_product_id.attr, + &dev_attr_device_id.attr, + &dev_attr_uuid.attr, + &dev_attr_hw_version.attr, + &dev_attr_imei.attr, + &dev_attr_wifi_mac_mths.attr, + &dev_attr_has_radio.attr, + + &dev_attr_reset.attr, + &dev_attr_reset_monitor.attr, + &dev_attr_reset_monitor_intervals.attr, + &dev_attr_wps_button_pin_mths.attr, + &dev_attr_wps_button_monitor_mths.attr, + &dev_attr_wps_button_monitor_intervals_mths.attr, + &dev_attr_bt_button_pin_mths.attr, + &dev_attr_bt_button_monitor_mths.attr, + &dev_attr_bt_button_monitor_intervals_mths.attr, + + &dev_attr_radio_power_mths.attr, + &dev_attr_radio_reset_mths.attr, + + &dev_attr_radio_reset_backoffs.attr, + &dev_attr_radio_reset_backoff_index.attr, + &dev_attr_radio_reset_backoff_seconds.attr, + + &dev_attr_extserial_ri_gpio_mths.attr, + &dev_attr_extserial_dtr_mths.attr, + &dev_attr_extserial_dsr_gpio_mths.attr, + &dev_attr_extserial_dcd_gpio_mths.attr, + + &dev_attr_wifi_bt_lpwkup_mths.attr, + &dev_attr_wifi_bt_ulpwkup_mths.attr, + &dev_attr_wifi_bt_reset_mths.attr, + &dev_attr_wifi_bt_lpmode_mths.attr, + &dev_attr_wifi_bt_int_mths.attr, + &dev_attr_gnss_reset_mths.attr, + &dev_attr_usbhub_reset_mths.attr, + &dev_attr_gnss_int_mths.attr, + + &dev_attr_led_status.attr, + &dev_attr_led_cd_gpio.attr, + &dev_attr_led_sig1_gpio.attr, + &dev_attr_led_sig2_gpio.attr, + &dev_attr_led_sig3_gpio.attr, + &dev_attr_led_wifi_gpio_mths.attr, + + &dev_attr_led_b_gpio.attr, + &dev_attr_led_c_gpio.attr, + &dev_attr_led_d_gpio.attr, + &dev_attr_led_e_gpio.attr, + &dev_attr_led_f_gpio_mths.attr, + + NULL, }; static struct attribute_group mths_0_0_platform_attribute_group = { - .attrs = mths_0_0_platform_attributes + .attrs = mths_0_0_platform_attributes }; +#ifdef NEW_BUTTON_INTERFACE + +//static const button_info_t user_button_mths = { +// .name = "User Push Button", +// .label_pin = "button", +// .label_monitor = "button-monitor", +// .label_monitor_intervals = "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 const button_info_t mths_buttons_0_0[] = { +// user_button_mths, +// {}, +//}; + + + + + +#endif //NEW_BUTTON_INTERFACE diff --git a/io-module/mts-io.c b/io-module/mts-io.c new file mode 100644 index 0000000..e51bd4b --- /dev/null +++ b/io-module/mts-io.c @@ -0,0 +1,1256 @@ +/* + * MTS-IO Controller + * + * Copyright (C) 2014 by Multi-Tech Systems + * Copyright (C) 2016 by Multi-Tech Systems + * + * Authors: James Maki + * Jesse Gilles + * Mike Fiore + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mts_io.h" +#include "buttons.h" + +#define PLATFORM_NAME "mts-io" + +#define LED_LS_CONTROLLABLE 0 + +/* on-board EEPROM */ +extern uint8_t mts_id_eeprom[512]; +static struct mts_id_eeprom_layout id_eeprom; + +// NUM_AP should be defined from the board code +// it should be set to the value of CONFIG_MTS_NUM_ACCESSORY_PORTS +// arch/arm/mach-at91/board-dt-sam9.c +// if it is 0 or undefined, there is no accessory card support on this HW +#ifdef CONFIG_MTS_NUM_ACCESSORY_PORTS + +#ifndef NUM_AP +#define NUM_AP CONFIG_MTS_NUM_ACCESSORY_PORTS +#endif + +#else +#define NUM_AP 0 +#endif + +static uint8_t mts_hw_version; +static struct platform_device *mts_io_platform_device; +static struct attribute_group *attr_group; +static struct attribute_group *attr_group_lora; // on-board lora peripheral to be stored in the lora/ sub-directory +static struct gpio_pin *gpio_pins; + +DEFINE_MUTEX(mts_io_mutex); + +static unsigned int *timings_data = NULL; +static unsigned int timings_data_size = 0; +static unsigned int timings_data_index = 0; +static time_t timings_data_stop_seconds = 0; +static struct timer_list radio_reset_timer; +static volatile int radio_reset_timer_is_start = 0; +static struct timer_list radio_reset_available_timer; +static volatile int radio_reset_available_timer_is_start = 0; +static time_t time_now_secs(void); +static void radio_reset_available_timer_callback(unsigned long data); +static void radio_reset_timer_callback(unsigned long data); + +/* generic GPIO support */ +#include "gpio.c" + +/* reset button handling */ +#define RESET_CHECK_PER_SEC 8 +#define RESET_INTERVAL (HZ / RESET_CHECK_PER_SEC) +#define RESET_HOLD_COUNT (RESET_CHECK_PER_SEC * 3) +#define RESET_LONG_HOLD_COUNT (RESET_CHECK_PER_SEC * 30) + +static pid_t reset_pid = -1; +static pid_t reset_count = 0; +bool sent_extra_long = false; +static int reset_short_signal = SIGUSR1; +static int reset_long_signal = SIGUSR2; +static int reset_extra_long_signal = SIGHUP; +static int reset_short_interval = RESET_HOLD_COUNT; +static int reset_long_interval = RESET_LONG_HOLD_COUNT; + +/* + * This function takes the product_id and tries to check + * for a modem. If there is an error, assume there is a + * modem. Saying there is a modem when there is not + * just results in a slower boot. + * If no hyphen in product ID, or no product ID, + * assume we have a radio. + * If there is a hyphen test the character after the + * first hyphen: + * If the character is numeric, we have no modem. + * if the chracter is not a B, we have a modem. + * If the character is a B, and it is followed + * by a numeric, we have no modem. + * If the B is the last character or is followed + * by a numeric, we have no modem. + * All other cases, we have a modem. + */ +static int +has_radio(const char *product_id, size_t len) +{ + char *p; + if (!product_id || ! *product_id) + return 1; /* No Product ID? */ + p = memchr(product_id,'-',len); + if (p) { /* Found a hyphen */ + log_debug("Found hyphen"); + p++; + if (p >= product_id+len) { + log_debug("End of string -- hyphen"); + return 1; /* Last character was hyphen */ + } + if (isdigit(*p)) { + log_debug("Found digit after hypen"); + return 0; /* Modem name never starts with a digit */ + } + if (*p != 'B') { + log_debug("Found neither B nor digit after hypen"); + return 1; /* Modem starting with a letter, but not B */ + } + /* Found a B */ + p++; + if (p >= product_id+len) { + log_debug("B at end of product-id string"); + return 1; /* Last character was B */ + } + if (isdigit(*p)) { + log_debug("B followed by digit after hyphen - no modem"); + return 0; /* B[numeric] is MTR Build number */ + } + log_debug("B followed by non-digit after hyphen - has modem"); + return 1; /* B[non-numeric] so assume a modem that starts with B */ + } /* End of found hyphen case */ + log_debug("Undefined product-id - has modem"); + return 1; /* Product id invalid or empty, so instantiate a radio anyway */ +} + + +#ifndef NEW_BUTTON_INTERFACE +static void reset_callback(struct work_struct *ignored); + +static DECLARE_DELAYED_WORK(reset_work, reset_callback); +static void reset_callback(struct work_struct *ignored) +{ + struct gpio_pin *pin; + int reset_pressed = 0; + struct pid *vpid = NULL; + + mutex_lock(&mts_io_mutex); + + pin = gpio_pin_by_attr_name("reset"); + if (pin) { + reset_pressed = !gpio_get_value(pin->pin.gpio); + } + + if (reset_pid > 0) { + vpid = find_vpid(reset_pid); + } + + if (vpid) { + if (reset_pressed) { + reset_count++; + } else { + //Reset button has not been pressed + if (reset_count > 0 && reset_count < reset_short_interval) { + kill_pid(vpid, reset_short_signal, 1); + } else if (reset_count >= reset_short_interval && reset_count < reset_long_interval) { + kill_pid(vpid, reset_long_signal, 1); + } + + reset_count = 0; + sent_extra_long = false; + } + if (reset_count >= reset_long_interval && ! sent_extra_long) { + kill_pid(vpid, reset_extra_long_signal, 1); + sent_extra_long = true; + } + } else { + reset_count = 0; + } + + mutex_unlock(&mts_io_mutex); + + schedule_delayed_work(&reset_work, RESET_INTERVAL); +} + +static ssize_t mts_attr_show_reset_monitor_intervals(struct device *dev, struct device_attribute *attr, char *buf) +{ + int ret; + + mutex_lock(&mts_io_mutex); + + ret = sprintf(buf, "%d %d\n", reset_short_interval / RESET_CHECK_PER_SEC, reset_long_interval / RESET_CHECK_PER_SEC); + + mutex_unlock(&mts_io_mutex); + + return ret; +} + +static ssize_t mts_attr_store_reset_monitor_intervals(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) +{ + int short_int; + int long_int; + + if (sscanf(buf, "%i %i", &short_int, &long_int) != 2) { + return -EINVAL; + } + + mutex_lock(&mts_io_mutex); + + reset_short_interval = short_int * RESET_CHECK_PER_SEC; + reset_long_interval = long_int * RESET_CHECK_PER_SEC; + + mutex_unlock(&mts_io_mutex); + + return count; +} + +static DEVICE_ATTR_MTS(dev_attr_reset_monitor_intervals, "reset-monitor-intervals", + mts_attr_show_reset_monitor_intervals, mts_attr_store_reset_monitor_intervals); + +static ssize_t mts_attr_show_reset_monitor(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int ret; + + mutex_lock(&mts_io_mutex); + + ret = sprintf(buf, "%d %d %d %d\n", reset_pid, reset_short_signal, reset_long_signal, reset_extra_long_signal); + + mutex_unlock(&mts_io_mutex); + + return ret; +} + +static ssize_t mts_attr_store_reset_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); + + if (result < 3 || result > 4) { + return -EINVAL; + } + + if(result == 3) { + mutex_lock(&mts_io_mutex); + + reset_pid = pid; + reset_short_signal = short_signal; + reset_long_signal = long_signal; + + mutex_unlock(&mts_io_mutex); + } else { + mutex_lock(&mts_io_mutex); + + reset_pid = pid; + reset_short_signal = short_signal; + reset_long_signal = long_signal; + reset_extra_long_signal = extra_long_signal; + + mutex_unlock(&mts_io_mutex); + } + + return count; +} + +static DEVICE_ATTR_MTS(dev_attr_reset_monitor, "reset-monitor", + mts_attr_show_reset_monitor, mts_attr_store_reset_monitor); +static DEVICE_ATTR_RO_MTS(dev_attr_reset, "reset", mts_attr_show_gpio_pin); +#endif +/* active-low socket modem reset */ +static ssize_t mts_attr_store_radio_reset(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + int value; /* 0 = normal reset; -1 = forced reset */ + int err; + struct gpio_pin *pin; + + if (sscanf(buf, "%i", &value) != 1) { + return -EINVAL; + } + if (value != 0 && value != -1) { + return -EINVAL; + } + + /* check reset timings is enabled */ + if (value != -1 && NULL != timings_data) { + /* check reset timer is started */ + if (radio_reset_timer_is_start == 1) { + log_info("radio reset timer is running. \n"); + return count; + } + + /* check reset timer available is started */ + if (radio_reset_available_timer_is_start == 1) { + del_timer(&radio_reset_available_timer); + radio_reset_available_timer_is_start = 0; + } + + /* reset timer not started, start it */ + mod_timer(&radio_reset_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000)); + //log_info("radio reset timer is start = [%d]\n", time_now_secs()); + /* save timings_data_stop_seconds */ + timings_data_stop_seconds = timings_data[timings_data_index] + time_now_secs(); + radio_reset_timer_is_start = 1; + } + + log_info("radio is reset\n"); + + pin = gpio_pin_by_attr_name("radio-reset"); + + if (!pin) { + return -ENODEV; + } + + mutex_lock(&mts_io_mutex); + + // 250ms low reset + err = reset_gpio_pin(pin, 250, 0); + + mutex_unlock(&mts_io_mutex); + + if (err) { + return err; + } + + return count; +} + +static DEVICE_ATTR_MTS(dev_attr_radio_reset, "radio-reset", + mts_attr_show_gpio_pin, mts_attr_store_radio_reset); + +/* shared gpio attributes */ +static DEVICE_ATTR_MTS(dev_attr_radio_power, "radio-power", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +/* backoff-timers */ +static time_t time_now_secs(void) +{ + struct timespec ts = current_kernel_time(); + return ts.tv_sec; +} + +static void radio_reset_available_timer_callback( unsigned long data ) +{ + /* do your timer stuff here */ + //log_info("radio_reset_available_timer_callback\n"); + //log_info("radio reset available timer is stop = [%d]\n", time_now_secs()); + + /* zero timings_data_index */ + timings_data_index = 0; + //log_info("timings data index is zero = [%d]\n", timings_data_index); + radio_reset_available_timer_is_start = 0; +} + +static void radio_reset_timer_callback( unsigned long data ) +{ + /* do your timer stuff here */ + //log_info("radio_reset_timer_callback\n"); + //log_info("radio reset timer is stop = [%d]\n", time_now_secs()); + + /* increment timings_data_index */ + timings_data_index++; + if(timings_data_index >= timings_data_size) { + timings_data_index = timings_data_size-1; + } + + //log_info("timings data index = [%d]\n", timings_data_index); + + /* reset available timer not started, start it */ + mod_timer(&radio_reset_available_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000)); + //log_info("radio reset available timer is start = [%d]\n", time_now_secs()); + radio_reset_available_timer_is_start = 1; + radio_reset_timer_is_start = 0; +} + +static ssize_t mts_attr_store_radio_reset_backoffs(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + char *timings_data_str = NULL; + const char delimiter[] = " "; + char * pch = NULL; + unsigned int size = 0; + + /* free previous timings_data */ + if (NULL != timings_data) { + /* stop timers */ + del_timer(&radio_reset_timer); + del_timer(&radio_reset_available_timer); + timings_data_index = 0; + radio_reset_timer_is_start = 0; + radio_reset_available_timer_is_start = 0; + + //log_info("free previous timings_data\n"); + kfree(timings_data); + timings_data = NULL; + timings_data_size = 0; + } + + /* make a copy */ + if( NULL == (timings_data_str = kzalloc((strlen(buf) + 1), GFP_KERNEL)) ){ + log_error("can`t allocate memory\n"); + return -EINVAL; + } + + //log_info("radio_reset_backoffs buf: [%s]", buf); + strncpy(timings_data_str, buf, (strlen(buf) + 1)); + + /* get number of tokens */ + while (NULL != (pch = strsep (&timings_data_str, delimiter))) { + int value = 0; + sscanf(pch, "%d", &value); + //log_info("radio reset backoffs pch = [%s]\n", pch); + if (value > 0){ + size++; + if (NULL == timings_data) { + /* make alloc */ + if (NULL == (timings_data = kmalloc(sizeof(unsigned int), GFP_KERNEL))) { + log_error("radio reset backoffs can`t allocate memory\n"); + goto free; + } + } else { + /* make realloc */ + if (NULL == (timings_data = krealloc(timings_data, size * sizeof(unsigned int), GFP_KERNEL))) { + log_error("radio reset backoffs can`t allocate memory\n"); + goto free; + } + } + /* save timings data */ + sscanf(pch, "%d", &timings_data[size-1]); + } + } + + timings_data_size = size; + //log_info("timings_data_size = %d\n", timings_data_size); + + if (NULL != timings_data_str) { + /* free timings_data_str */ + /* never get here in happy path */ + kfree(timings_data_str); + } + return count; + +free: + if (NULL != timings_data_str) { + /* free timings_data_str */ + kfree(timings_data_str); + } + + if (NULL != timings_data) { + kfree(timings_data); + timings_data = NULL; + timings_data_size = 0; + } + return -EINVAL; +} + +static ssize_t mts_attr_store_radio_reset_backoffs_index(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + int value; + + if (sscanf(buf, "%d", &value) != 1) { + return -EINVAL; + } + + if ((value < 0) || (value >= timings_data_size)) { + log_error("incorrect data\n"); + return -EINVAL; + } + + /* stop timers */ + del_timer(&radio_reset_timer); + del_timer(&radio_reset_available_timer); + radio_reset_timer_is_start = 0; + radio_reset_available_timer_is_start = 0; + timings_data_index = value; + + return count; +} + +static ssize_t mts_attr_show_radio_reset_backoffs(struct device *dev, + struct device_attribute *attr, char *buf) +{ + int ret = 0; + size_t i = 0; + size_t buf_left = 0; + + if (NULL != timings_data) { + for(i = 0; i < timings_data_size; ++i) { + buf_left = PAGE_SIZE - ret; + ret += snprintf(buf += strlen(buf), buf_left, "%d ", timings_data[i]); + } + } + + if (ret > 0) { + ret -= 1; + } + + return ret; +} + +static ssize_t mts_attr_show_radio_reset_backoff_index(struct device *dev, + struct device_attribute *attr, char *buf) +{ + ssize_t value; + + if (strcmp(attr->attr.name, "radio-reset-backoff-index") == 0) { + value = sprintf(buf, "%d", timings_data_index); + } + else { + log_error("attribute '%s' not found", attr->attr.name); + value = -1; + } + + return value; +} + +static ssize_t mts_attr_show_radio_reset_backoff_seconds(struct device *dev, + struct device_attribute *attr, char *buf) +{ + ssize_t value; + + if (strcmp(attr->attr.name, "radio-reset-backoff-seconds") == 0) { + if (radio_reset_timer_is_start == 1) { + value = sprintf(buf, "%lu", (timings_data_stop_seconds - time_now_secs())); + } else { + value = sprintf(buf, "%d", 0); + } + } else { + log_error("attribute '%s' not found", attr->attr.name); + value = -1; + } + + return value; +} + +static DEVICE_ATTR_MTS(dev_attr_radio_reset_backoffs, "radio-reset-backoffs", + mts_attr_show_radio_reset_backoffs, mts_attr_store_radio_reset_backoffs); + +static DEVICE_ATTR_MTS(dev_attr_radio_reset_backoff_index, "radio-reset-backoff-index", + mts_attr_show_radio_reset_backoff_index, mts_attr_store_radio_reset_backoffs_index); + +static DEVICE_ATTR_RO_MTS(dev_attr_radio_reset_backoff_seconds, "radio-reset-backoff-seconds", + mts_attr_show_radio_reset_backoff_seconds); + +/* shared gpio-based LEDs */ +static DEVICE_ATTR_MTS(dev_attr_led_status, "led-status", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_led_a_gpio, "led-a", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +#if LED_LS_CONTROLLABLE +static DEVICE_ATTR_MTS(dev_attr_led_ls, "led-ls", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +#else +static DEVICE_ATTR_RO_MTS(dev_attr_led_ls, "led-ls", + mts_attr_show_gpio_pin); +#endif + +static DEVICE_ATTR_MTS(dev_attr_led_b_gpio, "led-b", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_led_cd_gpio, "led-cd", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_led_c_gpio, "led-c", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_led_sig1_gpio, "led-sig1", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_led_sig2_gpio, "led-sig2", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_led_sig3_gpio, "led-sig3", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +static DEVICE_ATTR_MTS(dev_attr_led_d_gpio, "led-d", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_led_e_gpio, "led-e", + mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); + +/* eeprom info */ +static ssize_t mts_attr_show_product_info(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int i; + ssize_t value; + + if (strcmp(attr->attr.name, "vendor-id") == 0) { + value = sprintf(buf, "%.32s\n", id_eeprom.vendor_id); + } else if (strcmp(attr->attr.name, "product-id") == 0) { + value = sprintf(buf, "%.32s\n", id_eeprom.product_id); + } else if (strcmp(attr->attr.name, "has-radio") == 0) { + value = sprintf(buf, "%1d\n", + has_radio(id_eeprom.product_id,sizeof id_eeprom.product_id)); + } else if (strcmp(attr->attr.name, "device-id") == 0) { + value = sprintf(buf, "%.32s\n", id_eeprom.device_id); + } else if (strcmp(attr->attr.name, "uuid") == 0) { + //Loop Through UUID Bytes and print them in HEX + + for(i = 0; i < 16; i++) { + value = sprintf(buf, "%02X", id_eeprom.uuid[i]); + if(value == -1) { + return value; + } + buf += value; + } + value = sprintf(buf, "\n"); + if(value == -1) { + return value; + } + value = 33; //16*2 (ASCII HEX) + 1 ('\n') + } else if (strcmp(attr->attr.name, "hw-version") == 0) { + value = sprintf(buf, "%.32s\n", id_eeprom.hw_version); + } else if (strcmp(attr->attr.name, "imei") == 0) { + value = sprintf(buf, "%.32s\n", id_eeprom.imei); + } else if (strcmp(attr->attr.name, "mac-wifi") == 0) { + value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", + id_eeprom.mac_wifi[0], + id_eeprom.mac_wifi[1], + id_eeprom.mac_wifi[2], + id_eeprom.mac_wifi[3], + id_eeprom.mac_wifi[4], + id_eeprom.mac_wifi[5]); + } else if (strcmp(attr->attr.name, "mac-eth") == 0) { + value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", + id_eeprom.mac_addr[0], + id_eeprom.mac_addr[1], + id_eeprom.mac_addr[2], + id_eeprom.mac_addr[3], + id_eeprom.mac_addr[4], + id_eeprom.mac_addr[5]); + } else if (strcmp(attr->attr.name, "lora-eui") == 0) { + value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", + id_eeprom.lora_eui[0], + id_eeprom.lora_eui[1], + id_eeprom.lora_eui[2], + id_eeprom.lora_eui[3], + id_eeprom.lora_eui[4], + id_eeprom.lora_eui[5], + id_eeprom.lora_eui[6], + id_eeprom.lora_eui[7]); + } + else { + log_error("attribute '%s' not found", attr->attr.name); + value = -1; + } + + return value; +} + +static DEVICE_ATTR_RO_MTS(dev_attr_vendor_id, "vendor-id", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_product_id, "product-id", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_has_radio, "has-radio", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_device_id, "device-id", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_uuid, "uuid", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_hw_version, "hw-version", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_imei, "imei", + mts_attr_show_product_info); +static DEVICE_ATTR_RO_MTS(dev_attr_eth_mac, "mac-eth", + mts_attr_show_product_info); + +static int get_radio_model_from_product_id(void) { + int rc = RADIO_UNKNOWN; + + if (strstr(id_eeprom.product_id, "LEU1")) rc = RADIO_LEU1; + else if (strstr(id_eeprom.product_id, "LNA3")) rc = RADIO_LNA3; + + // Add other radios as needed. + return rc; +} + + +/* include on-board lora peripheral */ +#include "mts_lora.c" + +/* include per-device pins and attributes */ +#include "mtcdt.c" +#include "mtcap.c" +#include "mtr.c" +#include "mths.c" + +/* include capabilities sub-directory support */ +#include "mts_capab.c" + + +#if NUM_AP > 0 + +/* accessory card EEPROMs */ +extern uint8_t mts_ap_eeprom[NUM_AP][512]; +static struct mts_ap_eeprom_layout ap_eeprom[NUM_AP]; +/* kobject pointers for the apX subdirectories that correspond to the accessory ports */ +static struct kobject *ap_subdirs[NUM_AP]; +/* attribute groups for the accessory ports*/ +static struct attribute_group ap_attr_groups[NUM_AP]; +/* info for accessory port (contains function pointers for setup and teardown and and useful info) */ +static struct ap_info* port_info[NUM_AP]; + +/* accessory card support */ +#include "mtac.c" +#include "mtac_gpiob.c" +#include "mtac_mfser.c" +#include "mtac_eth.c" +#include "mtac_lora.c" + +static bool load_port(int port) { + int port_index = port - 1; + memcpy(&ap_eeprom[port_index], mts_ap_eeprom[port_index], sizeof(mts_ap_eeprom[port_index])); + + if (mts_ap_eeprom[port_index][0] == 0xFF) { + log_error("uninitialized eeprom on accessory card %d", port); + } else if (mts_ap_eeprom[port_index][0] == 0x00) { + log_info("no accessory card inserted in port %d", port); + } else { + port_info[port_index] = kzalloc(sizeof(struct ap_info), GFP_KERNEL); + if (! port_info[port_index]) { + log_error("alloc of port info failed"); + return false; + } + + if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_GPIOB)) { + if (! set_gpiob_info(port_info[port_index])) { + log_error("failed to set up gpiob port info"); + return false; + } + } else if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_MFSER)) { + if (! set_mfser_info(port_info[port_index])) { + log_error("failed to set up mfser port info"); + return false; + } + } else if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_ETH)) { + if (! set_eth_info(port_info[port_index])) { + log_error("failed to set up eth port info"); + return false; + } + } else if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_LORA)) { + if (! set_lora_info(port_info[port_index])) { + log_error("failed to set up lora port info"); + return false; + } + } else { + log_error("unknown accessory card [%s] in port %d", ap_eeprom[port_index].product_id, port); + kfree(port_info[port_index]); + port_info[port_index] = NULL; + return false; + } + + log_info("accessory card %d vendor-id: %.32s", port, ap_eeprom[port_index].vendor_id); + log_info("accessory card %d product-id: %.32s", port, ap_eeprom[port_index].product_id); + log_info("accessory card %d device-id: %.32s", port, ap_eeprom[port_index].device_id); + log_info("accessory card %d hw-version: %.32s", port, ap_eeprom[port_index].hw_version); + if (strncmp(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_ETH, strlen(PRODUCT_ID_MTAC_ETH)) == 0) { + log_info("accessory card %d mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", + port, + ap_eeprom[port_index].mac_addr[0], + ap_eeprom[port_index].mac_addr[1], + ap_eeprom[port_index].mac_addr[2], + ap_eeprom[port_index].mac_addr[3], + ap_eeprom[port_index].mac_addr[4], + ap_eeprom[port_index].mac_addr[5]); + } + if (strncmp(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_LORA, strlen(PRODUCT_ID_MTAC_LORA)) == 0) { + log_info("accessory card %d eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", + port, + ap_eeprom[port_index].eui[0], + ap_eeprom[port_index].eui[1], + ap_eeprom[port_index].eui[2], + ap_eeprom[port_index].eui[3], + ap_eeprom[port_index].eui[4], + ap_eeprom[port_index].eui[5], + ap_eeprom[port_index].eui[6], + ap_eeprom[port_index].eui[7]); + } + + if (! port_info[port_index]->setup(port)) { + log_error("accessory port %d setup failed", port); + port_info[port_index]->teardown(port); + kfree(port_info[port_index]); + port_info[port_index] = NULL; + return false; + } + } + + return true; +} + +static void init_accessory_ports(void) +{ + int port_index; + + for (port_index = 0; port_index < NUM_AP; port_index++) { + port_info[port_index] = NULL; + if (! load_port(port_index+1)) { + log_error("failed to load accessory card in port %d", port_index); + } + } +} + +static void teardown_accessory_ports(void) +{ + int port_index; + + for (port_index = 0; port_index < NUM_AP; port_index++) { + if (port_info[port_index]) { + port_info[port_index]->teardown(port_index+1); + kfree(port_info[port_index]); + } + } +} +#else /* NUM_AP > 0 */ +static void init_accessory_ports(void) {} +static void teardown_accessory_ports(void) {} +#endif + +static void init_ports(void) +{ + if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) && attr_group_lora) { + mts_load_lora_port(); + } + + init_accessory_ports(); +} + +static void teardown_ports(void) +{ + if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) && attr_group_lora) { + mts_teardown_lora_port(); + } + + teardown_accessory_ports(); +} + + +struct attribute *freelater = NULL; // Storage to free when driver is unloaded. + +static int +mts_id_eeprom_load(void) +{ + int i, j = 0; + char buf[64] = {0}; + char* ptr; + int attr_blength; // Byte length of base attribute array + int current_blength; // Current length in bytes of attribute array + int current_count; // Number of items in array + struct attribute **all_attrs = NULL; + char *tmp; + int noradio; + + //The mts_id_eeprom buffer is initialize once on boot + //reloading the mts_io.ko module will not reinitialize this buffer + //only rebooting will reinitialize this buffer + memcpy(&id_eeprom, mts_id_eeprom, sizeof(mts_id_eeprom)); + + if (mts_id_eeprom[0] == 0xFF) { + log_error("uninitialized eeprom"); + return -EIO; + } + + noradio = ! has_radio(id_eeprom.product_id,sizeof id_eeprom.product_id); + log_debug("mts_id_eeprom: noradio=%d",noradio); + + if (((tmp=HW_VERSION_MTCAP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) || + ((tmp=HW_VERSION_MTCAP_0_1),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0)) { + /* See if we have no radio, and if so, prune out the stuff that follows */ + if(noradio) { + struct attribute **ap = mtcap_0_0_platform_attribute_group.attrs; + while(1) { + if(ap[j] == NULL) { + log_info("Did not find radio power attribute. Possible driver fault."); + break; + } + j++; + if (is_radio_power_attr_mtcap(ap[j])) { + log_info("Pruning radio feature from mts-io",j); + ap[j] = NULL; + break; + } + } + } + attr_group = &mtcap_0_0_platform_attribute_group; + gpio_pins = gpio_pins_mtcap_0_0; + set_buttons(default_buttons); + if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) { + attr_group_lora = &mtcap_0_0_lora_attribute_group; + } + log_info("detected board %s", tmp); + } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTR_0_0, strlen(HW_VERSION_MTR_0_0)) == 0) { + attr_group = &mtr_platform_attribute_group; + gpio_pins = gpio_pins_mtr_0_0; + set_buttons(default_buttons); + mts_hw_version = MTR_0_0; + log_info("detected board %s", HW_VERSION_MTR_0_0); + } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTR_0_1, strlen(HW_VERSION_MTR_0_1)) == 0) { + attr_group = &mtr_platform_attribute_group; + gpio_pins = gpio_pins_mtr_0_1; + set_buttons(default_buttons); + mts_hw_version = MTR_0_1; + log_info("detected board %s", HW_VERSION_MTR_0_1); + } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_0, strlen(HW_VERSION_MTRV1_0_0)) == 0) { + attr_group = &mtr_platform_attribute_group; + gpio_pins = gpio_pins_mtrv1_0_0; + set_buttons(default_buttons); + mts_hw_version = MTRV1_0_0; + log_info("detected board %s", HW_VERSION_MTRV1_0_0); + } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_1, strlen(HW_VERSION_MTRV1_0_1)) == 0) { + attr_group = &mtrv1_0_1_platform_attribute_group; + gpio_pins = gpio_pins_mtrv1_0_1; + set_buttons(default_buttons); + mts_hw_version = MTRV1_0_1; + log_info("detected board %s", HW_VERSION_MTRV1_0_1); + } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_2, strlen(HW_VERSION_MTRV1_0_2)) == 0) { + attr_group = &mtrv1_0_2_platform_attribute_group; + gpio_pins = gpio_pins_mtrv1_0_2; + set_buttons(default_buttons); + mts_hw_version = MTRV1_0_2; + log_info("detected board %s", HW_VERSION_MTRV1_0_2); + } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTHS_0_0, strlen(HW_VERSION_MTHS_0_0)) == 0) { + attr_group = &mths_0_0_platform_attribute_group; + gpio_pins = gpio_pins_mths_0_0; + set_buttons(mths_buttons_0_0); + mts_hw_version = MTHS_0_0; + log_info("detected board %s", HW_VERSION_MTHS_0_0); + } else if (((tmp=HW_VERSION_MTCDT_0_1),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) || + ((tmp=HW_VERSION_MTCDTIP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0)) { + current_blength = attr_blength = sizeof mtcdt_0_1_platform_attributes; + current_blength -= sizeof(struct attribute *); /* Length without terminating NULL */ + + /* See if we have no radio, and if so, prune out the stuff that follows */ + if(noradio) { + struct attribute **ap = mtcdt_0_1_platform_attribute_group.attrs; + while(1) { + if(ap[j] == NULL) { + log_info("Did not find radio power attribute. Possible driver fault."); + break; + } + j++; + if (is_radio_power_attr_mtcdt(ap[j])) { + log_info("Pruning radio feature from mts-io",j); + ap[j] = NULL; + current_blength = attr_blength = j * sizeof (ap[j]); + break; + } + } + } + + if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { + attr_blength += sizeof mtcdt_0_1_wifi_bt_attributes; + } + if(DEVICE_CAPA(id_eeprom.capa, CAPA_GPS)) { + attr_blength += sizeof mtcdt_0_1_gnss_attributes; + } + if (current_blength+(sizeof(struct attribute *)) != attr_blength) { + freelater = all_attrs = kmalloc(attr_blength,GFP_KERNEL); + current_count = current_blength/(sizeof (struct attribute *)); + memcpy(all_attrs,mtcdt_0_1_platform_attributes,current_blength); + if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { + log_info("Adding WiFi/BT to mts-io driver"); + memcpy(all_attrs + current_count,mtcdt_0_1_wifi_bt_attributes,sizeof mtcdt_0_1_wifi_bt_attributes); + current_count += sizeof mtcdt_0_1_wifi_bt_attributes / (sizeof (struct attribute *)); + } + if(DEVICE_CAPA(id_eeprom.capa, CAPA_GPS)) { + log_info("Adding GPS to mts-io driver"); + attr_blength += sizeof mtcdt_0_1_gnss_attributes; + memcpy(all_attrs + current_count,mtcdt_0_1_gnss_attributes,sizeof mtcdt_0_1_gnss_attributes); + current_count += sizeof mtcdt_0_1_gnss_attributes / (sizeof (struct attribute *)); + } + all_attrs[current_count] = (struct attribute *)NULL; + mtcdt_0_1_platform_attribute_group.attrs = all_attrs; + } + + attr_group = &mtcdt_0_1_platform_attribute_group; + gpio_pins = gpio_pins_mtcdt_0_1; + set_buttons(default_buttons); + log_info("detected board %s", tmp); + } else if ((tmp=HW_VERSION_MTCDTIPHP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) { + current_blength = attr_blength = sizeof mtcdt_0_1_platform_attributes; + current_blength -= sizeof(struct attribute *); /* Length without terminating NULL */ + + /* See if we have no radio, and if so, prune out the stuff that follows */ + if(noradio) { + struct attribute **ap = mtcdt_0_1_platform_attribute_group.attrs; + while(1) { + if(ap[j] == NULL) { + log_info("Did not find radio power attribute. Possible driver fault."); + break; + } + j++; + if (is_radio_power_attr_mtcdt(ap[j])) { + log_info("Pruning radio feature from mts-io",j); + ap[j] = NULL; + current_blength = attr_blength = j * sizeof (ap[j]); + break; + } + } + } + if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { + attr_blength += sizeof mtcdt_0_1_wifi_bt_attributes; + } + if (current_blength+(sizeof(struct attribute *)) != attr_blength) { + freelater = all_attrs = kmalloc(attr_blength,GFP_KERNEL); + current_count = current_blength/(sizeof (struct attribute *)); + memcpy(all_attrs,mtcdt_0_1_platform_attributes,current_blength); + if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { + log_info("Adding WiFi/BT to mts-io driver"); + memcpy(all_attrs + current_count,mtcdt_0_1_wifi_bt_attributes,sizeof mtcdt_0_1_wifi_bt_attributes); + current_count += sizeof mtcdt_0_1_wifi_bt_attributes / (sizeof (struct attribute *)); + } + /* MTCDTIPHP does not have GPS reset/interrupt tied to the CPU + * so do not instantiate the GPS attributes */ + all_attrs[current_count] = (struct attribute *)NULL; + mtcdt_0_1_platform_attribute_group.attrs = all_attrs; + } + + attr_group = &mtcdt_0_1_platform_attribute_group; + gpio_pins = gpio_pins_mtcdt_0_1; + if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) { + attr_group_lora = &mtcdtiphp_0_0_lora_attribute_group; + } + set_buttons(default_buttons); + log_info("detected board %s", tmp); + } else { + if(noradio) { + struct attribute **ap = mtcdt_platform_attribute_group.attrs; + while(1) { + if(ap[j] == NULL) { + log_info("Did not find radio power attribute. Possible driver fault."); + break; + } + j++; + if (is_radio_power_attr_mtcdt(ap[j])) { + log_info("Pruning radio feature from mts-io",j); + ap[j] = NULL; + break; + } + } + } + + attr_group = &mtcdt_platform_attribute_group; + gpio_pins = gpio_pins_mtcdt_0_0; + mts_hw_version = MTCDT_0_0; + set_buttons(default_buttons); + log_info("detected board %s", HW_VERSION_MTCDT_0_0); + } + + log_info("sizeof: %lu", (unsigned long) sizeof(struct mts_id_eeprom_layout)); + log_info("vendor-id: %.32s", id_eeprom.vendor_id); + log_info("product-id: %.32s", id_eeprom.product_id); + log_info("device-id: %.32s", id_eeprom.device_id); + log_info("hw-version: %.32s", id_eeprom.hw_version); + log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", + id_eeprom.mac_addr[0], + id_eeprom.mac_addr[1], + id_eeprom.mac_addr[2], + id_eeprom.mac_addr[3], + id_eeprom.mac_addr[4], + id_eeprom.mac_addr[5]); + + log_info("imei: %.32s", id_eeprom.imei); + log_info("capa-gps: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_GPS) ? "yes" : "no"); + log_info("capa-din: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_DIN) ? "yes" : "no"); + log_info("capa-dout: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_DOUT) ? "yes" : "no"); + log_info("capa-adc: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_ADC) ? "yes" : "no"); + log_info("capa-wifi: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI) ? "yes" : "no"); + log_info("capa-bluetooth: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_BLUETOOTH) ? "yes" : "no"); + log_info("capa-lora: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) ? "yes" : "no"); + + if (DEVICE_CAPA(id_eeprom.capa, CAPA_BLUETOOTH)) { + log_info("mac-bluetooth: %02X:%02X:%02X:%02X:%02X:%02X", + id_eeprom.mac_bluetooth[0], + id_eeprom.mac_bluetooth[1], + id_eeprom.mac_bluetooth[2], + id_eeprom.mac_bluetooth[3], + id_eeprom.mac_bluetooth[4], + id_eeprom.mac_bluetooth[5]); + } + if (DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { + log_info("mac-wifi: %02X:%02X:%02X:%02X:%02X:%02X", + id_eeprom.mac_wifi[0], + id_eeprom.mac_wifi[1], + id_eeprom.mac_wifi[2], + id_eeprom.mac_wifi[3], + id_eeprom.mac_wifi[4], + id_eeprom.mac_wifi[5]); + } + //Loop Through UUID Bytes and print them in HEX + ptr = (char*)buf; + for(i = 0; i < 16; i++) { + ptr += sprintf(ptr, "%02X", id_eeprom.uuid[i]); + } + log_info("uuid: %s", (char*)buf); + + if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) { + log_info("lora-eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", + id_eeprom.lora_eui[0], + id_eeprom.lora_eui[1], + id_eeprom.lora_eui[2], + id_eeprom.lora_eui[3], + id_eeprom.lora_eui[4], + id_eeprom.lora_eui[5], + id_eeprom.lora_eui[6], + id_eeprom.lora_eui[7]); + + log_info("lora-product-id: %.32s", id_eeprom.lora_product_id); + log_info("lora-hw-version: %.32s", id_eeprom.lora_hw_version); + } + return 0; +} + +static void cleanup(void) +{ + log_info("cleaning up...."); + if (mts_io_platform_device) { + platform_device_unregister(mts_io_platform_device); + } + + teardown_ports(); + if(freelater) { + kfree(freelater); + freelater = NULL; + } + + mts_capab_dir_delete(); +} + +static int __init mts_io_init(void) +{ + struct gpio_pin *pin; + + int ret; + + log_info("init: " DRIVER_VERSION); + + ret = mts_id_eeprom_load(); + if (ret) { + cleanup(); + return ret; + } + + mts_io_platform_device = platform_device_alloc(PLATFORM_NAME, -1); + if (!mts_io_platform_device) { + cleanup(); + return -ENOMEM; + } + + ret = platform_device_add(mts_io_platform_device); + if (ret) { + cleanup(); + return ret; + } + + init_ports(); + + ret = mts_capab_dir_create(); + if (ret) { + cleanup(); + return ret; + } + + ret = sysfs_create_group(&mts_io_platform_device->dev.kobj, attr_group); + if (ret) { + cleanup(); + return ret; + } + + for (pin = gpio_pins; *pin->name; pin++) { + if (pin->capability == 0 || DEVICE_CAPA(id_eeprom.capa,pin->capability)) { + ret = gpio_request_one(pin->pin.gpio, pin->pin.flags, pin->pin.label); + if (ret) + log_debug("could not request pin %s (%d) but it could have already been requested under a different pin name", pin->name, ret); + } + } +#ifndef NEW_BUTTON_INTERFACE + // start the reset handler + reset_callback(NULL); +#else + // start general buttons processing + init_buttons(); +#endif + /* init timers */ + setup_timer(&radio_reset_timer, radio_reset_timer_callback, 0); + setup_timer(&radio_reset_available_timer, radio_reset_available_timer_callback, 0); + + return 0; +} + +static void __exit mts_io_exit(void) +{ + /* delete radio_reset_timer */ + del_timer(&radio_reset_timer); + /* delete radio_reset_available_timer */ + del_timer(&radio_reset_available_timer); + +#ifndef NEW_BUTTON_INTERFACE + cancel_delayed_work_sync(&reset_work); +#else + cleanup_buttons(); +#endif + cleanup(); + + log_info("exiting"); +} + +module_init(mts_io_init); +module_exit(mts_io_exit); + +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); + +MODULE_ALIAS("mts-io-ap1-dout"); +MODULE_ALIAS("mts-io-ap1-din"); +MODULE_ALIAS("mts-io-ap1-adc"); +MODULE_ALIAS("mts-io-ap2-dout"); +MODULE_ALIAS("mts-io-ap2-din"); +MODULE_ALIAS("mts-io-ap2-adc"); diff --git a/io-module/mts_io.c b/io-module/mts_io.c deleted file mode 100644 index 35146ea..0000000 --- a/io-module/mts_io.c +++ /dev/null @@ -1,1243 +0,0 @@ -/* - * MTS-IO Controller - * - * Copyright (C) 2014 by Multi-Tech Systems - * Copyright (C) 2016 by Multi-Tech Systems - * - * Authors: James Maki - * Jesse Gilles - * Mike Fiore - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "mts_io.h" - -#define DRIVER_VERSION "v2.0.5" -#define DRIVER_AUTHOR "James Maki " -#define DRIVER_DESC "MTS-IO Controller" -#define DRIVER_NAME "mts-io" - -#define PLATFORM_NAME "mts-io" - -#define LED_LS_CONTROLLABLE 0 - -/* on-board EEPROM */ -extern uint8_t mts_id_eeprom[512]; -static struct mts_id_eeprom_layout id_eeprom; - -// NUM_AP should be defined from the board code -// it should be set to the value of CONFIG_MTS_NUM_ACCESSORY_PORTS -// arch/arm/mach-at91/board-dt-sam9.c -// if it is 0 or undefined, there is no accessory card support on this HW -#ifdef CONFIG_MTS_NUM_ACCESSORY_PORTS - -#ifndef NUM_AP -#define NUM_AP CONFIG_MTS_NUM_ACCESSORY_PORTS -#endif - -#else -#define NUM_AP 0 -#endif - -static uint8_t mts_hw_version; -static struct platform_device *mts_io_platform_device; -static struct attribute_group *attr_group; -static struct attribute_group *attr_group_lora; // on-board lora peripheral to be stored in the lora/ sub-directory -static struct gpio_pin *gpio_pins; - -static DEFINE_MUTEX(mts_io_mutex); - -static unsigned int *timings_data = NULL; -static unsigned int timings_data_size = 0; -static unsigned int timings_data_index = 0; -static time_t timings_data_stop_seconds = 0; -static struct timer_list radio_reset_timer; -static volatile int radio_reset_timer_is_start = 0; -static struct timer_list radio_reset_available_timer; -static volatile int radio_reset_available_timer_is_start = 0; -static time_t time_now_secs(void); -static void radio_reset_available_timer_callback(unsigned long data); -static void radio_reset_timer_callback(unsigned long data); - -/* generic GPIO support */ -#include "gpio.c" - -/* reset button handling */ -#define RESET_CHECK_PER_SEC 8 -#define RESET_INTERVAL (HZ / RESET_CHECK_PER_SEC) -#define RESET_HOLD_COUNT (RESET_CHECK_PER_SEC * 3) -#define RESET_LONG_HOLD_COUNT (RESET_CHECK_PER_SEC * 30) - -static pid_t reset_pid = -1; -static pid_t reset_count = 0; -bool sent_extra_long = false; -static int reset_short_signal = SIGUSR1; -static int reset_long_signal = SIGUSR2; -static int reset_extra_long_signal = SIGHUP; -static int reset_short_interval = RESET_HOLD_COUNT; -static int reset_long_interval = RESET_LONG_HOLD_COUNT; - -static void reset_callback(struct work_struct *ignored); - -static DECLARE_DELAYED_WORK(reset_work, reset_callback); - -/* - * This function takes the product_id and tries to check - * for a modem. If there is an error, assume there is a - * modem. Saying there is a modem when there is not - * just results in a slower boot. - * If no hyphen in product ID, or no product ID, - * assume we have a radio. - * If there is a hyphen test the character after the - * first hyphen: - * If the character is numeric, we have no modem. - * if the chracter is not a B, we have a modem. - * If the character is a B, and it is followed - * by a numeric, we have no modem. - * If the B is the last character or is followed - * by a numeric, we have no modem. - * All other cases, we have a modem. - */ -static int -has_radio(const char *product_id, size_t len) -{ - char *p; - if (!product_id || ! *product_id) - return 1; /* No Product ID? */ - p = memchr(product_id,'-',len); - if (p) { /* Found a hyphen */ - log_debug("Found hyphen"); - p++; - if (p >= product_id+len) { - log_debug("End of string -- hyphen"); - return 1; /* Last character was hyphen */ - } - if (isdigit(*p)) { - log_debug("Found digit after hypen"); - return 0; /* Modem name never starts with a digit */ - } - if (*p != 'B') { - log_debug("Found neither B nor digit after hypen"); - return 1; /* Modem starting with a letter, but not B */ - } - /* Found a B */ - p++; - if (p >= product_id+len) { - log_debug("B at end of product-id string"); - return 1; /* Last character was B */ - } - if (isdigit(*p)) { - log_debug("B followed by digit after hyphen - no modem"); - return 0; /* B[numeric] is MTR Build number */ - } - log_debug("B followed by non-digit after hyphen - has modem"); - return 1; /* B[non-numeric] so assume a modem that starts with B */ - } /* End of found hyphen case */ - log_debug("Undefined product-id - has modem"); - return 1; /* Product id invalid or empty, so instantiate a radio anyway */ -} - -static void reset_callback(struct work_struct *ignored) -{ - struct gpio_pin *pin; - int reset_pressed = 0; - struct pid *vpid = NULL; - - mutex_lock(&mts_io_mutex); - - pin = gpio_pin_by_attr_name("reset"); - if (pin) { - reset_pressed = !gpio_get_value(pin->pin.gpio); - } - - if (reset_pid > 0) { - vpid = find_vpid(reset_pid); - } - - if (vpid) { - if (reset_pressed) { - reset_count++; - } else { - //Reset button has not been pressed - if (reset_count > 0 && reset_count < reset_short_interval) { - kill_pid(vpid, reset_short_signal, 1); - } else if (reset_count >= reset_short_interval && reset_count < reset_long_interval) { - kill_pid(vpid, reset_long_signal, 1); - } - - reset_count = 0; - sent_extra_long = false; - } - if (reset_count >= reset_long_interval && ! sent_extra_long) { - kill_pid(vpid, reset_extra_long_signal, 1); - sent_extra_long = true; - } - } else { - reset_count = 0; - } - - mutex_unlock(&mts_io_mutex); - - schedule_delayed_work(&reset_work, RESET_INTERVAL); -} - -static ssize_t mts_attr_show_reset_monitor_intervals(struct device *dev, struct device_attribute *attr, char *buf) -{ - int ret; - - mutex_lock(&mts_io_mutex); - - ret = sprintf(buf, "%d %d\n", reset_short_interval / RESET_CHECK_PER_SEC, reset_long_interval / RESET_CHECK_PER_SEC); - - mutex_unlock(&mts_io_mutex); - - return ret; -} - -static ssize_t mts_attr_store_reset_monitor_intervals(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) -{ - int short_int; - int long_int; - - if (sscanf(buf, "%i %i", &short_int, &long_int) != 2) { - return -EINVAL; - } - - mutex_lock(&mts_io_mutex); - - reset_short_interval = short_int * RESET_CHECK_PER_SEC; - reset_long_interval = long_int * RESET_CHECK_PER_SEC; - - mutex_unlock(&mts_io_mutex); - - return count; -} - -static DEVICE_ATTR_MTS(dev_attr_reset_monitor_intervals, "reset-monitor-intervals", - mts_attr_show_reset_monitor_intervals, mts_attr_store_reset_monitor_intervals); - -static ssize_t mts_attr_show_reset_monitor(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - int ret; - - mutex_lock(&mts_io_mutex); - - ret = sprintf(buf, "%d %d %d %d\n", reset_pid, reset_short_signal, reset_long_signal, reset_extra_long_signal); - - mutex_unlock(&mts_io_mutex); - - return ret; -} - -static ssize_t mts_attr_store_reset_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); - - if (result < 3 || result > 4) { - return -EINVAL; - } - - if(result == 3) { - mutex_lock(&mts_io_mutex); - - reset_pid = pid; - reset_short_signal = short_signal; - reset_long_signal = long_signal; - - mutex_unlock(&mts_io_mutex); - } else { - mutex_lock(&mts_io_mutex); - - reset_pid = pid; - reset_short_signal = short_signal; - reset_long_signal = long_signal; - reset_extra_long_signal = extra_long_signal; - - mutex_unlock(&mts_io_mutex); - } - - return count; -} - -static DEVICE_ATTR_MTS(dev_attr_reset_monitor, "reset-monitor", - mts_attr_show_reset_monitor, mts_attr_store_reset_monitor); -static DEVICE_ATTR_RO_MTS(dev_attr_reset, "reset", mts_attr_show_gpio_pin); - -/* active-low socket modem reset */ -static ssize_t mts_attr_store_radio_reset(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - int value; /* 0 = normal reset; -1 = forced reset */ - int err; - struct gpio_pin *pin; - - if (sscanf(buf, "%i", &value) != 1) { - return -EINVAL; - } - if (value != 0 && value != -1) { - return -EINVAL; - } - - /* check reset timings is enabled */ - if (value != -1 && NULL != timings_data) { - /* check reset timer is started */ - if (radio_reset_timer_is_start == 1) { - log_info("radio reset timer is running. \n"); - return count; - } - - /* check reset timer available is started */ - if (radio_reset_available_timer_is_start == 1) { - del_timer(&radio_reset_available_timer); - radio_reset_available_timer_is_start = 0; - } - - /* reset timer not started, start it */ - mod_timer(&radio_reset_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000)); - //log_info("radio reset timer is start = [%d]\n", time_now_secs()); - /* save timings_data_stop_seconds */ - timings_data_stop_seconds = timings_data[timings_data_index] + time_now_secs(); - radio_reset_timer_is_start = 1; - } - - log_info("radio is reset\n"); - - pin = gpio_pin_by_attr_name("radio-reset"); - - if (!pin) { - return -ENODEV; - } - - mutex_lock(&mts_io_mutex); - - // 250ms low reset - err = reset_gpio_pin(pin, 250, 0); - - mutex_unlock(&mts_io_mutex); - - if (err) { - return err; - } - - return count; -} - -static DEVICE_ATTR_MTS(dev_attr_radio_reset, "radio-reset", - mts_attr_show_gpio_pin, mts_attr_store_radio_reset); - -/* shared gpio attributes */ -static DEVICE_ATTR_MTS(dev_attr_radio_power, "radio-power", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); - -/* backoff-timers */ -static time_t time_now_secs(void) -{ - struct timespec ts = current_kernel_time(); - return ts.tv_sec; -} - -static void radio_reset_available_timer_callback( unsigned long data ) -{ - /* do your timer stuff here */ - //log_info("radio_reset_available_timer_callback\n"); - //log_info("radio reset available timer is stop = [%d]\n", time_now_secs()); - - /* zero timings_data_index */ - timings_data_index = 0; - //log_info("timings data index is zero = [%d]\n", timings_data_index); - radio_reset_available_timer_is_start = 0; -} - -static void radio_reset_timer_callback( unsigned long data ) -{ - /* do your timer stuff here */ - //log_info("radio_reset_timer_callback\n"); - //log_info("radio reset timer is stop = [%d]\n", time_now_secs()); - - /* increment timings_data_index */ - timings_data_index++; - if(timings_data_index >= timings_data_size) { - timings_data_index = timings_data_size-1; - } - - //log_info("timings data index = [%d]\n", timings_data_index); - - /* reset available timer not started, start it */ - mod_timer(&radio_reset_available_timer, jiffies + msecs_to_jiffies((timings_data[timings_data_index]) * 1000)); - //log_info("radio reset available timer is start = [%d]\n", time_now_secs()); - radio_reset_available_timer_is_start = 1; - radio_reset_timer_is_start = 0; -} - -static ssize_t mts_attr_store_radio_reset_backoffs(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - char *timings_data_str = NULL; - const char delimiter[] = " "; - char * pch = NULL; - unsigned int size = 0; - - /* free previous timings_data */ - if (NULL != timings_data) { - /* stop timers */ - del_timer(&radio_reset_timer); - del_timer(&radio_reset_available_timer); - timings_data_index = 0; - radio_reset_timer_is_start = 0; - radio_reset_available_timer_is_start = 0; - - //log_info("free previous timings_data\n"); - kfree(timings_data); - timings_data = NULL; - timings_data_size = 0; - } - - /* make a copy */ - if( NULL == (timings_data_str = kzalloc((strlen(buf) + 1), GFP_KERNEL)) ){ - log_error("can`t allocate memory\n"); - return -EINVAL; - } - - //log_info("radio_reset_backoffs buf: [%s]", buf); - strncpy(timings_data_str, buf, (strlen(buf) + 1)); - - /* get number of tokens */ - while (NULL != (pch = strsep (&timings_data_str, delimiter))) { - int value = 0; - sscanf(pch, "%d", &value); - //log_info("radio reset backoffs pch = [%s]\n", pch); - if (value > 0){ - size++; - if (NULL == timings_data) { - /* make alloc */ - if (NULL == (timings_data = kmalloc(sizeof(unsigned int), GFP_KERNEL))) { - log_error("radio reset backoffs can`t allocate memory\n"); - goto free; - } - } else { - /* make realloc */ - if (NULL == (timings_data = krealloc(timings_data, size * sizeof(unsigned int), GFP_KERNEL))) { - log_error("radio reset backoffs can`t allocate memory\n"); - goto free; - } - } - /* save timings data */ - sscanf(pch, "%d", &timings_data[size-1]); - } - } - - timings_data_size = size; - //log_info("timings_data_size = %d\n", timings_data_size); - - if (NULL != timings_data_str) { - /* free timings_data_str */ - /* never get here in happy path */ - kfree(timings_data_str); - } - return count; - -free: - if (NULL != timings_data_str) { - /* free timings_data_str */ - kfree(timings_data_str); - } - - if (NULL != timings_data) { - kfree(timings_data); - timings_data = NULL; - timings_data_size = 0; - } - return -EINVAL; -} - -static ssize_t mts_attr_store_radio_reset_backoffs_index(struct device *dev, - struct device_attribute *attr, const char *buf, size_t count) -{ - int value; - - if (sscanf(buf, "%d", &value) != 1) { - return -EINVAL; - } - - if ((value < 0) || (value >= timings_data_size)) { - log_error("incorrect data\n"); - return -EINVAL; - } - - /* stop timers */ - del_timer(&radio_reset_timer); - del_timer(&radio_reset_available_timer); - radio_reset_timer_is_start = 0; - radio_reset_available_timer_is_start = 0; - timings_data_index = value; - - return count; -} - -static ssize_t mts_attr_show_radio_reset_backoffs(struct device *dev, - struct device_attribute *attr, char *buf) -{ - int ret = 0; - size_t i = 0; - size_t buf_left = 0; - - if (NULL != timings_data) { - for(i = 0; i < timings_data_size; ++i) { - buf_left = PAGE_SIZE - ret; - ret += snprintf(buf += strlen(buf), buf_left, "%d ", timings_data[i]); - } - } - - if (ret > 0) { - ret -= 1; - } - - return ret; -} - -static ssize_t mts_attr_show_radio_reset_backoff_index(struct device *dev, - struct device_attribute *attr, char *buf) -{ - ssize_t value; - - if (strcmp(attr->attr.name, "radio-reset-backoff-index") == 0) { - value = sprintf(buf, "%d", timings_data_index); - } - else { - log_error("attribute '%s' not found", attr->attr.name); - value = -1; - } - - return value; -} - -static ssize_t mts_attr_show_radio_reset_backoff_seconds(struct device *dev, - struct device_attribute *attr, char *buf) -{ - ssize_t value; - - if (strcmp(attr->attr.name, "radio-reset-backoff-seconds") == 0) { - if (radio_reset_timer_is_start == 1) { - value = sprintf(buf, "%lu", (timings_data_stop_seconds - time_now_secs())); - } else { - value = sprintf(buf, "%d", 0); - } - } else { - log_error("attribute '%s' not found", attr->attr.name); - value = -1; - } - - return value; -} - -static DEVICE_ATTR_MTS(dev_attr_radio_reset_backoffs, "radio-reset-backoffs", - mts_attr_show_radio_reset_backoffs, mts_attr_store_radio_reset_backoffs); - -static DEVICE_ATTR_MTS(dev_attr_radio_reset_backoff_index, "radio-reset-backoff-index", - mts_attr_show_radio_reset_backoff_index, mts_attr_store_radio_reset_backoffs_index); - -static DEVICE_ATTR_RO_MTS(dev_attr_radio_reset_backoff_seconds, "radio-reset-backoff-seconds", - mts_attr_show_radio_reset_backoff_seconds); - -/* shared gpio-based LEDs */ -static DEVICE_ATTR_MTS(dev_attr_led_status, "led-status", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_a_gpio, "led-a", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); - -#if LED_LS_CONTROLLABLE -static DEVICE_ATTR_MTS(dev_attr_led_ls, "led-ls", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -#else -static DEVICE_ATTR_RO_MTS(dev_attr_led_ls, "led-ls", - mts_attr_show_gpio_pin); -#endif - -static DEVICE_ATTR_MTS(dev_attr_led_b_gpio, "led-b", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); - -static DEVICE_ATTR_MTS(dev_attr_led_cd_gpio, "led-cd", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_c_gpio, "led-c", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); - -static DEVICE_ATTR_MTS(dev_attr_led_sig1_gpio, "led-sig1", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_sig2_gpio, "led-sig2", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_sig3_gpio, "led-sig3", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); - -static DEVICE_ATTR_MTS(dev_attr_led_d_gpio, "led-d", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); -static DEVICE_ATTR_MTS(dev_attr_led_e_gpio, "led-e", - mts_attr_show_gpio_pin, mts_attr_store_gpio_pin); - -/* eeprom info */ -static ssize_t mts_attr_show_product_info(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - int i; - ssize_t value; - - if (strcmp(attr->attr.name, "vendor-id") == 0) { - value = sprintf(buf, "%.32s\n", id_eeprom.vendor_id); - } else if (strcmp(attr->attr.name, "product-id") == 0) { - value = sprintf(buf, "%.32s\n", id_eeprom.product_id); - } else if (strcmp(attr->attr.name, "has-radio") == 0) { - value = sprintf(buf, "%1d\n", - has_radio(id_eeprom.product_id,sizeof id_eeprom.product_id)); - } else if (strcmp(attr->attr.name, "device-id") == 0) { - value = sprintf(buf, "%.32s\n", id_eeprom.device_id); - } else if (strcmp(attr->attr.name, "uuid") == 0) { - //Loop Through UUID Bytes and print them in HEX - - for(i = 0; i < 16; i++) { - value = sprintf(buf, "%02X", id_eeprom.uuid[i]); - if(value == -1) { - return value; - } - buf += value; - } - value = sprintf(buf, "\n"); - if(value == -1) { - return value; - } - value = 33; //16*2 (ASCII HEX) + 1 ('\n') - } else if (strcmp(attr->attr.name, "hw-version") == 0) { - value = sprintf(buf, "%.32s\n", id_eeprom.hw_version); - } else if (strcmp(attr->attr.name, "imei") == 0) { - value = sprintf(buf, "%.32s\n", id_eeprom.imei); - } else if (strcmp(attr->attr.name, "mac-wifi") == 0) { - value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", - id_eeprom.mac_wifi[0], - id_eeprom.mac_wifi[1], - id_eeprom.mac_wifi[2], - id_eeprom.mac_wifi[3], - id_eeprom.mac_wifi[4], - id_eeprom.mac_wifi[5]); - } else if (strcmp(attr->attr.name, "mac-eth") == 0) { - value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", - id_eeprom.mac_addr[0], - id_eeprom.mac_addr[1], - id_eeprom.mac_addr[2], - id_eeprom.mac_addr[3], - id_eeprom.mac_addr[4], - id_eeprom.mac_addr[5]); - } else if (strcmp(attr->attr.name, "lora-eui") == 0) { - value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", - id_eeprom.lora_eui[0], - id_eeprom.lora_eui[1], - id_eeprom.lora_eui[2], - id_eeprom.lora_eui[3], - id_eeprom.lora_eui[4], - id_eeprom.lora_eui[5], - id_eeprom.lora_eui[6], - id_eeprom.lora_eui[7]); - } - else { - log_error("attribute '%s' not found", attr->attr.name); - value = -1; - } - - return value; -} - -static DEVICE_ATTR_RO_MTS(dev_attr_vendor_id, "vendor-id", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_product_id, "product-id", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_has_radio, "has-radio", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_device_id, "device-id", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_uuid, "uuid", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_hw_version, "hw-version", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_imei, "imei", - mts_attr_show_product_info); -static DEVICE_ATTR_RO_MTS(dev_attr_eth_mac, "mac-eth", - mts_attr_show_product_info); - -static int get_radio_model_from_product_id(void) { - int rc = RADIO_UNKNOWN; - - if (strstr(id_eeprom.product_id, "LEU1")) rc = RADIO_LEU1; - else if (strstr(id_eeprom.product_id, "LNA3")) rc = RADIO_LNA3; - - // Add other radios as needed. - return rc; -} - - -/* include on-board lora peripheral */ -#include "mts_lora.c" - -/* include per-device pins and attributes */ -#include "mtcdt.c" -#include "mtcap.c" -#include "mtr.c" -#include "hs.c" - -/* include capabilities sub-directory support */ -#include "mts_capab.c" - - -#if NUM_AP > 0 - -/* accessory card EEPROMs */ -extern uint8_t mts_ap_eeprom[NUM_AP][512]; -static struct mts_ap_eeprom_layout ap_eeprom[NUM_AP]; -/* kobject pointers for the apX subdirectories that correspond to the accessory ports */ -static struct kobject *ap_subdirs[NUM_AP]; -/* attribute groups for the accessory ports*/ -static struct attribute_group ap_attr_groups[NUM_AP]; -/* info for accessory port (contains function pointers for setup and teardown and and useful info) */ -static struct ap_info* port_info[NUM_AP]; - -/* accessory card support */ -#include "mtac.c" -#include "mtac_gpiob.c" -#include "mtac_mfser.c" -#include "mtac_eth.c" -#include "mtac_lora.c" - -static bool load_port(int port) { - int port_index = port - 1; - memcpy(&ap_eeprom[port_index], mts_ap_eeprom[port_index], sizeof(mts_ap_eeprom[port_index])); - - if (mts_ap_eeprom[port_index][0] == 0xFF) { - log_error("uninitialized eeprom on accessory card %d", port); - } else if (mts_ap_eeprom[port_index][0] == 0x00) { - log_info("no accessory card inserted in port %d", port); - } else { - port_info[port_index] = kzalloc(sizeof(struct ap_info), GFP_KERNEL); - if (! port_info[port_index]) { - log_error("alloc of port info failed"); - return false; - } - - if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_GPIOB)) { - if (! set_gpiob_info(port_info[port_index])) { - log_error("failed to set up gpiob port info"); - return false; - } - } else if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_MFSER)) { - if (! set_mfser_info(port_info[port_index])) { - log_error("failed to set up mfser port info"); - return false; - } - } else if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_ETH)) { - if (! set_eth_info(port_info[port_index])) { - log_error("failed to set up eth port info"); - return false; - } - } else if (strstr(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_LORA)) { - if (! set_lora_info(port_info[port_index])) { - log_error("failed to set up lora port info"); - return false; - } - } else { - log_error("unknown accessory card [%s] in port %d", ap_eeprom[port_index].product_id, port); - kfree(port_info[port_index]); - port_info[port_index] = NULL; - return false; - } - - log_info("accessory card %d vendor-id: %.32s", port, ap_eeprom[port_index].vendor_id); - log_info("accessory card %d product-id: %.32s", port, ap_eeprom[port_index].product_id); - log_info("accessory card %d device-id: %.32s", port, ap_eeprom[port_index].device_id); - log_info("accessory card %d hw-version: %.32s", port, ap_eeprom[port_index].hw_version); - if (strncmp(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_ETH, strlen(PRODUCT_ID_MTAC_ETH)) == 0) { - log_info("accessory card %d mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", - port, - ap_eeprom[port_index].mac_addr[0], - ap_eeprom[port_index].mac_addr[1], - ap_eeprom[port_index].mac_addr[2], - ap_eeprom[port_index].mac_addr[3], - ap_eeprom[port_index].mac_addr[4], - ap_eeprom[port_index].mac_addr[5]); - } - if (strncmp(ap_eeprom[port_index].product_id, PRODUCT_ID_MTAC_LORA, strlen(PRODUCT_ID_MTAC_LORA)) == 0) { - log_info("accessory card %d eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", - port, - ap_eeprom[port_index].eui[0], - ap_eeprom[port_index].eui[1], - ap_eeprom[port_index].eui[2], - ap_eeprom[port_index].eui[3], - ap_eeprom[port_index].eui[4], - ap_eeprom[port_index].eui[5], - ap_eeprom[port_index].eui[6], - ap_eeprom[port_index].eui[7]); - } - - if (! port_info[port_index]->setup(port)) { - log_error("accessory port %d setup failed", port); - port_info[port_index]->teardown(port); - kfree(port_info[port_index]); - port_info[port_index] = NULL; - return false; - } - } - - return true; -} - -static void init_accessory_ports(void) -{ - int port_index; - - for (port_index = 0; port_index < NUM_AP; port_index++) { - port_info[port_index] = NULL; - if (! load_port(port_index+1)) { - log_error("failed to load accessory card in port %d", port_index); - } - } -} - -static void teardown_accessory_ports(void) -{ - int port_index; - - for (port_index = 0; port_index < NUM_AP; port_index++) { - if (port_info[port_index]) { - port_info[port_index]->teardown(port_index+1); - kfree(port_info[port_index]); - } - } -} -#else /* NUM_AP > 0 */ -static void init_accessory_ports(void) {} -static void teardown_accessory_ports(void) {} -#endif - -static void init_ports(void) -{ - if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) && attr_group_lora) { - mts_load_lora_port(); - } - - init_accessory_ports(); -} - -static void teardown_ports(void) -{ - if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) && attr_group_lora) { - mts_teardown_lora_port(); - } - - teardown_accessory_ports(); -} - - -struct attribute *freelater = NULL; // Storage to free when driver is unloaded. - -static int -mts_id_eeprom_load(void) -{ - int i, j = 0; - char buf[64] = {0}; - char* ptr; - int attr_blength; // Byte length of base attribute array - int current_blength; // Current length in bytes of attribute array - int current_count; // Number of items in array - struct attribute **all_attrs = NULL; - char *tmp; - int noradio; - - //The mts_id_eeprom buffer is initialize once on boot - //reloading the mts_io.ko module will not reinitialize this buffer - //only rebooting will reinitialize this buffer - memcpy(&id_eeprom, mts_id_eeprom, sizeof(mts_id_eeprom)); - - if (mts_id_eeprom[0] == 0xFF) { - log_error("uninitialized eeprom"); - return -EIO; - } - - noradio = ! has_radio(id_eeprom.product_id,sizeof id_eeprom.product_id); - log_debug("mts_id_eeprom: noradio=%d",noradio); - - if (((tmp=HW_VERSION_MTCAP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) || - ((tmp=HW_VERSION_MTCAP_0_1),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0)) { - /* See if we have no radio, and if so, prune out the stuff that follows */ - if(noradio) { - struct attribute **ap = mtcap_0_0_platform_attribute_group.attrs; - while(1) { - if(ap[j] == NULL) { - log_info("Did not find radio power attribute. Possible driver fault."); - break; - } - j++; - if (is_radio_power_attr_mtcap(ap[j])) { - log_info("Pruning radio feature from mts-io",j); - ap[j] = NULL; - break; - } - } - } - attr_group = &mtcap_0_0_platform_attribute_group; - gpio_pins = gpio_pins_mtcap_0_0; - if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) { - attr_group_lora = &mtcap_0_0_lora_attribute_group; - } - log_info("detected board %s", tmp); - } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTR_0_0, strlen(HW_VERSION_MTR_0_0)) == 0) { - attr_group = &mtr_platform_attribute_group; - gpio_pins = gpio_pins_mtr_0_0; - mts_hw_version = MTR_0_0; - log_info("detected board %s", HW_VERSION_MTR_0_0); - } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTR_0_1, strlen(HW_VERSION_MTR_0_1)) == 0) { - attr_group = &mtr_platform_attribute_group; - gpio_pins = gpio_pins_mtr_0_1; - mts_hw_version = MTR_0_1; - log_info("detected board %s", HW_VERSION_MTR_0_1); - } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_0, strlen(HW_VERSION_MTRV1_0_0)) == 0) { - attr_group = &mtr_platform_attribute_group; - gpio_pins = gpio_pins_mtrv1_0_0; - mts_hw_version = MTRV1_0_0; - log_info("detected board %s", HW_VERSION_MTRV1_0_0); - } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_1, strlen(HW_VERSION_MTRV1_0_1)) == 0) { - attr_group = &mtrv1_0_1_platform_attribute_group; - gpio_pins = gpio_pins_mtrv1_0_1; - mts_hw_version = MTRV1_0_1; - log_info("detected board %s", HW_VERSION_MTRV1_0_1); - } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTRV1_0_2, strlen(HW_VERSION_MTRV1_0_2)) == 0) { - attr_group = &mtrv1_0_2_platform_attribute_group; - gpio_pins = gpio_pins_mtrv1_0_2; - mts_hw_version = MTRV1_0_2; - log_info("detected board %s", HW_VERSION_MTRV1_0_2); - } else if (strncmp(id_eeprom.hw_version, HW_VERSION_MTHS_0_0, strlen(HW_VERSION_MTHS_0_0)) == 0) { - attr_group = &mths_0_0_platform_attribute_group; - gpio_pins = gpio_pins_mths_0_0; - mts_hw_version = MTHS_0_0; - log_info("detected board %s", HW_VERSION_MTHS_0_0); - } else if (((tmp=HW_VERSION_MTCDT_0_1),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) || - ((tmp=HW_VERSION_MTCDTIP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0)) { - current_blength = attr_blength = sizeof mtcdt_0_1_platform_attributes; - current_blength -= sizeof(struct attribute *); /* Length without terminating NULL */ - - /* See if we have no radio, and if so, prune out the stuff that follows */ - if(noradio) { - struct attribute **ap = mtcdt_0_1_platform_attribute_group.attrs; - while(1) { - if(ap[j] == NULL) { - log_info("Did not find radio power attribute. Possible driver fault."); - break; - } - j++; - if (is_radio_power_attr_mtcdt(ap[j])) { - log_info("Pruning radio feature from mts-io",j); - ap[j] = NULL; - current_blength = attr_blength = j * sizeof (ap[j]); - break; - } - } - } - - if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { - attr_blength += sizeof mtcdt_0_1_wifi_bt_attributes; - } - if(DEVICE_CAPA(id_eeprom.capa, CAPA_GPS)) { - attr_blength += sizeof mtcdt_0_1_gnss_attributes; - } - if (current_blength+(sizeof(struct attribute *)) != attr_blength) { - freelater = all_attrs = kmalloc(attr_blength,GFP_KERNEL); - current_count = current_blength/(sizeof (struct attribute *)); - memcpy(all_attrs,mtcdt_0_1_platform_attributes,current_blength); - if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { - log_info("Adding WiFi/BT to mts-io driver"); - memcpy(all_attrs + current_count,mtcdt_0_1_wifi_bt_attributes,sizeof mtcdt_0_1_wifi_bt_attributes); - current_count += sizeof mtcdt_0_1_wifi_bt_attributes / (sizeof (struct attribute *)); - } - if(DEVICE_CAPA(id_eeprom.capa, CAPA_GPS)) { - log_info("Adding GPS to mts-io driver"); - attr_blength += sizeof mtcdt_0_1_gnss_attributes; - memcpy(all_attrs + current_count,mtcdt_0_1_gnss_attributes,sizeof mtcdt_0_1_gnss_attributes); - current_count += sizeof mtcdt_0_1_gnss_attributes / (sizeof (struct attribute *)); - } - all_attrs[current_count] = (struct attribute *)NULL; - mtcdt_0_1_platform_attribute_group.attrs = all_attrs; - } - - attr_group = &mtcdt_0_1_platform_attribute_group; - gpio_pins = gpio_pins_mtcdt_0_1; - log_info("detected board %s", tmp); - } else if ((tmp=HW_VERSION_MTCDTIPHP_0_0),strncmp(id_eeprom.hw_version, tmp, strlen(tmp)) == 0) { - current_blength = attr_blength = sizeof mtcdt_0_1_platform_attributes; - current_blength -= sizeof(struct attribute *); /* Length without terminating NULL */ - - /* See if we have no radio, and if so, prune out the stuff that follows */ - if(noradio) { - struct attribute **ap = mtcdt_0_1_platform_attribute_group.attrs; - while(1) { - if(ap[j] == NULL) { - log_info("Did not find radio power attribute. Possible driver fault."); - break; - } - j++; - if (is_radio_power_attr_mtcdt(ap[j])) { - log_info("Pruning radio feature from mts-io",j); - ap[j] = NULL; - current_blength = attr_blength = j * sizeof (ap[j]); - break; - } - } - } - if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { - attr_blength += sizeof mtcdt_0_1_wifi_bt_attributes; - } - if (current_blength+(sizeof(struct attribute *)) != attr_blength) { - freelater = all_attrs = kmalloc(attr_blength,GFP_KERNEL); - current_count = current_blength/(sizeof (struct attribute *)); - memcpy(all_attrs,mtcdt_0_1_platform_attributes,current_blength); - if(DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { - log_info("Adding WiFi/BT to mts-io driver"); - memcpy(all_attrs + current_count,mtcdt_0_1_wifi_bt_attributes,sizeof mtcdt_0_1_wifi_bt_attributes); - current_count += sizeof mtcdt_0_1_wifi_bt_attributes / (sizeof (struct attribute *)); - } - /* MTCDTIPHP does not have GPS reset/interrupt tied to the CPU - * so do not instantiate the GPS attributes */ - all_attrs[current_count] = (struct attribute *)NULL; - mtcdt_0_1_platform_attribute_group.attrs = all_attrs; - } - - attr_group = &mtcdt_0_1_platform_attribute_group; - gpio_pins = gpio_pins_mtcdt_0_1; - if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) { - attr_group_lora = &mtcdtiphp_0_0_lora_attribute_group; - } - log_info("detected board %s", tmp); - } else { - if(noradio) { - struct attribute **ap = mtcdt_platform_attribute_group.attrs; - while(1) { - if(ap[j] == NULL) { - log_info("Did not find radio power attribute. Possible driver fault."); - break; - } - j++; - if (is_radio_power_attr_mtcdt(ap[j])) { - log_info("Pruning radio feature from mts-io",j); - ap[j] = NULL; - break; - } - } - } - - attr_group = &mtcdt_platform_attribute_group; - gpio_pins = gpio_pins_mtcdt_0_0; - mts_hw_version = MTCDT_0_0; - log_info("detected board %s", HW_VERSION_MTCDT_0_0); - } - - log_info("sizeof: %lu", (unsigned long) sizeof(struct mts_id_eeprom_layout)); - log_info("vendor-id: %.32s", id_eeprom.vendor_id); - log_info("product-id: %.32s", id_eeprom.product_id); - log_info("device-id: %.32s", id_eeprom.device_id); - log_info("hw-version: %.32s", id_eeprom.hw_version); - log_info("mac-addr: %02X:%02X:%02X:%02X:%02X:%02X", - id_eeprom.mac_addr[0], - id_eeprom.mac_addr[1], - id_eeprom.mac_addr[2], - id_eeprom.mac_addr[3], - id_eeprom.mac_addr[4], - id_eeprom.mac_addr[5]); - - log_info("imei: %.32s", id_eeprom.imei); - log_info("capa-gps: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_GPS) ? "yes" : "no"); - log_info("capa-din: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_DIN) ? "yes" : "no"); - log_info("capa-dout: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_DOUT) ? "yes" : "no"); - log_info("capa-adc: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_ADC) ? "yes" : "no"); - log_info("capa-wifi: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI) ? "yes" : "no"); - log_info("capa-bluetooth: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_BLUETOOTH) ? "yes" : "no"); - log_info("capa-lora: %s", DEVICE_CAPA(id_eeprom.capa, CAPA_LORA) ? "yes" : "no"); - - if (DEVICE_CAPA(id_eeprom.capa, CAPA_BLUETOOTH)) { - log_info("mac-bluetooth: %02X:%02X:%02X:%02X:%02X:%02X", - id_eeprom.mac_bluetooth[0], - id_eeprom.mac_bluetooth[1], - id_eeprom.mac_bluetooth[2], - id_eeprom.mac_bluetooth[3], - id_eeprom.mac_bluetooth[4], - id_eeprom.mac_bluetooth[5]); - } - if (DEVICE_CAPA(id_eeprom.capa, CAPA_WIFI)) { - log_info("mac-wifi: %02X:%02X:%02X:%02X:%02X:%02X", - id_eeprom.mac_wifi[0], - id_eeprom.mac_wifi[1], - id_eeprom.mac_wifi[2], - id_eeprom.mac_wifi[3], - id_eeprom.mac_wifi[4], - id_eeprom.mac_wifi[5]); - } - //Loop Through UUID Bytes and print them in HEX - ptr = (char*)buf; - for(i = 0; i < 16; i++) { - ptr += sprintf(ptr, "%02X", id_eeprom.uuid[i]); - } - log_info("uuid: %s", (char*)buf); - - if (DEVICE_CAPA(id_eeprom.capa, CAPA_LORA)) { - log_info("lora-eui: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X", - id_eeprom.lora_eui[0], - id_eeprom.lora_eui[1], - id_eeprom.lora_eui[2], - id_eeprom.lora_eui[3], - id_eeprom.lora_eui[4], - id_eeprom.lora_eui[5], - id_eeprom.lora_eui[6], - id_eeprom.lora_eui[7]); - - log_info("lora-product-id: %.32s", id_eeprom.lora_product_id); - log_info("lora-hw-version: %.32s", id_eeprom.lora_hw_version); - } - return 0; -} - -static void cleanup(void) -{ - log_info("cleaning up...."); - if (mts_io_platform_device) { - platform_device_unregister(mts_io_platform_device); - } - - teardown_ports(); - if(freelater) { - kfree(freelater); - freelater = NULL; - } - - mts_capab_dir_delete(); -} - -static int __init mts_io_init(void) -{ - struct gpio_pin *pin; - - int ret; - - log_info("init: " DRIVER_VERSION); - - ret = mts_id_eeprom_load(); - if (ret) { - cleanup(); - return ret; - } - - mts_io_platform_device = platform_device_alloc(PLATFORM_NAME, -1); - if (!mts_io_platform_device) { - cleanup(); - return -ENOMEM; - } - - ret = platform_device_add(mts_io_platform_device); - if (ret) { - cleanup(); - return ret; - } - - init_ports(); - - ret = mts_capab_dir_create(); - if (ret) { - cleanup(); - return ret; - } - - ret = sysfs_create_group(&mts_io_platform_device->dev.kobj, attr_group); - if (ret) { - cleanup(); - return ret; - } - - for (pin = gpio_pins; *pin->name; pin++) { - if (pin->capability == 0 || DEVICE_CAPA(id_eeprom.capa,pin->capability)) { - ret = gpio_request_one(pin->pin.gpio, pin->pin.flags, pin->pin.label); - if (ret) - log_debug("could not request pin %s (%d) but it could have already been requested under a different pin name", pin->name, ret); - } - } - - // start the reset handler - reset_callback(NULL); - - /* init timers */ - setup_timer(&radio_reset_timer, radio_reset_timer_callback, 0); - setup_timer(&radio_reset_available_timer, radio_reset_available_timer_callback, 0); - - return 0; -} - -static void __exit mts_io_exit(void) -{ - /* delete radio_reset_timer */ - del_timer(&radio_reset_timer); - /* delete radio_reset_available_timer */ - del_timer(&radio_reset_available_timer); - - cancel_delayed_work_sync(&reset_work); - - cleanup(); - - log_info("exiting"); -} - -module_init(mts_io_init); -module_exit(mts_io_exit); - -MODULE_AUTHOR(DRIVER_AUTHOR); -MODULE_DESCRIPTION(DRIVER_DESC); -MODULE_VERSION(DRIVER_VERSION); -MODULE_LICENSE("GPL"); - -MODULE_ALIAS("mts-io-ap1-dout"); -MODULE_ALIAS("mts-io-ap1-din"); -MODULE_ALIAS("mts-io-ap1-adc"); -MODULE_ALIAS("mts-io-ap2-dout"); -MODULE_ALIAS("mts-io-ap2-din"); -MODULE_ALIAS("mts-io-ap2-adc"); diff --git a/io-module/mts_io.h b/io-module/mts_io.h index b622330..57f07af 100644 --- a/io-module/mts_io.h +++ b/io-module/mts_io.h @@ -4,7 +4,13 @@ #include "mts_eeprom.h" #include -#define DEBUG 0 + +#define DRIVER_VERSION "v2.0.5" +#define DRIVER_AUTHOR "James Maki " +#define DRIVER_DESC "MTS-IO Controller" +#define DRIVER_NAME "mts-io" + +#define DEBUG 1 #define __log(level, name, format, args...) \ printk(level "[" name "] " DRIVER_NAME ":%s:%d: " format "\n" , \ @@ -148,5 +154,7 @@ struct ap_info { char* (*gpio_pin_name_by_attr_name)(const char* name, int port); }; +extern struct mutex mts_io_mutex; + #endif /* ~__MTS_IO_H */ -- cgit v1.2.3