libftdi: (gerd) add ftdi_write_data_async(), needed to work around high latency of...
[libftdi] / src / ftdi.c
... / ...
CommitLineData
1/***************************************************************************
2 ftdi.c - description
3 -------------------
4 begin : Fri Apr 4 2003
5 copyright : (C) 2003 by Intra2net AG
6 email : opensource@intra2net.com
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 ***************************************************************************/
16
17/**
18 \mainpage libftdi API documentation
19
20 Library to talk to FTDI chips. You find the latest versions of libftdi at
21 http://www.intra2net.com/de/produkte/opensource/ftdi/
22
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.
27*/
28/** \addtogroup libftdi */
29/* @{ */
30
31#include <usb.h>
32#include <string.h>
33#include <errno.h>
34#include <sys/ioctl.h>
35
36#include "ftdi.h"
37
38#define ftdi_error_return(code, str) do { \
39 ftdi->error_str = str; \
40 return code; \
41 } while(0);
42
43
44/**
45 Initializes a ftdi_context.
46
47 \param ftdi pointer to ftdi_context
48
49 \retval 0: all fine
50 \retval -1: couldn't allocate read buffer
51
52 \remark This should be called before all functions
53*/
54int ftdi_init(struct ftdi_context *ftdi)
55{
56 ftdi->usb_dev = NULL;
57 ftdi->usb_read_timeout = 5000;
58 ftdi->usb_write_timeout = 5000;
59
60 ftdi->type = TYPE_BM; /* chip type */
61 ftdi->baudrate = -1;
62 ftdi->bitbang_enabled = 0;
63
64 ftdi->readbuffer = NULL;
65 ftdi->readbuffer_offset = 0;
66 ftdi->readbuffer_remaining = 0;
67 ftdi->writebuffer_chunksize = 4096;
68
69 ftdi->interface = 0;
70 ftdi->index = 0;
71 ftdi->in_ep = 0x02;
72 ftdi->out_ep = 0x81;
73 ftdi->bitbang_mode = 1; /* 1: Normal bitbang mode, 2: SPI bitbang mode */
74
75 ftdi->error_str = NULL;
76
77 /* All fine. Now allocate the readbuffer */
78 return ftdi_read_data_set_chunksize(ftdi, 4096);
79}
80
81/**
82 Open selected channels on a chip, otherwise use first channel.
83
84 \param ftdi pointer to ftdi_context
85 \param interface Interface to use for FT2232C chips.
86
87 \retval 0: all fine
88 \retval -1: unknown interface
89*/
90int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
91{
92 switch (interface) {
93 case INTERFACE_ANY:
94 case INTERFACE_A:
95 /* ftdi_usb_open_desc cares to set the right index, depending on the found chip */
96 break;
97 case INTERFACE_B:
98 ftdi->interface = 1;
99 ftdi->index = INTERFACE_B;
100 ftdi->in_ep = 0x04;
101 ftdi->out_ep = 0x83;
102 break;
103 default:
104 ftdi_error_return(-1, "Unknown interface");
105 }
106 return 0;
107}
108
109/**
110 Deinitializes a ftdi_context.
111
112 \param ftdi pointer to ftdi_context
113*/
114void ftdi_deinit(struct ftdi_context *ftdi)
115{
116 if (ftdi->readbuffer != NULL) {
117 free(ftdi->readbuffer);
118 ftdi->readbuffer = NULL;
119 }
120}
121
122/**
123 Use an already open libusb device.
124
125 \param ftdi pointer to ftdi_context
126 \param usb libusb usb_dev_handle to use
127*/
128void ftdi_set_usbdev (struct ftdi_context *ftdi, usb_dev_handle *usb)
129{
130 ftdi->usb_dev = usb;
131}
132
133
134/**
135 Finds all ftdi devices on the usb bus. Creates a new ftdi_device_list which
136 needs to be deallocated by ftdi_list_free() after use.
137
138 \param ftdi pointer to ftdi_context
139 \param devlist Pointer where to store list of found devices
140 \param vendor Vendor ID to search for
141 \param product Product ID to search for
142
143 \retval >0: number of devices found
144 \retval -1: usb_find_busses() failed
145 \retval -2: usb_find_devices() failed
146 \retval -3: out of memory
147*/
148int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
149{
150 struct ftdi_device_list **curdev;
151 struct usb_bus *bus;
152 struct usb_device *dev;
153 int count = 0;
154
155 usb_init();
156 if (usb_find_busses() < 0)
157 ftdi_error_return(-1, "usb_find_busses() failed");
158 if (usb_find_devices() < 0)
159 ftdi_error_return(-2, "usb_find_devices() failed");
160
161 curdev = devlist;
162 *curdev = NULL;
163 for (bus = usb_busses; bus; bus = bus->next) {
164 for (dev = bus->devices; dev; dev = dev->next) {
165 if (dev->descriptor.idVendor == vendor
166 && dev->descriptor.idProduct == product)
167 {
168 *curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
169 if (!*curdev)
170 ftdi_error_return(-3, "out of memory");
171
172 (*curdev)->next = NULL;
173 (*curdev)->dev = dev;
174
175 curdev = &(*curdev)->next;
176 count++;
177 }
178 }
179 }
180
181 return count;
182}
183
184/**
185 Frees a usb device list.
186
187 \param devlist USB device list created by ftdi_usb_find_all()
188*/
189void ftdi_list_free(struct ftdi_device_list **devlist)
190{
191 struct ftdi_device_list *curdev, *next;
192
193 for (curdev = *devlist; curdev != NULL;) {
194 next = curdev->next;
195 free(curdev);
196 curdev = next;
197 }
198
199 *devlist = NULL;
200}
201
202/**
203 Return device ID strings from the usb device.
204
205 The parameters manufacturer, description and serial may be NULL
206 or pointer to buffers to store the fetched strings.
207
208 \note Use this function only in combination with ftdi_usb_find_all()
209 as it closes the internal "usb_dev" after use.
210
211 \param ftdi pointer to ftdi_context
212 \param dev libusb usb_dev to use
213 \param manufacturer Store manufacturer string here if not NULL
214 \param mnf_len Buffer size of manufacturer string
215 \param description Store product description string here if not NULL
216 \param desc_len Buffer size of product description string
217 \param serial Store serial string here if not NULL
218 \param serial_len Buffer size of serial string
219
220 \retval 0: all fine
221 \retval -1: wrong arguments
222 \retval -4: unable to open device
223 \retval -7: get product manufacturer failed
224 \retval -8: get product description failed
225 \retval -9: get serial number failed
226 \retval -10: unable to close device
227*/
228int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct usb_device * dev,
229 char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
230{
231 if ((ftdi==NULL) || (dev==NULL))
232 return -1;
233
234 if (!(ftdi->usb_dev = usb_open(dev)))
235 ftdi_error_return(-4, usb_strerror());
236
237 if (manufacturer != NULL) {
238 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iManufacturer, manufacturer, mnf_len) <= 0) {
239 usb_close (ftdi->usb_dev);
240 ftdi_error_return(-7, usb_strerror());
241 }
242 }
243
244 if (description != NULL) {
245 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, description, desc_len) <= 0) {
246 usb_close (ftdi->usb_dev);
247 ftdi_error_return(-8, usb_strerror());
248 }
249 }
250
251 if (serial != NULL) {
252 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, serial, serial_len) <= 0) {
253 usb_close (ftdi->usb_dev);
254 ftdi_error_return(-9, usb_strerror());
255 }
256 }
257
258 if (usb_close (ftdi->usb_dev) != 0)
259 ftdi_error_return(-10, usb_strerror());
260
261 return 0;
262}
263
264/**
265 Opens a ftdi device given by a usb_device.
266
267 \param ftdi pointer to ftdi_context
268 \param dev libusb usb_dev to use
269
270 \retval 0: all fine
271 \retval -4: unable to open device
272 \retval -5: unable to claim device
273 \retval -6: reset failed
274 \retval -7: set baudrate failed
275*/
276int ftdi_usb_open_dev(struct ftdi_context *ftdi, struct usb_device *dev)
277{
278 int detach_errno = 0;
279 if (!(ftdi->usb_dev = usb_open(dev)))
280 ftdi_error_return(-4, "usb_open() failed");
281
282#ifdef LIBUSB_HAS_GET_DRIVER_NP
283 // Try to detach ftdi_sio kernel module
284 // Returns ENODATA if driver is not loaded
285 if (usb_detach_kernel_driver_np(ftdi->usb_dev, ftdi->interface) != 0 && errno != ENODATA)
286 detach_errno = errno;
287#endif
288
289 if (usb_claim_interface(ftdi->usb_dev, ftdi->interface) != 0) {
290 usb_close (ftdi->usb_dev);
291 if (detach_errno == EPERM) {
292 ftdi_error_return(-8, "inappropriate permissions on device!");
293 } else {
294 ftdi_error_return(-5, "unable to claim usb device. Make sure ftdi_sio is unloaded!");
295 }
296 }
297
298 if (ftdi_usb_reset (ftdi) != 0) {
299 usb_close (ftdi->usb_dev);
300 ftdi_error_return(-6, "ftdi_usb_reset failed");
301 }
302
303 if (ftdi_set_baudrate (ftdi, 9600) != 0) {
304 usb_close (ftdi->usb_dev);
305 ftdi_error_return(-7, "set baudrate failed");
306 }
307
308 // Try to guess chip type
309 // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
310 if (dev->descriptor.bcdDevice == 0x400 || (dev->descriptor.bcdDevice == 0x200
311 && dev->descriptor.iSerialNumber == 0))
312 ftdi->type = TYPE_BM;
313 else if (dev->descriptor.bcdDevice == 0x200)
314 ftdi->type = TYPE_AM;
315 else if (dev->descriptor.bcdDevice == 0x500) {
316 ftdi->type = TYPE_2232C;
317 if (!ftdi->index)
318 ftdi->index = INTERFACE_A;
319 } else if (dev->descriptor.bcdDevice == 0x600)
320 ftdi->type = TYPE_R;
321
322 ftdi_error_return(0, "all fine");
323}
324
325/**
326 Opens the first device with a given vendor and product ids.
327
328 \param ftdi pointer to ftdi_context
329 \param vendor Vendor ID
330 \param product Product ID
331
332 \retval same as ftdi_usb_open_desc()
333*/
334int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
335{
336 return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
337}
338
339/**
340 Opens the first device with a given, vendor id, product id,
341 description and serial.
342
343 \param ftdi pointer to ftdi_context
344 \param vendor Vendor ID
345 \param product Product ID
346 \param description Description to search for. Use NULL if not needed.
347 \param serial Serial to search for. Use NULL if not needed.
348
349 \retval 0: all fine
350 \retval -1: usb_find_busses() failed
351 \retval -2: usb_find_devices() failed
352 \retval -3: usb device not found
353 \retval -4: unable to open device
354 \retval -5: unable to claim device
355 \retval -6: reset failed
356 \retval -7: set baudrate failed
357 \retval -8: get product description failed
358 \retval -9: get serial number failed
359 \retval -10: unable to close device
360*/
361int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
362 const char* description, const char* serial)
363{
364 struct usb_bus *bus;
365 struct usb_device *dev;
366 char string[256];
367
368 usb_init();
369
370 if (usb_find_busses() < 0)
371 ftdi_error_return(-1, "usb_find_busses() failed");
372 if (usb_find_devices() < 0)
373 ftdi_error_return(-2, "usb_find_devices() failed");
374
375 for (bus = usb_busses; bus; bus = bus->next) {
376 for (dev = bus->devices; dev; dev = dev->next) {
377 if (dev->descriptor.idVendor == vendor
378 && dev->descriptor.idProduct == product) {
379 if (!(ftdi->usb_dev = usb_open(dev)))
380 ftdi_error_return(-4, "usb_open() failed");
381
382 if (description != NULL) {
383 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iProduct, string, sizeof(string)) <= 0) {
384 usb_close (ftdi->usb_dev);
385 ftdi_error_return(-8, "unable to fetch product description");
386 }
387 if (strncmp(string, description, sizeof(string)) != 0) {
388 if (usb_close (ftdi->usb_dev) != 0)
389 ftdi_error_return(-10, "unable to close device");
390 continue;
391 }
392 }
393 if (serial != NULL) {
394 if (usb_get_string_simple(ftdi->usb_dev, dev->descriptor.iSerialNumber, string, sizeof(string)) <= 0) {
395 usb_close (ftdi->usb_dev);
396 ftdi_error_return(-9, "unable to fetch serial number");
397 }
398 if (strncmp(string, serial, sizeof(string)) != 0) {
399 if (usb_close (ftdi->usb_dev) != 0)
400 ftdi_error_return(-10, "unable to close device");
401 continue;
402 }
403 }
404
405 if (usb_close (ftdi->usb_dev) != 0)
406 ftdi_error_return(-10, "unable to close device");
407
408 return ftdi_usb_open_dev(ftdi, dev);
409 }
410 }
411 }
412
413 // device not found
414 ftdi_error_return(-3, "device not found");
415}
416
417/**
418 Resets the ftdi device.
419
420 \param ftdi pointer to ftdi_context
421
422 \retval 0: all fine
423 \retval -1: FTDI reset failed
424*/
425int ftdi_usb_reset(struct ftdi_context *ftdi)
426{
427 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
428 ftdi_error_return(-1,"FTDI reset failed");
429
430 // Invalidate data in the readbuffer
431 ftdi->readbuffer_offset = 0;
432 ftdi->readbuffer_remaining = 0;
433
434 return 0;
435}
436
437/**
438 Clears the buffers on the chip.
439
440 \param ftdi pointer to ftdi_context
441
442 \retval 0: all fine
443 \retval -1: write buffer purge failed
444 \retval -2: read buffer purge failed
445*/
446int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
447{
448 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 1, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
449 ftdi_error_return(-1, "FTDI purge of RX buffer failed");
450
451 // Invalidate data in the readbuffer
452 ftdi->readbuffer_offset = 0;
453 ftdi->readbuffer_remaining = 0;
454
455 if (usb_control_msg(ftdi->usb_dev, 0x40, 0, 2, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
456 ftdi_error_return(-2, "FTDI purge of TX buffer failed");
457
458 return 0;
459}
460
461/**
462 Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
463
464 \param ftdi pointer to ftdi_context
465
466 \retval 0: all fine
467 \retval -1: usb_release failed
468 \retval -2: usb_close failed
469*/
470int ftdi_usb_close(struct ftdi_context *ftdi)
471{
472 int rtn = 0;
473
474 if (usb_release_interface(ftdi->usb_dev, ftdi->interface) != 0)
475 rtn = -1;
476
477 if (usb_close (ftdi->usb_dev) != 0)
478 rtn = -2;
479
480 return rtn;
481}
482
483/*
484 ftdi_convert_baudrate returns nearest supported baud rate to that requested.
485 Function is only used internally
486 \internal
487*/
488static int ftdi_convert_baudrate(int baudrate, struct ftdi_context *ftdi,
489 unsigned short *value, unsigned short *index)
490{
491 static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
492 static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
493 static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
494 int divisor, best_divisor, best_baud, best_baud_diff;
495 unsigned long encoded_divisor;
496 int i;
497
498 if (baudrate <= 0) {
499 // Return error
500 return -1;
501 }
502
503 divisor = 24000000 / baudrate;
504
505 if (ftdi->type == TYPE_AM) {
506 // Round down to supported fraction (AM only)
507 divisor -= am_adjust_dn[divisor & 7];
508 }
509
510 // Try this divisor and the one above it (because division rounds down)
511 best_divisor = 0;
512 best_baud = 0;
513 best_baud_diff = 0;
514 for (i = 0; i < 2; i++) {
515 int try_divisor = divisor + i;
516 int baud_estimate;
517 int baud_diff;
518
519 // Round up to supported divisor value
520 if (try_divisor <= 8) {
521 // Round up to minimum supported divisor
522 try_divisor = 8;
523 } else if (ftdi->type != TYPE_AM && try_divisor < 12) {
524 // BM doesn't support divisors 9 through 11 inclusive
525 try_divisor = 12;
526 } else if (divisor < 16) {
527 // AM doesn't support divisors 9 through 15 inclusive
528 try_divisor = 16;
529 } else {
530 if (ftdi->type == TYPE_AM) {
531 // Round up to supported fraction (AM only)
532 try_divisor += am_adjust_up[try_divisor & 7];
533 if (try_divisor > 0x1FFF8) {
534 // Round down to maximum supported divisor value (for AM)
535 try_divisor = 0x1FFF8;
536 }
537 } else {
538 if (try_divisor > 0x1FFFF) {
539 // Round down to maximum supported divisor value (for BM)
540 try_divisor = 0x1FFFF;
541 }
542 }
543 }
544 // Get estimated baud rate (to nearest integer)
545 baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
546 // Get absolute difference from requested baud rate
547 if (baud_estimate < baudrate) {
548 baud_diff = baudrate - baud_estimate;
549 } else {
550 baud_diff = baud_estimate - baudrate;
551 }
552 if (i == 0 || baud_diff < best_baud_diff) {
553 // Closest to requested baud rate so far
554 best_divisor = try_divisor;
555 best_baud = baud_estimate;
556 best_baud_diff = baud_diff;
557 if (baud_diff == 0) {
558 // Spot on! No point trying
559 break;
560 }
561 }
562 }
563 // Encode the best divisor value
564 encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
565 // Deal with special cases for encoded value
566 if (encoded_divisor == 1) {
567 encoded_divisor = 0; // 3000000 baud
568 } else if (encoded_divisor == 0x4001) {
569 encoded_divisor = 1; // 2000000 baud (BM only)
570 }
571 // Split into "value" and "index" values
572 *value = (unsigned short)(encoded_divisor & 0xFFFF);
573 if(ftdi->type == TYPE_2232C) {
574 *index = (unsigned short)(encoded_divisor >> 8);
575 *index &= 0xFF00;
576 *index |= ftdi->index;
577 }
578 else
579 *index = (unsigned short)(encoded_divisor >> 16);
580
581 // Return the nearest baud rate
582 return best_baud;
583}
584
585/**
586 Sets the chip baud rate
587
588 \param ftdi pointer to ftdi_context
589 \param baudrate baud rate to set
590
591 \retval 0: all fine
592 \retval -1: invalid baudrate
593 \retval -2: setting baudrate failed
594*/
595int ftdi_set_baudrate(struct ftdi_context *ftdi, int baudrate)
596{
597 unsigned short value, index;
598 int actual_baudrate;
599
600 if (ftdi->bitbang_enabled) {
601 baudrate = baudrate*4;
602 }
603
604 actual_baudrate = ftdi_convert_baudrate(baudrate, ftdi, &value, &index);
605 if (actual_baudrate <= 0)
606 ftdi_error_return (-1, "Silly baudrate <= 0.");
607
608 // Check within tolerance (about 5%)
609 if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
610 || ((actual_baudrate < baudrate)
611 ? (actual_baudrate * 21 < baudrate * 20)
612 : (baudrate * 21 < actual_baudrate * 20)))
613 ftdi_error_return (-1, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
614
615 if (usb_control_msg(ftdi->usb_dev, 0x40, 3, value, index, NULL, 0, ftdi->usb_write_timeout) != 0)
616 ftdi_error_return (-2, "Setting new baudrate failed");
617
618 ftdi->baudrate = baudrate;
619 return 0;
620}
621
622/**
623 Set (RS232) line characteristics by Alain Abbas
624
625 \param ftdi pointer to ftdi_context
626 \param bits Number of bits
627 \param sbit Number of stop bits
628 \param parity Parity mode
629
630 \retval 0: all fine
631 \retval -1: Setting line property failed
632*/
633int ftdi_set_line_property(struct ftdi_context *ftdi, enum ftdi_bits_type bits,
634 enum ftdi_stopbits_type sbit, enum ftdi_parity_type parity)
635{
636 unsigned short value = bits;
637
638 switch(parity) {
639 case NONE:
640 value |= (0x00 << 8);
641 break;
642 case ODD:
643 value |= (0x01 << 8);
644 break;
645 case EVEN:
646 value |= (0x02 << 8);
647 break;
648 case MARK:
649 value |= (0x03 << 8);
650 break;
651 case SPACE:
652 value |= (0x04 << 8);
653 break;
654 }
655
656 switch(sbit) {
657 case STOP_BIT_1:
658 value |= (0x00 << 11);
659 break;
660 case STOP_BIT_15:
661 value |= (0x01 << 11);
662 break;
663 case STOP_BIT_2:
664 value |= (0x02 << 11);
665 break;
666 }
667
668 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x04, value, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
669 ftdi_error_return (-1, "Setting new line property failed");
670
671 return 0;
672}
673
674/**
675 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip
676
677 \param ftdi pointer to ftdi_context
678 \param buf Buffer with the data
679 \param size Size of the buffer
680
681 \retval <0: error code from usb_bulk_write()
682 \retval >0: number of bytes written
683*/
684int ftdi_write_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
685{
686 int ret;
687 int offset = 0;
688 int total_written = 0;
689
690 while (offset < size) {
691 int write_size = ftdi->writebuffer_chunksize;
692
693 if (offset+write_size > size)
694 write_size = size-offset;
695
696 ret = usb_bulk_write(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size, ftdi->usb_write_timeout);
697 if (ret < 0)
698 ftdi_error_return(ret, "usb bulk write failed");
699
700 total_written += ret;
701 offset += write_size;
702 }
703
704 return total_written;
705}
706
707/* these structs are stolen from libusb linux implementation
708 they are needed to directly access usbfs and offer async
709 writing */
710
711struct usb_iso_packet_desc {
712 unsigned int length;
713 unsigned int actual_length;
714 unsigned int status;
715};
716
717struct usb_urb {
718 unsigned char type;
719 unsigned char endpoint;
720 int status;
721 unsigned int flags;
722 void *buffer;
723 int buffer_length;
724 int actual_length;
725 int start_frame;
726 int number_of_packets;
727 int error_count;
728 unsigned int signr; /* signal to be sent on error, -1 if none should be sent */
729 void *usercontext;
730 struct usb_iso_packet_desc iso_frame_desc[0];
731};
732
733/* this is strongly dependent on libusb using the same struct layout. If libusb
734 changes in some later version this may break horribly (this is for libusb 0.1.12) */
735struct usb_dev_handle {
736 int fd;
737 // some other stuff coming here we don't need
738};
739
740// some defines for direct usb access, taken from libusb
741#define MAX_READ_WRITE (16 * 1024)
742#define IOCTL_USB_SUBMITURB _IOR('U', 10, struct usb_urb)
743#define USB_URB_TYPE_BULK 3
744
745/**
746 Stupid libusb does not offer async writes nor does it allow
747 access to its fd - so we need some hacks here.
748*/
749static int usb_bulk_write_async(usb_dev_handle *dev, int ep, char *bytes, int size)
750{
751 struct usb_urb urb;
752 int bytesdone = 0, requested;
753 struct usb_urb *context;
754 int ret, waiting;
755
756 do {
757 fd_set writefds;
758
759 requested = size - bytesdone;
760 if (requested > MAX_READ_WRITE)
761 requested = MAX_READ_WRITE;
762
763 urb.type = USB_URB_TYPE_BULK;
764 urb.endpoint = ep;
765 urb.flags = 0;
766 urb.buffer = bytes + bytesdone;
767 urb.buffer_length = requested;
768 urb.signr = 0;
769 urb.actual_length = 0;
770 urb.number_of_packets = 0; /* don't do isochronous yet */
771 urb.usercontext = (void*)1000; /* use something else than libusb... */
772
773 ret = ioctl(dev->fd, IOCTL_USB_SUBMITURB, &urb);
774 if (ret < 0)
775 return ret; /* the caller can read errno to get more info */
776
777 bytesdone += requested;
778 } while (bytesdone < size);
779 return bytesdone;
780}
781
782/**
783 Writes data in chunks (see ftdi_write_data_set_chunksize()) to the chip.
784 Does not wait for completion of the transfer nor does it make sure that
785 the transfer was successful.
786
787 This function could be extended to use signals and callbacks to inform the
788 caller of completion or error - but this is not done yet, volunteers welcome.
789
790 Works around libusb and directly accesses functions only available on Linux.
791
792 \param ftdi pointer to ftdi_context
793 \param buf Buffer with the data
794 \param size Size of the buffer
795
796 \retval <0: error code from usb_bulk_write()
797 \retval >0: number of bytes written
798*/
799int ftdi_write_data_async(struct ftdi_context *ftdi, unsigned char *buf, int size)
800{
801 int ret;
802 int offset = 0;
803 int total_written = 0;
804
805 while (offset < size) {
806 int write_size = ftdi->writebuffer_chunksize;
807
808 if (offset+write_size > size)
809 write_size = size-offset;
810
811 ret = usb_bulk_write_async(ftdi->usb_dev, ftdi->in_ep, buf+offset, write_size);
812 if (ret < 0)
813 ftdi_error_return(ret, "usb bulk write async failed");
814
815 total_written += ret;
816 offset += write_size;
817 }
818
819 return total_written;
820}
821
822
823/**
824 Configure write buffer chunk size.
825 Default is 4096.
826
827 \param ftdi pointer to ftdi_context
828 \param chunksize Chunk size
829
830 \retval 0: all fine
831*/
832int ftdi_write_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
833{
834 ftdi->writebuffer_chunksize = chunksize;
835 return 0;
836}
837
838/**
839 Get write buffer chunk size.
840
841 \param ftdi pointer to ftdi_context
842 \param chunksize Pointer to store chunk size in
843
844 \retval 0: all fine
845*/
846int ftdi_write_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
847{
848 *chunksize = ftdi->writebuffer_chunksize;
849 return 0;
850}
851
852/**
853 Reads data in chunks (see ftdi_read_data_set_chunksize()) from the chip.
854
855 Automatically strips the two modem status bytes transfered during every read.
856
857 \param ftdi pointer to ftdi_context
858 \param buf Buffer to store data in
859 \param size Size of the buffer
860
861 \retval <0: error code from usb_bulk_read()
862 \retval 0: no data was available
863 \retval >0: number of bytes read
864
865 \remark This function is not useful in bitbang mode.
866 Use ftdi_read_pins() to get the current state of the pins.
867*/
868int ftdi_read_data(struct ftdi_context *ftdi, unsigned char *buf, int size)
869{
870 int offset = 0, ret = 1, i, num_of_chunks, chunk_remains;
871
872 // everything we want is still in the readbuffer?
873 if (size <= ftdi->readbuffer_remaining) {
874 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, size);
875
876 // Fix offsets
877 ftdi->readbuffer_remaining -= size;
878 ftdi->readbuffer_offset += size;
879
880 /* printf("Returning bytes from buffer: %d - remaining: %d\n", size, ftdi->readbuffer_remaining); */
881
882 return size;
883 }
884 // something still in the readbuffer, but not enough to satisfy 'size'?
885 if (ftdi->readbuffer_remaining != 0) {
886 memcpy (buf, ftdi->readbuffer+ftdi->readbuffer_offset, ftdi->readbuffer_remaining);
887
888 // Fix offset
889 offset += ftdi->readbuffer_remaining;
890 }
891 // do the actual USB read
892 while (offset < size && ret > 0) {
893 ftdi->readbuffer_remaining = 0;
894 ftdi->readbuffer_offset = 0;
895 /* returns how much received */
896 ret = usb_bulk_read (ftdi->usb_dev, ftdi->out_ep, ftdi->readbuffer, ftdi->readbuffer_chunksize, ftdi->usb_read_timeout);
897 if (ret < 0)
898 ftdi_error_return(ret, "usb bulk read failed");
899
900 if (ret > 2) {
901 // skip FTDI status bytes.
902 // Maybe stored in the future to enable modem use
903 num_of_chunks = ret / 64;
904 chunk_remains = ret % 64;
905 //printf("ret = %X, num_of_chunks = %X, chunk_remains = %X, readbuffer_offset = %X\n", ret, num_of_chunks, chunk_remains, ftdi->readbuffer_offset);
906
907 ftdi->readbuffer_offset += 2;
908 ret -= 2;
909
910 if (ret > 62) {
911 for (i = 1; i < num_of_chunks; i++)
912 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
913 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
914 62);
915 if (chunk_remains > 2) {
916 memmove (ftdi->readbuffer+ftdi->readbuffer_offset+62*i,
917 ftdi->readbuffer+ftdi->readbuffer_offset+64*i,
918 chunk_remains-2);
919 ret -= 2*num_of_chunks;
920 } else
921 ret -= 2*(num_of_chunks-1)+chunk_remains;
922 }
923 } else if (ret <= 2) {
924 // no more data to read?
925 return offset;
926 }
927 if (ret > 0) {
928 // data still fits in buf?
929 if (offset+ret <= size) {
930 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, ret);
931 //printf("buf[0] = %X, buf[1] = %X\n", buf[0], buf[1]);
932 offset += ret;
933
934 /* Did we read exactly the right amount of bytes? */
935 if (offset == size)
936 //printf("read_data exact rem %d offset %d\n",
937 //ftdi->readbuffer_remaining, offset);
938 return offset;
939 } else {
940 // only copy part of the data or size <= readbuffer_chunksize
941 int part_size = size-offset;
942 memcpy (buf+offset, ftdi->readbuffer+ftdi->readbuffer_offset, part_size);
943
944 ftdi->readbuffer_offset += part_size;
945 ftdi->readbuffer_remaining = ret-part_size;
946 offset += part_size;
947
948 /* printf("Returning part: %d - size: %d - offset: %d - ret: %d - remaining: %d\n",
949 part_size, size, offset, ret, ftdi->readbuffer_remaining); */
950
951 return offset;
952 }
953 }
954 }
955 // never reached
956 return -127;
957}
958
959/**
960 Configure read buffer chunk size.
961 Default is 4096.
962
963 Automatically reallocates the buffer.
964
965 \param ftdi pointer to ftdi_context
966 \param chunksize Chunk size
967
968 \retval 0: all fine
969*/
970int ftdi_read_data_set_chunksize(struct ftdi_context *ftdi, unsigned int chunksize)
971{
972 unsigned char *new_buf;
973
974 // Invalidate all remaining data
975 ftdi->readbuffer_offset = 0;
976 ftdi->readbuffer_remaining = 0;
977
978 if ((new_buf = (unsigned char *)realloc(ftdi->readbuffer, chunksize)) == NULL)
979 ftdi_error_return(-1, "out of memory for readbuffer");
980
981 ftdi->readbuffer = new_buf;
982 ftdi->readbuffer_chunksize = chunksize;
983
984 return 0;
985}
986
987/**
988 Get read buffer chunk size.
989
990 \param ftdi pointer to ftdi_context
991 \param chunksize Pointer to store chunk size in
992
993 \retval 0: all fine
994*/
995int ftdi_read_data_get_chunksize(struct ftdi_context *ftdi, unsigned int *chunksize)
996{
997 *chunksize = ftdi->readbuffer_chunksize;
998 return 0;
999}
1000
1001
1002/**
1003 Enable bitbang mode.
1004
1005 For advanced bitbang modes of the FT2232C chip use ftdi_set_bitmode().
1006
1007 \param ftdi pointer to ftdi_context
1008 \param bitmask Bitmask to configure lines.
1009 HIGH/ON value configures a line as output.
1010
1011 \retval 0: all fine
1012 \retval -1: can't enable bitbang mode
1013*/
1014int ftdi_enable_bitbang(struct ftdi_context *ftdi, unsigned char bitmask)
1015{
1016 unsigned short usb_val;
1017
1018 usb_val = bitmask; // low byte: bitmask
1019 /* FT2232C: Set bitbang_mode to 2 to enable SPI */
1020 usb_val |= (ftdi->bitbang_mode << 8);
1021
1022 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1023 ftdi_error_return(-1, "unable to enter bitbang mode. Perhaps not a BM type chip?");
1024
1025 ftdi->bitbang_enabled = 1;
1026 return 0;
1027}
1028
1029/**
1030 Disable bitbang mode.
1031
1032 \param ftdi pointer to ftdi_context
1033
1034 \retval 0: all fine
1035 \retval -1: can't disable bitbang mode
1036*/
1037int ftdi_disable_bitbang(struct ftdi_context *ftdi)
1038{
1039 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, 0, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1040 ftdi_error_return(-1, "unable to leave bitbang mode. Perhaps not a BM type chip?");
1041
1042 ftdi->bitbang_enabled = 0;
1043 return 0;
1044}
1045
1046/**
1047 Enable advanced bitbang mode for FT2232C chips.
1048
1049 \param ftdi pointer to ftdi_context
1050 \param bitmask Bitmask to configure lines.
1051 HIGH/ON value configures a line as output.
1052 \param mode Bitbang mode: 1 for normal mode, 2 for SPI mode
1053
1054 \retval 0: all fine
1055 \retval -1: can't enable bitbang mode
1056*/
1057int ftdi_set_bitmode(struct ftdi_context *ftdi, unsigned char bitmask, unsigned char mode)
1058{
1059 unsigned short usb_val;
1060
1061 usb_val = bitmask; // low byte: bitmask
1062 usb_val |= (mode << 8);
1063 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x0B, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1064 ftdi_error_return(-1, "unable to configure bitbang mode. Perhaps not a 2232C type chip?");
1065
1066 ftdi->bitbang_mode = mode;
1067 ftdi->bitbang_enabled = (mode == BITMODE_BITBANG || mode == BITMODE_SYNCBB)?1:0;
1068 return 0;
1069}
1070
1071/**
1072 Directly read pin state. Useful for bitbang mode.
1073
1074 \param ftdi pointer to ftdi_context
1075 \param pins Pointer to store pins into
1076
1077 \retval 0: all fine
1078 \retval -1: read pins failed
1079*/
1080int ftdi_read_pins(struct ftdi_context *ftdi, unsigned char *pins)
1081{
1082 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0C, 0, ftdi->index, (char *)pins, 1, ftdi->usb_read_timeout) != 1)
1083 ftdi_error_return(-1, "read pins failed");
1084
1085 return 0;
1086}
1087
1088/**
1089 Set latency timer
1090
1091 The FTDI chip keeps data in the internal buffer for a specific
1092 amount of time if the buffer is not full yet to decrease
1093 load on the usb bus.
1094
1095 \param ftdi pointer to ftdi_context
1096 \param latency Value between 1 and 255
1097
1098 \retval 0: all fine
1099 \retval -1: latency out of range
1100 \retval -2: unable to set latency timer
1101*/
1102int ftdi_set_latency_timer(struct ftdi_context *ftdi, unsigned char latency)
1103{
1104 unsigned short usb_val;
1105
1106 if (latency < 1)
1107 ftdi_error_return(-1, "latency out of range. Only valid for 1-255");
1108
1109 usb_val = latency;
1110 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x09, usb_val, ftdi->index, NULL, 0, ftdi->usb_write_timeout) != 0)
1111 ftdi_error_return(-2, "unable to set latency timer");
1112
1113 return 0;
1114}
1115
1116/**
1117 Get latency timer
1118
1119 \param ftdi pointer to ftdi_context
1120 \param latency Pointer to store latency value in
1121
1122 \retval 0: all fine
1123 \retval -1: unable to get latency timer
1124*/
1125int ftdi_get_latency_timer(struct ftdi_context *ftdi, unsigned char *latency)
1126{
1127 unsigned short usb_val;
1128 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x0A, 0, ftdi->index, (char *)&usb_val, 1, ftdi->usb_read_timeout) != 1)
1129 ftdi_error_return(-1, "reading latency timer failed");
1130
1131 *latency = (unsigned char)usb_val;
1132 return 0;
1133}
1134
1135/**
1136 Init eeprom with default values.
1137
1138 \param eeprom Pointer to ftdi_eeprom
1139*/
1140void ftdi_eeprom_initdefaults(struct ftdi_eeprom *eeprom)
1141{
1142 eeprom->vendor_id = 0x0403;
1143 eeprom->product_id = 0x6001;
1144
1145 eeprom->self_powered = 1;
1146 eeprom->remote_wakeup = 1;
1147 eeprom->BM_type_chip = 1;
1148
1149 eeprom->in_is_isochronous = 0;
1150 eeprom->out_is_isochronous = 0;
1151 eeprom->suspend_pull_downs = 0;
1152
1153 eeprom->use_serial = 0;
1154 eeprom->change_usb_version = 0;
1155 eeprom->usb_version = 0x0200;
1156 eeprom->max_power = 0;
1157
1158 eeprom->manufacturer = NULL;
1159 eeprom->product = NULL;
1160 eeprom->serial = NULL;
1161}
1162
1163/**
1164 Build binary output from ftdi_eeprom structure.
1165 Output is suitable for ftdi_write_eeprom().
1166
1167 \param eeprom Pointer to ftdi_eeprom
1168 \param output Buffer of 128 bytes to store eeprom image to
1169
1170 \retval >0: used eeprom size
1171 \retval -1: eeprom size (128 bytes) exceeded by custom strings
1172*/
1173int ftdi_eeprom_build(struct ftdi_eeprom *eeprom, unsigned char *output)
1174{
1175 unsigned char i, j;
1176 unsigned short checksum, value;
1177 unsigned char manufacturer_size = 0, product_size = 0, serial_size = 0;
1178 int size_check;
1179
1180 if (eeprom->manufacturer != NULL)
1181 manufacturer_size = strlen(eeprom->manufacturer);
1182 if (eeprom->product != NULL)
1183 product_size = strlen(eeprom->product);
1184 if (eeprom->serial != NULL)
1185 serial_size = strlen(eeprom->serial);
1186
1187 size_check = 128; // eeprom is 128 bytes
1188 size_check -= 28; // 28 are always in use (fixed)
1189 size_check -= manufacturer_size*2;
1190 size_check -= product_size*2;
1191 size_check -= serial_size*2;
1192
1193 // eeprom size exceeded?
1194 if (size_check < 0)
1195 return (-1);
1196
1197 // empty eeprom
1198 memset (output, 0, 128);
1199
1200 // Addr 00: Stay 00 00
1201 // Addr 02: Vendor ID
1202 output[0x02] = eeprom->vendor_id;
1203 output[0x03] = eeprom->vendor_id >> 8;
1204
1205 // Addr 04: Product ID
1206 output[0x04] = eeprom->product_id;
1207 output[0x05] = eeprom->product_id >> 8;
1208
1209 // Addr 06: Device release number (0400h for BM features)
1210 output[0x06] = 0x00;
1211
1212 if (eeprom->BM_type_chip == 1)
1213 output[0x07] = 0x04;
1214 else
1215 output[0x07] = 0x02;
1216
1217 // Addr 08: Config descriptor
1218 // Bit 1: remote wakeup if 1
1219 // Bit 0: self powered if 1
1220 //
1221 j = 0;
1222 if (eeprom->self_powered == 1)
1223 j = j | 1;
1224 if (eeprom->remote_wakeup == 1)
1225 j = j | 2;
1226 output[0x08] = j;
1227
1228 // Addr 09: Max power consumption: max power = value * 2 mA
1229 output[0x09] = eeprom->max_power;
1230 ;
1231
1232 // Addr 0A: Chip configuration
1233 // Bit 7: 0 - reserved
1234 // Bit 6: 0 - reserved
1235 // Bit 5: 0 - reserved
1236 // Bit 4: 1 - Change USB version
1237 // Bit 3: 1 - Use the serial number string
1238 // Bit 2: 1 - Enable suspend pull downs for lower power
1239 // Bit 1: 1 - Out EndPoint is Isochronous
1240 // Bit 0: 1 - In EndPoint is Isochronous
1241 //
1242 j = 0;
1243 if (eeprom->in_is_isochronous == 1)
1244 j = j | 1;
1245 if (eeprom->out_is_isochronous == 1)
1246 j = j | 2;
1247 if (eeprom->suspend_pull_downs == 1)
1248 j = j | 4;
1249 if (eeprom->use_serial == 1)
1250 j = j | 8;
1251 if (eeprom->change_usb_version == 1)
1252 j = j | 16;
1253 output[0x0A] = j;
1254
1255 // Addr 0B: reserved
1256 output[0x0B] = 0x00;
1257
1258 // Addr 0C: USB version low byte when 0x0A bit 4 is set
1259 // Addr 0D: USB version high byte when 0x0A bit 4 is set
1260 if (eeprom->change_usb_version == 1) {
1261 output[0x0C] = eeprom->usb_version;
1262 output[0x0D] = eeprom->usb_version >> 8;
1263 }
1264
1265
1266 // Addr 0E: Offset of the manufacturer string + 0x80
1267 output[0x0E] = 0x14 + 0x80;
1268
1269 // Addr 0F: Length of manufacturer string
1270 output[0x0F] = manufacturer_size*2 + 2;
1271
1272 // Addr 10: Offset of the product string + 0x80, calculated later
1273 // Addr 11: Length of product string
1274 output[0x11] = product_size*2 + 2;
1275
1276 // Addr 12: Offset of the serial string + 0x80, calculated later
1277 // Addr 13: Length of serial string
1278 output[0x13] = serial_size*2 + 2;
1279
1280 // Dynamic content
1281 output[0x14] = manufacturer_size*2 + 2;
1282 output[0x15] = 0x03; // type: string
1283
1284 i = 0x16, j = 0;
1285
1286 // Output manufacturer
1287 for (j = 0; j < manufacturer_size; j++) {
1288 output[i] = eeprom->manufacturer[j], i++;
1289 output[i] = 0x00, i++;
1290 }
1291
1292 // Output product name
1293 output[0x10] = i + 0x80; // calculate offset
1294 output[i] = product_size*2 + 2, i++;
1295 output[i] = 0x03, i++;
1296 for (j = 0; j < product_size; j++) {
1297 output[i] = eeprom->product[j], i++;
1298 output[i] = 0x00, i++;
1299 }
1300
1301 // Output serial
1302 output[0x12] = i + 0x80; // calculate offset
1303 output[i] = serial_size*2 + 2, i++;
1304 output[i] = 0x03, i++;
1305 for (j = 0; j < serial_size; j++) {
1306 output[i] = eeprom->serial[j], i++;
1307 output[i] = 0x00, i++;
1308 }
1309
1310 // calculate checksum
1311 checksum = 0xAAAA;
1312
1313 for (i = 0; i < 63; i++) {
1314 value = output[i*2];
1315 value += output[(i*2)+1] << 8;
1316
1317 checksum = value^checksum;
1318 checksum = (checksum << 1) | (checksum >> 15);
1319 }
1320
1321 output[0x7E] = checksum;
1322 output[0x7F] = checksum >> 8;
1323
1324 return size_check;
1325}
1326
1327/**
1328 Read eeprom
1329
1330 \param ftdi pointer to ftdi_context
1331 \param eeprom Pointer to store eeprom into
1332
1333 \retval 0: all fine
1334 \retval -1: read failed
1335*/
1336int ftdi_read_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1337{
1338 int i;
1339
1340 for (i = 0; i < 64; i++) {
1341 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, i, eeprom+(i*2), 2, ftdi->usb_read_timeout) != 2)
1342 ftdi_error_return(-1, "reading eeprom failed");
1343 }
1344
1345 return 0;
1346}
1347
1348/*
1349 ftdi_read_chipid_shift does the bitshift operation needed for the FTDIChip-ID
1350 Function is only used internally
1351 \internal
1352*/
1353static unsigned char ftdi_read_chipid_shift(unsigned char value)
1354{
1355 return ((value & 1) << 1) |
1356 ((value & 2) << 5) |
1357 ((value & 4) >> 2) |
1358 ((value & 8) << 4) |
1359 ((value & 16) >> 1) |
1360 ((value & 32) >> 1) |
1361 ((value & 64) >> 4) |
1362 ((value & 128) >> 2);
1363}
1364
1365/**
1366 Read the FTDIChip-ID from R-type devices
1367
1368 \param ftdi pointer to ftdi_context
1369 \param chipid Pointer to store FTDIChip-ID
1370
1371 \retval 0: all fine
1372 \retval -1: read failed
1373*/
1374int ftdi_read_chipid(struct ftdi_context *ftdi, unsigned int *chipid)
1375{
1376 unsigned int a = 0, b = 0;
1377
1378 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x43, (char *)&a, 2, ftdi->usb_read_timeout) == 2)
1379 {
1380 a = a << 8 | a >> 8;
1381 if (usb_control_msg(ftdi->usb_dev, 0xC0, 0x90, 0, 0x44, (char *)&b, 2, ftdi->usb_read_timeout) == 2)
1382 {
1383 b = b << 8 | b >> 8;
1384 a = (a << 16) | b;
1385 a = ftdi_read_chipid_shift(a) | ftdi_read_chipid_shift(a>>8)<<8
1386 | ftdi_read_chipid_shift(a>>16)<<16 | ftdi_read_chipid_shift(a>>24)<<24;
1387 *chipid = a ^ 0xa5f0f7d1;
1388 return 0;
1389 }
1390 }
1391
1392 ftdi_error_return(-1, "read of FTDIChip-ID failed");
1393}
1394
1395/**
1396 Write eeprom
1397
1398 \param ftdi pointer to ftdi_context
1399 \param eeprom Pointer to read eeprom from
1400
1401 \retval 0: all fine
1402 \retval -1: read failed
1403*/
1404int ftdi_write_eeprom(struct ftdi_context *ftdi, unsigned char *eeprom)
1405{
1406 unsigned short usb_val;
1407 int i;
1408
1409 for (i = 0; i < 64; i++) {
1410 usb_val = eeprom[i*2];
1411 usb_val += eeprom[(i*2)+1] << 8;
1412 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x91, usb_val, i, NULL, 0, ftdi->usb_write_timeout) != 0)
1413 ftdi_error_return(-1, "unable to write eeprom");
1414 }
1415
1416 return 0;
1417}
1418
1419/**
1420 Erase eeprom
1421
1422 \param ftdi pointer to ftdi_context
1423
1424 \retval 0: all fine
1425 \retval -1: erase failed
1426*/
1427int ftdi_erase_eeprom(struct ftdi_context *ftdi)
1428{
1429 if (usb_control_msg(ftdi->usb_dev, 0x40, 0x92, 0, 0, NULL, 0, ftdi->usb_write_timeout) != 0)
1430 ftdi_error_return(-1, "unable to erase eeprom");
1431
1432 return 0;
1433}
1434
1435/**
1436 Get string representation for last error code
1437
1438 \param ftdi pointer to ftdi_context
1439
1440 \retval Pointer to error string
1441*/
1442char *ftdi_get_error_string (struct ftdi_context *ftdi)
1443{
1444 return ftdi->error_str;
1445}
1446
1447/*
1448 Flow control code by Lorenz Moesenlechner (lorenz@hcilab.org)
1449 and Matthias Kranz (matthias@hcilab.org)
1450*/
1451/**
1452 Set flowcontrol for ftdi chip
1453
1454 \param ftdi pointer to ftdi_context
1455 \param flowctrl flow control to use. should be
1456 SIO_DISABLE_FLOW_CTRL, SIO_RTS_CTS_HS, SIO_DTR_DSR_HS or SIO_XON_XOFF_HS
1457
1458 \retval 0: all fine
1459 \retval -1: set flow control failed
1460*/
1461int ftdi_setflowctrl(struct ftdi_context *ftdi, int flowctrl)
1462{
1463 if (usb_control_msg(ftdi->usb_dev, SIO_SET_FLOW_CTRL_REQUEST_TYPE,
1464 SIO_SET_FLOW_CTRL_REQUEST, 0, (flowctrl | ftdi->interface),
1465 NULL, 0, ftdi->usb_write_timeout) != 0)
1466 ftdi_error_return(-1, "set flow control failed");
1467
1468 return 0;
1469}
1470
1471/**
1472 Set dtr line
1473
1474 \param ftdi pointer to ftdi_context
1475 \param state state to set line to (1 or 0)
1476
1477 \retval 0: all fine
1478 \retval -1: set dtr failed
1479*/
1480int ftdi_setdtr(struct ftdi_context *ftdi, int state)
1481{
1482 unsigned short usb_val;
1483
1484 if (state)
1485 usb_val = SIO_SET_DTR_HIGH;
1486 else
1487 usb_val = SIO_SET_DTR_LOW;
1488
1489 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1490 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1491 NULL, 0, ftdi->usb_write_timeout) != 0)
1492 ftdi_error_return(-1, "set dtr failed");
1493
1494 return 0;
1495}
1496
1497/**
1498 Set rts line
1499
1500 \param ftdi pointer to ftdi_context
1501 \param state state to set line to (1 or 0)
1502
1503 \retval 0: all fine
1504 \retval -1 set rts failed
1505*/
1506int ftdi_setrts(struct ftdi_context *ftdi, int state)
1507{
1508 unsigned short usb_val;
1509
1510 if (state)
1511 usb_val = SIO_SET_RTS_HIGH;
1512 else
1513 usb_val = SIO_SET_RTS_LOW;
1514
1515 if (usb_control_msg(ftdi->usb_dev, SIO_SET_MODEM_CTRL_REQUEST_TYPE,
1516 SIO_SET_MODEM_CTRL_REQUEST, usb_val, ftdi->interface,
1517 NULL, 0, ftdi->usb_write_timeout) != 0)
1518 ftdi_error_return(-1, "set of rts failed");
1519
1520 return 0;
1521}
1522
1523/* @} end of doxygen libftdi group */