7

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

Artium
  • 5,147
  • 8
  • 39
  • 60

1 Answers1

17

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.

dhanushka
  • 10,492
  • 2
  • 37
  • 47
  • is there any logic in class names? for example this one: /sys/class/leds/omega2p:amber:system what is :amber:system mean?! thanks – aleXela Nov 21 '18 at 10:24
  • @aleXela 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](https://docs.onion.io/omega2-docs/the-omega-led.html). If you have the sources, you can see how it is implemented. – dhanushka Nov 22 '18 at 13:55
  • @dhanushka any points on why class.h file is empty in kernel headers? – RaHuL Aug 15 '22 at 06:54
  • @RaHuL Can you please provide a link to the particular file you are talking about (e.g. from Elixir Bootlin)? – dhanushka Aug 15 '22 at 12:39
  • sure @dhanushka...this is the link https://elixir.bootlin.com/linux/latest/source/include/linux/device/class.h#L273. 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. – RaHuL Aug 16 '22 at 08:54
  • 1
    @RaHuL 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. – dhanushka Aug 16 '22 at 11:57
  • thanks much @dhanushka its there in device.h. – RaHuL Aug 16 '22 at 13:17