summaryrefslogtreecommitdiff
path: root/mtac_mfser.c
diff options
context:
space:
mode:
Diffstat (limited to 'mtac_mfser.c')
-rw-r--r--mtac_mfser.c430
1 files changed, 430 insertions, 0 deletions
diff --git a/mtac_mfser.c b/mtac_mfser.c
new file mode 100644
index 0000000..3f65a77
--- /dev/null
+++ b/mtac_mfser.c
@@ -0,0 +1,430 @@
+#define DRIVER_VERSION "v1.0.0"
+#define DRIVER_AUTHOR "John Klug <john.klug@multitech.com>"
+#define DRIVER_DESC "MTS Multi-Fuction Serial Accessory Card"
+#define DRIVER_NAME "mtac-mfser"
+
+#include <linux/types.h>
+#include <linux/gpio.h>
+#include <linux/platform_device.h>
+#include <linux/kmod.h>
+#include <linux/bitops.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/mtac.h>
+#include <linux/mts_io.h>
+
+static struct gpio_pin gpio_pins_mtcdt_mtac_mfser[] = {
+ // gpio pins for Accessory Card 1
+ {
+ .name = "AP1_GPIO1",
+ .pin = {
+ .gpio = AT91_PIN_PC6,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap1-gpio1",
+ },
+ .active_low = 1,
+ },
+ {
+ .name = "AP1_GPIO2",
+ .pin = {
+ .gpio = AT91_PIN_PC7,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap1-gpio2",
+ }
+ },
+ {
+ .name = "AP1_GPIO3",
+ .pin = {
+ .gpio = AT91_PIN_PC8,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap1-gpio3",
+ }
+ },
+ {
+ .name = "AP1_GPIO4",
+ .pin = {
+ .gpio = AT91_PIN_PC9,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap1-gpio4",
+ }
+ },
+
+
+ // gpio pins for Accessory Card 2
+ {
+ .name = "AP2_GPIO1",
+ .pin = {
+ .gpio = AT91_PIN_PC20,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap2-gpio1",
+ },
+ .active_low = 1,
+ },
+ {
+ .name = "AP2_GPIO2",
+ .pin = {
+ .gpio = AT91_PIN_PC21,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap2-gpio2",
+ }
+ },
+ {
+ .name = "AP2_GPIO3",
+ .pin = {
+ .gpio = AT91_PIN_PC22,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap2-gpio3",
+ }
+ },
+ {
+ .name = "AP2_GPIO4",
+ .pin = {
+ .gpio = AT91_PIN_PC23,
+ .flags = GPIOF_OUT_INIT_LOW,
+ .label = "ap2-gpio4",
+ }
+ },
+ { },
+};
+
+static char* mfser_gpio_pin_name_by_attr_name(const char* name, int port) {
+ switch (port) {
+ case port_1:
+ if (! strcmp(name, "rs4xx-term-res")) {
+ return "ap1-gpio3";
+ } else if (! strcmp(name, "rts-override")) {
+ return "ap1-gpio4";
+ } else {
+ log_error("attirbute name [%s] is invalid for MFSER in port %d", name, port);
+ return "";
+ }
+
+ case port_2:
+ if (! strcmp(name, "rs4xx-term-res")) {
+ return "ap2-gpio3";
+ } else if (! strcmp(name, "rts-override")) {
+ return "ap2-gpio4";
+ } else {
+ log_error("attirbute name [%s] is invalid for MFSER in port %d", name, port);
+ return "";
+ }
+ }
+ return "";
+}
+
+static ssize_t mts_attr_show_mfser_mode(struct kobject *kobj,
+ struct kobj_attribute *attr,
+ char *buf)
+{
+ int ret;
+ int port;
+ int modesel0;
+ int modesel1;
+ int port_index;
+
+ struct gpio_pin *pin_modesel0;
+ struct gpio_pin *pin_modesel1;
+
+ port = mtac_port_from_kobject(kobj);
+ if (port < 0) {
+ log_error("mtac_port_from_kobject returned %d", port);
+ return -EINVAL;
+ }
+
+ port_index = port - 1;
+ switch (port) {
+ case port_1:
+ pin_modesel0 = mtac_gpio_pin_by_name("AP1_GPIO1",port_index);
+ pin_modesel1 = mtac_gpio_pin_by_name("AP1_GPIO2",port_index);
+ break;
+
+ case port_2:
+ pin_modesel0 = mtac_gpio_pin_by_name("AP2_GPIO1",port_index);
+ pin_modesel1 = mtac_gpio_pin_by_name("AP2_GPIO2",port_index);
+ break;
+
+ default:
+ log_error("unknown serial-mode attr [%s]", attr->attr.name);
+ return -ENODEV;
+ }
+
+ if (!pin_modesel0 || !pin_modesel1)
+ return -ENODEV;
+
+ mutex_lock(&mtac_mutex);
+
+ modesel0 = gpio_get_value(pin_modesel0->pin.gpio);
+ modesel1 = gpio_get_value(pin_modesel1->pin.gpio);
+
+ if (modesel1 == 0 && modesel0 == 0)
+ ret = sprintf(buf, "loopback\n");
+ else if (modesel1 == 0 && modesel0 == 1)
+ ret = sprintf(buf, "rs232\n");
+ else if (modesel1 == 1 && modesel0 == 0)
+ ret = sprintf(buf, "rs485-half\n");
+ else if (modesel1 == 1 && modesel0 == 1)
+ ret = sprintf(buf, "rs422-485-full\n");
+ else
+ ret = sprintf(buf, "error\n");
+
+ mutex_unlock(&mtac_mutex);
+
+ return ret;
+}
+
+static ssize_t mts_attr_store_mfser_mode(struct kobject *kobj,
+ struct kobj_attribute *attr, const char *buf, size_t count)
+{
+ int port;
+ int modesel0;
+ int modesel1;
+ struct gpio_pin *pin_modesel0;
+ struct gpio_pin *pin_modesel1;
+ int port_index;
+
+ port = mtac_port_from_kobject(kobj);
+ if (port < 0) {
+ log_error("mtac_port_from_kobject returned %d", port);
+ return -EINVAL;
+ }
+
+ port_index = port - 1;
+ switch (port) {
+ case port_1:
+ pin_modesel0 = mtac_gpio_pin_by_name("AP1_GPIO1",port_index);
+ pin_modesel1 = mtac_gpio_pin_by_name("AP1_GPIO2",port_index);
+ break;
+
+ case port_2:
+ pin_modesel0 = mtac_gpio_pin_by_name("AP2_GPIO1",port_index);
+ pin_modesel1 = mtac_gpio_pin_by_name("AP2_GPIO2",port_index);
+ break;
+
+ default:
+ log_error("unknown serial-mode attr [%s]", attr->attr.name);
+ return -ENODEV;
+ }
+
+ if (!pin_modesel0 || !pin_modesel1)
+ return -ENODEV;
+
+ if (!strcasecmp(buf, "loopback")) {
+ modesel1 = 0;
+ modesel0 = 0;
+ }
+ else if (!strcasecmp(buf, "rs232")) {
+ modesel1 = 0;
+ modesel0 = 1;
+ }
+ else if (!strcasecmp(buf, "rs485-half")) {
+ modesel1 = 1;
+ modesel0 = 0;
+ }
+ else if (!strcasecmp(buf, "rs422-485-full")) {
+ modesel1 = 1;
+ modesel0 = 1;
+ }
+ else {
+ return -EINVAL;
+ }
+
+ mutex_lock(&mtac_mutex);
+
+ gpio_set_value(pin_modesel0->pin.gpio, modesel0);
+ gpio_set_value(pin_modesel1->pin.gpio, modesel1);
+
+ mutex_unlock(&mtac_mutex);
+
+ return count;
+}
+
+// 1 serial mode
+// 1 rs4xx term resistor
+// 1 rts override
+// 1 vendor-id
+// 1 product-id
+// 1 device-id
+// 1 hw-version
+// NULL
+static size_t ap_mfser_attrs_size = 8;
+
+static bool mfser_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;
+ struct kobject *subdir;
+
+ log_info("loading MFSER 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 MFSER in port %d failed", port);
+ return false;
+ }
+
+ mtac_set_port_pins(port_index,gpio_pins_mtcdt_mtac_mfser,subdir);
+
+ // create the link to the apX directory this card is in
+ // if we're in the first slot, we get plain "mfser"
+ // if we're in a different slot, we might need to use "mfser-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_MFSER)) {
+ count++;
+ }
+ }
+ }
+ }
+ if (count > 0) {
+ sprintf(buf, "mfser-%d", count + 1);
+ } else {
+ sprintf(buf, "mfser");
+ }
+ 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_mfser_attrs_size, GFP_KERNEL);
+ if (! attrs) {
+ log_error("failed to allocate attribute space for port %d", port);
+ return false;
+ }
+
+ sprintf(buf, "serial-mode");
+ attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RW);
+ if (! attr) {
+ log_error("failed to create attribute [%s] for MFSER in port %d", buf, port);
+ return false;
+ }
+ attr->show = mts_attr_show_mfser_mode;
+ attr->store = mts_attr_store_mfser_mode;
+ attrs[index++] = &attr->attr;
+
+ sprintf(buf, "rs4xx-term-res");
+ attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RW);
+ if (! attr) {
+ log_error("failed to create attribute [%s] for MFSER in port %d", buf, port);
+ return false;
+ }
+ attr->show = mtac_attr_show_ap_gpio_pin;
+ attr->store = mtac_attr_store_ap_gpio_pin;
+ attrs[index++] = &attr->attr;
+
+ sprintf(buf, "rts-override");
+ attr = mtac_create_attribute(buf, MTS_ATTR_MODE_RW);
+ if (! attr) {
+ log_error("failed to create attribute [%s] for MFSER in port %d", buf, port);
+ return false;
+ }
+ attr->show = mtac_attr_show_ap_gpio_pin;
+ attr->store = mtac_attr_store_ap_gpio_pin;
+ 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 MFSER in port %d", port);
+ return false;
+ }
+
+ attrs[index] = NULL;
+
+ mtac_port_info[port_index]->attr_group.attrs = attrs;
+ if (sysfs_create_group(mtac_port_info[port_index]->subdirs, &mtac_port_info[port_index]->attr_group)) {
+ log_error("sysfs_create_group failed for MFSER in port %d", port);
+ return false;
+ }
+
+ return true;
+}
+
+static bool mfser_teardown(enum ap port) {
+ int i;
+ int port_index = port - 1;
+ struct attribute **attrs = mtac_port_info[port_index]->attr_group.attrs;
+
+ log_info("unloading MFSER accessory card in port %d", port);
+
+ // clean up allocated memory for attributes
+ for (i = 0; i < ap_mfser_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_mfser_info(struct ap_info* info) {
+ snprintf(info->product_id, 32, "%s", PRODUCT_ID_MTAC_MFSER);
+ info->setup = &mfser_setup;
+ info->teardown = &mfser_teardown;
+ info->gpio_pin_name_by_attr_name = &mfser_gpio_pin_name_by_attr_name;
+}
+
+/*
+ * Loop through all the slots, and set up the
+ * mtac-mfser driver for all slots.
+ */
+static int __init mtac_mfser_init(void)
+{
+ int slot_count = 0;
+
+ slot_count = mtac_find(set_mfser_info,PRODUCT_ID_MTAC_MFSER);
+
+ if (slot_count < 1) {
+ log_debug("No MTAC MFSER 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_mfser_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_MFSER)) {
+ if (mtac_port_info[port_index]->setup == &mfser_setup) {
+ mtac_port_info[port_index]->teardown(port_index+1);
+ kfree(mtac_port_info[port_index]);
+ }
+ }
+ }
+ log_info("exiting");
+}
+
+module_init(mtac_mfser_init);
+module_exit(mtac_mfser_exit);
+
+MODULE_AUTHOR(DRIVER_AUTHOR);
+MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_VERSION(DRIVER_VERSION);
+MODULE_LICENSE("GPL");