Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160615
  • 博文数量: 12
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 900
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-19 21:27
个人简介

对于我们的阶级敌人 只有仇恨 没有宽恕

文章分类
文章存档

2014年(6)

2013年(1)

2012年(5)

我的朋友

分类: LINUX

2012-12-22 10:34:23


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);


Attributes
属性
~~~~~~~~~~
  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)

Example:
示例:

  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'
'dev_attr_power'。这两个属性可以象这样组织到一块:

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

阅读(3339) | 评论(1) | 转发(1) |
给主人留下些什么吧!~~

wh2303082014-08-18 14:56:58

翻译的不错,学习了