Difference between revisions of "Embedded-Linux-Device-Driver/C2/Passing-Parameters-to-a-Module/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'''. |-...")
 
 
(One intermediate revision by the same user not shown)
Line 14: Line 14:
  
 
Learning objectives:
 
Learning objectives:
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we will learn how to
+
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we will learn how to pass '''parameters''' to a '''module'''.
 
+
* Pass parameters to a '''module'''.
+
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 3:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 3:
Line 43: Line 41:
  
 
Point to the '''files.'''
 
Point to the '''files.'''
| style="border:1pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver '''folder in the desktop which we have created earlier.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Go to the '''DeviceDriver '''folder in the '''Desktop''' which we have created earlier.
  
Here, I have created a '''directory named Parameter.'''
+
Here, I have created a '''directory''' named '''Parameter.'''
  
 
In this '''directory''', I have saved a '''module file '''for demonstration purposes.
 
In this '''directory''', I have saved a '''module file '''for demonstration purposes.
Line 55: Line 53:
  
 
Code files
 
Code files
| style="border:0.5pt solid #000000;padding:0.176cm;"| * The '''files''' used in this tutorial are available in the '''Code Files '''link on this tutorial page.
+
| style="border:0.5pt 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 64: Line 63:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal''' by pressing '''ALT+Ctrl+T '''keys simultaneously.
 
| style="border:1pt solid #000000;padding:0.176cm;"| Open the '''terminal''' by pressing '''ALT+Ctrl+T '''keys simultaneously.
  
Press''' Enter''' after every command.
+
Press''' Enter''' after every '''command'''.
  
Go to the directory''' Parameter '''as shown here.
+
Go to the '''directory Parameter '''as shown here.
  
 
|-
 
|-
Line 88: Line 87:
 
| style="border:1pt solid #000000;padding:0.176cm;"| We have to include these''' header files''' as it is needed by all '''modules.'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| We have to include these''' header files''' as it is needed by all '''modules.'''
  
'''moduleparam.h file '''contains the macros required to pass parameters to a module.
+
'''moduleparam.h file '''contains the '''macros''' required to pass '''parameters''' to a '''module'''.
  
Here variables are declared in a '''module''' as '''global variables.'''
+
Here '''variables''' are declared in a '''module''' as '''global variables.'''
  
 
|-
 
|-
Line 100: Line 99:
  
 
'''// type- bool, int, long, short, uint, ulong, ushort, char*'''
 
'''// type- bool, int, long, short, uint, ulong, ushort, char*'''
| style="border:1pt solid #000000;padding:0.176cm;"| '''module_param() macro''' is used to pass parameters to a module.
+
| style="border:1pt solid #000000;padding:0.176cm;"| '''module_param() macro''' is used to pass '''parameters''' to a '''module'''.
  
The first''' '''parameter specifies the '''name''' of a parameter.
+
The first '''parameter''' specifies the name of the '''parameter'''.
  
The '''second''' parameter specifies the supported data types of variable.
+
Second''' parameter''' specifies the supported '''data types''' of '''variable'''.
  
 
For example, '''bool, int, long, short, uint, ulong, ushort '''are supported.
 
For example, '''bool, int, long, short, uint, ulong, ushort '''are supported.
  
A '''char''' '''pointer''' value is also supported.
+
A '''char pointer''' value is also supported.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''perm'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''perm'''
Line 114: Line 113:
 
Highlight''' S_I'''
 
Highlight''' S_I'''
  
| style="border:1pt solid #000000;padding:0.176cm;"| The''' third '''parameter specifies the '''permissions''' of this '''module file''' in''' sysfs. '''
+
| style="border:1pt solid #000000;padding:0.176cm;"| The third '''parameter''' specifies the '''permissions''' of this '''module file''' in''' sysfs. '''
  
 
The '''permissions''' can be specified in '''S_I''' format.  
 
The '''permissions''' can be specified in '''S_I''' format.  
Line 123: Line 122:
 
