blob: e887a94d20e23db2fc1b282b31daf6f2532d68f6 [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>
David Purselld2acbd12015-12-02 15:14:31 -080025#include <grp.h>
Elliott Hughes812f0302015-07-23 15:20:09 -070026#include <linux/usb/ch9.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080027#include <linux/usbdevice_fs.h>
28#include <linux/version.h>
Dan Albert76649012015-02-24 15:51:19 -080029#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <sys/ioctl.h>
33#include <sys/time.h>
34#include <sys/types.h>
35#include <unistd.h>
Elliott Hughes812f0302015-07-23 15:20:09 -070036
37#include <chrono>
38#include <condition_variable>
39#include <list>
40#include <mutex>
41#include <string>
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 Hughes812f0302015-07-23 15:20:09 -070050using namespace std::literals;
51
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080052/* usb scan debugging is waaaay too verbose */
53#define DBGX(x...)
54
Elliott Hughes812f0302015-07-23 15:20:09 -070055struct usb_handle {
56 ~usb_handle() {
57 if (fd != -1) unix_close(fd);
58 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
Elliott Hughes812f0302015-07-23 15:20:09 -070060 std::string path;
61 int fd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062 unsigned char ep_in;
63 unsigned char ep_out;
64
65 unsigned zero_mask;
Elliott Hughes812f0302015-07-23 15:20:09 -070066 unsigned writeable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067
Elliott Hughes812f0302015-07-23 15:20:09 -070068 usbdevfs_urb urb_in;
69 usbdevfs_urb urb_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
Elliott Hughes812f0302015-07-23 15:20:09 -070071 bool urb_in_busy = false;
72 bool urb_out_busy = false;
73 bool dead = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080074
Elliott Hughes812f0302015-07-23 15:20:09 -070075 std::condition_variable cv;
76 std::mutex mutex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077
78 // for garbage collecting disconnected devices
Elliott Hughes812f0302015-07-23 15:20:09 -070079 bool mark;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080
81 // ID of thread currently in REAPURB
Elliott Hughes812f0302015-07-23 15:20:09 -070082 pthread_t reaper_thread = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083};
84
Josh Gaob7b1edf2015-11-11 17:56:12 -080085static auto& g_usb_handles_mutex = *new std::mutex();
86static auto& g_usb_handles = *new std::list<usb_handle*>();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087
Elliott Hughes812f0302015-07-23 15:20:09 -070088static int is_known_device(const char* dev_name) {
89 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
90 for (usb_handle* usb : g_usb_handles) {
91 if (usb->path == dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 // set mark flag to indicate this device is still alive
Elliott Hughes812f0302015-07-23 15:20:09 -070093 usb->mark = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094 return 1;
95 }
96 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 return 0;
98}
99
Elliott Hughes812f0302015-07-23 15:20:09 -0700100static void kick_disconnected_devices() {
101 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102 // kick any devices in the device list that were not found in the device scan
Elliott Hughes812f0302015-07-23 15:20:09 -0700103 for (usb_handle* usb : g_usb_handles) {
104 if (!usb->mark) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 usb_kick(usb);
106 } else {
Elliott Hughes812f0302015-07-23 15:20:09 -0700107 usb->mark = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800108 }
109 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110}
111
Elliott Hughes812f0302015-07-23 15:20:09 -0700112static inline bool contains_non_digit(const char* name) {
113 while (*name) {
114 if (!isdigit(*name++)) return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700116 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800117}
118
Elliott Hughes812f0302015-07-23 15:20:09 -0700119static void find_usb_device(const std::string& base,
Mike Lockwood0927bf92009-08-08 12:37:44 -0400120 void (*register_device_callback)
Elliott Hughes812f0302015-07-23 15:20:09 -0700121 (const char*, const char*, unsigned char, unsigned char, int, int, unsigned))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800122{
Elliott Hughes812f0302015-07-23 15:20:09 -0700123 std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
124 if (!bus_dir) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125
Elliott Hughes812f0302015-07-23 15:20:09 -0700126 dirent* de;
127 while ((de = readdir(bus_dir.get())) != 0) {
128 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
Elliott Hughes812f0302015-07-23 15:20:09 -0700130 std::string bus_name = base + "/" + de->d_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800131
Elliott Hughes812f0302015-07-23 15:20:09 -0700132 std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
133 if (!dev_dir) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
Elliott Hughes812f0302015-07-23 15:20:09 -0700135 while ((de = readdir(dev_dir.get()))) {
Mike Lockwoodb5966082011-01-07 23:18:14 -0500136 unsigned char devdesc[4096];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 unsigned char* bufptr = devdesc;
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500138 unsigned char* bufend;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800139 struct usb_device_descriptor* device;
140 struct usb_config_descriptor* config;
141 struct usb_interface_descriptor* interface;
142 struct usb_endpoint_descriptor *ep1, *ep2;
143 unsigned zero_mask = 0;
144 unsigned vid, pid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
Elliott Hughes812f0302015-07-23 15:20:09 -0700146 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147
Elliott Hughes812f0302015-07-23 15:20:09 -0700148 std::string dev_name = bus_name + "/" + de->d_name;
149 if (is_known_device(dev_name.c_str())) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800150 continue;
151 }
152
Elliott Hughes812f0302015-07-23 15:20:09 -0700153 int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC);
154 if (fd == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 continue;
156 }
157
Elliott Hughes812f0302015-07-23 15:20:09 -0700158 size_t desclength = unix_read(fd, devdesc, sizeof(devdesc));
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500159 bufend = bufptr + desclength;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160
161 // should have device and configuration descriptors, and atleast two endpoints
162 if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700163 D("desclength %zu is too small", desclength);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700164 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800165 continue;
166 }
167
168 device = (struct usb_device_descriptor*)bufptr;
169 bufptr += USB_DT_DEVICE_SIZE;
170
171 if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700172 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 continue;
174 }
175
Marcus Comstedt6f703a22010-09-22 20:09:44 +0200176 vid = device->idVendor;
177 pid = device->idProduct;
Elliott Hughes812f0302015-07-23 15:20:09 -0700178 DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179
180 // should have config descriptor next
181 config = (struct usb_config_descriptor *)bufptr;
182 bufptr += USB_DT_CONFIG_SIZE;
183 if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700184 D("usb_config_descriptor not found");
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700185 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 continue;
187 }
188
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500189 // loop through all the descriptors and look for the ADB interface
190 while (bufptr < bufend) {
191 unsigned char length = bufptr[0];
192 unsigned char type = bufptr[1];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500194 if (type == USB_DT_INTERFACE) {
195 interface = (struct usb_interface_descriptor *)bufptr;
196 bufptr += length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800197
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500198 if (length != USB_DT_INTERFACE_SIZE) {
Yabin Cui7a3f8d62015-09-02 17:44:28 -0700199 D("interface descriptor has wrong size");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800200 break;
201 }
202
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500203 DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
204 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
205 interface->bInterfaceClass, interface->bInterfaceSubClass,
206 interface->bInterfaceProtocol, interface->bNumEndpoints);
207
208 if (interface->bNumEndpoints == 2 &&
209 is_adb_interface(vid, pid, interface->bInterfaceClass,
210 interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
211
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
Dan Albertd99d9022015-05-06 16:48:52 -0700575static void* device_poll_thread(void* unused) {
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();
582 sleep(1);
583 }
Dan Albertd99d9022015-05-06 16:48:52 -0700584 return nullptr;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800585}
586
Elliott Hughes812f0302015-07-23 15:20:09 -0700587void usb_init() {
588 struct sigaction actions;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800589 memset(&actions, 0, sizeof(actions));
590 sigemptyset(&actions.sa_mask);
591 actions.sa_flags = 0;
Elliott Hughes812f0302015-07-23 15:20:09 -0700592 actions.sa_handler = [](int) {};
593 sigaction(SIGALRM, &actions, nullptr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800594
Elliott Hughes9b0f3542015-05-05 13:41:21 -0700595 if (!adb_thread_create(device_poll_thread, nullptr)) {
Yabin Cuid6ab3c22015-08-31 11:50:24 -0700596 fatal_errno("cannot create device_poll thread");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800597 }
598}
David Purselld2acbd12015-12-02 15:14:31 -0800599
600static const char kPermissionsHelpUrl[] = "developer.android.com/tools/device.html";
601
602// Returns a message describing any potential problems we find with udev, or nullptr if we can't
603// find plugdev information (i.e. udev is not installed).
604static const char* GetUdevProblem() {
605 errno = 0;
606 group* plugdev_group = getgrnam("plugdev");
607
608 if (plugdev_group == nullptr) {
609 if (errno != 0) {
610 D("failed to read plugdev group info: %s", strerror(errno));
611 }
612 // We can't give any generally useful advice here, just let the caller print the help URL.
613 return nullptr;
614 }
615
616 // getgroups(2) indicates that the group_member() may not check the egid so we check it
617 // additionally just to be sure.
618 if (group_member(plugdev_group->gr_gid) || getegid() == plugdev_group->gr_gid) {
619 // The user is in plugdev so the problem is likely with the udev rules.
620 return "verify udev rules";
621 }
622 return "udev requires plugdev group membership";
623}
624
625// Short help text must be a single line, and will look something like:
626// no permissions (reason); see <URL>
627std::string UsbNoPermissionsShortHelpText() {
628 std::string help_text = "no permissions";
629
630 const char* problem = GetUdevProblem();
631 if (problem != nullptr) {
632 help_text += android::base::StringPrintf(" (%s)", problem);
633 }
634
635 return android::base::StringPrintf("%s; see [%s]", help_text.c_str(), kPermissionsHelpUrl);
636}
637
638// Long help text can span multiple lines and should provide more detailed information.
639std::string UsbNoPermissionsLongHelpText() {
640 std::string header = "USB permission failure";
641
642 const char* problem = GetUdevProblem();
643 if (problem != nullptr) {
644 header += android::base::StringPrintf(": %s", problem);
645 }
646
647 return android::base::StringPrintf("%s.\nSee [%s] for more information.",
648 header.c_str(), kPermissionsHelpUrl);
649}