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