Chinaunix首页 | 论坛 | 博客
  • 博客访问: 44805
  • 博文数量: 13
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 20
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-08 19:06




2013-07-11 14:59:52

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:

  1. 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 时应该从核心中移除该设备。可以使用以下

  1. struct device * get_device(struct device * dev);
  2. 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:

  1. void lock_device(struct device * dev);
  2. void unlock_device(struct device * dev);

  1. struct device_attribute {
  2.     struct attribute    attr;
  3.     ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  4.                     char *buf);
  5.     ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  6.                      const char *buf, size_t count);
  7. };

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。

  1. #define DEVICE_ATTR(name,mode,show,store)


  1. static DEVICE_ATTR(type, 0444, show_type, NULL);
  2. 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'

  1. static struct attribute *dev_attrs[] = {
  2.     &dev_attr_type.attr,
  3.     &dev_attr_power.attr,
  4.     NULL,
  5. };

  6. static struct attribute_group dev_attr_group = {
  7.     .attrs = dev_attrs,
  8. };

  9. static const struct attribute_group *dev_attr_groups[] = {
  10.     &dev_attr_group,
  11.     NULL,
  12. };

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 上:

  1. dev->groups = dev_attr_groups;
  2. 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() 的话,用户空间就永远也得不到关于新属性的通知。

阅读(1425) | 评论(0) | 转发(0) |