I have a code which detects GPIO interrupt in a kernel module. Now,I am looking for a mechanism to notify user space upon detecting gpio interrupt from kernel module. Any example / code snippet with certain advantages/disadvantages over different options? I would appreciate your response.
Take a look at the GPIO keyboard driver (drivers/input/keyboard/gpio_keys.c
). It is a good starting point for your problem.
In the userspace you then listen (some blocking read for example, or just tail
to test) to /dev/input/yourevent
for events.
You can send a signal to user space thread from kernel API, which can help u run non-blocking:
send_sig(int sig, struct task_struct *p, int priv)
But there is a limitation: u need to be aware of pid of user thread in Kernel. You can over come this by writing pid of user process via /proc and then kernel reading the pid. With this arrangement, when there is an interrupt, kernel can send signal to user thread. In case your process restarts or gets killed, you will have to update the pid via proc.
If i were you, i would have preferred to do this unless i dont want to transfer data from kernel to user. For data transfer requirement, i would have used Netlink or some other mechanism.
You can: (1) Send a signal to the user application, or (2) implement file_operations->poll method, use poll_wait and wait queue to wake user application when interrupt occur.
来源:https://stackoverflow.com/questions/19820177/notify-gpio-interrupt-to-user-space-from-a-kernel-module