Stop usbhid from claiming usb device on hotplug

Armin Moradi amoradi at fedoraproject.org
Sat May 30 01:43:20 EDT 2015


>> I'm writing a kernel module, but probe() is never called since usbhid
>> claims the usb device as soon as the device is plugged in.
>
> What type of module for what type of device?  And why do you want to
> override the hid driver from binding to it?
>
> thanks,
>
> greg k-h

I'm just learning about device drivers, so I'm writing a dummy usb
device driver. I've pasted the code here (I hope the formatting is
correct):

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/usb.h>

static int mmod_probe(struct usb_interface *intf,
                      const struct usb_device_id *id)
{
        pr_debug("mmod: probe is called");

        /* return -ENODEV if we don't want to claim the device */
        return 0;
}

static void mmod_disc(struct usb_interface *intf)
{
        pr_debug("mmod: disconnected");
}

static const struct usb_device_id mmod_id[] = {
        {.match_flags = USB_DEVICE_ID_MATCH_INT_PROTOCOL,
         .bInterfaceClass = 0x01
        },
        {}
};

/* export the id to userspace */
MODULE_DEVICE_TABLE(usb, mmod_id);

static struct usb_driver mmod_driver = {
        .name = "mmod_driver",
        .id_table = mmod_id,
        .probe = mmod_probe,
        .disconnect = mmod_disc
};

static int __init init_task05(void)
{
        pr_debug("mmod loaded");
        usb_register(&mmod_driver);

        return 0;
}

static void __exit exit_mymod(void)
{
        pr_debug("mmod unloaded");
        usb_deregister(&mmod_driver);
}

module_init(init_mmod);
module_exit(exit_mmod);

MODULE_LICENSE("GPL");


I can load and unload the module and I can see the messages being
printed. I can also see usbcore registering the new driver in dmesg,
but when I plug any usb keyboard (which I have registered in id
table), the probe never gets called. Instead, I see usbhid taking over
and initializing the device in dmesg. And as far as intention goes,
this is just for fun.

I have thought about editing hid-core.c and adding usb quirks there
and recompile the kernel, but that seems a bit drastic. I thought
maybe there's a better way to do it.

Thanks a lot,
-- 
Armin Moradi



More information about the Kernelnewbies mailing list