nfc-tools / libnfc

Platform independent Near Field Communication (NFC) library
http://nfc-tools.org
GNU Lesser General Public License v3.0
1.72k stars 442 forks source link

Unable to set alternate setting on USB interface (Numerical result out of range) #542

Open ErnyTech opened 5 years ago

ErnyTech commented 5 years ago

Hello,

I get the following message using my ACR122U with ArchLinux

sudo LIBNFC_LOG_LEVEL=3 nfc-list -v
info    libnfc.config   Unable to open file: /usr/local/etc/nfc/libnfc.conf
debug   libnfc.config   Unable to open directory: /usr/local/etc/nfc/devices.d
debug   libnfc.general  log_level is set to 3
debug   libnfc.general  allow_autoscan is set to true
debug   libnfc.general  allow_intrusive_scan is set to false
debug   libnfc.general  0 device(s) defined by user
nfc-list uses libnfc libnfc-1.7.1
debug   libnfc.driver.acr122_usb        device found: Bus 002 Device 015 Name ACS ACR122
debug   libnfc.general  1 device(s) found using acr122_usb driver
debug   libnfc.driver.acr122_usb        3 element(s) have been decoded from "acr122_usb:002:015"
error   libnfc.driver.acr122_usb        Unable to set alternate setting on USB interface (Numerical result out of range)
debug   libnfc.general  Unable to open "acr122_usb:002:015".
nfc-list: ERROR: Unable to open NFC device: acr122_usb:002:015

Does anyone know what to do to solve?

melchips commented 5 years ago

I have noticed the same behavior on NixOs or Kali.

The returned code seems to be -34 on my system (ERANGE, from the libusb_to_errno function of libusb-compat-0.1).

By calling usb_set_debug before the problematic usb_set_altinterface call, I get the following log from libusb:

libusb: error [op_set_interface] setintf failed error -1 errno 110

Error code 110, seems to be ETIMEDOUT (Connection timed out).

enabling libusb error reporting

Diff used:

diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c
index 8a16920..796d465 100644
--- a/libnfc/drivers/acr122_usb.c
+++ b/libnfc/drivers/acr122_usb.c
@@ -426,9 +426,10 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
         goto free_mem;
       }

+      usb_set_debug(3);
       res = usb_set_altinterface(data.pudh, 0);
       if (res < 0) {
-        log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", _usb_strerror(res));
+        log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s [%d])", _usb_strerror(res), res);
         usb_close(data.pudh);
         // we failed to use the specified device
         goto free_mem;

Full log:

# LIBNFC_LOG_LEVEL=3 LIBUSB_DEBUG=3 nfc-list -v
info    libnfc.config   Unable to open file: /nix/store/yn7x38apc7rc3zpacj9r050jjs4y34wg-libnfc-1.7.1/etc/nfc/libnfc.conf
debug   libnfc.config   Unable to open directory: /nix/store/yn7x38apc7rc3zpacj9r050jjs4y34wg-libnfc-1.7.1/etc/nfc/devices.d
debug   libnfc.general  log_level is set to 3
debug   libnfc.general  allow_autoscan is set to true
debug   libnfc.general  allow_intrusive_scan is set to false
debug   libnfc.general  0 device(s) defined by user
nfc-list uses libnfc 1.7.1
debug   libnfc.driver.acr122_usb        device found: Bus 002 Device 007 Name ACS ACR122
debug   libnfc.general  1 device(s) found using acr122_usb driver
debug   libnfc.general  0 device(s) found using pn53x_usb driver
debug   libnfc.driver.acr122_usb        3 element(s) have been decoded from "acr122_usb:002:007"
libusb: error [op_set_interface] setintf failed error -1 errno 110
error   libnfc.driver.acr122_usb        Unable to set alternate setting on USB interface (Numerical result out of range [-34])
debug   libnfc.general  Unable to open "acr122_usb:002:007".
nfc-list: ERROR: Unable to open NFC device: acr122_usb:002:007

potential workaround ?

As mentionned in issue #535, disabling this function call seems to fix the issue, at least for nfc-list.

It may however alter proper communication with the device.

diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c
index 8a16920..2db62b7 100644
--- a/libnfc/drivers/acr122_usb.c
+++ b/libnfc/drivers/acr122_usb.c
@@ -426,13 +426,13 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
         goto free_mem;
       }

-      res = usb_set_altinterface(data.pudh, 0);
+      /*res = usb_set_altinterface(data.pudh, 0);
       if (res < 0) {
         log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "Unable to set alternate setting on USB interface (%s)", _usb_strerror(res));
         usb_close(data.pudh);
         // we failed to use the specified device
         goto free_mem;
-      }
+      }*/

       // Allocate memory for the device info and specification, fill it and return the info
       pnd = nfc_device_new(context, connstring);

Working nfc-list command after disabling the call :

# nfc-list
nfc-list uses libnfc 1.7.1
NFC device: ACS / ACR122U PICC Interface opened

How could we debug the timeout in usb_set_altinterface call ? Is this call mandatory for this device ?