1. 程式人生 > 其它 >裝置驅動-i2c驅動-i2c_add_driver-driver_register-probe函式呼叫

裝置驅動-i2c驅動-i2c_add_driver-driver_register-probe函式呼叫

 

i2c_add_driver ->i2c_register_driver

linux/include/linux/i2c.h

 853/* use a define to avoid include chaining to get THIS_MODULE */
 854#define i2c_add_driver(driver) \
 855        i2c_register_driver(THIS_MODULE, driver)
 856

drivers/i2c/i2c-core-base.c

1780/*
1781 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1782 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1783 
*/ 1784 1785int i2c_register_driver(struct module *owner, struct i2c_driver *driver) 1786{ 1787 int res; 1788 1789 /* Can't register until after driver model init */ 1790 if (WARN_ON(!is_registered)) 1791 return -EAGAIN; 1792 1793 /* add the driver to the list of i2c drivers in the driver core
*/ 1794 driver->driver.owner = owner; 1795 driver->driver.bus = &i2c_bus_type; 1796 INIT_LIST_HEAD(&driver->clients); 1797 1798 /* When registration returns, the driver core 1799 * will have called probe() for all matching-but-unbound devices. 1800
*/ 1801 res = driver_register(&driver->driver); 1802 if (res) 1803 return res; 1806 1807 /* Walk the adapters that are already present */ 1808 i2c_for_each_dev(driver, __process_new_driver); 1809 1810 return 0; 1811} 1812EXPORT_SYMBOL(i2c_register_driver);

1790 - is_registered 在 i2c_init 裡面賦值為true ; i2c_init 函式在  postcore_initcall 組初始化。這這個後面,才可以 i2c_add_driver 註冊驅動 ; 

1794 ·1795 把 i2c_driver 內部的 driver 的 owner 賦值,並把 bus 賦值為 i2c_bus_type ;  之後

1801 呼叫 driver_register 將 i2c_driver 內部的 device_driver 交給 driver 框架去初始化  ;

 

driver_register 

drivers/base/driver.c

 147int driver_register(struct device_driver *drv)
 148{
 149        int ret;
 150        struct device_driver *other;
 151
 152        if (!drv->bus->p) {
 153                pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n",
 154                           drv->name, drv->bus->name);
 155                return -EINVAL;
 156        }
 157
 158        if ((drv->bus->probe && drv->probe) ||
 159            (drv->bus->remove && drv->remove) ||
 160            (drv->bus->shutdown && drv->shutdown))
 161                pr_warn("Driver '%s' needs updating - please use "
 162                        "bus_type methods\n", drv->name);
 163
 164        other = driver_find(drv->name, drv->bus);
 165        if (other) {
 166                pr_err("Error: Driver '%s' is already registered, "
 167                        "aborting...\n", drv->name);
 168                return -EBUSY;
 169        }
 170
 171        ret = bus_add_driver(drv);
 172        if (ret)
 173                return ret;
 174        ret = driver_add_groups(drv, drv->groups);
 175        if (ret) {
 176                bus_remove_driver(drv);
 177                return ret;
 178        }
 179        kobject_uevent(&drv->p->kobj, KOBJ_ADD);
 180
 181        return ret;
 182}
 183EXPORT_SYMBOL_GPL(driver_register);

152 ~ 156 檢查,drv 的bus 必須不為空。 否則報錯; 

158 ~ 162 ,bus->probe 和 drv->probe 同時存在 ; bus->remove & drv->remove ; bus->shutdown & drv->shotdown 同時存在,警告

164 ~ 169 檢查是否已經註冊過了 這個drv ,如果已經註冊過,就 報錯; 

171 ~ 173  呼叫 bus_add_driver 新增 drv ( 下面詳述)

 

174 ~ 178 給drv 加入到 groups 裡面 ; 

179 kobject uevent 事件觸發 ; 

 

bus_add_driver 

