/[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 2389 - (show annotations) (download)
Thu Feb 8 14:13:47 2007 UTC (6 years, 3 months ago) by rousseau
File MIME type: text/plain
File size: 11654 byte(s)
parse bNumEndpoints field
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 switch (usb_interface->bNumEndpoints)
170 {
171 case 0:
172 printf(" Control only\n");
173 break;
174 case 1:
175 printf(" Interrupt-IN\n");
176 break;
177 case 2:
178 printf(" bulk-IN and bulk-OUT\n");
179 break;
180 case 3:
181 printf(" bulk-IN, bulk-OUT and Interrupt-IN\n");
182 break;
183 default:
184 printf(" UNKNOWN value\n");
185 }
186
187 printf(" bInterfaceClass: 0x%02X", usb_interface->bInterfaceClass);
188 if (usb_interface->bInterfaceClass == 0x0b)
189 printf(" [Chip Card Interface Device Class (CCID)]\n");
190 else
191 {
192 printf("\n NOT A CCID DEVICE\n");
193 if (usb_interface->bInterfaceClass != 0xFF)
194 return TRUE;
195 else
196 printf(" Class is 0xFF (proprietary)\n");
197 }
198
199 printf(" bInterfaceSubClass: %d\n", usb_interface->bInterfaceSubClass);
200 if (usb_interface->bInterfaceSubClass)
201 printf(" UNSUPPORTED SubClass\n");
202
203 printf(" bInterfaceProtocol: %d\n", usb_interface->bInterfaceProtocol);
204 if (usb_interface->bInterfaceProtocol)
205 printf(" UNSUPPORTED InterfaceProtocol\n");
206
207 printf(" iInterface: %d\n", usb_interface->iInterface);
208
209 if (usb_interface->extralen < 54)
210 {
211 printf("USB extra length is too short: %d\n", usb_interface->extralen);
212 printf("\n NOT A CCID DEVICE\n");
213 return TRUE;
214 }
215
216 /*
217 * CCID Class Descriptor
218 */
219 printf(" CCID Class Descriptor\n");
220 extra = usb_interface->extra;
221
222 printf(" bLength: 0x%02X\n", extra[0]);
223 if (extra[0] != 0x36)
224 {
225 printf(" UNSUPPORTED bLength\n");
226 return TRUE;
227 }
228
229 printf(" bDescriptorType: 0x%02X\n", extra[1]);
230 if (extra[1] != 0x21)
231 {
232 if (0xFF == extra[1])
233 printf(" PROPRIETARY bDescriptorType\n");
234 else
235 {
236 printf(" UNSUPPORTED bDescriptorType\n");
237 return TRUE;
238 }
239 }
240
241 printf(" bcdCCID: %X.%02X\n", extra[3], extra[2]);
242 printf(" bMaxSlotIndex: 0x%02X\n", extra[4]);
243 printf(" bVoltageSupport: 0x%02X\n", extra[5]);
244 if (extra[5] & 0x01)
245 printf(" 5.0V\n");
246 if (extra[5] & 0x02)
247 printf(" 3.0V\n");
248 if (extra[5] & 0x04)
249 printf(" 1.8V\n");
250
251 printf(" dwProtocols: 0x%02X%02X 0x%02X%02X\n", extra[9], extra[8],
252 extra[7],extra[6]);
253 if (extra[6] & 0x01)
254 printf(" T=0\n");
255 if (extra[6] & 0x02)
256 printf(" T=1\n");
257
258 printf(" dwDefaultClock: %.3f MHz\n", dw2i(extra, 10)/1000.0);
259 printf(" dwMaximumClock: %.3f MHz\n", dw2i(extra, 14)/1000.0);
260 printf(" bNumClockSupported: %d %s\n", extra[18],
261 extra[18] ? "" : "(will use whatever is returned)");
262 {
263 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
264 int n;
265
266 /* See CCID 3.7.2 page 25 */
267 n = usb_control_msg(handle,
268 0xA1, /* request type */
269 0x02, /* GET CLOCK FREQUENCIES */
270 0x00, /* value */
271 usb_interface->bInterfaceNumber, /* interface */
272 (char *)buffer,
273 sizeof(buffer),
274 2 * 1000);
275
276 /* we got an error? */
277 if (n <= 0)
278 printf(" IFD does not support GET CLOCK FREQUENCIES request\n");
279 else
280 if (n % 4) /* not a multiple of 4 */
281 printf(" wrong size for GET CLOCK FREQUENCIES: %d\n", n);
282 else
283 {
284 int i;
285
286 /* we do not get the expected number of data rates */
287 if ((n != extra[18]*4) && extra[18])
288 {
289 printf(" Got %d clock frequencies but was expecting %d\n",
290 n/4, extra[18]);
291
292 /* we got more data than expected */
293 if (n > extra[18]*4)
294 n = extra[18]*4;
295 }
296
297 for (i=0; i<n; i+=4)
298 printf(" Support %d kHz\n", dw2i(buffer, i));
299 }
300 }
301 printf(" dwDataRate: %d bps\n", dw2i(extra, 19));
302 printf(" dwMaxDataRate: %d bps\n", dw2i(extra, 23));
303 printf(" bNumDataRatesSupported: %d %s\n", extra[27],
304 extra[27] ? "" : "(will use whatever is returned)");
305 {
306 unsigned char buffer[256*sizeof(int)]; /* maximum is 256 records */
307 int n;
308
309 /* See CCID 3.7.3 page 25 */
310 n = usb_control_msg(handle,
311 0xA1, /* request type */
312 0x03, /* GET DATA RATES */
313 0x00, /* value */
314 usb_interface->bInterfaceNumber, /* interface */
315 (char *)buffer,
316 sizeof(buffer),
317 2 * 1000);
318
319 /* we got an error? */
320 if (n <= 0)
321 printf(" IFD does not support GET_DATA_RATES request\n");
322 else
323 if (n % 4) /* not a multiple of 4 */
324 printf(" wrong size for GET_DATA_RATES: %d\n", n);
325 else
326 {
327 int i;
328
329 /* we do not get the expected number of data rates */
330 if ((n != extra[27]*4) && extra[27])
331 {
332 printf(" Got %d data rates but was expecting %d\n", n/4,
333 extra[27]);
334
335 /* we got more data than expected */
336 if (n > extra[27]*4)
337 n = extra[27]*4;
338 }
339
340 for (i=0; i<n; i+=4)
341 printf(" Support %d bps\n", dw2i(buffer, i));
342 }
343 }
344 printf(" dwMaxIFSD: %d\n", dw2i(extra, 28));
345 printf(" dwSynchProtocols: 0x%08X\n", dw2i(extra, 32));
346 if (extra[32] & 0x01)
347 printf(" 2-wire protocol\n");
348 if (extra[32] & 0x02)
349 printf(" 3-wire protocol\n");
350 if (extra[32] & 0x04)
351 printf(" I2C protocol\n");
352
353 printf(" dwMechanical: 0x%08X\n", dw2i(extra, 36));
354 if (extra[36] == 0)
355 printf(" No special characteristics\n");
356 if (extra[36] & 0x01)
357 printf(" Card accept mechanism\n");
358 if (extra[36] & 0x02)
359 printf(" Card ejection mechanism\n");
360 if (extra[36] & 0x04)
361 printf(" Card capture mechanism\n");
362 if (extra[36] & 0x08)
363 printf(" Card lock/unlock mechanism\n");
364
365 printf(" dwFeatures: 0x%08X\n", dw2i(extra, 40));
366 if (dw2i(extra, 40) == 0)
367 printf(" No special characteristics\n");
368 if (extra[40] & 0x02)
369 printf(" ....02 Automatic parameter configuration based on ATR data\n");
370 if (extra[40] & 0x04)
371 printf(" ....04 Automatic activation of ICC on inserting\n");
372 if (extra[40] & 0x08)
373 printf(" ....08 Automatic ICC voltage selection\n");
374 if (extra[40] & 0x10)
375 printf(" ....10 Automatic ICC clock frequency change according to parameters\n");
376 if (extra[40] & 0x20)
377 printf(" ....20 Automatic baud rate change according to frequency and Fi, Di params\n");
378 if (extra[40] & 0x40)
379 printf(" ....40 Automatic parameters negotiation made by the CCID\n");
380 if (extra[40] & 0x80)
381 printf(" ....80 Automatic PPS made by the CCID\n");
382 if (extra[41] & 0x01)
383 printf(" ..01.. CCID can set ICC in clock stop mode\n");
384 if (extra[41] & 0x02)
385 printf(" ..02.. NAD value other than 00 accepted (T=1)\n");
386 if (extra[41] & 0x04)
387 printf(" ..04.. Automatic IFSD exchange as first exchange (T=1)\n");
388 switch (extra[42] & 0x07)
389 {
390 case 0x00:
391 printf(" 00.... Character level exchange\n");
392 break;
393
394 case 0x01:
395 printf(" 01.... TPDU level exchange\n");
396 break;
397
398 case 0x02:
399 printf(" 02.... Short APDU level exchange\n");
400 break;
401
402 case 0x04:
403 printf(" 04.... Short and Extended APDU level exchange\n");
404 break;
405 }
406
407 printf(" dwMaxCCIDMessageLength: %d bytes\n", dw2i(extra, 44));
408 printf(" bClassGetResponse: 0x%02X\n", extra[48]);
409 if (0xFF == extra[48])
410 printf(" echoes the APDU class\n");
411 printf(" bClassEnveloppe: 0x%02X\n", extra[49]);
412 if (0xFF == extra[49])
413 printf(" echoes the APDU class\n");
414 printf(" wLcdLayout: 0x%04X\n", (extra[51] << 8)+extra[50]);
415 if (extra[50])
416 printf(" %d lines\n", extra[50]);
417 if (extra[51])
418 printf(" %d characters per line\n", extra[51]);
419 printf(" bPINSupport: 0x%02X\n", extra[52]);
420 if (extra[52] & 0x01)
421 printf(" PIN Verification supported\n");
422 if (extra[52] & 0x02)
423 printf(" PIN Modification supported\n");
424 printf(" bMaxCCIDBusySlots: %d\n", extra[53]);
425
426 return FALSE;
427 } /* ccid_parse_interface_descriptor */
428

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5