source: trunk/src/ifd/ifd-pertosmart1038.c @ 1028

Revision 1028, 30.6 KB checked in by alonbl, 4 years ago (diff)

New build system

Sync with other projects.

merge -r 1014:1027  https://www.opensc-project.org/svn/openct/branches/alonbl/build

_M .
_M macos
M macos/Makefile.am
M configure.ac
_M doc
D doc/changelog.sh
M doc/doxygen.conf.in
D doc/export-wiki.xsl
M doc/Makefile.am
A doc/opensc-logo.gif
D doc/generate-api.sh
D doc/export-wiki.sh
AM doc/nonpersistent
A doc/nonpersistent/export-wiki.xsl
A doc/nonpersistent/Makefile.am
A doc/nonpersistent/export-wiki.sh
A doc/nonpersistent/svn2cl.xsl
D doc/svn2cl.xsl
A doc/doxygen-footer.html
_M src
_M src/ctapi
M src/ctapi/Makefile.am
_M src/tools
M src/tools/Makefile.am
_M src/ifd
M src/ifd/ifd-cm4000.c
M src/ifd/atr.c
M src/ifd/ifd-pertosmart1030.c
M src/ifd/ifd-pertosmart1038.c
M src/ifd/ifd-rutoken.c
M src/ifd/sys-linux.c
M src/ifd/Makefile.am
_M src/include
_M src/include/openct
M src/include/openct/Makefile.am
M src/include/Makefile.am
_M src/ct
M src/ct/Makefile.am
_M src/pcsc
M src/pcsc/Makefile.am
M src/Makefile.am
M Makefile.am
A svnignore
_M etc
M etc/Makefile.am
_M solaris
_M aclocal
D aclocal/pkg.m4
M aclocal/ac_create_stdint_h.m4
D aclocal/ac_compile_check_sizeof.m4
M aclocal/Makefile.am

