source: mainline/uspace/lib/usbhost/src/usb2_bus.c@ 8b8c164

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b8c164 was 8b8c164, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

libusbhost bus: endpoint→device is now managed by bus implementation

That allows xhci to better isolate responsibilities.

  • Property mode set to 100644
File size: 14.0 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
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/** @addtogroup libusbhost
30 * @{
31 */
32/** @file
33 * HC Endpoint management.
34 */
35
36#include <usb/host/usb2_bus.h>
37#include <usb/host/endpoint.h>
38#include <usb/host/ddf_helpers.h>
39#include <usb/debug.h>
40#include <usb/request.h>
41#include <usb/descriptor.h>
42#include <usb/usb.h>
43
44#include <assert.h>
45#include <errno.h>
46#include <macros.h>
47#include <str_error.h>
48#include <stdlib.h>
49#include <stdbool.h>
50
51/** Ops receive generic bus_t pointer. */
52static inline usb2_bus_t *bus_to_usb2_bus(bus_t *bus_base)
53{
54 assert(bus_base);
55 return (usb2_bus_t *) bus_base;
56}
57
58/** Get list that holds endpoints for given address.
59 * @param bus usb2_bus structure, non-null.
60 * @param addr USB address, must be >= 0.
61 * @return Pointer to the appropriate list.
62 */
63static list_t * get_list(usb2_bus_t *bus, usb_address_t addr)
64{
65 assert(bus);
66 assert(addr >= 0);
67 return &bus->devices[addr % ARRAY_SIZE(bus->devices)].endpoint_list;
68}
69
70/** Get speed assigned to USB address.
71 *
72 * @param[in] bus Device manager structure to use.
73 * @param[in] address Address the caller wants to find.
74 * @param[out] speed Assigned speed.
75 * @return Error code.
76 */
77static int usb2_bus_get_speed(bus_t *bus_base, usb_address_t address, usb_speed_t *speed)
78{
79 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
80
81 if (!usb_address_is_valid(address)) {
82 return EINVAL;
83 }
84
85 const int ret = bus->devices[address].occupied ? EOK : ENOENT;
86 if (speed && bus->devices[address].occupied) {
87 *speed = bus->devices[address].speed;
88 }
89
90 return ret;
91}
92
93static const usb_endpoint_desc_t usb2_default_control_ep = {
94 .endpoint_no = 0,
95 .transfer_type = USB_TRANSFER_CONTROL,
96 .direction = USB_DIRECTION_BOTH,
97 .max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
98 .packets = 1,
99};
100
101
102static const usb_target_t usb2_default_target = {{
103 .address = USB_ADDRESS_DEFAULT,
104 .endpoint = 0,
105}};
106
107static int usb2_bus_address_device(bus_t *bus, hcd_t *hcd, device_t *dev)
108{
109 int err;
110
111 /** Reserve address early, we want pretty log messages */
112 usb_address_t address;
113 if ((err = bus_request_address(bus, &address, false, dev->speed))) {
114 usb_log_error("Failed to reserve new address: %s.",
115 str_error(err));
116 return err;
117 }
118 usb_log_debug("Device(%d): Reserved new address.", address);
119
120 /* Add default pipe on default address */
121 usb_log_debug("Device(%d): Adding default target (0:0)", address);
122
123 endpoint_t *default_ep;
124 err = bus_add_endpoint(bus, dev, &usb2_default_control_ep, &default_ep);
125 if (err != EOK) {
126 usb_log_error("Device(%d): Failed to add default target: %s.",
127 address, str_error(err));
128 goto err_address;
129 }
130
131 /* Get max packet size for default pipe */
132 usb_standard_device_descriptor_t desc = { 0 };
133 const usb_device_request_setup_packet_t get_device_desc_8 =
134 GET_DEVICE_DESC(CTRL_PIPE_MIN_PACKET_SIZE);
135
136 usb_log_debug("Device(%d): Requesting first 8B of device descriptor.",
137 address);
138 ssize_t got = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_IN,
139 (char *) &desc, CTRL_PIPE_MIN_PACKET_SIZE, *(uint64_t *)&get_device_desc_8,
140 "read first 8 bytes of dev descriptor");
141
142 if (got != CTRL_PIPE_MIN_PACKET_SIZE) {
143 err = got < 0 ? got : EOVERFLOW;
144 usb_log_error("Device(%d): Failed to get 8B of dev descr: %s.",
145 address, str_error(err));
146 goto err_default_control_ep;
147 }
148
149 /* Set new address */
150 const usb_device_request_setup_packet_t set_address = SET_ADDRESS(address);
151
152 usb_log_debug("Device(%d): Setting USB address.", address);
153 err = hcd_send_batch_sync(hcd, dev, usb2_default_target, USB_DIRECTION_OUT,
154 NULL, 0, *(uint64_t *)&set_address, "set address");
155 if (err != 0) {
156 usb_log_error("Device(%d): Failed to set new address: %s.",
157 address, str_error(got));
158 goto err_default_control_ep;
159 }
160
161 /* We need to remove ep before we change the address */
162 if ((err = bus_remove_endpoint(bus, default_ep))) {
163 usb_log_error("Device(%d): Failed to unregister default target: %s", address, str_error(err));
164 goto err_address;
165 }
166 endpoint_del_ref(default_ep);
167
168 dev->address = address;
169
170 const usb_endpoint_desc_t control_ep = {
171 .endpoint_no = 0,
172 .transfer_type = USB_TRANSFER_CONTROL,
173 .direction = USB_DIRECTION_BOTH,
174 .max_packet_size = ED_MPS_PACKET_SIZE_GET(uint16_usb2host(desc.max_packet_size)),
175 .packets = ED_MPS_TRANS_OPPORTUNITIES_GET(uint16_usb2host(desc.max_packet_size)),
176 };
177
178 /* Register EP on the new address */
179 usb_log_debug("Device(%d): Registering control EP.", address);
180 err = bus_add_endpoint(bus, dev, &control_ep, NULL);
181 if (err != EOK) {
182 usb_log_error("Device(%d): Failed to register EP0: %s",
183 address, str_error(err));
184 goto err_address;
185 }
186
187 return EOK;
188
189err_default_control_ep:
190 bus_remove_endpoint(bus, default_ep);
191 endpoint_del_ref(default_ep);
192err_address:
193 bus_release_address(bus, address);
194 return err;
195}
196
197/** Enumerate a new USB device
198 */
199static int usb2_bus_enumerate_device(bus_t *bus, hcd_t *hcd, device_t *dev)
200{
201 int err;
202
203 /* The speed of the new device was reported by the hub when reserving
204 * default address.
205 */
206 if ((err = usb2_bus_get_speed(bus, USB_ADDRESS_DEFAULT, &dev->speed))) {
207 usb_log_error("Failed to verify speed: %s.", str_error(err));
208 return err;
209 }
210 usb_log_debug("Found new %s speed USB device.", usb_str_speed(dev->speed));
211
212 if (dev->hub) {
213 /* Manage TT */
214 if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
215 /* For LS devices under HS hub */
216 /* TODO: How about SS hubs? */
217 dev->tt.address = dev->hub->address;
218 dev->tt.port = dev->port;
219 }
220 else {
221 /* Inherit hub's TT */
222 dev->tt = dev->hub->tt;
223 }
224 }
225 else {
226 dev->tt = (usb_tt_address_t) {
227 .address = -1,
228 .port = 0,
229 };
230 }
231
232 /* Assign an address to the device */
233 if ((err = usb2_bus_address_device(bus, hcd, dev))) {
234 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
235 return err;
236 }
237
238 /* Read the device descriptor, derive the match ids */
239 if ((err = hcd_ddf_device_explore(hcd, dev))) {
240 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
241 bus_release_address(bus, dev->address);
242 return err;
243 }
244
245 return EOK;
246}
247
248/** Get a free USB address
249 *
250 * @param[in] bus Device manager structure to use.
251 * @return Free address, or error code.
252 */
253static int usb_bus_get_free_address(usb2_bus_t *bus, usb_address_t *addr)
254{
255 usb_address_t new_address = bus->last_address;
256 do {
257 new_address = (new_address + 1) % USB_ADDRESS_COUNT;
258 if (new_address == USB_ADDRESS_DEFAULT)
259 new_address = 1;
260 if (new_address == bus->last_address)
261 return ENOSPC;
262 } while (bus->devices[new_address].occupied);
263
264 assert(new_address != USB_ADDRESS_DEFAULT);
265 bus->last_address = new_address;
266
267 *addr = new_address;
268 return EOK;
269}
270
271/** Find endpoint.
272 * @param bus usb_bus structure, non-null.
273 * @param target Endpoint address.
274 * @param direction Communication direction.
275 * @return Pointer to endpoint_t structure representing given communication
276 * target, NULL if there is no such endpoint registered.
277 * @note Assumes that the internal mutex is locked.
278 */
279static endpoint_t *usb2_bus_find_ep(bus_t *bus_base, device_t *device, usb_target_t target, usb_direction_t direction)
280{
281 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
282
283 assert(device->address == target.address);
284
285 list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
286 if (((direction == ep->direction)
287 || (ep->direction == USB_DIRECTION_BOTH)
288 || (direction == USB_DIRECTION_BOTH))
289 && (target.endpoint == ep->endpoint))
290 return ep;
291 }
292 return NULL;
293}
294
295static endpoint_t *usb2_bus_create_ep(bus_t *bus)
296{
297 endpoint_t *ep = malloc(sizeof(endpoint_t));
298 if (!ep)
299 return NULL;
300
301 endpoint_init(ep, bus);
302 return ep;
303}
304
305static usb_target_t usb2_ep_to_target(endpoint_t *ep)
306{
307 assert(ep);
308 assert(ep->device);
309
310 return (usb_target_t) {{
311 .address = ep->device->address,
312 .endpoint = ep->endpoint,
313 }};
314}
315
316/** Register an endpoint to the bus. Reserves bandwidth.
317 * @param bus usb_bus structure, non-null.
318 * @param endpoint USB endpoint number.
319 */
320static int usb2_bus_register_ep(bus_t *bus_base, device_t *device, endpoint_t *ep, const usb_endpoint_desc_t *desc)
321{
322 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
323 assert(ep);
324
325 ep->device = device;
326
327 /* Extract USB2-related information from endpoint_desc */
328 ep->endpoint = desc->endpoint_no;
329 ep->direction = desc->direction;
330 ep->transfer_type = desc->transfer_type;
331 ep->max_packet_size = desc->max_packet_size;
332 ep->packets = desc->packets;
333
334 ep->bandwidth = bus_base->ops.count_bw(ep, desc->max_packet_size);
335
336 /* Check for existence */
337 if (usb2_bus_find_ep(bus_base, ep->device, usb2_ep_to_target(ep), ep->direction))
338 return EEXIST;
339
340 /* Check for available bandwidth */
341 if (ep->bandwidth > bus->free_bw)
342 return ENOSPC;
343
344 list_append(&ep->link, get_list(bus, ep->device->address));
345 bus->free_bw -= ep->bandwidth;
346
347 return EOK;
348}
349
350
351/** Release bandwidth reserved by the given endpoint.
352 */
353static int usb2_bus_unregister_ep(bus_t *bus_base, endpoint_t *ep)
354{
355 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
356 assert(ep);
357
358 list_remove(&ep->link);
359 ep->device = NULL;
360
361 bus->free_bw += ep->bandwidth;
362
363 return EOK;
364}
365
366static int usb2_bus_reset_toggle(bus_t *bus_base, usb_target_t target, bool all)
367{
368 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
369
370 if (!usb_target_is_valid(target))
371 return EINVAL;
372
373 int ret = ENOENT;
374
375 list_foreach(*get_list(bus, target.address), link, endpoint_t, ep) {
376 assert(ep->device->address == target.address);
377
378 if (all || ep->endpoint == target.endpoint) {
379 endpoint_toggle_set(ep, 0);
380 ret = EOK;
381 }
382 }
383 return ret;
384}
385
386/** Unregister and destroy all endpoints using given address.
387 * @param bus usb_bus structure, non-null.
388 * @param address USB address.
389 * @param endpoint USB endpoint number.
390 * @param direction Communication direction.
391 * @return Error code.
392 */
393static int usb2_bus_release_address(bus_t *bus_base, usb_address_t address)
394{
395 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
396
397 if (!usb_address_is_valid(address))
398 return EINVAL;
399
400 const int ret = bus->devices[address].occupied ? EOK : ENOENT;
401 bus->devices[address].occupied = false;
402
403 list_t *list = get_list(bus, address);
404 for (link_t *link = list_first(list); link != NULL; ) {
405 endpoint_t *ep = list_get_instance(link, endpoint_t, link);
406 link = list_next(link, list);
407
408 assert(ep->device->address == address);
409 list_remove(&ep->link);
410
411 usb_log_warning("Endpoint %d:%d %s was left behind, removing.\n",
412 address, ep->endpoint, usb_str_direction(ep->direction));
413
414 /* Drop bus reference */
415 endpoint_del_ref(ep);
416 }
417
418 return ret;
419}
420
421/** Request USB address.
422 * @param bus usb_device_manager
423 * @param addr Pointer to requested address value, place to store new address
424 * @parma strict Fail if the requested address is not available.
425 * @return Error code.
426 * @note Default address is only available in strict mode.
427 */
428static int usb2_bus_request_address(bus_t *bus_base, usb_address_t *addr, bool strict, usb_speed_t speed)
429{
430 int err;
431
432 usb2_bus_t *bus = bus_to_usb2_bus(bus_base);
433 assert(addr);
434
435 if (!usb_address_is_valid(*addr))
436 return EINVAL;
437
438 /* Only grant default address to strict requests */
439 if ((*addr == USB_ADDRESS_DEFAULT) && !strict) {
440 if ((err = usb_bus_get_free_address(bus, addr)))
441 return err;
442 }
443 else if (bus->devices[*addr].occupied) {
444 if (strict) {
445 return ENOENT;
446 }
447 if ((err = usb_bus_get_free_address(bus, addr)))
448 return err;
449 }
450
451 assert(usb_address_is_valid(*addr));
452 assert(bus->devices[*addr].occupied == false);
453 assert(*addr != USB_ADDRESS_DEFAULT || strict);
454
455 bus->devices[*addr].occupied = true;
456 bus->devices[*addr].speed = speed;
457
458 return EOK;
459}
460
461static const bus_ops_t usb2_bus_ops = {
462 .enumerate_device = usb2_bus_enumerate_device,
463 .create_endpoint = usb2_bus_create_ep,
464 .find_endpoint = usb2_bus_find_ep,
465 .unregister_endpoint = usb2_bus_unregister_ep,
466 .register_endpoint = usb2_bus_register_ep,
467 .request_address = usb2_bus_request_address,
468 .release_address = usb2_bus_release_address,
469 .reset_toggle = usb2_bus_reset_toggle,
470};
471
472/** Initialize to default state.
473 *
474 * @param bus usb_bus structure, non-null.
475 * @param available_bandwidth Size of the bandwidth pool.
476 * @param bw_count function to use to calculate endpoint bw requirements.
477 * @return Error code.
478 */
479int usb2_bus_init(usb2_bus_t *bus, size_t available_bandwidth, count_bw_func_t count_bw)
480{
481 assert(bus);
482
483 bus_init(&bus->base, sizeof(device_t));
484
485 bus->base.ops = usb2_bus_ops;
486 bus->base.ops.count_bw = count_bw;
487
488 bus->free_bw = available_bandwidth;
489 bus->last_address = 0;
490 for (unsigned i = 0; i < ARRAY_SIZE(bus->devices); ++i) {
491 list_initialize(&bus->devices[i].endpoint_list);
492 bus->devices[i].speed = USB_SPEED_MAX;
493 bus->devices[i].occupied = false;
494 }
495 return EOK;
496}
497/**
498 * @}
499 */
Note: See TracBrowser for help on using the repository browser.