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.linuxidc.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.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.
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.};
下面数据结构用以描述结构间关系的
[cpp]
1./**
2. * struct device_private - structure to hold the private to the driver core portions of the device structure.
3. *
4. * @klist_children - klist containing all children of this device
5. * @knode_parent - node in sibling list
6. * @knode_driver - node in driver list
7. * @knode_bus - node in bus list
8. * @driver_data - private pointer for driver specific info. Will turn into a
9. * list soon.
10. * @device - pointer back to the struct class that this structure is
11. * associated with.
12. *
13. * Nothing outside of the driver core should ever touch these fields.
14. */
15.struct device_private {
16. struct klist klist_children;
17. struct klist_node knode_parent;
18. struct klist_node knode_driver;
19. struct klist_node knode_bus;
20. void *driver_data;
21. struct device *device;
22.};
[cpp]
1.struct class_private {
2. struct kset class_subsys;
3. struct klist class_devices;
4. struct list_head class_interfaces;
5. struct kset class_dirs;
6. struct mutex class_mutex;
7. struct class *class;
8.};
[cpp]
1./**
2. * struct bus_type_private - structure to hold the private to the driver core portions of the bus_type structure.
3. *
4. * @subsys - the struct kset that defines this bus. This is the main kobject
5. * @drivers_kset - the list of drivers associated with this bus
6. * @devices_kset - the list of devices associated with this bus
7. * @klist_devices - the klist to iterate over the @devices_kset
8. * @klist_drivers - the klist to iterate over the @drivers_kset
9. * @bus_notifier - the bus notifier list for anything that cares about things
10. * on this bus.
11. * @bus - pointer back to the struct bus_type that this structure is associated
12. * with.
13. *
14. * This structure is the one that is the actual kobject allowing struct
15. * bus_type to be statically allocated safely. Nothing outside of the driver
16. * core should ever touch these fields.
17. */
18.struct bus_type_private {
19. struct kset subsys;
20. struct kset *drivers_kset;
21. struct kset *devices_kset;
22. struct klist klist_devices;
23. struct klist klist_drivers;
24. struct blocking_notifier_head bus_notifier;
25. unsigned int drivers_autoprobe:1;
26. struct bus_type *bus;
27.};
28.
29.struct driver_private {
30. struct kobject kobj;
31. struct klist klist_devices;
32. struct klist_node knode_bus;
33. struct module_kobject *mkobj;
34. struct device_driver *driver;
35.};
描述属性文件的结构
[cpp]
1./* FIXME
2. * The *owner field is no longer used.
3. * x86 tree has been cleaned up. The owner
4. * attribute is still left for other arches.
5. */
6.struct attribute {
7. const char *name;
8. struct module *owner;
9. mode_t mode;
10.};