blob: e570ef599dbd25a459227848f8596594b733239e [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
Dan Albert33134262015-03-19 15:21:08 -070017#define TRACE_TAG TRACE_USB
18
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 Hughes9309ecb2015-04-27 14:20:17 -070041
42#include <base/file.h>
43#include <base/stringprintf.h>
44#include <base/strings.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080045
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046#include "adb.h"
Dan Albert76649012015-02-24 15:51:19 -080047#include "transport.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
Elliott Hughes812f0302015-07-23 15:20:09 -070049using namespace std::literals;
50
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051/* usb scan debugging is waaaay too verbose */
52#define DBGX(x...)
53
Elliott Hughes812f0302015-07-23 15:20:09 -070054struct usb_handle {
55 ~usb_handle() {
56 if (fd != -1) unix_close(fd);
57 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080058
Elliott Hughes812f0302015-07-23 15:20:09 -070059 std::string path;
60 int fd = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061 unsigned char ep_in;
62 unsigned char ep_out;
63
64 unsigned zero_mask;
Elliott Hughes812f0302015-07-23 15:20:09 -070065 unsigned writeable = 1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066
Elliott Hughes812f0302015-07-23 15:20:09 -070067 usbdevfs_urb urb_in;
68 usbdevfs_urb urb_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
Elliott Hughes812f0302015-07-23 15:20:09 -070070 bool urb_in_busy = false;
71 bool urb_out_busy = false;
72 bool dead = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073
Elliott Hughes812f0302015-07-23 15:20:09 -070074 std::condition_variable cv;
75 std::mutex mutex;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076
77 // for garbage collecting disconnected devices
Elliott Hughes812f0302015-07-23 15:20:09 -070078 bool mark;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079
80 // ID of thread currently in REAPURB
Elliott Hughes812f0302015-07-23 15:20:09 -070081 pthread_t reaper_thread = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082};
83
Elliott Hughes812f0302015-07-23 15:20:09 -070084static std::mutex g_usb_handles_mutex;
85static std::list<usb_handle*> g_usb_handles;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080086
Elliott Hughes812f0302015-07-23 15:20:09 -070087static int is_known_device(const char* dev_name) {
88 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
89 for (usb_handle* usb : g_usb_handles) {
90 if (usb->path == dev_name) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091 // set mark flag to indicate this device is still alive
Elliott Hughes812f0302015-07-23 15:20:09 -070092 usb->mark = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080093 return 1;
94 }
95 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 return 0;
97}
98
Elliott Hughes812f0302015-07-23 15:20:09 -070099static void kick_disconnected_devices() {
100 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101 // kick any devices in the device list that were not found in the device scan
Elliott Hughes812f0302015-07-23 15:20:09 -0700102 for (usb_handle* usb : g_usb_handles) {
103 if (!usb->mark) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 usb_kick(usb);
105 } else {
Elliott Hughes812f0302015-07-23 15:20:09 -0700106 usb->mark = false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 }
108 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109}
110
Elliott Hughes812f0302015-07-23 15:20:09 -0700111static inline bool contains_non_digit(const char* name) {
112 while (*name) {
113 if (!isdigit(*name++)) return true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800114 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700115 return false;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116}
117
Elliott Hughes812f0302015-07-23 15:20:09 -0700118static void find_usb_device(const std::string& base,
Mike Lockwood0927bf92009-08-08 12:37:44 -0400119 void (*register_device_callback)
Elliott Hughes812f0302015-07-23 15:20:09 -0700120 (const char*, const char*, unsigned char, unsigned char, int, int, unsigned))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121{
Elliott Hughes812f0302015-07-23 15:20:09 -0700122 std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir);
123 if (!bus_dir) return;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800124
Elliott Hughes812f0302015-07-23 15:20:09 -0700125 dirent* de;
126 while ((de = readdir(bus_dir.get())) != 0) {
127 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128
Elliott Hughes812f0302015-07-23 15:20:09 -0700129 std::string bus_name = base + "/" + de->d_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130
Elliott Hughes812f0302015-07-23 15:20:09 -0700131 std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir);
132 if (!dev_dir) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133
Elliott Hughes812f0302015-07-23 15:20:09 -0700134 while ((de = readdir(dev_dir.get()))) {
Mike Lockwoodb5966082011-01-07 23:18:14 -0500135 unsigned char devdesc[4096];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 unsigned char* bufptr = devdesc;
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500137 unsigned char* bufend;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138 struct usb_device_descriptor* device;
139 struct usb_config_descriptor* config;
140 struct usb_interface_descriptor* interface;
141 struct usb_endpoint_descriptor *ep1, *ep2;
142 unsigned zero_mask = 0;
143 unsigned vid, pid;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144
Elliott Hughes812f0302015-07-23 15:20:09 -0700145 if (contains_non_digit(de->d_name)) continue;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800146
Elliott Hughes812f0302015-07-23 15:20:09 -0700147 std::string dev_name = bus_name + "/" + de->d_name;
148 if (is_known_device(dev_name.c_str())) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 continue;
150 }
151
Elliott Hughes812f0302015-07-23 15:20:09 -0700152 int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC);
153 if (fd == -1) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154 continue;
155 }
156
Elliott Hughes812f0302015-07-23 15:20:09 -0700157 size_t desclength = unix_read(fd, devdesc, sizeof(devdesc));
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500158 bufend = bufptr + desclength;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159
160 // should have device and configuration descriptors, and atleast two endpoints
161 if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
Elliott Hughesccecf142014-01-16 10:53:11 -0800162 D("desclength %zu is too small\n", desclength);
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700163 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 continue;
165 }
166
167 device = (struct usb_device_descriptor*)bufptr;
168 bufptr += USB_DT_DEVICE_SIZE;
169
170 if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700171 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 continue;
173 }
174
Marcus Comstedt6f703a22010-09-22 20:09:44 +0200175 vid = device->idVendor;
176 pid = device->idProduct;
Elliott Hughes812f0302015-07-23 15:20:09 -0700177 DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178
179 // should have config descriptor next
180 config = (struct usb_config_descriptor *)bufptr;
181 bufptr += USB_DT_CONFIG_SIZE;
182 if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
183 D("usb_config_descriptor not found\n");
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700184 unix_close(fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800185 continue;
186 }
187
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500188 // loop through all the descriptors and look for the ADB interface
189 while (bufptr < bufend) {
190 unsigned char length = bufptr[0];
191 unsigned char type = bufptr[1];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500193 if (type == USB_DT_INTERFACE) {
194 interface = (struct usb_interface_descriptor *)bufptr;
195 bufptr += length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500197 if (length != USB_DT_INTERFACE_SIZE) {
198 D("interface descriptor has wrong size\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800199 break;
200 }
201
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500202 DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
203 "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
204 interface->bInterfaceClass, interface->bInterfaceSubClass,
205 interface->bInterfaceProtocol, interface->bNumEndpoints);
206
207 if (interface->bNumEndpoints == 2 &&
208 is_adb_interface(vid, pid, interface->bInterfaceClass,
209 interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
210
Scott Andersone109d262012-04-20 11:21:14 -0700211 struct stat st;
212 char pathbuf[128];
213 char link[256];
Elliott Hughes812f0302015-07-23 15:20:09 -0700214 char *devpath = nullptr;
Scott Andersone109d262012-04-20 11:21:14 -0700215
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500216 DBGX("looking for bulk endpoints\n");
217 // looks like ADB...
218 ep1 = (struct usb_endpoint_descriptor *)bufptr;
219 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff58b01e02014-05-16 21:51:41 +0200220 // For USB 3.0 SuperSpeed devices, skip potential
221 // USB 3.0 SuperSpeed Endpoint Companion descriptor
222 if (bufptr+2 <= devdesc + desclength &&
223 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
224 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
225 bufptr += USB_DT_SS_EP_COMP_SIZE;
226 }
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500227 ep2 = (struct usb_endpoint_descriptor *)bufptr;
228 bufptr += USB_DT_ENDPOINT_SIZE;
Ingo Rohloff58b01e02014-05-16 21:51:41 +0200229 if (bufptr+2 <= devdesc + desclength &&
230 bufptr[0] == USB_DT_SS_EP_COMP_SIZE &&
231 bufptr[1] == USB_DT_SS_ENDPOINT_COMP) {
232 bufptr += USB_DT_SS_EP_COMP_SIZE;
233 }
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500234
235 if (bufptr > devdesc + desclength ||
236 ep1->bLength != USB_DT_ENDPOINT_SIZE ||
237 ep1->bDescriptorType != USB_DT_ENDPOINT ||
238 ep2->bLength != USB_DT_ENDPOINT_SIZE ||
239 ep2->bDescriptorType != USB_DT_ENDPOINT) {
240 D("endpoints not found\n");
241 break;
242 }
243
244 // both endpoints should be bulk
245 if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
246 ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
247 D("bulk endpoints not found\n");
248 continue;
249 }
250 /* aproto 01 needs 0 termination */
251 if(interface->bInterfaceProtocol == 0x01) {
252 zero_mask = ep1->wMaxPacketSize - 1;
253 }
254
255 // 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 -0700256 unsigned char local_ep_in, local_ep_out;
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500257 if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
258 local_ep_in = ep1->bEndpointAddress;
259 local_ep_out = ep2->bEndpointAddress;
260 } else {
261 local_ep_in = ep2->bEndpointAddress;
262 local_ep_out = ep1->bEndpointAddress;
263 }
264
Scott Andersone109d262012-04-20 11:21:14 -0700265 // Determine the device path
266 if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
267 char *slash;
268 ssize_t link_len;
269 snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
270 major(st.st_rdev), minor(st.st_rdev));
271 link_len = readlink(pathbuf, link, sizeof(link) - 1);
272 if (link_len > 0) {
273 link[link_len] = '\0';
274 slash = strrchr(link, '/');
275 if (slash) {
276 snprintf(pathbuf, sizeof(pathbuf),
277 "usb:%s", slash + 1);
278 devpath = pathbuf;
279 }
280 }
281 }
282
Elliott Hughes812f0302015-07-23 15:20:09 -0700283 register_device_callback(dev_name.c_str(), devpath,
Scott Andersone109d262012-04-20 11:21:14 -0700284 local_ep_in, local_ep_out,
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500285 interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
286 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 } else {
Mike Lockwood07e8f7e2010-01-17 00:52:27 -0500289 bufptr += length;
290 }
291 } // end of while
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292
Spencer Low6ac5d7d2015-05-22 20:09:06 -0700293 unix_close(fd);
Elliott Hughes812f0302015-07-23 15:20:09 -0700294 }
295 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800296}
297
Elliott Hughes812f0302015-07-23 15:20:09 -0700298static int usb_bulk_write(usb_handle* h, const void* data, int len) {
299 std::unique_lock<std::mutex> lock(h->mutex);
300 D("++ usb_bulk_write ++\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800301
Elliott Hughes812f0302015-07-23 15:20:09 -0700302 usbdevfs_urb* urb = &h->urb_out;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800303 memset(urb, 0, sizeof(*urb));
304 urb->type = USBDEVFS_URB_TYPE_BULK;
305 urb->endpoint = h->ep_out;
306 urb->status = -1;
Elliott Hughes812f0302015-07-23 15:20:09 -0700307 urb->buffer = const_cast<void*>(data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308 urb->buffer_length = len;
309
Elliott Hughes812f0302015-07-23 15:20:09 -0700310 if (h->dead) {
311 errno = EINVAL;
312 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 }
314
Elliott Hughes812f0302015-07-23 15:20:09 -0700315 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
316 return -1;
317 }
318
319 h->urb_out_busy = true;
320 while (true) {
321 auto now = std::chrono::system_clock::now();
322 if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) {
323 // TODO: call USBDEVFS_DISCARDURB?
324 errno = ETIMEDOUT;
325 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700327 if (!h->urb_out_busy) {
328 if (urb->status != 0) {
329 errno = -urb->status;
330 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700332 return urb->actual_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800333 }
334 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335}
336
Elliott Hughes812f0302015-07-23 15:20:09 -0700337static int usb_bulk_read(usb_handle* h, void* data, int len) {
338 std::unique_lock<std::mutex> lock(h->mutex);
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100339 D("++ usb_bulk_read ++\n");
Elliott Hughes812f0302015-07-23 15:20:09 -0700340
341 usbdevfs_urb* urb = &h->urb_in;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342 memset(urb, 0, sizeof(*urb));
343 urb->type = USBDEVFS_URB_TYPE_BULK;
344 urb->endpoint = h->ep_in;
345 urb->status = -1;
346 urb->buffer = data;
347 urb->buffer_length = len;
348
Elliott Hughes812f0302015-07-23 15:20:09 -0700349 if (h->dead) {
350 errno = EINVAL;
351 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 }
353
Elliott Hughes812f0302015-07-23 15:20:09 -0700354 if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) {
355 return -1;
356 }
357
358 h->urb_in_busy = true;
359 while (true) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800360 D("[ reap urb - wait ]\n");
361 h->reaper_thread = pthread_self();
Elliott Hughes812f0302015-07-23 15:20:09 -0700362 int fd = h->fd;
363 lock.unlock();
364
365 // This ioctl must not have TEMP_FAILURE_RETRY because we send SIGALRM to break out.
366 usbdevfs_urb* out = nullptr;
367 int res = ioctl(fd, USBDEVFS_REAPURB, &out);
JP Abgrall408fa572011-03-16 15:57:42 -0700368 int saved_errno = errno;
Elliott Hughes812f0302015-07-23 15:20:09 -0700369
370 lock.lock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371 h->reaper_thread = 0;
Elliott Hughes812f0302015-07-23 15:20:09 -0700372 if (h->dead) {
373 errno = EINVAL;
374 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700376 if (res < 0) {
377 if (saved_errno == EINTR) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 continue;
379 }
380 D("[ reap urb - error ]\n");
Elliott Hughes812f0302015-07-23 15:20:09 -0700381 errno = saved_errno;
382 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700384 D("[ urb @%p status = %d, actual = %d ]\n", out, out->status, out->actual_length);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
Elliott Hughes812f0302015-07-23 15:20:09 -0700386 if (out == &h->urb_in) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800387 D("[ reap urb - IN complete ]\n");
Elliott Hughes812f0302015-07-23 15:20:09 -0700388 h->urb_in_busy = false;
389 if (urb->status != 0) {
390 errno = -urb->status;
391 return -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700393 return urb->actual_length;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394 }
Elliott Hughes812f0302015-07-23 15:20:09 -0700395 if (out == &h->urb_out) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396 D("[ reap urb - OUT compelete ]\n");
Elliott Hughes812f0302015-07-23 15:20:09 -0700397 h->urb_out_busy = false;
398 h->cv.notify_all();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800399 }
400 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800401}
402
403
404int usb_write(usb_handle *h, const void *_data, int len)
405{
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100406 D("++ usb_write ++\n");
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100407
408 unsigned char *data = (unsigned char*) _data;
409 int n = usb_bulk_write(h, data, len);
Elliott Hughes812f0302015-07-23 15:20:09 -0700410 if (n != len) {
411 D("ERROR: n = %d, errno = %d (%s)\n", n, errno, strerror(errno));
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100412 return -1;
413 }
414
Elliott Hughes812f0302015-07-23 15:20:09 -0700415 if (h->zero_mask && !(len & h->zero_mask)) {
416 // If we need 0-markers and our transfer is an even multiple of the packet size,
417 // then send a zero marker.
418 return usb_bulk_write(h, _data, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419 }
420
Nanik Tolaramb627a0e2015-02-18 22:53:37 +1100421 D("-- usb_write --\n");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422 return 0;
423}
424
425int usb_read(usb_handle *h, void *_data, int len)
426{
427 unsigned char *data = (unsigned char*) _data;
428 int n;
429
430 D("++ usb_read ++\n");
431 while(len > 0) {
Tamas Berghammer3d2904c2015-07-13 19:12:28 +0100432 int xfer = len;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433
Elliott Hughes812f0302015-07-23 15:20:09 -0700434 D("[ usb read %d fd = %d], path=%s\n", xfer, h->fd, h->path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435 n = usb_bulk_read(h, data, xfer);
Elliott Hughes812f0302015-07-23 15:20:09 -0700436 D("[ usb read %d ] = %d, path=%s\n", xfer, n, h->path.c_str());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437 if(n != xfer) {
Elliott Hughes812f0302015-07-23 15:20:09 -0700438 if((errno == ETIMEDOUT) && (h->fd != -1)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800439 D("[ timeout ]\n");
440 if(n > 0){
441 data += n;
442 len -= n;
443 }
444 continue;
445 }
446 D("ERROR: n = %d, errno = %d (%s)\n",
447 n, errno, strerror(errno));
448 return -1;
449 }
450
451 len -= xfer;
452 data += xfer;
453 }
454
455 D("-- usb_read --\n");
456 return 0;
457}
458
Elliott Hughes812f0302015-07-23 15:20:09 -0700459void usb_kick(usb_handle* h) {
460 std::lock_guard<std::mutex> lock(h->mutex);
461 D("[ kicking %p (fd = %d) ]\n", h, h->fd);
462 if (!h->dead) {
463 h->dead = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800464
Mike Lockwood0927bf92009-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 Projectdd7bc332009-03-03 19:32:55 -0800473
Mike Lockwood0927bf92009-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 Hughes812f0302015-07-23 15:20:09 -0700479 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in);
480 ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out);
Mike Lockwood0927bf92009-08-08 12:37:44 -0400481 h->urb_in.status = -ENODEV;
482 h->urb_out.status = -ENODEV;
Elliott Hughes812f0302015-07-23 15:20:09 -0700483 h->urb_in_busy = false;
484 h->urb_out_busy = false;
485 h->cv.notify_all();
Mike Lockwood0927bf92009-08-08 12:37:44 -0400486 } else {
487 unregister_usb_transport(h);
488 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490}
491
Elliott Hughes812f0302015-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 Projectdd7bc332009-03-03 19:32:55 -0800495
Elliott Hughes812f0302015-07-23 15:20:09 -0700496 D("-- usb close %p (fd = %d) --\n", h, h->fd);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800497
Elliott Hughes812f0302015-07-23 15:20:09 -0700498 delete h;
499
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800500 return 0;
501}
502
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700503static void register_device(const char* dev_name, const char* dev_path,
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800504 unsigned char ep_in, unsigned char ep_out,
Dan Albertd99d9022015-05-06 16:48:52 -0700505 int interface, int serial_index,
506 unsigned zero_mask) {
507 // Since Linux will not reassign the device ID (and dev_name) as long as the
508 // device is open, we can add to the list here once we open it and remove
509 // from the list when we're finally closed and everything will work out
510 // fine.
511 //
Elliott Hughes812f0302015-07-23 15:20:09 -0700512 // If we have a usb_handle on the list of handles with a matching name, we
Dan Albertd99d9022015-05-06 16:48:52 -0700513 // have no further work to do.
Elliott Hughes812f0302015-07-23 15:20:09 -0700514 {
515 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
516 for (usb_handle* usb: g_usb_handles) {
517 if (usb->path == dev_name) {
518 return;
519 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800520 }
521 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800522
Elliott Hughesce6363b2015-04-25 14:44:23 -0700523 D("[ usb located new device %s (%d/%d/%d) ]\n", dev_name, ep_in, ep_out, interface);
Elliott Hughes812f0302015-07-23 15:20:09 -0700524 std::unique_ptr<usb_handle> usb(new usb_handle);
525 usb->path = dev_name;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800526 usb->ep_in = ep_in;
527 usb->ep_out = ep_out;
528 usb->zero_mask = zero_mask;
529
Elliott Hughes812f0302015-07-23 15:20:09 -0700530 // Initialize mark so we don't get garbage collected after the device scan.
531 usb->mark = true;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800532
Elliott Hughes812f0302015-07-23 15:20:09 -0700533 usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC);
534 if (usb->fd == -1) {
Elliott Hughesce6363b2015-04-25 14:44:23 -0700535 // Opening RW failed, so see if we have RO access.
Elliott Hughes812f0302015-07-23 15:20:09 -0700536 usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC);
537 if (usb->fd == -1) {
538 D("[ usb open %s failed: %s]\n", usb->path.c_str(), strerror(errno));
Elliott Hughesce6363b2015-04-25 14:44:23 -0700539 return;
540 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400541 usb->writeable = 0;
Elliott Hughesce6363b2015-04-25 14:44:23 -0700542 }
543
Elliott Hughes812f0302015-07-23 15:20:09 -0700544 D("[ usb opened %s%s, fd=%d]\n",
545 usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd);
Elliott Hughesce6363b2015-04-25 14:44:23 -0700546
547 if (usb->writeable) {
Elliott Hughes812f0302015-07-23 15:20:09 -0700548 if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) {
549 D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]\n", usb->fd, strerror(errno));
Elliott Hughesce6363b2015-04-25 14:44:23 -0700550 return;
551 }
Mike Lockwood0927bf92009-08-08 12:37:44 -0400552 }
553
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700554 // Read the device's serial number.
Dan Albertd99d9022015-05-06 16:48:52 -0700555 std::string serial_path = android::base::StringPrintf(
556 "/sys/bus/usb/devices/%s/serial", dev_path + 4);
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700557 std::string serial;
558 if (!android::base::ReadFileToString(serial_path, &serial)) {
559 D("[ usb read %s failed: %s ]\n", serial_path.c_str(), strerror(errno));
Dan Albertd99d9022015-05-06 16:48:52 -0700560 // We don't actually want to treat an unknown serial as an error because
561 // devices aren't able to communicate a serial number in early bringup.
562 // http://b/20883914
563 serial = "";
Mike Lockwood0927bf92009-08-08 12:37:44 -0400564 }
Elliott Hughes9309ecb2015-04-27 14:20:17 -0700565 serial = android::base::Trim(serial);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800566
Dan Albertd99d9022015-05-06 16:48:52 -0700567 // Add to the end of the active handles.
Elliott Hughes812f0302015-07-23 15:20:09 -0700568 usb_handle* done_usb = usb.release();
569 {
570 std::lock_guard<std::mutex> lock(g_usb_handles_mutex);
571 g_usb_handles.push_back(done_usb);
572 }
573 register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800574}
575
Dan Albertd99d9022015-05-06 16:48:52 -0700576static void* device_poll_thread(void* unused) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800577 D("Created device thread\n");
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)) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800596 fatal_errno("cannot create input thread");
597 }
598}