~logan/ubuntu/quantal/acsccid/new-upstream

« back to all changes in this revision

Viewing changes to src/ccid_usb.c

  • Committer: Logan Rosen
  • Date: 2012-07-27 05:48:12 UTC
  • mfrom: (1.1.2)
  • Revision ID: logatronico@gmail.com-20120727054812-k7mhbnf0d2mqp61r
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#define __CCID_USB__
26
26
 
 
27
#ifdef __APPLE__
 
28
#include <IOKit/usb/IOUSBLib.h>
 
29
#endif
 
30
 
27
31
#include <stdio.h>
28
32
#include <string.h>
29
33
#include <errno.h>
106
110
        struct usb_device *dev, int num);
107
111
 
108
112
#ifdef __APPLE__
 
113
 
 
114
#if defined (kIOUSBInterfaceInterfaceID220)
 
115
#define usb_interface_t IOUSBInterfaceInterface220
 
116
#elif defined (kIOUSBInterfaceInterfaceID197)
 
117
#define usb_interface_t IOUSBInterfaceInterface197
 
118
#elif defined (kIOUSBInterfaceInterfaceID190)
 
119
#define usb_interface_t IOUSBInterfaceInterface190
 
120
#elif defined (kIOUSBInterfaceInterfaceID182)
 
121
#define usb_interface_t IOUSBInterfaceInterface182
 
122
#else
 
123
#define usb_interface_t IOUSBInterfaceInterface
 
124
#endif
 
125
 
 
126
#if defined (kIOUSBDeviceInterfaceID197)
 
127
#define usb_device_t    IOUSBDeviceInterface197
 
128
#elif defined (kIOUSBDeviceInterfaceID187)
 
129
#define usb_device_t    IOUSBDeviceInterface187
 
130
#elif defined (kIOUSBDeviceInterfaceID182)
 
131
#define usb_device_t    IOUSBDeviceInterface182
 
132
#else
 
133
#define usb_device_t    IOUSBDeviceInterface
 
134
#endif
 
135
 
 
136
struct darwin_dev_handle {
 
137
        usb_device_t **device;
 
138
        usb_interface_t **interface;
 
139
        int open;
 
140
 
 
141
        int num_endpoints;
 
142
        unsigned char *endpoint_addrs;
 
143
};
 
144
 
 
145
struct usb_dev_handle {
 
146
        int fd;
 
147
 
 
148
        struct usb_bus *bus;
 
149
        struct usb_device *device;
 
150
 
 
151
        int config;
 
152
        int interface;
 
153
        int altsetting;
 
154
 
 
155
        void *impl_info;
 
156
};
 
157
 
109
158
static void *CardDetectionThread(void *pParam);
 
159
static int GetPipeRef(struct darwin_dev_handle *device, int ep);
110
160
#endif
111
161
 
112
162
/* ne need to initialize to 0 since it is static */
274
324
                        /* dirname was just a temporary variable */
275
325
                        dirname = NULL;
276
326
                }
 
327
 
 
328
                /* format usb:%04x/%04x:libudev:%d:%s
 
329
                 * with %d set to
 
330
                 * 01 (or whatever the interface number is)
 
331
                 * and %s set to
 
332
                 * /dev/bus/usb/008/004
 
333
                 */
 
334
                if ((dirname = strstr(device, "libudev:")) != NULL)
 
335
                {
 
336
                        /* convert the interface number */
 
337
                        interface_number = atoi(dirname + 8 /* "libudev:" */);
 
338
                        DEBUG_COMM2("interface_number: %d", interface_number);
 
339
 
 
340
                        /* dirname was just a temporary variable */
 
341
                        dirname = NULL;
 
342
                }
277
343
        }
278
344
#endif
279
345
 
401
467
                                                // Simulate ACR1281 Dual Reader (composite device) as multi-slot reader
402
468
                                                else if ((ACS_ACR1281_DUAL_READER_QPBOC == readerID) ||
403
469
                                                        (ACS_ACR1281_DUAL_READER_BSI == readerID) ||
404
 
                                                        (ACS_ACR1281_1S_PICC_READER == readerID))
 
470
                                                        (ACS_ACR1281_1S_PICC_READER == readerID) ||
 
471
                                                        (ACS_ACR1251_1S_CL_READER == readerID))
