/[pcsclite]/trunk/Drivers/ccid/src/ccid_serial.c
ViewVC logotype

Contents of /trunk/Drivers/ccid/src/ccid_serial.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 453 - (show annotations) (download)
Fri Sep 19 12:08:09 2003 UTC (9 years, 8 months ago) by rousseau
File MIME type: text/plain
File size: 12627 byte(s)
complete reimplementation of the Twin serial protocol using a finite
state automata (code much simpler)
1 /*
2 * ccid_serial.c: communicate with a GemPC Twin smart card reader
3 * Copyright (C) 2001-2003 Ludovic Rousseau <ludovic.rousseau@free.fr>
4 *
5 * Thanks to Niki W. Waibel <niki.waibel@gmx.net> for a prototype version
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /*
23 * $Id$
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30 #include <termios.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35
36 #include "pcscdefines.h"
37 #include "config.h"
38 #include "debug.h"
39 #include "ccid.h"
40 #include "utils.h"
41
42 /* communication timeout in seconds */
43 #define SERIAL_TIMEOUT 2
44
45 #define SYNC 0x03
46 #define CTRL_ACK 0x06
47 #define CTRL_NAK 0x15
48 #define RDR_to_PC_NotifySlotChange 0x50
49 #define CARD_ABSENT 0x02
50 #define CARD_PRESENT 0x03
51
52 /*
53 * normal command:
54 * 1 : SYNC
55 * 1 : CTRL
56 * 10 +data length : CCID command
57 * 1 : LRC
58 *
59 * SYNC : 0x03
60 * CTRL : ACK (0x06) or NAK (0x15)
61 * CCID command : see USB CCID specs
62 * LRC : xor of all the previous byes
63 *
64 * Error message:
65 * 1 : SYNC (0x03)
66 * 1 : CTRL (NAK: 0x15)
67 * 1 : LRC (0x16)
68 *
69 * Card insertion/withdrawal
70 * 1 : RDR_to_PC_NotifySlotChange (0x50)
71 * 1 : bmSlotIccState
72 * 0x02 if card absent
73 * 0x03 is card present
74 *
75 * Time request
76 * T=1 : normal CCID command
77 * T=0 : 1 byte (value between 0x80 and 0xFF)
78 *
79 */
80
81 /*
82 * You may get read timeout after a card movement.
83 * This is because you will get the echo of the CCID command
84 * but not the result of the command.
85 *
86 * This is not an applicative issue since the card is either removed (and
87 * powered off) or just inserted (and not yet powered on).
88 */
89
90 /* 271 = max size for short APDU
91 * 2 bytes for header
92 * 1 byte checksum
93 * doubled for echo
94 */
95 #define GEMPCTWIN_MAXBUF (271 +2 +1) * 2
96
97 typedef struct
98 {
99 /*
100 * File handle on the serial port
101 */
102 int fd;
103
104 /*
105 * channel used (1..4)
106 */
107 int channel;
108
109 /*
110 * serial communication buffer
111 */
112 unsigned char buffer[GEMPCTWIN_MAXBUF];
113
114 /*
115 * next available byte
116 */
117 int buffer_offset;
118
119 /*
120 * number of available bytes
121 */
122 int buffer_offset_last;
123
124 /*
125 * CCID infos common to USB and serial
126 */
127 _ccid_descriptor ccid;
128
129 } _serialDevice;
130
131 /* The _serialDevice structure must be defined before including ccid_serial.h */
132 #include "ccid_serial.h"
133
134 static _serialDevice serialDevice[PCSCLITE_MAX_READERS] = {
135 [ 0 ... (PCSCLITE_MAX_READERS-1) ] = { -1, -1 }
136 };
137
138 /*****************************************************************************
139 *
140 * WriteSerial: Send bytes to the card reader
141 *
142 *****************************************************************************/
143 status_t WriteSerial(int lun, int length, unsigned char *buffer)
144 {
145 int i;
146 unsigned char lrc;
147 unsigned char low_level_buffer[GEMPCTWIN_MAXBUF];
148
149 #ifdef DEBUG_LEVEL_COMM
150 char debug_header[] = "-> 121234 ";
151
152 sprintf(debug_header, "-> %06X ", (int)lun);
153 #endif
154
155 if (length > GEMPCTWIN_MAXBUF-3)
156 {
157 DEBUG_CRITICAL3("WriteSerial: command too long: %d for max %d",
158 length, GEMPCTWIN_MAXBUF-3);
159 return STATUS_UNSUCCESSFUL;
160 }
161
162 /* header */
163 low_level_buffer[0] = 0x03; /* SYNC */
164 low_level_buffer[1] = 0x06; /* ACK */
165
166 /* CCID command */
167 memcpy(low_level_buffer+2, buffer, length);
168
169 /* checksum */
170 lrc = 0;
171 for(i=0; i<length+2; i++)
172 lrc ^= low_level_buffer[i];
173 low_level_buffer[length+2] = lrc;
174
175 #ifdef DEBUG_LEVEL_COMM
176 DEBUG_XXD(debug_header, low_level_buffer, length+3);
177 #endif
178
179 if (write(serialDevice[LunToReaderIndex(lun)].fd, low_level_buffer,
180 length+3) != length+3)
181 {
182 DEBUG_CRITICAL2("WriteSerial: write error: %s", strerror(errno));
183 return STATUS_UNSUCCESSFUL;
184 }
185
186 return STATUS_SUCCESS;
187 } /* WriteSerial */
188
189
190 /*****************************************************************************
191 *
192 * ReadSerial: Receive bytes from the card reader
193 *
194 *****************************************************************************/
195 status_t ReadSerial(int lun, int *length, unsigned char *buffer)
196 {
197 unsigned char c;
198 int rv;
199 int echo;
200 int to_read;
201 int i;
202
203 /* we get the echo first */
204 echo = TRUE;
205
206 start:
207 if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
208 return rv;
209
210 if (c == RDR_to_PC_NotifySlotChange)
211 goto slot_change;
212
213 if (c == SYNC)
214 goto sync;
215
216 if (c >= 0x80)
217 goto start;
218
219 DEBUG_CRITICAL2("Got 0x%02X", c);
220 return STATUS_COMM_ERROR;
221
222 slot_change:
223 if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
224 return rv;
225
226 if (c == CARD_ABSENT)
227 DEBUG_COMM("Card removed");
228 else
229 if (c == CARD_PRESENT)
230 DEBUG_COMM("Card inserted");
231 else
232 DEBUG_COMM2("Unknown card movement: %d", buffer[3]);
233 goto start;
234
235 sync:
236 if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
237 return rv;
238
239 if (c == CTRL_ACK)
240 goto ack;
241
242 if (c == CTRL_NAK)
243 goto nak;
244
245 DEBUG_CRITICAL2("Got 0x%02X instead of ACK/NAK", c);
246 return STATUS_COMM_ERROR;
247
248 nak:
249 if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
250 return rv;
251
252 if (c != (SYNC ^ CTRL_NAK))
253 {
254 DEBUG_CRITICAL2("Wrong LRC: 0x%02X", c);
255 return STATUS_COMM_ERROR;
256 }
257 else
258 goto start;
259
260 ack:
261 /* normal CCID frame */
262 if ((rv = get_bytes(lun, buffer, 5)) != STATUS_SUCCESS)
263 return rv;
264
265 /* total frame size */
266 to_read = 10+dw2i(buffer, 1);
267
268 if ((rv = get_bytes(lun, buffer+5, to_read-5)) != STATUS_SUCCESS)
269 return rv;
270
271 /* lrc */
272 if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
273 return rv;
274
275 for (i=0; i<to_read; i++)
276 c ^= buffer[i];
277
278 if (c != (SYNC ^ CTRL_ACK))
279 {
280 DEBUG_CRITICAL2("Wrong LRC: 0x%02X", c);
281 //return STATUS_COMM_ERROR;
282 }
283
284 if (echo)
285 {
286 echo = FALSE;
287 goto start;
288 }
289
290 return STATUS_SUCCESS;
291 } /* ReadSerial */
292
293
294 /*****************************************************************************
295 *
296 * get_bytes: get n bytes
297 *
298 *****************************************************************************/
299 int get_bytes(int lun, unsigned char *buffer, int length)
300 {
301 int offset = serialDevice[LunToReaderIndex(lun)].buffer_offset;
302 int offset_last = serialDevice[LunToReaderIndex(lun)].buffer_offset_last;
303
304 /* enough data are available */
305 if (offset + length <= offset_last)
306 {
307 memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, length);
308 serialDevice[LunToReaderIndex(lun)].buffer_offset += length;
309 }
310 else
311 {
312 int present, rv;
313
314 /* copy available data */
315 present = offset_last - offset;
316
317 if (present > 0)
318 memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, present);
319
320 /* get fresh data */
321 rv = ReadChunk(lun, serialDevice[LunToReaderIndex(lun)].buffer, sizeof(serialDevice[LunToReaderIndex(lun)].buffer), length - present);
322 if (rv < 0)
323 return STATUS_COMM_ERROR;
324
325 /* fill the buffer */
326 memcpy(buffer + present, serialDevice[LunToReaderIndex(lun)].buffer,
327 length - present);
328 serialDevice[LunToReaderIndex(lun)].buffer_offset = length - present;
329 serialDevice[LunToReaderIndex(lun)].buffer_offset_last = rv;
330 }
331
332 DEBUG_XXD("pouet: ", buffer, length);
333
334 return STATUS_SUCCESS;
335 } /* get_bytes */
336
337
338 /*****************************************************************************
339 *
340 * ReadChunk: read a minimum number of bytes
341 *
342 *****************************************************************************/
343 int ReadChunk(int lun, unsigned char *buffer, int buffer_length, int min_length)
344 {
345 int fd = serialDevice[LunToReaderIndex(lun)].fd;
346 fd_set fdset;
347 struct timeval t;
348 int i, rv;
349 #ifdef DEBUG_LEVEL_COMM
350 char debug_header[] = "<- 121234 ";
351
352 sprintf(debug_header, "<- %06X ", (int)lun);
353 #endif
354
355 /* use select() to, eventually, timeout */
356 FD_ZERO(&fdset);
357 FD_SET(fd, &fdset);
358 t.tv_sec = SERIAL_TIMEOUT;
359 t.tv_usec = 0;
360
361 i = select(fd+1, &fdset, NULL, NULL, &t);
362 if (i == -1)
363 {
364 DEBUG_CRITICAL2("select: %s", strerror(errno));
365 return -1;
366 }
367 else
368 if (i == 0)
369 {
370 DEBUG_COMM2("Timeout! (%d sec)", SERIAL_TIMEOUT);
371 return -1;
372 }
373
374 rv = read(fd, buffer, buffer_length);
375 if (rv < 0)
376 {
377 DEBUG_COMM2("read error: %s", strerror(errno));
378 return -1;
379 }
380
381 #ifdef DEBUG_LEVEL_COMM
382 DEBUG_XXD(debug_header, buffer, rv);
383 #endif
384
385 /* too short */
386 if (rv < min_length)
387 {
388 DEBUG_COMM3("ReadSerial: only %d byte(s) read, needed %d", rv,
389 min_length);
390
391 return -1;
392 }
393
394 return rv;
395 } /* ReadChunk */
396
397
398 /*****************************************************************************
399 *
400 * OpenSerial: open the port
401 *
402 *****************************************************************************/
403 status_t OpenSerial(int lun, int channel)
404 {
405 char dev_name[FILENAME_MAX];
406 struct termios current_termios;
407 int i;
408 int reader = LunToReaderIndex(lun);
409
410 DEBUG_COMM3("OpenSerial: Lun: %X, Channel: %d", lun, channel);
411
412 /*
413 * Conversion of old-style ifd-hanler 1.0 CHANNELID
414 */
415 if (channel == 0x0103F8)
416 channel = 1;
417 else
418 if (channel == 0x0102F8)
419 channel = 2;
420 else
421 if (channel == 0x0103E8)
422 channel = 3;
423 else
424 if (channel == 0x0102E8)
425 channel = 4;
426
427 if (channel < 0)
428 {
429 DEBUG_CRITICAL2("wrong port number: %d", (int) channel);
430 return STATUS_UNSUCCESSFUL;
431 }
432
433 sprintf(dev_name, "/dev/pcsc/%d", (int) channel);
434
435 /* check if the same channel is not already used */
436 for (i=0; i<PCSCLITE_MAX_READERS; i++)
437 {
438 if (serialDevice[i].channel == channel)
439 {
440 DEBUG_CRITICAL2("Channel %s already in use", dev_name);
441 return STATUS_UNSUCCESSFUL;
442 }
443 }
444
445 serialDevice[reader].fd = open(dev_name, O_RDWR | O_NOCTTY);
446
447 if (serialDevice[reader].fd <= 0)
448 {
449 DEBUG_CRITICAL3("open %s: %s", dev_name, strerror(errno));
450 return STATUS_UNSUCCESSFUL;
451 }
452
453 /* set channel used */
454 serialDevice[reader].channel = channel;
455
456 /* empty in and out serial buffers */
457 if (tcflush(serialDevice[reader].fd, TCIOFLUSH))
458 DEBUG_INFO2("tcflush() function error: %s", strerror(errno));
459
460 /* get config attributes */
461 if (tcgetattr(serialDevice[reader].fd, &current_termios) == -1)
462 {
463 DEBUG_INFO2("tcgetattr() function error: %s", strerror(errno));
464 close(serialDevice[reader].fd);
465 serialDevice[reader].fd = -1;
466
467 return STATUS_UNSUCCESSFUL;
468 }
469
470 /* IGNBRK: ignore BREAK condition on input
471 * IGNPAR: ignore framing errors and parity errors. */
472 current_termios.c_iflag = IGNBRK | IGNPAR;
473 current_termios.c_oflag = 0; /* Raw output modes */
474 /* CS8: 8-bits character size
475 * CSTOPB: set two stop bits
476 * CREAD: enable receiver
477 * CLOCAL: ignore modem control lines */
478 current_termios.c_cflag = CS8 | CSTOPB | CREAD | CLOCAL;
479
480 /* Do not echo characters because if you connect to a host it or your modem
481 * will echo characters for you. Don't generate signals. */
482 current_termios.c_lflag = 0;
483
484 /* set serial port speed to 115200 bauds */
485 cfsetspeed(&current_termios, B115200);
486
487 DEBUG_INFO("Set serial port baudrate to 115200 and correct configuration");
488 if (tcsetattr(serialDevice[reader].fd, TCSANOW, &current_termios) == -1)
489 {
490 close(serialDevice[reader].fd);
491 serialDevice[reader].fd = -1;
492 DEBUG_INFO2("tcsetattr error: %s", strerror(errno));
493
494 return STATUS_UNSUCCESSFUL;
495 }
496
497 serialDevice[reader].ccid.bSeq = 0;
498 serialDevice[reader].ccid.readerID = GEMPCTWIN;
499 serialDevice[reader].ccid.dwMaxCCIDMessageLength = 271;
500 serialDevice[reader].ccid.dwFeatures = 0x00010230;
501
502 serialDevice[reader].buffer_offset = 0;
503 serialDevice[reader].buffer_offset_last = 0;
504
505 ccid_open_hack(lun);
506
507 return STATUS_SUCCESS;
508 } /* OpenSerial */
509
510
511 /*****************************************************************************
512 *
513 * CloseSerial: close the port
514 *
515 *****************************************************************************/
516 status_t CloseSerial(int lun)
517 {
518 int reader = LunToReaderIndex(lun);
519
520 close(serialDevice[reader].fd);
521
522 serialDevice[reader].fd = -1;
523 serialDevice[reader].channel = -1;
524
525 return STATUS_SUCCESS;
526 } /* CloseSerial */
527
528
529 /*****************************************************************************
530 *
531 * get_ccid_descriptor
532 *
533 ****************************************************************************/
534 _ccid_descriptor *get_ccid_descriptor(int lun)
535 {
536 return &serialDevice[LunToReaderIndex(lun)].ccid;
537 } /* get_ccid_descriptor */
538
539

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.5