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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 4793023 was 0eadfd1e, checked in by Petr Manek <petr.manek@…>, 8 years ago

xhci: allocate/free transfer ring internally in endpoint init/fini

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