blob: 33009bc6377ca276439e8111d50aa44f8c61e2ab [file] [log] [blame]
Patrick Georgi6615ef32010-08-13 09:18:58 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2010 Patrick Georgi
Nico Huber90292652013-06-13 14:37:15 +02005 * Copyright (C) 2013 secunet Security Networks AG
Patrick Georgi6615ef32010-08-13 09:18:58 +00006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
Nico Huber90292652013-06-13 14:37:15 +020031//#define XHCI_SPEW_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +000032
Nico Huber90292652013-06-13 14:37:15 +020033#include <inttypes.h>
Patrick Georgi6615ef32010-08-13 09:18:58 +000034#include <arch/virtual.h>
Patrick Georgi6615ef32010-08-13 09:18:58 +000035#include "xhci_private.h"
Nico Huber90292652013-06-13 14:37:15 +020036#include "xhci.h"
Patrick Georgi6615ef32010-08-13 09:18:58 +000037
38static void xhci_start (hci_t *controller);
39static void xhci_stop (hci_t *controller);
40static void xhci_reset (hci_t *controller);
Nico Huber90292652013-06-13 14:37:15 +020041static void xhci_reinit (hci_t *controller);
Patrick Georgi6615ef32010-08-13 09:18:58 +000042static void xhci_shutdown (hci_t *controller);
43static int xhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize);
44static int xhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq,
45 int dalen, u8 *data);
46static void* xhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming);
47static void xhci_destroy_intr_queue (endpoint_t *ep, void *queue);
48static u8* xhci_poll_intr_queue (void *queue);
49
Nico Huber90292652013-06-13 14:37:15 +020050/*
51 * Some structures must not cross page boundaries. To get this,
52 * we align them by their size (or the next greater power of 2).
53 */
54void *
55xhci_align(const size_t min_align, const size_t size)
Patrick Georgi6615ef32010-08-13 09:18:58 +000056{
Nico Huber90292652013-06-13 14:37:15 +020057 size_t align;
58 if (!(size & (size - 1)))
59 align = size; /* It's a power of 2 */
60 else
61 align = 1 << ((sizeof(unsigned) << 3) - __builtin_clz(size));
62 if (align < min_align)
63 align = min_align;
64 xhci_spew("Aligning %zu to %zu\n", size, align);
Julius Werner1f864342013-09-03 17:15:31 -070065 return dma_memalign(align, size);
Nico Huber90292652013-06-13 14:37:15 +020066}
67
68void
69xhci_clear_trb(trb_t *const trb, const int pcs)
70{
71 trb->ptr_low = 0;
72 trb->ptr_high = 0;
73 trb->status = 0;
74 trb->control = !pcs;
75}
76
77void
78xhci_init_cycle_ring(transfer_ring_t *const tr, const size_t ring_size)
79{
80 memset((void *)tr->ring, 0, ring_size * sizeof(*tr->ring));
81 TRB_SET(TT, &tr->ring[ring_size - 1], TRB_LINK);
82 TRB_SET(TC, &tr->ring[ring_size - 1], 1);
83 /* only one segment that points to itself */
84 tr->ring[ring_size - 1].ptr_low = virt_to_phys(tr->ring);
85
86 tr->pcs = 1;
87 tr->cur = tr->ring;
88}
89
90/* On Panther Point: switch ports shared with EHCI to xHCI */
Marc Jones86127c72014-12-29 22:07:04 -070091#ifdef CONFIG_LP_USB_PCI
Nico Huber90292652013-06-13 14:37:15 +020092static void
93xhci_switch_ppt_ports(pcidev_t addr)
94{
95 if (pci_read_config32(addr, 0x00) == 0x1e318086) {
96 u32 reg32 = pci_read_config32(addr, 0xdc) & 0xf;
97 xhci_debug("Ports capable of SuperSpeed: 0x%"PRIx32"\n", reg32);
98
99 /* For now, do not enable SuperSpeed on any ports */
100 //pci_write_config32(addr, 0xd8, reg32);
101 pci_write_config32(addr, 0xd8, 0x00000000);
102 reg32 = pci_read_config32(addr, 0xd8) & 0xf;
103 xhci_debug("Configured for SuperSpeed: 0x%"PRIx32"\n", reg32);
104
105 reg32 = pci_read_config32(addr, 0xd4) & 0xf;
106 xhci_debug("Trying to switch over: 0x%"PRIx32"\n", reg32);
107
108 pci_write_config32(addr, 0xd0, reg32);
109 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
110 xhci_debug("Actually switched over: 0x%"PRIx32"\n", reg32);
111 }
112}
Marc Jones86127c72014-12-29 22:07:04 -0700113#endif
Nico Huber90292652013-06-13 14:37:15 +0200114
Nico Huberc3714422013-07-19 14:03:47 +0200115/* On Panther Point: switch all ports back to EHCI */
116static void
117xhci_switchback_ppt_ports(pcidev_t addr)
118{
119 if (pci_read_config32(addr, 0x00) == 0x1e318086) {
120 u32 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
121 xhci_debug("Switching ports back: 0x%"PRIx32"\n", reg32);
122 pci_write_config32(addr, 0xd0, 0x00000000);
123 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
124 xhci_debug("Still switched to xHCI: 0x%"PRIx32"\n", reg32);
125 }
126}
127
Nico Huber90292652013-06-13 14:37:15 +0200128static long
129xhci_handshake(volatile u32 *const reg, u32 mask, u32 wait_for, long timeout_us)
130{
131 while ((*reg & mask) != wait_for && timeout_us--) udelay(1);
132 return timeout_us;
133}
134
135static int
136xhci_wait_ready(xhci_t *const xhci)
137{
138 xhci_debug("Waiting for controller to be ready... ");
139 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_CNR, 0, 100000L)) {
140 usb_debug("timeout!\n");
141 return -1;
142 }
143 usb_debug("ok.\n");
144 return 0;
145}
146
147hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200148xhci_init (unsigned long physical_bar)
Nico Huber90292652013-06-13 14:37:15 +0200149{
150 int i;
151
152 /* First, allocate and initialize static controller structures */
153
154 hci_t *const controller = new_controller();
Nico Huber90292652013-06-13 14:37:15 +0200155 controller->type = XHCI;
156 controller->start = xhci_start;
157 controller->stop = xhci_stop;
158 controller->reset = xhci_reset;
159 controller->init = xhci_reinit;
160 controller->shutdown = xhci_shutdown;
161 controller->bulk = xhci_bulk;
162 controller->control = xhci_control;
163 controller->set_address = xhci_set_address;
164 controller->finish_device_config= xhci_finish_device_config;
165 controller->destroy_device = xhci_destroy_dev;
166 controller->create_intr_queue = xhci_create_intr_queue;
167 controller->destroy_intr_queue = xhci_destroy_intr_queue;
168 controller->poll_intr_queue = xhci_poll_intr_queue;
Nico Huber8b8e9632014-07-07 17:20:53 +0200169 controller->pcidev = 0;
Nico Huber90292652013-06-13 14:37:15 +0200170
Furquan Shaikh79a591f2014-05-13 13:47:32 -0700171 controller->reg_base = (uintptr_t)physical_bar;
Julius Werner7234d602014-04-08 12:54:25 -0700172 controller->instance = xzalloc(sizeof(xhci_t));
Nico Huber90292652013-06-13 14:37:15 +0200173 xhci_t *const xhci = (xhci_t *)controller->instance;
Nico Huber90292652013-06-13 14:37:15 +0200174
175 init_device_entry(controller, 0);
176 xhci->roothub = controller->devices[0];
177 xhci->cr.ring = xhci_align(64, COMMAND_RING_SIZE * sizeof(trb_t));
178 xhci->er.ring = xhci_align(64, EVENT_RING_SIZE * sizeof(trb_t));
179 xhci->ev_ring_table = xhci_align(64, sizeof(erst_entry_t));
180 if (!xhci->roothub || !xhci->cr.ring ||
181 !xhci->er.ring || !xhci->ev_ring_table) {
182 xhci_debug("Out of memory\n");
183 goto _free_xhci;
184 }
185
Nico Huber5b9e6f12014-07-10 12:56:34 +0200186 xhci->capreg = phys_to_virt(physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200187 xhci->opreg = ((void *)xhci->capreg) + xhci->capreg->caplength;
188 xhci->hcrreg = ((void *)xhci->capreg) + xhci->capreg->rtsoff;
189 xhci->dbreg = ((void *)xhci->capreg) + xhci->capreg->dboff;
Nico Huber5b9e6f12014-07-10 12:56:34 +0200190 xhci_debug("regbase: 0x%"PRIx32"\n", physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200191 xhci_debug("caplen: 0x%"PRIx32"\n", xhci->capreg->caplength);
192 xhci_debug("rtsoff: 0x%"PRIx32"\n", xhci->capreg->rtsoff);
193 xhci_debug("dboff: 0x%"PRIx32"\n", xhci->capreg->dboff);
194
195 xhci_debug("hciversion: %"PRIx8".%"PRIx8"\n",
196 xhci->capreg->hciver_hi, xhci->capreg->hciver_lo);
197 if ((xhci->capreg->hciversion < 0x96) ||
198 (xhci->capreg->hciversion > 0x100)) {
199 xhci_debug("Unsupported xHCI version\n");
200 goto _free_xhci;
201 }
202
Julius Werner1f864342013-09-03 17:15:31 -0700203 xhci_debug("context size: %dB\n", CTXSIZE(xhci));
Nico Huber90292652013-06-13 14:37:15 +0200204 xhci_debug("maxslots: 0x%02lx\n", xhci->capreg->MaxSlots);
205 xhci_debug("maxports: 0x%02lx\n", xhci->capreg->MaxPorts);
206 const unsigned pagesize = xhci->opreg->pagesize << 12;
207 xhci_debug("pagesize: 0x%04x\n", pagesize);
208
209 /*
210 * We haven't touched the hardware yet. So we allocate all dynamic
211 * structures at first and can still chicken out easily if we run out
212 * of memory.
213 */
Julius Werner1f864342013-09-03 17:15:31 -0700214 xhci->max_slots_en = xhci->capreg->MaxSlots & CONFIG_LP_MASK_MaxSlotsEn;
215 xhci->dcbaa = xhci_align(64, (xhci->max_slots_en + 1) * sizeof(u64));
216 xhci->dev = malloc((xhci->max_slots_en + 1) * sizeof(*xhci->dev));
217 if (!xhci->dcbaa || !xhci->dev) {
Nico Huber90292652013-06-13 14:37:15 +0200218 xhci_debug("Out of memory\n");
219 goto _free_xhci;
220 }
Julius Werner1f864342013-09-03 17:15:31 -0700221 memset(xhci->dcbaa, 0x00, (xhci->max_slots_en + 1) * sizeof(u64));
222 memset(xhci->dev, 0x00, (xhci->max_slots_en + 1) * sizeof(*xhci->dev));
Nico Huber90292652013-06-13 14:37:15 +0200223
224 /*
225 * Let dcbaa[0] point to another array of pointers, sp_ptrs.
226 * The pointers therein point to scratchpad buffers (pages).
227 */
228 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs;
229 xhci_debug("max scratchpad bufs: 0x%zx\n", max_sp_bufs);
230 if (max_sp_bufs) {
231 const size_t sp_ptrs_size = max_sp_bufs * sizeof(u64);
232 xhci->sp_ptrs = xhci_align(64, sp_ptrs_size);
233 if (!xhci->sp_ptrs) {
234 xhci_debug("Out of memory\n");
235 goto _free_xhci_structs;
236 }
237 memset(xhci->sp_ptrs, 0x00, sp_ptrs_size);
238 for (i = 0; i < max_sp_bufs; ++i) {
239 /* Could use mmap() here if we had it.
240 Maybe there is another way. */
241 void *const page = memalign(pagesize, pagesize);
242 if (!page) {
243 xhci_debug("Out of memory\n");
244 goto _free_xhci_structs;
245 }
246 xhci->sp_ptrs[i] = virt_to_phys(page);
247 }
248 xhci->dcbaa[0] = virt_to_phys(xhci->sp_ptrs);
249 }
250
Julius Werner1f864342013-09-03 17:15:31 -0700251 if (dma_initialized()) {
252 xhci->dma_buffer = dma_memalign(64 * 1024, DMA_SIZE);
253 if (!xhci->dma_buffer) {
254 xhci_debug("Not enough memory for DMA bounce buffer\n");
255 goto _free_xhci_structs;
256 }
257 }
258
Nico Huber90292652013-06-13 14:37:15 +0200259 /* Now start working on the hardware */
Nico Huber90292652013-06-13 14:37:15 +0200260 if (xhci_wait_ready(xhci))
Julius Werner1f864342013-09-03 17:15:31 -0700261 goto _free_xhci_structs;
Nico Huber90292652013-06-13 14:37:15 +0200262
263 /* TODO: Check if BIOS claims ownership (and hand over) */
264
265 xhci_reset(controller);
266 xhci_reinit(controller);
267
Nico Huber90292652013-06-13 14:37:15 +0200268 xhci->roothub->controller = controller;
269 xhci->roothub->init = xhci_rh_init;
270 xhci->roothub->init(xhci->roothub);
271
272 return controller;
273
274_free_xhci_structs:
Julius Werner7234d602014-04-08 12:54:25 -0700275 free(xhci->dma_buffer);
Nico Huber90292652013-06-13 14:37:15 +0200276 if (xhci->sp_ptrs) {
277 for (i = 0; i < max_sp_bufs; ++i) {
278 if (xhci->sp_ptrs[i])
279 free(phys_to_virt(xhci->sp_ptrs[i]));
280 }
281 }
282 free(xhci->sp_ptrs);
283 free(xhci->dcbaa);
284_free_xhci:
285 free((void *)xhci->ev_ring_table);
286 free((void *)xhci->er.ring);
287 free((void *)xhci->cr.ring);
288 free(xhci->roothub);
Julius Werner1f864342013-09-03 17:15:31 -0700289 free(xhci->dev);
Nico Huber90292652013-06-13 14:37:15 +0200290 free(xhci);
Marc Jones86127c72014-12-29 22:07:04 -0700291/* _free_controller: */
Nico Huber90292652013-06-13 14:37:15 +0200292 detach_controller(controller);
293 free(controller);
294 return NULL;
295}
296
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700297#ifdef CONFIG_LP_USB_PCI
Stefan Reinauer8992e532013-05-02 16:16:41 -0700298hci_t *
299xhci_pci_init (pcidev_t addr)
300{
301 u32 reg_addr;
Patrick Georgifdb348a2013-12-21 11:41:22 +0100302 hci_t *controller;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700303
Nico Huber6a058902014-07-04 18:17:39 +0200304 reg_addr = pci_read_config32 (addr, 0x10) & ~0xf;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700305 if (pci_read_config32 (addr, 0x14) > 0) {
306 fatal("We don't do 64bit addressing.\n");
307 }
308
Nico Huber6e230662014-07-07 16:33:59 +0200309 controller = xhci_init((unsigned long)reg_addr);
Nico Huberf4316f82014-07-07 17:11:53 +0200310 if (controller) {
311 controller->pcidev = addr;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700312
Nico Huberf4316f82014-07-07 17:11:53 +0200313 xhci_switch_ppt_ports(addr);
314 }
Stefan Reinauer8992e532013-05-02 16:16:41 -0700315
316 return controller;
317}
318#endif
319
Nico Huber90292652013-06-13 14:37:15 +0200320static void
321xhci_reset(hci_t *const controller)
322{
323 xhci_t *const xhci = XHCI_INST(controller);
324
325 xhci_stop(controller);
326
327 xhci->opreg->usbcmd |= USBCMD_HCRST;
328 xhci_debug("Resetting controller... ");
329 if (!xhci_handshake(&xhci->opreg->usbcmd, USBCMD_HCRST, 0, 1000000L))
330 usb_debug("timeout!\n");
331 else
332 usb_debug("ok.\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000333}
334
Nico Huber6e711c62012-11-12 16:20:32 +0100335static void
336xhci_reinit (hci_t *controller)
337{
Nico Huber90292652013-06-13 14:37:15 +0200338 xhci_t *const xhci = XHCI_INST(controller);
Nico Huber6e711c62012-11-12 16:20:32 +0100339
Nico Huber90292652013-06-13 14:37:15 +0200340 if (xhci_wait_ready(xhci))
341 return;
342
343 /* Enable all available slots */
Julius Werner1f864342013-09-03 17:15:31 -0700344 xhci->opreg->config = xhci->max_slots_en;
Nico Huber90292652013-06-13 14:37:15 +0200345
346 /* Set DCBAA */
347 xhci->opreg->dcbaap_lo = virt_to_phys(xhci->dcbaa);
348 xhci->opreg->dcbaap_hi = 0;
349
350 /* Initialize command ring */
351 xhci_init_cycle_ring(&xhci->cr, COMMAND_RING_SIZE);
352 xhci_debug("command ring @%p (0x%08x)\n",
353 xhci->cr.ring, virt_to_phys(xhci->cr.ring));
354 xhci->opreg->crcr_lo = virt_to_phys(xhci->cr.ring) | CRCR_RCS;
355 xhci->opreg->crcr_hi = 0;
356
357 /* Make sure interrupts are disabled */
358 xhci->opreg->usbcmd &= ~USBCMD_INTE;
359
360 /* Initialize event ring */
361 xhci_reset_event_ring(&xhci->er);
362 xhci_debug("event ring @%p (0x%08x)\n",
363 xhci->er.ring, virt_to_phys(xhci->er.ring));
364 xhci_debug("ERST Max: 0x%lx -> 0x%lx entries\n",
365 xhci->capreg->ERST_Max, 1 << xhci->capreg->ERST_Max);
366 memset((void*)xhci->ev_ring_table, 0x00, sizeof(erst_entry_t));
367 xhci->ev_ring_table[0].seg_base_lo = virt_to_phys(xhci->er.ring);
368 xhci->ev_ring_table[0].seg_base_hi = 0;
369 xhci->ev_ring_table[0].seg_size = EVENT_RING_SIZE;
370
371 /* Initialize primary interrupter */
372 xhci->hcrreg->intrrs[0].erstsz = 1;
373 xhci_update_event_dq(xhci);
374 /* erstba has to be written at last */
375 xhci->hcrreg->intrrs[0].erstba_lo = virt_to_phys(xhci->ev_ring_table);
376 xhci->hcrreg->intrrs[0].erstba_hi = 0;
377
378 xhci_start(controller);
379
380#ifdef USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +0000381 int i;
Nico Huber90292652013-06-13 14:37:15 +0200382 for (i = 0; i < 32; ++i) {
383 xhci_debug("NOOP run #%d\n", i);
384 trb_t *const cmd = xhci_next_command_trb(xhci);
385 TRB_SET(TT, cmd, TRB_CMD_NOOP);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000386
Nico Huber90292652013-06-13 14:37:15 +0200387 xhci_post_command(xhci);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000388
Nico Huber90292652013-06-13 14:37:15 +0200389 /* Wait for result in event ring */
390 xhci_wait_for_command_done(xhci, cmd, 1);
391 xhci_debug("Command ring is %srunning\n",
392 (xhci->opreg->crcr_lo & CRCR_CRR) ? "" : "not ");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000393 }
Nico Huber90292652013-06-13 14:37:15 +0200394#endif
Patrick Georgi6615ef32010-08-13 09:18:58 +0000395}
396
397static void
Nico Huber90292652013-06-13 14:37:15 +0200398xhci_shutdown(hci_t *const controller)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000399{
Nico Huber90292652013-06-13 14:37:15 +0200400 int i;
401
Patrick Georgi6615ef32010-08-13 09:18:58 +0000402 if (controller == 0)
403 return;
Nico Huber90292652013-06-13 14:37:15 +0200404
405 detach_controller(controller);
406
Julius Werner7234d602014-04-08 12:54:25 -0700407 xhci_t *const xhci = XHCI_INST(controller);
Nico Huber90292652013-06-13 14:37:15 +0200408 xhci_stop(controller);
409
Patrick Georgifdb348a2013-12-21 11:41:22 +0100410 if (controller->pcidev)
411 xhci_switchback_ppt_ports(controller->pcidev);
Nico Huberc3714422013-07-19 14:03:47 +0200412
Nico Huber90292652013-06-13 14:37:15 +0200413 if (xhci->sp_ptrs) {
414 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs;
415 for (i = 0; i < max_sp_bufs; ++i) {
416 if (xhci->sp_ptrs[i])
417 free(phys_to_virt(xhci->sp_ptrs[i]));
418 }
419 }
420 free(xhci->sp_ptrs);
Julius Werner7234d602014-04-08 12:54:25 -0700421 free(xhci->dma_buffer);
Nico Huber90292652013-06-13 14:37:15 +0200422 free(xhci->dcbaa);
Julius Werner1f864342013-09-03 17:15:31 -0700423 free(xhci->dev);
Nico Huber90292652013-06-13 14:37:15 +0200424 free((void *)xhci->ev_ring_table);
425 free((void *)xhci->er.ring);
426 free((void *)xhci->cr.ring);
427 free(xhci);
428 free(controller);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000429}
430
431static void
432xhci_start (hci_t *controller)
433{
Nico Huber90292652013-06-13 14:37:15 +0200434 xhci_t *const xhci = XHCI_INST(controller);
435
436 xhci->opreg->usbcmd |= USBCMD_RS;
437 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_HCH, 0, 1000000L))
438 xhci_debug("Controller didn't start within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000439}
440
441static void
442xhci_stop (hci_t *controller)
443{
Nico Huber90292652013-06-13 14:37:15 +0200444 xhci_t *const xhci = XHCI_INST(controller);
445
446 xhci->opreg->usbcmd &= ~USBCMD_RS;
447 if (!xhci_handshake(&xhci->opreg->usbsts,
448 USBSTS_HCH, USBSTS_HCH, 1000000L))
449 xhci_debug("Controller didn't halt within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000450}
451
452static int
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700453xhci_reset_endpoint(usbdev_t *const dev, endpoint_t *const ep)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000454{
Nico Huber90292652013-06-13 14:37:15 +0200455 xhci_t *const xhci = XHCI_INST(dev->controller);
Nico Huber90292652013-06-13 14:37:15 +0200456 const int slot_id = dev->address;
457 const int ep_id = ep ? xhci_ep_id(ep) : 1;
Julius Werner1f864342013-09-03 17:15:31 -0700458 epctx_t *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200459
460 xhci_debug("Resetting ID %d EP %d (ep state: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700461 slot_id, ep_id, EC_GET(STATE, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200462
463 /* Run Reset Endpoint Command if the EP is in Halted state */
Julius Werner1f864342013-09-03 17:15:31 -0700464 if (EC_GET(STATE, epctx) == 2) {
Nico Huber90292652013-06-13 14:37:15 +0200465 const int cc = xhci_cmd_reset_endpoint(xhci, slot_id, ep_id);
466 if (cc != CC_SUCCESS) {
467 xhci_debug("Reset Endpoint Command failed: %d\n", cc);
468 return 1;
469 }
470 }
471
472 /* Clear TT buffer for bulk and control endpoints behind a TT */
473 const int hub = dev->hub;
474 if (hub && dev->speed < HIGH_SPEED &&
475 dev->controller->devices[hub]->speed == HIGH_SPEED)
476 /* TODO */;
477
Nico Huber90292652013-06-13 14:37:15 +0200478 /* Reset transfer ring if the endpoint is in the right state */
Julius Werner1f864342013-09-03 17:15:31 -0700479 const unsigned ep_state = EC_GET(STATE, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200480 if (ep_state == 3 || ep_state == 4) {
Julius Werner1f864342013-09-03 17:15:31 -0700481 transfer_ring_t *const tr =
482 xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200483 const int cc = xhci_cmd_set_tr_dq(xhci, slot_id, ep_id,
484 tr->ring, 1);
485 if (cc != CC_SUCCESS) {
486 xhci_debug("Set TR Dequeue Command failed: %d\n", cc);
487 return 1;
488 }
489 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
490 }
491
492 xhci_debug("Finished resetting ID %d EP %d (ep state: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700493 slot_id, ep_id, EC_GET(STATE, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200494
495 return 0;
496}
497
498static void
499xhci_enqueue_trb(transfer_ring_t *const tr)
500{
501 const int chain = TRB_GET(CH, tr->cur);
502 TRB_SET(C, tr->cur, tr->pcs);
503 ++tr->cur;
504
505 while (TRB_GET(TT, tr->cur) == TRB_LINK) {
506 xhci_spew("Handling LINK pointer\n");
507 const int tc = TRB_GET(TC, tr->cur);
508 TRB_SET(CH, tr->cur, chain);
509 TRB_SET(C, tr->cur, tr->pcs);
510 tr->cur = phys_to_virt(tr->cur->ptr_low);
511 if (tc)
512 tr->pcs ^= 1;
513 }
514}
515
516static void
517xhci_enqueue_td(transfer_ring_t *const tr, const int ep, const size_t mps,
518 const int dalen, void *const data, const int dir)
519{
520 trb_t *trb = NULL; /* cur TRB */
521 u8 *cur_start = data; /* cur data pointer */
522 size_t length = dalen; /* remaining bytes */
523 size_t packets = (length + mps - 1) / mps; /* remaining packets */
524 size_t residue = 0; /* residue from last TRB */
525 size_t trb_count = 0; /* TRBs added so far */
526
527 while (length || !trb_count /* enqueue at least one */) {
528 const size_t cur_end = ((size_t)cur_start + 0x10000) & ~0xffff;
529 size_t cur_length = cur_end - (size_t)cur_start;
530 if (length < cur_length) {
531 cur_length = length;
532 packets = 0;
533 length = 0;
534 } else {
535 packets -= (residue + cur_length) / mps;
536 residue = (residue + cur_length) % mps;
537 length -= cur_length;
538 }
539
540 trb = tr->cur;
541 xhci_clear_trb(trb, tr->pcs);
542 trb->ptr_low = virt_to_phys(cur_start);
543 TRB_SET(TL, trb, cur_length);
Rajmohan Manid6fb32b2014-05-30 13:06:01 -0700544 TRB_SET(TDS, trb, MIN(TRB_MAX_TD_SIZE, packets));
Julius Werner83da5012013-09-27 12:45:11 -0700545 TRB_SET(CH, trb, 1);
Nico Huber90292652013-06-13 14:37:15 +0200546
547 /* Check for first, data stage TRB */
548 if (!trb_count && ep == 1) {
549 TRB_SET(DIR, trb, dir);
550 TRB_SET(TT, trb, TRB_DATA_STAGE);
551 } else {
552 TRB_SET(TT, trb, TRB_NORMAL);
553 }
554
Nico Huber90292652013-06-13 14:37:15 +0200555 xhci_enqueue_trb(tr);
556
557 cur_start += cur_length;
558 ++trb_count;
559 }
Julius Werner83da5012013-09-27 12:45:11 -0700560
561 trb = tr->cur;
562 xhci_clear_trb(trb, tr->pcs);
563 trb->ptr_low = virt_to_phys(trb); /* for easier debugging only */
564 TRB_SET(TT, trb, TRB_EVENT_DATA);
565 TRB_SET(IOC, trb, 1);
566
567 xhci_enqueue_trb(tr);
Nico Huber90292652013-06-13 14:37:15 +0200568}
569
570static int
571xhci_control(usbdev_t *const dev, const direction_t dir,
572 const int drlen, void *const devreq,
Julius Werner1f864342013-09-03 17:15:31 -0700573 const int dalen, unsigned char *const src)
Nico Huber90292652013-06-13 14:37:15 +0200574{
Julius Werner1f864342013-09-03 17:15:31 -0700575 unsigned char *data = src;
Nico Huber90292652013-06-13 14:37:15 +0200576 xhci_t *const xhci = XHCI_INST(dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700577 epctx_t *const epctx = xhci->dev[dev->address].ctx.ep0;
578 transfer_ring_t *const tr = xhci->dev[dev->address].transfer_rings[1];
Nico Huber90292652013-06-13 14:37:15 +0200579
580 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700581 if ((off + dalen) > ((TRANSFER_RING_SIZE - 4) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200582 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800583 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200584 }
585
Julius Werner49ba2832013-09-26 15:13:44 -0700586 /* Reset endpoint if it's not running */
Julius Werner1f864342013-09-03 17:15:31 -0700587 const unsigned ep_state = EC_GET(STATE, epctx);
Julius Werner49ba2832013-09-26 15:13:44 -0700588 if (ep_state > 1) {
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700589 if (xhci_reset_endpoint(dev, NULL))
Julius Wernere9738db2013-02-21 13:41:40 -0800590 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200591 }
592
Julius Werner1f864342013-09-03 17:15:31 -0700593 if (dalen && !dma_coherent(src)) {
594 data = xhci->dma_buffer;
595 if (dalen > DMA_SIZE) {
596 xhci_debug("Control transfer too large: %d\n", dalen);
597 return -1;
598 }
599 if (dir == OUT)
600 memcpy(data, src, dalen);
601 }
602
Nico Huber90292652013-06-13 14:37:15 +0200603 /* Fill and enqueue setup TRB */
604 trb_t *const setup = tr->cur;
605 xhci_clear_trb(setup, tr->pcs);
606 setup->ptr_low = ((u32 *)devreq)[0];
607 setup->ptr_high = ((u32 *)devreq)[1];
608 TRB_SET(TL, setup, 8);
609 TRB_SET(TRT, setup, (dalen)
610 ? ((dir == OUT) ? TRB_TRT_OUT_DATA : TRB_TRT_IN_DATA)
611 : TRB_TRT_NO_DATA);
612 TRB_SET(TT, setup, TRB_SETUP_STAGE);
613 TRB_SET(IDT, setup, 1);
614 TRB_SET(IOC, setup, 1);
615 xhci_enqueue_trb(tr);
616
617 /* Fill and enqueue data TRBs (if any) */
618 if (dalen) {
Julius Werner1f864342013-09-03 17:15:31 -0700619 const unsigned mps = EC_GET(MPS, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200620 const unsigned dt_dir = (dir == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
621 xhci_enqueue_td(tr, 1, mps, dalen, data, dt_dir);
622 }
623
624 /* Fill status TRB */
625 trb_t *const status = tr->cur;
626 xhci_clear_trb(status, tr->pcs);
627 TRB_SET(DIR, status, (dir == OUT) ? TRB_DIR_IN : TRB_DIR_OUT);
628 TRB_SET(TT, status, TRB_STATUS_STAGE);
629 TRB_SET(IOC, status, 1);
630 xhci_enqueue_trb(tr);
631
632 /* Ring doorbell for EP0 */
633 xhci->dbreg[dev->address] = 1;
634
635 /* Wait for transfer events */
Julius Wernere9738db2013-02-21 13:41:40 -0800636 int i, transferred = 0;
Nico Huber90292652013-06-13 14:37:15 +0200637 const int n_stages = 2 + !!dalen;
638 for (i = 0; i < n_stages; ++i) {
639 const int ret = xhci_wait_for_transfer(xhci, dev->address, 1);
Julius Wernere9738db2013-02-21 13:41:40 -0800640 transferred += ret;
641 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200642 if (ret == TIMEOUT) {
643 xhci_debug("Stopping ID %d EP 1\n",
644 dev->address);
645 xhci_cmd_stop_endpoint(xhci, dev->address, 1);
646 }
647 xhci_debug("Stage %d/%d failed: %d\n"
648 " trb ring: @%p\n"
649 " setup trb: @%p\n"
650 " status trb: @%p\n"
651 " ep state: %d -> %d\n"
652 " usbsts: 0x%08"PRIx32"\n",
653 i, n_stages, ret,
654 tr->ring, setup, status,
Julius Werner1f864342013-09-03 17:15:31 -0700655 ep_state, EC_GET(STATE, epctx),
Nico Huber90292652013-06-13 14:37:15 +0200656 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800657 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200658 }
659 }
660
Julius Werner1f864342013-09-03 17:15:31 -0700661 if (dir == IN && data != src)
662 memcpy(src, data, transferred);
Julius Wernere9738db2013-02-21 13:41:40 -0800663 return transferred;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000664}
665
666/* finalize == 1: if data is of packet aligned size, add a zero length packet */
667static int
Julius Werner1f864342013-09-03 17:15:31 -0700668xhci_bulk(endpoint_t *const ep, const int size, u8 *const src,
Nico Huber90292652013-06-13 14:37:15 +0200669 const int finalize)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000670{
Nico Huber90292652013-06-13 14:37:15 +0200671 /* finalize: Hopefully the xHCI controller always does this.
672 We have no control over the packets. */
673
Julius Werner1f864342013-09-03 17:15:31 -0700674 u8 *data = src;
Nico Huber90292652013-06-13 14:37:15 +0200675 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700676 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200677 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700678 epctx_t *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
679 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200680
681 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700682 if ((off + size) > ((TRANSFER_RING_SIZE - 2) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200683 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800684 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200685 }
686
Julius Werner1f864342013-09-03 17:15:31 -0700687 if (!dma_coherent(src)) {
688 data = xhci->dma_buffer;
689 if (size > DMA_SIZE) {
690 xhci_debug("Bulk transfer too large: %d\n", size);
691 return -1;
692 }
693 if (ep->direction == OUT)
694 memcpy(data, src, size);
695 }
696
Julius Werner49ba2832013-09-26 15:13:44 -0700697 /* Reset endpoint if it's not running */
Julius Werner1f864342013-09-03 17:15:31 -0700698 const unsigned ep_state = EC_GET(STATE, epctx);
Julius Werner49ba2832013-09-26 15:13:44 -0700699 if (ep_state > 1) {
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700700 if (xhci_reset_endpoint(ep->dev, ep))
Julius Wernere9738db2013-02-21 13:41:40 -0800701 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200702 }
703
704 /* Enqueue transfer and ring doorbell */
Julius Werner1f864342013-09-03 17:15:31 -0700705 const unsigned mps = EC_GET(MPS, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200706 const unsigned dir = (ep->direction == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
707 xhci_enqueue_td(tr, ep_id, mps, size, data, dir);
708 xhci->dbreg[ep->dev->address] = ep_id;
709
710 /* Wait for transfer event */
711 const int ret = xhci_wait_for_transfer(xhci, ep->dev->address, ep_id);
Julius Wernere9738db2013-02-21 13:41:40 -0800712 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200713 if (ret == TIMEOUT) {
714 xhci_debug("Stopping ID %d EP %d\n",
715 ep->dev->address, ep_id);
716 xhci_cmd_stop_endpoint(xhci, ep->dev->address, ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200717 }
718 xhci_debug("Bulk transfer failed: %d\n"
719 " ep state: %d -> %d\n"
720 " usbsts: 0x%08"PRIx32"\n",
721 ret, ep_state,
Julius Werner1f864342013-09-03 17:15:31 -0700722 EC_GET(STATE, epctx),
Nico Huber90292652013-06-13 14:37:15 +0200723 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800724 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200725 }
726
Julius Werner1f864342013-09-03 17:15:31 -0700727 if (ep->direction == IN && data != src)
728 memcpy(src, data, ret);
Julius Wernere9738db2013-02-21 13:41:40 -0800729 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200730}
731
732static trb_t *
733xhci_next_trb(trb_t *cur, int *const pcs)
734{
735 ++cur;
736 while (TRB_GET(TT, cur) == TRB_LINK) {
737 if (pcs && TRB_GET(TC, cur))
738 *pcs ^= 1;
739 cur = phys_to_virt(cur->ptr_low);
740 }
741 return cur;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000742}
743
744/* create and hook-up an intr queue into device schedule */
Nico Huber90292652013-06-13 14:37:15 +0200745static void *
746xhci_create_intr_queue(endpoint_t *const ep,
747 const int reqsize, const int reqcount,
748 const int reqtiming)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000749{
Nico Huber90292652013-06-13 14:37:15 +0200750 /* reqtiming: We ignore it and use the interval from the
751 endpoint descriptor configured earlier. */
752
753 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700754 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200755 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700756 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200757
758 if (reqcount > (TRANSFER_RING_SIZE - 2)) {
759 xhci_debug("reqcount is too high, at most %d supported\n",
760 TRANSFER_RING_SIZE - 2);
761 return NULL;
762 }
763 if (reqsize > 0x10000) {
764 xhci_debug("reqsize is too large, at most 64KiB supported\n");
765 return NULL;
766 }
Julius Werner1f864342013-09-03 17:15:31 -0700767 if (xhci->dev[slot_id].interrupt_queues[ep_id]) {
Nico Huber90292652013-06-13 14:37:15 +0200768 xhci_debug("Only one interrupt queue per endpoint supported\n");
769 return NULL;
770 }
771
772 /* Allocate intrq structure and reqdata chunks */
773
774 intrq_t *const intrq = malloc(sizeof(*intrq));
775 if (!intrq) {
776 xhci_debug("Out of memory\n");
777 return NULL;
778 }
779
780 int i;
781 int pcs = tr->pcs;
782 trb_t *cur = tr->cur;
783 for (i = 0; i < reqcount; ++i) {
784 if (TRB_GET(C, cur) == pcs) {
785 xhci_debug("Not enough empty TRBs\n");
786 goto _free_return;
787 }
788 void *const reqdata = xhci_align(1, reqsize);
789 if (!reqdata) {
790 xhci_debug("Out of memory\n");
791 goto _free_return;
792 }
793 xhci_clear_trb(cur, pcs);
794 cur->ptr_low = virt_to_phys(reqdata);
795 cur->ptr_high = 0;
796 TRB_SET(TL, cur, reqsize);
797 TRB_SET(TT, cur, TRB_NORMAL);
798 TRB_SET(ISP, cur, 1);
799 TRB_SET(IOC, cur, 1);
800
801 cur = xhci_next_trb(cur, &pcs);
802 }
803
804 intrq->size = reqsize;
805 intrq->count = reqcount;
806 intrq->next = tr->cur;
807 intrq->ready = NULL;
808 intrq->ep = ep;
Julius Werner1f864342013-09-03 17:15:31 -0700809 xhci->dev[slot_id].interrupt_queues[ep_id] = intrq;
Nico Huber90292652013-06-13 14:37:15 +0200810
811 /* Now enqueue all the prepared TRBs but the last
812 and ring the doorbell. */
813 for (i = 0; i < (reqcount - 1); ++i)
814 xhci_enqueue_trb(tr);
Julius Werner1f864342013-09-03 17:15:31 -0700815 xhci->dbreg[slot_id] = ep_id;
Nico Huber90292652013-06-13 14:37:15 +0200816
817 return intrq;
818
819_free_return:
820 cur = tr->cur;
821 for (--i; i >= 0; --i) {
822 free(phys_to_virt(cur->ptr_low));
823 cur = xhci_next_trb(cur, NULL);
824 }
825 free(intrq);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000826 return NULL;
827}
828
829/* remove queue from device schedule, dropping all data that came in */
830static void
Nico Huber90292652013-06-13 14:37:15 +0200831xhci_destroy_intr_queue(endpoint_t *const ep, void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000832{
Nico Huber90292652013-06-13 14:37:15 +0200833 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700834 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200835 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700836 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200837
838 intrq_t *const intrq = (intrq_t *)q;
839
840 /* Make sure the endpoint is stopped */
Julius Werner1f864342013-09-03 17:15:31 -0700841 if (EC_GET(STATE, xhci->dev[slot_id].ctx.ep[ep_id]) == 1) {
842 const int cc = xhci_cmd_stop_endpoint(xhci, slot_id, ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200843 if (cc != CC_SUCCESS)
844 xhci_debug("Warning: Failed to stop endpoint\n");
845 }
846
847 /* Process all remaining transfer events */
848 xhci_handle_events(xhci);
849
850 /* Free all pending transfers and the interrupt queue structure */
851 int i;
852 for (i = 0; i < intrq->count; ++i) {
853 free(phys_to_virt(intrq->next->ptr_low));
854 intrq->next = xhci_next_trb(intrq->next, NULL);
855 }
Julius Werner1f864342013-09-03 17:15:31 -0700856 xhci->dev[slot_id].interrupt_queues[ep_id] = NULL;
Nico Huber90292652013-06-13 14:37:15 +0200857 free((void *)intrq);
858
859 /* Reset the controller's dequeue pointer and reinitialize the ring */
Julius Werner1f864342013-09-03 17:15:31 -0700860 xhci_cmd_set_tr_dq(xhci, slot_id, ep_id, tr->ring, 1);
Nico Huber90292652013-06-13 14:37:15 +0200861 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000862}
863
864/* read one intr-packet from queue, if available. extend the queue for new input.
865 return NULL if nothing new available.
866 Recommended use: while (data=poll_intr_queue(q)) process(data);
867 */
Nico Huber90292652013-06-13 14:37:15 +0200868static u8 *
869xhci_poll_intr_queue(void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000870{
Nico Huber90292652013-06-13 14:37:15 +0200871 if (!q)
872 return NULL;
873
874 intrq_t *const intrq = (intrq_t *)q;
875 endpoint_t *const ep = intrq->ep;
876 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
877
878 /* TODO: Reset interrupt queue if it gets halted? */
879
880 xhci_handle_events(xhci);
881
882 u8 *reqdata = NULL;
883 while (!reqdata && intrq->ready) {
884 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700885 transfer_ring_t *const tr =
886 xhci->dev[ep->dev->address].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200887
888 /* Fetch the request's buffer */
889 reqdata = phys_to_virt(intrq->next->ptr_low);
890
891 /* Enqueue the last (spare) TRB and ring doorbell */
892 xhci_enqueue_trb(tr);
893 xhci->dbreg[ep->dev->address] = ep_id;
894
895 /* Reuse the current buffer for the next spare TRB */
896 xhci_clear_trb(tr->cur, tr->pcs);
897 tr->cur->ptr_low = virt_to_phys(reqdata);
898 tr->cur->ptr_high = 0;
899 TRB_SET(TL, tr->cur, intrq->size);
900 TRB_SET(TT, tr->cur, TRB_NORMAL);
901 TRB_SET(ISP, tr->cur, 1);
902 TRB_SET(IOC, tr->cur, 1);
903
904 /* Check if anything was transferred */
905 const size_t read = TRB_GET(TL, intrq->next);
906 if (!read)
907 reqdata = NULL;
908 else if (read < intrq->size)
909 /* At least zero it, poll interface is rather limited */
910 memset(reqdata + read, 0x00, intrq->size - read);
911
912 /* Advance the interrupt queue */
913 if (intrq->ready == intrq->next)
914 /* This was last TRB being ready */
915 intrq->ready = NULL;
916 intrq->next = xhci_next_trb(intrq->next, NULL);
917 }
918
919 return reqdata;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000920}