I am a newbie learning how to write Linux device drivers for USB devices. I am getting an error while compiling my code. There is a problem in the commented line. I am making a module for a USB drive as follows:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/usb.h>
static int pen_probe(struct usb_interface *intf,const struct usb_device_id *id)
{
printk(KERN_ALERT"\nthe probe is successful");
return 0;
}
static void pen_disconnect(struct usb_interface *intf)
{
printk(KERN_ALERT"pen drive removed");
}
const struct usb_device_id pen_table = {
USB_DEVICE(0x058f,0x6387),
};
MODULE_DEVICE_TABLE(usb,pen_table);
static struct usb_driver pen_driver = {
.name = "pen_driver",
.id_table = pen_table, // error coming at this line
.probe = pen_probe,
.disconnect = pen_disconnect,
};
static int __init pen_init(void)
{
int ret;
ret = usb_register(&pen_driver);
printk(KERN_ALERT"THE RET::%d\n",ret);
return 0;
}
static void __exit pen_exit(void)
{
usb_deregister(&pen_driver);
}
module_init(pen_init);
module_exit(pen_exit);
MODULE_LICENSE("GPL");
It’s giving me an error as follows:
:26:5: error: initializer element is not constant
/home/karan/practice/usb/usb1.c:26:5: error: (near initialization for ‘pen_driver.id_table’)
id_tablemember of the structure is of the typeconst struct usb_device_id *but you are assigningconst struct usb_device_id. Try changingpen_tableto&pen_tablein structure initialization.Hope this helps!
Edit: It actually look like you have the declaration of
pen_tableincorrect. It should probably be :and the initialization should be
pen_table(and not&pen_tableas suggested previously) as you have done in your code.