Embedded-Linux-Device-Driver/C3/File-Operations-on-a-Device/English

From Script | Spoken-Tutorial
Jump to: navigation, search
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