Linux设备驱动模型

本来是看了宋宝华写的《linux设备驱动开发详解(第2版)》的第5章:设备文件系统,关于设备模型的描述做了一些笔记,想参照着代码整理一下,发现所参考的Linux 3.0.8的内核代码和书上描述结构体的代码不一样。所以,就简单列一下这些关键的结构体,只求以后读内核代码的时候再见到这些结构体的时候知道是怎么回事。

kobject

kobject是linux 2.6引入的设备管理机制,在内核中由kobject结构体表示,该结构使所有设备在底层都具有统一的接口。kobject是linux内核中面向对象思想的重要体现,是一个基类, device对象、driver对象、class对象以及bus对象都继承或者间接继承此类。每一个kobject都对用sysfs中的一个目录。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <linux/kobject.h>
struct kobject {
	const char		*name;
	struct list_head	entry;
	struct kobject		*parent; // 指向父对象的指针
	struct kset		*kset;	 // 所属于哪个kset
	struct kobj_type	*ktype; // 对象类型描述符指针
	struct sysfs_dirent	*sd;
	struct kref		kref; // 对象引用计数
	unsigned int state_initialized:1;
	unsigned int state_in_sysfs:1;
	unsigned int state_add_uevent_sent:1;
	unsigned int state_remove_uevent_sent:1;
	unsigned int uevent_suppress:1;
};

struct kobj_type { 
    void (*release)(struct kobject *); /*当kobject的引用计数为0时调用此函数,用以释放资源*/
	const struct sysfs_ops *sysfs_ops;
	struct attribute	**default_attrs; 
}; 

内核中提供了一系列操作kobject的函数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
		/*初始化kobject, 设置引用计数为1, entry域指向自己,其所属的kset引用计数加1*/
int kobject_set_name(struct kobject *kobj, const char *name, ...);
		/*用于设置kobject的名字*/

int kobject_add(struct kobject *kobj, struct kobject *parent, const char *fmt, ...);
		/*该函数用于将kobject对象加入Linux设备层次,它会挂接该kobject对象到kset的list链中,增加父目录各级kobject的引用计数,在其parent指向的目录下创建文件节点,并启动该类型内核对象的hotplug函数*/
void kobject_del(struct kobject *kobj);
		/*该函数执行和kobject_add()相反的操作*/

void kobject_cleanup(struct kobject *kobj);
void kobject_release(struct kref *kref)
		/*这两个函数用于清除kobject,当其引用计数为0时,释放对象所占用的资源*/
struct kobject *kobject_get(struct kobject *kobj);
void kobject_put(struct kobject *kobj);
		/*这两个函数分配用于增加和减少引用计数,当引用计数为0时,所有该对象使用的资源将被释放*/

kset

kobject通常通过kset组织成层次化的结构,kset是具有相同类型的kobject的集合,在内核中用结构体kset表示。

kset是kobject的子类之一,表示一些kobject的集合。比如在总线中,所有挂接在同一总线的device就可以组成成一个kset,所有挂接在同一总线的driver也可以组织成一个kset。

1
2
3
4
5
6
7
#include <linux/kobject.h>
struct kset {
	struct list_head list;
	spinlock_t list_lock;
	struct kobject kobj; /* 内嵌一个kobject对象,用作kset的引用计数,也作为集合内所有kobject对象的parent*/
	const struct kset_uevent_ops *uevent_ops;
};

kobject类似,内核也提供了一些操作kset的函数。

1
2
3
4
5
void kset_init(struct kset *kset);
int __must_check kset_register(struct kset *kset);
void kset_unregister(struct kset *kset);
struct kset *kset_get(struct kset *k);
void kset_put(struct kset *k);

kobject被创建或者删除时会产生时间(event),kobject所属的kset会过滤事件或者为用户空间添加信息,每个kset能支持一些特定的事件变量,在热插拔事件发生时,kset的成员函数可以设置一些事件变量,这些变量将被导出到用户空间。ksetuevent_ops成员是执行该kset事件操作集的指针。

1
2
3
4
5
6
struct kset_uevent_ops {
	int (* const filter)(struct kset *kset, struct kobject *kobj);//用与过滤掉不需要导出的事件
	const char *(* const name)(struct kset *kset, struct kobject *kobj);//用于导出一些环境变量给用户空间的热插拔处理程序。用户空间的热插拔脚本根据传入的参数,以及内核导出便来给你采取行动。
	int (* const uevent)(struct kset *kset, struct kobject *kobj,
		      struct kobj_uevent_env *env);
};

kset和kobject的关系

subsystem

貌似是老版本的内核代码中有的东西,在linux 3.0.8中找不到相关代码。

在linux 2.6版本中此结构封装了struct kset

device

