Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 04-26-2013, 04:35 PM   #1
LQ Newbie
Registered: Apr 2013
Posts: 4

Rep: Reputation: Disabled
How to create a simple sysfs class attribute in Linux kernel v3.2

I'm learning how to use sysfs in my Linux modules, but I'm having the hardest time finding current documentation on these topics. The Linux Device Drivers 3rd Edition book I've been using seems to be rather dated in this area unfortunately (e.g. the class_device appears to be completely gone in current Linux versions).

I'm simply trying to get an attribute to appear, under the respective sysfs class for my module, that will allow me to read the value of a module variable from kernel space.

In my code, I have a class created that allows udev to create a device node at /dev/foo for my module:
dev_t foo_dev;
alloc_chrdev_region(&foo_dev, 0, 1, "bar");

struct class *bar = class_create(THIS_MODULE, "bar");

device_create(bar, NULL, foo_dev, NULL, "foo");

struct cdev foo_dev_file;
cdev_init(&foo_dev_file, &fops); /* fops defined earlier */
cdev_add(&foo_dev_file, foo_dev, 1);
When I insert the module I get a sysfs class directory created and populated with some default attributes at /sys/class/bar/foo/. How can I create attributes that show up under this new directory?

I have the concepts down pretty well I believe -- create attribute structure, define sysfs_ops functions, etc -- my problem is that I don't know which particular kernel structure to use (class_attribute?), nor how to make these attributes appear under the right sysfs directory.

Would anyone point me to a tutorial or article detailing the process for current Linux kernels?

Last edited by vilhelmgray; 04-29-2013 at 09:01 AM.
Old 04-29-2013, 09:00 AM   #2
LQ Newbie
Registered: Apr 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
I figured out that I can use the class_create_file function after the class_create call to make a class attribute appear under my class directory.

class_create_file(bar, my_foobar_attr);
Assuming my_foobar_attr is a pointer to a defined class_attribute structure, that line will make the class attribute appear under /sys/class/bar/.

Use the device_create_file function to add attribute files under the foo device directory.

Last edited by vilhelmgray; 04-29-2013 at 02:40 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sysfs class registration Linux - Embedded & Single-board computer 5 05-10-2012 10:06 PM
binary attribute file in a sysfs group ThomasFrei Programming 0 03-10-2011 02:24 AM
sysfs read access on attribute -> output buffer donhuansohn Programming 0 07-14-2009 12:59 PM
Device attribute in sysfs sticktrick Linux - Kernel 0 07-11-2009 11:04 AM
Error:Failed to load Main-Class manifest attribute from imgbpatch.jar xbc Ubuntu 0 02-18-2007 04:37 PM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 12:46 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration