What is the purpose of class and class device?
Asked Answered
C

2

8

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/?

Carol answered 16/2, 2018 at 18:20 Comment(2)
Forget about /proc, nowadays devices are advertised through sysfs.Proteolysis
@Proteolysis Can you suggest any good reading about the correct and modern way of writing device drivers?Carol
A
21

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.

Araliaceous answered 17/2, 2018 at 3:3 Comment(7)
is there any logic in class names? for example this one: /sys/class/leds/omega2p:amber:system what is :amber:system mean?! thanksJerid
@Jerid I think you can give any descriptive name for the class. Here, amber could be the led color, and system could be related to the led's designated functionality. I just did a search and found this. If you have the sources, you can see how it is implemented.Araliaceous
@Araliaceous any points on why class.h file is empty in kernel headers?Hennahane
@Hennahane Can you please provide a link to the particular file you are talking about (e.g. from Elixir Bootlin)?Araliaceous
sure @dhanushka...this is the link elixir.bootlin.com/linux/latest/source/include/linux/device/…. So in my system the class.h is empty(no contents) i dont know why and how class_create gets linked without that header. Thanks.Hennahane
@Hennahane Then the class_create macro should be in a different header, probably device.h? You can perform a search and verify it's there in a header.Araliaceous
thanks much @Araliaceous its there in device.h.Hennahane
C
1

From Essential Linux Device Drivers:

class_create() -> It add the device entry in sysfs.

device_create() -> It generates a uevent, which is then captured by udevd, leading to the creation of the node in /dev.

/proc/ entry is created when you initialize the device.

alloc_chardev_region(...) or register_chrdev_region(...);

Cognition answered 10/8 at 17:21 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Inspectorate

© 2022 - 2024 — McMap. All rights reserved.