source: trunk/csctapi/icc_async.c@ 1601

Last change on this file since 1601 was 1601, checked in by merek, 11 years ago

Merge from UMP r1717

File size: 23.6 KB
Line 
1/*
2 icc_async.c
3 Asynchronous ICC's handling functions
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
7
8 This version is modified by doz21 to work in a special manner ;)
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include "../globals.h"
29#include "defines.h"
30#include "icc_async.h"
31#include "ifd.h"
32#include "mc_global.h"
33#include "apdu.h"
34#include "protocol_t0.h"
35#include "protocol_t1.h"
36
37// Default T0/T14 settings
38#define DEFAULT_WI 10
39// Default T1 settings
40#define DEFAULT_IFSC 32
41#define MAX_IFSC 251 /* Cannot send > 255 buffer */
42#define DEFAULT_CWI 13
43#define DEFAULT_BWI 4
44#define EDC_LRC 0
45
46#define PPS_MAX_LENGTH 6
47#define PPS_HAS_PPS1(block) ((block[1] & 0x10) == 0x10)
48#define PPS_HAS_PPS2(block) ((block[1] & 0x20) == 0x20)
49#define PPS_HAS_PPS3(block) ((block[1] & 0x40) == 0x40)
50
51
52/*
53 * Not exported functions declaration
54 */
55
56static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
57static int Parse_ATR (ATR * atr, unsigned short deprecated);
58static int PPS_Exchange (BYTE * params, unsigned *length);
59static unsigned PPS_GetLength (BYTE * block);
60static int InitCard (ATR * atr, BYTE FI, double d, double n, unsigned short deprecated);
61static unsigned int ETU_to_ms(unsigned long WWT);
62static BYTE PPS_GetPCK (BYTE * block, unsigned length);
63static int Protocol_Command (unsigned char * command, unsigned long command_len, APDU_Rsp ** rsp);
64static int SetRightParity (void);
65
66int fdmc=(-1);
67
68/*
69 * Exported functions definition
70 */
71
72int ICC_Async_Device_Init ()
73{
74 cs_debug_mask (D_IFD, "IFD: Opening device %s\n", reader[ridx].device);
75
76 wr = 0;
77 if (reader[ridx].typ <= R_MOUSE)
78 reader[ridx].handle = open (reader[ridx].device, O_RDWR | O_NOCTTY| O_NONBLOCK);
79 if (reader[ridx].handle < 0) {
80 cs_log("ERROR opening device %s",reader[ridx].device);
81 return ERROR;
82 }
83
84 switch(reader[ridx].typ) {
85 case R_MOUSE:
86 break;
87#if defined(TUXBOX) && defined(PPC)
88 case R_DB2COM1:
89 case R_DB2COM2:
90 if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0) {
91 close(reader[ridx].handle);
92 cs_log("ERROR opening device %s",DEV_MULTICAM);
93 return ERROR;
94 }
95 break;
96#endif
97 case R_SMART:
98#if defined(LIBUSB)
99 call (SR_Init(&reader[ridx]));
100 break;
101#else
102 cs_log("ERROR, you have specified 'protocol = smartreader' in oscam.server,");
103 cs_log("recompile with SmartReader support.");
104 return ERROR;
105#endif
106 case R_INTERNAL:
107#ifdef COOL
108 return Cool_Init();
109#elif SCI_DEV
110 #if defined(SH4) || defined(STB04SCI)
111 reader[ridx].handle = open (reader[ridx].device, O_RDWR|O_NONBLOCK|O_NOCTTY);
112 #else
113 reader[ridx].handle = open (reader[ridx].device, O_RDWR);
114 #endif
115 if (reader[ridx].handle < 0) {
116 cs_log("ERROR opening device %s",reader[ridx].device);
117 return ERROR;
118 }
119#else//SCI_DEV
120 cs_log("ERROR, you have specified 'protocol = internal' in oscam.server,");
121 cs_log("recompile with internal reader support.");
122 return ERROR;
123#endif//SCI_DEV
124 break;
125 default:
126 cs_log("ERROR ICC_Device_Init: unknow reader type %i",reader[ridx].typ);
127 return ERROR;
128 }
129
130 if (reader[ridx].typ <= R_MOUSE)
131 if (Phoenix_Init()) {
132 cs_log("ERROR: Phoenix_Init returns error");
133 Phoenix_Close ();
134 return ERROR;
135 }
136 cs_debug_mask (D_IFD, "IFD: Device %s succesfully opened\n", reader[ridx].device);
137 return OK;
138}
139
140int ICC_Async_GetStatus (int * card)
141{
142 int in;
143
144// printf("\n%08X\n", (int)ifd->io);
145
146 switch(reader[ridx].typ) {
147 case R_DB2COM1:
148 case R_DB2COM2:
149#if defined(TUXBOX) && defined(PPC)
150 {
151 ushort msr=1;
152 extern int fdmc;
153 IO_Serial_Ioctl_Lock(1);
154 ioctl(fdmc, GET_PCDAT, &msr);
155 if (reader[ridx].typ == R_DB2COM2)
156 in=(!(msr & 1));
157 else
158 in=((msr & 0x0f00) == 0x0f00);
159 IO_Serial_Ioctl_Lock(0);
160 }
161 break;
162#endif
163 case R_MOUSE:
164 call (Phoenix_GetStatus(&in));
165 break;
166#if defined(LIBUSB)
167 case R_SMART:
168 call (SR_GetStatus(&reader[ridx],&in));
169 break;
170#endif
171 case R_INTERNAL:
172#ifdef SCI_DEV
173 call (Sci_GetStatus(reader[ridx].handle, &in));
174#elif COOL
175 call (Cool_GetStatus(&in));
176#endif
177 break;
178 default:
179 cs_log("ERROR ICC_Device_Init: unknow reader type %i",reader[ridx].typ);
180 return ERROR;
181 }
182
183 if (in)
184 *card = TRUE;
185 else
186 *card = FALSE;
187
188 //cs_debug_mask (D_TRACE, "IFD: Status = %s", in ? "card": "no card");
189
190 return OK;
191}
192
193int ICC_Async_Activate (ATR * atr, unsigned short deprecated)
194{
195 cs_debug_mask (D_IFD, "IFD: Activating card in reader %s\n", reader[ridx].label);
196 int card;
197
198 call (ICC_Async_GetStatus (&card));
199 if (card == 0) {
200 cs_log("ERROR: Trying to activate card but no card inside");
201 return ERROR;
202 }
203
204 current_baudrate = DEFAULT_BAUDRATE; //this is needed for all readers to calculate work_etu for timings
205 switch(reader[ridx].typ) {
206 case R_DB2COM1:
207 case R_DB2COM2:
208 case R_MOUSE:
209 call (Phoenix_Reset(atr));
210 break;
211#if defined(LIBUSB)
212 case R_SMART:
213 call (SR_Reset(&reader[ridx],atr));
214 break;
215#endif
216 case R_INTERNAL:
217#ifdef SCI_DEV
218 call (Sci_Activate());
219 call (Sci_Reset(atr));
220#elif COOL
221 call (Cool_Reset(atr));
222#endif
223 break;
224 default:
225 cs_log("ERROR ICC_Async_Activate: unknow reader type %i",reader[ridx].typ);
226 return ERROR;
227 }
228
229 unsigned char atrarr[64];
230 unsigned int atr_size;
231 ATR_GetRaw(atr, atrarr, &atr_size);
232 cs_ri_log("ATR: %s", cs_hexdump(1, atrarr, atr_size));
233
234 /* Get ICC convention */
235 if (ATR_GetConvention (atr, &(convention)) != ATR_OK) {
236 cs_log("ERROR: Could not read convention");
237 convention = 0;
238 protocol_type = 0;
239 return ERROR;
240 }
241
242 protocol_type = ATR_PROTOCOL_TYPE_T0;
243
244 unsigned short cs_ptyp_orig=cs_ptyp;
245 cs_ptyp=D_ATR;
246 int ret = Parse_ATR(atr, deprecated);
247 if (ret)
248 cs_log("ERROR: Parse_ATR returned error");
249 cs_ptyp=cs_ptyp_orig;
250 if (ret)
251 return ERROR;
252 cs_debug_mask (D_IFD, "IFD: Card in reader %s succesfully activated\n", reader[ridx].label);
253 return OK;
254}
255
256int ICC_Async_CardWrite (unsigned char *cmd, unsigned short lc, unsigned char *rsp, unsigned short *lr)
257{
258 APDU_Rsp *apdu_rsp = NULL;
259 int remain;
260 bool err = FALSE;
261
262 call (Protocol_Command (cmd, lc, &apdu_rsp));
263 {
264 if (apdu_rsp != NULL) {
265 /* Copy APDU data to rsp */
266 remain = MAX ((short)APDU_Rsp_RawLen(apdu_rsp) - (*lr),0);
267 if (remain > 0) {
268 cs_log("MEMORY ERROR");
269 err = TRUE; //FIXME do I need this?
270 }
271 (*lr) = MIN ((*lr), (short)APDU_Rsp_RawLen (apdu_rsp));
272 memcpy (rsp, APDU_Rsp_Raw (apdu_rsp) + remain, (*lr));
273 APDU_Rsp_Delete (apdu_rsp);
274 }
275 else
276 (*lr) = 0;
277 }
278
279 if (err) {
280 cs_log("ERROR creating APDU response");
281 return ERROR;
282 }
283
284 return OK;
285}
286
287int Protocol_Command (unsigned char * command, unsigned long command_len, APDU_Rsp ** rsp)
288{
289 switch (protocol_type) {
290 case ATR_PROTOCOL_TYPE_T0:
291 call (Protocol_T0_Command (command, command_len, rsp));
292 break;
293 case ATR_PROTOCOL_TYPE_T1:
294 {
295 int try = 1;
296 do {
297 if (Protocol_T1_Command (command, command_len, rsp) == OK)
298 break;
299 try++;
300 //try to resync
301 APDU_Rsp ** rsp;
302 unsigned char resync[] = { 0x21, 0xC0, 0x00, 0xE1 };
303 Protocol_T1_Command (resync, sizeof(resync), rsp);
304 ifsc = DEFAULT_IFSC;
305 } while (try <= 3);
306 break;
307 }
308 case ATR_PROTOCOL_TYPE_T14:
309 call (Protocol_T14_ExchangeTPDU (command, command_len, rsp));
310 break;
311 default:
312 cs_log("Error, unknown protocol type %i",protocol_type);
313 return ERROR;
314 }
315 return OK;
316}
317
318int ICC_Async_SetTimings (unsigned wait_etu)
319{
320 read_timeout = ETU_to_ms(wait_etu);
321 cs_debug_mask(D_IFD, "Setting timeout to %i", wait_etu);
322 return OK;
323}
324
325int ICC_Async_Transmit (unsigned size, BYTE * data)
326{
327 cs_ddump_mask(D_IFD, data, size, "IFD Transmit: ");
328 BYTE *buffer = NULL, *sent;
329
330 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ <= R_MOUSE) {
331 buffer = (BYTE *) calloc(sizeof (BYTE), size);
332 memcpy (buffer, data, size);
333 ICC_Async_InvertBuffer (size, buffer);
334 sent = buffer;
335 }
336 else
337 sent = data;
338
339 switch(reader[ridx].typ) {
340 case R_DB2COM1:
341 case R_DB2COM2:
342 case R_MOUSE:
343 call (Phoenix_Transmit (sent, size, icc_timings.block_delay, icc_timings.char_delay));
344 break;
345#if defined(LIBUSB)
346 case R_SMART:
347 call (SR_Transmit(&reader[ridx], sent, size));
348 break;
349#endif
350 case R_INTERNAL:
351#ifdef COOL
352 call (Cool_Transmit(sent, size));
353#elif SCI_DEV
354 call (Phoenix_Transmit (sent, size, 0, 0)); //the internal reader will provide the delay
355#endif
356 break;
357 default:
358 cs_log("ERROR ICC_Async_Transmit: unknow reader type %i",reader[ridx].typ);
359 return ERROR;
360 }
361
362 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ <= R_MOUSE)
363 free (buffer);
364 cs_debug_mask(D_IFD, "IFD Transmit succesful");
365 return OK;
366}
367
368int ICC_Async_Receive (unsigned size, BYTE * data)
369{
370 switch(reader[ridx].typ) {
371 case R_DB2COM1:
372 case R_DB2COM2:
373 case R_MOUSE:
374 call (Phoenix_Receive (data, size, read_timeout));
375 break;
376#if defined(LIBUSB)
377 case R_SMART:
378 call (SR_Receive(&reader[ridx], data, size));
379 break;
380#endif
381 case R_INTERNAL:
382#ifdef COOL
383 call (Cool_Receive(data, size));
384#elif SCI_DEV
385 call (Phoenix_Receive (data, size, read_timeout));
386#endif
387 break;
388 default:
389 cs_log("ERROR ICC_Async_Receive: unknow reader type %i",reader[ridx].typ);
390 return ERROR;
391 }
392
393 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ <= R_MOUSE)
394 ICC_Async_InvertBuffer (size, data);
395
396 cs_ddump_mask(D_IFD, data, size, "IFD Received: ");
397 return OK;
398}
399
400int ICC_Async_Close ()
401{ //FIXME this routine is never called!
402 cs_debug_mask (D_IFD, "IFD: Closing device %s", reader[ridx].device);
403
404 switch(reader[ridx].typ) {
405 case R_DB2COM1:
406 case R_DB2COM2:
407 case R_MOUSE:
408 call (Phoenix_Close());
409 break;
410#if defined(LIBUSB)
411 case R_SMART:
412 call (SR_Close(&reader[ridx]));
413 break;
414#endif
415 case R_INTERNAL:
416#ifdef SCI_DEV
417 /* Dectivate ICC */
418 call (Sci_Deactivate());
419 call (Phoenix_Close());
420#endif
421 break;
422 default:
423 cs_log("ERROR ICC_Async_Close: unknow reader type %i",reader[ridx].typ);
424 return ERROR;
425 }
426
427 cs_debug_mask (D_IFD, "IFD: Device %s succesfully closed", reader[ridx].device);
428 return OK;
429}
430
431unsigned long ICC_Async_GetClockRate ()
432{
433 switch (reader[ridx].cardmhz) {
434 case 357:
435 case 358:
436 return (372L * 9600L);
437 case 368:
438 return (384L * 9600L);
439 default:
440 return reader[ridx].cardmhz * 10000L;
441 }
442}
443
444static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
445{
446 uint i;
447
448 for (i = 0; i < size; i++)
449 buffer[i] = ~(INVERT_BYTE (buffer[i]));
450}
451
452static int Parse_ATR (ATR * atr, unsigned short deprecated)
453{
454 BYTE FI = ATR_DEFAULT_FI;
455 //BYTE t = ATR_PROTOCOL_TYPE_T0;
456 double d = ATR_DEFAULT_D;
457 double n = ATR_DEFAULT_N;
458 int ret;
459
460 int numprot = atr->pn;
461 //if there is a trailing TD, this number is one too high
462 BYTE tx;
463 if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
464 if ((tx & 0xF0) == 0)
465 numprot--;
466 int i,point;
467 char txt[50];
468 bool OffersT[3]; //T14 stored as T2
469 for (i = 0; i <= 2; i++)
470 OffersT[i] = FALSE;
471 for (i=1; i<= numprot; i++) {
472 point = 0;
473 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
474 sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
475 point +=7;
476 }
477 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
478 sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
479 point +=7;
480 }
481 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
482 sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
483 point +=7;
484 }
485 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
486 sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
487 point +=7;
488 tx &= 0X0F;
489 sprintf((char *)txt+point,"(T%i)",tx);
490 if (tx == 14)
491 OffersT[2] = TRUE;
492 else
493 OffersT[tx] = TRUE;
494 }
495 else {
496 sprintf((char *)txt+point,"no TD%i means T0",i);
497 OffersT[0] = TRUE;
498 }
499 cs_debug("%s",txt);
500 }
501
502 int numprottype = 0;
503 for (i = 0; i <= 2; i++)
504 if (OffersT[i])
505 numprottype ++;
506 cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
507
508 ATR_GetParameter (atr, ATR_PARAMETER_N, &(n));
509 ATR_GetProtocolType(atr,1,&(protocol_type)); //get protocol from TD1
510 BYTE TA2;
511 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
512 if (SpecificMode) {
513 protocol_type = TA2 & 0x0F;
514 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
515 BYTE TA1;
516 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
517 FI = TA1 >> 4;
518 ATR_GetParameter (atr, ATR_PARAMETER_D, &(d));
519 }
520 else {
521 FI = ATR_DEFAULT_FI;
522 d = ATR_DEFAULT_D;
523 }
524 }
525 else {
526 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
527 FI = ATR_DEFAULT_FI;
528 d = ATR_DEFAULT_D;
529 }
530 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", protocol_type, (double) atr_f_table[FI], d, n);
531 }
532 else { //negotiable mode
533
534 bool PPS_success = FALSE;
535 bool NeedsPTS = ((protocol_type != ATR_PROTOCOL_TYPE_T14) && (numprottype > 1 || (atr->ib[0][ATR_INTERFACE_BYTE_TA].present == TRUE && atr->ib[0][ATR_INTERFACE_BYTE_TA].value != 0x11) || n == 255)); //needs PTS according to old ISO 7816
536 if (NeedsPTS && deprecated == 0) {
537 // PTSS PTS0 PTS1 PCK
538 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
539 req[1]=0x10 | protocol_type; //PTS0 always flags PTS1 to be sent always
540 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
541 req[2] = 0x11; //defaults FI and DI to 1
542 unsigned int len = sizeof(req);
543 ret = PPS_Exchange (req, &len);
544 if (ret == OK) {
545 FI = req[2] >> 4;
546 BYTE DI = req[2] & 0x0F;
547 d = (double) (atr_d_table[DI]);
548 PPS_success = TRUE;
549 cs_debug("PTS Succesfull, selected protocol: T%i, F=%.0f, D=%.6f, N=%.0f\n", protocol_type, (double) atr_f_table[FI], d, n);
550 }
551 else
552 cs_ddump(req,4,"PTS Failure, response:");
553 }
554
555 //When for SCI, T14 protocol, TA1 is obeyed, this goes OK for mosts devices, but somehow on DM7025 Sky S02 card goes wrong when setting ETU (ok on DM800/DM8000)
556 if (!PPS_success) {//last PPS not succesfull
557 BYTE TA1;
558 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
559 FI = TA1 >> 4;
560 ATR_GetParameter (atr, ATR_PARAMETER_D, &(d));
561 }
562 else { //do not obey TA1
563 FI = ATR_DEFAULT_FI;
564 d = ATR_DEFAULT_D;
565 }
566 if (NeedsPTS) {
567 if ((d == 32) || (d == 12) || (d == 20)) //those values were RFU in old table
568 d = 0; // viaccess cards that fail PTS need this
569 }
570
571 cs_debug("No PTS %s, selected protocol T%i, F=%.0f, D=%.6f, N=%.0f\n", NeedsPTS?"happened":"needed", protocol_type, (double) atr_f_table[FI], d, n);
572 }
573 }//end negotiable mode
574
575 //make sure no zero values
576 double F = (double) atr_f_table[FI];
577 if (!F) {
578 FI = ATR_DEFAULT_FI;
579 cs_log("Warning: F=0 is invalid, forcing FI=%d", FI);
580 }
581 if (!d) {
582 d = ATR_DEFAULT_D;
583 cs_log("Warning: D=0 is invalid, forcing D=%.0f",d);
584 }
585
586 if (deprecated == 0)
587 return InitCard (atr, FI, d, n, deprecated);
588 else
589 return InitCard (atr, ATR_DEFAULT_FI, ATR_DEFAULT_D, n, deprecated);
590}
591
592static int PPS_Exchange (BYTE * params, unsigned *length)
593{
594 BYTE confirm[PPS_MAX_LENGTH];
595 unsigned len_request, len_confirm;
596 int ret;
597
598 len_request = PPS_GetLength (params);
599 params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
600 cs_debug_mask (D_IFD,"PTS: Sending request: %s", cs_hexdump(1, params, len_request));
601
602 /* Send PPS request */
603 call (ICC_Async_Transmit (len_request, params));
604
605 /* Get PPS confirm */
606 call (ICC_Async_Receive (2, confirm));
607 len_confirm = PPS_GetLength (confirm);
608 call (ICC_Async_Receive (len_confirm - 2, confirm + 2));
609
610 cs_debug_mask(D_IFD, "PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
611 if ((len_request != len_confirm) || (memcmp (params, confirm, len_request)))
612 ret = ERROR;
613 else
614 ret = OK;
615
616 /* Copy PPS handsake */
617 memcpy (params, confirm, len_confirm);
618 (*length) = len_confirm;
619 return ret;
620}
621
622static unsigned PPS_GetLength (BYTE * block)
623{
624 unsigned length = 3;
625
626 if (PPS_HAS_PPS1 (block))
627 length++;
628
629 if (PPS_HAS_PPS2 (block))
630 length++;
631
632 if (PPS_HAS_PPS3 (block))
633 length++;
634
635 return length;
636}
637
638static unsigned int ETU_to_ms(unsigned long WWT)
639{
640#define CHAR_LEN 10L //character length in ETU, perhaps should be 9 when parity = none?
641 if (WWT > CHAR_LEN)
642 WWT -= CHAR_LEN;
643 else
644 WWT = 0;
645 double work_etu = 1000 / (double)current_baudrate;//FIXME sometimes work_etu should be used, sometimes initial etu
646 return (unsigned int) WWT * work_etu * reader[ridx].cardmhz / reader[ridx].mhz;
647}
648
649static int ICC_Async_SetParity (unsigned short parity)
650{
651 switch(reader[ridx].typ) {
652 case R_DB2COM1:
653 case R_DB2COM2:
654 case R_MOUSE:
655 call (IO_Serial_SetParity (parity));
656 break;
657#if defined(LIBUSB)
658 case R_SMART:
659 reader[ridx].sr_config.inv= (convention == ATR_CONVENTION_INVERSE) ? 1: 0;
660 reader[ridx].sr_config.parity=parity;
661 call (SR_SetParity(&reader[ridx]));
662 break;
663#endif
664 case R_INTERNAL:
665 return OK;
666 default:
667 cs_log("ERROR ICC_Async_SetParity: unknow reader type %i",reader[ridx].typ);
668 return ERROR;
669 }
670 return OK;
671}
672
673static int SetRightParity (void)
674{
675 //set right parity
676 unsigned short parity = PARITY_EVEN;
677 if (convention == ATR_CONVENTION_INVERSE)
678 parity = PARITY_ODD;
679 else if(protocol_type == ATR_PROTOCOL_TYPE_T14)
680 parity = PARITY_NONE;
681
682 call (ICC_Async_SetParity(parity));
683
684#ifdef COOL
685 if (reader[ridx].typ != R_INTERNAL)
686#endif
687#if defined(LIBUSB)
688 if (reader[ridx].typ != R_SMART)
689#endif
690 IO_Serial_Flush();
691 return OK;
692}
693
694static int InitCard (ATR * atr, BYTE FI, double d, double n, unsigned short deprecated)
695{
696 double P,I;
697 double F;
698 unsigned long BGT, edc, EGT, CGT, WWT = 0;
699 unsigned int GT;
700 unsigned long gt_ms;
701 current_baudrate = DEFAULT_BAUDRATE;
702
703 //set the amps and the volts according to ATR
704 if (ATR_GetParameter(atr, ATR_PARAMETER_P, &P) != ATR_OK)
705 P = 0;
706 if (ATR_GetParameter(atr, ATR_PARAMETER_I, &I) != ATR_OK)
707 I = 0;
708
709 //set clock speed to max if internal reader
710 if(reader[ridx].typ > R_MOUSE)
711 if (reader[ridx].mhz == 357 || reader[ridx].mhz == 358) //no overclocking
712 reader[ridx].mhz = atr_fs_table[FI] / 10000; //we are going to clock the card to this nominal frequency
713
714 //set clock speed/baudrate must be done before timings
715 //because current_baudrate is used in calculation of timings
716 F = (double) atr_f_table[FI];
717
718 if (deprecated == 0)
719 if (protocol_type != ATR_PROTOCOL_TYPE_T14) { //dont switch for T14
720 unsigned long baud_temp = d * ICC_Async_GetClockRate () / F;
721 if (reader[ridx].typ <= R_MOUSE)
722 call (Phoenix_SetBaudrate (baud_temp));
723 cs_debug_mask(D_IFD, "Setting baudrate to %lu", baud_temp);
724 current_baudrate = baud_temp; //this is needed for all readers to calculate work_etu for timings
725 }
726
727 //set timings according to ATR
728 read_timeout = 0;
729 icc_timings.block_delay = 0;
730 icc_timings.char_delay = 0;
731
732 if (n == 255) //Extra Guard Time
733 EGT = 0;
734 else
735 EGT = n;
736 GT = EGT + 12; //Guard Time in ETU
737 gt_ms = ETU_to_ms(GT);
738
739 switch (protocol_type) {
740 case ATR_PROTOCOL_TYPE_T0:
741 case ATR_PROTOCOL_TYPE_T14:
742 {
743 BYTE wi;
744 /* Integer value WI = TC2, by default 10 */
745#ifndef PROTOCOL_T0_USE_DEFAULT_TIMINGS
746 if (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TC, &(wi)) != ATR_OK)
747#endif
748 wi = DEFAULT_WI;
749
750 // WWT = 960 * WI * (Fi / f) * 1000 milliseconds
751 WWT = (unsigned long) 960 * wi; //in ETU
752 if (protocol_type == ATR_PROTOCOL_TYPE_T14)
753 WWT >>= 1; //is this correct?
754
755 read_timeout = ETU_to_ms(WWT);
756 icc_timings.block_delay = gt_ms;
757 icc_timings.char_delay = gt_ms;
758 cs_debug("Setting timings: timeout=%u ms, block_delay=%u ms, char_delay=%u ms", read_timeout, icc_timings.block_delay, icc_timings.char_delay);
759 cs_debug_mask (D_IFD,"Protocol: T=%i: WWT=%d, Clockrate=%lu\n", protocol_type, (int)(WWT), ICC_Async_GetClockRate());
760 }
761 break;
762 case ATR_PROTOCOL_TYPE_T1:
763 {
764 BYTE ta, tb, tc, cwi, bwi;
765
766 // Set IFSC
767 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TA, &ta) == ATR_NOT_FOUND)
768 ifsc = DEFAULT_IFSC;
769 else if ((ta != 0x00) && (ta != 0xFF))
770 ifsc = ta;
771 else
772 ifsc = DEFAULT_IFSC;
773
774 // Towitoko does not allow IFSC > 251 //FIXME not sure whether this limitation still exists
775 ifsc = MIN (ifsc, MAX_IFSC);
776
777 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
778 // Calculate CWI and BWI
779 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND)
780 {
781 #endif
782 cwi = DEFAULT_CWI;
783 bwi = DEFAULT_BWI;
784 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
785 }
786 else
787 {
788 cwi = tb & 0x0F;
789 bwi = tb >> 4;
790 }
791 #endif
792
793 // Set CWT = (2^CWI + 11) work etu
794 CWT = (unsigned short) (((1<<cwi) + 11)); // in ETU
795
796 // Set BWT = (2^BWI * 960 + 11) work etu
797 BWT = (unsigned short)((1<<bwi) * 960 * 372 * 9600 / ICC_Async_GetClockRate() ) + 11 ;
798
799 // Set BGT = 22 * work etu
800 BGT = 22L; //in ETU
801
802 if (n == 255)
803 CGT = 11L; //in ETU
804 else
805 CGT = GT;
806
807 // Set the error detection code type
808 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
809 edc = EDC_LRC;
810 else
811 edc = tc & 0x01;
812
813 // Set initial send sequence (NS)
814 ns = 1;
815
816 cs_debug ("Protocol: T=1: IFSC=%d, CWT=%d etu, BWT=%d etu, BGT=%d etu, EDC=%s\n", ifsc, CWT, BWT, BGT, (edc == EDC_LRC) ? "LRC" : "CRC");
817
818 read_timeout = ETU_to_ms(BWT);
819 icc_timings.block_delay = ETU_to_ms(BGT);
820 icc_timings.char_delay = ETU_to_ms(CGT);
821 cs_debug("Setting timings: timeout=%u ms, block_delay=%u ms, char_delay=%u ms", read_timeout, icc_timings.block_delay, icc_timings.char_delay);
822 }
823 break;
824 default:
825 return ERROR;
826 break;
827 }//switch
828
829 call (SetRightParity ());
830
831 //write settings to internal device
832 if(reader[ridx].typ == R_INTERNAL) {
833#ifdef SCI_DEV
834 double F = (double) atr_f_table[FI];
835 unsigned long ETU = 0;
836 //for Irdeto T14 cards, do not set ETU
837 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && protocol_type == ATR_PROTOCOL_TYPE_T14))
838 ETU = F / d;
839 call (Sci_WriteSettings (protocol_type, reader[ridx].mhz / 100, ETU, WWT, BWT, CWT, EGT, (unsigned char)P, (unsigned char)I));
840#elif COOL
841 call (Cool_SetClockrate(reader[ridx].mhz));
842 call (Cool_WriteSettings (BWT, CWT, EGT, BGT));
843#endif //COOL
844 }
845#if defined(LIBUSB)
846 if (reader[ridx].typ == R_SMART)
847 SR_WriteSettings(&reader[ridx], (unsigned short) atr_f_table[FI], (BYTE)d, (BYTE)EGT, (BYTE)protocol_type);
848#endif
849 cs_log("Maximum frequency for this card is formally %i Mhz, clocking it to %.2f Mhz", atr_fs_table[FI] / 1000000, (float) reader[ridx].mhz / 100);
850
851 //IFS setting in case of T1
852 if ((protocol_type == ATR_PROTOCOL_TYPE_T1) && (ifsc != DEFAULT_IFSC)) {
853 APDU_Rsp ** rsp;
854 unsigned char tmp[] = { 0x21, 0xC1, 0x01, 0x00, 0x00 };
855 tmp[3] = ifsc; // Information Field size
856 tmp[4] = ifsc ^ 0xE1;
857 Protocol_T1_Command (tmp, sizeof(tmp), rsp);
858 }
859 return OK;
860}
861
862static BYTE PPS_GetPCK (BYTE * block, unsigned length)
863{
864 BYTE pck;
865 unsigned i;
866
867 pck = block[0];
868 for (i = 1; i < length; i++)
869 pck ^= block[i];
870
871 return pck;
872}
Note: See TracBrowser for help on using the repository browser.