source: trunk/csctapi/icc_async.c@ 1973

Last change on this file since 1973 was 1973, checked in by merek, 10 years ago

Fix strange logging messages on QBOXHD

File size: 25.0 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#ifndef QBOXHD
214 //FIXME: This causes strange card/no card messages on QBOXHD
215 cs_debug_mask (D_IFD, "IFD: Status = %s", in ? "card": "no card");
216#endif
217
218 return OK;
219}
220
221int ICC_Async_Activate (struct s_reader *reader, ATR * atr, unsigned short deprecated)
222{
223 cs_debug_mask (D_IFD, "IFD: Activating card in reader %s\n", reader->label);
224
225 reader->current_baudrate = DEFAULT_BAUDRATE; //this is needed for all readers to calculate work_etu for timings
226
227 if (reader->atr[0] != 0) {
228 cs_log("using ATR from reader config");
229 ATR_InitFromArray(atr, reader->atr, 33);
230 }
231 else {
232 switch(reader->typ) {
233 case R_DB2COM1:
234 case R_DB2COM2:
235 case R_SC8in1:
236 case R_MOUSE:
237 call (Phoenix_Reset(reader, atr));
238 break;
239#if defined(LIBUSB)
240 case R_SMART:
241 call (SR_Reset(reader, atr));
242 break;
243#endif
244 case R_INTERNAL:
245#ifdef SCI_DEV
246 call (Sci_Activate(reader));
247 call (Sci_Reset(reader, atr));
248#elif COOL
249 call (Cool_Reset(atr));
250#endif
251 break;
252 default:
253 cs_log("ERROR ICC_Async_Activate: unknow reader type %i",reader->typ);
254 return ERROR;
255 }
256 }
257
258 unsigned char atrarr[64];
259 unsigned int atr_size;
260 ATR_GetRaw(atr, atrarr, &atr_size);
261 cs_ri_log(reader, "ATR: %s", cs_hexdump(1, atrarr, atr_size));
262
263
264 /* Get ICC reader->convention */
265 if (ATR_GetConvention (atr, &(reader->convention)) != ATR_OK) {
266 cs_log("ERROR: Could not read reader->convention");
267 reader->convention = 0;
268 reader->protocol_type = 0;
269 return ERROR;
270 }
271
272 reader->protocol_type = ATR_PROTOCOL_TYPE_T0;
273
274 unsigned short cs_ptyp_orig=cs_ptyp;
275 cs_ptyp=D_ATR;
276 int ret = Parse_ATR(reader, atr, deprecated);
277 if (ret)
278 cs_log("ERROR: Parse_ATR returned error");
279 cs_ptyp=cs_ptyp_orig;
280 if (ret)
281 return ERROR;
282 cs_debug_mask (D_IFD, "IFD: Card in reader %s succesfully activated\n", reader->label);
283 return OK;
284}
285
286int ICC_Async_CardWrite (struct s_reader *reader, unsigned char *command, unsigned short command_len, unsigned char *rsp, unsigned short *lr)
287{
288 *lr = 0; //will be returned in case of error
289 switch (reader->protocol_type) {
290 case ATR_PROTOCOL_TYPE_T0:
291 call (Protocol_T0_Command (reader, command, command_len, rsp, lr));
292 break;
293 case ATR_PROTOCOL_TYPE_T1:
294 {
295 int try = 1;
296 do {
297 if (Protocol_T1_Command (reader, command, command_len, rsp, lr) == OK)
298 break;
299 try++;
300 //try to resync
301 unsigned char resync[] = { 0x21, 0xC0, 0x00, 0xE1 };
302 Protocol_T1_Command (reader, resync, sizeof(resync), rsp, lr);
303 ifsc = DEFAULT_IFSC;
304 } while (try <= 3);
305 break;
306 }
307 case ATR_PROTOCOL_TYPE_T14:
308 call (Protocol_T14_ExchangeTPDU (reader, command, command_len, rsp, lr));
309 break;
310 default:
311 cs_log("Error, unknown protocol type %i",reader->protocol_type);
312 return ERROR;
313 }
314 return OK;
315}
316
317int ICC_Async_SetTimings (struct s_reader * reader, unsigned wait_etu)
318{
319 reader->read_timeout = ETU_to_ms(reader, wait_etu);
320 cs_debug_mask(D_IFD, "Setting timeout to %i", wait_etu);
321 return OK;
322}
323
324int ICC_Async_Transmit (struct s_reader *reader, unsigned size, BYTE * data)
325{
326 cs_ddump_mask(D_IFD, data, size, "IFD Transmit: ");
327 BYTE *buffer = NULL, *sent;
328
329 if (reader->convention == ATR_CONVENTION_INVERSE && reader->typ <= R_MOUSE) {
330 buffer = (BYTE *) calloc(sizeof (BYTE), size);
331 memcpy (buffer, data, size);
332 ICC_Async_InvertBuffer (size, buffer);
333 sent = buffer;
334 }
335 else
336 sent = data;
337
338 switch(reader->typ) {
339 case R_DB2COM1:
340 case R_DB2COM2:
341 case R_SC8in1:
342 case R_MOUSE:
343 call (Phoenix_Transmit (reader, sent, size, reader->block_delay, reader->char_delay));
344 break;
345#if defined(LIBUSB)
346 case R_SMART:
347 call (SR_Transmit(reader, 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 (reader, 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->typ);
359 return ERROR;
360 }
361
362 if (reader->convention == ATR_CONVENTION_INVERSE && reader->typ <= R_MOUSE)
363 free (buffer);
364 cs_debug_mask(D_IFD, "IFD Transmit succesful");
365 return OK;
366}
367
368int ICC_Async_Receive (struct s_reader *reader, unsigned size, BYTE * data)
369{
370 switch(reader->typ) {
371 case R_DB2COM1:
372 case R_DB2COM2:
373 case R_SC8in1:
374 case R_MOUSE:
375 call (Phoenix_Receive (reader, data, size, reader->read_timeout));
376 break;
377#if defined(LIBUSB)
378 case R_SMART:
379 call (SR_Receive(reader, data, size));
380 break;
381#endif
382 case R_INTERNAL:
383#ifdef COOL
384 call (Cool_Receive(data, size));
385#elif SCI_DEV
386 call (Phoenix_Receive (reader, data, size, reader->read_timeout));
387#endif
388 break;
389 default:
390 cs_log("ERROR ICC_Async_Receive: unknow reader type %i",reader->typ);
391 return ERROR;
392 }
393
394 if (reader->convention == ATR_CONVENTION_INVERSE && reader->typ <= R_MOUSE)
395 ICC_Async_InvertBuffer (size, data);
396
397 cs_ddump_mask(D_IFD, data, size, "IFD Received: ");
398 return OK;
399}
400
401int ICC_Async_Close (struct s_reader *reader)
402{ //FIXME this routine is never called!
403 cs_debug_mask (D_IFD, "IFD: Closing device %s", reader->device);
404
405 switch(reader->typ) {
406 case R_DB2COM1:
407 case R_DB2COM2:
408 case R_MOUSE:
409 call (Phoenix_Close(reader));
410 break;
411#if defined(LIBUSB)
412 case R_SMART:
413 call (SR_Close(reader));
414 break;
415#endif
416 case R_INTERNAL:
417#ifdef SCI_DEV
418 /* Dectivate ICC */
419 call (Sci_Deactivate(reader));
420 call (Phoenix_Close(reader));
421#endif
422 break;
423 default:
424 cs_log("ERROR ICC_Async_Close: unknow reader type %i",reader->typ);
425 return ERROR;
426 }
427
428 cs_debug_mask (D_IFD, "IFD: Device %s succesfully closed", reader->device);
429 return OK;
430}
431
432static unsigned long ICC_Async_GetClockRate (int cardmhz)
433{
434 switch (cardmhz) {
435 case 357:
436 case 358:
437 return (372L * 9600L);
438 case 368:
439 return (384L * 9600L);
440 default:
441 return cardmhz * 10000L;
442 }
443}
444
445static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
446{
447 uint i;
448
449 for (i = 0; i < size; i++)
450 buffer[i] = ~(INVERT_BYTE (buffer[i]));
451}
452
453static int Parse_ATR (struct s_reader * reader, ATR * atr, unsigned short deprecated)
454{
455 BYTE FI = ATR_DEFAULT_FI;
456 //BYTE t = ATR_PROTOCOL_TYPE_T0;
457 double d = ATR_DEFAULT_D;
458 double n = ATR_DEFAULT_N;
459 int ret;
460
461 int numprot = atr->pn;
462 //if there is a trailing TD, this number is one too high
463 BYTE tx;
464 if (ATR_GetInterfaceByte (atr, numprot-1, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK)
465 if ((tx & 0xF0) == 0)
466 numprot--;
467 int i,point;
468 char txt[50];
469 bool OffersT[3]; //T14 stored as T2
470 for (i = 0; i <= 2; i++)
471 OffersT[i] = FALSE;
472 for (i=1; i<= numprot; i++) {
473 point = 0;
474 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TA, &tx) == ATR_OK) {
475 sprintf((char *)txt+point,"TA%i=%02X ",i,tx);
476 point +=7;
477 }
478 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TB, &tx) == ATR_OK) {
479 sprintf((char *)txt+point,"TB%i=%02X ",i,tx);
480 point +=7;
481 }
482 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TC, &tx) == ATR_OK) {
483 sprintf((char *)txt+point,"TC%i=%02X ",i,tx);
484 point +=7;
485 }
486 if (ATR_GetInterfaceByte (atr, i, ATR_INTERFACE_BYTE_TD, &tx) == ATR_OK) {
487 sprintf((char *)txt+point,"TD%i=%02X ",i,tx);
488 point +=7;
489 tx &= 0X0F;
490 sprintf((char *)txt+point,"(T%i)",tx);
491 if (tx == 14)
492 OffersT[2] = TRUE;
493 else
494 OffersT[tx] = TRUE;
495 }
496 else {
497 sprintf((char *)txt+point,"no TD%i means T0",i);
498 OffersT[0] = TRUE;
499 }
500 cs_debug("%s",txt);
501 }
502
503 int numprottype = 0;
504 for (i = 0; i <= 2; i++)
505 if (OffersT[i])
506 numprottype ++;
507 cs_debug("%i protocol types detected. Historical bytes: %s",numprottype, cs_hexdump(1,atr->hb,atr->hbn));
508
509 ATR_GetParameter (atr, ATR_PARAMETER_N, &(n));
510 ATR_GetProtocolType(atr,1,&(reader->protocol_type)); //get protocol from TD1
511 BYTE TA2;
512 bool SpecificMode = (ATR_GetInterfaceByte (atr, 2, ATR_INTERFACE_BYTE_TA, &TA2) == ATR_OK); //if TA2 present, specific mode, else negotiable mode
513 if (SpecificMode) {
514 reader->protocol_type = TA2 & 0x0F;
515 if ((TA2 & 0x10) != 0x10) { //bit 5 set to 0 means F and D explicitly defined in interface characters
516 BYTE TA1;
517 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
518 FI = TA1 >> 4;
519 ATR_GetParameter (atr, ATR_PARAMETER_D, &(d));
520 }
521 else {
522 FI = ATR_DEFAULT_FI;
523 d = ATR_DEFAULT_D;
524 }
525 }
526 else {
527 cs_log("Specific mode: speed 'implicitly defined', not sure how to proceed, assuming default values");
528 FI = ATR_DEFAULT_FI;
529 d = ATR_DEFAULT_D;
530 }
531 cs_debug("Specific mode: T%i, F=%.0f, D=%.6f, N=%.0f\n", reader->protocol_type, (double) atr_f_table[FI], d, n);
532 }
533 else { //negotiable mode
534
535 bool PPS_success = FALSE;
536 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
537 if (NeedsPTS && deprecated == 0) {
538 // PTSS PTS0 PTS1 PCK
539 BYTE req[] = { 0xFF, 0x10, 0x00, 0x00 }; //we currently do not support PTS2, standard guardtimes
540 req[1]=0x10 | reader->protocol_type; //PTS0 always flags PTS1 to be sent always
541 if (ATR_GetInterfaceByte (atr, 1, ATR_INTERFACE_BYTE_TA, &req[2]) != ATR_OK) //PTS1
542 req[2] = 0x11; //defaults FI and DI to 1
543 unsigned int len = sizeof(req);
544 ret = PPS_Exchange (reader, req, &len);
545 if (ret == OK) {
546 FI = req[2] >> 4;
547 BYTE DI = req[2] & 0x0F;
548 d = (double) (atr_d_table[DI]);
549 PPS_success = TRUE;
550 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);
551 }
552 else
553 cs_ddump(req,4,"PTS Failure, response:");
554 }
555
556 //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)
557 if (!PPS_success) {//last PPS not succesfull
558 BYTE TA1;
559 if (ATR_GetInterfaceByte (atr, 1 , ATR_INTERFACE_BYTE_TA, &TA1) == ATR_OK) {
560 FI = TA1 >> 4;
561 ATR_GetParameter (atr, ATR_PARAMETER_D, &(d));
562 }
563 else { //do not obey TA1
564 FI = ATR_DEFAULT_FI;
565 d = ATR_DEFAULT_D;
566 }
567 if (NeedsPTS) {
568 if ((d == 32) || (d == 12) || (d == 20)) //those values were RFU in old table
569 d = 0; // viaccess cards that fail PTS need this
570 }
571
572 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);
573 }
574 }//end negotiable mode
575
576 //make sure no zero values
577 double F = (double) atr_f_table[FI];
578 if (!F) {
579 FI = ATR_DEFAULT_FI;
580 cs_log("Warning: F=0 is invalid, forcing FI=%d", FI);
581 }
582 if (!d) {
583 d = ATR_DEFAULT_D;
584 cs_log("Warning: D=0 is invalid, forcing D=%.0f",d);
585 }
586
587 if (deprecated == 0)
588 return InitCard (reader, atr, FI, d, n, deprecated);
589 else
590 return InitCard (reader, atr, ATR_DEFAULT_FI, ATR_DEFAULT_D, n, deprecated);
591}
592
593static int PPS_Exchange (struct s_reader * reader, BYTE * params, unsigned *length)
594{
595 BYTE confirm[PPS_MAX_LENGTH];
596 unsigned len_request, len_confirm;
597 int ret;
598
599 len_request = PPS_GetLength (params);
600 params[len_request - 1] = PPS_GetPCK(params, len_request - 1);
601 cs_debug_mask (D_IFD,"PTS: Sending request: %s", cs_hexdump(1, params, len_request));
602
603 /* Send PPS request */
604 call (ICC_Async_Transmit (reader, len_request, params));
605
606 /* Get PPS confirm */
607 call (ICC_Async_Receive (reader, 2, confirm));
608 len_confirm = PPS_GetLength (confirm);
609 call (ICC_Async_Receive (reader, len_confirm - 2, confirm + 2));
610
611 cs_debug_mask(D_IFD, "PTS: Receiving confirm: %s", cs_hexdump(1, confirm, len_confirm));
612 if ((len_request != len_confirm) || (memcmp (params, confirm, len_request)))
613 ret = ERROR;
614 else
615 ret = OK;
616
617 /* Copy PPS handsake */
618 memcpy (params, confirm, len_confirm);
619 (*length) = len_confirm;
620 return ret;
621}
622
623static unsigned PPS_GetLength (BYTE * block)
624{
625 unsigned length = 3;
626
627 if (PPS_HAS_PPS1 (block))
628 length++;
629
630 if (PPS_HAS_PPS2 (block))
631 length++;
632
633 if (PPS_HAS_PPS3 (block))
634 length++;
635
636 return length;
637}
638
639static unsigned int ETU_to_ms(struct s_reader * reader, unsigned long WWT)
640{
641#define CHAR_LEN 10L //character length in ETU, perhaps should be 9 when parity = none?
642 if (WWT > CHAR_LEN)
643 WWT -= CHAR_LEN;
644 else
645 WWT = 0;
646 double work_etu = 1000 / (double)reader->current_baudrate;//FIXME sometimes work_etu should be used, sometimes initial etu
647 return (unsigned int) WWT * work_etu * reader->cardmhz / reader->mhz;
648}
649
650static int ICC_Async_SetParity (struct s_reader * reader, unsigned short parity)
651{
652 switch(reader->typ) {
653 case R_DB2COM1:
654 case R_DB2COM2:
655 case R_SC8in1:
656 case R_MOUSE:
657 call (IO_Serial_SetParity (reader, parity));
658 break;
659#if defined(LIBUSB)
660 case R_SMART:
661 call (SR_SetParity(reader, parity));
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->typ);
668 return ERROR;
669 }
670 return OK;
671}
672
673static int SetRightParity (struct s_reader * reader)
674{
675 //set right parity
676 unsigned short parity = PARITY_EVEN;
677 if (reader->convention == ATR_CONVENTION_INVERSE)
678 parity = PARITY_ODD;
679 else if(reader->protocol_type == ATR_PROTOCOL_TYPE_T14)
680 parity = PARITY_NONE;
681
682 call (ICC_Async_SetParity(reader, parity));
683
684#ifdef COOL
685 if (reader->typ != R_INTERNAL)
686#endif
687#if defined(LIBUSB)
688 if (reader->typ != R_SMART)
689#endif
690 IO_Serial_Flush(reader);
691 return OK;
692}
693
694static int InitCard (struct s_reader * reader, 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 reader->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->typ > R_MOUSE)
711 if (reader->mhz == 357 || reader->mhz == 358) //no overclocking
712 reader->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 reader->current_baudrate is used in calculation of timings
716 F = (double) atr_f_table[FI];
717
718 if (deprecated == 0)
719 if (reader->protocol_type != ATR_PROTOCOL_TYPE_T14) { //dont switch for T14
720 unsigned long baud_temp = d * ICC_Async_GetClockRate (reader->cardmhz) / F;
721 if (reader->typ <= R_MOUSE)
722 call (Phoenix_SetBaudrate (reader, baud_temp));
723 cs_debug_mask(D_IFD, "Setting baudrate to %lu", baud_temp);
724 reader->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 reader->read_timeout = 0;
729 reader->block_delay = 0;
730 reader->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(reader, GT);
738
739 switch (reader->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 (reader->protocol_type == ATR_PROTOCOL_TYPE_T14)
753 WWT >>= 1; //is this correct?
754
755 reader->read_timeout = ETU_to_ms(reader, WWT);
756 reader->block_delay = gt_ms;
757 reader->char_delay = gt_ms;
758 cs_debug("Setting timings: timeout=%u ms, block_delay=%u ms, char_delay=%u ms", reader->read_timeout, reader->block_delay, reader->char_delay);
759 cs_debug_mask (D_IFD,"Protocol: T=%i: WWT=%d, Clockrate=%lu\n", reader->protocol_type, (int)(WWT), ICC_Async_GetClockRate(reader->cardmhz));
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 //FIXME workaround for Smargo until native mode works
775 if (reader->smargopatch == 1)
776 ifsc = MIN (ifsc, 28);
777 else
778 // Towitoko does not allow IFSC > 251
779 //FIXME not sure whether this limitation still exists
780 ifsc = MIN (ifsc, MAX_IFSC);
781
782 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
783 // Calculate CWI and BWI
784 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TB, &tb) == ATR_NOT_FOUND)
785 {
786 #endif
787 cwi = DEFAULT_CWI;
788 bwi = DEFAULT_BWI;
789 #ifndef PROTOCOL_T1_USE_DEFAULT_TIMINGS
790 }
791 else
792 {
793 cwi = tb & 0x0F;
794 bwi = tb >> 4;
795 }
796 #endif
797
798 // Set CWT = (2^CWI + 11) work etu
799 reader->CWT = (unsigned short) (((1<<cwi) + 11)); // in ETU
800
801 // Set BWT = (2^BWI * 960 + 11) work etu
802 reader->BWT = (unsigned short)((1<<bwi) * 960 * 372 * 9600 / ICC_Async_GetClockRate(reader->cardmhz)) + 11 ;
803
804 // Set BGT = 22 * work etu
805 BGT = 22L; //in ETU
806
807 if (n == 255)
808 CGT = 11L; //in ETU
809 else
810 CGT = GT;
811
812 // Set the error detection code type
813 if (ATR_GetInterfaceByte (atr, 3, ATR_INTERFACE_BYTE_TC, &tc) == ATR_NOT_FOUND)
814 edc = EDC_LRC;
815 else
816 edc = tc & 0x01;
817
818 // Set initial send sequence (NS)
819 ns = 1;
820
821 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");
822
823 reader->read_timeout = ETU_to_ms(reader, reader->BWT);
824 reader->block_delay = ETU_to_ms(reader, BGT);
825 reader->char_delay = ETU_to_ms(reader, CGT);
826 cs_debug("Setting timings: timeout=%u ms, block_delay=%u ms, char_delay=%u ms", reader->read_timeout, reader->block_delay, reader->char_delay);
827 }
828 break;
829 default:
830 return ERROR;
831 break;
832 }//switch
833
834 call (SetRightParity (reader));
835
836 //write settings to internal device
837 if(reader->typ == R_INTERNAL) {
838#ifdef SCI_DEV
839 double F = (double) atr_f_table[FI];
840 unsigned long ETU = 0;
841 //for Irdeto T14 cards, do not set ETU
842 if (!(atr->hbn >= 6 && !memcmp(atr->hb, "IRDETO", 6) && reader->protocol_type == ATR_PROTOCOL_TYPE_T14))
843 ETU = F / d;
844 call (Sci_WriteSettings (reader, reader->protocol_type, reader->mhz / 100, ETU, WWT, reader->BWT, reader->CWT, EGT, (unsigned char)P, (unsigned char)I));
845#elif COOL
846 call (Cool_SetClockrate(reader->mhz));
847 call (Cool_WriteSettings (reader->BWT, reader->CWT, EGT, BGT));
848#endif //COOL
849 }
850#if defined(LIBUSB)
851 if (reader->typ == R_SMART)
852 SR_WriteSettings(reader, (unsigned short) atr_f_table[FI], (BYTE)d, (BYTE)EGT, (BYTE)reader->protocol_type, reader->convention);
853#endif
854 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);
855
856 //IFS setting in case of T1
857 if ((reader->protocol_type == ATR_PROTOCOL_TYPE_T1) && (ifsc != DEFAULT_IFSC)) {
858 unsigned char rsp[CTA_RES_LEN];
859 unsigned short * lr;
860 unsigned char tmp[] = { 0x21, 0xC1, 0x01, 0x00, 0x00 };
861 tmp[3] = ifsc; // Information Field size
862 tmp[4] = ifsc ^ 0xE1;
863 Protocol_T1_Command (reader, tmp, sizeof(tmp), rsp, lr);
864 }
865 return OK;
866}
867
868static BYTE PPS_GetPCK (BYTE * block, unsigned length)
869{
870 BYTE pck;
871 unsigned i;
872
873 pck = block[0];
874 for (i = 1; i < length; i++)
875 pck ^= block[i];
876
877 return pck;
878}
Note: See TracBrowser for help on using the repository browser.