47

I am running into trouble calling libusb_open in C++ on Linux (running on Raspberry Pi but virtualized to x86 using Exagear).

I can find a device using libusb_get_device_list but it appears translating a device to a device handler using libusb_open is causing problems, even though it returns a 0 (success), however no further commands work. It is giving me the error:

libusb: error [op_open] getcap failed (22)

UPDATE

A little bit of digging has revealed that the following command is producing the error (within libusb):

r = ioctl(hpriv->fd, IOCTL_USBFS_GET_CAPABILITIES, &hpriv->caps);

where IOCTL_USBFS_GET_CAPABILITIES is _IOR('U', 26, __u32). Digging has revealed that error 22 from ioctl is an invalid argument. I'm still trying to unpick this, but any help here is greatly appreciated.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35
Edmund Gentle
  • 736
  • 1
  • 6
  • 16
  • What are your linux/libusb versions? Can you post the complete, runnable example? – Piotr Praszmo Jan 06 '17 at 12:52
  • Code: http://pastebin.com/S3f7ykAj building using `g++ -o test test.cpp -Wall -lusb-1.0`, Linux is 4.4.38-v7+ Debian. libusb is 1.0.19-1. Hope this helps! EDIT: line 95 – Edmund Gentle Jan 06 '17 at 14:01
  • 2
    My first thought was to check whether the version of the Linux kernel used supports `IOCTL_USBFS_GET_CAPABILITIES`. According to `usr/src/linux-headers-4.4.38-v7+/include/uapi/linux/usbdevice_fs.h` in linux-headers-4.4.38-v7+_4.4.38-v7+-2_armhf.deb **the ioctl is supported**. The next most common reason i can think of for `ioctl()` returning `EINVAL` (i.e. -22) is a ***32bit/64bit mismatch between the user-space app and the Linux kernel***. This [**answer**](http://stackoverflow.com/a/9883441/319204) explains it well. – TheCodeArtist Jan 14 '17 at 11:16
  • Do you have permissions to access the USB device? Does the call succeed when run from root? That would point you in the right direction, because if it runs under root, you need to grant your account the capabilities to access USB ports. – casualcoder Jan 19 '20 at 02:52
  • 1
    Is the hpriv->fd valid? – Surt Mar 27 '20 at 19:07
  • As the error states, check the arguments. It is either hpriv (then implicitly fd and caps), hpriv->fd or hpriv->caps. For address of the member, it would be more explicit to use '&(hpriv->caps)' to avoid thinking of operator precedence, ioctrl is overloaded enough to allow the wrong argument. – Patrick Z Feb 18 '21 at 10:42

0 Answers0