Android Usb源码分析
android上usb设备接入,拔出, 读写操作过程分析
a. usb设备插入,如何组成usbdevice. 存放在UsbManager.devicesList.
从两个角度来看
1, 从设备插入后, kernel端怎么处理.
usb设备插入后host端如何获取该设备信息呢?
分析:
当usb设备接入时, 会有硬件产生中断. 收到中断后host端会由总线驱动程序根据usb协议, 对端口0地址发送一个请求包给usb设备.
2. 设备收到请求后会将自己的设备描述符信息发给host端.
3. host端通过解析设备描述符来获取usb设备信息, 同时找到对应的设备驱动, 调用其prob函数.
在linux 4.4.103上代码跟踪如下:
drivers/usb/core/hub.c
hub_irq(struct urb *urb)
|-kick_hub_wq(struct usb_hub *hub)
|-queue_work(hub_wq, &hub->events)
hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
|-INIT_WORK(&hub->events, hub_event);
|-hub_event(struct work_struct *work)
|–port_event(struct usb_hub *hub, int port1)
|—hub_port_connect_change(struct usb_hub *hub, int port1, u16 portstatus, u16 portchange)
|----hub_port_connect(struct usb_hub *hub, int port1, u16 portstatus,u16 portchange)
|-----udev = usb_alloc_dev(hdev, hdev->bus, port1);
|-----hub_port_init(struct usb_hub *hub, struct usb_device *udev, int port1,int retry_counter)
|------usb_control_msg()
|------hub_set_address(udev, devnum);
|------usb_get_device_descriptor(udev, 8);
…
|-----hcd->driver->update_device(hcd, udev);
未完待续…
2, 从调用角度来跟踪.
获取后又是如何将改设备信息组成usbdevice对象存放在deviceslist?
反过来跟踪代码, 从调用端看.
|-UsbManager#getDeviceList();
|–mService.getDeviceList(bundle);
|—UsbServer#getDeviceList(Bundle devices)
|----UsbHostManager#getDeviceList(Bundle devices)
从全局变量HashMap<String, UsbDevice> mDevices取出数据存入Bundle.
而更新mDevices数据由底层jni函数调用
/* Called from JNI in monitorUsbHostBus() to finish adding a new device /
@SuppressWarnings(“unused”)
private void endUsbDeviceAdded()
看注释可以发现,底层jni会分别调用如下函数来晚上devices信息.
/ Called from JNI in monitorUsbHostBus() to report new USB devices
Returns true if successful, in which case the JNI code will continue adding configurations,
interfaces and endpoints, and finally call endUsbDeviceAdded after all descriptors
have been processed
*/
现在我们需要跟踪一下JNI层beginUsbDeviceAdded和adding configurations, interfaces and endpoints和endUsbDeviceAdded
分别是在什么时候调用的
jni的调用来自于com_android_server_UsbHostManager.cpp base\services\core\jni 中
usb_device_added分别调用了
|–method_beginUsbDeviceAdded
|–method_addUsbConfiguration
|–method_addUsbInterface
|–method_addUsbEndpoint
|–method_endUsbDeviceAdded
而这个函数由java端调用.
static const JNINativeMethod method_table[] = {
{ "monitorUsbHostBus", "()V", (void*)android_server_UsbHostManager_monitorUsbHostBus },
{ "nativeOpenDevice", "(Ljava/lang/String;)Landroid/os/ParcelFileDescriptor;",
(void*)android_server_UsbHostManager_openDevice },
};
>UsbHostManager里
public void systemReady() {
synchronized (mLock) {
// Create a thread to call into native code to wait for USB host events.
// This thread will call us back on usbDeviceAdded and usbDeviceRemoved.
Runnable runnable = this::monitorUsbHostBus;
new Thread(null, runnable, "UsbService host thread").start();
}
}
>systemReady调用来自于UsbService的Lifecycle/SystemService
> @Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY) {
mUsbService.systemReady();
} else if (phase == SystemService.PHASE_BOOT_COMPLETED) {
mUsbService.bootCompleted();
}
}
所以操作又重新回到com_android_server_UsbHostManager.cpp中
static void android_server_UsbHostManager_monit