source: trunk/csctapi/ifd_towitoko.c@ 1236

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

New SetParity function in IO_Serial

File size: 11.6 KB
Line 
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
75static int IFD_Towitoko_GetReaderInfo (IFD * ifd);
76static void IFD_Towitoko_Clear (IFD * ifd);
77
78#ifdef USE_GPIO
79
80int gpio_outen,gpio_out,gpio_in;
81unsigned int pin,gpio;
82int gpio_detect=0;
83
84static 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
98static 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
112static 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
119static 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
131IFD * 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
143void IFD_Towitoko_Delete (IFD * ifd)
144{
145 free (ifd);
146}
147
148int 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, reader[ridx].typ);
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(reader[ridx].typ == 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 = 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_SetPropertiesOld (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 if (!IO_Serial_SetParity (PARITY_EVEN))
212 return IFD_TOWITOKO_IO_ERROR;
213
214 if (ret != IFD_TOWITOKO_OK)
215 {
216 IFD_Towitoko_Clear (ifd);
217 return ret;
218 }
219
220 ret = IFD_Towitoko_GetReaderInfo (ifd);
221
222 if (ret != IFD_TOWITOKO_OK)
223 {
224 IFD_Towitoko_Clear (ifd);
225 }
226 else
227 {
228 IO_Serial_Flush(ifd->io);
229 }
230
231 return ret;
232}
233
234int IFD_Towitoko_Close (IFD * ifd)
235{
236 int ret;
237
238#ifdef USE_GPIO
239 if(gpio_detect)
240 {
241 close(gpio_outen);
242 close(gpio_out);
243 close(gpio_in);
244 }
245#endif
246
247#ifdef DEBUG_IFD
248 printf ("IFD: Closing slot number %d\n", ifd->slot);
249#endif
250
251 IFD_Towitoko_Clear (ifd);
252
253
254 return IFD_TOWITOKO_OK;
255}
256
257int IFD_Towitoko_SetBaudrate (IFD * ifd, unsigned long baudrate)
258{
259 if(reader[ridx].typ == R_INTERNAL)
260 {
261 return IFD_TOWITOKO_OK;
262 }
263#ifdef DEBUG_IFD
264 printf ("IFD: Setting baudrate to %lu\n", baudrate);
265#endif
266 /* Get current settings */
267 if (!IO_Serial_GetPropertiesOld (ifd->io))
268 return IFD_TOWITOKO_IO_ERROR;
269
270 if (ifd->io->output_bitrate == baudrate)
271 return IFD_TOWITOKO_OK;
272
273
274 /* Set serial device bitrate */
275 ifd->io->output_bitrate = baudrate;
276 ifd->io->input_bitrate = baudrate;
277
278 if (!IO_Serial_SetPropertiesOld (ifd->io))
279 return IFD_TOWITOKO_IO_ERROR;
280
281 return IFD_TOWITOKO_OK;
282}
283
284int IFD_Towitoko_GetBaudrate (IFD * ifd, unsigned long *baudrate)
285{
286 if(reader[ridx].typ == R_INTERNAL)
287 {
288 return IFD_TOWITOKO_OK;
289 }
290
291 /* Get current settings */
292 if (!IO_Serial_GetPropertiesOld (ifd->io))
293 return IFD_TOWITOKO_IO_ERROR;
294
295 (*baudrate) = ifd->io->output_bitrate;
296
297 return IFD_TOWITOKO_OK;
298}
299
300int IFD_Towitoko_ActivateICC (IFD * ifd)
301{
302#ifdef DEBUG_IFD
303 printf ("IFD: Activating card\n");
304#endif
305#ifdef SCI_DEV
306 if(reader[ridx].typ == R_INTERNAL)
307 {
308 int in;
309
310#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
311 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
312#else
313 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
314#endif
315 return IFD_TOWITOKO_IO_ERROR;
316
317 if(in)
318 {
319 struct timespec req_ts;
320 req_ts.tv_sec = 0;
321 req_ts.tv_nsec = 50000000;
322 nanosleep (&req_ts, NULL);
323 return IFD_TOWITOKO_OK;
324 }
325 else
326 {
327 return IFD_TOWITOKO_IO_ERROR;
328 }
329 }
330 else
331#endif
332 {
333 return IFD_TOWITOKO_OK;
334 }
335}
336
337int IFD_Towitoko_DeactivateICC (IFD * ifd)
338{
339#ifdef DEBUG_IFD
340 printf ("IFD: Deactivating card\n");
341#endif
342
343#ifdef SCI_DEV
344 if(reader[ridx].typ == R_INTERNAL)
345 {
346 int in;
347
348#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
349 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
350#else
351 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
352#endif
353 return IFD_TOWITOKO_IO_ERROR;
354
355 if(in)
356 {
357 if(ioctl(reader[ridx].handle, IOCTL_SET_DEACTIVATE)<0)
358 return IFD_TOWITOKO_IO_ERROR;
359 }
360
361
362 }
363#endif
364
365 return IFD_TOWITOKO_OK;
366}
367
368//extern void print_hex_data(unsigned char *data, int len);
369
370int IFD_Towitoko_ResetAsyncICC (IFD * ifd, ATR ** atr)
371{
372
373#ifdef DEBUG_IFD
374 printf ("IFD: Resetting card:\n");
375#endif
376
377 int ret;
378 int parity;
379 int i;
380 int par[3] = {PARITY_EVEN, PARITY_ODD, PARITY_NONE};
381#ifdef HAVE_NANOSLEEP
382 struct timespec req_ts;
383 req_ts.tv_sec = 0;
384 req_ts.tv_nsec = 50000000;
385#endif
386
387 (*atr) = NULL;
388 for(i=0; i<3; i++)
389 {
390 parity = par[i];
391 IO_Serial_Flush();
392
393 if (!IO_Serial_SetParity (parity))
394 return IFD_TOWITOKO_IO_ERROR;
395
396 ret = IFD_TOWITOKO_IO_ERROR;
397
398 IO_Serial_Ioctl_Lock(1);
399#ifdef USE_GPIO
400 if (gpio_detect)
401 {
402 set_gpio(0);
403 set_gpio1(0);
404 }
405 else
406#endif
407 IO_Serial_RTS_Set();
408
409#ifdef HAVE_NANOSLEEP
410 nanosleep (&req_ts, NULL);
411#else
412 usleep (50000L);
413#endif
414#ifdef USE_GPIO
415 if (gpio_detect)
416 {
417 set_gpio_input();
418 set_gpio1(1);
419 }
420 else
421#endif
422 IO_Serial_RTS_Clr();
423
424 IO_Serial_Ioctl_Lock(0);
425
426 (*atr) = ATR_New ();
427
428 if(ATR_InitFromStream ((*atr), IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
429 ret = IFD_TOWITOKO_OK;
430
431 /* Succesfully retrive ATR */
432 if (ret == IFD_TOWITOKO_OK)
433 {
434 break;
435 }
436 else
437 {
438 ATR_Delete (*atr);
439 (*atr) = NULL;
440#ifdef USE_GPIO
441 if (gpio_detect) set_gpio1(0);
442#endif
443 }
444 }
445
446 IO_Serial_Flush();
447
448/*
449 //PLAYGROUND faking ATR for test purposes only
450 //
451 // 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 };
452 // 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 };
453 // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
454 //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
455 ATR_Delete(*atr); //throw away actual ATR
456 (*atr) = ATR_New ();
457 ATR_InitFromArray ((*atr), atr_test, sizeof(atr_test));
458 //END OF PLAYGROUND
459*/
460
461 return ret;
462}
463
464BYTE IFD_Towitoko_GetType (IFD * ifd)
465{
466 return ifd->type;
467}
468
469void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
470{
471 char buffer[3];
472
473 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
474 memcpy (desc,"CE2",MIN(length,3));
475
476 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
477 memcpy (desc,"CE1",MIN(length,3));
478
479 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
480 memcpy (desc,"CDI",MIN(length,3));
481
482 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
483 memcpy (desc,"CDM",MIN(length,3));
484
485 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
486 memcpy (desc,"KZ2",MIN(length,3));
487
488 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
489 memcpy (desc,"KZ1",MIN(length,3));
490
491 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
492 memcpy (desc,"MCM",MIN(length,3));
493
494 else
495 memcpy (desc,"UNK",MIN(length,3));
496
497 snprintf (buffer, 3, "%02X", ifd->firmware);
498
499 if (length > 3)
500 memcpy (desc+3, buffer, MIN(length-3,2));
501}
502
503BYTE
504IFD_Towitoko_GetFirmware (IFD * ifd)
505{
506 return ifd->firmware;
507}
508
509BYTE
510IFD_Towitoko_GetSlot (IFD * ifd)
511{
512 return ifd->slot;
513}
514
515unsigned
516IFD_Towitoko_GetNumSlots ()
517{
518 return 1;
519}
520
521/*
522 * Not exported funcions definition
523 */
524
525
526static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
527{
528 BYTE status[3];
529
530 status[0] = IFD_TOWITOKO_MULTICAM;
531 status[1] = 0x00;
532
533 ifd->type = status[0];
534 ifd->firmware = status[1];
535
536#ifdef DEBUG_IFD
537 printf ("IFD: Reader type = %s\n",
538 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
539 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
540 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
541 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
542 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
543 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
544 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
545#endif
546
547 return IFD_TOWITOKO_OK;
548}
549
550
551static void IFD_Towitoko_Clear (IFD * ifd)
552{
553 ifd->io = NULL;
554 ifd->slot = 0x00;
555 ifd->type = 0x00;
556 ifd->firmware = 0x00;
557 reader[ridx].status = 0;
558}
Note: See TracBrowser for help on using the repository browser.