-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
ccid_usb.c
2190 lines (1845 loc) · 61.6 KB
/
ccid_usb.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
/*
ccid_usb.c: USB access routines using the libusb library
Copyright (C) 2003-2010 Ludovic Rousseau
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define __CCID_USB__
#include <stdio.h>
#include <string.h>
# ifdef S_SPLINT_S
# include <sys/types.h>
# endif
#include <libusb.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/time.h>
#include <stdatomic.h>
#include <unistd.h>
#include <ifdhandler.h>
#include <config.h>
#include "misc.h"
#include "ccid.h"
#include "debug.h"
#include "defs.h"
#include "utils.h"
#include "parser.h"
#include "ccid_ifdhandler.h"
#include "sys_generic.h"
/* write timeout
* we don't have to wait a long time since the card was doing nothing */
#define USB_WRITE_TIMEOUT (5 * 1000) /* 5 seconds timeout */
/*
* Proprietary USB Class (0xFF) are (or are not) accepted
* A proprietary class is used for devices released before the final CCID
* specifications were ready.
* We should not have problems with non CCID devices because the
* Manufacturer and Product ID are also used to identify the device */
#define ALLOW_PROPRIETARY_CLASS
#define BUS_DEVICE_STRSIZE 32
/* Using the default libusb context */
/* does not work for libusb <= 1.0.8 */
/* #define ctx NULL */
static libusb_context *ctx = NULL;
#define CCID_INTERRUPT_SIZE 8
struct multiSlot_ConcurrentAccess
{
unsigned char buffer[10 + MAX_BUFFER_SIZE_EXTENDED];
int length;
pthread_mutex_t mutex;
pthread_cond_t condition;
};
struct usbDevice_MultiSlot_Extension
{
int reader_index;
/* The multi-threaded polling part */
_Atomic bool terminated;
int status;
unsigned char buffer[CCID_INTERRUPT_SIZE];
pthread_t thread_proc;
pthread_mutex_t mutex;
pthread_cond_t condition;
pthread_t thread_concurrent;
struct multiSlot_ConcurrentAccess *concurrent;
libusb_device_handle *dev_handle;
};
typedef struct
{
libusb_device_handle *dev_handle;
uint8_t bus_number;
uint8_t device_address;
int interface;
/*
* Endpoints
*/
int bulk_in;
int bulk_out;
int interrupt;
/* Number of slots using the same device */
int real_nb_opened_slots;
int *nb_opened_slots;
/*
* CCID infos common to USB and serial
*/
_ccid_descriptor ccid;
/* libusb transfer for the polling (or NULL) */
pthread_mutex_t polling_transfer_mutex;
struct libusb_transfer *polling_transfer;
/* whether the polling should be terminated */
bool terminate_requested;
/* pointer to the multislot extension (if any) */
struct usbDevice_MultiSlot_Extension *multislot_extension;
bool disconnected;
} _usbDevice;
/* The _usbDevice structure must be defined before including ccid_usb.h */
#include "ccid_usb.h"
/* Specific hooks for multislot readers */
static int Multi_InterruptRead(int reader_index, int timeout /* in ms */);
static void Multi_InterruptStop(int reader_index);
static struct usbDevice_MultiSlot_Extension *Multi_CreateFirstSlot(int reader_index);
static struct usbDevice_MultiSlot_Extension *Multi_CreateNextSlot(int physical_reader_index);
static void Multi_PollingTerminate(struct usbDevice_MultiSlot_Extension *msExt);
static int get_end_points(const struct libusb_interface *usb_interface,
_usbDevice *usbdevice);
static bool ccid_check_firmware(struct libusb_device_descriptor *desc);
static unsigned int *get_data_rates(unsigned int reader_index,
const unsigned char bNumDataRatesSupported);
/* ne need to initialize to 0 since it is static */
static _usbDevice usbDevice[CCID_DRIVER_MAX_READERS];
#define PCSCLITE_MANUKEY_NAME "ifdVendorID"
#define PCSCLITE_PRODKEY_NAME "ifdProductID"
#define PCSCLITE_NAMEKEY_NAME "ifdFriendlyName"
struct _bogus_firmware
{
int vendor; /* idVendor */
int product; /* idProduct */
int firmware; /* bcdDevice: previous firmwares have bugs */
};
static struct _bogus_firmware Bogus_firmwares[] = {
{ 0x04e6, 0xe001, 0x0516 }, /* SCR 331 */
{ 0x04e6, 0x5111, 0x0620 }, /* SCR 331-DI */
{ 0x04e6, 0xe003, 0x0510 }, /* SPR 532 */
{ 0x0D46, 0x3001, 0x0037 }, /* KAAN Base */
{ 0x0D46, 0x3002, 0x0037 }, /* KAAN Advanced */
{ 0x09C3, 0x0008, 0x0203 }, /* ActivCard V2 */
{ 0x0DC3, 0x1004, 0x0502 }, /* ASE IIIe USBv2 */
{ 0x0DC3, 0x1102, 0x0607 }, /* ASE IIIe KB USB */
{ 0x058F, 0x9520, 0x0102 }, /* Alcor AU9520-G */
{ 0x072F, 0x2200, 0x0206 }, /* ACS ACR122U-WB-R */
{ 0x08C3, 0x0402, 0x5000 }, /* Precise Biometrics Precise 200 MC */
{ 0x08C3, 0x0401, 0x5000 }, /* Precise Biometrics Precise 250 MC */
{ 0x0B0C, 0x0050, 0x0101 }, /* Todos Argos Mini II */
{ 0x0DC3, 0x0900, 0x0200 }, /* Athena IDProtect Key v2 */
{ 0x03F0, 0x0036, 0x0124 }, /* HP USB CCID Smartcard Keyboard */
{ 0x062D, 0x0001, 0x0102 }, /* THRC Smart Card Reader */
{ 0x04E6, 0x5291, 0x0112 }, /* SCM SCL010 Contactless Reader */
/* the firmware version is not correct since I do not have received a
* working reader yet */
#ifndef O2MICRO_OZ776_PATCH
{ 0x0b97, 0x7762, 0x0111 }, /* Oz776S */
#endif
};
/* data rates supported by the secondary slots on the GemCore Pos Pro & SIM Pro */
static unsigned int SerialCustomDataRates[] = { GEMPLUS_CUSTOM_DATA_RATES, 0 };
/*****************************************************************************
*
* close_libusb_if_needed
*
****************************************************************************/
static void close_libusb_if_needed(void)
{
bool to_exit = true;
if (NULL == ctx)
return;
/* if at least 1 reader is still in use we do not exit libusb */
for (int i=0; i<CCID_DRIVER_MAX_READERS; i++)
{
if (usbDevice[i].dev_handle != NULL)
to_exit = false;
}
if (to_exit)
{
DEBUG_INFO1("libusb_exit");
libusb_exit(ctx);
ctx = NULL;
}
} /* close_libusb_if_needed */
/*****************************************************************************
*
* OpenUSB
*
****************************************************************************/
status_t OpenUSB(unsigned int reader_index, /*@unused@*/ int Channel)
{
(void)Channel;
return OpenUSBByName(reader_index, NULL);
} /* OpenUSB */
/*****************************************************************************
*
* OpenUSBByName
*
****************************************************************************/
status_t OpenUSBByName(unsigned int reader_index, /*@null@*/ char *device)
{
unsigned int alias;
struct libusb_device_handle *dev_handle;
char infofile[FILENAME_MAX];
#ifndef __APPLE__
unsigned int device_vendor, device_product;
unsigned int device_bus = 0;
unsigned int device_addr = 0;
#else
/* 100 ms delay */
struct timespec sleep_time = { 0, 100 * 1000 * 1000 };
int count_libusb = 10;
#endif
int interface_number = -1;
int i;
static int previous_reader_index = -1;
libusb_device **devs, *dev;
ssize_t cnt;
list_t plist, *values, *ifdVendorID, *ifdProductID, *ifdFriendlyName;
int rv;
bool claim_failed = false;
int return_value = STATUS_SUCCESS;
const char * hpDirPath;
#ifdef USE_COMPOSITE_AS_MULTISLOT
/* use the first CCID interface on first call */
static int static_interface = -1;
#endif
DEBUG_COMM3("Reader index: %X, Device: " LOG_STRING, reader_index, device);
#ifndef __APPLE__
/* device name specified */
if (device)
{
char *dirname;
/* format: usb:%04x/%04x, vendor, product */
if (strncmp("usb:", device, 4) != 0)
{
DEBUG_CRITICAL2("device name does not start with \"usb:\": %s",
device);
return STATUS_UNSUCCESSFUL;
}
if (sscanf(device, "usb:%x/%x", &device_vendor, &device_product) != 2)
{
DEBUG_CRITICAL2("device name can't be parsed: %s", device);
return STATUS_UNSUCCESSFUL;
}
/* format usb:%04x/%04x:libudev:%d:%s
* with %d set to
* 01 (or whatever the interface number is)
* and %s set to
* /dev/bus/usb/008/004
*/
if ((dirname = strstr(device, "libudev:")) != NULL)
{
/* convert the interface number, bus and device ids */
if (sscanf(dirname + 8, "%d:/dev/bus/usb/%d/%d", &interface_number, &device_bus, &device_addr) == 3) {
DEBUG_COMM2("interface_number: %d", interface_number);
DEBUG_COMM3("usb bus/device: %d/%d", device_bus, device_addr);
}
}
else
{
/* format usb:%04x/%04x:libusb-1.0:%d:%d:%d */
if ((dirname = strstr(device, "libusb-1.0:")) != NULL)
{
/* convert the interface number, bus and device ids */
if (sscanf(dirname + 11, "%d:%d:%d",
&device_bus, &device_addr, &interface_number) == 3)
{
DEBUG_COMM2("interface_number: %d", interface_number);
DEBUG_COMM3("usb bus/device: %d/%d", device_bus,
device_addr);
}
}
}
}
#endif
/* is the reader_index already used? */
if (usbDevice[reader_index].dev_handle != NULL)
{
DEBUG_CRITICAL2("USB driver with index %X already in use",
reader_index);
return STATUS_UNSUCCESSFUL;
}
/* Check if path override present in environment */
hpDirPath = SYS_GetEnv("PCSCLITE_HP_DROPDIR");
if (NULL == hpDirPath)
hpDirPath = PCSCLITE_HP_DROPDIR;
/* Info.plist full patch filename */
(void)snprintf(infofile, sizeof(infofile), "%s/%s/Contents/Info.plist",
hpDirPath, BUNDLE);
DEBUG_INFO2("Using: " LOG_STRING, infofile);
rv = bundleParse(infofile, &plist);
if (rv)
return STATUS_UNSUCCESSFUL;
#define GET_KEY(key, values) \
rv = LTPBundleFindValueWithKey(&plist, key, &values); \
if (rv) \
{ \
DEBUG_CRITICAL2("Value/Key not defined for " key " in %s", infofile); \
return_value = STATUS_UNSUCCESSFUL; \
goto end1; \
} \
else \
DEBUG_INFO2(key ": " LOG_STRING, (char *)list_get_at(values, 0));
/* general driver info */
GET_KEY("ifdManufacturerString", values)
GET_KEY("ifdProductString", values)
GET_KEY("Copyright", values)
if (NULL == ctx)
{
rv = libusb_init(&ctx);
if (rv != 0)
{
DEBUG_CRITICAL2("libusb_init failed: %s", libusb_error_name(rv));
return_value = STATUS_UNSUCCESSFUL;
goto end1;
}
}
#define GET_KEYS(key, values) \
rv = LTPBundleFindValueWithKey(&plist, key, values); \
if (rv) \
{ \
DEBUG_CRITICAL2("Value/Key not defined for " key " in %s", infofile); \
return_value = STATUS_UNSUCCESSFUL; \
goto end1; \
}
GET_KEYS("ifdVendorID", &ifdVendorID)
GET_KEYS("ifdProductID", &ifdProductID);
GET_KEYS("ifdFriendlyName", &ifdFriendlyName)
/* The 3 lists do not have the same size */
if ((list_size(ifdVendorID) != list_size(ifdProductID))
|| (list_size(ifdVendorID) != list_size(ifdFriendlyName)))
{
DEBUG_CRITICAL2("Error parsing %s", infofile);
return_value = STATUS_UNSUCCESSFUL;
goto end1;
}
#ifdef __APPLE__
again_libusb:
#endif
cnt = libusb_get_device_list(ctx, &devs);
if (cnt < 0)
{
DEBUG_CRITICAL("libusb_get_device_list() failed\n");
return_value = STATUS_UNSUCCESSFUL;
goto end1;
}
/* for any supported reader */
for (alias=0; alias<list_size(ifdVendorID); alias++)
{
unsigned int vendorID, productID;
char *friendlyName;
vendorID = strtoul(list_get_at(ifdVendorID, alias), NULL, 0);
productID = strtoul(list_get_at(ifdProductID, alias), NULL, 0);
friendlyName = list_get_at(ifdFriendlyName, alias);
#ifndef __APPLE__
/* the device was specified but is not the one we are trying to find */
if (device
&& (vendorID != device_vendor || productID != device_product))
continue;
#else
/* Leopard puts the friendlyname in the device argument */
if (device && strcmp(device, friendlyName))
continue;
#endif
/* for every device */
i = 0;
while ((dev = devs[i++]) != NULL)
{
struct libusb_device_descriptor desc;
struct libusb_config_descriptor *config_desc;
uint8_t bus_number = libusb_get_bus_number(dev);
uint8_t device_address = libusb_get_device_address(dev);
#ifndef __APPLE__
if ((device_bus || device_addr)
&& ((bus_number != device_bus)
|| (device_address != device_addr))) {
/* not the USB device we are looking for */
continue;
}
#endif
DEBUG_COMM3("Try device: %d/%d", bus_number, device_address);
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0)
{
DEBUG_INFO3("failed to get device descriptor for %d/%d",
bus_number, device_address);
continue;
}
DEBUG_COMM3("vid/pid : %04X/%04X", desc.idVendor, desc.idProduct);
if (desc.idVendor == vendorID && desc.idProduct == productID)
{
bool already_used;
const struct libusb_interface *usb_interface = NULL;
int interface;
int num = 0;
const unsigned char *device_descriptor;
int readerID = (vendorID << 16) + productID;
#ifdef USE_COMPOSITE_AS_MULTISLOT
/* use the first CCID interface on first call */
int max_interface_number = -1;
int num_CCID_interfaces = 1;
/*
* We can't talk to the two CCID interfaces
* at the same time (the reader enters a
* dead lock). So we simulate a multi slot
* reader. By default multi slot readers
* can't use the slots at the same time. See
* TAG_IFD_SLOT_THREAD_SAFE
*
* One side effect is that the two readers
* are seen by pcscd as one reader so the
* interface name is the same for the two.
*
* So we have:
* 0: Gemalto Prox-DU [Prox-DU Contact_09A00795] (09A00795) 00 00
* 1: Gemalto Prox-DU [Prox-DU Contact_09A00795] (09A00795) 00 01
* instead of
* 0: Gemalto Prox-DU [Prox-DU Contact_09A00795] (09A00795) 00 00
* 1: Gemalto Prox-DU [Prox-DU Contactless_09A00795] (09A00795) 01 00
*/
/* simulate a composite device as when libudev is used */
switch (readerID)
{
/* For the HID Omnikey 5422 the interfaces are:
* 0: OMNIKEY 5422CL Smartcard Reader
* 1: OMNIKEY 5422 Smartcard Reader
*/
case HID_OMNIKEY_5422:
case ALCOR_LINK_AK9567:
case ALCOR_LINK_AK9572:
case ACS_WALLETMATE:
case ACS_ACR1251:
case ACS_ACR1252:
case ACS_ACR1252IMP:
case ACS_ACR1552:
max_interface_number = 1; /* 2 interfaces */
num_CCID_interfaces = 2; /* 2 CCID interfaces */
break;
/* For the Gemalto Prox-DU/SU the interfaces are:
* 0: Prox-DU HID (not used)
* 1: Prox-DU Contactless (CCID)
* 2: Prox-DU Contact (CCID)
*/
case GEMALTOPROXDU:
case GEMALTOPROXSU:
max_interface_number = 2; /* 3 interfaces */
num_CCID_interfaces = 2; /* 2 CCID interfaces */
break;
case ACS_ACR1581:
max_interface_number = 2; /* 3 interfaces */
num_CCID_interfaces = 3; /* 3 CCID interfaces */
break;
/* For the Feitian R502 the interfaces are:
* 0: R502 Contactless Reader (CCID)
* 1: R502 Contact Reader (CCID)
* 2: R502 SAM1 Reader (CCID)
* 3: R502 SAM2 Reader (CCID)
*/
case FEITIANR502DUAL:
max_interface_number = 3; /* 4 interfaces */
num_CCID_interfaces = 4; /* 4 CCID interfaces */
break;
/* Kap-eCV: only the first interface is a CCID one
* 0: CCID contactless
* 1: HID
* 2: CDC
* We need to handle this case here even if
* because we have a generic test for all Kapelse
* readers (VENDOR_KAPELSE) later in the code
*/
case KAPELSE_KAPECV:
max_interface_number = 0;
num_CCID_interfaces = 1;
break;
/* Kap&Link2: only 3 first interfaces are CCID ones
* 0: CCID contact
* 1: CCID contactless
* 2: CCID contactless
* or, depending on user configuration:
* 0: CCID contact
* 1: CCID contactless
* 2: CCID contactless
* 3: CDC-ACM
*/
case KAPELSE_KAPLIN2:
max_interface_number = 2;
num_CCID_interfaces = 3;
break;
}
interface_number = static_interface;
#endif
/* is it already opened? */
already_used = false;
DEBUG_COMM3("Checking device: %d/%d",
bus_number, device_address);
for (r=0; r<CCID_DRIVER_MAX_READERS; r++)
{
if (usbDevice[r].dev_handle)
{
/* same bus, same address */
if (usbDevice[r].bus_number == bus_number
&& usbDevice[r].device_address == device_address)
already_used = true;
}
}
/* this reader is already managed by us */
if (already_used)
{
if ((previous_reader_index != -1)
&& usbDevice[previous_reader_index].dev_handle
&& (usbDevice[previous_reader_index].bus_number == bus_number)
&& (usbDevice[previous_reader_index].device_address == device_address)
&& usbDevice[previous_reader_index].ccid.bCurrentSlotIndex < usbDevice[previous_reader_index].ccid.bMaxSlotIndex)
{
/* we reuse the same device
* and the reader is multi-slot */
usbDevice[reader_index] = usbDevice[previous_reader_index];
/* The other slots of GemCore SIM Pro firmware
* 1.0 do not have the same data rates.
* Firmware 2.0 do not have this limitation */
if ((GEMCOREPOSPRO == readerID)
|| ((GEMCORESIMPRO == readerID)
&& (usbDevice[reader_index].ccid.IFD_bcdDevice < 0x0200)))
{
/* Allocate a memory buffer that will be
* released in CloseUSB() */
void *ptr = malloc(sizeof SerialCustomDataRates);
if (ptr)
{
memcpy(ptr, SerialCustomDataRates,
sizeof SerialCustomDataRates);
}
usbDevice[reader_index].ccid.arrayOfSupportedDataRates = ptr;
usbDevice[reader_index].ccid.dwMaxDataRate = 125000;
}
*usbDevice[reader_index].nb_opened_slots += 1;
usbDevice[reader_index].ccid.bCurrentSlotIndex++;
usbDevice[reader_index].ccid.dwSlotStatus =
IFD_ICC_PRESENT;
DEBUG_INFO2("Opening slot: %d",
usbDevice[reader_index].ccid.bCurrentSlotIndex);
/* This is a multislot reader
* Init the multislot stuff for this next slot */
usbDevice[reader_index].multislot_extension = Multi_CreateNextSlot(previous_reader_index);
goto end;
}
else
{
/* if an interface number is given by udev we
* continue with this device. */
if (-1 == interface_number)
{
DEBUG_INFO3("USB device %d/%d already in use."
" Checking next one.",
bus_number, device_address);
continue;
}
}
}
DEBUG_COMM3("Trying to open USB bus/device: %d/%d",
bus_number, device_address);
r = libusb_open(dev, &dev_handle);
if (r < 0)
{
DEBUG_CRITICAL4("Can't libusb_open(%d/%d): %s",
bus_number, device_address, libusb_error_name(r));
continue;
}
again:
r = libusb_get_active_config_descriptor(dev, &config_desc);
if (r < 0)
{
#ifdef __APPLE__
/* Some early Gemalto Ezio CB+ readers have
* bDeviceClass, bDeviceSubClass and bDeviceProtocol set
* to 0xFF (proprietary) instead of 0x00.
*
* So on Mac OS X the reader configuration is not done
* by the OS/kernel and we do it ourself.
*/
if ((0xFF == desc.bDeviceClass)
&& (0xFF == desc.bDeviceSubClass)
&& (0xFF == desc.bDeviceProtocol))
{
r = libusb_set_configuration(dev_handle, 1);
if (r < 0)
{
(void)libusb_close(dev_handle);
DEBUG_CRITICAL4("Can't set configuration on %d/%d: %s",
bus_number, device_address,
libusb_error_name(r));
continue;
}
}
/* recall */
r = libusb_get_active_config_descriptor(dev, &config_desc);
if (r < 0)
{
#endif
(void)libusb_close(dev_handle);
DEBUG_CRITICAL4("Can't get config descriptor on %d/%d: %s",
bus_number, device_address, libusb_error_name(r));
continue;
}
#ifdef __APPLE__
}
#endif
#ifdef USE_COMPOSITE_AS_MULTISLOT
if ((VENDOR_KAPELSE == GET_VENDOR(readerID))
&& (-1 == max_interface_number))
{
/* Kapelse: all interfaces are CCID ones */
num_CCID_interfaces = config_desc->bNumInterfaces;
max_interface_number = num_CCID_interfaces-1;
}
#endif
usb_interface = get_ccid_usb_interface(config_desc, &num);
if (usb_interface == NULL)
{
libusb_free_config_descriptor(config_desc);
(void)libusb_close(dev_handle);
if (0 == num)
DEBUG_CRITICAL3("Can't find a CCID interface on %d/%d",
bus_number, device_address);
interface_number = -1;
continue;
}
device_descriptor = get_ccid_device_descriptor(usb_interface);
if (NULL == device_descriptor)
{
libusb_free_config_descriptor(config_desc);
(void)libusb_close(dev_handle);
DEBUG_CRITICAL3("Unable to find the device descriptor for %d/%d",
bus_number, device_address);
return_value = STATUS_UNSUCCESSFUL;
goto end2;
}
interface = usb_interface->altsetting->bInterfaceNumber;
if (interface_number >= 0 && interface != interface_number)
{
libusb_free_config_descriptor(config_desc);
/* an interface was specified and it is not the
* current one */
DEBUG_INFO3("Found interface %d but expecting %d",
interface, interface_number);
DEBUG_INFO3("Wrong interface for USB device %d/%d."
" Checking next one.", bus_number, device_address);
/* check for another CCID interface on the same device */
num++;
goto again;
}
r = libusb_claim_interface(dev_handle, interface);
if (r < 0)
{
libusb_free_config_descriptor(config_desc);
(void)libusb_close(dev_handle);
DEBUG_CRITICAL4("Can't claim interface %d/%d: %s",
bus_number, device_address, libusb_error_name(r));
claim_failed = true;
interface_number = -1;
continue;
}
DEBUG_INFO4("Found Vendor/Product: %04X/%04X (" LOG_STRING ")",
desc.idVendor, desc.idProduct, friendlyName);
DEBUG_INFO3("Using USB bus/device: %d/%d",
bus_number, device_address);
/* check for firmware bugs */
if (ccid_check_firmware(&desc))
{
libusb_free_config_descriptor(config_desc);
(void)libusb_close(dev_handle);
return_value = STATUS_UNSUCCESSFUL;
goto end2;
}
#ifdef USE_COMPOSITE_AS_MULTISLOT
/* only if max_interface_number has a value set earlier */
if (max_interface_number >= 0)
{
/* use the next interface for the next "slot" */
static_interface = interface + 1;
/* reset for a next reader */
if (static_interface > max_interface_number)
static_interface = -1;
}
#endif
/* Get Endpoints values*/
(void)get_end_points(usb_interface, &usbDevice[reader_index]);
/* store device information */
usbDevice[reader_index].dev_handle = dev_handle;
usbDevice[reader_index].bus_number = bus_number;
usbDevice[reader_index].device_address = device_address;
usbDevice[reader_index].interface = interface;
usbDevice[reader_index].real_nb_opened_slots = 1;
usbDevice[reader_index].nb_opened_slots = &usbDevice[reader_index].real_nb_opened_slots;
pthread_mutex_init(&usbDevice[reader_index].polling_transfer_mutex, NULL);
usbDevice[reader_index].polling_transfer = NULL;
usbDevice[reader_index].terminate_requested = false;
usbDevice[reader_index].disconnected = false;
/* CCID common information */
#ifdef USE_COMPOSITE_AS_MULTISLOT
usbDevice[reader_index].ccid.num_interfaces = num_CCID_interfaces;
#endif
usbDevice[reader_index].ccid.real_bSeq = 0;
usbDevice[reader_index].ccid.pbSeq = &usbDevice[reader_index].ccid.real_bSeq;
usbDevice[reader_index].ccid.readerID =
(desc.idVendor << 16) + desc.idProduct;
usbDevice[reader_index].ccid.dwFeatures = dw2i(device_descriptor, 40);
usbDevice[reader_index].ccid.wLcdLayout =
(device_descriptor[51] << 8) + device_descriptor[50];
usbDevice[reader_index].ccid.bPINSupport = device_descriptor[52];
usbDevice[reader_index].ccid.dwMaxCCIDMessageLength = dw2i(device_descriptor, 44);
usbDevice[reader_index].ccid.dwMaxIFSD = dw2i(device_descriptor, 28);
usbDevice[reader_index].ccid.dwDefaultClock = dw2i(device_descriptor, 10);
usbDevice[reader_index].ccid.dwMaxDataRate = dw2i(device_descriptor, 23);
usbDevice[reader_index].ccid.bMaxSlotIndex = device_descriptor[4];
usbDevice[reader_index].ccid.bMaxCCIDBusySlots = device_descriptor[53];
usbDevice[reader_index].ccid.bCurrentSlotIndex = 0;
usbDevice[reader_index].ccid.readTimeout = DEFAULT_COM_READ_TIMEOUT;
if (device_descriptor[27])
usbDevice[reader_index].ccid.arrayOfSupportedDataRates = get_data_rates(reader_index, device_descriptor[27]);
else
{
usbDevice[reader_index].ccid.arrayOfSupportedDataRates = NULL;
DEBUG_INFO1("bNumDataRatesSupported is 0");
}
usbDevice[reader_index].ccid.bInterfaceProtocol = usb_interface->altsetting->bInterfaceProtocol;
usbDevice[reader_index].ccid.bNumEndpoints = usb_interface->altsetting->bNumEndpoints;
usbDevice[reader_index].ccid.dwSlotStatus = IFD_ICC_PRESENT;
usbDevice[reader_index].ccid.bVoltageSupport = device_descriptor[5];
usbDevice[reader_index].ccid.sIFD_serial_number = NULL;
usbDevice[reader_index].ccid.gemalto_firmware_features = NULL;
usbDevice[reader_index].ccid.dwProtocols = dw2i(device_descriptor, 6);
#ifdef ENABLE_ZLP
usbDevice[reader_index].ccid.zlp = false;
#endif
if (desc.iSerialNumber)
{
unsigned char serial[128];
int ret;
ret = libusb_get_string_descriptor_ascii(dev_handle,
desc.iSerialNumber, serial,
sizeof(serial));
if (ret > 0)
usbDevice[reader_index].ccid.sIFD_serial_number
= strdup((char *)serial);
}
usbDevice[reader_index].ccid.sIFD_iManufacturer = NULL;
if (desc.iManufacturer)
{
unsigned char iManufacturer[128];
int ret;
ret = libusb_get_string_descriptor_ascii(dev_handle,
desc.iManufacturer, iManufacturer,
sizeof(iManufacturer));
if (ret > 0)
usbDevice[reader_index].ccid.sIFD_iManufacturer
= strdup((char *)iManufacturer);
}
usbDevice[reader_index].ccid.IFD_bcdDevice = desc.bcdDevice;
/* If this is a multislot reader, init the multislot stuff */
if (usbDevice[reader_index].ccid.bMaxSlotIndex)
usbDevice[reader_index].multislot_extension = Multi_CreateFirstSlot(reader_index);
else
usbDevice[reader_index].multislot_extension = NULL;
libusb_free_config_descriptor(config_desc);
goto end;
}
}
}
end:
if (usbDevice[reader_index].dev_handle == NULL)
{
/* free the libusb allocated list & devices */
libusb_free_device_list(devs, 1);
#ifdef __APPLE__
/* give some time to libusb to detect the new USB devices on Mac OS X */
if (count_libusb > 0)
{
count_libusb--;
DEBUG_INFO2("Wait after libusb: %d", count_libusb);
nanosleep(&sleep_time, NULL);
goto again_libusb;
}
#endif
/* free bundle list */
bundleRelease(&plist);
/* failed */
close_libusb_if_needed();
if (claim_failed)
return STATUS_COMM_ERROR;
DEBUG_INFO1("Device not found?");
#ifdef USE_COMPOSITE_AS_MULTISLOT
static_interface = -1;
#endif
return STATUS_NO_SUCH_DEVICE;
}
/* memorise the current reader_index so we can detect
* a new OpenUSBByName on a multi slot reader */
previous_reader_index = reader_index;
end2:
/* free the libusb allocated list & devices */
libusb_free_device_list(devs, 1);
end1:
/* free bundle list */
bundleRelease(&plist);
if (return_value != STATUS_SUCCESS)
close_libusb_if_needed();
return return_value;
} /* OpenUSBByName */
/*****************************************************************************
*
* WriteUSB
*
****************************************************************************/
status_t WriteUSB(unsigned int reader_index, unsigned int length,
unsigned char *buffer)
{
int rv;
int actual_length;
char debug_header[] = "-> 121234 ";
(void)snprintf(debug_header, sizeof(debug_header), "-> %06X ",
(int)reader_index);
if (usbDevice[reader_index].disconnected)
{
DEBUG_COMM("Reader disconnected");
return STATUS_NO_SUCH_DEVICE;
}
#ifdef ENABLE_ZLP
if (usbDevice[reader_index].ccid.zlp)
{ /* Zero Length Packet */
int dummy_length;
/* try to read a ZLP so transfer length = 0
* timeout of 10 ms */
(void)libusb_bulk_transfer(usbDevice[reader_index].dev_handle,
usbDevice[reader_index].bulk_in, NULL, 0, &dummy_length, 10);
}
#endif
DEBUG_XXD(debug_header, buffer, length);
rv = libusb_bulk_transfer(usbDevice[reader_index].dev_handle,
usbDevice[reader_index].bulk_out, buffer, length,
&actual_length, USB_WRITE_TIMEOUT);
if (rv < 0)
{
DEBUG_CRITICAL4("write failed (%d/%d): %s",
usbDevice[reader_index].bus_number,
usbDevice[reader_index].device_address, libusb_error_name(rv));
if (LIBUSB_ERROR_NO_DEVICE == rv)
return STATUS_NO_SUCH_DEVICE;
return STATUS_UNSUCCESSFUL;
}
return STATUS_SUCCESS;
} /* WriteUSB */
/*****************************************************************************
*
* ReadUSB
*
****************************************************************************/
status_t ReadUSB(unsigned int reader_index, unsigned int * length,
unsigned char *buffer, int bSeq)
{
int rv;
int actual_length;
char debug_header[] = "<- 121234 ";
int duplicate_frame = 0;
if (usbDevice[reader_index].disconnected)
{
DEBUG_COMM("Reader disconnected");
return STATUS_NO_SUCH_DEVICE;
}
read_again:
(void)snprintf(debug_header, sizeof(debug_header), "<- %06X ",
(int)reader_index);
if (usbDevice[reader_index].multislot_extension)
{
/* multi slot read */