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

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

xhci: connecting devices deeper than to roothub

It still does not work, because the address command fails, but there should not be any fundamental problem.

  • Property mode set to 100644
File size: 10.4 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/utils/malloc32.h>
36#include <usb/host/ddf_helpers.h>
37#include <usb/host/endpoint.h>
38#include <usb/host/hcd.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/* FIXME Are these really static? Older HCs fetch it from descriptor. */
54/* FIXME Add USB3 options, if applicable. */
55static const usb_endpoint_desc_t ep0_desc = {
56 .endpoint_no = 0,
57 .direction = USB_DIRECTION_BOTH,
58 .transfer_type = USB_TRANSFER_CONTROL,
59 .max_packet_size = CTRL_PIPE_MIN_PACKET_SIZE,
60 .packets = 1,
61};
62
63static int prepare_endpoint(xhci_endpoint_t *ep, const usb_endpoint_desc_t *desc)
64{
65 /* Extract information from endpoint_desc */
66 ep->base.endpoint = desc->endpoint_no;
67 ep->base.direction = desc->direction;
68 ep->base.transfer_type = desc->transfer_type;
69 ep->base.max_packet_size = desc->max_packet_size;
70 ep->base.packets = desc->packets;
71 ep->max_streams = desc->usb3.max_streams;
72 ep->max_burst = desc->usb3.max_burst;
73 // TODO add this property to usb_endpoint_desc_t and fetch it from ss companion desc
74 ep->mult = 0;
75
76 return xhci_endpoint_alloc_transfer_ds(ep);
77}
78
79static endpoint_t *create_endpoint(bus_t *base);
80
81static int address_device(xhci_hc_t *hc, xhci_device_t *dev)
82{
83 int err;
84
85 /* Enable new slot. */
86 if ((err = hc_enable_slot(hc, &dev->slot_id)) != EOK)
87 return err;
88 usb_log_debug2("Obtained slot ID: %u.\n", dev->slot_id);
89
90 /* Create and configure control endpoint. */
91 endpoint_t *ep0_base = create_endpoint(&hc->bus.base);
92 if (!ep0_base)
93 goto err_slot;
94
95 /* Temporary reference */
96 endpoint_add_ref(ep0_base);
97
98 ep0_base->device = &dev->base;
99 xhci_endpoint_t *ep0 = xhci_endpoint_get(ep0_base);
100
101 if ((err = prepare_endpoint(ep0, &ep0_desc)))
102 goto err_ep;
103
104 /* Address device */
105 if ((err = hc_address_device(hc, dev, ep0)))
106 goto err_prepared_ep;
107
108 /* Register EP0, passing Temporary reference */
109 dev->endpoints[0] = ep0;
110
111 return EOK;
112
113err_prepared_ep:
114 xhci_endpoint_free_transfer_ds(ep0);
115err_ep:
116 endpoint_del_ref(ep0_base);
117err_slot:
118 hc_disable_slot(hc, dev->slot_id);
119 return err;
120}
121
122int xhci_bus_enumerate_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
123{
124 int err;
125 xhci_device_t *xhci_dev = xhci_device_get(dev);
126
127 /* Manage TT */
128 if (dev->hub->speed == USB_SPEED_HIGH && usb_speed_is_11(dev->speed)) {
129 /* For LS devices under HS hub */
130 /* TODO: How about SS hubs? */
131 dev->tt.address = dev->hub->address;
132 dev->tt.port = dev->port;
133 }
134 else {
135 /* Inherit hub's TT */
136 dev->tt = dev->hub->tt;
137 }
138
139 /* Calculate route string */
140 xhci_device_t *xhci_hub = xhci_device_get(dev->hub);
141 xhci_dev->tier = xhci_hub->tier + 1;
142 xhci_dev->route_str = xhci_hub->route_str;
143
144 /* Roothub port is not part of the route string */
145 if (xhci_dev->tier >= 2) {
146 const unsigned offset = 4 * (xhci_dev->tier - 2);
147 xhci_dev->route_str |= (dev->port & 0xf) << offset;
148 }
149
150 fibril_mutex_lock(&bus->base.guard);
151 /* Assign an address to the device */
152 if ((err = address_device(hc, xhci_dev))) {
153 usb_log_error("Failed to setup address of the new device: %s", str_error(err));
154 return err;
155 }
156
157 // TODO: Fetch descriptor of EP0 and reconfigure it accordingly
158 assert(xhci_dev->endpoints[0]);
159
160 assert(bus->devices_by_slot[xhci_dev->slot_id] == NULL);
161 bus->devices_by_slot[xhci_dev->slot_id] = xhci_dev;
162 fibril_mutex_unlock(&bus->base.guard);
163
164 /* Read the device descriptor, derive the match ids */
165 if ((err = hcd_ddf_device_explore(hc->hcd, dev))) {
166 usb_log_error("Device(%d): Failed to explore device: %s", dev->address, str_error(err));
167 goto err_address;
168 }
169
170 return EOK;
171
172err_address:
173 bus_release_address(&bus->base, dev->address);
174 return err;
175}
176
177static int unregister_endpoint(bus_t *, endpoint_t *);
178
179int xhci_bus_remove_device(xhci_bus_t *bus, xhci_hc_t *hc, device_t *dev)
180{
181 xhci_device_t *xhci_dev = xhci_device_get(dev);
182
183 /* Unregister remaining endpoints. */
184 for (unsigned i = 0; i < ARRAY_SIZE(xhci_dev->endpoints); ++i) {
185 if (!xhci_dev->endpoints[i])
186 continue;
187
188 const int err = unregister_endpoint(&bus->base, &xhci_dev->endpoints[i]->base);
189 if (err)
190 usb_log_warning("Failed to unregister EP (%u:%u): %s", dev->address, i, str_error(err));
191 }
192
193 // XXX: Ugly here. Move to device_destroy at endpoint.c?
194 free32(xhci_dev->dev_ctx);
195 hc->dcbaa[xhci_dev->slot_id] = 0;
196 return EOK;
197}
198
199/** Ops receive generic bus_t pointer. */
200static inline xhci_bus_t *bus_to_xhci_bus(bus_t *bus_base)
201{
202 assert(bus_base);
203 return (xhci_bus_t *) bus_base;
204}
205
206static int enumerate_device(bus_t *bus_base, hcd_t *hcd, device_t *dev)
207{
208 xhci_hc_t *hc = hcd_get_driver_data(hcd);
209 assert(hc);
210
211 xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
212 assert(bus);
213
214 return xhci_bus_enumerate_device(bus, hc, dev);
215}
216
217static int remove_device(bus_t *bus_base, hcd_t *hcd, device_t *dev)
218{
219 xhci_hc_t *hc = hcd_get_driver_data(hcd);
220 assert(hc);
221
222 xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
223 assert(bus);
224
225 return xhci_bus_remove_device(bus, hc, dev);
226}
227
228static endpoint_t *create_endpoint(bus_t *base)
229{
230 xhci_bus_t *bus = bus_to_xhci_bus(base);
231
232 xhci_endpoint_t *ep = calloc(1, sizeof(xhci_endpoint_t));
233 if (!ep)
234 return NULL;
235
236 if (xhci_endpoint_init(ep, bus)) {
237 free(ep);
238 return NULL;
239 }
240
241 return &ep->base;
242}
243
244static void destroy_endpoint(endpoint_t *ep)
245{
246 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
247
248 xhci_endpoint_fini(xhci_ep);
249 free(xhci_ep);
250}
251
252static int register_endpoint(bus_t *bus_base, endpoint_t *ep, const usb_endpoint_desc_t *desc)
253{
254 int err;
255 xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
256 assert(bus);
257
258 assert(ep->device);
259
260 xhci_device_t *xhci_dev = xhci_device_get(ep->device);
261 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
262
263 if ((err = prepare_endpoint(xhci_ep, desc)))
264 return err;
265
266 usb_log_info("Endpoint(%d:%d) registered to XHCI bus.", ep->device->address, ep->endpoint);
267 return xhci_device_add_endpoint(xhci_dev, xhci_ep);
268}
269
270static int unregister_endpoint(bus_t *bus_base, endpoint_t *ep)
271{
272 xhci_bus_t *bus = bus_to_xhci_bus(bus_base);
273 assert(bus);
274
275 usb_log_info("Endpoint(%d:%d) unregistered from XHCI bus.", ep->device->address, ep->endpoint);
276
277 xhci_device_t *xhci_dev = xhci_device_get(ep->device);
278 xhci_endpoint_t *xhci_ep = xhci_endpoint_get(ep);
279 const int res = xhci_device_remove_endpoint(xhci_dev, xhci_ep);
280 if (res != EOK)
281 return res;
282
283 return EOK;
284}
285
286static endpoint_t* find_endpoint(bus_t *bus_base, device_t *dev_base, usb_target_t target, usb_direction_t direction)
287{
288 xhci_device_t *dev = xhci_device_get(dev_base);
289
290 xhci_endpoint_t *ep = xhci_device_get_endpoint(dev, target.endpoint);
291 if (!ep)
292 return NULL;
293
294 return &ep->base;
295}
296
297static int reset_toggle(bus_t *bus_base, usb_target_t target, bool all)
298{
299 // TODO: Implement me!
300 return ENOTSUP;
301}
302
303static size_t count_bw(endpoint_t *ep, size_t size)
304{
305 // TODO: Implement me!
306 return 0;
307}
308
309/* Endpoint ops, optional (have generic fallback) */
310static bool endpoint_get_toggle(endpoint_t *ep)
311{
312 // TODO: Implement me!
313 return ENOTSUP;
314}
315
316static void endpoint_set_toggle(endpoint_t *ep, bool toggle)
317{
318 // TODO: Implement me!
319}
320
321static int request_address(bus_t *bus_base, usb_address_t *addr, bool strict, usb_speed_t speed)
322{
323 assert(addr);
324
325 if (*addr != USB_ADDRESS_DEFAULT)
326 /* xHCI does not allow software to assign addresses. */
327 return ENOTSUP;
328
329 assert(strict);
330
331 xhci_bus_t *xhci_bus = bus_to_xhci_bus(bus_base);
332
333 if (xhci_bus->default_address_speed != USB_SPEED_MAX)
334 /* Already allocated */
335 return ENOENT;
336
337 xhci_bus->default_address_speed = speed;
338 return EOK;
339}
340
341static int release_address(bus_t *bus_base, usb_address_t addr)
342{
343 if (addr != USB_ADDRESS_DEFAULT)
344 return ENOTSUP;
345
346 xhci_bus_t *xhci_bus = bus_to_xhci_bus(bus_base);
347
348 xhci_bus->default_address_speed = USB_SPEED_MAX;
349 return EOK;
350}
351
352static usb_transfer_batch_t *create_batch(bus_t *bus, endpoint_t *ep)
353{
354 xhci_transfer_t *transfer = xhci_transfer_create(ep);
355 return &transfer->batch;
356}
357
358static void destroy_batch(usb_transfer_batch_t *batch)
359{
360 xhci_transfer_destroy(xhci_transfer_from_batch(batch));
361}
362
363static const bus_ops_t xhci_bus_ops = {
364 .enumerate_device = enumerate_device,
365 .remove_device = remove_device,
366
367 .create_endpoint = create_endpoint,
368 .destroy_endpoint = destroy_endpoint,
369
370 .register_endpoint = register_endpoint,
371 .unregister_endpoint = unregister_endpoint,
372 .find_endpoint = find_endpoint,
373
374 .request_address = request_address,
375 .release_address = release_address,
376 .reset_toggle = reset_toggle,
377
378 .count_bw = count_bw,
379
380 .endpoint_get_toggle = endpoint_get_toggle,
381 .endpoint_set_toggle = endpoint_set_toggle,
382
383 .create_batch = create_batch,
384 .destroy_batch = destroy_batch,
385};
386
387int xhci_bus_init(xhci_bus_t *bus, xhci_hc_t *hc)
388{
389 assert(bus);
390
391 bus_init(&bus->base, sizeof(xhci_device_t));
392
393 bus->devices_by_slot = calloc(hc->max_slots, sizeof(xhci_device_t *));
394 if (!bus->devices_by_slot)
395 return ENOMEM;
396
397 bus->base.ops = xhci_bus_ops;
398 bus->default_address_speed = USB_SPEED_MAX;
399 return EOK;
400}
401
402void xhci_bus_fini(xhci_bus_t *bus)
403{
404
405}
406
407/**
408 * @}
409 */
Note: See TracBrowser for help on using the repository browser.