0

I'm doing a Linux kernel module which every time a keyboard is plugged in it prints a message like "Keyboard connected!"

#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("rcortes-");
MODULE_DESCRIPTION("Load when a Keyboard is Plugged In");
MODULE_VERSION("0.01");

static int __init kb_init(void)
{
    printk(KERN_INFO "Keyboard has been connected!\n");
    return 0;
}

static void __exit kb_exit(void)
{
    printk(KERN_INFO "Module clean up!\n");
}

module_init(kb_init);
module_exit(kb_exit);

I have a udev rule to load the module when it detects the keyboard:

ACTION=="add", SUBSYSTEM=="usb", ATTRS{bInterfaceClass}=="03", ATTRS{bInterfaceSubClass}=="01", ATTRS{bInterfaceProtocol}=="01", RUN+="/usr/sbin/insmod /home/LinuxKernelAssignments/ex04/basic/kb_hotplug.ko"

Now, the problem is that when I connect the keyboard, my udev rule fails with exit code 1. I guess the problem is that usbhid is already registering the keyboard before udev loads the module but I want kb_hotplug to be executed first.

What 'dmesg -w' prints:

[ 1285.506100] usb 2-2: new low-speed USB device number 8 using ohci-pci
[ 1286.345073] usb 2-2: New USB device found, idVendor=04d9, idProduct=1702, bcdDevice=4.06
[ 1286.345086] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=0
[ 1286.345091] usb 2-2: Product: USB Keyboard
[ 1286.345096] usb 2-2: Manufacturer: 
[ 1286.385105] input: USB Keyboard as /devices/pci0000:0000:0000:06.0/usb2/2-2/2-2:1.0/0003:04D9:1702.0012/input/input29
[ 1286.920473] hid-generic 0003:04D9:1702.0012: input,hidraw1: USB HID v1.10 Keyboard [USB Keyboard] on usb-0000:00:06.0-2/input0
[ 1286.963103] input: USB Keyboard System Control as /devices/pci0000:0000:0000:06.0/usb2/2-2/2-2:1.1/0003:04D9:1702.0013/input/input30
[ 1287.015041] input: USB Keyboard Consumer Control as /devices/pci0000:0000:0000:06.0/usb2/2-2/2-2:1.1/0003:04D9:1702.0013/input/input31
[ 1287.015194] hid-generic 0003:04D9:1702.0013: input,hidraw2: USB HID v1.10 Device [USB Keyboard] on usb-0000:00:06.0-2/input1
[ 1287.038807] (udev-worke[612]: 2-2:1.0: Process '/usr/sbin/insmod /home/LinuxKernelAssignments/ex04/basic/kb_hotplug.ko' failed with exit code 1.

I'm using a Linux from scratch distro.

2
  • I've edited your post, mostly to fix the formatting. Please pay attention: to enable syntax coloring, you have to specify the language in the line of the opening Markdown code fence. For example: ~~~c++. For plain text output, specify ~~~text. Commented Aug 18 at 7:01
  • The issue is that your module is trying to load after the USB HID driver has already claimed the keyboard device. The exit code 1 from insmod likely means the module is already loaded or there's a dependency issue. Here are several solutions: Solution 1: Use USB device driver approach instead of udev Commented Nov 1 at 16:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.