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