test/baudrate.cpp: Change the expected results to the present results. AM still has...
[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
9956d428
UB
1072
1073 value[13:0] of value is the divisor
1074 index[9] mean 12 MHz Base(120 MHz/10) rate versus 3 MHz (48 MHz/16) else
1075
1076 H Type have all features above with
1077 {index[8],value[15:14]} is the encoded subdivisor
1078
1079 FT232R, FT2232 and FT232BM have no option for 12 MHz and with
1080 {index[0],value[15:14]} is the encoded subdivisor
1081
1082 AM Type chips have only four fractional subdivisors at value[15:14]
1083 for subdivisors 0, 0.5, 0.25, 0.125
f15786e4
UB
1084*/
1085static int ftdi_to_clkbits(int baudrate, unsigned int clk, int clk_div, unsigned long *encoded_divisor)
1086{
1087 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
1088 int best_baud = 0;
1089 int divisor, best_divisor;
1090 if (baudrate >= clk/clk_div)
1091 {
1092 *encoded_divisor = 0;
1093 best_baud = clk/clk_div;
1094 }
1095 else if (baudrate >= clk/(clk_div + clk_div/2))
1096 {
1097 *encoded_divisor = 1;
1098 best_baud = clk/(clk_div + clk_div/2);
1099 }
1100 else if (baudrate >= clk/(2*clk_div))
1101 {
1102 *encoded_divisor = 2;
1103 best_baud = clk/(2*clk_div);
1104 }
1105 else
1106 {
1107 /* We divide by 16 to have 3 fractional bits and one bit for rounding */
1108 divisor = clk*16/clk_div / baudrate;
1109 if (divisor & 1) /* Decide if to round up or down*/
1110 best_divisor = divisor /2 +1;
1111 else
1112 best_divisor = divisor/2;
1113 if(best_divisor > 0x20000)
1114 best_divisor = 0x1ffff;
1115 best_baud = clk*8/clk_div/best_divisor;
1116 *encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 0x7] << 14);
1117 }
1118 return best_baud;
1119}
1120/**
1121 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
1122 Function is only used internally
1123 \internal
1124*/
1125static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
1126 unsigned short *value, unsigned short *index)
1127{
1128 int best_baud;
1129 unsigned long encoded_divisor;
1130
1131 if (baudrate <= 0)
1132 {
1133 // Return error
1134 return -1;
1135 }
1136
1137#define H_CLK 120000000
1138#define C_CLK 48000000
1139 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H) || (ftdi->type == TYPE_232H ))
1140 {
1141 if(baudrate*10 > H_CLK /0x3fff)
1142 {
1143 /* On H Devices, use 12 000 000 Baudrate when possible
1144 We have a 14 bit divisor, a 1 bit divisor switch (10 or 16)
1145 three fractional bits and a 120 MHz clock
1146 Assume AN_120 "Sub-integer divisors between 0 and 2 are not allowed" holds for
1147 DIV/10 CLK too, so /1, /1.5 and /2 can be handled the same*/
1148 best_baud = ftdi_to_clkbits(baudrate, H_CLK, 10, &encoded_divisor);
1149 encoded_divisor |= 0x20000; /* switch on CLK/10*/
1150 }
1151 else
1152 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1153 }
1154 else if ((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C) || (ftdi->type == TYPE_R ))
1155 {
1156 best_baud = ftdi_to_clkbits(baudrate, C_CLK, 16, &encoded_divisor);
1157 }
1158 else
22d12cda 1159 {
f15786e4 1160 best_baud = ftdi_to_clkbits_AM(baudrate, &encoded_divisor);
53ad271d
TJ
1161 }
1162 // Split into "value" and "index" values
1163 *value = (unsigned short)(encoded_divisor & 0xFFFF);
e03f60a1 1164 if (ftdi->type == TYPE_2232H ||
f15786e4 1165 ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H )
22d12cda 1166 {
0126d22e
TJ
1167 *index = (unsigned short)(encoded_divisor >> 8);
1168 *index &= 0xFF00;
a9c57c05 1169 *index |= ftdi->index;
0126d22e
TJ
1170 }
1171 else
1172 *index = (unsigned short)(encoded_divisor >> 16);
c3d95b87 1173
53ad271d
TJ
1174 // Return the nearest baud rate
1175 return best_baud;
1176}
1177
1941414d 1178/**
ac6944cc
TJ
1179 * @brief Wrapper function to export ftdi_convert_baudrate() to the unit test
1180 * Do not use, it's only for the unit test framework
1181 **/
1182int convert_baudrate_UT_export(int baudrate, struct ftdi_context *ftdi,
1183 unsigned short *value, unsigned short *index)
1184{
1185 return ftdi_convert_baudrate(baudrate, ftdi, value, index);
1186}
1187
1188/**
9bec2387 1189 Sets the chip baud rate
1941414d
TJ
1190
1191 \param ftdi pointer to ftdi_context
9bec2387 1192 \param baudrate baud rate to set
1941414d
TJ
1193
1194 \retval 0: all fine
1195 \retval -1: invalid baudrate
1196 \retval -2: setting baudrate failed
22a1b5c1 1197 \retval -3: USB device unavailable
a3da1d95 1198*/
a8f46ddc
TJ
1199int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1200{
53ad271d
TJ
1201 unsigned short value, index;
1202 int actual_baudrate;
a3da1d95 1203
22a1b5c1
TJ
1204 if (ftdi == NULL || ftdi->usb_dev == NULL)
1205 ftdi_error_return(-3, "USB device unavailable");
1206
22d12cda
TJ
1207 if (ftdi->bitbang_enabled)
1208 {
a3da1d95
GE
1209 baudrate = baudrate*4;
1210 }
1211
25707904 1212 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
c3d95b87
TJ
1213 if (actual_baudrate <= 0)
1214 ftdi_error_return (-1, "Silly baudrate <= 0.");
a3da1d95 1215
53ad271d
TJ
1216 // Check within tolerance (about 5%)
1217 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1218 || ((actual_baudrate < baudrate)
1219 ? (actual_baudrate * 21 < baudrate * 20)
c3d95b87
TJ
1220 : (baudrate * 21 < actual_baudrate * 20)))
1221 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
545820ce 1222
579b006f
JZ
1223 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1224 SIO_SET_BAUDRATE_REQUEST, value,
1225 index, NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87 1226 ftdi_error_return (-2, "Setting new baudrate failed");
a3da1d95
GE
1227
1228 ftdi->baudrate = baudrate;
1229 return 0;
1230}
1231
1941414d 1232/**
6c32e222
TJ
1233 Set (RS232) line characteristics.
1234 The break type can only be set via ftdi_set_line_property2()
1235 and defaults to "off".
4837f98a 1236
1941414d
TJ
1237 \param ftdi pointer to ftdi_context
1238 \param bits Number of bits
1239 \param sbit Number of stop bits
1240 \param parity Parity mode
1241
1242 \retval 0: all fine
1243 \retval -1: Setting line property failed
2f73e59f
TJ
1244*/
1245int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
d2f10023 1246 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
2f73e59f 1247{
6c32e222
TJ
1248 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1249}
1250
1251/**
1252 Set (RS232) line characteristics
1253
1254 \param ftdi pointer to ftdi_context
1255 \param bits Number of bits
1256 \param sbit Number of stop bits
1257 \param parity Parity mode
1258 \param break_type Break type
1259
1260 \retval 0: all fine
1261 \retval -1: Setting line property failed
22a1b5c1 1262 \retval -2: USB device unavailable
6c32e222
TJ
1263*/
1264int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
22d12cda
TJ
1265 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1266 enum ftdi_break_type break_type)
6c32e222 1267{
2f73e59f
TJ
1268 unsigned short value = bits;
1269
22a1b5c1
TJ
1270 if (ftdi == NULL || ftdi->usb_dev == NULL)
1271 ftdi_error_return(-2, "USB device unavailable");
1272
22d12cda
TJ
1273 switch (parity)
1274 {
1275 case NONE:
1276 value |= (0x00 << 8);
1277 break;
1278 case ODD:
1279 value |= (0x01 << 8);
1280 break;
1281 case EVEN:
1282 value |= (0x02 << 8);
1283 break;
1284 case MARK:
1285 value |= (0x03 << 8);
1286 break;
1287 case SPACE:
1288 value |= (0x04 << 8);
1289 break;
2f73e59f 1290 }
d2f10023 1291
22d12cda
TJ
1292 switch (sbit)
1293 {
1294 case STOP_BIT_1:
1295 value |= (0x00 << 11);
1296 break;
1297 case STOP_BIT_15:
1298 value |= (0x01 << 11);
1299 break;
1300 case STOP_BIT_2:
1301 value |= (0x02 << 11);
1302 break;
2f73e59f 1303 }
d2f10023 1304
22d12cda
TJ
1305 switch (break_type)
1306 {
1307 case BREAK_OFF:
1308 value |= (0x00 << 14);
1309 break;
1310 case BREAK_ON:
1311 value |= (0x01 << 14);
1312 break;
6c32e222
TJ
1313 }
1314
579b006f
JZ
1315 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1316 SIO_SET_DATA_REQUEST, value,
1317 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2f73e59f 1318 ftdi_error_return (-1, "Setting new line property failed");
d2f10023 1319
2f73e59f
TJ
1320 return 0;
1321}
a3da1d95 1322
1941414d
TJ
1323/**
1324 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1325
1326 \param ftdi pointer to ftdi_context
1327 \param buf Buffer with the data
1328 \param size Size of the buffer
1329
22a1b5c1 1330 \retval -666: USB device unavailable
1941414d
TJ
1331 \retval <0: error code from usb_bulk_write()
1332 \retval >0: number of bytes written
1333*/
a8f46ddc
TJ
1334int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1335{
a3da1d95 1336 int offset = 0;
579b006f 1337 int actual_length;
c3d95b87 1338
22a1b5c1
TJ
1339 if (ftdi == NULL || ftdi->usb_dev == NULL)
1340 ftdi_error_return(-666, "USB device unavailable");
1341
22d12cda
TJ
1342 while (offset < size)
1343 {
948f9ada 1344 int write_size = ftdi->writebuffer_chunksize;
a3da1d95
GE
1345
1346 if (offset+write_size > size)
1347 write_size = size-offset;
1348
579b006f
JZ
1349 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1350 ftdi_error_return(-1, "usb bulk write failed");
a3da1d95 1351
579b006f 1352 offset += actual_length;
a3da1d95
GE
1353 }
1354
579b006f 1355 return offset;
a3da1d95
GE
1356}
1357
579b006f 1358static void ftdi_read_data_cb(struct libusb_transfer *transfer)
22d12cda 1359{
579b006f
JZ
1360 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1361 struct ftdi_context *ftdi = tc->ftdi;
1362 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
4c9e3812 1363
b1139150 1364 packet_size = ftdi->max_packet_size;
579b006f
JZ
1365
1366 actual_length = transfer->actual_length;
1367
1368 if (actual_length > 2)
1369 {
1370 // skip FTDI status bytes.
1371 // Maybe stored in the future to enable modem use
1372 num_of_chunks = actual_length / packet_size;
1373 chunk_remains = actual_length % packet_size;
1374 //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);
1375
1376 ftdi->readbuffer_offset += 2;
1377 actual_length -= 2;
1378
1379 if (actual_length > packet_size - 2)
1380 {
1381 for (i = 1; i < num_of_chunks; i++)
56ac0383
TJ
1382 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1383 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1384 packet_size - 2);
579b006f
JZ
1385 if (chunk_remains > 2)
1386 {
1387 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1388 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1389 chunk_remains-2);
1390 actual_length -= 2*num_of_chunks;
1391 }
1392 else
56ac0383 1393 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
579b006f
JZ
1394 }
1395
1396 if (actual_length > 0)
1397 {
1398 // data still fits in buf?
1399 if (tc->offset + actual_length <= tc->size)
1400 {
1401 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1402 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1403 tc->offset += actual_length;
1404
1405 ftdi->readbuffer_offset = 0;
1406 ftdi->readbuffer_remaining = 0;
1407
1408 /* Did we read exactly the right amount of bytes? */
1409 if (tc->offset == tc->size)
1410 {
1411 //printf("read_data exact rem %d offset %d\n",
1412 //ftdi->readbuffer_remaining, offset);
1413 tc->completed = 1;
1414 return;
1415 }
1416 }
1417 else
1418 {
1419 // only copy part of the data or size <= readbuffer_chunksize
1420 int part_size = tc->size - tc->offset;
1421 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1422 tc->offset += part_size;
1423
1424 ftdi->readbuffer_offset += part_size;
1425 ftdi->readbuffer_remaining = actual_length - part_size;
1426
1427 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1428 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1429 tc->completed = 1;
1430 return;
1431 }
1432 }
1433 }
1434 ret = libusb_submit_transfer (transfer);
1435 if (ret < 0)
1436 tc->completed = 1;
1437}
1438
1439
1440static void ftdi_write_data_cb(struct libusb_transfer *transfer)
7cc9950e 1441{
579b006f
JZ
1442 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1443 struct ftdi_context *ftdi = tc->ftdi;
56ac0383 1444
90ef163e 1445 tc->offset += transfer->actual_length;
56ac0383 1446
579b006f 1447 if (tc->offset == tc->size)
22d12cda 1448 {
579b006f 1449 tc->completed = 1;
7cc9950e 1450 }
579b006f
JZ
1451 else
1452 {
1453 int write_size = ftdi->writebuffer_chunksize;
1454 int ret;
7cc9950e 1455
579b006f
JZ
1456 if (tc->offset + write_size > tc->size)
1457 write_size = tc->size - tc->offset;
1458
1459 transfer->length = write_size;
1460 transfer->buffer = tc->buf + tc->offset;
1461 ret = libusb_submit_transfer (transfer);
1462 if (ret < 0)
1463 tc->completed = 1;
1464 }
7cc9950e
GE
1465}
1466
579b006f 1467
84f85aaa 1468/**
579b006f
JZ
1469 Writes data to the chip. Does not wait for completion of the transfer
1470 nor does it make sure that the transfer was successful.
1471
249888c8 1472 Use libusb 1.0 asynchronous API.
84f85aaa
GE
1473
1474 \param ftdi pointer to ftdi_context
579b006f
JZ
1475 \param buf Buffer with the data
1476 \param size Size of the buffer
84f85aaa 1477
579b006f
JZ
1478 \retval NULL: Some error happens when submit transfer
1479 \retval !NULL: Pointer to a ftdi_transfer_control
c201f80f 1480*/
579b006f
JZ
1481
1482struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
7cc9950e 1483{
579b006f 1484 struct ftdi_transfer_control *tc;
5e77e870 1485 struct libusb_transfer *transfer;
579b006f 1486 int write_size, ret;
22d12cda 1487
22a1b5c1 1488 if (ftdi == NULL || ftdi->usb_dev == NULL)
22a1b5c1 1489 return NULL;
22a1b5c1 1490
579b006f 1491 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
5e77e870
TJ
1492 if (!tc)
1493 return NULL;
22d12cda 1494
5e77e870
TJ
1495 transfer = libusb_alloc_transfer(0);
1496 if (!transfer)
1497 {
1498 free(tc);
579b006f 1499 return NULL;
5e77e870 1500 }
22d12cda 1501
579b006f
JZ
1502 tc->ftdi = ftdi;
1503 tc->completed = 0;
1504 tc->buf = buf;
1505 tc->size = size;
1506 tc->offset = 0;
7cc9950e 1507
579b006f 1508 if (size < ftdi->writebuffer_chunksize)
56ac0383 1509 write_size = size;
579b006f 1510 else
56ac0383 1511 write_size = ftdi->writebuffer_chunksize;
22d12cda 1512
90ef163e
YSL
1513 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1514 write_size, ftdi_write_data_cb, tc,
1515 ftdi->usb_write_timeout);
579b006f 1516 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
7cc9950e 1517
579b006f
JZ
1518 ret = libusb_submit_transfer(transfer);
1519 if (ret < 0)
1520 {
1521 libusb_free_transfer(transfer);
5e77e870 1522 free(tc);
579b006f 1523 return NULL;
7cc9950e 1524 }
579b006f
JZ
1525 tc->transfer = transfer;
1526
1527 return tc;
7cc9950e
GE
1528}
1529
1530/**
579b006f
JZ
1531 Reads data from the chip. Does not wait for completion of the transfer
1532 nor does it make sure that the transfer was successful.
1533
249888c8 1534 Use libusb 1.0 asynchronous API.
7cc9950e
GE
1535
1536 \param ftdi pointer to ftdi_context
579b006f
JZ
1537 \param buf Buffer with the data
1538 \param size Size of the buffer
4c9e3812 1539
579b006f
JZ
1540 \retval NULL: Some error happens when submit transfer
1541 \retval !NULL: Pointer to a ftdi_transfer_control
4c9e3812 1542*/
579b006f
JZ
1543
1544struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
4c9e3812 1545{
579b006f
JZ
1546 struct ftdi_transfer_control *tc;
1547 struct libusb_transfer *transfer;
1548 int ret;
22d12cda 1549
22a1b5c1
TJ
1550 if (ftdi == NULL || ftdi->usb_dev == NULL)
1551 return NULL;
1552
579b006f
JZ
1553 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1554 if (!tc)
1555 return NULL;
1556
1557 tc->ftdi = ftdi;
1558 tc->buf = buf;
1559 tc->size = size;
1560
1561 if (size <= ftdi->readbuffer_remaining)
7cc9950e 1562 {
579b006f 1563 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
7cc9950e 1564
579b006f
JZ
1565 // Fix offsets
1566 ftdi->readbuffer_remaining -= size;
1567 ftdi->readbuffer_offset += size;
7cc9950e 1568
579b006f 1569 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
22d12cda 1570
579b006f
JZ
1571 tc->completed = 1;
1572 tc->offset = size;
1573 tc->transfer = NULL;
1574 return tc;
1575 }
4c9e3812 1576
579b006f
JZ
1577 tc->completed = 0;
1578 if (ftdi->readbuffer_remaining != 0)
1579 {
1580 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
22d12cda 1581
579b006f
JZ
1582 tc->offset = ftdi->readbuffer_remaining;
1583 }
1584 else
1585 tc->offset = 0;
22d12cda 1586
579b006f
JZ
1587 transfer = libusb_alloc_transfer(0);
1588 if (!transfer)
1589 {
1590 free (tc);
1591 return NULL;
1592 }
22d12cda 1593
579b006f
JZ
1594 ftdi->readbuffer_remaining = 0;
1595 ftdi->readbuffer_offset = 0;
1596
1597 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);
1598 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1599
1600 ret = libusb_submit_transfer(transfer);
1601 if (ret < 0)
1602 {
1603 libusb_free_transfer(transfer);
1604 free (tc);
1605 return NULL;
22d12cda 1606 }
579b006f
JZ
1607 tc->transfer = transfer;
1608
1609 return tc;
4c9e3812
GE
1610}
1611
1612/**
579b006f 1613 Wait for completion of the transfer.
4c9e3812 1614
249888c8 1615 Use libusb 1.0 asynchronous API.
4c9e3812 1616
579b006f 1617 \param tc pointer to ftdi_transfer_control
4c9e3812 1618
579b006f
JZ
1619 \retval < 0: Some error happens
1620 \retval >= 0: Data size transferred
4c9e3812 1621*/
579b006f
JZ
1622
1623int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
4c9e3812
GE
1624{
1625 int ret;
4c9e3812 1626
579b006f 1627 while (!tc->completed)
22d12cda 1628 {
29b1dfd9 1629 ret = libusb_handle_events(tc->ftdi->usb_ctx);
4c9e3812 1630 if (ret < 0)
579b006f
JZ
1631 {
1632 if (ret == LIBUSB_ERROR_INTERRUPTED)
1633 continue;
1634 libusb_cancel_transfer(tc->transfer);
1635 while (!tc->completed)
29b1dfd9 1636 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
579b006f
JZ
1637 break;
1638 libusb_free_transfer(tc->transfer);
1639 free (tc);
579b006f
JZ
1640 return ret;
1641 }
4c9e3812
GE
1642 }
1643
90ef163e
YSL
1644 ret = tc->offset;
1645 /**
1646 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
ef15fab5 1647 * at ftdi_read_data_submit(). Therefore, we need to check it here.
90ef163e 1648 **/
ef15fab5
TJ
1649 if (tc->transfer)
1650 {
1651 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1652 ret = -1;
1653 libusb_free_transfer(tc->transfer);
90ef163e 1654 }
579b006f
JZ
1655 free(tc);
1656 return ret;
4c9e3812 1657}
579b006f 1658
1941414d
TJ
1659/**
1660 Configure write buffer chunk size.
1661 Default is 4096.
1662
1663 \param ftdi pointer to ftdi_context
1664 \param chunksize Chunk size
a3da1d95 1665
1941414d 1666 \retval 0: all fine
22a1b5c1 1667 \retval -1: ftdi context invalid
1941414d 1668*/
a8f46ddc
TJ
1669int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1670{
22a1b5c1
TJ
1671 if (ftdi == NULL)
1672 ftdi_error_return(-1, "ftdi context invalid");
1673
948f9ada
TJ
1674 ftdi->writebuffer_chunksize = chunksize;
1675 return 0;
1676}
1677
1941414d
TJ
1678/**
1679 Get write buffer chunk size.
1680
1681 \param ftdi pointer to ftdi_context
1682 \param chunksize Pointer to store chunk size in
948f9ada 1683
1941414d 1684 \retval 0: all fine
22a1b5c1 1685 \retval -1: ftdi context invalid
1941414d 1686*/
a8f46ddc
TJ
1687int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1688{
22a1b5c1
TJ
1689 if (ftdi == NULL)
1690 ftdi_error_return(-1, "ftdi context invalid");
1691
948f9ada
TJ
1692 *chunksize = ftdi->writebuffer_chunksize;
1693 return 0;
1694}
cbabb7d3 1695
1941414d
TJ
1696/**
1697 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1698
1699 Automatically strips the two modem status bytes transfered during every read.
948f9ada 1700
1941414d
TJ
1701 \param ftdi pointer to ftdi_context
1702 \param buf Buffer to store data in
1703 \param size Size of the buffer
1704
22a1b5c1 1705 \retval -666: USB device unavailable
579b006f 1706 \retval <0: error code from libusb_bulk_transfer()
d77b0e94 1707 \retval 0: no data was available
1941414d
TJ
1708 \retval >0: number of bytes read
1709
1941414d 1710*/
a8f46ddc
TJ
1711int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1712{
579b006f 1713 int offset = 0, ret, i, num_of_chunks, chunk_remains;
e2f12a4f 1714 int packet_size = ftdi->max_packet_size;
579b006f 1715 int actual_length = 1;
f2f00cb5 1716
22a1b5c1
TJ
1717 if (ftdi == NULL || ftdi->usb_dev == NULL)
1718 ftdi_error_return(-666, "USB device unavailable");
1719
e2f12a4f
TJ
1720 // Packet size sanity check (avoid division by zero)
1721 if (packet_size == 0)
1722 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
d9f0cce7 1723
948f9ada 1724 // everything we want is still in the readbuffer?
22d12cda
TJ
1725 if (size <= ftdi->readbuffer_remaining)
1726 {
d9f0cce7
TJ
1727 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1728
1729 // Fix offsets
1730 ftdi->readbuffer_remaining -= size;
1731 ftdi->readbuffer_offset += size;
1732
545820ce 1733 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
1734
1735 return size;
979a145c 1736 }
948f9ada 1737 // something still in the readbuffer, but not enough to satisfy 'size'?
22d12cda
TJ
1738 if (ftdi->readbuffer_remaining != 0)
1739 {
d9f0cce7 1740 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
979a145c 1741
d9f0cce7
TJ
1742 // Fix offset
1743 offset += ftdi->readbuffer_remaining;
948f9ada 1744 }
948f9ada 1745 // do the actual USB read
579b006f 1746 while (offset < size && actual_length > 0)
22d12cda 1747 {
d9f0cce7
TJ
1748 ftdi->readbuffer_remaining = 0;
1749 ftdi->readbuffer_offset = 0;
98452d97 1750 /* returns how much received */
579b006f 1751 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
c3d95b87
TJ
1752 if (ret < 0)
1753 ftdi_error_return(ret, "usb bulk read failed");
98452d97 1754
579b006f 1755 if (actual_length > 2)
22d12cda 1756 {
d9f0cce7
TJ
1757 // skip FTDI status bytes.
1758 // Maybe stored in the future to enable modem use
579b006f
JZ
1759 num_of_chunks = actual_length / packet_size;
1760 chunk_remains = actual_length % packet_size;
1761 //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 1762
d9f0cce7 1763 ftdi->readbuffer_offset += 2;
579b006f 1764 actual_length -= 2;
1c733d33 1765
579b006f 1766 if (actual_length > packet_size - 2)
22d12cda 1767 {
1c733d33 1768 for (i = 1; i < num_of_chunks; i++)
f2f00cb5
DC
1769 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1770 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1771 packet_size - 2);
22d12cda
TJ
1772 if (chunk_remains > 2)
1773 {
f2f00cb5
DC
1774 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1775 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1c733d33 1776 chunk_remains-2);
579b006f 1777 actual_length -= 2*num_of_chunks;
22d12cda
TJ
1778 }
1779 else
579b006f 1780 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1c733d33 1781 }
22d12cda 1782 }
579b006f 1783 else if (actual_length <= 2)
22d12cda 1784 {
d9f0cce7
TJ
1785 // no more data to read?
1786 return offset;
1787 }
579b006f 1788 if (actual_length > 0)
22d12cda 1789 {
d9f0cce7 1790 // data still fits in buf?
579b006f 1791 if (offset+actual_length <= size)
22d12cda 1792 {
579b006f 1793 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
545820ce 1794 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
579b006f 1795 offset += actual_length;
d9f0cce7 1796
53ad271d 1797 /* Did we read exactly the right amount of bytes? */
d9f0cce7 1798 if (offset == size)
c4446c36
TJ
1799 //printf("read_data exact rem %d offset %d\n",
1800 //ftdi->readbuffer_remaining, offset);
d9f0cce7 1801 return offset;
22d12cda
TJ
1802 }
1803 else
1804 {
d9f0cce7
TJ
1805 // only copy part of the data or size <= readbuffer_chunksize
1806 int part_size = size-offset;
1807 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
98452d97 1808
d9f0cce7 1809 ftdi->readbuffer_offset += part_size;
579b006f 1810 ftdi->readbuffer_remaining = actual_length-part_size;
d9f0cce7
TJ
1811 offset += part_size;
1812
579b006f
JZ
1813 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1814 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
1815
1816 return offset;
1817 }
1818 }
cbabb7d3 1819 }
948f9ada 1820 // never reached
29c4af7f 1821 return -127;
a3da1d95
GE
1822}
1823
1941414d
TJ
1824/**
1825 Configure read buffer chunk size.
1826 Default is 4096.
1827
1828 Automatically reallocates the buffer.
a3da1d95 1829
1941414d
TJ
1830 \param ftdi pointer to ftdi_context
1831 \param chunksize Chunk size
1832
1833 \retval 0: all fine
22a1b5c1 1834 \retval -1: ftdi context invalid
1941414d 1835*/
a8f46ddc
TJ
1836int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1837{
29c4af7f
TJ
1838 unsigned char *new_buf;
1839
22a1b5c1
TJ
1840 if (ftdi == NULL)
1841 ftdi_error_return(-1, "ftdi context invalid");
1842
948f9ada
TJ
1843 // Invalidate all remaining data
1844 ftdi->readbuffer_offset = 0;
1845 ftdi->readbuffer_remaining = 0;
8de6eea4
JZ
1846#ifdef __linux__
1847 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1848 which is defined in libusb-1.0. Otherwise, each USB read request will
2e685a1f 1849 be divided into multiple URBs. This will cause issues on Linux kernel
8de6eea4
JZ
1850 older than 2.6.32. */
1851 if (chunksize > 16384)
1852 chunksize = 16384;
1853#endif
948f9ada 1854
c3d95b87
TJ
1855 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1856 ftdi_error_return(-1, "out of memory for readbuffer");
d9f0cce7 1857
948f9ada
TJ
1858 ftdi->readbuffer = new_buf;
1859 ftdi->readbuffer_chunksize = chunksize;
1860
1861 return 0;
1862}
1863
1941414d
TJ
1864/**
1865 Get read buffer chunk size.
948f9ada 1866
1941414d
TJ
1867 \param ftdi pointer to ftdi_context
1868 \param chunksize Pointer to store chunk size in
1869
1870 \retval 0: all fine
22a1b5c1 1871 \retval -1: FTDI context invalid
1941414d 1872*/
a8f46ddc
TJ
1873int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1874{
22a1b5c1
TJ
1875 if (ftdi == NULL)
1876 ftdi_error_return(-1, "FTDI context invalid");
1877
948f9ada
TJ
1878 *chunksize = ftdi->readbuffer_chunksize;
1879 return 0;
1880}
1881
1882
1941414d
TJ
1883/**
1884 Enable bitbang mode.
948f9ada 1885
fd282db3 1886 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1941414d
TJ
1887
1888 \param ftdi pointer to ftdi_context
1889 \param bitmask Bitmask to configure lines.
1890 HIGH/ON value configures a line as output.
1891
1892 \retval 0: all fine
1893 \retval -1: can't enable bitbang mode
22a1b5c1 1894 \retval -2: USB device unavailable
1941414d 1895*/
a8f46ddc
TJ
1896int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1897{
a3da1d95
GE
1898 unsigned short usb_val;
1899
22a1b5c1
TJ
1900 if (ftdi == NULL || ftdi->usb_dev == NULL)
1901 ftdi_error_return(-2, "USB device unavailable");
1902
d9f0cce7 1903 usb_val = bitmask; // low byte: bitmask
3119537f
TJ
1904 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1905 usb_val |= (ftdi->bitbang_mode << 8);
1906
579b006f
JZ
1907 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1908 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1909 NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87
TJ
1910 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1911
a3da1d95
GE
1912 ftdi->bitbang_enabled = 1;
1913 return 0;
1914}
1915
1941414d
TJ
1916/**
1917 Disable bitbang mode.
a3da1d95 1918
1941414d
TJ
1919 \param ftdi pointer to ftdi_context
1920
1921 \retval 0: all fine
1922 \retval -1: can't disable bitbang mode
22a1b5c1 1923 \retval -2: USB device unavailable
1941414d 1924*/
a8f46ddc
TJ
1925int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1926{
22a1b5c1
TJ
1927 if (ftdi == NULL || ftdi->usb_dev == NULL)
1928 ftdi_error_return(-2, "USB device unavailable");
1929
579b006f 1930 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 1931 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
a3da1d95
GE
1932
1933 ftdi->bitbang_enabled = 0;
1934 return 0;
1935}
1936
1941414d 1937/**
418aaa72 1938 Enable/disable bitbang modes.
a3da1d95 1939
1941414d
TJ
1940 \param ftdi pointer to ftdi_context
1941 \param bitmask Bitmask to configure lines.
1942 HIGH/ON value configures a line as output.
fd282db3 1943 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1941414d
TJ
1944
1945 \retval 0: all fine
1946 \retval -1: can't enable bitbang mode
22a1b5c1 1947 \retval -2: USB device unavailable
1941414d 1948*/
c4446c36
TJ
1949int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1950{
1951 unsigned short usb_val;
1952
22a1b5c1
TJ
1953 if (ftdi == NULL || ftdi->usb_dev == NULL)
1954 ftdi_error_return(-2, "USB device unavailable");
1955
c4446c36
TJ
1956 usb_val = bitmask; // low byte: bitmask
1957 usb_val |= (mode << 8);
579b006f
JZ
1958 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)
1959 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
c4446c36
TJ
1960
1961 ftdi->bitbang_mode = mode;
418aaa72 1962 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
c4446c36
TJ
1963 return 0;
1964}
1965
1941414d 1966/**
418aaa72 1967 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1941414d
TJ
1968
1969 \param ftdi pointer to ftdi_context
1970 \param pins Pointer to store pins into
1971
1972 \retval 0: all fine
1973 \retval -1: read pins failed
22a1b5c1 1974 \retval -2: USB device unavailable
1941414d 1975*/
a8f46ddc
TJ
1976int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1977{
22a1b5c1
TJ
1978 if (ftdi == NULL || ftdi->usb_dev == NULL)
1979 ftdi_error_return(-2, "USB device unavailable");
1980
579b006f 1981 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 1982 ftdi_error_return(-1, "read pins failed");
a3da1d95 1983
a3da1d95
GE
1984 return 0;
1985}
1986
1941414d
TJ
1987/**
1988 Set latency timer
1989
1990 The FTDI chip keeps data in the internal buffer for a specific
1991 amount of time if the buffer is not full yet to decrease
1992 load on the usb bus.
a3da1d95 1993
1941414d
TJ
1994 \param ftdi pointer to ftdi_context
1995 \param latency Value between 1 and 255
1996
1997 \retval 0: all fine
1998 \retval -1: latency out of range
1999 \retval -2: unable to set latency timer
22a1b5c1 2000 \retval -3: USB device unavailable
1941414d 2001*/
a8f46ddc
TJ
2002int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
2003{
a3da1d95
GE
2004 unsigned short usb_val;
2005
c3d95b87
TJ
2006 if (latency < 1)
2007 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
a3da1d95 2008
22a1b5c1
TJ
2009 if (ftdi == NULL || ftdi->usb_dev == NULL)
2010 ftdi_error_return(-3, "USB device unavailable");
2011
d79d2e68 2012 usb_val = latency;
579b006f 2013 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
2014 ftdi_error_return(-2, "unable to set latency timer");
2015
a3da1d95
GE
2016 return 0;
2017}
2018
1941414d
TJ
2019/**
2020 Get latency timer
a3da1d95 2021
1941414d
TJ
2022 \param ftdi pointer to ftdi_context
2023 \param latency Pointer to store latency value in
2024
2025 \retval 0: all fine
2026 \retval -1: unable to get latency timer
22a1b5c1 2027 \retval -2: USB device unavailable
1941414d 2028*/
a8f46ddc
TJ
2029int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
2030{
a3da1d95 2031 unsigned short usb_val;
22a1b5c1
TJ
2032
2033 if (ftdi == NULL || ftdi->usb_dev == NULL)
2034 ftdi_error_return(-2, "USB device unavailable");
2035
579b006f 2036 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 2037 ftdi_error_return(-1, "reading latency timer failed");
a3da1d95
GE
2038
2039 *latency = (unsigned char)usb_val;
2040 return 0;
2041}
2042
1941414d 2043/**
1189b11a
TJ
2044 Poll modem status information
2045
2046 This function allows the retrieve the two status bytes of the device.
2047 The device sends these bytes also as a header for each read access
2048 where they are discarded by ftdi_read_data(). The chip generates
2049 the two stripped status bytes in the absence of data every 40 ms.
2050
2051 Layout of the first byte:
2052 - B0..B3 - must be 0
2053 - B4 Clear to send (CTS)
2054 0 = inactive
2055 1 = active
2056 - B5 Data set ready (DTS)
2057 0 = inactive
2058 1 = active
2059 - B6 Ring indicator (RI)
2060 0 = inactive
2061 1 = active
2062 - B7 Receive line signal detect (RLSD)
2063 0 = inactive
2064 1 = active
2065
2066 Layout of the second byte:
2067 - B0 Data ready (DR)
2068 - B1 Overrun error (OE)
2069 - B2 Parity error (PE)
2070 - B3 Framing error (FE)
2071 - B4 Break interrupt (BI)
2072 - B5 Transmitter holding register (THRE)
2073 - B6 Transmitter empty (TEMT)
2074 - B7 Error in RCVR FIFO
2075
2076 \param ftdi pointer to ftdi_context
2077 \param status Pointer to store status information in. Must be two bytes.
2078
2079 \retval 0: all fine
2080 \retval -1: unable to retrieve status information
22a1b5c1 2081 \retval -2: USB device unavailable
1189b11a
TJ
2082*/
2083int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
2084{
2085 char usb_val[2];
2086
22a1b5c1
TJ
2087 if (ftdi == NULL || ftdi->usb_dev == NULL)
2088 ftdi_error_return(-2, "USB device unavailable");
2089
579b006f 2090 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
2091 ftdi_error_return(-1, "getting modem status failed");
2092
dc09eaa8 2093 *status = (usb_val[1] << 8) | (usb_val[0] & 0xFF);
1189b11a
TJ
2094
2095 return 0;
2096}
2097
a7fb8440
TJ
2098/**
2099 Set flowcontrol for ftdi chip
2100
2101 \param ftdi pointer to ftdi_context
22d12cda
TJ
2102 \param flowctrl flow control to use. should be
2103 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
a7fb8440
TJ
2104
2105 \retval 0: all fine
2106 \retval -1: set flow control failed
22a1b5c1 2107 \retval -2: USB device unavailable
a7fb8440
TJ
2108*/
2109int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
2110{
22a1b5c1
TJ
2111 if (ftdi == NULL || ftdi->usb_dev == NULL)
2112 ftdi_error_return(-2, "USB device unavailable");
2113
579b006f
JZ
2114 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2115 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
2116 NULL, 0, ftdi->usb_write_timeout) < 0)
a7fb8440
TJ
2117 ftdi_error_return(-1, "set flow control failed");
2118
2119 return 0;
2120}
2121
2122/**
2123 Set dtr line
2124
2125 \param ftdi pointer to ftdi_context
2126 \param state state to set line to (1 or 0)
2127
2128 \retval 0: all fine
2129 \retval -1: set dtr failed
22a1b5c1 2130 \retval -2: USB device unavailable
a7fb8440
TJ
2131*/
2132int ftdi_setdtr(struct ftdi_context *ftdi, int state)
2133{
2134 unsigned short usb_val;
2135
22a1b5c1
TJ
2136 if (ftdi == NULL || ftdi->usb_dev == NULL)
2137 ftdi_error_return(-2, "USB device unavailable");
2138
a7fb8440
TJ
2139 if (state)
2140 usb_val = SIO_SET_DTR_HIGH;
2141 else
2142 usb_val = SIO_SET_DTR_LOW;
2143
579b006f
JZ
2144 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2145 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2146 NULL, 0, ftdi->usb_write_timeout) < 0)
a7fb8440
TJ
2147 ftdi_error_return(-1, "set dtr failed");
2148
2149 return 0;
2150}
2151
2152/**
2153 Set rts line
2154
2155 \param ftdi pointer to ftdi_context
2156 \param state state to set line to (1 or 0)
2157
2158 \retval 0: all fine
22a1b5c1
TJ
2159 \retval -1: set rts failed
2160 \retval -2: USB device unavailable
a7fb8440
TJ
2161*/
2162int ftdi_setrts(struct ftdi_context *ftdi, int state)
2163{
2164 unsigned short usb_val;
2165
22a1b5c1
TJ
2166 if (ftdi == NULL || ftdi->usb_dev == NULL)
2167 ftdi_error_return(-2, "USB device unavailable");
2168
a7fb8440
TJ
2169 if (state)
2170 usb_val = SIO_SET_RTS_HIGH;
2171 else
2172 usb_val = SIO_SET_RTS_LOW;
2173
579b006f
JZ
2174 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2175 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2176 NULL, 0, ftdi->usb_write_timeout) < 0)
a7fb8440
TJ
2177 ftdi_error_return(-1, "set of rts failed");
2178
2179 return 0;
2180}
2181
1189b11a 2182/**
22a1b5c1 2183 Set dtr and rts line in one pass
9ecfef2a 2184
22a1b5c1
TJ
2185 \param ftdi pointer to ftdi_context
2186 \param dtr DTR state to set line to (1 or 0)
2187 \param rts RTS state to set line to (1 or 0)
9ecfef2a 2188
22a1b5c1
TJ
2189 \retval 0: all fine
2190 \retval -1: set dtr/rts failed
2191 \retval -2: USB device unavailable
9ecfef2a
TJ
2192 */
2193int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2194{
2195 unsigned short usb_val;
2196
22a1b5c1
TJ
2197 if (ftdi == NULL || ftdi->usb_dev == NULL)
2198 ftdi_error_return(-2, "USB device unavailable");
2199
9ecfef2a 2200 if (dtr)
22d12cda 2201 usb_val = SIO_SET_DTR_HIGH;
9ecfef2a 2202 else
22d12cda 2203 usb_val = SIO_SET_DTR_LOW;
9ecfef2a
TJ
2204
2205 if (rts)
22d12cda 2206 usb_val |= SIO_SET_RTS_HIGH;
9ecfef2a 2207 else
22d12cda 2208 usb_val |= SIO_SET_RTS_LOW;
9ecfef2a 2209
579b006f
JZ
2210 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2211 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2212 NULL, 0, ftdi->usb_write_timeout) < 0)
22d12cda 2213 ftdi_error_return(-1, "set of rts/dtr failed");
9ecfef2a
TJ
2214
2215 return 0;
2216}
2217
2218/**
1189b11a
TJ
2219 Set the special event character
2220
2221 \param ftdi pointer to ftdi_context
2222 \param eventch Event character
2223 \param enable 0 to disable the event character, non-zero otherwise
2224
2225 \retval 0: all fine
2226 \retval -1: unable to set event character
22a1b5c1 2227 \retval -2: USB device unavailable
1189b11a
TJ
2228*/
2229int ftdi_set_event_char(struct ftdi_context *ftdi,
22d12cda 2230 unsigned char eventch, unsigned char enable)
1189b11a
TJ
2231{
2232 unsigned short usb_val;
2233
22a1b5c1
TJ
2234 if (ftdi == NULL || ftdi->usb_dev == NULL)
2235 ftdi_error_return(-2, "USB device unavailable");
2236
1189b11a
TJ
2237 usb_val = eventch;
2238 if (enable)
2239 usb_val |= 1 << 8;
2240
579b006f 2241 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
2242 ftdi_error_return(-1, "setting event character failed");
2243
2244 return 0;
2245}
2246
2247/**
2248 Set error character
2249
2250 \param ftdi pointer to ftdi_context
2251 \param errorch Error character
2252 \param enable 0 to disable the error character, non-zero otherwise
2253
2254 \retval 0: all fine
2255 \retval -1: unable to set error character
22a1b5c1 2256 \retval -2: USB device unavailable
1189b11a
TJ
2257*/
2258int ftdi_set_error_char(struct ftdi_context *ftdi,
22d12cda 2259 unsigned char errorch, unsigned char enable)
1189b11a
TJ
2260{
2261 unsigned short usb_val;
2262
22a1b5c1
TJ
2263 if (ftdi == NULL || ftdi->usb_dev == NULL)
2264 ftdi_error_return(-2, "USB device unavailable");
2265
1189b11a
TJ
2266 usb_val = errorch;
2267 if (enable)
2268 usb_val |= 1 << 8;
2269
579b006f 2270 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
2271 ftdi_error_return(-1, "setting error character failed");
2272
2273 return 0;
2274}
2275
2276/**
44f41f11 2277 Init eeprom with default values for the connected device
a35aa9bd 2278 \param ftdi pointer to ftdi_context
f14f84d3
UB
2279 \param manufacturer String to use as Manufacturer
2280 \param product String to use as Product description
2281 \param serial String to use as Serial number description
4e74064b 2282
f14f84d3
UB
2283 \retval 0: all fine
2284 \retval -1: No struct ftdi_context
2285 \retval -2: No struct ftdi_eeprom
44f41f11 2286 \retval -3: No connected device or device not yet opened
1941414d 2287*/
f14f84d3 2288int ftdi_eeprom_initdefaults(struct ftdi_context *ftdi, char * manufacturer,
56ac0383 2289 char * product, char * serial)
a8f46ddc 2290{
c0a96aed 2291 struct ftdi_eeprom *eeprom;
f505134f 2292
c0a96aed 2293 if (ftdi == NULL)
f14f84d3 2294 ftdi_error_return(-1, "No struct ftdi_context");
c0a96aed
UB
2295
2296 if (ftdi->eeprom == NULL)
56ac0383 2297 ftdi_error_return(-2,"No struct ftdi_eeprom");
22a1b5c1 2298
c0a96aed 2299 eeprom = ftdi->eeprom;
a02587d5 2300 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
c0a96aed 2301
44f41f11
UB
2302 if (ftdi->usb_dev == NULL)
2303 ftdi_error_return(-3, "No connected device or device not yet opened");
2304
f396dbad 2305 eeprom->vendor_id = 0x0403;
d4b5af27 2306 eeprom->use_serial = 1;
56ac0383
TJ
2307 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM) ||
2308 (ftdi->type == TYPE_R))
a02587d5 2309 eeprom->product_id = 0x6001;
c7e4c09e
UB
2310 else if (ftdi->type == TYPE_4232H)
2311 eeprom->product_id = 0x6011;
2312 else if (ftdi->type == TYPE_232H)
2313 eeprom->product_id = 0x6014;
a02587d5
UB
2314 else
2315 eeprom->product_id = 0x6010;
b1859923
UB
2316 if (ftdi->type == TYPE_AM)
2317 eeprom->usb_version = 0x0101;
2318 else
2319 eeprom->usb_version = 0x0200;
a886436a 2320 eeprom->max_power = 100;
d9f0cce7 2321
74e8e79d
UB
2322 if (eeprom->manufacturer)
2323 free (eeprom->manufacturer);
b8aa7b35 2324 eeprom->manufacturer = NULL;
74e8e79d
UB
2325 if (manufacturer)
2326 {
2327 eeprom->manufacturer = malloc(strlen(manufacturer)+1);
2328 if (eeprom->manufacturer)
2329 strcpy(eeprom->manufacturer, manufacturer);
2330 }
2331
2332 if (eeprom->product)
2333 free (eeprom->product);
b8aa7b35 2334 eeprom->product = NULL;
10771971 2335 if(product)
74e8e79d
UB
2336 {
2337 eeprom->product = malloc(strlen(product)+1);
2338 if (eeprom->product)
2339 strcpy(eeprom->product, product);
2340 }
6a6fcd89
UB
2341 else
2342 {
2343 const char* default_product;
2344 switch(ftdi->type)
2345 {
2346 case TYPE_AM: default_product = "AM"; break;
2347 case TYPE_BM: default_product = "BM"; break;
2348 case TYPE_2232C: default_product = "Dual RS232"; break;
2349 case TYPE_R: default_product = "FT232R USB UART"; break;
2350 case TYPE_2232H: default_product = "Dual RS232-HS"; break;
2351 case TYPE_4232H: default_product = "FT4232H"; break;
2352 case TYPE_232H: default_product = "Single-RS232-HS"; break;
2353 default:
2354 ftdi_error_return(-3, "Unknown chip type");
2355 }
2356 eeprom->product = malloc(strlen(default_product) +1);
2357 if (eeprom->product)
2358 strcpy(eeprom->product, default_product);
2359 }
74e8e79d
UB
2360
2361 if (eeprom->serial)
2362 free (eeprom->serial);
b8aa7b35 2363 eeprom->serial = NULL;
74e8e79d
UB
2364 if (serial)
2365 {
2366 eeprom->serial = malloc(strlen(serial)+1);
2367 if (eeprom->serial)
2368 strcpy(eeprom->serial, serial);
2369 }
2370
c201f80f 2371
56ac0383 2372 if (ftdi->type == TYPE_R)
a4980043 2373 {
a886436a 2374 eeprom->max_power = 90;
a02587d5 2375 eeprom->size = 0x80;
a4980043
UB
2376 eeprom->cbus_function[0] = CBUS_TXLED;
2377 eeprom->cbus_function[1] = CBUS_RXLED;
2378 eeprom->cbus_function[2] = CBUS_TXDEN;
2379 eeprom->cbus_function[3] = CBUS_PWREN;
2380 eeprom->cbus_function[4] = CBUS_SLEEP;
2381 }
a02587d5 2382 else
263d3ba0
UB
2383 {
2384 if(ftdi->type == TYPE_232H)
2385 {
2386 int i;
2387 for (i=0; i<10; i++)
2388 eeprom->cbus_function[i] = CBUSH_TRISTATE;
2389 }
a02587d5 2390 eeprom->size = -1;
263d3ba0 2391 }
44f41f11 2392 eeprom->initialized_for_connected_device = 1;
f14f84d3 2393 return 0;
b8aa7b35 2394}
263d3ba0
UB
2395/*FTD2XX doesn't check for values not fitting in the ACBUS Signal oprtions*/
2396void set_ft232h_cbus(struct ftdi_eeprom *eeprom, unsigned char * output)
2397{
2398 int i;
2399 for(i=0; i<5;i++)
2400 {
2401 int mode_low, mode_high;
2402 if (eeprom->cbus_function[2*i]> CBUSH_CLK7_5)
2403 mode_low = CBUSH_TRISTATE;
2404 else
2405 mode_low = eeprom->cbus_function[2*i];
2406 if (eeprom->cbus_function[2*i+1]> CBUSH_CLK7_5)
2407 mode_high = CBUSH_TRISTATE;
2408 else
2409 mode_high = eeprom->cbus_function[2*i];
b8aa7b35 2410
263d3ba0
UB
2411 output[0x18+i] = mode_high <<4 | mode_low;
2412 }
2413}
c8f69686
UB
2414/* Return the bits for the encoded EEPROM Structure of a requested Mode
2415 *
2416 */
2417static unsigned char type2bit(unsigned char type, enum ftdi_chip_type chip)
2418{
2419 switch (chip)
2420 {
2421 case TYPE_2232H:
2422 case TYPE_2232C:
2423 {
2424 switch (type)
2425 {
2426 case CHANNEL_IS_UART: return 0;
2427 case CHANNEL_IS_FIFO: return 0x01;
2428 case CHANNEL_IS_OPTO: return 0x02;
2429 case CHANNEL_IS_CPU : return 0x04;
2430 default: return 0;
2431 }
2432 }
2433 case TYPE_232H:
2434 {
2435 switch (type)
2436 {
2437 case CHANNEL_IS_UART : return 0;
2438 case CHANNEL_IS_FIFO : return 0x01;
2439 case CHANNEL_IS_OPTO : return 0x02;
2440 case CHANNEL_IS_CPU : return 0x04;
2441 case CHANNEL_IS_FT1284 : return 0x08;
2442 default: return 0;
2443 }
2444 }
2445 default: return 0;
2446 }
2447 return 0;
2448}
2449
1941414d 2450/**
a35aa9bd 2451 Build binary buffer from ftdi_eeprom structure.
22a1b5c1 2452 Output is suitable for ftdi_write_eeprom().
b8aa7b35 2453
a35aa9bd 2454 \param ftdi pointer to ftdi_context
1941414d 2455
516ebfb1 2456 \retval >=0: size of eeprom user area in bytes
22a1b5c1 2457 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2c1e2bde
TJ
2458 \retval -2: Invalid eeprom or ftdi pointer
2459 \retval -3: Invalid cbus function setting (FIXME: Not in the code?)
2460 \retval -4: Chip doesn't support invert (FIXME: Not in the code?)
2461 \retval -5: Chip doesn't support high current drive (FIXME: Not in the code?)
2b9a3c82 2462 \retval -6: No connected EEPROM or EEPROM Type unknown
b8aa7b35 2463*/
a35aa9bd 2464int ftdi_eeprom_build(struct ftdi_context *ftdi)
a8f46ddc 2465{
e2bbd9af 2466 unsigned char i, j, eeprom_size_mask;
b8aa7b35
TJ
2467 unsigned short checksum, value;
2468 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
516ebfb1 2469 int user_area_size;
c0a96aed 2470 struct ftdi_eeprom *eeprom;
a35aa9bd 2471 unsigned char * output;
b8aa7b35 2472
c0a96aed 2473 if (ftdi == NULL)
cc9c9d58 2474 ftdi_error_return(-2,"No context");
c0a96aed 2475 if (ftdi->eeprom == NULL)
cc9c9d58 2476 ftdi_error_return(-2,"No eeprom structure");
c0a96aed
UB
2477
2478 eeprom= ftdi->eeprom;
a35aa9bd 2479 output = eeprom->buf;
22a1b5c1 2480
56ac0383 2481 if (eeprom->chip == -1)
2c1e2bde 2482 ftdi_error_return(-6,"No connected EEPROM or EEPROM type unknown");
2b9a3c82 2483
f75bf139
UB
2484 if ((eeprom->chip == 0x56) || (eeprom->chip == 0x66))
2485 eeprom->size = 0x100;
2486 else
2487 eeprom->size = 0x80;
2488
b8aa7b35 2489 if (eeprom->manufacturer != NULL)
d9f0cce7 2490 manufacturer_size = strlen(eeprom->manufacturer);
b8aa7b35 2491 if (eeprom->product != NULL)
d9f0cce7 2492 product_size = strlen(eeprom->product);
b8aa7b35 2493 if (eeprom->serial != NULL)
d9f0cce7 2494 serial_size = strlen(eeprom->serial);
b8aa7b35 2495
814710ba
TJ
2496 // eeprom size check
2497 switch (ftdi->type)
2498 {
2499 case TYPE_AM:
2500 case TYPE_BM:
2501 user_area_size = 96; // base size for strings (total of 48 characters)
2502 break;
2503 case TYPE_2232C:
56ac0383
TJ
2504 user_area_size = 90; // two extra config bytes and 4 bytes PnP stuff
2505 break;
814710ba 2506 case TYPE_R:
56ac0383
TJ
2507 user_area_size = 88; // four extra config bytes + 4 bytes PnP stuff
2508 break;
814710ba
TJ
2509 case TYPE_2232H: // six extra config bytes + 4 bytes PnP stuff
2510 case TYPE_4232H:
56ac0383 2511 user_area_size = 86;
118c4561 2512 break;
c1c3d564
UB
2513 case TYPE_232H:
2514 user_area_size = 80;
2515 break;
2c1e2bde
TJ
2516 default:
2517 user_area_size = 0;
56ac0383 2518 break;
665cda04
UB
2519 }
2520 user_area_size -= (manufacturer_size + product_size + serial_size) * 2;
814710ba 2521
516ebfb1
TJ
2522 if (user_area_size < 0)
2523 ftdi_error_return(-1,"eeprom size exceeded");
b8aa7b35
TJ
2524
2525 // empty eeprom
a35aa9bd 2526 memset (ftdi->eeprom->buf, 0, FTDI_MAX_EEPROM_SIZE);
b8aa7b35 2527
93738c79
UB
2528 // Bytes and Bits set for all Types
2529
b8aa7b35
TJ
2530 // Addr 02: Vendor ID
2531 output[0x02] = eeprom->vendor_id;
2532 output[0x03] = eeprom->vendor_id >> 8;
2533
2534 // Addr 04: Product ID
2535 output[0x04] = eeprom->product_id;
2536 output[0x05] = eeprom->product_id >> 8;
2537
2538 // Addr 06: Device release number (0400h for BM features)
2539 output[0x06] = 0x00;
814710ba
TJ
2540 switch (ftdi->type)
2541 {
f505134f
HK
2542 case TYPE_AM:
2543 output[0x07] = 0x02;
2544 break;
2545 case TYPE_BM:
2546 output[0x07] = 0x04;
2547 break;
2548 case TYPE_2232C:
2549 output[0x07] = 0x05;
2550 break;
2551 case TYPE_R:
2552 output[0x07] = 0x06;
2553 break;
56ac0383 2554 case TYPE_2232H:
6123f7ab
UB
2555 output[0x07] = 0x07;
2556 break;
56ac0383 2557 case TYPE_4232H:
6123f7ab
UB
2558 output[0x07] = 0x08;
2559 break;
c7e4c09e
UB
2560 case TYPE_232H:
2561 output[0x07] = 0x09;
2562 break;
f505134f
HK
2563 default:
2564 output[0x07] = 0x00;
2565 }
b8aa7b35
TJ
2566
2567 // Addr 08: Config descriptor
8fae3e8e
TJ
2568 // Bit 7: always 1
2569 // Bit 6: 1 if this device is self powered, 0 if bus powered
2570 // Bit 5: 1 if this device uses remote wakeup
37186e34 2571 // Bit 4-0: reserved - 0
5a1dcd55 2572 j = 0x80;
b8aa7b35 2573 if (eeprom->self_powered == 1)
5a1dcd55 2574 j |= 0x40;
b8aa7b35 2575 if (eeprom->remote_wakeup == 1)
5a1dcd55 2576 j |= 0x20;
b8aa7b35
TJ
2577 output[0x08] = j;
2578
2579 // Addr 09: Max power consumption: max power = value * 2 mA
bb5ec68a 2580 output[0x09] = eeprom->max_power>>1;
d9f0cce7 2581
56ac0383 2582 if (ftdi->type != TYPE_AM)
93738c79
UB
2583 {
2584 // Addr 0A: Chip configuration
2585 // Bit 7: 0 - reserved
2586 // Bit 6: 0 - reserved
2587 // Bit 5: 0 - reserved
56ac0383 2588 // Bit 4: 1 - Change USB version
93738c79
UB
2589 // Bit 3: 1 - Use the serial number string
2590 // Bit 2: 1 - Enable suspend pull downs for lower power
2591 // Bit 1: 1 - Out EndPoint is Isochronous
2592 // Bit 0: 1 - In EndPoint is Isochronous
2593 //
2594 j = 0;
2595 if (eeprom->in_is_isochronous == 1)
2596 j = j | 1;
2597 if (eeprom->out_is_isochronous == 1)
2598 j = j | 2;
2599 output[0x0A] = j;
2600 }
f505134f 2601
b8aa7b35 2602 // Dynamic content
93738c79
UB
2603 // Strings start at 0x94 (TYPE_AM, TYPE_BM)
2604 // 0x96 (TYPE_2232C), 0x98 (TYPE_R) and 0x9a (TYPE_x232H)
c7e4c09e 2605 // 0xa0 (TYPE_232H)
93738c79 2606 i = 0;
56ac0383
TJ
2607 switch (ftdi->type)
2608 {
c7e4c09e
UB
2609 case TYPE_232H:
2610 i += 2;
56ac0383
TJ
2611 case TYPE_2232H:
2612 case TYPE_4232H:
2613 i += 2;
2614 case TYPE_R:
2615 i += 2;
2616 case TYPE_2232C:
2617 i += 2;
2618 case TYPE_AM:
2619 case TYPE_BM:
2620 i += 0x94;
f505134f 2621 }
93738c79 2622 /* Wrap around 0x80 for 128 byte EEPROMS (Internale and 93x46) */
e2bbd9af 2623 eeprom_size_mask = eeprom->size -1;
c201f80f 2624
93738c79
UB
2625 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2626 // Addr 0F: Length of manufacturer string
22d12cda 2627 // Output manufacturer
93738c79 2628 output[0x0E] = i; // calculate offset
e2bbd9af
TJ
2629 output[i & eeprom_size_mask] = manufacturer_size*2 + 2, i++;
2630 output[i & eeprom_size_mask] = 0x03, i++; // type: string
22d12cda
TJ
2631 for (j = 0; j < manufacturer_size; j++)
2632 {
e2bbd9af
TJ
2633 output[i & eeprom_size_mask] = eeprom->manufacturer[j], i++;
2634 output[i & eeprom_size_mask] = 0x00, i++;
b8aa7b35 2635 }
93738c79 2636 output[0x0F] = manufacturer_size*2 + 2;
b8aa7b35 2637
93738c79
UB
2638 // Addr 10: Offset of the product string + 0x80, calculated later
2639 // Addr 11: Length of product string
c201f80f 2640 output[0x10] = i | 0x80; // calculate offset
e2bbd9af
TJ
2641 output[i & eeprom_size_mask] = product_size*2 + 2, i++;
2642 output[i & eeprom_size_mask] = 0x03, i++;
22d12cda
TJ
2643 for (j = 0; j < product_size; j++)
2644 {
e2bbd9af
TJ
2645 output[i & eeprom_size_mask] = eeprom->product[j], i++;
2646 output[i & eeprom_size_mask] = 0x00, i++;
b8aa7b35 2647 }
93738c79 2648 output[0x11] = product_size*2 + 2;
37186e34 2649
93738c79
UB
2650 // Addr 12: Offset of the serial string + 0x80, calculated later
2651 // Addr 13: Length of serial string
c201f80f 2652 output[0x12] = i | 0x80; // calculate offset
e2bbd9af
TJ
2653 output[i & eeprom_size_mask] = serial_size*2 + 2, i++;
2654 output[i & eeprom_size_mask] = 0x03, i++;
22d12cda
TJ
2655 for (j = 0; j < serial_size; j++)
2656 {
e2bbd9af
TJ
2657 output[i & eeprom_size_mask] = eeprom->serial[j], i++;
2658 output[i & eeprom_size_mask] = 0x00, i++;
b8aa7b35 2659 }
c2700d6d
TJ
2660
2661 // Legacy port name and PnP fields for FT2232 and newer chips
2662 if (ftdi->type > TYPE_BM)
2663 {
2664 output[i & eeprom_size_mask] = 0x02; /* as seen when written with FTD2XX */
2665 i++;
2666 output[i & eeprom_size_mask] = 0x03; /* as seen when written with FTD2XX */
2667 i++;
2668 output[i & eeprom_size_mask] = eeprom->is_not_pnp; /* as seen when written with FTD2XX */
2669 i++;
2670 }
802a949e 2671
93738c79 2672 output[0x13] = serial_size*2 + 2;
b8aa7b35 2673
56ac0383 2674 if (ftdi->type > TYPE_AM) /* use_serial not used in AM devices */
bf2f6ef7 2675 {
d4b5af27 2676 if (eeprom->use_serial)
bf2f6ef7
UB
2677 output[0x0A] |= USE_SERIAL_NUM;
2678 else
2679 output[0x0A] &= ~USE_SERIAL_NUM;
2680 }
3802140c
UB
2681
2682 /* Bytes and Bits specific to (some) types
2683 Write linear, as this allows easier fixing*/
56ac0383
TJ
2684 switch (ftdi->type)
2685 {
2686 case TYPE_AM:
2687 break;
2688 case TYPE_BM:
2689 output[0x0C] = eeprom->usb_version & 0xff;
2690 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2691 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2692 output[0x0A] |= USE_USB_VERSION_BIT;
2693 else
2694 output[0x0A] &= ~USE_USB_VERSION_BIT;
caec1294 2695
56ac0383
TJ
2696 break;
2697 case TYPE_2232C:
3802140c 2698
c8f69686 2699 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232C);
56ac0383
TJ
2700 if ( eeprom->channel_a_driver == DRIVER_VCP)
2701 output[0x00] |= DRIVER_VCP;
2702 else
2703 output[0x00] &= ~DRIVER_VCP;
4e74064b 2704
56ac0383
TJ
2705 if ( eeprom->high_current_a == HIGH_CURRENT_DRIVE)
2706 output[0x00] |= HIGH_CURRENT_DRIVE;
2707 else
2708 output[0x00] &= ~HIGH_CURRENT_DRIVE;
3802140c 2709
c8f69686 2710 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232C);
56ac0383
TJ
2711 if ( eeprom->channel_b_driver == DRIVER_VCP)
2712 output[0x01] |= DRIVER_VCP;
2713 else
2714 output[0x01] &= ~DRIVER_VCP;
4e74064b 2715
56ac0383
TJ
2716 if ( eeprom->high_current_b == HIGH_CURRENT_DRIVE)
2717 output[0x01] |= HIGH_CURRENT_DRIVE;
2718 else
2719 output[0x01] &= ~HIGH_CURRENT_DRIVE;
3802140c 2720
56ac0383
TJ
2721 if (eeprom->in_is_isochronous == 1)
2722 output[0x0A] |= 0x1;
2723 else
2724 output[0x0A] &= ~0x1;
2725 if (eeprom->out_is_isochronous == 1)
2726 output[0x0A] |= 0x2;
2727 else
2728 output[0x0A] &= ~0x2;
2729 if (eeprom->suspend_pull_downs == 1)
2730 output[0x0A] |= 0x4;
2731 else
2732 output[0x0A] &= ~0x4;
2733 if (eeprom->use_usb_version == USE_USB_VERSION_BIT)
2734 output[0x0A] |= USE_USB_VERSION_BIT;
2735 else
2736 output[0x0A] &= ~USE_USB_VERSION_BIT;
4e74064b 2737
56ac0383
TJ
2738 output[0x0C] = eeprom->usb_version & 0xff;
2739 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
2740 output[0x14] = eeprom->chip;
2741 break;
2742 case TYPE_R:
2743 if (eeprom->high_current == HIGH_CURRENT_DRIVE_R)
2744 output[0x00] |= HIGH_CURRENT_DRIVE_R;
2745 output[0x01] = 0x40; /* Hard coded Endpoint Size*/
4e74064b 2746
56ac0383
TJ
2747 if (eeprom->suspend_pull_downs == 1)
2748 output[0x0A] |= 0x4;
2749 else
2750 output[0x0A] &= ~0x4;
2751 output[0x0B] = eeprom->invert;
2752 output[0x0C] = eeprom->usb_version & 0xff;
2753 output[0x0D] = (eeprom->usb_version>>8) & 0xff;
4e74064b 2754
56ac0383
TJ
2755 if (eeprom->cbus_function[0] > CBUS_BB)
2756 output[0x14] = CBUS_TXLED;
2757 else
2758 output[0x14] = eeprom->cbus_function[0];
4e74064b 2759
56ac0383
TJ
2760 if (eeprom->cbus_function[1] > CBUS_BB)
2761 output[0x14] |= CBUS_RXLED<<4;
2762 else
2763 output[0x14] |= eeprom->cbus_function[1]<<4;
4e74064b 2764
56ac0383
TJ
2765 if (eeprom->cbus_function[2] > CBUS_BB)
2766 output[0x15] = CBUS_TXDEN;
2767 else
2768 output[0x15] = eeprom->cbus_function[2];
4e74064b 2769
56ac0383
TJ
2770 if (eeprom->cbus_function[3] > CBUS_BB)
2771 output[0x15] |= CBUS_PWREN<<4;
2772 else
2773 output[0x15] |= eeprom->cbus_function[3]<<4;
4e74064b 2774
56ac0383
TJ
2775 if (eeprom->cbus_function[4] > CBUS_CLK6)
2776 output[0x16] = CBUS_SLEEP;
2777 else
2778 output[0x16] = eeprom->cbus_function[4];
2779 break;
2780 case TYPE_2232H:
c8f69686 2781 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_2232H);
56ac0383
TJ
2782 if ( eeprom->channel_a_driver == DRIVER_VCP)
2783 output[0x00] |= DRIVER_VCP;
2784 else
2785 output[0x00] &= ~DRIVER_VCP;
6e6a1c3f 2786
c8f69686 2787 output[0x01] = type2bit(eeprom->channel_b_type, TYPE_2232H);
56ac0383
TJ
2788 if ( eeprom->channel_b_driver == DRIVER_VCP)
2789 output[0x01] |= DRIVER_VCP;
2790 else
2791 output[0x01] &= ~DRIVER_VCP;
2792 if (eeprom->suspend_dbus7 == SUSPEND_DBUS7_BIT)
2793 output[0x01] |= SUSPEND_DBUS7_BIT;
2794 else
2795 output[0x01] &= ~SUSPEND_DBUS7_BIT;
2796
2797 if (eeprom->suspend_pull_downs == 1)
2798 output[0x0A] |= 0x4;
2799 else
2800 output[0x0A] &= ~0x4;
2801
2802 if (eeprom->group0_drive > DRIVE_16MA)
2803 output[0x0c] |= DRIVE_16MA;
2804 else
2805 output[0x0c] |= eeprom->group0_drive;
2806 if (eeprom->group0_schmitt == IS_SCHMITT)
2807 output[0x0c] |= IS_SCHMITT;
2808 if (eeprom->group0_slew == SLOW_SLEW)
2809 output[0x0c] |= SLOW_SLEW;
2810
2811 if (eeprom->group1_drive > DRIVE_16MA)
2812 output[0x0c] |= DRIVE_16MA<<4;
2813 else
2814 output[0x0c] |= eeprom->group1_drive<<4;
2815 if (eeprom->group1_schmitt == IS_SCHMITT)
2816 output[0x0c] |= IS_SCHMITT<<4;
2817 if (eeprom->group1_slew == SLOW_SLEW)
2818 output[0x0c] |= SLOW_SLEW<<4;
2819
2820 if (eeprom->group2_drive > DRIVE_16MA)
2821 output[0x0d] |= DRIVE_16MA;
2822 else
2823 output[0x0d] |= eeprom->group2_drive;
2824 if (eeprom->group2_schmitt == IS_SCHMITT)
2825 output[0x0d] |= IS_SCHMITT;
2826 if (eeprom->group2_slew == SLOW_SLEW)
2827 output[0x0d] |= SLOW_SLEW;
2828
2829 if (eeprom->group3_drive > DRIVE_16MA)
2830 output[0x0d] |= DRIVE_16MA<<4;
2831 else
2832 output[0x0d] |= eeprom->group3_drive<<4;
2833 if (eeprom->group3_schmitt == IS_SCHMITT)
2834 output[0x0d] |= IS_SCHMITT<<4;
2835 if (eeprom->group3_slew == SLOW_SLEW)
2836 output[0x0d] |= SLOW_SLEW<<4;
3802140c 2837
56ac0383 2838 output[0x18] = eeprom->chip;
3802140c 2839
56ac0383
TJ
2840 break;
2841 case TYPE_4232H:
c7e4c09e 2842 output[0x18] = eeprom->chip;
56ac0383 2843 fprintf(stderr,"FIXME: Build FT4232H specific EEPROM settings\n");
c7e4c09e
UB
2844 break;
2845 case TYPE_232H:
c8f69686 2846 output[0x00] = type2bit(eeprom->channel_a_type, TYPE_232H);
ac4a82a5
UB
2847 if ( eeprom->channel_a_driver == DRIVER_VCP)
2848 output[0x00] |= DRIVER_VCPH;
2849 else
2850 output[0x00] &= ~DRIVER_VCPH;
837a71d6
UB
2851 if (eeprom->powersave)
2852 output[0x01] |= POWER_SAVE_DISABLE_H;
2853 else
2854 output[0x01] &= ~POWER_SAVE_DISABLE_H;
18199b76
UB
2855 if (eeprom->clock_polarity)
2856 output[0x01] |= FT1284_CLK_IDLE_STATE;
2857 else
2858 output[0x01] &= ~FT1284_CLK_IDLE_STATE;
2859 if (eeprom->data_order)
2860 output[0x01] |= FT1284_DATA_LSB;
2861 else
2862 output[0x01] &= ~FT1284_DATA_LSB;
2863 if (eeprom->flow_control)
2864 output[0x01] |= FT1284_FLOW_CONTROL;
2865 else
2866 output[0x01] &= ~FT1284_FLOW_CONTROL;
91d7a201
UB
2867 if (eeprom->group0_drive > DRIVE_16MA)
2868 output[0x0c] |= DRIVE_16MA;
2869 else
2870 output[0x0c] |= eeprom->group0_drive;
2871 if (eeprom->group0_schmitt == IS_SCHMITT)
2872 output[0x0c] |= IS_SCHMITT;
2873 if (eeprom->group0_slew == SLOW_SLEW)
2874 output[0x0c] |= SLOW_SLEW;
2875
2876 if (eeprom->group1_drive > DRIVE_16MA)
2877 output[0x0d] |= DRIVE_16MA;
2878 else
2879 output[0x0d] |= eeprom->group1_drive;
2880 if (eeprom->group1_schmitt == IS_SCHMITT)
2881 output[0x0d] |= IS_SCHMITT;
2882 if (eeprom->group1_slew == SLOW_SLEW)
2883 output[0x0d] |= SLOW_SLEW;
2884
263d3ba0
UB
2885 set_ft232h_cbus(eeprom, output);
2886
c7e4c09e
UB
2887 output[0x1e] = eeprom->chip;
2888 fprintf(stderr,"FIXME: Build FT232H specific EEPROM settings\n");
2889 break;
2890
3802140c
UB
2891 }
2892
cbf65673 2893 // calculate checksum
b8aa7b35 2894 checksum = 0xAAAA;
d9f0cce7 2895
22d12cda
TJ
2896 for (i = 0; i < eeprom->size/2-1; i++)
2897 {
d9f0cce7
TJ
2898 value = output[i*2];
2899 value += output[(i*2)+1] << 8;
b8aa7b35 2900
d9f0cce7
TJ
2901 checksum = value^checksum;
2902 checksum = (checksum << 1) | (checksum >> 15);
b8aa7b35
TJ
2903 }
2904
c201f80f
TJ
2905 output[eeprom->size-2] = checksum;
2906 output[eeprom->size-1] = checksum >> 8;
b8aa7b35 2907
516ebfb1 2908 return user_area_size;
b8aa7b35 2909}
c8f69686
UB
2910/* Decode the encoded EEPROM field for the FTDI Mode into a value for the abstracted
2911 * EEPROM structure
2912 *
2913 * FTD2XX doesn't allow to set multiple bits in the interface mode bitfield, and so do we
2914 */
2915static unsigned char bit2type(unsigned char bits)
0fc2170c
UB
2916{
2917 switch (bits)
2918 {
c8f69686
UB
2919 case 0: return CHANNEL_IS_UART;
2920 case 1: return CHANNEL_IS_FIFO;
2921 case 2: return CHANNEL_IS_OPTO;
2922 case 4: return CHANNEL_IS_CPU;
2923 case 8: return CHANNEL_IS_FT1284;
0fc2170c
UB
2924 default:
2925 fprintf(stderr," Unexpected value %d for Hardware Interface type\n",
2926 bits);
2927 }
2928 return 0;
2929}
4af1d1bb
MK
2930/**
2931 Decode binary EEPROM image into an ftdi_eeprom structure.
2932
a35aa9bd
UB
2933 \param ftdi pointer to ftdi_context
2934 \param verbose Decode EEPROM on stdout
56ac0383 2935
4af1d1bb
MK
2936 \retval 0: all fine
2937 \retval -1: something went wrong
2938
2939 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2940 FIXME: Strings are malloc'ed here and should be freed somewhere
2941*/
a35aa9bd 2942int ftdi_eeprom_decode(struct ftdi_context *ftdi, int verbose)
b56d5a64
MK
2943{
2944 unsigned char i, j;
2945 unsigned short checksum, eeprom_checksum, value;
2946 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
f2cd9fd5 2947 int eeprom_size;
c0a96aed 2948 struct ftdi_eeprom *eeprom;
a35aa9bd 2949 unsigned char *buf = ftdi->eeprom->buf;
38801bf8 2950 int release;
22a1b5c1 2951
c0a96aed 2952 if (ftdi == NULL)
cc9c9d58 2953 ftdi_error_return(-1,"No context");
c0a96aed 2954 if (ftdi->eeprom == NULL)
6cd4f922 2955 ftdi_error_return(-1,"No eeprom structure");
56ac0383 2956
c0a96aed 2957 eeprom = ftdi->eeprom;
a35aa9bd 2958 eeprom_size = eeprom->size;
b56d5a64 2959
b56d5a64
MK
2960 // Addr 02: Vendor ID
2961 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2962
2963 // Addr 04: Product ID
2964 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
22d12cda 2965
38801bf8 2966 release = buf[0x06] + (buf[0x07]<<8);
b56d5a64
MK
2967
2968 // Addr 08: Config descriptor
2969 // Bit 7: always 1
2970 // Bit 6: 1 if this device is self powered, 0 if bus powered
2971 // Bit 5: 1 if this device uses remote wakeup
f6ef2983 2972 eeprom->self_powered = buf[0x08] & 0x40;
814710ba 2973 eeprom->remote_wakeup = buf[0x08] & 0x20;
b56d5a64
MK
2974
2975 // Addr 09: Max power consumption: max power = value * 2 mA
2976 eeprom->max_power = buf[0x09];
2977
2978 // Addr 0A: Chip configuration
2979 // Bit 7: 0 - reserved
2980 // Bit 6: 0 - reserved
2981 // Bit 5: 0 - reserved
caec1294 2982 // Bit 4: 1 - Change USB version on BM and 2232C
b56d5a64
MK
2983 // Bit 3: 1 - Use the serial number string
2984 // Bit 2: 1 - Enable suspend pull downs for lower power
2985 // Bit 1: 1 - Out EndPoint is Isochronous
2986 // Bit 0: 1 - In EndPoint is Isochronous
2987 //
8d3fe5c9
UB
2988 eeprom->in_is_isochronous = buf[0x0A]&0x01;
2989 eeprom->out_is_isochronous = buf[0x0A]&0x02;
2990 eeprom->suspend_pull_downs = buf[0x0A]&0x04;
d4b5af27 2991 eeprom->use_serial = (buf[0x0A] & USE_SERIAL_NUM)?1:0;
caec1294 2992 eeprom->use_usb_version = buf[0x0A] & USE_USB_VERSION_BIT;
b56d5a64 2993
b1859923 2994 // Addr 0C: USB version low byte when 0x0A
56ac0383 2995 // Addr 0D: USB version high byte when 0x0A
b1859923 2996 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
b56d5a64
MK
2997
2998 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2999 // Addr 0F: Length of manufacturer string
3000 manufacturer_size = buf[0x0F]/2;
56ac0383 3001 if (eeprom->manufacturer)
74e8e79d 3002 free(eeprom->manufacturer);
56ac0383 3003 if (manufacturer_size > 0)
acc1fa05
UB
3004 {
3005 eeprom->manufacturer = malloc(manufacturer_size);
3006 if (eeprom->manufacturer)
3007 {
3008 // Decode manufacturer
84ec032f 3009 i = buf[0x0E] & (eeprom_size -1); // offset
acc1fa05
UB
3010 for (j=0;j<manufacturer_size-1;j++)
3011 {
3012 eeprom->manufacturer[j] = buf[2*j+i+2];
3013 }
3014 eeprom->manufacturer[j] = '\0';
3015 }
3016 }
b56d5a64
MK
3017 else eeprom->manufacturer = NULL;
3018
3019 // Addr 10: Offset of the product string + 0x80, calculated later
3020 // Addr 11: Length of product string
56ac0383 3021 if (eeprom->product)
74e8e79d 3022 free(eeprom->product);
b56d5a64 3023 product_size = buf[0x11]/2;
acc1fa05
UB
3024 if (product_size > 0)
3025 {
3026 eeprom->product = malloc(product_size);
56ac0383 3027 if (eeprom->product)
acc1fa05
UB
3028 {
3029 // Decode product name
84ec032f 3030 i = buf[0x10] & (eeprom_size -1); // offset
acc1fa05
UB
3031 for (j=0;j<product_size-1;j++)
3032 {
3033 eeprom->product[j] = buf[2*j+i+2];
3034 }
3035 eeprom->product[j] = '\0';
3036 }
3037 }
b56d5a64
MK
3038 else eeprom->product = NULL;
3039
3040 // Addr 12: Offset of the serial string + 0x80, calculated later
3041 // Addr 13: Length of serial string
56ac0383 3042 if (eeprom->serial)
74e8e79d 3043 free(eeprom->serial);
b56d5a64 3044 serial_size = buf[0x13]/2;
acc1fa05
UB
3045 if (serial_size > 0)
3046 {
3047 eeprom->serial = malloc(serial_size);
56ac0383 3048 if (eeprom->serial)
acc1fa05
UB
3049 {
3050 // Decode serial
84ec032f 3051 i = buf[0x12] & (eeprom_size -1); // offset
acc1fa05
UB
3052 for (j=0;j<serial_size-1;j++)
3053 {
3054 eeprom->serial[j] = buf[2*j+i+2];
3055 }
3056 eeprom->serial[j] = '\0';
3057 }
3058 }
b56d5a64
MK
3059 else eeprom->serial = NULL;
3060
b56d5a64
MK
3061 // verify checksum
3062 checksum = 0xAAAA;
3063
22d12cda
TJ
3064 for (i = 0; i < eeprom_size/2-1; i++)
3065 {
b56d5a64
MK
3066 value = buf[i*2];
3067 value += buf[(i*2)+1] << 8;
3068
3069 checksum = value^checksum;
3070 checksum = (checksum << 1) | (checksum >> 15);
3071 }
3072
3073 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
3074
22d12cda
TJ
3075 if (eeprom_checksum != checksum)
3076 {
3077 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
cc9c9d58 3078 ftdi_error_return(-1,"EEPROM checksum error");
4af1d1bb
MK
3079 }
3080
eb498cff 3081 eeprom->channel_a_type = 0;
aa099f46 3082 if ((ftdi->type == TYPE_AM) || (ftdi->type == TYPE_BM))
f6ef2983 3083 {
6cd4f922 3084 eeprom->chip = -1;
f6ef2983 3085 }
56ac0383 3086 else if (ftdi->type == TYPE_2232C)
f6ef2983 3087 {
0fc2170c 3088 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
2cde7c52
UB
3089 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
3090 eeprom->high_current_a = buf[0x00] & HIGH_CURRENT_DRIVE;
3091 eeprom->channel_b_type = buf[0x01] & 0x7;
3092 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3093 eeprom->high_current_b = buf[0x01] & HIGH_CURRENT_DRIVE;
6cd4f922 3094 eeprom->chip = buf[0x14];
065edc58 3095 }
56ac0383 3096 else if (ftdi->type == TYPE_R)
564b2716 3097 {
2cde7c52
UB
3098 /* TYPE_R flags D2XX, not VCP as all others*/
3099 eeprom->channel_a_driver = (~buf[0x00]) & DRIVER_VCP;
3100 eeprom->high_current = buf[0x00] & HIGH_CURRENT_DRIVE_R;
56ac0383
TJ
3101 if ( (buf[0x01]&0x40) != 0x40)
3102 fprintf(stderr,
3103 "TYPE_R EEPROM byte[0x01] Bit 6 unexpected Endpoint size."
3104 " If this happened with the\n"
3105 " EEPROM programmed by FTDI tools, please report "
3106 "to libftdi@developer.intra2net.com\n");
2cde7c52 3107
6cd4f922 3108 eeprom->chip = buf[0x16];
cecb9cb2
UB
3109 // Addr 0B: Invert data lines
3110 // Works only on FT232R, not FT245R, but no way to distinguish
07851949
UB
3111 eeprom->invert = buf[0x0B];
3112 // Addr 14: CBUS function: CBUS0, CBUS1
3113 // Addr 15: CBUS function: CBUS2, CBUS3
3114 // Addr 16: CBUS function: CBUS5
3115 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
3116 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
3117 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
3118 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
3119 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
564b2716 3120 }
56ac0383 3121 else if ((ftdi->type == TYPE_2232H) ||(ftdi->type == TYPE_4232H))
db099ec5 3122 {
0fc2170c 3123 eeprom->channel_a_type = bit2type(buf[0x00] & 0x7);
2cde7c52 3124 eeprom->channel_a_driver = buf[0x00] & DRIVER_VCP;
c8f69686 3125 eeprom->channel_b_type = bit2type(buf[0x01] & 0x7);
2cde7c52
UB
3126 eeprom->channel_b_driver = buf[0x01] & DRIVER_VCP;
3127
56ac0383 3128 if (ftdi->type == TYPE_2232H)
ec0dcd3f 3129 eeprom->suspend_dbus7 = buf[0x01] & SUSPEND_DBUS7_BIT;
2cde7c52 3130
6cd4f922 3131 eeprom->chip = buf[0x18];
db099ec5
UB
3132 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3133 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3134 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3135 eeprom->group1_drive = (buf[0x0c] >> 4) & 0x3;
3136 eeprom->group1_schmitt = (buf[0x0c] >> 4) & IS_SCHMITT;
3137 eeprom->group1_slew = (buf[0x0c] >> 4) & SLOW_SLEW;
3138 eeprom->group2_drive = buf[0x0d] & DRIVE_16MA;
3139 eeprom->group2_schmitt = buf[0x0d] & IS_SCHMITT;
3140 eeprom->group2_slew = buf[0x0d] & SLOW_SLEW;
3141 eeprom->group3_drive = (buf[0x0d] >> 4) & DRIVE_16MA;
3142 eeprom->group3_schmitt = (buf[0x0d] >> 4) & IS_SCHMITT;
3143 eeprom->group3_slew = (buf[0x0d] >> 4) & SLOW_SLEW;
947d9552 3144 }
c7e4c09e
UB
3145 else if (ftdi->type == TYPE_232H)
3146 {
263d3ba0
UB
3147 int i;
3148
ac4a82a5
UB
3149 eeprom->channel_a_type = buf[0x00] & 0xf;
3150 eeprom->channel_a_driver = (buf[0x00] & DRIVER_VCPH)?DRIVER_VCP:0;
18199b76
UB
3151 eeprom->clock_polarity = buf[0x01] & FT1284_CLK_IDLE_STATE;
3152 eeprom->data_order = buf[0x01] & FT1284_DATA_LSB;
3153 eeprom->flow_control = buf[0x01] & FT1284_FLOW_CONTROL;
837a71d6 3154 eeprom->powersave = buf[0x01] & POWER_SAVE_DISABLE_H;
91d7a201
UB
3155 eeprom->group0_drive = buf[0x0c] & DRIVE_16MA;
3156 eeprom->group0_schmitt = buf[0x0c] & IS_SCHMITT;
3157 eeprom->group0_slew = buf[0x0c] & SLOW_SLEW;
3158 eeprom->group1_drive = buf[0x0d] & DRIVE_16MA;
3159 eeprom->group1_schmitt = buf[0x0d] & IS_SCHMITT;
3160 eeprom->group1_slew = buf[0x0d] & SLOW_SLEW;
3161
263d3ba0
UB
3162 for(i=0; i<5; i++)
3163 {
3164 eeprom->cbus_function[2*i ] = buf[0x18+i] & 0x0f;
3165 eeprom->cbus_function[2*i+1] = (buf[0x18+i] >> 4) & 0x0f;
3166 }
c7e4c09e
UB
3167 eeprom->chip = buf[0x1e];
3168 /*FIXME: Decipher more values*/
3169 }
56ac0383
TJ
3170
3171 if (verbose)
f6ef2983 3172 {
c8f69686 3173 char *channel_mode[] = {"UART", "FIFO", "CPU", "OPTO", "FT1284"};
f6ef2983
UB
3174 fprintf(stdout, "VID: 0x%04x\n",eeprom->vendor_id);
3175 fprintf(stdout, "PID: 0x%04x\n",eeprom->product_id);
38801bf8 3176 fprintf(stdout, "Release: 0x%04x\n",release);
f6ef2983 3177
56ac0383 3178 if (eeprom->self_powered)
f6ef2983
UB
3179 fprintf(stdout, "Self-Powered%s", (eeprom->remote_wakeup)?", USB Remote Wake Up\n":"\n");
3180 else
1cd815ad 3181 fprintf(stdout, "Bus Powered: %3d mA%s", eeprom->max_power * 2,
f6ef2983 3182 (eeprom->remote_wakeup)?" USB Remote Wake Up\n":"\n");
56ac0383 3183 if (eeprom->manufacturer)
f6ef2983 3184 fprintf(stdout, "Manufacturer: %s\n",eeprom->manufacturer);
56ac0383 3185 if (eeprom->product)
f6ef2983 3186 fprintf(stdout, "Product: %s\n",eeprom->product);
56ac0383 3187 if (eeprom->serial)
f6ef2983 3188 fprintf(stdout, "Serial: %s\n",eeprom->serial);
e107f509 3189 fprintf(stdout, "Checksum : %04x\n", checksum);
6cd4f922
UB
3190 if (ftdi->type == TYPE_R)
3191 fprintf(stdout, "Internal EEPROM\n");
3192 else if (eeprom->chip >= 0x46)
3193 fprintf(stdout, "Attached EEPROM: 93x%02x\n", eeprom->chip);
56ac0383
TJ
3194 if (eeprom->suspend_dbus7)
3195 fprintf(stdout, "Suspend on DBUS7\n");
3196 if (eeprom->suspend_pull_downs)
fb9bfdd1 3197 fprintf(stdout, "Pull IO pins low during suspend\n");
837a71d6
UB
3198 if(eeprom->powersave)
3199 {
3200 if(ftdi->type >= TYPE_232H)
3201 fprintf(stdout,"Enter low power state on ACBUS7\n");
3202 }
56ac0383 3203 if (eeprom->remote_wakeup)
fb9bfdd1 3204 fprintf(stdout, "Enable Remote Wake Up\n");
802a949e 3205 fprintf(stdout, "PNP: %d\n",(eeprom->is_not_pnp)?0:1);
db099ec5 3206 if (ftdi->type >= TYPE_2232C)
56ac0383 3207 fprintf(stdout,"Channel A has Mode %s%s%s\n",
e107f509 3208 channel_mode[eeprom->channel_a_type],
2cde7c52
UB
3209 (eeprom->channel_a_driver)?" VCP":"",
3210 (eeprom->high_current_a)?" High Current IO":"");
18199b76
UB
3211 if (ftdi->type >= TYPE_232H)
3212 {
3213 fprintf(stdout,"FT1284 Mode Clock is idle %s, %s first, %sFlow Control\n",
3214 (eeprom->clock_polarity)?"HIGH":"LOW",
3215 (eeprom->data_order)?"LSB":"MSB",
3216 (eeprom->flow_control)?"":"No ");
3217 }
c7e4c09e 3218 if ((ftdi->type >= TYPE_2232C) && (ftdi->type != TYPE_R) && (ftdi->type != TYPE_232H))
56ac0383 3219 fprintf(stdout,"Channel B has Mode %s%s%s\n",
e107f509 3220 channel_mode[eeprom->channel_b_type],
2cde7c52
UB
3221 (eeprom->channel_b_driver)?" VCP":"",
3222 (eeprom->high_current_b)?" High Current IO":"");
caec1294 3223 if (((ftdi->type == TYPE_BM) || (ftdi->type == TYPE_2232C)) &&
56ac0383 3224 eeprom->use_usb_version == USE_USB_VERSION_BIT)
caec1294
UB
3225 fprintf(stdout,"Use explicit USB Version %04x\n",eeprom->usb_version);
3226
56ac0383 3227 if ((ftdi->type == TYPE_2232H) || (ftdi->type == TYPE_4232H))
db099ec5
UB
3228 {
3229 fprintf(stdout,"%s has %d mA drive%s%s\n",
3230 (ftdi->type == TYPE_2232H)?"AL":"A",
3231 (eeprom->group0_drive+1) *4,
3232 (eeprom->group0_schmitt)?" Schmitt Input":"",
3233 (eeprom->group0_slew)?" Slow Slew":"");
3234 fprintf(stdout,"%s has %d mA drive%s%s\n",
3235 (ftdi->type == TYPE_2232H)?"AH":"B",
3236 (eeprom->group1_drive+1) *4,
3237 (eeprom->group1_schmitt)?" Schmitt Input":"",
3238 (eeprom->group1_slew)?" Slow Slew":"");
3239 fprintf(stdout,"%s has %d mA drive%s%s\n",
3240 (ftdi->type == TYPE_2232H)?"BL":"C",
3241 (eeprom->group2_drive+1) *4,
3242 (eeprom->group2_schmitt)?" Schmitt Input":"",
3243 (eeprom->group2_slew)?" Slow Slew":"");
3244 fprintf(stdout,"%s has %d mA drive%s%s\n",
3245 (ftdi->type == TYPE_2232H)?"BH":"D",
3246 (eeprom->group3_drive+1) *4,
3247 (eeprom->group3_schmitt)?" Schmitt Input":"",
3248 (eeprom->group3_slew)?" Slow Slew":"");
3249 }
91d7a201
UB
3250 else if (ftdi->type == TYPE_232H)
3251 {
263d3ba0
UB
3252 int i;
3253 char *cbush_mux[] = {"TRISTATE","RXLED","TXLED", "TXRXLED","PWREN",
3254 "SLEEP","DRIVE_0","DRIVE_1","IOMODE","TXDEN",
3255 "CLK30","CLK15","CLK7_5"
3256 };
91d7a201
UB
3257 fprintf(stdout,"ACBUS has %d mA drive%s%s\n",
3258 (eeprom->group0_drive+1) *4,
3259 (eeprom->group0_schmitt)?" Schmitt Input":"",
3260 (eeprom->group0_slew)?" Slow Slew":"");
3261 fprintf(stdout,"ADBUS has %d mA drive%s%s\n",
3262 (eeprom->group1_drive+1) *4,
3263 (eeprom->group1_schmitt)?" Schmitt Input":"",
3264 (eeprom->group1_slew)?" Slow Slew":"");
263d3ba0
UB
3265 for (i=0; i<10; i++)
3266 {
3267 if (eeprom->cbus_function[i]<= CBUSH_CLK7_5 )
3268 fprintf(stdout,"C%d Function: %s\n", i,
3269 cbush_mux[eeprom->cbus_function[i]]);
3270 }
3271
91d7a201
UB
3272 }
3273
a4980043
UB
3274 if (ftdi->type == TYPE_R)
3275 {
3276 char *cbus_mux[] = {"TXDEN","PWREN","RXLED", "TXLED","TX+RXLED",
13f00d3c 3277 "SLEEP","CLK48","CLK24","CLK12","CLK6",
56ac0383
TJ
3278 "IOMODE","BB_WR","BB_RD"
3279 };
13f00d3c 3280 char *cbus_BB[] = {"RXF","TXE","RD", "WR"};
56ac0383
TJ
3281
3282 if (eeprom->invert)
3283 {
a4980043
UB
3284 char *r_bits[] = {"TXD","RXD","RTS", "CTS","DTR","DSR","DCD","RI"};
3285 fprintf(stdout,"Inverted bits:");
3286 for (i=0; i<8; i++)
56ac0383 3287 if ((eeprom->invert & (1<<i)) == (1<<i))
a4980043
UB
3288 fprintf(stdout," %s",r_bits[i]);
3289 fprintf(stdout,"\n");
3290 }
56ac0383 3291 for (i=0; i<5; i++)
a4980043 3292 {
56ac0383 3293 if (eeprom->cbus_function[i]<CBUS_BB)
a4980043
UB
3294 fprintf(stdout,"C%d Function: %s\n", i,
3295 cbus_mux[eeprom->cbus_function[i]]);
3296 else
17431287 3297 {
598b2334
UB
3298 if (i < 4)
3299 /* Running MPROG show that C0..3 have fixed function Synchronous
3300 Bit Bang mode */
3301 fprintf(stdout,"C%d BB Function: %s\n", i,
3302 cbus_BB[i]);
3303 else
3304 fprintf(stdout, "Unknown CBUS mode. Might be special mode?\n");
17431287 3305 }
a4980043
UB
3306 }
3307 }
f6ef2983 3308 }
4af1d1bb 3309 return 0;
b56d5a64
MK
3310}
3311
1941414d 3312/**
44ef02bd
UB
3313 Get a value from the decoded EEPROM structure
3314
735e81ea
TJ
3315 \param ftdi pointer to ftdi_context
3316 \param value_name Enum of the value to query
3317 \param value Pointer to store read value
44ef02bd 3318
735e81ea
TJ
3319 \retval 0: all fine
3320 \retval -1: Value doesn't exist
44ef02bd
UB
3321*/
3322int ftdi_get_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int* value)
3323{
3324 switch (value_name)
3325 {
56ac0383
TJ
3326 case VENDOR_ID:
3327 *value = ftdi->eeprom->vendor_id;
3328 break;
3329 case PRODUCT_ID:
3330 *value = ftdi->eeprom->product_id;
3331 break;
3332 case SELF_POWERED:
3333 *value = ftdi->eeprom->self_powered;
3334 break;
3335 case REMOTE_WAKEUP:
3336 *value = ftdi->eeprom->remote_wakeup;
3337 break;
3338 case IS_NOT_PNP:
3339 *value = ftdi->eeprom->is_not_pnp;
3340 break;
3341 case SUSPEND_DBUS7:
3342 *value = ftdi->eeprom->suspend_dbus7;
3343 break;
3344 case IN_IS_ISOCHRONOUS:
3345 *value = ftdi->eeprom->in_is_isochronous;
3346 break;
cffed9f5
UB
3347 case OUT_IS_ISOCHRONOUS:
3348 *value = ftdi->eeprom->out_is_isochronous;
3349 break;
56ac0383
TJ
3350 case SUSPEND_PULL_DOWNS:
3351 *value = ftdi->eeprom->suspend_pull_downs;
3352 break;
3353 case USE_SERIAL:
3354 *value = ftdi->eeprom->use_serial;
3355 break;
3356 case USB_VERSION:
3357 *value = ftdi->eeprom->usb_version;
3358 break;
cffed9f5
UB
3359 case USE_USB_VERSION:
3360 *value = ftdi->eeprom->use_usb_version;
3361 break;
56ac0383
TJ
3362 case MAX_POWER:
3363 *value = ftdi->eeprom->max_power;
3364 break;
3365 case CHANNEL_A_TYPE:
3366 *value = ftdi->eeprom->channel_a_type;
3367 break;
3368 case CHANNEL_B_TYPE:
3369 *value = ftdi->eeprom->channel_b_type;
3370 break;
3371 case CHANNEL_A_DRIVER:
3372 *value = ftdi->eeprom->channel_a_driver;
3373 break;
3374 case CHANNEL_B_DRIVER:
3375 *value = ftdi->eeprom->channel_b_driver;
3376 break;
3377 case CBUS_FUNCTION_0:
3378 *value = ftdi->eeprom->cbus_function[0];
3379 break;
3380 case CBUS_FUNCTION_1:
3381 *value = ftdi->eeprom->cbus_function[1];
3382 break;
3383 case CBUS_FUNCTION_2:
3384 *value = ftdi->eeprom->cbus_function[2];
3385 break;
3386 case CBUS_FUNCTION_3:
3387 *value = ftdi->eeprom->cbus_function[3];
3388 break;
3389 case CBUS_FUNCTION_4:
3390 *value = ftdi->eeprom->cbus_function[4];
3391 break;
263d3ba0
UB
3392 case CBUS_FUNCTION_5:
3393 *value = ftdi->eeprom->cbus_function[5];
3394 break;
3395 case CBUS_FUNCTION_6:
3396 *value = ftdi->eeprom->cbus_function[6];
3397 break;
3398 case CBUS_FUNCTION_7:
3399 *value = ftdi->eeprom->cbus_function[7];
3400 break;
3401 case CBUS_FUNCTION_8:
3402 *value = ftdi->eeprom->cbus_function[8];
3403 break;
3404 case CBUS_FUNCTION_9:
3405 *value = ftdi->eeprom->cbus_function[8];
3406 break;
56ac0383
TJ
3407 case HIGH_CURRENT:
3408 *value = ftdi->eeprom->high_current;
3409 break;
3410 case HIGH_CURRENT_A:
3411 *value = ftdi->eeprom->high_current_a;
3412 break;
3413 case HIGH_CURRENT_B:
3414 *value = ftdi->eeprom->high_current_b;
3415 break;
3416 case INVERT:
3417 *value = ftdi->eeprom->invert;
3418 break;
3419 case GROUP0_DRIVE:
3420 *value = ftdi->eeprom->group0_drive;
3421 break;
3422 case GROUP0_SCHMITT:
3423 *value = ftdi->eeprom->group0_schmitt;
3424 break;
3425 case GROUP0_SLEW:
3426 *value = ftdi->eeprom->group0_slew;
3427 break;
3428 case GROUP1_DRIVE:
3429 *value = ftdi->eeprom->group1_drive;
3430 break;
3431 case GROUP1_SCHMITT:
3432 *value = ftdi->eeprom->group1_schmitt;
3433 break;
3434 case GROUP1_SLEW:
3435 *value = ftdi->eeprom->group1_slew;
3436 break;
3437 case GROUP2_DRIVE:
3438 *value = ftdi->eeprom->group2_drive;
3439 break;
3440 case GROUP2_SCHMITT:
3441 *value = ftdi->eeprom->group2_schmitt;
3442 break;
3443 case GROUP2_SLEW:
3444 *value = ftdi->eeprom->group2_slew;
3445 break;
3446 case GROUP3_DRIVE:
3447 *value = ftdi->eeprom->group3_drive;
3448 break;
3449 case GROUP3_SCHMITT:
3450 *value = ftdi->eeprom->group3_schmitt;
3451 break;
3452 case GROUP3_SLEW:
3453 *value = ftdi->eeprom->group3_slew;
3454 break;
837a71d6
UB
3455 case POWER_SAVE:
3456 *value = ftdi->eeprom->powersave;
3457 break;
18199b76
UB
3458 case CLOCK_POLARITY:
3459 *value = ftdi->eeprom->clock_polarity;
3460 break;
3461 case DATA_ORDER:
3462 *value = ftdi->eeprom->data_order;
3463 break;
3464 case FLOW_CONTROL:
3465 *value = ftdi->eeprom->flow_control;
3466 break;
3467 case CHIP_TYPE:
56ac0383
TJ
3468 *value = ftdi->eeprom->chip;
3469 break;
3470 case CHIP_SIZE:
3471 *value = ftdi->eeprom->size;
3472 break;
3473 default:
3474 ftdi_error_return(-1, "Request for unknown EEPROM value");
44ef02bd
UB
3475 }
3476 return 0;
3477}
3478
3479/**
3480 Set a value in the decoded EEPROM Structure
3481 No parameter checking is performed
3482
735e81ea 3483 \param ftdi pointer to ftdi_context
545f9df9 3484 \param value_name Enum of the value to set
735e81ea 3485 \param value to set
44ef02bd 3486
735e81ea
TJ
3487 \retval 0: all fine
3488 \retval -1: Value doesn't exist
3489 \retval -2: Value not user settable
44ef02bd
UB
3490*/
3491int ftdi_set_eeprom_value(struct ftdi_context *ftdi, enum ftdi_eeprom_value value_name, int value)
3492{
3493 switch (value_name)
3494 {
56ac0383
TJ
3495 case VENDOR_ID:
3496 ftdi->eeprom->vendor_id = value;
3497 break;
3498 case PRODUCT_ID:
3499 ftdi->eeprom->product_id = value;
3500 break;
3501 case SELF_POWERED:
3502 ftdi->eeprom->self_powered = value;
3503 break;
3504 case REMOTE_WAKEUP:
3505 ftdi->eeprom->remote_wakeup = value;
3506 break;
3507 case IS_NOT_PNP:
3508 ftdi->eeprom->is_not_pnp = value;
3509 break;
3510 case SUSPEND_DBUS7:
3511 ftdi->eeprom->suspend_dbus7 = value;
3512 break;
3513 case IN_IS_ISOCHRONOUS:
3514 ftdi->eeprom->in_is_isochronous = value;
3515 break;
cffed9f5
UB
3516 case OUT_IS_ISOCHRONOUS:
3517 ftdi->eeprom->out_is_isochronous = value;
3518 break;
56ac0383
TJ
3519 case SUSPEND_PULL_DOWNS:
3520 ftdi->eeprom->suspend_pull_downs = value;
3521 break;
3522 case USE_SERIAL:
3523 ftdi->eeprom->use_serial = value;
3524 break;
3525 case USB_VERSION:
3526 ftdi->eeprom->usb_version = value;
3527 break;
cffed9f5
UB
3528 case USE_USB_VERSION:
3529 ftdi->eeprom->use_usb_version = value;
3530 break;
56ac0383
TJ
3531 case MAX_POWER:
3532 ftdi->eeprom->max_power = value;
3533 break;
3534 case CHANNEL_A_TYPE:
3535 ftdi->eeprom->channel_a_type = value;
3536 break;
3537 case CHANNEL_B_TYPE:
3538 ftdi->eeprom->channel_b_type = value;
3539 break;
3540 case CHANNEL_A_DRIVER:
3541 ftdi->eeprom->channel_a_driver = value;
3542 break;
3543 case CHANNEL_B_DRIVER:
3544 ftdi->eeprom->channel_b_driver = value;
3545 break;
3546 case CBUS_FUNCTION_0:
3547 ftdi->eeprom->cbus_function[0] = value;
3548 break;
3549 case CBUS_FUNCTION_1:
3550 ftdi->eeprom->cbus_function[1] = value;
3551 break;
3552 case CBUS_FUNCTION_2:
3553 ftdi->eeprom->cbus_function[2] = value;
3554 break;
3555 case CBUS_FUNCTION_3:
3556 ftdi->eeprom->cbus_function[3] = value;
3557 break;
3558 case CBUS_FUNCTION_4:
3559 ftdi->eeprom->cbus_function[4] = value;
3560 break;
263d3ba0
UB
3561 case CBUS_FUNCTION_5:
3562 ftdi->eeprom->cbus_function[5] = value;
3563 break;
3564 case CBUS_FUNCTION_6:
3565 ftdi->eeprom->cbus_function[6] = value;
3566 break;
3567 case CBUS_FUNCTION_7:
3568 ftdi->eeprom->cbus_function[7] = value;
3569 break;
3570 case CBUS_FUNCTION_8:
3571 ftdi->eeprom->cbus_function[8] = value;
3572 break;
3573 case CBUS_FUNCTION_9:
3574 ftdi->eeprom->cbus_function[9] = value;
3575 break;
56ac0383
TJ
3576 case HIGH_CURRENT:
3577 ftdi->eeprom->high_current = value;
3578 break;
3579 case HIGH_CURRENT_A:
3580 ftdi->eeprom->high_current_a = value;
3581 break;
3582 case HIGH_CURRENT_B:
3583 ftdi->eeprom->high_current_b = value;
3584 break;
3585 case INVERT:
3586 ftdi->eeprom->invert = value;
3587 break;
3588 case GROUP0_DRIVE:
3589 ftdi->eeprom->group0_drive = value;
3590 break;
3591 case GROUP0_SCHMITT:
3592 ftdi->eeprom->group0_schmitt = value;
3593 break;
3594 case GROUP0_SLEW:
3595 ftdi->eeprom->group0_slew = value;
3596 break;
3597 case GROUP1_DRIVE:
3598 ftdi->eeprom->group1_drive = value;
3599 break;
3600 case GROUP1_SCHMITT:
3601 ftdi->eeprom->group1_schmitt = value;
3602 break;
3603 case GROUP1_SLEW:
3604 ftdi->eeprom->group1_slew = value;
3605 break;
3606 case GROUP2_DRIVE:
3607 ftdi->eeprom->group2_drive = value;
3608 break;
3609 case GROUP2_SCHMITT:
3610 ftdi->eeprom->group2_schmitt = value;
3611 break;
3612 case GROUP2_SLEW:
3613 ftdi->eeprom->group2_slew = value;
3614 break;
3615 case GROUP3_DRIVE:
3616 ftdi->eeprom->group3_drive = value;
3617 break;
3618 case GROUP3_SCHMITT:
3619 ftdi->eeprom->group3_schmitt = value;
3620 break;
3621 case GROUP3_SLEW:
3622 ftdi->eeprom->group3_slew = value;
3623 break;
3624 case CHIP_TYPE:
3625 ftdi->eeprom->chip = value;
3626 break;
837a71d6
UB
3627 case POWER_SAVE:
3628 ftdi->eeprom->powersave = value;
3629 break;
18199b76
UB
3630 case CLOCK_POLARITY:
3631 ftdi->eeprom->clock_polarity = value;
3632 break;
3633 case DATA_ORDER:
3634 ftdi->eeprom->data_order = value;
3635 break;
3636 case FLOW_CONTROL:
3637 ftdi->eeprom->flow_control = value;
3638 break;
56ac0383
TJ
3639 case CHIP_SIZE:
3640 ftdi_error_return(-2, "EEPROM Value can't be changed");
3641 default :
3642 ftdi_error_return(-1, "Request to unknown EEPROM value");
44ef02bd
UB
3643 }
3644 return 0;
3645}
3646
3647/** Get the read-only buffer to the binary EEPROM content
3648
3649 \param ftdi pointer to ftdi_context
735e81ea 3650 \param buf buffer to receive EEPROM content
44ef02bd
UB
3651 \param size Size of receiving buffer
3652
3653 \retval 0: All fine
3654 \retval -1: struct ftdi_contxt or ftdi_eeprom missing
200bd3ed 3655 \retval -2: Not enough room to store eeprom
44ef02bd 3656*/
56ac0383
TJ
3657int ftdi_get_eeprom_buf(struct ftdi_context *ftdi, unsigned char * buf, int size)
3658{
3659 if (!ftdi || !(ftdi->eeprom))
3660 ftdi_error_return(-1, "No appropriate structure");
b95e4654 3661
200bd3ed
TJ
3662 if (!buf || size < ftdi->eeprom->size)
3663 ftdi_error_return(-1, "Not enough room to store eeprom");
3664
b95e4654
TJ
3665 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3666 if (size > FTDI_MAX_EEPROM_SIZE)
3667 size = FTDI_MAX_EEPROM_SIZE;
3668
56ac0383 3669 memcpy(buf, ftdi->eeprom->buf, size);
b95e4654 3670
56ac0383
TJ
3671 return 0;
3672}
44ef02bd 3673
672fd368
UB
3674/** Set the EEPROM content from the user-supplied prefilled buffer
3675
3676 \param ftdi pointer to ftdi_context
3677 \param buf buffer to read EEPROM content
3678 \param size Size of buffer
3679
3680 \retval 0: All fine
3681 \retval -1: struct ftdi_contxt or ftdi_eeprom of buf missing
3682*/
3683int ftdi_set_eeprom_buf(struct ftdi_context *ftdi, const unsigned char * buf, int size)
3684{
3685 if (!ftdi || !(ftdi->eeprom) || !buf)
3686 ftdi_error_return(-1, "No appropriate structure");
3687
3688 // Only copy up to FTDI_MAX_EEPROM_SIZE bytes
3689 if (size > FTDI_MAX_EEPROM_SIZE)
3690 size = FTDI_MAX_EEPROM_SIZE;
3691
3692 memcpy(ftdi->eeprom->buf, buf, size);
3693
3694 return 0;
3695}
3696
44ef02bd 3697/**
c1c70e13
OS
3698 Read eeprom location
3699
3700 \param ftdi pointer to ftdi_context
3701 \param eeprom_addr Address of eeprom location to be read
3702 \param eeprom_val Pointer to store read eeprom location
3703
3704 \retval 0: all fine
3705 \retval -1: read failed
22a1b5c1 3706 \retval -2: USB device unavailable
c1c70e13
OS
3707*/
3708int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
3709{
22a1b5c1
TJ
3710 if (ftdi == NULL || ftdi->usb_dev == NULL)
3711 ftdi_error_return(-2, "USB device unavailable");
3712
97c6b5f6 3713 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
3714 ftdi_error_return(-1, "reading eeprom failed");
3715
3716 return 0;
3717}
3718
3719/**
1941414d
TJ
3720 Read eeprom
3721
3722 \param ftdi pointer to ftdi_context
b8aa7b35 3723
1941414d
TJ
3724 \retval 0: all fine
3725 \retval -1: read failed
22a1b5c1 3726 \retval -2: USB device unavailable
1941414d 3727*/
a35aa9bd 3728int ftdi_read_eeprom(struct ftdi_context *ftdi)
a8f46ddc 3729{
a3da1d95 3730 int i;
a35aa9bd 3731 unsigned char *buf;
a3da1d95 3732
22a1b5c1
TJ
3733 if (ftdi == NULL || ftdi->usb_dev == NULL)
3734 ftdi_error_return(-2, "USB device unavailable");
a35aa9bd 3735 buf = ftdi->eeprom->buf;
22a1b5c1 3736
2d543486 3737 for (i = 0; i < FTDI_MAX_EEPROM_SIZE/2; i++)
22d12cda 3738 {
a35aa9bd 3739 if (libusb_control_transfer(
56ac0383
TJ
3740 ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,SIO_READ_EEPROM_REQUEST, 0, i,
3741 buf+(i*2), 2, ftdi->usb_read_timeout) != 2)
c3d95b87 3742 ftdi_error_return(-1, "reading eeprom failed");
a3da1d95
GE
3743 }
3744
2d543486 3745 if (ftdi->type == TYPE_R)
a35aa9bd 3746 ftdi->eeprom->size = 0x80;
56ac0383 3747 /* Guesses size of eeprom by comparing halves
2d543486 3748 - will not work with blank eeprom */
a35aa9bd 3749 else if (strrchr((const char *)buf, 0xff) == ((const char *)buf +FTDI_MAX_EEPROM_SIZE -1))
2d543486 3750 ftdi->eeprom->size = -1;
56ac0383 3751 else if (memcmp(buf,&buf[0x80],0x80) == 0)
2d543486 3752 ftdi->eeprom->size = 0x80;
56ac0383 3753 else if (memcmp(buf,&buf[0x40],0x40) == 0)
2d543486
UB
3754 ftdi->eeprom->size = 0x40;
3755 else
3756 ftdi->eeprom->size = 0x100;
a3da1d95
GE
3757 return 0;
3758}
3759
cb6250fa
TJ
3760/*
3761 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
3762 Function is only used internally
3763 \internal
3764*/
3765static unsigned char ftdi_read_chipid_shift(unsigned char value)
3766{
3767 return ((value & 1) << 1) |
22d12cda
TJ
3768 ((value & 2) << 5) |
3769 ((value & 4) >> 2) |
3770 ((value & 8) << 4) |
3771 ((value & 16) >> 1) |
3772 ((value & 32) >> 1) |
3773 ((value & 64) >> 4) |
3774 ((value & 128) >> 2);
cb6250fa
TJ
3775}
3776
3777/**
3778 Read the FTDIChip-ID from R-type devices
3779
3780 \param ftdi pointer to ftdi_context
3781 \param chipid Pointer to store FTDIChip-ID
3782
3783 \retval 0: all fine
3784 \retval -1: read failed
22a1b5c1 3785 \retval -2: USB device unavailable
cb6250fa
TJ
3786*/
3787int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
3788{
c7eb3112 3789 unsigned int a = 0, b = 0;
cb6250fa 3790
22a1b5c1
TJ
3791 if (ftdi == NULL || ftdi->usb_dev == NULL)
3792 ftdi_error_return(-2, "USB device unavailable");
3793
579b006f 3794 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
3795 {
3796 a = a << 8 | a >> 8;
579b006f 3797 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
3798 {
3799 b = b << 8 | b >> 8;
5230676f 3800 a = (a << 16) | (b & 0xFFFF);
912d50ca
TJ
3801 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
3802 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
cb6250fa 3803 *chipid = a ^ 0xa5f0f7d1;
c7eb3112 3804 return 0;
cb6250fa
TJ
3805 }
3806 }
3807
c7eb3112 3808 ftdi_error_return(-1, "read of FTDIChip-ID failed");
cb6250fa
TJ
3809}
3810
1941414d 3811/**
c1c70e13
OS
3812 Write eeprom location
3813
3814 \param ftdi pointer to ftdi_context
3815 \param eeprom_addr Address of eeprom location to be written
3816 \param eeprom_val Value to be written
3817
3818 \retval 0: all fine
a661e3e4 3819 \retval -1: write failed
22a1b5c1 3820 \retval -2: USB device unavailable
a661e3e4
UB
3821 \retval -3: Invalid access to checksum protected area below 0x80
3822 \retval -4: Device can't access unprotected area
3823 \retval -5: Reading chip type failed
c1c70e13 3824*/
56ac0383 3825int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr,
a661e3e4 3826 unsigned short eeprom_val)
c1c70e13 3827{
a661e3e4
UB
3828 int chip_type_location;
3829 unsigned short chip_type;
3830
22a1b5c1
TJ
3831 if (ftdi == NULL || ftdi->usb_dev == NULL)
3832 ftdi_error_return(-2, "USB device unavailable");
3833
56ac0383 3834 if (eeprom_addr <0x80)
a661e3e4
UB
3835 ftdi_error_return(-2, "Invalid access to checksum protected area below 0x80");
3836
3837
3838 switch (ftdi->type)
3839 {
56ac0383
TJ
3840 case TYPE_BM:
3841 case TYPE_2232C:
3842 chip_type_location = 0x14;
3843 break;
3844 case TYPE_2232H:
3845 case TYPE_4232H:
3846 chip_type_location = 0x18;
3847 break;
c7e4c09e
UB
3848 case TYPE_232H:
3849 chip_type_location = 0x1e;
3850 break;
56ac0383
TJ
3851 default:
3852 ftdi_error_return(-4, "Device can't access unprotected area");
a661e3e4
UB
3853 }
3854
56ac0383 3855 if (ftdi_read_eeprom_location( ftdi, chip_type_location>>1, &chip_type))
a661e3e4 3856 ftdi_error_return(-5, "Reading failed failed");
56ac0383
TJ
3857 fprintf(stderr," loc 0x%04x val 0x%04x\n", chip_type_location,chip_type);
3858 if ((chip_type & 0xff) != 0x66)
a661e3e4
UB
3859 {
3860 ftdi_error_return(-6, "EEPROM is not of 93x66");
3861 }
3862
579b006f 3863 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
56ac0383
TJ
3864 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
3865 NULL, 0, ftdi->usb_write_timeout) != 0)
c1c70e13
OS
3866 ftdi_error_return(-1, "unable to write eeprom");
3867
3868 return 0;
3869}
3870
3871/**
1941414d 3872 Write eeprom
a3da1d95 3873
1941414d 3874 \param ftdi pointer to ftdi_context
56ac0383 3875
1941414d
TJ
3876 \retval 0: all fine
3877 \retval -1: read failed
22a1b5c1 3878 \retval -2: USB device unavailable
44f41f11 3879 \retval -3: EEPROM not initialized for the connected device;
1941414d 3880*/
a35aa9bd 3881int ftdi_write_eeprom(struct ftdi_context *ftdi)
a8f46ddc 3882{
ba5329be 3883 unsigned short usb_val, status;
e30da501 3884 int i, ret;
a35aa9bd 3885 unsigned char *eeprom;
a3da1d95 3886
22a1b5c1
TJ
3887 if (ftdi == NULL || ftdi->usb_dev == NULL)
3888 ftdi_error_return(-2, "USB device unavailable");
44f41f11
UB
3889
3890 if(ftdi->eeprom->initialized_for_connected_device == 0)
3891 ftdi_error_return(-3, "EEPROM not initialized for the connected device");
3892
a35aa9bd 3893 eeprom = ftdi->eeprom->buf;
22a1b5c1 3894
ba5329be 3895 /* These commands were traced while running MProg */
e30da501
TJ
3896 if ((ret = ftdi_usb_reset(ftdi)) != 0)
3897 return ret;
3898 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
3899 return ret;
3900 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
3901 return ret;
ba5329be 3902
c0a96aed 3903 for (i = 0; i < ftdi->eeprom->size/2; i++)
22d12cda 3904 {
d9f0cce7
TJ
3905 usb_val = eeprom[i*2];
3906 usb_val += eeprom[(i*2)+1] << 8;
579b006f
JZ
3907 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
3908 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
3909 NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87 3910 ftdi_error_return(-1, "unable to write eeprom");
a3da1d95
GE
3911 }
3912
3913 return 0;
3914}
3915
1941414d
TJ
3916/**
3917 Erase eeprom
a3da1d95 3918
a5e1bd8c
MK
3919 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
3920
1941414d
TJ
3921 \param ftdi pointer to ftdi_context
3922
3923 \retval 0: all fine
3924 \retval -1: erase failed
22a1b5c1 3925 \retval -2: USB device unavailable
99404ad5
UB
3926 \retval -3: Writing magic failed
3927 \retval -4: Read EEPROM failed
3928 \retval -5: Unexpected EEPROM value
1941414d 3929*/
99404ad5 3930#define MAGIC 0x55aa
a8f46ddc
TJ
3931int ftdi_erase_eeprom(struct ftdi_context *ftdi)
3932{
99404ad5 3933 unsigned short eeprom_value;
22a1b5c1
TJ
3934 if (ftdi == NULL || ftdi->usb_dev == NULL)
3935 ftdi_error_return(-2, "USB device unavailable");
3936
56ac0383 3937 if (ftdi->type == TYPE_R)
99404ad5
UB
3938 {
3939 ftdi->eeprom->chip = 0;
3940 return 0;
3941 }
3942
56ac0383 3943 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
99404ad5 3944 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87 3945 ftdi_error_return(-1, "unable to erase eeprom");
a3da1d95 3946
56ac0383 3947
99404ad5
UB
3948 /* detect chip type by writing 0x55AA as magic at word position 0xc0
3949 Chip is 93x46 if magic is read at word position 0x00, as wraparound happens around 0x40
3950 Chip is 93x56 if magic is read at word position 0x40, as wraparound happens around 0x80
3951 Chip is 93x66 if magic is only read at word position 0xc0*/
10186c1f 3952 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
56ac0383
TJ
3953 SIO_WRITE_EEPROM_REQUEST, MAGIC, 0xc0,
3954 NULL, 0, ftdi->usb_write_timeout) != 0)
99404ad5 3955 ftdi_error_return(-3, "Writing magic failed");
56ac0383 3956 if (ftdi_read_eeprom_location( ftdi, 0x00, &eeprom_value))
99404ad5 3957 ftdi_error_return(-4, "Reading failed failed");
56ac0383 3958 if (eeprom_value == MAGIC)
99404ad5
UB
3959 {
3960 ftdi->eeprom->chip = 0x46;
3961 }
56ac0383 3962 else
99404ad5 3963 {
56ac0383 3964 if (ftdi_read_eeprom_location( ftdi, 0x40, &eeprom_value))
99404ad5 3965 ftdi_error_return(-4, "Reading failed failed");
56ac0383 3966 if (eeprom_value == MAGIC)
99404ad5 3967 ftdi->eeprom->chip = 0x56;
56ac0383 3968 else
99404ad5 3969 {
56ac0383 3970 if (ftdi_read_eeprom_location( ftdi, 0xc0, &eeprom_value))
99404ad5 3971 ftdi_error_return(-4, "Reading failed failed");
56ac0383 3972 if (eeprom_value == MAGIC)
99404ad5
UB
3973 ftdi->eeprom->chip = 0x66;
3974 else
3975 {
3976 ftdi->eeprom->chip = -1;
3977 }
3978 }
3979 }
56ac0383 3980 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST,
99404ad5
UB
3981 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
3982 ftdi_error_return(-1, "unable to erase eeprom");
a3da1d95
GE
3983 return 0;
3984}
c3d95b87 3985
1941414d
TJ
3986/**
3987 Get string representation for last error code
c3d95b87 3988
1941414d
TJ
3989 \param ftdi pointer to ftdi_context
3990
3991 \retval Pointer to error string
3992*/
c3d95b87
TJ
3993char *ftdi_get_error_string (struct ftdi_context *ftdi)
3994{
22a1b5c1
TJ
3995 if (ftdi == NULL)
3996 return "";
3997
c3d95b87
TJ
3998 return ftdi->error_str;
3999}
a01d31e2 4000
b5ec1820 4001/* @} end of doxygen libftdi group */