Skip to content

Linux Device Driver Development

adi33kumar edited this page May 8, 2026 · 4 revisions

Topics to cover for Linux Device Driver Development

Basic Kernel concepts:

Kernel Locking APIs

Spin lock

Spin lock protects resources that only one CPU can take access at a time. HW Locking mechanism. Provides atomic operations Inter-cpu locking primitive/object.

Two ways to create/initialize the spin lock

  1. Statically: DEFINE_SPINLOCK macro. Declares and initialise the spin lock. static DEFINE_SPINLOCK(foo_lock)
  2. Dynamically: spin_lock_init(). For dynamic (runtime) allocation, you need to embed the spinlock into a bigger structure, allocate memory for this structure, and then call spin_lock_init() on the spinlock element.

E.g. struct bigger_struct { spinlock_t lock; unsigned int foo; [...] };

static struct bigger_struct *fake_alloc_init_function() { struct bigger_struct *bs; bs = kmalloc(sizeof(struct bigger_struct), GFP_KERNEL); if (!bs) return -ENOMEM; spin_lock_init(&bs->lock); return bs; }

Mutex

Task will be suspended if mutex is acquired by other task. Will be woken when mutex is released.

A spinlock is a lock held by a CPU, while a mutex is a lock held by a task.

Work deferring mechanism

SoftIRQ

Stands for software interrupt. Preempt all tasks except HW IRQs(since executed with IRQ enabled) Network devices and block devices directly uses softIRQs. Statically located during compile time. Represented by struct softirq_action structure. SoftIRQ can not preempt another soft IRQ. Only HW interrupts can. Executed with high priority with schedular preemption disabled but IRQ enabled. Concurrency: Soft IRQ can run on other processor at same time.(Advantage of Soft IRQ over hard IRQ). spin_lock and spin_unlock() should be used to acquire access of shared resource on other cores.

How to define the event when to raise_soft_irq?

TaskLets

Built on top of HI_SOFTIRQ. Follow same rules as softIRQs but same tasklets never run concurrently.

Workqueue

Linux kernel interrupt management

The interrupts are enabled/disabled by setting/clearing the bits in processor status/control register. ISR must be registers at special location called vector table. Task which is done when interrupt is generated. Context saving and masking interrupt on local CPU then interrupt servicing.

Design of interrupt handler

  1. Defining each system call for each device connected.

insmod

rmmod

Clone this wiki locally