blob: c60a9676079060c6b461e86c247c0acfb78b5199 [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 */
91static void
92xhci_switch_ppt_ports(pcidev_t addr)
93{
94 if (pci_read_config32(addr, 0x00) == 0x1e318086) {
95 u32 reg32 = pci_read_config32(addr, 0xdc) & 0xf;
96 xhci_debug("Ports capable of SuperSpeed: 0x%"PRIx32"\n", reg32);
97
98 /* For now, do not enable SuperSpeed on any ports */
99 //pci_write_config32(addr, 0xd8, reg32);
100 pci_write_config32(addr, 0xd8, 0x00000000);
101 reg32 = pci_read_config32(addr, 0xd8) & 0xf;
102 xhci_debug("Configured for SuperSpeed: 0x%"PRIx32"\n", reg32);
103
104 reg32 = pci_read_config32(addr, 0xd4) & 0xf;
105 xhci_debug("Trying to switch over: 0x%"PRIx32"\n", reg32);
106
107 pci_write_config32(addr, 0xd0, reg32);
108 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
109 xhci_debug("Actually switched over: 0x%"PRIx32"\n", reg32);
110 }
111}
112
Nico Huberc3714422013-07-19 14:03:47 +0200113/* On Panther Point: switch all ports back to EHCI */
114static void
115xhci_switchback_ppt_ports(pcidev_t addr)
116{
117 if (pci_read_config32(addr, 0x00) == 0x1e318086) {
118 u32 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
119 xhci_debug("Switching ports back: 0x%"PRIx32"\n", reg32);
120 pci_write_config32(addr, 0xd0, 0x00000000);
121 reg32 = pci_read_config32(addr, 0xd0) & 0xf;
122 xhci_debug("Still switched to xHCI: 0x%"PRIx32"\n", reg32);
123 }
124}
125
Nico Huber90292652013-06-13 14:37:15 +0200126static long
127xhci_handshake(volatile u32 *const reg, u32 mask, u32 wait_for, long timeout_us)
128{
129 while ((*reg & mask) != wait_for && timeout_us--) udelay(1);
130 return timeout_us;
131}
132
133static int
134xhci_wait_ready(xhci_t *const xhci)
135{
136 xhci_debug("Waiting for controller to be ready... ");
137 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_CNR, 0, 100000L)) {
138 usb_debug("timeout!\n");
139 return -1;
140 }
141 usb_debug("ok.\n");
142 return 0;
143}
144
145hci_t *
Nico Huber6e230662014-07-07 16:33:59 +0200146xhci_init (unsigned long physical_bar)
Nico Huber90292652013-06-13 14:37:15 +0200147{
148 int i;
149
150 /* First, allocate and initialize static controller structures */
151
152 hci_t *const controller = new_controller();
Nico Huber90292652013-06-13 14:37:15 +0200153 controller->type = XHCI;
154 controller->start = xhci_start;
155 controller->stop = xhci_stop;
156 controller->reset = xhci_reset;
157 controller->init = xhci_reinit;
158 controller->shutdown = xhci_shutdown;
159 controller->bulk = xhci_bulk;
160 controller->control = xhci_control;
161 controller->set_address = xhci_set_address;
162 controller->finish_device_config= xhci_finish_device_config;
163 controller->destroy_device = xhci_destroy_dev;
164 controller->create_intr_queue = xhci_create_intr_queue;
165 controller->destroy_intr_queue = xhci_destroy_intr_queue;
166 controller->poll_intr_queue = xhci_poll_intr_queue;
Nico Huber8b8e9632014-07-07 17:20:53 +0200167 controller->pcidev = 0;
Nico Huber90292652013-06-13 14:37:15 +0200168
Julius Werner7234d602014-04-08 12:54:25 -0700169 controller->reg_base = (u32)physical_bar;
170 controller->instance = xzalloc(sizeof(xhci_t));
Nico Huber90292652013-06-13 14:37:15 +0200171 xhci_t *const xhci = (xhci_t *)controller->instance;
Nico Huber90292652013-06-13 14:37:15 +0200172
173 init_device_entry(controller, 0);
174 xhci->roothub = controller->devices[0];
175 xhci->cr.ring = xhci_align(64, COMMAND_RING_SIZE * sizeof(trb_t));
176 xhci->er.ring = xhci_align(64, EVENT_RING_SIZE * sizeof(trb_t));
177 xhci->ev_ring_table = xhci_align(64, sizeof(erst_entry_t));
178 if (!xhci->roothub || !xhci->cr.ring ||
179 !xhci->er.ring || !xhci->ev_ring_table) {
180 xhci_debug("Out of memory\n");
181 goto _free_xhci;
182 }
183
Nico Huber5b9e6f12014-07-10 12:56:34 +0200184 xhci->capreg = phys_to_virt(physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200185 xhci->opreg = ((void *)xhci->capreg) + xhci->capreg->caplength;
186 xhci->hcrreg = ((void *)xhci->capreg) + xhci->capreg->rtsoff;
187 xhci->dbreg = ((void *)xhci->capreg) + xhci->capreg->dboff;
Nico Huber5b9e6f12014-07-10 12:56:34 +0200188 xhci_debug("regbase: 0x%"PRIx32"\n", physical_bar);
Nico Huber90292652013-06-13 14:37:15 +0200189 xhci_debug("caplen: 0x%"PRIx32"\n", xhci->capreg->caplength);
190 xhci_debug("rtsoff: 0x%"PRIx32"\n", xhci->capreg->rtsoff);
191 xhci_debug("dboff: 0x%"PRIx32"\n", xhci->capreg->dboff);
192
193 xhci_debug("hciversion: %"PRIx8".%"PRIx8"\n",
194 xhci->capreg->hciver_hi, xhci->capreg->hciver_lo);
195 if ((xhci->capreg->hciversion < 0x96) ||
196 (xhci->capreg->hciversion > 0x100)) {
197 xhci_debug("Unsupported xHCI version\n");
198 goto _free_xhci;
199 }
200
Julius Werner1f864342013-09-03 17:15:31 -0700201 xhci_debug("context size: %dB\n", CTXSIZE(xhci));
Nico Huber90292652013-06-13 14:37:15 +0200202 xhci_debug("maxslots: 0x%02lx\n", xhci->capreg->MaxSlots);
203 xhci_debug("maxports: 0x%02lx\n", xhci->capreg->MaxPorts);
204 const unsigned pagesize = xhci->opreg->pagesize << 12;
205 xhci_debug("pagesize: 0x%04x\n", pagesize);
206
207 /*
208 * We haven't touched the hardware yet. So we allocate all dynamic
209 * structures at first and can still chicken out easily if we run out
210 * of memory.
211 */
Julius Werner1f864342013-09-03 17:15:31 -0700212 xhci->max_slots_en = xhci->capreg->MaxSlots & CONFIG_LP_MASK_MaxSlotsEn;
213 xhci->dcbaa = xhci_align(64, (xhci->max_slots_en + 1) * sizeof(u64));
214 xhci->dev = malloc((xhci->max_slots_en + 1) * sizeof(*xhci->dev));
215 if (!xhci->dcbaa || !xhci->dev) {
Nico Huber90292652013-06-13 14:37:15 +0200216 xhci_debug("Out of memory\n");
217 goto _free_xhci;
218 }
Julius Werner1f864342013-09-03 17:15:31 -0700219 memset(xhci->dcbaa, 0x00, (xhci->max_slots_en + 1) * sizeof(u64));
220 memset(xhci->dev, 0x00, (xhci->max_slots_en + 1) * sizeof(*xhci->dev));
Nico Huber90292652013-06-13 14:37:15 +0200221
222 /*
223 * Let dcbaa[0] point to another array of pointers, sp_ptrs.
224 * The pointers therein point to scratchpad buffers (pages).
225 */
226 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs;
227 xhci_debug("max scratchpad bufs: 0x%zx\n", max_sp_bufs);
228 if (max_sp_bufs) {
229 const size_t sp_ptrs_size = max_sp_bufs * sizeof(u64);
230 xhci->sp_ptrs = xhci_align(64, sp_ptrs_size);
231 if (!xhci->sp_ptrs) {
232 xhci_debug("Out of memory\n");
233 goto _free_xhci_structs;
234 }
235 memset(xhci->sp_ptrs, 0x00, sp_ptrs_size);
236 for (i = 0; i < max_sp_bufs; ++i) {
237 /* Could use mmap() here if we had it.
238 Maybe there is another way. */
239 void *const page = memalign(pagesize, pagesize);
240 if (!page) {
241 xhci_debug("Out of memory\n");
242 goto _free_xhci_structs;
243 }
244 xhci->sp_ptrs[i] = virt_to_phys(page);
245 }
246 xhci->dcbaa[0] = virt_to_phys(xhci->sp_ptrs);
247 }
248
Julius Werner1f864342013-09-03 17:15:31 -0700249 if (dma_initialized()) {
250 xhci->dma_buffer = dma_memalign(64 * 1024, DMA_SIZE);
251 if (!xhci->dma_buffer) {
252 xhci_debug("Not enough memory for DMA bounce buffer\n");
253 goto _free_xhci_structs;
254 }
255 }
256
Nico Huber90292652013-06-13 14:37:15 +0200257 /* Now start working on the hardware */
Nico Huber90292652013-06-13 14:37:15 +0200258 if (xhci_wait_ready(xhci))
Julius Werner1f864342013-09-03 17:15:31 -0700259 goto _free_xhci_structs;
Nico Huber90292652013-06-13 14:37:15 +0200260
261 /* TODO: Check if BIOS claims ownership (and hand over) */
262
263 xhci_reset(controller);
264 xhci_reinit(controller);
265
Nico Huber90292652013-06-13 14:37:15 +0200266 xhci->roothub->controller = controller;
267 xhci->roothub->init = xhci_rh_init;
268 xhci->roothub->init(xhci->roothub);
269
270 return controller;
271
272_free_xhci_structs:
Julius Werner7234d602014-04-08 12:54:25 -0700273 free(xhci->dma_buffer);
Nico Huber90292652013-06-13 14:37:15 +0200274 if (xhci->sp_ptrs) {
275 for (i = 0; i < max_sp_bufs; ++i) {
276 if (xhci->sp_ptrs[i])
277 free(phys_to_virt(xhci->sp_ptrs[i]));
278 }
279 }
280 free(xhci->sp_ptrs);
281 free(xhci->dcbaa);
282_free_xhci:
283 free((void *)xhci->ev_ring_table);
284 free((void *)xhci->er.ring);
285 free((void *)xhci->cr.ring);
286 free(xhci->roothub);
Julius Werner1f864342013-09-03 17:15:31 -0700287 free(xhci->dev);
Nico Huber90292652013-06-13 14:37:15 +0200288 free(xhci);
289_free_controller:
290 detach_controller(controller);
291 free(controller);
292 return NULL;
293}
294
Gabe Black1ee2c6d2013-08-09 04:27:35 -0700295#ifdef CONFIG_LP_USB_PCI
Stefan Reinauer8992e532013-05-02 16:16:41 -0700296hci_t *
297xhci_pci_init (pcidev_t addr)
298{
299 u32 reg_addr;
Patrick Georgifdb348a2013-12-21 11:41:22 +0100300 hci_t *controller;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700301
Nico Huber6a058902014-07-04 18:17:39 +0200302 reg_addr = pci_read_config32 (addr, 0x10) & ~0xf;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700303 if (pci_read_config32 (addr, 0x14) > 0) {
304 fatal("We don't do 64bit addressing.\n");
305 }
306
Nico Huber6e230662014-07-07 16:33:59 +0200307 controller = xhci_init((unsigned long)reg_addr);
Nico Huberf4316f82014-07-07 17:11:53 +0200308 if (controller) {
309 controller->pcidev = addr;
Stefan Reinauer8992e532013-05-02 16:16:41 -0700310
Nico Huberf4316f82014-07-07 17:11:53 +0200311 xhci_switch_ppt_ports(addr);
312 }
Stefan Reinauer8992e532013-05-02 16:16:41 -0700313
314 return controller;
315}
316#endif
317
Nico Huber90292652013-06-13 14:37:15 +0200318static void
319xhci_reset(hci_t *const controller)
320{
321 xhci_t *const xhci = XHCI_INST(controller);
322
323 xhci_stop(controller);
324
325 xhci->opreg->usbcmd |= USBCMD_HCRST;
326 xhci_debug("Resetting controller... ");
327 if (!xhci_handshake(&xhci->opreg->usbcmd, USBCMD_HCRST, 0, 1000000L))
328 usb_debug("timeout!\n");
329 else
330 usb_debug("ok.\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000331}
332
Nico Huber6e711c62012-11-12 16:20:32 +0100333static void
334xhci_reinit (hci_t *controller)
335{
Nico Huber90292652013-06-13 14:37:15 +0200336 xhci_t *const xhci = XHCI_INST(controller);
Nico Huber6e711c62012-11-12 16:20:32 +0100337
Nico Huber90292652013-06-13 14:37:15 +0200338 if (xhci_wait_ready(xhci))
339 return;
340
341 /* Enable all available slots */
Julius Werner1f864342013-09-03 17:15:31 -0700342 xhci->opreg->config = xhci->max_slots_en;
Nico Huber90292652013-06-13 14:37:15 +0200343
344 /* Set DCBAA */
345 xhci->opreg->dcbaap_lo = virt_to_phys(xhci->dcbaa);
346 xhci->opreg->dcbaap_hi = 0;
347
348 /* Initialize command ring */
349 xhci_init_cycle_ring(&xhci->cr, COMMAND_RING_SIZE);
350 xhci_debug("command ring @%p (0x%08x)\n",
351 xhci->cr.ring, virt_to_phys(xhci->cr.ring));
352 xhci->opreg->crcr_lo = virt_to_phys(xhci->cr.ring) | CRCR_RCS;
353 xhci->opreg->crcr_hi = 0;
354
355 /* Make sure interrupts are disabled */
356 xhci->opreg->usbcmd &= ~USBCMD_INTE;
357
358 /* Initialize event ring */
359 xhci_reset_event_ring(&xhci->er);
360 xhci_debug("event ring @%p (0x%08x)\n",
361 xhci->er.ring, virt_to_phys(xhci->er.ring));
362 xhci_debug("ERST Max: 0x%lx -> 0x%lx entries\n",
363 xhci->capreg->ERST_Max, 1 << xhci->capreg->ERST_Max);
364 memset((void*)xhci->ev_ring_table, 0x00, sizeof(erst_entry_t));
365 xhci->ev_ring_table[0].seg_base_lo = virt_to_phys(xhci->er.ring);
366 xhci->ev_ring_table[0].seg_base_hi = 0;
367 xhci->ev_ring_table[0].seg_size = EVENT_RING_SIZE;
368
369 /* Initialize primary interrupter */
370 xhci->hcrreg->intrrs[0].erstsz = 1;
371 xhci_update_event_dq(xhci);
372 /* erstba has to be written at last */
373 xhci->hcrreg->intrrs[0].erstba_lo = virt_to_phys(xhci->ev_ring_table);
374 xhci->hcrreg->intrrs[0].erstba_hi = 0;
375
376 xhci_start(controller);
377
378#ifdef USB_DEBUG
Patrick Georgi6615ef32010-08-13 09:18:58 +0000379 int i;
Nico Huber90292652013-06-13 14:37:15 +0200380 for (i = 0; i < 32; ++i) {
381 xhci_debug("NOOP run #%d\n", i);
382 trb_t *const cmd = xhci_next_command_trb(xhci);
383 TRB_SET(TT, cmd, TRB_CMD_NOOP);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000384
Nico Huber90292652013-06-13 14:37:15 +0200385 xhci_post_command(xhci);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000386
Nico Huber90292652013-06-13 14:37:15 +0200387 /* Wait for result in event ring */
388 xhci_wait_for_command_done(xhci, cmd, 1);
389 xhci_debug("Command ring is %srunning\n",
390 (xhci->opreg->crcr_lo & CRCR_CRR) ? "" : "not ");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000391 }
Nico Huber90292652013-06-13 14:37:15 +0200392#endif
Patrick Georgi6615ef32010-08-13 09:18:58 +0000393}
394
395static void
Nico Huber90292652013-06-13 14:37:15 +0200396xhci_shutdown(hci_t *const controller)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000397{
Nico Huber90292652013-06-13 14:37:15 +0200398 int i;
399
Patrick Georgi6615ef32010-08-13 09:18:58 +0000400 if (controller == 0)
401 return;
Nico Huber90292652013-06-13 14:37:15 +0200402
403 detach_controller(controller);
404
Julius Werner7234d602014-04-08 12:54:25 -0700405 xhci_t *const xhci = XHCI_INST(controller);
Nico Huber90292652013-06-13 14:37:15 +0200406 xhci_stop(controller);
407
Patrick Georgifdb348a2013-12-21 11:41:22 +0100408 if (controller->pcidev)
409 xhci_switchback_ppt_ports(controller->pcidev);
Nico Huberc3714422013-07-19 14:03:47 +0200410
Nico Huber90292652013-06-13 14:37:15 +0200411 if (xhci->sp_ptrs) {
412 const size_t max_sp_bufs = xhci->capreg->Max_Scratchpad_Bufs;
413 for (i = 0; i < max_sp_bufs; ++i) {
414 if (xhci->sp_ptrs[i])
415 free(phys_to_virt(xhci->sp_ptrs[i]));
416 }
417 }
418 free(xhci->sp_ptrs);
Julius Werner7234d602014-04-08 12:54:25 -0700419 free(xhci->dma_buffer);
Nico Huber90292652013-06-13 14:37:15 +0200420 free(xhci->dcbaa);
Julius Werner1f864342013-09-03 17:15:31 -0700421 free(xhci->dev);
Nico Huber90292652013-06-13 14:37:15 +0200422 free((void *)xhci->ev_ring_table);
423 free((void *)xhci->er.ring);
424 free((void *)xhci->cr.ring);
425 free(xhci);
426 free(controller);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000427}
428
429static void
430xhci_start (hci_t *controller)
431{
Nico Huber90292652013-06-13 14:37:15 +0200432 xhci_t *const xhci = XHCI_INST(controller);
433
434 xhci->opreg->usbcmd |= USBCMD_RS;
435 if (!xhci_handshake(&xhci->opreg->usbsts, USBSTS_HCH, 0, 1000000L))
436 xhci_debug("Controller didn't start within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000437}
438
439static void
440xhci_stop (hci_t *controller)
441{
Nico Huber90292652013-06-13 14:37:15 +0200442 xhci_t *const xhci = XHCI_INST(controller);
443
444 xhci->opreg->usbcmd &= ~USBCMD_RS;
445 if (!xhci_handshake(&xhci->opreg->usbsts,
446 USBSTS_HCH, USBSTS_HCH, 1000000L))
447 xhci_debug("Controller didn't halt within 1s\n");
Patrick Georgi6615ef32010-08-13 09:18:58 +0000448}
449
450static int
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700451xhci_reset_endpoint(usbdev_t *const dev, endpoint_t *const ep)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000452{
Nico Huber90292652013-06-13 14:37:15 +0200453 xhci_t *const xhci = XHCI_INST(dev->controller);
Nico Huber90292652013-06-13 14:37:15 +0200454 const int slot_id = dev->address;
455 const int ep_id = ep ? xhci_ep_id(ep) : 1;
Julius Werner1f864342013-09-03 17:15:31 -0700456 epctx_t *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200457
458 xhci_debug("Resetting ID %d EP %d (ep state: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700459 slot_id, ep_id, EC_GET(STATE, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200460
461 /* Run Reset Endpoint Command if the EP is in Halted state */
Julius Werner1f864342013-09-03 17:15:31 -0700462 if (EC_GET(STATE, epctx) == 2) {
Nico Huber90292652013-06-13 14:37:15 +0200463 const int cc = xhci_cmd_reset_endpoint(xhci, slot_id, ep_id);
464 if (cc != CC_SUCCESS) {
465 xhci_debug("Reset Endpoint Command failed: %d\n", cc);
466 return 1;
467 }
468 }
469
470 /* Clear TT buffer for bulk and control endpoints behind a TT */
471 const int hub = dev->hub;
472 if (hub && dev->speed < HIGH_SPEED &&
473 dev->controller->devices[hub]->speed == HIGH_SPEED)
474 /* TODO */;
475
Nico Huber90292652013-06-13 14:37:15 +0200476 /* Reset transfer ring if the endpoint is in the right state */
Julius Werner1f864342013-09-03 17:15:31 -0700477 const unsigned ep_state = EC_GET(STATE, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200478 if (ep_state == 3 || ep_state == 4) {
Julius Werner1f864342013-09-03 17:15:31 -0700479 transfer_ring_t *const tr =
480 xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200481 const int cc = xhci_cmd_set_tr_dq(xhci, slot_id, ep_id,
482 tr->ring, 1);
483 if (cc != CC_SUCCESS) {
484 xhci_debug("Set TR Dequeue Command failed: %d\n", cc);
485 return 1;
486 }
487 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
488 }
489
490 xhci_debug("Finished resetting ID %d EP %d (ep state: %d)\n",
Julius Werner1f864342013-09-03 17:15:31 -0700491 slot_id, ep_id, EC_GET(STATE, epctx));
Nico Huber90292652013-06-13 14:37:15 +0200492
493 return 0;
494}
495
496static void
497xhci_enqueue_trb(transfer_ring_t *const tr)
498{
499 const int chain = TRB_GET(CH, tr->cur);
500 TRB_SET(C, tr->cur, tr->pcs);
501 ++tr->cur;
502
503 while (TRB_GET(TT, tr->cur) == TRB_LINK) {
504 xhci_spew("Handling LINK pointer\n");
505 const int tc = TRB_GET(TC, tr->cur);
506 TRB_SET(CH, tr->cur, chain);
507 TRB_SET(C, tr->cur, tr->pcs);
508 tr->cur = phys_to_virt(tr->cur->ptr_low);
509 if (tc)
510 tr->pcs ^= 1;
511 }
512}
513
514static void
515xhci_enqueue_td(transfer_ring_t *const tr, const int ep, const size_t mps,
516 const int dalen, void *const data, const int dir)
517{
518 trb_t *trb = NULL; /* cur TRB */
519 u8 *cur_start = data; /* cur data pointer */
520 size_t length = dalen; /* remaining bytes */
521 size_t packets = (length + mps - 1) / mps; /* remaining packets */
522 size_t residue = 0; /* residue from last TRB */
523 size_t trb_count = 0; /* TRBs added so far */
524
525 while (length || !trb_count /* enqueue at least one */) {
526 const size_t cur_end = ((size_t)cur_start + 0x10000) & ~0xffff;
527 size_t cur_length = cur_end - (size_t)cur_start;
528 if (length < cur_length) {
529 cur_length = length;
530 packets = 0;
531 length = 0;
532 } else {
533 packets -= (residue + cur_length) / mps;
534 residue = (residue + cur_length) % mps;
535 length -= cur_length;
536 }
537
538 trb = tr->cur;
539 xhci_clear_trb(trb, tr->pcs);
540 trb->ptr_low = virt_to_phys(cur_start);
541 TRB_SET(TL, trb, cur_length);
542 TRB_SET(TDS, trb, packets);
Julius Werner83da5012013-09-27 12:45:11 -0700543 TRB_SET(CH, trb, 1);
Nico Huber90292652013-06-13 14:37:15 +0200544
545 /* Check for first, data stage TRB */
546 if (!trb_count && ep == 1) {
547 TRB_SET(DIR, trb, dir);
548 TRB_SET(TT, trb, TRB_DATA_STAGE);
549 } else {
550 TRB_SET(TT, trb, TRB_NORMAL);
551 }
552
Nico Huber90292652013-06-13 14:37:15 +0200553 xhci_enqueue_trb(tr);
554
555 cur_start += cur_length;
556 ++trb_count;
557 }
Julius Werner83da5012013-09-27 12:45:11 -0700558
559 trb = tr->cur;
560 xhci_clear_trb(trb, tr->pcs);
561 trb->ptr_low = virt_to_phys(trb); /* for easier debugging only */
562 TRB_SET(TT, trb, TRB_EVENT_DATA);
563 TRB_SET(IOC, trb, 1);
564
565 xhci_enqueue_trb(tr);
Nico Huber90292652013-06-13 14:37:15 +0200566}
567
568static int
569xhci_control(usbdev_t *const dev, const direction_t dir,
570 const int drlen, void *const devreq,
Julius Werner1f864342013-09-03 17:15:31 -0700571 const int dalen, unsigned char *const src)
Nico Huber90292652013-06-13 14:37:15 +0200572{
Julius Werner1f864342013-09-03 17:15:31 -0700573 unsigned char *data = src;
Nico Huber90292652013-06-13 14:37:15 +0200574 xhci_t *const xhci = XHCI_INST(dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700575 epctx_t *const epctx = xhci->dev[dev->address].ctx.ep0;
576 transfer_ring_t *const tr = xhci->dev[dev->address].transfer_rings[1];
Nico Huber90292652013-06-13 14:37:15 +0200577
578 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700579 if ((off + dalen) > ((TRANSFER_RING_SIZE - 4) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200580 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800581 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200582 }
583
Julius Werner49ba2832013-09-26 15:13:44 -0700584 /* Reset endpoint if it's not running */
Julius Werner1f864342013-09-03 17:15:31 -0700585 const unsigned ep_state = EC_GET(STATE, epctx);
Julius Werner49ba2832013-09-26 15:13:44 -0700586 if (ep_state > 1) {
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700587 if (xhci_reset_endpoint(dev, NULL))
Julius Wernere9738db2013-02-21 13:41:40 -0800588 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200589 }
590
Julius Werner1f864342013-09-03 17:15:31 -0700591 if (dalen && !dma_coherent(src)) {
592 data = xhci->dma_buffer;
593 if (dalen > DMA_SIZE) {
594 xhci_debug("Control transfer too large: %d\n", dalen);
595 return -1;
596 }
597 if (dir == OUT)
598 memcpy(data, src, dalen);
599 }
600
Nico Huber90292652013-06-13 14:37:15 +0200601 /* Fill and enqueue setup TRB */
602 trb_t *const setup = tr->cur;
603 xhci_clear_trb(setup, tr->pcs);
604 setup->ptr_low = ((u32 *)devreq)[0];
605 setup->ptr_high = ((u32 *)devreq)[1];
606 TRB_SET(TL, setup, 8);
607 TRB_SET(TRT, setup, (dalen)
608 ? ((dir == OUT) ? TRB_TRT_OUT_DATA : TRB_TRT_IN_DATA)
609 : TRB_TRT_NO_DATA);
610 TRB_SET(TT, setup, TRB_SETUP_STAGE);
611 TRB_SET(IDT, setup, 1);
612 TRB_SET(IOC, setup, 1);
613 xhci_enqueue_trb(tr);
614
615 /* Fill and enqueue data TRBs (if any) */
616 if (dalen) {
Julius Werner1f864342013-09-03 17:15:31 -0700617 const unsigned mps = EC_GET(MPS, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200618 const unsigned dt_dir = (dir == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
619 xhci_enqueue_td(tr, 1, mps, dalen, data, dt_dir);
620 }
621
622 /* Fill status TRB */
623 trb_t *const status = tr->cur;
624 xhci_clear_trb(status, tr->pcs);
625 TRB_SET(DIR, status, (dir == OUT) ? TRB_DIR_IN : TRB_DIR_OUT);
626 TRB_SET(TT, status, TRB_STATUS_STAGE);
627 TRB_SET(IOC, status, 1);
628 xhci_enqueue_trb(tr);
629
630 /* Ring doorbell for EP0 */
631 xhci->dbreg[dev->address] = 1;
632
633 /* Wait for transfer events */
Julius Wernere9738db2013-02-21 13:41:40 -0800634 int i, transferred = 0;
Nico Huber90292652013-06-13 14:37:15 +0200635 const int n_stages = 2 + !!dalen;
636 for (i = 0; i < n_stages; ++i) {
637 const int ret = xhci_wait_for_transfer(xhci, dev->address, 1);
Julius Wernere9738db2013-02-21 13:41:40 -0800638 transferred += ret;
639 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200640 if (ret == TIMEOUT) {
641 xhci_debug("Stopping ID %d EP 1\n",
642 dev->address);
643 xhci_cmd_stop_endpoint(xhci, dev->address, 1);
644 }
645 xhci_debug("Stage %d/%d failed: %d\n"
646 " trb ring: @%p\n"
647 " setup trb: @%p\n"
648 " status trb: @%p\n"
649 " ep state: %d -> %d\n"
650 " usbsts: 0x%08"PRIx32"\n",
651 i, n_stages, ret,
652 tr->ring, setup, status,
Julius Werner1f864342013-09-03 17:15:31 -0700653 ep_state, EC_GET(STATE, epctx),
Nico Huber90292652013-06-13 14:37:15 +0200654 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800655 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200656 }
657 }
658
Julius Werner1f864342013-09-03 17:15:31 -0700659 if (dir == IN && data != src)
660 memcpy(src, data, transferred);
Julius Wernere9738db2013-02-21 13:41:40 -0800661 return transferred;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000662}
663
664/* finalize == 1: if data is of packet aligned size, add a zero length packet */
665static int
Julius Werner1f864342013-09-03 17:15:31 -0700666xhci_bulk(endpoint_t *const ep, const int size, u8 *const src,
Nico Huber90292652013-06-13 14:37:15 +0200667 const int finalize)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000668{
Nico Huber90292652013-06-13 14:37:15 +0200669 /* finalize: Hopefully the xHCI controller always does this.
670 We have no control over the packets. */
671
Julius Werner1f864342013-09-03 17:15:31 -0700672 u8 *data = src;
Nico Huber90292652013-06-13 14:37:15 +0200673 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700674 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200675 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700676 epctx_t *const epctx = xhci->dev[slot_id].ctx.ep[ep_id];
677 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200678
679 const size_t off = (size_t)data & 0xffff;
Julius Werner83da5012013-09-27 12:45:11 -0700680 if ((off + size) > ((TRANSFER_RING_SIZE - 2) << 16)) {
Nico Huber90292652013-06-13 14:37:15 +0200681 xhci_debug("Unsupported transfer size\n");
Julius Wernere9738db2013-02-21 13:41:40 -0800682 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200683 }
684
Julius Werner1f864342013-09-03 17:15:31 -0700685 if (!dma_coherent(src)) {
686 data = xhci->dma_buffer;
687 if (size > DMA_SIZE) {
688 xhci_debug("Bulk transfer too large: %d\n", size);
689 return -1;
690 }
691 if (ep->direction == OUT)
692 memcpy(data, src, size);
693 }
694
Julius Werner49ba2832013-09-26 15:13:44 -0700695 /* Reset endpoint if it's not running */
Julius Werner1f864342013-09-03 17:15:31 -0700696 const unsigned ep_state = EC_GET(STATE, epctx);
Julius Werner49ba2832013-09-26 15:13:44 -0700697 if (ep_state > 1) {
Shawn Nematbakhshc666e552014-04-02 09:14:32 -0700698 if (xhci_reset_endpoint(ep->dev, ep))
Julius Wernere9738db2013-02-21 13:41:40 -0800699 return -1;
Nico Huber90292652013-06-13 14:37:15 +0200700 }
701
702 /* Enqueue transfer and ring doorbell */
Julius Werner1f864342013-09-03 17:15:31 -0700703 const unsigned mps = EC_GET(MPS, epctx);
Nico Huber90292652013-06-13 14:37:15 +0200704 const unsigned dir = (ep->direction == OUT) ? TRB_DIR_OUT : TRB_DIR_IN;
705 xhci_enqueue_td(tr, ep_id, mps, size, data, dir);
706 xhci->dbreg[ep->dev->address] = ep_id;
707
708 /* Wait for transfer event */
709 const int ret = xhci_wait_for_transfer(xhci, ep->dev->address, ep_id);
Julius Wernere9738db2013-02-21 13:41:40 -0800710 if (ret < 0) {
Nico Huber90292652013-06-13 14:37:15 +0200711 if (ret == TIMEOUT) {
712 xhci_debug("Stopping ID %d EP %d\n",
713 ep->dev->address, ep_id);
714 xhci_cmd_stop_endpoint(xhci, ep->dev->address, ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200715 }
716 xhci_debug("Bulk transfer failed: %d\n"
717 " ep state: %d -> %d\n"
718 " usbsts: 0x%08"PRIx32"\n",
719 ret, ep_state,
Julius Werner1f864342013-09-03 17:15:31 -0700720 EC_GET(STATE, epctx),
Nico Huber90292652013-06-13 14:37:15 +0200721 xhci->opreg->usbsts);
Julius Wernere9738db2013-02-21 13:41:40 -0800722 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200723 }
724
Julius Werner1f864342013-09-03 17:15:31 -0700725 if (ep->direction == IN && data != src)
726 memcpy(src, data, ret);
Julius Wernere9738db2013-02-21 13:41:40 -0800727 return ret;
Nico Huber90292652013-06-13 14:37:15 +0200728}
729
730static trb_t *
731xhci_next_trb(trb_t *cur, int *const pcs)
732{
733 ++cur;
734 while (TRB_GET(TT, cur) == TRB_LINK) {
735 if (pcs && TRB_GET(TC, cur))
736 *pcs ^= 1;
737 cur = phys_to_virt(cur->ptr_low);
738 }
739 return cur;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000740}
741
742/* create and hook-up an intr queue into device schedule */
Nico Huber90292652013-06-13 14:37:15 +0200743static void *
744xhci_create_intr_queue(endpoint_t *const ep,
745 const int reqsize, const int reqcount,
746 const int reqtiming)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000747{
Nico Huber90292652013-06-13 14:37:15 +0200748 /* reqtiming: We ignore it and use the interval from the
749 endpoint descriptor configured earlier. */
750
751 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700752 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200753 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700754 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200755
756 if (reqcount > (TRANSFER_RING_SIZE - 2)) {
757 xhci_debug("reqcount is too high, at most %d supported\n",
758 TRANSFER_RING_SIZE - 2);
759 return NULL;
760 }
761 if (reqsize > 0x10000) {
762 xhci_debug("reqsize is too large, at most 64KiB supported\n");
763 return NULL;
764 }
Julius Werner1f864342013-09-03 17:15:31 -0700765 if (xhci->dev[slot_id].interrupt_queues[ep_id]) {
Nico Huber90292652013-06-13 14:37:15 +0200766 xhci_debug("Only one interrupt queue per endpoint supported\n");
767 return NULL;
768 }
769
770 /* Allocate intrq structure and reqdata chunks */
771
772 intrq_t *const intrq = malloc(sizeof(*intrq));
773 if (!intrq) {
774 xhci_debug("Out of memory\n");
775 return NULL;
776 }
777
778 int i;
779 int pcs = tr->pcs;
780 trb_t *cur = tr->cur;
781 for (i = 0; i < reqcount; ++i) {
782 if (TRB_GET(C, cur) == pcs) {
783 xhci_debug("Not enough empty TRBs\n");
784 goto _free_return;
785 }
786 void *const reqdata = xhci_align(1, reqsize);
787 if (!reqdata) {
788 xhci_debug("Out of memory\n");
789 goto _free_return;
790 }
791 xhci_clear_trb(cur, pcs);
792 cur->ptr_low = virt_to_phys(reqdata);
793 cur->ptr_high = 0;
794 TRB_SET(TL, cur, reqsize);
795 TRB_SET(TT, cur, TRB_NORMAL);
796 TRB_SET(ISP, cur, 1);
797 TRB_SET(IOC, cur, 1);
798
799 cur = xhci_next_trb(cur, &pcs);
800 }
801
802 intrq->size = reqsize;
803 intrq->count = reqcount;
804 intrq->next = tr->cur;
805 intrq->ready = NULL;
806 intrq->ep = ep;
Julius Werner1f864342013-09-03 17:15:31 -0700807 xhci->dev[slot_id].interrupt_queues[ep_id] = intrq;
Nico Huber90292652013-06-13 14:37:15 +0200808
809 /* Now enqueue all the prepared TRBs but the last
810 and ring the doorbell. */
811 for (i = 0; i < (reqcount - 1); ++i)
812 xhci_enqueue_trb(tr);
Julius Werner1f864342013-09-03 17:15:31 -0700813 xhci->dbreg[slot_id] = ep_id;
Nico Huber90292652013-06-13 14:37:15 +0200814
815 return intrq;
816
817_free_return:
818 cur = tr->cur;
819 for (--i; i >= 0; --i) {
820 free(phys_to_virt(cur->ptr_low));
821 cur = xhci_next_trb(cur, NULL);
822 }
823 free(intrq);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000824 return NULL;
825}
826
827/* remove queue from device schedule, dropping all data that came in */
828static void
Nico Huber90292652013-06-13 14:37:15 +0200829xhci_destroy_intr_queue(endpoint_t *const ep, void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000830{
Nico Huber90292652013-06-13 14:37:15 +0200831 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
Julius Werner1f864342013-09-03 17:15:31 -0700832 const int slot_id = ep->dev->address;
Nico Huber90292652013-06-13 14:37:15 +0200833 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700834 transfer_ring_t *const tr = xhci->dev[slot_id].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200835
836 intrq_t *const intrq = (intrq_t *)q;
837
838 /* Make sure the endpoint is stopped */
Julius Werner1f864342013-09-03 17:15:31 -0700839 if (EC_GET(STATE, xhci->dev[slot_id].ctx.ep[ep_id]) == 1) {
840 const int cc = xhci_cmd_stop_endpoint(xhci, slot_id, ep_id);
Nico Huber90292652013-06-13 14:37:15 +0200841 if (cc != CC_SUCCESS)
842 xhci_debug("Warning: Failed to stop endpoint\n");
843 }
844
845 /* Process all remaining transfer events */
846 xhci_handle_events(xhci);
847
848 /* Free all pending transfers and the interrupt queue structure */
849 int i;
850 for (i = 0; i < intrq->count; ++i) {
851 free(phys_to_virt(intrq->next->ptr_low));
852 intrq->next = xhci_next_trb(intrq->next, NULL);
853 }
Julius Werner1f864342013-09-03 17:15:31 -0700854 xhci->dev[slot_id].interrupt_queues[ep_id] = NULL;
Nico Huber90292652013-06-13 14:37:15 +0200855 free((void *)intrq);
856
857 /* Reset the controller's dequeue pointer and reinitialize the ring */
Julius Werner1f864342013-09-03 17:15:31 -0700858 xhci_cmd_set_tr_dq(xhci, slot_id, ep_id, tr->ring, 1);
Nico Huber90292652013-06-13 14:37:15 +0200859 xhci_init_cycle_ring(tr, TRANSFER_RING_SIZE);
Patrick Georgi6615ef32010-08-13 09:18:58 +0000860}
861
862/* read one intr-packet from queue, if available. extend the queue for new input.
863 return NULL if nothing new available.
864 Recommended use: while (data=poll_intr_queue(q)) process(data);
865 */
Nico Huber90292652013-06-13 14:37:15 +0200866static u8 *
867xhci_poll_intr_queue(void *const q)
Patrick Georgi6615ef32010-08-13 09:18:58 +0000868{
Nico Huber90292652013-06-13 14:37:15 +0200869 if (!q)
870 return NULL;
871
872 intrq_t *const intrq = (intrq_t *)q;
873 endpoint_t *const ep = intrq->ep;
874 xhci_t *const xhci = XHCI_INST(ep->dev->controller);
875
876 /* TODO: Reset interrupt queue if it gets halted? */
877
878 xhci_handle_events(xhci);
879
880 u8 *reqdata = NULL;
881 while (!reqdata && intrq->ready) {
882 const int ep_id = xhci_ep_id(ep);
Julius Werner1f864342013-09-03 17:15:31 -0700883 transfer_ring_t *const tr =
884 xhci->dev[ep->dev->address].transfer_rings[ep_id];
Nico Huber90292652013-06-13 14:37:15 +0200885
886 /* Fetch the request's buffer */
887 reqdata = phys_to_virt(intrq->next->ptr_low);
888
889 /* Enqueue the last (spare) TRB and ring doorbell */
890 xhci_enqueue_trb(tr);
891 xhci->dbreg[ep->dev->address] = ep_id;
892
893 /* Reuse the current buffer for the next spare TRB */
894 xhci_clear_trb(tr->cur, tr->pcs);
895 tr->cur->ptr_low = virt_to_phys(reqdata);
896 tr->cur->ptr_high = 0;
897 TRB_SET(TL, tr->cur, intrq->size);
898 TRB_SET(TT, tr->cur, TRB_NORMAL);
899 TRB_SET(ISP, tr->cur, 1);
900 TRB_SET(IOC, tr->cur, 1);
901
902 /* Check if anything was transferred */
903 const size_t read = TRB_GET(TL, intrq->next);
904 if (!read)
905 reqdata = NULL;
906 else if (read < intrq->size)
907 /* At least zero it, poll interface is rather limited */
908 memset(reqdata + read, 0x00, intrq->size - read);
909
910 /* Advance the interrupt queue */
911 if (intrq->ready == intrq->next)
912 /* This was last TRB being ready */
913 intrq->ready = NULL;
914 intrq->next = xhci_next_trb(intrq->next, NULL);
915 }
916
917 return reqdata;
Patrick Georgi6615ef32010-08-13 09:18:58 +0000918}