Linux驅動 probe函數調用

來源:互聯網
上載者:User

參考:

http://blog.chinaunix.net/space.php?uid=15887868&do=blog&id=2758294

http://www.cnblogs.com/hoys/archive/2011/04/01/2002299.html
1,driver_register把驅動註冊到匯流排

/** * driver_register - register driver with bus * @drv: driver to register * We pass off most of the work to the bus_add_driver() call, * since most of the things we have to do deal with the bus * structures. */int driver_register(struct device_driver *drv){……………………if ((drv->bus->probe && drv->probe) ||    (drv->bus->remove && drv->remove) ||    (drv->bus->shutdown && drv->shutdown))ret = bus_add_driver(drv);//把drv驅動,註冊到匯流排……………………}

2,驅動註冊到匯流排的實現函數

/** * bus_add_driver - Add a driver to the bus.        -----在匯流排上加入一個驅動 * @drv: driver. */int bus_add_driver(struct device_driver *drv){………………if (drv->bus->p->drivers_autoprobe) {error = driver_attach(drv);      //驅動的匹配函數………………}

3,driver_attach()

/** * driver_attach - try to bind driver to devices. * @drv: driver. * * Walk the list of devices that the bus has on it and try to * match the driver with each one.  If driver_probe_device() * returns 0 and the @dev->driver is set, we've found a * compatible pair. */int driver_attach(struct device_driver *drv){return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);//注意這裡的__driver_attach}EXPORT_SYMBOL_GPL(driver_attach);

上面真正起作用的是__driver_attach:

static int __driver_attach(struct device *dev, void *data){struct device_driver *drv = data;/* * Lock device and try to bind to it. We drop the error * here and always return 0, because we need to keep trying * to bind to devices and some drivers will return an error * simply if it didn't support the device. * * driver_probe_device() will spit a warning if there * is an error. */if (!driver_match_device(drv, dev))return 0;if (dev->parent)/* Needed for USB */device_lock(dev->parent);device_lock(dev);if (!dev->driver)driver_probe_device(drv, dev);//看下這個函數的實現過程device_unlock(dev);if (dev->parent)device_unlock(dev->parent);return 0;}int driver_probe_device(structdevice_driver *drv, struct device *dev){...//1.先是判斷bus是否match:if (drv->bus->match && !drv->bus->match(dev, drv))   goto done;//2.再具體執行probe:ret = really_probe(dev, drv);...}

4,really_probe是我們真正要找的函數

static int really_probe(struct device *dev, struct device_driver *drv){……………………//1.先是調用的驅動所屬匯流排的probe函數:if (dev->bus->probe) {ret = dev->bus->probe(dev);if (ret)goto probe_failed;} //2.再調用的驅動中的probe函數:else if (drv->probe) {ret = drv->probe(dev);if (ret)goto probe_failed;}driver_bound(dev);ret = 1;pr_debug("bus: '%s': %s: bound device %s to driver %s\n", drv->bus->name, __func__, dev_name(dev), drv->name);//這個資訊可以看到我們註冊的總 驅動、裝置和匯流排  資訊goto done;………………}

列印出的驅動裝置資訊如:

………………

[    0.588087] bus: 'platform': really_probe: bound device power.0to driverpower  //匯流排:platform  裝置:power.0  驅動:power
[    0.661226] bus: 'platform': really_probe: bound device s3c24xx-pwm.0 to driver s3c24xx-pwm
[    0.678552] bus: 'platform': really_probe: bound device s3c24xx-pwm.1 to driver s3c24xx-pwm
[    0.695971] bus: 'platform': really_probe: bound device s3c24xx-pwm.2 to driver s3c24xx-pwm
[    0.713389 bus: 'platform': really_probe: bound device s3c24xx-pwm.3 to driver
……………………

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.