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 /* ftdi_init return codes:
24 -1: couldn't allocate read buffer
26 int ftdi_init(struct ftdi_context *ftdi)
29 ftdi->usb_read_timeout = 5000;
30 ftdi->usb_write_timeout = 5000;
32 ftdi->type = TYPE_BM; /* chip type */
34 ftdi->bitbang_enabled = 0;
36 ftdi->readbuffer = NULL;
37 ftdi->readbuffer_offset = 0;
38 ftdi->readbuffer_remaining = 0;
39 ftdi->writebuffer_chunksize = 4096;
45 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
47 ftdi->error_str = NULL;
49 /* All fine. Now allocate the readbuffer */
50 return ftdi_read_data_set_chunksize(ftdi, 4096);
54 void ftdi_deinit(struct ftdi_context *ftdi)
56 if (ftdi->readbuffer != NULL) {
57 free(ftdi->readbuffer);
58 ftdi->readbuffer = NULL;
63 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
69 /* ftdi_usb_open return codes:
71 -1: usb_find_busses() failed
72 -2: usb_find_devices() failed
73 -3: usb device not found
74 -4: unable to open device
75 -5: unable to claim device
77 -7: set baudrate failed
78 -8: get product description failed
79 -9: get serial number failed
80 -10: unable to close device
82 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
84 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
87 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
88 const char* description, const char* serial)
91 struct usb_device *dev;
95 if (usb_find_busses() < 0) {
96 ftdi->error_str = "usb_find_busses() failed";
100 if (usb_find_devices() < 0) {
101 ftdi->error_str = "usb_find_devices() failed";
105 for (bus = usb_busses; bus; bus = bus->next) {
106 for (dev = bus->devices; dev; dev = dev->next) {
107 if (dev->descriptor.idVendor == vendor
108 && dev->descriptor.idProduct == product) {
109 if (!(ftdi->usb_dev = usb_open(dev))) {
110 ftdi->error_str = "usb_open() failed";
115 if (description != NULL) {
116 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
117 ftdi->error_str = "unable to fetch product description\n";
118 if (usb_close (ftdi->usb_dev) != 0)
122 if (strncmp(string, description, sizeof(string)) != 0) {
123 ftdi->error_str = "product description not matching\n";
124 if (usb_close (ftdi->usb_dev) != 0)
129 if (serial != NULL) {
130 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
131 ftdi->error_str = "unable to fetch serial number\n";
132 if (usb_close (ftdi->usb_dev) != 0)
136 if (strncmp(string, serial, sizeof(string)) != 0) {
137 ftdi->error_str = "serial number not matching\n";
138 if (usb_close (ftdi->usb_dev) != 0)
144 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
145 ftdi->error_str = "unable to claim usb device. Make sure ftdi_sio is unloaded!";
146 if (usb_close (ftdi->usb_dev) != 0)
151 if (ftdi_usb_reset (ftdi) != 0) {
152 if (usb_close (ftdi->usb_dev) != 0)
157 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
158 if (usb_close (ftdi->usb_dev) != 0)
163 // Try to guess chip type
164 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
165 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
166 && dev->descriptor.iSerialNumber == 0))
167 ftdi->type = TYPE_BM;
168 else if (dev->descriptor.bcdDevice == 0x200)
169 ftdi->type = TYPE_AM;
170 else if (dev->descriptor.bcdDevice == 0x500)
171 ftdi->type = TYPE_2232C;
183 int ftdi_usb_reset(struct ftdi_context *ftdi)
185 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
186 ftdi->error_str = "FTDI reset failed";
190 if (usb_reset(ftdi->usb_dev) != 0) {
191 ftdi->error_str = "USB reset failed";
194 // Invalidate data in the readbuffer
195 ftdi->readbuffer_offset = 0;
196 ftdi->readbuffer_remaining = 0;
201 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
203 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
204 ftdi->error_str = "FTDI purge of RX buffer failed";
207 // Invalidate data in the readbuffer
208 ftdi->readbuffer_offset = 0;
209 ftdi->readbuffer_remaining = 0;
211 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
212 ftdi->error_str = "FTDI purge of TX buffer failed";
220 /* ftdi_usb_close return codes
222 -1: usb_release failed
225 int ftdi_usb_close(struct ftdi_context *ftdi)
229 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
232 if (usb_close (ftdi->usb_dev) != 0)
240 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
241 Function is only used internally
243 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
244 unsigned short *value, unsigned short *index)
246 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
247 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
248 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
249 int divisor, best_divisor, best_baud, best_baud_diff;
250 unsigned long encoded_divisor;
258 divisor = 24000000 / baudrate;
260 if (ftdi->type == TYPE_AM) {
261 // Round down to supported fraction (AM only)
262 divisor -= am_adjust_dn[divisor & 7];
265 // Try this divisor and the one above it (because division rounds down)
269 for (i = 0; i < 2; i++) {
270 int try_divisor = divisor + i;
274 // Round up to supported divisor value
275 if (try_divisor < 8) {
276 // Round up to minimum supported divisor
278 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
279 // BM doesn't support divisors 9 through 11 inclusive
281 } else if (divisor < 16) {
282 // AM doesn't support divisors 9 through 15 inclusive
285 if (ftdi->type == TYPE_AM) {
286 // Round up to supported fraction (AM only)
287 try_divisor += am_adjust_up[try_divisor & 7];
288 if (try_divisor > 0x1FFF8) {
289 // Round down to maximum supported divisor value (for AM)
290 try_divisor = 0x1FFF8;
293 if (try_divisor > 0x1FFFF) {
294 // Round down to maximum supported divisor value (for BM)
295 try_divisor = 0x1FFFF;
299 // Get estimated baud rate (to nearest integer)
300 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
301 // Get absolute difference from requested baud rate
302 if (baud_estimate < baudrate) {
303 baud_diff = baudrate - baud_estimate;
305 baud_diff = baud_estimate - baudrate;
307 if (i == 0 || baud_diff < best_baud_diff) {
308 // Closest to requested baud rate so far
309 best_divisor = try_divisor;
310 best_baud = baud_estimate;
311 best_baud_diff = baud_diff;
312 if (baud_diff == 0) {
313 // Spot on! No point trying
318 // Encode the best divisor value
319 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
320 // Deal with special cases for encoded value
321 if (encoded_divisor == 1) {
322 encoded_divisor = 0; // 3000000 baud
323 } else if (encoded_divisor == 0x4001) {
324 encoded_divisor = 1; // 2000000 baud (BM only)
326 // Split into "value" and "index" values
327 *value = (unsigned short)(encoded_divisor & 0xFFFF);
328 if(ftdi->type == TYPE_2232C) {
329 *index = (unsigned short)(encoded_divisor >> 8);
331 *index |= ftdi->interface;
334 *index = (unsigned short)(encoded_divisor >> 16);
336 // Return the nearest baud rate
341 ftdi_set_baudrate return codes:
344 -2: setting baudrate failed
346 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
348 unsigned short value, index;
351 if (ftdi->bitbang_enabled) {
352 baudrate = baudrate*4;
355 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
356 if (actual_baudrate <= 0) {
357 ftdi->error_str = "Silly baudrate <= 0.";
361 // Check within tolerance (about 5%)
362 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
363 || ((actual_baudrate < baudrate)
364 ? (actual_baudrate * 21 < baudrate * 20)
365 : (baudrate * 21 < actual_baudrate * 20))) {
366 ftdi->error_str = "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4";
370 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) {
371 ftdi->error_str = "Setting new baudrate failed";
375 ftdi->baudrate = baudrate;
380 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
384 int total_written = 0;
385 while (offset < size) {
386 int write_size = ftdi->writebuffer_chunksize;
388 if (offset+write_size > size)
389 write_size = size-offset;
391 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
394 ftdi->error_str = "bulk write failed";
396 ftdi->error_str = "usb failed";
399 total_written += ret;
401 offset += write_size;
404 return total_written;
408 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
410 ftdi->writebuffer_chunksize = chunksize;
415 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
417 *chunksize = ftdi->writebuffer_chunksize;
422 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
424 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
426 // everything we want is still in the readbuffer?
427 if (size <= ftdi->readbuffer_remaining) {
428 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
431 ftdi->readbuffer_remaining -= size;
432 ftdi->readbuffer_offset += size;
434 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
438 // something still in the readbuffer, but not enough to satisfy 'size'?
439 if (ftdi->readbuffer_remaining != 0) {
440 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
443 offset += ftdi->readbuffer_remaining;
445 // do the actual USB read
446 while (offset < size && ret > 0) {
447 ftdi->readbuffer_remaining = 0;
448 ftdi->readbuffer_offset = 0;
449 /* returns how much received */
450 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
454 ftdi->error_str = "bulk read failed";
456 ftdi->error_str = "usb failed";
461 // skip FTDI status bytes.
462 // Maybe stored in the future to enable modem use
463 num_of_chunks = ret / 64;
464 chunk_remains = ret % 64;
465 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
467 ftdi->readbuffer_offset += 2;
471 for (i = 1; i < num_of_chunks; i++)
472 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
473 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
475 if (chunk_remains > 2) {
476 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
477 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
479 ret -= 2*num_of_chunks;
481 ret -= 2*(num_of_chunks-1)+chunk_remains;
483 } else if (ret <= 2) {
484 // no more data to read?
488 // data still fits in buf?
489 if (offset+ret <= size) {
490 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
491 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
494 /* Did we read exactly the right amount of bytes? */
498 // only copy part of the data or size <= readbuffer_chunksize
499 int part_size = size-offset;
500 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
502 ftdi->readbuffer_offset += part_size;
503 ftdi->readbuffer_remaining = ret-part_size;
506 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
507 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
518 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
520 unsigned char *new_buf;
522 // Invalidate all remaining data
523 ftdi->readbuffer_offset = 0;
524 ftdi->readbuffer_remaining = 0;
526 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
527 ftdi->error_str = "out of memory for readbuffer";
531 ftdi->readbuffer = new_buf;
532 ftdi->readbuffer_chunksize = chunksize;
538 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
540 *chunksize = ftdi->readbuffer_chunksize;
546 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
548 unsigned short usb_val;
550 usb_val = bitmask; // low byte: bitmask
551 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
552 usb_val |= (ftdi->bitbang_mode << 8);
554 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
555 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
558 ftdi->bitbang_enabled = 1;
563 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
565 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
566 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
570 ftdi->bitbang_enabled = 0;
575 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
577 unsigned short usb_val;
578 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
579 ftdi->error_str = "Read pins failed";
583 *pins = (unsigned char)usb_val;
588 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
590 unsigned short usb_val;
593 ftdi->error_str = "Latency out of range. Only valid for 1-255";
598 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
599 ftdi->error_str = "Unable to set latency timer";
606 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
608 unsigned short usb_val;
609 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
610 ftdi->error_str = "Reading latency timer failed";
614 *latency = (unsigned char)usb_val;
619 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
621 eeprom->vendor_id = 0x0403;
622 eeprom->product_id = 0x6001;
624 eeprom->self_powered = 1;
625 eeprom->remote_wakeup = 1;
626 eeprom->BM_type_chip = 1;
628 eeprom->in_is_isochronous = 0;
629 eeprom->out_is_isochronous = 0;
630 eeprom->suspend_pull_downs = 0;
632 eeprom->use_serial = 0;
633 eeprom->change_usb_version = 0;
634 eeprom->usb_version = 0x0200;
635 eeprom->max_power = 0;
637 eeprom->manufacturer = NULL;
638 eeprom->product = NULL;
639 eeprom->serial = NULL;
644 ftdi_eeprom_build return codes:
645 positive value: used eeprom size
646 -1: eeprom size (128 bytes) exceeded by custom strings
648 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
651 unsigned short checksum, value;
652 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
655 if (eeprom->manufacturer != NULL)
656 manufacturer_size = strlen(eeprom->manufacturer);
657 if (eeprom->product != NULL)
658 product_size = strlen(eeprom->product);
659 if (eeprom->serial != NULL)
660 serial_size = strlen(eeprom->serial);
662 size_check = 128; // eeprom is 128 bytes
663 size_check -= 28; // 28 are always in use (fixed)
664 size_check -= manufacturer_size*2;
665 size_check -= product_size*2;
666 size_check -= serial_size*2;
668 // eeprom size exceeded?
673 memset (output, 0, 128);
675 // Addr 00: Stay 00 00
676 // Addr 02: Vendor ID
677 output[0x02] = eeprom->vendor_id;
678 output[0x03] = eeprom->vendor_id >> 8;
680 // Addr 04: Product ID
681 output[0x04] = eeprom->product_id;
682 output[0x05] = eeprom->product_id >> 8;
684 // Addr 06: Device release number (0400h for BM features)
687 if (eeprom->BM_type_chip == 1)
692 // Addr 08: Config descriptor
693 // Bit 1: remote wakeup if 1
694 // Bit 0: self powered if 1
697 if (eeprom->self_powered == 1)
699 if (eeprom->remote_wakeup == 1)
703 // Addr 09: Max power consumption: max power = value * 2 mA
704 output[0x09] = eeprom->max_power;
707 // Addr 0A: Chip configuration
708 // Bit 7: 0 - reserved
709 // Bit 6: 0 - reserved
710 // Bit 5: 0 - reserved
711 // Bit 4: 1 - Change USB version
712 // Bit 3: 1 - Use the serial number string
713 // Bit 2: 1 - Enable suspend pull downs for lower power
714 // Bit 1: 1 - Out EndPoint is Isochronous
715 // Bit 0: 1 - In EndPoint is Isochronous
718 if (eeprom->in_is_isochronous == 1)
720 if (eeprom->out_is_isochronous == 1)
722 if (eeprom->suspend_pull_downs == 1)
724 if (eeprom->use_serial == 1)
726 if (eeprom->change_usb_version == 1)
733 // Addr 0C: USB version low byte when 0x0A bit 4 is set
734 // Addr 0D: USB version high byte when 0x0A bit 4 is set
735 if (eeprom->change_usb_version == 1) {
736 output[0x0C] = eeprom->usb_version;
737 output[0x0D] = eeprom->usb_version >> 8;
741 // Addr 0E: Offset of the manufacturer string + 0x80
742 output[0x0E] = 0x14 + 0x80;
744 // Addr 0F: Length of manufacturer string
745 output[0x0F] = manufacturer_size*2 + 2;
747 // Addr 10: Offset of the product string + 0x80, calculated later
748 // Addr 11: Length of product string
749 output[0x11] = product_size*2 + 2;
751 // Addr 12: Offset of the serial string + 0x80, calculated later
752 // Addr 13: Length of serial string
753 output[0x13] = serial_size*2 + 2;
756 output[0x14] = manufacturer_size*2 + 2;
757 output[0x15] = 0x03; // type: string
761 // Output manufacturer
762 for (j = 0; j < manufacturer_size; j++) {
763 output[i] = eeprom->manufacturer[j], i++;
764 output[i] = 0x00, i++;
767 // Output product name
768 output[0x10] = i + 0x80; // calculate offset
769 output[i] = product_size*2 + 2, i++;
770 output[i] = 0x03, i++;
771 for (j = 0; j < product_size; j++) {
772 output[i] = eeprom->product[j], i++;
773 output[i] = 0x00, i++;
777 output[0x12] = i + 0x80; // calculate offset
778 output[i] = serial_size*2 + 2, i++;
779 output[i] = 0x03, i++;
780 for (j = 0; j < serial_size; j++) {
781 output[i] = eeprom->serial[j], i++;
782 output[i] = 0x00, i++;
785 // calculate checksum
788 for (i = 0; i < 63; i++) {
790 value += output[(i*2)+1] << 8;
792 checksum = value^checksum;
793 checksum = (checksum << 1) | (checksum >> 15);
796 output[0x7E] = checksum;
797 output[0x7F] = checksum >> 8;
803 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
807 for (i = 0; i < 64; i++) {
808 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) {
809 ftdi->error_str = "Reading eeprom failed";
818 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
820 unsigned short usb_val;
823 for (i = 0; i < 64; i++) {
824 usb_val = eeprom[i*2];
825 usb_val += eeprom[(i*2)+1] << 8;
826 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) {
827 ftdi->error_str = "Unable to write eeprom";
836 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
838 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) {
839 ftdi->error_str = "Unable to erase eeprom";