Embedded-Linux-Device-Driver/C2/Passing-Parameters-to-a-Module/English

From Script | Spoken-Tutorial
Revision as of 15:40, 21 July 2020 by Nirmala Venkat (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
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 a parameter.

The 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