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 "icc_async.h"
|
---|
27 | #include <stdlib.h>
|
---|
28 | #include <stdio.h>
|
---|
29 | #include <string.h>
|
---|
30 |
|
---|
31 | /*
|
---|
32 | * Not exported constants definition
|
---|
33 | */
|
---|
34 | #define ICC_ASYNC_MAX_TRANSMIT 255
|
---|
35 | #define ICC_ASYNC_BAUDRATE 9600
|
---|
36 |
|
---|
37 | /*
|
---|
38 | * Not exported functions declaration
|
---|
39 | */
|
---|
40 |
|
---|
41 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer);
|
---|
42 | static void ICC_Async_Clear (ICC_Async * icc);
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Exported functions definition
|
---|
46 | */
|
---|
47 |
|
---|
48 | ICC_Async *ICC_Async_New (void)
|
---|
49 | {
|
---|
50 | ICC_Async *icc;
|
---|
51 |
|
---|
52 | /* Allocate memory */
|
---|
53 | icc = (ICC_Async *) malloc (sizeof (ICC_Async));
|
---|
54 |
|
---|
55 | if (icc != NULL)
|
---|
56 | ICC_Async_Clear (icc);
|
---|
57 |
|
---|
58 | return icc;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int ICC_Async_Init (ICC_Async * icc, IFD * ifd)
|
---|
62 | {
|
---|
63 | #ifndef ICC_TYPE_SYNC
|
---|
64 | unsigned np=0;
|
---|
65 |
|
---|
66 | /* LED Red */
|
---|
67 | if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
|
---|
68 | return ICC_ASYNC_IFD_ERROR;
|
---|
69 |
|
---|
70 | /* Initialize Baudrate */
|
---|
71 | if (IFD_Towitoko_SetBaudrate (ifd, ICC_ASYNC_BAUDRATE)!= IFD_TOWITOKO_OK)
|
---|
72 | return ICC_ASYNC_IFD_ERROR;
|
---|
73 |
|
---|
74 | /* Activate ICC */
|
---|
75 | #ifdef SCI_DEV
|
---|
76 | if (IFD_Towitoko_ActivateICC (ifd) != IFD_TOWITOKO_OK)
|
---|
77 | return ICC_ASYNC_IFD_ERROR;
|
---|
78 | #endif
|
---|
79 | /* Reset ICC */
|
---|
80 | if (IFD_Towitoko_ResetAsyncICC (ifd, &(icc->atr)) != IFD_TOWITOKO_OK)
|
---|
81 | {
|
---|
82 | icc->atr = NULL;
|
---|
83 | return ICC_ASYNC_IFD_ERROR;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* Get ICC convention */
|
---|
87 | if (ATR_GetConvention (icc->atr, &(icc->convention)) != ATR_OK)
|
---|
88 | {
|
---|
89 | ATR_Delete (icc->atr);
|
---|
90 | icc->atr = NULL;
|
---|
91 | icc->convention = 0;
|
---|
92 |
|
---|
93 | return ICC_ASYNC_ATR_ERROR;
|
---|
94 | }
|
---|
95 |
|
---|
96 | icc->protocol_type = ATR_PROTOCOL_TYPE_T0;
|
---|
97 |
|
---|
98 | ATR_GetNumberOfProtocols (icc->atr, &np);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Get protocol offered by interface bytes T*2 if available,
|
---|
102 | * (that is, if TD1 is available), * otherwise use default T=0
|
---|
103 | */
|
---|
104 | /* if (np>1)
|
---|
105 | ATR_GetProtocolType (icc->atr, 1, &(icc->protocol_type));
|
---|
106 |
|
---|
107 | #ifdef DEBUG_ICC
|
---|
108 | printf("ICC: Detected %s convention processor card T=%d\n",(icc->convention == ATR_CONVENTION_DIRECT ? "direct" : "inverse"), icc->protocol_type);
|
---|
109 | #endif
|
---|
110 | *///really should let PPS handle this
|
---|
111 | /* LED Green */
|
---|
112 | if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
|
---|
113 | {
|
---|
114 | ATR_Delete (icc->atr);
|
---|
115 | icc->atr = NULL;
|
---|
116 | icc->convention = 0;
|
---|
117 |
|
---|
118 | return ICC_ASYNC_IFD_ERROR;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* Initialize member variables */
|
---|
122 | icc->baudrate = ICC_ASYNC_BAUDRATE;
|
---|
123 | icc->ifd = ifd;
|
---|
124 |
|
---|
125 | #ifdef NO_PAR_SWITCH
|
---|
126 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
127 | {
|
---|
128 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
|
---|
129 | return ICC_ASYNC_IFD_ERROR;
|
---|
130 | }
|
---|
131 | else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
132 | {
|
---|
133 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
134 | return ICC_ASYNC_IFD_ERROR;
|
---|
135 | }
|
---|
136 | else
|
---|
137 | {
|
---|
138 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
|
---|
139 | return ICC_ASYNC_IFD_ERROR;
|
---|
140 | }
|
---|
141 | IO_Serial_Flush(ifd->io);
|
---|
142 | #endif
|
---|
143 | return ICC_ASYNC_OK;
|
---|
144 | #else
|
---|
145 | return ICC_ASYNC_ATR_ERROR;
|
---|
146 | #endif
|
---|
147 | }
|
---|
148 |
|
---|
149 | int ICC_Async_SetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
|
---|
150 | {
|
---|
151 | icc->timings.block_delay = timings->block_delay;
|
---|
152 | icc->timings.char_delay = timings->char_delay;
|
---|
153 | icc->timings.block_timeout = timings->block_timeout;
|
---|
154 | icc->timings.char_timeout = timings->char_timeout;
|
---|
155 | /* if (icc->protocol_type == ATR_PROTOCOL_TYPE_T1)
|
---|
156 | 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);
|
---|
157 | else
|
---|
158 | 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);*/
|
---|
159 |
|
---|
160 | #ifdef SCI_DEV
|
---|
161 | #include <sys/ioctl.h>
|
---|
162 | #include "sci_global.h"
|
---|
163 | #include "sci_ioctl.h"
|
---|
164 | if (icc->ifd->io->com == RTYP_SCI) {
|
---|
165 | SCI_PARAMETERS params;
|
---|
166 | if (ioctl(icc->ifd->io->fd, IOCTL_GET_PARAMETERS, ¶ms) < 0 )
|
---|
167 | return ICC_ASYNC_IFD_ERROR;
|
---|
168 | switch (icc->protocol_type) {
|
---|
169 | case ATR_PROTOCOL_TYPE_T1:
|
---|
170 | params.BWT = icc->timings.block_timeout;
|
---|
171 | params.CWT = icc->timings.char_timeout;
|
---|
172 | //params.BGT = icc->timings.block_delay; load into params.EGT??
|
---|
173 | break;
|
---|
174 | case ATR_PROTOCOL_TYPE_T0:
|
---|
175 | case ATR_PROTOCOL_TYPE_T14:
|
---|
176 | default:
|
---|
177 | params.WWT = icc->timings.char_timeout;
|
---|
178 | break;
|
---|
179 | }
|
---|
180 | if (ioctl(icc->ifd->io->fd, IOCTL_SET_PARAMETERS, ¶ms)!=0)
|
---|
181 | return ICC_ASYNC_IFD_ERROR;
|
---|
182 |
|
---|
183 | 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);
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 | return ICC_ASYNC_OK;
|
---|
187 | }
|
---|
188 |
|
---|
189 | int ICC_Async_GetTimings (ICC_Async * icc, ICC_Async_Timings * timings)
|
---|
190 | {
|
---|
191 | timings->block_delay = icc->timings.block_delay;
|
---|
192 | timings->char_delay = icc->timings.char_delay;
|
---|
193 | timings->block_timeout = icc->timings.block_timeout;
|
---|
194 | timings->char_timeout = icc->timings.char_timeout;
|
---|
195 |
|
---|
196 | return ICC_ASYNC_OK;
|
---|
197 | }
|
---|
198 |
|
---|
199 | int ICC_Async_SetBaudrate (ICC_Async * icc, unsigned long baudrate)
|
---|
200 | {
|
---|
201 | icc->baudrate = baudrate;
|
---|
202 | if (IFD_Towitoko_SetBaudrate (icc->ifd, baudrate) != IFD_TOWITOKO_OK)
|
---|
203 | return ICC_ASYNC_IFD_ERROR;
|
---|
204 |
|
---|
205 | return ICC_ASYNC_OK;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int ICC_Async_GetBaudrate (ICC_Async * icc, unsigned long * baudrate)
|
---|
209 | {
|
---|
210 | (*baudrate) = icc->baudrate;
|
---|
211 | return ICC_ASYNC_OK;
|
---|
212 | }
|
---|
213 |
|
---|
214 | #ifndef NO_PAR_SWITCH
|
---|
215 | int ICC_Async_BeginTransmission (ICC_Async * icc)
|
---|
216 | {
|
---|
217 | /* Setup parity for this ICC */
|
---|
218 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
219 | {
|
---|
220 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_ODD) != IFD_TOWITOKO_OK)
|
---|
221 | return ICC_ASYNC_IFD_ERROR;
|
---|
222 | }
|
---|
223 | else if(icc->protocol_type == ATR_PROTOCOL_TYPE_T14)
|
---|
224 | {
|
---|
225 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
226 | return ICC_ASYNC_IFD_ERROR;
|
---|
227 | }
|
---|
228 | else
|
---|
229 | {
|
---|
230 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_EVEN) != IFD_TOWITOKO_OK)
|
---|
231 | return ICC_ASYNC_IFD_ERROR;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /* Setup baudrate for this ICC */
|
---|
235 | /* if (IFD_Towitoko_SetBaudrate (icc->ifd, icc->baudrate)!= IFD_TOWITOKO_OK)
|
---|
236 | return ICC_ASYNC_IFD_ERROR;
|
---|
237 | */
|
---|
238 | return ICC_ASYNC_OK;
|
---|
239 | }
|
---|
240 |
|
---|
241 | int ICC_Async_EndTransmission (ICC_Async * icc)
|
---|
242 | {
|
---|
243 | /* Restore parity */
|
---|
244 | if (IFD_Towitoko_SetParity (icc->ifd, IFD_TOWITOKO_PARITY_NONE) != IFD_TOWITOKO_OK)
|
---|
245 | return ICC_ASYNC_IFD_ERROR;
|
---|
246 |
|
---|
247 | return ICC_ASYNC_OK;
|
---|
248 | }
|
---|
249 | #endif
|
---|
250 |
|
---|
251 | int ICC_Async_Transmit (ICC_Async * icc, unsigned size, BYTE * data)
|
---|
252 | {
|
---|
253 | BYTE *buffer = NULL, *sent;
|
---|
254 | IFD_Timings timings;
|
---|
255 |
|
---|
256 | if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
|
---|
257 | {
|
---|
258 | buffer = (BYTE *) calloc(sizeof (BYTE), size);
|
---|
259 | memcpy (buffer, data, size);
|
---|
260 | ICC_Async_InvertBuffer (size, buffer);
|
---|
261 | sent = buffer;
|
---|
262 | }
|
---|
263 | else
|
---|
264 | {
|
---|
265 | sent = data;
|
---|
266 | }
|
---|
267 |
|
---|
268 | timings.block_delay = icc->timings.block_delay;
|
---|
269 | timings.char_delay = icc->timings.char_delay;
|
---|
270 |
|
---|
271 | if (IFD_Towitoko_Transmit (icc->ifd, &timings, size, sent) != IFD_TOWITOKO_OK)
|
---|
272 | return ICC_ASYNC_IFD_ERROR;
|
---|
273 |
|
---|
274 | if (icc->convention == ATR_CONVENTION_INVERSE)
|
---|
275 | free (buffer);
|
---|
276 |
|
---|
277 | return ICC_ASYNC_OK;
|
---|
278 | }
|
---|
279 |
|
---|
280 | int ICC_Async_Receive (ICC_Async * icc, unsigned size, BYTE * data)
|
---|
281 | {
|
---|
282 | IFD_Timings timings;
|
---|
283 |
|
---|
284 | timings.block_timeout = icc->timings.block_timeout;
|
---|
285 | timings.char_timeout = icc->timings.char_timeout;
|
---|
286 |
|
---|
287 | if (IFD_Towitoko_Receive (icc->ifd, &timings, size, data) != IFD_TOWITOKO_OK)
|
---|
288 | return ICC_ASYNC_IFD_ERROR;
|
---|
289 |
|
---|
290 | if (icc->convention == ATR_CONVENTION_INVERSE && icc->ifd->io->com!=RTYP_SCI)
|
---|
291 | ICC_Async_InvertBuffer (size, data);
|
---|
292 |
|
---|
293 | return ICC_ASYNC_OK;
|
---|
294 | }
|
---|
295 |
|
---|
296 | ATR * ICC_Async_GetAtr (ICC_Async * icc)
|
---|
297 | {
|
---|
298 | return icc->atr;
|
---|
299 | }
|
---|
300 |
|
---|
301 | IFD * ICC_Async_GetIFD (ICC_Async * icc)
|
---|
302 | {
|
---|
303 | return icc->ifd;
|
---|
304 | }
|
---|
305 |
|
---|
306 | int ICC_Async_Close (ICC_Async * icc)
|
---|
307 | {
|
---|
308 | /* Dectivate ICC */
|
---|
309 | #ifdef SCI_DEV
|
---|
310 | if (IFD_Towitoko_DeactivateICC (icc->ifd) != IFD_TOWITOKO_OK)
|
---|
311 | return ICC_ASYNC_IFD_ERROR;
|
---|
312 | #endif
|
---|
313 |
|
---|
314 | /* LED Off */
|
---|
315 | if (IFD_Towitoko_SetLED () != IFD_TOWITOKO_OK)
|
---|
316 | return ICC_ASYNC_IFD_ERROR;
|
---|
317 |
|
---|
318 | /* Delete atr */
|
---|
319 | ATR_Delete (icc->atr);
|
---|
320 |
|
---|
321 | ICC_Async_Clear (icc);
|
---|
322 |
|
---|
323 | return ICC_ASYNC_OK;
|
---|
324 | }
|
---|
325 |
|
---|
326 | unsigned long ICC_Async_GetClockRate (ICC_Async * icc)
|
---|
327 | {
|
---|
328 | switch (icc->ifd->io->cardmhz) {
|
---|
329 | case 357:
|
---|
330 | case 358:
|
---|
331 | return (372L * 9600L);
|
---|
332 | case 368:
|
---|
333 | return (384L * 9600L);
|
---|
334 | default:
|
---|
335 | return icc->ifd->io->cardmhz * 10000L;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | void ICC_Async_Delete (ICC_Async * icc)
|
---|
340 | {
|
---|
341 | free (icc);
|
---|
342 | }
|
---|
343 |
|
---|
344 | /*
|
---|
345 | * Not exported functions definition
|
---|
346 | */
|
---|
347 |
|
---|
348 | static void ICC_Async_InvertBuffer (unsigned size, BYTE * buffer)
|
---|
349 | {
|
---|
350 | int i;
|
---|
351 |
|
---|
352 | for (i = 0; i < size; i++)
|
---|
353 | buffer[i] = ~(INVERT_BYTE (buffer[i]));
|
---|
354 | }
|
---|
355 |
|
---|
356 | static void ICC_Async_Clear (ICC_Async * icc)
|
---|
357 | {
|
---|
358 | icc->ifd = NULL;
|
---|
359 | icc->atr = NULL;
|
---|
360 | icc->baudrate = 0L;
|
---|
361 | icc->convention = 0;
|
---|
362 | icc->protocol_type = -1;
|
---|
363 | icc->timings.block_delay = 0;
|
---|
364 | icc->timings.char_delay = 0;
|
---|
365 | icc->timings.block_timeout = 0;
|
---|
366 | icc->timings.char_timeout = 0;
|
---|
367 | }
|
---|