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 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 143 |
107 UsbInterfaceDescriptor::UsbInterfaceDescriptor(uint8_t interface_number, | 144 UsbInterfaceDescriptor::UsbInterfaceDescriptor(uint8_t interface_number, |
108 uint8_t alternate_setting, | 145 uint8_t alternate_setting, |
109 uint8_t interface_class, | 146 uint8_t interface_class, |
110 uint8_t interface_subclass, | 147 uint8_t interface_subclass, |
111 uint8_t interface_protocol) | 148 uint8_t interface_protocol) |
112 : interface_number(interface_number), | 149 : interface_number(interface_number), |
113 alternate_setting(alternate_setting), | 150 alternate_setting(alternate_setting), |
114 interface_class(interface_class), | 151 interface_class(interface_class), |
115 interface_subclass(interface_subclass), | 152 interface_subclass(interface_subclass), |
116 interface_protocol(interface_protocol) {} | 153 interface_protocol(interface_protocol), |
154 first_interface(interface_number) {} | |
117 | 155 |
118 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() = default; | 156 UsbInterfaceDescriptor::~UsbInterfaceDescriptor() = default; |
119 | 157 |
120 UsbConfigDescriptor::UsbConfigDescriptor(uint8_t configuration_value, | 158 UsbConfigDescriptor::UsbConfigDescriptor(uint8_t configuration_value, |
121 bool self_powered, | 159 bool self_powered, |
122 bool remote_wakeup, | 160 bool remote_wakeup, |
123 uint16_t maximum_power) | 161 uint16_t maximum_power) |
124 : configuration_value(configuration_value), | 162 : configuration_value(configuration_value), |
125 self_powered(self_powered), | 163 self_powered(self_powered), |
126 remote_wakeup(remote_wakeup), | 164 remote_wakeup(remote_wakeup), |
127 maximum_power(maximum_power) {} | 165 maximum_power(maximum_power) {} |
128 | 166 |
129 UsbConfigDescriptor::~UsbConfigDescriptor() = default; | 167 UsbConfigDescriptor::~UsbConfigDescriptor() = default; |
130 | 168 |
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() && | |
gab
2016/02/14 21:02:17
This is wrong as if the inner loop exits because t
gab
2016/02/14 21:10:06
Apologies, looks like there is a unit test, but it
| |
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 | |
131 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, | 210 bool ParseUsbStringDescriptor(const std::vector<uint8_t>& descriptor, |
132 base::string16* output) { | 211 base::string16* output) { |
133 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) | 212 if (descriptor.size() < 2 || descriptor[1] != kStringDescriptorType) |
134 return false; | 213 return false; |
135 | 214 |
136 // Let the device return a buffer larger than the actual string but prefer the | 215 // Let the device return a buffer larger than the actual string but prefer the |
137 // length reported inside the descriptor. | 216 // length reported inside the descriptor. |
138 size_t length = descriptor[0]; | 217 size_t length = descriptor[0]; |
139 length = std::min(length, descriptor.size()); | 218 length = std::min(length, descriptor.size()); |
140 if (length < 2) | 219 if (length < 2) |
(...skipping 16 matching lines...) Expand all Loading... | |
157 callback.Run(std::move(index_map)); | 236 callback.Run(std::move(index_map)); |
158 return; | 237 return; |
159 } | 238 } |
160 | 239 |
161 ReadStringDescriptor(device_handle, 0, 0, | 240 ReadStringDescriptor(device_handle, 0, 0, |
162 base::Bind(&OnReadLanguageIds, device_handle, | 241 base::Bind(&OnReadLanguageIds, device_handle, |
163 base::Passed(&index_map), callback)); | 242 base::Passed(&index_map), callback)); |
164 } | 243 } |
165 | 244 |
166 } // namespace device | 245 } // namespace device |
OLD | NEW |