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