Fix doxygen documentation
[libftdi] / src / ftdi.c
... / ...
CommitLineData
1/***************************************************************************
2 ftdi.c - description
3 -------------------
4 begin : Fri Apr 4 2003
5 copyright : (C) 2003-2010 by Intra2net AG
6 email : opensource@intra2net.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
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; *
14 * *
15 ***************************************************************************/
16
17/**
18 \mainpage libftdi API documentation
19
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/en/developer/libftdi/
22
23 The library is easy to use. Have a look at this short example:
24 \include simple.c
25
26 More examples can be found in the "examples" directory.
27*/
28/** \addtogroup libftdi */
29/* @{ */
30
31#include <libusb.h>
32#include <string.h>
33#include <errno.h>
34#include <stdio.h>
35#include <stdlib.h>
36
37#include "ftdi.h"
38
39#define ftdi_error_return(code, str) do { \
40 ftdi->error_str = str; \
41 return code; \
42 } while(0);
43
44#define ftdi_error_return_free_device_list(code, str, devs) do { \
45 libusb_free_device_list(devs,1); \
46 ftdi->error_str = str; \
47 return code; \
48 } while(0);
49
50
51/**
52 Internal function to close usb device pointer.
53 Sets ftdi->usb_dev to NULL.
54 \internal
55
56 \param ftdi pointer to ftdi_context
57
58 \retval none
59*/
60static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
61{
62 if (ftdi && ftdi->usb_dev)
63 {
64 libusb_close (ftdi->usb_dev);
65 ftdi->usb_dev = NULL;
66 }
67}
68
69/**
70 Initializes a ftdi_context.
71
72 \param ftdi pointer to ftdi_context
73
74 \retval 0: all fine
75 \retval -1: couldn't allocate read buffer
76 \retval -2: couldn't allocate struct buffer
77
78 \remark This should be called before all functions
79*/
80int ftdi_init(struct ftdi_context *ftdi)
81{
82 struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
83 ftdi->usb_ctx = NULL;
84 ftdi->usb_dev = NULL;
85 ftdi->usb_read_timeout = 5000;
86 ftdi->usb_write_timeout = 5000;
87
88 ftdi->type = TYPE_BM; /* chip type */
89 ftdi->baudrate = -1;
90 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
91
92 ftdi->readbuffer = NULL;
93 ftdi->readbuffer_offset = 0;
94 ftdi->readbuffer_remaining = 0;
95 ftdi->writebuffer_chunksize = 4096;
96 ftdi->max_packet_size = 0;
97
98 ftdi_set_interface(ftdi, INTERFACE_ANY);
99 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
100
101 ftdi->error_str = NULL;
102
103 if (eeprom == 0)
104 ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
105 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
106 ftdi->eeprom = eeprom;
107
108 /* All fine. Now allocate the readbuffer */
109 return ftdi_read_data_set_chunksize(ftdi, 4096);
110}
111
112/**
113 Allocate and initialize a new ftdi_context
114
115 \return a pointer to a new ftdi_context, or NULL on failure
116*/
117struct ftdi_context *ftdi_new(void)
118{
119 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
120
121 if (ftdi == NULL)
122 {
123 return NULL;
124 }
125
126 if (ftdi_init(ftdi) != 0)
127 {
128 free(ftdi);
129 return NULL;
130 }
131
132 return ftdi;
133}
134
135/**
136 Open selected channels on a chip, otherwise use first channel.
137
138 \param ftdi pointer to ftdi_context
139 \param interface Interface to use for FT2232C/2232H/4232H chips.
140
141 \retval 0: all fine
142 \retval -1: unknown interface
143 \retval -2: USB device unavailable
144*/
145int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
146{
147 if (ftdi == NULL)
148 ftdi_error_return(-2, "USB device unavailable");
149
150 switch (interface)
151 {
152 case INTERFACE_ANY:
153 case INTERFACE_A:
154 ftdi->interface = 0;
155 ftdi->index = INTERFACE_A;
156 ftdi->in_ep = 0x02;
157 ftdi->out_ep = 0x81;
158 break;
159 case INTERFACE_B:
160 ftdi->interface = 1;
161 ftdi->index = INTERFACE_B;
162 ftdi->in_ep = 0x04;
163 ftdi->out_ep = 0x83;
164 break;
165 case INTERFACE_C:
166 ftdi->interface = 2;
167 ftdi->index = INTERFACE_C;
168 ftdi->in_ep = 0x06;
169 ftdi->out_ep = 0x85;
170 break;
171 case INTERFACE_D:
172 ftdi->interface = 3;
173 ftdi->index = INTERFACE_D;
174 ftdi->in_ep = 0x08;
175 ftdi->out_ep = 0x87;
176 break;
177 default:
178 ftdi_error_return(-1, "Unknown interface");
179 }
180 return 0;
181}
182
183/**
184 Deinitializes a ftdi_context.
185
186 \param ftdi pointer to ftdi_context
187*/
188void ftdi_deinit(struct ftdi_context *ftdi)
189{
190 if (ftdi == NULL)
191 return;
192
193 ftdi_usb_close_internal (ftdi);
194
195 if (ftdi->readbuffer != NULL)
196 {
197 free(ftdi->readbuffer);
198 ftdi->readbuffer = NULL;
199 }
200
201 if (ftdi->eeprom != NULL)
202 {
203 if (ftdi->eeprom->manufacturer != 0)
204 {
205 free(ftdi->eeprom->manufacturer);
206 ftdi->eeprom->manufacturer = 0;
207 }
208 if (ftdi->eeprom->product != 0)
209 {
210 free(ftdi->eeprom->product);
211 ftdi->eeprom->product = 0;
212 }
213 if (ftdi->eeprom->serial != 0)
214 {
215 free(ftdi->eeprom->serial);
216 ftdi->eeprom->serial = 0;
217 }
218 free(ftdi->eeprom);
219 ftdi->eeprom = NULL;
220 }
221 libusb_exit(ftdi->usb_ctx);
222}
223
224/**
225 Deinitialize and free an ftdi_context.
226
227 \param ftdi pointer to ftdi_context
228*/
229void ftdi_free(struct ftdi_context *ftdi)
230{
231 ftdi_deinit(ftdi);
232 free(ftdi);
233}
234
235/**
236 Use an already open libusb device.
237
238 \param ftdi pointer to ftdi_context
239 \param usb libusb libusb_device_handle to use
240*/
241void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
242{
243 if (ftdi == NULL)
244 return;
245
246 ftdi->usb_dev = usb;
247}
248
249
250/**
251 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
252 needs to be deallocated by ftdi_list_free() after use.
253
254 \param ftdi pointer to ftdi_context
255 \param devlist Pointer where to store list of found devices
256 \param vendor Vendor ID to search for
257 \param product Product ID to search for
258
259 \retval >0: number of devices found
260 \retval -3: out of memory
261 \retval -4: libusb_init() failed
262 \retval -5: libusb_get_device_list() failed
263 \retval -6: libusb_get_device_descriptor() failed
264*/
265int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
266{
267 struct ftdi_device_list **curdev;
268 libusb_device *dev;
269 libusb_device **devs;
270 int count = 0;
271 int i = 0;
272
273 if (libusb_init(&ftdi->usb_ctx) < 0)
274 ftdi_error_return(-4, "libusb_init() failed");
275
276 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
277 ftdi_error_return(-5, "libusb_get_device_list() failed");
278
279 curdev = devlist;
280 *curdev = NULL;
281
282 while ((dev = devs[i++]) != NULL)
283 {
284 struct libusb_device_descriptor desc;
285
286 if (libusb_get_device_descriptor(dev, &desc) < 0)
287 ftdi_error_return(-6, "libusb_get_device_descriptor() failed");
288
289 if (desc.idVendor == vendor && desc.idProduct == product)
290 {
291 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
292 if (!*curdev)
293 ftdi_error_return(-3, "out of memory");
294
295 (*curdev)->next = NULL;
296 (*curdev)->dev = dev;
297
298 curdev = &(*curdev)->next;
299 count++;
300 }
301 }
302
303 return count;
304}
305
306/**
307 Frees a usb device list.
308
309 \param devlist USB device list created by ftdi_usb_find_all()
310*/
311void ftdi_list_free(struct ftdi_device_list **devlist)
312{
313 struct ftdi_device_list *curdev, *next;
314
315 for (curdev = *devlist; curdev != NULL;)
316 {
317 next = curdev->next;
318 free(curdev);
319 curdev = next;
320 }
321
322 *devlist = NULL;
323}
324
325/**
326 Frees a usb device list.
327
328 \param devlist USB device list created by ftdi_usb_find_all()
329*/
330void ftdi_list_free2(struct ftdi_device_list *devlist)
331{
332 ftdi_list_free(&devlist);
333}
334
335/**
336 Return device ID strings from the usb device.
337
338 The parameters manufacturer, description and serial may be NULL
339 or pointer to buffers to store the fetched strings.
340
341 \note Use this function only in combination with ftdi_usb_find_all()
342 as it closes the internal "usb_dev" after use.
343
344 \param ftdi pointer to ftdi_context
345 \param dev libusb usb_dev to use
346 \param manufacturer Store manufacturer string here if not NULL
347 \param mnf_len Buffer size of manufacturer string
348 \param description Store product description string here if not NULL
349 \param desc_len Buffer size of product description string
350 \param serial Store serial string here if not NULL
351 \param serial_len Buffer size of serial string
352
353 \retval 0: all fine
354 \retval -1: wrong arguments
355 \retval -4: unable to open device
356 \retval -7: get product manufacturer failed
357 \retval -8: get product description failed
358 \retval -9: get serial number failed
359 \retval -11: libusb_get_device_descriptor() failed
360*/
361int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
362 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
363{
364 struct libusb_device_descriptor desc;
365
366 if ((ftdi==NULL) || (dev==NULL))
367 return -1;
368
369 if (libusb_open(dev, &ftdi->usb_dev) < 0)
370 ftdi_error_return(-4, "libusb_open() failed");
371
372 if (libusb_get_device_descriptor(dev, &desc) < 0)
373 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
374
375 if (manufacturer != NULL)
376 {
377 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
378 {
379 ftdi_usb_close_internal (ftdi);
380 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
381 }
382 }
383
384 if (description != NULL)
385 {
386 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
387 {
388 ftdi_usb_close_internal (ftdi);
389 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
390 }
391 }
392
393 if (serial != NULL)
394 {
395 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
396 {
397 ftdi_usb_close_internal (ftdi);
398 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
399 }
400 }
401
402 ftdi_usb_close_internal (ftdi);
403
404 return 0;
405}
406
407/**
408 * Internal function to determine the maximum packet size.
409 * \param ftdi pointer to ftdi_context
410 * \param dev libusb usb_dev to use
411 * \retval Maximum packet size for this device
412 */
413static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
414{
415 struct libusb_device_descriptor desc;
416 struct libusb_config_descriptor *config0;
417 unsigned int packet_size;
418
419 // Sanity check
420 if (ftdi == NULL || dev == NULL)
421 return 64;
422
423 // Determine maximum packet size. Init with default value.
424 // New hi-speed devices from FTDI use a packet size of 512 bytes
425 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
426 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
427 packet_size = 512;
428 else
429 packet_size = 64;
430
431 if (libusb_get_device_descriptor(dev, &desc) < 0)
432 return packet_size;
433
434 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
435 return packet_size;
436
437 if (desc.bNumConfigurations > 0)
438 {
439 if (ftdi->interface < config0->bNumInterfaces)
440 {
441 struct libusb_interface interface = config0->interface[ftdi->interface];
442 if (interface.num_altsetting > 0)
443 {
444 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
445 if (descriptor.bNumEndpoints > 0)
446 {
447 packet_size = descriptor.endpoint[0].wMaxPacketSize;
448 }
449 }
450 }
451 }
452
453 libusb_free_config_descriptor (config0);
454 return packet_size;
455}
456
457/**
458 Opens a ftdi device given by an usb_device.
459
460 \param ftdi pointer to ftdi_context
461 \param dev libusb usb_dev to use
462
463 \retval 0: all fine
464 \retval -3: unable to config device
465 \retval -4: unable to open device
466 \retval -5: unable to claim device
467 \retval -6: reset failed
468 \retval -7: set baudrate failed
469 \retval -8: ftdi context invalid
470 \retval -9: libusb_get_device_descriptor() failed
471 \retval -10: libusb_get_config_descriptor() failed
472 \retval -11: libusb_etach_kernel_driver() failed
473 \retval -12: libusb_get_configuration() failed
474*/
475int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
476{
477 struct libusb_device_descriptor desc;
478 struct libusb_config_descriptor *config0;
479 int cfg, cfg0, detach_errno = 0;
480
481 if (ftdi == NULL)
482 ftdi_error_return(-8, "ftdi context invalid");
483
484 if (libusb_open(dev, &ftdi->usb_dev) < 0)
485 ftdi_error_return(-4, "libusb_open() failed");
486
487 if (libusb_get_device_descriptor(dev, &desc) < 0)
488 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
489
490 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
491 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
492 cfg0 = config0->bConfigurationValue;
493 libusb_free_config_descriptor (config0);
494
495 // Try to detach ftdi_sio kernel module.
496 //
497 // The return code is kept in a separate variable and only parsed
498 // if usb_set_configuration() or usb_claim_interface() fails as the
499 // detach operation might be denied and everything still works fine.
500 // Likely scenario is a static ftdi_sio kernel module.
501 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
502 detach_errno = errno;
503
504 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
505 ftdi_error_return(-12, "libusb_get_configuration () failed");
506 // set configuration (needed especially for windows)
507 // tolerate EBUSY: one device with one configuration, but two interfaces
508 // and libftdi sessions to both interfaces (e.g. FT2232)
509 if (desc.bNumConfigurations > 0 && cfg != cfg0)
510 {
511 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
512 {
513 ftdi_usb_close_internal (ftdi);
514 if (detach_errno == EPERM)
515 {
516 ftdi_error_return(-8, "inappropriate permissions on device!");
517 }
518 else
519 {
520 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
521 }
522 }
523 }
524
525 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
526 {
527 ftdi_usb_close_internal (ftdi);
528 if (detach_errno == EPERM)
529 {
530 ftdi_error_return(-8, "inappropriate permissions on device!");
531 }
532 else
533 {
534 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
535 }
536 }
537
538 if (ftdi_usb_reset (ftdi) != 0)
539 {
540 ftdi_usb_close_internal (ftdi);
541 ftdi_error_return(-6, "ftdi_usb_reset failed");
542 }
543
544 // Try to guess chip type
545 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
546 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
547 && desc.iSerialNumber == 0))
548 ftdi->type = TYPE_BM;
549 else if (desc.bcdDevice == 0x200)
550 ftdi->type = TYPE_AM;
551 else if (desc.bcdDevice == 0x500)
552 ftdi->type = TYPE_2232C;
553 else if (desc.bcdDevice == 0x600)
554 ftdi->type = TYPE_R;
555 else if (desc.bcdDevice == 0x700)
556 ftdi->type = TYPE_2232H;
557 else if (desc.bcdDevice == 0x800)
558 ftdi->type = TYPE_4232H;
559
560 // Determine maximum packet size
561 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
562
563 if (ftdi_set_baudrate (ftdi, 9600) != 0)
564 {
565 ftdi_usb_close_internal (ftdi);
566 ftdi_error_return(-7, "set baudrate failed");
567 }
568
569 ftdi_error_return(0, "all fine");
570}
571
572/**
573 Opens the first device with a given vendor and product ids.
574
575 \param ftdi pointer to ftdi_context
576 \param vendor Vendor ID
577 \param product Product ID
578
579 \retval same as ftdi_usb_open_desc()
580*/
581int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
582{
583 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
584}
585
586/**
587 Opens the first device with a given, vendor id, product id,
588 description and serial.
589
590 \param ftdi pointer to ftdi_context
591 \param vendor Vendor ID
592 \param product Product ID
593 \param description Description to search for. Use NULL if not needed.
594 \param serial Serial to search for. Use NULL if not needed.
595
596 \retval 0: all fine
597 \retval -3: usb device not found
598 \retval -4: unable to open device
599 \retval -5: unable to claim device
600 \retval -6: reset failed
601 \retval -7: set baudrate failed
602 \retval -8: get product description failed
603 \retval -9: get serial number failed
604 \retval -11: libusb_init() failed
605 \retval -12: libusb_get_device_list() failed
606 \retval -13: libusb_get_device_descriptor() failed
607*/
608int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
609 const char* description, const char* serial)
610{
611 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
612}
613
614/**
615 Opens the index-th device with a given, vendor id, product id,
616 description and serial.
617
618 \param ftdi pointer to ftdi_context
619 \param vendor Vendor ID
620 \param product Product ID
621 \param description Description to search for. Use NULL if not needed.
622 \param serial Serial to search for. Use NULL if not needed.
623 \param index Number of matching device to open if there are more than one, starts with 0.
624
625 \retval 0: all fine
626 \retval -1: usb_find_busses() failed
627 \retval -2: usb_find_devices() failed
628 \retval -3: usb device not found
629 \retval -4: unable to open device
630 \retval -5: unable to claim device
631 \retval -6: reset failed
632 \retval -7: set baudrate failed
633 \retval -8: get product description failed
634 \retval -9: get serial number failed
635 \retval -10: unable to close device
636 \retval -11: ftdi context invalid
637*/
638int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
639 const char* description, const char* serial, unsigned int index)
640{
641 libusb_device *dev;
642 libusb_device **devs;
643 char string[256];
644 int i = 0;
645
646 if (libusb_init(&ftdi->usb_ctx) < 0)
647 ftdi_error_return(-11, "libusb_init() failed");
648
649 if (ftdi == NULL)
650 ftdi_error_return(-11, "ftdi context invalid");
651
652 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
653 ftdi_error_return(-12, "libusb_get_device_list() failed");
654
655 while ((dev = devs[i++]) != NULL)
656 {
657 struct libusb_device_descriptor desc;
658 int res;
659
660 if (libusb_get_device_descriptor(dev, &desc) < 0)
661 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
662
663 if (desc.idVendor == vendor && desc.idProduct == product)
664 {
665 if (libusb_open(dev, &ftdi->usb_dev) < 0)
666 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
667
668 if (description != NULL)
669 {
670 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
671 {
672 libusb_close (ftdi->usb_dev);
673 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
674 }
675 if (strncmp(string, description, sizeof(string)) != 0)
676 {
677 libusb_close (ftdi->usb_dev);
678 continue;
679 }
680 }
681 if (serial != NULL)
682 {
683 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
684 {
685 ftdi_usb_close_internal (ftdi);
686 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
687 }
688 if (strncmp(string, serial, sizeof(string)) != 0)
689 {
690 ftdi_usb_close_internal (ftdi);
691 continue;
692 }
693 }
694
695 ftdi_usb_close_internal (ftdi);
696
697 if (index > 0)
698 {
699 index--;
700 continue;
701 }
702
703 res = ftdi_usb_open_dev(ftdi, dev);
704 libusb_free_device_list(devs,1);
705 return res;
706 }
707 }
708
709 // device not found
710 ftdi_error_return_free_device_list(-3, "device not found", devs);
711}
712
713/**
714 Opens the ftdi-device described by a description-string.
715 Intended to be used for parsing a device-description given as commandline argument.
716
717 \param ftdi pointer to ftdi_context
718 \param description NULL-terminated description-string, using this format:
719 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
720 \li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x")
721 \li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one
722 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
723
724 \note The description format may be extended in later versions.
725
726 \retval 0: all fine
727 \retval -1: libusb_init() failed
728 \retval -2: libusb_get_device_list() failed
729 \retval -3: usb device not found
730 \retval -4: unable to open device
731 \retval -5: unable to claim device
732 \retval -6: reset failed
733 \retval -7: set baudrate failed
734 \retval -8: get product description failed
735 \retval -9: get serial number failed
736 \retval -10: unable to close device
737 \retval -11: illegal description format
738 \retval -12: ftdi context invalid
739*/
740int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
741{
742 if (ftdi == NULL)
743 ftdi_error_return(-12, "ftdi context invalid");
744
745 if (description[0] == 0 || description[1] != ':')
746 ftdi_error_return(-11, "illegal description format");
747
748 if (description[0] == 'd')
749 {
750 libusb_device *dev;
751 libusb_device **devs;
752 unsigned int bus_number, device_address;
753 int i = 0;
754
755 if (libusb_init (&ftdi->usb_ctx) < 0)
756 ftdi_error_return(-1, "libusb_init() failed");
757
758 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
759 ftdi_error_return(-2, "libusb_get_device_list() failed");
760
761 /* XXX: This doesn't handle symlinks/odd paths/etc... */
762 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
763 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
764
765 while ((dev = devs[i++]) != NULL)
766 {
767 int ret;
768 if (bus_number == libusb_get_bus_number (dev)
769 && device_address == libusb_get_device_address (dev))
770 {
771 ret = ftdi_usb_open_dev(ftdi, dev);
772 libusb_free_device_list(devs,1);
773 return ret;
774 }
775 }
776
777 // device not found
778 ftdi_error_return_free_device_list(-3, "device not found", devs);
779 }
780 else if (description[0] == 'i' || description[0] == 's')
781 {
782 unsigned int vendor;
783 unsigned int product;
784 unsigned int index=0;
785 const char *serial=NULL;
786 const char *startp, *endp;
787
788 errno=0;
789 startp=description+2;
790 vendor=strtoul((char*)startp,(char**)&endp,0);
791 if (*endp != ':' || endp == startp || errno != 0)
792 ftdi_error_return(-11, "illegal description format");
793
794 startp=endp+1;
795 product=strtoul((char*)startp,(char**)&endp,0);
796 if (endp == startp || errno != 0)
797 ftdi_error_return(-11, "illegal description format");
798
799 if (description[0] == 'i' && *endp != 0)
800 {
801 /* optional index field in i-mode */
802 if (*endp != ':')
803 ftdi_error_return(-11, "illegal description format");
804
805 startp=endp+1;
806 index=strtoul((char*)startp,(char**)&endp,0);
807 if (*endp != 0 || endp == startp || errno != 0)
808 ftdi_error_return(-11, "illegal description format");
809 }
810 if (description[0] == 's')
811 {
812 if (*endp != ':')
813 ftdi_error_return(-11, "illegal description format");
814
815 /* rest of the description is the serial */
816 serial=endp+1;
817 }
818
819 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
820 }
821 else
822 {
823 ftdi_error_return(-11, "illegal description format");
824 }
825}
826
827/**
828 Resets the ftdi device.
829
830 \param ftdi pointer to ftdi_context
831
832 \retval 0: all fine
833 \retval -1: FTDI reset failed
834 \retval -2: USB device unavailable
835*/
836int ftdi_usb_reset(struct ftdi_context *ftdi)
837{
838 if (ftdi == NULL || ftdi->usb_dev == NULL)
839 ftdi_error_return(-2, "USB device unavailable");
840
841 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
842 SIO_RESET_REQUEST, SIO_RESET_SIO,
843 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
844 ftdi_error_return(-1,"FTDI reset failed");
845
846 // Invalidate data in the readbuffer
847 ftdi->readbuffer_offset = 0;
848 ftdi->readbuffer_remaining = 0;
849
850 return 0;
851}
852
853/**
854 Clears the read buffer on the chip and the internal read buffer.
855
856 \param ftdi pointer to ftdi_context
857
858 \retval 0: all fine
859 \retval -1: read buffer purge failed
860 \retval -2: USB device unavailable
861*/
862int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
863{
864 if (ftdi == NULL || ftdi->usb_dev == NULL)
865 ftdi_error_return(-2, "USB device unavailable");
866
867 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
868 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
869 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
870 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
871
872 // Invalidate data in the readbuffer
873 ftdi->readbuffer_offset = 0;
874 ftdi->readbuffer_remaining = 0;
875
876 return 0;
877}
878
879/**
880 Clears the write buffer on the chip.
881
882 \param ftdi pointer to ftdi_context
883
884 \retval 0: all fine
885 \retval -1: write buffer purge failed
886 \retval -2: USB device unavailable
887*/
888int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
889{
890 if (ftdi == NULL || ftdi->usb_dev == NULL)
891 ftdi_error_return(-2, "USB device unavailable");
892
893 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
894 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
895 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
896 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
897
898 return 0;
899}
900
901/**
902 Clears the buffers on the chip and the internal read buffer.
903
904 \param ftdi pointer to ftdi_context
905
906 \retval 0: all fine
907 \retval -1: read buffer purge failed
908 \retval -2: write buffer purge failed
909 \retval -3: USB device unavailable
910*/
911int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
912{
913 int result;
914
915 if (ftdi == NULL || ftdi->usb_dev == NULL)
916 ftdi_error_return(-3, "USB device unavailable");
917
918 result = ftdi_usb_purge_rx_buffer(ftdi);
919 if (result < 0)
920 return -1;
921
922 result = ftdi_usb_purge_tx_buffer(ftdi);
923 if (result < 0)
924 return -2;
925
926 return 0;
927}
928
929
930
931/**
932 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
933
934 \param ftdi pointer to ftdi_context
935
936 \retval 0: all fine
937 \retval -1: usb_release failed
938 \retval -3: ftdi context invalid
939*/
940int ftdi_usb_close(struct ftdi_context *ftdi)
941{
942 int rtn = 0;
943
944 if (ftdi == NULL)
945 ftdi_error_return(-3, "ftdi context invalid");
946
947 if (ftdi->usb_dev != NULL)
948 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
949 rtn = -1;
950
951 ftdi_usb_close_internal (ftdi);
952
953 return rtn;
954}
955
956/**
957 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
958 Function is only used internally
959 \internal
960*/
961static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
962 unsigned short *value, unsigned short *index)
963{
964 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
965 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
966 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
967 int divisor, best_divisor, best_baud, best_baud_diff;
968 unsigned long encoded_divisor;
969 int i;
970
971 if (baudrate <= 0)
972 {
973 // Return error
974 return -1;
975 }
976
977 divisor = 24000000 / baudrate;
978
979 if (ftdi->type == TYPE_AM)
980 {
981 // Round down to supported fraction (AM only)
982 divisor -= am_adjust_dn[divisor & 7];
983 }
984
985 // Try this divisor and the one above it (because division rounds down)
986 best_divisor = 0;
987 best_baud = 0;
988 best_baud_diff = 0;
989 for (i = 0; i < 2; i++)
990 {
991 int try_divisor = divisor + i;
992 int baud_estimate;
993 int baud_diff;
994
995 // Round up to supported divisor value
996 if (try_divisor <= 8)
997 {
998 // Round up to minimum supported divisor
999 try_divisor = 8;
1000 }
1001 else if (ftdi->type != TYPE_AM && try_divisor < 12)
1002 {
1003 // BM doesn't support divisors 9 through 11 inclusive
1004 try_divisor = 12;
1005 }
1006 else if (divisor < 16)
1007 {
1008 // AM doesn't support divisors 9 through 15 inclusive
1009 try_divisor = 16;
1010 }
1011 else
1012 {
1013 if (ftdi->type == TYPE_AM)
1014 {
1015 // Round up to supported fraction (AM only)
1016 try_divisor += am_adjust_up[try_divisor & 7];
1017 if (try_divisor > 0x1FFF8)
1018 {
1019 // Round down to maximum supported divisor value (for AM)
1020 try_divisor = 0x1FFF8;
1021 }
1022 }
1023 else
1024 {
1025 if (try_divisor > 0x1FFFF)
1026 {
1027 // Round down to maximum supported divisor value (for BM)
1028 try_divisor = 0x1FFFF;
1029 }
1030 }
1031 }
1032 // Get estimated baud rate (to nearest integer)
1033 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1034 // Get absolute difference from requested baud rate
1035 if (baud_estimate < baudrate)
1036 {
1037 baud_diff = baudrate - baud_estimate;
1038 }
1039 else
1040 {
1041 baud_diff = baud_estimate - baudrate;
1042 }
1043 if (i == 0 || baud_diff < best_baud_diff)
1044 {
1045 // Closest to requested baud rate so far
1046 best_divisor = try_divisor;
1047 best_baud = baud_estimate;
1048 best_baud_diff = baud_diff;
1049 if (baud_diff == 0)
1050 {
1051 // Spot on! No point trying
1052 break;
1053 }
1054 }
1055 }
1056 // Encode the best divisor value
1057 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1058 // Deal with special cases for encoded value
1059 if (encoded_divisor == 1)
1060 {
1061 encoded_divisor = 0; // 3000000 baud
1062 }
1063 else if (encoded_divisor == 0x4001)
1064 {
1065 encoded_divisor = 1; // 2000000 baud (BM only)
1066 }
1067 // Split into "value" and "index" values
1068 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1069 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1070 {
1071 *index = (unsigned short)(encoded_divisor >> 8);
1072 *index &= 0xFF00;
1073 *index |= ftdi->index;
1074 }
1075 else
1076 *index = (unsigned short)(encoded_divisor >> 16);
1077
1078 // Return the nearest baud rate
1079 return best_baud;
1080}
1081
1082/**
1083 Sets the chip baud rate
1084
1085 \param ftdi pointer to ftdi_context
1086 \param baudrate baud rate to set
1087
1088 \retval 0: all fine
1089 \retval -1: invalid baudrate
1090 \retval -2: setting baudrate failed
1091 \retval -3: USB device unavailable
1092*/
1093int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1094{
1095 unsigned short value, index;
1096 int actual_baudrate;
1097
1098 if (ftdi == NULL || ftdi->usb_dev == NULL)
1099 ftdi_error_return(-3, "USB device unavailable");
1100
1101 if (ftdi->bitbang_enabled)
1102 {
1103 baudrate = baudrate*4;
1104 }
1105
1106 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
1107 if (actual_baudrate <= 0)
1108 ftdi_error_return (-1, "Silly baudrate <= 0.");
1109
1110 // Check within tolerance (about 5%)
1111 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1112 || ((actual_baudrate < baudrate)
1113 ? (actual_baudrate * 21 < baudrate * 20)
1114 : (baudrate * 21 < actual_baudrate * 20)))
1115 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
1116
1117 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1118 SIO_SET_BAUDRATE_REQUEST, value,
1119 index, NULL, 0, ftdi->usb_write_timeout) < 0)
1120 ftdi_error_return (-2, "Setting new baudrate failed");
1121
1122 ftdi->baudrate = baudrate;
1123 return 0;
1124}
1125
1126/**
1127 Set (RS232) line characteristics.
1128 The break type can only be set via ftdi_set_line_property2()
1129 and defaults to "off".
1130
1131 \param ftdi pointer to ftdi_context
1132 \param bits Number of bits
1133 \param sbit Number of stop bits
1134 \param parity Parity mode
1135
1136 \retval 0: all fine
1137 \retval -1: Setting line property failed
1138*/
1139int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1140 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
1141{
1142 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1143}
1144
1145/**
1146 Set (RS232) line characteristics
1147
1148 \param ftdi pointer to ftdi_context
1149 \param bits Number of bits
1150 \param sbit Number of stop bits
1151 \param parity Parity mode
1152 \param break_type Break type
1153
1154 \retval 0: all fine
1155 \retval -1: Setting line property failed
1156 \retval -2: USB device unavailable
1157*/
1158int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
1159 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1160 enum ftdi_break_type break_type)
1161{
1162 unsigned short value = bits;
1163
1164 if (ftdi == NULL || ftdi->usb_dev == NULL)
1165 ftdi_error_return(-2, "USB device unavailable");
1166
1167 switch (parity)
1168 {
1169 case NONE:
1170 value |= (0x00 << 8);
1171 break;
1172 case ODD:
1173 value |= (0x01 << 8);
1174 break;
1175 case EVEN:
1176 value |= (0x02 << 8);
1177 break;
1178 case MARK:
1179 value |= (0x03 << 8);
1180 break;
1181 case SPACE:
1182 value |= (0x04 << 8);
1183 break;
1184 }
1185
1186 switch (sbit)
1187 {
1188 case STOP_BIT_1:
1189 value |= (0x00 << 11);
1190 break;
1191 case STOP_BIT_15:
1192 value |= (0x01 << 11);
1193 break;
1194 case STOP_BIT_2:
1195 value |= (0x02 << 11);
1196 break;
1197 }
1198
1199 switch (break_type)
1200 {
1201 case BREAK_OFF:
1202 value |= (0x00 << 14);
1203 break;
1204 case BREAK_ON:
1205 value |= (0x01 << 14);
1206 break;
1207 }
1208
1209 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1210 SIO_SET_DATA_REQUEST, value,
1211 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1212 ftdi_error_return (-1, "Setting new line property failed");
1213
1214 return 0;
1215}
1216
1217/**
1218 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1219
1220 \param ftdi pointer to ftdi_context
1221 \param buf Buffer with the data
1222 \param size Size of the buffer
1223
1224 \retval -666: USB device unavailable
1225 \retval <0: error code from usb_bulk_write()
1226 \retval >0: number of bytes written
1227*/
1228int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1229{
1230 int offset = 0;
1231 int actual_length;
1232
1233 if (ftdi == NULL || ftdi->usb_dev == NULL)
1234 ftdi_error_return(-666, "USB device unavailable");
1235
1236 while (offset < size)
1237 {
1238 int write_size = ftdi->writebuffer_chunksize;
1239
1240 if (offset+write_size > size)
1241 write_size = size-offset;
1242
1243 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1244 ftdi_error_return(-1, "usb bulk write failed");
1245
1246 offset += actual_length;
1247 }
1248
1249 return offset;
1250}
1251
1252static void ftdi_read_data_cb(struct libusb_transfer *transfer)
1253{
1254 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1255 struct ftdi_context *ftdi = tc->ftdi;
1256 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
1257
1258 packet_size = ftdi->max_packet_size;
1259
1260 actual_length = transfer->actual_length;
1261
1262 if (actual_length > 2)
1263 {
1264 // skip FTDI status bytes.
1265 // Maybe stored in the future to enable modem use
1266 num_of_chunks = actual_length / packet_size;
1267 chunk_remains = actual_length % packet_size;
1268 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1269
1270 ftdi->readbuffer_offset += 2;
1271 actual_length -= 2;
1272
1273 if (actual_length > packet_size - 2)
1274 {
1275 for (i = 1; i < num_of_chunks; i++)
1276 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1277 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1278 packet_size - 2);
1279 if (chunk_remains > 2)
1280 {
1281 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1282 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1283 chunk_remains-2);
1284 actual_length -= 2*num_of_chunks;
1285 }
1286 else
1287 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1288 }
1289
1290 if (actual_length > 0)
1291 {
1292 // data still fits in buf?
1293 if (tc->offset + actual_length <= tc->size)
1294 {
1295 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1296 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1297 tc->offset += actual_length;
1298
1299 ftdi->readbuffer_offset = 0;
1300 ftdi->readbuffer_remaining = 0;
1301
1302 /* Did we read exactly the right amount of bytes? */
1303 if (tc->offset == tc->size)
1304 {
1305 //printf("read_data exact rem %d offset %d\n",
1306 //ftdi->readbuffer_remaining, offset);
1307 tc->completed = 1;
1308 return;
1309 }
1310 }
1311 else
1312 {
1313 // only copy part of the data or size <= readbuffer_chunksize
1314 int part_size = tc->size - tc->offset;
1315 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1316 tc->offset += part_size;
1317
1318 ftdi->readbuffer_offset += part_size;
1319 ftdi->readbuffer_remaining = actual_length - part_size;
1320
1321 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1322 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1323 tc->completed = 1;
1324 return;
1325 }
1326 }
1327 }
1328 ret = libusb_submit_transfer (transfer);
1329 if (ret < 0)
1330 tc->completed = 1;
1331}
1332
1333
1334static void ftdi_write_data_cb(struct libusb_transfer *transfer)
1335{
1336 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1337 struct ftdi_context *ftdi = tc->ftdi;
1338
1339 tc->offset += transfer->actual_length;
1340
1341 if (tc->offset == tc->size)
1342 {
1343 tc->completed = 1;
1344 }
1345 else
1346 {
1347 int write_size = ftdi->writebuffer_chunksize;
1348 int ret;
1349
1350 if (tc->offset + write_size > tc->size)
1351 write_size = tc->size - tc->offset;
1352
1353 transfer->length = write_size;
1354 transfer->buffer = tc->buf + tc->offset;
1355 ret = libusb_submit_transfer (transfer);
1356 if (ret < 0)
1357 tc->completed = 1;
1358 }
1359}
1360
1361
1362/**
1363 Writes data to the chip. Does not wait for completion of the transfer
1364 nor does it make sure that the transfer was successful.
1365
1366 Use libusb 1.0 asynchronous API.
1367
1368 \param ftdi pointer to ftdi_context
1369 \param buf Buffer with the data
1370 \param size Size of the buffer
1371
1372 \retval NULL: Some error happens when submit transfer
1373 \retval !NULL: Pointer to a ftdi_transfer_control
1374*/
1375
1376struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1377{
1378 struct ftdi_transfer_control *tc;
1379 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
1380 int write_size, ret;
1381
1382 if (ftdi == NULL || ftdi->usb_dev == NULL)
1383 {
1384 libusb_free_transfer(transfer);
1385 return NULL;
1386 }
1387
1388 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1389
1390 if (!tc || !transfer)
1391 return NULL;
1392
1393 tc->ftdi = ftdi;
1394 tc->completed = 0;
1395 tc->buf = buf;
1396 tc->size = size;
1397 tc->offset = 0;
1398
1399 if (size < ftdi->writebuffer_chunksize)
1400 write_size = size;
1401 else
1402 write_size = ftdi->writebuffer_chunksize;
1403
1404 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1405 write_size, ftdi_write_data_cb, tc,
1406 ftdi->usb_write_timeout);
1407 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1408
1409 ret = libusb_submit_transfer(transfer);
1410 if (ret < 0)
1411 {
1412 libusb_free_transfer(transfer);
1413 tc->completed = 1;
1414 tc->transfer = NULL;
1415 return NULL;
1416 }
1417 tc->transfer = transfer;
1418
1419 return tc;
1420}
1421
1422/**
1423 Reads data from the chip. Does not wait for completion of the transfer
1424 nor does it make sure that the transfer was successful.
1425
1426 Use libusb 1.0 asynchronous API.
1427
1428 \param ftdi pointer to ftdi_context
1429 \param buf Buffer with the data
1430 \param size Size of the buffer
1431
1432 \retval NULL: Some error happens when submit transfer
1433 \retval !NULL: Pointer to a ftdi_transfer_control
1434*/
1435
1436struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
1437{
1438 struct ftdi_transfer_control *tc;
1439 struct libusb_transfer *transfer;
1440 int ret;
1441
1442 if (ftdi == NULL || ftdi->usb_dev == NULL)
1443 return NULL;
1444
1445 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1446 if (!tc)
1447 return NULL;
1448
1449 tc->ftdi = ftdi;
1450 tc->buf = buf;
1451 tc->size = size;
1452
1453 if (size <= ftdi->readbuffer_remaining)
1454 {
1455 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1456
1457 // Fix offsets
1458 ftdi->readbuffer_remaining -= size;
1459 ftdi->readbuffer_offset += size;
1460
1461 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1462
1463 tc->completed = 1;
1464 tc->offset = size;
1465 tc->transfer = NULL;
1466 return tc;
1467 }
1468
1469 tc->completed = 0;
1470 if (ftdi->readbuffer_remaining != 0)
1471 {
1472 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1473
1474 tc->offset = ftdi->readbuffer_remaining;
1475 }
1476 else
1477 tc->offset = 0;
1478
1479 transfer = libusb_alloc_transfer(0);
1480 if (!transfer)
1481 {
1482 free (tc);
1483 return NULL;
1484 }
1485
1486 ftdi->readbuffer_remaining = 0;
1487 ftdi->readbuffer_offset = 0;
1488
1489 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi_read_data_cb, tc, ftdi->usb_read_timeout);
1490 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1491
1492 ret = libusb_submit_transfer(transfer);
1493 if (ret < 0)
1494 {
1495 libusb_free_transfer(transfer);
1496 free (tc);
1497 return NULL;
1498 }
1499 tc->transfer = transfer;
1500
1501 return tc;
1502}
1503
1504/**
1505 Wait for completion of the transfer.
1506
1507 Use libusb 1.0 asynchronous API.
1508
1509 \param tc pointer to ftdi_transfer_control
1510
1511 \retval < 0: Some error happens
1512 \retval >= 0: Data size transferred
1513*/
1514
1515int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
1516{
1517 int ret;
1518
1519 while (!tc->completed)
1520 {
1521 ret = libusb_handle_events(tc->ftdi->usb_ctx);
1522 if (ret < 0)
1523 {
1524 if (ret == LIBUSB_ERROR_INTERRUPTED)
1525 continue;
1526 libusb_cancel_transfer(tc->transfer);
1527 while (!tc->completed)
1528 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
1529 break;
1530 libusb_free_transfer(tc->transfer);
1531 free (tc);
1532 return ret;
1533 }
1534 }
1535
1536 ret = tc->offset;
1537 /**
1538 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
1539 * at ftdi_read_data_submit(). Therefore, we need to check it here.
1540 **/
1541 if (tc->transfer)
1542 {
1543 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1544 ret = -1;
1545 libusb_free_transfer(tc->transfer);
1546 }
1547 free(tc);
1548 return ret;
1549}
1550
1551/**
1552 Configure write buffer chunk size.
1553 Default is 4096.
1554
1555 \param ftdi pointer to ftdi_context
1556 \param chunksize Chunk size
1557
1558 \retval 0: all fine
1559 \retval -1: ftdi context invalid
1560*/
1561int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1562{
1563 if (ftdi == NULL)
1564 ftdi_error_return(-1, "ftdi context invalid");
1565
1566 ftdi->writebuffer_chunksize = chunksize;
1567 return 0;
1568}
1569
1570/**
1571 Get write buffer chunk size.
1572
1573 \param ftdi pointer to ftdi_context
1574 \param chunksize Pointer to store chunk size in
1575
1576 \retval 0: all fine
1577 \retval -1: ftdi context invalid
1578*/
1579int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1580{
1581 if (ftdi == NULL)
1582 ftdi_error_return(-1, "ftdi context invalid");
1583
1584 *chunksize = ftdi->writebuffer_chunksize;
1585 return 0;
1586}
1587
1588/**
1589 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1590
1591 Automatically strips the two modem status bytes transfered during every read.
1592
1593 \param ftdi pointer to ftdi_context
1594 \param buf Buffer to store data in
1595 \param size Size of the buffer
1596
1597 \retval -666: USB device unavailable
1598 \retval <0: error code from libusb_bulk_transfer()
1599 \retval 0: no data was available
1600 \retval >0: number of bytes read
1601
1602*/
1603int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1604{
1605 int offset = 0, ret, i, num_of_chunks, chunk_remains;
1606 int packet_size = ftdi->max_packet_size;
1607 int actual_length = 1;
1608
1609 if (ftdi == NULL || ftdi->usb_dev == NULL)
1610 ftdi_error_return(-666, "USB device unavailable");
1611
1612 // Packet size sanity check (avoid division by zero)
1613 if (packet_size == 0)
1614 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
1615
1616 // everything we want is still in the readbuffer?
1617 if (size <= ftdi->readbuffer_remaining)
1618 {
1619 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1620
1621 // Fix offsets
1622 ftdi->readbuffer_remaining -= size;
1623 ftdi->readbuffer_offset += size;
1624
1625 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
1626
1627 return size;
1628 }
1629 // something still in the readbuffer, but not enough to satisfy 'size'?
1630 if (ftdi->readbuffer_remaining != 0)
1631 {
1632 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
1633
1634 // Fix offset
1635 offset += ftdi->readbuffer_remaining;
1636 }
1637 // do the actual USB read
1638 while (offset < size && actual_length > 0)
1639 {
1640 ftdi->readbuffer_remaining = 0;
1641 ftdi->readbuffer_offset = 0;
1642 /* returns how much received */
1643 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
1644 if (ret < 0)
1645 ftdi_error_return(ret, "usb bulk read failed");
1646
1647 if (actual_length > 2)
1648 {
1649 // skip FTDI status bytes.
1650 // Maybe stored in the future to enable modem use
1651 num_of_chunks = actual_length / packet_size;
1652 chunk_remains = actual_length % packet_size;
1653 //printf("actual_length = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", actual_length, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1654
1655 ftdi->readbuffer_offset += 2;
1656 actual_length -= 2;
1657
1658 if (actual_length > packet_size - 2)
1659 {
1660 for (i = 1; i < num_of_chunks; i++)
1661 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1662 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1663 packet_size - 2);
1664 if (chunk_remains > 2)
1665 {
1666 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1667 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1668 chunk_remains-2);
1669 actual_length -= 2*num_of_chunks;
1670 }
1671 else
1672 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1673 }
1674 }
1675 else if (actual_length <= 2)
1676 {
1677 // no more data to read?
1678 return offset;
1679 }
1680 if (actual_length > 0)
1681 {
1682 // data still fits in buf?
1683 if (offset+actual_length <= size)
1684 {
1685 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
1686 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1687 offset += actual_length;
1688
1689 /* Did we read exactly the right amount of bytes? */
1690 if (offset == size)
1691 //printf("read_data exact rem %d offset %d\n",
1692 //ftdi->readbuffer_remaining, offset);
1693 return offset;
1694 }
1695 else
1696 {
1697 // only copy part of the data or size <= readbuffer_chunksize
1698 int part_size = size-offset;
1699 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
1700
1701 ftdi->readbuffer_offset += part_size;
1702 ftdi->readbuffer_remaining = actual_length-part_size;
1703 offset += part_size;
1704
1705 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1706 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1707
1708 return offset;
1709 }
1710 }
1711 }
1712 // never reached
1713 return -127;
1714}
1715
1716/**
1717 Configure read buffer chunk size.
1718 Default is 4096.
1719
1720 Automatically reallocates the buffer.
1721
1722 \param ftdi pointer to ftdi_context
1723 \param chunksize Chunk size
1724
1725 \retval 0: all fine
1726 \retval -1: ftdi context invalid
1727*/
1728int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1729{
1730 unsigned char *new_buf;
1731
1732 if (ftdi == NULL)
1733 ftdi_error_return(-1, "ftdi context invalid");
1734
1735 // Invalidate all remaining data
1736 ftdi->readbuffer_offset = 0;
1737 ftdi->readbuffer_remaining = 0;
1738#ifdef __linux__
1739 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1740 which is defined in libusb-1.0. Otherwise, each USB read request will
1741 be divided into multiple URBs. This will cause issues on Linux kernel
1742 older than 2.6.32. */
1743 if (chunksize > 16384)
1744 chunksize = 16384;
1745#endif
1746
1747 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1748 ftdi_error_return(-1, "out of memory for readbuffer");
1749
1750 ftdi->readbuffer = new_buf;
1751 ftdi->readbuffer_chunksize = chunksize;
1752
1753 return 0;
1754}
1755
1756/**
1757 Get read buffer chunk size.
1758
1759 \param ftdi pointer to ftdi_context
1760 \param chunksize Pointer to store chunk size in
1761
1762 \retval 0: all fine
1763 \retval -1: FTDI context invalid
1764*/
1765int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1766{
1767 if (ftdi == NULL)
1768 ftdi_error_return(-1, "FTDI context invalid");
1769
1770 *chunksize = ftdi->readbuffer_chunksize;
1771 return 0;
1772}
1773
1774
1775/**
1776 Enable bitbang mode.
1777
1778 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1779
1780 \param ftdi pointer to ftdi_context
1781 \param bitmask Bitmask to configure lines.
1782 HIGH/ON value configures a line as output.
1783
1784 \retval 0: all fine
1785 \retval -1: can't enable bitbang mode
1786 \retval -2: USB device unavailable
1787*/
1788int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1789{
1790 unsigned short usb_val;
1791
1792 if (ftdi == NULL || ftdi->usb_dev == NULL)
1793 ftdi_error_return(-2, "USB device unavailable");
1794
1795 usb_val = bitmask; // low byte: bitmask
1796 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1797 usb_val |= (ftdi->bitbang_mode << 8);
1798
1799 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1800 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1801 NULL, 0, ftdi->usb_write_timeout) < 0)
1802 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1803
1804 ftdi->bitbang_enabled = 1;
1805 return 0;
1806}
1807
1808/**
1809 Disable bitbang mode.
1810
1811 \param ftdi pointer to ftdi_context
1812
1813 \retval 0: all fine
1814 \retval -1: can't disable bitbang mode
1815 \retval -2: USB device unavailable
1816*/
1817int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1818{
1819 if (ftdi == NULL || ftdi->usb_dev == NULL)
1820 ftdi_error_return(-2, "USB device unavailable");
1821
1822 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1823 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1824
1825 ftdi->bitbang_enabled = 0;
1826 return 0;
1827}
1828
1829/**
1830 Enable/disable bitbang modes.
1831
1832 \param ftdi pointer to ftdi_context
1833 \param bitmask Bitmask to configure lines.
1834 HIGH/ON value configures a line as output.
1835 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1836
1837 \retval 0: all fine
1838 \retval -1: can't enable bitbang mode
1839 \retval -2: USB device unavailable
1840*/
1841int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1842{
1843 unsigned short usb_val;
1844
1845 if (ftdi == NULL || ftdi->usb_dev == NULL)
1846 ftdi_error_return(-2, "USB device unavailable");
1847
1848 usb_val = bitmask; // low byte: bitmask
1849 usb_val |= (mode << 8);
1850 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1851 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1852
1853 ftdi->bitbang_mode = mode;
1854 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
1855 return 0;
1856}
1857
1858/**
1859 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1860
1861 \param ftdi pointer to ftdi_context
1862 \param pins Pointer to store pins into
1863
1864 \retval 0: all fine
1865 \retval -1: read pins failed
1866 \retval -2: USB device unavailable
1867*/
1868int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1869{
1870 if (ftdi == NULL || ftdi->usb_dev == NULL)
1871 ftdi_error_return(-2, "USB device unavailable");
1872
1873 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (unsigned char *)pins, 1, ftdi->usb_read_timeout) != 1)
1874 ftdi_error_return(-1, "read pins failed");
1875
1876 return 0;
1877}
1878
1879/**
1880 Set latency timer
1881
1882 The FTDI chip keeps data in the internal buffer for a specific
1883 amount of time if the buffer is not full yet to decrease
1884 load on the usb bus.
1885
1886 \param ftdi pointer to ftdi_context
1887 \param latency Value between 1 and 255
1888
1889 \retval 0: all fine
1890 \retval -1: latency out of range
1891 \retval -2: unable to set latency timer
1892 \retval -3: USB device unavailable
1893*/
1894int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1895{
1896 unsigned short usb_val;
1897
1898 if (latency < 1)
1899 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1900
1901 if (ftdi == NULL || ftdi->usb_dev == NULL)
1902 ftdi_error_return(-3, "USB device unavailable");
1903
1904 usb_val = latency;
1905 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_LATENCY_TIMER_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1906 ftdi_error_return(-2, "unable to set latency timer");
1907
1908 return 0;
1909}
1910
1911/**
1912 Get latency timer
1913
1914 \param ftdi pointer to ftdi_context
1915 \param latency Pointer to store latency value in
1916
1917 \retval 0: all fine
1918 \retval -1: unable to get latency timer
1919 \retval -2: USB device unavailable
1920*/
1921int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1922{
1923 unsigned short usb_val;
1924
1925 if (ftdi == NULL || ftdi->usb_dev == NULL)
1926 ftdi_error_return(-2, "USB device unavailable");
1927
1928 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (unsigned char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1929 ftdi_error_return(-1, "reading latency timer failed");
1930
1931 *latency = (unsigned char)usb_val;
1932 return 0;
1933}
1934
1935/**
1936 Poll modem status information
1937
1938 This function allows the retrieve the two status bytes of the device.
1939 The device sends these bytes also as a header for each read access
1940 where they are discarded by ftdi_read_data(). The chip generates
1941 the two stripped status bytes in the absence of data every 40 ms.
1942
1943 Layout of the first byte:
1944 - B0..B3 - must be 0
1945 - B4 Clear to send (CTS)
1946 0 = inactive
1947 1 = active
1948 - B5 Data set ready (DTS)
1949 0 = inactive
1950 1 = active
1951 - B6 Ring indicator (RI)
1952 0 = inactive
1953 1 = active
1954 - B7 Receive line signal detect (RLSD)
1955 0 = inactive
1956 1 = active
1957
1958 Layout of the second byte:
1959 - B0 Data ready (DR)
1960 - B1 Overrun error (OE)
1961 - B2 Parity error (PE)
1962 - B3 Framing error (FE)
1963 - B4 Break interrupt (BI)
1964 - B5 Transmitter holding register (THRE)
1965 - B6 Transmitter empty (TEMT)
1966 - B7 Error in RCVR FIFO
1967
1968 \param ftdi pointer to ftdi_context
1969 \param status Pointer to store status information in. Must be two bytes.
1970
1971 \retval 0: all fine
1972 \retval -1: unable to retrieve status information
1973 \retval -2: USB device unavailable
1974*/
1975int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1976{
1977 char usb_val[2];
1978
1979 if (ftdi == NULL || ftdi->usb_dev == NULL)
1980 ftdi_error_return(-2, "USB device unavailable");
1981
1982 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, (unsigned char *)usb_val, 2, ftdi->usb_read_timeout) != 2)
1983 ftdi_error_return(-1, "getting modem status failed");
1984
1985 *status = (usb_val[1] << 8) | usb_val[0];
1986
1987 return 0;
1988}
1989
1990/**
1991 Set flowcontrol for ftdi chip
1992
1993 \param ftdi pointer to ftdi_context
1994 \param flowctrl flow control to use. should be
1995 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1996
1997 \retval 0: all fine
1998 \retval -1: set flow control failed
1999 \retval -2: USB device unavailable
2000*/
2001int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2002{
2003 if (ftdi == NULL || ftdi->usb_dev == NULL)
2004 ftdi_error_return(-2, "USB device unavailable");
2005
2006 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2007 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2008 NULL, 0, ftdi->usb_write_timeout) < 0)
2009 ftdi_error_return(-1, "set flow control failed");
2010
2011 return 0;
2012}
2013
2014/**
2015 Set dtr line
2016
2017 \param ftdi pointer to ftdi_context
2018 \param state state to set line to (1 or 0)
2019
2020 \retval 0: all fine
2021 \retval -1: set dtr failed
2022 \retval -2: USB device unavailable
2023*/
2024int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2025{
2026 unsigned short usb_val;
2027
2028 if (ftdi == NULL || ftdi->usb_dev == NULL)
2029 ftdi_error_return(-2, "USB device unavailable");
2030
2031 if (state)
2032 usb_val = SIO_SET_DTR_HIGH;
2033 else
2034 usb_val = SIO_SET_DTR_LOW;
2035
2036 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2037 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2038 NULL, 0, ftdi->usb_write_timeout) < 0)
2039 ftdi_error_return(-1, "set dtr failed");
2040
2041 return 0;
2042}
2043
2044/**
2045 Set rts line
2046
2047 \param ftdi pointer to ftdi_context
2048 \param state state to set line to (1 or 0)
2049
2050 \retval 0: all fine
2051 \retval -1: set rts failed
2052 \retval -2: USB device unavailable
2053*/
2054int ftdi_setrts(struct ftdi_context *ftdi, int state)
2055{
2056 unsigned short usb_val;
2057
2058 if (ftdi == NULL || ftdi->usb_dev == NULL)
2059 ftdi_error_return(-2, "USB device unavailable");
2060
2061 if (state)
2062 usb_val = SIO_SET_RTS_HIGH;
2063 else
2064 usb_val = SIO_SET_RTS_LOW;
2065
2066 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2067 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2068 NULL, 0, ftdi->usb_write_timeout) < 0)
2069 ftdi_error_return(-1, "set of rts failed");
2070
2071 return 0;
2072}
2073
2074/**
2075 Set dtr and rts line in one pass
2076
2077 \param ftdi pointer to ftdi_context
2078 \param dtr DTR state to set line to (1 or 0)
2079 \param rts RTS state to set line to (1 or 0)
2080
2081 \retval 0: all fine
2082 \retval -1: set dtr/rts failed
2083 \retval -2: USB device unavailable
2084 */
2085int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2086{
2087 unsigned short usb_val;
2088
2089 if (ftdi == NULL || ftdi->usb_dev == NULL)
2090 ftdi_error_return(-2, "USB device unavailable");
2091
2092 if (dtr)
2093 usb_val = SIO_SET_DTR_HIGH;
2094 else
2095 usb_val = SIO_SET_DTR_LOW;
2096
2097 if (rts)
2098 usb_val |= SIO_SET_RTS_HIGH;
2099 else
2100 usb_val |= SIO_SET_RTS_LOW;
2101
2102 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2103 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2104 NULL, 0, ftdi->usb_write_timeout) < 0)
2105 ftdi_error_return(-1, "set of rts/dtr failed");
2106
2107 return 0;
2108}
2109
2110/**
2111 Set the special event character
2112
2113 \param ftdi pointer to ftdi_context
2114 \param eventch Event character
2115 \param enable 0 to disable the event character, non-zero otherwise
2116
2117 \retval 0: all fine
2118 \retval -1: unable to set event character
2119 \retval -2: USB device unavailable
2120*/
2121int ftdi_set_event_char(struct ftdi_context *ftdi,
2122 unsigned char eventch, unsigned char enable)
2123{
2124 unsigned short usb_val;
2125
2126 if (ftdi == NULL || ftdi->usb_dev == NULL)
2127 ftdi_error_return(-2, "USB device unavailable");
2128
2129 usb_val = eventch;
2130 if (enable)
2131 usb_val |= 1 << 8;
2132
2133 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_EVENT_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2134 ftdi_error_return(-1, "setting event character failed");
2135
2136 return 0;
2137}
2138
2139/**
2140 Set error character
2141
2142 \param ftdi pointer to ftdi_context
2143 \param errorch Error character
2144 \param enable 0 to disable the error character, non-zero otherwise
2145
2146 \retval 0: all fine
2147 \retval -1: unable to set error character
2148 \retval -2: USB device unavailable
2149*/
2150int ftdi_set_error_char(struct ftdi_context *ftdi,
2151 unsigned char errorch, unsigned char enable)
2152{
2153 unsigned short usb_val;
2154
2155 if (ftdi == NULL || ftdi->usb_dev == NULL)
2156 ftdi_error_return(-2, "USB device unavailable");
2157
2158 usb_val = errorch;
2159 if (enable)
2160 usb_val |= 1 << 8;
2161
2162 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_ERROR_CHAR_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2163 ftdi_error_return(-1, "setting error character failed");
2164
2165 return 0;
2166}
2167
2168/**
2169 Init eeprom with default values.
2170 \param ftdi pointer to ftdi_context
2171 \param manufacturer String to use as Manufacturer
2172 \param product String to use as Product description
2173 \param serial String to use as Serial number description
2174
2175 \retval 0: all fine
2176 \retval -1: No struct ftdi_context
2177 \retval -2: No struct ftdi_eeprom
2178*/
2179int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
2180 char * product, char * serial)
2181{
2182 struct ftdi_eeprom *eeprom;
2183
2184 if (ftdi == NULL)
2185 ftdi_error_return(-1, "No struct ftdi_context");
2186
2187 if (ftdi->eeprom == NULL)
2188 ftdi_error_return(-2,"No struct ftdi_eeprom");
2189
2190 eeprom = ftdi->eeprom;
2191 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
2192
2193 eeprom->vendor_id = 0x0403;
2194 eeprom->use_serial = USE_SERIAL_NUM;
2195 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2196 (ftdi->type == TYPE_R))
2197 eeprom->product_id = 0x6001;
2198 else
2199 eeprom->product_id = 0x6010;
2200 if (ftdi->type == TYPE_AM)
2201 eeprom->usb_version = 0x0101;
2202 else
2203 eeprom->usb_version = 0x0200;
2204 eeprom->max_power = 100;
2205
2206 if (eeprom->manufacturer)
2207 free (eeprom->manufacturer);
2208 eeprom->manufacturer = NULL;
2209 if (manufacturer)
2210 {
2211 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2212 if (eeprom->manufacturer)
2213 strcpy(eeprom->manufacturer, manufacturer);
2214 }
2215
2216 if (eeprom->product)
2217 free (eeprom->product);
2218 eeprom->product = NULL;
2219 {
2220 eeprom->product = malloc(strlen(product)+1);
2221 if (eeprom->product)
2222 strcpy(eeprom->product, product);
2223 }
2224
2225 if (eeprom->serial)
2226 free (eeprom->serial);
2227 eeprom->serial = NULL;
2228 if (serial)
2229 {
2230 eeprom->serial = malloc(strlen(serial)+1);
2231 if (eeprom->serial)
2232 strcpy(eeprom->serial, serial);
2233 }
2234
2235
2236 if (ftdi->type == TYPE_R)
2237 {
2238 eeprom->max_power = 90;
2239 eeprom->size = 0x80;
2240 eeprom->cbus_function[0] = CBUS_TXLED;
2241 eeprom->cbus_function[1] = CBUS_RXLED;
2242 eeprom->cbus_function[2] = CBUS_TXDEN;
2243 eeprom->cbus_function[3] = CBUS_PWREN;
2244 eeprom->cbus_function[4] = CBUS_SLEEP;
2245 }
2246 else
2247 eeprom->size = -1;
2248 return 0;
2249}
2250
2251/**
2252 Build binary buffer from ftdi_eeprom structure.
2253 Output is suitable for ftdi_write_eeprom().
2254
2255 \param ftdi pointer to ftdi_context
2256
2257 \retval >=0: size of eeprom user area in bytes
2258 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2259 \retval -2: Invalid eeprom or ftdi pointer
2260 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2261 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2262 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2263 \retval -6: No connected EEPROM or EEPROM Type unknown
2264*/
2265int ftdi_eeprom_build(struct ftdi_context *ftdi)
2266{
2267 unsigned char i, j, eeprom_size_mask;
2268 unsigned short checksum, value;
2269 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2270 int user_area_size;
2271 struct ftdi_eeprom *eeprom;
2272 unsigned char * output;
2273
2274 if (ftdi == NULL)
2275 ftdi_error_return(-2,"No context");
2276 if (ftdi->eeprom == NULL)
2277 ftdi_error_return(-2,"No eeprom structure");
2278
2279 eeprom= ftdi->eeprom;
2280 output = eeprom->buf;
2281
2282 if (eeprom->chip == -1)
2283 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2284
2285 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2286 eeprom->size = 0x100;
2287 else
2288 eeprom->size = 0x80;
2289
2290 if (eeprom->manufacturer != NULL)
2291 manufacturer_size = strlen(eeprom->manufacturer);
2292 if (eeprom->product != NULL)
2293 product_size = strlen(eeprom->product);
2294 if (eeprom->serial != NULL)
2295 serial_size = strlen(eeprom->serial);
2296
2297 // eeprom size check
2298 switch (ftdi->type)
2299 {
2300 case TYPE_AM:
2301 case TYPE_BM:
2302 user_area_size = 96; // base size for strings (total of 48 characters)
2303 break;
2304 case TYPE_2232C:
2305 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2306 break;
2307 case TYPE_R:
2308 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2309 break;
2310 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2311 case TYPE_4232H:
2312 user_area_size = 86;
2313 default:
2314 user_area_size = 0;
2315 break;
2316 }
2317 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
2318
2319 if (user_area_size < 0)
2320 ftdi_error_return(-1,"eeprom size exceeded");
2321
2322 // empty eeprom
2323 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
2324
2325 // Bytes and Bits set for all Types
2326
2327 // Addr 02: Vendor ID
2328 output[0x02] = eeprom->vendor_id;
2329 output[0x03] = eeprom->vendor_id >> 8;
2330
2331 // Addr 04: Product ID
2332 output[0x04] = eeprom->product_id;
2333 output[0x05] = eeprom->product_id >> 8;
2334
2335 // Addr 06: Device release number (0400h for BM features)
2336 output[0x06] = 0x00;
2337 switch (ftdi->type)
2338 {
2339 case TYPE_AM:
2340 output[0x07] = 0x02;
2341 break;
2342 case TYPE_BM:
2343 output[0x07] = 0x04;
2344 break;
2345 case TYPE_2232C:
2346 output[0x07] = 0x05;
2347 break;
2348 case TYPE_R:
2349 output[0x07] = 0x06;
2350 break;
2351 case TYPE_2232H:
2352 output[0x07] = 0x07;
2353 break;
2354 case TYPE_4232H:
2355 output[0x07] = 0x08;
2356 break;
2357 default:
2358 output[0x07] = 0x00;
2359 }
2360
2361 // Addr 08: Config descriptor
2362 // Bit 7: always 1
2363 // Bit 6: 1 if this device is self powered, 0 if bus powered
2364 // Bit 5: 1 if this device uses remote wakeup
2365 // Bit 4-0: reserved - 0
2366 j = 0x80;
2367 if (eeprom->self_powered == 1)
2368 j |= 0x40;
2369 if (eeprom->remote_wakeup == 1)
2370 j |= 0x20;
2371 output[0x08] = j;
2372
2373 // Addr 09: Max power consumption: max power = value * 2 mA
2374 output[0x09] = eeprom->max_power>>1;
2375
2376 if (ftdi->type != TYPE_AM)
2377 {
2378 // Addr 0A: Chip configuration
2379 // Bit 7: 0 - reserved
2380 // Bit 6: 0 - reserved
2381 // Bit 5: 0 - reserved
2382 // Bit 4: 1 - Change USB version
2383 // Bit 3: 1 - Use the serial number string
2384 // Bit 2: 1 - Enable suspend pull downs for lower power
2385 // Bit 1: 1 - Out EndPoint is Isochronous
2386 // Bit 0: 1 - In EndPoint is Isochronous
2387 //
2388 j = 0;
2389 if (eeprom->in_is_isochronous == 1)
2390 j = j | 1;
2391 if (eeprom->out_is_isochronous == 1)
2392 j = j | 2;
2393 output[0x0A] = j;
2394 }
2395
2396 // Dynamic content
2397 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2398 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
2399 i = 0;
2400 switch (ftdi->type)
2401 {
2402 case TYPE_2232H:
2403 case TYPE_4232H:
2404 i += 2;
2405 case TYPE_R:
2406 i += 2;
2407 case TYPE_2232C:
2408 i += 2;
2409 case TYPE_AM:
2410 case TYPE_BM:
2411 i += 0x94;
2412 }
2413 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
2414 eeprom_size_mask = eeprom->size -1;
2415
2416 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2417 // Addr 0F: Length of manufacturer string
2418 // Output manufacturer
2419 output[0x0E] = i; // calculate offset
2420 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2421 output[i & eeprom_size_mask] = 0x03, i++; // type: string
2422 for (j = 0; j < manufacturer_size; j++)
2423 {
2424 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2425 output[i & eeprom_size_mask] = 0x00, i++;
2426 }
2427 output[0x0F] = manufacturer_size*2 + 2;
2428
2429 // Addr 10: Offset of the product string + 0x80, calculated later
2430 // Addr 11: Length of product string
2431 output[0x10] = i | 0x80; // calculate offset
2432 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2433 output[i & eeprom_size_mask] = 0x03, i++;
2434 for (j = 0; j < product_size; j++)
2435 {
2436 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2437 output[i & eeprom_size_mask] = 0x00, i++;
2438 }
2439 output[0x11] = product_size*2 + 2;
2440
2441 // Addr 12: Offset of the serial string + 0x80, calculated later
2442 // Addr 13: Length of serial string
2443 output[0x12] = i | 0x80; // calculate offset
2444 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2445 output[i & eeprom_size_mask] = 0x03, i++;
2446 for (j = 0; j < serial_size; j++)
2447 {
2448 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2449 output[i & eeprom_size_mask] = 0x00, i++;
2450 }
2451
2452 // Legacy port name and PnP fields for FT2232 and newer chips
2453 if (ftdi->type > TYPE_BM)
2454 {
2455 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2456 i++;
2457 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2458 i++;
2459 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2460 i++;
2461 }
2462
2463 output[0x13] = serial_size*2 + 2;
2464
2465 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
2466 {
2467 if (eeprom->use_serial == USE_SERIAL_NUM )
2468 output[0x0A] |= USE_SERIAL_NUM;
2469 else
2470 output[0x0A] &= ~USE_SERIAL_NUM;
2471 }
2472
2473 /* Bytes and Bits specific to (some) types
2474 Write linear, as this allows easier fixing*/
2475 switch (ftdi->type)
2476 {
2477 case TYPE_AM:
2478 break;
2479 case TYPE_BM:
2480 output[0x0C] = eeprom->usb_version & 0xff;
2481 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2482 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2483 output[0x0A] |= USE_USB_VERSION_BIT;
2484 else
2485 output[0x0A] &= ~USE_USB_VERSION_BIT;
2486
2487 break;
2488 case TYPE_2232C:
2489
2490 output[0x00] = (eeprom->channel_a_type);
2491 if ( eeprom->channel_a_driver == DRIVER_VCP)
2492 output[0x00] |= DRIVER_VCP;
2493 else
2494 output[0x00] &= ~DRIVER_VCP;
2495
2496 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2497 output[0x00] |= HIGH_CURRENT_DRIVE;
2498 else
2499 output[0x00] &= ~HIGH_CURRENT_DRIVE;
2500
2501 output[0x01] = (eeprom->channel_b_type);
2502 if ( eeprom->channel_b_driver == DRIVER_VCP)
2503 output[0x01] |= DRIVER_VCP;
2504 else
2505 output[0x01] &= ~DRIVER_VCP;
2506
2507 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2508 output[0x01] |= HIGH_CURRENT_DRIVE;
2509 else
2510 output[0x01] &= ~HIGH_CURRENT_DRIVE;
2511
2512 if (eeprom->in_is_isochronous == 1)
2513 output[0x0A] |= 0x1;
2514 else
2515 output[0x0A] &= ~0x1;
2516 if (eeprom->out_is_isochronous == 1)
2517 output[0x0A] |= 0x2;
2518 else
2519 output[0x0A] &= ~0x2;
2520 if (eeprom->suspend_pull_downs == 1)
2521 output[0x0A] |= 0x4;
2522 else
2523 output[0x0A] &= ~0x4;
2524 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2525 output[0x0A] |= USE_USB_VERSION_BIT;
2526 else
2527 output[0x0A] &= ~USE_USB_VERSION_BIT;
2528
2529 output[0x0C] = eeprom->usb_version & 0xff;
2530 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2531 output[0x14] = eeprom->chip;
2532 break;
2533 case TYPE_R:
2534 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2535 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2536 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
2537
2538 if (eeprom->suspend_pull_downs == 1)
2539 output[0x0A] |= 0x4;
2540 else
2541 output[0x0A] &= ~0x4;
2542 output[0x0B] = eeprom->invert;
2543 output[0x0C] = eeprom->usb_version & 0xff;
2544 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2545
2546 if (eeprom->cbus_function[0] > CBUS_BB)
2547 output[0x14] = CBUS_TXLED;
2548 else
2549 output[0x14] = eeprom->cbus_function[0];
2550
2551 if (eeprom->cbus_function[1] > CBUS_BB)
2552 output[0x14] |= CBUS_RXLED<<4;
2553 else
2554 output[0x14] |= eeprom->cbus_function[1]<<4;
2555
2556 if (eeprom->cbus_function[2] > CBUS_BB)
2557 output[0x15] = CBUS_TXDEN;
2558 else
2559 output[0x15] = eeprom->cbus_function[2];
2560
2561 if (eeprom->cbus_function[3] > CBUS_BB)
2562 output[0x15] |= CBUS_PWREN<<4;
2563 else
2564 output[0x15] |= eeprom->cbus_function[3]<<4;
2565
2566 if (eeprom->cbus_function[4] > CBUS_CLK6)
2567 output[0x16] = CBUS_SLEEP;
2568 else
2569 output[0x16] = eeprom->cbus_function[4];
2570 break;
2571 case TYPE_2232H:
2572 output[0x00] = (eeprom->channel_a_type);
2573 if ( eeprom->channel_a_driver == DRIVER_VCP)
2574 output[0x00] |= DRIVER_VCP;
2575 else
2576 output[0x00] &= ~DRIVER_VCP;
2577
2578 output[0x01] = (eeprom->channel_b_type);
2579 if ( eeprom->channel_b_driver == DRIVER_VCP)
2580 output[0x01] |= DRIVER_VCP;
2581 else
2582 output[0x01] &= ~DRIVER_VCP;
2583 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2584 output[0x01] |= SUSPEND_DBUS7_BIT;
2585 else
2586 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2587
2588 if (eeprom->suspend_pull_downs == 1)
2589 output[0x0A] |= 0x4;
2590 else
2591 output[0x0A] &= ~0x4;
2592
2593 if (eeprom->group0_drive > DRIVE_16MA)
2594 output[0x0c] |= DRIVE_16MA;
2595 else
2596 output[0x0c] |= eeprom->group0_drive;
2597 if (eeprom->group0_schmitt == IS_SCHMITT)
2598 output[0x0c] |= IS_SCHMITT;
2599 if (eeprom->group0_slew == SLOW_SLEW)
2600 output[0x0c] |= SLOW_SLEW;
2601
2602 if (eeprom->group1_drive > DRIVE_16MA)
2603 output[0x0c] |= DRIVE_16MA<<4;
2604 else
2605 output[0x0c] |= eeprom->group1_drive<<4;
2606 if (eeprom->group1_schmitt == IS_SCHMITT)
2607 output[0x0c] |= IS_SCHMITT<<4;
2608 if (eeprom->group1_slew == SLOW_SLEW)
2609 output[0x0c] |= SLOW_SLEW<<4;
2610
2611 if (eeprom->group2_drive > DRIVE_16MA)
2612 output[0x0d] |= DRIVE_16MA;
2613 else
2614 output[0x0d] |= eeprom->group2_drive;
2615 if (eeprom->group2_schmitt == IS_SCHMITT)
2616 output[0x0d] |= IS_SCHMITT;
2617 if (eeprom->group2_slew == SLOW_SLEW)
2618 output[0x0d] |= SLOW_SLEW;
2619
2620 if (eeprom->group3_drive > DRIVE_16MA)
2621 output[0x0d] |= DRIVE_16MA<<4;
2622 else
2623 output[0x0d] |= eeprom->group3_drive<<4;
2624 if (eeprom->group3_schmitt == IS_SCHMITT)
2625 output[0x0d] |= IS_SCHMITT<<4;
2626 if (eeprom->group3_slew == SLOW_SLEW)
2627 output[0x0d] |= SLOW_SLEW<<4;
2628
2629 output[0x18] = eeprom->chip;
2630
2631 break;
2632 case TYPE_4232H:
2633 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
2634 }
2635
2636 // calculate checksum
2637 checksum = 0xAAAA;
2638
2639 for (i = 0; i < eeprom->size/2-1; i++)
2640 {
2641 value = output[i*2];
2642 value += output[(i*2)+1] << 8;
2643
2644 checksum = value^checksum;
2645 checksum = (checksum << 1) | (checksum >> 15);
2646 }
2647
2648 output[eeprom->size-2] = checksum;
2649 output[eeprom->size-1] = checksum >> 8;
2650
2651 return user_area_size;
2652}
2653
2654/**
2655 Decode binary EEPROM image into an ftdi_eeprom structure.
2656
2657 \param ftdi pointer to ftdi_context
2658 \param verbose Decode EEPROM on stdout
2659
2660 \retval 0: all fine
2661 \retval -1: something went wrong
2662
2663 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2664 FIXME: Strings are malloc'ed here and should be freed somewhere
2665*/
2666int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
2667{
2668 unsigned char i, j;
2669 unsigned short checksum, eeprom_checksum, value;
2670 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2671 int eeprom_size;
2672 struct ftdi_eeprom *eeprom;
2673 unsigned char *buf = ftdi->eeprom->buf;
2674 int release;
2675
2676 if (ftdi == NULL)
2677 ftdi_error_return(-1,"No context");
2678 if (ftdi->eeprom == NULL)
2679 ftdi_error_return(-1,"No eeprom structure");
2680
2681 eeprom = ftdi->eeprom;
2682 eeprom_size = eeprom->size;
2683
2684 // Addr 02: Vendor ID
2685 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2686
2687 // Addr 04: Product ID
2688 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
2689
2690 release = buf[0x06] + (buf[0x07]<<8);
2691
2692 // Addr 08: Config descriptor
2693 // Bit 7: always 1
2694 // Bit 6: 1 if this device is self powered, 0 if bus powered
2695 // Bit 5: 1 if this device uses remote wakeup
2696 eeprom->self_powered = buf[0x08] & 0x40;
2697 eeprom->remote_wakeup = buf[0x08] & 0x20;
2698
2699 // Addr 09: Max power consumption: max power = value * 2 mA
2700 eeprom->max_power = buf[0x09];
2701
2702 // Addr 0A: Chip configuration
2703 // Bit 7: 0 - reserved
2704 // Bit 6: 0 - reserved
2705 // Bit 5: 0 - reserved
2706 // Bit 4: 1 - Change USB version on BM and 2232C
2707 // Bit 3: 1 - Use the serial number string
2708 // Bit 2: 1 - Enable suspend pull downs for lower power
2709 // Bit 1: 1 - Out EndPoint is Isochronous
2710 // Bit 0: 1 - In EndPoint is Isochronous
2711 //
2712 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2713 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2714 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
2715 eeprom->use_serial = buf[0x0A] & USE_SERIAL_NUM;
2716 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
2717
2718 // Addr 0C: USB version low byte when 0x0A
2719 // Addr 0D: USB version high byte when 0x0A
2720 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
2721
2722 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2723 // Addr 0F: Length of manufacturer string
2724 manufacturer_size = buf[0x0F]/2;
2725 if (eeprom->manufacturer)
2726 free(eeprom->manufacturer);
2727 if (manufacturer_size > 0)
2728 {
2729 eeprom->manufacturer = malloc(manufacturer_size);
2730 if (eeprom->manufacturer)
2731 {
2732 // Decode manufacturer
2733 i = buf[0x0E] & (eeprom_size -1); // offset
2734 for (j=0;j<manufacturer_size-1;j++)
2735 {
2736 eeprom->manufacturer[j] = buf[2*j+i+2];
2737 }
2738 eeprom->manufacturer[j] = '\0';
2739 }
2740 }
2741 else eeprom->manufacturer = NULL;
2742
2743 // Addr 10: Offset of the product string + 0x80, calculated later
2744 // Addr 11: Length of product string
2745 if (eeprom->product)
2746 free(eeprom->product);
2747 product_size = buf[0x11]/2;
2748 if (product_size > 0)
2749 {
2750 eeprom->product = malloc(product_size);
2751 if (eeprom->product)
2752 {
2753 // Decode product name
2754 i = buf[0x10] & (eeprom_size -1); // offset
2755 for (j=0;j<product_size-1;j++)
2756 {
2757 eeprom->product[j] = buf[2*j+i+2];
2758 }
2759 eeprom->product[j] = '\0';
2760 }
2761 }
2762 else eeprom->product = NULL;
2763
2764 // Addr 12: Offset of the serial string + 0x80, calculated later
2765 // Addr 13: Length of serial string
2766 if (eeprom->serial)
2767 free(eeprom->serial);
2768 serial_size = buf[0x13]/2;
2769 if (serial_size > 0)
2770 {
2771 eeprom->serial = malloc(serial_size);
2772 if (eeprom->serial)
2773 {
2774 // Decode serial
2775 i = buf[0x12] & (eeprom_size -1); // offset
2776 for (j=0;j<serial_size-1;j++)
2777 {
2778 eeprom->serial[j] = buf[2*j+i+2];
2779 }
2780 eeprom->serial[j] = '\0';
2781 }
2782 }
2783 else eeprom->serial = NULL;
2784
2785 // verify checksum
2786 checksum = 0xAAAA;
2787
2788 for (i = 0; i < eeprom_size/2-1; i++)
2789 {
2790 value = buf[i*2];
2791 value += buf[(i*2)+1] << 8;
2792
2793 checksum = value^checksum;
2794 checksum = (checksum << 1) | (checksum >> 15);
2795 }
2796
2797 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2798
2799 if (eeprom_checksum != checksum)
2800 {
2801 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2802 ftdi_error_return(-1,"EEPROM checksum error");
2803 }
2804
2805 eeprom->channel_a_type = 0;
2806 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
2807 {
2808 eeprom->chip = -1;
2809 }
2810 else if (ftdi->type == TYPE_2232C)
2811 {
2812 eeprom->channel_a_type = buf[0x00] & 0x7;
2813 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2814 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
2815 eeprom->channel_b_type = buf[0x01] & 0x7;
2816 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2817 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
2818 eeprom->chip = buf[0x14];
2819 }
2820 else if (ftdi->type == TYPE_R)
2821 {
2822 /* TYPE_R flags D2XX, not VCP as all others*/
2823 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
2824 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
2825 if ( (buf[0x01]&0x40) != 0x40)
2826 fprintf(stderr,
2827 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
2828 " If this happened with the\n"
2829 " EEPROM programmed by FTDI tools, please report "
2830 "to libftdi@developer.intra2net.com\n");
2831
2832 eeprom->chip = buf[0x16];
2833 // Addr 0B: Invert data lines
2834 // Works only on FT232R, not FT245R, but no way to distinguish
2835 eeprom->invert = buf[0x0B];
2836 // Addr 14: CBUS function: CBUS0, CBUS1
2837 // Addr 15: CBUS function: CBUS2, CBUS3
2838 // Addr 16: CBUS function: CBUS5
2839 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2840 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2841 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2842 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2843 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2844 }
2845 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
2846 {
2847 eeprom->channel_a_type = buf[0x00] & 0x7;
2848 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
2849 eeprom->channel_b_type = buf[0x01] & 0x7;
2850 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
2851
2852 if (ftdi->type == TYPE_2232H)
2853 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2854
2855 eeprom->chip = buf[0x18];
2856 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
2857 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
2858 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
2859 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
2860 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
2861 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
2862 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
2863 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
2864 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
2865 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
2866 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
2867 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
2868 }
2869
2870 if (verbose)
2871 {
2872 char *channel_mode[] = {"UART","245","CPU", "unknown", "OPTO"};
2873 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
2874 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
2875 fprintf(stdout, "Release: 0x%04x\n",release);
2876
2877 if (eeprom->self_powered)
2878 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
2879 else
2880 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
2881 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
2882 if (eeprom->manufacturer)
2883 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
2884 if (eeprom->product)
2885 fprintf(stdout, "Product: %s\n",eeprom->product);
2886 if (eeprom->serial)
2887 fprintf(stdout, "Serial: %s\n",eeprom->serial);
2888 fprintf(stdout, "Checksum : %04x\n", checksum);
2889 if (ftdi->type == TYPE_R)
2890 fprintf(stdout, "Internal EEPROM\n");
2891 else if (eeprom->chip >= 0x46)
2892 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
2893 if (eeprom->suspend_dbus7)
2894 fprintf(stdout, "Suspend on DBUS7\n");
2895 if (eeprom->suspend_pull_downs)
2896 fprintf(stdout, "Pull IO pins low during suspend\n");
2897 if (eeprom->remote_wakeup)
2898 fprintf(stdout, "Enable Remote Wake Up\n");
2899 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
2900 if (ftdi->type >= TYPE_2232C)
2901 fprintf(stdout,"Channel A has Mode %s%s%s\n",
2902 channel_mode[eeprom->channel_a_type],
2903 (eeprom->channel_a_driver)?" VCP":"",
2904 (eeprom->high_current_a)?" High Current IO":"");
2905 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R))
2906 fprintf(stdout,"Channel B has Mode %s%s%s\n",
2907 channel_mode[eeprom->channel_b_type],
2908 (eeprom->channel_b_driver)?" VCP":"",
2909 (eeprom->high_current_b)?" High Current IO":"");
2910 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
2911 eeprom->use_usb_version == USE_USB_VERSION_BIT)
2912 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
2913
2914 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
2915 {
2916 fprintf(stdout,"%s has %d mA drive%s%s\n",
2917 (ftdi->type == TYPE_2232H)?"AL":"A",
2918 (eeprom->group0_drive+1) *4,
2919 (eeprom->group0_schmitt)?" Schmitt Input":"",
2920 (eeprom->group0_slew)?" Slow Slew":"");
2921 fprintf(stdout,"%s has %d mA drive%s%s\n",
2922 (ftdi->type == TYPE_2232H)?"AH":"B",
2923 (eeprom->group1_drive+1) *4,
2924 (eeprom->group1_schmitt)?" Schmitt Input":"",
2925 (eeprom->group1_slew)?" Slow Slew":"");
2926 fprintf(stdout,"%s has %d mA drive%s%s\n",
2927 (ftdi->type == TYPE_2232H)?"BL":"C",
2928 (eeprom->group2_drive+1) *4,
2929 (eeprom->group2_schmitt)?" Schmitt Input":"",
2930 (eeprom->group2_slew)?" Slow Slew":"");
2931 fprintf(stdout,"%s has %d mA drive%s%s\n",
2932 (ftdi->type == TYPE_2232H)?"BH":"D",
2933 (eeprom->group3_drive+1) *4,
2934 (eeprom->group3_schmitt)?" Schmitt Input":"",
2935 (eeprom->group3_slew)?" Slow Slew":"");
2936 }
2937 if (ftdi->type == TYPE_R)
2938 {
2939 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
2940 "SLEEP","CLK48","CLK24","CLK12","CLK6",
2941 "IOMODE","BB_WR","BB_RD"
2942 };
2943 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
2944 int i;
2945
2946 if (eeprom->invert)
2947 {
2948 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
2949 fprintf(stdout,"Inverted bits:");
2950 for (i=0; i<8; i++)
2951 if ((eeprom->invert & (1<<i)) == (1<<i))
2952 fprintf(stdout," %s",r_bits[i]);
2953 fprintf(stdout,"\n");
2954 }
2955 for (i=0; i<5; i++)
2956 {
2957 if (eeprom->cbus_function[i]<CBUS_BB)
2958 fprintf(stdout,"C%d Function: %s\n", i,
2959 cbus_mux[eeprom->cbus_function[i]]);
2960 else
2961 {
2962 /* FIXME for Uwe: This results in an access above array bounds.
2963 Also I couldn't find documentation about this mode.
2964 fprintf(stdout,"C%d BB Function: %s\n", i,
2965 cbus_BB[i]);
2966 */
2967 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
2968 (void)cbus_BB;
2969 }
2970 }
2971 }
2972 }
2973 return 0;
2974}
2975
2976/**
2977 Get a value from the decoded EEPROM structure
2978
2979 \param ftdi pointer to ftdi_context
2980 \param value_name Enum of the value to query
2981 \param value Pointer to store read value
2982
2983 \retval 0: all fine
2984 \retval -1: Value doesn't exist
2985*/
2986int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
2987{
2988 switch (value_name)
2989 {
2990 case VENDOR_ID:
2991 *value = ftdi->eeprom->vendor_id;
2992 break;
2993 case PRODUCT_ID:
2994 *value = ftdi->eeprom->product_id;
2995 break;
2996 case SELF_POWERED:
2997 *value = ftdi->eeprom->self_powered;
2998 break;
2999 case REMOTE_WAKEUP:
3000 *value = ftdi->eeprom->remote_wakeup;
3001 break;
3002 case IS_NOT_PNP:
3003 *value = ftdi->eeprom->is_not_pnp;
3004 break;
3005 case SUSPEND_DBUS7:
3006 *value = ftdi->eeprom->suspend_dbus7;
3007 break;
3008 case IN_IS_ISOCHRONOUS:
3009 *value = ftdi->eeprom->in_is_isochronous;
3010 break;
3011 case SUSPEND_PULL_DOWNS:
3012 *value = ftdi->eeprom->suspend_pull_downs;
3013 break;
3014 case USE_SERIAL:
3015 *value = ftdi->eeprom->use_serial;
3016 break;
3017 case USB_VERSION:
3018 *value = ftdi->eeprom->usb_version;
3019 break;
3020 case MAX_POWER:
3021 *value = ftdi->eeprom->max_power;
3022 break;
3023 case CHANNEL_A_TYPE:
3024 *value = ftdi->eeprom->channel_a_type;
3025 break;
3026 case CHANNEL_B_TYPE:
3027 *value = ftdi->eeprom->channel_b_type;
3028 break;
3029 case CHANNEL_A_DRIVER:
3030 *value = ftdi->eeprom->channel_a_driver;
3031 break;
3032 case CHANNEL_B_DRIVER:
3033 *value = ftdi->eeprom->channel_b_driver;
3034 break;
3035 case CBUS_FUNCTION_0:
3036 *value = ftdi->eeprom->cbus_function[0];
3037 break;
3038 case CBUS_FUNCTION_1:
3039 *value = ftdi->eeprom->cbus_function[1];
3040 break;
3041 case CBUS_FUNCTION_2:
3042 *value = ftdi->eeprom->cbus_function[2];
3043 break;
3044 case CBUS_FUNCTION_3:
3045 *value = ftdi->eeprom->cbus_function[3];
3046 break;
3047 case CBUS_FUNCTION_4:
3048 *value = ftdi->eeprom->cbus_function[4];
3049 break;
3050 case HIGH_CURRENT:
3051 *value = ftdi->eeprom->high_current;
3052 break;
3053 case HIGH_CURRENT_A:
3054 *value = ftdi->eeprom->high_current_a;
3055 break;
3056 case HIGH_CURRENT_B:
3057 *value = ftdi->eeprom->high_current_b;
3058 break;
3059 case INVERT:
3060 *value = ftdi->eeprom->invert;
3061 break;
3062 case GROUP0_DRIVE:
3063 *value = ftdi->eeprom->group0_drive;
3064 break;
3065 case GROUP0_SCHMITT:
3066 *value = ftdi->eeprom->group0_schmitt;
3067 break;
3068 case GROUP0_SLEW:
3069 *value = ftdi->eeprom->group0_slew;
3070 break;
3071 case GROUP1_DRIVE:
3072 *value = ftdi->eeprom->group1_drive;
3073 break;
3074 case GROUP1_SCHMITT:
3075 *value = ftdi->eeprom->group1_schmitt;
3076 break;
3077 case GROUP1_SLEW:
3078 *value = ftdi->eeprom->group1_slew;
3079 break;
3080 case GROUP2_DRIVE:
3081 *value = ftdi->eeprom->group2_drive;
3082 break;
3083 case GROUP2_SCHMITT:
3084 *value = ftdi->eeprom->group2_schmitt;
3085 break;
3086 case GROUP2_SLEW:
3087 *value = ftdi->eeprom->group2_slew;
3088 break;
3089 case GROUP3_DRIVE:
3090 *value = ftdi->eeprom->group3_drive;
3091 break;
3092 case GROUP3_SCHMITT:
3093 *value = ftdi->eeprom->group3_schmitt;
3094 break;
3095 case GROUP3_SLEW:
3096 *value = ftdi->eeprom->group3_slew;
3097 break;
3098 case CHIP_TYPE:
3099 *value = ftdi->eeprom->chip;
3100 break;
3101 case CHIP_SIZE:
3102 *value = ftdi->eeprom->size;
3103 break;
3104 default:
3105 ftdi_error_return(-1, "Request for unknown EEPROM value");
3106 }
3107 return 0;
3108}
3109
3110/**
3111 Set a value in the decoded EEPROM Structure
3112 No parameter checking is performed
3113
3114 \param ftdi pointer to ftdi_context
3115 \param value_name Enum of the value to query
3116 \param value to set
3117
3118 \retval 0: all fine
3119 \retval -1: Value doesn't exist
3120 \retval -2: Value not user settable
3121*/
3122int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3123{
3124 switch (value_name)
3125 {
3126 case VENDOR_ID:
3127 ftdi->eeprom->vendor_id = value;
3128 break;
3129 case PRODUCT_ID:
3130 ftdi->eeprom->product_id = value;
3131 break;
3132 case SELF_POWERED:
3133 ftdi->eeprom->self_powered = value;
3134 break;
3135 case REMOTE_WAKEUP:
3136 ftdi->eeprom->remote_wakeup = value;
3137 break;
3138 case IS_NOT_PNP:
3139 ftdi->eeprom->is_not_pnp = value;
3140 break;
3141 case SUSPEND_DBUS7:
3142 ftdi->eeprom->suspend_dbus7 = value;
3143 break;
3144 case IN_IS_ISOCHRONOUS:
3145 ftdi->eeprom->in_is_isochronous = value;
3146 break;
3147 case SUSPEND_PULL_DOWNS:
3148 ftdi->eeprom->suspend_pull_downs = value;
3149 break;
3150 case USE_SERIAL:
3151 ftdi->eeprom->use_serial = value;
3152 break;
3153 case USB_VERSION:
3154 ftdi->eeprom->usb_version = value;
3155 break;
3156 case MAX_POWER:
3157 ftdi->eeprom->max_power = value;
3158 break;
3159 case CHANNEL_A_TYPE:
3160 ftdi->eeprom->channel_a_type = value;
3161 break;
3162 case CHANNEL_B_TYPE:
3163 ftdi->eeprom->channel_b_type = value;
3164 break;
3165 case CHANNEL_A_DRIVER:
3166 ftdi->eeprom->channel_a_driver = value;
3167 break;
3168 case CHANNEL_B_DRIVER:
3169 ftdi->eeprom->channel_b_driver = value;
3170 break;
3171 case CBUS_FUNCTION_0:
3172 ftdi->eeprom->cbus_function[0] = value;
3173 break;
3174 case CBUS_FUNCTION_1:
3175 ftdi->eeprom->cbus_function[1] = value;
3176 break;
3177 case CBUS_FUNCTION_2:
3178 ftdi->eeprom->cbus_function[2] = value;
3179 break;
3180 case CBUS_FUNCTION_3:
3181 ftdi->eeprom->cbus_function[3] = value;
3182 break;
3183 case CBUS_FUNCTION_4:
3184 ftdi->eeprom->cbus_function[4] = value;
3185 break;
3186 case HIGH_CURRENT:
3187 ftdi->eeprom->high_current = value;
3188 break;
3189 case HIGH_CURRENT_A:
3190 ftdi->eeprom->high_current_a = value;
3191 break;
3192 case HIGH_CURRENT_B:
3193 ftdi->eeprom->high_current_b = value;
3194 break;
3195 case INVERT:
3196 ftdi->eeprom->invert = value;
3197 break;
3198 case GROUP0_DRIVE:
3199 ftdi->eeprom->group0_drive = value;
3200 break;
3201 case GROUP0_SCHMITT:
3202 ftdi->eeprom->group0_schmitt = value;
3203 break;
3204 case GROUP0_SLEW:
3205 ftdi->eeprom->group0_slew = value;
3206 break;
3207 case GROUP1_DRIVE:
3208 ftdi->eeprom->group1_drive = value;
3209 break;
3210 case GROUP1_SCHMITT:
3211 ftdi->eeprom->group1_schmitt = value;
3212 break;
3213 case GROUP1_SLEW:
3214 ftdi->eeprom->group1_slew = value;
3215 break;
3216 case GROUP2_DRIVE:
3217 ftdi->eeprom->group2_drive = value;
3218 break;
3219 case GROUP2_SCHMITT:
3220 ftdi->eeprom->group2_schmitt = value;
3221 break;
3222 case GROUP2_SLEW:
3223 ftdi->eeprom->group2_slew = value;
3224 break;
3225 case GROUP3_DRIVE:
3226 ftdi->eeprom->group3_drive = value;
3227 break;
3228 case GROUP3_SCHMITT:
3229 ftdi->eeprom->group3_schmitt = value;
3230 break;
3231 case GROUP3_SLEW:
3232 ftdi->eeprom->group3_slew = value;
3233 break;
3234 case CHIP_TYPE:
3235 ftdi->eeprom->chip = value;
3236 break;
3237 case CHIP_SIZE:
3238 ftdi_error_return(-2, "EEPROM Value can't be changed");
3239 default :
3240 ftdi_error_return(-1, "Request to unknown EEPROM value");
3241 }
3242 return 0;
3243}
3244
3245/** Get the read-only buffer to the binary EEPROM content
3246
3247 \param ftdi pointer to ftdi_context
3248 \param buf buffer to receive EEPROM content
3249 \param size Size of receiving buffer
3250
3251 \retval 0: All fine
3252 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
3253*/
3254int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3255{
3256 if (!ftdi || !(ftdi->eeprom))
3257 ftdi_error_return(-1, "No appropriate structure");
3258 memcpy(buf, ftdi->eeprom->buf, size);
3259 return 0;
3260}
3261
3262/**
3263 Read eeprom location
3264
3265 \param ftdi pointer to ftdi_context
3266 \param eeprom_addr Address of eeprom location to be read
3267 \param eeprom_val Pointer to store read eeprom location
3268
3269 \retval 0: all fine
3270 \retval -1: read failed
3271 \retval -2: USB device unavailable
3272*/
3273int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3274{
3275 if (ftdi == NULL || ftdi->usb_dev == NULL)
3276 ftdi_error_return(-2, "USB device unavailable");
3277
3278 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (unsigned char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
3279 ftdi_error_return(-1, "reading eeprom failed");
3280
3281 return 0;
3282}
3283
3284/**
3285 Read eeprom
3286
3287 \param ftdi pointer to ftdi_context
3288
3289 \retval 0: all fine
3290 \retval -1: read failed
3291 \retval -2: USB device unavailable
3292*/
3293int ftdi_read_eeprom(struct ftdi_context *ftdi)
3294{
3295 int i;
3296 unsigned char *buf;
3297
3298 if (ftdi == NULL || ftdi->usb_dev == NULL)
3299 ftdi_error_return(-2, "USB device unavailable");
3300 buf = ftdi->eeprom->buf;
3301
3302 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
3303 {
3304 if (libusb_control_transfer(
3305 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3306 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
3307 ftdi_error_return(-1, "reading eeprom failed");
3308 }
3309
3310 if (ftdi->type == TYPE_R)
3311 ftdi->eeprom->size = 0x80;
3312 /* Guesses size of eeprom by comparing halves
3313 - will not work with blank eeprom */
3314 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
3315 ftdi->eeprom->size = -1;
3316 else if (memcmp(buf,&buf[0x80],0x80) == 0)
3317 ftdi->eeprom->size = 0x80;
3318 else if (memcmp(buf,&buf[0x40],0x40) == 0)
3319 ftdi->eeprom->size = 0x40;
3320 else
3321 ftdi->eeprom->size = 0x100;
3322 return 0;
3323}
3324
3325/*
3326 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3327 Function is only used internally
3328 \internal
3329*/
3330static unsigned char ftdi_read_chipid_shift(unsigned char value)
3331{
3332 return ((value & 1) << 1) |
3333 ((value & 2) << 5) |
3334 ((value & 4) >> 2) |
3335 ((value & 8) << 4) |
3336 ((value & 16) >> 1) |
3337 ((value & 32) >> 1) |
3338 ((value & 64) >> 4) |
3339 ((value & 128) >> 2);
3340}
3341
3342/**
3343 Read the FTDIChip-ID from R-type devices
3344
3345 \param ftdi pointer to ftdi_context
3346 \param chipid Pointer to store FTDIChip-ID
3347
3348 \retval 0: all fine
3349 \retval -1: read failed
3350 \retval -2: USB device unavailable
3351*/
3352int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3353{
3354 unsigned int a = 0, b = 0;
3355
3356 if (ftdi == NULL || ftdi->usb_dev == NULL)
3357 ftdi_error_return(-2, "USB device unavailable");
3358
3359 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (unsigned char *)&a, 2, ftdi->usb_read_timeout) == 2)
3360 {
3361 a = a << 8 | a >> 8;
3362 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (unsigned char *)&b, 2, ftdi->usb_read_timeout) == 2)
3363 {
3364 b = b << 8 | b >> 8;
3365 a = (a << 16) | (b & 0xFFFF);
3366 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3367 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
3368 *chipid = a ^ 0xa5f0f7d1;
3369 return 0;
3370 }
3371 }
3372
3373 ftdi_error_return(-1, "read of FTDIChip-ID failed");
3374}
3375
3376/**
3377 Write eeprom location
3378
3379 \param ftdi pointer to ftdi_context
3380 \param eeprom_addr Address of eeprom location to be written
3381 \param eeprom_val Value to be written
3382
3383 \retval 0: all fine
3384 \retval -1: write failed
3385 \retval -2: USB device unavailable
3386 \retval -3: Invalid access to checksum protected area below 0x80
3387 \retval -4: Device can't access unprotected area
3388 \retval -5: Reading chip type failed
3389*/
3390int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
3391 unsigned short eeprom_val)
3392{
3393 int chip_type_location;
3394 unsigned short chip_type;
3395
3396 if (ftdi == NULL || ftdi->usb_dev == NULL)
3397 ftdi_error_return(-2, "USB device unavailable");
3398
3399 if (eeprom_addr <0x80)
3400 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3401
3402
3403 switch (ftdi->type)
3404 {
3405 case TYPE_BM:
3406 case TYPE_2232C:
3407 chip_type_location = 0x14;
3408 break;
3409 case TYPE_2232H:
3410 case TYPE_4232H:
3411 chip_type_location = 0x18;
3412 break;
3413 default:
3414 ftdi_error_return(-4, "Device can't access unprotected area");
3415 }
3416
3417 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
3418 ftdi_error_return(-5, "Reading failed failed");
3419 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3420 if ((chip_type & 0xff) != 0x66)
3421 {
3422 ftdi_error_return(-6, "EEPROM is not of 93x66");
3423 }
3424
3425 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3426 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3427 NULL, 0, ftdi->usb_write_timeout) != 0)
3428 ftdi_error_return(-1, "unable to write eeprom");
3429
3430 return 0;
3431}
3432
3433/**
3434 Write eeprom
3435
3436 \param ftdi pointer to ftdi_context
3437
3438 \retval 0: all fine
3439 \retval -1: read failed
3440 \retval -2: USB device unavailable
3441*/
3442int ftdi_write_eeprom(struct ftdi_context *ftdi)
3443{
3444 unsigned short usb_val, status;
3445 int i, ret;
3446 unsigned char *eeprom;
3447
3448 if (ftdi == NULL || ftdi->usb_dev == NULL)
3449 ftdi_error_return(-2, "USB device unavailable");
3450 eeprom = ftdi->eeprom->buf;
3451
3452 /* These commands were traced while running MProg */
3453 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3454 return ret;
3455 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3456 return ret;
3457 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3458 return ret;
3459
3460 for (i = 0; i < ftdi->eeprom->size/2; i++)
3461 {
3462 usb_val = eeprom[i*2];
3463 usb_val += eeprom[(i*2)+1] << 8;
3464 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3465 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3466 NULL, 0, ftdi->usb_write_timeout) < 0)
3467 ftdi_error_return(-1, "unable to write eeprom");
3468 }
3469
3470 return 0;
3471}
3472
3473/**
3474 Erase eeprom
3475
3476 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3477
3478 \param ftdi pointer to ftdi_context
3479
3480 \retval 0: all fine
3481 \retval -1: erase failed
3482 \retval -2: USB device unavailable
3483 \retval -3: Writing magic failed
3484 \retval -4: Read EEPROM failed
3485 \retval -5: Unexpected EEPROM value
3486*/
3487#define MAGIC 0x55aa
3488int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3489{
3490 unsigned short eeprom_value;
3491 if (ftdi == NULL || ftdi->usb_dev == NULL)
3492 ftdi_error_return(-2, "USB device unavailable");
3493
3494 if (ftdi->type == TYPE_R)
3495 {
3496 ftdi->eeprom->chip = 0;
3497 return 0;
3498 }
3499
3500 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3501 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3502 ftdi_error_return(-1, "unable to erase eeprom");
3503
3504
3505 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3506 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3507 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3508 Chip is 93x66 if magic is only read at word position 0xc0*/
3509 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3510 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3511 NULL, 0, ftdi->usb_write_timeout) != 0)
3512 ftdi_error_return(-3, "Writing magic failed");
3513 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
3514 ftdi_error_return(-4, "Reading failed failed");
3515 if (eeprom_value == MAGIC)
3516 {
3517 ftdi->eeprom->chip = 0x46;
3518 }
3519 else
3520 {
3521 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
3522 ftdi_error_return(-4, "Reading failed failed");
3523 if (eeprom_value == MAGIC)
3524 ftdi->eeprom->chip = 0x56;
3525 else
3526 {
3527 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
3528 ftdi_error_return(-4, "Reading failed failed");
3529 if (eeprom_value == MAGIC)
3530 ftdi->eeprom->chip = 0x66;
3531 else
3532 {
3533 ftdi->eeprom->chip = -1;
3534 }
3535 }
3536 }
3537 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
3538 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3539 ftdi_error_return(-1, "unable to erase eeprom");
3540 return 0;
3541}
3542
3543/**
3544 Get string representation for last error code
3545
3546 \param ftdi pointer to ftdi_context
3547
3548 \retval Pointer to error string
3549*/
3550char *ftdi_get_error_string (struct ftdi_context *ftdi)
3551{
3552 if (ftdi == NULL)
3553 return "";
3554
3555 return ftdi->error_str;
3556}
3557
3558/* @} end of doxygen libftdi group */