source: trunk/csctapi/icc_async.c@ 1278

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

Cleanup ICC_Async: atr, baudrate, timings, convention

File size: 10.0 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(&(atr)))
214 {
215 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(&(atr)))
224 {
225 atr = NULL;
226 return ICC_ASYNC_IFD_ERROR;
227 }
228 }
229 else
230#endif
231 if (!Phoenix_Reset(&(atr)))
232 {
233 atr = NULL;
234 return ICC_ASYNC_IFD_ERROR;
235 }
236 /* Get ICC convention */
237 if (ATR_GetConvention (atr, &(convention)) != ATR_OK)
238 {
239 ATR_Delete (atr);
240 atr = NULL;
241 convention = 0;
242
243 return ICC_ASYNC_ATR_ERROR;
244 }
245
246 protocol_type = ATR_PROTOCOL_TYPE_T0;
247
248 ATR_GetNumberOfProtocols (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 (atr, 1, &(protocol_type));
256
257#ifdef DEBUG_ICC
258 printf("ICC: Detected %s convention processor card T=%d\n",(convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), protocol_type);
259#endif
260 *///really should let PPS handle this
261
262 /* Initialize member variables */
263 if (convention == ATR_CONVENTION_INVERSE)
264 {
265 if (!IO_Serial_SetParity (PARITY_ODD))
266 return ICC_ASYNC_IFD_ERROR;
267 }
268 else if(protocol_type == ATR_PROTOCOL_TYPE_T14)
269 {
270 if (!IO_Serial_SetParity (PARITY_NONE))
271 return ICC_ASYNC_IFD_ERROR;
272 }
273 else
274 {
275 if (!IO_Serial_SetParity (PARITY_EVEN))
276 return ICC_ASYNC_IFD_ERROR;
277 }
278#ifdef COOL
279 if (reader[ridx].typ != R_INTERNAL)
280#endif
281 IO_Serial_Flush();
282 return ICC_ASYNC_OK;
283#else
284 return ICC_ASYNC_ATR_ERROR;
285#endif
286}
287
288int ICC_Async_SetTimings ()
289{
290/* if (protocol_type == ATR_PROTOCOL_TYPE_T1)
291 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);
292 else
293 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);*/
294
295#ifdef SCI_DEV
296#include <sys/ioctl.h>
297#include "sci_global.h"
298#include "sci_ioctl.h"
299 if (reader[ridx].typ == R_INTERNAL) {
300 SCI_PARAMETERS params;
301 if (ioctl(reader[ridx].handle, IOCTL_GET_PARAMETERS, &params) < 0 )
302 return ICC_ASYNC_IFD_ERROR;
303 switch (protocol_type) {
304 case ATR_PROTOCOL_TYPE_T1:
305 params.BWT = icc_timings.block_timeout;
306 params.CWT = icc_timings.char_timeout;
307 //params.BGT = icc_timings.block_delay; load into params.EGT??
308 break;
309 case ATR_PROTOCOL_TYPE_T0:
310 case ATR_PROTOCOL_TYPE_T14:
311 default:
312 params.WWT = icc_timings.char_timeout;
313 break;
314 }
315 if (ioctl(reader[ridx].handle, IOCTL_SET_PARAMETERS, &params)!=0)
316 return ICC_ASYNC_IFD_ERROR;
317
318 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);
319 }
320#endif
321 return ICC_ASYNC_OK;
322}
323
324int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
325{
326 if (!Phoenix_SetBaudrate (baudrate))
327 return ICC_ASYNC_IFD_ERROR;
328
329 return ICC_ASYNC_OK;
330}
331
332int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
333{
334 (*baudrate) = reader[ridx].baudrate;
335 return ICC_ASYNC_OK;
336}
337
338int ICC_Async_Transmit (unsigned size, BYTE * data)
339{
340 BYTE *buffer = NULL, *sent;
341 IFD_Timings timings;
342
343 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
344 {
345 buffer = (BYTE *) calloc(sizeof (BYTE), size);
346 memcpy (buffer, data, size);
347 ICC_Async_InvertBuffer (size, buffer);
348 sent = buffer;
349 }
350 else
351 {
352 sent = data;
353 }
354
355 timings.block_delay = icc_timings.block_delay;
356 timings.char_delay = icc_timings.char_delay;
357
358#ifdef COOL
359 if (reader[ridx].typ == R_INTERNAL) {
360 if (!Cool_Transmit(sent, size))
361 return ICC_ASYNC_IFD_ERROR;
362 }
363 else
364#endif
365 if (!Phoenix_Transmit (sent, size, &timings, size))
366 return ICC_ASYNC_IFD_ERROR;
367
368 if (convention == ATR_CONVENTION_INVERSE)
369 free (buffer);
370
371 return ICC_ASYNC_OK;
372}
373
374int ICC_Async_Receive (unsigned size, BYTE * data)
375{
376 IFD_Timings timings;
377
378 timings.block_timeout = icc_timings.block_timeout;
379 timings.char_timeout = icc_timings.char_timeout;
380
381#ifdef COOL
382 if (reader[ridx].typ == R_INTERNAL) {
383 if (!Cool_Receive(data, size))
384 return ICC_ASYNC_IFD_ERROR;
385 }
386 else
387#else
388 if (!Phoenix_Receive (data, size, &timings))
389 return ICC_ASYNC_IFD_ERROR;
390#endif
391
392 if (convention == ATR_CONVENTION_INVERSE && reader[ridx].typ != R_INTERNAL)
393 ICC_Async_InvertBuffer (size, data);
394
395 return ICC_ASYNC_OK;
396}
397
398int ICC_Async_Close (ICC_Async * icc)
399{
400#ifdef SCI_DEV
401 /* Dectivate ICC */
402 if (!Sci_Deactivate())
403 return ICC_ASYNC_IFD_ERROR;
404#endif
405
406 /* Delete atr */
407 ATR_Delete (atr);
408
409 ICC_Async_Clear (icc);
410
411 return ICC_ASYNC_OK;
412}
413
414unsigned long ICC_Async_GetClockRate ()
415{
416 switch (reader[ridx].cardmhz) {
417 case 357:
418 case 358:
419 return (372L * 9600L);
420 case 368:
421 return (384L * 9600L);
422 default:
423 return reader[ridx].cardmhz * 10000L;
424 }
425}
426
427void ICC_Async_Delete (ICC_Async * icc)
428{
429 free (icc);
430}
431
432/*
433 * Not exported functions definition
434 */
435
436static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
437{
438 uint i;
439
440 for (i = 0; i < size; i++)
441 buffer[i] = ~(INVERT_BYTE (buffer[i]));
442}
443
444static void ICC_Async_Clear (ICC_Async * icc)
445{
446 atr = NULL;
447 convention = 0;
448 protocol_type = -1;
449 icc_timings.block_delay = 0;
450 icc_timings.char_delay = 0;
451 icc_timings.block_timeout = 0;
452 icc_timings.char_timeout = 0;
453}
Note: See TracBrowser for help on using the repository browser.