| 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 <vector> | |
| 11 | 10 |
| 12 #include "base/barrier_closure.h" | 11 #include "base/barrier_closure.h" |
| 13 #include "base/bind.h" | 12 #include "base/bind.h" |
| 14 #include "device/usb/usb_device_handle.h" | 13 #include "device/usb/usb_device_handle.h" |
| 15 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 16 | 15 |
| 17 namespace device { | 16 namespace device { |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 using IndexMap = std::map<uint8_t, base::string16>; | 20 using IndexMap = std::map<uint8_t, base::string16>; |
| 22 using IndexMapPtr = scoped_ptr<IndexMap>; | 21 using IndexMapPtr = scoped_ptr<IndexMap>; |
| 23 | 22 |
| 24 // Standard USB requests and descriptor types: | 23 // Standard USB requests and descriptor types: |
| 25 const uint8_t kGetDescriptorRequest = 0x06; | 24 const uint8_t kGetDescriptorRequest = 0x06; |
| 26 const uint8_t kStringDescriptorType = 0x03; | 25 const uint8_t kStringDescriptorType = 0x03; |
| 27 | 26 |
| 28 const int kControlTransferTimeout = 60000; // 1 minute | 27 const int kControlTransferTimeout = 60000; // 1 minute |
| 29 | 28 |
| 30 struct UsbInterfaceAssociationDescriptor { | |
| 31 UsbInterfaceAssociationDescriptor(uint8_t first_interface, | |
| 32 uint8_t interface_count) | |
| 33 : first_interface(first_interface), interface_count(interface_count) {} | |
| 34 | |
| 35 bool operator<(const UsbInterfaceAssociationDescriptor& other) const { | |
| 36 return first_interface < other.first_interface; | |
| 37 } | |
| 38 | |
| 39 uint8_t first_interface; | |
| 40 uint8_t interface_count; | |
| 41 }; | |
| 42 | |
| 43 void ParseInterfaceAssociationDescriptors( | |
| 44 const std::vector<uint8_t>& buffer, | |
| 45 std::vector<UsbInterfaceAssociationDescriptor>* functions) { | |
| 46 const uint8_t kInterfaceAssociationDescriptorType = 11; | |
| 47 const uint8_t kInterfaceAssociationDescriptorLength = 8; | |
| 48 std::vector<uint8_t>::const_iterator it = buffer.begin(); | |
| 49 | |
| 50 while (it != buffer.end()) { | |
| 51 // All descriptors must be at least 2 byte which means the length and type | |
| 52 // are safe to read. | |
| 53 if (std::distance(it, buffer.end()) < 2) | |
| 54 return; | |
| 55 uint8_t length = it[0]; | |
| 56 if (length > std::distance(it, buffer.end())) | |
| 57 return; | |
| 58 if (it[1] == kInterfaceAssociationDescriptorType && | |
| 59 length == kInterfaceAssociationDescriptorLength) { | |
| 60 functions->push_back(UsbInterfaceAssociationDescriptor(it[2], it[3])); | |
| 61 } | |
| 62 std::advance(it, length); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void StoreStringDescriptor(IndexMap::iterator it, | 29 void StoreStringDescriptor(IndexMap::iterator it, |
| 67 const base::Closure& callback, | 30 const base::Closure& callback, |
| 68 const base::string16& string) { | 31 const base::string16& string) { |
| 69 it->second = string; | 32 it->second = string; |
| 70 callback.Run(); | 33 callback.Run(); |
| 71 } | 34 } |
| 72 | 35 |
| 73 void OnReadStringDescriptor( | 36 void OnReadStringDescriptor( |
| 74 const base::Callback<void(const base::string16&)>& callback, | 37 const base::Callback<void(const base::string16&)>& callback, |
| 75 UsbTransferStatus status, | 38 UsbTransferStatus status, |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 106 |
| 144 UsbInterfaceDescriptor::UsbInterfaceDescriptor(uint8_t interface_number, | 107 UsbInterfaceDescriptor::UsbInterfaceDescriptor(uint8_t interface_number, |
| 145 uint8_t alternate_setting, | 108 uint8_t alternate_setting, |
| 146 uint8_t interface_class, | 109 uint8_t interface_class, |
| 147 uint8_t interface_subclass, | 110 uint8_t interface_subclass, |
| 148 uint8_t interface_protocol) | 111 uint8_t interface_protocol) |
| 149 : interface_number(interface_number), | 112 : interface_number(interface_number), |
| 150 alternate_setting(alternate_setting), | 113 alternate_setting(alternate_setting), |
| 151 interface_class(interface_class), | 114 interface_class(interface_class), |
| 152 interface_subclass(interface_subclass), | 115 interface_subclass(interface_subclass), |
| 153 interface_protocol(interface_protocol), | 116 interface_protocol(interface_protocol) {} |
| 154 first_interface(interface_number) {} | |
| 155 | 117 |
| 156 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() = default; | 118 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() = default; |
| 157 | 119 |
| 158 UsbConfigDescriptor::UsbConfigDescriptor(uint8_t configuration_value, | 120 UsbConfigDescriptor::UsbConfigDescriptor(uint8_t configuration_value, |
| 159 bool self_powered, | 121 bool self_powered, |
| 160 bool remote_wakeup, | 122 bool remote_wakeup, |
| 161 uint16_t maximum_power) | 123 uint16_t maximum_power) |
| 162 : configuration_value(configuration_value), | 124 : configuration_value(configuration_value), |
| 163 self_powered(self_powered), | 125 self_powered(self_powered), |
| 164 remote_wakeup(remote_wakeup), | 126 remote_wakeup(remote_wakeup), |
| 165 maximum_power(maximum_power) {} | 127 maximum_power(maximum_power) {} |
| 166 | 128 |
| 167 UsbConfigDescriptor::~UsbConfigDescriptor() = default; | 129 UsbConfigDescriptor::~UsbConfigDescriptor() = default; |
| 168 | 130 |
| 169 void UsbConfigDescriptor::AssignFirstInterfaceNumbers() { | |
| 170 std::vector<UsbInterfaceAssociationDescriptor> functions; | |
| 171 ParseInterfaceAssociationDescriptors(extra_data, &functions); | |
| 172 for (const auto& interface : interfaces) { | |
| 173 ParseInterfaceAssociationDescriptors(interface.extra_data, &functions); | |
| 174 for (const auto& endpoint : interface.endpoints) | |
| 175 ParseInterfaceAssociationDescriptors(endpoint.extra_data, &functions); | |
| 176 } | |
| 177 | |
| 178 // libusb has collected interface association descriptors in the |extra_data| | |
| 179 // fields of other descriptor types. This may have disturbed their order | |
| 180 // but sorting by the bFirstInterface should fix it. | |
| 181 std::sort(functions.begin(), functions.end()); | |
| 182 | |
| 183 uint8_t remaining_interfaces = 0; | |
| 184 auto function_it = functions.cbegin(); | |
| 185 for (auto interface_it = interfaces.begin(); interface_it != interfaces.end(); | |
| 186 ++interface_it) { | |
| 187 if (remaining_interfaces > 0) { | |
| 188 // Continuation of a previous function. Tag all alternate interfaces | |
| 189 // (which are guaranteed to be contiguous). | |
| 190 for (uint8_t interface_number = interface_it->interface_number; | |
| 191 interface_it != interfaces.end() && | |
| 192 interface_it->interface_number == interface_number; | |
| 193 ++interface_it) { | |
| 194 interface_it->first_interface = function_it->first_interface; | |
| 195 } | |
| 196 if (--remaining_interfaces == 0) | |
| 197 ++function_it; | |
| 198 } else if (function_it != functions.end() && | |
| 199 interface_it->interface_number == function_it->first_interface) { | |
| 200 // Start of a new function. | |
| 201 interface_it->first_interface = function_it->first_interface; | |
| 202 remaining_interfaces = function_it->interface_count - 1; | |
| 203 } else { | |
| 204 // Unassociated interfaces already have |first_interface| set to | |
| 205 // |interface_number|. | |
| 206 } | |
| 207 } | |
| 208 } | |
| 209 | |
| 210 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, | 131 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, |
| 211 base::string16* output) { | 132 base::string16* output) { |
| 212 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) | 133 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) |
| 213 return false; | 134 return false; |
| 214 | 135 |
| 215 // Let the device return a buffer larger than the actual string but prefer the | 136 // Let the device return a buffer larger than the actual string but prefer the |
| 216 // length reported inside the descriptor. | 137 // length reported inside the descriptor. |
| 217 size_t length = descriptor[0]; | 138 size_t length = descriptor[0]; |
| 218 length = std::min(length, descriptor.size()); | 139 length = std::min(length, descriptor.size()); |
| 219 if (length < 2) | 140 if (length < 2) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 236 callback.Run(std::move(index_map)); | 157 callback.Run(std::move(index_map)); |
| 237 return; | 158 return; |
| 238 } | 159 } |
| 239 | 160 |
| 240 ReadStringDescriptor(device_handle, 0, 0, | 161 ReadStringDescriptor(device_handle, 0, 0, |
| 241 base::Bind(&OnReadLanguageIds, device_handle, | 162 base::Bind(&OnReadLanguageIds, device_handle, |
| 242 base::Passed(&index_map), callback)); | 163 base::Passed(&index_map), callback)); |
| 243 } | 164 } |
| 244 | 165 |
| 245 } // namespace device | 166 } // namespace device |
| OLD | NEW |