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