Remove duplicate init code
[libftdi] / src / ftdi.c
CommitLineData
a3da1d95
GE
1/***************************************************************************
2 ftdi.c - description
3 -------------------
4 begin : Fri Apr 4 2003
22a1b5c1 5 copyright : (C) 2003-2010 by Intra2net AG
5fdb1cb1 6 email : opensource@intra2net.com
a3da1d95
GE
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU Lesser General Public License *
13 * version 2.1 as published by the Free Software Foundation; *
14 * *
15 ***************************************************************************/
d9f0cce7 16
b5ec1820
TJ
17/**
18 \mainpage libftdi API documentation
19
ad397a4b 20 Library to talk to FTDI chips. You find the latest versions of libftdi at
1bfc403c 21 http://www.intra2net.com/en/developer/libftdi/
b5ec1820 22
ad397a4b
TJ
23 The library is easy to use. Have a look at this short example:
24 \include simple.c
25
26 More examples can be found in the "examples" directory.
b5ec1820
TJ
27*/
28/** \addtogroup libftdi */
29/* @{ */
30
579b006f 31#include <libusb.h>
a8f46ddc 32#include <string.h>
d2f10023 33#include <errno.h>
b56d5a64 34#include <stdio.h>
579b006f 35#include <stdlib.h>
0e302db6 36
98452d97 37#include "ftdi.h"
a3da1d95 38
21abaf2e 39#define ftdi_error_return(code, str) do { \
2f73e59f 40 ftdi->error_str = str; \
21abaf2e 41 return code; \
d2f10023 42 } while(0);
c3d95b87 43
99650502
UB
44#define ftdi_error_return_free_device_list(code, str, devs) do { \
45 libusb_free_device_list(devs,1); \
46 ftdi->error_str = str; \
47 return code; \
48 } while(0);
49
418aaa72 50
f3f81007
TJ
51/**
52 Internal function to close usb device pointer.
53 Sets ftdi->usb_dev to NULL.
54 \internal
55
56 \param ftdi pointer to ftdi_context
57
579b006f 58 \retval none
f3f81007 59*/
579b006f 60static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
dff4fdb0 61{
22a1b5c1 62 if (ftdi && ftdi->usb_dev)
dff4fdb0 63 {
579b006f 64 libusb_close (ftdi->usb_dev);
dff4fdb0
NF
65 ftdi->usb_dev = NULL;
66 }
dff4fdb0 67}
c3d95b87 68
1941414d
TJ
69/**
70 Initializes a ftdi_context.
4837f98a 71
1941414d 72 \param ftdi pointer to ftdi_context
4837f98a 73
1941414d
TJ
74 \retval 0: all fine
75 \retval -1: couldn't allocate read buffer
76
77 \remark This should be called before all functions
948f9ada 78*/
a8f46ddc
TJ
79int ftdi_init(struct ftdi_context *ftdi)
80{
02212d8e 81 ftdi->usb_ctx = NULL;
98452d97 82 ftdi->usb_dev = NULL;
545820ce
TJ
83 ftdi->usb_read_timeout = 5000;
84 ftdi->usb_write_timeout = 5000;
a3da1d95 85
53ad271d 86 ftdi->type = TYPE_BM; /* chip type */
a3da1d95 87 ftdi->baudrate = -1;
418aaa72 88 ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
a3da1d95 89
948f9ada
TJ
90 ftdi->readbuffer = NULL;
91 ftdi->readbuffer_offset = 0;
92 ftdi->readbuffer_remaining = 0;
93 ftdi->writebuffer_chunksize = 4096;
e2f12a4f 94 ftdi->max_packet_size = 0;
948f9ada 95
783b771a 96 ftdi_set_interface(ftdi, INTERFACE_ANY);
418aaa72 97 ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
53ad271d 98
a3da1d95
GE
99 ftdi->error_str = NULL;
100
c201f80f
TJ
101 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
102
1c733d33
TJ
103 /* All fine. Now allocate the readbuffer */
104 return ftdi_read_data_set_chunksize(ftdi, 4096);
948f9ada 105}
4837f98a 106
1941414d 107/**
cef378aa
TJ
108 Allocate and initialize a new ftdi_context
109
110 \return a pointer to a new ftdi_context, or NULL on failure
111*/
672ac008 112struct ftdi_context *ftdi_new(void)
cef378aa
TJ
113{
114 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
115
22d12cda
TJ
116 if (ftdi == NULL)
117 {
cef378aa
TJ
118 return NULL;
119 }
120
22d12cda
TJ
121 if (ftdi_init(ftdi) != 0)
122 {
cef378aa 123 free(ftdi);
cdf448f6 124 return NULL;
cef378aa
TJ
125 }
126
127 return ftdi;
128}
129
130/**
1941414d
TJ
131 Open selected channels on a chip, otherwise use first channel.
132
133 \param ftdi pointer to ftdi_context
f9d69895 134 \param interface Interface to use for FT2232C/2232H/4232H chips.
1941414d
TJ
135
136 \retval 0: all fine
137 \retval -1: unknown interface
22a1b5c1 138 \retval -2: USB device unavailable
c4446c36 139*/
0ce2f5fa 140int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
c4446c36 141{
1971c26d 142 if (ftdi == NULL)
22a1b5c1
TJ
143 ftdi_error_return(-2, "USB device unavailable");
144
22d12cda
TJ
145 switch (interface)
146 {
147 case INTERFACE_ANY:
148 case INTERFACE_A:
783b771a
VY
149 ftdi->interface = 0;
150 ftdi->index = INTERFACE_A;
151 ftdi->in_ep = 0x02;
152 ftdi->out_ep = 0x81;
22d12cda
TJ
153 break;
154 case INTERFACE_B:
155 ftdi->interface = 1;
156 ftdi->index = INTERFACE_B;
157 ftdi->in_ep = 0x04;
158 ftdi->out_ep = 0x83;
159 break;
f9d69895
AH
160 case INTERFACE_C:
161 ftdi->interface = 2;
162 ftdi->index = INTERFACE_C;
163 ftdi->in_ep = 0x06;
164 ftdi->out_ep = 0x85;
165 break;
166 case INTERFACE_D:
167 ftdi->interface = 3;
168 ftdi->index = INTERFACE_D;
169 ftdi->in_ep = 0x08;
170 ftdi->out_ep = 0x87;
171 break;
22d12cda
TJ
172 default:
173 ftdi_error_return(-1, "Unknown interface");
c4446c36
TJ
174 }
175 return 0;
176}
948f9ada 177
1941414d
TJ
178/**
179 Deinitializes a ftdi_context.
4837f98a 180
1941414d 181 \param ftdi pointer to ftdi_context
4837f98a 182*/
a8f46ddc
TJ
183void ftdi_deinit(struct ftdi_context *ftdi)
184{
22a1b5c1
TJ
185 if (ftdi == NULL)
186 return;
187
f3f81007 188 ftdi_usb_close_internal (ftdi);
dff4fdb0 189
22d12cda
TJ
190 if (ftdi->readbuffer != NULL)
191 {
d9f0cce7
TJ
192 free(ftdi->readbuffer);
193 ftdi->readbuffer = NULL;
948f9ada 194 }
02212d8e 195 libusb_exit(ftdi->usb_ctx);
a3da1d95
GE
196}
197
1941414d 198/**
cef378aa
TJ
199 Deinitialize and free an ftdi_context.
200
201 \param ftdi pointer to ftdi_context
202*/
203void ftdi_free(struct ftdi_context *ftdi)
204{
205 ftdi_deinit(ftdi);
206 free(ftdi);
207}
208
209/**
1941414d
TJ
210 Use an already open libusb device.
211
212 \param ftdi pointer to ftdi_context
579b006f 213 \param usb libusb libusb_device_handle to use
4837f98a 214*/
579b006f 215void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
a8f46ddc 216{
22a1b5c1
TJ
217 if (ftdi == NULL)
218 return;
219
98452d97
TJ
220 ftdi->usb_dev = usb;
221}
222
223
1941414d
TJ
224/**
225 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
226 needs to be deallocated by ftdi_list_free() after use.
227
228 \param ftdi pointer to ftdi_context
229 \param devlist Pointer where to store list of found devices
230 \param vendor Vendor ID to search for
231 \param product Product ID to search for
edb82cbf 232
1941414d 233 \retval >0: number of devices found
1941414d 234 \retval -3: out of memory
579b006f
JZ
235 \retval -4: libusb_init() failed
236 \retval -5: libusb_get_device_list() failed
237 \retval -6: libusb_get_device_descriptor() failed
edb82cbf 238*/
d2f10023 239int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
edb82cbf
TJ
240{
241 struct ftdi_device_list **curdev;
579b006f
JZ
242 libusb_device *dev;
243 libusb_device **devs;
edb82cbf 244 int count = 0;
579b006f
JZ
245 int i = 0;
246
02212d8e 247 if (libusb_init(&ftdi->usb_ctx) < 0)
579b006f 248 ftdi_error_return(-4, "libusb_init() failed");
d2f10023 249
02212d8e 250 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
579b006f 251 ftdi_error_return(-5, "libusb_get_device_list() failed");
edb82cbf
TJ
252
253 curdev = devlist;
6db32169 254 *curdev = NULL;
579b006f
JZ
255
256 while ((dev = devs[i++]) != NULL)
22d12cda 257 {
579b006f 258 struct libusb_device_descriptor desc;
d2f10023 259
579b006f
JZ
260 if (libusb_get_device_descriptor(dev, &desc) < 0)
261 ftdi_error_return(-6, "libusb_get_device_descriptor() failed");
edb82cbf 262
579b006f
JZ
263 if (desc.idVendor == vendor && desc.idProduct == product)
264 {
265 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
266 if (!*curdev)
267 ftdi_error_return(-3, "out of memory");
268
269 (*curdev)->next = NULL;
270 (*curdev)->dev = dev;
271
272 curdev = &(*curdev)->next;
273 count++;
edb82cbf
TJ
274 }
275 }
d2f10023 276
edb82cbf
TJ
277 return count;
278}
279
1941414d
TJ
280/**
281 Frees a usb device list.
edb82cbf 282
1941414d 283 \param devlist USB device list created by ftdi_usb_find_all()
edb82cbf 284*/
d2f10023 285void ftdi_list_free(struct ftdi_device_list **devlist)
edb82cbf 286{
6db32169
TJ
287 struct ftdi_device_list *curdev, *next;
288
22d12cda
TJ
289 for (curdev = *devlist; curdev != NULL;)
290 {
6db32169
TJ
291 next = curdev->next;
292 free(curdev);
293 curdev = next;
edb82cbf
TJ
294 }
295
6db32169 296 *devlist = NULL;
edb82cbf
TJ
297}
298
1941414d 299/**
cef378aa
TJ
300 Frees a usb device list.
301
302 \param devlist USB device list created by ftdi_usb_find_all()
303*/
304void ftdi_list_free2(struct ftdi_device_list *devlist)
305{
306 ftdi_list_free(&devlist);
307}
308
309/**
474786c0
TJ
310 Return device ID strings from the usb device.
311
312 The parameters manufacturer, description and serial may be NULL
313 or pointer to buffers to store the fetched strings.
314
898c34dd
TJ
315 \note Use this function only in combination with ftdi_usb_find_all()
316 as it closes the internal "usb_dev" after use.
317
474786c0
TJ
318 \param ftdi pointer to ftdi_context
319 \param dev libusb usb_dev to use
320 \param manufacturer Store manufacturer string here if not NULL
321 \param mnf_len Buffer size of manufacturer string
322 \param description Store product description string here if not NULL
323 \param desc_len Buffer size of product description string
324 \param serial Store serial string here if not NULL
325 \param serial_len Buffer size of serial string
326
327 \retval 0: all fine
328 \retval -1: wrong arguments
329 \retval -4: unable to open device
330 \retval -7: get product manufacturer failed
331 \retval -8: get product description failed
332 \retval -9: get serial number failed
579b006f 333 \retval -11: libusb_get_device_descriptor() failed
474786c0 334*/
579b006f 335int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
22d12cda 336 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
474786c0 337{
579b006f
JZ
338 struct libusb_device_descriptor desc;
339
474786c0
TJ
340 if ((ftdi==NULL) || (dev==NULL))
341 return -1;
342
579b006f
JZ
343 if (libusb_open(dev, &ftdi->usb_dev) < 0)
344 ftdi_error_return(-4, "libusb_open() failed");
345
346 if (libusb_get_device_descriptor(dev, &desc) < 0)
347 ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
474786c0 348
22d12cda
TJ
349 if (manufacturer != NULL)
350 {
579b006f 351 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
22d12cda 352 {
f3f81007 353 ftdi_usb_close_internal (ftdi);
579b006f 354 ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
474786c0
TJ
355 }
356 }
357
22d12cda
TJ
358 if (description != NULL)
359 {
579b006f 360 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
22d12cda 361 {
f3f81007 362 ftdi_usb_close_internal (ftdi);
579b006f 363 ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
474786c0
TJ
364 }
365 }
366
22d12cda
TJ
367 if (serial != NULL)
368 {
579b006f 369 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
22d12cda 370 {
f3f81007 371 ftdi_usb_close_internal (ftdi);
579b006f 372 ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
474786c0
TJ
373 }
374 }
375
579b006f 376 ftdi_usb_close_internal (ftdi);
474786c0
TJ
377
378 return 0;
379}
380
381/**
e2f12a4f
TJ
382 * Internal function to determine the maximum packet size.
383 * \param ftdi pointer to ftdi_context
384 * \param dev libusb usb_dev to use
385 * \retval Maximum packet size for this device
386 */
579b006f 387static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
e2f12a4f 388{
579b006f
JZ
389 struct libusb_device_descriptor desc;
390 struct libusb_config_descriptor *config0;
e2f12a4f
TJ
391 unsigned int packet_size;
392
22a1b5c1
TJ
393 // Sanity check
394 if (ftdi == NULL || dev == NULL)
395 return 64;
396
e2f12a4f
TJ
397 // Determine maximum packet size. Init with default value.
398 // New hi-speed devices from FTDI use a packet size of 512 bytes
399 // but could be connected to a normal speed USB hub -> 64 bytes packet size.
400 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
401 packet_size = 512;
402 else
403 packet_size = 64;
404
579b006f
JZ
405 if (libusb_get_device_descriptor(dev, &desc) < 0)
406 return packet_size;
407
408 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
409 return packet_size;
e2f12a4f 410
579b006f
JZ
411 if (desc.bNumConfigurations > 0)
412 {
413 if (ftdi->interface < config0->bNumInterfaces)
e2f12a4f 414 {
579b006f 415 struct libusb_interface interface = config0->interface[ftdi->interface];
e2f12a4f
TJ
416 if (interface.num_altsetting > 0)
417 {
579b006f 418 struct libusb_interface_descriptor descriptor = interface.altsetting[0];
e2f12a4f
TJ
419 if (descriptor.bNumEndpoints > 0)
420 {
421 packet_size = descriptor.endpoint[0].wMaxPacketSize;
422 }
423 }
424 }
425 }
426
579b006f 427 libusb_free_config_descriptor (config0);
e2f12a4f
TJ
428 return packet_size;
429}
430
431/**
418aaa72 432 Opens a ftdi device given by an usb_device.
7b18bef6 433
1941414d
TJ
434 \param ftdi pointer to ftdi_context
435 \param dev libusb usb_dev to use
436
437 \retval 0: all fine
23b1798d 438 \retval -3: unable to config device
1941414d
TJ
439 \retval -4: unable to open device
440 \retval -5: unable to claim device
441 \retval -6: reset failed
442 \retval -7: set baudrate failed
22a1b5c1 443 \retval -8: ftdi context invalid
579b006f
JZ
444 \retval -9: libusb_get_device_descriptor() failed
445 \retval -10: libusb_get_config_descriptor() failed
446 \retval -11: libusb_etach_kernel_driver() failed
447 \retval -12: libusb_get_configuration() failed
7b18bef6 448*/
579b006f 449int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
7b18bef6 450{
579b006f
JZ
451 struct libusb_device_descriptor desc;
452 struct libusb_config_descriptor *config0;
43aee24f 453 int cfg, cfg0, detach_errno = 0;
579b006f 454
22a1b5c1
TJ
455 if (ftdi == NULL)
456 ftdi_error_return(-8, "ftdi context invalid");
457
579b006f
JZ
458 if (libusb_open(dev, &ftdi->usb_dev) < 0)
459 ftdi_error_return(-4, "libusb_open() failed");
460
461 if (libusb_get_device_descriptor(dev, &desc) < 0)
462 ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
463
464 if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
465 ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
466 cfg0 = config0->bConfigurationValue;
467 libusb_free_config_descriptor (config0);
d2f10023 468
22592e17 469 // Try to detach ftdi_sio kernel module.
22592e17
TJ
470 //
471 // The return code is kept in a separate variable and only parsed
472 // if usb_set_configuration() or usb_claim_interface() fails as the
473 // detach operation might be denied and everything still works fine.
474 // Likely scenario is a static ftdi_sio kernel module.
43aee24f
UB
475 if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
476 detach_errno = errno;
d2f10023 477
579b006f
JZ
478 if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
479 ftdi_error_return(-12, "libusb_get_configuration () failed");
b57aedfd
GE
480 // set configuration (needed especially for windows)
481 // tolerate EBUSY: one device with one configuration, but two interfaces
482 // and libftdi sessions to both interfaces (e.g. FT2232)
579b006f 483 if (desc.bNumConfigurations > 0 && cfg != cfg0)
b57aedfd 484 {
579b006f 485 if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
22d12cda 486 {
a56ba2bd 487 ftdi_usb_close_internal (ftdi);
43aee24f
UB
488 if(detach_errno == EPERM)
489 {
490 ftdi_error_return(-8, "inappropriate permissions on device!");
491 }
492 else
493 {
c16b162d 494 ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
43aee24f 495 }
23b1798d
TJ
496 }
497 }
498
579b006f 499 if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
22d12cda 500 {
f3f81007 501 ftdi_usb_close_internal (ftdi);
43aee24f
UB
502 if(detach_errno == EPERM)
503 {
504 ftdi_error_return(-8, "inappropriate permissions on device!");
505 }
506 else
507 {
c16b162d 508 ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
43aee24f 509 }
7b18bef6
TJ
510 }
511
22d12cda
TJ
512 if (ftdi_usb_reset (ftdi) != 0)
513 {
f3f81007 514 ftdi_usb_close_internal (ftdi);
7b18bef6
TJ
515 ftdi_error_return(-6, "ftdi_usb_reset failed");
516 }
517
7b18bef6
TJ
518 // Try to guess chip type
519 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
579b006f
JZ
520 if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
521 && desc.iSerialNumber == 0))
7b18bef6 522 ftdi->type = TYPE_BM;
579b006f 523 else if (desc.bcdDevice == 0x200)
7b18bef6 524 ftdi->type = TYPE_AM;
579b006f 525 else if (desc.bcdDevice == 0x500)
7b18bef6 526 ftdi->type = TYPE_2232C;
579b006f 527 else if (desc.bcdDevice == 0x600)
cb6250fa 528 ftdi->type = TYPE_R;
579b006f 529 else if (desc.bcdDevice == 0x700)
0beb9686 530 ftdi->type = TYPE_2232H;
579b006f 531 else if (desc.bcdDevice == 0x800)
0beb9686 532 ftdi->type = TYPE_4232H;
7b18bef6 533
e2f12a4f
TJ
534 // Determine maximum packet size
535 ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
536
ef6f4838
TE
537 if (ftdi_set_baudrate (ftdi, 9600) != 0)
538 {
539 ftdi_usb_close_internal (ftdi);
540 ftdi_error_return(-7, "set baudrate failed");
541 }
542
7b18bef6
TJ
543 ftdi_error_return(0, "all fine");
544}
545
1941414d
TJ
546/**
547 Opens the first device with a given vendor and product ids.
548
549 \param ftdi pointer to ftdi_context
550 \param vendor Vendor ID
551 \param product Product ID
552
9bec2387 553 \retval same as ftdi_usb_open_desc()
1941414d 554*/
edb82cbf
TJ
555int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
556{
557 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
558}
559
1941414d
TJ
560/**
561 Opens the first device with a given, vendor id, product id,
562 description and serial.
563
564 \param ftdi pointer to ftdi_context
565 \param vendor Vendor ID
566 \param product Product ID
567 \param description Description to search for. Use NULL if not needed.
568 \param serial Serial to search for. Use NULL if not needed.
569
570 \retval 0: all fine
1941414d
TJ
571 \retval -3: usb device not found
572 \retval -4: unable to open device
573 \retval -5: unable to claim device
574 \retval -6: reset failed
575 \retval -7: set baudrate failed
576 \retval -8: get product description failed
577 \retval -9: get serial number failed
579b006f
JZ
578 \retval -11: libusb_init() failed
579 \retval -12: libusb_get_device_list() failed
580 \retval -13: libusb_get_device_descriptor() failed
a3da1d95 581*/
04e1ea0a 582int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
a8f46ddc
TJ
583 const char* description, const char* serial)
584{
5ebbdab9
GE
585 return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
586}
587
588/**
589 Opens the index-th device with a given, vendor id, product id,
590 description and serial.
591
592 \param ftdi pointer to ftdi_context
593 \param vendor Vendor ID
594 \param product Product ID
595 \param description Description to search for. Use NULL if not needed.
596 \param serial Serial to search for. Use NULL if not needed.
597 \param index Number of matching device to open if there are more than one, starts with 0.
598
599 \retval 0: all fine
600 \retval -1: usb_find_busses() failed
601 \retval -2: usb_find_devices() failed
602 \retval -3: usb device not found
603 \retval -4: unable to open device
604 \retval -5: unable to claim device
605 \retval -6: reset failed
606 \retval -7: set baudrate failed
607 \retval -8: get product description failed
608 \retval -9: get serial number failed
609 \retval -10: unable to close device
22a1b5c1 610 \retval -11: ftdi context invalid
5ebbdab9
GE
611*/
612int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
613 const char* description, const char* serial, unsigned int index)
614{
579b006f
JZ
615 libusb_device *dev;
616 libusb_device **devs;
c3d95b87 617 char string[256];
579b006f 618 int i = 0;
98452d97 619
02212d8e 620 if (libusb_init(&ftdi->usb_ctx) < 0)
579b006f 621 ftdi_error_return(-11, "libusb_init() failed");
98452d97 622
22a1b5c1
TJ
623 if (ftdi == NULL)
624 ftdi_error_return(-11, "ftdi context invalid");
625
02212d8e 626 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
99650502
UB
627 ftdi_error_return(-12, "libusb_get_device_list() failed");
628
579b006f 629 while ((dev = devs[i++]) != NULL)
22d12cda 630 {
579b006f 631 struct libusb_device_descriptor desc;
99650502 632 int res;
579b006f
JZ
633
634 if (libusb_get_device_descriptor(dev, &desc) < 0)
99650502 635 ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
579b006f
JZ
636
637 if (desc.idVendor == vendor && desc.idProduct == product)
22d12cda 638 {
579b006f 639 if (libusb_open(dev, &ftdi->usb_dev) < 0)
99650502 640 ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
c3d95b87 641
579b006f
JZ
642 if (description != NULL)
643 {
644 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
22d12cda 645 {
579b006f 646 libusb_close (ftdi->usb_dev);
99650502 647 ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
a8f46ddc 648 }
579b006f 649 if (strncmp(string, description, sizeof(string)) != 0)
22d12cda 650 {
579b006f
JZ
651 libusb_close (ftdi->usb_dev);
652 continue;
a8f46ddc 653 }
579b006f
JZ
654 }
655 if (serial != NULL)
656 {
657 if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
658 {
659 ftdi_usb_close_internal (ftdi);
99650502 660 ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
579b006f
JZ
661 }
662 if (strncmp(string, serial, sizeof(string)) != 0)
663 {
664 ftdi_usb_close_internal (ftdi);
665 continue;
666 }
667 }
98452d97 668
579b006f 669 ftdi_usb_close_internal (ftdi);
d2f10023 670
5ebbdab9
GE
671 if (index > 0)
672 {
673 index--;
674 continue;
675 }
676
99650502
UB
677 res = ftdi_usb_open_dev(ftdi, dev);
678 libusb_free_device_list(devs,1);
679 return res;
98452d97 680 }
98452d97 681 }
a3da1d95 682
98452d97 683 // device not found
99650502 684 ftdi_error_return_free_device_list(-3, "device not found", devs);
a3da1d95
GE
685}
686
1941414d 687/**
5ebbdab9
GE
688 Opens the ftdi-device described by a description-string.
689 Intended to be used for parsing a device-description given as commandline argument.
690
691 \param ftdi pointer to ftdi_context
692 \param description NULL-terminated description-string, using this format:
693 \li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
694 \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")
695 \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
696 \li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
697
698 \note The description format may be extended in later versions.
699
700 \retval 0: all fine
579b006f
JZ
701 \retval -1: libusb_init() failed
702 \retval -2: libusb_get_device_list() failed
5ebbdab9
GE
703 \retval -3: usb device not found
704 \retval -4: unable to open device
705 \retval -5: unable to claim device
706 \retval -6: reset failed
707 \retval -7: set baudrate failed
708 \retval -8: get product description failed
709 \retval -9: get serial number failed
710 \retval -10: unable to close device
711 \retval -11: illegal description format
22a1b5c1 712 \retval -12: ftdi context invalid
5ebbdab9
GE
713*/
714int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
715{
22a1b5c1
TJ
716 if (ftdi == NULL)
717 ftdi_error_return(-12, "ftdi context invalid");
718
5ebbdab9
GE
719 if (description[0] == 0 || description[1] != ':')
720 ftdi_error_return(-11, "illegal description format");
721
722 if (description[0] == 'd')
723 {
579b006f
JZ
724 libusb_device *dev;
725 libusb_device **devs;
726 unsigned int bus_number, device_address;
727 int i = 0;
728
02212d8e 729 if (libusb_init (&ftdi->usb_ctx) < 0)
579b006f 730 ftdi_error_return(-1, "libusb_init() failed");
5ebbdab9 731
02212d8e 732 if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
579b006f 733 ftdi_error_return(-2, "libusb_get_device_list() failed");
5ebbdab9 734
579b006f
JZ
735 /* XXX: This doesn't handle symlinks/odd paths/etc... */
736 if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
99650502 737 ftdi_error_return_free_device_list(-11, "illegal description format", devs);
5ebbdab9 738
579b006f 739 while ((dev = devs[i++]) != NULL)
5ebbdab9 740 {
99650502 741 int ret;
579b006f
JZ
742 if (bus_number == libusb_get_bus_number (dev)
743 && device_address == libusb_get_device_address (dev))
99650502
UB
744 {
745 ret = ftdi_usb_open_dev(ftdi, dev);
746 libusb_free_device_list(devs,1);
747 return ret;
748 }
5ebbdab9
GE
749 }
750
751 // device not found
99650502 752 ftdi_error_return_free_device_list(-3, "device not found", devs);
5ebbdab9
GE
753 }
754 else if (description[0] == 'i' || description[0] == 's')
755 {
756 unsigned int vendor;
757 unsigned int product;
758 unsigned int index=0;
0e6cf62b 759 const char *serial=NULL;
5ebbdab9
GE
760 const char *startp, *endp;
761
762 errno=0;
763 startp=description+2;
764 vendor=strtoul((char*)startp,(char**)&endp,0);
765 if (*endp != ':' || endp == startp || errno != 0)
766 ftdi_error_return(-11, "illegal description format");
767
768 startp=endp+1;
769 product=strtoul((char*)startp,(char**)&endp,0);
770 if (endp == startp || errno != 0)
771 ftdi_error_return(-11, "illegal description format");
772
773 if (description[0] == 'i' && *endp != 0)
774 {
775 /* optional index field in i-mode */
776 if (*endp != ':')
777 ftdi_error_return(-11, "illegal description format");
778
779 startp=endp+1;
780 index=strtoul((char*)startp,(char**)&endp,0);
781 if (*endp != 0 || endp == startp || errno != 0)
782 ftdi_error_return(-11, "illegal description format");
783 }
784 if (description[0] == 's')
785 {
786 if (*endp != ':')
787 ftdi_error_return(-11, "illegal description format");
788
789 /* rest of the description is the serial */
790 serial=endp+1;
791 }
792
793 return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
794 }
795 else
796 {
797 ftdi_error_return(-11, "illegal description format");
798 }
799}
800
801/**
1941414d 802 Resets the ftdi device.
a3da1d95 803
1941414d
TJ
804 \param ftdi pointer to ftdi_context
805
806 \retval 0: all fine
807 \retval -1: FTDI reset failed
22a1b5c1 808 \retval -2: USB device unavailable
4837f98a 809*/
edb82cbf 810int ftdi_usb_reset(struct ftdi_context *ftdi)
a8f46ddc 811{
22a1b5c1
TJ
812 if (ftdi == NULL || ftdi->usb_dev == NULL)
813 ftdi_error_return(-2, "USB device unavailable");
814
579b006f
JZ
815 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
816 SIO_RESET_REQUEST, SIO_RESET_SIO,
817 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
22d12cda 818 ftdi_error_return(-1,"FTDI reset failed");
c3d95b87 819
545820ce 820 // Invalidate data in the readbuffer
bfcee05b
TJ
821 ftdi->readbuffer_offset = 0;
822 ftdi->readbuffer_remaining = 0;
823
a3da1d95
GE
824 return 0;
825}
826
1941414d 827/**
1189b11a 828 Clears the read buffer on the chip and the internal read buffer.
1941414d
TJ
829
830 \param ftdi pointer to ftdi_context
4837f98a 831
1941414d 832 \retval 0: all fine
1189b11a 833 \retval -1: read buffer purge failed
22a1b5c1 834 \retval -2: USB device unavailable
4837f98a 835*/
1189b11a 836int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
a8f46ddc 837{
22a1b5c1
TJ
838 if (ftdi == NULL || ftdi->usb_dev == NULL)
839 ftdi_error_return(-2, "USB device unavailable");
840
579b006f
JZ
841 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
842 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
843 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87
TJ
844 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
845
545820ce 846 // Invalidate data in the readbuffer
bfcee05b
TJ
847 ftdi->readbuffer_offset = 0;
848 ftdi->readbuffer_remaining = 0;
a60be878 849
1189b11a
TJ
850 return 0;
851}
852
853/**
854 Clears the write buffer on the chip.
855
856 \param ftdi pointer to ftdi_context
857
858 \retval 0: all fine
859 \retval -1: write buffer purge failed
22a1b5c1 860 \retval -2: USB device unavailable
1189b11a
TJ
861*/
862int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
863{
22a1b5c1
TJ
864 if (ftdi == NULL || ftdi->usb_dev == NULL)
865 ftdi_error_return(-2, "USB device unavailable");
866
579b006f
JZ
867 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
868 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
869 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
1189b11a
TJ
870 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
871
872 return 0;
873}
874
875/**
876 Clears the buffers on the chip and the internal read buffer.
877
878 \param ftdi pointer to ftdi_context
879
880 \retval 0: all fine
881 \retval -1: read buffer purge failed
882 \retval -2: write buffer purge failed
22a1b5c1 883 \retval -3: USB device unavailable
1189b11a
TJ
884*/
885int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
886{
887 int result;
888
22a1b5c1
TJ
889 if (ftdi == NULL || ftdi->usb_dev == NULL)
890 ftdi_error_return(-3, "USB device unavailable");
891
1189b11a 892 result = ftdi_usb_purge_rx_buffer(ftdi);
5a2b51cb 893 if (result < 0)
1189b11a
TJ
894 return -1;
895
896 result = ftdi_usb_purge_tx_buffer(ftdi);
5a2b51cb 897 if (result < 0)
1189b11a 898 return -2;
545820ce 899
a60be878
TJ
900 return 0;
901}
a3da1d95 902
f3f81007
TJ
903
904
1941414d
TJ
905/**
906 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
907
908 \param ftdi pointer to ftdi_context
909
910 \retval 0: all fine
911 \retval -1: usb_release failed
22a1b5c1 912 \retval -3: ftdi context invalid
a3da1d95 913*/
a8f46ddc
TJ
914int ftdi_usb_close(struct ftdi_context *ftdi)
915{
a3da1d95
GE
916 int rtn = 0;
917
22a1b5c1
TJ
918 if (ftdi == NULL)
919 ftdi_error_return(-3, "ftdi context invalid");
920
dff4fdb0 921 if (ftdi->usb_dev != NULL)
579b006f 922 if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
dff4fdb0 923 rtn = -1;
98452d97 924
579b006f 925 ftdi_usb_close_internal (ftdi);
98452d97 926
a3da1d95
GE
927 return rtn;
928}
929
418aaa72 930/**
53ad271d
TJ
931 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
932 Function is only used internally
b5ec1820 933 \internal
53ad271d 934*/
0126d22e 935static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
a8f46ddc
TJ
936 unsigned short *value, unsigned short *index)
937{
53ad271d
TJ
938 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
939 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
940 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
941 int divisor, best_divisor, best_baud, best_baud_diff;
942 unsigned long encoded_divisor;
943 int i;
944
22d12cda
TJ
945 if (baudrate <= 0)
946 {
53ad271d
TJ
947 // Return error
948 return -1;
949 }
950
951 divisor = 24000000 / baudrate;
952
22d12cda
TJ
953 if (ftdi->type == TYPE_AM)
954 {
53ad271d
TJ
955 // Round down to supported fraction (AM only)
956 divisor -= am_adjust_dn[divisor & 7];
957 }
958
959 // Try this divisor and the one above it (because division rounds down)
960 best_divisor = 0;
961 best_baud = 0;
962 best_baud_diff = 0;
22d12cda
TJ
963 for (i = 0; i < 2; i++)
964 {
53ad271d
TJ
965 int try_divisor = divisor + i;
966 int baud_estimate;
967 int baud_diff;
968
969 // Round up to supported divisor value
22d12cda
TJ
970 if (try_divisor <= 8)
971 {
53ad271d
TJ
972 // Round up to minimum supported divisor
973 try_divisor = 8;
22d12cda
TJ
974 }
975 else if (ftdi->type != TYPE_AM && try_divisor < 12)
976 {
53ad271d
TJ
977 // BM doesn't support divisors 9 through 11 inclusive
978 try_divisor = 12;
22d12cda
TJ
979 }
980 else if (divisor < 16)
981 {
53ad271d
TJ
982 // AM doesn't support divisors 9 through 15 inclusive
983 try_divisor = 16;
22d12cda
TJ
984 }
985 else
986 {
987 if (ftdi->type == TYPE_AM)
988 {
53ad271d
TJ
989 // Round up to supported fraction (AM only)
990 try_divisor += am_adjust_up[try_divisor & 7];
22d12cda
TJ
991 if (try_divisor > 0x1FFF8)
992 {
53ad271d
TJ
993 // Round down to maximum supported divisor value (for AM)
994 try_divisor = 0x1FFF8;
995 }
22d12cda
TJ
996 }
997 else
998 {
999 if (try_divisor > 0x1FFFF)
1000 {
53ad271d
TJ
1001 // Round down to maximum supported divisor value (for BM)
1002 try_divisor = 0x1FFFF;
1003 }
1004 }
1005 }
1006 // Get estimated baud rate (to nearest integer)
1007 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
1008 // Get absolute difference from requested baud rate
22d12cda
TJ
1009 if (baud_estimate < baudrate)
1010 {
53ad271d 1011 baud_diff = baudrate - baud_estimate;
22d12cda
TJ
1012 }
1013 else
1014 {
53ad271d
TJ
1015 baud_diff = baud_estimate - baudrate;
1016 }
22d12cda
TJ
1017 if (i == 0 || baud_diff < best_baud_diff)
1018 {
53ad271d
TJ
1019 // Closest to requested baud rate so far
1020 best_divisor = try_divisor;
1021 best_baud = baud_estimate;
1022 best_baud_diff = baud_diff;
22d12cda
TJ
1023 if (baud_diff == 0)
1024 {
53ad271d
TJ
1025 // Spot on! No point trying
1026 break;
1027 }
1028 }
1029 }
1030 // Encode the best divisor value
1031 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
1032 // Deal with special cases for encoded value
22d12cda
TJ
1033 if (encoded_divisor == 1)
1034 {
4837f98a 1035 encoded_divisor = 0; // 3000000 baud
22d12cda
TJ
1036 }
1037 else if (encoded_divisor == 0x4001)
1038 {
4837f98a 1039 encoded_divisor = 1; // 2000000 baud (BM only)
53ad271d
TJ
1040 }
1041 // Split into "value" and "index" values
1042 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1416eb14 1043 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
22d12cda 1044 {
0126d22e
TJ
1045 *index = (unsigned short)(encoded_divisor >> 8);
1046 *index &= 0xFF00;
a9c57c05 1047 *index |= ftdi->index;
0126d22e
TJ
1048 }
1049 else
1050 *index = (unsigned short)(encoded_divisor >> 16);
c3d95b87 1051
53ad271d
TJ
1052 // Return the nearest baud rate
1053 return best_baud;
1054}
1055
1941414d 1056/**
9bec2387 1057 Sets the chip baud rate
1941414d
TJ
1058
1059 \param ftdi pointer to ftdi_context
9bec2387 1060 \param baudrate baud rate to set
1941414d
TJ
1061
1062 \retval 0: all fine
1063 \retval -1: invalid baudrate
1064 \retval -2: setting baudrate failed
22a1b5c1 1065 \retval -3: USB device unavailable
a3da1d95 1066*/
a8f46ddc
TJ
1067int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
1068{
53ad271d
TJ
1069 unsigned short value, index;
1070 int actual_baudrate;
a3da1d95 1071
22a1b5c1
TJ
1072 if (ftdi == NULL || ftdi->usb_dev == NULL)
1073 ftdi_error_return(-3, "USB device unavailable");
1074
22d12cda
TJ
1075 if (ftdi->bitbang_enabled)
1076 {
a3da1d95
GE
1077 baudrate = baudrate*4;
1078 }
1079
25707904 1080 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
c3d95b87
TJ
1081 if (actual_baudrate <= 0)
1082 ftdi_error_return (-1, "Silly baudrate <= 0.");
a3da1d95 1083
53ad271d
TJ
1084 // Check within tolerance (about 5%)
1085 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
1086 || ((actual_baudrate < baudrate)
1087 ? (actual_baudrate * 21 < baudrate * 20)
c3d95b87
TJ
1088 : (baudrate * 21 < actual_baudrate * 20)))
1089 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
545820ce 1090
579b006f
JZ
1091 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1092 SIO_SET_BAUDRATE_REQUEST, value,
1093 index, NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87 1094 ftdi_error_return (-2, "Setting new baudrate failed");
a3da1d95
GE
1095
1096 ftdi->baudrate = baudrate;
1097 return 0;
1098}
1099
1941414d 1100/**
6c32e222
TJ
1101 Set (RS232) line characteristics.
1102 The break type can only be set via ftdi_set_line_property2()
1103 and defaults to "off".
4837f98a 1104
1941414d
TJ
1105 \param ftdi pointer to ftdi_context
1106 \param bits Number of bits
1107 \param sbit Number of stop bits
1108 \param parity Parity mode
1109
1110 \retval 0: all fine
1111 \retval -1: Setting line property failed
2f73e59f
TJ
1112*/
1113int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
d2f10023 1114 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
2f73e59f 1115{
6c32e222
TJ
1116 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
1117}
1118
1119/**
1120 Set (RS232) line characteristics
1121
1122 \param ftdi pointer to ftdi_context
1123 \param bits Number of bits
1124 \param sbit Number of stop bits
1125 \param parity Parity mode
1126 \param break_type Break type
1127
1128 \retval 0: all fine
1129 \retval -1: Setting line property failed
22a1b5c1 1130 \retval -2: USB device unavailable
6c32e222
TJ
1131*/
1132int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
22d12cda
TJ
1133 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
1134 enum ftdi_break_type break_type)
6c32e222 1135{
2f73e59f
TJ
1136 unsigned short value = bits;
1137
22a1b5c1
TJ
1138 if (ftdi == NULL || ftdi->usb_dev == NULL)
1139 ftdi_error_return(-2, "USB device unavailable");
1140
22d12cda
TJ
1141 switch (parity)
1142 {
1143 case NONE:
1144 value |= (0x00 << 8);
1145 break;
1146 case ODD:
1147 value |= (0x01 << 8);
1148 break;
1149 case EVEN:
1150 value |= (0x02 << 8);
1151 break;
1152 case MARK:
1153 value |= (0x03 << 8);
1154 break;
1155 case SPACE:
1156 value |= (0x04 << 8);
1157 break;
2f73e59f 1158 }
d2f10023 1159
22d12cda
TJ
1160 switch (sbit)
1161 {
1162 case STOP_BIT_1:
1163 value |= (0x00 << 11);
1164 break;
1165 case STOP_BIT_15:
1166 value |= (0x01 << 11);
1167 break;
1168 case STOP_BIT_2:
1169 value |= (0x02 << 11);
1170 break;
2f73e59f 1171 }
d2f10023 1172
22d12cda
TJ
1173 switch (break_type)
1174 {
1175 case BREAK_OFF:
1176 value |= (0x00 << 14);
1177 break;
1178 case BREAK_ON:
1179 value |= (0x01 << 14);
1180 break;
6c32e222
TJ
1181 }
1182
579b006f
JZ
1183 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1184 SIO_SET_DATA_REQUEST, value,
1185 ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
2f73e59f 1186 ftdi_error_return (-1, "Setting new line property failed");
d2f10023 1187
2f73e59f
TJ
1188 return 0;
1189}
a3da1d95 1190
1941414d
TJ
1191/**
1192 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
1193
1194 \param ftdi pointer to ftdi_context
1195 \param buf Buffer with the data
1196 \param size Size of the buffer
1197
22a1b5c1 1198 \retval -666: USB device unavailable
1941414d
TJ
1199 \retval <0: error code from usb_bulk_write()
1200 \retval >0: number of bytes written
1201*/
a8f46ddc
TJ
1202int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1203{
a3da1d95 1204 int offset = 0;
579b006f 1205 int actual_length;
c3d95b87 1206
22a1b5c1
TJ
1207 if (ftdi == NULL || ftdi->usb_dev == NULL)
1208 ftdi_error_return(-666, "USB device unavailable");
1209
22d12cda
TJ
1210 while (offset < size)
1211 {
948f9ada 1212 int write_size = ftdi->writebuffer_chunksize;
a3da1d95
GE
1213
1214 if (offset+write_size > size)
1215 write_size = size-offset;
1216
579b006f
JZ
1217 if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)
1218 ftdi_error_return(-1, "usb bulk write failed");
a3da1d95 1219
579b006f 1220 offset += actual_length;
a3da1d95
GE
1221 }
1222
579b006f 1223 return offset;
a3da1d95
GE
1224}
1225
579b006f 1226static void ftdi_read_data_cb(struct libusb_transfer *transfer)
22d12cda 1227{
579b006f
JZ
1228 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1229 struct ftdi_context *ftdi = tc->ftdi;
1230 int packet_size, actual_length, num_of_chunks, chunk_remains, i, ret;
4c9e3812 1231
b1139150 1232 packet_size = ftdi->max_packet_size;
579b006f
JZ
1233
1234 actual_length = transfer->actual_length;
1235
1236 if (actual_length > 2)
1237 {
1238 // skip FTDI status bytes.
1239 // Maybe stored in the future to enable modem use
1240 num_of_chunks = actual_length / packet_size;
1241 chunk_remains = actual_length % packet_size;
1242 //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);
1243
1244 ftdi->readbuffer_offset += 2;
1245 actual_length -= 2;
1246
1247 if (actual_length > packet_size - 2)
1248 {
1249 for (i = 1; i < num_of_chunks; i++)
1250 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1251 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1252 packet_size - 2);
1253 if (chunk_remains > 2)
1254 {
1255 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1256 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1257 chunk_remains-2);
1258 actual_length -= 2*num_of_chunks;
1259 }
1260 else
1261 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1262 }
1263
1264 if (actual_length > 0)
1265 {
1266 // data still fits in buf?
1267 if (tc->offset + actual_length <= tc->size)
1268 {
1269 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, actual_length);
1270 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
1271 tc->offset += actual_length;
1272
1273 ftdi->readbuffer_offset = 0;
1274 ftdi->readbuffer_remaining = 0;
1275
1276 /* Did we read exactly the right amount of bytes? */
1277 if (tc->offset == tc->size)
1278 {
1279 //printf("read_data exact rem %d offset %d\n",
1280 //ftdi->readbuffer_remaining, offset);
1281 tc->completed = 1;
1282 return;
1283 }
1284 }
1285 else
1286 {
1287 // only copy part of the data or size <= readbuffer_chunksize
1288 int part_size = tc->size - tc->offset;
1289 memcpy (tc->buf + tc->offset, ftdi->readbuffer + ftdi->readbuffer_offset, part_size);
1290 tc->offset += part_size;
1291
1292 ftdi->readbuffer_offset += part_size;
1293 ftdi->readbuffer_remaining = actual_length - part_size;
1294
1295 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1296 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
1297 tc->completed = 1;
1298 return;
1299 }
1300 }
1301 }
1302 ret = libusb_submit_transfer (transfer);
1303 if (ret < 0)
1304 tc->completed = 1;
1305}
1306
1307
1308static void ftdi_write_data_cb(struct libusb_transfer *transfer)
7cc9950e 1309{
579b006f
JZ
1310 struct ftdi_transfer_control *tc = (struct ftdi_transfer_control *) transfer->user_data;
1311 struct ftdi_context *ftdi = tc->ftdi;
90ef163e
YSL
1312
1313 tc->offset += transfer->actual_length;
1314
579b006f 1315 if (tc->offset == tc->size)
22d12cda 1316 {
579b006f 1317 tc->completed = 1;
7cc9950e 1318 }
579b006f
JZ
1319 else
1320 {
1321 int write_size = ftdi->writebuffer_chunksize;
1322 int ret;
7cc9950e 1323
579b006f
JZ
1324 if (tc->offset + write_size > tc->size)
1325 write_size = tc->size - tc->offset;
1326
1327 transfer->length = write_size;
1328 transfer->buffer = tc->buf + tc->offset;
1329 ret = libusb_submit_transfer (transfer);
1330 if (ret < 0)
1331 tc->completed = 1;
1332 }
7cc9950e
GE
1333}
1334
579b006f 1335
84f85aaa 1336/**
579b006f
JZ
1337 Writes data to the chip. Does not wait for completion of the transfer
1338 nor does it make sure that the transfer was successful.
1339
249888c8 1340 Use libusb 1.0 asynchronous API.
84f85aaa
GE
1341
1342 \param ftdi pointer to ftdi_context
579b006f
JZ
1343 \param buf Buffer with the data
1344 \param size Size of the buffer
84f85aaa 1345
579b006f
JZ
1346 \retval NULL: Some error happens when submit transfer
1347 \retval !NULL: Pointer to a ftdi_transfer_control
c201f80f 1348*/
579b006f
JZ
1349
1350struct ftdi_transfer_control *ftdi_write_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
7cc9950e 1351{
579b006f
JZ
1352 struct ftdi_transfer_control *tc;
1353 struct libusb_transfer *transfer = libusb_alloc_transfer(0);
1354 int write_size, ret;
22d12cda 1355
22a1b5c1
TJ
1356 if (ftdi == NULL || ftdi->usb_dev == NULL)
1357 {
1358 libusb_free_transfer(transfer);
1359 return NULL;
1360 }
1361
579b006f 1362 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
22d12cda 1363
579b006f
JZ
1364 if (!tc || !transfer)
1365 return NULL;
22d12cda 1366
579b006f
JZ
1367 tc->ftdi = ftdi;
1368 tc->completed = 0;
1369 tc->buf = buf;
1370 tc->size = size;
1371 tc->offset = 0;
7cc9950e 1372
579b006f
JZ
1373 if (size < ftdi->writebuffer_chunksize)
1374 write_size = size;
1375 else
1376 write_size = ftdi->writebuffer_chunksize;
22d12cda 1377
90ef163e
YSL
1378 libusb_fill_bulk_transfer(transfer, ftdi->usb_dev, ftdi->in_ep, buf,
1379 write_size, ftdi_write_data_cb, tc,
1380 ftdi->usb_write_timeout);
579b006f 1381 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
7cc9950e 1382
579b006f
JZ
1383 ret = libusb_submit_transfer(transfer);
1384 if (ret < 0)
1385 {
1386 libusb_free_transfer(transfer);
1387 tc->completed = 1;
1388 tc->transfer = NULL;
1389 return NULL;
7cc9950e 1390 }
579b006f
JZ
1391 tc->transfer = transfer;
1392
1393 return tc;
7cc9950e
GE
1394}
1395
1396/**
579b006f
JZ
1397 Reads data from the chip. Does not wait for completion of the transfer
1398 nor does it make sure that the transfer was successful.
1399
249888c8 1400 Use libusb 1.0 asynchronous API.
7cc9950e
GE
1401
1402 \param ftdi pointer to ftdi_context
579b006f
JZ
1403 \param buf Buffer with the data
1404 \param size Size of the buffer
4c9e3812 1405
579b006f
JZ
1406 \retval NULL: Some error happens when submit transfer
1407 \retval !NULL: Pointer to a ftdi_transfer_control
4c9e3812 1408*/
579b006f
JZ
1409
1410struct ftdi_transfer_control *ftdi_read_data_submit(struct ftdi_context *ftdi, unsigned char *buf, int size)
4c9e3812 1411{
579b006f
JZ
1412 struct ftdi_transfer_control *tc;
1413 struct libusb_transfer *transfer;
1414 int ret;
22d12cda 1415
22a1b5c1
TJ
1416 if (ftdi == NULL || ftdi->usb_dev == NULL)
1417 return NULL;
1418
579b006f
JZ
1419 tc = (struct ftdi_transfer_control *) malloc (sizeof (*tc));
1420 if (!tc)
1421 return NULL;
1422
1423 tc->ftdi = ftdi;
1424 tc->buf = buf;
1425 tc->size = size;
1426
1427 if (size <= ftdi->readbuffer_remaining)
7cc9950e 1428 {
579b006f 1429 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
7cc9950e 1430
579b006f
JZ
1431 // Fix offsets
1432 ftdi->readbuffer_remaining -= size;
1433 ftdi->readbuffer_offset += size;
7cc9950e 1434
579b006f 1435 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
22d12cda 1436
579b006f
JZ
1437 tc->completed = 1;
1438 tc->offset = size;
1439 tc->transfer = NULL;
1440 return tc;
1441 }
4c9e3812 1442
579b006f
JZ
1443 tc->completed = 0;
1444 if (ftdi->readbuffer_remaining != 0)
1445 {
1446 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
22d12cda 1447
579b006f
JZ
1448 tc->offset = ftdi->readbuffer_remaining;
1449 }
1450 else
1451 tc->offset = 0;
22d12cda 1452
579b006f
JZ
1453 transfer = libusb_alloc_transfer(0);
1454 if (!transfer)
1455 {
1456 free (tc);
1457 return NULL;
1458 }
22d12cda 1459
579b006f
JZ
1460 ftdi->readbuffer_remaining = 0;
1461 ftdi->readbuffer_offset = 0;
1462
1463 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);
1464 transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
1465
1466 ret = libusb_submit_transfer(transfer);
1467 if (ret < 0)
1468 {
1469 libusb_free_transfer(transfer);
1470 free (tc);
1471 return NULL;
22d12cda 1472 }
579b006f
JZ
1473 tc->transfer = transfer;
1474
1475 return tc;
4c9e3812
GE
1476}
1477
1478/**
579b006f 1479 Wait for completion of the transfer.
4c9e3812 1480
249888c8 1481 Use libusb 1.0 asynchronous API.
4c9e3812 1482
579b006f 1483 \param tc pointer to ftdi_transfer_control
4c9e3812 1484
579b006f
JZ
1485 \retval < 0: Some error happens
1486 \retval >= 0: Data size transferred
4c9e3812 1487*/
579b006f
JZ
1488
1489int ftdi_transfer_data_done(struct ftdi_transfer_control *tc)
4c9e3812
GE
1490{
1491 int ret;
4c9e3812 1492
579b006f 1493 while (!tc->completed)
22d12cda 1494 {
29b1dfd9 1495 ret = libusb_handle_events(tc->ftdi->usb_ctx);
4c9e3812 1496 if (ret < 0)
579b006f
JZ
1497 {
1498 if (ret == LIBUSB_ERROR_INTERRUPTED)
1499 continue;
1500 libusb_cancel_transfer(tc->transfer);
1501 while (!tc->completed)
29b1dfd9 1502 if (libusb_handle_events(tc->ftdi->usb_ctx) < 0)
579b006f
JZ
1503 break;
1504 libusb_free_transfer(tc->transfer);
1505 free (tc);
579b006f
JZ
1506 return ret;
1507 }
4c9e3812
GE
1508 }
1509
90ef163e
YSL
1510 ret = tc->offset;
1511 /**
1512 * tc->transfer could be NULL if "(size <= ftdi->readbuffer_remaining)"
ef15fab5 1513 * at ftdi_read_data_submit(). Therefore, we need to check it here.
90ef163e 1514 **/
ef15fab5
TJ
1515 if (tc->transfer)
1516 {
1517 if (tc->transfer->status != LIBUSB_TRANSFER_COMPLETED)
1518 ret = -1;
1519 libusb_free_transfer(tc->transfer);
90ef163e 1520 }
579b006f
JZ
1521 free(tc);
1522 return ret;
4c9e3812 1523}
579b006f 1524
1941414d
TJ
1525/**
1526 Configure write buffer chunk size.
1527 Default is 4096.
1528
1529 \param ftdi pointer to ftdi_context
1530 \param chunksize Chunk size
a3da1d95 1531
1941414d 1532 \retval 0: all fine
22a1b5c1 1533 \retval -1: ftdi context invalid
1941414d 1534*/
a8f46ddc
TJ
1535int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1536{
22a1b5c1
TJ
1537 if (ftdi == NULL)
1538 ftdi_error_return(-1, "ftdi context invalid");
1539
948f9ada
TJ
1540 ftdi->writebuffer_chunksize = chunksize;
1541 return 0;
1542}
1543
1941414d
TJ
1544/**
1545 Get write buffer chunk size.
1546
1547 \param ftdi pointer to ftdi_context
1548 \param chunksize Pointer to store chunk size in
948f9ada 1549
1941414d 1550 \retval 0: all fine
22a1b5c1 1551 \retval -1: ftdi context invalid
1941414d 1552*/
a8f46ddc
TJ
1553int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1554{
22a1b5c1
TJ
1555 if (ftdi == NULL)
1556 ftdi_error_return(-1, "ftdi context invalid");
1557
948f9ada
TJ
1558 *chunksize = ftdi->writebuffer_chunksize;
1559 return 0;
1560}
cbabb7d3 1561
1941414d
TJ
1562/**
1563 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1564
1565 Automatically strips the two modem status bytes transfered during every read.
948f9ada 1566
1941414d
TJ
1567 \param ftdi pointer to ftdi_context
1568 \param buf Buffer to store data in
1569 \param size Size of the buffer
1570
22a1b5c1 1571 \retval -666: USB device unavailable
579b006f 1572 \retval <0: error code from libusb_bulk_transfer()
d77b0e94 1573 \retval 0: no data was available
1941414d
TJ
1574 \retval >0: number of bytes read
1575
1941414d 1576*/
a8f46ddc
TJ
1577int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1578{
579b006f 1579 int offset = 0, ret, i, num_of_chunks, chunk_remains;
e2f12a4f 1580 int packet_size = ftdi->max_packet_size;
579b006f 1581 int actual_length = 1;
f2f00cb5 1582
22a1b5c1
TJ
1583 if (ftdi == NULL || ftdi->usb_dev == NULL)
1584 ftdi_error_return(-666, "USB device unavailable");
1585
e2f12a4f
TJ
1586 // Packet size sanity check (avoid division by zero)
1587 if (packet_size == 0)
1588 ftdi_error_return(-1, "max_packet_size is bogus (zero)");
d9f0cce7 1589
948f9ada 1590 // everything we want is still in the readbuffer?
22d12cda
TJ
1591 if (size <= ftdi->readbuffer_remaining)
1592 {
d9f0cce7
TJ
1593 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1594
1595 // Fix offsets
1596 ftdi->readbuffer_remaining -= size;
1597 ftdi->readbuffer_offset += size;
1598
545820ce 1599 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
1600
1601 return size;
979a145c 1602 }
948f9ada 1603 // something still in the readbuffer, but not enough to satisfy 'size'?
22d12cda
TJ
1604 if (ftdi->readbuffer_remaining != 0)
1605 {
d9f0cce7 1606 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
979a145c 1607
d9f0cce7
TJ
1608 // Fix offset
1609 offset += ftdi->readbuffer_remaining;
948f9ada 1610 }
948f9ada 1611 // do the actual USB read
579b006f 1612 while (offset < size && actual_length > 0)
22d12cda 1613 {
d9f0cce7
TJ
1614 ftdi->readbuffer_remaining = 0;
1615 ftdi->readbuffer_offset = 0;
98452d97 1616 /* returns how much received */
579b006f 1617 ret = libusb_bulk_transfer (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, &actual_length, ftdi->usb_read_timeout);
c3d95b87
TJ
1618 if (ret < 0)
1619 ftdi_error_return(ret, "usb bulk read failed");
98452d97 1620
579b006f 1621 if (actual_length > 2)
22d12cda 1622 {
d9f0cce7
TJ
1623 // skip FTDI status bytes.
1624 // Maybe stored in the future to enable modem use
579b006f
JZ
1625 num_of_chunks = actual_length / packet_size;
1626 chunk_remains = actual_length % packet_size;
1627 //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 1628
d9f0cce7 1629 ftdi->readbuffer_offset += 2;
579b006f 1630 actual_length -= 2;
1c733d33 1631
579b006f 1632 if (actual_length > packet_size - 2)
22d12cda 1633 {
1c733d33 1634 for (i = 1; i < num_of_chunks; i++)
f2f00cb5
DC
1635 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1636 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1637 packet_size - 2);
22d12cda
TJ
1638 if (chunk_remains > 2)
1639 {
f2f00cb5
DC
1640 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1641 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1c733d33 1642 chunk_remains-2);
579b006f 1643 actual_length -= 2*num_of_chunks;
22d12cda
TJ
1644 }
1645 else
579b006f 1646 actual_length -= 2*(num_of_chunks-1)+chunk_remains;
1c733d33 1647 }
22d12cda 1648 }
579b006f 1649 else if (actual_length <= 2)
22d12cda 1650 {
d9f0cce7
TJ
1651 // no more data to read?
1652 return offset;
1653 }
579b006f 1654 if (actual_length > 0)
22d12cda 1655 {
d9f0cce7 1656 // data still fits in buf?
579b006f 1657 if (offset+actual_length <= size)
22d12cda 1658 {
579b006f 1659 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, actual_length);
545820ce 1660 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
579b006f 1661 offset += actual_length;
d9f0cce7 1662
53ad271d 1663 /* Did we read exactly the right amount of bytes? */
d9f0cce7 1664 if (offset == size)
c4446c36
TJ
1665 //printf("read_data exact rem %d offset %d\n",
1666 //ftdi->readbuffer_remaining, offset);
d9f0cce7 1667 return offset;
22d12cda
TJ
1668 }
1669 else
1670 {
d9f0cce7
TJ
1671 // only copy part of the data or size <= readbuffer_chunksize
1672 int part_size = size-offset;
1673 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
98452d97 1674
d9f0cce7 1675 ftdi->readbuffer_offset += part_size;
579b006f 1676 ftdi->readbuffer_remaining = actual_length-part_size;
d9f0cce7
TJ
1677 offset += part_size;
1678
579b006f
JZ
1679 /* printf("Returning part: %d - size: %d - offset: %d - actual_length: %d - remaining: %d\n",
1680 part_size, size, offset, actual_length, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
1681
1682 return offset;
1683 }
1684 }
cbabb7d3 1685 }
948f9ada 1686 // never reached
29c4af7f 1687 return -127;
a3da1d95
GE
1688}
1689
1941414d
TJ
1690/**
1691 Configure read buffer chunk size.
1692 Default is 4096.
1693
1694 Automatically reallocates the buffer.
a3da1d95 1695
1941414d
TJ
1696 \param ftdi pointer to ftdi_context
1697 \param chunksize Chunk size
1698
1699 \retval 0: all fine
22a1b5c1 1700 \retval -1: ftdi context invalid
1941414d 1701*/
a8f46ddc
TJ
1702int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1703{
29c4af7f
TJ
1704 unsigned char *new_buf;
1705
22a1b5c1
TJ
1706 if (ftdi == NULL)
1707 ftdi_error_return(-1, "ftdi context invalid");
1708
948f9ada
TJ
1709 // Invalidate all remaining data
1710 ftdi->readbuffer_offset = 0;
1711 ftdi->readbuffer_remaining = 0;
8de6eea4
JZ
1712#ifdef __linux__
1713 /* We can't set readbuffer_chunksize larger than MAX_BULK_BUFFER_LENGTH,
1714 which is defined in libusb-1.0. Otherwise, each USB read request will
2e685a1f 1715 be divided into multiple URBs. This will cause issues on Linux kernel
8de6eea4
JZ
1716 older than 2.6.32. */
1717 if (chunksize > 16384)
1718 chunksize = 16384;
1719#endif
948f9ada 1720
c3d95b87
TJ
1721 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1722 ftdi_error_return(-1, "out of memory for readbuffer");
d9f0cce7 1723
948f9ada
TJ
1724 ftdi->readbuffer = new_buf;
1725 ftdi->readbuffer_chunksize = chunksize;
1726
1727 return 0;
1728}
1729
1941414d
TJ
1730/**
1731 Get read buffer chunk size.
948f9ada 1732
1941414d
TJ
1733 \param ftdi pointer to ftdi_context
1734 \param chunksize Pointer to store chunk size in
1735
1736 \retval 0: all fine
22a1b5c1 1737 \retval -1: FTDI context invalid
1941414d 1738*/
a8f46ddc
TJ
1739int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1740{
22a1b5c1
TJ
1741 if (ftdi == NULL)
1742 ftdi_error_return(-1, "FTDI context invalid");
1743
948f9ada
TJ
1744 *chunksize = ftdi->readbuffer_chunksize;
1745 return 0;
1746}
1747
1748
1941414d
TJ
1749/**
1750 Enable bitbang mode.
948f9ada 1751
fd282db3 1752 \deprecated use \ref ftdi_set_bitmode with mode BITMODE_BITBANG instead
1941414d
TJ
1753
1754 \param ftdi pointer to ftdi_context
1755 \param bitmask Bitmask to configure lines.
1756 HIGH/ON value configures a line as output.
1757
1758 \retval 0: all fine
1759 \retval -1: can't enable bitbang mode
22a1b5c1 1760 \retval -2: USB device unavailable
1941414d 1761*/
a8f46ddc
TJ
1762int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1763{
a3da1d95
GE
1764 unsigned short usb_val;
1765
22a1b5c1
TJ
1766 if (ftdi == NULL || ftdi->usb_dev == NULL)
1767 ftdi_error_return(-2, "USB device unavailable");
1768
d9f0cce7 1769 usb_val = bitmask; // low byte: bitmask
3119537f
TJ
1770 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1771 usb_val |= (ftdi->bitbang_mode << 8);
1772
579b006f
JZ
1773 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1774 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
1775 NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87
TJ
1776 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1777
a3da1d95
GE
1778 ftdi->bitbang_enabled = 1;
1779 return 0;
1780}
1781
1941414d
TJ
1782/**
1783 Disable bitbang mode.
a3da1d95 1784
1941414d
TJ
1785 \param ftdi pointer to ftdi_context
1786
1787 \retval 0: all fine
1788 \retval -1: can't disable bitbang mode
22a1b5c1 1789 \retval -2: USB device unavailable
1941414d 1790*/
a8f46ddc
TJ
1791int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1792{
22a1b5c1
TJ
1793 if (ftdi == NULL || ftdi->usb_dev == NULL)
1794 ftdi_error_return(-2, "USB device unavailable");
1795
579b006f 1796 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 1797 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
a3da1d95
GE
1798
1799 ftdi->bitbang_enabled = 0;
1800 return 0;
1801}
1802
1941414d 1803/**
418aaa72 1804 Enable/disable bitbang modes.
a3da1d95 1805
1941414d
TJ
1806 \param ftdi pointer to ftdi_context
1807 \param bitmask Bitmask to configure lines.
1808 HIGH/ON value configures a line as output.
fd282db3 1809 \param mode Bitbang mode: use the values defined in \ref ftdi_mpsse_mode
1941414d
TJ
1810
1811 \retval 0: all fine
1812 \retval -1: can't enable bitbang mode
22a1b5c1 1813 \retval -2: USB device unavailable
1941414d 1814*/
c4446c36
TJ
1815int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1816{
1817 unsigned short usb_val;
1818
22a1b5c1
TJ
1819 if (ftdi == NULL || ftdi->usb_dev == NULL)
1820 ftdi_error_return(-2, "USB device unavailable");
1821
c4446c36
TJ
1822 usb_val = bitmask; // low byte: bitmask
1823 usb_val |= (mode << 8);
579b006f
JZ
1824 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)
1825 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
c4446c36
TJ
1826
1827 ftdi->bitbang_mode = mode;
418aaa72 1828 ftdi->bitbang_enabled = (mode == BITMODE_RESET) ? 0 : 1;
c4446c36
TJ
1829 return 0;
1830}
1831
1941414d 1832/**
418aaa72 1833 Directly read pin state, circumventing the read buffer. Useful for bitbang mode.
1941414d
TJ
1834
1835 \param ftdi pointer to ftdi_context
1836 \param pins Pointer to store pins into
1837
1838 \retval 0: all fine
1839 \retval -1: read pins failed
22a1b5c1 1840 \retval -2: USB device unavailable
1941414d 1841*/
a8f46ddc
TJ
1842int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1843{
22a1b5c1
TJ
1844 if (ftdi == NULL || ftdi->usb_dev == NULL)
1845 ftdi_error_return(-2, "USB device unavailable");
1846
579b006f 1847 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 1848 ftdi_error_return(-1, "read pins failed");
a3da1d95 1849
a3da1d95
GE
1850 return 0;
1851}
1852
1941414d
TJ
1853/**
1854 Set latency timer
1855
1856 The FTDI chip keeps data in the internal buffer for a specific
1857 amount of time if the buffer is not full yet to decrease
1858 load on the usb bus.
a3da1d95 1859
1941414d
TJ
1860 \param ftdi pointer to ftdi_context
1861 \param latency Value between 1 and 255
1862
1863 \retval 0: all fine
1864 \retval -1: latency out of range
1865 \retval -2: unable to set latency timer
22a1b5c1 1866 \retval -3: USB device unavailable
1941414d 1867*/
a8f46ddc
TJ
1868int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1869{
a3da1d95
GE
1870 unsigned short usb_val;
1871
c3d95b87
TJ
1872 if (latency < 1)
1873 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
a3da1d95 1874
22a1b5c1
TJ
1875 if (ftdi == NULL || ftdi->usb_dev == NULL)
1876 ftdi_error_return(-3, "USB device unavailable");
1877
d79d2e68 1878 usb_val = latency;
579b006f 1879 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
1880 ftdi_error_return(-2, "unable to set latency timer");
1881
a3da1d95
GE
1882 return 0;
1883}
1884
1941414d
TJ
1885/**
1886 Get latency timer
a3da1d95 1887
1941414d
TJ
1888 \param ftdi pointer to ftdi_context
1889 \param latency Pointer to store latency value in
1890
1891 \retval 0: all fine
1892 \retval -1: unable to get latency timer
22a1b5c1 1893 \retval -2: USB device unavailable
1941414d 1894*/
a8f46ddc
TJ
1895int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1896{
a3da1d95 1897 unsigned short usb_val;
22a1b5c1
TJ
1898
1899 if (ftdi == NULL || ftdi->usb_dev == NULL)
1900 ftdi_error_return(-2, "USB device unavailable");
1901
579b006f 1902 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 1903 ftdi_error_return(-1, "reading latency timer failed");
a3da1d95
GE
1904
1905 *latency = (unsigned char)usb_val;
1906 return 0;
1907}
1908
1941414d 1909/**
1189b11a
TJ
1910 Poll modem status information
1911
1912 This function allows the retrieve the two status bytes of the device.
1913 The device sends these bytes also as a header for each read access
1914 where they are discarded by ftdi_read_data(). The chip generates
1915 the two stripped status bytes in the absence of data every 40 ms.
1916
1917 Layout of the first byte:
1918 - B0..B3 - must be 0
1919 - B4 Clear to send (CTS)
1920 0 = inactive
1921 1 = active
1922 - B5 Data set ready (DTS)
1923 0 = inactive
1924 1 = active
1925 - B6 Ring indicator (RI)
1926 0 = inactive
1927 1 = active
1928 - B7 Receive line signal detect (RLSD)
1929 0 = inactive
1930 1 = active
1931
1932 Layout of the second byte:
1933 - B0 Data ready (DR)
1934 - B1 Overrun error (OE)
1935 - B2 Parity error (PE)
1936 - B3 Framing error (FE)
1937 - B4 Break interrupt (BI)
1938 - B5 Transmitter holding register (THRE)
1939 - B6 Transmitter empty (TEMT)
1940 - B7 Error in RCVR FIFO
1941
1942 \param ftdi pointer to ftdi_context
1943 \param status Pointer to store status information in. Must be two bytes.
1944
1945 \retval 0: all fine
1946 \retval -1: unable to retrieve status information
22a1b5c1 1947 \retval -2: USB device unavailable
1189b11a
TJ
1948*/
1949int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1950{
1951 char usb_val[2];
1952
22a1b5c1
TJ
1953 if (ftdi == NULL || ftdi->usb_dev == NULL)
1954 ftdi_error_return(-2, "USB device unavailable");
1955
579b006f 1956 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
1957 ftdi_error_return(-1, "getting modem status failed");
1958
1959 *status = (usb_val[1] << 8) | usb_val[0];
1960
1961 return 0;
1962}
1963
a7fb8440
TJ
1964/**
1965 Set flowcontrol for ftdi chip
1966
1967 \param ftdi pointer to ftdi_context
22d12cda
TJ
1968 \param flowctrl flow control to use. should be
1969 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
a7fb8440
TJ
1970
1971 \retval 0: all fine
1972 \retval -1: set flow control failed
22a1b5c1 1973 \retval -2: USB device unavailable
a7fb8440
TJ
1974*/
1975int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1976{
22a1b5c1
TJ
1977 if (ftdi == NULL || ftdi->usb_dev == NULL)
1978 ftdi_error_return(-2, "USB device unavailable");
1979
579b006f
JZ
1980 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1981 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
1982 NULL, 0, ftdi->usb_write_timeout) < 0)
a7fb8440
TJ
1983 ftdi_error_return(-1, "set flow control failed");
1984
1985 return 0;
1986}
1987
1988/**
1989 Set dtr line
1990
1991 \param ftdi pointer to ftdi_context
1992 \param state state to set line to (1 or 0)
1993
1994 \retval 0: all fine
1995 \retval -1: set dtr failed
22a1b5c1 1996 \retval -2: USB device unavailable
a7fb8440
TJ
1997*/
1998int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1999{
2000 unsigned short usb_val;
2001
22a1b5c1
TJ
2002 if (ftdi == NULL || ftdi->usb_dev == NULL)
2003 ftdi_error_return(-2, "USB device unavailable");
2004
a7fb8440
TJ
2005 if (state)
2006 usb_val = SIO_SET_DTR_HIGH;
2007 else
2008 usb_val = SIO_SET_DTR_LOW;
2009
579b006f
JZ
2010 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2011 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2012 NULL, 0, ftdi->usb_write_timeout) < 0)
a7fb8440
TJ
2013 ftdi_error_return(-1, "set dtr failed");
2014
2015 return 0;
2016}
2017
2018/**
2019 Set rts line
2020
2021 \param ftdi pointer to ftdi_context
2022 \param state state to set line to (1 or 0)
2023
2024 \retval 0: all fine
22a1b5c1
TJ
2025 \retval -1: set rts failed
2026 \retval -2: USB device unavailable
a7fb8440
TJ
2027*/
2028int ftdi_setrts(struct ftdi_context *ftdi, int state)
2029{
2030 unsigned short usb_val;
2031
22a1b5c1
TJ
2032 if (ftdi == NULL || ftdi->usb_dev == NULL)
2033 ftdi_error_return(-2, "USB device unavailable");
2034
a7fb8440
TJ
2035 if (state)
2036 usb_val = SIO_SET_RTS_HIGH;
2037 else
2038 usb_val = SIO_SET_RTS_LOW;
2039
579b006f
JZ
2040 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2041 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2042 NULL, 0, ftdi->usb_write_timeout) < 0)
a7fb8440
TJ
2043 ftdi_error_return(-1, "set of rts failed");
2044
2045 return 0;
2046}
2047
1189b11a 2048/**
22a1b5c1 2049 Set dtr and rts line in one pass
9ecfef2a 2050
22a1b5c1
TJ
2051 \param ftdi pointer to ftdi_context
2052 \param dtr DTR state to set line to (1 or 0)
2053 \param rts RTS state to set line to (1 or 0)
9ecfef2a 2054
22a1b5c1
TJ
2055 \retval 0: all fine
2056 \retval -1: set dtr/rts failed
2057 \retval -2: USB device unavailable
9ecfef2a
TJ
2058 */
2059int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
2060{
2061 unsigned short usb_val;
2062
22a1b5c1
TJ
2063 if (ftdi == NULL || ftdi->usb_dev == NULL)
2064 ftdi_error_return(-2, "USB device unavailable");
2065
9ecfef2a 2066 if (dtr)
22d12cda 2067 usb_val = SIO_SET_DTR_HIGH;
9ecfef2a 2068 else
22d12cda 2069 usb_val = SIO_SET_DTR_LOW;
9ecfef2a
TJ
2070
2071 if (rts)
22d12cda 2072 usb_val |= SIO_SET_RTS_HIGH;
9ecfef2a 2073 else
22d12cda 2074 usb_val |= SIO_SET_RTS_LOW;
9ecfef2a 2075
579b006f
JZ
2076 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2077 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
2078 NULL, 0, ftdi->usb_write_timeout) < 0)
22d12cda 2079 ftdi_error_return(-1, "set of rts/dtr failed");
9ecfef2a
TJ
2080
2081 return 0;
2082}
2083
2084/**
1189b11a
TJ
2085 Set the special event character
2086
2087 \param ftdi pointer to ftdi_context
2088 \param eventch Event character
2089 \param enable 0 to disable the event character, non-zero otherwise
2090
2091 \retval 0: all fine
2092 \retval -1: unable to set event character
22a1b5c1 2093 \retval -2: USB device unavailable
1189b11a
TJ
2094*/
2095int ftdi_set_event_char(struct ftdi_context *ftdi,
22d12cda 2096 unsigned char eventch, unsigned char enable)
1189b11a
TJ
2097{
2098 unsigned short usb_val;
2099
22a1b5c1
TJ
2100 if (ftdi == NULL || ftdi->usb_dev == NULL)
2101 ftdi_error_return(-2, "USB device unavailable");
2102
1189b11a
TJ
2103 usb_val = eventch;
2104 if (enable)
2105 usb_val |= 1 << 8;
2106
579b006f 2107 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
2108 ftdi_error_return(-1, "setting event character failed");
2109
2110 return 0;
2111}
2112
2113/**
2114 Set error character
2115
2116 \param ftdi pointer to ftdi_context
2117 \param errorch Error character
2118 \param enable 0 to disable the error character, non-zero otherwise
2119
2120 \retval 0: all fine
2121 \retval -1: unable to set error character
22a1b5c1 2122 \retval -2: USB device unavailable
1189b11a
TJ
2123*/
2124int ftdi_set_error_char(struct ftdi_context *ftdi,
22d12cda 2125 unsigned char errorch, unsigned char enable)
1189b11a
TJ
2126{
2127 unsigned short usb_val;
2128
22a1b5c1
TJ
2129 if (ftdi == NULL || ftdi->usb_dev == NULL)
2130 ftdi_error_return(-2, "USB device unavailable");
2131
1189b11a
TJ
2132 usb_val = errorch;
2133 if (enable)
2134 usb_val |= 1 << 8;
2135
579b006f 2136 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
2137 ftdi_error_return(-1, "setting error character failed");
2138
2139 return 0;
2140}
2141
2142/**
c201f80f
TJ
2143 Set the eeprom size
2144
2145 \param ftdi pointer to ftdi_context
2146 \param eeprom Pointer to ftdi_eeprom
2147 \param size
2148
2149*/
2150void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
2151{
22a1b5c1
TJ
2152 if (ftdi == NULL)
2153 return;
2154
22d12cda
TJ
2155 ftdi->eeprom_size=size;
2156 eeprom->size=size;
c201f80f
TJ
2157}
2158
2159/**
1941414d 2160 Init eeprom with default values.
a3da1d95 2161
1941414d
TJ
2162 \param eeprom Pointer to ftdi_eeprom
2163*/
a8f46ddc
TJ
2164void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
2165{
f505134f
HK
2166 int i;
2167
22a1b5c1
TJ
2168 if (eeprom == NULL)
2169 return;
2170
f396dbad
TJ
2171 eeprom->vendor_id = 0x0403;
2172 eeprom->product_id = 0x6001;
d9f0cce7 2173
b8aa7b35
TJ
2174 eeprom->self_powered = 1;
2175 eeprom->remote_wakeup = 1;
f505134f 2176 eeprom->chip_type = TYPE_BM;
d9f0cce7 2177
b8aa7b35
TJ
2178 eeprom->in_is_isochronous = 0;
2179 eeprom->out_is_isochronous = 0;
2180 eeprom->suspend_pull_downs = 0;
d9f0cce7 2181
b8aa7b35
TJ
2182 eeprom->use_serial = 0;
2183 eeprom->change_usb_version = 0;
f396dbad 2184 eeprom->usb_version = 0x0200;
b8aa7b35 2185 eeprom->max_power = 0;
d9f0cce7 2186
b8aa7b35
TJ
2187 eeprom->manufacturer = NULL;
2188 eeprom->product = NULL;
2189 eeprom->serial = NULL;
f505134f
HK
2190 for (i=0; i < 5; i++)
2191 {
2192 eeprom->cbus_function[i] = 0;
2193 }
2194 eeprom->high_current = 0;
2195 eeprom->invert = 0;
c201f80f
TJ
2196
2197 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
b8aa7b35
TJ
2198}
2199
1941414d 2200/**
95ac1ca2
WH
2201 Frees allocated memory in eeprom.
2202
2203 \param eeprom Pointer to ftdi_eeprom
2204*/
2205void ftdi_eeprom_free(struct ftdi_eeprom *eeprom)
2206{
2207 if (eeprom->manufacturer != 0) {
2208 free(eeprom->manufacturer);
2209 eeprom->manufacturer = 0;
2210 }
2211 if (eeprom->product != 0) {
2212 free(eeprom->product);
2213 eeprom->product = 0;
2214 }
2215 if (eeprom->serial != 0) {
2216 free(eeprom->serial);
2217 eeprom->serial = 0;
2218 }
2219}
2220
2221/**
22a1b5c1
TJ
2222 Build binary output from ftdi_eeprom structure.
2223 Output is suitable for ftdi_write_eeprom().
b8aa7b35 2224
f505134f 2225 \note This function doesn't handle FT2232x devices. Only FT232x.
22a1b5c1
TJ
2226 \param eeprom Pointer to ftdi_eeprom
2227 \param output Buffer of 128 bytes to store eeprom image to
1941414d 2228
f505134f 2229 \retval >0: free eeprom size
22a1b5c1
TJ
2230 \retval -1: eeprom size (128 bytes) exceeded by custom strings
2231 \retval -2: Invalid eeprom pointer
f505134f
HK
2232 \retval -3: Invalid cbus function setting
2233 \retval -4: Chip doesn't support invert
2234 \retval -5: Chip doesn't support high current drive
b8aa7b35 2235*/
a8f46ddc
TJ
2236int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
2237{
b8aa7b35
TJ
2238 unsigned char i, j;
2239 unsigned short checksum, value;
2240 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
2241 int size_check;
f505134f 2242 const int cbus_max[5] = {13, 13, 13, 13, 9};
b8aa7b35 2243
22a1b5c1
TJ
2244 if (eeprom == NULL)
2245 return -2;
2246
b8aa7b35 2247 if (eeprom->manufacturer != NULL)
d9f0cce7 2248 manufacturer_size = strlen(eeprom->manufacturer);
b8aa7b35 2249 if (eeprom->product != NULL)
d9f0cce7 2250 product_size = strlen(eeprom->product);
b8aa7b35 2251 if (eeprom->serial != NULL)
d9f0cce7 2252 serial_size = strlen(eeprom->serial);
b8aa7b35 2253
f505134f
HK
2254 // highest allowed cbus value
2255 for (i = 0; i < 5; i++)
2256 {
2257 if ((eeprom->cbus_function[i] > cbus_max[i]) ||
2258 (eeprom->cbus_function[i] && eeprom->chip_type != TYPE_R)) return -3;
2259 }
2260 if (eeprom->chip_type != TYPE_R)
2261 {
2262 if (eeprom->invert) return -4;
2263 if (eeprom->high_current) return -5;
2264 }
2265
c201f80f 2266 size_check = eeprom->size;
d9f0cce7 2267 size_check -= 28; // 28 are always in use (fixed)
c201f80f 2268
22d12cda 2269 // Top half of a 256byte eeprom is used just for strings and checksum
c201f80f
TJ
2270 // it seems that the FTDI chip will not read these strings from the lower half
2271 // Each string starts with two bytes; offset and type (0x03 for string)
2272 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
f505134f 2273 if (eeprom->size>=256) size_check = 120;
b8aa7b35
TJ
2274 size_check -= manufacturer_size*2;
2275 size_check -= product_size*2;
2276 size_check -= serial_size*2;
2277
2278 // eeprom size exceeded?
2279 if (size_check < 0)
d9f0cce7 2280 return (-1);
b8aa7b35
TJ
2281
2282 // empty eeprom
c201f80f 2283 memset (output, 0, eeprom->size);
b8aa7b35 2284
f505134f
HK
2285 // Addr 00: High current IO
2286 output[0x00] = eeprom->high_current ? HIGH_CURRENT_DRIVE : 0;
2287 // Addr 01: IN endpoint size (for R type devices, different for FT2232)
2288 if (eeprom->chip_type == TYPE_R) {
2289 output[0x01] = 0x40;
2290 }
b8aa7b35
TJ
2291 // Addr 02: Vendor ID
2292 output[0x02] = eeprom->vendor_id;
2293 output[0x03] = eeprom->vendor_id >> 8;
2294
2295 // Addr 04: Product ID
2296 output[0x04] = eeprom->product_id;
2297 output[0x05] = eeprom->product_id >> 8;
2298
2299 // Addr 06: Device release number (0400h for BM features)
2300 output[0x06] = 0x00;
f505134f
HK
2301 switch (eeprom->chip_type) {
2302 case TYPE_AM:
2303 output[0x07] = 0x02;
2304 break;
2305 case TYPE_BM:
2306 output[0x07] = 0x04;
2307 break;
2308 case TYPE_2232C:
2309 output[0x07] = 0x05;
2310 break;
2311 case TYPE_R:
2312 output[0x07] = 0x06;
2313 break;
2314 default:
2315 output[0x07] = 0x00;
2316 }
b8aa7b35
TJ
2317
2318 // Addr 08: Config descriptor
8fae3e8e
TJ
2319 // Bit 7: always 1
2320 // Bit 6: 1 if this device is self powered, 0 if bus powered
2321 // Bit 5: 1 if this device uses remote wakeup
2322 // Bit 4: 1 if this device is battery powered
5a1dcd55 2323 j = 0x80;
b8aa7b35 2324 if (eeprom->self_powered == 1)
5a1dcd55 2325 j |= 0x40;
b8aa7b35 2326 if (eeprom->remote_wakeup == 1)
5a1dcd55 2327 j |= 0x20;
b8aa7b35
TJ
2328 output[0x08] = j;
2329
2330 // Addr 09: Max power consumption: max power = value * 2 mA
d9f0cce7 2331 output[0x09] = eeprom->max_power;
d9f0cce7 2332
b8aa7b35
TJ
2333 // Addr 0A: Chip configuration
2334 // Bit 7: 0 - reserved
2335 // Bit 6: 0 - reserved
2336 // Bit 5: 0 - reserved
2337 // Bit 4: 1 - Change USB version
2338 // Bit 3: 1 - Use the serial number string
2339 // Bit 2: 1 - Enable suspend pull downs for lower power
2340 // Bit 1: 1 - Out EndPoint is Isochronous
2341 // Bit 0: 1 - In EndPoint is Isochronous
2342 //
2343 j = 0;
2344 if (eeprom->in_is_isochronous == 1)
d9f0cce7 2345 j = j | 1;
b8aa7b35 2346 if (eeprom->out_is_isochronous == 1)
d9f0cce7 2347 j = j | 2;
b8aa7b35 2348 if (eeprom->suspend_pull_downs == 1)
d9f0cce7 2349 j = j | 4;
b8aa7b35 2350 if (eeprom->use_serial == 1)
d9f0cce7 2351 j = j | 8;
b8aa7b35 2352 if (eeprom->change_usb_version == 1)
d9f0cce7 2353 j = j | 16;
b8aa7b35 2354 output[0x0A] = j;
d9f0cce7 2355
f505134f
HK
2356 // Addr 0B: Invert data lines
2357 output[0x0B] = eeprom->invert & 0xff;
d9f0cce7 2358
b8aa7b35
TJ
2359 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2360 // Addr 0D: USB version high byte when 0x0A bit 4 is set
22d12cda
TJ
2361 if (eeprom->change_usb_version == 1)
2362 {
b8aa7b35 2363 output[0x0C] = eeprom->usb_version;
d9f0cce7 2364 output[0x0D] = eeprom->usb_version >> 8;
b8aa7b35
TJ
2365 }
2366
2367
c201f80f 2368 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
b8aa7b35
TJ
2369 // Addr 0F: Length of manufacturer string
2370 output[0x0F] = manufacturer_size*2 + 2;
2371
2372 // Addr 10: Offset of the product string + 0x80, calculated later
2373 // Addr 11: Length of product string
2374 output[0x11] = product_size*2 + 2;
2375
2376 // Addr 12: Offset of the serial string + 0x80, calculated later
2377 // Addr 13: Length of serial string
2378 output[0x13] = serial_size*2 + 2;
2379
f505134f
HK
2380 // Addr 14: CBUS function: CBUS0, CBUS1
2381 // Addr 15: CBUS function: CBUS2, CBUS3
2382 // Addr 16: CBUS function: CBUS5
2383 output[0x14] = eeprom->cbus_function[0] | (eeprom->cbus_function[1] << 4);
2384 output[0x15] = eeprom->cbus_function[2] | (eeprom->cbus_function[3] << 4);
2385 output[0x16] = eeprom->cbus_function[4];
2386 // Addr 17: Unknown
2387
b8aa7b35 2388 // Dynamic content
f505134f
HK
2389 // In images produced by FTDI's FT_Prog for FT232R strings start at 0x18
2390 // Space till 0x18 should be considered as reserved.
2391 if (eeprom->chip_type >= TYPE_R) {
2392 i = 0x18;
2393 } else {
2394 i = 0x14;
2395 }
2396 if (eeprom->size >= 256) i = 0x80;
f01d7ca6 2397
c201f80f 2398
22d12cda 2399 // Output manufacturer
c201f80f
TJ
2400 output[0x0E] = i | 0x80; // calculate offset
2401 output[i++] = manufacturer_size*2 + 2;
2402 output[i++] = 0x03; // type: string
22d12cda
TJ
2403 for (j = 0; j < manufacturer_size; j++)
2404 {
d9f0cce7
TJ
2405 output[i] = eeprom->manufacturer[j], i++;
2406 output[i] = 0x00, i++;
b8aa7b35
TJ
2407 }
2408
2409 // Output product name
c201f80f 2410 output[0x10] = i | 0x80; // calculate offset
b8aa7b35
TJ
2411 output[i] = product_size*2 + 2, i++;
2412 output[i] = 0x03, i++;
22d12cda
TJ
2413 for (j = 0; j < product_size; j++)
2414 {
d9f0cce7
TJ
2415 output[i] = eeprom->product[j], i++;
2416 output[i] = 0x00, i++;
b8aa7b35 2417 }
d9f0cce7 2418
b8aa7b35 2419 // Output serial
c201f80f 2420 output[0x12] = i | 0x80; // calculate offset
b8aa7b35
TJ
2421 output[i] = serial_size*2 + 2, i++;
2422 output[i] = 0x03, i++;
22d12cda
TJ
2423 for (j = 0; j < serial_size; j++)
2424 {
d9f0cce7
TJ
2425 output[i] = eeprom->serial[j], i++;
2426 output[i] = 0x00, i++;
b8aa7b35
TJ
2427 }
2428
2429 // calculate checksum
2430 checksum = 0xAAAA;
d9f0cce7 2431
22d12cda
TJ
2432 for (i = 0; i < eeprom->size/2-1; i++)
2433 {
d9f0cce7
TJ
2434 value = output[i*2];
2435 value += output[(i*2)+1] << 8;
b8aa7b35 2436
d9f0cce7
TJ
2437 checksum = value^checksum;
2438 checksum = (checksum << 1) | (checksum >> 15);
b8aa7b35
TJ
2439 }
2440
c201f80f
TJ
2441 output[eeprom->size-2] = checksum;
2442 output[eeprom->size-1] = checksum >> 8;
b8aa7b35 2443
8ed61121 2444 return size_check;
b8aa7b35
TJ
2445}
2446
4af1d1bb
MK
2447/**
2448 Decode binary EEPROM image into an ftdi_eeprom structure.
2449
2450 \param eeprom Pointer to ftdi_eeprom which will be filled in.
1bbaf1ce 2451 \param buf Buffer of \a size bytes of raw eeprom data
4af1d1bb
MK
2452 \param size size size of eeprom data in bytes
2453
2454 \retval 0: all fine
2455 \retval -1: something went wrong
2456
2457 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
2458 FIXME: Strings are malloc'ed here and should be freed somewhere
2459*/
49c5ac72 2460int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
b56d5a64
MK
2461{
2462 unsigned char i, j;
2463 unsigned short checksum, eeprom_checksum, value;
2464 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
b56d5a64 2465 int eeprom_size = 128;
22a1b5c1
TJ
2466
2467 if (eeprom == NULL)
2468 return -1;
b56d5a64
MK
2469#if 0
2470 size_check = eeprom->size;
2471 size_check -= 28; // 28 are always in use (fixed)
2472
22d12cda 2473 // Top half of a 256byte eeprom is used just for strings and checksum
b56d5a64
MK
2474 // it seems that the FTDI chip will not read these strings from the lower half
2475 // Each string starts with two bytes; offset and type (0x03 for string)
2476 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
22d12cda 2477 if (eeprom->size>=256)size_check = 120;
b56d5a64
MK
2478 size_check -= manufacturer_size*2;
2479 size_check -= product_size*2;
2480 size_check -= serial_size*2;
2481
2482 // eeprom size exceeded?
2483 if (size_check < 0)
2484 return (-1);
2485#endif
2486
2487 // empty eeprom struct
4af1d1bb 2488 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
b56d5a64 2489
f505134f
HK
2490 // Addr 00: High current IO
2491 eeprom->high_current = (buf[0x02] & HIGH_CURRENT_DRIVE);
b56d5a64
MK
2492
2493 // Addr 02: Vendor ID
2494 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2495
2496 // Addr 04: Product ID
2497 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
22d12cda 2498
6335545d
TJ
2499 value = buf[0x06] + (buf[0x07]<<8);
2500 switch (value)
22d12cda 2501 {
f505134f
HK
2502 case 0x0600:
2503 eeprom->chip_type = TYPE_R;
2504 break;
22d12cda 2505 case 0x0400:
f505134f 2506 eeprom->chip_type = TYPE_BM;
22d12cda
TJ
2507 break;
2508 case 0x0200:
f505134f 2509 eeprom->chip_type = TYPE_AM;
22d12cda
TJ
2510 break;
2511 default: // Unknown device
f505134f 2512 eeprom->chip_type = 0;
22d12cda 2513 break;
4af1d1bb 2514 }
b56d5a64
MK
2515
2516 // Addr 08: Config descriptor
2517 // Bit 7: always 1
2518 // Bit 6: 1 if this device is self powered, 0 if bus powered
2519 // Bit 5: 1 if this device uses remote wakeup
2520 // Bit 4: 1 if this device is battery powered
2521 j = buf[0x08];
b56d5a64
MK
2522 if (j&0x40) eeprom->self_powered = 1;
2523 if (j&0x20) eeprom->remote_wakeup = 1;
2524
2525 // Addr 09: Max power consumption: max power = value * 2 mA
2526 eeprom->max_power = buf[0x09];
2527
2528 // Addr 0A: Chip configuration
2529 // Bit 7: 0 - reserved
2530 // Bit 6: 0 - reserved
2531 // Bit 5: 0 - reserved
2532 // Bit 4: 1 - Change USB version
2533 // Bit 3: 1 - Use the serial number string
2534 // Bit 2: 1 - Enable suspend pull downs for lower power
2535 // Bit 1: 1 - Out EndPoint is Isochronous
2536 // Bit 0: 1 - In EndPoint is Isochronous
2537 //
2538 j = buf[0x0A];
4af1d1bb
MK
2539 if (j&0x01) eeprom->in_is_isochronous = 1;
2540 if (j&0x02) eeprom->out_is_isochronous = 1;
2541 if (j&0x04) eeprom->suspend_pull_downs = 1;
2542 if (j&0x08) eeprom->use_serial = 1;
2543 if (j&0x10) eeprom->change_usb_version = 1;
b56d5a64 2544
f505134f
HK
2545 // Addr 0B: Invert data lines
2546 eeprom->invert = buf[0x0B];
b56d5a64
MK
2547
2548 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2549 // Addr 0D: USB version high byte when 0x0A bit 4 is set
22d12cda
TJ
2550 if (eeprom->change_usb_version == 1)
2551 {
2552 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
b56d5a64
MK
2553 }
2554
2555 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2556 // Addr 0F: Length of manufacturer string
2557 manufacturer_size = buf[0x0F]/2;
2558 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2559 else eeprom->manufacturer = NULL;
2560
2561 // Addr 10: Offset of the product string + 0x80, calculated later
2562 // Addr 11: Length of product string
2563 product_size = buf[0x11]/2;
2564 if (product_size > 0) eeprom->product = malloc(product_size);
2565 else eeprom->product = NULL;
2566
2567 // Addr 12: Offset of the serial string + 0x80, calculated later
2568 // Addr 13: Length of serial string
2569 serial_size = buf[0x13]/2;
2570 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2571 else eeprom->serial = NULL;
2572
f505134f
HK
2573 // Addr 14: CBUS function: CBUS0, CBUS1
2574 // Addr 15: CBUS function: CBUS2, CBUS3
2575 // Addr 16: CBUS function: CBUS5
2576 if (eeprom->chip_type == TYPE_R) {
2577 eeprom->cbus_function[0] = buf[0x14] & 0x0f;
2578 eeprom->cbus_function[1] = (buf[0x14] >> 4) & 0x0f;
2579 eeprom->cbus_function[2] = buf[0x15] & 0x0f;
2580 eeprom->cbus_function[3] = (buf[0x15] >> 4) & 0x0f;
2581 eeprom->cbus_function[4] = buf[0x16] & 0x0f;
2582 } else {
2583 for (j=0; j<5; j++) eeprom->cbus_function[j] = 0;
2584 }
2585
22d12cda 2586 // Decode manufacturer
b56d5a64 2587 i = buf[0x0E] & 0x7f; // offset
22d12cda
TJ
2588 for (j=0;j<manufacturer_size-1;j++)
2589 {
2590 eeprom->manufacturer[j] = buf[2*j+i+2];
b56d5a64
MK
2591 }
2592 eeprom->manufacturer[j] = '\0';
2593
2594 // Decode product name
2595 i = buf[0x10] & 0x7f; // offset
22d12cda
TJ
2596 for (j=0;j<product_size-1;j++)
2597 {
2598 eeprom->product[j] = buf[2*j+i+2];
b56d5a64
MK
2599 }
2600 eeprom->product[j] = '\0';
2601
2602 // Decode serial
2603 i = buf[0x12] & 0x7f; // offset
22d12cda
TJ
2604 for (j=0;j<serial_size-1;j++)
2605 {
2606 eeprom->serial[j] = buf[2*j+i+2];
b56d5a64
MK
2607 }
2608 eeprom->serial[j] = '\0';
2609
2610 // verify checksum
2611 checksum = 0xAAAA;
2612
22d12cda
TJ
2613 for (i = 0; i < eeprom_size/2-1; i++)
2614 {
b56d5a64
MK
2615 value = buf[i*2];
2616 value += buf[(i*2)+1] << 8;
2617
2618 checksum = value^checksum;
2619 checksum = (checksum << 1) | (checksum >> 15);
2620 }
2621
2622 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2623
22d12cda
TJ
2624 if (eeprom_checksum != checksum)
2625 {
2626 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2627 return -1;
4af1d1bb
MK
2628 }
2629
2630 return 0;
b56d5a64
MK
2631}
2632
1941414d 2633/**
c1c70e13
OS
2634 Read eeprom location
2635
2636 \param ftdi pointer to ftdi_context
2637 \param eeprom_addr Address of eeprom location to be read
2638 \param eeprom_val Pointer to store read eeprom location
2639
2640 \retval 0: all fine
2641 \retval -1: read failed
22a1b5c1 2642 \retval -2: USB device unavailable
c1c70e13
OS
2643*/
2644int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2645{
22a1b5c1
TJ
2646 if (ftdi == NULL || ftdi->usb_dev == NULL)
2647 ftdi_error_return(-2, "USB device unavailable");
2648
97c6b5f6 2649 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
2650 ftdi_error_return(-1, "reading eeprom failed");
2651
2652 return 0;
2653}
2654
2655/**
1941414d
TJ
2656 Read eeprom
2657
2658 \param ftdi pointer to ftdi_context
2659 \param eeprom Pointer to store eeprom into
b8aa7b35 2660
1941414d
TJ
2661 \retval 0: all fine
2662 \retval -1: read failed
22a1b5c1 2663 \retval -2: USB device unavailable
1941414d 2664*/
a8f46ddc
TJ
2665int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2666{
a3da1d95
GE
2667 int i;
2668
22a1b5c1
TJ
2669 if (ftdi == NULL || ftdi->usb_dev == NULL)
2670 ftdi_error_return(-2, "USB device unavailable");
2671
22d12cda
TJ
2672 for (i = 0; i < ftdi->eeprom_size/2; i++)
2673 {
579b006f 2674 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
c3d95b87 2675 ftdi_error_return(-1, "reading eeprom failed");
a3da1d95
GE
2676 }
2677
2678 return 0;
2679}
2680
cb6250fa
TJ
2681/*
2682 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2683 Function is only used internally
2684 \internal
2685*/
2686static unsigned char ftdi_read_chipid_shift(unsigned char value)
2687{
2688 return ((value & 1) << 1) |
22d12cda
TJ
2689 ((value & 2) << 5) |
2690 ((value & 4) >> 2) |
2691 ((value & 8) << 4) |
2692 ((value & 16) >> 1) |
2693 ((value & 32) >> 1) |
2694 ((value & 64) >> 4) |
2695 ((value & 128) >> 2);
cb6250fa
TJ
2696}
2697
2698/**
2699 Read the FTDIChip-ID from R-type devices
2700
2701 \param ftdi pointer to ftdi_context
2702 \param chipid Pointer to store FTDIChip-ID
2703
2704 \retval 0: all fine
2705 \retval -1: read failed
22a1b5c1 2706 \retval -2: USB device unavailable
cb6250fa
TJ
2707*/
2708int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2709{
c7eb3112 2710 unsigned int a = 0, b = 0;
cb6250fa 2711
22a1b5c1
TJ
2712 if (ftdi == NULL || ftdi->usb_dev == NULL)
2713 ftdi_error_return(-2, "USB device unavailable");
2714
579b006f 2715 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
2716 {
2717 a = a << 8 | a >> 8;
579b006f 2718 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
2719 {
2720 b = b << 8 | b >> 8;
5230676f 2721 a = (a << 16) | (b & 0xFFFF);
912d50ca
TJ
2722 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2723 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
cb6250fa 2724 *chipid = a ^ 0xa5f0f7d1;
c7eb3112 2725 return 0;
cb6250fa
TJ
2726 }
2727 }
2728
c7eb3112 2729 ftdi_error_return(-1, "read of FTDIChip-ID failed");
cb6250fa
TJ
2730}
2731
1941414d 2732/**
22a1b5c1
TJ
2733 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2734 Call this function then do a write then call again to see if size changes, if so write again.
c201f80f 2735
22a1b5c1
TJ
2736 \param ftdi pointer to ftdi_context
2737 \param eeprom Pointer to store eeprom into
2738 \param maxsize the size of the buffer to read into
c201f80f 2739
22a1b5c1
TJ
2740 \retval -1: eeprom read failed
2741 \retval -2: USB device unavailable
2742 \retval >=0: size of eeprom
c201f80f
TJ
2743*/
2744int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2745{
2746 int i=0,j,minsize=32;
2747 int size=minsize;
2748
22a1b5c1
TJ
2749 if (ftdi == NULL || ftdi->usb_dev == NULL)
2750 ftdi_error_return(-2, "USB device unavailable");
2751
22d12cda
TJ
2752 do
2753 {
2754 for (j = 0; i < maxsize/2 && j<size; j++)
2755 {
579b006f
JZ
2756 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2757 SIO_READ_EEPROM_REQUEST, 0, i,
2758 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
22a1b5c1 2759 ftdi_error_return(-1, "eeprom read failed");
22d12cda
TJ
2760 i++;
2761 }
2762 size*=2;
2763 }
2764 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
c201f80f
TJ
2765
2766 return size/2;
2767}
2768
2769/**
c1c70e13
OS
2770 Write eeprom location
2771
2772 \param ftdi pointer to ftdi_context
2773 \param eeprom_addr Address of eeprom location to be written
2774 \param eeprom_val Value to be written
2775
2776 \retval 0: all fine
2777 \retval -1: read failed
22a1b5c1 2778 \retval -2: USB device unavailable
c1c70e13
OS
2779*/
2780int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2781{
22a1b5c1
TJ
2782 if (ftdi == NULL || ftdi->usb_dev == NULL)
2783 ftdi_error_return(-2, "USB device unavailable");
2784
579b006f 2785 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
c1c70e13
OS
2786 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2787 NULL, 0, ftdi->usb_write_timeout) != 0)
2788 ftdi_error_return(-1, "unable to write eeprom");
2789
2790 return 0;
2791}
2792
2793/**
1941414d 2794 Write eeprom
a3da1d95 2795
1941414d
TJ
2796 \param ftdi pointer to ftdi_context
2797 \param eeprom Pointer to read eeprom from
2798
2799 \retval 0: all fine
2800 \retval -1: read failed
22a1b5c1 2801 \retval -2: USB device unavailable
1941414d 2802*/
a8f46ddc
TJ
2803int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2804{
ba5329be 2805 unsigned short usb_val, status;
e30da501 2806 int i, ret;
a3da1d95 2807
22a1b5c1
TJ
2808 if (ftdi == NULL || ftdi->usb_dev == NULL)
2809 ftdi_error_return(-2, "USB device unavailable");
2810
ba5329be 2811 /* These commands were traced while running MProg */
e30da501
TJ
2812 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2813 return ret;
2814 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2815 return ret;
2816 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2817 return ret;
ba5329be 2818
22d12cda
TJ
2819 for (i = 0; i < ftdi->eeprom_size/2; i++)
2820 {
d9f0cce7
TJ
2821 usb_val = eeprom[i*2];
2822 usb_val += eeprom[(i*2)+1] << 8;
579b006f
JZ
2823 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2824 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
2825 NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87 2826 ftdi_error_return(-1, "unable to write eeprom");
a3da1d95
GE
2827 }
2828
2829 return 0;
2830}
2831
1941414d
TJ
2832/**
2833 Erase eeprom
a3da1d95 2834
a5e1bd8c
MK
2835 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2836
1941414d
TJ
2837 \param ftdi pointer to ftdi_context
2838
2839 \retval 0: all fine
2840 \retval -1: erase failed
22a1b5c1 2841 \retval -2: USB device unavailable
1941414d 2842*/
a8f46ddc
TJ
2843int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2844{
22a1b5c1
TJ
2845 if (ftdi == NULL || ftdi->usb_dev == NULL)
2846 ftdi_error_return(-2, "USB device unavailable");
2847
579b006f 2848 if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) < 0)
c3d95b87 2849 ftdi_error_return(-1, "unable to erase eeprom");
a3da1d95
GE
2850
2851 return 0;
2852}
c3d95b87 2853
1941414d
TJ
2854/**
2855 Get string representation for last error code
c3d95b87 2856
1941414d
TJ
2857 \param ftdi pointer to ftdi_context
2858
2859 \retval Pointer to error string
2860*/
c3d95b87
TJ
2861char *ftdi_get_error_string (struct ftdi_context *ftdi)
2862{
22a1b5c1
TJ
2863 if (ftdi == NULL)
2864 return "";
2865
c3d95b87
TJ
2866 return ftdi->error_str;
2867}
a01d31e2 2868
b5ec1820 2869/* @} end of doxygen libftdi group */