/[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 2328 - (show annotations) (download)
Wed Jan 10 20:53:44 2007 UTC (6 years, 4 months ago) by rousseau
File MIME type: text/plain
File size: 11332 byte(s)
typo: "busses" -> "buses"

Thanks to Martin Paljak for the patch
1 /*
2 parse.c: parse CCID structure
3 Copyright (C) 2003-2004 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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 <usb.h>
30 #include <errno.h>
31
32 #include "defs.h"
33 #include "ccid.h"
34
35 #ifndef TRUE
36 #define TRUE 1
37 #define FALSE 0
38 #endif
39
40 static int ccid_parse_interface_descriptor(usb_dev_handle *handle,
41 struct usb_device *dev);
42
43
44 /*****************************************************************************
45 *
46 * main
47 *
48 ****************************************************************************/
49 int main(void)
50 {
51 static struct usb_bus *busses = NULL;
52 struct usb_bus *bus;
53 struct usb_dev_handle *dev_handle;
54
55 usb_init();
56 usb_find_busses();
57 usb_find_devices();
58
59 busses = usb_get_busses();
60 if (busses == NULL)
61 {
62 printf("No USB buses found\n");
63 return -1;
64 }
65
66 /* on any USB buses */
67 for (bus = busses; bus; bus = bus->next)
68 {
69 struct usb_device *dev;
70
71 /* any device on this bus */
72 for (dev = bus->devices; dev; dev = dev->next)
73 {
74 struct usb_interface *usb_interface = NULL;
75
76 /* check if the device has bInterfaceClass == 11 */
77 usb_interface = get_ccid_usb_interface(dev);
78 if (NULL == usb_interface)
79 continue;
80
81 fprintf(stderr, "Trying to open USB bus/device: %s/%s\n",
82 bus->dirname, dev->filename);
83
84 dev_handle = usb_open(dev);
85 if (NULL == dev_handle)
86 {
87 fprintf(stderr, "Can't usb_open(%s/%s): %s\n",
88 bus->dirname, dev->filename, strerror(errno));
89 continue;
90 }
91
92 /* now we found a free reader and we try to use it */
93 if (NULL == dev->config)
94 {
95 usb_close(dev_handle);
96 fprintf(stderr, "No dev->config found for %s/%s\n",
97 bus->dirname, dev->filename);
98 continue;
99 }
100
101 usb_interface = get_ccid_usb_interface(dev);
102 if (NULL == usb_interface)
103 {
104 usb_close(dev_handle);
105 fprintf(stderr, "Can't find a CCID interface on %s/%s\n",
106 bus->dirname, dev->filename);
107 continue;
108 }
109
110 ccid_parse_interface_descriptor(dev_handle, dev);
111 usb_close(dev_handle);
112 }
113 }
114
115 return 0;
116 } /* main */
117
118
119 /*****************************************************************************
120 *
121 * Parse a CCID USB Descriptor
122 *
123 ****************************************************************************/
124 static int ccid_parse_interface_descriptor(usb_dev_handle *handle,
125 struct usb_device *dev)
126 {
127 struct usb_interface_descriptor *usb_interface;
128 unsigned char *extra;
129 char buffer[255];
130
131 /*
132 * Vendor/model name
133 */
134 printf(" idVendor: 0x%04X\n", dev->descriptor.idVendor);
135 if (usb_get_string_simple(handle, dev->descriptor.iManufacturer,
136 buffer, sizeof(buffer)) < 0)
137 {
138 printf(" Can't get iManufacturer string\n");
139 if (getuid())
140 {
141 printf("\33[01;31mPlease, restart the command as root\33[0m\n\n");
142 return TRUE;
143 }
144 }
145 else
146 printf(" iManufacturer: %s\n", buffer);
147
148 printf(" idProduct: 0x%04X\n", dev->descriptor.idProduct);
149 if (usb_get_string_simple(handle, dev->descriptor.iProduct,
150 buffer, sizeof(buffer)) < 0)
151 printf(" Can't get iProduct string\n");
152 else
153 printf(" iProduct: %s\n", buffer);
154
155 printf(" bcdDevice: %X.%02X (firmware release?)\n",
156 dev->descriptor.bcdDevice >> 8, dev->descriptor.bcdDevice & 0xFF);
157
158 usb_interface = get_ccid_usb_interface(dev)->altsetting;
159
160 printf(" bLength: %d\n", usb_interface->bLength);
161
162 printf(" bDescriptorType: %d\n", usb_interface->bDescriptorType);
163
164 printf(" bInterfaceNumber: %d\n", usb_interface->bInterfaceNumber);
165
166 printf(" bAlternateSetting: %d\n", usb_interface->bAlternateSetting);
167
168 printf(" bNumEndpoints: %d\n", usb_interface->bNumEndpoints);
169
170 printf(" bInterfaceClass: 0x%02X", usb_interface->bInterfaceClass);
171 if (usb_interface->bInterfaceClass == 0x0b)
172 printf(" [Chip Card Interface Device Class (CCID)]\n");
173 else
174 {
175 printf("\n NOT A CCID DEVICE\n");
176 if (usb_interface->bInterfaceClass != 0xFF)
177 return TRUE;
178 else
179 printf(" Class is 0xFF (proprietary)\n");
180 }
181
182 printf(" bInterfaceSubClass: %d\n", usb_interface->bInterfaceSubClass);
183 if (usb_interface->bInterfaceSubClass)
184 printf(" UNSUPPORTED SubClass\n");
185
186 printf(" bInterfaceProtocol: %d\n", usb_interface->bInterfaceProtocol);
187 if (usb_interface->bInterfaceProtocol)
188 printf(" UNSUPPORTED InterfaceProtocol\n");
189
190 printf(" iInterface: %d\n", usb_interface->iInterface);
191
192 if (usb_interface->extralen < 54)
193 {
194 printf("USB extra length is too short: %d\n", usb_interface->extralen);
195 printf("\n NOT A CCID DEVICE\n");
196 return TRUE;
197 }
198
199 /*
200 * CCID Class Descriptor
201 */
202 printf(" CCID Class Descriptor\n");
203 extra = usb_interface->extra;
204
205 printf(" bLength: 0x%02X\n", extra[0]);
206 if (extra[0] != 0x36)
207 {
208 printf(" UNSUPPORTED bLength\n");
209 return TRUE;
210 }
211
212 printf(" bDescriptorType: 0x%02X\n", extra[1]);
213 if (extra[1] != 0x21)
214 {
215 if (0xFF == extra[1])
216 printf(" PROPRIETARY bDescriptorType\n");
217 else
218 {
219 printf(" UNSUPPORTED bDescriptorType\n");
220 return TRUE;
221 }
222 }
223
224 printf(" bcdCCID: %X.%02X\n", extra[3], extra[2]);
225 printf(" bMaxSlotIndex: 0x%02X\n", extra[4]);
226 printf(" bVoltageSupport: 0x%02X\n", extra[5]);
227 if (extra[5] & 0x01)
228 printf(" 5.0V\n");
229 if (extra[5] & 0x02)
230 printf(" 3.0V\n");
231 if (extra[5] & 0x04)
232 printf(" 1.8V\n");
233
234 printf(" dwProtocols: 0x%02X%02X 0x%02X%02X\n", extra[9], extra[8],
235 extra[7],extra[6]);
236 if (extra[6] & 0x01)
237 printf(" T=0\n");
238 if (extra[6] & 0x02)
239 printf(" T=1\n");
240
241 printf(" dwDefaultClock: %.3f MHz\n", dw2i(extra, 10)/1000.0);
242 printf(" dwMaximumClock: %.3f MHz\n", dw2i(extra, 14)/1000.0);
243 printf(" bNumClockSupported: %d %s\n", extra[18],
244 extra[18] ? "" : "(will use whatever is returned)");
245 {
246 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
247 int n;
248
249 /* See CCID 3.7.2 page 25 */
250 n = usb_control_msg(handle,
251 0xA1, /* request type */
252 0x02, /* GET CLOCK FREQUENCIES */
253 0x00, /* value */
254 usb_interface->bInterfaceNumber, /* interface */
255 (char *)buffer,
256 sizeof(buffer),
257 2 * 1000);
258
259 /* we got an error? */
260 if (n <= 0)
261 printf(" IFD does not support GET CLOCK FREQUENCIES request\n");
262 else
263 if (n % 4) /* not a multiple of 4 */
264 printf(" wrong size for GET CLOCK FREQUENCIES: %d\n", n);
265 else
266 {
267 int i;
268
269 /* we do not get the expected number of data rates */
270 if ((n != extra[18]*4) && extra[18])
271 {
272 printf(" Got %d clock frequencies but was expecting %d\n",
273 n/4, extra[18]);
274
275 /* we got more data than expected */
276 if (n > extra[18]*4)
277 n = extra[18]*4;
278 }
279
280 for (i=0; i<n; i+=4)
281 printf(" Support %d kHz\n", dw2i(buffer, i));
282 }
283 }
284 printf(" dwDataRate: %d bps\n", dw2i(extra, 19));
285 printf(" dwMaxDataRate: %d bps\n", dw2i(extra, 23));
286 printf(" bNumDataRatesSupported: %d %s\n", extra[27],
287 extra[27] ? "" : "(will use whatever is returned)");
288 {
289 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
290 int n;
291
292 /* See CCID 3.7.3 page 25 */
293 n = usb_control_msg(handle,
294 0xA1, /* request type */
295 0x03, /* GET DATA RATES */
296 0x00, /* value */
297 usb_interface->bInterfaceNumber, /* interface */
298 (char *)buffer,
299 sizeof(buffer),
300 2 * 1000);
301
302 /* we got an error? */
303 if (n <= 0)
304 printf(" IFD does not support GET_DATA_RATES request\n");
305 else
306 if (n % 4) /* not a multiple of 4 */
307 printf(" wrong size for GET_DATA_RATES: %d\n", n);
308 else
309 {
310 int i;
311
312 /* we do not get the expected number of data rates */
313 if ((n != extra[27]*4) && extra[27])
314 {
315 printf(" Got %d data rates but was expecting %d\n", n/4,
316 extra[27]);
317
318 /* we got more data than expected */
319 if (n > extra[27]*4)
320 n = extra[27]*4;
321 }
322
323 for (i=0; i<n; i+=4)
324 printf(" Support %d bps\n", dw2i(buffer, i));
325 }
326 }
327 printf(" dwMaxIFSD: %d\n", dw2i(extra, 28));
328 printf(" dwSynchProtocols: 0x%08X\n", dw2i(extra, 32));
329 if (extra[32] & 0x01)
330 printf(" 2-wire protocol\n");
331 if (extra[32] & 0x02)
332 printf(" 3-wire protocol\n");
333 if (extra[32] & 0x04)
334 printf(" I2C protocol\n");
335
336 printf(" dwMechanical: 0x%08X\n", dw2i(extra, 36));
337 if (extra[36] == 0)
338 printf(" No special characteristics\n");
339 if (extra[36] & 0x01)
340 printf(" Card accept mechanism\n");
341 if (extra[36] & 0x02)
342 printf(" Card ejection mechanism\n");
343 if (extra[36] & 0x04)
344 printf(" Card capture mechanism\n");
345 if (extra[36] & 0x08)
346 printf(" Card lock/unlock mechanism\n");
347
348 printf(" dwFeatures: 0x%08X\n", dw2i(extra, 40));
349 if (dw2i(extra, 40) == 0)
350 printf(" No special characteristics\n");
351 if (extra[40] & 0x02)
352 printf(" ....02 Automatic parameter configuration based on ATR data\n");
353 if (extra[40] & 0x04)
354 printf(" ....04 Automatic activation of ICC on inserting\n");
355 if (extra[40] & 0x08)
356 printf(" ....08 Automatic ICC voltage selection\n");
357 if (extra[40] & 0x10)
358 printf(" ....10 Automatic ICC clock frequency change according to parameters\n");
359 if (extra[40] & 0x20)
360 printf(" ....20 Automatic baud rate change according to frequency and Fi, Di params\n");
361 if (extra[40] & 0x40)
362 printf(" ....40 Automatic parameters negotiation made by the CCID\n");
363 if (extra[40] & 0x80)
364 printf(" ....80 Automatic PPS made by the CCID\n");
365 if (extra[41] & 0x01)
366 printf(" ..01.. CCID can set ICC in clock stop mode\n");
367 if (extra[41] & 0x02)
368 printf(" ..02.. NAD value other than 00 accepted (T=1)\n");
369 if (extra[41] & 0x04)
370 printf(" ..04.. Automatic IFSD exchange as first exchange (T=1)\n");
371 switch (extra[42] & 0x07)
372 {
373 case 0x00:
374 printf(" 00.... Character level exchange\n");
375 break;
376
377 case 0x01:
378 printf(" 01.... TPDU level exchange\n");
379 break;
380
381 case 0x02:
382 printf(" 02.... Short APDU level exchange\n");
383 break;
384
385 case 0x04:
386 printf(" 04.... Short and Extended APDU level exchange\n");
387 break;
388 }
389
390 printf(" dwMaxCCIDMessageLength: %d bytes\n", dw2i(extra, 44));
391 printf(" bClassGetResponse: 0x%02X\n", extra[48]);
392 if (0xFF == extra[48])
393 printf(" echoes the APDU class\n");
394 printf(" bClassEnveloppe: 0x%02X\n", extra[49]);
395 if (0xFF == extra[49])
396 printf(" echoes the APDU class\n");
397 printf(" wLcdLayout: 0x%04X\n", (extra[51] << 8)+extra[50]);
398 if (extra[50])
399 printf(" %d lines\n", extra[50]);
400 if (extra[51])
401 printf(" %d characters per line\n", extra[51]);
402 printf(" bPINSupport: 0x%02X\n", extra[52]);
403 if (extra[52] & 0x01)
404 printf(" PIN Verification supported\n");
405 if (extra[52] & 0x02)
406 printf(" PIN Modification supported\n");
407 printf(" bMaxCCIDBusySlots: %d\n", extra[53]);
408
409 return FALSE;
410 } /* ccid_parse_interface_descriptor */
411

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5