1 | //FIXME Only threadsafe for ONE sc8in1/MCR reader; two readers WILL go wrong !!!
|
---|
2 | /*
|
---|
3 | *
|
---|
4 | * This program is free software; you can redistribute it and/or modify
|
---|
5 | * it under the terms of the GNU General Public License as published by
|
---|
6 | * the Free Software Foundation; either version 2 of the License, or
|
---|
7 | * (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This program is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 | *
|
---|
14 | * You should have received a copy of the GNU General Public License
|
---|
15 | * along with this program; if not, write to the Free Software
|
---|
16 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
17 | *
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <stdio.h>
|
---|
21 | //#include <time.h>
|
---|
22 | //#include <string.h>
|
---|
23 | //#include "ioctls.h"
|
---|
24 | #include "../globals.h"
|
---|
25 | #include "atr.h"
|
---|
26 | #include <termios.h>
|
---|
27 | #include "ifd_sc8in1.h"
|
---|
28 | #include "io_serial.h"
|
---|
29 | #include "icc_async.h"
|
---|
30 |
|
---|
31 | static struct termios stored_termio[8];//FIXME no globals please
|
---|
32 | static int32_t current_slot; //FIXME should not be a global, but one per SC8in1
|
---|
33 | static unsigned char cardstatus; //FIXME not global but one per SC8in1 //if not static, the threads dont share same cardstatus!
|
---|
34 |
|
---|
35 | static int32_t sc8in1_command(struct s_reader * reader, unsigned char * buff, uint16_t lenwrite, uint16_t lenread)
|
---|
36 | {
|
---|
37 | struct termios termio, termiobackup;
|
---|
38 |
|
---|
39 | // backup data
|
---|
40 | tcgetattr(reader->handle,&termio);
|
---|
41 | memcpy(&termiobackup,&termio,sizeof(termio));
|
---|
42 |
|
---|
43 | // switch SC8in1 to command mode
|
---|
44 | IO_Serial_DTR_Set(reader);
|
---|
45 |
|
---|
46 | // set communication parameters
|
---|
47 | termio.c_oflag = 0;
|
---|
48 | termio.c_lflag = 0;
|
---|
49 | termio.c_cc[VTIME] = 1; // working
|
---|
50 | termio.c_cflag = B9600|CS8|CREAD|CLOCAL;
|
---|
51 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
52 | cs_log("ERROR: SC8in1 Command error in set RS232 attributes\n");
|
---|
53 | return ERROR;
|
---|
54 | }
|
---|
55 | cs_ddump_mask (D_DEVICE, buff, lenwrite, "IO: Sending: ");
|
---|
56 | if (!write(reader->handle, buff, lenwrite)) { //dont use IO_Serial_Write since mcr commands dont echo back
|
---|
57 | cs_log("SC8in1 Command write error");
|
---|
58 | return ERROR;
|
---|
59 | }
|
---|
60 | tcdrain(reader->handle);
|
---|
61 | // give some time back to the system .. we're in a thread after all
|
---|
62 | sched_yield();
|
---|
63 |
|
---|
64 | if (IO_Serial_Read (reader, 1000, lenread, buff) == ERROR) {
|
---|
65 | cs_log("SC8in1 Command read error");
|
---|
66 | return ERROR;
|
---|
67 | }
|
---|
68 | // give some time back to the system .. we're in a thread after all
|
---|
69 | sched_yield();
|
---|
70 | // restore data
|
---|
71 | memcpy(&termio,&termiobackup,sizeof(termio));
|
---|
72 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
73 | cs_log("ERROR: SC8in1 Command error in restore RS232 attributes\n");
|
---|
74 | return ERROR;
|
---|
75 | }
|
---|
76 | // switch SC8in1 to normal mode
|
---|
77 | IO_Serial_DTR_Clr(reader);
|
---|
78 | // give some time back to the system .. we're in a thread after all
|
---|
79 | sched_yield();
|
---|
80 | return OK;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static int32_t readsc8in1(struct s_reader * reader)
|
---|
84 | {
|
---|
85 | // Reads the card status
|
---|
86 | //
|
---|
87 | // the bits in the return bytes:
|
---|
88 | // bit0=1 means Slot1=Smartcard inside
|
---|
89 | // bit1=1 means Slot2=Smartcard inside
|
---|
90 | // bit2=1 means Slot3=Smartcard inside
|
---|
91 | // bit3=1 means Slot4=Smartcard inside
|
---|
92 | // bit4=1 means Slot5=Smartcard inside
|
---|
93 | // bit5=1 means Slot6=Smartcard inside
|
---|
94 | // bit6=1 means Slot7=Smartcard inside
|
---|
95 | // bit7=1 means Slot8=Smartcard inside
|
---|
96 | unsigned char buf[10];
|
---|
97 | buf[0]=0x47;
|
---|
98 | IO_Serial_Flush(reader);
|
---|
99 | if (sc8in1_command(reader, buf, 1, 8) < 0) return (-1);
|
---|
100 | if (buf[1]!=0x90) return(-1);
|
---|
101 |
|
---|
102 | // return result byte
|
---|
103 | return(buf[2]);
|
---|
104 | }
|
---|
105 |
|
---|
106 | int32_t Sc8in1_Selectslot(struct s_reader * reader, int32_t slot) {
|
---|
107 | // selects the Smartcard Socket "slot"
|
---|
108 | //
|
---|
109 | if (slot == current_slot)
|
---|
110 | return OK;
|
---|
111 | cs_debug_mask(D_DEVICE, "SC8in1: select slot %i", slot);
|
---|
112 | int32_t res;
|
---|
113 | unsigned char tmp[128];
|
---|
114 | struct termios termio;
|
---|
115 | //cs_sleepms(10); //FIXME do I need this?
|
---|
116 | // backup rs232 data
|
---|
117 | tcgetattr(reader->handle,&termio);
|
---|
118 | if (current_slot != 0)
|
---|
119 | memcpy(&stored_termio[current_slot-1],&termio,sizeof(termio)); //not if current_slot is undefine
|
---|
120 | //
|
---|
121 | // switch SC8in1 to command mode
|
---|
122 | IO_Serial_DTR_Set(reader);
|
---|
123 | // give some time back to the system .. we're in a thread after all
|
---|
124 | sched_yield();
|
---|
125 |
|
---|
126 | // set communication parameters
|
---|
127 | termio.c_cc[VTIME] = 1; // working
|
---|
128 | termio.c_cflag = B9600|CS8|CREAD|CLOCAL;
|
---|
129 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
130 | cs_log("ERROR: SC8in1 selectslot set RS232 attributes\n");
|
---|
131 | return ERROR;
|
---|
132 | }
|
---|
133 | tcflush(reader->handle, TCIOFLUSH);
|
---|
134 | // selecd select slot command to SC8in1
|
---|
135 | //tmp[0]=0x73; //MCR command
|
---|
136 | tmp[0]=0x53;
|
---|
137 | tmp[1]=slot&0x0F;
|
---|
138 | IO_Serial_Write (reader, 0, 2, tmp);
|
---|
139 | // give some time back to the system .. we're in a thread after all
|
---|
140 | sched_yield();
|
---|
141 | tcdrain(reader->handle);
|
---|
142 | //tcflush(reader->handle, TCIOFLUSH);
|
---|
143 | res=IO_Serial_Read (reader, 1000, 4, tmp); // ignore reader response of 4 bytes
|
---|
144 | current_slot = slot;
|
---|
145 | // give some time back to the system .. we're in a thread after all
|
---|
146 | sched_yield();
|
---|
147 | tcdrain(reader->handle);
|
---|
148 | // restore rs232 data
|
---|
149 | memcpy(&termio, &stored_termio[reader->slot-1], sizeof(termio));
|
---|
150 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
151 | cs_log("ERROR: SC8in1 selectslot restore RS232 attributes\n");
|
---|
152 | return ERROR;
|
---|
153 | }
|
---|
154 | // switch SC8in1 to normal mode
|
---|
155 | IO_Serial_DTR_Clr(reader);
|
---|
156 | //cs_sleepms(10); //FIXME do I need this?
|
---|
157 | // give some time back to the system .. we're in a thread after all
|
---|
158 | sched_yield();
|
---|
159 | return OK;
|
---|
160 | }
|
---|
161 |
|
---|
162 | int32_t Sc8in1_Init(struct s_reader * reader)
|
---|
163 | {
|
---|
164 | //additional init, Phoenix_Init is also called for Sc8in1 !
|
---|
165 | struct termios termio;
|
---|
166 | int32_t i, speed;
|
---|
167 | uint32_t is_mcr = 0, sc8in1_clock = 0;
|
---|
168 | unsigned char buff[3];
|
---|
169 |
|
---|
170 | tcgetattr(reader->handle,&termio);
|
---|
171 | for (i=0; i<8; i++) {
|
---|
172 | //init all stored termios to default comm settings after device init, before ATR
|
---|
173 | memcpy(&stored_termio[i],&termio,sizeof(termio)); //FIXME overclocking factor not taken into account?
|
---|
174 | }
|
---|
175 |
|
---|
176 | // check for a MCR device and how many slots it has.
|
---|
177 | buff[0] = 0x74;
|
---|
178 | sc8in1_command(reader, buff, 1, 1);
|
---|
179 | if (buff[0] == 4 || buff[0] == 8) {
|
---|
180 | is_mcr = (uint16_t) buff[0];
|
---|
181 | cs_log("SC8in1: device MCR%i detected", is_mcr);
|
---|
182 |
|
---|
183 | //now work-around the problem that timeout of MCR has to be 0 in case of USB
|
---|
184 | buff[0] = 0x72; //get timeout
|
---|
185 | buff[1] = 0;
|
---|
186 | buff[2] = 0;
|
---|
187 | int32_t ret = sc8in1_command(reader, buff, 1, 2);
|
---|
188 | if ((strstr(reader->device, "USB")) && (ret == ERROR || buff[0] != 0 || buff[1] != 0)) {//assuming we are connected thru USB and timeout is undetected or not zero
|
---|
189 | cs_log("DINGO: Detected Sc8in1 device connected with USB, setting timeout to 0 and writing to EEPROM");
|
---|
190 | buff[0] = 0x70; //enable write EEPROM
|
---|
191 | buff[1] = 0xab;
|
---|
192 | buff[2] = 0xba;
|
---|
193 | sc8in1_command(reader, buff, 3, 0);
|
---|
194 | buff[0] = 0x6F; //set timeout
|
---|
195 | buff[1] = 0;
|
---|
196 | buff[2] = 0;
|
---|
197 | sc8in1_command(reader, buff, 3, 0);
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | tcflush(reader->handle, TCIOFLUSH); // a non MCR reader might give longer answer
|
---|
202 |
|
---|
203 | struct s_reader *rdr;
|
---|
204 | LL_ITER itr = ll_iter_create(configured_readers);
|
---|
205 | while((rdr = ll_iter_next(&itr))) //also do this for disabled readers, so we configure the clocks right for all readers
|
---|
206 | if (rdr->handle == reader->handle) { //corresponding slot
|
---|
207 |
|
---|
208 | //check slot boundaries
|
---|
209 | int32_t upper_slot = (is_mcr)? is_mcr : 8; //set upper limit to 8 for non MCR readers
|
---|
210 | if (rdr->slot <= 0 || rdr->slot > upper_slot) {
|
---|
211 | cs_log("ERROR: device %s has invalid slot number %i", rdr->device, rdr->slot);
|
---|
212 | return ERROR;
|
---|
213 | }
|
---|
214 |
|
---|
215 | if (is_mcr) {
|
---|
216 | //set RTS for every slot to 1 to prevent jitter/glitch detection problems
|
---|
217 | Sc8in1_Selectslot(rdr, rdr->slot);
|
---|
218 | IO_Serial_RTS_Set(reader);
|
---|
219 |
|
---|
220 | //calculate clock-bits
|
---|
221 | switch (rdr->mhz) {
|
---|
222 | case 357:
|
---|
223 | case 358:
|
---|
224 | speed=0;
|
---|
225 | break;
|
---|
226 | case 368:
|
---|
227 | case 369:
|
---|
228 | speed = 1;
|
---|
229 | break;
|
---|
230 | case 600:
|
---|
231 | speed = 2;
|
---|
232 | break;
|
---|
233 | case 800:
|
---|
234 | speed = 3;
|
---|
235 | break;
|
---|
236 | default:
|
---|
237 | speed = 0;
|
---|
238 | cs_log("ERROR Sc8in1, cannot set clockspeed to %i", rdr->mhz);
|
---|
239 | break;
|
---|
240 | }
|
---|
241 | sc8in1_clock |= (speed << (rdr->slot - 1) * 2);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (is_mcr) {
|
---|
246 | //set clockspeeds for all slots
|
---|
247 | buff[0] = 0x63; //MCR set clock
|
---|
248 | buff[1] = (sc8in1_clock >> 8) & 0xFF;
|
---|
249 | buff[2] = sc8in1_clock & 0xFF;
|
---|
250 | sc8in1_command(reader, buff, 3, 0); //FIXME this doesnt work properly yet
|
---|
251 |
|
---|
252 | //DEBUG get clockspeeds
|
---|
253 | buff[0] = 0x67;
|
---|
254 | sc8in1_command(reader, buff, 1, 2);
|
---|
255 | static char * clock[] = { "3,57", "3,68", "6,00", "8,00" };
|
---|
256 | uint16_t result = buff[0]<<8 | buff[1];
|
---|
257 | for(i=0; i<8; i++) {
|
---|
258 | cs_log("Slot %i is clocked with %s mhz", i+1, clock[(result>>(i*2))&0X0003]);
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | //IO_Serial_Flush(reader); //FIXME somehow ATR is generated and must be flushed
|
---|
263 | i = -1; //Flag for GetStatus init
|
---|
264 | Sc8in1_GetStatus(reader, &i); //Initialize cardstatus
|
---|
265 |
|
---|
266 | return OK;
|
---|
267 | }
|
---|
268 |
|
---|
269 | int32_t Sc8in1_Card_Changed(struct s_reader * reader) {
|
---|
270 | // returns the SC8in1 Status
|
---|
271 | // 0= no card was changed (inserted or removed)
|
---|
272 | // -1= one ore more cards were changed (inserted or removed)
|
---|
273 | int32_t result;
|
---|
274 | int32_t lineData;
|
---|
275 | ioctl(reader->handle, TIOCMGET, &lineData);
|
---|
276 | result= (lineData & TIOCM_CTS) / TIOCM_CTS;
|
---|
277 | // give some time back to the system .. we're in a thread after all
|
---|
278 | sched_yield();
|
---|
279 | return(result-1);
|
---|
280 | }
|
---|
281 |
|
---|
282 | int32_t Sc8in1_GetStatus (struct s_reader * reader, int32_t * in)
|
---|
283 | {
|
---|
284 | if (Sc8in1_Card_Changed(reader)|| *in == -1) {
|
---|
285 | int32_t i=readsc8in1(reader); //read cardstatus
|
---|
286 | if (i < 0) {
|
---|
287 | // give some time back to the system .. we're in a thread after all
|
---|
288 | sched_yield();
|
---|
289 | return ERROR;
|
---|
290 | }
|
---|
291 | cardstatus = i;
|
---|
292 | }
|
---|
293 | *in = (cardstatus & 1<<(reader->slot-1));
|
---|
294 | // give some time back to the system .. we're in a thread after all
|
---|
295 | sched_yield();
|
---|
296 | return OK;
|
---|
297 | }
|
---|