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