3

Reading through all the books/articles/tutorials/examples on character device drivers I could find, they all cover how to acquire a major number in my driver code and all suggest that I do so dynamically by invoking alloc_chrdev_region(). To make the actual device node, they all then tell me to invoke mknod outside my driver code either manually or in a script. One reference wants my driver to print the major number. Another wants me to use awk on /proc/devices to find the major number. There is mention of the function mknod(), but it appears to be intended for use in userland code.

I'm obviously missing something since I don't understand why there isn't a way for my driver to be self contained, i.e, I do modprobe my_driver and end up with, say, /dev/my_driver created without having to do anything else.

2 Answers 2

3

The command pair device_create and class_create do the trick in Linux. Check https://stackoverflow.com/questions/5970595/create-a-device-node-in-code

1
  • This appears to be a most interesting approach. I intend to pursue it -- once management puts me back on the project that prompted my question ;-) Commented May 4, 2017 at 21:01
0

Unix has traditionally followed the "mechanism, not policy" tradition. In the case of device drivers, this means the driver identity in the kernel is just the major/minor device number. Naming the device (the "policy*) using mknod has been left to user space.

3
  • A lot of the references I looked at do emphasize the "mechanism, not policy" philosophy. However the /dev directory is full of entries that don't seem to have gotten there via any external scripting that I've been able to find. For example, /dev/ttyACM0 is the device created by the USB CDC/ACM (USB "serial port") driver though I've not yet figured out how it does it. I want my driver to be just like that. Commented Feb 7, 2017 at 20:55
  • The udev device manager manages the entries in /dev. See the Wikipedia article on udev Commented Feb 8, 2017 at 4:37
  • The challenge of Linux is that well written comprehensive documentation is invariably out-of-date while the most up-to-date techniques are poorly documented. Combine that with the mix of old and new techniques that exist in the code base confounded with the difficulty of telling which is which -- well thanks to the gods of coffee and programming that StackExchange exists! The pointer to udev was most useful. Commented May 4, 2017 at 20:59

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.