How to get a free minor number for a Linux device driver?

I have several processes in my Linux environment that must use the same device driver to access a number of similar devices. My problem is that if one process instantly passes the device (through the instance, I mean the mknod file) and uses the lowest number, say 0 and 1 for two devices. So how does another process, doing the same, find out which minor number to use?

In short, in the application space, you can find the number of device files that use the same driver?

+3
source share
3 answers

Assuming that you mean the driver you are creating, or at least have a source, try using the lower numbers in numerical order (for simplicity), starting from scratch. Ask your driver to keep track of how many minor numbers / devices have been created and invoice this via an entry in sysfs. When you go to instantiate a new device, use the current variable value for the minor number of the new device and increase the variable (inside the driver).

+1
source

My problem is that if one process instantly transfers the device (through the instance, I mean the mknod file)

* NIX world / . , , Linux, / /dev (/proc /sys ).

IOW, , ( ) , / .

, 3- LDD, / .

/dev/now udev, .

+3

Helper numbers used by devices registered with your driver should be visible to applications in the pseudo file system /sys. The file devlocated in the directories / directories /sys/class/...associated with your device contains the major: minor.

Your driver probably uses /sys/class/misc/.../.

0
source

Source: https://habr.com/ru/post/1751265/


All Articles