我正在尝试实现 USB 设备驱动程序。我写过
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/usb.h>
struct usb_device_id IDtable[]={
{USB_DEVICE(0x067b,0x2303)},
{}
};
int ttlprobe(struct usb_interface *interface, const struct usb_device_id *id)
{
printk(KERN_INFO "Probed (%04X:%04X) device\n", id->idVendor, id->idProduct);
printk(KERN_INFO "num_altsetting : %d\n",interface->num_altsetting);
return 0;
}
void ttldisconnect(struct usb_interface *interface)
{
printk(KERN_INFO "Device disconnected \n");
}
struct usb_driver busb;
busb.name="guru";
busb.id_table=IDtable;
busb.probe=ttlprobe(&busb,IDtable);
busb.disconnect=ttldisconnect;
int init_module(void){
printk(KERN_ALERT "intimodule exec Trying to register to USB CORE \n");
int k=usb_register(&busb);
printk(KERN_INFO "retuern value of usb_register_driver : %d\n",k);
pr_info("name = %s\n", THIS_MODULE->name);
pr_info("name = %s\n", KBUILD_MODNAME);
return k;
}
void cleanup_module(void){
printk(KERN_ALERT "Trying to de-register to USB CORE");
return usb_deregister(&busb);
}
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Anil Kumar Pugalia <email_at_sarika-pugs_dot_com>");
MODULE_DESCRIPTION("USB Pen Registration Driver");
输出是这样的,
make -C /lib/modules/4.10.0-28-generic/build M=/home/guru/Desktop/Linuxx/Drivers/USB/check modules
make[1]: Entering directory '/usr/src/linux-headers-4.10.0-28-generic'
CC [M] /home/guru/Desktop/Linuxx/Drivers/USB/check/chc.o
/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.c:20:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
busb.name="guru";
^
/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.c:21:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
busb.id_table=IDtable;
^
/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.c:22:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
busb.probe=ttlprobe(&busb,IDtable);
^
/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.c:23:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token
busb.disconnect=ttldisconnect;
^
/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.c: In function ‘init_module’:
/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.c:27:1: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
int k=usb_register(&busb);
^
scripts/Makefile.build:301: recipe for target '/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.o' failed
make[2]: *** [/home/guru/Desktop/Linuxx/Drivers/USB/check/chc.o] Error 1
Makefile:1524: recipe for target '_module_/home/guru/Desktop/Linuxx/Drivers/USB/check' failed
make[1]: *** [_module_/home/guru/Desktop/Linuxx/Drivers/USB/check] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.10.0-28-generic'
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
当这个有效时,
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/usb.h>
struct usb_device_id IDtable[]={ // Defined in "/include/linux/mod_devicetable.h" which is *included* into "linux/usb.h"
{USB_DEVICE(0x067b,0x2303)},
{}
};
int ttlprobe(struct usb_interface *interface, const struct usb_device_id *id)
{
printk(KERN_INFO "Probed (%04X:%04X) device\n", id->idVendor, id->idProduct);
printk(KERN_INFO "num_altsetting : %d\n",interface->num_altsetting);
return 0;
}
void ttldisconnect(struct usb_interface *interface)
{
printk(KERN_INFO "Device disconnected \n");
}
struct usb_driver DetectingUSB={ // Defined in /include/linux/usb.h
.name="USBtoTTL",
.id_table=IDtable,
.probe=ttlprobe,
.disconnect=ttldisconnect,
};
int init_module(void){
printk(KERN_ALERT "intimodule exec Trying to register to USB CORE \n");
int k=usb_register(&DetectingUSB);
printk(KERN_INFO "retuern value of usb_register_driver : %d\n",k);
pr_info("name = %s\n", THIS_MODULE->name);
pr_info("name = %s\n", KBUILD_MODNAME);
return k;
}
void cleanup_module(void){
printk(KERN_ALERT "Trying to de-register to USB CORE");
return usb_deregister(&DetectingUSB);
}
MODULE_LICENSE("GPL");
为什么我不能单独初始化成员字段?!
答案1
您无法在函数外部以这种方式初始化字段成员。如果您确实想这样做,则需要将初始化代码移至init_module
函数内。
还要注意的是你的
busb.probe=ttlprobe(&busb,IDtable);
会将结果分配给ttlprobe(&busb, IDtable)
to probe
,而不是预期的指向函数的指针。
使用初始化块,这就是这里所期望的。