From 555f2f237166b2ffefa7b7f39dbaeb98be5ddaee Mon Sep 17 00:00:00 2001 From: John Klug Date: Mon, 15 Oct 2018 14:50:19 -0500 Subject: mtac Ethernet derived from mts-io 2.2.2 --- mtac_eth.c | 265 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 mtac_eth.c (limited to 'mtac_eth.c') diff --git a/mtac_eth.c b/mtac_eth.c new file mode 100644 index 0000000..9d7dcce --- /dev/null +++ b/mtac_eth.c @@ -0,0 +1,265 @@ +#define DRIVER_VERSION "v1.0.0" +#define DRIVER_AUTHOR "John Klug " +#define DRIVER_DESC "MTS Ethernet Accessory Card" +#define DRIVER_NAME "mtac-eth" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct gpio_pin gpio_pins_mtcdt_mtac_eth[] = { + // gpio pins for Accessory Card 1 + { + .name = "AP1_RESET", + .pin = { + .gpio = AT91_PIN_PB12, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "ap1-reset", + } + }, + + + // gpio pins for Accessory Card 2 + { + .name = "AP2_RESET", + .pin = { + .gpio = AT91_PIN_PB13, + .flags = GPIOF_OUT_INIT_HIGH, + .label = "ap2-reset", + } + }, + { }, +}; + +ssize_t eth_show_mac_addr(struct kobject *kobj, struct kobj_attribute *attr, char *buf) +{ + int retval = -1; + int port; + int port_index; + struct mts_ap_eeprom_layout *app; + + port = mtac_port_from_kobject(kobj); + if (port < 1) { + log_error("mtac_port_from_kobject returned %d", port); + return -1; + } + port_index = port - 1; + app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index]; + if (! strcmp(attr->attr.name, "mac-addr")) { + retval = sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X\n", + app->mac_addr[0], + app->mac_addr[1], + app->mac_addr[2], + app->mac_addr[3], + app->mac_addr[4], + app->mac_addr[5]); + } + return retval; +} + +static char* eth_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("attribute name [%s] is invalid for ETH in port %d", name, port); + return ""; + } + + case port_2: + if (! strcmp(name, "reset")) { + return "ap2-reset"; + } else { + log_error("attribute name [%s] is invalid for ETH in port %d", name, port); + return ""; + } + } + /* NOTREACHED */ + return ""; +} + +// 1 vendor-id +// 1 product-id +// 1 device-id +// 1 hw-version +// 1 mac-addr or eui +// 1 reset +// NULL +static size_t ap_eth_attrs_size = 7; + +static bool eth_setup(enum ap port) { + int i; + int port_index = port - 1; + int index = 0; + int count = 0; + int ret; + char buf[32]; + struct attribute **attrs; + struct kobj_attribute* attr; + struct kobject *subdir; + + log_info("loading ETH accessory card in port %d", port); + + sprintf(buf, "ap%d", port); + subdir = kobject_create_and_add(buf, &mts_io_platform_device->dev.kobj); + if (! subdir) { + log_error("kobject_create_and_add for ETH in port %d failed", port); + return false; + } + + mtac_set_port_pins(port_index,gpio_pins_mtcdt_mtac_eth,subdir); + + // create the link to the apX directory this card is in + // if we're in the first slot, we get plain "eth" + // if we're in a different slot, we might need to use "eth-2" to differentiate + if (port > 1) { + for (i = 1; i < port; i++) { + if (mtac_port_info[i - 1]) { + if (strstr(mtac_port_info[i - 1]->product_id, PRODUCT_ID_MTAC_ETH)) { + count++; + } + } + } + } + if (count > 0) { + sprintf(buf, "eth-%d", count + 1); + } else { + sprintf(buf, "eth"); + } + ret = sysfs_create_link(mtac_port_info[port_index]->subdirs->parent, mtac_port_info[port_index]->subdirs, buf); + if (ret) { + log_error("failed to link [%s] to [%s], %d", buf, mtac_port_info[port_index]->subdirs->name, ret); + } + + attrs = kzalloc(sizeof(struct attribute*) * ap_eth_attrs_size, GFP_KERNEL); + if (! attrs) { + log_error("failed to allocate attribute space for port %d", port); + return false; + } + + sprintf(buf, "reset"); + attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RW); + if (! attr) { + log_error("failed to create attribute [%s] for ETH in port %d", buf, port); + kfree(attrs); + return false; + } + mtac_port_info[port_index]->attr_group.attrs = attrs; + + attr->show = mtac_attr_show_ap_gpio_pin; + attr->store = mtac_attr_store_ap_gpio_pin; + attrs[index++] = &attr->attr; + + sprintf(buf, "mac-addr"); + attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RO); + if (! attr) { + log_error("failed to create attribute [%s] for ETH in port %d", buf, port); + return false; + } + attr->show = eth_show_mac_addr; + attrs[index++] = &attr->attr; + + + // add attributes for eeprom contents + if (! mtac_add_product_info_attributes(port, attrs, &index)) { + log_error("failed to add product info attributes for ETH in port %d", port); + return false; + } + attrs[index] = NULL; + + if (sysfs_create_group(mtac_port_info[port_index]->subdirs, &mtac_port_info[port_index]->attr_group)) { + log_error("sysfs_create_group failed for ETH in port %d", port); + return false; + } + + return true; +} + +static bool eth_teardown(enum ap port) { + int i; + int port_index = port - 1; + struct attribute **attrs = mtac_port_info[port_index]->attr_group.attrs; + + mtac_port_info[port_index]->attr_group.attrs = NULL; + + log_info("unloading ETH accessory card in port %d", port); + + // clean up allocated memory for attributes + for (i = 0; i < ap_eth_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 (mtac_port_info[port_index]->subdirs) { + kobject_put(mtac_port_info[port_index]->subdirs); + } + mtac_clear_port_pins(port_index); + return true; +} + +void set_eth_info(struct ap_info* info) { + snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_ETH); + info->setup = ð_setup; + info->teardown = ð_teardown; + info->gpio_pin_name_by_attr_name = ð_gpio_pin_name_by_attr_name; +} +/* + * Loop through all the slots, and set up the + * mtac-eth driver for all slots. + */ +static int __init mtac_eth_init(void) +{ + int slot_count = 0; + slot_count = mtac_find(set_eth_info,PRODUCT_ID_MTAC_ETH); + + if (slot_count < 1) { + log_debug("No MTAC ETH found"); + if (slot_count < 0) + return slot_count; + else + return -ENXIO; + + } + + return 0; +} + +/* We can only tear down our own device */ +static void __exit mtac_eth_exit(void) +{ + int port_index; + struct mts_ap_eeprom_layout *app; + + for (port_index = 0; port_index < NUM_AP; port_index++) { + app = (struct mts_ap_eeprom_layout *)mts_ap_eeprom[port_index]; + if (app && strstr(app->product_id, PRODUCT_ID_MTAC_ETH)) { + if (mtac_port_info[port_index]->setup == ð_setup) { + mtac_port_info[port_index]->teardown(port_index+1); + kfree(mtac_port_info[port_index]); + } + } + } + log_info("exiting"); +} + +module_init(mtac_eth_init); +module_exit(mtac_eth_exit); + +MODULE_AUTHOR(DRIVER_AUTHOR); +MODULE_DESCRIPTION(DRIVER_DESC); +MODULE_VERSION(DRIVER_VERSION); +MODULE_LICENSE("GPL"); -- cgit v1.2.3