blob: e7f1338b3d6d4b3263a2e5002c8da3573202d350 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Yabin Cuiaed3c612015-09-22 15:52:57 -070017#define TRACE_TAG USB
Dan Albert33134262015-03-19 15:21:08 -070018
19#include "sysdeps.h"
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <ctype.h>
Dan Albert76649012015-02-24 15:51:19 -080022#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
Elliott Hughes812f0302015-07-23 15:20:09 -070025#include <linux/usb/ch9.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026#include <linux/usbdevice_fs.h>
27#include <linux/version.h>
Dan Albert76649012015-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 Hughes812f0302015-07-23 15:20:09 -070035
36#include <chrono>
37#include <condition_variable>
38#include <list>
39#include <mutex>
40#include <string>
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080041#include <thread>
Elliott Hughes9309ecb2015-04-27 14:20:17 -070042
Elliott Hughes4f713192015-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 Projectdd7bc332009-03-03 19:32:55 -080046
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080048#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
Elliott Hughesdbe91ee2016-11-15 12:37:32 -080050using namespace std::chrono_literals;
Elliott Hughes812f0302015-07-23 15:20:09 -070051using namespace std::literals;
52
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053/* usb scan debugging is waaaay too verbose */
54#define DBGX(x...)
55
Elliott Hughes812f0302015-07-23 15:20:09 -070056struct usb_handle {
57 ~usb_handle() {
58 if (fd != -1) unix_close(fd);
59 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080060
Elliott Hughes812f0302015-07-23 15:20:09 -070061 std::string path;
62 int fd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 unsigned char ep_in;
64 unsigned char ep_out;
65
66 unsigned zero_mask;
Elliott Hughes812f0302015-07-23 15:20:09 -070067 unsigned writeable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068
Elliott Hughes812f0302015-07-23 15:20:09 -070069 usbdevfs_urb urb_in;
70 usbdevfs_urb urb_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080071
Elliott Hughes812f0302015-07-23 15:20:09 -070072 bool urb_in_busy = false;
73 bool urb_out_busy = false;
74 bool dead = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
Elliott Hughes812f0302015-07-23 15:20:09 -070076 std::condition_variable cv;
77 std::mutex mutex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080078
79 // for garbage collecting disconnected devices
Elliott Hughes812f0302015-07-23 15:20:09 -070080 bool mark;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080081
82 // ID of thread currently in REAPURB
Elliott Hughes812f0302015-07-23 15:20:09 -070083 pthread_t reaper_thread = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080084};
85
Josh Gaob7b1edf2015-11-11 17:56:12 -080086static auto& g_usb_handles_mutex = *new std::mutex();
87static auto& g_usb_handles = *new std::list<usb_handle*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080088
Elliott Hughes812f0302015-07-23 15:20:09 -070089static int is_known_device(const char* dev_name) {
90 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
91 for (usb_handle* usb : g_usb_handles) {
92 if (usb->path == dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 // set mark flag to indicate this device is still alive
Elliott Hughes812f0302015-07-23 15:20:09 -070094 usb->mark = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095 return 1;
96 }
97 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 return 0;
99}
100
Elliott Hughes812f0302015-07-23 15:20:09 -0700101static void kick_disconnected_devices() {
102 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800103 // kick any devices in the device list that were not found in the device scan
Elliott Hughes812f0302015-07-23 15:20:09 -0700104 for (usb_handle* usb : g_usb_handles) {
105 if (!usb->mark) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 usb_kick(usb);
107 } else {
Elliott Hughes812f0302015-07-23 15:20:09 -0700108 usb->mark = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 }
110 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111}
112
Elliott Hughes812f0302015-07-23 15:20:09 -0700113static inline bool contains_non_digit(const char* name) {
114 while (*name) {
115 if (!isdigit(*name++)) return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700117 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800118}
119
Elliott Hughes812f0302015-07-23 15:20:09 -0700120static void find_usb_device(const std::string& base,
Mike Lockwood0927bf92009-08-08 12:37:44 -0400121 void (*register_device_callback)
Elliott Hughes812f0302015-07-23 15:20:09 -0700122 (const char*, const char*, unsigned char, unsigned char, int, int, unsigned))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800123{
Elliott Hughes812f0302015-07-23 15:20:09 -0700124 std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
125 if (!bus_dir) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800126
Elliott Hughes812f0302015-07-23 15:20:09 -0700127 dirent* de;
128 while ((de = readdir(bus_dir.get())) != 0) {
129 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
Elliott Hughes812f0302015-07-23 15:20:09 -0700131 std::string bus_name = base + "/" + de->d_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
Elliott Hughes812f0302015-07-23 15:20:09 -0700133 std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
134 if (!dev_dir) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135
Elliott Hughes812f0302015-07-23 15:20:09 -0700136 while ((de = readdir(dev_dir.get()))) {
Mike Lockwoodb5966082011-01-07 23:18:14 -0500137 unsigned char devdesc[4096];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 unsigned char* bufptr = devdesc;
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500139 unsigned char* bufend;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140 struct usb_device_descriptor* device;
141 struct usb_config_descriptor* config;
142 struct usb_interface_descriptor* interface;
143 struct usb_endpoint_descriptor *ep1, *ep2;
144 unsigned zero_mask = 0;
145 unsigned vid, pid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146
Elliott Hughes812f0302015-07-23 15:20:09 -0700147 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148
Elliott Hughes812f0302015-07-23 15:20:09 -0700149 std::string dev_name = bus_name + "/" + de->d_name;
150 if (is_known_device(dev_name.c_str())) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 continue;
152 }
153
Elliott Hughes812f0302015-07-23 15:20:09 -0700154 int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC);
155 if (fd == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156 continue;
157 }
158
Elliott Hughes812f0302015-07-23 15:20:09 -0700159 size_t desclength = unix_read(fd, devdesc, sizeof(devdesc));
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500160 bufend = bufptr + desclength;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161
162 // should have device and configuration descriptors, and atleast two endpoints
163 if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700164 D("desclength %zu is too small", desclength);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700165 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800166 continue;
167 }
168
169 device = (struct usb_device_descriptor*)bufptr;
170 bufptr += USB_DT_DEVICE_SIZE;
171
172 if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700173 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 continue;
175 }
176
Marcus Comstedt6f703a22010-09-22 20:09:44 +0200177 vid = device->idVendor;
178 pid = device->idProduct;
Elliott Hughes812f0302015-07-23 15:20:09 -0700179 DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180
181 // should have config descriptor next
182 config = (struct usb_config_descriptor *)bufptr;
183 bufptr += USB_DT_CONFIG_SIZE;
184 if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700185 D("usb_config_descriptor not found");
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700186 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 continue;
188 }
189
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500190 // loop through all the descriptors and look for the ADB interface
191 while (bufptr < bufend) {
192 unsigned char length = bufptr[0];
193 unsigned char type = bufptr[1];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800194
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500195 if (type == USB_DT_INTERFACE) {
196 interface = (struct usb_interface_descriptor *)bufptr;
197 bufptr += length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500199 if (length != USB_DT_INTERFACE_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700200 D("interface descriptor has wrong size");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800201 break;
202 }
203
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500204 DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
205 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
206 interface->bInterfaceClass, interface->bInterfaceSubClass,
207 interface->bInterfaceProtocol, interface->bNumEndpoints);
208
209 if (interface->bNumEndpoints == 2 &&
Josh Gao30186df2016-09-26 21:18:58 -0700210 is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass,
211 interface->bInterfaceProtocol)) {
Scott Andersone109d262012-04-20 11:21:14 -0700212 struct stat st;
213 char pathbuf[128];
214 char link[256];
Elliott Hughes812f0302015-07-23 15:20:09 -0700215 char *devpath = nullptr;
Scott Andersone109d262012-04-20 11:21:14 -0700216
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500217 DBGX("looking for bulk endpoints\n");
218 // looks like ADB...
219 ep1 = (struct usb_endpoint_descriptor *)bufptr;
220 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff58b01e02014-05-16 21:51:41 +0200221 // For USB 3.0 SuperSpeed devices, skip potential
222 // USB 3.0 SuperSpeed Endpoint Companion descriptor
223 if (bufptr+2 <= devdesc + desclength &&
224 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
225 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
226 bufptr += USB_DT_SS_EP_COMP_SIZE;
227 }
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500228 ep2 = (struct usb_endpoint_descriptor *)bufptr;
229 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff58b01e02014-05-16 21:51:41 +0200230 if (bufptr+2 <= devdesc + desclength &&
231 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
232 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
233 bufptr += USB_DT_SS_EP_COMP_SIZE;
234 }
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500235
236 if (bufptr > devdesc + desclength ||
237 ep1->bLength != USB_DT_ENDPOINT_SIZE ||
238 ep1->bDescriptorType != USB_DT_ENDPOINT ||
239 ep2->bLength != USB_DT_ENDPOINT_SIZE ||
240 ep2->bDescriptorType != USB_DT_ENDPOINT) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700241 D("endpoints not found");
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500242 break;
243 }
244
245 // both endpoints should be bulk
246 if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
247 ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700248 D("bulk endpoints not found");
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500249 continue;
250 }
251 /* aproto 01 needs 0 termination */
252 if(interface->bInterfaceProtocol == 0x01) {
253 zero_mask = ep1->wMaxPacketSize - 1;
254 }
255
256 // we have a match. now we just need to figure out which is in and which is out.
Elliott Hughes812f0302015-07-23 15:20:09 -0700257 unsigned char local_ep_in, local_ep_out;
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500258 if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
259 local_ep_in = ep1->bEndpointAddress;
260 local_ep_out = ep2->bEndpointAddress;
261 } else {
262 local_ep_in = ep2->bEndpointAddress;
263 local_ep_out = ep1->bEndpointAddress;
264 }
265
Scott Andersone109d262012-04-20 11:21:14 -0700266 // Determine the device path
267 if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
Scott Andersone109d262012-04-20 11:21:14 -0700268 snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
269 major(st.st_rdev), minor(st.st_rdev));
Elliott Hughes3e7048c2015-07-27 21:21:39 -0700270 ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1);
Scott Andersone109d262012-04-20 11:21:14 -0700271 if (link_len > 0) {
272 link[link_len] = '\0';
Elliott Hughes3e7048c2015-07-27 21:21:39 -0700273 const char* slash = strrchr(link, '/');
Scott Andersone109d262012-04-20 11:21:14 -0700274 if (slash) {
275 snprintf(pathbuf, sizeof(pathbuf),
276 "usb:%s", slash + 1);
277 devpath = pathbuf;
278 }
279 }
280 }
281
Elliott Hughes812f0302015-07-23 15:20:09 -0700282 register_device_callback(dev_name.c_str(), devpath,
Scott Andersone109d262012-04-20 11:21:14 -0700283 local_ep_in, local_ep_out,
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500284 interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
285 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800286 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 } else {
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500288 bufptr += length;
289 }
290 } // end of while
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700292 unix_close(fd);
Elliott Hughes812f0302015-07-23 15:20:09 -0700293 }
294 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295}
296
Elliott Hughes812f0302015-07-23 15:20:09 -0700297static int usb_bulk_write(usb_handle* h, const void* data, int len) {
298 std::unique_lock<std::mutex> lock(h->mutex);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700299 D("++ usb_bulk_write ++");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300
Elliott Hughes812f0302015-07-23 15:20:09 -0700301 usbdevfs_urb* urb = &h->urb_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302 memset(urb, 0, sizeof(*urb));
303 urb->type = USBDEVFS_URB_TYPE_BULK;
304 urb->endpoint = h->ep_out;
305 urb->status = -1;
Elliott Hughes812f0302015-07-23 15:20:09 -0700306 urb->buffer = const_cast<void*>(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307 urb->buffer_length = len;
308
Elliott Hughes812f0302015-07-23 15:20:09 -0700309 if (h->dead) {
310 errno = EINVAL;
311 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800312 }
313
Elliott Hughes812f0302015-07-23 15:20:09 -0700314 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
315 return -1;
316 }
317
318 h->urb_out_busy = true;
319 while (true) {
320 auto now = std::chrono::system_clock::now();
321 if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) {
322 // TODO: call USBDEVFS_DISCARDURB?
323 errno = ETIMEDOUT;
324 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700326 if (!h->urb_out_busy) {
327 if (urb->status != 0) {
328 errno = -urb->status;
329 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700331 return urb->actual_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332 }
333 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334}
335
Elliott Hughes812f0302015-07-23 15:20:09 -0700336static int usb_bulk_read(usb_handle* h, void* data, int len) {
337 std::unique_lock<std::mutex> lock(h->mutex);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700338 D("++ usb_bulk_read ++");
Elliott Hughes812f0302015-07-23 15:20:09 -0700339
340 usbdevfs_urb* urb = &h->urb_in;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 memset(urb, 0, sizeof(*urb));
342 urb->type = USBDEVFS_URB_TYPE_BULK;
343 urb->endpoint = h->ep_in;
344 urb->status = -1;
345 urb->buffer = data;
346 urb->buffer_length = len;
347
Elliott Hughes812f0302015-07-23 15:20:09 -0700348 if (h->dead) {
349 errno = EINVAL;
350 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351 }
352
Elliott Hughes812f0302015-07-23 15:20:09 -0700353 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
354 return -1;
355 }
356
357 h->urb_in_busy = true;
358 while (true) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700359 D("[ reap urb - wait ]");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 h->reaper_thread = pthread_self();
Elliott Hughes812f0302015-07-23 15:20:09 -0700361 int fd = h->fd;
362 lock.unlock();
363
364 // This ioctl must not have TEMP_FAILURE_RETRY because we send SIGALRM to break out.
365 usbdevfs_urb* out = nullptr;
366 int res = ioctl(fd, USBDEVFS_REAPURB, &out);
JP Abgrall408fa572011-03-16 15:57:42 -0700367 int saved_errno = errno;
Elliott Hughes812f0302015-07-23 15:20:09 -0700368
369 lock.lock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800370 h->reaper_thread = 0;
Elliott Hughes812f0302015-07-23 15:20:09 -0700371 if (h->dead) {
372 errno = EINVAL;
373 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700375 if (res < 0) {
376 if (saved_errno == EINTR) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800377 continue;
378 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700379 D("[ reap urb - error ]");
Elliott Hughes812f0302015-07-23 15:20:09 -0700380 errno = saved_errno;
381 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700383 D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
Elliott Hughes812f0302015-07-23 15:20:09 -0700385 if (out == &h->urb_in) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700386 D("[ reap urb - IN complete ]");
Elliott Hughes812f0302015-07-23 15:20:09 -0700387 h->urb_in_busy = false;
388 if (urb->status != 0) {
389 errno = -urb->status;
390 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700392 return urb->actual_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700394 if (out == &h->urb_out) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700395 D("[ reap urb - OUT compelete ]");
Elliott Hughes812f0302015-07-23 15:20:09 -0700396 h->urb_out_busy = false;
397 h->cv.notify_all();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 }
399 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800400}
401
402
403int usb_write(usb_handle *h, const void *_data, int len)
404{
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700405 D("++ usb_write ++");
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100406
407 unsigned char *data = (unsigned char*) _data;
408 int n = usb_bulk_write(h, data, len);
Elliott Hughes812f0302015-07-23 15:20:09 -0700409 if (n != len) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700410 D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno));
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100411 return -1;
412 }
413
Elliott Hughes812f0302015-07-23 15:20:09 -0700414 if (h->zero_mask && !(len & h->zero_mask)) {
415 // If we need 0-markers and our transfer is an even multiple of the packet size,
416 // then send a zero marker.
417 return usb_bulk_write(h, _data, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418 }
419
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700420 D("-- usb_write --");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421 return 0;
422}
423
424int usb_read(usb_handle *h, void *_data, int len)
425{
426 unsigned char *data = (unsigned char*) _data;
427 int n;
428
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700429 D("++ usb_read ++");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 while(len > 0) {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100431 int xfer = len;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700433 D("[ usb read %d fd = %d], path=%s", xfer, h->fd, h->path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434 n = usb_bulk_read(h, data, xfer);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700435 D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436 if(n != xfer) {
Elliott Hughes812f0302015-07-23 15:20:09 -0700437 if((errno == ETIMEDOUT) && (h->fd != -1)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700438 D("[ timeout ]");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 if(n > 0){
440 data += n;
441 len -= n;
442 }
443 continue;
444 }
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700445 D("ERROR: n = %d, errno = %d (%s)",
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800446 n, errno, strerror(errno));
447 return -1;
448 }
449
450 len -= xfer;
451 data += xfer;
452 }
453
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700454 D("-- usb_read --");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455 return 0;
456}
457
Elliott Hughes812f0302015-07-23 15:20:09 -0700458void usb_kick(usb_handle* h) {
459 std::lock_guard<std::mutex> lock(h->mutex);
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700460 D("[ kicking %p (fd = %d) ]", h, h->fd);
Elliott Hughes812f0302015-07-23 15:20:09 -0700461 if (!h->dead) {
462 h->dead = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800463
Mike Lockwood0927bf92009-08-08 12:37:44 -0400464 if (h->writeable) {
465 /* HACK ALERT!
466 ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
467 ** This is a workaround for that problem.
468 */
469 if (h->reaper_thread) {
470 pthread_kill(h->reaper_thread, SIGALRM);
471 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472
Mike Lockwood0927bf92009-08-08 12:37:44 -0400473 /* cancel any pending transactions
474 ** these will quietly fail if the txns are not active,
475 ** but this ensures that a reader blocked on REAPURB
476 ** will get unblocked
477 */
Elliott Hughes812f0302015-07-23 15:20:09 -0700478 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
479 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400480 h->urb_in.status = -ENODEV;
481 h->urb_out.status = -ENODEV;
Elliott Hughes812f0302015-07-23 15:20:09 -0700482 h->urb_in_busy = false;
483 h->urb_out_busy = false;
484 h->cv.notify_all();
Mike Lockwood0927bf92009-08-08 12:37:44 -0400485 } else {
486 unregister_usb_transport(h);
487 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489}
490
Elliott Hughes812f0302015-07-23 15:20:09 -0700491int usb_close(usb_handle* h) {
492 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
493 g_usb_handles.remove(h);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700495 D("-- usb close %p (fd = %d) --", h, h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800496
Elliott Hughes812f0302015-07-23 15:20:09 -0700497 delete h;
498
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800499 return 0;
500}
501
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700502static void register_device(const char* dev_name, const char* dev_path,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800503 unsigned char ep_in, unsigned char ep_out,
Dan Albertd99d9022015-05-06 16:48:52 -0700504 int interface, int serial_index,
505 unsigned zero_mask) {
506 // Since Linux will not reassign the device ID (and dev_name) as long as the
507 // device is open, we can add to the list here once we open it and remove
508 // from the list when we're finally closed and everything will work out
509 // fine.
510 //
Elliott Hughes812f0302015-07-23 15:20:09 -0700511 // If we have a usb_handle on the list of handles with a matching name, we
Dan Albertd99d9022015-05-06 16:48:52 -0700512 // have no further work to do.
Elliott Hughes812f0302015-07-23 15:20:09 -0700513 {
514 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
515 for (usb_handle* usb: g_usb_handles) {
516 if (usb->path == dev_name) {
517 return;
518 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800519 }
520 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800521
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700522 D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface);
Elliott Hughes812f0302015-07-23 15:20:09 -0700523 std::unique_ptr<usb_handle> usb(new usb_handle);
524 usb->path = dev_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800525 usb->ep_in = ep_in;
526 usb->ep_out = ep_out;
527 usb->zero_mask = zero_mask;
528
Elliott Hughes812f0302015-07-23 15:20:09 -0700529 // Initialize mark so we don't get garbage collected after the device scan.
530 usb->mark = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800531
Elliott Hughes812f0302015-07-23 15:20:09 -0700532 usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC);
533 if (usb->fd == -1) {
Elliott Hughesce6363b2015-04-25 14:44:23 -0700534 // Opening RW failed, so see if we have RO access.
Elliott Hughes812f0302015-07-23 15:20:09 -0700535 usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC);
536 if (usb->fd == -1) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700537 D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno));
Elliott Hughesce6363b2015-04-25 14:44:23 -0700538 return;
539 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400540 usb->writeable = 0;
Elliott Hughesce6363b2015-04-25 14:44:23 -0700541 }
542
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700543 D("[ usb opened %s%s, fd=%d]",
Elliott Hughes812f0302015-07-23 15:20:09 -0700544 usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd);
Elliott Hughesce6363b2015-04-25 14:44:23 -0700545
546 if (usb->writeable) {
Elliott Hughes812f0302015-07-23 15:20:09 -0700547 if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700548 D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno));
Elliott Hughesce6363b2015-04-25 14:44:23 -0700549 return;
550 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400551 }
552
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700553 // Read the device's serial number.
Dan Albertd99d9022015-05-06 16:48:52 -0700554 std::string serial_path = android::base::StringPrintf(
555 "/sys/bus/usb/devices/%s/serial", dev_path + 4);
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700556 std::string serial;
557 if (!android::base::ReadFileToString(serial_path, &serial)) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700558 D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno));
Dan Albertd99d9022015-05-06 16:48:52 -0700559 // We don't actually want to treat an unknown serial as an error because
560 // devices aren't able to communicate a serial number in early bringup.
561 // http://b/20883914
562 serial = "";
Mike Lockwood0927bf92009-08-08 12:37:44 -0400563 }
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700564 serial = android::base::Trim(serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800565
Dan Albertd99d9022015-05-06 16:48:52 -0700566 // Add to the end of the active handles.
Elliott Hughes812f0302015-07-23 15:20:09 -0700567 usb_handle* done_usb = usb.release();
568 {
569 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
570 g_usb_handles.push_back(done_usb);
571 }
572 register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800573}
574
Josh Gaob5fea142016-02-12 14:31:15 -0800575static void device_poll_thread(void*) {
Siva Velusamy49ee7cf2015-08-28 16:37:29 -0700576 adb_thread_setname("device poll");
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700577 D("Created device thread");
Dan Albertd99d9022015-05-06 16:48:52 -0700578 while (true) {
579 // TODO: Use inotify.
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800580 find_usb_device("/dev/bus/usb", register_device);
581 kick_disconnected_devices();
Elliott Hughesdbe91ee2016-11-15 12:37:32 -0800582 std::this_thread::sleep_for(1s);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800583 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800584}
585
Elliott Hughes812f0302015-07-23 15:20:09 -0700586void usb_init() {
587 struct sigaction actions;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800588 memset(&actions, 0, sizeof(actions));
589 sigemptyset(&actions.sa_mask);
590 actions.sa_flags = 0;
Elliott Hughes812f0302015-07-23 15:20:09 -0700591 actions.sa_handler = [](int) {};
592 sigaction(SIGALRM, &actions, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800593
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700594 if (!adb_thread_create(device_poll_thread, nullptr)) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700595 fatal_errno("cannot create device_poll thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 }
597}