drivers/base/bus.c

 594int bus_add_driver(struct device_driver *drv)
 595{
 596        struct bus_type *bus;
 597        struct driver_private *priv;
 598        int error = 0;


 606        priv = kzalloc(sizeof(*priv), GFP_KERNEL);

 611        klist_init(&priv->klist_devices, NULL, NULL);
 612        priv->driver = drv;
 613        drv->p = priv;
 614        priv->kobj.kset = bus->p->drivers_kset;
 615        error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,
 616                                     "%s", drv->name);
 617        if (error)
 618                goto out_unregister;
 619
 620        klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);
 621        if (drv->bus->p->drivers_autoprobe) {
 622                error = driver_attach(drv);
 623                if (error)
 624                        goto out_unregister;
 625        }



 658}

606 ~ 618  -  申請空間,初始化 driver_private 物件。 把這個物件 裡面的 driver 賦值為 drv ; 再把這個 private 物件地址放在 drv->p  裡面; 然後把這個 private 物件,通過內部的 kobj ,加入到核心 kobject 系統中 ;

621 ~ 622 - 如果 drv 所屬 bus 的private 物件中的 drivers_autoprobe 為true ; 呼叫 driver_attach ( drv ) 函式,將 驅動 attach 到 bus 上面 ;

 

driver_attach

drivers/base/dd.c

1104int driver_attach(struct device_driver *drv)
1105{
1106        return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
1107}

 

__driver_attach

drivers/base/dd.c

1044static int __driver_attach(struct device *dev, void *data)
1045{
1046        struct device_driver *drv = data;
1047        int ret;

1059        ret = driver_match_device(drv, dev);
1060        if (ret == 0) {
1061                /* no match */
1062                return 0;
1063        } else if (ret == -EPROBE_DEFER) {
1064                dev_dbg(dev, "Device match requests probe deferral\n");
1065                driver_deferred_probe_add(dev);
1066        } else if (ret < 0) {
1067                dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1068                return ret;
1069        } /* ret > 0 means positive match */
1070
1071        if (driver_allows_async_probing(drv)) {
1080                device_lock(dev);
1081                if (!dev->driver) {
1082                        get_device(dev);
1083                        dev->p->async_driver = drv;
1084                        async_schedule_dev(__driver_attach_async_helper, dev);
1085                }
1086                device_unlock(dev);
1087                return 0;
1088        }
1089
1090        device_driver_attach(drv, dev);
1092        return 0;
1093}

1059 ~ 1069 - 使用 driver_match_device 對 drv 和dev 進行匹配,並對返回值 等於 0  ( 不匹配 ) 和 小於 0 ( 匹配失敗)進行處理。

       driver_match_device 就會 使用 driver 中的 of_match_table 或者  id_table (後面詳述 ); 

 

1071 ~ 1087 ~ drv 和 dev 匹配到了,如果 drv 允許 非同步 probing ,  並且 dev->driver 也為 空 (即 dev 沒有匹配的 drv ) , 就   dev->p->async_driver = drv , 然後安排非同步的 dev attach drv 事件 ;然後 return ; 

 

1090  到這兒,就是不支援  非同步 dev drv 關聯; 就呼叫 device_deiver_attach 直接關聯  (後面詳述)

 

driver_match_device 

drivers/base/base.h

 140static inline int driver_match_device(struct device_driver *drv,
 141                                      struct device *dev)
 142{
 143        return drv->bus->match ? drv->bus->match(dev, drv) : 1;
 144}

呼叫匯流排的match匹配函式 .match = i2c_device_match,

 

 i2c_device_match

drivers/i2c/i2c-core-base.c

  94static int i2c_device_match(struct device *dev, struct device_driver *drv)
  95{
  96        struct i2c_client       *client = i2c_verify_client(dev);
  97        struct i2c_driver       *driver;
  98
  99
 100        /* Attempt an OF style match */
 101        if (i2c_of_match_device(drv->of_match_table, client))
 102                return 1;
 103
 104        /* Then ACPI style match */
 105        if (acpi_driver_match_device(dev, drv))
 106                return 1;
 107
 108        driver = to_i2c_driver(drv);
 109
 110        /* Finally an I2C match */
 111        if (i2c_match_id(driver->id_table, client))
 112                return 1;
 113
 114        return 0;
 115}

 

