TTY: Sourced serial device functions out into serial.c/serial.h.
authorFlorian Pose <fp@igh-essen.com>
Fri, 08 Jan 2010 13:10:12 +0100
changeset 1613 070b4b420ab1
parent 1612 c6779ebcda88
child 1614 2f362d2b6d6e
TTY: Sourced serial device functions out into serial.c/serial.h.
examples/Makefile.am
examples/tty/Kbuild.in
examples/tty/Makefile.am
examples/tty/serial.c
examples/tty/serial.h
examples/tty/tty.c
--- a/examples/Makefile.am	Fri Jan 08 11:40:27 2010 +0100
+++ b/examples/Makefile.am	Fri Jan 08 13:10:12 2010 +0100
@@ -27,12 +27,19 @@
 #
 #------------------------------------------------------------------------------
 
+SUBDIRS =
+
 if ENABLE_USERLIB
-SUBDIRS = \
+SUBDIRS += \
 	dc_user \
 	user
 endif
 
+if ENABLE_TTY
+SUBDIRS += \
+	tty
+endif
+
 DIST_SUBDIRS = \
 	dc_rtai \
 	dc_user \
--- a/examples/tty/Kbuild.in	Fri Jan 08 11:40:27 2010 +0100
+++ b/examples/tty/Kbuild.in	Fri Jan 08 13:10:12 2010 +0100
@@ -33,7 +33,11 @@
 
 obj-m := ec_tty_example.o
 
-ec_tty_example-objs := tty.o
+ec_tty_example-objs := \
+	serial.o \
+	tty.o
+
+CFLAGS_tty.o := -I$(src)
 
 KBUILD_EXTRA_SYMBOLS := \
 	@abs_top_builddir@/Module.symvers \
--- a/examples/tty/Makefile.am	Fri Jan 08 11:40:27 2010 +0100
+++ b/examples/tty/Makefile.am	Fri Jan 08 13:10:12 2010 +0100
@@ -31,9 +31,12 @@
 #
 #------------------------------------------------------------------------------
 
+noinst_HEADERS = \
+	serial.h \
+	tty.c
+
 EXTRA_DIST = \
