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.
Related videos on Youtube
Author by
Aruna Hewapathirane
Updated on September 18, 2022Comments
-
Aruna Hewapathirane over 1 year
I have a USB Linux Kernel module that compiles and builds. Running
insmod
loads my module anddmseg
andtail -f /var/log/debug
shows me it works as expected.Running
depmod -a
thenmodprobe
from the terminal loads the module andmodprobe -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 runningUbuntu 12.04.4 LTS
with a Custom Linux Kernel3.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 about 10 yearsYou can follow this article linuxjournal.com/article/7353
-
Quint about 7 yearsPlease 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 over 8 yearsis it possible to load a module without the struct usb_driver and call to probe() ?