master/slave.c
changeset 260 5fe7df7f2433
parent 256 dc71aee17f8a
child 264 76cba93ba122
equal deleted inserted replaced
259:87b20532872b 260:5fe7df7f2433
   149     slave->eeprom_group = NULL;
   149     slave->eeprom_group = NULL;
   150     slave->eeprom_desc = NULL;
   150     slave->eeprom_desc = NULL;
   151     slave->requested_state = EC_SLAVE_STATE_UNKNOWN;
   151     slave->requested_state = EC_SLAVE_STATE_UNKNOWN;
   152     slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   152     slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   153     slave->state_error = 0;
   153     slave->state_error = 0;
       
   154     slave->online = 1;
   154 
   155 
   155     ec_command_init(&slave->mbox_command);
   156     ec_command_init(&slave->mbox_command);
   156 
   157 
   157     INIT_LIST_HEAD(&slave->eeprom_strings);
   158     INIT_LIST_HEAD(&slave->eeprom_strings);
   158     INIT_LIST_HEAD(&slave->eeprom_syncs);
   159     INIT_LIST_HEAD(&slave->eeprom_syncs);
   837         udelay(100); // wait a little bit...
   838         udelay(100); // wait a little bit...
   838 
   839 
   839         if (ec_command_nprd(command, slave->station_address, 0x0130, 2))
   840         if (ec_command_nprd(command, slave->station_address, 0x0130, 2))
   840             return;
   841             return;
   841         if (unlikely(ec_master_simple_io(slave->master, command))) {
   842         if (unlikely(ec_master_simple_io(slave->master, command))) {
       
   843             slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   842             EC_WARN("Acknowledge checking failed on slave %i!\n",
   844             EC_WARN("Acknowledge checking failed on slave %i!\n",
   843                     slave->ring_position);
   845                     slave->ring_position);
   844             return;
   846             return;
   845         }
   847         }
   846 
   848 
   847         end = get_cycles();
   849         end = get_cycles();
   848 
   850 
   849         if (likely(EC_READ_U8(command->data) == state)) {
   851         if (likely(EC_READ_U8(command->data) == state)) {
       
   852             slave->current_state = state;
   850             EC_INFO("Acknowleged state 0x%02X on slave %i.\n", state,
   853             EC_INFO("Acknowleged state 0x%02X on slave %i.\n", state,
   851                     slave->ring_position);
   854                     slave->ring_position);
   852             return;
   855             return;
   853         }
   856         }
   854 
   857 
   855         if (unlikely((end - start) >= timeout)) {
   858         if (unlikely((end - start) >= timeout)) {
       
   859             slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   856             EC_WARN("Failed to acknowledge state 0x%02X on slave %i"
   860             EC_WARN("Failed to acknowledge state 0x%02X on slave %i"
   857                     " - Timeout!\n", state, slave->ring_position);
   861                     " - Timeout!\n", state, slave->ring_position);
   858             return;
   862             return;
   859         }
   863         }
   860     }
   864     }
   910     ec_command_t *command;
   914     ec_command_t *command;
   911     cycles_t start, end, timeout;
   915     cycles_t start, end, timeout;
   912 
   916 
   913     command = &slave->master->simple_command;
   917     command = &slave->master->simple_command;
   914 
   918 
       
   919     slave->requested_state = state;
       
   920 
   915     if (ec_command_npwr(command, slave->station_address, 0x0120, 2)) return -1;
   921     if (ec_command_npwr(command, slave->station_address, 0x0120, 2)) return -1;
   916     EC_WRITE_U16(command->data, state);
   922     EC_WRITE_U16(command->data, state);
   917     if (unlikely(ec_master_simple_io(slave->master, command))) {
   923     if (unlikely(ec_master_simple_io(slave->master, command))) {
   918         EC_ERR("Failed to set state 0x%02X on slave %i!\n",
   924         EC_ERR("Failed to set state 0x%02X on slave %i!\n",
   919                state, slave->ring_position);
   925                state, slave->ring_position);
   928         udelay(100); // wait a little bit
   934         udelay(100); // wait a little bit
   929 
   935 
   930         if (ec_command_nprd(command, slave->station_address, 0x0130, 2))
   936         if (ec_command_nprd(command, slave->station_address, 0x0130, 2))
   931             return -1;
   937             return -1;
   932         if (unlikely(ec_master_simple_io(slave->master, command))) {
   938         if (unlikely(ec_master_simple_io(slave->master, command))) {
       
   939             slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   933             EC_ERR("Failed to check state 0x%02X on slave %i!\n",
   940             EC_ERR("Failed to check state 0x%02X on slave %i!\n",
   934                    state, slave->ring_position);
   941                    state, slave->ring_position);
   935             return -1;
   942             return -1;
   936         }
   943         }
   937 
   944 
   939 
   946 
   940         if (unlikely(EC_READ_U8(command->data) & 0x10)) { // state change error
   947         if (unlikely(EC_READ_U8(command->data) & 0x10)) { // state change error
   941             EC_ERR("Failed to set state 0x%02X - Slave %i refused state change"
   948             EC_ERR("Failed to set state 0x%02X - Slave %i refused state change"
   942                    " (code 0x%02X)!\n", state, slave->ring_position,
   949                    " (code 0x%02X)!\n", state, slave->ring_position,
   943                    EC_READ_U8(command->data));
   950                    EC_READ_U8(command->data));
   944             state = EC_READ_U8(command->data) & 0x0F;
   951             slave->current_state = EC_READ_U8(command->data);
       
   952             state = slave->current_state & 0x0F;
   945             ec_slave_read_al_status_code(slave);
   953             ec_slave_read_al_status_code(slave);
   946             ec_slave_state_ack(slave, state);
   954             ec_slave_state_ack(slave, state);
   947             return -1;
   955             return -1;
   948         }
   956         }
   949 
   957 
   950         if (likely(EC_READ_U8(command->data) == (state & 0x0F)))
   958         if (likely(EC_READ_U8(command->data) == (state & 0x0F))) {
       
   959             slave->current_state = state;
   951             return 0; // state change successful
   960             return 0; // state change successful
       
   961         }
   952 
   962 
   953         if (unlikely((end - start) >= timeout)) {
   963         if (unlikely((end - start) >= timeout)) {
       
   964             slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   954             EC_ERR("Failed to check state 0x%02X of slave %i - Timeout!\n",
   965             EC_ERR("Failed to check state 0x%02X of slave %i - Timeout!\n",
   955                    state, slave->ring_position);
   966                    state, slave->ring_position);
   956             return -1;
   967             return -1;
   957         }
   968         }
   958     }
   969     }
  1298     ec_slave_t *slave = container_of(kobj, ec_slave_t, kobj);
  1309     ec_slave_t *slave = container_of(kobj, ec_slave_t, kobj);
  1299 
  1310 
  1300     if (attr == &attr_state) {
  1311     if (attr == &attr_state) {
  1301         if (!strcmp(buffer, "INIT\n")) {
  1312         if (!strcmp(buffer, "INIT\n")) {
  1302             slave->requested_state = EC_SLAVE_STATE_INIT;
  1313             slave->requested_state = EC_SLAVE_STATE_INIT;
       
  1314             slave->state_error = 0;
  1303             return size;
  1315             return size;
  1304         }
  1316         }
  1305         else if (!strcmp(buffer, "PREOP\n")) {
  1317         else if (!strcmp(buffer, "PREOP\n")) {
  1306             slave->requested_state = EC_SLAVE_STATE_PREOP;
  1318             slave->requested_state = EC_SLAVE_STATE_PREOP;
       
  1319             slave->state_error = 0;
  1307             return size;
  1320             return size;
  1308         }
  1321         }
  1309         else if (!strcmp(buffer, "SAVEOP\n")) {
  1322         else if (!strcmp(buffer, "SAVEOP\n")) {
  1310             slave->requested_state = EC_SLAVE_STATE_SAVEOP;
  1323             slave->requested_state = EC_SLAVE_STATE_SAVEOP;
       
  1324             slave->state_error = 0;
  1311             return size;
  1325             return size;
  1312         }
  1326         }
  1313         else if (!strcmp(buffer, "OP\n")) {
  1327         else if (!strcmp(buffer, "OP\n")) {
  1314             slave->requested_state = EC_SLAVE_STATE_OP;
  1328             slave->requested_state = EC_SLAVE_STATE_OP;
       
  1329             slave->state_error = 0;
  1315             return size;
  1330             return size;
  1316         }
  1331         }
  1317 
  1332 
  1318         EC_ERR("Failed to set slave state!\n");
  1333         EC_ERR("Failed to set slave state!\n");
  1319     }
  1334     }