Solved

Easiest way to read from device inside another kernel driver

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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.

615 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