405
472
                                                {
406
473
                                                        // the CCID interfaces are 0 and 1
407
474
                                                        interface_number = static_interface - 1;
784
851
 
785
852
                                        // Initialize isSamSlot
786
853
                                        usbDevice[reader_index].ccid.isSamSlot = FALSE;
 
854
 
 
855
                                        // The 2nd interface (composite device) is a SAM slot
 
856
                                        if ((readerID == ACS_ACR1281_1S_PICC_READER) ||
 
857
                                                (readerID == ACS_ACR1251_1S_CL_READER))
 
858
                                        {
 
859
                                                if (interface == 1)
 
860
                                                        usbDevice[reader_index].ccid.isSamSlot = TRUE;
 
861
                                        }
787
862
#ifdef __APPLE__
788
863
                                        // Initialize terminated flag to false
789
864
                                        usbDevice[reader_index].terminated = FALSE;
1056
1131
        /* release the allocated ressources for the last slot only */
1057
1132
        if (0 == *usbDevice[reader_index].nb_opened_slots)
1058
1133
        {
 
1134
#ifdef __APPLE__
 
1135
                struct darwin_dev_handle *device;
 
1136
                int pipeRef;
 
1137
#endif
1059
1138
                DEBUG_COMM("Last slot closed. Release resources");
1060
1139
 
1061
1140
                /* reset so that bSeq starts at 0 again */
1062
1141
                if (DriverOptions & DRIVER_OPTION_RESET_ON_CLOSE)
1063
1142
                        (void)usb_reset(usbDevice[reader_index].handle);
1064
 
 
1065
 
                (void)usb_release_interface(usbDevice[reader_index].handle,
1066
 
                        usbDevice[reader_index].interface);
1067
 
                (void)usb_close(usbDevice[reader_index].handle);
1068
1143
#ifdef __APPLE__
1069
1144
                DEBUG_INFO3("Terminating thread: %s/%s",
1070
1145
                        usbDevice[reader_index].dirname, usbDevice[reader_index].filename);
1071
1146
 
1072
1147
                // Terminate thread
1073
1148
                *usbDevice[reader_index].pTerminated = TRUE;
 
1149
 
 
1150
                // Abort interrupt pipe
 
1151
                device = (struct darwin_dev_handle *) usbDevice[reader_index].handle->impl_info;
 
1152
                pipeRef = GetPipeRef(device, usbDevice[reader_index].interrupt);
 
1153
                (*(device->interface))->AbortPipe(device->interface, pipeRef);
 
1154
 
 
1155
                // Wait thread
1074
1156
                pthread_join(usbDevice[reader_index].hThread, NULL);
1075
1157
 
1076
1158
                // Free bStatus lock
1077
1159
                pthread_mutex_destroy(usbDevice[reader_index].ccid.pbStatusLock);
1078
1160
#endif
 
1161
                (void)usb_release_interface(usbDevice[reader_index].handle,
 
1162
                        usbDevice[reader_index].interface);
 
1163
                (void)usb_close(usbDevice[reader_index].handle);
 
1164
 
1079
1165
                // Free array of bStatus
1080
1166
                free(usbDevice[reader_index].ccid.bStatus);
1081
1167
 
1088
1174
        usbDevice[reader_index].dirname = NULL;
1089
1175
        usbDevice[reader_index].filename = NULL;
1090
1176
        usbDevice[reader_index].interface = 0;
1091
 
        usbDevice[reader_index].ccid.bStatus = NULL;            // Array of bStatus
 
1177
        usbDevice[reader_index].ccid.bStatus = NULL;    // Array of bStatus
1092
1178
 
1093
1179
        return STATUS_SUCCESS;
1094
1180
} /* CloseUSB */
1342
1428
                request, value, usbDevice[reader_index].interface, (char *)bytes, size,
1343
1429
                usbDevice[reader_index].ccid.readTimeout * 1000);
1344
1430
 
 
1431
        if (ret < 0)
 
1432
        {
 
1433
                DEBUG_CRITICAL4("control failed (%s/%s): %s",
 
1434
                        usbDevice[reader_index].dirname, usbDevice[reader_index].filename,
 
1435
                        strerror(errno));
 
1436
 
 
1437
                return ret;
 
1438
        }
 
1439
 
1345
1440
        if (requesttype & 0x80)
1346
1441
                DEBUG_XXD("receive: ", bytes, ret);
1347
1442
 
1429
1524
        DEBUG_INFO2("Exit: Reader: %d", reader_index);
1430
1525
        return ((void *) 0);
1431
1526
}
 
1527
 
 
1528
// Get pipe reference from endpoint number
 
1529
static int GetPipeRef(struct darwin_dev_handle *device, int ep)
 
1530
{
 
1531
        int i;
 
1532
 
 
1533
        for (i = 0; i < device->num_endpoints; i++)
 
1534
                if (device->endpoint_addrs[i] == ep)
 
1535
                        return i + 1;
 
1536
 
 
1537
        return -1;
 
1538
}
1432
1539
#endif