系统中任一设备在设备模型中都由一个struct device对象描述。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <linux/device.h>
/**
 * struct device - The basic device structure
 * @parent:	The device's "parent" device, the device to which it is attached.
 * 		In most cases, a parent device is some sort of bus or host
 * 		controller. If parent is NULL, the device, is a top-level device,
 * 		which is not usually what you want.
 * @p:		Holds the private data of the driver core portions of the device.
 * 		See the comment of the struct device_private for detail.
 * @kobj:	A top-level, abstract class from which other classes are derived.
 * @init_name:	Initial name of the device.
 * @type:	The type of device.
 * 		This identifies the device type and carries type-specific
 * 		information.
 * @mutex:	Mutex to synchronize calls to its driver.
 * @bus:	Type of bus device is on.
 * @driver:	Which driver has allocated this
 * @platform_data: Platform data specific to the device.
 * 		Example: For devices on custom boards, as typical of embedded
 * 		and SOC based hardware, Linux often uses platform_data to point
 * 		to board-specific structures describing devices and how they
 * 		are wired.  That can include what ports are available, chip
 * 		variants, which GPIO pins act in what additional roles, and so
 * 		on.  This shrinks the "Board Support Packages" (BSPs) and
 * 		minimizes board-specific #ifdefs in drivers.
 * @power:	For device power management.
 * 		See Documentation/power/devices.txt for details.
 * @pwr_domain:	Provide callbacks that are executed during system suspend,
 * 		hibernation, system resume and during runtime PM transitions
 * 		along with subsystem-level and driver-level callbacks.
 * @numa_node:	NUMA node this device is close to.
 * @dma_mask:	Dma mask (if dma'ble device).
 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
 * 		hardware supports 64-bit addresses for consistent allocations
 * 		such descriptors.
 * @dma_parms:	A low level driver may set these to teach IOMMU code about
 * 		segment limitations.
 * @dma_pools:	Dma pools (if dma'ble device).
 * @dma_mem:	Internal for coherent mem override.
 * @archdata:	For arch-specific additions.
 * @of_node:	Associated device tree node.
 * @devt:	For creating the sysfs "dev".
 * @devres_lock: Spinlock to protect the resource of the device.
 * @devres_head: The resources list of the device.
 * @knode_class: The node used to add the device to the class list.
 * @class:	The class of the device.
 * @groups:	Optional attribute groups.
 * @release:	Callback to free the device after all references have
 * 		gone away. This should be set by the allocator of the
 * 		device (i.e. the bus driver that discovered the device).
 *
 * At the lowest level, every device in a Linux system is represented by an
 * instance of struct device. The device structure contains the information
 * that the device model core needs to model the system. Most subsystems,
 * however, track additional information about the devices they host. As a
 * result, it is rare for devices to be represented by bare device structures;
 * instead, that structure, like kobject structures, is usually embedded within
 * a higher-level representation of the device.
 */
struct device {
	struct device		*parent; 

	struct device_private	*p;

	struct kobject kobj; // 内嵌一个kobject对象
	const char		*init_name; /* initial name of the device */
	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 device_node	*of_node; /* associated device tree node */

	dev_t			devt;	/* dev_t, creates the sysfs "dev" */

	spinlock_t		devres_lock;
	struct list_head	devres_head;

	struct klist_node	knode_class;
	struct class		*class;
	const struct attribute_group **groups;	
	
	void	(*release)(struct device *dev);
};

device结构体用于描述设备有关信息,设备之间的层次关系,以及设备与总线驱动的关系,通常device结构体不单独使用,而是嵌入在更大的结构体中,内核提供了很多操作device的方法。

1
2
int __must_check device_register(struct device *dev); //此函数调用device_add()函数,将一个新的device对象插入设备模型,并自动在/sys/devices下创建一个对应的目录
void device_unregister(struct device *dev); //执行与device_register()函数相反的操作

driver

系统中每个驱动程序都由一个struct device_driver对象描述。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <linux/device.h>
/**
 * struct device_driver - The basic device driver structure
 * @name:	Name of the device driver.
 * @bus:	The bus which the device of this driver belongs to.
 * @owner:	The module owner.
 * @mod_name:	Used for built-in modules.
 * @suppress_bind_attrs: Disables bind/unbind via sysfs.
 * @of_match_table: The open firmware table.
 * @probe:	Called to query the existence of a specific device,
 *		whether this driver can work with it, and bind the driver
 *		to a specific device.
 * @remove:	Called when the device is removed from the system to
 *		unbind a device from this driver.
 * @shutdown:	Called at shut-down time to quiesce the device.
 * @suspend:	Called to put the device to sleep mode. Usually to a
 *		low power state.
 * @resume:	Called to bring a device from sleep mode.
 * @groups:	Default attributes that get created by the driver core
 *		automatically.
 * @pm:		Power management operations of the device which matched
 *		this driver.
 * @p:		Driver core's private data, no one other than the driver
 *		core can touch this.
 *
 * The device driver-model tracks all of the drivers known to the system.
 * The main reason for this tracking is to enable the driver core to match
 * up drivers with new devices. Once drivers are known objects within the
 * system, however, a number of other things become possible. Device drivers
 * can export information and configuration variables that are independent
 * of any specific device.
 */
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);
	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 attribute_group **groups;

	const struct dev_pm_ops *pm;

	struct driver_private *p;
};

内核中提供操作struct device_driver的函数

