Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Linux - Kernel This forum is for all discussion relating to the Linux kernel.


  Search this Thread
Old 01-21-2011, 10:05 AM   #1
LQ Newbie
Registered: Jan 2011
Location: Bratislava, Slovak Republic
Distribution: Ubuntu
Posts: 2

Rep: Reputation: 0
Question Linux Driver - how to read/write a block or character device in kernel space (procfs)


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.

Last edited by stybi; 01-27-2011 at 06:08 AM.
Old 01-27-2011, 06:26 AM   #2
LQ Newbie
Registered: Jan 2011
Location: Bratislava, Slovak Republic
Distribution: Ubuntu
Posts: 2

Original Poster
Rep: Reputation: 0
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.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
mount: block device is write-protected, mounting read-only xpucto Linux - Server 10 02-01-2017 02:16 AM
MDIO Read/Write character driver abhaygc Linux - Networking 0 10-27-2010 01:27 AM
procfs read and write doubts... Devika99 Programming 2 11-30-2009 07:08 AM
How to write a character device driver to read the processor's GDT kiranshadow Linux - General 1 05-10-2008 11:28 AM > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 07:08 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration