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" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK: | 82 case LIBUSB_ISO_USAGE_TYPE_FEEDBACK: |
83 return USB_USAGE_FEEDBACK; | 83 return USB_USAGE_FEEDBACK; |
84 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT: | 84 case LIBUSB_ISO_USAGE_TYPE_IMPLICIT: |
85 return USB_USAGE_EXPLICIT_FEEDBACK; | 85 return USB_USAGE_EXPLICIT_FEEDBACK; |
86 default: | 86 default: |
87 NOTREACHED(); | 87 NOTREACHED(); |
88 return USB_USAGE_DATA; | 88 return USB_USAGE_DATA; |
89 } | 89 } |
90 } | 90 } |
91 | 91 |
| 92 void ConvertConfigDescriptor(const libusb_config_descriptor* platform_config, |
| 93 UsbConfigDescriptor* configuration) { |
| 94 configuration->configuration_value = platform_config->bConfigurationValue; |
| 95 configuration->self_powered = (platform_config->bmAttributes & 0x40) != 0; |
| 96 configuration->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0; |
| 97 configuration->maximum_power = platform_config->MaxPower * 2; |
| 98 |
| 99 for (size_t i = 0; i < platform_config->bNumInterfaces; ++i) { |
| 100 const struct libusb_interface* platform_interface = |
| 101 &platform_config->interface[i]; |
| 102 for (int j = 0; j < platform_interface->num_altsetting; ++j) { |
| 103 const struct libusb_interface_descriptor* platform_alt_setting = |
| 104 &platform_interface->altsetting[j]; |
| 105 UsbInterfaceDescriptor interface; |
| 106 |
| 107 interface.interface_number = platform_alt_setting->bInterfaceNumber; |
| 108 interface.alternate_setting = platform_alt_setting->bAlternateSetting; |
| 109 interface.interface_class = platform_alt_setting->bInterfaceClass; |
| 110 interface.interface_subclass = platform_alt_setting->bInterfaceSubClass; |
| 111 interface.interface_protocol = platform_alt_setting->bInterfaceProtocol; |
| 112 |
| 113 interface.endpoints.reserve(platform_alt_setting->bNumEndpoints); |
| 114 for (size_t k = 0; k < platform_alt_setting->bNumEndpoints; ++k) { |
| 115 const struct libusb_endpoint_descriptor* platform_endpoint = |
| 116 &platform_alt_setting->endpoint[k]; |
| 117 UsbEndpointDescriptor endpoint; |
| 118 |
| 119 endpoint.address = platform_endpoint->bEndpointAddress; |
| 120 endpoint.direction = GetDirection(platform_endpoint); |
| 121 endpoint.maximum_packet_size = platform_endpoint->wMaxPacketSize; |
| 122 endpoint.synchronization_type = |
| 123 GetSynchronizationType(platform_endpoint); |
| 124 endpoint.transfer_type = GetTransferType(platform_endpoint); |
| 125 endpoint.usage_type = GetUsageType(platform_endpoint); |
| 126 endpoint.polling_interval = platform_endpoint->bInterval; |
| 127 endpoint.extra_data = std::vector<uint8_t>( |
| 128 platform_endpoint->extra, |
| 129 platform_endpoint->extra + platform_endpoint->extra_length); |
| 130 |
| 131 interface.endpoints.push_back(endpoint); |
| 132 } |
| 133 |
| 134 interface.extra_data = std::vector<uint8_t>( |
| 135 platform_alt_setting->extra, |
| 136 platform_alt_setting->extra + platform_alt_setting->extra_length); |
| 137 |
| 138 configuration->interfaces.push_back(interface); |
| 139 } |
| 140 } |
| 141 |
| 142 configuration->extra_data = std::vector<uint8_t>( |
| 143 platform_config->extra, |
| 144 platform_config->extra + platform_config->extra_length); |
| 145 } |
| 146 |
92 } // namespace | 147 } // namespace |
93 | 148 |
94 UsbDeviceImpl::UsbDeviceImpl( | 149 UsbDeviceImpl::UsbDeviceImpl( |
95 scoped_refptr<UsbContext> context, | 150 scoped_refptr<UsbContext> context, |
96 PlatformUsbDevice platform_device, | 151 PlatformUsbDevice platform_device, |
97 uint16 vendor_id, | 152 uint16 vendor_id, |
98 uint16 product_id, | 153 uint16 product_id, |
99 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) | 154 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) |
100 : UsbDevice(vendor_id, | 155 : UsbDevice(vendor_id, |
101 product_id, | 156 product_id, |
102 base::string16(), | 157 base::string16(), |
103 base::string16(), | 158 base::string16(), |
104 base::string16()), | 159 base::string16()), |
105 platform_device_(platform_device), | 160 platform_device_(platform_device), |
106 context_(context), | 161 context_(context), |
107 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 162 task_runner_(base::ThreadTaskRunnerHandle::Get()), |
108 blocking_task_runner_(blocking_task_runner) { | 163 blocking_task_runner_(blocking_task_runner) { |
109 CHECK(platform_device) << "platform_device cannot be NULL"; | 164 CHECK(platform_device) << "platform_device cannot be NULL"; |
110 libusb_ref_device(platform_device); | 165 libusb_ref_device(platform_device); |
111 RefreshConfiguration(); | 166 ReadAllConfigurations(); |
| 167 RefreshActiveConfiguration(); |
112 } | 168 } |
113 | 169 |
114 UsbDeviceImpl::~UsbDeviceImpl() { | 170 UsbDeviceImpl::~UsbDeviceImpl() { |
115 // The destructor must be safe to call from any thread. | 171 // The destructor must be safe to call from any thread. |
116 libusb_unref_device(platform_device_); | 172 libusb_unref_device(platform_device_); |
117 } | 173 } |
118 | 174 |
119 #if defined(OS_CHROMEOS) | 175 #if defined(OS_CHROMEOS) |
120 | 176 |
121 void UsbDeviceImpl::CheckUsbAccess(const ResultCallback& callback) { | 177 void UsbDeviceImpl::CheckUsbAccess(const ResultCallback& callback) { |
(...skipping 30 matching lines...) Expand all Loading... |
152 ++it) { | 208 ++it) { |
153 if (it->get() == handle.get()) { | 209 if (it->get() == handle.get()) { |
154 (*it)->InternalClose(); | 210 (*it)->InternalClose(); |
155 handles_.erase(it); | 211 handles_.erase(it); |
156 return true; | 212 return true; |
157 } | 213 } |
158 } | 214 } |
159 return false; | 215 return false; |
160 } | 216 } |
161 | 217 |
162 const UsbConfigDescriptor* UsbDeviceImpl::GetConfiguration() { | 218 const UsbConfigDescriptor* UsbDeviceImpl::GetActiveConfiguration() { |
163 DCHECK(thread_checker_.CalledOnValidThread()); | 219 DCHECK(thread_checker_.CalledOnValidThread()); |
164 return configuration_.get(); | 220 return active_configuration_; |
165 } | 221 } |
166 | 222 |
167 void UsbDeviceImpl::OnDisconnect() { | 223 void UsbDeviceImpl::OnDisconnect() { |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 224 DCHECK(thread_checker_.CalledOnValidThread()); |
169 | 225 |
170 // Swap the list of handles into a local variable because closing all open | 226 // Swap the list of handles into a local variable because closing all open |
171 // handles may release the last reference to this object. | 227 // handles may release the last reference to this object. |
172 HandlesVector handles; | 228 HandlesVector handles; |
173 swap(handles, handles_); | 229 swap(handles, handles_); |
174 | 230 |
175 for (const scoped_refptr<UsbDeviceHandleImpl>& handle : handles_) { | 231 for (const scoped_refptr<UsbDeviceHandleImpl>& handle : handles_) { |
176 handle->InternalClose(); | 232 handle->InternalClose(); |
177 } | 233 } |
178 } | 234 } |
179 | 235 |
180 void UsbDeviceImpl::RefreshConfiguration() { | 236 void UsbDeviceImpl::ReadAllConfigurations() { |
| 237 libusb_device_descriptor device_descriptor; |
| 238 int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor); |
| 239 if (rv == LIBUSB_SUCCESS) { |
| 240 uint8_t num_configurations = device_descriptor.bNumConfigurations; |
| 241 configurations_.reserve(num_configurations); |
| 242 for (uint8_t i = 0; i < num_configurations; ++i) { |
| 243 libusb_config_descriptor* platform_config; |
| 244 rv = libusb_get_config_descriptor(platform_device_, i, &platform_config); |
| 245 if (rv != LIBUSB_SUCCESS) { |
| 246 USB_LOG(EVENT) << "Failed to get config descriptor: " |
| 247 << ConvertPlatformUsbErrorToString(rv); |
| 248 continue; |
| 249 } |
| 250 |
| 251 UsbConfigDescriptor config_descriptor; |
| 252 ConvertConfigDescriptor(platform_config, &config_descriptor); |
| 253 configurations_.push_back(config_descriptor); |
| 254 libusb_free_config_descriptor(platform_config); |
| 255 } |
| 256 } else { |
| 257 USB_LOG(EVENT) << "Failed to get device descriptor: " |
| 258 << ConvertPlatformUsbErrorToString(rv); |
| 259 } |
| 260 } |
| 261 |
| 262 void UsbDeviceImpl::RefreshActiveConfiguration() { |
181 libusb_config_descriptor* platform_config; | 263 libusb_config_descriptor* platform_config; |
182 int rv = | 264 int rv = |
183 libusb_get_active_config_descriptor(platform_device_, &platform_config); | 265 libusb_get_active_config_descriptor(platform_device_, &platform_config); |
184 if (rv != LIBUSB_SUCCESS) { | 266 if (rv != LIBUSB_SUCCESS) { |
185 USB_LOG(EVENT) << "Failed to get config descriptor: " | 267 USB_LOG(EVENT) << "Failed to get config descriptor: " |
186 << ConvertPlatformUsbErrorToString(rv); | 268 << ConvertPlatformUsbErrorToString(rv); |
187 return; | 269 return; |
188 } | 270 } |
189 | 271 |
190 configuration_.reset(new UsbConfigDescriptor()); | 272 active_configuration_ = nullptr; |
191 configuration_->configuration_value = platform_config->bConfigurationValue; | 273 for (const auto& config : configurations_) { |
192 configuration_->self_powered = (platform_config->bmAttributes & 0x40) != 0; | 274 if (config.configuration_value == platform_config->bConfigurationValue) { |
193 configuration_->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0; | 275 active_configuration_ = &config; |
194 configuration_->maximum_power = platform_config->MaxPower * 2; | 276 break; |
195 | |
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; | |
203 | |
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; | |
209 | |
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; | |
214 | |
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); | |
226 | |
227 interface.endpoints.push_back(endpoint); | |
228 } | |
229 | |
230 interface.extra_data = std::vector<uint8_t>( | |
231 platform_alt_setting->extra, | |
232 platform_alt_setting->extra + platform_alt_setting->extra_length); | |
233 | |
234 configuration_->interfaces.push_back(interface); | |
235 } | 277 } |
236 } | 278 } |
237 | 279 |
238 configuration_->extra_data = std::vector<uint8_t>( | |
239 platform_config->extra, | |
240 platform_config->extra + platform_config->extra_length); | |
241 | |
242 libusb_free_config_descriptor(platform_config); | 280 libusb_free_config_descriptor(platform_config); |
243 } | 281 } |
244 | 282 |
245 #if defined(OS_CHROMEOS) | 283 #if defined(OS_CHROMEOS) |
246 | 284 |
247 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback, | 285 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback, |
248 dbus::FileDescriptor fd) { | 286 dbus::FileDescriptor fd) { |
249 blocking_task_runner_->PostTask( | 287 blocking_task_runner_->PostTask( |
250 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this, | 288 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this, |
251 base::Passed(&fd), callback)); | 289 base::Passed(&fd), callback)); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, | 328 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, |
291 const OpenCallback& callback) { | 329 const OpenCallback& callback) { |
292 DCHECK(thread_checker_.CalledOnValidThread()); | 330 DCHECK(thread_checker_.CalledOnValidThread()); |
293 scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl( | 331 scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl( |
294 context_, this, platform_handle, blocking_task_runner_); | 332 context_, this, platform_handle, blocking_task_runner_); |
295 handles_.push_back(device_handle); | 333 handles_.push_back(device_handle); |
296 callback.Run(device_handle); | 334 callback.Run(device_handle); |
297 } | 335 } |
298 | 336 |
299 } // namespace device | 337 } // namespace device |
OLD | NEW |