source: trunk/csctapi/ifd_sc8in1.c@ 2980

Last change on this file since 2980 was 2980, checked in by rorothetroll, 10 years ago

new patch to save emm even if we block them

File size: 8.8 KB
Line 
1/*
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19#include <stdio.h>
20//#include <time.h>
21//#include <string.h>
22//#include "ioctls.h"
23#include "../globals.h"
24#include "atr.h"
25#include <termios.h>
26#include "ifd_sc8in1.h"
27#include "io_serial.h"
28#include "icc_async.h"
29
30static struct termios stored_termio[8];//FIXME no globals please
31static int current_slot; //FIXME should not be a global, but one per SC8in1
32static unsigned char cardstatus; //FIXME not global but one per SC8in1 //if not static, the threads dont share same cardstatus!
33
34#define MAX_TRANSMIT 255
35
36static int sc8in1_command(struct s_reader * reader, unsigned char * buff, unsigned short lenwrite, unsigned short lenread)
37{
38 int init_phase = (buff[0] == 0x63); //FIXME UGLY
39 struct termios termio, termiobackup;
40
41 // backup data
42 tcgetattr(reader->handle,&termio);
43 memcpy(&termiobackup,&termio,sizeof(termio));
44
45 // switch SC8in1 to command mode
46 IO_Serial_DTR_Set(reader);
47
48 // set communication parameters
49 termio.c_oflag = 0;
50 termio.c_lflag = 0;
51 termio.c_cc[VTIME] = 1; // working
52 termio.c_cflag = B9600|CS8|CREAD|CLOCAL;
53 if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
54 cs_log("ERROR: SC8in1 Command error in set RS232 attributes\n");
55 return ERROR;
56 }
57 cs_ddump_mask (D_DEVICE, buff, lenwrite, "IO: Sending: ");
58 if (!write(reader->handle, buff, lenwrite)) { //dont use IO_Serial_Write since mcr commands dont echo back
59 cs_log("SC8in1 Command write error");
60 return ERROR;
61 }
62 tcdrain(reader->handle);
63 // give some time back to the system .. we're in a thread after all
64 sched_yield();
65
66 if (IO_Serial_Read (reader, 1000, lenread, buff) == ERROR) {
67 cs_log("SC8in1 Command read error");
68 return ERROR;
69 }
70 // give some time back to the system .. we're in a thread after all
71 sched_yield();
72 // restore data
73 memcpy(&termio,&termiobackup,sizeof(termio));
74 if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
75 cs_log("ERROR: SC8in1 Command error in restore RS232 attributes\n");
76 return ERROR;
77 }
78 if(!init_phase)
79 // switch SC8in1 to normal mode
80 IO_Serial_DTR_Clr(reader);
81 // give some time back to the system .. we're in a thread after all
82 sched_yield();
83 return OK;
84}
85
86static int readsc8in1(struct s_reader * reader)
87{
88 // Reads the card status
89 //
90 // the bits in the return bytes:
91 // bit0=1 means Slot1=Smartcard inside
92 // bit1=1 means Slot2=Smartcard inside
93 // bit2=1 means Slot3=Smartcard inside
94 // bit3=1 means Slot4=Smartcard inside
95 // bit4=1 means Slot5=Smartcard inside
96 // bit5=1 means Slot6=Smartcard inside
97 // bit6=1 means Slot7=Smartcard inside
98 // bit7=1 means Slot8=Smartcard inside
99 unsigned char buf[10];
100 buf[0]=0x47;
101 IO_Serial_Flush(reader);
102 if (sc8in1_command(reader, buf, 1, 8) < 0) return (-1);
103 if (buf[1]!=0x90) return(-1);
104
105 // return result byte
106 return(buf[2]);
107}
108
109int Sc8in1_Selectslot(struct s_reader * reader, int slot) {
110 // selects the Smartcard Socket "slot"
111 //
112 if (slot == current_slot)
113 return OK;
114 cs_log("SC8in1: select slot %i", slot);
115 int res;
116 unsigned char tmp[128];
117 struct termios termio;
118 //cs_sleepms(10); //FIXME do I need this?
119 // backup rs232 data
120 tcgetattr(reader->handle,&termio);
121 if (current_slot != 0)
122 memcpy(&stored_termio[current_slot-1],&termio,sizeof(termio));
123 //
124 // switch SC8in1 to command mode
125 IO_Serial_DTR_Set(reader);
126 // give some time back to the system .. we're in a thread after all
127 sched_yield();
128
129 // set communication parameters
130 termio.c_cc[VTIME] = 1; // working
131 termio.c_cflag = B9600|CS8|CREAD|CLOCAL;
132 if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
133 cs_log("ERROR: SC8in1 selectslot set RS232 attributes\n");
134 return ERROR;
135 }
136 tcflush(reader->handle, TCIOFLUSH);
137 // selecd select slot command to SC8in1
138 //tmp[0]=0x73; //MCR command
139 tmp[0]=0x53;
140 tmp[1]=slot&0x0F;
141 IO_Serial_Write (reader, 0, 2, tmp);
142 // give some time back to the system .. we're in a thread after all
143 sched_yield();
144 tcdrain(reader->handle);
145 //tcflush(reader->handle, TCIOFLUSH);
146 res=IO_Serial_Read (reader, 1000, 4, tmp); // ignore reader response of 4 bytes
147 current_slot = slot;
148 // give some time back to the system .. we're in a thread after all
149 sched_yield();
150 tcdrain(reader->handle);
151 // restore rs232 data
152 memcpy(&termio, &stored_termio[reader->slot-1], sizeof(termio));
153 if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
154 cs_log("ERROR: SC8in1 selectslot restore RS232 attributes\n");
155 return ERROR;
156 }
157 // switch SC8in1 to normal mode
158 IO_Serial_DTR_Clr(reader);
159 //cs_sleepms(10); //FIXME do I need this?
160 // give some time back to the system .. we're in a thread after all
161 sched_yield();
162 return OK;
163}
164
165int Sc8in1_Init(struct s_reader * reader)
166{
167 //additional init, Phoenix_Init is also called for Sc8in1 !
168 struct termios termio;
169 int i,pos, speed,fd = reader->handle;
170 unsigned int is_mcr, sc8in1_clock = 0;
171
172 tcgetattr(reader->handle,&termio);
173 for (i=0; i<8; i++) {
174 //init all stored termios to default comm settings after device init, before ATR
175 memcpy(&stored_termio[i],&termio,sizeof(termio));
176 }
177
178 // check for a MCR device and how many slots it has.
179 unsigned char buff[] = { 0x74 };
180 sc8in1_command(reader, buff, 1, 1);
181 if (buff[0] == 4 || buff[0] == 8) {
182 is_mcr = (unsigned short) buff[0];
183 cs_log("SC8in1: device MCR%i detected", is_mcr);
184 }
185 else
186 is_mcr = 0;
187
188 tcflush(reader->handle, TCIOFLUSH); // a non MCR reader might give longer answer
189
190 for (i=0; i<CS_MAXREADER; i++) //copy handle to other slots, FIXME change this if multiple sc8in1 readers
191 if (reader[i].typ == R_SC8in1) {
192 if (reader[i].slot == 0) {//not initialized yet
193 pos = strlen(reader[i].device)-2; //this is where : should be located; is also valid length of physical device name
194 if (reader[i].device[pos] != 0x3a) //0x3a = ":"
195 cs_log("ERROR: '%c' detected instead of slot separator `:` at second to last position of device %s", reader[i].device[pos], reader[i].device);
196 reader[i].slot=(int)reader[i].device[pos+1] - 0x30;//FIXME test boundaries
197 reader[i].device[pos]= 0; //slot 1 reader now gets correct physicalname
198 }
199 reader[i].handle = fd;
200 }
201
202 if (is_mcr) {
203 //if MCR set clock
204 switch (reader->mhz) {
205 case 357:
206 case 358:
207 speed=0;
208 break;
209 case 368:
210 case 369:
211 speed = 1;
212 break;
213 case 600:
214 speed = 2;
215 break;
216 case 800:
217 speed = 3;
218 break;
219 default:
220 speed = 0;
221 cs_log("ERROR Sc8in1, cannot set clockspeed to %i", reader->mhz);
222 break;
223 }
224 sc8in1_clock |= (speed << (reader[i].slot - 1) * 2);
225 buff[0] = 0x63; //MCR set clock
226 buff[1] = (sc8in1_clock >> 8) & 0xFF;
227 buff[2] = sc8in1_clock & 0xFF;
228 sc8in1_command(reader, buff, 3, 0);
229 }
230
231 //IO_Serial_Flush(reader); //FIXME somehow ATR is generated and must be flushed
232 i = -1; //Flag for GetStatus init
233 Sc8in1_GetStatus(reader, &i); //Initialize cardstatus
234
235 return OK;
236}
237
238int Sc8in1_Card_Changed(struct s_reader * reader) {
239 // returns the SC8in1 Status
240 // 0= no card was changed (inserted or removed)
241 // -1= one ore more cards were changed (inserted or removed)
242 int result;
243 int lineData;
244 ioctl(reader->handle, TIOCMGET, &lineData);
245 result= (lineData & TIOCM_CTS) / TIOCM_CTS;
246 // give some time back to the system .. we're in a thread after all
247 sched_yield();
248 return(result-1);
249}
250
251int Sc8in1_GetStatus (struct s_reader * reader, int * in)
252{
253 if (Sc8in1_Card_Changed(reader)|| *in == -1) { //FIXME what happens if slot 1 has no reader defined
254 cs_debug("SC8in1: locking for Getstatus for slot %i",reader->slot);
255 pthread_mutex_lock(&sc8in1);
256 cs_debug("SC8in1: locked for Getstatus for slot %i",reader->slot);
257 int i=readsc8in1(reader); //read cardstatus
258 pthread_mutex_unlock(&sc8in1);
259 cs_debug("SC8in1: unlocked for Getstatus for slot %i",reader->slot);
260 if (i < 0) {
261 // give some time back to the system .. we're in a thread after all
262 sched_yield();
263 return ERROR;
264 }
265 cardstatus = i;
266 }
267 *in = (cardstatus & 1<<(reader->slot-1));
268 // give some time back to the system .. we're in a thread after all
269 sched_yield();
270 return OK;
271}
Note: See TracBrowser for help on using the repository browser.