blob: 8d45bbde3da4db17786f5bc6fe0763de6aa6f61f [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
Andiry Xubdfca502011-01-06 15:43:39 +080096 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070097 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070098 */
99int xhci_halt(struct xhci_hcd *xhci)
100{
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700102 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700103
104 return handshake(xhci, &xhci->op_regs->status,
105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
106}
107
108/*
Sarah Sharped074532010-05-24 13:25:21 -0700109 * Set the run bit and wait for the host to be running.
110 */
111int xhci_start(struct xhci_hcd *xhci)
112{
113 u32 temp;
114 int ret;
115
116 temp = xhci_readl(xhci, &xhci->op_regs->command);
117 temp |= (CMD_RUN);
118 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
119 temp);
120 xhci_writel(xhci, temp, &xhci->op_regs->command);
121
122 /*
123 * Wait for the HCHalted Status bit to be 0 to indicate the host is
124 * running.
125 */
126 ret = handshake(xhci, &xhci->op_regs->status,
127 STS_HALT, 0, XHCI_MAX_HALT_USEC);
128 if (ret == -ETIMEDOUT)
129 xhci_err(xhci, "Host took too long to start, "
130 "waited %u microseconds.\n",
131 XHCI_MAX_HALT_USEC);
132 return ret;
133}
134
135/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800136 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700137 *
138 * This resets pipelines, timers, counters, state machines, etc.
139 * Transactions will be terminated immediately, and operational registers
140 * will be set to their defaults.
141 */
142int xhci_reset(struct xhci_hcd *xhci)
143{
144 u32 command;
145 u32 state;
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700146 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700147
148 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700149 if ((state & STS_HALT) == 0) {
150 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
151 return 0;
152 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700153
154 xhci_dbg(xhci, "// Reset the HC\n");
155 command = xhci_readl(xhci, &xhci->op_regs->command);
156 command |= CMD_RESET;
157 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700158
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700159 ret = handshake(xhci, &xhci->op_regs->command,
160 CMD_RESET, 0, 250 * 1000);
161 if (ret)
162 return ret;
163
164 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
165 /*
166 * xHCI cannot write to any doorbells or operational registers other
167 * than status until the "Controller Not Ready" flag is cleared.
168 */
169 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700170}
171
Dong Nguyen43b86af2010-07-21 16:56:08 -0700172/*
173 * Free IRQs
174 * free all IRQs request
175 */
176static void xhci_free_irq(struct xhci_hcd *xhci)
177{
178 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700179 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
180
Dong Nguyen43b86af2010-07-21 16:56:08 -0700181 /* return if using legacy interrupt */
182 if (xhci_to_hcd(xhci)->irq >= 0)
183 return;
184
185 if (xhci->msix_entries) {
186 for (i = 0; i < xhci->msix_count; i++)
187 if (xhci->msix_entries[i].vector)
188 free_irq(xhci->msix_entries[i].vector,
189 xhci_to_hcd(xhci));
190 } else if (pdev->irq >= 0)
191 free_irq(pdev->irq, xhci_to_hcd(xhci));
192
193 return;
194}
195
196/*
197 * Set up MSI
198 */
199static int xhci_setup_msi(struct xhci_hcd *xhci)
200{
201 int ret;
202 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
203
204 ret = pci_enable_msi(pdev);
205 if (ret) {
206 xhci_err(xhci, "failed to allocate MSI entry\n");
207 return ret;
208 }
209
210 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
211 0, "xhci_hcd", xhci_to_hcd(xhci));
212 if (ret) {
213 xhci_err(xhci, "disable MSI interrupt\n");
214 pci_disable_msi(pdev);
215 }
216
217 return ret;
218}
219
220/*
221 * Set up MSI-X
222 */
223static int xhci_setup_msix(struct xhci_hcd *xhci)
224{
225 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800226 struct usb_hcd *hcd = xhci_to_hcd(xhci);
227 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700228
229 /*
230 * calculate number of msi-x vectors supported.
231 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
232 * with max number of interrupters based on the xhci HCSPARAMS1.
233 * - num_online_cpus: maximum msi-x vectors per CPUs core.
234 * Add additional 1 vector to ensure always available interrupt.
235 */
236 xhci->msix_count = min(num_online_cpus() + 1,
237 HCS_MAX_INTRS(xhci->hcs_params1));
238
239 xhci->msix_entries =
240 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800241 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700242 if (!xhci->msix_entries) {
243 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
244 return -ENOMEM;
245 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700246
247 for (i = 0; i < xhci->msix_count; i++) {
248 xhci->msix_entries[i].entry = i;
249 xhci->msix_entries[i].vector = 0;
250 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700251
252 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
253 if (ret) {
254 xhci_err(xhci, "Failed to enable MSI-X\n");
255 goto free_entries;
256 }
257
Dong Nguyen43b86af2010-07-21 16:56:08 -0700258 for (i = 0; i < xhci->msix_count; i++) {
259 ret = request_irq(xhci->msix_entries[i].vector,
260 (irq_handler_t)xhci_msi_irq,
261 0, "xhci_hcd", xhci_to_hcd(xhci));
262 if (ret)
263 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700264 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700265
Andiry Xu00292272010-12-27 17:39:02 +0800266 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700267 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700268
269disable_msix:
Dong Nguyen43b86af2010-07-21 16:56:08 -0700270 xhci_err(xhci, "disable MSI-X interrupt\n");
271 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700272 pci_disable_msix(pdev);
273free_entries:
274 kfree(xhci->msix_entries);
275 xhci->msix_entries = NULL;
276 return ret;
277}
278
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700279/* Free any IRQs and disable MSI-X */
280static void xhci_cleanup_msix(struct xhci_hcd *xhci)
281{
Andiry Xu00292272010-12-27 17:39:02 +0800282 struct usb_hcd *hcd = xhci_to_hcd(xhci);
283 struct pci_dev *pdev = to_pci_dev(hcd->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
Andiry Xu00292272010-12-27 17:39:02 +0800295 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700296 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700297}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700298
299/*
300 * Initialize memory for HCD and xHC (one-time init).
301 *
302 * Program the PAGESIZE register, initialize the device context array, create
303 * device contexts (?), set up a command ring segment (or two?), create event
304 * ring (one for now).
305 */
306int xhci_init(struct usb_hcd *hcd)
307{
308 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
309 int retval = 0;
310
311 xhci_dbg(xhci, "xhci_init\n");
312 spin_lock_init(&xhci->lock);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700313 if (link_quirk) {
314 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
315 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
316 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700317 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700318 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700319 retval = xhci_mem_init(xhci, GFP_KERNEL);
320 xhci_dbg(xhci, "Finished xhci_init\n");
321
322 return retval;
323}
324
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700325/*-------------------------------------------------------------------------*/
326
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700327
328#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700329void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700330{
331 unsigned long flags;
332 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700333 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700334 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
335 int i, j;
336
337 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
338
339 spin_lock_irqsave(&xhci->lock, flags);
340 temp = xhci_readl(xhci, &xhci->op_regs->status);
341 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700342 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700343 xhci_dbg(xhci, "HW died, polling stopped.\n");
344 spin_unlock_irqrestore(&xhci->lock, flags);
345 return;
346 }
347
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700348 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
349 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700350 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 spin_unlock_irqrestore(&xhci->lock, flags);
370
371 if (!xhci->zombie)
372 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
373 else
374 xhci_dbg(xhci, "Quit polling the event ring.\n");
375}
376#endif
377
378/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700379 * Start the HC after it was halted.
380 *
381 * This function is called by the USB core when the HC driver is added.
382 * Its opposite is xhci_stop().
383 *
384 * xhci_init() must be called once before this function can be called.
385 * Reset the HC, enable device slot contexts, program DCBAAP, and
386 * set command ring pointer and event ring pointer.
387 *
388 * Setup MSI-X vectors and enable interrupts.
389 */
390int xhci_run(struct usb_hcd *hcd)
391{
392 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700393 u64 temp_64;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700394 u32 ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700395 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700396 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700397
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700398 hcd->uses_new_polling = 1;
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700399
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700400 xhci_dbg(xhci, "xhci_run\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700401 /* unregister the legacy interrupt */
402 if (hcd->irq)
403 free_irq(hcd->irq, hcd);
404 hcd->irq = -1;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700405
Dong Nguyen43b86af2010-07-21 16:56:08 -0700406 ret = xhci_setup_msix(xhci);
407 if (ret)
408 /* fall back to msi*/
409 ret = xhci_setup_msi(xhci);
410
411 if (ret) {
412 /* fall back to legacy interrupt*/
413 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
414 hcd->irq_descr, hcd);
415 if (ret) {
416 xhci_err(xhci, "request interrupt %d failed\n",
417 pdev->irq);
418 return ret;
419 }
420 hcd->irq = pdev->irq;
421 }
422
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700423#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
424 init_timer(&xhci->event_ring_timer);
425 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700426 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700427 /* Poll the event ring */
428 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
429 xhci->zombie = 0;
430 xhci_dbg(xhci, "Setting event ring polling timer\n");
431 add_timer(&xhci->event_ring_timer);
432#endif
433
Sarah Sharp66e49d82009-07-27 12:03:46 -0700434 xhci_dbg(xhci, "Command ring memory map follows:\n");
435 xhci_debug_ring(xhci, xhci->cmd_ring);
436 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
437 xhci_dbg_cmd_ptrs(xhci);
438
439 xhci_dbg(xhci, "ERST memory map follows:\n");
440 xhci_dbg_erst(xhci, &xhci->erst);
441 xhci_dbg(xhci, "Event ring:\n");
442 xhci_debug_ring(xhci, xhci->event_ring);
443 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
444 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
445 temp_64 &= ~ERST_PTR_MASK;
446 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
447
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700448 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
449 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700450 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700451 temp |= (u32) 160;
452 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
453
454 /* Set the HCD state before we enable the irqs */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700455 temp = xhci_readl(xhci, &xhci->op_regs->command);
456 temp |= (CMD_EIE);
457 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
458 temp);
459 xhci_writel(xhci, temp, &xhci->op_regs->command);
460
461 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700462 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
463 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700464 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
465 &xhci->ir_set->irq_pending);
466 xhci_print_ir_set(xhci, xhci->ir_set, 0);
467
Sarah Sharp02386342010-05-24 13:25:28 -0700468 if (xhci->quirks & XHCI_NEC_HOST)
469 xhci_queue_vendor_command(xhci, 0, 0, 0,
470 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700471
Sarah Sharped074532010-05-24 13:25:21 -0700472 if (xhci_start(xhci)) {
473 xhci_halt(xhci);
474 return -ENODEV;
475 }
476
Sarah Sharp02386342010-05-24 13:25:28 -0700477 if (xhci->quirks & XHCI_NEC_HOST)
478 xhci_ring_cmd_db(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700479
480 xhci_dbg(xhci, "Finished xhci_run\n");
481 return 0;
482}
483
484/*
485 * Stop xHCI driver.
486 *
487 * This function is called by the USB core when the HC driver is removed.
488 * Its opposite is xhci_run().
489 *
490 * Disable device contexts, disable IRQs, and quiesce the HC.
491 * Reset the HC, finish any completed transactions, and cleanup memory.
492 */
493void xhci_stop(struct usb_hcd *hcd)
494{
495 u32 temp;
496 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
497
498 spin_lock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700499 xhci_halt(xhci);
500 xhci_reset(xhci);
501 spin_unlock_irq(&xhci->lock);
502
Zhang Rui40a9fb12010-12-17 13:17:04 -0800503 xhci_cleanup_msix(xhci);
504
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700505#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
506 /* Tell the event ring poll function not to reschedule */
507 xhci->zombie = 1;
508 del_timer_sync(&xhci->event_ring_timer);
509#endif
510
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700511 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
512 temp = xhci_readl(xhci, &xhci->op_regs->status);
513 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
514 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
515 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
516 &xhci->ir_set->irq_pending);
517 xhci_print_ir_set(xhci, xhci->ir_set, 0);
518
519 xhci_dbg(xhci, "cleaning up memory\n");
520 xhci_mem_cleanup(xhci);
521 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
522 xhci_readl(xhci, &xhci->op_regs->status));
523}
524
525/*
526 * Shutdown HC (not bus-specific)
527 *
528 * This is called when the machine is rebooting or halting. We assume that the
529 * machine will be powered off, and the HC's internal state will be reset.
530 * Don't bother to free memory.
531 */
532void xhci_shutdown(struct usb_hcd *hcd)
533{
534 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
535
536 spin_lock_irq(&xhci->lock);
537 xhci_halt(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700538 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700539
Zhang Rui40a9fb12010-12-17 13:17:04 -0800540 xhci_cleanup_msix(xhci);
541
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700542 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
543 xhci_readl(xhci, &xhci->op_regs->status));
544}
545
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700546#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700547static void xhci_save_registers(struct xhci_hcd *xhci)
548{
549 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
550 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
551 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
552 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
553 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
554 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
555 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
556 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
557 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
558}
559
560static void xhci_restore_registers(struct xhci_hcd *xhci)
561{
562 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
563 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
564 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
565 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
566 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
567 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
568 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
569 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
570}
571
Sarah Sharp89821322010-11-12 11:59:31 -0800572static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
573{
574 u64 val_64;
575
576 /* step 2: initialize command ring buffer */
577 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
578 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
579 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
580 xhci->cmd_ring->dequeue) &
581 (u64) ~CMD_RING_RSVD_BITS) |
582 xhci->cmd_ring->cycle_state;
583 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
584 (long unsigned long) val_64);
585 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
586}
587
588/*
589 * The whole command ring must be cleared to zero when we suspend the host.
590 *
591 * The host doesn't save the command ring pointer in the suspend well, so we
592 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
593 * aligned, because of the reserved bits in the command ring dequeue pointer
594 * register. Therefore, we can't just set the dequeue pointer back in the
595 * middle of the ring (TRBs are 16-byte aligned).
596 */
597static void xhci_clear_command_ring(struct xhci_hcd *xhci)
598{
599 struct xhci_ring *ring;
600 struct xhci_segment *seg;
601
602 ring = xhci->cmd_ring;
603 seg = ring->deq_seg;
604 do {
605 memset(seg->trbs, 0, SEGMENT_SIZE);
606 seg = seg->next;
607 } while (seg != ring->deq_seg);
608
609 /* Reset the software enqueue and dequeue pointers */
610 ring->deq_seg = ring->first_seg;
611 ring->dequeue = ring->first_seg->trbs;
612 ring->enq_seg = ring->deq_seg;
613 ring->enqueue = ring->dequeue;
614
615 /*
616 * Ring is now zeroed, so the HW should look for change of ownership
617 * when the cycle bit is set to 1.
618 */
619 ring->cycle_state = 1;
620
621 /*
622 * Reset the hardware dequeue pointer.
623 * Yes, this will need to be re-written after resume, but we're paranoid
624 * and want to make sure the hardware doesn't access bogus memory
625 * because, say, the BIOS or an SMI started the host without changing
626 * the command ring pointers.
627 */
628 xhci_set_cmd_ring_deq(xhci);
629}
630
Andiry Xu5535b1d2010-10-14 07:23:06 -0700631/*
632 * Stop HC (not bus-specific)
633 *
634 * This is called when the machine transition into S3/S4 mode.
635 *
636 */
637int xhci_suspend(struct xhci_hcd *xhci)
638{
639 int rc = 0;
640 struct usb_hcd *hcd = xhci_to_hcd(xhci);
641 u32 command;
Andiry Xu00292272010-12-27 17:39:02 +0800642 int i;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700643
644 spin_lock_irq(&xhci->lock);
645 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
646 /* step 1: stop endpoint */
647 /* skipped assuming that port suspend has done */
648
649 /* step 2: clear Run/Stop bit */
650 command = xhci_readl(xhci, &xhci->op_regs->command);
651 command &= ~CMD_RUN;
652 xhci_writel(xhci, command, &xhci->op_regs->command);
653 if (handshake(xhci, &xhci->op_regs->status,
654 STS_HALT, STS_HALT, 100*100)) {
655 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
656 spin_unlock_irq(&xhci->lock);
657 return -ETIMEDOUT;
658 }
Sarah Sharp89821322010-11-12 11:59:31 -0800659 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700660
661 /* step 3: save registers */
662 xhci_save_registers(xhci);
663
664 /* step 4: set CSS flag */
665 command = xhci_readl(xhci, &xhci->op_regs->command);
666 command |= CMD_CSS;
667 xhci_writel(xhci, command, &xhci->op_regs->command);
668 if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10*100)) {
669 xhci_warn(xhci, "WARN: xHC CMD_CSS timeout\n");
670 spin_unlock_irq(&xhci->lock);
671 return -ETIMEDOUT;
672 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700673 spin_unlock_irq(&xhci->lock);
674
Andiry Xu00292272010-12-27 17:39:02 +0800675 /* step 5: remove core well power */
676 /* synchronize irq when using MSI-X */
677 if (xhci->msix_entries) {
678 for (i = 0; i < xhci->msix_count; i++)
679 synchronize_irq(xhci->msix_entries[i].vector);
680 }
681
Andiry Xu5535b1d2010-10-14 07:23:06 -0700682 return rc;
683}
684
685/*
686 * start xHC (not bus-specific)
687 *
688 * This is called when the machine transition from S3/S4 mode.
689 *
690 */
691int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
692{
693 u32 command, temp = 0;
694 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Andiry Xu019a35f2011-01-06 15:43:17 +0800695 int retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700696
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800697 /* Wait a bit if the bus needs to settle from the transistion to
698 * suspend.
699 */
700 if (time_before(jiffies, xhci->bus_state[0].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -0700701 msleep(100);
702
703 spin_lock_irq(&xhci->lock);
704
705 if (!hibernated) {
706 /* step 1: restore register */
707 xhci_restore_registers(xhci);
708 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800709 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700710 /* step 3: restore state and start state*/
711 /* step 3: set CRS flag */
712 command = xhci_readl(xhci, &xhci->op_regs->command);
713 command |= CMD_CRS;
714 xhci_writel(xhci, command, &xhci->op_regs->command);
715 if (handshake(xhci, &xhci->op_regs->status,
716 STS_RESTORE, 0, 10*100)) {
717 xhci_dbg(xhci, "WARN: xHC CMD_CSS timeout\n");
718 spin_unlock_irq(&xhci->lock);
719 return -ETIMEDOUT;
720 }
721 temp = xhci_readl(xhci, &xhci->op_regs->status);
722 }
723
724 /* If restore operation fails, re-initialize the HC during resume */
725 if ((temp & STS_SRE) || hibernated) {
726 usb_root_hub_lost_power(hcd->self.root_hub);
727
728 xhci_dbg(xhci, "Stop HCD\n");
729 xhci_halt(xhci);
730 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700731 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +0800732 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700733
734#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
735 /* Tell the event ring poll function not to reschedule */
736 xhci->zombie = 1;
737 del_timer_sync(&xhci->event_ring_timer);
738#endif
739
740 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
741 temp = xhci_readl(xhci, &xhci->op_regs->status);
742 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
743 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
744 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
745 &xhci->ir_set->irq_pending);
746 xhci_print_ir_set(xhci, xhci->ir_set, 0);
747
748 xhci_dbg(xhci, "cleaning up memory\n");
749 xhci_mem_cleanup(xhci);
750 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
751 xhci_readl(xhci, &xhci->op_regs->status));
752
753 xhci_dbg(xhci, "Initialize the HCD\n");
754 retval = xhci_init(hcd);
755 if (retval)
756 return retval;
757
758 xhci_dbg(xhci, "Start the HCD\n");
759 retval = xhci_run(hcd);
760 if (!retval)
761 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
762 hcd->state = HC_STATE_SUSPENDED;
763 return retval;
764 }
765
Andiry Xu5535b1d2010-10-14 07:23:06 -0700766 /* step 4: set Run/Stop bit */
767 command = xhci_readl(xhci, &xhci->op_regs->command);
768 command |= CMD_RUN;
769 xhci_writel(xhci, command, &xhci->op_regs->command);
770 handshake(xhci, &xhci->op_regs->status, STS_HALT,
771 0, 250 * 1000);
772
773 /* step 5: walk topology and initialize portsc,
774 * portpmsc and portli
775 */
776 /* this is done in bus_resume */
777
778 /* step 6: restart each of the previously
779 * Running endpoints by ringing their doorbells
780 */
781
782 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700783
784 spin_unlock_irq(&xhci->lock);
785 return 0;
786}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700787#endif /* CONFIG_PM */
788
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700789/*-------------------------------------------------------------------------*/
790
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700791/**
792 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
793 * HCDs. Find the index for an endpoint given its descriptor. Use the return
794 * value to right shift 1 for the bitmask.
795 *
796 * Index = (epnum * 2) + direction - 1,
797 * where direction = 0 for OUT, 1 for IN.
798 * For control endpoints, the IN index is used (OUT index is unused), so
799 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
800 */
801unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
802{
803 unsigned int index;
804 if (usb_endpoint_xfer_control(desc))
805 index = (unsigned int) (usb_endpoint_num(desc)*2);
806 else
807 index = (unsigned int) (usb_endpoint_num(desc)*2) +
808 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
809 return index;
810}
811
Sarah Sharpf94e01862009-04-27 19:58:38 -0700812/* Find the flag for this endpoint (for use in the control context). Use the
813 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
814 * bit 1, etc.
815 */
816unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
817{
818 return 1 << (xhci_get_endpoint_index(desc) + 1);
819}
820
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700821/* Find the flag for this endpoint (for use in the control context). Use the
822 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
823 * bit 1, etc.
824 */
825unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
826{
827 return 1 << (ep_index + 1);
828}
829
Sarah Sharpf94e01862009-04-27 19:58:38 -0700830/* Compute the last valid endpoint context index. Basically, this is the
831 * endpoint index plus one. For slot contexts with more than valid endpoint,
832 * we find the most significant bit set in the added contexts flags.
833 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
834 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
835 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700836unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700837{
838 return fls(added_ctxs) - 1;
839}
840
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700841/* Returns 1 if the arguments are OK;
842 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
843 */
844int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -0700845 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
846 const char *func) {
847 struct xhci_hcd *xhci;
848 struct xhci_virt_device *virt_dev;
849
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700850 if (!hcd || (check_ep && !ep) || !udev) {
851 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
852 func);
853 return -EINVAL;
854 }
855 if (!udev->parent) {
856 printk(KERN_DEBUG "xHCI %s called for root hub\n",
857 func);
858 return 0;
859 }
Andiry Xu64927732010-10-14 07:22:45 -0700860
861 if (check_virt_dev) {
862 xhci = hcd_to_xhci(hcd);
863 if (!udev->slot_id || !xhci->devs
864 || !xhci->devs[udev->slot_id]) {
865 printk(KERN_DEBUG "xHCI %s called with unaddressed "
866 "device\n", func);
867 return -EINVAL;
868 }
869
870 virt_dev = xhci->devs[udev->slot_id];
871 if (virt_dev->udev != udev) {
872 printk(KERN_DEBUG "xHCI %s called with udev and "
873 "virt_dev does not match\n", func);
874 return -EINVAL;
875 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700876 }
Andiry Xu64927732010-10-14 07:22:45 -0700877
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700878 return 1;
879}
880
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700881static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700882 struct usb_device *udev, struct xhci_command *command,
883 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700884
885/*
886 * Full speed devices may have a max packet size greater than 8 bytes, but the
887 * USB core doesn't know that until it reads the first 8 bytes of the
888 * descriptor. If the usb_device's max packet size changes after that point,
889 * we need to issue an evaluate context command and wait on it.
890 */
891static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
892 unsigned int ep_index, struct urb *urb)
893{
894 struct xhci_container_ctx *in_ctx;
895 struct xhci_container_ctx *out_ctx;
896 struct xhci_input_control_ctx *ctrl_ctx;
897 struct xhci_ep_ctx *ep_ctx;
898 int max_packet_size;
899 int hw_max_packet_size;
900 int ret = 0;
901
902 out_ctx = xhci->devs[slot_id]->out_ctx;
903 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
904 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
905 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
906 if (hw_max_packet_size != max_packet_size) {
907 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
908 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
909 max_packet_size);
910 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
911 hw_max_packet_size);
912 xhci_dbg(xhci, "Issuing evaluate context command.\n");
913
914 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700915 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
916 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700917 in_ctx = xhci->devs[slot_id]->in_ctx;
918 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
919 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
920 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
921
922 /* Set up the input context flags for the command */
923 /* FIXME: This won't work if a non-default control endpoint
924 * changes max packet sizes.
925 */
926 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
927 ctrl_ctx->add_flags = EP0_FLAG;
928 ctrl_ctx->drop_flags = 0;
929
930 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
931 xhci_dbg_ctx(xhci, in_ctx, ep_index);
932 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
933 xhci_dbg_ctx(xhci, out_ctx, ep_index);
934
Sarah Sharp913a8a32009-09-04 10:53:13 -0700935 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
936 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700937
938 /* Clean up the input context for later use by bandwidth
939 * functions.
940 */
941 ctrl_ctx->add_flags = SLOT_FLAG;
942 }
943 return ret;
944}
945
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700946/*
947 * non-error returns are a promise to giveback() the urb later
948 * we drop ownership so next owner (or urb unlink) can get it
949 */
950int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
951{
952 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
953 unsigned long flags;
954 int ret = 0;
955 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700956 struct urb_priv *urb_priv;
957 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700958
Andiry Xu64927732010-10-14 07:22:45 -0700959 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
960 true, true, __func__) <= 0)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700961 return -EINVAL;
962
963 slot_id = urb->dev->slot_id;
964 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700965
Alan Stern541c7d42010-06-22 16:39:10 -0400966 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700967 if (!in_interrupt())
968 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
969 ret = -ESHUTDOWN;
970 goto exit;
971 }
Andiry Xu8e51adc2010-07-22 15:23:31 -0700972
973 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
974 size = urb->number_of_packets;
975 else
976 size = 1;
977
978 urb_priv = kzalloc(sizeof(struct urb_priv) +
979 size * sizeof(struct xhci_td *), mem_flags);
980 if (!urb_priv)
981 return -ENOMEM;
982
983 for (i = 0; i < size; i++) {
984 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
985 if (!urb_priv->td[i]) {
986 urb_priv->length = i;
987 xhci_urb_free_priv(xhci, urb_priv);
988 return -ENOMEM;
989 }
990 }
991
992 urb_priv->length = size;
993 urb_priv->td_cnt = 0;
994 urb->hcpriv = urb_priv;
995
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700996 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
997 /* Check to see if the max packet size for the default control
998 * endpoint changed during FS device enumeration
999 */
1000 if (urb->dev->speed == USB_SPEED_FULL) {
1001 ret = xhci_check_maxpacket(xhci, slot_id,
1002 ep_index, urb);
1003 if (ret < 0)
1004 return ret;
1005 }
1006
Sarah Sharpb11069f2009-07-27 12:03:23 -07001007 /* We have a spinlock and interrupts disabled, so we must pass
1008 * atomic context to this function, which may allocate memory.
1009 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001010 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001011 if (xhci->xhc_state & XHCI_STATE_DYING)
1012 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001013 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001014 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001015 spin_unlock_irqrestore(&xhci->lock, flags);
1016 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1017 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001018 if (xhci->xhc_state & XHCI_STATE_DYING)
1019 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001020 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1021 EP_GETTING_STREAMS) {
1022 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1023 "is transitioning to using streams.\n");
1024 ret = -EINVAL;
1025 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1026 EP_GETTING_NO_STREAMS) {
1027 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1028 "is transitioning to "
1029 "not having streams.\n");
1030 ret = -EINVAL;
1031 } else {
1032 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1033 slot_id, ep_index);
1034 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001035 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001036 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1037 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001038 if (xhci->xhc_state & XHCI_STATE_DYING)
1039 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001040 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1041 slot_id, ep_index);
1042 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001043 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001044 spin_lock_irqsave(&xhci->lock, flags);
1045 if (xhci->xhc_state & XHCI_STATE_DYING)
1046 goto dying;
1047 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1048 slot_id, ep_index);
1049 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001050 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001051exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001052 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001053dying:
Andiry Xu8e51adc2010-07-22 15:23:31 -07001054 xhci_urb_free_priv(xhci, urb_priv);
1055 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001056 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1057 "non-responsive xHCI host.\n",
1058 urb->ep->desc.bEndpointAddress, urb);
1059 spin_unlock_irqrestore(&xhci->lock, flags);
1060 return -ESHUTDOWN;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001061}
1062
Sarah Sharp021bff92010-07-29 22:12:20 -07001063/* Get the right ring for the given URB.
1064 * If the endpoint supports streams, boundary check the URB's stream ID.
1065 * If the endpoint doesn't support streams, return the singular endpoint ring.
1066 */
1067static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1068 struct urb *urb)
1069{
1070 unsigned int slot_id;
1071 unsigned int ep_index;
1072 unsigned int stream_id;
1073 struct xhci_virt_ep *ep;
1074
1075 slot_id = urb->dev->slot_id;
1076 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1077 stream_id = urb->stream_id;
1078 ep = &xhci->devs[slot_id]->eps[ep_index];
1079 /* Common case: no streams */
1080 if (!(ep->ep_state & EP_HAS_STREAMS))
1081 return ep->ring;
1082
1083 if (stream_id == 0) {
1084 xhci_warn(xhci,
1085 "WARN: Slot ID %u, ep index %u has streams, "
1086 "but URB has no stream ID.\n",
1087 slot_id, ep_index);
1088 return NULL;
1089 }
1090
1091 if (stream_id < ep->stream_info->num_streams)
1092 return ep->stream_info->stream_rings[stream_id];
1093
1094 xhci_warn(xhci,
1095 "WARN: Slot ID %u, ep index %u has "
1096 "stream IDs 1 to %u allocated, "
1097 "but stream ID %u is requested.\n",
1098 slot_id, ep_index,
1099 ep->stream_info->num_streams - 1,
1100 stream_id);
1101 return NULL;
1102}
1103
Sarah Sharpae636742009-04-29 19:02:31 -07001104/*
1105 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1106 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1107 * should pick up where it left off in the TD, unless a Set Transfer Ring
1108 * Dequeue Pointer is issued.
1109 *
1110 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1111 * the ring. Since the ring is a contiguous structure, they can't be physically
1112 * removed. Instead, there are two options:
1113 *
1114 * 1) If the HC is in the middle of processing the URB to be canceled, we
1115 * simply move the ring's dequeue pointer past those TRBs using the Set
1116 * Transfer Ring Dequeue Pointer command. This will be the common case,
1117 * when drivers timeout on the last submitted URB and attempt to cancel.
1118 *
1119 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1120 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1121 * HC will need to invalidate the any TRBs it has cached after the stop
1122 * endpoint command, as noted in the xHCI 0.95 errata.
1123 *
1124 * 3) The TD may have completed by the time the Stop Endpoint Command
1125 * completes, so software needs to handle that case too.
1126 *
1127 * This function should protect against the TD enqueueing code ringing the
1128 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1129 * It also needs to account for multiple cancellations on happening at the same
1130 * time for the same endpoint.
1131 *
1132 * Note that this function can be called in any context, or so says
1133 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001134 */
1135int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1136{
Sarah Sharpae636742009-04-29 19:02:31 -07001137 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001138 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001139 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001140 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001141 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001142 struct xhci_td *td;
1143 unsigned int ep_index;
1144 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001145 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001146
1147 xhci = hcd_to_xhci(hcd);
1148 spin_lock_irqsave(&xhci->lock, flags);
1149 /* Make sure the URB hasn't completed or been unlinked already */
1150 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1151 if (ret || !urb->hcpriv)
1152 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001153 temp = xhci_readl(xhci, &xhci->op_regs->status);
1154 if (temp == 0xffffffff) {
1155 xhci_dbg(xhci, "HW died, freeing TD.\n");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001156 urb_priv = urb->hcpriv;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001157
1158 usb_hcd_unlink_urb_from_ep(hcd, urb);
1159 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001160 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001161 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001162 return ret;
1163 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001164 if (xhci->xhc_state & XHCI_STATE_DYING) {
1165 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1166 "non-responsive xHCI host.\n",
1167 urb->ep->desc.bEndpointAddress, urb);
1168 /* Let the stop endpoint command watchdog timer (which set this
1169 * state) finish cleaning up the endpoint TD lists. We must
1170 * have caught it in the middle of dropping a lock and giving
1171 * back an URB.
1172 */
1173 goto done;
1174 }
Sarah Sharpae636742009-04-29 19:02:31 -07001175
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001176 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -07001177 xhci_dbg(xhci, "Event ring:\n");
1178 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -07001179 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001180 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001181 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1182 if (!ep_ring) {
1183 ret = -EINVAL;
1184 goto done;
1185 }
1186
Sarah Sharp66e49d82009-07-27 12:03:46 -07001187 xhci_dbg(xhci, "Endpoint ring:\n");
1188 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -07001189
Andiry Xu8e51adc2010-07-22 15:23:31 -07001190 urb_priv = urb->hcpriv;
1191
1192 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1193 td = urb_priv->td[i];
1194 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1195 }
1196
Sarah Sharpae636742009-04-29 19:02:31 -07001197 /* Queue a stop endpoint command, but only if this is
1198 * the first cancellation to be handled.
1199 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001200 if (!(ep->ep_state & EP_HALT_PENDING)) {
1201 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001202 ep->stop_cmds_pending++;
1203 ep->stop_cmd_timer.expires = jiffies +
1204 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1205 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001206 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001207 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001208 }
1209done:
1210 spin_unlock_irqrestore(&xhci->lock, flags);
1211 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001212}
1213
Sarah Sharpf94e01862009-04-27 19:58:38 -07001214/* Drop an endpoint from a new bandwidth configuration for this device.
1215 * Only one call to this function is allowed per endpoint before
1216 * check_bandwidth() or reset_bandwidth() must be called.
1217 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1218 * add the endpoint to the schedule with possibly new parameters denoted by a
1219 * different endpoint descriptor in usb_host_endpoint.
1220 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1221 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001222 *
1223 * The USB core will not allow URBs to be queued to an endpoint that is being
1224 * disabled, so there's no need for mutual exclusion to protect
1225 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001226 */
1227int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1228 struct usb_host_endpoint *ep)
1229{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001230 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001231 struct xhci_container_ctx *in_ctx, *out_ctx;
1232 struct xhci_input_control_ctx *ctrl_ctx;
1233 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001234 unsigned int last_ctx;
1235 unsigned int ep_index;
1236 struct xhci_ep_ctx *ep_ctx;
1237 u32 drop_flag;
1238 u32 new_add_flags, new_drop_flags, new_slot_info;
1239 int ret;
1240
Andiry Xu64927732010-10-14 07:22:45 -07001241 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001242 if (ret <= 0)
1243 return ret;
1244 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001245 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001246
1247 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1248 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1249 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1250 __func__, drop_flag);
1251 return 0;
1252 }
1253
Sarah Sharpf94e01862009-04-27 19:58:38 -07001254 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001255 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1256 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001257 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001258 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001259 /* If the HC already knows the endpoint is disabled,
1260 * or the HCD has noted it is disabled, ignore this request
1261 */
1262 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
John Yound115b042009-07-27 12:05:15 -07001263 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001264 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1265 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001266 return 0;
1267 }
1268
John Yound115b042009-07-27 12:05:15 -07001269 ctrl_ctx->drop_flags |= drop_flag;
1270 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001271
Sarah Sharp0a023c62009-09-18 08:55:12 -07001272 ctrl_ctx->add_flags &= ~drop_flag;
John Yound115b042009-07-27 12:05:15 -07001273 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001274
John Yound115b042009-07-27 12:05:15 -07001275 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
1276 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001277 /* Update the last valid endpoint context, if we deleted the last one */
John Yound115b042009-07-27 12:05:15 -07001278 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
1279 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1280 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001281 }
John Yound115b042009-07-27 12:05:15 -07001282 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001283
1284 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1285
Sarah Sharpf94e01862009-04-27 19:58:38 -07001286 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1287 (unsigned int) ep->desc.bEndpointAddress,
1288 udev->slot_id,
1289 (unsigned int) new_drop_flags,
1290 (unsigned int) new_add_flags,
1291 (unsigned int) new_slot_info);
1292 return 0;
1293}
1294
1295/* Add an endpoint to a new possible bandwidth configuration for this device.
1296 * Only one call to this function is allowed per endpoint before
1297 * check_bandwidth() or reset_bandwidth() must be called.
1298 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1299 * add the endpoint to the schedule with possibly new parameters denoted by a
1300 * different endpoint descriptor in usb_host_endpoint.
1301 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1302 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001303 *
1304 * The USB core will not allow URBs to be queued to an endpoint until the
1305 * configuration or alt setting is installed in the device, so there's no need
1306 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001307 */
1308int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1309 struct usb_host_endpoint *ep)
1310{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001311 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001312 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001313 unsigned int ep_index;
1314 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001315 struct xhci_slot_ctx *slot_ctx;
1316 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001317 u32 added_ctxs;
1318 unsigned int last_ctx;
1319 u32 new_add_flags, new_drop_flags, new_slot_info;
1320 int ret = 0;
1321
Andiry Xu64927732010-10-14 07:22:45 -07001322 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001323 if (ret <= 0) {
1324 /* So we won't queue a reset ep command for a root hub */
1325 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001326 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001327 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001328 xhci = hcd_to_xhci(hcd);
1329
1330 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1331 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1332 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1333 /* FIXME when we have to issue an evaluate endpoint command to
1334 * deal with ep0 max packet size changing once we get the
1335 * descriptors
1336 */
1337 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1338 __func__, added_ctxs);
1339 return 0;
1340 }
1341
Sarah Sharpf94e01862009-04-27 19:58:38 -07001342 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001343 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1344 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001345 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001346 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001347 /* If the HCD has already noted the endpoint is enabled,
1348 * ignore this request.
1349 */
John Yound115b042009-07-27 12:05:15 -07001350 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001351 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1352 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001353 return 0;
1354 }
1355
Sarah Sharpf88ba782009-05-14 11:44:22 -07001356 /*
1357 * Configuration and alternate setting changes must be done in
1358 * process context, not interrupt context (or so documenation
1359 * for usb_set_interface() and usb_set_configuration() claim).
1360 */
1361 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
Oliver Neukum319c3ea2009-12-16 19:43:59 +01001362 udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001363 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1364 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001365 return -ENOMEM;
1366 }
1367
John Yound115b042009-07-27 12:05:15 -07001368 ctrl_ctx->add_flags |= added_ctxs;
1369 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001370
1371 /* If xhci_endpoint_disable() was called for this endpoint, but the
1372 * xHC hasn't been notified yet through the check_bandwidth() call,
1373 * this re-adds a new state for the endpoint from the new endpoint
1374 * descriptors. We must drop and re-add this endpoint, so we leave the
1375 * drop flags alone.
1376 */
John Yound115b042009-07-27 12:05:15 -07001377 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001378
John Yound115b042009-07-27 12:05:15 -07001379 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001380 /* Update the last valid endpoint context, if we just added one past */
John Yound115b042009-07-27 12:05:15 -07001381 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1382 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1383 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001384 }
John Yound115b042009-07-27 12:05:15 -07001385 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001386
Sarah Sharpa1587d92009-07-27 12:03:15 -07001387 /* Store the usb_device pointer for later use */
1388 ep->hcpriv = udev;
1389
Sarah Sharpf94e01862009-04-27 19:58:38 -07001390 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1391 (unsigned int) ep->desc.bEndpointAddress,
1392 udev->slot_id,
1393 (unsigned int) new_drop_flags,
1394 (unsigned int) new_add_flags,
1395 (unsigned int) new_slot_info);
1396 return 0;
1397}
1398
John Yound115b042009-07-27 12:05:15 -07001399static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001400{
John Yound115b042009-07-27 12:05:15 -07001401 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001402 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001403 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001404 int i;
1405
1406 /* When a device's add flag and drop flag are zero, any subsequent
1407 * configure endpoint command will leave that endpoint's state
1408 * untouched. Make sure we don't leave any old state in the input
1409 * endpoint contexts.
1410 */
John Yound115b042009-07-27 12:05:15 -07001411 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1412 ctrl_ctx->drop_flags = 0;
1413 ctrl_ctx->add_flags = 0;
1414 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1415 slot_ctx->dev_info &= ~LAST_CTX_MASK;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001416 /* Endpoint 0 is always valid */
John Yound115b042009-07-27 12:05:15 -07001417 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001418 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001419 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001420 ep_ctx->ep_info = 0;
1421 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001422 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001423 ep_ctx->tx_info = 0;
1424 }
1425}
1426
Sarah Sharpf2217e82009-08-07 14:04:43 -07001427static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001428 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001429{
1430 int ret;
1431
Sarah Sharp913a8a32009-09-04 10:53:13 -07001432 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001433 case COMP_ENOMEM:
1434 dev_warn(&udev->dev, "Not enough host controller resources "
1435 "for new device state.\n");
1436 ret = -ENOMEM;
1437 /* FIXME: can we allocate more resources for the HC? */
1438 break;
1439 case COMP_BW_ERR:
1440 dev_warn(&udev->dev, "Not enough bandwidth "
1441 "for new device state.\n");
1442 ret = -ENOSPC;
1443 /* FIXME: can we go back to the old state? */
1444 break;
1445 case COMP_TRB_ERR:
1446 /* the HCD set up something wrong */
1447 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1448 "add flag = 1, "
1449 "and endpoint is not disabled.\n");
1450 ret = -EINVAL;
1451 break;
1452 case COMP_SUCCESS:
1453 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1454 ret = 0;
1455 break;
1456 default:
1457 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001458 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001459 ret = -EINVAL;
1460 break;
1461 }
1462 return ret;
1463}
1464
1465static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001466 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001467{
1468 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001469 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001470
Sarah Sharp913a8a32009-09-04 10:53:13 -07001471 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001472 case COMP_EINVAL:
1473 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1474 "context command.\n");
1475 ret = -EINVAL;
1476 break;
1477 case COMP_EBADSLT:
1478 dev_warn(&udev->dev, "WARN: slot not enabled for"
1479 "evaluate context command.\n");
1480 case COMP_CTX_STATE:
1481 dev_warn(&udev->dev, "WARN: invalid context state for "
1482 "evaluate context command.\n");
1483 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1484 ret = -EINVAL;
1485 break;
1486 case COMP_SUCCESS:
1487 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1488 ret = 0;
1489 break;
1490 default:
1491 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001492 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001493 ret = -EINVAL;
1494 break;
1495 }
1496 return ret;
1497}
1498
1499/* Issue a configure endpoint command or evaluate context command
1500 * and wait for it to finish.
1501 */
1502static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001503 struct usb_device *udev,
1504 struct xhci_command *command,
1505 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001506{
1507 int ret;
1508 int timeleft;
1509 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001510 struct xhci_container_ctx *in_ctx;
1511 struct completion *cmd_completion;
1512 int *cmd_status;
1513 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001514
1515 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001516 virt_dev = xhci->devs[udev->slot_id];
1517 if (command) {
1518 in_ctx = command->in_ctx;
1519 cmd_completion = command->completion;
1520 cmd_status = &command->status;
1521 command->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08001522
1523 /* Enqueue pointer can be left pointing to the link TRB,
1524 * we must handle that
1525 */
1526 if ((command->command_trb->link.control & TRB_TYPE_BITMASK)
1527 == TRB_TYPE(TRB_LINK))
1528 command->command_trb =
1529 xhci->cmd_ring->enq_seg->next->trbs;
1530
Sarah Sharp913a8a32009-09-04 10:53:13 -07001531 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1532 } else {
1533 in_ctx = virt_dev->in_ctx;
1534 cmd_completion = &virt_dev->cmd_completion;
1535 cmd_status = &virt_dev->cmd_status;
1536 }
Andiry Xu1d680642010-03-12 17:10:04 +08001537 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001538
Sarah Sharpf2217e82009-08-07 14:04:43 -07001539 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001540 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1541 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001542 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07001543 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001544 udev->slot_id);
1545 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08001546 if (command)
1547 list_del(&command->cmd_list);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001548 spin_unlock_irqrestore(&xhci->lock, flags);
1549 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1550 return -ENOMEM;
1551 }
1552 xhci_ring_cmd_db(xhci);
1553 spin_unlock_irqrestore(&xhci->lock, flags);
1554
1555 /* Wait for the configure endpoint command to complete */
1556 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07001557 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001558 USB_CTRL_SET_TIMEOUT);
1559 if (timeleft <= 0) {
1560 xhci_warn(xhci, "%s while waiting for %s command\n",
1561 timeleft == 0 ? "Timeout" : "Signal",
1562 ctx_change == 0 ?
1563 "configure endpoint" :
1564 "evaluate context");
1565 /* FIXME cancel the configure endpoint command */
1566 return -ETIME;
1567 }
1568
1569 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001570 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1571 return xhci_evaluate_context_result(xhci, udev, cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001572}
1573
Sarah Sharpf88ba782009-05-14 11:44:22 -07001574/* Called after one or more calls to xhci_add_endpoint() or
1575 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1576 * to call xhci_reset_bandwidth().
1577 *
1578 * Since we are in the middle of changing either configuration or
1579 * installing a new alt setting, the USB core won't allow URBs to be
1580 * enqueued for any endpoint on the old config or interface. Nothing
1581 * else should be touching the xhci->devs[slot_id] structure, so we
1582 * don't need to take the xhci->lock for manipulating that.
1583 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001584int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1585{
1586 int i;
1587 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001588 struct xhci_hcd *xhci;
1589 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07001590 struct xhci_input_control_ctx *ctrl_ctx;
1591 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001592
Andiry Xu64927732010-10-14 07:22:45 -07001593 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001594 if (ret <= 0)
1595 return ret;
1596 xhci = hcd_to_xhci(hcd);
1597
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001598 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001599 virt_dev = xhci->devs[udev->slot_id];
1600
1601 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07001602 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1603 ctrl_ctx->add_flags |= SLOT_FLAG;
1604 ctrl_ctx->add_flags &= ~EP0_FLAG;
1605 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1606 ctrl_ctx->drop_flags &= ~EP0_FLAG;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001607 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07001608 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1609 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1610 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001611
Sarah Sharp913a8a32009-09-04 10:53:13 -07001612 ret = xhci_configure_endpoint(xhci, udev, NULL,
1613 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001614 if (ret) {
1615 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001616 return ret;
1617 }
1618
1619 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07001620 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1621 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001622
John Yound115b042009-07-27 12:05:15 -07001623 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001624 /* Install new rings and free or cache any old rings */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001625 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001626 if (!virt_dev->eps[i].new_ring)
1627 continue;
1628 /* Only cache or free the old ring if it exists.
1629 * It may not if this is the first add of an endpoint.
1630 */
1631 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08001632 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001633 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001634 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1635 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001636 }
1637
Sarah Sharpf94e01862009-04-27 19:58:38 -07001638 return ret;
1639}
1640
1641void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1642{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001643 struct xhci_hcd *xhci;
1644 struct xhci_virt_device *virt_dev;
1645 int i, ret;
1646
Andiry Xu64927732010-10-14 07:22:45 -07001647 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001648 if (ret <= 0)
1649 return;
1650 xhci = hcd_to_xhci(hcd);
1651
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001652 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001653 virt_dev = xhci->devs[udev->slot_id];
1654 /* Free any rings allocated for added endpoints */
1655 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001656 if (virt_dev->eps[i].new_ring) {
1657 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1658 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001659 }
1660 }
John Yound115b042009-07-27 12:05:15 -07001661 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001662}
1663
Sarah Sharp5270b952009-09-04 10:53:11 -07001664static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001665 struct xhci_container_ctx *in_ctx,
1666 struct xhci_container_ctx *out_ctx,
1667 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07001668{
1669 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001670 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001671 ctrl_ctx->add_flags = add_flags;
1672 ctrl_ctx->drop_flags = drop_flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001673 xhci_slot_copy(xhci, in_ctx, out_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001674 ctrl_ctx->add_flags |= SLOT_FLAG;
1675
Sarah Sharp913a8a32009-09-04 10:53:13 -07001676 xhci_dbg(xhci, "Input Context:\n");
1677 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07001678}
1679
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001680void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1681 unsigned int slot_id, unsigned int ep_index,
1682 struct xhci_dequeue_state *deq_state)
1683{
1684 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001685 struct xhci_ep_ctx *ep_ctx;
1686 u32 added_ctxs;
1687 dma_addr_t addr;
1688
Sarah Sharp913a8a32009-09-04 10:53:13 -07001689 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1690 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001691 in_ctx = xhci->devs[slot_id]->in_ctx;
1692 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1693 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1694 deq_state->new_deq_ptr);
1695 if (addr == 0) {
1696 xhci_warn(xhci, "WARN Cannot submit config ep after "
1697 "reset ep command\n");
1698 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1699 deq_state->new_deq_seg,
1700 deq_state->new_deq_ptr);
1701 return;
1702 }
1703 ep_ctx->deq = addr | deq_state->new_cycle_state;
1704
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001705 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001706 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1707 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001708}
1709
Sarah Sharp82d10092009-08-07 14:04:52 -07001710void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001711 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07001712{
1713 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001714 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07001715
1716 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001717 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07001718 /* We need to move the HW's dequeue pointer past this TD,
1719 * or it will attempt to resend it on the next doorbell ring.
1720 */
1721 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001722 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001723 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07001724
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001725 /* HW with the reset endpoint quirk will use the saved dequeue state to
1726 * issue a configure endpoint command later.
1727 */
1728 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1729 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001730 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001731 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001732 } else {
1733 /* Better hope no one uses the input context between now and the
1734 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001735 * XXX: No idea how this hardware will react when stream rings
1736 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001737 */
1738 xhci_dbg(xhci, "Setting up input context for "
1739 "configure endpoint command\n");
1740 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1741 ep_index, &deq_state);
1742 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001743}
1744
Sarah Sharpa1587d92009-07-27 12:03:15 -07001745/* Deal with stalled endpoints. The core should have sent the control message
1746 * to clear the halt condition. However, we need to make the xHCI hardware
1747 * reset its sequence number, since a device will expect a sequence number of
1748 * zero after the halt condition is cleared.
1749 * Context: in_interrupt
1750 */
1751void xhci_endpoint_reset(struct usb_hcd *hcd,
1752 struct usb_host_endpoint *ep)
1753{
1754 struct xhci_hcd *xhci;
1755 struct usb_device *udev;
1756 unsigned int ep_index;
1757 unsigned long flags;
1758 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001759 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001760
1761 xhci = hcd_to_xhci(hcd);
1762 udev = (struct usb_device *) ep->hcpriv;
1763 /* Called with a root hub endpoint (or an endpoint that wasn't added
1764 * with xhci_add_endpoint()
1765 */
1766 if (!ep->hcpriv)
1767 return;
1768 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001769 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1770 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001771 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1772 ep->desc.bEndpointAddress);
1773 return;
1774 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001775 if (usb_endpoint_xfer_control(&ep->desc)) {
1776 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1777 return;
1778 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001779
1780 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1781 spin_lock_irqsave(&xhci->lock, flags);
1782 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001783 /*
1784 * Can't change the ring dequeue pointer until it's transitioned to the
1785 * stopped state, which is only upon a successful reset endpoint
1786 * command. Better hope that last command worked!
1787 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07001788 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001789 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1790 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001791 xhci_ring_cmd_db(xhci);
1792 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07001793 virt_ep->stopped_td = NULL;
1794 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001795 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001796 spin_unlock_irqrestore(&xhci->lock, flags);
1797
1798 if (ret)
1799 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1800}
1801
Sarah Sharp8df75f42010-04-02 15:34:16 -07001802static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1803 struct usb_device *udev, struct usb_host_endpoint *ep,
1804 unsigned int slot_id)
1805{
1806 int ret;
1807 unsigned int ep_index;
1808 unsigned int ep_state;
1809
1810 if (!ep)
1811 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07001812 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001813 if (ret <= 0)
1814 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04001815 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07001816 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1817 " descriptor for ep 0x%x does not support streams\n",
1818 ep->desc.bEndpointAddress);
1819 return -EINVAL;
1820 }
1821
1822 ep_index = xhci_get_endpoint_index(&ep->desc);
1823 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1824 if (ep_state & EP_HAS_STREAMS ||
1825 ep_state & EP_GETTING_STREAMS) {
1826 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1827 "already has streams set up.\n",
1828 ep->desc.bEndpointAddress);
1829 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1830 "dynamic stream context array reallocation.\n");
1831 return -EINVAL;
1832 }
1833 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1834 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1835 "endpoint 0x%x; URBs are pending.\n",
1836 ep->desc.bEndpointAddress);
1837 return -EINVAL;
1838 }
1839 return 0;
1840}
1841
1842static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1843 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1844{
1845 unsigned int max_streams;
1846
1847 /* The stream context array size must be a power of two */
1848 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1849 /*
1850 * Find out how many primary stream array entries the host controller
1851 * supports. Later we may use secondary stream arrays (similar to 2nd
1852 * level page entries), but that's an optional feature for xHCI host
1853 * controllers. xHCs must support at least 4 stream IDs.
1854 */
1855 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1856 if (*num_stream_ctxs > max_streams) {
1857 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1858 max_streams);
1859 *num_stream_ctxs = max_streams;
1860 *num_streams = max_streams;
1861 }
1862}
1863
1864/* Returns an error code if one of the endpoint already has streams.
1865 * This does not change any data structures, it only checks and gathers
1866 * information.
1867 */
1868static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1869 struct usb_device *udev,
1870 struct usb_host_endpoint **eps, unsigned int num_eps,
1871 unsigned int *num_streams, u32 *changed_ep_bitmask)
1872{
Sarah Sharp8df75f42010-04-02 15:34:16 -07001873 unsigned int max_streams;
1874 unsigned int endpoint_flag;
1875 int i;
1876 int ret;
1877
1878 for (i = 0; i < num_eps; i++) {
1879 ret = xhci_check_streams_endpoint(xhci, udev,
1880 eps[i], udev->slot_id);
1881 if (ret < 0)
1882 return ret;
1883
Alan Stern842f1692010-04-30 12:44:46 -04001884 max_streams = USB_SS_MAX_STREAMS(
1885 eps[i]->ss_ep_comp.bmAttributes);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001886 if (max_streams < (*num_streams - 1)) {
1887 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1888 eps[i]->desc.bEndpointAddress,
1889 max_streams);
1890 *num_streams = max_streams+1;
1891 }
1892
1893 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1894 if (*changed_ep_bitmask & endpoint_flag)
1895 return -EINVAL;
1896 *changed_ep_bitmask |= endpoint_flag;
1897 }
1898 return 0;
1899}
1900
1901static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1902 struct usb_device *udev,
1903 struct usb_host_endpoint **eps, unsigned int num_eps)
1904{
1905 u32 changed_ep_bitmask = 0;
1906 unsigned int slot_id;
1907 unsigned int ep_index;
1908 unsigned int ep_state;
1909 int i;
1910
1911 slot_id = udev->slot_id;
1912 if (!xhci->devs[slot_id])
1913 return 0;
1914
1915 for (i = 0; i < num_eps; i++) {
1916 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1917 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1918 /* Are streams already being freed for the endpoint? */
1919 if (ep_state & EP_GETTING_NO_STREAMS) {
1920 xhci_warn(xhci, "WARN Can't disable streams for "
1921 "endpoint 0x%x\n, "
1922 "streams are being disabled already.",
1923 eps[i]->desc.bEndpointAddress);
1924 return 0;
1925 }
1926 /* Are there actually any streams to free? */
1927 if (!(ep_state & EP_HAS_STREAMS) &&
1928 !(ep_state & EP_GETTING_STREAMS)) {
1929 xhci_warn(xhci, "WARN Can't disable streams for "
1930 "endpoint 0x%x\n, "
1931 "streams are already disabled!",
1932 eps[i]->desc.bEndpointAddress);
1933 xhci_warn(xhci, "WARN xhci_free_streams() called "
1934 "with non-streams endpoint\n");
1935 return 0;
1936 }
1937 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1938 }
1939 return changed_ep_bitmask;
1940}
1941
1942/*
1943 * The USB device drivers use this function (though the HCD interface in USB
1944 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1945 * coordinate mass storage command queueing across multiple endpoints (basically
1946 * a stream ID == a task ID).
1947 *
1948 * Setting up streams involves allocating the same size stream context array
1949 * for each endpoint and issuing a configure endpoint command for all endpoints.
1950 *
1951 * Don't allow the call to succeed if one endpoint only supports one stream
1952 * (which means it doesn't support streams at all).
1953 *
1954 * Drivers may get less stream IDs than they asked for, if the host controller
1955 * hardware or endpoints claim they can't support the number of requested
1956 * stream IDs.
1957 */
1958int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1959 struct usb_host_endpoint **eps, unsigned int num_eps,
1960 unsigned int num_streams, gfp_t mem_flags)
1961{
1962 int i, ret;
1963 struct xhci_hcd *xhci;
1964 struct xhci_virt_device *vdev;
1965 struct xhci_command *config_cmd;
1966 unsigned int ep_index;
1967 unsigned int num_stream_ctxs;
1968 unsigned long flags;
1969 u32 changed_ep_bitmask = 0;
1970
1971 if (!eps)
1972 return -EINVAL;
1973
1974 /* Add one to the number of streams requested to account for
1975 * stream 0 that is reserved for xHCI usage.
1976 */
1977 num_streams += 1;
1978 xhci = hcd_to_xhci(hcd);
1979 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
1980 num_streams);
1981
1982 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
1983 if (!config_cmd) {
1984 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
1985 return -ENOMEM;
1986 }
1987
1988 /* Check to make sure all endpoints are not already configured for
1989 * streams. While we're at it, find the maximum number of streams that
1990 * all the endpoints will support and check for duplicate endpoints.
1991 */
1992 spin_lock_irqsave(&xhci->lock, flags);
1993 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
1994 num_eps, &num_streams, &changed_ep_bitmask);
1995 if (ret < 0) {
1996 xhci_free_command(xhci, config_cmd);
1997 spin_unlock_irqrestore(&xhci->lock, flags);
1998 return ret;
1999 }
2000 if (num_streams <= 1) {
2001 xhci_warn(xhci, "WARN: endpoints can't handle "
2002 "more than one stream.\n");
2003 xhci_free_command(xhci, config_cmd);
2004 spin_unlock_irqrestore(&xhci->lock, flags);
2005 return -EINVAL;
2006 }
2007 vdev = xhci->devs[udev->slot_id];
2008 /* Mark each endpoint as being in transistion, so
2009 * xhci_urb_enqueue() will reject all URBs.
2010 */
2011 for (i = 0; i < num_eps; i++) {
2012 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2013 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
2014 }
2015 spin_unlock_irqrestore(&xhci->lock, flags);
2016
2017 /* Setup internal data structures and allocate HW data structures for
2018 * streams (but don't install the HW structures in the input context
2019 * until we're sure all memory allocation succeeded).
2020 */
2021 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
2022 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
2023 num_stream_ctxs, num_streams);
2024
2025 for (i = 0; i < num_eps; i++) {
2026 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2027 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
2028 num_stream_ctxs,
2029 num_streams, mem_flags);
2030 if (!vdev->eps[ep_index].stream_info)
2031 goto cleanup;
2032 /* Set maxPstreams in endpoint context and update deq ptr to
2033 * point to stream context array. FIXME
2034 */
2035 }
2036
2037 /* Set up the input context for a configure endpoint command. */
2038 for (i = 0; i < num_eps; i++) {
2039 struct xhci_ep_ctx *ep_ctx;
2040
2041 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2042 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
2043
2044 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
2045 vdev->out_ctx, ep_index);
2046 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
2047 vdev->eps[ep_index].stream_info);
2048 }
2049 /* Tell the HW to drop its old copy of the endpoint context info
2050 * and add the updated copy from the input context.
2051 */
2052 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
2053 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
2054
2055 /* Issue and wait for the configure endpoint command */
2056 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
2057 false, false);
2058
2059 /* xHC rejected the configure endpoint command for some reason, so we
2060 * leave the old ring intact and free our internal streams data
2061 * structure.
2062 */
2063 if (ret < 0)
2064 goto cleanup;
2065
2066 spin_lock_irqsave(&xhci->lock, flags);
2067 for (i = 0; i < num_eps; i++) {
2068 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2069 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
2070 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
2071 udev->slot_id, ep_index);
2072 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
2073 }
2074 xhci_free_command(xhci, config_cmd);
2075 spin_unlock_irqrestore(&xhci->lock, flags);
2076
2077 /* Subtract 1 for stream 0, which drivers can't use */
2078 return num_streams - 1;
2079
2080cleanup:
2081 /* If it didn't work, free the streams! */
2082 for (i = 0; i < num_eps; i++) {
2083 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2084 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07002085 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07002086 /* FIXME Unset maxPstreams in endpoint context and
2087 * update deq ptr to point to normal string ring.
2088 */
2089 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
2090 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
2091 xhci_endpoint_zero(xhci, vdev, eps[i]);
2092 }
2093 xhci_free_command(xhci, config_cmd);
2094 return -ENOMEM;
2095}
2096
2097/* Transition the endpoint from using streams to being a "normal" endpoint
2098 * without streams.
2099 *
2100 * Modify the endpoint context state, submit a configure endpoint command,
2101 * and free all endpoint rings for streams if that completes successfully.
2102 */
2103int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2104 struct usb_host_endpoint **eps, unsigned int num_eps,
2105 gfp_t mem_flags)
2106{
2107 int i, ret;
2108 struct xhci_hcd *xhci;
2109 struct xhci_virt_device *vdev;
2110 struct xhci_command *command;
2111 unsigned int ep_index;
2112 unsigned long flags;
2113 u32 changed_ep_bitmask;
2114
2115 xhci = hcd_to_xhci(hcd);
2116 vdev = xhci->devs[udev->slot_id];
2117
2118 /* Set up a configure endpoint command to remove the streams rings */
2119 spin_lock_irqsave(&xhci->lock, flags);
2120 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
2121 udev, eps, num_eps);
2122 if (changed_ep_bitmask == 0) {
2123 spin_unlock_irqrestore(&xhci->lock, flags);
2124 return -EINVAL;
2125 }
2126
2127 /* Use the xhci_command structure from the first endpoint. We may have
2128 * allocated too many, but the driver may call xhci_free_streams() for
2129 * each endpoint it grouped into one call to xhci_alloc_streams().
2130 */
2131 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
2132 command = vdev->eps[ep_index].stream_info->free_streams_command;
2133 for (i = 0; i < num_eps; i++) {
2134 struct xhci_ep_ctx *ep_ctx;
2135
2136 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2137 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
2138 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
2139 EP_GETTING_NO_STREAMS;
2140
2141 xhci_endpoint_copy(xhci, command->in_ctx,
2142 vdev->out_ctx, ep_index);
2143 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
2144 &vdev->eps[ep_index]);
2145 }
2146 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
2147 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
2148 spin_unlock_irqrestore(&xhci->lock, flags);
2149
2150 /* Issue and wait for the configure endpoint command,
2151 * which must succeed.
2152 */
2153 ret = xhci_configure_endpoint(xhci, udev, command,
2154 false, true);
2155
2156 /* xHC rejected the configure endpoint command for some reason, so we
2157 * leave the streams rings intact.
2158 */
2159 if (ret < 0)
2160 return ret;
2161
2162 spin_lock_irqsave(&xhci->lock, flags);
2163 for (i = 0; i < num_eps; i++) {
2164 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2165 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07002166 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07002167 /* FIXME Unset maxPstreams in endpoint context and
2168 * update deq ptr to point to normal string ring.
2169 */
2170 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
2171 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
2172 }
2173 spin_unlock_irqrestore(&xhci->lock, flags);
2174
2175 return 0;
2176}
2177
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002178/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002179 * This submits a Reset Device Command, which will set the device state to 0,
2180 * set the device address to 0, and disable all the endpoints except the default
2181 * control endpoint. The USB core should come back and call
2182 * xhci_address_device(), and then re-set up the configuration. If this is
2183 * called because of a usb_reset_and_verify_device(), then the old alternate
2184 * settings will be re-installed through the normal bandwidth allocation
2185 * functions.
2186 *
2187 * Wait for the Reset Device command to finish. Remove all structures
2188 * associated with the endpoints that were disabled. Clear the input device
2189 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07002190 *
2191 * If the virt_dev to be reset does not exist or does not match the udev,
2192 * it means the device is lost, possibly due to the xHC restore error and
2193 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
2194 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002195 */
Andiry Xuf0615c42010-10-14 07:22:48 -07002196int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002197{
2198 int ret, i;
2199 unsigned long flags;
2200 struct xhci_hcd *xhci;
2201 unsigned int slot_id;
2202 struct xhci_virt_device *virt_dev;
2203 struct xhci_command *reset_device_cmd;
2204 int timeleft;
2205 int last_freed_endpoint;
2206
Andiry Xuf0615c42010-10-14 07:22:48 -07002207 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002208 if (ret <= 0)
2209 return ret;
2210 xhci = hcd_to_xhci(hcd);
2211 slot_id = udev->slot_id;
2212 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07002213 if (!virt_dev) {
2214 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
2215 "not exist. Re-allocate the device\n", slot_id);
2216 ret = xhci_alloc_dev(hcd, udev);
2217 if (ret == 1)
2218 return 0;
2219 else
2220 return -EINVAL;
2221 }
2222
2223 if (virt_dev->udev != udev) {
2224 /* If the virt_dev and the udev does not match, this virt_dev
2225 * may belong to another udev.
2226 * Re-allocate the device.
2227 */
2228 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
2229 "not match the udev. Re-allocate the device\n",
2230 slot_id);
2231 ret = xhci_alloc_dev(hcd, udev);
2232 if (ret == 1)
2233 return 0;
2234 else
2235 return -EINVAL;
2236 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002237
2238 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
2239 /* Allocate the command structure that holds the struct completion.
2240 * Assume we're in process context, since the normal device reset
2241 * process has to wait for the device anyway. Storage devices are
2242 * reset as part of error handling, so use GFP_NOIO instead of
2243 * GFP_KERNEL.
2244 */
2245 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
2246 if (!reset_device_cmd) {
2247 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
2248 return -ENOMEM;
2249 }
2250
2251 /* Attempt to submit the Reset Device command to the command ring */
2252 spin_lock_irqsave(&xhci->lock, flags);
2253 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002254
2255 /* Enqueue pointer can be left pointing to the link TRB,
2256 * we must handle that
2257 */
2258 if ((reset_device_cmd->command_trb->link.control & TRB_TYPE_BITMASK)
2259 == TRB_TYPE(TRB_LINK))
2260 reset_device_cmd->command_trb =
2261 xhci->cmd_ring->enq_seg->next->trbs;
2262
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002263 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
2264 ret = xhci_queue_reset_device(xhci, slot_id);
2265 if (ret) {
2266 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2267 list_del(&reset_device_cmd->cmd_list);
2268 spin_unlock_irqrestore(&xhci->lock, flags);
2269 goto command_cleanup;
2270 }
2271 xhci_ring_cmd_db(xhci);
2272 spin_unlock_irqrestore(&xhci->lock, flags);
2273
2274 /* Wait for the Reset Device command to finish */
2275 timeleft = wait_for_completion_interruptible_timeout(
2276 reset_device_cmd->completion,
2277 USB_CTRL_SET_TIMEOUT);
2278 if (timeleft <= 0) {
2279 xhci_warn(xhci, "%s while waiting for reset device command\n",
2280 timeleft == 0 ? "Timeout" : "Signal");
2281 spin_lock_irqsave(&xhci->lock, flags);
2282 /* The timeout might have raced with the event ring handler, so
2283 * only delete from the list if the item isn't poisoned.
2284 */
2285 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
2286 list_del(&reset_device_cmd->cmd_list);
2287 spin_unlock_irqrestore(&xhci->lock, flags);
2288 ret = -ETIME;
2289 goto command_cleanup;
2290 }
2291
2292 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
2293 * unless we tried to reset a slot ID that wasn't enabled,
2294 * or the device wasn't in the addressed or configured state.
2295 */
2296 ret = reset_device_cmd->status;
2297 switch (ret) {
2298 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
2299 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
2300 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
2301 slot_id,
2302 xhci_get_slot_state(xhci, virt_dev->out_ctx));
2303 xhci_info(xhci, "Not freeing device rings.\n");
2304 /* Don't treat this as an error. May change my mind later. */
2305 ret = 0;
2306 goto command_cleanup;
2307 case COMP_SUCCESS:
2308 xhci_dbg(xhci, "Successful reset device command.\n");
2309 break;
2310 default:
2311 if (xhci_is_vendor_info_code(xhci, ret))
2312 break;
2313 xhci_warn(xhci, "Unknown completion code %u for "
2314 "reset device command.\n", ret);
2315 ret = -EINVAL;
2316 goto command_cleanup;
2317 }
2318
2319 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
2320 last_freed_endpoint = 1;
2321 for (i = 1; i < 31; ++i) {
2322 if (!virt_dev->eps[i].ring)
2323 continue;
2324 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2325 last_freed_endpoint = i;
2326 }
2327 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
2328 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2329 ret = 0;
2330
2331command_cleanup:
2332 xhci_free_command(xhci, reset_device_cmd);
2333 return ret;
2334}
2335
2336/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002337 * At this point, the struct usb_device is about to go away, the device has
2338 * disconnected, and all traffic has been stopped and the endpoints have been
2339 * disabled. Free any HC data structures associated with that device.
2340 */
2341void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2342{
2343 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002344 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002345 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002346 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07002347 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002348
Andiry Xu64927732010-10-14 07:22:45 -07002349 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2350 if (ret <= 0)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002351 return;
Andiry Xu64927732010-10-14 07:22:45 -07002352
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002353 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002354
2355 /* Stop any wayward timer functions (which may grab the lock) */
2356 for (i = 0; i < 31; ++i) {
2357 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2358 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2359 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002360
2361 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002362 /* Don't disable the slot if the host controller is dead. */
2363 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002364 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002365 xhci_free_virt_device(xhci, udev->slot_id);
2366 spin_unlock_irqrestore(&xhci->lock, flags);
2367 return;
2368 }
2369
Sarah Sharp23e3be12009-04-29 19:05:20 -07002370 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002371 spin_unlock_irqrestore(&xhci->lock, flags);
2372 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2373 return;
2374 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002375 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002376 spin_unlock_irqrestore(&xhci->lock, flags);
2377 /*
2378 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07002379 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002380 */
2381}
2382
2383/*
2384 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2385 * timed out, or allocating memory failed. Returns 1 on success.
2386 */
2387int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2388{
2389 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2390 unsigned long flags;
2391 int timeleft;
2392 int ret;
2393
2394 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002395 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002396 if (ret) {
2397 spin_unlock_irqrestore(&xhci->lock, flags);
2398 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2399 return 0;
2400 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002401 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002402 spin_unlock_irqrestore(&xhci->lock, flags);
2403
2404 /* XXX: how much time for xHC slot assignment? */
2405 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2406 USB_CTRL_SET_TIMEOUT);
2407 if (timeleft <= 0) {
2408 xhci_warn(xhci, "%s while waiting for a slot\n",
2409 timeleft == 0 ? "Timeout" : "Signal");
2410 /* FIXME cancel the enable slot request */
2411 return 0;
2412 }
2413
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002414 if (!xhci->slot_id) {
2415 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002416 return 0;
2417 }
Sarah Sharpa6d940d2010-12-28 13:08:42 -08002418 /* xhci_alloc_virt_device() does not touch rings; no need to lock.
2419 * Use GFP_NOIO, since this function can be called from
2420 * xhci_discover_or_reset_device(), which may be called as part of
2421 * mass storage driver error handling.
2422 */
2423 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002424 /* Disable slot, if we can do it without mem alloc */
2425 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07002426 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002427 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2428 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002429 spin_unlock_irqrestore(&xhci->lock, flags);
2430 return 0;
2431 }
2432 udev->slot_id = xhci->slot_id;
2433 /* Is this a LS or FS device under a HS hub? */
2434 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002435 return 1;
2436}
2437
2438/*
2439 * Issue an Address Device command (which will issue a SetAddress request to
2440 * the device).
2441 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2442 * we should only issue and wait on one address command at the same time.
2443 *
2444 * We add one to the device address issued by the hardware because the USB core
2445 * uses address 1 for the root hubs (even though they're not really devices).
2446 */
2447int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2448{
2449 unsigned long flags;
2450 int timeleft;
2451 struct xhci_virt_device *virt_dev;
2452 int ret = 0;
2453 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07002454 struct xhci_slot_ctx *slot_ctx;
2455 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07002456 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002457
2458 if (!udev->slot_id) {
2459 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2460 return -EINVAL;
2461 }
2462
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002463 virt_dev = xhci->devs[udev->slot_id];
2464
Andiry Xuf0615c42010-10-14 07:22:48 -07002465 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2466 /*
2467 * If this is the first Set Address since device plug-in or
2468 * virt_device realloaction after a resume with an xHCI power loss,
2469 * then set up the slot context.
2470 */
2471 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002472 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07002473 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02002474 else
2475 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002476 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002477 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002478
Sarah Sharpf88ba782009-05-14 11:44:22 -07002479 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07002480 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2481 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002482 if (ret) {
2483 spin_unlock_irqrestore(&xhci->lock, flags);
2484 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2485 return ret;
2486 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002487 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002488 spin_unlock_irqrestore(&xhci->lock, flags);
2489
2490 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2491 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2492 USB_CTRL_SET_TIMEOUT);
2493 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2494 * the SetAddress() "recovery interval" required by USB and aborting the
2495 * command on a timeout.
2496 */
2497 if (timeleft <= 0) {
2498 xhci_warn(xhci, "%s while waiting for a slot\n",
2499 timeleft == 0 ? "Timeout" : "Signal");
2500 /* FIXME cancel the address device command */
2501 return -ETIME;
2502 }
2503
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002504 switch (virt_dev->cmd_status) {
2505 case COMP_CTX_STATE:
2506 case COMP_EBADSLT:
2507 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2508 udev->slot_id);
2509 ret = -EINVAL;
2510 break;
2511 case COMP_TX_ERR:
2512 dev_warn(&udev->dev, "Device not responding to set address.\n");
2513 ret = -EPROTO;
2514 break;
2515 case COMP_SUCCESS:
2516 xhci_dbg(xhci, "Successful Address Device command\n");
2517 break;
2518 default:
2519 xhci_err(xhci, "ERROR: unexpected command completion "
2520 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002521 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002522 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002523 ret = -EINVAL;
2524 break;
2525 }
2526 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002527 return ret;
2528 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07002529 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2530 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2531 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002532 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002533 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2534 (unsigned long long)
2535 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002536 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07002537 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002538 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002539 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002540 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002541 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002542 /*
2543 * USB core uses address 1 for the roothubs, so we add one to the
2544 * address given back to us by the HC.
2545 */
John Yound115b042009-07-27 12:05:15 -07002546 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Andiry Xuc8d4af82010-10-14 07:22:51 -07002547 /* Use kernel assigned address for devices; store xHC assigned
2548 * address locally. */
2549 virt_dev->address = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002550 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07002551 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2552 ctrl_ctx->add_flags = 0;
2553 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002554
Andiry Xuc8d4af82010-10-14 07:22:51 -07002555 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002556
2557 return 0;
2558}
2559
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002560/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2561 * internal data structures for the device.
2562 */
2563int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2564 struct usb_tt *tt, gfp_t mem_flags)
2565{
2566 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2567 struct xhci_virt_device *vdev;
2568 struct xhci_command *config_cmd;
2569 struct xhci_input_control_ctx *ctrl_ctx;
2570 struct xhci_slot_ctx *slot_ctx;
2571 unsigned long flags;
2572 unsigned think_time;
2573 int ret;
2574
2575 /* Ignore root hubs */
2576 if (!hdev->parent)
2577 return 0;
2578
2579 vdev = xhci->devs[hdev->slot_id];
2580 if (!vdev) {
2581 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2582 return -EINVAL;
2583 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08002584 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002585 if (!config_cmd) {
2586 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2587 return -ENOMEM;
2588 }
2589
2590 spin_lock_irqsave(&xhci->lock, flags);
2591 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2592 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2593 ctrl_ctx->add_flags |= SLOT_FLAG;
2594 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2595 slot_ctx->dev_info |= DEV_HUB;
2596 if (tt->multi)
2597 slot_ctx->dev_info |= DEV_MTT;
2598 if (xhci->hci_version > 0x95) {
2599 xhci_dbg(xhci, "xHCI version %x needs hub "
2600 "TT think time and number of ports\n",
2601 (unsigned int) xhci->hci_version);
2602 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2603 /* Set TT think time - convert from ns to FS bit times.
2604 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2605 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2606 */
2607 think_time = tt->think_time;
2608 if (think_time != 0)
2609 think_time = (think_time / 666) - 1;
2610 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2611 } else {
2612 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2613 "TT think time or number of ports\n",
2614 (unsigned int) xhci->hci_version);
2615 }
2616 slot_ctx->dev_state = 0;
2617 spin_unlock_irqrestore(&xhci->lock, flags);
2618
2619 xhci_dbg(xhci, "Set up %s for hub device.\n",
2620 (xhci->hci_version > 0x95) ?
2621 "configure endpoint" : "evaluate context");
2622 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2623 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2624
2625 /* Issue and wait for the configure endpoint or
2626 * evaluate context command.
2627 */
2628 if (xhci->hci_version > 0x95)
2629 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2630 false, false);
2631 else
2632 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2633 true, false);
2634
2635 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2636 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2637
2638 xhci_free_command(xhci, config_cmd);
2639 return ret;
2640}
2641
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002642int xhci_get_frame(struct usb_hcd *hcd)
2643{
2644 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2645 /* EHCI mods by the periodic size. Why? */
2646 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2647}
2648
2649MODULE_DESCRIPTION(DRIVER_DESC);
2650MODULE_AUTHOR(DRIVER_AUTHOR);
2651MODULE_LICENSE("GPL");
2652
2653static int __init xhci_hcd_init(void)
2654{
2655#ifdef CONFIG_PCI
2656 int retval = 0;
2657
2658 retval = xhci_register_pci();
2659
2660 if (retval < 0) {
2661 printk(KERN_DEBUG "Problem registering PCI driver.");
2662 return retval;
2663 }
2664#endif
Sarah Sharp98441972009-05-14 11:44:18 -07002665 /*
2666 * Check the compiler generated sizes of structures that must be laid
2667 * out in specific ways for hardware access.
2668 */
2669 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2670 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2671 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2672 /* xhci_device_control has eight fields, and also
2673 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2674 */
Sarah Sharp98441972009-05-14 11:44:18 -07002675 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2676 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2677 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2678 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2679 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2680 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2681 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2682 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002683 return 0;
2684}
2685module_init(xhci_hcd_init);
2686
2687static void __exit xhci_hcd_cleanup(void)
2688{
2689#ifdef CONFIG_PCI
2690 xhci_unregister_pci();
2691#endif
2692}
2693module_exit(xhci_hcd_cleanup);