美文网首页
linux驱动-使用sysfs_create_group创建sy

linux驱动-使用sysfs_create_group创建sy

作者: xuefeng_apple | 来源:发表于2020-03-25 10:08 被阅读0次

    1、创建属性文件的sysfs接口:

    1.1 创建属性文件的sysfs接口函数
    ret = sysfs_create_group(&pdev->dev.kobj,&gpio_attr_grp);
    sysfs_create_group()在kobj目录下创建一个属性集合,并显示集合中的属性文件。如果文件已存在,会报错。

    1.2 删除接口函数
    sysfs_remove_group(&pdev->dev.kobj,&gpio_keys_attr_group);
    sysfs_remove_group()在kobj目录下删除一个属性集合,并删除集合中的属性文件

    2、测试实例代码

    2.1 驱动c代码

    #include <linux/init.h>
    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/platform_device.h>
    #include <linux/slab.h>
    #include <linux/extcon.h>
    #include <linux/gpio.h>
    #include <linux/of_gpio.h>
    #include <linux/irq.h>
    #include <linux/interrupt.h>
    #include <linux/delay.h>
     
    static ssize_t vpd_flip_show(struct device *dev,
                     struct device_attribute *attr, char *buf)
    {
     
        return sprintf(buf, "%d\n", 855);
    }
     
    static ssize_t vpd_flip_store(struct device *dev,
                      struct device_attribute *attr,
                      const char *buf, size_t count)
    {
         dev_info(dev, "%s: %d enter string:%s\n", __func__, __LINE__,buf);
        return count;
    }
    static DEVICE_ATTR(flip, S_IWUSR | S_IRUSR, vpd_flip_show, vpd_flip_store);
     
    static ssize_t vpd_ss_show(struct device *dev,
                   struct device_attribute *attr, char *buf)
    {
     
        return sprintf(buf, "%d\n", 350);
    }
     
    static ssize_t vpd_ss_store(struct device *dev,
                      struct device_attribute *attr,
                      const char *buf, size_t count)
    {
        dev_info(dev, "%s: %d enter string:%s\n", __func__, __LINE__,buf);
        return count;
    }
    static DEVICE_ATTR(ss, S_IWUSR | S_IRUSR, vpd_ss_show, vpd_ss_store);
    static ssize_t vpd_mode_show(struct device *dev,
                   struct device_attribute *attr, char *buf)
    {
        dev_info(dev, "%s: %d enter string:%s\n", __func__, __LINE__,buf);
        return sprintf(buf, "%d\n", 120);
    }
     
    static ssize_t vpd_mode_store(struct device *dev,
                      struct device_attribute *attr,
                      const char *buf, size_t count)
    {
        dev_info(dev, "%s: %d enter string:%s\n", __func__, __LINE__,buf);
        return count;
    }
    static DEVICE_ATTR(mode, S_IWUSR | S_IRUSR, vpd_mode_show, vpd_mode_store);
     
    static ssize_t vpd_enable_show(struct device *dev,
                     struct device_attribute *attr, char *buf)
    {
        return sprintf(buf, "%d\n", 520);
    }
    static ssize_t vpd_enable_store(struct device *dev,
                      struct device_attribute *attr,
                      const char *buf, size_t count)
    {
        dev_info(dev, "%s: %d enter string:%s\n", __func__, __LINE__,buf);
        return count;
    }
    static DEVICE_ATTR(enable, S_IWUSR | S_IRUSR, vpd_enable_show, vpd_enable_store);
     
    static struct attribute *vpd_attributes[] = {
        &dev_attr_flip.attr,
        &dev_attr_ss.attr,
        &dev_attr_mode.attr,
        &dev_attr_enable.attr,
        NULL////属性结构体数组最后一项必须以NULL结尾。
    };static const struct attribute_group vpd_attr_group = {
        .attrs = vpd_attributes,
    };
     
    static int vpd_extcon_probe(struct platform_device *pdev)
    {
        struct virtual_pd *vpd;
        struct device *dev = &pdev->dev;
        int ret = 0;
        dev_info(dev, "%s: %d d29dstart\n", __func__, __LINE__);
     
        ret = sysfs_create_group(&dev->kobj, &vpd_attr_group);
        if (ret < 0)
            dev_warn(dev, "attr group create failed\n");
        else
            dev_info(dev, "attr group create success!\n");
     
        
        dev_info(dev, "%s: %d sussess\n", __func__, __LINE__);
     
        return 0;
    }
    static int vpd_extcon_remove(struct platform_device *pdev)
    {
        dev_info("8", "%s: %d \n", __func__, __LINE__);
        return 0;
    }
     
     
     
    static const struct of_device_id vpd_extcon_dt_match[] = {
        { .compatible = "linux,sysfs_create_group_test", },
        { /* sentinel */ }
    };
    MODULE_DEVICE_TABLE(of, vpd_extcon_dt_match);
    static struct platform_driver vpd_extcon_driver = {
        .probe      = vpd_extcon_probe,
        .remove     = vpd_extcon_remove,
        .driver     = {
            .name   = "sysfs_create_group_test",
            .of_match_table = vpd_extcon_dt_match,
        },
    };
    static int __init __vpd_extcon_init(void)
    {
            return platform_driver_register(&vpd_extcon_driver);
    }
     
    static void __exit __vpd_extcon_exit(void)
    {
            platform_driver_unregister(&vpd_extcon_driver);
    }
     
    module_init(__vpd_extcon_init);
    module_exit(__vpd_extcon_exit);
     
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("tanghanyue");
    MODULE_DESCRIPTION("Virtual Typec-pd extcon driver")
    

    2.2 dts文件添加下面内容

    test-power {
    
    status = "okay";
    
    compatible = "linux,sysfs_create_group_test";
    
    };
    

    3、执行测试

    3.1 adb控制台,可以看到/sys/devices/platform/文件夹下面生产sysfs_create文件夹,该文件夹下面有mode等节点。


    图片.png

    3.2执行结果,串口打印信息


    图片.png

    备注:
    msm-4.14/drivers/hid/hid-qvr.c

    qvr_external_sensor_init--->sysfs_create_group(qvr_external_sensor_kobj, &attr_group); 创建sys 接口

    qvr_external_sensor_kobj = kobject_create_and_add("qvr_external_sensor", kernel_kobj);
    static struct attribute_group attr_group = {
        .attrs = attrs,
    };
    
    static struct attribute *attrs[] = {
        &fd_attribute.attr,
        &ts_base_attribute.attr,
        &ts_offset_attribute.attr,
        NULL,
    };
    
    
    static struct kobj_attribute fd_attribute = __ATTR(fd, 0664,
        fd_show,
        fd_store);
    
    
    
    static ssize_t fd_store(struct kobject *kobj,
        struct kobj_attribute *attr,
        const char *buf, size_t count)
    {
        struct qvr_external_sensor *sensor = &qvr_external_sensor;
        int ret;
    
        ret = kstrtoint(buf, 10, &sensor->fd);
        if (ret < 0)
            return ret;
        if (sensor->fd == -1)
            kernel_unmap_gyro_buffer();
        else
            kernel_map_gyro_buffer();----->进行了sensor->vaddr = dma_buf_kmap(sensor->qvr_buf, 0);  不在释放
        sensor->ts_base = 0;
        sensor->ts_offset = 0;
    
        return count;
    }
    
    
    static int kernel_map_gyro_buffer(void)
    {
        struct qvr_external_sensor *sensor = &qvr_external_sensor;
        int ret = 0;
    
        sensor->qvr_buf = dma_buf_get(sensor->fd);
        if (IS_ERR_OR_NULL(sensor->qvr_buf)) {
            ret = -ENOMEM;
            pr_err("dma_buf_get failed for fd: %d\n", sensor->fd);
            goto done;
        }
        ret = dma_buf_begin_cpu_access(sensor->qvr_buf, DMA_BIDIRECTIONAL);
        if (ret) {
            pr_err("%s: dma_buf_begin_cpu_access failed\n", __func__);
            goto err_dma;
        }
        sensor->vsize = sensor->qvr_buf->size;
        sensor->vaddr = dma_buf_kmap(sensor->qvr_buf, 0);
        if (IS_ERR_OR_NULL(sensor->vaddr)) {
            ret = -ENOMEM;
            pr_err("dma_buf_kmap failed for fd: %d\n", sensor->fd);
            goto err_end_access;
        }
    
        return 0;
    
    err_end_access:
        dma_buf_end_cpu_access(sensor->qvr_buf, DMA_BIDIRECTIONAL);
    err_dma:
        dma_buf_put(sensor->qvr_buf);
        sensor->qvr_buf = NULL;
    done:
        return ret;
    
    }
    

    相关文章

      网友评论

          本文标题:linux驱动-使用sysfs_create_group创建sy

          本文链接:https://www.haomeiwen.com/subject/bvctuhtx.html