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