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 は USB スティックの ID です。ID がわからない場合は、スティック挿入時の dmesg メッセージを確認してください。

于 2011-05-09T09:05:42.663 に答える
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 に答える