Re: Sysfs class attribute problem

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jun 20, 2012 at 11:32 PM, Jeshwanth Kumar N K Jeshu
<jeshkumar555@xxxxxxxxx> wrote:
> 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 :)
#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.

>
>
>
> On Tue, Jun 19, 2012 at 4:02 PM, anish singh <anish198519851985@xxxxxxxxx>
> wrote:
>>
>> On Tue, Jun 19, 2012 at 3:14 PM, jeshwanth Kumar N K
>> <jeshkumar555@xxxxxxxxx> wrote:
>> > 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.
>> >
>> > // in store function I am doing this:
>> > Myglobal = bus;
>> >
>> > Sent from my HTC
>> >
>> >
>> >
>> >
>> > _______________________________________________
>> > Kernelnewbies mailing list
>> > Kernelnewbies@xxxxxxxxxxxxxxxxx
>> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>> >
>
>
>
>
> --
> Regards
> Jeshwanth Kumar N K
> +91-7411483498
>

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



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux