问题描述
在遵循本教程以及其他内容的同时([ http://tali.admingilde.org/linux-docbook/writing_usb_driver.pdf][1] )并阅读linux设备驱动程序书中的某些章节,我无法在probe函数中获得pr_debug语句来显示dmesg中的任何输出.
While following, among others, this tutorial ([http://tali.admingilde.org/linux-docbook/writing_usb_driver.pdf][1]) and reading certain chapters in the linux device drivers book, I cannot get the pr_debug statements in the probe function to show any output in dmesg.
这是我的代码:
#include <linux/module.h> /*included for all kernel modules*/
#include <linux/kernel.h> /*included for KERN_DEBUG*/
#include <linux/init.h> /*included for __init and __exit macros*/
#include <linux/usb.h>
#include <linux/usb/input.h>
#include <linux/hid.h>
#define VENDOR_ID 0x0930
#define DEVICE_ID 0x6545
MODULE_LICENSE("GPL");
MODULE_AUTHOR("dev");
MODULE_DESCRIPTION("eusb driver");
static struct usb_device_id eusb_table[] = {
{ USB_DEVICE(VENDOR_ID, DEVICE_ID) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, eusb_table);
static int eusb_probe(struct usb_interface *interface, const struct usb_device_id *id)
{
pr_debug("USB probe function called\n");
return 0;
}
static void eusb_disconnect(struct usb_interface *interface)
{
pr_debug("USB disconnect function called\n");
}
static struct usb_driver eusb_driver = {
//.owner = THIS_MODULE,
.name = "eusb",
.probe = eusb_probe,
.disconnect = eusb_disconnect,
.id_table = eusb_table
};
static int __init eusb_init(void)
{
int result = 0;
pr_debug("Hello world!\n");
result = usb_register(&eusb_driver);
if(result){
pr_debug("error %d while registering usb\n", result);}
else{pr_debug("no error while registering usb\n");}
return 0;
}
static void __exit eusb_exit(void)
{
usb_deregister(&eusb_driver);
pr_debug("Exiting module\n");
}
module_init(eusb_init);
module_exit(eusb_exit);
和makefile:
obj-m := eusb.o
CFLAGS_eusb.o := -DDEBUG
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
制作完成没有错误,在insmod之后,我可以看到lsmod中列出的模块,并且init和exit函数中的pr_debug在dmesg中显示输出.
Make finishes without errors, after insmod I can see the module listed in lsmod and the pr_debug in the init and exit functions show output in dmesg.
但是,在插入设备时,似乎未调用probe函数(或pr_debug语句在dmesg中未显示任何输出).
When inserting the device however the probe function seems to not get called (or the pr_debug statements do not show any output in dmesg).
Dmesg输出:
[ 7777.521236] Hello world!
[ 7777.521264] usbcore: registered new interface driver eusb
[ 7777.521266] no error while registering usb
[ 7780.597087] usb 1-6: USB disconnect, device number 9
[ 7797.686970] usb 1-6: new high-speed USB device number 10 using xhci_hcd
[ 7797.857324] usb 1-6: New USB device found, idVendor=0930, idProduct=6545
[ 7797.857328] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 7797.857330] usb 1-6: Product: DataTraveler 2.0
[ 7797.857331] usb 1-6: Manufacturer: Kingston
[ 7797.857333] usb 1-6: SerialNumber: 08606E6D407FED10571E5067
[ 7797.858787] usb-storage 1-6:1.0: USB Mass Storage device detected
[ 7797.858902] scsi host11: usb-storage 1-6:1.0
[ 7798.931417] scsi 11:0:0:0: Direct-Access Kingston DataTraveler 2.0 PMAP PQ: 0 ANSI: 4
[ 7798.931824] sd 11:0:0:0: Attached scsi generic sg3 type 0
[ 7800.184749] sd 11:0:0:0: [sdc] 60964864 512-byte logical blocks: (31.2 GB/29.0 GiB)
[ 7800.186338] sd 11:0:0:0: [sdc] Write Protect is off
[ 7800.186343] sd 11:0:0:0: [sdc] Mode Sense: 23 00 00 00
[ 7800.187948] sd 11:0:0:0: [sdc] No Caching mode page found
[ 7800.187952] sd 11:0:0:0: [sdc] Assuming drive cache: write through
[ 7800.220477] sdc: sdc1 sdc2 sdc3
[ 7800.225068] sd 11:0:0:0: [sdc] Attached SCSI removable disk
[ 7802.798403] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 7802.799507] ISO 9660 Extensions: RRIP_1991A
我尝试使用另一台设备,尝试使用printk而不是pr_debug.我在SO上发现了几个相同问题的问题,但据我所知,我的代码与答案中的代码几乎/完全相同.
I have tried with another device, tried with printk instead of pr_debug. I found several questions on SO with the same problem, but my code is as far as I can tell almost/completely the same as the code in the answers.
我也尝试过
USB_INTERFACE_INFO(
USB_INTERFACE_CLASS_HID,
USB_INTERFACE_SUBCLASS_BOOT,
USB_INTERFACE_PROTOCOL_KEYBOARD)
代替USB_DEVICE(另一个设备是键盘).
instead of USB_DEVICE (the other device was a keyboard).
我注意到有一些答案是关于platform_driver而不是usb_driver,但我认为这与我无关,因为在教程中从未提及.
I noticed that some answers talk about platform_driver instead of usb_driver, but I think this is not relevant for me, as it is never mentioned in the tutorials.
我要去哪里错了?
推荐答案
您的目的仅仅是出于教育目的吗?否则,驱动程序已经在那里,它被称为usb-storage
.似乎驱动程序首先枚举了您的设备.尝试禁用usb-storage
并再次加载驱动程序.或者最好使用虚拟机(KVM/Qemu)尝试驱动程序.
Is your purpose just educational? Otherwise the driver is already there and it's called usb-storage
. Seems even that driver enumerates your device first. Try to disable usb-storage
and load your driver again. Or better use the virtual machine (KVM / Qemu) to try your drivers.
这篇关于为什么我的内核模块中的probe函数没有被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!