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

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

usb: unified logging

Use logger instead of printf. Logger adds newlines automatically.

  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[a5976973]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
[20eaa82]35#include <usb/host/ddf_helpers.h>
[a5976973]36#include <usb/host/endpoint.h>
[20eaa82]37#include <usb/host/hcd.h>
[17c5e62]38#include <usb/descriptor.h>
[a5976973]39#include <usb/debug.h>
40
41#include <assert.h>
42#include <errno.h>
[20eaa82]43#include <str_error.h>
[a5976973]44#include <macros.h>
45#include <stdbool.h>
46
[2cf28b9]47#include "hc.h"
[a5976973]48#include "bus.h"
49#include "endpoint.h"
[5fd9c30]50#include "transfers.h"
[a5976973]51
[0206d35]52
[95a62dc]53/** Initial descriptor used for control endpoint 0 before more configuration is retrieved. */
[9efad54]54static const usb_endpoint_descriptors_t ep0_initial_desc = {
55 .endpoint.max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
[0206d35]56};
57
[9efad54]58static endpoint_t *endpoint_create(device_t *, const usb_endpoint_descriptors_t *);
[0206d35]59
[eb928c4]60/**
61 * Assign address and control endpoint to a new XHCI device. Once this function
62 * successfully returns, the device is online.
[95a62dc]63 *
[eb928c4]64 * @param[in] bus XHCI bus, in which the address is assigned.
65 * @param[in] dev New device to address and configure./e
[95a62dc]66 * @return Error code.
67 */
[6832245]68static int address_device(xhci_bus_t *bus, xhci_device_t *dev)
[0206d35]69{
70 int err;
71
72 /* Enable new slot. */
[6832245]73 if ((err = hc_enable_slot(bus->hc, &dev->slot_id)) != EOK)
[0206d35]74 return err;
[a1732929]75 usb_log_debug2("Obtained slot ID: %u.", dev->slot_id);
[0206d35]76
77 /* Create and configure control endpoint. */
[6832245]78 endpoint_t *ep0_base = endpoint_create(&dev->base, &ep0_initial_desc);
[0206d35]79 if (!ep0_base)
80 goto err_slot;
81
[56257ba]82 /* Bus reference */
[0206d35]83 endpoint_add_ref(ep0_base);
[56257ba]84 dev->base.endpoints[0] = ep0_base;
[0206d35]85
86 xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
87
88 /* Address device */
[6832245]89 if ((err = hc_address_device(bus->hc, dev, ep0)))
[0eadfd1e]90 goto err_added;
[0206d35]91
92 return EOK;
93
[56257ba]94err_added:
95 /* Bus reference */
[0206d35]96 endpoint_del_ref(ep0_base);
[56257ba]97 dev->base.endpoints[0] = NULL;
[0206d35]98err_slot:
[6832245]99 hc_disable_slot(bus->hc, dev);
[0206d35]100 return err;
101}
102
[eb928c4]103/**
104 * Retrieve and set maximum packet size for endpoint zero of a XHCI device.
105 *
[95a62dc]106 * @param[in] hc Host controller, which manages the device.
107 * @param[in] dev Device with operational endpoint zero.
108 * @return Error code.
109 */
[306a36d]110static int setup_ep0_packet_size(xhci_hc_t *hc, xhci_device_t *dev)
111{
112 int err;
113
114 uint16_t max_packet_size;
[32fb6bce]115 if ((err = hcd_get_ep0_max_packet_size(&max_packet_size, (bus_t *) &hc->bus, &dev->base)))
[306a36d]116 return err;
117
[1ed3eb4]118 xhci_endpoint_t *ep0 = xhci_endpoint_get(dev->base.endpoints[0]);
[306a36d]119 assert(ep0);
120
121 if (ep0->base.max_packet_size == max_packet_size)
122 return EOK;
123
124 ep0->base.max_packet_size = max_packet_size;
[bdd8842c]125 ep0->base.max_transfer_size = max_packet_size * ep0->base.packets_per_uframe;
[306a36d]126
127 xhci_ep_ctx_t ep_ctx;
128 xhci_setup_endpoint_context(ep0, &ep_ctx);
129
130 if ((err = hc_update_endpoint(hc, dev->slot_id, 0, &ep_ctx)))
131 return err;
132
133 return EOK;
134}
135
[eb928c4]136/**
137 * Respond to a new device on the XHCI bus. Address it, negotiate packet size
[95a62dc]138 * and retrieve USB descriptors.
[eb928c4]139 *
[95a62dc]140 * @param[in] bus XHCI bus, where the new device emerged.
141 * @param[in] dev XHCI device, which has appeared on the bus.
142 *
143 * @return Error code.
144 */
[eb928c4]145static int device_enumerate(device_t *dev)
[20eaa82]146{
147 int err;
[eb928c4]148 xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
[2b61945]149 xhci_device_t *xhci_dev = xhci_device_get(dev);
[20eaa82]150
[2cf28b9]151 /* Calculate route string */
152 xhci_device_t *xhci_hub = xhci_device_get(dev->hub);
153 xhci_dev->tier = xhci_hub->tier + 1;
154 xhci_dev->route_str = xhci_hub->route_str;
155
156 /* Roothub port is not part of the route string */
157 if (xhci_dev->tier >= 2) {
158 const unsigned offset = 4 * (xhci_dev->tier - 2);
159 xhci_dev->route_str |= (dev->port & 0xf) << offset;
[62558202]160 xhci_dev->rh_port = xhci_hub->rh_port;
[2cf28b9]161 }
162
[ba2e17f]163 int retries = 3;
164 do {
165 /* Assign an address to the device */
166 err = address_device(bus, xhci_dev);
167 } while (err == ESTALL && --retries > 0);
168
169 if (err) {
[20eaa82]170 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
171 return err;
172 }
173
[53db806]174 /* Setup EP0 might already need to issue a transfer. */
175 fibril_mutex_lock(&bus->base.guard);
176 assert(bus->devices_by_slot[xhci_dev->slot_id] == NULL);
177 bus->devices_by_slot[xhci_dev->slot_id] = xhci_dev;
178 fibril_mutex_unlock(&bus->base.guard);
179
[6832245]180 if ((err = setup_ep0_packet_size(bus->hc, xhci_dev))) {
[306a36d]181 usb_log_error("Failed to setup control endpoint of the new device: %s", str_error(err));
182 goto err_address;
183 }
[0206d35]184
[20eaa82]185 /* Read the device descriptor, derive the match ids */
[32fb6bce]186 if ((err = hcd_device_explore(dev))) {
[20eaa82]187 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
[327f147]188 goto err_address;
[20eaa82]189 }
190
191 return EOK;
[2b61945]192
193err_address:
[10cd715]194 // TODO: deaddress device
[2b61945]195 return err;
[20eaa82]196}
197
[eb928c4]198/**
199 * Remove device from XHCI bus. Transition it to the offline state, abort all
[95a62dc]200 * ongoing transfers and unregister all of its endpoints.
[eb928c4]201 *
202 * Bus callback.
203 *
[95a62dc]204 * @param[in] bus XHCI bus, from which the device is removed.
205 * @param[in] dev XHCI device, which is removed from the bus.
206 * @return Error code.
207 */
[9848c77]208static void device_gone(device_t *dev)
[20eaa82]209{
[40a3bfa]210 int err;
[eb928c4]211 xhci_bus_t *bus = bus_to_xhci_bus(dev->bus);
[2b61945]212 xhci_device_t *xhci_dev = xhci_device_get(dev);
213
[9620a54]214 /* Disable the slot, dropping all endpoints. */
215 const uint32_t slot_id = xhci_dev->slot_id;
[6832245]216 if ((err = hc_disable_slot(bus->hc, xhci_dev))) {
[9620a54]217 usb_log_warning("Failed to disable slot of device " XHCI_DEV_FMT ": %s",
218 XHCI_DEV_ARGS(*xhci_dev), str_error(err));
219 }
220
221 bus->devices_by_slot[slot_id] = NULL;
[20eaa82]222}
223
[eb928c4]224/**
225 * Reverts things device_offline did, getting the device back up.
226 *
227 * Bus callback.
228 */
[6832245]229static int device_online(device_t *dev_base)
[d37514e]230{
231 int err;
232
[6832245]233 xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
[d37514e]234 assert(bus);
235
236 xhci_device_t *dev = xhci_device_get(dev_base);
237 assert(dev);
238
[6b2930b]239 /* Transition the device from the Addressed to the Configured state. */
[6832245]240 if ((err = hc_configure_device(bus->hc, dev->slot_id))) {
[9620a54]241 usb_log_warning("Failed to configure device " XHCI_DEV_FMT ".", XHCI_DEV_ARGS(*dev));
[d37514e]242 return err;
243 }
244
245 return EOK;
246}
247
[eb928c4]248/**
249 * Make given device offline. Offline the DDF function, tear down all
250 * endpoints, issue Deconfigure Device command to xHC.
251 *
252 * Bus callback.
253 */
[0892663a]254static void device_offline(device_t *dev_base)
[d37514e]255{
256 int err;
257
[6832245]258 xhci_bus_t *bus = bus_to_xhci_bus(dev_base->bus);
[d37514e]259 assert(bus);
260
261 xhci_device_t *dev = xhci_device_get(dev_base);
262 assert(dev);
263
[6b2930b]264 /* Issue one HC command to simultaneously drop all endpoints except zero. */
[6832245]265 if ((err = hc_deconfigure_device(bus->hc, dev->slot_id))) {
[9620a54]266 usb_log_warning("Failed to deconfigure device " XHCI_DEV_FMT ".",
267 XHCI_DEV_ARGS(*dev));
[6b2930b]268 }
[d37514e]269}
270
[eb928c4]271/**
272 * Create a new xHCI endpoint structure.
273 *
274 * Bus callback.
275 */
[9efad54]276static endpoint_t *endpoint_create(device_t *dev, const usb_endpoint_descriptors_t *desc)
[a5976973]277{
[17c5e62]278 const usb_transfer_type_t type = USB_ED_GET_TRANSFER_TYPE(desc->endpoint);
279
280 xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t)
281 + (type == USB_TRANSFER_ISOCHRONOUS) * sizeof(*ep->isoch));
[a5976973]282 if (!ep)
283 return NULL;
284
[6832245]285 if (xhci_endpoint_init(ep, dev, desc)) {
[a5976973]286 free(ep);
287 return NULL;
288 }
289
290 return &ep->base;
291}
292
[eb928c4]293/**
294 * Destroy given xHCI endpoint structure.
295 *
296 * Bus callback.
297 */
[6832245]298static void endpoint_destroy(endpoint_t *ep)
[a5976973]299{
300 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
301
302 xhci_endpoint_fini(xhci_ep);
303 free(xhci_ep);
304}
305
[eb928c4]306/**
[0892663a]307 * Register an andpoint to the xHC.
[eb928c4]308 *
309 * Bus callback.
310 */
[6832245]311static int endpoint_register(endpoint_t *ep_base)
[add878aa]312{
[0206d35]313 int err;
[6832245]314 xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
[8b8c164]315 xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
[6832245]316 xhci_device_t *dev = xhci_device_get(ep_base->device);
[56db65d]317
[8b8c164]318 xhci_ep_ctx_t ep_ctx;
319 xhci_setup_endpoint_context(ep, &ep_ctx);
320
321 if ((err = hc_add_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep), &ep_ctx)))
[0eadfd1e]322 return err;
[8b8c164]323
324 return EOK;
[add878aa]325}
326
[30fc56f]327/**
328 * Abort a transfer on an endpoint.
329 */
330static int endpoint_abort(endpoint_t *ep)
331{
332 xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep));
333 xhci_device_t *dev = xhci_device_get(ep->device);
334
335 usb_transfer_batch_t *batch = NULL;
336 fibril_mutex_lock(&ep->guard);
337 if (ep->active_batch) {
338 if (dev->slot_id) {
339 const int err = hc_stop_endpoint(bus->hc, dev->slot_id, xhci_endpoint_dci(xhci_endpoint_get(ep)));
340 if (err) {
341 usb_log_warning("Failed to stop endpoint %u of device " XHCI_DEV_FMT ": %s",
342 ep->endpoint, XHCI_DEV_ARGS(*dev), str_error(err));
343 }
344
345 endpoint_wait_timeout_locked(ep, 2000);
346 }
347
348 batch = ep->active_batch;
349 if (batch) {
350 endpoint_deactivate_locked(ep);
351 }
352 }
353 fibril_mutex_unlock(&ep->guard);
354
355 if (batch) {
356 batch->error = EINTR;
357 batch->transfered_size = 0;
358 usb_transfer_batch_finish(batch);
359 }
360 return EOK;
361}
362
[eb928c4]363/**
364 * Unregister an endpoint. If the device is still available, inform the xHC
[0892663a]365 * about it.
[eb928c4]366 *
367 * Bus callback.
368 */
[bad4a05]369static void endpoint_unregister(endpoint_t *ep_base)
[add878aa]370{
[8b8c164]371 int err;
[6832245]372 xhci_bus_t *bus = bus_to_xhci_bus(endpoint_get_bus(ep_base));
[8b8c164]373 xhci_endpoint_t *ep = xhci_endpoint_get(ep_base);
374 xhci_device_t *dev = xhci_device_get(ep_base->device);
[a8435eb5]375
[30fc56f]376 endpoint_abort(ep_base);
377
[9620a54]378 /* If device slot is still available, drop the endpoint. */
379 if (dev->slot_id) {
[30fc56f]380
[9620a54]381 if ((err = hc_drop_endpoint(bus->hc, dev->slot_id, xhci_endpoint_index(ep)))) {
382 usb_log_error("Failed to drop endpoint " XHCI_EP_FMT ": %s", XHCI_EP_ARGS(*ep), str_error(err));
383 }
384 } else {
385 usb_log_debug("Not going to drop endpoint " XHCI_EP_FMT " because"
386 " the slot has already been disabled.", XHCI_EP_ARGS(*ep));
[8b8c164]387 }
[add878aa]388}
389
[eb928c4]390/**
391 * Schedule a batch for xHC.
392 *
393 * Bus callback.
394 */
395static int batch_schedule(usb_transfer_batch_t *batch)
[5fd9c30]396{
[eb928c4]397 assert(batch);
398 xhci_hc_t *hc = bus_to_hc(endpoint_get_bus(batch->ep));
[5fd9c30]399
[eb928c4]400 if (!batch->target.address) {
401 usb_log_error("Attempted to schedule transfer to address 0.");
402 return EINVAL;
403 }
404
405 return xhci_transfer_schedule(hc, batch);
[5fd9c30]406}
407
[a5976973]408static const bus_ops_t xhci_bus_ops = {
[32fb6bce]409 .interrupt = hc_interrupt,
410 .status = hc_status,
[eb928c4]411
412 .device_enumerate = device_enumerate,
[9848c77]413 .device_gone = device_gone,
[eb928c4]414 .device_online = device_online,
415 .device_offline = device_offline,
416
417 .endpoint_create = endpoint_create,
418 .endpoint_destroy = endpoint_destroy,
419 .endpoint_register = endpoint_register,
420 .endpoint_unregister = endpoint_unregister,
421
422 .batch_schedule = batch_schedule,
423 .batch_create = xhci_transfer_create,
424 .batch_destroy = xhci_transfer_destroy,
[a5976973]425};
426
[95a62dc]427/** Initialize XHCI bus.
428 * @param[in] bus Allocated XHCI bus to initialize.
429 * @param[in] hc Associated host controller, which manages the bus.
430 *
431 * @return Error code.
432 */
[2b61945]433int xhci_bus_init(xhci_bus_t *bus, xhci_hc_t *hc)
[a5976973]434{
435 assert(bus);
436
[32fb6bce]437 bus_init(&bus->base, sizeof(xhci_device_t));
[2b61945]438
439 bus->devices_by_slot = calloc(hc->max_slots, sizeof(xhci_device_t *));
440 if (!bus->devices_by_slot)
441 return ENOMEM;
[a5976973]442
[25251bb]443 bus->hc = hc;
[6832245]444 bus->base.ops = &xhci_bus_ops;
[a5976973]445 return EOK;
446}
[a8435eb5]447
[95a62dc]448/** Finalize XHCI bus.
449 * @param[in] bus XHCI bus to finalize.
450 */
[a8435eb5]451void xhci_bus_fini(xhci_bus_t *bus)
452{
[eb928c4]453 // FIXME: Ensure there are no more devices?
454 free(bus->devices_by_slot);
[95a62dc]455 // FIXME: Something else we forgot?
[a8435eb5]456}
[20eaa82]457
[a5976973]458/**
459 * @}
460 */
Note: See TracBrowser for help on using the repository browser.