blob: 2e370fea95902522bd9984636459c9da56e3639c [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>
Sarah Sharp8df75f42010-04-02 15:34:16 -070024#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070025#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070026#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070028
29#include "xhci.h"
30
31#define DRIVER_AUTHOR "Sarah Sharp"
32#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
33
Sarah Sharpb0567b32009-08-07 14:04:36 -070034/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
35static int link_quirk;
36module_param(link_quirk, int, S_IRUGO | S_IWUSR);
37MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
38
Sarah Sharp66d4ead2009-04-27 19:52:28 -070039/* TODO: copied from ehci-hcd.c - can this be refactored? */
40/*
41 * handshake - spin reading hc until handshake completes or fails
42 * @ptr: address of hc register to be read
43 * @mask: bits to look at in result of read
44 * @done: value of those bits when handshake succeeds
45 * @usec: timeout in microseconds
46 *
47 * Returns negative errno, or zero on success
48 *
49 * Success happens when the "mask" bits have the specified value (hardware
50 * handshake done). There are two failure modes: "usec" have passed (major
51 * hardware flakeout), or the register reads as all-ones (hardware removed).
52 */
53static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
54 u32 mask, u32 done, int usec)
55{
56 u32 result;
57
58 do {
59 result = xhci_readl(xhci, ptr);
60 if (result == ~(u32)0) /* card removed */
61 return -ENODEV;
62 result &= mask;
63 if (result == done)
64 return 0;
65 udelay(1);
66 usec--;
67 } while (usec > 0);
68 return -ETIMEDOUT;
69}
70
71/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070072 * Disable interrupts and begin the xHCI halting process.
73 */
74void xhci_quiesce(struct xhci_hcd *xhci)
75{
76 u32 halted;
77 u32 cmd;
78 u32 mask;
79
80 mask = ~(XHCI_IRQS);
81 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
82 if (!halted)
83 mask &= ~CMD_RUN;
84
85 cmd = xhci_readl(xhci, &xhci->op_regs->command);
86 cmd &= mask;
87 xhci_writel(xhci, cmd, &xhci->op_regs->command);
88}
89
90/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070091 * Force HC into halt state.
92 *
93 * Disable any IRQs and clear the run/stop bit.
94 * HC will complete any current and actively pipelined transactions, and
95 * should halt within 16 microframes of the run/stop bit being cleared.
96 * Read HC Halted bit in the status register to see when the HC is finished.
97 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
98 */
99int xhci_halt(struct xhci_hcd *xhci)
100{
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700102 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700103
104 return handshake(xhci, &xhci->op_regs->status,
105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
106}
107
108/*
109 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
110 *
111 * This resets pipelines, timers, counters, state machines, etc.
112 * Transactions will be terminated immediately, and operational registers
113 * will be set to their defaults.
114 */
115int xhci_reset(struct xhci_hcd *xhci)
116{
117 u32 command;
118 u32 state;
119
120 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700121 if ((state & STS_HALT) == 0) {
122 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
123 return 0;
124 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700125
126 xhci_dbg(xhci, "// Reset the HC\n");
127 command = xhci_readl(xhci, &xhci->op_regs->command);
128 command |= CMD_RESET;
129 xhci_writel(xhci, command, &xhci->op_regs->command);
130 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
131 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
132
133 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
134}
135
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700136
137#if 0
138/* Set up MSI-X table for entry 0 (may claim other entries later) */
139static int xhci_setup_msix(struct xhci_hcd *xhci)
140{
141 int ret;
142 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
143
144 xhci->msix_count = 0;
145 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
146 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
147 if (!xhci->msix_entries) {
148 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
149 return -ENOMEM;
150 }
151 xhci->msix_entries[0].entry = 0;
152
153 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
154 if (ret) {
155 xhci_err(xhci, "Failed to enable MSI-X\n");
156 goto free_entries;
157 }
158
159 /*
160 * Pass the xhci pointer value as the request_irq "cookie".
161 * If more irqs are added, this will need to be unique for each one.
162 */
163 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
164 "xHCI", xhci_to_hcd(xhci));
165 if (ret) {
166 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
167 goto disable_msix;
168 }
169 xhci_dbg(xhci, "Finished setting up MSI-X\n");
170 return 0;
171
172disable_msix:
173 pci_disable_msix(pdev);
174free_entries:
175 kfree(xhci->msix_entries);
176 xhci->msix_entries = NULL;
177 return ret;
178}
179
180/* XXX: code duplication; can xhci_setup_msix call this? */
181/* Free any IRQs and disable MSI-X */
182static void xhci_cleanup_msix(struct xhci_hcd *xhci)
183{
184 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
185 if (!xhci->msix_entries)
186 return;
187
188 free_irq(xhci->msix_entries[0].vector, xhci);
189 pci_disable_msix(pdev);
190 kfree(xhci->msix_entries);
191 xhci->msix_entries = NULL;
192 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
193}
194#endif
195
196/*
197 * Initialize memory for HCD and xHC (one-time init).
198 *
199 * Program the PAGESIZE register, initialize the device context array, create
200 * device contexts (?), set up a command ring segment (or two?), create event
201 * ring (one for now).
202 */
203int xhci_init(struct usb_hcd *hcd)
204{
205 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
206 int retval = 0;
207
208 xhci_dbg(xhci, "xhci_init\n");
209 spin_lock_init(&xhci->lock);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700210 if (link_quirk) {
211 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
212 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
213 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700214 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700215 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700216 retval = xhci_mem_init(xhci, GFP_KERNEL);
217 xhci_dbg(xhci, "Finished xhci_init\n");
218
219 return retval;
220}
221
222/*
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700223 * Called in interrupt context when there might be work
224 * queued on the event ring
225 *
226 * xhci->lock must be held by caller.
227 */
228static void xhci_work(struct xhci_hcd *xhci)
229{
230 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700231 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700232
233 /*
234 * Clear the op reg interrupt status first,
235 * so we can receive interrupts from other MSI-X interrupters.
236 * Write 1 to clear the interrupt status.
237 */
238 temp = xhci_readl(xhci, &xhci->op_regs->status);
239 temp |= STS_EINT;
240 xhci_writel(xhci, temp, &xhci->op_regs->status);
241 /* FIXME when MSI-X is supported and there are multiple vectors */
242 /* Clear the MSI-X event interrupt status */
243
244 /* Acknowledge the interrupt */
245 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
246 temp |= 0x3;
247 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
248 /* Flush posted writes */
249 xhci_readl(xhci, &xhci->ir_set->irq_pending);
250
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700251 if (xhci->xhc_state & XHCI_STATE_DYING)
252 xhci_dbg(xhci, "xHCI dying, ignoring interrupt. "
253 "Shouldn't IRQs be disabled?\n");
254 else
255 /* FIXME this should be a delayed service routine
256 * that clears the EHB.
257 */
258 xhci_handle_event(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700259
Sarah Sharp2d831092009-07-27 12:03:40 -0700260 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
Sarah Sharp8e595a52009-07-27 12:03:31 -0700261 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp2d831092009-07-27 12:03:40 -0700262 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700263 /* Flush posted writes -- FIXME is this necessary? */
264 xhci_readl(xhci, &xhci->ir_set->irq_pending);
265}
266
267/*-------------------------------------------------------------------------*/
268
269/*
270 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
271 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
272 * indicators of an event TRB error, but we check the status *first* to be safe.
273 */
274irqreturn_t xhci_irq(struct usb_hcd *hcd)
275{
276 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
277 u32 temp, temp2;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700278 union xhci_trb *trb;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700279
280 spin_lock(&xhci->lock);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700281 trb = xhci->event_ring->dequeue;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700282 /* Check if the xHC generated the interrupt, or the irq is shared */
283 temp = xhci_readl(xhci, &xhci->op_regs->status);
284 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Sarah Sharpfcf8f572009-07-27 12:04:01 -0700285 if (temp == 0xffffffff && temp2 == 0xffffffff)
286 goto hw_died;
287
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700288 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
289 spin_unlock(&xhci->lock);
290 return IRQ_NONE;
291 }
Sarah Sharp66e49d82009-07-27 12:03:46 -0700292 xhci_dbg(xhci, "op reg status = %08x\n", temp);
293 xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2);
294 xhci_dbg(xhci, "Event ring dequeue ptr:\n");
295 xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n",
296 (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb),
297 lower_32_bits(trb->link.segment_ptr),
298 upper_32_bits(trb->link.segment_ptr),
299 (unsigned int) trb->link.intr_target,
300 (unsigned int) trb->link.control);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700301
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700302 if (temp & STS_FATAL) {
303 xhci_warn(xhci, "WARNING: Host System Error\n");
304 xhci_halt(xhci);
Sarah Sharpfcf8f572009-07-27 12:04:01 -0700305hw_died:
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700306 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
Sarah Sharpc96a2b82009-04-29 19:05:40 -0700307 spin_unlock(&xhci->lock);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700308 return -ESHUTDOWN;
309 }
310
311 xhci_work(xhci);
312 spin_unlock(&xhci->lock);
313
314 return IRQ_HANDLED;
315}
316
317#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700318void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700319{
320 unsigned long flags;
321 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700322 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700323 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
324 int i, j;
325
326 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
327
328 spin_lock_irqsave(&xhci->lock, flags);
329 temp = xhci_readl(xhci, &xhci->op_regs->status);
330 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700331 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700332 xhci_dbg(xhci, "HW died, polling stopped.\n");
333 spin_unlock_irqrestore(&xhci->lock, flags);
334 return;
335 }
336
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700337 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
338 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
339 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
340 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
341 xhci->error_bitmask = 0;
342 xhci_dbg(xhci, "Event ring:\n");
343 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
344 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700345 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
346 temp_64 &= ~ERST_PTR_MASK;
347 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700348 xhci_dbg(xhci, "Command ring:\n");
349 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
350 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
351 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700352 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700353 if (!xhci->devs[i])
354 continue;
355 for (j = 0; j < 31; ++j) {
356 struct xhci_ring *ring = xhci->devs[i]->eps[j].ring;
357 if (!ring)
358 continue;
359 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
360 xhci_debug_segment(xhci, ring->deq_seg);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700361 }
362 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700363
364 if (xhci->noops_submitted != NUM_TEST_NOOPS)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700365 if (xhci_setup_one_noop(xhci))
366 xhci_ring_cmd_db(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700367 spin_unlock_irqrestore(&xhci->lock, flags);
368
369 if (!xhci->zombie)
370 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
371 else
372 xhci_dbg(xhci, "Quit polling the event ring.\n");
373}
374#endif
375
376/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700377 * Start the HC after it was halted.
378 *
379 * This function is called by the USB core when the HC driver is added.
380 * Its opposite is xhci_stop().
381 *
382 * xhci_init() must be called once before this function can be called.
383 * Reset the HC, enable device slot contexts, program DCBAAP, and
384 * set command ring pointer and event ring pointer.
385 *
386 * Setup MSI-X vectors and enable interrupts.
387 */
388int xhci_run(struct usb_hcd *hcd)
389{
390 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700391 u64 temp_64;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700392 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700393 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700394
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700395 hcd->uses_new_polling = 1;
396 hcd->poll_rh = 0;
397
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700398 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700399#if 0 /* FIXME: MSI not setup yet */
400 /* Do this at the very last minute */
401 ret = xhci_setup_msix(xhci);
402 if (!ret)
403 return ret;
404
405 return -ENOSYS;
406#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700407#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
408 init_timer(&xhci->event_ring_timer);
409 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700410 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700411 /* Poll the event ring */
412 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
413 xhci->zombie = 0;
414 xhci_dbg(xhci, "Setting event ring polling timer\n");
415 add_timer(&xhci->event_ring_timer);
416#endif
417
Sarah Sharp66e49d82009-07-27 12:03:46 -0700418 xhci_dbg(xhci, "Command ring memory map follows:\n");
419 xhci_debug_ring(xhci, xhci->cmd_ring);
420 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
421 xhci_dbg_cmd_ptrs(xhci);
422
423 xhci_dbg(xhci, "ERST memory map follows:\n");
424 xhci_dbg_erst(xhci, &xhci->erst);
425 xhci_dbg(xhci, "Event ring:\n");
426 xhci_debug_ring(xhci, xhci->event_ring);
427 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
428 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
429 temp_64 &= ~ERST_PTR_MASK;
430 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
431
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700432 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
433 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700434 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700435 temp |= (u32) 160;
436 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
437
438 /* Set the HCD state before we enable the irqs */
439 hcd->state = HC_STATE_RUNNING;
440 temp = xhci_readl(xhci, &xhci->op_regs->command);
441 temp |= (CMD_EIE);
442 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
443 temp);
444 xhci_writel(xhci, temp, &xhci->op_regs->command);
445
446 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700447 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
448 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700449 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
450 &xhci->ir_set->irq_pending);
451 xhci_print_ir_set(xhci, xhci->ir_set, 0);
452
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700453 if (NUM_TEST_NOOPS > 0)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700454 doorbell = xhci_setup_one_noop(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700455
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700456 temp = xhci_readl(xhci, &xhci->op_regs->command);
457 temp |= (CMD_RUN);
458 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
459 temp);
460 xhci_writel(xhci, temp, &xhci->op_regs->command);
461 /* Flush PCI posted writes */
462 temp = xhci_readl(xhci, &xhci->op_regs->command);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700463 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700464 if (doorbell)
465 (*doorbell)(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700466
467 xhci_dbg(xhci, "Finished xhci_run\n");
468 return 0;
469}
470
471/*
472 * Stop xHCI driver.
473 *
474 * This function is called by the USB core when the HC driver is removed.
475 * Its opposite is xhci_run().
476 *
477 * Disable device contexts, disable IRQs, and quiesce the HC.
478 * Reset the HC, finish any completed transactions, and cleanup memory.
479 */
480void xhci_stop(struct usb_hcd *hcd)
481{
482 u32 temp;
483 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
484
485 spin_lock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700486 xhci_halt(xhci);
487 xhci_reset(xhci);
488 spin_unlock_irq(&xhci->lock);
489
490#if 0 /* No MSI yet */
491 xhci_cleanup_msix(xhci);
492#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700493#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
494 /* Tell the event ring poll function not to reschedule */
495 xhci->zombie = 1;
496 del_timer_sync(&xhci->event_ring_timer);
497#endif
498
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700499 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
500 temp = xhci_readl(xhci, &xhci->op_regs->status);
501 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
502 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
503 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
504 &xhci->ir_set->irq_pending);
505 xhci_print_ir_set(xhci, xhci->ir_set, 0);
506
507 xhci_dbg(xhci, "cleaning up memory\n");
508 xhci_mem_cleanup(xhci);
509 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
510 xhci_readl(xhci, &xhci->op_regs->status));
511}
512
513/*
514 * Shutdown HC (not bus-specific)
515 *
516 * This is called when the machine is rebooting or halting. We assume that the
517 * machine will be powered off, and the HC's internal state will be reset.
518 * Don't bother to free memory.
519 */
520void xhci_shutdown(struct usb_hcd *hcd)
521{
522 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
523
524 spin_lock_irq(&xhci->lock);
525 xhci_halt(xhci);
526 spin_unlock_irq(&xhci->lock);
527
528#if 0
529 xhci_cleanup_msix(xhci);
530#endif
531
532 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
533 xhci_readl(xhci, &xhci->op_regs->status));
534}
535
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700536/*-------------------------------------------------------------------------*/
537
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700538/**
539 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
540 * HCDs. Find the index for an endpoint given its descriptor. Use the return
541 * value to right shift 1 for the bitmask.
542 *
543 * Index = (epnum * 2) + direction - 1,
544 * where direction = 0 for OUT, 1 for IN.
545 * For control endpoints, the IN index is used (OUT index is unused), so
546 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
547 */
548unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
549{
550 unsigned int index;
551 if (usb_endpoint_xfer_control(desc))
552 index = (unsigned int) (usb_endpoint_num(desc)*2);
553 else
554 index = (unsigned int) (usb_endpoint_num(desc)*2) +
555 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
556 return index;
557}
558
Sarah Sharpf94e01862009-04-27 19:58:38 -0700559/* Find the flag for this endpoint (for use in the control context). Use the
560 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
561 * bit 1, etc.
562 */
563unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
564{
565 return 1 << (xhci_get_endpoint_index(desc) + 1);
566}
567
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700568/* Find the flag for this endpoint (for use in the control context). Use the
569 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
570 * bit 1, etc.
571 */
572unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
573{
574 return 1 << (ep_index + 1);
575}
576
Sarah Sharpf94e01862009-04-27 19:58:38 -0700577/* Compute the last valid endpoint context index. Basically, this is the
578 * endpoint index plus one. For slot contexts with more than valid endpoint,
579 * we find the most significant bit set in the added contexts flags.
580 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
581 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
582 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700583unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700584{
585 return fls(added_ctxs) - 1;
586}
587
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700588/* Returns 1 if the arguments are OK;
589 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
590 */
591int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
592 struct usb_host_endpoint *ep, int check_ep, const char *func) {
593 if (!hcd || (check_ep && !ep) || !udev) {
594 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
595 func);
596 return -EINVAL;
597 }
598 if (!udev->parent) {
599 printk(KERN_DEBUG "xHCI %s called for root hub\n",
600 func);
601 return 0;
602 }
603 if (!udev->slot_id) {
604 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
605 func);
606 return -EINVAL;
607 }
608 return 1;
609}
610
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700611static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700612 struct usb_device *udev, struct xhci_command *command,
613 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700614
615/*
616 * Full speed devices may have a max packet size greater than 8 bytes, but the
617 * USB core doesn't know that until it reads the first 8 bytes of the
618 * descriptor. If the usb_device's max packet size changes after that point,
619 * we need to issue an evaluate context command and wait on it.
620 */
621static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
622 unsigned int ep_index, struct urb *urb)
623{
624 struct xhci_container_ctx *in_ctx;
625 struct xhci_container_ctx *out_ctx;
626 struct xhci_input_control_ctx *ctrl_ctx;
627 struct xhci_ep_ctx *ep_ctx;
628 int max_packet_size;
629 int hw_max_packet_size;
630 int ret = 0;
631
632 out_ctx = xhci->devs[slot_id]->out_ctx;
633 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
634 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
635 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
636 if (hw_max_packet_size != max_packet_size) {
637 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
638 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
639 max_packet_size);
640 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
641 hw_max_packet_size);
642 xhci_dbg(xhci, "Issuing evaluate context command.\n");
643
644 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700645 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
646 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700647 in_ctx = xhci->devs[slot_id]->in_ctx;
648 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
649 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
650 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
651
652 /* Set up the input context flags for the command */
653 /* FIXME: This won't work if a non-default control endpoint
654 * changes max packet sizes.
655 */
656 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
657 ctrl_ctx->add_flags = EP0_FLAG;
658 ctrl_ctx->drop_flags = 0;
659
660 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
661 xhci_dbg_ctx(xhci, in_ctx, ep_index);
662 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
663 xhci_dbg_ctx(xhci, out_ctx, ep_index);
664
Sarah Sharp913a8a32009-09-04 10:53:13 -0700665 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
666 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700667
668 /* Clean up the input context for later use by bandwidth
669 * functions.
670 */
671 ctrl_ctx->add_flags = SLOT_FLAG;
672 }
673 return ret;
674}
675
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700676/*
677 * non-error returns are a promise to giveback() the urb later
678 * we drop ownership so next owner (or urb unlink) can get it
679 */
680int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
681{
682 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
683 unsigned long flags;
684 int ret = 0;
685 unsigned int slot_id, ep_index;
686
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700687
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700688 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
689 return -EINVAL;
690
691 slot_id = urb->dev->slot_id;
692 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700693
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700694 if (!xhci->devs || !xhci->devs[slot_id]) {
695 if (!in_interrupt())
696 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
Sarah Sharpc7959fb2009-04-29 19:06:36 -0700697 ret = -EINVAL;
698 goto exit;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700699 }
700 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
701 if (!in_interrupt())
702 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
703 ret = -ESHUTDOWN;
704 goto exit;
705 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700706 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
707 /* Check to see if the max packet size for the default control
708 * endpoint changed during FS device enumeration
709 */
710 if (urb->dev->speed == USB_SPEED_FULL) {
711 ret = xhci_check_maxpacket(xhci, slot_id,
712 ep_index, urb);
713 if (ret < 0)
714 return ret;
715 }
716
Sarah Sharpb11069f2009-07-27 12:03:23 -0700717 /* We have a spinlock and interrupts disabled, so we must pass
718 * atomic context to this function, which may allocate memory.
719 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700720 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700721 if (xhci->xhc_state & XHCI_STATE_DYING)
722 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -0700723 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -0700724 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700725 spin_unlock_irqrestore(&xhci->lock, flags);
726 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
727 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700728 if (xhci->xhc_state & XHCI_STATE_DYING)
729 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -0700730 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
731 EP_GETTING_STREAMS) {
732 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
733 "is transitioning to using streams.\n");
734 ret = -EINVAL;
735 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
736 EP_GETTING_NO_STREAMS) {
737 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
738 "is transitioning to "
739 "not having streams.\n");
740 ret = -EINVAL;
741 } else {
742 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
743 slot_id, ep_index);
744 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700745 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -0700746 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
747 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700748 if (xhci->xhc_state & XHCI_STATE_DYING)
749 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -0700750 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
751 slot_id, ep_index);
752 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700753 } else {
Sarah Sharpb10de142009-04-27 19:58:50 -0700754 ret = -EINVAL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700755 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700756exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700757 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700758dying:
759 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
760 "non-responsive xHCI host.\n",
761 urb->ep->desc.bEndpointAddress, urb);
762 spin_unlock_irqrestore(&xhci->lock, flags);
763 return -ESHUTDOWN;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700764}
765
Sarah Sharpae636742009-04-29 19:02:31 -0700766/*
767 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
768 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
769 * should pick up where it left off in the TD, unless a Set Transfer Ring
770 * Dequeue Pointer is issued.
771 *
772 * The TRBs that make up the buffers for the canceled URB will be "removed" from
773 * the ring. Since the ring is a contiguous structure, they can't be physically
774 * removed. Instead, there are two options:
775 *
776 * 1) If the HC is in the middle of processing the URB to be canceled, we
777 * simply move the ring's dequeue pointer past those TRBs using the Set
778 * Transfer Ring Dequeue Pointer command. This will be the common case,
779 * when drivers timeout on the last submitted URB and attempt to cancel.
780 *
781 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
782 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
783 * HC will need to invalidate the any TRBs it has cached after the stop
784 * endpoint command, as noted in the xHCI 0.95 errata.
785 *
786 * 3) The TD may have completed by the time the Stop Endpoint Command
787 * completes, so software needs to handle that case too.
788 *
789 * This function should protect against the TD enqueueing code ringing the
790 * doorbell while this code is waiting for a Stop Endpoint command to complete.
791 * It also needs to account for multiple cancellations on happening at the same
792 * time for the same endpoint.
793 *
794 * Note that this function can be called in any context, or so says
795 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700796 */
797int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
798{
Sarah Sharpae636742009-04-29 19:02:31 -0700799 unsigned long flags;
800 int ret;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -0700801 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -0700802 struct xhci_hcd *xhci;
803 struct xhci_td *td;
804 unsigned int ep_index;
805 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700806 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -0700807
808 xhci = hcd_to_xhci(hcd);
809 spin_lock_irqsave(&xhci->lock, flags);
810 /* Make sure the URB hasn't completed or been unlinked already */
811 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
812 if (ret || !urb->hcpriv)
813 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -0700814 temp = xhci_readl(xhci, &xhci->op_regs->status);
815 if (temp == 0xffffffff) {
816 xhci_dbg(xhci, "HW died, freeing TD.\n");
817 td = (struct xhci_td *) urb->hcpriv;
818
819 usb_hcd_unlink_urb_from_ep(hcd, urb);
820 spin_unlock_irqrestore(&xhci->lock, flags);
821 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
822 kfree(td);
823 return ret;
824 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700825 if (xhci->xhc_state & XHCI_STATE_DYING) {
826 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
827 "non-responsive xHCI host.\n",
828 urb->ep->desc.bEndpointAddress, urb);
829 /* Let the stop endpoint command watchdog timer (which set this
830 * state) finish cleaning up the endpoint TD lists. We must
831 * have caught it in the middle of dropping a lock and giving
832 * back an URB.
833 */
834 goto done;
835 }
Sarah Sharpae636742009-04-29 19:02:31 -0700836
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700837 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700838 xhci_dbg(xhci, "Event ring:\n");
839 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700840 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700841 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
842 ep_ring = ep->ring;
Sarah Sharp66e49d82009-07-27 12:03:46 -0700843 xhci_dbg(xhci, "Endpoint ring:\n");
844 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -0700845 td = (struct xhci_td *) urb->hcpriv;
846
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700847 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
Sarah Sharpae636742009-04-29 19:02:31 -0700848 /* Queue a stop endpoint command, but only if this is
849 * the first cancellation to be handled.
850 */
Sarah Sharp678539c2009-10-27 10:55:52 -0700851 if (!(ep->ep_state & EP_HALT_PENDING)) {
852 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700853 ep->stop_cmds_pending++;
854 ep->stop_cmd_timer.expires = jiffies +
855 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
856 add_timer(&ep->stop_cmd_timer);
Sarah Sharp23e3be12009-04-29 19:05:20 -0700857 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
858 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -0700859 }
860done:
861 spin_unlock_irqrestore(&xhci->lock, flags);
862 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700863}
864
Sarah Sharpf94e01862009-04-27 19:58:38 -0700865/* Drop an endpoint from a new bandwidth configuration for this device.
866 * Only one call to this function is allowed per endpoint before
867 * check_bandwidth() or reset_bandwidth() must be called.
868 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
869 * add the endpoint to the schedule with possibly new parameters denoted by a
870 * different endpoint descriptor in usb_host_endpoint.
871 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
872 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700873 *
874 * The USB core will not allow URBs to be queued to an endpoint that is being
875 * disabled, so there's no need for mutual exclusion to protect
876 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700877 */
878int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
879 struct usb_host_endpoint *ep)
880{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700881 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -0700882 struct xhci_container_ctx *in_ctx, *out_ctx;
883 struct xhci_input_control_ctx *ctrl_ctx;
884 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700885 unsigned int last_ctx;
886 unsigned int ep_index;
887 struct xhci_ep_ctx *ep_ctx;
888 u32 drop_flag;
889 u32 new_add_flags, new_drop_flags, new_slot_info;
890 int ret;
891
892 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700893 if (ret <= 0)
894 return ret;
895 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700896 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700897
898 drop_flag = xhci_get_endpoint_flag(&ep->desc);
899 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
900 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
901 __func__, drop_flag);
902 return 0;
903 }
904
Sarah Sharpf94e01862009-04-27 19:58:38 -0700905 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
906 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
907 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700908 return -EINVAL;
909 }
910
911 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -0700912 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
913 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700914 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -0700915 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700916 /* If the HC already knows the endpoint is disabled,
917 * or the HCD has noted it is disabled, ignore this request
918 */
919 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
John Yound115b042009-07-27 12:05:15 -0700920 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700921 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
922 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700923 return 0;
924 }
925
John Yound115b042009-07-27 12:05:15 -0700926 ctrl_ctx->drop_flags |= drop_flag;
927 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700928
Sarah Sharp0a023c62009-09-18 08:55:12 -0700929 ctrl_ctx->add_flags &= ~drop_flag;
John Yound115b042009-07-27 12:05:15 -0700930 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700931
John Yound115b042009-07-27 12:05:15 -0700932 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
933 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700934 /* Update the last valid endpoint context, if we deleted the last one */
John Yound115b042009-07-27 12:05:15 -0700935 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
936 slot_ctx->dev_info &= ~LAST_CTX_MASK;
937 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -0700938 }
John Yound115b042009-07-27 12:05:15 -0700939 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700940
941 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
942
Sarah Sharpf94e01862009-04-27 19:58:38 -0700943 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
944 (unsigned int) ep->desc.bEndpointAddress,
945 udev->slot_id,
946 (unsigned int) new_drop_flags,
947 (unsigned int) new_add_flags,
948 (unsigned int) new_slot_info);
949 return 0;
950}
951
952/* Add an endpoint to a new possible bandwidth configuration for this device.
953 * Only one call to this function is allowed per endpoint before
954 * check_bandwidth() or reset_bandwidth() must be called.
955 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
956 * add the endpoint to the schedule with possibly new parameters denoted by a
957 * different endpoint descriptor in usb_host_endpoint.
958 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
959 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -0700960 *
961 * The USB core will not allow URBs to be queued to an endpoint until the
962 * configuration or alt setting is installed in the device, so there's no need
963 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -0700964 */
965int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
966 struct usb_host_endpoint *ep)
967{
Sarah Sharpf94e01862009-04-27 19:58:38 -0700968 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -0700969 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700970 unsigned int ep_index;
971 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -0700972 struct xhci_slot_ctx *slot_ctx;
973 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700974 u32 added_ctxs;
975 unsigned int last_ctx;
976 u32 new_add_flags, new_drop_flags, new_slot_info;
977 int ret = 0;
978
979 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -0700980 if (ret <= 0) {
981 /* So we won't queue a reset ep command for a root hub */
982 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -0700983 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -0700984 }
Sarah Sharpf94e01862009-04-27 19:58:38 -0700985 xhci = hcd_to_xhci(hcd);
986
987 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
988 last_ctx = xhci_last_valid_endpoint(added_ctxs);
989 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
990 /* FIXME when we have to issue an evaluate endpoint command to
991 * deal with ep0 max packet size changing once we get the
992 * descriptors
993 */
994 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
995 __func__, added_ctxs);
996 return 0;
997 }
998
Sarah Sharpf94e01862009-04-27 19:58:38 -0700999 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1000 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1001 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001002 return -EINVAL;
1003 }
1004
1005 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001006 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1007 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001008 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001009 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001010 /* If the HCD has already noted the endpoint is enabled,
1011 * ignore this request.
1012 */
John Yound115b042009-07-27 12:05:15 -07001013 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001014 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1015 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001016 return 0;
1017 }
1018
Sarah Sharpf88ba782009-05-14 11:44:22 -07001019 /*
1020 * Configuration and alternate setting changes must be done in
1021 * process context, not interrupt context (or so documenation
1022 * for usb_set_interface() and usb_set_configuration() claim).
1023 */
1024 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
Oliver Neukum319c3ea2009-12-16 19:43:59 +01001025 udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001026 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1027 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001028 return -ENOMEM;
1029 }
1030
John Yound115b042009-07-27 12:05:15 -07001031 ctrl_ctx->add_flags |= added_ctxs;
1032 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001033
1034 /* If xhci_endpoint_disable() was called for this endpoint, but the
1035 * xHC hasn't been notified yet through the check_bandwidth() call,
1036 * this re-adds a new state for the endpoint from the new endpoint
1037 * descriptors. We must drop and re-add this endpoint, so we leave the
1038 * drop flags alone.
1039 */
John Yound115b042009-07-27 12:05:15 -07001040 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001041
John Yound115b042009-07-27 12:05:15 -07001042 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001043 /* Update the last valid endpoint context, if we just added one past */
John Yound115b042009-07-27 12:05:15 -07001044 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1045 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1046 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001047 }
John Yound115b042009-07-27 12:05:15 -07001048 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001049
Sarah Sharpa1587d92009-07-27 12:03:15 -07001050 /* Store the usb_device pointer for later use */
1051 ep->hcpriv = udev;
1052
Sarah Sharpf94e01862009-04-27 19:58:38 -07001053 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1054 (unsigned int) ep->desc.bEndpointAddress,
1055 udev->slot_id,
1056 (unsigned int) new_drop_flags,
1057 (unsigned int) new_add_flags,
1058 (unsigned int) new_slot_info);
1059 return 0;
1060}
1061
John Yound115b042009-07-27 12:05:15 -07001062static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001063{
John Yound115b042009-07-27 12:05:15 -07001064 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001065 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001066 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001067 int i;
1068
1069 /* When a device's add flag and drop flag are zero, any subsequent
1070 * configure endpoint command will leave that endpoint's state
1071 * untouched. Make sure we don't leave any old state in the input
1072 * endpoint contexts.
1073 */
John Yound115b042009-07-27 12:05:15 -07001074 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1075 ctrl_ctx->drop_flags = 0;
1076 ctrl_ctx->add_flags = 0;
1077 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1078 slot_ctx->dev_info &= ~LAST_CTX_MASK;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001079 /* Endpoint 0 is always valid */
John Yound115b042009-07-27 12:05:15 -07001080 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001081 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001082 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001083 ep_ctx->ep_info = 0;
1084 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001085 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001086 ep_ctx->tx_info = 0;
1087 }
1088}
1089
Sarah Sharpf2217e82009-08-07 14:04:43 -07001090static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001091 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001092{
1093 int ret;
1094
Sarah Sharp913a8a32009-09-04 10:53:13 -07001095 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001096 case COMP_ENOMEM:
1097 dev_warn(&udev->dev, "Not enough host controller resources "
1098 "for new device state.\n");
1099 ret = -ENOMEM;
1100 /* FIXME: can we allocate more resources for the HC? */
1101 break;
1102 case COMP_BW_ERR:
1103 dev_warn(&udev->dev, "Not enough bandwidth "
1104 "for new device state.\n");
1105 ret = -ENOSPC;
1106 /* FIXME: can we go back to the old state? */
1107 break;
1108 case COMP_TRB_ERR:
1109 /* the HCD set up something wrong */
1110 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1111 "add flag = 1, "
1112 "and endpoint is not disabled.\n");
1113 ret = -EINVAL;
1114 break;
1115 case COMP_SUCCESS:
1116 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1117 ret = 0;
1118 break;
1119 default:
1120 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001121 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001122 ret = -EINVAL;
1123 break;
1124 }
1125 return ret;
1126}
1127
1128static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001129 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001130{
1131 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001132 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001133
Sarah Sharp913a8a32009-09-04 10:53:13 -07001134 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001135 case COMP_EINVAL:
1136 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1137 "context command.\n");
1138 ret = -EINVAL;
1139 break;
1140 case COMP_EBADSLT:
1141 dev_warn(&udev->dev, "WARN: slot not enabled for"
1142 "evaluate context command.\n");
1143 case COMP_CTX_STATE:
1144 dev_warn(&udev->dev, "WARN: invalid context state for "
1145 "evaluate context command.\n");
1146 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1147 ret = -EINVAL;
1148 break;
1149 case COMP_SUCCESS:
1150 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1151 ret = 0;
1152 break;
1153 default:
1154 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001155 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001156 ret = -EINVAL;
1157 break;
1158 }
1159 return ret;
1160}
1161
1162/* Issue a configure endpoint command or evaluate context command
1163 * and wait for it to finish.
1164 */
1165static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001166 struct usb_device *udev,
1167 struct xhci_command *command,
1168 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001169{
1170 int ret;
1171 int timeleft;
1172 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001173 struct xhci_container_ctx *in_ctx;
1174 struct completion *cmd_completion;
1175 int *cmd_status;
1176 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001177
1178 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001179 virt_dev = xhci->devs[udev->slot_id];
1180 if (command) {
1181 in_ctx = command->in_ctx;
1182 cmd_completion = command->completion;
1183 cmd_status = &command->status;
1184 command->command_trb = xhci->cmd_ring->enqueue;
1185 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1186 } else {
1187 in_ctx = virt_dev->in_ctx;
1188 cmd_completion = &virt_dev->cmd_completion;
1189 cmd_status = &virt_dev->cmd_status;
1190 }
Andiry Xu1d680642010-03-12 17:10:04 +08001191 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001192
Sarah Sharpf2217e82009-08-07 14:04:43 -07001193 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001194 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1195 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001196 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07001197 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001198 udev->slot_id);
1199 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08001200 if (command)
1201 list_del(&command->cmd_list);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001202 spin_unlock_irqrestore(&xhci->lock, flags);
1203 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1204 return -ENOMEM;
1205 }
1206 xhci_ring_cmd_db(xhci);
1207 spin_unlock_irqrestore(&xhci->lock, flags);
1208
1209 /* Wait for the configure endpoint command to complete */
1210 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07001211 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001212 USB_CTRL_SET_TIMEOUT);
1213 if (timeleft <= 0) {
1214 xhci_warn(xhci, "%s while waiting for %s command\n",
1215 timeleft == 0 ? "Timeout" : "Signal",
1216 ctx_change == 0 ?
1217 "configure endpoint" :
1218 "evaluate context");
1219 /* FIXME cancel the configure endpoint command */
1220 return -ETIME;
1221 }
1222
1223 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001224 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1225 return xhci_evaluate_context_result(xhci, udev, cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001226}
1227
Sarah Sharpf88ba782009-05-14 11:44:22 -07001228/* Called after one or more calls to xhci_add_endpoint() or
1229 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1230 * to call xhci_reset_bandwidth().
1231 *
1232 * Since we are in the middle of changing either configuration or
1233 * installing a new alt setting, the USB core won't allow URBs to be
1234 * enqueued for any endpoint on the old config or interface. Nothing
1235 * else should be touching the xhci->devs[slot_id] structure, so we
1236 * don't need to take the xhci->lock for manipulating that.
1237 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001238int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1239{
1240 int i;
1241 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001242 struct xhci_hcd *xhci;
1243 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07001244 struct xhci_input_control_ctx *ctrl_ctx;
1245 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001246
1247 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1248 if (ret <= 0)
1249 return ret;
1250 xhci = hcd_to_xhci(hcd);
1251
Sarah Sharpf94e01862009-04-27 19:58:38 -07001252 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1253 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1254 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001255 return -EINVAL;
1256 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001257 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001258 virt_dev = xhci->devs[udev->slot_id];
1259
1260 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07001261 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1262 ctrl_ctx->add_flags |= SLOT_FLAG;
1263 ctrl_ctx->add_flags &= ~EP0_FLAG;
1264 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1265 ctrl_ctx->drop_flags &= ~EP0_FLAG;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001266 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07001267 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1268 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1269 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001270
Sarah Sharp913a8a32009-09-04 10:53:13 -07001271 ret = xhci_configure_endpoint(xhci, udev, NULL,
1272 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001273 if (ret) {
1274 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001275 return ret;
1276 }
1277
1278 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07001279 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1280 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001281
John Yound115b042009-07-27 12:05:15 -07001282 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001283 /* Install new rings and free or cache any old rings */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001284 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001285 if (!virt_dev->eps[i].new_ring)
1286 continue;
1287 /* Only cache or free the old ring if it exists.
1288 * It may not if this is the first add of an endpoint.
1289 */
1290 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08001291 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001292 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001293 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1294 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001295 }
1296
Sarah Sharpf94e01862009-04-27 19:58:38 -07001297 return ret;
1298}
1299
1300void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1301{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001302 struct xhci_hcd *xhci;
1303 struct xhci_virt_device *virt_dev;
1304 int i, ret;
1305
1306 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1307 if (ret <= 0)
1308 return;
1309 xhci = hcd_to_xhci(hcd);
1310
Sarah Sharpf94e01862009-04-27 19:58:38 -07001311 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1312 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1313 __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001314 return;
1315 }
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001316 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001317 virt_dev = xhci->devs[udev->slot_id];
1318 /* Free any rings allocated for added endpoints */
1319 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001320 if (virt_dev->eps[i].new_ring) {
1321 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1322 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001323 }
1324 }
John Yound115b042009-07-27 12:05:15 -07001325 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001326}
1327
Sarah Sharp5270b952009-09-04 10:53:11 -07001328static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001329 struct xhci_container_ctx *in_ctx,
1330 struct xhci_container_ctx *out_ctx,
1331 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07001332{
1333 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001334 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001335 ctrl_ctx->add_flags = add_flags;
1336 ctrl_ctx->drop_flags = drop_flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001337 xhci_slot_copy(xhci, in_ctx, out_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001338 ctrl_ctx->add_flags |= SLOT_FLAG;
1339
Sarah Sharp913a8a32009-09-04 10:53:13 -07001340 xhci_dbg(xhci, "Input Context:\n");
1341 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07001342}
1343
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001344void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1345 unsigned int slot_id, unsigned int ep_index,
1346 struct xhci_dequeue_state *deq_state)
1347{
1348 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001349 struct xhci_ep_ctx *ep_ctx;
1350 u32 added_ctxs;
1351 dma_addr_t addr;
1352
Sarah Sharp913a8a32009-09-04 10:53:13 -07001353 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1354 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001355 in_ctx = xhci->devs[slot_id]->in_ctx;
1356 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1357 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1358 deq_state->new_deq_ptr);
1359 if (addr == 0) {
1360 xhci_warn(xhci, "WARN Cannot submit config ep after "
1361 "reset ep command\n");
1362 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1363 deq_state->new_deq_seg,
1364 deq_state->new_deq_ptr);
1365 return;
1366 }
1367 ep_ctx->deq = addr | deq_state->new_cycle_state;
1368
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001369 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001370 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1371 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001372}
1373
Sarah Sharp82d10092009-08-07 14:04:52 -07001374void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001375 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07001376{
1377 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001378 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07001379
1380 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001381 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07001382 /* We need to move the HW's dequeue pointer past this TD,
1383 * or it will attempt to resend it on the next doorbell ring.
1384 */
1385 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001386 ep_index, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001387 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07001388
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001389 /* HW with the reset endpoint quirk will use the saved dequeue state to
1390 * issue a configure endpoint command later.
1391 */
1392 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1393 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001394 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001395 ep_index, &deq_state);
1396 } else {
1397 /* Better hope no one uses the input context between now and the
1398 * reset endpoint completion!
1399 */
1400 xhci_dbg(xhci, "Setting up input context for "
1401 "configure endpoint command\n");
1402 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1403 ep_index, &deq_state);
1404 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001405}
1406
Sarah Sharpa1587d92009-07-27 12:03:15 -07001407/* Deal with stalled endpoints. The core should have sent the control message
1408 * to clear the halt condition. However, we need to make the xHCI hardware
1409 * reset its sequence number, since a device will expect a sequence number of
1410 * zero after the halt condition is cleared.
1411 * Context: in_interrupt
1412 */
1413void xhci_endpoint_reset(struct usb_hcd *hcd,
1414 struct usb_host_endpoint *ep)
1415{
1416 struct xhci_hcd *xhci;
1417 struct usb_device *udev;
1418 unsigned int ep_index;
1419 unsigned long flags;
1420 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001421 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001422
1423 xhci = hcd_to_xhci(hcd);
1424 udev = (struct usb_device *) ep->hcpriv;
1425 /* Called with a root hub endpoint (or an endpoint that wasn't added
1426 * with xhci_add_endpoint()
1427 */
1428 if (!ep->hcpriv)
1429 return;
1430 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001431 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1432 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001433 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1434 ep->desc.bEndpointAddress);
1435 return;
1436 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001437 if (usb_endpoint_xfer_control(&ep->desc)) {
1438 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1439 return;
1440 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001441
1442 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1443 spin_lock_irqsave(&xhci->lock, flags);
1444 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001445 /*
1446 * Can't change the ring dequeue pointer until it's transitioned to the
1447 * stopped state, which is only upon a successful reset endpoint
1448 * command. Better hope that last command worked!
1449 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07001450 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001451 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1452 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001453 xhci_ring_cmd_db(xhci);
1454 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07001455 virt_ep->stopped_td = NULL;
1456 virt_ep->stopped_trb = NULL;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001457 spin_unlock_irqrestore(&xhci->lock, flags);
1458
1459 if (ret)
1460 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1461}
1462
Sarah Sharp8df75f42010-04-02 15:34:16 -07001463static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1464 struct usb_device *udev, struct usb_host_endpoint *ep,
1465 unsigned int slot_id)
1466{
1467 int ret;
1468 unsigned int ep_index;
1469 unsigned int ep_state;
1470
1471 if (!ep)
1472 return -EINVAL;
1473 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__);
1474 if (ret <= 0)
1475 return -EINVAL;
1476 if (!ep->ss_ep_comp) {
1477 xhci_warn(xhci, "WARN: No SuperSpeed Endpoint Companion"
1478 " descriptor for ep 0x%x\n",
1479 ep->desc.bEndpointAddress);
1480 return -EINVAL;
1481 }
1482 if (ep->ss_ep_comp->desc.bmAttributes == 0) {
1483 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1484 " descriptor for ep 0x%x does not support streams\n",
1485 ep->desc.bEndpointAddress);
1486 return -EINVAL;
1487 }
1488
1489 ep_index = xhci_get_endpoint_index(&ep->desc);
1490 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1491 if (ep_state & EP_HAS_STREAMS ||
1492 ep_state & EP_GETTING_STREAMS) {
1493 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1494 "already has streams set up.\n",
1495 ep->desc.bEndpointAddress);
1496 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1497 "dynamic stream context array reallocation.\n");
1498 return -EINVAL;
1499 }
1500 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1501 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1502 "endpoint 0x%x; URBs are pending.\n",
1503 ep->desc.bEndpointAddress);
1504 return -EINVAL;
1505 }
1506 return 0;
1507}
1508
1509static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1510 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1511{
1512 unsigned int max_streams;
1513
1514 /* The stream context array size must be a power of two */
1515 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1516 /*
1517 * Find out how many primary stream array entries the host controller
1518 * supports. Later we may use secondary stream arrays (similar to 2nd
1519 * level page entries), but that's an optional feature for xHCI host
1520 * controllers. xHCs must support at least 4 stream IDs.
1521 */
1522 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1523 if (*num_stream_ctxs > max_streams) {
1524 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1525 max_streams);
1526 *num_stream_ctxs = max_streams;
1527 *num_streams = max_streams;
1528 }
1529}
1530
1531/* Returns an error code if one of the endpoint already has streams.
1532 * This does not change any data structures, it only checks and gathers
1533 * information.
1534 */
1535static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1536 struct usb_device *udev,
1537 struct usb_host_endpoint **eps, unsigned int num_eps,
1538 unsigned int *num_streams, u32 *changed_ep_bitmask)
1539{
1540 struct usb_host_ss_ep_comp *ss_ep_comp;
1541 unsigned int max_streams;
1542 unsigned int endpoint_flag;
1543 int i;
1544 int ret;
1545
1546 for (i = 0; i < num_eps; i++) {
1547 ret = xhci_check_streams_endpoint(xhci, udev,
1548 eps[i], udev->slot_id);
1549 if (ret < 0)
1550 return ret;
1551
1552 ss_ep_comp = eps[i]->ss_ep_comp;
1553 max_streams = USB_SS_MAX_STREAMS(ss_ep_comp->desc.bmAttributes);
1554 if (max_streams < (*num_streams - 1)) {
1555 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1556 eps[i]->desc.bEndpointAddress,
1557 max_streams);
1558 *num_streams = max_streams+1;
1559 }
1560
1561 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1562 if (*changed_ep_bitmask & endpoint_flag)
1563 return -EINVAL;
1564 *changed_ep_bitmask |= endpoint_flag;
1565 }
1566 return 0;
1567}
1568
1569static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1570 struct usb_device *udev,
1571 struct usb_host_endpoint **eps, unsigned int num_eps)
1572{
1573 u32 changed_ep_bitmask = 0;
1574 unsigned int slot_id;
1575 unsigned int ep_index;
1576 unsigned int ep_state;
1577 int i;
1578
1579 slot_id = udev->slot_id;
1580 if (!xhci->devs[slot_id])
1581 return 0;
1582
1583 for (i = 0; i < num_eps; i++) {
1584 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1585 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1586 /* Are streams already being freed for the endpoint? */
1587 if (ep_state & EP_GETTING_NO_STREAMS) {
1588 xhci_warn(xhci, "WARN Can't disable streams for "
1589 "endpoint 0x%x\n, "
1590 "streams are being disabled already.",
1591 eps[i]->desc.bEndpointAddress);
1592 return 0;
1593 }
1594 /* Are there actually any streams to free? */
1595 if (!(ep_state & EP_HAS_STREAMS) &&
1596 !(ep_state & EP_GETTING_STREAMS)) {
1597 xhci_warn(xhci, "WARN Can't disable streams for "
1598 "endpoint 0x%x\n, "
1599 "streams are already disabled!",
1600 eps[i]->desc.bEndpointAddress);
1601 xhci_warn(xhci, "WARN xhci_free_streams() called "
1602 "with non-streams endpoint\n");
1603 return 0;
1604 }
1605 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1606 }
1607 return changed_ep_bitmask;
1608}
1609
1610/*
1611 * The USB device drivers use this function (though the HCD interface in USB
1612 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1613 * coordinate mass storage command queueing across multiple endpoints (basically
1614 * a stream ID == a task ID).
1615 *
1616 * Setting up streams involves allocating the same size stream context array
1617 * for each endpoint and issuing a configure endpoint command for all endpoints.
1618 *
1619 * Don't allow the call to succeed if one endpoint only supports one stream
1620 * (which means it doesn't support streams at all).
1621 *
1622 * Drivers may get less stream IDs than they asked for, if the host controller
1623 * hardware or endpoints claim they can't support the number of requested
1624 * stream IDs.
1625 */
1626int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1627 struct usb_host_endpoint **eps, unsigned int num_eps,
1628 unsigned int num_streams, gfp_t mem_flags)
1629{
1630 int i, ret;
1631 struct xhci_hcd *xhci;
1632 struct xhci_virt_device *vdev;
1633 struct xhci_command *config_cmd;
1634 unsigned int ep_index;
1635 unsigned int num_stream_ctxs;
1636 unsigned long flags;
1637 u32 changed_ep_bitmask = 0;
1638
1639 if (!eps)
1640 return -EINVAL;
1641
1642 /* Add one to the number of streams requested to account for
1643 * stream 0 that is reserved for xHCI usage.
1644 */
1645 num_streams += 1;
1646 xhci = hcd_to_xhci(hcd);
1647 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
1648 num_streams);
1649
1650 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
1651 if (!config_cmd) {
1652 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1653 return -ENOMEM;
1654 }
1655
1656 /* Check to make sure all endpoints are not already configured for
1657 * streams. While we're at it, find the maximum number of streams that
1658 * all the endpoints will support and check for duplicate endpoints.
1659 */
1660 spin_lock_irqsave(&xhci->lock, flags);
1661 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
1662 num_eps, &num_streams, &changed_ep_bitmask);
1663 if (ret < 0) {
1664 xhci_free_command(xhci, config_cmd);
1665 spin_unlock_irqrestore(&xhci->lock, flags);
1666 return ret;
1667 }
1668 if (num_streams <= 1) {
1669 xhci_warn(xhci, "WARN: endpoints can't handle "
1670 "more than one stream.\n");
1671 xhci_free_command(xhci, config_cmd);
1672 spin_unlock_irqrestore(&xhci->lock, flags);
1673 return -EINVAL;
1674 }
1675 vdev = xhci->devs[udev->slot_id];
1676 /* Mark each endpoint as being in transistion, so
1677 * xhci_urb_enqueue() will reject all URBs.
1678 */
1679 for (i = 0; i < num_eps; i++) {
1680 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1681 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
1682 }
1683 spin_unlock_irqrestore(&xhci->lock, flags);
1684
1685 /* Setup internal data structures and allocate HW data structures for
1686 * streams (but don't install the HW structures in the input context
1687 * until we're sure all memory allocation succeeded).
1688 */
1689 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
1690 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
1691 num_stream_ctxs, num_streams);
1692
1693 for (i = 0; i < num_eps; i++) {
1694 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1695 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
1696 num_stream_ctxs,
1697 num_streams, mem_flags);
1698 if (!vdev->eps[ep_index].stream_info)
1699 goto cleanup;
1700 /* Set maxPstreams in endpoint context and update deq ptr to
1701 * point to stream context array. FIXME
1702 */
1703 }
1704
1705 /* Set up the input context for a configure endpoint command. */
1706 for (i = 0; i < num_eps; i++) {
1707 struct xhci_ep_ctx *ep_ctx;
1708
1709 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1710 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
1711
1712 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
1713 vdev->out_ctx, ep_index);
1714 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
1715 vdev->eps[ep_index].stream_info);
1716 }
1717 /* Tell the HW to drop its old copy of the endpoint context info
1718 * and add the updated copy from the input context.
1719 */
1720 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
1721 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1722
1723 /* Issue and wait for the configure endpoint command */
1724 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
1725 false, false);
1726
1727 /* xHC rejected the configure endpoint command for some reason, so we
1728 * leave the old ring intact and free our internal streams data
1729 * structure.
1730 */
1731 if (ret < 0)
1732 goto cleanup;
1733
1734 spin_lock_irqsave(&xhci->lock, flags);
1735 for (i = 0; i < num_eps; i++) {
1736 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1737 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1738 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
1739 udev->slot_id, ep_index);
1740 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
1741 }
1742 xhci_free_command(xhci, config_cmd);
1743 spin_unlock_irqrestore(&xhci->lock, flags);
1744
1745 /* Subtract 1 for stream 0, which drivers can't use */
1746 return num_streams - 1;
1747
1748cleanup:
1749 /* If it didn't work, free the streams! */
1750 for (i = 0; i < num_eps; i++) {
1751 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1752 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
1753 /* FIXME Unset maxPstreams in endpoint context and
1754 * update deq ptr to point to normal string ring.
1755 */
1756 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
1757 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1758 xhci_endpoint_zero(xhci, vdev, eps[i]);
1759 }
1760 xhci_free_command(xhci, config_cmd);
1761 return -ENOMEM;
1762}
1763
1764/* Transition the endpoint from using streams to being a "normal" endpoint
1765 * without streams.
1766 *
1767 * Modify the endpoint context state, submit a configure endpoint command,
1768 * and free all endpoint rings for streams if that completes successfully.
1769 */
1770int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1771 struct usb_host_endpoint **eps, unsigned int num_eps,
1772 gfp_t mem_flags)
1773{
1774 int i, ret;
1775 struct xhci_hcd *xhci;
1776 struct xhci_virt_device *vdev;
1777 struct xhci_command *command;
1778 unsigned int ep_index;
1779 unsigned long flags;
1780 u32 changed_ep_bitmask;
1781
1782 xhci = hcd_to_xhci(hcd);
1783 vdev = xhci->devs[udev->slot_id];
1784
1785 /* Set up a configure endpoint command to remove the streams rings */
1786 spin_lock_irqsave(&xhci->lock, flags);
1787 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
1788 udev, eps, num_eps);
1789 if (changed_ep_bitmask == 0) {
1790 spin_unlock_irqrestore(&xhci->lock, flags);
1791 return -EINVAL;
1792 }
1793
1794 /* Use the xhci_command structure from the first endpoint. We may have
1795 * allocated too many, but the driver may call xhci_free_streams() for
1796 * each endpoint it grouped into one call to xhci_alloc_streams().
1797 */
1798 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
1799 command = vdev->eps[ep_index].stream_info->free_streams_command;
1800 for (i = 0; i < num_eps; i++) {
1801 struct xhci_ep_ctx *ep_ctx;
1802
1803 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1804 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
1805 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
1806 EP_GETTING_NO_STREAMS;
1807
1808 xhci_endpoint_copy(xhci, command->in_ctx,
1809 vdev->out_ctx, ep_index);
1810 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
1811 &vdev->eps[ep_index]);
1812 }
1813 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
1814 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
1815 spin_unlock_irqrestore(&xhci->lock, flags);
1816
1817 /* Issue and wait for the configure endpoint command,
1818 * which must succeed.
1819 */
1820 ret = xhci_configure_endpoint(xhci, udev, command,
1821 false, true);
1822
1823 /* xHC rejected the configure endpoint command for some reason, so we
1824 * leave the streams rings intact.
1825 */
1826 if (ret < 0)
1827 return ret;
1828
1829 spin_lock_irqsave(&xhci->lock, flags);
1830 for (i = 0; i < num_eps; i++) {
1831 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1832 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
1833 /* FIXME Unset maxPstreams in endpoint context and
1834 * update deq ptr to point to normal string ring.
1835 */
1836 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
1837 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
1838 }
1839 spin_unlock_irqrestore(&xhci->lock, flags);
1840
1841 return 0;
1842}
1843
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001844/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08001845 * This submits a Reset Device Command, which will set the device state to 0,
1846 * set the device address to 0, and disable all the endpoints except the default
1847 * control endpoint. The USB core should come back and call
1848 * xhci_address_device(), and then re-set up the configuration. If this is
1849 * called because of a usb_reset_and_verify_device(), then the old alternate
1850 * settings will be re-installed through the normal bandwidth allocation
1851 * functions.
1852 *
1853 * Wait for the Reset Device command to finish. Remove all structures
1854 * associated with the endpoints that were disabled. Clear the input device
1855 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
1856 */
1857int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
1858{
1859 int ret, i;
1860 unsigned long flags;
1861 struct xhci_hcd *xhci;
1862 unsigned int slot_id;
1863 struct xhci_virt_device *virt_dev;
1864 struct xhci_command *reset_device_cmd;
1865 int timeleft;
1866 int last_freed_endpoint;
1867
1868 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1869 if (ret <= 0)
1870 return ret;
1871 xhci = hcd_to_xhci(hcd);
1872 slot_id = udev->slot_id;
1873 virt_dev = xhci->devs[slot_id];
1874 if (!virt_dev) {
1875 xhci_dbg(xhci, "%s called with invalid slot ID %u\n",
1876 __func__, slot_id);
1877 return -EINVAL;
1878 }
1879
1880 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
1881 /* Allocate the command structure that holds the struct completion.
1882 * Assume we're in process context, since the normal device reset
1883 * process has to wait for the device anyway. Storage devices are
1884 * reset as part of error handling, so use GFP_NOIO instead of
1885 * GFP_KERNEL.
1886 */
1887 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
1888 if (!reset_device_cmd) {
1889 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
1890 return -ENOMEM;
1891 }
1892
1893 /* Attempt to submit the Reset Device command to the command ring */
1894 spin_lock_irqsave(&xhci->lock, flags);
1895 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
1896 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
1897 ret = xhci_queue_reset_device(xhci, slot_id);
1898 if (ret) {
1899 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1900 list_del(&reset_device_cmd->cmd_list);
1901 spin_unlock_irqrestore(&xhci->lock, flags);
1902 goto command_cleanup;
1903 }
1904 xhci_ring_cmd_db(xhci);
1905 spin_unlock_irqrestore(&xhci->lock, flags);
1906
1907 /* Wait for the Reset Device command to finish */
1908 timeleft = wait_for_completion_interruptible_timeout(
1909 reset_device_cmd->completion,
1910 USB_CTRL_SET_TIMEOUT);
1911 if (timeleft <= 0) {
1912 xhci_warn(xhci, "%s while waiting for reset device command\n",
1913 timeleft == 0 ? "Timeout" : "Signal");
1914 spin_lock_irqsave(&xhci->lock, flags);
1915 /* The timeout might have raced with the event ring handler, so
1916 * only delete from the list if the item isn't poisoned.
1917 */
1918 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
1919 list_del(&reset_device_cmd->cmd_list);
1920 spin_unlock_irqrestore(&xhci->lock, flags);
1921 ret = -ETIME;
1922 goto command_cleanup;
1923 }
1924
1925 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
1926 * unless we tried to reset a slot ID that wasn't enabled,
1927 * or the device wasn't in the addressed or configured state.
1928 */
1929 ret = reset_device_cmd->status;
1930 switch (ret) {
1931 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
1932 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
1933 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
1934 slot_id,
1935 xhci_get_slot_state(xhci, virt_dev->out_ctx));
1936 xhci_info(xhci, "Not freeing device rings.\n");
1937 /* Don't treat this as an error. May change my mind later. */
1938 ret = 0;
1939 goto command_cleanup;
1940 case COMP_SUCCESS:
1941 xhci_dbg(xhci, "Successful reset device command.\n");
1942 break;
1943 default:
1944 if (xhci_is_vendor_info_code(xhci, ret))
1945 break;
1946 xhci_warn(xhci, "Unknown completion code %u for "
1947 "reset device command.\n", ret);
1948 ret = -EINVAL;
1949 goto command_cleanup;
1950 }
1951
1952 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
1953 last_freed_endpoint = 1;
1954 for (i = 1; i < 31; ++i) {
1955 if (!virt_dev->eps[i].ring)
1956 continue;
1957 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
1958 last_freed_endpoint = i;
1959 }
1960 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
1961 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
1962 ret = 0;
1963
1964command_cleanup:
1965 xhci_free_command(xhci, reset_device_cmd);
1966 return ret;
1967}
1968
1969/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001970 * At this point, the struct usb_device is about to go away, the device has
1971 * disconnected, and all traffic has been stopped and the endpoints have been
1972 * disabled. Free any HC data structures associated with that device.
1973 */
1974void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1975{
1976 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001977 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001978 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07001979 u32 state;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001980 int i;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001981
1982 if (udev->slot_id == 0)
1983 return;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001984 virt_dev = xhci->devs[udev->slot_id];
1985 if (!virt_dev)
1986 return;
1987
1988 /* Stop any wayward timer functions (which may grab the lock) */
1989 for (i = 0; i < 31; ++i) {
1990 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
1991 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
1992 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001993
1994 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07001995 /* Don't disable the slot if the host controller is dead. */
1996 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001997 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07001998 xhci_free_virt_device(xhci, udev->slot_id);
1999 spin_unlock_irqrestore(&xhci->lock, flags);
2000 return;
2001 }
2002
Sarah Sharp23e3be12009-04-29 19:05:20 -07002003 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002004 spin_unlock_irqrestore(&xhci->lock, flags);
2005 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2006 return;
2007 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002008 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002009 spin_unlock_irqrestore(&xhci->lock, flags);
2010 /*
2011 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07002012 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002013 */
2014}
2015
2016/*
2017 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2018 * timed out, or allocating memory failed. Returns 1 on success.
2019 */
2020int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2021{
2022 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2023 unsigned long flags;
2024 int timeleft;
2025 int ret;
2026
2027 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002028 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002029 if (ret) {
2030 spin_unlock_irqrestore(&xhci->lock, flags);
2031 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2032 return 0;
2033 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002034 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002035 spin_unlock_irqrestore(&xhci->lock, flags);
2036
2037 /* XXX: how much time for xHC slot assignment? */
2038 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2039 USB_CTRL_SET_TIMEOUT);
2040 if (timeleft <= 0) {
2041 xhci_warn(xhci, "%s while waiting for a slot\n",
2042 timeleft == 0 ? "Timeout" : "Signal");
2043 /* FIXME cancel the enable slot request */
2044 return 0;
2045 }
2046
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002047 if (!xhci->slot_id) {
2048 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002049 return 0;
2050 }
Sarah Sharpf88ba782009-05-14 11:44:22 -07002051 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002052 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
2053 /* Disable slot, if we can do it without mem alloc */
2054 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07002055 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002056 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2057 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002058 spin_unlock_irqrestore(&xhci->lock, flags);
2059 return 0;
2060 }
2061 udev->slot_id = xhci->slot_id;
2062 /* Is this a LS or FS device under a HS hub? */
2063 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002064 return 1;
2065}
2066
2067/*
2068 * Issue an Address Device command (which will issue a SetAddress request to
2069 * the device).
2070 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2071 * we should only issue and wait on one address command at the same time.
2072 *
2073 * We add one to the device address issued by the hardware because the USB core
2074 * uses address 1 for the root hubs (even though they're not really devices).
2075 */
2076int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2077{
2078 unsigned long flags;
2079 int timeleft;
2080 struct xhci_virt_device *virt_dev;
2081 int ret = 0;
2082 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07002083 struct xhci_slot_ctx *slot_ctx;
2084 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07002085 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002086
2087 if (!udev->slot_id) {
2088 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2089 return -EINVAL;
2090 }
2091
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002092 virt_dev = xhci->devs[udev->slot_id];
2093
2094 /* If this is a Set Address to an unconfigured device, setup ep 0 */
2095 if (!udev->config)
2096 xhci_setup_addressable_virt_dev(xhci, udev);
2097 /* Otherwise, assume the core has the device configured how it wants */
Sarah Sharp66e49d82009-07-27 12:03:46 -07002098 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002099 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002100
Sarah Sharpf88ba782009-05-14 11:44:22 -07002101 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07002102 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2103 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002104 if (ret) {
2105 spin_unlock_irqrestore(&xhci->lock, flags);
2106 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2107 return ret;
2108 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002109 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002110 spin_unlock_irqrestore(&xhci->lock, flags);
2111
2112 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2113 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2114 USB_CTRL_SET_TIMEOUT);
2115 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2116 * the SetAddress() "recovery interval" required by USB and aborting the
2117 * command on a timeout.
2118 */
2119 if (timeleft <= 0) {
2120 xhci_warn(xhci, "%s while waiting for a slot\n",
2121 timeleft == 0 ? "Timeout" : "Signal");
2122 /* FIXME cancel the address device command */
2123 return -ETIME;
2124 }
2125
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002126 switch (virt_dev->cmd_status) {
2127 case COMP_CTX_STATE:
2128 case COMP_EBADSLT:
2129 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2130 udev->slot_id);
2131 ret = -EINVAL;
2132 break;
2133 case COMP_TX_ERR:
2134 dev_warn(&udev->dev, "Device not responding to set address.\n");
2135 ret = -EPROTO;
2136 break;
2137 case COMP_SUCCESS:
2138 xhci_dbg(xhci, "Successful Address Device command\n");
2139 break;
2140 default:
2141 xhci_err(xhci, "ERROR: unexpected command completion "
2142 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002143 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002144 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002145 ret = -EINVAL;
2146 break;
2147 }
2148 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002149 return ret;
2150 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07002151 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2152 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2153 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002154 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002155 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2156 (unsigned long long)
2157 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002158 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07002159 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002160 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002161 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002162 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002163 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002164 /*
2165 * USB core uses address 1 for the roothubs, so we add one to the
2166 * address given back to us by the HC.
2167 */
John Yound115b042009-07-27 12:05:15 -07002168 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
2169 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002170 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07002171 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2172 ctrl_ctx->add_flags = 0;
2173 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002174
2175 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
2176 /* XXX Meh, not sure if anyone else but choose_address uses this. */
2177 set_bit(udev->devnum, udev->bus->devmap.devicemap);
2178
2179 return 0;
2180}
2181
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002182/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2183 * internal data structures for the device.
2184 */
2185int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2186 struct usb_tt *tt, gfp_t mem_flags)
2187{
2188 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2189 struct xhci_virt_device *vdev;
2190 struct xhci_command *config_cmd;
2191 struct xhci_input_control_ctx *ctrl_ctx;
2192 struct xhci_slot_ctx *slot_ctx;
2193 unsigned long flags;
2194 unsigned think_time;
2195 int ret;
2196
2197 /* Ignore root hubs */
2198 if (!hdev->parent)
2199 return 0;
2200
2201 vdev = xhci->devs[hdev->slot_id];
2202 if (!vdev) {
2203 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2204 return -EINVAL;
2205 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08002206 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002207 if (!config_cmd) {
2208 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2209 return -ENOMEM;
2210 }
2211
2212 spin_lock_irqsave(&xhci->lock, flags);
2213 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2214 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2215 ctrl_ctx->add_flags |= SLOT_FLAG;
2216 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2217 slot_ctx->dev_info |= DEV_HUB;
2218 if (tt->multi)
2219 slot_ctx->dev_info |= DEV_MTT;
2220 if (xhci->hci_version > 0x95) {
2221 xhci_dbg(xhci, "xHCI version %x needs hub "
2222 "TT think time and number of ports\n",
2223 (unsigned int) xhci->hci_version);
2224 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2225 /* Set TT think time - convert from ns to FS bit times.
2226 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2227 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2228 */
2229 think_time = tt->think_time;
2230 if (think_time != 0)
2231 think_time = (think_time / 666) - 1;
2232 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2233 } else {
2234 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2235 "TT think time or number of ports\n",
2236 (unsigned int) xhci->hci_version);
2237 }
2238 slot_ctx->dev_state = 0;
2239 spin_unlock_irqrestore(&xhci->lock, flags);
2240
2241 xhci_dbg(xhci, "Set up %s for hub device.\n",
2242 (xhci->hci_version > 0x95) ?
2243 "configure endpoint" : "evaluate context");
2244 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2245 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2246
2247 /* Issue and wait for the configure endpoint or
2248 * evaluate context command.
2249 */
2250 if (xhci->hci_version > 0x95)
2251 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2252 false, false);
2253 else
2254 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2255 true, false);
2256
2257 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2258 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2259
2260 xhci_free_command(xhci, config_cmd);
2261 return ret;
2262}
2263
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002264int xhci_get_frame(struct usb_hcd *hcd)
2265{
2266 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2267 /* EHCI mods by the periodic size. Why? */
2268 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2269}
2270
2271MODULE_DESCRIPTION(DRIVER_DESC);
2272MODULE_AUTHOR(DRIVER_AUTHOR);
2273MODULE_LICENSE("GPL");
2274
2275static int __init xhci_hcd_init(void)
2276{
2277#ifdef CONFIG_PCI
2278 int retval = 0;
2279
2280 retval = xhci_register_pci();
2281
2282 if (retval < 0) {
2283 printk(KERN_DEBUG "Problem registering PCI driver.");
2284 return retval;
2285 }
2286#endif
Sarah Sharp98441972009-05-14 11:44:18 -07002287 /*
2288 * Check the compiler generated sizes of structures that must be laid
2289 * out in specific ways for hardware access.
2290 */
2291 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2292 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2293 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2294 /* xhci_device_control has eight fields, and also
2295 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2296 */
Sarah Sharp98441972009-05-14 11:44:18 -07002297 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2298 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2299 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2300 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2301 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2302 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2303 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2304 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002305 return 0;
2306}
2307module_init(xhci_hcd_init);
2308
2309static void __exit xhci_hcd_cleanup(void)
2310{
2311#ifdef CONFIG_PCI
2312 xhci_unregister_pci();
2313#endif
2314}
2315module_exit(xhci_hcd_cleanup);