blob: 45e4a3108cc31285fcb41c7a83972a621a72a68b [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
Dong Nguyen43b86af2010-07-21 16:56:08 -070023#include <linux/pci.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070024#include <linux/irq.h>
Sarah Sharp8df75f42010-04-02 15:34:16 -070025#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070026#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070027#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070029
30#include "xhci.h"
31
32#define DRIVER_AUTHOR "Sarah Sharp"
33#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
34
Sarah Sharpb0567b32009-08-07 14:04:36 -070035/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
36static int link_quirk;
37module_param(link_quirk, int, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
39
Sarah Sharp66d4ead2009-04-27 19:52:28 -070040/* TODO: copied from ehci-hcd.c - can this be refactored? */
41/*
42 * handshake - spin reading hc until handshake completes or fails
43 * @ptr: address of hc register to be read
44 * @mask: bits to look at in result of read
45 * @done: value of those bits when handshake succeeds
46 * @usec: timeout in microseconds
47 *
48 * Returns negative errno, or zero on success
49 *
50 * Success happens when the "mask" bits have the specified value (hardware
51 * handshake done). There are two failure modes: "usec" have passed (major
52 * hardware flakeout), or the register reads as all-ones (hardware removed).
53 */
54static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
55 u32 mask, u32 done, int usec)
56{
57 u32 result;
58
59 do {
60 result = xhci_readl(xhci, ptr);
61 if (result == ~(u32)0) /* card removed */
62 return -ENODEV;
63 result &= mask;
64 if (result == done)
65 return 0;
66 udelay(1);
67 usec--;
68 } while (usec > 0);
69 return -ETIMEDOUT;
70}
71
72/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070073 * Disable interrupts and begin the xHCI halting process.
74 */
75void xhci_quiesce(struct xhci_hcd *xhci)
76{
77 u32 halted;
78 u32 cmd;
79 u32 mask;
80
81 mask = ~(XHCI_IRQS);
82 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
83 if (!halted)
84 mask &= ~CMD_RUN;
85
86 cmd = xhci_readl(xhci, &xhci->op_regs->command);
87 cmd &= mask;
88 xhci_writel(xhci, cmd, &xhci->op_regs->command);
89}
90
91/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070092 * Force HC into halt state.
93 *
94 * Disable any IRQs and clear the run/stop bit.
95 * HC will complete any current and actively pipelined transactions, and
96 * should halt within 16 microframes of the run/stop bit being cleared.
97 * Read HC Halted bit in the status register to see when the HC is finished.
98 * XXX: shouldn't we set HC_STATE_HALT here somewhere?
99 */
100int xhci_halt(struct xhci_hcd *xhci)
101{
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700102 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700103 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700104
105 return handshake(xhci, &xhci->op_regs->status,
106 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
107}
108
109/*
Sarah Sharped074532010-05-24 13:25:21 -0700110 * Set the run bit and wait for the host to be running.
111 */
112int xhci_start(struct xhci_hcd *xhci)
113{
114 u32 temp;
115 int ret;
116
117 temp = xhci_readl(xhci, &xhci->op_regs->command);
118 temp |= (CMD_RUN);
119 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
120 temp);
121 xhci_writel(xhci, temp, &xhci->op_regs->command);
122
123 /*
124 * Wait for the HCHalted Status bit to be 0 to indicate the host is
125 * running.
126 */
127 ret = handshake(xhci, &xhci->op_regs->status,
128 STS_HALT, 0, XHCI_MAX_HALT_USEC);
129 if (ret == -ETIMEDOUT)
130 xhci_err(xhci, "Host took too long to start, "
131 "waited %u microseconds.\n",
132 XHCI_MAX_HALT_USEC);
133 return ret;
134}
135
136/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700137 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
138 *
139 * This resets pipelines, timers, counters, state machines, etc.
140 * Transactions will be terminated immediately, and operational registers
141 * will be set to their defaults.
142 */
143int xhci_reset(struct xhci_hcd *xhci)
144{
145 u32 command;
146 u32 state;
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700147 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700148
149 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700150 if ((state & STS_HALT) == 0) {
151 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
152 return 0;
153 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700154
155 xhci_dbg(xhci, "// Reset the HC\n");
156 command = xhci_readl(xhci, &xhci->op_regs->command);
157 command |= CMD_RESET;
158 xhci_writel(xhci, command, &xhci->op_regs->command);
159 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
160 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
161
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700162 ret = handshake(xhci, &xhci->op_regs->command,
163 CMD_RESET, 0, 250 * 1000);
164 if (ret)
165 return ret;
166
167 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
168 /*
169 * xHCI cannot write to any doorbells or operational registers other
170 * than status until the "Controller Not Ready" flag is cleared.
171 */
172 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700173}
174
Dong Nguyen43b86af2010-07-21 16:56:08 -0700175/*
176 * Free IRQs
177 * free all IRQs request
178 */
179static void xhci_free_irq(struct xhci_hcd *xhci)
180{
181 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700182 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
183
Dong Nguyen43b86af2010-07-21 16:56:08 -0700184 /* return if using legacy interrupt */
185 if (xhci_to_hcd(xhci)->irq >= 0)
186 return;
187
188 if (xhci->msix_entries) {
189 for (i = 0; i < xhci->msix_count; i++)
190 if (xhci->msix_entries[i].vector)
191 free_irq(xhci->msix_entries[i].vector,
192 xhci_to_hcd(xhci));
193 } else if (pdev->irq >= 0)
194 free_irq(pdev->irq, xhci_to_hcd(xhci));
195
196 return;
197}
198
199/*
200 * Set up MSI
201 */
202static int xhci_setup_msi(struct xhci_hcd *xhci)
203{
204 int ret;
205 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
206
207 ret = pci_enable_msi(pdev);
208 if (ret) {
209 xhci_err(xhci, "failed to allocate MSI entry\n");
210 return ret;
211 }
212
213 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
214 0, "xhci_hcd", xhci_to_hcd(xhci));
215 if (ret) {
216 xhci_err(xhci, "disable MSI interrupt\n");
217 pci_disable_msi(pdev);
218 }
219
220 return ret;
221}
222
223/*
224 * Set up MSI-X
225 */
226static int xhci_setup_msix(struct xhci_hcd *xhci)
227{
228 int i, ret = 0;
229 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
230
231 /*
232 * calculate number of msi-x vectors supported.
233 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
234 * with max number of interrupters based on the xhci HCSPARAMS1.
235 * - num_online_cpus: maximum msi-x vectors per CPUs core.
236 * Add additional 1 vector to ensure always available interrupt.
237 */
238 xhci->msix_count = min(num_online_cpus() + 1,
239 HCS_MAX_INTRS(xhci->hcs_params1));
240
241 xhci->msix_entries =
242 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800243 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700244 if (!xhci->msix_entries) {
245 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
246 return -ENOMEM;
247 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700248
249 for (i = 0; i < xhci->msix_count; i++) {
250 xhci->msix_entries[i].entry = i;
251 xhci->msix_entries[i].vector = 0;
252 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700253
254 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
255 if (ret) {
256 xhci_err(xhci, "Failed to enable MSI-X\n");
257 goto free_entries;
258 }
259
Dong Nguyen43b86af2010-07-21 16:56:08 -0700260 for (i = 0; i < xhci->msix_count; i++) {
261 ret = request_irq(xhci->msix_entries[i].vector,
262 (irq_handler_t)xhci_msi_irq,
263 0, "xhci_hcd", xhci_to_hcd(xhci));
264 if (ret)
265 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700266 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700267
268 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700269
270disable_msix:
Dong Nguyen43b86af2010-07-21 16:56:08 -0700271 xhci_err(xhci, "disable MSI-X interrupt\n");
272 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700273 pci_disable_msix(pdev);
274free_entries:
275 kfree(xhci->msix_entries);
276 xhci->msix_entries = NULL;
277 return ret;
278}
279
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700280/* Free any IRQs and disable MSI-X */
281static void xhci_cleanup_msix(struct xhci_hcd *xhci)
282{
283 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700284
Dong Nguyen43b86af2010-07-21 16:56:08 -0700285 xhci_free_irq(xhci);
286
287 if (xhci->msix_entries) {
288 pci_disable_msix(pdev);
289 kfree(xhci->msix_entries);
290 xhci->msix_entries = NULL;
291 } else {
292 pci_disable_msi(pdev);
293 }
294
295 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700296}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700297
298/*
299 * Initialize memory for HCD and xHC (one-time init).
300 *
301 * Program the PAGESIZE register, initialize the device context array, create
302 * device contexts (?), set up a command ring segment (or two?), create event
303 * ring (one for now).
304 */
305int xhci_init(struct usb_hcd *hcd)
306{
307 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
308 int retval = 0;
309
310 xhci_dbg(xhci, "xhci_init\n");
311 spin_lock_init(&xhci->lock);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700312 if (link_quirk) {
313 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
314 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
315 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700316 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700317 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700318 retval = xhci_mem_init(xhci, GFP_KERNEL);
319 xhci_dbg(xhci, "Finished xhci_init\n");
320
321 return retval;
322}
323
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700324/*-------------------------------------------------------------------------*/
325
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700326
327#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700328void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700329{
330 unsigned long flags;
331 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700332 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700333 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
334 int i, j;
335
336 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
337
338 spin_lock_irqsave(&xhci->lock, flags);
339 temp = xhci_readl(xhci, &xhci->op_regs->status);
340 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700341 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700342 xhci_dbg(xhci, "HW died, polling stopped.\n");
343 spin_unlock_irqrestore(&xhci->lock, flags);
344 return;
345 }
346
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700347 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
348 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
349 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
350 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
351 xhci->error_bitmask = 0;
352 xhci_dbg(xhci, "Event ring:\n");
353 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
354 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700355 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
356 temp_64 &= ~ERST_PTR_MASK;
357 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700358 xhci_dbg(xhci, "Command ring:\n");
359 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
360 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
361 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700362 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700363 if (!xhci->devs[i])
364 continue;
365 for (j = 0; j < 31; ++j) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700366 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700367 }
368 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700369
370 if (xhci->noops_submitted != NUM_TEST_NOOPS)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700371 if (xhci_setup_one_noop(xhci))
372 xhci_ring_cmd_db(xhci);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700373 spin_unlock_irqrestore(&xhci->lock, flags);
374
375 if (!xhci->zombie)
376 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
377 else
378 xhci_dbg(xhci, "Quit polling the event ring.\n");
379}
380#endif
381
382/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700383 * Start the HC after it was halted.
384 *
385 * This function is called by the USB core when the HC driver is added.
386 * Its opposite is xhci_stop().
387 *
388 * xhci_init() must be called once before this function can be called.
389 * Reset the HC, enable device slot contexts, program DCBAAP, and
390 * set command ring pointer and event ring pointer.
391 *
392 * Setup MSI-X vectors and enable interrupts.
393 */
394int xhci_run(struct usb_hcd *hcd)
395{
396 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700397 u64 temp_64;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700398 u32 ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700399 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700400 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700401 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700402
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700403 hcd->uses_new_polling = 1;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700404
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700405 xhci_dbg(xhci, "xhci_run\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700406 /* unregister the legacy interrupt */
407 if (hcd->irq)
408 free_irq(hcd->irq, hcd);
409 hcd->irq = -1;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700410
Dong Nguyen43b86af2010-07-21 16:56:08 -0700411 ret = xhci_setup_msix(xhci);
412 if (ret)
413 /* fall back to msi*/
414 ret = xhci_setup_msi(xhci);
415
416 if (ret) {
417 /* fall back to legacy interrupt*/
418 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
419 hcd->irq_descr, hcd);
420 if (ret) {
421 xhci_err(xhci, "request interrupt %d failed\n",
422 pdev->irq);
423 return ret;
424 }
425 hcd->irq = pdev->irq;
426 }
427
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700428#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
429 init_timer(&xhci->event_ring_timer);
430 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700431 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700432 /* Poll the event ring */
433 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
434 xhci->zombie = 0;
435 xhci_dbg(xhci, "Setting event ring polling timer\n");
436 add_timer(&xhci->event_ring_timer);
437#endif
438
Sarah Sharp66e49d82009-07-27 12:03:46 -0700439 xhci_dbg(xhci, "Command ring memory map follows:\n");
440 xhci_debug_ring(xhci, xhci->cmd_ring);
441 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
442 xhci_dbg_cmd_ptrs(xhci);
443
444 xhci_dbg(xhci, "ERST memory map follows:\n");
445 xhci_dbg_erst(xhci, &xhci->erst);
446 xhci_dbg(xhci, "Event ring:\n");
447 xhci_debug_ring(xhci, xhci->event_ring);
448 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
449 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
450 temp_64 &= ~ERST_PTR_MASK;
451 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
452
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700453 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
454 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700455 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700456 temp |= (u32) 160;
457 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
458
459 /* Set the HCD state before we enable the irqs */
460 hcd->state = HC_STATE_RUNNING;
461 temp = xhci_readl(xhci, &xhci->op_regs->command);
462 temp |= (CMD_EIE);
463 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
464 temp);
465 xhci_writel(xhci, temp, &xhci->op_regs->command);
466
467 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700468 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
469 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700470 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
471 &xhci->ir_set->irq_pending);
472 xhci_print_ir_set(xhci, xhci->ir_set, 0);
473
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700474 if (NUM_TEST_NOOPS > 0)
Sarah Sharp23e3be12009-04-29 19:05:20 -0700475 doorbell = xhci_setup_one_noop(xhci);
Sarah Sharp02386342010-05-24 13:25:28 -0700476 if (xhci->quirks & XHCI_NEC_HOST)
477 xhci_queue_vendor_command(xhci, 0, 0, 0,
478 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700479
Sarah Sharped074532010-05-24 13:25:21 -0700480 if (xhci_start(xhci)) {
481 xhci_halt(xhci);
482 return -ENODEV;
483 }
484
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700485 if (doorbell)
486 (*doorbell)(xhci);
Sarah Sharp02386342010-05-24 13:25:28 -0700487 if (xhci->quirks & XHCI_NEC_HOST)
488 xhci_ring_cmd_db(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700489
490 xhci_dbg(xhci, "Finished xhci_run\n");
491 return 0;
492}
493
494/*
495 * Stop xHCI driver.
496 *
497 * This function is called by the USB core when the HC driver is removed.
498 * Its opposite is xhci_run().
499 *
500 * Disable device contexts, disable IRQs, and quiesce the HC.
501 * Reset the HC, finish any completed transactions, and cleanup memory.
502 */
503void xhci_stop(struct usb_hcd *hcd)
504{
505 u32 temp;
506 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
507
508 spin_lock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700509 xhci_halt(xhci);
510 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700511 xhci_cleanup_msix(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700512 spin_unlock_irq(&xhci->lock);
513
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700514#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
515 /* Tell the event ring poll function not to reschedule */
516 xhci->zombie = 1;
517 del_timer_sync(&xhci->event_ring_timer);
518#endif
519
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700520 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
521 temp = xhci_readl(xhci, &xhci->op_regs->status);
522 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
523 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
524 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
525 &xhci->ir_set->irq_pending);
526 xhci_print_ir_set(xhci, xhci->ir_set, 0);
527
528 xhci_dbg(xhci, "cleaning up memory\n");
529 xhci_mem_cleanup(xhci);
530 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
531 xhci_readl(xhci, &xhci->op_regs->status));
532}
533
534/*
535 * Shutdown HC (not bus-specific)
536 *
537 * This is called when the machine is rebooting or halting. We assume that the
538 * machine will be powered off, and the HC's internal state will be reset.
539 * Don't bother to free memory.
540 */
541void xhci_shutdown(struct usb_hcd *hcd)
542{
543 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
544
545 spin_lock_irq(&xhci->lock);
546 xhci_halt(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700547 xhci_cleanup_msix(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700548 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700549
550 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
551 xhci_readl(xhci, &xhci->op_regs->status));
552}
553
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700554#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700555static void xhci_save_registers(struct xhci_hcd *xhci)
556{
557 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
558 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
559 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
560 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
561 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
562 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
563 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
564 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
565 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
566}
567
568static void xhci_restore_registers(struct xhci_hcd *xhci)
569{
570 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
571 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
572 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
573 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
574 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
575 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
576 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
577 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
578}
579
Sarah Sharp89821322010-11-12 11:59:31 -0800580static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
581{
582 u64 val_64;
583
584 /* step 2: initialize command ring buffer */
585 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
586 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
587 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
588 xhci->cmd_ring->dequeue) &
589 (u64) ~CMD_RING_RSVD_BITS) |
590 xhci->cmd_ring->cycle_state;
591 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
592 (long unsigned long) val_64);
593 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
594}
595
596/*
597 * The whole command ring must be cleared to zero when we suspend the host.
598 *
599 * The host doesn't save the command ring pointer in the suspend well, so we
600 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
601 * aligned, because of the reserved bits in the command ring dequeue pointer
602 * register. Therefore, we can't just set the dequeue pointer back in the
603 * middle of the ring (TRBs are 16-byte aligned).
604 */
605static void xhci_clear_command_ring(struct xhci_hcd *xhci)
606{
607 struct xhci_ring *ring;
608 struct xhci_segment *seg;
609
610 ring = xhci->cmd_ring;
611 seg = ring->deq_seg;
612 do {
613 memset(seg->trbs, 0, SEGMENT_SIZE);
614 seg = seg->next;
615 } while (seg != ring->deq_seg);
616
617 /* Reset the software enqueue and dequeue pointers */
618 ring->deq_seg = ring->first_seg;
619 ring->dequeue = ring->first_seg->trbs;
620 ring->enq_seg = ring->deq_seg;
621 ring->enqueue = ring->dequeue;
622
623 /*
624 * Ring is now zeroed, so the HW should look for change of ownership
625 * when the cycle bit is set to 1.
626 */
627 ring->cycle_state = 1;
628
629 /*
630 * Reset the hardware dequeue pointer.
631 * Yes, this will need to be re-written after resume, but we're paranoid
632 * and want to make sure the hardware doesn't access bogus memory
633 * because, say, the BIOS or an SMI started the host without changing
634 * the command ring pointers.
635 */
636 xhci_set_cmd_ring_deq(xhci);
637}
638
Andiry Xu5535b1d2010-10-14 07:23:06 -0700639/*
640 * Stop HC (not bus-specific)
641 *
642 * This is called when the machine transition into S3/S4 mode.
643 *
644 */
645int xhci_suspend(struct xhci_hcd *xhci)
646{
647 int rc = 0;
648 struct usb_hcd *hcd = xhci_to_hcd(xhci);
649 u32 command;
650
651 spin_lock_irq(&xhci->lock);
652 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
653 /* step 1: stop endpoint */
654 /* skipped assuming that port suspend has done */
655
656 /* step 2: clear Run/Stop bit */
657 command = xhci_readl(xhci, &xhci->op_regs->command);
658 command &= ~CMD_RUN;
659 xhci_writel(xhci, command, &xhci->op_regs->command);
660 if (handshake(xhci, &xhci->op_regs->status,
661 STS_HALT, STS_HALT, 100*100)) {
662 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
663 spin_unlock_irq(&xhci->lock);
664 return -ETIMEDOUT;
665 }
Sarah Sharp89821322010-11-12 11:59:31 -0800666 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700667
668 /* step 3: save registers */
669 xhci_save_registers(xhci);
670
671 /* step 4: set CSS flag */
672 command = xhci_readl(xhci, &xhci->op_regs->command);
673 command |= CMD_CSS;
674 xhci_writel(xhci, command, &xhci->op_regs->command);
675 if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10*100)) {
676 xhci_warn(xhci, "WARN: xHC CMD_CSS timeout\n");
677 spin_unlock_irq(&xhci->lock);
678 return -ETIMEDOUT;
679 }
680 /* step 5: remove core well power */
681 xhci_cleanup_msix(xhci);
682 spin_unlock_irq(&xhci->lock);
683
684 return rc;
685}
686
687/*
688 * start xHC (not bus-specific)
689 *
690 * This is called when the machine transition from S3/S4 mode.
691 *
692 */
693int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
694{
695 u32 command, temp = 0;
696 struct usb_hcd *hcd = xhci_to_hcd(xhci);
697 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700698 int old_state, retval;
699
700 old_state = hcd->state;
701 if (time_before(jiffies, xhci->next_statechange))
702 msleep(100);
703
704 spin_lock_irq(&xhci->lock);
705
706 if (!hibernated) {
707 /* step 1: restore register */
708 xhci_restore_registers(xhci);
709 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800710 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700711 /* step 3: restore state and start state*/
712 /* step 3: set CRS flag */
713 command = xhci_readl(xhci, &xhci->op_regs->command);
714 command |= CMD_CRS;
715 xhci_writel(xhci, command, &xhci->op_regs->command);
716 if (handshake(xhci, &xhci->op_regs->status,
717 STS_RESTORE, 0, 10*100)) {
718 xhci_dbg(xhci, "WARN: xHC CMD_CSS timeout\n");
719 spin_unlock_irq(&xhci->lock);
720 return -ETIMEDOUT;
721 }
722 temp = xhci_readl(xhci, &xhci->op_regs->status);
723 }
724
725 /* If restore operation fails, re-initialize the HC during resume */
726 if ((temp & STS_SRE) || hibernated) {
727 usb_root_hub_lost_power(hcd->self.root_hub);
728
729 xhci_dbg(xhci, "Stop HCD\n");
730 xhci_halt(xhci);
731 xhci_reset(xhci);
732 if (hibernated)
733 xhci_cleanup_msix(xhci);
734 spin_unlock_irq(&xhci->lock);
735
736#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
737 /* Tell the event ring poll function not to reschedule */
738 xhci->zombie = 1;
739 del_timer_sync(&xhci->event_ring_timer);
740#endif
741
742 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
743 temp = xhci_readl(xhci, &xhci->op_regs->status);
744 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
745 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
746 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
747 &xhci->ir_set->irq_pending);
748 xhci_print_ir_set(xhci, xhci->ir_set, 0);
749
750 xhci_dbg(xhci, "cleaning up memory\n");
751 xhci_mem_cleanup(xhci);
752 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
753 xhci_readl(xhci, &xhci->op_regs->status));
754
755 xhci_dbg(xhci, "Initialize the HCD\n");
756 retval = xhci_init(hcd);
757 if (retval)
758 return retval;
759
760 xhci_dbg(xhci, "Start the HCD\n");
761 retval = xhci_run(hcd);
762 if (!retval)
763 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
764 hcd->state = HC_STATE_SUSPENDED;
765 return retval;
766 }
767
Andiry Xu74bb8442010-10-27 16:44:52 +0800768 spin_unlock_irq(&xhci->lock);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700769 /* Re-setup MSI-X */
770 if (hcd->irq)
771 free_irq(hcd->irq, hcd);
772 hcd->irq = -1;
773
774 retval = xhci_setup_msix(xhci);
775 if (retval)
776 /* fall back to msi*/
777 retval = xhci_setup_msi(xhci);
778
779 if (retval) {
780 /* fall back to legacy interrupt*/
781 retval = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
782 hcd->irq_descr, hcd);
783 if (retval) {
784 xhci_err(xhci, "request interrupt %d failed\n",
785 pdev->irq);
786 return retval;
787 }
788 hcd->irq = pdev->irq;
789 }
790
Andiry Xu74bb8442010-10-27 16:44:52 +0800791 spin_lock_irq(&xhci->lock);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700792 /* step 4: set Run/Stop bit */
793 command = xhci_readl(xhci, &xhci->op_regs->command);
794 command |= CMD_RUN;
795 xhci_writel(xhci, command, &xhci->op_regs->command);
796 handshake(xhci, &xhci->op_regs->status, STS_HALT,
797 0, 250 * 1000);
798
799 /* step 5: walk topology and initialize portsc,
800 * portpmsc and portli
801 */
802 /* this is done in bus_resume */
803
804 /* step 6: restart each of the previously
805 * Running endpoints by ringing their doorbells
806 */
807
808 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
809 if (!hibernated)
810 hcd->state = old_state;
811 else
812 hcd->state = HC_STATE_SUSPENDED;
813
814 spin_unlock_irq(&xhci->lock);
815 return 0;
816}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700817#endif /* CONFIG_PM */
818
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700819/*-------------------------------------------------------------------------*/
820
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700821/**
822 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
823 * HCDs. Find the index for an endpoint given its descriptor. Use the return
824 * value to right shift 1 for the bitmask.
825 *
826 * Index = (epnum * 2) + direction - 1,
827 * where direction = 0 for OUT, 1 for IN.
828 * For control endpoints, the IN index is used (OUT index is unused), so
829 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
830 */
831unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
832{
833 unsigned int index;
834 if (usb_endpoint_xfer_control(desc))
835 index = (unsigned int) (usb_endpoint_num(desc)*2);
836 else
837 index = (unsigned int) (usb_endpoint_num(desc)*2) +
838 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
839 return index;
840}
841
Sarah Sharpf94e01862009-04-27 19:58:38 -0700842/* Find the flag for this endpoint (for use in the control context). Use the
843 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
844 * bit 1, etc.
845 */
846unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
847{
848 return 1 << (xhci_get_endpoint_index(desc) + 1);
849}
850
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700851/* Find the flag for this endpoint (for use in the control context). Use the
852 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
853 * bit 1, etc.
854 */
855unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
856{
857 return 1 << (ep_index + 1);
858}
859
Sarah Sharpf94e01862009-04-27 19:58:38 -0700860/* Compute the last valid endpoint context index. Basically, this is the
861 * endpoint index plus one. For slot contexts with more than valid endpoint,
862 * we find the most significant bit set in the added contexts flags.
863 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
864 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
865 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700866unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700867{
868 return fls(added_ctxs) - 1;
869}
870
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700871/* Returns 1 if the arguments are OK;
872 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
873 */
874int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -0700875 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
876 const char *func) {
877 struct xhci_hcd *xhci;
878 struct xhci_virt_device *virt_dev;
879
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700880 if (!hcd || (check_ep && !ep) || !udev) {
881 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
882 func);
883 return -EINVAL;
884 }
885 if (!udev->parent) {
886 printk(KERN_DEBUG "xHCI %s called for root hub\n",
887 func);
888 return 0;
889 }
Andiry Xu64927732010-10-14 07:22:45 -0700890
891 if (check_virt_dev) {
892 xhci = hcd_to_xhci(hcd);
893 if (!udev->slot_id || !xhci->devs
894 || !xhci->devs[udev->slot_id]) {
895 printk(KERN_DEBUG "xHCI %s called with unaddressed "
896 "device\n", func);
897 return -EINVAL;
898 }
899
900 virt_dev = xhci->devs[udev->slot_id];
901 if (virt_dev->udev != udev) {
902 printk(KERN_DEBUG "xHCI %s called with udev and "
903 "virt_dev does not match\n", func);
904 return -EINVAL;
905 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700906 }
Andiry Xu64927732010-10-14 07:22:45 -0700907
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700908 return 1;
909}
910
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700911static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700912 struct usb_device *udev, struct xhci_command *command,
913 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700914
915/*
916 * Full speed devices may have a max packet size greater than 8 bytes, but the
917 * USB core doesn't know that until it reads the first 8 bytes of the
918 * descriptor. If the usb_device's max packet size changes after that point,
919 * we need to issue an evaluate context command and wait on it.
920 */
921static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
922 unsigned int ep_index, struct urb *urb)
923{
924 struct xhci_container_ctx *in_ctx;
925 struct xhci_container_ctx *out_ctx;
926 struct xhci_input_control_ctx *ctrl_ctx;
927 struct xhci_ep_ctx *ep_ctx;
928 int max_packet_size;
929 int hw_max_packet_size;
930 int ret = 0;
931
932 out_ctx = xhci->devs[slot_id]->out_ctx;
933 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
934 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
935 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
936 if (hw_max_packet_size != max_packet_size) {
937 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
938 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
939 max_packet_size);
940 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
941 hw_max_packet_size);
942 xhci_dbg(xhci, "Issuing evaluate context command.\n");
943
944 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700945 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
946 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700947 in_ctx = xhci->devs[slot_id]->in_ctx;
948 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
949 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
950 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
951
952 /* Set up the input context flags for the command */
953 /* FIXME: This won't work if a non-default control endpoint
954 * changes max packet sizes.
955 */
956 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
957 ctrl_ctx->add_flags = EP0_FLAG;
958 ctrl_ctx->drop_flags = 0;
959
960 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
961 xhci_dbg_ctx(xhci, in_ctx, ep_index);
962 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
963 xhci_dbg_ctx(xhci, out_ctx, ep_index);
964
Sarah Sharp913a8a32009-09-04 10:53:13 -0700965 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
966 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700967
968 /* Clean up the input context for later use by bandwidth
969 * functions.
970 */
971 ctrl_ctx->add_flags = SLOT_FLAG;
972 }
973 return ret;
974}
975
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700976/*
977 * non-error returns are a promise to giveback() the urb later
978 * we drop ownership so next owner (or urb unlink) can get it
979 */
980int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
981{
982 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
983 unsigned long flags;
984 int ret = 0;
985 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700986 struct urb_priv *urb_priv;
987 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700988
Andiry Xu64927732010-10-14 07:22:45 -0700989 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
990 true, true, __func__) <= 0)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700991 return -EINVAL;
992
993 slot_id = urb->dev->slot_id;
994 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700995
Alan Stern541c7d42010-06-22 16:39:10 -0400996 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700997 if (!in_interrupt())
998 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
999 ret = -ESHUTDOWN;
1000 goto exit;
1001 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001002
1003 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1004 size = urb->number_of_packets;
1005 else
1006 size = 1;
1007
1008 urb_priv = kzalloc(sizeof(struct urb_priv) +
1009 size * sizeof(struct xhci_td *), mem_flags);
1010 if (!urb_priv)
1011 return -ENOMEM;
1012
1013 for (i = 0; i < size; i++) {
1014 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
1015 if (!urb_priv->td[i]) {
1016 urb_priv->length = i;
1017 xhci_urb_free_priv(xhci, urb_priv);
1018 return -ENOMEM;
1019 }
1020 }
1021
1022 urb_priv->length = size;
1023 urb_priv->td_cnt = 0;
1024 urb->hcpriv = urb_priv;
1025
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001026 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1027 /* Check to see if the max packet size for the default control
1028 * endpoint changed during FS device enumeration
1029 */
1030 if (urb->dev->speed == USB_SPEED_FULL) {
1031 ret = xhci_check_maxpacket(xhci, slot_id,
1032 ep_index, urb);
1033 if (ret < 0)
1034 return ret;
1035 }
1036
Sarah Sharpb11069f2009-07-27 12:03:23 -07001037 /* We have a spinlock and interrupts disabled, so we must pass
1038 * atomic context to this function, which may allocate memory.
1039 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001040 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001041 if (xhci->xhc_state & XHCI_STATE_DYING)
1042 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001043 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001044 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001045 spin_unlock_irqrestore(&xhci->lock, flags);
1046 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1047 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001048 if (xhci->xhc_state & XHCI_STATE_DYING)
1049 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001050 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1051 EP_GETTING_STREAMS) {
1052 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1053 "is transitioning to using streams.\n");
1054 ret = -EINVAL;
1055 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1056 EP_GETTING_NO_STREAMS) {
1057 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1058 "is transitioning to "
1059 "not having streams.\n");
1060 ret = -EINVAL;
1061 } else {
1062 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1063 slot_id, ep_index);
1064 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001065 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001066 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1067 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001068 if (xhci->xhc_state & XHCI_STATE_DYING)
1069 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001070 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1071 slot_id, ep_index);
1072 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001073 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001074 spin_lock_irqsave(&xhci->lock, flags);
1075 if (xhci->xhc_state & XHCI_STATE_DYING)
1076 goto dying;
1077 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1078 slot_id, ep_index);
1079 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001080 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001081exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001082 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001083dying:
Andiry Xu8e51adc2010-07-22 15:23:31 -07001084 xhci_urb_free_priv(xhci, urb_priv);
1085 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001086 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1087 "non-responsive xHCI host.\n",
1088 urb->ep->desc.bEndpointAddress, urb);
1089 spin_unlock_irqrestore(&xhci->lock, flags);
1090 return -ESHUTDOWN;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001091}
1092
Sarah Sharp021bff92010-07-29 22:12:20 -07001093/* Get the right ring for the given URB.
1094 * If the endpoint supports streams, boundary check the URB's stream ID.
1095 * If the endpoint doesn't support streams, return the singular endpoint ring.
1096 */
1097static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1098 struct urb *urb)
1099{
1100 unsigned int slot_id;
1101 unsigned int ep_index;
1102 unsigned int stream_id;
1103 struct xhci_virt_ep *ep;
1104
1105 slot_id = urb->dev->slot_id;
1106 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1107 stream_id = urb->stream_id;
1108 ep = &xhci->devs[slot_id]->eps[ep_index];
1109 /* Common case: no streams */
1110 if (!(ep->ep_state & EP_HAS_STREAMS))
1111 return ep->ring;
1112
1113 if (stream_id == 0) {
1114 xhci_warn(xhci,
1115 "WARN: Slot ID %u, ep index %u has streams, "
1116 "but URB has no stream ID.\n",
1117 slot_id, ep_index);
1118 return NULL;
1119 }
1120
1121 if (stream_id < ep->stream_info->num_streams)
1122 return ep->stream_info->stream_rings[stream_id];
1123
1124 xhci_warn(xhci,
1125 "WARN: Slot ID %u, ep index %u has "
1126 "stream IDs 1 to %u allocated, "
1127 "but stream ID %u is requested.\n",
1128 slot_id, ep_index,
1129 ep->stream_info->num_streams - 1,
1130 stream_id);
1131 return NULL;
1132}
1133
Sarah Sharpae636742009-04-29 19:02:31 -07001134/*
1135 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1136 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1137 * should pick up where it left off in the TD, unless a Set Transfer Ring
1138 * Dequeue Pointer is issued.
1139 *
1140 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1141 * the ring. Since the ring is a contiguous structure, they can't be physically
1142 * removed. Instead, there are two options:
1143 *
1144 * 1) If the HC is in the middle of processing the URB to be canceled, we
1145 * simply move the ring's dequeue pointer past those TRBs using the Set
1146 * Transfer Ring Dequeue Pointer command. This will be the common case,
1147 * when drivers timeout on the last submitted URB and attempt to cancel.
1148 *
1149 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1150 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1151 * HC will need to invalidate the any TRBs it has cached after the stop
1152 * endpoint command, as noted in the xHCI 0.95 errata.
1153 *
1154 * 3) The TD may have completed by the time the Stop Endpoint Command
1155 * completes, so software needs to handle that case too.
1156 *
1157 * This function should protect against the TD enqueueing code ringing the
1158 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1159 * It also needs to account for multiple cancellations on happening at the same
1160 * time for the same endpoint.
1161 *
1162 * Note that this function can be called in any context, or so says
1163 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001164 */
1165int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1166{
Sarah Sharpae636742009-04-29 19:02:31 -07001167 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001168 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001169 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001170 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001171 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001172 struct xhci_td *td;
1173 unsigned int ep_index;
1174 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001175 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001176
1177 xhci = hcd_to_xhci(hcd);
1178 spin_lock_irqsave(&xhci->lock, flags);
1179 /* Make sure the URB hasn't completed or been unlinked already */
1180 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1181 if (ret || !urb->hcpriv)
1182 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001183 temp = xhci_readl(xhci, &xhci->op_regs->status);
1184 if (temp == 0xffffffff) {
1185 xhci_dbg(xhci, "HW died, freeing TD.\n");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001186 urb_priv = urb->hcpriv;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001187
1188 usb_hcd_unlink_urb_from_ep(hcd, urb);
1189 spin_unlock_irqrestore(&xhci->lock, flags);
1190 usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001191 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001192 return ret;
1193 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001194 if (xhci->xhc_state & XHCI_STATE_DYING) {
1195 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1196 "non-responsive xHCI host.\n",
1197 urb->ep->desc.bEndpointAddress, urb);
1198 /* Let the stop endpoint command watchdog timer (which set this
1199 * state) finish cleaning up the endpoint TD lists. We must
1200 * have caught it in the middle of dropping a lock and giving
1201 * back an URB.
1202 */
1203 goto done;
1204 }
Sarah Sharpae636742009-04-29 19:02:31 -07001205
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001206 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -07001207 xhci_dbg(xhci, "Event ring:\n");
1208 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -07001209 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001210 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001211 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1212 if (!ep_ring) {
1213 ret = -EINVAL;
1214 goto done;
1215 }
1216
Sarah Sharp66e49d82009-07-27 12:03:46 -07001217 xhci_dbg(xhci, "Endpoint ring:\n");
1218 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -07001219
Andiry Xu8e51adc2010-07-22 15:23:31 -07001220 urb_priv = urb->hcpriv;
1221
1222 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1223 td = urb_priv->td[i];
1224 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1225 }
1226
Sarah Sharpae636742009-04-29 19:02:31 -07001227 /* Queue a stop endpoint command, but only if this is
1228 * the first cancellation to be handled.
1229 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001230 if (!(ep->ep_state & EP_HALT_PENDING)) {
1231 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001232 ep->stop_cmds_pending++;
1233 ep->stop_cmd_timer.expires = jiffies +
1234 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1235 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001236 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001237 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001238 }
1239done:
1240 spin_unlock_irqrestore(&xhci->lock, flags);
1241 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001242}
1243
Sarah Sharpf94e01862009-04-27 19:58:38 -07001244/* Drop an endpoint from a new bandwidth configuration for this device.
1245 * Only one call to this function is allowed per endpoint before
1246 * check_bandwidth() or reset_bandwidth() must be called.
1247 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1248 * add the endpoint to the schedule with possibly new parameters denoted by a
1249 * different endpoint descriptor in usb_host_endpoint.
1250 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1251 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001252 *
1253 * The USB core will not allow URBs to be queued to an endpoint that is being
1254 * disabled, so there's no need for mutual exclusion to protect
1255 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001256 */
1257int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1258 struct usb_host_endpoint *ep)
1259{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001260 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001261 struct xhci_container_ctx *in_ctx, *out_ctx;
1262 struct xhci_input_control_ctx *ctrl_ctx;
1263 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001264 unsigned int last_ctx;
1265 unsigned int ep_index;
1266 struct xhci_ep_ctx *ep_ctx;
1267 u32 drop_flag;
1268 u32 new_add_flags, new_drop_flags, new_slot_info;
1269 int ret;
1270
Andiry Xu64927732010-10-14 07:22:45 -07001271 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001272 if (ret <= 0)
1273 return ret;
1274 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001275 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001276
1277 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1278 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1279 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1280 __func__, drop_flag);
1281 return 0;
1282 }
1283
Sarah Sharpf94e01862009-04-27 19:58:38 -07001284 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001285 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1286 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001287 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001288 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001289 /* If the HC already knows the endpoint is disabled,
1290 * or the HCD has noted it is disabled, ignore this request
1291 */
1292 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
John Yound115b042009-07-27 12:05:15 -07001293 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001294 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1295 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001296 return 0;
1297 }
1298
John Yound115b042009-07-27 12:05:15 -07001299 ctrl_ctx->drop_flags |= drop_flag;
1300 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001301
Sarah Sharp0a023c62009-09-18 08:55:12 -07001302 ctrl_ctx->add_flags &= ~drop_flag;
John Yound115b042009-07-27 12:05:15 -07001303 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001304
John Yound115b042009-07-27 12:05:15 -07001305 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
1306 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001307 /* Update the last valid endpoint context, if we deleted the last one */
John Yound115b042009-07-27 12:05:15 -07001308 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
1309 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1310 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001311 }
John Yound115b042009-07-27 12:05:15 -07001312 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001313
1314 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1315
Sarah Sharpf94e01862009-04-27 19:58:38 -07001316 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1317 (unsigned int) ep->desc.bEndpointAddress,
1318 udev->slot_id,
1319 (unsigned int) new_drop_flags,
1320 (unsigned int) new_add_flags,
1321 (unsigned int) new_slot_info);
1322 return 0;
1323}
1324
1325/* Add an endpoint to a new possible bandwidth configuration for this device.
1326 * Only one call to this function is allowed per endpoint before
1327 * check_bandwidth() or reset_bandwidth() must be called.
1328 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1329 * add the endpoint to the schedule with possibly new parameters denoted by a
1330 * different endpoint descriptor in usb_host_endpoint.
1331 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1332 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001333 *
1334 * The USB core will not allow URBs to be queued to an endpoint until the
1335 * configuration or alt setting is installed in the device, so there's no need
1336 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001337 */
1338int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1339 struct usb_host_endpoint *ep)
1340{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001341 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001342 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001343 unsigned int ep_index;
1344 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001345 struct xhci_slot_ctx *slot_ctx;
1346 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001347 u32 added_ctxs;
1348 unsigned int last_ctx;
1349 u32 new_add_flags, new_drop_flags, new_slot_info;
1350 int ret = 0;
1351
Andiry Xu64927732010-10-14 07:22:45 -07001352 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001353 if (ret <= 0) {
1354 /* So we won't queue a reset ep command for a root hub */
1355 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001356 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001357 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001358 xhci = hcd_to_xhci(hcd);
1359
1360 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1361 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1362 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1363 /* FIXME when we have to issue an evaluate endpoint command to
1364 * deal with ep0 max packet size changing once we get the
1365 * descriptors
1366 */
1367 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1368 __func__, added_ctxs);
1369 return 0;
1370 }
1371
Sarah Sharpf94e01862009-04-27 19:58:38 -07001372 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001373 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1374 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001375 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001376 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001377 /* If the HCD has already noted the endpoint is enabled,
1378 * ignore this request.
1379 */
John Yound115b042009-07-27 12:05:15 -07001380 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001381 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1382 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001383 return 0;
1384 }
1385
Sarah Sharpf88ba782009-05-14 11:44:22 -07001386 /*
1387 * Configuration and alternate setting changes must be done in
1388 * process context, not interrupt context (or so documenation
1389 * for usb_set_interface() and usb_set_configuration() claim).
1390 */
1391 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
Oliver Neukum319c3ea2009-12-16 19:43:59 +01001392 udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001393 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1394 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001395 return -ENOMEM;
1396 }
1397
John Yound115b042009-07-27 12:05:15 -07001398 ctrl_ctx->add_flags |= added_ctxs;
1399 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001400
1401 /* If xhci_endpoint_disable() was called for this endpoint, but the
1402 * xHC hasn't been notified yet through the check_bandwidth() call,
1403 * this re-adds a new state for the endpoint from the new endpoint
1404 * descriptors. We must drop and re-add this endpoint, so we leave the
1405 * drop flags alone.
1406 */
John Yound115b042009-07-27 12:05:15 -07001407 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001408
John Yound115b042009-07-27 12:05:15 -07001409 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001410 /* Update the last valid endpoint context, if we just added one past */
John Yound115b042009-07-27 12:05:15 -07001411 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1412 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1413 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001414 }
John Yound115b042009-07-27 12:05:15 -07001415 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001416
Sarah Sharpa1587d92009-07-27 12:03:15 -07001417 /* Store the usb_device pointer for later use */
1418 ep->hcpriv = udev;
1419
Sarah Sharpf94e01862009-04-27 19:58:38 -07001420 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1421 (unsigned int) ep->desc.bEndpointAddress,
1422 udev->slot_id,
1423 (unsigned int) new_drop_flags,
1424 (unsigned int) new_add_flags,
1425 (unsigned int) new_slot_info);
1426 return 0;
1427}
1428
John Yound115b042009-07-27 12:05:15 -07001429static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001430{
John Yound115b042009-07-27 12:05:15 -07001431 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001432 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001433 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001434 int i;
1435
1436 /* When a device's add flag and drop flag are zero, any subsequent
1437 * configure endpoint command will leave that endpoint's state
1438 * untouched. Make sure we don't leave any old state in the input
1439 * endpoint contexts.
1440 */
John Yound115b042009-07-27 12:05:15 -07001441 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1442 ctrl_ctx->drop_flags = 0;
1443 ctrl_ctx->add_flags = 0;
1444 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1445 slot_ctx->dev_info &= ~LAST_CTX_MASK;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001446 /* Endpoint 0 is always valid */
John Yound115b042009-07-27 12:05:15 -07001447 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001448 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001449 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001450 ep_ctx->ep_info = 0;
1451 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001452 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001453 ep_ctx->tx_info = 0;
1454 }
1455}
1456
Sarah Sharpf2217e82009-08-07 14:04:43 -07001457static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001458 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001459{
1460 int ret;
1461
Sarah Sharp913a8a32009-09-04 10:53:13 -07001462 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001463 case COMP_ENOMEM:
1464 dev_warn(&udev->dev, "Not enough host controller resources "
1465 "for new device state.\n");
1466 ret = -ENOMEM;
1467 /* FIXME: can we allocate more resources for the HC? */
1468 break;
1469 case COMP_BW_ERR:
1470 dev_warn(&udev->dev, "Not enough bandwidth "
1471 "for new device state.\n");
1472 ret = -ENOSPC;
1473 /* FIXME: can we go back to the old state? */
1474 break;
1475 case COMP_TRB_ERR:
1476 /* the HCD set up something wrong */
1477 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1478 "add flag = 1, "
1479 "and endpoint is not disabled.\n");
1480 ret = -EINVAL;
1481 break;
1482 case COMP_SUCCESS:
1483 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1484 ret = 0;
1485 break;
1486 default:
1487 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001488 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001489 ret = -EINVAL;
1490 break;
1491 }
1492 return ret;
1493}
1494
1495static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001496 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001497{
1498 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001499 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001500
Sarah Sharp913a8a32009-09-04 10:53:13 -07001501 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001502 case COMP_EINVAL:
1503 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1504 "context command.\n");
1505 ret = -EINVAL;
1506 break;
1507 case COMP_EBADSLT:
1508 dev_warn(&udev->dev, "WARN: slot not enabled for"
1509 "evaluate context command.\n");
1510 case COMP_CTX_STATE:
1511 dev_warn(&udev->dev, "WARN: invalid context state for "
1512 "evaluate context command.\n");
1513 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1514 ret = -EINVAL;
1515 break;
1516 case COMP_SUCCESS:
1517 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1518 ret = 0;
1519 break;
1520 default:
1521 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001522 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001523 ret = -EINVAL;
1524 break;
1525 }
1526 return ret;
1527}
1528
1529/* Issue a configure endpoint command or evaluate context command
1530 * and wait for it to finish.
1531 */
1532static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001533 struct usb_device *udev,
1534 struct xhci_command *command,
1535 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001536{
1537 int ret;
1538 int timeleft;
1539 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001540 struct xhci_container_ctx *in_ctx;
1541 struct completion *cmd_completion;
1542 int *cmd_status;
1543 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001544
1545 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001546 virt_dev = xhci->devs[udev->slot_id];
1547 if (command) {
1548 in_ctx = command->in_ctx;
1549 cmd_completion = command->completion;
1550 cmd_status = &command->status;
1551 command->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08001552
1553 /* Enqueue pointer can be left pointing to the link TRB,
1554 * we must handle that
1555 */
1556 if ((command->command_trb->link.control & TRB_TYPE_BITMASK)
1557 == TRB_TYPE(TRB_LINK))
1558 command->command_trb =
1559 xhci->cmd_ring->enq_seg->next->trbs;
1560
Sarah Sharp913a8a32009-09-04 10:53:13 -07001561 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1562 } else {
1563 in_ctx = virt_dev->in_ctx;
1564 cmd_completion = &virt_dev->cmd_completion;
1565 cmd_status = &virt_dev->cmd_status;
1566 }
Andiry Xu1d680642010-03-12 17:10:04 +08001567 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001568
Sarah Sharpf2217e82009-08-07 14:04:43 -07001569 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001570 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1571 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001572 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07001573 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001574 udev->slot_id);
1575 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08001576 if (command)
1577 list_del(&command->cmd_list);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001578 spin_unlock_irqrestore(&xhci->lock, flags);
1579 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1580 return -ENOMEM;
1581 }
1582 xhci_ring_cmd_db(xhci);
1583 spin_unlock_irqrestore(&xhci->lock, flags);
1584
1585 /* Wait for the configure endpoint command to complete */
1586 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07001587 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001588 USB_CTRL_SET_TIMEOUT);
1589 if (timeleft <= 0) {
1590 xhci_warn(xhci, "%s while waiting for %s command\n",
1591 timeleft == 0 ? "Timeout" : "Signal",
1592 ctx_change == 0 ?
1593 "configure endpoint" :
1594 "evaluate context");
1595 /* FIXME cancel the configure endpoint command */
1596 return -ETIME;
1597 }
1598
1599 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001600 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1601 return xhci_evaluate_context_result(xhci, udev, cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001602}
1603
Sarah Sharpf88ba782009-05-14 11:44:22 -07001604/* Called after one or more calls to xhci_add_endpoint() or
1605 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1606 * to call xhci_reset_bandwidth().
1607 *
1608 * Since we are in the middle of changing either configuration or
1609 * installing a new alt setting, the USB core won't allow URBs to be
1610 * enqueued for any endpoint on the old config or interface. Nothing
1611 * else should be touching the xhci->devs[slot_id] structure, so we
1612 * don't need to take the xhci->lock for manipulating that.
1613 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001614int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1615{
1616 int i;
1617 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001618 struct xhci_hcd *xhci;
1619 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07001620 struct xhci_input_control_ctx *ctrl_ctx;
1621 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001622
Andiry Xu64927732010-10-14 07:22:45 -07001623 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001624 if (ret <= 0)
1625 return ret;
1626 xhci = hcd_to_xhci(hcd);
1627
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001628 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001629 virt_dev = xhci->devs[udev->slot_id];
1630
1631 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07001632 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1633 ctrl_ctx->add_flags |= SLOT_FLAG;
1634 ctrl_ctx->add_flags &= ~EP0_FLAG;
1635 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1636 ctrl_ctx->drop_flags &= ~EP0_FLAG;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001637 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07001638 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1639 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1640 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001641
Sarah Sharp913a8a32009-09-04 10:53:13 -07001642 ret = xhci_configure_endpoint(xhci, udev, NULL,
1643 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001644 if (ret) {
1645 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001646 return ret;
1647 }
1648
1649 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07001650 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1651 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001652
John Yound115b042009-07-27 12:05:15 -07001653 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001654 /* Install new rings and free or cache any old rings */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001655 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001656 if (!virt_dev->eps[i].new_ring)
1657 continue;
1658 /* Only cache or free the old ring if it exists.
1659 * It may not if this is the first add of an endpoint.
1660 */
1661 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08001662 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001663 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001664 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1665 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001666 }
1667
Sarah Sharpf94e01862009-04-27 19:58:38 -07001668 return ret;
1669}
1670
1671void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1672{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001673 struct xhci_hcd *xhci;
1674 struct xhci_virt_device *virt_dev;
1675 int i, ret;
1676
Andiry Xu64927732010-10-14 07:22:45 -07001677 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001678 if (ret <= 0)
1679 return;
1680 xhci = hcd_to_xhci(hcd);
1681
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001682 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001683 virt_dev = xhci->devs[udev->slot_id];
1684 /* Free any rings allocated for added endpoints */
1685 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001686 if (virt_dev->eps[i].new_ring) {
1687 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1688 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001689 }
1690 }
John Yound115b042009-07-27 12:05:15 -07001691 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001692}
1693
Sarah Sharp5270b952009-09-04 10:53:11 -07001694static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001695 struct xhci_container_ctx *in_ctx,
1696 struct xhci_container_ctx *out_ctx,
1697 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07001698{
1699 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001700 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001701 ctrl_ctx->add_flags = add_flags;
1702 ctrl_ctx->drop_flags = drop_flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001703 xhci_slot_copy(xhci, in_ctx, out_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001704 ctrl_ctx->add_flags |= SLOT_FLAG;
1705
Sarah Sharp913a8a32009-09-04 10:53:13 -07001706 xhci_dbg(xhci, "Input Context:\n");
1707 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07001708}
1709
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001710void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1711 unsigned int slot_id, unsigned int ep_index,
1712 struct xhci_dequeue_state *deq_state)
1713{
1714 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001715 struct xhci_ep_ctx *ep_ctx;
1716 u32 added_ctxs;
1717 dma_addr_t addr;
1718
Sarah Sharp913a8a32009-09-04 10:53:13 -07001719 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1720 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001721 in_ctx = xhci->devs[slot_id]->in_ctx;
1722 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1723 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1724 deq_state->new_deq_ptr);
1725 if (addr == 0) {
1726 xhci_warn(xhci, "WARN Cannot submit config ep after "
1727 "reset ep command\n");
1728 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1729 deq_state->new_deq_seg,
1730 deq_state->new_deq_ptr);
1731 return;
1732 }
1733 ep_ctx->deq = addr | deq_state->new_cycle_state;
1734
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001735 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001736 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1737 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001738}
1739
Sarah Sharp82d10092009-08-07 14:04:52 -07001740void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001741 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07001742{
1743 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001744 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07001745
1746 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001747 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07001748 /* We need to move the HW's dequeue pointer past this TD,
1749 * or it will attempt to resend it on the next doorbell ring.
1750 */
1751 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001752 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001753 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07001754
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001755 /* HW with the reset endpoint quirk will use the saved dequeue state to
1756 * issue a configure endpoint command later.
1757 */
1758 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1759 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001760 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001761 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001762 } else {
1763 /* Better hope no one uses the input context between now and the
1764 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001765 * XXX: No idea how this hardware will react when stream rings
1766 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001767 */
1768 xhci_dbg(xhci, "Setting up input context for "
1769 "configure endpoint command\n");
1770 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1771 ep_index, &deq_state);
1772 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001773}
1774
Sarah Sharpa1587d92009-07-27 12:03:15 -07001775/* Deal with stalled endpoints. The core should have sent the control message
1776 * to clear the halt condition. However, we need to make the xHCI hardware
1777 * reset its sequence number, since a device will expect a sequence number of
1778 * zero after the halt condition is cleared.
1779 * Context: in_interrupt
1780 */
1781void xhci_endpoint_reset(struct usb_hcd *hcd,
1782 struct usb_host_endpoint *ep)
1783{
1784 struct xhci_hcd *xhci;
1785 struct usb_device *udev;
1786 unsigned int ep_index;
1787 unsigned long flags;
1788 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001789 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001790
1791 xhci = hcd_to_xhci(hcd);
1792 udev = (struct usb_device *) ep->hcpriv;
1793 /* Called with a root hub endpoint (or an endpoint that wasn't added
1794 * with xhci_add_endpoint()
1795 */
1796 if (!ep->hcpriv)
1797 return;
1798 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001799 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1800 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001801 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1802 ep->desc.bEndpointAddress);
1803 return;
1804 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001805 if (usb_endpoint_xfer_control(&ep->desc)) {
1806 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1807 return;
1808 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001809
1810 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1811 spin_lock_irqsave(&xhci->lock, flags);
1812 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001813 /*
1814 * Can't change the ring dequeue pointer until it's transitioned to the
1815 * stopped state, which is only upon a successful reset endpoint
1816 * command. Better hope that last command worked!
1817 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07001818 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001819 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1820 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001821 xhci_ring_cmd_db(xhci);
1822 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07001823 virt_ep->stopped_td = NULL;
1824 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001825 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001826 spin_unlock_irqrestore(&xhci->lock, flags);
1827
1828 if (ret)
1829 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1830}
1831
Sarah Sharp8df75f42010-04-02 15:34:16 -07001832static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1833 struct usb_device *udev, struct usb_host_endpoint *ep,
1834 unsigned int slot_id)
1835{
1836 int ret;
1837 unsigned int ep_index;
1838 unsigned int ep_state;
1839
1840 if (!ep)
1841 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07001842 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001843 if (ret <= 0)
1844 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04001845 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07001846 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1847 " descriptor for ep 0x%x does not support streams\n",
1848 ep->desc.bEndpointAddress);
1849 return -EINVAL;
1850 }
1851
1852 ep_index = xhci_get_endpoint_index(&ep->desc);
1853 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1854 if (ep_state & EP_HAS_STREAMS ||
1855 ep_state & EP_GETTING_STREAMS) {
1856 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1857 "already has streams set up.\n",
1858 ep->desc.bEndpointAddress);
1859 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1860 "dynamic stream context array reallocation.\n");
1861 return -EINVAL;
1862 }
1863 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1864 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1865 "endpoint 0x%x; URBs are pending.\n",
1866 ep->desc.bEndpointAddress);
1867 return -EINVAL;
1868 }
1869 return 0;
1870}
1871
1872static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1873 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1874{
1875 unsigned int max_streams;
1876
1877 /* The stream context array size must be a power of two */
1878 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1879 /*
1880 * Find out how many primary stream array entries the host controller
1881 * supports. Later we may use secondary stream arrays (similar to 2nd
1882 * level page entries), but that's an optional feature for xHCI host
1883 * controllers. xHCs must support at least 4 stream IDs.
1884 */
1885 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1886 if (*num_stream_ctxs > max_streams) {
1887 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1888 max_streams);
1889 *num_stream_ctxs = max_streams;
1890 *num_streams = max_streams;
1891 }
1892}
1893
1894/* Returns an error code if one of the endpoint already has streams.
1895 * This does not change any data structures, it only checks and gathers
1896 * information.
1897 */
1898static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1899 struct usb_device *udev,
1900 struct usb_host_endpoint **eps, unsigned int num_eps,
1901 unsigned int *num_streams, u32 *changed_ep_bitmask)
1902{
Sarah Sharp8df75f42010-04-02 15:34:16 -07001903 unsigned int max_streams;
1904 unsigned int endpoint_flag;
1905 int i;
1906 int ret;
1907
1908 for (i = 0; i < num_eps; i++) {
1909 ret = xhci_check_streams_endpoint(xhci, udev,
1910 eps[i], udev->slot_id);
1911 if (ret < 0)
1912 return ret;
1913
Alan Stern842f1692010-04-30 12:44:46 -04001914 max_streams = USB_SS_MAX_STREAMS(
1915 eps[i]->ss_ep_comp.bmAttributes);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001916 if (max_streams < (*num_streams - 1)) {
1917 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1918 eps[i]->desc.bEndpointAddress,
1919 max_streams);
1920 *num_streams = max_streams+1;
1921 }
1922
1923 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1924 if (*changed_ep_bitmask & endpoint_flag)
1925 return -EINVAL;
1926 *changed_ep_bitmask |= endpoint_flag;
1927 }
1928 return 0;
1929}
1930
1931static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1932 struct usb_device *udev,
1933 struct usb_host_endpoint **eps, unsigned int num_eps)
1934{
1935 u32 changed_ep_bitmask = 0;
1936 unsigned int slot_id;
1937 unsigned int ep_index;
1938 unsigned int ep_state;
1939 int i;
1940
1941 slot_id = udev->slot_id;
1942 if (!xhci->devs[slot_id])
1943 return 0;
1944
1945 for (i = 0; i < num_eps; i++) {
1946 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1947 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1948 /* Are streams already being freed for the endpoint? */
1949 if (ep_state & EP_GETTING_NO_STREAMS) {
1950 xhci_warn(xhci, "WARN Can't disable streams for "
1951 "endpoint 0x%x\n, "
1952 "streams are being disabled already.",
1953 eps[i]->desc.bEndpointAddress);
1954 return 0;
1955 }
1956 /* Are there actually any streams to free? */
1957 if (!(ep_state & EP_HAS_STREAMS) &&
1958 !(ep_state & EP_GETTING_STREAMS)) {
1959 xhci_warn(xhci, "WARN Can't disable streams for "
1960 "endpoint 0x%x\n, "
1961 "streams are already disabled!",
1962 eps[i]->desc.bEndpointAddress);
1963 xhci_warn(xhci, "WARN xhci_free_streams() called "
1964 "with non-streams endpoint\n");
1965 return 0;
1966 }
1967 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1968 }
1969 return changed_ep_bitmask;
1970}
1971
1972/*
1973 * The USB device drivers use this function (though the HCD interface in USB
1974 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1975 * coordinate mass storage command queueing across multiple endpoints (basically
1976 * a stream ID == a task ID).
1977 *
1978 * Setting up streams involves allocating the same size stream context array
1979 * for each endpoint and issuing a configure endpoint command for all endpoints.
1980 *
1981 * Don't allow the call to succeed if one endpoint only supports one stream
1982 * (which means it doesn't support streams at all).
1983 *
1984 * Drivers may get less stream IDs than they asked for, if the host controller
1985 * hardware or endpoints claim they can't support the number of requested
1986 * stream IDs.
1987 */
1988int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1989 struct usb_host_endpoint **eps, unsigned int num_eps,
1990 unsigned int num_streams, gfp_t mem_flags)
1991{
1992 int i, ret;
1993 struct xhci_hcd *xhci;
1994 struct xhci_virt_device *vdev;
1995 struct xhci_command *config_cmd;
1996 unsigned int ep_index;
1997 unsigned int num_stream_ctxs;
1998 unsigned long flags;
1999 u32 changed_ep_bitmask = 0;
2000
2001 if (!eps)
2002 return -EINVAL;
2003
2004 /* Add one to the number of streams requested to account for
2005 * stream 0 that is reserved for xHCI usage.
2006 */
2007 num_streams += 1;
2008 xhci = hcd_to_xhci(hcd);
2009 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
2010 num_streams);
2011
2012 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
2013 if (!config_cmd) {
2014 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2015 return -ENOMEM;
2016 }
2017
2018 /* Check to make sure all endpoints are not already configured for
2019 * streams. While we're at it, find the maximum number of streams that
2020 * all the endpoints will support and check for duplicate endpoints.
2021 */
2022 spin_lock_irqsave(&xhci->lock, flags);
2023 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
2024 num_eps, &num_streams, &changed_ep_bitmask);
2025 if (ret < 0) {
2026 xhci_free_command(xhci, config_cmd);
2027 spin_unlock_irqrestore(&xhci->lock, flags);
2028 return ret;
2029 }
2030 if (num_streams <= 1) {
2031 xhci_warn(xhci, "WARN: endpoints can't handle "
2032 "more than one stream.\n");
2033 xhci_free_command(xhci, config_cmd);
2034 spin_unlock_irqrestore(&xhci->lock, flags);
2035 return -EINVAL;
2036 }
2037 vdev = xhci->devs[udev->slot_id];
2038 /* Mark each endpoint as being in transistion, so
2039 * xhci_urb_enqueue() will reject all URBs.
2040 */
2041 for (i = 0; i < num_eps; i++) {
2042 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2043 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
2044 }
2045 spin_unlock_irqrestore(&xhci->lock, flags);
2046
2047 /* Setup internal data structures and allocate HW data structures for
2048 * streams (but don't install the HW structures in the input context
2049 * until we're sure all memory allocation succeeded).
2050 */
2051 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
2052 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
2053 num_stream_ctxs, num_streams);
2054
2055 for (i = 0; i < num_eps; i++) {
2056 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2057 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
2058 num_stream_ctxs,
2059 num_streams, mem_flags);
2060 if (!vdev->eps[ep_index].stream_info)
2061 goto cleanup;
2062 /* Set maxPstreams in endpoint context and update deq ptr to
2063 * point to stream context array. FIXME
2064 */
2065 }
2066
2067 /* Set up the input context for a configure endpoint command. */
2068 for (i = 0; i < num_eps; i++) {
2069 struct xhci_ep_ctx *ep_ctx;
2070
2071 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2072 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
2073
2074 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
2075 vdev->out_ctx, ep_index);
2076 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
2077 vdev->eps[ep_index].stream_info);
2078 }
2079 /* Tell the HW to drop its old copy of the endpoint context info
2080 * and add the updated copy from the input context.
2081 */
2082 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
2083 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
2084
2085 /* Issue and wait for the configure endpoint command */
2086 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
2087 false, false);
2088
2089 /* xHC rejected the configure endpoint command for some reason, so we
2090 * leave the old ring intact and free our internal streams data
2091 * structure.
2092 */
2093 if (ret < 0)
2094 goto cleanup;
2095
2096 spin_lock_irqsave(&xhci->lock, flags);
2097 for (i = 0; i < num_eps; i++) {
2098 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2099 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
2100 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
2101 udev->slot_id, ep_index);
2102 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
2103 }
2104 xhci_free_command(xhci, config_cmd);
2105 spin_unlock_irqrestore(&xhci->lock, flags);
2106
2107 /* Subtract 1 for stream 0, which drivers can't use */
2108 return num_streams - 1;
2109
2110cleanup:
2111 /* If it didn't work, free the streams! */
2112 for (i = 0; i < num_eps; i++) {
2113 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2114 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07002115 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07002116 /* FIXME Unset maxPstreams in endpoint context and
2117 * update deq ptr to point to normal string ring.
2118 */
2119 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
2120 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
2121 xhci_endpoint_zero(xhci, vdev, eps[i]);
2122 }
2123 xhci_free_command(xhci, config_cmd);
2124 return -ENOMEM;
2125}
2126
2127/* Transition the endpoint from using streams to being a "normal" endpoint
2128 * without streams.
2129 *
2130 * Modify the endpoint context state, submit a configure endpoint command,
2131 * and free all endpoint rings for streams if that completes successfully.
2132 */
2133int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2134 struct usb_host_endpoint **eps, unsigned int num_eps,
2135 gfp_t mem_flags)
2136{
2137 int i, ret;
2138 struct xhci_hcd *xhci;
2139 struct xhci_virt_device *vdev;
2140 struct xhci_command *command;
2141 unsigned int ep_index;
2142 unsigned long flags;
2143 u32 changed_ep_bitmask;
2144
2145 xhci = hcd_to_xhci(hcd);
2146 vdev = xhci->devs[udev->slot_id];
2147
2148 /* Set up a configure endpoint command to remove the streams rings */
2149 spin_lock_irqsave(&xhci->lock, flags);
2150 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
2151 udev, eps, num_eps);
2152 if (changed_ep_bitmask == 0) {
2153 spin_unlock_irqrestore(&xhci->lock, flags);
2154 return -EINVAL;
2155 }
2156
2157 /* Use the xhci_command structure from the first endpoint. We may have
2158 * allocated too many, but the driver may call xhci_free_streams() for
2159 * each endpoint it grouped into one call to xhci_alloc_streams().
2160 */
2161 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
2162 command = vdev->eps[ep_index].stream_info->free_streams_command;
2163 for (i = 0; i < num_eps; i++) {
2164 struct xhci_ep_ctx *ep_ctx;
2165
2166 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2167 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
2168 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
2169 EP_GETTING_NO_STREAMS;
2170
2171 xhci_endpoint_copy(xhci, command->in_ctx,
2172 vdev->out_ctx, ep_index);
2173 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
2174 &vdev->eps[ep_index]);
2175 }
2176 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
2177 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
2178 spin_unlock_irqrestore(&xhci->lock, flags);
2179
2180 /* Issue and wait for the configure endpoint command,
2181 * which must succeed.
2182 */
2183 ret = xhci_configure_endpoint(xhci, udev, command,
2184 false, true);
2185
2186 /* xHC rejected the configure endpoint command for some reason, so we
2187 * leave the streams rings intact.
2188 */
2189 if (ret < 0)
2190 return ret;
2191
2192 spin_lock_irqsave(&xhci->lock, flags);
2193 for (i = 0; i < num_eps; i++) {
2194 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2195 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07002196 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07002197 /* FIXME Unset maxPstreams in endpoint context and
2198 * update deq ptr to point to normal string ring.
2199 */
2200 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
2201 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
2202 }
2203 spin_unlock_irqrestore(&xhci->lock, flags);
2204
2205 return 0;
2206}
2207
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002208/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002209 * This submits a Reset Device Command, which will set the device state to 0,
2210 * set the device address to 0, and disable all the endpoints except the default
2211 * control endpoint. The USB core should come back and call
2212 * xhci_address_device(), and then re-set up the configuration. If this is
2213 * called because of a usb_reset_and_verify_device(), then the old alternate
2214 * settings will be re-installed through the normal bandwidth allocation
2215 * functions.
2216 *
2217 * Wait for the Reset Device command to finish. Remove all structures
2218 * associated with the endpoints that were disabled. Clear the input device
2219 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07002220 *
2221 * If the virt_dev to be reset does not exist or does not match the udev,
2222 * it means the device is lost, possibly due to the xHC restore error and
2223 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
2224 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002225 */
Andiry Xuf0615c42010-10-14 07:22:48 -07002226int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002227{
2228 int ret, i;
2229 unsigned long flags;
2230 struct xhci_hcd *xhci;
2231 unsigned int slot_id;
2232 struct xhci_virt_device *virt_dev;
2233 struct xhci_command *reset_device_cmd;
2234 int timeleft;
2235 int last_freed_endpoint;
2236
Andiry Xuf0615c42010-10-14 07:22:48 -07002237 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002238 if (ret <= 0)
2239 return ret;
2240 xhci = hcd_to_xhci(hcd);
2241 slot_id = udev->slot_id;
2242 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07002243 if (!virt_dev) {
2244 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
2245 "not exist. Re-allocate the device\n", slot_id);
2246 ret = xhci_alloc_dev(hcd, udev);
2247 if (ret == 1)
2248 return 0;
2249 else
2250 return -EINVAL;
2251 }
2252
2253 if (virt_dev->udev != udev) {
2254 /* If the virt_dev and the udev does not match, this virt_dev
2255 * may belong to another udev.
2256 * Re-allocate the device.
2257 */
2258 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
2259 "not match the udev. Re-allocate the device\n",
2260 slot_id);
2261 ret = xhci_alloc_dev(hcd, udev);
2262 if (ret == 1)
2263 return 0;
2264 else
2265 return -EINVAL;
2266 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002267
2268 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
2269 /* Allocate the command structure that holds the struct completion.
2270 * Assume we're in process context, since the normal device reset
2271 * process has to wait for the device anyway. Storage devices are
2272 * reset as part of error handling, so use GFP_NOIO instead of
2273 * GFP_KERNEL.
2274 */
2275 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
2276 if (!reset_device_cmd) {
2277 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
2278 return -ENOMEM;
2279 }
2280
2281 /* Attempt to submit the Reset Device command to the command ring */
2282 spin_lock_irqsave(&xhci->lock, flags);
2283 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002284
2285 /* Enqueue pointer can be left pointing to the link TRB,
2286 * we must handle that
2287 */
2288 if ((reset_device_cmd->command_trb->link.control & TRB_TYPE_BITMASK)
2289 == TRB_TYPE(TRB_LINK))
2290 reset_device_cmd->command_trb =
2291 xhci->cmd_ring->enq_seg->next->trbs;
2292
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002293 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
2294 ret = xhci_queue_reset_device(xhci, slot_id);
2295 if (ret) {
2296 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2297 list_del(&reset_device_cmd->cmd_list);
2298 spin_unlock_irqrestore(&xhci->lock, flags);
2299 goto command_cleanup;
2300 }
2301 xhci_ring_cmd_db(xhci);
2302 spin_unlock_irqrestore(&xhci->lock, flags);
2303
2304 /* Wait for the Reset Device command to finish */
2305 timeleft = wait_for_completion_interruptible_timeout(
2306 reset_device_cmd->completion,
2307 USB_CTRL_SET_TIMEOUT);
2308 if (timeleft <= 0) {
2309 xhci_warn(xhci, "%s while waiting for reset device command\n",
2310 timeleft == 0 ? "Timeout" : "Signal");
2311 spin_lock_irqsave(&xhci->lock, flags);
2312 /* The timeout might have raced with the event ring handler, so
2313 * only delete from the list if the item isn't poisoned.
2314 */
2315 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
2316 list_del(&reset_device_cmd->cmd_list);
2317 spin_unlock_irqrestore(&xhci->lock, flags);
2318 ret = -ETIME;
2319 goto command_cleanup;
2320 }
2321
2322 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
2323 * unless we tried to reset a slot ID that wasn't enabled,
2324 * or the device wasn't in the addressed or configured state.
2325 */
2326 ret = reset_device_cmd->status;
2327 switch (ret) {
2328 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
2329 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
2330 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
2331 slot_id,
2332 xhci_get_slot_state(xhci, virt_dev->out_ctx));
2333 xhci_info(xhci, "Not freeing device rings.\n");
2334 /* Don't treat this as an error. May change my mind later. */
2335 ret = 0;
2336 goto command_cleanup;
2337 case COMP_SUCCESS:
2338 xhci_dbg(xhci, "Successful reset device command.\n");
2339 break;
2340 default:
2341 if (xhci_is_vendor_info_code(xhci, ret))
2342 break;
2343 xhci_warn(xhci, "Unknown completion code %u for "
2344 "reset device command.\n", ret);
2345 ret = -EINVAL;
2346 goto command_cleanup;
2347 }
2348
2349 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
2350 last_freed_endpoint = 1;
2351 for (i = 1; i < 31; ++i) {
2352 if (!virt_dev->eps[i].ring)
2353 continue;
2354 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2355 last_freed_endpoint = i;
2356 }
2357 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
2358 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2359 ret = 0;
2360
2361command_cleanup:
2362 xhci_free_command(xhci, reset_device_cmd);
2363 return ret;
2364}
2365
2366/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002367 * At this point, the struct usb_device is about to go away, the device has
2368 * disconnected, and all traffic has been stopped and the endpoints have been
2369 * disabled. Free any HC data structures associated with that device.
2370 */
2371void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2372{
2373 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002374 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002375 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002376 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07002377 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002378
Andiry Xu64927732010-10-14 07:22:45 -07002379 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2380 if (ret <= 0)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002381 return;
Andiry Xu64927732010-10-14 07:22:45 -07002382
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002383 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002384
2385 /* Stop any wayward timer functions (which may grab the lock) */
2386 for (i = 0; i < 31; ++i) {
2387 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2388 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2389 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002390
2391 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002392 /* Don't disable the slot if the host controller is dead. */
2393 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002394 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002395 xhci_free_virt_device(xhci, udev->slot_id);
2396 spin_unlock_irqrestore(&xhci->lock, flags);
2397 return;
2398 }
2399
Sarah Sharp23e3be12009-04-29 19:05:20 -07002400 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002401 spin_unlock_irqrestore(&xhci->lock, flags);
2402 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2403 return;
2404 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002405 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002406 spin_unlock_irqrestore(&xhci->lock, flags);
2407 /*
2408 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07002409 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002410 */
2411}
2412
2413/*
2414 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2415 * timed out, or allocating memory failed. Returns 1 on success.
2416 */
2417int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2418{
2419 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2420 unsigned long flags;
2421 int timeleft;
2422 int ret;
2423
2424 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002425 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002426 if (ret) {
2427 spin_unlock_irqrestore(&xhci->lock, flags);
2428 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2429 return 0;
2430 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002431 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002432 spin_unlock_irqrestore(&xhci->lock, flags);
2433
2434 /* XXX: how much time for xHC slot assignment? */
2435 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2436 USB_CTRL_SET_TIMEOUT);
2437 if (timeleft <= 0) {
2438 xhci_warn(xhci, "%s while waiting for a slot\n",
2439 timeleft == 0 ? "Timeout" : "Signal");
2440 /* FIXME cancel the enable slot request */
2441 return 0;
2442 }
2443
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002444 if (!xhci->slot_id) {
2445 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002446 return 0;
2447 }
Sarah Sharpf88ba782009-05-14 11:44:22 -07002448 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002449 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
2450 /* Disable slot, if we can do it without mem alloc */
2451 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07002452 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002453 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2454 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002455 spin_unlock_irqrestore(&xhci->lock, flags);
2456 return 0;
2457 }
2458 udev->slot_id = xhci->slot_id;
2459 /* Is this a LS or FS device under a HS hub? */
2460 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002461 return 1;
2462}
2463
2464/*
2465 * Issue an Address Device command (which will issue a SetAddress request to
2466 * the device).
2467 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2468 * we should only issue and wait on one address command at the same time.
2469 *
2470 * We add one to the device address issued by the hardware because the USB core
2471 * uses address 1 for the root hubs (even though they're not really devices).
2472 */
2473int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2474{
2475 unsigned long flags;
2476 int timeleft;
2477 struct xhci_virt_device *virt_dev;
2478 int ret = 0;
2479 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07002480 struct xhci_slot_ctx *slot_ctx;
2481 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07002482 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002483
2484 if (!udev->slot_id) {
2485 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2486 return -EINVAL;
2487 }
2488
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002489 virt_dev = xhci->devs[udev->slot_id];
2490
Andiry Xuf0615c42010-10-14 07:22:48 -07002491 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2492 /*
2493 * If this is the first Set Address since device plug-in or
2494 * virt_device realloaction after a resume with an xHCI power loss,
2495 * then set up the slot context.
2496 */
2497 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002498 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07002499 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02002500 else
2501 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002502 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002503 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002504
Sarah Sharpf88ba782009-05-14 11:44:22 -07002505 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07002506 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2507 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002508 if (ret) {
2509 spin_unlock_irqrestore(&xhci->lock, flags);
2510 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2511 return ret;
2512 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002513 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002514 spin_unlock_irqrestore(&xhci->lock, flags);
2515
2516 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2517 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2518 USB_CTRL_SET_TIMEOUT);
2519 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2520 * the SetAddress() "recovery interval" required by USB and aborting the
2521 * command on a timeout.
2522 */
2523 if (timeleft <= 0) {
2524 xhci_warn(xhci, "%s while waiting for a slot\n",
2525 timeleft == 0 ? "Timeout" : "Signal");
2526 /* FIXME cancel the address device command */
2527 return -ETIME;
2528 }
2529
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002530 switch (virt_dev->cmd_status) {
2531 case COMP_CTX_STATE:
2532 case COMP_EBADSLT:
2533 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2534 udev->slot_id);
2535 ret = -EINVAL;
2536 break;
2537 case COMP_TX_ERR:
2538 dev_warn(&udev->dev, "Device not responding to set address.\n");
2539 ret = -EPROTO;
2540 break;
2541 case COMP_SUCCESS:
2542 xhci_dbg(xhci, "Successful Address Device command\n");
2543 break;
2544 default:
2545 xhci_err(xhci, "ERROR: unexpected command completion "
2546 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002547 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002548 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002549 ret = -EINVAL;
2550 break;
2551 }
2552 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002553 return ret;
2554 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07002555 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2556 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2557 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002558 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002559 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2560 (unsigned long long)
2561 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002562 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07002563 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002564 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002565 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002566 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002567 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002568 /*
2569 * USB core uses address 1 for the roothubs, so we add one to the
2570 * address given back to us by the HC.
2571 */
John Yound115b042009-07-27 12:05:15 -07002572 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Andiry Xuc8d4af82010-10-14 07:22:51 -07002573 /* Use kernel assigned address for devices; store xHC assigned
2574 * address locally. */
2575 virt_dev->address = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002576 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07002577 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2578 ctrl_ctx->add_flags = 0;
2579 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002580
Andiry Xuc8d4af82010-10-14 07:22:51 -07002581 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002582
2583 return 0;
2584}
2585
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002586/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2587 * internal data structures for the device.
2588 */
2589int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2590 struct usb_tt *tt, gfp_t mem_flags)
2591{
2592 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2593 struct xhci_virt_device *vdev;
2594 struct xhci_command *config_cmd;
2595 struct xhci_input_control_ctx *ctrl_ctx;
2596 struct xhci_slot_ctx *slot_ctx;
2597 unsigned long flags;
2598 unsigned think_time;
2599 int ret;
2600
2601 /* Ignore root hubs */
2602 if (!hdev->parent)
2603 return 0;
2604
2605 vdev = xhci->devs[hdev->slot_id];
2606 if (!vdev) {
2607 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2608 return -EINVAL;
2609 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08002610 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002611 if (!config_cmd) {
2612 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2613 return -ENOMEM;
2614 }
2615
2616 spin_lock_irqsave(&xhci->lock, flags);
2617 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2618 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2619 ctrl_ctx->add_flags |= SLOT_FLAG;
2620 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2621 slot_ctx->dev_info |= DEV_HUB;
2622 if (tt->multi)
2623 slot_ctx->dev_info |= DEV_MTT;
2624 if (xhci->hci_version > 0x95) {
2625 xhci_dbg(xhci, "xHCI version %x needs hub "
2626 "TT think time and number of ports\n",
2627 (unsigned int) xhci->hci_version);
2628 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2629 /* Set TT think time - convert from ns to FS bit times.
2630 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2631 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2632 */
2633 think_time = tt->think_time;
2634 if (think_time != 0)
2635 think_time = (think_time / 666) - 1;
2636 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2637 } else {
2638 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2639 "TT think time or number of ports\n",
2640 (unsigned int) xhci->hci_version);
2641 }
2642 slot_ctx->dev_state = 0;
2643 spin_unlock_irqrestore(&xhci->lock, flags);
2644
2645 xhci_dbg(xhci, "Set up %s for hub device.\n",
2646 (xhci->hci_version > 0x95) ?
2647 "configure endpoint" : "evaluate context");
2648 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2649 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2650
2651 /* Issue and wait for the configure endpoint or
2652 * evaluate context command.
2653 */
2654 if (xhci->hci_version > 0x95)
2655 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2656 false, false);
2657 else
2658 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2659 true, false);
2660
2661 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2662 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2663
2664 xhci_free_command(xhci, config_cmd);
2665 return ret;
2666}
2667
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002668int xhci_get_frame(struct usb_hcd *hcd)
2669{
2670 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2671 /* EHCI mods by the periodic size. Why? */
2672 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2673}
2674
2675MODULE_DESCRIPTION(DRIVER_DESC);
2676MODULE_AUTHOR(DRIVER_AUTHOR);
2677MODULE_LICENSE("GPL");
2678
2679static int __init xhci_hcd_init(void)
2680{
2681#ifdef CONFIG_PCI
2682 int retval = 0;
2683
2684 retval = xhci_register_pci();
2685
2686 if (retval < 0) {
2687 printk(KERN_DEBUG "Problem registering PCI driver.");
2688 return retval;
2689 }
2690#endif
Sarah Sharp98441972009-05-14 11:44:18 -07002691 /*
2692 * Check the compiler generated sizes of structures that must be laid
2693 * out in specific ways for hardware access.
2694 */
2695 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2696 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2697 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2698 /* xhci_device_control has eight fields, and also
2699 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2700 */
Sarah Sharp98441972009-05-14 11:44:18 -07002701 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2702 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2703 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2704 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2705 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2706 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2707 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2708 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002709 return 0;
2710}
2711module_init(xhci_hcd_init);
2712
2713static void __exit xhci_hcd_cleanup(void)
2714{
2715#ifdef CONFIG_PCI
2716 xhci_unregister_pci();
2717#endif
2718}
2719module_exit(xhci_hcd_cleanup);