Commit 345bd7cf authored by Dimitris Lampridis's avatar Dimitris Lampridis

sw: rework eeprom programming

The new scheme also work for empty EEPROMs, as they come from the
factory after production.

We basically ignore everything and erase the eeprom and update all
fields.

We also correctly setup the drivers so that when the attenuator is
powered up, all four channels are correctly initialised at -40dB.
parent e58527a0
......@@ -55,40 +55,56 @@ int oau_factory_program_eeprom(unsigned int bus, unsigned int dev, char *serial)
ret = ftdi_usb_open_string(ctx, devbus);
if (ret != 0) {
fprintf(stderr, "cannot open usb device %s\n", devbus);
return -EINVAL;
return -ENODEV;
}
b = libusb_get_bus_number(libusb_get_device(ctx->usb_dev));
d = libusb_get_port_number(libusb_get_device(ctx->usb_dev));
// Read and backup any existing contents
ret = ftdi_read_eeprom(ctx);
if (ret != 0) {
fprintf(stderr, "could not read eeprom of %03x:%03x\n", b, d);
return -ENOENT;
}
ret = ftdi_eeprom_decode(ctx, 1);
if (ret != 0) {
fprintf(stderr, "error %d decoding eeprom of %03x:%03x\n", b, d);
return -ENOENT;
}
ret = ftdi_get_eeprom_buf(ctx, eeprom_raw, sizeof(eeprom_raw));
if (ret != 0) {
fprintf(stderr, "could not read raw eeprom of %03x:%03x\n", b, d);
return -EINVAL;
return -ENOENT;
}
eeprom_backup(eeprom_raw, sizeof(eeprom_raw));
// Ignore all existing data and start from scratch, setting all necessary fields.
// Erasing is also necessary to properly detect the size of the attached EEPROM.
ret = ftdi_eeprom_initdefaults(ctx, NULL, NULL, NULL);
if (ret != 0) {
fprintf(stderr, "could not init eeprom of %03x:%03x\n", b, d);
return -EINVAL;
}
ret = ftdi_erase_eeprom(ctx);
if (ret != 0) {
fprintf(stderr, "could not erase eeprom of %03x:%03x\n", b, d);
return -EIO;
}
snprintf(ser, sizeof(ser), serial);
ret = 0;
ret = ret || ftdi_eeprom_set_strings(ctx, OATTNUSB_MANUFACTURER, OATTNUSB_PRODUCT, ser);
ret = ret || ftdi_set_eeprom_value(ctx, VENDOR_ID, OATTNUSB_VENDOR_ID);
ret = ret || ftdi_set_eeprom_value(ctx, PRODUCT_ID, OATTNUSB_DEVICE_ID);
ret = ret || ftdi_set_eeprom_value(ctx, CHANNEL_A_TYPE, CHANNEL_IS_FIFO);
ret = ret || ftdi_set_eeprom_value(ctx, CHANNEL_B_TYPE, CHANNEL_IS_FIFO);
ret = ret || ftdi_set_eeprom_value(ctx, CHANNEL_A_DRIVER, DRIVER_VCP);
ret = ret || ftdi_set_eeprom_value(ctx, CHANNEL_B_DRIVER, DRIVER_VCP);
// Try to rebuild the binary buffer, back it up and check that it is valid
ret = ret || (ftdi_eeprom_build(ctx) < 0);
eeprom_backup(eeprom_raw, sizeof(eeprom_raw));
ret = ret || ftdi_eeprom_decode(ctx, 1);
if (ret != 0) {
fprintf(stderr, "error building new EEPROM, cowardly refusing to write it\n");
return -EINVAL;
}
ret = ftdi_write_eeprom(ctx);
if (ret == 0) {
fprintf(stderr, "successful EEPROM write, resetting USB device\n");
......@@ -97,6 +113,7 @@ int oau_factory_program_eeprom(unsigned int bus, unsigned int dev, char *serial)
fprintf(stderr, "EEPROM write error, ftdi_write_eeprom returned %d\n", ret);
return -EIO;
}
ftdi_free(ctx);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment