The Android Open Source Project | 9ca14dc | 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 | 19bec5b | 2015-09-22 15:52:57 -0700 | [diff] [blame] | 17 | #define TRACE_TAG USB |
Dan Albert | db6fe64 | 2015-03-19 15:21:08 -0700 | [diff] [blame] | 18 | |
| 19 | #include "sysdeps.h" |
| 20 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <ctype.h> |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <errno.h> |
| 24 | #include <fcntl.h> |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 25 | #include <linux/usb/ch9.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | #include <linux/usbdevice_fs.h> |
| 27 | #include <linux/version.h> |
Dan Albert | b302d12 | 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 | 3af421c | 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 | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 41 | #include <thread> |
Elliott Hughes | 949f262 | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | f55ead9 | 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 | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 47 | #include "adb.h" |
Dan Albert | b302d12 | 2015-02-24 15:51:19 -0800 | [diff] [blame] | 48 | #include "transport.h" |
Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 49 | #include "usb.h" |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 51 | using namespace std::chrono_literals; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 52 | using namespace std::literals; |
| 53 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | /* usb scan debugging is waaaay too verbose */ |
| 55 | #define DBGX(x...) |
| 56 | |
Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 57 | namespace native { |
| 58 | struct usb_handle : public ::usb_handle { |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 59 | ~usb_handle() { |
| 60 | if (fd != -1) unix_close(fd); |
| 61 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 62 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 63 | std::string path; |
| 64 | int fd = -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | unsigned char ep_in; |
| 66 | unsigned char ep_out; |
| 67 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 68 | size_t max_packet_size; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 69 | unsigned zero_mask; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 70 | unsigned writeable = 1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 72 | usbdevfs_urb urb_in; |
| 73 | usbdevfs_urb urb_out; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 75 | bool urb_in_busy = false; |
| 76 | bool urb_out_busy = false; |
| 77 | bool dead = false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 78 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 79 | std::condition_variable cv; |
| 80 | std::mutex mutex; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | |
| 82 | // for garbage collecting disconnected devices |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 83 | bool mark; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 84 | |
| 85 | // ID of thread currently in REAPURB |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 86 | pthread_t reaper_thread = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Josh Gao | e3a87d0 | 2015-11-11 17:56:12 -0800 | [diff] [blame] | 89 | static auto& g_usb_handles_mutex = *new std::mutex(); |
| 90 | static auto& g_usb_handles = *new std::list<usb_handle*>(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 91 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 92 | static int is_known_device(const char* dev_name) { |
| 93 | std::lock_guard<std::mutex> lock(g_usb_handles_mutex); |
| 94 | for (usb_handle* usb : g_usb_handles) { |
| 95 | if (usb->path == dev_name) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 96 | // set mark flag to indicate this device is still alive |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 97 | usb->mark = true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 98 | return 1; |
| 99 | } |
| 100 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 104 | static void kick_disconnected_devices() { |
| 105 | std::lock_guard<std::mutex> lock(g_usb_handles_mutex); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | // kick any devices in the device list that were not found in the device scan |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 107 | for (usb_handle* usb : g_usb_handles) { |
| 108 | if (!usb->mark) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 109 | usb_kick(usb); |
| 110 | } else { |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 111 | usb->mark = false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 116 | static inline bool contains_non_digit(const char* name) { |
| 117 | while (*name) { |
| 118 | if (!isdigit(*name++)) return true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | } |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 120 | return false; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 123 | static void find_usb_device(const std::string& base, |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 124 | void (*register_device_callback)(const char*, const char*, |
| 125 | unsigned char, unsigned char, int, int, |
| 126 | unsigned, size_t)) { |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 127 | std::unique_ptr<DIR, int(*)(DIR*)> bus_dir(opendir(base.c_str()), closedir); |
| 128 | if (!bus_dir) return; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 130 | dirent* de; |
| 131 | while ((de = readdir(bus_dir.get())) != 0) { |
| 132 | if (contains_non_digit(de->d_name)) continue; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 134 | std::string bus_name = base + "/" + de->d_name; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 135 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 136 | std::unique_ptr<DIR, int(*)(DIR*)> dev_dir(opendir(bus_name.c_str()), closedir); |
| 137 | if (!dev_dir) continue; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 139 | while ((de = readdir(dev_dir.get()))) { |
Mike Lockwood | f166771 | 2011-01-07 23:18:14 -0500 | [diff] [blame] | 140 | unsigned char devdesc[4096]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 141 | unsigned char* bufptr = devdesc; |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 142 | unsigned char* bufend; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 143 | struct usb_device_descriptor* device; |
| 144 | struct usb_config_descriptor* config; |
| 145 | struct usb_interface_descriptor* interface; |
| 146 | struct usb_endpoint_descriptor *ep1, *ep2; |
| 147 | unsigned zero_mask = 0; |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 148 | size_t max_packet_size = 0; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | unsigned vid, pid; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 151 | if (contains_non_digit(de->d_name)) continue; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 153 | std::string dev_name = bus_name + "/" + de->d_name; |
| 154 | if (is_known_device(dev_name.c_str())) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 | continue; |
| 156 | } |
| 157 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 158 | int fd = unix_open(dev_name.c_str(), O_RDONLY | O_CLOEXEC); |
| 159 | if (fd == -1) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | continue; |
| 161 | } |
| 162 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 163 | size_t desclength = unix_read(fd, devdesc, sizeof(devdesc)); |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 164 | bufend = bufptr + desclength; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | |
| 166 | // should have device and configuration descriptors, and atleast two endpoints |
| 167 | if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 168 | D("desclength %zu is too small", desclength); |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 169 | unix_close(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 170 | continue; |
| 171 | } |
| 172 | |
| 173 | device = (struct usb_device_descriptor*)bufptr; |
| 174 | bufptr += USB_DT_DEVICE_SIZE; |
| 175 | |
| 176 | if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) { |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 177 | unix_close(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 178 | continue; |
| 179 | } |
| 180 | |
Marcus Comstedt | 3d2f518 | 2010-09-22 20:09:44 +0200 | [diff] [blame] | 181 | vid = device->idVendor; |
| 182 | pid = device->idProduct; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 183 | DBGX("[ %s is V:%04x P:%04x ]\n", dev_name.c_str(), vid, pid); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 184 | |
| 185 | // should have config descriptor next |
| 186 | config = (struct usb_config_descriptor *)bufptr; |
| 187 | bufptr += USB_DT_CONFIG_SIZE; |
| 188 | if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 189 | D("usb_config_descriptor not found"); |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 190 | unix_close(fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 191 | continue; |
| 192 | } |
| 193 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 194 | // loop through all the descriptors and look for the ADB interface |
| 195 | while (bufptr < bufend) { |
| 196 | unsigned char length = bufptr[0]; |
| 197 | unsigned char type = bufptr[1]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 198 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 199 | if (type == USB_DT_INTERFACE) { |
| 200 | interface = (struct usb_interface_descriptor *)bufptr; |
| 201 | bufptr += length; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 202 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 203 | if (length != USB_DT_INTERFACE_SIZE) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 204 | D("interface descriptor has wrong size"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 208 | DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d," |
| 209 | "bInterfaceProtocol: %d, bNumEndpoints: %d\n", |
| 210 | interface->bInterfaceClass, interface->bInterfaceSubClass, |
| 211 | interface->bInterfaceProtocol, interface->bNumEndpoints); |
| 212 | |
| 213 | if (interface->bNumEndpoints == 2 && |
Josh Gao | 08dda21 | 2016-09-26 21:18:58 -0700 | [diff] [blame] | 214 | is_adb_interface(interface->bInterfaceClass, interface->bInterfaceSubClass, |
| 215 | interface->bInterfaceProtocol)) { |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 216 | struct stat st; |
| 217 | char pathbuf[128]; |
| 218 | char link[256]; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 219 | char *devpath = nullptr; |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 220 | |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 221 | DBGX("looking for bulk endpoints\n"); |
| 222 | // looks like ADB... |
| 223 | ep1 = (struct usb_endpoint_descriptor *)bufptr; |
| 224 | bufptr += USB_DT_ENDPOINT_SIZE; |
Ingo Rohloff | 5b27663 | 2014-05-16 21:51:41 +0200 | [diff] [blame] | 225 | // For USB 3.0 SuperSpeed devices, skip potential |
| 226 | // USB 3.0 SuperSpeed Endpoint Companion descriptor |
| 227 | if (bufptr+2 <= devdesc + desclength && |
| 228 | bufptr[0] == USB_DT_SS_EP_COMP_SIZE && |
| 229 | bufptr[1] == USB_DT_SS_ENDPOINT_COMP) { |
| 230 | bufptr += USB_DT_SS_EP_COMP_SIZE; |
| 231 | } |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 232 | ep2 = (struct usb_endpoint_descriptor *)bufptr; |
| 233 | bufptr += USB_DT_ENDPOINT_SIZE; |
Ingo Rohloff | 5b27663 | 2014-05-16 21:51:41 +0200 | [diff] [blame] | 234 | if (bufptr+2 <= devdesc + desclength && |
| 235 | bufptr[0] == USB_DT_SS_EP_COMP_SIZE && |
| 236 | bufptr[1] == USB_DT_SS_ENDPOINT_COMP) { |
| 237 | bufptr += USB_DT_SS_EP_COMP_SIZE; |
| 238 | } |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 239 | |
| 240 | if (bufptr > devdesc + desclength || |
| 241 | ep1->bLength != USB_DT_ENDPOINT_SIZE || |
| 242 | ep1->bDescriptorType != USB_DT_ENDPOINT || |
| 243 | ep2->bLength != USB_DT_ENDPOINT_SIZE || |
| 244 | ep2->bDescriptorType != USB_DT_ENDPOINT) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 245 | D("endpoints not found"); |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | |
| 249 | // both endpoints should be bulk |
| 250 | if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK || |
| 251 | ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 252 | D("bulk endpoints not found"); |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 253 | continue; |
| 254 | } |
| 255 | /* aproto 01 needs 0 termination */ |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 256 | if (interface->bInterfaceProtocol == 0x01) { |
| 257 | max_packet_size = ep1->wMaxPacketSize; |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 258 | zero_mask = ep1->wMaxPacketSize - 1; |
| 259 | } |
| 260 | |
| 261 | // we have a match. now we just need to figure out which is in and which is out. |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 262 | unsigned char local_ep_in, local_ep_out; |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 263 | if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) { |
| 264 | local_ep_in = ep1->bEndpointAddress; |
| 265 | local_ep_out = ep2->bEndpointAddress; |
| 266 | } else { |
| 267 | local_ep_in = ep2->bEndpointAddress; |
| 268 | local_ep_out = ep1->bEndpointAddress; |
| 269 | } |
| 270 | |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 271 | // Determine the device path |
| 272 | if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) { |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 273 | snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d", |
| 274 | major(st.st_rdev), minor(st.st_rdev)); |
Elliott Hughes | 24f9b08 | 2015-07-27 21:21:39 -0700 | [diff] [blame] | 275 | ssize_t link_len = readlink(pathbuf, link, sizeof(link) - 1); |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 276 | if (link_len > 0) { |
| 277 | link[link_len] = '\0'; |
Elliott Hughes | 24f9b08 | 2015-07-27 21:21:39 -0700 | [diff] [blame] | 278 | const char* slash = strrchr(link, '/'); |
Scott Anderson | 6dfaf4b | 2012-04-20 11:21:14 -0700 | [diff] [blame] | 279 | if (slash) { |
| 280 | snprintf(pathbuf, sizeof(pathbuf), |
| 281 | "usb:%s", slash + 1); |
| 282 | devpath = pathbuf; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 287 | register_device_callback(dev_name.c_str(), devpath, local_ep_in, |
| 288 | local_ep_out, interface->bInterfaceNumber, |
| 289 | device->iSerialNumber, zero_mask, max_packet_size); |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 290 | break; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 291 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 292 | } else { |
Mike Lockwood | 31175d6 | 2010-01-17 00:52:27 -0500 | [diff] [blame] | 293 | bufptr += length; |
| 294 | } |
| 295 | } // end of while |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 296 | |
Spencer Low | 3a2421b | 2015-05-22 20:09:06 -0700 | [diff] [blame] | 297 | unix_close(fd); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 300 | } |
| 301 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 302 | static int usb_bulk_write(usb_handle* h, const void* data, int len) { |
| 303 | std::unique_lock<std::mutex> lock(h->mutex); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 304 | D("++ usb_bulk_write ++"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 305 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 306 | usbdevfs_urb* urb = &h->urb_out; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 307 | memset(urb, 0, sizeof(*urb)); |
| 308 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 309 | urb->endpoint = h->ep_out; |
| 310 | urb->status = -1; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 311 | urb->buffer = const_cast<void*>(data); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 312 | urb->buffer_length = len; |
| 313 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 314 | if (h->dead) { |
| 315 | errno = EINVAL; |
| 316 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 317 | } |
| 318 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 319 | if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) { |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | h->urb_out_busy = true; |
| 324 | while (true) { |
| 325 | auto now = std::chrono::system_clock::now(); |
| 326 | if (h->cv.wait_until(lock, now + 5s) == std::cv_status::timeout || h->dead) { |
| 327 | // TODO: call USBDEVFS_DISCARDURB? |
| 328 | errno = ETIMEDOUT; |
| 329 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 330 | } |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 331 | if (!h->urb_out_busy) { |
| 332 | if (urb->status != 0) { |
| 333 | errno = -urb->status; |
| 334 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 335 | } |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 336 | return urb->actual_length; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 337 | } |
| 338 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 339 | } |
| 340 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 341 | static int usb_bulk_read(usb_handle* h, void* data, int len) { |
| 342 | std::unique_lock<std::mutex> lock(h->mutex); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 343 | D("++ usb_bulk_read ++"); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 344 | |
| 345 | usbdevfs_urb* urb = &h->urb_in; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 346 | memset(urb, 0, sizeof(*urb)); |
| 347 | urb->type = USBDEVFS_URB_TYPE_BULK; |
| 348 | urb->endpoint = h->ep_in; |
| 349 | urb->status = -1; |
| 350 | urb->buffer = data; |
| 351 | urb->buffer_length = len; |
| 352 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 353 | if (h->dead) { |
| 354 | errno = EINVAL; |
| 355 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 358 | if (TEMP_FAILURE_RETRY(ioctl(h->fd, USBDEVFS_SUBMITURB, urb)) == -1) { |
| 359 | return -1; |
| 360 | } |
| 361 | |
| 362 | h->urb_in_busy = true; |
| 363 | while (true) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 364 | D("[ reap urb - wait ]"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 365 | h->reaper_thread = pthread_self(); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 366 | int fd = h->fd; |
| 367 | lock.unlock(); |
| 368 | |
| 369 | // This ioctl must not have TEMP_FAILURE_RETRY because we send SIGALRM to break out. |
| 370 | usbdevfs_urb* out = nullptr; |
| 371 | int res = ioctl(fd, USBDEVFS_REAPURB, &out); |
JP Abgrall | 2e5dd6e | 2011-03-16 15:57:42 -0700 | [diff] [blame] | 372 | int saved_errno = errno; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 373 | |
| 374 | lock.lock(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 375 | h->reaper_thread = 0; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 376 | if (h->dead) { |
| 377 | errno = EINVAL; |
| 378 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | } |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 380 | if (res < 0) { |
| 381 | if (saved_errno == EINTR) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 382 | continue; |
| 383 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 384 | D("[ reap urb - error ]"); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 385 | errno = saved_errno; |
| 386 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 387 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 388 | D("[ urb @%p status = %d, actual = %d ]", out, out->status, out->actual_length); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 389 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 390 | if (out == &h->urb_in) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 391 | D("[ reap urb - IN complete ]"); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 392 | h->urb_in_busy = false; |
| 393 | if (urb->status != 0) { |
| 394 | errno = -urb->status; |
| 395 | return -1; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 396 | } |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 397 | return urb->actual_length; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 398 | } |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 399 | if (out == &h->urb_out) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 400 | D("[ reap urb - OUT compelete ]"); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 401 | h->urb_out_busy = false; |
| 402 | h->cv.notify_all(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 403 | } |
| 404 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 405 | } |
| 406 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 407 | int usb_write(usb_handle *h, const void *_data, int len) |
| 408 | { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 409 | D("++ usb_write ++"); |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 410 | |
| 411 | unsigned char *data = (unsigned char*) _data; |
| 412 | int n = usb_bulk_write(h, data, len); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 413 | if (n != len) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 414 | D("ERROR: n = %d, errno = %d (%s)", n, errno, strerror(errno)); |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 415 | return -1; |
| 416 | } |
| 417 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 418 | if (h->zero_mask && !(len & h->zero_mask)) { |
| 419 | // If we need 0-markers and our transfer is an even multiple of the packet size, |
| 420 | // then send a zero marker. |
| 421 | return usb_bulk_write(h, _data, 0); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 424 | D("-- usb_write --"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | int usb_read(usb_handle *h, void *_data, int len) |
| 429 | { |
| 430 | unsigned char *data = (unsigned char*) _data; |
| 431 | int n; |
| 432 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 433 | D("++ usb_read ++"); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 434 | int orig_len = len; |
| 435 | while (len == orig_len) { |
Tamas Berghammer | a1c60c0 | 2015-07-13 19:12:28 +0100 | [diff] [blame] | 436 | int xfer = len; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 437 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 438 | D("[ usb read %d fd = %d], path=%s", xfer, h->fd, h->path.c_str()); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 439 | n = usb_bulk_read(h, data, xfer); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 440 | D("[ usb read %d ] = %d, path=%s", xfer, n, h->path.c_str()); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 441 | if (n <= 0) { |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 442 | if((errno == ETIMEDOUT) && (h->fd != -1)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 443 | D("[ timeout ]"); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 444 | continue; |
| 445 | } |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 446 | D("ERROR: n = %d, errno = %d (%s)", |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 447 | n, errno, strerror(errno)); |
| 448 | return -1; |
| 449 | } |
| 450 | |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 451 | len -= n; |
| 452 | data += n; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 455 | D("-- usb_read --"); |
Yabin Cui | 3cf1b36 | 2017-03-10 16:01:01 -0800 | [diff] [blame] | 456 | return orig_len - len; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 459 | void usb_kick(usb_handle* h) { |
| 460 | std::lock_guard<std::mutex> lock(h->mutex); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 461 | D("[ kicking %p (fd = %d) ]", h, h->fd); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 462 | if (!h->dead) { |
| 463 | h->dead = true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 465 | 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 473 | |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 474 | /* 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 Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 479 | ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_in); |
| 480 | ioctl(h->fd, USBDEVFS_DISCARDURB, &h->urb_out); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 481 | h->urb_in.status = -ENODEV; |
| 482 | h->urb_out.status = -ENODEV; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 483 | h->urb_in_busy = false; |
| 484 | h->urb_out_busy = false; |
| 485 | h->cv.notify_all(); |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 486 | } else { |
| 487 | unregister_usb_transport(h); |
| 488 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 489 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 492 | int 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 Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 495 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 496 | D("-- usb close %p (fd = %d) --", h, h->fd); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 497 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 498 | delete h; |
| 499 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 503 | size_t usb_get_max_packet_size(usb_handle* h) { |
| 504 | return h->max_packet_size; |
| 505 | } |
| 506 | |
| 507 | static void register_device(const char* dev_name, const char* dev_path, unsigned char ep_in, |
| 508 | unsigned char ep_out, int interface, int serial_index, |
| 509 | unsigned zero_mask, size_t max_packet_size) { |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 510 | // Since Linux will not reassign the device ID (and dev_name) as long as the |
| 511 | // device is open, we can add to the list here once we open it and remove |
| 512 | // from the list when we're finally closed and everything will work out |
| 513 | // fine. |
| 514 | // |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 515 | // If we have a usb_handle on the list of handles with a matching name, we |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 516 | // have no further work to do. |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 517 | { |
| 518 | std::lock_guard<std::mutex> lock(g_usb_handles_mutex); |
| 519 | for (usb_handle* usb: g_usb_handles) { |
| 520 | if (usb->path == dev_name) { |
| 521 | return; |
| 522 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 523 | } |
| 524 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 525 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 526 | D("[ usb located new device %s (%d/%d/%d) ]", dev_name, ep_in, ep_out, interface); |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 527 | std::unique_ptr<usb_handle> usb(new usb_handle); |
| 528 | usb->path = dev_name; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 529 | usb->ep_in = ep_in; |
| 530 | usb->ep_out = ep_out; |
| 531 | usb->zero_mask = zero_mask; |
Josh Gao | 3734cf0 | 2017-05-02 15:01:09 -0700 | [diff] [blame] | 532 | usb->max_packet_size = max_packet_size; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 533 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 534 | // Initialize mark so we don't get garbage collected after the device scan. |
| 535 | usb->mark = true; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 536 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 537 | usb->fd = unix_open(usb->path.c_str(), O_RDWR | O_CLOEXEC); |
| 538 | if (usb->fd == -1) { |
Elliott Hughes | 5d50437 | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 539 | // Opening RW failed, so see if we have RO access. |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 540 | usb->fd = unix_open(usb->path.c_str(), O_RDONLY | O_CLOEXEC); |
| 541 | if (usb->fd == -1) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 542 | D("[ usb open %s failed: %s]", usb->path.c_str(), strerror(errno)); |
Elliott Hughes | 5d50437 | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 543 | return; |
| 544 | } |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 545 | usb->writeable = 0; |
Elliott Hughes | 5d50437 | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 548 | D("[ usb opened %s%s, fd=%d]", |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 549 | usb->path.c_str(), (usb->writeable ? "" : " (read-only)"), usb->fd); |
Elliott Hughes | 5d50437 | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 550 | |
| 551 | if (usb->writeable) { |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 552 | if (ioctl(usb->fd, USBDEVFS_CLAIMINTERFACE, &interface) != 0) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 553 | D("[ usb ioctl(%d, USBDEVFS_CLAIMINTERFACE) failed: %s]", usb->fd, strerror(errno)); |
Elliott Hughes | 5d50437 | 2015-04-25 14:44:23 -0700 | [diff] [blame] | 554 | return; |
| 555 | } |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Elliott Hughes | 949f262 | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 558 | // Read the device's serial number. |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 559 | std::string serial_path = android::base::StringPrintf( |
| 560 | "/sys/bus/usb/devices/%s/serial", dev_path + 4); |
Elliott Hughes | 949f262 | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 561 | std::string serial; |
| 562 | if (!android::base::ReadFileToString(serial_path, &serial)) { |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 563 | D("[ usb read %s failed: %s ]", serial_path.c_str(), strerror(errno)); |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 564 | // We don't actually want to treat an unknown serial as an error because |
| 565 | // devices aren't able to communicate a serial number in early bringup. |
| 566 | // http://b/20883914 |
| 567 | serial = ""; |
Mike Lockwood | e45583f | 2009-08-08 12:37:44 -0400 | [diff] [blame] | 568 | } |
Elliott Hughes | 949f262 | 2015-04-27 14:20:17 -0700 | [diff] [blame] | 569 | serial = android::base::Trim(serial); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 570 | |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 571 | // Add to the end of the active handles. |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 572 | usb_handle* done_usb = usb.release(); |
| 573 | { |
| 574 | std::lock_guard<std::mutex> lock(g_usb_handles_mutex); |
| 575 | g_usb_handles.push_back(done_usb); |
| 576 | } |
| 577 | register_usb_transport(done_usb, serial.c_str(), dev_path, done_usb->writeable); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 578 | } |
| 579 | |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 580 | static void device_poll_thread() { |
Siva Velusamy | 2669cf9 | 2015-08-28 16:37:29 -0700 | [diff] [blame] | 581 | adb_thread_setname("device poll"); |
Yabin Cui | 815ad88 | 2015-09-02 17:44:28 -0700 | [diff] [blame] | 582 | D("Created device thread"); |
Dan Albert | a8c3414 | 2015-05-06 16:48:52 -0700 | [diff] [blame] | 583 | while (true) { |
| 584 | // TODO: Use inotify. |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 585 | find_usb_device("/dev/bus/usb", register_device); |
| 586 | kick_disconnected_devices(); |
Elliott Hughes | 7392598 | 2016-11-15 12:37:32 -0800 | [diff] [blame] | 587 | std::this_thread::sleep_for(1s); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 588 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 591 | void usb_init() { |
| 592 | struct sigaction actions; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 593 | memset(&actions, 0, sizeof(actions)); |
| 594 | sigemptyset(&actions.sa_mask); |
| 595 | actions.sa_flags = 0; |
Elliott Hughes | 3af421c | 2015-07-23 15:20:09 -0700 | [diff] [blame] | 596 | actions.sa_handler = [](int) {}; |
| 597 | sigaction(SIGALRM, &actions, nullptr); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | |
Josh Gao | 0f3312a | 2017-04-12 17:00:49 -0700 | [diff] [blame] | 599 | std::thread(device_poll_thread).detach(); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 600 | } |
Josh Gao | 90b6253 | 2017-05-10 13:51:36 -0700 | [diff] [blame] | 601 | |
| 602 | void usb_cleanup() {} |
| 603 | |
Josh Gao | b736692 | 2016-09-28 12:32:45 -0700 | [diff] [blame] | 604 | } // namespace native |