source: trunk/csctapi/icc_async.c@ 3181

Last change on this file since 3181 was 3181, checked in by dingo35, 10 years ago

Adding threadsafety FIXMEs, feel free to join checking..

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