Add bitmode for synchronous fifo if FT2232H
[libftdi] / src / ftdi.c
CommitLineData
a3da1d95
GE
1/***************************************************************************
2 ftdi.c - description
3 -------------------
4 begin : Fri Apr 4 2003
c201f80f 5 copyright : (C) 2003-2008 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
98452d97 31#include <usb.h>
a8f46ddc 32#include <string.h>
d2f10023 33#include <errno.h>
b56d5a64 34#include <stdio.h>
0e302db6 35
98452d97 36#include "ftdi.h"
a3da1d95 37
7cc9950e 38/* stuff needed for async write */
f01d7ca6 39#ifdef LIBFTDI_LINUX_ASYNC_MODE
22d12cda
TJ
40#include <sys/ioctl.h>
41#include <sys/time.h>
42#include <sys/select.h>
43#include <sys/types.h>
44#include <unistd.h>
45#include <linux/usbdevice_fs.h>
f01d7ca6 46#endif
7cc9950e 47
21abaf2e 48#define ftdi_error_return(code, str) do { \
2f73e59f 49 ftdi->error_str = str; \
21abaf2e 50 return code; \
d2f10023 51 } while(0);
c3d95b87 52
f3f81007
TJ
53/**
54 Internal function to close usb device pointer.
55 Sets ftdi->usb_dev to NULL.
56 \internal
57
58 \param ftdi pointer to ftdi_context
59
60 \retval zero if all is fine, otherwise error code from usb_close()
61*/
62static int ftdi_usb_close_internal (struct ftdi_context *ftdi)
dff4fdb0
NF
63{
64 int ret = 0;
65
f3f81007 66 if (ftdi->usb_dev)
dff4fdb0
NF
67 {
68 ret = usb_close (ftdi->usb_dev);
69 ftdi->usb_dev = NULL;
70 }
f3f81007 71
dff4fdb0
NF
72 return ret;
73}
c3d95b87 74
1941414d
TJ
75/**
76 Initializes a ftdi_context.
4837f98a 77
1941414d 78 \param ftdi pointer to ftdi_context
4837f98a 79
1941414d
TJ
80 \retval 0: all fine
81 \retval -1: couldn't allocate read buffer
82
83 \remark This should be called before all functions
948f9ada 84*/
a8f46ddc
TJ
85int ftdi_init(struct ftdi_context *ftdi)
86{
bf35baa0 87 unsigned int i;
7cc9950e 88
98452d97 89 ftdi->usb_dev = NULL;
545820ce
TJ
90 ftdi->usb_read_timeout = 5000;
91 ftdi->usb_write_timeout = 5000;
a3da1d95 92
53ad271d 93 ftdi->type = TYPE_BM; /* chip type */
a3da1d95
GE
94 ftdi->baudrate = -1;
95 ftdi->bitbang_enabled = 0;
96
948f9ada
TJ
97 ftdi->readbuffer = NULL;
98 ftdi->readbuffer_offset = 0;
99 ftdi->readbuffer_remaining = 0;
100 ftdi->writebuffer_chunksize = 4096;
101
545820ce
TJ
102 ftdi->interface = 0;
103 ftdi->index = 0;
104 ftdi->in_ep = 0x02;
105 ftdi->out_ep = 0x81;
3119537f 106 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
53ad271d 107
a3da1d95
GE
108 ftdi->error_str = NULL;
109
f01d7ca6 110#ifdef LIBFTDI_LINUX_ASYNC_MODE
7cc9950e
GE
111 ftdi->async_usb_buffer_size=10;
112 if ((ftdi->async_usb_buffer=malloc(sizeof(struct usbdevfs_urb)*ftdi->async_usb_buffer_size)) == NULL)
113 ftdi_error_return(-1, "out of memory for async usb buffer");
114
115 /* initialize async usb buffer with unused-marker */
116 for (i=0; i < ftdi->async_usb_buffer_size; i++)
117 ((struct usbdevfs_urb*)ftdi->async_usb_buffer)[i].usercontext = FTDI_URB_USERCONTEXT_COOKIE;
f01d7ca6
TJ
118#else
119 ftdi->async_usb_buffer_size=0;
120 ftdi->async_usb_buffer = NULL;
121#endif
7cc9950e 122
c201f80f
TJ
123 ftdi->eeprom_size = FTDI_DEFAULT_EEPROM_SIZE;
124
1c733d33
TJ
125 /* All fine. Now allocate the readbuffer */
126 return ftdi_read_data_set_chunksize(ftdi, 4096);
948f9ada 127}
4837f98a 128
1941414d 129/**
cef378aa
TJ
130 Allocate and initialize a new ftdi_context
131
132 \return a pointer to a new ftdi_context, or NULL on failure
133*/
672ac008 134struct ftdi_context *ftdi_new(void)
cef378aa
TJ
135{
136 struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
137
22d12cda
TJ
138 if (ftdi == NULL)
139 {
cef378aa
TJ
140 return NULL;
141 }
142
22d12cda
TJ
143 if (ftdi_init(ftdi) != 0)
144 {
cef378aa 145 free(ftdi);
cdf448f6 146 return NULL;
cef378aa
TJ
147 }
148
149 return ftdi;
150}
151
152/**
1941414d
TJ
153 Open selected channels on a chip, otherwise use first channel.
154
155 \param ftdi pointer to ftdi_context
f9d69895 156 \param interface Interface to use for FT2232C/2232H/4232H chips.
1941414d
TJ
157
158 \retval 0: all fine
159 \retval -1: unknown interface
c4446c36 160*/
0ce2f5fa 161int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
c4446c36 162{
22d12cda
TJ
163 switch (interface)
164 {
165 case INTERFACE_ANY:
166 case INTERFACE_A:
167 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
168 break;
169 case INTERFACE_B:
170 ftdi->interface = 1;
171 ftdi->index = INTERFACE_B;
172 ftdi->in_ep = 0x04;
173 ftdi->out_ep = 0x83;
174 break;
f9d69895
AH
175 case INTERFACE_C:
176 ftdi->interface = 2;
177 ftdi->index = INTERFACE_C;
178 ftdi->in_ep = 0x06;
179 ftdi->out_ep = 0x85;
180 break;
181 case INTERFACE_D:
182 ftdi->interface = 3;
183 ftdi->index = INTERFACE_D;
184 ftdi->in_ep = 0x08;
185 ftdi->out_ep = 0x87;
186 break;
22d12cda
TJ
187 default:
188 ftdi_error_return(-1, "Unknown interface");
c4446c36
TJ
189 }
190 return 0;
191}
948f9ada 192
1941414d
TJ
193/**
194 Deinitializes a ftdi_context.
4837f98a 195
1941414d 196 \param ftdi pointer to ftdi_context
4837f98a 197*/
a8f46ddc
TJ
198void ftdi_deinit(struct ftdi_context *ftdi)
199{
f3f81007 200 ftdi_usb_close_internal (ftdi);
dff4fdb0 201
22d12cda
TJ
202 if (ftdi->async_usb_buffer != NULL)
203 {
7cc9950e
GE
204 free(ftdi->async_usb_buffer);
205 ftdi->async_usb_buffer = NULL;
206 }
207
22d12cda
TJ
208 if (ftdi->readbuffer != NULL)
209 {
d9f0cce7
TJ
210 free(ftdi->readbuffer);
211 ftdi->readbuffer = NULL;
948f9ada 212 }
a3da1d95
GE
213}
214
1941414d 215/**
cef378aa
TJ
216 Deinitialize and free an ftdi_context.
217
218 \param ftdi pointer to ftdi_context
219*/
220void ftdi_free(struct ftdi_context *ftdi)
221{
222 ftdi_deinit(ftdi);
223 free(ftdi);
224}
225
226/**
1941414d
TJ
227 Use an already open libusb device.
228
229 \param ftdi pointer to ftdi_context
230 \param usb libusb usb_dev_handle to use
4837f98a 231*/
a8f46ddc
TJ
232void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
233{
98452d97
TJ
234 ftdi->usb_dev = usb;
235}
236
237
1941414d
TJ
238/**
239 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
240 needs to be deallocated by ftdi_list_free() after use.
241
242 \param ftdi pointer to ftdi_context
243 \param devlist Pointer where to store list of found devices
244 \param vendor Vendor ID to search for
245 \param product Product ID to search for
edb82cbf 246
1941414d
TJ
247 \retval >0: number of devices found
248 \retval -1: usb_find_busses() failed
249 \retval -2: usb_find_devices() failed
250 \retval -3: out of memory
edb82cbf 251*/
d2f10023 252int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
edb82cbf
TJ
253{
254 struct ftdi_device_list **curdev;
255 struct usb_bus *bus;
256 struct usb_device *dev;
257 int count = 0;
d2f10023 258
edb82cbf
TJ
259 usb_init();
260 if (usb_find_busses() < 0)
261 ftdi_error_return(-1, "usb_find_busses() failed");
262 if (usb_find_devices() < 0)
263 ftdi_error_return(-2, "usb_find_devices() failed");
264
265 curdev = devlist;
6db32169 266 *curdev = NULL;
22d12cda
TJ
267 for (bus = usb_get_busses(); bus; bus = bus->next)
268 {
269 for (dev = bus->devices; dev; dev = dev->next)
270 {
edb82cbf
TJ
271 if (dev->descriptor.idVendor == vendor
272 && dev->descriptor.idProduct == product)
273 {
274 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
275 if (!*curdev)
276 ftdi_error_return(-3, "out of memory");
d2f10023 277
edb82cbf
TJ
278 (*curdev)->next = NULL;
279 (*curdev)->dev = dev;
280
281 curdev = &(*curdev)->next;
282 count++;
283 }
284 }
285 }
d2f10023 286
edb82cbf
TJ
287 return count;
288}
289
1941414d
TJ
290/**
291 Frees a usb device list.
edb82cbf 292
1941414d 293 \param devlist USB device list created by ftdi_usb_find_all()
edb82cbf 294*/
d2f10023 295void ftdi_list_free(struct ftdi_device_list **devlist)
edb82cbf 296{
6db32169
TJ
297 struct ftdi_device_list *curdev, *next;
298
22d12cda
TJ
299 for (curdev = *devlist; curdev != NULL;)
300 {
6db32169
TJ
301 next = curdev->next;
302 free(curdev);
303 curdev = next;
edb82cbf
TJ
304 }
305
6db32169 306 *devlist = NULL;
edb82cbf
TJ
307}
308
1941414d 309/**
cef378aa
TJ
310 Frees a usb device list.
311
312 \param devlist USB device list created by ftdi_usb_find_all()
313*/
314void ftdi_list_free2(struct ftdi_device_list *devlist)
315{
316 ftdi_list_free(&devlist);
317}
318
319/**
474786c0
TJ
320 Return device ID strings from the usb device.
321
322 The parameters manufacturer, description and serial may be NULL
323 or pointer to buffers to store the fetched strings.
324
898c34dd
TJ
325 \note Use this function only in combination with ftdi_usb_find_all()
326 as it closes the internal "usb_dev" after use.
327
474786c0
TJ
328 \param ftdi pointer to ftdi_context
329 \param dev libusb usb_dev to use
330 \param manufacturer Store manufacturer string here if not NULL
331 \param mnf_len Buffer size of manufacturer string
332 \param description Store product description string here if not NULL
333 \param desc_len Buffer size of product description string
334 \param serial Store serial string here if not NULL
335 \param serial_len Buffer size of serial string
336
337 \retval 0: all fine
338 \retval -1: wrong arguments
339 \retval -4: unable to open device
340 \retval -7: get product manufacturer failed
341 \retval -8: get product description failed
342 \retval -9: get serial number failed
343 \retval -10: unable to close device
344*/
345int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
22d12cda 346 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
474786c0
TJ
347{
348 if ((ftdi==NULL) || (dev==NULL))
349 return -1;
350
351 if (!(ftdi->usb_dev = usb_open(dev)))
352 ftdi_error_return(-4, usb_strerror());
353
22d12cda
TJ
354 if (manufacturer != NULL)
355 {
356 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0)
357 {
f3f81007 358 ftdi_usb_close_internal (ftdi);
474786c0
TJ
359 ftdi_error_return(-7, usb_strerror());
360 }
361 }
362
22d12cda
TJ
363 if (description != NULL)
364 {
365 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0)
366 {
f3f81007 367 ftdi_usb_close_internal (ftdi);
474786c0
TJ
368 ftdi_error_return(-8, usb_strerror());
369 }
370 }
371
22d12cda
TJ
372 if (serial != NULL)
373 {
374 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0)
375 {
f3f81007 376 ftdi_usb_close_internal (ftdi);
474786c0
TJ
377 ftdi_error_return(-9, usb_strerror());
378 }
379 }
380
f3f81007 381 if (ftdi_usb_close_internal (ftdi) != 0)
474786c0
TJ
382 ftdi_error_return(-10, usb_strerror());
383
384 return 0;
385}
386
387/**
1941414d 388 Opens a ftdi device given by a usb_device.
7b18bef6 389
1941414d
TJ
390 \param ftdi pointer to ftdi_context
391 \param dev libusb usb_dev to use
392
393 \retval 0: all fine
23b1798d 394 \retval -3: unable to config device
1941414d
TJ
395 \retval -4: unable to open device
396 \retval -5: unable to claim device
397 \retval -6: reset failed
398 \retval -7: set baudrate failed
7b18bef6
TJ
399*/
400int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
401{
d2f10023 402 int detach_errno = 0;
a56ba2bd 403 int config_val = 1;
7b18bef6
TJ
404 if (!(ftdi->usb_dev = usb_open(dev)))
405 ftdi_error_return(-4, "usb_open() failed");
d2f10023
TJ
406
407#ifdef LIBUSB_HAS_GET_DRIVER_NP
22592e17
TJ
408 // Try to detach ftdi_sio kernel module.
409 // Returns ENODATA if driver is not loaded.
410 //
411 // The return code is kept in a separate variable and only parsed
412 // if usb_set_configuration() or usb_claim_interface() fails as the
413 // detach operation might be denied and everything still works fine.
414 // Likely scenario is a static ftdi_sio kernel module.
d2f10023
TJ
415 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
416 detach_errno = errno;
417#endif
418
8af5bbc7 419#ifdef __WIN32__
b57aedfd
GE
420 // set configuration (needed especially for windows)
421 // tolerate EBUSY: one device with one configuration, but two interfaces
422 // and libftdi sessions to both interfaces (e.g. FT2232)
a56ba2bd
TJ
423
424 if (dev->descriptor.bNumConfigurations > 0)
b57aedfd 425 {
a56ba2bd
TJ
426 // libusb-win32 on Windows 64 can return a null pointer for a valid device
427 if (dev->config)
428 config_val = dev->config[0].bConfigurationValue;
429
430 if (usb_set_configuration(ftdi->usb_dev, config_val) &&
431 errno != EBUSY)
22d12cda 432 {
a56ba2bd
TJ
433 ftdi_usb_close_internal (ftdi);
434 if (detach_errno == EPERM)
435 {
436 ftdi_error_return(-8, "inappropriate permissions on device!");
437 }
438 else
439 {
440 ftdi_error_return(-3, "unable to set usb configuration. Make sure ftdi_sio is unloaded!");
441 }
23b1798d
TJ
442 }
443 }
8af5bbc7 444#endif
23b1798d 445
22d12cda
TJ
446 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0)
447 {
f3f81007 448 ftdi_usb_close_internal (ftdi);
22d12cda
TJ
449 if (detach_errno == EPERM)
450 {
d2f10023 451 ftdi_error_return(-8, "inappropriate permissions on device!");
22d12cda
TJ
452 }
453 else
454 {
d2f10023
TJ
455 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
456 }
7b18bef6
TJ
457 }
458
22d12cda
TJ
459 if (ftdi_usb_reset (ftdi) != 0)
460 {
f3f81007 461 ftdi_usb_close_internal (ftdi);
7b18bef6
TJ
462 ftdi_error_return(-6, "ftdi_usb_reset failed");
463 }
464
7b18bef6
TJ
465 // Try to guess chip type
466 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
467 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
468 && dev->descriptor.iSerialNumber == 0))
469 ftdi->type = TYPE_BM;
470 else if (dev->descriptor.bcdDevice == 0x200)
471 ftdi->type = TYPE_AM;
22d12cda 472 else if (dev->descriptor.bcdDevice == 0x500)
7b18bef6 473 ftdi->type = TYPE_2232C;
22d12cda 474 else if (dev->descriptor.bcdDevice == 0x600)
cb6250fa 475 ftdi->type = TYPE_R;
0beb9686
TJ
476 else if (dev->descriptor.bcdDevice == 0x700)
477 ftdi->type = TYPE_2232H;
478 else if (dev->descriptor.bcdDevice == 0x800)
479 ftdi->type = TYPE_4232H;
7b18bef6 480
f9d69895
AH
481 // Set default interface on dual/quad type chips
482 switch(ftdi->type)
483 {
484 case TYPE_2232C:
485 case TYPE_2232H:
486 case TYPE_4232H:
487 if (!ftdi->index)
488 ftdi->index = INTERFACE_A;
489 break;
490 default:
491 break;
492 }
493
ef6f4838
TE
494 if (ftdi_set_baudrate (ftdi, 9600) != 0)
495 {
496 ftdi_usb_close_internal (ftdi);
497 ftdi_error_return(-7, "set baudrate failed");
498 }
499
7b18bef6
TJ
500 ftdi_error_return(0, "all fine");
501}
502
1941414d
TJ
503/**
504 Opens the first device with a given vendor and product ids.
505
506 \param ftdi pointer to ftdi_context
507 \param vendor Vendor ID
508 \param product Product ID
509
9bec2387 510 \retval same as ftdi_usb_open_desc()
1941414d 511*/
edb82cbf
TJ
512int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
513{
514 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
515}
516
1941414d
TJ
517/**
518 Opens the first device with a given, vendor id, product id,
519 description and serial.
520
521 \param ftdi pointer to ftdi_context
522 \param vendor Vendor ID
523 \param product Product ID
524 \param description Description to search for. Use NULL if not needed.
525 \param serial Serial to search for. Use NULL if not needed.
526
527 \retval 0: all fine
528 \retval -1: usb_find_busses() failed
529 \retval -2: usb_find_devices() failed
530 \retval -3: usb device not found
531 \retval -4: unable to open device
532 \retval -5: unable to claim device
533 \retval -6: reset failed
534 \retval -7: set baudrate failed
535 \retval -8: get product description failed
536 \retval -9: get serial number failed
537 \retval -10: unable to close device
a3da1d95 538*/
04e1ea0a 539int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
a8f46ddc
TJ
540 const char* description, const char* serial)
541{
98452d97
TJ
542 struct usb_bus *bus;
543 struct usb_device *dev;
c3d95b87 544 char string[256];
98452d97
TJ
545
546 usb_init();
547
c3d95b87
TJ
548 if (usb_find_busses() < 0)
549 ftdi_error_return(-1, "usb_find_busses() failed");
c3d95b87 550 if (usb_find_devices() < 0)
edb82cbf 551 ftdi_error_return(-2, "usb_find_devices() failed");
a3da1d95 552
22d12cda
TJ
553 for (bus = usb_get_busses(); bus; bus = bus->next)
554 {
555 for (dev = bus->devices; dev; dev = dev->next)
556 {
a8f46ddc 557 if (dev->descriptor.idVendor == vendor
22d12cda
TJ
558 && dev->descriptor.idProduct == product)
559 {
c3d95b87
TJ
560 if (!(ftdi->usb_dev = usb_open(dev)))
561 ftdi_error_return(-4, "usb_open() failed");
562
22d12cda
TJ
563 if (description != NULL)
564 {
565 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0)
566 {
f3f81007 567 ftdi_usb_close_internal (ftdi);
c3d95b87 568 ftdi_error_return(-8, "unable to fetch product description");
98452d97 569 }
22d12cda
TJ
570 if (strncmp(string, description, sizeof(string)) != 0)
571 {
f3f81007 572 if (ftdi_usb_close_internal (ftdi) != 0)
edb82cbf 573 ftdi_error_return(-10, "unable to close device");
a8f46ddc
TJ
574 continue;
575 }
576 }
22d12cda
TJ
577 if (serial != NULL)
578 {
579 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0)
580 {
f3f81007 581 ftdi_usb_close_internal (ftdi);
c3d95b87 582 ftdi_error_return(-9, "unable to fetch serial number");
a8f46ddc 583 }
22d12cda
TJ
584 if (strncmp(string, serial, sizeof(string)) != 0)
585 {
f3f81007 586 if (ftdi_usb_close_internal (ftdi) != 0)
edb82cbf 587 ftdi_error_return(-10, "unable to close device");
a8f46ddc
TJ
588 continue;
589 }
590 }
98452d97 591
f3f81007 592 if (ftdi_usb_close_internal (ftdi) != 0)
edb82cbf 593 ftdi_error_return(-10, "unable to close device");
d2f10023 594
edb82cbf 595 return ftdi_usb_open_dev(ftdi, dev);
98452d97
TJ
596 }
597 }
98452d97 598 }
a3da1d95 599
98452d97 600 // device not found
c3d95b87 601 ftdi_error_return(-3, "device not found");
a3da1d95
GE
602}
603
1941414d
TJ
604/**
605 Resets the ftdi device.
a3da1d95 606
1941414d
TJ
607 \param ftdi pointer to ftdi_context
608
609 \retval 0: all fine
610 \retval -1: FTDI reset failed
4837f98a 611*/
edb82cbf 612int ftdi_usb_reset(struct ftdi_context *ftdi)
a8f46ddc 613{
a5e1bd8c
MK
614 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
615 SIO_RESET_REQUEST, SIO_RESET_SIO,
616 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
22d12cda 617 ftdi_error_return(-1,"FTDI reset failed");
c3d95b87 618
545820ce 619 // Invalidate data in the readbuffer
bfcee05b
TJ
620 ftdi->readbuffer_offset = 0;
621 ftdi->readbuffer_remaining = 0;
622
a3da1d95
GE
623 return 0;
624}
625
1941414d 626/**
1189b11a 627 Clears the read buffer on the chip and the internal read buffer.
1941414d
TJ
628
629 \param ftdi pointer to ftdi_context
4837f98a 630
1941414d 631 \retval 0: all fine
1189b11a 632 \retval -1: read buffer purge failed
4837f98a 633*/
1189b11a 634int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
a8f46ddc 635{
22d12cda
TJ
636 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
637 SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
638 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
c3d95b87
TJ
639 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
640
545820ce 641 // Invalidate data in the readbuffer
bfcee05b
TJ
642 ftdi->readbuffer_offset = 0;
643 ftdi->readbuffer_remaining = 0;
a60be878 644
1189b11a
TJ
645 return 0;
646}
647
648/**
649 Clears the write buffer on the chip.
650
651 \param ftdi pointer to ftdi_context
652
653 \retval 0: all fine
654 \retval -1: write buffer purge failed
655*/
656int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
657{
22d12cda
TJ
658 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
659 SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
660 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1189b11a
TJ
661 ftdi_error_return(-1, "FTDI purge of TX buffer failed");
662
663 return 0;
664}
665
666/**
667 Clears the buffers on the chip and the internal read buffer.
668
669 \param ftdi pointer to ftdi_context
670
671 \retval 0: all fine
672 \retval -1: read buffer purge failed
673 \retval -2: write buffer purge failed
674*/
675int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
676{
677 int result;
678
679 result = ftdi_usb_purge_rx_buffer(ftdi);
5a2b51cb 680 if (result < 0)
1189b11a
TJ
681 return -1;
682
683 result = ftdi_usb_purge_tx_buffer(ftdi);
5a2b51cb 684 if (result < 0)
1189b11a 685 return -2;
545820ce 686
a60be878
TJ
687 return 0;
688}
a3da1d95 689
f3f81007
TJ
690
691
1941414d
TJ
692/**
693 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
694
695 \param ftdi pointer to ftdi_context
696
697 \retval 0: all fine
698 \retval -1: usb_release failed
699 \retval -2: usb_close failed
a3da1d95 700*/
a8f46ddc
TJ
701int ftdi_usb_close(struct ftdi_context *ftdi)
702{
a3da1d95
GE
703 int rtn = 0;
704
f01d7ca6 705#ifdef LIBFTDI_LINUX_ASYNC_MODE
7cc9950e
GE
706 /* try to release some kernel resources */
707 ftdi_async_complete(ftdi,1);
f01d7ca6 708#endif
7cc9950e 709
dff4fdb0
NF
710 if (ftdi->usb_dev != NULL)
711 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
712 rtn = -1;
98452d97 713
f3f81007 714 if (ftdi_usb_close_internal (ftdi) != 0)
a3da1d95 715 rtn = -2;
98452d97 716
a3da1d95
GE
717 return rtn;
718}
719
a3da1d95 720/*
53ad271d
TJ
721 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
722 Function is only used internally
b5ec1820 723 \internal
53ad271d 724*/
0126d22e 725static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
a8f46ddc
TJ
726 unsigned short *value, unsigned short *index)
727{
53ad271d
TJ
728 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
729 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
730 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
731 int divisor, best_divisor, best_baud, best_baud_diff;
732 unsigned long encoded_divisor;
733 int i;
734
22d12cda
TJ
735 if (baudrate <= 0)
736 {
53ad271d
TJ
737 // Return error
738 return -1;
739 }
740
741 divisor = 24000000 / baudrate;
742
22d12cda
TJ
743 if (ftdi->type == TYPE_AM)
744 {
53ad271d
TJ
745 // Round down to supported fraction (AM only)
746 divisor -= am_adjust_dn[divisor & 7];
747 }
748
749 // Try this divisor and the one above it (because division rounds down)
750 best_divisor = 0;
751 best_baud = 0;
752 best_baud_diff = 0;
22d12cda
TJ
753 for (i = 0; i < 2; i++)
754 {
53ad271d
TJ
755 int try_divisor = divisor + i;
756 int baud_estimate;
757 int baud_diff;
758
759 // Round up to supported divisor value
22d12cda
TJ
760 if (try_divisor <= 8)
761 {
53ad271d
TJ
762 // Round up to minimum supported divisor
763 try_divisor = 8;
22d12cda
TJ
764 }
765 else if (ftdi->type != TYPE_AM && try_divisor < 12)
766 {
53ad271d
TJ
767 // BM doesn't support divisors 9 through 11 inclusive
768 try_divisor = 12;
22d12cda
TJ
769 }
770 else if (divisor < 16)
771 {
53ad271d
TJ
772 // AM doesn't support divisors 9 through 15 inclusive
773 try_divisor = 16;
22d12cda
TJ
774 }
775 else
776 {
777 if (ftdi->type == TYPE_AM)
778 {
53ad271d
TJ
779 // Round up to supported fraction (AM only)
780 try_divisor += am_adjust_up[try_divisor & 7];
22d12cda
TJ
781 if (try_divisor > 0x1FFF8)
782 {
53ad271d
TJ
783 // Round down to maximum supported divisor value (for AM)
784 try_divisor = 0x1FFF8;
785 }
22d12cda
TJ
786 }
787 else
788 {
789 if (try_divisor > 0x1FFFF)
790 {
53ad271d
TJ
791 // Round down to maximum supported divisor value (for BM)
792 try_divisor = 0x1FFFF;
793 }
794 }
795 }
796 // Get estimated baud rate (to nearest integer)
797 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
798 // Get absolute difference from requested baud rate
22d12cda
TJ
799 if (baud_estimate < baudrate)
800 {
53ad271d 801 baud_diff = baudrate - baud_estimate;
22d12cda
TJ
802 }
803 else
804 {
53ad271d
TJ
805 baud_diff = baud_estimate - baudrate;
806 }
22d12cda
TJ
807 if (i == 0 || baud_diff < best_baud_diff)
808 {
53ad271d
TJ
809 // Closest to requested baud rate so far
810 best_divisor = try_divisor;
811 best_baud = baud_estimate;
812 best_baud_diff = baud_diff;
22d12cda
TJ
813 if (baud_diff == 0)
814 {
53ad271d
TJ
815 // Spot on! No point trying
816 break;
817 }
818 }
819 }
820 // Encode the best divisor value
821 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
822 // Deal with special cases for encoded value
22d12cda
TJ
823 if (encoded_divisor == 1)
824 {
4837f98a 825 encoded_divisor = 0; // 3000000 baud
22d12cda
TJ
826 }
827 else if (encoded_divisor == 0x4001)
828 {
4837f98a 829 encoded_divisor = 1; // 2000000 baud (BM only)
53ad271d
TJ
830 }
831 // Split into "value" and "index" values
832 *value = (unsigned short)(encoded_divisor & 0xFFFF);
1416eb14 833 if (ftdi->type == TYPE_2232C || ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
22d12cda 834 {
0126d22e
TJ
835 *index = (unsigned short)(encoded_divisor >> 8);
836 *index &= 0xFF00;
a9c57c05 837 *index |= ftdi->index;
0126d22e
TJ
838 }
839 else
840 *index = (unsigned short)(encoded_divisor >> 16);
c3d95b87 841
53ad271d
TJ
842 // Return the nearest baud rate
843 return best_baud;
844}
845
1941414d 846/**
9bec2387 847 Sets the chip baud rate
1941414d
TJ
848
849 \param ftdi pointer to ftdi_context
9bec2387 850 \param baudrate baud rate to set
1941414d
TJ
851
852 \retval 0: all fine
853 \retval -1: invalid baudrate
854 \retval -2: setting baudrate failed
a3da1d95 855*/
a8f46ddc
TJ
856int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
857{
53ad271d
TJ
858 unsigned short value, index;
859 int actual_baudrate;
a3da1d95 860
22d12cda
TJ
861 if (ftdi->bitbang_enabled)
862 {
a3da1d95
GE
863 baudrate = baudrate*4;
864 }
865
25707904 866 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
c3d95b87
TJ
867 if (actual_baudrate <= 0)
868 ftdi_error_return (-1, "Silly baudrate <= 0.");
a3da1d95 869
53ad271d
TJ
870 // Check within tolerance (about 5%)
871 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
872 || ((actual_baudrate < baudrate)
873 ? (actual_baudrate * 21 < baudrate * 20)
c3d95b87
TJ
874 : (baudrate * 21 < actual_baudrate * 20)))
875 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
545820ce 876
a5e1bd8c 877 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
9ecfef2a
TJ
878 SIO_SET_BAUDRATE_REQUEST, value,
879 index, NULL, 0, ftdi->usb_write_timeout) != 0)
c3d95b87 880 ftdi_error_return (-2, "Setting new baudrate failed");
a3da1d95
GE
881
882 ftdi->baudrate = baudrate;
883 return 0;
884}
885
1941414d 886/**
6c32e222
TJ
887 Set (RS232) line characteristics.
888 The break type can only be set via ftdi_set_line_property2()
889 and defaults to "off".
4837f98a 890
1941414d
TJ
891 \param ftdi pointer to ftdi_context
892 \param bits Number of bits
893 \param sbit Number of stop bits
894 \param parity Parity mode
895
896 \retval 0: all fine
897 \retval -1: Setting line property failed
2f73e59f
TJ
898*/
899int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
d2f10023 900 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
2f73e59f 901{
6c32e222
TJ
902 return ftdi_set_line_property2(ftdi, bits, sbit, parity, BREAK_OFF);
903}
904
905/**
906 Set (RS232) line characteristics
907
908 \param ftdi pointer to ftdi_context
909 \param bits Number of bits
910 \param sbit Number of stop bits
911 \param parity Parity mode
912 \param break_type Break type
913
914 \retval 0: all fine
915 \retval -1: Setting line property failed
916*/
917int ftdi_set_line_property2(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
22d12cda
TJ
918 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity,
919 enum ftdi_break_type break_type)
6c32e222 920{
2f73e59f
TJ
921 unsigned short value = bits;
922
22d12cda
TJ
923 switch (parity)
924 {
925 case NONE:
926 value |= (0x00 << 8);
927 break;
928 case ODD:
929 value |= (0x01 << 8);
930 break;
931 case EVEN:
932 value |= (0x02 << 8);
933 break;
934 case MARK:
935 value |= (0x03 << 8);
936 break;
937 case SPACE:
938 value |= (0x04 << 8);
939 break;
2f73e59f 940 }
d2f10023 941
22d12cda
TJ
942 switch (sbit)
943 {
944 case STOP_BIT_1:
945 value |= (0x00 << 11);
946 break;
947 case STOP_BIT_15:
948 value |= (0x01 << 11);
949 break;
950 case STOP_BIT_2:
951 value |= (0x02 << 11);
952 break;
2f73e59f 953 }
d2f10023 954
22d12cda
TJ
955 switch (break_type)
956 {
957 case BREAK_OFF:
958 value |= (0x00 << 14);
959 break;
960 case BREAK_ON:
961 value |= (0x01 << 14);
962 break;
6c32e222
TJ
963 }
964
a5e1bd8c 965 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
9ecfef2a
TJ
966 SIO_SET_DATA_REQUEST, value,
967 ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
2f73e59f 968 ftdi_error_return (-1, "Setting new line property failed");
d2f10023 969
2f73e59f
TJ
970 return 0;
971}
a3da1d95 972
1941414d
TJ
973/**
974 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
975
976 \param ftdi pointer to ftdi_context
977 \param buf Buffer with the data
978 \param size Size of the buffer
979
980 \retval <0: error code from usb_bulk_write()
981 \retval >0: number of bytes written
982*/
a8f46ddc
TJ
983int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
984{
a3da1d95
GE
985 int ret;
986 int offset = 0;
545820ce 987 int total_written = 0;
c3d95b87 988
22d12cda
TJ
989 while (offset < size)
990 {
948f9ada 991 int write_size = ftdi->writebuffer_chunksize;
a3da1d95
GE
992
993 if (offset+write_size > size)
994 write_size = size-offset;
995
98452d97 996 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
c3d95b87
TJ
997 if (ret < 0)
998 ftdi_error_return(ret, "usb bulk write failed");
a3da1d95 999
c3d95b87 1000 total_written += ret;
a3da1d95
GE
1001 offset += write_size;
1002 }
1003
545820ce 1004 return total_written;
a3da1d95
GE
1005}
1006
f01d7ca6 1007#ifdef LIBFTDI_LINUX_ASYNC_MODE
e59bc450
CW
1008#ifdef USB_CLASS_PTP
1009#error LIBFTDI_LINUX_ASYNC_MODE is not compatible with libusb-compat-0.1!
1010#endif
4c9e3812
GE
1011/* this is strongly dependent on libusb using the same struct layout. If libusb
1012 changes in some later version this may break horribly (this is for libusb 0.1.12) */
22d12cda
TJ
1013struct usb_dev_handle
1014{
1015 int fd;
1016 // some other stuff coming here we don't need
4c9e3812
GE
1017};
1018
84f85aaa 1019/**
c201f80f
TJ
1020 Check for pending async urbs
1021 \internal
1022*/
1023static int _usb_get_async_urbs_pending(struct ftdi_context *ftdi)
7cc9950e
GE
1024{
1025 struct usbdevfs_urb *urb;
1026 int pending=0;
bf35baa0 1027 unsigned int i;
7cc9950e 1028
22d12cda
TJ
1029 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1030 {
7cc9950e
GE
1031 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1032 if (urb->usercontext != FTDI_URB_USERCONTEXT_COOKIE)
1033 pending++;
1034 }
1035
1036 return pending;
1037}
1038
84f85aaa
GE
1039/**
1040 Wait until one or more async URBs are completed by the kernel and mark their
1041 positions in the async-buffer as unused
1042
1043 \param ftdi pointer to ftdi_context
1044 \param wait_for_more if != 0 wait for more than one write to complete
1045 \param timeout_msec max milliseconds to wait
1046
c201f80f
TJ
1047 \internal
1048*/
1049static void _usb_async_cleanup(struct ftdi_context *ftdi, int wait_for_more, int timeout_msec)
7cc9950e 1050{
22d12cda
TJ
1051 struct timeval tv;
1052 struct usbdevfs_urb *urb=NULL;
1053 int ret;
1054 fd_set writefds;
1055 int keep_going=0;
1056
1057 FD_ZERO(&writefds);
1058 FD_SET(ftdi->usb_dev->fd, &writefds);
1059
1060 /* init timeout only once, select writes time left after call */
1061 tv.tv_sec = timeout_msec / 1000;
1062 tv.tv_usec = (timeout_msec % 1000) * 1000;
1063
1064 do
7cc9950e 1065 {
22d12cda
TJ
1066 while (_usb_get_async_urbs_pending(ftdi)
1067 && (ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_REAPURBNDELAY, &urb)) == -1
1068 && errno == EAGAIN)
1069 {
1070 if (keep_going && !wait_for_more)
1071 {
1072 /* don't wait if repeating only for keep_going */
1073 keep_going=0;
1074 break;
1075 }
7cc9950e 1076
22d12cda
TJ
1077 /* wait for timeout msec or something written ready */
1078 select(ftdi->usb_dev->fd+1, NULL, &writefds, NULL, &tv);
1079 }
1080
1081 if (ret == 0 && urb != NULL)
1082 {
1083 /* got a free urb, mark it */
1084 urb->usercontext = FTDI_URB_USERCONTEXT_COOKIE;
7cc9950e 1085
22d12cda
TJ
1086 /* try to get more urbs that are ready now, but don't wait anymore */
1087 urb=NULL;
1088 keep_going=1;
1089 }
1090 else
1091 {
1092 /* no more urbs waiting */
1093 keep_going=0;
1094 }
7cc9950e 1095 }
22d12cda 1096 while (keep_going);
7cc9950e
GE
1097}
1098
1099/**
84f85aaa
GE
1100 Wait until one or more async URBs are completed by the kernel and mark their
1101 positions in the async-buffer as unused.
7cc9950e
GE
1102
1103 \param ftdi pointer to ftdi_context
1104 \param wait_for_more if != 0 wait for more than one write to complete (until write timeout)
1105*/
1106void ftdi_async_complete(struct ftdi_context *ftdi, int wait_for_more)
1107{
22d12cda 1108 _usb_async_cleanup(ftdi,wait_for_more,ftdi->usb_write_timeout);
7cc9950e 1109}
4c9e3812
GE
1110
1111/**
1112 Stupid libusb does not offer async writes nor does it allow
1113 access to its fd - so we need some hacks here.
c201f80f 1114 \internal
4c9e3812 1115*/
c201f80f 1116static int _usb_bulk_write_async(struct ftdi_context *ftdi, int ep, char *bytes, int size)
4c9e3812 1117{
22d12cda
TJ
1118 struct usbdevfs_urb *urb;
1119 int bytesdone = 0, requested;
bf35baa0
TJ
1120 int ret, cleanup_count;
1121 unsigned int i;
22d12cda
TJ
1122
1123 do
7cc9950e 1124 {
22d12cda
TJ
1125 /* find a free urb buffer we can use */
1126 urb=NULL;
1127 for (cleanup_count=0; urb==NULL && cleanup_count <= 1; cleanup_count++)
1128 {
1129 if (i==ftdi->async_usb_buffer_size)
1130 {
1131 /* wait until some buffers are free */
1132 _usb_async_cleanup(ftdi,0,ftdi->usb_write_timeout);
1133 }
7cc9950e 1134
22d12cda
TJ
1135 for (i=0; i < ftdi->async_usb_buffer_size; i++)
1136 {
1137 urb=&((struct usbdevfs_urb *)(ftdi->async_usb_buffer))[i];
1138 if (urb->usercontext == FTDI_URB_USERCONTEXT_COOKIE)
1139 break; /* found a free urb position */
1140 urb=NULL;
1141 }
7cc9950e 1142 }
7cc9950e 1143
22d12cda
TJ
1144 /* no free urb position found */
1145 if (urb==NULL)
1146 return -1;
1147
1148 requested = size - bytesdone;
1149 if (requested > 4096)
1150 requested = 4096;
4c9e3812 1151
22d12cda
TJ
1152 memset(urb,0,sizeof(urb));
1153
1154 urb->type = USBDEVFS_URB_TYPE_BULK;
1155 urb->endpoint = ep;
1156 urb->flags = 0;
1157 urb->buffer = bytes + bytesdone;
1158 urb->buffer_length = requested;
1159 urb->signr = 0;
1160 urb->actual_length = 0;
1161 urb->number_of_packets = 0;
1162 urb->usercontext = 0;
1163
1164 do
1165 {
1166 ret = ioctl(ftdi->usb_dev->fd, USBDEVFS_SUBMITURB, urb);
1167 }
1168 while (ret < 0 && errno == EINTR);
1169 if (ret < 0)
1170 return ret; /* the caller can read errno to get more info */
1171
1172 bytesdone += requested;
1173 }
1174 while (bytesdone < size);
1175 return bytesdone;
4c9e3812
GE
1176}
1177
1178/**
1179 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
1180 Does not wait for completion of the transfer nor does it make sure that
1181 the transfer was successful.
1182
1183 This function could be extended to use signals and callbacks to inform the
1184 caller of completion or error - but this is not done yet, volunteers welcome.
1185
1186 Works around libusb and directly accesses functions only available on Linux.
cef378aa 1187 Only available if compiled with --with-async-mode.
4c9e3812
GE
1188
1189 \param ftdi pointer to ftdi_context
1190 \param buf Buffer with the data
1191 \param size Size of the buffer
1192
1193 \retval <0: error code from usb_bulk_write()
1194 \retval >0: number of bytes written
1195*/
1196int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
1197{
1198 int ret;
1199 int offset = 0;
1200 int total_written = 0;
1201
22d12cda
TJ
1202 while (offset < size)
1203 {
4c9e3812
GE
1204 int write_size = ftdi->writebuffer_chunksize;
1205
1206 if (offset+write_size > size)
1207 write_size = size-offset;
1208
c201f80f 1209 ret = _usb_bulk_write_async(ftdi, ftdi->in_ep, buf+offset, write_size);
4c9e3812
GE
1210 if (ret < 0)
1211 ftdi_error_return(ret, "usb bulk write async failed");
1212
1213 total_written += ret;
1214 offset += write_size;
1215 }
1216
1217 return total_written;
1218}
f01d7ca6 1219#endif // LIBFTDI_LINUX_ASYNC_MODE
4c9e3812 1220
1941414d
TJ
1221/**
1222 Configure write buffer chunk size.
1223 Default is 4096.
1224
1225 \param ftdi pointer to ftdi_context
1226 \param chunksize Chunk size
a3da1d95 1227
1941414d
TJ
1228 \retval 0: all fine
1229*/
a8f46ddc
TJ
1230int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1231{
948f9ada
TJ
1232 ftdi->writebuffer_chunksize = chunksize;
1233 return 0;
1234}
1235
1941414d
TJ
1236/**
1237 Get write buffer chunk size.
1238
1239 \param ftdi pointer to ftdi_context
1240 \param chunksize Pointer to store chunk size in
948f9ada 1241
1941414d
TJ
1242 \retval 0: all fine
1243*/
a8f46ddc
TJ
1244int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1245{
948f9ada
TJ
1246 *chunksize = ftdi->writebuffer_chunksize;
1247 return 0;
1248}
cbabb7d3 1249
1941414d
TJ
1250/**
1251 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
1252
1253 Automatically strips the two modem status bytes transfered during every read.
948f9ada 1254
1941414d
TJ
1255 \param ftdi pointer to ftdi_context
1256 \param buf Buffer to store data in
1257 \param size Size of the buffer
1258
1259 \retval <0: error code from usb_bulk_read()
d77b0e94 1260 \retval 0: no data was available
1941414d
TJ
1261 \retval >0: number of bytes read
1262
1263 \remark This function is not useful in bitbang mode.
1264 Use ftdi_read_pins() to get the current state of the pins.
1265*/
a8f46ddc
TJ
1266int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
1267{
1c733d33 1268 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
f2f00cb5
DC
1269 int packet_size;
1270
1271 // New hi-speed devices from FTDI use a packet size of 512 bytes
1272 if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H)
1273 packet_size = 512;
1274 else
1275 packet_size = 64;
d9f0cce7 1276
948f9ada 1277 // everything we want is still in the readbuffer?
22d12cda
TJ
1278 if (size <= ftdi->readbuffer_remaining)
1279 {
d9f0cce7
TJ
1280 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
1281
1282 // Fix offsets
1283 ftdi->readbuffer_remaining -= size;
1284 ftdi->readbuffer_offset += size;
1285
545820ce 1286 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
1287
1288 return size;
979a145c 1289 }
948f9ada 1290 // something still in the readbuffer, but not enough to satisfy 'size'?
22d12cda
TJ
1291 if (ftdi->readbuffer_remaining != 0)
1292 {
d9f0cce7 1293 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
979a145c 1294
d9f0cce7
TJ
1295 // Fix offset
1296 offset += ftdi->readbuffer_remaining;
948f9ada 1297 }
948f9ada 1298 // do the actual USB read
22d12cda
TJ
1299 while (offset < size && ret > 0)
1300 {
d9f0cce7
TJ
1301 ftdi->readbuffer_remaining = 0;
1302 ftdi->readbuffer_offset = 0;
98452d97
TJ
1303 /* returns how much received */
1304 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
c3d95b87
TJ
1305 if (ret < 0)
1306 ftdi_error_return(ret, "usb bulk read failed");
98452d97 1307
22d12cda
TJ
1308 if (ret > 2)
1309 {
d9f0cce7
TJ
1310 // skip FTDI status bytes.
1311 // Maybe stored in the future to enable modem use
f2f00cb5
DC
1312 num_of_chunks = ret / packet_size;
1313 chunk_remains = ret % packet_size;
1c733d33
TJ
1314 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
1315
d9f0cce7
TJ
1316 ftdi->readbuffer_offset += 2;
1317 ret -= 2;
1c733d33 1318
f2f00cb5 1319 if (ret > packet_size - 2)
22d12cda 1320 {
1c733d33 1321 for (i = 1; i < num_of_chunks; i++)
f2f00cb5
DC
1322 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1323 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1324 packet_size - 2);
22d12cda
TJ
1325 if (chunk_remains > 2)
1326 {
f2f00cb5
DC
1327 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+(packet_size - 2)*i,
1328 ftdi->readbuffer+ftdi->readbuffer_offset+packet_size*i,
1c733d33
TJ
1329 chunk_remains-2);
1330 ret -= 2*num_of_chunks;
22d12cda
TJ
1331 }
1332 else
1c733d33
TJ
1333 ret -= 2*(num_of_chunks-1)+chunk_remains;
1334 }
22d12cda
TJ
1335 }
1336 else if (ret <= 2)
1337 {
d9f0cce7
TJ
1338 // no more data to read?
1339 return offset;
1340 }
22d12cda
TJ
1341 if (ret > 0)
1342 {
d9f0cce7 1343 // data still fits in buf?
22d12cda
TJ
1344 if (offset+ret <= size)
1345 {
d9f0cce7 1346 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
545820ce 1347 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
d9f0cce7
TJ
1348 offset += ret;
1349
53ad271d 1350 /* Did we read exactly the right amount of bytes? */
d9f0cce7 1351 if (offset == size)
c4446c36
TJ
1352 //printf("read_data exact rem %d offset %d\n",
1353 //ftdi->readbuffer_remaining, offset);
d9f0cce7 1354 return offset;
22d12cda
TJ
1355 }
1356 else
1357 {
d9f0cce7
TJ
1358 // only copy part of the data or size <= readbuffer_chunksize
1359 int part_size = size-offset;
1360 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
98452d97 1361
d9f0cce7
TJ
1362 ftdi->readbuffer_offset += part_size;
1363 ftdi->readbuffer_remaining = ret-part_size;
1364 offset += part_size;
1365
53ad271d
TJ
1366 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
1367 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
d9f0cce7
TJ
1368
1369 return offset;
1370 }
1371 }
cbabb7d3 1372 }
948f9ada 1373 // never reached
29c4af7f 1374 return -127;
a3da1d95
GE
1375}
1376
1941414d
TJ
1377/**
1378 Configure read buffer chunk size.
1379 Default is 4096.
1380
1381 Automatically reallocates the buffer.
a3da1d95 1382
1941414d
TJ
1383 \param ftdi pointer to ftdi_context
1384 \param chunksize Chunk size
1385
1386 \retval 0: all fine
1387*/
a8f46ddc
TJ
1388int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
1389{
29c4af7f
TJ
1390 unsigned char *new_buf;
1391
948f9ada
TJ
1392 // Invalidate all remaining data
1393 ftdi->readbuffer_offset = 0;
1394 ftdi->readbuffer_remaining = 0;
1395
c3d95b87
TJ
1396 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
1397 ftdi_error_return(-1, "out of memory for readbuffer");
d9f0cce7 1398
948f9ada
TJ
1399 ftdi->readbuffer = new_buf;
1400 ftdi->readbuffer_chunksize = chunksize;
1401
1402 return 0;
1403}
1404
1941414d
TJ
1405/**
1406 Get read buffer chunk size.
948f9ada 1407
1941414d
TJ
1408 \param ftdi pointer to ftdi_context
1409 \param chunksize Pointer to store chunk size in
1410
1411 \retval 0: all fine
1412*/
a8f46ddc
TJ
1413int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
1414{
948f9ada
TJ
1415 *chunksize = ftdi->readbuffer_chunksize;
1416 return 0;
1417}
1418
1419
1941414d
TJ
1420/**
1421 Enable bitbang mode.
948f9ada 1422
1941414d
TJ
1423 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
1424
1425 \param ftdi pointer to ftdi_context
1426 \param bitmask Bitmask to configure lines.
1427 HIGH/ON value configures a line as output.
1428
1429 \retval 0: all fine
1430 \retval -1: can't enable bitbang mode
1431*/
a8f46ddc
TJ
1432int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1433{
a3da1d95
GE
1434 unsigned short usb_val;
1435
d9f0cce7 1436 usb_val = bitmask; // low byte: bitmask
3119537f
TJ
1437 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1438 usb_val |= (ftdi->bitbang_mode << 8);
1439
22d12cda
TJ
1440 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
1441 SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index,
a5e1bd8c 1442 NULL, 0, ftdi->usb_write_timeout) != 0)
c3d95b87
TJ
1443 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1444
a3da1d95
GE
1445 ftdi->bitbang_enabled = 1;
1446 return 0;
1447}
1448
1941414d
TJ
1449/**
1450 Disable bitbang mode.
a3da1d95 1451
1941414d
TJ
1452 \param ftdi pointer to ftdi_context
1453
1454 \retval 0: all fine
1455 \retval -1: can't disable bitbang mode
1456*/
a8f46ddc
TJ
1457int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1458{
a5e1bd8c 1459 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
c3d95b87 1460 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
a3da1d95
GE
1461
1462 ftdi->bitbang_enabled = 0;
1463 return 0;
1464}
1465
1941414d
TJ
1466/**
1467 Enable advanced bitbang mode for FT2232C chips.
a3da1d95 1468
1941414d
TJ
1469 \param ftdi pointer to ftdi_context
1470 \param bitmask Bitmask to configure lines.
1471 HIGH/ON value configures a line as output.
1472 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
1473
1474 \retval 0: all fine
1475 \retval -1: can't enable bitbang mode
1476*/
c4446c36
TJ
1477int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1478{
1479 unsigned short usb_val;
1480
1481 usb_val = bitmask; // low byte: bitmask
1482 usb_val |= (mode << 8);
a5e1bd8c 1483 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_SET_BITMODE_REQUEST, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
c4446c36
TJ
1484 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1485
1486 ftdi->bitbang_mode = mode;
1487 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
1488 return 0;
1489}
1490
1941414d
TJ
1491/**
1492 Directly read pin state. Useful for bitbang mode.
1493
1494 \param ftdi pointer to ftdi_context
1495 \param pins Pointer to store pins into
1496
1497 \retval 0: all fine
1498 \retval -1: read pins failed
1499*/
a8f46ddc
TJ
1500int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1501{
a5e1bd8c 1502 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_PINS_REQUEST, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
c3d95b87 1503 ftdi_error_return(-1, "read pins failed");
a3da1d95 1504
a3da1d95
GE
1505 return 0;
1506}
1507
1941414d
TJ
1508/**
1509 Set latency timer
1510
1511 The FTDI chip keeps data in the internal buffer for a specific
1512 amount of time if the buffer is not full yet to decrease
1513 load on the usb bus.
a3da1d95 1514
1941414d
TJ
1515 \param ftdi pointer to ftdi_context
1516 \param latency Value between 1 and 255
1517
1518 \retval 0: all fine
1519 \retval -1: latency out of range
1520 \retval -2: unable to set latency timer
1521*/
a8f46ddc
TJ
1522int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1523{
a3da1d95
GE
1524 unsigned short usb_val;
1525
c3d95b87
TJ
1526 if (latency < 1)
1527 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
a3da1d95 1528
d79d2e68 1529 usb_val = latency;
a5e1bd8c 1530 if (usb_control_msg(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
1531 ftdi_error_return(-2, "unable to set latency timer");
1532
a3da1d95
GE
1533 return 0;
1534}
1535
1941414d
TJ
1536/**
1537 Get latency timer
a3da1d95 1538
1941414d
TJ
1539 \param ftdi pointer to ftdi_context
1540 \param latency Pointer to store latency value in
1541
1542 \retval 0: all fine
1543 \retval -1: unable to get latency timer
1544*/
a8f46ddc
TJ
1545int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1546{
a3da1d95 1547 unsigned short usb_val;
a5e1bd8c 1548 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_GET_LATENCY_TIMER_REQUEST, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
c3d95b87 1549 ftdi_error_return(-1, "reading latency timer failed");
a3da1d95
GE
1550
1551 *latency = (unsigned char)usb_val;
1552 return 0;
1553}
1554
1941414d 1555/**
1189b11a
TJ
1556 Poll modem status information
1557
1558 This function allows the retrieve the two status bytes of the device.
1559 The device sends these bytes also as a header for each read access
1560 where they are discarded by ftdi_read_data(). The chip generates
1561 the two stripped status bytes in the absence of data every 40 ms.
1562
1563 Layout of the first byte:
1564 - B0..B3 - must be 0
1565 - B4 Clear to send (CTS)
1566 0 = inactive
1567 1 = active
1568 - B5 Data set ready (DTS)
1569 0 = inactive
1570 1 = active
1571 - B6 Ring indicator (RI)
1572 0 = inactive
1573 1 = active
1574 - B7 Receive line signal detect (RLSD)
1575 0 = inactive
1576 1 = active
1577
1578 Layout of the second byte:
1579 - B0 Data ready (DR)
1580 - B1 Overrun error (OE)
1581 - B2 Parity error (PE)
1582 - B3 Framing error (FE)
1583 - B4 Break interrupt (BI)
1584 - B5 Transmitter holding register (THRE)
1585 - B6 Transmitter empty (TEMT)
1586 - B7 Error in RCVR FIFO
1587
1588 \param ftdi pointer to ftdi_context
1589 \param status Pointer to store status information in. Must be two bytes.
1590
1591 \retval 0: all fine
1592 \retval -1: unable to retrieve status information
1593*/
1594int ftdi_poll_modem_status(struct ftdi_context *ftdi, unsigned short *status)
1595{
1596 char usb_val[2];
1597
a5e1bd8c 1598 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_POLL_MODEM_STATUS_REQUEST, 0, ftdi->index, usb_val, 2, ftdi->usb_read_timeout) != 2)
1189b11a
TJ
1599 ftdi_error_return(-1, "getting modem status failed");
1600
1601 *status = (usb_val[1] << 8) | usb_val[0];
1602
1603 return 0;
1604}
1605
a7fb8440
TJ
1606/**
1607 Set flowcontrol for ftdi chip
1608
1609 \param ftdi pointer to ftdi_context
22d12cda
TJ
1610 \param flowctrl flow control to use. should be
1611 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
a7fb8440
TJ
1612
1613 \retval 0: all fine
1614 \retval -1: set flow control failed
1615*/
1616int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1617{
a5e1bd8c 1618 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
9ecfef2a 1619 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->index),
a7fb8440
TJ
1620 NULL, 0, ftdi->usb_write_timeout) != 0)
1621 ftdi_error_return(-1, "set flow control failed");
1622
1623 return 0;
1624}
1625
1626/**
1627 Set dtr line
1628
1629 \param ftdi pointer to ftdi_context
1630 \param state state to set line to (1 or 0)
1631
1632 \retval 0: all fine
1633 \retval -1: set dtr failed
1634*/
1635int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1636{
1637 unsigned short usb_val;
1638
1639 if (state)
1640 usb_val = SIO_SET_DTR_HIGH;
1641 else
1642 usb_val = SIO_SET_DTR_LOW;
1643
a5e1bd8c 1644 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
9ecfef2a 1645 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
a7fb8440
TJ
1646 NULL, 0, ftdi->usb_write_timeout) != 0)
1647 ftdi_error_return(-1, "set dtr failed");
1648
1649 return 0;
1650}
1651
1652/**
1653 Set rts line
1654
1655 \param ftdi pointer to ftdi_context
1656 \param state state to set line to (1 or 0)
1657
1658 \retval 0: all fine
1659 \retval -1 set rts failed
1660*/
1661int ftdi_setrts(struct ftdi_context *ftdi, int state)
1662{
1663 unsigned short usb_val;
1664
1665 if (state)
1666 usb_val = SIO_SET_RTS_HIGH;
1667 else
1668 usb_val = SIO_SET_RTS_LOW;
1669
a5e1bd8c 1670 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
9ecfef2a 1671 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
a7fb8440
TJ
1672 NULL, 0, ftdi->usb_write_timeout) != 0)
1673 ftdi_error_return(-1, "set of rts failed");
1674
1675 return 0;
1676}
1677
1189b11a 1678/**
9ecfef2a
TJ
1679 Set dtr and rts line in one pass
1680
1681 \param ftdi pointer to ftdi_context
1682 \param dtr DTR state to set line to (1 or 0)
1683 \param rts RTS state to set line to (1 or 0)
1684
1685 \retval 0: all fine
1686 \retval -1 set dtr/rts failed
1687 */
1688int ftdi_setdtr_rts(struct ftdi_context *ftdi, int dtr, int rts)
1689{
1690 unsigned short usb_val;
1691
1692 if (dtr)
22d12cda 1693 usb_val = SIO_SET_DTR_HIGH;
9ecfef2a 1694 else
22d12cda 1695 usb_val = SIO_SET_DTR_LOW;
9ecfef2a
TJ
1696
1697 if (rts)
22d12cda 1698 usb_val |= SIO_SET_RTS_HIGH;
9ecfef2a 1699 else
22d12cda 1700 usb_val |= SIO_SET_RTS_LOW;
9ecfef2a 1701
a5e1bd8c 1702 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
9ecfef2a
TJ
1703 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->index,
1704 NULL, 0, ftdi->usb_write_timeout) != 0)
22d12cda 1705 ftdi_error_return(-1, "set of rts/dtr failed");
9ecfef2a
TJ
1706
1707 return 0;
1708}
1709
1710/**
1189b11a
TJ
1711 Set the special event character
1712
1713 \param ftdi pointer to ftdi_context
1714 \param eventch Event character
1715 \param enable 0 to disable the event character, non-zero otherwise
1716
1717 \retval 0: all fine
1718 \retval -1: unable to set event character
1719*/
1720int ftdi_set_event_char(struct ftdi_context *ftdi,
22d12cda 1721 unsigned char eventch, unsigned char enable)
1189b11a
TJ
1722{
1723 unsigned short usb_val;
1724
1725 usb_val = eventch;
1726 if (enable)
1727 usb_val |= 1 << 8;
1728
a5e1bd8c 1729 if (usb_control_msg(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
1730 ftdi_error_return(-1, "setting event character failed");
1731
1732 return 0;
1733}
1734
1735/**
1736 Set error character
1737
1738 \param ftdi pointer to ftdi_context
1739 \param errorch Error character
1740 \param enable 0 to disable the error character, non-zero otherwise
1741
1742 \retval 0: all fine
1743 \retval -1: unable to set error character
1744*/
1745int ftdi_set_error_char(struct ftdi_context *ftdi,
22d12cda 1746 unsigned char errorch, unsigned char enable)
1189b11a
TJ
1747{
1748 unsigned short usb_val;
1749
1750 usb_val = errorch;
1751 if (enable)
1752 usb_val |= 1 << 8;
1753
a5e1bd8c 1754 if (usb_control_msg(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
1755 ftdi_error_return(-1, "setting error character failed");
1756
1757 return 0;
1758}
1759
1760/**
c201f80f
TJ
1761 Set the eeprom size
1762
1763 \param ftdi pointer to ftdi_context
1764 \param eeprom Pointer to ftdi_eeprom
1765 \param size
1766
1767*/
1768void ftdi_eeprom_setsize(struct ftdi_context *ftdi, struct ftdi_eeprom *eeprom, int size)
1769{
22d12cda
TJ
1770 ftdi->eeprom_size=size;
1771 eeprom->size=size;
c201f80f
TJ
1772}
1773
1774/**
1941414d 1775 Init eeprom with default values.
a3da1d95 1776
1941414d
TJ
1777 \param eeprom Pointer to ftdi_eeprom
1778*/
a8f46ddc
TJ
1779void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
1780{
f396dbad
TJ
1781 eeprom->vendor_id = 0x0403;
1782 eeprom->product_id = 0x6001;
d9f0cce7 1783
b8aa7b35
TJ
1784 eeprom->self_powered = 1;
1785 eeprom->remote_wakeup = 1;
1786 eeprom->BM_type_chip = 1;
d9f0cce7 1787
b8aa7b35
TJ
1788 eeprom->in_is_isochronous = 0;
1789 eeprom->out_is_isochronous = 0;
1790 eeprom->suspend_pull_downs = 0;
d9f0cce7 1791
b8aa7b35
TJ
1792 eeprom->use_serial = 0;
1793 eeprom->change_usb_version = 0;
f396dbad 1794 eeprom->usb_version = 0x0200;
b8aa7b35 1795 eeprom->max_power = 0;
d9f0cce7 1796
b8aa7b35
TJ
1797 eeprom->manufacturer = NULL;
1798 eeprom->product = NULL;
1799 eeprom->serial = NULL;
c201f80f
TJ
1800
1801 eeprom->size = FTDI_DEFAULT_EEPROM_SIZE;
b8aa7b35
TJ
1802}
1803
1941414d
TJ
1804/**
1805 Build binary output from ftdi_eeprom structure.
1806 Output is suitable for ftdi_write_eeprom().
b8aa7b35 1807
1941414d
TJ
1808 \param eeprom Pointer to ftdi_eeprom
1809 \param output Buffer of 128 bytes to store eeprom image to
1810
1811 \retval >0: used eeprom size
1812 \retval -1: eeprom size (128 bytes) exceeded by custom strings
b8aa7b35 1813*/
a8f46ddc
TJ
1814int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
1815{
b8aa7b35
TJ
1816 unsigned char i, j;
1817 unsigned short checksum, value;
1818 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
1819 int size_check;
1820
1821 if (eeprom->manufacturer != NULL)
d9f0cce7 1822 manufacturer_size = strlen(eeprom->manufacturer);
b8aa7b35 1823 if (eeprom->product != NULL)
d9f0cce7 1824 product_size = strlen(eeprom->product);
b8aa7b35 1825 if (eeprom->serial != NULL)
d9f0cce7 1826 serial_size = strlen(eeprom->serial);
b8aa7b35 1827
c201f80f 1828 size_check = eeprom->size;
d9f0cce7 1829 size_check -= 28; // 28 are always in use (fixed)
c201f80f 1830
22d12cda 1831 // Top half of a 256byte eeprom is used just for strings and checksum
c201f80f
TJ
1832 // it seems that the FTDI chip will not read these strings from the lower half
1833 // Each string starts with two bytes; offset and type (0x03 for string)
1834 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
22d12cda 1835 if (eeprom->size>=256)size_check = 120;
b8aa7b35
TJ
1836 size_check -= manufacturer_size*2;
1837 size_check -= product_size*2;
1838 size_check -= serial_size*2;
1839
1840 // eeprom size exceeded?
1841 if (size_check < 0)
d9f0cce7 1842 return (-1);
b8aa7b35
TJ
1843
1844 // empty eeprom
c201f80f 1845 memset (output, 0, eeprom->size);
b8aa7b35
TJ
1846
1847 // Addr 00: Stay 00 00
1848 // Addr 02: Vendor ID
1849 output[0x02] = eeprom->vendor_id;
1850 output[0x03] = eeprom->vendor_id >> 8;
1851
1852 // Addr 04: Product ID
1853 output[0x04] = eeprom->product_id;
1854 output[0x05] = eeprom->product_id >> 8;
1855
1856 // Addr 06: Device release number (0400h for BM features)
1857 output[0x06] = 0x00;
d9f0cce7 1858
b8aa7b35 1859 if (eeprom->BM_type_chip == 1)
d9f0cce7 1860 output[0x07] = 0x04;
b8aa7b35 1861 else
d9f0cce7 1862 output[0x07] = 0x02;
b8aa7b35
TJ
1863
1864 // Addr 08: Config descriptor
8fae3e8e
TJ
1865 // Bit 7: always 1
1866 // Bit 6: 1 if this device is self powered, 0 if bus powered
1867 // Bit 5: 1 if this device uses remote wakeup
1868 // Bit 4: 1 if this device is battery powered
5a1dcd55 1869 j = 0x80;
b8aa7b35 1870 if (eeprom->self_powered == 1)
5a1dcd55 1871 j |= 0x40;
b8aa7b35 1872 if (eeprom->remote_wakeup == 1)
5a1dcd55 1873 j |= 0x20;
b8aa7b35
TJ
1874 output[0x08] = j;
1875
1876 // Addr 09: Max power consumption: max power = value * 2 mA
d9f0cce7 1877 output[0x09] = eeprom->max_power;
d9f0cce7 1878
b8aa7b35
TJ
1879 // Addr 0A: Chip configuration
1880 // Bit 7: 0 - reserved
1881 // Bit 6: 0 - reserved
1882 // Bit 5: 0 - reserved
1883 // Bit 4: 1 - Change USB version
1884 // Bit 3: 1 - Use the serial number string
1885 // Bit 2: 1 - Enable suspend pull downs for lower power
1886 // Bit 1: 1 - Out EndPoint is Isochronous
1887 // Bit 0: 1 - In EndPoint is Isochronous
1888 //
1889 j = 0;
1890 if (eeprom->in_is_isochronous == 1)
d9f0cce7 1891 j = j | 1;
b8aa7b35 1892 if (eeprom->out_is_isochronous == 1)
d9f0cce7 1893 j = j | 2;
b8aa7b35 1894 if (eeprom->suspend_pull_downs == 1)
d9f0cce7 1895 j = j | 4;
b8aa7b35 1896 if (eeprom->use_serial == 1)
d9f0cce7 1897 j = j | 8;
b8aa7b35 1898 if (eeprom->change_usb_version == 1)
d9f0cce7 1899 j = j | 16;
b8aa7b35 1900 output[0x0A] = j;
d9f0cce7 1901
b8aa7b35
TJ
1902 // Addr 0B: reserved
1903 output[0x0B] = 0x00;
d9f0cce7 1904
b8aa7b35
TJ
1905 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1906 // Addr 0D: USB version high byte when 0x0A bit 4 is set
22d12cda
TJ
1907 if (eeprom->change_usb_version == 1)
1908 {
b8aa7b35 1909 output[0x0C] = eeprom->usb_version;
d9f0cce7 1910 output[0x0D] = eeprom->usb_version >> 8;
b8aa7b35
TJ
1911 }
1912
1913
c201f80f 1914 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
b8aa7b35
TJ
1915 // Addr 0F: Length of manufacturer string
1916 output[0x0F] = manufacturer_size*2 + 2;
1917
1918 // Addr 10: Offset of the product string + 0x80, calculated later
1919 // Addr 11: Length of product string
1920 output[0x11] = product_size*2 + 2;
1921
1922 // Addr 12: Offset of the serial string + 0x80, calculated later
1923 // Addr 13: Length of serial string
1924 output[0x13] = serial_size*2 + 2;
1925
1926 // Dynamic content
c201f80f 1927 i=0x14;
22d12cda 1928 if (eeprom->size>=256) i = 0x80;
f01d7ca6 1929
c201f80f 1930
22d12cda 1931 // Output manufacturer
c201f80f
TJ
1932 output[0x0E] = i | 0x80; // calculate offset
1933 output[i++] = manufacturer_size*2 + 2;
1934 output[i++] = 0x03; // type: string
22d12cda
TJ
1935 for (j = 0; j < manufacturer_size; j++)
1936 {
d9f0cce7
TJ
1937 output[i] = eeprom->manufacturer[j], i++;
1938 output[i] = 0x00, i++;
b8aa7b35
TJ
1939 }
1940
1941 // Output product name
c201f80f 1942 output[0x10] = i | 0x80; // calculate offset
b8aa7b35
TJ
1943 output[i] = product_size*2 + 2, i++;
1944 output[i] = 0x03, i++;
22d12cda
TJ
1945 for (j = 0; j < product_size; j++)
1946 {
d9f0cce7
TJ
1947 output[i] = eeprom->product[j], i++;
1948 output[i] = 0x00, i++;
b8aa7b35 1949 }
d9f0cce7 1950
b8aa7b35 1951 // Output serial
c201f80f 1952 output[0x12] = i | 0x80; // calculate offset
b8aa7b35
TJ
1953 output[i] = serial_size*2 + 2, i++;
1954 output[i] = 0x03, i++;
22d12cda
TJ
1955 for (j = 0; j < serial_size; j++)
1956 {
d9f0cce7
TJ
1957 output[i] = eeprom->serial[j], i++;
1958 output[i] = 0x00, i++;
b8aa7b35
TJ
1959 }
1960
1961 // calculate checksum
1962 checksum = 0xAAAA;
d9f0cce7 1963
22d12cda
TJ
1964 for (i = 0; i < eeprom->size/2-1; i++)
1965 {
d9f0cce7
TJ
1966 value = output[i*2];
1967 value += output[(i*2)+1] << 8;
b8aa7b35 1968
d9f0cce7
TJ
1969 checksum = value^checksum;
1970 checksum = (checksum << 1) | (checksum >> 15);
b8aa7b35
TJ
1971 }
1972
c201f80f
TJ
1973 output[eeprom->size-2] = checksum;
1974 output[eeprom->size-1] = checksum >> 8;
b8aa7b35 1975
8ed61121 1976 return size_check;
b8aa7b35
TJ
1977}
1978
4af1d1bb
MK
1979/**
1980 Decode binary EEPROM image into an ftdi_eeprom structure.
1981
1982 \param eeprom Pointer to ftdi_eeprom which will be filled in.
1bbaf1ce 1983 \param buf Buffer of \a size bytes of raw eeprom data
4af1d1bb
MK
1984 \param size size size of eeprom data in bytes
1985
1986 \retval 0: all fine
1987 \retval -1: something went wrong
1988
1989 FIXME: How to pass size? How to handle size field in ftdi_eeprom?
1990 FIXME: Strings are malloc'ed here and should be freed somewhere
1991*/
49c5ac72 1992int ftdi_eeprom_decode(struct ftdi_eeprom *eeprom, unsigned char *buf, int size)
b56d5a64
MK
1993{
1994 unsigned char i, j;
1995 unsigned short checksum, eeprom_checksum, value;
1996 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
1997 int size_check;
1998 int eeprom_size = 128;
1999#if 0
2000 size_check = eeprom->size;
2001 size_check -= 28; // 28 are always in use (fixed)
2002
22d12cda 2003 // Top half of a 256byte eeprom is used just for strings and checksum
b56d5a64
MK
2004 // it seems that the FTDI chip will not read these strings from the lower half
2005 // Each string starts with two bytes; offset and type (0x03 for string)
2006 // the checksum needs two bytes, so without the string data that 8 bytes from the top half
22d12cda 2007 if (eeprom->size>=256)size_check = 120;
b56d5a64
MK
2008 size_check -= manufacturer_size*2;
2009 size_check -= product_size*2;
2010 size_check -= serial_size*2;
2011
2012 // eeprom size exceeded?
2013 if (size_check < 0)
2014 return (-1);
2015#endif
2016
2017 // empty eeprom struct
4af1d1bb 2018 memset(eeprom, 0, sizeof(struct ftdi_eeprom));
b56d5a64
MK
2019
2020 // Addr 00: Stay 00 00
2021
2022 // Addr 02: Vendor ID
2023 eeprom->vendor_id = buf[0x02] + (buf[0x03] << 8);
2024
2025 // Addr 04: Product ID
2026 eeprom->product_id = buf[0x04] + (buf[0x05] << 8);
22d12cda 2027
6335545d
TJ
2028 value = buf[0x06] + (buf[0x07]<<8);
2029 switch (value)
22d12cda
TJ
2030 {
2031 case 0x0400:
2032 eeprom->BM_type_chip = 1;
2033 break;
2034 case 0x0200:
2035 eeprom->BM_type_chip = 0;
2036 break;
2037 default: // Unknown device
2038 eeprom->BM_type_chip = 0;
2039 break;
4af1d1bb 2040 }
b56d5a64
MK
2041
2042 // Addr 08: Config descriptor
2043 // Bit 7: always 1
2044 // Bit 6: 1 if this device is self powered, 0 if bus powered
2045 // Bit 5: 1 if this device uses remote wakeup
2046 // Bit 4: 1 if this device is battery powered
2047 j = buf[0x08];
b56d5a64
MK
2048 if (j&0x40) eeprom->self_powered = 1;
2049 if (j&0x20) eeprom->remote_wakeup = 1;
2050
2051 // Addr 09: Max power consumption: max power = value * 2 mA
2052 eeprom->max_power = buf[0x09];
2053
2054 // Addr 0A: Chip configuration
2055 // Bit 7: 0 - reserved
2056 // Bit 6: 0 - reserved
2057 // Bit 5: 0 - reserved
2058 // Bit 4: 1 - Change USB version
2059 // Bit 3: 1 - Use the serial number string
2060 // Bit 2: 1 - Enable suspend pull downs for lower power
2061 // Bit 1: 1 - Out EndPoint is Isochronous
2062 // Bit 0: 1 - In EndPoint is Isochronous
2063 //
2064 j = buf[0x0A];
4af1d1bb
MK
2065 if (j&0x01) eeprom->in_is_isochronous = 1;
2066 if (j&0x02) eeprom->out_is_isochronous = 1;
2067 if (j&0x04) eeprom->suspend_pull_downs = 1;
2068 if (j&0x08) eeprom->use_serial = 1;
2069 if (j&0x10) eeprom->change_usb_version = 1;
b56d5a64 2070
4af1d1bb 2071 // Addr 0B: reserved
b56d5a64
MK
2072
2073 // Addr 0C: USB version low byte when 0x0A bit 4 is set
2074 // Addr 0D: USB version high byte when 0x0A bit 4 is set
22d12cda
TJ
2075 if (eeprom->change_usb_version == 1)
2076 {
2077 eeprom->usb_version = buf[0x0C] + (buf[0x0D] << 8);
b56d5a64
MK
2078 }
2079
2080 // Addr 0E: Offset of the manufacturer string + 0x80, calculated later
2081 // Addr 0F: Length of manufacturer string
2082 manufacturer_size = buf[0x0F]/2;
2083 if (manufacturer_size > 0) eeprom->manufacturer = malloc(manufacturer_size);
2084 else eeprom->manufacturer = NULL;
2085
2086 // Addr 10: Offset of the product string + 0x80, calculated later
2087 // Addr 11: Length of product string
2088 product_size = buf[0x11]/2;
2089 if (product_size > 0) eeprom->product = malloc(product_size);
2090 else eeprom->product = NULL;
2091
2092 // Addr 12: Offset of the serial string + 0x80, calculated later
2093 // Addr 13: Length of serial string
2094 serial_size = buf[0x13]/2;
2095 if (serial_size > 0) eeprom->serial = malloc(serial_size);
2096 else eeprom->serial = NULL;
2097
22d12cda 2098 // Decode manufacturer
b56d5a64 2099 i = buf[0x0E] & 0x7f; // offset
22d12cda
TJ
2100 for (j=0;j<manufacturer_size-1;j++)
2101 {
2102 eeprom->manufacturer[j] = buf[2*j+i+2];
b56d5a64
MK
2103 }
2104 eeprom->manufacturer[j] = '\0';
2105
2106 // Decode product name
2107 i = buf[0x10] & 0x7f; // offset
22d12cda
TJ
2108 for (j=0;j<product_size-1;j++)
2109 {
2110 eeprom->product[j] = buf[2*j+i+2];
b56d5a64
MK
2111 }
2112 eeprom->product[j] = '\0';
2113
2114 // Decode serial
2115 i = buf[0x12] & 0x7f; // offset
22d12cda
TJ
2116 for (j=0;j<serial_size-1;j++)
2117 {
2118 eeprom->serial[j] = buf[2*j+i+2];
b56d5a64
MK
2119 }
2120 eeprom->serial[j] = '\0';
2121
2122 // verify checksum
2123 checksum = 0xAAAA;
2124
22d12cda
TJ
2125 for (i = 0; i < eeprom_size/2-1; i++)
2126 {
b56d5a64
MK
2127 value = buf[i*2];
2128 value += buf[(i*2)+1] << 8;
2129
2130 checksum = value^checksum;
2131 checksum = (checksum << 1) | (checksum >> 15);
2132 }
2133
2134 eeprom_checksum = buf[eeprom_size-2] + (buf[eeprom_size-1] << 8);
2135
22d12cda
TJ
2136 if (eeprom_checksum != checksum)
2137 {
2138 fprintf(stderr, "Checksum Error: %04x %04x\n", checksum, eeprom_checksum);
2139 return -1;
4af1d1bb
MK
2140 }
2141
2142 return 0;
b56d5a64
MK
2143}
2144
1941414d 2145/**
c1c70e13
OS
2146 Read eeprom location
2147
2148 \param ftdi pointer to ftdi_context
2149 \param eeprom_addr Address of eeprom location to be read
2150 \param eeprom_val Pointer to store read eeprom location
2151
2152 \retval 0: all fine
2153 \retval -1: read failed
2154*/
2155int ftdi_read_eeprom_location (struct ftdi_context *ftdi, int eeprom_addr, unsigned short *eeprom_val)
2156{
2157 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, eeprom_addr, (char *)eeprom_val, 2, ftdi->usb_read_timeout) != 2)
2158 ftdi_error_return(-1, "reading eeprom failed");
2159
2160 return 0;
2161}
2162
2163/**
1941414d
TJ
2164 Read eeprom
2165
2166 \param ftdi pointer to ftdi_context
2167 \param eeprom Pointer to store eeprom into
b8aa7b35 2168
1941414d
TJ
2169 \retval 0: all fine
2170 \retval -1: read failed
2171*/
a8f46ddc
TJ
2172int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2173{
a3da1d95
GE
2174 int i;
2175
22d12cda
TJ
2176 for (i = 0; i < ftdi->eeprom_size/2; i++)
2177 {
a5e1bd8c 2178 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
c3d95b87 2179 ftdi_error_return(-1, "reading eeprom failed");
a3da1d95
GE
2180 }
2181
2182 return 0;
2183}
2184
cb6250fa
TJ
2185/*
2186 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
2187 Function is only used internally
2188 \internal
2189*/
2190static unsigned char ftdi_read_chipid_shift(unsigned char value)
2191{
2192 return ((value & 1) << 1) |
22d12cda
TJ
2193 ((value & 2) << 5) |
2194 ((value & 4) >> 2) |
2195 ((value & 8) << 4) |
2196 ((value & 16) >> 1) |
2197 ((value & 32) >> 1) |
2198 ((value & 64) >> 4) |
2199 ((value & 128) >> 2);
cb6250fa
TJ
2200}
2201
2202/**
2203 Read the FTDIChip-ID from R-type devices
2204
2205 \param ftdi pointer to ftdi_context
2206 \param chipid Pointer to store FTDIChip-ID
2207
2208 \retval 0: all fine
2209 \retval -1: read failed
2210*/
2211int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
2212{
c7eb3112 2213 unsigned int a = 0, b = 0;
cb6250fa 2214
a5e1bd8c 2215 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2)
cb6250fa
TJ
2216 {
2217 a = a << 8 | a >> 8;
a5e1bd8c 2218 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE, SIO_READ_EEPROM_REQUEST, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2)
cb6250fa
TJ
2219 {
2220 b = b << 8 | b >> 8;
5230676f 2221 a = (a << 16) | (b & 0xFFFF);
912d50ca
TJ
2222 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
2223 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
cb6250fa 2224 *chipid = a ^ 0xa5f0f7d1;
c7eb3112 2225 return 0;
cb6250fa
TJ
2226 }
2227 }
2228
c7eb3112 2229 ftdi_error_return(-1, "read of FTDIChip-ID failed");
cb6250fa
TJ
2230}
2231
1941414d 2232/**
c201f80f
TJ
2233 Guesses size of eeprom by reading eeprom and comparing halves - will not work with blank eeprom
2234 Call this function then do a write then call again to see if size changes, if so write again.
2235
2236 \param ftdi pointer to ftdi_context
2237 \param eeprom Pointer to store eeprom into
2238 \param maxsize the size of the buffer to read into
2239
2240 \retval size of eeprom
2241*/
2242int ftdi_read_eeprom_getsize(struct ftdi_context *ftdi, unsigned char *eeprom, int maxsize)
2243{
2244 int i=0,j,minsize=32;
2245 int size=minsize;
2246
22d12cda
TJ
2247 do
2248 {
2249 for (j = 0; i < maxsize/2 && j<size; j++)
2250 {
2251 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_IN_REQTYPE,
2252 SIO_READ_EEPROM_REQUEST, 0, i,
2253 eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
2254 ftdi_error_return(-1, "reading eeprom failed");
2255 i++;
2256 }
2257 size*=2;
2258 }
2259 while (size<=maxsize && memcmp(eeprom,&eeprom[size/2],size/2)!=0);
c201f80f
TJ
2260
2261 return size/2;
2262}
2263
2264/**
c1c70e13
OS
2265 Write eeprom location
2266
2267 \param ftdi pointer to ftdi_context
2268 \param eeprom_addr Address of eeprom location to be written
2269 \param eeprom_val Value to be written
2270
2271 \retval 0: all fine
2272 \retval -1: read failed
2273*/
2274int ftdi_write_eeprom_location(struct ftdi_context *ftdi, int eeprom_addr, unsigned short eeprom_val)
2275{
2276 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
2277 SIO_WRITE_EEPROM_REQUEST, eeprom_val, eeprom_addr,
2278 NULL, 0, ftdi->usb_write_timeout) != 0)
2279 ftdi_error_return(-1, "unable to write eeprom");
2280
2281 return 0;
2282}
2283
2284/**
1941414d 2285 Write eeprom
a3da1d95 2286
1941414d
TJ
2287 \param ftdi pointer to ftdi_context
2288 \param eeprom Pointer to read eeprom from
2289
2290 \retval 0: all fine
2291 \retval -1: read failed
2292*/
a8f46ddc
TJ
2293int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
2294{
ba5329be 2295 unsigned short usb_val, status;
e30da501 2296 int i, ret;
a3da1d95 2297
ba5329be 2298 /* These commands were traced while running MProg */
e30da501
TJ
2299 if ((ret = ftdi_usb_reset(ftdi)) != 0)
2300 return ret;
2301 if ((ret = ftdi_poll_modem_status(ftdi, &status)) != 0)
2302 return ret;
2303 if ((ret = ftdi_set_latency_timer(ftdi, 0x77)) != 0)
2304 return ret;
ba5329be 2305
22d12cda
TJ
2306 for (i = 0; i < ftdi->eeprom_size/2; i++)
2307 {
d9f0cce7
TJ
2308 usb_val = eeprom[i*2];
2309 usb_val += eeprom[(i*2)+1] << 8;
a5e1bd8c 2310 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
22d12cda 2311 SIO_WRITE_EEPROM_REQUEST, usb_val, i,
a5e1bd8c 2312 NULL, 0, ftdi->usb_write_timeout) != 0)
c3d95b87 2313 ftdi_error_return(-1, "unable to write eeprom");
a3da1d95
GE
2314 }
2315
2316 return 0;
2317}
2318
1941414d
TJ
2319/**
2320 Erase eeprom
a3da1d95 2321
a5e1bd8c
MK
2322 This is not supported on FT232R/FT245R according to the MProg manual from FTDI.
2323
1941414d
TJ
2324 \param ftdi pointer to ftdi_context
2325
2326 \retval 0: all fine
2327 \retval -1: erase failed
2328*/
a8f46ddc
TJ
2329int ftdi_erase_eeprom(struct ftdi_context *ftdi)
2330{
a5e1bd8c 2331 if (usb_control_msg(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE, SIO_ERASE_EEPROM_REQUEST, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
c3d95b87 2332 ftdi_error_return(-1, "unable to erase eeprom");
a3da1d95
GE
2333
2334 return 0;
2335}
c3d95b87 2336
1941414d
TJ
2337/**
2338 Get string representation for last error code
c3d95b87 2339
1941414d
TJ
2340 \param ftdi pointer to ftdi_context
2341
2342 \retval Pointer to error string
2343*/
c3d95b87
TJ
2344char *ftdi_get_error_string (struct ftdi_context *ftdi)
2345{
2346 return ftdi->error_str;
2347}
a01d31e2 2348
b5ec1820 2349/* @} end of doxygen libftdi group */