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