components: Fix 'gn check' errors for web_cache and web_modal.
[chromium-blink-merge.git] / device / usb / usb_device_impl.cc
blob114e6acaa568fec23630556cd6347d1519aec033
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/usb/usb_device_impl.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/sequenced_task_runner.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/stl_util.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "components/device_event_log/device_event_log.h"
16 #include "device/usb/usb_context.h"
17 #include "device/usb/usb_descriptors.h"
18 #include "device/usb/usb_device_handle_impl.h"
19 #include "device/usb/usb_error.h"
20 #include "third_party/libusb/src/libusb/libusb.h"
22 #if defined(OS_CHROMEOS)
23 #include "chromeos/dbus/dbus_thread_manager.h"
24 #include "chromeos/dbus/permission_broker_client.h"
25 #include "dbus/file_descriptor.h"
26 #endif // defined(OS_CHROMEOS)
28 namespace device {
30 namespace {
32 UsbEndpointDirection GetDirection(
33 const libusb_endpoint_descriptor* descriptor) {
34 switch (descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
35 case LIBUSB_ENDPOINT_IN:
36 return USB_DIRECTION_INBOUND;
37 case LIBUSB_ENDPOINT_OUT:
38 return USB_DIRECTION_OUTBOUND;
39 default:
40 NOTREACHED();
41 return USB_DIRECTION_INBOUND;
45 UsbSynchronizationType GetSynchronizationType(
46 const libusb_endpoint_descriptor* descriptor) {
47 switch ((descriptor->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) >> 2) {
48 case LIBUSB_ISO_SYNC_TYPE_NONE:
49 return USB_SYNCHRONIZATION_NONE;
50 case LIBUSB_ISO_SYNC_TYPE_ASYNC:
51 return USB_SYNCHRONIZATION_ASYNCHRONOUS;
52 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
53 return USB_SYNCHRONIZATION_ADAPTIVE;
54 case LIBUSB_ISO_SYNC_TYPE_SYNC:
55 return USB_SYNCHRONIZATION_SYNCHRONOUS;
56 default:
57 NOTREACHED();
58 return USB_SYNCHRONIZATION_NONE;
62 UsbTransferType GetTransferType(const libusb_endpoint_descriptor* descriptor) {
63 switch (descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
64 case LIBUSB_TRANSFER_TYPE_CONTROL:
65 return USB_TRANSFER_CONTROL;
66 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
67 return USB_TRANSFER_ISOCHRONOUS;
68 case LIBUSB_TRANSFER_TYPE_BULK:
69 return USB_TRANSFER_BULK;
70 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
71 return USB_TRANSFER_INTERRUPT;
72 default:
73 NOTREACHED();
74 return USB_TRANSFER_CONTROL;
78 UsbUsageType GetUsageType(const libusb_endpoint_descriptor* descriptor) {
79 switch ((descriptor->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) >> 4) {
80 case LIBUSB_ISO_USAGE_TYPE_DATA:
81 return USB_USAGE_DATA;
82 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
83 return USB_USAGE_FEEDBACK;
84 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
85 return USB_USAGE_EXPLICIT_FEEDBACK;
86 default:
87 NOTREACHED();
88 return USB_USAGE_DATA;
92 } // namespace
94 UsbDeviceImpl::UsbDeviceImpl(
95 scoped_refptr<UsbContext> context,
96 PlatformUsbDevice platform_device,
97 uint16 vendor_id,
98 uint16 product_id,
99 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
100 : UsbDevice(vendor_id,
101 product_id,
102 base::string16(),
103 base::string16(),
104 base::string16()),
105 platform_device_(platform_device),
106 context_(context),
107 task_runner_(base::ThreadTaskRunnerHandle::Get()),
108 blocking_task_runner_(blocking_task_runner) {
109 CHECK(platform_device) << "platform_device cannot be NULL";
110 libusb_ref_device(platform_device);
111 RefreshConfiguration();
114 UsbDeviceImpl::~UsbDeviceImpl() {
115 // The destructor must be safe to call from any thread.
116 libusb_unref_device(platform_device_);
119 #if defined(OS_CHROMEOS)
121 void UsbDeviceImpl::CheckUsbAccess(const ResultCallback& callback) {
122 DCHECK(thread_checker_.CalledOnValidThread());
123 chromeos::PermissionBrokerClient* client =
124 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
125 DCHECK(client) << "Could not get permission broker client.";
126 client->CheckPathAccess(device_path_, callback);
129 #endif // defined(OS_CHROMEOS)
131 void UsbDeviceImpl::Open(const OpenCallback& callback) {
132 DCHECK(thread_checker_.CalledOnValidThread());
134 #if defined(OS_CHROMEOS)
135 chromeos::PermissionBrokerClient* client =
136 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
137 DCHECK(client) << "Could not get permission broker client.";
138 client->OpenPath(
139 device_path_,
140 base::Bind(&UsbDeviceImpl::OnOpenRequestComplete, this, callback));
141 #else
142 blocking_task_runner_->PostTask(
143 FROM_HERE,
144 base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback));
145 #endif // defined(OS_CHROMEOS)
148 bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) {
149 DCHECK(thread_checker_.CalledOnValidThread());
151 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end();
152 ++it) {
153 if (it->get() == handle.get()) {
154 (*it)->InternalClose();
155 handles_.erase(it);
156 return true;
159 return false;
162 const UsbConfigDescriptor* UsbDeviceImpl::GetConfiguration() {
163 DCHECK(thread_checker_.CalledOnValidThread());
164 return configuration_.get();
167 void UsbDeviceImpl::OnDisconnect() {
168 DCHECK(thread_checker_.CalledOnValidThread());
170 // Swap the list of handles into a local variable because closing all open
171 // handles may release the last reference to this object.
172 HandlesVector handles;
173 swap(handles, handles_);
175 for (const scoped_refptr<UsbDeviceHandleImpl>& handle : handles_) {
176 handle->InternalClose();
180 void UsbDeviceImpl::RefreshConfiguration() {
181 libusb_config_descriptor* platform_config;
182 int rv =
183 libusb_get_active_config_descriptor(platform_device_, &platform_config);
184 if (rv != LIBUSB_SUCCESS) {
185 USB_LOG(EVENT) << "Failed to get config descriptor: "
186 << ConvertPlatformUsbErrorToString(rv);
187 return;
190 configuration_.reset(new UsbConfigDescriptor());
191 configuration_->configuration_value = platform_config->bConfigurationValue;
192 configuration_->self_powered = (platform_config->bmAttributes & 0x40) != 0;
193 configuration_->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0;
194 configuration_->maximum_power = platform_config->MaxPower * 2;
196 for (size_t i = 0; i < platform_config->bNumInterfaces; ++i) {
197 const struct libusb_interface* platform_interface =
198 &platform_config->interface[i];
199 for (int j = 0; j < platform_interface->num_altsetting; ++j) {
200 const struct libusb_interface_descriptor* platform_alt_setting =
201 &platform_interface->altsetting[j];
202 UsbInterfaceDescriptor interface;
204 interface.interface_number = platform_alt_setting->bInterfaceNumber;
205 interface.alternate_setting = platform_alt_setting->bAlternateSetting;
206 interface.interface_class = platform_alt_setting->bInterfaceClass;
207 interface.interface_subclass = platform_alt_setting->bInterfaceSubClass;
208 interface.interface_protocol = platform_alt_setting->bInterfaceProtocol;
210 for (size_t k = 0; k < platform_alt_setting->bNumEndpoints; ++k) {
211 const struct libusb_endpoint_descriptor* platform_endpoint =
212 &platform_alt_setting->endpoint[k];
213 UsbEndpointDescriptor endpoint;
215 endpoint.address = platform_endpoint->bEndpointAddress;
216 endpoint.direction = GetDirection(platform_endpoint);
217 endpoint.maximum_packet_size = platform_endpoint->wMaxPacketSize;
218 endpoint.synchronization_type =
219 GetSynchronizationType(platform_endpoint);
220 endpoint.transfer_type = GetTransferType(platform_endpoint);
221 endpoint.usage_type = GetUsageType(platform_endpoint);
222 endpoint.polling_interval = platform_endpoint->bInterval;
223 endpoint.extra_data = std::vector<uint8_t>(
224 platform_endpoint->extra,
225 platform_endpoint->extra + platform_endpoint->extra_length);
227 interface.endpoints.push_back(endpoint);
230 interface.extra_data = std::vector<uint8_t>(
231 platform_alt_setting->extra,
232 platform_alt_setting->extra + platform_alt_setting->extra_length);
234 configuration_->interfaces.push_back(interface);
238 configuration_->extra_data = std::vector<uint8_t>(
239 platform_config->extra,
240 platform_config->extra + platform_config->extra_length);
242 libusb_free_config_descriptor(platform_config);
245 #if defined(OS_CHROMEOS)
247 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback,
248 dbus::FileDescriptor fd) {
249 blocking_task_runner_->PostTask(
250 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this,
251 base::Passed(&fd), callback));
254 void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd,
255 const OpenCallback& callback) {
256 fd.CheckValidity();
257 if (!fd.is_valid()) {
258 USB_LOG(EVENT) << "Did not get valid device handle from permission broker.";
259 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
260 return;
263 PlatformUsbDeviceHandle handle;
264 const int rv = libusb_open_fd(platform_device_, fd.TakeValue(), &handle);
265 if (LIBUSB_SUCCESS == rv) {
266 task_runner_->PostTask(
267 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
268 } else {
269 USB_LOG(EVENT) << "Failed to open device: "
270 << ConvertPlatformUsbErrorToString(rv);
271 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
275 #endif // defined(OS_CHROMEOS)
277 void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
278 PlatformUsbDeviceHandle handle;
279 const int rv = libusb_open(platform_device_, &handle);
280 if (LIBUSB_SUCCESS == rv) {
281 task_runner_->PostTask(
282 FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
283 } else {
284 USB_LOG(EVENT) << "Failed to open device: "
285 << ConvertPlatformUsbErrorToString(rv);
286 task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
290 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle,
291 const OpenCallback& callback) {
292 DCHECK(thread_checker_.CalledOnValidThread());
293 scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl(
294 context_, this, platform_handle, blocking_task_runner_);
295 handles_.push_back(device_handle);
296 callback.Run(device_handle);
299 } // namespace device