Implemented fetching of SDO dictionary.
authorFlorian Pose <fp@igh-essen.com>
Tue, 17 Oct 2006 14:50:04 +0000
changeset 409 bc4c852e1b93
parent 408 0eda82d1a8c1
child 410 3b8e94d623ab
Implemented fetching of SDO dictionary.
master/Kbuild
master/Makefile.am
master/canopen.c
master/canopen.h
master/fsm.c
master/fsm.h
master/slave.c
master/slave.h
--- a/master/Kbuild	Tue Oct 17 14:37:46 2006 +0000
+++ b/master/Kbuild	Tue Oct 17 14:50:04 2006 +0000
@@ -38,7 +38,7 @@
 obj-m := ec_master.o
 
 ec_master-objs := module.o master.o device.o slave.o datagram.o \
-		domain.o mailbox.o ethernet.o fsm.o xmldev.o
+		domain.o mailbox.o canopen.o ethernet.o fsm.o xmldev.o
 
 ifeq ($(EC_DBG_IF),1)
 	ec_master-objs += debug.o
--- a/master/Makefile.am	Tue Oct 17 14:37:46 2006 +0000
+++ b/master/Makefile.am	Tue Oct 17 14:50:04 2006 +0000
@@ -37,6 +37,7 @@
 
 EXTRA_DIST = \
 	Kbuild \
+	canopen.c canopen.h \
 	datagram.c datagram.h \
 	debug.c	debug.h \
 	device.c device.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master/canopen.c	Tue Oct 17 14:50:04 2006 +0000
@@ -0,0 +1,330 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006  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
+ *  as published by the Free Software Foundation; either version 2 of the
+ *  License, or (at your option) any later version.
+ *
+ *  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 right to use EtherCAT Technology is granted and comes free of
+ *  charge under condition of compatibility of product made by
+ *  Licensee. People intending to distribute/sell products based on the
+ *  code, have to sign an agreement to guarantee that products using
+ *  software based on IgH EtherCAT master stay compatible with the actual
+ *  EtherCAT specification (which are released themselves as an open
+ *  standard) as the (only) precondition to have the right to use EtherCAT
+ *  Technology, IP and trade marks.
+ *
+ *****************************************************************************/
+
+/**
+   \file
+   Canopen-over-EtherCAT functions.
+*/
+
+/*****************************************************************************/
+
+#include <linux/module.h>
+
+#include "canopen.h"
+
+/*****************************************************************************/
+
+ssize_t ec_show_sdo_attribute(struct kobject *, struct attribute *, char *);
+ssize_t ec_show_sdo_entry_attribute(struct kobject *, struct attribute *,
+                                    char *);
+void ec_sdo_clear(struct kobject *);
+void ec_sdo_entry_clear(struct kobject *);
+
+/*****************************************************************************/
+
+/** \cond */
+
+EC_SYSFS_READ_ATTR(info);
+
+static struct attribute *sdo_def_attrs[] = {
+    &attr_info,
+    NULL,
+};
+
+static struct sysfs_ops sdo_sysfs_ops = {
+    .show = &ec_show_sdo_attribute,
+    .store = NULL
+};
+
+static struct kobj_type ktype_ec_sdo = {
+    .release = ec_sdo_clear,
+    .sysfs_ops = &sdo_sysfs_ops,
+    .default_attrs = sdo_def_attrs
+};
+
+static struct attribute *sdo_entry_def_attrs[] = {
+    &attr_info,
+    NULL,
+};
+
+static struct sysfs_ops sdo_entry_sysfs_ops = {
+    .show = &ec_show_sdo_entry_attribute,
+    .store = NULL
+};
+
+static struct kobj_type ktype_ec_sdo_entry = {
+    .release = ec_sdo_entry_clear,
+    .sysfs_ops = &sdo_entry_sysfs_ops,
+    .default_attrs = sdo_entry_def_attrs
+};
+
+/** \endcond */
+
+/*****************************************************************************/
+
+/**
+   SDO constructor.
+*/
+
+int ec_sdo_init(ec_sdo_t *sdo, /**< SDO */
+                uint16_t index, /**< SDO index */
+                ec_slave_t *slave /**< parent slave */
+                )
+{
+    sdo->index = index;
+    sdo->object_code = 0x00;
+    sdo->name = NULL;
+    sdo->subindices = 0;
+    INIT_LIST_HEAD(&sdo->entries);
+
+    // init kobject and add it to the hierarchy
+    memset(&sdo->kobj, 0x00, sizeof(struct kobject));
+    kobject_init(&sdo->kobj);
+    sdo->kobj.ktype = &ktype_ec_sdo;
+    sdo->kobj.parent = &slave->kobj;
+    if (kobject_set_name(&sdo->kobj, "sdo%4X", sdo->index)) {
+        EC_ERR("Failed to set kobj name.\n");
+        return -1;
+    }
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+/**
+   SDO destructor.
+*/
+
+void ec_sdo_clear(struct kobject *kobj /**< SDO's kobject */)
+{
+    ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj);
+    ec_sdo_entry_t *entry, *next;
+
+    // free all entries
+    list_for_each_entry_safe(entry, next, &sdo->entries, list) {
+        list_del(&entry->list);
+        kobject_del(&entry->kobj);
+        kobject_put(&entry->kobj);
+    }
+
+    if (sdo->name) kfree(sdo->name);
+
+    kfree(sdo);
+}
+
+/*****************************************************************************/
+
+ssize_t ec_sdo_info(ec_sdo_t *sdo, /**< SDO */
+                    char *buffer /**< target buffer */
+                    )
+{
+    off_t off = 0;
+
+    off += sprintf(buffer + off, "Index: 0x%04X\n", sdo->index);
+    off += sprintf(buffer + off, "Name: %s\n", sdo->name ? sdo->name : "");
+    off += sprintf(buffer + off, "Subindices: %i\n", sdo->subindices);
+
+    return off;
+}
+
+/*****************************************************************************/
+
+ssize_t ec_show_sdo_attribute(struct kobject *kobj, /**< kobject */
+                              struct attribute *attr,
+                              char *buffer
+                              )
+{
+    ec_sdo_t *sdo = container_of(kobj, ec_sdo_t, kobj);
+
+    if (attr == &attr_info) {
+        return ec_sdo_info(sdo, buffer);
+    }
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+/**
+   SDO entry constructor.
+*/
+
+int ec_sdo_entry_init(ec_sdo_entry_t *entry, /**< SDO entry */
+                      uint8_t subindex, /**< SDO entry subindex */
+                      ec_sdo_t *sdo /**< parent SDO */
+                      )
+{
+    entry->subindex = subindex;
+    entry->data_type = 0x0000;
+    entry->bit_length = 0;
+    entry->description = NULL;
+
+    // init kobject and add it to the hierarchy
+    memset(&entry->kobj, 0x00, sizeof(struct kobject));
+    kobject_init(&entry->kobj);
+    entry->kobj.ktype = &ktype_ec_sdo_entry;
+    entry->kobj.parent = &sdo->kobj;
+    if (kobject_set_name(&entry->kobj, "entry%02X", entry->subindex)) {
+        EC_ERR("Failed to set kobj name.\n");
+        return -1;
+    }
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+/**
+   SDO destructor.
+*/
+
+void ec_sdo_entry_clear(struct kobject *kobj /**< SDO entry's kobject */)
+{
+    ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj);
+
+    if (entry->description) kfree(entry->description);
+
+    kfree(entry);
+}
+
+/*****************************************************************************/
+
+ssize_t ec_sdo_entry_info(ec_sdo_entry_t *entry, /**< SDO entry */
+                          char *buffer /**< target buffer */
+                          )
+{
+    off_t off = 0;
+
+    off += sprintf(buffer + off, "Subindex: 0x%02X\n", entry->subindex);
+    off += sprintf(buffer + off, "Description: %s\n",
+                   entry->description ? entry->description : "");
+    off += sprintf(buffer + off, "Data type: 0x%04X\n", entry->data_type);
+    off += sprintf(buffer + off, "Bit length: %i\n", entry->bit_length);
+
+    return off;
+}
+
+/*****************************************************************************/
+
+ssize_t ec_show_sdo_entry_attribute(struct kobject *kobj, /**< kobject */
+                                    struct attribute *attr,
+                                    char *buffer
+                                    )
+{
+    ec_sdo_entry_t *entry = container_of(kobj, ec_sdo_entry_t, kobj);
+
+    if (attr == &attr_info) {
+        return ec_sdo_entry_info(entry, buffer);
+    }
+
+    return 0;
+}
+
+/*****************************************************************************/
+
+#if 0
+int ecrt_slave_sdo_read(ec_slave_t *slave, /**< EtherCAT slave */
+                        uint16_t sdo_index, /**< SDO index */
+                        uint8_t sdo_subindex, /**< SDO subindex */
+                        uint8_t *target, /**< memory for value */
+                        size_t *size /**< target memory size */
+                        )
+{
+    uint8_t *data;
+    size_t rec_size, data_size;
+    uint32_t complete_size;
+    ec_datagram_t datagram;
+
+    ec_datagram_init(&datagram);
+
+    if (!(data = ec_slave_mbox_prepare_send(slave, &datagram, 0x03, 6)))
+        goto err;
+
+    EC_WRITE_U16(data, 0x2 << 12); // SDO request
+    EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request
+    EC_WRITE_U16(data + 3, sdo_index);
+    EC_WRITE_U8 (data + 5, sdo_subindex);
+
+    if (!(data = ec_slave_mbox_simple_io(slave, &datagram, &rec_size)))
+        goto err;
+
+    if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
+        EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
+        EC_ERR("SDO upload 0x%04X:%X aborted on slave %i.\n",
+               sdo_index, sdo_subindex, slave->ring_position);
+        ec_canopen_abort_msg(EC_READ_U32(data + 6));
+        goto err;
+    }
+
+    if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
+        EC_READ_U8 (data + 2) >> 5 != 0x2 || // initiate upload response
+        EC_READ_U16(data + 3) != sdo_index || // index
+        EC_READ_U8 (data + 5) != sdo_subindex) { // subindex
+        EC_ERR("SDO upload 0x%04X:%X failed:\n", sdo_index, sdo_subindex);
+        EC_ERR("Invalid SDO upload response at slave %i!\n",
+               slave->ring_position);
+        ec_print_data(data, rec_size);
+        goto err;
+    }
+
+    if (rec_size < 10) {
+        EC_ERR("Received currupted SDO upload response!\n");
+        ec_print_data(data, rec_size);
+        goto err;
+    }
+
+    if ((complete_size = EC_READ_U32(data + 6)) > *size) {
+        EC_ERR("SDO data does not fit into buffer (%i / %i)!\n",
+               complete_size, *size);
+        goto err;
+    }
+
+    data_size = rec_size - 10;
+
+    if (data_size != complete_size) {
+        EC_ERR("SDO data incomplete - Fragmenting not implemented.\n");
+        goto err;
+    }
+
+    memcpy(target, data + 10, data_size);
+
+    ec_datagram_clear(&datagram);
+    return 0;
+ err:
+    ec_datagram_clear(&datagram);
+    return -1;
+}
+#endif
+
+/*****************************************************************************/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/master/canopen.h	Tue Oct 17 14:50:04 2006 +0000
@@ -0,0 +1,108 @@
+/******************************************************************************
+ *
+ *  $Id$
+ *
+ *  Copyright (C) 2006  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
+ *  as published by the Free Software Foundation; either version 2 of the
+ *  License, or (at your option) any later version.
+ *
+ *  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 right to use EtherCAT Technology is granted and comes free of
+ *  charge under condition of compatibility of product made by
+ *  Licensee. People intending to distribute/sell products based on the
+ *  code, have to sign an agreement to guarantee that products using
+ *  software based on IgH EtherCAT master stay compatible with the actual
+ *  EtherCAT specification (which are released themselves as an open
+ *  standard) as the (only) precondition to have the right to use EtherCAT
+ *  Technology, IP and trade marks.
+ *
+ *****************************************************************************/
+
+/**
+   \file
+   EtherCAT CANopen structures.
+*/
+
+/*****************************************************************************/
+
+#ifndef _EC_CANOPEN_H_
+#define _EC_CANOPEN_H_
+
+#include <linux/list.h>
+#include <linux/kobject.h>
+
+#include "globals.h"
+#include "slave.h"
+
+/*****************************************************************************/
+
+/**
+   CANopen SDO.
+*/
+
+typedef struct
+{
+    struct kobject kobj; /**< kobject */
+    struct list_head list; /**< list item */
+    uint16_t index; /**< SDO index */
+    uint8_t object_code; /**< object code */
+    char *name; /**< SDO name */
+    uint8_t subindices; /**< subindices */
+    struct list_head entries; /**< entry list */
+}
+ec_sdo_t;
+
+/*****************************************************************************/
+
+/**
+   CANopen SDO entry.
+*/
+
+typedef struct
+{
+    struct kobject kobj; /**< kobject */
+    struct list_head list; /**< list item */
+    uint8_t subindex; /**< entry subindex */
+    uint16_t data_type; /**< entry data type */
+    uint16_t bit_length; /**< entry length in bit */
+    char *description; /**< entry description */
+}
+ec_sdo_entry_t;
+
+/*****************************************************************************/
+
+/**
+   CANopen SDO configuration data.
+*/
+
+typedef struct
+{
+    struct list_head list; /**< list item */
+    uint16_t index; /**< SDO index */
+    uint8_t subindex; /**< SDO subindex */
+    uint8_t *data; /**< pointer to SDO data */
+    size_t size; /**< size of SDO data */
+}
+ec_sdo_data_t;
+
+/*****************************************************************************/
+
+int ec_sdo_init(ec_sdo_t *, uint16_t, ec_slave_t *);
+int ec_sdo_entry_init(ec_sdo_entry_t *, uint8_t, ec_sdo_t *);
+
+/*****************************************************************************/
+
+#endif
--- a/master/fsm.c	Tue Oct 17 14:37:46 2006 +0000
+++ b/master/fsm.c	Tue Oct 17 14:50:04 2006 +0000
@@ -73,8 +73,9 @@
 void ec_fsm_slaveconf_init(ec_fsm_t *);
 void ec_fsm_slaveconf_sync(ec_fsm_t *);
 void ec_fsm_slaveconf_preop(ec_fsm_t *);
+void ec_fsm_slaveconf_sdodict(ec_fsm_t *);
+void ec_fsm_slaveconf_sdoconf(ec_fsm_t *);
 void ec_fsm_slaveconf_fmmu(ec_fsm_t *);
-void ec_fsm_slaveconf_sdoconf(ec_fsm_t *);
 void ec_fsm_slaveconf_saveop(ec_fsm_t *);
 void ec_fsm_slaveconf_op(ec_fsm_t *);
 
@@ -92,6 +93,17 @@
 void ec_fsm_change_ack(ec_fsm_t *);
 void ec_fsm_change_check_ack(ec_fsm_t *);
 
+void ec_fsm_coe_dict_start(ec_fsm_t *);
+void ec_fsm_coe_dict_request(ec_fsm_t *);
+void ec_fsm_coe_dict_check(ec_fsm_t *);
+void ec_fsm_coe_dict_response(ec_fsm_t *);
+void ec_fsm_coe_dict_desc_request(ec_fsm_t *);
+void ec_fsm_coe_dict_desc_check(ec_fsm_t *);
+void ec_fsm_coe_dict_desc_response(ec_fsm_t *);
+void ec_fsm_coe_dict_entry_request(ec_fsm_t *);
+void ec_fsm_coe_dict_entry_check(ec_fsm_t *);
+void ec_fsm_coe_dict_entry_response(ec_fsm_t *);
+
 void ec_fsm_coe_down_start(ec_fsm_t *);
 void ec_fsm_coe_down_request(ec_fsm_t *);
 void ec_fsm_coe_down_check(ec_fsm_t *);
@@ -661,7 +673,7 @@
 /*****************************************************************************/
 
 /**
-   Master state: STATES.
+   Master state: READ STATES.
    Fetches the AL- and online state of a slave.
 */
 
@@ -832,7 +844,7 @@
 /*****************************************************************************/
 
 /**
-   Master state: ADDRESS.
+   Master state: REWRITE ADDRESS.
    Checks, if the new station address has been written to the slave.
 */
 
@@ -864,7 +876,7 @@
 /*****************************************************************************/
 
 /**
-   Master state: SCAN.
+   Master state: SCAN SLAVES.
    Executes the sub-statemachine for the scanning of a slave.
 */
 
@@ -911,7 +923,7 @@
 /*****************************************************************************/
 
 /**
-   Master state: CONF.
+   Master state: CONFIGURE SLAVES.
    Starts configuring a slave.
 */
 
@@ -930,7 +942,7 @@
 /*****************************************************************************/
 
 /**
-   Master state: EEPROM.
+   Master state: WRITE EEPROM.
 */
 
 void ec_fsm_master_write_eeprom(ec_fsm_t *fsm /**< finite state machine */)
@@ -978,7 +990,7 @@
  *****************************************************************************/
 
 /**
-   Slave state: START_READING.
+   Slave scan state: START.
    First state of the slave state machine. Writes the station address to the
    slave, according to its ring position.
 */
@@ -997,7 +1009,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: ADDRESS.
+   Slave scan state: ADDRESS.
 */
 
 void ec_fsm_slavescan_address(ec_fsm_t *fsm /**< finite state machine */)
@@ -1022,7 +1034,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: STATE.
+   Slave scan state: STATE.
 */
 
 void ec_fsm_slavescan_state(ec_fsm_t *fsm /**< finite state machine */)
@@ -1054,7 +1066,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: BASE.
+   Slave scan state: BASE.
 */
 
 void ec_fsm_slavescan_base(ec_fsm_t *fsm /**< finite state machine */)
@@ -1089,7 +1101,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: DATALINK.
+   Slave scan state: DATALINK.
 */
 
 void ec_fsm_slavescan_datalink(ec_fsm_t *fsm /**< finite state machine */)
@@ -1127,7 +1139,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: EEPROM_SIZE.
+   Slave scan state: EEPROM SIZE.
 */
 
 void ec_fsm_slavescan_eeprom_size(ec_fsm_t *fsm /**< finite state machine */)
@@ -1187,7 +1199,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: EEPROM_DATA.
+   Slave scan state: EEPROM DATA.
 */
 
 void ec_fsm_slavescan_eeprom_data(ec_fsm_t *fsm /**< finite state machine */)
@@ -1301,11 +1313,12 @@
  *****************************************************************************/
 
 /**
-   Slave state: INIT.
+   Slave configuration state: INIT.
 */
 
 void ec_fsm_slaveconf_init(ec_fsm_t *fsm /**< finite state machine */)
 {
+    ec_master_t *master = fsm->master;
     ec_slave_t *slave = fsm->slave;
     ec_datagram_t *datagram = &fsm->datagram;
     const ec_sii_sync_t *sync;
@@ -1320,9 +1333,17 @@
 
     if (fsm->change_state != ec_fsm_end) return;
 
+    if (master->debug_level) {
+        EC_DBG("Slave %i is now in INIT.\n", slave->ring_position);
+    }
+
     // slave is now in INIT
     if (slave->current_state == slave->requested_state) {
         fsm->slave_state = ec_fsm_end; // successful
+        if (master->debug_level) {
+            EC_DBG("Finished configuration of slave %i.\n",
+                   slave->ring_position);
+        }
         return;
     }
 
@@ -1338,6 +1359,11 @@
         return;
     }
 
+    if (master->debug_level) {
+        EC_DBG("Configuring sync managers of slave %i.\n",
+               slave->ring_position);
+    }
+
     // configure sync managers
     ec_datagram_npwr(datagram, slave->station_address, 0x0800,
                      EC_SYNC_SIZE * slave->base_sync_count);
@@ -1361,7 +1387,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: SYNC.
+   Slave configuration state: SYNC.
 */
 
 void ec_fsm_slaveconf_sync(ec_fsm_t *fsm /**< finite state machine */)
@@ -1387,7 +1413,7 @@
 /*****************************************************************************/
 
 /**
-   Slave state: PREOP.
+   Slave configuration state: PREOP.
 */
 
 void ec_fsm_slaveconf_preop(ec_fsm_t *fsm /**< finite state machine */)
@@ -1408,19 +1434,91 @@
     if (fsm->change_state != ec_fsm_end) return;
 
     // slave is now in PREOP
-    if (slave->current_state == slave->requested_state) {
-        fsm->slave_state = ec_fsm_end; // successful
-        return;
-    }
-
-    if (!slave->base_fmmu_count) { // skip FMMU configuration
-        if (list_empty(&slave->sdo_confs)) { // skip SDO configuration
+
+    if (master->debug_level) {
+        EC_DBG("Slave %i is now in PREOP.\n", slave->ring_position);
+    }
+
+    if (!(slave->sii_mailbox_protocols & EC_MBOX_COE)) {
+        // slave does not support CoE. skip dictionary fetching
+        // and SDO configuration
+
+        if (slave->current_state == slave->requested_state) {
+            fsm->slave_state = ec_fsm_end; // successful
+            if (master->debug_level) {
+                EC_DBG("Finished configuration of slave %i.\n",
+                       slave->ring_position);
+            }
+            return;
+        }
+
+        if (!slave->base_fmmu_count) {
+            // slave has no FMMUs. skip configuration and go to SAVEOP
             fsm->slave_state = ec_fsm_slaveconf_saveop;
             fsm->change_new = EC_SLAVE_STATE_SAVEOP;
             fsm->change_state = ec_fsm_change_start;
             fsm->change_state(fsm); // execute immediately
             return;
         }
+
+        // configure FMMUs
+        ec_datagram_npwr(datagram, slave->station_address,
+                         0x0600, EC_FMMU_SIZE * slave->base_fmmu_count);
+        memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count);
+        for (j = 0; j < slave->fmmu_count; j++) {
+            ec_fmmu_config(&slave->fmmus[j], slave,
+                           datagram->data + EC_FMMU_SIZE * j);
+        }
+
+        ec_master_queue_datagram(master, datagram);
+        fsm->slave_state = ec_fsm_slaveconf_fmmu;
+        return;
+    }
+
+    if (!list_empty(&slave->sdo_dictionary)) {
+        // SDO dictionary already fetched
+
+        if (master->debug_level) {
+            EC_DBG("SDO dictionary already present for slave %i.\n",
+                   slave->ring_position);
+        }
+
+        if (slave->current_state == slave->requested_state) {
+            fsm->slave_state = ec_fsm_end; // successful
+            if (master->debug_level) {
+                EC_DBG("Finished configuration of slave %i.\n",
+                       slave->ring_position);
+            }
+            return;
+        }
+
+        if (list_empty(&slave->sdo_confs)) {
+            // skip SDO configuration
+
+            if (!slave->base_fmmu_count) {
+                // slave has no FMMUs. skip configuration and go to SAVEOP
+                fsm->slave_state = ec_fsm_slaveconf_saveop;
+                fsm->change_new = EC_SLAVE_STATE_SAVEOP;
+                fsm->change_state = ec_fsm_change_start;
+                fsm->change_state(fsm); // execute immediately
+                return;
+            }
+
+            // configure FMMUs
+            ec_datagram_npwr(datagram, slave->station_address,
+                             0x0600, EC_FMMU_SIZE * slave->base_fmmu_count);
+            memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count);
+            for (j = 0; j < slave->fmmu_count; j++) {
+                ec_fmmu_config(&slave->fmmus[j], slave,
+                               datagram->data + EC_FMMU_SIZE * j);
+            }
+
+            ec_master_queue_datagram(master, datagram);
+            fsm->slave_state = ec_fsm_slaveconf_fmmu;
+            return;
+        }
+
+        // start SDO configuration
         fsm->slave_state = ec_fsm_slaveconf_sdoconf;
         fsm->coe_sdodata = list_entry(slave->sdo_confs.next, ec_sdo_data_t, list);
         fsm->coe_state = ec_fsm_coe_down_start;
@@ -1428,6 +1526,136 @@
         return;
     }
 
