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