source: trunk/csctapi/icc_async.c@ 1223

Last change on this file since 1223 was 1223, checked in by dingo35, 12 years ago

Fix compiler errors for Coolstream, GPIO (for wrt) not fixed yet

File size: 11.8 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 /* LED Red */
213 if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
214 return ICC_ASYNC_IFD_ERROR;
215
216 /* Initialize Baudrate */
217 if (IFD_Towitoko_SetBaudrate (ifd, ICC_ASYNC_BAUDRATE)!= IFD_TOWITOKO_OK)
218 return ICC_ASYNC_IFD_ERROR;
219
220 /* Activate ICC */
221 if (IFD_Towitoko_ActivateICC (ifd) != IFD_TOWITOKO_OK)
222 return ICC_ASYNC_IFD_ERROR;
223 /* Reset ICC */
224#ifdef SCI_DEV
225 if (reader[ridx].typ == R_INTERNAL) {
226 if (!Sci_Reset(ifd, &(icc->atr)))
227 {
228 icc->atr = NULL;
229 return ICC_ASYNC_IFD_ERROR;
230 }
231 }
232 else
233#endif
234#ifdef COOL
235 if (reader[ridx].typ == R_INTERNAL) {
236 if (!Cool_Reset(&(icc->atr)))
237 {
238 icc->atr = NULL;
239 return ICC_ASYNC_IFD_ERROR;
240 }
241 }
242 else
243#endif
244 if (IFD_Towitoko_ResetAsyncICC (ifd, &(icc->atr)) != IFD_TOWITOKO_OK)
245 {
246 icc->atr = NULL;
247 return ICC_ASYNC_IFD_ERROR;
248 }
249 /* Get ICC convention */
250 if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
251 {
252 ATR_Delete (icc->atr);
253 icc->atr = NULL;
254 icc->convention = 0;
255
256 return ICC_ASYNC_ATR_ERROR;
257 }
258
259 icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
260
261 ATR_GetNumberOfProtocols (icc->atr, &np);
262
263 /*
264 * Get protocol offered by interface bytes T*2 if available,
265 * (that is, if TD1 is available), * otherwise use default T=0
266 */
267/* if (np>1)
268 ATR_GetProtocolType (icc->atr, 1, &(icc->protocol_type));
269
270#ifdef DEBUG_ICC
271 printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
272#endif
273 *///really should let PPS handle this
274 /* LED Green */
275 if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
276 {
277 ATR_Delete (icc->atr);
278 icc->atr = NULL;
279 icc->convention = 0;
280
281 return ICC_ASYNC_IFD_ERROR;
282 }
283
284 /* Initialize member variables */
285 icc->baudrate = ICC_ASYNC_BAUDRATE;
286 icc->ifd = ifd;
287
288 if (icc->convention == ATR_CONVENTION_INVERSE)
289 {
290 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
291 return ICC_ASYNC_IFD_ERROR;
292 }
293 else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
294 {
295 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
296 return ICC_ASYNC_IFD_ERROR;
297 }
298 else
299 {
300 if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
301 return ICC_ASYNC_IFD_ERROR;
302 }
303#ifdef COOL
304 if (reader[ridx].typ != R_INTERNAL)
305#endif
306 IO_Serial_Flush(ifd->io);
307 return ICC_ASYNC_OK;
308#else
309 return ICC_ASYNC_ATR_ERROR;
310#endif
311}
312
313int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
314{
315 icc->timings.block_delay = timings->block_delay;
316 icc->timings.char_delay = timings->char_delay;
317 icc->timings.block_timeout = timings->block_timeout;
318 icc->timings.char_timeout = timings->char_timeout;
319/* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
320 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);
321 else
322 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);*/
323
324#ifdef SCI_DEV
325#include <sys/ioctl.h>
326#include "sci_global.h"
327#include "sci_ioctl.h"
328 if (reader[ridx].typ == R_INTERNAL) {
329 SCI_PARAMETERS params;
330 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
331 return ICC_ASYNC_IFD_ERROR;
332 switch (icc->protocol_type) {
333 case ATR_PROTOCOL_TYPE_T1:
334 params.BWT = icc->timings.block_timeout;
335 params.CWT = icc->timings.char_timeout;
336 //params.BGT = icc->timings.block_delay; load into params.EGT??
337 break;
338 case ATR_PROTOCOL_TYPE_T0:
339 case ATR_PROTOCOL_TYPE_T14:
340 default:
341 params.WWT = icc->timings.char_timeout;
342 break;
343 }
344 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
345 return ICC_ASYNC_IFD_ERROR;
346
347 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);
348 }
349#endif
350 return ICC_ASYNC_OK;
351}
352
353int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
354{
355 timings->block_delay = icc->timings.block_delay;
356 timings->char_delay = icc->timings.char_delay;
357 timings->block_timeout = icc->timings.block_timeout;
358 timings->char_timeout = icc->timings.char_timeout;
359
360 return ICC_ASYNC_OK;
361}
362
363int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
364{
365 icc->baudrate = baudrate;
366 if (IFD_Towitoko_SetBaudrate (icc->ifd, baudrate) != IFD_TOWITOKO_OK)
367 return ICC_ASYNC_IFD_ERROR;
368
369 return ICC_ASYNC_OK;
370}
371
372int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
373{
374 (*baudrate) = icc->baudrate;
375 return ICC_ASYNC_OK;
376}
377
378int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
379{
380 BYTE *buffer = NULL, *sent;
381 IFD_Timings timings;
382
383 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
384 {
385 buffer = (BYTE *) calloc(sizeof (BYTE), size);
386 memcpy (buffer, data, size);
387 ICC_Async_InvertBuffer (size, buffer);
388 sent = buffer;
389 }
390 else
391 {
392 sent = data;
393 }
394
395 timings.block_delay = icc->timings.block_delay;
396 timings.char_delay = icc->timings.char_delay;
397
398#ifdef COOL
399 if (reader[ridx].typ == R_INTERNAL) {
400 if (!Cool_Transmit(sent, size))
401 return ICC_ASYNC_IFD_ERROR;
402 }
403 else
404#endif
405 if (IFD_Towitoko_Transmit (icc->ifd, &timings, size, sent) != IFD_TOWITOKO_OK)
406 return ICC_ASYNC_IFD_ERROR;
407
408 if (icc->convention == ATR_CONVENTION_INVERSE)
409 free (buffer);
410
411 return ICC_ASYNC_OK;
412}
413
414int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
415{
416 IFD_Timings timings;
417
418 timings.block_timeout = icc->timings.block_timeout;
419 timings.char_timeout = icc->timings.char_timeout;
420
421#ifdef COOL
422 if (reader[ridx].typ == R_INTERNAL) {
423 if (!Cool_Receive(data, size))
424 return ICC_ASYNC_IFD_ERROR;
425 }
426 else
427#else
428 if (IFD_Towitoko_Receive (icc->ifd, &timings, size, data) != IFD_TOWITOKO_OK)
429 return ICC_ASYNC_IFD_ERROR;
430#endif
431
432 if (icc->convention == ATR_CONVENTION_INVERSE && reader[ridx].typ!=R_INTERNAL)
433 ICC_Async_InvertBuffer (size, data);
434
435 return ICC_ASYNC_OK;
436}
437
438ATR * ICC_Async_GetAtr (ICC_Async * icc)
439{
440 return icc->atr;
441}
442
443IFD * ICC_Async_GetIFD (ICC_Async * icc)
444{
445 return icc->ifd;
446}
447
448int ICC_Async_Close (ICC_Async * icc)
449{
450 /* Dectivate ICC */
451 if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
452 return ICC_ASYNC_IFD_ERROR;
453
454 /* LED Off */
455 if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
456 return ICC_ASYNC_IFD_ERROR;
457
458 /* Delete atr */
459 ATR_Delete (icc->atr);
460
461 ICC_Async_Clear (icc);
462
463 return ICC_ASYNC_OK;
464}
465
466unsigned long ICC_Async_GetClockRate (ICC_Async * icc)
467{
468 switch (reader[ridx].cardmhz) {
469 case 357:
470 case 358:
471 return (372L * 9600L);
472 case 368:
473 return (384L * 9600L);
474 default:
475 return reader[ridx].cardmhz * 10000L;
476 }
477}
478
479void ICC_Async_Delete (ICC_Async * icc)
480{
481 free (icc);
482}
483
484/*
485 * Not exported functions definition
486 */
487
488static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
489{
490 uint i;
491
492 for (i = 0; i < size; i++)
493 buffer[i] = ~(INVERT_BYTE (buffer[i]));
494}
495
496static void ICC_Async_Clear (ICC_Async * icc)
497{
498 icc->ifd = NULL;
499 icc->atr = NULL;
500 icc->baudrate = 0L;
501 icc->convention = 0;
502 icc->protocol_type = -1;
503 icc->timings.block_delay = 0;
504 icc->timings.char_delay = 0;
505 icc->timings.block_timeout = 0;
506 icc->timings.char_timeout = 0;
507}
Note: See TracBrowser for help on using the repository browser.