48 #include <sys/types.h>
74 #define BUS_DEVICE_STRSIZE 10+1+10+1+10+1
76 #define READER_ABSENT 0
77 #define READER_PRESENT 1
78 #define READER_FAILED 2
83 extern char Add_Serial_In_Name;
88 pthread_mutex_t usbNotifierMutex;
90 static pthread_t usbNotifyThread;
91 static int driverSize = -1;
92 static char AraKiriHotPlug = FALSE;
93 static int rescan_pipe[] = { -1, -1 };
94 extern int HPForceReaderPolling;
97 #define IFD_GENERATE_HOTPLUG 1
102 static struct _driverTracker
105 unsigned int productID;
111 } *driverTracker = NULL;
112 #define DRIVER_TRACKER_SIZE_STEP 8
117 static struct _readerTracker
120 char bus_device[BUS_DEVICE_STRSIZE];
124 static LONG HPAddHotPluggable(
struct libusb_device *dev,
125 struct libusb_device_descriptor desc,
126 const char bus_device[],
int interface,
127 struct _driverTracker *driver);
128 static LONG HPRemoveHotPluggable(
int reader_index);
130 static LONG HPReadBundleValues(
void)
134 struct dirent *currFP = NULL;
135 char fullPath[FILENAME_MAX];
136 char fullLibPath[FILENAME_MAX];
139 hpDir = opendir(PCSCLITE_HP_DROPDIR);
143 Log1(PCSC_LOG_ERROR,
"Cannot open PC/SC drivers directory: " PCSCLITE_HP_DROPDIR);
144 Log1(PCSC_LOG_ERROR,
"Disabling USB support for pcscd.");
149 driverTracker = calloc(DRIVER_TRACKER_SIZE_STEP,
sizeof(*driverTracker));
150 if (NULL == driverTracker)
152 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
155 driverSize = DRIVER_TRACKER_SIZE_STEP;
157 #define GET_KEY(key, values) \
158 rv = LTPBundleFindValueWithKey(&plist, key, values); \
161 Log2(PCSC_LOG_ERROR, "Value/Key not defined for " key " in %s", \
166 while ((currFP = readdir(hpDir)) != 0)
168 if (strstr(currFP->d_name,
".bundle") != 0)
172 list_t *manuIDs, *productIDs, *readerNames;
180 snprintf(fullPath,
sizeof(fullPath),
"%s/%s/Contents/Info.plist",
181 PCSCLITE_HP_DROPDIR, currFP->d_name);
182 fullPath[
sizeof(fullPath) - 1] =
'\0';
184 rv = bundleParse(fullPath, &plist);
189 GET_KEY(PCSCLITE_HP_LIBRKEY_NAME, &values)
190 libraryPath = list_get_at(values, 0);
191 (
void)snprintf(fullLibPath, sizeof(fullLibPath),
192 "%s/%s/Contents/%s/%s",
193 PCSCLITE_HP_DROPDIR, currFP->d_name, PCSC_ARCH,
195 fullLibPath[sizeof(fullLibPath) - 1] = '\0';
198 GET_KEY(PCSCLITE_HP_CPCTKEY_NAME, &values)
199 ifdCapabilities = strtol(list_get_at(values, 0), NULL, 16);
201 GET_KEY(PCSCLITE_HP_MANUKEY_NAME, &manuIDs)
202 GET_KEY(PCSCLITE_HP_PRODKEY_NAME, &productIDs)
203 GET_KEY(PCSCLITE_HP_NAMEKEY_NAME, &readerNames)
206 for (alias=0; alias<list_size(manuIDs); alias++)
211 value = list_get_at(manuIDs, alias);
212 driverTracker[listCount].manuID = strtol(value, NULL, 16);
214 value = list_get_at(productIDs, alias);
215 driverTracker[listCount].productID = strtol(value, NULL, 16);
217 driverTracker[listCount].readerName = strdup(list_get_at(readerNames, alias));
220 driverTracker[listCount].bundleName = strdup(currFP->d_name);
221 driverTracker[listCount].libraryPath = strdup(fullLibPath);
222 driverTracker[listCount].ifdCapabilities = ifdCapabilities;
225 Log2(PCSC_LOG_INFO,
"Found driver for: %s",
226 driverTracker[listCount].readerName);
229 if (listCount >= driverSize)
234 driverSize += DRIVER_TRACKER_SIZE_STEP;
237 "Increase driverTracker to %d entries", driverSize);
239 driverTracker = realloc(driverTracker,
240 driverSize *
sizeof(*driverTracker));
241 if (NULL == driverTracker)
243 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
250 for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
252 driverTracker[i].manuID = 0;
253 driverTracker[i].productID = 0;
254 driverTracker[i].bundleName = NULL;
255 driverTracker[i].libraryPath = NULL;
256 driverTracker[i].readerName = NULL;
257 driverTracker[i].ifdCapabilities = 0;
261 bundleRelease(&plist);
265 driverSize = listCount;
271 Log1(PCSC_LOG_INFO,
"No bundle files in pcsc drivers directory: " PCSCLITE_HP_DROPDIR);
272 Log1(PCSC_LOG_INFO,
"Disabling USB support for pcscd");
277 Log2(PCSC_LOG_INFO,
"Found drivers for %d readers", listCount);
283 static void HPRescanUsbBus(
void)
286 char bus_device[BUS_DEVICE_STRSIZE];
287 libusb_device **devs, *dev;
292 readerTracker[i].status = READER_ABSENT;
294 cnt = libusb_get_device_list(ctx, &devs);
297 Log1(PCSC_LOG_CRITICAL,
"libusb_get_device_list() failed\n");
303 while ((dev = devs[cnt++]) != NULL)
305 struct libusb_device_descriptor desc;
306 struct libusb_config_descriptor *config_desc;
307 uint8_t bus_number = libusb_get_bus_number(dev);
308 uint8_t device_address = libusb_get_device_address(dev);
310 int r = libusb_get_device_descriptor(dev, &desc);
313 Log3(PCSC_LOG_ERROR,
"failed to get device descriptor for %d/%d",
314 bus_number, device_address);
318 r = libusb_get_active_config_descriptor(dev, &config_desc);
321 Log3(PCSC_LOG_ERROR,
"failed to get device config for %d/%d",
322 bus_number, device_address);
327 for (i=0; i<driverSize; i++)
329 if (driverTracker[i].libraryPath != NULL &&
330 desc.idVendor == driverTracker[i].manuID &&
331 desc.idProduct == driverTracker[i].productID)
336 Log3(PCSC_LOG_DEBUG,
"Found matching USB device: %d:%d",
337 bus_number, device_address);
340 for (interface = 0;
interface < config_desc->bNumInterfaces;
346 snprintf(bus_device, BUS_DEVICE_STRSIZE,
"%d:%d:%d",
347 bus_number, device_address, interface);
348 bus_device[BUS_DEVICE_STRSIZE - 1] =
'\0';
354 if (strncmp(readerTracker[j].bus_device,
355 bus_device, BUS_DEVICE_STRSIZE) == 0)
358 readerTracker[j].status = READER_PRESENT;
361 Log2(PCSC_LOG_DEBUG,
"Refresh USB device: %s",
371 if (config_desc->bNumInterfaces > 1)
372 HPAddHotPluggable(dev, desc, bus_device,
373 interface, &driverTracker[i]);
375 HPAddHotPluggable(dev, desc, bus_device,
376 -1, &driverTracker[i]);
381 libusb_free_config_descriptor(config_desc);
389 if ((readerTracker[i].status == READER_ABSENT) &&
390 (readerTracker[i].fullName != NULL))
391 HPRemoveHotPluggable(i);
398 for (i=0; i<driverSize; i++)
401 free(driverTracker[i].bundleName);
402 free(driverTracker[i].libraryPath);
403 free(driverTracker[i].readerName);
407 Log1(PCSC_LOG_INFO,
"Hotplug stopped");
408 pthread_exit(&retval);
412 libusb_free_device_list(devs, 1);
415 static void HPEstablishUSBNotifications(
int pipefd[2])
421 r = libusb_init(ctx);
424 Log2(PCSC_LOG_CRITICAL,
"libusb_init failed: %d", r);
426 kill(getpid(), SIGTERM);
434 write(pipefd[1], &c, 1);
438 for (i=0; i<driverSize; i++)
439 if (driverTracker[i].libraryPath)
440 if ((driverTracker[i].ifdCapabilities & IFD_GENERATE_HOTPLUG) == 0)
443 "Driver %s does not support IFD_GENERATE_HOTPLUG. Using active polling instead.",
444 driverTracker[i].bundleName);
445 if (HPForceReaderPolling < 1)
446 HPForceReaderPolling = 1;
450 if (HPForceReaderPolling)
453 "Polling forced every %d second(s)", HPForceReaderPolling);
459 while (!AraKiriHotPlug)
470 while (read(rescan_pipe[0], &dummy,
sizeof(dummy)) > 0)
472 Log1(PCSC_LOG_INFO,
"Reload serial configuration");
475 RFReCheckReaderConf();
477 Log1(PCSC_LOG_INFO,
"End reload serial configuration");
479 close(rescan_pipe[0]);
484 LONG HPSearchHotPluggables(
void)
490 readerTracker[i].status = READER_ABSENT;
491 readerTracker[i].bus_device[0] =
'\0';
492 readerTracker[i].fullName = NULL;
495 if (HPReadBundleValues())
500 if (pipe(pipefd) == -1)
502 Log2(PCSC_LOG_ERROR,
"pipe: %s", strerror(errno));
506 ThreadCreate(&usbNotifyThread, THREAD_ATTR_DETACHED,
507 (PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, pipefd);
510 read(pipefd[0], &c, 1);
520 LONG HPStopHotPluggables(
void)
522 AraKiriHotPlug = TRUE;
523 if (rescan_pipe[1] >= 0)
525 close(rescan_pipe[1]);
532 static LONG HPAddHotPluggable(
struct libusb_device *dev,
533 struct libusb_device_descriptor desc,
534 const char bus_device[],
int interface,
535 struct _driverTracker *driver)
538 char deviceName[MAX_DEVICENAME];
540 Log2(PCSC_LOG_INFO,
"Adding USB device: %s", bus_device);
543 snprintf(deviceName,
sizeof(deviceName),
"usb:%04x/%04x:libhal:/org/freedesktop/Hal/devices/usb_device_%04x_%04x_serialnotneeded_if%d",
544 desc.idVendor, desc.idProduct, desc.idVendor, desc.idProduct,
547 snprintf(deviceName,
sizeof(deviceName),
"usb:%04x/%04x:libusb-1.0:%s",
548 desc.idVendor, desc.idProduct, bus_device);
550 deviceName[
sizeof(deviceName) -1] =
'\0';
552 pthread_mutex_lock(&usbNotifierMutex);
557 if (readerTracker[i].fullName == NULL)
561 if (i==PCSCLITE_MAX_READERS_CONTEXTS)
564 "Not enough reader entries. Already found %d readers", i);
565 pthread_mutex_unlock(&usbNotifierMutex);
569 strncpy(readerTracker[i].bus_device, bus_device,
570 sizeof(readerTracker[i].bus_device));
571 readerTracker[i].bus_device[
sizeof(readerTracker[i].bus_device) - 1] =
'\0';
573 if (Add_Serial_In_Name && desc.iSerialNumber)
575 libusb_device_handle *device;
578 ret = libusb_open(dev, &device);
581 Log2(PCSC_LOG_ERROR,
"libusb_open failed: %d", ret);
585 unsigned char serialNumber[MAX_READERNAME];
587 ret = libusb_get_string_descriptor_ascii(device, desc.iSerialNumber,
588 serialNumber, MAX_READERNAME);
589 libusb_close(device);
594 "libusb_get_string_descriptor_ascii failed: %d", ret);
595 readerTracker[i].fullName = strdup(driver->readerName);
599 char fullname[MAX_READERNAME];
601 snprintf(fullname,
sizeof(fullname),
"%s (%s)",
602 driver->readerName, serialNumber);
603 readerTracker[i].fullName = strdup(fullname);
608 readerTracker[i].fullName = strdup(driver->readerName);
610 if (RFAddReader(readerTracker[i].fullName, PCSCLITE_HP_BASE_PORT + i,
612 readerTracker[i].status = READER_PRESENT;
615 readerTracker[i].status = READER_FAILED;
617 (void)CheckForOpenCT();
620 pthread_mutex_unlock(&usbNotifierMutex);
625 static LONG HPRemoveHotPluggable(
int reader_index)
627 pthread_mutex_lock(&usbNotifierMutex);
629 Log3(PCSC_LOG_INFO,
"Removing USB device[%d]: %s", reader_index,
630 readerTracker[reader_index].bus_device);
632 RFRemoveReader(readerTracker[reader_index].fullName,
633 PCSCLITE_HP_BASE_PORT + reader_index);
634 free(readerTracker[reader_index].fullName);
635 readerTracker[reader_index].status = READER_ABSENT;
636 readerTracker[reader_index].bus_device[0] =
'\0';
637 readerTracker[reader_index].fullName = NULL;
639 pthread_mutex_unlock(&usbNotifierMutex);
647 ULONG HPRegisterForHotplugEvents(
void)
649 (void)pthread_mutex_init(&usbNotifierMutex, NULL);
653 void HPReCheckSerialReaders(
void)
656 if (rescan_pipe[1] >= 0)
659 write(rescan_pipe[1], &dummy,
sizeof(dummy));
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
Reads lexical config files and updates database.
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
This defines some structures and #defines to be used over the transport layer.
This keeps a list of Windows(R) types.
This keeps a list of defines for pcsc-lite.
This keeps track of a list of currently available reader structures.
This provides a search API for hot pluggble devices.
#define SCARD_S_SUCCESS
error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx