blob: 99911e727e0b7950e4a11dec17bb17f08151e5e4 [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/irq.h>
24#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070025#include <linux/moduleparam.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070026
27#include "xhci.h"
28
29#define DRIVER_AUTHOR "Sarah Sharp"
30#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
31
Sarah Sharpb0567b32009-08-07 14:04:36 -070032/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
33static int link_quirk;
34module_param(link_quirk, int, S_IRUGO | S_IWUSR);
35MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
36
Sarah Sharp66d4ead2009-04-27 19:52:28 -070037/* TODO: copied from ehci-hcd.c - can this be refactored? */
38/*
39 * handshake - spin reading hc until handshake completes or fails
40 * @ptr: address of hc register to be read
41 * @mask: bits to look at in result of read
42 * @done: value of those bits when handshake succeeds
43 * @usec: timeout in microseconds
44 *
45 * Returns negative errno, or zero on success
46 *
47 * Success happens when the "mask" bits have the specified value (hardware
48 * handshake done). There are two failure modes: "usec" have passed (major
49 * hardware flakeout), or the register reads as all-ones (hardware removed).
50 */
51static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
52 u32 mask, u32 done, int usec)
53{
54 u32 result;
55
56 do {
57 result = xhci_readl(xhci, ptr);
58 if (result == ~(u32)0) /* card removed */
59 return -ENODEV;
60 result &= mask;
61 if (result == done)
62 return 0;
63 udelay(1);
64 usec--;
65 } while (usec > 0);
66 return -ETIMEDOUT;
67}
68
69/*
70 * Force HC into halt state.
71 *
72 * Disable any IRQs and clear the run/stop bit.
73 * HC will complete any current and actively pipelined transactions, and
74 * should halt within 16 microframes of the run/stop bit being cleared.
75 * Read HC Halted bit in the status register to see when the HC is finished.
76 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
77 */
78int xhci_halt(struct xhci_hcd *xhci)
79{
80 u32 halted;
81 u32 cmd;
82 u32 mask;
83
84 xhci_dbg(xhci, "// Halt the HC\n");
85 /* Disable all interrupts from the host controller */
86 mask = ~(XHCI_IRQS);
87 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
88 if (!halted)
89 mask &= ~CMD_RUN;
90
91 cmd = xhci_readl(xhci, &xhci->op_regs->command);
92 cmd &= mask;
93 xhci_writel(xhci, cmd, &xhci->op_regs->command);
94
95 return handshake(xhci, &xhci->op_regs->status,
96 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
97}
98
99/*
100 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
101 *
102 * This resets pipelines, timers, counters, state machines, etc.
103 * Transactions will be terminated immediately, and operational registers
104 * will be set to their defaults.
105 */
106int xhci_reset(struct xhci_hcd *xhci)
107{
108 u32 command;
109 u32 state;
110
111 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700112 if ((state & STS_HALT) == 0) {
113 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
114 return 0;
115 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700116
117 xhci_dbg(xhci, "// Reset the HC\n");
118 command = xhci_readl(xhci, &xhci->op_regs->command);
119 command |= CMD_RESET;
120 xhci_writel(xhci, command, &xhci->op_regs->command);
121 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
122 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
123
124 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
125}
126
127/*
128 * Stop the HC from processing the endpoint queues.
129 */
130static void xhci_quiesce(struct xhci_hcd *xhci)
131{
132 /*
133 * Queues are per endpoint, so we need to disable an endpoint or slot.
134 *
135 * To disable a slot, we need to insert a disable slot command on the
136 * command ring and ring the doorbell. This will also free any internal
137 * resources associated with the slot (which might not be what we want).
138 *
139 * A Release Endpoint command sounds better - doesn't free internal HC
140 * memory, but removes the endpoints from the schedule and releases the
141 * bandwidth, disables the doorbells, and clears the endpoint enable
142 * flag. Usually used prior to a set interface command.
143 *
144 * TODO: Implement after command ring code is done.
145 */
146 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
147 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
148}
149
150#if 0
151/* Set up MSI-X table for entry 0 (may claim other entries later) */
152static int xhci_setup_msix(struct xhci_hcd *xhci)
153{
154 int ret;
155 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
156
157 xhci->msix_count = 0;
158 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
159 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
160 if (!xhci->msix_entries) {
161 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
162 return -ENOMEM;
163 }
164 xhci->msix_entries[0].entry = 0;
165
166 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
167 if (ret) {
168 xhci_err(xhci, "Failed to enable MSI-X\n");
169 goto free_entries;
170 }
171
172 /*
173 * Pass the xhci pointer value as the request_irq "cookie".
174 * If more irqs are added, this will need to be unique for each one.
175 */
176 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
177 "xHCI", xhci_to_hcd(xhci));
178 if (ret) {
179 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
180 goto disable_msix;
181 }
182 xhci_dbg(xhci, "Finished setting up MSI-X\n");
183 return 0;
184
185disable_msix:
186 pci_disable_msix(pdev);
187free_entries:
188 kfree(xhci->msix_entries);
189 xhci->msix_entries = NULL;
190 return ret;
191}
192
193/* XXX: code duplication; can xhci_setup_msix call this? */
194/* Free any IRQs and disable MSI-X */
195static void xhci_cleanup_msix(struct xhci_hcd *xhci)
196{
197 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
198 if (!xhci->msix_entries)
199 return;
200
201 free_irq(xhci->msix_entries[0].vector, xhci);
202 pci_disable_msix(pdev);
203 kfree(xhci->msix_entries);
204 xhci->msix_entries = NULL;
205 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
206}
207#endif
208
209/*
210 * Initialize memory for HCD and xHC (one-time init).
211 *
212 * Program the PAGESIZE register, initialize the device context array, create
213 * device contexts (?), set up a command ring segment (or two?), create event
214 * ring (one for now).
215 */
216int xhci_init(struct usb_hcd *hcd)
217{
218 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
219 int retval = 0;
220
221 xhci_dbg(xhci, "xhci_init\n");
222 spin_lock_init(&xhci->lock);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700223 if (link_quirk) {
224 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
225 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
226 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700227 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700228 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700229 retval = xhci_mem_init(xhci, GFP_KERNEL);
230 xhci_dbg(xhci, "Finished xhci_init\n");
231
232 return retval;
233}
234
235/*
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700236 * Called in interrupt context when there might be work
237 * queued on the event ring
238 *
239 * xhci->lock must be held by caller.
240 */
241static void xhci_work(struct xhci_hcd *xhci)
242{
243 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700244 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700245
246 /*
247 * Clear the op reg interrupt status first,
248 * so we can receive interrupts from other MSI-X interrupters.
249 * Write 1 to clear the interrupt status.
250 */
251 temp = xhci_readl(xhci, &xhci->op_regs->status);
252 temp |= STS_EINT;
253 xhci_writel(xhci, temp, &xhci->op_regs->status);
254 /* FIXME when MSI-X is supported and there are multiple vectors */
255 /* Clear the MSI-X event interrupt status */
256
257 /* Acknowledge the interrupt */
258 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
259 temp |= 0x3;
260 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
261 /* Flush posted writes */
262 xhci_readl(xhci, &xhci->ir_set->irq_pending);
263
264 /* FIXME this should be a delayed service routine that clears the EHB */
Stephen Rothwellb7258a42009-04-29 19:02:47 -0700265 xhci_handle_event(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700266
Sarah Sharp2d831092009-07-27 12:03:40 -0700267 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
Sarah Sharp8e595a52009-07-27 12:03:31 -0700268 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp2d831092009-07-27 12:03:40 -0700269 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700270 /* Flush posted writes -- FIXME is this necessary? */
271 xhci_readl(xhci, &xhci->ir_set->irq_pending);
272}
273
274/*-------------------------------------------------------------------------*/
275
276/*
277 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
278 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
279 * indicators of an event TRB error, but we check the status *first* to be safe.
280 */
281irqreturn_t xhci_irq(struct usb_hcd *hcd)
282{
283 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
284 u32 temp, temp2;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700285 union xhci_trb *trb;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700286
287 spin_lock(&xhci->lock);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700288 trb = xhci->event_ring->dequeue;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700289 /* Check if the xHC generated the interrupt, or the irq is shared */
290 temp = xhci_readl(xhci, &xhci->op_regs->status);
291 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Sarah Sharpfcf8f572009-07-27 12:04:01 -0700292 if (temp == 0xffffffff && temp2 == 0xffffffff)
293 goto hw_died;
294
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700295 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
296 spin_unlock(&xhci->lock);
297 return IRQ_NONE;
298 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700299 xhci_dbg(xhci, "op reg status = %08x\n", temp);
300 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
301 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
302 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
303 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
304 lower_32_bits(trb->link.segment_ptr),
305 upper_32_bits(trb->link.segment_ptr),
306 (unsigned int) trb->link.intr_target,
307 (unsigned int) trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700308
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700309 if (temp & STS_FATAL) {
310 xhci_warn(xhci, "WARNING: Host System Error\n");
311 xhci_halt(xhci);
Sarah Sharpfcf8f572009-07-27 12:04:01 -0700312hw_died:
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700313 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
Sarah Sharpc96a2b82009-04-29 19:05:40 -0700314 spin_unlock(&xhci->lock);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700315 return -ESHUTDOWN;
316 }
317
318 xhci_work(xhci);
319 spin_unlock(&xhci->lock);
320
321 return IRQ_HANDLED;
322}
323
324#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700325void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700326{
327 unsigned long flags;
328 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700329 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700330 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
331 int i, j;
332
333 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
334
335 spin_lock_irqsave(&xhci->lock, flags);
336 temp = xhci_readl(xhci, &xhci->op_regs->status);
337 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
338 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
339 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
340 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
341 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
342 xhci->error_bitmask = 0;
343 xhci_dbg(xhci, "Event ring:\n");
344 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
345 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700346 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
347 temp_64 &= ~ERST_PTR_MASK;
348 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700349 xhci_dbg(xhci, "Command ring:\n");
350 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
351 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
352 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700353 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700354 if (!xhci->devs[i])
355 continue;
356 for (j = 0; j < 31; ++j) {
357 struct xhci_ring *ring = xhci->devs[i]->eps[j].ring;
358 if (!ring)
359 continue;
360 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
361 xhci_debug_segment(xhci, ring->deq_seg);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700362 }
363 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700364
365 if (xhci->noops_submitted != NUM_TEST_NOOPS)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700366 if (xhci_setup_one_noop(xhci))
367 xhci_ring_cmd_db(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700368 spin_unlock_irqrestore(&xhci->lock, flags);
369
370 if (!xhci->zombie)
371 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
372 else
373 xhci_dbg(xhci, "Quit polling the event ring.\n");
374}
375#endif
376
377/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700378 * Start the HC after it was halted.
379 *
380 * This function is called by the USB core when the HC driver is added.
381 * Its opposite is xhci_stop().
382 *
383 * xhci_init() must be called once before this function can be called.
384 * Reset the HC, enable device slot contexts, program DCBAAP, and
385 * set command ring pointer and event ring pointer.
386 *
387 * Setup MSI-X vectors and enable interrupts.
388 */
389int xhci_run(struct usb_hcd *hcd)
390{
391 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700392 u64 temp_64;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700393 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700394 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700395
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700396 hcd->uses_new_polling = 1;
397 hcd->poll_rh = 0;
398
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700399 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700400#if 0 /* FIXME: MSI not setup yet */
401 /* Do this at the very last minute */
402 ret = xhci_setup_msix(xhci);
403 if (!ret)
404 return ret;
405
406 return -ENOSYS;
407#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700408#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
409 init_timer(&xhci->event_ring_timer);
410 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700411 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700412 /* Poll the event ring */
413 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
414 xhci->zombie = 0;
415 xhci_dbg(xhci, "Setting event ring polling timer\n");
416 add_timer(&xhci->event_ring_timer);
417#endif
418
Sarah Sharp66e49d82009-07-27 12:03:46 -0700419 xhci_dbg(xhci, "Command ring memory map follows:\n");
420 xhci_debug_ring(xhci, xhci->cmd_ring);
421 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
422 xhci_dbg_cmd_ptrs(xhci);
423
424 xhci_dbg(xhci, "ERST memory map follows:\n");
425 xhci_dbg_erst(xhci, &xhci->erst);
426 xhci_dbg(xhci, "Event ring:\n");
427 xhci_debug_ring(xhci, xhci->event_ring);
428 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
429 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
430 temp_64 &= ~ERST_PTR_MASK;
431 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
432
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700433 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
434 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700435 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700436 temp |= (u32) 160;
437 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
438
439 /* Set the HCD state before we enable the irqs */
440 hcd->state = HC_STATE_RUNNING;
441 temp = xhci_readl(xhci, &xhci->op_regs->command);
442 temp |= (CMD_EIE);
443 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
444 temp);
445 xhci_writel(xhci, temp, &xhci->op_regs->command);
446
447 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700448 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
449 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700450 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
451 &xhci->ir_set->irq_pending);
452 xhci_print_ir_set(xhci, xhci->ir_set, 0);
453
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700454 if (NUM_TEST_NOOPS > 0)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700455 doorbell = xhci_setup_one_noop(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700456
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700457 temp = xhci_readl(xhci, &xhci->op_regs->command);
458 temp |= (CMD_RUN);
459 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
460 temp);
461 xhci_writel(xhci, temp, &xhci->op_regs->command);
462 /* Flush PCI posted writes */
463 temp = xhci_readl(xhci, &xhci->op_regs->command);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700464 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700465 if (doorbell)
466 (*doorbell)(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700467
468 xhci_dbg(xhci, "Finished xhci_run\n");
469 return 0;
470}
471
472/*
473 * Stop xHCI driver.
474 *
475 * This function is called by the USB core when the HC driver is removed.
476 * Its opposite is xhci_run().
477 *
478 * Disable device contexts, disable IRQs, and quiesce the HC.
479 * Reset the HC, finish any completed transactions, and cleanup memory.
480 */
481void xhci_stop(struct usb_hcd *hcd)
482{
483 u32 temp;
484 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
485
486 spin_lock_irq(&xhci->lock);
487 if (HC_IS_RUNNING(hcd->state))
488 xhci_quiesce(xhci);
489 xhci_halt(xhci);
490 xhci_reset(xhci);
491 spin_unlock_irq(&xhci->lock);
492
493#if 0 /* No MSI yet */
494 xhci_cleanup_msix(xhci);
495#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700496#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
497 /* Tell the event ring poll function not to reschedule */
498 xhci->zombie = 1;
499 del_timer_sync(&xhci->event_ring_timer);
500#endif
501
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700502 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
503 temp = xhci_readl(xhci, &xhci->op_regs->status);
504 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
505 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
506 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
507 &xhci->ir_set->irq_pending);
508 xhci_print_ir_set(xhci, xhci->ir_set, 0);
509
510 xhci_dbg(xhci, "cleaning up memory\n");
511 xhci_mem_cleanup(xhci);
512 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
513 xhci_readl(xhci, &xhci->op_regs->status));
514}
515
516/*
517 * Shutdown HC (not bus-specific)
518 *
519 * This is called when the machine is rebooting or halting. We assume that the
520 * machine will be powered off, and the HC's internal state will be reset.
521 * Don't bother to free memory.
522 */
523void xhci_shutdown(struct usb_hcd *hcd)
524{
525 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
526
527 spin_lock_irq(&xhci->lock);
528 xhci_halt(xhci);
529 spin_unlock_irq(&xhci->lock);
530
531#if 0
532 xhci_cleanup_msix(xhci);
533#endif
534
535 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
536 xhci_readl(xhci, &xhci->op_regs->status));
537}
538
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700539/*-------------------------------------------------------------------------*/
540
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700541/**
542 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
543 * HCDs. Find the index for an endpoint given its descriptor. Use the return
544 * value to right shift 1 for the bitmask.
545 *
546 * Index = (epnum * 2) + direction - 1,
547 * where direction = 0 for OUT, 1 for IN.
548 * For control endpoints, the IN index is used (OUT index is unused), so
549 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
550 */
551unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
552{
553 unsigned int index;
554 if (usb_endpoint_xfer_control(desc))
555 index = (unsigned int) (usb_endpoint_num(desc)*2);
556 else
557 index = (unsigned int) (usb_endpoint_num(desc)*2) +
558 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
559 return index;
560}
561
Sarah Sharpf94e01862009-04-27 19:58:38 -0700562/* Find the flag for this endpoint (for use in the control context). Use the
563 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
564 * bit 1, etc.
565 */
566unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
567{
568 return 1 << (xhci_get_endpoint_index(desc) + 1);
569}
570
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700571/* Find the flag for this endpoint (for use in the control context). Use the
572 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
573 * bit 1, etc.
574 */
575unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
576{
577 return 1 << (ep_index + 1);
578}
579
Sarah Sharpf94e01862009-04-27 19:58:38 -0700580/* Compute the last valid endpoint context index. Basically, this is the
581 * endpoint index plus one. For slot contexts with more than valid endpoint,
582 * we find the most significant bit set in the added contexts flags.
583 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
584 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
585 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700586unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700587{
588 return fls(added_ctxs) - 1;
589}
590
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700591/* Returns 1 if the arguments are OK;
592 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
593 */
594int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
595 struct usb_host_endpoint *ep, int check_ep, const char *func) {
596 if (!hcd || (check_ep && !ep) || !udev) {
597 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
598 func);
599 return -EINVAL;
600 }
601 if (!udev->parent) {
602 printk(KERN_DEBUG "xHCI %s called for root hub\n",
603 func);
604 return 0;
605 }
606 if (!udev->slot_id) {
607 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
608 func);
609 return -EINVAL;
610 }
611 return 1;
612}
613
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700614static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700615 struct usb_device *udev, struct xhci_command *command,
616 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700617
618/*
619 * Full speed devices may have a max packet size greater than 8 bytes, but the
620 * USB core doesn't know that until it reads the first 8 bytes of the
621 * descriptor. If the usb_device's max packet size changes after that point,
622 * we need to issue an evaluate context command and wait on it.
623 */
624static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
625 unsigned int ep_index, struct urb *urb)
626{
627 struct xhci_container_ctx *in_ctx;
628 struct xhci_container_ctx *out_ctx;
629 struct xhci_input_control_ctx *ctrl_ctx;
630 struct xhci_ep_ctx *ep_ctx;
631 int max_packet_size;
632 int hw_max_packet_size;
633 int ret = 0;
634
635 out_ctx = xhci->devs[slot_id]->out_ctx;
636 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
637 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
638 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
639 if (hw_max_packet_size != max_packet_size) {
640 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
641 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
642 max_packet_size);
643 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
644 hw_max_packet_size);
645 xhci_dbg(xhci, "Issuing evaluate context command.\n");
646
647 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700648 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
649 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700650 in_ctx = xhci->devs[slot_id]->in_ctx;
651 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
652 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
653 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
654
655 /* Set up the input context flags for the command */
656 /* FIXME: This won't work if a non-default control endpoint
657 * changes max packet sizes.
658 */
659 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
660 ctrl_ctx->add_flags = EP0_FLAG;
661 ctrl_ctx->drop_flags = 0;
662
663 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
664 xhci_dbg_ctx(xhci, in_ctx, ep_index);
665 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
666 xhci_dbg_ctx(xhci, out_ctx, ep_index);
667
Sarah Sharp913a8a32009-09-04 10:53:13 -0700668 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
669 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700670
671 /* Clean up the input context for later use by bandwidth
672 * functions.
673 */
674 ctrl_ctx->add_flags = SLOT_FLAG;
675 }
676 return ret;
677}
678
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700679/*
680 * non-error returns are a promise to giveback() the urb later
681 * we drop ownership so next owner (or urb unlink) can get it
682 */
683int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
684{
685 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
686 unsigned long flags;
687 int ret = 0;
688 unsigned int slot_id, ep_index;
689
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700690
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700691 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
692 return -EINVAL;
693
694 slot_id = urb->dev->slot_id;
695 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700696
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700697 if (!xhci->devs || !xhci->devs[slot_id]) {
698 if (!in_interrupt())
699 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
Sarah Sharpc7959fb2009-04-29 19:06:36 -0700700 ret = -EINVAL;
701 goto exit;
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700702 }
703 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
704 if (!in_interrupt())
705 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
706 ret = -ESHUTDOWN;
707 goto exit;
708 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700709 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
710 /* Check to see if the max packet size for the default control
711 * endpoint changed during FS device enumeration
712 */
713 if (urb->dev->speed == USB_SPEED_FULL) {
714 ret = xhci_check_maxpacket(xhci, slot_id,
715 ep_index, urb);
716 if (ret < 0)
717 return ret;
718 }
719
Sarah Sharpb11069f2009-07-27 12:03:23 -0700720 /* We have a spinlock and interrupts disabled, so we must pass
721 * atomic context to this function, which may allocate memory.
722 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700723 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpb11069f2009-07-27 12:03:23 -0700724 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700725 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700726 spin_unlock_irqrestore(&xhci->lock, flags);
727 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
728 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpb11069f2009-07-27 12:03:23 -0700729 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700730 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700731 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -0700732 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
733 spin_lock_irqsave(&xhci->lock, flags);
734 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
735 slot_id, ep_index);
736 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700737 } else {
Sarah Sharpb10de142009-04-27 19:58:50 -0700738 ret = -EINVAL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700739 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700740exit:
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700741 return ret;
742}
743
Sarah Sharpae636742009-04-29 19:02:31 -0700744/*
745 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
746 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
747 * should pick up where it left off in the TD, unless a Set Transfer Ring
748 * Dequeue Pointer is issued.
749 *
750 * The TRBs that make up the buffers for the canceled URB will be "removed" from
751 * the ring. Since the ring is a contiguous structure, they can't be physically
752 * removed. Instead, there are two options:
753 *
754 * 1) If the HC is in the middle of processing the URB to be canceled, we
755 * simply move the ring's dequeue pointer past those TRBs using the Set
756 * Transfer Ring Dequeue Pointer command. This will be the common case,
757 * when drivers timeout on the last submitted URB and attempt to cancel.
758 *
759 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
760 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
761 * HC will need to invalidate the any TRBs it has cached after the stop
762 * endpoint command, as noted in the xHCI 0.95 errata.
763 *
764 * 3) The TD may have completed by the time the Stop Endpoint Command
765 * completes, so software needs to handle that case too.
766 *
767 * This function should protect against the TD enqueueing code ringing the
768 * doorbell while this code is waiting for a Stop Endpoint command to complete.
769 * It also needs to account for multiple cancellations on happening at the same
770 * time for the same endpoint.
771 *
772 * Note that this function can be called in any context, or so says
773 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700774 */
775int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
776{
Sarah Sharpae636742009-04-29 19:02:31 -0700777 unsigned long flags;
778 int ret;
779 struct xhci_hcd *xhci;
780 struct xhci_td *td;
781 unsigned int ep_index;
782 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700783 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700784
785 xhci = hcd_to_xhci(hcd);
786 spin_lock_irqsave(&xhci->lock, flags);
787 /* Make sure the URB hasn't completed or been unlinked already */
788 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
789 if (ret || !urb->hcpriv)
790 goto done;
791
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700792 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700793 xhci_dbg(xhci, "Event ring:\n");
794 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700795 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700796 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
797 ep_ring = ep->ring;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700798 xhci_dbg(xhci, "Endpoint ring:\n");
799 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700800 td = (struct xhci_td *) urb->hcpriv;
801
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700802 ep->cancels_pending++;
803 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700804 /* Queue a stop endpoint command, but only if this is
805 * the first cancellation to be handled.
806 */
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700807 if (ep->cancels_pending == 1) {
Sarah Sharp23e3be12009-04-29 19:05:20 -0700808 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
809 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700810 }
811done:
812 spin_unlock_irqrestore(&xhci->lock, flags);
813 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700814}
815
Sarah Sharpf94e01862009-04-27 19:58:38 -0700816/* Drop an endpoint from a new bandwidth configuration for this device.
817 * Only one call to this function is allowed per endpoint before
818 * check_bandwidth() or reset_bandwidth() must be called.
819 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
820 * add the endpoint to the schedule with possibly new parameters denoted by a
821 * different endpoint descriptor in usb_host_endpoint.
822 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
823 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700824 *
825 * The USB core will not allow URBs to be queued to an endpoint that is being
826 * disabled, so there's no need for mutual exclusion to protect
827 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700828 */
829int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
830 struct usb_host_endpoint *ep)
831{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700832 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -0700833 struct xhci_container_ctx *in_ctx, *out_ctx;
834 struct xhci_input_control_ctx *ctrl_ctx;
835 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700836 unsigned int last_ctx;
837 unsigned int ep_index;
838 struct xhci_ep_ctx *ep_ctx;
839 u32 drop_flag;
840 u32 new_add_flags, new_drop_flags, new_slot_info;
841 int ret;
842
843 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700844 if (ret <= 0)
845 return ret;
846 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700847 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700848
849 drop_flag = xhci_get_endpoint_flag(&ep->desc);
850 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
851 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
852 __func__, drop_flag);
853 return 0;
854 }
855
Sarah Sharpf94e01862009-04-27 19:58:38 -0700856 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
857 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
858 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700859 return -EINVAL;
860 }
861
862 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -0700863 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
864 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700865 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -0700866 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700867 /* If the HC already knows the endpoint is disabled,
868 * or the HCD has noted it is disabled, ignore this request
869 */
870 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
John Yound115b042009-07-27 12:05:15 -0700871 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700872 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
873 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700874 return 0;
875 }
876
John Yound115b042009-07-27 12:05:15 -0700877 ctrl_ctx->drop_flags |= drop_flag;
878 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700879
John Yound115b042009-07-27 12:05:15 -0700880 ctrl_ctx->add_flags = ~drop_flag;
881 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700882
John Yound115b042009-07-27 12:05:15 -0700883 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
884 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700885 /* Update the last valid endpoint context, if we deleted the last one */
John Yound115b042009-07-27 12:05:15 -0700886 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
887 slot_ctx->dev_info &= ~LAST_CTX_MASK;
888 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700889 }
John Yound115b042009-07-27 12:05:15 -0700890 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700891
892 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
893
Sarah Sharpf94e01862009-04-27 19:58:38 -0700894 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
895 (unsigned int) ep->desc.bEndpointAddress,
896 udev->slot_id,
897 (unsigned int) new_drop_flags,
898 (unsigned int) new_add_flags,
899 (unsigned int) new_slot_info);
900 return 0;
901}
902
903/* Add an endpoint to a new possible bandwidth configuration for this device.
904 * Only one call to this function is allowed per endpoint before
905 * check_bandwidth() or reset_bandwidth() must be called.
906 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
907 * add the endpoint to the schedule with possibly new parameters denoted by a
908 * different endpoint descriptor in usb_host_endpoint.
909 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
910 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700911 *
912 * The USB core will not allow URBs to be queued to an endpoint until the
913 * configuration or alt setting is installed in the device, so there's no need
914 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700915 */
916int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
917 struct usb_host_endpoint *ep)
918{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700919 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -0700920 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700921 unsigned int ep_index;
922 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -0700923 struct xhci_slot_ctx *slot_ctx;
924 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700925 u32 added_ctxs;
926 unsigned int last_ctx;
927 u32 new_add_flags, new_drop_flags, new_slot_info;
928 int ret = 0;
929
930 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -0700931 if (ret <= 0) {
932 /* So we won't queue a reset ep command for a root hub */
933 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700934 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -0700935 }
Sarah Sharpf94e01862009-04-27 19:58:38 -0700936 xhci = hcd_to_xhci(hcd);
937
938 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
939 last_ctx = xhci_last_valid_endpoint(added_ctxs);
940 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
941 /* FIXME when we have to issue an evaluate endpoint command to
942 * deal with ep0 max packet size changing once we get the
943 * descriptors
944 */
945 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
946 __func__, added_ctxs);
947 return 0;
948 }
949
Sarah Sharpf94e01862009-04-27 19:58:38 -0700950 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
951 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
952 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700953 return -EINVAL;
954 }
955
956 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -0700957 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
958 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700959 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -0700960 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700961 /* If the HCD has already noted the endpoint is enabled,
962 * ignore this request.
963 */
John Yound115b042009-07-27 12:05:15 -0700964 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700965 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
966 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700967 return 0;
968 }
969
Sarah Sharpf88ba782009-05-14 11:44:22 -0700970 /*
971 * Configuration and alternate setting changes must be done in
972 * process context, not interrupt context (or so documenation
973 * for usb_set_interface() and usb_set_configuration() claim).
974 */
975 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
976 udev, ep, GFP_KERNEL) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -0700977 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
978 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700979 return -ENOMEM;
980 }
981
John Yound115b042009-07-27 12:05:15 -0700982 ctrl_ctx->add_flags |= added_ctxs;
983 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700984
985 /* If xhci_endpoint_disable() was called for this endpoint, but the
986 * xHC hasn't been notified yet through the check_bandwidth() call,
987 * this re-adds a new state for the endpoint from the new endpoint
988 * descriptors. We must drop and re-add this endpoint, so we leave the
989 * drop flags alone.
990 */
John Yound115b042009-07-27 12:05:15 -0700991 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700992
John Yound115b042009-07-27 12:05:15 -0700993 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700994 /* Update the last valid endpoint context, if we just added one past */
John Yound115b042009-07-27 12:05:15 -0700995 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
996 slot_ctx->dev_info &= ~LAST_CTX_MASK;
997 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700998 }
John Yound115b042009-07-27 12:05:15 -0700999 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001000
Sarah Sharpa1587d92009-07-27 12:03:15 -07001001 /* Store the usb_device pointer for later use */
1002 ep->hcpriv = udev;
1003
Sarah Sharpf94e01862009-04-27 19:58:38 -07001004 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1005 (unsigned int) ep->desc.bEndpointAddress,
1006 udev->slot_id,
1007 (unsigned int) new_drop_flags,
1008 (unsigned int) new_add_flags,
1009 (unsigned int) new_slot_info);
1010 return 0;
1011}
1012
John Yound115b042009-07-27 12:05:15 -07001013static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001014{
John Yound115b042009-07-27 12:05:15 -07001015 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001016 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001017 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001018 int i;
1019
1020 /* When a device's add flag and drop flag are zero, any subsequent
1021 * configure endpoint command will leave that endpoint's state
1022 * untouched. Make sure we don't leave any old state in the input
1023 * endpoint contexts.
1024 */
John Yound115b042009-07-27 12:05:15 -07001025 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1026 ctrl_ctx->drop_flags = 0;
1027 ctrl_ctx->add_flags = 0;
1028 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1029 slot_ctx->dev_info &= ~LAST_CTX_MASK;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001030 /* Endpoint 0 is always valid */
John Yound115b042009-07-27 12:05:15 -07001031 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001032 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001033 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001034 ep_ctx->ep_info = 0;
1035 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001036 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001037 ep_ctx->tx_info = 0;
1038 }
1039}
1040
Sarah Sharpf2217e82009-08-07 14:04:43 -07001041static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001042 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001043{
1044 int ret;
1045
Sarah Sharp913a8a32009-09-04 10:53:13 -07001046 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001047 case COMP_ENOMEM:
1048 dev_warn(&udev->dev, "Not enough host controller resources "
1049 "for new device state.\n");
1050 ret = -ENOMEM;
1051 /* FIXME: can we allocate more resources for the HC? */
1052 break;
1053 case COMP_BW_ERR:
1054 dev_warn(&udev->dev, "Not enough bandwidth "
1055 "for new device state.\n");
1056 ret = -ENOSPC;
1057 /* FIXME: can we go back to the old state? */
1058 break;
1059 case COMP_TRB_ERR:
1060 /* the HCD set up something wrong */
1061 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1062 "add flag = 1, "
1063 "and endpoint is not disabled.\n");
1064 ret = -EINVAL;
1065 break;
1066 case COMP_SUCCESS:
1067 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1068 ret = 0;
1069 break;
1070 default:
1071 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001072 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001073 ret = -EINVAL;
1074 break;
1075 }
1076 return ret;
1077}
1078
1079static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001080 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001081{
1082 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001083 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001084
Sarah Sharp913a8a32009-09-04 10:53:13 -07001085 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001086 case COMP_EINVAL:
1087 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1088 "context command.\n");
1089 ret = -EINVAL;
1090 break;
1091 case COMP_EBADSLT:
1092 dev_warn(&udev->dev, "WARN: slot not enabled for"
1093 "evaluate context command.\n");
1094 case COMP_CTX_STATE:
1095 dev_warn(&udev->dev, "WARN: invalid context state for "
1096 "evaluate context command.\n");
1097 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1098 ret = -EINVAL;
1099 break;
1100 case COMP_SUCCESS:
1101 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1102 ret = 0;
1103 break;
1104 default:
1105 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001106 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001107 ret = -EINVAL;
1108 break;
1109 }
1110 return ret;
1111}
1112
1113/* Issue a configure endpoint command or evaluate context command
1114 * and wait for it to finish.
1115 */
1116static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001117 struct usb_device *udev,
1118 struct xhci_command *command,
1119 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001120{
1121 int ret;
1122 int timeleft;
1123 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001124 struct xhci_container_ctx *in_ctx;
1125 struct completion *cmd_completion;
1126 int *cmd_status;
1127 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001128
1129 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001130 virt_dev = xhci->devs[udev->slot_id];
1131 if (command) {
1132 in_ctx = command->in_ctx;
1133 cmd_completion = command->completion;
1134 cmd_status = &command->status;
1135 command->command_trb = xhci->cmd_ring->enqueue;
1136 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1137 } else {
1138 in_ctx = virt_dev->in_ctx;
1139 cmd_completion = &virt_dev->cmd_completion;
1140 cmd_status = &virt_dev->cmd_status;
1141 }
1142
Sarah Sharpf2217e82009-08-07 14:04:43 -07001143 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001144 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1145 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001146 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07001147 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001148 udev->slot_id);
1149 if (ret < 0) {
1150 spin_unlock_irqrestore(&xhci->lock, flags);
1151 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1152 return -ENOMEM;
1153 }
1154 xhci_ring_cmd_db(xhci);
1155 spin_unlock_irqrestore(&xhci->lock, flags);
1156
1157 /* Wait for the configure endpoint command to complete */
1158 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07001159 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001160 USB_CTRL_SET_TIMEOUT);
1161 if (timeleft <= 0) {
1162 xhci_warn(xhci, "%s while waiting for %s command\n",
1163 timeleft == 0 ? "Timeout" : "Signal",
1164 ctx_change == 0 ?
1165 "configure endpoint" :
1166 "evaluate context");
1167 /* FIXME cancel the configure endpoint command */
1168 return -ETIME;
1169 }
1170
1171 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001172 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1173 return xhci_evaluate_context_result(xhci, udev, cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001174}
1175
Sarah Sharpf88ba782009-05-14 11:44:22 -07001176/* Called after one or more calls to xhci_add_endpoint() or
1177 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1178 * to call xhci_reset_bandwidth().
1179 *
1180 * Since we are in the middle of changing either configuration or
1181 * installing a new alt setting, the USB core won't allow URBs to be
1182 * enqueued for any endpoint on the old config or interface. Nothing
1183 * else should be touching the xhci->devs[slot_id] structure, so we
1184 * don't need to take the xhci->lock for manipulating that.
1185 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001186int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1187{
1188 int i;
1189 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001190 struct xhci_hcd *xhci;
1191 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07001192 struct xhci_input_control_ctx *ctrl_ctx;
1193 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001194
1195 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1196 if (ret <= 0)
1197 return ret;
1198 xhci = hcd_to_xhci(hcd);
1199
Sarah Sharpf94e01862009-04-27 19:58:38 -07001200 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1201 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1202 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001203 return -EINVAL;
1204 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001205 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001206 virt_dev = xhci->devs[udev->slot_id];
1207
1208 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07001209 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1210 ctrl_ctx->add_flags |= SLOT_FLAG;
1211 ctrl_ctx->add_flags &= ~EP0_FLAG;
1212 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1213 ctrl_ctx->drop_flags &= ~EP0_FLAG;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001214 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07001215 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1216 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1217 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001218
Sarah Sharp913a8a32009-09-04 10:53:13 -07001219 ret = xhci_configure_endpoint(xhci, udev, NULL,
1220 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001221 if (ret) {
1222 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001223 return ret;
1224 }
1225
1226 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07001227 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1228 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001229
John Yound115b042009-07-27 12:05:15 -07001230 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001231 /* Free any old rings */
1232 for (i = 1; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001233 if (virt_dev->eps[i].new_ring) {
1234 xhci_ring_free(xhci, virt_dev->eps[i].ring);
1235 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1236 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001237 }
1238 }
1239
Sarah Sharpf94e01862009-04-27 19:58:38 -07001240 return ret;
1241}
1242
1243void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1244{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001245 struct xhci_hcd *xhci;
1246 struct xhci_virt_device *virt_dev;
1247 int i, ret;
1248
1249 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1250 if (ret <= 0)
1251 return;
1252 xhci = hcd_to_xhci(hcd);
1253
Sarah Sharpf94e01862009-04-27 19:58:38 -07001254 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1255 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1256 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001257 return;
1258 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001259 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001260 virt_dev = xhci->devs[udev->slot_id];
1261 /* Free any rings allocated for added endpoints */
1262 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001263 if (virt_dev->eps[i].new_ring) {
1264 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1265 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001266 }
1267 }
John Yound115b042009-07-27 12:05:15 -07001268 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001269}
1270
Sarah Sharp5270b952009-09-04 10:53:11 -07001271static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001272 struct xhci_container_ctx *in_ctx,
1273 struct xhci_container_ctx *out_ctx,
1274 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07001275{
1276 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001277 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001278 ctrl_ctx->add_flags = add_flags;
1279 ctrl_ctx->drop_flags = drop_flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001280 xhci_slot_copy(xhci, in_ctx, out_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001281 ctrl_ctx->add_flags |= SLOT_FLAG;
1282
Sarah Sharp913a8a32009-09-04 10:53:13 -07001283 xhci_dbg(xhci, "Input Context:\n");
1284 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07001285}
1286
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001287void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1288 unsigned int slot_id, unsigned int ep_index,
1289 struct xhci_dequeue_state *deq_state)
1290{
1291 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001292 struct xhci_ep_ctx *ep_ctx;
1293 u32 added_ctxs;
1294 dma_addr_t addr;
1295
Sarah Sharp913a8a32009-09-04 10:53:13 -07001296 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1297 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001298 in_ctx = xhci->devs[slot_id]->in_ctx;
1299 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1300 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1301 deq_state->new_deq_ptr);
1302 if (addr == 0) {
1303 xhci_warn(xhci, "WARN Cannot submit config ep after "
1304 "reset ep command\n");
1305 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1306 deq_state->new_deq_seg,
1307 deq_state->new_deq_ptr);
1308 return;
1309 }
1310 ep_ctx->deq = addr | deq_state->new_cycle_state;
1311
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001312 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001313 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1314 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001315}
1316
Sarah Sharp82d10092009-08-07 14:04:52 -07001317void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001318 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07001319{
1320 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001321 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07001322
1323 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001324 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07001325 /* We need to move the HW's dequeue pointer past this TD,
1326 * or it will attempt to resend it on the next doorbell ring.
1327 */
1328 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001329 ep_index, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001330 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07001331
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001332 /* HW with the reset endpoint quirk will use the saved dequeue state to
1333 * issue a configure endpoint command later.
1334 */
1335 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1336 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001337 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001338 ep_index, &deq_state);
1339 } else {
1340 /* Better hope no one uses the input context between now and the
1341 * reset endpoint completion!
1342 */
1343 xhci_dbg(xhci, "Setting up input context for "
1344 "configure endpoint command\n");
1345 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1346 ep_index, &deq_state);
1347 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001348}
1349
Sarah Sharpa1587d92009-07-27 12:03:15 -07001350/* Deal with stalled endpoints. The core should have sent the control message
1351 * to clear the halt condition. However, we need to make the xHCI hardware
1352 * reset its sequence number, since a device will expect a sequence number of
1353 * zero after the halt condition is cleared.
1354 * Context: in_interrupt
1355 */
1356void xhci_endpoint_reset(struct usb_hcd *hcd,
1357 struct usb_host_endpoint *ep)
1358{
1359 struct xhci_hcd *xhci;
1360 struct usb_device *udev;
1361 unsigned int ep_index;
1362 unsigned long flags;
1363 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001364 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001365
1366 xhci = hcd_to_xhci(hcd);
1367 udev = (struct usb_device *) ep->hcpriv;
1368 /* Called with a root hub endpoint (or an endpoint that wasn't added
1369 * with xhci_add_endpoint()
1370 */
1371 if (!ep->hcpriv)
1372 return;
1373 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001374 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1375 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001376 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1377 ep->desc.bEndpointAddress);
1378 return;
1379 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001380 if (usb_endpoint_xfer_control(&ep->desc)) {
1381 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1382 return;
1383 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001384
1385 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1386 spin_lock_irqsave(&xhci->lock, flags);
1387 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001388 /*
1389 * Can't change the ring dequeue pointer until it's transitioned to the
1390 * stopped state, which is only upon a successful reset endpoint
1391 * command. Better hope that last command worked!
1392 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07001393 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001394 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1395 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001396 xhci_ring_cmd_db(xhci);
1397 }
1398 spin_unlock_irqrestore(&xhci->lock, flags);
1399
1400 if (ret)
1401 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1402}
1403
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001404/*
1405 * At this point, the struct usb_device is about to go away, the device has
1406 * disconnected, and all traffic has been stopped and the endpoints have been
1407 * disabled. Free any HC data structures associated with that device.
1408 */
1409void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1410{
1411 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1412 unsigned long flags;
1413
1414 if (udev->slot_id == 0)
1415 return;
1416
1417 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001418 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001419 spin_unlock_irqrestore(&xhci->lock, flags);
1420 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1421 return;
1422 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001423 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001424 spin_unlock_irqrestore(&xhci->lock, flags);
1425 /*
1426 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07001427 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001428 */
1429}
1430
1431/*
1432 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
1433 * timed out, or allocating memory failed. Returns 1 on success.
1434 */
1435int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
1436{
1437 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1438 unsigned long flags;
1439 int timeleft;
1440 int ret;
1441
1442 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001443 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001444 if (ret) {
1445 spin_unlock_irqrestore(&xhci->lock, flags);
1446 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1447 return 0;
1448 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001449 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001450 spin_unlock_irqrestore(&xhci->lock, flags);
1451
1452 /* XXX: how much time for xHC slot assignment? */
1453 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1454 USB_CTRL_SET_TIMEOUT);
1455 if (timeleft <= 0) {
1456 xhci_warn(xhci, "%s while waiting for a slot\n",
1457 timeleft == 0 ? "Timeout" : "Signal");
1458 /* FIXME cancel the enable slot request */
1459 return 0;
1460 }
1461
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001462 if (!xhci->slot_id) {
1463 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001464 return 0;
1465 }
Sarah Sharpf88ba782009-05-14 11:44:22 -07001466 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001467 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
1468 /* Disable slot, if we can do it without mem alloc */
1469 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07001470 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001471 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
1472 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001473 spin_unlock_irqrestore(&xhci->lock, flags);
1474 return 0;
1475 }
1476 udev->slot_id = xhci->slot_id;
1477 /* Is this a LS or FS device under a HS hub? */
1478 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001479 return 1;
1480}
1481
1482/*
1483 * Issue an Address Device command (which will issue a SetAddress request to
1484 * the device).
1485 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
1486 * we should only issue and wait on one address command at the same time.
1487 *
1488 * We add one to the device address issued by the hardware because the USB core
1489 * uses address 1 for the root hubs (even though they're not really devices).
1490 */
1491int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
1492{
1493 unsigned long flags;
1494 int timeleft;
1495 struct xhci_virt_device *virt_dev;
1496 int ret = 0;
1497 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07001498 struct xhci_slot_ctx *slot_ctx;
1499 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001500 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001501
1502 if (!udev->slot_id) {
1503 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
1504 return -EINVAL;
1505 }
1506
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001507 virt_dev = xhci->devs[udev->slot_id];
1508
1509 /* If this is a Set Address to an unconfigured device, setup ep 0 */
1510 if (!udev->config)
1511 xhci_setup_addressable_virt_dev(xhci, udev);
1512 /* Otherwise, assume the core has the device configured how it wants */
Sarah Sharp66e49d82009-07-27 12:03:46 -07001513 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07001514 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001515
Sarah Sharpf88ba782009-05-14 11:44:22 -07001516 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07001517 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
1518 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001519 if (ret) {
1520 spin_unlock_irqrestore(&xhci->lock, flags);
1521 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1522 return ret;
1523 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07001524 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001525 spin_unlock_irqrestore(&xhci->lock, flags);
1526
1527 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
1528 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1529 USB_CTRL_SET_TIMEOUT);
1530 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
1531 * the SetAddress() "recovery interval" required by USB and aborting the
1532 * command on a timeout.
1533 */
1534 if (timeleft <= 0) {
1535 xhci_warn(xhci, "%s while waiting for a slot\n",
1536 timeleft == 0 ? "Timeout" : "Signal");
1537 /* FIXME cancel the address device command */
1538 return -ETIME;
1539 }
1540
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001541 switch (virt_dev->cmd_status) {
1542 case COMP_CTX_STATE:
1543 case COMP_EBADSLT:
1544 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
1545 udev->slot_id);
1546 ret = -EINVAL;
1547 break;
1548 case COMP_TX_ERR:
1549 dev_warn(&udev->dev, "Device not responding to set address.\n");
1550 ret = -EPROTO;
1551 break;
1552 case COMP_SUCCESS:
1553 xhci_dbg(xhci, "Successful Address Device command\n");
1554 break;
1555 default:
1556 xhci_err(xhci, "ERROR: unexpected command completion "
1557 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07001558 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07001559 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001560 ret = -EINVAL;
1561 break;
1562 }
1563 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001564 return ret;
1565 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07001566 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
1567 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
1568 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001569 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07001570 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
1571 (unsigned long long)
1572 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001573 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07001574 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001575 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07001576 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001577 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07001578 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001579 /*
1580 * USB core uses address 1 for the roothubs, so we add one to the
1581 * address given back to us by the HC.
1582 */
John Yound115b042009-07-27 12:05:15 -07001583 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
1584 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001585 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07001586 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1587 ctrl_ctx->add_flags = 0;
1588 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001589
1590 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
1591 /* XXX Meh, not sure if anyone else but choose_address uses this. */
1592 set_bit(udev->devnum, udev->bus->devmap.devicemap);
1593
1594 return 0;
1595}
1596
Sarah Sharpac1c1b72009-09-04 10:53:20 -07001597/* Once a hub descriptor is fetched for a device, we need to update the xHC's
1598 * internal data structures for the device.
1599 */
1600int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
1601 struct usb_tt *tt, gfp_t mem_flags)
1602{
1603 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1604 struct xhci_virt_device *vdev;
1605 struct xhci_command *config_cmd;
1606 struct xhci_input_control_ctx *ctrl_ctx;
1607 struct xhci_slot_ctx *slot_ctx;
1608 unsigned long flags;
1609 unsigned think_time;
1610 int ret;
1611
1612 /* Ignore root hubs */
1613 if (!hdev->parent)
1614 return 0;
1615
1616 vdev = xhci->devs[hdev->slot_id];
1617 if (!vdev) {
1618 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
1619 return -EINVAL;
1620 }
1621 config_cmd = xhci_alloc_command(xhci, true, mem_flags);
1622 if (!config_cmd) {
1623 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1624 return -ENOMEM;
1625 }
1626
1627 spin_lock_irqsave(&xhci->lock, flags);
1628 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
1629 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
1630 ctrl_ctx->add_flags |= SLOT_FLAG;
1631 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
1632 slot_ctx->dev_info |= DEV_HUB;
1633 if (tt->multi)
1634 slot_ctx->dev_info |= DEV_MTT;
1635 if (xhci->hci_version > 0x95) {
1636 xhci_dbg(xhci, "xHCI version %x needs hub "
1637 "TT think time and number of ports\n",
1638 (unsigned int) xhci->hci_version);
1639 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
1640 /* Set TT think time - convert from ns to FS bit times.
1641 * 0 = 8 FS bit times, 1 = 16 FS bit times,
1642 * 2 = 24 FS bit times, 3 = 32 FS bit times.
1643 */
1644 think_time = tt->think_time;
1645 if (think_time != 0)
1646 think_time = (think_time / 666) - 1;
1647 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
1648 } else {
1649 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
1650 "TT think time or number of ports\n",
1651 (unsigned int) xhci->hci_version);
1652 }
1653 slot_ctx->dev_state = 0;
1654 spin_unlock_irqrestore(&xhci->lock, flags);
1655
1656 xhci_dbg(xhci, "Set up %s for hub device.\n",
1657 (xhci->hci_version > 0x95) ?
1658 "configure endpoint" : "evaluate context");
1659 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
1660 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
1661
1662 /* Issue and wait for the configure endpoint or
1663 * evaluate context command.
1664 */
1665 if (xhci->hci_version > 0x95)
1666 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
1667 false, false);
1668 else
1669 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
1670 true, false);
1671
1672 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
1673 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
1674
1675 xhci_free_command(xhci, config_cmd);
1676 return ret;
1677}
1678
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001679int xhci_get_frame(struct usb_hcd *hcd)
1680{
1681 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1682 /* EHCI mods by the periodic size. Why? */
1683 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
1684}
1685
1686MODULE_DESCRIPTION(DRIVER_DESC);
1687MODULE_AUTHOR(DRIVER_AUTHOR);
1688MODULE_LICENSE("GPL");
1689
1690static int __init xhci_hcd_init(void)
1691{
1692#ifdef CONFIG_PCI
1693 int retval = 0;
1694
1695 retval = xhci_register_pci();
1696
1697 if (retval < 0) {
1698 printk(KERN_DEBUG "Problem registering PCI driver.");
1699 return retval;
1700 }
1701#endif
Sarah Sharp98441972009-05-14 11:44:18 -07001702 /*
1703 * Check the compiler generated sizes of structures that must be laid
1704 * out in specific ways for hardware access.
1705 */
1706 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1707 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
1708 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
1709 /* xhci_device_control has eight fields, and also
1710 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
1711 */
Sarah Sharp98441972009-05-14 11:44:18 -07001712 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
1713 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
1714 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
1715 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
1716 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
1717 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
1718 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
1719 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001720 return 0;
1721}
1722module_init(xhci_hcd_init);
1723
1724static void __exit xhci_hcd_cleanup(void)
1725{
1726#ifdef CONFIG_PCI
1727 xhci_unregister_pci();
1728#endif
1729}
1730module_exit(xhci_hcd_cleanup);