http://wb127.blog.51cto.com/2232662/793116
android平台2.3.4,发现插上usb鼠标和键盘开机,那么都能正常使用,一旦拔出以后再插回去,就不能使用了。
首先检测/dev/input下的设备节点是否正常,发现拔出和插入设备,节点文件都能正常删除和创建。
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 2012 ./
drwxrwxrwx 7 default default 4096 Feb 29 2012 ../
crw-r----- 1 root root 13, 63 Feb 28 2012 mice
插入鼠标:
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 2012 ./
drwxrwxrwx 7 default default 4096 Feb 29 2012 ../
crw-r----- 1 root root 13, 64 Feb 29 2012 event0
crw-r----- 1 root root 13, 63 Feb 28 2012 mice
crw-r----- 1 root root 13, 32 Feb 29 2012 mouse0
再插入键盘:
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 2012 ./
drwxrwxrwx 7 default default 4096 Feb 29 2012 ../
crw-r----- 1 root root 13, 64 Feb 29 2012 event0
crw-r----- 1 root root 13, 65 Feb 29 2012 event1
crw-r----- 1 root root 13, 63 Feb 28 2012 mice
crw-r----- 1 root root 13, 32 Feb 29 2012 mouse0
直接cat /dev/input/event0,然后晃动鼠标,能看见有接收到消息。说明kernel这一层的input系统工作正常。问题应该在android内部。
打开logcat,查看拔插消息,发现如下打印:
如果鼠标正常时拔除:
E/EventHub( 1202): remove device: /dev/input/mouse0 not found
I/EventHub( 1202): Removed device: path=/dev/input/event0 name=Logitech USB Optical Mouse id=0x10001 (of 0x2) index=3 fd=76 classes=0x8
I/InputReader( 1202): Device removed: id=0x10001, name=Logitech USB Optical Mouse, sources=00010004
如果已经拔出过,再次插入拔出时:
E/EventHub( 1202): remove device: /dev/input/mouse0 not found
E/EventHub( 1202): remove device: /dev/input/event0 not found
插入鼠标时:
E/EventHub( 1202): could not open /dev/input/mouse0, Permission denied
E/EventHub( 1202): could not open /dev/input/event0, Permission denied
android的设备检测由两部分来合作:
1.Init - system/core/init/负责处理uevent消息并在/dev下建立相关节点文件
system/core/init/devices.c
2.EventHub - frameworks/base/libs/ui/处理/dev/input/下的节点文件,监测是否有文件新建(IN_CREATE)
frameworks/base/libs/ui/EventHub.cpp
当init使用mknod()在/dev下建立节点文件,文件的owner和group都是root。然后根据需要使用chown()改变起 ower和group属性。对于/dev/input/下的节点文件,group变为input。但是EventHub随时随刻都在监测/dev /input/event*,在init创建节点但是还没有执行chown时,此时EventHub没有权限去打开。
为了验证这个理论,你可以自己写一个模块,在其初始化函数里面在/dev/input使用evdev创建节点文件event*,然后insmod这个模块,你会在logcat里面看见对应的event*无法打开,和我们开头的出错信息一样:
E/EventHub( 953): could not open /dev/input/event3, Permission denied
以下是修改内容:
--- old/system/init/devices.c
+++ new/system/init/devices.c
@@ -209,8 +209,10 @@ static void make_device(const char *path, int block, int major, int minor)
mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
dev = makedev(major, minor);
- mknod(path, mode, dev);
- chown(path, uid, -1);
+ unlink("/dev/.initdev");
+ mknod("/dev/.initdev", mode, dev);
+ chown("/dev/.initdev", uid, -1);
+ rename("/dev/.initdev", path);
}
--- old/frameworks/base/libs/ui/EventHub_old.cpp 2012-02-29 23:48:42.000000000 +0800
+++ new/frameworks/base/libs/ui/EventHub.cpp 2012-02-29 23:49:15.000000000 +0800
@@ -515,7 +515,7 @@
mDevices[0] = NULL;
#ifdef HAVE_INOTIFY
mFDs[0].fd = inotify_init();
- res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE);
+ res = inotify_add_watch(mFDs[0].fd, device_path, IN_DELETE | IN_CREATE | IN_MOVED_TO);
if(res < 0) {
LOGE("could not add watch for %s, %s\n", device_path, strerror(errno));
}
@@ -955,7 +955,7 @@
//printf("%d: %08x \"%s\"\n", event->wd, event->mask, event->len ? event->name : "");
if(event->len) {
strcpy(filename, event->name);
- if(event->mask & IN_CREATE) {
+ if(event->mask & (IN_CREATE | IN_MOVED_TO))
openDevice(devname);
}
else {