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