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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c046942 was bd575647, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

libusbdev: Use shared hc_connection for pipes.

  • Property mode set to 100644
File size: 13.9 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 libusbdev
30 * @{
31 */
32/** @file
33 * Initialization of endpoint pipes.
34 *
35 */
36#include <usb/usb.h>
37#include <usb/dev/pipes.h>
38#include <usb/dev/dp.h>
39#include <usb/dev/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 const 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(const 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 const 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 const usb_endpoint_description_t *found_endpoint,
123 int interface_number, int interface_setting)
124{
125 while (mapping_count > 0) {
126 bool interface_number_fits = (mapping->interface_no < 0)
127 || (mapping->interface_no == interface_number);
128
129 bool interface_setting_fits = (mapping->interface_setting < 0)
130 || (mapping->interface_setting == interface_setting);
131
132 bool endpoint_descriptions_fits = endpoint_fits_description(
133 mapping->description, found_endpoint);
134
135 if (interface_number_fits
136 && interface_setting_fits
137 && endpoint_descriptions_fits) {
138 return mapping;
139 }
140
141 mapping++;
142 mapping_count--;
143 }
144 return NULL;
145}
146
147/** Process endpoint descriptor.
148 *
149 * @param mapping Endpoint mapping list.
150 * @param mapping_count Number of endpoint mappings in @p mapping.
151 * @param interface Interface descriptor under which belongs the @p endpoint.
152 * @param endpoint Endpoint descriptor.
153 * @param wire Connection backing the endpoint pipes.
154 * @return Error code.
155 */
156static int process_endpoint(
157 usb_endpoint_mapping_t *mapping, size_t mapping_count,
158 usb_standard_interface_descriptor_t *interface,
159 usb_standard_endpoint_descriptor_t *endpoint,
160 usb_device_connection_t *wire)
161{
162
163 /*
164 * Get endpoint characteristics.
165 */
166
167 /* Actual endpoint number is in bits 0..3 */
168 const usb_endpoint_t ep_no = endpoint->endpoint_address & 0x0F;
169
170 const usb_endpoint_description_t description = {
171 /* Endpoint direction is set by bit 7 */
172 .direction = (endpoint->endpoint_address & 128)
173 ? USB_DIRECTION_IN : USB_DIRECTION_OUT,
174 /* Transfer type is in bits 0..2 and
175 * the enum values corresponds 1:1 */
176 .transfer_type = endpoint->attributes & 3,
177
178 /* Get interface characteristics. */
179 .interface_class = interface->interface_class,
180 .interface_subclass = interface->interface_subclass,
181 .interface_protocol = interface->interface_protocol,
182 };
183
184 /*
185 * Find the most fitting mapping and initialize the pipe.
186 */
187 usb_endpoint_mapping_t *ep_mapping = find_endpoint_mapping(mapping,
188 mapping_count, &description,
189 interface->interface_number, interface->alternate_setting);
190 if (ep_mapping == NULL) {
191 return ENOENT;
192 }
193
194 if (ep_mapping->present) {
195 return EEXISTS;
196 }
197
198 int rc = usb_pipe_initialize(&ep_mapping->pipe, wire,
199 ep_no, description.transfer_type, endpoint->max_packet_size,
200 description.direction);
201 if (rc != EOK) {
202 return rc;
203 }
204
205 ep_mapping->present = true;
206 ep_mapping->descriptor = endpoint;
207 ep_mapping->interface = interface;
208
209 return EOK;
210}
211
212/** Process whole USB interface.
213 *
214 * @param mapping Endpoint mapping list.
215 * @param mapping_count Number of endpoint mappings in @p mapping.
216 * @param parser Descriptor parser.
217 * @param parser_data Descriptor parser data.
218 * @param interface_descriptor Interface descriptor.
219 * @return Error code.
220 */
221static int process_interface(
222 usb_endpoint_mapping_t *mapping, size_t mapping_count,
223 const usb_dp_parser_t *parser, const usb_dp_parser_data_t *parser_data,
224 const uint8_t *interface_descriptor)
225{
226 const uint8_t *descriptor = usb_dp_get_nested_descriptor(parser,
227 parser_data, interface_descriptor);
228
229 if (descriptor == NULL) {
230 return ENOENT;
231 }
232
233 do {
234 if (is_endpoint_descriptor(descriptor)) {
235 (void) process_endpoint(mapping, mapping_count,
236 (usb_standard_interface_descriptor_t *)
237 interface_descriptor,
238 (usb_standard_endpoint_descriptor_t *)
239 descriptor,
240 (usb_device_connection_t *) parser_data->arg);
241 }
242
243 descriptor = usb_dp_get_sibling_descriptor(parser, parser_data,
244 interface_descriptor, descriptor);
245 } while (descriptor != NULL);
246
247 return EOK;
248}
249
250/** Initialize endpoint pipes from configuration descriptor.
251 *
252 * The mapping array is expected to conform to following rules:
253 * - @c pipe must be uninitialized pipe
254 * - @c description must point to prepared endpoint description
255 * - @c descriptor does not need to be initialized (will be overwritten)
256 * - @c interface does not need to be initialized (will be overwritten)
257 * - @c present does not need to be initialized (will be overwritten)
258 *
259 * After processing the configuration descriptor, the mapping is updated
260 * in the following fashion:
261 * - @c present will be set to @c true when the endpoint was found in the
262 * configuration
263 * - @c descriptor will point inside the configuration descriptor to endpoint
264 * corresponding to given description (or NULL for not found descriptor)
265 * - @c interface will point inside the configuration descriptor to interface
266 * descriptor the endpoint @c descriptor belongs to (or NULL for not found
267 * descriptor)
268 * - @c pipe will be initialized when found, otherwise left untouched
269 * - @c description will be untouched under all circumstances
270 *
271 * @param mapping Endpoint mapping list.
272 * @param mapping_count Number of endpoint mappings in @p mapping.
273 * @param configuration_descriptor Full configuration descriptor (is expected
274 * to be in USB endianness: i.e. as-is after being retrieved from
275 * the device).
276 * @param configuration_descriptor_size Size of @p configuration_descriptor
277 * in bytes.
278 * @param connection Connection backing the endpoint pipes.
279 * @return Error code.
280 */
281int usb_pipe_initialize_from_configuration(
282 usb_endpoint_mapping_t *mapping, size_t mapping_count,
283 const uint8_t *config_descriptor, size_t config_descriptor_size,
284 usb_device_connection_t *connection)
285{
286 assert(connection);
287
288 if (config_descriptor == NULL) {
289 return EBADMEM;
290 }
291 if (config_descriptor_size
292 < sizeof(usb_standard_configuration_descriptor_t)) {
293 return ERANGE;
294 }
295
296 /* Go through the mapping and set all endpoints to not present. */
297 for (size_t i = 0; i < mapping_count; i++) {
298 mapping[i].present = false;
299 mapping[i].descriptor = NULL;
300 mapping[i].interface = NULL;
301 }
302
303 /* Prepare the descriptor parser. */
304 const usb_dp_parser_t dp_parser = {
305 .nesting = descriptor_nesting
306 };
307 const usb_dp_parser_data_t dp_data = {
308 .data = config_descriptor,
309 .size = config_descriptor_size,
310 .arg = connection
311 };
312
313 /*
314 * Iterate through all interfaces.
315 */
316 const uint8_t *interface = usb_dp_get_nested_descriptor(&dp_parser,
317 &dp_data, config_descriptor);
318 if (interface == NULL) {
319 return ENOENT;
320 }
321 do {
322 (void) process_interface(mapping, mapping_count,
323 &dp_parser, &dp_data, interface);
324 interface = usb_dp_get_sibling_descriptor(&dp_parser, &dp_data,
325 config_descriptor, interface);
326 } while (interface != NULL);
327
328 return EOK;
329}
330
331/** Initialize USB endpoint pipe.
332 *
333 * @param pipe Endpoint pipe to be initialized.
334 * @param connection Connection to the USB device backing this pipe (the wire).
335 * @param endpoint_no Endpoint number (in USB 1.1 in range 0 to 15).
336 * @param transfer_type Transfer type (e.g. interrupt or bulk).
337 * @param max_packet_size Maximum packet size in bytes.
338 * @param direction Endpoint direction (in/out).
339 * @return Error code.
340 */
341int usb_pipe_initialize(usb_pipe_t *pipe,
342 usb_device_connection_t *connection, usb_endpoint_t endpoint_no,
343 usb_transfer_type_t transfer_type, size_t max_packet_size,
344 usb_direction_t direction)
345{
346 assert(pipe);
347 assert(connection);
348
349 fibril_mutex_initialize(&pipe->guard);
350 pipe->wire = connection;
351 pipe->endpoint_no = endpoint_no;
352 pipe->transfer_type = transfer_type;
353 pipe->max_packet_size = max_packet_size;
354 pipe->direction = direction;
355 pipe->auto_reset_halt = false;
356
357 return EOK;
358}
359
360
361/** Initialize USB endpoint pipe as the default zero control pipe.
362 *
363 * @param pipe Endpoint pipe to be initialized.
364 * @param connection Connection to the USB device backing this pipe (the wire).
365 * @return Error code.
366 */
367int usb_pipe_initialize_default_control(usb_pipe_t *pipe,
368 usb_device_connection_t *connection)
369{
370 assert(pipe);
371 assert(connection);
372
373 int rc = usb_pipe_initialize(pipe, connection,
374 0, USB_TRANSFER_CONTROL, CTRL_PIPE_MIN_PACKET_SIZE,
375 USB_DIRECTION_BOTH);
376
377 pipe->auto_reset_halt = true;
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
404 usb_pipe_start_long_transfer(pipe);
405
406 uint8_t dev_descr_start[CTRL_PIPE_MIN_PACKET_SIZE];
407 size_t transferred_size;
408 int rc;
409 for (size_t attempt_var = 0; attempt_var < 3; ++attempt_var) {
410 rc = usb_request_get_descriptor(pipe, USB_REQUEST_TYPE_STANDARD,
411 USB_REQUEST_RECIPIENT_DEVICE, USB_DESCTYPE_DEVICE,
412 0, 0, dev_descr_start, CTRL_PIPE_MIN_PACKET_SIZE,
413 &transferred_size);
414 if (rc == EOK) {
415 if (transferred_size != CTRL_PIPE_MIN_PACKET_SIZE) {
416 rc = ELIMIT;
417 continue;
418 }
419 break;
420 }
421 }
422 usb_pipe_end_long_transfer(pipe);
423 if (rc != EOK) {
424 return rc;
425 }
426
427 pipe->max_packet_size
428 = dev_descr_start[DEV_DESCR_MAX_PACKET_SIZE_OFFSET];
429
430 return EOK;
431}
432
433/** Register endpoint with the host controller.
434 *
435 * @param pipe Pipe to be registered.
436 * @param interval Polling interval.
437 * @param hc_connection Connection to the host controller (must be opened).
438 * @return Error code.
439 */
440int usb_pipe_register(usb_pipe_t *pipe, unsigned interval)
441{
442 assert(pipe);
443 assert(pipe->wire);
444 assert(pipe->wire->hc_connection);
445
446 return usb_hc_register_endpoint(pipe->wire->hc_connection,
447 pipe->wire->address, pipe->endpoint_no, pipe->transfer_type,
448 pipe->direction, pipe->max_packet_size, interval);
449}
450
451/** Revert endpoint registration with the host controller.
452 *
453 * @param pipe Pipe to be unregistered.
454 * @param hc_connection Connection to the host controller (must be opened).
455 * @return Error code.
456 */
457int usb_pipe_unregister(usb_pipe_t *pipe)
458{
459 assert(pipe);
460 assert(pipe->wire);
461 assert(pipe->wire->hc_connection);
462
463 return usb_hc_unregister_endpoint(pipe->wire->hc_connection,
464 pipe->wire->address, pipe->endpoint_no, pipe->direction);
465}
466
467/**
468 * @}
469 */
Note: See TracBrowser for help on using the repository browser.