Interrupt Handling and User Space Notification

sending asynchronous events to user space on interrupt detection

You can use Netlink sockets in order to send an event from kernel space to user space. You can then spawn a thread in user space which listens to this Netlink socket. This can be done using select() or epoll() function in your user-space application. http://www.linuxjournal.com/article/7356 is a good reference.

Event notification from kernel space to user space

(Had replied in the chat session but it seems like this should be in an answer so putting it here with more detail.)

What poll_wait does is add your driver to the list of file descriptors being waited for by the user space program. The pattern is:

  • user program calls poll/select/epoll_ctl
  • core kernel calls your driver's poll entry point
  • driver calls poll_wait to add its wait queue to the list of waitqueues (unless the GPIO data is already readable, which you indicate via return value)
  • later, when the device interrupts, you call wake_up on your wait queue, this unblocks the process if it's (still) waiting in a poll/select call
  • user-mode program wakes up, calls read to actually obtain the data

IOW, poll_wait itself doesn't sleep (or block); it just adds your device to the list of programs that might wake the process up later. The sleep is done in the core kernel (inside the select system call, for example). That way, a user program can wait on any number of devices at once using select.

If your user-space program really doesn't have anything else to do while waiting, then you can simply have the user-program call read, and have your driver set up its wait queue and call wait_event_interruptible (or one of the other wait_event_* variants). This will block the process until your interrupt handler calls wake_up; at which time you copy from kernel buffer to user buffer.

Or you could support both methods. Typically if you support the select method, you also check the O_NONBLOCK file flag in your read function so that the user code has the option to not block in the read.

Yes, ISRs can call wake_up. It's a common pattern for device I/O: wait/block in "process context", wake-up in "interrupt context", then complete the I/O after returning to process context.

BTW, from the driver point of view, using select, poll or epoll are typically the same. From the user point of view, using select or poll is somewhat easier. It's a "one shot" deal: "here are a set of file descriptors; block until one of them is ready for read (or write etc) or until a timeout".

Whereas with epoll, you first create an epoll descriptor, then add I/O file descriptors individually. But then the "wait" call just specifies the single epoll descriptor. So if you have a large number of file descriptors to wait on, you don't have to specify all of them in each system call which leads to lower system call overhead on each epoll call.



Related Topics



Leave a reply



Submit