Difference between revisions of "Embedded-Linux-Device-Driver/C3/File-Operations-on-a-Device/English"
(Created page with " {| style="border-spacing:0;" | style="border:1pt solid #000000;padding:0.176cm;"| '''Visual cue :''' | style="border:1pt solid #000000;padding:0.176cm;"| '''Narration :''' |...") |
Nancyvarkey (Talk | contribs) |
||
Line 23: | Line 23: | ||
* '''VirtualBox''' version '''5.2.''' | * '''VirtualBox''' version '''5.2.''' | ||
* '''Ubuntu Linux 18.04 LTS''' operating system. | * '''Ubuntu Linux 18.04 LTS''' operating system. | ||
− | * '''Linux kernel '''version | + | * '''Linux kernel '''version 5.0.0-31 '''generic '''and |
* '''gedit text editor''' | * '''gedit text editor''' | ||
|- | |- | ||
Line 36: | Line 36: | ||
If not, then go through the '''C/C++''' and '''Linux''' spoken tutorials on this website. | If not, then go through the '''C/C++''' and '''Linux''' spoken tutorials on this website. | ||
|- | |- | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| Point to the '''folder''' and''' file''' in ''' | + | | style="border:1pt solid #000000;padding:0.176cm;"| Point to the '''folder''' and''' file''' in '''Desktop''' |
Point to the '''files.''' | Point to the '''files.''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver '''folder on the | + | | style="border:1pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver '''folder on the '''Desktop'''. |
− | Here I have created a new directory | + | Here I have created a new directory as '''FileOperations.''' |
− | In this directory, I have saved a '''simple_driver dot c driver file, Makefile and user.c.''' | + | In this directory, I have saved a '''simple_driver dot c driver''' file, '''Makefile and '''user.c.''' |
− | I’ll use these files | + | I’ll use these files for demonstration. |
|- | |- | ||
Line 51: | Line 51: | ||
'''Code files:''' | '''Code files:''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The files used in this tutorial are available in the '''Code Files '''link on this tutorial page. | + | | style="border:1pt solid #000000;padding:0.176cm;"| |
+ | * The files used in this tutorial are available in the '''Code Files '''link on this tutorial page. | ||
* Please download and extract them | * Please download and extract them | ||
* Make a copy and then use them while practising | * Make a copy and then use them while practising | ||
Line 59: | Line 60: | ||
About new_device | About new_device | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| We have already registered and created a device called '''new_device '''in the earlier tutorial. | + | | style="border:1pt solid #000000;padding:0.176cm;"| We have already registered and created a '''device''' called '''new_device '''in the earlier tutorial. |
− | In this tutorial, we will perform file operations such as '''open''' and '''close''' on''' new_device.''' | + | In this tutorial, we will perform '''file operations''' such as '''open''' and '''close''' on''' new_device.''' |
− | We will use a user program called '''user.c '''to access this device. | + | We will use a '''user program''' called '''user.c '''to access this '''device'''. |
|- | |- | ||
Line 74: | Line 75: | ||
| style="border:1pt solid #000000;padding:0.176cm;"| Go to the directory '''FileOperations '''as shown here. | | style="border:1pt solid #000000;padding:0.176cm;"| Go to the directory '''FileOperations '''as shown here. | ||
− | Press '''Enter '''key after every | + | Press '''Enter '''key after every '''command'''. |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit simple_driver.c''' | | style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit simple_driver.c''' | ||
Line 80: | Line 81: | ||
| style="border:1pt solid #000000;padding:0.176cm;"| Type''' gedit space simple_driver dot c.''' | | style="border:1pt solid #000000;padding:0.176cm;"| Type''' gedit space simple_driver dot c.''' | ||
− | I have used the same | + | I have used the same file''' simple_driver dot c '''which we used earlier. |
− | Here, I have implemented the basic file functions for the | + | Here, I have implemented the basic '''file functions''' for the '''device new_device.''' |
− | Using these''' ''' | + | Using these '''functions, users''' can perform the '''open '''and '''close operations '''on a '''device'''. |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.176cm;"| | | style="border:1pt solid #000000;padding:0.176cm;"| | ||
Line 97: | Line 98: | ||
Highlight '''struct cdev.''' | Highlight '''struct cdev.''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| We have to include these '''header files '''to perform file''' ''' | + | | style="border:1pt solid #000000;padding:0.176cm;"| We have to include these '''header files '''to perform file related '''operations'''. |
The''' cdev dot h header '''file is used to create the''' character device structure.''' | The''' cdev dot h header '''file is used to create the''' character device structure.''' | ||
Line 113: | Line 114: | ||
'''struct file_operations structure''' | '''struct file_operations structure''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| * '''File operations''' are defined in the''' kernel''' as the instance of '''struct file_operation.''' | + | | style="border:1pt solid #000000;padding:0.176cm;"| |
− | * This is the most important structure of a driver defined in the '''fs dot h file.''' | + | * '''File operations''' are defined in the''' kernel''' as the instance of '''struct file_operation.''' |
+ | * This is the most important '''structure''' of a '''driver''' defined in the '''fs dot h file.''' | ||
* Each field of this '''structure '''is a '''function pointer.''' | * Each field of this '''structure '''is a '''function pointer.''' | ||
Line 123: | Line 125: | ||
| style="border:1pt solid #000000;padding:0.176cm;"| Here, I have defined the''' file_operations structure '''for our '''device.''' | | style="border:1pt solid #000000;padding:0.176cm;"| Here, I have defined the''' file_operations structure '''for our '''device.''' | ||
− | We will implement two | + | We will implement two '''functions - mydevice_open '''and''' mydevice_release '''in a '''driver'''. |
|- | |- | ||
Line 129: | Line 131: | ||
| style="border:1pt solid #000000;padding:0.176cm;"| You have to declare the prototypes of these '''functions '''at the start of the '''driver.''' | | style="border:1pt solid #000000;padding:0.176cm;"| You have to declare the prototypes of these '''functions '''at the start of the '''driver.''' | ||
− | You can get these '''functions ''' | + | You can get these '''functions declarations''' from the '''fs dot h header file'''. |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.176cm;"| | | style="border:1pt solid #000000;padding:0.176cm;"| | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now let us see the''' kernels functions''' used to implement the file operations. | + | | style="border:1pt solid #000000;padding:0.176cm;"| Now let us see the''' kernels functions''' used to implement the '''file operations'''. |
|- | |- | ||
Line 142: | Line 144: | ||
| style="border:1pt solid #000000;padding:0.176cm;"| The '''cdev_init() function''' is used to initialize the members of the '''cdev structure.''' | | style="border:1pt solid #000000;padding:0.176cm;"| The '''cdev_init() function''' is used to initialize the members of the '''cdev structure.''' | ||
− | '''cdev''' specifies a''' ''' | + | '''cdev''' specifies a '''pointer''' to the''' cdev structure''' to initialize. |
− | '''fops''' specifies a''' ''' | + | '''fops''' specifies a '''pointer''' to '''device file operation structure.''' |
|- | |- | ||
Line 152: | Line 154: | ||
Highlight '''my_cdev''' and '''fops.''' | Highlight '''my_cdev''' and '''fops.''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| In the '''init_function''', we have already created a | + | | style="border:1pt solid #000000;padding:0.176cm;"| In the '''init_function''', we have already created a '''device new_device '''and its '''class.''' |
− | After that we have to initialize the '''cdev structure '''of a device as shown here. | + | After that we have to initialize the '''cdev structure '''of a '''device''' as shown here. |
It will establish a connection between '''cdev structure '''and '''file_operations.''' | It will establish a connection between '''cdev structure '''and '''file_operations.''' | ||
Line 163: | Line 165: | ||
Highlight '''dev''' | Highlight '''dev''' | ||
Highlight '''count''' | Highlight '''count''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| '''cdev_add()''' adds a '''cdev''' to the system to complete the registration of a device. | + | | style="border:1pt solid #000000;padding:0.176cm;"| '''cdev_add()''' adds a '''cdev''' to the '''system''' to complete the registration of a '''device'''. |
− | It returns a negative error code on failure. | + | It '''returns''' a '''negative error code''' on failure. |
− | '''p '''specifies the cdev structure for the device. | + | '''p '''specifies the '''cdev structure''' for the '''device'''. |
− | '''dev''' specifies a device number. | + | '''dev''' specifies a '''device''' number. |
− | '''count '''specifies | + | '''count '''specifies a number of consecutive minor numbers corresponding to this '''device'''. |
|- | |- | ||
Line 187: | Line 189: | ||
If the registration fails then we have to destroy a '''device''' and its '''class. ''' | If the registration fails then we have to destroy a '''device''' and its '''class. ''' | ||
− | Also unregister the device from the system as shown here. | + | Also unregister the '''device''' from the system as shown here. |
− | This '''printk''' message will show the successful execution of the '''cdev_add() | + | This '''printk''' message will show the successful execution of the '''cdev_add() function'''. |
|- | |- | ||
Line 195: | Line 197: | ||
Highlight '''printk''' message | Highlight '''printk''' message | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| Now we will learn how to implement the''' device | + | | style="border:1pt solid #000000;padding:0.176cm;"| Now we will learn how to implement the''' device''' file '''functions''' |
− | Here, I have defined the '''open''' | + | Here, I have defined the '''open function''' with a '''printk''' message. |
− | '''mydevice_open() ''' | + | '''mydevice_open() function''' gets executed when the '''user program''' opens the '''device'''. |
This''' printk''' message will display in the '''kernel log level.''' | This''' printk''' message will display in the '''kernel log level.''' | ||
Line 208: | Line 210: | ||
Highlight '''printk message''' | Highlight '''printk message''' | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| Similarly, I have defined the '''release ''' | + | | style="border:1pt solid #000000;padding:0.176cm;"| Similarly, I have defined the '''release function''' with a '''printk''' message. |
'''mydevice_release() function''' gets executed when the '''user''' '''program''' closes the device. | '''mydevice_release() function''' gets executed when the '''user''' '''program''' closes the device. | ||
Line 391: | Line 393: | ||
About Spoken Tutorial Project: | About Spoken Tutorial Project: | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| * The video at the following link summarizes the Spoken Tutorial project. | + | | style="border:1pt solid #000000;padding:0.176cm;"| |
+ | * The video at the following link summarizes the Spoken Tutorial project. | ||
* Please download and watch it. | * Please download and watch it. | ||
Line 398: | Line 401: | ||
Spoken Tutorial workshops: | Spoken Tutorial workshops: | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team | + | | style="border:1pt solid #000000;padding:0.176cm;"| The''' Spoken Tutorial Project''' Team conducts workshops and gives certificates. |
− | + | ||
− | + | ||
− | + | ||
For more details, please write to us. | For more details, please write to us. | ||
Line 409: | Line 409: | ||
Forum questions: | Forum questions: | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| | + | | style="border:1pt solid #000000;padding:0.176cm;"| Please post your timed queries in this forum. |
|- | |- | ||
Line 415: | Line 415: | ||
Forum for specific questions: | Forum for specific questions: | ||
− | | style="border:1pt solid #000000;padding:0.176cm;"| * Do you have any general or technical questions on Embedded Linux Device Driver? | + | | style="border:1pt solid #000000;padding:0.176cm;"| |
+ | * Do you have any general or technical questions on Embedded Linux Device Driver? | ||
* Please visit the FOSSEE forum and post your question. | * Please visit the FOSSEE forum and post your question. | ||
|- | |- |
Revision as of 15:38, 28 September 2020
Visual cue : | Narration : |
Slide 1 : | Welcome to the spoken tutorial on File Operations on a Device. |
Slide 2 :
Learning objectives: |
In this tutorial, we will learn how to
|
Slide 3:
System Requirements: |
To record this tutorial, I am using,
|
Slide 4 :
Prerequisites: |
To follow this tutorial, you should be familiar with:
If not, then go through the C/C++ and Linux spoken tutorials on this website. |
Point to the folder and file in Desktop
Point to the files. |
Go to the DeviceDriver folder on the Desktop.
Here I have created a new directory as FileOperations. In this directory, I have saved a simple_driver dot c driver file, Makefile and user.c. I’ll use these files for demonstration. |
Slide 5:
Code files: |
|
Slide 6:
About new_device |
We have already registered and created a device called new_device in the earlier tutorial.
In this tutorial, we will perform file operations such as open and close on new_device. We will use a user program called user.c to access this device. |
Open the terminal | Open the terminal by pressing ALT+Ctrl+T keys simultaneously. |
Type >> cd Desktop/DeviceDriver/FileOperations | Go to the directory FileOperations as shown here.
Press Enter key after every command. |
Type >> gedit simple_driver.c | Type gedit space simple_driver dot c.
I have used the same file simple_driver dot c which we used earlier. Here, I have implemented the basic file functions for the device new_device. Using these functions, users can perform the open and close operations on a device. |
Let me explain the code. | |
Highlight #include<linux/fs.h> and
#include<linux/cdev.h> Highlight cdev.h Highlight struct cdev. |
We have to include these header files to perform file related operations.
The cdev dot h header file is used to create the character device structure. The cdev structure is used to describe character devices in the Linux kernel. Here, my_cdev represents our device cdev structure in the kernel. |
Now let us see how to implement the device file operations. | |
Highlight :
struct file_operations structure |
|
Highlight struct file_operations structure
Highlight mydevice_open and mydevice_release |
Here, I have defined the file_operations structure for our device.
We will implement two functions - mydevice_open and mydevice_release in a driver. |
Highlight function declarations. | You have to declare the prototypes of these functions at the start of the driver.
You can get these functions declarations from the fs dot h header file. |
Now let us see the kernels functions used to implement the file operations. | |
Highlight void cdev_init()
Highlight cdev Highlight fops |
The cdev_init() function is used to initialize the members of the cdev structure.
cdev specifies a pointer to the cdev structure to initialize. fops specifies a pointer to device file operation structure. |
Highlight init_function()
Highlight cdev_init(&my_cdev, &fops) Highlight my_cdev and fops. |
In the init_function, we have already created a device new_device and its class.
After that we have to initialize the cdev structure of a device as shown here. It will establish a connection between cdev structure and file_operations. |
Highlight int cdev_add()
Highlight p Highlight dev Highlight count |
cdev_add() adds a cdev to the system to complete the registration of a device.
It returns a negative error code on failure. p specifies the cdev structure for the device. dev specifies a device number. count specifies a number of consecutive minor numbers corresponding to this device. |
Highlight cdev_add()
Highlight my_cdev Highlight the device_destroy(cl,device_num), class_destroy(cl), unregister_chrdev_region(device_num,1); Highlight printk |
I used this function to add the cdev structure of new_device as shown here.
If the registration fails then we have to destroy a device and its class. Also unregister the device from the system as shown here. This printk message will show the successful execution of the cdev_add() function. |
Highlight mydevice_open()
Highlight printk message |
Now we will learn how to implement the device file functions
Here, I have defined the open function with a printk message. mydevice_open() function gets executed when the user program opens the device. This printk message will display in the kernel log level. |
Highlight mydevice_release()
Highlight printk message |
Similarly, I have defined the release function with a printk message.
mydevice_release() function gets executed when the user program closes the device. This printk message will display in the kernel log level. |
Let us see the exit function of a driver. | |
void cdev_del (struct cdev * cdev)
Highlight cdev |
cdev_del() function will remove a cdev structure of a device from the kernel.
cdev specifies the cdev structure to be removed. |
Highlight cdev_del() | Here, we remove the cdev structure of a new_device before removing its file. |
Now let us write the program to perform the file operations on the device file.
Save and close the file. | |
Type >> gedit user.c. | Type gedit user dot c.
Here, I have created a simple user program which will open and close the device. |
Highlight open() function
Highlight printf Highlight close() function |
The open() system call will internally call the open function of new_device from its driver.
printf function will print the message if the open function fails to open the new_device. close() system call will internally call the close function of a new_device from its driver. Save and close the file. |
Type >> gedit Makefile
Save the Makefile |
Let us create a Makefile to compile a driver.
Type gedit space Makefile. Type this code in a Makefile or you can use the downloaded Makefile. Save and close the file. |
Type sudo su
Type >> make all |
Let us compile the driver.
Type sudo su and password to be a superuser. Type make space all. Clear the screen. |
Type >> insmod simple_driver.ko | Now let us load the driver into the kernel.
For that, type insmod space simple_driver dot ko. |
Type >> clear | Clear the screen. |
Type >> dmesg | grep simple_driver
Highlight the output |
Let us see the loaded printk messages from the initialisation function.
Type dmesg space pipe space grep space simple_driver. Here, you can see the cdev structure of a new_device registered successfully. |
Type >> clear | Clear the screen. |
Type >> gcc -c user. c
Type >> gcc -o user user.o. Type >> ./user Highlight output |
Now let us compile the user.c file
Type gcc space hyphen c space user dot c Then type gcc space hyphen o space user space user dot o Now to execute the program type dot slash user. Here, it indicates that the new_device opened successfully. |
Type >> clear | Clear the screen. |
Type >> dmesg | grep simple_driver
Highlight the respective messages Type >> clear |
Type the command as shown here.
The output shows that open and close functions of the driver are executed successfully. It indicates that the new_device is opened and closed by the user program. Clear the screen. |
Type rmmod simple_driver.ko
Type dmesg | grep simple_driver Highlight the output Type >> clear |
Now let us unload the driver.
Type rmmod space simple_driver dot ko To see the unloaded printk messages type this dmesg command. It indicates that the cdev structure of a new_device removed from the kernel. Clear the screen. |
Type >> make clean | Let us remove the object files created after the compilation.
Type make space clean. |
With this, we come to the end of this tutorial. Let us summarize. | |
Slide 7:
Summary: |
In this tutorial, we learnt how to
|
Slide 8 :
Assignment : |
As an assignment,
|
Slide 9 :
About Spoken Tutorial Project: |
|
Slide 10:
Spoken Tutorial workshops: |
The Spoken Tutorial Project Team conducts workshops and gives certificates.
For more details, please write to us. |
Slide 11:
Forum questions: |
Please post your timed queries in this forum. |
Slide 12 :
Forum for specific questions: |
|
Slide 13 :
Acknowledgment: |
The Spoken Tutorial Project is funded by MHRD, Government of India. |
Slide 14:
Thank you slide: |
This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
This is Mayuri Panchakshari signing off. Thanks for watching. |