Difference between revisions of "Embedded-Linux-Device-Driver/C3/Creating-a-New-Character-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:''' |-...")
 
Line 25: Line 25:
 
| style="border:1pt solid #000000;padding:0.176cm;"| To record this tutorial, I am using,
 
| style="border:1pt solid #000000;padding:0.176cm;"| To record this tutorial, I am using,
  
* '''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 46: Line 46:
 
'''Class and its device :'''
 
'''Class and its device :'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| * A''' class '''specifies the type of '''device''' that has a similar behavior.
 
| style="border:1pt solid #000000;padding:0.176cm;"| * A''' class '''specifies the type of '''device''' that has a similar behavior.
* For example, audio devices.<br/>
+
* For example, '''audio devices'''.  
  
* '''Classes''' provide a grouping of devices based on functionality.<br/>
+
* '''Classes''' provide a grouping of '''devices''' based on '''functionality'''.
  
* Mostly you can associate a device with a specific '''class'''.<br/>
+
* Mostly you can associate a '''device''' with a specific '''class'''.
  
 
* For example, '''block devices''' such as '''hard drives''' are associated with the '''block class'''.
 
* For example, '''block devices''' such as '''hard drives''' are associated with the '''block class'''.
  
 
|-
 
|-
| 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 '''CreateNewDevice.'''
 
Here I have created a new '''directory '''as '''CreateNewDevice.'''
Line 70: Line 70:
  
 
'''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 83: Line 84:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Go to the directory '''CreateNewDevice''' as shown here.
 
| style="border:1pt solid #000000;padding:0.176cm;"| Go to the directory '''CreateNewDevice''' as shown here.
  
Press '''Enter key''' after every command.
+
Press '''Enter key''' after every '''command'''.
  
 
|-
 
|-
Line 90: Line 91:
 
open '''simple_driver.c'''
 
open '''simple_driver.c'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| Type''' gedit space simple_driver dot c''' to open the driver '''file.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| 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.
 
I have used the same file ''' simple_driver dot c '''which we used earlier.
  
In this file, I have added required''' kernels '''functions.
+
In this file, I have added required''' kernels functions'''.
  
These kernel functions are used to create a new '''character device '''and its''' class.'''
+
These '''kernel functions''' are used to create a new '''character device '''and its''' class.'''
  
 
Let me explain the code.
 
Let me explain the code.
Line 108: Line 109:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static struct class *cl'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''static struct class *cl'''
| style="border:1pt solid #000000;padding:0.176cm;"| First we have to declare a class structure for a new device as shown here.
+
| style="border:1pt solid #000000;padding:0.176cm;"| 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.'''
 
A '''class''' is defined in the '''kernel''' with the '''struct class structure.'''
Line 122: Line 123:
  
 
Highlight '''name'''
 
Highlight '''name'''
| style="border:1pt solid #000000;padding:0.176cm;"| The''' class_create''' function is used to create a '''class''' in the '''sysfs filesystem.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| The''' class_create function''' is used to create a '''class''' in the '''sysfs filesystem.'''
  
This function returns the '''struct class pointer'''.
+
This '''function''' returns the '''struct class pointer'''.
  
'''owner '''specifies the''' pointer''' to the module in which this '''struct class '''is defined.
+
'''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.'''
+
'''name '''specifies the '''pointer '''to a '''string''' for the name of this '''class.'''
  
 
|-
 
