Solved

Easiest way to read from device inside another kernel driver

Posted on 2008-06-24
5
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

752 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