Friday, 20 July 2018

real Char Device Driver

copy_from_user()

This function is used to Copy a block of data from user space (Copy data from user space to kernel space).
unsigned long copy_from_user(void *to, const void __user *from, unsigned long  n);
Arguments
to – Destination address, in kernel space
from – Source address in user space
n – Number of bytes to copy
Returns number of bytes that could not be copied. On success, this will be zero.

copy_to_user()

This function is used to Copy a block of data into user space (Copy data from kernel space to user space).
unsigned long copy_to_user(const void __user *to, const void *from, unsigned long  n);
Arguments
to – Destination address, in user space
from – Source address in kernel space
n – Number of bytes to copy
Returns number of bytes that could not be copied. On success, this will be zero.

Full Driver Code


User Space Application



Compile the User Space Application

Use below line in terminal to compile the user space application.
gcc -o test_app test_app.c

Execution (Output)

As of now, we have driver.ko and test_app. Now we will see the output.
  • Load the driver using sudo insmod driver.ko
  • Run the application (sudo ./test_app)
*********************************
*******WWW.EmbeTronicX.com*******
****Please Enter the Option******
1. Write
2. Read
3. Exit
*********************************
  • Select option 1 to write data to driver and write the string ( In this case i’m going to write “embetronicx” to driver.
1
Your Option = 1
Enter the string to write into driver :embetronicx
Data Writing ...Done!
****Please Enter the Option******
1. Write
2. Read
3. Exit
*********************************
  • That “embetronicx” string got passed to the driver. And driver stored that string in the kernel space. That kernel space was allocated by kmalloc.
  • Now select the option 2 to read the data from the device driver.
2
Your Option = 2
Data Reading ...Done!
Data = embetronicx

No comments:

Post a Comment