/[pcsclite]/trunk/Drivers/ccid/src/ccid_usb.c
ViewVC logotype

Contents of /trunk/Drivers/ccid/src/ccid_usb.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4284 - (show annotations) (download)
Mon Jun 29 12:30:03 2009 UTC (3 years, 10 months ago) by rousseau
File MIME type: text/plain
File size: 28584 byte(s)
InterruptRead(): use PERIODIC instead of COMM debug level to not pollute
to much the logs for this _periodic_ call
1 /*
2 ccid_usb.c: USB access routines using the libusb library
3 Copyright (C) 2003-2009 Ludovic Rousseau
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21 * $Id$
22 */
23
24 #define __CCID_USB__
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <errno.h>
29 # ifdef S_SPLINT_S
30 # include <sys/types.h>
31 # endif
32 #include <usb.h>
33 #include <ifdhandler.h>
34
35 #include "misc.h"
36 #include "ccid.h"
37 #include "config.h"
38 #include "debug.h"
39 #include "defs.h"
40 #include "utils.h"
41 #include "parser.h"
42 #include "ccid_ifdhandler.h"
43
44
45 /* write timeout
46 * we don't have to wait a long time since the card was doing nothing */
47 #define USB_WRITE_TIMEOUT (5 * 1000) /* 5 seconds timeout */
48
49 /*
50 * Proprietary USB Class (0xFF) are (or are not) accepted
51 * A proprietary class is used for devices released before the final CCID
52 * specifications were ready.
53 * We should not have problems with non CCID devices becasue the
54 * Manufacturer and Product ID are also used to identify the device */
55 #define ALLOW_PROPRIETARY_CLASS
56
57 #define BUS_DEVICE_STRSIZE 32
58
59 typedef struct
60 {
61 usb_dev_handle *handle;
62 char *dirname;
63 char *filename;
64 int interface;
65
66 /*
67 * Endpoints
68 */
69 int bulk_in;
70 int bulk_out;
71 int interrupt;
72
73 /* Number of slots using the same device */
74 int real_nb_opened_slots;
75 int *nb_opened_slots;
76
77 /*
78 * CCID infos common to USB and serial
79 */
80 _ccid_descriptor ccid;
81
82 } _usbDevice;
83
84 /* The _usbDevice structure must be defined before including ccid_usb.h */
85 #include "ccid_usb.h"
86
87 static int get_end_points(struct usb_device *dev, _usbDevice *usbdevice, int num);
88 int ccid_check_firmware(struct usb_device *dev);
89 static unsigned int *get_data_rates(unsigned int reader_index,
90 struct usb_device *dev, int num);
91
92 /* ne need to initialize to 0 since it is static */
93 static _usbDevice usbDevice[CCID_DRIVER_MAX_READERS];
94
95 #define PCSCLITE_MANUKEY_NAME "ifdVendorID"
96 #define PCSCLITE_PRODKEY_NAME "ifdProductID"
97 #define PCSCLITE_NAMEKEY_NAME "ifdFriendlyName"
98
99 struct _bogus_firmware
100 {
101 int vendor; /* idVendor */
102 int product; /* idProduct */
103 int firmware; /* bcdDevice: previous firmwares have bugs */
104 };
105
106 static struct _bogus_firmware Bogus_firmwares[] = {
107 { 0x04e6, 0xe001, 0x0516 }, /* SCR 331 */
108 { 0x04e6, 0x5111, 0x0620 }, /* SCR 331-DI */
109 { 0x04e6, 0x5115, 0x0514 }, /* SCR 335 */
110 { 0x04e6, 0xe003, 0x0510 }, /* SPR 532 */
111 { 0x0D46, 0x3001, 0x0037 }, /* KAAN Base */
112 { 0x0D46, 0x3002, 0x0037 }, /* KAAN Advanced */
113 { 0x09C3, 0x0008, 0x0203 }, /* ActivCard V2 */
114 { 0x0DC3, 0x1004, 0x0502 }, /* ASE IIIe USBv2 */
115 { 0x0DC3, 0x1102, 0x0607 }, /* ASE IIIe KB USB */
116 { 0x058F, 0x9520, 0x0102 }, /* Alcor AU9520-G */
117 { 0x072F, 0x2200, 0x0206 }, /* ACS ACR122U-WB-R */
118
119 /* the firmware version is not correct since I do not have received a
120 * working reader yet */
121 #ifndef O2MICRO_OZ776_PATCH
122 { 0x0b97, 0x7762, 0x0111 }, /* Oz776S */
123 #endif
124 };
125
126 /* data rates supported by the secondary slots on the GemCore Pos Pro & SIM Pro */
127 unsigned int SerialCustomDataRates[] = { GEMPLUS_CUSTOM_DATA_RATES, 0 };
128
129
130 /*****************************************************************************
131 *
132 * OpenUSB
133 *
134 ****************************************************************************/
135 status_t OpenUSB(unsigned int reader_index, /*@unused@*/ int Channel)
136 {
137 return OpenUSBByName(reader_index, NULL);
138 } /* OpenUSB */
139
140
141 /*****************************************************************************
142 *
143 * OpenUSBByName
144 *
145 ****************************************************************************/
146 status_t OpenUSBByName(unsigned int reader_index, /*@null@*/ char *device)
147 {
148 static struct usb_bus *busses = NULL;
149 int alias = 0;
150 struct usb_bus *bus;
151 struct usb_dev_handle *dev_handle;
152 char keyValue[TOKEN_MAX_VALUE_SIZE];
153 unsigned int vendorID, productID;
154 char infofile[FILENAME_MAX];
155 #ifndef __APPLE__
156 unsigned int device_vendor, device_product;
157 #endif
158 char *dirname = NULL, *filename = NULL;
159 int interface_number = -1;
160 static int previous_reader_index = -1;
161
162 DEBUG_COMM3("Reader index: %X, Device: %s", reader_index, device);
163
164 #ifndef __APPLE__
165 /* device name specified */
166 if (device)
167 {
168 /* format: usb:%04x/%04x, vendor, product */
169 if (strncmp("usb:", device, 4) != 0)
170 {
171 DEBUG_CRITICAL2("device name does not start with \"usb:\": %s",
172 device);
173 return STATUS_UNSUCCESSFUL;
174 }
175
176 if (sscanf(device, "usb:%x/%x", &device_vendor, &device_product) != 2)
177 {
178 DEBUG_CRITICAL2("device name can't be parsed: %s", device);
179 return STATUS_UNSUCCESSFUL;
180 }
181
182 /* format usb:%04x/%04x:libusb:%s
183 * with %s set to %s:%s, dirname, filename */
184 if ((dirname = strstr(device, "libusb:")) != NULL)
185 {
186 /* dirname points to the first char after libusb: */
187 dirname += strlen("libusb:");
188
189 /* search the : (separation) char */
190 filename = strchr(dirname, ':');
191
192 if (filename)
193 {
194 /* end the dirname string */
195 *filename = '\0';
196
197 /* filename points to the first char after : */
198 filename++;
199 }
200 else
201 {
202 /* parse failed */
203 dirname = NULL;
204
205 DEBUG_CRITICAL2("can't parse using libusb scheme: %s", device);
206 }
207 }
208
209 /* format usb:%04x/%04x:libhal:%s
210 * with %s set to
211 * /org/freedesktop/Hal/devices/usb_device_VID_PID_SERIAL_ifX
212 * VID is VendorID
213 * PID is ProductID
214 * SERIAL is device serial number
215 * X is the interface number
216 */
217 if ((dirname = strstr(device, "libhal:")) != NULL)
218 {
219 char *p;
220
221 #define HAL_HEADER "usb_device_"
222
223 /* parse the hal string */
224 if (
225 /* search the last '/' char */
226 (p = strrchr(dirname, '/'))
227
228 /* if the string starts with "usb_device_" we continue */
229 && (0 == strncmp(++p, HAL_HEADER, sizeof(HAL_HEADER)-1))
230 /* skip the HAL header */
231 && (p += sizeof(HAL_HEADER)-1)
232
233 /* search the '_' before PID */
234 && (p = strchr(++p, '_'))
235
236 /* search the '_' before SERIAL */
237 && (p = strchr(++p, '_'))
238
239 /* search the '_' before ifX */
240 && (p = strchr(++p, '_'))
241 && (0 == strncmp(++p, "if", 2))
242 )
243 {
244 /* terminate the serial number C-string */
245 *(p-1) = '\0';
246
247 /* convert the interface number */
248 interface_number = atoi(p+2);
249 }
250 else
251 DEBUG_CRITICAL2("can't parse using libhal scheme: %s", device);
252
253 /* dirname was just a temporary variable */
254 dirname = NULL;
255 }
256 }
257 #endif
258
259 if (busses == NULL)
260 usb_init();
261
262 (void)usb_find_busses();
263 (void)usb_find_devices();
264
265 busses = usb_get_busses();
266
267 if (busses == NULL)
268 {
269 DEBUG_CRITICAL("No USB busses found");
270 return STATUS_UNSUCCESSFUL;
271 }
272
273 /* is the reader_index already used? */
274 if (usbDevice[reader_index].handle != NULL)
275 {
276 DEBUG_CRITICAL2("USB driver with index %X already in use",
277 reader_index);
278 return STATUS_UNSUCCESSFUL;
279 }
280
281 /* Info.plist full patch filename */
282 (void)snprintf(infofile, sizeof(infofile), "%s/%s/Contents/Info.plist",
283 PCSCLITE_HP_DROPDIR, BUNDLE);
284
285 /* general driver info */
286 if (!LTPBundleFindValueWithKey(infofile, "ifdManufacturerString", keyValue, 0))
287 {
288 DEBUG_INFO2("Manufacturer: %s", keyValue);
289 }
290 else
291 {
292 DEBUG_INFO2("LTPBundleFindValueWithKey error. Can't find %s?",
293 infofile);
294 return STATUS_UNSUCCESSFUL;
295 }
296 if (!LTPBundleFindValueWithKey(infofile, "ifdProductString", keyValue, 0))
297 {
298 DEBUG_INFO2("ProductString: %s", keyValue);
299 }
300 else
301 return STATUS_UNSUCCESSFUL;
302 if (!LTPBundleFindValueWithKey(infofile, "Copyright", keyValue, 0))
303 {
304 DEBUG_INFO2("Copyright: %s", keyValue);
305 }
306 else
307 return STATUS_UNSUCCESSFUL;
308 vendorID = strlen(keyValue);
309 alias = 0x1C;
310 for (; vendorID--;)
311 alias ^= keyValue[vendorID];
312
313 /* for any supported reader */
314 while (LTPBundleFindValueWithKey(infofile, PCSCLITE_MANUKEY_NAME, keyValue, alias) == 0)
315 {
316 vendorID = strtoul(keyValue, NULL, 0);
317
318 if (LTPBundleFindValueWithKey(infofile, PCSCLITE_PRODKEY_NAME, keyValue, alias))
319 goto end;
320 productID = strtoul(keyValue, NULL, 0);
321
322 if (LTPBundleFindValueWithKey(infofile, PCSCLITE_NAMEKEY_NAME, keyValue, alias))
323 goto end;
324
325 /* go to next supported reader for next round */
326 alias++;
327
328 #ifndef __APPLE__
329 /* the device was specified but is not the one we are trying to find */
330 if (device
331 && (vendorID != device_vendor || productID != device_product))
332 continue;
333 #else
334 /* Leopard puts the friendlyname in the device argument */
335 if (device && strcmp(device, keyValue))
336 continue;
337 #endif
338
339 /* on any USB buses */
340 for (bus = busses; bus; bus = bus->next)
341 {
342 struct usb_device *dev;
343
344 /* any device on this bus */
345 for (dev = bus->devices; dev; dev = dev->next)
346 {
347 /* device defined by name? */
348 if (dirname && (strcmp(dirname, bus->dirname)
349 || strcmp(filename, dev->filename)))
350 continue;
351
352 if (dev->descriptor.idVendor == vendorID
353 && dev->descriptor.idProduct == productID)
354 {
355 int r, already_used;
356 struct usb_interface *usb_interface = NULL;
357 int interface;
358 int num = 0;
359
360 #ifdef USE_COMPOSITE_AS_MULTISLOT
361 static int static_interface = 1;
362
363 {
364 /* simulate a composite device as when libhal is
365 * used */
366 int readerID = (vendorID << 16) + productID;
367
368 if ((GEMALTOPROXDU == readerID)
369 || (GEMALTOPROXSU == readerID))
370 {
371 if(interface_number >= 0)
372 {
373 DEBUG_CRITICAL("USE_COMPOSITE_AS_MULTISLOT can't be used with libhal");
374 continue;
375 }
376
377 /* the CCID interfaces are 1 and 2 */
378 interface_number = static_interface;
379 }
380 }
381 #endif
382 /* is it already opened? */
383 already_used = FALSE;
384
385 DEBUG_COMM3("Checking device: %s/%s",
386 bus->dirname, dev->filename);
387 for (r=0; r<CCID_DRIVER_MAX_READERS; r++)
388 {
389 if (usbDevice[r].handle)
390 {
391 /* same busname, same filename */
392 if (strcmp(usbDevice[r].dirname, bus->dirname) == 0 && strcmp(usbDevice[r].filename, dev->filename) == 0)
393 already_used = TRUE;
394 }
395 }
396
397 /* this reader is already managed by us */
398 if (already_used)
399 {
400 if ((previous_reader_index != -1)
401 && usbDevice[previous_reader_index].handle
402 && (strcmp(usbDevice[previous_reader_index].dirname, bus->dirname) == 0)
403 && (strcmp(usbDevice[previous_reader_index].filename, dev->filename) == 0)
404 && usbDevice[previous_reader_index].ccid.bCurrentSlotIndex < usbDevice[previous_reader_index].ccid.bMaxSlotIndex)
405 {
406 /* we reuse the same device
407 * and the reader is multi-slot */
408 usbDevice[reader_index] = usbDevice[previous_reader_index];
409 /* the other slots do not have the same data rates */
410 if ((GEMCOREPOSPRO == usbDevice[reader_index].ccid.readerID)
411 || (GEMCORESIMPRO == usbDevice[reader_index].ccid.readerID))
412 {
413 usbDevice[reader_index].ccid.arrayOfSupportedDataRates = SerialCustomDataRates;
414 usbDevice[reader_index].ccid.dwMaxDataRate = 125000;
415 }
416
417 *usbDevice[reader_index].nb_opened_slots += 1;
418 usbDevice[reader_index].ccid.bCurrentSlotIndex++;
419 usbDevice[reader_index].ccid.dwSlotStatus =
420 IFD_ICC_PRESENT;
421 DEBUG_INFO2("Opening slot: %d",
422 usbDevice[reader_index].ccid.bCurrentSlotIndex);
423 goto end;
424 }
425 else
426 {
427 /* if an interface number is given by HAL we
428 * continue with this device. */
429 if (-1 == interface_number)
430 {
431 DEBUG_INFO3("USB device %s/%s already in use."
432 " Checking next one.",
433 bus->dirname, dev->filename);
434 continue;
435 }
436 }
437 }
438
439 DEBUG_COMM3("Trying to open USB bus/device: %s/%s",
440 bus->dirname, dev->filename);
441
442 dev_handle = usb_open(dev);
443 if (dev_handle == NULL)
444 {
445 DEBUG_CRITICAL4("Can't usb_open(%s/%s): %s",
446 bus->dirname, dev->filename, strerror(errno));
447
448 continue;
449 }
450
451 /* now we found a free reader and we try to use it */
452 if (dev->config == NULL)
453 {
454 (void)usb_close(dev_handle);
455 DEBUG_CRITICAL3("No dev->config found for %s/%s",
456 bus->dirname, dev->filename);
457 return STATUS_UNSUCCESSFUL;
458 }
459
460 again:
461 usb_interface = get_ccid_usb_interface(dev, &num);
462 if (usb_interface == NULL)
463 {
464 (void)usb_close(dev_handle);
465 if (0 == num)
466 DEBUG_CRITICAL3("Can't find a CCID interface on %s/%s",
467 bus->dirname, dev->filename);
468 interface_number = -1;
469 continue;
470 }
471
472 if (usb_interface->altsetting->extralen != 54)
473 {
474 (void)usb_close(dev_handle);
475 DEBUG_CRITICAL4("Extra field for %s/%s has a wrong length: %d", bus->dirname, dev->filename, usb_interface->altsetting->extralen);
476 return STATUS_UNSUCCESSFUL;
477 }
478
479 interface = usb_interface->altsetting->bInterfaceNumber;
480 if (interface_number >= 0 && interface != interface_number)
481 {
482 /* an interface was specified and it is not the
483 * current one */
484 DEBUG_INFO3("Wrong interface for USB device %s/%s."
485 " Checking next one.", bus->dirname, dev->filename);
486
487 /* check for another CCID interface on the same device */
488 num++;
489
490 goto again;
491 }
492
493 if (usb_claim_interface(dev_handle, interface) < 0)
494 {
495 (void)usb_close(dev_handle);
496 DEBUG_CRITICAL4("Can't claim interface %s/%s: %s",
497 bus->dirname, dev->filename, strerror(errno));
498 interface_number = -1;
499 continue;
500 }
501
502 DEBUG_INFO4("Found Vendor/Product: %04X/%04X (%s)",
503 dev->descriptor.idVendor,
504 dev->descriptor.idProduct, keyValue);
505 DEBUG_INFO3("Using USB bus/device: %s/%s",
506 bus->dirname, dev->filename);
507
508 /* check for firmware bugs */
509 if (ccid_check_firmware(dev))
510 {
511 (void)usb_close(dev_handle);
512 return STATUS_UNSUCCESSFUL;
513 }
514
515 #ifdef USE_COMPOSITE_AS_MULTISLOT
516 /* use the next interface for the next "slot" */
517 static_interface++;
518
519 /* reset for a next reader */
520 if (static_interface > 2)
521 static_interface = 1;
522 #endif
523
524 /* Get Endpoints values*/
525 (void)get_end_points(dev, &usbDevice[reader_index], num);
526
527 /* store device information */
528 usbDevice[reader_index].handle = dev_handle;
529 usbDevice[reader_index].dirname = strdup(bus->dirname);
530 usbDevice[reader_index].filename = strdup(dev->filename);
531 usbDevice[reader_index].interface = interface;
532 usbDevice[reader_index].real_nb_opened_slots = 1;
533 usbDevice[reader_index].nb_opened_slots = &usbDevice[reader_index].real_nb_opened_slots;
534
535 /* CCID common informations */
536 usbDevice[reader_index].ccid.real_bSeq = 0;
537 usbDevice[reader_index].ccid.pbSeq = &usbDevice[reader_index].ccid.real_bSeq;
538 usbDevice[reader_index].ccid.readerID =
539 (dev->descriptor.idVendor << 16) +
540 dev->descriptor.idProduct;
541 usbDevice[reader_index].ccid.dwFeatures = dw2i(usb_interface->altsetting->extra, 40);
542 usbDevice[reader_index].ccid.wLcdLayout =
543 (usb_interface->altsetting->extra[51] << 8) +
544 usb_interface->altsetting->extra[50];
545 usbDevice[reader_index].ccid.bPINSupport = usb_interface->altsetting->extra[52];
546 usbDevice[reader_index].ccid.dwMaxCCIDMessageLength = dw2i(usb_interface->altsetting->extra, 44);
547 usbDevice[reader_index].ccid.dwMaxIFSD = dw2i(usb_interface->altsetting->extra, 28);
548 usbDevice[reader_index].ccid.dwDefaultClock = dw2i(usb_interface->altsetting->extra, 10);
549 usbDevice[reader_index].ccid.dwMaxDataRate = dw2i(usb_interface->altsetting->extra, 23);
550 usbDevice[reader_index].ccid.bMaxSlotIndex = usb_interface->altsetting->extra[4];
551 usbDevice[reader_index].ccid.bCurrentSlotIndex = 0;
552 usbDevice[reader_index].ccid.readTimeout = DEFAULT_COM_READ_TIMEOUT;
553 usbDevice[reader_index].ccid.arrayOfSupportedDataRates = get_data_rates(reader_index, dev, num);
554 usbDevice[reader_index].ccid.bInterfaceProtocol = usb_interface->altsetting->bInterfaceProtocol;
555 usbDevice[reader_index].ccid.bNumEndpoints = usb_interface->altsetting->bNumEndpoints;
556 usbDevice[reader_index].ccid.dwSlotStatus = IFD_ICC_PRESENT;
557 usbDevice[reader_index].ccid.bVoltageSupport = usb_interface->altsetting->extra[5];
558 goto end;
559 }
560 }
561 }
562 }
563 end:
564 if (usbDevice[reader_index].handle == NULL)
565 return STATUS_UNSUCCESSFUL;
566
567 /* memorise the current reader_index so we can detect
568 * a new OpenUSBByName on a multi slot reader */
569 previous_reader_index = reader_index;
570
571 return STATUS_SUCCESS;
572 } /* OpenUSBByName */
573
574
575 /*****************************************************************************
576 *
577 * WriteUSB
578 *
579 ****************************************************************************/
580 status_t WriteUSB(unsigned int reader_index, unsigned int length,
581 unsigned char *buffer)
582 {
583 int rv;
584 char debug_header[] = "-> 121234 ";
585
586 (void)snprintf(debug_header, sizeof(debug_header), "-> %06X ",
587 (int)reader_index);
588
589 DEBUG_XXD(debug_header, buffer, length);
590
591 rv = usb_bulk_write(usbDevice[reader_index].handle,
592 usbDevice[reader_index].bulk_out, (char *)buffer, length,
593 USB_WRITE_TIMEOUT);
594
595 if (rv < 0)
596 {
597 DEBUG_CRITICAL4("usb_bulk_write(%s/%s): %s",
598 usbDevice[reader_index].dirname, usbDevice[reader_index].filename,
599 strerror(errno));
600
601 if (ENODEV == errno)
602 return STATUS_NO_SUCH_DEVICE;
603
604 return STATUS_UNSUCCESSFUL;
605 }
606
607 return STATUS_SUCCESS;
608 } /* WriteUSB */
609
610
611 /*****************************************************************************
612 *
613 * ReadUSB
614 *
615 ****************************************************************************/
616 status_t ReadUSB(unsigned int reader_index, unsigned int * length,
617 unsigned char *buffer)
618 {
619 int rv;
620 char debug_header[] = "<- 121234 ";
621 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
622 int duplicate_frame = 0;
623
624 read_again:
625 (void)snprintf(debug_header, sizeof(debug_header), "<- %06X ",
626 (int)reader_index);
627
628 rv = usb_bulk_read(usbDevice[reader_index].handle,
629 usbDevice[reader_index].bulk_in, (char *)buffer, *length,
630 usbDevice[reader_index].ccid.readTimeout * 1000);
631
632 if (rv < 0)
633 {
634 *length = 0;
635 DEBUG_CRITICAL4("usb_bulk_read(%s/%s): %s",
636 usbDevice[reader_index].dirname, usbDevice[reader_index].filename,
637 strerror(errno));
638
639 if (ENODEV == errno)
640 return STATUS_NO_SUCH_DEVICE;
641
642 return STATUS_UNSUCCESSFUL;
643 }
644
645 *length = rv;
646
647 DEBUG_XXD(debug_header, buffer, *length);
648
649 #define BSEQ_OFFSET 6
650 if ((*length >= BSEQ_OFFSET)
651 && (buffer[BSEQ_OFFSET] < *ccid_descriptor->pbSeq -1))
652 {
653 duplicate_frame++;
654 if (duplicate_frame > 10)
655 {
656 DEBUG_CRITICAL("Too many duplicate frame detected");
657 return STATUS_UNSUCCESSFUL;
658 }
659 DEBUG_INFO("Duplicate frame detected");
660 goto read_again;
661 }
662
663 return STATUS_SUCCESS;
664 } /* ReadUSB */
665
666
667 /*****************************************************************************
668 *
669 * CloseUSB
670 *
671 ****************************************************************************/
672 status_t CloseUSB(unsigned int reader_index)
673 {
674 /* device not opened */
675 if (usbDevice[reader_index].handle == NULL)
676 return STATUS_UNSUCCESSFUL;
677
678 DEBUG_COMM3("Closing USB device: %s/%s",
679 usbDevice[reader_index].dirname,
680 usbDevice[reader_index].filename);
681
682 if (usbDevice[reader_index].ccid.arrayOfSupportedDataRates
683 && (usbDevice[reader_index].ccid.bCurrentSlotIndex == 0))
684 {
685 free(usbDevice[reader_index].ccid.arrayOfSupportedDataRates);
686 usbDevice[reader_index].ccid.arrayOfSupportedDataRates = NULL;
687 }
688
689 /* one slot closed */
690 (*usbDevice[reader_index].nb_opened_slots)--;
691
692 /* release the allocated ressources for the last slot only */
693 if (0 == *usbDevice[reader_index].nb_opened_slots)
694 {
695 DEBUG_COMM("Last slot closed. Release resources");
696
697 /* reset so that bSeq starts at 0 again */
698 if (DriverOptions & DRIVER_OPTION_RESET_ON_CLOSE)
699 (void)usb_reset(usbDevice[reader_index].handle);
700
701 (void)usb_release_interface(usbDevice[reader_index].handle,
702 usbDevice[reader_index].interface);
703 (void)usb_close(usbDevice[reader_index].handle);
704
705 free(usbDevice[reader_index].dirname);
706 free(usbDevice[reader_index].filename);
707 }
708
709 /* mark the resource unused */
710 usbDevice[reader_index].handle = NULL;
711 usbDevice[reader_index].dirname = NULL;
712 usbDevice[reader_index].filename = NULL;
713 usbDevice[reader_index].interface = 0;
714
715 return STATUS_SUCCESS;
716 } /* CloseUSB */
717
718
719 /*****************************************************************************
720 *
721 * get_ccid_descriptor
722 *
723 ****************************************************************************/
724 _ccid_descriptor *get_ccid_descriptor(unsigned int reader_index)
725 {
726 return &usbDevice[reader_index].ccid;
727 } /* get_ccid_descriptor */
728
729
730 /*****************************************************************************
731 *
732 * get_end_points
733 *
734 ****************************************************************************/
735 static int get_end_points(struct usb_device *dev, _usbDevice *usbdevice,
736 int num)
737 {
738 int i;
739 int bEndpointAddress;
740 struct usb_interface *usb_interface = get_ccid_usb_interface(dev, &num);
741
742 /*
743 * 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
744 */
745 for (i=0; i<usb_interface->altsetting->bNumEndpoints; i++)
746 {
747 /* interrupt end point (if available) */
748 if (usb_interface->altsetting->endpoint[i].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT)
749 {
750 usbdevice->interrupt = usb_interface->altsetting->endpoint[i].bEndpointAddress;
751 continue;
752 }
753
754 if (usb_interface->altsetting->endpoint[i].bmAttributes != USB_ENDPOINT_TYPE_BULK)
755 continue;
756
757 bEndpointAddress = usb_interface->altsetting->endpoint[i].bEndpointAddress;
758
759 if ((bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
760 usbdevice->bulk_in = bEndpointAddress;
761
762 if ((bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)
763 usbdevice->bulk_out = bEndpointAddress;
764 }
765
766 return 0;
767 } /* get_end_points */
768
769
770 /*****************************************************************************
771 *
772 * get_ccid_usb_interface
773 *
774 ****************************************************************************/
775 /*@null@*/ EXTERNAL struct usb_interface * get_ccid_usb_interface(
776 struct usb_device *dev, int *num)
777 {
778 struct usb_interface *usb_interface = NULL;
779 int i;
780 #ifdef O2MICRO_OZ776_PATCH
781 int readerID;
782 #endif
783
784 /* if multiple interfaces use the first one with CCID class type */
785 for (i = *num; dev->config && i<dev->config->bNumInterfaces; i++)
786 {
787 /* CCID Class? */
788 if (dev->config->interface[i].altsetting->bInterfaceClass == 0xb
789 #ifdef ALLOW_PROPRIETARY_CLASS
790 || dev->config->interface[i].altsetting->bInterfaceClass == 0xff
791 #endif
792 )
793 {
794 usb_interface = &dev->config->interface[i];
795 /* store the interface number for further reference */
796 *num = i;
797 break;
798 }
799 }
800
801 #ifdef O2MICRO_OZ776_PATCH
802 readerID = (dev->descriptor.idVendor << 16) + dev->descriptor.idProduct;
803 if (usb_interface != NULL
804 && ((OZ776 == readerID) || (OZ776_7772 == readerID)
805 || (REINER_SCT == readerID) || (BLUDRIVEII_CCID == readerID))
806 && (0 == usb_interface->altsetting->extralen)) /* this is the bug */
807 {
808 int j;
809 for (j=0; j<usb_interface->altsetting->bNumEndpoints; j++)
810 {
811 /* find the extra[] array */
812 if (54 == usb_interface->altsetting->endpoint[j].extralen)
813 {
814 /* get the extra[] from the endpoint */
815 usb_interface->altsetting->extralen = 54;
816 usb_interface->altsetting->extra =
817 usb_interface->altsetting->endpoint[j].extra;
818 /* avoid double free on close */
819 usb_interface->altsetting->endpoint[j].extra = NULL;
820 usb_interface->altsetting->endpoint[j].extralen = 0;
821 break;
822 }
823 }
824 }
825 #endif
826
827 return usb_interface;
828 } /* get_ccid_usb_interface */
829
830
831 /*****************************************************************************
832 *
833 * ccid_check_firmware
834 *
835 ****************************************************************************/
836 int ccid_check_firmware(struct usb_device *dev)
837 {
838 unsigned int i;
839
840 for (i=0; i<sizeof(Bogus_firmwares)/sizeof(Bogus_firmwares[0]); i++)
841 {
842 if (dev->descriptor.idVendor != Bogus_firmwares[i].vendor)
843 continue;
844
845 if (dev->descriptor.idProduct != Bogus_firmwares[i].product)
846 continue;
847
848 /* firmware too old and buggy */
849 if (dev->descriptor.bcdDevice < Bogus_firmwares[i].firmware)
850 {
851 if (DriverOptions & DRIVER_OPTION_USE_BOGUS_FIRMWARE)
852 {
853 DEBUG_INFO3("Firmware (%X.%02X) is bogus! but you choosed to use it",
854 dev->descriptor.bcdDevice >> 8,
855 dev->descriptor.bcdDevice & 0xFF);
856 return FALSE;
857 }
858 else
859 {
860 DEBUG_CRITICAL3("Firmware (%X.%02X) is bogus! Upgrade the reader firmware or get a new reader.",
861 dev->descriptor.bcdDevice >> 8,
862 dev->descriptor.bcdDevice & 0xFF);
863 return TRUE;
864 }
865 }
866 }
867
868 /* by default the firmware is not bogus */
869 return FALSE;
870 } /* ccid_check_firmware */
871
872
873 /*****************************************************************************
874 *
875 * get_data_rates
876 *
877 ****************************************************************************/
878 static unsigned int *get_data_rates(unsigned int reader_index,
879 struct usb_device *dev, int num)
880 {
881 int n, i, len;
882 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
883 unsigned int *int_array;
884
885 /* See CCID 3.7.3 page 25 */
886 n = ControlUSB(reader_index,
887 0xA1, /* request type */
888 0x03, /* GET_DATA_RATES */
889 0x00, /* value */
890 buffer, sizeof(buffer));
891
892 /* we got an error? */
893 if (n <= 0)
894 {
895 DEBUG_INFO2("IFD does not support GET_DATA_RATES request: %s",
896 strerror(errno));
897 return NULL;
898 }
899
900 /* we got a strange value */
901 if (n % 4)
902 {
903 DEBUG_INFO2("Wrong GET DATA RATES size: %d", n);
904 return NULL;
905 }
906
907 /* allocate the buffer (including the end marker) */
908 n /= sizeof(int);
909
910 /* we do not get the expected number of data rates */
911 len = get_ccid_usb_interface(dev, &num)->altsetting->extra[27]; /* bNumDataRatesSupported */
912 if ((n != len) && len)
913 {
914 DEBUG_INFO3("Got %d data rates but was expecting %d", n, len);
915
916 /* we got more data than expected */
917 if (n > len)
918 n = len;
919 }
920
921 int_array = calloc(n+1, sizeof(int));
922 if (NULL == int_array)
923 {
924 DEBUG_CRITICAL("Memory allocation failed");
925 return NULL;
926 }
927
928 /* convert in correct endianess */
929 for (i=0; i<n; i++)
930 {
931 int_array[i] = dw2i(buffer, i*4);
932 DEBUG_INFO2("declared: %d bps", int_array[i]);
933 }
934
935 /* end of array marker */
936 int_array[i] = 0;
937
938 return int_array;
939 } /* get_data_rates */
940
941
942 /*****************************************************************************
943 *
944 * ControlUSB
945 *
946 ****************************************************************************/
947 int ControlUSB(int reader_index, int requesttype, int request, int value,
948 unsigned char *bytes, unsigned int size)
949 {
950 int ret;
951
952 DEBUG_COMM2("request: 0x%02X", request);
953
954 if (0 == (requesttype & 0x80))
955 DEBUG_XXD("send: ", bytes, size);
956
957 ret = usb_control_msg(usbDevice[reader_index].handle, requesttype,
958 request, value, usbDevice[reader_index].interface, (char *)bytes, size,
959 usbDevice[reader_index].ccid.readTimeout * 1000);
960
961 if (requesttype & 0x80)
962 DEBUG_XXD("receive: ", bytes, ret);
963
964 return ret;
965 } /* ControlUSB */
966
967 /*****************************************************************************
968 *
969 * InterruptRead
970 *
971 *
972 ****************************************************************************/
973 int InterruptRead(int reader_index)
974 {
975 int ret;
976 char buffer[8];
977 int timeout = 2*1000; /* 2 seconds */
978
979 DEBUG_PERIODIC("before");
980 ret = usb_interrupt_read(usbDevice[reader_index].handle,
981 usbDevice[reader_index].interrupt, buffer, sizeof(buffer), timeout);
982 DEBUG_PERIODIC2("after %d", ret);
983
984 if (ret < 0)
985 {
986 /* if usb_interrupt_read() times out we get EILSEQ or EAGAIN */
987 if ((errno != EILSEQ) && (errno != EAGAIN) && (errno != ENODEV) && (errno != 0))
988 DEBUG_COMM4("usb_interrupt_read(%s/%s): %s",
989 usbDevice[reader_index].dirname,
990 usbDevice[reader_index].filename, strerror(errno));
991 }
992 else
993 DEBUG_XXD("NotifySlotChange: ", buffer, ret);
994
995 return ret;
996 } /* InterruptRead */
997

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5