blob: 6b6e3e3f262c9e25bdc13aa9cc3ef28566c5803b [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
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080021#include <ctype.h>
Dan Albertb302d122015-02-24 15:51:19 -080022#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
Elliott Hughes3af421c2015-07-23 15:20:09 -070025#include <linux/usb/ch9.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080026#include <linux/usbdevice_fs.h>
27#include <linux/version.h>
Dan Albertb302d122015-02-24 15:51:19 -080028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/ioctl.h>
32#include <sys/time.h>
Ivan Afonichevb955e762018-08-30 01:12:56 +040033#include <sys/sysmacros.h>
Dan Albertb302d122015-02-24 15:51:19 -080034#include <sys/types.h>
35#include <unistd.h>
Elliott Hughes3af421c2015-07-23 15:20:09 -070036
37#include <chrono>
38#include <condition_variable>
39#include <list>
40#include <mutex>
41#include <string>
Elliott Hughes73925982016-11-15 12:37:32 -080042#include <thread>
Elliott Hughes949f2622015-04-27 14:20:17 -070043
Elliott Hughesf55ead92015-12-04 22:00:26 -080044#include <android-base/file.h>
45#include <android-base/stringprintf.h>
46#include <android-base/strings.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080048#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080049#include "transport.h"
Josh Gaob7366922016-09-28 12:32:45 -070050#include "usb.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080051
Elliott Hughes73925982016-11-15 12:37:32 -080052using namespace std::chrono_literals;
Elliott Hughes3af421c2015-07-23 15:20:09 -070053using namespace std::literals;
54
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080055/* usb scan debugging is waaaay too verbose */
56#define DBGX(x...)
57
Josh Gaob7366922016-09-28 12:32:45 -070058namespace native {
59struct usb_handle : public ::usb_handle {
Elliott Hughes3af421c2015-07-23 15:20:09 -070060 ~usb_handle() {
61 if (fd != -1) unix_close(fd);
62 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080063
Elliott Hughes3af421c2015-07-23 15:20:09 -070064 std::string path;
65 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080066 unsigned char ep_in;
67 unsigned char ep_out;
68
Josh Gao3734cf02017-05-02 15:01:09 -070069 size_t max_packet_size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080070 unsigned zero_mask;
Elliott Hughes3af421c2015-07-23 15:20:09 -070071 unsigned writeable = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080072
Elliott Hughes3af421c2015-07-23 15:20:09 -070073 usbdevfs_urb urb_in;
74 usbdevfs_urb urb_out;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080075
Elliott Hughes3af421c2015-07-23 15:20:09 -070076 bool urb_in_busy = false;
77 bool urb_out_busy = false;
78 bool dead = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080079
Elliott Hughes3af421c2015-07-23 15:20:09 -070080 std::condition_variable cv;
81 std::mutex mutex;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080082
83 // for garbage collecting disconnected devices
Elliott Hughes3af421c2015-07-23 15:20:09 -070084 bool mark;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080085
86 // ID of thread currently in REAPURB
Elliott Hughes3af421c2015-07-23 15:20:09 -070087 pthread_t reaper_thread = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080088};
89
Josh Gaoe3a87d02015-11-11 17:56:12 -080090static auto& g_usb_handles_mutex = *new std::mutex();
91static auto& g_usb_handles = *new std::list<usb_handle*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080092
Elliott Hughes3af421c2015-07-23 15:20:09 -070093static int is_known_device(const char* dev_name) {
94 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
95 for (usb_handle* usb : g_usb_handles) {
96 if (usb->path == dev_name) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080097 // set mark flag to indicate this device is still alive
Elliott Hughes3af421c2015-07-23 15:20:09 -070098 usb->mark = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080099 return 1;
100 }
101 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800102 return 0;
103}
104
Elliott Hughes3af421c2015-07-23 15:20:09 -0700105static void kick_disconnected_devices() {
106 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800107 // kick any devices in the device list that were not found in the device scan
Elliott Hughes3af421c2015-07-23 15:20:09 -0700108 for (usb_handle* usb : g_usb_handles) {
109 if (!usb->mark) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800110 usb_kick(usb);
111 } else {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700112 usb->mark = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800113 }
114 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800115}
116
Elliott Hughes3af421c2015-07-23 15:20:09 -0700117static inline bool contains_non_digit(const char* name) {
118 while (*name) {
119 if (!isdigit(*name++)) return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800120 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700121 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800122}
123
Elliott Hughes3af421c2015-07-23 15:20:09 -0700124static void find_usb_device(const std::string& base,
Josh Gao3734cf02017-05-02 15:01:09 -0700125 void (*register_device_callback)(const char*, const char*,
126 unsigned char, unsigned char, int, int,
127 unsigned, size_t)) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700128 std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
129 if (!bus_dir) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800130
Elliott Hughes3af421c2015-07-23 15:20:09 -0700131 dirent* de;
Yi Kong86e67182018-07-13 18:15:16 -0700132 while ((de = readdir(bus_dir.get())) != nullptr) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700133 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800134
Elliott Hughes3af421c2015-07-23 15:20:09 -0700135 std::string bus_name = base + "/" + de->d_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800136
Elliott Hughes3af421c2015-07-23 15:20:09 -0700137 std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
138 if (!dev_dir) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800139
Elliott Hughes3af421c2015-07-23 15:20:09 -0700140 while ((de = readdir(dev_dir.get()))) {
Mike Lockwoodf1667712011-01-07 23:18:14 -0500141 unsigned char devdesc[4096];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800142 unsigned char* bufptr = devdesc;
Mike Lockwood31175d62010-01-17 00:52:27 -0500143 unsigned char* bufend;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800144 struct usb_device_descriptor* device;
145 struct usb_config_descriptor* config;
146 struct usb_interface_descriptor* interface;
147 struct usb_endpoint_descriptor *ep1, *ep2;
148 unsigned zero_mask = 0;
Josh Gao3734cf02017-05-02 15:01:09 -0700149 size_t max_packet_size = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150 unsigned vid, pid;
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;
155 if (is_known_device(dev_name.c_str())) {
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
Marcus Comstedt3d2f5182010-09-22 20:09:44 +0200182 vid = device->idVendor;
183 pid = device->idProduct;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700184 DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800185
186 // should have config descriptor next
187 config = (struct usb_config_descriptor *)bufptr;
188 bufptr += USB_DT_CONFIG_SIZE;
189 if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
Yabin Cui815ad882015-09-02 17:44:28 -0700190 D("usb_config_descriptor not found");
Spencer Low3a2421b2015-05-22 20:09:06 -0700191 unix_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800192 continue;
193 }
194
Mike Lockwood31175d62010-01-17 00:52:27 -0500195 // loop through all the descriptors and look for the ADB interface
196 while (bufptr < bufend) {
197 unsigned char length = bufptr[0];
198 unsigned char type = bufptr[1];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800199
Mike Lockwood31175d62010-01-17 00:52:27 -0500200 if (type == USB_DT_INTERFACE) {
201 interface = (struct usb_interface_descriptor *)bufptr;
202 bufptr += length;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800203
Mike Lockwood31175d62010-01-17 00:52:27 -0500204 if (length != USB_DT_INTERFACE_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700205 D("interface descriptor has wrong size");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800206 break;
207 }
208
Mike Lockwood31175d62010-01-17 00:52:27 -0500209 DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
210 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
211 interface->bInterfaceClass, interface->bInterfaceSubClass,
212 interface->bInterfaceProtocol, interface->bNumEndpoints);
213
214 if (interface->bNumEndpoints == 2 &&
Josh Gao08dda212016-09-26 21:18:58 -0700215 is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass,
216 interface->bInterfaceProtocol)) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700217 struct stat st;
218 char pathbuf[128];
219 char link[256];
Elliott Hughes3af421c2015-07-23 15:20:09 -0700220 char *devpath = nullptr;
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700221
Mike Lockwood31175d62010-01-17 00:52:27 -0500222 DBGX("looking for bulk endpoints\n");
223 // looks like ADB...
224 ep1 = (struct usb_endpoint_descriptor *)bufptr;
225 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff5b276632014-05-16 21:51:41 +0200226 // For USB 3.0 SuperSpeed devices, skip potential
227 // USB 3.0 SuperSpeed Endpoint Companion descriptor
228 if (bufptr+2 <= devdesc + desclength &&
229 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
230 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
231 bufptr += USB_DT_SS_EP_COMP_SIZE;
232 }
Mike Lockwood31175d62010-01-17 00:52:27 -0500233 ep2 = (struct usb_endpoint_descriptor *)bufptr;
234 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff5b276632014-05-16 21:51:41 +0200235 if (bufptr+2 <= devdesc + desclength &&
236 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
237 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
238 bufptr += USB_DT_SS_EP_COMP_SIZE;
239 }
Mike Lockwood31175d62010-01-17 00:52:27 -0500240
241 if (bufptr > devdesc + desclength ||
242 ep1->bLength != USB_DT_ENDPOINT_SIZE ||
243 ep1->bDescriptorType != USB_DT_ENDPOINT ||
244 ep2->bLength != USB_DT_ENDPOINT_SIZE ||
245 ep2->bDescriptorType != USB_DT_ENDPOINT) {
Yabin Cui815ad882015-09-02 17:44:28 -0700246 D("endpoints not found");
Mike Lockwood31175d62010-01-17 00:52:27 -0500247 break;
248 }
249
250 // both endpoints should be bulk
251 if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
252 ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
Yabin Cui815ad882015-09-02 17:44:28 -0700253 D("bulk endpoints not found");
Mike Lockwood31175d62010-01-17 00:52:27 -0500254 continue;
255 }
256 /* aproto 01 needs 0 termination */
Mark Salyzyn21a991d2017-10-04 15:05:40 -0700257 if (interface->bInterfaceProtocol == ADB_PROTOCOL) {
Josh Gao3734cf02017-05-02 15:01:09 -0700258 max_packet_size = ep1->wMaxPacketSize;
Mike Lockwood31175d62010-01-17 00:52:27 -0500259 zero_mask = ep1->wMaxPacketSize - 1;
260 }
261
262 // 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 -0700263 unsigned char local_ep_in, local_ep_out;
Mike Lockwood31175d62010-01-17 00:52:27 -0500264 if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
265 local_ep_in = ep1->bEndpointAddress;
266 local_ep_out = ep2->bEndpointAddress;
267 } else {
268 local_ep_in = ep2->bEndpointAddress;
269 local_ep_out = ep1->bEndpointAddress;
270 }
271
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700272 // Determine the device path
273 if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700274 snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
275 major(st.st_rdev), minor(st.st_rdev));
Elliott Hughes24f9b082015-07-27 21:21:39 -0700276 ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1);
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700277 if (link_len > 0) {
278 link[link_len] = '\0';
Elliott Hughes24f9b082015-07-27 21:21:39 -0700279 const char* slash = strrchr(link, '/');
Scott Anderson6dfaf4b2012-04-20 11:21:14 -0700280 if (slash) {
281 snprintf(pathbuf, sizeof(pathbuf),
282 "usb:%s", slash + 1);
283 devpath = pathbuf;
284 }
285 }
286 }
287
Josh Gao3734cf02017-05-02 15:01:09 -0700288 register_device_callback(dev_name.c_str(), devpath, local_ep_in,
289 local_ep_out, interface->bInterfaceNumber,
290 device->iSerialNumber, zero_mask, max_packet_size);
Mike Lockwood31175d62010-01-17 00:52:27 -0500291 break;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800292 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800293 } else {
Mike Lockwood31175d62010-01-17 00:52:27 -0500294 bufptr += length;
295 }
296 } // end of while
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800297
Spencer Low3a2421b2015-05-22 20:09:06 -0700298 unix_close(fd);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700299 }
300 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800301}
302
Elliott Hughes3af421c2015-07-23 15:20:09 -0700303static int usb_bulk_write(usb_handle* h, const void* data, int len) {
304 std::unique_lock<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700305 D("++ usb_bulk_write ++");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800306
Elliott Hughes3af421c2015-07-23 15:20:09 -0700307 usbdevfs_urb* urb = &h->urb_out;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800308 memset(urb, 0, sizeof(*urb));
309 urb->type = USBDEVFS_URB_TYPE_BULK;
310 urb->endpoint = h->ep_out;
311 urb->status = -1;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700312 urb->buffer = const_cast<void*>(data);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800313 urb->buffer_length = len;
314
Elliott Hughes3af421c2015-07-23 15:20:09 -0700315 if (h->dead) {
316 errno = EINVAL;
317 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800318 }
319
Elliott Hughes3af421c2015-07-23 15:20:09 -0700320 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
321 return -1;
322 }
323
324 h->urb_out_busy = true;
325 while (true) {
326 auto now = std::chrono::system_clock::now();
327 if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) {
328 // TODO: call USBDEVFS_DISCARDURB?
329 errno = ETIMEDOUT;
330 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800331 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700332 if (!h->urb_out_busy) {
333 if (urb->status != 0) {
334 errno = -urb->status;
335 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800336 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700337 return urb->actual_length;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800338 }
339 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800340}
341
Elliott Hughes3af421c2015-07-23 15:20:09 -0700342static int usb_bulk_read(usb_handle* h, void* data, int len) {
343 std::unique_lock<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700344 D("++ usb_bulk_read ++");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700345
346 usbdevfs_urb* urb = &h->urb_in;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800347 memset(urb, 0, sizeof(*urb));
348 urb->type = USBDEVFS_URB_TYPE_BULK;
349 urb->endpoint = h->ep_in;
350 urb->status = -1;
351 urb->buffer = data;
352 urb->buffer_length = len;
353
Elliott Hughes3af421c2015-07-23 15:20:09 -0700354 if (h->dead) {
355 errno = EINVAL;
356 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800357 }
358
Elliott Hughes3af421c2015-07-23 15:20:09 -0700359 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
360 return -1;
361 }
362
363 h->urb_in_busy = true;
364 while (true) {
Yabin Cui815ad882015-09-02 17:44:28 -0700365 D("[ reap urb - wait ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800366 h->reaper_thread = pthread_self();
Elliott Hughes3af421c2015-07-23 15:20:09 -0700367 int fd = h->fd;
368 lock.unlock();
369
370 // This ioctl must not have TEMP_FAILURE_RETRY because we send SIGALRM to break out.
371 usbdevfs_urb* out = nullptr;
372 int res = ioctl(fd, USBDEVFS_REAPURB, &out);
JP Abgrall2e5dd6e2011-03-16 15:57:42 -0700373 int saved_errno = errno;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700374
375 lock.lock();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800376 h->reaper_thread = 0;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700377 if (h->dead) {
378 errno = EINVAL;
379 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800380 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700381 if (res < 0) {
382 if (saved_errno == EINTR) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800383 continue;
384 }
Yabin Cui815ad882015-09-02 17:44:28 -0700385 D("[ reap urb - error ]");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700386 errno = saved_errno;
387 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800388 }
Yabin Cui815ad882015-09-02 17:44:28 -0700389 D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800390
Elliott Hughes3af421c2015-07-23 15:20:09 -0700391 if (out == &h->urb_in) {
Yabin Cui815ad882015-09-02 17:44:28 -0700392 D("[ reap urb - IN complete ]");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700393 h->urb_in_busy = false;
394 if (urb->status != 0) {
395 errno = -urb->status;
396 return -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800397 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700398 return urb->actual_length;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800399 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700400 if (out == &h->urb_out) {
Yabin Cui815ad882015-09-02 17:44:28 -0700401 D("[ reap urb - OUT compelete ]");
Elliott Hughes3af421c2015-07-23 15:20:09 -0700402 h->urb_out_busy = false;
403 h->cv.notify_all();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800404 }
405 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800406}
407
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800408int usb_write(usb_handle *h, const void *_data, int len)
409{
Yabin Cui815ad882015-09-02 17:44:28 -0700410 D("++ usb_write ++");
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100411
412 unsigned char *data = (unsigned char*) _data;
413 int n = usb_bulk_write(h, data, len);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700414 if (n != len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700415 D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno));
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100416 return -1;
417 }
418
Elliott Hughes3af421c2015-07-23 15:20:09 -0700419 if (h->zero_mask && !(len & h->zero_mask)) {
420 // If we need 0-markers and our transfer is an even multiple of the packet size,
421 // then send a zero marker.
Jerry Zhang76d17db2018-05-15 16:20:41 -0700422 return usb_bulk_write(h, _data, 0) == 0 ? n : -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800423 }
424
Yabin Cui815ad882015-09-02 17:44:28 -0700425 D("-- usb_write --");
Jerry Zhang76d17db2018-05-15 16:20:41 -0700426 return n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800427}
428
429int usb_read(usb_handle *h, void *_data, int len)
430{
431 unsigned char *data = (unsigned char*) _data;
432 int n;
433
Yabin Cui815ad882015-09-02 17:44:28 -0700434 D("++ usb_read ++");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800435 int orig_len = len;
436 while (len == orig_len) {
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100437 int xfer = len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800438
Yabin Cui815ad882015-09-02 17:44:28 -0700439 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 -0800440 n = usb_bulk_read(h, data, xfer);
Yabin Cui815ad882015-09-02 17:44:28 -0700441 D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str());
Yabin Cui3cf1b362017-03-10 16:01:01 -0800442 if (n <= 0) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700443 if((errno == ETIMEDOUT) && (h->fd != -1)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700444 D("[ timeout ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800445 continue;
446 }
Yabin Cui815ad882015-09-02 17:44:28 -0700447 D("ERROR: n = %d, errno = %d (%s)",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800448 n, errno, strerror(errno));
449 return -1;
450 }
451
Yabin Cui3cf1b362017-03-10 16:01:01 -0800452 len -= n;
453 data += n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800454 }
455
Yabin Cui815ad882015-09-02 17:44:28 -0700456 D("-- usb_read --");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800457 return orig_len - len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800458}
459
Elliott Hughes3af421c2015-07-23 15:20:09 -0700460void usb_kick(usb_handle* h) {
461 std::lock_guard<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700462 D("[ kicking %p (fd = %d) ]", h, h->fd);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700463 if (!h->dead) {
464 h->dead = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800465
Mike Lockwoode45583f2009-08-08 12:37:44 -0400466 if (h->writeable) {
467 /* HACK ALERT!
468 ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
469 ** This is a workaround for that problem.
470 */
471 if (h->reaper_thread) {
472 pthread_kill(h->reaper_thread, SIGALRM);
473 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800474
Mike Lockwoode45583f2009-08-08 12:37:44 -0400475 /* cancel any pending transactions
476 ** these will quietly fail if the txns are not active,
477 ** but this ensures that a reader blocked on REAPURB
478 ** will get unblocked
479 */
Elliott Hughes3af421c2015-07-23 15:20:09 -0700480 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
481 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400482 h->urb_in.status = -ENODEV;
483 h->urb_out.status = -ENODEV;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700484 h->urb_in_busy = false;
485 h->urb_out_busy = false;
486 h->cv.notify_all();
Mike Lockwoode45583f2009-08-08 12:37:44 -0400487 } else {
488 unregister_usb_transport(h);
489 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800490 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800491}
492
Elliott Hughes3af421c2015-07-23 15:20:09 -0700493int usb_close(usb_handle* h) {
494 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
495 g_usb_handles.remove(h);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800496
Yabin Cui815ad882015-09-02 17:44:28 -0700497 D("-- usb close %p (fd = %d) --", h, h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800498
Elliott Hughes3af421c2015-07-23 15:20:09 -0700499 delete h;
500
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800501 return 0;
502}
503
Josh Gao3734cf02017-05-02 15:01:09 -0700504size_t usb_get_max_packet_size(usb_handle* h) {
505 return h->max_packet_size;
506}
507
508static void register_device(const char* dev_name, const char* dev_path, unsigned char ep_in,
509 unsigned char ep_out, int interface, int serial_index,
510 unsigned zero_mask, size_t max_packet_size) {
Dan Alberta8c34142015-05-06 16:48:52 -0700511 // Since Linux will not reassign the device ID (and dev_name) as long as the
512 // device is open, we can add to the list here once we open it and remove
513 // from the list when we're finally closed and everything will work out
514 // fine.
515 //
Elliott Hughes3af421c2015-07-23 15:20:09 -0700516 // If we have a usb_handle on the list of handles with a matching name, we
Dan Alberta8c34142015-05-06 16:48:52 -0700517 // have no further work to do.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700518 {
519 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
520 for (usb_handle* usb: g_usb_handles) {
521 if (usb->path == dev_name) {
522 return;
523 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800524 }
525 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800526
Yabin Cui815ad882015-09-02 17:44:28 -0700527 D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700528 std::unique_ptr<usb_handle> usb(new usb_handle);
529 usb->path = dev_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800530 usb->ep_in = ep_in;
531 usb->ep_out = ep_out;
532 usb->zero_mask = zero_mask;
Josh Gao3734cf02017-05-02 15:01:09 -0700533 usb->max_packet_size = max_packet_size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800534
Elliott Hughes3af421c2015-07-23 15:20:09 -0700535 // Initialize mark so we don't get garbage collected after the device scan.
536 usb->mark = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800537
Josh Gaof0c44032018-12-12 16:12:28 -0800538 usb->fd = unix_open(usb->path, O_RDWR | O_CLOEXEC);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700539 if (usb->fd == -1) {
Elliott Hughes5d504372015-04-25 14:44:23 -0700540 // Opening RW failed, so see if we have RO access.
Josh Gaof0c44032018-12-12 16:12:28 -0800541 usb->fd = unix_open(usb->path, O_RDONLY | O_CLOEXEC);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700542 if (usb->fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700543 D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno));
Elliott Hughes5d504372015-04-25 14:44:23 -0700544 return;
545 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400546 usb->writeable = 0;
Elliott Hughes5d504372015-04-25 14:44:23 -0700547 }
548
Yabin Cui815ad882015-09-02 17:44:28 -0700549 D("[ usb opened %s%s, fd=%d]",
Elliott Hughes3af421c2015-07-23 15:20:09 -0700550 usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd);
Elliott Hughes5d504372015-04-25 14:44:23 -0700551
552 if (usb->writeable) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700553 if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700554 D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno));
Elliott Hughes5d504372015-04-25 14:44:23 -0700555 return;
556 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400557 }
558
Elliott Hughes949f2622015-04-27 14:20:17 -0700559 // Read the device's serial number.
Dan Alberta8c34142015-05-06 16:48:52 -0700560 std::string serial_path = android::base::StringPrintf(
561 "/sys/bus/usb/devices/%s/serial", dev_path + 4);
Elliott Hughes949f2622015-04-27 14:20:17 -0700562 std::string serial;
563 if (!android::base::ReadFileToString(serial_path, &serial)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700564 D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno));
Dan Alberta8c34142015-05-06 16:48:52 -0700565 // We don't actually want to treat an unknown serial as an error because
566 // devices aren't able to communicate a serial number in early bringup.
567 // http://b/20883914
568 serial = "";
Mike Lockwoode45583f2009-08-08 12:37:44 -0400569 }
Elliott Hughes949f2622015-04-27 14:20:17 -0700570 serial = android::base::Trim(serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800571
Dan Alberta8c34142015-05-06 16:48:52 -0700572 // Add to the end of the active handles.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700573 usb_handle* done_usb = usb.release();
574 {
575 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
576 g_usb_handles.push_back(done_usb);
577 }
578 register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800579}
580
Josh Gao0f3312a2017-04-12 17:00:49 -0700581static void device_poll_thread() {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700582 adb_thread_setname("device poll");
Yabin Cui815ad882015-09-02 17:44:28 -0700583 D("Created device thread");
Dan Alberta8c34142015-05-06 16:48:52 -0700584 while (true) {
585 // TODO: Use inotify.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800586 find_usb_device("/dev/bus/usb", register_device);
587 kick_disconnected_devices();
Elliott Hughes73925982016-11-15 12:37:32 -0800588 std::this_thread::sleep_for(1s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800589 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800590}
591
Elliott Hughes3af421c2015-07-23 15:20:09 -0700592void usb_init() {
593 struct sigaction actions;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800594 memset(&actions, 0, sizeof(actions));
595 sigemptyset(&actions.sa_mask);
596 actions.sa_flags = 0;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700597 actions.sa_handler = [](int) {};
598 sigaction(SIGALRM, &actions, nullptr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800599
Josh Gao0f3312a2017-04-12 17:00:49 -0700600 std::thread(device_poll_thread).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800601}
Josh Gao90b62532017-05-10 13:51:36 -0700602
603void usb_cleanup() {}
604
Josh Gaob7366922016-09-28 12:32:45 -0700605} // namespace native