source: mainline/uspace/lib/usbdev/src/pipesinit.c@ e49d0ac

Last change on this file since e49d0ac was d7f7a4a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 4 years ago

Replace some license headers with SPDX identifier

Headers are replaced using tools/transorm-copyright.sh only
when it can be matched verbatim with the license header used
throughout most of the codebase.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * SPDX-FileCopyrightText: 2011 Vojtech Horky
3 * SPDX-FileCopyrightText: 2018 Ondrej Hlavaty, Michal Staruch
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8/** @addtogroup libusbdev
9 * @{
10 */
11/** @file
12 * Non trivial initialization of endpoint pipes.
13 *
14 */
15#include <usb/dev/pipes.h>
16#include <usb/dev/dp.h>
17#include <usb/dev/request.h>
18#include <usb/usb.h>
19#include <usb/debug.h>
20#include <usb/descriptor.h>
21
22#include <assert.h>
23#include <errno.h>
24
25#define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
26
27#define NESTING(parentname, childname) \
28 { \
29 .child = USB_DESCTYPE_##childname, \
30 .parent = USB_DESCTYPE_##parentname, \
31 }
32#define LAST_NESTING { -1, -1 }
33
34/** Nesting pairs of standard descriptors. */
35static const usb_dp_descriptor_nesting_t descriptor_nesting[] = {
36 NESTING(CONFIGURATION, INTERFACE),
37 NESTING(INTERFACE, ENDPOINT),
38 NESTING(INTERFACE, HUB),
39 NESTING(INTERFACE, HID),
40 NESTING(HID, HID_REPORT),
41 NESTING(ENDPOINT, SSPEED_EP_COMPANION),
42 LAST_NESTING
43};
44
45/** Tells whether given descriptor is of endpoint type.
46 *
47 * @param descriptor Descriptor in question.
48 * @return Whether the given descriptor is endpoint descriptor.
49 */
50static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
51{
52 return descriptor[1] == USB_DESCTYPE_ENDPOINT;
53}
54
55/** Tells whether given descriptor is of superspeed companion type.
56 *
57 * @param descriptor Descriptor in question.
58 * @return Whether the given descriptor is superspeed companion descriptor.
59 */
60static inline bool is_superspeed_companion_descriptor(const uint8_t *descriptor)
61{
62 return descriptor[1] == USB_DESCTYPE_SSPEED_EP_COMPANION;
63}
64
65/** Tells whether found endpoint corresponds to endpoint described by user.
66 *
67 * @param wanted Endpoint description as entered by driver author.
68 * @param found Endpoint description obtained from endpoint descriptor.
69 * @return Whether the @p found descriptor fits the @p wanted descriptor.
70 */
71static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
72 const usb_endpoint_description_t *found)
73{
74#define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
75
76 if (!_SAME(direction)) {
77 return false;
78 }
79
80 if (!_SAME(transfer_type)) {
81 return false;
82 }
83
84 if ((wanted->interface_class >= 0) && !_SAME(interface_class)) {
85 return false;
86 }
87
88 if ((wanted->interface_subclass >= 0) && !_SAME(interface_subclass)) {
89 return false;
90 }
91
92 if ((wanted->interface_protocol >= 0) && !_SAME(interface_protocol)) {
93 return false;
94 }
95
96#undef _SAME
97
98 return true;
99}
100
101/** Find endpoint mapping for a found endpoint.
102 *
103 * @param mapping Endpoint mapping list.
104 * @param mapping_count Number of endpoint mappings in @p mapping.
105 * @param found_endpoint Description of found endpoint.
106 * @param interface_number Number of currently processed interface.
107 * @return Endpoint mapping corresponding to @p found_endpoint.
108 * @retval NULL No corresponding endpoint found.
109 */
110static usb_endpoint_mapping_t *find_endpoint_mapping(
111 usb_endpoint_mapping_t *mapping, size_t mapping_count,
112 const usb_endpoint_description_t *found_endpoint,
113 int interface_number, int interface_setting)
114{
115 while (mapping_count > 0) {
116 bool interface_number_fits = (mapping->interface_no < 0) ||
117 (mapping->interface_no == interface_number);
118
119 bool interface_setting_fits = (mapping->interface_setting < 0) ||
120 (mapping->interface_setting == interface_setting);
121
122 bool endpoint_descriptions_fits = endpoint_fits_description(
123 mapping->description, found_endpoint);
124
125 if (interface_number_fits &&
126 interface_setting_fits &&
127 endpoint_descriptions_fits &&
128 !mapping->present) {
129 return mapping;
130 }
131
132 mapping++;
133 mapping_count--;
134 }
135
136 return NULL;
137}
138
139/** Process endpoint descriptor.
140 *
141 * @param mapping Endpoint mapping list.
142 * @param mapping_count Number of endpoint mappings in @p mapping.
143 * @param interface Interface descriptor under which belongs the @p endpoint.
144 * @param endpoint Endpoint descriptor.
145 * @param companion Superspeed companion descriptor.
146 * @return Error code.
147 */
148static errno_t process_endpoint(
149 usb_endpoint_mapping_t *mapping, size_t mapping_count,
150 usb_standard_interface_descriptor_t *interface,
151 usb_standard_endpoint_descriptor_t *endpoint_desc,
152 usb_superspeed_endpoint_companion_descriptor_t *companion_desc,
153 usb_dev_session_t *bus_session)
154{
155
156 /*
157 * Get endpoint characteristics.
158 */
159 const usb_endpoint_description_t description = {
160 .transfer_type = USB_ED_GET_TRANSFER_TYPE(*endpoint_desc),
161 .direction = USB_ED_GET_DIR(*endpoint_desc),
162
163 /* Get interface characteristics. */
164 .interface_class = interface->interface_class,
165 .interface_subclass = interface->interface_subclass,
166 .interface_protocol = interface->interface_protocol,
167 };
168
169 /*
170 * Find the most fitting mapping and initialize the pipe.
171 */
172 usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
173 mapping_count, &description,
174 interface->interface_number, interface->alternate_setting);
175 if (ep_mapping == NULL) {
176 return ENOENT;
177 }
178
179 if (ep_mapping->present) {
180 return EEXIST;
181 }
182
183 errno_t err = usb_pipe_initialize(&ep_mapping->pipe, bus_session);
184 if (err)
185 return err;
186
187 ep_mapping->present = true;
188 ep_mapping->descriptor = endpoint_desc;
189 ep_mapping->companion_descriptor = companion_desc;
190 ep_mapping->interface = interface;
191
192 return EOK;
193}
194
195/** Process whole USB interface.
196 *
197 * @param mapping Endpoint mapping list.
198 * @param mapping_count Number of endpoint mappings in @p mapping.
199 * @param parser Descriptor parser.
200 * @param parser_data Descriptor parser data.
201 * @param interface_descriptor Interface descriptor.
202 * @return Error code.
203 */
204static errno_t process_interface(
205 usb_endpoint_mapping_t *mapping, size_t mapping_count,
206 const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
207 const uint8_t *interface_descriptor, usb_dev_session_t *bus_session)
208{
209 const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
210 parser_data, interface_descriptor);
211
212 if (descriptor == NULL) {
213 return ENOENT;
214 }
215
216 do {
217 if (is_endpoint_descriptor(descriptor)) {
218 /* Check if companion descriptor is present too, it should immediatelly follow. */
219 const uint8_t *companion_desc = usb_dp_get_nested_descriptor(parser,
220 parser_data, descriptor);
221 if (companion_desc && !is_superspeed_companion_descriptor(companion_desc)) {
222 /* Not what we wanted, don't pass it further. */
223 companion_desc = NULL;
224 }
225
226 (void) process_endpoint(mapping, mapping_count,
227 (usb_standard_interface_descriptor_t *)
228 interface_descriptor,
229 (usb_standard_endpoint_descriptor_t *)
230 descriptor,
231 (usb_superspeed_endpoint_companion_descriptor_t *)
232 companion_desc,
233 bus_session);
234 }
235
236 descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
237 interface_descriptor, descriptor);
238 } while (descriptor != NULL);
239
240 return EOK;
241}
242
243/** Initialize endpoint pipes from configuration descriptor.
244 *
245 * The mapping array is expected to conform to following rules:
246 * - @c pipe must be uninitialized pipe
247 * - @c description must point to prepared endpoint description
248 * - @c descriptor does not need to be initialized (will be overwritten)
249 * - @c interface does not need to be initialized (will be overwritten)
250 * - @c present does not need to be initialized (will be overwritten)
251 *
252 * After processing the configuration descriptor, the mapping is updated
253 * in the following fashion:
254 * - @c present will be set to @c true when the endpoint was found in the
255 * configuration
256 * - @c descriptor will point inside the configuration descriptor to endpoint
257 * corresponding to given description (or NULL for not found descriptor)
258 * - @c interface will point inside the configuration descriptor to interface
259 * descriptor the endpoint @c descriptor belongs to (or NULL for not found
260 * descriptor)
261 * - @c pipe will be initialized when found, otherwise left untouched
262 * - @c description will be untouched under all circumstances
263 *
264 * @param mapping Endpoint mapping list.
265 * @param mapping_count Number of endpoint mappings in @p mapping.
266 * @param configuration_descriptor Full configuration descriptor (is expected
267 * to be in USB endianness: i.e. as-is after being retrieved from
268 * the device).
269 * @param configuration_descriptor_size Size of @p configuration_descriptor
270 * in bytes.
271 * @param connection Connection backing the endpoint pipes.
272 * @return Error code.
273 */
274errno_t usb_pipe_initialize_from_configuration(
275 usb_endpoint_mapping_t *mapping, size_t mapping_count,
276 const uint8_t *config_descriptor, size_t config_descriptor_size,
277 usb_dev_session_t *bus_session)
278{
279 if (config_descriptor == NULL)
280 return EBADMEM;
281
282 if (config_descriptor_size <
283 sizeof(usb_standard_configuration_descriptor_t)) {
284 return ERANGE;
285 }
286
287 /* Go through the mapping and set all endpoints to not present. */
288 for (size_t i = 0; i < mapping_count; i++) {
289 mapping[i].present = false;
290 mapping[i].descriptor = NULL;
291 mapping[i].interface = NULL;
292 }
293
294 /* Prepare the descriptor parser. */
295 const usb_dp_parser_t dp_parser = {
296 .nesting = descriptor_nesting
297 };
298 const usb_dp_parser_data_t dp_data = {
299 .data = config_descriptor,
300 .size = config_descriptor_size,
301 };
302
303 /*
304 * Iterate through all interfaces.
305 */
306 const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
307 &dp_data, config_descriptor);
308 if (interface == NULL) {
309 return ENOENT;
310 }
311 do {
312 (void) process_interface(mapping, mapping_count,
313 &dp_parser, &dp_data, interface, bus_session);
314 interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
315 config_descriptor, interface);
316 } while (interface != NULL);
317
318 return EOK;
319}
320
321/**
322 * @}
323 */
Note: See TracBrowser for help on using the repository browser.