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 SetConfigDescriptor(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); |
| 165 GetAllConfigurations(); | |
| 111 RefreshConfiguration(); | 166 RefreshConfiguration(); |
| 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 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after 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::GetCurrentConfiguration() { |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | 218 DCHECK(thread_checker_.CalledOnValidThread()); |
| 164 return configuration_.get(); | 219 return configuration_.get(); |
| 165 } | 220 } |
| 166 | 221 |
| 222 const std::vector<UsbConfigDescriptor>& UsbDeviceImpl::configurations() const { | |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 224 return configurations_; | |
| 225 } | |
| 226 | |
| 167 void UsbDeviceImpl::OnDisconnect() { | 227 void UsbDeviceImpl::OnDisconnect() { |
| 168 DCHECK(thread_checker_.CalledOnValidThread()); | 228 DCHECK(thread_checker_.CalledOnValidThread()); |
| 169 | 229 |
| 170 // Swap the list of handles into a local variable because closing all open | 230 // Swap the list of handles into a local variable because closing all open |
| 171 // handles may release the last reference to this object. | 231 // handles may release the last reference to this object. |
| 172 HandlesVector handles; | 232 HandlesVector handles; |
| 173 swap(handles, handles_); | 233 swap(handles, handles_); |
| 174 | 234 |
| 175 for (const scoped_refptr<UsbDeviceHandleImpl>& handle : handles_) { | 235 for (const scoped_refptr<UsbDeviceHandleImpl>& handle : handles_) { |
| 176 handle->InternalClose(); | 236 handle->InternalClose(); |
| 177 } | 237 } |
| 178 } | 238 } |
| 179 | 239 |
| 180 void UsbDeviceImpl::RefreshConfiguration() { | 240 void UsbDeviceImpl::RefreshConfiguration() { |
| 181 libusb_config_descriptor* platform_config; | 241 libusb_config_descriptor* platform_config; |
| 182 int rv = | 242 int rv = |
| 183 libusb_get_active_config_descriptor(platform_device_, &platform_config); | 243 libusb_get_active_config_descriptor(platform_device_, &platform_config); |
| 184 if (rv != LIBUSB_SUCCESS) { | 244 if (rv != LIBUSB_SUCCESS) { |
| 185 USB_LOG(EVENT) << "Failed to get config descriptor: " | 245 USB_LOG(EVENT) << "Failed to get config descriptor: " |
| 186 << ConvertPlatformUsbErrorToString(rv); | 246 << ConvertPlatformUsbErrorToString(rv); |
| 187 return; | 247 return; |
| 188 } | 248 } |
| 189 | 249 |
| 190 configuration_.reset(new UsbConfigDescriptor()); | 250 configuration_.reset(new UsbConfigDescriptor()); |
| 191 configuration_->configuration_value = platform_config->bConfigurationValue; | 251 SetConfigDescriptor(platform_config, configuration_.get()); |
|
Reilly Grant (use Gerrit)
2015/07/31 23:36:12
Instead of reparsing platform_config just find the
juncai
2015/08/03 17:46:01
Done.
| |
| 192 configuration_->self_powered = (platform_config->bmAttributes & 0x40) != 0; | |
| 193 configuration_->remote_wakeup = (platform_config->bmAttributes & 0x20) != 0; | |
| 194 configuration_->maximum_power = platform_config->MaxPower * 2; | |
| 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 } | |
| 236 } | |
| 237 | |
| 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); | 252 libusb_free_config_descriptor(platform_config); |
| 243 } | 253 } |
| 244 | 254 |
| 255 void UsbDeviceImpl::GetAllConfigurations() { | |
| 256 libusb_device_descriptor device_descriptor; | |
| 257 int rv = libusb_get_device_descriptor(platform_device_, &device_descriptor); | |
| 258 if (rv == LIBUSB_SUCCESS) { | |
| 259 uint8_t num_configurations = device_descriptor.bNumConfigurations; | |
| 260 for (uint8_t i = 0; i < num_configurations; i++) { | |
| 261 libusb_config_descriptor* platform_config; | |
| 262 rv = libusb_get_config_descriptor(platform_device_, i, &platform_config); | |
| 263 if (rv != LIBUSB_SUCCESS) { | |
| 264 USB_LOG(EVENT) << "Failed to get config descriptor: " | |
| 265 << ConvertPlatformUsbErrorToString(rv); | |
| 266 return; | |
| 267 } | |
| 268 | |
| 269 UsbConfigDescriptor config_descriptor; | |
| 270 SetConfigDescriptor(platform_config, &config_descriptor); | |
| 271 configurations_.push_back(config_descriptor); | |
| 272 libusb_free_config_descriptor(platform_config); | |
| 273 } | |
| 274 } else { | |
| 275 USB_LOG(EVENT) << "Failed to get device descriptor: " | |
| 276 << ConvertPlatformUsbErrorToString(rv); | |
| 277 } | |
| 278 } | |
| 279 | |
| 245 #if defined(OS_CHROMEOS) | 280 #if defined(OS_CHROMEOS) |
| 246 | 281 |
| 247 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback, | 282 void UsbDeviceImpl::OnOpenRequestComplete(const OpenCallback& callback, |
| 248 dbus::FileDescriptor fd) { | 283 dbus::FileDescriptor fd) { |
| 249 blocking_task_runner_->PostTask( | 284 blocking_task_runner_->PostTask( |
| 250 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this, | 285 FROM_HERE, base::Bind(&UsbDeviceImpl::OpenOnBlockingThreadWithFd, this, |
| 251 base::Passed(&fd), callback)); | 286 base::Passed(&fd), callback)); |
| 252 } | 287 } |
| 253 | 288 |
| 254 void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd, | 289 void UsbDeviceImpl::OpenOnBlockingThreadWithFd(dbus::FileDescriptor fd, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, | 325 void UsbDeviceImpl::Opened(PlatformUsbDeviceHandle platform_handle, |
| 291 const OpenCallback& callback) { | 326 const OpenCallback& callback) { |
| 292 DCHECK(thread_checker_.CalledOnValidThread()); | 327 DCHECK(thread_checker_.CalledOnValidThread()); |
| 293 scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl( | 328 scoped_refptr<UsbDeviceHandleImpl> device_handle = new UsbDeviceHandleImpl( |
| 294 context_, this, platform_handle, blocking_task_runner_); | 329 context_, this, platform_handle, blocking_task_runner_); |
| 295 handles_.push_back(device_handle); | 330 handles_.push_back(device_handle); |
| 296 callback.Run(device_handle); | 331 callback.Run(device_handle); |
| 297 } | 332 } |
| 298 | 333 |
| 299 } // namespace device | 334 } // namespace device |
| OLD | NEW |