source: trunk/csctapi/icc_async.c@ 1252

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

New SetBaudrate in ifd_phoenix.c eliminates lot of stuff

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 (!Phoenix_SetBaudrate (ICC_ASYNC_BAUDRATE))
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 icc->ifd->io->parity = PARITY_ODD;
280 }
281 else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
282 {
283 if (!IO_Serial_SetParity (PARITY_NONE))
284 return ICC_ASYNC_IFD_ERROR;
285 icc->ifd->io->parity = PARITY_NONE;
286 }
287 else
288 {
289 if (!IO_Serial_SetParity (PARITY_EVEN))
290 return ICC_ASYNC_IFD_ERROR;
291 icc->ifd->io->parity = PARITY_EVEN;
292 }
293#ifdef COOL
294 if (reader[ridx].typ != R_INTERNAL)
295#endif
296 IO_Serial_Flush(ifd->io);
297 return ICC_ASYNC_OK;
298#else
299 return ICC_ASYNC_ATR_ERROR;
300#endif
301}
302
303int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
304{
305 icc->timings.block_delay = timings->block_delay;
306 icc->timings.char_delay = timings->char_delay;
307 icc->timings.block_timeout = timings->block_timeout;
308 icc->timings.char_timeout = timings->char_timeout;
309/* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
310 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);
311 else
312 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);*/
313
314#ifdef SCI_DEV
315#include <sys/ioctl.h>
316#include "sci_global.h"
317#include "sci_ioctl.h"
318 if (reader[ridx].typ == R_INTERNAL) {
319 SCI_PARAMETERS params;
320 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
321 return ICC_ASYNC_IFD_ERROR;
322 switch (icc->protocol_type) {
323 case ATR_PROTOCOL_TYPE_T1:
324 params.BWT = icc->timings.block_timeout;
325 params.CWT = icc->timings.char_timeout;
326 //params.BGT = icc->timings.block_delay; load into params.EGT??
327 break;
328 case ATR_PROTOCOL_TYPE_T0:
329 case ATR_PROTOCOL_TYPE_T14:
330 default:
331 params.WWT = icc->timings.char_timeout;
332 break;
333 }
334 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
335 return ICC_ASYNC_IFD_ERROR;
336
337 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);
338 }
339#endif
340 return ICC_ASYNC_OK;
341}
342
343int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
344{
345 timings->block_delay = icc->timings.block_delay;
346 timings->char_delay = icc->timings.char_delay;
347 timings->block_timeout = icc->timings.block_timeout;
348 timings->char_timeout = icc->timings.char_timeout;
349
350 return ICC_ASYNC_OK;
351}
352
353int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
354{
355 icc->baudrate = baudrate;
356 if (!Phoenix_SetBaudrate (baudrate))
357 return ICC_ASYNC_IFD_ERROR;
358
359 return ICC_ASYNC_OK;
360}
361
362int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
363{
364 (*baudrate) = icc->baudrate;
365 return ICC_ASYNC_OK;
366}
367
368int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
369{
370 BYTE *buffer = NULL, *sent;
371 IFD_Timings timings;
372
373 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
374 {
375 buffer = (BYTE *) calloc(sizeof (BYTE), size);
376 memcpy (buffer, data, size);
377 ICC_Async_InvertBuffer (size, buffer);
378 sent = buffer;
379 }
380 else
381 {
382 sent = data;
383 }
384
385 timings.block_delay = icc->timings.block_delay;
386 timings.char_delay = icc->timings.char_delay;
387
388#ifdef COOL
389 if (reader[ridx].typ == R_INTERNAL) {
390 if (!Cool_Transmit(sent, size))
391 return ICC_ASYNC_IFD_ERROR;
392 }
393 else
394#endif
395 if (!Phoenix_Transmit (sent, size, &timings, size))
396 return ICC_ASYNC_IFD_ERROR;
397
398 if (icc->convention == ATR_CONVENTION_INVERSE)
399 free (buffer);
400
401 return ICC_ASYNC_OK;
402}
403
404int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
405{
406 IFD_Timings timings;
407
408 timings.block_timeout = icc->timings.block_timeout;
409 timings.char_timeout = icc->timings.char_timeout;
410
411#ifdef COOL
412 if (reader[ridx].typ == R_INTERNAL) {
413 if (!Cool_Receive(data, size))
414 return ICC_ASYNC_IFD_ERROR;
415 }
416 else
417#else
418 if (!Phoenix_Receive (data, size, &timings))
419 return ICC_ASYNC_IFD_ERROR;
420#endif
421
422 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
423 ICC_Async_InvertBuffer (size, data);
424
425 return ICC_ASYNC_OK;
426}
427
428ATR * ICC_Async_GetAtr (ICC_Async * icc)
429{
430 return icc->atr;
431}
432
433IFD * ICC_Async_GetIFD (ICC_Async * icc)
434{
435 return icc->ifd;
436}
437
438int ICC_Async_Close (ICC_Async * icc)
439{
440 /* Dectivate ICC */
441 if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
442 return ICC_ASYNC_IFD_ERROR;
443
444 /* Delete atr */
445 ATR_Delete (icc->atr);
446
447 ICC_Async_Clear (icc);
448
449 return ICC_ASYNC_OK;
450}
451
452unsigned long ICC_Async_GetClockRate ()
453{
454 switch (reader[ridx].cardmhz) {
455 case 357:
456 case 358:
457 return (372L * 9600L);
458 case 368:
459 return (384L * 9600L);
460 default:
461 return reader[ridx].cardmhz * 10000L;
462 }
463}
464
465void ICC_Async_Delete (ICC_Async * icc)
466{
467 free (icc);
468}
469
470/*
471 * Not exported functions definition
472 */
473
474static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
475{
476 uint i;
477
478 for (i = 0; i < size; i++)
479 buffer[i] = ~(INVERT_BYTE (buffer[i]));
480}
481
482static void ICC_Async_Clear (ICC_Async * icc)
483{
484 icc->ifd = NULL;
485 icc->atr = NULL;
486 icc->baudrate = 0L;
487 icc->convention = 0;
488 icc->protocol_type = -1;
489 icc->timings.block_delay = 0;
490 icc->timings.char_delay = 0;
491 icc->timings.block_timeout = 0;
492 icc->timings.char_timeout = 0;
493}
Note: See TracBrowser for help on using the repository browser.