0
votes

I'm currently getting familiar with device drivers, and am reading the Linux Kernel Module Programming Manual. I coded the example in section 6.5, the char_dev.c character device driver. The manual states that, although it is not a strict contract between the user and the kernel, device files should be stores in the /dev directory. Firstly, I add the char_dev.c's object file to the makefile, creating the module char_dev.ko file. Once this is done, I save the .ko module file in the /dev directory. This is where I'm a bit lost. Previously, the simple modules I've been working with have been directly insmod'd and rmmod'd to and form the kernel in a separate directory I created. The char_dev.c device driver should, if it is opened, print out a message stating that it's already been opened N times by the user. It doesn't support any write() operations, and as far as I can tell, it's read() operation doesn't do anything besides copy the message generated from opening the device from kernel space to userspace and return how many bytes are read.

The manual states that through cat'ing /proc/devices I can view it's device file, but doing this doesn't show any char_dev device driver with any kind of major or minor number assigned.

I don't explicitly load the module into the kernel, although I assume I should, but the manual doesn't explicitly say to do this.

Any help would be greatly appreciated as this is my first time writing a / working with device drivers.

1

1 Answers

3
votes

You're not supposed to place your module file in /dev; it won't do anything useful by being there. You can put it wherever you want, but certainly you do have to insmod it. That might be an accidental omission from the manual, or they thought it was obvious enough that they didn't need to say it.

What you're supposed to create in /dev is a new special file, aka device file, with the major and minor numbers corresponding to whatever you used in your driver's source code, or whatever was automatically assigned by the kernel. You do this with the mknod command: sudo mknod /dev/mydevice c 123 45, where 123, 45 are the major and minor device numbers respectively. There's some explanation of this in Section 5.6 of the Guide. (Note that the example driver in the Guide apparently doesn't look at the minor number, so it can be anything you want.)

You should then be able to do something like sudo cat /dev/mydevice and have your driver's read function get called.

If there are unprivileged users on this machine, use the -m flag to mknod to give the special file more restrictive permissions so they can't access it, e.g. mknod -m 0700 /dev/mydevice c 123 45.

Strictly speaking you don't have to create the special file in /dev, and you could instead do mknod /other/dir/mydevice c 123 45, but it may cause confusion.

Depending on how your distribution manages /dev, the /dev filesystem may not persist across reboots, so you might have to recreate your special file on each boot. It may be managed by a daemon such as udev, and if you're going to use your device permanently, it has config files you can edit to ensure it adds your special file on boot. That is more a system administration than a programming topic, so head over to https://unix.stackexchange.com if you want to know more.