Sponsored Content
Top Forums UNIX for Advanced & Expert Users Get pointer for existing device class (struct class) in Linux kernel module Post 302796723 by hdaniel@ualg.pt on Saturday 20th of April 2013 02:46:10 PM
Old 04-20-2013
Get pointer for existing device class (struct class) in Linux kernel module

Hi all!

I am trying to register a device in an existing device class, but I am

having trouble getting the pointer to an existing class.
I can create a class in a module, get the pointer to it and then use

it to register the device with:

Code:
*cl = class_create(THIS_MODULE, className);
dev_ret = device_create(*cl, NULL, *dev, NULL, driverName);

However I need to register another device in the same class with

another module, but I couldn't find a way to get the pointer to an

existing class. And I can not crete the class again in the other

module, because since class already exists class_create() returns NULL

and not the pointer to the class required by device_create().

I found in a function that returns a pointer to a class by its name googling (sorry system did not allowed to post url)

Code:
struct class * class_find(char * name)

However when I try to compile the function compiler says it does not exist.
I thougth this function was exported by the kernel (my module have
license GPL) but it appears it is not.
Maybe I need to include some header?

I know its a patch I suposed it was in main stream kernel already.
I tried to rewrite this function since the code is available. The code is:

Code:
+struct class * class_find(char * name)
+{
+	struct class * this_class;
+
+	if (!name)
+		return NULL;
+
+	list_for_each_entry(this_class, &class_subsys.kset.list, subsys.kset.kobj.entry) {
+		if (!(strcmp(this_class->name, name)))
+			return this_class;
+	}
+
+	return NULL;
+}

But when I try to iterate over class_subsys with:

list_for_each_entry(this_class, &class_subsys.kset.list, subsys.kset.kobj.entry)

now symbol class_subsys is not found. Again I thougth it is exported

to the kernel.

I am not sure what is missing. Some header?
Am I doing it the wrong way?
There is another function to do it?

I supose if I could traverse sysfs from start I could get a pointer to an existing class.
But I also did not find how to start traversing sysfs.
All functions I have seen requires a pointer to kobject or kset to

start traversing. But I have no poniter even to the root of sysfs or kernel objects, so I can not start traversin the tree to get a class pointer.

Can anyone point me in the right direction please?

Last edited by jim mcnamara; 04-20-2013 at 05:08 PM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

can't get perl Class::Struct to work

This is my code: #!/usr/bin/perl -w use strict; use Class::Struct; struct App => { name => '$', }; sub App::name { my $self = shift; if( @_ ) { $self->{'name'} = shift; } return $self->{'name'}; (3 Replies)
Discussion started by: IMTheNachoMan
3 Replies

2. UNIX for Advanced & Expert Users

Passing socket struct between kernel threads - module programming

I write kernel module with kernel threads using linux/kthread.h on 2.6.* kernel I tried to passing data between two kernel threads with data argument of kthread_run( fun, data , NAME ); but this is not work I dont know why. I tried many possibility and nothing works. So I thought that... (0 Replies)
Discussion started by: marcintom
0 Replies

3. UNIX for Dummies Questions & Answers

car class (not school class)

im just trying to have some fun and kill some time writing a c++ program that has a person type in a car make and model then gives them a year and a price. or something like that. i always have problems getting it goin but once the ball is rolling im usually pretty good. anyone wanna help me out? ... (1 Reply)
Discussion started by: rickym2626
1 Replies

4. Programming

C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento) typedef struct elemento { char name; char value; List<struct elemento> ltElementos; ... (7 Replies)
Discussion started by: pogdorica
7 Replies

5. Programming

Pointer for class not working that well. Syntax I think.

I'll be gratefull for any help. Thanks. :) This is the non class type error: # g++ -I/usr/include/mysql -I/usr/include/mysql++ -lmysqlpp -L/usr/lib/mysql -L/usr/local/lib/mysql++ loaddsgsports.cpp -o loaddsgsports loaddsgsports.cpp: In function âint outputToImport(const char*, const char*,... (1 Reply)
Discussion started by: sepoto
1 Replies

6. Programming

Class Pointer initialization C++

Hello everyone, I have a question, that are the following ways of pointer intialization same ? ClassA *point; point = 0; point = new ClassA; Thanks a load in advance!! Regards, (10 Replies)
Discussion started by: mind@work
10 Replies

7. Fedora

Is Kernel module is the same as a device driver?

I have been reading prep questions for my second unix academy exam, and there's a nuance, I'm not sure I understand it correctly. I've been under impression from my readings of book by Evi Nemeth and from unix academy DVDs I've been watching, that kernel's modules are drivers. I think of it, as... (25 Replies)
Discussion started by: newlinuxuser1
25 Replies

8. Programming

static use for class inside the same class c++

Hi, I believe the next code is wrong: class Egg { Egg e; int i; Egg(int ii=0) : i(ii) {} }; because you would end up with an endless definition (memory allocation) of Egg objects, thus int i. Ok, so God Eckel proposes for a singleton: class Egg { static Egg e; int... (5 Replies)
Discussion started by: xavipoes
5 Replies

9. Programming

Size of Derived class, upon virtual base class inheritance

I have the two class definition as follows. class A { public: int a; }; class B : virtual public A{ }; The size of class A is shown as 4, and size of class B is shown as 16. Why is this effect ?. (2 Replies)
Discussion started by: techmonk
2 Replies

10. Programming

C++ : Base class member function not accessible from derived class

Hello All, I am a learner in C++. I was testing my inheritance knowledge with following piece of code. #include <iostream> using namespace std; class base { public : void display() { cout << "In base display()" << endl; } void display(int k) {... (2 Replies)
Discussion started by: anand.shah
2 Replies
DEVICE_CREATE(9)					   Device drivers infrastructure					  DEVICE_CREATE(9)

NAME
device_create - creates a device and registers it with sysfs SYNOPSIS
struct device * device_create(struct class * class, struct device * parent, dev_t devt, void * drvdata, const char * fmt, ...); ARGUMENTS
class pointer to the struct class that this device should be registered to parent pointer to the parent struct device of this new device, if any devt the dev_t for the char device to be added drvdata the data to be added to the device for callbacks fmt string for the device's name ... variable arguments DESCRIPTION
This function can be used by char device classes. A struct device will be created in sysfs, registered to the specified class. A "dev" file will be created, showing the dev_t for the device, if the dev_t is not 0,0. If a pointer to a parent struct device is passed in, the newly created struct device will be a child of that device in sysfs. The pointer to the struct device will be returned from the call. Any further sysfs files that might be required can be created using this pointer. Returns struct device pointer on success, or ERR_PTR on error. NOTE
the struct class passed to this function must have previously been created with a call to class_create. COPYRIGHT
Kernel Hackers Manual 2.6. July 2010 DEVICE_CREATE(9)
All times are GMT -4. The time now is 06:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy