c - Linux 内核模块,防止 usbcore 在探测后注册另一个接口(interface)

标签 c linux-kernel kernel

我正在寻找一种方法来防止 usbcore 在“请求”探测后注册新接口(interface) usbhid 或其他。

C 函数 Linux 内核模块 (mUSBdriver.c)

...
static int mUSBdriver_probe(struct usb_interface *interface, const struct usb_device_id *id){
       printk(KERN_INFO "mUSBdriver: new USB device PLUGGED!");
       return 0; // indicate we will manage this device
       //return -ENODEV;  indicate we will NOT manage this device
    }
...

下面是我编译和插入的步骤

rmmod usbhid 
rmmod mUSBdriver 
make 
insmod mUSBdriver.ko

当我热插拔我的设备时

~# tail -n 50 /var/log/syslog
...
...kernel: usbcore: registered new interface driver mUSBdriver
...kernel: usb 2-1: Product HID Keyboard
...
...kernel: mUSBdriver:new USB device PLUGGED!
...

我的设备是HID键盘类,那么

~# tail -n 50 /var/log/syslog
...
...kernel: usbcore: registered new interface driver mUSBdriver
...kernel: usb 2-1: Product HID Keyboard
...
...kernel: mUSBdriver:new USB device PLUGGED!
...kernel: usbcore: registered new interface driver usbhid
...kernel: usbhid: USB HID core driver
...

自从我决定管理(返回 0)这个设备后,什么也没有出现。 我正在寻找一种解决方案,以防止 usbcore 在我的驱动程序收到探测后注册另一个接口(interface)。我想将此设备视为“BAD USB”并取消所有其他操作。我在 usb.h 中搜索但没有。

知道我该如何服用吗?

my idea: 
- send a notification to usbcore ?
- hook on usbcore  ?

(but I do not know how to do it)

最佳答案

usbcore 永远不允许两个驱动程序同时连接到同一个接口(interface)。

usbcore 实际上做的是注册一个新的接口(interface)driver。这与该驱动程序的接口(interface)是否存在或实际可用无关。它发生在加载该驱动程序(模块)时。


但是,可能存在一个问题:第一个被探测到的驱动程序获胜。如果 usbhid 驱动程序已经加载,它可能会在您自己的驱动程序之前附加。

要防止 usbhid 连接到您的设备,您必须使用模块选项告诉它忽略该设备:

options usbhid quirks=0x1234:0x5678:4

或将 HID_QUIRK_IGNORE 条目添加到 drivers/hid/usbhid/hid-quirks.c

关于c - Linux 内核模块,防止 usbcore 在探测后注册另一个接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32681578/

相关文章:

与字符串指针混淆

linux-kernel - 为什么自旋锁在单处理器(单核)系统中不起作用?

linux - Linux 内核是如何测试的?

linux-kernel - remap_pfn_range如何将内核内存重新映射到用户空间?

linux - 内核 oops - 页面保护错误

c - 用 C 实现的解释器中的垃圾收集问题

合作工具组 : coding in C

c - 结构体指针指向结构体数组

android - 低级安卓调试

linux - enqueue_head宏linux的含义