Solved

Easiest way to read from device inside another kernel driver

Posted on 2008-06-24
5
364 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now