Linux Device Model (2.6)
时间:2006-08-06 来源:muddogxp
2.6和2.4最大的区别和进步可能要数新的Device Model-设备模型。
Functions
以下是LDD3中描写新模型作用的段落,我摘了下来:
The 2.6 device model provides that abstraction. It is now used within the kernel to support a wide variety of tasks, including:
Power management and system shutdown
These require an understanding of the system’s structure. For example,a USB host adaptor cannot be shut down before dealing with all of the devices connected to that adaptor. The device model enables a traversal of the system’s hardware in the right order.
(内核通过设备模型能够了解设备的结构层次及存在关系,使得有依赖关系的设备在电源管理及关闭时能有一个准确的顺序)
Communications with user space
The implementation of the sysfs virtual filesystem is tightly tied into the device model and exposes the structure represented by it. The provision of information about the system to user space and knobs for changing operating parameters is increasingly done through sysfs and, therefore, through the device model.
(sysfs 文件系统。提供设备及驱动给用户的接口,以改变设备及驱动参数。可能是由于/proc的滥用,增加了sysfs出现的必要性)
Hotpluggable devices
Computer hardware is increasingly dynamic; peripherals can come and go at the whim of the user. The hotplug mechanism used within the kernel to handle and (especially) communicate with user space about the plugging and unplugging of
devices is managed through the device model.
(Hotplug。将硬件的热插拔event传递给应用层,应用层可以根据消息,加载或卸载驱动,或其他用户通知)
Device classes
Many parts of the system have little interest in how devices are connected,but they need to know what kinds of devices are available. The device model includes a mechanism for assigning devices to classes,which describe those devices at a higher,functional level and allow them to be discovered from user space.
(设备分类)
Object lifecycles
Many of the functions described above,including hotplug support and sysfs, complicate the creation and manipulation of objects created within the kernel.The implementation of the device model required the creation of a set of mechanisms for dealing with object lifecycles,their relationships to each other,and their representation in user space.
(有效管理设备,驱动的生存周期)
2.4 中对device的管理基本上是利用数组,而不是2.6中的树型结构。字符设备一个数组,块设备一个数组。设备之间的关联性应该完全靠驱动管理,而且驱动 的意义基本就是module。而2.6中强调了driver的重要性,以及device如何和driver绑定的机制。这样的好处是,device可以先 于它的driver注册,然后等待上层选择合适的driver module来load,最后绑定。
Device Model Internal
刚开始看ldd3中关于Device Model的介绍很晦涩,只有自己研究代码后,才能很清楚的了解。
整个Device Model由一棵sysfs树组成。最上层的是sys树根,往下是subsystem(子系统),再往下是kset(kobject 集合),再下是kobject(kernel object),最后叶子上是attributes。
每个kobject对应sysfs中的一个目录项,attribute对应一个文件。在subsystem和kset结构中都嵌入kobject对象,所以每个subsystem和kset也都对应一个目录项。
1) subsystem
kernel 默认初始化的子系统比较典型的有Devices和Buses,devices目录中存放所有注册的设备,buses里存放所有的注册的bus。一般 subsystem都在/sys这层目录下,但所有注册的bus都是例外,他们虽然都在/sys/buses下,但也都是subsystem。
2) kset
kset 是kobject的集合,一般描述一类相似的kobject。由kset结构中的list双向链表连接kobject->entry。每个注册的 bus,都会再注册devices和drivers两个kset,所以你会再/sys/buses/bus_xx/下面看到devices和 drivers目录。然后所有注册在该bus下的设备kobject会连接在devices ksets上,驱动也一样。每个kset结构中又有kset_hotplug_ops结构,用于在一个hotplug消息传递前,追加该kset上的环境 值。下面会谈及到hotplug消息。
3) kobject
kobject可以说是正真的实体,它被嵌入在device和 device_driver结构中。kobject结构中主要包括对象名称、kset指针、引用计数及最重要的kobj_type结构指针, kobj_type其实是kobject的一部分,用来保存attribute及对attribute的操作函数。attribute文件又 sys_create_file创建,操作由kobject->kobj_type->sysfs_ops完成,这个sysfs_ops就象 原来的/proc,有读写两个操作。
500)this.width=500;" border="0">
500)this.width=500;" border="0">
图1,显示的是当一个dev1的设备及相应的驱动drv1注册在bus2后,sysfs上的结构。以及一些符号连接,及创建连接的函数。
图2,则描述的是subsystem,kset,kobject之间的包含关系。
Hotplug
Device Model 中一个很重要的机制就是hotplug。一般用在当总线的kernel thread daemon探测到总线上有新设备插入或拔出,添加、删除该设备时(首先通过ISR获取中断,然后向kthread挂上event),向应用层传递 hotplug消息,由应用加载或卸载相应驱动。一般kobject_hotplug()会在device_add,device_del中调用,往 sysfs树根方向找到最近的kset结构,然后调用结构里hotplug_ops的filter和hotplug。一般情况下,首先会找到 devices子系统中kset,然后调用dev_hotplug(),该函数又调用bus->hotplug()。最后,等所有hotplug函 数构建完环境区后,调用应用层/sbin/hotplug,实现hotplug。
相关阅读 更多 +