| 107 |
/* |
/* |
| 108 |
* device used ("/dev/ttyS?" under Linux) |
* device used ("/dev/ttyS?" under Linux) |
| 109 |
*/ |
*/ |
| 110 |
char *device; |
/*@null@*/ char *device; |
| 111 |
|
|
| 112 |
/* |
/* |
| 113 |
* serial communication buffer |
* serial communication buffer |
| 135 |
#include "ccid_serial.h" |
#include "ccid_serial.h" |
| 136 |
|
|
| 137 |
/* no need to initialize to 0 since it is static */ |
/* no need to initialize to 0 since it is static */ |
| 138 |
static _serialDevice serialDevice[PCSCLITE_MAX_READERS]; |
static _serialDevice serialDevice[CCID_DRIVER_MAX_READERS]; |
| 139 |
|
|
| 140 |
/* unexported functions */ |
/* unexported functions */ |
| 141 |
static int ReadChunk(unsigned int lun, unsigned char *buffer, int buffer_length, |
static int ReadChunk(unsigned int reader_index, unsigned char *buffer, |
| 142 |
int min_length); |
int buffer_length, int min_length); |
| 143 |
|
|
| 144 |
static int get_bytes(unsigned int lun, unsigned char *buffer, int length); |
static int get_bytes(unsigned int reader_index, unsigned char *buffer, |
| 145 |
|
int length); |
| 146 |
|
|
| 147 |
|
|
| 148 |
/***************************************************************************** |
/***************************************************************************** |
| 150 |
* WriteSerial: Send bytes to the card reader |
* WriteSerial: Send bytes to the card reader |
| 151 |
* |
* |
| 152 |
*****************************************************************************/ |
*****************************************************************************/ |
| 153 |
status_t WriteSerial(unsigned int lun, unsigned int length, unsigned char *buffer) |
status_t WriteSerial(unsigned int reader_index, unsigned int length, |
| 154 |
|
unsigned char *buffer) |
| 155 |
{ |
{ |
| 156 |
int i; |
int i; |
| 157 |
unsigned char lrc; |
unsigned char lrc; |
| 160 |
#ifdef DEBUG_LEVEL_COMM |
#ifdef DEBUG_LEVEL_COMM |
| 161 |
char debug_header[] = "-> 123456 "; |
char debug_header[] = "-> 123456 "; |
| 162 |
|
|
| 163 |
sprintf(debug_header, "-> %06X ", lun); |
sprintf(debug_header, "-> %06X ", reader_index); |
| 164 |
#endif |
#endif |
| 165 |
|
|
| 166 |
if (length > GEMPCTWIN_MAXBUF-3) |
if (length > GEMPCTWIN_MAXBUF-3) |
| 187 |
DEBUG_XXD(debug_header, low_level_buffer, length+3); |
DEBUG_XXD(debug_header, low_level_buffer, length+3); |
| 188 |
#endif |
#endif |
| 189 |
|
|
| 190 |
if (write(serialDevice[LunToReaderIndex(lun)].fd, low_level_buffer, |
if (write(serialDevice[reader_index].fd, low_level_buffer, |
| 191 |
length+3) != length+3) |
length+3) != length+3) |
| 192 |
{ |
{ |
| 193 |
DEBUG_CRITICAL2("write error: %s", strerror(errno)); |
DEBUG_CRITICAL2("write error: %s", strerror(errno)); |
| 203 |
* ReadSerial: Receive bytes from the card reader |
* ReadSerial: Receive bytes from the card reader |
| 204 |
* |
* |
| 205 |
*****************************************************************************/ |
*****************************************************************************/ |
| 206 |
status_t ReadSerial(unsigned int lun, /*@unused@*/ unsigned int *length, |
status_t ReadSerial(unsigned int reader_index, |
| 207 |
unsigned char *buffer) |
/*@unused@*/ unsigned int *length, unsigned char *buffer) |
| 208 |
{ |
{ |
| 209 |
unsigned char c; |
unsigned char c; |
| 210 |
int rv; |
int rv; |
| 217 |
|
|
| 218 |
start: |
start: |
| 219 |
DEBUG_COMM("start"); |
DEBUG_COMM("start"); |
| 220 |
if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, &c, 1)) != STATUS_SUCCESS) |
| 221 |
return rv; |
return rv; |
| 222 |
|
|
| 223 |
if (c == RDR_to_PC_NotifySlotChange) |
if (c == RDR_to_PC_NotifySlotChange) |
| 237 |
|
|
| 238 |
slot_change: |
slot_change: |
| 239 |
DEBUG_COMM("slot change"); |
DEBUG_COMM("slot change"); |
| 240 |
if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, &c, 1)) != STATUS_SUCCESS) |
| 241 |
return rv; |
return rv; |
| 242 |
|
|
| 243 |
if (c == CARD_ABSENT) |
if (c == CARD_ABSENT) |
| 257 |
|
|
| 258 |
sync: |
sync: |
| 259 |
DEBUG_COMM("sync"); |
DEBUG_COMM("sync"); |
| 260 |
if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, &c, 1)) != STATUS_SUCCESS) |
| 261 |
return rv; |
return rv; |
| 262 |
|
|
| 263 |
if (c == CTRL_ACK) |
if (c == CTRL_ACK) |
| 271 |
|
|
| 272 |
nak: |
nak: |
| 273 |
DEBUG_COMM("nak"); |
DEBUG_COMM("nak"); |
| 274 |
if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, &c, 1)) != STATUS_SUCCESS) |
| 275 |
return rv; |
return rv; |
| 276 |
|
|
| 277 |
if (c != (SYNC ^ CTRL_NAK)) |
if (c != (SYNC ^ CTRL_NAK)) |
| 285 |
ack: |
ack: |
| 286 |
DEBUG_COMM("ack"); |
DEBUG_COMM("ack"); |
| 287 |
/* normal CCID frame */ |
/* normal CCID frame */ |
| 288 |
if ((rv = get_bytes(lun, buffer, 5)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, buffer, 5)) != STATUS_SUCCESS) |
| 289 |
return rv; |
return rv; |
| 290 |
|
|
| 291 |
/* total frame size */ |
/* total frame size */ |
| 292 |
to_read = 10+dw2i(buffer, 1); |
to_read = 10+dw2i(buffer, 1); |
| 293 |
|
|
| 294 |
DEBUG_COMM2("frame size: %d", to_read); |
DEBUG_COMM2("frame size: %d", to_read); |
| 295 |
if ((rv = get_bytes(lun, buffer+5, to_read-5)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, buffer+5, to_read-5)) != STATUS_SUCCESS) |
| 296 |
return rv; |
return rv; |
| 297 |
|
|
| 298 |
#ifdef DEBUG_LEVEL_COMM |
#ifdef DEBUG_LEVEL_COMM |
| 301 |
|
|
| 302 |
/* lrc */ |
/* lrc */ |
| 303 |
DEBUG_COMM("lrc"); |
DEBUG_COMM("lrc"); |
| 304 |
if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS) |
if ((rv = get_bytes(reader_index, &c, 1)) != STATUS_SUCCESS) |
| 305 |
return rv; |
return rv; |
| 306 |
|
|
| 307 |
DEBUG_COMM2("lrc: 0x%02X", c); |
DEBUG_COMM2("lrc: 0x%02X", c); |
| 326 |
* get_bytes: get n bytes |
* get_bytes: get n bytes |
| 327 |
* |
* |
| 328 |
*****************************************************************************/ |
*****************************************************************************/ |
| 329 |
int get_bytes(unsigned int lun, unsigned char *buffer, int length) |
int get_bytes(unsigned int reader_index, unsigned char *buffer, int length) |
| 330 |
{ |
{ |
| 331 |
int offset = serialDevice[LunToReaderIndex(lun)].buffer_offset; |
int offset = serialDevice[reader_index].buffer_offset; |
| 332 |
int offset_last = serialDevice[LunToReaderIndex(lun)].buffer_offset_last; |
int offset_last = serialDevice[reader_index].buffer_offset_last; |
| 333 |
|
|
| 334 |
DEBUG_COMM3("available: %d, needed: %d", offset_last-offset, |
DEBUG_COMM3("available: %d, needed: %d", offset_last-offset, |
| 335 |
length); |
length); |
| 337 |
if (offset + length <= offset_last) |
if (offset + length <= offset_last) |
| 338 |
{ |
{ |
| 339 |
DEBUG_COMM("data available"); |
DEBUG_COMM("data available"); |
| 340 |
memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, length); |
memcpy(buffer, serialDevice[reader_index].buffer + offset, length); |
| 341 |
serialDevice[LunToReaderIndex(lun)].buffer_offset += length; |
serialDevice[reader_index].buffer_offset += length; |
| 342 |
} |
} |
| 343 |
else |
else |
| 344 |
{ |
{ |
| 350 |
if (present > 0) |
if (present > 0) |
| 351 |
{ |
{ |
| 352 |
DEBUG_COMM2("some data available: %d", present); |
DEBUG_COMM2("some data available: %d", present); |
| 353 |
memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, |
memcpy(buffer, serialDevice[reader_index].buffer + offset, |
| 354 |
present); |
present); |
| 355 |
} |
} |
| 356 |
|
|
| 357 |
/* get fresh data */ |
/* get fresh data */ |
| 358 |
DEBUG_COMM2("get more data: %d", length - present); |
DEBUG_COMM2("get more data: %d", length - present); |
| 359 |
rv = ReadChunk(lun, serialDevice[LunToReaderIndex(lun)].buffer, sizeof(serialDevice[LunToReaderIndex(lun)].buffer), length - present); |
rv = ReadChunk(reader_index, serialDevice[reader_index].buffer, |
| 360 |
|
sizeof(serialDevice[reader_index].buffer), length - present); |
| 361 |
if (rv < 0) |
if (rv < 0) |
| 362 |
return STATUS_COMM_ERROR; |
return STATUS_COMM_ERROR; |
| 363 |
|
|
| 364 |
/* fill the buffer */ |
/* fill the buffer */ |
| 365 |
memcpy(buffer + present, serialDevice[LunToReaderIndex(lun)].buffer, |
memcpy(buffer + present, serialDevice[reader_index].buffer, |
| 366 |
length - present); |
length - present); |
| 367 |
serialDevice[LunToReaderIndex(lun)].buffer_offset = length - present; |
serialDevice[reader_index].buffer_offset = length - present; |
| 368 |
serialDevice[LunToReaderIndex(lun)].buffer_offset_last = rv; |
serialDevice[reader_index].buffer_offset_last = rv; |
| 369 |
DEBUG_COMM3("offset: %d, last_offset: %d", |
DEBUG_COMM3("offset: %d, last_offset: %d", |
| 370 |
serialDevice[LunToReaderIndex(lun)].buffer_offset, |
serialDevice[reader_index].buffer_offset, |
| 371 |
serialDevice[LunToReaderIndex(lun)].buffer_offset_last); |
serialDevice[reader_index].buffer_offset_last); |
| 372 |
} |
} |
| 373 |
|
|
| 374 |
return STATUS_SUCCESS; |
return STATUS_SUCCESS; |
| 380 |
* ReadChunk: read a minimum number of bytes |
* ReadChunk: read a minimum number of bytes |
| 381 |
* |
* |
| 382 |
*****************************************************************************/ |
*****************************************************************************/ |
| 383 |
static int ReadChunk(unsigned int lun, unsigned char *buffer, |
static int ReadChunk(unsigned int reader_index, unsigned char *buffer, |
| 384 |
int buffer_length, int min_length) |
int buffer_length, int min_length) |
| 385 |
{ |
{ |
| 386 |
int fd = serialDevice[LunToReaderIndex(lun)].fd; |
int fd = serialDevice[reader_index].fd; |
| 387 |
# ifndef S_SPLINT_S |
# ifndef S_SPLINT_S |
| 388 |
fd_set fdset; |
fd_set fdset; |
| 389 |
# endif |
# endif |
| 393 |
#ifdef DEBUG_LEVEL_COMM |
#ifdef DEBUG_LEVEL_COMM |
| 394 |
char debug_header[] = "<- 123456 "; |
char debug_header[] = "<- 123456 "; |
| 395 |
|
|
| 396 |
sprintf(debug_header, "<- %06X ", lun); |
sprintf(debug_header, "<- %06X ", reader_index); |
| 397 |
#endif |
#endif |
| 398 |
|
|
| 399 |
already_read = 0; |
already_read = 0; |
| 443 |
* OpenSerial: open the port |
* OpenSerial: open the port |
| 444 |
* |
* |
| 445 |
*****************************************************************************/ |
*****************************************************************************/ |
| 446 |
status_t OpenSerial(unsigned int lun, int channel) |
status_t OpenSerial(unsigned int reader_index, int channel) |
| 447 |
{ |
{ |
| 448 |
char dev_name[FILENAME_MAX]; |
char dev_name[FILENAME_MAX]; |
| 449 |
|
|
| 450 |
DEBUG_COMM3("Lun: %X, Channel: %d", lun, channel); |
DEBUG_COMM3("Reader index: %X, Channel: %d", reader_index, channel); |
| 451 |
|
|
| 452 |
/* |
/* |
| 453 |
* Conversion of old-style ifd-hanler 1.0 CHANNELID |
* Conversion of old-style ifd-hanler 1.0 CHANNELID |
| 472 |
|
|
| 473 |
sprintf(dev_name, "/dev/pcsc/%d", (int) channel); |
sprintf(dev_name, "/dev/pcsc/%d", (int) channel); |
| 474 |
|
|
| 475 |
return OpenSerialByName(lun, dev_name); |
return OpenSerialByName(reader_index, dev_name); |
| 476 |
} /* OpenSerial */ |
} /* OpenSerial */ |
| 477 |
|
|
| 478 |
/***************************************************************************** |
/***************************************************************************** |
| 480 |
* OpenSerialByName: open the port |
* OpenSerialByName: open the port |
| 481 |
* |
* |
| 482 |
*****************************************************************************/ |
*****************************************************************************/ |
| 483 |
status_t OpenSerialByName(unsigned int lun, char *dev_name) |
status_t OpenSerialByName(unsigned int reader_index, char *dev_name) |
| 484 |
{ |
{ |
| 485 |
struct termios current_termios; |
struct termios current_termios; |
| 486 |
int i; |
int i; |
| 487 |
unsigned int reader = LunToReaderIndex(lun); |
unsigned int reader = reader_index; |
| 488 |
|
|
| 489 |
DEBUG_COMM3("Lun: %X, Device: %d", lun, dev_name); |
DEBUG_COMM3("Reader index: %X, Device: %d", reader_index, dev_name); |
| 490 |
|
|
| 491 |
/* check if the same channel is not already used */ |
/* check if the same channel is not already used */ |
| 492 |
for (i=0; i<PCSCLITE_MAX_READERS; i++) |
for (i=0; i<CCID_DRIVER_MAX_READERS; i++) |
| 493 |
{ |
{ |
| 494 |
if (serialDevice[i].device && |
if (serialDevice[i].device && |
| 495 |
strcmp(serialDevice[i].device, dev_name) == 0) |
strcmp(serialDevice[i].device, dev_name) == 0) |
| 559 |
serialDevice[reader].ccid.bPINSupport = 0x0; |
serialDevice[reader].ccid.bPINSupport = 0x0; |
| 560 |
serialDevice[reader].ccid.dwDefaultClock = 4000; |
serialDevice[reader].ccid.dwDefaultClock = 4000; |
| 561 |
serialDevice[reader].ccid.dwMaxDataRate = 344086; |
serialDevice[reader].ccid.dwMaxDataRate = 344086; |
| 562 |
|
serialDevice[reader].ccid.bMaxSlotIndex = 0; |
| 563 |
|
serialDevice[reader].ccid.bCurrentSlotIndex = 0; |
| 564 |
|
|
| 565 |
serialDevice[reader].buffer_offset = 0; |
serialDevice[reader].buffer_offset = 0; |
| 566 |
serialDevice[reader].buffer_offset_last = 0; |
serialDevice[reader].buffer_offset_last = 0; |
| 572 |
unsigned char rx_buffer[50]; |
unsigned char rx_buffer[50]; |
| 573 |
unsigned int rx_length = sizeof(rx_buffer); |
unsigned int rx_length = sizeof(rx_buffer); |
| 574 |
|
|
| 575 |
if (IFD_SUCCESS != CmdEscape(lun, tx_buffer, sizeof(tx_buffer), |
if (IFD_SUCCESS != CmdEscape(reader_index, tx_buffer, sizeof(tx_buffer), |
| 576 |
rx_buffer, &rx_length)) |
rx_buffer, &rx_length)) |
| 577 |
{ |
{ |
| 578 |
DEBUG_CRITICAL("Get firmware failed. Maybe the reader is not co,,ected"); |
DEBUG_CRITICAL("Get firmware failed. Maybe the reader is not co,,ected"); |
| 592 |
* CloseSerial: close the port |
* CloseSerial: close the port |
| 593 |
* |
* |
| 594 |
*****************************************************************************/ |
*****************************************************************************/ |
| 595 |
status_t CloseSerial(unsigned int lun) |
status_t CloseSerial(unsigned int reader_index) |
| 596 |
{ |
{ |
| 597 |
unsigned int reader = LunToReaderIndex(lun); |
unsigned int reader = reader_index; |
| 598 |
|
|
| 599 |
close(serialDevice[reader].fd); |
close(serialDevice[reader].fd); |
| 600 |
serialDevice[reader].fd = -1; |
serialDevice[reader].fd = -1; |
| 611 |
* get_ccid_descriptor |
* get_ccid_descriptor |
| 612 |
* |
* |
| 613 |
****************************************************************************/ |
****************************************************************************/ |
| 614 |
_ccid_descriptor *get_ccid_descriptor(unsigned int lun) |
_ccid_descriptor *get_ccid_descriptor(unsigned int reader_index) |
| 615 |
{ |
{ |
| 616 |
return &serialDevice[LunToReaderIndex(lun)].ccid; |
return &serialDevice[reader_index].ccid; |
| 617 |
} /* get_ccid_descriptor */ |
} /* get_ccid_descriptor */ |
| 618 |
|
|
| 619 |
|
|