fp@238: /****************************************************************************** fp@238: * fp@238: * $Id$ fp@238: * fp@238: * Copyright (C) 2006 Florian Pose, Ingenieurgemeinschaft IgH fp@238: * fp@238: * This file is part of the IgH EtherCAT Master. fp@238: * fp@238: * The IgH EtherCAT Master is free software; you can redistribute it fp@238: * and/or modify it under the terms of the GNU General Public License fp@246: * as published by the Free Software Foundation; either version 2 of the fp@246: * License, or (at your option) any later version. fp@238: * fp@238: * The IgH EtherCAT Master is distributed in the hope that it will be fp@238: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of fp@238: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the fp@238: * GNU General Public License for more details. fp@238: * fp@238: * You should have received a copy of the GNU General Public License fp@238: * along with the IgH EtherCAT Master; if not, write to the Free Software fp@238: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA fp@238: * fp@246: * The right to use EtherCAT Technology is granted and comes free of fp@246: * charge under condition of compatibility of product made by fp@246: * Licensee. People intending to distribute/sell products based on the fp@246: * code, have to sign an agreement to guarantee that products using fp@246: * software based on IgH EtherCAT master stay compatible with the actual fp@246: * EtherCAT specification (which are released themselves as an open fp@246: * standard) as the (only) precondition to have the right to use EtherCAT fp@246: * Technology, IP and trade marks. fp@246: * fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@238: \file fp@238: EtherCAT finite state machines. fp@238: */ fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: #include "globals.h" fp@238: #include "master.h" fp@329: #include "mailbox.h" fp@715: #ifdef EC_EOE fp@661: #include "ethernet.h" fp@715: #endif fp@849: fp@528: #include "fsm_master.h" fp@528: fp@528: /*****************************************************************************/ fp@528: fp@528: void ec_fsm_master_state_start(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_broadcast(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_read_states(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_acknowledge(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_validate_vendor(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_validate_product(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_rewrite_addresses(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_configure_slave(ec_fsm_master_t *); fp@717: void ec_fsm_master_state_clear_addresses(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_scan_slaves(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_write_eeprom(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_sdodict(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_sdo_request(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_end(ec_fsm_master_t *); fp@528: void ec_fsm_master_state_error(ec_fsm_master_t *); fp@251: fp@238: /*****************************************************************************/ fp@238: fp@260: /** fp@260: Constructor. fp@260: */ fp@260: fp@528: void ec_fsm_master_init(ec_fsm_master_t *fsm, /**< master state machine */ fp@528: ec_master_t *master, /**< EtherCAT master */ fp@528: ec_datagram_t *datagram /**< datagram object to use */ fp@528: ) fp@238: { fp@238: fsm->master = master; fp@528: fsm->datagram = datagram; fp@528: fsm->state = ec_fsm_master_state_start; fp@650: fsm->idle = 0; fp@528: fsm->slaves_responding = 0; fp@553: fsm->topology_change_pending = 0; fp@528: fsm->slave_states = EC_SLAVE_STATE_UNKNOWN; fp@528: fsm->validate = 0; fp@612: fsm->tainted = 0; fp@238: fp@436: // init sub-state-machines fp@830: ec_fsm_slave_config_init(&fsm->fsm_slave_config, fsm->datagram); fp@831: ec_fsm_slave_scan_init(&fsm->fsm_slave_scan, fsm->datagram, fp@831: &fsm->fsm_slave_config, &fsm->fsm_coe_map); fp@528: ec_fsm_sii_init(&fsm->fsm_sii, fsm->datagram); fp@528: ec_fsm_change_init(&fsm->fsm_change, fsm->datagram); fp@528: ec_fsm_coe_init(&fsm->fsm_coe, fsm->datagram); fp@741: ec_fsm_coe_map_init(&fsm->fsm_coe_map, &fsm->fsm_coe); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@260: /** fp@260: Destructor. fp@260: */ fp@260: fp@528: void ec_fsm_master_clear(ec_fsm_master_t *fsm /**< master state machine */) fp@238: { fp@436: // clear sub-state machines fp@830: ec_fsm_slave_config_clear(&fsm->fsm_slave_config); fp@830: ec_fsm_slave_scan_clear(&fsm->fsm_slave_scan); fp@433: ec_fsm_sii_clear(&fsm->fsm_sii); fp@434: ec_fsm_change_clear(&fsm->fsm_change); fp@436: ec_fsm_coe_clear(&fsm->fsm_coe); fp@741: ec_fsm_coe_map_clear(&fsm->fsm_coe_map); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@260: /** fp@260: Executes the current state of the state machine. fp@505: If the state machine's datagram is not sent or received yet, the execution fp@505: of the state machine is delayed to the next cycle. fp@435: \return false, if state machine has terminated fp@435: */ fp@435: fp@528: int ec_fsm_master_exec(ec_fsm_master_t *fsm /**< master state machine */) fp@528: { fp@528: if (fsm->datagram->state == EC_DATAGRAM_SENT fp@528: || fsm->datagram->state == EC_DATAGRAM_QUEUED) { fp@505: // datagram was not sent or received yet. fp@528: return ec_fsm_master_running(fsm); fp@528: } fp@528: fp@528: fsm->state(fsm); fp@528: return ec_fsm_master_running(fsm); fp@456: } fp@456: fp@456: /*****************************************************************************/ fp@456: fp@456: /** fp@650: * \return false, if state machine has terminated fp@650: */ fp@650: fp@650: int ec_fsm_master_running( fp@650: const ec_fsm_master_t *fsm /**< master state machine */ fp@650: ) fp@528: { fp@528: return fsm->state != ec_fsm_master_state_end fp@528: && fsm->state != ec_fsm_master_state_error; fp@446: } fp@446: fp@650: /*****************************************************************************/ fp@650: fp@650: /** fp@650: * \return true, if the state machine is in an idle phase fp@650: */ fp@650: fp@650: int ec_fsm_master_idle( fp@650: const ec_fsm_master_t *fsm /**< master state machine */ fp@650: ) fp@650: { fp@650: return fsm->idle; fp@650: } fp@650: fp@238: /****************************************************************************** fp@637: * master state machine fp@238: *****************************************************************************/ fp@238: fp@238: /** fp@260: Master state: START. fp@238: Starts with getting slave count and slave states. fp@238: */ fp@238: fp@528: void ec_fsm_master_state_start(ec_fsm_master_t *fsm) fp@528: { fp@650: fsm->idle = 1; fp@528: ec_datagram_brd(fsm->datagram, 0x0130, 2); fp@528: fsm->state = ec_fsm_master_state_broadcast; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: BROADCAST. fp@260: Processes the broadcast read slave count and slaves states. fp@260: */ fp@260: fp@528: void ec_fsm_master_state_broadcast(ec_fsm_master_t *fsm /**< master state machine */) fp@528: { fp@528: ec_datagram_t *datagram = fsm->datagram; fp@553: unsigned int i; fp@238: ec_slave_t *slave; fp@260: ec_master_t *master = fsm->master; fp@238: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT) fp@637: return; // always retry fp@505: fp@650: if (datagram->state != EC_DATAGRAM_RECEIVED) { // link is down fp@528: fsm->slaves_responding = 0; fp@505: list_for_each_entry(slave, &master->slaves, list) { fp@610: ec_slave_set_online_state(slave, EC_SLAVE_OFFLINE); fp@446: } fp@528: fsm->state = ec_fsm_master_state_error; fp@238: return; fp@238: } fp@238: fp@553: // bus topology change? fp@553: if (datagram->working_counter != fsm->slaves_responding) { fp@553: fsm->topology_change_pending = 1; fp@553: fsm->slaves_responding = datagram->working_counter; fp@553: fp@792: EC_INFO("%u slave%s responding.\n", fp@528: fsm->slaves_responding, fp@528: fsm->slaves_responding == 1 ? "" : "s"); fp@262: fp@325: if (master->mode == EC_MASTER_MODE_OPERATION) { fp@528: if (fsm->slaves_responding == master->slave_count) { fp@528: fsm->validate = 1; // start validation later fp@262: } fp@262: else { fp@262: EC_WARN("Invalid slave count. Bus in tainted state.\n"); fp@612: fsm->tainted = 1; fp@262: } fp@260: } fp@260: } fp@260: fp@553: // slave states changed? fp@553: if (EC_READ_U8(datagram->data) != fsm->slave_states) { fp@403: char states[EC_STATE_STRING_SIZE]; fp@553: fsm->slave_states = EC_READ_U8(datagram->data); fp@528: ec_state_string(fsm->slave_states, states); fp@325: EC_INFO("Slave states: %s.\n", states); fp@260: } fp@260: fp@656: if (fsm->topology_change_pending) { fp@656: down(&master->scan_sem); fp@656: if (!master->allow_scan) { fp@656: up(&master->scan_sem); fp@656: } fp@656: else { fp@656: master->scan_state = EC_REQUEST_IN_PROGRESS; fp@656: up(&master->scan_sem); fp@656: fp@656: // topology change when scan is allowed: fp@656: // clear all slaves and scan the bus fp@656: fsm->topology_change_pending = 0; fp@656: fsm->tainted = 0; fp@656: fsm->idle = 0; fp@656: fsm->scan_jiffies = jiffies; fp@656: fp@715: #ifdef EC_EOE fp@656: ec_master_eoe_stop(master); fp@661: ec_master_clear_eoe_handlers(master); fp@715: #endif fp@656: ec_master_destroy_slaves(master); fp@656: fp@656: master->slave_count = datagram->working_counter; fp@656: fp@656: if (!master->slave_count) { fp@656: // no slaves present -> finish state machine. fp@656: master->scan_state = EC_REQUEST_COMPLETE; fp@656: wake_up_interruptible(&master->scan_queue); fp@656: fsm->state = ec_fsm_master_state_end; fp@260: return; fp@260: } fp@260: fp@656: // init slaves fp@656: for (i = 0; i < master->slave_count; i++) { fp@656: if (!(slave = (ec_slave_t *) kmalloc(sizeof(ec_slave_t), fp@656: GFP_ATOMIC))) { fp@656: EC_ERR("Failed to allocate slave %i!\n", i); fp@656: ec_master_destroy_slaves(master); fp@656: master->scan_state = EC_REQUEST_FAILURE; fp@656: wake_up_interruptible(&master->scan_queue); fp@656: fsm->state = ec_fsm_master_state_error; fp@656: return; fp@656: } fp@656: fp@656: if (ec_slave_init(slave, master, i, i + 1)) { fp@656: // freeing of "slave" already done fp@656: ec_master_destroy_slaves(master); fp@656: master->scan_state = EC_REQUEST_FAILURE; fp@656: wake_up_interruptible(&master->scan_queue); fp@656: fsm->state = ec_fsm_master_state_error; fp@656: return; fp@656: } fp@656: fp@656: list_add_tail(&slave->list, &master->slaves); fp@260: } fp@260: fp@717: if (master->debug_level) fp@717: EC_DBG("Clearing station addresses...\n"); fp@717: fp@717: ec_datagram_bwr(datagram, 0x0010, 2); fp@717: EC_WRITE_U16(datagram->data, 0x0000); fp@717: fsm->retries = EC_FSM_RETRIES; fp@717: fsm->state = ec_fsm_master_state_clear_addresses; fp@656: return; fp@656: } fp@260: } fp@260: fp@260: // fetch state from each slave fp@260: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@815: ec_datagram_fprd(fsm->datagram, fsm->slave->station_address, 0x0130, 2); fp@505: fsm->retries = EC_FSM_RETRIES; fp@528: fsm->state = ec_fsm_master_state_read_states; fp@304: } fp@304: fp@304: /*****************************************************************************/ fp@304: fp@304: /** fp@601: * Check for pending EEPROM write requests and process one. fp@601: * \return non-zero, if an EEPROM write request is processed. fp@601: */ fp@601: fp@601: int ec_fsm_master_action_process_eeprom( fp@601: ec_fsm_master_t *fsm /**< master state machine */ fp@601: ) fp@601: { fp@601: ec_master_t *master = fsm->master; fp@604: ec_eeprom_write_request_t *request; fp@601: ec_slave_t *slave; fp@601: fp@604: // search the first request to be processed fp@604: while (1) { fp@604: down(&master->eeprom_sem); fp@604: if (list_empty(&master->eeprom_requests)) { fp@604: up(&master->eeprom_sem); fp@604: break; fp@604: } fp@604: // get first request fp@604: request = list_entry(master->eeprom_requests.next, fp@604: ec_eeprom_write_request_t, list); fp@601: list_del_init(&request->list); // dequeue fp@649: request->state = EC_REQUEST_IN_PROGRESS; fp@601: up(&master->eeprom_sem); fp@601: fp@601: slave = request->slave; fp@729: if (slave->online_state == EC_SLAVE_OFFLINE) { fp@729: EC_ERR("Discarding EEPROM data, slave %i offline.\n", fp@601: slave->ring_position); fp@649: request->state = EC_REQUEST_FAILURE; fp@605: wake_up(&master->eeprom_queue); fp@601: continue; fp@601: } fp@601: fp@601: // found pending EEPROM write operation. execute it! fp@607: if (master->debug_level) fp@607: EC_DBG("Writing EEPROM data to slave %i...\n", fp@607: slave->ring_position); fp@601: fsm->eeprom_request = request; fp@601: fsm->eeprom_index = 0; fp@754: ec_fsm_sii_write(&fsm->fsm_sii, request->slave, request->word_offset, fp@815: request->data, EC_FSM_SII_USE_CONFIGURED_ADDRESS); fp@601: fsm->state = ec_fsm_master_state_write_eeprom; fp@601: fsm->state(fsm); // execute immediately fp@601: return 1; fp@601: } fp@601: fp@601: return 0; fp@601: } fp@601: fp@601: /*****************************************************************************/ fp@601: fp@601: /** fp@814: * Check for pending Sdo requests and process one. fp@814: * \return non-zero, if an Sdo request is processed. fp@646: */ fp@646: fp@646: int ec_fsm_master_action_process_sdo( fp@646: ec_fsm_master_t *fsm /**< master state machine */ fp@646: ) fp@646: { fp@646: ec_master_t *master = fsm->master; fp@849: ec_master_sdo_request_t *request; fp@646: ec_slave_t *slave; fp@646: fp@646: // search the first request to be processed fp@646: while (1) { fp@646: down(&master->sdo_sem); fp@849: if (list_empty(&master->slave_sdo_requests)) { fp@646: up(&master->sdo_sem); fp@646: break; fp@646: } fp@646: // get first request fp@849: request = list_entry(master->slave_sdo_requests.next, fp@849: ec_master_sdo_request_t, list); fp@646: list_del_init(&request->list); // dequeue fp@849: request->req.state = EC_REQUEST_IN_PROGRESS; fp@646: up(&master->sdo_sem); fp@646: fp@831: slave = request->slave; fp@646: if (slave->current_state == EC_SLAVE_STATE_INIT || fp@646: slave->online_state == EC_SLAVE_OFFLINE || fp@646: slave->error_flag) { fp@849: EC_ERR("Discarding Sdo request, slave %u not ready.\n", fp@646: slave->ring_position); fp@849: request->req.state = EC_REQUEST_FAILURE; fp@646: wake_up(&master->sdo_queue); fp@646: continue; fp@646: } fp@646: fp@849: // Found pending Sdo request. Execute it! fp@646: if (master->debug_level) fp@814: EC_DBG("Processing Sdo request for slave %i...\n", fp@646: slave->ring_position); fp@646: fp@849: // Start uploading Sdo fp@650: fsm->idle = 0; fp@646: fsm->sdo_request = request; fp@646: fsm->state = ec_fsm_master_state_sdo_request; fp@849: ec_fsm_coe_upload(&fsm->fsm_coe, slave, &request->req); fp@646: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@646: return 1; fp@646: } fp@646: fp@646: return 0; fp@646: } fp@646: fp@646: /*****************************************************************************/ fp@646: fp@646: /** fp@758: * Check for slaves that are not configured and configure them. fp@656: */ fp@656: fp@656: int ec_fsm_master_action_configure( fp@656: ec_fsm_master_t *fsm /**< master state machine */ fp@656: ) fp@656: { fp@656: ec_slave_t *slave; fp@304: ec_master_t *master = fsm->master; fp@403: char old_state[EC_STATE_STRING_SIZE], new_state[EC_STATE_STRING_SIZE]; fp@304: fp@304: // check if any slaves are not in the state, they're supposed to be fp@656: // FIXME do not check all slaves in every cycle... fp@304: list_for_each_entry(slave, &master->slaves, list) { fp@421: if (slave->error_flag fp@656: || slave->online_state == EC_SLAVE_OFFLINE fp@656: || slave->requested_state == EC_SLAVE_STATE_UNKNOWN fp@656: || (slave->current_state == slave->requested_state fp@656: && slave->self_configured)) continue; fp@421: fp@446: if (master->debug_level) { fp@446: ec_state_string(slave->current_state, old_state); fp@523: if (slave->current_state != slave->requested_state) { fp@523: ec_state_string(slave->requested_state, new_state); fp@523: EC_DBG("Changing state of slave %i (%s -> %s).\n", fp@656: slave->ring_position, old_state, new_state); fp@523: } fp@523: else if (!slave->self_configured) { fp@523: EC_DBG("Reconfiguring slave %i (%s).\n", fp@656: slave->ring_position, old_state); fp@523: } fp@421: } fp@304: fp@650: fsm->idle = 0; fp@304: fsm->slave = slave; fp@637: fsm->state = ec_fsm_master_state_configure_slave; fp@830: ec_fsm_slave_config_start(&fsm->fsm_slave_config, slave); fp@830: ec_fsm_slave_config_exec(&fsm->fsm_slave_config); // execute immediately fp@656: return 1; fp@656: } fp@656: fp@656: if (fsm->config_error) fp@656: master->config_state = EC_REQUEST_FAILURE; fp@656: else fp@656: master->config_state = EC_REQUEST_COMPLETE; fp@656: wake_up_interruptible(&master->config_queue); fp@656: return 0; fp@656: } fp@656: fp@656: /*****************************************************************************/ fp@656: fp@656: /** fp@656: Master action: PROC_STATES. fp@656: Processes the slave states. fp@656: */ fp@656: fp@656: void ec_fsm_master_action_process_states(ec_fsm_master_t *fsm fp@656: /**< master state machine */ fp@656: ) fp@656: { fp@656: ec_master_t *master = fsm->master; fp@656: ec_slave_t *slave; fp@656: fp@656: down(&master->config_sem); fp@656: if (!master->allow_config) { fp@656: up(&master->config_sem); fp@792: } else { fp@656: master->config_state = EC_REQUEST_IN_PROGRESS; fp@656: fsm->config_error = 0; fp@656: up(&master->config_sem); fp@792: fp@656: // check for pending slave configurations fp@656: if (ec_fsm_master_action_configure(fsm)) fp@656: return; fp@304: } fp@304: fp@814: // Check for a pending Sdo request fp@646: if (ec_fsm_master_action_process_sdo(fsm)) fp@646: return; fp@646: fp@442: if (master->mode == EC_MASTER_MODE_IDLE) { fp@442: fp@814: // check, if slaves have an Sdo dictionary to read out. fp@442: list_for_each_entry(slave, &master->slaves, list) { fp@834: if (!(slave->sii.mailbox_protocols & EC_MBOX_COE) fp@442: || slave->sdo_dictionary_fetched fp@442: || slave->current_state == EC_SLAVE_STATE_INIT fp@442: || jiffies - slave->jiffies_preop < EC_WAIT_SDO_DICT * HZ fp@610: || slave->online_state == EC_SLAVE_OFFLINE fp@442: || slave->error_flag) continue; fp@442: fp@442: if (master->debug_level) { fp@814: EC_DBG("Fetching Sdo dictionary from slave %i.\n", fp@442: slave->ring_position); fp@442: } fp@442: fp@442: slave->sdo_dictionary_fetched = 1; fp@442: fp@814: // start fetching Sdo dictionary fp@650: fsm->idle = 0; fp@441: fsm->slave = slave; fp@528: fsm->state = ec_fsm_master_state_sdodict; fp@442: ec_fsm_coe_dictionary(&fsm->fsm_coe, slave); fp@441: ec_fsm_coe_exec(&fsm->fsm_coe); // execute immediately fp@441: return; fp@441: } fp@442: fp@442: // check for pending EEPROM write operations. fp@601: if (ec_fsm_master_action_process_eeprom(fsm)) fp@601: return; // EEPROM write request found fp@304: } fp@304: fp@528: fsm->state = ec_fsm_master_state_end; fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master action: Get state of next slave. fp@260: */ fp@260: fp@528: void ec_fsm_master_action_next_slave_state(ec_fsm_master_t *fsm fp@528: /**< master state machine */) fp@260: { fp@260: ec_master_t *master = fsm->master; fp@260: ec_slave_t *slave = fsm->slave; fp@260: fp@304: // is there another slave to query? fp@292: if (slave->list.next != &master->slaves) { fp@292: // process next slave fp@650: fsm->idle = 1; fp@637: fsm->slave = list_entry(slave->list.next, ec_slave_t, list); fp@815: ec_datagram_fprd(fsm->datagram, fsm->slave->station_address, fp@293: 0x0130, 2); fp@505: fsm->retries = EC_FSM_RETRIES; fp@528: fsm->state = ec_fsm_master_state_read_states; fp@304: return; fp@304: } fp@304: fp@304: // all slave states read fp@304: fp@304: // check, if a bus validation has to be done fp@528: if (fsm->validate) { fp@528: fsm->validate = 0; fp@292: list_for_each_entry(slave, &master->slaves, list) { fp@610: if (slave->online_state == EC_SLAVE_ONLINE) continue; fp@292: fp@292: // At least one slave is offline. validate! fp@292: EC_INFO("Validating bus.\n"); fp@650: fsm->idle = 0; fp@292: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@528: fsm->state = ec_fsm_master_state_validate_vendor; fp@815: ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x0008, fp@815: EC_FSM_SII_USE_INCREMENT_ADDRESS); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@292: return; fp@251: } fp@292: } fp@292: fp@304: ec_fsm_master_action_process_states(fsm); fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@409: Master state: READ STATES. fp@260: Fetches the AL- and online state of a slave. fp@260: */ fp@260: fp@528: void ec_fsm_master_state_read_states(ec_fsm_master_t *fsm /**< master state machine */) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@528: ec_datagram_t *datagram = fsm->datagram; fp@260: fp@637: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@637: return; fp@505: fp@325: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@594: EC_ERR("Failed to receive AL state datagram for slave %i" fp@713: " (datagram state %i)\n", fp@713: slave->ring_position, datagram->state); fp@528: fsm->state = ec_fsm_master_state_error; fp@238: return; fp@238: } fp@238: fp@260: // did the slave not respond to its station address? fp@713: if (datagram->working_counter == 0) { fp@610: ec_slave_set_online_state(slave, EC_SLAVE_OFFLINE); fp@260: ec_fsm_master_action_next_slave_state(fsm); fp@260: return; fp@260: } fp@260: fp@713: // FIXME what to to on multiple response? fp@713: fp@260: // slave responded fp@610: ec_slave_set_state(slave, EC_READ_U8(datagram->data)); // set app state first fp@610: ec_slave_set_online_state(slave, EC_SLAVE_ONLINE); fp@260: fp@454: // check, if new slave state has to be acknowledged fp@454: if (slave->current_state & EC_SLAVE_STATE_ACK_ERR && !slave->error_flag) { fp@650: fsm->idle = 0; fp@637: fsm->state = ec_fsm_master_state_acknowledge; fp@454: ec_fsm_change_ack(&fsm->fsm_change, slave); fp@454: ec_fsm_change_exec(&fsm->fsm_change); fp@454: return; fp@454: } fp@454: fp@454: ec_fsm_master_action_next_slave_state(fsm); fp@454: } fp@454: fp@454: /*****************************************************************************/ fp@454: fp@454: /** fp@454: Master state: ACKNOWLEDGE fp@454: */ fp@454: fp@528: void ec_fsm_master_state_acknowledge(ec_fsm_master_t *fsm /**< master state machine */) fp@454: { fp@454: ec_slave_t *slave = fsm->slave; fp@454: fp@454: if (ec_fsm_change_exec(&fsm->fsm_change)) return; fp@454: fp@454: if (!ec_fsm_change_success(&fsm->fsm_change)) { fp@454: fsm->slave->error_flag = 1; fp@454: EC_ERR("Failed to acknowledge state change on slave %i.\n", fp@454: slave->ring_position); fp@528: fsm->state = ec_fsm_master_state_error; fp@454: return; fp@454: } fp@454: fp@260: ec_fsm_master_action_next_slave_state(fsm); fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@260: Master state: VALIDATE_VENDOR. fp@260: Validates the vendor ID of a slave. fp@260: */ fp@260: fp@528: void ec_fsm_master_state_validate_vendor(ec_fsm_master_t *fsm /**< master state machine */) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@260: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@292: fsm->slave->error_flag = 1; fp@260: EC_ERR("Failed to validate vendor ID of slave %i.\n", fp@260: slave->ring_position); fp@528: fsm->state = ec_fsm_master_state_error; fp@238: return; fp@238: } fp@238: fp@834: if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii.vendor_id) { fp@446: EC_ERR("Slave %i has an invalid vendor ID!\n", slave->ring_position); fp@528: fsm->state = ec_fsm_master_state_error; fp@238: return; fp@238: } fp@238: fp@260: // vendor ID is ok. check product code. fp@528: fsm->state = ec_fsm_master_state_validate_product; fp@815: ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x000A, fp@815: EC_FSM_SII_USE_INCREMENT_ADDRESS); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@304: Master action: ADDRESS. fp@260: Looks for slave, that have lost their configuration and writes fp@260: their station address, so that they can be reconfigured later. fp@260: */ fp@260: fp@528: void ec_fsm_master_action_addresses(ec_fsm_master_t *fsm /**< master state machine */) fp@528: { fp@528: ec_datagram_t *datagram = fsm->datagram; fp@260: fp@610: while (fsm->slave->online_state == EC_SLAVE_ONLINE) { fp@260: if (fsm->slave->list.next == &fsm->master->slaves) { // last slave? fp@637: fsm->state = ec_fsm_master_state_end; fp@238: return; fp@238: } fp@260: // check next slave fp@260: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@260: } fp@260: fp@446: if (fsm->master->debug_level) fp@446: EC_DBG("Reinitializing slave %i.\n", fsm->slave->ring_position); fp@260: fp@260: // write station address fp@293: ec_datagram_apwr(datagram, fsm->slave->ring_position, 0x0010, 2); fp@293: EC_WRITE_U16(datagram->data, fsm->slave->station_address); fp@505: fsm->retries = EC_FSM_RETRIES; fp@528: fsm->state = ec_fsm_master_state_rewrite_addresses; fp@304: } fp@304: fp@304: /*****************************************************************************/ fp@304: fp@304: /** fp@304: Master state: VALIDATE_PRODUCT. fp@304: Validates the product ID of a slave. fp@304: */ fp@304: fp@528: void ec_fsm_master_state_validate_product(ec_fsm_master_t *fsm /**< master state machine */) fp@304: { fp@304: ec_slave_t *slave = fsm->slave; fp@304: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@304: fsm->slave->error_flag = 1; fp@304: EC_ERR("Failed to validate product code of slave %i.\n", fp@304: slave->ring_position); fp@528: fsm->state = ec_fsm_master_state_error; fp@304: return; fp@304: } fp@304: fp@834: if (EC_READ_U32(fsm->fsm_sii.value) != slave->sii.product_code) { fp@304: EC_ERR("Slave %i: invalid product code!\n", slave->ring_position); fp@834: EC_ERR("expected 0x%08X, got 0x%08X.\n", slave->sii.product_code, fp@433: EC_READ_U32(fsm->fsm_sii.value)); fp@528: fsm->state = ec_fsm_master_state_error; fp@304: return; fp@304: } fp@304: fp@304: // have all states been validated? fp@304: if (slave->list.next == &fsm->master->slaves) { fp@642: fsm->topology_change_pending = 0; fp@612: fsm->tainted = 0; fp@304: fsm->slave = list_entry(fsm->master->slaves.next, ec_slave_t, list); fp@304: // start writing addresses to offline slaves fp@304: ec_fsm_master_action_addresses(fsm); fp@304: return; fp@304: } fp@304: fp@304: // validate next slave fp@304: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@528: fsm->state = ec_fsm_master_state_validate_vendor; fp@815: ec_fsm_sii_read(&fsm->fsm_sii, slave, 0x0008, fp@815: EC_FSM_SII_USE_INCREMENT_ADDRESS); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@260: } fp@260: fp@260: /*****************************************************************************/ fp@260: fp@260: /** fp@409: Master state: REWRITE ADDRESS. fp@260: Checks, if the new station address has been written to the slave. fp@260: */ fp@260: fp@528: void ec_fsm_master_state_rewrite_addresses(ec_fsm_master_t *fsm fp@528: /**< master state machine */ fp@304: ) fp@260: { fp@260: ec_slave_t *slave = fsm->slave; fp@528: ec_datagram_t *datagram = fsm->datagram; fp@293: 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@594: EC_ERR("Failed to receive address datagram for slave %i" fp@594: " (datagram state %i).\n", fp@594: slave->ring_position, datagram->state); fp@528: fsm->state = ec_fsm_master_state_error; fp@505: return; fp@505: } fp@505: fp@505: if (datagram->working_counter != 1) { fp@713: EC_ERR("Failed to write station address of slave %i: ", fp@505: slave->ring_position); fp@713: ec_datagram_print_wc_error(datagram); fp@528: fsm->state = ec_fsm_master_state_error; fp@505: return; fp@260: } fp@260: fp@260: if (fsm->slave->list.next == &fsm->master->slaves) { // last slave? fp@637: fsm->state = ec_fsm_master_state_end; fp@260: return; fp@260: } fp@260: fp@260: // check next slave fp@260: fsm->slave = list_entry(fsm->slave->list.next, ec_slave_t, list); fp@304: // Write new station address to slave fp@304: ec_fsm_master_action_addresses(fsm); fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@717: * Master state: CLEAR ADDRESSES. fp@717: */ fp@717: fp@717: void ec_fsm_master_state_clear_addresses( fp@717: ec_fsm_master_t *fsm /**< master state machine */ fp@717: ) fp@717: { fp@717: ec_master_t *master = fsm->master; fp@717: ec_datagram_t *datagram = fsm->datagram; fp@717: fp@717: if (datagram->state == EC_DATAGRAM_TIMED_OUT && fsm->retries--) fp@717: return; fp@717: fp@717: if (datagram->state != EC_DATAGRAM_RECEIVED) { fp@717: EC_ERR("Failed to receive address clearing datagram (state %i).\n", fp@717: datagram->state); fp@794: master->scan_state = EC_REQUEST_FAILURE; fp@794: wake_up_interruptible(&master->scan_queue); fp@717: fsm->state = ec_fsm_master_state_error; fp@717: return; fp@717: } fp@717: fp@717: if (datagram->working_counter != master->slave_count) { fp@717: EC_WARN("Failed to clear all station addresses: Cleared %u of %u", fp@717: datagram->working_counter, master->slave_count); fp@717: } fp@717: fp@717: EC_INFO("Scanning bus.\n"); fp@717: fp@717: // begin scanning of slaves fp@717: fsm->slave = list_entry(master->slaves.next, ec_slave_t, list); fp@717: fsm->state = ec_fsm_master_state_scan_slaves; fp@830: ec_fsm_slave_scan_start(&fsm->fsm_slave_scan, fsm->slave); fp@830: ec_fsm_slave_scan_exec(&fsm->fsm_slave_scan); // execute immediately fp@717: } fp@717: fp@717: /*****************************************************************************/ fp@717: fp@717: /** fp@652: * Master state: SCAN SLAVES. fp@652: * Executes the sub-statemachine for the scanning of a slave. fp@652: */ fp@652: fp@652: void ec_fsm_master_state_scan_slaves( fp@652: ec_fsm_master_t *fsm /**< master state machine */ fp@652: ) fp@238: { fp@238: ec_master_t *master = fsm->master; fp@637: ec_slave_t *slave = fsm->slave; fp@325: fp@830: if (ec_fsm_slave_scan_exec(&fsm->fsm_slave_scan)) // execute slave state machine fp@528: return; fp@325: fp@715: #ifdef EC_EOE fp@834: if (slave->sii.mailbox_protocols & EC_MBOX_EOE) { fp@661: // create EoE handler for this slave fp@661: ec_eoe_t *eoe; fp@661: if (!(eoe = kmalloc(sizeof(ec_eoe_t), GFP_KERNEL))) { fp@661: EC_ERR("Failed to allocate EoE handler memory for slave %u!\n", fp@661: slave->ring_position); fp@661: } fp@661: else if (ec_eoe_init(eoe, slave)) { fp@661: EC_ERR("Failed to init EoE handler for slave %u!\n", fp@661: slave->ring_position); fp@661: kfree(eoe); fp@661: } fp@661: else { fp@661: list_add_tail(&eoe->list, &master->eoe_handlers); fp@661: } fp@661: } fp@715: #endif fp@661: fp@325: // another slave to fetch? fp@637: if (slave->list.next != &master->slaves) { fp@637: fsm->slave = list_entry(slave->list.next, ec_slave_t, list); fp@830: ec_fsm_slave_scan_start(&fsm->fsm_slave_scan, fsm->slave); fp@830: ec_fsm_slave_scan_exec(&fsm->fsm_slave_scan); // execute immediately fp@325: return; fp@325: } fp@325: fp@652: EC_INFO("Bus scanning completed in %u ms.\n", fp@652: (u32) (jiffies - fsm->scan_jiffies) * 1000 / HZ); fp@661: fp@792: // Attach slave configurations fp@792: ec_master_attach_slave_configs(master); fp@792: fp@716: #ifdef EC_EOE fp@661: // check if EoE processing has to be started fp@661: ec_master_eoe_start(master); fp@715: #endif fp@661: fp@656: master->scan_state = EC_REQUEST_COMPLETE; fp@656: wake_up_interruptible(&master->scan_queue); fp@446: fp@528: fsm->state = ec_fsm_master_state_end; fp@238: } fp@238: fp@238: /*****************************************************************************/ fp@238: fp@238: /** fp@409: Master state: CONFIGURE SLAVES. fp@260: Starts configuring a slave. fp@260: */ fp@260: fp@528: void ec_fsm_master_state_configure_slave(ec_fsm_master_t *fsm fp@528: /**< master state machine */ fp@304: ) fp@260: { fp@830: if (ec_fsm_slave_config_exec(&fsm->fsm_slave_config)) // execute slave's state machine fp@830: return; fp@830: fp@830: if (!ec_fsm_slave_config_success(&fsm->fsm_slave_config)) fp@656: fsm->config_error = 1; fp@656: fp@656: // configure next slave, if necessary fp@656: if (ec_fsm_master_action_configure(fsm)) fp@656: return; fp@656: fp@656: fsm->state = ec_fsm_master_state_end; fp@238: } fp@238: fp@269: /*****************************************************************************/ fp@269: fp@269: /** fp@409: Master state: WRITE EEPROM. fp@269: */ fp@269: fp@607: void ec_fsm_master_state_write_eeprom( fp@607: ec_fsm_master_t *fsm /**< master state machine */) fp@269: { fp@601: ec_master_t *master = fsm->master; fp@601: ec_eeprom_write_request_t *request = fsm->eeprom_request; fp@601: ec_slave_t *slave = request->slave; fp@269: fp@435: if (ec_fsm_sii_exec(&fsm->fsm_sii)) return; fp@433: fp@433: if (!ec_fsm_sii_success(&fsm->fsm_sii)) { fp@601: slave->error_flag = 1; fp@607: EC_ERR("Failed to write EEPROM data to slave %i.\n", fp@607: slave->ring_position); fp@649: request->state = EC_REQUEST_FAILURE; fp@605: wake_up(&master->eeprom_queue); fp@601: fsm->state = ec_fsm_master_state_error; fp@601: return; fp@601: } fp@601: fp@601: fsm->eeprom_index++; fp@754: if (fsm->eeprom_index < request->word_size) { fp@601: ec_fsm_sii_write(&fsm->fsm_sii, slave, fp@754: request->word_offset + fsm->eeprom_index, fp@754: request->data + fsm->eeprom_index * 2, fp@815: EC_FSM_SII_USE_CONFIGURED_ADDRESS); fp@433: ec_fsm_sii_exec(&fsm->fsm_sii); // execute immediately fp@269: return; fp@269: } fp@269: fp@269: // finished writing EEPROM fp@607: if (master->debug_level) fp@743: EC_DBG("Finished writing %u words of EEPROM data to slave %u.\n", fp@754: request->word_size, slave->ring_position); fp@649: request->state = EC_REQUEST_COMPLETE; fp@605: wake_up(&master->eeprom_queue); fp@269: fp@325: // TODO: Evaluate new EEPROM contents! fp@325: fp@601: // check for another EEPROM write request fp@601: if (ec_fsm_master_action_process_eeprom(fsm)) fp@601: return; // processing another request fp@601: fp@637: fsm->state = ec_fsm_master_state_end; fp@419: } fp@419: fp@419: /*****************************************************************************/ fp@419: fp@419: /** fp@814: Master state: SdoDICT. fp@419: */ fp@419: fp@528: void ec_fsm_master_state_sdodict(ec_fsm_master_t *fsm /**< master state machine */) fp@419: { fp@419: ec_slave_t *slave = fsm->slave; fp@419: ec_master_t *master = fsm->master; fp@419: fp@436: if (ec_fsm_coe_exec(&fsm->fsm_coe)) return; fp@436: fp@436: if (!ec_fsm_coe_success(&fsm->fsm_coe)) { fp@528: fsm->state = ec_fsm_master_state_error; fp@419: return; fp@419: } fp@419: fp@814: // Sdo dictionary fetching finished fp@419: fp@419: if (master->debug_level) { fp@423: unsigned int sdo_count, entry_count; fp@423: ec_slave_sdo_dict_info(slave, &sdo_count, &entry_count); fp@814: EC_DBG("Fetched %i Sdos and %i entries from slave %i.\n", fp@423: sdo_count, entry_count, slave->ring_position); fp@419: } fp@419: fp@637: fsm->state = ec_fsm_master_state_end; fp@269: } fp@269: fp@430: /*****************************************************************************/ fp@430: fp@430: /** fp@831: Master state: SDO REQUEST. fp@430: */ fp@430: fp@528: void ec_fsm_master_state_sdo_request(ec_fsm_master_t *fsm /**< master state machine */) fp@430: { fp@430: ec_master_t *master = fsm->master; fp@849: ec_master_sdo_request_t *request = fsm->sdo_request; fp@436: fp@436: if (ec_fsm_coe_exec(&fsm->fsm_coe)) return; fp@436: fp@436: if (!ec_fsm_coe_success(&fsm->fsm_coe)) { fp@814: EC_DBG("Failed to process Sdo request for slave %i.\n", fp@646: fsm->slave->ring_position); fp@849: request->req.state = EC_REQUEST_FAILURE; fp@646: wake_up(&master->sdo_queue); fp@646: fsm->state = ec_fsm_master_state_error; fp@646: return; fp@646: } fp@646: fp@814: // Sdo request finished fp@849: request->req.state = EC_REQUEST_COMPLETE; fp@646: wake_up(&master->sdo_queue); fp@646: fp@646: if (master->debug_level) fp@814: EC_DBG("Finished Sdo request for slave %i.\n", fp@646: fsm->slave->ring_position); fp@646: fp@814: // check for another Sdo request fp@646: if (ec_fsm_master_action_process_sdo(fsm)) fp@646: return; // processing another request fp@430: fp@637: fsm->state = ec_fsm_master_state_end; fp@430: } fp@430: fp@446: /*****************************************************************************/ fp@446: fp@446: /** fp@446: State: ERROR. fp@446: */ fp@446: fp@528: void ec_fsm_master_state_error( fp@528: ec_fsm_master_t *fsm /**< master state machine */ fp@528: ) fp@528: { fp@528: fsm->state = ec_fsm_master_state_start; fp@446: } fp@446: fp@446: /*****************************************************************************/ fp@446: fp@446: /** fp@446: State: END. fp@446: */ fp@446: fp@528: void ec_fsm_master_state_end(ec_fsm_master_t *fsm /**< master state machine */) fp@528: { fp@528: fsm->state = ec_fsm_master_state_start; fp@528: } fp@528: fp@528: /*****************************************************************************/ fp@528: