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