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> |
10 | 11 |
11 #include "base/barrier_closure.h" | 12 #include "base/barrier_closure.h" |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "device/usb/usb_device_handle.h" | 14 #include "device/usb/usb_device_handle.h" |
14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
15 | 16 |
16 namespace device { | 17 namespace device { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 using IndexMap = std::map<uint8_t, base::string16>; | 21 using IndexMap = std::map<uint8_t, base::string16>; |
21 using IndexMapPtr = scoped_ptr<IndexMap>; | 22 using IndexMapPtr = scoped_ptr<IndexMap>; |
22 | 23 |
23 // Standard USB requests and descriptor types: | 24 // Standard USB requests and descriptor types: |
24 const uint8_t kGetDescriptorRequest = 0x06; | 25 const uint8_t kGetDescriptorRequest = 0x06; |
25 const uint8_t kStringDescriptorType = 0x03; | 26 const uint8_t kStringDescriptorType = 0x03; |
26 | 27 |
27 const int kControlTransferTimeout = 60000; // 1 minute | 28 const int kControlTransferTimeout = 60000; // 1 minute |
28 | 29 |
| 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 |
29 void StoreStringDescriptor(IndexMap::iterator it, | 66 void StoreStringDescriptor(IndexMap::iterator it, |
30 const base::Closure& callback, | 67 const base::Closure& callback, |
31 const base::string16& string) { | 68 const base::string16& string) { |
32 it->second = string; | 69 it->second = string; |
33 callback.Run(); | 70 callback.Run(); |
34 } | 71 } |
35 | 72 |
36 void OnReadStringDescriptor( | 73 void OnReadStringDescriptor( |
37 const base::Callback<void(const base::string16&)>& callback, | 74 const base::Callback<void(const base::string16&)>& callback, |
38 UsbTransferStatus status, | 75 UsbTransferStatus status, |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 146 |
110 UsbInterfaceDescriptor::UsbInterfaceDescriptor(uint8_t interface_number, | 147 UsbInterfaceDescriptor::UsbInterfaceDescriptor(uint8_t interface_number, |
111 uint8_t alternate_setting, | 148 uint8_t alternate_setting, |
112 uint8_t interface_class, | 149 uint8_t interface_class, |
113 uint8_t interface_subclass, | 150 uint8_t interface_subclass, |
114 uint8_t interface_protocol) | 151 uint8_t interface_protocol) |
115 : interface_number(interface_number), | 152 : interface_number(interface_number), |
116 alternate_setting(alternate_setting), | 153 alternate_setting(alternate_setting), |
117 interface_class(interface_class), | 154 interface_class(interface_class), |
118 interface_subclass(interface_subclass), | 155 interface_subclass(interface_subclass), |
119 interface_protocol(interface_protocol) {} | 156 interface_protocol(interface_protocol), |
| 157 first_interface(interface_number) {} |
120 | 158 |
121 UsbInterfaceDescriptor::UsbInterfaceDescriptor( | 159 UsbInterfaceDescriptor::UsbInterfaceDescriptor( |
122 const UsbInterfaceDescriptor& other) = default; | 160 const UsbInterfaceDescriptor& other) = default; |
123 | 161 |
124 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() = default; | 162 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() = default; |
125 | 163 |
126 UsbConfigDescriptor::UsbConfigDescriptor(uint8_t configuration_value, | 164 UsbConfigDescriptor::UsbConfigDescriptor(uint8_t configuration_value, |
127 bool self_powered, | 165 bool self_powered, |
128 bool remote_wakeup, | 166 bool remote_wakeup, |
129 uint16_t maximum_power) | 167 uint16_t maximum_power) |
130 : configuration_value(configuration_value), | 168 : configuration_value(configuration_value), |
131 self_powered(self_powered), | 169 self_powered(self_powered), |
132 remote_wakeup(remote_wakeup), | 170 remote_wakeup(remote_wakeup), |
133 maximum_power(maximum_power) {} | 171 maximum_power(maximum_power) {} |
134 | 172 |
135 UsbConfigDescriptor::UsbConfigDescriptor(const UsbConfigDescriptor& other) = | 173 UsbConfigDescriptor::UsbConfigDescriptor(const UsbConfigDescriptor& other) = |
136 default; | 174 default; |
137 | 175 |
138 UsbConfigDescriptor::~UsbConfigDescriptor() = default; | 176 UsbConfigDescriptor::~UsbConfigDescriptor() = default; |
139 | 177 |
| 178 void UsbConfigDescriptor::AssignFirstInterfaceNumbers() { |
| 179 std::vector<UsbInterfaceAssociationDescriptor> functions; |
| 180 ParseInterfaceAssociationDescriptors(extra_data, &functions); |
| 181 for (const auto& interface : interfaces) { |
| 182 ParseInterfaceAssociationDescriptors(interface.extra_data, &functions); |
| 183 for (const auto& endpoint : interface.endpoints) |
| 184 ParseInterfaceAssociationDescriptors(endpoint.extra_data, &functions); |
| 185 } |
| 186 |
| 187 // libusb has collected interface association descriptors in the |extra_data| |
| 188 // fields of other descriptor types. This may have disturbed their order |
| 189 // but sorting by the bFirstInterface should fix it. |
| 190 std::sort(functions.begin(), functions.end()); |
| 191 |
| 192 uint8_t remaining_interfaces = 0; |
| 193 auto function_it = functions.cbegin(); |
| 194 auto interface_it = interfaces.begin(); |
| 195 while (interface_it != interfaces.end()) { |
| 196 if (remaining_interfaces > 0) { |
| 197 // Continuation of a previous function. Tag all alternate interfaces |
| 198 // (which are guaranteed to be contiguous). |
| 199 for (uint8_t interface_number = interface_it->interface_number; |
| 200 interface_it != interfaces.end() && |
| 201 interface_it->interface_number == interface_number; |
| 202 ++interface_it) { |
| 203 interface_it->first_interface = function_it->first_interface; |
| 204 } |
| 205 if (--remaining_interfaces == 0) |
| 206 ++function_it; |
| 207 } else if (function_it != functions.end() && |
| 208 interface_it->interface_number == function_it->first_interface) { |
| 209 // Start of a new function. |
| 210 interface_it->first_interface = function_it->first_interface; |
| 211 if (function_it->interface_count > 1) |
| 212 remaining_interfaces = function_it->interface_count - 1; |
| 213 else |
| 214 ++function_it; |
| 215 ++interface_it; |
| 216 } else { |
| 217 // Unassociated interfaces already have |first_interface| set to |
| 218 // |interface_number|. |
| 219 ++interface_it; |
| 220 } |
| 221 } |
| 222 } |
| 223 |
140 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, | 224 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, |
141 base::string16* output) { | 225 base::string16* output) { |
142 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) | 226 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) |
143 return false; | 227 return false; |
144 | 228 |
145 // Let the device return a buffer larger than the actual string but prefer the | 229 // Let the device return a buffer larger than the actual string but prefer the |
146 // length reported inside the descriptor. | 230 // length reported inside the descriptor. |
147 size_t length = descriptor[0]; | 231 size_t length = descriptor[0]; |
148 length = std::min(length, descriptor.size()); | 232 length = std::min(length, descriptor.size()); |
149 if (length < 2) | 233 if (length < 2) |
(...skipping 16 matching lines...) Expand all Loading... |
166 callback.Run(std::move(index_map)); | 250 callback.Run(std::move(index_map)); |
167 return; | 251 return; |
168 } | 252 } |
169 | 253 |
170 ReadStringDescriptor(device_handle, 0, 0, | 254 ReadStringDescriptor(device_handle, 0, 0, |
171 base::Bind(&OnReadLanguageIds, device_handle, | 255 base::Bind(&OnReadLanguageIds, device_handle, |
172 base::Passed(&index_map), callback)); | 256 base::Passed(&index_map), callback)); |
173 } | 257 } |
174 | 258 |
175 } // namespace device | 259 } // namespace device |
OLD | NEW |