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 ***************************************************************************/
21 /* ftdi_init return codes:
23 -1: couldn't allocate read buffer
25 int ftdi_init(struct ftdi_context *ftdi) {
27 ftdi->usb_read_timeout = 5000;
28 ftdi->usb_write_timeout = 5000;
30 ftdi->type = TYPE_BM; /* chip type */
32 ftdi->bitbang_enabled = 0;
34 ftdi->readbuffer = NULL;
35 ftdi->readbuffer_offset = 0;
36 ftdi->readbuffer_remaining = 0;
37 ftdi->writebuffer_chunksize = 4096;
43 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
45 ftdi->error_str = NULL;
47 // all fine. Now allocate the readbuffer
48 return ftdi_read_data_set_chunksize(ftdi, 4096);
52 void ftdi_deinit(struct ftdi_context *ftdi) {
53 if (ftdi->readbuffer != NULL) {
54 free(ftdi->readbuffer);
55 ftdi->readbuffer = NULL;
60 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb) {
65 /* ftdi_usb_open return codes:
67 -1: usb_find_busses() failed
68 -2: usb_find_devices() failed
69 -3: usb device not found
70 -4: unable to open device
71 -5: unable to claim device
73 -7: set baudrate failed
75 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product) {
77 struct usb_device *dev;
81 if (usb_find_busses() < 0) {
82 ftdi->error_str = "usb_find_busses() failed";
86 if (usb_find_devices() < 0) {
87 ftdi->error_str = "usb_find_devices() failed";
91 for (bus = usb_busses; bus; bus = bus->next) {
92 for (dev = bus->devices; dev; dev = dev->next) {
93 if (dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product) {
94 ftdi->usb_dev = usb_open(dev);
96 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
97 ftdi->error_str = "unable to claim usb device. Make sure ftdi_sio is unloaded!";
101 if (ftdi_usb_reset (ftdi) != 0)
104 if (ftdi_set_baudrate (ftdi, 9600) != 0)
107 // Try to guess chip type
108 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
109 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
110 && dev->descriptor.iSerialNumber == 0))
111 ftdi->type = TYPE_BM;
112 else if (dev->descriptor.bcdDevice == 0x200)
113 ftdi->type = TYPE_AM;
114 else if (dev->descriptor.bcdDevice == 0x500)
115 ftdi->type = TYPE_2232C;
119 ftdi->error_str = "usb_open() failed";
132 int ftdi_usb_reset(struct ftdi_context *ftdi) {
133 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
134 ftdi->error_str = "FTDI reset failed";
137 // Invalidate data in the readbuffer
138 ftdi->readbuffer_offset = 0;
139 ftdi->readbuffer_remaining = 0;
144 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi) {
145 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
146 ftdi->error_str = "FTDI purge of RX buffer failed";
149 // Invalidate data in the readbuffer
150 ftdi->readbuffer_offset = 0;
151 ftdi->readbuffer_remaining = 0;
153 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
154 ftdi->error_str = "FTDI purge of TX buffer failed";
162 /* ftdi_usb_close return codes
164 -1: usb_release failed
167 int ftdi_usb_close(struct ftdi_context *ftdi) {
170 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
173 if (usb_close (ftdi->usb_dev) != 0)
181 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
182 Function is only used internally
184 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
185 unsigned short *value, unsigned short *index) {
186 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
187 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
188 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
189 int divisor, best_divisor, best_baud, best_baud_diff;
190 unsigned long encoded_divisor;
198 divisor = 24000000 / baudrate;
200 if (ftdi->type == TYPE_AM) {
201 // Round down to supported fraction (AM only)
202 divisor -= am_adjust_dn[divisor & 7];
205 // Try this divisor and the one above it (because division rounds down)
209 for (i = 0; i < 2; i++) {
210 int try_divisor = divisor + i;
214 // Round up to supported divisor value
215 if (try_divisor < 8) {
216 // Round up to minimum supported divisor
218 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
219 // BM doesn't support divisors 9 through 11 inclusive
221 } else if (divisor < 16) {
222 // AM doesn't support divisors 9 through 15 inclusive
225 if (ftdi->type == TYPE_AM) {
226 // Round up to supported fraction (AM only)
227 try_divisor += am_adjust_up[try_divisor & 7];
228 if (try_divisor > 0x1FFF8) {
229 // Round down to maximum supported divisor value (for AM)
230 try_divisor = 0x1FFF8;
233 if (try_divisor > 0x1FFFF) {
234 // Round down to maximum supported divisor value (for BM)
235 try_divisor = 0x1FFFF;
239 // Get estimated baud rate (to nearest integer)
240 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
241 // Get absolute difference from requested baud rate
242 if (baud_estimate < baudrate) {
243 baud_diff = baudrate - baud_estimate;
245 baud_diff = baud_estimate - baudrate;
247 if (i == 0 || baud_diff < best_baud_diff) {
248 // Closest to requested baud rate so far
249 best_divisor = try_divisor;
250 best_baud = baud_estimate;
251 best_baud_diff = baud_diff;
252 if (baud_diff == 0) {
253 // Spot on! No point trying
258 // Encode the best divisor value
259 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
260 // Deal with special cases for encoded value
261 if (encoded_divisor == 1) {
262 encoded_divisor = 0; // 3000000 baud
263 } else if (encoded_divisor == 0x4001) {
264 encoded_divisor = 1; // 2000000 baud (BM only)
266 // Split into "value" and "index" values
267 *value = (unsigned short)(encoded_divisor & 0xFFFF);
268 if(ftdi->type == TYPE_2232C) {
269 *index = (unsigned short)(encoded_divisor >> 8);
271 *index |= ftdi->interface;
274 *index = (unsigned short)(encoded_divisor >> 16);
276 // Return the nearest baud rate
281 ftdi_set_baudrate return codes:
284 -2: setting baudrate failed
286 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate) {
287 unsigned short value, index;
290 if (ftdi->bitbang_enabled) {
291 baudrate = baudrate*4;
294 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
295 if (actual_baudrate <= 0) {
296 ftdi->error_str = "Silly baudrate <= 0.";
300 // Check within tolerance (about 5%)
301 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
302 || ((actual_baudrate < baudrate)
303 ? (actual_baudrate * 21 < baudrate * 20)
304 : (baudrate * 21 < actual_baudrate * 20))) {
305 ftdi->error_str = "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4";
309 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0) {
310 ftdi->error_str = "Setting new baudrate failed";
314 ftdi->baudrate = baudrate;
319 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
322 int total_written = 0;
323 while (offset < size) {
324 int write_size = ftdi->writebuffer_chunksize;
326 if (offset+write_size > size)
327 write_size = size-offset;
329 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
331 ftdi->error_str = "bulk write failed";
334 total_written += ret;
336 offset += write_size;
339 return total_written;
343 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
344 ftdi->writebuffer_chunksize = chunksize;
349 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
350 *chunksize = ftdi->writebuffer_chunksize;
355 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size) {
356 int offset = 0, ret = 1;
358 // everything we want is still in the readbuffer?
359 if (size <= ftdi->readbuffer_remaining) {
360 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
363 ftdi->readbuffer_remaining -= size;
364 ftdi->readbuffer_offset += size;
366 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
370 // something still in the readbuffer, but not enough to satisfy 'size'?
371 if (ftdi->readbuffer_remaining != 0) {
372 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
375 offset += ftdi->readbuffer_remaining;
377 // do the actual USB read
378 while (offset < size && ret > 0) {
379 ftdi->readbuffer_remaining = 0;
380 ftdi->readbuffer_offset = 0;
381 /* returns how much received */
382 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
385 ftdi->error_str = "bulk read failed";
390 // skip FTDI status bytes.
391 // Maybe stored in the future to enable modem use
392 ftdi->readbuffer_offset += 2;
394 } else if (ret <= 2) {
395 // no more data to read?
399 // data still fits in buf?
400 if (offset+ret <= size) {
401 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
402 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
405 /* Did we read exactly the right amount of bytes? */
409 // only copy part of the data or size <= readbuffer_chunksize
410 int part_size = size-offset;
411 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
413 ftdi->readbuffer_offset += part_size;
414 ftdi->readbuffer_remaining = ret-part_size;
417 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
418 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
429 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize) {
430 // Invalidate all remaining data
431 ftdi->readbuffer_offset = 0;
432 ftdi->readbuffer_remaining = 0;
434 unsigned char *new_buf;
435 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL) {
436 ftdi->error_str = "out of memory for readbuffer";
440 ftdi->readbuffer = new_buf;
441 ftdi->readbuffer_chunksize = chunksize;
447 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize) {
448 *chunksize = ftdi->readbuffer_chunksize;
454 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask) {
455 unsigned short usb_val;
457 usb_val = bitmask; // low byte: bitmask
458 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
459 usb_val |= (ftdi->bitbang_mode << 8);
461 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
462 ftdi->error_str = "Unable to enter bitbang mode. Perhaps not a BM type chip?";
465 ftdi->bitbang_enabled = 1;
470 int ftdi_disable_bitbang(struct ftdi_context *ftdi) {
471 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
472 ftdi->error_str = "Unable to leave bitbang mode. Perhaps not a BM type chip?";
476 ftdi->bitbang_enabled = 0;
481 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins) {
482 unsigned short usb_val;
483 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
484 ftdi->error_str = "Read pins failed";
488 *pins = (unsigned char)usb_val;
493 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency) {
494 unsigned short usb_val;
497 ftdi->error_str = "Latency out of range. Only valid for 1-255";
502 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0) {
503 ftdi->error_str = "Unable to set latency timer";
510 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency) {
511 unsigned short usb_val;
512 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1) {
513 ftdi->error_str = "Reading latency timer failed";
517 *latency = (unsigned char)usb_val;
522 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom) {
523 eeprom->vendor_id = 0x0403;
524 eeprom->product_id = 0x6001;
526 eeprom->self_powered = 1;
527 eeprom->remote_wakeup = 1;
528 eeprom->BM_type_chip = 1;
530 eeprom->in_is_isochronous = 0;
531 eeprom->out_is_isochronous = 0;
532 eeprom->suspend_pull_downs = 0;
534 eeprom->use_serial = 0;
535 eeprom->change_usb_version = 0;
536 eeprom->usb_version = 0x0200;
537 eeprom->max_power = 0;
539 eeprom->manufacturer = NULL;
540 eeprom->product = NULL;
541 eeprom->serial = NULL;
546 ftdi_eeprom_build return codes:
547 positive value: used eeprom size
548 -1: eeprom size (128 bytes) exceeded by custom strings
550 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output) {
552 unsigned short checksum, value;
553 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
556 if (eeprom->manufacturer != NULL)
557 manufacturer_size = strlen(eeprom->manufacturer);
558 if (eeprom->product != NULL)
559 product_size = strlen(eeprom->product);
560 if (eeprom->serial != NULL)
561 serial_size = strlen(eeprom->serial);
563 size_check = 128; // eeprom is 128 bytes
564 size_check -= 28; // 28 are always in use (fixed)
565 size_check -= manufacturer_size*2;
566 size_check -= product_size*2;
567 size_check -= serial_size*2;
569 // eeprom size exceeded?
574 memset (output, 0, 128);
576 // Addr 00: Stay 00 00
577 // Addr 02: Vendor ID
578 output[0x02] = eeprom->vendor_id;
579 output[0x03] = eeprom->vendor_id >> 8;
581 // Addr 04: Product ID
582 output[0x04] = eeprom->product_id;
583 output[0x05] = eeprom->product_id >> 8;
585 // Addr 06: Device release number (0400h for BM features)
588 if (eeprom->BM_type_chip == 1)
593 // Addr 08: Config descriptor
594 // Bit 1: remote wakeup if 1
595 // Bit 0: self powered if 1
598 if (eeprom->self_powered == 1)
600 if (eeprom->remote_wakeup == 1)
604 // Addr 09: Max power consumption: max power = value * 2 mA
605 output[0x09] = eeprom->max_power;
608 // Addr 0A: Chip configuration
609 // Bit 7: 0 - reserved
610 // Bit 6: 0 - reserved
611 // Bit 5: 0 - reserved
612 // Bit 4: 1 - Change USB version
613 // Bit 3: 1 - Use the serial number string
614 // Bit 2: 1 - Enable suspend pull downs for lower power
615 // Bit 1: 1 - Out EndPoint is Isochronous
616 // Bit 0: 1 - In EndPoint is Isochronous
619 if (eeprom->in_is_isochronous == 1)
621 if (eeprom->out_is_isochronous == 1)
623 if (eeprom->suspend_pull_downs == 1)
625 if (eeprom->use_serial == 1)
627 if (eeprom->change_usb_version == 1)
634 // Addr 0C: USB version low byte when 0x0A bit 4 is set
635 // Addr 0D: USB version high byte when 0x0A bit 4 is set
636 if (eeprom->change_usb_version == 1) {
637 output[0x0C] = eeprom->usb_version;
638 output[0x0D] = eeprom->usb_version >> 8;
642 // Addr 0E: Offset of the manufacturer string + 0x80
643 output[0x0E] = 0x14 + 0x80;
645 // Addr 0F: Length of manufacturer string
646 output[0x0F] = manufacturer_size*2 + 2;
648 // Addr 10: Offset of the product string + 0x80, calculated later
649 // Addr 11: Length of product string
650 output[0x11] = product_size*2 + 2;
652 // Addr 12: Offset of the serial string + 0x80, calculated later
653 // Addr 13: Length of serial string
654 output[0x13] = serial_size*2 + 2;
657 output[0x14] = manufacturer_size*2 + 2;
658 output[0x15] = 0x03; // type: string
662 // Output manufacturer
663 for (j = 0; j < manufacturer_size; j++) {
664 output[i] = eeprom->manufacturer[j], i++;
665 output[i] = 0x00, i++;
668 // Output product name
669 output[0x10] = i + 0x80; // calculate offset
670 output[i] = product_size*2 + 2, i++;
671 output[i] = 0x03, i++;
672 for (j = 0; j < product_size; j++) {
673 output[i] = eeprom->product[j], i++;
674 output[i] = 0x00, i++;
678 output[0x12] = i + 0x80; // calculate offset
679 output[i] = serial_size*2 + 2, i++;
680 output[i] = 0x03, i++;
681 for (j = 0; j < serial_size; j++) {
682 output[i] = eeprom->serial[j], i++;
683 output[i] = 0x00, i++;
686 // calculate checksum
689 for (i = 0; i < 63; i++) {
691 value += output[(i*2)+1] << 8;
693 checksum = value^checksum;
694 checksum = (checksum << 1) | (checksum >> 15);
697 output[0x7E] = checksum;
698 output[0x7F] = checksum >> 8;
704 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
707 for (i = 0; i < 64; i++) {
708 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2) {
709 ftdi->error_str = "Reading eeprom failed";
718 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom) {
719 unsigned short usb_val;
722 for (i = 0; i < 64; i++) {
723 usb_val = eeprom[i*2];
724 usb_val += eeprom[(i*2)+1] << 8;
725 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0) {
726 ftdi->error_str = "Unable to write eeprom";
735 int ftdi_erase_eeprom(struct ftdi_context *ftdi) {
736 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0) {
737 ftdi->error_str = "Unable to erase eeprom";