Solved

Easiest way to read from device inside another kernel driver

Posted on 2008-06-24
5
379 Views
Last Modified: 2010-04-21
I have some data available in /dev/mtd/0ro. I have no problem to get this data from userspace by opening this device and reading from it. But I need to get this data also within some kernel driver.

What is the easiest way I can do this?
What should I watch for?
Any hints/examples are welcomed.

Thank you.
0
Comment
Question by:ineya
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
kolrabi earned 500 total points
ID: 21878149
I never tried it, but this should work:

1. Get a file pointer for the device you want to open using filp_open.
2. Use the vfs_* functions to operate on the file. (See your linux/fs.h for syntax)
3. Close the file using filp_close.

Something like the snippet (which lacks any error checking).

HTH :)
struct file *f = filp_open("/dev/mtd/0ro", O_RDONLY, 0444);
 
/* seek to the beginning, not sure if this is really needed. */
vfs_llseek(f, 0, 0);
 
/* read some data */
vfs_read(f, buffer, buffer_size, &f->f_pos);
 
/* do something with the data ... */
 
/* close the file pointed to by f. */
filp_close(f, 0);

Open in new window

0
 
LVL 3

Author Comment

by:ineya
ID: 21882558
Another solution seems to be to rely on EXPORT_SYMBOL of mtd driver:
http://linux.derkeiler.com/Mailing-Lists/Kernel/2003-10/3671.html
0
 
LVL 3

Author Comment

by:ineya
ID: 21908926
@kolrabi: Do you know if there is some documentation for that filp_open, vfs_read? I'm failed trying to google it.
0
 
LVL 1

Assisted Solution

by:kolrabi
kolrabi earned 500 total points
ID: 21914665
As far as I can tell from the header files, the syntax for filp_open() is the same as for open() but returning a "struct file" pointer instead of a file descriptor.

The syntax for the vfs_* functions seems to be the same as the syntax for the callbacks specified in the f_op member (struct file_operations) of the file pointer.

The file struct and the file_operations struct are explained in a little bit more detail here:

http://www.moses.uklinux.net/patches/lki-3.html#ss3.4

0
 
LVL 3

Author Closing Comment

by:ineya
ID: 31470094
I didn't get to implement it, but it looks promising, thanks for the effort.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question