fp@436: /******************************************************************************
fp@436:  *
fp@436:  *  $Id$
fp@436:  *
fp@1326:  *  Copyright (C) 2006-2008  Florian Pose, Ingenieurgemeinschaft IgH
fp@436:  *
fp@436:  *  This file is part of the IgH EtherCAT Master.
fp@436:  *
fp@1326:  *  The IgH EtherCAT Master is free software; you can redistribute it and/or
fp@1326:  *  modify it under the terms of the GNU General Public License version 2, as
fp@1326:  *  published by the Free Software Foundation.
fp@436:  *
fp@1326:  *  The IgH EtherCAT Master is distributed in the hope that it will be useful,
fp@1326:  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
fp@1326:  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
fp@1326:  *  Public License for more details.
fp@436:  *
fp@1326:  *  You should have received a copy of the GNU General Public License along
fp@1326:  *  with the IgH EtherCAT Master; if not, write to the Free Software
fp@436:  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
fp@436:  *
fp@1363:  *  ---
fp@1363:  *
fp@1363:  *  The license mentioned above concerns the source code only. Using the
fp@1363:  *  EtherCAT technology and brand is only permitted in compliance with the
fp@1363:  *  industrial property and similar rights of Beckhoff Automation GmbH.
fp@436:  *
fp@436:  *****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    \file
fp@436:    EtherCAT CoE state machines.
fp@436: */
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: #include "globals.h"
fp@436: #include "master.h"
fp@436: #include "mailbox.h"
fp@436: #include "fsm_coe.h"
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@1053: /** Maximum time in ms to wait for responses when reading out the dictionary.
fp@1053:  */
fp@1794: #define EC_FSM_COE_DICT_TIMEOUT 1000
fp@1053: 
fp@1563: #define EC_COE_DOWN_REQ_HEADER_SIZE      10
fp@1563: #define EC_COE_DOWN_SEG_REQ_HEADER_SIZE  3
fp@1563: #define EC_COE_DOWN_SEG_MIN_DATA_SIZE    7
fp@1563: 
fp@1791: #define DEBUG_RETRIES 0
fp@1792: #define DEBUG_LONG 0
fp@1791: 
fp@1053: /*****************************************************************************/
fp@1053: 
fp@436: void ec_fsm_coe_dict_start(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_request(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_check(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_response(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *);
fp@436: 
fp@436: void ec_fsm_coe_down_start(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_down_request(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_down_check(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_down_response(ec_fsm_coe_t *);
fp@1562: void ec_fsm_coe_down_seg_check(ec_fsm_coe_t *);
fp@1562: void ec_fsm_coe_down_seg_response(ec_fsm_coe_t *);
fp@436: 
fp@436: void ec_fsm_coe_up_start(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_up_request(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_up_check(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_up_response(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *);
fp@436: 
fp@436: void ec_fsm_coe_end(ec_fsm_coe_t *);
fp@436: void ec_fsm_coe_error(ec_fsm_coe_t *);
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@1327:    SDO abort messages.
fp@1327:    The "abort SDO transfer request" supplies an abort code,
fp@436:    which can be translated to clear text. This table does
fp@436:    the mapping of the codes and messages.
fp@436: */
fp@436: 
fp@436: const ec_code_msg_t sdo_abort_messages[] = {
fp@436:     {0x05030000, "Toggle bit not changed"},
fp@1327:     {0x05040000, "SDO protocol timeout"},
fp@436:     {0x05040001, "Client/Server command specifier not valid or unknown"},
fp@436:     {0x05040005, "Out of memory"},
fp@436:     {0x06010000, "Unsupported access to an object"},
fp@436:     {0x06010001, "Attempt to read a write-only object"},
fp@436:     {0x06010002, "Attempt to write a read-only object"},
fp@436:     {0x06020000, "This object does not exist in the object directory"},
fp@1327:     {0x06040041, "The object cannot be mapped into the PDO"},
fp@436:     {0x06040042, "The number and length of the objects to be mapped would"
fp@1327:      " exceed the PDO length"},
fp@436:     {0x06040043, "General parameter incompatibility reason"},
fp@436:     {0x06040047, "Gerneral internal incompatibility in device"},
fp@436:     {0x06060000, "Access failure due to a hardware error"},
fp@436:     {0x06070010, "Data type does not match, length of service parameter does"
fp@436:      " not match"},
fp@436:     {0x06070012, "Data type does not match, length of service parameter too"
fp@436:      " high"},
fp@436:     {0x06070013, "Data type does not match, length of service parameter too"
fp@436:      " low"},
fp@436:     {0x06090011, "Subindex does not exist"},
fp@436:     {0x06090030, "Value range of parameter exceeded"},
fp@436:     {0x06090031, "Value of parameter written too high"},
fp@436:     {0x06090032, "Value of parameter written too low"},
fp@436:     {0x06090036, "Maximum value is less than minimum value"},
fp@436:     {0x08000000, "General error"},
fp@436:     {0x08000020, "Data cannot be transferred or stored to the application"},
fp@436:     {0x08000021, "Data cannot be transferred or stored to the application"
fp@436:      " because of local control"},
fp@436:     {0x08000022, "Data cannot be transferred or stored to the application"
fp@436:      " because of the present device state"},
fp@436:     {0x08000023, "Object dictionary dynamic generation fails or no object"
fp@436:      " dictionary is present"},
fp@436:     {}
fp@436: };
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@1327:    Outputs an SDO abort message.
fp@436: */
fp@436: 
fp@436: void ec_canopen_abort_msg(uint32_t abort_code)
fp@436: {
fp@436:     const ec_code_msg_t *abort_msg;
fp@436: 
fp@436:     for (abort_msg = sdo_abort_messages; abort_msg->code; abort_msg++) {
fp@436:         if (abort_msg->code == abort_code) {
fp@1327:             EC_ERR("SDO abort message 0x%08X: \"%s\".\n",
fp@436:                    abort_msg->code, abort_msg->message);
fp@436:             return;
fp@436:         }
fp@436:     }
fp@436: 
fp@1327:     EC_ERR("Unknown SDO abort code 0x%08X.\n", abort_code);
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    Constructor.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_init(ec_fsm_coe_t *fsm, /**< finite state machine */
fp@436:                      ec_datagram_t *datagram /**< datagram */
fp@436:                      )
fp@436: {
fp@436:     fsm->state = NULL;
fp@436:     fsm->datagram = datagram;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    Destructor.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_clear(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@1327:    Starts reading a slaves' SDO dictionary.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dictionary(ec_fsm_coe_t *fsm, /**< finite state machine */
fp@436:                            ec_slave_t *slave /**< EtherCAT slave */
fp@436:                            )
fp@436: {
fp@436:     fsm->slave = slave;
fp@436:     fsm->state = ec_fsm_coe_dict_start;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@1327:    Starts to transfer an SDO to/from a slave.
fp@859: */
fp@859: 
fp@859: void ec_fsm_coe_transfer(
fp@854:         ec_fsm_coe_t *fsm, /**< State machine. */
fp@854:         ec_slave_t *slave, /**< EtherCAT slave. */
fp@1327:         ec_sdo_request_t *request /**< SDO request. */
fp@854:         )
fp@436: {
fp@436:     fsm->slave = slave;
fp@854:     fsm->request = request;
fp@859:     if (request->dir == EC_DIR_OUTPUT)
fp@859:         fsm->state = ec_fsm_coe_down_start;
fp@859:     else
fp@859:         fsm->state = ec_fsm_coe_up_start;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    Executes the current state of the state machine.
fp@436:    \return false, if state machine has terminated
fp@436: */
fp@436: 
fp@436: int ec_fsm_coe_exec(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     fsm->state(fsm);
fp@436: 
fp@436:     return fsm->state != ec_fsm_coe_end && fsm->state != ec_fsm_coe_error;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    Returns, if the state machine terminated with success.
fp@436:    \return non-zero if successful.
fp@436: */
fp@436: 
fp@436: int ec_fsm_coe_success(ec_fsm_coe_t *fsm /**< Finite state machine */)
fp@436: {
fp@436:     return fsm->state == ec_fsm_coe_end;
fp@436: }
fp@436: 
fp@895: /*****************************************************************************/
fp@895: 
fp@1092: /** Check if the received data are a CoE emergency request.
fp@1092:  *
fp@1092:  * If the check is positive, the emergency request is output.
fp@1092:  *
fp@1092:  * \return The data were an emergency request.
fp@895:  */
fp@895: int ec_fsm_coe_check_emergency(
fp@895:         ec_fsm_coe_t *fsm, /**< Finite state machine */
fp@895:         const uint8_t *data, /**< CoE mailbox data. */
fp@895:         size_t size /**< CoE mailbox data size. */
fp@895:         )
fp@895: {
fp@895:     if (size < 2 || ((EC_READ_U16(data) >> 12) & 0x0F) != 0x01)
fp@895:         return 0;
fp@895: 
fp@895:     if (size < 10) {
fp@895:         EC_WARN("Received incomplete CoE Emergency request from slave %u:\n",
fp@895:                 fsm->slave->ring_position);
fp@895:         ec_print_data(data, size);
fp@895:         return 1;
fp@895:     }
fp@895:     
fp@895:     EC_INFO("CoE Emergency Request received from slave %u:\n",
fp@895:             fsm->slave->ring_position);
fp@895:     EC_INFO("Error code 0x%04X, Error register 0x%02X, data:\n",
fp@895:             EC_READ_U16(data + 2), EC_READ_U8(data + 4));
fp@895:     ec_print_data(data + 5, 5);
fp@895:     return 1;
fp@895: }
fp@895: 
fp@436: /******************************************************************************
fp@436:  *  CoE dictionary state machine
fp@436:  *****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT START.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_start(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     uint8_t *data;
fp@436: 
fp@834:     if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
fp@825:         EC_ERR("Slave %u does not support CoE!\n", slave->ring_position);
fp@825:         fsm->state = ec_fsm_coe_error;
fp@825:         return;
fp@825:     }
fp@825: 
fp@1219:     if (slave->sii.has_general && !slave->sii.coe_details.enable_sdo_info) {
fp@1327:         EC_ERR("Slave %u does not support SDO information service!\n",
fp@1219:                 slave->ring_position);
fp@1219:         fsm->state = ec_fsm_coe_error;
fp@1219:         return;
fp@1219:     }
fp@1219: 
fp@1313:     data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     EC_WRITE_U16(data, 0x8 << 12); // SDO information
fp@436:     EC_WRITE_U8 (data + 2, 0x01); // Get OD List Request
fp@436:     EC_WRITE_U8 (data + 3, 0x00);
fp@436:     EC_WRITE_U16(data + 4, 0x0000);
fp@1327:     EC_WRITE_U16(data + 6, 0x0001); // deliver all SDOs!
fp@436: 
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_request;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT REQUEST.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_request(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE dictionary request datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Reception of CoE dictionary request failed on slave %u: ",
fp@713:                 slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1038:     fsm->jiffies_start = datagram->jiffies_sent;
fp@436: 
fp@436:     ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_check;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT CHECK.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_check(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return;
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Reception of CoE mailbox check datagram failed on slave %u: ",
fp@436:                slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (!ec_slave_mbox_check(datagram)) {
fp@1038:         unsigned long diff_ms =
fp@1038:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1053:         if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@1327:             EC_ERR("Timeout while waiting for SDO dictionary list response "
fp@1111:                     "on slave %u.\n", slave->ring_position);
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     // Fetch response
fp@436:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_response;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT RESPONSE.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_response(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     uint8_t *data, mbox_prot;
fp@436:     size_t rec_size;
fp@436:     unsigned int sdo_count, i;
fp@769:     uint16_t sdo_index, fragments_left;
fp@436:     ec_sdo_t *sdo;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE dictionary response datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Reception of CoE dictionary response failed on slave %u: ",
fp@713:                 slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1313:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (mbox_prot != 0x03) { // CoE
fp@446:         EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@769:         return;
fp@436:     }
fp@436: 
fp@895:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@895:         // check for CoE response again
fp@895:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@895:         fsm->retries = EC_FSM_RETRIES;
fp@895:         fsm->state = ec_fsm_coe_dict_check;
fp@895:         return;
fp@895:     }
fp@895: 
fp@1037:     if (rec_size < 3) {
fp@1543:         EC_ERR("Received corrupted SDO dictionary response (size %zu).\n",
fp@1037:                 rec_size);
fp@1037:         fsm->state = ec_fsm_coe_error;
fp@1037:         return;
fp@1037:     }
fp@1037: 
fp@1327:     if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
fp@436:         (EC_READ_U8(data + 2) & 0x7F) == 0x07) { // error response
fp@1327:         EC_ERR("SDO information error response at slave %u!\n",
fp@436:                slave->ring_position);
fp@1037:         if (rec_size < 10) {
fp@1327:             EC_ERR("Incomplete SDO information error response:\n");
fp@1037:             ec_print_data(data, rec_size);
fp@1037:         } else {
fp@1037:             ec_canopen_abort_msg(EC_READ_U32(data + 6));
fp@1037:         }
fp@436:         fsm->state = ec_fsm_coe_error;
fp@769:         return;
fp@436:     }
fp@436: 
fp@1327:     if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
fp@436:         (EC_READ_U8 (data + 2) & 0x7F) != 0x02) { // Get OD List response
fp@1053:         if (fsm->slave->master->debug_level) {
fp@1327:             EC_DBG("Invalid SDO list response at slave %u! Retrying...\n",
fp@1053:                     slave->ring_position);
fp@1053:             ec_print_data(data, rec_size);
fp@1053:         }
fp@1053:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1053:         fsm->retries = EC_FSM_RETRIES;
fp@1053:         fsm->state = ec_fsm_coe_dict_check;
fp@769:         return;
fp@436:     }
fp@436: 
fp@1037:     if (rec_size < 8 || rec_size % 2) {
fp@1543:         EC_ERR("Invalid data size %zu!\n", rec_size);
fp@436:         ec_print_data(data, rec_size);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@769:         return;
fp@436:     }
fp@436: 
fp@436:     sdo_count = (rec_size - 8) / 2;
fp@436: 
fp@436:     for (i = 0; i < sdo_count; i++) {
fp@436:         sdo_index = EC_READ_U16(data + 8 + i * 2);
fp@436:         if (!sdo_index) {
fp@461:             if (slave->master->debug_level)
fp@1327:                 EC_WARN("SDO dictionary of slave %u contains index 0x0000.\n",
fp@461:                         slave->ring_position);
fp@436:             continue;
fp@436:         }
fp@436: 
fp@998:         if (!(sdo = (ec_sdo_t *) kmalloc(sizeof(ec_sdo_t), GFP_KERNEL))) {
fp@1327:             EC_ERR("Failed to allocate memory for SDO!\n");
fp@436:             fsm->state = ec_fsm_coe_error;
fp@436:             return;
fp@436:         }
fp@436: 
fp@992:         ec_sdo_init(sdo, slave, sdo_index);
fp@436:         list_add_tail(&sdo->list, &slave->sdo_dictionary);
fp@436:     }
fp@436: 
fp@769:     fragments_left = EC_READ_U16(data + 4);
fp@769:     if (slave->master->debug_level && fragments_left) {
fp@1327:         EC_DBG("SDO list fragments left: %u\n", fragments_left);
fp@769:     }
fp@769: 
fp@769:     if (EC_READ_U8(data + 2) & 0x80 || fragments_left) { // more messages waiting. check again.
fp@1038:         fsm->jiffies_start = datagram->jiffies_sent;
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         fsm->state = ec_fsm_coe_dict_check;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (list_empty(&slave->sdo_dictionary)) {
fp@1327:         // no SDOs in dictionary. finished.
fp@436:         fsm->state = ec_fsm_coe_end; // success
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     // fetch SDO descriptions
fp@436:     fsm->sdo = list_entry(slave->sdo_dictionary.next, ec_sdo_t, list);
fp@436: 
fp@1313:     data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     EC_WRITE_U16(data, 0x8 << 12); // SDO information
fp@436:     EC_WRITE_U8 (data + 2, 0x03); // Get object description request
fp@436:     EC_WRITE_U8 (data + 3, 0x00);
fp@436:     EC_WRITE_U16(data + 4, 0x0000);
fp@1327:     EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index
fp@436: 
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_desc_request;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT DESC REQUEST.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_desc_request(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: check for response first?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Failed to receive CoE SDO description request datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Reception of CoE SDO description"
fp@895:                 " request failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1038:     fsm->jiffies_start = datagram->jiffies_sent;
fp@436: 
fp@436:     ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_desc_check;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT DESC CHECK.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_desc_check(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return;
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE mailbox check datagram from slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                 slave->ring_position, datagram->state);
fp@436:         return;
fp@436:     }
fp@436: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE mailbox check"
fp@895:                 " datagram failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@505:         return;
fp@505:     }
fp@505: 
fp@436:     if (!ec_slave_mbox_check(datagram)) {
fp@1038:         unsigned long diff_ms =
fp@1038:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1053:         if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@1793:             EC_ERR("Timeout while waiting for SDO 0x%04x object description "
fp@1793:                     "response on slave %u.\n", fsm->sdo->index,
fp@1793:                     slave->ring_position);
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     // Fetch response
fp@436:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_desc_response;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT DESC RESPONSE.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_desc_response(ec_fsm_coe_t *fsm
fp@436:                                    /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     ec_sdo_t *sdo = fsm->sdo;
fp@436:     uint8_t *data, mbox_prot;
fp@436:     size_t rec_size, name_size;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Failed to receive CoE SDO description response datagram from"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Reception of CoE SDO description"
fp@895:                 " response failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1313:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (mbox_prot != 0x03) { // CoE
fp@446:         EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@895:     }
fp@895: 
fp@895:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@895:         // check for CoE response again
fp@895:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@895:         fsm->retries = EC_FSM_RETRIES;
fp@895:         fsm->state = ec_fsm_coe_dict_desc_check;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1037:     if (rec_size < 3) {
fp@1543:         EC_ERR("Received corrupted SDO description response (size %zu).\n",
fp@1037:                 rec_size);
fp@1037:         fsm->state = ec_fsm_coe_error;
fp@1037:         return;
fp@1037:     }
fp@1037: 
fp@1327:     if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
fp@436:         (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
fp@1327:         EC_ERR("SDO information error response at slave %u while"
fp@1327:                " fetching SDO 0x%04X!\n", slave->ring_position,
fp@436:                sdo->index);
fp@436:         ec_canopen_abort_msg(EC_READ_U32(data + 6));
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1037:     if (rec_size < 8) {
fp@1543:         EC_ERR("Received corrupted SDO description response (size %zu).\n",
fp@1037:                 rec_size);
fp@1037:         fsm->state = ec_fsm_coe_error;
fp@1037:         return;
fp@1037:     }
fp@1037: 
fp@1327:     if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
fp@436:         (EC_READ_U8 (data + 2) & 0x7F) != 0x04 || // Object desc. response
fp@1327:         EC_READ_U16(data + 6) != sdo->index) { // SDO index
fp@1053:         if (fsm->slave->master->debug_level) {
fp@1053:             EC_DBG("Invalid object description response at slave %u while"
fp@1327:                     " fetching SDO 0x%04X!\n", slave->ring_position,
fp@1053:                     sdo->index);
fp@1053:             ec_print_data(data, rec_size);
fp@1053:         }
fp@1053:         // check for CoE response again
fp@1053:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1053:         fsm->retries = EC_FSM_RETRIES;
fp@1053:         fsm->state = ec_fsm_coe_dict_desc_check;
fp@895:         return;
fp@436:     }
fp@436: 
fp@436:     if (rec_size < 12) {
fp@436:         EC_ERR("Invalid data size!\n");
fp@436:         ec_print_data(data, rec_size);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@962:     sdo->max_subindex = EC_READ_U8(data + 10);
fp@436:     sdo->object_code = EC_READ_U8(data + 11);
fp@436: 
fp@436:     name_size = rec_size - 12;
fp@436:     if (name_size) {
fp@998:         if (!(sdo->name = kmalloc(name_size + 1, GFP_KERNEL))) {
fp@1327:             EC_ERR("Failed to allocate SDO name!\n");
fp@436:             fsm->state = ec_fsm_coe_error;
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         memcpy(sdo->name, data + 12, name_size);
fp@436:         sdo->name[name_size] = 0;
fp@436:     }
fp@436: 
fp@436:     if (EC_READ_U8(data + 2) & 0x80) {
fp@436:         EC_ERR("Fragment follows (not implemented)!\n");
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@436:     // start fetching entries
fp@436: 
fp@436:     fsm->subindex = 0;
fp@436: 
fp@1313:     data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     EC_WRITE_U16(data, 0x8 << 12); // SDO information
fp@436:     EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
fp@436:     EC_WRITE_U8 (data + 3, 0x00);
fp@436:     EC_WRITE_U16(data + 4, 0x0000);
fp@1327:     EC_WRITE_U16(data + 6, sdo->index); // SDO index
fp@1327:     EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex
fp@1382:     EC_WRITE_U8 (data + 9, 0x01); // value info (access rights only)
fp@436: 
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_entry_request;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT ENTRY REQUEST.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_entry_request(ec_fsm_coe_t *fsm
fp@436:                                    /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: check for response first?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Failed to receive CoE SDO entry request datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Reception of CoE SDO entry request failed on slave %u: ",
fp@713:                 slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1038:     fsm->jiffies_start = datagram->jiffies_sent;
fp@436: 
fp@505:     ec_slave_mbox_prepare_check(slave, datagram); // can not fail
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_entry_check;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT ENTRY CHECK.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_entry_check(ec_fsm_coe_t *fsm
fp@436:                                  /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return;
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE mailbox check datagram from slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@436:         return;
fp@436:     }
fp@436: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE mailbox check"
fp@895:                 " datagram failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@505:         return;
fp@505:     }
fp@505: 
fp@436:     if (!ec_slave_mbox_check(datagram)) {
fp@1038:         unsigned long diff_ms =
fp@1038:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1053:         if (diff_ms >= EC_FSM_COE_DICT_TIMEOUT) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@1793:             EC_ERR("Timeout while waiting for SDO entry 0x%04x:%x"
fp@1793:                     " description response on slave %u.\n",
fp@1793:                     fsm->sdo->index, fsm->subindex, slave->ring_position);
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     // Fetch response
fp@436:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_dict_entry_response;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DICT ENTRY RESPONSE.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_dict_entry_response(ec_fsm_coe_t *fsm
fp@436:                                     /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     ec_sdo_t *sdo = fsm->sdo;
fp@436:     uint8_t *data, mbox_prot;
fp@436:     size_t rec_size, data_size;
fp@436:     ec_sdo_entry_t *entry;
fp@1382:     u16 word;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Failed to receive CoE SDO description response datagram from"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1327:         EC_ERR("Reception of CoE SDO description"
fp@895:                 " response failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1313:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (mbox_prot != 0x03) { // CoE
fp@446:         EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@895:     }
fp@895: 
fp@895:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@895:         // check for CoE response again
fp@895:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@895:         fsm->retries = EC_FSM_RETRIES;
fp@895:         fsm->state = ec_fsm_coe_dict_entry_check;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1037:     if (rec_size < 3) {
fp@1327:         EC_ERR("Received corrupted SDO entry description response "
fp@1543:                 "(size %zu).\n", rec_size);
fp@1037:         fsm->state = ec_fsm_coe_error;
fp@1037:         return;
fp@1037:     }
fp@1037: 
fp@1327:     if (EC_READ_U16(data) >> 12 == 0x8 && // SDO information
fp@436:         (EC_READ_U8 (data + 2) & 0x7F) == 0x07) { // error response
fp@1327:         EC_ERR("SDO information error response at slave %u while"
fp@1327:                " fetching SDO entry 0x%04X:%02X!\n", slave->ring_position,
fp@436:                sdo->index, fsm->subindex);
fp@436:         ec_canopen_abort_msg(EC_READ_U32(data + 6));
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1037:     if (rec_size < 9) {
fp@1327:         EC_ERR("Received corrupted SDO entry description response "
fp@1543:                 "(size %zu).\n", rec_size);
fp@1037:         fsm->state = ec_fsm_coe_error;
fp@1037:         return;
fp@1037:     }
fp@1037: 
fp@1327:     if (EC_READ_U16(data) >> 12 != 0x8 || // SDO information
fp@436:         (EC_READ_U8(data + 2) & 0x7F) != 0x06 || // Entry desc. response
fp@1327:         EC_READ_U16(data + 6) != sdo->index || // SDO index
fp@1327:         EC_READ_U8(data + 8) != fsm->subindex) { // SDO subindex
fp@1053:         if (fsm->slave->master->debug_level) {
fp@1053:             EC_DBG("Invalid entry description response at slave %u while"
fp@1327:                     " fetching SDO entry 0x%04X:%02X!\n", slave->ring_position,
fp@1053:                     sdo->index, fsm->subindex);
fp@1053:             ec_print_data(data, rec_size);
fp@1053:         }
fp@1053:         // check for CoE response again
fp@1053:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1053:         fsm->retries = EC_FSM_RETRIES;
fp@1053:         fsm->state = ec_fsm_coe_dict_entry_check;
fp@895:         return;
fp@436:     }
fp@436: 
fp@436:     if (rec_size < 16) {
fp@1543:         EC_ERR("Invalid data size %zu!\n", rec_size);
fp@436:         ec_print_data(data, rec_size);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@436:     data_size = rec_size - 16;
fp@436: 
fp@436:     if (!(entry = (ec_sdo_entry_t *)
fp@998:           kmalloc(sizeof(ec_sdo_entry_t), GFP_KERNEL))) {
fp@436:         EC_ERR("Failed to allocate entry!\n");
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@991:     ec_sdo_entry_init(entry, sdo, fsm->subindex);
fp@436:     entry->data_type = EC_READ_U16(data + 10);
fp@436:     entry->bit_length = EC_READ_U16(data + 12);
fp@436: 
fp@1382:     // read access rights
fp@1382:     word = EC_READ_U16(data + 14);
fp@1382:     entry->read_access[EC_SDO_ENTRY_ACCESS_PREOP] = word & 0x0001;
fp@1382:     entry->read_access[EC_SDO_ENTRY_ACCESS_SAFEOP] = (word >> 1)  & 0x0001;
fp@1382:     entry->read_access[EC_SDO_ENTRY_ACCESS_OP] = (word >> 2)  & 0x0001;
fp@1382:     entry->write_access[EC_SDO_ENTRY_ACCESS_PREOP] = (word >> 3) & 0x0001;
fp@1382:     entry->write_access[EC_SDO_ENTRY_ACCESS_SAFEOP] = (word >> 4)  & 0x0001;
fp@1382:     entry->write_access[EC_SDO_ENTRY_ACCESS_OP] = (word >> 5)  & 0x0001;
fp@1382: 
fp@436:     if (data_size) {
fp@484:         uint8_t *desc;
fp@998:         if (!(desc = kmalloc(data_size + 1, GFP_KERNEL))) {
fp@1327:             EC_ERR("Failed to allocate SDO entry name!\n");
fp@436:             fsm->state = ec_fsm_coe_error;
fp@436:             return;
fp@436:         }
fp@484:         memcpy(desc, data + 16, data_size);
fp@484:         desc[data_size] = 0;
fp@484:         entry->description = desc;
fp@436:     }
fp@436: 
fp@436:     list_add_tail(&entry->list, &sdo->entries);
fp@436: 
fp@962:     if (fsm->subindex < sdo->max_subindex) {
fp@436:         fsm->subindex++;
fp@436: 
fp@1313:         data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10);
fp@1313:         if (IS_ERR(data)) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@436:             return;
fp@436:         }
fp@436: 
fp@1327:         EC_WRITE_U16(data, 0x8 << 12); // SDO information
fp@436:         EC_WRITE_U8 (data + 2, 0x05); // Get entry description request
fp@436:         EC_WRITE_U8 (data + 3, 0x00);
fp@436:         EC_WRITE_U16(data + 4, 0x0000);
fp@1327:         EC_WRITE_U16(data + 6, sdo->index); // SDO index
fp@1327:         EC_WRITE_U8 (data + 8, fsm->subindex); // SDO subindex
fp@436:         EC_WRITE_U8 (data + 9, 0x00); // value info (no values)
fp@436: 
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         fsm->state = ec_fsm_coe_dict_entry_request;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     // another SDO description to fetch?
fp@436:     if (fsm->sdo->list.next != &slave->sdo_dictionary) {
fp@436:         fsm->sdo = list_entry(fsm->sdo->list.next, ec_sdo_t, list);
fp@436: 
fp@1313:         data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 8);
fp@1313:         if (IS_ERR(data)) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@436:             return;
fp@436:         }
fp@436: 
fp@1327:         EC_WRITE_U16(data, 0x8 << 12); // SDO information
fp@436:         EC_WRITE_U8 (data + 2, 0x03); // Get object description request
fp@436:         EC_WRITE_U8 (data + 3, 0x00);
fp@436:         EC_WRITE_U16(data + 4, 0x0000);
fp@1327:         EC_WRITE_U16(data + 6, fsm->sdo->index); // SDO index
fp@436: 
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         fsm->state = ec_fsm_coe_dict_desc_request;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     fsm->state = ec_fsm_coe_end;
fp@436: }
fp@436: 
fp@436: /******************************************************************************
fp@436:  *  CoE state machine
fp@436:  *****************************************************************************/
fp@436: 
fp@1563: /** CoE state: DOWN START.
fp@1563:  */
fp@1563: void ec_fsm_coe_down_start(
fp@1563:         ec_fsm_coe_t *fsm /**< finite state machine */
fp@1563:         )
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@854:     ec_sdo_request_t *request = fsm->request;
fp@436:     uint8_t *data;
fp@1562:     uint8_t data_set_size;
fp@436: 
fp@874:     if (fsm->slave->master->debug_level) {
fp@1526:         char subidxstr[10];
fp@1526:         if (request->complete_access) {
fp@1526:             subidxstr[0] = 0x00;
fp@1526:         } else {
fp@1526:             sprintf(subidxstr, ":%02X", request->subindex);
fp@1526:         }
fp@1526:         EC_DBG("Downloading SDO 0x%04X%s to slave %u.\n",
fp@1526:                 request->index, subidxstr, slave->ring_position);
fp@874:         ec_print_data(request->data, request->data_size);
fp@874:     }
fp@436: 
fp@834:     if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
fp@825:         EC_ERR("Slave %u does not support CoE!\n", slave->ring_position);
fp@825:         fsm->state = ec_fsm_coe_error;
fp@825:         return;
fp@825:     }
fp@1507: 
fp@1563:     if (slave->configured_rx_mailbox_size < 
fp@1563:             EC_MBOX_HEADER_SIZE + EC_COE_DOWN_REQ_HEADER_SIZE) {
fp@1562:         EC_ERR("Mailbox too small!\n");
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1507:     if (request->data_size <= 4) { // use expedited transfer type
fp@1563:         data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
fp@1563:                 EC_COE_DOWN_REQ_HEADER_SIZE);
fp@1313:         if (IS_ERR(data)) {
fp@1507:             fsm->state = ec_fsm_coe_error;
fp@1507:             return;
fp@1507:         }
fp@1507: 
fp@1562:         fsm->remaining = 0;
fp@1562: 
fp@1562:         data_set_size = 4 - request->data_size;
fp@1507: 
fp@1507:         EC_WRITE_U16(data, 0x2 << 12); // SDO request
fp@1507:         EC_WRITE_U8 (data + 2, (0x3 // size specified, expedited
fp@1562:                     | data_set_size << 2
fp@1526:                     | ((request->complete_access ? 1 : 0) << 4) 
fp@1507:                     | 0x1 << 5)); // Download request
fp@1507:         EC_WRITE_U16(data + 3, request->index);
fp@1526:         EC_WRITE_U8 (data + 5,
fp@1526:                 request->complete_access ? 0x00 : request->subindex);
fp@1507:         memcpy(data + 6, request->data, request->data_size);
fp@1457:         memset(data + 6 + request->data_size, 0x00, 4 - request->data_size);
fp@1169: 
fp@1169:         if (slave->master->debug_level) {
fp@1169:             EC_DBG("Expedited download request:\n");
fp@1563:             ec_print_data(data, EC_COE_DOWN_REQ_HEADER_SIZE);
fp@1169:         }
fp@1507:     }
fp@1169:     else { // request->data_size > 4, use normal transfer type
fp@1563:         size_t data_size,
fp@1563:                max_data_size =
fp@1563:                    slave->configured_rx_mailbox_size - EC_MBOX_HEADER_SIZE,
fp@1563:                required_data_size =
fp@1563:                    EC_COE_DOWN_REQ_HEADER_SIZE + request->data_size;
fp@1563: 
fp@1563:         if (max_data_size < required_data_size) {
fp@1562:             // segmenting needed
fp@1563:             data_size = max_data_size;
fp@1562:         } else {
fp@1563:             data_size = required_data_size;
fp@1507:         }
fp@1507: 
fp@1507:         data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
fp@1562:                 data_size);
fp@1313:         if (IS_ERR(data)) {
fp@1507:             fsm->state = ec_fsm_coe_error;
fp@1507:             return;
fp@1507:         }
fp@1507: 
fp@1562:         fsm->offset = 0;
fp@1562:         fsm->remaining = request->data_size;
fp@1562: 
fp@1507:         EC_WRITE_U16(data, 0x2 << 12); // SDO request
fp@1563:         EC_WRITE_U8(data + 2,
fp@1563:                 0x1 // size indicator, normal
fp@1563:                 | ((request->complete_access ? 1 : 0) << 4) 
fp@1563:                 | 0x1 << 5); // Download request
fp@1507:         EC_WRITE_U16(data + 3, request->index);
fp@1526:         EC_WRITE_U8 (data + 5,
fp@1526:                 request->complete_access ? 0x00 : request->subindex);
fp@1507:         EC_WRITE_U32(data + 6, request->data_size);
fp@1562: 
fp@1563:         if (data_size > EC_COE_DOWN_REQ_HEADER_SIZE) {
fp@1563:             size_t segment_size = data_size - EC_COE_DOWN_REQ_HEADER_SIZE;
fp@1563:             memcpy(data + EC_COE_DOWN_REQ_HEADER_SIZE,
fp@1563:                     request->data, segment_size);
fp@1563:             fsm->offset += segment_size;
fp@1562:             fsm->remaining -= segment_size;
fp@1562:         }
fp@1169: 
fp@1169:         if (slave->master->debug_level) {
fp@1169:             EC_DBG("Normal download request:\n");
fp@1562:             ec_print_data(data, data_size);
fp@1169:         }
fp@1507:     }
fp@975: 
fp@1035:     fsm->request->jiffies_sent = jiffies;
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_down_request;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DOWN REQUEST.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_down_request(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@1792:     unsigned long diff_ms;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: check for response first?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE download request datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@1792:     diff_ms = (jiffies - fsm->request->jiffies_sent) * 1000 / HZ;
fp@1792: 
fp@505:     if (datagram->working_counter != 1) {
fp@1035:         if (!datagram->working_counter) {
fp@1035:             if (diff_ms < fsm->request->response_timeout) {
fp@1791: #if DEBUG_RETRIES
fp@1035:                 if (fsm->slave->master->debug_level) {
fp@1327:                     EC_DBG("Slave %u did not respond to SDO download request. "
fp@1035:                             "Retrying after %u ms...\n",
fp@1035:                             slave->ring_position, (u32) diff_ms);
fp@1035:                 }
fp@1791: #endif
fp@1112:                 // no response; send request datagram again
fp@1112:                 return;
fp@1035:             }
fp@1035:         }
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1792:         EC_ERR("Reception of CoE download request for SDO 0x%04x:%x failed"
fp@1792:                 " with timeout after %u ms on slave %u: ",
fp@1792:                 fsm->request->index, fsm->request->subindex, (u32) diff_ms,
fp@1792:                 fsm->slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1792: #if DEBUG_LONG
fp@1792:     if (diff_ms > 200) {
fp@1792:         EC_WARN("SDO 0x%04x:%x download took %u ms on slave %u.\n",
fp@1792:                 fsm->request->index, fsm->request->subindex, (u32) diff_ms,
fp@1792:                 fsm->slave->ring_position);
fp@1792:     }
fp@1792: #endif
fp@1792: 
fp@1038:     fsm->jiffies_start = datagram->jiffies_sent;
fp@436: 
fp@436:     ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_down_check;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: DOWN CHECK.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_down_check(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return;
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE mailbox check"
fp@895:                 " datagram failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (!ec_slave_mbox_check(datagram)) {
fp@1038:         unsigned long diff_ms =
fp@1038:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1053:         if (diff_ms >= fsm->request->response_timeout) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@1792:             EC_ERR("Timeout after %u ms while waiting for SDO 0x%04x:%x"
fp@1792:                     " download response on slave %u.\n", (u32) diff_ms,
fp@1792:                     fsm->request->index, fsm->request->subindex, 
fp@1792:                     slave->ring_position);
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     // Fetch response
fp@436:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_down_response;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@1563: void ec_fsm_coe_down_prepare_segment_request(
fp@1563:         ec_fsm_coe_t *fsm /**< finite state machine */
fp@1563:         )
fp@1563: {
fp@1563:     ec_datagram_t *datagram = fsm->datagram;
fp@1563:     ec_slave_t *slave = fsm->slave;
fp@1563:     ec_sdo_request_t *request = fsm->request;
fp@1563:     size_t max_segment_size =
fp@1563:         slave->configured_rx_mailbox_size
fp@1563:         - EC_MBOX_HEADER_SIZE
fp@1563:         - EC_COE_DOWN_SEG_REQ_HEADER_SIZE;
fp@1563:     size_t segment_size, data_size;
fp@1563:     uint8_t last_segment, seg_data_size, *data;
fp@1563: 
fp@1563:     if (fsm->remaining > max_segment_size) {
fp@1563:         segment_size = max_segment_size;
fp@1563:         last_segment = 0;
fp@1563:     } else {
fp@1563:         segment_size = fsm->remaining;
fp@1563:         last_segment = 1;
fp@1563:     }
fp@1563: 
fp@1563:     if (segment_size > EC_COE_DOWN_SEG_MIN_DATA_SIZE) {
fp@1563:         seg_data_size = 0x00;
fp@1563:         data_size = EC_COE_DOWN_SEG_REQ_HEADER_SIZE + segment_size;
fp@1563:     } else {
fp@1563:         seg_data_size = EC_COE_DOWN_SEG_MIN_DATA_SIZE - segment_size;
fp@1563:         data_size = EC_COE_DOWN_SEG_REQ_HEADER_SIZE
fp@1563:             + EC_COE_DOWN_SEG_MIN_DATA_SIZE;
fp@1563:     }
fp@1563: 
fp@1563:     data = ec_slave_mbox_prepare_send(slave, datagram, 0x03,
fp@1563:             data_size);
fp@1563:     if (IS_ERR(data)) {
fp@1563:         fsm->state = ec_fsm_coe_error;
fp@1563:         return;
fp@1563:     }
fp@1563: 
fp@1563:     EC_WRITE_U16(data, 0x2 << 12); // SDO request
fp@1563:     EC_WRITE_U8(data + 2, (last_segment ? 1 : 0)
fp@1563:             | (seg_data_size << 1) 
fp@1563:             | (fsm->toggle << 4)
fp@1563:             | (0x00 << 5)); // Download segment request
fp@1563:     memcpy(data + EC_COE_DOWN_SEG_REQ_HEADER_SIZE,
fp@1563:             request->data + fsm->offset, segment_size);
fp@1563:     if (segment_size < EC_COE_DOWN_SEG_MIN_DATA_SIZE) {
fp@1563:         memset(data + EC_COE_DOWN_SEG_REQ_HEADER_SIZE + segment_size, 0x00,
fp@1563:                 EC_COE_DOWN_SEG_MIN_DATA_SIZE - segment_size);
fp@1563:     }
fp@1563: 
fp@1563:     fsm->offset += segment_size;
fp@1563:     fsm->remaining -= segment_size;
fp@1563: 
fp@1563:     if (slave->master->debug_level) {
fp@1563:         EC_DBG("Download segment request:\n");
fp@1563:         ec_print_data(data, data_size);
fp@1563:     }
fp@1563: 
fp@1563:     fsm->state = ec_fsm_coe_down_seg_check;
fp@1563: }
fp@1563: 
fp@1563: /*****************************************************************************/
fp@1563: 
fp@436: /**
fp@436:    CoE state: DOWN RESPONSE.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_down_response(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     uint8_t *data, mbox_prot;
fp@436:     size_t rec_size;
fp@854:     ec_sdo_request_t *request = fsm->request;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE download response datagram from"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Reception of CoE download response failed on slave %u: ",
fp@713:                 slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1313:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (mbox_prot != 0x03) { // CoE
fp@860:         fsm->state = ec_fsm_coe_error;
fp@446:         EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@860:         return;
fp@436:     }
fp@436: 
fp@895:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@895:         // check for CoE response again
fp@895:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@895:         fsm->retries = EC_FSM_RETRIES;
fp@895:         fsm->state = ec_fsm_coe_down_check;
fp@895:         return;
fp@895:     }
fp@895: 
fp@975:     if (slave->master->debug_level) {
fp@975:         EC_DBG("Download response:\n");
fp@975:         ec_print_data(data, rec_size);
fp@975:     }
fp@975: 
fp@436:     if (rec_size < 6) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@1543:         EC_ERR("Received data are too small (%zu bytes):\n", rec_size);
fp@436:         ec_print_data(data, rec_size);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
fp@1327:         EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
fp@1526:         char subidxstr[10];
fp@1526:         fsm->state = ec_fsm_coe_error;
fp@1526:         if (request->complete_access) {
fp@1526:             subidxstr[0] = 0x00;
fp@1526:         } else {
fp@1526:             sprintf(subidxstr, ":%02X", request->subindex);
fp@1526:         }
fp@1543:         EC_ERR("SDO download 0x%04X%s (%zu bytes) aborted on slave %u.\n",
fp@1526:                 request->index, subidxstr, request->data_size,
fp@1526:                 slave->ring_position);
fp@436:         if (rec_size < 10) {
fp@1526:             EC_ERR("Incomplete abort command:\n");
fp@436:             ec_print_data(data, rec_size);
fp@1037:         } else {
fp@1037:             fsm->request->abort_code = EC_READ_U32(data + 6);
fp@1037:             ec_canopen_abort_msg(fsm->request->abort_code);
fp@1037:         }
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
fp@436:         EC_READ_U8 (data + 2) >> 5 != 0x3 || // Download response
fp@854:         EC_READ_U16(data + 3) != request->index || // index
fp@854:         EC_READ_U8 (data + 5) != request->subindex) { // subindex
fp@1053:         if (slave->master->debug_level) {
fp@1327:             EC_DBG("Invalid SDO download response at slave %u! Retrying...\n",
fp@1053:                     slave->ring_position);
fp@1053:             ec_print_data(data, rec_size);
fp@1053:         }
fp@1053:         // check for CoE response again
fp@1053:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1053:         fsm->retries = EC_FSM_RETRIES;
fp@1053:         fsm->state = ec_fsm_coe_down_check;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1562:     if (fsm->remaining) { // more segments to download
fp@1562:         fsm->toggle = 0;
fp@1563:         ec_fsm_coe_down_prepare_segment_request(fsm);
fp@1562:     } else {
fp@1562:         fsm->state = ec_fsm_coe_end; // success
fp@1562:     }
fp@1562: }
fp@1562: 
fp@1562: /*****************************************************************************/
fp@1562: 
fp@1562: /**
fp@1562:    CoE state: DOWN SEG CHECK.
fp@1562: */
fp@1562: 
fp@1562: void ec_fsm_coe_down_seg_check(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@1562: {
fp@1562:     ec_datagram_t *datagram = fsm->datagram;
fp@1562:     ec_slave_t *slave = fsm->slave;
fp@1562: 
fp@1562:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@1562:         return;
fp@1562: 
fp@1562:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
fp@1562:                 " (datagram state %u).\n",
fp@1562:                slave->ring_position, datagram->state);
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (datagram->working_counter != 1) {
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         EC_ERR("Reception of CoE mailbox segment check"
fp@1562:                 " datagram failed on slave %u: ", slave->ring_position);
fp@1562:         ec_datagram_print_wc_error(datagram);
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (!ec_slave_mbox_check(datagram)) {
fp@1562:         unsigned long diff_ms =
fp@1562:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1562:         if (diff_ms >= fsm->request->response_timeout) {
fp@1562:             fsm->state = ec_fsm_coe_error;
fp@1562:             EC_ERR("Timeout while waiting for SDO download segment response "
fp@1562:                     "on slave %u.\n", slave->ring_position);
fp@1562:             return;
fp@1562:         }
fp@1562: 
fp@1562:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1562:         fsm->retries = EC_FSM_RETRIES;
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     // Fetch response
fp@1562:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@1562:     fsm->retries = EC_FSM_RETRIES;
fp@1562:     fsm->state = ec_fsm_coe_down_seg_response;
fp@1562: }
fp@1562: 
fp@1562: /*****************************************************************************/
fp@1562: 
fp@1562: /**
fp@1562:    CoE state: DOWN SEG RESPONSE.
fp@1562:    \todo Timeout behavior
fp@1562: */
fp@1562: 
fp@1562: void ec_fsm_coe_down_seg_response(
fp@1562:         ec_fsm_coe_t *fsm /**< finite state machine */
fp@1562:         )
fp@1562: {
fp@1562:     ec_datagram_t *datagram = fsm->datagram;
fp@1562:     ec_slave_t *slave = fsm->slave;
fp@1562:     uint8_t *data, mbox_prot;
fp@1562:     size_t rec_size;
fp@1562:     ec_sdo_request_t *request = fsm->request;
fp@1562: 
fp@1562:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@1562:         return; // FIXME: request again?
fp@1562: 
fp@1562:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         EC_ERR("Failed to receive CoE download response datagram from"
fp@1562:                " slave %u (datagram state %u).\n",
fp@1562:                slave->ring_position, datagram->state);
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (datagram->working_counter != 1) {
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         EC_ERR("Reception of CoE download response failed on slave %u: ",
fp@1562:                 slave->ring_position);
fp@1562:         ec_datagram_print_wc_error(datagram);
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1562:     if (IS_ERR(data)) {
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (mbox_prot != 0x03) { // CoE
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@1562:         // check for CoE response again
fp@1562:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1562:         fsm->retries = EC_FSM_RETRIES;
fp@1562:         fsm->state = ec_fsm_coe_down_check;
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (slave->master->debug_level) {
fp@1562:         EC_DBG("Download response:\n");
fp@1562:         ec_print_data(data, rec_size);
fp@1562:     }
fp@1562: 
fp@1562:     if (rec_size < 6) {
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         EC_ERR("Received data are too small (%zu bytes):\n", rec_size);
fp@1562:         ec_print_data(data, rec_size);
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
fp@1562:         EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
fp@1562:         char subidxstr[10];
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         if (request->complete_access) {
fp@1562:             subidxstr[0] = 0x00;
fp@1562:         } else {
fp@1562:             sprintf(subidxstr, ":%02X", request->subindex);
fp@1562:         }
fp@1562:         EC_ERR("SDO download 0x%04X%s (%zu bytes) aborted on slave %u.\n",
fp@1562:                 request->index, subidxstr, request->data_size,
fp@1562:                 slave->ring_position);
fp@1562:         if (rec_size < 10) {
fp@1562:             EC_ERR("Incomplete abort command:\n");
fp@1562:             ec_print_data(data, rec_size);
fp@1562:         } else {
fp@1562:             fsm->request->abort_code = EC_READ_U32(data + 6);
fp@1562:             ec_canopen_abort_msg(fsm->request->abort_code);
fp@1562:         }
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (EC_READ_U16(data) >> 12 != 0x3 ||
fp@1562:             ((EC_READ_U8(data + 2) >> 5) != 0x01)) { // segment response
fp@1562:         if (slave->master->debug_level) {
fp@1562:             EC_DBG("Invalid SDO download response at slave %u! Retrying...\n",
fp@1562:                     slave->ring_position);
fp@1562:             ec_print_data(data, rec_size);
fp@1562:         }
fp@1562:         // check for CoE response again
fp@1562:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1562:         fsm->retries = EC_FSM_RETRIES;
fp@1562:         fsm->state = ec_fsm_coe_down_seg_check;
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (((EC_READ_U8(data + 2) >> 4) & 0x01) != fsm->toggle) {
fp@1562:         EC_ERR("Invalid toggle received during segmented download:\n");
fp@1562:         ec_print_data(data, rec_size);
fp@1562:         fsm->state = ec_fsm_coe_error;
fp@1562:         return;
fp@1562:     }
fp@1562: 
fp@1562:     if (fsm->remaining) { // more segments to download
fp@1562:         fsm->toggle = !fsm->toggle;
fp@1563:         ec_fsm_coe_down_prepare_segment_request(fsm);
fp@1562:     } else {
fp@1562:         fsm->state = ec_fsm_coe_end; // success
fp@1562:     }
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: UP START.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_start(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     ec_master_t *master = slave->master;
fp@436:     ec_sdo_request_t *request = fsm->request;
fp@436:     uint8_t *data;
fp@436: 
fp@459:     if (master->debug_level)
fp@1327:         EC_DBG("Uploading SDO 0x%04X:%02X from slave %u.\n",
fp@831:                request->index, request->subindex, slave->ring_position);
fp@436: 
fp@834:     if (!(slave->sii.mailbox_protocols & EC_MBOX_COE)) {
fp@825:         EC_ERR("Slave %u does not support CoE!\n", slave->ring_position);
fp@825:         fsm->state = ec_fsm_coe_error;
fp@825:         return;
fp@825:     }
fp@825: 
fp@1313:     data = ec_slave_mbox_prepare_send(slave, datagram, 0x03, 10);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1327:     EC_WRITE_U16(data, 0x2 << 12); // SDO request
fp@436:     EC_WRITE_U8 (data + 2, 0x2 << 5); // initiate upload request
fp@831:     EC_WRITE_U16(data + 3, request->index);
fp@831:     EC_WRITE_U8 (data + 5, request->subindex);
fp@476:     memset(data + 6, 0x00, 4);
fp@436: 
fp@436:     if (master->debug_level) {
fp@436:         EC_DBG("Upload request:\n");
fp@476:         ec_print_data(data, 10);
fp@436:     }
fp@436: 
fp@1035:     fsm->request->jiffies_sent = jiffies;
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_up_request;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: UP REQUEST.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_request(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@1792:     unsigned long diff_ms;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: check for response first?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE upload request for slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@1792:     diff_ms = (jiffies - fsm->request->jiffies_sent) * 1000 / HZ;
fp@1792: 
fp@505:     if (datagram->working_counter != 1) {
fp@1035:         if (!datagram->working_counter) {
fp@1035:             if (diff_ms < fsm->request->response_timeout) {
fp@1791: #if DEBUG_RETRIES
fp@1035:                 if (fsm->slave->master->debug_level) {
fp@1327:                     EC_DBG("Slave %u did not respond to SDO upload request. "
fp@1035:                             "Retrying after %u ms...\n",
fp@1035:                             slave->ring_position, (u32) diff_ms);
fp@1035:                 }
fp@1791: #endif
fp@1112:                 // no response; send request datagram again
fp@1112:                 return;
fp@1035:             }
fp@1035:         }
fp@505:         fsm->state = ec_fsm_coe_error;
fp@1792:         EC_ERR("Reception of CoE upload request for SDO 0x%04x:%x failed"
fp@1792:                 " with timeout after %u ms on slave %u: ",
fp@1792:                 fsm->request->index, fsm->request->subindex, (u32) diff_ms,
fp@1792:                 fsm->slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1792: #if DEBUG_LONG
fp@1792:     if (diff_ms > 200) {
fp@1792:         EC_WARN("SDO 0x%04x:%x upload took %u ms on slave %u.\n",
fp@1792:                 fsm->request->index, fsm->request->subindex, (u32) diff_ms,
fp@1792:                 fsm->slave->ring_position);
fp@1792:     }
fp@1792: #endif
fp@1792: 
fp@1038:     fsm->jiffies_start = datagram->jiffies_sent;
fp@436: 
fp@436:     ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_up_check;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: UP CHECK.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_check(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return;
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE mailbox check datagram from slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE mailbox check"
fp@895:                 " datagram failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (!ec_slave_mbox_check(datagram)) {
fp@1038:         unsigned long diff_ms =
fp@1038:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1043:         if (diff_ms >= fsm->request->response_timeout) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@1792:             EC_ERR("Timeout after %u ms while waiting for SDO 0x%04x:%x"
fp@1792:                     " upload response on slave %u.\n", (u32) diff_ms,
fp@1792:                     fsm->request->index, fsm->request->subindex,
fp@1792:                     slave->ring_position);
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     // Fetch response
fp@436:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_up_response;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@1498: /** Prepare an SDO upload segment request.
fp@1498:  */
fp@1498: void ec_fsm_coe_up_prepare_segment_request(
fp@1498:         ec_fsm_coe_t *fsm /**< Finite state machine */
fp@1498:         )
fp@1498: {
fp@1498:     uint8_t *data =
fp@1498:         ec_slave_mbox_prepare_send(fsm->slave, fsm->datagram, 0x03, 10);
fp@1498:     if (IS_ERR(data)) {
fp@1498:         fsm->state = ec_fsm_coe_error;
fp@1498:         return;
fp@1498:     }
fp@1498: 
fp@1498:     EC_WRITE_U16(data, 0x2 << 12); // SDO request
fp@1498:     EC_WRITE_U8 (data + 2, (fsm->toggle << 4 // toggle
fp@1498:                 | 0x3 << 5)); // upload segment request
fp@1550:     memset(data + 3, 0x00, 7);
fp@1498: 
fp@1498:     if (fsm->slave->master->debug_level) {
fp@1498:         EC_DBG("Upload segment request:\n");
fp@1498:         ec_print_data(data, 10);
fp@1498:     }
fp@1498: }
fp@1498: 
fp@1498: /*****************************************************************************/
fp@1498: 
fp@436: /**
fp@436:    CoE state: UP RESPONSE.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_response(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     ec_master_t *master = slave->master;
fp@1468:     uint16_t rec_index;
fp@1468:     uint8_t *data, mbox_prot, rec_subindex;
fp@436:     size_t rec_size, data_size;
fp@436:     ec_sdo_request_t *request = fsm->request;
fp@436:     unsigned int expedited, size_specified;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE upload response datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Reception of CoE upload response failed on slave %u: ",
fp@713:                 slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1313:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (master->debug_level) {
fp@436:         EC_DBG("Upload response:\n");
fp@436:         ec_print_data(data, rec_size);
fp@436:     }
fp@436: 
fp@436:     if (mbox_prot != 0x03) { // CoE
fp@860:         fsm->state = ec_fsm_coe_error;
fp@436:         EC_WARN("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@769:         return;
fp@769:     }
fp@769: 
fp@895:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@895:         // check for CoE response again
fp@895:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@895:         fsm->retries = EC_FSM_RETRIES;
fp@895:         fsm->state = ec_fsm_coe_up_check;
fp@895:         return;
fp@895:     }
fp@895: 
fp@1468:     if (rec_size < 6) {
fp@860:         fsm->state = ec_fsm_coe_error;
fp@1543:         EC_ERR("Received currupted SDO upload response (%zu bytes)!\n", rec_size);
fp@436:         ec_print_data(data, rec_size);
fp@769:         return;
fp@436:     }
fp@436: 
fp@1327:     if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
fp@1468:             EC_READ_U8(data + 2) >> 5 == 0x4) { // abort SDO transfer request
fp@1327:         EC_ERR("SDO upload 0x%04X:%02X aborted on slave %u.\n",
fp@831:                request->index, request->subindex, slave->ring_position);
fp@1037:         if (rec_size >= 10) {
fp@1037:             request->abort_code = EC_READ_U32(data + 6);
fp@1037:             ec_canopen_abort_msg(request->abort_code);
fp@1037:         } else {
fp@769:             EC_ERR("No abort message.\n");
fp@1037:         }
fp@769:         fsm->state = ec_fsm_coe_error;
fp@769:         return;
fp@769:     }
fp@769: 
fp@1468:     if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
fp@1468:             EC_READ_U8(data + 2) >> 5 != 0x2) { // upload response
fp@1468:         EC_ERR("Received unknown response while uploading SDO 0x%04X:%02X"
fp@1468:                 " from slave %u.\n", request->index, request->subindex,
fp@1468:                 slave->ring_position);
fp@1468:         ec_print_data(data, rec_size);
fp@1468:         fsm->state = ec_fsm_coe_error;
fp@1468:         return;
fp@1468:     }
fp@1468: 
fp@1468:     rec_index = EC_READ_U16(data + 3);
fp@1468:     rec_subindex = EC_READ_U8(data + 5);
fp@1468: 
fp@1468:     if (rec_index != request->index || rec_subindex != request->subindex) {
fp@1468:         EC_ERR("Received upload response for wrong SDO (0x%04X:%02X,"
fp@1468:                 " requested: 0x%04X:%02X) from slave %u.\n",
fp@1468:                 rec_index, rec_subindex, request->index, request->subindex,
fp@1468:                 slave->ring_position);
fp@1468:         ec_print_data(data, rec_size);
fp@1468: 
fp@1468:         // check for CoE response again
fp@1468:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1468:         fsm->retries = EC_FSM_RETRIES;
fp@1468:         fsm->state = ec_fsm_coe_up_check;
fp@1468:         return;
fp@1468:     }
fp@1468: 
fp@769:     // normal or expedited?
fp@436:     expedited = EC_READ_U8(data + 2) & 0x02;
fp@436: 
fp@436:     if (expedited) {
fp@436:         size_specified = EC_READ_U8(data + 2) & 0x01;
fp@436:         if (size_specified) {
fp@1339:             fsm->complete_size = 4 - ((EC_READ_U8(data + 2) & 0x0C) >> 2);
fp@769:         } else {
fp@1339:             fsm->complete_size = 4;
fp@1339:         }
fp@1339: 
fp@1339:         if (rec_size < 6 + fsm->complete_size) {
fp@860:             fsm->state = ec_fsm_coe_error;
fp@1327:             EC_ERR("Received currupted SDO expedited upload"
fp@1543:                     " response (only %zu bytes)!\n", rec_size);
fp@769:             ec_print_data(data, rec_size);
fp@769:             return;
fp@769:         }
fp@769: 
fp@1339:         if (ec_sdo_request_copy_data(request, data + 6, fsm->complete_size)) {
fp@854:             fsm->state = ec_fsm_coe_error;
fp@854:             return;
fp@854:         }
fp@769:     } else { // normal
fp@769:         if (rec_size < 10) {
fp@860:             fsm->state = ec_fsm_coe_error;
fp@1327:             EC_ERR("Received currupted SDO normal upload"
fp@1543:                     " response (only %zu bytes)!\n", rec_size);
fp@769:             ec_print_data(data, rec_size);
fp@769:             return;
fp@769:         }
fp@769: 
fp@769:         data_size = rec_size - 10;
fp@1339:         fsm->complete_size = EC_READ_U32(data + 6);
fp@1339: 
fp@1339:         if (!fsm->complete_size) {
fp@860:             fsm->state = ec_fsm_coe_error;
fp@769:             EC_ERR("No complete size supplied!\n");
fp@769:             ec_print_data(data, rec_size);
fp@769:             return;
fp@769:         }
fp@769: 
fp@1339:         if (ec_sdo_request_alloc(request, fsm->complete_size)) {
fp@854:             fsm->state = ec_fsm_coe_error;
fp@854:             return;
fp@854:         }
fp@854: 
fp@854:         if (ec_sdo_request_copy_data(request, data + 10, data_size)) {
fp@854:             fsm->state = ec_fsm_coe_error;
fp@854:             return;
fp@854:         }
fp@854: 
fp@436:         fsm->toggle = 0;
fp@436: 
fp@1339:         if (data_size < fsm->complete_size) {
fp@1339:             if (master->debug_level)
fp@1543:                 EC_DBG("SDO data incomplete (%zu / %u). Segmenting...\n",
fp@1339:                         data_size, fsm->complete_size);
fp@446: 
fp@1498:             ec_fsm_coe_up_prepare_segment_request(fsm);
fp@505:             fsm->retries = EC_FSM_RETRIES;
fp@436:             fsm->state = ec_fsm_coe_up_seg_request;
fp@436:             return;
fp@436:         }
fp@436:     }
fp@436: 
fp@874:     if (master->debug_level) {
fp@874:         EC_DBG("Uploaded data:\n");
fp@874:         ec_print_data(request->data, request->data_size);
fp@874:     }
fp@874: 
fp@436:     fsm->state = ec_fsm_coe_end; // success
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: UP REQUEST.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_seg_request(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: check for response first?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE upload segment request datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE upload segment"
fp@895:                 " request failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1038:     fsm->jiffies_start = datagram->jiffies_sent;
fp@436: 
fp@436:     ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_up_seg_check;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: UP CHECK.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_seg_check(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return;
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@895:         EC_ERR("Failed to receive CoE mailbox check datagram for slave %u"
fp@986:                 " (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE mailbox check"
fp@895:                 " datagram failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (!ec_slave_mbox_check(datagram)) {
fp@1038:         unsigned long diff_ms =
fp@1038:             (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ;
fp@1053:         if (diff_ms >= fsm->request->response_timeout) {
fp@436:             fsm->state = ec_fsm_coe_error;
fp@1327:             EC_ERR("Timeout while waiting for SDO upload segment response "
fp@1053:                     "on slave %u.\n", slave->ring_position);
fp@436:             return;
fp@436:         }
fp@436: 
fp@436:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     // Fetch response
fp@436:     ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail.
fp@505:     fsm->retries = EC_FSM_RETRIES;
fp@436:     fsm->state = ec_fsm_coe_up_seg_response;
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    CoE state: UP RESPONSE.
fp@505:    \todo Timeout behavior
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_up_seg_response(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436:     ec_datagram_t *datagram = fsm->datagram;
fp@436:     ec_slave_t *slave = fsm->slave;
fp@436:     ec_master_t *master = slave->master;
fp@436:     uint8_t *data, mbox_prot;
fp@436:     size_t rec_size, data_size;
fp@436:     ec_sdo_request_t *request = fsm->request;
fp@436:     unsigned int last_segment;
fp@436: 
fp@637:     if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--)
fp@637:         return; // FIXME: request again?
fp@505: 
fp@505:     if (datagram->state != EC_DATAGRAM_RECEIVED) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@505:         EC_ERR("Failed to receive CoE upload segment response datagram for"
fp@986:                " slave %u (datagram state %u).\n",
fp@594:                slave->ring_position, datagram->state);
fp@505:         return;
fp@505:     }
fp@505: 
fp@505:     if (datagram->working_counter != 1) {
fp@505:         fsm->state = ec_fsm_coe_error;
fp@713:         EC_ERR("Reception of CoE upload segment"
fp@895:                 " response failed on slave %u: ", slave->ring_position);
fp@713:         ec_datagram_print_wc_error(datagram);
fp@436:         return;
fp@436:     }
fp@436: 
fp@1313:     data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size);
fp@1313:     if (IS_ERR(data)) {
fp@436:         fsm->state = ec_fsm_coe_error;
fp@436:         return;
fp@436:     }
fp@436: 
fp@436:     if (master->debug_level) {
fp@436:         EC_DBG("Upload segment response:\n");
fp@436:         ec_print_data(data, rec_size);
fp@436:     }
fp@436: 
fp@436:     if (mbox_prot != 0x03) { // CoE
fp@446:         EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@895:     }
fp@895: 
fp@895:     if (ec_fsm_coe_check_emergency(fsm, data, rec_size)) {
fp@895:         // check for CoE response again
fp@895:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@895:         fsm->retries = EC_FSM_RETRIES;
fp@895:         fsm->state = ec_fsm_coe_up_seg_check;
fp@895:         return;
fp@436:     }
fp@436: 
fp@436:     if (rec_size < 10) {
fp@1327:         EC_ERR("Received currupted SDO upload segment response!\n");
fp@436:         ec_print_data(data, rec_size);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1327:     if (EC_READ_U16(data) >> 12 == 0x2 && // SDO request
fp@1468:             EC_READ_U8 (data + 2) >> 5 == 0x4) { // abort SDO transfer request
fp@1327:         EC_ERR("SDO upload 0x%04X:%02X aborted on slave %u.\n",
fp@831:                request->index, request->subindex, slave->ring_position);
fp@1037:         request->abort_code = EC_READ_U32(data + 6);
fp@1037:         ec_canopen_abort_msg(request->abort_code);
fp@436:         fsm->state = ec_fsm_coe_error;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1327:     if (EC_READ_U16(data) >> 12 != 0x3 || // SDO response
fp@436:         EC_READ_U8 (data + 2) >> 5 != 0x0) { // upload segment response
fp@1053:         if (fsm->slave->master->debug_level) {
fp@1327:             EC_DBG("Invalid SDO upload segment response at slave %u!\n",
fp@436:                slave->ring_position);
fp@1053:             ec_print_data(data, rec_size);
fp@1053:         }
fp@1053:         // check for CoE response again
fp@1053:         ec_slave_mbox_prepare_check(slave, datagram); // can not fail.
fp@1053:         fsm->retries = EC_FSM_RETRIES;
fp@1053:         fsm->state = ec_fsm_coe_up_seg_check;
fp@895:         return;
fp@436:     }
fp@436: 
fp@1533:     data_size = rec_size - 3; /* Header of segment upload is smaller than
fp@1533:                                  normal upload */
fp@1533:     if (rec_size == 10) {
fp@1533:         uint8_t seg_size = (EC_READ_U8(data + 2) & 0xE) >> 1;
fp@1533:         data_size -= seg_size;
fp@1339:     }
fp@1339: 
fp@1339:     if (request->data_size + data_size > fsm->complete_size) {
fp@1339:         EC_ERR("SDO upload 0x%04X:%02X failed on slave %u: Fragment"
fp@1339:                 " exceeding complete size!\n",
fp@1339:                request->index, request->subindex, slave->ring_position);
fp@1339:         fsm->state = ec_fsm_coe_error;
fp@1339:         return;
fp@436:     }
fp@436: 
fp@1498:     memcpy(request->data + request->data_size, data + 3, data_size);
fp@854:     request->data_size += data_size;
fp@436: 
fp@1533:     last_segment = EC_READ_U8(data + 2) & 0x01;
fp@436:     if (!last_segment) {
fp@436:         fsm->toggle = !fsm->toggle;
fp@1498:         ec_fsm_coe_up_prepare_segment_request(fsm);
fp@505:         fsm->retries = EC_FSM_RETRIES;
fp@436:         fsm->state = ec_fsm_coe_up_seg_request;
fp@436:         return;
fp@436:     }
fp@436: 
fp@1339:     if (request->data_size != fsm->complete_size) {
fp@1339:         EC_WARN("SDO upload 0x%04X:%02X on slave %u: Assembled data"
fp@1543:                 " size (%zu) does not match complete size (%u)!\n",
fp@1339:                 request->index, request->subindex, slave->ring_position,
fp@1339:                 request->data_size, fsm->complete_size);
fp@1339:     }
fp@1339: 
fp@874:     if (master->debug_level) {
fp@874:         EC_DBG("Uploaded data:\n");
fp@874:         ec_print_data(request->data, request->data_size);
fp@874:     }
fp@874: 
fp@436:     fsm->state = ec_fsm_coe_end; // success
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    State: ERROR.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_error(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/
fp@436: 
fp@436: /**
fp@436:    State: END.
fp@436: */
fp@436: 
fp@436: void ec_fsm_coe_end(ec_fsm_coe_t *fsm /**< finite state machine */)
fp@436: {
fp@436: }
fp@436: 
fp@436: /*****************************************************************************/