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