73#define BUS_DEVICE_STRSIZE 10+1+10+1+10+1
75#define READER_ABSENT 0
76#define READER_PRESENT 1
77#define READER_FAILED 2
82extern char Add_Interface_In_Name;
83extern char Add_Serial_In_Name;
88pthread_mutex_t usbNotifierMutex;
90static pthread_t usbNotifyThread;
91static int driverSize = -1;
92static char AraKiriHotPlug = FALSE;
93static int rescan_pipe[] = { -1, -1 };
94extern int HPForceReaderPolling;
97#define IFD_GENERATE_HOTPLUG 1
102static struct _driverTracker
105 unsigned int productID;
112} *driverTracker = NULL;
113#define DRIVER_TRACKER_SIZE_STEP 8
118static struct _readerTracker
121 char bus_device[BUS_DEVICE_STRSIZE];
125static LONG HPAddHotPluggable(
struct libusb_device *dev,
126 struct libusb_device_descriptor desc,
127 const char bus_device[],
128 const struct libusb_interface *idesc,
129 struct _driverTracker *driver,
130 struct _driverTracker *classdriver);
131static LONG HPRemoveHotPluggable(
int reader_index);
133static LONG HPReadBundleValues(
void)
137 struct dirent *currFP = NULL;
138 char fullPath[FILENAME_MAX];
139 char fullLibPath[FILENAME_MAX];
142 hpDir = opendir(PCSCLITE_HP_DROPDIR);
146 Log1(PCSC_LOG_ERROR,
"Cannot open PC/SC drivers directory: " PCSCLITE_HP_DROPDIR);
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 PCSCLITE_HP_DROPDIR, 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 PCSCLITE_HP_DROPDIR, 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 = strdup(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 = CFBundleName;
238 Log2(PCSC_LOG_INFO,
"Found driver for: %s",
239 driverTracker[listCount].readerName);
242 if (listCount >= driverSize)
247 driverSize += DRIVER_TRACKER_SIZE_STEP;
250 "Increase driverTracker to %d entries", driverSize);
252 void* tmp = realloc(driverTracker,
253 driverSize *
sizeof(*driverTracker));
257 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
265 for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
267 driverTracker[i].manuID = 0;
268 driverTracker[i].productID = 0;
269 driverTracker[i].bundleName = NULL;
270 driverTracker[i].libraryPath = NULL;
271 driverTracker[i].readerName = NULL;
272 driverTracker[i].ifdCapabilities = 0;
273 driverTracker[i].CFBundleName = NULL;
277 bundleRelease(&plist);
281 driverSize = listCount;
286 Log1(PCSC_LOG_INFO,
"No bundle files in pcsc drivers directory: " PCSCLITE_HP_DROPDIR);
287 Log1(PCSC_LOG_INFO,
"Disabling USB support for pcscd");
291 Log2(PCSC_LOG_INFO,
"Found drivers for %d readers", listCount);
297static struct _driverTracker *get_driver(
unsigned int idVendor,
298 unsigned int idProduct,
struct _driverTracker **classdriver)
301 static struct _driverTracker *driver;
305 "Looking for a driver for VID: 0x%04X, PID: 0x%04X",
306 idVendor, idProduct);
312 for (i=0; i<driverSize; i++)
314 if (driverTracker[i].libraryPath != NULL &&
315 idVendor == driverTracker[i].manuID &&
316 idProduct == driverTracker[i].productID)
318 if ((driverTracker[i].CFBundleName != NULL)
319 && (0 == strcmp(driverTracker[i].CFBundleName,
"CCIDCLASSDRIVER")))
320 *classdriver = &driverTracker[i];
323 driver = &driverTracker[i];
335static void HPRescanUsbBus(
void)
338 char bus_device[BUS_DEVICE_STRSIZE];
339 libusb_device **devs, *dev;
344 readerTracker[i].status = READER_ABSENT;
346 cnt = libusb_get_device_list(ctx, &devs);
349 Log2(PCSC_LOG_CRITICAL,
"libusb_get_device_list() failed: %s",
350 libusb_error_name(cnt));
356 while ((dev = devs[cnt++]) != NULL)
358 struct libusb_device_descriptor desc;
359 struct libusb_config_descriptor *config_desc;
360 uint8_t bus_number = libusb_get_bus_number(dev);
361 uint8_t device_address = libusb_get_device_address(dev);
362 struct _driverTracker *driver, *classdriver;
365 int r = libusb_get_device_descriptor(dev, &desc);
369 "failed to get device descriptor for %d/%d: %s",
370 bus_number, device_address, libusb_error_name(r));
374 r = libusb_get_active_config_descriptor(dev, &config_desc);
377 Log4(PCSC_LOG_ERROR,
"failed to get device config for %d/%d: %s",
378 bus_number, device_address, libusb_error_name(r));
382 driver = get_driver(desc.idVendor, desc.idProduct, &classdriver);
387 Log3(PCSC_LOG_DEBUG,
"%d/%d is not a supported smart card reader",
388 bus_number, device_address);
390 libusb_free_config_descriptor(config_desc);
395 Log3(PCSC_LOG_DEBUG,
"Found matching USB device: %d:%d",
396 bus_number, device_address);
399 for (interface = 0;
interface < config_desc->bNumInterfaces;
405 snprintf(bus_device, BUS_DEVICE_STRSIZE,
"%d:%d:%d",
406 bus_number, device_address, interface);
407 bus_device[BUS_DEVICE_STRSIZE - 1] =
'\0';
413 if (strncmp(readerTracker[j].bus_device,
414 bus_device, BUS_DEVICE_STRSIZE) == 0)
417 readerTracker[j].status = READER_PRESENT;
420 Log2(PCSC_LOG_DEBUG,
"Refresh USB device: %s",
429 HPAddHotPluggable(dev, desc, bus_device,
430 &config_desc->interface[interface], driver, classdriver);
433 libusb_free_config_descriptor(config_desc);
441 if ((readerTracker[i].status == READER_ABSENT) &&
442 (readerTracker[i].fullName != NULL))
443 HPRemoveHotPluggable(i);
450 for (i=0; i<driverSize; i++)
453 free(driverTracker[i].bundleName);
454 free(driverTracker[i].libraryPath);
455 free(driverTracker[i].readerName);
456 free(driverTracker[i].CFBundleName);
460 Log1(PCSC_LOG_INFO,
"Hotplug stopped");
461 pthread_exit(&retval);
465 libusb_free_device_list(devs, 1);
468static void * HPEstablishUSBNotifications(
int pipefd[2])
474 r = libusb_init(ctx);
477 Log2(PCSC_LOG_CRITICAL,
"libusb_init failed: %s", libusb_error_name(r));
479 kill(getpid(), SIGTERM);
487 if (write(pipefd[1], &c, 1) == -1)
489 Log2(PCSC_LOG_ERROR,
"write: %s", strerror(errno));
495 for (i=0; i<driverSize; i++)
496 if (driverTracker[i].libraryPath)
497 if ((driverTracker[i].ifdCapabilities & IFD_GENERATE_HOTPLUG) == 0)
500 "Driver %s does not support IFD_GENERATE_HOTPLUG. Using active polling instead.",
501 driverTracker[i].bundleName);
502 if (HPForceReaderPolling < 1)
503 HPForceReaderPolling = 1;
507 if (HPForceReaderPolling)
510 "Polling forced every %d second(s)", HPForceReaderPolling);
516 while (!AraKiriHotPlug)
527 if (pipe(rescan_pipe) == -1)
529 Log2(PCSC_LOG_ERROR,
"pipe: %s", strerror(errno));
532 while (read(rescan_pipe[0], &dummy,
sizeof(dummy)) > 0)
534 Log1(PCSC_LOG_INFO,
"Reload serial configuration");
537 RFReCheckReaderConf();
539 Log1(PCSC_LOG_INFO,
"End reload serial configuration");
541 close(rescan_pipe[0]);
548LONG HPSearchHotPluggables(
void)
554 readerTracker[i].status = READER_ABSENT;
555 readerTracker[i].bus_device[0] =
'\0';
556 readerTracker[i].fullName = NULL;
559 if (HPReadBundleValues() > 0)
564 if (pipe(pipefd) == -1)
566 Log2(PCSC_LOG_ERROR,
"pipe: %s", strerror(errno));
570 ThreadCreate(&usbNotifyThread, THREAD_ATTR_DETACHED,
571 (PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, pipefd);
574 if (read(pipefd[0], &c, 1) == -1)
576 Log2(PCSC_LOG_ERROR,
"read: %s", strerror(errno));
588LONG HPStopHotPluggables(
void)
590 AraKiriHotPlug = TRUE;
591 if (rescan_pipe[1] >= 0)
593 close(rescan_pipe[1]);
600static LONG HPAddHotPluggable(
struct libusb_device *dev,
601 struct libusb_device_descriptor desc,
602 const char bus_device[],
603 const struct libusb_interface *idesc,
604 struct _driverTracker *driver,
605 struct _driverTracker *classdriver)
608 uint8_t iInterface = 0;
609 uint8_t iSerialNumber = 0;
612 Log2(PCSC_LOG_INFO,
"Adding USB device: %s", bus_device);
614 i = asprintf(&deviceName,
"usb:%04x/%04x:libusb-1.0:%s",
615 desc.idVendor, desc.idProduct, bus_device);
618 Log1(PCSC_LOG_ERROR,
"asprintf() failed");
622 pthread_mutex_lock(&usbNotifierMutex);
627 if (readerTracker[i].fullName == NULL)
634 "Not enough reader entries. Already found %d readers", i);
635 pthread_mutex_unlock(&usbNotifierMutex);
639 strncpy(readerTracker[i].bus_device, bus_device,
640 sizeof(readerTracker[i].bus_device));
641 readerTracker[i].bus_device[
sizeof(readerTracker[i].bus_device) - 1] =
'\0';
642 readerTracker[i].fullName = NULL;
644 if (Add_Interface_In_Name && idesc->num_altsetting > 0)
645 iInterface = idesc->altsetting[0].iInterface;
647 if (Add_Serial_In_Name)
648 iSerialNumber = desc.iSerialNumber;
650 if (iSerialNumber != 0 || iInterface != 0)
652 libusb_device_handle *device;
655 ret = libusb_open(dev, &device);
658 Log2(PCSC_LOG_ERROR,
"libusb_open failed: %s",
659 libusb_error_name(ret));
663 unsigned char interfaceName[MAX_READERNAME];
664 unsigned char serialNumber[MAX_READERNAME];
665 char fullname[MAX_READERNAME * 3];
667 int ret_interface = 0;
672 ret_interface = libusb_get_string_descriptor_ascii(device,
673 iInterface, interfaceName,
sizeof interfaceName);
674 if (ret_interface < 0)
677 "libusb_get_string_descriptor_ascii failed: %s",
678 libusb_error_name(ret_interface));
684 ret_serial = libusb_get_string_descriptor_ascii(device,
685 iSerialNumber, serialNumber,
sizeof serialNumber);
689 "libusb_get_string_descriptor_ascii failed: %s",
690 libusb_error_name(ret_serial));
694 libusb_close(device);
696 if (ret_interface > 0 && ret_serial > 0)
698 snprintf(fullname,
sizeof(fullname),
"%s [%s] (%s)",
699 driver->readerName, interfaceName, serialNumber);
703 if (ret_interface > 0)
705 snprintf(fullname,
sizeof(fullname),
"%s [%s]",
706 driver->readerName, interfaceName);
712 snprintf(fullname,
sizeof(fullname),
"%s (%s)",
713 driver->readerName, serialNumber);
718 if (fullname[0] !=
'\0')
719 readerTracker[i].fullName = strdup(fullname);
723 if (readerTracker[i].fullName == NULL)
724 readerTracker[i].fullName = strdup(driver->readerName);
727 ret = RFAddReader(readerTracker[i].fullName, PCSCLITE_HP_BASE_PORT + i,
728 driver->libraryPath, deviceName);
730 readerTracker[i].status = READER_PRESENT;
733 Log2(PCSC_LOG_ERROR,
"Failed adding USB device: %s",
736 if (classdriver && driver != classdriver)
739 ret = RFAddReader(readerTracker[i].fullName,
740 PCSCLITE_HP_BASE_PORT + i,
741 classdriver->libraryPath, deviceName);
744 Log2(PCSC_LOG_ERROR,
"Failed adding USB device: %s",
746 readerTracker[i].status = READER_FAILED;
750 readerTracker[i].status = READER_FAILED;
753 if (READER_FAILED == readerTracker[i].status)
754 (void)CheckForOpenCT();
756 pthread_mutex_unlock(&usbNotifierMutex);
763static LONG HPRemoveHotPluggable(
int reader_index)
765 pthread_mutex_lock(&usbNotifierMutex);
767 Log3(PCSC_LOG_INFO,
"Removing USB device[%d]: %s", reader_index,
768 readerTracker[reader_index].bus_device);
770 RFRemoveReader(readerTracker[reader_index].fullName,
771 PCSCLITE_HP_BASE_PORT + reader_index, REMOVE_READER_FLAG_REMOVED);
772 free(readerTracker[reader_index].fullName);
773 readerTracker[reader_index].status = READER_ABSENT;
774 readerTracker[reader_index].bus_device[0] =
'\0';
775 readerTracker[reader_index].fullName = NULL;
777 pthread_mutex_unlock(&usbNotifierMutex);
785ULONG HPRegisterForHotplugEvents(
void)
787 (void)pthread_mutex_init(&usbNotifierMutex, NULL);
791void HPReCheckSerialReaders(
void)
794 if (rescan_pipe[1] >= 0)
797 if (write(rescan_pipe[1], &dummy,
sizeof(dummy)) == -1)
798 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.
This keeps a list of defines for pcsc-lite.
#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.