最近在做Marvell的项目,碰到个耳机热插拔的问题,所以趁便看了下相关代码,这儿来奉献一下给咱们。
咱们这儿以PXA的代码为例,整个结构比较的简略明晰,上层是分别是HeadsetObserver.java以及HookSwitchObserver.java,
它们会直接读取/sys/class/switch/中某一特定的设备的state,比方/sys/class/switch/h2w/state,来获取最新的状况。
咱们把它分为两部分来看
kernel/drivers/switch/
这儿存放着headset以及hook的驱动代码,首要switch_class.c中会先注册switch这个子类,并供给switch_dev_register这个注册进口,
对应的设备驱动调用switch_dev_register把自己注册在switch这个子类中,比方:
switch_data_headset->sdev.name = pdata_headset->name; //name is h2w
switch_data_headset->name_on = pdata_headset->name_on;
switch_data_headset->name_off = pdata_headset->name_off;
switch_data_headset->state_on = pdata_headset->state_on;
switch_data_headset->state_off = pdata_headset->state_off;
switch_data_headset->sdev.print_state = switch_headset_print_state;
info->psw_data_headset = switch_data_headset;
ret = switch_dev_register(switch_data_headset->sdev);
那么在/sys/class/switch目录中就会生成h2w这个子目录,里边会有state,name等一些成员,而驱动判别耳机是否刺进则是经过
gpio模仿中止,在耳机刺进的时分发生电平的改动来调用中止回调函数,回调函数中会改动state状况值,就这么简略
frameworks/base/services/java/com/android/server/HeadsetObserver.java这是android耳机监测的代码
它会经过读取/sys/class/switch/h2w/state来判别此刻是否有耳机刺进,比方:
private static final String HEADSET_STATE_PATH = /sys/class/switch/h2w/state;
FileReader file = new FileReader(HEADSET_STATE_PATH);
然后经过
public void onUEvent(UEventObserver.UEvent event) {
if (LOG) Slog.v(TAG, Headset UEVENT: + event.toString());
try {
update(event.get(SWITCH_NAME), Integer.parseInt(event.get(SWITCH_STATE)));
} catch (NumberFormatException e) {
Slog.e(TAG, Could not parse switch state from event + event);
}
}
来更新耳机的检测状况。hook监测和耳机监测相似,这儿就不再重复了,写得有点乱,期望对朋友们有协助。