[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
reillyg | 4591981 | 2015-01-22 02:39:59 | [diff] [blame] | 5 | #include "device/usb/usb_service_impl.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 6 | |
avi | 176e269 | 2015-12-22 19:26:52 | [diff] [blame] | 7 | #include <stdint.h> |
thakis | acd68a2 | 2016-04-11 16:12:23 | [diff] [blame] | 8 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 9 | #include <list> |
thakis | acd68a2 | 2016-04-11 16:12:23 | [diff] [blame] | 10 | #include <memory> |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 11 | #include <set> |
dcheng | 64f325f | 2015-12-27 22:05:10 | [diff] [blame] | 12 | #include <utility> |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 13 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 14 | #include "base/barrier_closure.h" |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 15 | #include "base/bind.h" |
reillyg | 4591981 | 2015-01-22 02:39:59 | [diff] [blame] | 16 | #include "base/location.h" |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 17 | #include "base/memory/weak_ptr.h" |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 19 | #include "base/stl_util.h" |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 20 | #include "base/strings/string_number_conversions.h" |
| 21 | #include "base/strings/utf_string_conversions.h" |
gab | c7c963d | 2016-05-11 21:01:55 | [diff] [blame] | 22 | #include "base/threading/thread_task_runner_handle.h" |
avi | 176e269 | 2015-12-22 19:26:52 | [diff] [blame] | 23 | #include "build/build_config.h" |
reillyg | 0f2dc63 | 2015-02-23 20:02:08 | [diff] [blame] | 24 | #include "components/device_event_log/device_event_log.h" |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 25 | #include "device/usb/usb_device_handle.h" |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame] | 26 | #include "device/usb/usb_error.h" |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 27 | #include "device/usb/webusb_descriptors.h" |
reillyg | a9de0fa | 2016-01-28 01:38:01 | [diff] [blame] | 28 | #include "net/base/io_buffer.h" |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 29 | #include "third_party/libusb/src/libusb/libusb.h" |
[email protected] | 1c39a6b | 2012-04-27 16:09:57 | [diff] [blame] | 30 | |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 31 | #if defined(OS_WIN) |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 32 | #include <setupapi.h> |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 33 | #include <usbiodef.h> |
| 34 | |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 35 | #include "base/strings/string_util.h" |
juncai | 0c9eb03 | 2015-11-21 05:54:27 | [diff] [blame] | 36 | #include "device/core/device_info_query_win.h" |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 37 | #endif // OS_WIN |
| 38 | |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 39 | using net::IOBufferWithSize; |
| 40 | |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame] | 41 | namespace device { |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 42 | |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 43 | namespace { |
| 44 | |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 45 | // Standard USB requests and descriptor types: |
| 46 | const uint16_t kUsbVersion2_1 = 0x0210; |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 47 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 48 | #if defined(OS_WIN) |
| 49 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 50 | bool IsWinUsbInterface(const std::string& device_path) { |
juncai | 0c9eb03 | 2015-11-21 05:54:27 | [diff] [blame] | 51 | DeviceInfoQueryWin device_info_query; |
| 52 | if (!device_info_query.device_info_list_valid()) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 53 | USB_PLOG(ERROR) << "Failed to create a device information set"; |
| 54 | return false; |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 55 | } |
| 56 | |
juncai | 0c9eb03 | 2015-11-21 05:54:27 | [diff] [blame] | 57 | // This will add the device so we can query driver info. |
| 58 | if (!device_info_query.AddDevice(device_path.c_str())) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 59 | USB_PLOG(ERROR) << "Failed to get device interface data for " |
| 60 | << device_path; |
| 61 | return false; |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 62 | } |
| 63 | |
juncai | 0c9eb03 | 2015-11-21 05:54:27 | [diff] [blame] | 64 | if (!device_info_query.GetDeviceInfo()) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 65 | USB_PLOG(ERROR) << "Failed to get device info for " << device_path; |
| 66 | return false; |
| 67 | } |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 68 | |
juncai | 0c9eb03 | 2015-11-21 05:54:27 | [diff] [blame] | 69 | std::string buffer; |
| 70 | if (!device_info_query.GetDeviceStringProperty(SPDRP_SERVICE, &buffer)) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 71 | USB_PLOG(ERROR) << "Failed to get device service property"; |
| 72 | return false; |
| 73 | } |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 74 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 75 | USB_LOG(DEBUG) << "Driver for " << device_path << " is " << buffer << "."; |
juncai | 0c9eb03 | 2015-11-21 05:54:27 | [diff] [blame] | 76 | if (base::StartsWith(buffer, "WinUSB", base::CompareCase::INSENSITIVE_ASCII)) |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 77 | return true; |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 78 | return false; |
| 79 | } |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 80 | |
| 81 | #endif // OS_WIN |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 82 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 83 | void GetDeviceListOnBlockingThread( |
| 84 | const std::string& new_device_path, |
| 85 | scoped_refptr<UsbContext> usb_context, |
| 86 | scoped_refptr<base::SequencedTaskRunner> task_runner, |
| 87 | base::Callback<void(libusb_device**, size_t)> callback) { |
| 88 | #if defined(OS_WIN) |
| 89 | if (!new_device_path.empty()) { |
| 90 | if (!IsWinUsbInterface(new_device_path)) { |
| 91 | // Wait to call libusb_get_device_list until libusb will be able to find |
| 92 | // a WinUSB interface for the device. |
| 93 | task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr, 0)); |
| 94 | return; |
| 95 | } |
| 96 | } |
| 97 | #endif // defined(OS_WIN) |
| 98 | |
| 99 | libusb_device** platform_devices = NULL; |
| 100 | const ssize_t device_count = |
| 101 | libusb_get_device_list(usb_context->context(), &platform_devices); |
| 102 | if (device_count < 0) { |
| 103 | USB_LOG(ERROR) << "Failed to get device list: " |
| 104 | << ConvertPlatformUsbErrorToString(device_count); |
| 105 | task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr, 0)); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | task_runner->PostTask(FROM_HERE, |
| 110 | base::Bind(callback, platform_devices, device_count)); |
| 111 | } |
| 112 | |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 113 | void CloseHandleAndRunContinuation(scoped_refptr<UsbDeviceHandle> device_handle, |
| 114 | const base::Closure& continuation) { |
| 115 | device_handle->Close(); |
| 116 | continuation.Run(); |
| 117 | } |
| 118 | |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 119 | void SaveStringsAndRunContinuation( |
| 120 | scoped_refptr<UsbDeviceImpl> device, |
| 121 | uint8_t manufacturer, |
| 122 | uint8_t product, |
| 123 | uint8_t serial_number, |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 124 | const base::Closure& continuation, |
thakis | acd68a2 | 2016-04-11 16:12:23 | [diff] [blame] | 125 | std::unique_ptr<std::map<uint8_t, base::string16>> string_map) { |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 126 | if (manufacturer != 0) |
| 127 | device->set_manufacturer_string((*string_map)[manufacturer]); |
| 128 | if (product != 0) |
| 129 | device->set_product_string((*string_map)[product]); |
| 130 | if (serial_number != 0) |
| 131 | device->set_serial_number((*string_map)[serial_number]); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 132 | continuation.Run(); |
| 133 | } |
| 134 | |
reillyg | 2437acd | 2016-01-29 01:51:37 | [diff] [blame] | 135 | void OnReadBosDescriptor(scoped_refptr<UsbDeviceHandle> device_handle, |
| 136 | const base::Closure& barrier, |
thakis | acd68a2 | 2016-04-11 16:12:23 | [diff] [blame] | 137 | std::unique_ptr<WebUsbAllowedOrigins> allowed_origins, |
reillyg | 2437acd | 2016-01-29 01:51:37 | [diff] [blame] | 138 | const GURL& landing_page) { |
| 139 | scoped_refptr<UsbDeviceImpl> device = |
| 140 | static_cast<UsbDeviceImpl*>(device_handle->GetDevice().get()); |
| 141 | |
| 142 | if (allowed_origins) |
| 143 | device->set_webusb_allowed_origins(std::move(allowed_origins)); |
| 144 | if (landing_page.is_valid()) |
| 145 | device->set_webusb_landing_page(landing_page); |
| 146 | |
| 147 | barrier.Run(); |
| 148 | } |
| 149 | |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 150 | void OnDeviceOpenedReadDescriptors( |
reillyg | 18b45516 | 2015-09-24 02:32:18 | [diff] [blame] | 151 | uint8_t manufacturer, |
| 152 | uint8_t product, |
| 153 | uint8_t serial_number, |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 154 | bool read_bos_descriptors, |
| 155 | const base::Closure& success_closure, |
| 156 | const base::Closure& failure_closure, |
| 157 | scoped_refptr<UsbDeviceHandle> device_handle) { |
| 158 | if (device_handle) { |
thakis | acd68a2 | 2016-04-11 16:12:23 | [diff] [blame] | 159 | std::unique_ptr<std::map<uint8_t, base::string16>> string_map( |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 160 | new std::map<uint8_t, base::string16>()); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 161 | if (manufacturer != 0) |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 162 | (*string_map)[manufacturer] = base::string16(); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 163 | if (product != 0) |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 164 | (*string_map)[product] = base::string16(); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 165 | if (serial_number != 0) |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 166 | (*string_map)[serial_number] = base::string16(); |
| 167 | |
| 168 | int count = 0; |
| 169 | if (!string_map->empty()) |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 170 | count++; |
| 171 | if (read_bos_descriptors) |
| 172 | count++; |
| 173 | DCHECK_GT(count, 0); |
| 174 | |
| 175 | base::Closure barrier = |
| 176 | base::BarrierClosure(count, base::Bind(&CloseHandleAndRunContinuation, |
| 177 | device_handle, success_closure)); |
| 178 | |
reillyg | 7446a70 | 2016-01-30 01:06:41 | [diff] [blame] | 179 | if (!string_map->empty()) { |
| 180 | scoped_refptr<UsbDeviceImpl> device = |
| 181 | static_cast<UsbDeviceImpl*>(device_handle->GetDevice().get()); |
| 182 | |
| 183 | ReadUsbStringDescriptors( |
| 184 | device_handle, std::move(string_map), |
| 185 | base::Bind(&SaveStringsAndRunContinuation, device, manufacturer, |
| 186 | product, serial_number, barrier)); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (read_bos_descriptors) { |
reillyg | 2437acd | 2016-01-29 01:51:37 | [diff] [blame] | 190 | ReadWebUsbDescriptors(device_handle, base::Bind(&OnReadBosDescriptor, |
| 191 | device_handle, barrier)); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 192 | } |
| 193 | } else { |
| 194 | failure_closure.Run(); |
| 195 | } |
| 196 | } |
| 197 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 198 | } // namespace |
| 199 | |
reillyg | e471fab | 2014-08-29 01:58:43 | [diff] [blame] | 200 | UsbServiceImpl::UsbServiceImpl( |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 201 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 202 | : UsbService(base::ThreadTaskRunnerHandle::Get(), blocking_task_runner), |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 203 | #if defined(OS_WIN) |
| 204 | device_observer_(this), |
| 205 | #endif |
reillyg | 257f4309 | 2015-01-08 00:48:10 | [diff] [blame] | 206 | weak_factory_(this) { |
reillyg | c4baf41 | 2015-08-08 22:51:04 | [diff] [blame] | 207 | PlatformUsbContext platform_context = nullptr; |
| 208 | int rv = libusb_init(&platform_context); |
| 209 | if (rv != LIBUSB_SUCCESS || !platform_context) { |
| 210 | USB_LOG(DEBUG) << "Failed to initialize libusb: " |
| 211 | << ConvertPlatformUsbErrorToString(rv); |
| 212 | return; |
| 213 | } |
| 214 | context_ = new UsbContext(platform_context); |
| 215 | |
| 216 | rv = libusb_hotplug_register_callback( |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 217 | context_->context(), |
| 218 | static_cast<libusb_hotplug_event>(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | |
| 219 | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 220 | static_cast<libusb_hotplug_flag>(0), LIBUSB_HOTPLUG_MATCH_ANY, |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 221 | LIBUSB_HOTPLUG_MATCH_ANY, LIBUSB_HOTPLUG_MATCH_ANY, |
| 222 | &UsbServiceImpl::HotplugCallback, this, &hotplug_handle_); |
| 223 | if (rv == LIBUSB_SUCCESS) { |
| 224 | hotplug_enabled_ = true; |
| 225 | } |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 226 | |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 227 | RefreshDevices(); |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 228 | #if defined(OS_WIN) |
| 229 | DeviceMonitorWin* device_monitor = DeviceMonitorWin::GetForAllInterfaces(); |
| 230 | if (device_monitor) { |
| 231 | device_observer_.Add(device_monitor); |
| 232 | } |
| 233 | #endif // OS_WIN |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 234 | } |
| 235 | |
[email protected] | ad2263f | 2014-05-05 13:57:50 | [diff] [blame] | 236 | UsbServiceImpl::~UsbServiceImpl() { |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 237 | if (hotplug_enabled_) |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 238 | libusb_hotplug_deregister_callback(context_->context(), hotplug_handle_); |
vmpstr | b0e145b | 2016-07-28 02:55:36 | [diff] [blame] | 239 | for (auto* platform_device : ignored_devices_) |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 240 | libusb_unref_device(platform_device); |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 241 | } |
| 242 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 243 | void UsbServiceImpl::GetDevices(const GetDevicesCallback& callback) { |
[email protected] | e195487 | 2014-04-13 17:43:29 | [diff] [blame] | 244 | DCHECK(CalledOnValidThread()); |
[email protected] | d165a644 | 2013-08-08 19:38:11 | [diff] [blame] | 245 | |
reillyg | c4baf41 | 2015-08-08 22:51:04 | [diff] [blame] | 246 | if (!context_) { |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 247 | task_runner()->PostTask( |
reillyg | c4baf41 | 2015-08-08 22:51:04 | [diff] [blame] | 248 | FROM_HERE, |
| 249 | base::Bind(callback, std::vector<scoped_refptr<UsbDevice>>())); |
| 250 | return; |
| 251 | } |
| 252 | |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 253 | if (hotplug_enabled_ && !enumeration_in_progress_) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 254 | // The device list is updated live when hotplug events are supported. |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 255 | UsbService::GetDevices(callback); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 256 | } else { |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 257 | pending_enumeration_callbacks_.push_back(callback); |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 258 | RefreshDevices(); |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 259 | } |
[email protected] | 22ac24e | 2012-10-03 17:56:05 | [diff] [blame] | 260 | } |
[email protected] | b550868 | 2014-04-22 17:10:01 | [diff] [blame] | 261 | |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 262 | #if defined(OS_WIN) |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 263 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 264 | void UsbServiceImpl::OnDeviceAdded(const GUID& class_guid, |
| 265 | const std::string& device_path) { |
| 266 | // Only the root node of a composite USB device has the class GUID |
| 267 | // GUID_DEVINTERFACE_USB_DEVICE but we want to wait until WinUSB is loaded. |
| 268 | // This first pass filter will catch anything that's sitting on the USB bus |
| 269 | // (including devices on 3rd party USB controllers) to avoid the more |
| 270 | // expensive driver check that needs to be done on the FILE thread. |
| 271 | if (device_path.find("usb") != std::string::npos) { |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 272 | pending_path_enumerations_.push(device_path); |
| 273 | RefreshDevices(); |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 274 | } |
| 275 | } |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 276 | |
| 277 | void UsbServiceImpl::OnDeviceRemoved(const GUID& class_guid, |
| 278 | const std::string& device_path) { |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 279 | // The root USB device node is removed last. |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 280 | if (class_guid == GUID_DEVINTERFACE_USB_DEVICE) { |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 281 | RefreshDevices(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
reillyg | 8cbc3ce | 2015-04-03 07:39:43 | [diff] [blame] | 285 | #endif // OS_WIN |
| 286 | |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 287 | void UsbServiceImpl::RefreshDevices() { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 288 | DCHECK(CalledOnValidThread()); |
reillyg | c4baf41 | 2015-08-08 22:51:04 | [diff] [blame] | 289 | DCHECK(context_); |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 290 | |
| 291 | if (enumeration_in_progress_) { |
| 292 | return; |
| 293 | } |
| 294 | |
| 295 | enumeration_in_progress_ = true; |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 296 | DCHECK(devices_being_enumerated_.empty()); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 297 | |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 298 | std::string device_path; |
| 299 | if (!pending_path_enumerations_.empty()) { |
| 300 | device_path = pending_path_enumerations_.front(); |
| 301 | pending_path_enumerations_.pop(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 302 | } |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 303 | |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 304 | blocking_task_runner()->PostTask( |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 305 | FROM_HERE, |
| 306 | base::Bind(&GetDeviceListOnBlockingThread, device_path, context_, |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 307 | task_runner(), base::Bind(&UsbServiceImpl::OnDeviceList, |
| 308 | weak_factory_.GetWeakPtr()))); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 309 | } |
| 310 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 311 | void UsbServiceImpl::OnDeviceList(libusb_device** platform_devices, |
| 312 | size_t device_count) { |
| 313 | DCHECK(CalledOnValidThread()); |
| 314 | if (!platform_devices) { |
| 315 | RefreshDevicesComplete(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 319 | base::Closure refresh_complete = |
| 320 | base::BarrierClosure(static_cast<int>(device_count), |
| 321 | base::Bind(&UsbServiceImpl::RefreshDevicesComplete, |
| 322 | weak_factory_.GetWeakPtr())); |
| 323 | std::list<PlatformUsbDevice> new_devices; |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 324 | std::set<PlatformUsbDevice> existing_ignored_devices; |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 325 | |
| 326 | // Look for new and existing devices. |
| 327 | for (size_t i = 0; i < device_count; ++i) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 328 | PlatformUsbDevice platform_device = platform_devices[i]; |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 329 | // Ignore some devices. |
skyostil | 61e1aee2 | 2016-08-12 14:12:09 | [diff] [blame] | 330 | if (base::ContainsValue(ignored_devices_, platform_device)) { |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 331 | existing_ignored_devices.insert(platform_device); |
| 332 | refresh_complete.Run(); |
| 333 | continue; |
| 334 | } |
| 335 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 336 | auto it = platform_devices_.find(platform_device); |
| 337 | |
| 338 | if (it == platform_devices_.end()) { |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 339 | new_devices.push_back(platform_device); |
| 340 | } else { |
| 341 | it->second->set_visited(true); |
| 342 | refresh_complete.Run(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 346 | // Remove devices not seen in this enumeration. |
| 347 | for (PlatformDeviceMap::iterator it = platform_devices_.begin(); |
| 348 | it != platform_devices_.end(); |
| 349 | /* incremented internally */) { |
| 350 | PlatformDeviceMap::iterator current = it++; |
| 351 | const scoped_refptr<UsbDeviceImpl>& device = current->second; |
| 352 | if (device->was_visited()) { |
| 353 | device->set_visited(false); |
| 354 | } else { |
| 355 | RemoveDevice(device); |
| 356 | } |
| 357 | } |
| 358 | |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 359 | // Remove devices not seen in this enumeration from |ignored_devices_|. |
| 360 | for (auto it = ignored_devices_.begin(); it != ignored_devices_.end(); |
| 361 | /* incremented internally */) { |
| 362 | auto current = it++; |
skyostil | 61e1aee2 | 2016-08-12 14:12:09 | [diff] [blame] | 363 | if (!base::ContainsValue(existing_ignored_devices, *current)) { |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 364 | libusb_unref_device(*current); |
| 365 | ignored_devices_.erase(current); |
| 366 | } |
| 367 | } |
| 368 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 369 | for (PlatformUsbDevice platform_device : new_devices) { |
| 370 | EnumerateDevice(platform_device, refresh_complete); |
| 371 | } |
| 372 | |
| 373 | libusb_free_device_list(platform_devices, true); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 374 | } |
| 375 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 376 | void UsbServiceImpl::RefreshDevicesComplete() { |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 377 | DCHECK(CalledOnValidThread()); |
| 378 | DCHECK(enumeration_in_progress_); |
| 379 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 380 | enumeration_ready_ = true; |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 381 | enumeration_in_progress_ = false; |
| 382 | devices_being_enumerated_.clear(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 383 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 384 | if (!pending_enumeration_callbacks_.empty()) { |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 385 | std::vector<scoped_refptr<UsbDevice>> result; |
| 386 | result.reserve(devices().size()); |
| 387 | for (const auto& map_entry : devices()) |
| 388 | result.push_back(map_entry.second); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 389 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 390 | std::vector<GetDevicesCallback> callbacks; |
| 391 | callbacks.swap(pending_enumeration_callbacks_); |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 392 | for (const GetDevicesCallback& callback : callbacks) |
| 393 | callback.Run(result); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 394 | } |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 395 | |
| 396 | if (!pending_path_enumerations_.empty()) { |
reillyg | ef1a7a5 | 2015-06-17 16:50:15 | [diff] [blame] | 397 | RefreshDevices(); |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 398 | } |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 399 | } |
| 400 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 401 | void UsbServiceImpl::EnumerateDevice(PlatformUsbDevice platform_device, |
| 402 | const base::Closure& refresh_complete) { |
reillyg | c4baf41 | 2015-08-08 22:51:04 | [diff] [blame] | 403 | DCHECK(context_); |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 404 | devices_being_enumerated_.insert(platform_device); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 405 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 406 | libusb_device_descriptor descriptor; |
| 407 | int rv = libusb_get_device_descriptor(platform_device, &descriptor); |
| 408 | if (rv == LIBUSB_SUCCESS) { |
reillyg | 8fe5eaba | 2016-04-05 21:34:12 | [diff] [blame] | 409 | if (descriptor.bDeviceClass == LIBUSB_CLASS_HUB) { |
| 410 | // Don't try to enumerate hubs. We never want to connect to a hub. |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 411 | libusb_ref_device(platform_device); |
| 412 | ignored_devices_.insert(platform_device); |
reillyg | 8fe5eaba | 2016-04-05 21:34:12 | [diff] [blame] | 413 | refresh_complete.Run(); |
| 414 | return; |
| 415 | } |
| 416 | |
reillyg | a8a7407b | 2016-04-07 02:20:29 | [diff] [blame] | 417 | scoped_refptr<UsbDeviceImpl> device(new UsbDeviceImpl( |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 418 | context_, platform_device, descriptor, blocking_task_runner())); |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 419 | base::Closure add_device = |
| 420 | base::Bind(&UsbServiceImpl::AddDevice, weak_factory_.GetWeakPtr(), |
| 421 | refresh_complete, device); |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 422 | base::Closure enumeration_failed = base::Bind( |
| 423 | &UsbServiceImpl::EnumerationFailed, weak_factory_.GetWeakPtr(), |
| 424 | platform_device, refresh_complete); |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 425 | bool read_bos_descriptors = descriptor.bcdUSB >= kUsbVersion2_1; |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 426 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 427 | if (descriptor.iManufacturer == 0 && descriptor.iProduct == 0 && |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 428 | descriptor.iSerialNumber == 0 && !read_bos_descriptors) { |
| 429 | // Don't bother disturbing the device if it has no descriptors to offer. |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 430 | add_device.Run(); |
| 431 | } else { |
reillyg | 6eb7a8a | 2015-08-06 00:34:07 | [diff] [blame] | 432 | device->Open(base::Bind(&OnDeviceOpenedReadDescriptors, |
| 433 | descriptor.iManufacturer, descriptor.iProduct, |
| 434 | descriptor.iSerialNumber, read_bos_descriptors, |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 435 | add_device, enumeration_failed)); |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 436 | } |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 437 | } else { |
| 438 | USB_LOG(EVENT) << "Failed to get device descriptor: " |
| 439 | << ConvertPlatformUsbErrorToString(rv); |
| 440 | refresh_complete.Run(); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | void UsbServiceImpl::AddDevice(const base::Closure& refresh_complete, |
| 445 | scoped_refptr<UsbDeviceImpl> device) { |
| 446 | auto it = devices_being_enumerated_.find(device->platform_device()); |
| 447 | if (it == devices_being_enumerated_.end()) { |
| 448 | // Device was removed while being enumerated. |
| 449 | refresh_complete.Run(); |
| 450 | return; |
| 451 | } |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 452 | |
| 453 | platform_devices_[device->platform_device()] = device; |
skyostil | 61e1aee2 | 2016-08-12 14:12:09 | [diff] [blame] | 454 | DCHECK(!base::ContainsKey(devices(), device->guid())); |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 455 | devices()[device->guid()] = device; |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 456 | |
| 457 | USB_LOG(USER) << "USB device added: vendor=" << device->vendor_id() << " \"" |
| 458 | << device->manufacturer_string() |
| 459 | << "\", product=" << device->product_id() << " \"" |
| 460 | << device->product_string() << "\", serial=\"" |
rockot | dfbc94d1d | 2015-06-01 20:53:09 | [diff] [blame] | 461 | << device->serial_number() << "\", guid=" << device->guid(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 462 | |
| 463 | if (enumeration_ready_) { |
| 464 | NotifyDeviceAdded(device); |
| 465 | } |
| 466 | |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 467 | refresh_complete.Run(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | void UsbServiceImpl::RemoveDevice(scoped_refptr<UsbDeviceImpl> device) { |
| 471 | platform_devices_.erase(device->platform_device()); |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 472 | devices().erase(device->guid()); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 473 | |
rockot | dfbc94d1d | 2015-06-01 20:53:09 | [diff] [blame] | 474 | USB_LOG(USER) << "USB device removed: guid=" << device->guid(); |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 475 | |
| 476 | NotifyDeviceRemoved(device); |
| 477 | device->OnDisconnect(); |
| 478 | } |
| 479 | |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 480 | // static |
| 481 | int LIBUSB_CALL UsbServiceImpl::HotplugCallback(libusb_context* context, |
| 482 | PlatformUsbDevice device, |
| 483 | libusb_hotplug_event event, |
| 484 | void* user_data) { |
| 485 | // It is safe to access the UsbServiceImpl* here because libusb takes a lock |
| 486 | // around registering, deregistering and calling hotplug callback functions |
| 487 | // and so guarantees that this function will not be called by the event |
| 488 | // processing thread after it has been deregistered. |
| 489 | UsbServiceImpl* self = reinterpret_cast<UsbServiceImpl*>(user_data); |
| 490 | switch (event) { |
| 491 | case LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED: |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 492 | libusb_ref_device(device); // Released in OnPlatformDeviceAdded. |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 493 | if (self->task_runner()->BelongsToCurrentThread()) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 494 | self->OnPlatformDeviceAdded(device); |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 495 | } else { |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 496 | self->task_runner()->PostTask( |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 497 | FROM_HERE, base::Bind(&UsbServiceImpl::OnPlatformDeviceAdded, |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 498 | base::Unretained(self), device)); |
| 499 | } |
| 500 | break; |
| 501 | case LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT: |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 502 | libusb_ref_device(device); // Released in OnPlatformDeviceRemoved. |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 503 | if (self->task_runner()->BelongsToCurrentThread()) { |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 504 | self->OnPlatformDeviceRemoved(device); |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 505 | } else { |
reillyg | b36f8b59 | 2016-06-16 08:40:35 | [diff] [blame] | 506 | self->task_runner()->PostTask( |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 507 | FROM_HERE, base::Bind(&UsbServiceImpl::OnPlatformDeviceRemoved, |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 508 | base::Unretained(self), device)); |
| 509 | } |
| 510 | break; |
| 511 | default: |
| 512 | NOTREACHED(); |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 518 | void UsbServiceImpl::OnPlatformDeviceAdded(PlatformUsbDevice platform_device) { |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 519 | DCHECK(CalledOnValidThread()); |
skyostil | 61e1aee2 | 2016-08-12 14:12:09 | [diff] [blame] | 520 | DCHECK(!base::ContainsKey(platform_devices_, platform_device)); |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 521 | EnumerateDevice(platform_device, base::Bind(&base::DoNothing)); |
| 522 | libusb_unref_device(platform_device); |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 523 | } |
| 524 | |
reillyg | b87cb27 | 2015-04-16 19:11:04 | [diff] [blame] | 525 | void UsbServiceImpl::OnPlatformDeviceRemoved( |
| 526 | PlatformUsbDevice platform_device) { |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 527 | DCHECK(CalledOnValidThread()); |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 528 | PlatformDeviceMap::iterator it = platform_devices_.find(platform_device); |
| 529 | if (it != platform_devices_.end()) { |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 530 | RemoveDevice(it->second); |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 531 | } else { |
reillyg | 91fa995 | 2015-06-15 21:47:19 | [diff] [blame] | 532 | devices_being_enumerated_.erase(platform_device); |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 533 | } |
reillyg | 0ae3a999 | 2015-01-05 20:12:42 | [diff] [blame] | 534 | libusb_unref_device(platform_device); |
| 535 | } |
| 536 | |
juncai | d23f2ba | 2016-04-27 23:32:26 | [diff] [blame] | 537 | void UsbServiceImpl::EnumerationFailed(PlatformUsbDevice platform_device, |
| 538 | const base::Closure& refresh_complete) { |
| 539 | libusb_ref_device(platform_device); |
| 540 | ignored_devices_.insert(platform_device); |
| 541 | refresh_complete.Run(); |
| 542 | } |
| 543 | |
reillyg | d77718d | 2014-09-04 00:57:56 | [diff] [blame] | 544 | } // namespace device |