Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have multiple processes in my linux environment that have to use the same device driver to access a number of similar devices. My problem is that if one process instantaiates a device(by instantiate, I mean mknod ) file, and uses a minor number say 0 and 1 for two devices. So how does the other process while doing the same thing come to know that which minor number to use?

In short, in application space is it possible to find the number of device files that use the same driver?

like image 489
vaibhav3002 Avatar asked Nov 08 '25 00:11

vaibhav3002


2 Answers

My problem is that if one process instantaiates a device(by instantiate, I mean mknod ) file

In *NIX world devices generally assigned major/minor numbers statically and inside of kernel. Statically in a sense that most devices now under Linux have dynamic major/minors and are accessed via devices nodes under /dev (or /proc or /sys).

IOW, user space application cannot do it, because to communicate with the driver (or device) it has to know how the major/minor to already be able to access the driver.

Details would be many, better read on the 3rd chapter of LDD where discussion about major/minor assignment are discussed.

Automatic creation of devices under /dev/ now generally handled by udev which I'm not very familiar with.

like image 76
Dummy00001 Avatar answered Nov 10 '25 20:11

Dummy00001


Assuming that you are referring to a driver that you are creating or at least have the source to, try using minor numbers in numerical order (for simplicity) starting with zero. Have your driver keep track of how many minor numbers/devices have been instantiated and expose that count via an entry in sysfs. When you go to instantiate a new device, use the current value of the variable for the new device's minor number and increment the variable (inside the driver).

like image 21
bta Avatar answered Nov 10 '25 22:11

bta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!