+    if (master->debug_level) {
+        EC_DBG("Fetching SDO dictionary of slave %i.\n",
+               slave->ring_position);
+    }
+
+    // start fetching SDO dictionary
+    fsm->slave_state = ec_fsm_slaveconf_sdodict;
+    fsm->coe_state = ec_fsm_coe_dict_start;
+    fsm->coe_state(fsm); // execute immediately
+}
+
+/*****************************************************************************/
+
+/**
+   Slave configuration state: SDODICT.
+*/
+
+void ec_fsm_slaveconf_sdodict(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_slave_t *slave = fsm->slave;
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_master_t *master = fsm->master;
+    unsigned int j;
+
+    fsm->coe_state(fsm); // execute CoE state machine
+
+    if (fsm->coe_state == ec_fsm_error) {
+        fsm->slave->error_flag = 1;
+        fsm->slave_state = ec_fsm_error;
+        return;
+    }
+
+    if (fsm->coe_state != ec_fsm_end) return;
+
+    // SDO dictionary fetching finished
+
+    if (master->debug_level) {
+        EC_DBG("Finished fetching SDO dictionary of slave %i.\n",
+               slave->ring_position);
+    }
+
+    if (slave->current_state == slave->requested_state) {
+        fsm->slave_state = ec_fsm_end; // successful
+        if (master->debug_level) {
+            EC_DBG("Finished configuration of slave %i.\n",
+                   slave->ring_position);
+        }
+        return;
+    }
+
+    if (list_empty(&slave->sdo_confs)) {
+        // skip SDO configuration
+
+        if (!slave->base_fmmu_count) {
+            // slave has no FMMUs. skip configuration.
+
+            // set state to SAVEOP
+            fsm->slave_state = ec_fsm_slaveconf_saveop;
+            fsm->change_new = EC_SLAVE_STATE_SAVEOP;
+            fsm->change_state = ec_fsm_change_start;
+            fsm->change_state(fsm); // execute immediately
+            return;
+        }
+
+        // configure FMMUs
+        ec_datagram_npwr(datagram, slave->station_address,
+                         0x0600, EC_FMMU_SIZE * slave->base_fmmu_count);
+        memset(datagram->data, 0x00, EC_FMMU_SIZE * slave->base_fmmu_count);
+        for (j = 0; j < slave->fmmu_count; j++) {
+            ec_fmmu_config(&slave->fmmus[j], slave,
+                           datagram->data + EC_FMMU_SIZE * j);
+        }
+
+        ec_master_queue_datagram(master, datagram);
+        fsm->slave_state = ec_fsm_slaveconf_fmmu;
+        return;
+    }
+
+    // start SDO configuration
+    fsm->slave_state = ec_fsm_slaveconf_sdoconf;
+    fsm->coe_sdodata = list_entry(slave->sdo_confs.next, ec_sdo_data_t, list);
+    fsm->coe_state = ec_fsm_coe_down_start;
+    fsm->coe_state(fsm); // execute immediately
+}
+
+/*****************************************************************************/
+
+/**
+   Slave configuration state: SDOCONF.
+*/
+
+void ec_fsm_slaveconf_sdoconf(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_slave_t *slave = fsm->slave;
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_master_t *master = fsm->master;
+    unsigned int j;
+
+    fsm->coe_state(fsm); // execute CoE state machine
+
+    if (fsm->coe_state == ec_fsm_error) {
+        fsm->slave->error_flag = 1;
+        fsm->slave_state = ec_fsm_error;
+        return;
+    }
+
+    if (fsm->coe_state != ec_fsm_end) return;
+
+    // Another SDO to configure?
+    if (fsm->coe_sdodata->list.next != &fsm->slave->sdo_confs) {
+        fsm->coe_sdodata = list_entry(fsm->coe_sdodata->list.next,
+                                      ec_sdo_data_t, list);
+        fsm->coe_state = ec_fsm_coe_down_start;
+        fsm->coe_state(fsm); // execute immediately
+        return;
+    }
+
+    // All SDOs are now configured.
+
+    if (!slave->base_fmmu_count) {
+        // slave has no FMMUs. skip configuration.
+
+        // set state to SAVEOP
+        fsm->slave_state = ec_fsm_slaveconf_saveop;
+        fsm->change_new = EC_SLAVE_STATE_SAVEOP;
+        fsm->change_state = ec_fsm_change_start;
+        fsm->change_state(fsm); // execute immediately
+        return;
+    }
+
     // configure FMMUs
     ec_datagram_npwr(datagram, slave->station_address,
                      0x0600, EC_FMMU_SIZE * slave->base_fmmu_count);
@@ -1444,13 +1672,12 @@
 /*****************************************************************************/
 
 /**
-   Slave state: FMMU.
+   Slave configuration state: FMMU.
 */
 
 void ec_fsm_slaveconf_fmmu(ec_fsm_t *fsm /**< finite state machine */)
 {
     ec_datagram_t *datagram = &fsm->datagram;
-    ec_slave_t *slave = fsm->slave;
 
     if (datagram->state != EC_DATAGRAM_RECEIVED
         || datagram->working_counter != 1) {
@@ -1461,51 +1688,6 @@
         return;
     }
 
-    // No CoE configuration to be applied? Jump to SAVEOP state.
-    if (list_empty(&slave->sdo_confs)) { // skip SDO configuration
-        // set state to SAVEOP
-        fsm->slave_state = ec_fsm_slaveconf_saveop;
-        fsm->change_new = EC_SLAVE_STATE_SAVEOP;
-        fsm->change_state = ec_fsm_change_start;
-        fsm->change_state(fsm); // execute immediately
-        return;
-    }
-
-    fsm->slave_state = ec_fsm_slaveconf_sdoconf;
-    fsm->coe_sdodata = list_entry(slave->sdo_confs.next, ec_sdo_data_t, list);
-    fsm->coe_state = ec_fsm_coe_down_start;
-    fsm->coe_state(fsm); // execute immediately
-}
-
-/*****************************************************************************/
-
-/**
-   Slave state: SDOCONF.
-*/
-
-void ec_fsm_slaveconf_sdoconf(ec_fsm_t *fsm /**< finite state machine */)
-{
-    fsm->coe_state(fsm); // execute CoE state machine
-
-    if (fsm->coe_state == ec_fsm_error) {
-        fsm->slave->error_flag = 1;
-        fsm->slave_state = ec_fsm_error;
-        return;
-    }
-
-    if (fsm->coe_state != ec_fsm_end) return;
-
-    // Another SDO to configure?
-    if (fsm->coe_sdodata->list.next != &fsm->slave->sdo_confs) {
-        fsm->coe_sdodata = list_entry(fsm->coe_sdodata->list.next,
-                                      ec_sdo_data_t, list);
-        fsm->coe_state = ec_fsm_coe_down_start;
-        fsm->coe_state(fsm); // execute immediately
-        return;
-    }
-
-    // All SDOs are now configured.
-
     // set state to SAVEOP
     fsm->slave_state = ec_fsm_slaveconf_saveop;
     fsm->change_new = EC_SLAVE_STATE_SAVEOP;
@@ -1516,11 +1698,14 @@
 /*****************************************************************************/
 
 /**
-   Slave state: SAVEOP.
+   Slave configuration state: SAVEOP.
 */
 
 void ec_fsm_slaveconf_saveop(ec_fsm_t *fsm /**< finite state machine */)
 {
+    ec_master_t *master = fsm->master;
+    ec_slave_t *slave = fsm->slave;
+
     fsm->change_state(fsm); // execute state change state machine
 
     if (fsm->change_state == ec_fsm_error) {
@@ -1532,8 +1717,17 @@
     if (fsm->change_state != ec_fsm_end) return;
 
     // slave is now in SAVEOP
+
+    if (master->debug_level) {
+        EC_DBG("Slave %i is now in PREOP.\n", slave->ring_position);
+    }
+
     if (fsm->slave->current_state == fsm->slave->requested_state) {
         fsm->slave_state = ec_fsm_end; // successful
+        if (master->debug_level) {
+            EC_DBG("Finished configuration of slave %i.\n",
+                   slave->ring_position);
+        }
         return;
     }
 
@@ -1547,15 +1741,18 @@
 /*****************************************************************************/
 
 /**
-   Slave state: OP
+   Slave configuration state: OP
 */
 
 void ec_fsm_slaveconf_op(ec_fsm_t *fsm /**< finite state machine */)
 {
+    ec_master_t *master = fsm->master;
+    ec_slave_t *slave = fsm->slave;
+
     fsm->change_state(fsm); // execute state change state machine
 
     if (fsm->change_state == ec_fsm_error) {
-        fsm->slave->error_flag = 1;
+        slave->error_flag = 1;
         fsm->slave_state = ec_fsm_error;
         return;
     }
@@ -1563,6 +1760,12 @@
     if (fsm->change_state != ec_fsm_end) return;
 
     // slave is now in OP
+
+    if (master->debug_level) {
+        EC_DBG("Slave %i is now in OP.\n", slave->ring_position);
+        EC_DBG("Finished configuration of slave %i.\n", slave->ring_position);
+    }
+
     fsm->slave_state = ec_fsm_end; // successful
 }
 
@@ -1571,7 +1774,7 @@
  *****************************************************************************/
 
 /**
-   SII state: START_READING.
+   SII state: START READING.
    Starts reading the slave information interface.
 */
 
@@ -1597,7 +1800,7 @@
 /*****************************************************************************/
 
 /**
-   SII state: READ_CHECK.
+   SII state: READ CHECK.
    Checks, if the SII-read-datagram has been sent and issues a fetch datagram.
 */
 
@@ -1630,7 +1833,7 @@
 /*****************************************************************************/
 
 /**
-   SII state: READ_FETCH.
+   SII state: READ FETCH.
    Fetches the result of an SII-read datagram.
 */
 
@@ -1692,7 +1895,7 @@
 /*****************************************************************************/
 
 /**
-   SII state: START_WRITING.
+   SII state: START WRITING.
    Starts reading the slave information interface.
 */
 
@@ -1713,7 +1916,7 @@
 /*****************************************************************************/
 
 /**
-   SII state: WRITE_CHECK.
+   SII state: WRITE CHECK.
 */
 
 void ec_fsm_sii_write_check(ec_fsm_t *fsm /**< finite state machine */)
@@ -1739,7 +1942,7 @@
 /*****************************************************************************/
 
 /**
-   SII state: WRITE_CHECK2.
+   SII state: WRITE CHECK 2.
 */
 
 void ec_fsm_sii_write_check2(ec_fsm_t *fsm /**< finite state machine */)
@@ -2057,11 +2260,640 @@
 }
 
 /******************************************************************************
+ *  CoE dictionary state machine
+ *****************************************************************************/
+
+/**
+   CoE state: DICT START.
+*/
+
+void ec_fsm_coe_dict_start(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+    uint8_t *data;
+
+    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    EC_WRITE_U16(data, 0x8 << 12); // SDO information
+    EC_WRITE_U8 (data + 2, 0x01); // Get OD List Request
+    EC_WRITE_U8 (data + 3, 0x00);
+    EC_WRITE_U16(data + 4, 0x0000);
+    EC_WRITE_U16(data + 6, 0x0001); // deliver all SDOs!
+
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_request;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT REQUEST.
+*/
+
+void ec_fsm_coe_dict_request(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE dictionary request failed.\n");
+        return;
+    }
+
+    fsm->coe_start = datagram->cycles_sent;
+
+    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_check;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT CHECK.
+*/
+
+void ec_fsm_coe_dict_check(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE mailbox check datagram failed.\n");
+        return;
+    }
+
+    if (!ec_slave_mbox_check(datagram)) {
+        if (datagram->cycles_received
+            - fsm->coe_start >= (cycles_t) 100 * cpu_khz) {
+            fsm->coe_state = ec_fsm_error;
+            EC_ERR("Timeout while checking SDO dictionary on slave %i.\n",
+                   slave->ring_position);
+            return;
+        }
+
+        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+        ec_master_queue_datagram(fsm->master, datagram);
+        return;
+    }
+
+    // Fetch response
+    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_response;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT RESPONSE.
+*/
+
+void ec_fsm_coe_dict_response(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_master_t *master = fsm->master;
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+    uint8_t *data, mbox_prot;
+    size_t rec_size;
+    unsigned int sdo_count, i;
+    uint16_t sdo_index;
+    ec_sdo_t *sdo;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE dictionary response failed.\n");
+        return;
+    }
+
+    if (!(data = ec_slave_mbox_fetch(slave, datagram,
+				     &mbox_prot, &rec_size))) {
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    if (mbox_prot != 0x03) { // CoE
+        EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
+        (EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response
+        EC_ERR("SDO information error response at slave %i!\n",
+               slave->ring_position);
+        ec_canopen_abort_msg(EC_READ_U32(data + 6));
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
+        (EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response
+        EC_ERR("Invalid SDO list response at slave %i!\n",
+               slave->ring_position);
+        ec_print_data(data, rec_size);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (rec_size < 8) {
+        EC_ERR("Invalid data size!\n");
+        ec_print_data(data, rec_size);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    sdo_count = (rec_size - 8) / 2;
+
+    if (master->debug_level) {
+        EC_DBG("Fetching %i SDOs.\n", sdo_count);
+    }
+
+    for (i = 0; i < sdo_count; i++) {
+        sdo_index = EC_READ_U16(data + 8 + i * 2);
+        if (!sdo_index) {
+            // sometimes index is 0... ???
+            if (master->debug_level) {
+                EC_DBG("Received SDO with index 0x0000.\n");
+            }
+            continue;
+        }
+
+        if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_ATOMIC))) {
+            EC_ERR("Failed to allocate memory for SDO!\n");
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+
+        if (ec_sdo_init(sdo, sdo_index, slave)) {
+            EC_ERR("Failed to init SDO!\n");
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+
+        if (kobject_add(&sdo->kobj)) {
+            EC_ERR("Failed to add kobject.\n");
+            kobject_put(&sdo->kobj); // free
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+
+        list_add_tail(&sdo->list, &slave->sdo_dictionary);
+    }
+
+    if (EC_READ_U8(data + 2) & 0x80) { // more messages waiting. check again.
+        fsm->coe_start = datagram->cycles_sent;
+        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+        ec_master_queue_datagram(fsm->master, datagram);
+        fsm->coe_state = ec_fsm_coe_dict_check;
+        return;
+    }
+
+    if (list_empty(&slave->sdo_dictionary)) {
+        // no SDOs in dictionary. finished.
+        fsm->coe_state = ec_fsm_end; // success
+        return;
+    }
+
+    // fetch SDO descriptions
+    fsm->coe_sdo = list_entry(slave->sdo_dictionary.next, ec_sdo_t, list);
+
+    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    EC_WRITE_U16(data, 0x8 << 12); // SDO information
+    EC_WRITE_U8 (data + 2, 0x03); // Get object description request
+    EC_WRITE_U8 (data + 3, 0x00);
+    EC_WRITE_U16(data + 4, 0x0000);
+    EC_WRITE_U16(data + 6, fsm->coe_sdo->index); // SDO index
+
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_desc_request;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT DESC REQUEST.
+*/
+
+void ec_fsm_coe_dict_desc_request(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE SDO description request failed.\n");
+        return;
+    }
+
+    fsm->coe_start = datagram->cycles_sent;
+
+    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_desc_check;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT DESC CHECK.
+*/
+
+void ec_fsm_coe_dict_desc_check(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE mailbox check datagram failed.\n");
+        return;
+    }
+
+    if (!ec_slave_mbox_check(datagram)) {
+        if (datagram->cycles_received
+            - fsm->coe_start >= (cycles_t) 100 * cpu_khz) {
+            fsm->coe_state = ec_fsm_error;
+            EC_ERR("Timeout while checking SDO description on slave %i.\n",
+                   slave->ring_position);
+            return;
+        }
+
+        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+        ec_master_queue_datagram(fsm->master, datagram);
+        return;
+    }
+
+    // Fetch response
+    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_desc_response;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT DESC RESPONSE.
+*/
+
+void ec_fsm_coe_dict_desc_response(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_master_t *master = fsm->master;
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+    ec_sdo_t *sdo = fsm->coe_sdo;
+    uint8_t *data, mbox_prot;
+    size_t rec_size, name_size;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE SDO description response failed.\n");
+        return;
+    }
+
+    if (!(data = ec_slave_mbox_fetch(slave, datagram,
+				     &mbox_prot, &rec_size))) {
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    if (mbox_prot != 0x03) { // CoE
+        EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
+        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
+        EC_ERR("SDO information error response at slave %i while"
+               " fetching SDO 0x%04X!\n", slave->ring_position,
+               sdo->index);
+        ec_canopen_abort_msg(EC_READ_U32(data + 6));
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
+        (EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response
+        EC_READ_U16(data + 6) != sdo->index) { // SDO index
+        EC_ERR("Invalid object description response at slave %i while"
+               " fetching SDO 0x%04X!\n", slave->ring_position,
+               sdo->index);
+        ec_print_data(data, rec_size);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (rec_size < 12) {
+        EC_ERR("Invalid data size!\n");
+        ec_print_data(data, rec_size);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    sdo->subindices = EC_READ_U8(data + 10);
+    sdo->object_code = EC_READ_U8(data + 11);
+
+    name_size = rec_size - 12;
+    if (name_size) {
+        if (!(sdo->name = kmalloc(name_size + 1, GFP_ATOMIC))) {
+            EC_ERR("Failed to allocate SDO name!\n");
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+
+        memcpy(sdo->name, data + 12, name_size);
+        sdo->name[name_size] = 0;
+    }
+
+    if (EC_READ_U8(data + 2) & 0x80) {
+        EC_ERR("Fragment follows (not implemented)!\n");
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (!sdo->subindices) { // no entries
+        // another SDO description to fetch?
+        if (fsm->coe_sdo->list.next != &slave->sdo_dictionary) {
+            fsm->coe_sdo = list_entry(fsm->coe_sdo->list.next, ec_sdo_t, list);
+
+            if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
+                fsm->coe_state = ec_fsm_error;
+                return;
+            }
+
+            EC_WRITE_U16(data, 0x8 << 12); // SDO information
+            EC_WRITE_U8 (data + 2, 0x03); // Get object description request
+            EC_WRITE_U8 (data + 3, 0x00);
+            EC_WRITE_U16(data + 4, 0x0000);
+            EC_WRITE_U16(data + 6, fsm->coe_sdo->index); // SDO index
+
+            ec_master_queue_datagram(fsm->master, datagram);
+            fsm->coe_state = ec_fsm_coe_dict_desc_request;
+            return;
+        }
+
+        if (master->debug_level) {
+            EC_DBG("Finished fetching SDO descriptions.\n");
+        }
+
+        fsm->coe_state = ec_fsm_end;
+        return;
+    }
+
+    fsm->coe_subindex = 1;
+
+    if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) {
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    EC_WRITE_U16(data, 0x8 << 12); // SDO information
+    EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
+    EC_WRITE_U8 (data + 3, 0x00);
+    EC_WRITE_U16(data + 4, 0x0000);
+    EC_WRITE_U16(data + 6, sdo->index); // SDO index
+    EC_WRITE_U8 (data + 8, fsm->coe_subindex); // SDO subindex
+    EC_WRITE_U8 (data + 9, 0x00); // value info (no values)
+
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_entry_request;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT ENTRY REQUEST.
+*/
+
+void ec_fsm_coe_dict_entry_request(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE SDO entry request failed.\n");
+        return;
+    }
+
+    fsm->coe_start = datagram->cycles_sent;
+
+    ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_entry_check;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT ENTRY CHECK.
+*/
+
+void ec_fsm_coe_dict_entry_check(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE mailbox check datagram failed.\n");
+        return;
+    }
+
+    if (!ec_slave_mbox_check(datagram)) {
+        if (datagram->cycles_received
+            - fsm->coe_start >= (cycles_t) 100 * cpu_khz) {
+            fsm->coe_state = ec_fsm_error;
+            EC_ERR("Timeout while checking SDO entry on slave %i.\n",
+                   slave->ring_position);
+            return;
+        }
+
+        ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
+        ec_master_queue_datagram(fsm->master, datagram);
+        return;
+    }
+
+    // Fetch response
+    ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
+    ec_master_queue_datagram(fsm->master, datagram);
+    fsm->coe_state = ec_fsm_coe_dict_entry_response;
+}
+
+/*****************************************************************************/
+
+/**
+   CoE state: DICT ENTRY RESPONSE.
+*/
+
+void ec_fsm_coe_dict_entry_response(ec_fsm_t *fsm /**< finite state machine */)
+{
+    ec_master_t *master = fsm->master;
+    ec_datagram_t *datagram = &fsm->datagram;
+    ec_slave_t *slave = fsm->slave;
+    ec_sdo_t *sdo = fsm->coe_sdo;
+    uint8_t *data, mbox_prot;
+    size_t rec_size, data_size;
+    ec_sdo_entry_t *entry;
+
+    if (datagram->state != EC_DATAGRAM_RECEIVED
+        || datagram->working_counter != 1) {
+        fsm->coe_state = ec_fsm_error;
+        EC_ERR("Reception of CoE SDO description response failed.\n");
+        return;
+    }
+
+    if (!(data = ec_slave_mbox_fetch(slave, datagram,
+				     &mbox_prot, &rec_size))) {
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    if (mbox_prot != 0x03) { // CoE
+        EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
+        (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
+        EC_ERR("SDO information error response at slave %i while"
+               " fetching SDO entry 0x%04X:%i!\n", slave->ring_position,
+               sdo->index, fsm->coe_subindex);
+        ec_canopen_abort_msg(EC_READ_U32(data + 6));
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
+        (EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response
+        EC_READ_U16(data + 6) != sdo->index || // SDO index
+        EC_READ_U8(data + 8) != fsm->coe_subindex) { // SDO subindex
+        EC_ERR("Invalid entry description response at slave %i while"
+               " fetching SDO entry 0x%04X:%i!\n", slave->ring_position,
+               sdo->index, fsm->coe_subindex);
+        ec_print_data(data, rec_size);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (rec_size < 16) {
+        EC_ERR("Invalid data size!\n");
+        ec_print_data(data, rec_size);
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    data_size = rec_size - 16;
+
+    if (!(entry = (ec_sdo_entry_t *)
+          kmalloc(sizeof(ec_sdo_entry_t), GFP_ATOMIC))) {
+        EC_ERR("Failed to allocate entry!\n");
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    if (ec_sdo_entry_init(entry, fsm->coe_subindex, sdo)) {
+        EC_ERR("Failed to init entry!\n");
+        fsm->coe_state = ec_fsm_error;
+	return;
+    }
+
+    entry->data_type = EC_READ_U16(data + 10);
+    entry->bit_length = EC_READ_U16(data + 12);
+
+    if (data_size) {
+        if (!(entry->description = kmalloc(data_size + 1, GFP_ATOMIC))) {
+            EC_ERR("Failed to allocate SDO entry name!\n");
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+        memcpy(entry->description, data + 16, data_size);
+        entry->description[data_size] = 0;
+    }
+
+    if (kobject_add(&entry->kobj)) {
+        EC_ERR("Failed to add kobject.\n");
+        kobject_put(&entry->kobj); // free
+        fsm->coe_state = ec_fsm_error;
+        return;
+    }
+
+    list_add_tail(&entry->list, &sdo->entries);
+
+    if (fsm->coe_subindex < sdo->subindices) {
+        fsm->coe_subindex++;
+
+        if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10))) {
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+
+        EC_WRITE_U16(data, 0x8 << 12); // SDO information
+        EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
+        EC_WRITE_U8 (data + 3, 0x00);
+        EC_WRITE_U16(data + 4, 0x0000);
+        EC_WRITE_U16(data + 6, sdo->index); // SDO index
+        EC_WRITE_U8 (data + 8, fsm->coe_subindex); // SDO subindex
+        EC_WRITE_U8 (data + 9, 0x00); // value info (no values)
+
+        ec_master_queue_datagram(fsm->master, datagram);
+        fsm->coe_state = ec_fsm_coe_dict_entry_request;
+        return;
+    }
+
+    // another SDO description to fetch?
+    if (fsm->coe_sdo->list.next != &slave->sdo_dictionary) {
+        fsm->coe_sdo = list_entry(fsm->coe_sdo->list.next, ec_sdo_t, list);
+
+        if (!(data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8))) {
+            fsm->coe_state = ec_fsm_error;
+            return;
+        }
+
+        EC_WRITE_U16(data, 0x8 << 12); // SDO information
+        EC_WRITE_U8 (data + 2, 0x03); // Get object description request
+        EC_WRITE_U8 (data + 3, 0x00);
+        EC_WRITE_U16(data + 4, 0x0000);
+        EC_WRITE_U16(data + 6, fsm->coe_sdo->index); // SDO index
+
+        ec_master_queue_datagram(fsm->master, datagram);
+        fsm->coe_state = ec_fsm_coe_dict_desc_request;
+        return;
+    }
+
+    if (master->debug_level) {
+        EC_DBG("Finished fetching SDO descriptions.\n");
+    }
+
+    fsm->coe_state = ec_fsm_end;
+}
+
+/******************************************************************************
  *  CoE state machine
  *****************************************************************************/
 
 /**
-   CoE state: DOWN_START.
+   CoE state: DOWN START.
 */
 
 void ec_fsm_coe_down_start(ec_fsm_t *fsm /**< finite state machine */)
@@ -2101,7 +2933,7 @@
 /*****************************************************************************/
 
 /**
-   CoE state: DOWN_REQUEST.
+   CoE state: DOWN REQUEST.
 */
 
 void ec_fsm_coe_down_request(ec_fsm_t *fsm /**< finite state machine */)
@@ -2126,7 +2958,7 @@
 /*****************************************************************************/
 
 /**
-   CoE state: DOWN_CHECK.
+   CoE state: DOWN CHECK.
 */
 
 void ec_fsm_coe_down_check(ec_fsm_t *fsm /**< finite state machine */)
@@ -2164,7 +2996,7 @@
 /*****************************************************************************/
 
 /**
-   CoE state: DOWN_RESPONSE.
+   CoE state: DOWN RESPONSE.
 */
 
 void ec_fsm_coe_down_response(ec_fsm_t *fsm /**< finite state machine */)
--- a/master/fsm.h	Tue Oct 17 14:37:46 2006 +0000
+++ b/master/fsm.h	Tue Oct 17 14:50:04 2006 +0000
@@ -45,6 +45,7 @@
 #include "../include/ecrt.h"
 #include "datagram.h"
 #include "slave.h"
+#include "canopen.h"
 
 /*****************************************************************************/
 
@@ -82,6 +83,8 @@
     void (*coe_state)(ec_fsm_t *); /**< CoE state function */
     ec_sdo_data_t *coe_sdodata; /**< input/output: SDO data object */
     cycles_t coe_start; /**< CoE timestamp */
+    ec_sdo_t *coe_sdo; /**< current SDO */
+    uint8_t coe_subindex; /**< current subindex */
 };
 
 /*****************************************************************************/
--- a/master/slave.c	Tue Oct 17 14:37:46 2006 +0000
+++ b/master/slave.c	Tue Oct 17 14:50:04 2006 +0000
@@ -182,7 +182,6 @@
     ec_sii_pdo_t *pdo, *next_pdo;
     ec_sii_pdo_entry_t *entry, *next_ent;
     ec_sdo_t *sdo, *next_sdo;
-    ec_sdo_entry_t *en, *next_en;
     ec_sdo_data_t *sdodata, *next_sdodata;
 
     slave = container_of(kobj, ec_slave_t, kobj);
@@ -222,14 +221,8 @@
     // free all SDOs
     list_for_each_entry_safe(sdo, next_sdo, &slave->sdo_dictionary, list) {
         list_del(&sdo->list);
-        if (sdo->name) kfree(sdo->name);
-
-        // free all SDO entries
-        list_for_each_entry_safe(en, next_en, &sdo->entries, list) {
-            list_del(&en->list);
-            kfree(en);
-        }
-        kfree(sdo);
+        kobject_del(&sdo->kobj);
+        kobject_put(&sdo->kobj);
     }
 
     // free all SDO configurations
--- a/master/slave.h	Tue Oct 17 14:37:46 2006 +0000
+++ b/master/slave.h	Tue Oct 17 14:50:04 2006 +0000
@@ -168,50 +168,6 @@
 /*****************************************************************************/
 
 /**
-   CANopen SDO.
-*/
-
-typedef struct
-{
-    struct list_head list; /**< list item */
-    uint16_t index; /**< SDO index */
-    uint8_t object_code; /**< object code */
-    char *name; /**< SDO name */
-    struct list_head entries; /**< entry list */
-}
-ec_sdo_t;
-
-/*****************************************************************************/
-
-/**
-   CANopen SDO entry.
-*/
-
-typedef struct
-{
-    struct list_head list; /**< list item */
-    uint8_t subindex; /**< entry subindex */
-    uint16_t data_type; /**< entry data type */
-    uint16_t bit_length; /**< entry length in bit */
-    char *name; /**< entry name */
-}
-ec_sdo_entry_t;
-
-/*****************************************************************************/
-
-typedef struct
-{
-    struct list_head list; /**< list item */
-    uint16_t index; /**< SDO index */
-    uint8_t subindex; /**< SDO subindex */
-    uint8_t *data; /**< pointer to SDO data */
-    size_t size; /**< size of SDO data */
-}
-ec_sdo_data_t;
-
-/*****************************************************************************/
-
-/**
    FMMU configuration.
 */