'''File''' permissions in '''S_I''' format
 
'''File''' permissions in '''S_I''' format
  
| style="border:1pt solid #000000;padding:0.176cm;"| Let us see the file permissions in the '''module param() '''macro using''' S_I defines.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us see the '''file permissions''' in the '''module param() macro''' using''' S_I defines.'''
  
We can use these formats in our '''code''' as per the requirement.
+
We can use these formats in our code as per the requirement.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''code'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''code'''
| style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the '''code.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Switch back to the code.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''value'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Highlight '''value'''
Line 136: Line 135:
  
 
Highlight''' S_IRUSR | S_IWUSR'''
 
Highlight''' S_IRUSR | S_IWUSR'''
| style="border:1pt solid #000000;padding:0.176cm;"| Here, I have passed the n'''value''' parameter of''' integer''' data type to a '''module.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Here, I have passed the '''nvalue parameter''' of''' integer data type''' to a '''module.'''
  
The''' '''next '''parameter''' indicates that the user has the read and write''' permissions.'''
+
The next '''parameter''' indicates that the user has the '''read''' and '''write permissions.'''
  
 
|-
 
|-
Line 153: Line 152:
 
The''' array underscore value''' specifies the name of an '''array'''.
 
The''' array underscore value''' specifies the name of an '''array'''.
  
Here, the data type of '''array '''elements is '''int.'''
+
Here, the '''data type''' of '''array elements''' is '''int.'''
  
The third parameter is a''' pointer.'''
+
The third '''parameter''' is a''' pointer.'''
  
In this parameter, the '''kernel''' stores the number of''' array elements.'''
+
In this '''parameter''', the '''kernel''' stores the number of''' array elements.'''
  
Here, we are passing a '''NULL''' pointer.  
+
Here, we are passing a '''NULL pointer'''.  
  
The''' '''next parameter indicates that '''user''' has the read and write permissions.
+
The next '''parameter''' indicates that user has the '''read''' and '''write permissions.'''
  
 
|-
 
|-
Line 173: Line 172:
  
 
Highlight '''S_IRUSR | S_IWUSR'''
 
Highlight '''S_IRUSR | S_IWUSR'''
| style="border:1pt solid #000000;padding:0.176cm;"| '''module_param_named''' '''macro''' is used to give different names to the same '''variable.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| '''module_param_named macro''' is used to give different names to the same '''variable.'''
  
Here, '''name''' is the externally viewable parameter.
+
Here, '''name''' is the externally viewable '''parameter'''.
  
The''' value '''is the name of the internal global variable.
+
The''' value '''is the name of the internal '''global variable'''.
  
The data type of this '''variable''' is an '''integer'''.
+
The '''data type''' of this '''variable''' is an '''integer'''.
  
The''' user''' can read and write to this''' file.'''
+
The user can '''read''' and '''write''' to this''' file.'''
  
 
|-
 
|-
Line 193: Line 192:
 
| style="border:1pt solid #000000;padding:0.176cm;"| This is an '''initialization function''' of a '''module '''containing the '''printk''' messages.
 
| style="border:1pt solid #000000;padding:0.176cm;"| This is an '''initialization function''' of a '''module '''containing the '''printk''' messages.
  
The '''printk''' messages will print the different parameters of the above macro.
+
The '''printk''' messages will print the different '''parameters''' of the above '''macro'''.
  
 
The''' exit function '''is the '''cleanup function''' of the '''module'''.
 
The''' exit function '''is the '''cleanup function''' of the '''module'''.
  
This function contains a printk message as shown here.
+
This '''function''' contains a '''printk''' message as shown here.
  
 
|-
 
|-
Line 205: Line 204:
 
Switch back to the''' terminal.'''
 
Switch back to the''' terminal.'''
  
'''Clear the screen.'''
+
Clear the screen.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''gedit Makefile'''
Line 216: Line 215:
  
 
Open the''' terminal.'''
 
Open the''' terminal.'''
| style="border:1pt solid #000000;padding:0.176cm;"| Let us create a '''Makefile '''to compile the '''module. '''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us create a '''Makefile '''to '''compile''' the '''module. '''
  
 
Type '''gedit space Makefile.'''  
 
Type '''gedit space Makefile.'''  
  
Write the''' code '''as shown here.
+
Write the code as shown here.
  
Save this '''file '''and '''close '''it.
+
 
 +
Save this '''file '''and close it.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >>''' make all'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >>''' make all'''
Line 229: Line 229:
  
 
Type >>''' clear.'''
 
Type >>''' clear.'''
| style="border:1pt solid #000000;padding:0.176cm;"| To build the '''module, '''type '''make space all.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| To '''build''' the '''module, '''type '''make space all.'''
 
+
Type '''sudo''' '''space''' '''su '''to be a''' superuser '''and
+
  
type the system password.
+
Type '''sudo space su '''to be a''' superuser''' and type the '''system password'''.
  
Clear the screen'''.'''
+
Clear the screen.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''insmod mod_param.ko value=4 name=9 array_value=11,12,13,14'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''insmod mod_param.ko value=4 name=9 array_value=11,12,13,14'''
Line 242: Line 240:
  
 
Highlight 11,12,13,14.
 
Highlight 11,12,13,14.
| style="border:1pt solid #000000;padding:0.176cm;"| Let us insert '''mod_param'''.'''ko''' into the '''kernel'''.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us insert '''mod_param.ko''' into the '''kernel'''.
  
 
Type the''' command '''as shown.
 
Type the''' command '''as shown.
  
Here, we pass a value 4 to the variable''' nvalue '''and''' '''9 to the variable '''name'''.
+
Here, we pass a value 4 to the '''variable nvalue '''and 9 to the '''variable name'''.
  
11,12,13,14 are passed as an '''array''' element to '''array underscore value. '''
+
11,12,13,14 are passed as an '''array element''' to '''array underscore value. '''
  
 
|-
 
|-
Line 256: Line 254:
  
 
Type >> '''clear'''
 
Type >> '''clear'''
| style="border:1pt solid #000000;padding:0.176cm;"| Let us see the loaded''' printk''' messages of a module.
+
| style="border:1pt solid #000000;padding:0.176cm;"| Let us see the loaded''' printk''' messages of a '''module'''.
  
 
Type''' dmesg space pipe space grep space my underscore module '''
 
Type''' dmesg space pipe space grep space my underscore module '''
  
These printk messages which display the parameter passes to a module.
+
These '''printk''' messages which display the '''parameter passes''' to a '''module'''.
  
Clear the '''screen.'''
+
Clear the screen.
  
 
|-
 
|-
Line 281: Line 279:
 
Type '''dmesg space pipe space grep space my underscore module '''
 
Type '''dmesg space pipe space grep space my underscore module '''
  
The output shows the printk messages from the exit function.
+
The output shows the '''printk''' messages from the '''exit function.'''
  
'''Clear the screen'''
+
Clear the screen.
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''make clean'''
| style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean.'''
+
| style="border:1pt solid #000000;padding:0.176cm;"| Type '''make space clean'''
  
 
It removes all '''files '''created after '''compilation.'''
 
