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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1477 - (hide annotations) (download)
Wed Apr 27 14:08:35 2005 UTC (8 years ago) by rousseau
File MIME type: text/plain
File size: 32225 byte(s)
IFDHSetProtocolParameters(): we do not check if (card_baudrate <=
ccid_desc->dwMaxDataRate) since find_baud_rate() will tell us if the
speed is supported or not by the reader
1 rousseau 269 /*
2     ifdhandler.c: IFDH API
3 rousseau 1408 Copyright (C) 2003-2005 Ludovic Rousseau
4 rousseau 269
5 rousseau 1399 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 rousseau 269
10 rousseau 1399 This library is distributed in the hope that it will be useful,
11 rousseau 269 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 rousseau 1399 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14 rousseau 269
15 rousseau 1399 You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 rousseau 269 */
19    
20 rousseau 773 /* $Id$ */
21 rousseau 770
22 rousseau 269 #include <stdio.h>
23     #include <string.h>
24 rousseau 773 #include <stdlib.h>
25 rousseau 998 #include <PCSC/pcsclite.h>
26     #include <PCSC/ifdhandler.h>
27 rousseau 269
28 rousseau 880 #include "ccid.h"
29 rousseau 611 #include "defs.h"
30 rousseau 880 #include "ccid_ifdhandler.h"
31 rousseau 269 #include "config.h"
32     #include "debug.h"
33     #include "utils.h"
34     #include "commands.h"
35 rousseau 998 #include "towitoko/atr.h"
36     #include "towitoko/pps.h"
37 rousseau 773 #include "parser.h"
38 rousseau 269
39 rousseau 563 #ifdef HAVE_PTHREAD
40 rousseau 463 #include <pthread.h>
41     #endif
42    
43 rousseau 410 /* Array of structures to hold the ATR and other state value of each slot */
44 rousseau 1077 static CcidDesc CcidSlots[CCID_DRIVER_MAX_READERS];
45 rousseau 269
46 rousseau 463 /* global mutex */
47 rousseau 563 #ifdef HAVE_PTHREAD
48 rousseau 463 static pthread_mutex_t ifdh_context_mutex = PTHREAD_MUTEX_INITIALIZER;
49     #endif
50 rousseau 269
51 rousseau 880 int LogLevel = DEBUG_LEVEL_CRITICAL | DEBUG_LEVEL_INFO;
52     int DriverOptions = 0;
53 rousseau 773 static int DebugInitialized = FALSE;
54 rousseau 463
55 rousseau 880 /* local functions */
56     static void init_driver(void);
57 rousseau 1438 static void extra_egt(ATR_t *atr, _ccid_descriptor *ccid_desc, DWORD Protocol);
58 rousseau 1448 static char find_baud_rate(unsigned int baudrate, unsigned int *list);
59 rousseau 1451 static unsigned int T0_card_timeout(double f, int TC2, int clock_frequency);
60     static unsigned int T1_card_timeout(double f, double d, int BWI,
61     int clock_frequency);
62 rousseau 773
63    
64 rousseau 998 RESPONSECODE IFDHCreateChannelByName(DWORD Lun, LPTSTR lpcDevice)
65 rousseau 649 {
66 rousseau 770 RESPONSECODE return_value = IFD_SUCCESS;
67 rousseau 1107 int reader_index;
68 rousseau 770
69 rousseau 773 if (! DebugInitialized)
70 rousseau 880 init_driver();
71 rousseau 773
72 rousseau 649 DEBUG_INFO3("lun: %X, device: %s", Lun, lpcDevice);
73    
74 rousseau 1107 if (-1 == (reader_index = GetNewReaderIndex(Lun)))
75 rousseau 649 return IFD_COMMUNICATION_ERROR;
76    
77 rousseau 892 /* Reset ATR buffer */
78 rousseau 1107 CcidSlots[reader_index].nATRLength = 0;
79     *CcidSlots[reader_index].pcATRBuffer = '\0';
80 rousseau 649
81 rousseau 892 /* Reset PowerFlags */
82 rousseau 1107 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ;
83 rousseau 649
84     #ifdef HAVE_PTHREAD
85     pthread_mutex_lock(&ifdh_context_mutex);
86     #endif
87    
88 rousseau 1107 if (OpenPortByName(reader_index, lpcDevice) != STATUS_SUCCESS)
89 rousseau 649 {
90 rousseau 663 DEBUG_CRITICAL("failed");
91 rousseau 770 return_value = IFD_COMMUNICATION_ERROR;
92 rousseau 1152
93     /* release the allocated reader_index */
94     ReleaseReaderIndex(reader_index);
95 rousseau 649 }
96 rousseau 1152 else
97     /* Maybe we have a special treatment for this reader */
98     ccid_open_hack(reader_index);
99 rousseau 649
100     #ifdef HAVE_PTHREAD
101     pthread_mutex_unlock(&ifdh_context_mutex);
102     #endif
103    
104 rousseau 770 return return_value;
105 rousseau 649 } /* IFDHCreateChannelByName */
106    
107    
108 rousseau 269 RESPONSECODE IFDHCreateChannel(DWORD Lun, DWORD Channel)
109     {
110     /*
111     * Lun - Logical Unit Number, use this for multiple card slots or
112     * multiple readers. 0xXXXXYYYY - XXXX multiple readers, YYYY multiple
113     * slots. The resource manager will set these automatically. By
114     * default the resource manager loads a new instance of the driver so
115     * if your reader does not have more than one smartcard slot then
116     * ignore the Lun in all the functions. Future versions of PC/SC might
117     * support loading multiple readers through one instance of the driver
118     * in which XXXX would be important to implement if you want this.
119     */
120    
121     /*
122     * Channel - Channel ID. This is denoted by the following: 0x000001 -
123     * /dev/pcsc/1 0x000002 - /dev/pcsc/2 0x000003 - /dev/pcsc/3
124     *
125     * USB readers may choose to ignore this parameter and query the bus
126     * for the particular reader.
127     */
128    
129     /*
130     * This function is required to open a communications channel to the
131     * port listed by Channel. For example, the first serial reader on
132     * COM1 would link to /dev/pcsc/1 which would be a sym link to
133     * /dev/ttyS0 on some machines This is used to help with intermachine
134     * independance.
135     *
136     * Once the channel is opened the reader must be in a state in which
137     * it is possible to query IFDHICCPresence() for card status.
138     *
139     * returns:
140     *
141     * IFD_SUCCESS IFD_COMMUNICATION_ERROR
142     */
143     RESPONSECODE return_value = IFD_SUCCESS;
144 rousseau 1107 int reader_index;
145 rousseau 269
146 rousseau 773 if (! DebugInitialized)
147 rousseau 880 init_driver();
148 rousseau 773
149 rousseau 608 DEBUG_INFO2("lun: %X", Lun);
150 rousseau 269
151 rousseau 1107 if (-1 == (reader_index = GetNewReaderIndex(Lun)))
152 rousseau 269 return IFD_COMMUNICATION_ERROR;
153    
154 rousseau 410 /* Reset ATR buffer */
155 rousseau 1107 CcidSlots[reader_index].nATRLength = 0;
156     *CcidSlots[reader_index].pcATRBuffer = '\0';
157 rousseau 269
158 rousseau 410 /* Reset PowerFlags */
159 rousseau 1107 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ;
160 rousseau 269
161 rousseau 563 #ifdef HAVE_PTHREAD
162 rousseau 463 pthread_mutex_lock(&ifdh_context_mutex);
163     #endif
164    
165 rousseau 1107 if (OpenPort(reader_index, Channel) != STATUS_SUCCESS)
166 rousseau 269 {
167 rousseau 663 DEBUG_CRITICAL("failed");
168 rousseau 269 return_value = IFD_COMMUNICATION_ERROR;
169 rousseau 1152
170     /* release the allocated reader_index */
171     ReleaseReaderIndex(reader_index);
172 rousseau 269 }
173 rousseau 1152 else
174     /* Maybe we have a special treatment for this reader */
175     ccid_open_hack(reader_index);
176 rousseau 269
177 rousseau 563 #ifdef HAVE_PTHREAD
178 rousseau 463 pthread_mutex_unlock(&ifdh_context_mutex);
179     #endif
180    
181 rousseau 269 return return_value;
182     } /* IFDHCreateChannel */
183    
184    
185     RESPONSECODE IFDHCloseChannel(DWORD Lun)
186     {
187     /*
188     * This function should close the reader communication channel for the
189     * particular reader. Prior to closing the communication channel the
190     * reader should make sure the card is powered down and the terminal
191     * is also powered down.
192     *
193     * returns:
194     *
195     * IFD_SUCCESS IFD_COMMUNICATION_ERROR
196     */
197 rousseau 1107 int reader_index;
198 rousseau 269
199 rousseau 608 DEBUG_INFO2("lun: %X", Lun);
200 rousseau 269
201 rousseau 1107 if (-1 == (reader_index = LunToReaderIndex(Lun)))
202 rousseau 269 return IFD_COMMUNICATION_ERROR;
203    
204 rousseau 1452 /* Restore the default timeout
205     * No need to wait too long if the reader disapeared */
206     get_ccid_descriptor(reader_index)->readTimeout = DEFAULT_COM_READ_TIMEOUT;
207    
208 rousseau 1107 (void)CmdPowerOff(reader_index);
209 rousseau 410 /* No reader status check, if it failed, what can you do ? :) */
210 rousseau 269
211 rousseau 563 #ifdef HAVE_PTHREAD
212 rousseau 463 pthread_mutex_lock(&ifdh_context_mutex);
213     #endif
214    
215 rousseau 1107 (void)ClosePort(reader_index);
216     ReleaseReaderIndex(reader_index);
217 rousseau 269
218 rousseau 563 #ifdef HAVE_PTHREAD
219 rousseau 463 pthread_mutex_unlock(&ifdh_context_mutex);
220     #endif
221    
222 rousseau 269 return IFD_SUCCESS;
223     } /* IFDHCloseChannel */
224    
225    
226     RESPONSECODE IFDHGetCapabilities(DWORD Lun, DWORD Tag,
227     PDWORD Length, PUCHAR Value)
228     {
229     /*
230     * This function should get the slot/card capabilities for a
231     * particular slot/card specified by Lun. Again, if you have only 1
232     * card slot and don't mind loading a new driver for each reader then
233     * ignore Lun.
234     *
235     * Tag - the tag for the information requested example: TAG_IFD_ATR -
236     * return the Atr and it's size (required). these tags are defined in
237     * ifdhandler.h
238     *
239     * Length - the length of the returned data Value - the value of the
240     * data
241     *
242     * returns:
243     *
244     * IFD_SUCCESS IFD_ERROR_TAG
245     */
246 rousseau 1107 int reader_index;
247 rousseau 269
248 rousseau 804 DEBUG_INFO3("lun: %X, tag: 0x%X", Lun, Tag);
249 rousseau 269
250 rousseau 1107 if (-1 == (reader_index = LunToReaderIndex(Lun)))
251 rousseau 269 return IFD_COMMUNICATION_ERROR;
252    
253     switch (Tag)
254     {
255     case TAG_IFD_ATR:
256 rousseau 804 case SCARD_ATTR_ATR_STRING:
257 rousseau 410 /* If Length is not zero, powerICC has been performed.
258     * Otherwise, return NULL pointer
259     * Buffer size is stored in *Length */
260 rousseau 1107 *Length = (*Length < CcidSlots[reader_index].nATRLength) ?
261     *Length : CcidSlots[reader_index].nATRLength;
262 rousseau 269
263     if (*Length)
264 rousseau 1437 memcpy(Value, CcidSlots[reader_index].pcATRBuffer, *Length);
265 rousseau 269 break;
266    
267 rousseau 563 #ifdef HAVE_PTHREAD
268 rousseau 269 case TAG_IFD_SIMULTANEOUS_ACCESS:
269     if (*Length >= 1)
270     {
271     *Length = 1;
272 rousseau 1077 *Value = CCID_DRIVER_MAX_READERS;
273 rousseau 269 }
274     break;
275    
276 rousseau 463 case TAG_IFD_THREAD_SAFE:
277     if (*Length >= 1)
278     {
279     *Length = 1;
280     *Value = 1; /* Can talk to multiple readers at the same time */
281     }
282     break;
283 rousseau 563 #endif
284 rousseau 463
285 rousseau 269 case TAG_IFD_SLOTS_NUMBER:
286     if (*Length >= 1)
287     {
288     *Length = 1;
289 rousseau 1107 *Value = 1 + get_ccid_descriptor(reader_index) -> bMaxSlotIndex;
290     DEBUG_COMM2("Reader supports %d slots", *Value);
291 rousseau 269 }
292     break;
293    
294 rousseau 1107 case TAG_IFD_SLOT_THREAD_SAFE:
295     if (*Length >= 1)
296     {
297     *Length = 1;
298 rousseau 1153 *Value = 0; /* Can NOT talk to multiple slots at the same time */
299 rousseau 1107 }
300     break;
301    
302 rousseau 900 case IOCTL_SMARTCARD_VENDOR_VERIFY_PIN:
303     if (*Length >= 1)
304     {
305     *Length = 1;
306 rousseau 1107 *Value = get_ccid_descriptor(reader_index) -> bPINSupport & CCID_CLASS_PIN_VERIFY;
307 rousseau 900 }
308     break;
309    
310 rousseau 269 default:
311     return IFD_ERROR_TAG;
312     }
313 rousseau 569
314 rousseau 269 return IFD_SUCCESS;
315     } /* IFDHGetCapabilities */
316    
317    
318     RESPONSECODE IFDHSetCapabilities(DWORD Lun, DWORD Tag,
319 rousseau 1053 /*@unused@*/ DWORD Length, /*@unused@*/ PUCHAR Value)
320 rousseau 269 {
321     /*
322     * This function should set the slot/card capabilities for a
323     * particular slot/card specified by Lun. Again, if you have only 1
324     * card slot and don't mind loading a new driver for each reader then
325     * ignore Lun.
326     *
327     * Tag - the tag for the information needing set
328     *
329     * Length - the length of the returned data Value - the value of the
330     * data
331     *
332     * returns:
333     *
334     * IFD_SUCCESS IFD_ERROR_TAG IFD_ERROR_SET_FAILURE
335     * IFD_ERROR_VALUE_READ_ONLY
336     */
337    
338 rousseau 410 /* By default, say it worked */
339 rousseau 269
340 rousseau 804 DEBUG_INFO3("lun: %X, tag: 0x%X", Lun, Tag);
341 rousseau 269
342     /* if (CheckLun(Lun))
343     return IFD_COMMUNICATION_ERROR; */
344    
345 rousseau 569 return IFD_NOT_SUPPORTED;
346 rousseau 269 } /* IFDHSetCapabilities */
347    
348    
349     RESPONSECODE IFDHSetProtocolParameters(DWORD Lun, DWORD Protocol,
350     UCHAR Flags, UCHAR PTS1, UCHAR PTS2, UCHAR PTS3)
351     {
352     /*
353     * This function should set the PTS of a particular card/slot using
354     * the three PTS parameters sent
355     *
356 rousseau 998 * Protocol - 0 .... 14 T=0 .... T=14
357     * Flags - Logical OR of possible values:
358     * IFD_NEGOTIATE_PTS1
359     * IFD_NEGOTIATE_PTS2
360     * IFD_NEGOTIATE_PTS3
361     * to determine which PTS values to negotiate.
362     * PTS1,PTS2,PTS3 - PTS Values.
363 rousseau 269 *
364     * returns:
365 rousseau 998 * IFD_SUCCESS
366     * IFD_ERROR_PTS_FAILURE
367     * IFD_COMMUNICATION_ERROR
368     * IFD_PROTOCOL_NOT_SUPPORTED
369 rousseau 269 */
370    
371 rousseau 998 BYTE pps[PPS_MAX_LENGTH];
372 rousseau 1368 ATR_t atr;
373 rousseau 998 unsigned int len;
374     int convention;
375 rousseau 1107 int reader_index;
376 rousseau 269
377 rousseau 998 /* Set ccid desc params */
378     CcidDesc *ccid_slot;
379     _ccid_descriptor *ccid_desc;
380 rousseau 269
381 rousseau 998 DEBUG_INFO3("lun: %X, protocol T=%d", Lun, Protocol-1);
382    
383 rousseau 1107 if (-1 == (reader_index = LunToReaderIndex(Lun)))
384 rousseau 998 return IFD_COMMUNICATION_ERROR;
385    
386     /* Set to zero buffer */
387     memset(pps, 0, sizeof(pps));
388     memset(&atr, 0, sizeof(atr));
389    
390     /* Get ccid params */
391 rousseau 1107 ccid_slot = get_ccid_slot(reader_index);
392     ccid_desc = get_ccid_descriptor(reader_index);
393 rousseau 998
394 rousseau 1354 /* Do not send CCID command SetParameters or PPS to the CCID
395     * The CCID will do this himself */
396     if (ccid_desc->dwFeatures & CCID_CLASS_AUTO_PPS_PROP)
397     return IFD_SUCCESS;
398    
399 rousseau 998 /* Get ATR of the card */
400     ATR_InitFromArray(&atr, ccid_slot->pcATRBuffer, ccid_slot->nATRLength);
401    
402 rousseau 1438 /* Apply Extra EGT patch for bogus cards */
403     extra_egt(&atr, ccid_desc, Protocol);
404    
405 rousseau 998 if (SCARD_PROTOCOL_T0 == Protocol)
406     pps[1] |= ATR_PROTOCOL_TYPE_T0;
407     else
408     if (SCARD_PROTOCOL_T1 == Protocol)
409     pps[1] |= ATR_PROTOCOL_TYPE_T1;
410     else
411     return IFD_PROTOCOL_NOT_SUPPORTED;
412    
413     /* TA2 present -> specific mode */
414     if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present)
415     {
416     if (pps[1] != (atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F))
417     {
418     /* wrong protocol */
419     DEBUG_COMM3("Specific mode in T=%d and T=%d requested",
420     atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x0F, pps[1]);
421    
422     return IFD_PROTOCOL_NOT_SUPPORTED;
423     }
424     }
425    
426     /* TCi (i>2) indicates CRC instead of LRC */
427     if (SCARD_PROTOCOL_T1 == Protocol)
428     {
429     t1_state_t *t1 = &(ccid_slot -> t1);
430     int i;
431    
432     /* TCi (i>2) present? */
433     for (i=2; i<ATR_MAX_PROTOCOLS; i++)
434     if (atr.ib[i][ATR_INTERFACE_BYTE_TC].present)
435     {
436     if (0 == atr.ib[i][ATR_INTERFACE_BYTE_TC].value)
437     {
438     DEBUG_COMM("Use LRC");
439     t1_set_param(t1, IFD_PROTOCOL_T1_CHECKSUM_LRC, 0);
440     }
441     else
442     if (1 == atr.ib[i][ATR_INTERFACE_BYTE_TC].value)
443     {
444     DEBUG_COMM("Use CRC");
445     t1_set_param(t1, IFD_PROTOCOL_T1_CHECKSUM_CRC, 0);
446     }
447     else
448     DEBUG_COMM2("Wrong value for TCi: %d",
449     atr.ib[i][ATR_INTERFACE_BYTE_TC].value);
450    
451     /* only the first TCi (i>2) must be used */
452     break;
453     }
454     }
455    
456     /* PTS1? */
457     if (Flags & IFD_NEGOTIATE_PTS1)
458     {
459     /* just use the value passed in argument */
460     pps[1] |= 0x10; /* PTS1 presence */
461     pps[2] = PTS1;
462     }
463     else
464     {
465 rousseau 1352 /* TA1 present */
466     if (atr.ib[0][ATR_INTERFACE_BYTE_TA].present)
467 rousseau 998 {
468     unsigned int card_baudrate;
469     unsigned int default_baudrate;
470     double f, d;
471    
472     ATR_GetParameter(&atr, ATR_PARAMETER_D, &d);
473     ATR_GetParameter(&atr, ATR_PARAMETER_F, &f);
474    
475     /* Baudrate = f x D/F */
476     card_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock
477     * d / f);
478    
479     default_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock
480     * ATR_DEFAULT_D / ATR_DEFAULT_F);
481    
482 rousseau 1477 /* if the card does not try to lower the default speed */
483     if (card_baudrate > default_baudrate)
484 rousseau 998 {
485 rousseau 1448 if (find_baud_rate(card_baudrate,
486     ccid_desc->arrayOfSupportedDataRates))
487     {
488     pps[1] |= 0x10; /* PTS1 presence */
489     pps[2] = atr.ib[0][ATR_INTERFACE_BYTE_TA].value;
490 rousseau 998
491 rousseau 1448 DEBUG_COMM2("Set speed to %d bauds", card_baudrate);
492     }
493     else
494     DEBUG_COMM2("Reader does not support %d bauds",
495     card_baudrate);
496 rousseau 998 }
497     }
498     }
499    
500     /* PTS2? */
501     if (Flags & IFD_NEGOTIATE_PTS2)
502     {
503     pps[1] |= 0x20; /* PTS2 presence */
504     pps[3] = PTS2;
505     }
506    
507     /* PTS3? */
508     if (Flags & IFD_NEGOTIATE_PTS3)
509     {
510     pps[1] |= 0x40; /* PTS3 presence */
511     pps[4] = PTS3;
512     }
513    
514     /* Generate PPS */
515     pps[0] = 0xFF;
516    
517     /* Automatic PPS made by the ICC? */
518     if ((! (ccid_desc->dwFeatures & CCID_CLASS_AUTO_PPS_CUR))
519     /* TA2 absent: negociable mode */
520     && (! atr.ib[1][ATR_INTERFACE_BYTE_TA].present))
521     {
522     int default_protocol;
523    
524     if (ATR_MALFORMED == ATR_GetDefaultProtocol(&atr, &default_protocol))
525     return IFD_PROTOCOL_NOT_SUPPORTED;
526    
527     /* if the requested protocol is not the default one
528     * or a TA1/PPS1 is present */
529     if (((pps[1] & 0x0F) != default_protocol) || (PPS_HAS_PPS1(pps)))
530 rousseau 1107 if (PPS_Exchange(reader_index, pps, &len, &pps[2]) != PPS_OK)
531 rousseau 998 {
532     DEBUG_INFO("PPS_Exchange Failed");
533    
534     return IFD_ERROR_PTS_FAILURE;
535     }
536     }
537    
538     /* Now we must set the reader parameters */
539     ATR_GetConvention(&atr, &convention);
540    
541     /* specific mode and implicit parameters? (b5 of TA2) */
542     if (atr.ib[1][ATR_INTERFACE_BYTE_TA].present
543     && (atr.ib[1][ATR_INTERFACE_BYTE_TA].value & 0x10))
544     return IFD_COMMUNICATION_ERROR;
545    
546     /* T=1 */
547     if (SCARD_PROTOCOL_T1 == Protocol)
548     {
549     BYTE param[] = {
550     0x11, /* Fi/Di */
551     0x10, /* TCCKS */
552     0x00, /* GuardTime */
553 rousseau 1439 0x4D, /* BWI/CWI */
554 rousseau 998 0x00, /* ClockStop */
555     0x20, /* IFSC */
556     0x00 /* NADValue */
557     };
558     int i;
559 rousseau 1195 t1_state_t *t1 = &(ccid_slot -> t1);
560 rousseau 1213 RESPONSECODE ret;
561 rousseau 1452 double f;
562     double d;
563     int BWI;
564 rousseau 998
565     /* TA1 is not default */
566     if (PPS_HAS_PPS1(pps))
567     param[0] = pps[2];
568 rousseau 1437
569 rousseau 1195 /* CRC checksum? */
570     if (2 == t1->rc_bytes)
571 rousseau 1196 param[1] |= 0x01;
572 rousseau 998
573 rousseau 1195 /* the CCID should ignore this bit */
574 rousseau 998 if (ATR_CONVENTION_INVERSE == convention)
575 rousseau 1196 param[1] |= 0x02;
576 rousseau 998
577     /* get TC1 Extra guard time */
578     if (atr.ib[0][ATR_INTERFACE_BYTE_TC].present)
579     param[2] = atr.ib[0][ATR_INTERFACE_BYTE_TC].value;
580    
581 rousseau 1439 /* TBi (i>2) present? BWI/CWI */
582 rousseau 998 for (i=2; i<ATR_MAX_PROTOCOLS; i++)
583     if (atr.ib[i][ATR_INTERFACE_BYTE_TB].present)
584     {
585 rousseau 1439 DEBUG_COMM3("BWI/CWI (TB%d) present: 0x%02X", i+1,
586 rousseau 998 atr.ib[i][ATR_INTERFACE_BYTE_TB].value);
587     param[3] = atr.ib[i][ATR_INTERFACE_BYTE_TB].value;
588    
589     /* only the first TBi (i>2) must be used */
590     break;
591     }
592    
593 rousseau 1452 /* compute communication timeout */
594     ATR_GetParameter(&atr, ATR_PARAMETER_F, &f);
595     ATR_GetParameter(&atr, ATR_PARAMETER_D, &d);
596     BWI = (param[3] & 0xF0) >> 4;
597     ccid_desc->readTimeout = T1_card_timeout(f, d, BWI /* BWI */,
598     ccid_desc->dwDefaultClock);
599    
600     DEBUG_COMM2("Timeout: %d seconds", ccid_desc->readTimeout);
601    
602 rousseau 1213 ret = SetParameters(reader_index, 1, sizeof(param), param);
603     if (IFD_SUCCESS != ret)
604     return ret;
605 rousseau 998 }
606     else
607     /* T=0 */
608     {
609     BYTE param[] = {
610     0x11, /* Fi/Di */
611     0x00, /* TCCKS */
612     0x00, /* GuardTime */
613     0x0A, /* WaitingInteger */
614     0x00 /* ClockStop */
615     };
616 rousseau 1213 RESPONSECODE ret;
617 rousseau 1452 double f;
618 rousseau 998
619     /* TA1 is not default */
620     if (PPS_HAS_PPS1(pps))
621     param[0] = pps[2];
622    
623     if (ATR_CONVENTION_INVERSE == convention)
624 rousseau 1196 param[1] |= 0x02;
625 rousseau 998
626     /* get TC1 Extra guard time */
627     if (atr.ib[0][ATR_INTERFACE_BYTE_TC].present)
628     param[2] = atr.ib[0][ATR_INTERFACE_BYTE_TC].value;
629    
630     /* TC2 WWT */
631     if (atr.ib[1][ATR_INTERFACE_BYTE_TC].present)
632     param[3] = atr.ib[1][ATR_INTERFACE_BYTE_TC].value;
633    
634 rousseau 1452 /* compute communication timeout */
635     ATR_GetParameter(&atr, ATR_PARAMETER_F, &f);
636     ccid_desc->readTimeout = T0_card_timeout(f, param[3] /* TC2 */,
637     ccid_desc->dwDefaultClock);
638    
639     DEBUG_COMM2("Communication timeout %d seconds",
640     ccid_desc->readTimeout);
641    
642 rousseau 1213 ret = SetParameters(reader_index, 0, sizeof(param), param);
643     if (IFD_SUCCESS != ret)
644     return ret;
645 rousseau 998 }
646    
647     /* set IFSC & IFSD in T=1 */
648     if (SCARD_PROTOCOL_T1 == Protocol)
649     {
650     t1_state_t *t1 = &(ccid_slot -> t1);
651     int i;
652    
653     /* TAi (i>2) present? */
654     for (i=2; i<ATR_MAX_PROTOCOLS; i++)
655     if (atr.ib[i][ATR_INTERFACE_BYTE_TA].present)
656     {
657     DEBUG_COMM3("IFSC (TA%d) present: %d", i+1,
658     atr.ib[i][ATR_INTERFACE_BYTE_TA].value);
659     t1_set_param(t1, IFD_PROTOCOL_T1_IFSC,
660     atr.ib[i][ATR_INTERFACE_BYTE_TA].value);
661    
662     /* only the first TAi (i>2) must be used */
663     break;
664     }
665    
666     /* IFSD not negociated by the reader? */
667     if (! (ccid_desc->dwFeatures & CCID_CLASS_AUTO_IFSD))
668     {
669 rousseau 1437 DEBUG_COMM2("Negociate IFSD at %d", ccid_desc -> dwMaxIFSD);
670 rousseau 998 if (t1_negociate_ifsd(t1, 0, ccid_desc -> dwMaxIFSD) < 0)
671     return IFD_COMMUNICATION_ERROR;
672     }
673     t1_set_param(t1, IFD_PROTOCOL_T1_IFSD, ccid_desc -> dwMaxIFSD);
674    
675     DEBUG_COMM3("T=1: IFSC=%d, IFSD=%d", t1->ifsc, t1->ifsd);
676     }
677    
678     return IFD_SUCCESS;
679 rousseau 269 } /* IFDHSetProtocolParameters */
680    
681    
682     RESPONSECODE IFDHPowerICC(DWORD Lun, DWORD Action,
683     PUCHAR Atr, PDWORD AtrLength)
684     {
685     /*
686     * This function controls the power and reset signals of the smartcard
687     * reader at the particular reader/slot specified by Lun.
688     *
689     * Action - Action to be taken on the card.
690     *
691     * IFD_POWER_UP - Power and reset the card if not done so (store the
692     * ATR and return it and it's length).
693     *
694     * IFD_POWER_DOWN - Power down the card if not done already
695     * (Atr/AtrLength should be zero'd)
696     *
697     * IFD_RESET - Perform a quick reset on the card. If the card is not
698     * powered power up the card. (Store and return the Atr/Length)
699     *
700     * Atr - Answer to Reset of the card. The driver is responsible for
701     * caching this value in case IFDHGetCapabilities is called requesting
702     * the ATR and it's length. This should not exceed MAX_ATR_SIZE.
703     *
704     * AtrLength - Length of the Atr. This should not exceed
705     * MAX_ATR_SIZE.
706     *
707     * Notes:
708     *
709     * Memory cards without an ATR should return IFD_SUCCESS on reset but
710     * the Atr should be zero'd and the length should be zero
711     *
712     * Reset errors should return zero for the AtrLength and return
713     * IFD_ERROR_POWER_ACTION.
714     *
715     * returns:
716     *
717     * IFD_SUCCESS IFD_ERROR_POWER_ACTION IFD_COMMUNICATION_ERROR
718     * IFD_NOT_SUPPORTED
719     */
720    
721 rousseau 892 unsigned int nlength;
722 rousseau 269 RESPONSECODE return_value = IFD_SUCCESS;
723     unsigned char pcbuffer[RESP_BUF_SIZE];
724 rousseau 1107 int reader_index;
725 rousseau 269
726 rousseau 608 DEBUG_INFO2("lun: %X", Lun);
727 rousseau 269
728 rousseau 410 /* By default, assume it won't work :) */
729 rousseau 269 *AtrLength = 0;
730    
731 rousseau 1107 if (-1 == (reader_index = LunToReaderIndex(Lun)))
732 rousseau 269 return IFD_COMMUNICATION_ERROR;
733    
734 rousseau 766 switch (Action)
735 rousseau 269 {
736 rousseau 766 case IFD_POWER_DOWN:
737     /* Clear ATR buffer */
738 rousseau 1107 CcidSlots[reader_index].nATRLength = 0;
739     *CcidSlots[reader_index].pcATRBuffer = '\0';
740 rousseau 269
741 rousseau 766 /* Memorise the request */
742 rousseau 1437 CcidSlots[reader_index].bPowerFlags |= MASK_POWERFLAGS_PDWN;
743 rousseau 998
744 rousseau 766 /* send the command */
745 rousseau 1107 if (IFD_SUCCESS != CmdPowerOff(reader_index))
746 rousseau 766 {
747     DEBUG_CRITICAL("PowerDown failed");
748     return_value = IFD_ERROR_POWER_ACTION;
749     goto end;
750     }
751 rousseau 269
752 rousseau 998 /* clear T=1 context */
753 rousseau 1107 t1_release(&(get_ccid_slot(reader_index) -> t1));
754 rousseau 840 break;
755 rousseau 616
756 rousseau 766 case IFD_POWER_UP:
757     case IFD_RESET:
758     nlength = sizeof(pcbuffer);
759 rousseau 1107 if (CmdPowerOn(reader_index, &nlength, pcbuffer) != IFD_SUCCESS)
760 rousseau 766 {
761     DEBUG_CRITICAL("PowerUp failed");
762     return_value = IFD_ERROR_POWER_ACTION;
763     goto end;
764     }
765 rousseau 269
766 rousseau 766 /* Power up successful, set state variable to memorise it */
767 rousseau 1437 CcidSlots[reader_index].bPowerFlags |= MASK_POWERFLAGS_PUP;
768     CcidSlots[reader_index].bPowerFlags &= ~MASK_POWERFLAGS_PDWN;
769 rousseau 269
770 rousseau 766 /* Reset is returned, even if TCK is wrong */
771 rousseau 1107 CcidSlots[reader_index].nATRLength = *AtrLength =
772 rousseau 766 (nlength < MAX_ATR_SIZE) ? nlength : MAX_ATR_SIZE;
773     memcpy(Atr, pcbuffer, *AtrLength);
774 rousseau 1437 memcpy(CcidSlots[reader_index].pcATRBuffer, pcbuffer, *AtrLength);
775 rousseau 616
776 rousseau 998 /* initialise T=1 context */
777 rousseau 1107 t1_init(&(get_ccid_slot(reader_index) -> t1), reader_index);
778 rousseau 766 break;
779    
780     default:
781     DEBUG_CRITICAL("Action not supported");
782     return_value = IFD_NOT_SUPPORTED;
783 rousseau 269 }
784     end:
785    
786     return return_value;
787     } /* IFDHPowerICC */
788    
789    
790     RESPONSECODE IFDHTransmitToICC(DWORD Lun, SCARD_IO_HEADER SendPci,
791     PUCHAR TxBuffer, DWORD TxLength,
792 rousseau 1053 PUCHAR RxBuffer, PDWORD RxLength, /*@unused@*/ PSCARD_IO_HEADER RecvPci)
793 rousseau 269 {
794     /*
795     * This function performs an APDU exchange with the card/slot
796     * specified by Lun. The driver is responsible for performing any
797     * protocol specific exchanges such as T=0/1 ... differences. Calling
798     * this function will abstract all protocol differences.
799     *
800     * SendPci Protocol - 0, 1, .... 14 Length - Not used.
801     *
802     * TxBuffer - Transmit APDU example (0x00 0xA4 0x00 0x00 0x02 0x3F
803     * 0x00) TxLength - Length of this buffer. RxBuffer - Receive APDU
804     * example (0x61 0x14) RxLength - Length of the received APDU. This
805     * function will be passed the size of the buffer of RxBuffer and this
806     * function is responsible for setting this to the length of the
807     * received APDU. This should be ZERO on all errors. The resource
808     * manager will take responsibility of zeroing out any temporary APDU
809     * buffers for security reasons.
810     *
811     * RecvPci Protocol - 0, 1, .... 14 Length - Not used.
812     *
813     * Notes: The driver is responsible for knowing what type of card it
814     * has. If the current slot/card contains a memory card then this
815     * command should ignore the Protocol and use the MCT style commands
816     * for support for these style cards and transmit them appropriately.
817     * If your reader does not support memory cards or you don't want to
818     * then ignore this.
819     *
820     * RxLength should be set to zero on error.
821     *
822     * returns:
823     *
824     * IFD_SUCCESS IFD_COMMUNICATION_ERROR IFD_RESPONSE_TIMEOUT
825     * IFD_ICC_NOT_PRESENT IFD_PROTOCOL_NOT_SUPPORTED
826     */
827    
828 rousseau 611 RESPONSECODE return_value;
829 rousseau 892 unsigned int rx_length;
830 rousseau 1107 int reader_index;
831 rousseau 269
832 rousseau 608 DEBUG_INFO2("lun: %X", Lun);
833 rousseau 269
834 rousseau 1107 if (-1 == (reader_index = LunToReaderIndex(Lun)))
835 rousseau 269 return IFD_COMMUNICATION_ERROR;
836    
837 rousseau 611 rx_length = *RxLength;
838 rousseau 1107 return_value = CmdXfrBlock(reader_index, TxLength, TxBuffer, &rx_length,
839     RxBuffer, SendPci.Protocol);
840 rousseau 611 *RxLength = rx_length;
841 rousseau 269
842     return return_value;
843     } /* IFDHTransmitToICC */
844    
845    
846 rousseau 1085 RESPONSECODE IFDHControl(DWORD Lun, DWORD dwControlCode, PUCHAR TxBuffer,
847     DWORD TxLength, PUCHAR RxBuffer, DWORD RxLength, PDWORD pdwBytesReturned)
848 rousseau 269 {
849     /*
850     * This function performs a data exchange with the reader (not the
851     * card) specified by Lun. Here XXXX will only be used. It is
852     * responsible for abstracting functionality such as PIN pads,
853     * biometrics, LCD panels, etc. You should follow the MCT, CTBCS
854     * specifications for a list of accepted commands to implement.
855     *
856     * TxBuffer - Transmit data TxLength - Length of this buffer. RxBuffer
857     * - Receive data RxLength - Length of the received data. This
858     * function will be passed the length of the buffer RxBuffer and it
859     * must set this to the length of the received data.
860     *
861     * Notes: RxLength should be zero on error.
862     */
863 rousseau 880 RESPONSECODE return_value = IFD_COMMUNICATION_ERROR;
864 rousseau 1107 int reader_index;
865 rousseau 269
866 rousseau 795 DEBUG_INFO3("lun: %X, ControlCode: 0x%X", Lun, dwControlCode);
867 rousseau 269
868 rousseau 1107 reader_index = LunToReaderIndex(Lun);
869     if ((-1 == reader_index) || (NULL == pdwBytesReturned)
870     || (NULL == RxBuffer))
871 rousseau 880 return return_value;
872 rousseau 269
873 rousseau 568 /* Set the return length to 0 to avoid problems */
874 rousseau 880 *pdwBytesReturned = 0;
875 rousseau 568
876 rousseau 880 if (IOCTL_SMARTCARD_VENDOR_IFD_EXCHANGE == dwControlCode)
877     {
878     if (FALSE == (DriverOptions & DRIVER_OPTION_CCID_EXCHANGE_AUTHORIZED))
879 rousseau 891 {
880     DEBUG_INFO("ifd exchange (Escape command) not allowed");
881 rousseau 880 return_value = IFD_COMMUNICATION_ERROR;
882 rousseau 891 }
883 rousseau 880 else
884     {
885 rousseau 892 unsigned int iBytesReturned;
886 rousseau 891
887     iBytesReturned = RxLength;
888 rousseau 1107 return_value = CmdEscape(reader_index, TxBuffer, TxLength, RxBuffer,
889 rousseau 891 &iBytesReturned);
890     *pdwBytesReturned = iBytesReturned;
891 rousseau 880 }
892     }
893    
894 rousseau 891 if (IOCTL_SMARTCARD_VENDOR_VERIFY_PIN == dwControlCode)
895     {
896 rousseau 892 unsigned int iBytesReturned;
897 rousseau 891
898     iBytesReturned = RxLength;
899 rousseau 1107 return_value = SecurePIN(reader_index, TxBuffer, TxLength, RxBuffer,
900 rousseau 891 &iBytesReturned);
901     *pdwBytesReturned = iBytesReturned;
902     }
903    
904 rousseau 880 return return_value;
905 rousseau 269 } /* IFDHControl */
906    
907    
908     RESPONSECODE IFDHICCPresence(DWORD Lun)
909     {
910     /*
911     * This function returns the status of the card inserted in the
912     * reader/slot specified by Lun. It will return either:
913     *
914     * returns: IFD_ICC_PRESENT IFD_ICC_NOT_PRESENT
915     * IFD_COMMUNICATION_ERROR
916     */
917    
918     unsigned char pcbuffer[SIZE_GET_SLOT_STATUS];
919     RESPONSECODE return_value = IFD_COMMUNICATION_ERROR;
920 rousseau 999 int oldLogLevel;
921 rousseau 1107 int reader_index;
922 rousseau 1154 _ccid_descriptor *ccid_descriptor;
923 rousseau 1452 unsigned int oldReadTimeout;
924 rousseau 269
925 rousseau 608 DEBUG_PERIODIC2("lun: %X", Lun);
926 rousseau 269
927 rousseau 1107 if (-1 == (reader_index = LunToReaderIndex(Lun)))
928 rousseau 269 return IFD_COMMUNICATION_ERROR;
929    
930 rousseau 1154 ccid_descriptor = get_ccid_descriptor(reader_index);
931    
932 rousseau 1452 /* save the current read timeout computed from card capabilities */
933     oldReadTimeout = ccid_descriptor->readTimeout;
934    
935     /* use default timeout since the reader may not be present anymore */
936     ccid_descriptor->readTimeout = DEFAULT_COM_READ_TIMEOUT;
937    
938 rousseau 999 /* if DEBUG_LEVEL_PERIODIC is not set we remove DEBUG_LEVEL_COMM */
939     oldLogLevel = LogLevel;
940     if (! (LogLevel & DEBUG_LEVEL_PERIODIC))
941     LogLevel &= ~DEBUG_LEVEL_COMM;
942    
943 rousseau 1107 return_value = CmdGetSlotStatus(reader_index, pcbuffer);
944 rousseau 999
945 rousseau 1452 /* set back the old timeout */
946     ccid_descriptor->readTimeout = oldReadTimeout;
947    
948 rousseau 999 /* set back the old LogLevel */
949     LogLevel = oldLogLevel;
950    
951     if (return_value != IFD_SUCCESS)
952 rousseau 269 return IFD_COMMUNICATION_ERROR;
953    
954 rousseau 999 return_value = IFD_COMMUNICATION_ERROR;
955 rousseau 1261 switch (pcbuffer[7] & CCID_ICC_STATUS_MASK) /* bStatus */
956 rousseau 269 {
957 rousseau 1261 case CCID_ICC_PRESENT_ACTIVE:
958     case CCID_ICC_PRESENT_INACTIVE:
959 rousseau 269 return_value = IFD_ICC_PRESENT;
960 rousseau 1094 /* use default slot */
961 rousseau 269 break;
962    
963 rousseau 1261 case CCID_ICC_ABSENT:
964 rousseau 807 /* Reset ATR buffer */
965 rousseau 1107 CcidSlots[reader_index].nATRLength = 0;
966     *CcidSlots[reader_index].pcATRBuffer = '\0';
967 rousseau 807
968     /* Reset PowerFlags */
969 rousseau 1107 CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ;
970 rousseau 807
971 rousseau 269 return_value = IFD_ICC_NOT_PRESENT;
972     break;
973     }
974    
975 rousseau 1094 /* SCR331-DI contactless reader */
976     if ((SCR331DI == ccid_descriptor->readerID)
977 rousseau 1155 && (ccid_descriptor->bCurrentSlotIndex > 0))
978 rousseau 1094 {
979     unsigned char cmd[] = { 0x11 };
980     /* command: 11 ??
981     * response: 00 11 01 ?? no card
982     * 01 04 00 ?? card present */
983    
984     unsigned char res[10];
985     unsigned int length_res = sizeof(res);
986    
987     /* if DEBUG_LEVEL_PERIODIC is not set we remove DEBUG_LEVEL_COMM */
988     oldLogLevel = LogLevel;
989     if (! (LogLevel & DEBUG_LEVEL_PERIODIC))
990     LogLevel &= ~DEBUG_LEVEL_COMM;
991    
992 rousseau 1107 CmdEscape(reader_index, cmd, sizeof(cmd), res, &length_res);
993 rousseau 1094
994     /* set back the old LogLevel */
995     LogLevel = oldLogLevel;
996    
997     if (0x01 == res[0])
998     return_value = IFD_ICC_PRESENT;
999 rousseau 1155 else
1000     {
1001     /* Reset ATR buffer */
1002     CcidSlots[reader_index].nATRLength = 0;
1003     *CcidSlots[reader_index].pcATRBuffer = '\0';
1004 rousseau 1094
1005 rousseau 1155 /* Reset PowerFlags */
1006     CcidSlots[reader_index].bPowerFlags = POWERFLAGS_RAZ;
1007 rousseau 1094
1008 rousseau 1155 return_value = IFD_ICC_NOT_PRESENT;
1009 rousseau 1094 }
1010     }
1011    
1012 rousseau 1156 DEBUG_PERIODIC2("Card %s",
1013     IFD_ICC_PRESENT == return_value ? "present" : "absent");
1014    
1015 rousseau 269 return return_value;
1016     } /* IFDHICCPresence */
1017    
1018 rousseau 609
1019 rousseau 1107 CcidDesc *get_ccid_slot(unsigned int reader_index)
1020 rousseau 609 {
1021 rousseau 1107 return &CcidSlots[reader_index];
1022 rousseau 609 } /* get_ccid_slot */
1023    
1024 rousseau 673
1025 rousseau 880 void init_driver(void)
1026 rousseau 773 {
1027     char keyValue[TOKEN_MAX_VALUE_SIZE];
1028     char infofile[FILENAME_MAX];
1029    
1030     /* Info.plist full patch filename */
1031     snprintf(infofile, sizeof(infofile), "%s/%s/Contents/Info.plist",
1032     PCSCLITE_HP_DROPDIR, BUNDLE);
1033    
1034 rousseau 880 /* Log level */
1035     if (0 == LTPBundleFindValueWithKey(infofile, "ifdLogLevel", keyValue, 0))
1036     {
1037     /* convert from hex or dec or octal */
1038 rousseau 1078 LogLevel = strtoul(keyValue, NULL, 0);
1039 rousseau 773
1040 rousseau 880 /* print the log level used */
1041 rousseau 1408 DEBUG_INFO2("LogLevel: 0x%.4X", LogLevel);
1042 rousseau 880 }
1043 rousseau 773
1044 rousseau 880 /* Driver options */
1045     if (0 == LTPBundleFindValueWithKey(infofile, "ifdDriverOptions", keyValue, 0))
1046     {
1047     /* convert from hex or dec or octal */
1048 rousseau 1078 DriverOptions = strtoul(keyValue, NULL, 0);
1049 rousseau 773
1050 rousseau 880 /* print the log level used */
1051 rousseau 1408 DEBUG_INFO2("DriverOptions: 0x%.4X", DriverOptions);
1052 rousseau 880 }
1053    
1054 rousseau 1107 /* initialise the Lun to reader_index mapping */
1055     InitReaderIndex();
1056    
1057 rousseau 773 DebugInitialized = TRUE;
1058 rousseau 880 } /* init_driver */
1059 rousseau 773
1060 rousseau 1438
1061     void extra_egt(ATR_t *atr, _ccid_descriptor *ccid_desc, DWORD Protocol)
1062     {
1063 rousseau 1442 /* This function use an EGT value for cards who comply with followings
1064     * criterias:
1065     * - TA1 > 11
1066     * - current EGT = 0x00 or 0xFF
1067     * - T=0 or (T=1 and CWI >= 2)
1068     *
1069     * Without this larger EGT some non ISO 7816-3 smart cards may not
1070     * communicate with the reader.
1071     *
1072     * This modification is harmless, the reader will just be less restrictive
1073 rousseau 1438 */
1074    
1075 rousseau 1441 unsigned int card_baudrate;
1076     unsigned int default_baudrate;
1077     double f, d;
1078 rousseau 1438 int i;
1079    
1080     /* if TA1 not present */
1081     if (! atr->ib[0][ATR_INTERFACE_BYTE_TA].present)
1082     return;
1083    
1084     ATR_GetParameter(atr, ATR_PARAMETER_D, &d);
1085     ATR_GetParameter(atr, ATR_PARAMETER_F, &f);
1086    
1087     /* Baudrate = f x D/F */
1088 rousseau 1441 card_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock * d / f);
1089 rousseau 1438
1090 rousseau 1441 default_baudrate = (unsigned int) (1000 * ccid_desc->dwDefaultClock
1091     * ATR_DEFAULT_D / ATR_DEFAULT_F);
1092 rousseau 1438
1093     /* TA1 > 11? */
1094     if (card_baudrate <= default_baudrate)
1095     return;
1096    
1097     /* Current EGT = 0 or FF? */
1098     if (atr->ib[0][ATR_INTERFACE_BYTE_TC].present &&
1099     ((0x00 == atr->ib[0][ATR_INTERFACE_BYTE_TC].value) ||
1100 rousseau 1440 (0xFF == atr->ib[0][ATR_INTERFACE_BYTE_TC].value)))
1101 rousseau 1438 {
1102     if (SCARD_PROTOCOL_T0 == Protocol)
1103     {
1104     /* Init TC1 */
1105     atr->ib[0][ATR_INTERFACE_BYTE_TC].present = TRUE;
1106     atr->ib[0][ATR_INTERFACE_BYTE_TC].value = 2;
1107 rousseau 1440 DEBUG_INFO("Extra EGT patch applied");
1108 rousseau 1438 }
1109    
1110     if (SCARD_PROTOCOL_T1 == Protocol)
1111     {
1112 rousseau 1439 /* TBi (i>2) present? BWI/CWI */
1113 rousseau 1438 for (i=2; i<ATR_MAX_PROTOCOLS; i++)
1114     {
1115 rousseau 1440 /* CWI >= 2 ? */
1116     if (atr->ib[i][ATR_INTERFACE_BYTE_TB].present &&
1117     ((atr->ib[i][ATR_INTERFACE_BYTE_TB].value & 0x0F) >= 2))
1118 rousseau 1438 {
1119 rousseau 1440 /* Init TC1 */
1120     atr->ib[0][ATR_INTERFACE_BYTE_TC].present = TRUE;
1121     atr->ib[0][ATR_INTERFACE_BYTE_TC].value = 2;
1122     DEBUG_INFO("Extra EGT patch applied");
1123 rousseau 1438
1124     /* only the first TBi (i>2) must be used */
1125     break;
1126     }
1127     }
1128     }
1129     }
1130     } /* extra_egt */
1131    
1132 rousseau 1448
1133     static char find_baud_rate(unsigned int baudrate, unsigned int *list)
1134     {
1135     int i;
1136    
1137     DEBUG_COMM2("Card baud rate: %d", baudrate);
1138    
1139     /* Does the reader support the annonced smart card data speed? */
1140     for (i=0;; i++)
1141     {
1142     /* end of array marker */
1143     if (0 == list[i])
1144     break;
1145    
1146     DEBUG_COMM2("Reader can do: %d", list[i]);
1147    
1148     /* We must take into account that the card_baudrate integral value
1149     * is an approximative result, computed from the d/f float result.
1150     */
1151     if ((baudrate < list[i] + 2) && (baudrate > list[i] - 2))
1152     return TRUE;
1153     }
1154    
1155     return FALSE;
1156     } /* find_baud_rate */
1157    
1158 rousseau 1451
1159     static unsigned int T0_card_timeout(double f, int TC2, int clock_frequency)
1160     {
1161     unsigned int timeout = DEFAULT_COM_READ_TIMEOUT;
1162     unsigned int t;
1163    
1164     /* card WWT */
1165     /* see ch. 8.2 Character level, page 15 of ISO 7816-3 */
1166     t = 960 * TC2 * f / (clock_frequency * 1000);
1167    
1168     /* use the bigest one */
1169     if (t > timeout)
1170     timeout = t;
1171    
1172     /* default WWT (TC2=0x0A) */
1173     t = 960 * 0x0A * f / (clock_frequency * 1000);
1174    
1175     /* use the bigest one */
1176     if (t > timeout)
1177     timeout = t;
1178    
1179     return timeout;
1180     } /* T0_card_timeout */
1181    
1182    
1183     static unsigned int T1_card_timeout(double f, double d, int BWI,
1184     int clock_frequency)
1185     {
1186     unsigned int timeout = DEFAULT_COM_READ_TIMEOUT;
1187     unsigned int t;
1188    
1189     /* card BWT */
1190     /* see ch. 9.5.3.2 Block waiting time, page 20 of ISO 7816-3 */
1191     t = 11 * f / d / (clock_frequency * 1000) + (1<<BWI) * 960 * 372 / (clock_frequency * 1000); /* seconds */
1192    
1193     /* use the bigest one */
1194     if (t > timeout)
1195     timeout = t;
1196    
1197     /* default BWT (BWI=0x04) */
1198     t = 11 * f / d / (clock_frequency * 1000) + (1<<4) * 960 * 372 / (clock_frequency * 1000); /* seconds */
1199    
1200     /* use the bigest one */
1201     if (t > timeout)
1202     timeout = t;
1203    
1204     return timeout;
1205     } /* T1_card_timeout */
1206    

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.5