1
2
int __must_check driver_register(struct device_driver *drv);
void driver_unregister(struct device_driver *drv);

bus

系统中的总线都由struct bus_type描述, 每个bus_type对象都对应/sys/bus目录下的一个子目录,每个子目录下又都存在devicesdrivers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <linux/device.h>
/**
 * struct bus_type - The bus type of the device
 *
 * @name:	The name of the bus.
 * @bus_attrs:	Default attributes of the bus.
 * @dev_attrs:	Default attributes of the devices on the bus.
 * @drv_attrs:	Default attributes of the device drivers on the bus.
 * @match:	Called, perhaps multiple times, whenever a new device or driver
 *		is added for this bus. It should return a nonzero value if the
 *		given device can be handled by the given driver.
 * @uevent:	Called when a device is added, removed, or a few other things
 *		that generate uevents to add the environment variables.
 * @probe:	Called when a new device or driver add to this bus, and callback
 *		the specific driver's probe to initial the matched device.
 * @remove:	Called when a device removed from this bus.
 * @shutdown:	Called at shut-down time to quiesce the device.
 * @suspend:	Called when a device on this bus wants to go to sleep mode.
 * @resume:	Called to bring a device on this bus out of sleep mode.
 * @pm:		Power management operations of this bus, callback the specific
 *		device driver's pm-ops.
 * @p:		The private data of the driver core, only the driver core can
 *		touch this.
 *
 * A bus is a channel between the processor and one or more devices. For the
 * purposes of the device model, all devices are connected via a bus, even if
 * it is an internal, virtual, "platform" bus. Buses can plug into each other.
 * A USB controller is usually a PCI device, for example. The device model
 * represents the actual connections between buses and the devices they control.
 * A bus is represented by the bus_type structure. It contains the name, the
 * default attributes, the bus' methods, PM operations, and the driver core's
 * private data.
 */

struct bus_type {
	const char		*name;
	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 subsys_private *p;
};

内核提供了一些bus_type的操作函数

1
2
int __must_check bus_register(struct bus_type *bus);
void bus_unregister(struct bus_type *bus);

class

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <linux/device.h>
/**
 * struct class - device classes
 * @name:	Name of the class.
 * @owner:	The module owner.
 * @class_attrs: Default attributes of this class.
 * @dev_attrs:	Default attributes of the devices belong to the class.
 * @dev_bin_attrs: Default binary attributes of the devices belong to the class.
 * @dev_kobj:	The kobject that represents this class and links it into the hierarchy.
 * @dev_uevent:	Called when a device is added, removed from this class, or a
 *		few other things that generate uevents to add the environment
 *		variables.
 * @devnode:	Callback to provide the devtmpfs.
 * @class_release: Called to release this class.
 * @dev_release: Called to release the device.
 * @suspend:	Used to put the device to sleep mode, usually to a low power
 *		state.
 * @resume:	Used to bring the device from the sleep mode.
 * @ns_type:	Callbacks so sysfs can detemine namespaces.
 * @namespace:	Namespace of the device belongs to this class.
 * @pm:		The default device power management operations of this class.
 * @p:		The private data of the driver core, no one other than the
 *		driver core can touch this.
 *
 * A class is a higher-level view of a device that abstracts out low-level
 * implementation details. Drivers may see a SCSI disk or an ATA disk, but,
 * at the class level, they are all simply disks. Classes allow user space
 * to work with devices based on what they do, rather than how they are
 * connected or how they work.
 */
struct class {
	const char		*name;
	struct module		*owner;

	struct class_attribute		*class_attrs;
	struct device_attribute		*dev_attrs;
	struct bin_attribute		*dev_bin_attrs;
	struct kobject			*dev_kobj;

	int (*dev_uevent)(struct device *dev, struct kobj_uevent_env *env);
	char *(*devnode)(struct device *dev, mode_t *mode);

	void (*class_release)(struct class *class);
	void (*dev_release)(struct device *dev);

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

	const struct kobj_ns_type_operations *ns_type;
	const void *(*namespace)(struct device *dev);

	const struct dev_pm_ops *pm;

	struct subsys_private *p;
};

属性

bus devicedriverclass层次上都分别定义了其属性结构体,包括 bus_attibutedevice_attibutedriver_attibute 以及 class_attibute。都定义在include\linux\device.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
struct bus_attribute {
	struct attribute	attr;
	ssize_t (*show)(struct bus_type *bus, char *buf);
	ssize_t (*store)(struct bus_type *bus, const char *buf, size_t count);
};

#define BUS_ATTR(_name, _mode, _show, _store)	\
struct bus_attribute bus_attr_##_name = __ATTR(_name, _mode, _show, _store) //创建初始化宏

int __must_check bus_create_file(struct bus_type *, struct bus_attribute *);
void bus_remove_file(struct bus_type *, struct bus_attribute *);
// xxx_create_file()函数会调用sysfs_create_file()函数创建对应的sys文件节点
// xxx_remove_file()函数会调用sysfs_remove_file()函数删除对应的sys文件节点