drivers/i2c/i2c-core-of.c

 211const struct of_device_id
 212*i2c_of_match_device(const struct of_device_id *matches,
 213                     struct i2c_client *client)
 214{
 215        const struct of_device_id *match;
 216
 217        if (!(client && matches))
 218                return NULL;
 219
 220        match = of_match_device(matches, &client->dev);
 221        if (match)
 222                return match;
 223
 224        return i2c_of_match_device_sysfs(matches, client);
 225}
 226EXPORT_SYMBOL_GPL(i2c_of_match_device);

使用 of_match_device 將 device 和 of_device_id 陣列進行匹配,匹配成功時,返回 匹配到的 項的 指標 ; 

i2c_client -> dev  時一個 device 物件, device 物件裡面 由 of_node 指向 裝置對應的 device_node 物件 ; device_node 物件 是 解析 dts 創建出來的; 

drivers/i2c/i2c-core-base.c

  79const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
  80                                                const struct i2c_client *client)
  81{
  82        if (!(id && client))
  83                return NULL;
  84
  85        while (id->name[0]) {
  86                if (strcmp(client->name, id->name) == 0)
  87                        return id;
  88                id++;
  89        }
  90        return NULL;
  91}
  92EXPORT_SYMBOL_GPL(i2c_match_id);

id->name 和 client->name 匹配了,也算 匹配; 

總之,就是 通過driver 中的 of_match_table 或 id_table 進行匹配; 成功返回 1 ; 

 

device_deiver_attach 

drivers/base/dd.c

1002int device_driver_attach(struct device_driver *drv, struct device *dev)
1003{
1004        int ret = 0;
1005
1006        __device_driver_lock(dev, dev->parent);
1007
1008        /*
1009         * If device has been removed or someone has already successfully
1010         * bound a driver before us just skip the driver probe call.
1011         */
1012        if (!dev->p->dead && !dev->driver)
1013                ret = driver_probe_device(drv, dev);
1014
1015        __device_driver_unlock(dev, dev->parent);
1016
1017        return ret;
1018}
1019

 

driver_probe_device

drivers/base/dd.c

 720int driver_probe_device(struct device_driver *drv, struct device *dev)
 721{
 722        int ret = 0;

 735        if (initcall_debug)
 736                ret = really_probe_debug(dev, drv);
 737        else
 738                ret = really_probe(dev, drv);

 745        return ret;
 746}

really_probe

drivers/base/dd.c

 494static int really_probe(struct device *dev, struct device_driver *drv)
 495{
 496        int ret = -EPROBE_DEFER;


 553        if (dev->bus->probe) {
 554                ret = dev->bus->probe(dev);
 555                if (ret)
 556                        goto probe_failed;
 557        } else if (drv->probe) {
 558                ret = drv->probe(dev);
 559                if (ret)
 560                        goto probe_failed;
 561        }


 601        driver_bound(dev);
 602        ret = 1;
 603        pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
 604                 drv->bus->name, __func__, dev_name(dev), drv->name);

553 優先使用  dev->bus->probe 函式, 如果沒有,557 行,就 嘗試 使用 dev 匹配的 driver 的 probe 函式

 

.probe = i2c_device_probe,

i2c_device_probe

drivers/i2c/i2c-core-base.c

 439static int i2c_device_probe(struct device *dev)
 440{
 441        struct i2c_client       *client = i2c_verify_client(dev);


 525        if (driver->probe_new)
 526                status = driver->probe_new(client);
 527        else if (driver->probe)
 528                status = driver->probe(client,
 529                                       i2c_match_id(driver->id_table, client));
 530        else
 531                status = -EINVAL;
 532
 533        if (status)
 534                goto err_detach_pm_domain;
 535
 536        return 0;

 

525 如果driver 有 probe_new 函式,優先呼叫 probe_new 函式, 傳遞引數 client ; 

527 如果 driver 有 probe 函式,嘗試呼叫probe 函式,傳遞 client,  和 driver->id_table 的match 結果; 

 

備註:

really_probe 中的 driver->probe (dev) 傳遞的引數 僅是 原始 的 device  ; 

i2c_device_probe 中的, driver->probe_new 和 driver->probe 傳遞引數個數不一樣;