最近在做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);
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);
- }
- }