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