Skip to contentSkip to conversation

Notification when a new device is added or removed

DISCUSSION Alessio Sangalli 3 participants 6 messages Feb 28, 2009 – Mar 9, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Hi, what is the easiest way to get somewhat notified when a device is added or removed from the system? I know /proc/bus/input/devices reports the devices list, can I make use of that? I am in an embedded environment where I prefer not having to bringup all the hotplug + udev infrastructure.

bye
Alessio

maintainer
#1 lore raw
Quoted 6 lines · Alessio Sangalli

Hi, what is the easiest way to get somewhat notified when a device is added or removed from the system? I know /proc/bus/input/devices reports the devices list, can I make use of that? I am in an embedded environment where I prefer not having to bringup all the hotplug + udev infrastructure.

You should be able to poll (select) /proc/bus/input/devices.

Signature · 1 line
Dmitry
#2 lore raw
Quoted 1 line · Dmitry Torokhov

You should be able to poll (select) /proc/bus/input/devices.

Yeah that would be great; anyway, I tried and... well nothing happens. I am obviously missing something here:

#include <poll.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()

{
        int fd, c;
        struct pollfd pfd;

        fd=open("/proc/bus/input/devices", O_RDONLY);
        if(fd==-1)
                return -1;
        pfd.fd=fd;
        pfd.events=POLLIN;
        while(1) {
                c=poll(&pfd, 1, 20000);
                if(c==-1)
                        printf("Error polling\n");
                else if(c==0)
                        printf("Timeout\n");
                else
                        printf("Something changed");
        }
}

The result, while I detach or attach new devices is:

Timeout
Timeout
Timeout
Timeout
Timeout

bye
Alessio

maintainer
#3 lore raw
Quoted 7 lines · Alessio Sangalli
Quoted 1 line · Dmitry Torokhov

You should be able to poll (select) /proc/bus/input/devices.

Yeah that would be great; anyway, I tried and... well nothing happens. I am obviously missing something here:

Hmm, it turns out poll is completely busted. The patch below should help.

Signature · 9 lines
Dmitry

Input: fix polling of /proc/bus/input/devices

Signed-off-by: Dmitry Torokhov <[email protected]>
---

 drivers/input/input.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
drivers/input/input.c +3 -3
index 46e9ce1..913392f 100644
@@ -744,11 +744,11 @@ static inline void input_wakeup_procfs_readers(void)
 
 static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
 {
-	int state = input_devices_state;
-
 	poll_wait(file, &input_devices_poll_wait, wait);
-	if (state != input_devices_state)
+	if (file->f_version != input_devices_state) {
+		file->f_version = input_devices_state;
 		return POLLIN | POLLRDNORM;
+	}
 
 	return 0;
 }
#4 lore raw
Quoted 2 lines · Dmitry Torokhov

Hmm, it turns out poll is completely busted. The patch below should help.

Testing shows that patch solves the problem.

bye!
as

#5 lore raw
In reply to Dmitry Torokhov
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Dmitry" == Dmitry Torokhov <[email protected]> writes:

Hi,

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

You should be able to poll (select) /proc/bus/input/devices.

Yeah that would be great; anyway, I tried and... well nothing happens. I am obviously missing something here:

Dmitry> Hmm, it turns out poll is completely busted. The patch below should Dmitry> help.

2.6.29 material?

Signature · 1 line
Bye, Peter Korsgaard