39#if defined(HAVE_LIBUDEV) && defined(USE_USB)
59#ifndef TEMP_FAILURE_RETRY
60#define TEMP_FAILURE_RETRY(expression) \
62 ({ long int __result; \
63 do __result = (long int) (expression); \
64 while (__result == -1L && errno == EINTR); \
70extern bool Add_Interface_In_Name;
71extern bool Add_Serial_In_Name;
73static pthread_t usbNotifyThread;
74static int driverSize = -1;
75static struct udev *Udev;
76static struct udev_monitor *Udev_monitor;
82static struct _driverTracker
85 unsigned int productID;
91} *driverTracker = NULL;
92#define DRIVER_TRACKER_SIZE_STEP 10
96#define DRIVER_TRACKER_INITIAL_SIZE 200
101static struct _readerTracker
109static LONG HPReadBundleValues(
const char * hpDirPath)
113 struct dirent *currFP = NULL;
114 char fullPath[FILENAME_MAX];
115 char fullLibPath[FILENAME_MAX];
118 hpDir = opendir(hpDirPath);
122 Log2(PCSC_LOG_ERROR,
"Cannot open PC/SC drivers directory: %s", hpDirPath);
123 Log1(PCSC_LOG_ERROR,
"Disabling USB support for pcscd.");
128 driverSize = DRIVER_TRACKER_INITIAL_SIZE;
129 driverTracker = calloc(driverSize,
sizeof(*driverTracker));
130 if (NULL == driverTracker)
132 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
133 (void)closedir(hpDir);
137#define GET_KEY(key, values) \
138 rv = LTPBundleFindValueWithKey(&plist, key, values); \
141 Log2(PCSC_LOG_ERROR, "Value/Key not defined for " key " in %s", \
146 while ((currFP = readdir(hpDir)) != 0)
148 if (strstr(currFP->d_name,
".bundle") != 0)
152 list_t *manuIDs, *productIDs, *readerNames;
160 (void)snprintf(fullPath,
sizeof(fullPath),
"%s/%s/Contents/Info.plist",
161 hpDirPath, currFP->d_name);
162 fullPath[
sizeof(fullPath) - 1] =
'\0';
164 rv = bundleParse(fullPath, &plist);
169 GET_KEY(PCSCLITE_HP_LIBRKEY_NAME, &values)
170 libraryPath = list_get_at(values, 0);
171 (void)snprintf(fullLibPath,
sizeof(fullLibPath),
172 "%s/%s/Contents/%s/%s",
173 hpDirPath, currFP->d_name, PCSC_ARCH,
175 fullLibPath[
sizeof(fullLibPath) - 1] =
'\0';
177 GET_KEY(PCSCLITE_HP_MANUKEY_NAME, &manuIDs)
178 GET_KEY(PCSCLITE_HP_PRODKEY_NAME, &productIDs)
179 GET_KEY(PCSCLITE_HP_NAMEKEY_NAME, &readerNames)
181 if ((list_size(manuIDs) != list_size(productIDs))
182 || (list_size(manuIDs) != list_size(readerNames)))
184 Log2(PCSC_LOG_CRITICAL,
"Error parsing %s", fullPath);
185 (void)closedir(hpDir);
190 rv = LTPBundleFindValueWithKey(&plist, PCSCLITE_HP_CFBUNDLE_NAME,
195 CFBundleName = list_get_at(values, 0);
198 for (alias=0; alias<list_size(manuIDs); alias++)
203 value = list_get_at(manuIDs, alias);
204 driverTracker[listCount].manuID = strtol(value, NULL, 16);
206 value = list_get_at(productIDs, alias);
207 driverTracker[listCount].productID = strtol(value, NULL, 16);
209 driverTracker[listCount].readerName = strdup(list_get_at(readerNames, alias));
212 driverTracker[listCount].bundleName = strdup(currFP->d_name);
213 driverTracker[listCount].libraryPath = strdup(fullLibPath);
214 driverTracker[listCount].CFBundleName = CFBundleName ? strdup(CFBundleName) : NULL;
217 Log2(PCSC_LOG_INFO,
"Found driver for: %s",
218 driverTracker[listCount].readerName);
221 if (listCount >= driverSize)
226 driverSize += DRIVER_TRACKER_SIZE_STEP;
229 "Increase driverTracker to %d entries", driverSize);
232 void* tmp = realloc(driverTracker,
233 driverSize *
sizeof(*driverTracker));
238 Log1(PCSC_LOG_CRITICAL,
"Not enough memory");
240 (void)closedir(hpDir);
246 for (i=driverSize-DRIVER_TRACKER_SIZE_STEP; i<driverSize; i++)
248 driverTracker[i].manuID = 0;
249 driverTracker[i].productID = 0;
250 driverTracker[i].bundleName = NULL;
251 driverTracker[i].libraryPath = NULL;
252 driverTracker[i].readerName = NULL;
253 driverTracker[i].CFBundleName = NULL;
257 bundleRelease(&plist);
261 driverSize = listCount;
262 (void)closedir(hpDir);
265 Log2(PCSC_LOG_INFO,
"Found drivers for %d readers", listCount);
272 static struct _driverTracker *get_driver(
struct udev_device *dev,
273 const char *devpath,
struct _driverTracker **classdriver)
276 unsigned int idVendor, idProduct;
277 static struct _driverTracker *driver;
280 str = udev_device_get_sysattr_value(dev,
"idVendor");
283 Log1(PCSC_LOG_ERROR,
"udev_device_get_sysattr_value() failed");
286 idVendor = strtol(str, NULL, 16);
288 str = udev_device_get_sysattr_value(dev,
"idProduct");
291 Log1(PCSC_LOG_ERROR,
"udev_device_get_sysattr_value() failed");
294 idProduct = strtol(str, NULL, 16);
300 "Looking for a driver for VID: 0x%04X, PID: 0x%04X, path: %s",
301 idVendor, idProduct, devpath);
306 for (i=0; i<driverSize; i++)
308 if (driverTracker[i].libraryPath != NULL &&
309 idVendor == driverTracker[i].manuID &&
310 idProduct == driverTracker[i].productID)
312 if ((driverTracker[i].CFBundleName != NULL)
313 && (0 == strcmp(driverTracker[i].CFBundleName,
"CCIDCLASSDRIVER")))
314 *classdriver = &driverTracker[i];
317 driver = &driverTracker[i];
330static void HPRemoveDevice(
struct udev_device *dev)
335 sysname = udev_device_get_sysname(dev);
338 Log1(PCSC_LOG_ERROR,
"udev_device_get_sysname() failed");
344 if (readerTracker[i].fullName && !strcmp(sysname, readerTracker[i].sysname))
346 Log4(PCSC_LOG_INFO,
"Removing USB device[%d]: %s at %s", i,
347 readerTracker[i].fullName, readerTracker[i].devpath);
349 RFRemoveReader(readerTracker[i].fullName, PCSCLITE_HP_BASE_PORT + i, REMOVE_READER_FLAG_REMOVED);
351 free(readerTracker[i].devpath);
352 readerTracker[i].devpath = NULL;
353 free(readerTracker[i].fullName);
354 readerTracker[i].fullName = NULL;
355 free(readerTracker[i].sysname);
356 readerTracker[i].sysname = NULL;
363static void HPAddDevice(
struct udev_device *dev)
366 char *deviceName = NULL;
367 char *fullname = NULL;
368 struct _driverTracker *driver, *classdriver;
369 const char *sSerialNumber = NULL, *sInterfaceName = NULL;
370 const char *sInterfaceNumber;
372 int bInterfaceNumber;
374 struct udev_device *parent;
382 parent = udev_device_get_parent_with_subsystem_devtype(dev,
"usb",
387 devpath = udev_device_get_devnode(parent);
391 Log1(PCSC_LOG_ERROR,
"udev_device_get_devnode() failed");
395 driver = get_driver(parent, devpath, &classdriver);
400 Log2(PCSC_LOG_DEBUG,
"%s is not a supported smart card reader",
406 sysname = udev_device_get_sysname(dev);
409 Log1(PCSC_LOG_ERROR,
"udev_device_get_sysname() failed");
416 if (readerTracker[index].fullName && !strcmp(sysname, readerTracker[index].sysname))
420 Log2(PCSC_LOG_INFO,
"Adding USB device: %s", driver->readerName);
422 sInterfaceNumber = udev_device_get_sysattr_value(dev,
"bInterfaceNumber");
423 if (sInterfaceNumber)
424 bInterfaceNumber = atoi(sInterfaceNumber);
426 bInterfaceNumber = 0;
428 a = asprintf(&deviceName,
"usb:%04x/%04x:libudev:%d:%s",
429 driver->manuID, driver->productID, bInterfaceNumber, devpath);
432 Log1(PCSC_LOG_ERROR,
"asprintf() failed");
439 if (NULL == readerTracker[index].fullName)
446 "Not enough reader entries. Already found %d readers", index);
450 if (Add_Interface_In_Name)
451 sInterfaceName = udev_device_get_sysattr_value(dev,
"interface");
453 if (Add_Serial_In_Name)
454 sSerialNumber = udev_device_get_sysattr_value(parent,
"serial");
457 fullname = strdup(driver->readerName);
464 char *tmpInterfaceName = strdup(sInterfaceName);
467 for (
size_t i=0; i<strlen(tmpInterfaceName); i++)
469 if (! isascii(tmpInterfaceName[i]))
470 tmpInterfaceName[i] =
'.';
474 a = asprintf(&result,
"%s [%s]", fullname, tmpInterfaceName);
477 Log1(PCSC_LOG_ERROR,
"asprintf() failed");
478 free(tmpInterfaceName);
483 free(tmpInterfaceName);
492 if (!sInterfaceName || NULL == strstr(sInterfaceName, sSerialNumber))
497 a = asprintf(&result,
"%s (%s)", fullname, sSerialNumber);
500 Log1(PCSC_LOG_ERROR,
"asprintf() failed");
509 readerTracker[index].fullName = strdup(fullname);
510 readerTracker[index].devpath = strdup(devpath);
511 readerTracker[index].sysname = strdup(sysname);
513 ret = RFAddReader(fullname, PCSCLITE_HP_BASE_PORT + index,
514 driver->libraryPath, deviceName);
517 Log2(PCSC_LOG_ERROR,
"Failed adding USB device: %s",
520 if (classdriver && driver != classdriver)
523 ret = RFAddReader(fullname, PCSCLITE_HP_BASE_PORT + index,
524 classdriver->libraryPath, deviceName);
527 Log2(PCSC_LOG_ERROR,
"Failed adding USB device: %s",
529 (void)CheckForOpenCT();
534 (void)CheckForOpenCT();
541 free(readerTracker[index].devpath);
542 readerTracker[index].devpath = NULL;
543 free(readerTracker[index].fullName);
544 readerTracker[index].fullName = NULL;
545 free(readerTracker[index].sysname);
546 readerTracker[index].sysname = NULL;
555static void HPScanUSB(
struct udev *udev)
557 struct udev_enumerate *enumerate;
558 struct udev_list_entry *devices, *dev_list_entry;
561 enumerate = udev_enumerate_new(udev);
562 udev_enumerate_add_match_subsystem(enumerate,
"usb");
563 udev_enumerate_scan_devices(enumerate);
564 devices = udev_enumerate_get_list_entry(enumerate);
567 udev_list_entry_foreach(dev_list_entry, devices)
569 struct udev_device *dev;
574 devpath = udev_list_entry_get_name(dev_list_entry);
575 dev = udev_device_new_from_syspath(udev, devpath);
578 Log2(PCSC_LOG_DEBUG,
"Found matching USB device: %s", devpath);
583 udev_device_unref(dev);
587 udev_enumerate_unref(enumerate);
591static void * HPEstablishUSBNotifications(
void *arg)
593 struct udev_monitor *udev_monitor = arg;
598 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
599 pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
602 fd = udev_monitor_get_fd(udev_monitor);
605 Log2(PCSC_LOG_ERROR,
"udev_monitor_get_fd() error: %d", fd);
614 struct udev_device *dev;
619 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
622 r = TEMP_FAILURE_RETRY(poll(&pfd, 1, -1));
625 Log2(PCSC_LOG_ERROR,
"select(): %s", strerror(errno));
629 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
631 dev = udev_monitor_receive_device(udev_monitor);
634 const char *action = udev_device_get_action(dev);
638 if (!strcmp(
"remove", action))
640 Log1(PCSC_LOG_INFO,
"USB Device removed");
644 if (!strcmp(
"add", action))
646 Log1(PCSC_LOG_INFO,
"USB Device add");
652 udev_device_unref(dev);
663LONG HPSearchHotPluggables(
const char * hpDirPath)
669 readerTracker[i].devpath = NULL;
670 readerTracker[i].fullName = NULL;
671 readerTracker[i].sysname = NULL;
674 return HPReadBundleValues(hpDirPath);
681LONG HPStopHotPluggables(
void)
691 pthread_cancel(usbNotifyThread);
692 pthread_join(usbNotifyThread, NULL);
694 for (i=0; i<driverSize; i++)
697 free(driverTracker[i].bundleName);
698 free(driverTracker[i].libraryPath);
699 free(driverTracker[i].readerName);
700 free(driverTracker[i].CFBundleName);
705 udev_monitor_unref(Udev_monitor);
710 Log1(PCSC_LOG_INFO,
"Hotplug stopped");
718ULONG HPRegisterForHotplugEvents(
const char * hpDirPath)
725 Log2(PCSC_LOG_INFO,
"No bundle files in pcsc drivers directory: %s",
727 Log1(PCSC_LOG_INFO,
"Disabling USB support for pcscd");
735 Log1(PCSC_LOG_ERROR,
"udev_new() failed");
739 Udev_monitor = udev_monitor_new_from_netlink(Udev,
"udev");
740 if (NULL == Udev_monitor)
742 Log1(PCSC_LOG_ERROR,
"udev_monitor_new_from_netlink() error");
747 r = udev_monitor_filter_add_match_subsystem_devtype(Udev_monitor,
"usb",
751 Log2(PCSC_LOG_ERROR,
"udev_monitor_filter_add_match_subsystem_devtype() error: %d\n", r);
755 r = udev_monitor_enable_receiving(Udev_monitor);
758 Log2(PCSC_LOG_ERROR,
"udev_monitor_enable_receiving() error: %d\n", r);
765 if (ThreadCreate(&usbNotifyThread, 0,
766 (PCSCLITE_THREAD_FUNCTION( )) HPEstablishUSBNotifications, Udev_monitor))
768 Log1(PCSC_LOG_ERROR,
"ThreadCreate() failed");
776void HPReCheckSerialReaders(
void)
780 Log0(PCSC_LOG_ERROR);
#define SCARD_F_INTERNAL_ERROR
An internal consistency check failed.
#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.