内核驱动框架

来源:互联网 发布:python logging模块 编辑:程序博客网 时间:2024/06/06 15:00

本博客转载于:http://blog.chinaunix.net/uid-24148050-id-153462.html


1.数据结构

这个框架涉及到3个比较重要的数据结构:
   struct bus_type         用来描述总线
   struct device           用来描述设备
   struct device_driver   用来描述设备驱动

有如下拓扑图


 内核中可以有很多的总线bus_type
 总线bus_type上可以有很多的设备device
 每个设备device都有它所对应的驱动device_driver

  1. struct bus_type { //…..省略一些成员
  2.     const char* name;
  3.     struct kset drivers;
  4.     struct kset devices;

  5.     int (*match)(struct device* dev, struct device_driver * drv);
  6.     int (*uevent)(struct device*dev, struct kobj_uevent_env *env);
  7.     int (*probe)(struct device* dev);
  8.     int (*remove)(struct device * dev);
  9.     void (*shutdown)(struct device * dev);

  10.     int (*suspend)(struct device* dev, pm_message_t state);
  11.     int (*suspend_late)(struct device* dev, pm_message_t state);
  12.     int (*resume_early)(struct device* dev);
  13.     int (*resume)(struct device* dev);
  14.     unsigned int drivers_autoprobe:1;
  15. };

  16. struct device {//…..省略一些成员
  17.     struct klist klist_children;
  18.     struct klist_node knode_parent;/* node in sibling list */
  19.     struct klist_node knode_driver;
  20.     struct klist_node knode_bus;
  21.     struct device *parent;
  22.     char bus_id[BUS_ID_SIZE];      /* position on parent bus */

  23.     struct bus_type * bus;          /* type of bus device is on */
  24.     struct device_driver *driver;   /* which driver has allocated this device */
  25.     void *driver_data;             /* data private to the driver */
  26.     void *platform_data;           /* Platform specific data, device core doesn't touch it */
  27. };

  28. struct device_driver { //…..省略一些成员
  29.     const char* name;
  30.     struct bus_type * bus;
  31.     int (*probe)(struct device * dev);
  32.     int (*remove)(struct device * dev);
  33.     void(*shutdown)(struct device * dev);
  34.     int (*suspend)(struct device * dev, pm_message_t state);
  35.     int (*resume)(struct device * dev);
  36. };



按照面向对象的思想,
bus_type虚基类,子类包括 platform_bus_type,mdio_bus_type,i2c_bus_type,pci_bus_type等
device是虚基类,子类包括 platform_device,  phy_device,   i2c_client, pci_device等
device_driver虚基类,子类有platform_driver,  phy_driver,i2c_driver, pci_driver等

2.API


框架有几个关键API:
bus_register(stuct bus_type* bus)         用来注册一条总线bus
device_register(struct device *dev)       把dev加到bus的设备列表中去

driver_register(stuct device_driver *drv)   
    1. 遍历bus上所有的dev,调用 drv->match(dev,drv) 对drv与dev进行比对
    2. 比对成功后调用 dev->bus->probe(dev) 或 drv->probe(dev)
    3. 把驱动drv加入到bus的驱动列表中去
    4. 绑定drv和dev

为了便于使用,内核针对不同总线,对上述API进行了包装,衍生出了另外一批API:
platform_driver_register
platform_device_register

phy_driver_register
mdiobus_register



下面讲将这3个API

  1. bus_register(struct bus_type*bus)
  2.     //没啥说的,此函数最重要一点就是 bus->drivers_autoprobe=1


  1. driver_register(struct device_driver*drv)
  2.     --》bus_add_driver(drv)
  3.     //若bus->drivers_autoprobe = 1
  4.         --》driver_attach(drv)
  5.         //遍历bus下所有dev,调用
  6.             --》__driver_attach(dev,drv)
  7.                 --》driver_probe_device(drv,dev)
  8.             //调用 bus_type.match(dev, drv),如果比对成功就执行
  9.                 --》really_probe(dev,drv)
  10.                     //调用 bus->probe(dev) 或 drv->probe(dev)

3.总线的注册

所有总线的注册几乎内核启动过程时完成的。

start_kernel()
  --》rest_init()
    --》kernel_init()
      --》do_basic_setup()
        --》do_initcalls()
          --》platform_bus_init()--》bus_register(&platform_bus)
          --》mdio_bus_init()    --》bus_register(&mdio_bus_type)
          --》i2c_init()         --》bus_register(&i2c_bus_type)

粉红色的都是全局变量

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 地板买的太暗了怎么办 木地板贴好后发现颜色不好看怎么办 鞋上鞋扣老是开怎么办 虚拟机的win10蓝屏重启怎么办 fs免拆模板专利怎么办 新买的床咯吱响怎么办 实木床一动就响怎么办 cad图纸是斜的怎么办 老房子土墙腻子老掉怎么办 生完孩子子宫大怎么办 刚开的店没人怎么办 刚开业的店没人怎么办 美容店没人进店怎么办 新店开业招不到营业员怎么办 无意开通了花呗怎么办? 花呗灰色用不了怎么办 花呗逾期用不了怎么办 店铺花呗关闭了怎么办 花呗无法使用了怎么办 借呗自动关闭了怎么办 蚂蚁花呗无法开通怎么办 花呗本月还不了怎么办 蚂蚁借呗没钱还怎么办 淘宝账号被限制登录怎么办 辱骂买家被投诉骚扰怎么办 淘宝卖家骚扰买家怎么办 被淘宝卖家电话骚扰怎么办 闲鱼卖东西买家退货运费怎么办 淘宝被恶意拍单怎么办 炒作信用度评价被删除怎么办 删除淘宝评价之后卖家不返现怎么办 美团客户给差评怎么办 淘宝卖家评价差怎么办 淘宝卖家发货未揽收怎么办 周不过5我超过了怎么办 淘宝申请退款后卖家不确认怎么办 淘宝直通车b类扣12分怎么办 淘宝店铺被买家投诉怎么办 淘宝店铺遭买家投诉怎么办 淘宝店铺被买家恶意投诉怎么办 苹果手机无法识别指纹怎么办