OLD | NEW |
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" | |
16 #include "device/usb/usb_device_handle_impl.h" | 15 #include "device/usb/usb_device_handle_impl.h" |
17 #include "device/usb/usb_error.h" | 16 #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 | |
28 namespace { | 26 namespace { |
29 | 27 |
30 #if defined(OS_CHROMEOS) | 28 #if defined(OS_CHROMEOS) |
31 void OnRequestUsbAccessReplied( | 29 void OnRequestUsbAccessReplied( |
32 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | 30 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
33 const base::Callback<void(bool success)>& callback, | 31 const base::Callback<void(bool success)>& callback, |
34 bool success) { | 32 bool success) { |
35 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); | 33 task_runner->PostTask(FROM_HERE, base::Bind(callback, success)); |
36 } | 34 } |
37 #endif // defined(OS_CHROMEOS) | 35 #endif // defined(OS_CHROMEOS) |
38 | 36 |
39 UsbEndpointDirection GetDirection( | 37 } // namespace |
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 | 38 |
52 UsbSynchronizationType GetSynchronizationType( | 39 namespace device { |
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 | |
99 } // namespace | |
100 | 40 |
101 UsbDeviceImpl::UsbDeviceImpl( | 41 UsbDeviceImpl::UsbDeviceImpl( |
102 scoped_refptr<UsbContext> context, | 42 scoped_refptr<UsbContext> context, |
103 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, | 43 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
104 PlatformUsbDevice platform_device, | 44 PlatformUsbDevice platform_device, |
105 uint16 vendor_id, | 45 uint16 vendor_id, |
106 uint16 product_id, | 46 uint16 product_id, |
107 uint32 unique_id) | 47 uint32 unique_id) |
108 : UsbDevice(vendor_id, product_id, unique_id), | 48 : UsbDevice(vendor_id, product_id, unique_id), |
109 platform_device_(platform_device), | 49 platform_device_(platform_device), |
110 current_configuration_cached_(false), | |
111 context_(context), | 50 context_(context), |
112 ui_task_runner_(ui_task_runner) { | 51 ui_task_runner_(ui_task_runner) { |
113 CHECK(platform_device) << "platform_device cannot be NULL"; | 52 CHECK(platform_device) << "platform_device cannot be NULL"; |
114 libusb_ref_device(platform_device); | 53 libusb_ref_device(platform_device); |
115 } | 54 } |
116 | 55 |
117 UsbDeviceImpl::~UsbDeviceImpl() { | 56 UsbDeviceImpl::~UsbDeviceImpl() { |
118 DCHECK(thread_checker_.CalledOnValidThread()); | 57 DCHECK(thread_checker_.CalledOnValidThread()); |
119 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); | 58 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); |
120 ++it) { | 59 ++it) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 } | 94 } |
156 } | 95 } |
157 | 96 |
158 #endif | 97 #endif |
159 | 98 |
160 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { | 99 scoped_refptr<UsbDeviceHandle> UsbDeviceImpl::Open() { |
161 DCHECK(thread_checker_.CalledOnValidThread()); | 100 DCHECK(thread_checker_.CalledOnValidThread()); |
162 PlatformUsbDeviceHandle handle; | 101 PlatformUsbDeviceHandle handle; |
163 const int rv = libusb_open(platform_device_, &handle); | 102 const int rv = libusb_open(platform_device_, &handle); |
164 if (LIBUSB_SUCCESS == rv) { | 103 if (LIBUSB_SUCCESS == rv) { |
165 GetConfiguration(); | 104 scoped_refptr<UsbConfigDescriptor> interfaces = ListInterfaces(); |
166 if (!current_configuration_cached_) { | 105 if (!interfaces.get()) |
167 return NULL; | 106 return NULL; |
168 } | |
169 scoped_refptr<UsbDeviceHandleImpl> device_handle = | 107 scoped_refptr<UsbDeviceHandleImpl> device_handle = |
170 new UsbDeviceHandleImpl(context_, this, handle, current_configuration_); | 108 new UsbDeviceHandleImpl(context_, this, handle, interfaces); |
171 handles_.push_back(device_handle); | 109 handles_.push_back(device_handle); |
172 return device_handle; | 110 return device_handle; |
173 } else { | 111 } else { |
174 VLOG(1) << "Failed to open device: " << ConvertPlatformUsbErrorToString(rv); | 112 VLOG(1) << "Failed to open device: " << ConvertPlatformUsbErrorToString(rv); |
175 return NULL; | 113 return NULL; |
176 } | 114 } |
177 } | 115 } |
178 | 116 |
179 bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { | 117 bool UsbDeviceImpl::Close(scoped_refptr<UsbDeviceHandle> handle) { |
180 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
181 | 119 |
182 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); | 120 for (HandlesVector::iterator it = handles_.begin(); it != handles_.end(); |
183 ++it) { | 121 ++it) { |
184 if (it->get() == handle.get()) { | 122 if (it->get() == handle.get()) { |
185 (*it)->InternalClose(); | 123 (*it)->InternalClose(); |
186 handles_.erase(it); | 124 handles_.erase(it); |
187 return true; | 125 return true; |
188 } | 126 } |
189 } | 127 } |
190 return false; | 128 return false; |
191 } | 129 } |
192 | 130 |
193 const UsbConfigDescriptor& UsbDeviceImpl::GetConfiguration() { | 131 scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() { |
194 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK(thread_checker_.CalledOnValidThread()); |
195 | 133 |
196 if (!current_configuration_cached_) { | 134 PlatformUsbConfigDescriptor platform_config; |
197 libusb_config_descriptor* platform_config; | 135 const int rv = |
198 const int rv = | 136 libusb_get_active_config_descriptor(platform_device_, &platform_config); |
199 libusb_get_active_config_descriptor(platform_device_, &platform_config); | 137 if (rv == LIBUSB_SUCCESS) { |
200 if (rv != LIBUSB_SUCCESS) { | 138 return new UsbConfigDescriptorImpl(platform_config); |
201 VLOG(1) << "Failed to get config descriptor: " | 139 } else { |
202 << ConvertPlatformUsbErrorToString(rv); | 140 VLOG(1) << "Failed to get config descriptor: " |
203 return current_configuration_; | 141 << ConvertPlatformUsbErrorToString(rv); |
204 } | 142 return NULL; |
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; | |
260 } | 143 } |
261 | |
262 return current_configuration_; | |
263 } | 144 } |
264 | 145 |
265 void UsbDeviceImpl::OnDisconnect() { | 146 void UsbDeviceImpl::OnDisconnect() { |
266 DCHECK(thread_checker_.CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
267 HandlesVector handles; | 148 HandlesVector handles; |
268 swap(handles, handles_); | 149 swap(handles, handles_); |
269 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) | 150 for (HandlesVector::iterator it = handles.begin(); it != handles.end(); ++it) |
270 (*it)->InternalClose(); | 151 (*it)->InternalClose(); |
271 } | 152 } |
272 | 153 |
273 } // namespace device | 154 } // namespace device |
OLD | NEW |