1 /***************************************************************************
5 copyright : (C) 2003 by Intra2net AG
6 email : opensource@intra2net.com
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
15 ***************************************************************************/
22 #define ftdi_error_return(code, str) do { \
23 ftdi->error_str = str; \
30 Initializes a ftdi_context.
34 -1: Couldn't allocate read buffer
36 int ftdi_init(struct ftdi_context *ftdi)
39 ftdi->usb_read_timeout = 5000;
40 ftdi->usb_write_timeout = 5000;
42 ftdi->type = TYPE_BM; /* chip type */
44 ftdi->bitbang_enabled = 0;
46 ftdi->readbuffer = NULL;
47 ftdi->readbuffer_offset = 0;
48 ftdi->readbuffer_remaining = 0;
49 ftdi->writebuffer_chunksize = 4096;
55 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
57 ftdi->error_str = NULL;
59 /* All fine. Now allocate the readbuffer */
60 return ftdi_read_data_set_chunksize(ftdi, 4096);
67 Open selected channels on a chip, otherwise use first channel
71 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
76 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
80 ftdi->index = INTERFACE_B;
85 ftdi_error_return(-1, "Unknown interface");
92 Deinitializes a ftdi_context.
94 void ftdi_deinit(struct ftdi_context *ftdi)
96 if (ftdi->readbuffer != NULL) {
97 free(ftdi->readbuffer);
98 ftdi->readbuffer = NULL;
104 Use an already open device.
106 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
114 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
115 needs to be deallocated by ftdi_list_free after use.
118 >0: number of devices found
119 -1: usb_find_busses() failed
120 -2: usb_find_devices() failed
123 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
125 struct ftdi_device_list **curdev;
127 struct usb_device *dev;
131 if (usb_find_busses() < 0)
132 ftdi_error_return(-1, "usb_find_busses() failed");
133 if (usb_find_devices() < 0)
134 ftdi_error_return(-2, "usb_find_devices() failed");
137 for (bus = usb_busses; bus; bus = bus->next) {
138 for (dev = bus->devices; dev; dev = dev->next) {
139 if (dev->descriptor.idVendor == vendor
140 && dev->descriptor.idProduct == product)
142 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
144 ftdi_error_return(-3, "out of memory");
146 (*curdev)->next = NULL;
147 (*curdev)->dev = dev;
149 curdev = &(*curdev)->next;
160 Frees a created device list.
162 void ftdi_list_free(struct ftdi_device_list **devlist)
164 struct ftdi_device_list **curdev;
165 for (; *devlist == NULL; devlist = curdev) {
166 curdev = &(*devlist)->next;
175 Opens a ftdi device given by a usb_device.
179 -4: unable to open device
180 -5: unable to claim device
182 -7: set baudrate failed
184 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
186 if (!(ftdi->usb_dev = usb_open(dev)))
187 ftdi_error_return(-4, "usb_open() failed");
189 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
190 usb_close (ftdi->usb_dev);
191 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
194 if (ftdi_usb_reset (ftdi) != 0) {
195 usb_close (ftdi->usb_dev);
196 ftdi_error_return(-6, "ftdi_usb_reset failed");
199 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
200 usb_close (ftdi->usb_dev);
201 ftdi_error_return(-7, "set baudrate failed");
204 // Try to guess chip type
205 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
206 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
207 && dev->descriptor.iSerialNumber == 0))
208 ftdi->type = TYPE_BM;
209 else if (dev->descriptor.bcdDevice == 0x200)
210 ftdi->type = TYPE_AM;
211 else if (dev->descriptor.bcdDevice == 0x500) {
212 ftdi->type = TYPE_2232C;
214 ftdi->index = INTERFACE_A;
217 ftdi_error_return(0, "all fine");
222 Opens the first device with a given vendor and product ids.
225 See ftdi_usb_open_desc()
227 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
229 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
232 /* ftdi_usb_open_desc
234 Opens the first device with a given, vendor id, product id,
235 description and serial.
239 -1: usb_find_busses() failed
240 -2: usb_find_devices() failed
241 -3: usb device not found
242 -4: unable to open device
243 -5: unable to claim device
245 -7: set baudrate failed
246 -8: get product description failed
247 -9: get serial number failed
248 -10: unable to close device
250 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
251 const char* description, const char* serial)
254 struct usb_device *dev;
259 if (usb_find_busses() < 0)
260 ftdi_error_return(-1, "usb_find_busses() failed");
261 if (usb_find_devices() < 0)
262 ftdi_error_return(-2, "usb_find_devices() failed");
264 for (bus = usb_busses; bus; bus = bus->next) {
265 for (dev = bus->devices; dev; dev = dev->next) {
266 if (dev->descriptor.idVendor == vendor
267 && dev->descriptor.idProduct == product) {
268 if (!(ftdi->usb_dev = usb_open(dev)))
269 ftdi_error_return(-4, "usb_open() failed");
271 if (description != NULL) {
272 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
273 usb_close (ftdi->usb_dev);
274 ftdi_error_return(-8, "unable to fetch product description");
276 if (strncmp(string, description, sizeof(string)) != 0) {
277 if (usb_close (ftdi->usb_dev) != 0)
278 ftdi_error_return(-10, "unable to close device");
282 if (serial != NULL) {
283 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
284 usb_close (ftdi->usb_dev);
285 ftdi_error_return(-9, "unable to fetch serial number");
287 if (strncmp(string, serial, sizeof(string)) != 0) {
288 if (usb_close (ftdi->usb_dev) != 0)
289 ftdi_error_return(-10, "unable to close device");
294 if (usb_close (ftdi->usb_dev) != 0)
295 ftdi_error_return(-10, "unable to close device");
297 return ftdi_usb_open_dev(ftdi, dev);
303 ftdi_error_return(-3, "device not found");
308 Resets the ftdi device.
312 -1: FTDI reset failed
314 int ftdi_usb_reset(struct ftdi_context *ftdi)
316 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
317 ftdi_error_return(-1,"FTDI reset failed");
319 // Invalidate data in the readbuffer
320 ftdi->readbuffer_offset = 0;
321 ftdi->readbuffer_remaining = 0;
326 /* ftdi_usb_purge_buffers
328 Cleans the buffers of the ftdi device.
332 -1: write buffer purge failed
333 -2: read buffer purge failed
335 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
337 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
338 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
340 // Invalidate data in the readbuffer
341 ftdi->readbuffer_offset = 0;
342 ftdi->readbuffer_remaining = 0;
344 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
345 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
352 Closes the ftdi device.
356 -1: usb_release failed
359 int ftdi_usb_close(struct ftdi_context *ftdi)
363 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
366 if (usb_close (ftdi->usb_dev) != 0)
374 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
375 Function is only used internally
377 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
378 unsigned short *value, unsigned short *index)
380 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
381 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
382 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
383 int divisor, best_divisor, best_baud, best_baud_diff;
384 unsigned long encoded_divisor;
392 divisor = 24000000 / baudrate;
394 if (ftdi->type == TYPE_AM) {
395 // Round down to supported fraction (AM only)
396 divisor -= am_adjust_dn[divisor & 7];
399 // Try this divisor and the one above it (because division rounds down)
403 for (i = 0; i < 2; i++) {
404 int try_divisor = divisor + i;
408 // Round up to supported divisor value
409 if (try_divisor <= 8) {
410 // Round up to minimum supported divisor
412 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
413 // BM doesn't support divisors 9 through 11 inclusive
415 } else if (divisor < 16) {
416 // AM doesn't support divisors 9 through 15 inclusive
419 if (ftdi->type == TYPE_AM) {
420 // Round up to supported fraction (AM only)
421 try_divisor += am_adjust_up[try_divisor & 7];
422 if (try_divisor > 0x1FFF8) {
423 // Round down to maximum supported divisor value (for AM)
424 try_divisor = 0x1FFF8;
427 if (try_divisor > 0x1FFFF) {
428 // Round down to maximum supported divisor value (for BM)
429 try_divisor = 0x1FFFF;
433 // Get estimated baud rate (to nearest integer)
434 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
435 // Get absolute difference from requested baud rate
436 if (baud_estimate < baudrate) {
437 baud_diff = baudrate - baud_estimate;
439 baud_diff = baud_estimate - baudrate;
441 if (i == 0 || baud_diff < best_baud_diff) {
442 // Closest to requested baud rate so far
443 best_divisor = try_divisor;
444 best_baud = baud_estimate;
445 best_baud_diff = baud_diff;
446 if (baud_diff == 0) {
447 // Spot on! No point trying
452 // Encode the best divisor value
453 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
454 // Deal with special cases for encoded value
455 if (encoded_divisor == 1) {
456 encoded_divisor = 0; // 3000000 baud
457 } else if (encoded_divisor == 0x4001) {
458 encoded_divisor = 1; // 2000000 baud (BM only)
460 // Split into "value" and "index" values
461 *value = (unsigned short)(encoded_divisor & 0xFFFF);
462 if(ftdi->type == TYPE_2232C) {
463 *index = (unsigned short)(encoded_divisor >> 8);
465 *index |= ftdi->interface;
468 *index = (unsigned short)(encoded_divisor >> 16);
470 // Return the nearest baud rate
477 Sets the chip baudrate
482 -2: setting baudrate failed
484 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
486 unsigned short value, index;
489 if (ftdi->bitbang_enabled) {
490 baudrate = baudrate*4;
493 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
494 if (actual_baudrate <= 0)
495 ftdi_error_return (-1, "Silly baudrate <= 0.");
497 // Check within tolerance (about 5%)
498 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
499 || ((actual_baudrate < baudrate)
500 ? (actual_baudrate * 21 < baudrate * 20)
501 : (baudrate * 21 < actual_baudrate * 20)))
502 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
504 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
505 ftdi_error_return (-2, "Setting new baudrate failed");
507 ftdi->baudrate = baudrate;
512 ftdi_set_line_property
514 set (RS232) line characteristics by Alain Abbas
518 -1: Setting line property failed
520 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
521 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
523 unsigned short value = bits;
527 value |= (0x00 << 8);
530 value |= (0x01 << 8);
533 value |= (0x02 << 8);
536 value |= (0x03 << 8);
539 value |= (0x04 << 8);
545 value |= (0x00 << 11);
548 value |= (0x01 << 11);
551 value |= (0x02 << 11);
555 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
556 ftdi_error_return (-1, "Setting new line property failed");
561 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
565 int total_written = 0;
567 while (offset < size) {
568 int write_size = ftdi->writebuffer_chunksize;
570 if (offset+write_size > size)
571 write_size = size-offset;
573 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
575 ftdi_error_return(ret, "usb bulk write failed");
577 total_written += ret;
578 offset += write_size;
581 return total_written;
585 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
587 ftdi->writebuffer_chunksize = chunksize;
592 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
594 *chunksize = ftdi->writebuffer_chunksize;
599 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
601 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
603 // everything we want is still in the readbuffer?
604 if (size <= ftdi->readbuffer_remaining) {
605 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
608 ftdi->readbuffer_remaining -= size;
609 ftdi->readbuffer_offset += size;
611 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
615 // something still in the readbuffer, but not enough to satisfy 'size'?
616 if (ftdi->readbuffer_remaining != 0) {
617 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
620 offset += ftdi->readbuffer_remaining;
622 // do the actual USB read
623 while (offset < size && ret > 0) {
624 ftdi->readbuffer_remaining = 0;
625 ftdi->readbuffer_offset = 0;
626 /* returns how much received */
627 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
629 ftdi_error_return(ret, "usb bulk read failed");
632 // skip FTDI status bytes.
633 // Maybe stored in the future to enable modem use
634 num_of_chunks = ret / 64;
635 chunk_remains = ret % 64;
636 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
638 ftdi->readbuffer_offset += 2;
642 for (i = 1; i < num_of_chunks; i++)
643 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
644 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
646 if (chunk_remains > 2) {
647 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
648 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
650 ret -= 2*num_of_chunks;
652 ret -= 2*(num_of_chunks-1)+chunk_remains;
654 } else if (ret <= 2) {
655 // no more data to read?
659 // data still fits in buf?
660 if (offset+ret <= size) {
661 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
662 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
665 /* Did we read exactly the right amount of bytes? */
667 //printf("read_data exact rem %d offset %d\n",
668 //ftdi->readbuffer_remaining, offset);
671 // only copy part of the data or size <= readbuffer_chunksize
672 int part_size = size-offset;
673 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
675 ftdi->readbuffer_offset += part_size;
676 ftdi->readbuffer_remaining = ret-part_size;
679 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
680 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
691 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
693 unsigned char *new_buf;
695 // Invalidate all remaining data
696 ftdi->readbuffer_offset = 0;
697 ftdi->readbuffer_remaining = 0;
699 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
700 ftdi_error_return(-1, "out of memory for readbuffer");
702 ftdi->readbuffer = new_buf;
703 ftdi->readbuffer_chunksize = chunksize;
709 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
711 *chunksize = ftdi->readbuffer_chunksize;
717 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
719 unsigned short usb_val;
721 usb_val = bitmask; // low byte: bitmask
722 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
723 usb_val |= (ftdi->bitbang_mode << 8);
725 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
726 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
728 ftdi->bitbang_enabled = 1;
733 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
735 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
736 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
738 ftdi->bitbang_enabled = 0;
743 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
745 unsigned short usb_val;
747 usb_val = bitmask; // low byte: bitmask
748 usb_val |= (mode << 8);
749 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
750 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
752 ftdi->bitbang_mode = mode;
753 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
757 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
759 unsigned short usb_val;
760 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
761 ftdi_error_return(-1, "read pins failed");
763 *pins = (unsigned char)usb_val;
768 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
770 unsigned short usb_val;
773 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
776 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
777 ftdi_error_return(-2, "unable to set latency timer");
783 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
785 unsigned short usb_val;
786 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
787 ftdi_error_return(-1, "reading latency timer failed");
789 *latency = (unsigned char)usb_val;
794 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
796 eeprom->vendor_id = 0x0403;
797 eeprom->product_id = 0x6001;
799 eeprom->self_powered = 1;
800 eeprom->remote_wakeup = 1;
801 eeprom->BM_type_chip = 1;
803 eeprom->in_is_isochronous = 0;
804 eeprom->out_is_isochronous = 0;
805 eeprom->suspend_pull_downs = 0;
807 eeprom->use_serial = 0;
808 eeprom->change_usb_version = 0;
809 eeprom->usb_version = 0x0200;
810 eeprom->max_power = 0;
812 eeprom->manufacturer = NULL;
813 eeprom->product = NULL;
814 eeprom->serial = NULL;
821 Build binary output from ftdi_eeprom structure.
822 Output is suitable for ftdi_write_eeprom.
825 positive value: used eeprom size
826 -1: eeprom size (128 bytes) exceeded by custom strings
828 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
831 unsigned short checksum, value;
832 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
835 if (eeprom->manufacturer != NULL)
836 manufacturer_size = strlen(eeprom->manufacturer);
837 if (eeprom->product != NULL)
838 product_size = strlen(eeprom->product);
839 if (eeprom->serial != NULL)
840 serial_size = strlen(eeprom->serial);
842 size_check = 128; // eeprom is 128 bytes
843 size_check -= 28; // 28 are always in use (fixed)
844 size_check -= manufacturer_size*2;
845 size_check -= product_size*2;
846 size_check -= serial_size*2;
848 // eeprom size exceeded?
853 memset (output, 0, 128);
855 // Addr 00: Stay 00 00
856 // Addr 02: Vendor ID
857 output[0x02] = eeprom->vendor_id;
858 output[0x03] = eeprom->vendor_id >> 8;
860 // Addr 04: Product ID
861 output[0x04] = eeprom->product_id;
862 output[0x05] = eeprom->product_id >> 8;
864 // Addr 06: Device release number (0400h for BM features)
867 if (eeprom->BM_type_chip == 1)
872 // Addr 08: Config descriptor
873 // Bit 1: remote wakeup if 1
874 // Bit 0: self powered if 1
877 if (eeprom->self_powered == 1)
879 if (eeprom->remote_wakeup == 1)
883 // Addr 09: Max power consumption: max power = value * 2 mA
884 output[0x09] = eeprom->max_power;
887 // Addr 0A: Chip configuration
888 // Bit 7: 0 - reserved
889 // Bit 6: 0 - reserved
890 // Bit 5: 0 - reserved
891 // Bit 4: 1 - Change USB version
892 // Bit 3: 1 - Use the serial number string
893 // Bit 2: 1 - Enable suspend pull downs for lower power
894 // Bit 1: 1 - Out EndPoint is Isochronous
895 // Bit 0: 1 - In EndPoint is Isochronous
898 if (eeprom->in_is_isochronous == 1)
900 if (eeprom->out_is_isochronous == 1)
902 if (eeprom->suspend_pull_downs == 1)
904 if (eeprom->use_serial == 1)
906 if (eeprom->change_usb_version == 1)
913 // Addr 0C: USB version low byte when 0x0A bit 4 is set
914 // Addr 0D: USB version high byte when 0x0A bit 4 is set
915 if (eeprom->change_usb_version == 1) {
916 output[0x0C] = eeprom->usb_version;
917 output[0x0D] = eeprom->usb_version >> 8;
921 // Addr 0E: Offset of the manufacturer string + 0x80
922 output[0x0E] = 0x14 + 0x80;
924 // Addr 0F: Length of manufacturer string
925 output[0x0F] = manufacturer_size*2 + 2;
927 // Addr 10: Offset of the product string + 0x80, calculated later
928 // Addr 11: Length of product string
929 output[0x11] = product_size*2 + 2;
931 // Addr 12: Offset of the serial string + 0x80, calculated later
932 // Addr 13: Length of serial string
933 output[0x13] = serial_size*2 + 2;
936 output[0x14] = manufacturer_size*2 + 2;
937 output[0x15] = 0x03; // type: string
941 // Output manufacturer
942 for (j = 0; j < manufacturer_size; j++) {
943 output[i] = eeprom->manufacturer[j], i++;
944 output[i] = 0x00, i++;
947 // Output product name
948 output[0x10] = i + 0x80; // calculate offset
949 output[i] = product_size*2 + 2, i++;
950 output[i] = 0x03, i++;
951 for (j = 0; j < product_size; j++) {
952 output[i] = eeprom->product[j], i++;
953 output[i] = 0x00, i++;
957 output[0x12] = i + 0x80; // calculate offset
958 output[i] = serial_size*2 + 2, i++;
959 output[i] = 0x03, i++;
960 for (j = 0; j < serial_size; j++) {
961 output[i] = eeprom->serial[j], i++;
962 output[i] = 0x00, i++;
965 // calculate checksum
968 for (i = 0; i < 63; i++) {
970 value += output[(i*2)+1] << 8;
972 checksum = value^checksum;
973 checksum = (checksum << 1) | (checksum >> 15);
976 output[0x7E] = checksum;
977 output[0x7F] = checksum >> 8;
983 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
987 for (i = 0; i < 64; i++) {
988 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
989 ftdi_error_return(-1, "reading eeprom failed");
996 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
998 unsigned short usb_val;
1001 for (i = 0; i < 64; i++) {
1002 usb_val = eeprom[i*2];
1003 usb_val += eeprom[(i*2)+1] << 8;
1004 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1005 ftdi_error_return(-1, "unable to write eeprom");
1012 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1014 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1015 ftdi_error_return(-1, "unable to erase eeprom");
1021 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1023 return ftdi->error_str;