Difference between revisions of "Embedded-Linux-Device-Driver/C3/Registering-a-Character-Device-Number/English"
(Created page with " {| style="border-spacing:0;" | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''Visual cue :''' | style="bo...") |
Nancyvarkey (Talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
Line 45: | Line 45: | ||
'''Device file''' in '''Linux'''. | '''Device file''' in '''Linux'''. | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| * Each '''device''' is represented as a '''file''' in '''Linux'''. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| |
+ | * Each '''device''' is represented as a '''file''' in '''Linux'''. | ||
* The '''device files''' are located under the '''dev directory.''' | * The '''device files''' are located under the '''dev directory.''' | ||
* Each '''device file''' in''' Linux''' has a unique number associated with it. | * Each '''device file''' in''' Linux''' has a unique number associated with it. | ||
Line 51: | Line 52: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see some of the | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see some of the '''device files''' that are already present in the '''Linux system'''. |
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Open the''' terminal''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Open the''' terminal''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Open the''' ''' | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Open the '''terminal''' by pressing''' Alt+Ctrl+T''' keys simultaneously. |
− | Press | + | Press '''Enter''' key after every '''command'''. |
|- | |- | ||
Line 63: | Line 64: | ||
Show the '''output''' | Show the '''output''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see about the specific device files such as '''serial character device files. ''' | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see about the specific '''device files''' such as '''serial character device files. ''' |
− | Type the command as shown here. | + | Type the '''command''' as shown here. |
|- | |- | ||
Line 73: | Line 74: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Here, 4 and 1 indicate the '''major''' and '''minor''' numbers of '''tty1 device'''. | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Here, 4 and 1 indicate the '''major''' and '''minor''' numbers of '''tty1 device'''. | ||
− | The '''major''' number indicates the driver associated with the '''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. | It indicates that the''' tty0 '''and '''tty1 serial ports '''are managed by the '''driver''' 4. | ||
Line 83: | Line 84: | ||
The''' tty0 '''and '''tty1 '''have different '''minor''' numbers 0 and 1 as shown here. | 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 | + | 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'''. | Using these '''minor''' numbers, the '''driver 4 '''is able to differentiate these''' devices'''. | ||
Line 91: | Line 92: | ||
Internal representation of device number | Internal representation of device number | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| * The '''kernel''' uses the '''dev_t type variable '''to hold '''major''' and '''minor''' numbers. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| |
+ | * The '''kernel''' uses the '''dev_t type variable '''to hold '''major''' and '''minor''' numbers. | ||
* The size of the '''dev_t''' is''' 32-bit.''' | * The size of the '''dev_t''' is''' 32-bit.''' | ||
* '''12 bits''' are used for '''major''' numbers and '''20 bits''' are used for '''minor''' numbers. | * '''12 bits''' are used for '''major''' numbers and '''20 bits''' are used for '''minor''' numbers. | ||
Line 99: | Line 101: | ||
Point to the '''files.''' | Point to the '''files.''' | ||
− | | style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Go to the '''DeviceDriver '''folder in the | + | | style="border-top:1pt solid #000000;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Go to the '''DeviceDriver '''folder in the '''Desktop''' which we have created earlier. |
− | I have created a directory | + | I have created a directory named''' RegisterDevice.''' |
In this directory, I have saved '''simple_driver dot c''' and '''Makefile'''. | In this directory, I have saved '''simple_driver dot c''' and '''Makefile'''. | ||
− | I will use these | + | I will use these files for demonstration. |
|- | |- | ||
Line 111: | Line 113: | ||
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 | ||
|- | |- | ||
| style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >> '''cd Desktop/DeviceDriver/RegisterDevice''' | | style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >> '''cd Desktop/DeviceDriver/RegisterDevice''' | ||
− | | style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Switch back to the | + | | style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Switch back to the '''terminal'''. |
− | Go to the directory''' RegisterDevice ''' | + | Go to the directory where''' RegisterDevice ''' is saved in your '''system'''. |
|- | |- | ||
Line 136: | Line 139: | ||
The purpose of each '''header file''' is mentioned here. | The purpose of each '''header file''' is mentioned here. | ||
− | '''fs.h''' contains the required functions to register a '''character device number.''' | + | '''fs.h''' contains the required '''functions''' to register a '''character device number.''' |
|- | |- | ||
| style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight''' device_num''' | | style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight''' device_num''' | ||
− | | style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''device_num '''is a '''dev_t type ''' | + | | style="border:0.5pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''device_num '''is a '''dev_t type variable'''. |
It will hold the '''major''' and '''minor''' number of a '''device'''. | It will hold the '''major''' and '''minor''' number of a '''device'''. | ||
Line 146: | Line 149: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see how to register a''' character device number dynamically.''' | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see how to '''register''' a''' character device number dynamically.''' |
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight '''alloc_chrdev_region()''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight '''alloc_chrdev_region()''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''alloc_chrdev_region | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''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'''. | The''' kernel '''will find the unused '''major''' number and assign it to a '''device'''. | ||
− | This function returns a value less than zero on failure. | + | This '''function''' returns a value less than zero on failure. |
|- | |- | ||
Line 165: | Line 168: | ||
Highlight '''name''' | Highlight '''name''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see the parameters of this '''function'''. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see the '''parameters''' of this '''function'''. |
− | '''dev '''is the variable that holds the '''device '''number. | + | '''dev '''is the '''variable''' that holds the '''device '''number. |
'''firstminor '''specifies the starting '''minor''' number which is '''0.''' | '''firstminor '''specifies the starting '''minor''' number which is '''0.''' | ||
Line 180: | Line 183: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| In this program, we allocate a '''minor''' number starting from '''0'''. | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| In this program, we allocate a '''minor''' number starting from '''0'''. | ||
− | This '''function''' will register one device with the name '''my_device.''' | + | This '''function''' will '''register''' one '''device''' with the name '''my_device.''' |
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight''' printk messages''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight''' printk messages''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''printk function '''will be executed to indicate the success or failure of the''' function | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''printk function '''will be executed to indicate the success or failure of the '''function'''. |
|- | |- | ||
Line 193: | Line 196: | ||
Highlight '''macros''' | Highlight '''macros''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''MAJOR | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''MAJOR macro''' is used to extract the '''major''' number from the '''dev_t variable.''' |
− | Similarly, '''MINOR | + | 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'''. | It will print the '''major''' and '''minor''' numbers of '''my_device '''using these '''macros'''. | ||
Line 201: | Line 204: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight '''Unregister_chrdev_region()''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight '''Unregister_chrdev_region()''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''unregister_chrdev_region() ''' | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''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. | You should free the''' device numbers '''when they are no longer in use. | ||
Line 209: | Line 212: | ||
Highlight '''count''' | Highlight '''count''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''dev_t '''specifies the''' ''' | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| '''dev_t '''specifies the beginning of '''device number''' range to be freed. |
− | '''count '''represents the number of devices to unregister. | + | '''count '''represents the number of '''devices''' to '''unregister'''. |
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight '''unregister_chrdev_region()''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Highlight '''unregister_chrdev_region()''' | ||
Highlight '''printk()''' | Highlight '''printk()''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| This function will | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| This '''function''' will '''unregister my_device '''from the''' kernel'''. |
− | When the '''driver '''is unloaded, these messages will be printed in the '''kernel | + | When the '''driver '''is unloaded, these messages will be printed in the '''kernel log level. ''' |
Save the file and close the window. | Save the file and close the window. | ||
Line 229: | Line 232: | ||
Type >> '''clear''' | Type >> '''clear''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| To compile the '''driver''', we need to create a '''Makefile'''. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| To '''compile''' the '''driver''', we need to create a '''Makefile'''. |
Type '''gedit''' space '''Makefile'''. | Type '''gedit''' space '''Makefile'''. | ||
Line 235: | Line 238: | ||
Type the''' code '''as shown or you can make use of the downloaded''' Makefile'''. | Type the''' code '''as shown or you can make use of the downloaded''' Makefile'''. | ||
− | Save and close the file | + | Save and close the file. |
|- | |- | ||
Line 241: | Line 244: | ||
Type >> '''sudo su''' | Type >> '''sudo su''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us compile the '''driver'''. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us '''compile''' the '''driver'''. |
− | Type '''sudo | + | Type '''sudo space su '''to be a '''superuser''' and type the '''system password'''. |
|- | |- | ||
Line 275: | Line 278: | ||
We can see the '''major''' and '''minor''' number of '''my_device ''' | We can see the '''major''' and '''minor''' number of '''my_device ''' | ||
− | It indicates that the '''my_device''' is registered inside the '''kernel '''successfully. | + | It indicates that the '''my_device''' is '''registered''' inside the '''kernel '''successfully. |
|- | |- | ||
Line 287: | Line 290: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us check whether our '''device''' appears in the''' proc file''' system or not. | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us check whether our '''device''' appears in the''' proc file''' system or not. | ||
− | Type''' ''' | + | Type the '''command''' as shown here. |
This file displays the various '''devices '''currently configured in the '''kernel'''. | This file displays the various '''devices '''currently configured in the '''kernel'''. | ||
Line 294: | Line 297: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Here, you can see the '''my_device''' with its '''major''' number. | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Here, you can see the '''my_device''' with its '''major''' number. | ||
− | It indicates that '''my_device '''is registered inside the '''kernel''' successfully. | + | It indicates that '''my_device '''is '''registered''' inside the '''kernel''' successfully. |
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >>''' clear''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >>''' clear''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Clear the screen | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Clear the screen. |
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >> '''rmmod simple_driver.ko ''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >> '''rmmod simple_driver.ko ''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us unload a''' module''' from the kernel. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us unload a''' module''' from the '''kernel'''. |
Type''' rmmod space simple_driver dot ko''' | Type''' rmmod space simple_driver dot ko''' | ||
Line 315: | Line 318: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see the unloaded''' printk messages''' of the '''driver'''. | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Let us see the unloaded''' printk messages''' of the '''driver'''. | ||
− | Type the command as shown here. | + | Type the '''command''' as shown here. |
− | It indicates that the '''my_device''' is unregistered from the '''kernel '''successfully. | + | It indicates that the '''my_device''' is '''unregistered''' from the '''kernel '''successfully. |
Clear the screen. | Clear the screen. | ||
Line 327: | Line 330: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type '''make space clean.''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type '''make space clean.''' | ||
− | It removes all '''objects | + | It removes all '''objects files '''created after the '''compilation.''' |
Clear the screen. | Clear the screen. | ||
Line 333: | Line 336: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >> '''exit''' | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| Type >> '''exit''' | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| To be''' | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| To be a regular '''user''', type '''exit.''' |
|- | |- | ||
Line 345: | Line 348: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| In this tutorial, we learned how to | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| In this tutorial, we learned how to | ||
− | * Dynamically allocate the '''major''' and '''minor '''numbers to a | + | * Dynamically allocate the '''major''' and '''minor '''numbers to a '''device'''. |
− | * Register a new character device inside the kernel. | + | * Register a new '''character device''' inside the '''kernel'''. |
|- | |- | ||
Line 355: | Line 358: | ||
# Open the''' simple_driver.c '''file. | # Open the''' simple_driver.c '''file. | ||
− | # Allocate the device number using the '''kernel function.''' | + | # Allocate the '''device number''' using the '''kernel function.''' |
# Change the name of the '''device'''. | # Change the name of the '''device'''. | ||
− | # Compile and load the '''driver''' into the '''kernel'''. | + | # '''Compile''' and load the '''driver''' into the '''kernel'''. |
− | # See the registered device in the '''procfs''' | + | # See the '''registered device''' in the '''procfs''' |
− | # Unload the driver from the''' kernel'''. | + | # Unload the '''driver''' from the''' kernel'''. |
|- | |- | ||
Line 365: | Line 368: | ||
About Spoken Tutorial Project: | About Spoken Tutorial Project: | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| * The video at the following link summarizes the Spoken Tutorial project. | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| |
+ | * The video at the following link summarizes the Spoken Tutorial project. | ||
* Please download and watch it. | * Please download and watch it. | ||
Line 372: | Line 376: | ||
Spoken Tutorial workshops: | Spoken Tutorial workshops: | ||
− | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| The''' Spoken Tutorial Project''' Team | + | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| The''' Spoken Tutorial Project''' Team conducts workshops and gives certificates. |
− | + | For more details, please write to us. | |
− | + | ||
|- | |- | ||
Line 381: | Line 384: | ||
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 387: | Line 390: | ||
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 400: | Line 404: | ||
| style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay. | | style="border:1pt solid #000000;padding-top:0cm;padding-bottom:0cm;padding-left:0.206cm;padding-right:0.191cm;"| This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay. | ||
− | This is | + | This is Usha from IIT Bombay signing off. |
Thanks for watching. | Thanks for watching. | ||
|} | |} |
Latest revision as of 15:32, 30 October 2020
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. |