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 Note: A readbuffer size above 64 bytes results in buggy input.
51 This seems to be a hardware limitation as noted
52 in the ftdi_sio driver */
53 return ftdi_read_data_set_chunksize(ftdi, 64);
57 void ftdi_deinit(struct ftdi_context *ftdi)
59 if (ftdi->readbuffer != NULL) {
60 free(ftdi->readbuffer);
61 ftdi->readbuffer = NULL;
66 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
72 /* ftdi_usb_open return codes:
74 -1: usb_find_busses() failed
75 -2: usb_find_devices() failed
76 -3: usb device not found
77 -4: unable to open device
78 -5: unable to claim device
80 -7: set baudrate failed
81 -8: get product description failed
82 -9: get serial number failed
83 -10: unable to close device
85 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
87 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
90 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
91 const char* description, const char* serial)
94 struct usb_device *dev;
98 if (usb_find_busses() < 0) {
99 ftdi->error_str = "usb_find_busses() failed";
103 if (usb_find_devices() < 0) {
104 ftdi->error_str = "usb_find_devices() failed";
108 for (bus = usb_busses; bus; bus = bus->next) {
109 for (dev = bus->devices; dev; dev = dev->next) {
110 if (dev->descriptor.idVendor == vendor
111 && dev->descriptor.idProduct == product) {
112 if (!(ftdi->usb_dev = usb_open(dev))) {
113 ftdi->error_str = "usb_open() failed";
118 if (description != NULL) {
119 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
120 ftdi->error_str = "unable to fetch product description\n";
121 if (usb_close (ftdi->usb_dev) != 0)
125 if (strncmp(string, description, sizeof(string)) != 0) {
126 ftdi->error_str = "product description not matching\n";
127 if (usb_close (ftdi->usb_dev) != 0)
132 if (serial != NULL) {
133 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
134 ftdi->error_str = "unable to fetch serial number\n";
135 if (usb_close (ftdi->usb_dev) != 0)
139 if (strncmp(string, serial, sizeof(string)) != 0) {
140 ftdi->error_str = "serial number not matching\n";
141 if (usb_close (ftdi->usb_dev) != 0)
147 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
148 ftdi->error_str = "unable to claim usb device. Make sure ftdi_sio is unloaded!";
149 if (usb_close (ftdi->usb_dev) != 0)
154 if (ftdi_usb_reset (ftdi) != 0) {
155 if (usb_close (ftdi->usb_dev) != 0)
160 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
161 if (usb_close (ftdi->usb_dev) != 0)
166 // Try to guess chip type
167 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
168 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
169 && dev->descriptor.iSerialNumber == 0))
170 ftdi->type = TYPE_BM;
171 else if (dev->descriptor.bcdDevice == 0x200)
172 ftdi->type = TYPE_AM;
173 else if (dev->descriptor.bcdDevice == 0x500)
174 ftdi->type = TYPE_2232C;
186 int ftdi_usb_reset(struct ftdi_context *ftdi)
188 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
189 ftdi->error_str = "FTDI reset failed";
192 // Invalidate data in the readbuffer
193 ftdi->readbuffer_offset = 0;
194 ftdi->readbuffer_remaining = 0;
199 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
201 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
202 ftdi->error_str = "FTDI purge of RX buffer failed";
205 // Invalidate data in the readbuffer
206 ftdi->readbuffer_offset = 0;
207 ftdi->readbuffer_remaining = 0;
209 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
210 ftdi->error_str = "FTDI purge of TX buffer failed";
218 /* ftdi_usb_close return codes
220 -1: usb_release failed
223 int ftdi_usb_close(struct ftdi_context *ftdi)
227 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
230 if (usb_close (ftdi->usb_dev) != 0)
238 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
239 Function is only used internally
241 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
242 unsigned short *value, unsigned short *index)
244 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
245 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
246 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
247 int divisor, best_divisor, best_baud, best_baud_diff;
248 unsigned long encoded_divisor;
256 divisor = 24000000 / baudrate;
258 if (ftdi->type == TYPE_AM) {
259 // Round down to supported fraction (AM only)
260 divisor -= am_adjust_dn[divisor & 7];
263 // Try this divisor and the one above it (because division rounds down)
267 for (i = 0; i < 2; i++) {
268 int try_divisor = divisor + i;
272 // Round up to supported divisor value
273 if (try_divisor < 8) {
274 // Round up to minimum supported divisor
276 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
277 // BM doesn't support divisors 9 through 11 inclusive
279 } else if (divisor < 16) {
280 // AM doesn't support divisors 9 through 15 inclusive
283 if (ftdi->type == TYPE_AM) {
284 // Round up to supported fraction (AM only)
285 try_divisor += am_adjust_up[try_divisor & 7];
286 if (try_divisor > 0x1FFF8) {
287 // Round down to maximum supported divisor value (for AM)
288 try_divisor = 0x1FFF8;
291 if (try_divisor > 0x1FFFF) {
292 // Round down to maximum supported divisor value (for BM)
293 try_divisor = 0x1FFFF;
297 // Get estimated baud rate (to nearest integer)
298 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
299 // Get absolute difference from requested baud rate
300 if (baud_estimate < baudrate) {
301 baud_diff = baudrate - baud_estimate;
303 baud_diff = baud_estimate - baudrate;
305 if (i == 0 || baud_diff < best_baud_diff) {
306 // Closest to requested baud rate so far
307 best_divisor = try_divisor;
308 best_baud = baud_estimate;
309 best_baud_diff = baud_diff;
310 if (baud_diff == 0) {
311 // Spot on! No point trying
316 // Encode the best divisor value
317 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
318 // Deal with special cases for encoded value
319 if (encoded_divisor == 1) {
320 encoded_divisor = 0; // 3000000 baud
321 } else if (encoded_divisor == 0x4001) {
322 encoded_divisor = 1; // 2000000 baud (BM only)
324 // Split into "value" and "index" values
325 *value = (unsigned short)(encoded_divisor & 0xFFFF);
326 if(ftdi->type == TYPE_2232C) {
327 *index = (unsigned short)(encoded_divisor >> 8);
329 *index |= ftdi->interface;
332 *index = (unsigned short)(encoded_divisor >> 16);
334 // Return the nearest baud rate
339 ftdi_set_baudrate return codes:
342 -2: setting baudrate failed
344 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
346 unsigned short value, index;
349 if (ftdi->bitbang_enabled) {
350 baudrate = baudrate*4;
353 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
354 if (actual_baudrate <= 0) {
355 ftdi->error_str = "Silly baudrate <= 0.";
359 // Check within tolerance (about 5%)
360 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
361 || ((actual_baudrate < baudrate)
362 ? (actual_baudrate * 21 < baudrate * 20)
363 : (baudrate * 21 < actual_baudrate * 20))) {
364 ftdi->error_str = "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4";
368 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) {
369 ftdi->error_str = "Setting new baudrate failed";
373 ftdi->baudrate = baudrate;
378 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
382 int total_written = 0;
383 while (offset < size) {
384 int write_size = ftdi->writebuffer_chunksize;
386 if (offset+write_size > size)
387 write_size = size-offset;
389 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
392 ftdi->error_str = "bulk write failed";
394 ftdi->error_str = "usb failed";
397 total_written += ret;
399 offset += write_size;
402 return total_written;
406 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
408 ftdi->writebuffer_chunksize = chunksize;
413 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
415 *chunksize = ftdi->writebuffer_chunksize;
420 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
422 int offset = 0, ret = 1;
424 // everything we want is still in the readbuffer?
425 if (size <= ftdi->readbuffer_remaining) {
426 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
429 ftdi->readbuffer_remaining -= size;
430 ftdi->readbuffer_offset += size;
432 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
436 // something still in the readbuffer, but not enough to satisfy 'size'?
437 if (ftdi->readbuffer_remaining != 0) {
438 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
441 offset += ftdi->readbuffer_remaining;
443 // do the actual USB read
444 while (offset < size && ret > 0) {
445 ftdi->readbuffer_remaining = 0;
446 ftdi->readbuffer_offset = 0;
447 /* returns how much received */
448 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
452 ftdi->error_str = "bulk read failed";
454 ftdi->error_str = "usb failed";
459 // skip FTDI status bytes.
460 // Maybe stored in the future to enable modem use
461 ftdi->readbuffer_offset += 2;
463 } else if (ret <= 2) {
464 // no more data to read?
468 // data still fits in buf?
469 if (offset+ret <= size) {
470 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
471 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
474 /* Did we read exactly the right amount of bytes? */
478 // only copy part of the data or size <= readbuffer_chunksize
479 int part_size = size-offset;
480 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
482 ftdi->readbuffer_offset += part_size;
483 ftdi->readbuffer_remaining = ret-part_size;
486 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
487 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
498 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
500 unsigned char *new_buf;
502 // Invalidate all remaining data
503 ftdi->readbuffer_offset = 0;
504 ftdi->readbuffer_remaining = 0;
506 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
507 ftdi->error_str = "out of memory for readbuffer";
511 ftdi->readbuffer = new_buf;
512 ftdi->readbuffer_chunksize = chunksize;
518 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
520 *chunksize = ftdi->readbuffer_chunksize;
526 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
528 unsigned short usb_val;
530 usb_val = bitmask; // low byte: bitmask
531 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
532 usb_val |= (ftdi->bitbang_mode << 8);
534 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
535 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
538 ftdi->bitbang_enabled = 1;
543 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
545 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
546 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
550 ftdi->bitbang_enabled = 0;
555 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
557 unsigned short usb_val;
558 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
559 ftdi->error_str = "Read pins failed";
563 *pins = (unsigned char)usb_val;
568 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
570 unsigned short usb_val;
573 ftdi->error_str = "Latency out of range. Only valid for 1-255";
578 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
579 ftdi->error_str = "Unable to set latency timer";
586 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
588 unsigned short usb_val;
589 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
590 ftdi->error_str = "Reading latency timer failed";
594 *latency = (unsigned char)usb_val;
599 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
601 eeprom->vendor_id = 0x0403;
602 eeprom->product_id = 0x6001;
604 eeprom->self_powered = 1;
605 eeprom->remote_wakeup = 1;
606 eeprom->BM_type_chip = 1;
608 eeprom->in_is_isochronous = 0;
609 eeprom->out_is_isochronous = 0;
610 eeprom->suspend_pull_downs = 0;
612 eeprom->use_serial = 0;
613 eeprom->change_usb_version = 0;
614 eeprom->usb_version = 0x0200;
615 eeprom->max_power = 0;
617 eeprom->manufacturer = NULL;
618 eeprom->product = NULL;
619 eeprom->serial = NULL;
624 ftdi_eeprom_build return codes:
625 positive value: used eeprom size
626 -1: eeprom size (128 bytes) exceeded by custom strings
628 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
631 unsigned short checksum, value;
632 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
635 if (eeprom->manufacturer != NULL)
636 manufacturer_size = strlen(eeprom->manufacturer);
637 if (eeprom->product != NULL)
638 product_size = strlen(eeprom->product);
639 if (eeprom->serial != NULL)
640 serial_size = strlen(eeprom->serial);
642 size_check = 128; // eeprom is 128 bytes
643 size_check -= 28; // 28 are always in use (fixed)
644 size_check -= manufacturer_size*2;
645 size_check -= product_size*2;
646 size_check -= serial_size*2;
648 // eeprom size exceeded?
653 memset (output, 0, 128);
655 // Addr 00: Stay 00 00
656 // Addr 02: Vendor ID
657 output[0x02] = eeprom->vendor_id;
658 output[0x03] = eeprom->vendor_id >> 8;
660 // Addr 04: Product ID
661 output[0x04] = eeprom->product_id;
662 output[0x05] = eeprom->product_id >> 8;
664 // Addr 06: Device release number (0400h for BM features)
667 if (eeprom->BM_type_chip == 1)
672 // Addr 08: Config descriptor
673 // Bit 1: remote wakeup if 1
674 // Bit 0: self powered if 1
677 if (eeprom->self_powered == 1)
679 if (eeprom->remote_wakeup == 1)
683 // Addr 09: Max power consumption: max power = value * 2 mA
684 output[0x09] = eeprom->max_power;
687 // Addr 0A: Chip configuration
688 // Bit 7: 0 - reserved
689 // Bit 6: 0 - reserved
690 // Bit 5: 0 - reserved
691 // Bit 4: 1 - Change USB version
692 // Bit 3: 1 - Use the serial number string
693 // Bit 2: 1 - Enable suspend pull downs for lower power
694 // Bit 1: 1 - Out EndPoint is Isochronous
695 // Bit 0: 1 - In EndPoint is Isochronous
698 if (eeprom->in_is_isochronous == 1)
700 if (eeprom->out_is_isochronous == 1)
702 if (eeprom->suspend_pull_downs == 1)
704 if (eeprom->use_serial == 1)
706 if (eeprom->change_usb_version == 1)
713 // Addr 0C: USB version low byte when 0x0A bit 4 is set
714 // Addr 0D: USB version high byte when 0x0A bit 4 is set
715 if (eeprom->change_usb_version == 1) {
716 output[0x0C] = eeprom->usb_version;
717 output[0x0D] = eeprom->usb_version >> 8;
721 // Addr 0E: Offset of the manufacturer string + 0x80
722 output[0x0E] = 0x14 + 0x80;
724 // Addr 0F: Length of manufacturer string
725 output[0x0F] = manufacturer_size*2 + 2;
727 // Addr 10: Offset of the product string + 0x80, calculated later
728 // Addr 11: Length of product string
729 output[0x11] = product_size*2 + 2;
731 // Addr 12: Offset of the serial string + 0x80, calculated later
732 // Addr 13: Length of serial string
733 output[0x13] = serial_size*2 + 2;
736 output[0x14] = manufacturer_size*2 + 2;
737 output[0x15] = 0x03; // type: string
741 // Output manufacturer
742 for (j = 0; j < manufacturer_size; j++) {
743 output[i] = eeprom->manufacturer[j], i++;
744 output[i] = 0x00, i++;
747 // Output product name
748 output[0x10] = i + 0x80; // calculate offset
749 output[i] = product_size*2 + 2, i++;
750 output[i] = 0x03, i++;
751 for (j = 0; j < product_size; j++) {
752 output[i] = eeprom->product[j], i++;
753 output[i] = 0x00, i++;
757 output[0x12] = i + 0x80; // calculate offset
758 output[i] = serial_size*2 + 2, i++;
759 output[i] = 0x03, i++;
760 for (j = 0; j < serial_size; j++) {
761 output[i] = eeprom->serial[j], i++;
762 output[i] = 0x00, i++;
765 // calculate checksum
768 for (i = 0; i < 63; i++) {
770 value += output[(i*2)+1] << 8;
772 checksum = value^checksum;
773 checksum = (checksum << 1) | (checksum >> 15);
776 output[0x7E] = checksum;
777 output[0x7F] = checksum >> 8;
783 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
787 for (i = 0; i < 64; i++) {
788 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) {
789 ftdi->error_str = "Reading eeprom failed";
798 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
800 unsigned short usb_val;
803 for (i = 0; i < 64; i++) {
804 usb_val = eeprom[i*2];
805 usb_val += eeprom[(i*2)+1] << 8;
806 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) {
807 ftdi->error_str = "Unable to write eeprom";
816 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
818 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) {
819 ftdi->error_str = "Unable to erase eeprom";