歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Linux設備驅動編程模型之設備篇

Linux設備驅動編程模型之設備篇

日期:2017/3/1 10:29:00   编辑:Linux編程
設備驅動程序模型建立在幾個基本數據結構上,這些結構描述了總線、設備、設備驅動、屬性以及他們之間的關系。我們首先認識一下這些數據結構。

一、數據結構

設備表述符

[cpp]
  1. struct device {
  2. struct device *parent;/*指向父設備的指針*/
  3. /*該字段用於管理device和其他device結構,一起device
  4. 與其他結構之間的關系*/
  5. struct device_private *p;
  6. struct kobject kobj;/*內嵌的kobject結構*/
  7. const char *init_name; /* initial name of the device */
  8. struct device_type *type;
  9. struct semaphore sem; /* semaphore to synchronize calls to
  10. * its driver.
  11. */
  12. struct bus_type *bus;/*指向所連接總線的指針*/ /* type of bus device is on */
  13. struct device_driver *driver;/*指向控制設備驅動程序的指針*//* which driver has allocated this
  14. device */
  15. void *platform_data;/*指向遺留設備驅動程序的私有數據的指針*//* Platform specific data, device
  16. core doesn't touch it */
  17. struct dev_pm_info power;
  18. #ifdef CONFIG_NUMA
  19. int numa_node; /* NUMA node this device is close to */
  20. #endif
  21. u64 *dma_mask; /* dma mask (if dma'able device) */
  22. u64 coherent_dma_mask;/* Like dma_mask, but for
  23. alloc_coherent mappings as
  24. not all hardware supports
  25. 64 bit addresses for consistent
  26. allocations such descriptors. */
  27. struct device_dma_parameters *dma_parms;
  28. struct list_head dma_pools;/*www.linuxidc.com聚集的DMA緩沖池鏈表的首部*//* dma pools (if dma'ble) */
  29. struct dma_coherent_mem *dma_mem;/*指向設備所使用的一致性DMA存儲器描述符的指針*//* internal for coherent mem
  30. override */
  31. /* arch specific additions */
  32. struct dev_archdata archdata;
  33. dev_t devt; /* dev_t, creates the sysfs "dev" */
  34. spinlock_t devres_lock;
  35. struct list_head devres_head;
  36. struct klist_node knode_class;
  37. struct class *class;
  38. const struct attribute_group **groups; /* optional groups */
  39. void (*release)(struct device *dev);/*釋放設備描述符的回調函數*/
  40. };

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.linuxidc.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. int (*suspend)(struct device *dev, pm_message_t state);
  18. int (*resume)(struct device *dev);
  19. const struct dev_pm_ops *pm;
  20. struct bus_type_private *p;
  21. };

設備驅動

[cpp]
  1. /*設備驅動程序模型中的每個驅動程序*/
  2. struct device_driver {
  3. const char *name;/*設備驅動程序的名稱*/
  4. struct bus_type *bus;/*指向總線描述符的指針,總線連接所支持的設備*/
  5. struct module *owner;/*標識實現設備程序的模塊,如果有的話*/
  6. const char *mod_name; /* used for built-in modules */
  7. bool suppress_bind_attrs; /* disables bind/unbind via sysfs */
  8. int (*probe) (struct device *dev);/*探測設備的方法(檢驗設備驅動程序是否可以控制該設備)*/
  9. int (*remove) (struct device *dev);/*移走設備時所調用的方法*/
  10. void (*shutdown) (struct device *dev);/*設備斷電時所調用的方法*/
  11. int (*suspend) (struct device *dev, pm_message_t state);/*設備置於低功率狀態時所調用的方法*/
  12. int (*resume) (struct device *dev);/*設備恢復正常狀態時所調用的方法*/
  13. const struct attribute_group **groups;
  14. const struct dev_pm_ops *pm;
  15. struct driver_private *p;
  16. };

類描述符

[cpp]
  1. /*
  2. * device classes
  3. */
  4. struct class {
  5. const char *name;
  6. struct module *owner;
  7. struct class_attribute *class_attrs;
  8. struct device_attribute *dev_attrs;
  9. struct kobject *dev_kobj;
  10. int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
  11. char *(*devnode)(struct device *dev, mode_t *mode);
  12. void (*class_release)(struct class *class);
  13. void (*dev_release)(struct device *dev);
  14. int (*suspend)(struct device *dev, pm_message_t state);
  15. int (*resume)(struct device *dev);
  16. const struct dev_pm_ops *pm;
  17. struct class_private *p;
  18. };
Copyright © Linux教程網 All Rights Reserved