[1926] | 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 |
|
---|
[1960] | 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 |
|
---|
[1926] | 34 | #define MAX_TRANSMIT 255
|
---|
| 35 |
|
---|
[1960] | 36 | static int sc8in1_command(struct s_reader * reader, unsigned char * buff, unsigned short lenwrite, unsigned short lenread)
|
---|
| 37 | {
|
---|
[2291] | 38 | int init_phase = (buff[0] == 0x63); //FIXME UGLY
|
---|
[1926] | 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) {
|
---|
[1962] | 54 | cs_log("ERROR: SC8in1 Command error in set RS232 attributes\n");
|
---|
[1960] | 55 | return ERROR;
|
---|
[1926] | 56 | }
|
---|
[1962] | 57 | cs_ddump_mask (D_DEVICE, buff, lenwrite, "IO: Sending: ");
|
---|
[2291] | 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 | }
|
---|
[1926] | 62 | tcdrain(reader->handle);
|
---|
[2291] | 63 | // give some time back to the system .. we're in a thread after all
|
---|
| 64 | sched_yield();
|
---|
| 65 |
|
---|
[1960] | 66 | if (IO_Serial_Read (reader, 1000, lenread, buff) == ERROR) {
|
---|
[1966] | 67 | cs_log("SC8in1 Command read error");
|
---|
[1960] | 68 | return ERROR;
|
---|
[1926] | 69 | }
|
---|
[2291] | 70 | // give some time back to the system .. we're in a thread after all
|
---|
| 71 | sched_yield();
|
---|
[1926] | 72 | // restore data
|
---|
| 73 | memcpy(&termio,&termiobackup,sizeof(termio));
|
---|
| 74 | if (tcsetattr(reader->handle,TCSANOW,&termio) < 0) {
|
---|
[1962] | 75 | cs_log("ERROR: SC8in1 Command error in restore RS232 attributes\n");
|
---|
[1960] | 76 | return ERROR;
|
---|
[1926] | 77 | }
|
---|
[2291] | 78 | if(!init_phase)
|
---|
[2021] | 79 | // switch SC8in1 to normal mode
|
---|
[2291] | 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;
|
---|
[1960] | 84 | }
|
---|
[1926] | 85 |
|
---|
[1960] | 86 | static 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;
|
---|
[2291] | 101 | IO_Serial_Flush(reader);
|
---|
| 102 | if (sc8in1_command(reader, buf, 1, 8) < 0) return (-1);
|
---|
[1962] | 103 | if (buf[1]!=0x90) return(-1);
|
---|
[1926] | 104 |
|
---|
| 105 | // return result byte
|
---|
[1962] | 106 | return(buf[2]);
|
---|
[1926] | 107 | }
|
---|
| 108 |
|
---|
[1960] | 109 | int Sc8in1_Selectslot(struct s_reader * reader, int slot) {
|
---|
[1926] | 110 | // selects the Smartcard Socket "slot"
|
---|
| 111 | //
|
---|
| 112 | if (slot == current_slot)
|
---|
[1960] | 113 | return OK;
|
---|
[1933] | 114 | cs_log("SC8in1: select slot %i", slot);
|
---|
[1926] | 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);
|
---|
[1933] | 121 | if (current_slot != 0)
|
---|
| 122 | memcpy(&stored_termio[current_slot-1],&termio,sizeof(termio));
|
---|
[1926] | 123 | //
|
---|
| 124 | // switch SC8in1 to command mode
|
---|
| 125 | IO_Serial_DTR_Set(reader);
|
---|
[2291] | 126 | // give some time back to the system .. we're in a thread after all
|
---|
| 127 | sched_yield();
|
---|
| 128 |
|
---|
[1926] | 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");
|
---|
[1960] | 134 | return ERROR;
|
---|
[1926] | 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);
|
---|
[2291] | 142 | // give some time back to the system .. we're in a thread after all
|
---|
| 143 | sched_yield();
|
---|
[1926] | 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;
|
---|
[2291] | 148 | // give some time back to the system .. we're in a thread after all
|
---|
| 149 | sched_yield();
|
---|
[1953] | 150 | tcdrain(reader->handle);
|
---|
[1926] | 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");
|
---|
[1960] | 155 | return ERROR;
|
---|
[1926] | 156 | }
|
---|
[2021] | 157 | // switch SC8in1 to normal mode
|
---|
| 158 | IO_Serial_DTR_Clr(reader);
|
---|
[1926] | 159 | //cs_sleepms(10); //FIXME do I need this?
|
---|
[2291] | 160 | // give some time back to the system .. we're in a thread after all
|
---|
| 161 | sched_yield();
|
---|
[1960] | 162 | return OK;
|
---|
[1926] | 163 | }
|
---|
| 164 |
|
---|
[1960] | 165 | int Sc8in1_Init(struct s_reader * reader)
|
---|
| 166 | {
|
---|
| 167 | //additional init, Phoenix_Init is also called for Sc8in1 !
|
---|
| 168 | struct termios termio;
|
---|
[1969] | 169 | int i,pos, speed,fd = reader->handle;
|
---|
[1966] | 170 | unsigned int is_mcr, sc8in1_clock = 0;
|
---|
[2980] | 171 |
|
---|
[1960] | 172 | tcgetattr(reader->handle,&termio);
|
---|
[2980] | 173 | for (i=0; i<8; i++) {
|
---|
[1960] | 174 | //init all stored termios to default comm settings after device init, before ATR
|
---|
| 175 | memcpy(&stored_termio[i],&termio,sizeof(termio));
|
---|
[2980] | 176 | }
|
---|
| 177 |
|
---|
| 178 | // check for a MCR device and how many slots it has.
|
---|
[1964] | 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;
|
---|
[2980] | 187 |
|
---|
[1964] | 188 | tcflush(reader->handle, TCIOFLUSH); // a non MCR reader might give longer answer
|
---|
[2980] | 189 |
|
---|
[1964] | 190 | for (i=0; i<CS_MAXREADER; i++) //copy handle to other slots, FIXME change this if multiple sc8in1 readers
|
---|
[1966] | 191 | if (reader[i].typ == R_SC8in1) {
|
---|
[1969] | 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 | }
|
---|
[1964] | 199 | reader[i].handle = fd;
|
---|
[1966] | 200 | }
|
---|
[2980] | 201 |
|
---|
[1969] | 202 | if (is_mcr) {
|
---|
[2980] | 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);
|
---|
[1969] | 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 | }
|
---|
[2291] | 230 |
|
---|
[2021] | 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 |
|
---|
[1960] | 235 | return OK;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[1926] | 238 | int 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;
|
---|
[2291] | 246 | // give some time back to the system .. we're in a thread after all
|
---|
| 247 | sched_yield();
|
---|
[1926] | 248 | return(result-1);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | int 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);
|
---|
[1962] | 257 | int i=readsc8in1(reader); //read cardstatus
|
---|
[1926] | 258 | pthread_mutex_unlock(&sc8in1);
|
---|
| 259 | cs_debug("SC8in1: unlocked for Getstatus for slot %i",reader->slot);
|
---|
[2291] | 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 | }
|
---|
[1926] | 265 | cardstatus = i;
|
---|
| 266 | }
|
---|
| 267 | *in = (cardstatus & 1<<(reader->slot-1));
|
---|
[2291] | 268 | // give some time back to the system .. we're in a thread after all
|
---|
| 269 | sched_yield();
|
---|
[1926] | 270 | return OK;
|
---|
| 271 | }
|
---|