-	Kbuild.in \
-	tty.c
+	Kbuild.in
 
 BUILT_SOURCES = \
 	Kbuild
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/tty/serial.c	Fri Jan 08 13:10:12 2010 +0100
@@ -0,0 +1,438 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+ *  Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  ---
+ *
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ *****************************************************************************/
+
+#include <linux/module.h>
+#include <linux/err.h>
+
+#include "../../include/ecrt.h" // EtherCAT realtime interface
+#include "../../include/ectty.h" // EtherCAT TTY interface
+
+/*****************************************************************************/
+
+// Optional features
+#define PFX "ec_tty_example: "
+
+/*****************************************************************************/
+
+#define VendorIdBeckhoff 0x00000002
+#define ProductCodeBeckhoffEL6002 0x17723052
+#define Beckhoff_EL6002 VendorIdBeckhoff, ProductCodeBeckhoffEL6002
+
+typedef enum {
+    SER_REQUEST_INIT,
+    SER_WAIT_FOR_INIT_RESPONSE,
+    SER_READY
+} serial_state_t;
+
+typedef struct {
+    ec_tty_t *tty;
+    ec_slave_config_t *sc;
+
+    size_t max_tx_data_size;
+    size_t max_rx_data_size;
+
+    u8 *tx_data;
+    u8 tx_data_size;
+
+    serial_state_t state;
+
+    u8 tx_request_toggle;
+    u8 tx_accepted_toggle;
+
+    u8 rx_request_toggle;
+    u8 rx_accepted_toggle;
+
+    u16 control;
+
+    u32 off_ctrl;
+    u32 off_tx;
+    u32 off_status;
+    u32 off_rx;
+} el6002_t;
+
+static el6002_t *ser = NULL;
+        
+/*****************************************************************************/
+
+/* Beckhoff EL6002
+ * Vendor ID:       0x00000002
+ * Product code:    0x17723052
+ * Revision number: 0x00100000
+ */
+
+ec_pdo_entry_info_t el6002_pdo_entries[] = {
+   {0x7001, 0x01, 16}, /* Ctrl */
+   {0x7000, 0x11, 8}, /* Data Out 0 */
+   {0x7000, 0x12, 8}, /* Data Out 1 */
+   {0x7000, 0x13, 8}, /* Data Out 2 */
+   {0x7000, 0x14, 8}, /* Data Out 3 */
+   {0x7000, 0x15, 8}, /* Data Out 4 */
+   {0x7000, 0x16, 8}, /* Data Out 5 */
+   {0x7000, 0x17, 8}, /* Data Out 6 */
+   {0x7000, 0x18, 8}, /* Data Out 7 */
+   {0x7000, 0x19, 8}, /* Data Out 8 */
+   {0x7000, 0x1a, 8}, /* Data Out 9 */
+   {0x7000, 0x1b, 8}, /* Data Out 10 */
+   {0x7000, 0x1c, 8}, /* Data Out 11 */
+   {0x7000, 0x1d, 8}, /* Data Out 12 */
+   {0x7000, 0x1e, 8}, /* Data Out 13 */
+   {0x7000, 0x1f, 8}, /* Data Out 14 */
+   {0x7000, 0x20, 8}, /* Data Out 15 */
+   {0x7000, 0x21, 8}, /* Data Out 16 */
+   {0x7000, 0x22, 8}, /* Data Out 17 */
+   {0x7000, 0x23, 8}, /* Data Out 18 */
+   {0x7000, 0x24, 8}, /* Data Out 19 */
+   {0x7000, 0x25, 8}, /* Data Out 20 */
+   {0x7000, 0x26, 8}, /* Data Out 21 */
+   {0x7011, 0x01, 16}, /* Ctrl */
+   {0x7010, 0x11, 8}, /* Data Out 0 */
+   {0x7010, 0x12, 8}, /* Data Out 1 */
+   {0x7010, 0x13, 8}, /* Data Out 2 */
+   {0x7010, 0x14, 8}, /* Data Out 3 */
+   {0x7010, 0x15, 8}, /* Data Out 4 */
+   {0x7010, 0x16, 8}, /* Data Out 5 */
+   {0x7010, 0x17, 8}, /* Data Out 6 */
+   {0x7010, 0x18, 8}, /* Data Out 7 */
+   {0x7010, 0x19, 8}, /* Data Out 8 */
+   {0x7010, 0x1a, 8}, /* Data Out 9 */
+   {0x7010, 0x1b, 8}, /* Data Out 10 */
+   {0x7010, 0x1c, 8}, /* Data Out 11 */
+   {0x7010, 0x1d, 8}, /* Data Out 12 */
+   {0x7010, 0x1e, 8}, /* Data Out 13 */
+   {0x7010, 0x1f, 8}, /* Data Out 14 */
+   {0x7010, 0x20, 8}, /* Data Out 15 */
+   {0x7010, 0x21, 8}, /* Data Out 16 */
+   {0x7010, 0x22, 8}, /* Data Out 17 */
+   {0x7010, 0x23, 8}, /* Data Out 18 */
+   {0x7010, 0x24, 8}, /* Data Out 19 */
+   {0x7010, 0x25, 8}, /* Data Out 20 */
+   {0x7010, 0x26, 8}, /* Data Out 21 */
+   {0x6001, 0x01, 16}, /* Status */
+   {0x6000, 0x11, 8}, /* Data In 0 */
+   {0x6000, 0x12, 8}, /* Data In 1 */
+   {0x6000, 0x13, 8}, /* Data In 2 */
+   {0x6000, 0x14, 8}, /* Data In 3 */
+   {0x6000, 0x15, 8}, /* Data In 4 */
+   {0x6000, 0x16, 8}, /* Data In 5 */
+   {0x6000, 0x17, 8}, /* Data In 6 */
+   {0x6000, 0x18, 8}, /* Data In 7 */
+   {0x6000, 0x19, 8}, /* Data In 8 */
+   {0x6000, 0x1a, 8}, /* Data In 9 */
+   {0x6000, 0x1b, 8}, /* Data In 10 */
+   {0x6000, 0x1c, 8}, /* Data In 11 */
+   {0x6000, 0x1d, 8}, /* Data In 12 */
+   {0x6000, 0x1e, 8}, /* Data In 13 */
+   {0x6000, 0x1f, 8}, /* Data In 14 */
+   {0x6000, 0x20, 8}, /* Data In 15 */
+   {0x6000, 0x21, 8}, /* Data In 16 */
+   {0x6000, 0x22, 8}, /* Data In 17 */
+   {0x6000, 0x23, 8}, /* Data In 18 */
+   {0x6000, 0x24, 8}, /* Data In 19 */
+   {0x6000, 0x25, 8}, /* Data In 20 */
+   {0x6000, 0x26, 8}, /* Data In 21 */
+   {0x6011, 0x01, 16}, /* Status */
+   {0x6010, 0x11, 8}, /* Data In 0 */
+   {0x6010, 0x12, 8}, /* Data In 1 */
+   {0x6010, 0x13, 8}, /* Data In 2 */
+   {0x6010, 0x14, 8}, /* Data In 3 */
+   {0x6010, 0x15, 8}, /* Data In 4 */
+   {0x6010, 0x16, 8}, /* Data In 5 */
+   {0x6010, 0x17, 8}, /* Data In 6 */
+   {0x6010, 0x18, 8}, /* Data In 7 */
+   {0x6010, 0x19, 8}, /* Data In 8 */
+   {0x6010, 0x1a, 8}, /* Data In 9 */
+   {0x6010, 0x1b, 8}, /* Data In 10 */
+   {0x6010, 0x1c, 8}, /* Data In 11 */
+   {0x6010, 0x1d, 8}, /* Data In 12 */
+   {0x6010, 0x1e, 8}, /* Data In 13 */
+   {0x6010, 0x1f, 8}, /* Data In 14 */
+   {0x6010, 0x20, 8}, /* Data In 15 */
+   {0x6010, 0x21, 8}, /* Data In 16 */
+   {0x6010, 0x22, 8}, /* Data In 17 */
+   {0x6010, 0x23, 8}, /* Data In 18 */
+   {0x6010, 0x24, 8}, /* Data In 19 */
+   {0x6010, 0x25, 8}, /* Data In 20 */
+   {0x6010, 0x26, 8}, /* Data In 21 */
+};
+
+ec_pdo_info_t el6002_pdos[] = {
+   {0x1604, 23, el6002_pdo_entries + 0}, /* COM RxPDO-Map Outputs Ch.1 */
+   {0x1605, 23, el6002_pdo_entries + 23}, /* COM RxPDO-Map Outputs Ch.2 */
+   {0x1a04, 23, el6002_pdo_entries + 46}, /* COM TxPDO-Map Inputs Ch.1 */
+   {0x1a05, 23, el6002_pdo_entries + 69}, /* COM TxPDO-Map Inputs Ch.2 */
+};
+
+ec_sync_info_t el6002_syncs[] = {
+   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
+   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
+   {2, EC_DIR_OUTPUT, 2, el6002_pdos + 0, EC_WD_DISABLE},
+   {3, EC_DIR_INPUT, 2, el6002_pdos + 2, EC_WD_DISABLE},
+   {0xff}
+};
+
+/****************************************************************************/
+
+int el6002_init(el6002_t *ser, ec_master_t *master, u16 position,
+        ec_domain_t *domain)
+{
+    int ret = 0;
+
+    ser->tty = ectty_create();
+    if (IS_ERR(ser->tty)) {
+        printk(KERN_ERR PFX "Failed to create tty.\n");
+        ret = PTR_ERR(ser->tty);
+        goto out_return;
+    }
+
+    ser->sc = NULL;
+    ser->max_tx_data_size = 22;
+    ser->max_rx_data_size = 22;
+    ser->tx_data = NULL;
+    ser->tx_data_size = 0;
+    ser->state = SER_REQUEST_INIT;
+    ser->tx_request_toggle = 0;
+    ser->rx_accepted_toggle = 0;
+    ser->control = 0x0000;
+    ser->off_ctrl = 0;
+    ser->off_tx = 0;
+    ser->off_status = 0;
+    ser->off_rx = 0;
+
+    if (!(ser->sc = ecrt_master_slave_config(
+                    master, 0, position, Beckhoff_EL6002))) {
+        printk(KERN_ERR PFX "Failed to create slave configuration.\n");
+        ret = -EBUSY;
+        goto out_free_tty;
+    }
+
+    if (ecrt_slave_config_pdos(ser->sc, EC_END, el6002_syncs)) {
+        printk(KERN_ERR PFX "Failed to configure PDOs.\n");
+        ret = -ENOMEM;
+        goto out_free_tty;
+    }
+    
+    ret = ecrt_slave_config_reg_pdo_entry(
+            ser->sc, 0x7001, 0x01, domain, NULL);
+    if (ret < 0) {
+        printk(KERN_ERR PFX "Failed to register PDO entry.\n");
+        goto out_free_tty;
+    }
+    ser->off_ctrl = ret;
+
+    ret = ecrt_slave_config_reg_pdo_entry(
+            ser->sc, 0x7000, 0x11, domain, NULL);
+    if (ret < 0) {
+        printk(KERN_ERR PFX "Failed to register PDO entry.\n");
+        goto out_free_tty;
+    }
+    ser->off_tx = ret;
+
+    ret = ecrt_slave_config_reg_pdo_entry(
+            ser->sc, 0x6001, 0x01, domain, NULL);
+    if (ret < 0) {
+        printk(KERN_ERR PFX "Failed to register PDO entry.\n");
+        goto out_free_tty;
+    }
+    ser->off_status = ret;
+
+    ret = ecrt_slave_config_reg_pdo_entry(
+            ser->sc, 0x6000, 0x11, domain, NULL);
+    if (ret < 0) {
+        printk(KERN_ERR PFX "Failed to register PDO entry.\n");
+        goto out_free_tty;
+    }
+    ser->off_rx = ret;
+
+    if (ser->max_tx_data_size > 0) {
+        ser->tx_data = kmalloc(ser->max_tx_data_size, GFP_KERNEL);
+        if (ser->tx_data == NULL) {
+            ret = -ENOMEM;
+            goto out_free_tty;
+        }
+    }
+
+    return 0;
+
+out_free_tty:
+    ectty_free(ser->tty);
+out_return:
+    return ret;
+}
+
+/****************************************************************************/
+
+void el6002_clear(el6002_t *ser)
+{
+    ectty_free(ser->tty);
+    if (ser->tx_data) {
+        kfree(ser->tx_data);
+    }
+}
+
+/****************************************************************************/
+
+void el6002_run(el6002_t *ser, u8 *pd)
+{
+    u16 status = EC_READ_U16(pd + ser->off_status);
+    u8 *rx_data = pd + ser->off_rx;
+    uint8_t tx_accepted_toggle, rx_request_toggle;
+
+    switch (ser->state) {
+        case SER_READY:
+
+            /* Send data */
+            
+            tx_accepted_toggle = status & 0x0001;
+            if (tx_accepted_toggle != ser->tx_accepted_toggle) { // ready
+                ser->tx_data_size =
+                    ectty_tx_data(ser->tty, ser->tx_data, ser->max_tx_data_size);
+                if (ser->tx_data_size) {
+                    printk(KERN_INFO PFX "Sending %u bytes.\n", ser->tx_data_size);
+                    ser->tx_request_toggle = !ser->tx_request_toggle;
+                    ser->tx_accepted_toggle = tx_accepted_toggle;
+                }
+            }
+
+            /* Receive data */
+
+            rx_request_toggle = status & 0x0002;
+            if (rx_request_toggle != ser->rx_request_toggle) {
+                uint8_t rx_data_size = status >> 8;
+                ser->rx_request_toggle = rx_request_toggle;
+                printk(KERN_INFO PFX "Received %u bytes.\n", rx_data_size);
+                ectty_rx_data(ser->tty, rx_data, rx_data_size);
+                ser->rx_accepted_toggle = !ser->rx_accepted_toggle;
+            }
+
+            ser->control =
+                ser->tx_request_toggle |
+                ser->rx_accepted_toggle << 1 |
+                ser->tx_data_size << 8;
+            break;
+
+        case SER_REQUEST_INIT:
+            if (status & (1 << 2)) {
+                ser->control = 0x0000;
+                ser->state = SER_WAIT_FOR_INIT_RESPONSE;
+            } else {
+                ser->control = 1 << 2; // CW.2, request initialization
+            }
+            break;
+
+        case SER_WAIT_FOR_INIT_RESPONSE:
+            if (!(status & (1 << 2))) {
+                printk(KERN_INFO PFX "Init successful.\n");
+                ser->tx_accepted_toggle = 1;
+                ser->control = 0x0000;
+                ser->state = SER_READY;
+            }
+            break;
+    }
+
+    EC_WRITE_U16(pd + ser->off_ctrl, ser->control);
+    memcpy(pd + ser->off_tx, ser->tx_data, ser->tx_data_size);
+}
+
+/*****************************************************************************/
+
+void run_serial_devices(u8 *pd)
+{
+    if (ser) {
+        el6002_run(ser, pd);
+    }
+}
+
+/*****************************************************************************/
+
+int create_serial_devices(ec_master_t *master, ec_domain_t *domain)
+{
+    int i, ret;
+    ec_master_info_t master_info;
+    ec_slave_info_t slave_info;
+    
+    printk(KERN_INFO PFX "Registering serial devices...\n");
+
+    ret = ecrt_master(master, &master_info);
+    if (ret) {
+        printk(KERN_ERR PFX "Failed to obtain master information.\n");
+        return ret;
+    }
+
+    for (i = 0; i < master_info.slave_count; i++) {
+        ret = ecrt_master_get_slave(master, i, &slave_info);
+        if (ret) {
+            printk(KERN_ERR PFX "Failed to obtain master information.\n");
+            return ret;
+        }
+
+        if (slave_info.vendor_id != VendorIdBeckhoff
+                || slave_info.product_code != ProductCodeBeckhoffEL6002) {
+            continue;
+        }
+
+        printk(KERN_INFO PFX "Creating handler for serial device"
+                " at position %i\n", i);
+
+        ser = kmalloc(sizeof(*ser), GFP_KERNEL);
+        if (!ser) {
+            printk(KERN_ERR PFX "Failed to allocate serial device object.\n");
+            return -ENOMEM;
+        }
+
+        ret = el6002_init(ser, master, i, domain);
+        if (ret) {
+            printk(KERN_ERR PFX "Failed to init serial device object.\n");
+            kfree(ser);
+            ser = NULL;
+            return ret;
+        }
+
+        break; // FIXME
+    }
+
+
+    printk(KERN_INFO PFX "Finished.\n");
+    return 0;
+}
+
+/*****************************************************************************/
+
+void cleanup_serial_devices(void)
+{
+    printk(KERN_INFO PFX "Cleaning up serial devices...\n");
+
+    if (ser) {
+        el6002_clear(ser);
+        kfree(ser);
+    }
+
+    printk(KERN_INFO PFX "Finished cleaning up serial devices.\n");
+}
+
+/*****************************************************************************/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/tty/serial.h	Fri Jan 08 13:10:12 2010 +0100
@@ -0,0 +1,34 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
+ *
+ *  This file is part of the IgH EtherCAT Master.
+ *
+ *  The IgH EtherCAT Master is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License version 2, as
+ *  published by the Free Software Foundation.
+ *
+ *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
+ *  Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with the IgH EtherCAT Master; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ *  ---
+ *
+ *  The license mentioned above concerns the source code only. Using the
+ *  EtherCAT technology and brand is only permitted in compliance with the
+ *  industrial property and similar rights of Beckhoff Automation GmbH.
+ *
+ *****************************************************************************/
+
+int create_serial_devices(ec_master_t *, ec_domain_t *);
+void free_serial_devices(void);
+void run_serial_devices(u8 *);
+
+/*****************************************************************************/
--- a/examples/tty/tty.c	Fri Jan 08 11:40:27 2010 +0100
+++ b/examples/tty/tty.c	Fri Jan 08 13:10:12 2010 +0100
@@ -40,7 +40,8 @@
 #endif
 
 #include "../../include/ecrt.h" // EtherCAT realtime interface
