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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5474 - (show annotations) (download)
Tue Jan 4 09:53:35 2011 UTC (2 years, 5 months ago) by rousseau
File MIME type: text/plain
File size: 17563 byte(s)
Use get_ccid_device_descriptor() instead of using
usb_interface_descriptor->extra directly to support "bogus" readers
1 /*
2 parse.c: parse CCID structure
3 Copyright (C) 2003-2010 Ludovic Rousseau
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program 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
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc., 51
17 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 /*
21 * $Id$
22 */
23
24 #include <stdio.h>
25 #include <string.h>
26 # ifdef S_SPLINT_S
27 # include <sys/types.h>
28 # endif
29 #include <errno.h>
30
31 #include "defs.h"
32 #include "ccid.h"
33
34 /* define DISPLAY_EXTRA_VALUES to display the extra (invalid) values
35 * returned by bNumClockSupported and bNumDataRatesSupported */
36 #undef DISPLAY_EXTRA_VALUES
37
38 #ifndef TRUE
39 #define TRUE 1
40 #define FALSE 0
41 #endif
42
43 #define BLUE "\33[34m"
44 #define RED "\33[31m"
45 #define BRIGHT_RED "\33[01;31m"
46 #define GREEN "\33[32m"
47 #define MAGENTA "\33[35m"
48 #define NORMAL "\33[0m"
49
50 /* global variables used in ccid_usb.c but defined in ifdhandler.c */
51 int LogLevel = 1+2+4+8; /* full debug */
52 int DriverOptions = 0;
53
54 static int ccid_parse_interface_descriptor(libusb_device_handle *handle,
55 struct libusb_device_descriptor desc,
56 struct libusb_config_descriptor *config_desc,
57 int num,
58 const struct libusb_interface *usb_interface);
59
60
61 /*****************************************************************************
62 *
63 * main
64 *
65 ****************************************************************************/
66 int main(int argc, char *argv[])
67 {
68 libusb_device **devs, *dev;
69 int nb = 0, r, i;
70 unsigned char buffer[256];
71 char class_ff = FALSE;
72 ssize_t cnt;
73
74 if ((argc > 1) && (0 == strcmp(argv[1], "-p")))
75 class_ff = TRUE;
76
77 r = libusb_init(NULL);
78 if (r < 0)
79 {
80 (void)printf("libusb_init() failed\n");
81 return r;
82 }
83
84 cnt = libusb_get_device_list(NULL, &devs);
85 if (cnt < 0)
86 {
87 (void)printf("libusb_get_device_list() failed\n");
88 return (int)cnt;
89 }
90
91 /* for every device */
92 i = 0;
93 while ((dev = devs[i++]) != NULL)
94 {
95 struct libusb_device_descriptor desc;
96 struct libusb_config_descriptor *config_desc;
97 struct libusb_device_handle *handle;
98 const struct libusb_interface *usb_interface = NULL;
99 int interface;
100 int num = 0;
101
102 r = libusb_open(dev, &handle);
103 if (r < 0)
104 {
105 (void)fprintf(stderr, "Can't libusb_open(): %s\n", strerror(errno));
106 if (getuid())
107 {
108 (void)fprintf(stderr,
109 BRIGHT_RED "Please, restart the command as root\n" NORMAL);
110 return 1;
111 }
112 continue;
113 }
114
115 r = libusb_get_device_descriptor(dev, &desc);
116 if (r < 0)
117 {
118 (void)fprintf(stderr,
119 BRIGHT_RED "failed to get device descriptor" NORMAL);
120 return 1;
121 }
122
123 (void)fprintf(stderr,
124 "Parsing USB bus/device: %04X:%04X (bus %d, device %d)\n",
125 desc.idVendor, desc.idProduct,
126 libusb_get_bus_number(dev), libusb_get_device_address(dev));
127
128 (void)fprintf(stderr, " idVendor: 0x%04X", desc.idVendor);
129 r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
130 buffer, sizeof(buffer));
131 if (r < 0)
132 {
133 (void)fprintf(stderr, " Can't get iManufacturer string\n");
134 if (getuid())
135 {
136 (void)fprintf(stderr,
137 BRIGHT_RED "Please, restart the command as root\n" NORMAL);
138 return 1;
139 }
140 }
141 else
142 (void)fprintf(stderr,
143 " iManufacturer: " BLUE "%s\n" NORMAL, buffer);
144
145 (void)fprintf(stderr, " idProduct: 0x%04X", desc.idProduct);
146 r = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
147 buffer, sizeof(buffer));
148 if (r < 0)
149 (void)fprintf(stderr, " Can't get iProduct string\n");
150 else
151 (void)fprintf(stderr, " iProduct: " BLUE "%s\n" NORMAL, buffer);
152
153 again:
154 /* check if the device has bInterfaceClass == 11 */
155 r = libusb_get_active_config_descriptor(dev, &config_desc);
156 if (r < 0)
157 {
158 (void)fprintf(stderr, " Can't get config descriptor: %d\n", r);
159 continue;
160 }
161
162 usb_interface = get_ccid_usb_interface(config_desc, &num);
163 if (NULL == usb_interface)
164 {
165 (void)libusb_close(handle);
166 /* only if we found no CCID interface */
167 if (0 == num)
168 (void)fprintf(stderr, RED " NOT a CCID/ICCD device\n" NORMAL);
169 continue;
170 }
171 if (!class_ff && (0xFF == usb_interface->altsetting->bInterfaceClass))
172 {
173 (void)fprintf(stderr, MAGENTA " Found a possibly CCID/ICCD device (bInterfaceClass = 0xFF). Use -p\n" NORMAL);
174 continue;
175 }
176 (void)fprintf(stderr,
177 GREEN " Found a CCID/ICCD device at interface %d\n" NORMAL, num);
178
179 /* now we found a free reader and we try to use it */
180 #if 0
181 if (NULL == dev->config)
182 {
183 (void)libusb_close(handle);
184 (void)fprintf(stderr, "No dev->config found for %s/%s\n",
185 bus->dirname, dev->filename);
186 continue;
187 }
188 #endif
189
190 interface = usb_interface->altsetting->bInterfaceNumber;
191 #ifndef __APPLE__
192 r = libusb_claim_interface(handle, interface);
193 if (r < 0)
194 {
195 (void)fprintf(stderr,
196 "Can't claim interface (bus %d, device %d): %s\n",
197 libusb_get_bus_number(dev), libusb_get_device_address(dev),
198 strerror(errno));
199 (void)libusb_close(handle);
200
201 if (EBUSY == errno)
202 {
203 (void)fprintf(stderr,
204 BRIGHT_RED " Please, stop pcscd and retry\n\n" NORMAL);
205 return TRUE;
206 }
207 continue;
208 }
209 #endif
210
211 (void)ccid_parse_interface_descriptor(handle, desc, config_desc, num,
212 usb_interface);
213
214 #ifndef __APPLE__
215 (void)libusb_release_interface(handle, interface);
216 #endif
217 /* check for another CCID interface on the same device */
218 num++;
219 goto again;
220
221 (void)libusb_close(handle);
222 nb++;
223 }
224
225 if ((0 == nb) && (0 != geteuid()))
226 (void)fprintf(stderr,
227 "Can't find any CCID device.\nMaybe you must run parse as root?\n");
228
229 libusb_exit(NULL);
230
231 return 0;
232 } /* main */
233
234
235 /*****************************************************************************
236 *
237 * Parse a CCID USB Descriptor
238 *
239 ****************************************************************************/
240 static int ccid_parse_interface_descriptor(libusb_device_handle *handle,
241 struct libusb_device_descriptor desc,
242 struct libusb_config_descriptor *config_desc,
243 int num,
244 const struct libusb_interface *usb_interface)
245 {
246 const struct libusb_interface_descriptor *usb_interface_descriptor;
247 const unsigned char *device_descriptor;
248 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
249 int r;
250
251 /*
252 * Vendor/model name
253 */
254 (void)printf(" idVendor: 0x%04X\n", desc.idVendor);
255 r = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer,
256 buffer, sizeof(buffer));
257 if (r < 0)
258 {
259 (void)printf(" Can't get iManufacturer string\n");
260 if (getuid())
261 {
262 (void)fprintf(stderr,
263 BRIGHT_RED "Please, restart the command as root\n\n" NORMAL);
264 return TRUE;
265 }
266 }
267 else
268 (void)printf(" iManufacturer: %s\n", buffer);
269
270 (void)printf(" idProduct: 0x%04X\n", desc.idProduct);
271 r = libusb_get_string_descriptor_ascii(handle, desc.iProduct,
272 buffer, sizeof(buffer));
273 if (r < 0)
274 (void)printf(" Can't get iProduct string\n");
275 else
276 (void)printf(" iProduct: %s\n", buffer);
277
278 (void)printf(" bcdDevice: %X.%02X (firmware release?)\n",
279 desc.bcdDevice >> 8, desc.bcdDevice & 0xFF);
280
281 usb_interface_descriptor = get_ccid_usb_interface(config_desc, &num)->altsetting;
282
283 (void)printf(" bLength: %d\n", usb_interface_descriptor->bLength);
284
285 (void)printf(" bDescriptorType: %d\n", usb_interface_descriptor->bDescriptorType);
286
287 (void)printf(" bInterfaceNumber: %d\n", usb_interface_descriptor->bInterfaceNumber);
288
289 (void)printf(" bAlternateSetting: %d\n", usb_interface_descriptor->bAlternateSetting);
290
291 (void)printf(" bNumEndpoints: %d\n", usb_interface_descriptor->bNumEndpoints);
292 switch (usb_interface_descriptor->bNumEndpoints)
293 {
294 case 0:
295 (void)printf(" Control only\n");
296 break;
297 case 1:
298 (void)printf(" Interrupt-IN\n");
299 break;
300 case 2:
301 (void)printf(" bulk-IN and bulk-OUT\n");
302 break;
303 case 3:
304 (void)printf(" bulk-IN, bulk-OUT and Interrupt-IN\n");
305 break;
306 default:
307 (void)printf(" UNKNOWN value\n");
308 }
309
310 (void)printf(" bInterfaceClass: 0x%02X", usb_interface_descriptor->bInterfaceClass);
311 if (usb_interface_descriptor->bInterfaceClass == 0x0b)
312 (void)printf(" [Chip Card Interface Device Class (CCID)]\n");
313 else
314 {
315 (void)printf("\n NOT A CCID DEVICE\n");
316 if (usb_interface_descriptor->bInterfaceClass != 0xFF)
317 return TRUE;
318 else
319 (void)printf(" Class is 0xFF (proprietary)\n");
320 }
321
322 (void)printf(" bInterfaceSubClass: %d\n",
323 usb_interface_descriptor->bInterfaceSubClass);
324 if (usb_interface_descriptor->bInterfaceSubClass)
325 (void)printf(" UNSUPPORTED SubClass\n");
326
327 (void)printf(" bInterfaceProtocol: %d\n",
328 usb_interface_descriptor->bInterfaceProtocol);
329 switch (usb_interface_descriptor->bInterfaceProtocol)
330 {
331 case 0:
332 (void)printf(" bulk transfer, optional interrupt-IN (CCID)\n");
333 break;
334 case 1:
335 (void)printf(" ICCD Version A, Control transfers, (no interrupt-IN)\n");
336 break;
337 case 2:
338 (void)printf(" ICCD Version B, Control transfers, (optional interrupt-IN)\n");
339 break;
340 default:
341 (void)printf(" UNSUPPORTED InterfaceProtocol\n");
342 }
343
344 r = libusb_get_string_descriptor_ascii(handle, usb_interface_descriptor->iInterface,
345 buffer, sizeof(buffer));
346 if (r < 0)
347 (void)printf(" Can't get iInterface string\n");
348 else
349 (void)printf(" iInterface: %s\n", buffer);
350
351 device_descriptor = get_ccid_device_descriptor(usb_interface);
352 if (NULL == device_descriptor)
353 {
354 (void)printf("\n NOT A CCID DEVICE\n");
355 return TRUE;
356 }
357
358 /*
359 * CCID Class Descriptor
360 */
361 (void)printf(" CCID Class Descriptor\n");
362
363 (void)printf(" bLength: 0x%02X\n", device_descriptor[0]);
364 if (device_descriptor[0] != 0x36)
365 {
366 (void)printf(" UNSUPPORTED bLength\n");
367 return TRUE;
368 }
369
370 (void)printf(" bDescriptorType: 0x%02X\n", device_descriptor[1]);
371 if (device_descriptor[1] != 0x21)
372 {
373 if (0xFF == device_descriptor[1])
374 (void)printf(" PROPRIETARY bDescriptorType\n");
375 else
376 {
377 (void)printf(" UNSUPPORTED bDescriptorType\n");
378 return TRUE;
379 }
380 }
381
382 (void)printf(" bcdCCID: %X.%02X\n", device_descriptor[3], device_descriptor[2]);
383 (void)printf(" bMaxSlotIndex: 0x%02X\n", device_descriptor[4]);
384 (void)printf(" bVoltageSupport: 0x%02X\n", device_descriptor[5]);
385 if (device_descriptor[5] & 0x01)
386 (void)printf(" 5.0V\n");
387 if (device_descriptor[5] & 0x02)
388 (void)printf(" 3.0V\n");
389 if (device_descriptor[5] & 0x04)
390 (void)printf(" 1.8V\n");
391
392 (void)printf(" dwProtocols: 0x%02X%02X 0x%02X%02X\n", device_descriptor[9], device_descriptor[8],
393 device_descriptor[7], device_descriptor[6]);
394 if (device_descriptor[6] & 0x01)
395 (void)printf(" T=0\n");
396 if (device_descriptor[6] & 0x02)
397 (void)printf(" T=1\n");
398
399 (void)printf(" dwDefaultClock: %.3f MHz\n", dw2i(device_descriptor, 10)/1000.0);
400 (void)printf(" dwMaximumClock: %.3f MHz\n", dw2i(device_descriptor, 14)/1000.0);
401 (void)printf(" bNumClockSupported: %d%s\n", device_descriptor[18],
402 device_descriptor[18] ? "" : " (will use whatever is returned)");
403 {
404 int n;
405
406 /* See CCID 3.7.2 page 25 */
407 n = libusb_control_transfer(handle,
408 0xA1, /* request type */
409 0x02, /* GET CLOCK FREQUENCIES */
410 0x00, /* value */
411 usb_interface_descriptor->bInterfaceNumber, /* interface */
412 buffer,
413 sizeof(buffer),
414 2 * 1000);
415
416 /* we got an error? */
417 if (n <= 0)
418 {
419 (void)(void)printf(" IFD does not support GET CLOCK FREQUENCIES request: %s\n", strerror(errno));
420 if (EBUSY == errno)
421 {
422 (void)fprintf(stderr,
423 BRIGHT_RED " Please, stop pcscd and retry\n\n" NORMAL);
424 return TRUE;
425 }
426 }
427 else
428 if (n % 4) /* not a multiple of 4 */
429 (void)printf(" wrong size for GET CLOCK FREQUENCIES: %d\n", n);
430 else
431 {
432 int i;
433
434 /* we do not get the expected number of data rates */
435 if ((n != device_descriptor[18]*4) && device_descriptor[18])
436 {
437 (void)printf(" Got %d clock frequencies but was expecting %d\n",
438 n/4, device_descriptor[18]);
439
440 /* we got more data than expected */
441 #ifndef DISPLAY_EXTRA_VALUES
442 if (n > device_descriptor[18]*4)
443 n = device_descriptor[18]*4;
444 #endif
445 }
446
447 for (i=0; i<n; i+=4)
448 (void)printf(" Support %d kHz\n", dw2i(buffer, i));
449 }
450 }
451 (void)printf(" dwDataRate: %d bps\n", dw2i(device_descriptor, 19));
452 (void)printf(" dwMaxDataRate: %d bps\n", dw2i(device_descriptor, 23));
453 (void)printf(" bNumDataRatesSupported: %d%s\n", device_descriptor[27],
454 device_descriptor[27] ? "" : " (will use whatever is returned)");
455 {
456 int n;
457
458 /* See CCID 3.7.3 page 25 */
459 n = libusb_control_transfer(handle,
460 0xA1, /* request type */
461 0x03, /* GET DATA RATES */
462 0x00, /* value */
463 usb_interface_descriptor->bInterfaceNumber, /* interface */
464 buffer,
465 sizeof(buffer),
466 2 * 1000);
467
468 /* we got an error? */
469 if (n <= 0)
470 (void)printf(" IFD does not support GET_DATA_RATES request: %s\n",
471 strerror(errno));
472 else
473 if (n % 4) /* not a multiple of 4 */
474 (void)printf(" wrong size for GET_DATA_RATES: %d\n", n);
475 else
476 {
477 int i;
478
479 /* we do not get the expected number of data rates */
480 if ((n != device_descriptor[27]*4) && device_descriptor[27])
481 {
482 (void)printf(" Got %d data rates but was expecting %d\n", n/4,
483 device_descriptor[27]);
484
485 /* we got more data than expected */
486 #ifndef DISPLAY_EXTRA_VALUES
487 if (n > device_descriptor[27]*4)
488 n = device_descriptor[27]*4;
489 #endif
490 }
491
492 for (i=0; i<n; i+=4)
493 (void)printf(" Support %d bps\n", dw2i(buffer, i));
494 }
495 }
496 (void)printf(" dwMaxIFSD: %d\n", dw2i(device_descriptor, 28));
497 (void)printf(" dwSynchProtocols: 0x%08X\n", dw2i(device_descriptor, 32));
498 if (device_descriptor[32] & 0x01)
499 (void)printf(" 2-wire protocol\n");
500 if (device_descriptor[32] & 0x02)
501 (void)printf(" 3-wire protocol\n");
502 if (device_descriptor[32] & 0x04)
503 (void)printf(" I2C protocol\n");
504
505 (void)printf(" dwMechanical: 0x%08X\n", dw2i(device_descriptor, 36));
506 if (device_descriptor[36] == 0)
507 (void)printf(" No special characteristics\n");
508 if (device_descriptor[36] & 0x01)
509 (void)printf(" Card accept mechanism\n");
510 if (device_descriptor[36] & 0x02)
511 (void)printf(" Card ejection mechanism\n");
512 if (device_descriptor[36] & 0x04)
513 (void)printf(" Card capture mechanism\n");
514 if (device_descriptor[36] & 0x08)
515 (void)printf(" Card lock/unlock mechanism\n");
516
517 (void)printf(" dwFeatures: 0x%08X\n", dw2i(device_descriptor, 40));
518 if (dw2i(device_descriptor, 40) == 0)
519 (void)printf(" No special characteristics\n");
520 if (device_descriptor[40] & 0x02)
521 (void)printf(" ....02 Automatic parameter configuration based on ATR data\n");
522 if (device_descriptor[40] & 0x04)
523 (void)printf(" ....04 Automatic activation of ICC on inserting\n");
524 if (device_descriptor[40] & 0x08)
525 (void)printf(" ....08 Automatic ICC voltage selection\n");
526 if (device_descriptor[40] & 0x10)
527 (void)printf(" ....10 Automatic ICC clock frequency change according to parameters\n");
528 if (device_descriptor[40] & 0x20)
529 (void)printf(" ....20 Automatic baud rate change according to frequency and Fi, Di params\n");
530 if (device_descriptor[40] & 0x40)
531 (void)printf(" ....40 Automatic parameters negotiation made by the CCID\n");
532 if (device_descriptor[40] & 0x80)
533 (void)printf(" ....80 Automatic PPS made by the CCID\n");
534 if (device_descriptor[41] & 0x01)
535 (void)printf(" ..01.. CCID can set ICC in clock stop mode\n");
536 if (device_descriptor[41] & 0x02)
537 (void)printf(" ..02.. NAD value other than 00 accepted (T=1)\n");
538 if (device_descriptor[41] & 0x04)
539 (void)printf(" ..04.. Automatic IFSD exchange as first exchange (T=1)\n");
540 if (device_descriptor[41] & 0x08)
541 (void)printf(" ..08.. Unknown (ICCD?)\n");
542 switch (device_descriptor[42] & 0x07)
543 {
544 case 0x00:
545 (void)printf(" 00.... Character level exchange\n");
546 break;
547
548 case 0x01:
549 (void)printf(" 01.... TPDU level exchange\n");
550 break;
551
552 case 0x02:
553 (void)printf(" 02.... Short APDU level exchange\n");
554 break;
555
556 case 0x04:
557 (void)printf(" 04.... Short and Extended APDU level exchange\n");
558 break;
559 }
560 if (device_descriptor[42] & 0x10)
561 (void)printf(" 10.... USB Wake up signaling supported on card insertion and removal\n");
562
563 (void)printf(" dwMaxCCIDMessageLength: %d bytes\n", dw2i(device_descriptor, 44));
564 (void)printf(" bClassGetResponse: 0x%02X\n", device_descriptor[48]);
565 if (0xFF == device_descriptor[48])
566 (void)printf(" echoes the APDU class\n");
567 (void)printf(" bClassEnveloppe: 0x%02X\n", device_descriptor[49]);
568 if (0xFF == device_descriptor[49])
569 (void)printf(" echoes the APDU class\n");
570 (void)printf(" wLcdLayout: 0x%04X\n", (device_descriptor[51] << 8)+device_descriptor[50]);
571 if (device_descriptor[50])
572 (void)printf(" %d lines\n", device_descriptor[50]);
573 if (device_descriptor[51])
574 (void)printf(" %d characters per line\n", device_descriptor[51]);
575 (void)printf(" bPINSupport: 0x%02X\n", device_descriptor[52]);
576 if (device_descriptor[52] & 0x01)
577 (void)printf(" PIN Verification supported\n");
578 if (device_descriptor[52] & 0x02)
579 (void)printf(" PIN Modification supported\n");
580 (void)printf(" bMaxCCIDBusySlots: %d\n", device_descriptor[53]);
581
582 return FALSE;
583 } /* ccid_parse_interface_descriptor */
584

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5