source: mainline/uspace/drv/bus/usb/xhci/endpoint.c@ 83fb72e

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

usbhost bus: refactor the bus ops

This way, method names better represent the entity it is working with.
Their semantics was shifted a bit.

Regarding the tree of structures:

bus ← device ← endpoint ← batch

Previously, devices were kept in DDF function nodes, and endpoints had
pointer to the bus and device. Now, devices have pointer to bus,
endpoints don't.

Pointer to hcd_t in bus is WIP, and will be removed.

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