I2C——i2c_driver的注册及probe探测函数调用过程

    在 linux I2C 驱动之----i2c_client 的注册中介绍了i2c_client的注册,现在再来说说i2c_driver的注册过程。

   每一个驱动程序都有 module_init(xxxx_init) 这个语句,它代表系统启动的时候会自动执行 xxxx 这个函数,也就是说驱动的人口函数是由module_init来定义的。当然还有module_exit(XXXX),它代表系统卸载驱动时调用(linux系统允许动态加载卸载驱动),这一部分这不细说了。

       上文提到的 xxxx_init 是驱动的入口函数,在此函数中,我们一般注册驱动的driver,比如我今天说的 i2c_driver:

        static int __init xxxx_init(void)
       {
           
return i2c_add_driver(&xxxx_driver);
       }

其中xxxx_driver就是我们今天的主角 i2c_driver :

      static struct i2c_driver xxxx_driver =
     {
         .driver =
         {
             .owner = THIS_MODULE, 
             .name = "xxxx",
         }, 
        .id_table = xxxx_idtable,
        .probe = xxxx_probe, 
     }

完整的 i2c_driver 非常复杂,上面的定义只是完成了最基本的部分,当然也是必须,也就代表完成了上面的部分你的驱动就可以工作了,当然还有其他的,这里不细说了。i2c_driver 中的driver.name 要和i2c_client一致,因为这是他们配备的一个依据,id_table 是i2c_device_id结构体的一个对象,里面定义了i2c驱动对应设备的i2c地址,probe函数是个非常重要的函数,等下我再来细说。

     现在我来说说 i2c_add_driver 的执行过程。

   (注意:此函数是linux系统i2c 子系统已经为我们做好了的,我们完全不需要了解它的执行过程,只需要明白,当系统中已经注册了和上文提到的i2c_driver  xxxx_driver 对应的i2c_client,所谓对应的是说,name一样,i2c设备的地址也一样,那么i2c_driver 与i2c_client配备成功,接着就会调用i2c_driver 的probe函数,在此函数中可以做一系列的初始化工作。)

     i2c_add_driver 函数只是调用了i2c_register_driver函数,在i2c_register_driver里调用了driver_register(&i2c_driver->driver),

注意 driver_register 是linux系统设备模型里面的函数了,每一类linux设备驱动的注册最终都会调用它,传递的参数也由原来的i2c_driver 变成了 device_driver。driver_register做了一些判断,最后调用了 bus_add_driver,然后调用 driver_attach,然后调用

bus_for_each_dev,这个函数就是搜索总线上所有的device,我们这里是i2c总线,也即搜索i2c总线上的i2c_client,调用__driver_attach判断i2c_driver i2c_client是否配备,再调用driver_match_device,driver_match_device最终调用了bus的match函数,我们这也就是i2c bus的match函数,再调用

i2c_match_id,这里到最后的id 配备了,如果配备成功则正常返回到__driver_attach函数,调用driver_probe_device,再调用really_probe,最后调用bus的probe函数,在bus的probe函数里利用to_i2c_driver 将device_driver转换成了i2c_driver,最后调用了i2c_driver的probe函数,这个部分有点复杂,其中我可能也没说的太清楚,建议用source insight自己跟一边,因为其中有很多是公用的,跟一边后,其他驱动注册也就容易了。

1、driver_register把驱动注册到总线

[cpp]  view plain copy
  1. /**
  2. * driver_register - register driver with bus
  3. * @drv: driver to register
  4. * We pass off most of the work to the bus_add_driver() call,
  5. * since most of the things we have to do deal with the bus
  6. * structures.
  7. */ 
  8. int driver_register(struct device_driver *drv) 
  9. …………………… 
  10.     if ((drv->bus->probe && drv->probe) || 
  11.         (drv->bus->remove && drv->remove) || 
  12.         (drv->bus->shutdown && drv->shutdown)) 
  13.  
  14.     ret = bus_add_driver(drv);//把drv驱动,注册到总线 
  15. …………………… 

2、驱动注册到总线的实现函数

[cpp]  view plain copy
  1. /**
  2. * bus_add_driver - Add a driver to the bus.        -----在总线上加入一个驱动
  3. * @drv: driver.
  4. */ 
  5. int bus_add_driver(struct device_driver *drv) 
  6. ……………… 
  7.     if (drv->bus->p->drivers_autoprobe) { 
  8.         error = driver_attach(drv);      //驱动的匹配函数 
  9. ……………… 

3、driver_attach()

[cpp]  view plain copy
  1. /**
  2. * driver_attach - try to bind driver to devices.
  3. * @drv: driver.
  4. *
  5. * Walk the list of devices that the bus has on it and try to
  6. * match the driver with each one.  If driver_probe_device()
  7. * returns 0 and the @dev->driver is set, we've found a
  8. * compatible pair.
  9. */ 
  10. int driver_attach(struct device_driver *drv) 
  11.     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);//注意这里的__driver_attach 
  12. EXPORT_SYMBOL_GPL(driver_attach); 

上面真正起作用的是__driver_attach:

[cpp]  view plain copy
  1. static int __driver_attach(struct device *dev, void *data) 
  2.     struct device_driver *drv = data; 
  3.  
  4.     /*
  5.      * Lock device and try to bind to it. We drop the error
  6.      * here and always return 0, because we need to keep trying
  7.      * to bind to devices and some drivers will return an error
  8.      * simply if it didn't support the device.
  9.      *
  10.      * driver_probe_device() will spit a warning if there
  11.      * is an error.
  12.      */ 
  13.  
  14.     if (!driver_match_device(drv, dev)) 
  15.         return 0; 
  16.  
  17.     if (dev->parent) /* Needed for USB */ 
  18.         device_lock(dev->parent); 
  19.     device_lock(dev); 
  20.     if (!dev->driver) 
  21.         driver_probe_device(drv, dev);//看下这个函数的实现过程 
  22.     device_unlock(dev); 
  23.     if (dev->parent) 
  24.         device_unlock(dev->parent); 
  25.  
  26.     return 0; 
  27. int driver_probe_device(structdevice_driver *drv, struct device *dev) 
  28. ... 
  29. //1.先是判断bus是否match: 
  30. if (drv->bus->match && !drv->bus->match(dev, drv)) 
  31.    goto done; 
  32. //2.再具体执行probe: 
  33. ret = really_probe(dev, drv); 
  34. ... 

4、really_probe是我们真正要找的函数

[cpp]  view plain copy
  1. static int really_probe(struct device *dev, struct device_driver *drv) 
  2. …………………… 
  3. //1.先是调用的驱动所属总线的probe函数: 
  4.     if (dev->bus->probe) { 
  5.         ret = dev->bus->probe(dev); 
  6.         if (ret) 
  7.             goto probe_failed; 
  8.     }  
  9. //2.再调用的驱动中的probe函数: 
  10.     else if (drv->probe) { 
  11.         ret = drv->probe(dev); 
  12.         if (ret) 
  13.             goto probe_failed; 
  14.     } 
  15.  
  16.     driver_bound(dev); 
  17.     ret = 1; 
  18.     pr_debug("bus: '%s': %s: bound device %s to driver %s\n"
  19.          drv->bus->name, __func__, dev_name(dev), drv->name);//这个信息可以看到我们注册的总 驱动、设备和总线  信息 
  20.     goto done; 
  21. ……………… 
  • 6
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值