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