From: http://embed.chinaitlab.com/J2ME/877029.html
Recently, I was working on a Marvell project and encountered a hot plug-in problem. So I took a look at the relevant code. I would like to make a contribution here.
Here we take the PXA code as an example. The entire framework is simple and clear. The upper layers are headsetobserver. Java and hookswitchobserver. java,
They will directly read the state of a specific device in/sys/class/switch/, such as "/sys/class/switch/h2w/state" to obtain the latest state.
Let's divide it into two parts.
Kernel/Drivers/switch/
The headset and hook driver code are stored here. First, switch_class.c registers the switch sub-class and provides the switch_dev_register registration entry,
The corresponding device driver calls switch_dev_register to register itself in the switch subclass. For example:
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 );
In the/sys/class/switch directory, the h2w sub-directory will be generated, with some members such as State and name, and the driver will determine whether the headset is inserted through
Gpio simulates the interruption. When the headset is inserted, a level change is generated to call the interrupt callback function. The callback function changes the state value, which is so simple.
Frameworks/base/services/Java/COM/Android/Server/headsetobserver. Java this is the android headset monitoring code
It reads/sys/class/switch/h2w/State to determine whether there is a headset inserted at this time. For example:
Private Static final string headset_state_path = "/sys/class/switch/h2w/state ";
Filereader file = new filereader (headset_state_path );
Then
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, "cocould not parse switch state from event" + Event );
}
}
To update the Detection Status of the headset. Hook monitoring is similar to headset monitoring, so we will not repeat it here. It is a bit messy and I hope it will be helpful to my friends.