46 #include <sys/types.h>
72 #define BUS_DEVICE_STRSIZE 10+1+10+1+10+1
74 #define READER_ABSENT 0
75 #define READER_PRESENT 1
76 #define READER_FAILED 2
81 extern char Add_Serial_In_Name;
86 pthread_mutex_t usbNotifierMutex;
88 static pthread_t usbNotifyThread;
89 static int driverSize = -1;
90 static char AraKiriHotPlug = FALSE;
91 static int rescan_pipe[] = { -1, -1 };
92 extern int HPForceReaderPolling;
95 #define IFD_GENERATE_HOTPLUG 1
100 static struct _driverTracker
103 unsigned int productID;
109 } *driverTracker = NULL;
110 #define DRIVER_TRACKER_SIZE_STEP 8
115 static struct _readerTracker
118 char bus_device[BUS_DEVICE_STRSIZE];
122 static LONG HPAddHotPluggable(
struct libusb_device *dev,
123 struct libusb_device_descriptor desc,
124 const char bus_device[],
int interface,
125 struct _driverTracker *driver);
126 static LONG HPRemoveHotPluggable(
int reader_index);
128 static LONG HPReadBundleValues(
void)
132 struct dirent *currFP = NULL;
133 char fullPath[FILENAME_MAX];
134 char fullLibPath[FILENAME_MAX];
137 hpDir = opendir(PCSCLITE_HP_DROPDIR);
141 Log1(PCSC_LOG_ERROR,
"Cannot open PC/SC drivers directory: " PCSCLITE_HP_DROPDIR);
142 Log1(PCSC_LOG_ERROR,
"Disabling USB support for pcscd.");
147 driverTracker = calloc(DRIVER_TRACKER_SIZE_STEP,
sizeof(*driverTracker));
148 if (NULL == driverTracker)
150 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
153 driverSize = DRIVER_TRACKER_SIZE_STEP;
155 #define GET_KEY(key, values) \
156 rv = LTPBundleFindValueWithKey(&plist, key, values); \
159 Log2(PCSC_LOG_ERROR, "Value/Key not defined for " key " in %s", \
164 while ((currFP = readdir(hpDir)) != 0)
166 if (strstr(currFP->d_name,
".bundle") != 0)
170 list_t *manuIDs, *productIDs, *readerNames;
178 snprintf(fullPath,
sizeof(fullPath),
"%s/%s/Contents/Info.plist",
179 PCSCLITE_HP_DROPDIR, currFP->d_name);
180 fullPath[
sizeof(fullPath) - 1] =
'\0';
182 rv = bundleParse(fullPath, &plist);
187 GET_KEY(PCSCLITE_HP_LIBRKEY_NAME, &values)
188 libraryPath = list_get_at(values, 0);
189 (
void)snprintf(fullLibPath, sizeof(fullLibPath),
190 "%s/%s/Contents/%s/%s",
191 PCSCLITE_HP_DROPDIR, currFP->d_name, PCSC_ARCH,
193 fullLibPath[sizeof(fullLibPath) - 1] = '\0';
196 GET_KEY(PCSCLITE_HP_CPCTKEY_NAME, &values)
197 ifdCapabilities = strtol(list_get_at(values, 0), NULL, 16);
199 GET_KEY(PCSCLITE_HP_MANUKEY_NAME, &manuIDs)
200 GET_KEY(PCSCLITE_HP_PRODKEY_NAME, &productIDs)
201 GET_KEY(PCSCLITE_HP_NAMEKEY_NAME, &readerNames)
204 for (alias=0; alias<list_size(manuIDs); alias++)
209 value = list_get_at(manuIDs, alias);
210 driverTracker[listCount].manuID = strtol(value, NULL, 16);
212 value = list_get_at(productIDs, alias);
213 driverTracker[listCount].productID = strtol(value, NULL, 16);
215 driverTracker[listCount].readerName = strdup(list_get_at(readerNames, alias));
218 driverTracker[listCount].bundleName = strdup(currFP->d_name);
219 driverTracker[listCount].libraryPath = strdup(fullLibPath);
220 driverTracker[listCount].ifdCapabilities = ifdCapabilities;
223 Log2(PCSC_LOG_INFO,
"Found driver for: %s",
224 driverTracker[listCount].readerName);
227 if (listCount >= driverSize)
232 driverSize += DRIVER_TRACKER_SIZE_STEP;
235 "Increase driverTracker to %d entries", driverSize);
237 driverTracker = realloc(driverTracker,
238 driverSize *
sizeof(*driverTracker));
239 if (NULL == driverTracker)
241 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
248 for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
250 driverTracker[i].manuID = 0;
251 driverTracker[i].productID = 0;
252 driverTracker[i].bundleName = NULL;
253 driverTracker[i].libraryPath = NULL;
254 driverTracker[i].readerName = NULL;
255 driverTracker[i].ifdCapabilities = 0;
259 bundleRelease(&plist);
263 driverSize = listCount;
269 Log1(PCSC_LOG_INFO,
"No bundle files in pcsc drivers directory: " PCSCLITE_HP_DROPDIR);
270 Log1(PCSC_LOG_INFO,
"Disabling USB support for pcscd");
275 Log2(PCSC_LOG_INFO,
"Found drivers for %d readers", listCount);
281 static void HPRescanUsbBus(
void)
284 char bus_device[BUS_DEVICE_STRSIZE];
285 libusb_device **devs, *dev;
290 readerTracker[i].status = READER_ABSENT;
292 cnt = libusb_get_device_list(ctx, &devs);
295 Log1(PCSC_LOG_CRITICAL,
"libusb_get_device_list() failed\n");
301 while ((dev = devs[cnt++]) != NULL)
303 struct libusb_device_descriptor desc;
304 struct libusb_config_descriptor *config_desc;
305 uint8_t bus_number = libusb_get_bus_number(dev);
306 uint8_t device_address = libusb_get_device_address(dev);
308 int r = libusb_get_device_descriptor(dev, &desc);
311 Log3(PCSC_LOG_ERROR,
"failed to get device descriptor for %d/%d",
312 bus_number, device_address);
316 r = libusb_get_active_config_descriptor(dev, &config_desc);
319 Log3(PCSC_LOG_ERROR,
"failed to get device config for %d/%d",
320 bus_number, device_address);
325 for (i=0; i<driverSize; i++)
327 if (driverTracker[i].libraryPath != NULL &&
328 desc.idVendor == driverTracker[i].manuID &&
329 desc.idProduct == driverTracker[i].productID)
334 Log3(PCSC_LOG_DEBUG,
"Found matching USB device: %d:%d",
335 bus_number, device_address);
338 for (interface = 0;
interface < config_desc->bNumInterfaces;
344 snprintf(bus_device, BUS_DEVICE_STRSIZE,
"%d:%d:%d",
345 bus_number, device_address, interface);
346 bus_device[BUS_DEVICE_STRSIZE - 1] =
'\0';
352 if (strncmp(readerTracker[j].bus_device,
353 bus_device, BUS_DEVICE_STRSIZE) == 0)
356 readerTracker[j].status = READER_PRESENT;
359 Log2(PCSC_LOG_DEBUG,
"Refresh USB device: %s",
369 if (config_desc->bNumInterfaces > 1)
370 HPAddHotPluggable(dev, desc, bus_device,
371 interface, &driverTracker[i]);
373 HPAddHotPluggable(dev, desc, bus_device,
374 -1, &driverTracker[i]);
379 libusb_free_config_descriptor(config_desc);
387 if ((readerTracker[i].status == READER_ABSENT) &&
388 (readerTracker[i].fullName != NULL))
389 HPRemoveHotPluggable(i);
396 for (i=0; i<driverSize; i++)
399 free(driverTracker[i].bundleName);
400 free(driverTracker[i].libraryPath);
401 free(driverTracker[i].readerName);
405 Log1(PCSC_LOG_INFO,
"Hotplug stopped");
406 pthread_exit(&retval);
410 libusb_free_device_list(devs, 1);
413 static void HPEstablishUSBNotifications(
int pipefd[2])
419 r = libusb_init(ctx);
422 Log2(PCSC_LOG_CRITICAL,
"libusb_init failed: %d", r);
424 kill(getpid(), SIGTERM);
432 write(pipefd[1], &c, 1);
436 for (i=0; i<driverSize; i++)
437 if (driverTracker[i].libraryPath)
438 if ((driverTracker[i].ifdCapabilities & IFD_GENERATE_HOTPLUG) == 0)
441 "Driver %s does not support IFD_GENERATE_HOTPLUG. Using active polling instead.",
442 driverTracker[i].bundleName);
443 if (HPForceReaderPolling < 1)
444 HPForceReaderPolling = 1;
448 if (HPForceReaderPolling)
451 "Polling forced every %d second(s)", HPForceReaderPolling);
457 while (!AraKiriHotPlug)
468 while (read(rescan_pipe[0], &dummy,
sizeof(dummy)) > 0)
470 Log1(PCSC_LOG_INFO,
"Reload serial configuration");
473 RFReCheckReaderConf();
475 Log1(PCSC_LOG_INFO,
"End reload serial configuration");
477 close(rescan_pipe[0]);
482 LONG HPSearchHotPluggables(
void)
488 readerTracker[i].status = READER_ABSENT;
489 readerTracker[i].bus_device[0] =
'\0';
490 readerTracker[i].fullName = NULL;
493 if (HPReadBundleValues())
498 if (pipe(pipefd) == -1)
500 Log2(PCSC_LOG_ERROR,
"pipe: %s", strerror(errno));
504 ThreadCreate(&usbNotifyThread, THREAD_ATTR_DETACHED,
505 (PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, pipefd);
508 read(pipefd[0], &c, 1);
518 LONG HPStopHotPluggables(
void)
520 AraKiriHotPlug = TRUE;
521 if (rescan_pipe[1] >= 0)
523 close(rescan_pipe[1]);
530 static LONG HPAddHotPluggable(
struct libusb_device *dev,
531 struct libusb_device_descriptor desc,
532 const char bus_device[],
int interface,
533 struct _driverTracker *driver)
536 char deviceName[MAX_DEVICENAME];
538 Log2(PCSC_LOG_INFO,
"Adding USB device: %s", bus_device);
541 snprintf(deviceName,
sizeof(deviceName),
"usb:%04x/%04x:libhal:/org/freedesktop/Hal/devices/usb_device_%04x_%04x_serialnotneeded_if%d",
542 desc.idVendor, desc.idProduct, desc.idVendor, desc.idProduct,
545 snprintf(deviceName,
sizeof(deviceName),
"usb:%04x/%04x:libusb-1.0:%s",
546 desc.idVendor, desc.idProduct, bus_device);
548 deviceName[
sizeof(deviceName) -1] =
'\0';
550 pthread_mutex_lock(&usbNotifierMutex);
555 if (readerTracker[i].fullName == NULL)
559 if (i==PCSCLITE_MAX_READERS_CONTEXTS)
562 "Not enough reader entries. Already found %d readers", i);
563 pthread_mutex_unlock(&usbNotifierMutex);
567 strncpy(readerTracker[i].bus_device, bus_device,
568 sizeof(readerTracker[i].bus_device));
569 readerTracker[i].bus_device[
sizeof(readerTracker[i].bus_device) - 1] =
'\0';
571 if (Add_Serial_In_Name && desc.iSerialNumber)
573 libusb_device_handle *device;
576 ret = libusb_open(dev, &device);
579 Log2(PCSC_LOG_ERROR,
"libusb_open failed: %d", ret);
583 unsigned char serialNumber[MAX_READERNAME];
585 ret = libusb_get_string_descriptor_ascii(device, desc.iSerialNumber,
586 serialNumber, MAX_READERNAME);
587 libusb_close(device);
592 "libusb_get_string_descriptor_ascii failed: %d", ret);
593 readerTracker[i].fullName = strdup(driver->readerName);
597 char fullname[MAX_READERNAME];
599 snprintf(fullname,
sizeof(fullname),
"%s (%s)",
600 driver->readerName, serialNumber);
601 readerTracker[i].fullName = strdup(fullname);
606 readerTracker[i].fullName = strdup(driver->readerName);
608 if (RFAddReader(readerTracker[i].fullName, PCSCLITE_HP_BASE_PORT + i,
610 readerTracker[i].status = READER_PRESENT;
613 readerTracker[i].status = READER_FAILED;
615 (void)CheckForOpenCT();
618 pthread_mutex_unlock(&usbNotifierMutex);
623 static LONG HPRemoveHotPluggable(
int reader_index)
625 pthread_mutex_lock(&usbNotifierMutex);
627 Log3(PCSC_LOG_INFO,
"Removing USB device[%d]: %s", reader_index,
628 readerTracker[reader_index].bus_device);
630 RFRemoveReader(readerTracker[reader_index].fullName,
631 PCSCLITE_HP_BASE_PORT + reader_index);
632 free(readerTracker[reader_index].fullName);
633 readerTracker[reader_index].status = READER_ABSENT;
634 readerTracker[reader_index].bus_device[0] =
'\0';
635 readerTracker[reader_index].fullName = NULL;
637 pthread_mutex_unlock(&usbNotifierMutex);
645 ULONG HPRegisterForHotplugEvents(
void)
647 (void)pthread_mutex_init(&usbNotifierMutex, NULL);
651 void HPReCheckSerialReaders(
void)
654 if (rescan_pipe[1] >= 0)
657 write(rescan_pipe[1], &dummy,
sizeof(dummy));
#define SCARD_S_SUCCESS
No error was encountered.
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.