Who calls the probe() of driver

前端 未结 6 606
北海茫月
北海茫月 2020-12-12 14:35

How does probe() call gets called? Who calls it? As per my understanding, __init() registers driver and then somehow probe()

相关标签:
6条回答
  • 2020-12-12 15:00

    I have prepared a graph that traces platform drive's probe function. You are working with I2C driver which AFAIK is a platform driver. I hope this helps you to trace the issue.

    Also, look into following link to see the discussion on kernelnewbies.

    https://www.mail-archive.com/kernelnewbies%40kernelnewbies.org/msg12423.html

    0 讨论(0)
  • 2020-12-12 15:01

    @iSegFault : 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

    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

    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.

    One link that will surely clear your doubt. P.S link

    In kernel 2.4.29, i can show you that how does probe happen ? Please see below (File name: 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

    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

    2. http://www.programering.com/a/MjNwcTMwATM.html

    3. http://www.linuxjournal.com/article/6717

    4. http://www.developermemo.com/2943157/

    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

    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:

    File Name: 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;
    }
    
    0 讨论(0)
  • 2020-12-12 15:10

    Long story short: the probe() function of the driver is called as a result of calling the register_driver for that specific bus. More precisely, it's called by the probe() of that bus_type structure. In your case: i2c_bus_type.

    Here's the call chain in your I2C case:

    • i2c_register_driver
    • driver_register
    • bus_add_driver
    • driver_attach
    • __driver_attach (for your device)
    • driver_probe_device
    • really_probe
    • i2c_device_probe (this is what dev->bus->probe is for an i2c driver)
    • your_probe_function
    0 讨论(0)
  • 2020-12-12 15:12

    Probe function will be called for every interface of the detected device except the ones which are already registered.

    0 讨论(0)
  • 2020-12-12 15:15

    Probe function will gets called when name will match form drivers structure to device structure. Below mentioned eg for both driver and device structure.

    1: Driver Structure

     static struct driver your_driver = {
            .driver = {
                  .name = YUR_NAME,
    
                      },
    

    eg.

    static struct i2c_driver l3gd20_driver = {
            .driver = {
    
                            .name = l3gd20_gyr,
    
                       }
    

    2: Device Structure

    static structure device  your_devices = {
                  .name = YUR_NAME,
                  },
    

    eg.

    static struct i2c_board_info mxc_i2c2_board_info[] __initdata = {
                    {
                    I2C_BOARD_INFO("l3gd20_gyr", 0x6a),
                     },
    

    Note: When name(l3gd20_gyr) from driver and device will match your probe will gets call.

    0 讨论(0)
  • 2020-12-12 15:19

    Lets consider an example of a platform device driver:

    1. The starting trigger function for the driver->probe() callback is the module_init() macro called while loading the driver; this macro is defined in include/linux/module.h.

    2. module_init(my_driver_init) has the callback to my_driver_init() function. my_driver_init() function should have a call to platform_driver_register(my_driver)

    3. platform_driver_register(my_driver) assigns my_driver -> probe() handle to generic drv -> probe() and calls the driver_register(my_driver) function.

    4. driver_register(my_driver) function adds my_driver to the platform bus and calls driver_attach() function.

    5. In the same way, even the platform_device needs to attach to the platform bus.

    6. Finally, only if the driver_match_device() returns success based on the .name & .id_table of the driver matches in the platform devices list that comes either from ACPI/DTS, then the driver_probe_device() gets called that has the drv->probe() callback.

    0 讨论(0)
提交回复
热议问题