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 ***************************************************************************/
23 #define ftdi_error_return(code, str) do { \
24 ftdi->error_str = str; \
30 Initializes a ftdi_context.
32 \param ftdi pointer to ftdi_context
35 \retval -1: couldn't allocate read buffer
37 \remark This should be called before all functions
39 int ftdi_init(struct ftdi_context *ftdi)
42 ftdi->usb_read_timeout = 5000;
43 ftdi->usb_write_timeout = 5000;
45 ftdi->type = TYPE_BM; /* chip type */
47 ftdi->bitbang_enabled = 0;
49 ftdi->readbuffer = NULL;
50 ftdi->readbuffer_offset = 0;
51 ftdi->readbuffer_remaining = 0;
52 ftdi->writebuffer_chunksize = 4096;
58 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
60 ftdi->error_str = NULL;
62 /* All fine. Now allocate the readbuffer */
63 return ftdi_read_data_set_chunksize(ftdi, 4096);
67 Open selected channels on a chip, otherwise use first channel.
69 \param ftdi pointer to ftdi_context
70 \param interface Interface to use for FT2232C chips.
73 \retval -1: unknown interface
75 int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
80 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
84 ftdi->index = INTERFACE_B;
89 ftdi_error_return(-1, "Unknown interface");
95 Deinitializes a ftdi_context.
97 \param ftdi pointer to ftdi_context
99 void ftdi_deinit(struct ftdi_context *ftdi)
101 if (ftdi->readbuffer != NULL) {
102 free(ftdi->readbuffer);
103 ftdi->readbuffer = NULL;
108 Use an already open libusb device.
110 \param ftdi pointer to ftdi_context
111 \param usb libusb usb_dev_handle to use
113 void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
120 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
121 needs to be deallocated by ftdi_list_free() after use.
123 \param ftdi pointer to ftdi_context
124 \param devlist Pointer where to store list of found devices
125 \param vendor Vendor ID to search for
126 \param product Product ID to search for
128 \retval >0: number of devices found
129 \retval -1: usb_find_busses() failed
130 \retval -2: usb_find_devices() failed
131 \retval -3: out of memory
133 int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
135 struct ftdi_device_list **curdev;
137 struct usb_device *dev;
141 if (usb_find_busses() < 0)
142 ftdi_error_return(-1, "usb_find_busses() failed");
143 if (usb_find_devices() < 0)
144 ftdi_error_return(-2, "usb_find_devices() failed");
147 for (bus = usb_busses; bus; bus = bus->next) {
148 for (dev = bus->devices; dev; dev = dev->next) {
149 if (dev->descriptor.idVendor == vendor
150 && dev->descriptor.idProduct == product)
152 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
154 ftdi_error_return(-3, "out of memory");
156 (*curdev)->next = NULL;
157 (*curdev)->dev = dev;
159 curdev = &(*curdev)->next;
169 Frees a usb device list.
171 \param devlist USB device list created by ftdi_usb_find_all()
173 void ftdi_list_free(struct ftdi_device_list **devlist)
175 struct ftdi_device_list **curdev;
176 for (; *devlist == NULL; devlist = curdev) {
177 curdev = &(*devlist)->next;
185 Opens a ftdi device given by a usb_device.
187 \param ftdi pointer to ftdi_context
188 \param dev libusb usb_dev to use
191 \retval -4: unable to open device
192 \retval -5: unable to claim device
193 \retval -6: reset failed
194 \retval -7: set baudrate failed
196 int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
198 int detach_errno = 0;
199 if (!(ftdi->usb_dev = usb_open(dev)))
200 ftdi_error_return(-4, "usb_open() failed");
202 #ifdef LIBUSB_HAS_GET_DRIVER_NP
203 // Try to detach ftdi_sio kernel module
204 // Returns ENODATA if driver is not loaded
205 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
206 detach_errno = errno;
209 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
210 usb_close (ftdi->usb_dev);
211 if (detach_errno == EPERM) {
212 ftdi_error_return(-8, "inappropriate permissions on device!");
214 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
218 if (ftdi_usb_reset (ftdi) != 0) {
219 usb_close (ftdi->usb_dev);
220 ftdi_error_return(-6, "ftdi_usb_reset failed");
223 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
224 usb_close (ftdi->usb_dev);
225 ftdi_error_return(-7, "set baudrate failed");
228 // Try to guess chip type
229 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
230 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
231 && dev->descriptor.iSerialNumber == 0))
232 ftdi->type = TYPE_BM;
233 else if (dev->descriptor.bcdDevice == 0x200)
234 ftdi->type = TYPE_AM;
235 else if (dev->descriptor.bcdDevice == 0x500) {
236 ftdi->type = TYPE_2232C;
238 ftdi->index = INTERFACE_A;
241 ftdi_error_return(0, "all fine");
245 Opens the first device with a given vendor and product ids.
247 \param ftdi pointer to ftdi_context
248 \param vendor Vendor ID
249 \param product Product ID
251 \retval same as ftdi_usb_open_desc()
253 int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
255 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
259 Opens the first device with a given, vendor id, product id,
260 description and serial.
262 \param ftdi pointer to ftdi_context
263 \param vendor Vendor ID
264 \param product Product ID
265 \param description Description to search for. Use NULL if not needed.
266 \param serial Serial to search for. Use NULL if not needed.
269 \retval -1: usb_find_busses() failed
270 \retval -2: usb_find_devices() failed
271 \retval -3: usb device not found
272 \retval -4: unable to open device
273 \retval -5: unable to claim device
274 \retval -6: reset failed
275 \retval -7: set baudrate failed
276 \retval -8: get product description failed
277 \retval -9: get serial number failed
278 \retval -10: unable to close device
280 int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
281 const char* description, const char* serial)
284 struct usb_device *dev;
289 if (usb_find_busses() < 0)
290 ftdi_error_return(-1, "usb_find_busses() failed");
291 if (usb_find_devices() < 0)
292 ftdi_error_return(-2, "usb_find_devices() failed");
294 for (bus = usb_busses; bus; bus = bus->next) {
295 for (dev = bus->devices; dev; dev = dev->next) {
296 if (dev->descriptor.idVendor == vendor
297 && dev->descriptor.idProduct == product) {
298 if (!(ftdi->usb_dev = usb_open(dev)))
299 ftdi_error_return(-4, "usb_open() failed");
301 if (description != NULL) {
302 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
303 usb_close (ftdi->usb_dev);
304 ftdi_error_return(-8, "unable to fetch product description");
306 if (strncmp(string, description, sizeof(string)) != 0) {
307 if (usb_close (ftdi->usb_dev) != 0)
308 ftdi_error_return(-10, "unable to close device");
312 if (serial != NULL) {
313 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
314 usb_close (ftdi->usb_dev);
315 ftdi_error_return(-9, "unable to fetch serial number");
317 if (strncmp(string, serial, sizeof(string)) != 0) {
318 if (usb_close (ftdi->usb_dev) != 0)
319 ftdi_error_return(-10, "unable to close device");
324 if (usb_close (ftdi->usb_dev) != 0)
325 ftdi_error_return(-10, "unable to close device");
327 return ftdi_usb_open_dev(ftdi, dev);
333 ftdi_error_return(-3, "device not found");
337 Resets the ftdi device.
339 \param ftdi pointer to ftdi_context
342 \retval -1: FTDI reset failed
344 int ftdi_usb_reset(struct ftdi_context *ftdi)
346 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
347 ftdi_error_return(-1,"FTDI reset failed");
349 // Invalidate data in the readbuffer
350 ftdi->readbuffer_offset = 0;
351 ftdi->readbuffer_remaining = 0;
357 Clears the buffers on the chip.
359 \param ftdi pointer to ftdi_context
362 \retval -1: write buffer purge failed
363 \retval -2: read buffer purge failed
365 int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
367 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
368 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
370 // Invalidate data in the readbuffer
371 ftdi->readbuffer_offset = 0;
372 ftdi->readbuffer_remaining = 0;
374 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
375 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
381 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
383 \param ftdi pointer to ftdi_context
386 \retval -1: usb_release failed
387 \retval -2: usb_close failed
389 int ftdi_usb_close(struct ftdi_context *ftdi)
393 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
396 if (usb_close (ftdi->usb_dev) != 0)
403 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
404 Function is only used internally
406 static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
407 unsigned short *value, unsigned short *index)
409 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
410 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
411 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
412 int divisor, best_divisor, best_baud, best_baud_diff;
413 unsigned long encoded_divisor;
421 divisor = 24000000 / baudrate;
423 if (ftdi->type == TYPE_AM) {
424 // Round down to supported fraction (AM only)
425 divisor -= am_adjust_dn[divisor & 7];
428 // Try this divisor and the one above it (because division rounds down)
432 for (i = 0; i < 2; i++) {
433 int try_divisor = divisor + i;
437 // Round up to supported divisor value
438 if (try_divisor <= 8) {
439 // Round up to minimum supported divisor
441 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
442 // BM doesn't support divisors 9 through 11 inclusive
444 } else if (divisor < 16) {
445 // AM doesn't support divisors 9 through 15 inclusive
448 if (ftdi->type == TYPE_AM) {
449 // Round up to supported fraction (AM only)
450 try_divisor += am_adjust_up[try_divisor & 7];
451 if (try_divisor > 0x1FFF8) {
452 // Round down to maximum supported divisor value (for AM)
453 try_divisor = 0x1FFF8;
456 if (try_divisor > 0x1FFFF) {
457 // Round down to maximum supported divisor value (for BM)
458 try_divisor = 0x1FFFF;
462 // Get estimated baud rate (to nearest integer)
463 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
464 // Get absolute difference from requested baud rate
465 if (baud_estimate < baudrate) {
466 baud_diff = baudrate - baud_estimate;
468 baud_diff = baud_estimate - baudrate;
470 if (i == 0 || baud_diff < best_baud_diff) {
471 // Closest to requested baud rate so far
472 best_divisor = try_divisor;
473 best_baud = baud_estimate;
474 best_baud_diff = baud_diff;
475 if (baud_diff == 0) {
476 // Spot on! No point trying
481 // Encode the best divisor value
482 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
483 // Deal with special cases for encoded value
484 if (encoded_divisor == 1) {
485 encoded_divisor = 0; // 3000000 baud
486 } else if (encoded_divisor == 0x4001) {
487 encoded_divisor = 1; // 2000000 baud (BM only)
489 // Split into "value" and "index" values
490 *value = (unsigned short)(encoded_divisor & 0xFFFF);
491 if(ftdi->type == TYPE_2232C) {
492 *index = (unsigned short)(encoded_divisor >> 8);
494 *index |= ftdi->index;
497 *index = (unsigned short)(encoded_divisor >> 16);
499 // Return the nearest baud rate
504 Sets the chip baud rate
506 \param ftdi pointer to ftdi_context
507 \param baudrate baud rate to set
510 \retval -1: invalid baudrate
511 \retval -2: setting baudrate failed
513 int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
515 unsigned short value, index;
518 if (ftdi->bitbang_enabled) {
519 baudrate = baudrate*4;
522 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
523 if (actual_baudrate <= 0)
524 ftdi_error_return (-1, "Silly baudrate <= 0.");
526 // Check within tolerance (about 5%)
527 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
528 || ((actual_baudrate < baudrate)
529 ? (actual_baudrate * 21 < baudrate * 20)
530 : (baudrate * 21 < actual_baudrate * 20)))
531 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
533 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
534 ftdi_error_return (-2, "Setting new baudrate failed");
536 ftdi->baudrate = baudrate;
541 Set (RS232) line characteristics by Alain Abbas
543 \param ftdi pointer to ftdi_context
544 \param bits Number of bits
545 \param sbit Number of stop bits
546 \param parity Parity mode
549 \retval -1: Setting line property failed
551 int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
552 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
554 unsigned short value = bits;
558 value |= (0x00 << 8);
561 value |= (0x01 << 8);
564 value |= (0x02 << 8);
567 value |= (0x03 << 8);
570 value |= (0x04 << 8);
576 value |= (0x00 << 11);
579 value |= (0x01 << 11);
582 value |= (0x02 << 11);
586 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
587 ftdi_error_return (-1, "Setting new line property failed");
593 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
595 \param ftdi pointer to ftdi_context
596 \param buf Buffer with the data
597 \param size Size of the buffer
599 \retval <0: error code from usb_bulk_write()
600 \retval >0: number of bytes written
602 int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
606 int total_written = 0;
608 while (offset < size) {
609 int write_size = ftdi->writebuffer_chunksize;
611 if (offset+write_size > size)
612 write_size = size-offset;
614 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
616 ftdi_error_return(ret, "usb bulk write failed");
618 total_written += ret;
619 offset += write_size;
622 return total_written;
626 Configure write buffer chunk size.
629 \param ftdi pointer to ftdi_context
630 \param chunksize Chunk size
634 int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
636 ftdi->writebuffer_chunksize = chunksize;
641 Get write buffer chunk size.
643 \param ftdi pointer to ftdi_context
644 \param chunksize Pointer to store chunk size in
648 int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
650 *chunksize = ftdi->writebuffer_chunksize;
655 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
657 Automatically strips the two modem status bytes transfered during every read.
659 \param ftdi pointer to ftdi_context
660 \param buf Buffer to store data in
661 \param size Size of the buffer
663 \retval <0: error code from usb_bulk_read()
664 \retval >0: number of bytes read
666 \remark This function is not useful in bitbang mode.
667 Use ftdi_read_pins() to get the current state of the pins.
669 int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
671 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
673 // everything we want is still in the readbuffer?
674 if (size <= ftdi->readbuffer_remaining) {
675 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
678 ftdi->readbuffer_remaining -= size;
679 ftdi->readbuffer_offset += size;
681 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
685 // something still in the readbuffer, but not enough to satisfy 'size'?
686 if (ftdi->readbuffer_remaining != 0) {
687 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
690 offset += ftdi->readbuffer_remaining;
692 // do the actual USB read
693 while (offset < size && ret > 0) {
694 ftdi->readbuffer_remaining = 0;
695 ftdi->readbuffer_offset = 0;
696 /* returns how much received */
697 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
699 ftdi_error_return(ret, "usb bulk read failed");
702 // skip FTDI status bytes.
703 // Maybe stored in the future to enable modem use
704 num_of_chunks = ret / 64;
705 chunk_remains = ret % 64;
706 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
708 ftdi->readbuffer_offset += 2;
712 for (i = 1; i < num_of_chunks; i++)
713 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
714 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
716 if (chunk_remains > 2) {
717 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
718 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
720 ret -= 2*num_of_chunks;
722 ret -= 2*(num_of_chunks-1)+chunk_remains;
724 } else if (ret <= 2) {
725 // no more data to read?
729 // data still fits in buf?
730 if (offset+ret <= size) {
731 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
732 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
735 /* Did we read exactly the right amount of bytes? */
737 //printf("read_data exact rem %d offset %d\n",
738 //ftdi->readbuffer_remaining, offset);
741 // only copy part of the data or size <= readbuffer_chunksize
742 int part_size = size-offset;
743 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
745 ftdi->readbuffer_offset += part_size;
746 ftdi->readbuffer_remaining = ret-part_size;
749 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
750 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
761 Configure read buffer chunk size.
764 Automatically reallocates the buffer.
766 \param ftdi pointer to ftdi_context
767 \param chunksize Chunk size
771 int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
773 unsigned char *new_buf;
775 // Invalidate all remaining data
776 ftdi->readbuffer_offset = 0;
777 ftdi->readbuffer_remaining = 0;
779 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
780 ftdi_error_return(-1, "out of memory for readbuffer");
782 ftdi->readbuffer = new_buf;
783 ftdi->readbuffer_chunksize = chunksize;
789 Get read buffer chunk size.
791 \param ftdi pointer to ftdi_context
792 \param chunksize Pointer to store chunk size in
796 int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
798 *chunksize = ftdi->readbuffer_chunksize;
806 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
808 \param ftdi pointer to ftdi_context
809 \param bitmask Bitmask to configure lines.
810 HIGH/ON value configures a line as output.
813 \retval -1: can't enable bitbang mode
815 int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
817 unsigned short usb_val;
819 usb_val = bitmask; // low byte: bitmask
820 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
821 usb_val |= (ftdi->bitbang_mode << 8);
823 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
824 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
826 ftdi->bitbang_enabled = 1;
831 Disable bitbang mode.
833 \param ftdi pointer to ftdi_context
836 \retval -1: can't disable bitbang mode
838 int ftdi_disable_bitbang(struct ftdi_context *ftdi)
840 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
841 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
843 ftdi->bitbang_enabled = 0;
848 Enable advanced bitbang mode for FT2232C chips.
850 \param ftdi pointer to ftdi_context
851 \param bitmask Bitmask to configure lines.
852 HIGH/ON value configures a line as output.
853 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
856 \retval -1: can't enable bitbang mode
858 int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
860 unsigned short usb_val;
862 usb_val = bitmask; // low byte: bitmask
863 usb_val |= (mode << 8);
864 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
865 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
867 ftdi->bitbang_mode = mode;
868 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
873 Directly read pin state. Useful for bitbang mode.
875 \param ftdi pointer to ftdi_context
876 \param pins Pointer to store pins into
879 \retval -1: read pins failed
881 int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
883 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
884 ftdi_error_return(-1, "read pins failed");
892 The FTDI chip keeps data in the internal buffer for a specific
893 amount of time if the buffer is not full yet to decrease
896 \param ftdi pointer to ftdi_context
897 \param latency Value between 1 and 255
900 \retval -1: latency out of range
901 \retval -2: unable to set latency timer
903 int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
905 unsigned short usb_val;
908 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
911 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
912 ftdi_error_return(-2, "unable to set latency timer");
920 \param ftdi pointer to ftdi_context
921 \param latency Pointer to store latency value in
924 \retval -1: unable to get latency timer
926 int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
928 unsigned short usb_val;
929 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
930 ftdi_error_return(-1, "reading latency timer failed");
932 *latency = (unsigned char)usb_val;
937 Init eeprom with default values.
939 \param eeprom Pointer to ftdi_eeprom
941 void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
943 eeprom->vendor_id = 0x0403;
944 eeprom->product_id = 0x6001;
946 eeprom->self_powered = 1;
947 eeprom->remote_wakeup = 1;
948 eeprom->BM_type_chip = 1;
950 eeprom->in_is_isochronous = 0;
951 eeprom->out_is_isochronous = 0;
952 eeprom->suspend_pull_downs = 0;
954 eeprom->use_serial = 0;
955 eeprom->change_usb_version = 0;
956 eeprom->usb_version = 0x0200;
957 eeprom->max_power = 0;
959 eeprom->manufacturer = NULL;
960 eeprom->product = NULL;
961 eeprom->serial = NULL;
965 Build binary output from ftdi_eeprom structure.
966 Output is suitable for ftdi_write_eeprom().
968 \param eeprom Pointer to ftdi_eeprom
969 \param output Buffer of 128 bytes to store eeprom image to
971 \retval >0: used eeprom size
972 \retval -1: eeprom size (128 bytes) exceeded by custom strings
974 int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
977 unsigned short checksum, value;
978 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
981 if (eeprom->manufacturer != NULL)
982 manufacturer_size = strlen(eeprom->manufacturer);
983 if (eeprom->product != NULL)
984 product_size = strlen(eeprom->product);
985 if (eeprom->serial != NULL)
986 serial_size = strlen(eeprom->serial);
988 size_check = 128; // eeprom is 128 bytes
989 size_check -= 28; // 28 are always in use (fixed)
990 size_check -= manufacturer_size*2;
991 size_check -= product_size*2;
992 size_check -= serial_size*2;
994 // eeprom size exceeded?
999 memset (output, 0, 128);
1001 // Addr 00: Stay 00 00
1002 // Addr 02: Vendor ID
1003 output[0x02] = eeprom->vendor_id;
1004 output[0x03] = eeprom->vendor_id >> 8;
1006 // Addr 04: Product ID
1007 output[0x04] = eeprom->product_id;
1008 output[0x05] = eeprom->product_id >> 8;
1010 // Addr 06: Device release number (0400h for BM features)
1011 output[0x06] = 0x00;
1013 if (eeprom->BM_type_chip == 1)
1014 output[0x07] = 0x04;
1016 output[0x07] = 0x02;
1018 // Addr 08: Config descriptor
1019 // Bit 1: remote wakeup if 1
1020 // Bit 0: self powered if 1
1023 if (eeprom->self_powered == 1)
1025 if (eeprom->remote_wakeup == 1)
1029 // Addr 09: Max power consumption: max power = value * 2 mA
1030 output[0x09] = eeprom->max_power;
1033 // Addr 0A: Chip configuration
1034 // Bit 7: 0 - reserved
1035 // Bit 6: 0 - reserved
1036 // Bit 5: 0 - reserved
1037 // Bit 4: 1 - Change USB version
1038 // Bit 3: 1 - Use the serial number string
1039 // Bit 2: 1 - Enable suspend pull downs for lower power
1040 // Bit 1: 1 - Out EndPoint is Isochronous
1041 // Bit 0: 1 - In EndPoint is Isochronous
1044 if (eeprom->in_is_isochronous == 1)
1046 if (eeprom->out_is_isochronous == 1)
1048 if (eeprom->suspend_pull_downs == 1)
1050 if (eeprom->use_serial == 1)
1052 if (eeprom->change_usb_version == 1)
1056 // Addr 0B: reserved
1057 output[0x0B] = 0x00;
1059 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1060 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1061 if (eeprom->change_usb_version == 1) {
1062 output[0x0C] = eeprom->usb_version;
1063 output[0x0D] = eeprom->usb_version >> 8;
1067 // Addr 0E: Offset of the manufacturer string + 0x80
1068 output[0x0E] = 0x14 + 0x80;
1070 // Addr 0F: Length of manufacturer string
1071 output[0x0F] = manufacturer_size*2 + 2;
1073 // Addr 10: Offset of the product string + 0x80, calculated later
1074 // Addr 11: Length of product string
1075 output[0x11] = product_size*2 + 2;
1077 // Addr 12: Offset of the serial string + 0x80, calculated later
1078 // Addr 13: Length of serial string
1079 output[0x13] = serial_size*2 + 2;
1082 output[0x14] = manufacturer_size*2 + 2;
1083 output[0x15] = 0x03; // type: string
1087 // Output manufacturer
1088 for (j = 0; j < manufacturer_size; j++) {
1089 output[i] = eeprom->manufacturer[j], i++;
1090 output[i] = 0x00, i++;
1093 // Output product name
1094 output[0x10] = i + 0x80; // calculate offset
1095 output[i] = product_size*2 + 2, i++;
1096 output[i] = 0x03, i++;
1097 for (j = 0; j < product_size; j++) {
1098 output[i] = eeprom->product[j], i++;
1099 output[i] = 0x00, i++;
1103 output[0x12] = i + 0x80; // calculate offset
1104 output[i] = serial_size*2 + 2, i++;
1105 output[i] = 0x03, i++;
1106 for (j = 0; j < serial_size; j++) {
1107 output[i] = eeprom->serial[j], i++;
1108 output[i] = 0x00, i++;
1111 // calculate checksum
1114 for (i = 0; i < 63; i++) {
1115 value = output[i*2];
1116 value += output[(i*2)+1] << 8;
1118 checksum = value^checksum;
1119 checksum = (checksum << 1) | (checksum >> 15);
1122 output[0x7E] = checksum;
1123 output[0x7F] = checksum >> 8;
1131 \param ftdi pointer to ftdi_context
1132 \param eeprom Pointer to store eeprom into
1135 \retval -1: read failed
1137 int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1141 for (i = 0; i < 64; i++) {
1142 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1143 ftdi_error_return(-1, "reading eeprom failed");
1152 \param ftdi pointer to ftdi_context
1153 \param eeprom Pointer to read eeprom from
1156 \retval -1: read failed
1158 int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1160 unsigned short usb_val;
1163 for (i = 0; i < 64; i++) {
1164 usb_val = eeprom[i*2];
1165 usb_val += eeprom[(i*2)+1] << 8;
1166 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1167 ftdi_error_return(-1, "unable to write eeprom");
1176 \param ftdi pointer to ftdi_context
1179 \retval -1: erase failed
1181 int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1183 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1184 ftdi_error_return(-1, "unable to erase eeprom");
1190 Get string representation for last error code
1192 \param ftdi pointer to ftdi_context
1194 \retval Pointer to error string
1196 char *ftdi_get_error_string (struct ftdi_context *ftdi)
1198 return ftdi->error_str;
1202 Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org)
1203 and Matthias Kranz (matthias@hcilab.org)
1206 Set flowcontrol for ftdi chip
1208 \param ftdi pointer to ftdi_context
1209 \param flowctrl flow control to use. should be
1210 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1213 \retval -1: set flow control failed
1215 int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1217 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1218 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1219 NULL, 0, ftdi->usb_write_timeout) != 0)
1220 ftdi_error_return(-1, "set flow control failed");
1228 \param ftdi pointer to ftdi_context
1229 \param state state to set line to (1 or 0)
1232 \retval -1: set dtr failed
1234 int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1236 unsigned short usb_val;
1239 usb_val = SIO_SET_DTR_HIGH;
1241 usb_val = SIO_SET_DTR_LOW;
1243 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1244 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1245 NULL, 0, ftdi->usb_write_timeout) != 0)
1246 ftdi_error_return(-1, "set dtr failed");
1254 \param ftdi pointer to ftdi_context
1255 \param state state to set line to (1 or 0)
1258 \retval -1 set rts failed
1260 int ftdi_setrts(struct ftdi_context *ftdi, int state)
1262 unsigned short usb_val;
1265 usb_val = SIO_SET_RTS_HIGH;
1267 usb_val = SIO_SET_RTS_LOW;
1269 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1270 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1271 NULL, 0, ftdi->usb_write_timeout) != 0)
1272 ftdi_error_return(-1, "set of rts failed");