简单的USB驱动程序

kar*_*421 6 c linux-kernel

我是一个学习如何为USB设备编写Linux设备驱动程序的新手.我在编译代码时遇到错误.注释行中存在问题.我正在为USB驱动器制作模块,如下所示:

#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");
Run Code Online (Sandbox Code Playgroud)

它给我一个错误如下:

  :26:5: error: initializer element is not constant

  /home/karan/practice/usb/usb1.c:26:5: error: (near initialization for ‘pen_driver.id_table’)
Run Code Online (Sandbox Code Playgroud)

ano*_*ard 3

id_table结构体的成员属于 类型const struct usb_device_id *,但您正在分配const struct usb_device_id。尝试更改pen_table&pen_table结构初始化。
希望这可以帮助!
编辑:实际上看起来您的声明pen_table不正确。大概应该是:

const struct usb_device_id pen_table[] = { 
   {USB_DEVICE(0x058f,0x6387)},
   {}  
};
Run Code Online (Sandbox Code Playgroud)

并且初始化应该像您在代码中所做的那样pen_table(而不是像之前建议的那样)。&pen_table