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