LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Regarding how the parameters to the read function is passed in simple char driver (http://www.linuxquestions.org/questions/programming-9/regarding-how-the-parameters-to-the-read-function-is-passed-in-simple-char-driver-4175455109/)

tamilvanan 03-22-2013 04:32 AM

Regarding how the parameters to the read function is passed in simple char driver
 
1 Attachment(s)
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

Lilgamesh 03-22-2013 11:00 AM

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.

tamilvanan 03-23-2013 09:40 AM

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

theNbomr 03-24-2013 09:57 AM

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.


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