static struct kobj_attribute* create_attribute(const char* _name, umode_t _mode) { char* attr_name; struct kobj_attribute* _attr; _attr = kzalloc(sizeof(struct kobj_attribute), GFP_KERNEL); if (! _attr) { log_error("kzalloc of attribute [%s] failed", _name); return NULL; } sysfs_attr_init(_attr); attr_name = kstrdup(_name, GFP_KERNEL); if (! attr_name) { log_error("GFP_KERNEL dup failed for attribute [%s]", _name); return NULL; } _attr->attr.name = attr_name; _attr->attr.mode = _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; 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-addr")) { value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", 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 if (! strcmp(attr->attr.name, "eui")) { value = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X\n", 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]); } else { 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 attribute** attrs, int* index) { char buf[32]; struct kobj_attribute* kobj_attr; switch (type) { case MTAC_ETH_0_0: sprintf(buf, "mac-addr"); 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; } kobj_attr->show = ap_show_product_info; attrs[(*index)++] = &kobj_attr->attr; break; case MTAC_GPIOB_0_0: case MTAC_MFSER_0_0: case MTAC_PULSE_1_0: break; case MTAC_LORA_0_0: case MTAC_LORA_1_0: case MTAC_LORA_1_1: case MTAC_LORA_1_5: sprintf(buf, "eui"); 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; } kobj_attr->show = ap_show_product_info; attrs[(*index)++] = &kobj_attr->attr; break; default: log_error("invalid accessory card type"); return false; } 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; } kobj_attr->show = ap_show_product_info; attrs[(*index)++] = &kobj_attr->attr; 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; } kobj_attr->show = ap_show_product_info; attrs[(*index)++] = &kobj_attr->attr; 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; } kobj_attr->show = ap_show_product_info; attrs[(*index)++] = &kobj_attr->attr; 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; } 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, int port) { struct gpio_pin *pin; char *pin_attr_name; int port_index = port - 1; 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)) { 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; 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; } 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; 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; } 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; }