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