简体   繁体   中英

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 = {
    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_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[] = { 
   {USB_DEVICE(0x058f,0x6387)},
   {}  
};

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

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

static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
printk(KERN_INFO "Pen drive (%04X:%04X) plugged\n", id->idVendor, id->idProduct);
return 0;
}

static void pen_disconnect(struct usb_interface *interface)
{
printk(KERN_INFO "Pen drive removed\n");
}

static struct usb_device_id pen_table[] =
{
{ USB_DEVICE(0x058F, 0x6387) },
{} /* Terminating entry */
};
 MODULE_DEVICE_TABLE (usb, pen_table);

   static struct usb_driver pen_driver =
  {
.name = "pen_driver",
.id_table = pen_table,
.probe = pen_probe,
.disconnect = pen_disconnect,
};

   static int __init pen_init(void)
 {
return usb_register(&pen_driver);
    }

 static void __exit pen_exit(void)
  {
usb_deregister(&pen_driver);
  }

  module_init(pen_init);
   module_exit(pen_exit);
obj-m +=usbDemo1.o

KVERSION = $(shell uname -r)
all:
       make -C/lib/modules/$(KVERSION)/build M=$(PWD) modules
clean:
       make -C/lib/modules/$(KVERSION)/build M=$(PWD) clean

obj-m +=usbDemo1.o

KVERSION = $(shell uname -r) all: make -C/lib/modules/$(KVERSION)/build M=$(PWD) modules clean: make -C/lib/modules/$(KVERSION)/build M=$(PWD) clean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM