I'm reading through the Linux Kernel Module programming guide and I am a bit onfused about how device drivers work.

I tried following along and making a basic device driver, which shows up in /proc/devices, which is great. According to the guide, to interface with the driver in Linux, a userspace program can interact with that device's device file in /dev/. Right now though, the module I've written doesn't generate a device file. What manages the generation of new files? Is that something I'm supposed to do in module_init?

And assuming I've gone and gotten a device file made, is interacting with the driver, in say a C++ program, just opening up an fstream and reading or writing that file, or are there specific libraries used to interact with device files in userspace?

  • Classically, you use the mknod command to create the device file with the correct major and minor device numbers. Programs then interact with your device driver by opening the device and using the read, write, seek, close system calls. Remember, on UNIX-like systems, everything's a file. Jun 15, 2016 at 8:07
  • Another interesting thing to do with most device files is to call ioctl() with driver specific parameters. Jun 15, 2016 at 8:20
  • To make a device file magically appear (through udev) you must - as far as I understand it - trigger certain uevents. Jun 15, 2016 at 8:23
  • So what calls mknod? I could do it manually from the terminal, but I never do that for other stuff like plugging in a usb?
    – Zephyr
    Jun 15, 2016 at 11:18

1 Answer 1


Files in /dev are mostly created by the udev process which receives events from the kernel by listening to the netlink socket for NETLINK_KOBJECT_UEVENT (see man 7 netlink). The events are sent when a new kernel object (kobject) is created. These objects are also seen in the /sys sysfs filesystem. In particular, files named dev in the /sys/devices subtree hold the major and minor number of the node to use for a given device. Eg

$ cat /sys/devices/pnp0/00:05/tty/ttyS0/dev

This is explained in this online chapter 14 pdf of Linux Device Drivers, Third Edition from 2005, The Linux Device Model. It is a little outdated and does not mention netlink for example.

  • Ok, I followed some other references and found the libraries that register a device for you so now I have a device file. So is there ever actually anything IN that file, or is it just a transfer to the driver? I tried making a program that just opens an fstream to the device file, writes a thing into it, and closes it. Then when I cat /var/log/messages, I see the message that my driver is supposed to report when it gets read, so that seemed to work. But if I cat the actual file itself, it's empty.
    – Zephyr
    Jun 15, 2016 at 16:01
  • 1
    As you say, a cat of your /dev file just calls your read() routine. You should then use copy_to_user() to copy data to the user provided buffer, and return the number of bytes copied. See chapter 3 of the same book and the scull_read() example.
    – meuh
    Jun 15, 2016 at 16:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .