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