source: mainline/uspace/lib/usb/src/pipesinit.c@ 3954a63b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3954a63b was 3954a63b, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Rename usb_endpoint_pipe_*() ⇒ usb_pipe_*()

No change in functionality.

  • Property mode set to 100644
File size: 14.2 KB
Line 
1/*
2 * Copyright (c) 2011 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libusb
30 * @{
31 */
32/** @file
33 * Initialization of endpoint pipes.
34 *
35 */
36#include <usb/usb.h>
37#include <usb/pipes.h>
38#include <usb/dp.h>
39#include <usb/request.h>
40#include <usbhc_iface.h>
41#include <errno.h>
42#include <assert.h>
43
44#define CTRL_PIPE_MIN_PACKET_SIZE 8
45#define DEV_DESCR_MAX_PACKET_SIZE_OFFSET 7
46
47
48#define NESTING(parentname, childname) \
49 { \
50 .child = USB_DESCTYPE_##childname, \
51 .parent = USB_DESCTYPE_##parentname, \
52 }
53#define LAST_NESTING { -1, -1 }
54
55/** Nesting pairs of standard descriptors. */
56static usb_dp_descriptor_nesting_t descriptor_nesting[] = {
57 NESTING(CONFIGURATION, INTERFACE),
58 NESTING(INTERFACE, ENDPOINT),
59 NESTING(INTERFACE, HUB),
60 NESTING(INTERFACE, HID),
61 NESTING(HID, HID_REPORT),
62 LAST_NESTING
63};
64
65/** Tells whether given descriptor is of endpoint type.
66 *
67 * @param descriptor Descriptor in question.
68 * @return Whether the given descriptor is endpoint descriptor.
69 */
70static inline bool is_endpoint_descriptor(uint8_t *descriptor)
71{
72 return descriptor[1] == USB_DESCTYPE_ENDPOINT;
73}
74
75/** Tells whether found endpoint corresponds to endpoint described by user.
76 *
77 * @param wanted Endpoint description as entered by driver author.
78 * @param found Endpoint description obtained from endpoint descriptor.
79 * @return Whether the @p found descriptor fits the @p wanted descriptor.
80 */
81static bool endpoint_fits_description(const usb_endpoint_description_t *wanted,
82 usb_endpoint_description_t *found)
83{
84#define _SAME(fieldname) ((wanted->fieldname) == (found->fieldname))
85
86 if (!_SAME(direction)) {
87 return false;
88 }
89
90 if (!_SAME(transfer_type)) {
91 return false;
92 }
93
94 if ((wanted->interface_class >= 0) && !_SAME(interface_class)) {
95 return false;
96 }
97
98 if ((wanted->interface_subclass >= 0) && !_SAME(interface_subclass)) {
99 return false;
100 }
101
102 if ((wanted->interface_protocol >= 0) && !_SAME(interface_protocol)) {
103 return false;
104 }
105
106#undef _SAME
107
108 return true;
109}
110
111/** Find endpoint mapping for a found endpoint.
112 *
113 * @param mapping Endpoint mapping list.
114 * @param mapping_count Number of endpoint mappings in @p mapping.
115 * @param found_endpoint Description of found endpoint.
116 * @param interface_number Number of currently processed interface.
117 * @return Endpoint mapping corresponding to @p found_endpoint.
118 * @retval NULL No corresponding endpoint found.
119 */
120static usb_endpoint_mapping_t *find_endpoint_mapping(
121 usb_endpoint_mapping_t *mapping, size_t mapping_count,
122 usb_endpoint_description_t *found_endpoint,
123 int interface_number)
124{
125 while (mapping_count > 0) {
126 bool interface_number_fits = (mapping->interface_no < 0)
127 || (mapping->interface_no == interface_number);
128
129 bool endpoint_descriptions_fits = endpoint_fits_description(
130 mapping->description, found_endpoint);
131
132 if (interface_number_fits && endpoint_descriptions_fits) {
133 return mapping;
134 }
135
136 mapping++;
137 mapping_count--;
138 }
139 return NULL;
140}
141
142/** Process endpoint descriptor.
143 *
144 * @param mapping Endpoint mapping list.
145 * @param mapping_count Number of endpoint mappings in @p mapping.
146 * @param interface Interface descriptor under which belongs the @p endpoint.
147 * @param endpoint Endpoint descriptor.
148 * @param wire Connection backing the endpoint pipes.
149 * @return Error code.
150 */
151static int process_endpoint(
152 usb_endpoint_mapping_t *mapping, size_t mapping_count,
153 usb_standard_interface_descriptor_t *interface,
154 usb_standard_endpoint_descriptor_t *endpoint,
155 usb_device_connection_t *wire)
156{
157 usb_endpoint_description_t description;
158
159 /*
160 * Get endpoint characteristics.
161 */
162
163 /* Actual endpoint number is in bits 0..3 */
164 usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
165
166 /* Endpoint direction is set by bit 7 */
167 description.direction = (endpoint->endpoint_address & 128)
168 ? USB_DIRECTION_IN : USB_DIRECTION_OUT;
169 /* Transfer type is in bits 0..2 and the enum values corresponds 1:1 */
170 description.transfer_type = endpoint->attributes & 3;
171
172 /*
173 * Get interface characteristics.
174 */
175 description.interface_class = interface->interface_class;
176 description.interface_subclass = interface->interface_subclass;
177 description.interface_protocol = interface->interface_protocol;
178
179 /*
180 * Find the most fitting mapping and initialize the pipe.
181 */
182 usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
183 mapping_count, &description, interface->interface_number);
184 if (ep_mapping == NULL) {
185 return ENOENT;
186 }
187
188 if (ep_mapping->pipe == NULL) {
189 return EBADMEM;
190 }
191 if (ep_mapping->present) {
192 return EEXISTS;
193 }
194
195 int rc = usb_pipe_initialize(ep_mapping->pipe, wire,
196 ep_no, description.transfer_type, endpoint->max_packet_size,
197 description.direction);
198 if (rc != EOK) {
199 return rc;
200 }
201
202 ep_mapping->present = true;
203 ep_mapping->descriptor = endpoint;
204 ep_mapping->interface = interface;
205
206 return EOK;
207}
208
209/** Process whole USB interface.
210 *
211 * @param mapping Endpoint mapping list.
212 * @param mapping_count Number of endpoint mappings in @p mapping.
213 * @param parser Descriptor parser.
214 * @param parser_data Descriptor parser data.
215 * @param interface_descriptor Interface descriptor.
216 * @return Error code.
217 */
218static int process_interface(
219 usb_endpoint_mapping_t *mapping, size_t mapping_count,
220 usb_dp_parser_t *parser, usb_dp_parser_data_t *parser_data,
221 uint8_t *interface_descriptor)
222{
223 uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
224 parser_data, interface_descriptor);
225
226 if (descriptor == NULL) {
227 return ENOENT;
228 }
229
230 do {
231 if (is_endpoint_descriptor(descriptor)) {
232 (void) process_endpoint(mapping, mapping_count,
233 (usb_standard_interface_descriptor_t *)
234 interface_descriptor,
235 (usb_standard_endpoint_descriptor_t *)
236 descriptor,
237 (usb_device_connection_t *) parser_data->arg);
238 }
239
240 descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
241 interface_descriptor, descriptor);
242 } while (descriptor != NULL);
243
244 return EOK;
245}
246
247/** Initialize endpoint pipes from configuration descriptor.
248 *
249 * The mapping array is expected to conform to following rules:
250 * - @c pipe must point to already allocated structure with uninitialized pipe
251 * - @c description must point to prepared endpoint description
252 * - @c descriptor does not need to be initialized (will be overwritten)
253 * - @c interface does not need to be initialized (will be overwritten)
254 * - @c present does not need to be initialized (will be overwritten)
255 *
256 * After processing the configuration descriptor, the mapping is updated
257 * in the following fashion:
258 * - @c present will be set to @c true when the endpoint was found in the
259 * configuration
260 * - @c descriptor will point inside the configuration descriptor to endpoint
261 * corresponding to given description (or NULL for not found descriptor)
262 * - @c interface will point inside the configuration descriptor to interface
263 * descriptor the endpoint @c descriptor belongs to (or NULL for not found
264 * descriptor)
265 * - @c pipe will be initialized when found, otherwise left untouched
266 * - @c description will be untouched under all circumstances
267 *
268 * @param mapping Endpoint mapping list.
269 * @param mapping_count Number of endpoint mappings in @p mapping.
270 * @param configuration_descriptor Full configuration descriptor (is expected
271 * to be in USB endianness: i.e. as-is after being retrieved from
272 * the device).
273 * @param configuration_descriptor_size Size of @p configuration_descriptor
274 * in bytes.
275 * @param connection Connection backing the endpoint pipes.
276 * @return Error code.
277 */
278int usb_pipe_initialize_from_configuration(
279 usb_endpoint_mapping_t *mapping, size_t mapping_count,
280 uint8_t *configuration_descriptor, size_t configuration_descriptor_size,
281 usb_device_connection_t *connection)
282{
283 assert(connection);
284
285 if (configuration_descriptor == NULL) {
286 return EBADMEM;
287 }
288 if (configuration_descriptor_size
289 < sizeof(usb_standard_configuration_descriptor_t)) {
290 return ERANGE;
291 }
292
293 /*
294 * Go through the mapping and set all endpoints to not present.
295 */
296 size_t i;
297 for (i = 0; i < mapping_count; i++) {
298 mapping[i].present = false;
299 mapping[i].descriptor = NULL;
300 mapping[i].interface = NULL;
301 }
302
303 /*
304 * Prepare the descriptor parser.
305 */
306 usb_dp_parser_t dp_parser = {
307 .nesting = descriptor_nesting
308 };
309 usb_dp_parser_data_t dp_data = {
310 .data = configuration_descriptor,
311 .size = configuration_descriptor_size,
312 .arg = connection
313 };
314
315 /*
316 * Iterate through all interfaces.
317 */
318 uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
319 &dp_data, configuration_descriptor);
320 if (interface == NULL) {
321 return ENOENT;
322 }
323 do {
324 (void) process_interface(mapping, mapping_count,
325 &dp_parser, &dp_data,
326 interface);
327 interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
328 configuration_descriptor, interface);
329 } while (interface != NULL);
330
331 return EOK;
332}
333
334/** Initialize USB endpoint pipe.
335 *
336 * @param pipe Endpoint pipe to be initialized.
337 * @param connection Connection to the USB device backing this pipe (the wire).
338 * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
339 * @param transfer_type Transfer type (e.g. interrupt or bulk).
340 * @param max_packet_size Maximum packet size in bytes.
341 * @param direction Endpoint direction (in/out).
342 * @return Error code.
343 */
344int usb_pipe_initialize(usb_pipe_t *pipe,
345 usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
346 usb_transfer_type_t transfer_type, size_t max_packet_size,
347 usb_direction_t direction)
348{
349 assert(pipe);
350 assert(connection);
351
352 pipe->wire = connection;
353 pipe->hc_phone = -1;
354 pipe->endpoint_no = endpoint_no;
355 pipe->transfer_type = transfer_type;
356 pipe->max_packet_size = max_packet_size;
357 pipe->direction = direction;
358
359 return EOK;
360}
361
362
363/** Initialize USB endpoint pipe as the default zero control pipe.
364 *
365 * @param pipe Endpoint pipe to be initialized.
366 * @param connection Connection to the USB device backing this pipe (the wire).
367 * @return Error code.
368 */
369int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
370 usb_device_connection_t *connection)
371{
372 assert(pipe);
373 assert(connection);
374
375 int rc = usb_pipe_initialize(pipe, connection,
376 0, USB_TRANSFER_CONTROL, CTRL_PIPE_MIN_PACKET_SIZE,
377 USB_DIRECTION_BOTH);
378
379 return rc;
380}
381
382/** Probe default control pipe for max packet size.
383 *
384 * The function tries to get the correct value of max packet size several
385 * time before giving up.
386 *
387 * The session on the pipe shall not be started.
388 *
389 * @param pipe Default control pipe.
390 * @return Error code.
391 */
392int usb_pipe_probe_default_control(usb_pipe_t *pipe)
393{
394 assert(pipe);
395 assert(DEV_DESCR_MAX_PACKET_SIZE_OFFSET < CTRL_PIPE_MIN_PACKET_SIZE);
396
397 if ((pipe->direction != USB_DIRECTION_BOTH) ||
398 (pipe->transfer_type != USB_TRANSFER_CONTROL) ||
399 (pipe->endpoint_no != 0)) {
400 return EINVAL;
401 }
402
403#define TRY_LOOP(attempt_var) \
404 for (attempt_var = 0; attempt_var < 3; attempt_var++)
405
406 size_t failed_attempts;
407 int rc;
408
409 TRY_LOOP(failed_attempts) {
410 rc = usb_pipe_start_session(pipe);
411 if (rc == EOK) {
412 break;
413 }
414 }
415 if (rc != EOK) {
416 return rc;
417 }
418
419
420 uint8_t dev_descr_start[CTRL_PIPE_MIN_PACKET_SIZE];
421 size_t transferred_size;
422 TRY_LOOP(failed_attempts) {
423 rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD,
424 USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_DEVICE,
425 0, 0, dev_descr_start, CTRL_PIPE_MIN_PACKET_SIZE,
426 &transferred_size);
427 if (rc == EOK) {
428 if (transferred_size != CTRL_PIPE_MIN_PACKET_SIZE) {
429 rc = ELIMIT;
430 continue;
431 }
432 break;
433 }
434 }
435 usb_pipe_end_session(pipe);
436 if (rc != EOK) {
437 return rc;
438 }
439
440 pipe->max_packet_size
441 = dev_descr_start[DEV_DESCR_MAX_PACKET_SIZE_OFFSET];
442
443 return EOK;
444}
445
446/** Register endpoint with the host controller.
447 *
448 * @param pipe Pipe to be registered.
449 * @param interval Polling interval.
450 * @param hc_connection Connection to the host controller (must be opened).
451 * @return Error code.
452 */
453int usb_pipe_register(usb_pipe_t *pipe,
454 unsigned int interval,
455 usb_hc_connection_t *hc_connection)
456{
457 assert(pipe);
458 assert(hc_connection);
459
460 if (!usb_hc_connection_is_opened(hc_connection)) {
461 return EBADF;
462 }
463
464#define _PACK(high, low) ((high) * 256 + (low))
465
466 return async_req_5_0(hc_connection->hc_phone,
467 DEV_IFACE_ID(USBHC_DEV_IFACE), IPC_M_USBHC_REGISTER_ENDPOINT,
468 _PACK(pipe->wire->address, pipe->endpoint_no),
469 _PACK(pipe->transfer_type, pipe->direction),
470 pipe->max_packet_size, interval);
471
472#undef _PACK
473}
474
475/** Revert endpoint registration with the host controller.
476 *
477 * @param pipe Pipe to be unregistered.
478 * @param hc_connection Connection to the host controller (must be opened).
479 * @return Error code.
480 */
481int usb_pipe_unregister(usb_pipe_t *pipe,
482 usb_hc_connection_t *hc_connection)
483{
484 assert(pipe);
485 assert(hc_connection);
486
487 if (!usb_hc_connection_is_opened(hc_connection)) {
488 return EBADF;
489 }
490
491 return async_req_4_0(hc_connection->hc_phone,
492 DEV_IFACE_ID(USBHC_DEV_IFACE), IPC_M_USBHC_UNREGISTER_ENDPOINT,
493 pipe->wire->address, pipe->endpoint_no, pipe->direction);
494}
495
496/**
497 * @}
498 */
Note: See TracBrowser for help on using the repository browser.