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 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 the continuation (Part 2) of the Previous Tutorial.

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

If you don’t know the functions used in the linked list, please refer to 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 the software. If you don’t know how it works, please refer to this tutorial.
  2. The interrupt will invoke the ISR function.
  3. In ISR we are allocating work to the Workqueue.
  4. Whenever Workqueue executing, we are creating the 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 are present in the Linked List using traverse.
  6. When we are removing the driver using rmmod, it will remove 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 members. 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 node using list_for_each_entry and print those values. (This part will be present in the read function)

Deleting Linked List

This will traverse 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.

Programming

Driver Source Code

MakeFile

Building and Testing Driver

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

  • By this time there are 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)

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

In our next tutorial, we will discuss kernel threads.

0 0 vote
Article Rating
Subscribe
Notify of
guest

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

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
madhab
madhab
9 months ago

where can i find my inserted values except dmesg ?
and where and when we should use this linux list ?

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