| 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_descriptors.h" | 5 #include "device/usb/usb_descriptors.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 const uint8_t kInterfaceDescriptorType = 0x04; | 34 const uint8_t kInterfaceDescriptorType = 0x04; |
| 35 const uint8_t kEndpointDescriptorType = 0x05; | 35 const uint8_t kEndpointDescriptorType = 0x05; |
| 36 const uint8_t kInterfaceAssociationDescriptorType = 11; | 36 const uint8_t kInterfaceAssociationDescriptorType = 11; |
| 37 | 37 |
| 38 const uint8_t kDeviceDescriptorLength = 18; | 38 const uint8_t kDeviceDescriptorLength = 18; |
| 39 const uint8_t kConfigurationDescriptorLength = 9; | 39 const uint8_t kConfigurationDescriptorLength = 9; |
| 40 const uint8_t kInterfaceDescriptorLength = 9; | 40 const uint8_t kInterfaceDescriptorLength = 9; |
| 41 const uint8_t kEndpointDescriptorLength = 7; | 41 const uint8_t kEndpointDescriptorLength = 7; |
| 42 const uint8_t kInterfaceAssociationDescriptorLength = 8; | 42 const uint8_t kInterfaceAssociationDescriptorLength = 8; |
| 43 | 43 |
| 44 const int kControlTransferTimeout = 60000; // 1 minute | 44 const int kControlTransferTimeoutMs = 2000; // 2 seconds |
| 45 | 45 |
| 46 struct UsbInterfaceAssociationDescriptor { | 46 struct UsbInterfaceAssociationDescriptor { |
| 47 UsbInterfaceAssociationDescriptor(uint8_t first_interface, | 47 UsbInterfaceAssociationDescriptor(uint8_t first_interface, |
| 48 uint8_t interface_count) | 48 uint8_t interface_count) |
| 49 : first_interface(first_interface), interface_count(interface_count) {} | 49 : first_interface(first_interface), interface_count(interface_count) {} |
| 50 | 50 |
| 51 bool operator<(const UsbInterfaceAssociationDescriptor& other) const { | 51 bool operator<(const UsbInterfaceAssociationDescriptor& other) const { |
| 52 return first_interface < other.first_interface; | 52 return first_interface < other.first_interface; |
| 53 } | 53 } |
| 54 | 54 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 scoped_refptr<IOBuffer> header, | 116 scoped_refptr<IOBuffer> header, |
| 117 size_t length) { | 117 size_t length) { |
| 118 if (status == UsbTransferStatus::COMPLETED && length == 4) { | 118 if (status == UsbTransferStatus::COMPLETED && length == 4) { |
| 119 const uint8_t* data = reinterpret_cast<const uint8_t*>(header->data()); | 119 const uint8_t* data = reinterpret_cast<const uint8_t*>(header->data()); |
| 120 uint16_t total_length = data[2] | data[3] << 8; | 120 uint16_t total_length = data[2] | data[3] << 8; |
| 121 scoped_refptr<IOBuffer> buffer = new IOBuffer(total_length); | 121 scoped_refptr<IOBuffer> buffer = new IOBuffer(total_length); |
| 122 device_handle->ControlTransfer( | 122 device_handle->ControlTransfer( |
| 123 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, | 123 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, |
| 124 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, | 124 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, |
| 125 kConfigurationDescriptorType << 8 | index, 0, buffer, total_length, | 125 kConfigurationDescriptorType << 8 | index, 0, buffer, total_length, |
| 126 kControlTransferTimeout, | 126 kControlTransferTimeoutMs, |
| 127 base::Bind(&OnReadConfigDescriptor, desc, closure)); | 127 base::Bind(&OnReadConfigDescriptor, desc, closure)); |
| 128 } else { | 128 } else { |
| 129 LOG(ERROR) << "Failed to read length for configuration " | 129 LOG(ERROR) << "Failed to read length for configuration " |
| 130 << static_cast<int>(index) << "."; | 130 << static_cast<int>(index) << "."; |
| 131 closure.Run(); | 131 closure.Run(); |
| 132 } | 132 } |
| 133 } | 133 } |
| 134 | 134 |
| 135 void OnReadDeviceDescriptor( | 135 void OnReadDeviceDescriptor( |
| 136 scoped_refptr<UsbDeviceHandle> device_handle, | 136 scoped_refptr<UsbDeviceHandle> device_handle, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 162 base::Closure closure = base::BarrierClosure( | 162 base::Closure closure = base::BarrierClosure( |
| 163 num_configurations, | 163 num_configurations, |
| 164 base::Bind(OnDoneReadingConfigDescriptors, device_handle, | 164 base::Bind(OnDoneReadingConfigDescriptors, device_handle, |
| 165 base::Passed(&desc), callback)); | 165 base::Passed(&desc), callback)); |
| 166 for (uint8_t i = 0; i < num_configurations; ++i) { | 166 for (uint8_t i = 0; i < num_configurations; ++i) { |
| 167 scoped_refptr<IOBufferWithSize> header = new IOBufferWithSize(4); | 167 scoped_refptr<IOBufferWithSize> header = new IOBufferWithSize(4); |
| 168 device_handle->ControlTransfer( | 168 device_handle->ControlTransfer( |
| 169 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, | 169 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, |
| 170 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, | 170 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, |
| 171 kConfigurationDescriptorType << 8 | i, 0, header, header->size(), | 171 kConfigurationDescriptorType << 8 | i, 0, header, header->size(), |
| 172 kControlTransferTimeout, | 172 kControlTransferTimeoutMs, |
| 173 base::Bind(&OnReadConfigDescriptorHeader, device_handle, desc_ptr, i, | 173 base::Bind(&OnReadConfigDescriptorHeader, device_handle, desc_ptr, i, |
| 174 closure)); | 174 closure)); |
| 175 } | 175 } |
| 176 } | 176 } |
| 177 | 177 |
| 178 void StoreStringDescriptor(IndexMap::iterator it, | 178 void StoreStringDescriptor(IndexMap::iterator it, |
| 179 const base::Closure& callback, | 179 const base::Closure& callback, |
| 180 const base::string16& string) { | 180 const base::string16& string) { |
| 181 it->second = string; | 181 it->second = string; |
| 182 callback.Run(); | 182 callback.Run(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 201 void ReadStringDescriptor( | 201 void ReadStringDescriptor( |
| 202 scoped_refptr<UsbDeviceHandle> device_handle, | 202 scoped_refptr<UsbDeviceHandle> device_handle, |
| 203 uint8_t index, | 203 uint8_t index, |
| 204 uint16_t language_id, | 204 uint16_t language_id, |
| 205 const base::Callback<void(const base::string16&)>& callback) { | 205 const base::Callback<void(const base::string16&)>& callback) { |
| 206 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); | 206 scoped_refptr<IOBufferWithSize> buffer = new IOBufferWithSize(255); |
| 207 device_handle->ControlTransfer( | 207 device_handle->ControlTransfer( |
| 208 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, | 208 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, |
| 209 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, | 209 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, |
| 210 kStringDescriptorType << 8 | index, language_id, buffer, buffer->size(), | 210 kStringDescriptorType << 8 | index, language_id, buffer, buffer->size(), |
| 211 kControlTransferTimeout, base::Bind(&OnReadStringDescriptor, callback)); | 211 kControlTransferTimeoutMs, base::Bind(&OnReadStringDescriptor, callback)); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void OnReadLanguageIds(scoped_refptr<UsbDeviceHandle> device_handle, | 214 void OnReadLanguageIds(scoped_refptr<UsbDeviceHandle> device_handle, |
| 215 IndexMapPtr index_map, | 215 IndexMapPtr index_map, |
| 216 const base::Callback<void(IndexMapPtr)>& callback, | 216 const base::Callback<void(IndexMapPtr)>& callback, |
| 217 const base::string16& languages) { | 217 const base::string16& languages) { |
| 218 // Default to English unless the device provides a language and then just pick | 218 // Default to English unless the device provides a language and then just pick |
| 219 // the first one. | 219 // the first one. |
| 220 uint16_t language_id = languages.empty() ? 0x0409 : languages[0]; | 220 uint16_t language_id = languages.empty() ? 0x0409 : languages[0]; |
| 221 | 221 |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 | 496 |
| 497 void ReadUsbDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, | 497 void ReadUsbDescriptors(scoped_refptr<UsbDeviceHandle> device_handle, |
| 498 const base::Callback<void( | 498 const base::Callback<void( |
| 499 std::unique_ptr<UsbDeviceDescriptor>)>& callback) { | 499 std::unique_ptr<UsbDeviceDescriptor>)>& callback) { |
| 500 scoped_refptr<IOBufferWithSize> buffer = | 500 scoped_refptr<IOBufferWithSize> buffer = |
| 501 new IOBufferWithSize(kDeviceDescriptorLength); | 501 new IOBufferWithSize(kDeviceDescriptorLength); |
| 502 device_handle->ControlTransfer( | 502 device_handle->ControlTransfer( |
| 503 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, | 503 UsbTransferDirection::INBOUND, UsbControlTransferType::STANDARD, |
| 504 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, | 504 UsbControlTransferRecipient::DEVICE, kGetDescriptorRequest, |
| 505 kDeviceDescriptorType << 8, 0, buffer, buffer->size(), | 505 kDeviceDescriptorType << 8, 0, buffer, buffer->size(), |
| 506 kControlTransferTimeout, | 506 kControlTransferTimeoutMs, |
| 507 base::Bind(&OnReadDeviceDescriptor, device_handle, callback)); | 507 base::Bind(&OnReadDeviceDescriptor, device_handle, callback)); |
| 508 } | 508 } |
| 509 | 509 |
| 510 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, | 510 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, |
| 511 base::string16* output) { | 511 base::string16* output) { |
| 512 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) | 512 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) |
| 513 return false; | 513 return false; |
| 514 | 514 |
| 515 // Let the device return a buffer larger than the actual string but prefer the | 515 // Let the device return a buffer larger than the actual string but prefer the |
| 516 // length reported inside the descriptor. | 516 // length reported inside the descriptor. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 536 callback.Run(std::move(index_map)); | 536 callback.Run(std::move(index_map)); |
| 537 return; | 537 return; |
| 538 } | 538 } |
| 539 | 539 |
| 540 ReadStringDescriptor(device_handle, 0, 0, | 540 ReadStringDescriptor(device_handle, 0, 0, |
| 541 base::Bind(&OnReadLanguageIds, device_handle, | 541 base::Bind(&OnReadLanguageIds, device_handle, |
| 542 base::Passed(&index_map), callback)); | 542 base::Passed(&index_map), callback)); |
| 543 } | 543 } |
| 544 | 544 |
| 545 } // namespace device | 545 } // namespace device |
| OLD | NEW |