Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of class and class device?

I followed some tutorials that explained how to write Linux kernel modules and I am a bit confused. Even after reading the official "documentation", I have poor understanding of the concepts.

After creating a character device (register_chrdev), I see it is common to use a combination of the following functions:

class_create

class_device_create

device_create

I was not able to understand, what is a class, device and, class device and driver?

Which one of these actually responsible to create an entry under /proc/?

like image 386
Artium Avatar asked Dec 11 '22 07:12

Artium


1 Answers

Rather than going into what's a class, or what's a device (I'm no expert in Linux kernel), I will address the question as follows.

After creating the character device, you want to be able to access it from the user space. To do this, you need to add a device node under /dev. You can do this in two ways.

Use mknod to manually add a device node (old)

mknod /dev/<name> c <major> <minor>

OR

Use udev

This is where the class_create and device_create or class_device_create (old) come in.

To notify udev from your kernel module, you first create a virtual device class using

struct class * class_create(owner, name)

Now, the name will appear in /sys/class/<name>.

Then, create a device and register it with sysfs.

struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...)

Now, device name will appear in /sys/devices/virtual/<class name>/<device name> and /dev/<device name>

It's not clear what you are asking about the /proc entry.

After your module is loaded, it will appear in /proc/modules (do a cat /proc/modules to see it). And, after you allocate the device numbers, say with

int register_chrdev_region(dev_t first, unsigned int count, char *name)

, the name will appear in /proc/devices (do a cat /proc/devices to see it).

And, please check the kernel sources for these functions as well, as they provide a good description of what they do in their comments.

The good old LDD3 does not provide these mechanisms, but it's a very good source.

like image 179
dhanushka Avatar answered Dec 26 '22 10:12

dhanushka