int recognized_device(usb_handle* handle) { if (nullptr == handle) return 0; // Check vendor and product id first USB_DEVICE_DESCRIPTOR device_desc; if (!AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) { D("AdbGetUsbDeviceDescriptor failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); return 0; } // Then check interface properties USB_INTERFACE_DESCRIPTOR interf_desc; if (!AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) { D("AdbGetUsbInterfaceDescriptor failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); return 0; } // Must have two endpoints if (2 != interf_desc.bNumEndpoints) { return 0; } if (!is_adb_interface(interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) { return 0; } AdbEndpointInformation endpoint_info; // assuming zero is a valid bulk endpoint ID if (AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) { handle->max_packet_size = endpoint_info.max_packet_size; handle->zero_mask = endpoint_info.max_packet_size - 1; D("device zero_mask: 0x%x", handle->zero_mask); } else { D("AdbGetEndpointInformation failed: %s", android::base::SystemErrorCodeToString(GetLastError()).c_str()); } return 1; }
int recognized_device(usb_handle* handle) { if (NULL == handle) return 0; // Check vendor and product id first USB_DEVICE_DESCRIPTOR device_desc; if (!bridge->AdbGetUsbDeviceDescriptor(handle->adb_interface, &device_desc)) { return 0; } // Then check interface properties USB_INTERFACE_DESCRIPTOR interf_desc; if (!bridge->AdbGetUsbInterfaceDescriptor(handle->adb_interface, &interf_desc)) { return 0; } // Must have two endpoints if (2 != interf_desc.bNumEndpoints) { return 0; } if (is_adb_interface(device_desc.idVendor, device_desc.idProduct, interf_desc.bInterfaceClass, interf_desc.bInterfaceSubClass, interf_desc.bInterfaceProtocol)) { if(interf_desc.bInterfaceProtocol == 0x01) { AdbEndpointInformation endpoint_info; // assuming zero is a valid bulk endpoint ID if (bridge->AdbGetEndpointInformation(handle->adb_interface, 0, &endpoint_info)) { handle->zero_mask = endpoint_info.max_packet_size - 1; } } return 1; } return 0; }
//* TODO: simplify this further since we only register to get ADB interface //* subclass+protocol events static usb_handle* CheckInterface(IOUSBInterfaceInterface **interface, UInt16 vendor, UInt16 product) { usb_handle* handle = NULL; IOReturn kr; UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol; UInt8 endpoint; //* Now open the interface. This will cause the pipes associated with //* the endpoints in the interface descriptor to be instantiated kr = (*interface)->USBInterfaceOpen(interface); if (kr != kIOReturnSuccess) { DBG("ERR: Could not open interface: (%08x)\n", kr); return NULL; } //* Get the number of endpoints associated with this interface kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints); if (kr != kIOReturnSuccess) { DBG("ERR: Unable to get number of endpoints: (%08x)\n", kr); goto err_get_num_ep; } //* Get interface class, subclass and protocol if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess || (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess || (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) { DBG("ERR: Unable to get interface class, subclass and protocol\n"); goto err_get_interface_class; } //* check to make sure interface class, subclass and protocol match ADB //* avoid opening mass storage endpoints if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) goto err_bad_adb_interface; handle = (usb_handle *)calloc(1, sizeof(usb_handle)); //* Iterate over the endpoints for this interface and find the first //* bulk in/out pipes available. These will be our read/write pipes. for (endpoint = 0; endpoint <= interfaceNumEndpoints; endpoint++) { UInt8 transferType; UInt16 maxPacketSize; UInt8 interval; UInt8 number; UInt8 direction; kr = (*interface)->GetPipeProperties(interface, endpoint, &direction, &number, &transferType, &maxPacketSize, &interval); if (kIOReturnSuccess == kr) { if (kUSBBulk != transferType) continue; if (kUSBIn == direction) handle->bulkIn = endpoint; if (kUSBOut == direction) handle->bulkOut = endpoint; handle->zero_mask = maxPacketSize - 1; } else { DBG("ERR: FindDeviceInterface - could not get pipe properties\n"); goto err_get_pipe_props; } } handle->interface = interface; return handle; err_get_pipe_props: free(handle); err_bad_adb_interface: err_get_interface_class: err_get_num_ep: (*interface)->USBInterfaceClose(interface); return NULL; }