source: trunk/csctapi/icc_async.c@ 1236

Last change on this file since 1236 was 1236, checked in by dingo35, 11 years ago

New SetParity function in IO_Serial

File size: 11.3 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
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
43static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
44static void ICC_Async_Clear (ICC_Async * icc);
45
46int fdmc=(-1);
47
48/*
49 * Exported functions definition
50 */
51
52ICC_Async *ICC_Async_New (void)
53{
54 ICC_Async *icc;
55
56 /* Allocate memory */
57 icc = (ICC_Async *) malloc (sizeof (ICC_Async));
58
59 if (icc != NULL)
60 ICC_Async_Clear (icc);
61
62 return icc;
63}
64
65int ICC_Async_Device_Init ()
66{
67
68#ifdef DEBUG_IO
69 printf ("IO: Opening serial port %s\n", reader[ridx].device);
70#endif
71
72#if defined(SCI_DEV) || defined(COOL)
73 if (reader[ridx].typ == R_INTERNAL)
74#ifdef SH4
75 reader[ridx].handle = open (reader[ridx].device, O_RDWR|O_NONBLOCK|O_NOCTTY);
76#elif COOL
77 return Cool_Init();
78#else
79 reader[ridx].handle = open (reader[ridx].device, O_RDWR);
80#endif
81 else
82#endif
83
84 reader[ridx].handle = open (reader[ridx].device, O_RDWR | O_NOCTTY| O_NONBLOCK);
85
86 if (reader[ridx].handle < 0)
87 return ICC_ASYNC_IFD_ERROR;
88
89#if defined(TUXBOX) && defined(PPC)
90 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2))
91 if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0)
92 {
93 close(reader[ridx].handle);
94 return ICC_ASYNC_IFD_ERROR;
95 }
96#endif
97
98 return ICC_ASYNC_OK;
99}
100
101int ICC_Async_GetStatus (BYTE * result)
102{
103 BYTE status[2];
104// unsigned int modembits=0;
105 int in;
106
107// printf("\n%08X\n", (int)ifd->io);
108
109// status : 0 -start, 1 - card, 2- no card
110
111#ifdef SCI_DEV
112 if(reader[ridx].typ == R_INTERNAL)
113 {
114 if(!Sci_GetStatus(reader[ridx].handle, &in))
115 return IFD_TOWITOKO_IO_ERROR;
116 }
117 else
118#elif COOL
119 if(reader[ridx].typ == R_INTERNAL)
120 {
121 if (!Cool_GetStatus(&in))
122 return IFD_TOWITOKO_IO_ERROR;
123 }
124 else
125#endif
126
127#if defined(TUXBOX) && defined(PPC)
128 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2))
129 {
130 ushort msr=1;
131 extern int fdmc;
132 IO_Serial_Ioctl_Lock(1);
133 ioctl(fdmc, GET_PCDAT, &msr);
134 if (reader[ridx].typ == R_DB2COM2)
135 in=(!(msr & 1));
136 else
137 in=((msr & 0x0f00) == 0x0f00);
138 IO_Serial_Ioctl_Lock(0);
139 }
140 else
141#endif
142#ifdef USE_GPIO
143 if (gpio_detect)
144 in=get_gpio();
145 else
146#endif
147 if (!Phoenix_GetStatus(&in))
148 return IFD_TOWITOKO_IO_ERROR;
149
150 if (in)
151 {
152 if(reader[ridx].status == 0)
153 {
154 status[0] = IFD_TOWITOKO_CARD_CHANGE;
155 reader[ridx].status = 1;
156#ifdef USE_GPIO
157 if (gpio_detect) set_gpio1(0);
158#endif
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#ifdef USE_GPIO
169 if (gpio_detect) set_gpio1(0);
170#endif
171 }
172 }
173 else
174 {
175 if(reader[ridx].status == 0)
176 {
177 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
178 reader[ridx].status = 2;
179#ifdef USE_GPIO
180 if (gpio_detect) set_gpio1(1);
181#endif
182 }
183 else if(reader[ridx].status == 1)
184 {
185 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
186 reader[ridx].status = 2;
187#ifdef USE_GPIO
188 if (gpio_detect) set_gpio1(1);
189#endif
190 }
191 else
192 {
193 status[0] = IFD_TOWITOKO_NOCARD_NOCHANGE;
194 }
195 }
196
197
198 (*result) = status[0];
199
200#ifdef DEBUG_IFD
201 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");
202#endif
203
204 return IFD_TOWITOKO_OK;
205}
206
207int ICC_Async_Init (ICC_Async * icc, IFD * ifd)
208{
209#ifndef ICC_TYPE_SYNC
210 unsigned np=0;
211
212 /* Initialize Baudrate */
213 if (IFD_Towitoko_SetBaudrate (ifd, ICC_ASYNC_BAUDRATE)!= IFD_TOWITOKO_OK)
214 return ICC_ASYNC_IFD_ERROR;
215
216 /* Activate ICC */
217 if (IFD_Towitoko_ActivateICC (ifd) != IFD_TOWITOKO_OK)
218 return ICC_ASYNC_IFD_ERROR;
219 /* Reset ICC */
220#ifdef SCI_DEV
221 if (reader[ridx].typ == R_INTERNAL) {
222 if (!Sci_Reset(&(icc->atr)))
223 {
224 icc->atr = NULL;
225 return ICC_ASYNC_IFD_ERROR;
226 }
227 }
228 else
229#endif
230#ifdef COOL
231 if (reader[ridx].typ == R_INTERNAL) {
232 if (!Cool_Reset(&(icc->atr)))
233 {
234 icc->atr = NULL;
235 return ICC_ASYNC_IFD_ERROR;
236 }
237 }
238 else
239#endif
240 if (IFD_Towitoko_ResetAsyncICC (ifd, &(icc->atr)) != IFD_TOWITOKO_OK)
241 {
242 icc->atr = NULL;
243 return ICC_ASYNC_IFD_ERROR;
244 }
245 /* Get ICC convention */
246 if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
247 {
248 ATR_Delete (icc->atr);
249 icc->atr = NULL;
250 icc->convention = 0;
251
252 return ICC_ASYNC_ATR_ERROR;
253 }
254
255 icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
256
257 ATR_GetNumberOfProtocols (icc->atr, &np);
258
259 /*
260 * Get protocol offered by interface bytes T*2 if available,
261 * (that is, if TD1 is available), * otherwise use default T=0
262 */
263/* if (np>1)
264 ATR_GetProtocolType (icc->atr, 1, &(icc->protocol_type));
265
266#ifdef DEBUG_ICC
267 printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
268#endif
269 *///really should let PPS handle this
270
271 /* Initialize member variables */
272 icc->baudrate = ICC_ASYNC_BAUDRATE;
273 icc->ifd = ifd;
274
275 if (icc->convention == ATR_CONVENTION_INVERSE)
276 {
277 if (!IO_Serial_SetParity (PARITY_ODD))
278 return ICC_ASYNC_IFD_ERROR;
279 }
280 else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
281 {
282 if (!IO_Serial_SetParity (PARITY_NONE))
283 return ICC_ASYNC_IFD_ERROR;
284 }
285 else
286 {
287 if (!IO_Serial_SetParity (PARITY_EVEN))
288 return ICC_ASYNC_IFD_ERROR;
289 }
290#ifdef COOL
291 if (reader[ridx].typ != R_INTERNAL)
292#endif
293 IO_Serial_Flush(ifd->io);
294 return ICC_ASYNC_OK;
295#else
296 return ICC_ASYNC_ATR_ERROR;
297#endif
298}
299
300int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
301{
302 icc->timings.block_delay = timings->block_delay;
303 icc->timings.char_delay = timings->char_delay;
304 icc->timings.block_timeout = timings->block_timeout;
305 icc->timings.char_timeout = timings->char_timeout;
306/* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
307 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);
308 else
309 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);*/
310
311#ifdef SCI_DEV
312#include <sys/ioctl.h>
313#include "sci_global.h"
314#include "sci_ioctl.h"
315 if (reader[ridx].typ == R_INTERNAL) {
316 SCI_PARAMETERS params;
317 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
318 return ICC_ASYNC_IFD_ERROR;
319 switch (icc->protocol_type) {
320 case ATR_PROTOCOL_TYPE_T1:
321 params.BWT = icc->timings.block_timeout;
322 params.CWT = icc->timings.char_timeout;
323 //params.BGT = icc->timings.block_delay; load into params.EGT??
324 break;
325 case ATR_PROTOCOL_TYPE_T0:
326 case ATR_PROTOCOL_TYPE_T14:
327 default:
328 params.WWT = icc->timings.char_timeout;
329 break;
330 }
331 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
332 return ICC_ASYNC_IFD_ERROR;
333
334 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);
335 }
336#endif
337 return ICC_ASYNC_OK;
338}
339
340int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
341{
342 timings->block_delay = icc->timings.block_delay;
343 timings->char_delay = icc->timings.char_delay;
344 timings->block_timeout = icc->timings.block_timeout;
345 timings->char_timeout = icc->timings.char_timeout;
346
347 return ICC_ASYNC_OK;
348}
349
350int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
351{
352 icc->baudrate = baudrate;
353 if (IFD_Towitoko_SetBaudrate (icc->ifd, baudrate) != IFD_TOWITOKO_OK)
354 return ICC_ASYNC_IFD_ERROR;
355
356 return ICC_ASYNC_OK;
357}
358
359int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
360{
361 (*baudrate) = icc->baudrate;
362 return ICC_ASYNC_OK;
363}
364
365int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
366{
367 BYTE *buffer = NULL, *sent;
368 IFD_Timings timings;
369
370 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
371 {
372 buffer = (BYTE *) calloc(sizeof (BYTE), size);
373 memcpy (buffer, data, size);
374 ICC_Async_InvertBuffer (size, buffer);
375 sent = buffer;
376 }
377 else
378 {
379 sent = data;
380 }
381
382 timings.block_delay = icc->timings.block_delay;
383 timings.char_delay = icc->timings.char_delay;
384
385#ifdef COOL
386 if (reader[ridx].typ == R_INTERNAL) {
387 if (!Cool_Transmit(sent, size))
388 return ICC_ASYNC_IFD_ERROR;
389 }
390 else
391#endif
392 if (!Phoenix_Transmit (sent, size, &timings, size))
393 return ICC_ASYNC_IFD_ERROR;
394
395 if (icc->convention == ATR_CONVENTION_INVERSE)
396 free (buffer);
397
398 return ICC_ASYNC_OK;
399}
400
401int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
402{
403 IFD_Timings timings;
404
405 timings.block_timeout = icc->timings.block_timeout;
406 timings.char_timeout = icc->timings.char_timeout;
407
408#ifdef COOL
409 if (reader[ridx].typ == R_INTERNAL) {
410 if (!Cool_Receive(data, size))
411 return ICC_ASYNC_IFD_ERROR;
412 }
413 else
414#else
415 if (!Phoenix_Receive (data, size, &timings))
416 return ICC_ASYNC_IFD_ERROR;
417#endif
418
419 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
420 ICC_Async_InvertBuffer (size, data);
421
422 return ICC_ASYNC_OK;
423}
424
425ATR * ICC_Async_GetAtr (ICC_Async * icc)
426{
427 return icc->atr;
428}
429
430IFD * ICC_Async_GetIFD (ICC_Async * icc)
431{
432 return icc->ifd;
433}
434
435int ICC_Async_Close (ICC_Async * icc)
436{
437 /* Dectivate ICC */
438 if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
439 return ICC_ASYNC_IFD_ERROR;
440
441 /* Delete atr */
442 ATR_Delete (icc->atr);
443
444 ICC_Async_Clear (icc);
445
446 return ICC_ASYNC_OK;
447}
448
449unsigned long ICC_Async_GetClockRate ()
450{
451 switch (reader[ridx].cardmhz) {
452 case 357:
453 case 358:
454 return (372L * 9600L);
455 case 368:
456 return (384L * 9600L);
457 default:
458 return reader[ridx].cardmhz * 10000L;
459 }
460}
461
462void ICC_Async_Delete (ICC_Async * icc)
463{
464 free (icc);
465}
466
467/*
468 * Not exported functions definition
469 */
470
471static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
472{
473 uint i;
474
475 for (i = 0; i < size; i++)
476 buffer[i] = ~(INVERT_BYTE (buffer[i]));
477}
478
479static void ICC_Async_Clear (ICC_Async * icc)
480{
481 icc->ifd = NULL;
482 icc->atr = NULL;
483 icc->baudrate = 0L;
484 icc->convention = 0;
485 icc->protocol_type = -1;
486 icc->timings.block_delay = 0;
487 icc->timings.char_delay = 0;
488 icc->timings.block_timeout = 0;
489 icc->timings.char_timeout = 0;
490}
Note: See TracBrowser for help on using the repository browser.