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