source: branches/smartreader/csctapi/ifd_towitoko.c@ 1254

Last change on this file since 1254 was 1254, checked in by rorothetroll, 11 years ago

resync with trunk

File size: 10.5 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 if (!IO_Serial_SetParams (IFD_TOWITOKO_BAUDRATE, 8, PARITY_EVEN, 2, IO_SERIAL_HIGH, IO_SERIAL_LOW))
184 return FALSE;
185
186 /* Default ifd settings */
187
188 ifd->io = io;
189 ifd->slot = slot;
190 ifd->type = IFD_TOWITOKO_MULTICAM;
191
192 if (!Phoenix_SetBaudrate(IFD_TOWITOKO_BAUDRATE))
193 {
194 IFD_Towitoko_Clear (ifd);
195 return IFD_TOWITOKO_IO_ERROR;
196 }
197
198 if (!IO_Serial_SetParity (PARITY_EVEN))
199 return IFD_TOWITOKO_IO_ERROR;
200
201 if (ret != IFD_TOWITOKO_OK)
202 {
203 IFD_Towitoko_Clear (ifd);
204 return ret;
205 }
206
207 ret = IFD_Towitoko_GetReaderInfo (ifd);
208
209 if (ret != IFD_TOWITOKO_OK)
210 {
211 IFD_Towitoko_Clear (ifd);
212 }
213 else
214 {
215 IO_Serial_Flush(ifd->io);
216 }
217
218 return ret;
219}
220
221int IFD_Towitoko_Close (IFD * ifd)
222{
223 int ret;
224
225#ifdef USE_GPIO
226 if(gpio_detect)
227 {
228 close(gpio_outen);
229 close(gpio_out);
230 close(gpio_in);
231 }
232#endif
233
234#ifdef DEBUG_IFD
235 printf ("IFD: Closing slot number %d\n", ifd->slot);
236#endif
237
238 IFD_Towitoko_Clear (ifd);
239
240
241 return IFD_TOWITOKO_OK;
242}
243
244int IFD_Towitoko_ActivateICC (IFD * ifd)
245{
246#ifdef DEBUG_IFD
247 printf ("IFD: Activating card\n");
248#endif
249#ifdef SCI_DEV
250 if(reader[ridx].typ == R_INTERNAL)
251 {
252 int in;
253
254#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
255 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
256#else
257 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
258#endif
259 return IFD_TOWITOKO_IO_ERROR;
260
261 if(in)
262 {
263 struct timespec req_ts;
264 req_ts.tv_sec = 0;
265 req_ts.tv_nsec = 50000000;
266 nanosleep (&req_ts, NULL);
267 return IFD_TOWITOKO_OK;
268 }
269 else
270 {
271 return IFD_TOWITOKO_IO_ERROR;
272 }
273 }
274 else
275#endif
276 {
277 return IFD_TOWITOKO_OK;
278 }
279}
280
281int IFD_Towitoko_DeactivateICC (IFD * ifd)
282{
283#ifdef DEBUG_IFD
284 printf ("IFD: Deactivating card\n");
285#endif
286
287#ifdef SCI_DEV
288 if(reader[ridx].typ == R_INTERNAL)
289 {
290 int in;
291
292#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
293 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
294#else
295 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
296#endif
297 return IFD_TOWITOKO_IO_ERROR;
298
299 if(in)
300 {
301 if(ioctl(reader[ridx].handle, IOCTL_SET_DEACTIVATE)<0)
302 return IFD_TOWITOKO_IO_ERROR;
303 }
304
305
306 }
307#endif
308
309 return IFD_TOWITOKO_OK;
310}
311
312//extern void print_hex_data(unsigned char *data, int len);
313
314int IFD_Towitoko_ResetAsyncICC (IFD * ifd, ATR ** atr)
315{
316
317#ifdef DEBUG_IFD
318 printf ("IFD: Resetting card:\n");
319#endif
320
321 int ret;
322 int parity;
323 int i;
324 int par[3] = {PARITY_EVEN, PARITY_ODD, PARITY_NONE};
325#ifdef HAVE_NANOSLEEP
326 struct timespec req_ts;
327 req_ts.tv_sec = 0;
328 req_ts.tv_nsec = 50000000;
329#endif
330
331 (*atr) = NULL;
332 for(i=0; i<3; i++)
333 {
334 parity = par[i];
335 IO_Serial_Flush();
336
337 if (!IO_Serial_SetParity (parity))
338 return IFD_TOWITOKO_IO_ERROR;
339
340 ret = IFD_TOWITOKO_IO_ERROR;
341
342 IO_Serial_Ioctl_Lock(1);
343#ifdef USE_GPIO
344 if (gpio_detect)
345 {
346 set_gpio(0);
347 set_gpio1(0);
348 }
349 else
350#endif
351 IO_Serial_RTS_Set();
352
353#ifdef HAVE_NANOSLEEP
354 nanosleep (&req_ts, NULL);
355#else
356 usleep (50000L);
357#endif
358#ifdef USE_GPIO
359 if (gpio_detect)
360 {
361 set_gpio_input();
362 set_gpio1(1);
363 }
364 else
365#endif
366 IO_Serial_RTS_Clr();
367
368 IO_Serial_Ioctl_Lock(0);
369
370 (*atr) = ATR_New ();
371
372 if(ATR_InitFromStream ((*atr), IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
373 ret = IFD_TOWITOKO_OK;
374
375 /* Succesfully retrive ATR */
376 if (ret == IFD_TOWITOKO_OK)
377 {
378 break;
379 }
380 else
381 {
382 ATR_Delete (*atr);
383 (*atr) = NULL;
384#ifdef USE_GPIO
385 if (gpio_detect) set_gpio1(0);
386#endif
387 }
388 }
389
390 IO_Serial_Flush();
391
392/*
393 //PLAYGROUND faking ATR for test purposes only
394 //
395 // 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 };
396 // 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 };
397 // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
398 //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
399 ATR_Delete(*atr); //throw away actual ATR
400 (*atr) = ATR_New ();
401 ATR_InitFromArray ((*atr), atr_test, sizeof(atr_test));
402 //END OF PLAYGROUND
403*/
404
405 return ret;
406}
407
408BYTE IFD_Towitoko_GetType (IFD * ifd)
409{
410 return ifd->type;
411}
412
413void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
414{
415 char buffer[3];
416
417 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
418 memcpy (desc,"CE2",MIN(length,3));
419
420 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
421 memcpy (desc,"CE1",MIN(length,3));
422
423 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
424 memcpy (desc,"CDI",MIN(length,3));
425
426 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
427 memcpy (desc,"CDM",MIN(length,3));
428
429 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
430 memcpy (desc,"KZ2",MIN(length,3));
431
432 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
433 memcpy (desc,"KZ1",MIN(length,3));
434
435 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
436 memcpy (desc,"MCM",MIN(length,3));
437
438 else
439 memcpy (desc,"UNK",MIN(length,3));
440
441 snprintf (buffer, 3, "%02X", ifd->firmware);
442
443 if (length > 3)
444 memcpy (desc+3, buffer, MIN(length-3,2));
445}
446
447BYTE
448IFD_Towitoko_GetFirmware (IFD * ifd)
449{
450 return ifd->firmware;
451}
452
453BYTE
454IFD_Towitoko_GetSlot (IFD * ifd)
455{
456 return ifd->slot;
457}
458
459unsigned
460IFD_Towitoko_GetNumSlots ()
461{
462 return 1;
463}
464
465/*
466 * Not exported funcions definition
467 */
468
469
470static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
471{
472 BYTE status[3];
473
474 status[0] = IFD_TOWITOKO_MULTICAM;
475 status[1] = 0x00;
476
477 ifd->type = status[0];
478 ifd->firmware = status[1];
479
480#ifdef DEBUG_IFD
481 printf ("IFD: Reader type = %s\n",
482 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
483 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
484 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
485 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
486 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
487 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
488 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
489#endif
490
491 return IFD_TOWITOKO_OK;
492}
493
494
495static void IFD_Towitoko_Clear (IFD * ifd)
496{
497 ifd->io = NULL;
498 ifd->slot = 0x00;
499 ifd->type = 0x00;
500 ifd->firmware = 0x00;
501 reader[ridx].status = 0;
502}
Note: See TracBrowser for help on using the repository browser.