我是一个学习如何为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");
它给我一个错误如下:
:26:5: error: initializer element is not constant
/home/karan/practice/usb/usb1.c:26:5: error: (near initialization for ‘pen_driver.id_table’)
答案 0 :(得分: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)},
{}
};
并且初始化应该是pen_table
(而不是之前建议的&pen_table
),就像在代码中所做的那样。
答案 1 :(得分:0)
#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);
答案 2 :(得分:-1)
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
答案 3 :(得分:-2)
obj-m + = usbDemo1.o
KVERSION = $(shell uname -r) 所有: make -C / lib / modules / $(KVERSION)/ build M = $(PWD)模块 清洁: make -C / lib / modules / $(KVERSION)/ build M = $(PWD)clean