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 | call (IO_Serial_InitPnP ());
|
---|
59 | IO_Serial_Flush();
|
---|
60 |
|
---|
61 | #ifdef USE_GPIO //felix: define gpio number used for card detect and reset. ref to globals.h
|
---|
62 | if (reader[ridx].detect>4)
|
---|
63 | {
|
---|
64 | gpio_detect=reader[ridx].detect-4;
|
---|
65 | pin = 1<<gpio_detect;
|
---|
66 | gpio_outen=open("/dev/gpio/outen",O_RDWR);
|
---|
67 | gpio_out=open("/dev/gpio/out",O_RDWR);
|
---|
68 | gpio_in=open("/dev/gpio/in",O_RDWR);
|
---|
69 | set_gpio_input();
|
---|
70 | }
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | cs_debug_mask (D_IFD, "IFD: Initializing reader %s type=%d\n", reader[ridx].label, reader[ridx].typ);
|
---|
74 |
|
---|
75 | /* Default serial port settings */
|
---|
76 | call (IO_Serial_SetParams (DEFAULT_BAUDRATE, 8, PARITY_EVEN, 2, IO_SERIAL_HIGH, IO_SERIAL_LOW));
|
---|
77 | call (Phoenix_SetBaudrate (DEFAULT_BAUDRATE));
|
---|
78 | call (IO_Serial_SetParity (PARITY_EVEN));
|
---|
79 | IO_Serial_Flush();
|
---|
80 | return OK;
|
---|
81 | }
|
---|
82 |
|
---|
83 | int Phoenix_GetStatus (int * status)
|
---|
84 | {
|
---|
85 | #ifdef USE_GPIO //felix: detect card via defined gpio
|
---|
86 | if (gpio_detect)
|
---|
87 | *status=!get_gpio();
|
---|
88 | else
|
---|
89 | #endif
|
---|
90 | {
|
---|
91 | unsigned int modembits=0;
|
---|
92 | call (ioctl(reader[ridx].handle, TIOCMGET,&modembits)<0);
|
---|
93 | switch(reader[ridx].detect&0x7f)
|
---|
94 | {
|
---|
95 | case 0: *status=(modembits & TIOCM_CAR); break;
|
---|
96 | case 1: *status=(modembits & TIOCM_DSR); break;
|
---|
97 | case 2: *status=(modembits & TIOCM_CTS); break;
|
---|
98 | case 3: *status=(modembits & TIOCM_RNG); break;
|
---|
99 | default: *status=0; // dummy
|
---|
100 | }
|
---|
101 | if (!(reader[ridx].detect&0x80))
|
---|
102 | *status=!*status;
|
---|
103 | }
|
---|
104 | return OK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int Phoenix_Reset (ATR * atr)
|
---|
108 | {
|
---|
109 | cs_debug_mask (D_IFD, "IFD: Resetting card:\n");
|
---|
110 | int ret;
|
---|
111 | int i;
|
---|
112 | int parity[3] = {PARITY_EVEN, PARITY_ODD, PARITY_NONE};
|
---|
113 | call (Phoenix_SetBaudrate (DEFAULT_BAUDRATE));
|
---|
114 | for(i=0; i<3; i++) {
|
---|
115 | cs_sleepms(200); // pause for 200ms as this might help with the PL2303
|
---|
116 | IO_Serial_Flush();
|
---|
117 | call (IO_Serial_SetParity (parity[i]));
|
---|
118 |
|
---|
119 | ret = ERROR;
|
---|
120 | cs_sleepms(500); //smartreader in mouse mode needs this
|
---|
121 | IO_Serial_Ioctl_Lock(1);
|
---|
122 | #ifdef USE_GPIO
|
---|
123 | if (gpio_detect)
|
---|
124 | set_gpio(0);
|
---|
125 | else
|
---|
126 | #endif
|
---|
127 | IO_Serial_RTS_Set();
|
---|
128 | cs_sleepms(200); // we went form 50 to 200 as this might help with the PL2303
|
---|
129 | #ifdef USE_GPIO //felix: set card reset hi (inactive)
|
---|
130 | if (gpio_detect) {
|
---|
131 | set_gpio_input();
|
---|
132 | }
|
---|
133 | else
|
---|
134 | #endif
|
---|
135 | IO_Serial_RTS_Clr();
|
---|
136 | cs_sleepms(200); // pause for 200ms as this might help with the PL2303
|
---|
137 | IO_Serial_Ioctl_Lock(0);
|
---|
138 | if(ATR_InitFromStream (atr, ATR_TIMEOUT) == ATR_OK)
|
---|
139 | ret = OK;
|
---|
140 | // Succesfully retrieve ATR
|
---|
141 | if (ret == OK)
|
---|
142 | break;
|
---|
143 | }
|
---|
144 | IO_Serial_Flush();
|
---|
145 |
|
---|
146 | /*
|
---|
147 | //PLAYGROUND faking ATR for test purposes only
|
---|
148 | //
|
---|
149 | // 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 };
|
---|
150 | // 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 };
|
---|
151 | // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
|
---|
152 | // conax unsigned char atr_test[] = { 0x3B, 0x24, 0x00, 0x30, 0x42, 0x30, 0x30 };
|
---|
153 | //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
|
---|
154 | ATR_InitFromArray (atr, atr_test, sizeof(atr_test));
|
---|
155 | //END OF PLAYGROUND
|
---|
156 | */
|
---|
157 |
|
---|
158 | return ret;
|
---|
159 | }
|
---|
160 |
|
---|
161 | int Phoenix_Transmit (BYTE * buffer, unsigned size, unsigned int block_delay, unsigned int char_delay)
|
---|
162 | {
|
---|
163 | unsigned sent=0, to_send = 0;
|
---|
164 |
|
---|
165 | for (sent = 0; sent < size; sent = sent + to_send)
|
---|
166 | {
|
---|
167 | /* Calculate number of bytes to send */
|
---|
168 | to_send = MIN(size, MAX_TRANSMIT);
|
---|
169 |
|
---|
170 | /* Send data */
|
---|
171 | if ((sent == 0) && (block_delay != char_delay))
|
---|
172 | {
|
---|
173 | call (IO_Serial_Write (block_delay, 1, buffer));
|
---|
174 | call (IO_Serial_Write (char_delay, to_send-1, buffer+1));
|
---|
175 | }
|
---|
176 | else
|
---|
177 | call (IO_Serial_Write (char_delay, to_send, buffer+sent));
|
---|
178 | }
|
---|
179 | return OK;
|
---|
180 | }
|
---|
181 |
|
---|
182 | int Phoenix_Receive (BYTE * buffer, unsigned size, unsigned int timeout)
|
---|
183 | {
|
---|
184 | #define IFD_TOWITOKO_TIMEOUT 1000
|
---|
185 |
|
---|
186 | /* Read all data bytes with the same timeout */
|
---|
187 | call (IO_Serial_Read (timeout + IFD_TOWITOKO_TIMEOUT, size, buffer));
|
---|
188 | return OK;
|
---|
189 | }
|
---|
190 |
|
---|
191 | int Phoenix_SetBaudrate (unsigned long baudrate)
|
---|
192 | {
|
---|
193 | cs_debug_mask (D_IFD, "IFD: Phoenix Setting baudrate to %lu\n", baudrate);
|
---|
194 | if (current_baudrate != baudrate)
|
---|
195 | {
|
---|
196 | /* Get current settings */
|
---|
197 | struct termios tio;
|
---|
198 | call (tcgetattr (reader[ridx].handle, &tio) != 0);
|
---|
199 | call (IO_Serial_SetBitrate (baudrate, &tio));
|
---|
200 | cs_sleepms(200); // pause for 200ms as this might help with the PL2303
|
---|
201 | call (IO_Serial_SetProperties(tio));
|
---|
202 | cs_sleepms(200); // pause for 200ms as this might help with the PL2303
|
---|
203 | }
|
---|
204 | current_baudrate = baudrate; //so if update fails, current_baudrate is not changed either
|
---|
205 | return OK;
|
---|
206 | }
|
---|
207 |
|
---|
208 | int Phoenix_Close ()
|
---|
209 | {
|
---|
210 | #ifdef USE_GPIO //felix: close dev if card detected
|
---|
211 | if(gpio_detect)
|
---|
212 | {
|
---|
213 | close(gpio_outen);
|
---|
214 | close(gpio_out);
|
---|
215 | close(gpio_in);
|
---|
216 | }
|
---|
217 | #endif
|
---|
218 | IO_Serial_Close();
|
---|
219 | cs_debug_mask (D_IFD, "IFD: Closing phoenix device %s", reader[ridx].device);
|
---|
220 | return OK;
|
---|
221 | }
|
---|