發表文章

目前顯示的是 7月, 2022的文章

sysfs_create_group 註冊 attribute

參考kx122.c 中 sysfs 註冊 device_attribute  kx122_sysfs_get_enable 實作成 _show() kx122_sysfs_set_enable 實作成 _store() static struct device_attribute dev_attr_accel_enable = __ATTR(enable,      S_IRUGO | S_IWUSR,      kx122_sysfs_get_enable,      kx122_sysfs_set_enable); static struct device_attribute dev_attr_accel_delay = __ATTR(delay,      S_IRUGO | S_IWUSR,      kx122_sysfs_get_delay,      kx122_sysfs_set_delay); static struct device_attribute dev_attr_reg_read = __ATTR(reg_read,      S_IWUSR,      NULL,      kx122_sysfs_reg_read); static struct device_attribute dev_attr_reg_write = __ATTR(reg_write,      S_IWUSR,      NULL,      kx122_sysfs_reg_write); static struct attribute *kx122_sysfs_attrs[] = {      &dev_attr_accel_enable.attr,      &dev_attr_acce...

Linux-DEVICE_ATTR()參考

圖片
  1.介绍 使用 DEVICE_ATTR ,可以实现驱动在sys目录自动创建文件,我们只需要实现show和store函数即可. 然后在应用层就能通过cat和echo命令来对sys创建出来的文件进行读写驱动设备,实现交互. 2.DEVICE_ATTR()宏定义 DEVICE_ATTR() 定义位于include/linux/device.h中,定义如下所示: #define DEVICE_ATTR(_name, _mode, _show, _store) \ struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store) 以下面DEVICE_ATTR()定义为例: static ssize_t show_my_device( struct device * dev, struct device_attribute *attr, char *buf) // cat命令时,将会调用该函数 { return buf; } static ssize_t set_my_device( struct device * dev, struct device_attribute * attr, const char *buf, size_t len) // echo命令时,将会调用该函数. { return len; } static DEVICE_ATTR(my_device_test, S_IWUSR|S_IRUSR, show_my_device, set_my_device); // 定义一个名字为my_device_test的设备属性文件 最终将宏展开为: struct device_attribute dev_attr_my_device_test = { .attr = {.name = " my_device_test " , .mode = S_IWUSR| S_IRUSR }, .show = show_my_device, ...