【Tiny6410 And Linux】—(1.4)—device 设备模型——原理

上一节原理中有一个特别重要的东西:

总线也是一个设备,必须按照设备注册!

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,   \


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值