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