Embedded-Linux-Device-Driver/C3/Creating-a-New-Character-Device/English
Visual cue: | Narration: |
Slide 1:
Welcome slide: |
Welcome to the spoken tutorial on Creating a new character 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. |
Slide 5:
Class and its device : |
|
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 CreateNewDevice. In this directory, I have saved a simple_driver dot c driver file and Makefile. I’ll use these files for demonstration. |
Slide:5
Code files: |
|
Open the terminal | Open the terminal by pressing Alt+Ctrl+T keys simultaneously. |
Type >> cd Desktop/DeviceDriver/CreateNewDevice | Go to the directory where CreateNewDevice is saved on your system.
|
Type >> gedit simple_driver.c
open simple_driver.c |
Type gedit space simple_driver dot c to open the driver file.
I have used the same file simple_driver dot c which we used earlier. In this file, I have added required kernels functions. These kernel functions are used to create a new character device and its class. Let me explain the code. |
Highlight #include<device.h> | We have to include this device.h header file.
The kernel functions that are required to create a new device are declared in it. |
Highlight static struct class *cl | First we have to declare a class structure for a new device as shown here.
A class is defined in the kernel with the struct class structure. |
Let us see how to create a class using the kernels function in the sysfs. | |
Highlight struct class *class_create()
Highlight owner Highlight name |
The class_create function is used to create a class in the sysfs filesystem.
This function returns the struct class pointer. owner specifies the pointer to the module in which this struct class is defined. name specifies the pointer to a string for the name of this class. |
Highlight class_create() | Here, class_create() function will create my_class in the sysfs.
The sysfs exports the system information from the kernel space to user space. |
Highlight unregister_chrdev_region
Highlight printk |
On failure, it will unregister the device and will exit with the failure message.
Otherwise, printk will print the message that the class is created successfully. |
Let us see the kernel function used to create a device file. | |
Highlight struct device *device_create() | device_create() function is used to create the device file in the dev directory.
It will also create the device file in the sysfs. It returns the struct device pointer. |
Highlight class
Highlight parent Highlight devt Highlight drvdata Highlight fmt |
We will see the parameters that are passed to device_create() function.
class specifies the device class. parent specifies the parent device of this device. If it is not specified, it is referred to as NULL. devt specifies the dev_t variable which holds the device number. drvdata is a pointer to the driver data. fmt is a string used for the device name. |
Highlight device_create()
Highlight new_device Highlight NULL |
Here, I have given a new character device name as new_device.
cl specifies the pointer to the class structure of this new device. As the device does not have a parent device, I have passed the NULL pointer. device_num holds the major and minor numbers of the new_device. |
Let us see the kernel function used to remove the class of device to free its memory. | |
Highlight void class_destroy(struct class *class)
Highlight cl |
class_destroy function destroys a struct class structure.
class specifies the pointer to the struct class that has to be destroyed. |
Highlight class_destroy(cl), unregister_chrdev_region()
Highlight class_destroy(cl) |
These functions are executed when the device file creation fails.
class_destroy() will remove the class of device from the sysfs. |
Highlight printk messages | Depending upon its success or failure, the corresponding message will be printed. |
Now let us see the kernel function used to remove the device from the kernel. | |
Highlight void device_destroy
Highlight class Highlight devt |
device_destroy function removes a device that was created with device_create().
This function is used in the exit function to free the kernel's memory. Class is a pointer to the struct class that this device was registered with. devt specifies the dev_t of the device that was previously registered. |
Highlight exit_function()
Highlight printk |
In the exit function, we have to return all the resources to the system. |
Highlight class_destroy() and device_destroy()
Highlight printk messages. |
Here, the class and device will be removed by their corresponding functions.
Printk message indicates the successful removal of the class and device. |
Highlight unregister_chrdev_region() and printk | Finally, we have to unregister the device from the kernel as shown here. |
Save the file | Save and close the file. |
Type >> gedit Makefile
Save the Makefile |
Let us create a Makefile to compile the driver.
Type gedit space Makefile. Type the code as shown or you can make use of the downloaded Makefile. |
Type >> make all | Let us compile the driver.
Type make space all. Clear the screen. |
Type >> insmod simple_driver.ko
Type sudo su |
Now let us load the driver into the kernel.
Type sudo space su . Type the password to be a superuser. Type insmod space simple_driver.ko |
Type >> clear | Clear the screen. |
Type >> dmesg | grep simple_driver
Highlight respective printk messages |
Let us see the loaded printk messages from the initialisation function.
Type dmesg space pipe space grep space simple_driver. It shows that the class_create() function is executed successfully. Similarly, it shows that the device_create() function is executed successfully. These messages indicate that our device and its class is created successfully. |
Type >> clear | Clear the screen. |
Type >> ls -l /sys/class/ | grep my_class
Highlight the output Type >> ls -l /sys/class/my_class/ Highlight the output |
Now let’s check whether the class is created in the sysfs or not.
Type the command as shown. Here, you can see the new my_class is registered successfully. Similarly check whether a device is created in the sysfs or not. Type the command as shown. This shows that the new_device is created in the sysfs. |
Type ls -l /dev/new_device
Highlight the output |
Let us check if the device file is created under the dev directory or not.
Type the command as shown. You can see the new_device file is created. The user can access this file to transfer the data to the new_device using its driver. |
Type >> clear | Clear the screen. |
Type rmmod simple_driver .ko
Type dmesg | grep simple_driver Type >> clear |
Now let us unload the driver.
Type rmmod space simple_driver dot ko Clear the screen. |
Highlight the output | To see the unloaded printk messages type the dmesg command as shown.
The output shows that the device file and its class are removed from the kernel. It indicates that the device is also unregistered here. |
Type >> make clean
Type >> exit |
Let us remove the object files created after the compilation.
Type make clean. To be a regular user , type exit. |
With this, we come to the end of this tutorial. Let us summarize. | |
Slide 6:
Summary: |
In this tutorial, we learnt how to
|
Slide 7:
Assignment : |
As an assignment:
|
Slide 8:
About Spoken Tutorial Project: |
|
Slide 9:
Spoken Tutorial workshops: |
The Spoken Tutorial Project Team conducts workshops and gives certificates.
For more details, please write to us. |
Slide 10:
Forum questions: |
Please post your timed queries in this forum. |
Slide 11:
Forum for specific questions: |
|
Slide 12:
Acknowledgment: |
The Spoken Tutorial Project is funded by MHRD, Government of India. |
Slide 13:
Thank you slide: |
This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
This is Usha signing off. Thanks for watching. |