4

我正在尝试熟悉 Linux 内核模块。所以我写了这个在usb上工作的最简单的模块。我不确定我错过了什么。正在加载模块。同样在 dmesg 我可以看到:

   [27245.911387] usbcore: registered new interface driver testusb
   [27245.911392] testusb: driver registered successfully

但是当我插入 USB 棒时,我的 testusb_probe 函数没有被调用。知道我哪里出错了。这是模块的代码:

   #include <linux/kernel.h>
   #include <linux/module.h>
   #include <linux/usb.h>


   static int testusb_probe(struct usb_interface *interface, const struct usb_device_id *id)
   {
    printk("testusb: probe module\n");
    return 0;
   }


   static void testusb_disconnect(struct usb_interface *interface)
   {
    printk("testusb: disconnect module\n");
   }


   static struct usb_driver testusb_driver = {
           name: "testusb",
        probe: testusb_probe,
           disconnect: testusb_disconnect,
   };

   static int __init testusb_init(void)
   {
           int result;

           result = usb_register(&testusb_driver);
           if (result) {
                   printk("testusb: registering driver failed");
           } else {
                   printk("testusb: driver registered successfully");
           }

           return result;
   }


   static void __exit testusb_exit(void)
   {
           usb_deregister(&testusb_driver);
           printk("testusb: module deregistered");
   }

   module_init(testusb_init);
   module_exit(testusb_exit);

   MODULE_AUTHOR("Dal Chand");
   MODULE_LICENSE("GPL");
4

3 回答 3

2

您的测试驱动程序未启用 USB 热插拔。

http://www.linuxjournal.com/node/4786/print

/* Define these values to match your devices */
#define USB_VENDOR_ID      0xfff0
#define USB_PRODUCT_ID     0xfff0

/* table of devices that work with this driver */
static struct usb_device_id test_table [] = {
        { USB_DEVICE(USB_VENDOR_ID, USB_PRODUCT_ID) },
        { }                                     /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, test_table);

USB_VENDOR_ID 和 USB_PRODUCT_ID 是您的 U 盘的 ID。如果您不知道 ID,请在插入记忆棒时检查 dmesg 消息。

于 2011-05-09T09:05:42.663 回答
2

根据上面内核源代码中的struct usb_driver注释linux/usb.h

USB interface drivers must provide a name, probe() and disconnect()
methods, and an id_table.  Other driver fields are optional.

The id_table is used in hotplugging.  It holds a set of descriptors,
and specialized data may be associated with each entry.  That table
is used by both user and kernel mode hotplugging support.

您的testusb_driver结构不正确。您需要设置id_table字段。

这需要创建一个数组,struct usb_device_idUSB 子系统将使用该数组来匹配设备。这个数组需要用MODULE_DEVICE_TABLE(usb, ...). Wataru给出的答案就是一个很好的例子。

于 2019-09-10T19:14:33.350 回答
0

您必须为来自 usbcore 的通知注册驱动程序。以下代码应该可以工作:

static int usb_notify(struct notifier_block *self, unsigned long action, void *dev) { 
    printk(KERN_INFO “USB device added \n”); 
    switch (action) { 
    case USB_DEVICE_ADD: 
        printk(KERN_INFO “USB device added \n”);
        break;
    case USB_DEVICE_REMOVE: 
        printk(KERN_INFO “USB device removed \n”); 
        break; 
    case USB_BUS_ADD: 
        printk(KERN_INFO “USB Bus added \n”); 
        break; 
    case USB_BUS_REMOVE: 
        printk(KERN_INFO “USB Bus removed \n”); 
    } 
    return NOTIFY_OK; 
} 

static struct notifier_block usb_nb = { 
    .notifier_call = usb_notify, 
}; 

int init_module(void) { 
    printk(KERN_INFO “Init USB hook.\n”); 
    /* 
    * Hook to the USB core to get notification on any addition or removal of USB devices */ 
    usb_register_notify(&usb_nb); 
    return 0; 
}

void cleanup_module(void) { 
    /* 
    * Remove the hook */ 
    usb_unregister_notify(&usb_nb); 
    printk(KERN_INFO “Remove USB hook\n”);
} 
于 2011-05-09T07:22:11.570 回答