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

Last change on this file was 3bacee1, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Make ccheck-fix again and commit more good files.

  • Property mode set to 100644
File size: 10.8 KB
RevLine 
[93ef8f6]1/*
2 * Copyright (c) 2011 Vojtech Horky
[e0a5d4c]3 * Copyright (c) 2018 Ondrej Hlavaty, Michal Staruch
[93ef8f6]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[160b75e]30/** @addtogroup libusbdev
[93ef8f6]31 * @{
32 */
33/** @file
[3538b0e]34 * Non trivial initialization of endpoint pipes.
[93ef8f6]35 *
36 */
[7d521e24]37#include <usb/dev/pipes.h>
38#include <usb/dev/dp.h>
39#include <usb/dev/request.h>
[c01987c]40#include <usb/usb.h>
[ec700c7]41#include <usb/debug.h>
[c01987c]42#include <usb/descriptor.h>
43
[93ef8f6]44#include <assert.h>
[c01987c]45#include <errno.h>
[93ef8f6]46
[206f71a]47#define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
48
[93ef8f6]49#define NESTING(parentname, childname) \
50 { \
51 .child = USB_DESCTYPE_##childname, \
52 .parent = USB_DESCTYPE_##parentname, \
53 }
54#define LAST_NESTING { -1, -1 }
55
56/** Nesting pairs of standard descriptors. */
[3ddbd38]57static const usb_dp_descriptor_nesting_t descriptor_nesting[] = {
[93ef8f6]58 NESTING(CONFIGURATION, INTERFACE),
59 NESTING(INTERFACE, ENDPOINT),
60 NESTING(INTERFACE, HUB),
61 NESTING(INTERFACE, HID),
62 NESTING(HID, HID_REPORT),
[ec700c7]63 NESTING(ENDPOINT, SSPEED_EP_COMPANION),
[93ef8f6]64 LAST_NESTING
65};
66
67/** Tells whether given descriptor is of endpoint type.
68 *
69 * @param descriptor Descriptor in question.
70 * @return Whether the given descriptor is endpoint descriptor.
71 */
[8a121b1]72static inline bool is_endpoint_descriptor(const uint8_t *descriptor)
[93ef8f6]73{
74 return descriptor[1] == USB_DESCTYPE_ENDPOINT;
75}
76
[ec700c7]77/** Tells whether given descriptor is of superspeed companion type.
78 *
79 * @param descriptor Descriptor in question.
80 * @return Whether the given descriptor is superspeed companion descriptor.
81 */
82static inline bool is_superspeed_companion_descriptor(const uint8_t *descriptor)
83{
84 return descriptor[1] == USB_DESCTYPE_SSPEED_EP_COMPANION;
85}
86
[93ef8f6]87/** Tells whether found endpoint corresponds to endpoint described by user.
88 *
89 * @param wanted Endpoint description as entered by driver author.
90 * @param found Endpoint description obtained from endpoint descriptor.
91 * @return Whether the @p found descriptor fits the @p wanted descriptor.
92 */
93static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
[8a121b1]94 const usb_endpoint_description_t *found)
[93ef8f6]95{
96#define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
97
98 if (!_SAME(direction)) {
99 return false;
100 }
101
102 if (!_SAME(transfer_type)) {
103 return false;
104 }
105
106 if ((wanted->interface_class >= 0) && !_SAME(interface_class)) {
107 return false;
108 }
109
110 if ((wanted->interface_subclass >= 0) && !_SAME(interface_subclass)) {
111 return false;
112 }
113
114 if ((wanted->interface_protocol >= 0) && !_SAME(interface_protocol)) {
115 return false;
116 }
117
118#undef _SAME
119
120 return true;
121}
122
123/** Find endpoint mapping for a found endpoint.
124 *
125 * @param mapping Endpoint mapping list.
126 * @param mapping_count Number of endpoint mappings in @p mapping.
127 * @param found_endpoint Description of found endpoint.
[18cb870]128 * @param interface_number Number of currently processed interface.
[93ef8f6]129 * @return Endpoint mapping corresponding to @p found_endpoint.
130 * @retval NULL No corresponding endpoint found.
131 */
132static usb_endpoint_mapping_t *find_endpoint_mapping(
133 usb_endpoint_mapping_t *mapping, size_t mapping_count,
[8a121b1]134 const usb_endpoint_description_t *found_endpoint,
[159b91f4]135 int interface_number, int interface_setting)
[93ef8f6]136{
137 while (mapping_count > 0) {
[3bacee1]138 bool interface_number_fits = (mapping->interface_no < 0) ||
139 (mapping->interface_no == interface_number);
[18cb870]140
[3bacee1]141 bool interface_setting_fits = (mapping->interface_setting < 0) ||
142 (mapping->interface_setting == interface_setting);
[159b91f4]143
[18cb870]144 bool endpoint_descriptions_fits = endpoint_fits_description(
145 mapping->description, found_endpoint);
146
[3bacee1]147 if (interface_number_fits &&
148 interface_setting_fits &&
149 endpoint_descriptions_fits &&
150 !mapping->present) {
[93ef8f6]151 return mapping;
152 }
153
154 mapping++;
155 mapping_count--;
156 }
[1a2227d]157
[93ef8f6]158 return NULL;
159}
160
161/** Process endpoint descriptor.
162 *
163 * @param mapping Endpoint mapping list.
164 * @param mapping_count Number of endpoint mappings in @p mapping.
165 * @param interface Interface descriptor under which belongs the @p endpoint.
166 * @param endpoint Endpoint descriptor.
[ec700c7]167 * @param companion Superspeed companion descriptor.
[93ef8f6]168 * @return Error code.
169 */
[5a6cc679]170static errno_t process_endpoint(
[93ef8f6]171 usb_endpoint_mapping_t *mapping, size_t mapping_count,
172 usb_standard_interface_descriptor_t *interface,
[48f0f73a]173 usb_standard_endpoint_descriptor_t *endpoint_desc,
[ec700c7]174 usb_superspeed_endpoint_companion_descriptor_t *companion_desc,
[d93f5afb]175 usb_dev_session_t *bus_session)
[93ef8f6]176{
177
178 /*
179 * Get endpoint characteristics.
180 */
[8a121b1]181 const usb_endpoint_description_t description = {
[9efad54]182 .transfer_type = USB_ED_GET_TRANSFER_TYPE(*endpoint_desc),
183 .direction = USB_ED_GET_DIR(*endpoint_desc),
[8a121b1]184
185 /* Get interface characteristics. */
186 .interface_class = interface->interface_class,
187 .interface_subclass = interface->interface_subclass,
188 .interface_protocol = interface->interface_protocol,
189 };
[93ef8f6]190
191 /*
192 * Find the most fitting mapping and initialize the pipe.
193 */
194 usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
[159b91f4]195 mapping_count, &description,
196 interface->interface_number, interface->alternate_setting);
[93ef8f6]197 if (ep_mapping == NULL) {
198 return ENOENT;
199 }
200
201 if (ep_mapping->present) {
[8a637a4]202 return EEXIST;
[93ef8f6]203 }
204
[5a6cc679]205 errno_t err = usb_pipe_initialize(&ep_mapping->pipe, bus_session);
[9e5b162]206 if (err)
207 return err;
[93ef8f6]208
209 ep_mapping->present = true;
[48f0f73a]210 ep_mapping->descriptor = endpoint_desc;
[ec700c7]211 ep_mapping->companion_descriptor = companion_desc;
[9d4579e]212 ep_mapping->interface = interface;
[93ef8f6]213
214 return EOK;
215}
216
217/** Process whole USB interface.
218 *
219 * @param mapping Endpoint mapping list.
220 * @param mapping_count Number of endpoint mappings in @p mapping.
221 * @param parser Descriptor parser.
222 * @param parser_data Descriptor parser data.
223 * @param interface_descriptor Interface descriptor.
224 * @return Error code.
225 */
[5a6cc679]226static errno_t process_interface(
[93ef8f6]227 usb_endpoint_mapping_t *mapping, size_t mapping_count,
[7c95d6f5]228 const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
[8582076]229 const uint8_t *interface_descriptor, usb_dev_session_t *bus_session)
[93ef8f6]230{
[8a121b1]231 const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
[93ef8f6]232 parser_data, interface_descriptor);
233
234 if (descriptor == NULL) {
235 return ENOENT;
236 }
237
238 do {
239 if (is_endpoint_descriptor(descriptor)) {
[ec700c7]240 /* Check if companion descriptor is present too, it should immediatelly follow. */
241 const uint8_t *companion_desc = usb_dp_get_nested_descriptor(parser,
[3bacee1]242 parser_data, descriptor);
[ec700c7]243 if (companion_desc && !is_superspeed_companion_descriptor(companion_desc)) {
244 /* Not what we wanted, don't pass it further. */
245 companion_desc = NULL;
246 }
247
[93ef8f6]248 (void) process_endpoint(mapping, mapping_count,
249 (usb_standard_interface_descriptor_t *)
[3bacee1]250 interface_descriptor,
[93ef8f6]251 (usb_standard_endpoint_descriptor_t *)
[3bacee1]252 descriptor,
[ec700c7]253 (usb_superspeed_endpoint_companion_descriptor_t *)
[3bacee1]254 companion_desc,
[8582076]255 bus_session);
[93ef8f6]256 }
257
258 descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
259 interface_descriptor, descriptor);
260 } while (descriptor != NULL);
261
262 return EOK;
263}
264
265/** Initialize endpoint pipes from configuration descriptor.
266 *
267 * The mapping array is expected to conform to following rules:
[b77931d]268 * - @c pipe must be uninitialized pipe
[93ef8f6]269 * - @c description must point to prepared endpoint description
270 * - @c descriptor does not need to be initialized (will be overwritten)
[9d4579e]271 * - @c interface does not need to be initialized (will be overwritten)
[93ef8f6]272 * - @c present does not need to be initialized (will be overwritten)
273 *
274 * After processing the configuration descriptor, the mapping is updated
275 * in the following fashion:
276 * - @c present will be set to @c true when the endpoint was found in the
277 * configuration
278 * - @c descriptor will point inside the configuration descriptor to endpoint
[1110ebd]279 * corresponding to given description (or NULL for not found descriptor)
[9d4579e]280 * - @c interface will point inside the configuration descriptor to interface
281 * descriptor the endpoint @c descriptor belongs to (or NULL for not found
282 * descriptor)
[93ef8f6]283 * - @c pipe will be initialized when found, otherwise left untouched
284 * - @c description will be untouched under all circumstances
285 *
286 * @param mapping Endpoint mapping list.
287 * @param mapping_count Number of endpoint mappings in @p mapping.
288 * @param configuration_descriptor Full configuration descriptor (is expected
289 * to be in USB endianness: i.e. as-is after being retrieved from
290 * the device).
291 * @param configuration_descriptor_size Size of @p configuration_descriptor
292 * in bytes.
293 * @param connection Connection backing the endpoint pipes.
294 * @return Error code.
295 */
[5a6cc679]296errno_t usb_pipe_initialize_from_configuration(
[93ef8f6]297 usb_endpoint_mapping_t *mapping, size_t mapping_count,
[7c95d6f5]298 const uint8_t *config_descriptor, size_t config_descriptor_size,
[d93f5afb]299 usb_dev_session_t *bus_session)
[93ef8f6]300{
[58563585]301 if (config_descriptor == NULL)
[93ef8f6]302 return EBADMEM;
[816f5f4]303
[58563585]304 if (config_descriptor_size <
305 sizeof(usb_standard_configuration_descriptor_t)) {
[93ef8f6]306 return ERANGE;
307 }
308
[b77931d]309 /* Go through the mapping and set all endpoints to not present. */
310 for (size_t i = 0; i < mapping_count; i++) {
[93ef8f6]311 mapping[i].present = false;
312 mapping[i].descriptor = NULL;
[9d4579e]313 mapping[i].interface = NULL;
[93ef8f6]314 }
315
[b77931d]316 /* Prepare the descriptor parser. */
[7c95d6f5]317 const usb_dp_parser_t dp_parser = {
[93ef8f6]318 .nesting = descriptor_nesting
319 };
[7c95d6f5]320 const usb_dp_parser_data_t dp_data = {
321 .data = config_descriptor,
322 .size = config_descriptor_size,
[93ef8f6]323 };
324
325 /*
326 * Iterate through all interfaces.
327 */
[8a121b1]328 const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
[7c95d6f5]329 &dp_data, config_descriptor);
[93ef8f6]330 if (interface == NULL) {
331 return ENOENT;
332 }
333 do {
334 (void) process_interface(mapping, mapping_count,
[8582076]335 &dp_parser, &dp_data, interface, bus_session);
[93ef8f6]336 interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
[7c95d6f5]337 config_descriptor, interface);
[93ef8f6]338 } while (interface != NULL);
339
340 return EOK;
341}
342
343/**
344 * @}
345 */
Note: See TracBrowser for help on using the repository browser.