blob: a7df0ed511502e2c77526e26c9b0057738513e44 [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>
33#include <sys/types.h>
34#include <unistd.h>
Elliott Hughes3af421c2015-07-23 15:20:09 -070035
36#include <chrono>
37#include <condition_variable>
38#include <list>
39#include <mutex>
40#include <string>
Elliott Hughes73925982016-11-15 12:37:32 -080041#include <thread>
Elliott Hughes949f2622015-04-27 14:20:17 -070042
Elliott Hughesf55ead92015-12-04 22:00:26 -080043#include <android-base/file.h>
44#include <android-base/stringprintf.h>
45#include <android-base/strings.h>
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080046
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080047#include "adb.h"
Dan Albertb302d122015-02-24 15:51:19 -080048#include "transport.h"
Josh Gaob7366922016-09-28 12:32:45 -070049#include "usb.h"
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080050
Elliott Hughes73925982016-11-15 12:37:32 -080051using namespace std::chrono_literals;
Elliott Hughes3af421c2015-07-23 15:20:09 -070052using namespace std::literals;
53
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080054/* usb scan debugging is waaaay too verbose */
55#define DBGX(x...)
56
Josh Gaob7366922016-09-28 12:32:45 -070057namespace native {
58struct usb_handle : public ::usb_handle {
Elliott Hughes3af421c2015-07-23 15:20:09 -070059 ~usb_handle() {
60 if (fd != -1) unix_close(fd);
61 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080062
Elliott Hughes3af421c2015-07-23 15:20:09 -070063 std::string path;
64 int fd = -1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080065 unsigned char ep_in;
66 unsigned char ep_out;
67
Josh Gao3734cf02017-05-02 15:01:09 -070068 size_t max_packet_size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080069 unsigned zero_mask;
Elliott Hughes3af421c2015-07-23 15:20:09 -070070 unsigned writeable = 1;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080071
Elliott Hughes3af421c2015-07-23 15:20:09 -070072 usbdevfs_urb urb_in;
73 usbdevfs_urb urb_out;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080074
Elliott Hughes3af421c2015-07-23 15:20:09 -070075 bool urb_in_busy = false;
76 bool urb_out_busy = false;
77 bool dead = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080078
Elliott Hughes3af421c2015-07-23 15:20:09 -070079 std::condition_variable cv;
80 std::mutex mutex;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080081
82 // for garbage collecting disconnected devices
Elliott Hughes3af421c2015-07-23 15:20:09 -070083 bool mark;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080084
85 // ID of thread currently in REAPURB
Elliott Hughes3af421c2015-07-23 15:20:09 -070086 pthread_t reaper_thread = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080087};
88
Josh Gaoe3a87d02015-11-11 17:56:12 -080089static auto& g_usb_handles_mutex = *new std::mutex();
90static auto& g_usb_handles = *new std::list<usb_handle*>();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080091
Elliott Hughes3af421c2015-07-23 15:20:09 -070092static int is_known_device(const char* dev_name) {
93 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
94 for (usb_handle* usb : g_usb_handles) {
95 if (usb->path == dev_name) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080096 // set mark flag to indicate this device is still alive
Elliott Hughes3af421c2015-07-23 15:20:09 -070097 usb->mark = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -080098 return 1;
99 }
100 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800101 return 0;
102}
103
Elliott Hughes3af421c2015-07-23 15:20:09 -0700104static void kick_disconnected_devices() {
105 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800106 // kick any devices in the device list that were not found in the device scan
Elliott Hughes3af421c2015-07-23 15:20:09 -0700107 for (usb_handle* usb : g_usb_handles) {
108 if (!usb->mark) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800109 usb_kick(usb);
110 } else {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700111 usb->mark = false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800112 }
113 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800114}
115
Elliott Hughes3af421c2015-07-23 15:20:09 -0700116static inline bool contains_non_digit(const char* name) {
117 while (*name) {
118 if (!isdigit(*name++)) return true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800119 }
Elliott Hughes3af421c2015-07-23 15:20:09 -0700120 return false;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800121}
122
Elliott Hughes3af421c2015-07-23 15:20:09 -0700123static void find_usb_device(const std::string& base,
Josh Gao3734cf02017-05-02 15:01:09 -0700124 void (*register_device_callback)(const char*, const char*,
125 unsigned char, unsigned char, int, int,
126 unsigned, size_t)) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700127 std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
128 if (!bus_dir) return;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800129
Elliott Hughes3af421c2015-07-23 15:20:09 -0700130 dirent* de;
131 while ((de = readdir(bus_dir.get())) != 0) {
132 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800133
Elliott Hughes3af421c2015-07-23 15:20:09 -0700134 std::string bus_name = base + "/" + de->d_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800135
Elliott Hughes3af421c2015-07-23 15:20:09 -0700136 std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
137 if (!dev_dir) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800138
Elliott Hughes3af421c2015-07-23 15:20:09 -0700139 while ((de = readdir(dev_dir.get()))) {
Mike Lockwoodf1667712011-01-07 23:18:14 -0500140 unsigned char devdesc[4096];
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800141 unsigned char* bufptr = devdesc;
Mike Lockwood31175d62010-01-17 00:52:27 -0500142 unsigned char* bufend;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800143 struct usb_device_descriptor* device;
144 struct usb_config_descriptor* config;
145 struct usb_interface_descriptor* interface;
146 struct usb_endpoint_descriptor *ep1, *ep2;
147 unsigned zero_mask = 0;
Josh Gao3734cf02017-05-02 15:01:09 -0700148 size_t max_packet_size = 0;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800149 unsigned vid, pid;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800150
Elliott Hughes3af421c2015-07-23 15:20:09 -0700151 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800152
Elliott Hughes3af421c2015-07-23 15:20:09 -0700153 std::string dev_name = bus_name + "/" + de->d_name;
154 if (is_known_device(dev_name.c_str())) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800155 continue;
156 }
157
Elliott Hughes3af421c2015-07-23 15:20:09 -0700158 int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC);
159 if (fd == -1) {
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800160 continue;
161 }
162
Elliott Hughes3af421c2015-07-23 15:20:09 -0700163 size_t desclength = unix_read(fd, devdesc, sizeof(devdesc));
Mike Lockwood31175d62010-01-17 00:52:27 -0500164 bufend = bufptr + desclength;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800165
166 // should have device and configuration descriptors, and atleast two endpoints
167 if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
Yabin Cui815ad882015-09-02 17:44:28 -0700168 D("desclength %zu is too small", desclength);
Spencer Low3a2421b2015-05-22 20:09:06 -0700169 unix_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800170 continue;
171 }
172
173 device = (struct usb_device_descriptor*)bufptr;
174 bufptr += USB_DT_DEVICE_SIZE;
175
176 if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
Spencer Low3a2421b2015-05-22 20:09:06 -0700177 unix_close(fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800178 continue;
179 }
180
Marcus Comstedt3d2f5182010-09-22 20:09:44 +0200181 vid = device->idVendor;
182 pid = device->idProduct;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700183 DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800184
185 // should have config descriptor next
186 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 */
Josh Gao3734cf02017-05-02 15:01:09 -0700256 if (interface->bInterfaceProtocol == 0x01) {
257 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) {
325 auto now = std::chrono::system_clock::now();
326 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
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800407int usb_write(usb_handle *h, const void *_data, int len)
408{
Yabin Cui815ad882015-09-02 17:44:28 -0700409 D("++ usb_write ++");
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100410
411 unsigned char *data = (unsigned char*) _data;
412 int n = usb_bulk_write(h, data, len);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700413 if (n != len) {
Yabin Cui815ad882015-09-02 17:44:28 -0700414 D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno));
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100415 return -1;
416 }
417
Elliott Hughes3af421c2015-07-23 15:20:09 -0700418 if (h->zero_mask && !(len & h->zero_mask)) {
419 // If we need 0-markers and our transfer is an even multiple of the packet size,
420 // then send a zero marker.
421 return usb_bulk_write(h, _data, 0);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800422 }
423
Yabin Cui815ad882015-09-02 17:44:28 -0700424 D("-- usb_write --");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800425 return 0;
426}
427
428int usb_read(usb_handle *h, void *_data, int len)
429{
430 unsigned char *data = (unsigned char*) _data;
431 int n;
432
Yabin Cui815ad882015-09-02 17:44:28 -0700433 D("++ usb_read ++");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800434 int orig_len = len;
435 while (len == orig_len) {
Tamas Berghammera1c60c02015-07-13 19:12:28 +0100436 int xfer = len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800437
Yabin Cui815ad882015-09-02 17:44:28 -0700438 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 -0800439 n = usb_bulk_read(h, data, xfer);
Yabin Cui815ad882015-09-02 17:44:28 -0700440 D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str());
Yabin Cui3cf1b362017-03-10 16:01:01 -0800441 if (n <= 0) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700442 if((errno == ETIMEDOUT) && (h->fd != -1)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700443 D("[ timeout ]");
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800444 continue;
445 }
Yabin Cui815ad882015-09-02 17:44:28 -0700446 D("ERROR: n = %d, errno = %d (%s)",
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800447 n, errno, strerror(errno));
448 return -1;
449 }
450
Yabin Cui3cf1b362017-03-10 16:01:01 -0800451 len -= n;
452 data += n;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800453 }
454
Yabin Cui815ad882015-09-02 17:44:28 -0700455 D("-- usb_read --");
Yabin Cui3cf1b362017-03-10 16:01:01 -0800456 return orig_len - len;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800457}
458
Elliott Hughes3af421c2015-07-23 15:20:09 -0700459void usb_kick(usb_handle* h) {
460 std::lock_guard<std::mutex> lock(h->mutex);
Yabin Cui815ad882015-09-02 17:44:28 -0700461 D("[ kicking %p (fd = %d) ]", h, h->fd);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700462 if (!h->dead) {
463 h->dead = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800464
Mike Lockwoode45583f2009-08-08 12:37:44 -0400465 if (h->writeable) {
466 /* HACK ALERT!
467 ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
468 ** This is a workaround for that problem.
469 */
470 if (h->reaper_thread) {
471 pthread_kill(h->reaper_thread, SIGALRM);
472 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800473
Mike Lockwoode45583f2009-08-08 12:37:44 -0400474 /* cancel any pending transactions
475 ** these will quietly fail if the txns are not active,
476 ** but this ensures that a reader blocked on REAPURB
477 ** will get unblocked
478 */
Elliott Hughes3af421c2015-07-23 15:20:09 -0700479 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
480 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
Mike Lockwoode45583f2009-08-08 12:37:44 -0400481 h->urb_in.status = -ENODEV;
482 h->urb_out.status = -ENODEV;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700483 h->urb_in_busy = false;
484 h->urb_out_busy = false;
485 h->cv.notify_all();
Mike Lockwoode45583f2009-08-08 12:37:44 -0400486 } else {
487 unregister_usb_transport(h);
488 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800489 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800490}
491
Elliott Hughes3af421c2015-07-23 15:20:09 -0700492int usb_close(usb_handle* h) {
493 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
494 g_usb_handles.remove(h);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800495
Yabin Cui815ad882015-09-02 17:44:28 -0700496 D("-- usb close %p (fd = %d) --", h, h->fd);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800497
Elliott Hughes3af421c2015-07-23 15:20:09 -0700498 delete h;
499
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800500 return 0;
501}
502
Josh Gao3734cf02017-05-02 15:01:09 -0700503size_t usb_get_max_packet_size(usb_handle* h) {
504 return h->max_packet_size;
505}
506
507static void register_device(const char* dev_name, const char* dev_path, unsigned char ep_in,
508 unsigned char ep_out, int interface, int serial_index,
509 unsigned zero_mask, size_t max_packet_size) {
Dan Alberta8c34142015-05-06 16:48:52 -0700510 // Since Linux will not reassign the device ID (and dev_name) as long as the
511 // device is open, we can add to the list here once we open it and remove
512 // from the list when we're finally closed and everything will work out
513 // fine.
514 //
Elliott Hughes3af421c2015-07-23 15:20:09 -0700515 // If we have a usb_handle on the list of handles with a matching name, we
Dan Alberta8c34142015-05-06 16:48:52 -0700516 // have no further work to do.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700517 {
518 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
519 for (usb_handle* usb: g_usb_handles) {
520 if (usb->path == dev_name) {
521 return;
522 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800523 }
524 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800525
Yabin Cui815ad882015-09-02 17:44:28 -0700526 D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface);
Elliott Hughes3af421c2015-07-23 15:20:09 -0700527 std::unique_ptr<usb_handle> usb(new usb_handle);
528 usb->path = dev_name;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800529 usb->ep_in = ep_in;
530 usb->ep_out = ep_out;
531 usb->zero_mask = zero_mask;
Josh Gao3734cf02017-05-02 15:01:09 -0700532 usb->max_packet_size = max_packet_size;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800533
Elliott Hughes3af421c2015-07-23 15:20:09 -0700534 // Initialize mark so we don't get garbage collected after the device scan.
535 usb->mark = true;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800536
Elliott Hughes3af421c2015-07-23 15:20:09 -0700537 usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC);
538 if (usb->fd == -1) {
Elliott Hughes5d504372015-04-25 14:44:23 -0700539 // Opening RW failed, so see if we have RO access.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700540 usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC);
541 if (usb->fd == -1) {
Yabin Cui815ad882015-09-02 17:44:28 -0700542 D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno));
Elliott Hughes5d504372015-04-25 14:44:23 -0700543 return;
544 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400545 usb->writeable = 0;
Elliott Hughes5d504372015-04-25 14:44:23 -0700546 }
547
Yabin Cui815ad882015-09-02 17:44:28 -0700548 D("[ usb opened %s%s, fd=%d]",
Elliott Hughes3af421c2015-07-23 15:20:09 -0700549 usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd);
Elliott Hughes5d504372015-04-25 14:44:23 -0700550
551 if (usb->writeable) {
Elliott Hughes3af421c2015-07-23 15:20:09 -0700552 if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
Yabin Cui815ad882015-09-02 17:44:28 -0700553 D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno));
Elliott Hughes5d504372015-04-25 14:44:23 -0700554 return;
555 }
Mike Lockwoode45583f2009-08-08 12:37:44 -0400556 }
557
Elliott Hughes949f2622015-04-27 14:20:17 -0700558 // Read the device's serial number.
Dan Alberta8c34142015-05-06 16:48:52 -0700559 std::string serial_path = android::base::StringPrintf(
560 "/sys/bus/usb/devices/%s/serial", dev_path + 4);
Elliott Hughes949f2622015-04-27 14:20:17 -0700561 std::string serial;
562 if (!android::base::ReadFileToString(serial_path, &serial)) {
Yabin Cui815ad882015-09-02 17:44:28 -0700563 D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno));
Dan Alberta8c34142015-05-06 16:48:52 -0700564 // We don't actually want to treat an unknown serial as an error because
565 // devices aren't able to communicate a serial number in early bringup.
566 // http://b/20883914
567 serial = "";
Mike Lockwoode45583f2009-08-08 12:37:44 -0400568 }
Elliott Hughes949f2622015-04-27 14:20:17 -0700569 serial = android::base::Trim(serial);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800570
Dan Alberta8c34142015-05-06 16:48:52 -0700571 // Add to the end of the active handles.
Elliott Hughes3af421c2015-07-23 15:20:09 -0700572 usb_handle* done_usb = usb.release();
573 {
574 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
575 g_usb_handles.push_back(done_usb);
576 }
577 register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800578}
579
Josh Gao0f3312a2017-04-12 17:00:49 -0700580static void device_poll_thread() {
Siva Velusamy2669cf92015-08-28 16:37:29 -0700581 adb_thread_setname("device poll");
Yabin Cui815ad882015-09-02 17:44:28 -0700582 D("Created device thread");
Dan Alberta8c34142015-05-06 16:48:52 -0700583 while (true) {
584 // TODO: Use inotify.
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800585 find_usb_device("/dev/bus/usb", register_device);
586 kick_disconnected_devices();
Elliott Hughes73925982016-11-15 12:37:32 -0800587 std::this_thread::sleep_for(1s);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800588 }
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800589}
590
Elliott Hughes3af421c2015-07-23 15:20:09 -0700591void usb_init() {
592 struct sigaction actions;
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800593 memset(&actions, 0, sizeof(actions));
594 sigemptyset(&actions.sa_mask);
595 actions.sa_flags = 0;
Elliott Hughes3af421c2015-07-23 15:20:09 -0700596 actions.sa_handler = [](int) {};
597 sigaction(SIGALRM, &actions, nullptr);
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800598
Josh Gao0f3312a2017-04-12 17:00:49 -0700599 std::thread(device_poll_thread).detach();
The Android Open Source Project9ca14dc2009-03-03 19:32:55 -0800600}
Josh Gao90b62532017-05-10 13:51:36 -0700601
602void usb_cleanup() {}
603
Josh Gaob7366922016-09-28 12:32:45 -0700604} // namespace native