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