Solved

Easiest way to read from device inside another kernel driver

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Linux centos 5.7 3 87
why "." vs "->" 23 119
An API detour question 7 79
outlook office 365 8 81
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
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 how to create, access, and change arrays in the C programming language.

919 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

21 Experts available now in Live!

Get 1:1 Help Now