1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/usb/usb_device_impl.h"
#include <algorithm>
#include "base/bind.h"
#include "base/location.h"
#include "base/sequenced_task_runner.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "components/device_event_log/device_event_log.h"
#include "device/usb/usb_context.h"
#include "device/usb/usb_descriptors.h"
#include "device/usb/usb_device_handle_impl.h"
#include "device/usb/usb_error.h"
#include "third_party/libusb/src/libusb/libusb.h"
#if defined(OS_CHROMEOS)
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/permission_broker_client.h"
#include "dbus/file_descriptor.h"
#endif // defined(OS_CHROMEOS)
namespace device {
namespace {
UsbEndpointDirection GetDirection(
const libusb_endpoint_descriptor* descriptor) {
switch (descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
case LIBUSB_ENDPOINT_IN:
return USB_DIRECTION_INBOUND;
case LIBUSB_ENDPOINT_OUT:
return USB_DIRECTION_OUTBOUND;
default:
NOTREACHED();
return USB_DIRECTION_INBOUND;
}
}
UsbSynchronizationType GetSynchronizationType(
const libusb_endpoint_descriptor* descriptor) {
switch ((descriptor->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) >> 2) {
case LIBUSB_ISO_SYNC_TYPE_NONE:
return USB_SYNCHRONIZATION_NONE;
case LIBUSB_ISO_SYNC_TYPE_ASYNC:
return USB_SYNCHRONIZATION_ASYNCHRONOUS;
case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
return USB_SYNCHRONIZATION_ADAPTIVE;
case LIBUSB_ISO_SYNC_TYPE_SYNC:
return USB_SYNCHRONIZATION_SYNCHRONOUS;
default:
NOTREACHED();
return USB_SYNCHRONIZATION_NONE;
}
}
UsbTransferType GetTransferType(const libusb_endpoint_descriptor* descriptor) {
switch (descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
case LIBUSB_TRANSFER_TYPE_CONTROL:
return USB_TRANSFER_CONTROL;
case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
return USB_TRANSFER_ISOCHRONOUS;
case LIBUSB_TRANSFER_TYPE_BULK:
return USB_TRANSFER_BULK;
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
return USB_TRANSFER_INTERRUPT;
default:
NOTREACHED();
return USB_TRANSFER_CONTROL;
}
}
UsbUsageType GetUsageType(const libusb_endpoint_descriptor* descriptor) {
switch ((descriptor->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) >> 4) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return USB_USAGE_DATA;
case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
return USB_USAGE_FEEDBACK;
case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
return USB_USAGE_EXPLICIT_FEEDBACK;
default:
NOTREACHED();
return USB_USAGE_DATA;
}
}
} // namespace
UsbDeviceImpl::UsbDeviceImpl(
scoped_refptr<UsbContext> context,
PlatformUsbDevice platform_device,
uint16 vendor_id,
uint16 product_id,
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner)
: UsbDevice(vendor_id,
product_id,
base::string16(),
base::string16(),
base::string16()),
platform_device_(platform_device),
context_(context),
task_runner_(base::ThreadTaskRunnerHandle::Get()),
blocking_task_runner_(blocking_task_runner) {
CHECK(platform_device) << "platform_device cannot be NULL";
libusb_ref_device(platform_device);
RefreshConfiguration();
}
UsbDeviceImpl::~UsbDeviceImpl() {
// The destructor must be safe to call from any thread.
libusb_unref_device(platform_device_);
}
#if defined(OS_CHROMEOS)
void UsbDeviceImpl::CheckUsbAccess(const ResultCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
chromeos::PermissionBrokerClient* client =
chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
DCHECK(client) << "Could not get permission broker client.";
client->CheckPathAccess(device_path_, callback);
}
#endif // defined(OS_CHROMEOS)
void UsbDeviceImpl::Open(const OpenCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
#if defined(OS_CHROMEOS)
chromeos::PermissionBrokerClient* client =
chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
DCHECK(client) << "Could not get permission broker client.";
client->OpenPath(
device_path_,
base::Bind(&UsbDeviceImpl::OnOpenRequestComplete, this, callback));
#else
blocking_task_runner_->PostTask(
FROM_HERE,
base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback));
#endif // defined(OS_CHROMEOS)
}
#if defined(OS_CHROMEOS)
void UsbDeviceImpl::OpenInterface(int interface_id,
const OpenCallback& callback) {
chromeos::PermissionBrokerClient* client =
chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
DCHECK(client) << "Could not get permission broker client.";
client->RequestPathAccess(
device_path_, interface_id,
base::Bind(&UsbDeviceImpl::OnPathAccessRequestComplete, this, callback));
}
#endif // defined(OS_CHROMEOS)
bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) {
DCHECK(thread_checker_.CalledOnValidThread());
for (HandlesVector::iterator it = handles_.begin(); it != handles_.end();
++it) {
if (it->get() == handle.get()) {
(*it)->InternalClose();
handles_.erase(it);
return true;
}
}
return false;
}
const UsbConfigDescriptor* UsbDeviceImpl::GetConfiguration() {
DCHECK(thread_checker_.CalledOnValidThread());
return configuration_.get();
}
void UsbDeviceImpl::OnDisconnect() {
DCHECK(thread_checker_.CalledOnValidThread());
// Swap the list of handles into a local variable because closing all open
// handles may release the last reference to this object.
HandlesVector handles;
swap(handles, handles_);
for (const scoped_refptr<UsbDeviceHandleImpl>& handle : handles_) {
handle->InternalClose();
}
}
void UsbDeviceImpl::RefreshConfiguration() {
libusb_config_descriptor* platform_config;
int rv =
libusb_get_active_config_descriptor(platform_device_, &platform_config);
if (rv != LIBUSB_SUCCESS) {
USB_LOG(EVENT) << "Failed to get config descriptor: "
<< ConvertPlatformUsbErrorToString(rv);
return;
}
configuration_.reset(new UsbConfigDescriptor());
configuration_->configuration_value = platform_config->bConfigurationValue;
configuration_->self_powered = (platform_config->bmAttributes & 0x40) != 0;
configuration_->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0;
configuration_->maximum_power = platform_config->MaxPower * 2;
for (size_t i = 0; i < platform_config->bNumInterfaces; ++i) {
const struct libusb_interface* platform_interface =
&platform_config->interface[i];
for (int j = 0; j < platform_interface->num_altsetting; ++j) {
const struct libusb_interface_descriptor* platform_alt_setting =
&platform_interface->altsetting[j];
UsbInterfaceDescriptor interface;
interface.interface_number = platform_alt_setting->bInterfaceNumber;
interface.alternate_setting = platform_alt_setting->bAlternateSetting;
interface.interface_class = platform_alt_setting->bInterfaceClass;
interface.interface_subclass = platform_alt_setting->bInterfaceSubClass;
interface.interface_protocol = platform_alt_setting->bInterfaceProtocol;
for (size_t k = 0; k < platform_alt_setting->bNumEndpoints; ++k) {
const struct libusb_endpoint_descriptor* platform_endpoint =
&platform_alt_setting->endpoint[k];
UsbEndpointDescriptor endpoint;
endpoint.address = platform_endpoint->bEndpointAddress;
endpoint.direction = GetDirection(platform_endpoint);
endpoint.maximum_packet_size = platform_endpoint->wMaxPacketSize;
endpoint.synchronization_type =
GetSynchronizationType(platform_endpoint);
endpoint.transfer_type = GetTransferType(platform_endpoint);
endpoint.usage_type = GetUsageType(platform_endpoint);
endpoint.polling_interval = platform_endpoint->bInterval;
endpoint.extra_data = std::vector<uint8_t>(
platform_endpoint->extra,
platform_endpoint->extra + platform_endpoint->extra_length);
interface.endpoints.push_back(endpoint);
}
interface.extra_data = std::vector<uint8_t>(
platform_alt_setting->extra,
platform_alt_setting->extra + platform_alt_setting->extra_length);
configuration_->interfaces.push_back(interface);
}
}
configuration_->extra_data = std::vector<uint8_t>(
platform_config->extra,
platform_config->extra + platform_config->extra_length);
libusb_free_config_descriptor(platform_config);
}
#if defined(OS_CHROMEOS)
void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback,
dbus::FileDescriptor fd) {
blocking_task_runner_->PostTask(
FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this,
base::Passed(&fd), callback));
}
void UsbDeviceImpl::OnPathAccessRequestComplete(const OpenCallback& callback,
bool success) {
if (success) {
blocking_task_runner_->PostTask(
FROM_HERE,
base::Bind(&UsbDeviceImpl::OpenOnBlockingThread, this, callback));
} else {
// Once permission_broker changes for crbug.com/496469 land,
// RequestPathAccess will fail for partially claimed devices regardless of
// the interface number requested. Fall back to OpenPath to prevent a
// regression. This code will then be removed.
chromeos::PermissionBrokerClient* client =
chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient();
DCHECK(client) << "Could not get permission broker client.";
client->OpenPath(
device_path_,
base::Bind(&UsbDeviceImpl::OnOpenRequestComplete, this, callback));
}
}
void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd,
const OpenCallback& callback) {
fd.CheckValidity();
if (!fd.is_valid()) {
USB_LOG(EVENT) << "Did not get valid device handle from permission broker.";
task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
return;
}
PlatformUsbDeviceHandle handle;
const int rv = libusb_open_fd(platform_device_, fd.TakeValue(), &handle);
if (LIBUSB_SUCCESS == rv) {
task_runner_->PostTask(
FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
} else {
USB_LOG(EVENT) << "Failed to open device: "
<< ConvertPlatformUsbErrorToString(rv);
task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
}
}
#endif // defined(OS_CHROMEOS)
void UsbDeviceImpl::OpenOnBlockingThread(const OpenCallback& callback) {
PlatformUsbDeviceHandle handle;
const int rv = libusb_open(platform_device_, &handle);
if (LIBUSB_SUCCESS == rv) {
task_runner_->PostTask(
FROM_HERE, base::Bind(&UsbDeviceImpl::Opened, this, handle, callback));
} else {
USB_LOG(EVENT) << "Failed to open device: "
<< ConvertPlatformUsbErrorToString(rv);
task_runner_->PostTask(FROM_HERE, base::Bind(callback, nullptr));
}
}
void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle,
const OpenCallback& callback) {
DCHECK(thread_checker_.CalledOnValidThread());
scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl(
context_, this, platform_handle, blocking_task_runner_);
handles_.push_back(device_handle);
callback.Run(device_handle);
}
} // namespace device
|