-#include "../../include/ectty.h" // EtherCAT TTY interface
+
+#include "serial.h"
 
 /*****************************************************************************/
 
@@ -70,264 +71,11 @@
 static uint8_t *domain1_pd; // process data memory
 
 #define BusCouplerPos  0, 0
-#define SerialPos      0, 1
 
 #define Beckhoff_EK1100 0x00000002, 0x044c2c52
-#define Beckhoff_EL6002 0x00000002, 0x17723052
-
-// offsets for PDO entries
-static unsigned int off_ctrl;
-static unsigned int off_tx;
-static unsigned int off_status;
-static unsigned int off_rx;
-
-const static ec_pdo_entry_reg_t domain1_regs[] = {
-    {SerialPos,  Beckhoff_EL6002, 0x7001, 0x01, &off_ctrl},
-    {SerialPos,  Beckhoff_EL6002, 0x7000, 0x11, &off_tx},
-    {SerialPos,  Beckhoff_EL6002, 0x6001, 0x01, &off_status},
-    {SerialPos,  Beckhoff_EL6002, 0x6000, 0x11, &off_rx},
-    {}
-};
 
 static unsigned int counter = 0;
 
-typedef enum {
-    SER_REQUEST_INIT,
-    SER_WAIT_FOR_INIT_RESPONSE,
-    SER_READY
-} serial_state_t;
-
-typedef struct {
-    size_t max_tx_data_size;
-    size_t max_rx_data_size;
-
-    uint8_t *tx_data;
-    uint8_t tx_data_size;
-
-    serial_state_t state;
-
-    uint8_t tx_request_toggle;
-    uint8_t tx_accepted_toggle;
-
-    uint8_t rx_request_toggle;
-    uint8_t rx_accepted_toggle;
-
-    uint16_t control;
-} serial_device_t;
-
-static serial_device_t *ser = NULL;
-static ec_tty_t *tty = NULL;
-        
-/*****************************************************************************/
-
-/* Slave 1, "EL6002"
- * Vendor ID:       0x00000002
- * Product code:    0x17723052
- * Revision number: 0x00100000
- */
-
-ec_pdo_entry_info_t slave_1_pdo_entries[] = {
-   {0x7001, 0x01, 16}, /* Ctrl */
-   {0x7000, 0x11, 8}, /* Data Out 0 */
-   {0x7000, 0x12, 8}, /* Data Out 1 */
-   {0x7000, 0x13, 8}, /* Data Out 2 */
-   {0x7000, 0x14, 8}, /* Data Out 3 */
-   {0x7000, 0x15, 8}, /* Data Out 4 */
-   {0x7000, 0x16, 8}, /* Data Out 5 */
-   {0x7000, 0x17, 8}, /* Data Out 6 */
-   {0x7000, 0x18, 8}, /* Data Out 7 */
-   {0x7000, 0x19, 8}, /* Data Out 8 */
-   {0x7000, 0x1a, 8}, /* Data Out 9 */
-   {0x7000, 0x1b, 8}, /* Data Out 10 */
-   {0x7000, 0x1c, 8}, /* Data Out 11 */
-   {0x7000, 0x1d, 8}, /* Data Out 12 */
-   {0x7000, 0x1e, 8}, /* Data Out 13 */
-   {0x7000, 0x1f, 8}, /* Data Out 14 */
-   {0x7000, 0x20, 8}, /* Data Out 15 */
-   {0x7000, 0x21, 8}, /* Data Out 16 */
-   {0x7000, 0x22, 8}, /* Data Out 17 */
-   {0x7000, 0x23, 8}, /* Data Out 18 */
-   {0x7000, 0x24, 8}, /* Data Out 19 */
-   {0x7000, 0x25, 8}, /* Data Out 20 */
-   {0x7000, 0x26, 8}, /* Data Out 21 */
-   {0x7011, 0x01, 16}, /* Ctrl */
-   {0x7010, 0x11, 8}, /* Data Out 0 */
-   {0x7010, 0x12, 8}, /* Data Out 1 */
-   {0x7010, 0x13, 8}, /* Data Out 2 */
-   {0x7010, 0x14, 8}, /* Data Out 3 */
-   {0x7010, 0x15, 8}, /* Data Out 4 */
-   {0x7010, 0x16, 8}, /* Data Out 5 */
-   {0x7010, 0x17, 8}, /* Data Out 6 */
-   {0x7010, 0x18, 8}, /* Data Out 7 */
-   {0x7010, 0x19, 8}, /* Data Out 8 */
-   {0x7010, 0x1a, 8}, /* Data Out 9 */
-   {0x7010, 0x1b, 8}, /* Data Out 10 */
-   {0x7010, 0x1c, 8}, /* Data Out 11 */
-   {0x7010, 0x1d, 8}, /* Data Out 12 */
-   {0x7010, 0x1e, 8}, /* Data Out 13 */
-   {0x7010, 0x1f, 8}, /* Data Out 14 */
-   {0x7010, 0x20, 8}, /* Data Out 15 */
-   {0x7010, 0x21, 8}, /* Data Out 16 */
-   {0x7010, 0x22, 8}, /* Data Out 17 */
-   {0x7010, 0x23, 8}, /* Data Out 18 */
-   {0x7010, 0x24, 8}, /* Data Out 19 */
-   {0x7010, 0x25, 8}, /* Data Out 20 */
-   {0x7010, 0x26, 8}, /* Data Out 21 */
-   {0x6001, 0x01, 16}, /* Status */
-   {0x6000, 0x11, 8}, /* Data In 0 */
-   {0x6000, 0x12, 8}, /* Data In 1 */
-   {0x6000, 0x13, 8}, /* Data In 2 */
-   {0x6000, 0x14, 8}, /* Data In 3 */
-   {0x6000, 0x15, 8}, /* Data In 4 */
-   {0x6000, 0x16, 8}, /* Data In 5 */
-   {0x6000, 0x17, 8}, /* Data In 6 */
-   {0x6000, 0x18, 8}, /* Data In 7 */
-   {0x6000, 0x19, 8}, /* Data In 8 */
-   {0x6000, 0x1a, 8}, /* Data In 9 */
-   {0x6000, 0x1b, 8}, /* Data In 10 */
-   {0x6000, 0x1c, 8}, /* Data In 11 */
-   {0x6000, 0x1d, 8}, /* Data In 12 */
-   {0x6000, 0x1e, 8}, /* Data In 13 */
-   {0x6000, 0x1f, 8}, /* Data In 14 */
-   {0x6000, 0x20, 8}, /* Data In 15 */
-   {0x6000, 0x21, 8}, /* Data In 16 */
-   {0x6000, 0x22, 8}, /* Data In 17 */
-   {0x6000, 0x23, 8}, /* Data In 18 */
-   {0x6000, 0x24, 8}, /* Data In 19 */
-   {0x6000, 0x25, 8}, /* Data In 20 */
-   {0x6000, 0x26, 8}, /* Data In 21 */
-   {0x6011, 0x01, 16}, /* Status */
-   {0x6010, 0x11, 8}, /* Data In 0 */
-   {0x6010, 0x12, 8}, /* Data In 1 */
-   {0x6010, 0x13, 8}, /* Data In 2 */
-   {0x6010, 0x14, 8}, /* Data In 3 */
-   {0x6010, 0x15, 8}, /* Data In 4 */
-   {0x6010, 0x16, 8}, /* Data In 5 */
-   {0x6010, 0x17, 8}, /* Data In 6 */
-   {0x6010, 0x18, 8}, /* Data In 7 */
-   {0x6010, 0x19, 8}, /* Data In 8 */
-   {0x6010, 0x1a, 8}, /* Data In 9 */
-   {0x6010, 0x1b, 8}, /* Data In 10 */
-   {0x6010, 0x1c, 8}, /* Data In 11 */
-   {0x6010, 0x1d, 8}, /* Data In 12 */
-   {0x6010, 0x1e, 8}, /* Data In 13 */
-   {0x6010, 0x1f, 8}, /* Data In 14 */
-   {0x6010, 0x20, 8}, /* Data In 15 */
-   {0x6010, 0x21, 8}, /* Data In 16 */
-   {0x6010, 0x22, 8}, /* Data In 17 */
-   {0x6010, 0x23, 8}, /* Data In 18 */
-   {0x6010, 0x24, 8}, /* Data In 19 */
-   {0x6010, 0x25, 8}, /* Data In 20 */
-   {0x6010, 0x26, 8}, /* Data In 21 */
-};
-
-ec_pdo_info_t slave_1_pdos[] = {
-   {0x1604, 23, slave_1_pdo_entries + 0}, /* COM RxPDO-Map Outputs Ch.1 */
-   {0x1605, 23, slave_1_pdo_entries + 23}, /* COM RxPDO-Map Outputs Ch.2 */
-   {0x1a04, 23, slave_1_pdo_entries + 46}, /* COM TxPDO-Map Inputs Ch.1 */
-   {0x1a05, 23, slave_1_pdo_entries + 69}, /* COM TxPDO-Map Inputs Ch.2 */
-};
-
-ec_sync_info_t slave_1_syncs[] = {
-   {0, EC_DIR_OUTPUT, 0, NULL, EC_WD_DISABLE},
-   {1, EC_DIR_INPUT, 0, NULL, EC_WD_DISABLE},
-   {2, EC_DIR_OUTPUT, 2, slave_1_pdos + 0, EC_WD_DISABLE},
-   {3, EC_DIR_INPUT, 2, slave_1_pdos + 2, EC_WD_DISABLE},
-   {0xff}
-};
-
-/****************************************************************************/
-
-int serial_init(serial_device_t *ser, size_t max_tx, size_t max_rx)
-{
-    ser->max_tx_data_size = max_tx;
-    ser->max_rx_data_size = max_rx;
-    ser->tx_data = NULL;
-    ser->tx_data_size = 0;
-    ser->state = SER_REQUEST_INIT;
-    ser->tx_request_toggle = 0;
-    ser->rx_accepted_toggle = 0;
-    ser->control = 0x0000;
-
-    if (max_tx > 0) {
-        ser->tx_data = kmalloc(max_tx, GFP_KERNEL);
-        if (ser->tx_data == NULL) {
-            return -ENOMEM;
-        }
-    }
-
-    return 0;
-}
-
-/****************************************************************************/
-
-void serial_clear(serial_device_t *ser)
-{
-    if (ser->tx_data) {
-        kfree(ser->tx_data);
-    }
-}
-
-/****************************************************************************/
-
-void serial_run(serial_device_t *ser, uint16_t status, uint8_t *rx_data)
-{
-    uint8_t tx_accepted_toggle, rx_request_toggle;
-
-    switch (ser->state) {
-        case SER_READY:
-
-            /* Send data */
-            
-            tx_accepted_toggle = status & 0x0001;
-            if (tx_accepted_toggle != ser->tx_accepted_toggle) { // ready
-                ser->tx_data_size =
-                    ectty_tx_data(tty, ser->tx_data, ser->max_tx_data_size);
-                if (ser->tx_data_size) {
-                    printk(KERN_INFO PFX "Sending %u bytes.\n", ser->tx_data_size);
-                    ser->tx_request_toggle = !ser->tx_request_toggle;
-                    ser->tx_accepted_toggle = tx_accepted_toggle;
-                }
-            }
-
-            /* Receive data */
-
-            rx_request_toggle = status & 0x0002;
-            if (rx_request_toggle != ser->rx_request_toggle) {
-                uint8_t rx_data_size = status >> 8;
-                ser->rx_request_toggle = rx_request_toggle;
-                printk(KERN_INFO PFX "Received %u bytes.\n", rx_data_size);
-                ectty_rx_data(tty, rx_data, rx_data_size);
-                ser->rx_accepted_toggle = !ser->rx_accepted_toggle;
-            }
-
-            ser->control =
-                ser->tx_request_toggle |
-                ser->rx_accepted_toggle << 1 |
-                ser->tx_data_size << 8;
-            break;
-
-        case SER_REQUEST_INIT:
-            if (status & (1 << 2)) {
-                ser->control = 0x0000;
-                ser->state = SER_WAIT_FOR_INIT_RESPONSE;
-            } else {
-                ser->control = 1 << 2; // CW.2, request initialization
-            }
-            break;
-
-        case SER_WAIT_FOR_INIT_RESPONSE:
-            if (!(status & (1 << 2))) {
-                printk(KERN_INFO PFX "Init successful.\n");
-                ser->tx_accepted_toggle = 1;
-                ser->control = 0x0000;
-                ser->state = SER_READY;
-            }
-            break;
-    }
-
-}
-
 /*****************************************************************************/
 
 void check_domain1_state(void)
