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