开发者

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 devic开发者_Python百科e files that use the same driver?


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.


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).


The minor numbers being used by the devices registered by your driver should be visible to applications in the /sys pseudo-filesystem. The dev file, located in the /sys/class/... directory/directories associated with your device, contains the major:minor.

Likely, your driver is using /sys/class/misc/.../.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