|-
Line 135: Line 136:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Here,''' class_create() function''' will create '''my_class''' in the '''sysfs.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Here,''' class_create() function''' will create '''my_class''' in the '''sysfs.'''
  
The '''sysfs''' exports the system information from the '''kernel '''space to user space.
+
The '''sysfs''' exports the system information from the '''kernel space''' to '''user space'''.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' unregister_chrdev_region'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight''' unregister_chrdev_region'''
Line 143: Line 144:
 
| style="border:1pt solid #000000;padding:0.176cm;"| On failure, it will unregister the '''device '''and will exit with the failure message.
 
| style="border:1pt solid #000000;padding:0.176cm;"| 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.
+
Otherwise, '''printk '''will print the message that the '''class''' is created successfully.
  
 
|-
 
|-
Line 153: Line 154:
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''device_create() function''' is used to create the '''device file''' in the''' dev directory.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''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 will also create the '''device file''' in the '''sysfs'''.
  
It returns the''' struct device pointer.'''
+
It '''returns''' the''' struct device pointer.'''
  
 
|-
 
|-
Line 167: Line 168:
  
 
Highlight '''fmt '''
 
Highlight '''fmt '''
| style="border:1pt solid #000000;padding:0.176cm;"| We will see the parameters that are passed to '''device_create() function'''.
+
| style="border:1pt solid #000000;padding:0.176cm;"| We will see the '''parameters''' that are passed to '''device_create() function'''.
  
'''class '''specifies the device class.
+
'''class '''specifies the '''device class'''.
  
'''parent '''specifies the parent device of this device.  
+
'''parent '''specifies the parent '''device''' of this '''device'''.  
  
If it is not specified, it is''' '''referred to as '''NULL.'''
+
If it is not specified, it is referred to as '''NULL.'''
  
'''devt '''specifies''' '''the''' dev_t '''variable which holds the '''device number.'''
+
'''devt '''specifies the''' dev_t variable''' which holds the '''device number.'''
  
'''drvdata '''is a '''pointer''' to the '''driver''' data.
+
'''drvdata '''is a '''pointer''' to the '''driver data'''.
  
'''fmt '''is a string used for the '''device name.'''
+
'''fmt '''is a '''string''' used for the '''device name.'''
  
 
|-
 
|-
Line 187: Line 188:
  
 
Highlight''' NULL'''
 
Highlight''' NULL'''
| style="border:1pt solid #000000;padding:0.176cm;"| Here, I have given a new character '''device''' name as '''new_device'''.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Here, I have given a new '''character device''' name as '''new_device'''.
  
'''cl '''specifies the '''pointer''' to the '''class structure''' of this 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'''.
+
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'''.
 
'''device_num''' holds the major and minor numbers of the '''new_device'''.
Line 216: Line 217:
 
|-
 
|-
 
| 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''' kernel function '''used to remove the '''device '''from the kernel
+
| style="border:1pt solid #000000;padding:0.176cm;"| Now let us see the''' kernel function '''used to remove the '''device '''from the '''kernel'''.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''void device_destroy'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''void device_destroy'''
Line 225: Line 226:
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''device_destroy function '''removes a''' device''' that was created with '''device_create().'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| '''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.  
+
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.
+
'''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.
+
'''devt '''specifies the '''dev_t''' of the '''device''' that was previously '''registered'''.
  
 
|-
 
|-
Line 236: Line 237:
 
Highlight '''printk'''
 
Highlight '''printk'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| In the''' exit function''', we have to return all the resources to the''' system.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| In the''' exit function''', we have to '''return''' all the resources to the''' system.'''
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''class_destroy() '''and '''device_destroy()'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''class_destroy() '''and '''device_destroy()'''
Line 247: Line 248:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''unregister_chrdev_region() '''and '''printk'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''unregister_chrdev_region() '''and '''printk'''
| style="border:1pt solid #000000;padding:0.176cm;"| Finally, we have to unregister the device from the '''kernel''' as shown here.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Finally, we have to '''unregister''' the '''device''' from the '''kernel''' as shown here.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Save the''' file'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Save the''' file'''
Line 276: Line 277:
 
| 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.'''
  
Type''' sudo space su '''and''' password''' to be a superuser.
+
Type''' sudo space su '''and''' password''' to be a '''superuser'''.
  
 
For that, type '''insmod space simple_driver.ko'''
 
For that, type '''insmod space simple_driver.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.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''dmesg | grep simple_driver'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''dmesg | grep simple_driver'''
Line 294: Line 295:
 
