LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-22-2013, 05:32 AM   #1
tamilvanan
LQ Newbie
 
Registered: Sep 2012
Posts: 10

Rep: Reputation: Disabled
Regarding how the parameters to the read function is passed in simple char driver


Hi everyone,

I am newbei to driver programming i am started writing the simple char driver
which i have attached . Then i created special file for my char driver
mknod /dev/simple-driver c 250 0 .when it type cat /dev/simple-driver. it shows the string "Hello world tamil_vanan!". i know that function

static ssize_t device_file_read(
struct file *file_ptr
, char __user *user_buffer
, size_t count
, loff_t *possition)
{
printk( KERN_NOTICE "Simple-driver: Device file is read at offset = %i, read bytes count = %u"
, (int)*possition
, (unsigned int)count );

if( *possition >= g_s_Hello_World_size )
return 0;

if( *possition + count > g_s_Hello_World_size )
count = g_s_Hello_World_size - *possition;

if( copy_to_user(user_buffer, g_s_Hello_World_string + *possition, count) != 0 )
return -EFAULT;

*possition += count;
return count;
}
is get called . This is mapped to (*read) in file_opreation structure of my driver .My question is how this function is get called , how the parameters like struct file,char,count, offset are passed bcoz is i simply typed cat command ..Please elabroate how this happening

Regards,

Tamil Vanan K
Attached Files
File Type: txt simple_driver.txt (3.4 KB, 9 views)
 
Old 03-22-2013, 12:00 PM   #2
Lilgamesh
Member
 
Registered: Mar 2013
Posts: 33

Rep: Reputation: Disabled
cat /dev/simple_driver

means you want to print the contents of "/dev/simple_driver", that means you want to read, so your read function is called. it same as
fd = open("/dev/simple_driver");
nr = read(fd, buf, 80);

similarly if you do
echo "write to simple driver" > /dev/simple_driver will call your write function.

Kernel knows this as you specified the name of the device(simple_device) during char device registering in init function.
 
Old 03-23-2013, 10:40 AM   #3
tamilvanan
LQ Newbie
 
Registered: Sep 2012
Posts: 10

Original Poster
Rep: Reputation: Disabled
Regarding simple char driver

Hi ,

Ya thanks for the information. i know the that read and write requested are redirected to driver read ,write functions. but i like to know how its happening and how the read operation in my driver gets all the four parameters.


Regards,

Tamil Vanan K
 
Old 03-24-2013, 10:57 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
You will probably be best served by looking in the kernel sources to find the answer to that. Or ask on a forum or mailing list where the inhabitants deal specifically in kernel internals.

A more general answer is that the kernel maps the device node to your driver when you load your driver. When a userspace system call is made for reading or writing your device, the kernel uses the association it has previously remembered, and dispatches the accordant function in your driver. The cat and echo commands are userspace applications that do read and write system calls, and these result in the calls that you are witnessing. Any application that calls read(), write(), open(), close(), seek(), ioctl(), stat(), etc on a device supported by your driver will cause the respective function in your driver to be invoked.

--- rod.
 
  


Reply

Tags
drivers, read


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CHAR driver's open function issue barunparichha Linux - Software 3 02-19-2013 12:32 AM
Problem with char driver write function - infinite loop Cherubim Linux - Kernel 1 10-13-2009 02:08 AM
can't read from the device driver using read() function. parthi_s Linux - Newbie 1 03-23-2009 07:16 AM
how to test a simple char driver in FC5? agpperera Linux - Newbie 2 10-17-2007 02:16 AM
Can parameters passed to a php be hide ? cjagdish69 Linux - Software 1 11-11-2003 08:09 AM


All times are GMT -5. The time now is 07:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration