设备驱动-i2c驱动-i2c_add_driver-driver_register-probe函数调用
上一篇: module_i2c_diver 声明到 i2c_add_driver
https://www.cnblogs.com/zhangzhiwei122/p/16125079.html
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 传递参数个数不一样;