The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
| 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 Cui | aed3c61 | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG USB |
Dan Albert | 3313426 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 25 | #include <linux/usb/ch9.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <linux/usbdevice_fs.h> |
| 27 | #include <linux/version.h> |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 28 | #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 Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 35 | |
| 36 | #include <chrono> |
| 37 | #include <condition_variable> |
| 38 | #include <list> |
| 39 | #include <mutex> |
| 40 | #include <string> |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 41 | #include <thread> |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/file.h> |
| 44 | #include <android-base/stringprintf.h> |
| 45 | #include <android-base/strings.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include "adb.h" |
Dan Albert | 7664901 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 48 | #include "transport.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 50 | using namespace std::chrono_literals; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 51 | using namespace std::literals; |
| 52 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 53 | /* usb scan debugging is waaaay too verbose */ |
| 54 | #define DBGX(x...) |
| 55 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 56 | struct usb_handle { |
| 57 | ~usb_handle() { |
| 58 | if (fd != -1) unix_close(fd); |
| 59 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 60 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 61 | std::string path; |
| 62 | int fd = -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 63 | unsigned char ep_in; |
| 64 | unsigned char ep_out; |
| 65 | |
| 66 | unsigned zero_mask; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 67 | unsigned writeable = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 68 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 69 | usbdevfs_urb urb_in; |
| 70 | usbdevfs_urb urb_out; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 72 | bool urb_in_busy = false; |
| 73 | bool urb_out_busy = false; |
| 74 | bool dead = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 76 | std::condition_variable cv; |
| 77 | std::mutex mutex; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | |
| 79 | // for garbage collecting disconnected devices |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 80 | bool mark; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | |
| 82 | // ID of thread currently in REAPURB |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 83 | pthread_t reaper_thread = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
Josh Gao | b7b1edf | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 86 | static auto& g_usb_handles_mutex = *new std::mutex(); |
| 87 | static auto& g_usb_handles = *new std::list<usb_handle*>(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 88 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 89 | static 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | // set mark flag to indicate this device is still alive |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 94 | usb->mark = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 101 | static void kick_disconnected_devices() { |
| 102 | std::lock_guard<std::mutex> lock(g_usb_handles_mutex); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | // kick any devices in the device list that were not found in the device scan |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 104 | for (usb_handle* usb : g_usb_handles) { |
| 105 | if (!usb->mark) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | usb_kick(usb); |
| 107 | } else { |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 108 | usb->mark = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | } |
| 110 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 113 | static inline bool contains_non_digit(const char* name) { |
| 114 | while (*name) { |
| 115 | if (!isdigit(*name++)) return true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | } |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 117 | return false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 120 | static void find_usb_device(const std::string& base, |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 121 | void (*register_device_callback) |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 122 | (const char*, const char*, unsigned char, unsigned char, int, int, unsigned)) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 123 | { |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 124 | std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir); |
| 125 | if (!bus_dir) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 126 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 127 | dirent* de; |
| 128 | while ((de = readdir(bus_dir.get())) != 0) { |
| 129 | if (contains_non_digit(de->d_name)) continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 131 | std::string bus_name = base + "/" + de->d_name; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 132 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 133 | std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir); |
| 134 | if (!dev_dir) continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 136 | while ((de = readdir(dev_dir.get()))) { |
Mike Lockwood | b596608 | 2011-01-07 23:18:14 -0500 | [diff] [blame] | 137 | unsigned char devdesc[4096]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | unsigned char* bufptr = devdesc; |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 139 | unsigned char* bufend; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 147 | if (contains_non_digit(de->d_name)) continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 149 | std::string dev_name = bus_name + "/" + de->d_name; |
| 150 | if (is_known_device(dev_name.c_str())) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | continue; |
| 152 | } |
| 153 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 154 | int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC); |
| 155 | if (fd == -1) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 156 | continue; |
| 157 | } |
| 158 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 159 | size_t desclength = unix_read(fd, devdesc, sizeof(devdesc)); |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 160 | bufend = bufptr + desclength; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | |
| 162 | // should have device and configuration descriptors, and atleast two endpoints |
| 163 | if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 164 | D("desclength %zu is too small", desclength); |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 165 | unix_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 166 | 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 Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 173 | unix_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | continue; |
| 175 | } |
| 176 | |
Marcus Comstedt | 6f703a2 | 2010-09-22 20:09:44 +0200 | [diff] [blame] | 177 | vid = device->idVendor; |
| 178 | pid = device->idProduct; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 179 | DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | |
| 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 Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 185 | D("usb_config_descriptor not found"); |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 186 | unix_close(fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 187 | continue; |
| 188 | } |
| 189 | |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 190 | // 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 194 | |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 195 | if (type == USB_DT_INTERFACE) { |
| 196 | interface = (struct usb_interface_descriptor *)bufptr; |
| 197 | bufptr += length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 199 | if (length != USB_DT_INTERFACE_SIZE) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 200 | D("interface descriptor has wrong size"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 201 | break; |
| 202 | } |
| 203 | |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 204 | 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 Gao | 30186df | 2016-09-26 21:18:58 -0700 | [diff] [blame] | 210 | is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass, |
| 211 | interface->bInterfaceProtocol)) { |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 212 | struct stat st; |
| 213 | char pathbuf[128]; |
| 214 | char link[256]; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 215 | char *devpath = nullptr; |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 216 | |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 217 | DBGX("looking for bulk endpoints\n"); |
| 218 | // looks like ADB... |
| 219 | ep1 = (struct usb_endpoint_descriptor *)bufptr; |
| 220 | bufptr += USB_DT_ENDPOINT_SIZE; |
Ingo Rohloff | 58b01e0 | 2014-05-16 21:51:41 +0200 | [diff] [blame] | 221 | // 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 Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 228 | ep2 = (struct usb_endpoint_descriptor *)bufptr; |
| 229 | bufptr += USB_DT_ENDPOINT_SIZE; |
Ingo Rohloff | 58b01e0 | 2014-05-16 21:51:41 +0200 | [diff] [blame] | 230 | 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 Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 235 | |
| 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 Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 241 | D("endpoints not found"); |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 242 | 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 Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 248 | D("bulk endpoints not found"); |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 249 | 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 Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 257 | unsigned char local_ep_in, local_ep_out; |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 258 | 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 Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 266 | // Determine the device path |
| 267 | if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) { |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 268 | snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d", |
| 269 | major(st.st_rdev), minor(st.st_rdev)); |
Elliott Hughes | 3e7048c | 2015-07-27 21:21:39 -0700 | [diff] [blame] | 270 | ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 271 | if (link_len > 0) { |
| 272 | link[link_len] = '\0'; |
Elliott Hughes | 3e7048c | 2015-07-27 21:21:39 -0700 | [diff] [blame] | 273 | const char* slash = strrchr(link, '/'); |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 274 | if (slash) { |
| 275 | snprintf(pathbuf, sizeof(pathbuf), |
| 276 | "usb:%s", slash + 1); |
| 277 | devpath = pathbuf; |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 282 | register_device_callback(dev_name.c_str(), devpath, |
Scott Anderson | e109d26 | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 283 | local_ep_in, local_ep_out, |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 284 | interface->bInterfaceNumber, device->iSerialNumber, zero_mask); |
| 285 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 286 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 287 | } else { |
Mike Lockwood | 07e8f7e | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 288 | bufptr += length; |
| 289 | } |
| 290 | } // end of while |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | |
Spencer Low | 6ac5d7d | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 292 | unix_close(fd); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 297 | static int usb_bulk_write(usb_handle* h, const void* data, int len) { |
| 298 | std::unique_lock<std::mutex> lock(h->mutex); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 299 | D("++ usb_bulk_write ++"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 301 | usbdevfs_urb* urb = &h->urb_out; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 302 | memset(urb, 0, sizeof(*urb)); |
| 303 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 304 | urb->endpoint = h->ep_out; |
| 305 | urb->status = -1; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 306 | urb->buffer = const_cast<void*>(data); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | urb->buffer_length = len; |
| 308 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 309 | if (h->dead) { |
| 310 | errno = EINVAL; |
| 311 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | } |
| 313 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 314 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 325 | } |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 326 | if (!h->urb_out_busy) { |
| 327 | if (urb->status != 0) { |
| 328 | errno = -urb->status; |
| 329 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | } |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 331 | return urb->actual_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 336 | static int usb_bulk_read(usb_handle* h, void* data, int len) { |
| 337 | std::unique_lock<std::mutex> lock(h->mutex); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 338 | D("++ usb_bulk_read ++"); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 339 | |
| 340 | usbdevfs_urb* urb = &h->urb_in; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 341 | 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 Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 348 | if (h->dead) { |
| 349 | errno = EINVAL; |
| 350 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 353 | 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 Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 359 | D("[ reap urb - wait ]"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 360 | h->reaper_thread = pthread_self(); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 361 | 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 Abgrall | 408fa57 | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 367 | int saved_errno = errno; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 368 | |
| 369 | lock.lock(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 370 | h->reaper_thread = 0; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 371 | if (h->dead) { |
| 372 | errno = EINVAL; |
| 373 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 374 | } |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 375 | if (res < 0) { |
| 376 | if (saved_errno == EINTR) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 377 | continue; |
| 378 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 379 | D("[ reap urb - error ]"); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 380 | errno = saved_errno; |
| 381 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 383 | D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 384 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 385 | if (out == &h->urb_in) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 386 | D("[ reap urb - IN complete ]"); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 387 | h->urb_in_busy = false; |
| 388 | if (urb->status != 0) { |
| 389 | errno = -urb->status; |
| 390 | return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 391 | } |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 392 | return urb->actual_length; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 393 | } |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 394 | if (out == &h->urb_out) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 395 | D("[ reap urb - OUT compelete ]"); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 396 | h->urb_out_busy = false; |
| 397 | h->cv.notify_all(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | } |
| 399 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | |
| 403 | int usb_write(usb_handle *h, const void *_data, int len) |
| 404 | { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 405 | D("++ usb_write ++"); |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 406 | |
| 407 | unsigned char *data = (unsigned char*) _data; |
| 408 | int n = usb_bulk_write(h, data, len); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 409 | if (n != len) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 410 | D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno)); |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 411 | return -1; |
| 412 | } |
| 413 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 414 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 420 | D("-- usb_write --"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | int usb_read(usb_handle *h, void *_data, int len) |
| 425 | { |
| 426 | unsigned char *data = (unsigned char*) _data; |
| 427 | int n; |
| 428 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 429 | D("++ usb_read ++"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 430 | while(len > 0) { |
Tamas Berghammer | 3d2904c | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 431 | int xfer = len; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 432 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 433 | D("[ usb read %d fd = %d], path=%s", xfer, h->fd, h->path.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 434 | n = usb_bulk_read(h, data, xfer); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 435 | D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 436 | if(n != xfer) { |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 437 | if((errno == ETIMEDOUT) && (h->fd != -1)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 438 | D("[ timeout ]"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | if(n > 0){ |
| 440 | data += n; |
| 441 | len -= n; |
| 442 | } |
| 443 | continue; |
| 444 | } |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 445 | D("ERROR: n = %d, errno = %d (%s)", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | n, errno, strerror(errno)); |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | len -= xfer; |
| 451 | data += xfer; |
| 452 | } |
| 453 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 454 | D("-- usb_read --"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | return 0; |
| 456 | } |
| 457 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 458 | void usb_kick(usb_handle* h) { |
| 459 | std::lock_guard<std::mutex> lock(h->mutex); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 460 | D("[ kicking %p (fd = %d) ]", h, h->fd); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 461 | if (!h->dead) { |
| 462 | h->dead = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 463 | |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 464 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 472 | |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 473 | /* 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 Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 478 | ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in); |
| 479 | ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 480 | h->urb_in.status = -ENODEV; |
| 481 | h->urb_out.status = -ENODEV; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 482 | h->urb_in_busy = false; |
| 483 | h->urb_out_busy = false; |
| 484 | h->cv.notify_all(); |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 485 | } else { |
| 486 | unregister_usb_transport(h); |
| 487 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 488 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 491 | int 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 494 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 495 | D("-- usb close %p (fd = %d) --", h, h->fd); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 496 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 497 | delete h; |
| 498 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 499 | return 0; |
| 500 | } |
| 501 | |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 502 | static void register_device(const char* dev_name, const char* dev_path, |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 503 | unsigned char ep_in, unsigned char ep_out, |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 504 | 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 Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 511 | // If we have a usb_handle on the list of handles with a matching name, we |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 512 | // have no further work to do. |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 513 | { |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 519 | } |
| 520 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 521 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 522 | D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface); |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 523 | std::unique_ptr<usb_handle> usb(new usb_handle); |
| 524 | usb->path = dev_name; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 525 | usb->ep_in = ep_in; |
| 526 | usb->ep_out = ep_out; |
| 527 | usb->zero_mask = zero_mask; |
| 528 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 529 | // Initialize mark so we don't get garbage collected after the device scan. |
| 530 | usb->mark = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 531 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 532 | usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC); |
| 533 | if (usb->fd == -1) { |
Elliott Hughes | ce6363b | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 534 | // Opening RW failed, so see if we have RO access. |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 535 | usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC); |
| 536 | if (usb->fd == -1) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 537 | D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno)); |
Elliott Hughes | ce6363b | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 538 | return; |
| 539 | } |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 540 | usb->writeable = 0; |
Elliott Hughes | ce6363b | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 543 | D("[ usb opened %s%s, fd=%d]", |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 544 | usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd); |
Elliott Hughes | ce6363b | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 545 | |
| 546 | if (usb->writeable) { |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 547 | if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 548 | D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno)); |
Elliott Hughes | ce6363b | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 549 | return; |
| 550 | } |
Mike Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 551 | } |
| 552 | |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 553 | // Read the device's serial number. |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 554 | std::string serial_path = android::base::StringPrintf( |
| 555 | "/sys/bus/usb/devices/%s/serial", dev_path + 4); |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 556 | std::string serial; |
| 557 | if (!android::base::ReadFileToString(serial_path, &serial)) { |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 558 | D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno)); |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 559 | // 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 Lockwood | 0927bf9 | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 563 | } |
Elliott Hughes | 9309ecb | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 564 | serial = android::base::Trim(serial); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 566 | // Add to the end of the active handles. |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 567 | 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Josh Gao | b5fea14 | 2016-02-12 14:31:15 -0800 | [diff] [blame] | 575 | static void device_poll_thread(void*) { |
Siva Velusamy | 49ee7cf | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 576 | adb_thread_setname("device poll"); |
Yabin Cui | 7a3f8d6 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 577 | D("Created device thread"); |
Dan Albert | d99d902 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 578 | while (true) { |
| 579 | // TODO: Use inotify. |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 580 | find_usb_device("/dev/bus/usb", register_device); |
| 581 | kick_disconnected_devices(); |
Elliott Hughes | dbe91ee | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 582 | std::this_thread::sleep_for(1s); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 583 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 586 | void usb_init() { |
| 587 | struct sigaction actions; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 588 | memset(&actions, 0, sizeof(actions)); |
| 589 | sigemptyset(&actions.sa_mask); |
| 590 | actions.sa_flags = 0; |
Elliott Hughes | 812f030 | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 591 | actions.sa_handler = [](int) {}; |
| 592 | sigaction(SIGALRM, &actions, nullptr); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 593 | |
Elliott Hughes | 9b0f354 | 2015-05-05 13:41:21 -0700 | [diff] [blame] | 594 | if (!adb_thread_create(device_poll_thread, nullptr)) { |
Yabin Cui | d6ab3c2 | 2015-08-31 11:50:24 -0700 | [diff] [blame] | 595 | fatal_errno("cannot create device_poll thread"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 596 | } |
| 597 | } |