Linux Device Driver Tutorial Part 21 – Tasklets | Dynamic Method

This is the Series on Linux Device Driver. The aim of this series is to provide the easy and practical examples that anyone can understand. This is the Linux Device Driver Tutorial Part 21 – Tasklets Dynamic Method Tutorial.

Prerequisites

This is the continuation of Interrupts in Linux Kernel. So I’d suggest you to know some ideas about Linux Interrupts. You can find the some useful tutorials about Interrupts and Bottom Halves below.

  1. Interrupts in Linux Kernel 
  2. Interrupts Example Program
  3. Workqueue Example – Static Method
  4. Workqueue Example – Dynamic Method
  5. Workqueue Example – Own Workqueue
  6. Tasklet Example – Static Method

Tasklets in Linux Driver

Introduction

In our Previous Tutorial we have seen the Tasklet using Static Method. In that method we had initialized the tasklet statically. But in this tutorial we are going to initialize the tasklet using dynamically. So except creation of the tasklet, everything will be same as Previous tutorial. Please refer previous tutorial for Scheduling, Enable, Disable, Kill the Tasklet.

Dynamically Creation of Tasklet

tasklet_init

This function used to Initialize the tasklet in dynamically.

void tasklet_init ( struct tasklet_struct *t,
                    void(*)(unsigned long) func,
                    unsigned long data
)

Where,

t – tasklet struct that should be initialized

func – This is the main function of the tasklet. Pointer to the function that needs to scheduled for execution at a later time.

data – Data to be passed to the function “func”.

Example

Now we will see how the function is working in background. When I call the function like above, it assigns the parameter to the passed tasklet structure. It will be looks like below.

NOTE : Please refer previous tutorial for rest of the function like Scheduling, Enable, Disable, Kill the Tasklet.

Programming

Driver Source Code

In that source code, When we read the /dev/etx_device interrupt will hit (To understand interrupts in Linux go to this tutorial). Whenever interrupt hits, I’m scheduling the task to the tasklet. I’m not going to do any job in both interrupt handler and tasklet function (only print),  since it is a tutorial post. But in real tasklet, this function can be used to carry out any operations that need to be scheduled.

NOTE: In this source code many unwanted functions will be there (which is not related to the Tasklet). Because I’m just maintaining the source code throughout these Device driver series.

MakeFile

Building and Testing Driver

  • Build the driver by using Makefile (sudo make)
  • Load the driver using sudo insmod driver.ko
  • To trigger interrupt read device file (sudo cat /dev/etx_device)
  • Now see the Dmesg (dmesg)

[email protected]: dmesg

[12372.451624] Major = 246 Minor = 0
[12372.456927] Device Driver Insert…Done!!!
[12375.112089] Device File Opened…!!!
[12375.112109] Read function
[12375.112134] Shared IRQ: Interrupt Occurred
[12375.112139] Executing Tasklet Function : arg = 0
[12375.112147] Device File Closed…!!!
[12377.954952] Device Driver Remove…Done!!!

  • We can able to see the print “Shared IRQ: Interrupt Occurred“ and “Executing Tasklet Function : arg = 0
  • Unload the module using sudo rmmod driver

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!