驱动学习--SYS属性节点创建

<pre name="code" class="objc">#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <linux/platform_device.h>

struct att_dev
{
    struct platform_device *pdev;
    struct kobject *kobj;
};

static struct att_dev *dev = NULL;

static ssize_t att_store(struct device *dev,
                         struct device_attribute *attr,
                         const char *buf, size_t count)
{
    printk("echo debug buf\n");

    return count;
}

static ssize_t att_show(struct device *dev,
                        struct device_attribute *attr,
                        char *buf)
{
    printk("cat debug buf\n");
    return 0;
}
static DEVICE_ATTR(test,0666,att_show,att_store);//使用DEVICE_ATTR,可以在sys fs中添加“文件”,通过修改该文件内容,可以实现在运行过程中动态控制device的目的


static __devinit int att_probe(struct platform_device *pdev)
{
    int ret;

    /* 增加节点/sys/devic
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在字符设备驱动中添加ATTR设备节点通常需要使用sysfs文件系统。要指定ATTR节点的路径,需要在设备驱动的probe函数中创建一个sysfs目录,并在该目录下创建具有所需名称的ATTR节点。 在创建sysfs目录和ATTR节点之前,需要为设备驱动创建一个设备类和一个设备节点。这可以使用以下代码完成: ``` static struct class *my_class; static struct device *my_device; static int mydriver_probe(struct platform_device *pdev) { int ret; // 创建设备类 my_class = class_create(THIS_MODULE, "my_class"); if (IS_ERR(my_class)) { ret = PTR_ERR(my_class); printk(KERN_ERR "Failed to create device class, error %d\n", ret); return ret; } // 创建设备节点 my_device = device_create(my_class, NULL, MKDEV(0, 0), NULL, "my_device"); if (IS_ERR(my_device)) { ret = PTR_ERR(my_device); printk(KERN_ERR "Failed to create device node, error %d\n", ret); class_destroy(my_class); return ret; } // 在设备节点上添加ATTR节点 ret = device_create_file(my_device, &dev_attr_myattr.attr); if (ret < 0) { printk(KERN_ERR "Failed to create device attribute, error %d\n", ret); device_destroy(my_class, MKDEV(0, 0)); class_destroy(my_class); return ret; } return 0; } static int mydriver_remove(struct platform_device *pdev) { // 移除设备节点和设备类 device_destroy(my_class, MKDEV(0, 0)); class_destroy(my_class); return 0; } static struct platform_driver my_driver = { .probe = mydriver_probe, .remove = mydriver_remove, .driver = { .name = "my_driver", }, }; ``` 在上述代码中,设备节点的名称为"my_device",设备类的名称为"my_class"。在probe函数中,使用device_create_file函数为设备节点添加名为"myattr"的ATTR节点。这将在/sys/class/my_class/my_device/myattr路径下创建一个文件,该文件可以用于读写设备的状态和属性。 如果需要指定不同的ATTR节点路径,则可以使用device_create_file函数的第二个参数来指定路径。例如,以下代码将ATTR节点的路径设置为/sys/my_device/myattr: ``` static int mydriver_probe(struct platform_device *pdev) { int ret; // 创建设备类 my_class = class_create(THIS_MODULE, "my_class"); if (IS_ERR(my_class)) { ret = PTR_ERR(my_class); printk(KERN_ERR "Failed to create device class, error %d\n", ret); return ret; } // 创建设备节点 my_device = device_create(my_class, NULL, MKDEV(0, 0), NULL, "my_device"); if (IS_ERR(my_device)) { ret = PTR_ERR(my_device); printk(KERN_ERR "Failed to create device node, error %d\n", ret); class_destroy(my_class); return ret; } // 在指定路径上添加ATTR节点 ret = device_create_file(my_device, &dev_attr_myattr.attr, &my_device->kobj, "myattr"); if (ret < 0) { printk(KERN_ERR "Failed to create device attribute, error %d\n", ret); device_destroy(my_class, MKDEV(0, 0)); class_destroy(my_class); return ret; } return 0; } ``` 在上述代码中,device_create_file的第三个参数指定了ATTR节点的父目录为my_device设备节点的/sys路径,第四个参数指定了ATTR节点的名称为"myattr"。这将在/sys/my_device/myattr路径下创建一个文件,该文件可以用于读写设备的状态和属性

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值