blob: 7f926266498a0d324555cb399a7a2c0a4fb8e04a [file] [log] [blame]
The Android Open Source Project9ca14dc2009-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 Cui19bec5b2015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albertdb6fe642015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
Josh Gao6b55e752020-03-27 18:09:56 -070021#include "client/usb.h"
22
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080023#include <ctype.h>
Dan Albertb302d122015-02-24 15:51:19 -080024#include <dirent.h>
25#include <errno.h>
26#include <fcntl.h>
Elliott Hughes3af421c2015-07-23 15:20:09 -070027#include <linux/usb/ch9.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080028#include <linux/usbdevice_fs.h>
29#include <linux/version.h>
Dan Albertb302d122015-02-24 15:51:19 -080030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/ioctl.h>
34#include <sys/time.h>
Ivan Afonichevb955e762018-08-30 01:12:56 +040035#include <sys/sysmacros.h>
Dan Albertb302d122015-02-24 15:51:19 -080036#include <sys/types.h>
37#include <unistd.h>
Elliott Hughes3af421c2015-07-23 15:20:09 -070038
39#include <chrono>
40#include <condition_variable>
41#include <list>
42#include <mutex>
43#include <string>
Josh Gao2d627472018-12-12 16:18:00 -080044#include <string_view>
Elliott Hughes73925982016-11-15 12:37:32 -080045#include <thread>
Elliott Hughes949f2622015-04-27 14:20:17 -070046
Elliott Hughesf55ead92015-12-04 22:00:26 -080047#include <android-base/file.h>
48#include <android-base/stringprintf.h>
49#include <android-base/strings.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080050
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080052#include "transport.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080053
Elliott Hughes73925982016-11-15 12:37:32 -080054using namespace std::chrono_literals;
Elliott Hughes3af421c2015-07-23 15:20:09 -070055using namespace std::literals;
56
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080057/* usb scan debugging is waaaay too verbose */
58#define DBGX(x...)
59
Josh Gaof2d6af52021-04-27 20:32:02 -070060struct usb_handle {
Elliott Hughes3af421c2015-07-23 15:20:09 -070061 ~usb_handle() {
62 if (fd != -1) unix_close(fd);
63 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080064
Elliott Hughes3af421c2015-07-23 15:20:09 -070065 std::string path;
66 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080067 unsigned char ep_in;
68 unsigned char ep_out;
69
Josh Gao3734cf02017-05-02 15:01:09 -070070 size_t max_packet_size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071 unsigned zero_mask;
Elliott Hughes3af421c2015-07-23 15:20:09 -070072 unsigned writeable = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080073
Elliott Hughes3af421c2015-07-23 15:20:09 -070074 usbdevfs_urb urb_in;
75 usbdevfs_urb urb_out;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080076
Elliott Hughes3af421c2015-07-23 15:20:09 -070077 bool urb_in_busy = false;
78 bool urb_out_busy = false;
79 bool dead = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080080
Elliott Hughes3af421c2015-07-23 15:20:09 -070081 std::condition_variable cv;
82 std::mutex mutex;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080083
84 // for garbage collecting disconnected devices
Elliott Hughes3af421c2015-07-23 15:20:09 -070085 bool mark;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080086
87 // ID of thread currently in REAPURB
Elliott Hughes3af421c2015-07-23 15:20:09 -070088 pthread_t reaper_thread = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080089};
90
Josh Gaoe3a87d02015-11-11 17:56:12 -080091static auto& g_usb_handles_mutex = *new std::mutex();
92static auto& g_usb_handles = *new std::list<usb_handle*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080093
Josh Gao2d627472018-12-12 16:18:00 -080094static int is_known_device(std::string_view dev_name) {
Elliott Hughes3af421c2015-07-23 15:20:09 -070095 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
96 for (usb_handle* usb : g_usb_handles) {
97 if (usb->path == dev_name) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080098 // set mark flag to indicate this device is still alive
Elliott Hughes3af421c2015-07-23 15:20:09 -070099 usb->mark = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800100 return 1;
101 }
102 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800103 return 0;
104}
105
Elliott Hughes3af421c2015-07-23 15:20:09 -0700106static void kick_disconnected_devices() {
107 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800108 // kick any devices in the device list that were not found in the device scan
Elliott Hughes3af421c2015-07-23 15:20:09 -0700109 for (usb_handle* usb : g_usb_handles) {
110 if (!usb->mark) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800111 usb_kick(usb);
112 } else {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700113 usb->mark = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800114 }
115 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800116}
117
Elliott Hughes3af421c2015-07-23 15:20:09 -0700118static inline bool contains_non_digit(const char* name) {
119 while (*name) {
120 if (!isdigit(*name++)) return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800121 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700122 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800123}
124
Elliott Hughes3af421c2015-07-23 15:20:09 -0700125static void find_usb_device(const std::string& base,
Josh Gao3734cf02017-05-02 15:01:09 -0700126 void (*register_device_callback)(const char*, const char*,
127 unsigned char, unsigned char, int, int,
128 unsigned, size_t)) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700129 std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
130 if (!bus_dir) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800131
Elliott Hughes3af421c2015-07-23 15:20:09 -0700132 dirent* de;
Yi Kong86e67182018-07-13 18:15:16 -0700133 while ((de = readdir(bus_dir.get())) != nullptr) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700134 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135
Elliott Hughes3af421c2015-07-23 15:20:09 -0700136 std::string bus_name = base + "/" + de->d_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800137
Elliott Hughes3af421c2015-07-23 15:20:09 -0700138 std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
139 if (!dev_dir) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800140
Elliott Hughes3af421c2015-07-23 15:20:09 -0700141 while ((de = readdir(dev_dir.get()))) {
Mike Lockwoodf1667712011-01-07 23:18:14 -0500142 unsigned char devdesc[4096];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143 unsigned char* bufptr = devdesc;
Mike Lockwood31175d62010-01-17 00:52:27 -0500144 unsigned char* bufend;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800145 struct usb_device_descriptor* device;
146 struct usb_config_descriptor* config;
147 struct usb_interface_descriptor* interface;
148 struct usb_endpoint_descriptor *ep1, *ep2;
149 unsigned zero_mask = 0;
Josh Gao3734cf02017-05-02 15:01:09 -0700150 size_t max_packet_size = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800151
Elliott Hughes3af421c2015-07-23 15:20:09 -0700152 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800153
Elliott Hughes3af421c2015-07-23 15:20:09 -0700154 std::string dev_name = bus_name + "/" + de->d_name;
Josh Gao2d627472018-12-12 16:18:00 -0800155 if (is_known_device(dev_name)) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800156 continue;
157 }
158
Josh Gaof0c44032018-12-12 16:12:28 -0800159 int fd = unix_open(dev_name, O_RDONLY | O_CLOEXEC);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700160 if (fd == -1) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800161 continue;
162 }
163
Elliott Hughes3af421c2015-07-23 15:20:09 -0700164 size_t desclength = unix_read(fd, devdesc, sizeof(devdesc));
Mike Lockwood31175d62010-01-17 00:52:27 -0500165 bufend = bufptr + desclength;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800166
167 // should have device and configuration descriptors, and atleast two endpoints
168 if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700169 D("desclength %zu is too small", desclength);
Spencer Low3a2421b2015-05-22 20:09:06 -0700170 unix_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800171 continue;
172 }
173
174 device = (struct usb_device_descriptor*)bufptr;
175 bufptr += USB_DT_DEVICE_SIZE;
176
177 if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
Spencer Low3a2421b2015-05-22 20:09:06 -0700178 unix_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800179 continue;
180 }
181
Yi Kong867c71e2021-09-13 14:34:25 +0800182 DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), device->idVendor,
183 device->idProduct);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184
Yi Kong867c71e2021-09-13 14:34:25 +0800185 // should have config descriptor next
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800186 config = (struct usb_config_descriptor *)bufptr;
187 bufptr += USB_DT_CONFIG_SIZE;
188 if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
Yabin Cui815ad882015-09-02 17:44:28 -0700189 D("usb_config_descriptor not found");
Spencer Low3a2421b2015-05-22 20:09:06 -0700190 unix_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800191 continue;
192 }
193
Mike Lockwood31175d62010-01-17 00:52:27 -0500194 // loop through all the descriptors and look for the ADB interface
195 while (bufptr < bufend) {
196 unsigned char length = bufptr[0];
197 unsigned char type = bufptr[1];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800198
Mike Lockwood31175d62010-01-17 00:52:27 -0500199 if (type == USB_DT_INTERFACE) {
200 interface = (struct usb_interface_descriptor *)bufptr;
201 bufptr += length;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800202
Mike Lockwood31175d62010-01-17 00:52:27 -0500203 if (length != USB_DT_INTERFACE_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700204 D("interface descriptor has wrong size");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800205 break;
206 }
207
Mike Lockwood31175d62010-01-17 00:52:27 -0500208 DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
209 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
210 interface->bInterfaceClass, interface->bInterfaceSubClass,
211 interface->bInterfaceProtocol, interface->bNumEndpoints);
212
213 if (interface->bNumEndpoints == 2 &&
Josh Gao08dda212016-09-26 21:18:58 -0700214 is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass,
215 interface->bInterfaceProtocol)) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700216 struct stat st;
217 char pathbuf[128];
218 char link[256];
Elliott Hughes3af421c2015-07-23 15:20:09 -0700219 char *devpath = nullptr;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700220
Mike Lockwood31175d62010-01-17 00:52:27 -0500221 DBGX("looking for bulk endpoints\n");
222 // looks like ADB...
223 ep1 = (struct usb_endpoint_descriptor *)bufptr;
224 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff5b276632014-05-16 21:51:41 +0200225 // For USB 3.0 SuperSpeed devices, skip potential
226 // USB 3.0 SuperSpeed Endpoint Companion descriptor
227 if (bufptr+2 <= devdesc + desclength &&
228 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
229 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
230 bufptr += USB_DT_SS_EP_COMP_SIZE;
231 }
Mike Lockwood31175d62010-01-17 00:52:27 -0500232 ep2 = (struct usb_endpoint_descriptor *)bufptr;
233 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff5b276632014-05-16 21:51:41 +0200234 if (bufptr+2 <= devdesc + desclength &&
235 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
236 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
237 bufptr += USB_DT_SS_EP_COMP_SIZE;
238 }
Mike Lockwood31175d62010-01-17 00:52:27 -0500239
240 if (bufptr > devdesc + desclength ||
241 ep1->bLength != USB_DT_ENDPOINT_SIZE ||
242 ep1->bDescriptorType != USB_DT_ENDPOINT ||
243 ep2->bLength != USB_DT_ENDPOINT_SIZE ||
244 ep2->bDescriptorType != USB_DT_ENDPOINT) {
Yabin Cui815ad882015-09-02 17:44:28 -0700245 D("endpoints not found");
Mike Lockwood31175d62010-01-17 00:52:27 -0500246 break;
247 }
248
249 // both endpoints should be bulk
250 if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
251 ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
Yabin Cui815ad882015-09-02 17:44:28 -0700252 D("bulk endpoints not found");
Mike Lockwood31175d62010-01-17 00:52:27 -0500253 continue;
254 }
255 /* aproto 01 needs 0 termination */
Mark Salyzyn21a991d2017-10-04 15:05:40 -0700256 if (interface->bInterfaceProtocol == ADB_PROTOCOL) {
Josh Gao3734cf02017-05-02 15:01:09 -0700257 max_packet_size = ep1->wMaxPacketSize;
Mike Lockwood31175d62010-01-17 00:52:27 -0500258 zero_mask = ep1->wMaxPacketSize - 1;
259 }
260
261 // we have a match. now we just need to figure out which is in and which is out.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700262 unsigned char local_ep_in, local_ep_out;
Mike Lockwood31175d62010-01-17 00:52:27 -0500263 if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
264 local_ep_in = ep1->bEndpointAddress;
265 local_ep_out = ep2->bEndpointAddress;
266 } else {
267 local_ep_in = ep2->bEndpointAddress;
268 local_ep_out = ep1->bEndpointAddress;
269 }
270
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700271 // Determine the device path
272 if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700273 snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
274 major(st.st_rdev), minor(st.st_rdev));
Elliott Hughes24f9b082015-07-27 21:21:39 -0700275 ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700276 if (link_len > 0) {
277 link[link_len] = '\0';
Elliott Hughes24f9b082015-07-27 21:21:39 -0700278 const char* slash = strrchr(link, '/');
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700279 if (slash) {
280 snprintf(pathbuf, sizeof(pathbuf),
281 "usb:%s", slash + 1);
282 devpath = pathbuf;
283 }
284 }
285 }
286
Josh Gao3734cf02017-05-02 15:01:09 -0700287 register_device_callback(dev_name.c_str(), devpath, local_ep_in,
288 local_ep_out, interface->bInterfaceNumber,
289 device->iSerialNumber, zero_mask, max_packet_size);
Mike Lockwood31175d62010-01-17 00:52:27 -0500290 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800291 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292 } else {
Mike Lockwood31175d62010-01-17 00:52:27 -0500293 bufptr += length;
294 }
295 } // end of while
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800296
Spencer Low3a2421b2015-05-22 20:09:06 -0700297 unix_close(fd);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700298 }
299 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800300}
301
Elliott Hughes3af421c2015-07-23 15:20:09 -0700302static int usb_bulk_write(usb_handle* h, const void* data, int len) {
303 std::unique_lock<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700304 D("++ usb_bulk_write ++");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800305
Elliott Hughes3af421c2015-07-23 15:20:09 -0700306 usbdevfs_urb* urb = &h->urb_out;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800307 memset(urb, 0, sizeof(*urb));
308 urb->type = USBDEVFS_URB_TYPE_BULK;
309 urb->endpoint = h->ep_out;
310 urb->status = -1;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700311 urb->buffer = const_cast<void*>(data);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800312 urb->buffer_length = len;
313
Elliott Hughes3af421c2015-07-23 15:20:09 -0700314 if (h->dead) {
315 errno = EINVAL;
316 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800317 }
318
Elliott Hughes3af421c2015-07-23 15:20:09 -0700319 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
320 return -1;
321 }
322
323 h->urb_out_busy = true;
324 while (true) {
Josh Gao835d90e2019-07-18 14:46:53 -0700325 auto now = std::chrono::steady_clock::now();
Elliott Hughes3af421c2015-07-23 15:20:09 -0700326 if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) {
327 // TODO: call USBDEVFS_DISCARDURB?
328 errno = ETIMEDOUT;
329 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800330 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700331 if (!h->urb_out_busy) {
332 if (urb->status != 0) {
333 errno = -urb->status;
334 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800335 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700336 return urb->actual_length;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800337 }
338 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800339}
340
Elliott Hughes3af421c2015-07-23 15:20:09 -0700341static int usb_bulk_read(usb_handle* h, void* data, int len) {
342 std::unique_lock<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700343 D("++ usb_bulk_read ++");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700344
345 usbdevfs_urb* urb = &h->urb_in;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800346 memset(urb, 0, sizeof(*urb));
347 urb->type = USBDEVFS_URB_TYPE_BULK;
348 urb->endpoint = h->ep_in;
349 urb->status = -1;
350 urb->buffer = data;
351 urb->buffer_length = len;
352
Elliott Hughes3af421c2015-07-23 15:20:09 -0700353 if (h->dead) {
354 errno = EINVAL;
355 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800356 }
357
Elliott Hughes3af421c2015-07-23 15:20:09 -0700358 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
359 return -1;
360 }
361
362 h->urb_in_busy = true;
363 while (true) {
Yabin Cui815ad882015-09-02 17:44:28 -0700364 D("[ reap urb - wait ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800365 h->reaper_thread = pthread_self();
Elliott Hughes3af421c2015-07-23 15:20:09 -0700366 int fd = h->fd;
367 lock.unlock();
368
369 // This ioctl must not have TEMP_FAILURE_RETRY because we send SIGALRM to break out.
370 usbdevfs_urb* out = nullptr;
371 int res = ioctl(fd, USBDEVFS_REAPURB, &out);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700372 int saved_errno = errno;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700373
374 lock.lock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800375 h->reaper_thread = 0;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700376 if (h->dead) {
377 errno = EINVAL;
378 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800379 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700380 if (res < 0) {
381 if (saved_errno == EINTR) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800382 continue;
383 }
Yabin Cui815ad882015-09-02 17:44:28 -0700384 D("[ reap urb - error ]");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700385 errno = saved_errno;
386 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800387 }
Yabin Cui815ad882015-09-02 17:44:28 -0700388 D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800389
Elliott Hughes3af421c2015-07-23 15:20:09 -0700390 if (out == &h->urb_in) {
Yabin Cui815ad882015-09-02 17:44:28 -0700391 D("[ reap urb - IN complete ]");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700392 h->urb_in_busy = false;
393 if (urb->status != 0) {
394 errno = -urb->status;
395 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800396 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700397 return urb->actual_length;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800398 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700399 if (out == &h->urb_out) {
Yabin Cui815ad882015-09-02 17:44:28 -0700400 D("[ reap urb - OUT compelete ]");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700401 h->urb_out_busy = false;
402 h->cv.notify_all();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800403 }
404 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800405}
406
Josh Gao949e0e62019-09-12 00:03:42 +0800407static int usb_write_split(usb_handle* h, unsigned char* data, int len) {
408 for (int i = 0; i < len; i += 16384) {
409 int chunk_size = (i + 16384 > len) ? len - i : 16384;
410 int n = usb_bulk_write(h, data + i, chunk_size);
411 if (n != chunk_size) {
412 D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno));
413 return -1;
414 }
415 }
416
417 return len;
418}
419
420int usb_write(usb_handle* h, const void* _data, int len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700421 D("++ usb_write ++");
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100422
Josh Gao949e0e62019-09-12 00:03:42 +0800423 unsigned char* data = (unsigned char*)_data;
424
425 // The kernel will attempt to allocate a contiguous buffer for each write we submit.
426 // This might fail due to heap fragmentation, so attempt a contiguous write once, and if that
427 // fails, retry after having split the data into 16kB chunks to avoid allocation failure.
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100428 int n = usb_bulk_write(h, data, len);
Josh Gao949e0e62019-09-12 00:03:42 +0800429 if (n == -1 && errno == ENOMEM) {
430 n = usb_write_split(h, data, len);
431 }
432
433 if (n == -1) {
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100434 return -1;
435 }
436
Elliott Hughes3af421c2015-07-23 15:20:09 -0700437 if (h->zero_mask && !(len & h->zero_mask)) {
438 // If we need 0-markers and our transfer is an even multiple of the packet size,
439 // then send a zero marker.
Josh Gao949e0e62019-09-12 00:03:42 +0800440 return usb_bulk_write(h, _data, 0) == 0 ? len : -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800441 }
442
Yabin Cui815ad882015-09-02 17:44:28 -0700443 D("-- usb_write --");
Josh Gao949e0e62019-09-12 00:03:42 +0800444 return len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800445}
446
447int usb_read(usb_handle *h, void *_data, int len)
448{
449 unsigned char *data = (unsigned char*) _data;
450 int n;
451
Yabin Cui815ad882015-09-02 17:44:28 -0700452 D("++ usb_read ++");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800453 int orig_len = len;
454 while (len == orig_len) {
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100455 int xfer = len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800456
Yabin Cui815ad882015-09-02 17:44:28 -0700457 D("[ usb read %d fd = %d], path=%s", xfer, h->fd, h->path.c_str());
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458 n = usb_bulk_read(h, data, xfer);
Yabin Cui815ad882015-09-02 17:44:28 -0700459 D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str());
Yabin Cui3cf1b362017-03-10 16:01:01 -0800460 if (n <= 0) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700461 if((errno == ETIMEDOUT) && (h->fd != -1)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700462 D("[ timeout ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800463 continue;
464 }
Yabin Cui815ad882015-09-02 17:44:28 -0700465 D("ERROR: n = %d, errno = %d (%s)",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800466 n, errno, strerror(errno));
467 return -1;
468 }
469
Yabin Cui3cf1b362017-03-10 16:01:01 -0800470 len -= n;
471 data += n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800472 }
473
Yabin Cui815ad882015-09-02 17:44:28 -0700474 D("-- usb_read --");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800475 return orig_len - len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800476}
477
Josh Gao3a2172b2019-03-28 15:47:44 -0700478void usb_reset(usb_handle* h) {
479 ioctl(h->fd, USBDEVFS_RESET);
480 usb_kick(h);
481}
482
Elliott Hughes3af421c2015-07-23 15:20:09 -0700483void usb_kick(usb_handle* h) {
484 std::lock_guard<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700485 D("[ kicking %p (fd = %d) ]", h, h->fd);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700486 if (!h->dead) {
487 h->dead = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800488
Mike Lockwoode45583f2009-08-08 12:37:44 -0400489 if (h->writeable) {
490 /* HACK ALERT!
491 ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
492 ** This is a workaround for that problem.
493 */
494 if (h->reaper_thread) {
495 pthread_kill(h->reaper_thread, SIGALRM);
496 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800497
Mike Lockwoode45583f2009-08-08 12:37:44 -0400498 /* cancel any pending transactions
499 ** these will quietly fail if the txns are not active,
500 ** but this ensures that a reader blocked on REAPURB
501 ** will get unblocked
502 */
Elliott Hughes3af421c2015-07-23 15:20:09 -0700503 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
504 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400505 h->urb_in.status = -ENODEV;
506 h->urb_out.status = -ENODEV;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700507 h->urb_in_busy = false;
508 h->urb_out_busy = false;
509 h->cv.notify_all();
Mike Lockwoode45583f2009-08-08 12:37:44 -0400510 } else {
511 unregister_usb_transport(h);
512 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800513 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800514}
515
Elliott Hughes3af421c2015-07-23 15:20:09 -0700516int usb_close(usb_handle* h) {
517 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
518 g_usb_handles.remove(h);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800519
Yabin Cui815ad882015-09-02 17:44:28 -0700520 D("-- usb close %p (fd = %d) --", h, h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800521
Elliott Hughes3af421c2015-07-23 15:20:09 -0700522 delete h;
523
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800524 return 0;
525}
526
Josh Gao3734cf02017-05-02 15:01:09 -0700527size_t usb_get_max_packet_size(usb_handle* h) {
528 return h->max_packet_size;
529}
530
531static void register_device(const char* dev_name, const char* dev_path, unsigned char ep_in,
532 unsigned char ep_out, int interface, int serial_index,
533 unsigned zero_mask, size_t max_packet_size) {
Vince Harron5703eb32021-11-12 12:40:58 -0800534 // Read the device's serial number.
535 std::string serial_path =
536 android::base::StringPrintf("/sys/bus/usb/devices/%s/serial", dev_path + 4);
537 std::string serial;
538 if (!android::base::ReadFileToString(serial_path, &serial)) {
539 D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno));
540 // We don't actually want to treat an unknown serial as an error because
541 // devices aren't able to communicate a serial number in early bringup.
542 // http://b/20883914
543 serial = "";
544 }
545 serial = android::base::Trim(serial);
546
547 if (!transport_server_owns_device(dev_path, serial)) {
548 // We aren't allowed to communicate with this device. Don't open this device.
549 return;
550 }
551
Dan Alberta8c34142015-05-06 16:48:52 -0700552 // Since Linux will not reassign the device ID (and dev_name) as long as the
553 // device is open, we can add to the list here once we open it and remove
554 // from the list when we're finally closed and everything will work out
555 // fine.
556 //
Elliott Hughes3af421c2015-07-23 15:20:09 -0700557 // If we have a usb_handle on the list of handles with a matching name, we
Dan Alberta8c34142015-05-06 16:48:52 -0700558 // have no further work to do.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700559 {
560 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
561 for (usb_handle* usb: g_usb_handles) {
562 if (usb->path == dev_name) {
563 return;
564 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800565 }
566 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800567
Yabin Cui815ad882015-09-02 17:44:28 -0700568 D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700569 std::unique_ptr<usb_handle> usb(new usb_handle);
570 usb->path = dev_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800571 usb->ep_in = ep_in;
572 usb->ep_out = ep_out;
573 usb->zero_mask = zero_mask;
Josh Gao3734cf02017-05-02 15:01:09 -0700574 usb->max_packet_size = max_packet_size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800575
Elliott Hughes3af421c2015-07-23 15:20:09 -0700576 // Initialize mark so we don't get garbage collected after the device scan.
577 usb->mark = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800578
Josh Gaof0c44032018-12-12 16:12:28 -0800579 usb->fd = unix_open(usb->path, O_RDWR | O_CLOEXEC);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700580 if (usb->fd == -1) {
Elliott Hughes5d504372015-04-25 14:44:23 -0700581 // Opening RW failed, so see if we have RO access.
Josh Gaof0c44032018-12-12 16:12:28 -0800582 usb->fd = unix_open(usb->path, O_RDONLY | O_CLOEXEC);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700583 if (usb->fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700584 D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno));
Elliott Hughes5d504372015-04-25 14:44:23 -0700585 return;
586 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400587 usb->writeable = 0;
Elliott Hughes5d504372015-04-25 14:44:23 -0700588 }
589
Yabin Cui815ad882015-09-02 17:44:28 -0700590 D("[ usb opened %s%s, fd=%d]",
Elliott Hughes3af421c2015-07-23 15:20:09 -0700591 usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd);
Elliott Hughes5d504372015-04-25 14:44:23 -0700592
593 if (usb->writeable) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700594 if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700595 D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno));
Elliott Hughes5d504372015-04-25 14:44:23 -0700596 return;
597 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400598 }
599
Dan Alberta8c34142015-05-06 16:48:52 -0700600 // Add to the end of the active handles.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700601 usb_handle* done_usb = usb.release();
602 {
603 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
604 g_usb_handles.push_back(done_usb);
605 }
606 register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800607}
608
Josh Gao0f3312a2017-04-12 17:00:49 -0700609static void device_poll_thread() {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700610 adb_thread_setname("device poll");
Yabin Cui815ad882015-09-02 17:44:28 -0700611 D("Created device thread");
Dan Alberta8c34142015-05-06 16:48:52 -0700612 while (true) {
613 // TODO: Use inotify.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800614 find_usb_device("/dev/bus/usb", register_device);
Daniel Colascione2155f3f2019-11-04 11:33:58 -0800615 adb_notify_device_scan_complete();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800616 kick_disconnected_devices();
Elliott Hughes73925982016-11-15 12:37:32 -0800617 std::this_thread::sleep_for(1s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800618 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800619}
620
Elliott Hughes3af421c2015-07-23 15:20:09 -0700621void usb_init() {
622 struct sigaction actions;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800623 memset(&actions, 0, sizeof(actions));
624 sigemptyset(&actions.sa_mask);
625 actions.sa_flags = 0;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700626 actions.sa_handler = [](int) {};
627 sigaction(SIGALRM, &actions, nullptr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800628
Josh Gao0f3312a2017-04-12 17:00:49 -0700629 std::thread(device_poll_thread).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800630}
Josh Gao90b62532017-05-10 13:51:36 -0700631
632void usb_cleanup() {}