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