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 easy and practical examples that anyone can understand. This is the Linux Device Driver Tutorial Part 21 – Tasklet Dynamic Method Tutorial.

Prerequisites

This is the continuation of Interrupts in the Linux Kernel. So I’d suggest you to know some ideas about Linux Interrupts. You can find 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 the same as the previous tutorial. Please refer the previous tutorial for Scheduling, Enable, Disable, Kill the Tasklet.

Dynamically Creation of Tasklet

tasklet_init

This function used to Initialize the tasklet 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 schedule 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 the 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 the previous tutorial for the rest of the function like Scheduling, Enable, Disable, Kill the Tasklet.

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.

[Get the source code from the GitHub]

MakeFile

Building and Testing Driver

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

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

In our next tutorial, we will discuss Mutex in the Linux device driver.

0 0 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pallavi Shetty
Pallavi Shetty
May 2, 2020 5:24 PM

Good article. Cheers.

KOTHAPALLY AJAY KUMAR
KOTHAPALLY AJAY KUMAR
May 25, 2020 4:36 PM

Good explanation sir.
Can you please upload device drivers interview questions or provide me if you have any links?

2
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: