1 | /*
|
---|
2 | ifd_phoenix.c
|
---|
3 | This module provides IFD handling functions for Smartmouse/Phoenix reader.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include <stdio.h>
|
---|
7 | //#include <time.h>
|
---|
8 | //#include <string.h>
|
---|
9 | //#include "ioctls.h"
|
---|
10 | #include "../globals.h"
|
---|
11 | #include "atr.h"
|
---|
12 | #include <termios.h>
|
---|
13 | #include "ifd_phoenix.h"
|
---|
14 | #include "icc_async.h"
|
---|
15 |
|
---|
16 | #define MAX_TRANSMIT 255
|
---|
17 |
|
---|
18 | #ifdef USE_GPIO //felix: definition of gpio functions
|
---|
19 | int gpio_outen,gpio_out,gpio_in;
|
---|
20 | unsigned int pin,gpio;
|
---|
21 | int gpio_detect=0;
|
---|
22 |
|
---|
23 | static void set_gpio(int level)
|
---|
24 | {
|
---|
25 | read(gpio_outen, &gpio, sizeof(gpio));
|
---|
26 | gpio |= pin;
|
---|
27 | write(gpio_outen, &gpio, sizeof(gpio));
|
---|
28 |
|
---|
29 | read(gpio_out, &gpio, sizeof(gpio));
|
---|
30 | if (level>0)
|
---|
31 | gpio|=pin;
|
---|
32 | else
|
---|
33 | gpio&=~pin;
|
---|
34 | write(gpio_out, &gpio, sizeof(gpio));
|
---|
35 | }
|
---|
36 |
|
---|
37 | static void set_gpio_input(void)
|
---|
38 | {
|
---|
39 | read(gpio_outen, &gpio, sizeof(gpio));
|
---|
40 | gpio &= ~pin;
|
---|
41 | write(gpio_outen, &gpio, sizeof(gpio));
|
---|
42 | }
|
---|
43 |
|
---|
44 | static int get_gpio(void)
|
---|
45 | {
|
---|
46 | set_gpio_input();
|
---|
47 | read(gpio_in, &gpio, sizeof(gpio));
|
---|
48 | if (gpio&pin)
|
---|
49 | return OK;
|
---|
50 | else
|
---|
51 | return ERROR;
|
---|
52 | }
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | int Phoenix_Init ()
|
---|
57 | {
|
---|
58 | #ifdef USE_GPIO //felix: define gpio number used for card detect and reset. ref to globals.h
|
---|
59 | if (reader[ridx].detect>4)
|
---|
60 | {
|
---|
61 | gpio_detect=reader[ridx].detect-4;
|
---|
62 | pin = 1<<gpio_detect;
|
---|
63 | gpio_outen=open("/dev/gpio/outen",O_RDWR);
|
---|
64 | gpio_out=open("/dev/gpio/out",O_RDWR);
|
---|
65 | gpio_in=open("/dev/gpio/in",O_RDWR);
|
---|
66 | set_gpio_input();
|
---|
67 | }
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #ifdef DEBUG_IFD
|
---|
71 | printf ("IFD: Initializing com=%d\n", reader[ridx].typ);
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | if(reader[ridx].typ == R_INTERNAL) //not sure whether this should be moved in front of GPIO part
|
---|
75 | return OK;
|
---|
76 |
|
---|
77 | /* Default serial port settings */
|
---|
78 | if (IO_Serial_SetParams (DEFAULT_BAUDRATE, 8, PARITY_EVEN, 2, IO_SERIAL_HIGH, IO_SERIAL_LOW))
|
---|
79 | {
|
---|
80 | reader[ridx].status = 0;//added this one because it seemed logical
|
---|
81 | return ERROR;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (Phoenix_SetBaudrate(DEFAULT_BAUDRATE))
|
---|
85 | {
|
---|
86 | reader[ridx].status = 0;
|
---|
87 | return ERROR;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (IO_Serial_SetParity (PARITY_EVEN))
|
---|
91 | {
|
---|
92 | reader[ridx].status = 0;
|
---|
93 | return ERROR;
|
---|
94 | }
|
---|
95 |
|
---|
96 | IO_Serial_Flush();
|
---|
97 | return OK;
|
---|
98 | }
|
---|
99 |
|
---|
100 | int Phoenix_GetStatus (int * status)
|
---|
101 | {
|
---|
102 | #ifdef USE_GPIO //felix: detect card via defined gpio
|
---|
103 | if (gpio_detect)
|
---|
104 | *status=!get_gpio();
|
---|
105 | else
|
---|
106 | #endif
|
---|
107 | {
|
---|
108 | unsigned int modembits=0;
|
---|
109 | if (ioctl(reader[ridx].handle, TIOCMGET,&modembits)<0)
|
---|
110 | return ERROR;
|
---|
111 | switch(reader[ridx].detect&0x7f)
|
---|
112 | {
|
---|
113 | case 0: *status=(modembits & TIOCM_CAR); break;
|
---|
114 | case 1: *status=(modembits & TIOCM_DSR); break;
|
---|
115 | case 2: *status=(modembits & TIOCM_CTS); break;
|
---|
116 | case 3: *status=(modembits & TIOCM_RNG); break;
|
---|
117 | default: *status=0; // dummy
|
---|
118 | }
|
---|
119 | if (!(reader[ridx].detect&0x80))
|
---|
120 | *status=!*status;
|
---|
121 | }
|
---|
122 | return OK;
|
---|
123 | }
|
---|
124 |
|
---|
125 | int Phoenix_Reset (ATR * atr)
|
---|
126 | {
|
---|
127 | #ifdef DEBUG_IFD
|
---|
128 | printf ("IFD: Resetting card:\n");
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | int ret;
|
---|
132 | int i;
|
---|
133 | int parity[3] = {PARITY_EVEN, PARITY_ODD, PARITY_NONE};
|
---|
134 | #ifdef HAVE_NANOSLEEP
|
---|
135 | struct timespec req_ts;
|
---|
136 | req_ts.tv_sec = 0;
|
---|
137 | req_ts.tv_nsec = 50000000;
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | for(i=0; i<3; i++) {
|
---|
141 | IO_Serial_Flush();
|
---|
142 | if (IO_Serial_SetParity (parity[i]))
|
---|
143 | return ERROR;
|
---|
144 |
|
---|
145 | ret = ERROR;
|
---|
146 | IO_Serial_Ioctl_Lock(1);
|
---|
147 | #ifdef USE_GPIO
|
---|
148 | if (gpio_detect)
|
---|
149 | set_gpio(0);
|
---|
150 | else
|
---|
151 | #endif
|
---|
152 | IO_Serial_RTS_Set();
|
---|
153 | #ifdef HAVE_NANOSLEEP
|
---|
154 | nanosleep (&req_ts, NULL);
|
---|
155 | #else
|
---|
156 | usleep (50000L);
|
---|
157 | #endif
|
---|
158 | #ifdef USE_GPIO //felix: set card reset hi (inactive)
|
---|
159 | if (gpio_detect) {
|
---|
160 | set_gpio_input();
|
---|
161 | }
|
---|
162 | else
|
---|
163 | #endif
|
---|
164 | IO_Serial_RTS_Clr();
|
---|
165 | IO_Serial_Ioctl_Lock(0);
|
---|
166 | if(ATR_InitFromStream (atr, ATR_TIMEOUT) == ATR_OK)
|
---|
167 | ret = OK;
|
---|
168 | // Succesfully retrieve ATR
|
---|
169 | if (ret == OK)
|
---|
170 | break;
|
---|
171 | }
|
---|
172 | IO_Serial_Flush();
|
---|
173 |
|
---|
174 | /*
|
---|
175 | //PLAYGROUND faking ATR for test purposes only
|
---|
176 | //
|
---|
177 | // 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 };
|
---|
178 | // 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 };
|
---|
179 | // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
|
---|
180 | // conax unsigned char atr_test[] = { 0x3B, 0x24, 0x00, 0x30, 0x42, 0x30, 0x30 };
|
---|
181 | //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
|
---|
182 | ATR_InitFromArray (atr, atr_test, sizeof(atr_test));
|
---|
183 | //END OF PLAYGROUND
|
---|
184 | */
|
---|
185 |
|
---|
186 | return ret;
|
---|
187 | }
|
---|
188 |
|
---|
189 | int Phoenix_Transmit (BYTE * buffer, unsigned size, unsigned int block_delay, unsigned int char_delay)
|
---|
190 | {
|
---|
191 | unsigned sent=0, to_send = 0;
|
---|
192 |
|
---|
193 | #ifdef DEBUG_IFD
|
---|
194 | printf ("IFD: Transmit: ");
|
---|
195 | for (sent = 0; sent < size; sent++)
|
---|
196 | printf ("%X ", buffer[sent]);
|
---|
197 | printf ("\n");
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | for (sent = 0; sent < size; sent = sent + to_send)
|
---|
201 | {
|
---|
202 | /* Calculate number of bytes to send */
|
---|
203 | to_send = MIN(size, MAX_TRANSMIT);
|
---|
204 |
|
---|
205 | /* Send data */
|
---|
206 | if ((sent == 0) && (block_delay != char_delay))
|
---|
207 | {
|
---|
208 | if (IO_Serial_Write (block_delay, 1, buffer))
|
---|
209 | return ERROR;
|
---|
210 |
|
---|
211 | if (IO_Serial_Write (char_delay, to_send-1, buffer+1))
|
---|
212 | return ERROR;
|
---|
213 | }
|
---|
214 | else
|
---|
215 | {
|
---|
216 | if (IO_Serial_Write (char_delay, to_send, buffer+sent))
|
---|
217 | return ERROR;
|
---|
218 | }
|
---|
219 | }
|
---|
220 | return OK;
|
---|
221 | }
|
---|
222 |
|
---|
223 | int Phoenix_Receive (BYTE * buffer, unsigned size, unsigned int timeout)
|
---|
224 | {
|
---|
225 | #define IFD_TOWITOKO_TIMEOUT 1000
|
---|
226 |
|
---|
227 | /* Read all data bytes with the same timeout */
|
---|
228 | if (IO_Serial_Read (timeout + IFD_TOWITOKO_TIMEOUT, size, buffer))
|
---|
229 | return ERROR;
|
---|
230 |
|
---|
231 | #ifdef DEBUG_IFD
|
---|
232 | int i;
|
---|
233 | printf ("IFD: Receive: ");
|
---|
234 | for (i = 0; i < size; i++)
|
---|
235 | printf ("%X ", buffer[i]);
|
---|
236 | printf ("\n");
|
---|
237 | #endif
|
---|
238 |
|
---|
239 | return OK;
|
---|
240 | }
|
---|
241 |
|
---|
242 | int Phoenix_SetBaudrate (unsigned long baudrate)
|
---|
243 | {
|
---|
244 | #ifdef DEBUG_IFD
|
---|
245 | printf ("IFD: Setting baudrate to %lu\n", baudrate);
|
---|
246 | #endif
|
---|
247 | if ((reader[ridx].typ != R_INTERNAL) && (current_baudrate != baudrate))
|
---|
248 | {
|
---|
249 | /* Get current settings */
|
---|
250 | struct termios tio;
|
---|
251 | if (tcgetattr (reader[ridx].handle, &tio) != 0)
|
---|
252 | return ERROR;
|
---|
253 |
|
---|
254 | if (IO_Serial_SetBitrate (baudrate, &tio))
|
---|
255 | return ERROR;
|
---|
256 |
|
---|
257 | if (IO_Serial_SetProperties(tio))
|
---|
258 | return ERROR;
|
---|
259 | }
|
---|
260 | current_baudrate = baudrate; //so if update fails, current_baudrate is not changed either
|
---|
261 | return OK;
|
---|
262 | }
|
---|
263 |
|
---|
264 | int Phoenix_Close ()
|
---|
265 | {
|
---|
266 | #ifdef USE_GPIO //felix: close dev if card detected
|
---|
267 | if(gpio_detect)
|
---|
268 | {
|
---|
269 | close(gpio_outen);
|
---|
270 | close(gpio_out);
|
---|
271 | close(gpio_in);
|
---|
272 | }
|
---|
273 | #endif
|
---|
274 |
|
---|
275 | #ifdef DEBUG_IFD
|
---|
276 | printf ("IFD: Closing slot\n");
|
---|
277 | #endif
|
---|
278 |
|
---|
279 | reader[ridx].status = 0;
|
---|
280 | return OK;
|
---|
281 | }
|
---|