Device Driver's Probe Routine

本文详细解析了设备驱动中probe()函数的调用流程及参数来源,并以TCC UART设备驱动为例,阐述了从驱动注册到probe()函数被调用的具体过程。

This post tries to answer two questions:

  • Who calls device driver’s probe() routine, and when?
  • Where is probe() parameter from?

  • Who calls device driver’s probe() routine, and when?

The story started from where the driver was registed.

TCC UART device driver (drivers/serial/tcc_serial.c), For example, its entry point defined like below:

        static int __init tcc_seril_modinit(void)
        {
            ...
            platform_driver_register(drv);
            ...
        }

        module_init(tcc_serial_moinit);

The function call stack thereafter is:
1. XXX_driver_register(drv)
2. driver_register(&drv->driver);
3. bus_add_driver(drv);
4. driver_attach(drv);
5. bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
6. __driver_attach(struct device *dev, void *data);
7. driver_probe_device(drv, dev);
8. really_probe(dev, drv)
{

ret = drv->probe(dev);

}

Now it’s clear that the driver’s register function eventually called
driver’s probe() routine.

And, the “__init” symbol denotes this happened on kernel start up.


  • Where is probe() parameter from?
    The XXX_driver_register() passes a single parameter, drv. In case of TCC serial driver, its type is struct platform_driver.However, the driver’s probe() routine get a parameter dev, which type is struct platform_device.

Go through above functions call stack, we have noticed that from step 1 to step 4, the parameter is struct platform_driver; from step 6 to step 8, there is at least one struct platform_device parameter.
The step 5, however, references member struct bus_type of the driver.
The bus found(match) the driver’s partnership, the device, and passes struct device to __driver_attach.

NOTE:
Registing device MUST be prior to the driver’s registration.

The camera module init routine, for example:
int __init camera_core_init(void)
{
    platform_device_register(&camera_core_device);
    platform_driver_register(&camera_core_driver);
    return 0;
}

In the case of TCC serial device driver, the dev parameter of probe() is exact the data passed to platform_device_register().
In arch/arm/mach-tcc8900/devices.c

platform_device_register(&tcc8900_uart0_device); 

static struct resource uart0_resources[] = {
/* PA -> VA */
    [0] = {
        .start  = 0xF0532000,
        .end    = 0xF05320FF,
        .flags  = IORESOURCE_MEM,
    },
    [1] = {
        .start  = INT_UART0,
        .end    = INT_UART0,
        .flags  = IORESOURCE_IRQ,
    },
};

static struct platform_device tcc8900_uart0_device = {
    .name           = "tcc8900-uart",
    .id             = 0,
    .resource       = uart0_resources,
    .num_resources  = ARRAY_SIZE(uart0_resources),
};  
appyorangedog@happyorangedog-Legion-Y9000P-IAX10:~/桌面$ dmesg | grep -i -E 'nvidia|pci|02:00.0' | tail -n 50 [19757.401402] nvidia-nvlink: Nvlink Core is being initialized, major device number 508 [19757.402388] nvidia 0000:02:00.0: vgaarb: changed VGA decodes: olddecodes=none,decodes=none:owns=none [19757.402416] NVRM: The NVIDIA GPU 0000:02:00.0 (PCI ID: 10de:2d59) NVRM: NVIDIA 470.256.02 driver release. NVRM: Please see 'Appendix A - Supported NVIDIA GPU Products' NVRM: specific graphics driver download page at www.nvidia.com. [19757.402472] nvidia: probe of 0000:02:00.0 failed with error -1 [19757.402492] NVRM: The NVIDIA probe routine failed for 1 device(s). [19757.402493] NVRM: None of the NVIDIA devices were initialized. [19757.403351] nvidia-nvlink: Unregistered the Nvlink Core, major device number 508 [19757.683000] nvidia-nvlink: Nvlink Core is being initialized, major device number 508 [19757.684018] nvidia 0000:02:00.0: vgaarb: changed VGA decodes: olddecodes=none,decodes=none:owns=none [19757.684044] NVRM: The NVIDIA GPU 0000:02:00.0 (PCI ID: 10de:2d59) NVRM: NVIDIA 470.256.02 driver release. NVRM: Please see 'Appendix A - Supported NVIDIA GPU Products' NVRM: specific graphics driver download page at www.nvidia.com. [19757.684094] nvidia: probe of 0000:02:00.0 failed with error -1 [19757.684112] NVRM: The NVIDIA probe routine failed for 1 device(s). [19757.684113] NVRM: None of the NVIDIA devices were initialized. [19757.684375] nvidia-nvlink: Unregistered the Nvlink Core, major device number 508 [19758.122468] nvidia-nvlink: Nvlink Core is being initialized, major device number 508 [19758.123418] nvidia 0000:02:00.0: vgaarb: changed VGA decodes: olddecodes=none,decodes=none:owns=none [19758.123445] NVRM: The NVIDIA GPU 0000:02:00.0 (PCI ID: 10de:2d59) NVRM: NVIDIA 470.256.02 driver release. NVRM: Please see 'Appendix A - Supported NVIDIA GPU Products' NVRM: specific graphics driver download page at www.nvidia.com. [19758.123501] nvidia: probe of 0000:02:00.0 failed with error -1 [19758.123521] NVRM: The NVIDIA probe routine failed for 1 device(s). [19758.123521] NVRM: None of the NVIDIA devices were initialized. [19758.123921] nvidia-nvlink: Unregistered the Nvlink Core, major device number 508 [19758.416138] nvidia-nvlink: Nvlink Core is being initialized, major device number 508 [19758.417011] nvidia 0000:02:00.0: vgaarb: changed VGA decodes: olddecodes=none,decodes=none:owns=none [19758.417035] NVRM: The NVIDIA GPU 0000:02:00.0 (PCI ID: 10de:2d59) NVRM: NVIDIA 470.256.02 driver release. NVRM: Please see 'Appendix A - Supported NVIDIA GPU Products' NVRM: specific graphics driver download page at www.nvidia.com. [19758.417085] nvidia: probe of 0000:02:00.0 failed with error -1 [19758.417102] NVRM: The NVIDIA probe routine failed for 1 device(s). [19758.417103] NVRM: None of the NVIDIA devices were initialized. [19758.418271] nvidia-nvlink: Unregistered the Nvlink Core, major device number 508 [19758.664889] nvidia-nvlink: Nvlink Core is being initialized, major device number 508 [19758.665906] nvidia 0000:02:00.0: vgaarb: changed VGA decodes: olddecodes=none,decodes=none:owns=none [19758.665935] NVRM: The NVIDIA GPU 0000:02:00.0 (PCI ID: 10de:2d59) NVRM: NVIDIA 470.256.02 driver release. NVRM: Please see 'Appendix A - Supported NVIDIA GPU Products' NVRM: specific graphics driver download page at www.nvidia.com. [19758.666002] nvidia: probe of 0000:02:00.0 failed with error -1 [19758.666025] NVRM: The NVIDIA probe routine failed for 1 device(s). [19758.666026] NVRM: None of the NVIDIA devices were initialized. [19758.666742] nvidia-nvlink: Unregistered the Nvlink Core, major device number 508
07-22
在 PetaLinux 中确认驱动程序是否成功探测(probe)通常涉及以下几个方面: 1. **查看 dmesg 日志** 驱动程序的探测过程会通过 `printk` 输出到内核日志中。可以在目标设备上运行以下命令来查看日志: ```bash dmesg | grep -i "your_driver_name" ``` 如果驱动成功探测,通常可以看到类似 `your_driver_name: probe succeeded` 的信息。 2. **检查 sysfs 或 devtmpfs 节点** 当驱动成功注册并探测设备后,系统会在 `/sys/class/` 或 `/dev/` 目录下创建相应的设备节点。例如: ```bash ls /dev/your_device_node ``` 同时也可以检查 `/sys/class/your_class_name/` 下是否存在对应的设备目录。 3. **使用 modinfo 查看模块信息** 如果驱动是以模块形式加载的,可以通过以下命令查看模块是否已经正确加载: ```bash modinfo your_driver_module.ko ``` 确认模块信息后,再使用 `lsmod` 查看该模块是否出现在已加载模块列表中: ```bash lsmod | grep -i "your_driver_module" ``` 4. **查看 platform_device 和 platform_driver 匹配情况** 在内核启动过程中,`platform_bus` 会尝试将设备和驱动进行匹配。如果驱动和设备树中的设备匹配成功,则会调用 `probe` 函数。可以在 `dmesg` 中查找 `platform` 总线相关的输出,例如: ```bash dmesg | grep -i "platform" ``` 正常情况下会看到类似 `platform your_device_name: driver bound to device` 的信息。 5. **在驱动代码中添加调试信息** 在驱动的 `probe` 函数中加入 `printk` 打印语句,确保其被调用。例如: ```c static int your_driver_probe(struct platform_device *pdev) { printk(KERN_INFO "Your Driver: Probing device\n"); // Probe logic here return 0; } ``` 编译并加载驱动后,查看 `dmesg` 是否输出了该信息[^1]。 6. **使用 devicetree 匹配检查** 确保设备树中定义的兼容性字符串(compatible)与驱动中定义的 `.of_match_table` 完全一致。如果两者不匹配,驱动将不会被调用。例如,在驱动中: ```c static const struct of_device_id your_driver_of_match[] = { { .compatible = "xlnx,your-device-compatible-string" }, {} }; MODULE_DEVICE_TABLE(of, your_driver_of_match); ``` 并在设备树中: ```dts your_device_node: your_device@address { compatible = "xlnx,your-device-compatible-string"; reg = <0x0 0x1000>; }; ``` 7. **检查驱动是否被静态编入或动态加载** 如果驱动是静态编入内核的,它将在系统启动时自动加载并尝试探测设备。如果是模块化驱动,则需要手动加载: ```bash insmod your_driver_module.ko ``` 然后再查看 `dmesg` 输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值