Line 
1/**
2 * PertoSmart card reader driver (for readers using ACS AC-1038 chipset).
3 *
4 * Copyright 2005, Carlos Henrique Bauer <carlos.bauer@smartcon.com.br>
5 */
6
7#include "internal.h"
8#include <string.h>
9#include <stdlib.h>
10#include <unistd.h>
11
12#include "atr.h"
13
14#ifndef NULL
15#define NULL 0
16#endif
17
18#ifndef FALSE
19#define FALSE 0
20#endif
21
22#ifndef TRUE
23#define TRUE (!FALSE)
24#endif
25
26#ifndef min
27#define min(a, b) (((a) < (b)) ? (a) : (b))
28#endif
29
30#ifndef max
31#define max(a, b) (((a) > (b)) ? (a) : (b))
32#endif
33
34/**
35 * Reader USB Interface info
36 *
37 *   Endpoint    Address      Function              Direction     Packet size
38 * Bulk out        0x02    Command               host -> reader     64 bytes
39 * Bulk in         0x82    Response              host <- reader     64 bytes
40 * Interrupt in    0x81    Card status message   host <- reader      8 bytes
41 */
42
43#define PS_USB_INTERFACE_INDEX              0x00
44#define PS_USB_BULK_OUTPUT_ENDPOINT_ADDRESS 0x02
45#define PS_USB_BULK_INPUT_ENDPOINT_ADDRESS  0x82
46#define PS_USB_INTERRUPT_ENDPOINT_ADDRESS   0x81
47
48typedef enum PS_INSTRUCTIION {
49        PS_GET_ACR_STAT = 0x01,
50        PS_SELECT_CARD_TYPE = 0x02,
51        PS_SET_OPTION = 0x07,
52        PS_RESET = 0x80,
53        PS_EXCHANGE_TPDU_T0 = 0xa0,
54        PS_EXCHANGE_TPDU_T1 = 0xa1,
55        PS_POWER_OFF = 0x81,
56        PS_SET_CARD_PPS = 0x0a,
57        PS_SET_READER_PPS = 0x0b,
58} ps_instruction_t;
59
60static const unsigned char PS_CARD_INSERTED_NOTIFICATION[] = {
61        0x01, 0xc1, 0x00, 0x00
62};
63
64static const unsigned char PS_CARD_REMOVED_NOTIFICATION[] = {
65        0x01, 0xc0, 0x00, 0x00
66};
67
68typedef enum PS_CARD_TYPE {
69        PS_AUTO_T0_OR_T1_CARD_TYPE = 0x00,      /* automatic T=0 or T=1
70                                                   protocol selection for
71                                                   MCU cards                   */
72        PS_T0_CARD_TYPE = 0x0c, /* T=0 protocol for MCU cards  */
73        PS_T1_CARD_TYPE = 0x0d, /* T=1 protocol for MCU cards  */
74        PS_I2C_CARD_TYPE_1K_16K_CARD_TYPE = 0x01,       /* I2C memory card:
75                                                           1k, 2k, 4k, 8k and 16k    */
76        PS_I2C_CARD_TYPE_32K_1024K_CARD_TYPE = 0x02,    /* I2C memory card:
77                                                           32k, 64k, 128k, 256k,
78                                                           512k and 1024k            */
79        PS_AT88SC153_CARD_TYPE = 0x03,  /* ATMEL AT88SC153 secure
80                                           memory card                 */
81        PS_AT88SC1608_CARD_TYPE = 0x04, /* ATMEL AT88SC1608 secure
82                                           memory card                 */
83        PS_3WIRE_CARD_TYPE = 0x05,      /* SLE4418 or SLE4428 card     */
84        PS_2WIRE_CARD_TYPE = 0x06,      /* SLE4432 or SLE4442 cards    */
85} ps_card_type_t;
86
87typedef enum PS_RESET_VOLTAGE_SELECTION {
88        PS_RESET_SELECT_AUTO_V = 0x00,
89        PS_RESET_SELECT_5V = 0x01,
90        PS_RESET_SELECT_3V = 0x02,
91        PS_RESET_SELECT_1V8 = 0x03
92} ps_reset_voltage_selection_t;
93
94typedef enum PS_RESPONSE_STATUS_CODE {
95        PS_SUCCESS = 0x00,
96        PS_SLOTERROR_PROCEDURE_BYTE_CONFLICT = 0xf4,
97        PS_SLOTERROR_BAD_LENGTH = 0xf6,
98        PS_SLOTERROR_BAD_FIDI = 0xf7,
99        PS_SLOTERROR_BAD_ATR_IS = 0xf8,
100        PS_SLOTERROR_ICC_NOT_POWERED_UP = 0xf9,
101        PS_SLOTERROR_ICC_NOT_INSERTED = 0xfa,
102        PS_SLOTERROR_HW_ERROR = 0xfb,
103        PS_SLOTERROR_XFE_OVERRUN = 0xfc,
104        PS_SLOTERROR_XFE_PARITY_ERROR = 0xfd,
105        PS_SLOTERROR_ICC_MUTE = 0xfe,
106        PS_SLOTERROR_CMD_ABORTED = 0xff,
107} ps_response_status_code_t;
108
109typedef enum PS_TRANSMISSION_STATE {
110        IDLE = 0,
111        WAITING_TO_SEND,
112        SENDING,
113        FINISHED,
114        RECEIVING,
115        ERROR
116} ps_transmission_state_t;
117
118typedef enum PS_CARD_STATUS {
119        PS_CARD_UNKNOWN = -1,
120        PS_CARD_NOT_INSERTED = 0,
121        PS_CARD_INSERTED = 1
122} ps_card_status_t;
123
124typedef struct ps_stat {
125        unsigned char internal[10];
126        unsigned char max_c;
127        unsigned char max_r;
128        unsigned short c_type;
129        unsigned char c_sel;
130        unsigned char c_stat;
131} ps_stat_t;
132
133typedef struct ps_device_data {
134        /* current card status */
135        ps_card_status_t card_status;
136        /* state of the serial or usb interface */
137        ps_transmission_state_t if_state;
138        /* current protocol (negotiated by the reader during card powering up). */
139        int cur_icc_proto;
140        long dev_timeout;
141        struct timeval begin;
142        long if_timeout;
143        ifd_usb_capture_t *capture;
144} ps_device_data_t;
145
146#define PS_MAX_SEND_LEN         65535
147
148#define PS_HEADER               0x01
149
150#define PS_HEADER_IDX           0
151#define PS_INSTRUCTION_IDX      1
152#define PS_COMMAND_LENGTH0_IDX  2
153#define PS_COMMAND_LENGTH1_IDX  3
154
155#define PS_STATUS_IDX           1
156#define PS_RESPONSE_LENGTH0_IDX 2
157#define PS_RESPONSE_LENGTH1_IDX 3
158
159#define PS_COMMAND_HEADER_SIZE   4
160#define PS_RESPONSE_HEADER_SIZE  4
161#define PS_RESPONSE_DATA_IDX     PS_RESPONSE_HEADER_SIZE
162
163#define PS_INTERRUPT_URB_DATA_SIZE 0x08
164#define PS_ENDPOINT                0x81
165
166#define PS_BULK_SEND_PACKET_SIZE    64
167#define PS_BULK_RECEIVE_PACKET_SIZE 64
168
169#define PS_OPTION_EMV_MODE_ON    (1 << 4)       /* done */
170#define PS_OPTION_MEMORY_CARD_ON (1 << 5)       /* done */
171
172#define PS_DEFAULT_T1_IFSC 0x20
173#define PS_MAX_T1_IFSD     0xfe
174
175/* read timeout
176 * we must wait enough so that the card can finish its calculation */
177static const long PS_BULK_TIMEOUT = 30000;
178static const long PS_INTERRUPT_TIMEOUT = 100;
179
180/* reader names */
181static const char PS_USB_READER_NAME[] = "PertoSmart EMV (AC1038, USB)";
182
183typedef int complete_fn_t(const void *, size_t);
184
185typedef struct PS_RESPONSE_STATUS_CODE_MAP_ENTRY {
186        ps_response_status_code_t status_code;
187        char *status_str;
188} ps_response_status_code_map_entry_t;
189
190/*
191 * Convert reader status byte to a string.
192 */
193static const char *ps_get_status_string(ps_response_status_code_t status_code)
194{
195        static const ps_response_status_code_map_entry_t
196            ps_response_status_code_map[] = {
197                {PS_SUCCESS, "Success"},
198                {PS_SLOTERROR_PROCEDURE_BYTE_CONFLICT,
199                 "Procedure Byte Conflict"},
200                {PS_SLOTERROR_BAD_LENGTH, "Bad Length"},
201                {PS_SLOTERROR_BAD_FIDI, "Bad Fidi"},
202                {PS_SLOTERROR_BAD_ATR_IS, "Bad Atr IS"},
203                {PS_SLOTERROR_ICC_NOT_POWERED_UP, "Icc Not Powered Up"},
204                {PS_SLOTERROR_ICC_NOT_INSERTED, "Icc Not Inserted"},
205                {PS_SLOTERROR_HW_ERROR, "Hardware Error"},
206                {PS_SLOTERROR_XFE_OVERRUN, "Transfer Overrun"},
207                {PS_SLOTERROR_XFE_PARITY_ERROR, "Transfer Parity Error"},
208                {PS_SLOTERROR_ICC_MUTE, "ICC mute"},
209                {PS_SLOTERROR_CMD_ABORTED, "Command Aborted"}
210        };
211
212        int i;
213
214        for (i = 0; i < sizeof(ps_response_status_code_map) /
215             sizeof(ps_response_status_code_map[0]); i++) {
216
217                if (ps_response_status_code_map[i].status_code == status_code) {
218                        return ps_response_status_code_map[i].status_str;
219                }
220        }
221
222        return "Unknown response status code";
223}
224
225/*
226 * Switch the driver to transmission state.
227 */
228static int ps_if_transmission_start(ifd_device_t * dev, long timeout)
229{
230        ps_device_data_t *device_data;
231
232        if (ct_config.debug >= 1)
233                ct_debug("ps_if_transmission_start: called");
234
235        device_data = (ps_device_data_t *) dev->user_data;
236
237        if (device_data->if_state != IDLE && device_data->if_state != ERROR) {
238                ct_error("ps_if_transmission_start: can't start "
239                         "transmission: device not idle");
240                return IFD_ERROR_LOCKED;
241        }
242
243        device_data->if_state = WAITING_TO_SEND;
244        device_data->if_timeout = (timeout < 0) ? dev->timeout : timeout;
245
246        return IFD_SUCCESS;
247}
248
249/*
250 * Send data to reader.
251 */
252static int
253ps_if_transmission_send(ifd_device_t * dev,
254                        const unsigned char *sbuf, size_t slen)
255{
256        int rc;
257        ps_device_data_t *device_data;
258
259        if (ct_config.debug >= 1)
260                ct_debug("ps_if_transmission_send: called");
261        if (ct_config.debug >= 4)
262                ct_debug("ps_if_transmission_send: sending %u bytes: %s",
263                         slen, ct_hexdump(sbuf, slen));
264
265        device_data = (ps_device_data_t *) dev->user_data;
266
267        if (device_data->if_state  != WAITING_TO_SEND &&
268            device_data->if_state != SENDING) {
269                ct_error("ps_if_transmission_send: "
270                         "invalid transmission state %i.",
271                         device_data->if_state);
272                rc = IFD_ERROR_GENERIC;
273                goto out;
274        }
275
276        if (device_data->if_state == WAITING_TO_SEND) {
277                gettimeofday(&(device_data->begin), NULL);
278                ifd_device_flush(dev);
279                device_data->if_state = SENDING;
280        }
281
282        /* complete packet */
283        rc = ifd_device_send(dev, sbuf, slen);
284        if (slen != rc) {
285                if (rc >= IFD_SUCCESS) {
286                        /* if_device_send didn't reported an error,
287                           but didn't transmitted a full packet to the reader */
288                        if (ct_config.debug >= 1)
289                                ct_debug("ps_if_transmission_send: unexpected "
290                                         "result from ifd_device_send: %i", rc);
291                        rc = IFD_ERROR_COMM_ERROR;
292                }
293                goto out;
294        }
295
296      out:
297
298        if (rc < 0) {
299                device_data->if_state = ERROR;
300                ct_error("ps_if_transmission_send: failed: %i", rc);
301        }
302
303        return rc;
304}
305
306/*
307 * Receive data from reader.
308 */
309static int
310ps_if_transmission_receive(ifd_device_t * dev, unsigned char *rbuf, size_t rlen)
311{
312
313        int rc = IFD_SUCCESS;
314        ps_device_data_t *device_data = NULL;
315        unsigned char *chunk_start = NULL;
316        size_t chunk_len = 0;
317        size_t rbuf_offset = 0;
318
319        if (ct_config.debug >= 1)
320                ct_debug("ps_if_transmission_receive: called");
321
322        device_data = (ps_device_data_t *) dev->user_data;
323
324        if (device_data->if_state != SENDING
325            && device_data->if_state != RECEIVING) {
326                ct_error("ps_if_transmission_receive: "
327                         "invalid transmission state %i.",
328                         device_data->if_state);
329                rc = IFD_ERROR_GENERIC;
330                goto out;
331        }
332
333        device_data->if_state = RECEIVING;
334
335        for (;;) {
336                chunk_len = min(PS_BULK_RECEIVE_PACKET_SIZE,
337                                rlen - rbuf_offset);
338
339                if (chunk_len > 0) {
340                        long timeout;
341                        chunk_start = &rbuf[rbuf_offset];
342
343                        timeout = device_data->if_timeout -
344                            ifd_time_elapsed(&(device_data->begin));
345
346                        rc = ifd_device_recv(dev, chunk_start, chunk_len,
347                                             timeout);
348
349                        if (rc < IFD_SUCCESS) {
350                                if(ct_config.debug >= 1)
351                                        ct_debug("ps_if_transmission_receive: error: %i",
352                                                 rc);
353                                goto out;
354                        }
355
356                        rbuf_offset += rc;
357                }
358
359                if (rc < chunk_len || rbuf_offset >= rlen) {
360                        break;
361                }
362        }
363
364        /* return the length of received data */
365        rc = rbuf_offset;
366
367      out:
368        if (rc < 0) {
369                device_data->if_state = ERROR;
370                ct_error("ps_if_transmission_receive: failed: %i", rc);
371        } else {
372                if (ct_config.debug >= 4)
373                        ct_debug("ps_if_transmission_receive: received %u bytes:%s",
374                                 rc, ct_hexdump(rbuf, rc));
375        }
376
377        return rc;
378}
379
380/*
381 * Receive all data reader want to send us. Reader will
382 * switch to receive state after sending the last packet.
383 */
384static int ps_if_transmission_flush_reader_output_buffer(ifd_device_t * dev)
385{
386        unsigned char buffer[PS_BULK_RECEIVE_PACKET_SIZE];
387
388        int rc = IFD_SUCCESS;
389        ps_device_data_t *device_data = NULL;
390        const size_t buffer_len = sizeof(buffer);
391        const long timeout = 100;
392
393        if (ct_config.debug >= 1)
394                ct_debug("ps_if_transmission_flush_reader_output_buffer: called");
395
396        device_data = (ps_device_data_t *) dev->user_data;
397
398        do {
399                rc = ifd_device_recv(dev, buffer, buffer_len, timeout);
400        } while (rc > 0);
401
402        /* clear possible sensitive information */
403        memset(buffer, 0, buffer_len);
404
405        return IFD_SUCCESS;
406}
407
408/*
409 * Switch driver state to non transmission state.
410 */
411static int ps_if_transmission_end(ifd_device_t * dev)
412{
413        ps_device_data_t *device_data;
414
415        if (ct_config.debug >= 1)
416                ct_debug("ps_if_transmission_end: called");
417
418        device_data = (ps_device_data_t *) dev->user_data;
419
420        if (device_data->if_state != IDLE) {
421                device_data->if_state = IDLE;
422        }
423
424        return IFD_SUCCESS;
425}
426
427/*
428 * Send command to reader.
429 */
430static int
431ps_send_to_ifd(ifd_reader_t * reader,
432               ps_instruction_t instruction,
433               const unsigned char *sbuf, size_t slen)
434{
435        unsigned char good_buffer[5 * PS_BULK_SEND_PACKET_SIZE];
436        unsigned char *buffer = good_buffer;
437
438        size_t buffer_len = sizeof(good_buffer);
439
440        int rc = IFD_SUCCESS;
441        ifd_device_t *dev = NULL;
442        ps_device_data_t *device_data = NULL;
443
444        size_t command_size = PS_COMMAND_HEADER_SIZE + slen;
445
446        if (ct_config.debug >= 1)
447                ct_debug("ps_send_to_ifd: called");
448        if (ct_config.debug >= 3)
449                ct_debug("ps_send_to_ifd: sending %u bytes:%s", rc,
450                         ct_hexdump(sbuf, slen));
451       
452        /* needs padding? */
453        if (command_size % PS_BULK_SEND_PACKET_SIZE) {
454                /* calculate padding */
455                command_size = PS_BULK_SEND_PACKET_SIZE *
456                    (1 + (command_size / PS_BULK_SEND_PACKET_SIZE));
457        }
458
459        dev = reader->device;
460        device_data = (ps_device_data_t *) dev->user_data;
461
462        if (PS_MAX_SEND_LEN < slen) {
463                ct_error("ps_apdu_send: transmission is "
464                         "larger than maximum allowed: %i",
465                         slen);
466                goto out;
467        }
468
469        if (buffer_len < command_size) {
470                buffer = (unsigned char *)malloc(command_size);
471
472                if (buffer == NULL) {
473                        rc = IFD_ERROR_NO_MEMORY;
474                        goto out;
475                }
476
477                buffer_len = command_size;
478        }
479
480        /* build command */
481
482        /* header */
483        buffer[PS_HEADER_IDX] = PS_HEADER;
484        buffer[PS_INSTRUCTION_IDX] = instruction;
485        buffer[PS_COMMAND_LENGTH0_IDX] = (unsigned char)((slen >> 8) & 0xff);
486        buffer[PS_COMMAND_LENGTH1_IDX] = (unsigned char)(slen & 0xff);
487
488        /* data */
489        memcpy(&buffer[PS_COMMAND_HEADER_SIZE], sbuf, slen);
490
491        /* padding */
492        if (buffer_len > slen) {
493                const size_t pad_start = slen + PS_COMMAND_HEADER_SIZE;
494                memset(&buffer[pad_start], 0, buffer_len - pad_start);
495        }
496
497        /* send the first packet to reader */
498        rc = ps_if_transmission_send(dev, buffer, command_size);
499
500        if (command_size != rc) {
501                /* something is wrong */
502                if (rc >= 0) {
503                        /* didn't sent all the data to the reader */
504                        rc = IFD_ERROR_COMM_ERROR;
505                }
506
507                goto out;
508        }
509
510        /* return the length of data sent to reader */
511        rc = slen;
512
513      out:
514        if (buffer != NULL) {
515
516                /* clear possible sensitive information */
517                memset(buffer, 0, buffer_len);
518        }
519
520        if (buffer != good_buffer) {
521                free(buffer);
522        }
523
524        if (rc < 0 ) {
525                device_data->if_state = ERROR;
526                ct_error("ps_send_to_ifd: failed: %i", rc);
527        } else {
528                if (ct_config.debug >= 4)
529                        ct_debug("ps_send_to_ifd: sent %u bytes:%s",
530                                 slen, ct_hexdump(sbuf, slen));
531        }
532
533        return rc;
534}
535
536/*
537 * Receive a response from reader
538 *
539 * (rbuf == NULL && rlen == 0) means caller wants no data,
540 * just the reader status
541 */
542static int
543ps_receive_from_ifd(ifd_reader_t * reader, unsigned char *rbuf, size_t rlen)
544{
545
546        int rc = IFD_SUCCESS;
547        ifd_device_t *dev = NULL;
548        ps_device_data_t *device_data = NULL;
549        ps_response_status_code_t status = PS_SUCCESS;
550        size_t data_len = 0;
551        size_t received = 0;
552
553        unsigned char buffer[PS_BULK_RECEIVE_PACKET_SIZE];
554
555        const size_t buffer_len = sizeof(buffer);
556
557        if (ct_config.debug >= 1)
558                ct_debug("ps_receive_from_ifd: called");
559
560        dev = reader->device;
561        device_data = (ps_device_data_t *) dev->user_data;
562
563        if (rbuf == NULL && rlen > 0) {
564                ct_error("ps_receive_from_ifd: rbuf == NULL");
565                rc = IFD_ERROR_GENERIC;
566                goto out;
567        }
568
569        /* receive the response header */
570        rc = ps_if_transmission_receive(dev, buffer, buffer_len);
571
572        if (rc < 0 )
573                goto out;
574
575        if (rc < PS_RESPONSE_HEADER_SIZE) {
576                /* response header too small to contain a valid response header */
577                rc = IFD_ERROR_COMM_ERROR;
578                goto out;
579        }
580
581        /* interpret the response header */
582
583        if (PS_HEADER != buffer[PS_HEADER_IDX]) {
584                /* receive error */
585                rc = IFD_ERROR_COMM_ERROR;
586                goto out;
587        }
588
589        /* decode status word */
590
591        status = buffer[PS_STATUS_IDX];
592
593        if (ct_config.debug >= 1)
594                ct_debug("ps_receive_from_ifd: status = %#02x, %s\n",
595                         status, ps_get_status_string(status));
596
597        switch (status) {
598
599        case PS_SUCCESS:
600                break;
601
602        case PS_SLOTERROR_ICC_NOT_INSERTED:
603                rc = IFD_ERROR_NO_CARD;
604                goto out;
605
606        case PS_SLOTERROR_XFE_OVERRUN:
607        case PS_SLOTERROR_XFE_PARITY_ERROR:
608                rc = IFD_ERROR_COMM_ERROR;
609                goto out;
610
611        default:
612                rc = IFD_ERROR_GENERIC;
613                goto out;
614        }
615
616        /* decode data length */
617        data_len = (buffer[PS_RESPONSE_LENGTH0_IDX] << 8) |
618            buffer[PS_RESPONSE_LENGTH1_IDX];
619
620        if (data_len > rlen) {
621                rc = IFD_ERROR_BUFFER_TOO_SMALL;
622                goto out;
623        }
624
625        if (data_len > 0) {
626                size_t remaining;
627                /* copy data from first packet */
628                received = rc - PS_RESPONSE_DATA_IDX;
629
630                if (received > 0) {
631                        memcpy(rbuf, &buffer[PS_RESPONSE_DATA_IDX], received);
632                }
633
634                /* receive the remaining data */
635                remaining = data_len - received;
636
637                rc = ps_if_transmission_receive(dev,
638                                                &rbuf[received], remaining);
639                if (rc < 0)
640                        goto out;
641
642                received += rc;
643
644                if (received != data_len) {
645                        rc = IFD_ERROR_COMM_ERROR;
646                        goto out;
647                }
648        }
649
650        /* return the length of received data */
651        rc = received;
652
653      out:
654
655        memset(buffer, 0, buffer_len);
656
657        if (rc < 0) {
658                ps_if_transmission_flush_reader_output_buffer(dev);
659                device_data->if_state = ERROR;
660                ct_error("ps_receive_from_ifd: failed: %i", rc);
661        } else {
662                if (ct_config.debug >= 3)
663                        ct_debug("ps_if_transmission_receive: "
664                                 "received %u bytes:%s", rc,
665                                 ct_hexdump(rbuf, rc));
666        }
667
668        return rc;
669}
670
671/*
672 * Send an command and receive the response.
673 */
674static int
675ps_transceive_instruction(ifd_reader_t * reader,
676                          ps_instruction_t instruction,
677                          const unsigned char *sbuf,
678                          size_t slen, unsigned char *rbuf, size_t rlen)
679{
680        int rc = IFD_SUCCESS;
681        ifd_device_t *dev = NULL;
682       
683        if (ct_config.debug >= 1)
684                ct_debug("ps_transceive_instruction: called");
685
686        dev = reader->device;
687
688        if (rc == IFD_SUCCESS) {
689                /* start the transmission */
690                rc = ps_if_transmission_start(dev, dev->timeout);
691
692                if (IFD_SUCCESS == rc) {
693                        /* send the data */
694                        rc = ps_send_to_ifd(reader, instruction, sbuf, slen);
695
696                        if (rc >= 0) {
697                                /* receive the data from the reader */
698                                rc = ps_receive_from_ifd(reader, rbuf, rlen);
699                        }
700
701                        ps_if_transmission_end(dev);
702                }
703        }
704
705        if (rc < 0) 
706                ct_error("ps_transceive_instruction: failed: %i", rc);
707
708        return rc;
709}
710
711/*
712 * Activate the reader.
713 */
714static int ps_activate(ifd_reader_t * reader)
715{
716        if (ct_config.debug >= 1)
717                ct_debug("ps_activate: called");
718        return IFD_SUCCESS;
719}
720
721/*
722 * Deactivate the reader.
723 */
724static int ps_deactivate(ifd_reader_t * reader)
725{
726        int rc;
727
728        if (ct_config.debug >= 1)
729                ct_debug("ps_deactivate: called");
730
731        rc = ps_transceive_instruction(reader, PS_POWER_OFF, NULL, 0, NULL, 0);
732
733        if (rc < 0) {
734                ct_error("ps_deactivate: failed: %i", rc);
735        }
736
737        return rc;
738}
739
740/*
741 * Get the current reader status.
742 */
743static int ps_get_stat(ifd_reader_t * reader, ps_stat_t * stat)
744{
745        int rc;
746        unsigned char buffer[16];
747        unsigned char *p;
748
749        if (ct_config.debug >= 1)
750                ct_debug("ps_get_stat: called");
751
752        rc = ps_transceive_instruction(reader, PS_GET_ACR_STAT,
753                                       NULL, 0, buffer, sizeof(buffer));
754
755        if (rc < 0 ) 
756                goto failed;
757
758        if (rc < sizeof(buffer)) {
759                rc = IFD_ERROR_COMM_ERROR;
760                goto failed;
761        }
762
763        for (p = buffer; p < (buffer + sizeof(stat->internal)); p++) {
764                stat->internal[p - buffer] = *p;
765        }
766
767        stat->max_c = *p++;
768        stat->max_r = *p++;
769        stat->c_type = *p++ << 8;
770        stat->c_type |= *p++;
771        stat->c_sel = *p++;
772        stat->c_stat = *p++;
773
774        return IFD_SUCCESS;
775
776      failed:
777        ct_error("ps_get_stat: failed: %i", rc);
778        return rc;
779}
780
781/*
782 * Get the current card status.
783 */
784static int ps_card_status(ifd_reader_t * reader, int slot, int *status)
785{
786        int rc;
787        ifd_device_t *dev;
788        ps_device_data_t *device_data;
789        ps_card_status_t card_status;
790        int status_tmp = 0;
791
792        if (ct_config.debug >= 1)
793                ct_debug("ps_card_status: called");
794
795        if (slot != 0) {
796                ct_error("ps_card_status: bad slot index %u", slot);
797                return IFD_ERROR_INVALID_SLOT;
798        }
799
800        dev = reader->device;
801
802        device_data = (ps_device_data_t *) dev->user_data;
803        card_status = device_data->card_status;
804
805        if (card_status == PS_CARD_UNKNOWN) {
806                ps_stat_t stat;
807
808                /* ask the current status to the reader */
809                memset(&stat, 0, sizeof(stat));
810                rc = ps_get_stat(reader, &stat);
811
812                if (rc >= 0) {
813                        if (stat.c_stat) {
814                                device_data->card_status = PS_CARD_INSERTED;
815                                status_tmp = IFD_CARD_STATUS_CHANGED;
816                        } else {
817                                device_data->card_status = PS_CARD_NOT_INSERTED;
818                        }
819                }
820        } else {
821                unsigned char packet_buf[PS_INTERRUPT_URB_DATA_SIZE];
822                const size_t packet_buf_len = sizeof(packet_buf);
823
824                /* read notifications received from the reader */
825
826                for (;;) {
827                        int inserted = FALSE;
828                       
829                        rc = ifd_usb_capture(dev,
830                                             device_data->capture,
831                                             packet_buf,
832                                             packet_buf_len,
833                                             PS_INTERRUPT_TIMEOUT);
834
835                        if (IFD_ERROR_TIMEOUT == rc) {
836                                rc = IFD_SUCCESS;
837                                break;
838                        }
839
840                        if (rc < 0) 
841                                break;
842
843                        if (0 == memcmp(packet_buf,
844                                        PS_CARD_INSERTED_NOTIFICATION,
845                                        sizeof(PS_CARD_INSERTED_NOTIFICATION)))
846                        {
847                                inserted = TRUE;
848                        } else if (0 != memcmp(packet_buf,
849                                               PS_CARD_REMOVED_NOTIFICATION,
850                                               sizeof
851                                               (PS_CARD_REMOVED_NOTIFICATION)))
852                        {
853                                continue;
854                        }
855
856                        if (inserted) {
857                                if (device_data->card_status != PS_CARD_INSERTED) {
858                                        device_data->card_status = PS_CARD_INSERTED;
859                                        status_tmp = IFD_CARD_STATUS_CHANGED;
860                                }
861                        } else {
862                                if (device_data->card_status != PS_CARD_NOT_INSERTED) {
863                                        device_data->card_status = PS_CARD_NOT_INSERTED;
864                                        status_tmp = IFD_CARD_STATUS_CHANGED;
865                                }
866                        }
867                }
868        }
869
870        if (rc < 0) {
871                if (ct_config.debug >= 1)
872                        ct_debug("ps_card_status: failed: %i", rc);
873        } else {
874                if (device_data->card_status == PS_CARD_INSERTED) {
875                        status_tmp |= IFD_CARD_PRESENT;
876                }
877                *status = status_tmp;
878        }
879
880        return rc;
881}
882
883/*
884 * Reset card and select the protocol
885 */
886static int
887ps_card_reset_select_protocol(ifd_reader_t * reader, int nslot,
888                              void *atr, size_t size, int new_icc_proto)
889{
890        int rc;
891        int atr_len;
892        unsigned char sbuf[1];
893        ifd_device_t *dev;
894        ps_device_data_t *device_data;
895        ifd_slot_t *slot;
896        ifd_atr_info_t atr_info;
897
898        if (ct_config.debug >= 1)
899                ct_debug("ps_card_reset_select_protocol: called");
900
901        if (nslot != 0) {
902                ct_error("ps_card_reset_select_protocol: bad slot index %u",
903                         nslot);
904                return IFD_ERROR_INVALID_SLOT;
905        }
906
907        dev = reader->device;
908        slot = &reader->slot[nslot];
909        device_data = (ps_device_data_t *) dev->user_data;
910
911        /* protocol changed? */
912        if (NULL == slot->proto || device_data->cur_icc_proto != new_icc_proto) {
913
914                switch (new_icc_proto) {
915
916                case IFD_PROTOCOL_DEFAULT:
917                        if (ct_config.debug >= 1)
918                                ct_debug("ps_card_reset_select_protocol: "
919                                         "using automatic protocol selection");
920                        sbuf[0] = PS_AUTO_T0_OR_T1_CARD_TYPE;
921                        break;
922
923                case IFD_PROTOCOL_T0:
924                        if (ct_config.debug >= 1)
925                                ct_debug("ps_card_reset_select_protocol: "
926                                         "selecting protocol T0");
927                        sbuf[0] = PS_T0_CARD_TYPE;
928                        break;
929
930                case IFD_PROTOCOL_T1:
931                        if (ct_config.debug >= 1)
932                                ct_debug("ps_card_reset_select_protocol: "
933                                         "selecting protocol T1");
934                        sbuf[0] = PS_T1_CARD_TYPE;
935                        break;
936
937                default:
938                        ct_error("ps_card_reset_select_protocol: unknow or "
939                                 "unsupported protocol %i", new_icc_proto);
940                        return IFD_ERROR_NOT_SUPPORTED;
941                }
942
943                /* power off the card, the reader does PPS negotiation
944                   after the next power up */
945                rc = ps_transceive_instruction(reader, PS_POWER_OFF, NULL, 0,
946                                               NULL, 0);
947
948                if (IFD_SUCCESS != rc) {
949                        ct_error("ps_card_reset_select_protocol: "
950                                 "failed (PS_POWER_OF): %i", rc);
951                        return rc;
952                }
953
954                rc = ps_transceive_instruction(reader, PS_SELECT_CARD_TYPE,
955                                               sbuf, sizeof(sbuf), NULL, 0);
956
957                if (IFD_SUCCESS != rc) {
958                        ct_error("ps_card_reset_select_protocol: "
959                                 "error selecting card type %#02x", sbuf[0]);
960                        return rc;
961                }
962        }
963
964        /* power up the card */
965        rc = ps_transceive_instruction(reader, PS_RESET, NULL, 0, atr, size);
966        if (rc < 0) {
967                ct_error("ps_card_reset_select_protocol: failed (PS_RESET): %i",
968                         rc);
969                return rc;
970        }
971
972        atr_len = rc;
973
974        {
975                int status;
976
977                ps_card_status(reader, nslot, &status);
978        }
979
980        /* the reader did PPS negotiation with the card
981           parse the atr to check the protocol negotiated by the reader */
982
983        rc = ifd_atr_parse(&atr_info, atr, atr_len);
984
985        if (rc < 0) {
986                ct_error("ps_card_reset_select_protocol: %s: Bad ATR",
987                         reader->name);
988                return rc;
989        }
990
991        if (-1 != atr_info.TA[1]) {
992                /* specific mode */
993                if (ct_config.debug >= 1)
994                        ct_debug("ps_card_reset_select_protocol: "
995                                 "card in specific mode %#02x",
996                                 atr_info.TA[1] & 0x0f);
997                new_icc_proto = atr_info.TA[1] & 0x0f;
998        } else if (IFD_PROTOCOL_DEFAULT == new_icc_proto) {
999                new_icc_proto = atr_info.default_protocol;
1000        } else if (!(atr_info.supported_protocols & (1 << new_icc_proto))) {
1001                ct_error("Protocol not supported by card (according to ATR)");
1002                return IFD_ERROR_NOT_SUPPORTED;
1003        }
1004
1005        if (NULL == slot->proto || device_data->cur_icc_proto != new_icc_proto) {
1006
1007                if (NULL != slot->proto) {
1008                        ifd_protocol_free(slot->proto);
1009                }
1010
1011                slot->proto =
1012                    ifd_protocol_new(new_icc_proto, reader, slot->dad);
1013
1014                if (slot->proto == NULL) {
1015                        ct_error("ps_cart_reset_select_protocol: "
1016                                 "ifd_protocol_new");
1017                        return IFD_ERROR_GENERIC;
1018                }
1019
1020                /* set protocol parameters */
1021
1022                switch (new_icc_proto) {
1023
1024                case IFD_PROTOCOL_T0:
1025                        if (ct_config.debug >= 1)
1026                                ct_debug("ps_card_reset_select_protocol: "
1027                                         "using protocol T0");
1028                        ifd_protocol_set_parameter(slot->proto,
1029                                                   IFD_PROTOCOL_BLOCK_ORIENTED,
1030                                                   1);
1031                        break;
1032
1033                case IFD_PROTOCOL_T1:
1034                        if (ct_config.debug >= 1)
1035                                ct_debug("ps_card_reset_select_protocol: "
1036                                         "using protocol T1");
1037                        ifd_protocol_set_parameter(slot->proto,
1038                                                   IFD_PROTOCOL_BLOCK_ORIENTED,
1039                                                   1);
1040                        ifd_protocol_set_parameter(slot->proto,
1041                                                   IFD_PROTOCOL_T1_IFSC,
1042                                                   (atr_info.TA[2] !=
1043                                                    -1) ? atr_info.
1044                                                   TA[2] : PS_DEFAULT_T1_IFSC);
1045                        ifd_protocol_set_parameter(slot->proto,
1046                                                   IFD_PROTOCOL_T1_IFSD,
1047                                                   PS_MAX_T1_IFSD);
1048                        break;
1049
1050                default:
1051                        ct_error("ps_card_reset_select_protocol: "
1052                                 "protocol not supported %#02x",
1053                                 atr_info.default_protocol);
1054                        return IFD_ERROR_NOT_SUPPORTED;
1055                }
1056
1057                /* save protocol info */
1058                device_data->cur_icc_proto = new_icc_proto;
1059        }
1060
1061        return atr_len;
1062}
1063
1064/*
1065 * Reset card
1066 */
1067static int
1068ps_card_reset(ifd_reader_t * reader, int slot, void *atr, size_t size)
1069{
1070        ifd_device_t *dev;
1071        ps_device_data_t *device_data;
1072
1073        if (ct_config.debug >= 1)
1074                ct_debug("ps_card_reset: called");
1075
1076        dev = reader->device;
1077        device_data = (ps_device_data_t *) dev->user_data;
1078
1079        return ps_card_reset_select_protocol(reader, slot,
1080                                             atr, size,
1081                                             device_data->cur_icc_proto);
1082}
1083
1084/*
1085 * Select a protocol for communication with the ICC.
1086 */
1087static int ps_set_protocol(ifd_reader_t * reader, int nslot, int proto)
1088{
1089        int rc;
1090        ifd_device_t *dev;
1091        ps_device_data_t *device_data;
1092        ifd_slot_t *slot;
1093
1094        if (ct_config.debug >= 1)
1095                ct_debug("ps_set_protocol: called");
1096
1097        dev = reader->device;
1098        device_data = (ps_device_data_t *) dev->user_data;
1099        slot = &reader->slot[nslot];
1100
1101        if (NULL == slot->proto || device_data->cur_icc_proto != proto) {
1102
1103                /* the reader negotiates the protocol during the card power up
1104                   must power down and reset the card to change it */
1105                rc = ps_card_reset_select_protocol(reader, nslot,
1106                                                   slot->atr,
1107                                                   sizeof(slot->atr), proto);
1108
1109                if (rc >= IFD_SUCCESS) {
1110                        slot->atr_len = rc;
1111                        rc = IFD_SUCCESS;
1112                } else {
1113                        memset(slot->atr, 0, sizeof(slot->atr));
1114                        slot->atr_len = 0;
1115                }
1116
1117        } else {
1118                rc = IFD_SUCCESS;
1119        }
1120
1121        return rc;
1122}
1123
1124/*
1125 * Send an apdu to reader.
1126 */
1127static int
1128ps_apdu_send(ifd_reader_t * reader, unsigned int dad,
1129             const unsigned char *sbuf, size_t slen)
1130{
1131        int rc;
1132        ifd_device_t *dev;
1133        ps_device_data_t *device_data;
1134        ps_instruction_t instruction;
1135
1136        if (ct_config.debug >= 1) 
1137                ct_debug("ps_apdu_send: called");
1138        if (ct_config.debug >= 3) 
1139                ct_debug("ps_apdu_send: sending %i: %s",
1140                         slen, ct_hexdump(sbuf, slen));
1141
1142        dev = reader->device;
1143        device_data = (ps_device_data_t *) dev->user_data;
1144
1145        switch (device_data->cur_icc_proto) {
1146
1147        case IFD_PROTOCOL_T0:
1148
1149                if (ct_config.debug >= 1)
1150                        ct_debug("ps_apdu_send: using EXCHANGE_TPDU_T0");
1151
1152                instruction = PS_EXCHANGE_TPDU_T0;
1153
1154                break;
1155
1156        case IFD_PROTOCOL_T1:
1157               
1158                if (ct_config.debug >= 1)
1159                        ct_debug("ps_apdu_send: using EXCHANGE_TPDU_T1");
1160
1161                instruction = PS_EXCHANGE_TPDU_T1;
1162
1163                break;
1164
1165        default:
1166
1167                if (ct_config.debug >= 1)
1168                        ct_debug("ps_apdu_send: unknow protocol");
1169                return IFD_ERROR_GENERIC;
1170        }
1171
1172        /* start the transmission */
1173        rc = ps_if_transmission_start(dev, dev->timeout);
1174
1175        if (IFD_SUCCESS == rc) {
1176
1177                rc = ps_send_to_ifd(reader, instruction, sbuf, slen);
1178
1179        }
1180
1181        if (0 > rc) {
1182                ct_error("ps_apdu_send: error %i", rc);
1183        }
1184
1185        return rc;
1186}
1187
1188/*
1189 * Receive an apdu from reader.
1190 */
1191static int
1192ps_apdu_recv(ifd_reader_t * reader, unsigned int dad, unsigned char *buffer,
1193             size_t len, long timeout)
1194{
1195        int rc = IFD_SUCCESS;
1196        ifd_device_t *dev = NULL;
1197        ps_device_data_t *dev_data = NULL;
1198
1199        if (ct_config.debug >= 1)
1200                ct_debug("ps_apdu_recv: called");
1201
1202        dev = reader->device;
1203        dev_data = (ps_device_data_t *) dev->user_data;
1204
1205        rc = ps_receive_from_ifd(reader, buffer, len);
1206
1207        if (rc < 0) {
1208                ct_error("ps_apdu_recv: failed");
1209        } else {
1210                if (ct_config.debug >= 3)
1211                        ct_debug("ps_apdu_recv: received %i bytes: %s",
1212                                 rc, ct_hexdump(buffer, rc));
1213        }
1214
1215        ps_if_transmission_end(dev);
1216
1217        return rc;
1218}
1219
1220/*
1221 * Initialize the device
1222 */
1223static int ps_open(ifd_reader_t * reader, const char *device_name)
1224{
1225        int rc = IFD_SUCCESS;
1226        ifd_device_t *dev = NULL;
1227        ps_device_data_t *device_data = NULL;
1228        ifd_device_params_t params;
1229
1230        unsigned char sbuf[1];
1231
1232        if (ct_config.debug >= 1)
1233                ct_debug("ps_open: called: device name =%s", device_name);
1234
1235        dev = ifd_device_open(device_name);
1236
1237        if (NULL == dev) {
1238                ct_error("ps_open: failed to open device: %", device_name);
1239                rc = IFD_ERROR_GENERIC;
1240                goto out;
1241        }
1242
1243        switch (dev->type) {
1244
1245        case IFD_DEVICE_TYPE_USB:
1246                reader->name = PS_USB_READER_NAME;
1247                break;
1248
1249        default:
1250                ct_error("ps_open: unknow device type %i", dev->type);
1251                rc = IFD_ERROR_GENERIC;
1252                goto out;
1253        }
1254
1255        /* set usb interface parameters */
1256
1257        params = dev->settings;
1258
1259        params.usb.interface = PS_USB_INTERFACE_INDEX;
1260        params.usb.ep_intr = PS_USB_INTERRUPT_ENDPOINT_ADDRESS;
1261        params.usb.ep_o = PS_USB_BULK_OUTPUT_ENDPOINT_ADDRESS;
1262        params.usb.ep_i = PS_USB_BULK_INPUT_ENDPOINT_ADDRESS;
1263
1264        rc = ifd_device_set_parameters(dev, &params);
1265
1266        if (IFD_SUCCESS > rc) {
1267                ct_error("ps_open: ifd_device_set_parameters returned error %i",
1268                         rc);
1269                goto out;
1270        }
1271
1272        device_data = (ps_device_data_t *) calloc(1, sizeof(*device_data));
1273
1274        if (NULL == device_data) {
1275                ct_error("ps_open: not enough memory");
1276                rc = IFD_ERROR_NO_MEMORY;
1277                goto out;
1278        }
1279
1280        memset(device_data, 0, sizeof(*device_data));
1281
1282        device_data->if_state = IDLE;
1283        device_data->card_status = PS_CARD_UNKNOWN;
1284        device_data->cur_icc_proto = IFD_PROTOCOL_DEFAULT;
1285
1286        reader->nslots = 1;
1287        reader->device = dev;
1288        reader->device->user_data = device_data;
1289        reader->device->timeout = PS_BULK_TIMEOUT;
1290
1291        /* set reader mode */
1292        sbuf[0] = PS_OPTION_EMV_MODE_ON;
1293
1294        rc = ps_transceive_instruction(reader, PS_SET_OPTION, sbuf, 1, NULL, 0);
1295
1296        if (IFD_SUCCESS > rc) {
1297                ct_error("ps_open: error setting reader option");
1298                goto out;
1299        }
1300
1301        rc = ifd_usb_begin_capture(dev,
1302                                   IFD_USB_URB_TYPE_INTERRUPT,
1303                                   params.usb.ep_intr,
1304                                   PS_INTERRUPT_URB_DATA_SIZE,
1305                                   &(device_data->capture));
1306
1307      out:
1308
1309        if (IFD_SUCCESS > rc && NULL != dev) {
1310                ifd_device_close(dev);
1311        }
1312
1313        return rc;
1314}
1315
1316/*
1317 * Free resources used by reader.
1318 */
1319static int ps_close(ifd_reader_t * reader)
1320{
1321        ifd_device_t *dev;
1322        ps_device_data_t *device_data;
1323
1324        if (ct_config.debug >= 1)
1325                ct_debug("ps_close: called");
1326
1327        dev = reader->device;
1328        device_data = (ps_device_data_t *) dev->user_data;
1329
1330        ps_deactivate(reader);
1331
1332        if (NULL != device_data->capture) {
1333                ifd_usb_end_capture(dev, device_data->capture);
1334                device_data->capture = NULL;
1335        }
1336
1337        free(device_data);
1338
1339        ifd_device_close(dev);
1340
1341        return 0;
1342}
1343
1344/*
1345 * Initialize this module
1346 */
1347void ifd_pertosmart_ac1038_register(void)
1348{
1349
1350        static struct ifd_driver_ops perto_smart_driver;
1351
1352        perto_smart_driver.open = ps_open;
1353        perto_smart_driver.close = ps_close;
1354        perto_smart_driver.activate = ps_activate;
1355        perto_smart_driver.deactivate = ps_deactivate;
1356        perto_smart_driver.card_status = ps_card_status;
1357        perto_smart_driver.card_reset = ps_card_reset;
1358        perto_smart_driver.set_protocol = ps_set_protocol;
1359        perto_smart_driver.send = ps_apdu_send;
1360        perto_smart_driver.recv = ps_apdu_recv;
1361
1362        ifd_driver_register("pertosmart1038", &perto_smart_driver);
1363}
Note: See TracBrowser for help on using the repository browser.