Skip to contentSkip to conversation

Sysfs class attribute problem

DISCUSSION jeshwanth Kumar N K 2 participants 5 messages Jun 19, 2012 – Jun 21, 2012

lore
Share & tools
lore
#0 lore raw

Hello all

I am new to sysfs interface ans I read about in mochel's documentation. And doing some experiements on it. Let's come to Tue problem, in my module I have a global variable type char* myglobal and it s static, I am putting that value in my one and only class attribute using show function. But for the same if I a user put some value in to the using echo or something, the attribute value is not changing. And one more thing s after I passed the value to the attribute my system not shutting down :( . Please help me how to read values.

// in store function I am doing this:
Myglobal = bus;

Sent from my HTC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120619/3ed6a6e0/attachment.html

#1 lore raw
Quoted 10 lines · jeshwanth Kumar N K

Hello all

I am new to sysfs interface ans I read about in mochel's documentation. And doing some experiements on it. Let's come to Tue problem, in my module I have a global variable type char* myglobal and it s static, I am putting that value in my one and only class attribute using show function. But for the same if I a user put some value in to the using echo or something, the attribute value is not changing. And one more thing s after I passed the value to the attribute my system not shutting down :( . Please help me how to read values.

Why don't you post your code?As that will really help and moreover as this is your code
you won't be breakign any law set by your company.

Quoted 14 lines

// in store function I am doing this:
Myglobal = bus;

Sent from my HTC

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

#2 lore raw

Hello

Thank you anish for the reply, the code is I pasted below. [code-starts]

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>

static char *gvar = "BeHonest";
// Store and Show functions......
static ssize_t attr1_store(struct class *cls, struct class_attribute
*attr, const char *buf, size_t count);
static ssize_t attr1_show(struct class *cls, struct class_attribute *attr,
char *buf);

static struct class_attribute pwm_class_attrs[] = {
    __ATTR(attr1, S_IRUGO | S_IWUSR , attr1_show, attr1_store),
    __ATTR_NULL
};

static struct class pwm_class =
{
.name = "dev_jes",
.owner = THIS_MODULE,
.class_attrs = pwm_class_attrs
};

static int hello_init(void)

{
    class_register(&pwm_class);
    printk("In hello_init function \n");

    return 0;
}

static ssize_t attr1_show(struct class *cls, struct class_attribute *attr, char *buf)
{

    printk("In attr1_show function\n");
    return sprintf(buf, "%s\n", gvar);
}

static ssize_t attr1_store(struct class *cls, struct class_attribute *attr, const char *buf, size_t count)

{
            printk("the string is : %s\n",buf);
            printk("In attr1_store function\n");
            return 1;
            //return sprintf(gvar, "%s\n", buf);  --- If I put this line I

am getting ERROR :(
}

static void hello_exit(void)

{
    class_unregister(&pwm_class);
    printk("In hello_exit function \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jeshwanth");
MODULE_DESCRIPTION("Learning sysfs");

[code-ends]

echo "jeshu" > /sys/class/dev_jes/attr1

And If I pass the command to atttribute I got result below in dmesg.

[ 3435.613057] the string is : jeshu
[ 3435.613061]
[ 3435.613066] In attr1_store function
[ 3435.613072] the string is : eshu
[ 3435.613074]
[ 3435.613078] In attr1_store function
[ 3435.613083] the string is : shu
[ 3435.613085]
[ 3435.613088] In attr1_store function
[ 3435.613093] the string is : hu
[ 3435.613095]
[ 3435.613098] In attr1_store function
[ 3435.613103] the string is : u
[ 3435.613105]
[ 3435.613108] In attr1_store function
[ 3435.613113] the string is :
[ 3435.613115]
[ 3435.613118] In attr1_store function

So please help me how to read the attribute in store function.. Thanks :)

Quoted 34 lines · anish singh
Quoted 3 lines · jeshwanth Kumar N K

Hello all

I am new to sysfs interface ans I read about in mochel's documentation.

And

Quoted 3 lines

doing some experiements on it. Let's come to Tue problem, in my module I have a global variable type char* myglobal and it s static, I am putting that value in my one and only class attribute using show function. But

for

Quoted 1 line

the same if I a user put some value in to the using echo or something,

the

Quoted 2 lines

attribute value is not changing. And one more thing s after I passed the value to the attribute my system not shutting down :( . Please help me

how

Quoted 1 line

to read values.

Why don't you post your code?As that will really help and moreover as this is your code
you won't be breakign any law set by your company.

Quoted 14 lines

// in store function I am doing this:
Myglobal = bus;

Sent from my HTC

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Signature · 6 lines
Regards
Jeshwanth Kumar N K
+91-7411483498
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120620/72d70af2/attachment-0001.html 
#3 lore raw
Quoted 103 lines · Jeshwanth Kumar N K Jeshu

Hello

Thank you anish for the reply, the code is I pasted below. [code-starts]

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>

static char *gvar = "BeHonest";
// Store and Show functions......
?static ssize_t attr1_store(struct class *cls, struct class_attribute *attr, const char *buf, size_t count);
?static ssize_t attr1_show(struct class *cls, struct class_attribute *attr, char *buf);

static struct class_attribute pwm_class_attrs[] = { ??? __ATTR(attr1, S_IRUGO | S_IWUSR , attr1_show, attr1_store), ??? __ATTR_NULL
};

static struct class pwm_class =
{
??? .name = "dev_jes",
??? .owner = THIS_MODULE,
??? .class_attrs = pwm_class_attrs
};

static int hello_init(void)
{
??? class_register(&pwm_class);
??? printk("In hello_init function \n");

??? return 0;
}

static ssize_t attr1_show(struct class *cls, struct class_attribute *attr, char *buf)
{

??? printk("In attr1_show function\n");
??? return sprintf(buf, "%s\n", gvar);
}

static ssize_t attr1_store(struct class *cls, struct class_attribute *attr, const char *buf, size_t count)
{
??????????? printk("the string is : %s\n",buf);
??????????? printk("In attr1_store function\n");
??????????? return 1;
??????????? //return sprintf(gvar, "%s\n", buf);? --- If I put this line I am getting ERROR :(
}

static void? hello_exit(void)
{
??? class_unregister(&pwm_class);
??? printk("In hello_exit function \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jeshwanth");
MODULE_DESCRIPTION("Learning sysfs");

[code-ends]

echo "jeshu" > /sys/class/dev_jes/attr1

And If I pass the command to atttribute I got result below in dmesg.

[ 3435.613057] the string is : jeshu
[ 3435.613061]
[ 3435.613066] In attr1_store function
[ 3435.613072] the string is : eshu
[ 3435.613074]
[ 3435.613078] In attr1_store function
[ 3435.613083] the string is : shu
[ 3435.613085]
[ 3435.613088] In attr1_store function
[ 3435.613093] the string is : hu
[ 3435.613095]
[ 3435.613098] In attr1_store function
[ 3435.613103] the string is : u
[ 3435.613105]
[ 3435.613108] In attr1_store function
[ 3435.613113] the string is :
[ 3435.613115]
[ 3435.613118] In attr1_store function
Quoted 1 line

So please help me how to read the attribute in store function.. Thanks :)

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>

static char *gvar = NULL;
static ssize_t attr1_store(struct class *cls, const char *buf, size_t count); static ssize_t attr1_show(struct class *cls, char *buf);

static struct class_attribute pwm_class_attrs[] = {
        __ATTR(attr1, 0666, attr1_show, attr1_store), //use macro for permission
        __ATTR_NULL
};

static struct class pwm_class =
{
.name = "dev_jes",
.owner = THIS_MODULE,
.class_attrs = pwm_class_attrs
};

static int hello_init(void)

{
        char string[] = "nothing";
        gvar = kmalloc(sizeof(char)*strlen(string), GFP_KERNEL);
        class_register(&pwm_class);
        snprintf(gvar, sizeof(char)*strlen(string)+1, "%s", string);
        printk("anish In hello_init function \n");
        return 0;
}

static ssize_t attr1_show(struct class *cls, char *buf) {

        printk("In attr1_show function\n");
        printk("%s\n", gvar);
        printk("In attr1_show function\n");
        return sprintf(buf, "%s", gvar);
}

static ssize_t attr1_store(struct class *cls, const char *buf, size_t count)

{
        printk("the string is : %s count %d\n", buf, count);
        return snprintf(gvar, count, "%s\n", buf);
}

static void hello_exit(void)

{
        class_unregister(&pwm_class);
        printk("In hello_exit function \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jeshwanth");
MODULE_DESCRIPTION("Learning sysfs");
======================================================
Hope you got the point.

Quoted 48 lines
Quoted 17 lines · anish singh
Quoted 14 lines · jeshwanth Kumar N K

Hello all

I am new to sysfs interface ans I read about in mochel's documentation. And
doing some experiements on it. Let's come to Tue problem, in my module I have a global variable type char* myglobal and it s static, I am putting that value in my one and only class attribute using show function. But for
the same if I a user put some value in to the using echo or something, the
attribute value is not changing. And one more thing s after I passed the value to the attribute my system not shutting down :( . Please help me how
to read values.

Quoted 17 lines

Why don't you post your code?As that will really help and moreover as this is your code
you won't be breakign any law set by your company.

Quoted 14 lines

// in store function I am doing this:
Myglobal = bus;

Sent from my HTC

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

--
Regards
Jeshwanth Kumar N K
+91-7411483498

#4 lore raw

Hello Anish

Thank you for the changes. Ya now I understoor dealing with store function. :)
And I did small change in ur code.

I have declared the show and store function as I declared before.

static ssize_t attr1_store(struct class *cls, struct class_attribute *attr, const char *buf, size_t count);
static ssize_t attr1_show(struct class *cls, struct class_attribute *attr, char *buf);

instead.

static ssize_t attr1_store(struct class *cls, const char *buf, size_t count);
static ssize_t attr1_show(struct class *cls, char *buf);

After this its working fine.

Quoted 238 lines · anish singh
Quoted 17 lines · Jeshwanth Kumar N K Jeshu

Hello

Thank you anish for the reply, the code is I pasted below. [code-starts]

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>

Quoted 3 lines

static char *gvar = "BeHonest";
// Store and Show functions......
static ssize_t attr1_store(struct class *cls, struct class_attribute

*attr,

Quoted 2 lines

const char *buf, size_t count);
static ssize_t attr1_show(struct class *cls, struct class_attribute

*attr,

Quoted 16 lines

char *buf);

static struct class_attribute pwm_class_attrs[] = {
    __ATTR(attr1, S_IRUGO | S_IWUSR , attr1_show, attr1_store),
    __ATTR_NULL
};

static struct class pwm_class = {
.name = "dev_jes",
.owner = THIS_MODULE, .class_attrs = pwm_class_attrs
};

static int hello_init(void)

Quoted 8 lines
{
    class_register(&pwm_class);
    printk("In hello_init function \n");

    return 0;
}

static ssize_t attr1_show(struct class *cls, struct class_attribute

*attr,

Quoted 8 lines

char *buf)
{

    printk("In attr1_show function\n");
    return sprintf(buf, "%s\n", gvar);
}

static ssize_t attr1_store(struct class *cls, struct class_attribute

*attr,

Quoted 6 lines

const char *buf, size_t count)

{
            printk("the string is : %s\n",buf);
            printk("In attr1_store function\n");
            return 1;
            //return sprintf(gvar, "%s\n", buf);  --- If I put this line

I

Quoted 43 lines

am getting ERROR :(
}

static void hello_exit(void)

{
    class_unregister(&pwm_class);
    printk("In hello_exit function \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL"); MODULE_AUTHOR("Jeshwanth"); MODULE_DESCRIPTION("Learning sysfs");

[code-ends]

echo "jeshu" > /sys/class/dev_jes/attr1

And If I pass the command to atttribute I got result below in dmesg.

[ 3435.613057] the string is : jeshu
[ 3435.613061]
[ 3435.613066] In attr1_store function
[ 3435.613072] the string is : eshu
[ 3435.613074]
[ 3435.613078] In attr1_store function
[ 3435.613083] the string is : shu
[ 3435.613085]
[ 3435.613088] In attr1_store function
[ 3435.613093] the string is : hu
[ 3435.613095]
[ 3435.613098] In attr1_store function
[ 3435.613103] the string is : u
[ 3435.613105]
[ 3435.613108] In attr1_store function
[ 3435.613113] the string is :
[ 3435.613115]
[ 3435.613118] In attr1_store function
Quoted 1 line

So please help me how to read the attribute in store function.. Thanks :)

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/sysdev.h>
#include <linux/major.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>

static char *gvar = NULL;
static ssize_t attr1_store(struct class *cls, const char *buf, size_t count);
static ssize_t attr1_show(struct class *cls, char *buf);

static struct class_attribute pwm_class_attrs[] = {
        __ATTR(attr1, 0666, attr1_show, attr1_store), //use macro for
permission
        __ATTR_NULL
};

static struct class pwm_class =
{
.name = "dev_jes",
.owner = THIS_MODULE,
.class_attrs = pwm_class_attrs
};

static int hello_init(void)

{
        char string[] = "nothing";
       gvar = kmalloc(sizeof(char)*strlen(string), GFP_KERNEL);
       class_register(&pwm_class);
       snprintf(gvar, sizeof(char)*strlen(string)+1, "%s", string);
       printk("anish In hello_init function \n");
       return 0;
}

static ssize_t attr1_show(struct class *cls, char *buf) {

       printk("In attr1_show function\n");
        printk("%s\n", gvar);
        printk("In attr1_show function\n");
        return sprintf(buf, "%s", gvar);
}

static ssize_t attr1_store(struct class *cls, const char *buf, size_t count)

{
       printk("the string is : %s count %d\n", buf, count);
       return snprintf(gvar, count, "%s\n", buf);
}

static void hello_exit(void)

{
       class_unregister(&pwm_class);
       printk("In hello_exit function \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Jeshwanth");
MODULE_DESCRIPTION("Learning sysfs");
======================================================
Hope you got the point.

Quoted 4 lines

On Tue, Jun 19, 2012 at 4:02 PM, anish singh <

anish198519851985 at gmail.com>

Quoted 7 lines
Quoted 6 lines · wrote:
Quoted 3 lines · jeshwanth Kumar N K

Hello all

I am new to sysfs interface ans I read about in mochel's

documentation.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

And
doing some experiements on it. Let's come to Tue problem, in my

module I

Quoted 1 line
Quoted 1 line
Quoted 1 line

have a global variable type char* myglobal and it s static, I am

putting

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

that value in my one and only class attribute using show function. But for
the same if I a user put some value in to the using echo or something, the
attribute value is not changing. And one more thing s after I passed

the

Quoted 29 lines
Quoted 20 lines
Quoted 3 lines

value to the attribute my system not shutting down :( . Please help me how
to read values.

Why don't you post your code?As that will really help and moreover as this is your code
you won't be breakign any law set by your company.

Quoted 14 lines

// in store function I am doing this:
Myglobal = bus;

Sent from my HTC

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

--
Regards
Jeshwanth Kumar N K
+91-7411483498

Signature · 6 lines
Regards
Jeshwanth Kumar N K
+91-7411483498
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120621/823ccff0/attachment.html