上一节原理中有一个特别重要的东西:
总线也是一个设备,必须按照设备注册!
1、设备描述结构
在 Linux 设备模型中,总线由 device 结构表示,定义在 <linux/device.h> 中。
其中 release 是必须要设置的!!!
struct device
{
struct device *parent; /* 父设备 */
struct device_private *p; /* 设备私有数据 */
struct kobject kobj; /* 设备基类 */
const char *init_name; /* 设备初始化名称 */
struct bus_type *bus; /* 设备所在总线 */
struct device_driver *driver; /* 管理该设备的驱动 */
struct (*relaese)(struct device *dev); /* 设备释放方法 */
...
};
2、设备方法函数
设备的注册使用:
int device_register(struct device *dev);
设备的注销使用:
void device_unregister(struct device *dev);
如果需要为设备创建属性文件需要赋值如下结构:
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);
};
其中 attr 成员用于描述属性的名称和对应属性文件的权限,show、store 成员方法则用于构造属性文件的读写嘻嘻,设备属性文件并不会随着设备的注册而自动创建,需要通过调用下面的函数来创建:
int device_create_file(struct device *device,struct device_attribute *entry);
设备属性文件删除使用函数:
void device_remove_file(struct device *dev,struct device_attribute *attr);
设备属性结构可以使用下面宏来完成初始化:
#define DEVICE_ATTR(_name,_mode,_show,_store) \
struct device_attribute dev_attr_##_name = _ATTR(_name,_mode,_show,_store)
其中宏 __ATTR 定义在 linux/sysfs.h 下:
#define __ATTR(_name,_mode,_show,_store) { \
.attr = {.name = __stringify(_name),.mode = _mode}, \
.show = _show, \
.store = _store, \