source: trunk/csctapi/icc_async.c@ 1969

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