39 #ifdef LIBFTDI_LINUX_ASYNC_MODE
40 #include <sys/ioctl.h>
42 #include <sys/select.h>
43 #include <sys/types.h>
45 #include <linux/usbdevice_fs.h>
48 #define ftdi_error_return(code, str) do { \
49 ftdi->error_str = str; \
63 static int ftdi_usb_close_internal (
struct ftdi_context *ftdi)
69 ret = usb_close (ftdi->
usb_dev);
112 #ifdef LIBFTDI_LINUX_ASYNC_MODE
209 ftdi_usb_close_internal (ftdi);
268 struct usb_device *dev;
272 if (usb_find_busses() < 0)
274 if (usb_find_devices() < 0)
279 for (bus = usb_get_busses(); bus; bus = bus->next)
281 for (dev = bus->devices; dev; dev = dev->next)
283 if (dev->descriptor.idVendor == vendor
284 && dev->descriptor.idProduct == product)
290 (*curdev)->next = NULL;
291 (*curdev)->dev =
dev;
293 curdev = &(*curdev)->
next;
311 for (curdev = *devlist; curdev != NULL;)
358 char * manufacturer,
int mnf_len,
char * description,
int desc_len,
char * serial,
int serial_len)
360 if ((ftdi==NULL) || (dev==NULL))
363 if (!(ftdi->
usb_dev = usb_open(dev)))
366 if (manufacturer != NULL)
368 if (usb_get_string_simple(ftdi->
usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
370 ftdi_usb_close_internal (ftdi);
375 if (description != NULL)
377 if (usb_get_string_simple(ftdi->
usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
379 ftdi_usb_close_internal (ftdi);
386 if (usb_get_string_simple(ftdi->
usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
388 ftdi_usb_close_internal (ftdi);
393 if (ftdi_usb_close_internal (ftdi) != 0)
405 static unsigned int _ftdi_determine_max_packet_size(
struct ftdi_context *ftdi,
struct usb_device *
dev)
407 unsigned int packet_size;
410 if (ftdi == NULL || dev == NULL)
421 if (dev->descriptor.bNumConfigurations > 0 && dev->config)
423 struct usb_config_descriptor config = dev->config[0];
425 if (ftdi->
interface < config.bNumInterfaces)
427 struct usb_interface interface = config.interface[ftdi->
interface];
428 if (interface.num_altsetting > 0)
430 struct usb_interface_descriptor descriptor = interface.altsetting[0];
431 if (descriptor.bNumEndpoints > 0)
433 packet_size = descriptor.endpoint[0].wMaxPacketSize;
458 int detach_errno = 0;
464 if (!(ftdi->
usb_dev = usb_open(dev)))
467 #ifdef LIBUSB_HAS_GET_DRIVER_NP
475 if (usb_detach_kernel_driver_np(ftdi->
usb_dev, ftdi->
interface) != 0 && errno != ENODATA)
476 detach_errno = errno;
484 if (dev->descriptor.bNumConfigurations > 0)
488 config_val = dev->config[0].bConfigurationValue;
490 if (usb_set_configuration(ftdi->
usb_dev, config_val) &&
493 ftdi_usb_close_internal (ftdi);
494 if (detach_errno == EPERM)
500 ftdi_error_return(-3,
"unable to set usb configuration. Make sure the default FTDI driver is not in use");
508 ftdi_usb_close_internal (ftdi);
509 if (detach_errno == EPERM)
515 ftdi_error_return(-5,
"unable to claim usb device. Make sure the default FTDI driver is not in use");
521 ftdi_usb_close_internal (ftdi);
527 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
528 && dev->descriptor.iSerialNumber == 0))
530 else if (dev->descriptor.bcdDevice == 0x200)
532 else if (dev->descriptor.bcdDevice == 0x500)
534 else if (dev->descriptor.bcdDevice == 0x600)
536 else if (dev->descriptor.bcdDevice == 0x700)
538 else if (dev->descriptor.bcdDevice == 0x800)
559 ftdi_usb_close_internal (ftdi);
603 const char* description,
const char* serial)
633 const char* description,
const char* serial,
unsigned int index)
636 struct usb_device *dev;
641 if (usb_find_busses() < 0)
643 if (usb_find_devices() < 0)
649 for (bus = usb_get_busses(); bus; bus = bus->next)
651 for (dev = bus->devices; dev; dev = dev->next)
653 if (dev->descriptor.idVendor == vendor
654 && dev->descriptor.idProduct == product)
656 if (!(ftdi->
usb_dev = usb_open(dev)))
659 if (description != NULL)
661 if (usb_get_string_simple(ftdi->
usb_dev, dev->descriptor.iProduct,
string,
sizeof(
string)) <= 0)
663 ftdi_usb_close_internal (ftdi);
666 if (strncmp(
string, description,
sizeof(
string)) != 0)
668 if (ftdi_usb_close_internal (ftdi) != 0)
675 if (usb_get_string_simple(ftdi->
usb_dev, dev->descriptor.iSerialNumber,
string,
sizeof(
string)) <= 0)
677 ftdi_usb_close_internal (ftdi);
680 if (strncmp(
string, serial,
sizeof(
string)) != 0)
682 if (ftdi_usb_close_internal (ftdi) != 0)
688 if (ftdi_usb_close_internal (ftdi) != 0)
738 if (description[0] == 0 || description[1] !=
':')
741 if (description[0] ==
'd')
744 struct usb_device *dev;
748 if (usb_find_busses() < 0)
750 if (usb_find_devices() < 0)
753 for (bus = usb_get_busses(); bus; bus = bus->next)
755 for (dev = bus->devices; dev; dev = dev->next)
758 const char *desc = description + 2;
759 size_t len = strlen(bus->dirname);
760 if (strncmp(desc, bus->dirname, len))
766 if (strcmp(desc, dev->filename))
775 else if (description[0] ==
'i' || description[0] ==
's')
778 unsigned int product;
779 unsigned int index=0;
780 const char *serial=NULL;
781 const char *startp, *endp;
784 startp=description+2;
785 vendor=strtoul((
char*)startp,(
char**)&endp,0);
786 if (*endp !=
':' || endp == startp || errno != 0)
790 product=strtoul((
char*)startp,(
char**)&endp,0);
791 if (endp == startp || errno != 0)
794 if (description[0] ==
'i' && *endp != 0)
801 index=strtoul((
char*)startp,(
char**)&endp,0);
802 if (*endp != 0 || endp == startp || errno != 0)
805 if (description[0] ==
's')
833 if (ftdi == NULL || ftdi->
usb_dev == NULL)
859 if (ftdi == NULL || ftdi->
usb_dev == NULL)
885 if (ftdi == NULL || ftdi->
usb_dev == NULL)
910 if (ftdi == NULL || ftdi->
usb_dev == NULL)
943 #ifdef LIBFTDI_LINUX_ASYNC_MODE
952 if (ftdi_usb_close_internal (ftdi) != 0)
963 static int ftdi_convert_baudrate(
int baudrate,
struct ftdi_context *ftdi,
964 unsigned short *value,
unsigned short *index)
966 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
967 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
968 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
969 int divisor, best_divisor, best_baud, best_baud_diff;
970 unsigned long encoded_divisor;
979 divisor = 24000000 / baudrate;
984 divisor -= am_adjust_dn[divisor & 7];
991 for (i = 0; i < 2; i++)
993 int try_divisor = divisor + i;
998 if (try_divisor <= 8)
1003 else if (ftdi->
type !=
TYPE_AM && try_divisor < 12)
1008 else if (divisor < 16)
1018 try_divisor += am_adjust_up[try_divisor & 7];
1019 if (try_divisor > 0x1FFF8)
1022 try_divisor = 0x1FFF8;
1027 if (try_divisor > 0x1FFFF)
1030 try_divisor = 0x1FFFF;
1035 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1037 if (baud_estimate < baudrate)
1039 baud_diff = baudrate - baud_estimate;
1043 baud_diff = baud_estimate - baudrate;
1045 if (i == 0 || baud_diff < best_baud_diff)
1048 best_divisor = try_divisor;
1049 best_baud = baud_estimate;
1050 best_baud_diff = baud_diff;
1059 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1061 if (encoded_divisor == 1)
1063 encoded_divisor = 0;
1065 else if (encoded_divisor == 0x4001)
1067 encoded_divisor = 1;
1070 *value = (
unsigned short)(encoded_divisor & 0xFFFF);
1073 *index = (
unsigned short)(encoded_divisor >> 8);
1075 *index |= ftdi->
index;
1078 *index = (
unsigned short)(encoded_divisor >> 16);
1097 unsigned short value, index;
1098 int actual_baudrate;
1100 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1105 baudrate = baudrate*4;
1108 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1109 if (actual_baudrate <= 0)
1113 if ((actual_baudrate * 2 < baudrate )
1114 || ((actual_baudrate < baudrate)
1115 ? (actual_baudrate * 21 < baudrate * 20)
1116 : (baudrate * 21 < actual_baudrate * 20)))
1117 ftdi_error_return (-1,
"Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1164 unsigned short value = bits;
1166 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1172 value |= (0x00 << 8);
1175 value |= (0x01 << 8);
1178 value |= (0x02 << 8);
1181 value |= (0x03 << 8);
1184 value |= (0x04 << 8);
1191 value |= (0x00 << 11);
1194 value |= (0x01 << 11);
1197 value |= (0x02 << 11);
1204 value |= (0x00 << 14);
1207 value |= (0x01 << 14);
1234 int total_written = 0;
1236 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1239 while (offset < size)
1243 if (offset+write_size > size)
1244 write_size = size-offset;
1250 total_written += ret;
1251 offset += write_size;
1254 return total_written;
1257 #ifdef LIBFTDI_LINUX_ASYNC_MODE
1258 #ifdef USB_CLASS_PTP
1259 #error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1273 static int _usb_get_async_urbs_pending(
struct ftdi_context *ftdi)
1275 struct usbdevfs_urb *urb;
1299 static void _usb_async_cleanup(
struct ftdi_context *ftdi,
int wait_for_more,
int timeout_msec)
1302 struct usbdevfs_urb *urb=NULL;
1311 tv.tv_sec = timeout_msec / 1000;
1312 tv.tv_usec = (timeout_msec % 1000) * 1000;
1316 while (_usb_get_async_urbs_pending(ftdi)
1317 && (ret = ioctl(ftdi->
usb_dev->
fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1320 if (keep_going && !wait_for_more)
1328 select(ftdi->
usb_dev->
fd+1, NULL, &writefds, NULL, &tv);
1331 if (ret == 0 && urb != NULL)
1366 static int _usb_bulk_write_async(
struct ftdi_context *ftdi,
int ep,
char *bytes,
int size)
1368 struct usbdevfs_urb *urb;
1369 int bytesdone = 0, requested;
1370 int ret, cleanup_count;
1377 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1398 requested = size - bytesdone;
1399 if (requested > 4096)
1402 memset(urb,0,
sizeof(urb));
1404 urb->type = USBDEVFS_URB_TYPE_BULK;
1407 urb->buffer = bytes + bytesdone;
1408 urb->buffer_length = requested;
1410 urb->actual_length = 0;
1411 urb->number_of_packets = 0;
1412 urb->usercontext = 0;
1416 ret = ioctl(ftdi->
usb_dev->
fd, USBDEVFS_SUBMITURB, urb);
1418 while (ret < 0 && errno == EINTR);
1422 bytesdone += requested;
1424 while (bytesdone < size);
1451 int total_written = 0;
1453 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1456 while (offset < size)
1460 if (offset+write_size > size)
1461 write_size = size-offset;
1463 ret = _usb_bulk_write_async(ftdi, ftdi->
in_ep, buf+offset, write_size);
1467 total_written += ret;
1468 offset += write_size;
1471 return total_written;
1473 #endif // LIBFTDI_LINUX_ASYNC_MODE
1529 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
1532 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1537 if (packet_size == 0)
1541 if (size <= ftdi->readbuffer_remaining)
1562 while (offset < size && ret > 0)
1575 num_of_chunks = ret / packet_size;
1576 chunk_remains = ret % packet_size;
1582 if (ret > packet_size - 2)
1584 for (i = 1; i < num_of_chunks; i++)
1588 if (chunk_remains > 2)
1593 ret -= 2*num_of_chunks;
1596 ret -= 2*(num_of_chunks-1)+chunk_remains;
1607 if (offset+ret <= size)
1622 int part_size = size-offset;
1627 offset += part_size;
1654 unsigned char *new_buf;
1663 if ((new_buf = (
unsigned char *)realloc(ftdi->
readbuffer, chunksize)) == NULL)
1706 unsigned short usb_val;
1708 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1718 ftdi_error_return(-1,
"unable to enter bitbang mode. Perhaps not a BM type chip?");
1735 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1739 ftdi_error_return(-1,
"unable to leave bitbang mode. Perhaps not a BM type chip?");
1759 unsigned short usb_val;
1761 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1765 usb_val |= (mode << 8);
1767 ftdi_error_return(-1,
"unable to configure bitbang mode. Perhaps selected mode not supported on your chip?");
1786 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1812 unsigned short usb_val;
1817 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1839 unsigned short usb_val;
1841 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1847 *latency = (
unsigned char)usb_val;
1895 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1901 *status = (usb_val[1] << 8) | usb_val[0];
1919 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1942 unsigned short usb_val;
1944 if (ftdi == NULL || ftdi->
usb_dev == NULL)
1972 unsigned short usb_val;
1974 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2003 unsigned short usb_val;
2005 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2038 unsigned char eventch,
unsigned char enable)
2040 unsigned short usb_val;
2042 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2067 unsigned char errorch,
unsigned char enable)
2069 unsigned short usb_val;
2071 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2152 if (eeprom->
serial != 0) {
2172 unsigned short checksum, value;
2173 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2182 product_size = strlen(eeprom->
product);
2183 if (eeprom->
serial != NULL)
2184 serial_size = strlen(eeprom->
serial);
2186 size_check = eeprom->
size;
2193 if (eeprom->
size>=256)size_check = 120;
2194 size_check -= manufacturer_size*2;
2195 size_check -= product_size*2;
2196 size_check -= serial_size*2;
2203 memset (output, 0, eeprom->
size);
2215 output[0x06] = 0x00;
2218 output[0x07] = 0x04;
2220 output[0x07] = 0x02;
2261 output[0x0B] = 0x00;
2274 output[0x0F] = manufacturer_size*2 + 2;
2278 output[0x11] = product_size*2 + 2;
2282 output[0x13] = serial_size*2 + 2;
2286 if (eeprom->
size>=256) i = 0x80;
2290 output[0x0E] = i | 0x80;
2291 output[i++] = manufacturer_size*2 + 2;
2293 for (j = 0; j < manufacturer_size; j++)
2296 output[i] = 0x00, i++;
2300 output[0x10] = i | 0x80;
2301 output[i] = product_size*2 + 2, i++;
2302 output[i] = 0x03, i++;
2303 for (j = 0; j < product_size; j++)
2305 output[i] = eeprom->
product[j], i++;
2306 output[i] = 0x00, i++;
2310 output[0x12] = i | 0x80;
2311 output[i] = serial_size*2 + 2, i++;
2312 output[i] = 0x03, i++;
2313 for (j = 0; j < serial_size; j++)
2315 output[i] = eeprom->
serial[j], i++;
2316 output[i] = 0x00, i++;
2322 for (i = 0; i < eeprom->
size/2-1; i++)
2324 value = output[i*2];
2325 value += output[(i*2)+1] << 8;
2327 checksum = value^checksum;
2328 checksum = (checksum << 1) | (checksum >> 15);
2331 output[eeprom->
size-2] = checksum;
2332 output[eeprom->
size-1] = checksum >> 8;
2353 unsigned short checksum, eeprom_checksum, value;
2354 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2356 int eeprom_size = 128;
2361 size_check = eeprom->
size;
2368 if (eeprom->
size>=256)size_check = 120;
2369 size_check -= manufacturer_size*2;
2370 size_check -= product_size*2;
2371 size_check -= serial_size*2;
2384 eeprom->
vendor_id = buf[0x02] + (buf[0x03] << 8);
2387 eeprom->
product_id = buf[0x04] + (buf[0x05] << 8);
2389 value = buf[0x06] + (buf[0x07]<<8);
2438 eeprom->
usb_version = buf[0x0C] + (buf[0x0D] << 8);
2443 manufacturer_size = buf[0x0F]/2;
2444 if (manufacturer_size > 0) eeprom->
manufacturer = malloc(manufacturer_size);
2449 product_size = buf[0x11]/2;
2450 if (product_size > 0) eeprom->
product = malloc(product_size);
2455 serial_size = buf[0x13]/2;
2456 if (serial_size > 0) eeprom->
serial = malloc(serial_size);
2457 else eeprom->
serial = NULL;
2460 i = buf[0x0E] & 0x7f;
2461 for (j=0;j<manufacturer_size-1;j++)
2468 i = buf[0x10] & 0x7f;
2469 for (j=0;j<product_size-1;j++)
2471 eeprom->
product[j] = buf[2*j+i+2];
2476 i = buf[0x12] & 0x7f;
2477 for (j=0;j<serial_size-1;j++)
2479 eeprom->
serial[j] = buf[2*j+i+2];
2481 eeprom->
serial[j] =
'\0';
2486 for (i = 0; i < eeprom_size/2-1; i++)
2489 value += buf[(i*2)+1] << 8;
2491 checksum = value^checksum;
2492 checksum = (checksum << 1) | (checksum >> 15);
2495 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2497 if (eeprom_checksum != checksum)
2499 fprintf(stderr,
"Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2519 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2542 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2559 static unsigned char ftdi_read_chipid_shift(
unsigned char value)
2561 return ((value & 1) << 1) |
2562 ((value & 2) << 5) |
2563 ((value & 4) >> 2) |
2564 ((value & 8) << 4) |
2565 ((value & 16) >> 1) |
2566 ((value & 32) >> 1) |
2567 ((value & 64) >> 4) |
2568 ((value & 128) >> 2);
2583 unsigned int a = 0, b = 0;
2585 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2590 a = a << 8 | a >> 8;
2593 b = b << 8 | b >> 8;
2594 a = (a << 16) | (b & 0xFFFF);
2595 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2596 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
2597 *chipid = a ^ 0xa5f0f7d1;
2619 int i=0,j,minsize=32;
2622 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2627 for (j = 0; i < maxsize/2 && j<size; j++)
2637 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
2655 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2678 unsigned short usb_val, status;
2681 if (ftdi == NULL || ftdi->
usb_dev == NULL)
2694 usb_val = eeprom[i*2];
2695 usb_val += eeprom[(i*2)+1] << 8;
2718 if (ftdi == NULL || ftdi->
usb_dev == NULL)