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