blob: ddde454d12583d76186f5ea49d8a607f0064c279 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <CoreFoundation/CoreFoundation.h>
22
23#include <IOKit/IOKitLib.h>
24#include <IOKit/IOCFPlugIn.h>
25#include <IOKit/usb/IOUSBLib.h>
26#include <IOKit/IOMessage.h>
27#include <mach/mach_port.h>
28
Dan Albert7447dd02015-04-16 19:20:40 -070029#include <inttypes.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030#include <stdio.h>
31
Yabin Cui71bddf82016-03-25 13:55:07 -070032#include <atomic>
33#include <memory>
34#include <mutex>
35#include <vector>
36
Elliott Hughes4f713192015-12-04 22:00:26 -080037#include <android-base/logging.h>
38#include <android-base/stringprintf.h>
Siva Velusamy743883b2015-08-18 17:53:16 -070039
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040#include "adb.h"
Dan Albertdc0f8ec2015-02-25 10:26:17 -080041#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043struct usb_handle
44{
Siva Velusamyd8b48a62015-08-13 08:48:06 -070045 UInt8 bulkIn;
46 UInt8 bulkOut;
47 IOUSBInterfaceInterface190** interface;
Siva Velusamyd8b48a62015-08-13 08:48:06 -070048 unsigned int zero_mask;
Yabin Cui71bddf82016-03-25 13:55:07 -070049
50 // For garbage collecting disconnected devices.
51 bool mark;
52 std::string devpath;
53 std::atomic<bool> dead;
54
55 usb_handle() : bulkIn(0), bulkOut(0), interface(nullptr),
56 zero_mask(0), mark(false), dead(false) {
57 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058};
59
Yabin Cui71bddf82016-03-25 13:55:07 -070060static std::atomic<bool> usb_inited_flag;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061
Yabin Cui71bddf82016-03-25 13:55:07 -070062static auto& g_usb_handles_mutex = *new std::mutex();
63static auto& g_usb_handles = *new std::vector<std::unique_ptr<usb_handle>>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
Yabin Cui71bddf82016-03-25 13:55:07 -070065static bool IsKnownDevice(const std::string& devpath) {
66 std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
67 for (auto& usb : g_usb_handles) {
68 if (usb->devpath == devpath) {
69 // Set mark flag to indicate this device is still alive.
70 usb->mark = true;
71 return true;
72 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 }
Yabin Cui71bddf82016-03-25 13:55:07 -070074 return false;
75}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
Yabin Cui71bddf82016-03-25 13:55:07 -070077static void usb_kick_locked(usb_handle* handle);
Al Sutton8e01cc62014-11-21 12:21:12 +000078
Yabin Cui71bddf82016-03-25 13:55:07 -070079static void KickDisconnectedDevices() {
80 std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
81 for (auto& usb : g_usb_handles) {
82 if (!usb->mark) {
83 usb_kick_locked(usb.get());
84 } else {
85 usb->mark = false;
86 }
87 }
88}
Al Sutton8e01cc62014-11-21 12:21:12 +000089
Yabin Cui71bddf82016-03-25 13:55:07 -070090static void AddDevice(std::unique_ptr<usb_handle> handle) {
91 handle->mark = true;
92 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
93 g_usb_handles.push_back(std::move(handle));
94}
95
96static void AndroidInterfaceAdded(io_iterator_t iterator);
97static std::unique_ptr<usb_handle> CheckInterface(IOUSBInterfaceInterface190 **iface,
98 UInt16 vendor, UInt16 product);
99
100static bool FindUSBDevices() {
101 // Create the matching dictionary to find the Android device's adb interface.
102 CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBInterfaceClassName);
103 if (!matchingDict) {
104 LOG(ERROR) << "couldn't create USB matching dictionary";
105 return false;
106 }
107 // Create an iterator for all I/O Registry objects that match the dictionary.
108 io_iterator_t iter = 0;
109 kern_return_t kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
110 if (kr != KERN_SUCCESS) {
111 LOG(ERROR) << "failed to get matching services";
112 return false;
113 }
114 // Iterate over all matching objects.
115 AndroidInterfaceAdded(iter);
116 IOObjectRelease(iter);
117 return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118}
119
120static void
Yabin Cui71bddf82016-03-25 13:55:07 -0700121AndroidInterfaceAdded(io_iterator_t iterator)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122{
123 kern_return_t kr;
124 io_service_t usbDevice;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700125 io_service_t usbInterface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126 IOCFPlugInInterface **plugInInterface = NULL;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700127 IOUSBInterfaceInterface220 **iface = NULL;
128 IOUSBDeviceInterface197 **dev = NULL;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 HRESULT result;
130 SInt32 score;
Elliott Hughes62077d32015-08-25 17:48:12 -0700131 uint32_t locationId;
Dan Albert7447dd02015-04-16 19:20:40 -0700132 UInt8 if_class, subclass, protocol;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133 UInt16 vendor;
134 UInt16 product;
135 UInt8 serialIndex;
136 char serial[256];
Yabin Cui71bddf82016-03-25 13:55:07 -0700137 std::string devpath;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138
Dima Zavin3fd82b82009-05-08 18:25:58 -0700139 while ((usbInterface = IOIteratorNext(iterator))) {
140 //* Create an intermediate interface plugin
141 kr = IOCreatePlugInInterfaceForService(usbInterface,
142 kIOUSBInterfaceUserClientTypeID,
143 kIOCFPlugInInterfaceID,
144 &plugInInterface, &score);
145 IOObjectRelease(usbInterface);
146 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700147 LOG(ERROR) << "Unable to create an interface plug-in (" << std::hex << kr << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700148 continue;
149 }
150
151 //* This gets us the interface object
Dan Albert7447dd02015-04-16 19:20:40 -0700152 result = (*plugInInterface)->QueryInterface(
153 plugInInterface,
154 CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*)&iface);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700155 //* We only needed the plugin to get the interface, so discard it
156 (*plugInInterface)->Release(plugInInterface);
157 if (result || !iface) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700158 LOG(ERROR) << "Couldn't query the interface (" << std::hex << result << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700159 continue;
160 }
161
Dan Albert7447dd02015-04-16 19:20:40 -0700162 kr = (*iface)->GetInterfaceClass(iface, &if_class);
Al Sutton8e01cc62014-11-21 12:21:12 +0000163 kr = (*iface)->GetInterfaceSubClass(iface, &subclass);
164 kr = (*iface)->GetInterfaceProtocol(iface, &protocol);
Dan Albert7447dd02015-04-16 19:20:40 -0700165 if(if_class != ADB_CLASS || subclass != ADB_SUBCLASS || protocol != ADB_PROTOCOL) {
Al Sutton8e01cc62014-11-21 12:21:12 +0000166 // Ignore non-ADB devices.
Siva Velusamy743883b2015-08-18 17:53:16 -0700167 LOG(DEBUG) << "Ignoring interface with incorrect class/subclass/protocol - " << if_class
168 << ", " << subclass << ", " << protocol;
Al Sutton8e01cc62014-11-21 12:21:12 +0000169 (*iface)->Release(iface);
170 continue;
171 }
172
Dima Zavin3fd82b82009-05-08 18:25:58 -0700173 //* this gets us an ioservice, with which we will find the actual
174 //* device; after getting a plugin, and querying the interface, of
175 //* course.
176 //* Gotta love OS X
177 kr = (*iface)->GetDevice(iface, &usbDevice);
178 if (kIOReturnSuccess != kr || !usbDevice) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700179 LOG(ERROR) << "Couldn't grab device from interface (" << std::hex << kr << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700180 continue;
181 }
182
183 plugInInterface = NULL;
184 score = 0;
185 //* create an intermediate device plugin
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 kr = IOCreatePlugInInterfaceForService(usbDevice,
187 kIOUSBDeviceUserClientTypeID,
188 kIOCFPlugInInterfaceID,
189 &plugInInterface, &score);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700190 //* only needed this to find the plugin
191 (void)IOObjectRelease(usbDevice);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 if ((kIOReturnSuccess != kr) || (!plugInInterface)) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700193 LOG(ERROR) << "Unable to create a device plug-in (" << std::hex << kr << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700194 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800195 }
196
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197 result = (*plugInInterface)->QueryInterface(plugInInterface,
Dan Albert7447dd02015-04-16 19:20:40 -0700198 CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID), (LPVOID*)&dev);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700199 //* only needed this to query the plugin
200 (*plugInInterface)->Release(plugInInterface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 if (result || !dev) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700202 LOG(ERROR) << "Couldn't create a device interface (" << std::hex << result << ")";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700203 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204 }
205
Dima Zavin3fd82b82009-05-08 18:25:58 -0700206 //* Now after all that, we actually have a ref to the device and
207 //* the interface that matched our criteria
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800208 kr = (*dev)->GetDeviceVendor(dev, &vendor);
209 kr = (*dev)->GetDeviceProduct(dev, &product);
Scott Andersone109d262012-04-20 11:21:14 -0700210 kr = (*dev)->GetLocationID(dev, &locationId);
Yabin Cui71bddf82016-03-25 13:55:07 -0700211 if (kr == KERN_SUCCESS) {
212 devpath = android::base::StringPrintf("usb:%" PRIu32 "X", locationId);
213 if (IsKnownDevice(devpath)) {
214 continue;
215 }
Scott Andersone109d262012-04-20 11:21:14 -0700216 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217 kr = (*dev)->USBGetSerialNumberStringIndex(dev, &serialIndex);
218
Siva Velusamy743883b2015-08-18 17:53:16 -0700219 if (serialIndex > 0) {
220 IOUSBDevRequest req;
221 UInt16 buffer[256];
222 UInt16 languages[128];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
Siva Velusamy743883b2015-08-18 17:53:16 -0700224 memset(languages, 0, sizeof(languages));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800225
Siva Velusamy743883b2015-08-18 17:53:16 -0700226 req.bmRequestType =
227 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
228 req.bRequest = kUSBRqGetDescriptor;
229 req.wValue = (kUSBStringDesc << 8) | 0;
230 req.wIndex = 0;
231 req.pData = languages;
232 req.wLength = sizeof(languages);
233 kr = (*dev)->DeviceRequest(dev, &req);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234
Siva Velusamy743883b2015-08-18 17:53:16 -0700235 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
Guang Zhu1a1f8182009-08-06 17:21:52 -0700236
Siva Velusamy743883b2015-08-18 17:53:16 -0700237 int langCount = (req.wLenDone - 2) / 2, lang;
Guang Zhu1a1f8182009-08-06 17:21:52 -0700238
Siva Velusamy743883b2015-08-18 17:53:16 -0700239 for (lang = 1; lang <= langCount; lang++) {
Guang Zhu1a1f8182009-08-06 17:21:52 -0700240
Siva Velusamy743883b2015-08-18 17:53:16 -0700241 memset(buffer, 0, sizeof(buffer));
242 memset(&req, 0, sizeof(req));
Guang Zhu1a1f8182009-08-06 17:21:52 -0700243
Siva Velusamy743883b2015-08-18 17:53:16 -0700244 req.bmRequestType =
245 USBmakebmRequestType(kUSBIn, kUSBStandard, kUSBDevice);
246 req.bRequest = kUSBRqGetDescriptor;
247 req.wValue = (kUSBStringDesc << 8) | serialIndex;
248 req.wIndex = languages[lang];
249 req.pData = buffer;
250 req.wLength = sizeof(buffer);
251 kr = (*dev)->DeviceRequest(dev, &req);
Guang Zhu1a1f8182009-08-06 17:21:52 -0700252
Siva Velusamy743883b2015-08-18 17:53:16 -0700253 if (kr == kIOReturnSuccess && req.wLenDone > 0) {
254 int i, count;
Guang Zhu1a1f8182009-08-06 17:21:52 -0700255
Siva Velusamy743883b2015-08-18 17:53:16 -0700256 // skip first word, and copy the rest to the serial string,
257 // changing shorts to bytes.
258 count = (req.wLenDone - 1) / 2;
259 for (i = 0; i < count; i++)
260 serial[i] = buffer[i + 1];
261 serial[i] = 0;
262 break;
263 }
264 }
265 }
266 }
267
Dima Zavin3fd82b82009-05-08 18:25:58 -0700268 (*dev)->Release(dev);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269
Yabin Cui71bddf82016-03-25 13:55:07 -0700270 VLOG(USB) << android::base::StringPrintf("Found vid=%04x pid=%04x serial=%s\n",
Siva Velusamy743883b2015-08-18 17:53:16 -0700271 vendor, product, serial);
Yabin Cui71bddf82016-03-25 13:55:07 -0700272 if (devpath.empty()) {
273 devpath = serial;
274 }
275 if (IsKnownDevice(devpath)) {
276 (*iface)->USBInterfaceClose(iface);
Dima Zavin3fd82b82009-05-08 18:25:58 -0700277 (*iface)->Release(iface);
278 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279 }
280
Yabin Cui71bddf82016-03-25 13:55:07 -0700281 std::unique_ptr<usb_handle> handle = CheckInterface((IOUSBInterfaceInterface190**)iface,
282 vendor, product);
283 if (handle == nullptr) {
284 LOG(ERROR) << "Could not find device interface";
285 (*iface)->Release(iface);
286 continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 }
Yabin Cui71bddf82016-03-25 13:55:07 -0700288 handle->devpath = devpath;
289 usb_handle* handle_p = handle.get();
290 VLOG(USB) << "Add usb device " << serial;
291 AddDevice(std::move(handle));
292 register_usb_transport(handle_p, serial, devpath.c_str(), 1);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800293 }
294}
295
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700296// Used to clear both the endpoints before starting.
297// When adb quits, we might clear the host endpoint but not the device.
298// So we make sure both sides are clear before starting up.
299static bool ClearPipeStallBothEnds(IOUSBInterfaceInterface190** interface, UInt8 bulkEp) {
300 IOReturn rc = (*interface)->ClearPipeStallBothEnds(interface, bulkEp);
301 if (rc != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700302 LOG(ERROR) << "Could not clear pipe stall both ends: " << std::hex << rc;
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700303 return false;
304 }
305 return true;
306}
307
Dima Zavin3fd82b82009-05-08 18:25:58 -0700308//* TODO: simplify this further since we only register to get ADB interface
309//* subclass+protocol events
Yabin Cui71bddf82016-03-25 13:55:07 -0700310static std::unique_ptr<usb_handle>
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700311CheckInterface(IOUSBInterfaceInterface190 **interface, UInt16 vendor, UInt16 product)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312{
Yabin Cui71bddf82016-03-25 13:55:07 -0700313 std::unique_ptr<usb_handle> handle;
Elliott Hughes2d4f8522015-08-13 15:01:18 -0700314 IOReturn kr;
315 UInt8 interfaceNumEndpoints, interfaceClass, interfaceSubClass, interfaceProtocol;
316 UInt8 endpoint;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800317
Dima Zavin3fd82b82009-05-08 18:25:58 -0700318 //* Now open the interface. This will cause the pipes associated with
319 //* the endpoints in the interface descriptor to be instantiated
320 kr = (*interface)->USBInterfaceOpen(interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800321 if (kr != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700322 LOG(ERROR) << "Could not open interface: " << std::hex << kr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800323 return NULL;
324 }
325
Dima Zavin3fd82b82009-05-08 18:25:58 -0700326 //* Get the number of endpoints associated with this interface
327 kr = (*interface)->GetNumEndpoints(interface, &interfaceNumEndpoints);
328 if (kr != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700329 LOG(ERROR) << "Unable to get number of endpoints: " << std::hex << kr;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700330 goto err_get_num_ep;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 }
332
Dima Zavin3fd82b82009-05-08 18:25:58 -0700333 //* Get interface class, subclass and protocol
334 if ((*interface)->GetInterfaceClass(interface, &interfaceClass) != kIOReturnSuccess ||
335 (*interface)->GetInterfaceSubClass(interface, &interfaceSubClass) != kIOReturnSuccess ||
336 (*interface)->GetInterfaceProtocol(interface, &interfaceProtocol) != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700337 LOG(ERROR) << "Unable to get interface class, subclass and protocol";
Dima Zavin3fd82b82009-05-08 18:25:58 -0700338 goto err_get_interface_class;
339 }
340
341 //* check to make sure interface class, subclass and protocol match ADB
342 //* avoid opening mass storage endpoints
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700343 if (!is_adb_interface(vendor, product, interfaceClass, interfaceSubClass, interfaceProtocol)) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700344 goto err_bad_adb_interface;
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700345 }
Dima Zavin3fd82b82009-05-08 18:25:58 -0700346
Yabin Cui71bddf82016-03-25 13:55:07 -0700347 handle.reset(new usb_handle);
348 if (handle == nullptr) {
349 goto err_bad_adb_interface;
350 }
Dima Zavin3fd82b82009-05-08 18:25:58 -0700351
352 //* Iterate over the endpoints for this interface and find the first
353 //* bulk in/out pipes available. These will be our read/write pipes.
Elliott Hughes2d4f8522015-08-13 15:01:18 -0700354 for (endpoint = 1; endpoint <= interfaceNumEndpoints; endpoint++) {
Dima Zavin3fd82b82009-05-08 18:25:58 -0700355 UInt8 transferType;
356 UInt16 maxPacketSize;
357 UInt8 interval;
358 UInt8 number;
359 UInt8 direction;
360
361 kr = (*interface)->GetPipeProperties(interface, endpoint, &direction,
362 &number, &transferType, &maxPacketSize, &interval);
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700363 if (kr != kIOReturnSuccess) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700364 LOG(ERROR) << "FindDeviceInterface - could not get pipe properties: "
365 << std::hex << kr;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700366 goto err_get_pipe_props;
367 }
Siva Velusamyd8b48a62015-08-13 08:48:06 -0700368
369 if (kUSBBulk != transferType) continue;
370
371 if (kUSBIn == direction) {
372 handle->bulkIn = endpoint;
373 if (!ClearPipeStallBothEnds(interface, handle->bulkIn)) goto err_get_pipe_props;
374 }
375
376 if (kUSBOut == direction) {
377 handle->bulkOut = endpoint;
378 if (!ClearPipeStallBothEnds(interface, handle->bulkOut)) goto err_get_pipe_props;
379 }
380
381 handle->zero_mask = maxPacketSize - 1;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700382 }
383
384 handle->interface = interface;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385 return handle;
Dima Zavin3fd82b82009-05-08 18:25:58 -0700386
387err_get_pipe_props:
Dima Zavin3fd82b82009-05-08 18:25:58 -0700388err_bad_adb_interface:
389err_get_interface_class:
390err_get_num_ep:
391 (*interface)->USBInterfaceClose(interface);
Yabin Cui71bddf82016-03-25 13:55:07 -0700392 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393}
394
Yabin Cui71bddf82016-03-25 13:55:07 -0700395std::mutex& operate_device_lock = *new std::mutex();
396
Josh Gaod9db09c2016-02-12 14:31:15 -0800397static void RunLoopThread(void* unused) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700398 adb_thread_setname("RunLoop");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399
Yabin Cui71bddf82016-03-25 13:55:07 -0700400 VLOG(USB) << "RunLoopThread started";
401 while (true) {
402 {
403 std::lock_guard<std::mutex> lock_guard(operate_device_lock);
404 FindUSBDevices();
405 KickDisconnectedDevices();
406 }
407 // Signal the parent that we are running
408 usb_inited_flag = true;
409 adb_sleep_ms(1000);
410 }
411 VLOG(USB) << "RunLoopThread done";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
413
Dan Albertc89e0cc2015-05-08 16:13:53 -0700414static void usb_cleanup() {
Yabin Cui71bddf82016-03-25 13:55:07 -0700415 VLOG(USB) << "usb_cleanup";
416 // Wait until usb operations in RunLoopThread finish, and prevent further operations.
417 operate_device_lock.lock();
Dan Albertc89e0cc2015-05-08 16:13:53 -0700418 close_usb_devices();
Dan Albertc89e0cc2015-05-08 16:13:53 -0700419}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700421void usb_init() {
Dan Albertc89e0cc2015-05-08 16:13:53 -0700422 static bool initialized = false;
423 if (!initialized) {
424 atexit(usb_cleanup);
425
Yabin Cui71bddf82016-03-25 13:55:07 -0700426 usb_inited_flag = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800427
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700428 if (!adb_thread_create(RunLoopThread, nullptr)) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700429 fatal_errno("cannot create RunLoop thread");
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700430 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431
432 // Wait for initialization to finish
Yabin Cui71bddf82016-03-25 13:55:07 -0700433 while (!usb_inited_flag) {
434 adb_sleep_ms(100);
435 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436
Dan Albertc89e0cc2015-05-08 16:13:53 -0700437 initialized = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 }
439}
440
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800441int usb_write(usb_handle *handle, const void *buf, int len)
442{
443 IOReturn result;
444
445 if (!len)
446 return 0;
447
Yabin Cui71bddf82016-03-25 13:55:07 -0700448 if (!handle || handle->dead)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800449 return -1;
450
451 if (NULL == handle->interface) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700452 LOG(ERROR) << "usb_write interface was null";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 return -1;
454 }
455
456 if (0 == handle->bulkOut) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700457 LOG(ERROR) << "bulkOut endpoint not assigned";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458 return -1;
459 }
460
461 result =
Siva Velusamy743883b2015-08-18 17:53:16 -0700462 (*handle->interface)->WritePipe(handle->interface, handle->bulkOut, (void *)buf, len);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463
464 if ((result == 0) && (handle->zero_mask)) {
465 /* we need 0-markers and our transfer */
466 if(!(len & handle->zero_mask)) {
467 result =
468 (*handle->interface)->WritePipe(
469 handle->interface, handle->bulkOut, (void *)buf, 0);
470 }
471 }
472
473 if (0 == result)
474 return 0;
475
Siva Velusamy743883b2015-08-18 17:53:16 -0700476 LOG(ERROR) << "usb_write failed with status: " << std::hex << result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477 return -1;
478}
479
480int usb_read(usb_handle *handle, void *buf, int len)
481{
482 IOReturn result;
483 UInt32 numBytes = len;
484
485 if (!len) {
486 return 0;
487 }
488
Yabin Cui71bddf82016-03-25 13:55:07 -0700489 if (!handle || handle->dead) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490 return -1;
491 }
492
493 if (NULL == handle->interface) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700494 LOG(ERROR) << "usb_read interface was null";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 return -1;
496 }
497
498 if (0 == handle->bulkIn) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700499 LOG(ERROR) << "bulkIn endpoint not assigned";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 return -1;
501 }
502
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700503 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700505 if (kIOUSBPipeStalled == result) {
Siva Velusamy743883b2015-08-18 17:53:16 -0700506 LOG(ERROR) << "Pipe stalled, clearing stall.\n";
Esteban de la Canal9dd83dc2014-09-11 11:02:17 -0700507 (*handle->interface)->ClearPipeStall(handle->interface, handle->bulkIn);
508 result = (*handle->interface)->ReadPipe(handle->interface, handle->bulkIn, buf, &numBytes);
509 }
510
511 if (kIOReturnSuccess == result)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800512 return 0;
513 else {
Siva Velusamy743883b2015-08-18 17:53:16 -0700514 LOG(ERROR) << "usb_read failed with status: " << std::hex << result;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515 }
516
517 return -1;
518}
519
520int usb_close(usb_handle *handle)
521{
Yabin Cui71bddf82016-03-25 13:55:07 -0700522 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
523 for (auto it = g_usb_handles.begin(); it != g_usb_handles.end(); ++it) {
524 if ((*it).get() == handle) {
525 g_usb_handles.erase(it);
526 break;
527 }
528 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800529 return 0;
530}
531
Yabin Cui71bddf82016-03-25 13:55:07 -0700532static void usb_kick_locked(usb_handle *handle)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800533{
Siva Velusamy743883b2015-08-18 17:53:16 -0700534 LOG(INFO) << "Kicking handle";
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800535 /* release the interface */
Dima Zavin3fd82b82009-05-08 18:25:58 -0700536 if (!handle)
537 return;
538
Yabin Cui71bddf82016-03-25 13:55:07 -0700539 if (!handle->dead)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800540 {
Yabin Cui71bddf82016-03-25 13:55:07 -0700541 handle->dead = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800542 (*handle->interface)->USBInterfaceClose(handle->interface);
543 (*handle->interface)->Release(handle->interface);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800544 }
545}
Yabin Cui71bddf82016-03-25 13:55:07 -0700546
547void usb_kick(usb_handle *handle) {
548 // Use the lock to avoid multiple thread kicking the device at the same time.
549 std::lock_guard<std::mutex> lock_guard(g_usb_handles_mutex);
550 usb_kick_locked(handle);
551}