Difference between revisions of "Embedded-Linux-Device-Driver/C3/File-Operations-on-a-Device/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(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 :''' |...")
 
 
(One intermediate revision by the same user not shown)
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''' 5.0.0-31 generic '''and
+
* '''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 '''desktop'''
+
| 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 desktop.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver '''folder on the '''Desktop'''.
  
Here I have created a new directory''' '''as '''FileOperations.'''
+
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''' '''for demonstration.
+
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 72: Line 73:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''cd Desktop/DeviceDriver/FileOperations'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''cd Desktop/DeviceDriver/FileOperations'''
| 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 where '''FileOperations''' is saved in your system.
  
Press '''Enter '''key after every command'''.'''
+
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''' '''file''' simple_driver dot c '''which we used earlier.
+
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.'''
+
I have implemented the basic '''file functions''' such as '''open''' and '''close''' for the '''device new_device.'''
  
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 97:
  
 
Highlight '''struct cdev.'''
 
Highlight '''struct cdev.'''
| style="border:1pt solid #000000;padding:0.176cm;"| We have to include these '''header files '''to perform file''' '''related operations.
+
| 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 113:
  
 
'''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_operations.'''
 +
* 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 124:
 
| 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 functions''' mydevice_open '''and''' mydevice_release '''in a driver.
+
We will implement two '''functions - mydevice_open '''and''' mydevice_release '''in a '''driver'''.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' function declarations.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' function declarations.'''
| 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;"| We 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'''.
+
We 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 143:
 
| 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''' '''pointer to the''' cdev structure''' to initialize.
+
'''cdev''' specifies a '''pointer''' to the''' cdev structure''' to initialize.
  
'''fops''' specifies a''' '''pointer to device '''file operation structure.'''
+
'''fops''' specifies a '''pointer''' to '''device file operation structure.'''
  
 
|-
 
|-
Line 152: Line 153:
  
 
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 device '''new_device '''and its '''class.'''
+
| 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 164:
 
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''' '''a number of consecutive minor numbers corresponding to this device.
+
'''count '''specifies a number of consecutive minor numbers corresponding to this '''device'''.
  
 
|-
 
|-
Line 183: Line 184:
  
 
Highlight''' printk'''
 
Highlight''' printk'''
| style="border:1pt solid #000000;padding:0.176cm;"| I used this '''function''' to add the '''cdev structure''' of '''new_device '''as shown here.
+
| style="border:1pt solid #000000;padding:0.176cm;"| '''cdeve_add function''' is used 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. '''
 
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()''' '''function'''.
+
This '''printk''' message will show the successful '''execution''' of the '''cdev_add() function'''.
  
 
|-
 
|-
Line 195: Line 196:
  
 
Highlight '''printk''' message
 
Highlight '''printk''' message
| style="border:1pt solid #000000;padding:0.176cm;"| Now we will learn how to implement the''' device file '''functions'''.'''
+
| 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''' function with a '''printk''' message.  
+
Here, I have defined the '''open function''' with a '''printk''' message.  
  
'''mydevice_open() '''function gets executed when the user program opens the device.
+
'''mydevice_open() function''' gets executed when the '''user program''' opens the '''device'''.
  
This''' printk''' message will display in the '''kernel log level.'''
+
''' printk''' message will display in the '''kernel log level.'''
  
 
|-
 
|-
Line 208: Line 209:
 
Highlight '''printk message'''
 
Highlight '''printk message'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| Similarly, I have defined the '''release '''function with a '''printk''' message.  
+
| 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'''.
  
This '''printk''' message will display in the''' kernel log level'''.
+
This '''printk''' message will display the '''success message''' in the''' kernel log level'''.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| Let us see the '''exit function '''of a driver.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us see the '''exit function '''of a '''driver'''.
  
 
|-
 
|-
Line 222: Line 223:
  
 
Highlight''' cdev'''
 
Highlight''' cdev'''
| style="border:1pt solid #000000;padding:0.176cm;"| '''cdev_del() function''' will remove a''' cdev structure '''of a '''device '''from the kernel.
+
| style="border:1pt solid #000000;padding:0.176cm;"| '''cdev_del() function''' will remove a''' cdev structure '''of a '''device '''from the '''kernel'''.
  
'''cdev '''specifies the '''cdev''' structure to be removed.
+
'''cdev '''specifies the '''cdev structure''' to be removed.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''cdev_del()'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''cdev_del()'''
| style="border:1pt solid #000000;padding:0.176cm;"| Here, we remove the '''cdev structure '''of a '''new_device '''before removing its''' '''file'''.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Here, we remove the '''cdev structure '''of a '''new_device '''before removing its file.
  
 
|-
 
|-
Line 239: Line 240:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit user.c.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit user.c.'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| Type '''gedit user dot c.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''
  
Here, I have created a simple''' user program''' which will open and close the '''device.'''
+
Type '''gedit user dot c.'''
 +
 
 +
Here, I have created a simple''' user program''' which will '''open''' and '''close''' the '''device.'''
  
 
|-
 
|-
Line 250: Line 253:
 
Highlight '''close() function'''
 
Highlight '''close() function'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| The '''open() system call '''will internally call the''' open function''' of '''new_device '''from its driver.
+
| style="border:1pt solid #000000;padding:0.176cm;"| 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.'''
+
'''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.
+
'''close() system call''' will internally call the '''close function '''of a''' new_device''' from its '''driver'''.
  
Save and close the''' '''file'''.'''
+
Save and close the file.
  
 
|-
 
|-
Line 262: Line 265:
  
 
Save the''' Makefile '''
 
Save the''' Makefile '''
| style="border:1pt solid #000000;padding:0.176cm;"| Let us create a '''Makefile''' to '''compile''' a '''driver.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''terminal'''.
 +
 
 +
Let us create a '''Makefile''' to '''compile''' a '''driver.'''
  
 
Type''' gedit space Makefile.'''
 
Type''' gedit space Makefile.'''
Line 276: Line 281:
  
 
| style="border:1pt solid #000000;padding:0.176cm;"| Let us '''compile''' the '''driver.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Let us '''compile''' the '''driver.'''
Type''' sudo su '''and''' password''' to be a superuser.
+
 
 +
Type''' sudo su '''and''' password''' to be a '''superuser'''.
 +
 
 
Type '''make space all.'''
 
Type '''make space all.'''
  
Line 285: Line 292:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us load the '''driver '''into the''' kernel.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us load the '''driver '''into the''' kernel.'''
  
For that, type '''insmod space simple_driver dot ko.'''
+
Type '''insmod space simple_driver dot ko.'''
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen'''.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen.
  
 
|-
 
|-
Line 302: Line 309:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >>''' clear'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >>''' clear'''
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen'''.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen.
  
 
|-
 
|-
Line 312: Line 319:
  
 
Highlight output
 
Highlight output
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us compile the '''user.c file'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us '''compile''' the '''user.c file'''
  
 
Type '''gcc space hyphen c space user dot c'''  
 
Type '''gcc space hyphen c space user dot c'''  
Line 318: Line 325:
 
Then type''' gcc space hyphen o space user space user dot o'''
 
Then type''' gcc space hyphen o space user space user dot o'''
  
Now to execute the program type '''dot slash user.'''
+
Now to '''execute''' the '''program''' type '''dot slash user.'''
  
Here, it indicates that the '''new_device '''opened successfully.
+
The output shows that the '''new_device ''''''open'''ed successfully.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >>''' clear'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >>''' clear'''
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen'''.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Clear the screen.
  
 
|-
 
|-
Line 331: Line 338:
  
 
Type >>''' clear'''
 
Type >>''' clear'''
| style="border:1pt solid #000000;padding:0.176cm;"| Type the command as shown here.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Type the '''command''' as shown here.
  
The output shows that '''open '''and''' close''' '''functions '''of the driver are executed successfully.
+
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.'''
+
It indicates that the '''new_device''' is '''open'''ed and '''close'''d by the '''user program.'''
  
Clear the '''screen.'''
+
Clear the screen.
  
 
|-
 
|-
Line 351: Line 358:
 
Type '''rmmod space simple_driver dot ko'''
 
Type '''rmmod space simple_driver dot ko'''
  
To see the unloaded '''printk '''messages type this '''dmesg '''command.
+
To see the unloaded '''printk '''messages type this '''dmesg command'''.
  
 
It indicates that the''' cdev structure '''of a '''new_device '''removed from the '''kernel. '''
 
It indicates that the''' cdev structure '''of a '''new_device '''removed from the '''kernel. '''
Line 359: Line 366:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| style="border:1pt solid #000000;padding:0.176cm;"| Let us remove the '''object files''' created after the compilation'''.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us remove the '''object files''' created after the compilation.
  
 
Type '''make space clean.'''
 
Type '''make space clean.'''
Line 365: Line 372:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
+
| style="border:1pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial.  
 +
 
 +
Let us summarize.
  
 
|-
 
|-
Line 373: Line 382:
 
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learnt how to
 
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learnt how to
  
* Implement the basic''' file operations '''such as '''open() '''and''' close''' on a device.
+
* Implement the basic''' file operations '''such as '''open '''and''' close''' on a '''device'''.
  
 
|-
 
|-
Line 382: Line 391:
  
 
# Open the '''simple driver.c file.'''
 
# Open the '''simple driver.c file.'''
# Write the different '''printk''' messages in the open and close functions.
+
# Write the different '''printk''' messages in the '''open''' and '''close functions'''.
# Compile and load the''' driver.'''
+
# '''Compile''' and load the''' driver.'''
# Compile and execute the''' user program.'''
+
# '''Compile''' and '''execute''' the''' user program.'''
# Use '''dmesg command '''to check whether the device is opened or not.
+
# Use '''dmesg command '''to check whether the '''device''' is '''open'''ed or not.
# Unload the driver from the''' kernel.'''
+
# Unload the '''driver''' from the''' kernel.'''
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 9 :
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 9 :
  
 
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 408:
  
 
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.
 
+
* conducts workshops and
+
* gives certificates.
+
  
 
For more details, please write to us.
 
For more details, please write to us.
Line 409: Line 416:
  
 
Forum questions:
 
Forum questions:
| style="border:1pt solid #000000;padding:0.176cm;"| * Please post your timed queries in this forum.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Please post your timed queries in this forum.
  
 
|-
 
|-
Line 415: Line 422:
  
 
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.
 
|-
 
|-
Line 429: Line 437:
 
| style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.  
 
| style="border:1pt solid #000000;padding:0.176cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.  
  
This is Mayuri Panchakshari signing off.
+
This is Usha signing off.
  
 
Thanks for watching.
 
Thanks for watching.
  
 
|}
 
|}

Latest revision as of 13:51, 15 October 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
  • Implement the basic file operations such as open() and close() on a device.
Slide 3:

System Requirements:

To record this tutorial, I am using,
  • VirtualBox version 5.2.
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel version 5.0.0-31 generic and
  • gedit text editor
Slide 4 :

Prerequisites:

To follow this tutorial, you should be familiar with:
  • C programming language and
  • Basics of Linux kernel

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:

  • The files used in this tutorial are available in the Code Files link on this tutorial page.
  • Please download and extract them
  • Make a copy and then use them while practising
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 where FileOperations is saved in your system.

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.

I have implemented the basic file functions such as open and close for the device new_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

  • File operations are defined in the kernel as the instance of struct file_operations.
  • 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.
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. We have to declare the prototypes of these functions at the start of the driver.

We 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

cdeve_add function is used 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.

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 the success message 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. Switch back to the terminal.

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

Switch back to the terminal.

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.

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.

The output shows that the new_device 'open'ed 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
  • Implement the basic file operations such as open and close on a device.
Slide 8 :

Assignment :

As an assignment,
  1. Open the simple driver.c file.
  2. Write the different printk messages in the open and close functions.
  3. Compile and load the driver.
  4. Compile and execute the user program.
  5. Use dmesg command to check whether the device is opened or not.
  6. Unload the driver from the kernel.
Slide 9 :

About Spoken Tutorial Project:

  • The video at the following link summarizes the Spoken Tutorial project.
  • Please download and watch it.
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:

  • Do you have any general or technical questions on Embedded Linux Device Driver?
  • Please visit the FOSSEE forum and post your question.
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 Usha signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat