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 ***************************************************************************/
23 #define ftdi_error_return(code, str) do { \
24 ftdi->error_str = str; \
31 Initializes a ftdi_context.
35 -1: Couldn't allocate read buffer
37 int ftdi_init(struct ftdi_context *ftdi)
40 ftdi->usb_read_timeout = 5000;
41 ftdi->usb_write_timeout = 5000;
43 ftdi->type = TYPE_BM; /* chip type */
45 ftdi->bitbang_enabled = 0;
47 ftdi->readbuffer = NULL;
48 ftdi->readbuffer_offset = 0;
49 ftdi->readbuffer_remaining = 0;
50 ftdi->writebuffer_chunksize = 4096;
56 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
58 ftdi->error_str = NULL;
60 /* All fine. Now allocate the readbuffer */
61 return ftdi_read_data_set_chunksize(ftdi, 4096);
68 Open selected channels on a chip, otherwise use first channel
72 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
77 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
81 ftdi->index = INTERFACE_B;
86 ftdi_error_return(-1, "Unknown interface");
93 Deinitializes a ftdi_context.
95 void ftdi_deinit(struct ftdi_context *ftdi)
97 if (ftdi->readbuffer != NULL) {
98 free(ftdi->readbuffer);
99 ftdi->readbuffer = NULL;
105 Use an already open device.
107 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
115 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
116 needs to be deallocated by ftdi_list_free after use.
119 >0: number of devices found
120 -1: usb_find_busses() failed
121 -2: usb_find_devices() failed
124 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
126 struct ftdi_device_list **curdev;
128 struct usb_device *dev;
132 if (usb_find_busses() < 0)
133 ftdi_error_return(-1, "usb_find_busses() failed");
134 if (usb_find_devices() < 0)
135 ftdi_error_return(-2, "usb_find_devices() failed");
138 for (bus = usb_busses; bus; bus = bus->next) {
139 for (dev = bus->devices; dev; dev = dev->next) {
140 if (dev->descriptor.idVendor == vendor
141 && dev->descriptor.idProduct == product)
143 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
145 ftdi_error_return(-3, "out of memory");
147 (*curdev)->next = NULL;
148 (*curdev)->dev = dev;
150 curdev = &(*curdev)->next;
161 Frees a created device list.
163 void ftdi_list_free(struct ftdi_device_list **devlist)
165 struct ftdi_device_list **curdev;
166 for (; *devlist == NULL; devlist = curdev) {
167 curdev = &(*devlist)->next;
176 Opens a ftdi device given by a usb_device.
180 -4: unable to open device
181 -5: unable to claim device
183 -7: set baudrate failed
185 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
187 int detach_errno = 0;
188 if (!(ftdi->usb_dev = usb_open(dev)))
189 ftdi_error_return(-4, "usb_open() failed");
191 #ifdef LIBUSB_HAS_GET_DRIVER_NP
192 // Try to detach ftdi_sio kernel module
193 // Returns ENODATA if driver is not loaded
194 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
195 detach_errno = errno;
198 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
199 usb_close (ftdi->usb_dev);
200 if (detach_errno == EPERM) {
201 ftdi_error_return(-8, "inappropriate permissions on device!");
203 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
207 if (ftdi_usb_reset (ftdi) != 0) {
208 usb_close (ftdi->usb_dev);
209 ftdi_error_return(-6, "ftdi_usb_reset failed");
212 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
213 usb_close (ftdi->usb_dev);
214 ftdi_error_return(-7, "set baudrate failed");
217 // Try to guess chip type
218 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
219 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
220 && dev->descriptor.iSerialNumber == 0))
221 ftdi->type = TYPE_BM;
222 else if (dev->descriptor.bcdDevice == 0x200)
223 ftdi->type = TYPE_AM;
224 else if (dev->descriptor.bcdDevice == 0x500) {
225 ftdi->type = TYPE_2232C;
227 ftdi->index = INTERFACE_A;
230 ftdi_error_return(0, "all fine");
235 Opens the first device with a given vendor and product ids.
238 See ftdi_usb_open_desc()
240 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
242 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
245 /* ftdi_usb_open_desc
247 Opens the first device with a given, vendor id, product id,
248 description and serial.
252 -1: usb_find_busses() failed
253 -2: usb_find_devices() failed
254 -3: usb device not found
255 -4: unable to open device
256 -5: unable to claim device
258 -7: set baudrate failed
259 -8: get product description failed
260 -9: get serial number failed
261 -10: unable to close device
263 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
264 const char* description, const char* serial)
267 struct usb_device *dev;
272 if (usb_find_busses() < 0)
273 ftdi_error_return(-1, "usb_find_busses() failed");
274 if (usb_find_devices() < 0)
275 ftdi_error_return(-2, "usb_find_devices() failed");
277 for (bus = usb_busses; bus; bus = bus->next) {
278 for (dev = bus->devices; dev; dev = dev->next) {
279 if (dev->descriptor.idVendor == vendor
280 && dev->descriptor.idProduct == product) {
281 if (!(ftdi->usb_dev = usb_open(dev)))
282 ftdi_error_return(-4, "usb_open() failed");
284 if (description != NULL) {
285 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
286 usb_close (ftdi->usb_dev);
287 ftdi_error_return(-8, "unable to fetch product description");
289 if (strncmp(string, description, sizeof(string)) != 0) {
290 if (usb_close (ftdi->usb_dev) != 0)
291 ftdi_error_return(-10, "unable to close device");
295 if (serial != NULL) {
296 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
297 usb_close (ftdi->usb_dev);
298 ftdi_error_return(-9, "unable to fetch serial number");
300 if (strncmp(string, serial, sizeof(string)) != 0) {
301 if (usb_close (ftdi->usb_dev) != 0)
302 ftdi_error_return(-10, "unable to close device");
307 if (usb_close (ftdi->usb_dev) != 0)
308 ftdi_error_return(-10, "unable to close device");
310 return ftdi_usb_open_dev(ftdi, dev);
316 ftdi_error_return(-3, "device not found");
321 Resets the ftdi device.
325 -1: FTDI reset failed
327 int ftdi_usb_reset(struct ftdi_context *ftdi)
329 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
330 ftdi_error_return(-1,"FTDI reset failed");
332 // Invalidate data in the readbuffer
333 ftdi->readbuffer_offset = 0;
334 ftdi->readbuffer_remaining = 0;
339 /* ftdi_usb_purge_buffers
341 Cleans the buffers of the ftdi device.
345 -1: write buffer purge failed
346 -2: read buffer purge failed
348 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
350 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
351 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
353 // Invalidate data in the readbuffer
354 ftdi->readbuffer_offset = 0;
355 ftdi->readbuffer_remaining = 0;
357 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
358 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
365 Closes the ftdi device.
369 -1: usb_release failed
372 int ftdi_usb_close(struct ftdi_context *ftdi)
376 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
379 if (usb_close (ftdi->usb_dev) != 0)
387 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
388 Function is only used internally
390 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
391 unsigned short *value, unsigned short *index)
393 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
394 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
395 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
396 int divisor, best_divisor, best_baud, best_baud_diff;
397 unsigned long encoded_divisor;
405 divisor = 24000000 / baudrate;
407 if (ftdi->type == TYPE_AM) {
408 // Round down to supported fraction (AM only)
409 divisor -= am_adjust_dn[divisor & 7];
412 // Try this divisor and the one above it (because division rounds down)
416 for (i = 0; i < 2; i++) {
417 int try_divisor = divisor + i;
421 // Round up to supported divisor value
422 if (try_divisor <= 8) {
423 // Round up to minimum supported divisor
425 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
426 // BM doesn't support divisors 9 through 11 inclusive
428 } else if (divisor < 16) {
429 // AM doesn't support divisors 9 through 15 inclusive
432 if (ftdi->type == TYPE_AM) {
433 // Round up to supported fraction (AM only)
434 try_divisor += am_adjust_up[try_divisor & 7];
435 if (try_divisor > 0x1FFF8) {
436 // Round down to maximum supported divisor value (for AM)
437 try_divisor = 0x1FFF8;
440 if (try_divisor > 0x1FFFF) {
441 // Round down to maximum supported divisor value (for BM)
442 try_divisor = 0x1FFFF;
446 // Get estimated baud rate (to nearest integer)
447 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
448 // Get absolute difference from requested baud rate
449 if (baud_estimate < baudrate) {
450 baud_diff = baudrate - baud_estimate;
452 baud_diff = baud_estimate - baudrate;
454 if (i == 0 || baud_diff < best_baud_diff) {
455 // Closest to requested baud rate so far
456 best_divisor = try_divisor;
457 best_baud = baud_estimate;
458 best_baud_diff = baud_diff;
459 if (baud_diff == 0) {
460 // Spot on! No point trying
465 // Encode the best divisor value
466 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
467 // Deal with special cases for encoded value
468 if (encoded_divisor == 1) {
469 encoded_divisor = 0; // 3000000 baud
470 } else if (encoded_divisor == 0x4001) {
471 encoded_divisor = 1; // 2000000 baud (BM only)
473 // Split into "value" and "index" values
474 *value = (unsigned short)(encoded_divisor & 0xFFFF);
475 if(ftdi->type == TYPE_2232C) {
476 *index = (unsigned short)(encoded_divisor >> 8);
478 *index |= ftdi->index;
481 *index = (unsigned short)(encoded_divisor >> 16);
483 // Return the nearest baud rate
490 Sets the chip baudrate
495 -2: setting baudrate failed
497 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
499 unsigned short value, index;
502 if (ftdi->bitbang_enabled) {
503 baudrate = baudrate*4;
506 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
507 if (actual_baudrate <= 0)
508 ftdi_error_return (-1, "Silly baudrate <= 0.");
510 // Check within tolerance (about 5%)
511 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
512 || ((actual_baudrate < baudrate)
513 ? (actual_baudrate * 21 < baudrate * 20)
514 : (baudrate * 21 < actual_baudrate * 20)))
515 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
517 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
518 ftdi_error_return (-2, "Setting new baudrate failed");
520 ftdi->baudrate = baudrate;
525 ftdi_set_line_property
527 set (RS232) line characteristics by Alain Abbas
531 -1: Setting line property failed
533 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
534 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
536 unsigned short value = bits;
540 value |= (0x00 << 8);
543 value |= (0x01 << 8);
546 value |= (0x02 << 8);
549 value |= (0x03 << 8);
552 value |= (0x04 << 8);
558 value |= (0x00 << 11);
561 value |= (0x01 << 11);
564 value |= (0x02 << 11);
568 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
569 ftdi_error_return (-1, "Setting new line property failed");
574 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
578 int total_written = 0;
580 while (offset < size) {
581 int write_size = ftdi->writebuffer_chunksize;
583 if (offset+write_size > size)
584 write_size = size-offset;
586 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
588 ftdi_error_return(ret, "usb bulk write failed");
590 total_written += ret;
591 offset += write_size;
594 return total_written;
598 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
600 ftdi->writebuffer_chunksize = chunksize;
605 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
607 *chunksize = ftdi->writebuffer_chunksize;
612 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
614 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
616 // everything we want is still in the readbuffer?
617 if (size <= ftdi->readbuffer_remaining) {
618 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
621 ftdi->readbuffer_remaining -= size;
622 ftdi->readbuffer_offset += size;
624 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
628 // something still in the readbuffer, but not enough to satisfy 'size'?
629 if (ftdi->readbuffer_remaining != 0) {
630 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
633 offset += ftdi->readbuffer_remaining;
635 // do the actual USB read
636 while (offset < size && ret > 0) {
637 ftdi->readbuffer_remaining = 0;
638 ftdi->readbuffer_offset = 0;
639 /* returns how much received */
640 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
641 //printf("usbread %d\n", ret);
643 ftdi_error_return(ret, "usb bulk read failed");
646 // skip FTDI status bytes.
647 // Maybe stored in the future to enable modem use
648 num_of_chunks = ret / 64;
649 chunk_remains = ret % 64;
650 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
652 ftdi->readbuffer_offset += 2;
656 for (i = 1; i < num_of_chunks; i++)
657 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
658 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
660 if (chunk_remains > 2) {
661 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
662 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
664 ret -= 2*num_of_chunks;
666 ret -= 2*(num_of_chunks-1)+chunk_remains;
668 } else if (ret <= 2) {
669 // no more data to read?
673 // data still fits in buf?
674 if (offset+ret <= size) {
675 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
676 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
679 /* Did we read exactly the right amount of bytes? */
681 //printf("read_data exact rem %d offset %d\n",
682 //ftdi->readbuffer_remaining, offset);
685 // only copy part of the data or size <= readbuffer_chunksize
686 int part_size = size-offset;
687 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
689 ftdi->readbuffer_offset += part_size;
690 ftdi->readbuffer_remaining = ret-part_size;
693 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
694 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
705 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
707 unsigned char *new_buf;
709 // Invalidate all remaining data
710 ftdi->readbuffer_offset = 0;
711 ftdi->readbuffer_remaining = 0;
713 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
714 ftdi_error_return(-1, "out of memory for readbuffer");
716 ftdi->readbuffer = new_buf;
717 ftdi->readbuffer_chunksize = chunksize;
723 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
725 *chunksize = ftdi->readbuffer_chunksize;
731 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
733 unsigned short usb_val;
735 usb_val = bitmask; // low byte: bitmask
736 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
737 usb_val |= (ftdi->bitbang_mode << 8);
739 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
740 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
742 ftdi->bitbang_enabled = 1;
747 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
749 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
750 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
752 ftdi->bitbang_enabled = 0;
757 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
759 unsigned short usb_val;
761 usb_val = bitmask; // low byte: bitmask
762 usb_val |= (mode << 8);
763 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
764 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
766 ftdi->bitbang_mode = mode;
767 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
771 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
773 unsigned short usb_val;
774 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
775 ftdi_error_return(-1, "read pins failed");
777 *pins = (unsigned char)usb_val;
782 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
784 unsigned short usb_val;
787 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
790 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
791 ftdi_error_return(-2, "unable to set latency timer");
797 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
799 unsigned short usb_val;
800 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
801 ftdi_error_return(-1, "reading latency timer failed");
803 *latency = (unsigned char)usb_val;
808 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
810 eeprom->vendor_id = 0x0403;
811 eeprom->product_id = 0x6001;
813 eeprom->self_powered = 1;
814 eeprom->remote_wakeup = 1;
815 eeprom->BM_type_chip = 1;
817 eeprom->in_is_isochronous = 0;
818 eeprom->out_is_isochronous = 0;
819 eeprom->suspend_pull_downs = 0;
821 eeprom->use_serial = 0;
822 eeprom->change_usb_version = 0;
823 eeprom->usb_version = 0x0200;
824 eeprom->max_power = 0;
826 eeprom->manufacturer = NULL;
827 eeprom->product = NULL;
828 eeprom->serial = NULL;
835 Build binary output from ftdi_eeprom structure.
836 Output is suitable for ftdi_write_eeprom.
839 positive value: used eeprom size
840 -1: eeprom size (128 bytes) exceeded by custom strings
842 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
845 unsigned short checksum, value;
846 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
849 if (eeprom->manufacturer != NULL)
850 manufacturer_size = strlen(eeprom->manufacturer);
851 if (eeprom->product != NULL)
852 product_size = strlen(eeprom->product);
853 if (eeprom->serial != NULL)
854 serial_size = strlen(eeprom->serial);
856 size_check = 128; // eeprom is 128 bytes
857 size_check -= 28; // 28 are always in use (fixed)
858 size_check -= manufacturer_size*2;
859 size_check -= product_size*2;
860 size_check -= serial_size*2;
862 // eeprom size exceeded?
867 memset (output, 0, 128);
869 // Addr 00: Stay 00 00
870 // Addr 02: Vendor ID
871 output[0x02] = eeprom->vendor_id;
872 output[0x03] = eeprom->vendor_id >> 8;
874 // Addr 04: Product ID
875 output[0x04] = eeprom->product_id;
876 output[0x05] = eeprom->product_id >> 8;
878 // Addr 06: Device release number (0400h for BM features)
881 if (eeprom->BM_type_chip == 1)
886 // Addr 08: Config descriptor
887 // Bit 1: remote wakeup if 1
888 // Bit 0: self powered if 1
891 if (eeprom->self_powered == 1)
893 if (eeprom->remote_wakeup == 1)
897 // Addr 09: Max power consumption: max power = value * 2 mA
898 output[0x09] = eeprom->max_power;
901 // Addr 0A: Chip configuration
902 // Bit 7: 0 - reserved
903 // Bit 6: 0 - reserved
904 // Bit 5: 0 - reserved
905 // Bit 4: 1 - Change USB version
906 // Bit 3: 1 - Use the serial number string
907 // Bit 2: 1 - Enable suspend pull downs for lower power
908 // Bit 1: 1 - Out EndPoint is Isochronous
909 // Bit 0: 1 - In EndPoint is Isochronous
912 if (eeprom->in_is_isochronous == 1)
914 if (eeprom->out_is_isochronous == 1)
916 if (eeprom->suspend_pull_downs == 1)
918 if (eeprom->use_serial == 1)
920 if (eeprom->change_usb_version == 1)
927 // Addr 0C: USB version low byte when 0x0A bit 4 is set
928 // Addr 0D: USB version high byte when 0x0A bit 4 is set
929 if (eeprom->change_usb_version == 1) {
930 output[0x0C] = eeprom->usb_version;
931 output[0x0D] = eeprom->usb_version >> 8;
935 // Addr 0E: Offset of the manufacturer string + 0x80
936 output[0x0E] = 0x14 + 0x80;
938 // Addr 0F: Length of manufacturer string
939 output[0x0F] = manufacturer_size*2 + 2;
941 // Addr 10: Offset of the product string + 0x80, calculated later
942 // Addr 11: Length of product string
943 output[0x11] = product_size*2 + 2;
945 // Addr 12: Offset of the serial string + 0x80, calculated later
946 // Addr 13: Length of serial string
947 output[0x13] = serial_size*2 + 2;
950 output[0x14] = manufacturer_size*2 + 2;
951 output[0x15] = 0x03; // type: string
955 // Output manufacturer
956 for (j = 0; j < manufacturer_size; j++) {
957 output[i] = eeprom->manufacturer[j], i++;
958 output[i] = 0x00, i++;
961 // Output product name
962 output[0x10] = i + 0x80; // calculate offset
963 output[i] = product_size*2 + 2, i++;
964 output[i] = 0x03, i++;
965 for (j = 0; j < product_size; j++) {
966 output[i] = eeprom->product[j], i++;
967 output[i] = 0x00, i++;
971 output[0x12] = i + 0x80; // calculate offset
972 output[i] = serial_size*2 + 2, i++;
973 output[i] = 0x03, i++;
974 for (j = 0; j < serial_size; j++) {
975 output[i] = eeprom->serial[j], i++;
976 output[i] = 0x00, i++;
979 // calculate checksum
982 for (i = 0; i < 63; i++) {
984 value += output[(i*2)+1] << 8;
986 checksum = value^checksum;
987 checksum = (checksum << 1) | (checksum >> 15);
990 output[0x7E] = checksum;
991 output[0x7F] = checksum >> 8;
997 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1001 for (i = 0; i < 64; i++) {
1002 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1003 ftdi_error_return(-1, "reading eeprom failed");
1010 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1012 unsigned short usb_val;
1015 for (i = 0; i < 64; i++) {
1016 usb_val = eeprom[i*2];
1017 usb_val += eeprom[(i*2)+1] << 8;
1018 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1019 ftdi_error_return(-1, "unable to write eeprom");
1026 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1028 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1029 ftdi_error_return(-1, "unable to erase eeprom");
1035 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1037 return ftdi->error_str;
1041 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1043 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1044 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1045 NULL, 0, ftdi->usb_write_timeout) != 0)
1046 ftdi_error_return(-1, "set flow control failed");
1051 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1053 unsigned short usb_val;
1056 usb_val = SIO_SET_DTR_HIGH;
1058 usb_val = SIO_SET_DTR_LOW;
1060 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1061 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1062 NULL, 0, ftdi->usb_write_timeout) != 0)
1063 ftdi_error_return(-1, "set dtr failed");
1068 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1070 unsigned short usb_val;
1073 usb_val = SIO_SET_RTS_HIGH;
1075 usb_val = SIO_SET_RTS_LOW;
1077 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1078 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1079 NULL, 0, ftdi->usb_write_timeout) != 0)
1080 ftdi_error_return(-1, "set of rts failed");