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