google/gousb

libusb: bad access [code -3]

jparrill opened this issue · 8 comments

I've seen the issue from 2017, but this is still happenning using the example from the Go documentation

// Initialize a new Context.
    ctx := gousb.NewContext()
    defer ctx.Close()

    // Open any device with a given VID/PID using a convenience function.
    // Luxafor (PID 0xf372 | VID 0x04d8)
    dev, err := ctx.OpenDeviceWithVIDPID(0x04d8, 0xf372)
    if err != nil {
        log.Fatalf("Could not open a device: %v", err)
    }
    defer dev.Close()

    cfg, err := dev.Config(1)
    if err != nil {
        log.Fatalln("Cannot instanciate Config from Dev")
    }

    fmt.Println(cfg.Interface(0, 0))

    // Claim the default interface using a convenience function.
    // The default interface is always #0 alt #0 in the currently active
    // config.
    intf, done, err := dev.DefaultInterface()
    if err != nil {
        log.Fatalf("%s.DefaultInterface(): %v", dev, err)
    }
    defer done()

I'm trying to use it with Luxafor device, which are those VID,PID.

And this is the output:

<nil> failed to claim interface 0 on vid=04d8,pid=f372,bus=4,addr=8,config=1: libusb: bad access [code -3]
2022/08/02 18:23:58 vid=04d8,pid=f372,bus=4,addr=8.DefaultInterface(): failed to select interface #0 alternate setting 0 of config 1 of device vid=04d8,pid=f372,bus=4,addr=8: failed to claim interface 0 on vid=04d8,pid=f372,bus=4,addr=8,config=1: libusb: bad access [code -3]
exit status 1

It's failling on the default interface assignation: intf, done, err := dev.DefaultInterface()

I've tried other number as an num of inerface but the output shows that the 0, 0 values are the right ones.

<nil> descriptor of interface (3, 0) in vid=04d8,pid=f372,bus=4,addr=8,config=1: interface 3 not found, available interface numbers: [0]
<nil> descriptor of interface (0, 3) in vid=04d8,pid=f372,bus=4,addr=8,config=1: alternate setting 3 not found for Interface 0 (1 alternate settings), available alt settings: [0]

My environment is:

Go Version: go version go1.18.5 darwin/arm64
Uname: Darwin xps-xenomorph-eth.kerbnet 21.6.0 Darwin Kernel Version 21.6.0: Sat Jun 18 17:07:22 PDT 2022; root:xnu-8020.140.41~1/RELEASE_ARM64_T6000 arm64
Σ goard git:(main) brew list libusb
/opt/homebrew/Cellar/libusb/1.0.26/include/libusb-1.0/libusb.h
/opt/homebrew/Cellar/libusb/1.0.26/lib/libusb-1.0.0.dylib
/opt/homebrew/Cellar/libusb/1.0.26/lib/pkgconfig/libusb-1.0.pc
/opt/homebrew/Cellar/libusb/1.0.26/lib/ (2 other files)
/opt/homebrew/Cellar/libusb/1.0.26/share/libusb/ (9 files)

This is the lsusb info about the device:

004.008 04d8:f372 Unknown (Microchip Technology, Inc.)
  Protocol: (Defined at Interface level)
  Configuration 1:
    --------------
    Interface 0 alternate setting 0 (available endpoints: [0x01(1,OUT) 0x81(1,IN)])
      Human Interface Device (No Subclass) None
      ep #1 IN (address 0x81) interrupt - undefined usage [8 bytes]
      ep #1 OUT (address 0x01) interrupt - undefined usage [8 bytes]
    --------------

Any ideas?

Hello?

I've seen the issue from 2017

Have you tried the solution from that thread? This is almost definitely a permissions error at your end, not a library problem.

As also mentioned in that issue, you can add ctx.Debug(4) to your code before the call to OpenDeviceWithVIDPID() to get libus's debug output, which is likely to contain something like:

libusb: error [get_usbfs_fd] libusb couldn't open USB device /dev/bus/usb/004/008, errno=13
libusb: error [get_usbfs_fd] libusb requires write access to USB device nodes
libusb: debug [libusb_open] open 4.8 returns -3

Adding the Debug statement, I've seen this:

[ 0.014493] [000663a0] libusb: debug [libusb_get_configuration] active config 1
vid=04d8,pid=f372,bus=4,addr=3,config=1
[ 0.014647] [000663a0] libusb: debug [libusb_claim_interface] interface 0
[ 0.015079] [000663a0] libusb: info [darwin_claim_interface] USBInterfaceOpen: another process has device opened for exclusive access
<nil> failed to claim interface 0 on vid=04d8,pid=f372,bus=4,addr=3,config=1: libusb: bad access [code -3]
[ 0.015166] [000663a0] libusb: debug [libusb_get_configuration]
[ 0.015169] [000663a0] libusb: debug [libusb_get_configuration] active config 1
[ 0.015173] [000663a0] libusb: debug [libusb_get_configuration]
[ 0.015175] [000663a0] libusb: debug [libusb_get_configuration] active config 1
[ 0.015178] [000663a0] libusb: debug [libusb_claim_interface] interface 0
[ 0.015419] [000663a0] libusb: info [darwin_claim_interface] USBInterfaceOpen: another process has device opened for exclusive access
2022/09/05 22:26:07 vid=04d8,pid=f372,bus=4,addr=3.DefaultInterface(): failed to select interface #0 alternate setting 0 of config 1 of device vid=04d8,pid=f372,bus=4,addr=3: failed to claim interface 0 on vid=04d8,pid=f372,bus=4,addr=3,config=1: libusb: bad access [code -3]
exit status 1

@chrBrd any ideas about the trace I've copied? I mean, looks like there are an info message just before the Error saying something about exclusive access to the device but I didn't modify anything in the permissions side. Even I've executed the Go code with sudo and is getting me the same error...

Apparently it's caused by Apple locking down device access in macOS 12. There's a long thread on an open issue in the libusb repo. There's a workaround suggested in libusb's FAQ, and a more straight-to-the-point digest of it on Stack Overflow.

@jparrill As this issue isn't anything to do with gousb, could you please do the maintainers a wee favour and close it?

Dynom commented

Hi,

I've a similar issue on a RaspberryPI and I'm seeing different behaviour between usage and while it might not be a problem with gousb, I do hope anyone can point me in the right direction.

Trying to open a USB device, I see the infamous libusb: bad access [code -3]. Running the same application elevated and it all works as expected: I'm able to "open" the device at least.
However what I don't understand is why I'm unable to open the USB device.

I run the application as a user, member of the group gpio and I've added liberal permissions to the device:

pi@raspberrypi:/dev/input $ l
crw-rw-rw- 1 root gpio  13, 66 Apr 12 15:47 event2

Is the library not leaning on udev? And/or how can I give access to a device so that libusb is able to access it without elevated privileges?

--edit-----

To answer my own question and for those stumbling on this issue later, libdev will try to use the bus directly. It needs write permissions. One way to solve it is by adding a udev rule. For testing, you can try:

echo 'SUBSYSTEM=="usb", DRIVER=="usb", MODE:="0666"' > /etc/udev/rules.d/99-liberal-usb.rules
sudo udevadm control --reload && sudo udevadm trigger

Hi Dynom .
Follow my modest suggestion for Raspberry Pi installation:

Updated Gousb instructions for Raspberry Pi : (2021)
#45 (comment)