5

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);
  • You can follow this article linuxjournal.com/article/7353 – maayke Apr 2 '14 at 8:31
  • 1
    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. – Quint Mar 21 '17 at 0:55
6

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.

  • is it possible to load a module without the struct usb_driver and call to probe() ? – v0d1ch Nov 19 '15 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.