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

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

Added memory structure for stream TRB rings. Implemented their initialization. Fixed white space.

  • Property mode set to 100644
File size: 11.1 KB
RevLine 
[c0ec9e7]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
[b7db009]36#include <usb/host/utils/malloc32.h>
[41924f30]37#include <usb/host/endpoint.h>
[9b2f69e]38#include <usb/descriptor.h>
[41924f30]39
[c0ec9e7]40#include <errno.h>
[2cf28b9]41#include <macros.h>
[c0ec9e7]42
[2cf28b9]43#include "hc.h"
[41924f30]44#include "bus.h"
[d7869d7e]45#include "commands.h"
[c0ec9e7]46#include "endpoint.h"
47
[41924f30]48int xhci_endpoint_init(xhci_endpoint_t *xhci_ep, xhci_bus_t *xhci_bus)
[c0ec9e7]49{
[41924f30]50 assert(xhci_ep);
51 assert(xhci_bus);
[176a70a]52
[41924f30]53 bus_t *bus = &xhci_bus->base;
54 endpoint_t *ep = &xhci_ep->base;
[176a70a]55
[41924f30]56 endpoint_init(ep, bus);
57
[89cefe78]58 return EOK;
[c0ec9e7]59}
60
[41924f30]61void xhci_endpoint_fini(xhci_endpoint_t *xhci_ep)
[c0ec9e7]62{
[41924f30]63 assert(xhci_ep);
64
[89cefe78]65 // TODO: Something missed?
66}
67
[3f6c94ed]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)
[81487c4a]93{
[3f6c94ed]94 return xhci_ep->primary_stream_ctx_array != NULL;
[81487c4a]95}
96
[3f6c94ed]97static size_t primary_stream_ctx_array_max_size(xhci_endpoint_t *xhci_ep)
[81487c4a]98{
[3f6c94ed]99 if (!xhci_ep->max_streams)
[81487c4a]100 return 0;
101
102 /* Section 6.2.3, Table 61 */
103 return 1 << (xhci_ep->max_streams + 1);
104}
105
[3f6c94ed]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) {
[ef1a3a8]118 xhci_stream_ctx_t *ctx = &xhci_ep->primary_stream_ctx_array[index];
119 xhci_trb_ring_t *ring = &xhci_ep->primary_stream_rings[index];
[3f6c94ed]120
[ef1a3a8]121 /* Init and register TRB ring for every primary stream */
122 xhci_trb_ring_init(ring);
123 XHCI_STREAM_DEQ_PTR_SET(*ctx, ring->dequeue);
124
125 /* Set to linear stream array */
126 XHCI_STREAM_SCT_SET(*ctx, 1);
[3f6c94ed]127 }
[894f58c]128}
129
[3f6c94ed]130static void setup_stream_context(xhci_endpoint_t *xhci_ep, xhci_ep_ctx_t *ctx, unsigned pstreams) {
131 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(xhci_ep));
132 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, xhci_ep->base.max_packet_size);
133 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, xhci_ep->max_burst);
134 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
135
136 XHCI_EP_MAX_P_STREAMS_SET(*ctx, pstreams);
137 XHCI_EP_TR_DPTR_SET(*ctx, addr_to_phys(xhci_ep->primary_stream_ctx_array));
138 // TODO: set HID?
139 XHCI_EP_LSA_SET(*ctx, 1);
[894f58c]140}
141
[3f6c94ed]142int xhci_endpoint_request_streams(xhci_hc_t *hc, xhci_device_t *dev, xhci_endpoint_t *xhci_ep, unsigned count) {
143 if (xhci_ep->base.transfer_type != USB_TRANSFER_BULK
144 || xhci_ep->base.speed != USB_SPEED_SUPER) {
145 usb_log_error("Streams are only supported by superspeed bulk endpoints.");
146 return EINVAL;
147 }
148
149 if (!primary_stream_ctx_array_max_size(xhci_ep)) {
150 usb_log_error("Streams are not supported by endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
151 return EINVAL;
152 }
153
154 uint8_t max_psa_size = 2 << XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_PSA_SIZE);
155 if (count > max_psa_size) {
[ef1a3a8]156 // FIXME: We don't support secondary stream arrays yet, so we just give up for this
[3f6c94ed]157 return ENOTSUP;
158 }
[81487c4a]159
[3f6c94ed]160 if (count > (unsigned) (1 << xhci_ep->max_streams)) {
161 usb_log_error("Endpoint " XHCI_EP_FMT " supports only %u streams.",
162 XHCI_EP_ARGS(*xhci_ep), (1 << xhci_ep->max_streams));
163 return EINVAL;
164 }
165
166 if (count <= 1024) {
167 usb_log_debug2("Allocating primary stream context array of size %u for endpoint " XHCI_EP_FMT,
168 count, XHCI_EP_ARGS(*xhci_ep));
169 xhci_ep->primary_stream_ctx_array = malloc32(count * sizeof(xhci_stream_ctx_t));
[89cefe78]170 if (!xhci_ep->primary_stream_ctx_array) {
171 return ENOMEM;
172 }
[81487c4a]173
[ef1a3a8]174 xhci_ep->primary_stream_rings = calloc(count, sizeof(xhci_trb_ring_t));
175 if (!xhci_ep->primary_stream_rings) {
176 free32(xhci_ep->primary_stream_ctx_array);
177 return ENOMEM;
178 }
179
[3f6c94ed]180 // FIXME: count should be rounded to nearest power of 2 for xHC, workaround for now
181 count = 1024;
182 // FIXME: pstreams are "log2(count) - 1"
183 const size_t pstreams = 9;
184 xhci_ep->primary_stream_ctx_array_size = count;
[31cca4f3]185
[3f6c94ed]186 memset(xhci_ep->primary_stream_ctx_array, 0, count * sizeof(xhci_stream_ctx_t));
187 initialize_primary_streams(hc, xhci_ep, count);
[31cca4f3]188
[3f6c94ed]189 xhci_ep_ctx_t ep_ctx;
190 setup_stream_context(xhci_ep, &ep_ctx, pstreams);
191 return hc_add_endpoint(hc, dev->slot_id, xhci_endpoint_index(xhci_ep), &ep_ctx);
192 }
[ef1a3a8]193 // FIXME: Complex stuff not supported yet
[3f6c94ed]194 return ENOTSUP;
195}
196
197int xhci_endpoint_alloc_transfer_ds(xhci_endpoint_t *xhci_ep)
198{
[9620a54]199 /* Can't use XHCI_EP_FMT because the endpoint may not have device. */
200 usb_log_debug2("Allocating main transfer ring for endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
[3f6c94ed]201
202 xhci_ep->primary_stream_ctx_array = NULL;
203
204 int err;
205 if ((err = xhci_trb_ring_init(&xhci_ep->ring))) {
206 return err;
[89cefe78]207 }
208
209 return EOK;
210}
211
[9620a54]212void xhci_endpoint_free_transfer_ds(xhci_endpoint_t *xhci_ep)
[89cefe78]213{
[3f6c94ed]214 if (endpoint_using_streams(xhci_ep)) {
[9620a54]215 usb_log_debug2("Freeing primary stream context array of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
[31cca4f3]216
[894f58c]217 // maybe check if LSA, then skip?
[3f6c94ed]218 // for (size_t index = 0; index < primary_stream_ctx_array_size(xhci_ep); ++index) {
219 // xhci_stream_ctx_t *primary_ctx = xhci_ep->primary_stream_ctx_array + index;
220 // if (primary_stream_ctx_has_secondary_array(primary_ctx)) {
221 // // uintptr_t phys = XHCI_STREAM_DEQ_PTR(*primary_ctx);
222 // /* size_t size = */ secondary_stream_ctx_array_size(primary_ctx);
223 // // TODO: somehow map the address to virtual and free the secondary array
224 // }
225 // }
226 for (size_t index = 0; index < xhci_ep->primary_stream_ctx_array_size; ++index) {
227 // FIXME: Get the trb ring associated with stream [index] and fini it
[894f58c]228 }
[89cefe78]229 free32(xhci_ep->primary_stream_ctx_array);
230 } else {
[9620a54]231 usb_log_debug2("Freeing main transfer ring of endpoint " XHCI_EP_FMT, XHCI_EP_ARGS(*xhci_ep));
[31cca4f3]232
[9620a54]233 xhci_trb_ring_fini(&xhci_ep->ring);
[89cefe78]234 }
[c0ec9e7]235}
236
[2b61945]237/** See section 4.5.1 of the xHCI spec.
238 */
239uint8_t xhci_endpoint_dci(xhci_endpoint_t *ep)
[c10daa8]240{
[a5b3de6]241 return (2 * ep->base.endpoint) +
[2b61945]242 (ep->base.transfer_type == USB_TRANSFER_CONTROL
243 || ep->base.direction == USB_DIRECTION_IN);
[9b2f69e]244}
245
[dbf32b1]246/** Return an index to the endpoint array. The indices are assigned as follows:
247 * 0 EP0 BOTH
248 * 1 EP1 OUT
249 * 2 EP1 IN
250 *
251 * For control endpoints >0, the IN endpoint index is used.
[913007f]252 *
[dbf32b1]253 * The index returned must be usually offset by a number of contexts preceding
254 * the endpoint contexts themselves.
255 */
256uint8_t xhci_endpoint_index(xhci_endpoint_t *ep)
[9b2f69e]257{
[2b61945]258 return xhci_endpoint_dci(ep) - 1;
[9b2f69e]259}
260
[89cefe78]261static void setup_control_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
[9b2f69e]262{
263 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
264 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
[0206d35]265 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
266 XHCI_EP_MULT_SET(*ctx, ep->mult);
[9b2f69e]267 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
[89cefe78]268 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
[9b2f69e]269 XHCI_EP_DCS_SET(*ctx, 1);
270}
271
[89cefe78]272static void setup_bulk_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
[9b2f69e]273{
274 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
275 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size);
[0206d35]276 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
[9b2f69e]277 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
278
[3f6c94ed]279 XHCI_EP_MAX_P_STREAMS_SET(*ctx, 0);
280 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
281 XHCI_EP_DCS_SET(*ctx, 1);
[9b2f69e]282}
283
[89cefe78]284static void setup_isoch_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
[9b2f69e]285{
286 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
287 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
[89cefe78]288 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
289 XHCI_EP_MULT_SET(*ctx, ep->mult);
[9b2f69e]290 XHCI_EP_ERROR_COUNT_SET(*ctx, 0);
[89cefe78]291 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
[9b2f69e]292 XHCI_EP_DCS_SET(*ctx, 1);
293 // TODO: max ESIT payload
294}
295
[89cefe78]296static void setup_interrupt_ep_ctx(xhci_endpoint_t *ep, xhci_ep_ctx_t *ctx)
[9b2f69e]297{
298 XHCI_EP_TYPE_SET(*ctx, xhci_endpoint_type(ep));
299 XHCI_EP_MAX_PACKET_SIZE_SET(*ctx, ep->base.max_packet_size & 0x07FF);
[89cefe78]300 XHCI_EP_MAX_BURST_SIZE_SET(*ctx, ep->max_burst);
[9b2f69e]301 XHCI_EP_MULT_SET(*ctx, 0);
302 XHCI_EP_ERROR_COUNT_SET(*ctx, 3);
[89cefe78]303 XHCI_EP_TR_DPTR_SET(*ctx, ep->ring.dequeue);
[9b2f69e]304 XHCI_EP_DCS_SET(*ctx, 1);
305 // TODO: max ESIT payload
[c10daa8]306}
307
[89cefe78]308typedef void (*setup_ep_ctx_helper)(xhci_endpoint_t *, xhci_ep_ctx_t *);
309
310static const setup_ep_ctx_helper setup_ep_ctx_helpers[] = {
311 [USB_TRANSFER_CONTROL] = setup_control_ep_ctx,
312 [USB_TRANSFER_ISOCHRONOUS] = setup_isoch_ep_ctx,
313 [USB_TRANSFER_BULK] = setup_bulk_ep_ctx,
314 [USB_TRANSFER_INTERRUPT] = setup_interrupt_ep_ctx,
315};
316
[0206d35]317void xhci_setup_endpoint_context(xhci_endpoint_t *ep, xhci_ep_ctx_t *ep_ctx)
318{
319 assert(ep);
320 assert(ep_ctx);
321
322 usb_transfer_type_t tt = ep->base.transfer_type;
323 assert(tt < ARRAY_SIZE(setup_ep_ctx_helpers));
324
325 memset(ep_ctx, 0, sizeof(*ep_ctx));
326 setup_ep_ctx_helpers[tt](ep, ep_ctx);
327}
328
[8b8c164]329int xhci_device_add_endpoint(xhci_device_t *dev, xhci_endpoint_t *ep)
[c10daa8]330{
[2b61945]331 assert(dev);
332 assert(ep);
333
[a4e26882]334 /* Offline devices don't create new endpoints other than EP0. */
[8b8c164]335 if (!dev->online && ep->base.endpoint > 0) {
[a4e26882]336 return EAGAIN;
337 }
338
[a5b3de6]339 const usb_endpoint_t ep_num = ep->base.endpoint;
[9b2f69e]340
[8b8c164]341 if (dev->endpoints[ep_num])
342 return EEXIST;
[56db65d]343
[8b8c164]344 /* Device reference */
345 endpoint_add_ref(&ep->base);
346 ep->base.device = &dev->base;
[2b61945]347 dev->endpoints[ep_num] = ep;
[9b2f69e]348
[8b8c164]349 return EOK;
[c10daa8]350}
351
[8b8c164]352void xhci_device_remove_endpoint(xhci_endpoint_t *ep)
[c10daa8]353{
[8b8c164]354 assert(ep);
355 xhci_device_t *dev = xhci_device_get(ep->base.device);
[c10daa8]356
[8b8c164]357 assert(dev->endpoints[ep->base.endpoint]);
[a5b3de6]358 dev->endpoints[ep->base.endpoint] = NULL;
[8b8c164]359 ep->base.device = NULL;
[31cca4f3]360
[8b8c164]361 endpoint_del_ref(&ep->base);
[c10daa8]362}
363
[31cca4f3]364xhci_endpoint_t *xhci_device_get_endpoint(xhci_device_t *dev, usb_endpoint_t ep)
[c10daa8]365{
366 return dev->endpoints[ep];
367}
368
[c0ec9e7]369/**
370 * @}
371 */
Note: See TracBrowser for help on using the repository browser.