@@ -388,9 +136,7 @@
         check_master_state();
     }
 
-    serial_run(ser, EC_READ_U16(domain1_pd + off_status), domain1_pd + off_rx);
-    EC_WRITE_U16(domain1_pd + off_ctrl, ser->control);
-    memcpy(domain1_pd + off_tx, ser->tx_data, 22);
+    run_serial_devices(domain1_pd);
 
     // send process data
     down(&master_sem);
@@ -432,31 +178,11 @@
     
     printk(KERN_INFO PFX "Starting...\n");
 
-    ser = kmalloc(sizeof(*ser), GFP_KERNEL);
-    if (!ser) {
-        printk(KERN_ERR PFX "Failed to allocate serial device object.\n");
-        ret = -ENOMEM;
-        goto out_return;
-    }
-
-    ret = serial_init(ser, 22, 22);
-    if (ret) {
-        printk(KERN_ERR PFX "Failed to init serial device object.\n");
-        goto out_free_serial;
-    }
-
-    tty = ectty_create();
-    if (IS_ERR(tty)) {
-        printk(KERN_ERR PFX "Failed to create tty.\n");
-        ret = PTR_ERR(tty);
-        goto out_serial;
-    }
-    
     master = ecrt_request_master(0);
     if (!master) {
+        printk(KERN_ERR PFX "Requesting master 0 failed.\n");
         ret = -EBUSY; 
-        printk(KERN_ERR PFX "Requesting master 0 failed.\n");
-        goto out_tty;
+        goto out_return;
     }
 
     sema_init(&master_sem, 1);
