master/slave.c
changeset 269 a03be9a6fed6
parent 266 0234b0c26c52
child 275 ba51285d4ef6
equal deleted inserted replaced
268:4f9c149fb71f 269:a03be9a6fed6
    67 EC_SYSFS_READ_ATTR(product_name);
    67 EC_SYSFS_READ_ATTR(product_name);
    68 EC_SYSFS_READ_ATTR(product_desc);
    68 EC_SYSFS_READ_ATTR(product_desc);
    69 EC_SYSFS_READ_ATTR(sii_name);
    69 EC_SYSFS_READ_ATTR(sii_name);
    70 EC_SYSFS_READ_ATTR(type);
    70 EC_SYSFS_READ_ATTR(type);
    71 EC_SYSFS_READ_WRITE_ATTR(state);
    71 EC_SYSFS_READ_WRITE_ATTR(state);
    72 EC_SYSFS_READ_ATTR(eeprom);
    72 EC_SYSFS_READ_WRITE_ATTR(eeprom);
    73 
    73 
    74 static struct attribute *def_attrs[] = {
    74 static struct attribute *def_attrs[] = {
    75     &attr_ring_position,
    75     &attr_ring_position,
    76     &attr_coupler_address,
    76     &attr_coupler_address,
    77     &attr_vendor_name,
    77     &attr_vendor_name,
   155     slave->eeprom_name = NULL;
   155     slave->eeprom_name = NULL;
   156     slave->requested_state = EC_SLAVE_STATE_UNKNOWN;
   156     slave->requested_state = EC_SLAVE_STATE_UNKNOWN;
   157     slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   157     slave->current_state = EC_SLAVE_STATE_UNKNOWN;
   158     slave->state_error = 0;
   158     slave->state_error = 0;
   159     slave->online = 1;
   159     slave->online = 1;
       
   160     slave->new_eeprom_data = NULL;
       
   161     slave->new_eeprom_size = 0;
   160 
   162 
   161     ec_command_init(&slave->mbox_command);
   163     ec_command_init(&slave->mbox_command);
   162 
   164 
   163     INIT_LIST_HEAD(&slave->eeprom_strings);
   165     INIT_LIST_HEAD(&slave->eeprom_strings);
   164     INIT_LIST_HEAD(&slave->eeprom_syncs);
   166     INIT_LIST_HEAD(&slave->eeprom_syncs);
   237         }
   239         }
   238         kfree(sdo);
   240         kfree(sdo);
   239     }
   241     }
   240 
   242 
   241     if (slave->eeprom_data) kfree(slave->eeprom_data);
   243     if (slave->eeprom_data) kfree(slave->eeprom_data);
       
   244     if (slave->new_eeprom_data) kfree(slave->new_eeprom_data);
   242 
   245 
   243     ec_command_clear(&slave->mbox_command);
   246     ec_command_clear(&slave->mbox_command);
   244 }
   247 }
   245 
   248 
   246 /*****************************************************************************/
   249 /*****************************************************************************/
  1245 }
  1248 }
  1246 
  1249 
  1247 /*****************************************************************************/
  1250 /*****************************************************************************/
  1248 
  1251 
  1249 /**
  1252 /**
       
  1253    Schedules an EEPROM write operation.
       
  1254    \return 0 in case of success, else < 0
       
  1255 */
       
  1256 
       
  1257 ssize_t ec_slave_write_eeprom(ec_slave_t *slave, /**< EtherCAT slave */
       
  1258                               const uint8_t *data, /**< new EEPROM data */
       
  1259                               size_t size /**< size of data in bytes */
       
  1260                               )
       
  1261 {
       
  1262     uint16_t word_size, cat_type, cat_size;
       
  1263     const uint16_t *data_words, *next_header;
       
  1264     uint16_t *new_data;
       
  1265 
       
  1266     if (!slave->master->eeprom_write_enable) {
       
  1267         EC_ERR("Writing EEPROMs not allowed! Enable via"
       
  1268                " eeprom_write_enable SysFS entry.\n");
       
  1269         return -1;
       
  1270     }
       
  1271 
       
  1272     if (slave->master->mode != EC_MASTER_MODE_FREERUN) {
       
  1273         EC_ERR("Writing EEPROMs only allowed in freerun mode!\n");
       
  1274         return -1;
       
  1275     }
       
  1276 
       
  1277     if (slave->new_eeprom_data) {
       
  1278         EC_ERR("Slave %i already has a pending EEPROM write operation!\n",
       
  1279                slave->ring_position);
       
  1280         return -1;
       
  1281     }
       
  1282 
       
  1283     // coarse check of the data
       
  1284 
       
  1285     if (size % 2) {
       
  1286         EC_ERR("EEPROM size is odd! Dropping.\n");
       
  1287         return -1;
       
  1288     }
       
  1289 
       
  1290     data_words = (const uint16_t *) data;
       
  1291     word_size = size / 2;
       
  1292 
       
  1293     if (word_size < 0x0041) {
       
  1294         EC_ERR("EEPROM data too short! Dropping.\n");
       
  1295         return -1;
       
  1296     }
       
  1297 
       
  1298     next_header = data_words + 0x0040;
       
  1299     cat_type = EC_READ_U16(next_header);
       
  1300     while (cat_type != 0xFFFF) {
       
  1301         cat_type = EC_READ_U16(next_header);
       
  1302         cat_size = EC_READ_U16(next_header + 1);
       
  1303         if ((next_header + cat_size + 2) - data_words >= word_size) {
       
  1304             EC_ERR("EEPROM data seems to be corrupted! Dropping.\n");
       
  1305             return -1;
       
  1306         }
       
  1307         next_header += cat_size + 2;
       
  1308         cat_type = EC_READ_U16(next_header);
       
  1309     }
       
  1310 
       
  1311     // data ok!
       
  1312 
       
  1313     if (!(new_data = (uint16_t *) kmalloc(word_size * 2, GFP_KERNEL))) {
       
  1314         EC_ERR("Unable to allocate memory for new EEPROM data!\n");
       
  1315         return -1;
       
  1316     }
       
  1317     memcpy(new_data, data, size);
       
  1318 
       
  1319     slave->new_eeprom_size = word_size;
       
  1320     slave->new_eeprom_data = new_data;
       
  1321 
       
  1322     EC_INFO("EEPROM writing scheduled for slave %i, %i words.\n",
       
  1323             slave->ring_position, word_size);
       
  1324     return 0;
       
  1325 }
       
  1326 
       
  1327 /*****************************************************************************/
       
  1328 
       
  1329 /**
  1250    Formats attribute data for SysFS read access.
  1330    Formats attribute data for SysFS read access.
  1251    \return number of bytes to read
  1331    \return number of bytes to read
  1252 */
  1332 */
  1253 
  1333 
  1254 ssize_t ec_show_slave_attribute(struct kobject *kobj, /**< slave's kobject */
  1334 ssize_t ec_show_slave_attribute(struct kobject *kobj, /**< slave's kobject */
  1357             return size;
  1437             return size;
  1358         }
  1438         }
  1359 
  1439 
  1360         EC_ERR("Failed to set slave state!\n");
  1440         EC_ERR("Failed to set slave state!\n");
  1361     }
  1441     }
       
  1442     else if (attr == &attr_eeprom) {
       
  1443         if (!ec_slave_write_eeprom(slave, buffer, size))
       
  1444             return size;
       
  1445     }
  1362 
  1446 
  1363     return -EINVAL;
  1447     return -EINVAL;
  1364 }
  1448 }
  1365 
  1449 
  1366 /******************************************************************************
  1450 /******************************************************************************