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
80extern bool Add_Interface_In_Name;
81extern bool Add_Serial_In_Name;
86pthread_mutex_t usbNotifierMutex;
88static pthread_t usbNotifyThread;
89static int driverSize = -1;
90static bool AraKiriHotPlug =
false;
91static int rescan_pipe[] = { -1, -1 };
92extern int HPForceReaderPolling;
95#define IFD_GENERATE_HOTPLUG 1
100static struct _driverTracker
103 unsigned int productID;
110} *driverTracker = NULL;
111#define DRIVER_TRACKER_SIZE_STEP 8
116static struct _readerTracker
119 char bus_device[BUS_DEVICE_STRSIZE];
123static LONG HPAddHotPluggable(
struct libusb_device *dev,
124 struct libusb_device_descriptor desc,
125 const char bus_device[],
126 const struct libusb_interface *idesc,
127 struct _driverTracker *driver,
128 struct _driverTracker *classdriver);
129static LONG HPRemoveHotPluggable(
int reader_index);
130static void HPCleanupHotPluggable(
int reader_index);
132static LONG HPReadBundleValues(
const char * hpDirPath)
136 struct dirent *currFP = NULL;
137 char fullPath[FILENAME_MAX];
138 char fullLibPath[FILENAME_MAX];
141 hpDir = opendir(hpDirPath);
145 Log2(PCSC_LOG_ERROR,
"Cannot open PC/SC drivers directory: %s",
147 Log1(PCSC_LOG_ERROR,
"Disabling USB support for pcscd.");
152 driverTracker = calloc(DRIVER_TRACKER_SIZE_STEP,
sizeof(*driverTracker));
153 if (NULL == driverTracker)
155 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
158 driverSize = DRIVER_TRACKER_SIZE_STEP;
160#define GET_KEY(key, values) \
161 rv = LTPBundleFindValueWithKey(&plist, key, values); \
164 Log2(PCSC_LOG_ERROR, "Value/Key not defined for " key " in %s", \
169 while ((currFP = readdir(hpDir)) != 0)
171 if (strstr(currFP->d_name,
".bundle") != 0)
175 list_t *manuIDs, *productIDs, *readerNames;
184 snprintf(fullPath,
sizeof(fullPath),
"%s/%s/Contents/Info.plist",
185 hpDirPath, currFP->d_name);
186 fullPath[
sizeof(fullPath) - 1] =
'\0';
188 rv = bundleParse(fullPath, &plist);
193 GET_KEY(PCSCLITE_HP_LIBRKEY_NAME, &values)
194 libraryPath = list_get_at(values, 0);
195 (void)snprintf(fullLibPath,
sizeof(fullLibPath),
196 "%s/%s/Contents/%s/%s",
197 hpDirPath, currFP->d_name, PCSC_ARCH,
199 fullLibPath[
sizeof(fullLibPath) - 1] =
'\0';
202 GET_KEY(PCSCLITE_HP_CPCTKEY_NAME, &values)
203 ifdCapabilities = strtol(list_get_at(values, 0), NULL, 16);
205 GET_KEY(PCSCLITE_HP_MANUKEY_NAME, &manuIDs)
206 GET_KEY(PCSCLITE_HP_PRODKEY_NAME, &productIDs)
207 GET_KEY(PCSCLITE_HP_NAMEKEY_NAME, &readerNames)
210 rv = LTPBundleFindValueWithKey(&plist, PCSCLITE_HP_CFBUNDLE_NAME,
215 CFBundleName = list_get_at(values, 0);
218 for (alias=0; alias<list_size(manuIDs); alias++)
223 value = list_get_at(manuIDs, alias);
224 driverTracker[listCount].manuID = strtol(value, NULL, 16);
226 value = list_get_at(productIDs, alias);
227 driverTracker[listCount].productID = strtol(value, NULL, 16);
229 driverTracker[listCount].readerName = strdup(list_get_at(readerNames, alias));
232 driverTracker[listCount].bundleName = strdup(currFP->d_name);
233 driverTracker[listCount].libraryPath = strdup(fullLibPath);
234 driverTracker[listCount].ifdCapabilities = ifdCapabilities;
235 driverTracker[listCount].CFBundleName =
236 CFBundleName ? strdup(CFBundleName) : NULL;
239 Log2(PCSC_LOG_INFO,
"Found driver for: %s",
240 driverTracker[listCount].readerName);
243 if (listCount >= driverSize)
248 driverSize += DRIVER_TRACKER_SIZE_STEP;
251 "Increase driverTracker to %d entries", driverSize);
253 void* tmp = realloc(driverTracker,
254 driverSize *
sizeof(*driverTracker));
258 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
266 for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
268 driverTracker[i].manuID = 0;
269 driverTracker[i].productID = 0;
270 driverTracker[i].bundleName = NULL;
271 driverTracker[i].libraryPath = NULL;
272 driverTracker[i].readerName = NULL;
273 driverTracker[i].ifdCapabilities = 0;
274 driverTracker[i].CFBundleName = NULL;
278 bundleRelease(&plist);
282 driverSize = listCount;
287 Log2(PCSC_LOG_INFO,
"No bundle files in pcsc drivers directory: %s",
289 Log1(PCSC_LOG_INFO,
"Disabling USB support for pcscd");
293 Log2(PCSC_LOG_INFO,
"Found drivers for %d readers", listCount);
299static struct _driverTracker *get_driver(
unsigned int idVendor,
300 unsigned int idProduct,
struct _driverTracker **classdriver)
303 static struct _driverTracker *driver;
307 "Looking for a driver for VID: 0x%04X, PID: 0x%04X",
308 idVendor, idProduct);
314 for (i=0; i<driverSize; i++)
316 if (driverTracker[i].libraryPath != NULL &&
317 idVendor == driverTracker[i].manuID &&
318 idProduct == driverTracker[i].productID)
320 if ((driverTracker[i].CFBundleName != NULL)
321 && (0 == strcmp(driverTracker[i].CFBundleName,
"CCIDCLASSDRIVER")))
322 *classdriver = &driverTracker[i];
325 driver = &driverTracker[i];
337static void HPRescanUsbBus(
void)
340 char bus_device[BUS_DEVICE_STRSIZE];
341 libusb_device **devs, *dev;
346 readerTracker[i].status = READER_ABSENT;
348 cnt = libusb_get_device_list(ctx, &devs);
351 Log2(PCSC_LOG_CRITICAL,
"libusb_get_device_list() failed: %s",
352 libusb_error_name(cnt));
358 while ((dev = devs[cnt++]) != NULL)
360 struct libusb_device_descriptor desc;
361 struct libusb_config_descriptor *config_desc;
362 uint8_t bus_number = libusb_get_bus_number(dev);
363 uint8_t device_address = libusb_get_device_address(dev);
364 struct _driverTracker *driver, *classdriver;
367 int r = libusb_get_device_descriptor(dev, &desc);
371 "failed to get device descriptor for %d/%d: %s",
372 bus_number, device_address, libusb_error_name(r));
376 r = libusb_get_active_config_descriptor(dev, &config_desc);
379 Log4(PCSC_LOG_ERROR,
"failed to get device config for %d/%d: %s",
380 bus_number, device_address, libusb_error_name(r));
384 driver = get_driver(desc.idVendor, desc.idProduct, &classdriver);
389 Log3(PCSC_LOG_DEBUG,
"%d/%d is not a supported smart card reader",
390 bus_number, device_address);
392 libusb_free_config_descriptor(config_desc);
397 Log3(PCSC_LOG_DEBUG,
"Found matching USB device: %d:%d",
398 bus_number, device_address);
401 for (interface = 0;
interface < config_desc->bNumInterfaces;
407 snprintf(bus_device, BUS_DEVICE_STRSIZE,
"%d:%d:%d",
408 bus_number, device_address, interface);
409 bus_device[BUS_DEVICE_STRSIZE - 1] =
'\0';
415 if (strncmp(readerTracker[j].bus_device,
416 bus_device, BUS_DEVICE_STRSIZE) == 0)
419 readerTracker[j].status = READER_PRESENT;
422 Log2(PCSC_LOG_DEBUG,
"Refresh USB device: %s",
431 HPAddHotPluggable(dev, desc, bus_device,
432 &config_desc->interface[interface], driver, classdriver);
435 libusb_free_config_descriptor(config_desc);
443 if ((readerTracker[i].status == READER_ABSENT) &&
444 (readerTracker[i].fullName != NULL))
445 HPRemoveHotPluggable(i);
449 libusb_free_device_list(devs, 1);
452static void * HPEstablishUSBNotifications(
int pipefd[2])
458 r = libusb_init(ctx);
461 Log2(PCSC_LOG_CRITICAL,
"libusb_init failed: %s", libusb_error_name(r));
463 kill(getpid(), SIGTERM);
471 if (write(pipefd[1], &c, 1) == -1)
473 Log2(PCSC_LOG_ERROR,
"write: %s", strerror(errno));
479 for (
int i=0; i<driverSize; i++)
480 if (driverTracker[i].libraryPath)
481 if ((driverTracker[i].ifdCapabilities & IFD_GENERATE_HOTPLUG) == 0)
484 "Driver %s does not support IFD_GENERATE_HOTPLUG. Using active polling instead.",
485 driverTracker[i].bundleName);
486 if (HPForceReaderPolling < 1)
487 HPForceReaderPolling = 1;
491 if (HPForceReaderPolling)
494 "Polling forced every %d second(s)", HPForceReaderPolling);
500 while (!AraKiriHotPlug)
509 while (read(rescan_pipe[0], &dummy,
sizeof(dummy)) > 0)
513 Log1(PCSC_LOG_INFO,
"Reload serial configuration");
516 RFReCheckReaderConf();
518 Log1(PCSC_LOG_INFO,
"End reload serial configuration");
526 if (readerTracker[i].fullName != NULL)
527 HPCleanupHotPluggable(i);
530 for (
int i=0; i<driverSize; i++)
533 free(driverTracker[i].bundleName);
534 free(driverTracker[i].libraryPath);
535 free(driverTracker[i].readerName);
536 free(driverTracker[i].CFBundleName);
540 close(rescan_pipe[0]);
543 Log1(PCSC_LOG_INFO,
"Hotplug stopped");
548LONG HPSearchHotPluggables(
const char * hpDirPath)
552 AraKiriHotPlug =
false;
555 readerTracker[i].status = READER_ABSENT;
556 readerTracker[i].bus_device[0] =
'\0';
557 readerTracker[i].fullName = NULL;
560 if (HPReadBundleValues(hpDirPath) > 0)
566 if (pipe(pipefd) == -1)
568 Log2(PCSC_LOG_ERROR,
"pipe: %s", strerror(errno));
573 if (pipe(rescan_pipe) == -1)
575 Log2(PCSC_LOG_ERROR,
"pipe: %s", strerror(errno));
579 ThreadCreate(&usbNotifyThread, 0,
580 (PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, pipefd);
583 if (read(pipefd[0], &c, 1) == -1)
585 Log2(PCSC_LOG_ERROR,
"read: %s", strerror(errno));
597LONG HPStopHotPluggables(
void)
601 AraKiriHotPlug =
true;
602 HPReCheckSerialReaders();
604 if (rescan_pipe[1] >= 0)
606 close(rescan_pipe[1]);
610 pthread_join(usbNotifyThread, NULL);
615static LONG HPAddHotPluggable(
struct libusb_device *dev,
616 struct libusb_device_descriptor desc,
617 const char bus_device[],
618 const struct libusb_interface *idesc,
619 struct _driverTracker *driver,
620 struct _driverTracker *classdriver)
623 uint8_t iInterface = 0;
624 uint8_t iSerialNumber = 0;
627 Log2(PCSC_LOG_INFO,
"Adding USB device: %s", bus_device);
629 i = asprintf(&deviceName,
"usb:%04x/%04x:libusb-1.0:%s",
630 desc.idVendor, desc.idProduct, bus_device);
633 Log1(PCSC_LOG_ERROR,
"asprintf() failed");
637 pthread_mutex_lock(&usbNotifierMutex);
642 if (readerTracker[i].fullName == NULL)
649 "Not enough reader entries. Already found %d readers", i);
650 pthread_mutex_unlock(&usbNotifierMutex);
654 strncpy(readerTracker[i].bus_device, bus_device,
655 sizeof(readerTracker[i].bus_device));
656 readerTracker[i].bus_device[
sizeof(readerTracker[i].bus_device) - 1] =
'\0';
657 readerTracker[i].fullName = NULL;
659 if (Add_Interface_In_Name && idesc->num_altsetting > 0)
660 iInterface = idesc->altsetting[0].iInterface;
662 if (Add_Serial_In_Name)
663 iSerialNumber = desc.iSerialNumber;
665 if (iSerialNumber != 0 || iInterface != 0)
667 libusb_device_handle *device;
670 ret = libusb_open(dev, &device);
673 Log2(PCSC_LOG_ERROR,
"libusb_open failed: %s",
674 libusb_error_name(ret));
678 unsigned char interfaceName[MAX_READERNAME];
679 unsigned char serialNumber[MAX_READERNAME];
680 char fullname[MAX_READERNAME * 3];
682 int ret_interface = 0;
687 ret_interface = libusb_get_string_descriptor_ascii(device,
688 iInterface, interfaceName,
sizeof interfaceName);
689 if (ret_interface < 0)
692 "libusb_get_string_descriptor_ascii failed: %s",
693 libusb_error_name(ret_interface));
699 ret_serial = libusb_get_string_descriptor_ascii(device,
700 iSerialNumber, serialNumber,
sizeof serialNumber);
704 "libusb_get_string_descriptor_ascii failed: %s",
705 libusb_error_name(ret_serial));
709 libusb_close(device);
711 if (ret_interface > 0 && ret_serial > 0)
713 snprintf(fullname,
sizeof(fullname),
"%s [%s] (%s)",
714 driver->readerName, interfaceName, serialNumber);
718 if (ret_interface > 0)
720 snprintf(fullname,
sizeof(fullname),
"%s [%s]",
721 driver->readerName, interfaceName);
727 snprintf(fullname,
sizeof(fullname),
"%s (%s)",
728 driver->readerName, serialNumber);
733 if (fullname[0] !=
'\0')
734 readerTracker[i].fullName = strdup(fullname);
738 if (readerTracker[i].fullName == NULL)
739 readerTracker[i].fullName = strdup(driver->readerName);
742 ret = RFAddReader(readerTracker[i].fullName, PCSCLITE_HP_BASE_PORT + i,
743 driver->libraryPath, deviceName);
745 readerTracker[i].status = READER_PRESENT;
748 Log2(PCSC_LOG_ERROR,
"Failed adding USB device: %s",
751 if (classdriver && driver != classdriver)
754 ret = RFAddReader(readerTracker[i].fullName,
755 PCSCLITE_HP_BASE_PORT + i,
756 classdriver->libraryPath, deviceName);
759 Log2(PCSC_LOG_ERROR,
"Failed adding USB device: %s",
761 readerTracker[i].status = READER_FAILED;
765 readerTracker[i].status = READER_FAILED;
768 if (READER_FAILED == readerTracker[i].status)
769 (void)CheckForOpenCT();
771 pthread_mutex_unlock(&usbNotifierMutex);
778static LONG HPRemoveHotPluggable(
int reader_index)
780 pthread_mutex_lock(&usbNotifierMutex);
782 Log3(PCSC_LOG_INFO,
"Removing USB device[%d]: %s", reader_index,
783 readerTracker[reader_index].bus_device);
785 RFRemoveReader(readerTracker[reader_index].fullName,
786 PCSCLITE_HP_BASE_PORT + reader_index, REMOVE_READER_FLAG_REMOVED);
787 HPCleanupHotPluggable(reader_index);
789 pthread_mutex_unlock(&usbNotifierMutex);
794static void HPCleanupHotPluggable(
int reader_index)
796 free(readerTracker[reader_index].fullName);
797 readerTracker[reader_index].status = READER_ABSENT;
798 readerTracker[reader_index].bus_device[0] =
'\0';
799 readerTracker[reader_index].fullName = NULL;
805ULONG HPRegisterForHotplugEvents(
const char * hpDirPath)
809 (void)pthread_mutex_init(&usbNotifierMutex, NULL);
813void HPReCheckSerialReaders(
void)
816 if (rescan_pipe[1] >= 0)
819 if (write(rescan_pipe[1], &dummy,
sizeof(dummy)) == -1)
820 Log2(PCSC_LOG_ERROR,
"write: %s", strerror(errno));
#define SCARD_E_UNKNOWN_READER
The specified reader name is not recognized.
#define SCARD_S_SUCCESS
No error was encountered.
This provides a search API for hot pluggble devices.
Reads lexical config files and updates database.
#define PCSCLITE_MAX_READERS_CONTEXTS
Maximum readers context (a slot is count as a reader)
This keeps track of a list of currently available reader structures.
This handles abstract system level calls.
int SYS_Sleep(int)
Makes the current process sleep for some seconds.
This defines some structures and #defines to be used over the transport layer.
This keeps a list of Windows(R) types.