@@ -470,30 +196,18 @@
 
     // Create configuration for bus coupler
     sc = ecrt_master_slave_config(master, BusCouplerPos, Beckhoff_EK1100);
-    if (!sc)
-        return -1;
-
-    if (!(sc = ecrt_master_slave_config(
-                    master, SerialPos, Beckhoff_EL6002))) {
-        printk(KERN_ERR PFX "Failed to get slave configuration.\n");
-        return -1;
-    }
-
-    printk("Configuring PDOs...\n");
-    if (ecrt_slave_config_pdos(sc, EC_END, slave_1_syncs)) {
-        printk(KERN_ERR PFX "Failed to configure PDOs.\n");
-        return -1;
-    }
-
-    if (ecrt_domain_reg_pdo_entry_list(domain1, domain1_regs)) {
-        printk(KERN_ERR PFX "PDO entry registration failed!\n");
-        return -1;
-    }
+    if (!sc) {
+        printk(KERN_ERR PFX "Failed to create slave config.\n");
+        ret = -ENOMEM;
+        goto out_release_master;
+    }
+
+    create_serial_devices(master, domain1);
 
     printk(KERN_INFO PFX "Activating master...\n");
     if (ecrt_master_activate(master)) {
         printk(KERN_ERR PFX "Failed to activate master!\n");
-        goto out_release_master;
+        goto out_free_serial;
     }
 
     // Get internal process data for domain
@@ -508,15 +222,11 @@
     printk(KERN_INFO PFX "Started.\n");
     return 0;
 
+out_free_serial:
+    free_serial_devices();
 out_release_master:
     printk(KERN_ERR PFX "Releasing master...\n");
     ecrt_release_master(master);
-out_tty:
-    ectty_free(tty);
-out_serial:
-    serial_clear(ser);
-out_free_serial:
-    kfree(ser);
 out_return:
     printk(KERN_ERR PFX "Failed to load. Aborting.\n");
     return ret;
@@ -530,13 +240,11 @@
 
     del_timer_sync(&timer);
 
+    free_serial_devices();
+
     printk(KERN_INFO PFX "Releasing master...\n");
     ecrt_release_master(master);
 
-    ectty_free(tty);
-    serial_clear(ser);
-    kfree(ser);
-
     printk(KERN_INFO PFX "Unloading.\n");
 }