驱动模型




驱动模型:

为了方便维护设备和驱动,内核抽象出三个类:

总线 struct bus_type;
设备 struct device;
驱动 struct device_driver;

这里的总线是抽象的该念总线,逻辑层面的,只是为了管理匹配
向内核注册并和总线关联的设备和驱动的,匹配规则决定于总线。

--------------------------------------------------------------------------------

struct bus_type {
 const char  *name; //总线的名字,注册成功后会见于/sys/bus/
 const char  *dev_name;
 struct device  *dev_root;
 struct bus_attribute *bus_attrs;
 struct device_attribute *dev_attrs;
 struct driver_attribute *drv_attrs;

 int (*match)(struct device *dev, struct device_driver *drv); //匹配规则
 int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
 int (*probe)(struct device *dev);
 int (*remove)(struct device *dev);
 void (*shutdown)(struct device *dev);

 int (*suspend)(struct device *dev, pm_message_t state);
 int (*resume)(struct device *dev);

 const struct dev_pm_ops *pm;

 struct iommu_ops *iommu_ops;

 struct subsys_private *p;
};
 
int (*match)(struct device *dev, struct device_driver *drv); //匹配规则
匹配规则由编写驱动者自己定义。但是匹配成功必须返回1,否则返回0;


总线的注册:

 bus_register(struct bus_type *bus);
 bus_unregister(struct bus_type *bus);

--------------------------------------------------------------------------------

设备:

struct device {
 struct device  *parent;

 struct device_private *p;

 struct kobject kobj;
 const char  *init_name; /* initial name of the device */ //设备的名字
      // /sys/bus/xx/devices
 const struct device_type *type;

 struct mutex  mutex; /* mutex to synchronize calls to
      * its driver.
      */
 
 //用于关联的总线
 struct bus_type *bus;  /* type of bus device is on */


 //匹配成功后指向设备的驱动结构体变量。
 struct device_driver *driver; /* which driver has allocated this
        device */

 //很重要
 void  *platform_data; /* Platform specific data, device
        core doesn't touch it */
 struct dev_pm_info power;
 struct dev_pm_domain *pm_domain;

#ifdef CONFIG_NUMA
 int  numa_node; /* NUMA node this device is close to */
#endif
 u64  *dma_mask; /* dma mask (if dma'able device) */
 u64  coherent_dma_mask;/* Like dma_mask, but for
          alloc_coherent mappings as
          not all hardware supports
          64 bit addresses for consistent
          allocations such descriptors. */

 struct device_dma_parameters *dma_parms;

 struct list_head dma_pools; /* dma pools (if dma'ble) */

 struct dma_coherent_mem *dma_mem; /* internal for coherent mem
          override */
#ifdef CONFIG_CMA
 struct cma *cma_area;  /* contiguous memory area for dma
        allocations */
#endif
 /* arch specific additions */
 struct dev_archdata archdata;

 struct device_node *of_node; /* associated device tree node */

 dev_t   devt; /* dev_t, creates the sysfs "dev" */
 u32   id; /* device instance */

 spinlock_t  devres_lock;
 struct list_head devres_head;

 struct klist_node knode_class;
 struct class  *class;
 const struct attribute_group **groups; /* optional groups */

 //必须实现,当设备从内核移除时会调用此函数
 void (*release)(struct device *dev);
};


设备的注册及移除:

device_register(struct device *dev);
device_unregister(struct device *dev);


---------------------------------------------------------------------------------------

驱动:

struct device_driver {
 const char  *name; //驱动的名字
 struct bus_type  *bus; //驱动需要关联的总线

 struct module  *owner;
 const char  *mod_name; /* used for built-in modules */

 bool suppress_bind_attrs; /* disables bind/unbind via sysfs */

 const struct of_device_id *of_match_table;

 int (*probe) (struct device *dev); //匹配成功,内核自动调用probe
 int (*remove) (struct device *dev); //移除驱动时会自动调用remove
 void (*shutdown) (struct device *dev);
 int (*suspend) (struct device *dev, pm_message_t state);
 int (*resume) (struct device *dev);
 const struct attribute_group **groups;

 const struct dev_pm_ops *pm;

 //很重要
 struct driver_private *p;
};

驱动的注册及移除:
 int driver_register(struct device_driver *drv)
 int driver_unregister(struct device_driver *drv)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘十彡

IT发展快就是因为有开源精神

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值