android平台2.3.4,发现插上usb鼠标和键盘开机,那么都能正常使用,一旦拔出以后再插回去,就不能使用了。
首先检测/dev/input下的设备节点是否正常,发现拔出和插入设备,节点文件都能正常删除和创建。
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 ./
drwxrwxrwx 7 default default 4096 Feb 29 ../
crw-r----- 1 root root 13, 63 Feb 28 mice
插入鼠标:
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 ./
drwxrwxrwx 7 default default 4096 Feb 29 ../
crw-r----- 1 root root 13, 64 Feb 29 event0
crw-r----- 1 root root 13, 63 Feb 28 mice
crw-r----- 1 root root 13, 32 Feb 29 mouse0
再插入键盘:
# ls /dev/input/ -al
total 8
drwxrwxrwx 2 root root 4096 Feb 29 ./
drwxrwxrwx 7 default default 4096 Feb 29 ../
crw-r----- 1 root root 13, 64 Feb 29 event0
crw-r----- 1 root root 13, 65 Feb 29 event1
crw-r----- 1 root root 13, 63 Feb 28 mice
crw-r----- 1 root root 13, 32 Feb 29 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
以下是修改内容: