source: trunk/csctapi/ifd_towitoko.c@ 1234

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

Clean up unused csctapi code

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