source: trunk/csctapi/ifd_towitoko.c@ 1253

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

Move Reset to ifd_phoenix.c and cleanup lot of overhead

File size: 8.2 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, 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->slot = slot;
176 ifd->type = IFD_TOWITOKO_MULTICAM;
177 return IFD_TOWITOKO_OK;
178 }
179
180
181 /* Default serial port settings */
182 if (!IO_Serial_SetParams (IFD_TOWITOKO_BAUDRATE, 8, PARITY_EVEN, 2, IO_SERIAL_HIGH, IO_SERIAL_LOW))
183 return FALSE;
184
185 /* Default ifd settings */
186
187 ifd->slot = slot;
188 ifd->type = IFD_TOWITOKO_MULTICAM;
189
190 if (!Phoenix_SetBaudrate(IFD_TOWITOKO_BAUDRATE))
191 {
192 IFD_Towitoko_Clear (ifd);
193 return IFD_TOWITOKO_IO_ERROR;
194 }
195
196 if (!IO_Serial_SetParity (PARITY_EVEN))
197 {
198 IFD_Towitoko_Clear (ifd);
199 return IFD_TOWITOKO_IO_ERROR;
200 }
201
202 ret = IFD_Towitoko_GetReaderInfo (ifd);
203
204 if (ret != IFD_TOWITOKO_OK)
205 {
206 IFD_Towitoko_Clear (ifd);
207 }
208 else
209 {
210 IO_Serial_Flush();
211 }
212
213 return ret;
214}
215
216int IFD_Towitoko_Close (IFD * ifd)
217{
218#ifdef USE_GPIO
219 if(gpio_detect)
220 {
221 close(gpio_outen);
222 close(gpio_out);
223 close(gpio_in);
224 }
225#endif
226
227#ifdef DEBUG_IFD
228 printf ("IFD: Closing slot number %d\n", ifd->slot);
229#endif
230
231 IFD_Towitoko_Clear (ifd);
232
233
234 return IFD_TOWITOKO_OK;
235}
236
237int IFD_Towitoko_ActivateICC ()
238{
239#ifdef DEBUG_IFD
240 printf ("IFD: Activating card\n");
241#endif
242#ifdef SCI_DEV
243 if(reader[ridx].typ == R_INTERNAL)
244 {
245 int in;
246
247#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
248 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
249#else
250 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
251#endif
252 return IFD_TOWITOKO_IO_ERROR;
253
254 if(in)
255 {
256 struct timespec req_ts;
257 req_ts.tv_sec = 0;
258 req_ts.tv_nsec = 50000000;
259 nanosleep (&req_ts, NULL);
260 return IFD_TOWITOKO_OK;
261 }
262 else
263 {
264 return IFD_TOWITOKO_IO_ERROR;
265 }
266 }
267 else
268#endif
269 {
270 return IFD_TOWITOKO_OK;
271 }
272}
273
274int IFD_Towitoko_DeactivateICC ()
275{
276#ifdef DEBUG_IFD
277 printf ("IFD: Deactivating card\n");
278#endif
279
280#ifdef SCI_DEV
281 if(reader[ridx].typ == R_INTERNAL)
282 {
283 int in;
284
285#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
286 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
287#else
288 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
289#endif
290 return IFD_TOWITOKO_IO_ERROR;
291
292 if(in)
293 {
294 if(ioctl(reader[ridx].handle, IOCTL_SET_DEACTIVATE)<0)
295 return IFD_TOWITOKO_IO_ERROR;
296 }
297
298
299 }
300#endif
301
302 return IFD_TOWITOKO_OK;
303}
304
305//extern void print_hex_data(unsigned char *data, int len);
306
307BYTE IFD_Towitoko_GetType (IFD * ifd)
308{
309 return ifd->type;
310}
311
312void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
313{
314 char buffer[3];
315
316 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
317 memcpy (desc,"CE2",MIN(length,3));
318
319 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
320 memcpy (desc,"CE1",MIN(length,3));
321
322 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
323 memcpy (desc,"CDI",MIN(length,3));
324
325 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
326 memcpy (desc,"CDM",MIN(length,3));
327
328 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
329 memcpy (desc,"KZ2",MIN(length,3));
330
331 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
332 memcpy (desc,"KZ1",MIN(length,3));
333
334 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
335 memcpy (desc,"MCM",MIN(length,3));
336
337 else
338 memcpy (desc,"UNK",MIN(length,3));
339
340 snprintf (buffer, 3, "%02X", ifd->firmware);
341
342 if (length > 3)
343 memcpy (desc+3, buffer, MIN(length-3,2));
344}
345
346BYTE
347IFD_Towitoko_GetFirmware (IFD * ifd)
348{
349 return ifd->firmware;
350}
351
352BYTE
353IFD_Towitoko_GetSlot (IFD * ifd)
354{
355 return ifd->slot;
356}
357
358unsigned
359IFD_Towitoko_GetNumSlots ()
360{
361 return 1;
362}
363
364/*
365 * Not exported funcions definition
366 */
367
368
369static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
370{
371 BYTE status[3];
372
373 status[0] = IFD_TOWITOKO_MULTICAM;
374 status[1] = 0x00;
375
376 ifd->type = status[0];
377 ifd->firmware = status[1];
378
379#ifdef DEBUG_IFD
380 printf ("IFD: Reader type = %s\n",
381 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
382 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
383 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
384 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
385 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
386 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
387 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
388#endif
389
390 return IFD_TOWITOKO_OK;
391}
392
393
394static void IFD_Towitoko_Clear (IFD * ifd)
395{
396 ifd->slot = 0x00;
397 ifd->type = 0x00;
398 ifd->firmware = 0x00;
399 reader[ridx].status = 0;
400}
Note: See TracBrowser for help on using the repository browser.