diff options
Diffstat (limited to 'io-module/mtac.c')
-rw-r--r-- | io-module/mtac.c | 169 |
1 files changed, 101 insertions, 68 deletions
diff --git a/io-module/mtac.c b/io-module/mtac.c index 1c88b40..9b27e0d 100644 --- a/io-module/mtac.c +++ b/io-module/mtac.c @@ -4,7 +4,7 @@ static struct kobj_attribute* create_attribute(const char* _name, umode_t _mode) _attr = kzalloc(sizeof(struct kobj_attribute), GFP_KERNEL); if (! _attr) { - log_error("kzalloc of attribute %s failed", _name); + log_error("kzalloc of attribute [%s] failed", _name); return NULL; } @@ -21,48 +21,79 @@ static struct kobj_attribute* create_attribute(const char* _name, umode_t _mode) return _attr; } +static int port_from_kobject(struct kobject *kobj) { + int port; + const char *name; + + name = kobj->name; + if (! name) { + log_error("kobject->name is NULL"); + return -1; + } + + if (sscanf(name, "ap%d", &port) < 1) { + log_error("failed to scan port from kobject->name [%s]", name); + return -1; + } + + if (port < 1 || port > NUM_AP) { + log_error("port number %d is invalid", port); + return -1; + } + + return port; +} + static ssize_t ap_show_product_info(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { ssize_t value; int port; + int port_index; - if (sscanf(attr->attr.name, "vendor-id:%d", &port) > 0) { - value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].vendor_id); - } else if (sscanf(attr->attr.name, "product-id:%d", &port) > 0) { - value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].product_id); - } else if (sscanf(attr->attr.name, "device-id:%d", &port) > 0) { - value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].device_id); - } else if (sscanf(attr->attr.name, "hw-version:%d", &port) > 0) { - value = snprintf(buf, 32, "%s\n", ap_eeprom[port - 1].hw_version); - } else if (sscanf(attr->attr.name, "mac-eth:%d", &port) > 0) { + port = port_from_kobject(kobj); + if (port < 1) { + log_error("port_from_kobject returned %d", port); + return -1; + } + port_index = port - 1; + + if (! strcmp(attr->attr.name, "vendor-id")) { + value = snprintf(buf, 32, "%s\n", ap_eeprom[port_index].vendor_id); + } else if (! strcmp(attr->attr.name, "product-id")) { + value = snprintf(buf, 32, "%s\n", ap_eeprom[port_index].product_id); + } else if (! strcmp(attr->attr.name, "device-id")) { + value = snprintf(buf, 32, "%s\n", ap_eeprom[port_index].device_id); + } else if (! strcmp(attr->attr.name, "hw-version")) { + value = snprintf(buf, 32, "%s\n", ap_eeprom[port_index].hw_version); + } else if (! strcmp(attr->attr.name, "mac-eth")) { value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", - ap_eeprom[port - 1].mac_addr[0], - ap_eeprom[port - 1].mac_addr[1], - ap_eeprom[port - 1].mac_addr[2], - ap_eeprom[port - 1].mac_addr[3], - ap_eeprom[port - 1].mac_addr[4], - ap_eeprom[port - 1].mac_addr[5]); + 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]); } else { - log_error("attribute '%s' not found", attr->attr.name); + log_error("attribute [%s] not found", attr->attr.name); value = -1; } return value; } -static bool ap_add_product_info_attributes(int port, int type) { - struct kobj_attribute *attr; +static bool ap_add_product_info_attributes(int port, int type, struct attribute** attrs, int* index) { char buf[32]; + struct kobj_attribute* kobj_attr; switch (type) { case MTAC_ETH_0_0: - sprintf(buf, "mac-eth:%d", port); - attr = create_attribute(buf, MTS_ATTR_MODE_RO); - if (! attr) { - log_error("failed to create attribute[%s]", buf); + sprintf(buf, "mac-eth"); + kobj_attr = create_attribute(buf, MTS_ATTR_MODE_RO); + if (! kobj_attr) { + log_error("failed to create attribute [%s] in port %d", buf, port); return false; } - attr->show = ap_show_product_info; - device_attrs[device_attrs_size++] = &attr->attr; + kobj_attr->show = ap_show_product_info; + attrs[*index++] = &kobj_attr->attr; break; case MTAC_GPIOB_0_0: @@ -73,65 +104,51 @@ static bool ap_add_product_info_attributes(int port, int type) { return false; } - sprintf(buf, "vendor-id:%d", port); - attr = create_attribute(buf, MTS_ATTR_MODE_RO); - if (! attr) { - log_error("failed to create attribute[%s]", buf); + sprintf(buf, "vendor-id"); + kobj_attr = create_attribute(buf, MTS_ATTR_MODE_RO); + if (! kobj_attr) { + log_error("failed to create attribute [%s] in port %d", buf, port); return false; } - attr->show = ap_show_product_info; - device_attrs[device_attrs_size++] = &attr->attr; + kobj_attr->show = ap_show_product_info; + attrs[(*index)++] = &kobj_attr->attr; - sprintf(buf, "product-id:%d", port); - attr = create_attribute(buf, MTS_ATTR_MODE_RO); - if (! attr) { - log_error("failed to create attribute[%s]", buf); + sprintf(buf, "product-id"); + kobj_attr = create_attribute(buf, MTS_ATTR_MODE_RO); + if (! kobj_attr) { + log_error("failed to create attribute [%s] in port %d", buf, port); return false; } - attr->show = ap_show_product_info; - device_attrs[device_attrs_size++] = &attr->attr; + kobj_attr->show = ap_show_product_info; + attrs[(*index)++] = &kobj_attr->attr; - sprintf(buf, "device-id:%d", port); - attr = create_attribute(buf, MTS_ATTR_MODE_RO); - if (! attr) { - log_error("failed to create attribute[%s]", buf); + sprintf(buf, "device-id"); + kobj_attr = create_attribute(buf, MTS_ATTR_MODE_RO); + if (! kobj_attr) { + log_error("failed to create attribute [%s] in port %d", buf, port); return false; } - attr->show = ap_show_product_info; - device_attrs[device_attrs_size++] = &attr->attr; + kobj_attr->show = ap_show_product_info; + attrs[(*index)++] = &kobj_attr->attr; - sprintf(buf, "hw-version:%d", port); - attr = create_attribute(buf, MTS_ATTR_MODE_RO); - if (! attr) { - log_error("failed to create attribute[%s]", buf); + sprintf(buf, "hw-version"); + kobj_attr = create_attribute(buf, MTS_ATTR_MODE_RO); + if (! kobj_attr) { + log_error("failed to create attribute [%s] in port %d", buf, port); return false; } - attr->show = ap_show_product_info; - device_attrs[device_attrs_size++] = &attr->attr; + kobj_attr->show = ap_show_product_info; + attrs[(*index)++] = &kobj_attr->attr; return true; } -struct gpio_pin *ap_gpio_pin_by_attr_name(const char *name) { +struct gpio_pin *ap_gpio_pin_by_attr_name(const char *name, int port) { struct gpio_pin *pin; char *pin_attr_name; - long port; - int port_index; - char *colon; + int port_index = port - 1; - 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); + pin_attr_name = port_info[port_index]->gpio_pin_name_by_attr_name(name, port); for (pin = gpio_pins; *pin->name; pin++) { if (!strcmp(pin->pin.label, pin_attr_name)) { @@ -148,8 +165,16 @@ static ssize_t mts_attr_show_ap_gpio_pin(struct kobject *kobj, char *buf) { int value; - struct gpio_pin *pin = ap_gpio_pin_by_attr_name(attr->attr.name); + int port; + struct gpio_pin *pin; + + port = port_from_kobject(kobj); + if (port < 1) { + log_error("port_from_kobject returned %d", port); + return -EINVAL; + } + pin = ap_gpio_pin_by_attr_name(attr->attr.name, port); if (!pin) { return -ENODEV; } @@ -175,8 +200,16 @@ 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); + int port; + struct gpio_pin *pin; + port = port_from_kobject(kobj); + if (port < 1) { + log_error("port_from_kobject returned %d", port); + return -EINVAL; + } + + pin = ap_gpio_pin_by_attr_name(attr->attr.name, port); if (!pin) { return -ENODEV; } |