source: mainline/uspace/drv/bus/usb/xhci/endpoint.c@ 3f6c94ed

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3f6c94ed was 3f6c94ed, checked in by Michal Staruch <salmelu@…>, 8 years ago

Stubs for stream initialization

Streams should not be initialized always because the drivers may not support it.
Moved streams initialization to on-demand function.
Needs more work, but allows usbmast to initialize again and not crash.

  • Property mode set to 100644
File size: 11.6 KB
Line 
1/*
2 * Copyright (c) 2017 Petr Manek
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
29/** @addtogroup drvusbxhci
30 * @{
31 */
32/** @file
33 * @brief The host controller endpoint management.
34 */
35
36#include <usb/host/utils/malloc32.h>
37#include <usb/host/endpoint.h>
38#include <usb/descriptor.h>
39
40#include <errno.h>
41#include <macros.h>
42
43#include "hc.h"
44#include "bus.h"
45#include "commands.h"
46#include "endpoint.h"
47
48int xhci_endpoint_init(xhci_endpoint_t *xhci_ep, xhci_bus_t *xhci_bus)
49{
50 assert(xhci_ep);
51 assert(xhci_bus);
52
53 bus_t *bus = &xhci_bus->base;
54 endpoint_t *ep = &xhci_ep->base;
55
56 endpoint_init(ep, bus);
57
58 return EOK;
59}
60
61void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
62{
63 assert(xhci_ep);
64
65 // TODO: Something missed?
66}
67
68static int xhci_endpoint_type(xhci_endpoint_t *ep)
69{
70 const bool in = ep->base.direction == USB_DIRECTION_IN;
71
72 switch (ep->base.transfer_type) {
73 case USB_TRANSFER_CONTROL:
74 return EP_TYPE_CONTROL;
75
76 case USB_TRANSFER_ISOCHRONOUS:
77 return in ? EP_TYPE_ISOCH_IN
78 : EP_TYPE_ISOCH_OUT;
79
80 case USB_TRANSFER_BULK:
81 return in ? EP_TYPE_BULK_IN
82 : EP_TYPE_BULK_OUT;
83
84 case USB_TRANSFER_INTERRUPT:
85 return in ? EP_TYPE_INTERRUPT_IN
86 : EP_TYPE_INTERRUPT_OUT;
87 }
88
89 return EP_TYPE_INVALID;
90}
91
92static bool endpoint_using_streams(xhci_endpoint_t *xhci_ep)
93{
94 return xhci_ep->primary_stream_ctx_array != NULL;
95}
96
97static size_t primary_stream_ctx_array_max_size(xhci_endpoint_t *xhci_ep)
98{
99 if (!xhci_ep->max_streams)
100 return 0;
101
102 /* Section 6.2.3, Table 61 */
103 return 1 << (xhci_ep->max_streams + 1);
104}
105
106// static bool primary_stream_ctx_has_secondary_array(xhci_stream_ctx_t *primary_ctx) {
107// /* Section 6.2.4.1, SCT values */
108// return XHCI_STREAM_SCT(*primary_ctx) >= 2;
109// }
110//
111// static size_t secondary_stream_ctx_array_size(xhci_stream_ctx_t *primary_ctx) {
112// if (XHCI_STREAM_SCT(*primary_ctx) < 2) return 0;
113// return 2 << XHCI_STREAM_SCT(*primary_ctx);
114// }
115
116static void initialize_primary_streams(xhci_hc_t *hc, xhci_endpoint_t *xhci_ep, unsigned count) {
117 for (size_t index = 0; index < count; ++index) {
118 // Create trb ring for every primary stream
119 // Store it somewhere
120 // Set the dequeue pointer in stream context structure
121
122 // Set to linear stream array
123 XHCI_STREAM_SCT_SET(xhci_ep->primary_stream_ctx_array[index], 1);
124 }
125}
126
127static void setup_stream_context(xhci_endpoint_t *xhci_ep, xhci_ep_ctx_t *ctx, unsigned pstreams) {
128 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(xhci_ep));
129 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, xhci_ep->base.max_packet_size);
130 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, xhci_ep->max_burst);
131 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
132
133 XHCI_EP_MAX_P_STREAMS_SET(*ctx, pstreams);
134 XHCI_EP_TR_DPTR_SET(*ctx, addr_to_phys(xhci_ep->primary_stream_ctx_array));
135 // TODO: set HID?
136 XHCI_EP_LSA_SET(*ctx, 1);
137}
138
139int xhci_endpoint_request_streams(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *xhci_ep, unsigned count) {
140 if (xhci_ep->base.transfer_type != USB_TRANSFER_BULK
141 || xhci_ep->base.speed != USB_SPEED_SUPER) {
142 usb_log_error("Streams are only supported by superspeed bulk endpoints.");
143 return EINVAL;
144 }
145
146 if (!primary_stream_ctx_array_max_size(xhci_ep)) {
147 usb_log_error("Streams are not supported by endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
148 return EINVAL;
149 }
150
151 uint8_t max_psa_size = 2 << XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PSA_SIZE);
152 if (count > max_psa_size) {
153 // We don't support secondary stream arrays yet, so we just give up for this
154 return ENOTSUP;
155 }
156
157 if (count > (unsigned) (1 << xhci_ep->max_streams)) {
158 usb_log_error("Endpoint " XHCI_EP_FMT " supports only %u streams.",
159 XHCI_EP_ARGS(*xhci_ep), (1 << xhci_ep->max_streams));
160 return EINVAL;
161 }
162
163 if (count <= 1024) {
164 usb_log_debug2("Allocating primary stream context array of size %u for endpoint " XHCI_EP_FMT,
165 count, XHCI_EP_ARGS(*xhci_ep));
166 xhci_ep->primary_stream_ctx_array = malloc32(count * sizeof(xhci_stream_ctx_t));
167 if (!xhci_ep->primary_stream_ctx_array) {
168 return ENOMEM;
169 }
170
171 // FIXME: count should be rounded to nearest power of 2 for xHC, workaround for now
172 count = 1024;
173 // FIXME: pstreams are "log2(count) - 1"
174 const size_t pstreams = 9;
175 xhci_ep->primary_stream_ctx_array_size = count;
176
177 memset(xhci_ep->primary_stream_ctx_array, 0, count * sizeof(xhci_stream_ctx_t));
178 initialize_primary_streams(hc, xhci_ep, count);
179
180 xhci_ep_ctx_t ep_ctx;
181 setup_stream_context(xhci_ep, &ep_ctx, pstreams);
182 return hc_add_endpoint(hc, dev->slot_id, xhci_endpoint_index(xhci_ep), &ep_ctx);
183 }
184 // Complex stuff not supported yet
185 return ENOTSUP;
186}
187
188int xhci_endpoint_alloc_transfer_ds(xhci_endpoint_t *xhci_ep)
189{
190
191 usb_log_debug2("Allocating main transfer ring for endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
192
193 xhci_ep->primary_stream_ctx_array = NULL;
194
195 int err;
196 if ((err = xhci_trb_ring_init(&xhci_ep->ring))) {
197 return err;
198 }
199
200 return EOK;
201}
202
203int xhci_endpoint_free_transfer_ds(xhci_endpoint_t *xhci_ep)
204{
205 if (endpoint_using_streams(xhci_ep)) {
206 usb_log_debug2("Freeing primary stream context array for endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
207
208 // maybe check if LSA, then skip?
209 // for (size_t index = 0; index < primary_stream_ctx_array_size(xhci_ep); ++index) {
210 // xhci_stream_ctx_t *primary_ctx = xhci_ep->primary_stream_ctx_array + index;
211 // if (primary_stream_ctx_has_secondary_array(primary_ctx)) {
212 // // uintptr_t phys = XHCI_STREAM_DEQ_PTR(*primary_ctx);
213 // /* size_t size = */ secondary_stream_ctx_array_size(primary_ctx);
214 // // TODO: somehow map the address to virtual and free the secondary array
215 // }
216 // }
217 for (size_t index = 0; index < xhci_ep->primary_stream_ctx_array_size; ++index) {
218 // FIXME: Get the trb ring associated with stream [index] and fini it
219 }
220 free32(xhci_ep->primary_stream_ctx_array);
221 } else {
222 usb_log_debug2("Freeing main transfer ring for endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
223
224 int err;
225 if ((err = xhci_trb_ring_fini(&xhci_ep->ring))) {
226 return err;
227 }
228 }
229
230 return EOK;
231}
232
233/** See section 4.5.1 of the xHCI spec.
234 */
235uint8_t xhci_endpoint_dci(xhci_endpoint_t *ep)
236{
237 return (2 * ep->base.endpoint) +
238 (ep->base.transfer_type == USB_TRANSFER_CONTROL
239 || ep->base.direction == USB_DIRECTION_IN);
240}
241
242/** Return an index to the endpoint array. The indices are assigned as follows:
243 * 0 EP0 BOTH
244 * 1 EP1 OUT
245 * 2 EP1 IN
246 *
247 * For control endpoints >0, the IN endpoint index is used.
248 *
249 * The index returned must be usually offset by a number of contexts preceding
250 * the endpoint contexts themselves.
251 */
252uint8_t xhci_endpoint_index(xhci_endpoint_t *ep)
253{
254 return xhci_endpoint_dci(ep) - 1;
255}
256
257static void setup_control_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
258{
259 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
260 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
261 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
262 XHCI_EP_MULT_SET(*ctx, ep->mult);
263 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
264 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
265 XHCI_EP_DCS_SET(*ctx, 1);
266}
267
268static void setup_bulk_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
269{
270 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
271 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
272 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
273 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
274
275 XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
276 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
277 XHCI_EP_DCS_SET(*ctx, 1);
278}
279
280static void setup_isoch_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
281{
282 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
283 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
284 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
285 XHCI_EP_MULT_SET(*ctx, ep->mult);
286 XHCI_EP_ERROR_COUNT_SET(*ctx, 0);
287 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
288 XHCI_EP_DCS_SET(*ctx, 1);
289 // TODO: max ESIT payload
290}
291
292static void setup_interrupt_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
293{
294 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
295 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
296 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
297 XHCI_EP_MULT_SET(*ctx, 0);
298 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
299 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
300 XHCI_EP_DCS_SET(*ctx, 1);
301 // TODO: max ESIT payload
302}
303
304typedef void (*setup_ep_ctx_helper)(xhci_endpoint_t *, xhci_ep_ctx_t *);
305
306static const setup_ep_ctx_helper setup_ep_ctx_helpers[] = {
307 [USB_TRANSFER_CONTROL] = setup_control_ep_ctx,
308 [USB_TRANSFER_ISOCHRONOUS] = setup_isoch_ep_ctx,
309 [USB_TRANSFER_BULK] = setup_bulk_ep_ctx,
310 [USB_TRANSFER_INTERRUPT] = setup_interrupt_ep_ctx,
311};
312
313void xhci_setup_endpoint_context(xhci_endpoint_t *ep, xhci_ep_ctx_t *ep_ctx)
314{
315 assert(ep);
316 assert(ep_ctx);
317
318 usb_transfer_type_t tt = ep->base.transfer_type;
319 assert(tt < ARRAY_SIZE(setup_ep_ctx_helpers));
320
321 memset(ep_ctx, 0, sizeof(*ep_ctx));
322 setup_ep_ctx_helpers[tt](ep, ep_ctx);
323}
324
325int xhci_device_add_endpoint(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *ep)
326{
327 assert(dev);
328 assert(ep);
329
330 /* Offline devices don't create new endpoints other than EP0. */
331 if (!dev->online) {
332 return EAGAIN;
333 }
334
335 const usb_endpoint_t ep_num = ep->base.endpoint;
336
337 assert(&dev->base == ep->base.device);
338
339 // TODO Do not fail hard on runtime conditions
340 assert(!dev->endpoints[ep_num]);
341
342 dev->endpoints[ep_num] = ep;
343 ++dev->active_endpoint_count;
344
345 if (ep_num == 0) {
346 /* EP 0 is initialized while setting up the device,
347 * so we must not issue the command now. */
348 return EOK;
349 }
350
351 /* Add endpoint. */
352 xhci_ep_ctx_t ep_ctx;
353 xhci_setup_endpoint_context(ep, &ep_ctx);
354
355 return hc_add_endpoint(hc, dev->slot_id, xhci_endpoint_index(ep), &ep_ctx);
356}
357
358int xhci_device_remove_endpoint(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *ep)
359{
360 assert(&dev->base == ep->base.device);
361 assert(dev->endpoints[ep->base.endpoint]);
362
363 int err = ENOMEM;
364 const usb_endpoint_t ep_num = ep->base.endpoint;
365
366 dev->endpoints[ep->base.endpoint] = NULL;
367 --dev->active_endpoint_count;
368
369 if (ep_num == 0) {
370 /* EP 0 is finalized while releasing the device,
371 * so we must not issue the command now. */
372 return EOK;
373 }
374
375 /* Drop the endpoint. */
376 if ((err = hc_drop_endpoint(hc, dev->slot_id, xhci_endpoint_index(ep)))) {
377 goto err;
378 }
379
380 /* Tear down TRB ring / PSA. */
381 /* FIXME: For some reason, this causes crash at xhci_trb_ring_fini.
382 if ((err = xhci_endpoint_free_transfer_ds(ep))) {
383 goto err_cmd;
384 }
385 */
386
387 return EOK;
388
389err:
390 dev->endpoints[ep_num] = ep;
391 ++dev->active_endpoint_count;
392 return err;
393}
394
395xhci_endpoint_t *xhci_device_get_endpoint(xhci_device_t *dev, usb_endpoint_t ep)
396{
397 return dev->endpoints[ep];
398}
399
400/**
401 * @}
402 */
Note: See TracBrowser for help on using the repository browser.