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 asrealloc
.
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()
):
- Call
alloc_chrdev_region()
to get a major number and a range of minor numbers to work with. - Create a device class for your devices with
class_create()
. - For each device, call
cdev_init()
andcdev_add()
to add the character device to the system. - 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 formknod()
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
Sox Batch Process Under Debian
Running Linux Container on Docker Windows
Docker: Permission Denied to Local MySQL Volume
How to Disable Qt's Behavior on Linux of Capturing Arrow Keys for Widget Focus Navigation
Communication Between Linked Docker Containers
Capture Network Traffic on Two Different Ports Simultaneously
Possible I/O Sync Issue with Ruby Script Under Nohup
Listening for New Processes in Linux Kernel Module
Autostart Javafx Application on Raspberrypi
Toolchain to Crosscompile Applications for Bbb
What Is The Differences and Relationships Between "Process", "Threads", "Task" and "Jobs" in Linux
Echo 'The Character - (Dash) in The Unix Command Line
How to Include Cutil.H in Linux
How to Access Google Drive from Cli Cyberduck
How to Use Find on Dirs with White Spaces
How to Link a Linux's Thread Tid and a Pthread_T "Thread Id"
Stream Static Image to V4L2Loopback Using Obs with V4L2 Plugin, Ffmpeg or Gstreamer