707
/* Helper for scan_or_find_devices. This function returns true if a
708
requested device has been found or the caller should stop scanning
709
for other reasons. */
711
scan_or_find_usb_device (int scan_mode,
712
int *readerno, int *count, char **rid_list,
713
const char *readerid,
714
struct usb_device *dev,
716
struct usb_device **r_dev,
717
usb_dev_handle **r_idev,
718
unsigned char **ifcdesc_extra,
719
size_t *ifcdesc_extra_len,
720
int *interface_number,
721
int *ep_bulk_out, int *ep_bulk_in, int *ep_intr)
726
struct usb_config_descriptor *config;
727
struct usb_interface *interface;
728
struct usb_interface_descriptor *ifcdesc;
730
usb_dev_handle *idev;
734
for (cfg_no=0; cfg_no < dev->descriptor.bNumConfigurations; cfg_no++)
736
config = dev->config + cfg_no;
740
for (ifc_no=0; ifc_no < config->bNumInterfaces; ifc_no++)
742
interface = config->interface + ifc_no;
746
for (set_no=0; set_no < interface->num_altsetting; set_no++)
748
ifcdesc = (interface->altsetting + set_no);
749
/* The second condition is for older SCM SPR 532 who did
750
not know about the assigned CCID class. Instead of
751
trying to interpret the strings we simply check the
753
if (ifcdesc && ifcdesc->extra
754
&& ((ifcdesc->bInterfaceClass == 11
755
&& ifcdesc->bInterfaceSubClass == 0
756
&& ifcdesc->bInterfaceProtocol == 0)
757
|| (ifcdesc->bInterfaceClass == 255
758
&& dev->descriptor.idVendor == VENDOR_SCM
759
&& dev->descriptor.idProduct == 0xe003)))
761
idev = usb_open (dev);
764
DEBUGOUT_1 ("usb_open failed: %s\n",
766
continue; /* with next setting. */
769
rid = make_reader_id (idev,
770
dev->descriptor.idVendor,
771
dev->descriptor.idProduct,
772
dev->descriptor.iSerialNumber);
779
/* We are collecting infos about all
780
available CCID readers. Store them and
782
DEBUGOUT_2 ("found CCID reader %d (ID=%s)\n",
784
p = malloc ((*rid_list? strlen (*rid_list):0) + 1
791
strcat (p, *rid_list);
798
else /* Out of memory. */
807
&& !strcmp (readerid, rid)))
809
/* We found the requested reader. */
810
if (ifcdesc_extra && ifcdesc_extra_len)
812
*ifcdesc_extra = malloc (ifcdesc
818
return 1; /* Out of core. */
820
memcpy (*ifcdesc_extra, ifcdesc->extra,
822
*ifcdesc_extra_len = ifcdesc->extralen;
825
if (interface_number)
826
*interface_number = (ifcdesc->bInterfaceNumber);
829
*ep_bulk_out = find_endpoint (ifcdesc, 0);
831
*ep_bulk_in = find_endpoint (ifcdesc, 1);
833
*ep_intr = find_endpoint (ifcdesc, 2);
846
return 1; /* Found requested device. */
850
/* This is not yet the reader we want.
851
fixme: We should avoid the extra usb_open
662
870
/* Combination function to either scan all CCID devices or to find and
663
871
open one specific device.
873
The function returns 0 if a reader has been found or when a scan
874
returned without error.
665
876
With READERNO = -1 and READERID is NULL, scan mode is used and
666
877
R_RID should be the address where to store the list of reader_ids
667
878
we found. If on return this list is empty, no CCID device has been
671
882
With READERNO >= 0 or READERID is not NULL find mode is used. This
672
883
uses the same algorithm as the scan mode but stops and returns at
673
884
the entry number READERNO and return the handle for the the opened
674
USB device. If R_ID is not NULL it will receive the reader ID of
885
USB device. If R_RID is not NULL it will receive the reader ID of
675
886
that device. If R_DEV is not NULL it will the device pointer of
676
887
that device. If IFCDESC_EXTRA is NOT NULL it will receive a
677
888
malloced copy of the interfaces "extra: data filed;
678
IFCDESC_EXTRA_LEN receive the lengtyh of this field. If there is
889
IFCDESC_EXTRA_LEN receive the length of this field. If there is
679
890
no reader with number READERNO or that reader is not usable by our
680
891
implementation NULL will be returned. The caller must close a
681
892
returned USB device handle and free (if not passed as NULL) the
684
895
IFCDESC_EXTRA_LEN. With READERID being -1 the function stops if
685
896
the READERID was found.
898
If R_FD is not -1 on return the device is not using USB for
899
transport but the device associated with that file descriptor. In
900
this case INTERFACE will receive the transport type and the other
901
USB specific return values are not used; the return value is
687
904
Note that the first entry of the returned reader ID list in scan mode
688
905
corresponds with a READERNO of 0 in find mode.
690
static usb_dev_handle *
691
908
scan_or_find_devices (int readerno, const char *readerid,
693
910
struct usb_device **r_dev,
694
911
unsigned char **ifcdesc_extra,
695
912
size_t *ifcdesc_extra_len,
696
913
int *interface_number,
697
int *ep_bulk_out, int *ep_bulk_in, int *ep_intr)
914
int *ep_bulk_out, int *ep_bulk_in, int *ep_intr,
915
usb_dev_handle **r_idev,
699
918
char *rid_list = NULL;
735
959
for (dev = bus->devices; dev; dev = dev->next)
739
for (cfg_no=0; cfg_no < dev->descriptor.bNumConfigurations; cfg_no++)
961
if (scan_or_find_usb_device (scan_mode, &readerno, &count, &rid_list,
970
ep_bulk_out, ep_bulk_in, ep_intr))
741
struct usb_config_descriptor *config = dev->config + cfg_no;
747
for (ifc_no=0; ifc_no < config->bNumInterfaces; ifc_no++)
972
/* Found requested device or out of core. */
749
struct usb_interface *interface
750
= config->interface + ifc_no;
756
for (set_no=0; set_no < interface->num_altsetting; set_no++)
758
struct usb_interface_descriptor *ifcdesc
759
= interface->altsetting + set_no;
762
/* The second condition is for some SCM Micro
763
SPR 532 which does not know about the
764
assigned CCID class. Instead of trying to
765
interpret the strings we simply look at the
767
if (ifcdesc && ifcdesc->extra
768
&& ( (ifcdesc->bInterfaceClass == 11
769
&& ifcdesc->bInterfaceSubClass == 0
770
&& ifcdesc->bInterfaceProtocol == 0)
771
|| (ifcdesc->bInterfaceClass == 255
772
&& dev->descriptor.idVendor == 0x04e6
773
&& dev->descriptor.idProduct == 0xe003)))
775
idev = usb_open (dev);
778
DEBUGOUT_1 ("usb_open failed: %s\n",
783
rid = make_reader_id (idev,
784
dev->descriptor.idVendor,
785
dev->descriptor.idProduct,
786
dev->descriptor.iSerialNumber);
793
/* We are collecting infos about all
794
available CCID readers. Store
795
them and continue. */
796
DEBUGOUT_2 ("found CCID reader %d "
799
if ((p = malloc ((rid_list?
807
strcat (p, rid_list);
814
else /* Out of memory. */
822
&& !strcmp (readerid, rid)))
824
/* We found the requested reader. */
825
if (ifcdesc_extra && ifcdesc_extra_len)
827
*ifcdesc_extra = malloc (ifcdesc
833
return NULL; /* Out of core. */
835
memcpy (*ifcdesc_extra, ifcdesc->extra,
837
*ifcdesc_extra_len = ifcdesc->extralen;
839
if (interface_number)
840
*interface_number = (ifcdesc->
843
*ep_bulk_out = find_endpoint (ifcdesc, 0);
845
*ep_bulk_in = find_endpoint (ifcdesc, 1);
847
*ep_intr = find_endpoint (ifcdesc, 2);
859
return idev; /* READY. */
863
/* This is not yet the reader we
864
want. fixme: We could avoid the
865
extra usb_open in this case. */
976
return -1; /* error */
984
/* Now check whether there are any devices with special transport types. */
985
for (i=0; transports[i].name; i++)
990
fd = open (transports[i].name, O_RDWR);
993
log_debug ("failed to open `%s': %s\n",
994
transports[i].name, strerror (errno));
997
log_debug ("opened `%s': fd=%d\n", transports[i].name, fd);
999
rid = malloc (strlen (transports[i].name) + 30 + 10);
1004
return -1; /* Error. */
1006
sprintf (rid, "0000:%04X:%s:0", transports[i].type, transports[i].name);
1009
DEBUGOUT_2 ("found CCID reader %d (ID=%s)\n", count, rid);
1010
p = malloc ((rid_list? strlen (rid_list):0) + 1 + strlen (rid) + 1);
1016
return -1; /* Error. */
1021
strcat (p, rid_list);
1029
else if (!readerno ||
1030
(readerno < 0 && readerid && !strcmp (readerid, rid)))
1032
/* Found requested device. */
1033
if (interface_number)
1034
*interface_number = transports[i].type;
1041
return 0; /* Okay, found device */
1043
else /* This is not yet the reader we want. */
1050
log_debug ("closed fd %d\n", fd);
891
/* Set the level of debugging to to usea dn return the old level. -1
1063
/* Set the level of debugging to LEVEL and return the old level. -1
892
1064
just returns the old level. A level of 0 disables debugging, 1
893
1065
enables debugging, 2 enables additional tracing of the T=1
894
1066
protocol, other values are not yet defined. */
960
1134
readerno = 0; /* Default. */
962
idev = scan_or_find_devices (readerno, readerid, &rid, &dev,
963
&ifcdesc_extra, &ifcdesc_extra_len,
964
&ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr);
1136
if (scan_or_find_devices (readerno, readerid, &rid, &dev,
1137
&ifcdesc_extra, &ifcdesc_extra_len,
1138
&ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr,
967
1141
if (readerno == -1)
968
1142
DEBUGOUT_1 ("no CCID reader with ID %s\n", readerid );
980
1154
rc = CCID_DRIVER_ERR_OUT_OF_CORE;
983
(*handle)->idev = idev;
984
1157
(*handle)->rid = rid;
985
(*handle)->id_vendor = dev->descriptor.idVendor;
986
(*handle)->id_product = dev->descriptor.idProduct;
987
(*handle)->bcd_device = dev->descriptor.bcdDevice;
988
(*handle)->ifc_no = ifc_no;
989
(*handle)->ep_bulk_out = ep_bulk_out;
990
(*handle)->ep_bulk_in = ep_bulk_in;
991
(*handle)->ep_intr = ep_intr;
1158
if (idev) /* Regular USB transport. */
1160
(*handle)->idev = idev;
1161
(*handle)->dev_fd = -1;
1162
(*handle)->id_vendor = dev->descriptor.idVendor;
1163
(*handle)->id_product = dev->descriptor.idProduct;
1164
(*handle)->bcd_device = dev->descriptor.bcdDevice;
1165
(*handle)->ifc_no = ifc_no;
1166
(*handle)->ep_bulk_out = ep_bulk_out;
1167
(*handle)->ep_bulk_in = ep_bulk_in;
1168
(*handle)->ep_intr = ep_intr;
1170
else if (dev_fd != -1) /* Device transport. */
1172
(*handle)->idev = NULL;
1173
(*handle)->dev_fd = dev_fd;
1174
(*handle)->id_vendor = 0; /* Magic vendor for special transport. */
1175
(*handle)->id_product = ifc_no; /* Transport type */
1176
prepare_special_transport (*handle);
1180
assert (!"no transport"); /* Bug. */
993
1183
DEBUGOUT_2 ("using CCID reader %d (ID=%s)\n", readerno, rid );
1187
if (parse_ccid_descriptor (*handle, ifcdesc_extra, ifcdesc_extra_len))
1189
DEBUGOUT ("device not supported\n");
1190
rc = CCID_DRIVER_ERR_NO_READER;
1194
rc = usb_claim_interface (idev, ifc_no);
1197
DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1198
rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
996
if (parse_ccid_descriptor (*handle, ifcdesc_extra, ifcdesc_extra_len))
998
DEBUGOUT ("device not supported\n");
999
rc = CCID_DRIVER_ERR_NO_READER;
1003
rc = usb_claim_interface (idev, ifc_no);
1006
DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1007
rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1012
1204
free (ifcdesc_extra);
1081
1284
do_close_reader (handle);
1083
idev = scan_or_find_devices (-1, handle->rid, NULL, &dev,
1084
&ifcdesc_extra, &ifcdesc_extra_len,
1085
&ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr);
1286
if (scan_or_find_devices (-1, handle->rid, NULL, &dev,
1287
&ifcdesc_extra, &ifcdesc_extra_len,
1288
&ifc_no, &ep_bulk_out, &ep_bulk_in, &ep_intr,
1289
&idev, NULL) || !idev)
1088
1291
DEBUGOUT_1 ("no CCID reader with ID %s\n", handle->rid);
1089
1292
return CCID_DRIVER_ERR_NO_READER;
1093
handle->idev = idev;
1094
handle->ifc_no = ifc_no;
1095
handle->ep_bulk_out = ep_bulk_out;
1096
handle->ep_bulk_in = ep_bulk_in;
1097
handle->ep_intr = ep_intr;
1099
if (parse_ccid_descriptor (handle, ifcdesc_extra, ifcdesc_extra_len))
1101
DEBUGOUT ("device not supported\n");
1102
rc = CCID_DRIVER_ERR_NO_READER;
1106
rc = usb_claim_interface (idev, ifc_no);
1109
DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1110
rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1297
handle->idev = idev;
1298
handle->ifc_no = ifc_no;
1299
handle->ep_bulk_out = ep_bulk_out;
1300
handle->ep_bulk_in = ep_bulk_in;
1301
handle->ep_intr = ep_intr;
1303
if (parse_ccid_descriptor (handle, ifcdesc_extra, ifcdesc_extra_len))
1305
DEBUGOUT ("device not supported\n");
1306
rc = CCID_DRIVER_ERR_NO_READER;
1310
rc = usb_claim_interface (idev, ifc_no);
1313
DEBUGOUT_1 ("usb_claim_interface failed: %d\n", rc);
1314
rc = CCID_DRIVER_ERR_CARD_IO_ERROR;
1115
1320
free (ifcdesc_extra);
1118
usb_close (handle->idev);
1324
usb_close (handle->idev);
1119
1325
handle->idev = NULL;
1326
if (handle->dev_fd != -1)
1328
close (handle->dev_fd);
1329
log_debug ("closed fd %d\n", handle->dev_fd);
1331
handle->dev_fd = -1;
1157
rc = usb_bulk_write (handle->idev,
1158
handle->ep_bulk_out,
1160
1000 /* ms timeout */);
1165
DEBUGOUT_1 ("usb_bulk_write error: %s\n", strerror (errno));
1396
rc = usb_bulk_write (handle->idev,
1397
handle->ep_bulk_out,
1399
1000 /* ms timeout */);
1403
DEBUGOUT_1 ("usb_bulk_write error: %s\n", strerror (errno));
1405
DEBUGOUT_1 ("usb_bulk_write failed: %d\n", rc);
1167
DEBUGOUT_1 ("usb_bulk_write failed: %d\n", rc);
1409
rc = writen (handle->dev_fd, msg, msglen);
1412
DEBUGOUT_2 ("writen to %d failed: %s\n",
1413
handle->dev_fd, strerror (errno));
1168
1416
return CCID_DRIVER_ERR_CARD_IO_ERROR;
1187
1435
for USB IOCTLs. */
1188
1436
memset (buffer, 0, length);
1190
rc = usb_bulk_read (handle->idev,
1192
(char*)buffer, length,
1196
DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
1197
return CCID_DRIVER_ERR_CARD_IO_ERROR;
1440
rc = usb_bulk_read (handle->idev,
1442
(char*)buffer, length,
1446
DEBUGOUT_1 ("usb_bulk_read error: %s\n", strerror (errno));
1447
return CCID_DRIVER_ERR_CARD_IO_ERROR;
1449
*nread = msglen = rc;
1453
rc = read (handle->dev_fd, buffer, length);
1456
DEBUGOUT_2 ("read from %d failed: %s\n",
1457
handle->dev_fd, strerror (errno));
1458
return CCID_DRIVER_ERR_CARD_IO_ERROR;
1460
*nread = msglen = rc;
1200
*nread = msglen = rc;
1202
1464
if (msglen < 10)
1778
msg[0] = PC_to_RDR_GetParameters;
1779
msg[5] = 0; /* slot */
1780
msg[6] = seqno = handle->seqno++;
1781
msg[7] = 0; /* RFU */
1782
msg[8] = 0; /* RFU */
1783
msg[9] = 0; /* RFU */
1784
set_msg_len (msg, 0);
1786
rc = bulk_out (handle, msg, msglen);
1788
rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
1791
DEBUGOUT ("GetParameters failed\n");
1794
DEBUGOUT ("GetParametes returned");
1795
for (i=0; i < msglen; i++)
1796
DEBUGOUT_CONT_1 (" %02X", msg[i]);
1800
DEBUGOUT_1 (" protocol ..........: T=%d\n", msg[9]);
1801
if (msglen == 17 && msg[9] == 1)
1803
DEBUGOUT_1 (" bmFindexDindex ....: %02X\n", msg[10]);
1804
DEBUGOUT_1 (" bmTCCKST1 .........: %02X\n", msg[11]);
1805
DEBUGOUT_1 (" bGuardTimeT1 ......: %02X\n", msg[12]);
1806
DEBUGOUT_1 (" bmWaitingIntegersT1: %02X\n", msg[13]);
1807
DEBUGOUT_1 (" bClockStop ........: %02X\n", msg[14]);
1808
DEBUGOUT_1 (" bIFSC .............: %d\n", msg[15]);
1809
DEBUGOUT_1 (" bNadValue .........: %d\n", msg[16]);
1508
1815
/* Setup parameters to select T=1. */
1509
1816
msg[0] = PC_to_RDR_SetParameters;
1510
1817
msg[5] = 0; /* slot */
1513
1820
msg[8] = 0; /* RFU */
1514
1821
msg[9] = 0; /* RFU */
1516
/* FIXME: Get those values from the ATR. */
1517
msg[10]= 0x01; /* Fi/Di */
1518
msg[11]= 0x10; /* LRC, direct convention. */
1519
msg[12]= 0; /* Extra guardtime. */
1520
msg[13]= 0x41; /* BWI/CWI */
1521
msg[14]= 0; /* No clock stoppping. */
1522
msg[15]= 254; /* IFSC */
1523
msg[16]= 0; /* Does not support non default NAD values. */
1825
/* FIXME: Get those values from the ATR. */
1826
msg[10]= 0x01; /* Fi/Di */
1827
msg[11]= 0x10; /* LRC, direct convention. */
1828
msg[12]= 0; /* Extra guardtime. */
1829
msg[13]= 0x41; /* BWI/CWI */
1830
msg[14]= 0; /* No clock stoppping. */
1831
msg[15]= 254; /* IFSC */
1832
msg[16]= 0; /* Does not support non default NAD values. */
1524
1834
set_msg_len (msg, 7);
1525
1835
msglen = 10 + 7;
1532
1842
rc = bulk_out (handle, msg, msglen);
1535
/* Note that we ignore the error code on purpose. */
1536
bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
1845
rc = bulk_in (handle, msg, sizeof msg, &msglen, RDR_to_PC_Parameters,
1848
DEBUGOUT ("SetParameters failed (ignored)\n");
1539
1850
handle->t1_ns = 0;
1540
1851
handle->t1_nr = 0;
1542
/* Send an S-Block with our maximun IFSD to the CCID. */
1543
if (!handle->auto_ifsd)
1853
/* Send an S-Block with our maximum IFSD to the CCID. */
1854
if (!handle->apdu_level && !handle->auto_ifsd)
1546
1857
/* NAD: DAD=1, SAD=0 */