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