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