-
Notifications
You must be signed in to change notification settings - Fork 8
/
ftdi.c
4323 lines (3739 loc) · 137 KB
/
ftdi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
ftdi.c - description
-------------------
begin : Fri Apr 4 2003
copyright : (C) 2003-2014 by Intra2net AG and the libftdi developers
email : opensource@intra2net.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License *
* version 2.1 as published by the Free Software Foundation; *
* *
***************************************************************************/
/**
\mainpage libftdi API documentation
Library to talk to FTDI chips. You find the latest versions of libftdi at
http://www.intra2net.com/en/developer/libftdi/
The library is easy to use. Have a look at this short example:
\include simple.c
More examples can be found in the "examples" directory.
*/
/** \addtogroup libftdi */
/* @{ */
#include <libusb.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "ftdi_i.h"
#include "ftdi.h"
#include "ftdi_version_i.h"
#define ftdi_error_return(code, str) do { \
if ( ftdi ) \
ftdi->error_str = str; \
else \
fprintf(stderr, str); \
return code; \
} while(0);
#define ftdi_error_return_free_device_list(code, str, devs) do { \
libusb_free_device_list(devs,1); \
ftdi->error_str = str; \
return code; \
} while(0);
/**
Internal function to close usb device pointer.
Sets ftdi->usb_dev to NULL.
\internal
\param ftdi pointer to ftdi_context
\retval none
*/
static void ftdi_usb_close_internal (struct ftdi_context *ftdi)
{
if (ftdi && ftdi->usb_dev)
{
libusb_close (ftdi->usb_dev);
ftdi->usb_dev = NULL;
if(ftdi->eeprom)
ftdi->eeprom->initialized_for_connected_device = 0;
}
}
/**
Initializes a ftdi_context.
\param ftdi pointer to ftdi_context
\retval 0: all fine
\retval -1: couldn't allocate read buffer
\retval -2: couldn't allocate struct buffer
\retval -3: libusb_init() failed
\remark This should be called before all functions
*/
int ftdi_init(struct ftdi_context *ftdi)
{
struct ftdi_eeprom* eeprom = (struct ftdi_eeprom *)malloc(sizeof(struct ftdi_eeprom));
ftdi->usb_ctx = NULL;
ftdi->usb_dev = NULL;
ftdi->usb_read_timeout = 5000;
ftdi->usb_write_timeout = 5000;
ftdi->type = TYPE_BM; /* chip type */
ftdi->baudrate = -1;
ftdi->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
ftdi->readbuffer = NULL;
ftdi->readbuffer_offset = 0;
ftdi->readbuffer_remaining = 0;
ftdi->writebuffer_chunksize = 4096;
ftdi->max_packet_size = 0;
ftdi->error_str = NULL;
ftdi->module_detach_mode = AUTO_DETACH_SIO_MODULE;
if (libusb_init(&ftdi->usb_ctx) < 0)
ftdi_error_return(-3, "libusb_init() failed");
ftdi_set_interface(ftdi, INTERFACE_ANY);
ftdi->bitbang_mode = 1; /* when bitbang is enabled this holds the number of the mode */
if (eeprom == 0)
ftdi_error_return(-2, "Can't malloc struct ftdi_eeprom");
memset(eeprom, 0, sizeof(struct ftdi_eeprom));
ftdi->eeprom = eeprom;
/* All fine. Now allocate the readbuffer */
return ftdi_read_data_set_chunksize(ftdi, 4096);
}
/**
Allocate and initialize a new ftdi_context
\return a pointer to a new ftdi_context, or NULL on failure
*/
struct ftdi_context *ftdi_new(void)
{
struct ftdi_context * ftdi = (struct ftdi_context *)malloc(sizeof(struct ftdi_context));
if (ftdi == NULL)
{
return NULL;
}
if (ftdi_init(ftdi) != 0)
{
free(ftdi);
return NULL;
}
return ftdi;
}
/**
Open selected channels on a chip, otherwise use first channel.
\param ftdi pointer to ftdi_context
\param interface Interface to use for FT2232C/2232H/4232H chips.
\retval 0: all fine
\retval -1: unknown interface
\retval -2: USB device unavailable
\retval -3: Device already open, interface can't be set in that state
*/
int ftdi_set_interface(struct ftdi_context *ftdi, enum ftdi_interface interface)
{
if (ftdi == NULL)
ftdi_error_return(-2, "USB device unavailable");
if (ftdi->usb_dev != NULL)
{
int check_interface = interface;
if (check_interface == INTERFACE_ANY)
check_interface = INTERFACE_A;
if (ftdi->index != check_interface)
ftdi_error_return(-3, "Interface can not be changed on an already open device");
}
switch (interface)
{
case INTERFACE_ANY:
case INTERFACE_A:
ftdi->interface = 0;
ftdi->index = INTERFACE_A;
ftdi->in_ep = 0x02;
ftdi->out_ep = 0x81;
break;
case INTERFACE_B:
ftdi->interface = 1;
ftdi->index = INTERFACE_B;
ftdi->in_ep = 0x04;
ftdi->out_ep = 0x83;
break;
case INTERFACE_C:
ftdi->interface = 2;
ftdi->index = INTERFACE_C;
ftdi->in_ep = 0x06;
ftdi->out_ep = 0x85;
break;
case INTERFACE_D:
ftdi->interface = 3;
ftdi->index = INTERFACE_D;
ftdi->in_ep = 0x08;
ftdi->out_ep = 0x87;
break;
default:
ftdi_error_return(-1, "Unknown interface");
}
return 0;
}
/**
Deinitializes a ftdi_context.
\param ftdi pointer to ftdi_context
*/
void ftdi_deinit(struct ftdi_context *ftdi)
{
if (ftdi == NULL)
return;
ftdi_usb_close_internal (ftdi);
if (ftdi->readbuffer != NULL)
{
free(ftdi->readbuffer);
ftdi->readbuffer = NULL;
}
if (ftdi->eeprom != NULL)
{
if (ftdi->eeprom->manufacturer != 0)
{
free(ftdi->eeprom->manufacturer);
ftdi->eeprom->manufacturer = 0;
}
if (ftdi->eeprom->product != 0)
{
free(ftdi->eeprom->product);
ftdi->eeprom->product = 0;
}
if (ftdi->eeprom->serial != 0)
{
free(ftdi->eeprom->serial);
ftdi->eeprom->serial = 0;
}
free(ftdi->eeprom);
ftdi->eeprom = NULL;
}
if (ftdi->usb_ctx)
{
libusb_exit(ftdi->usb_ctx);
ftdi->usb_ctx = NULL;
}
}
/**
Deinitialize and free an ftdi_context.
\param ftdi pointer to ftdi_context
*/
void ftdi_free(struct ftdi_context *ftdi)
{
ftdi_deinit(ftdi);
free(ftdi);
}
/**
Use an already open libusb device.
\param ftdi pointer to ftdi_context
\param usb libusb libusb_device_handle to use
*/
void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
{
if (ftdi == NULL)
return;
ftdi->usb_dev = usb;
}
/**
* @brief Get libftdi library version
*
* @return ftdi_version_info Library version information
**/
struct ftdi_version_info ftdi_get_library_version(void)
{
struct ftdi_version_info ver;
ver.major = FTDI_MAJOR_VERSION;
ver.minor = FTDI_MINOR_VERSION;
ver.micro = FTDI_MICRO_VERSION;
ver.version_str = FTDI_VERSION_STRING;
ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
return ver;
}
/**
Finds all ftdi devices with given VID:PID on the usb bus. Creates a new
ftdi_device_list which needs to be deallocated by ftdi_list_free() after
use. With VID:PID 0:0, search for the default devices
(0x403:0x6001, 0x403:0x6010, 0x403:0x6011, 0x403:0x6014, 0x403:0x6015)
\param ftdi pointer to ftdi_context
\param devlist Pointer where to store list of found devices
\param vendor Vendor ID to search for
\param product Product ID to search for
\retval >0: number of devices found
\retval -3: out of memory
\retval -5: libusb_get_device_list() failed
\retval -6: libusb_get_device_descriptor() failed
*/
int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist, int vendor, int product)
{
struct ftdi_device_list **curdev;
libusb_device *dev;
libusb_device **devs;
int count = 0;
int i = 0;
if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
ftdi_error_return(-5, "libusb_get_device_list() failed");
curdev = devlist;
*curdev = NULL;
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
if (libusb_get_device_descriptor(dev, &desc) < 0)
ftdi_error_return_free_device_list(-6, "libusb_get_device_descriptor() failed", devs);
if (((vendor || product) &&
desc.idVendor == vendor && desc.idProduct == product) ||
(!(vendor || product) &&
(desc.idVendor == 0x403) && (desc.idProduct == 0x6001 || desc.idProduct == 0x6010
|| desc.idProduct == 0x6011 || desc.idProduct == 0x6014
|| desc.idProduct == 0x6015)))
{
*curdev = (struct ftdi_device_list*)malloc(sizeof(struct ftdi_device_list));
if (!*curdev)
ftdi_error_return_free_device_list(-3, "out of memory", devs);
(*curdev)->next = NULL;
(*curdev)->dev = dev;
libusb_ref_device(dev);
curdev = &(*curdev)->next;
count++;
}
}
libusb_free_device_list(devs,1);
return count;
}
/**
Frees a usb device list.
\param devlist USB device list created by ftdi_usb_find_all()
*/
void ftdi_list_free(struct ftdi_device_list **devlist)
{
struct ftdi_device_list *curdev, *next;
for (curdev = *devlist; curdev != NULL;)
{
next = curdev->next;
libusb_unref_device(curdev->dev);
free(curdev);
curdev = next;
}
*devlist = NULL;
}
/**
Frees a usb device list.
\param devlist USB device list created by ftdi_usb_find_all()
*/
void ftdi_list_free2(struct ftdi_device_list *devlist)
{
ftdi_list_free(&devlist);
}
/**
Return device ID strings from the usb device.
The parameters manufacturer, description and serial may be NULL
or pointer to buffers to store the fetched strings.
\note Use this function only in combination with ftdi_usb_find_all()
as it closes the internal "usb_dev" after use.
\param ftdi pointer to ftdi_context
\param dev libusb usb_dev to use
\param manufacturer Store manufacturer string here if not NULL
\param mnf_len Buffer size of manufacturer string
\param description Store product description string here if not NULL
\param desc_len Buffer size of product description string
\param serial Store serial string here if not NULL
\param serial_len Buffer size of serial string
\retval 0: all fine
\retval -1: wrong arguments
\retval -4: unable to open device
\retval -7: get product manufacturer failed
\retval -8: get product description failed
\retval -9: get serial number failed
\retval -11: libusb_get_device_descriptor() failed
*/
int ftdi_usb_get_strings(struct ftdi_context * ftdi, struct libusb_device * dev,
char * manufacturer, int mnf_len, char * description, int desc_len, char * serial, int serial_len)
{
struct libusb_device_descriptor desc;
if ((ftdi==NULL) || (dev==NULL))
return -1;
if (ftdi->usb_dev == NULL && libusb_open(dev, &ftdi->usb_dev) < 0)
ftdi_error_return(-4, "libusb_open() failed");
if (libusb_get_device_descriptor(dev, &desc) < 0)
ftdi_error_return(-11, "libusb_get_device_descriptor() failed");
if (manufacturer != NULL)
{
if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iManufacturer, (unsigned char *)manufacturer, mnf_len) < 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return(-7, "libusb_get_string_descriptor_ascii() failed");
}
}
if (description != NULL)
{
if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)description, desc_len) < 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return(-8, "libusb_get_string_descriptor_ascii() failed");
}
}
if (serial != NULL)
{
if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)serial, serial_len) < 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return(-9, "libusb_get_string_descriptor_ascii() failed");
}
}
ftdi_usb_close_internal (ftdi);
return 0;
}
/**
* Internal function to determine the maximum packet size.
* \param ftdi pointer to ftdi_context
* \param dev libusb usb_dev to use
* \retval Maximum packet size for this device
*/
static unsigned int _ftdi_determine_max_packet_size(struct ftdi_context *ftdi, libusb_device *dev)
{
struct libusb_device_descriptor desc;
struct libusb_config_descriptor *config0;
unsigned int packet_size;
// Sanity check
if (ftdi == NULL || dev == NULL)
return 64;
// Determine maximum packet size. Init with default value.
// New hi-speed devices from FTDI use a packet size of 512 bytes
// but could be connected to a normal speed USB hub -> 64 bytes packet size.
if (ftdi->type == TYPE_2232H || ftdi->type == TYPE_4232H || ftdi->type == TYPE_232H)
packet_size = 512;
else
packet_size = 64;
if (libusb_get_device_descriptor(dev, &desc) < 0)
return packet_size;
if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
return packet_size;
if (desc.bNumConfigurations > 0)
{
if (ftdi->interface < config0->bNumInterfaces)
{
struct libusb_interface interface = config0->interface[ftdi->interface];
if (interface.num_altsetting > 0)
{
struct libusb_interface_descriptor descriptor = interface.altsetting[0];
if (descriptor.bNumEndpoints > 0)
{
packet_size = descriptor.endpoint[0].wMaxPacketSize;
}
}
}
}
libusb_free_config_descriptor (config0);
return packet_size;
}
/**
Opens a ftdi device given by an usb_device.
\param ftdi pointer to ftdi_context
\param dev libusb usb_dev to use
\retval 0: all fine
\retval -3: unable to config device
\retval -4: unable to open device
\retval -5: unable to claim device
\retval -6: reset failed
\retval -7: set baudrate failed
\retval -8: ftdi context invalid
\retval -9: libusb_get_device_descriptor() failed
\retval -10: libusb_get_config_descriptor() failed
\retval -11: libusb_detach_kernel_driver() failed
\retval -12: libusb_get_configuration() failed
*/
int ftdi_usb_open_dev(struct ftdi_context *ftdi, libusb_device *dev)
{
struct libusb_device_descriptor desc;
struct libusb_config_descriptor *config0;
int cfg, cfg0, detach_errno = 0;
if (ftdi == NULL)
ftdi_error_return(-8, "ftdi context invalid");
if (libusb_open(dev, &ftdi->usb_dev) < 0)
ftdi_error_return(-4, "libusb_open() failed");
if (libusb_get_device_descriptor(dev, &desc) < 0)
ftdi_error_return(-9, "libusb_get_device_descriptor() failed");
if (libusb_get_config_descriptor(dev, 0, &config0) < 0)
ftdi_error_return(-10, "libusb_get_config_descriptor() failed");
cfg0 = config0->bConfigurationValue;
libusb_free_config_descriptor (config0);
// Try to detach ftdi_sio kernel module.
//
// The return code is kept in a separate variable and only parsed
// if usb_set_configuration() or usb_claim_interface() fails as the
// detach operation might be denied and everything still works fine.
// Likely scenario is a static ftdi_sio kernel module.
if (ftdi->module_detach_mode == AUTO_DETACH_SIO_MODULE)
{
if (libusb_detach_kernel_driver(ftdi->usb_dev, ftdi->interface) !=0)
detach_errno = errno;
}
if (libusb_get_configuration (ftdi->usb_dev, &cfg) < 0)
ftdi_error_return(-12, "libusb_get_configuration () failed");
// set configuration (needed especially for windows)
// tolerate EBUSY: one device with one configuration, but two interfaces
// and libftdi sessions to both interfaces (e.g. FT2232)
if (desc.bNumConfigurations > 0 && cfg != cfg0)
{
if (libusb_set_configuration(ftdi->usb_dev, cfg0) < 0)
{
ftdi_usb_close_internal (ftdi);
if (detach_errno == EPERM)
{
ftdi_error_return(-8, "inappropriate permissions on device!");
}
else
{
ftdi_error_return(-3, "unable to set usb configuration. Make sure the default FTDI driver is not in use");
}
}
}
if (libusb_claim_interface(ftdi->usb_dev, ftdi->interface) < 0)
{
ftdi_usb_close_internal (ftdi);
if (detach_errno == EPERM)
{
ftdi_error_return(-8, "inappropriate permissions on device!");
}
else
{
ftdi_error_return(-5, "unable to claim usb device. Make sure the default FTDI driver is not in use");
}
}
if (ftdi_usb_reset (ftdi) != 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return(-6, "ftdi_usb_reset failed");
}
// Try to guess chip type
// Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
if (desc.bcdDevice == 0x400 || (desc.bcdDevice == 0x200
&& desc.iSerialNumber == 0))
ftdi->type = TYPE_BM;
else if (desc.bcdDevice == 0x200)
ftdi->type = TYPE_AM;
else if (desc.bcdDevice == 0x500)
ftdi->type = TYPE_2232C;
else if (desc.bcdDevice == 0x600)
ftdi->type = TYPE_R;
else if (desc.bcdDevice == 0x700)
ftdi->type = TYPE_2232H;
else if (desc.bcdDevice == 0x800)
ftdi->type = TYPE_4232H;
else if (desc.bcdDevice == 0x900)
ftdi->type = TYPE_232H;
else if (desc.bcdDevice == 0x1000)
ftdi->type = TYPE_230X;
// Determine maximum packet size
ftdi->max_packet_size = _ftdi_determine_max_packet_size(ftdi, dev);
if (ftdi_set_baudrate (ftdi, 9600) != 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return(-7, "set baudrate failed");
}
ftdi_error_return(0, "all fine");
}
/**
Opens the first device with a given vendor and product ids.
\param ftdi pointer to ftdi_context
\param vendor Vendor ID
\param product Product ID
\retval same as ftdi_usb_open_desc()
*/
int ftdi_usb_open(struct ftdi_context *ftdi, int vendor, int product)
{
return ftdi_usb_open_desc(ftdi, vendor, product, NULL, NULL);
}
/**
Opens the first device with a given, vendor id, product id,
description and serial.
\param ftdi pointer to ftdi_context
\param vendor Vendor ID
\param product Product ID
\param description Description to search for. Use NULL if not needed.
\param serial Serial to search for. Use NULL if not needed.
\retval 0: all fine
\retval -3: usb device not found
\retval -4: unable to open device
\retval -5: unable to claim device
\retval -6: reset failed
\retval -7: set baudrate failed
\retval -8: get product description failed
\retval -9: get serial number failed
\retval -12: libusb_get_device_list() failed
\retval -13: libusb_get_device_descriptor() failed
*/
int ftdi_usb_open_desc(struct ftdi_context *ftdi, int vendor, int product,
const char* description, const char* serial)
{
return ftdi_usb_open_desc_index(ftdi,vendor,product,description,serial,0);
}
/**
Opens the index-th device with a given, vendor id, product id,
description and serial.
\param ftdi pointer to ftdi_context
\param vendor Vendor ID
\param product Product ID
\param description Description to search for. Use NULL if not needed.
\param serial Serial to search for. Use NULL if not needed.
\param index Number of matching device to open if there are more than one, starts with 0.
\retval 0: all fine
\retval -1: usb_find_busses() failed
\retval -2: usb_find_devices() failed
\retval -3: usb device not found
\retval -4: unable to open device
\retval -5: unable to claim device
\retval -6: reset failed
\retval -7: set baudrate failed
\retval -8: get product description failed
\retval -9: get serial number failed
\retval -10: unable to close device
\retval -11: ftdi context invalid
*/
int ftdi_usb_open_desc_index(struct ftdi_context *ftdi, int vendor, int product,
const char* description, const char* serial, unsigned int index)
{
libusb_device *dev;
libusb_device **devs;
char string[256];
int i = 0;
if (ftdi == NULL)
ftdi_error_return(-11, "ftdi context invalid");
if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
ftdi_error_return(-12, "libusb_get_device_list() failed");
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
int res;
if (libusb_get_device_descriptor(dev, &desc) < 0)
ftdi_error_return_free_device_list(-13, "libusb_get_device_descriptor() failed", devs);
if (desc.idVendor == vendor && desc.idProduct == product)
{
if (libusb_open(dev, &ftdi->usb_dev) < 0)
ftdi_error_return_free_device_list(-4, "usb_open() failed", devs);
if (description != NULL)
{
if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iProduct, (unsigned char *)string, sizeof(string)) < 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return_free_device_list(-8, "unable to fetch product description", devs);
}
if (strncmp(string, description, sizeof(string)) != 0)
{
ftdi_usb_close_internal (ftdi);
continue;
}
}
if (serial != NULL)
{
if (libusb_get_string_descriptor_ascii(ftdi->usb_dev, desc.iSerialNumber, (unsigned char *)string, sizeof(string)) < 0)
{
ftdi_usb_close_internal (ftdi);
ftdi_error_return_free_device_list(-9, "unable to fetch serial number", devs);
}
if (strncmp(string, serial, sizeof(string)) != 0)
{
ftdi_usb_close_internal (ftdi);
continue;
}
}
ftdi_usb_close_internal (ftdi);
if (index > 0)
{
index--;
continue;
}
res = ftdi_usb_open_dev(ftdi, dev);
libusb_free_device_list(devs,1);
return res;
}
}
// device not found
ftdi_error_return_free_device_list(-3, "device not found", devs);
}
/**
Opens the ftdi-device described by a description-string.
Intended to be used for parsing a device-description given as commandline argument.
\param ftdi pointer to ftdi_context
\param description NULL-terminated description-string, using this format:
\li <tt>d:\<devicenode></tt> path of bus and device-node (e.g. "003/001") within usb device tree (usually at /proc/bus/usb/)
\li <tt>i:\<vendor>:\<product></tt> first device with given vendor and product id, ids can be decimal, octal (preceded by "0") or hex (preceded by "0x")
\li <tt>i:\<vendor>:\<product>:\<index></tt> as above with index being the number of the device (starting with 0) if there are more than one
\li <tt>s:\<vendor>:\<product>:\<serial></tt> first device with given vendor id, product id and serial string
\note The description format may be extended in later versions.
\retval 0: all fine
\retval -2: libusb_get_device_list() failed
\retval -3: usb device not found
\retval -4: unable to open device
\retval -5: unable to claim device
\retval -6: reset failed
\retval -7: set baudrate failed
\retval -8: get product description failed
\retval -9: get serial number failed
\retval -10: unable to close device
\retval -11: illegal description format
\retval -12: ftdi context invalid
*/
int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
{
if (ftdi == NULL)
ftdi_error_return(-12, "ftdi context invalid");
if (description[0] == 0 || description[1] != ':')
ftdi_error_return(-11, "illegal description format");
if (description[0] == 'd')
{
libusb_device *dev;
libusb_device **devs;
unsigned int bus_number, device_address;
int i = 0;
if (libusb_get_device_list(ftdi->usb_ctx, &devs) < 0)
ftdi_error_return(-2, "libusb_get_device_list() failed");
/* XXX: This doesn't handle symlinks/odd paths/etc... */
if (sscanf (description + 2, "%u/%u", &bus_number, &device_address) != 2)
ftdi_error_return_free_device_list(-11, "illegal description format", devs);
while ((dev = devs[i++]) != NULL)
{
int ret;
if (bus_number == libusb_get_bus_number (dev)
&& device_address == libusb_get_device_address (dev))
{
ret = ftdi_usb_open_dev(ftdi, dev);
libusb_free_device_list(devs,1);
return ret;
}
}
// device not found
ftdi_error_return_free_device_list(-3, "device not found", devs);
}
else if (description[0] == 'i' || description[0] == 's')
{
unsigned int vendor;
unsigned int product;
unsigned int index=0;
const char *serial=NULL;
const char *startp, *endp;
errno=0;
startp=description+2;
vendor=strtoul((char*)startp,(char**)&endp,0);
if (*endp != ':' || endp == startp || errno != 0)
ftdi_error_return(-11, "illegal description format");
startp=endp+1;
product=strtoul((char*)startp,(char**)&endp,0);
if (endp == startp || errno != 0)
ftdi_error_return(-11, "illegal description format");
if (description[0] == 'i' && *endp != 0)
{
/* optional index field in i-mode */
if (*endp != ':')
ftdi_error_return(-11, "illegal description format");
startp=endp+1;
index=strtoul((char*)startp,(char**)&endp,0);
if (*endp != 0 || endp == startp || errno != 0)
ftdi_error_return(-11, "illegal description format");
}
if (description[0] == 's')
{
if (*endp != ':')
ftdi_error_return(-11, "illegal description format");
/* rest of the description is the serial */
serial=endp+1;
}
return ftdi_usb_open_desc_index(ftdi, vendor, product, NULL, serial, index);
}
else
{
ftdi_error_return(-11, "illegal description format");
}
}
/**
Resets the ftdi device.
\param ftdi pointer to ftdi_context
\retval 0: all fine
\retval -1: FTDI reset failed
\retval -2: USB device unavailable
*/
int ftdi_usb_reset(struct ftdi_context *ftdi)
{
if (ftdi == NULL || ftdi->usb_dev == NULL)
ftdi_error_return(-2, "USB device unavailable");
if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
SIO_RESET_REQUEST, SIO_RESET_SIO,
ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
ftdi_error_return(-1,"FTDI reset failed");
// Invalidate data in the readbuffer
ftdi->readbuffer_offset = 0;
ftdi->readbuffer_remaining = 0;
return 0;
}
/**
Clears the read buffer on the chip and the internal read buffer.
\param ftdi pointer to ftdi_context
\retval 0: all fine
\retval -1: read buffer purge failed
\retval -2: USB device unavailable
*/
int ftdi_usb_purge_rx_buffer(struct ftdi_context *ftdi)
{
if (ftdi == NULL || ftdi->usb_dev == NULL)
ftdi_error_return(-2, "USB device unavailable");
if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
SIO_RESET_REQUEST, SIO_RESET_PURGE_RX,
ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
ftdi_error_return(-1, "FTDI purge of RX buffer failed");
// Invalidate data in the readbuffer
ftdi->readbuffer_offset = 0;
ftdi->readbuffer_remaining = 0;
return 0;
}
/**
Clears the write buffer on the chip.
\param ftdi pointer to ftdi_context
\retval 0: all fine
\retval -1: write buffer purge failed
\retval -2: USB device unavailable
*/
int ftdi_usb_purge_tx_buffer(struct ftdi_context *ftdi)
{
if (ftdi == NULL || ftdi->usb_dev == NULL)
ftdi_error_return(-2, "USB device unavailable");
if (libusb_control_transfer(ftdi->usb_dev, FTDI_DEVICE_OUT_REQTYPE,
SIO_RESET_REQUEST, SIO_RESET_PURGE_TX,
ftdi->index, NULL, 0, ftdi->usb_write_timeout) < 0)
ftdi_error_return(-1, "FTDI purge of TX buffer failed");
return 0;
}
/**
Clears the buffers on the chip and the internal read buffer.
\param ftdi pointer to ftdi_context
\retval 0: all fine
\retval -1: read buffer purge failed
\retval -2: write buffer purge failed
\retval -3: USB device unavailable
*/
int ftdi_usb_purge_buffers(struct ftdi_context *ftdi)
{
int result;
if (ftdi == NULL || ftdi->usb_dev == NULL)
ftdi_error_return(-3, "USB device unavailable");
result = ftdi_usb_purge_rx_buffer(ftdi);
if (result < 0)
return -1;
result = ftdi_usb_purge_tx_buffer(ftdi);
if (result < 0)
return -2;
return 0;
}
/**
Closes the ftdi device. Call ftdi_deinit() if you're cleaning up.
\param ftdi pointer to ftdi_context
\retval 0: all fine
\retval -1: usb_release failed
\retval -3: ftdi context invalid
*/
int ftdi_usb_close(struct ftdi_context *ftdi)
{
int rtn = 0;
if (ftdi == NULL)
ftdi_error_return(-3, "ftdi context invalid");
if (ftdi->usb_dev != NULL)
if (libusb_release_interface(ftdi->usb_dev, ftdi->interface) < 0)
rtn = -1;
ftdi_usb_close_internal (ftdi);
return rtn;