简体   繁体   English

如何分离用于 HID 的系统 USB 驱动程序?

[英]How to detach the system USB driver for HID?

I need to communicate with a home-made controller over USB.我需要通过 USB 与自制的 controller 通信。 I'd like to use Python.我想使用 Python。 The controller 'speaks' HID, and is working (detected by Linux when plugged in). controller 'speaks' HID,并且正在工作(插入时由 Linux 检测到)。

The problem is that linux helpfully attaches the hid driver when the controller is plugged in. When 'Opening' the devices, I get the error OSError: open failed , which I suspect come from Linux's automatic attachment.问题是 linux 在插入 controller 时有助于附加隐藏驱动程序。当“打开”设备时,我收到错误OSError: open failed ,我怀疑它来自 Linux 的自动附加。

Libusb has functions to a) check if the system driver is attached, and b) to detach the driver. Libusb 具有以下功能:a) 检查系统驱动程序是否已连接,以及 b) 分离驱动程序。 But, as mentioned before, the controller needs the HID protocol, and revising the different Python HID implementations, they do not seem to care about detaching the system driver.但是,如前所述,controller 需要HID协议,并且修改了不同的 Python HID实现,他们似乎并不关心分离系统驱动程序。

I don't feel comfortable loading both a libusb (just for detaching?) and an hid(api) module.我对同时加载libusb (只是为了分离?)和hid(api)模块感到不舒服。 Isn't there a more elegant way to do this?难道没有更优雅的方式来做到这一点吗?

The code used to open is:用于打开的代码是:

import hid

VID = 0x04d8
PID = 0xf83f

def init(VID, PID):
    dev = hid.device(VID, PID)
    handle = dev.open()

init(VID, PID)

which gives:这使:

OSError: open failed

Edit:编辑:

Prevent claiming of novelty usb device by usbhid gives two answers with udev rules which try to detach the driver. 防止声称新奇 usb 设备 by usbhid给出了两个答案,其中 udev 规则试图分离驱动程序。 The first gives an error message in dmesg .第一个在dmesg中给出错误消息。 The second, apparently, does nothing.第二个,显然,什么都不做。

EDIT : Still no joy.编辑:仍然没有喜悦。 Maybe a few cues to help:也许有一些提示可以帮助:

On boot, the USB device shows up in lsusb -t as (last line):启动时,USB 设备在lsusb -t中显示为(最后一行):

/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/2p, 480M
    |__ Port 2: Dev 93, If 0, Class=Hub, Driver=hub/4p, 480M
        |__ Port 1: Dev 95, If 0, Class=Hub, Driver=hub/4p, 12M
            |__ Port 2: Dev 96, If 0, Class=Printer, Driver=, 12M
        |__ Port 3: Dev 94, If 0, Class=Human Interface Device, Driver=usbhid, 12M

It also shows up in dmesg , with correct information.它还显示在dmesg中,并带有正确的信息。 As shown, the OS connects the device to the usbhid driver.如图所示,操作系统将设备连接到usbhid驱动程序。

This code succesfully detaches it from the driver:此代码成功地将其与驱动程序分离:

import libusb as usb
class Module():
    def __init__(self, VID, PID):
        self.usb = usb.init(None)
        dev = usb.open_device_with_vid_pid(None, VID, PID)
        if usb.kernel_driver_active(dev, 0):
            print("Kernel has device, detaching...")
            usb.detach_kernel_driver(dev, 0)
        usb.close(dev)

Now it shows in lsusb -t as unconnected:现在它在lsusb -t中显示为未连接:

Port 3: Dev 94, If 0, Class=Human Interface Device, Driver=, 12M

But still, the following code cannot open the device (and tried other methods too).但是,以下代码仍然无法打开设备(并且也尝试了其他方法)。 Neither does it enumerate in the enumerate call:它也没有在enumerate调用中枚举:

import hid
class Motor():
    def __init__(self, vid, pid):
        self.vid, self.vid = vid, vid

        print(len(hid.enumerate()))
        for rec in print(hid.enumerate()):
            print(rec)

        dev = hid.Device(vid, vid)
        handle = dev.open()

Well, it's working.嗯,它正在工作。 Though I'm not 100% sure of the cause, and I really lost too much time already to experiment at the moment, things started working after finding out that libusbmuxd was running and possibly interfering with the normal use of usbhid or hidapi .虽然我不是 100% 确定原因,而且我现在确实已经浪费了太多时间来进行实验,但在发现libusbmuxd正在运行并可能干扰usbhidhidapi的正常使用后,事情开始起作用了。

After removing all traces I could find (the normal Slackware removepkg command didn't remove the libraries, and libusbmuxd continued present - I had to remove the.so's by hand) the same hidapi test program ran perfectly.删除所有我能找到的痕迹后(正常的 Slackware removepkg命令没有删除库, libusbmuxd继续存在 - 我必须手动删除.so)相同的hidapi测试程序运行完美。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM