kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: anish198519851985@gmail.com (anish singh)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Sysfs class attribute problem
Date: Thu, 21 Jun 2012 09:04:58 +0530	[thread overview]
Message-ID: <CAK7N6vrpH49LD1tfSdtDOVLeHGKjqatKs-rgdDnfd6zL7Ew8yQ@mail.gmail.com> (raw)
In-Reply-To: <CAC-LjFsrQk4FQab-Yb=ZKExd_FT9dB0X_BvLK6R-oGuOpVXtPg@mail.gmail.com>

On Wed, Jun 20, 2012 at 11:32 PM, Jeshwanth Kumar N K Jeshu
<jeshkumar555@gmail.com> 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@gmail.com>
> wrote:
>>
>> On Tue, Jun 19, 2012 at 3:14 PM, jeshwanth Kumar N K
>> <jeshkumar555@gmail.com> 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 at kernelnewbies.org
>> > http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>> >
>
>
>
>
> --
> Regards
> Jeshwanth Kumar N K
> +91-7411483498
>

  reply	other threads:[~2012-06-21  3:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-19  9:44 Sysfs class attribute problem jeshwanth Kumar N K
2012-06-19 10:32 ` anish singh
2012-06-20 18:02   ` Jeshwanth Kumar N K Jeshu
2012-06-21  3:34     ` anish singh [this message]
2012-06-21 18:28       ` Jeshwanth Kumar N K Jeshu
     [not found] <mailman.154.1340215374.2625.kernelnewbies@kernelnewbies.org>
2012-06-21  8:23 ` Pranay Kumar Srivastava

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAK7N6vrpH49LD1tfSdtDOVLeHGKjqatKs-rgdDnfd6zL7Ew8yQ@mail.gmail.com \
    --to=anish198519851985@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).