Drivers
為什麼我不能單獨初始化結構成員欄位?!(在 USB 設備驅動程序中)
我正在嘗試實現 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");
為什麼我不能單獨初始化成員欄位?!
您不能在函式之外以這種方式初始化欄位成員。如果你真的想這樣做,你需要在
init_module
函式內移動初始化程式碼。另請注意,您的
busb.probe=ttlprobe(&busb,IDtable);
將分配
ttlprobe(&busb, IDtable)
to的結果probe
,而不是指向預期的函式的指針。使用初始化塊,這就是這裡所期望的。