It removes all '''files '''created after '''compilation.'''
Line 293: Line 291:
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''exit'''
 
| style="border:1pt solid #000000;padding:0.176cm;"| Type >> '''exit'''
| style="border:1pt solid #000000;padding:0.176cm;"| To be''' '''the''' regular user, '''type '''exit.'''  
+
| style="border:1pt solid #000000;padding:0.176cm;"| To be the''' regular user, '''type '''exit.'''  
  
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
 
| style="border:1pt solid #000000;padding:0.176cm;"|  
| style="border:1pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial. Let us summarize.
+
| style="border:1pt solid #000000;padding:0.176cm;"| With this, we come to the end of this tutorial.  
 +
 
 +
Let us summarize.
  
 
|-
 
|-
Line 303: Line 303:
  
 
summary:
 
summary:
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learnt to,
+
| style="border:1pt solid #000000;padding:0.176cm;"| In this tutorial, we learnt to pass '''parameters''' to a '''module'''.
* Pass parameters to a '''module'''.
+
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 6:
Line 311: Line 310:
 
| style="border:1pt solid #000000;padding:0.176cm;"| As an assignment:
 
| style="border:1pt solid #000000;padding:0.176cm;"| As an assignment:
  
# Compile the '''mod_param.c. '''
+
# '''Compile''' the '''mod_param.c. '''
# Load the '''module''' with change in parameters value using the '''insmod tool.'''
+
# Load the '''module''' with change in '''parameters value''' using the '''insmod tool.'''
 
# See the output using '''dmesg command. '''
 
# See the output using '''dmesg command. '''
# Unload the module using '''rmmod tool.'''
+
# Unload the '''module''' using '''rmmod tool.'''
  
 
|-
 
|-
Line 320: Line 319:
  
 
About Spoken Tutorial Project:
 
About Spoken Tutorial Project:
| style="border:0.5pt solid #000000;padding:0.176cm;"| * The video at the following link summarizes the Spoken Tutorial project.
+
| style="border:0.5pt 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 327: Line 327:
  
 
Spoken Tutorial Workshop:
 
Spoken Tutorial Workshop:
| style="border:0.5pt solid #000000;padding:0.176cm;"| The '''Spoken Tutorial Project '''Team:
+
| style="border:0.5pt 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.
 
|-
 
|-
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
 
| style="border:1pt solid #000000;padding:0.176cm;"| Slide 9:
Line 343: Line 340:
  
 
Forum For Specific Questions:
 
Forum For Specific Questions:
| style="border:0.5pt solid #000000;padding:0.176cm;"| * Do you have any general or technical questions on Embedded Linux Device Driver?
+
| style="border:0.5pt 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..

Latest revision as of 18:57, 21 July 2020

Visual cue. Narration.
Slide 1:

Welcome slide:

Welcome to the spoken tutorial on Passing Parameters to a Module.
Slide 2:

Learning objectives:

In this tutorial, we will learn how to pass parameters to a module.
Slide 3:

System Requirements:

To record this tutorial, I am using,
  • VirtualBox version 5.2
  • Ubuntu Linux 18.04 LTS operating system.
  • Linux kernel 5.0.0-31 generic
  • 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 in the Desktop which we have created earlier.

Here, I have created a directory named Parameter.

In this directory, I have saved a module file for demonstration purposes.

I have named it as mod_param dot c.

slide:

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

Type >> cd Desktop\DeviceDriver\Parameter

Open the terminal by pressing ALT+Ctrl+T keys simultaneously.

Press Enter after every command.

Go to the directory Parameter as shown here.

Highlight mod_param.c file. First we will see an example on how to pass parameters to a module.

Let us open the file mod underscore param dot c.

Type >> gedit mod_param.c Type gedit space mod_param dot c

Let us look at the code.

Highlight header files.

Highlight #include<linux/moduleparam.h>

Highlight int value, array_value[4], value, i.

We have to include these header files as it is needed by all modules.

moduleparam.h file contains the macros required to pass parameters to a module.

Here variables are declared in a module as global variables.

Highlight module_param().

Highlight name

Highlight type

// type- bool, int, long, short, uint, ulong, ushort, char*

module_param() macro is used to pass parameters to a module.

The first parameter specifies the name of the parameter.

Second parameter specifies the supported data types of variable.

For example, bool, int, long, short, uint, ulong, ushort are supported.

A char pointer value is also supported.

Highlight perm

Highlight S_I

The third parameter specifies the permissions of this module file in sysfs.

The permissions can be specified in S_I format.

Slide 5:

File permissions in S_I format

Let us see the file permissions in the module param() macro using S_I defines.

We can use these formats in our code as per the requirement.

Switch back to the code Switch back to the code.
Highlight value

Highlight int

Highlight S_IRUSR | S_IWUSR

Here, I have passed the nvalue parameter of integer data type to a module.

The next parameter indicates that the user has the read and write permissions.

Highlight module_param_array(array_value, int, NULL, S_IRUSR|S_IWUSR)

Highlight int

Highlight NULL

Highlight S_IRUSR | S_IWUSR

module_param_array macro is used to send the array as a parameter.

The array underscore value specifies the name of an array.

Here, the data type of array elements is int.

The third parameter is a pointer.

In this parameter, the kernel stores the number of array elements.

Here, we are passing a NULL pointer.

The next parameter indicates that user has the read and write permissions.

Highlight module_param_named

Highlight name

Highlight value

Highlight int

Highlight S_IRUSR | S_IWUSR

module_param_named macro is used to give different names to the same variable.

Here, name is the externally viewable parameter.

The value is the name of the internal global variable.

The data type of this variable is an integer.

The user can read and write to this file.

Highlight init_function(void)

Highlight printk messages

Highlight exit_function(void)

Highlight printk message

This is an initialization function of a module containing the printk messages.

The printk messages will print the different parameters of the above macro.

The exit function is the cleanup function of the module.

This function contains a printk message as shown here.

Open the terminal. Now, save this file and close the window.

Switch back to the terminal.

Clear the screen.

Type >> gedit Makefile

Press Enter

Highlight obj-m += mod_param.o

Save and close a file.

Open the terminal.

Let us create a Makefile to compile the module.

Type gedit space Makefile.

Write the code as shown here.


Save this file and close it.

Type >> make all

Type >> sudo su

Type >> clear.

To build the module, type make space all.

Type sudo space su to be a superuser and type the system password.

Clear the screen.

Type >> insmod mod_param.ko value=4 name=9 array_value=11,12,13,14

Highlight 4 and 9.

Highlight 11,12,13,14.

Let us insert mod_param.ko into the kernel.

Type the command as shown.

Here, we pass a value 4 to the variable nvalue and 9 to the variable name.

11,12,13,14 are passed as an array element to array underscore value.

Type >> dmesg | grep my_module

Highlight the output

Type >> clear

Let us see the loaded printk messages of a module.

Type dmesg space pipe space grep space my underscore module

These printk messages which display the parameter passes to a module.

Clear the screen.

Type rmmod mod_param.ko Let us unload a module from the kernel.

Type rmmod space mod param dot ko.

Type >> dmesg | grep my_module

Highlight the output

Type >> clear

Let us see the unloaded printk messages of a module.

Type dmesg space pipe space grep space my underscore module

The output shows the printk messages from the exit function.

Clear the screen.

Type >> make clean Type make space clean

It removes all files created after compilation.

Type >> exit To be the regular user, type exit.
With this, we come to the end of this tutorial.

Let us summarize.

Slide 5:

summary:

In this tutorial, we learnt to pass parameters to a module.
Slide 6:

Assignment:

As an assignment:
  1. Compile the mod_param.c.
  2. Load the module with change in parameters value using the insmod tool.
  3. See the output using dmesg command.
  4. Unload the module using rmmod tool.
Slide 7:

About Spoken Tutorial Project:

  • The video at the following link summarizes the Spoken Tutorial project.
  • Please download and watch it.
Slide 8:

Spoken Tutorial Workshop:

The Spoken Tutorial Project Team conducts workshops and gives certificates.

For more details, please write to us.

Slide 9:

Forum For Specific Questions:

Please post your timed queries in this forum.
Slide 10:

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 11:

Acknowledgement:

The Spoken Tutorial project is funded by MHRD, Government of India.
Slide 12:

Thank you slide:

This tutorial has been contributed by FOSSEE and Spoken Tutorial Project, IIT Bombay.

This is Mayuri Panchakshari from IIT Bombay signing off.

Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat