source: mainline/uspace/drv/bus/usb/xhci/bus.c@ bdd8842c

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

xhci: revised handling of max_burst, mult and max_streams

  • Property mode set to 100644
File size: 14.2 KB
Line 
1/*
2 * Copyright (c) 2017 Ondrej Hlavaty <aearsis@eideo.cz>
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/** @addtogroup libusbhost
29 * @{
30 */
31/** @file
32 * HC Endpoint management.
33 */
34
35#include <usb/host/ddf_helpers.h>
36#include <usb/host/endpoint.h>
37#include <usb/host/hcd.h>
38#include <usb/debug.h>
39
40#include <assert.h>
41#include <errno.h>
42#include <str_error.h>
43#include <macros.h>
44#include <stdbool.h>
45
46#include "hc.h"
47#include "bus.h"
48#include "endpoint.h"
49#include "transfers.h"
50
51
52/** Initial descriptor used for control endpoint 0 before more configuration is retrieved. */
53static const usb_endpoint_descriptors_t ep0_initial_desc = {
54 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
55};
56
57static endpoint_t *endpoint_create(device_t *, const usb_endpoint_descriptors_t *);
58
59/** Ops receive generic bus_t pointer. */
60static inline xhci_bus_t *bus_to_xhci_bus(bus_t *bus_base)
61{
62 assert(bus_base);
63 return (xhci_bus_t *) bus_base;
64}
65
66/**
67 * Assign address and control endpoint to a new XHCI device. Once this function
68 * successfully returns, the device is online.
69 *
70 * @param[in] bus XHCI bus, in which the address is assigned.
71 * @param[in] dev New device to address and configure./e
72 * @return Error code.
73 */
74static int address_device(xhci_bus_t *bus, xhci_device_t *dev)
75{
76 int err;
77
78 /* Enable new slot. */
79 if ((err = hc_enable_slot(bus->hc, &dev->slot_id)) != EOK)
80 return err;
81 usb_log_debug2("Obtained slot ID: %u.\n", dev->slot_id);
82
83 /* Create and configure control endpoint. */
84 endpoint_t *ep0_base = endpoint_create(&dev->base, &ep0_initial_desc);
85 if (!ep0_base)
86 goto err_slot;
87
88 /* Bus reference */
89 endpoint_add_ref(ep0_base);
90 dev->base.endpoints[0] = ep0_base;
91
92 xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
93
94 if ((err = xhci_endpoint_alloc_transfer_ds(ep0)))
95 goto err_added;
96
97 /* Address device */
98 if ((err = hc_address_device(bus->hc, dev, ep0)))
99 goto err_prepared;
100
101 return EOK;
102
103err_prepared:
104 xhci_endpoint_free_transfer_ds(ep0);
105err_added:
106 /* Bus reference */
107 endpoint_del_ref(ep0_base);
108 dev->base.endpoints[0] = NULL;
109err_slot:
110 hc_disable_slot(bus->hc, dev);
111 return err;
112}
113
114/**
115 * Retrieve and set maximum packet size for endpoint zero of a XHCI device.
116 *
117 * @param[in] hc Host controller, which manages the device.
118 * @param[in] dev Device with operational endpoint zero.
119 * @return Error code.
120 */
121static int setup_ep0_packet_size(xhci_hc_t *hc, xhci_device_t *dev)
122{
123 int err;
124
125 uint16_t max_packet_size;
126 if ((err = hcd_get_ep0_max_packet_size(&max_packet_size, (bus_t *) &hc->bus, &dev->base)))
127 return err;
128
129 xhci_endpoint_t *ep0 = xhci_device_get_endpoint(dev, 0);
130 assert(ep0);
131
132 if (ep0->base.max_packet_size == max_packet_size)
133 return EOK;
134
135 ep0->base.max_packet_size = max_packet_size;
136 ep0->base.max_transfer_size = max_packet_size * ep0->base.packets_per_uframe;
137
138 xhci_ep_ctx_t ep_ctx;
139 xhci_setup_endpoint_context(ep0, &ep_ctx);
140
141 if ((err = hc_update_endpoint(hc, dev->slot_id, 0, &ep_ctx)))
142 return err;
143
144 return EOK;
145}
146
147/**
148 * Respond to a new device on the XHCI bus. Address it, negotiate packet size
149 * and retrieve USB descriptors.
150 *
151 * @param[in] bus XHCI bus, where the new device emerged.
152 * @param[in] dev XHCI device, which has appeared on the bus.
153 *
154 * @return Error code.
155 */
156static int device_enumerate(device_t *dev)
157{
158 int err;
159 xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
160 xhci_device_t *xhci_dev = xhci_device_get(dev);
161
162 hcd_setup_device_tt(dev);
163
164 /* Calculate route string */
165 xhci_device_t *xhci_hub = xhci_device_get(dev->hub);
166 xhci_dev->tier = xhci_hub->tier + 1;
167 xhci_dev->route_str = xhci_hub->route_str;
168
169 /* Roothub port is not part of the route string */
170 if (xhci_dev->tier >= 2) {
171 const unsigned offset = 4 * (xhci_dev->tier - 2);
172 xhci_dev->route_str |= (dev->port & 0xf) << offset;
173 xhci_dev->rh_port = xhci_hub->rh_port;
174 }
175
176 /* Assign an address to the device */
177 if ((err = address_device(bus, xhci_dev))) {
178 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
179 return err;
180 }
181
182 /* Setup EP0 might already need to issue a transfer. */
183 fibril_mutex_lock(&bus->base.guard);
184 assert(bus->devices_by_slot[xhci_dev->slot_id] == NULL);
185 bus->devices_by_slot[xhci_dev->slot_id] = xhci_dev;
186 fibril_mutex_unlock(&bus->base.guard);
187
188 if ((err = setup_ep0_packet_size(bus->hc, xhci_dev))) {
189 usb_log_error("Failed to setup control endpoint of the new device: %s", str_error(err));
190 goto err_address;
191 }
192
193 /* Read the device descriptor, derive the match ids */
194 if ((err = hcd_device_explore(dev))) {
195 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
196 goto err_address;
197 }
198
199 return EOK;
200
201err_address:
202 // TODO: deaddress device
203 return err;
204}
205
206static int endpoint_unregister(endpoint_t *);
207
208/**
209 * Remove device from XHCI bus. Transition it to the offline state, abort all
210 * ongoing transfers and unregister all of its endpoints.
211 *
212 * Bus callback.
213 *
214 * @param[in] bus XHCI bus, from which the device is removed.
215 * @param[in] dev XHCI device, which is removed from the bus.
216 * @return Error code.
217 */
218static int device_remove(device_t *dev)
219{
220 int err;
221 xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
222 xhci_device_t *xhci_dev = xhci_device_get(dev);
223
224 /* Block creation of new endpoints and transfers. */
225 usb_log_debug2("Device " XHCI_DEV_FMT " going offline.", XHCI_DEV_ARGS(*xhci_dev));
226 fibril_mutex_lock(&dev->guard);
227 dev->online = false;
228 fibril_mutex_unlock(&dev->guard);
229
230 /* Abort running transfers. */
231 usb_log_debug2("Aborting all active transfers to device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*xhci_dev));
232 for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
233 xhci_endpoint_t *ep = xhci_device_get_endpoint(xhci_dev, i);
234 if (!ep)
235 continue;
236
237 endpoint_abort(&ep->base);
238 }
239
240 /* TODO: Figure out how to handle errors here. So far, they are reported and skipped. */
241
242 /* Make DDF (and all drivers) forget about the device. */
243 if ((err = ddf_fun_unbind(dev->fun))) {
244 usb_log_warning("Failed to unbind DDF function of device " XHCI_DEV_FMT ": %s",
245 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
246 }
247
248 /* Disable the slot, dropping all endpoints. */
249 const uint32_t slot_id = xhci_dev->slot_id;
250 if ((err = hc_disable_slot(bus->hc, xhci_dev))) {
251 usb_log_warning("Failed to disable slot of device " XHCI_DEV_FMT ": %s",
252 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
253 }
254
255 bus->devices_by_slot[slot_id] = NULL;
256
257 /* Unregister remaining endpoints, freeing memory. */
258 for (usb_endpoint_t i = 0; i < USB_ENDPOINT_MAX; ++i) {
259 if (!dev->endpoints[i])
260 continue;
261
262 if ((err = endpoint_unregister(dev->endpoints[i]))) {
263 usb_log_warning("Failed to unregister endpoint " XHCI_EP_FMT ": %s",
264 XHCI_EP_ARGS(*xhci_device_get_endpoint(xhci_dev, i)), str_error(err));
265 }
266 }
267
268 /* Destroy DDF device. */
269 /* XXX: Not a good idea, this method should not destroy devices. */
270 hcd_ddf_fun_destroy(dev);
271
272 return EOK;
273}
274
275/**
276 * Reverts things device_offline did, getting the device back up.
277 *
278 * Bus callback.
279 */
280static int device_online(device_t *dev_base)
281{
282 int err;
283
284 xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
285 assert(bus);
286
287 xhci_device_t *dev = xhci_device_get(dev_base);
288 assert(dev);
289
290 /* Transition the device from the Addressed to the Configured state. */
291 if ((err = hc_configure_device(bus->hc, dev->slot_id))) {
292 usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
293 }
294
295 /* Allow creation of new endpoints and transfers. */
296 usb_log_debug2("Device " XHCI_DEV_FMT " going online.", XHCI_DEV_ARGS(*dev));
297 fibril_mutex_lock(&dev_base->guard);
298 dev_base->online = true;
299 fibril_mutex_unlock(&dev_base->guard);
300
301 if ((err = ddf_fun_online(dev_base->fun))) {
302 return err;
303 }
304
305 return EOK;
306}
307
308/**
309 * Make given device offline. Offline the DDF function, tear down all
310 * endpoints, issue Deconfigure Device command to xHC.
311 *
312 * Bus callback.
313 */
314static int device_offline(device_t *dev_base)
315{
316 int err;
317
318 xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
319 assert(bus);
320
321 xhci_device_t *dev = xhci_device_get(dev_base);
322 assert(dev);
323
324 /* Tear down all drivers working with the device. */
325 if ((err = ddf_fun_offline(dev_base->fun))) {
326 return err;
327 }
328
329 /* Block creation of new endpoints and transfers. */
330 usb_log_debug2("Device " XHCI_DEV_FMT " going offline.", XHCI_DEV_ARGS(*dev));
331 fibril_mutex_lock(&dev_base->guard);
332 dev_base->online = false;
333 fibril_mutex_unlock(&dev_base->guard);
334
335 /* We will need the endpoint array later for DS deallocation. */
336 endpoint_t *endpoints[USB_ENDPOINT_MAX];
337 memcpy(endpoints, dev->base.endpoints, sizeof(endpoints));
338
339 for (usb_endpoint_t i = 1; i < USB_ENDPOINT_MAX; ++i) {
340 /* FIXME: Asserting here that the endpoint is not active. If not, EBUSY? */
341 dev->base.endpoints[i] = NULL;
342 }
343
344 /* Issue one HC command to simultaneously drop all endpoints except zero. */
345 if ((err = hc_deconfigure_device(bus->hc, dev->slot_id))) {
346 usb_log_warning("Failed to deconfigure device " XHCI_DEV_FMT ".",
347 XHCI_DEV_ARGS(*dev));
348 }
349
350 /* Tear down TRB ring / PSA. */
351 for (unsigned i = 1; i < ARRAY_SIZE(endpoints); ++i) {
352 if (!endpoints[i])
353 continue;
354
355 xhci_endpoint_free_transfer_ds(xhci_endpoint_get(endpoints[i]));
356 /* Bus reference */
357 endpoint_del_ref(endpoints[i]);
358 }
359
360 return EOK;
361}
362
363/**
364 * Create a new xHCI endpoint structure.
365 *
366 * Bus callback.
367 */
368static endpoint_t *endpoint_create(device_t *dev, const usb_endpoint_descriptors_t *desc)
369{
370 xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t));
371 if (!ep)
372 return NULL;
373
374 if (xhci_endpoint_init(ep, dev, desc)) {
375 free(ep);
376 return NULL;
377 }
378
379 return &ep->base;
380}
381
382/**
383 * Destroy given xHCI endpoint structure.
384 *
385 * Bus callback.
386 */
387static void endpoint_destroy(endpoint_t *ep)
388{
389 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
390
391 xhci_endpoint_fini(xhci_ep);
392 free(xhci_ep);
393}
394
395/**
396 * Register an andpoint to the bus. Allocate its transfer ring(s) and inform
397 * xHC about it.
398 *
399 * Bus callback.
400 */
401static int endpoint_register(endpoint_t *ep_base)
402{
403 int err;
404 xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
405 xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
406
407 xhci_device_t *dev = xhci_device_get(ep_base->device);
408
409 if ((err = xhci_endpoint_alloc_transfer_ds(ep)))
410 return err;
411
412 usb_log_info("Endpoint " XHCI_EP_FMT " registered to XHCI bus.", XHCI_EP_ARGS(*ep));
413
414 xhci_ep_ctx_t ep_ctx;
415 xhci_setup_endpoint_context(ep, &ep_ctx);
416
417 if ((err = hc_add_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep), &ep_ctx)))
418 goto err_prepared;
419
420 return EOK;
421
422err_prepared:
423 xhci_endpoint_free_transfer_ds(ep);
424 return err;
425}
426
427/**
428 * Unregister an endpoint. If the device is still available, inform the xHC
429 * about it. Destroy resources allocated when registering.
430 *
431 * Bus callback.
432 */
433static int endpoint_unregister(endpoint_t *ep_base)
434{
435 int err;
436 xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
437 xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
438 xhci_device_t *dev = xhci_device_get(ep_base->device);
439
440 usb_log_info("Endpoint " XHCI_EP_FMT " unregistered from XHCI bus.", XHCI_EP_ARGS(*ep));
441
442 /* If device slot is still available, drop the endpoint. */
443 if (dev->slot_id) {
444 if ((err = hc_drop_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep)))) {
445 usb_log_error("Failed to drop endpoint " XHCI_EP_FMT ": %s", XHCI_EP_ARGS(*ep), str_error(err));
446 }
447 } else {
448 usb_log_debug("Not going to drop endpoint " XHCI_EP_FMT " because"
449 " the slot has already been disabled.", XHCI_EP_ARGS(*ep));
450 }
451
452 /* Tear down TRB ring / PSA. */
453 xhci_endpoint_free_transfer_ds(ep);
454
455 return EOK;
456}
457
458/**
459 * Schedule a batch for xHC.
460 *
461 * Bus callback.
462 */
463static int batch_schedule(usb_transfer_batch_t *batch)
464{
465 assert(batch);
466 xhci_hc_t *hc = bus_to_hc(endpoint_get_bus(batch->ep));
467
468 if (!batch->target.address) {
469 usb_log_error("Attempted to schedule transfer to address 0.");
470 return EINVAL;
471 }
472
473 return xhci_transfer_schedule(hc, batch);
474}
475
476static const bus_ops_t xhci_bus_ops = {
477 .interrupt = hc_interrupt,
478 .status = hc_status,
479
480 .device_enumerate = device_enumerate,
481 .device_remove = device_remove,
482 .device_online = device_online,
483 .device_offline = device_offline,
484
485 .endpoint_create = endpoint_create,
486 .endpoint_destroy = endpoint_destroy,
487 .endpoint_register = endpoint_register,
488 .endpoint_unregister = endpoint_unregister,
489
490 .batch_schedule = batch_schedule,
491 .batch_create = xhci_transfer_create,
492 .batch_destroy = xhci_transfer_destroy,
493};
494
495/** Initialize XHCI bus.
496 * @param[in] bus Allocated XHCI bus to initialize.
497 * @param[in] hc Associated host controller, which manages the bus.
498 *
499 * @return Error code.
500 */
501int xhci_bus_init(xhci_bus_t *bus, xhci_hc_t *hc)
502{
503 assert(bus);
504
505 bus_init(&bus->base, sizeof(xhci_device_t));
506
507 bus->devices_by_slot = calloc(hc->max_slots, sizeof(xhci_device_t *));
508 if (!bus->devices_by_slot)
509 return ENOMEM;
510
511 bus->hc = hc;
512 bus->base.ops = &xhci_bus_ops;
513 return EOK;
514}
515
516/** Finalize XHCI bus.
517 * @param[in] bus XHCI bus to finalize.
518 */
519void xhci_bus_fini(xhci_bus_t *bus)
520{
521 // FIXME: Ensure there are no more devices?
522 free(bus->devices_by_slot);
523 // FIXME: Something else we forgot?
524}
525
526/**
527 * @}
528 */
Note: See TracBrowser for help on using the repository browser.