The Basic Device Structure
基本设备结构体
~~~~~~~~~~~~~~~~~~~~~~~~~~
See the kerneldoc for the struct device.
Programming Interface
编程接口
~~~~~~~~~~~~~~~~~~~~~
The bus driver that discovers the device uses this to register the
device with the core:
那些发现了设备的总线驱动使用以下接口将这个设备注册到核心:
- int device_register(struct device * dev);
The bus should initialize the following fields:
总线应该初始化该设备的下列字段:
- parent
- name
- bus_id
- bus
A device is removed from the core when its reference count goes to
0. The reference count can be adjusted using:
当一个设备的引用计数减到 0 时应该从核心中移除该设备。可以使用以下
接口改变一个设备的引用计数。
- struct device * get_device(struct device * dev);
- void put_device(struct device * dev);
get_device() will return a pointer to the struct device passed to it
if the reference is not already 0 (if it's in the process of being
removed already).
如果 device 的引用计数不是 0(已经在移除的过程中)的话,get_device()
会返回一个指向传递给它的 struct device 的指针。
A driver can access the lock in the device structure using:
一个 driver 可以使用以下接口访问 device 结构体中的 lock:
- void lock_device(struct device * dev);
- void unlock_device(struct device * dev);
Attributes
属性
~~~~~~~~~~
- struct device_attribute {
- struct attribute attr;
- ssize_t (*show)(struct device *dev, struct device_attribute *attr,
- char *buf);
- ssize_t (*store)(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t count);
- };
Attributes of devices can be exported by a device driver through sysfs.
设备驱动程序可以通过 sysfs 导出设备的属性。
Please see Documentation/filesystems/sysfs.txt for more information
on how sysfs works.
请参考 Documentation/filesystems/sysfs.txt 来了解 sysfs 如何工作。
As explained in Documentation/kobject.txt, device attributes must be be
created before the KOBJ_ADD uevent is generated. The only way to realize
that is by defining an attribute group.
正如 Documentation/kobject.txt 中所述,设备属性的创建必须在 KOBJ_ADD
uevent 发生之前。实现这个的唯一途径就是定义一个属性组。
Attributes are declared using a macro called DEVICE_ATTR:
定义属性可以使用宏函数 DEVICE_ATTR。
- #define DEVICE_ATTR(name,mode,show,store)
Example:
示例:
- static DEVICE_ATTR(type, 0444, show_type, NULL);
- static DEVICE_ATTR(power, 0644, show_power, store_power);
This declares two structures of type struct device_attribute with respective
names 'dev_attr_type' and 'dev_attr_power'. These two attributes can be
organized as follows into a group:
这将定义两个拥有各自名称的 struct device_attribute 类型的结构体,'dev_attr_type'
和'dev_attr_power'。这两个属性可以象这样组织到一块:
- static struct attribute *dev_attrs[] = {
- &dev_attr_type.attr,
- &dev_attr_power.attr,
- NULL,
- };
- static struct attribute_group dev_attr_group = {
- .attrs = dev_attrs,
- };
- static const struct attribute_group *dev_attr_groups[] = {
- &dev_attr_group,
- NULL,
- };
This array of groups can then be associated with a device by setting the
group pointer in struct device before device_register() is invoked:
这些数组随后可以通过设置 groups 指针在 device_register() 被调用之前关联
到一个 device 上:
- dev->groups = dev_attr_groups;
- device_register(dev);
The device_register() function will use the 'groups' pointer to create the
device attributes and the device_unregister() function will use this pointer
to remove the device attributes.
device_register() 函数将使用‘groups‘指针去创建该设备的属性,而
device_unregister() 函数也将使用‘groups‘指针去移除该设备的属性。
Word of warning: While the kernel allows device_create_file() and
device_remove_file() to be called on a device at any time, userspace has
strict expectations on when attributes get created. When a new device is
registered in the kernel, a uevent is generated to notify userspace (like
udev) that a new device is available. If attributes are added after the
device is registered, then userspace won't get notified and userspace will
not know about the new attributes.
提醒一句:尽管 kernel 允许在任何时候都可以在一个设备上调用 device_create_file()
和 device_remove_file(),但用户空间还是强烈期望能够得知在属性何时被创建。
当一个新的设备注册进 kernel 时,就会产生一个 uevent 事件并通知用户空间该
设备可用了。但如果设备的属性是在设备注册之后才添加的话,那么用户空间将
得不到任何通知,对这个新的属性也将一无所知。
This is important for device driver that need to publish additional
attributes for a device at driver probe time. If the device driver simply
calls device_create_file() on the device structure passed to it, then
userspace will never be notified of the new attributes.
这对于那些需要在驱动探测(driver probe)时为设备发布附加属性的设备驱动
程序来说是很重要的。如果设备驱动程序只是简单的传递 device 结构体来调用
device_create_file() 的话,用户空间就永远也得不到关于新属性的通知。