用于USB的简单内核模块

时间:2011-05-06 13:03:24

标签: c linux-kernel usb device-driver kernel-module

我正在努力熟悉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");

3 个答案:

答案 0 :(得分:1)

您的测试驱动程序未启用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是您的USB棒ID。 如果您不知道ID,请在插入棒时检查dmesg消息。

答案 1 :(得分:1)

根据struct usb_driverlinux/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_id数组,USB子系统将使用该数组来匹配设备。该数组需要与MODULE_DEVICE_TABLE(usb, ...)一起导出。 Wataru给出的答案就是一个很好的例子。

答案 2 :(得分: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”);
}