Prevent claiming of novelty usb device by usbhid so I can control it with libusb?

11,595

Solution 1

If you simply run the libusb program as root,

usb_detach_kernel_driver_np()

actually works as expected.

Solution 2

I think that the following udev rule will do what you want:

SUBSYSTEM=="usb", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0701", MODE="0660", GROUP="plugdev", RUN="/bin/sh -c 'echo -n $id:1.0 > /sys/bus/usb/drivers/usbhid/unbind'"

Your user will need to be a member of the plugdev group to gain access. There's no need to run as root with this rule in place.

Solution 3

I think you will need to add the device id to udev's blacklist so that no module, including usbhid, gets attached to it.

Solution 4

For me (Debian sid/stretch), the udev $id attribute is empty when I plug in my USB device. It is $kernel that contains the necessary string to pass to USBHID's unbind.

Here are the udev rules that I'm using:

SUBSYSTEM=="usb", ATTRS{idVendor}=="0000", ATTRS{idProduct}=="0000", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="0000", ATTRS{idProduct}=="0000", DRIVER="usbhid", RUN="/bin/sh -c 'echo -n $kernel >/sys/bus/usb/drivers/usbhid/unbind'"

Replace the idVendor and idProduct with the IDs of your device, of course.


For writing these sorts of rules, the following command will show you all of the attributes you can use (for the given USB device):

udevadm info -a /sys/bus/usb/devices/1-3:1.0/

Finally, $id and $kernel are not shell variables; they are replaced by the udev parser. For a complete list of these variables, man udev and search for %k.

Share:
11,595

Related videos on Youtube

cemulate
Author by

cemulate

Updated on September 18, 2022

Comments

  • cemulate
    cemulate over 1 year

    I have a USB rocket launcher that I wish to experiment with through libusb. However, libusb cannot claim the interface; presumably because the output of usb-devices lists 'usbhid' as the driver for the device.

    From reading around on the internet, I've only come to the conclusion that I need to detach this driver from the device so I can use it with libusb. However, I have not found a single definitive way to do that, only several different ideas and bug reports.

    So, is there a way to detach the usbhid driver from a device that would be relevant with the kernel and tools supplied with Ubuntu 11.04?

    EDIT:

    I tried creating the file

    /etc/udev/rules.d/10-usbhid.rules
    

    and writing the following:

    ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0701", OPTIONS=="ignore_device"
    

    Saving, then rebooting. The file is still there, but it doesn't appear to be working at all.

    EDIT:

    Okay, I tried this:

    sudo -i
    echo -n "0003:0A81:0701.0006" > /sys/bus/hid/drivers/generic-usb/unbind
    

    After that, navigating to /sys/bus/hid/devices/0003:0A81:0701.0006 and ls yields:

    drwxr-xr-x 2 root root    0 2011-05-29 15:46 power
    lrwxrwxrwx 1 root root    0 2011-05-29 13:19 subsystem ->       ../../../../../../../../../bus/hid
    -rw-r--r-- 1 root root 4096 2011-05-29 13:19 uevent
    

    It no longer lists a "driver" symlink like it did before, so I would assume that it unbound it. However, all evidence seems to suggest that the driver is still usbhid. For example usb-devices yields:

    T:  Bus=02 Lev=03 Prnt=07 Port=00 Cnt=01 Dev#=  9 Spd=1.5 MxCh= 0
    D:  Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs=  1
    P:  Vendor=0a81 ProdID=0701 Rev=00.01
    S:  Manufacturer=Dream Link
    S:  Product=USB Missile Launcher v1.0
    C:  #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
    I:  If#= 0 Alt= 0 #EPs= 1 Cls=03(HID  ) Sub=00 Prot=00 Driver=usbhid
    

    libusb still retuns -1 on usb_claim_interface()....

  • cemulate
    cemulate almost 13 years
    How exactly would I go about that? Pardon my lack of experience, but this link wiki.archlinux.org/index.php/Blacklisting, that I got to by following a page on udev, does not appear to be specifically associated with udev, and does not actually tell me how to blacklist a particular device with vendor product id.
  • tcoolspy
    tcoolspy almost 13 years
    That page is about blacklisting a module from loading. You don't want to do that because you need the module for your keyboard and mouse! What you want is to stop that particular device from associating with it. I'm sorry I don't know the udev rule off hand. If I get a change to look it up I'll add it to my answer but I thought I'd point you in the direction first.