usb kernel module does not load on demand but works fine with insmod and modprobe from the shell

5,630

You are missing with usb_register and probe functions

Here is updated device driver with usb_register and probe functions

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

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Aruna Hewapathirane");
MODULE_DESCRIPTION("A USB Keyboard Driver Kernel Module");

static struct usb_device_id usb_kbd_id_table[] = {
    { USB_INTERFACE_INFO(
        USB_INTERFACE_CLASS_HID,
        USB_INTERFACE_SUBCLASS_BOOT,
    USB_INTERFACE_PROTOCOL_KEYBOARD) },
    { } /* Terminating entry */
};

MODULE_DEVICE_TABLE(usb, usb_kbd_id_table);

static int kbd_probe(struct usb_interface *interface,
    const struct usb_device_id *id)
{
    pr_info("USB keyboard probe function called\n");
    return 0;
}

static void kbd_disconnect(struct usb_interface *interface)
{
    pr_info("USB keyboard disconnect function called\n");
}

static struct usb_driver kbd_driver = {
    .name  = "usbkbd",
    .probe = kbd_probe,
    .disconnect = kbd_disconnect,
    .id_table = usb_kbd_id_table,
};

static int __init kbd_init(void)
{
    int res = 0;
    res = usb_register(&kbd_driver);
    if (res)
        pr_err("usb_register failed with error %d", res);
    return res;
}

static void __exit kbd_exit(void)
{
    pr_debug("USB Keyboard Removed..\n");
    usb_deregister(&kbd_driver);
    return;
}

module_init(kbd_init);
module_exit(kbd_exit);

Please refer previous SO question for probe function's use.

Share:
5,630

Related videos on Youtube

Aruna Hewapathirane
Author by

Aruna Hewapathirane

Updated on September 18, 2022

Comments

  • Aruna Hewapathirane
    Aruna Hewapathirane over 1 year

    I have a USB Linux Kernel module that compiles and builds. Running insmod loads my module and dmseg and tail -f /var/log/debug shows me it works as expected.

    Running depmod -a then modprobe from the terminal loads the module and modprobe -r unloads and I see - tail -f /var/log/debug output as expected.

    When I plug in my USB keyboard though it does not trigger and load on-demand as expected.

    I have investigated /etc/udev/rules.d with no success. Any workarounds or guidance is most welcome. Am running Ubuntu 12.04.4 LTS with a Custom Linux Kernel 3.14.0

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/usb.h>
    #include <linux/usb/input.h>
    #include <linux/hid.h>
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("Aruna Hewapathirane");
    MODULE_DESCRIPTION("A USB Keyboard Driver Kernel Module");
    
    static struct usb_device_id usb_kbd_id_table[] = {
        { USB_INTERFACE_INFO(
            USB_INTERFACE_CLASS_HID,
            USB_INTERFACE_SUBCLASS_BOOT,
            USB_INTERFACE_PROTOCOL_KEYBOARD) },
        { } /* Terminating entry */
    };
    
    MODULE_DEVICE_TABLE(usb, usb_kbd_id_table);
    
    static int __init kbd_init(void)
    {
        printk(KERN_DEBUG "USB Keyboard Plugged In.. \n");
        return 0;
    }
    
    static void __exit kbd_exit(void)
    {
        printk(KERN_DEBUG "USB Keyboard Removed.. \n");
        return ;
    }
    
    module_init(kbd_init);
    module_exit(kbd_exit);
    
    • maayke
      maayke about 10 years
      You can follow this article linuxjournal.com/article/7353
    • Quint
      Quint about 7 years
      Please avoid publishing questions about the challenge on public fora. You can always ask Little Penguin for pointers by replying to the task mail you received.
  • v0d1ch
    v0d1ch over 8 years
    is it possible to load a module without the struct usb_driver and call to probe() ?