1 | /*
|
---|
2 | ifd_towitoko.c
|
---|
3 | This module provides IFD 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 <stdio.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #ifdef OS_HPUX
|
---|
30 | #include <sys/modem.h>
|
---|
31 | #endif
|
---|
32 | #include <termios.h>
|
---|
33 | #include <unistd.h>
|
---|
34 | #include <sys/stat.h>
|
---|
35 | #include <fcntl.h>
|
---|
36 | #ifdef HAVE_POLL
|
---|
37 | #include <sys/poll.h>
|
---|
38 | #else
|
---|
39 | #include <sys/signal.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <sys/time.h>
|
---|
42 | #endif
|
---|
43 | #include <sys/ioctl.h>
|
---|
44 | #include <time.h>
|
---|
45 | #include "ifd_towitoko.h"
|
---|
46 | #include "io_serial.h"
|
---|
47 | #include "sci_global.h"
|
---|
48 | #include "sci_ioctl.h"
|
---|
49 | #include "ifd.h"
|
---|
50 | #include "../globals.h"
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Not exported constants
|
---|
54 | */
|
---|
55 |
|
---|
56 | #define IFD_TOWITOKO_TIMEOUT 1000
|
---|
57 | #define IFD_TOWITOKO_DELAY 0
|
---|
58 | #define IFD_TOWITOKO_BAUDRATE 9600
|
---|
59 | //#define IFD_TOWITOKO_PS 15
|
---|
60 | #define IFD_TOWITOKO_MAX_TRANSMIT 255
|
---|
61 | //#define IFD_TOWITOKO_ATR_TIMEOUT 200
|
---|
62 | //#define IFD_TOWITOKO_ATR_TIMEOUT 400
|
---|
63 | #define IFD_TOWITOKO_ATR_TIMEOUT 800
|
---|
64 | #define IFD_TOWITOKO_ATR_MIN_LENGTH 1
|
---|
65 | #define IFD_TOWITOKO_CLOCK_RATE (625L * 9600L)
|
---|
66 | //#define IFD_TOWITOKO_CLOCK_RATE (372L * 9600L)
|
---|
67 |
|
---|
68 | #define HI(a) (((a) & 0xff00) >> 8)
|
---|
69 | #define LO(a) ((a) & 0x00ff)
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Not exported functions declaration
|
---|
73 | */
|
---|
74 |
|
---|
75 | static int IFD_Towitoko_GetReaderInfo (IFD * ifd);
|
---|
76 | static void IFD_Towitoko_Clear (IFD * ifd);
|
---|
77 |
|
---|
78 | #ifdef USE_GPIO
|
---|
79 |
|
---|
80 | int gpio_outen,gpio_out,gpio_in;
|
---|
81 | unsigned int pin,gpio;
|
---|
82 | int gpio_detect=0;
|
---|
83 |
|
---|
84 | static void set_gpio(int level)
|
---|
85 | {
|
---|
86 | read(gpio_outen, &gpio, sizeof(gpio));
|
---|
87 | gpio |= pin;
|
---|
88 | write(gpio_outen, &gpio, sizeof(gpio));
|
---|
89 |
|
---|
90 | read(gpio_out, &gpio, sizeof(gpio));
|
---|
91 | if (level>0)
|
---|
92 | gpio|=pin;
|
---|
93 | else
|
---|
94 | gpio&=~pin;
|
---|
95 | write(gpio_out, &gpio, sizeof(gpio));
|
---|
96 | }
|
---|
97 |
|
---|
98 | static void set_gpio1(int level)
|
---|
99 | {
|
---|
100 | read(gpio_outen, &gpio, sizeof(gpio));
|
---|
101 | gpio |= 2;
|
---|
102 | write(gpio_outen, &gpio, sizeof(gpio));
|
---|
103 |
|
---|
104 | read(gpio_out, &gpio, sizeof(gpio));
|
---|
105 | if (level>0)
|
---|
106 | gpio|=2;
|
---|
107 | else
|
---|
108 | gpio&=~2;
|
---|
109 | write(gpio_out, &gpio, sizeof(gpio));
|
---|
110 | }
|
---|
111 |
|
---|
112 | static void set_gpio_input(void)
|
---|
113 | {
|
---|
114 | read(gpio_outen, &gpio, sizeof(gpio));
|
---|
115 | gpio &= ~pin;
|
---|
116 | write(gpio_outen, &gpio, sizeof(gpio));
|
---|
117 | }
|
---|
118 |
|
---|
119 | static int get_gpio(void)
|
---|
120 | {
|
---|
121 | set_gpio_input();
|
---|
122 | read(gpio_in, &gpio, sizeof(gpio));
|
---|
123 | return ((int)((gpio&pin)?1:0));
|
---|
124 | }
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Exported functions definition
|
---|
129 | */
|
---|
130 |
|
---|
131 | IFD * IFD_Towitoko_New ()
|
---|
132 | {
|
---|
133 | IFD *ifd;
|
---|
134 |
|
---|
135 | ifd = (IFD *) malloc (sizeof (IFD));
|
---|
136 |
|
---|
137 | if (ifd != NULL)
|
---|
138 | IFD_Towitoko_Clear (ifd);
|
---|
139 |
|
---|
140 | return ifd;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void IFD_Towitoko_Delete (IFD * ifd)
|
---|
144 | {
|
---|
145 | free (ifd);
|
---|
146 | }
|
---|
147 |
|
---|
148 | int IFD_Towitoko_Init (IFD * ifd, IO_Serial * io, BYTE slot)
|
---|
149 | {
|
---|
150 | int ret;
|
---|
151 |
|
---|
152 | #ifdef USE_GPIO
|
---|
153 | extern int oscam_card_detect;
|
---|
154 | if (oscam_card_detect>4)
|
---|
155 | {
|
---|
156 | gpio_detect=oscam_card_detect-3;
|
---|
157 | pin = 1<<gpio_detect;
|
---|
158 | gpio_outen=open("/dev/gpio/outen",O_RDWR);
|
---|
159 | gpio_out=open("/dev/gpio/out",O_RDWR);
|
---|
160 | gpio_in=open("/dev/gpio/in",O_RDWR);
|
---|
161 | set_gpio_input();
|
---|
162 | }
|
---|
163 | #endif
|
---|
164 |
|
---|
165 | #ifdef DEBUG_IFD
|
---|
166 | printf ("IFD: Initializing slot number %d, com=%d\n", slot, io->reader_type);
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | // if ((slot != IFD_TOWITOKO_SLOT_MULTICAM) && (slot != IFD_TOWITOKO_SLOT_A) && (slot != IFD_TOWITOKO_SLOT_B))
|
---|
170 | if (slot != IFD_TOWITOKO_SLOT_MULTICAM )
|
---|
171 | return IFD_TOWITOKO_PARAM_ERROR;
|
---|
172 |
|
---|
173 | if(io->reader_type==R_INTERNAL)
|
---|
174 | {
|
---|
175 | ifd->io = io;
|
---|
176 | ifd->slot = slot;
|
---|
177 | ifd->type = IFD_TOWITOKO_MULTICAM;
|
---|
178 | return IFD_TOWITOKO_OK;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /* Default serial port settings */
|
---|
183 | io->input_bitrate = IFD_TOWITOKO_BAUDRATE;
|
---|
184 | io->output_bitrate = IFD_TOWITOKO_BAUDRATE;
|
---|
185 | io->bits = 8;
|
---|
186 | io->stopbits = 2;
|
---|
187 | io->parity = IO_SERIAL_PARITY_EVEN;
|
---|
188 | io->dtr = IO_SERIAL_HIGH;
|
---|
189 | // io->dtr = IO_SERIAL_LOW;
|
---|
190 | // io->rts = IO_SERIAL_HIGH;
|
---|
191 | io->rts = IO_SERIAL_LOW;
|
---|
192 |
|
---|
193 |
|
---|
194 | if (!IO_Serial_SetProperties (io))
|
---|
195 | return IFD_TOWITOKO_IO_ERROR;
|
---|
196 |
|
---|
197 | /* Default ifd settings */
|
---|
198 |
|
---|
199 | ifd->io = io;
|
---|
200 | ifd->slot = slot;
|
---|
201 | ifd->type = IFD_TOWITOKO_MULTICAM;
|
---|
202 |
|
---|
203 | ret = IFD_Towitoko_SetBaudrate (ifd, IFD_TOWITOKO_BAUDRATE);
|
---|
204 |
|
---|
205 | if (ret != IFD_TOWITOKO_OK)
|
---|
206 | {
|
---|
207 | IFD_Towitoko_Clear (ifd);
|
---|
208 | return ret;
|
---|
209 | }
|
---|
210 |
|
---|
211 | ret = IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_EVEN);
|
---|
212 |
|
---|
213 | if (ret != IFD_TOWITOKO_OK)
|
---|
214 | {
|
---|
215 | IFD_Towitoko_Clear (ifd);
|
---|
216 | return ret;
|
---|
217 | }
|
---|
218 |
|
---|
219 | ret = IFD_Towitoko_GetReaderInfo (ifd);
|
---|
220 |
|
---|
221 | if (ret != IFD_TOWITOKO_OK)
|
---|
222 | {
|
---|
223 | IFD_Towitoko_Clear (ifd);
|
---|
224 | }
|
---|
225 | else
|
---|
226 | {
|
---|
227 | IO_Serial_Flush(ifd->io);
|
---|
228 | }
|
---|
229 |
|
---|
230 | return ret;
|
---|
231 | }
|
---|
232 |
|
---|
233 | int IFD_Towitoko_Close (IFD * ifd)
|
---|
234 | {
|
---|
235 | int ret;
|
---|
236 |
|
---|
237 | #ifdef USE_GPIO
|
---|
238 | if(gpio_detect)
|
---|
239 | {
|
---|
240 | close(gpio_outen);
|
---|
241 | close(gpio_out);
|
---|
242 | close(gpio_in);
|
---|
243 | }
|
---|
244 | #endif
|
---|
245 |
|
---|
246 | #ifdef DEBUG_IFD
|
---|
247 | printf ("IFD: Closing slot number %d\n", ifd->slot);
|
---|
248 | #endif
|
---|
249 |
|
---|
250 | ret = IFD_Towitoko_SetLED ();
|
---|
251 | if (ret != IFD_TOWITOKO_OK)
|
---|
252 | return ret;
|
---|
253 |
|
---|
254 | IFD_Towitoko_Clear (ifd);
|
---|
255 |
|
---|
256 |
|
---|
257 | return IFD_TOWITOKO_OK;
|
---|
258 | }
|
---|
259 |
|
---|
260 | int IFD_Towitoko_SetBaudrate (IFD * ifd, unsigned long baudrate)
|
---|
261 | {
|
---|
262 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
263 | {
|
---|
264 | return IFD_TOWITOKO_OK;
|
---|
265 | }
|
---|
266 | /*
|
---|
267 | if (IFD_Towitoko_GetMaxBaudrate () < baudrate)
|
---|
268 | {
|
---|
269 | #ifdef DEBUG_IFD
|
---|
270 | printf ("IFD: Tried to set unsupported baudrate: %lu", baudrate);
|
---|
271 | #endif
|
---|
272 | return IFD_TOWITOKO_PARAM_ERROR;
|
---|
273 | }*/
|
---|
274 |
|
---|
275 | #ifdef DEBUG_IFD
|
---|
276 | printf ("IFD: Setting baudrate to %lu\n", baudrate);
|
---|
277 | #endif
|
---|
278 | /* Get current settings */
|
---|
279 | if (!IO_Serial_GetProperties (ifd->io))
|
---|
280 | return IFD_TOWITOKO_IO_ERROR;
|
---|
281 |
|
---|
282 | if (ifd->io->output_bitrate == baudrate)
|
---|
283 | return IFD_TOWITOKO_OK;
|
---|
284 |
|
---|
285 |
|
---|
286 | /* Set serial device bitrate */
|
---|
287 | ifd->io->output_bitrate = baudrate;
|
---|
288 | ifd->io->input_bitrate = baudrate;
|
---|
289 |
|
---|
290 | if (!IO_Serial_SetProperties (ifd->io))
|
---|
291 | return IFD_TOWITOKO_IO_ERROR;
|
---|
292 |
|
---|
293 | return IFD_TOWITOKO_OK;
|
---|
294 | }
|
---|
295 |
|
---|
296 | int IFD_Towitoko_GetBaudrate (IFD * ifd, unsigned long *baudrate)
|
---|
297 | {
|
---|
298 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
299 | {
|
---|
300 | return IFD_TOWITOKO_OK;
|
---|
301 | }
|
---|
302 |
|
---|
303 | /* Get current settings */
|
---|
304 | if (!IO_Serial_GetProperties (ifd->io))
|
---|
305 | return IFD_TOWITOKO_IO_ERROR;
|
---|
306 |
|
---|
307 | (*baudrate) = ifd->io->output_bitrate;
|
---|
308 |
|
---|
309 | return IFD_TOWITOKO_OK;
|
---|
310 | }
|
---|
311 |
|
---|
312 | extern int IFD_Towitoko_SetParity (IFD * ifd, BYTE parity)
|
---|
313 | {
|
---|
314 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
315 | {
|
---|
316 | return IFD_TOWITOKO_OK;
|
---|
317 | }
|
---|
318 |
|
---|
319 | #ifdef DEBUG_IFD
|
---|
320 | printf ("IFD: Parity = %s\n",
|
---|
321 | parity == IFD_TOWITOKO_PARITY_ODD ? "Odd" :
|
---|
322 | parity == IFD_TOWITOKO_PARITY_EVEN ? "Even" : "Invalid");
|
---|
323 | #endif
|
---|
324 |
|
---|
325 | if ((parity != IFD_TOWITOKO_PARITY_EVEN) && (parity != IFD_TOWITOKO_PARITY_ODD) && (parity != IFD_TOWITOKO_PARITY_NONE))
|
---|
326 | return IFD_TOWITOKO_PARAM_ERROR;
|
---|
327 |
|
---|
328 | /* Get current settings */
|
---|
329 | if (!IO_Serial_GetProperties (ifd->io))
|
---|
330 | return IFD_TOWITOKO_IO_ERROR;
|
---|
331 |
|
---|
332 | if (ifd->io->parity !=parity)
|
---|
333 | {
|
---|
334 | ifd->io->parity = parity;
|
---|
335 |
|
---|
336 | if (!IO_Serial_SetProperties (ifd->io))
|
---|
337 | return IFD_TOWITOKO_IO_ERROR;
|
---|
338 | }
|
---|
339 |
|
---|
340 | return IFD_TOWITOKO_OK;
|
---|
341 | }
|
---|
342 |
|
---|
343 | int IFD_Towitoko_SetLED ()
|
---|
344 | {
|
---|
345 | return IFD_TOWITOKO_OK;
|
---|
346 | }
|
---|
347 |
|
---|
348 | int IFD_Towitoko_GetStatus (IFD * ifd, BYTE * result)
|
---|
349 | {
|
---|
350 | BYTE status[2];
|
---|
351 | unsigned int modembits=0;
|
---|
352 | int in;
|
---|
353 |
|
---|
354 | // printf("\n%08X\n", (int)ifd->io);
|
---|
355 |
|
---|
356 | // status : 0 -start, 1 - card, 2- no card
|
---|
357 |
|
---|
358 | #ifdef SCI_DEV
|
---|
359 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
360 | {
|
---|
361 | if(!Sci_GetStatus(ifd->io->fd, &in))
|
---|
362 | return IFD_TOWITOKO_IO_ERROR;
|
---|
363 | }
|
---|
364 | else
|
---|
365 | #elif COOL
|
---|
366 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
367 | {
|
---|
368 | if (!Cool_GetStatus(&in))
|
---|
369 | return IFD_TOWITOKO_IO_ERROR;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | #endif
|
---|
373 |
|
---|
374 | #if defined(TUXBOX) && defined(PPC)
|
---|
375 | if ((ifd->io->reader_type==R_DB2COM1) || (ifd->io->reader_type==R_DB2COM2))
|
---|
376 | {
|
---|
377 | ushort msr=1;
|
---|
378 | extern int fdmc;
|
---|
379 | IO_Serial_Ioctl_Lock(ifd->io, 1);
|
---|
380 | ioctl(fdmc, GET_PCDAT, &msr);
|
---|
381 | if (ifd->io->reader_type==R_DB2COM2)
|
---|
382 | in=(!(msr & 1));
|
---|
383 | else
|
---|
384 | in=((msr & 0x0f00) == 0x0f00);
|
---|
385 | IO_Serial_Ioctl_Lock(ifd->io, 0);
|
---|
386 | }
|
---|
387 | else
|
---|
388 | #endif
|
---|
389 | #ifdef USE_GPIO
|
---|
390 | if (gpio_detect)
|
---|
391 | in=get_gpio();
|
---|
392 | else
|
---|
393 | #endif
|
---|
394 | {
|
---|
395 | extern int oscam_card_detect;
|
---|
396 | if (ioctl(ifd->io->fd, TIOCMGET,&modembits)<0)
|
---|
397 | return IFD_TOWITOKO_IO_ERROR;
|
---|
398 | switch(oscam_card_detect&0x7f)
|
---|
399 | {
|
---|
400 | case 0: in=(modembits & TIOCM_CAR); break;
|
---|
401 | case 1: in=(modembits & TIOCM_DSR); break;
|
---|
402 | case 2: in=(modembits & TIOCM_CTS); break;
|
---|
403 | case 3: in=(modembits & TIOCM_RNG); break;
|
---|
404 | default: in=0; // dummy
|
---|
405 | }
|
---|
406 | if (!(oscam_card_detect&0x80))
|
---|
407 | in=!in;
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (in)
|
---|
411 | {
|
---|
412 | if(ifd->status == 0)
|
---|
413 | {
|
---|
414 | status[0] = IFD_TOWITOKO_CARD_CHANGE;
|
---|
415 | ifd->status = 1;
|
---|
416 | #ifdef USE_GPIO
|
---|
417 | if (gpio_detect) set_gpio1(0);
|
---|
418 | #endif
|
---|
419 | }
|
---|
420 | else if(ifd->status == 1)
|
---|
421 | {
|
---|
422 | status[0] = IFD_TOWITOKO_CARD_NOCHANGE;
|
---|
423 | }
|
---|
424 | else
|
---|
425 | {
|
---|
426 | status[0] = IFD_TOWITOKO_CARD_CHANGE;
|
---|
427 | ifd->status = 1;
|
---|
428 | #ifdef USE_GPIO
|
---|
429 | if (gpio_detect) set_gpio1(0);
|
---|
430 | #endif
|
---|
431 | }
|
---|
432 | }
|
---|
433 | else
|
---|
434 | {
|
---|
435 | if(ifd->status == 0)
|
---|
436 | {
|
---|
437 | status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
|
---|
438 | ifd->status = 2;
|
---|
439 | #ifdef USE_GPIO
|
---|
440 | if (gpio_detect) set_gpio1(1);
|
---|
441 | #endif
|
---|
442 | }
|
---|
443 | else if(ifd->status == 1)
|
---|
444 | {
|
---|
445 | status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
|
---|
446 | ifd->status = 2;
|
---|
447 | #ifdef USE_GPIO
|
---|
448 | if (gpio_detect) set_gpio1(1);
|
---|
449 | #endif
|
---|
450 | }
|
---|
451 | else
|
---|
452 | {
|
---|
453 | status[0] = IFD_TOWITOKO_NOCARD_NOCHANGE;
|
---|
454 | }
|
---|
455 | }
|
---|
456 |
|
---|
457 |
|
---|
458 | (*result) = status[0];
|
---|
459 |
|
---|
460 | #ifdef DEBUG_IFD
|
---|
461 | printf ("IFD: com%d Status = %s / %s\n", ifd->io->reader_type, IFD_TOWITOKO_CARD(status[0])? "card": "no card", IFD_TOWITOKO_CHANGE(status[0])? "change": "no change");
|
---|
462 | #endif
|
---|
463 |
|
---|
464 | return IFD_TOWITOKO_OK;
|
---|
465 | }
|
---|
466 |
|
---|
467 | int IFD_Towitoko_ActivateICC (IFD * ifd)
|
---|
468 | {
|
---|
469 | #ifdef DEBUG_IFD
|
---|
470 | printf ("IFD: Activating card\n");
|
---|
471 | #endif
|
---|
472 | #ifdef SCI_DEV
|
---|
473 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
474 | {
|
---|
475 | int in;
|
---|
476 |
|
---|
477 | #if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
|
---|
478 | if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
|
---|
479 | #else
|
---|
480 | if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
|
---|
481 | #endif
|
---|
482 | return IFD_TOWITOKO_IO_ERROR;
|
---|
483 |
|
---|
484 | if(in)
|
---|
485 | {
|
---|
486 | struct timespec req_ts;
|
---|
487 | req_ts.tv_sec = 0;
|
---|
488 | req_ts.tv_nsec = 50000000;
|
---|
489 | nanosleep (&req_ts, NULL);
|
---|
490 | return IFD_TOWITOKO_OK;
|
---|
491 | }
|
---|
492 | else
|
---|
493 | {
|
---|
494 | return IFD_TOWITOKO_IO_ERROR;
|
---|
495 | }
|
---|
496 | }
|
---|
497 | else
|
---|
498 | #endif
|
---|
499 | {
|
---|
500 | return IFD_TOWITOKO_OK;
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | int IFD_Towitoko_DeactivateICC (IFD * ifd)
|
---|
505 | {
|
---|
506 | #ifdef DEBUG_IFD
|
---|
507 | printf ("IFD: Deactivating card\n");
|
---|
508 | #endif
|
---|
509 |
|
---|
510 | #ifdef SCI_DEV
|
---|
511 | if(ifd->io->reader_type==R_INTERNAL)
|
---|
512 | {
|
---|
513 | int in;
|
---|
514 |
|
---|
515 | #if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
|
---|
516 | if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
|
---|
517 | #else
|
---|
518 | if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
|
---|
519 | #endif
|
---|
520 | return IFD_TOWITOKO_IO_ERROR;
|
---|
521 |
|
---|
522 | if(in)
|
---|
523 | {
|
---|
524 | if(ioctl(ifd->io->fd, IOCTL_SET_DEACTIVATE)<0)
|
---|
525 | return IFD_TOWITOKO_IO_ERROR;
|
---|
526 | }
|
---|
527 |
|
---|
528 |
|
---|
529 | }
|
---|
530 | #endif
|
---|
531 |
|
---|
532 | return IFD_TOWITOKO_OK;
|
---|
533 | }
|
---|
534 |
|
---|
535 | //extern void print_hex_data(unsigned char *data, int len);
|
---|
536 |
|
---|
537 | int IFD_Towitoko_ResetAsyncICC (IFD * ifd, ATR ** atr)
|
---|
538 | {
|
---|
539 |
|
---|
540 | #ifdef DEBUG_IFD
|
---|
541 | printf ("IFD: Resetting card:\n");
|
---|
542 | #endif
|
---|
543 |
|
---|
544 | int ret;
|
---|
545 | int parity;
|
---|
546 | int i;
|
---|
547 | int par[3] = {IFD_TOWITOKO_PARITY_EVEN, IFD_TOWITOKO_PARITY_ODD, IFD_TOWITOKO_PARITY_NONE};
|
---|
548 | #ifdef HAVE_NANOSLEEP
|
---|
549 | struct timespec req_ts;
|
---|
550 | req_ts.tv_sec = 0;
|
---|
551 | req_ts.tv_nsec = 50000000;
|
---|
552 | #endif
|
---|
553 |
|
---|
554 | (*atr) = NULL;
|
---|
555 | for(i=0; i<3; i++)
|
---|
556 | {
|
---|
557 | parity = par[i];
|
---|
558 | IO_Serial_Flush(ifd->io);
|
---|
559 |
|
---|
560 | ret = IFD_Towitoko_SetParity (ifd, parity);
|
---|
561 | if (ret != IFD_TOWITOKO_OK)
|
---|
562 | return ret;
|
---|
563 |
|
---|
564 | ret = IFD_TOWITOKO_IO_ERROR;
|
---|
565 |
|
---|
566 | IO_Serial_Ioctl_Lock(ifd->io, 1);
|
---|
567 | #ifdef USE_GPIO
|
---|
568 | if (gpio_detect)
|
---|
569 | {
|
---|
570 | set_gpio(0);
|
---|
571 | set_gpio1(0);
|
---|
572 | }
|
---|
573 | else
|
---|
574 | #endif
|
---|
575 | IO_Serial_RTS_Set(ifd->io);
|
---|
576 |
|
---|
577 | #ifdef HAVE_NANOSLEEP
|
---|
578 | nanosleep (&req_ts, NULL);
|
---|
579 | #else
|
---|
580 | usleep (50000L);
|
---|
581 | #endif
|
---|
582 | #ifdef USE_GPIO
|
---|
583 | if (gpio_detect)
|
---|
584 | {
|
---|
585 | set_gpio_input();
|
---|
586 | set_gpio1(1);
|
---|
587 | }
|
---|
588 | else
|
---|
589 | #endif
|
---|
590 | IO_Serial_RTS_Clr(ifd->io);
|
---|
591 |
|
---|
592 | IO_Serial_Ioctl_Lock(ifd->io, 0);
|
---|
593 |
|
---|
594 | (*atr) = ATR_New ();
|
---|
595 |
|
---|
596 | if(ATR_InitFromStream ((*atr), ifd->io, IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
|
---|
597 | ret = IFD_TOWITOKO_OK;
|
---|
598 |
|
---|
599 | /* Succesfully retrive ATR */
|
---|
600 | if (ret == IFD_TOWITOKO_OK)
|
---|
601 | {
|
---|
602 | break;
|
---|
603 | }
|
---|
604 | else
|
---|
605 | {
|
---|
606 | ATR_Delete (*atr);
|
---|
607 | (*atr) = NULL;
|
---|
608 | #ifdef USE_GPIO
|
---|
609 | if (gpio_detect) set_gpio1(0);
|
---|
610 | #endif
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | IO_Serial_Flush(ifd->io);
|
---|
615 | #ifndef NO_PAR_SWITCH
|
---|
616 | IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_NONE);
|
---|
617 | #endif
|
---|
618 |
|
---|
619 | /*
|
---|
620 | //PLAYGROUND faking ATR for test purposes only
|
---|
621 | //
|
---|
622 | // sky 919 unsigned char atr_test[] = { 0x3F, 0xFF, 0x13, 0x25, 0x03, 0x10, 0x80, 0x33, 0xB0, 0x0E, 0x69, 0xFF, 0x4A, 0x50, 0x70, 0x00, 0x00, 0x49, 0x54, 0x02, 0x00, 0x00 };
|
---|
623 | // HD+ unsigned char atr_test[] = { 0x3F, 0xFF, 0x95, 0x00, 0xFF, 0x91, 0x81, 0x71, 0xFE, 0x47, 0x00, 0x44, 0x4E, 0x41, 0x53, 0x50, 0x31, 0x34, 0x32, 0x20, 0x52, 0x65, 0x76, 0x47, 0x43, 0x34, 0x63 };
|
---|
624 | // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
|
---|
625 | //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
|
---|
626 | ATR_Delete(*atr); //throw away actual ATR
|
---|
627 | (*atr) = ATR_New ();
|
---|
628 | ATR_InitFromArray ((*atr), atr_test, sizeof(atr_test));
|
---|
629 | //END OF PLAYGROUND
|
---|
630 | */
|
---|
631 |
|
---|
632 | return ret;
|
---|
633 | }
|
---|
634 |
|
---|
635 | int IFD_Towitoko_Transmit (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
|
---|
636 | {
|
---|
637 | unsigned block_delay, char_delay, sent=0, to_send = 0;
|
---|
638 |
|
---|
639 | #ifdef DEBUG_IFD
|
---|
640 | printf ("IFD: Transmit: ");
|
---|
641 | for (sent = 0; sent < size; sent++)
|
---|
642 | printf ("%X ", buffer[sent]);
|
---|
643 | printf ("\n");
|
---|
644 | #endif
|
---|
645 |
|
---|
646 |
|
---|
647 | /* Calculate delays */
|
---|
648 | char_delay = IFD_TOWITOKO_DELAY + timings->char_delay;
|
---|
649 | block_delay = IFD_TOWITOKO_DELAY + timings->block_delay;
|
---|
650 |
|
---|
651 | #ifdef USE_GPIO
|
---|
652 | if (gpio_detect) set_gpio1(0);
|
---|
653 | #endif
|
---|
654 | for (sent = 0; sent < size; sent = sent + to_send)
|
---|
655 | {
|
---|
656 | /* Calculate number of bytes to send */
|
---|
657 | to_send = MIN(size, IFD_TOWITOKO_MAX_TRANSMIT);
|
---|
658 |
|
---|
659 | /* Send data */
|
---|
660 | if ((sent == 0) && (block_delay != char_delay))
|
---|
661 | {
|
---|
662 | if (!IO_Serial_Write (ifd->io, block_delay, 1, buffer))
|
---|
663 | return IFD_TOWITOKO_IO_ERROR;
|
---|
664 |
|
---|
665 | if (!IO_Serial_Write (ifd->io, char_delay, to_send-1, buffer+1))
|
---|
666 | return IFD_TOWITOKO_IO_ERROR;
|
---|
667 | }
|
---|
668 | else
|
---|
669 | {
|
---|
670 | if (!IO_Serial_Write (ifd->io, char_delay, to_send, buffer+sent))
|
---|
671 | return IFD_TOWITOKO_IO_ERROR;
|
---|
672 | }
|
---|
673 | }
|
---|
674 | #ifdef USE_GPIO
|
---|
675 | if (gpio_detect) set_gpio1(1);
|
---|
676 | #endif
|
---|
677 | return IFD_TOWITOKO_OK;
|
---|
678 | }
|
---|
679 |
|
---|
680 | int IFD_Towitoko_Receive (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
|
---|
681 | {
|
---|
682 | unsigned char_timeout, block_timeout;
|
---|
683 | #ifdef DEBUG_IFD
|
---|
684 | int i;
|
---|
685 | #endif
|
---|
686 |
|
---|
687 | /* Calculate timeouts */
|
---|
688 | char_timeout = IFD_TOWITOKO_TIMEOUT + timings->char_timeout;
|
---|
689 | block_timeout = IFD_TOWITOKO_TIMEOUT + timings->block_timeout;
|
---|
690 | #ifdef USE_GPIO
|
---|
691 | if (gpio_detect) set_gpio1(0);
|
---|
692 | #endif
|
---|
693 | if (block_timeout != char_timeout)
|
---|
694 | {
|
---|
695 | /* Read first byte using block timeout */
|
---|
696 | if (!IO_Serial_Read (ifd->io, block_timeout, 1, buffer))
|
---|
697 | return IFD_TOWITOKO_IO_ERROR;
|
---|
698 |
|
---|
699 | if (size > 1)
|
---|
700 | {
|
---|
701 | /* Read remaining data bytes using char timeout */
|
---|
702 | if (!IO_Serial_Read (ifd->io, char_timeout, size - 1, buffer + 1))
|
---|
703 | return IFD_TOWITOKO_IO_ERROR;
|
---|
704 | }
|
---|
705 | }
|
---|
706 | else
|
---|
707 | {
|
---|
708 | /* Read all data bytes with the same timeout */
|
---|
709 | if (!IO_Serial_Read (ifd->io, char_timeout, size, buffer))
|
---|
710 | return IFD_TOWITOKO_IO_ERROR;
|
---|
711 | }
|
---|
712 | #ifdef USE_GPIO
|
---|
713 | if (gpio_detect) set_gpio1(1);
|
---|
714 | #endif
|
---|
715 |
|
---|
716 | #ifdef DEBUG_IFD
|
---|
717 | printf ("IFD: Receive: ");
|
---|
718 | for (i = 0; i < size; i++)
|
---|
719 | printf ("%X ", buffer[i]);
|
---|
720 | printf ("\n");
|
---|
721 | #endif
|
---|
722 |
|
---|
723 | return IFD_TOWITOKO_OK;
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | BYTE IFD_Towitoko_GetType (IFD * ifd)
|
---|
728 | {
|
---|
729 | return ifd->type;
|
---|
730 | }
|
---|
731 |
|
---|
732 | void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
|
---|
733 | {
|
---|
734 | char buffer[3];
|
---|
735 |
|
---|
736 | if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
|
---|
737 | memcpy (desc,"CE2",MIN(length,3));
|
---|
738 |
|
---|
739 | else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
|
---|
740 | memcpy (desc,"CE1",MIN(length,3));
|
---|
741 |
|
---|
742 | else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
|
---|
743 | memcpy (desc,"CDI",MIN(length,3));
|
---|
744 |
|
---|
745 | else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
|
---|
746 | memcpy (desc,"CDM",MIN(length,3));
|
---|
747 |
|
---|
748 | else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
|
---|
749 | memcpy (desc,"KZ2",MIN(length,3));
|
---|
750 |
|
---|
751 | else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
|
---|
752 | memcpy (desc,"KZ1",MIN(length,3));
|
---|
753 |
|
---|
754 | else if (ifd->type == IFD_TOWITOKO_MULTICAM)
|
---|
755 | memcpy (desc,"MCM",MIN(length,3));
|
---|
756 |
|
---|
757 | else
|
---|
758 | memcpy (desc,"UNK",MIN(length,3));
|
---|
759 |
|
---|
760 | snprintf (buffer, 3, "%02X", ifd->firmware);
|
---|
761 |
|
---|
762 | if (length > 3)
|
---|
763 | memcpy (desc+3, buffer, MIN(length-3,2));
|
---|
764 | }
|
---|
765 |
|
---|
766 | BYTE
|
---|
767 | IFD_Towitoko_GetFirmware (IFD * ifd)
|
---|
768 | {
|
---|
769 | return ifd->firmware;
|
---|
770 | }
|
---|
771 |
|
---|
772 | BYTE
|
---|
773 | IFD_Towitoko_GetSlot (IFD * ifd)
|
---|
774 | {
|
---|
775 | return ifd->slot;
|
---|
776 | }
|
---|
777 |
|
---|
778 | unsigned
|
---|
779 | IFD_Towitoko_GetNumSlots ()
|
---|
780 | {
|
---|
781 | return 1;
|
---|
782 | }
|
---|
783 |
|
---|
784 | unsigned long
|
---|
785 | IFD_Towitoko_GetMaxBaudrate ()
|
---|
786 | {
|
---|
787 | return 115200L;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /*
|
---|
791 | * Not exported funcions definition
|
---|
792 | */
|
---|
793 |
|
---|
794 |
|
---|
795 | static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
|
---|
796 | {
|
---|
797 | BYTE status[3];
|
---|
798 |
|
---|
799 | status[0] = IFD_TOWITOKO_MULTICAM;
|
---|
800 | status[1] = 0x00;
|
---|
801 |
|
---|
802 | ifd->type = status[0];
|
---|
803 | ifd->firmware = status[1];
|
---|
804 |
|
---|
805 | #ifdef DEBUG_IFD
|
---|
806 | printf ("IFD: Reader type = %s\n",
|
---|
807 | status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
|
---|
808 | status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
|
---|
809 | status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
|
---|
810 | status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
|
---|
811 | status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
|
---|
812 | status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
|
---|
813 | status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
|
---|
814 | #endif
|
---|
815 |
|
---|
816 | return IFD_TOWITOKO_OK;
|
---|
817 | }
|
---|
818 |
|
---|
819 |
|
---|
820 | static void IFD_Towitoko_Clear (IFD * ifd)
|
---|
821 | {
|
---|
822 | ifd->io = NULL;
|
---|
823 | ifd->slot = 0x00;
|
---|
824 | ifd->type = 0x00;
|
---|
825 | ifd->firmware = 0x00;
|
---|
826 | ifd->status = 0;
|
---|
827 | }
|
---|