From 2a71c8fa82d206e34bc63e1852331912a560f411 Mon Sep 17 00:00:00 2001 From: Christian Prochaska Date: Tue, 5 Mar 2019 19:35:46 +0100 Subject: [PATCH] pcsc-lite: read vendor id and product id from USB device Fixes #3211 --- repos/libports/lib/mk/pcsc-lite.mk | 2 +- repos/libports/run/smartcard.run | 3 -- repos/libports/src/lib/pcsc-lite/README | 16 +++------ repos/libports/src/lib/pcsc-lite/init.cc | 44 +++++++++++++----------- 4 files changed, 28 insertions(+), 37 deletions(-) diff --git a/repos/libports/lib/mk/pcsc-lite.mk b/repos/libports/lib/mk/pcsc-lite.mk index 441154afa..d119cfdb8 100644 --- a/repos/libports/lib/mk/pcsc-lite.mk +++ b/repos/libports/lib/mk/pcsc-lite.mk @@ -2,7 +2,7 @@ PCSC_LITE_DIR := $(call select_from_ports,pcsc-lite)/src/lib/pcsc-lite include $(call select_from_repositories,lib/import/import-pcsc-lite.mk) -LIBS += ccid libc +LIBS += ccid libc libusb # find 'config.h' INC_DIR += $(REP_DIR)/src/lib/pcsc-lite diff --git a/repos/libports/run/smartcard.run b/repos/libports/run/smartcard.run index 3050d1824..520f6720d 100644 --- a/repos/libports/run/smartcard.run +++ b/repos/libports/run/smartcard.run @@ -112,9 +112,6 @@ append config { - - - diff --git a/repos/libports/src/lib/pcsc-lite/README b/repos/libports/src/lib/pcsc-lite/README index 192f43826..c67823651 100644 --- a/repos/libports/src/lib/pcsc-lite/README +++ b/repos/libports/src/lib/pcsc-lite/README @@ -1,12 +1,4 @@ -The USB card reader to be used by an application must be configured with its -vendor id and product id in a file '/config.pcsc-lite': - - - - - - - - - - +The library uses the first (and currently the only) USB device reported by +libusb as smartcard reader. The actual selection of the USB device to use is +done by the USB driver according to its configured policy for the 'Usb' session +with the label 'usb_device', which is used by libusb to access the device. diff --git a/repos/libports/src/lib/pcsc-lite/init.cc b/repos/libports/src/lib/pcsc-lite/init.cc index f1d69fabc..7d0c1610d 100644 --- a/repos/libports/src/lib/pcsc-lite/init.cc +++ b/repos/libports/src/lib/pcsc-lite/init.cc @@ -21,6 +21,9 @@ #include #include +/* libusb includes */ +#include + /* pcsc-lite includes */ extern "C" { #include @@ -42,39 +45,38 @@ struct Pcsc_lite_initializer (void)DebugLogSetCategory(DEBUG_CATEGORY_APDU); } - unsigned int vendor_id = 0x0000; - unsigned int product_id = 0x0000; + /* + * Find out vendor id and product id of the connected USB device + * and add it as reader. + */ - int fd = open("/config.pcsc-lite", O_RDONLY); + libusb_context *ctx = nullptr; - if (fd < 0) { - Genode::error("Could not open 'config.pcsc-lite'"); - exit(1); - } - - char config[128]; - if (read(fd, config, sizeof(config)) < 0) { - Genode::error("Could not read 'config.pcsc-lite'"); + libusb_init(&ctx); + + libusb_device **devs = nullptr; + + if (libusb_get_device_list(ctx, &devs) < 1) { + Genode::error("Could not find a USB device."); exit(1); } - try { + struct libusb_device_descriptor device_descriptor; - Genode::Xml_node config_node(config); - - vendor_id = config_node.attribute_value("vendor_id", 0U); - product_id = config_node.attribute_value("product_id", 0U); - - } catch (...) { - Genode::error("Error parsing 'config.pcsc-lite'"); + if (libusb_get_device_descriptor(devs[0], &device_descriptor) < 0) { + Genode::error("Could not read the device descriptor of the " + "USB device."); exit(1); } - close(fd); + libusb_free_device_list(devs, 1); + + libusb_exit(ctx); char device[14]; - snprintf(device, sizeof(device), "usb:%04x/%04x", vendor_id, product_id); + snprintf(device, sizeof(device), "usb:%04x/%04x", + device_descriptor.idVendor, device_descriptor.idProduct); RFAllocateReaderSpace(0); (void)RFAddReader("CCID", 0, "/", device);