LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   Linux Driver - how to read/write a block or character device in kernel space (procfs) (https://www.linuxquestions.org/questions/linux-kernel-70/linux-driver-how-to-read-write-a-block-or-character-device-in-kernel-space-procfs-857807/)

stybi 01-21-2011 10:05 AM

Linux Driver - how to read/write a block or character device in kernel space (procfs)
 
Hello,

I'm trying to learn how to make drivers for Linux Kernel. I have looked into some tutorials and used them. I was able to make a working block device driver as well as a character device driver and some procfs entries. These are really easy if you don't need to access the hardware really. Problems start when you try to do real things like these:
  1. I want to make a procfs entry, which will dump a string from the flash device /dev/mtdblock3 at a specific position and a specific maximum length. I was able to do it in user space, but I want to have it in the procfs for easy access from other apps. I have a template of the module without the access to block device driver (just simulated output), but now I'm lost.
    1. which functions (API) should I use?
    2. is it possible to call the device by it's major/minor numbers if the /dev/mtdblock3 node is not created or should I call mknod first?
  2. The second thing is almost the same as the first one, only I want to communicate with a character device. This time it may be even more complicated, as this is a "message based" interface. I have to "write a message" to the driver and wait for the response (read the message). So I have two problems here:
    1. which functions (API) should I use?
    2. may I use blocking read/write or should the driver support non-blocking read/write? As far as I know it's a proprietary driver which supports both blocking and non-blocking interface, but the non-blocking interface was never tested thoroughly.

Can somebody help me?

P.S.: I know about an option to open files in kernel space, but it was written on many places, that if you need it than you're driver is wrongly designed. I want to do it correctly. I looked into the md driver, but I'm lost in the code. It's really badly documented.

stybi 01-27-2011 06:26 AM

Some info I found, but still I'm lost
 
I have found the following functions (while studying the md driver), but I don't know how to use them correctly:
  1. open_by_dev_num(dev_t, mode);
    • Probably to open device by its device number, but can I do it if devfs entry is not existing?
  2. _bdevname(dev_t, char *);
    • This is probably to help while logging from the module.
  3. bd_claim(struct block_device, rdev_t);
    • I have no idea how to use it, and what does it do...
  4. blkdev_put(struct block_device);
    • Probably to "return" the driver to kernel? Oposite to open_by_devnum?
  5. bd_release(struct block_device);
    • Probably to "release" the driver - opostite to bd_claim?

There are many other functions using page allocations and "synchronization", but I need just a simple read. I cannot find anything. Even this page sync would be OK, because it would help next time I would like/need to write a driver.

I hope, that someone would be interested in this topic. Probably some similar sample driver can be included in the next edition of the "Linux Device Drivers" book. After I would have a first successful read I would publish the code, so it would be easy to include it there.

Still I have no idea how to read from the character device.


All times are GMT -5. The time now is 06:31 PM.