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 |
|
---|
30 | static struct termios stored_termio[8];//FIXME no globals please
|
---|
31 | static int current_slot; //FIXME should not be a global, but one per SC8in1
|
---|
32 | static 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 |
|
---|
36 | static 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("ERROR: SC8in1 Command write error");
|
---|
60 | return ERROR;
|
---|
61 | }
|
---|
62 | tcdrain(reader->handle);
|
---|
63 | if (IO_Serial_Read (reader, 1000, lenread, buff) == ERROR) {
|
---|
64 | cs_log("SC8in1 Command read error");
|
---|
65 | return ERROR;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // restore data
|
---|
69 | memcpy(&termio,&termiobackup,sizeof(termio));
|
---|
70 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
71 | cs_log("ERROR: SC8in1 Command error in restore RS232 attributes\n");
|
---|
72 | return ERROR;
|
---|
73 | }
|
---|
74 |
|
---|
75 | // switch SC8in1 to normal mode
|
---|
76 | IO_Serial_DTR_Clr(reader);
|
---|
77 | return OK;
|
---|
78 | }
|
---|
79 |
|
---|
80 | static int readsc8in1(struct s_reader * reader)
|
---|
81 | {
|
---|
82 | // Reads the card status
|
---|
83 | //
|
---|
84 | // the bits in the return bytes:
|
---|
85 | // bit0=1 means Slot1=Smartcard inside
|
---|
86 | // bit1=1 means Slot2=Smartcard inside
|
---|
87 | // bit2=1 means Slot3=Smartcard inside
|
---|
88 | // bit3=1 means Slot4=Smartcard inside
|
---|
89 | // bit4=1 means Slot5=Smartcard inside
|
---|
90 | // bit5=1 means Slot6=Smartcard inside
|
---|
91 | // bit6=1 means Slot7=Smartcard inside
|
---|
92 | // bit7=1 means Slot8=Smartcard inside
|
---|
93 | unsigned char buf[10];
|
---|
94 | buf[0]=0x47;
|
---|
95 | if (sc8in1_command(reader, buf, 1, 8) < 0) return (-1);
|
---|
96 | if (buf[1]!=0x90) return(-1);
|
---|
97 |
|
---|
98 | // return result byte
|
---|
99 | return(buf[2]);
|
---|
100 | }
|
---|
101 |
|
---|
102 | int Sc8in1_Selectslot(struct s_reader * reader, int slot) {
|
---|
103 | // selects the Smartcard Socket "slot"
|
---|
104 | //
|
---|
105 | if (slot == current_slot)
|
---|
106 | return OK;
|
---|
107 | cs_log("SC8in1: select slot %i", slot);
|
---|
108 | int res;
|
---|
109 | unsigned char tmp[128];
|
---|
110 | struct termios termio;
|
---|
111 | //cs_sleepms(10); //FIXME do I need this?
|
---|
112 | // backup rs232 data
|
---|
113 | tcgetattr(reader->handle,&termio);
|
---|
114 | if (current_slot != 0)
|
---|
115 | memcpy(&stored_termio[current_slot-1],&termio,sizeof(termio));
|
---|
116 | //
|
---|
117 | // switch SC8in1 to command mode
|
---|
118 | IO_Serial_DTR_Set(reader);
|
---|
119 | // set communication parameters
|
---|
120 | termio.c_cc[VTIME] = 1; // working
|
---|
121 | termio.c_cflag = B9600|CS8|CREAD|CLOCAL;
|
---|
122 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
123 | cs_log("ERROR: SC8in1 selectslot set RS232 attributes\n");
|
---|
124 | return ERROR;
|
---|
125 | }
|
---|
126 | tcflush(reader->handle, TCIOFLUSH);
|
---|
127 | // selecd select slot command to SC8in1
|
---|
128 | //tmp[0]=0x73; //MCR command
|
---|
129 | tmp[0]=0x53;
|
---|
130 | tmp[1]=slot&0x0F;
|
---|
131 | IO_Serial_Write (reader, 0, 2, tmp);
|
---|
132 | tcdrain(reader->handle);
|
---|
133 | //tcflush(reader->handle, TCIOFLUSH);
|
---|
134 | res=IO_Serial_Read (reader, 1000, 4, tmp); // ignore reader response of 4 bytes
|
---|
135 | current_slot = slot;
|
---|
136 | tcdrain(reader->handle);
|
---|
137 | // restore rs232 data
|
---|
138 | memcpy(&termio, &stored_termio[reader->slot-1], sizeof(termio));
|
---|
139 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
140 | cs_log("ERROR: SC8in1 selectslot restore RS232 attributes\n");
|
---|
141 | return ERROR;
|
---|
142 | }
|
---|
143 | // switch SC8in1 to normal mode
|
---|
144 | IO_Serial_DTR_Clr(reader);
|
---|
145 | //cs_sleepms(10); //FIXME do I need this?
|
---|
146 | return OK;
|
---|
147 | }
|
---|
148 |
|
---|
149 | int Sc8in1_Init(struct s_reader * reader)
|
---|
150 | {
|
---|
151 | //additional init, Phoenix_Init is also called for Sc8in1 !
|
---|
152 | struct termios termio;
|
---|
153 | int i,pos, speed,fd = reader->handle;
|
---|
154 | unsigned int is_mcr, sc8in1_clock = 0;
|
---|
155 | tcgetattr(reader->handle,&termio);
|
---|
156 | for (i=0; i<8; i++)
|
---|
157 | //init all stored termios to default comm settings after device init, before ATR
|
---|
158 | memcpy(&stored_termio[i],&termio,sizeof(termio));
|
---|
159 | unsigned char buff[] = { 0x74 };
|
---|
160 | sc8in1_command(reader, buff, 1, 1);
|
---|
161 | if (buff[0] == 4 || buff[0] == 8) {
|
---|
162 | is_mcr = (unsigned short) buff[0];
|
---|
163 | cs_log("SC8in1: device MCR%i detected", is_mcr);
|
---|
164 | }
|
---|
165 | else
|
---|
166 | is_mcr = 0;
|
---|
167 | tcflush(reader->handle, TCIOFLUSH); // a non MCR reader might give longer answer
|
---|
168 | for (i=0; i<CS_MAXREADER; i++) //copy handle to other slots, FIXME change this if multiple sc8in1 readers
|
---|
169 | if (reader[i].typ == R_SC8in1) {
|
---|
170 | if (reader[i].slot == 0) {//not initialized yet
|
---|
171 | pos = strlen(reader[i].device)-2; //this is where : should be located; is also valid length of physical device name
|
---|
172 | if (reader[i].device[pos] != 0x3a) //0x3a = ":"
|
---|
173 | cs_log("ERROR: '%c' detected instead of slot separator `:` at second to last position of device %s", reader[i].device[pos], reader[i].device);
|
---|
174 | reader[i].slot=(int)reader[i].device[pos+1] - 0x30;//FIXME test boundaries
|
---|
175 | reader[i].device[pos]= 0; //slot 1 reader now gets correct physicalname
|
---|
176 | }
|
---|
177 | reader[i].handle = fd;
|
---|
178 | if (!is_mcr)
|
---|
179 | continue;
|
---|
180 | //if MCR set clock
|
---|
181 | switch (reader[i].mhz) {
|
---|
182 | case 357:
|
---|
183 | case 358:
|
---|
184 | continue;
|
---|
185 | case 368:
|
---|
186 | case 369:
|
---|
187 | speed = 1;
|
---|
188 | break;
|
---|
189 | case 600:
|
---|
190 | speed = 2;
|
---|
191 | break;
|
---|
192 | case 800:
|
---|
193 | speed = 3;
|
---|
194 | break;
|
---|
195 | default:
|
---|
196 | speed = 0;
|
---|
197 | cs_log("ERROR Sc8in1, cannot set clockspeed to %i", reader->mhz);
|
---|
198 | break;
|
---|
199 | }
|
---|
200 | sc8in1_clock |= (speed << (reader[i].slot - 1) * 2);
|
---|
201 | }
|
---|
202 | if (is_mcr) {
|
---|
203 | buff[0] = 0x63; //MCR set clock
|
---|
204 | buff[1] = (sc8in1_clock >> 8) & 0xFF;
|
---|
205 | buff[2] = sc8in1_clock & 0xFF;
|
---|
206 | sc8in1_command(reader, buff, 3, 0);
|
---|
207 | }
|
---|
208 |
|
---|
209 | //IO_Serial_Flush(reader); //FIXME somehow ATR is generated and must be flushed
|
---|
210 | i = -1; //Flag for GetStatus init
|
---|
211 | Sc8in1_GetStatus(reader, &i); //Initialize cardstatus
|
---|
212 |
|
---|
213 | return OK;
|
---|
214 | }
|
---|
215 |
|
---|
216 | int Sc8in1_Card_Changed(struct s_reader * reader) {
|
---|
217 | // returns the SC8in1 Status
|
---|
218 | // 0= no card was changed (inserted or removed)
|
---|
219 | // -1= one ore more cards were changed (inserted or removed)
|
---|
220 | int result;
|
---|
221 | int lineData;
|
---|
222 | ioctl(reader->handle, TIOCMGET, &lineData);
|
---|
223 | result= (lineData & TIOCM_CTS) / TIOCM_CTS;
|
---|
224 | return(result-1);
|
---|
225 | }
|
---|
226 |
|
---|
227 | int Sc8in1_GetStatus (struct s_reader * reader, int * in)
|
---|
228 | {
|
---|
229 | if (Sc8in1_Card_Changed(reader)|| *in == -1) { //FIXME what happens if slot 1 has no reader defined
|
---|
230 | cs_debug("SC8in1: locking for Getstatus for slot %i",reader->slot);
|
---|
231 | pthread_mutex_lock(&sc8in1);
|
---|
232 | cs_debug("SC8in1: locked for Getstatus for slot %i",reader->slot);
|
---|
233 | int i=readsc8in1(reader); //read cardstatus
|
---|
234 | pthread_mutex_unlock(&sc8in1);
|
---|
235 | cs_debug("SC8in1: unlocked for Getstatus for slot %i",reader->slot);
|
---|
236 | if (i < 0)
|
---|
237 | return ERROR;
|
---|
238 | cardstatus = i;
|
---|
239 | }
|
---|
240 | *in = (cardstatus & 1<<(reader->slot-1));
|
---|
241 | return OK;
|
---|
242 | }
|
---|