/[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 3350 - (show annotations) (download)
Thu Mar 19 13:48:10 2009 UTC (4 years, 2 months ago) by rousseau
File MIME type: text/plain
File size: 27549 byte(s)
OpenUSBByName(): support more than one CCID interface per USB device
with the libhal scheme
1 /*
2 ccid_usb.c: USB access routines using the libusb library
3 Copyright (C) 2003-2008 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
118 /* the firmware version is not correct since I do not have received a
119 * working reader yet */
120 #ifndef O2MICRO_OZ776_PATCH
121 { 0x0b97, 0x7762, 0x0111 }, /* Oz776S */
122 #endif
123 };
124
125 /* data rates supported by the secondary slots on the GemCore Pos Pro & SIM Pro */
126 unsigned int SerialCustomDataRates[] = { GEMPLUS_CUSTOM_DATA_RATES, 0 };
127
128
129 /*****************************************************************************
130 *
131 * OpenUSB
132 *
133 ****************************************************************************/
134 status_t OpenUSB(unsigned int reader_index, /*@unused@*/ int Channel)
135 {
136 return OpenUSBByName(reader_index, NULL);
137 } /* OpenUSB */
138
139
140 /*****************************************************************************
141 *
142 * OpenUSBByName
143 *
144 ****************************************************************************/
145 status_t OpenUSBByName(unsigned int reader_index, /*@null@*/ char *device)
146 {
147 static struct usb_bus *busses = NULL;
148 int alias = 0;
149 struct usb_bus *bus;
150 struct usb_dev_handle *dev_handle;
151 char keyValue[TOKEN_MAX_VALUE_SIZE];
152 unsigned int vendorID, productID;
153 char infofile[FILENAME_MAX];
154 #ifndef __APPLE__
155 unsigned int device_vendor, device_product;
156 #endif
157 char *dirname = NULL, *filename = NULL;
158 char *serial = 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 && (serial = p+1)
239
240 /* search the '_' before ifX */
241 && (p = strchr(++p, '_'))
242 && (0 == strncmp(++p, "if", 2))
243 )
244 {
245 /* terminate the serial number C-string */
246 *(p-1) = '\0';
247
248 /* convert the interface number */
249 interface_number = atoi(p+2);
250 }
251 else
252 DEBUG_CRITICAL2("can't parse using libhal scheme: %s", device);
253
254 /* dirname was just a temporary variable */
255 dirname = NULL;
256 }
257 }
258 #endif
259
260 if (busses == NULL)
261 usb_init();
262
263 (void)usb_find_busses();
264 (void)usb_find_devices();
265
266 busses = usb_get_busses();
267
268 if (busses == NULL)
269 {
270 DEBUG_CRITICAL("No USB busses found");
271 return STATUS_UNSUCCESSFUL;
272 }
273
274 /* is the reader_index already used? */
275 if (usbDevice[reader_index].handle != NULL)
276 {
277 DEBUG_CRITICAL2("USB driver with index %X already in use",
278 reader_index);
279 return STATUS_UNSUCCESSFUL;
280 }
281
282 /* Info.plist full patch filename */
283 (void)snprintf(infofile, sizeof(infofile), "%s/%s/Contents/Info.plist",
284 PCSCLITE_HP_DROPDIR, BUNDLE);
285
286 /* general driver info */
287 if (!LTPBundleFindValueWithKey(infofile, "ifdManufacturerString", keyValue, 0))
288 {
289 DEBUG_INFO2("Manufacturer: %s", keyValue);
290 }
291 else
292 {
293 DEBUG_INFO2("LTPBundleFindValueWithKey error. Can't find %s?",
294 infofile);
295 return STATUS_UNSUCCESSFUL;
296 }
297 if (!LTPBundleFindValueWithKey(infofile, "ifdProductString", keyValue, 0))
298 {
299 DEBUG_INFO2("ProductString: %s", keyValue);
300 }
301 else
302 return STATUS_UNSUCCESSFUL;
303 if (!LTPBundleFindValueWithKey(infofile, "Copyright", keyValue, 0))
304 {
305 DEBUG_INFO2("Copyright: %s", keyValue);
306 }
307 else
308 return STATUS_UNSUCCESSFUL;
309 vendorID = strlen(keyValue);
310 alias = 0x1C;
311 for (; vendorID--;)
312 alias ^= keyValue[vendorID];
313
314 /* for any supported reader */
315 while (LTPBundleFindValueWithKey(infofile, PCSCLITE_MANUKEY_NAME, keyValue, alias) == 0)
316 {
317 vendorID = strtoul(keyValue, NULL, 0);
318
319 if (LTPBundleFindValueWithKey(infofile, PCSCLITE_PRODKEY_NAME, keyValue, alias))
320 goto end;
321 productID = strtoul(keyValue, NULL, 0);
322
323 if (LTPBundleFindValueWithKey(infofile, PCSCLITE_NAMEKEY_NAME, keyValue, alias))
324 goto end;
325
326 /* go to next supported reader for next round */
327 alias++;
328
329 #ifndef __APPLE__
330 /* the device was specified but is not the one we are trying to find */
331 if (device
332 && (vendorID != device_vendor || productID != device_product))
333 continue;
334 #else
335 /* Leopard puts the friendlyname in the device argument */
336 if (device && strcmp(device, keyValue))
337 continue;
338 #endif
339
340 /* on any USB buses */
341 for (bus = busses; bus; bus = bus->next)
342 {
343 struct usb_device *dev;
344
345 /* any device on this bus */
346 for (dev = bus->devices; dev; dev = dev->next)
347 {
348 /* device defined by name? */
349 if (dirname && (strcmp(dirname, bus->dirname)
350 || strcmp(filename, dev->filename)))
351 continue;
352
353 if (dev->descriptor.idVendor == vendorID
354 && dev->descriptor.idProduct == productID)
355 {
356 int r, already_used;
357 struct usb_interface *usb_interface = NULL;
358 int interface;
359 int num = 0;
360
361 /* is it already opened? */
362 already_used = FALSE;
363
364 DEBUG_COMM3("Checking device: %s/%s",
365 bus->dirname, dev->filename);
366 for (r=0; r<CCID_DRIVER_MAX_READERS; r++)
367 {
368 if (usbDevice[r].handle)
369 {
370 /* same busname, same filename */
371 if (strcmp(usbDevice[r].dirname, bus->dirname) == 0 && strcmp(usbDevice[r].filename, dev->filename) == 0)
372 already_used = TRUE;
373 }
374 }
375
376 /* this reader is already managed by us */
377 if (already_used)
378 {
379 if ((previous_reader_index != -1)
380 && usbDevice[previous_reader_index].handle
381 && (strcmp(usbDevice[previous_reader_index].dirname, bus->dirname) == 0)
382 && (strcmp(usbDevice[previous_reader_index].filename, dev->filename) == 0)
383 && usbDevice[previous_reader_index].ccid.bCurrentSlotIndex < usbDevice[previous_reader_index].ccid.bMaxSlotIndex)
384 {
385 /* we reuse the same device
386 * and the reader is multi-slot */
387 usbDevice[reader_index] = usbDevice[previous_reader_index];
388 /* the other slots do not have the same data rates */
389 if ((GEMCOREPOSPRO == usbDevice[reader_index].ccid.readerID)
390 || (GEMCORESIMPRO == usbDevice[reader_index].ccid.readerID))
391 {
392 usbDevice[reader_index].ccid.arrayOfSupportedDataRates = SerialCustomDataRates;
393 usbDevice[reader_index].ccid.dwMaxDataRate = 125000;
394 }
395
396 *usbDevice[reader_index].nb_opened_slots += 1;
397 usbDevice[reader_index].ccid.bCurrentSlotIndex++;
398 usbDevice[reader_index].ccid.dwSlotStatus =
399 IFD_ICC_PRESENT;
400 DEBUG_INFO2("Opening slot: %d",
401 usbDevice[reader_index].ccid.bCurrentSlotIndex);
402 goto end;
403 }
404 else
405 {
406 /* if an interface number is given by HAL we
407 * continue with this device. */
408 if (-1 == interface_number)
409 {
410 DEBUG_INFO3("USB device %s/%s already in use."
411 " Checking next one.",
412 bus->dirname, dev->filename);
413 continue;
414 }
415 }
416 }
417
418 DEBUG_COMM3("Trying to open USB bus/device: %s/%s",
419 bus->dirname, dev->filename);
420
421 dev_handle = usb_open(dev);
422 if (dev_handle == NULL)
423 {
424 DEBUG_CRITICAL4("Can't usb_open(%s/%s): %s",
425 bus->dirname, dev->filename, strerror(errno));
426
427 continue;
428 }
429
430 /* now we found a free reader and we try to use it */
431 if (dev->config == NULL)
432 {
433 (void)usb_close(dev_handle);
434 DEBUG_CRITICAL3("No dev->config found for %s/%s",
435 bus->dirname, dev->filename);
436 return STATUS_UNSUCCESSFUL;
437 }
438
439 again:
440 usb_interface = get_ccid_usb_interface(dev, &num);
441 if (usb_interface == NULL)
442 {
443 (void)usb_close(dev_handle);
444 if (0 == num)
445 DEBUG_CRITICAL3("Can't find a CCID interface on %s/%s",
446 bus->dirname, dev->filename);
447 return STATUS_UNSUCCESSFUL;
448 }
449
450 if (usb_interface->altsetting->extralen != 54)
451 {
452 (void)usb_close(dev_handle);
453 DEBUG_CRITICAL4("Extra field for %s/%s has a wrong length: %d", bus->dirname, dev->filename, usb_interface->altsetting->extralen);
454 return STATUS_UNSUCCESSFUL;
455 }
456
457 interface = usb_interface->altsetting->bInterfaceNumber;
458 if (interface_number >= 0 && interface != interface_number)
459 {
460 /* an interface was specified and it is not the
461 * current one */
462 DEBUG_INFO3("Wrong interface for USB device %s/%s."
463 " Checking next one.", bus->dirname, dev->filename);
464
465 /* check for another CCID interface on the same device */
466 num++;
467
468 goto again;
469 }
470
471 if (usb_claim_interface(dev_handle, interface) < 0)
472 {
473 (void)usb_close(dev_handle);
474 DEBUG_CRITICAL4("Can't claim interface %s/%s: %s",
475 bus->dirname, dev->filename, strerror(errno));
476 return STATUS_UNSUCCESSFUL;
477 }
478
479 DEBUG_INFO4("Found Vendor/Product: %04X/%04X (%s)",
480 dev->descriptor.idVendor,
481 dev->descriptor.idProduct, keyValue);
482 DEBUG_INFO3("Using USB bus/device: %s/%s",
483 bus->dirname, dev->filename);
484
485 /* check for firmware bugs */
486 if (ccid_check_firmware(dev))
487 {
488 (void)usb_close(dev_handle);
489 return STATUS_UNSUCCESSFUL;
490 }
491
492 /* Get Endpoints values*/
493 (void)get_end_points(dev, &usbDevice[reader_index], num);
494
495 /* store device information */
496 usbDevice[reader_index].handle = dev_handle;
497 usbDevice[reader_index].dirname = strdup(bus->dirname);
498 usbDevice[reader_index].filename = strdup(dev->filename);
499 usbDevice[reader_index].interface = interface;
500 usbDevice[reader_index].real_nb_opened_slots = 1;
501 usbDevice[reader_index].nb_opened_slots = &usbDevice[reader_index].real_nb_opened_slots;
502
503 /* CCID common informations */
504 usbDevice[reader_index].ccid.real_bSeq = 0;
505 usbDevice[reader_index].ccid.pbSeq = &usbDevice[reader_index].ccid.real_bSeq;
506 usbDevice[reader_index].ccid.readerID =
507 (dev->descriptor.idVendor << 16) +
508 dev->descriptor.idProduct;
509 usbDevice[reader_index].ccid.dwFeatures = dw2i(usb_interface->altsetting->extra, 40);
510 usbDevice[reader_index].ccid.bPINSupport = usb_interface->altsetting->extra[52];
511 usbDevice[reader_index].ccid.dwMaxCCIDMessageLength = dw2i(usb_interface->altsetting->extra, 44);
512 usbDevice[reader_index].ccid.dwMaxIFSD = dw2i(usb_interface->altsetting->extra, 28);
513 usbDevice[reader_index].ccid.dwDefaultClock = dw2i(usb_interface->altsetting->extra, 10);
514 usbDevice[reader_index].ccid.dwMaxDataRate = dw2i(usb_interface->altsetting->extra, 23);
515 usbDevice[reader_index].ccid.bMaxSlotIndex = usb_interface->altsetting->extra[4];
516 usbDevice[reader_index].ccid.bCurrentSlotIndex = 0;
517 usbDevice[reader_index].ccid.readTimeout = DEFAULT_COM_READ_TIMEOUT;
518 usbDevice[reader_index].ccid.arrayOfSupportedDataRates = get_data_rates(reader_index, dev, num);
519 usbDevice[reader_index].ccid.bInterfaceProtocol = usb_interface->altsetting->bInterfaceProtocol;
520 usbDevice[reader_index].ccid.bNumEndpoints = usb_interface->altsetting->bNumEndpoints;
521 usbDevice[reader_index].ccid.dwSlotStatus = IFD_ICC_PRESENT;
522 goto end;
523 }
524 }
525 }
526 }
527 end:
528 if (usbDevice[reader_index].handle == NULL)
529 return STATUS_UNSUCCESSFUL;
530
531 /* memorise the current reader_index so we can detect
532 * a new OpenUSBByName on a multi slot reader */
533 previous_reader_index = reader_index;
534
535 return STATUS_SUCCESS;
536 } /* OpenUSBByName */
537
538
539 /*****************************************************************************
540 *
541 * WriteUSB
542 *
543 ****************************************************************************/
544 status_t WriteUSB(unsigned int reader_index, unsigned int length,
545 unsigned char *buffer)
546 {
547 int rv;
548 char debug_header[] = "-> 121234 ";
549
550 (void)snprintf(debug_header, sizeof(debug_header), "-> %06X ",
551 (int)reader_index);
552
553 DEBUG_XXD(debug_header, buffer, length);
554
555 rv = usb_bulk_write(usbDevice[reader_index].handle,
556 usbDevice[reader_index].bulk_out, (char *)buffer, length,
557 USB_WRITE_TIMEOUT);
558
559 if (rv < 0)
560 {
561 DEBUG_CRITICAL4("usb_bulk_write(%s/%s): %s",
562 usbDevice[reader_index].dirname, usbDevice[reader_index].filename,
563 strerror(errno));
564
565 if (ENODEV == errno)
566 return STATUS_NO_SUCH_DEVICE;
567
568 return STATUS_UNSUCCESSFUL;
569 }
570
571 return STATUS_SUCCESS;
572 } /* WriteUSB */
573
574
575 /*****************************************************************************
576 *
577 * ReadUSB
578 *
579 ****************************************************************************/
580 status_t ReadUSB(unsigned int reader_index, unsigned int * length,
581 unsigned char *buffer)
582 {
583 int rv;
584 char debug_header[] = "<- 121234 ";
585 _ccid_descriptor *ccid_descriptor = get_ccid_descriptor(reader_index);
586 int duplicate_frame = 0;
587
588 read_again:
589 (void)snprintf(debug_header, sizeof(debug_header), "<- %06X ",
590 (int)reader_index);
591
592 rv = usb_bulk_read(usbDevice[reader_index].handle,
593 usbDevice[reader_index].bulk_in, (char *)buffer, *length,
594 usbDevice[reader_index].ccid.readTimeout * 1000);
595
596 if (rv < 0)
597 {
598 *length = 0;
599 DEBUG_CRITICAL4("usb_bulk_read(%s/%s): %s",
600 usbDevice[reader_index].dirname, usbDevice[reader_index].filename,
601 strerror(errno));
602
603 if (ENODEV == errno)
604 return STATUS_NO_SUCH_DEVICE;
605
606 return STATUS_UNSUCCESSFUL;
607 }
608
609 *length = rv;
610
611 DEBUG_XXD(debug_header, buffer, *length);
612
613 #define BSEQ_OFFSET 6
614 if ((*length >= BSEQ_OFFSET)
615 && (buffer[BSEQ_OFFSET] < *ccid_descriptor->pbSeq -1))
616 {
617 duplicate_frame++;
618 if (duplicate_frame > 10)
619 {
620 DEBUG_CRITICAL("Too many duplicate frame detected");
621 return STATUS_UNSUCCESSFUL;
622 }
623 DEBUG_INFO("Duplicate frame detected");
624 goto read_again;
625 }
626
627 return STATUS_SUCCESS;
628 } /* ReadUSB */
629
630
631 /*****************************************************************************
632 *
633 * CloseUSB
634 *
635 ****************************************************************************/
636 status_t CloseUSB(unsigned int reader_index)
637 {
638 /* device not opened */
639 if (usbDevice[reader_index].handle == NULL)
640 return STATUS_UNSUCCESSFUL;
641
642 DEBUG_COMM3("Closing USB device: %s/%s",
643 usbDevice[reader_index].dirname,
644 usbDevice[reader_index].filename);
645
646 if (usbDevice[reader_index].ccid.arrayOfSupportedDataRates
647 && (usbDevice[reader_index].ccid.bCurrentSlotIndex == 0))
648 {
649 free(usbDevice[reader_index].ccid.arrayOfSupportedDataRates);
650 usbDevice[reader_index].ccid.arrayOfSupportedDataRates = NULL;
651 }
652
653 /* one slot closed */
654 (*usbDevice[reader_index].nb_opened_slots)--;
655
656 /* release the allocated ressources for the last slot only */
657 if (0 == *usbDevice[reader_index].nb_opened_slots)
658 {
659 DEBUG_COMM("Last slot closed. Release resources");
660
661 /* reset so that bSeq starts at 0 again */
662 if (DriverOptions & DRIVER_OPTION_RESET_ON_CLOSE)
663 (void)usb_reset(usbDevice[reader_index].handle);
664
665 (void)usb_release_interface(usbDevice[reader_index].handle,
666 usbDevice[reader_index].interface);
667 (void)usb_close(usbDevice[reader_index].handle);
668
669 free(usbDevice[reader_index].dirname);
670 free(usbDevice[reader_index].filename);
671 }
672
673 /* mark the resource unused */
674 usbDevice[reader_index].handle = NULL;
675 usbDevice[reader_index].dirname = NULL;
676 usbDevice[reader_index].filename = NULL;
677 usbDevice[reader_index].interface = 0;
678
679 return STATUS_SUCCESS;
680 } /* CloseUSB */
681
682
683 /*****************************************************************************
684 *
685 * get_ccid_descriptor
686 *
687 ****************************************************************************/
688 _ccid_descriptor *get_ccid_descriptor(unsigned int reader_index)
689 {
690 return &usbDevice[reader_index].ccid;
691 } /* get_ccid_descriptor */
692
693
694 /*****************************************************************************
695 *
696 * get_end_points
697 *
698 ****************************************************************************/
699 static int get_end_points(struct usb_device *dev, _usbDevice *usbdevice,
700 int num)
701 {
702 int i;
703 int bEndpointAddress;
704 struct usb_interface *usb_interface = get_ccid_usb_interface(dev, &num);
705
706 /*
707 * 3 Endpoints maximum: Interrupt In, Bulk In, Bulk Out
708 */
709 for (i=0; i<usb_interface->altsetting->bNumEndpoints; i++)
710 {
711 /* interrupt end point (if available) */
712 if (usb_interface->altsetting->endpoint[i].bmAttributes == USB_ENDPOINT_TYPE_INTERRUPT)
713 {
714 usbdevice->interrupt = usb_interface->altsetting->endpoint[i].bEndpointAddress;
715 continue;
716 }
717
718 if (usb_interface->altsetting->endpoint[i].bmAttributes != USB_ENDPOINT_TYPE_BULK)
719 continue;
720
721 bEndpointAddress = usb_interface->altsetting->endpoint[i].bEndpointAddress;
722
723 if ((bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
724 usbdevice->bulk_in = bEndpointAddress;
725
726 if ((bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)
727 usbdevice->bulk_out = bEndpointAddress;
728 }
729
730 return 0;
731 } /* get_end_points */
732
733
734 /*****************************************************************************
735 *
736 * get_ccid_usb_interface
737 *
738 ****************************************************************************/
739 /*@null@*/ EXTERNAL struct usb_interface * get_ccid_usb_interface(
740 struct usb_device *dev, int *num)
741 {
742 struct usb_interface *usb_interface = NULL;
743 int i;
744 #ifdef O2MICRO_OZ776_PATCH
745 int readerID;
746 #endif
747
748 /* if multiple interfaces use the first one with CCID class type */
749 for (i = *num; dev->config && i<dev->config->bNumInterfaces; i++)
750 {
751 /* CCID Class? */
752 if (dev->config->interface[i].altsetting->bInterfaceClass == 0xb
753 #ifdef ALLOW_PROPRIETARY_CLASS
754 || dev->config->interface[i].altsetting->bInterfaceClass == 0xff
755 #endif
756 )
757 {
758 usb_interface = &dev->config->interface[i];
759 /* store the interface number for further reference */
760 *num = i;
761 break;
762 }
763 }
764
765 #ifdef O2MICRO_OZ776_PATCH
766 readerID = (dev->descriptor.idVendor << 16) + dev->descriptor.idProduct;
767 if (usb_interface != NULL
768 && ((OZ776 == readerID) || (OZ776_7772 == readerID)
769 || (REINER_SCT == readerID) || (BLUDRIVEII_CCID == readerID))
770 && (0 == usb_interface->altsetting->extralen)) /* this is the bug */
771 {
772 int j;
773 for (j=0; j<usb_interface->altsetting->bNumEndpoints; j++)
774 {
775 /* find the extra[] array */
776 if (54 == usb_interface->altsetting->endpoint[j].extralen)
777 {
778 /* get the extra[] from the endpoint */
779 usb_interface->altsetting->extralen = 54;
780 usb_interface->altsetting->extra =
781 usb_interface->altsetting->endpoint[j].extra;
782 /* avoid double free on close */
783 usb_interface->altsetting->endpoint[j].extra = NULL;
784 usb_interface->altsetting->endpoint[j].extralen = 0;
785 break;
786 }
787 }
788 }
789 #endif
790
791 return usb_interface;
792 } /* get_ccid_usb_interface */
793
794
795 /*****************************************************************************
796 *
797 * ccid_check_firmware
798 *
799 ****************************************************************************/
800 int ccid_check_firmware(struct usb_device *dev)
801 {
802 unsigned int i;
803
804 for (i=0; i<sizeof(Bogus_firmwares)/sizeof(Bogus_firmwares[0]); i++)
805 {
806 if (dev->descriptor.idVendor != Bogus_firmwares[i].vendor)
807 continue;
808
809 if (dev->descriptor.idProduct != Bogus_firmwares[i].product)
810 continue;
811
812 /* firmware too old and buggy */
813 if (dev->descriptor.bcdDevice < Bogus_firmwares[i].firmware)
814 {
815 if (DriverOptions & DRIVER_OPTION_USE_BOGUS_FIRMWARE)
816 {
817 DEBUG_INFO3("Firmware (%X.%02X) is bogus! but you choosed to use it",
818 dev->descriptor.bcdDevice >> 8,
819 dev->descriptor.bcdDevice & 0xFF);
820 return FALSE;
821 }
822 else
823 {
824 DEBUG_CRITICAL3("Firmware (%X.%02X) is bogus! Upgrade the reader firmware or get a new reader.",
825 dev->descriptor.bcdDevice >> 8,
826 dev->descriptor.bcdDevice & 0xFF);
827 return TRUE;
828 }
829 }
830 }
831
832 /* by default the firmware is not bogus */
833 return FALSE;
834 } /* ccid_check_firmware */
835
836
837 /*****************************************************************************
838 *
839 * get_data_rates
840 *
841 ****************************************************************************/
842 static unsigned int *get_data_rates(unsigned int reader_index,
843 struct usb_device *dev, int num)
844 {
845 int n, i, len;
846 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
847 unsigned int *int_array;
848
849 /* See CCID 3.7.3 page 25 */
850 n = ControlUSB(reader_index,
851 0xA1, /* request type */
852 0x03, /* GET_DATA_RATES */
853 0x00, /* value */
854 buffer, sizeof(buffer));
855
856 /* we got an error? */
857 if (n <= 0)
858 {
859 DEBUG_INFO2("IFD does not support GET_DATA_RATES request: %s",
860 strerror(errno));
861 return NULL;
862 }
863
864 /* we got a strange value */
865 if (n % 4)
866 {
867 DEBUG_INFO2("Wrong GET DATA RATES size: %d", n);
868 return NULL;
869 }
870
871 /* allocate the buffer (including the end marker) */
872 n /= sizeof(int);
873
874 /* we do not get the expected number of data rates */
875 len = get_ccid_usb_interface(dev, &num)->altsetting->extra[27]; /* bNumDataRatesSupported */
876 if ((n != len) && len)
877 {
878 DEBUG_INFO3("Got %d data rates but was expecting %d", n, len);
879
880 /* we got more data than expected */
881 if (n > len)
882 n = len;
883 }
884
885 int_array = calloc(n+1, sizeof(int));
886 if (NULL == int_array)
887 {
888 DEBUG_CRITICAL("Memory allocation failed");
889 return NULL;
890 }
891
892 /* convert in correct endianess */
893 for (i=0; i<n; i++)
894 {
895 int_array[i] = dw2i(buffer, i*4);
896 DEBUG_INFO2("declared: %d bps", int_array[i]);
897 }
898
899 /* end of array marker */
900 int_array[i] = 0;
901
902 return int_array;
903 } /* get_data_rates */
904
905
906 /*****************************************************************************
907 *
908 * ControlUSB
909 *
910 ****************************************************************************/
911 int ControlUSB(int reader_index, int requesttype, int request, int value,
912 unsigned char *bytes, unsigned int size)
913 {
914 int ret;
915
916 DEBUG_COMM2("request: 0x%02X", request);
917
918 if (0 == (requesttype & 0x80))
919 DEBUG_XXD("send: ", bytes, size);
920
921 ret = usb_control_msg(usbDevice[reader_index].handle, requesttype,
922 request, value, usbDevice[reader_index].interface, (char *)bytes, size,
923 usbDevice[reader_index].ccid.readTimeout * 1000);
924
925 if (requesttype & 0x80)
926 DEBUG_XXD("receive: ", bytes, ret);
927
928 return ret;
929 } /* ControlUSB */
930
931 /*****************************************************************************
932 *
933 * InterruptRead
934 *
935 *
936 ****************************************************************************/
937 int InterruptRead(int reader_index)
938 {
939 int ret;
940 char buffer[8];
941 int timeout = 2*1000; /* 2 seconds */
942
943 DEBUG_COMM("before");
944 ret = usb_interrupt_read(usbDevice[reader_index].handle,
945 usbDevice[reader_index].interrupt, buffer, sizeof(buffer), timeout);
946 DEBUG_COMM2("after %d", ret);
947
948 if (ret < 0)
949 {
950 /* if usb_interrupt_read() times out we get EILSEQ or EAGAIN */
951 if ((errno != EILSEQ) && (errno != EAGAIN) && (errno != ENODEV) && (errno != 0))
952 DEBUG_COMM4("usb_interrupt_read(%s/%s): %s",
953 usbDevice[reader_index].dirname,
954 usbDevice[reader_index].filename, strerror(errno));
955 }
956 else
957 DEBUG_XXD("NotifySlotChange: ", buffer, ret);
958
959 return ret;
960 } /* InterruptRead */
961

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5