Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Side by Side Diff: device/usb/usb_device_impl.cc

Issue 562763002: Convert device::UsbConfigDescriptor and friends to structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add interface and endpoint descriptor iterator typedefs. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/usb/usb_device_impl.h ('k') | device/usb/usb_interface.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/usb/usb_device_impl.h" 5 #include "device/usb/usb_device_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/single_thread_task_runner.h" 11 #include "base/single_thread_task_runner.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
14 #include "device/usb/usb_context.h" 14 #include "device/usb/usb_context.h"
15 #include "device/usb/usb_descriptors.h"
15 #include "device/usb/usb_device_handle_impl.h" 16 #include "device/usb/usb_device_handle_impl.h"
16 #include "device/usb/usb_error.h" 17 #include "device/usb/usb_error.h"
17 #include "device/usb/usb_interface_impl.h"
18 #include "third_party/libusb/src/libusb/libusb.h" 18 #include "third_party/libusb/src/libusb/libusb.h"
19 19
20 #if defined(OS_CHROMEOS) 20 #if defined(OS_CHROMEOS)
21 #include "base/sys_info.h" 21 #include "base/sys_info.h"
22 #include "chromeos/dbus/dbus_thread_manager.h" 22 #include "chromeos/dbus/dbus_thread_manager.h"
23 #include "chromeos/dbus/permission_broker_client.h" 23 #include "chromeos/dbus/permission_broker_client.h"
24 #endif // defined(OS_CHROMEOS) 24 #endif // defined(OS_CHROMEOS)
25 25
26 namespace device {
27
26 namespace { 28 namespace {
27 29
28 #if defined(OS_CHROMEOS) 30 #if defined(OS_CHROMEOS)
29 void OnRequestUsbAccessReplied( 31 void OnRequestUsbAccessReplied(
30 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 32 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
31 const base::Callback<void(bool success)>& callback, 33 const base::Callback<void(bool success)>& callback,
32 bool success) { 34 bool success) {
33 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); 35 task_runner->PostTask(FROM_HERE, base::Bind(callback, success));
34 } 36 }
35 #endif // defined(OS_CHROMEOS) 37 #endif // defined(OS_CHROMEOS)
36 38
39 UsbEndpointDirection GetDirection(
40 const libusb_endpoint_descriptor* descriptor) {
41 switch (descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
42 case LIBUSB_ENDPOINT_IN:
43 return USB_DIRECTION_INBOUND;
44 case LIBUSB_ENDPOINT_OUT:
45 return USB_DIRECTION_OUTBOUND;
46 default:
47 NOTREACHED();
48 return USB_DIRECTION_INBOUND;
49 }
50 }
51
52 UsbSynchronizationType GetSynchronizationType(
53 const libusb_endpoint_descriptor* descriptor) {
54 switch (descriptor->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
55 case LIBUSB_ISO_SYNC_TYPE_NONE:
56 return USB_SYNCHRONIZATION_NONE;
57 case LIBUSB_ISO_SYNC_TYPE_ASYNC:
58 return USB_SYNCHRONIZATION_ASYNCHRONOUS;
59 case LIBUSB_ISO_SYNC_TYPE_ADAPTIVE:
60 return USB_SYNCHRONIZATION_ADAPTIVE;
61 case LIBUSB_ISO_SYNC_TYPE_SYNC:
62 return USB_SYNCHRONIZATION_SYNCHRONOUS;
63 default:
64 NOTREACHED();
65 return USB_SYNCHRONIZATION_NONE;
66 }
67 }
68
69 UsbTransferType GetTransferType(const libusb_endpoint_descriptor* descriptor) {
70 switch (descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
71 case LIBUSB_TRANSFER_TYPE_CONTROL:
72 return USB_TRANSFER_CONTROL;
73 case LIBUSB_TRANSFER_TYPE_ISOCHRONOUS:
74 return USB_TRANSFER_ISOCHRONOUS;
75 case LIBUSB_TRANSFER_TYPE_BULK:
76 return USB_TRANSFER_BULK;
77 case LIBUSB_TRANSFER_TYPE_INTERRUPT:
78 return USB_TRANSFER_INTERRUPT;
79 default:
80 NOTREACHED();
81 return USB_TRANSFER_CONTROL;
82 }
83 }
84
85 UsbUsageType GetUsageType(const libusb_endpoint_descriptor* descriptor) {
86 switch (descriptor->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
87 case LIBUSB_ISO_USAGE_TYPE_DATA:
88 return USB_USAGE_DATA;
89 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK:
90 return USB_USAGE_FEEDBACK;
91 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT:
92 return USB_USAGE_EXPLICIT_FEEDBACK;
93 default:
94 NOTREACHED();
95 return USB_USAGE_DATA;
96 }
97 }
98
37 } // namespace 99 } // namespace
38 100
39 namespace device {
40
41 UsbDeviceImpl::UsbDeviceImpl( 101 UsbDeviceImpl::UsbDeviceImpl(
42 scoped_refptr<UsbContext> context, 102 scoped_refptr<UsbContext> context,
43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, 103 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
44 PlatformUsbDevice platform_device, 104 PlatformUsbDevice platform_device,
45 uint16 vendor_id, 105 uint16 vendor_id,
46 uint16 product_id, 106 uint16 product_id,
47 uint32 unique_id) 107 uint32 unique_id)
48 : UsbDevice(vendor_id, product_id, unique_id), 108 : UsbDevice(vendor_id, product_id, unique_id),
49 platform_device_(platform_device), 109 platform_device_(platform_device),
110 current_configuration_cached_(false),
50 context_(context), 111 context_(context),
51 ui_task_runner_(ui_task_runner) { 112 ui_task_runner_(ui_task_runner) {
52 CHECK(platform_device) << "platform_device cannot be NULL"; 113 CHECK(platform_device) << "platform_device cannot be NULL";
53 libusb_ref_device(platform_device); 114 libusb_ref_device(platform_device);
54 } 115 }
55 116
56 UsbDeviceImpl::~UsbDeviceImpl() { 117 UsbDeviceImpl::~UsbDeviceImpl() {
57 DCHECK(thread_checker_.CalledOnValidThread()); 118 DCHECK(thread_checker_.CalledOnValidThread());
58 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); 119 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end();
59 ++it) { 120 ++it) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 } 155 }
95 } 156 }
96 157
97 #endif 158 #endif
98 159
99 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { 160 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() {
100 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
101 PlatformUsbDeviceHandle handle; 162 PlatformUsbDeviceHandle handle;
102 const int rv = libusb_open(platform_device_, &handle); 163 const int rv = libusb_open(platform_device_, &handle);
103 if (LIBUSB_SUCCESS == rv) { 164 if (LIBUSB_SUCCESS == rv) {
104 scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces(); 165 GetConfiguration();
105 if (!interfaces.get()) 166 if (!current_configuration_cached_) {
106 return NULL; 167 return NULL;
168 }
107 scoped_refptr<UsbDeviceHandleImpl> device_handle = 169 scoped_refptr<UsbDeviceHandleImpl> device_handle =
108 new UsbDeviceHandleImpl(context_, this, handle, interfaces); 170 new UsbDeviceHandleImpl(context_, this, handle, current_configuration_);
109 handles_.push_back(device_handle); 171 handles_.push_back(device_handle);
110 return device_handle; 172 return device_handle;
111 } else { 173 } else {
112 VLOG(1) << "Failed to open device: " << ConvertPlatformUsbErrorToString(rv); 174 VLOG(1) << "Failed to open device: " << ConvertPlatformUsbErrorToString(rv);
113 return NULL; 175 return NULL;
114 } 176 }
115 } 177 }
116 178
117 bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { 179 bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) {
118 DCHECK(thread_checker_.CalledOnValidThread()); 180 DCHECK(thread_checker_.CalledOnValidThread());
119 181
120 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); 182 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end();
121 ++it) { 183 ++it) {
122 if (it->get() == handle.get()) { 184 if (it->get() == handle.get()) {
123 (*it)->InternalClose(); 185 (*it)->InternalClose();
124 handles_.erase(it); 186 handles_.erase(it);
125 return true; 187 return true;
126 } 188 }
127 } 189 }
128 return false; 190 return false;
129 } 191 }
130 192
131 scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() { 193 const UsbConfigDescriptor& UsbDeviceImpl::GetConfiguration() {
132 DCHECK(thread_checker_.CalledOnValidThread()); 194 DCHECK(thread_checker_.CalledOnValidThread());
133 195
134 PlatformUsbConfigDescriptor platform_config; 196 if (!current_configuration_cached_) {
135 const int rv = 197 libusb_config_descriptor* platform_config;
136 libusb_get_active_config_descriptor(platform_device_, &platform_config); 198 const int rv =
137 if (rv == LIBUSB_SUCCESS) { 199 libusb_get_active_config_descriptor(platform_device_, &platform_config);
138 return new UsbConfigDescriptorImpl(platform_config); 200 if (rv != LIBUSB_SUCCESS) {
139 } else { 201 VLOG(1) << "Failed to get config descriptor: "
140 VLOG(1) << "Failed to get config descriptor: " 202 << ConvertPlatformUsbErrorToString(rv);
141 << ConvertPlatformUsbErrorToString(rv); 203 return current_configuration_;
142 return NULL; 204 }
205
206 current_configuration_.configuration_value =
207 platform_config->bConfigurationValue;
208 current_configuration_.self_powered =
209 (platform_config->bmAttributes & 0x40) != 0;
210 current_configuration_.remote_wakeup =
211 (platform_config->bmAttributes & 0x20) != 0;
212 current_configuration_.maximum_power = platform_config->MaxPower * 2;
213
214 for (size_t i = 0; i < platform_config->bNumInterfaces; ++i) {
215 const struct libusb_interface* platform_interface =
216 &platform_config->interface[i];
217 for (int j = 0; j < platform_interface->num_altsetting; ++j) {
218 const struct libusb_interface_descriptor* platform_alt_setting =
219 &platform_interface->altsetting[j];
220 UsbInterfaceDescriptor interface;
221
222 interface.interface_number = platform_alt_setting->bInterfaceNumber;
223 interface.alternate_setting = platform_alt_setting->bAlternateSetting;
224 interface.interface_class = platform_alt_setting->bInterfaceClass;
225 interface.interface_subclass = platform_alt_setting->bInterfaceSubClass;
226 interface.interface_protocol = platform_alt_setting->bInterfaceProtocol;
227
228 for (size_t k = 0; k < platform_alt_setting->bNumEndpoints; ++k) {
229 const struct libusb_endpoint_descriptor* platform_endpoint =
230 &platform_alt_setting->endpoint[k];
231 UsbEndpointDescriptor endpoint;
232
233 endpoint.address = platform_endpoint->bEndpointAddress;
234 endpoint.direction = GetDirection(platform_endpoint);
235 endpoint.maximum_packet_size = platform_endpoint->wMaxPacketSize;
236 endpoint.synchronization_type =
237 GetSynchronizationType(platform_endpoint);
238 endpoint.transfer_type = GetTransferType(platform_endpoint);
239 endpoint.usage_type = GetUsageType(platform_endpoint);
240 endpoint.polling_interval = platform_endpoint->bInterval;
241 endpoint.extra_data = std::vector<uint8_t>(
242 platform_endpoint->extra,
243 platform_endpoint->extra + platform_endpoint->extra_length);
244
245 interface.endpoints.push_back(endpoint);
246 }
247
248 interface.extra_data = std::vector<uint8_t>(
249 platform_alt_setting->extra,
250 platform_alt_setting->extra + platform_alt_setting->extra_length);
251
252 current_configuration_.interfaces.push_back(interface);
253 }
254 }
255
256 current_configuration_.extra_data = std::vector<uint8_t>(
257 platform_config->extra,
258 platform_config->extra + platform_config->extra_length);
259 current_configuration_cached_ = true;
143 } 260 }
261
262 return current_configuration_;
144 } 263 }
145 264
146 void UsbDeviceImpl::OnDisconnect() { 265 void UsbDeviceImpl::OnDisconnect() {
147 DCHECK(thread_checker_.CalledOnValidThread()); 266 DCHECK(thread_checker_.CalledOnValidThread());
148 HandlesVector handles; 267 HandlesVector handles;
149 swap(handles, handles_); 268 swap(handles, handles_);
150 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) 269 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it)
151 (*it)->InternalClose(); 270 (*it)->InternalClose();
152 } 271 }
153 272
154 } // namespace device 273 } // namespace device
OLDNEW
« no previous file with comments | « device/usb/usb_device_impl.h ('k') | device/usb/usb_interface.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698