Embedded-Linux-Device-Driver/C3/Registering-a-Character-Device-Number/English
Visual cue : | Narration : |
Slide 1:
Welcome slide: |
Welcome to the spoken tutorial on Registering a Character Device Number. |
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:
Device file in Linux. |
|
Let us see some of the device files that are already present in the Linux system. | |
Open the terminal | Open the terminal by pressing Alt+Ctrl+T keys simultaneously.
Press Enter key after every command. |
Type >> ls -l /dev/tty* | head -5
Show the output |
Let us see about the specific device files such as serial character device files.
Type the command as shown here. |
Highlight numbers 4, 1
Highlight 4 |
Here, 4 and 1 indicate the major and minor numbers of tty1 device.
The major number indicates the driver associated with the device. It indicates that the tty0 and tty1 serial ports are managed by the driver 4. |
Highlight 0, 1 | Inside the driver, each device is identified by the unique number.
The tty0 and tty1 have different minor numbers 0 and 1 as shown here. The driver uses the minor number to distinguish individual physical or logical devices. Using these minor numbers, the driver 4 is able to differentiate these devices. |
Slide 6 :
Internal representation of device number |
|
Point to the folder and file in desktop
Point to the files. |
Go to the DeviceDriver folder in the Desktop which we have created earlier.
I have created a directory named RegisterDevice. In this directory, I have saved simple_driver dot c and Makefile. I will use these files for demonstration. |
Slide :
Code files: |
|
Type >> cd Desktop/DeviceDriver/RegisterDevice | Switch back to the terminal.
Go to the directory where RegisterDevice is saved in your system. |
Type >> gedit simple_driver.c | Let us open the file simple_driver dot c.
Type gedit space simple_driver dot c. Let us see the code. |
Highlight header files
Highlight fs.h |
We have to include these header files at the start of the program.
The purpose of each header file is mentioned here. fs.h contains the required functions to register a character device number. |
Highlight device_num | device_num is a dev_t type variable.
It will hold the major and minor number of a device. |
Let us see how to register a character device number dynamically. | |
Highlight alloc_chrdev_region() | alloc_chrdev_region function will dynamically allocate the major number to the device.
The kernel will find the unused major number and assign it to a device. This function returns a value less than zero on failure. |
Highlight dev
Highlight firstminor Highlight count Highlight name |
Let us see the parameters of this function.
dev is the variable that holds the device number. firstminor specifies the starting minor number which is 0. count specifies the number of supported devices. name is a device name used to identify the device. |
Highlight 0
Highlight alloc_chrdev_region() my_device. |
In this program, we allocate a minor number starting from 0.
This function will register one device with the name my_device. |
Highlight printk messages | printk function will be executed to indicate the success or failure of the function. |
Highlight MAJOR(dev_t dev)
Highlight MINOR(dev_t dev) Highlight macros |
MAJOR macro is used to extract the major number from the dev_t variable.
Similarly, MINOR macro is used to extract the minor number from the dev_t variable. It will print the major and minor numbers of my_device using these macros. |
Highlight Unregister_chrdev_region() | unregister_chrdev_region() function is used to remove a device number from kernel.
You should free the device numbers when they are no longer in use. |
Highlight dev_t
Highlight count |
dev_t specifies the beginning of device number range to be freed.
count represents the number of devices to unregister. |
Highlight unregister_chrdev_region()
Highlight printk() |
This function will unregister my_device from the kernel.
When the driver is unloaded, these messages will be printed in the kernel log level. Save the file and close the window. |
Show the terminal | Switch back to the terminal |
Type << gedit Makefile
Type >> clear |
To compile the driver, we need to create a Makefile.
Type gedit space Makefile. Type the code as shown or you can make use of the downloaded Makefile. Save and close the file. |
Open the terminal
Type >> sudo su |
Let us compile the driver.
Type sudo space su to be a superuser and type the system password. |
Type >> make all
Type >> clear |
Type make space all
Clear the screen. |
Type >> insmod space simple_driver.ko
Type >> clear |
Let us load the simple_driver dot ko into the kernel.
Type insmod space simple_driver dot ko. Clear the screen. |
Let us see the loaded printk messages of the driver. | |
Type >> dmesg | grep simple_driver
Highlight major and minor number |
Type dmesg space pipe space grep simple_driver.
We can see the major and minor number of my_device It indicates that the my_device is registered inside the kernel successfully. |
Type >> clear | Clear the screen |
Type >> cat /proc/devices | grep my_device
Highlight devices |
Let us check whether our device appears in the proc file system or not.
Type the command as shown here. This file displays the various devices currently configured in the kernel. |
Highlight my_device with number | Here, you can see the my_device with its major number.
It indicates that my_device is registered inside the kernel successfully. |
Type >> clear | Clear the screen. |
Type >> rmmod simple_driver.ko | Let us unload a module from the kernel.
Type rmmod space simple_driver dot ko |
Type >> dmesg | grep simple_driver
Highlight printk Type >> clear |
Let us see the unloaded printk messages of the driver.
Type the command as shown here. It indicates that the my_device is unregistered from the kernel successfully. Clear the screen. |
Type >> make clean
Type >> clear |
Type make space clean.
It removes all objects files created after the compilation. Clear the screen. |
Type >> exit | To be a regular user, type exit. |
With this, we come to the end of this tutorial. Let us summarize. | |
Slide :
Summary |
In this tutorial, we learned how to
|
Slide :
Assignment : |
As an assignment
|
Slide :
About Spoken Tutorial Project: |
|
Slide :
Spoken Tutorial workshops: |
The Spoken Tutorial Project Team conducts workshops and gives certificates.
For more details, please write to us. |
Slide :
Forum questions : |
Please post your timed queries in this forum |
Slide :
Forum for specific questions : |
|
Slide :
Acknowledgment: |
The Spoken Tutorial project is funded by MHRD, Government of India. |
Slide :
Thank you slide: |
This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.
This is Usha from IIT Bombay signing off. Thanks for watching. |