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 ***************************************************************************/
19 #include <sys/utsname.h>
23 #define ftdi_error_return(code, str) do { \
24 ftdi->error_str = str; \
29 /* ftdi_init return codes:
31 -1: couldn't allocate read buffer
33 int ftdi_init(struct ftdi_context *ftdi)
36 ftdi->usb_read_timeout = 5000;
37 ftdi->usb_write_timeout = 5000;
39 ftdi->type = TYPE_BM; /* chip type */
41 ftdi->bitbang_enabled = 0;
43 ftdi->readbuffer = NULL;
44 ftdi->readbuffer_offset = 0;
45 ftdi->readbuffer_remaining = 0;
46 ftdi->writebuffer_chunksize = 4096;
52 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
54 ftdi->error_str = NULL;
56 /* All fine. Now allocate the readbuffer */
57 return ftdi_read_data_set_chunksize(ftdi, 4096);
61 void ftdi_deinit(struct ftdi_context *ftdi)
63 if (ftdi->readbuffer != NULL) {
64 free(ftdi->readbuffer);
65 ftdi->readbuffer = NULL;
70 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
76 /* ftdi_usb_open return codes:
78 -1: usb_find_busses() failed
79 -2: usb_find_devices() failed
80 -3: usb device not found
81 -4: unable to open device
82 -5: unable to claim device
84 -7: set baudrate failed
85 -8: get product description failed
86 -9: get serial number failed
87 -10: unable to close device
89 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
91 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
94 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
95 const char* description, const char* serial)
98 struct usb_device *dev;
103 if (usb_find_busses() < 0)
104 ftdi_error_return(-1, "usb_find_busses() failed");
106 if (usb_find_devices() < 0)
107 ftdi_error_return(-2,"usb_find_devices() failed");
109 for (bus = usb_busses; bus; bus = bus->next) {
110 for (dev = bus->devices; dev; dev = dev->next) {
111 if (dev->descriptor.idVendor == vendor
112 && dev->descriptor.idProduct == product) {
113 if (!(ftdi->usb_dev = usb_open(dev)))
114 ftdi_error_return(-4, "usb_open() failed");
116 if (description != NULL) {
117 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
118 usb_close (ftdi->usb_dev);
119 ftdi_error_return(-8, "unable to fetch product description");
121 if (strncmp(string, description, sizeof(string)) != 0) {
122 if (usb_close (ftdi->usb_dev) < 0)
123 ftdi_error_return(-10, "product description not matching");
127 if (serial != NULL) {
128 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
129 usb_close (ftdi->usb_dev);
130 ftdi_error_return(-9, "unable to fetch serial number");
132 if (strncmp(string, serial, sizeof(string)) != 0) {
133 ftdi->error_str = "serial number not matching\n";
134 if (usb_close (ftdi->usb_dev) != 0)
135 ftdi_error_return(-10, "unable to fetch serial number");
140 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
141 usb_close (ftdi->usb_dev);
142 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
145 if (ftdi_usb_reset (ftdi) != 0) {
146 usb_close (ftdi->usb_dev);
147 ftdi_error_return(-6, "ftdi_usb_reset failed");
150 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
151 usb_close (ftdi->usb_dev);
152 ftdi_error_return(-7, "set baudrate failed");
155 // Try to guess chip type
156 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
157 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
158 && dev->descriptor.iSerialNumber == 0))
159 ftdi->type = TYPE_BM;
160 else if (dev->descriptor.bcdDevice == 0x200)
161 ftdi->type = TYPE_AM;
162 else if (dev->descriptor.bcdDevice == 0x500)
163 ftdi->type = TYPE_2232C;
165 ftdi_error_return(0, "all fine");
171 ftdi_error_return(-3, "device not found");
175 int ftdi_usb_reset(struct ftdi_context *ftdi)
177 #if defined(__linux__)
178 struct utsname kernelver;
179 int k_major, k_minor, k_myver;
182 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
183 ftdi_error_return(-1,"FTDI reset failed");
185 #if defined(__linux__)
186 /* Kernel 2.6 (maybe higher versions, too) need an additional usb_reset */
187 if (uname(&kernelver) == 0 && sscanf(kernelver.release, "%d.%d", &k_major, &k_minor) == 2) {
188 k_myver = k_major*10 + k_minor;
190 if (k_myver >= 26 && usb_reset(ftdi->usb_dev) != 0)
191 ftdi_error_return(-2, "USB reset failed");
195 // Invalidate data in the readbuffer
196 ftdi->readbuffer_offset = 0;
197 ftdi->readbuffer_remaining = 0;
202 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
204 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
205 ftdi_error_return(-1, "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_return(-2, "FTDI purge of TX buffer failed");
217 /* ftdi_usb_close return codes
219 -1: usb_release failed
222 int ftdi_usb_close(struct ftdi_context *ftdi)
226 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
229 if (usb_close (ftdi->usb_dev) != 0)
237 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
238 Function is only used internally
240 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
241 unsigned short *value, unsigned short *index)
243 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
244 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
245 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
246 int divisor, best_divisor, best_baud, best_baud_diff;
247 unsigned long encoded_divisor;
255 divisor = 24000000 / baudrate;
257 if (ftdi->type == TYPE_AM) {
258 // Round down to supported fraction (AM only)
259 divisor -= am_adjust_dn[divisor & 7];
262 // Try this divisor and the one above it (because division rounds down)
266 for (i = 0; i < 2; i++) {
267 int try_divisor = divisor + i;
271 // Round up to supported divisor value
272 if (try_divisor <= 8) {
273 // Round up to minimum supported divisor
275 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
276 // BM doesn't support divisors 9 through 11 inclusive
278 } else if (divisor < 16) {
279 // AM doesn't support divisors 9 through 15 inclusive
282 if (ftdi->type == TYPE_AM) {
283 // Round up to supported fraction (AM only)
284 try_divisor += am_adjust_up[try_divisor & 7];
285 if (try_divisor > 0x1FFF8) {
286 // Round down to maximum supported divisor value (for AM)
287 try_divisor = 0x1FFF8;
290 if (try_divisor > 0x1FFFF) {
291 // Round down to maximum supported divisor value (for BM)
292 try_divisor = 0x1FFFF;
296 // Get estimated baud rate (to nearest integer)
297 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
298 // Get absolute difference from requested baud rate
299 if (baud_estimate < baudrate) {
300 baud_diff = baudrate - baud_estimate;
302 baud_diff = baud_estimate - baudrate;
304 if (i == 0 || baud_diff < best_baud_diff) {
305 // Closest to requested baud rate so far
306 best_divisor = try_divisor;
307 best_baud = baud_estimate;
308 best_baud_diff = baud_diff;
309 if (baud_diff == 0) {
310 // Spot on! No point trying
315 // Encode the best divisor value
316 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
317 // Deal with special cases for encoded value
318 if (encoded_divisor == 1) {
319 encoded_divisor = 0; // 3000000 baud
320 } else if (encoded_divisor == 0x4001) {
321 encoded_divisor = 1; // 2000000 baud (BM only)
323 // Split into "value" and "index" values
324 *value = (unsigned short)(encoded_divisor & 0xFFFF);
325 if(ftdi->type == TYPE_2232C) {
326 *index = (unsigned short)(encoded_divisor >> 8);
328 *index |= ftdi->interface;
331 *index = (unsigned short)(encoded_divisor >> 16);
333 // Return the nearest baud rate
338 ftdi_set_baudrate return codes:
341 -2: setting baudrate failed
343 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
345 unsigned short value, index;
348 if (ftdi->bitbang_enabled) {
349 baudrate = baudrate*4;
352 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
353 if (actual_baudrate <= 0)
354 ftdi_error_return (-1, "Silly baudrate <= 0.");
356 // Check within tolerance (about 5%)
357 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
358 || ((actual_baudrate < baudrate)
359 ? (actual_baudrate * 21 < baudrate * 20)
360 : (baudrate * 21 < actual_baudrate * 20)))
361 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
363 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
364 ftdi_error_return (-2, "Setting new baudrate failed");
366 ftdi->baudrate = baudrate;
371 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
375 int total_written = 0;
377 while (offset < size) {
378 int write_size = ftdi->writebuffer_chunksize;
380 if (offset+write_size > size)
381 write_size = size-offset;
383 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
385 ftdi_error_return(ret, "usb bulk write failed");
387 total_written += ret;
388 offset += write_size;
391 return total_written;
395 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
397 ftdi->writebuffer_chunksize = chunksize;
402 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
404 *chunksize = ftdi->writebuffer_chunksize;
409 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
411 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
413 // everything we want is still in the readbuffer?
414 if (size <= ftdi->readbuffer_remaining) {
415 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
418 ftdi->readbuffer_remaining -= size;
419 ftdi->readbuffer_offset += size;
421 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
425 // something still in the readbuffer, but not enough to satisfy 'size'?
426 if (ftdi->readbuffer_remaining != 0) {
427 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
430 offset += ftdi->readbuffer_remaining;
432 // do the actual USB read
433 while (offset < size && ret > 0) {
434 ftdi->readbuffer_remaining = 0;
435 ftdi->readbuffer_offset = 0;
436 /* returns how much received */
437 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
439 ftdi_error_return(ret, "usb bulk read failed");
442 // skip FTDI status bytes.
443 // Maybe stored in the future to enable modem use
444 num_of_chunks = ret / 64;
445 chunk_remains = ret % 64;
446 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
448 ftdi->readbuffer_offset += 2;
452 for (i = 1; i < num_of_chunks; i++)
453 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
454 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
456 if (chunk_remains > 2) {
457 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
458 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
460 ret -= 2*num_of_chunks;
462 ret -= 2*(num_of_chunks-1)+chunk_remains;
464 } else if (ret <= 2) {
465 // no more data to read?
469 // data still fits in buf?
470 if (offset+ret <= size) {
471 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
472 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
475 /* Did we read exactly the right amount of bytes? */
479 // only copy part of the data or size <= readbuffer_chunksize
480 int part_size = size-offset;
481 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
483 ftdi->readbuffer_offset += part_size;
484 ftdi->readbuffer_remaining = ret-part_size;
487 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
488 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
499 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
501 unsigned char *new_buf;
503 // Invalidate all remaining data
504 ftdi->readbuffer_offset = 0;
505 ftdi->readbuffer_remaining = 0;
507 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
508 ftdi_error_return(-1, "out of memory for readbuffer");
510 ftdi->readbuffer = new_buf;
511 ftdi->readbuffer_chunksize = chunksize;
517 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
519 *chunksize = ftdi->readbuffer_chunksize;
525 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
527 unsigned short usb_val;
529 usb_val = bitmask; // low byte: bitmask
530 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
531 usb_val |= (ftdi->bitbang_mode << 8);
533 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
534 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
536 ftdi->bitbang_enabled = 1;
541 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
543 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
544 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
546 ftdi->bitbang_enabled = 0;
551 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
553 unsigned short usb_val;
554 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
555 ftdi_error_return(-1, "read pins failed");
557 *pins = (unsigned char)usb_val;
562 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
564 unsigned short usb_val;
567 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
570 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
571 ftdi_error_return(-2, "unable to set latency timer");
577 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
579 unsigned short usb_val;
580 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
581 ftdi_error_return(-1, "reading latency timer failed");
583 *latency = (unsigned char)usb_val;
588 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
590 eeprom->vendor_id = 0x0403;
591 eeprom->product_id = 0x6001;
593 eeprom->self_powered = 1;
594 eeprom->remote_wakeup = 1;
595 eeprom->BM_type_chip = 1;
597 eeprom->in_is_isochronous = 0;
598 eeprom->out_is_isochronous = 0;
599 eeprom->suspend_pull_downs = 0;
601 eeprom->use_serial = 0;
602 eeprom->change_usb_version = 0;
603 eeprom->usb_version = 0x0200;
604 eeprom->max_power = 0;
606 eeprom->manufacturer = NULL;
607 eeprom->product = NULL;
608 eeprom->serial = NULL;
613 ftdi_eeprom_build return codes:
614 positive value: used eeprom size
615 -1: eeprom size (128 bytes) exceeded by custom strings
617 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
620 unsigned short checksum, value;
621 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
624 if (eeprom->manufacturer != NULL)
625 manufacturer_size = strlen(eeprom->manufacturer);
626 if (eeprom->product != NULL)
627 product_size = strlen(eeprom->product);
628 if (eeprom->serial != NULL)
629 serial_size = strlen(eeprom->serial);
631 size_check = 128; // eeprom is 128 bytes
632 size_check -= 28; // 28 are always in use (fixed)
633 size_check -= manufacturer_size*2;
634 size_check -= product_size*2;
635 size_check -= serial_size*2;
637 // eeprom size exceeded?
642 memset (output, 0, 128);
644 // Addr 00: Stay 00 00
645 // Addr 02: Vendor ID
646 output[0x02] = eeprom->vendor_id;
647 output[0x03] = eeprom->vendor_id >> 8;
649 // Addr 04: Product ID
650 output[0x04] = eeprom->product_id;
651 output[0x05] = eeprom->product_id >> 8;
653 // Addr 06: Device release number (0400h for BM features)
656 if (eeprom->BM_type_chip == 1)
661 // Addr 08: Config descriptor
662 // Bit 1: remote wakeup if 1
663 // Bit 0: self powered if 1
666 if (eeprom->self_powered == 1)
668 if (eeprom->remote_wakeup == 1)
672 // Addr 09: Max power consumption: max power = value * 2 mA
673 output[0x09] = eeprom->max_power;
676 // Addr 0A: Chip configuration
677 // Bit 7: 0 - reserved
678 // Bit 6: 0 - reserved
679 // Bit 5: 0 - reserved
680 // Bit 4: 1 - Change USB version
681 // Bit 3: 1 - Use the serial number string
682 // Bit 2: 1 - Enable suspend pull downs for lower power
683 // Bit 1: 1 - Out EndPoint is Isochronous
684 // Bit 0: 1 - In EndPoint is Isochronous
687 if (eeprom->in_is_isochronous == 1)
689 if (eeprom->out_is_isochronous == 1)
691 if (eeprom->suspend_pull_downs == 1)
693 if (eeprom->use_serial == 1)
695 if (eeprom->change_usb_version == 1)
702 // Addr 0C: USB version low byte when 0x0A bit 4 is set
703 // Addr 0D: USB version high byte when 0x0A bit 4 is set
704 if (eeprom->change_usb_version == 1) {
705 output[0x0C] = eeprom->usb_version;
706 output[0x0D] = eeprom->usb_version >> 8;
710 // Addr 0E: Offset of the manufacturer string + 0x80
711 output[0x0E] = 0x14 + 0x80;
713 // Addr 0F: Length of manufacturer string
714 output[0x0F] = manufacturer_size*2 + 2;
716 // Addr 10: Offset of the product string + 0x80, calculated later
717 // Addr 11: Length of product string
718 output[0x11] = product_size*2 + 2;
720 // Addr 12: Offset of the serial string + 0x80, calculated later
721 // Addr 13: Length of serial string
722 output[0x13] = serial_size*2 + 2;
725 output[0x14] = manufacturer_size*2 + 2;
726 output[0x15] = 0x03; // type: string
730 // Output manufacturer
731 for (j = 0; j < manufacturer_size; j++) {
732 output[i] = eeprom->manufacturer[j], i++;
733 output[i] = 0x00, i++;
736 // Output product name
737 output[0x10] = i + 0x80; // calculate offset
738 output[i] = product_size*2 + 2, i++;
739 output[i] = 0x03, i++;
740 for (j = 0; j < product_size; j++) {
741 output[i] = eeprom->product[j], i++;
742 output[i] = 0x00, i++;
746 output[0x12] = i + 0x80; // calculate offset
747 output[i] = serial_size*2 + 2, i++;
748 output[i] = 0x03, i++;
749 for (j = 0; j < serial_size; j++) {
750 output[i] = eeprom->serial[j], i++;
751 output[i] = 0x00, i++;
754 // calculate checksum
757 for (i = 0; i < 63; i++) {
759 value += output[(i*2)+1] << 8;
761 checksum = value^checksum;
762 checksum = (checksum << 1) | (checksum >> 15);
765 output[0x7E] = checksum;
766 output[0x7F] = checksum >> 8;
772 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
776 for (i = 0; i < 64; i++) {
777 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
778 ftdi_error_return(-1, "reading eeprom failed");
785 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
787 unsigned short usb_val;
790 for (i = 0; i < 64; i++) {
791 usb_val = eeprom[i*2];
792 usb_val += eeprom[(i*2)+1] << 8;
793 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
794 ftdi_error_return(-1, "unable to write eeprom");
801 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
803 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
804 ftdi_error_return(-1, "unable to erase eeprom");
810 char *ftdi_get_error_string (struct ftdi_context *ftdi)
812 return ftdi->error_str;