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 "defines.h"
|
---|
26 | #include "../globals.h"
|
---|
27 | #include "icc_async.h"
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <stdio.h>
|
---|
30 | #include <string.h>
|
---|
31 | #include "ifd.h"
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * Not exported constants definition
|
---|
35 | */
|
---|
36 | #define ICC_ASYNC_MAX_TRANSMIT 255
|
---|
37 | #define ICC_ASYNC_BAUDRATE 9600
|
---|
38 |
|
---|
39 | /*
|
---|
40 | * Not exported functions declaration
|
---|
41 | */
|
---|
42 |
|
---|
43 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
|
---|
44 | static void ICC_Async_Clear (ICC_Async * icc);
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Exported functions definition
|
---|
48 | */
|
---|
49 |
|
---|
50 | ICC_Async *ICC_Async_New (void)
|
---|
51 | {
|
---|
52 | ICC_Async *icc;
|
---|
53 |
|
---|
54 | /* Allocate memory */
|
---|
55 | icc = (ICC_Async *) malloc (sizeof (ICC_Async));
|
---|
56 |
|
---|
57 | if (icc != NULL)
|
---|
58 | ICC_Async_Clear (icc);
|
---|
59 |
|
---|
60 | return icc;
|
---|
61 | }
|
---|
62 |
|
---|
63 | int ICC_Async_Init (ICC_Async * icc, IFD * ifd)
|
---|
64 | {
|
---|
65 | #ifndef ICC_TYPE_SYNC
|
---|
66 | unsigned np=0;
|
---|
67 |
|
---|
68 | /* LED Red */
|
---|
69 | if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
|
---|
70 | return ICC_ASYNC_IFD_ERROR;
|
---|
71 |
|
---|
72 | /* Initialize Baudrate */
|
---|
73 | if (IFD_Towitoko_SetBaudrate (ifd, ICC_ASYNC_BAUDRATE)!= IFD_TOWITOKO_OK)
|
---|
74 | return ICC_ASYNC_IFD_ERROR;
|
---|
75 |
|
---|
76 | /* Activate ICC */
|
---|
77 | if (IFD_Towitoko_ActivateICC (ifd) != IFD_TOWITOKO_OK)
|
---|
78 | return ICC_ASYNC_IFD_ERROR;
|
---|
79 | /* Reset ICC */
|
---|
80 | #ifdef COOL
|
---|
81 | if (ifd->io->com == RTYP_SCI) {
|
---|
82 | if (!Cool_Reset(&(icc->atr)))
|
---|
83 | {
|
---|
84 | icc->atr = NULL;
|
---|
85 | return ICC_ASYNC_IFD_ERROR;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | else
|
---|
89 | #endif
|
---|
90 | if (IFD_Towitoko_ResetAsyncICC (ifd, &(icc->atr)) != IFD_TOWITOKO_OK)
|
---|
91 | {
|
---|
92 | icc->atr = NULL;
|
---|
93 | return ICC_ASYNC_IFD_ERROR;
|
---|
94 | }
|
---|
95 | /* Get ICC convention */
|
---|
96 | if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
|
---|
97 | {
|
---|
98 | ATR_Delete (icc->atr);
|
---|
99 | icc->atr = NULL;
|
---|
100 | icc->convention = 0;
|
---|
101 |
|
---|
102 | return ICC_ASYNC_ATR_ERROR;
|
---|
103 | }
|
---|
104 |
|
---|
105 | icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
|
---|
106 |
|
---|
107 | ATR_GetNumberOfProtocols (icc->atr, &np);
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Get protocol offered by interface bytes T*2 if available,
|
---|
111 | * (that is, if TD1 is available), * otherwise use default T=0
|
---|
112 | */
|
---|
113 | /* if (np>1)
|
---|
114 | ATR_GetProtocolType (icc->atr, 1, &(icc->protocol_type));
|
---|
115 |
|
---|
116 | #ifdef DEBUG_ICC
|
---|
117 | printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
|
---|
118 | #endif
|
---|
119 | *///really should let PPS handle this
|
---|
120 | /* LED Green */
|
---|
121 | if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
|
---|
122 | {
|
---|
123 | ATR_Delete (icc->atr);
|
---|
124 | icc->atr = NULL;
|
---|
125 | icc->convention = 0;
|
---|
126 |
|
---|
127 | return ICC_ASYNC_IFD_ERROR;
|
---|
128 | }
|
---|
129 |
|
---|
130 | /* Initialize member variables */
|
---|
131 | icc->baudrate = ICC_ASYNC_BAUDRATE;
|
---|
132 | icc->ifd = ifd;
|
---|
133 |
|
---|
134 | #ifdef NO_PAR_SWITCH
|
---|
135 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
136 | {
|
---|
137 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
|
---|
138 | return ICC_ASYNC_IFD_ERROR;
|
---|
139 | }
|
---|
140 | else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
141 | {
|
---|
142 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
143 | return ICC_ASYNC_IFD_ERROR;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
|
---|
148 | return ICC_ASYNC_IFD_ERROR;
|
---|
149 | }
|
---|
150 | #ifdef COOL
|
---|
151 | if (icc->ifd->io->com != RTYP_SCI)
|
---|
152 | #endif
|
---|
153 | IO_Serial_Flush(ifd->io);
|
---|
154 | #endif
|
---|
155 | return ICC_ASYNC_OK;
|
---|
156 | #else
|
---|
157 | return ICC_ASYNC_ATR_ERROR;
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 | int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
|
---|
162 | {
|
---|
163 | icc->timings.block_delay = timings->block_delay;
|
---|
164 | icc->timings.char_delay = timings->char_delay;
|
---|
165 | icc->timings.block_timeout = timings->block_timeout;
|
---|
166 | icc->timings.char_timeout = timings->char_timeout;
|
---|
167 | /* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
|
---|
168 | cs_debug("SetTimings: T1: chardelay %d, chartimeout CWT %d, blockdelay BGT??? %d, blocktimeout BWT %d",timings->char_delay,timings->char_timeout, timings->block_delay, timings->block_timeout);
|
---|
169 | else
|
---|
170 | cs_debug("SetTimings: T0/T14: chardelay %d, chartimeout WWT %d, blockdelay %d, blocktimeout %d",timings->char_delay,timings->char_timeout, timings->block_delay, timings->block_timeout);*/
|
---|
171 |
|
---|
172 | #ifdef SCI_DEV
|
---|
173 | #include <sys/ioctl.h>
|
---|
174 | #include "sci_global.h"
|
---|
175 | #include "sci_ioctl.h"
|
---|
176 | if (icc->ifd->io->com == RTYP_SCI) {
|
---|
177 | SCI_PARAMETERS params;
|
---|
178 | if (ioctl(icc->ifd->io->fd, IOCTL_GET_PARAMETERS, ¶ms) < 0 )
|
---|
179 | return ICC_ASYNC_IFD_ERROR;
|
---|
180 | switch (icc->protocol_type) {
|
---|
181 | case ATR_PROTOCOL_TYPE_T1:
|
---|
182 | params.BWT = icc->timings.block_timeout;
|
---|
183 | params.CWT = icc->timings.char_timeout;
|
---|
184 | //params.BGT = icc->timings.block_delay; load into params.EGT??
|
---|
185 | break;
|
---|
186 | case ATR_PROTOCOL_TYPE_T0:
|
---|
187 | case ATR_PROTOCOL_TYPE_T14:
|
---|
188 | default:
|
---|
189 | params.WWT = icc->timings.char_timeout;
|
---|
190 | break;
|
---|
191 | }
|
---|
192 | if (ioctl(icc->ifd->io->fd, IOCTL_SET_PARAMETERS, ¶ms)!=0)
|
---|
193 | return ICC_ASYNC_IFD_ERROR;
|
---|
194 |
|
---|
195 | cs_debug("Set Timings: T=%d fs=%lu ETU=%d WWT=%d CWT=%d BWT=%d EGT=%d clock=%d check=%d P=%d I=%d U=%d", (int)params.T, params.fs, (int)params.ETU, (int)params.WWT, (int)params.CWT, (int)params.BWT, (int)params.EGT, (int)params.clock_stop_polarity, (int)params.check, (int)params.P, (int)params.I, (int)params.U);
|
---|
196 | }
|
---|
197 | #endif
|
---|
198 | return ICC_ASYNC_OK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
|
---|
202 | {
|
---|
203 | timings->block_delay = icc->timings.block_delay;
|
---|
204 | timings->char_delay = icc->timings.char_delay;
|
---|
205 | timings->block_timeout = icc->timings.block_timeout;
|
---|
206 | timings->char_timeout = icc->timings.char_timeout;
|
---|
207 |
|
---|
208 | return ICC_ASYNC_OK;
|
---|
209 | }
|
---|
210 |
|
---|
211 | int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
|
---|
212 | {
|
---|
213 | icc->baudrate = baudrate;
|
---|
214 | /*#ifdef COOL
|
---|
215 | if (icc->ifd->io->com==RTYP_SCI) {
|
---|
216 | typedef unsigned long u_int32;
|
---|
217 | u_int32 clk;
|
---|
218 | //clk = 357*10000; // MHZ
|
---|
219 | //clk = baudrate * 3570000L / 9600L;
|
---|
220 | clk = 500*10000;
|
---|
221 | if (cnxt_smc_set_clock_freq(icc->ifd->io->handle, clk))
|
---|
222 | return ICC_ASYNC_IFD_ERROR;
|
---|
223 | printf("set clock to %lu Hz\n", clk);
|
---|
224 | return ICC_ASYNC_OK;
|
---|
225 | }
|
---|
226 | else
|
---|
227 | #endif*/
|
---|
228 | if (IFD_Towitoko_SetBaudrate (icc->ifd, baudrate) != IFD_TOWITOKO_OK)
|
---|
229 | return ICC_ASYNC_IFD_ERROR;
|
---|
230 |
|
---|
231 | return ICC_ASYNC_OK;
|
---|
232 | }
|
---|
233 |
|
---|
234 | int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
|
---|
235 | {
|
---|
236 | (*baudrate) = icc->baudrate;
|
---|
237 | return ICC_ASYNC_OK;
|
---|
238 | }
|
---|
239 |
|
---|
240 | int ICC_Async_BeginTransmission (ICC_Async * icc)
|
---|
241 | {
|
---|
242 | /* Setup parity for this ICC */
|
---|
243 | #ifndef NO_PAR_SWITCH
|
---|
244 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
245 | {
|
---|
246 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
|
---|
247 | return ICC_ASYNC_IFD_ERROR;
|
---|
248 | }
|
---|
249 | else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
250 | {
|
---|
251 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
252 | return ICC_ASYNC_IFD_ERROR;
|
---|
253 | }
|
---|
254 | else
|
---|
255 | {
|
---|
256 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
|
---|
257 | return ICC_ASYNC_IFD_ERROR;
|
---|
258 | }
|
---|
259 |
|
---|
260 | /* Setup baudrate for this ICC */
|
---|
261 | /* if (IFD_Towitoko_SetBaudrate (icc->ifd, icc->baudrate)!= IFD_TOWITOKO_OK)
|
---|
262 | return ICC_ASYNC_IFD_ERROR;
|
---|
263 | */
|
---|
264 | #endif
|
---|
265 | return ICC_ASYNC_OK;
|
---|
266 | }
|
---|
267 |
|
---|
268 | #ifdef COOL
|
---|
269 | extern unsigned char cardbuffer[255];
|
---|
270 | extern int cardbuflen;
|
---|
271 | #endif
|
---|
272 |
|
---|
273 | int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
|
---|
274 | {
|
---|
275 | BYTE *buffer = NULL, *sent;
|
---|
276 | IFD_Timings timings;
|
---|
277 |
|
---|
278 | if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
|
---|
279 | {
|
---|
280 | buffer = (BYTE *) calloc(sizeof (BYTE), size);
|
---|
281 | memcpy (buffer, data, size);
|
---|
282 | ICC_Async_InvertBuffer (size, buffer);
|
---|
283 | sent = buffer;
|
---|
284 | }
|
---|
285 | else
|
---|
286 | {
|
---|
287 | sent = data;
|
---|
288 | }
|
---|
289 |
|
---|
290 | timings.block_delay = icc->timings.block_delay;
|
---|
291 | timings.char_delay = icc->timings.char_delay;
|
---|
292 |
|
---|
293 | #ifdef COOL
|
---|
294 | //transmit buffer to SC and put answer in cardbuffer
|
---|
295 | if (icc->ifd->io->com == RTYP_SCI) {
|
---|
296 | #define TIMEOUT 4000 //max 4294
|
---|
297 | cardbuflen = 256;//it needs to know max buffer size to respond?
|
---|
298 | if (cnxt_smc_read_write(handle, FALSE, sent, size, cardbuffer, &cardbuflen, TIMEOUT, 0))
|
---|
299 | return ICC_ASYNC_IFD_ERROR;
|
---|
300 |
|
---|
301 | #ifdef DEBUG_IFD
|
---|
302 | //usually done in IFD_Towitoko, for COOL do it here
|
---|
303 | printf ("COOLIFD: Transmit: ");
|
---|
304 | int i;
|
---|
305 | for (i = 0; i < size; i++)
|
---|
306 | printf ("%X ", sent[i]);
|
---|
307 | printf ("\n");
|
---|
308 | #endif
|
---|
309 | }
|
---|
310 | else
|
---|
311 | #else
|
---|
312 | if (IFD_Towitoko_Transmit (icc->ifd, &timings, size, sent) != IFD_TOWITOKO_OK)
|
---|
313 | return ICC_ASYNC_IFD_ERROR;
|
---|
314 | #endif
|
---|
315 |
|
---|
316 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
317 | free (buffer);
|
---|
318 |
|
---|
319 | return ICC_ASYNC_OK;
|
---|
320 | }
|
---|
321 |
|
---|
322 | int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
|
---|
323 | {
|
---|
324 | IFD_Timings timings;
|
---|
325 |
|
---|
326 | timings.block_timeout = icc->timings.block_timeout;
|
---|
327 | timings.char_timeout = icc->timings.char_timeout;
|
---|
328 |
|
---|
329 | #ifdef COOL
|
---|
330 | //receive buffer to SC
|
---|
331 | if (size > cardbuflen)
|
---|
332 | size = cardbuflen; //never read past end of buffer
|
---|
333 | memcpy(data,cardbuffer,size);
|
---|
334 | cardbuflen -= size;
|
---|
335 | memmove(cardbuffer,cardbuffer+size,cardbuflen);
|
---|
336 |
|
---|
337 | #ifdef DEBUG_IFD
|
---|
338 | int i;
|
---|
339 | printf ("COOLIFD: Receive: "); //I think
|
---|
340 | for (i = 0; i < size; i++)
|
---|
341 | printf ("%X ", data[i]);
|
---|
342 | printf ("\n");
|
---|
343 | fflush(stdout);
|
---|
344 | #endif
|
---|
345 | return ICC_ASYNC_OK;
|
---|
346 | #else
|
---|
347 | if (IFD_Towitoko_Receive (icc->ifd, &timings, size, data) != IFD_TOWITOKO_OK)
|
---|
348 | return ICC_ASYNC_IFD_ERROR;
|
---|
349 | #endif
|
---|
350 |
|
---|
351 | if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
|
---|
352 | ICC_Async_InvertBuffer (size, data);
|
---|
353 |
|
---|
354 | return ICC_ASYNC_OK;
|
---|
355 | }
|
---|
356 |
|
---|
357 | int ICC_Async_EndTransmission (ICC_Async * icc)
|
---|
358 | {
|
---|
359 | #ifndef NO_PAR_SWITCH
|
---|
360 | /* Restore parity */
|
---|
361 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
362 | return ICC_ASYNC_IFD_ERROR;
|
---|
363 | #endif
|
---|
364 |
|
---|
365 | return ICC_ASYNC_OK;
|
---|
366 | }
|
---|
367 |
|
---|
368 | ATR * ICC_Async_GetAtr (ICC_Async * icc)
|
---|
369 | {
|
---|
370 | return icc->atr;
|
---|
371 | }
|
---|
372 |
|
---|
373 | IFD * ICC_Async_GetIFD (ICC_Async * icc)
|
---|
374 | {
|
---|
375 | return icc->ifd;
|
---|
376 | }
|
---|
377 |
|
---|
378 | int ICC_Async_Close (ICC_Async * icc)
|
---|
379 | {
|
---|
380 | /* Dectivate ICC */
|
---|
381 | if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
|
---|
382 | return ICC_ASYNC_IFD_ERROR;
|
---|
383 |
|
---|
384 | /* LED Off */
|
---|
385 | if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
|
---|
386 | return ICC_ASYNC_IFD_ERROR;
|
---|
387 |
|
---|
388 | /* Delete atr */
|
---|
389 | ATR_Delete (icc->atr);
|
---|
390 |
|
---|
391 | ICC_Async_Clear (icc);
|
---|
392 |
|
---|
393 | return ICC_ASYNC_OK;
|
---|
394 | }
|
---|
395 |
|
---|
396 | unsigned long ICC_Async_GetClockRate (ICC_Async * icc)
|
---|
397 | {
|
---|
398 | switch (icc->ifd->io->cardmhz) {
|
---|
399 | case 357:
|
---|
400 | case 358:
|
---|
401 | return (372L * 9600L);
|
---|
402 | case 368:
|
---|
403 | return (384L * 9600L);
|
---|
404 | default:
|
---|
405 | return icc->ifd->io->cardmhz * 10000L;
|
---|
406 | }
|
---|
407 | }
|
---|
408 |
|
---|
409 | void ICC_Async_Delete (ICC_Async * icc)
|
---|
410 | {
|
---|
411 | free (icc);
|
---|
412 | }
|
---|
413 |
|
---|
414 | /*
|
---|
415 | * Not exported functions definition
|
---|
416 | */
|
---|
417 |
|
---|
418 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
|
---|
419 | {
|
---|
420 | uint i;
|
---|
421 |
|
---|
422 | for (i = 0; i < size; i++)
|
---|
423 | buffer[i] = ~(INVERT_BYTE (buffer[i]));
|
---|
424 | }
|
---|
425 |
|
---|
426 | static void ICC_Async_Clear (ICC_Async * icc)
|
---|
427 | {
|
---|
428 | icc->ifd = NULL;
|
---|
429 | icc->atr = NULL;
|
---|
430 | icc->baudrate = 0L;
|
---|
431 | icc->convention = 0;
|
---|
432 | icc->protocol_type = -1;
|
---|
433 | icc->timings.block_delay = 0;
|
---|
434 | icc->timings.char_delay = 0;
|
---|
435 | icc->timings.block_timeout = 0;
|
---|
436 | icc->timings.char_timeout = 0;
|
---|
437 | }
|
---|