Linux Device Driver Tutorial Part 18 – Linked List in Linux Kernel Part 2

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. In our previous tutorial we have seen  the functions used in Kernel Linked List. So this is the Linux Device Driver Tutorial Part 18 – Example Linked List in Linux Kernel which is continuation (Part 2) of Previous Tutorial.

Linux Device Driver Tutorial Part 18 – Example Linked List in Linux Kernel

If you don’t know the functions used in linked list, please refer this previous tutorial for the detailed explanation about all linked list functions.

So now we can directly enter into the Linux Linked List Kernel programming. I took the source code form the previous tutorial. First i will explain how this code works.

  1. When we write the value to our device file using echo value > /dev/etx_value, it will invoke the interrupt. Because we configured the interrupt by using software. If you don’t know how it works, Please refer this tutorial.
  2. Interrupt will invoke the ISR function.
  3. In ISR we are allocating work to the Workqueue.
  4. Whenever Workqueue executing, we are creating Linked List Node and adding the Node to the Linked List.
  5. When we are reading the driver using cat /dev/etx_device, printing all the nodes which is present in the Linked List using traverse.
  6. When we are removing the driver using rmmod, it will removes all the nodes in Linked List and free the memory.

Note : We are not using the sysfs functions. So I kept empty sysfs functions.

Creating Head Node

This will create the head node in the name of Head_Node and initialize that.

Creating Node and add that into Linked List

This will create the node, assign the data to its member. Then finally add that node to the Linked List using list_add_tail. (This part will be present in the workqueue function)

Traversing Linked List

Here, we are traversing each nodes using list_for_each_entryand print those values. (This part will be present in the read function)

Deleting Linked List

This will traverse the each node using  list_for_each_entry_safe and delete that using list_del. Finally we need to free the memory which is allocated using kmalloc.


Driver Source Code


Building and Testing Driver

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

[ 5310.125001] Major = 246 Minor = 0 n
[ 5310.133127] Device Driver Insert…Done!!!
[ 5346.839872] Device File Opened…!!!
[ 5346.839950] Read function
[ 5346.839954] Total Nodes = 0
[ 5346.839982] Device File Closed…!!!

  • By this time there is no nodes available.
  • So now write the value to driver using echo 10 > /dev/etx_device
  • By this time, One node has been added to the linked list.
  • To test that read the device file using cat /dev/etx_device
  • Now see the Dmesg (dmesg)

[ 5346.839982] Device File Closed…!!!
[ 5472.408239] Device File Opened…!!!
[ 5472.408266] Write Function
[ 5472.408293] Shared IRQ: Interrupt Occurred
[ 5472.408309] Device File Closed…!!!
[ 5472.409037] Executing Workqueue Function
[ 5551.996018] Device File Opened…!!!
[ 5551.996040] Read function
[ 5551.996044] Node 0 data = 10
[ 5551.996046] Total Nodes = 1
[ 5551.996052] Device File Closed…!!!

  • Our value has added to the list.
  • You can also write many times to create and add the node to linked list
  • Unload the module using rmmod driver
%d bloggers like this: