Linux设备驱动编程模型之设备篇


设备驱动程序模型建立在几个基本数据结构上,这些结构描述了总线、设备、设备驱动、属性以及他们之间的关系。我们首先认识一下这些数据结构。  

一、数据结构

设备表述符

[cpp]
  1. struct device {  
  2.     struct device       *parent;/*指向父设备的指针*/  
  3.     /*该字段用于管理device和其他device结构,一起device 
  4.     与其他结构之间的关系*/  
  5.     struct device_private   *p;  
  6.   
  7.     struct kobject kobj;/*内嵌的kobject结构*/  
  8.     const char      *init_name; /* initial name of the device */  
  9.     struct device_type  *type;  
  10.   
  11.     struct semaphore    sem;    /* semaphore to synchronize calls to 
  12.                      * its driver. 
  13.                      */  
  14.   
  15.     struct bus_type *bus;/*指向所连接总线的指针*/ /* type of bus device is on */  
  16.     struct device_driver *driver;/*指向控制设备驱动程序的指针*//* which driver has allocated this 
  17.                        device */  
  18.     void        *platform_data;/*指向遗留设备驱动程序的私有数据的指针*//* Platform specific data, device 
  19.                        core doesn't touch it */  
  20.     struct dev_pm_info  power;  
  21.   
  22. #ifdef CONFIG_NUMA   
  23.     int     numa_node;  /* NUMA node this device is close to */  
  24. #endif   
  25.     u64     *dma_mask;  /* dma mask (if dma'able device) */  
  26.     u64     coherent_dma_mask;/* Like dma_mask, but for 
  27.                          alloc_coherent mappings as 
  28.                          not all hardware supports 
  29.                          64 bit addresses for consistent 
  30.                          allocations such descriptors. */  
  31.   
  32.     struct device_dma_parameters *dma_parms;  
  33.   
  34.     struct list_head    dma_pools;/*www.bkjia.com聚集的DMA缓冲池链表的首部*//* dma pools (if dma'ble) */  
  35.   
  36.     struct dma_coherent_mem *dma_mem;/*指向设备所使用的一致性DMA存储器描述符的指针*//* internal for coherent mem 
  37.                          override */  
  38.     /* arch specific additions */  
  39.     struct dev_archdata archdata;  
  40.   
  41.     dev_t           devt;   /* dev_t, creates the sysfs "dev" */  
  42.   
  43.     spinlock_t      devres_lock;  
  44.     struct list_head    devres_head;  
  45.   
  46.     struct klist_node   knode_class;  
  47.     struct class        *class;  
  48.     const struct attribute_group **groups;  /* optional groups */  
  49.   
  50.     void    (*release)(struct device *dev);/*释放设备描述符的回调函数*/  
  51. };  

Bus描述符

[cpp]
  1. struct bus_type {  
  2.     const char      *name;/*总线类型名称*/  
  3.     /*指向对象的指针,该对象包含总线属性和用于导出此属性到sysfs文件系统的方法*/  
  4.     struct bus_attribute    *bus_attrs;  
  5.     /*指向对象的指针,该对象包含设备属性和用于导出此属性sysfs文件系统的方法*/  
  6.     struct device_attribute *dev_attrs;  
  7.     /*指向对象的指针,该对象包含设备驱动程序属性和用于导出此属性到sysfs文件 
  8.     的方法*/  
  9.     struct driver_attribute *drv_attrs;  
  10.     /*检验给定的设备驱动程序是否支持特定设备的方法www.bkjia.com*/  
  11.     int (*match)(struct device *dev, struct device_driver *drv);  
  12.     /**/  
  13.     int (*uevent)(struct device *dev, struct kobj_uevent_env *env);  
  14.     int (*probe)(struct device *dev);  
  15.     int (*remove)(struct device *dev);  
  16.     void (*shutdown)(struct device *dev);  
  17.   
  18.     int (*suspend)(struct device *dev, pm_message_t state);  
  19.     int (*resume)(struct device *dev);  
  20.   
  21.     const struct dev_pm_ops *pm;  
  22.   
  23.     struct bus_type_private *p;  
  24. };  

设备驱动

[cpp]
  1. /*设备驱动程序模型中的每个驱动程序*/  
  2. struct device_driver {  
  3.     const char      *name;/*设备驱动程序的名称*/  
  4.     struct bus_type     *bus;/*指向总线描述符的指针,总线连接所支持的设备*/  
  5.   
  6.     struct module       *owner;/*标识实现设备程序的模块,如果有的话*/  
  7.     const char      *mod_name;  /* used for built-in modules */  
  8.   
  9.     bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */  
  10.   
  11.     int (*probe) (struct device *dev);/*探测设备的方法(检验设备驱动程序是否可以控制该设备)*/  
  12.     int (*remove) (struct device *dev);/*移走设备时所调用的方法*/  
  13.     void (*shutdown) (struct device *dev);/*设备断电时所调用的方法*/  
  14.     int (*suspend) (struct device *dev, pm_message_t state);/*设备置于低功率状态时所调用的方法*/  
  15.     int (*resume) (struct device *dev);/*设备恢复正常状态时所调用的方法*/  
  16.     const struct attribute_group **groups;  
  17.   
  18.     const struct dev_pm_ops *pm;  
  19.   
  20.     struct driver_private *p;  
  21. };  

类描述符

[cpp]
  1. /* 
  2.  * device classes 
  3.  */  
  4. struct class {  
  5.     const char      *name;  
  6.     struct module       *owner;  
  7.   
  8.     struct class_attribute      *class_attrs;  
  9.     struct device_attribute     *dev_attrs;  
  10.     struct kobject          *dev_kobj;  
  11.   
  12.     int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);  
  13.     char *(*devnode)(struct device *dev, mode_t *mode);  
  14.   
  15.     void (*class_release)(struct class *class);  
  16.     void (*dev_release)(struct device *dev);  
  17.   
  18.     int (*suspend)(struct device *dev, pm_message_t state);  
  19.     int (*resume)(struct device *dev);  
  20.   
  21.     const struct dev_pm_ops *pm;  
  22.   
  23.     struct class_private *p;  
  24. };  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 下一页

相关内容