为什么Linux设备驱动中除了init还需要probe方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5059501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the probe method needed in Linux device drivers in addition to init?
提问by Bandicoot
In the linux kernel, what does the probe()
method, that the driver provides, do? How different is it from the driver's init
function, i.e. why can't the probe()
functions actions be performed in the driver's init
function ?
在linux内核中,probe()
驱动提供的方法是做什么的?它与驱动程序的init
功能有何不同,即为什么不能probe()
在驱动程序的init
功能中执行功能操作?
采纳答案by Eric Seppanen
Different device types can have probe() functions. For example, PCI and USB devices both have probe() functions.
不同的设备类型可以有 probe() 函数。例如,PCI 和USB 设备都有probe() 函数。
If you're talking about PCI devices, I would recommend you read chapter 12 of Linux Device Drivers, which covers this part of driver initialization. USB is covered in chapter 13.
如果您在谈论 PCI 设备,我建议您阅读Linux 设备驱动程序的第 12 章,其中涵盖了驱动程序初始化的这一部分。USB 将在第 13 章中介绍。
Shorter answer, assuming PCI: The driver's init function calls pci_register_driver()
which gives the kernel a list of devices it is able to service, along with a pointer to the probe()
function. The kernel then calls the driver's probe()
function once for each device.
更简短的答案,假设 PCI:驱动程序的 init 函数调用pci_register_driver()
,它为内核提供了一个它能够服务的设备列表,以及一个指向该probe()
函数的指针。然后内核probe()
为每个设备调用一次驱动程序的函数。
This probe function starts the per-device initialization: initializing hardware, allocating resources, and registering the device with the kernel as a block or network device or whatever it is.
这个探测函数开始每个设备的初始化:初始化硬件,分配资源,并将设备注册到内核作为块或网络设备或其他任何东西。
That makes it easier for device drivers, because they never need to search for devices or worry about finding a device that was hot-plugged. The kernel handles that part and notifies the right driver when it has a device for you to handle.
这使得设备驱动程序更容易,因为他们永远不需要搜索设备或担心找到热插拔的设备。内核处理该部分并在有设备供您处理时通知正确的驱动程序。
回答by fbp
Init(void) // runs once when the driver/module is invoked and sets things up for the kernel driver machine.
Init(void) // 当驱动程序/模块被调用并为内核驱动程序机器设置时运行一次。
Probe(*pdev) // is used by the kernel driver machine as needed to detect and install actual devices
Probe(*pdev) // 内核驱动机器根据需要使用它来检测和安装实际设备
回答by Sunil Bojanapally
The drivers xxx_init_module()
function calls pci_register_driver(struct pci_driver *drv)
by passing reference to structure of type pci_driver
. struct pci_driver
is an important structure all PCI drivers should have, which gets initialized with variables like drivers name, table list of PCI devices the driver can support, callback routines for the PCI core subsystem.
驱动程序xxx_init_module()
函数pci_register_driver(struct pci_driver *drv)
通过传递对类型结构的引用来调用pci_driver
。struct pci_driver
是所有 PCI 驱动程序都应该具有的重要结构,它使用驱动程序名称、驱动程序可以支持的 PCI 设备表列表、PCI 核心子系统的回调例程等变量进行初始化。
The drivers pci_driver structure has important member fields listed below:
驱动程序 pci_driver 结构具有下面列出的重要成员字段:
name
– Name to the driver which is unique among all PCI drivers in the kernel. It will appear under/sys/bus/pci/drivers
.pci_device_id
– A table of device identification data consists type of chips this driver supports.probe
– The address ofxxx_probe()
function.remove/suspend/resume/shutdown
– address to the function that the PCI core system calls when PCI device is removed/suspended/resumed/shutdown respectively. Generally used by upper layers for power management.
name
– 在内核中所有 PCI 驱动程序中唯一的驱动程序名称。它将出现在/sys/bus/pci/drivers
.pci_device_id
– 设备标识数据表包含此驱动程序支持的芯片类型。probe
–xxx_probe()
函数地址。remove/suspend/resume/shutdown
– 分别指向PCI设备被移除/挂起/恢复/关闭时PCI核心系统调用的函数。一般由上层用于电源管理。
For more information on how the probing of driver executed from PCI core refer Linux Device Driver Init.
有关如何探测从 PCI 内核执行的驱动程序的更多信息,请参阅Linux 设备驱动程序初始化。
回答by Manish
@Bandicoot: probe() will be called to make sure that the device exist and the functionality is fine.If device is not hot-pluggable, functionality of probe() can be put inside init() method.This will reduce driver's run time memory footprint. P.S link
@Bandicoot:将调用probe() 以确保设备存在且功能正常。如果设备不可热插拔,则可以将probe() 的功能放在init() 方法中。这将减少驱动程序的运行时间内存占用。PS链接
Probe() happens at the time of device boot or when device is connected.For a "platform" device the probe function is invoked when a platform device is registered and it's device name matches the name specified on the device driver. P.S link
Probe() 在设备启动时或设备连接时发生。对于“平台”设备,在注册平台设备并且其设备名称与设备驱动程序上指定的名称匹配时调用探测函数。PS链接
The i2c_detect function probes the I2C adapter, looking for the different addresses specified in the addr_data structure. If a device is found, the chip_detect function then is called. P.S link.
i2c_detect 函数探测 I2C 适配器,寻找在 addr_data 结构中指定的不同地址。如果找到设备,则调用chip_detect 函数。PS链接。
One link that will surely clear your doubt. P.S link
一个链接肯定会消除您的疑虑。PS链接
In kernel 2.4.29, i can show you that how does probe happen ? Please see below (File name: drivers/acorn/char/pcf8583.c)
在内核 2.4.29 中,我可以向您展示探测是如何发生的?请参见下文(文件名:drivers/acorn/char/pcf8583.c)
static struct i2c_driver pcf8583_driver = {
name: "PCF8583",
id: I2C_DRIVERID_PCF8583,
flags: I2C_DF_NOTIFY,
attach_adapter: pcf8583_probe, /* This will be called from i2c-core.c P.S see below function i2c_add_driver()*/
detach_client: pcf8583_detach,
command: pcf8583_command
};
};
File Name: drivers/i2c/i2c-core.c
文件名:drivers/i2c/i2c-core.c
int i2c_add_driver(struct i2c_driver *driver)
{
........................
........................
/* now look for instances of driver on our adapters
*/
if (driver->flags& (I2C_DF_NOTIFY|I2C_DF_DUMMY)) {
for (i=0;i<I2C_ADAP_MAX;i++)
if (adapters[i]!=NULL)
/* Ignore errors */
driver->attach_adapter(adapters[i]); /*This is a location from where probe is called. Pointer **driver** is of type **pcf8583_driver** which you have passed into this function*/
}
ADAP_UNLOCK();
return 0;
}
Few important links:
几个重要的链接:
1) http://www.slideshare.net/varunmahajan06/i2c-subsystem-in-linux2624
1) http://www.slideshare.net/varunmahajan06/i2c-subsystem-in-linux2624
2) http://www.programering.com/a/MjNwcTMwATM.html
2) http://www.programering.com/a/MjNwcTMwATM.html
3) http://www.linuxjournal.com/article/6717
3) http://www.linuxjournal.com/article/6717
4) http://www.developermemo.com/2943157/
4) http://www.developermemo.com/2943157/
5) http://free-electrons.com/doc/kernel-architecture.pdf
5) http://free-electrons.com/doc/kernel-architecture.pdf
6) http://www.techques.com/question/1-3014627/Probe-problem-when-writing-a-I2C-device-driver
6) http://www.techques.com/question/1-3014627/Probe-problem-when-writing-a-I2C-device-driver
In PCI for kernel-2.4.29, it gets called when vendor and device id are identified. PCI bus driver do this for you. Please see below code:
在内核 2.4.29 的 PCI 中,它在识别供应商和设备 ID 时被调用。PCI 总线驱动程序为您执行此操作。请看下面的代码:
File Name: drivers/pci/pci.c
文件名:drivers/pci/pci.c
static int pci_announce_device(struct pci_driver *drv, struct pci_dev *dev)
{
const struct pci_device_id *id;
int ret = 0;
if (drv->id_table) {
id = pci_match_device(drv->id_table, dev); /* check for device presence*/
if (!id) {
ret = 0;
goto out;
}
} else
id = NULL;
dev_probe_lock();
if (drv->probe(dev, id) >= 0) { /* This is a location from where probe is called*/
dev->driver = drv;
ret = 1;
}
dev_probe_unlock();
out:
return ret;
}
回答by Y?v??j S????
Probing is done when the probe() method is called by function pointer inside a structure which is used to device binding with default or custom platform data regarding to device. drivers uses lots of information about device so that the probing provides such information to drivers when an entry in the id_table name field matches device name will be probe.
当probe() 方法由结构内的函数指针调用时完成,该结构用于设备绑定与设备有关的默认或自定义平台数据。驱动程序使用有关设备的大量信息,以便当 id_table 名称字段中的条目与设备名称匹配时,探测将此类信息提供给驱动程序。
回答by Liyong Zhou
Linux kernel uses a hardware devicematching a software device driverprocess. init
is called very early, and registers probe
function and a hardware device name like "taiko_sound_card", to the kernel. This is to tell kernel that "I am sw driver for this device of this name". When the kernel goes through hw devices (device tree or bus enum), and finds a match, it will call your registered probe
function. Now your sw device driver ownsthe hw device.
Linux 内核使用与软件设备驱动进程相匹配的硬件设备。很早就被调用,并将函数和硬件设备名称(如“taiko_sound_card”)注册到内核。这是告诉内核“我是此名称设备的 sw 驱动程序”。当内核通过硬件设备(设备树或总线枚举)并找到匹配项时,它会调用您注册的函数。现在您的 sw 设备驱动程序拥有硬件设备。init
probe
probe
If no matching device is found, your probe may never get called. This is why typically init
is tiny and probe
does all the init work.
如果没有找到匹配的设备,您的探测器可能永远不会被调用。这就是为什么通常init
很小并且probe
完成所有初始化工作的原因。