source: trunk/csctapi/ifd_towitoko.c@ 1263

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

Remove gpio - LED stuff, add comments, thx to felixka. NOTE gpio still doesn not compile)

File size: 8.1 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 //felix: definition of gpio functions
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_gpio_input(void)
99{
100 read(gpio_outen, &gpio, sizeof(gpio));
101 gpio &= ~pin;
102 write(gpio_outen, &gpio, sizeof(gpio));
103}
104
105static int get_gpio(void)
106{
107 set_gpio_input();
108 read(gpio_in, &gpio, sizeof(gpio));
109 return ((int)((gpio&pin)?1:0));
110}
111#endif
112
113/*
114 * Exported functions definition
115 */
116
117IFD * IFD_Towitoko_New ()
118{
119 IFD *ifd;
120
121 ifd = (IFD *) malloc (sizeof (IFD));
122
123 if (ifd != NULL)
124 IFD_Towitoko_Clear (ifd);
125
126 return ifd;
127}
128
129void IFD_Towitoko_Delete (IFD * ifd)
130{
131 free (ifd);
132}
133
134int IFD_Towitoko_Init (IFD * ifd, BYTE slot)
135{
136 int ret;
137
138#ifdef USE_GPIO //felix: define gpio number used for card detect and reset. ref to globals.h
139 extern int oscam_card_detect;
140 if (oscam_card_detect>4)
141 {
142 gpio_detect=oscam_card_detect-4;
143 pin = 1<<gpio_detect;
144 gpio_outen=open("/dev/gpio/outen",O_RDWR);
145 gpio_out=open("/dev/gpio/out",O_RDWR);
146 gpio_in=open("/dev/gpio/in",O_RDWR);
147 set_gpio_input();
148 }
149#endif
150
151#ifdef DEBUG_IFD
152 printf ("IFD: Initializing slot number %d, com=%d\n", slot, reader[ridx].typ);
153#endif
154
155// if ((slot != IFD_TOWITOKO_SLOT_MULTICAM) && (slot != IFD_TOWITOKO_SLOT_A) && (slot != IFD_TOWITOKO_SLOT_B))
156 if (slot != IFD_TOWITOKO_SLOT_MULTICAM )
157 return IFD_TOWITOKO_PARAM_ERROR;
158
159 if(reader[ridx].typ == R_INTERNAL)
160 {
161 ifd->slot = slot;
162 ifd->type = IFD_TOWITOKO_MULTICAM;
163 return IFD_TOWITOKO_OK;
164 }
165
166
167 /* Default serial port settings */
168 if (!IO_Serial_SetParams (IFD_TOWITOKO_BAUDRATE, 8, PARITY_EVEN, 2, IO_SERIAL_HIGH, IO_SERIAL_LOW))
169 return FALSE;
170
171 /* Default ifd settings */
172
173 ifd->slot = slot;
174 ifd->type = IFD_TOWITOKO_MULTICAM;
175
176 if (!Phoenix_SetBaudrate(IFD_TOWITOKO_BAUDRATE))
177 {
178 IFD_Towitoko_Clear (ifd);
179 return IFD_TOWITOKO_IO_ERROR;
180 }
181
182 if (!IO_Serial_SetParity (PARITY_EVEN))
183 {
184 IFD_Towitoko_Clear (ifd);
185 return IFD_TOWITOKO_IO_ERROR;
186 }
187
188 ret = IFD_Towitoko_GetReaderInfo (ifd);
189
190 if (ret != IFD_TOWITOKO_OK)
191 {
192 IFD_Towitoko_Clear (ifd);
193 }
194 else
195 {
196 IO_Serial_Flush();
197 }
198
199 return ret;
200}
201
202int IFD_Towitoko_Close (IFD * ifd)
203{
204#ifdef USE_GPIO //felix: close dev if card detected
205 if(gpio_detect)
206 {
207 close(gpio_outen);
208 close(gpio_out);
209 close(gpio_in);
210 }
211#endif
212
213#ifdef DEBUG_IFD
214 printf ("IFD: Closing slot number %d\n", ifd->slot);
215#endif
216
217 IFD_Towitoko_Clear (ifd);
218
219
220 return IFD_TOWITOKO_OK;
221}
222
223int IFD_Towitoko_ActivateICC ()
224{
225#ifdef DEBUG_IFD
226 printf ("IFD: Activating card\n");
227#endif
228#ifdef SCI_DEV
229 if(reader[ridx].typ == R_INTERNAL)
230 {
231 int in;
232
233#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
234 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
235#else
236 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
237#endif
238 return IFD_TOWITOKO_IO_ERROR;
239
240 if(in)
241 {
242 struct timespec req_ts;
243 req_ts.tv_sec = 0;
244 req_ts.tv_nsec = 50000000;
245 nanosleep (&req_ts, NULL);
246 return IFD_TOWITOKO_OK;
247 }
248 else
249 {
250 return IFD_TOWITOKO_IO_ERROR;
251 }
252 }
253 else
254#endif
255 {
256 return IFD_TOWITOKO_OK;
257 }
258}
259
260int IFD_Towitoko_DeactivateICC ()
261{
262#ifdef DEBUG_IFD
263 printf ("IFD: Deactivating card\n");
264#endif
265
266#ifdef SCI_DEV
267 if(reader[ridx].typ == R_INTERNAL)
268 {
269 int in;
270
271#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
272 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
273#else
274 if(ioctl(reader[ridx].handle, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
275#endif
276 return IFD_TOWITOKO_IO_ERROR;
277
278 if(in)
279 {
280 if(ioctl(reader[ridx].handle, IOCTL_SET_DEACTIVATE)<0)
281 return IFD_TOWITOKO_IO_ERROR;
282 }
283
284
285 }
286#endif
287
288 return IFD_TOWITOKO_OK;
289}
290
291//extern void print_hex_data(unsigned char *data, int len);
292
293BYTE IFD_Towitoko_GetType (IFD * ifd)
294{
295 return ifd->type;
296}
297
298void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
299{
300 char buffer[3];
301
302 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
303 memcpy (desc,"CE2",MIN(length,3));
304
305 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
306 memcpy (desc,"CE1",MIN(length,3));
307
308 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
309 memcpy (desc,"CDI",MIN(length,3));
310
311 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
312 memcpy (desc,"CDM",MIN(length,3));
313
314 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
315 memcpy (desc,"KZ2",MIN(length,3));
316
317 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
318 memcpy (desc,"KZ1",MIN(length,3));
319
320 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
321 memcpy (desc,"MCM",MIN(length,3));
322
323 else
324 memcpy (desc,"UNK",MIN(length,3));
325
326 snprintf (buffer, 3, "%02X", ifd->firmware);
327
328 if (length > 3)
329 memcpy (desc+3, buffer, MIN(length-3,2));
330}
331
332BYTE
333IFD_Towitoko_GetFirmware (IFD * ifd)
334{
335 return ifd->firmware;
336}
337
338BYTE
339IFD_Towitoko_GetSlot (IFD * ifd)
340{
341 return ifd->slot;
342}
343
344unsigned
345IFD_Towitoko_GetNumSlots ()
346{
347 return 1;
348}
349
350/*
351 * Not exported funcions definition
352 */
353
354
355static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
356{
357 BYTE status[3];
358
359 status[0] = IFD_TOWITOKO_MULTICAM;
360 status[1] = 0x00;
361
362 ifd->type = status[0];
363 ifd->firmware = status[1];
364
365#ifdef DEBUG_IFD
366 printf ("IFD: Reader type = %s\n",
367 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
368 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
369 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
370 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
371 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
372 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
373 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
374#endif
375
376 return IFD_TOWITOKO_OK;
377}
378
379
380static void IFD_Towitoko_Clear (IFD * ifd)
381{
382 ifd->slot = 0x00;
383 ifd->type = 0x00;
384 ifd->firmware = 0x00;
385 reader[ridx].status = 0;
386}
Note: See TracBrowser for help on using the repository browser.