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