preetam
preetam

Reputation: 1587

adding attributes to platform device in linux kernel drivers

I have created a simple test device. My intention is to create a few custom sysfs files and get setting from them. I tried first by adding a kobject and with my own sys_ops. That worked well. However, trying to do the same by using the platform device is giving me a file which I cannot read or write to. Can any one tell me whats wrong here?

=============sysfs_file.c==================================

    #include <linux/kernel.h>
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/platform_device.h>
    #include <linux/kobject.h>
    #include <linux/slab.h>

    struct kobject *myob;
    char *ops_buffer;
    struct attribute my_atr = {
    .name = "custom_attrbute",
    .mode = S_IWUGO|S_IRUGO,
    };
    int atp_probe(struct platform_device *dev)
    {
        printk("%s\n", __func__);
        myob = &dev->dev.kobj;
        ops_buffer = kzalloc(PAGE_SIZE, GFP_KERNEL);
        sysfs_create_file(myob, &my_atr);
        return 0;
    }
    int atp_remove(struct platform_device *dev)
    {
    printk("%s\n", __func__);
        sysfs_remove_file(myob, &my_atr);
        kfree(ops_buffer);
        return 0;
    }
    struct platform_device atp_dev = {
        .name = "Aerrow_Test_Platform",
        .id = 0,
    };
    struct platform_driver atp_drv = {
        .driver = {
        .name = "Aerrow_Test_Platform",
        .owner = THIS_MODULE,
        },
        .probe = atp_probe,
        .remove = atp_remove,
    };
    static int __init sfst_init(void)
    {
        int ret;
        printk("%s\n",__func__);
        ret = platform_device_register(&atp_dev);
        printk("%s: device add ret = %d\n", __func__, ret);
        ret = platform_driver_register(&atp_drv);
        printk("%s: driver register ret = %d\n", __func__, ret);
        return 0;
    }
    static void __exit sfst_exit(void)
    {   
        printk("%s", __func__);
        platform_driver_unregister(&atp_drv);
        platform_device_del(&atp_dev);
    }
    module_init(sfst_init);
    module_exit(sfst_exit);
    MODULE_LICENSE("GPL");

==================================================================================== The output is:

preetam@preetam-Veriton-Series:ko_training$ cd /sys/devices/platform/Aerrow_Test_Platform.0/
preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ ls
custom_attrbute  driver  modalias  power  subsystem  uevent
preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ cat custom_attrbute 
cat: custom_attrbute: Input/output error
preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ echo "HELLO" > custom_attrbute 
bash: echo: write error: Input/output error
preetam@preetam-Veriton-Series:Aerrow_Test_Platform.0$ cat custom_attrbute cat: custom_attrbute: Input/output error

Is the platform device's ktype ops methods unable handle additional attributes or is something wrong with my code? The same thing works if I create my own kobject and put this attribute under that.

Upvotes: 1

Views: 4853

Answers (1)

Narender Ankam
Narender Ankam

Reputation: 36

"A bare attribute contains no means to read or write the value of the attribute. Subsystems are encouraged to define their own attribute structure and wrapper functions for adding and removing attributes for a specific object type." - from kernel.org/doc/Documentation/filesystems/sysfs.txt.

You can try device_attribute instead of this bare attribute structure. Also define your own show and store to read and write your sysfs entry.

Upvotes: 2

Related Questions