LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 08-20-2012, 10:17 AM   #1
wcrandthor
LQ Newbie
 
Registered: Aug 2012
Distribution: OpenSUSE 12.1
Posts: 9

Rep: Reputation: Disabled
How do I add a new device driver into an existing /sys/class?


I've got a linux kernel module and I'm trying to update the /sys entries for it. Searching around on google and other sources, I've seen lots of code along the lines of:

Code:
static dev_t MyDev; 
static struct class *c1;  
static int __init start_func(void) {
     ...
     MyDev = MKDEV(nMajor, MINOR_VERSION);
     register_chrdev_region(MyDev, 1, MODULE_NAME);
     c1 = class_create(THIS_MODULE, "chardrv");
     device_create(c1, NULL, MyDev, NULL, MODULE_NAME);
     ....
And I've verified this code works, and that it correctly creates a:
Code:
/sys/class/chardrv/<MODULE_NAME>
entry.

What I'd like to know is how do you create a device in an existing class.

For example lets say my device is now showing up in /sys/class/chardrv as per the above example, now I want a second kernel module to be able to populate its own devices also in the /sys/class/chardrv directory.

I can't call class_create() again, because that "chardrv" class already exists...

So I can run a check to see if /sys/class/chardrv exists, and this can help me decide if I need to call class_create() or not, that's not a problem. Lets put some pseudo code in here to clarify:

Code:
if ( path "/sys/class/chardrv" does not exist)
     new_class = class_create("chardrv")
else
     new_class = some how get class "chardrv" handle, or properties, or whatever device_create(new_class, ...)
So as per this example, if my class already exists, and I just want to add my new device into it I assume I need to create a class structure and somehow populate it with the correct "chardrv class" attributes then call device_create as before, but I'm not sure how to do that.

Any advice?
 
Old 08-20-2012, 09:05 PM   #2
Ygrex
Member
 
Registered: Nov 2004
Location: Russia (St.Petersburg)
Distribution: Debian
Posts: 666

Rep: Reputation: 68
you can find a complete example in kernel sources: /samples/kobject/kset-example.c
 
Old 08-23-2012, 06:19 PM   #3
njensen
LQ Newbie
 
Registered: Jun 2009
Posts: 4

Rep: Reputation: 0
wcrandthor,

Did you find a solution for this? This was precisely the problem I was trying to solve and eventually found my way to the same example code that was suggested in the response above.

While this was quite helpful, especially when combined with the discussion at http://lwn.net/Articles/260139/, I don't think this completely closes the gap.

Note that on line 238 the author has already started with a kset corresponding to the directory "kernel." The variable 'kernel_kobj' was defined as a global and exported for use in other modules. From there is was straight forward to add more directories and files.

I am inclined to similarly declare/export the class that my first module created and use it in the device_create call. That seems awfully rigid and inflexible. However, before I do that I would like to ask the question: Is it possible to iterate over ksets is some manner and search for them by name? Given that the same name could occur in different ksets I suspect you would have to start at the root of /sys and traverse a tree? Perhaps that is a bad practice, but it certainly seems intuitive and flexible to:

1. Look for the created class under the "class" kset.
2. If found, use it
3. If not found, create it
4. Create devices at will

Respectfully,
Nate
 
  


Reply



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
Add Hotspare to existing meta device eldo Linux - Server 2 11-18-2009 10:16 PM
Add san storage device to existing server, how to see and mount, etc. sir-lancealot Linux - Hardware 1 10-28-2009 05:36 PM
/sys/class/hwmon Question Aztral Linux - General 1 08-11-2009 02:00 PM
how to add existing files to existing imported kdevelop project?? santana Programming 1 05-22-2007 09:53 AM
linux video class device driver bharat.gusain Programming 0 11-21-2005 03:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 08:00 AM.

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