fp@1707: /****************************************************************************** fp@1707: * fp@1707: * $Id:$ fp@1707: * fp@1707: * Copyright (C) 2008 Olav Zarges, imc Meßsysteme GmbH fp@1707: * fp@1707: * This file is part of the IgH EtherCAT Master. fp@1707: * fp@1707: * The IgH EtherCAT Master is free software; you can redistribute it fp@1707: * and/or modify it under the terms of the GNU General Public License fp@1707: * as published by the Free Software Foundation; either version 2 of the fp@1707: * License, or (at your option) any later version. fp@1707: * fp@1707: * The IgH EtherCAT Master is distributed in the hope that it will be fp@1707: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@1707: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@1707: * GNU General Public License for more details. fp@1707: * fp@1707: * You should have received a copy of the GNU General Public License fp@1707: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@1707: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@1707: * fp@1707: * The right to use EtherCAT Technology is granted and comes free of fp@1707: * charge under condition of compatibility of product made by fp@1707: * Licensee. People intending to distribute/sell products based on the fp@1707: * code, have to sign an agreement to guarantee that products using fp@1707: * software based on IgH EtherCAT master stay compatible with the actual fp@1707: * EtherCAT specification (which are released themselves as an open fp@1707: * standard) as the (only) precondition to have the right to use EtherCAT fp@1707: * Technology, IP and trade marks. fp@1707: * fp@1707: *****************************************************************************/ fp@1707: fp@1707: /** fp@1707: \file fp@1707: EtherCAT FoE state machines. fp@1707: */ fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: #include "globals.h" fp@1707: #include "master.h" fp@1707: #include "mailbox.h" fp@1707: #include "fsm_foe.h" fp@1707: #include "foe.h" fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** Maximum time in ms to wait for responses when reading out the dictionary. fp@1707: */ fp@1707: #define EC_FSM_FOE_TIMEOUT 3000 fp@1707: fp@1707: #define EC_MBOX_TYPE_FILEACCESS 0x04 fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: int ec_foe_prepare_data_send( ec_fsm_foe_t * ); fp@1707: int ec_foe_prepare_wrq_send( ec_fsm_foe_t * ); fp@1707: int ec_foe_prepare_rrq_send( ec_fsm_foe_t * ); fp@1707: int ec_foe_prepare_send_ack( ec_fsm_foe_t * ); fp@1707: fp@1707: void ec_foe_set_tx_error( ec_fsm_foe_t *, uint32_t ); fp@1707: void ec_foe_set_rx_error( ec_fsm_foe_t *, uint32_t ); fp@1707: fp@1707: void ec_fsm_foe_write(ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_read(ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_end( ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_error( ec_fsm_foe_t * ); fp@1707: fp@1707: void ec_fsm_foe_state_wrq_sent( ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_state_rrq_sent( ec_fsm_foe_t * ); fp@1707: fp@1707: void ec_fsm_foe_state_ack_check( ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_state_ack_read( ec_fsm_foe_t * ); fp@1707: fp@1707: void ec_fsm_foe_state_data_sent( ec_fsm_foe_t * ); fp@1707: fp@1707: void ec_fsm_foe_state_data_check( ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_state_data_read ( ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_state_sent_ack( ec_fsm_foe_t * ); fp@1707: fp@1707: void ec_fsm_foe_write_start( ec_fsm_foe_t * ); fp@1707: void ec_fsm_foe_read_start(ec_fsm_foe_t * ); fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** fp@1707: Constructor. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_init(ec_fsm_foe_t *fsm, /**< finite state machine */ fp@1707: ec_datagram_t *datagram /**< datagram */ fp@1707: ) fp@1707: { fp@1707: fsm->state = NULL; fp@1707: fsm->datagram = datagram; fp@1707: fsm->rx_errors = 0; fp@1707: fsm->tx_errors = 0; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** fp@1707: Destructor. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_clear(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** fp@1707: Executes the current state of the state machine. fp@1707: \return false, if state machine has terminated fp@1707: */ fp@1707: fp@1707: int ec_fsm_foe_exec(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: fsm->state(fsm); fp@1707: fp@1707: return fsm->state != ec_fsm_foe_end && fsm->state != ec_fsm_foe_error; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** fp@1707: Returns, if the state machine terminated with success. fp@1707: \return non-zero if successful. fp@1707: */ fp@1707: fp@1707: int ec_fsm_foe_success(ec_fsm_foe_t *fsm /**< Finite state machine */) fp@1707: { fp@1707: return fsm->state == ec_fsm_foe_end; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_transfer( fp@1707: ec_fsm_foe_t *fsm, /**< State machine. */ fp@1707: ec_slave_t *slave, /**< EtherCAT slave. */ fp@1707: ec_foe_request_t *request /**< Sdo request. */ fp@1707: ) fp@1707: { fp@1707: fsm->slave = slave; fp@1707: fsm->request = request; fp@1707: if (request->dir == EC_DIR_OUTPUT) { fp@1707: fsm->state = ec_fsm_foe_write; fp@1707: } fp@1707: else { fp@1707: fsm->state = ec_fsm_foe_read; fp@1707: } fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** fp@1707: State: ERROR. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_error(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_error()\n"); fp@1707: #endif fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: /** fp@1707: State: END. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_end(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_end\n"); fp@1707: #endif fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: #define EC_MBOX_HEADER_SIZE 6 fp@1707: // uint16_t Length fp@1707: // uint16_t Address fp@1707: // uint8_t reserved fp@1707: // uint8_t Type:4 fp@1707: // uint8_t Counter:4 fp@1707: fp@1707: #define EC_FOE_HEADER_SIZE 6 fp@1707: // uint8_t OpMode fp@1707: // uint8_t reserved fp@1707: // uint32_t PacketNo, Password, ErrorCode fp@1707: fp@1707: enum { fp@1707: EC_FOE_OPMODE_RRQ = 1, fp@1707: EC_FOE_OPMODE_WRQ = 2, fp@1707: EC_FOE_OPMODE_DATA = 3, fp@1707: EC_FOE_OPMODE_ACK = 4, fp@1707: EC_FOE_OPMODE_ERR = 5, fp@1707: EC_FOE_OPMODE_BUSY = 6 fp@1707: } ec_foe_opmode_t; fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: Sends a file or the next fragment. fp@1707: */ fp@1707: fp@1707: int ec_foe_prepare_data_send( ec_fsm_foe_t *fsm ) { fp@1707: size_t remaining_size, current_size; fp@1707: uint8_t* data; fp@1707: fp@1707: remaining_size = fsm->tx_buffer_size - fsm->tx_buffer_offset; fp@1707: fp@1707: if (remaining_size < fsm->slave->sii.tx_mailbox_size - EC_MBOX_HEADER_SIZE - EC_FOE_HEADER_SIZE) { fp@1707: current_size = remaining_size; fp@1707: fsm->tx_last_packet = 1; fp@1707: } fp@1707: else { fp@1707: current_size = fsm->slave->sii.tx_mailbox_size - EC_MBOX_HEADER_SIZE - EC_FOE_HEADER_SIZE; fp@1707: } fp@1707: fp@1707: if (!(data = ec_slave_mbox_prepare_send(fsm->slave, fsm->datagram, fp@1707: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE))) fp@1707: return -1; fp@1707: fp@1707: EC_WRITE_U8 ( data, EC_FOE_OPMODE_DATA ); // OpMode = DataBlock req. fp@1707: EC_WRITE_U32( data + 2, fsm->tx_packet_no ); // PacketNo, Password fp@1707: fp@1707: memcpy(data + EC_FOE_HEADER_SIZE, fsm->tx_buffer + fsm->tx_buffer_offset, current_size); fp@1707: fp@1707: fsm->tx_current_size = current_size; fp@1707: fp@1707: return 0; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: Prepare a write request (WRQ) with filename fp@1707: */ fp@1707: fp@1707: int ec_foe_prepare_wrq_send( ec_fsm_foe_t *fsm ) { fp@1707: size_t current_size; fp@1707: uint8_t *data; fp@1707: fp@1707: fsm->tx_buffer_offset = 0; fp@1707: fsm->tx_current_size = 0; fp@1707: fsm->tx_packet_no = 0; fp@1707: fsm->tx_last_packet = 0; fp@1707: fp@1707: current_size = fsm->tx_filename_len; fp@1707: fp@1707: if (!(data = ec_slave_mbox_prepare_send(fsm->slave, fsm->datagram, fp@1707: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE))) fp@1707: return -1; fp@1707: fp@1707: EC_WRITE_U16( data, EC_FOE_OPMODE_WRQ); // fsm write request fp@1707: EC_WRITE_U32( data + 2, fsm->tx_packet_no ); fp@1707: fp@1707: memcpy(data + EC_FOE_HEADER_SIZE, fsm->tx_filename, current_size); fp@1707: fp@1707: return 0; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: char tx_buffer[0x1000]; fp@1707: void ec_fsm_foe_write(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: fsm->tx_buffer = fsm->request->buffer; fp@1707: fsm->tx_buffer_size = fsm->request->data_size; fp@1707: fsm->tx_buffer_offset = 0; fp@1707: fp@1707: fsm->tx_filename = fsm->request->file_name; fp@1707: fsm->tx_filename_len = strlen(fsm->tx_filename); fp@1707: fp@1707: fsm->state = ec_fsm_foe_write_start; fp@1707: fp@1707: #ifdef use_ext_buffer fp@1707: { fp@1707: int i; fp@1707: fsm->tx_data = tx_buffer; fp@1707: for (i=0 ; itx_data_len = sizeof(tx_buffer); fp@1707: } fp@1707: #endif fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: Initializes the SII write state machine. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_write_start(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: fsm->tx_buffer_offset = 0; fp@1707: fsm->tx_current_size = 0; fp@1707: fsm->tx_packet_no = 0; fp@1707: fsm->tx_last_packet = 0; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_write_start()\n"); fp@1707: #endif fp@1707: fp@1707: if (!(slave->sii.mailbox_protocols & EC_MBOX_FOE)) { fp@1707: ec_foe_set_tx_error(fsm, FOE_MBOX_PROT_ERROR); fp@1707: EC_ERR("Slave %u does not support FoE!\n", slave->ring_position); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (ec_foe_prepare_wrq_send(fsm)) { fp@1707: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: fsm->state = ec_fsm_foe_state_wrq_sent; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_state_ack_check( ec_fsm_foe_t *fsm ) { fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: // printk("ec_fsm_foe_ack_check()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to receive FoE mailbox check datagram for slave %u" fp@1707: " (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: // slave hat noch nichts in die Mailbox getan fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE mailbox check datagram failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (!ec_slave_mbox_check(datagram)) { fp@1707: unsigned long diff_ms = fp@1707: (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ; fp@1707: if (diff_ms >= EC_FSM_FOE_TIMEOUT) { fp@1707: ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); fp@1707: EC_ERR("Timeout while waiting for ack response " fp@1707: "on slave %u.\n", slave->ring_position); fp@1707: return; fp@1707: } fp@1707: // EC_ERR("WAIT!!!!!!!!!!!!!\n"); fp@1707: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: return; fp@1707: } fp@1707: fp@1707: // Fetch response fp@1707: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@1707: fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: fsm->state = ec_fsm_foe_state_ack_read; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_state_ack_read( ec_fsm_foe_t *fsm ) { fp@1707: fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: uint8_t *data, mbox_prot; fp@1707: uint16_t opMode; fp@1707: size_t rec_size; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_ack_read()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to receive FoE ack response datagram for" fp@1707: " slave %u (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE ack response failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (!(data = ec_slave_mbox_fetch(fsm->slave, datagram, &mbox_prot, &rec_size))) { fp@1707: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (mbox_prot != EC_MBOX_TYPE_FILEACCESS) { // FoE fp@1707: ec_foe_set_tx_error(fsm, FOE_MBOX_PROT_ERROR); fp@1707: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@1707: return; fp@1707: } fp@1707: fp@1707: opMode = EC_READ_U16(data); fp@1707: fp@1707: if ( opMode == EC_FOE_OPMODE_BUSY ) { fp@1707: // slave ist noch nicht bereit fp@1707: if (ec_foe_prepare_data_send(fsm)) { fp@1707: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1707: EC_ERR("Slave is busy.\n"); fp@1707: return; fp@1707: } fp@1707: fsm->state = ec_fsm_foe_state_data_sent; fp@1707: return; fp@1707: } fp@1707: fp@1707: if ( opMode == EC_FOE_OPMODE_ACK ) { fp@1707: fsm->tx_packet_no++; fp@1707: fsm->tx_buffer_offset += fsm->tx_current_size; fp@1707: fp@1707: if (fsm->tx_last_packet) { fp@1707: fsm->state = ec_fsm_foe_end; fp@1707: return; fp@1707: } fp@1707: fp@1707: if (ec_foe_prepare_data_send(fsm)) { fp@1707: ec_foe_set_tx_error(fsm, FOE_PROT_ERROR); fp@1707: return; fp@1707: } fp@1707: fsm->state = ec_fsm_foe_state_data_sent; fp@1707: return; fp@1707: } fp@1707: ec_foe_set_tx_error(fsm, FOE_ACK_ERROR); fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: State: WRQ SENT. fp@1707: Checks is the previous transmit datagram succeded and sends the next fp@1707: fragment, if necessary. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_state_wrq_sent( ec_fsm_foe_t *fsm ) { fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_foe_state_sent_wrq()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to send FoE WRQ for slave %u" fp@1707: " (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: // slave hat noch nichts in die Mailbox getan fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE WRQ failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: fsm->jiffies_start = datagram->jiffies_sent; fp@1707: fp@1707: ec_slave_mbox_prepare_check(fsm->slave, datagram); // can not fail. fp@1707: fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: fsm->state = ec_fsm_foe_state_ack_check; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: State: WRQ SENT. fp@1707: Checks is the previous transmit datagram succeded and sends the next fp@1707: fragment, if necessary. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_state_data_sent( ec_fsm_foe_t *fsm ) { fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_state_data_sent()\n"); fp@1707: #endif fp@1707: if (fsm->datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_tx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to receive FoE ack response datagram for" fp@1707: " slave %u (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (fsm->datagram->working_counter != 1) { fp@1707: ec_foe_set_tx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE data send failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: ec_slave_mbox_prepare_check(fsm->slave, fsm->datagram); fp@1707: fsm->jiffies_start = jiffies; fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: fsm->state = ec_fsm_foe_state_ack_check; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: Prepare a read request (RRQ) with filename fp@1707: */ fp@1707: fp@1707: int ec_foe_prepare_rrq_send( ec_fsm_foe_t *fsm ) { fp@1707: size_t current_size; fp@1707: uint8_t *data; fp@1707: fp@1707: current_size = fsm->rx_filename_len; fp@1707: fp@1707: if (!(data = ec_slave_mbox_prepare_send(fsm->slave, fsm->datagram, fp@1707: EC_MBOX_TYPE_FILEACCESS, current_size + EC_FOE_HEADER_SIZE))) fp@1707: return -1; fp@1707: fp@1707: EC_WRITE_U16( data, EC_FOE_OPMODE_RRQ); // fsm read request fp@1707: EC_WRITE_U32( data + 2, 0 ); fp@1707: fp@1707: memcpy(data + EC_FOE_HEADER_SIZE, fsm->rx_filename, current_size); fp@1707: fp@1707: return 0; fp@1707: } fp@1707: fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: int ec_foe_prepare_send_ack( ec_fsm_foe_t *foe ) { fp@1707: uint8_t *data; fp@1707: fp@1707: if (!(data = ec_slave_mbox_prepare_send(foe->slave, foe->datagram, fp@1707: EC_MBOX_TYPE_FILEACCESS, EC_FOE_HEADER_SIZE))) fp@1707: return -1; fp@1707: fp@1707: EC_WRITE_U16( data, EC_FOE_OPMODE_ACK); fp@1707: EC_WRITE_U32( data + 2, foe->rx_expected_packet_no ); fp@1707: fp@1707: return 0; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: /** fp@1707: State: RRQ SENT. fp@1707: Checks is the previous transmit datagram succeded and sends the next fp@1707: fragment, if necessary. fp@1707: */ fp@1707: fp@1707: void ec_fsm_foe_state_rrq_sent( ec_fsm_foe_t *fsm ) { fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_foe_state_rrq_sent()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to send FoE RRQ for slave %u" fp@1707: " (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: // slave hat noch nichts in die Mailbox getan fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE RRQ failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: fsm->jiffies_start = datagram->jiffies_sent; fp@1707: fp@1707: ec_slave_mbox_prepare_check(fsm->slave, datagram); // can not fail. fp@1707: fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: fsm->state = ec_fsm_foe_state_data_check; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: #ifdef myDEBUG fp@1707: char rx_buffer[0x8000]; fp@1707: #endif fp@1707: fp@1707: void ec_fsm_foe_read(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: fsm->state = ec_fsm_foe_read_start; fp@1707: fsm->rx_filename = fsm->request->file_name; fp@1707: fsm->rx_filename_len = strlen(fsm->rx_filename); fp@1707: fp@1707: fsm->rx_buffer = fsm->request->buffer; fp@1707: fsm->rx_buffer_size = fsm->request->buffer_size; fp@1707: fp@1707: #ifdef use_ext_buffer fp@1707: fsm->rx_buffer = rx_buffer; fp@1707: fsm->rx_buffer_size = sizeof(rx_buffer); fp@1707: #endif fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_read_start(ec_fsm_foe_t *fsm /**< finite state machine */) fp@1707: { fp@1707: size_t current_size; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: fsm->rx_buffer_offset = 0; fp@1707: fsm->rx_current_size = 0; fp@1707: fsm->rx_packet_no = 0; fp@1707: fsm->rx_expected_packet_no = 1; fp@1707: fsm->rx_last_packet = 0; fp@1707: fp@1707: current_size = fsm->rx_filename_len; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_read_start()\n"); fp@1707: #endif fp@1707: if (!(slave->sii.mailbox_protocols & EC_MBOX_FOE)) { fp@1707: ec_foe_set_tx_error(fsm, FOE_MBOX_PROT_ERROR); fp@1707: EC_ERR("Slave %u does not support FoE!\n", slave->ring_position); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (ec_foe_prepare_rrq_send(fsm)) { fp@1707: ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: fsm->state = ec_fsm_foe_state_rrq_sent; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_state_data_check ( ec_fsm_foe_t *fsm ) { fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_state_data_check()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to send FoE DATA READ for slave %u" fp@1707: " (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE DATA READ on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (!ec_slave_mbox_check(datagram)) { fp@1707: unsigned long diff_ms = fp@1707: (datagram->jiffies_received - fsm->jiffies_start) * 1000 / HZ; fp@1707: if (diff_ms >= EC_FSM_FOE_TIMEOUT) { fp@1707: ec_foe_set_tx_error(fsm, FOE_TIMEOUT_ERROR); fp@1707: EC_ERR("Timeout while waiting for ack response " fp@1707: "on slave %u.\n", slave->ring_position); fp@1707: return; fp@1707: } fp@1707: fp@1707: ec_slave_mbox_prepare_check(slave, datagram); // can not fail. fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: return; fp@1707: } fp@1707: fp@1707: // Fetch response fp@1707: ec_slave_mbox_prepare_fetch(slave, datagram); // can not fail. fp@1707: fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: fsm->state = ec_fsm_foe_state_data_read; fp@1707: fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_state_data_read ( ec_fsm_foe_t *fsm ) { fp@1707: size_t rec_size; fp@1707: uint8_t *data, opMode, packet_no, mbox_prot; fp@1707: fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_fsm_foe_state_data_read()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to receive FoE DATA READ datagram for" fp@1707: " slave %u (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE DATA READ failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (!(data = ec_slave_mbox_fetch(slave, datagram, &mbox_prot, &rec_size))) { fp@1707: ec_foe_set_rx_error(fsm, FOE_MBOX_FETCH_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (mbox_prot != EC_MBOX_TYPE_FILEACCESS) { // FoE fp@1707: EC_ERR("Received mailbox protocol 0x%02X as response.\n", mbox_prot); fp@1707: ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: opMode = EC_READ_U16(data); fp@1707: fp@1707: if (opMode == EC_FOE_OPMODE_BUSY) { fp@1707: if (ec_foe_prepare_send_ack(fsm)) { fp@1707: ec_foe_set_rx_error(fsm, FOE_PROT_ERROR); fp@1707: } fp@1707: return; fp@1707: } fp@1707: fp@1707: if (opMode != EC_FOE_OPMODE_DATA) { fp@1707: ec_foe_set_rx_error(fsm, FOE_OPMODE_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: packet_no = EC_READ_U16(data + 2); fp@1707: if (packet_no != fsm->rx_expected_packet_no) { fp@1707: ec_foe_set_rx_error(fsm, FOE_PACKETNO_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: rec_size -= EC_FOE_HEADER_SIZE; fp@1707: fp@1707: if ( fsm->rx_buffer_size >= fsm->rx_buffer_offset + rec_size ) { fp@1707: memcpy ( fsm->rx_buffer + fsm->rx_buffer_offset, data + EC_FOE_HEADER_SIZE, rec_size ); fp@1707: fsm->rx_buffer_offset += rec_size; fp@1707: } fp@1707: fp@1707: fsm->rx_last_packet = (rec_size + EC_MBOX_HEADER_SIZE + EC_FOE_HEADER_SIZE != fsm->slave->sii.rx_mailbox_size); fp@1707: fp@1707: if (fsm->rx_last_packet || fp@1707: slave->sii.rx_mailbox_size - EC_MBOX_HEADER_SIZE - EC_FOE_HEADER_SIZE + fsm->rx_buffer_offset <= fsm->rx_buffer_size) { fp@1707: // either it was the last packet or a new packet will fit into the delivered buffer fp@1707: #ifdef myDEBUG fp@1707: printk ("last_packet=true\n"); fp@1707: #endif fp@1707: if (ec_foe_prepare_send_ack(fsm)) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RX_DATA_ACK_ERROR); fp@1707: return; fp@1707: } fp@1707: fp@1707: fsm->state = ec_fsm_foe_state_sent_ack; fp@1707: } fp@1707: else { fp@1707: // no more data fits into the deliverd buffer fp@1707: // ... wait for new read request (an den Treiber) fp@1707: printk ("ERROR: data doesn't fit in receive buffer\n"); fp@1707: printk (" rx_buffer_size = %d\n", fsm->rx_buffer_size); fp@1707: printk (" rx_buffer_offset= %d\n", fsm->rx_buffer_offset); fp@1707: printk (" rec_size = %d\n", rec_size); fp@1707: printk (" rx_mailbox_size = %d\n", slave->sii.rx_mailbox_size); fp@1707: printk (" rx_last_packet = %d\n", fsm->rx_last_packet); fp@1707: // fsm->state = ec_fsm_state_wait_next_read; fp@1707: fsm->request->abort_code = FOE_READY; fp@1707: } fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_fsm_foe_state_sent_ack( ec_fsm_foe_t *fsm ) { fp@1707: fp@1707: ec_datagram_t *datagram = fsm->datagram; fp@1707: ec_slave_t *slave = fsm->slave; fp@1707: fp@1707: #ifdef myDEBUG fp@1707: printk("ec_foe_state_sent_ack()\n"); fp@1707: #endif fp@1707: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@1707: ec_foe_set_rx_error(fsm, FOE_RECEIVE_ERROR); fp@1707: EC_ERR("Failed to send FoE ACK for slave %u" fp@1707: " (datagram state %u).\n", fp@1707: slave->ring_position, datagram->state); fp@1707: return; fp@1707: } fp@1707: fp@1707: if (datagram->working_counter != 1) { fp@1707: // slave hat noch nichts in die Mailbox getan fp@1707: ec_foe_set_rx_error(fsm, FOE_WC_ERROR); fp@1707: EC_ERR("Reception of FoE ACK failed on slave %u: ", fp@1707: slave->ring_position); fp@1707: ec_datagram_print_wc_error(datagram); fp@1707: return; fp@1707: } fp@1707: fp@1707: fsm->jiffies_start = datagram->jiffies_sent; fp@1707: fp@1707: ec_slave_mbox_prepare_check(fsm->slave, datagram); // can not fail. fp@1707: fp@1707: if (fsm->rx_last_packet) { fp@1707: fsm->rx_expected_packet_no = 0; fp@1707: fsm->request->data_size = fsm->rx_buffer_offset; fp@1707: fsm->state = ec_fsm_foe_end; fp@1707: } fp@1707: else { fp@1707: fsm->rx_expected_packet_no++; fp@1707: fsm->retries = EC_FSM_RETRIES; fp@1707: fsm->state = ec_fsm_foe_state_data_check; fp@1707: } fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_foe_set_tx_error( ec_fsm_foe_t *fsm, uint32_t errorcode ) { fp@1707: fsm->tx_errors++; fp@1707: fsm->request->abort_code = errorcode; fp@1707: fsm->state = ec_fsm_foe_error; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/ fp@1707: fp@1707: void ec_foe_set_rx_error( ec_fsm_foe_t *fsm, uint32_t errorcode ) { fp@1707: fsm->rx_errors++; fp@1707: fsm->request->abort_code = errorcode; fp@1707: fsm->state = ec_fsm_foe_error; fp@1707: } fp@1707: fp@1707: /*****************************************************************************/