source: branches/smartreader/csctapi/icc_async.c@ 1271

Last change on this file since 1271 was 1271, checked in by rorothetroll, 13 years ago

resync with trunk

File size: 10.9 KB
Line 
1/*
2 icc_async.c
3 Asynchronous ICC's handling functions
4
5 This file is part of the Unix driver for Towitoko smartcard readers
6 Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
7
8 This version is modified by doz21 to work in a special manner ;)
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23*/
24
25#include "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#include "mc_global.h"
33
34/*
35 * Not exported constants definition
36 */
37#define ICC_ASYNC_MAX_TRANSMIT 255
38#define ICC_ASYNC_BAUDRATE 9600
39
40/*
41 * Not exported functions declaration
42 */
43
44static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
45static void ICC_Async_Clear (ICC_Async * icc);
46
47int fdmc=(-1);
48
49/*
50 * Exported functions definition
51 */
52
53ICC_Async *ICC_Async_New (void)
54{
55 ICC_Async *icc;
56
57 /* Allocate memory */
58 icc = (ICC_Async *) malloc (sizeof (ICC_Async));
59
60 if (icc != NULL)
61 ICC_Async_Clear (icc);
62
63 return icc;
64}
65
66int ICC_Async_Device_Init ()
67{
68
69 wr = 0;
70#ifdef DEBUG_IO
71 printf ("IO: Opening serial port %s\n", reader[ridx].device);
72#endif
73
74#if defined(SCI_DEV) || defined(COOL)
75 if (reader[ridx].typ == R_INTERNAL)
76#if defined(SH4) || defined(STB04SCI)
77 reader[ridx].handle = open (reader[ridx].device, O_RDWR|O_NONBLOCK|O_NOCTTY);
78#elif COOL
79 return Cool_Init();
80#else
81 reader[ridx].handle = open (reader[ridx].device, O_RDWR);
82#endif
83 else
84#endif
85
86 reader[ridx].handle = open (reader[ridx].device, O_RDWR | O_NOCTTY| O_NONBLOCK);
87
88 if (reader[ridx].handle < 0)
89 return ICC_ASYNC_IFD_ERROR;
90
91#if defined(TUXBOX) && defined(PPC)
92 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2))
93 if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0)
94 {
95 close(reader[ridx].handle);
96 return ICC_ASYNC_IFD_ERROR;
97 }
98#endif
99
100 if (reader[ridx].typ != R_INTERNAL) { //FIXME move to ifd_phoenix.c
101 if(!IO_Serial_InitPnP ())
102 return ICC_ASYNC_IFD_ERROR;
103 IO_Serial_Flush();
104 }
105
106 return ICC_ASYNC_OK;
107}
108
109int ICC_Async_GetStatus (BYTE * result)
110{
111 BYTE status[2];
112// unsigned int modembits=0;
113 int in;
114
115// printf("\n%08X\n", (int)ifd->io);
116
117// status : 0 -start, 1 - card, 2- no card
118
119#ifdef SCI_DEV
120 if(reader[ridx].typ == R_INTERNAL)
121 {
122 if(!Sci_GetStatus(reader[ridx].handle, &in))
123 return ICC_ASYNC_IFD_ERROR;
124 }
125 else
126#elif COOL
127 if(reader[ridx].typ == R_INTERNAL)
128 {
129 if (!Cool_GetStatus(&in))
130 return ICC_ASYNC_IFD_ERROR;
131 }
132 else
133#endif
134
135#if defined(TUXBOX) && defined(PPC)
136 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2))
137 {
138 ushort msr=1;
139 extern int fdmc;
140 IO_Serial_Ioctl_Lock(1);
141 ioctl(fdmc, GET_PCDAT, &msr);
142 if (reader[ridx].typ == R_DB2COM2)
143 in=(!(msr & 1));
144 else
145 in=((msr & 0x0f00) == 0x0f00);
146 IO_Serial_Ioctl_Lock(0);
147 }
148 else
149#endif
150 if (!Phoenix_GetStatus(&in))
151 return ICC_ASYNC_IFD_ERROR;
152
153 if (in)
154 {
155 if(reader[ridx].status == 0)
156 {
157 status[0] = IFD_TOWITOKO_CARD_CHANGE;
158 reader[ridx].status = 1;
159 }
160 else if(reader[ridx].status == 1)
161 {
162 status[0] = IFD_TOWITOKO_CARD_NOCHANGE;
163 }
164 else
165 {
166 status[0] = IFD_TOWITOKO_CARD_CHANGE;
167 reader[ridx].status = 1;
168 }
169 }
170 else
171 {
172 if(reader[ridx].status == 0)
173 {
174 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
175 reader[ridx].status = 2;
176 }
177 else if(reader[ridx].status == 1)
178 {
179 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
180 reader[ridx].status = 2;
181 }
182 else
183 {
184 status[0] = IFD_TOWITOKO_NOCARD_NOCHANGE;
185 }
186 }
187
188
189 (*result) = status[0];
190
191#ifdef DEBUG_IFD
192 printf ("IFD: com%d Status = %s / %s\n", reader[ridx].typ, IFD_TOWITOKO_CARD(status[0])? "card": "no card", IFD_TOWITOKO_CHANGE(status[0])? "change": "no change");
193#endif
194
195 return ICC_ASYNC_OK;
196}
197
198int ICC_Async_Init (ICC_Async * icc)
199{
200#ifndef ICC_TYPE_SYNC
201 unsigned np=0;
202
203 /* Initialize Baudrate */
204 if (!Phoenix_SetBaudrate (ICC_ASYNC_BAUDRATE))
205 return ICC_ASYNC_IFD_ERROR;
206
207#ifdef SCI_DEV
208 /* Activate ICC */
209 if (!Sci_Activate())
210 return ICC_ASYNC_IFD_ERROR;
211 /* Reset ICC */
212 if (reader[ridx].typ == R_INTERNAL) {
213 if (!Sci_Reset(&(icc->atr)))
214 {
215 icc->atr = NULL;
216 return ICC_ASYNC_IFD_ERROR;
217 }
218 }
219 else
220#endif
221#ifdef COOL
222 if (reader[ridx].typ == R_INTERNAL) {
223 if (!Cool_Reset(&(icc->atr)))
224 {
225 icc->atr = NULL;
226 return ICC_ASYNC_IFD_ERROR;
227 }
228 }
229 else
230#endif
231 if (!Phoenix_Reset(&(icc->atr)))
232 {
233 icc->atr = NULL;
234 return ICC_ASYNC_IFD_ERROR;
235 }
236 /* Get ICC convention */
237 if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
238 {
239 ATR_Delete (icc->atr);
240 icc->atr = NULL;
241 icc->convention = 0;
242
243 return ICC_ASYNC_ATR_ERROR;
244 }
245
246 icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
247
248 ATR_GetNumberOfProtocols (icc->atr, &np);
249
250 /*
251 * Get protocol offered by interface bytes T*2 if available,
252 * (that is, if TD1 is available), * otherwise use default T=0
253 */
254/* if (np>1)
255 ATR_GetProtocolType (icc->atr, 1, &(icc->protocol_type));
256
257#ifdef DEBUG_ICC
258 printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
259#endif
260 *///really should let PPS handle this
261
262 /* Initialize member variables */
263 icc->baudrate = ICC_ASYNC_BAUDRATE;
264
265 if (icc->convention == ATR_CONVENTION_INVERSE)
266 {
267 if (!IO_Serial_SetParity (PARITY_ODD))
268 return ICC_ASYNC_IFD_ERROR;
269 }
270 else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
271 {
272 if (!IO_Serial_SetParity (PARITY_NONE))
273 return ICC_ASYNC_IFD_ERROR;
274 }
275 else
276 {
277 if (!IO_Serial_SetParity (PARITY_EVEN))
278 return ICC_ASYNC_IFD_ERROR;
279 }
280#ifdef COOL
281 if (reader[ridx].typ != R_INTERNAL)
282#endif
283 IO_Serial_Flush();
284 return ICC_ASYNC_OK;
285#else
286 return ICC_ASYNC_ATR_ERROR;
287#endif
288}
289
290int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
291{
292 icc->timings.block_delay = timings->block_delay;
293 icc->timings.char_delay = timings->char_delay;
294 icc->timings.block_timeout = timings->block_timeout;
295 icc->timings.char_timeout = timings->char_timeout;
296/* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
297 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);
298 else
299 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);*/
300
301#ifdef SCI_DEV
302#include <sys/ioctl.h>
303#include "sci_global.h"
304#include "sci_ioctl.h"
305 if (reader[ridx].typ == R_INTERNAL) {
306 SCI_PARAMETERS params;
307 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
308 return ICC_ASYNC_IFD_ERROR;
309 switch (icc->protocol_type) {
310 case ATR_PROTOCOL_TYPE_T1:
311 params.BWT = icc->timings.block_timeout;
312 params.CWT = icc->timings.char_timeout;
313 //params.BGT = icc->timings.block_delay; load into params.EGT??
314 break;
315 case ATR_PROTOCOL_TYPE_T0:
316 case ATR_PROTOCOL_TYPE_T14:
317 default:
318 params.WWT = icc->timings.char_timeout;
319 break;
320 }
321 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
322 return ICC_ASYNC_IFD_ERROR;
323
324 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);
325 }
326#endif
327 return ICC_ASYNC_OK;
328}
329
330int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
331{
332 timings->block_delay = icc->timings.block_delay;
333 timings->char_delay = icc->timings.char_delay;
334 timings->block_timeout = icc->timings.block_timeout;
335 timings->char_timeout = icc->timings.char_timeout;
336
337 return ICC_ASYNC_OK;
338}
339
340int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
341{
342 icc->baudrate = baudrate;
343 if (!Phoenix_SetBaudrate (baudrate))
344 return ICC_ASYNC_IFD_ERROR;
345
346 return ICC_ASYNC_OK;
347}
348
349int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
350{
351 (*baudrate) = icc->baudrate;
352 return ICC_ASYNC_OK;
353}
354
355int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
356{
357 BYTE *buffer = NULL, *sent;
358 IFD_Timings timings;
359
360 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
361 {
362 buffer = (BYTE *) calloc(sizeof (BYTE), size);
363 memcpy (buffer, data, size);
364 ICC_Async_InvertBuffer (size, buffer);
365 sent = buffer;
366 }
367 else
368 {
369 sent = data;
370 }
371
372 timings.block_delay = icc->timings.block_delay;
373 timings.char_delay = icc->timings.char_delay;
374
375#ifdef COOL
376 if (reader[ridx].typ == R_INTERNAL) {
377 if (!Cool_Transmit(sent, size))
378 return ICC_ASYNC_IFD_ERROR;
379 }
380 else
381#endif
382 if (!Phoenix_Transmit (sent, size, &timings, size))
383 return ICC_ASYNC_IFD_ERROR;
384
385 if (icc->convention == ATR_CONVENTION_INVERSE)
386 free (buffer);
387
388 return ICC_ASYNC_OK;
389}
390
391int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
392{
393 IFD_Timings timings;
394
395 timings.block_timeout = icc->timings.block_timeout;
396 timings.char_timeout = icc->timings.char_timeout;
397
398#ifdef COOL
399 if (reader[ridx].typ == R_INTERNAL) {
400 if (!Cool_Receive(data, size))
401 return ICC_ASYNC_IFD_ERROR;
402 }
403 else
404#else
405 if (!Phoenix_Receive (data, size, &timings))
406 return ICC_ASYNC_IFD_ERROR;
407#endif
408
409 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
410 ICC_Async_InvertBuffer (size, data);
411
412 return ICC_ASYNC_OK;
413}
414
415ATR * ICC_Async_GetAtr (ICC_Async * icc)
416{
417 return icc->atr;
418}
419
420int ICC_Async_Close (ICC_Async * icc)
421{
422#ifdef SCI_DEV
423 /* Dectivate ICC */
424 if (!Sci_Deactivate())
425 return ICC_ASYNC_IFD_ERROR;
426#endif
427
428 /* Delete atr */
429 ATR_Delete (icc->atr);
430
431 ICC_Async_Clear (icc);
432
433 return ICC_ASYNC_OK;
434}
435
436unsigned long ICC_Async_GetClockRate ()
437{
438 switch (reader[ridx].cardmhz) {
439 case 357:
440 case 358:
441 return (372L * 9600L);
442 case 368:
443 return (384L * 9600L);
444 default:
445 return reader[ridx].cardmhz * 10000L;
446 }
447}
448
449void ICC_Async_Delete (ICC_Async * icc)
450{
451 free (icc);
452}
453
454/*
455 * Not exported functions definition
456 */
457
458static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
459{
460 uint i;
461
462 for (i = 0; i < size; i++)
463 buffer[i] = ~(INVERT_BYTE (buffer[i]));
464}
465
466static void ICC_Async_Clear (ICC_Async * icc)
467{
468 icc->atr = NULL;
469 icc->baudrate = 0L;
470 icc->convention = 0;
471 icc->protocol_type = -1;
472 icc->timings.block_delay = 0;
473 icc->timings.char_delay = 0;
474 icc->timings.block_timeout = 0;
475 icc->timings.char_timeout = 0;
476}
Note: See TracBrowser for help on using the repository browser.