Skip to content

Day 18 โ€” Blocking I/O and Event-driven Driver

๐ŸŽฏ Goal

Understand how Linux drivers handle data flow using:

  • blocking read
  • wait queue
  • poll
  • event-driven design

๐Ÿง  What I Learned

1. Blocking โ‰  automatic

Even without O_NONBLOCK:

  • read() does NOT automatically block
  • driver must implement wait_event()

2. Wait queue is the core

Used to connect:

  • interrupt / workqueue
  • user process

3. Proper event flow

IRQ โ†’ workqueue โ†’ enqueue โ†’ wake_up โ†’ read()


4. poll() integration

poll() relies on:

  • poll_wait()
  • wait queue

5. Event queue importance

  • avoid losing events
  • decouple producer / consumer

6. Lock design

  • spinlock โ†’ event queue
  • mutex โ†’ control plane

๐Ÿ›  Implementation

  • added enqueue / dequeue helper
  • refactored workqueue handler
  • implemented blocking read
  • supported O_NONBLOCK
  • verified poll behavior

๐Ÿงช Test Result

  • blocking read works
  • non-blocking returns -EAGAIN
  • poll wakes correctly
  • no busy loop
  • debounce works with sysfs / ioctl

๐Ÿ’ก Key Insight

A correct Linux driver is:

  • event-driven
  • not polling-based
  • not busy-looping

๐Ÿš€ Next

Move to real hardware driver:

  • I2C sensor
  • subsystem integration (IIO / hwmon)