驱动模型

来源:互联网 发布:怎么修改淘宝上的地址 编辑:程序博客网 时间:2024/06/07 14:37



驱动模型:

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

总线 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
原创粉丝点击