Simple kernel module for USB

I am trying to familiarize myself with the Linux kernel module. So I wrote this simplest module that works on usb. I'm not sure what I am missing. The module is loading. Also on dmesg I see this:

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

but when I insert the joystick, my testusb_probe function is not called. Any idea where I'm wrong. here is the module code:

  #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
source share
3 answers

The USB driver is not included in the test driver.

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 and USB_PRODUCT_ID are your USB folder identifiers. If you do not know the identifiers, check the dmesg message when you insert the stick.

+1
source

You need to register a driver for notifications from usbcore. The following code should work:

 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"); } 
0
source

According to the comment in the kernel source code above struct usb_driver in 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. 

Your testusb_driver structure is incorrect. You need to set the id_table field.

This requires the creation of a struct usb_device_id array that will be used by the USB subsystem to match the device. This array must be exported using MODULE_DEVICE_TABLE(usb, ...) . The answer given to Wataru is a great example.

0
source

All Articles