karan421 karan421 - 9 months ago 68
C Question

simple usb driver

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 = {


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)



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’)

Answer Source

id_table member of the structure is of the type const struct usb_device_id * but you are assigning const struct usb_device_id. Try changing pen_table to &pen_table in structure initialization.
Hope this helps!
Edit: It actually look like you have the declaration of pen_table incorrect. It should probably be :

const struct usb_device_id pen_table[] = { 

and the initialization should be pen_table (and not &pen_table as suggested previously) as you have done in your code.