Documentation/driver-model/device.txt 翻译学习 -- 需完善
2015-11-02 09:18
447 查看
Documentation/driver-model/device.txt 原文在线网址:
http://lxr.oss.org.cn/source/Documentation/driver-model/device.txt
(注:黑体加粗文字为翻译部分)
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) 去注册设备
int device_register(struct device * dev);
The bus should initialize the following fields:
- parent
- name
- bus_id
- bus
总线应该初始化下面的几个域
- 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:
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).
当一个设备的引用数量变成0的时候,它会从内核移除。引用数量可以被调整,利用下面两个函数
struct device * get_device(struct device * dev);
void put_device(struct device * dev);
如果设备引用数量不是0(如果它已经在被移除的过程中), get_device() 将会返回一个传递给这个struct device 的指针。
A driver can access the lock in the device structure using:
void lock_device(struct device * dev);
void unlock_device(struct device * dev);
一个驱动可以使用设备结构中的锁,利用下面两个函数
void lock_device(struct device * dev);
void unlock_device(struct device * dev);
Attributes
属性
~~~~~~~~~~
struct device_attribute {
struct attributeattr;
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_ADDuevent 发生之前。实现这个的唯一途径就是定义一个属性组。
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:
dev->groups = dev_attr_groups;
device_register(dev);
块的这些数组随后可以通过设置 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()函数将利用这个指针去移除设备属性。
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.
提醒:尽管内核允许一个设备在任何时候调用device_create_file() 和 device_remove_file() ,但是用户空间还是强烈期望得知属性是什么时候被创建的。当一个新的设备被注册进内核时,会产生一个uevent 事件来通知用户空间(就像udev一样)一个新的设备可以被使用了。如何在设备被注册之后又添加了新的属性,那么用户空间不会得到通知并且用户空间也不会知道这些新的属性。
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.
这对一个需要在驱动探测时去发布一个设备的额外属性的设备驱动来说是非常重要的。如果有一个设备驱动仅仅通过传递参数来调用device_create_file(), 那么用户空间将不会被通知这些新的属性。
参考:
device_driver结构体详解
http://blog.chinaunix.net/uid-20729605-id-1884305.html
/article/2139735.html
http://lxr.oss.org.cn/source/Documentation/driver-model/device.txt
(注:黑体加粗文字为翻译部分)
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) 去注册设备
int device_register(struct device * dev);
The bus should initialize the following fields:
- parent
- name
- bus_id
- bus
总线应该初始化下面的几个域
- 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:
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).
当一个设备的引用数量变成0的时候,它会从内核移除。引用数量可以被调整,利用下面两个函数
struct device * get_device(struct device * dev);
void put_device(struct device * dev);
如果设备引用数量不是0(如果它已经在被移除的过程中), get_device() 将会返回一个传递给这个struct device 的指针。
A driver can access the lock in the device structure using:
void lock_device(struct device * dev);
void unlock_device(struct device * dev);
一个驱动可以使用设备结构中的锁,利用下面两个函数
void lock_device(struct device * dev);
void unlock_device(struct device * dev);
Attributes
属性
~~~~~~~~~~
struct device_attribute {
struct attributeattr;
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_ADDuevent 发生之前。实现这个的唯一途径就是定义一个属性组。
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:
dev->groups = dev_attr_groups;
device_register(dev);
块的这些数组随后可以通过设置 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()函数将利用这个指针去移除设备属性。
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.
提醒:尽管内核允许一个设备在任何时候调用device_create_file() 和 device_remove_file() ,但是用户空间还是强烈期望得知属性是什么时候被创建的。当一个新的设备被注册进内核时,会产生一个uevent 事件来通知用户空间(就像udev一样)一个新的设备可以被使用了。如何在设备被注册之后又添加了新的属性,那么用户空间不会得到通知并且用户空间也不会知道这些新的属性。
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.
这对一个需要在驱动探测时去发布一个设备的额外属性的设备驱动来说是非常重要的。如果有一个设备驱动仅仅通过传递参数来调用device_create_file(), 那么用户空间将不会被通知这些新的属性。
参考:
device_driver结构体详解
http://blog.chinaunix.net/uid-20729605-id-1884305.html
/article/2139735.html
相关文章推荐
- centos下完全卸载mysql
- Linux/Uinx Socket select(1)
- Object-C系列<Objective-C之内存管理>
- VSftp配置
- 修改Launcher2在ADT上编译调试(一)
- Jquery日历插件
- oracle归档日志的开启和关闭
- java transient 和Volatile关键字
- Maximum Depth of Binary Tree
- css书写规范整理
- Android AndroidManifest 清单文件以及权限详解
- chrome浏览器控制台
- 【NOIP2011】选择客栈 乱搞
- js完美身份证验证
- jQuery 在Table中选择input之类的东西注意事项
- 项目考核维度
- dataGridView设置指定列不能修改
- QT基于UDP通信的多线程编程问题
- unaryPred函数对象,函数适配器(一)
- 数据库设计原则