Similarly, it shows that the '''device_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.
+
These messages indicate that our '''device''' and its '''class''' is created successfully.
  
 
|-
 
|-
Line 314: Line 315:
 
Here, you can see the new '''my_class''' is registered successfully.
 
Here, you can see the new '''my_class''' is registered successfully.
  
Similarly check whether a device is created in the '''sysfs '''or not.
+
Similarly check whether a '''device''' is created in the '''sysfs '''or not.
  
 
Type '''ls space hyphen l space slash sys slash class slash my class.'''
 
Type '''ls space hyphen l space slash sys slash class slash my class.'''
Line 323: Line 324:
  
 
Highlight the output
 
Highlight the output
| style="border:1pt solid #000000;padding:0.176cm;"| Let us check if the '''device file''' is created under the '''dev''' '''directory '''or not.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us check if the '''device file''' is created under the '''dev directory '''or not.
  
 
Type''' ls space hyphen l space slash dev slash new_device.'''
 
Type''' ls space hyphen l space slash dev slash new_device.'''
Line 329: Line 330:
 
You can see the '''new_device file''' is created.
 
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.'''
+
The '''user''' can access this''' file''' to transfer the '''data''' to the '''new_device''' using its''' driver.'''
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''clear'''
Line 346: Line 347:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight the '''output '''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight the '''output '''
| style="border:1pt solid #000000;padding:0.176cm;"| To see the unloaded '''printk '''messages type the '''dmesg '''command as shown.
+
| style="border:1pt solid #000000;padding:0.176cm;"| To see the unloaded '''printk '''messages type the '''dmesg command''' as shown.
  
The output shows that the '''device file''' and it’s '''class''' are removed from the '''kernel.'''
+
The output shows that the '''device file''' and its '''class''' are removed from the '''kernel.'''
  
It indicates that the '''device''' is also unregistered here.  
+
It indicates that the '''device''' is also '''unregistered''' here.  
  
 
|-
 
|-
Line 360: Line 361:
 
Type '''make clean.'''
 
Type '''make clean.'''
  
To be a regular user , type''' exit.'''
+
To be a regular '''user''' , type''' exit.'''
  
 
|-
 
|-
Line 372: Line 373:
 
| 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
  
* Create a''' class of a device''' in the '''sysfs.'''
+
* Create a''' class''' of a '''device''' in the '''sysfs.'''
 
* Create a''' device file''' in '''sysfs '''and''' dev directory. '''
 
* Create a''' device file''' in '''sysfs '''and''' dev directory. '''
 
|-
 
|-
Line 388: Line 389:
  
 
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 395: Line 397:
  
 
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 406: Line 405:
  
 
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 412: Line 411:
  
 
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.
 
|-
 
|-

Revision as of 12:00, 26 September 2020

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,
  • Create a class of a device in the sysfs.
  • Create a device file in sysfs and dev directory.
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.

Slide 5:

Class and its device :

* A class specifies the type of device that has a similar behavior.
  • For example, audio devices.
  • Classes provide a grouping of devices based on functionality.
  • Mostly you can associate a device with a specific class.
  • For example, block devices such as hard drives are associated with the block class.
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:

  • 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
Open the terminal Open the terminal by pressing Alt+Ctrl+T keys simultaneously.
Type >> cd Desktop/DeviceDriver/CreateNewDevice Go to the directory CreateNewDevice as shown here.

Press Enter key after every command.

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> You have to include this 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 new 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.

Here, class_destroy() will remove the class of device from the sysfs.

Highlight printk messages Depending upon its success or failure, the corresponding messages 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 and password to be a superuser.

For that, 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 ls space hyphen l space slash sys slash class slash pipeline grep my underscore class.

Here, you can see the new my_class is registered successfully.

Similarly check whether a device is created in the sysfs or not.

Type ls space hyphen l space slash sys slash class slash my class.

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 ls space hyphen l space slash dev slash new_device.

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
  • Create a class of a device in the sysfs.
  • Create a device file in sysfs and dev directory.
Slide 7:

Assignment :

As an assignment:
  1. Open the simple_driver.c file.
  2. Create a device with a different name.
  3. Compile and load the simple_driver dot c.
  4. See the newly created device file under the sysfs.
Slide 8:

About Spoken Tutorial Project:

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

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

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat