Where Do Char Device Appear After Cdev_Add() Registers Successfully with Major on 117.

What does actually cdev_add() do? in terms of registering a device to the kernel

You can read Linux Device Driver. It is a little bit old, but the main ideas are the same. It is difficoult to explain a simple operation like cdev_add() and all the stuff around in few lines.

I suggest you to read the book and the source code. If you have trouble to navigate your source code, you can use some tag system like etags + emacs, or the eclipse indexer.

How to access a dynamic character device from userspace?

As soon as a character device gets registered with a dynamic major number, the corresponding information appears in /proc/devices and thus can be retrieved by a user-space application/script in order to create an appropriate node.

For a better example you may refer to Linux Device Drivers book (3rd edition), for instance, a script to read /proc/devices is shown on this page.

Linux module __must_check annotation

__must_check is defined as:

#define __must_check __attribute__((warn_unused_result))

Quotes from Common Function Attributes

The warn_unused_result attribute causes a warning to be emitted if a caller of the function with this attribute does not use its return value. This is useful for functions where not checking the result is either a security problem or always a bug, such as realloc.

This is also applied to clang and Intel compiler.

This macro asks compiler to issue a warning if the return value is not used. This is important with function return value to indicate success or failure like scanf, printf, or function return memory like malloc, realloc.

How can I create a device node from the init_module code of a Linux kernel module?

To have more control over the device numbers and the device creation, you could do the following steps (instead of register_chrdev()):

  1. Call alloc_chrdev_region() to get a major number and a range of minor numbers to work with.
  2. Create a device class for your devices with class_create().
  3. For each device, call cdev_init() and cdev_add() to add the character device to the system.
  4. For each device, call device_create(). As a result, among other things, Udev will create device nodes for your devices. There isn’t any need for mknod() or the like. device_create() also allows you to control the names of the devices.

There are probably many examples of this on the Internet, and one of them is here.



Related Topics



Leave a reply



Submit