Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Kernel Driver Module: Handle multiple device files using a single driver

I am a beginner to kernel module programming. And using resources online (especially this tutorial) I have managed to write most of my driver.

The driver basically is a character device which maps different areas of SRAM into separate files. You can see the actual code here.

Right now I am able to successfully create 4 files under /dev and reading/writing the first file works too but the other 3 files do not work. I am using minor number to distinguish between the files and assign the starting address accordingly.

Questions:

  1. Why are the other files not working?
  2. Is there a better way to implement the module?

Thanks.

like image 773
Prateek Avatar asked Dec 01 '25 08:12

Prateek


1 Answers

Line 141, "if (cdev_add(&c_dev, first, 1) == -1)", only applies the file_operations struct to the first device. You should use MAXDEVICES instead of 1 here

On another note, the init code is messy (better use goto and not duplicate the cleanup for every function that can fail) and in some cases plain wrong (device_destroy() before any devices were created, resource leak in case you fail the create a device that isn't the first). The entire file does not stand up to kernel coding conventions.

like image 120
talshorer Avatar answered Dec 04 '25 21:12

talshorer



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!