diff options
-rw-r--r-- | io-module/mtac.c | 86 | ||||
-rw-r--r-- | io-module/mtac_gpiob.c | 109 | ||||
-rw-r--r-- | io-module/mtac_mfser.c | 100 | ||||
-rw-r--r-- | io-module/mts_io.h | 1 |
4 files changed, 123 insertions, 173 deletions
diff --git a/io-module/mtac.c b/io-module/mtac.c index 99e2ce7..1c88b40 100644 --- a/io-module/mtac.c +++ b/io-module/mtac.c @@ -111,3 +111,89 @@ static bool ap_add_product_info_attributes(int port, int type) { return true; } + +struct gpio_pin *ap_gpio_pin_by_attr_name(const char *name) { + struct gpio_pin *pin; + char *pin_attr_name; + long port; + int port_index; + char *colon; + + colon = strstr(name, ":"); + if (colon && ++colon) { + if (kstrtol(colon, 10, &port)) { + log_error("kstrtol failed on [%s]", colon); + return NULL; + } + } else { + log_error("could not read port from attr name [%s]", name); + return NULL; + } + port_index = port - 1; + + pin_attr_name = port_info[port_index]->gpio_pin_name_by_attr_name(name); + + for (pin = gpio_pins; *pin->name; pin++) { + if (!strcmp(pin->pin.label, pin_attr_name)) { + return pin; + } + } + + log_error("pin with attr name [%s] not found", name); + return NULL; +} + +static ssize_t mts_attr_show_ap_gpio_pin(struct kobject *kobj, + struct kobj_attribute *attr, + char *buf) +{ + int value; + struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name); + + if (!pin) { + return -ENODEV; + } + + mutex_lock(&mts_io_mutex); + + value = gpio_get_value(pin->pin.gpio); + + mutex_unlock(&mts_io_mutex); + + if (value < 0) { + return value; + } + + if (pin->active_low) { + value = !value; + } + + return sprintf(buf, "%d\n", value); +} + +static ssize_t mts_attr_store_ap_gpio_pin(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + int value; + struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name); + + if (!pin) { + return -ENODEV; + } + + if (sscanf(buf, "%i", &value) != 1) { + return -EINVAL; + } + + if (pin->active_low) { + value = !value; + } + + mutex_lock(&mts_io_mutex); + + gpio_set_value(pin->pin.gpio, value); + + mutex_unlock(&mts_io_mutex); + + return count; +} diff --git a/io-module/mtac_gpiob.c b/io-module/mtac_gpiob.c index ebbe0c6..ce1d7de 100644 --- a/io-module/mtac_gpiob.c +++ b/io-module/mtac_gpiob.c @@ -105,97 +105,27 @@ static int mts_spi_ap_remove(struct spi_device *spi) return 0; } -// Is there a way to make this dynamic as well? -struct gpio_pin *ap_gpio_pin_by_attr_name(const char *name) { - struct gpio_pin *pin; - char *pin_attr_name; - - if (!strcmp(name, "ap-led1:1")) { - pin_attr_name = "ap1-gpio3"; - } else if (!strcmp(name, "ap-led2:1")) { - pin_attr_name = "ap1-gpio4"; - } else if (!strcmp(name, "ap-dout-enable:1")) { - pin_attr_name = "ap1-gpio1"; - } else if (!strcmp(name, "ap-reset:1")) { - pin_attr_name = "ap1-reset"; - } else if (!strcmp(name, "ap-led1:2")) { - pin_attr_name = "ap2-gpio3"; - } else if (!strcmp(name, "ap-led2:2")) { - pin_attr_name = "ap2-gpio4"; - } else if (!strcmp(name, "ap-dout-enable:2")) { - pin_attr_name = "ap2-gpio1"; - } else if (!strcmp(name, "ap-reset:2")) { - pin_attr_name = "ap2-reset"; +static char* gpiob_gpio_pin_name_by_attr_name(const char* name) { + if (! strcmp(name, "ap-led1:1")) { + return "ap1-gpio3"; + } else if (! strcmp(name, "ap-led2:1")) { + return "ap1-gpio4"; + } else if (! strcmp(name, "ap-dout-enable:1")) { + return "ap1-gpio1"; + } else if (! strcmp(name, "ap-reset:1")) { + return "ap1-reset"; + } else if (! strcmp(name, "ap-led1:2")) { + return "ap2-gpio3"; + } else if (! strcmp(name, "ap-led2:2")) { + return "ap2-gpio4"; + } else if (! strcmp(name, "ap-dout-enable:2")) { + return "ap2-gpio1"; + } else if (! strcmp(name, "ap-reset:2")) { + return "ap2-reset"; } else { - log_error("accessory card attribute %s not available", name); - return NULL; + log_error("attirbute name [%s] is invalid for GPIOB", name); + return ""; } - - for (pin = gpio_pins; *pin->name; pin++) { - if (!strcmp(pin->pin.label, pin_attr_name)) { - return pin; - } - } - - log_error("pin with attr name %s not found", name); - - return NULL; -} - - -static ssize_t mts_attr_show_ap_gpio_pin(struct kobject *kobj, - struct kobj_attribute *attr, - char *buf) -{ - int value; - struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name); - - if (!pin) { - return -ENODEV; - } - - mutex_lock(&mts_io_mutex); - - value = gpio_get_value(pin->pin.gpio); - - mutex_unlock(&mts_io_mutex); - - if (value < 0) { - return value; - } - - if (pin->active_low) { - value = !value; - } - - return sprintf(buf, "%d\n", value); -} - -static ssize_t mts_attr_store_ap_gpio_pin(struct kobject *kobj, - struct kobj_attribute *attr, const char *buf, size_t count) -{ - int value; - struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name); - - if (!pin) { - return -ENODEV; - } - - if (sscanf(buf, "%i", &value) != 1) { - return -EINVAL; - } - - if (pin->active_low) { - value = !value; - } - - mutex_lock(&mts_io_mutex); - - gpio_set_value(pin->pin.gpio, value); - - mutex_unlock(&mts_io_mutex); - - return count; } static ssize_t mts_attr_show_ap_din(struct kobject *kobj, struct kobj_attribute *attr, char *buf) @@ -561,6 +491,7 @@ bool set_gpiob_info(struct ap_info* info) { info->teardown = &gpiob_teardown; info->attrs_start = 0; info->attrs_end = 0; + info->gpio_pin_name_by_attr_name = &gpiob_gpio_pin_name_by_attr_name; return true; } diff --git a/io-module/mtac_mfser.c b/io-module/mtac_mfser.c index ed97cc6..7c5862c 100644 --- a/io-module/mtac_mfser.c +++ b/io-module/mtac_mfser.c @@ -1,85 +1,16 @@ -struct gpio_pin *ap_mfser_pin_by_attr_name(const char *name) { - struct gpio_pin *pin; - char *pin_attr_name; - - if (!strcmp(name, "rs4xx-term-res:1")) { - pin_attr_name = "ap1-gpio3"; - } else if (!strcmp(name, "rts-override:1")) { - pin_attr_name = "ap1-gpio4"; - } else if (!strcmp(name, "rs4xx-term-res:2")) { - pin_attr_name = "ap2-gpio3"; - } else if (!strcmp(name, "rts-override:2")) { - pin_attr_name = "ap2-gpio4"; +static char* mfser_gpio_pin_name_by_attr_name(const char* name) { + if (! strcmp(name, "rs4xx-term-res:1")) { + return "ap1-gpio3"; + } else if (! strcmp(name, "rts-override:1")) { + return "ap1-gpio4"; + } else if (! strcmp(name, "rs4xx-term-res:2")) { + return "ap2-gpio3"; + } else if (! strcmp(name, "rts-override:2")) { + return "ap2-gpio4"; } else { - log_error("accessory card attribute %s not available", name); - return NULL; + log_error("attirbute name [%s] is invalid for MFSER", name); + return ""; } - - for (pin = gpio_pins; *pin->name; pin++) { - if (!strcmp(pin->pin.label, pin_attr_name)) { - return pin; - } - } - - log_error("pin with attr name %s not found", name); - - return NULL; -} - - -static ssize_t mts_attr_show_ap_mfser_pin(struct kobject *kobj, - struct kobj_attribute *attr, - char *buf) -{ - int value; - struct gpio_pin *pin = ap_mfser_pin_by_attr_name(attr->attr.name); - - if (!pin) { - return -ENODEV; - } - - mutex_lock(&mts_io_mutex); - - value = gpio_get_value(pin->pin.gpio); - - mutex_unlock(&mts_io_mutex); - - if (value < 0) { - return value; - } - - if (pin->active_low) { - value = !value; - } - - return sprintf(buf, "%d\n", value); -} - -static ssize_t mts_attr_store_ap_mfser_pin(struct kobject *kobj, - struct kobj_attribute *attr, const char *buf, size_t count) -{ - int value; - struct gpio_pin *pin = ap_mfser_pin_by_attr_name(attr->attr.name); - - if (!pin) { - return -ENODEV; - } - - if (sscanf(buf, "%i", &value) != 1) { - return -EINVAL; - } - - if (pin->active_low) { - value = !value; - } - - mutex_lock(&mts_io_mutex); - - gpio_set_value(pin->pin.gpio, value); - - mutex_unlock(&mts_io_mutex); - - return count; } static ssize_t mts_attr_show_mfser_mode(struct kobject *kobj, @@ -225,8 +156,8 @@ static bool mfser_setup(enum ap port) { log_error("failed to create attribute[%s]", buf); return false; } - attr->show = mts_attr_show_ap_mfser_pin; - attr->store = mts_attr_store_ap_mfser_pin; + attr->show = mts_attr_show_ap_gpio_pin; + attr->store = mts_attr_store_ap_gpio_pin; device_attrs[device_attrs_size++] = &attr->attr; sprintf(buf, "rts-override:%d", port); @@ -235,8 +166,8 @@ static bool mfser_setup(enum ap port) { log_error("failed to create attribute[%s]", buf); return false; } - attr->show = mts_attr_show_ap_mfser_pin; - attr->store = mts_attr_store_ap_mfser_pin; + attr->show = mts_attr_show_ap_gpio_pin; + attr->store = mts_attr_store_ap_gpio_pin; device_attrs[device_attrs_size++] = &attr->attr; // add attributes for eeprom contents @@ -276,6 +207,7 @@ bool set_mfser_info(struct ap_info* info) { info->teardown = &mfser_teardown; info->attrs_start = 0; info->attrs_end = 0; + info->gpio_pin_name_by_attr_name = &mfser_gpio_pin_name_by_attr_name; return true; } diff --git a/io-module/mts_io.h b/io-module/mts_io.h index f469b83..cc88392 100644 --- a/io-module/mts_io.h +++ b/io-module/mts_io.h @@ -112,6 +112,7 @@ struct ap_info { bool (*teardown)(enum ap port); int attrs_start; int attrs_end; + char* (*gpio_pin_name_by_attr_name)(const char* name); }; #endif /* ~__MTS_IO_H */ |