From ae93c853b5edd59059581c5a49a99a7326e4b31b Mon Sep 17 00:00:00 2001 From: Jesse Gilles Date: Wed, 7 Jan 2015 17:32:00 -0600 Subject: add mtac-lora support --- io-module/mtac.c | 1 + io-module/mtac_lora.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ io-module/mts_io.c | 6 +++ io-module/mts_io.h | 2 + 4 files changed, 146 insertions(+) create mode 100644 io-module/mtac_lora.c diff --git a/io-module/mtac.c b/io-module/mtac.c index 828d24b..6a80576 100644 --- a/io-module/mtac.c +++ b/io-module/mtac.c @@ -98,6 +98,7 @@ static bool ap_add_product_info_attributes(int port, int type, struct attribute* case MTAC_GPIOB_0_0: case MTAC_MFSER_0_0: + case MTAC_LORA_0_0: break; default: log_error("invalid accessory card type"); diff --git a/io-module/mtac_lora.c b/io-module/mtac_lora.c new file mode 100644 index 0000000..b546003 --- /dev/null +++ b/io-module/mtac_lora.c @@ -0,0 +1,137 @@ +static char* lora_gpio_pin_name_by_attr_name(const char* name, int port) { + switch (port) { + case port_1: + if (! strcmp(name, "reset")) { + return "ap1-reset"; + } else { + log_error("attirbute name [%s] is invalid for LORA in port %d", name, port); + return ""; + } + + case port_2: + if (! strcmp(name, "reset")) { + return "ap2-reset"; + } else { + log_error("attirbute name [%s] is invalid for LORA in port %d", name, port); + return ""; + } + } +} + +// 1 reset +// 1 vendor-id +// 1 product-id +// 1 device-id +// 1 hw-version +// NULL +static size_t ap_lora_attrs_size = 6; + +static bool lora_setup(enum ap port) { + int i; + int port_index = port - 1; + int index = 0; + int count = 0; + int ret; + char buf[32]; + struct kobj_attribute* attr; + struct attribute **attrs; + + log_info("loading LORA accessory card in port %d", port); + + sprintf(buf, "ap%d", port); + ap_subdirs[port_index] = kobject_create_and_add(buf, &mts_io_platform_device->dev.kobj); + if (! ap_subdirs[port_index]) { + log_error("kobject_create_and_add in port %d failed", port); + return false; + } + + // create the link to the apX directory this card is in + // if we're in the first slot, we get plain "lora" + // if we're in a different slot, we might need to use "lora-2" to differentiate + if (port > 1) { + for (i = 1; i < port; i++) { + if (port_info[i - 1]) { + if (strstr(port_info[i - 1]->product_id, PRODUCT_ID_MTAC_LORA)) { + count++; + } + } + } + } + if (count > 0) { + sprintf(buf, "lora-%d", count + 1); + } else { + sprintf(buf, "lora"); + } + ret = sysfs_create_link(ap_subdirs[port_index]->parent, ap_subdirs[port_index], buf); + if (ret) { + log_error("failed to link [%s] to [%s], %d", buf, ap_subdirs[port_index]->name, ret); + } + + attrs = kzalloc(sizeof(struct attribute*) * ap_lora_attrs_size, GFP_KERNEL); + if (! attrs) { + log_error("failed to allocate attribute space for port %d", port); + return false; + } + + sprintf(buf, "reset"); + attr = create_attribute(buf, MTS_ATTR_MODE_RW); + if (! attr) { + log_error("failed to create attribute [%s] for LORA in port %d", buf, port); + return false; + } + attr->show = mts_attr_show_ap_gpio_pin; + attr->store = mts_attr_store_ap_gpio_pin; + attrs[index++] = &attr->attr; + + // add attributes for eeprom contents + if (! ap_add_product_info_attributes(port, MTAC_LORA_0_0, attrs, &index)) { + log_error("failed to add product info attributes for LORA in port %d", port); + return false; + } + + attrs[index] = NULL; + + ap_attr_groups[port_index].attrs = attrs; + if (sysfs_create_group(ap_subdirs[port_index], &ap_attr_groups[port_index])) { + log_error("sysfs_create_group failed for LORA in port %d", port); + return false; + } + + return true; +} + +static bool lora_teardown(enum ap port) { + int i; + int port_index = port - 1; + struct attribute **attrs = ap_attr_groups[port_index].attrs; + + log_info("unloading LORA accessory card in port %d", port); + + // clean up allocated memory for attributes + for (i = 0; i < ap_lora_attrs_size; i++) { + if (attrs[i]) { + if (attrs[i]->name) + kfree(attrs[i]->name); + + kfree(attrs[i]); + } + } + + kfree(attrs); + + // clean up our "apX/" kobject if it exists + if (ap_subdirs[port_index]) { + kobject_put(ap_subdirs[port_index]); + } + + return true; +} + +bool set_lora_info(struct ap_info* info) { + snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_LORA); + info->setup = &lora_setup; + info->teardown = &lora_teardown; + info->gpio_pin_name_by_attr_name = &lora_gpio_pin_name_by_attr_name; + + return true; +} diff --git a/io-module/mts_io.c b/io-module/mts_io.c index b3c3a10..3b2e130 100644 --- a/io-module/mts_io.c +++ b/io-module/mts_io.c @@ -98,6 +98,7 @@ static DEFINE_MUTEX(mts_io_mutex); #include "mtac_gpiob.c" #include "mtac_mfser.c" #include "mtac_eth.c" +#include "mtac_lora.c" /* reset button handling */ #define RESET_CHECK_PER_SEC 8 @@ -406,6 +407,11 @@ static bool load_port(int port) { 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]); diff --git a/io-module/mts_io.h b/io-module/mts_io.h index 1b51cd3..216cc92 100644 --- a/io-module/mts_io.h +++ b/io-module/mts_io.h @@ -48,6 +48,7 @@ struct device_attribute _dev_name = { \ #define PRODUCT_ID_MTAC_GPIOB "MTAC-GPIOB" #define PRODUCT_ID_MTAC_MFSER "MTAC-MFSER" #define PRODUCT_ID_MTAC_ETH "MTAC-ETH" +#define PRODUCT_ID_MTAC_LORA "MTAC-LORA" #define HW_VERSION_MTCBA2_2_0 "MTCBA2-2.0" #define HW_VERSION_MTCDP_0_0 "MTCDP-0.0" @@ -71,6 +72,7 @@ enum { MTAC_GPIOB_0_0, MTAC_MFSER_0_0, MTAC_ETH_0_0, + MTAC_LORA_0_0, }; // GPIO pin types:input, output, open drain (1 = high Z, 0 = output low) -- cgit v1.2.3