Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 23 | #include <linux/pci.h> |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 24 | #include <linux/irq.h> |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 25 | #include <linux/log2.h> |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 27 | #include <linux/moduleparam.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 29 | |
| 30 | #include "xhci.h" |
| 31 | |
| 32 | #define DRIVER_AUTHOR "Sarah Sharp" |
| 33 | #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver" |
| 34 | |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 35 | /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */ |
| 36 | static int link_quirk; |
| 37 | module_param(link_quirk, int, S_IRUGO | S_IWUSR); |
| 38 | MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB"); |
| 39 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 40 | /* 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 | */ |
| 54 | static 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 Sharp | 4f0f0ba | 2009-10-27 10:56:33 -0700 | [diff] [blame] | 73 | * Disable interrupts and begin the xHCI halting process. |
| 74 | */ |
| 75 | void 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 Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 92 | * Force HC into halt state. |
| 93 | * |
| 94 | * Disable any IRQs and clear the run/stop bit. |
| 95 | * HC will complete any current and actively pipelined transactions, and |
| 96 | * should halt within 16 microframes of the run/stop bit being cleared. |
| 97 | * Read HC Halted bit in the status register to see when the HC is finished. |
| 98 | * XXX: shouldn't we set HC_STATE_HALT here somewhere? |
| 99 | */ |
| 100 | int xhci_halt(struct xhci_hcd *xhci) |
| 101 | { |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 102 | xhci_dbg(xhci, "// Halt the HC\n"); |
Sarah Sharp | 4f0f0ba | 2009-10-27 10:56:33 -0700 | [diff] [blame] | 103 | xhci_quiesce(xhci); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 104 | |
| 105 | return handshake(xhci, &xhci->op_regs->status, |
| 106 | STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); |
| 107 | } |
| 108 | |
| 109 | /* |
Sarah Sharp | ed07453 | 2010-05-24 13:25:21 -0700 | [diff] [blame] | 110 | * Set the run bit and wait for the host to be running. |
| 111 | */ |
| 112 | int xhci_start(struct xhci_hcd *xhci) |
| 113 | { |
| 114 | u32 temp; |
| 115 | int ret; |
| 116 | |
| 117 | temp = xhci_readl(xhci, &xhci->op_regs->command); |
| 118 | temp |= (CMD_RUN); |
| 119 | xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n", |
| 120 | temp); |
| 121 | xhci_writel(xhci, temp, &xhci->op_regs->command); |
| 122 | |
| 123 | /* |
| 124 | * Wait for the HCHalted Status bit to be 0 to indicate the host is |
| 125 | * running. |
| 126 | */ |
| 127 | ret = handshake(xhci, &xhci->op_regs->status, |
| 128 | STS_HALT, 0, XHCI_MAX_HALT_USEC); |
| 129 | if (ret == -ETIMEDOUT) |
| 130 | xhci_err(xhci, "Host took too long to start, " |
| 131 | "waited %u microseconds.\n", |
| 132 | XHCI_MAX_HALT_USEC); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 137 | * Reset a halted HC, and set the internal HC state to HC_STATE_HALT. |
| 138 | * |
| 139 | * This resets pipelines, timers, counters, state machines, etc. |
| 140 | * Transactions will be terminated immediately, and operational registers |
| 141 | * will be set to their defaults. |
| 142 | */ |
| 143 | int xhci_reset(struct xhci_hcd *xhci) |
| 144 | { |
| 145 | u32 command; |
| 146 | u32 state; |
Sarah Sharp | 2d62f3e | 2010-05-24 13:25:15 -0700 | [diff] [blame] | 147 | int ret; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 148 | |
| 149 | state = xhci_readl(xhci, &xhci->op_regs->status); |
Sarah Sharp | d3512f6 | 2009-07-27 12:03:50 -0700 | [diff] [blame] | 150 | if ((state & STS_HALT) == 0) { |
| 151 | xhci_warn(xhci, "Host controller not halted, aborting reset.\n"); |
| 152 | return 0; |
| 153 | } |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 154 | |
| 155 | xhci_dbg(xhci, "// Reset the HC\n"); |
| 156 | command = xhci_readl(xhci, &xhci->op_regs->command); |
| 157 | command |= CMD_RESET; |
| 158 | xhci_writel(xhci, command, &xhci->op_regs->command); |
| 159 | /* XXX: Why does EHCI set this here? Shouldn't other code do this? */ |
| 160 | xhci_to_hcd(xhci)->state = HC_STATE_HALT; |
| 161 | |
Sarah Sharp | 2d62f3e | 2010-05-24 13:25:15 -0700 | [diff] [blame] | 162 | ret = handshake(xhci, &xhci->op_regs->command, |
| 163 | CMD_RESET, 0, 250 * 1000); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n"); |
| 168 | /* |
| 169 | * xHCI cannot write to any doorbells or operational registers other |
| 170 | * than status until the "Controller Not Ready" flag is cleared. |
| 171 | */ |
| 172 | return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 175 | static irqreturn_t xhci_msi_irq(int irq, struct usb_hcd *hcd) |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 176 | { |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 177 | irqreturn_t ret; |
| 178 | |
| 179 | set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags); |
| 180 | |
| 181 | ret = xhci_irq(hcd); |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Free IRQs |
| 188 | * free all IRQs request |
| 189 | */ |
| 190 | static void xhci_free_irq(struct xhci_hcd *xhci) |
| 191 | { |
| 192 | int i; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 193 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
| 194 | |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 195 | /* return if using legacy interrupt */ |
| 196 | if (xhci_to_hcd(xhci)->irq >= 0) |
| 197 | return; |
| 198 | |
| 199 | if (xhci->msix_entries) { |
| 200 | for (i = 0; i < xhci->msix_count; i++) |
| 201 | if (xhci->msix_entries[i].vector) |
| 202 | free_irq(xhci->msix_entries[i].vector, |
| 203 | xhci_to_hcd(xhci)); |
| 204 | } else if (pdev->irq >= 0) |
| 205 | free_irq(pdev->irq, xhci_to_hcd(xhci)); |
| 206 | |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Set up MSI |
| 212 | */ |
| 213 | static int xhci_setup_msi(struct xhci_hcd *xhci) |
| 214 | { |
| 215 | int ret; |
| 216 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
| 217 | |
| 218 | ret = pci_enable_msi(pdev); |
| 219 | if (ret) { |
| 220 | xhci_err(xhci, "failed to allocate MSI entry\n"); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq, |
| 225 | 0, "xhci_hcd", xhci_to_hcd(xhci)); |
| 226 | if (ret) { |
| 227 | xhci_err(xhci, "disable MSI interrupt\n"); |
| 228 | pci_disable_msi(pdev); |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Set up MSI-X |
| 236 | */ |
| 237 | static int xhci_setup_msix(struct xhci_hcd *xhci) |
| 238 | { |
| 239 | int i, ret = 0; |
| 240 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
| 241 | |
| 242 | /* |
| 243 | * calculate number of msi-x vectors supported. |
| 244 | * - HCS_MAX_INTRS: the max number of interrupts the host can handle, |
| 245 | * with max number of interrupters based on the xhci HCSPARAMS1. |
| 246 | * - num_online_cpus: maximum msi-x vectors per CPUs core. |
| 247 | * Add additional 1 vector to ensure always available interrupt. |
| 248 | */ |
| 249 | xhci->msix_count = min(num_online_cpus() + 1, |
| 250 | HCS_MAX_INTRS(xhci->hcs_params1)); |
| 251 | |
| 252 | xhci->msix_entries = |
| 253 | kmalloc((sizeof(struct msix_entry))*xhci->msix_count, |
| 254 | GFP_KERNEL); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 255 | if (!xhci->msix_entries) { |
| 256 | xhci_err(xhci, "Failed to allocate MSI-X entries\n"); |
| 257 | return -ENOMEM; |
| 258 | } |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 259 | |
| 260 | for (i = 0; i < xhci->msix_count; i++) { |
| 261 | xhci->msix_entries[i].entry = i; |
| 262 | xhci->msix_entries[i].vector = 0; |
| 263 | } |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 264 | |
| 265 | ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count); |
| 266 | if (ret) { |
| 267 | xhci_err(xhci, "Failed to enable MSI-X\n"); |
| 268 | goto free_entries; |
| 269 | } |
| 270 | |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 271 | for (i = 0; i < xhci->msix_count; i++) { |
| 272 | ret = request_irq(xhci->msix_entries[i].vector, |
| 273 | (irq_handler_t)xhci_msi_irq, |
| 274 | 0, "xhci_hcd", xhci_to_hcd(xhci)); |
| 275 | if (ret) |
| 276 | goto disable_msix; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 277 | } |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 278 | |
| 279 | return ret; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 280 | |
| 281 | disable_msix: |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 282 | xhci_err(xhci, "disable MSI-X interrupt\n"); |
| 283 | xhci_free_irq(xhci); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 284 | pci_disable_msix(pdev); |
| 285 | free_entries: |
| 286 | kfree(xhci->msix_entries); |
| 287 | xhci->msix_entries = NULL; |
| 288 | return ret; |
| 289 | } |
| 290 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 291 | /* Free any IRQs and disable MSI-X */ |
| 292 | static void xhci_cleanup_msix(struct xhci_hcd *xhci) |
| 293 | { |
| 294 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 295 | |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 296 | xhci_free_irq(xhci); |
| 297 | |
| 298 | if (xhci->msix_entries) { |
| 299 | pci_disable_msix(pdev); |
| 300 | kfree(xhci->msix_entries); |
| 301 | xhci->msix_entries = NULL; |
| 302 | } else { |
| 303 | pci_disable_msi(pdev); |
| 304 | } |
| 305 | |
| 306 | return; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 307 | } |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * Initialize memory for HCD and xHC (one-time init). |
| 311 | * |
| 312 | * Program the PAGESIZE register, initialize the device context array, create |
| 313 | * device contexts (?), set up a command ring segment (or two?), create event |
| 314 | * ring (one for now). |
| 315 | */ |
| 316 | int xhci_init(struct usb_hcd *hcd) |
| 317 | { |
| 318 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 319 | int retval = 0; |
| 320 | |
| 321 | xhci_dbg(xhci, "xhci_init\n"); |
| 322 | spin_lock_init(&xhci->lock); |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 323 | if (link_quirk) { |
| 324 | xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n"); |
| 325 | xhci->quirks |= XHCI_LINK_TRB_QUIRK; |
| 326 | } else { |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 327 | xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n"); |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 328 | } |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 329 | retval = xhci_mem_init(xhci, GFP_KERNEL); |
| 330 | xhci_dbg(xhci, "Finished xhci_init\n"); |
| 331 | |
| 332 | return retval; |
| 333 | } |
| 334 | |
| 335 | /* |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 336 | * Called in interrupt context when there might be work |
| 337 | * queued on the event ring |
| 338 | * |
| 339 | * xhci->lock must be held by caller. |
| 340 | */ |
| 341 | static void xhci_work(struct xhci_hcd *xhci) |
| 342 | { |
| 343 | u32 temp; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 344 | u64 temp_64; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 345 | |
| 346 | /* |
| 347 | * Clear the op reg interrupt status first, |
| 348 | * so we can receive interrupts from other MSI-X interrupters. |
| 349 | * Write 1 to clear the interrupt status. |
| 350 | */ |
| 351 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 352 | temp |= STS_EINT; |
| 353 | xhci_writel(xhci, temp, &xhci->op_regs->status); |
| 354 | /* FIXME when MSI-X is supported and there are multiple vectors */ |
| 355 | /* Clear the MSI-X event interrupt status */ |
| 356 | |
| 357 | /* Acknowledge the interrupt */ |
| 358 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 359 | temp |= 0x3; |
| 360 | xhci_writel(xhci, temp, &xhci->ir_set->irq_pending); |
| 361 | /* Flush posted writes */ |
| 362 | xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 363 | |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 364 | if (xhci->xhc_state & XHCI_STATE_DYING) |
| 365 | xhci_dbg(xhci, "xHCI dying, ignoring interrupt. " |
| 366 | "Shouldn't IRQs be disabled?\n"); |
| 367 | else |
| 368 | /* FIXME this should be a delayed service routine |
| 369 | * that clears the EHB. |
| 370 | */ |
| 371 | xhci_handle_event(xhci); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 372 | |
Sarah Sharp | 2d83109 | 2009-07-27 12:03:40 -0700 | [diff] [blame] | 373 | /* Clear the event handler busy flag (RW1C); the event ring should be empty. */ |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 374 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
Sarah Sharp | 2d83109 | 2009-07-27 12:03:40 -0700 | [diff] [blame] | 375 | xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 376 | /* Flush posted writes -- FIXME is this necessary? */ |
| 377 | xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 378 | } |
| 379 | |
| 380 | /*-------------------------------------------------------------------------*/ |
| 381 | |
| 382 | /* |
| 383 | * xHCI spec says we can get an interrupt, and if the HC has an error condition, |
| 384 | * we might get bad data out of the event ring. Section 4.10.2.7 has a list of |
| 385 | * indicators of an event TRB error, but we check the status *first* to be safe. |
| 386 | */ |
| 387 | irqreturn_t xhci_irq(struct usb_hcd *hcd) |
| 388 | { |
| 389 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 390 | u32 temp, temp2; |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 391 | union xhci_trb *trb; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 392 | |
| 393 | spin_lock(&xhci->lock); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 394 | trb = xhci->event_ring->dequeue; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 395 | /* Check if the xHC generated the interrupt, or the irq is shared */ |
| 396 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 397 | temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
Sarah Sharp | fcf8f57 | 2009-07-27 12:04:01 -0700 | [diff] [blame] | 398 | if (temp == 0xffffffff && temp2 == 0xffffffff) |
| 399 | goto hw_died; |
| 400 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 401 | if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) { |
| 402 | spin_unlock(&xhci->lock); |
| 403 | return IRQ_NONE; |
| 404 | } |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 405 | xhci_dbg(xhci, "op reg status = %08x\n", temp); |
| 406 | xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2); |
| 407 | xhci_dbg(xhci, "Event ring dequeue ptr:\n"); |
| 408 | xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n", |
| 409 | (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb), |
| 410 | lower_32_bits(trb->link.segment_ptr), |
| 411 | upper_32_bits(trb->link.segment_ptr), |
| 412 | (unsigned int) trb->link.intr_target, |
| 413 | (unsigned int) trb->link.control); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 414 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 415 | if (temp & STS_FATAL) { |
| 416 | xhci_warn(xhci, "WARNING: Host System Error\n"); |
| 417 | xhci_halt(xhci); |
Sarah Sharp | fcf8f57 | 2009-07-27 12:04:01 -0700 | [diff] [blame] | 418 | hw_died: |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 419 | xhci_to_hcd(xhci)->state = HC_STATE_HALT; |
Sarah Sharp | c96a2b8 | 2009-04-29 19:05:40 -0700 | [diff] [blame] | 420 | spin_unlock(&xhci->lock); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 421 | return -ESHUTDOWN; |
| 422 | } |
| 423 | |
| 424 | xhci_work(xhci); |
| 425 | spin_unlock(&xhci->lock); |
| 426 | |
| 427 | return IRQ_HANDLED; |
| 428 | } |
| 429 | |
| 430 | #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 431 | void xhci_event_ring_work(unsigned long arg) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 432 | { |
| 433 | unsigned long flags; |
| 434 | int temp; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 435 | u64 temp_64; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 436 | struct xhci_hcd *xhci = (struct xhci_hcd *) arg; |
| 437 | int i, j; |
| 438 | |
| 439 | xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies); |
| 440 | |
| 441 | spin_lock_irqsave(&xhci->lock, flags); |
| 442 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 443 | xhci_dbg(xhci, "op reg status = 0x%x\n", temp); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 444 | if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) { |
Sarah Sharp | e4ab05d | 2009-09-16 16:42:30 -0700 | [diff] [blame] | 445 | xhci_dbg(xhci, "HW died, polling stopped.\n"); |
| 446 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 447 | return; |
| 448 | } |
| 449 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 450 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 451 | xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp); |
| 452 | xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled); |
| 453 | xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask); |
| 454 | xhci->error_bitmask = 0; |
| 455 | xhci_dbg(xhci, "Event ring:\n"); |
| 456 | xhci_debug_segment(xhci, xhci->event_ring->deq_seg); |
| 457 | xhci_dbg_ring_ptrs(xhci, xhci->event_ring); |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 458 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
| 459 | temp_64 &= ~ERST_PTR_MASK; |
| 460 | xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 461 | xhci_dbg(xhci, "Command ring:\n"); |
| 462 | xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg); |
| 463 | xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring); |
| 464 | xhci_dbg_cmd_ptrs(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 465 | for (i = 0; i < MAX_HC_SLOTS; ++i) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 466 | if (!xhci->devs[i]) |
| 467 | continue; |
| 468 | for (j = 0; j < 31; ++j) { |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 469 | xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 470 | } |
| 471 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 472 | |
| 473 | if (xhci->noops_submitted != NUM_TEST_NOOPS) |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 474 | if (xhci_setup_one_noop(xhci)) |
| 475 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 476 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 477 | |
| 478 | if (!xhci->zombie) |
| 479 | mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ); |
| 480 | else |
| 481 | xhci_dbg(xhci, "Quit polling the event ring.\n"); |
| 482 | } |
| 483 | #endif |
| 484 | |
| 485 | /* |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 486 | * Start the HC after it was halted. |
| 487 | * |
| 488 | * This function is called by the USB core when the HC driver is added. |
| 489 | * Its opposite is xhci_stop(). |
| 490 | * |
| 491 | * xhci_init() must be called once before this function can be called. |
| 492 | * Reset the HC, enable device slot contexts, program DCBAAP, and |
| 493 | * set command ring pointer and event ring pointer. |
| 494 | * |
| 495 | * Setup MSI-X vectors and enable interrupts. |
| 496 | */ |
| 497 | int xhci_run(struct usb_hcd *hcd) |
| 498 | { |
| 499 | u32 temp; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 500 | u64 temp_64; |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 501 | u32 ret; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 502 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 503 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 504 | void (*doorbell)(struct xhci_hcd *) = NULL; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 505 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 506 | hcd->uses_new_polling = 1; |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 507 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 508 | xhci_dbg(xhci, "xhci_run\n"); |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 509 | /* unregister the legacy interrupt */ |
| 510 | if (hcd->irq) |
| 511 | free_irq(hcd->irq, hcd); |
| 512 | hcd->irq = -1; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 513 | |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 514 | ret = xhci_setup_msix(xhci); |
| 515 | if (ret) |
| 516 | /* fall back to msi*/ |
| 517 | ret = xhci_setup_msi(xhci); |
| 518 | |
| 519 | if (ret) { |
| 520 | /* fall back to legacy interrupt*/ |
| 521 | ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED, |
| 522 | hcd->irq_descr, hcd); |
| 523 | if (ret) { |
| 524 | xhci_err(xhci, "request interrupt %d failed\n", |
| 525 | pdev->irq); |
| 526 | return ret; |
| 527 | } |
| 528 | hcd->irq = pdev->irq; |
| 529 | } |
| 530 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 531 | #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING |
| 532 | init_timer(&xhci->event_ring_timer); |
| 533 | xhci->event_ring_timer.data = (unsigned long) xhci; |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 534 | xhci->event_ring_timer.function = xhci_event_ring_work; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 535 | /* Poll the event ring */ |
| 536 | xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ; |
| 537 | xhci->zombie = 0; |
| 538 | xhci_dbg(xhci, "Setting event ring polling timer\n"); |
| 539 | add_timer(&xhci->event_ring_timer); |
| 540 | #endif |
| 541 | |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 542 | xhci_dbg(xhci, "Command ring memory map follows:\n"); |
| 543 | xhci_debug_ring(xhci, xhci->cmd_ring); |
| 544 | xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring); |
| 545 | xhci_dbg_cmd_ptrs(xhci); |
| 546 | |
| 547 | xhci_dbg(xhci, "ERST memory map follows:\n"); |
| 548 | xhci_dbg_erst(xhci, &xhci->erst); |
| 549 | xhci_dbg(xhci, "Event ring:\n"); |
| 550 | xhci_debug_ring(xhci, xhci->event_ring); |
| 551 | xhci_dbg_ring_ptrs(xhci, xhci->event_ring); |
| 552 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
| 553 | temp_64 &= ~ERST_PTR_MASK; |
| 554 | xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64); |
| 555 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 556 | xhci_dbg(xhci, "// Set the interrupt modulation register\n"); |
| 557 | temp = xhci_readl(xhci, &xhci->ir_set->irq_control); |
Sarah Sharp | a4d8830 | 2009-05-14 11:44:26 -0700 | [diff] [blame] | 558 | temp &= ~ER_IRQ_INTERVAL_MASK; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 559 | temp |= (u32) 160; |
| 560 | xhci_writel(xhci, temp, &xhci->ir_set->irq_control); |
| 561 | |
| 562 | /* Set the HCD state before we enable the irqs */ |
| 563 | hcd->state = HC_STATE_RUNNING; |
| 564 | temp = xhci_readl(xhci, &xhci->op_regs->command); |
| 565 | temp |= (CMD_EIE); |
| 566 | xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n", |
| 567 | temp); |
| 568 | xhci_writel(xhci, temp, &xhci->op_regs->command); |
| 569 | |
| 570 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 571 | xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n", |
| 572 | xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp)); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 573 | xhci_writel(xhci, ER_IRQ_ENABLE(temp), |
| 574 | &xhci->ir_set->irq_pending); |
| 575 | xhci_print_ir_set(xhci, xhci->ir_set, 0); |
| 576 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 577 | if (NUM_TEST_NOOPS > 0) |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 578 | doorbell = xhci_setup_one_noop(xhci); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 579 | if (xhci->quirks & XHCI_NEC_HOST) |
| 580 | xhci_queue_vendor_command(xhci, 0, 0, 0, |
| 581 | TRB_TYPE(TRB_NEC_GET_FW)); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 582 | |
Sarah Sharp | ed07453 | 2010-05-24 13:25:21 -0700 | [diff] [blame] | 583 | if (xhci_start(xhci)) { |
| 584 | xhci_halt(xhci); |
| 585 | return -ENODEV; |
| 586 | } |
| 587 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 588 | if (doorbell) |
| 589 | (*doorbell)(xhci); |
Sarah Sharp | 0238634 | 2010-05-24 13:25:28 -0700 | [diff] [blame] | 590 | if (xhci->quirks & XHCI_NEC_HOST) |
| 591 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 592 | |
| 593 | xhci_dbg(xhci, "Finished xhci_run\n"); |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | /* |
| 598 | * Stop xHCI driver. |
| 599 | * |
| 600 | * This function is called by the USB core when the HC driver is removed. |
| 601 | * Its opposite is xhci_run(). |
| 602 | * |
| 603 | * Disable device contexts, disable IRQs, and quiesce the HC. |
| 604 | * Reset the HC, finish any completed transactions, and cleanup memory. |
| 605 | */ |
| 606 | void xhci_stop(struct usb_hcd *hcd) |
| 607 | { |
| 608 | u32 temp; |
| 609 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 610 | |
| 611 | spin_lock_irq(&xhci->lock); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 612 | xhci_halt(xhci); |
| 613 | xhci_reset(xhci); |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 614 | xhci_cleanup_msix(xhci); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 615 | spin_unlock_irq(&xhci->lock); |
| 616 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 617 | #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING |
| 618 | /* Tell the event ring poll function not to reschedule */ |
| 619 | xhci->zombie = 1; |
| 620 | del_timer_sync(&xhci->event_ring_timer); |
| 621 | #endif |
| 622 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 623 | xhci_dbg(xhci, "// Disabling event ring interrupts\n"); |
| 624 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 625 | xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status); |
| 626 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 627 | xhci_writel(xhci, ER_IRQ_DISABLE(temp), |
| 628 | &xhci->ir_set->irq_pending); |
| 629 | xhci_print_ir_set(xhci, xhci->ir_set, 0); |
| 630 | |
| 631 | xhci_dbg(xhci, "cleaning up memory\n"); |
| 632 | xhci_mem_cleanup(xhci); |
| 633 | xhci_dbg(xhci, "xhci_stop completed - status = %x\n", |
| 634 | xhci_readl(xhci, &xhci->op_regs->status)); |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Shutdown HC (not bus-specific) |
| 639 | * |
| 640 | * This is called when the machine is rebooting or halting. We assume that the |
| 641 | * machine will be powered off, and the HC's internal state will be reset. |
| 642 | * Don't bother to free memory. |
| 643 | */ |
| 644 | void xhci_shutdown(struct usb_hcd *hcd) |
| 645 | { |
| 646 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 647 | |
| 648 | spin_lock_irq(&xhci->lock); |
| 649 | xhci_halt(xhci); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 650 | xhci_cleanup_msix(xhci); |
Dong Nguyen | 43b86af | 2010-07-21 16:56:08 -0700 | [diff] [blame] | 651 | spin_unlock_irq(&xhci->lock); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 652 | |
| 653 | xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n", |
| 654 | xhci_readl(xhci, &xhci->op_regs->status)); |
| 655 | } |
| 656 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 657 | /*-------------------------------------------------------------------------*/ |
| 658 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 659 | /** |
| 660 | * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and |
| 661 | * HCDs. Find the index for an endpoint given its descriptor. Use the return |
| 662 | * value to right shift 1 for the bitmask. |
| 663 | * |
| 664 | * Index = (epnum * 2) + direction - 1, |
| 665 | * where direction = 0 for OUT, 1 for IN. |
| 666 | * For control endpoints, the IN index is used (OUT index is unused), so |
| 667 | * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) |
| 668 | */ |
| 669 | unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc) |
| 670 | { |
| 671 | unsigned int index; |
| 672 | if (usb_endpoint_xfer_control(desc)) |
| 673 | index = (unsigned int) (usb_endpoint_num(desc)*2); |
| 674 | else |
| 675 | index = (unsigned int) (usb_endpoint_num(desc)*2) + |
| 676 | (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; |
| 677 | return index; |
| 678 | } |
| 679 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 680 | /* Find the flag for this endpoint (for use in the control context). Use the |
| 681 | * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is |
| 682 | * bit 1, etc. |
| 683 | */ |
| 684 | unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc) |
| 685 | { |
| 686 | return 1 << (xhci_get_endpoint_index(desc) + 1); |
| 687 | } |
| 688 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 689 | /* Find the flag for this endpoint (for use in the control context). Use the |
| 690 | * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is |
| 691 | * bit 1, etc. |
| 692 | */ |
| 693 | unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index) |
| 694 | { |
| 695 | return 1 << (ep_index + 1); |
| 696 | } |
| 697 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 698 | /* Compute the last valid endpoint context index. Basically, this is the |
| 699 | * endpoint index plus one. For slot contexts with more than valid endpoint, |
| 700 | * we find the most significant bit set in the added contexts flags. |
| 701 | * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000 |
| 702 | * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one. |
| 703 | */ |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 704 | unsigned int xhci_last_valid_endpoint(u32 added_ctxs) |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 705 | { |
| 706 | return fls(added_ctxs) - 1; |
| 707 | } |
| 708 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 709 | /* Returns 1 if the arguments are OK; |
| 710 | * returns 0 this is a root hub; returns -EINVAL for NULL pointers. |
| 711 | */ |
| 712 | int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev, |
| 713 | struct usb_host_endpoint *ep, int check_ep, const char *func) { |
| 714 | if (!hcd || (check_ep && !ep) || !udev) { |
| 715 | printk(KERN_DEBUG "xHCI %s called with invalid args\n", |
| 716 | func); |
| 717 | return -EINVAL; |
| 718 | } |
| 719 | if (!udev->parent) { |
| 720 | printk(KERN_DEBUG "xHCI %s called for root hub\n", |
| 721 | func); |
| 722 | return 0; |
| 723 | } |
| 724 | if (!udev->slot_id) { |
| 725 | printk(KERN_DEBUG "xHCI %s called with unaddressed device\n", |
| 726 | func); |
| 727 | return -EINVAL; |
| 728 | } |
| 729 | return 1; |
| 730 | } |
| 731 | |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 732 | static int xhci_configure_endpoint(struct xhci_hcd *xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 733 | struct usb_device *udev, struct xhci_command *command, |
| 734 | bool ctx_change, bool must_succeed); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 735 | |
| 736 | /* |
| 737 | * Full speed devices may have a max packet size greater than 8 bytes, but the |
| 738 | * USB core doesn't know that until it reads the first 8 bytes of the |
| 739 | * descriptor. If the usb_device's max packet size changes after that point, |
| 740 | * we need to issue an evaluate context command and wait on it. |
| 741 | */ |
| 742 | static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, |
| 743 | unsigned int ep_index, struct urb *urb) |
| 744 | { |
| 745 | struct xhci_container_ctx *in_ctx; |
| 746 | struct xhci_container_ctx *out_ctx; |
| 747 | struct xhci_input_control_ctx *ctrl_ctx; |
| 748 | struct xhci_ep_ctx *ep_ctx; |
| 749 | int max_packet_size; |
| 750 | int hw_max_packet_size; |
| 751 | int ret = 0; |
| 752 | |
| 753 | out_ctx = xhci->devs[slot_id]->out_ctx; |
| 754 | ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); |
| 755 | hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2); |
| 756 | max_packet_size = urb->dev->ep0.desc.wMaxPacketSize; |
| 757 | if (hw_max_packet_size != max_packet_size) { |
| 758 | xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n"); |
| 759 | xhci_dbg(xhci, "Max packet size in usb_device = %d\n", |
| 760 | max_packet_size); |
| 761 | xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n", |
| 762 | hw_max_packet_size); |
| 763 | xhci_dbg(xhci, "Issuing evaluate context command.\n"); |
| 764 | |
| 765 | /* Set up the modified control endpoint 0 */ |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 766 | xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, |
| 767 | xhci->devs[slot_id]->out_ctx, ep_index); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 768 | in_ctx = xhci->devs[slot_id]->in_ctx; |
| 769 | ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); |
| 770 | ep_ctx->ep_info2 &= ~MAX_PACKET_MASK; |
| 771 | ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size); |
| 772 | |
| 773 | /* Set up the input context flags for the command */ |
| 774 | /* FIXME: This won't work if a non-default control endpoint |
| 775 | * changes max packet sizes. |
| 776 | */ |
| 777 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
| 778 | ctrl_ctx->add_flags = EP0_FLAG; |
| 779 | ctrl_ctx->drop_flags = 0; |
| 780 | |
| 781 | xhci_dbg(xhci, "Slot %d input context\n", slot_id); |
| 782 | xhci_dbg_ctx(xhci, in_ctx, ep_index); |
| 783 | xhci_dbg(xhci, "Slot %d output context\n", slot_id); |
| 784 | xhci_dbg_ctx(xhci, out_ctx, ep_index); |
| 785 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 786 | ret = xhci_configure_endpoint(xhci, urb->dev, NULL, |
| 787 | true, false); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 788 | |
| 789 | /* Clean up the input context for later use by bandwidth |
| 790 | * functions. |
| 791 | */ |
| 792 | ctrl_ctx->add_flags = SLOT_FLAG; |
| 793 | } |
| 794 | return ret; |
| 795 | } |
| 796 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 797 | /* |
| 798 | * non-error returns are a promise to giveback() the urb later |
| 799 | * we drop ownership so next owner (or urb unlink) can get it |
| 800 | */ |
| 801 | int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) |
| 802 | { |
| 803 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 804 | unsigned long flags; |
| 805 | int ret = 0; |
| 806 | unsigned int slot_id, ep_index; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 807 | struct urb_priv *urb_priv; |
| 808 | int size, i; |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 809 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 810 | if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0) |
| 811 | return -EINVAL; |
| 812 | |
| 813 | slot_id = urb->dev->slot_id; |
| 814 | ep_index = xhci_get_endpoint_index(&urb->ep->desc); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 815 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 816 | if (!xhci->devs || !xhci->devs[slot_id]) { |
| 817 | if (!in_interrupt()) |
| 818 | dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n"); |
Sarah Sharp | c7959fb | 2009-04-29 19:06:36 -0700 | [diff] [blame] | 819 | ret = -EINVAL; |
| 820 | goto exit; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 821 | } |
Alan Stern | 541c7d4 | 2010-06-22 16:39:10 -0400 | [diff] [blame] | 822 | if (!HCD_HW_ACCESSIBLE(hcd)) { |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 823 | if (!in_interrupt()) |
| 824 | xhci_dbg(xhci, "urb submitted during PCI suspend\n"); |
| 825 | ret = -ESHUTDOWN; |
| 826 | goto exit; |
| 827 | } |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 828 | |
| 829 | if (usb_endpoint_xfer_isoc(&urb->ep->desc)) |
| 830 | size = urb->number_of_packets; |
| 831 | else |
| 832 | size = 1; |
| 833 | |
| 834 | urb_priv = kzalloc(sizeof(struct urb_priv) + |
| 835 | size * sizeof(struct xhci_td *), mem_flags); |
| 836 | if (!urb_priv) |
| 837 | return -ENOMEM; |
| 838 | |
| 839 | for (i = 0; i < size; i++) { |
| 840 | urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags); |
| 841 | if (!urb_priv->td[i]) { |
| 842 | urb_priv->length = i; |
| 843 | xhci_urb_free_priv(xhci, urb_priv); |
| 844 | return -ENOMEM; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | urb_priv->length = size; |
| 849 | urb_priv->td_cnt = 0; |
| 850 | urb->hcpriv = urb_priv; |
| 851 | |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 852 | if (usb_endpoint_xfer_control(&urb->ep->desc)) { |
| 853 | /* Check to see if the max packet size for the default control |
| 854 | * endpoint changed during FS device enumeration |
| 855 | */ |
| 856 | if (urb->dev->speed == USB_SPEED_FULL) { |
| 857 | ret = xhci_check_maxpacket(xhci, slot_id, |
| 858 | ep_index, urb); |
| 859 | if (ret < 0) |
| 860 | return ret; |
| 861 | } |
| 862 | |
Sarah Sharp | b11069f | 2009-07-27 12:03:23 -0700 | [diff] [blame] | 863 | /* We have a spinlock and interrupts disabled, so we must pass |
| 864 | * atomic context to this function, which may allocate memory. |
| 865 | */ |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 866 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 867 | if (xhci->xhc_state & XHCI_STATE_DYING) |
| 868 | goto dying; |
Sarah Sharp | b11069f | 2009-07-27 12:03:23 -0700 | [diff] [blame] | 869 | ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 870 | slot_id, ep_index); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 871 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 872 | } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) { |
| 873 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 874 | if (xhci->xhc_state & XHCI_STATE_DYING) |
| 875 | goto dying; |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 876 | if (xhci->devs[slot_id]->eps[ep_index].ep_state & |
| 877 | EP_GETTING_STREAMS) { |
| 878 | xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep " |
| 879 | "is transitioning to using streams.\n"); |
| 880 | ret = -EINVAL; |
| 881 | } else if (xhci->devs[slot_id]->eps[ep_index].ep_state & |
| 882 | EP_GETTING_NO_STREAMS) { |
| 883 | xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep " |
| 884 | "is transitioning to " |
| 885 | "not having streams.\n"); |
| 886 | ret = -EINVAL; |
| 887 | } else { |
| 888 | ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, |
| 889 | slot_id, ep_index); |
| 890 | } |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 891 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 892 | } else if (usb_endpoint_xfer_int(&urb->ep->desc)) { |
| 893 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 894 | if (xhci->xhc_state & XHCI_STATE_DYING) |
| 895 | goto dying; |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 896 | ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, |
| 897 | slot_id, ep_index); |
| 898 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 899 | } else { |
Andiry Xu | 787f4e5 | 2010-07-22 15:23:52 -0700 | [diff] [blame] | 900 | spin_lock_irqsave(&xhci->lock, flags); |
| 901 | if (xhci->xhc_state & XHCI_STATE_DYING) |
| 902 | goto dying; |
| 903 | ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb, |
| 904 | slot_id, ep_index); |
| 905 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 906 | } |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 907 | exit: |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 908 | return ret; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 909 | dying: |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 910 | xhci_urb_free_priv(xhci, urb_priv); |
| 911 | urb->hcpriv = NULL; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 912 | xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for " |
| 913 | "non-responsive xHCI host.\n", |
| 914 | urb->ep->desc.bEndpointAddress, urb); |
| 915 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 916 | return -ESHUTDOWN; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 919 | /* |
| 920 | * Remove the URB's TD from the endpoint ring. This may cause the HC to stop |
| 921 | * USB transfers, potentially stopping in the middle of a TRB buffer. The HC |
| 922 | * should pick up where it left off in the TD, unless a Set Transfer Ring |
| 923 | * Dequeue Pointer is issued. |
| 924 | * |
| 925 | * The TRBs that make up the buffers for the canceled URB will be "removed" from |
| 926 | * the ring. Since the ring is a contiguous structure, they can't be physically |
| 927 | * removed. Instead, there are two options: |
| 928 | * |
| 929 | * 1) If the HC is in the middle of processing the URB to be canceled, we |
| 930 | * simply move the ring's dequeue pointer past those TRBs using the Set |
| 931 | * Transfer Ring Dequeue Pointer command. This will be the common case, |
| 932 | * when drivers timeout on the last submitted URB and attempt to cancel. |
| 933 | * |
| 934 | * 2) If the HC is in the middle of a different TD, we turn the TRBs into a |
| 935 | * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The |
| 936 | * HC will need to invalidate the any TRBs it has cached after the stop |
| 937 | * endpoint command, as noted in the xHCI 0.95 errata. |
| 938 | * |
| 939 | * 3) The TD may have completed by the time the Stop Endpoint Command |
| 940 | * completes, so software needs to handle that case too. |
| 941 | * |
| 942 | * This function should protect against the TD enqueueing code ringing the |
| 943 | * doorbell while this code is waiting for a Stop Endpoint command to complete. |
| 944 | * It also needs to account for multiple cancellations on happening at the same |
| 945 | * time for the same endpoint. |
| 946 | * |
| 947 | * Note that this function can be called in any context, or so says |
| 948 | * usb_hcd_unlink_urb() |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 949 | */ |
| 950 | int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 951 | { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 952 | unsigned long flags; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 953 | int ret, i; |
Sarah Sharp | e34b2fb | 2009-09-28 17:21:37 -0700 | [diff] [blame] | 954 | u32 temp; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 955 | struct xhci_hcd *xhci; |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 956 | struct urb_priv *urb_priv; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 957 | struct xhci_td *td; |
| 958 | unsigned int ep_index; |
| 959 | struct xhci_ring *ep_ring; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 960 | struct xhci_virt_ep *ep; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 961 | |
| 962 | xhci = hcd_to_xhci(hcd); |
| 963 | spin_lock_irqsave(&xhci->lock, flags); |
| 964 | /* Make sure the URB hasn't completed or been unlinked already */ |
| 965 | ret = usb_hcd_check_unlink_urb(hcd, urb, status); |
| 966 | if (ret || !urb->hcpriv) |
| 967 | goto done; |
Sarah Sharp | e34b2fb | 2009-09-28 17:21:37 -0700 | [diff] [blame] | 968 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 969 | if (temp == 0xffffffff) { |
| 970 | xhci_dbg(xhci, "HW died, freeing TD.\n"); |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 971 | urb_priv = urb->hcpriv; |
Sarah Sharp | e34b2fb | 2009-09-28 17:21:37 -0700 | [diff] [blame] | 972 | |
| 973 | usb_hcd_unlink_urb_from_ep(hcd, urb); |
| 974 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 975 | usb_hcd_giveback_urb(xhci_to_hcd(xhci), urb, -ESHUTDOWN); |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 976 | xhci_urb_free_priv(xhci, urb_priv); |
Sarah Sharp | e34b2fb | 2009-09-28 17:21:37 -0700 | [diff] [blame] | 977 | return ret; |
| 978 | } |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 979 | if (xhci->xhc_state & XHCI_STATE_DYING) { |
| 980 | xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on " |
| 981 | "non-responsive xHCI host.\n", |
| 982 | urb->ep->desc.bEndpointAddress, urb); |
| 983 | /* Let the stop endpoint command watchdog timer (which set this |
| 984 | * state) finish cleaning up the endpoint TD lists. We must |
| 985 | * have caught it in the middle of dropping a lock and giving |
| 986 | * back an URB. |
| 987 | */ |
| 988 | goto done; |
| 989 | } |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 990 | |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 991 | xhci_dbg(xhci, "Cancel URB %p\n", urb); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 992 | xhci_dbg(xhci, "Event ring:\n"); |
| 993 | xhci_debug_ring(xhci, xhci->event_ring); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 994 | ep_index = xhci_get_endpoint_index(&urb->ep->desc); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 995 | ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index]; |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 996 | ep_ring = xhci_urb_to_transfer_ring(xhci, urb); |
| 997 | if (!ep_ring) { |
| 998 | ret = -EINVAL; |
| 999 | goto done; |
| 1000 | } |
| 1001 | |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 1002 | xhci_dbg(xhci, "Endpoint ring:\n"); |
| 1003 | xhci_debug_ring(xhci, ep_ring); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1004 | |
Andiry Xu | 8e51adc | 2010-07-22 15:23:31 -0700 | [diff] [blame] | 1005 | urb_priv = urb->hcpriv; |
| 1006 | |
| 1007 | for (i = urb_priv->td_cnt; i < urb_priv->length; i++) { |
| 1008 | td = urb_priv->td[i]; |
| 1009 | list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); |
| 1010 | } |
| 1011 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1012 | /* Queue a stop endpoint command, but only if this is |
| 1013 | * the first cancellation to be handled. |
| 1014 | */ |
Sarah Sharp | 678539c | 2009-10-27 10:55:52 -0700 | [diff] [blame] | 1015 | if (!(ep->ep_state & EP_HALT_PENDING)) { |
| 1016 | ep->ep_state |= EP_HALT_PENDING; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 1017 | ep->stop_cmds_pending++; |
| 1018 | ep->stop_cmd_timer.expires = jiffies + |
| 1019 | XHCI_STOP_EP_CMD_TIMEOUT * HZ; |
| 1020 | add_timer(&ep->stop_cmd_timer); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1021 | xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index); |
| 1022 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 1023 | } |
| 1024 | done: |
| 1025 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1026 | return ret; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 1027 | } |
| 1028 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1029 | /* Drop an endpoint from a new bandwidth configuration for this device. |
| 1030 | * Only one call to this function is allowed per endpoint before |
| 1031 | * check_bandwidth() or reset_bandwidth() must be called. |
| 1032 | * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will |
| 1033 | * add the endpoint to the schedule with possibly new parameters denoted by a |
| 1034 | * different endpoint descriptor in usb_host_endpoint. |
| 1035 | * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is |
| 1036 | * not allowed. |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1037 | * |
| 1038 | * The USB core will not allow URBs to be queued to an endpoint that is being |
| 1039 | * disabled, so there's no need for mutual exclusion to protect |
| 1040 | * the xhci->devs[slot_id] structure. |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1041 | */ |
| 1042 | int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, |
| 1043 | struct usb_host_endpoint *ep) |
| 1044 | { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1045 | struct xhci_hcd *xhci; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1046 | struct xhci_container_ctx *in_ctx, *out_ctx; |
| 1047 | struct xhci_input_control_ctx *ctrl_ctx; |
| 1048 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1049 | unsigned int last_ctx; |
| 1050 | unsigned int ep_index; |
| 1051 | struct xhci_ep_ctx *ep_ctx; |
| 1052 | u32 drop_flag; |
| 1053 | u32 new_add_flags, new_drop_flags, new_slot_info; |
| 1054 | int ret; |
| 1055 | |
| 1056 | ret = xhci_check_args(hcd, udev, ep, 1, __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1057 | if (ret <= 0) |
| 1058 | return ret; |
| 1059 | xhci = hcd_to_xhci(hcd); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1060 | xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1061 | |
| 1062 | drop_flag = xhci_get_endpoint_flag(&ep->desc); |
| 1063 | if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) { |
| 1064 | xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", |
| 1065 | __func__, drop_flag); |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1069 | if (!xhci->devs || !xhci->devs[udev->slot_id]) { |
| 1070 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 1071 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1072 | return -EINVAL; |
| 1073 | } |
| 1074 | |
| 1075 | in_ctx = xhci->devs[udev->slot_id]->in_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1076 | out_ctx = xhci->devs[udev->slot_id]->out_ctx; |
| 1077 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1078 | ep_index = xhci_get_endpoint_index(&ep->desc); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1079 | ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1080 | /* If the HC already knows the endpoint is disabled, |
| 1081 | * or the HCD has noted it is disabled, ignore this request |
| 1082 | */ |
| 1083 | if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED || |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1084 | ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) { |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1085 | xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", |
| 1086 | __func__, ep); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1090 | ctrl_ctx->drop_flags |= drop_flag; |
| 1091 | new_drop_flags = ctrl_ctx->drop_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1092 | |
Sarah Sharp | 0a023c6 | 2009-09-18 08:55:12 -0700 | [diff] [blame] | 1093 | ctrl_ctx->add_flags &= ~drop_flag; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1094 | new_add_flags = ctrl_ctx->add_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1095 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1096 | last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags); |
| 1097 | slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1098 | /* Update the last valid endpoint context, if we deleted the last one */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1099 | if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) { |
| 1100 | slot_ctx->dev_info &= ~LAST_CTX_MASK; |
| 1101 | slot_ctx->dev_info |= LAST_CTX(last_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1102 | } |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1103 | new_slot_info = slot_ctx->dev_info; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1104 | |
| 1105 | xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); |
| 1106 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1107 | xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n", |
| 1108 | (unsigned int) ep->desc.bEndpointAddress, |
| 1109 | udev->slot_id, |
| 1110 | (unsigned int) new_drop_flags, |
| 1111 | (unsigned int) new_add_flags, |
| 1112 | (unsigned int) new_slot_info); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /* Add an endpoint to a new possible bandwidth configuration for this device. |
| 1117 | * Only one call to this function is allowed per endpoint before |
| 1118 | * check_bandwidth() or reset_bandwidth() must be called. |
| 1119 | * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will |
| 1120 | * add the endpoint to the schedule with possibly new parameters denoted by a |
| 1121 | * different endpoint descriptor in usb_host_endpoint. |
| 1122 | * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is |
| 1123 | * not allowed. |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1124 | * |
| 1125 | * The USB core will not allow URBs to be queued to an endpoint until the |
| 1126 | * configuration or alt setting is installed in the device, so there's no need |
| 1127 | * for mutual exclusion to protect the xhci->devs[slot_id] structure. |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1128 | */ |
| 1129 | int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, |
| 1130 | struct usb_host_endpoint *ep) |
| 1131 | { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1132 | struct xhci_hcd *xhci; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1133 | struct xhci_container_ctx *in_ctx, *out_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1134 | unsigned int ep_index; |
| 1135 | struct xhci_ep_ctx *ep_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1136 | struct xhci_slot_ctx *slot_ctx; |
| 1137 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1138 | u32 added_ctxs; |
| 1139 | unsigned int last_ctx; |
| 1140 | u32 new_add_flags, new_drop_flags, new_slot_info; |
| 1141 | int ret = 0; |
| 1142 | |
| 1143 | ret = xhci_check_args(hcd, udev, ep, 1, __func__); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1144 | if (ret <= 0) { |
| 1145 | /* So we won't queue a reset ep command for a root hub */ |
| 1146 | ep->hcpriv = NULL; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1147 | return ret; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1148 | } |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1149 | xhci = hcd_to_xhci(hcd); |
| 1150 | |
| 1151 | added_ctxs = xhci_get_endpoint_flag(&ep->desc); |
| 1152 | last_ctx = xhci_last_valid_endpoint(added_ctxs); |
| 1153 | if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) { |
| 1154 | /* FIXME when we have to issue an evaluate endpoint command to |
| 1155 | * deal with ep0 max packet size changing once we get the |
| 1156 | * descriptors |
| 1157 | */ |
| 1158 | xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", |
| 1159 | __func__, added_ctxs); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1163 | if (!xhci->devs || !xhci->devs[udev->slot_id]) { |
| 1164 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 1165 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1166 | return -EINVAL; |
| 1167 | } |
| 1168 | |
| 1169 | in_ctx = xhci->devs[udev->slot_id]->in_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1170 | out_ctx = xhci->devs[udev->slot_id]->out_ctx; |
| 1171 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1172 | ep_index = xhci_get_endpoint_index(&ep->desc); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1173 | ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1174 | /* If the HCD has already noted the endpoint is enabled, |
| 1175 | * ignore this request. |
| 1176 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1177 | if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) { |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1178 | xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", |
| 1179 | __func__, ep); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
| 1182 | |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1183 | /* |
| 1184 | * Configuration and alternate setting changes must be done in |
| 1185 | * process context, not interrupt context (or so documenation |
| 1186 | * for usb_set_interface() and usb_set_configuration() claim). |
| 1187 | */ |
| 1188 | if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id], |
Oliver Neukum | 319c3ea | 2009-12-16 19:43:59 +0100 | [diff] [blame] | 1189 | udev, ep, GFP_NOIO) < 0) { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1190 | dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", |
| 1191 | __func__, ep->desc.bEndpointAddress); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1192 | return -ENOMEM; |
| 1193 | } |
| 1194 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1195 | ctrl_ctx->add_flags |= added_ctxs; |
| 1196 | new_add_flags = ctrl_ctx->add_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1197 | |
| 1198 | /* If xhci_endpoint_disable() was called for this endpoint, but the |
| 1199 | * xHC hasn't been notified yet through the check_bandwidth() call, |
| 1200 | * this re-adds a new state for the endpoint from the new endpoint |
| 1201 | * descriptors. We must drop and re-add this endpoint, so we leave the |
| 1202 | * drop flags alone. |
| 1203 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1204 | new_drop_flags = ctrl_ctx->drop_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1205 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1206 | slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1207 | /* Update the last valid endpoint context, if we just added one past */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1208 | if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) { |
| 1209 | slot_ctx->dev_info &= ~LAST_CTX_MASK; |
| 1210 | slot_ctx->dev_info |= LAST_CTX(last_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1211 | } |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1212 | new_slot_info = slot_ctx->dev_info; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1213 | |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1214 | /* Store the usb_device pointer for later use */ |
| 1215 | ep->hcpriv = udev; |
| 1216 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1217 | xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n", |
| 1218 | (unsigned int) ep->desc.bEndpointAddress, |
| 1219 | udev->slot_id, |
| 1220 | (unsigned int) new_drop_flags, |
| 1221 | (unsigned int) new_add_flags, |
| 1222 | (unsigned int) new_slot_info); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1226 | static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev) |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1227 | { |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1228 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1229 | struct xhci_ep_ctx *ep_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1230 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1231 | int i; |
| 1232 | |
| 1233 | /* When a device's add flag and drop flag are zero, any subsequent |
| 1234 | * configure endpoint command will leave that endpoint's state |
| 1235 | * untouched. Make sure we don't leave any old state in the input |
| 1236 | * endpoint contexts. |
| 1237 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1238 | ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); |
| 1239 | ctrl_ctx->drop_flags = 0; |
| 1240 | ctrl_ctx->add_flags = 0; |
| 1241 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); |
| 1242 | slot_ctx->dev_info &= ~LAST_CTX_MASK; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1243 | /* Endpoint 0 is always valid */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1244 | slot_ctx->dev_info |= LAST_CTX(1); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1245 | for (i = 1; i < 31; ++i) { |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1246 | ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1247 | ep_ctx->ep_info = 0; |
| 1248 | ep_ctx->ep_info2 = 0; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 1249 | ep_ctx->deq = 0; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1250 | ep_ctx->tx_info = 0; |
| 1251 | } |
| 1252 | } |
| 1253 | |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1254 | static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1255 | struct usb_device *udev, int *cmd_status) |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1256 | { |
| 1257 | int ret; |
| 1258 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1259 | switch (*cmd_status) { |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1260 | case COMP_ENOMEM: |
| 1261 | dev_warn(&udev->dev, "Not enough host controller resources " |
| 1262 | "for new device state.\n"); |
| 1263 | ret = -ENOMEM; |
| 1264 | /* FIXME: can we allocate more resources for the HC? */ |
| 1265 | break; |
| 1266 | case COMP_BW_ERR: |
| 1267 | dev_warn(&udev->dev, "Not enough bandwidth " |
| 1268 | "for new device state.\n"); |
| 1269 | ret = -ENOSPC; |
| 1270 | /* FIXME: can we go back to the old state? */ |
| 1271 | break; |
| 1272 | case COMP_TRB_ERR: |
| 1273 | /* the HCD set up something wrong */ |
| 1274 | dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " |
| 1275 | "add flag = 1, " |
| 1276 | "and endpoint is not disabled.\n"); |
| 1277 | ret = -EINVAL; |
| 1278 | break; |
| 1279 | case COMP_SUCCESS: |
| 1280 | dev_dbg(&udev->dev, "Successful Endpoint Configure command\n"); |
| 1281 | ret = 0; |
| 1282 | break; |
| 1283 | default: |
| 1284 | xhci_err(xhci, "ERROR: unexpected command completion " |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1285 | "code 0x%x.\n", *cmd_status); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1286 | ret = -EINVAL; |
| 1287 | break; |
| 1288 | } |
| 1289 | return ret; |
| 1290 | } |
| 1291 | |
| 1292 | static int xhci_evaluate_context_result(struct xhci_hcd *xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1293 | struct usb_device *udev, int *cmd_status) |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1294 | { |
| 1295 | int ret; |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1296 | struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id]; |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1297 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1298 | switch (*cmd_status) { |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1299 | case COMP_EINVAL: |
| 1300 | dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate " |
| 1301 | "context command.\n"); |
| 1302 | ret = -EINVAL; |
| 1303 | break; |
| 1304 | case COMP_EBADSLT: |
| 1305 | dev_warn(&udev->dev, "WARN: slot not enabled for" |
| 1306 | "evaluate context command.\n"); |
| 1307 | case COMP_CTX_STATE: |
| 1308 | dev_warn(&udev->dev, "WARN: invalid context state for " |
| 1309 | "evaluate context command.\n"); |
| 1310 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1); |
| 1311 | ret = -EINVAL; |
| 1312 | break; |
| 1313 | case COMP_SUCCESS: |
| 1314 | dev_dbg(&udev->dev, "Successful evaluate context command\n"); |
| 1315 | ret = 0; |
| 1316 | break; |
| 1317 | default: |
| 1318 | xhci_err(xhci, "ERROR: unexpected command completion " |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1319 | "code 0x%x.\n", *cmd_status); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1320 | ret = -EINVAL; |
| 1321 | break; |
| 1322 | } |
| 1323 | return ret; |
| 1324 | } |
| 1325 | |
| 1326 | /* Issue a configure endpoint command or evaluate context command |
| 1327 | * and wait for it to finish. |
| 1328 | */ |
| 1329 | static int xhci_configure_endpoint(struct xhci_hcd *xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1330 | struct usb_device *udev, |
| 1331 | struct xhci_command *command, |
| 1332 | bool ctx_change, bool must_succeed) |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1333 | { |
| 1334 | int ret; |
| 1335 | int timeleft; |
| 1336 | unsigned long flags; |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1337 | struct xhci_container_ctx *in_ctx; |
| 1338 | struct completion *cmd_completion; |
| 1339 | int *cmd_status; |
| 1340 | struct xhci_virt_device *virt_dev; |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1341 | |
| 1342 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1343 | virt_dev = xhci->devs[udev->slot_id]; |
| 1344 | if (command) { |
| 1345 | in_ctx = command->in_ctx; |
| 1346 | cmd_completion = command->completion; |
| 1347 | cmd_status = &command->status; |
| 1348 | command->command_trb = xhci->cmd_ring->enqueue; |
| 1349 | list_add_tail(&command->cmd_list, &virt_dev->cmd_list); |
| 1350 | } else { |
| 1351 | in_ctx = virt_dev->in_ctx; |
| 1352 | cmd_completion = &virt_dev->cmd_completion; |
| 1353 | cmd_status = &virt_dev->cmd_status; |
| 1354 | } |
Andiry Xu | 1d68064 | 2010-03-12 17:10:04 +0800 | [diff] [blame] | 1355 | init_completion(cmd_completion); |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1356 | |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1357 | if (!ctx_change) |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1358 | ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma, |
| 1359 | udev->slot_id, must_succeed); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1360 | else |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1361 | ret = xhci_queue_evaluate_context(xhci, in_ctx->dma, |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1362 | udev->slot_id); |
| 1363 | if (ret < 0) { |
Sarah Sharp | c01591b | 2009-12-09 15:58:58 -0800 | [diff] [blame] | 1364 | if (command) |
| 1365 | list_del(&command->cmd_list); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1366 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1367 | xhci_dbg(xhci, "FIXME allocate a new ring segment\n"); |
| 1368 | return -ENOMEM; |
| 1369 | } |
| 1370 | xhci_ring_cmd_db(xhci); |
| 1371 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1372 | |
| 1373 | /* Wait for the configure endpoint command to complete */ |
| 1374 | timeleft = wait_for_completion_interruptible_timeout( |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1375 | cmd_completion, |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1376 | USB_CTRL_SET_TIMEOUT); |
| 1377 | if (timeleft <= 0) { |
| 1378 | xhci_warn(xhci, "%s while waiting for %s command\n", |
| 1379 | timeleft == 0 ? "Timeout" : "Signal", |
| 1380 | ctx_change == 0 ? |
| 1381 | "configure endpoint" : |
| 1382 | "evaluate context"); |
| 1383 | /* FIXME cancel the configure endpoint command */ |
| 1384 | return -ETIME; |
| 1385 | } |
| 1386 | |
| 1387 | if (!ctx_change) |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1388 | return xhci_configure_endpoint_result(xhci, udev, cmd_status); |
| 1389 | return xhci_evaluate_context_result(xhci, udev, cmd_status); |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1392 | /* Called after one or more calls to xhci_add_endpoint() or |
| 1393 | * xhci_drop_endpoint(). If this call fails, the USB core is expected |
| 1394 | * to call xhci_reset_bandwidth(). |
| 1395 | * |
| 1396 | * Since we are in the middle of changing either configuration or |
| 1397 | * installing a new alt setting, the USB core won't allow URBs to be |
| 1398 | * enqueued for any endpoint on the old config or interface. Nothing |
| 1399 | * else should be touching the xhci->devs[slot_id] structure, so we |
| 1400 | * don't need to take the xhci->lock for manipulating that. |
| 1401 | */ |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1402 | int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) |
| 1403 | { |
| 1404 | int i; |
| 1405 | int ret = 0; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1406 | struct xhci_hcd *xhci; |
| 1407 | struct xhci_virt_device *virt_dev; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1408 | struct xhci_input_control_ctx *ctrl_ctx; |
| 1409 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1410 | |
| 1411 | ret = xhci_check_args(hcd, udev, NULL, 0, __func__); |
| 1412 | if (ret <= 0) |
| 1413 | return ret; |
| 1414 | xhci = hcd_to_xhci(hcd); |
| 1415 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1416 | if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) { |
| 1417 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 1418 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1419 | return -EINVAL; |
| 1420 | } |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1421 | xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1422 | virt_dev = xhci->devs[udev->slot_id]; |
| 1423 | |
| 1424 | /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1425 | ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); |
| 1426 | ctrl_ctx->add_flags |= SLOT_FLAG; |
| 1427 | ctrl_ctx->add_flags &= ~EP0_FLAG; |
| 1428 | ctrl_ctx->drop_flags &= ~SLOT_FLAG; |
| 1429 | ctrl_ctx->drop_flags &= ~EP0_FLAG; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1430 | xhci_dbg(xhci, "New Input Control Context:\n"); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1431 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); |
| 1432 | xhci_dbg_ctx(xhci, virt_dev->in_ctx, |
| 1433 | LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1434 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1435 | ret = xhci_configure_endpoint(xhci, udev, NULL, |
| 1436 | false, false); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1437 | if (ret) { |
| 1438 | /* Callee should call reset_bandwidth() */ |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1439 | return ret; |
| 1440 | } |
| 1441 | |
| 1442 | xhci_dbg(xhci, "Output context after successful config ep cmd:\n"); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1443 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, |
| 1444 | LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1445 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1446 | xhci_zero_in_ctx(xhci, virt_dev); |
Sarah Sharp | 74f9fe2 | 2009-12-03 09:44:29 -0800 | [diff] [blame] | 1447 | /* Install new rings and free or cache any old rings */ |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1448 | for (i = 1; i < 31; ++i) { |
Sarah Sharp | 74f9fe2 | 2009-12-03 09:44:29 -0800 | [diff] [blame] | 1449 | if (!virt_dev->eps[i].new_ring) |
| 1450 | continue; |
| 1451 | /* Only cache or free the old ring if it exists. |
| 1452 | * It may not if this is the first add of an endpoint. |
| 1453 | */ |
| 1454 | if (virt_dev->eps[i].ring) { |
Sarah Sharp | 412566b | 2009-12-09 15:59:01 -0800 | [diff] [blame] | 1455 | xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1456 | } |
Sarah Sharp | 74f9fe2 | 2009-12-03 09:44:29 -0800 | [diff] [blame] | 1457 | virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; |
| 1458 | virt_dev->eps[i].new_ring = NULL; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1459 | } |
| 1460 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1461 | return ret; |
| 1462 | } |
| 1463 | |
| 1464 | void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) |
| 1465 | { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1466 | struct xhci_hcd *xhci; |
| 1467 | struct xhci_virt_device *virt_dev; |
| 1468 | int i, ret; |
| 1469 | |
| 1470 | ret = xhci_check_args(hcd, udev, NULL, 0, __func__); |
| 1471 | if (ret <= 0) |
| 1472 | return; |
| 1473 | xhci = hcd_to_xhci(hcd); |
| 1474 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1475 | if (!xhci->devs || !xhci->devs[udev->slot_id]) { |
| 1476 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 1477 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1478 | return; |
| 1479 | } |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1480 | xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1481 | virt_dev = xhci->devs[udev->slot_id]; |
| 1482 | /* Free any rings allocated for added endpoints */ |
| 1483 | for (i = 0; i < 31; ++i) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1484 | if (virt_dev->eps[i].new_ring) { |
| 1485 | xhci_ring_free(xhci, virt_dev->eps[i].new_ring); |
| 1486 | virt_dev->eps[i].new_ring = NULL; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1487 | } |
| 1488 | } |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1489 | xhci_zero_in_ctx(xhci, virt_dev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Sarah Sharp | 5270b95 | 2009-09-04 10:53:11 -0700 | [diff] [blame] | 1492 | static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci, |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1493 | struct xhci_container_ctx *in_ctx, |
| 1494 | struct xhci_container_ctx *out_ctx, |
| 1495 | u32 add_flags, u32 drop_flags) |
Sarah Sharp | 5270b95 | 2009-09-04 10:53:11 -0700 | [diff] [blame] | 1496 | { |
| 1497 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1498 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
Sarah Sharp | 5270b95 | 2009-09-04 10:53:11 -0700 | [diff] [blame] | 1499 | ctrl_ctx->add_flags = add_flags; |
| 1500 | ctrl_ctx->drop_flags = drop_flags; |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1501 | xhci_slot_copy(xhci, in_ctx, out_ctx); |
Sarah Sharp | 5270b95 | 2009-09-04 10:53:11 -0700 | [diff] [blame] | 1502 | ctrl_ctx->add_flags |= SLOT_FLAG; |
| 1503 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1504 | xhci_dbg(xhci, "Input Context:\n"); |
| 1505 | xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags)); |
Sarah Sharp | 5270b95 | 2009-09-04 10:53:11 -0700 | [diff] [blame] | 1506 | } |
| 1507 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1508 | void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci, |
| 1509 | unsigned int slot_id, unsigned int ep_index, |
| 1510 | struct xhci_dequeue_state *deq_state) |
| 1511 | { |
| 1512 | struct xhci_container_ctx *in_ctx; |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1513 | struct xhci_ep_ctx *ep_ctx; |
| 1514 | u32 added_ctxs; |
| 1515 | dma_addr_t addr; |
| 1516 | |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1517 | xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx, |
| 1518 | xhci->devs[slot_id]->out_ctx, ep_index); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1519 | in_ctx = xhci->devs[slot_id]->in_ctx; |
| 1520 | ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); |
| 1521 | addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg, |
| 1522 | deq_state->new_deq_ptr); |
| 1523 | if (addr == 0) { |
| 1524 | xhci_warn(xhci, "WARN Cannot submit config ep after " |
| 1525 | "reset ep command\n"); |
| 1526 | xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n", |
| 1527 | deq_state->new_deq_seg, |
| 1528 | deq_state->new_deq_ptr); |
| 1529 | return; |
| 1530 | } |
| 1531 | ep_ctx->deq = addr | deq_state->new_cycle_state; |
| 1532 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1533 | added_ctxs = xhci_get_endpoint_flag_from_index(ep_index); |
Sarah Sharp | 913a8a3 | 2009-09-04 10:53:13 -0700 | [diff] [blame] | 1534 | xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx, |
| 1535 | xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1538 | void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1539 | struct usb_device *udev, unsigned int ep_index) |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1540 | { |
| 1541 | struct xhci_dequeue_state deq_state; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1542 | struct xhci_virt_ep *ep; |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1543 | |
| 1544 | xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n"); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1545 | ep = &xhci->devs[udev->slot_id]->eps[ep_index]; |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1546 | /* We need to move the HW's dequeue pointer past this TD, |
| 1547 | * or it will attempt to resend it on the next doorbell ring. |
| 1548 | */ |
| 1549 | xhci_find_new_dequeue_state(xhci, udev->slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1550 | ep_index, ep->stopped_stream, ep->stopped_td, |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1551 | &deq_state); |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1552 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1553 | /* HW with the reset endpoint quirk will use the saved dequeue state to |
| 1554 | * issue a configure endpoint command later. |
| 1555 | */ |
| 1556 | if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) { |
| 1557 | xhci_dbg(xhci, "Queueing new dequeue state\n"); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1558 | xhci_queue_new_dequeue_state(xhci, udev->slot_id, |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1559 | ep_index, ep->stopped_stream, &deq_state); |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1560 | } else { |
| 1561 | /* Better hope no one uses the input context between now and the |
| 1562 | * reset endpoint completion! |
Sarah Sharp | e9df17e | 2010-04-02 15:34:43 -0700 | [diff] [blame] | 1563 | * XXX: No idea how this hardware will react when stream rings |
| 1564 | * are enabled. |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1565 | */ |
| 1566 | xhci_dbg(xhci, "Setting up input context for " |
| 1567 | "configure endpoint command\n"); |
| 1568 | xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id, |
| 1569 | ep_index, &deq_state); |
| 1570 | } |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1573 | /* Deal with stalled endpoints. The core should have sent the control message |
| 1574 | * to clear the halt condition. However, we need to make the xHCI hardware |
| 1575 | * reset its sequence number, since a device will expect a sequence number of |
| 1576 | * zero after the halt condition is cleared. |
| 1577 | * Context: in_interrupt |
| 1578 | */ |
| 1579 | void xhci_endpoint_reset(struct usb_hcd *hcd, |
| 1580 | struct usb_host_endpoint *ep) |
| 1581 | { |
| 1582 | struct xhci_hcd *xhci; |
| 1583 | struct usb_device *udev; |
| 1584 | unsigned int ep_index; |
| 1585 | unsigned long flags; |
| 1586 | int ret; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1587 | struct xhci_virt_ep *virt_ep; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1588 | |
| 1589 | xhci = hcd_to_xhci(hcd); |
| 1590 | udev = (struct usb_device *) ep->hcpriv; |
| 1591 | /* Called with a root hub endpoint (or an endpoint that wasn't added |
| 1592 | * with xhci_add_endpoint() |
| 1593 | */ |
| 1594 | if (!ep->hcpriv) |
| 1595 | return; |
| 1596 | ep_index = xhci_get_endpoint_index(&ep->desc); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1597 | virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index]; |
| 1598 | if (!virt_ep->stopped_td) { |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 1599 | xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n", |
| 1600 | ep->desc.bEndpointAddress); |
| 1601 | return; |
| 1602 | } |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1603 | if (usb_endpoint_xfer_control(&ep->desc)) { |
| 1604 | xhci_dbg(xhci, "Control endpoint stall already handled.\n"); |
| 1605 | return; |
| 1606 | } |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1607 | |
| 1608 | xhci_dbg(xhci, "Queueing reset endpoint command\n"); |
| 1609 | spin_lock_irqsave(&xhci->lock, flags); |
| 1610 | ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index); |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 1611 | /* |
| 1612 | * Can't change the ring dequeue pointer until it's transitioned to the |
| 1613 | * stopped state, which is only upon a successful reset endpoint |
| 1614 | * command. Better hope that last command worked! |
| 1615 | */ |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1616 | if (!ret) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame] | 1617 | xhci_cleanup_stalled_ring(xhci, udev, ep_index); |
| 1618 | kfree(virt_ep->stopped_td); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1619 | xhci_ring_cmd_db(xhci); |
| 1620 | } |
Sarah Sharp | 1624ae1 | 2010-05-06 13:40:08 -0700 | [diff] [blame] | 1621 | virt_ep->stopped_td = NULL; |
| 1622 | virt_ep->stopped_trb = NULL; |
Sarah Sharp | 5e5cf6f | 2010-05-06 13:40:18 -0700 | [diff] [blame] | 1623 | virt_ep->stopped_stream = 0; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1624 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1625 | |
| 1626 | if (ret) |
| 1627 | xhci_warn(xhci, "FIXME allocate a new ring segment\n"); |
| 1628 | } |
| 1629 | |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1630 | static int xhci_check_streams_endpoint(struct xhci_hcd *xhci, |
| 1631 | struct usb_device *udev, struct usb_host_endpoint *ep, |
| 1632 | unsigned int slot_id) |
| 1633 | { |
| 1634 | int ret; |
| 1635 | unsigned int ep_index; |
| 1636 | unsigned int ep_state; |
| 1637 | |
| 1638 | if (!ep) |
| 1639 | return -EINVAL; |
| 1640 | ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, __func__); |
| 1641 | if (ret <= 0) |
| 1642 | return -EINVAL; |
Alan Stern | 842f169 | 2010-04-30 12:44:46 -0400 | [diff] [blame] | 1643 | if (ep->ss_ep_comp.bmAttributes == 0) { |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1644 | xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion" |
| 1645 | " descriptor for ep 0x%x does not support streams\n", |
| 1646 | ep->desc.bEndpointAddress); |
| 1647 | return -EINVAL; |
| 1648 | } |
| 1649 | |
| 1650 | ep_index = xhci_get_endpoint_index(&ep->desc); |
| 1651 | ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; |
| 1652 | if (ep_state & EP_HAS_STREAMS || |
| 1653 | ep_state & EP_GETTING_STREAMS) { |
| 1654 | xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x " |
| 1655 | "already has streams set up.\n", |
| 1656 | ep->desc.bEndpointAddress); |
| 1657 | xhci_warn(xhci, "Send email to xHCI maintainer and ask for " |
| 1658 | "dynamic stream context array reallocation.\n"); |
| 1659 | return -EINVAL; |
| 1660 | } |
| 1661 | if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) { |
| 1662 | xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk " |
| 1663 | "endpoint 0x%x; URBs are pending.\n", |
| 1664 | ep->desc.bEndpointAddress); |
| 1665 | return -EINVAL; |
| 1666 | } |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
| 1670 | static void xhci_calculate_streams_entries(struct xhci_hcd *xhci, |
| 1671 | unsigned int *num_streams, unsigned int *num_stream_ctxs) |
| 1672 | { |
| 1673 | unsigned int max_streams; |
| 1674 | |
| 1675 | /* The stream context array size must be a power of two */ |
| 1676 | *num_stream_ctxs = roundup_pow_of_two(*num_streams); |
| 1677 | /* |
| 1678 | * Find out how many primary stream array entries the host controller |
| 1679 | * supports. Later we may use secondary stream arrays (similar to 2nd |
| 1680 | * level page entries), but that's an optional feature for xHCI host |
| 1681 | * controllers. xHCs must support at least 4 stream IDs. |
| 1682 | */ |
| 1683 | max_streams = HCC_MAX_PSA(xhci->hcc_params); |
| 1684 | if (*num_stream_ctxs > max_streams) { |
| 1685 | xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n", |
| 1686 | max_streams); |
| 1687 | *num_stream_ctxs = max_streams; |
| 1688 | *num_streams = max_streams; |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | /* Returns an error code if one of the endpoint already has streams. |
| 1693 | * This does not change any data structures, it only checks and gathers |
| 1694 | * information. |
| 1695 | */ |
| 1696 | static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci, |
| 1697 | struct usb_device *udev, |
| 1698 | struct usb_host_endpoint **eps, unsigned int num_eps, |
| 1699 | unsigned int *num_streams, u32 *changed_ep_bitmask) |
| 1700 | { |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1701 | unsigned int max_streams; |
| 1702 | unsigned int endpoint_flag; |
| 1703 | int i; |
| 1704 | int ret; |
| 1705 | |
| 1706 | for (i = 0; i < num_eps; i++) { |
| 1707 | ret = xhci_check_streams_endpoint(xhci, udev, |
| 1708 | eps[i], udev->slot_id); |
| 1709 | if (ret < 0) |
| 1710 | return ret; |
| 1711 | |
Alan Stern | 842f169 | 2010-04-30 12:44:46 -0400 | [diff] [blame] | 1712 | max_streams = USB_SS_MAX_STREAMS( |
| 1713 | eps[i]->ss_ep_comp.bmAttributes); |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1714 | if (max_streams < (*num_streams - 1)) { |
| 1715 | xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n", |
| 1716 | eps[i]->desc.bEndpointAddress, |
| 1717 | max_streams); |
| 1718 | *num_streams = max_streams+1; |
| 1719 | } |
| 1720 | |
| 1721 | endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc); |
| 1722 | if (*changed_ep_bitmask & endpoint_flag) |
| 1723 | return -EINVAL; |
| 1724 | *changed_ep_bitmask |= endpoint_flag; |
| 1725 | } |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
| 1729 | static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci, |
| 1730 | struct usb_device *udev, |
| 1731 | struct usb_host_endpoint **eps, unsigned int num_eps) |
| 1732 | { |
| 1733 | u32 changed_ep_bitmask = 0; |
| 1734 | unsigned int slot_id; |
| 1735 | unsigned int ep_index; |
| 1736 | unsigned int ep_state; |
| 1737 | int i; |
| 1738 | |
| 1739 | slot_id = udev->slot_id; |
| 1740 | if (!xhci->devs[slot_id]) |
| 1741 | return 0; |
| 1742 | |
| 1743 | for (i = 0; i < num_eps; i++) { |
| 1744 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1745 | ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state; |
| 1746 | /* Are streams already being freed for the endpoint? */ |
| 1747 | if (ep_state & EP_GETTING_NO_STREAMS) { |
| 1748 | xhci_warn(xhci, "WARN Can't disable streams for " |
| 1749 | "endpoint 0x%x\n, " |
| 1750 | "streams are being disabled already.", |
| 1751 | eps[i]->desc.bEndpointAddress); |
| 1752 | return 0; |
| 1753 | } |
| 1754 | /* Are there actually any streams to free? */ |
| 1755 | if (!(ep_state & EP_HAS_STREAMS) && |
| 1756 | !(ep_state & EP_GETTING_STREAMS)) { |
| 1757 | xhci_warn(xhci, "WARN Can't disable streams for " |
| 1758 | "endpoint 0x%x\n, " |
| 1759 | "streams are already disabled!", |
| 1760 | eps[i]->desc.bEndpointAddress); |
| 1761 | xhci_warn(xhci, "WARN xhci_free_streams() called " |
| 1762 | "with non-streams endpoint\n"); |
| 1763 | return 0; |
| 1764 | } |
| 1765 | changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc); |
| 1766 | } |
| 1767 | return changed_ep_bitmask; |
| 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | * The USB device drivers use this function (though the HCD interface in USB |
| 1772 | * core) to prepare a set of bulk endpoints to use streams. Streams are used to |
| 1773 | * coordinate mass storage command queueing across multiple endpoints (basically |
| 1774 | * a stream ID == a task ID). |
| 1775 | * |
| 1776 | * Setting up streams involves allocating the same size stream context array |
| 1777 | * for each endpoint and issuing a configure endpoint command for all endpoints. |
| 1778 | * |
| 1779 | * Don't allow the call to succeed if one endpoint only supports one stream |
| 1780 | * (which means it doesn't support streams at all). |
| 1781 | * |
| 1782 | * Drivers may get less stream IDs than they asked for, if the host controller |
| 1783 | * hardware or endpoints claim they can't support the number of requested |
| 1784 | * stream IDs. |
| 1785 | */ |
| 1786 | int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev, |
| 1787 | struct usb_host_endpoint **eps, unsigned int num_eps, |
| 1788 | unsigned int num_streams, gfp_t mem_flags) |
| 1789 | { |
| 1790 | int i, ret; |
| 1791 | struct xhci_hcd *xhci; |
| 1792 | struct xhci_virt_device *vdev; |
| 1793 | struct xhci_command *config_cmd; |
| 1794 | unsigned int ep_index; |
| 1795 | unsigned int num_stream_ctxs; |
| 1796 | unsigned long flags; |
| 1797 | u32 changed_ep_bitmask = 0; |
| 1798 | |
| 1799 | if (!eps) |
| 1800 | return -EINVAL; |
| 1801 | |
| 1802 | /* Add one to the number of streams requested to account for |
| 1803 | * stream 0 that is reserved for xHCI usage. |
| 1804 | */ |
| 1805 | num_streams += 1; |
| 1806 | xhci = hcd_to_xhci(hcd); |
| 1807 | xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n", |
| 1808 | num_streams); |
| 1809 | |
| 1810 | config_cmd = xhci_alloc_command(xhci, true, true, mem_flags); |
| 1811 | if (!config_cmd) { |
| 1812 | xhci_dbg(xhci, "Could not allocate xHCI command structure.\n"); |
| 1813 | return -ENOMEM; |
| 1814 | } |
| 1815 | |
| 1816 | /* Check to make sure all endpoints are not already configured for |
| 1817 | * streams. While we're at it, find the maximum number of streams that |
| 1818 | * all the endpoints will support and check for duplicate endpoints. |
| 1819 | */ |
| 1820 | spin_lock_irqsave(&xhci->lock, flags); |
| 1821 | ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps, |
| 1822 | num_eps, &num_streams, &changed_ep_bitmask); |
| 1823 | if (ret < 0) { |
| 1824 | xhci_free_command(xhci, config_cmd); |
| 1825 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1826 | return ret; |
| 1827 | } |
| 1828 | if (num_streams <= 1) { |
| 1829 | xhci_warn(xhci, "WARN: endpoints can't handle " |
| 1830 | "more than one stream.\n"); |
| 1831 | xhci_free_command(xhci, config_cmd); |
| 1832 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1833 | return -EINVAL; |
| 1834 | } |
| 1835 | vdev = xhci->devs[udev->slot_id]; |
| 1836 | /* Mark each endpoint as being in transistion, so |
| 1837 | * xhci_urb_enqueue() will reject all URBs. |
| 1838 | */ |
| 1839 | for (i = 0; i < num_eps; i++) { |
| 1840 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1841 | vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS; |
| 1842 | } |
| 1843 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1844 | |
| 1845 | /* Setup internal data structures and allocate HW data structures for |
| 1846 | * streams (but don't install the HW structures in the input context |
| 1847 | * until we're sure all memory allocation succeeded). |
| 1848 | */ |
| 1849 | xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs); |
| 1850 | xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n", |
| 1851 | num_stream_ctxs, num_streams); |
| 1852 | |
| 1853 | for (i = 0; i < num_eps; i++) { |
| 1854 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1855 | vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci, |
| 1856 | num_stream_ctxs, |
| 1857 | num_streams, mem_flags); |
| 1858 | if (!vdev->eps[ep_index].stream_info) |
| 1859 | goto cleanup; |
| 1860 | /* Set maxPstreams in endpoint context and update deq ptr to |
| 1861 | * point to stream context array. FIXME |
| 1862 | */ |
| 1863 | } |
| 1864 | |
| 1865 | /* Set up the input context for a configure endpoint command. */ |
| 1866 | for (i = 0; i < num_eps; i++) { |
| 1867 | struct xhci_ep_ctx *ep_ctx; |
| 1868 | |
| 1869 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1870 | ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index); |
| 1871 | |
| 1872 | xhci_endpoint_copy(xhci, config_cmd->in_ctx, |
| 1873 | vdev->out_ctx, ep_index); |
| 1874 | xhci_setup_streams_ep_input_ctx(xhci, ep_ctx, |
| 1875 | vdev->eps[ep_index].stream_info); |
| 1876 | } |
| 1877 | /* Tell the HW to drop its old copy of the endpoint context info |
| 1878 | * and add the updated copy from the input context. |
| 1879 | */ |
| 1880 | xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx, |
| 1881 | vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask); |
| 1882 | |
| 1883 | /* Issue and wait for the configure endpoint command */ |
| 1884 | ret = xhci_configure_endpoint(xhci, udev, config_cmd, |
| 1885 | false, false); |
| 1886 | |
| 1887 | /* xHC rejected the configure endpoint command for some reason, so we |
| 1888 | * leave the old ring intact and free our internal streams data |
| 1889 | * structure. |
| 1890 | */ |
| 1891 | if (ret < 0) |
| 1892 | goto cleanup; |
| 1893 | |
| 1894 | spin_lock_irqsave(&xhci->lock, flags); |
| 1895 | for (i = 0; i < num_eps; i++) { |
| 1896 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1897 | vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; |
| 1898 | xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n", |
| 1899 | udev->slot_id, ep_index); |
| 1900 | vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS; |
| 1901 | } |
| 1902 | xhci_free_command(xhci, config_cmd); |
| 1903 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1904 | |
| 1905 | /* Subtract 1 for stream 0, which drivers can't use */ |
| 1906 | return num_streams - 1; |
| 1907 | |
| 1908 | cleanup: |
| 1909 | /* If it didn't work, free the streams! */ |
| 1910 | for (i = 0; i < num_eps; i++) { |
| 1911 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1912 | xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); |
Sarah Sharp | 8a00774 | 2010-04-30 15:37:56 -0700 | [diff] [blame] | 1913 | vdev->eps[ep_index].stream_info = NULL; |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1914 | /* FIXME Unset maxPstreams in endpoint context and |
| 1915 | * update deq ptr to point to normal string ring. |
| 1916 | */ |
| 1917 | vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS; |
| 1918 | vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; |
| 1919 | xhci_endpoint_zero(xhci, vdev, eps[i]); |
| 1920 | } |
| 1921 | xhci_free_command(xhci, config_cmd); |
| 1922 | return -ENOMEM; |
| 1923 | } |
| 1924 | |
| 1925 | /* Transition the endpoint from using streams to being a "normal" endpoint |
| 1926 | * without streams. |
| 1927 | * |
| 1928 | * Modify the endpoint context state, submit a configure endpoint command, |
| 1929 | * and free all endpoint rings for streams if that completes successfully. |
| 1930 | */ |
| 1931 | int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev, |
| 1932 | struct usb_host_endpoint **eps, unsigned int num_eps, |
| 1933 | gfp_t mem_flags) |
| 1934 | { |
| 1935 | int i, ret; |
| 1936 | struct xhci_hcd *xhci; |
| 1937 | struct xhci_virt_device *vdev; |
| 1938 | struct xhci_command *command; |
| 1939 | unsigned int ep_index; |
| 1940 | unsigned long flags; |
| 1941 | u32 changed_ep_bitmask; |
| 1942 | |
| 1943 | xhci = hcd_to_xhci(hcd); |
| 1944 | vdev = xhci->devs[udev->slot_id]; |
| 1945 | |
| 1946 | /* Set up a configure endpoint command to remove the streams rings */ |
| 1947 | spin_lock_irqsave(&xhci->lock, flags); |
| 1948 | changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci, |
| 1949 | udev, eps, num_eps); |
| 1950 | if (changed_ep_bitmask == 0) { |
| 1951 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1952 | return -EINVAL; |
| 1953 | } |
| 1954 | |
| 1955 | /* Use the xhci_command structure from the first endpoint. We may have |
| 1956 | * allocated too many, but the driver may call xhci_free_streams() for |
| 1957 | * each endpoint it grouped into one call to xhci_alloc_streams(). |
| 1958 | */ |
| 1959 | ep_index = xhci_get_endpoint_index(&eps[0]->desc); |
| 1960 | command = vdev->eps[ep_index].stream_info->free_streams_command; |
| 1961 | for (i = 0; i < num_eps; i++) { |
| 1962 | struct xhci_ep_ctx *ep_ctx; |
| 1963 | |
| 1964 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1965 | ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index); |
| 1966 | xhci->devs[udev->slot_id]->eps[ep_index].ep_state |= |
| 1967 | EP_GETTING_NO_STREAMS; |
| 1968 | |
| 1969 | xhci_endpoint_copy(xhci, command->in_ctx, |
| 1970 | vdev->out_ctx, ep_index); |
| 1971 | xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx, |
| 1972 | &vdev->eps[ep_index]); |
| 1973 | } |
| 1974 | xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx, |
| 1975 | vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask); |
| 1976 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1977 | |
| 1978 | /* Issue and wait for the configure endpoint command, |
| 1979 | * which must succeed. |
| 1980 | */ |
| 1981 | ret = xhci_configure_endpoint(xhci, udev, command, |
| 1982 | false, true); |
| 1983 | |
| 1984 | /* xHC rejected the configure endpoint command for some reason, so we |
| 1985 | * leave the streams rings intact. |
| 1986 | */ |
| 1987 | if (ret < 0) |
| 1988 | return ret; |
| 1989 | |
| 1990 | spin_lock_irqsave(&xhci->lock, flags); |
| 1991 | for (i = 0; i < num_eps; i++) { |
| 1992 | ep_index = xhci_get_endpoint_index(&eps[i]->desc); |
| 1993 | xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info); |
Sarah Sharp | 8a00774 | 2010-04-30 15:37:56 -0700 | [diff] [blame] | 1994 | vdev->eps[ep_index].stream_info = NULL; |
Sarah Sharp | 8df75f4 | 2010-04-02 15:34:16 -0700 | [diff] [blame] | 1995 | /* FIXME Unset maxPstreams in endpoint context and |
| 1996 | * update deq ptr to point to normal string ring. |
| 1997 | */ |
| 1998 | vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS; |
| 1999 | vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS; |
| 2000 | } |
| 2001 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2006 | /* |
Sarah Sharp | 2a8f82c | 2009-12-09 15:59:13 -0800 | [diff] [blame] | 2007 | * This submits a Reset Device Command, which will set the device state to 0, |
| 2008 | * set the device address to 0, and disable all the endpoints except the default |
| 2009 | * control endpoint. The USB core should come back and call |
| 2010 | * xhci_address_device(), and then re-set up the configuration. If this is |
| 2011 | * called because of a usb_reset_and_verify_device(), then the old alternate |
| 2012 | * settings will be re-installed through the normal bandwidth allocation |
| 2013 | * functions. |
| 2014 | * |
| 2015 | * Wait for the Reset Device command to finish. Remove all structures |
| 2016 | * associated with the endpoints that were disabled. Clear the input device |
| 2017 | * structure? Cache the rings? Reset the control endpoint 0 max packet size? |
| 2018 | */ |
| 2019 | int xhci_reset_device(struct usb_hcd *hcd, struct usb_device *udev) |
| 2020 | { |
| 2021 | int ret, i; |
| 2022 | unsigned long flags; |
| 2023 | struct xhci_hcd *xhci; |
| 2024 | unsigned int slot_id; |
| 2025 | struct xhci_virt_device *virt_dev; |
| 2026 | struct xhci_command *reset_device_cmd; |
| 2027 | int timeleft; |
| 2028 | int last_freed_endpoint; |
| 2029 | |
| 2030 | ret = xhci_check_args(hcd, udev, NULL, 0, __func__); |
| 2031 | if (ret <= 0) |
| 2032 | return ret; |
| 2033 | xhci = hcd_to_xhci(hcd); |
| 2034 | slot_id = udev->slot_id; |
| 2035 | virt_dev = xhci->devs[slot_id]; |
| 2036 | if (!virt_dev) { |
| 2037 | xhci_dbg(xhci, "%s called with invalid slot ID %u\n", |
| 2038 | __func__, slot_id); |
| 2039 | return -EINVAL; |
| 2040 | } |
| 2041 | |
| 2042 | xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id); |
| 2043 | /* Allocate the command structure that holds the struct completion. |
| 2044 | * Assume we're in process context, since the normal device reset |
| 2045 | * process has to wait for the device anyway. Storage devices are |
| 2046 | * reset as part of error handling, so use GFP_NOIO instead of |
| 2047 | * GFP_KERNEL. |
| 2048 | */ |
| 2049 | reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO); |
| 2050 | if (!reset_device_cmd) { |
| 2051 | xhci_dbg(xhci, "Couldn't allocate command structure.\n"); |
| 2052 | return -ENOMEM; |
| 2053 | } |
| 2054 | |
| 2055 | /* Attempt to submit the Reset Device command to the command ring */ |
| 2056 | spin_lock_irqsave(&xhci->lock, flags); |
| 2057 | reset_device_cmd->command_trb = xhci->cmd_ring->enqueue; |
| 2058 | list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list); |
| 2059 | ret = xhci_queue_reset_device(xhci, slot_id); |
| 2060 | if (ret) { |
| 2061 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 2062 | list_del(&reset_device_cmd->cmd_list); |
| 2063 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2064 | goto command_cleanup; |
| 2065 | } |
| 2066 | xhci_ring_cmd_db(xhci); |
| 2067 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2068 | |
| 2069 | /* Wait for the Reset Device command to finish */ |
| 2070 | timeleft = wait_for_completion_interruptible_timeout( |
| 2071 | reset_device_cmd->completion, |
| 2072 | USB_CTRL_SET_TIMEOUT); |
| 2073 | if (timeleft <= 0) { |
| 2074 | xhci_warn(xhci, "%s while waiting for reset device command\n", |
| 2075 | timeleft == 0 ? "Timeout" : "Signal"); |
| 2076 | spin_lock_irqsave(&xhci->lock, flags); |
| 2077 | /* The timeout might have raced with the event ring handler, so |
| 2078 | * only delete from the list if the item isn't poisoned. |
| 2079 | */ |
| 2080 | if (reset_device_cmd->cmd_list.next != LIST_POISON1) |
| 2081 | list_del(&reset_device_cmd->cmd_list); |
| 2082 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2083 | ret = -ETIME; |
| 2084 | goto command_cleanup; |
| 2085 | } |
| 2086 | |
| 2087 | /* The Reset Device command can't fail, according to the 0.95/0.96 spec, |
| 2088 | * unless we tried to reset a slot ID that wasn't enabled, |
| 2089 | * or the device wasn't in the addressed or configured state. |
| 2090 | */ |
| 2091 | ret = reset_device_cmd->status; |
| 2092 | switch (ret) { |
| 2093 | case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */ |
| 2094 | case COMP_CTX_STATE: /* 0.96 completion code for same thing */ |
| 2095 | xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n", |
| 2096 | slot_id, |
| 2097 | xhci_get_slot_state(xhci, virt_dev->out_ctx)); |
| 2098 | xhci_info(xhci, "Not freeing device rings.\n"); |
| 2099 | /* Don't treat this as an error. May change my mind later. */ |
| 2100 | ret = 0; |
| 2101 | goto command_cleanup; |
| 2102 | case COMP_SUCCESS: |
| 2103 | xhci_dbg(xhci, "Successful reset device command.\n"); |
| 2104 | break; |
| 2105 | default: |
| 2106 | if (xhci_is_vendor_info_code(xhci, ret)) |
| 2107 | break; |
| 2108 | xhci_warn(xhci, "Unknown completion code %u for " |
| 2109 | "reset device command.\n", ret); |
| 2110 | ret = -EINVAL; |
| 2111 | goto command_cleanup; |
| 2112 | } |
| 2113 | |
| 2114 | /* Everything but endpoint 0 is disabled, so free or cache the rings. */ |
| 2115 | last_freed_endpoint = 1; |
| 2116 | for (i = 1; i < 31; ++i) { |
| 2117 | if (!virt_dev->eps[i].ring) |
| 2118 | continue; |
| 2119 | xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i); |
| 2120 | last_freed_endpoint = i; |
| 2121 | } |
| 2122 | xhci_dbg(xhci, "Output context after successful reset device cmd:\n"); |
| 2123 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint); |
| 2124 | ret = 0; |
| 2125 | |
| 2126 | command_cleanup: |
| 2127 | xhci_free_command(xhci, reset_device_cmd); |
| 2128 | return ret; |
| 2129 | } |
| 2130 | |
| 2131 | /* |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2132 | * At this point, the struct usb_device is about to go away, the device has |
| 2133 | * disconnected, and all traffic has been stopped and the endpoints have been |
| 2134 | * disabled. Free any HC data structures associated with that device. |
| 2135 | */ |
| 2136 | void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) |
| 2137 | { |
| 2138 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2139 | struct xhci_virt_device *virt_dev; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2140 | unsigned long flags; |
Sarah Sharp | c526d0d | 2009-09-16 16:42:39 -0700 | [diff] [blame] | 2141 | u32 state; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2142 | int i; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2143 | |
| 2144 | if (udev->slot_id == 0) |
| 2145 | return; |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2146 | virt_dev = xhci->devs[udev->slot_id]; |
| 2147 | if (!virt_dev) |
| 2148 | return; |
| 2149 | |
| 2150 | /* Stop any wayward timer functions (which may grab the lock) */ |
| 2151 | for (i = 0; i < 31; ++i) { |
| 2152 | virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING; |
| 2153 | del_timer_sync(&virt_dev->eps[i].stop_cmd_timer); |
| 2154 | } |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2155 | |
| 2156 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | c526d0d | 2009-09-16 16:42:39 -0700 | [diff] [blame] | 2157 | /* Don't disable the slot if the host controller is dead. */ |
| 2158 | state = xhci_readl(xhci, &xhci->op_regs->status); |
Sarah Sharp | 6f5165c | 2009-10-27 10:57:01 -0700 | [diff] [blame] | 2159 | if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) { |
Sarah Sharp | c526d0d | 2009-09-16 16:42:39 -0700 | [diff] [blame] | 2160 | xhci_free_virt_device(xhci, udev->slot_id); |
| 2161 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2162 | return; |
| 2163 | } |
| 2164 | |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2165 | if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) { |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2166 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2167 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 2168 | return; |
| 2169 | } |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2170 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2171 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2172 | /* |
| 2173 | * Event command completion handler will free any data structures |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 2174 | * associated with the slot. XXX Can free sleep? |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2175 | */ |
| 2176 | } |
| 2177 | |
| 2178 | /* |
| 2179 | * Returns 0 if the xHC ran out of device slots, the Enable Slot command |
| 2180 | * timed out, or allocating memory failed. Returns 1 on success. |
| 2181 | */ |
| 2182 | int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev) |
| 2183 | { |
| 2184 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 2185 | unsigned long flags; |
| 2186 | int timeleft; |
| 2187 | int ret; |
| 2188 | |
| 2189 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2190 | ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2191 | if (ret) { |
| 2192 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2193 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 2194 | return 0; |
| 2195 | } |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2196 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2197 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2198 | |
| 2199 | /* XXX: how much time for xHC slot assignment? */ |
| 2200 | timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev, |
| 2201 | USB_CTRL_SET_TIMEOUT); |
| 2202 | if (timeleft <= 0) { |
| 2203 | xhci_warn(xhci, "%s while waiting for a slot\n", |
| 2204 | timeleft == 0 ? "Timeout" : "Signal"); |
| 2205 | /* FIXME cancel the enable slot request */ |
| 2206 | return 0; |
| 2207 | } |
| 2208 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2209 | if (!xhci->slot_id) { |
| 2210 | xhci_err(xhci, "Error while assigning device slot ID\n"); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2211 | return 0; |
| 2212 | } |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 2213 | /* xhci_alloc_virt_device() does not touch rings; no need to lock */ |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2214 | if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) { |
| 2215 | /* Disable slot, if we can do it without mem alloc */ |
| 2216 | xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 2217 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2218 | if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) |
| 2219 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2220 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2221 | return 0; |
| 2222 | } |
| 2223 | udev->slot_id = xhci->slot_id; |
| 2224 | /* Is this a LS or FS device under a HS hub? */ |
| 2225 | /* Hub or peripherial? */ |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2226 | return 1; |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * Issue an Address Device command (which will issue a SetAddress request to |
| 2231 | * the device). |
| 2232 | * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so |
| 2233 | * we should only issue and wait on one address command at the same time. |
| 2234 | * |
| 2235 | * We add one to the device address issued by the hardware because the USB core |
| 2236 | * uses address 1 for the root hubs (even though they're not really devices). |
| 2237 | */ |
| 2238 | int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev) |
| 2239 | { |
| 2240 | unsigned long flags; |
| 2241 | int timeleft; |
| 2242 | struct xhci_virt_device *virt_dev; |
| 2243 | int ret = 0; |
| 2244 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2245 | struct xhci_slot_ctx *slot_ctx; |
| 2246 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 2247 | u64 temp_64; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2248 | |
| 2249 | if (!udev->slot_id) { |
| 2250 | xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id); |
| 2251 | return -EINVAL; |
| 2252 | } |
| 2253 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2254 | virt_dev = xhci->devs[udev->slot_id]; |
| 2255 | |
| 2256 | /* If this is a Set Address to an unconfigured device, setup ep 0 */ |
| 2257 | if (!udev->config) |
| 2258 | xhci_setup_addressable_virt_dev(xhci, udev); |
Sarah Sharp | 2d1ee59 | 2010-07-09 17:08:54 +0200 | [diff] [blame] | 2259 | else |
| 2260 | xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2261 | /* Otherwise, assume the core has the device configured how it wants */ |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 2262 | xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2263 | xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2264 | |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 2265 | spin_lock_irqsave(&xhci->lock, flags); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2266 | ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma, |
| 2267 | udev->slot_id); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2268 | if (ret) { |
| 2269 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2270 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 2271 | return ret; |
| 2272 | } |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 2273 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2274 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2275 | |
| 2276 | /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */ |
| 2277 | timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev, |
| 2278 | USB_CTRL_SET_TIMEOUT); |
| 2279 | /* FIXME: From section 4.3.4: "Software shall be responsible for timing |
| 2280 | * the SetAddress() "recovery interval" required by USB and aborting the |
| 2281 | * command on a timeout. |
| 2282 | */ |
| 2283 | if (timeleft <= 0) { |
| 2284 | xhci_warn(xhci, "%s while waiting for a slot\n", |
| 2285 | timeleft == 0 ? "Timeout" : "Signal"); |
| 2286 | /* FIXME cancel the address device command */ |
| 2287 | return -ETIME; |
| 2288 | } |
| 2289 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2290 | switch (virt_dev->cmd_status) { |
| 2291 | case COMP_CTX_STATE: |
| 2292 | case COMP_EBADSLT: |
| 2293 | xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n", |
| 2294 | udev->slot_id); |
| 2295 | ret = -EINVAL; |
| 2296 | break; |
| 2297 | case COMP_TX_ERR: |
| 2298 | dev_warn(&udev->dev, "Device not responding to set address.\n"); |
| 2299 | ret = -EPROTO; |
| 2300 | break; |
| 2301 | case COMP_SUCCESS: |
| 2302 | xhci_dbg(xhci, "Successful Address Device command\n"); |
| 2303 | break; |
| 2304 | default: |
| 2305 | xhci_err(xhci, "ERROR: unexpected command completion " |
| 2306 | "code 0x%x.\n", virt_dev->cmd_status); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 2307 | xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2308 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2309 | ret = -EINVAL; |
| 2310 | break; |
| 2311 | } |
| 2312 | if (ret) { |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2313 | return ret; |
| 2314 | } |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 2315 | temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); |
| 2316 | xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64); |
| 2317 | xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n", |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2318 | udev->slot_id, |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 2319 | &xhci->dcbaa->dev_context_ptrs[udev->slot_id], |
| 2320 | (unsigned long long) |
| 2321 | xhci->dcbaa->dev_context_ptrs[udev->slot_id]); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 2322 | xhci_dbg(xhci, "Output Context DMA address = %#08llx\n", |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2323 | (unsigned long long)virt_dev->out_ctx->dma); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2324 | xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2325 | xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2326 | xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2327 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2328 | /* |
| 2329 | * USB core uses address 1 for the roothubs, so we add one to the |
| 2330 | * address given back to us by the HC. |
| 2331 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2332 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); |
| 2333 | udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 2334 | /* Zero the input context control for later use */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 2335 | ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); |
| 2336 | ctrl_ctx->add_flags = 0; |
| 2337 | ctrl_ctx->drop_flags = 0; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 2338 | |
| 2339 | xhci_dbg(xhci, "Device address = %d\n", udev->devnum); |
| 2340 | /* XXX Meh, not sure if anyone else but choose_address uses this. */ |
| 2341 | set_bit(udev->devnum, udev->bus->devmap.devicemap); |
| 2342 | |
| 2343 | return 0; |
| 2344 | } |
| 2345 | |
Sarah Sharp | ac1c1b7 | 2009-09-04 10:53:20 -0700 | [diff] [blame] | 2346 | /* Once a hub descriptor is fetched for a device, we need to update the xHC's |
| 2347 | * internal data structures for the device. |
| 2348 | */ |
| 2349 | int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev, |
| 2350 | struct usb_tt *tt, gfp_t mem_flags) |
| 2351 | { |
| 2352 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 2353 | struct xhci_virt_device *vdev; |
| 2354 | struct xhci_command *config_cmd; |
| 2355 | struct xhci_input_control_ctx *ctrl_ctx; |
| 2356 | struct xhci_slot_ctx *slot_ctx; |
| 2357 | unsigned long flags; |
| 2358 | unsigned think_time; |
| 2359 | int ret; |
| 2360 | |
| 2361 | /* Ignore root hubs */ |
| 2362 | if (!hdev->parent) |
| 2363 | return 0; |
| 2364 | |
| 2365 | vdev = xhci->devs[hdev->slot_id]; |
| 2366 | if (!vdev) { |
| 2367 | xhci_warn(xhci, "Cannot update hub desc for unknown device.\n"); |
| 2368 | return -EINVAL; |
| 2369 | } |
Sarah Sharp | a1d78c1 | 2009-12-09 15:59:03 -0800 | [diff] [blame] | 2370 | config_cmd = xhci_alloc_command(xhci, true, true, mem_flags); |
Sarah Sharp | ac1c1b7 | 2009-09-04 10:53:20 -0700 | [diff] [blame] | 2371 | if (!config_cmd) { |
| 2372 | xhci_dbg(xhci, "Could not allocate xHCI command structure.\n"); |
| 2373 | return -ENOMEM; |
| 2374 | } |
| 2375 | |
| 2376 | spin_lock_irqsave(&xhci->lock, flags); |
| 2377 | xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx); |
| 2378 | ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx); |
| 2379 | ctrl_ctx->add_flags |= SLOT_FLAG; |
| 2380 | slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx); |
| 2381 | slot_ctx->dev_info |= DEV_HUB; |
| 2382 | if (tt->multi) |
| 2383 | slot_ctx->dev_info |= DEV_MTT; |
| 2384 | if (xhci->hci_version > 0x95) { |
| 2385 | xhci_dbg(xhci, "xHCI version %x needs hub " |
| 2386 | "TT think time and number of ports\n", |
| 2387 | (unsigned int) xhci->hci_version); |
| 2388 | slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild); |
| 2389 | /* Set TT think time - convert from ns to FS bit times. |
| 2390 | * 0 = 8 FS bit times, 1 = 16 FS bit times, |
| 2391 | * 2 = 24 FS bit times, 3 = 32 FS bit times. |
| 2392 | */ |
| 2393 | think_time = tt->think_time; |
| 2394 | if (think_time != 0) |
| 2395 | think_time = (think_time / 666) - 1; |
| 2396 | slot_ctx->tt_info |= TT_THINK_TIME(think_time); |
| 2397 | } else { |
| 2398 | xhci_dbg(xhci, "xHCI version %x doesn't need hub " |
| 2399 | "TT think time or number of ports\n", |
| 2400 | (unsigned int) xhci->hci_version); |
| 2401 | } |
| 2402 | slot_ctx->dev_state = 0; |
| 2403 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 2404 | |
| 2405 | xhci_dbg(xhci, "Set up %s for hub device.\n", |
| 2406 | (xhci->hci_version > 0x95) ? |
| 2407 | "configure endpoint" : "evaluate context"); |
| 2408 | xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id); |
| 2409 | xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0); |
| 2410 | |
| 2411 | /* Issue and wait for the configure endpoint or |
| 2412 | * evaluate context command. |
| 2413 | */ |
| 2414 | if (xhci->hci_version > 0x95) |
| 2415 | ret = xhci_configure_endpoint(xhci, hdev, config_cmd, |
| 2416 | false, false); |
| 2417 | else |
| 2418 | ret = xhci_configure_endpoint(xhci, hdev, config_cmd, |
| 2419 | true, false); |
| 2420 | |
| 2421 | xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id); |
| 2422 | xhci_dbg_ctx(xhci, vdev->out_ctx, 0); |
| 2423 | |
| 2424 | xhci_free_command(xhci, config_cmd); |
| 2425 | return ret; |
| 2426 | } |
| 2427 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 2428 | int xhci_get_frame(struct usb_hcd *hcd) |
| 2429 | { |
| 2430 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 2431 | /* EHCI mods by the periodic size. Why? */ |
| 2432 | return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3; |
| 2433 | } |
| 2434 | |
| 2435 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 2436 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 2437 | MODULE_LICENSE("GPL"); |
| 2438 | |
| 2439 | static int __init xhci_hcd_init(void) |
| 2440 | { |
| 2441 | #ifdef CONFIG_PCI |
| 2442 | int retval = 0; |
| 2443 | |
| 2444 | retval = xhci_register_pci(); |
| 2445 | |
| 2446 | if (retval < 0) { |
| 2447 | printk(KERN_DEBUG "Problem registering PCI driver."); |
| 2448 | return retval; |
| 2449 | } |
| 2450 | #endif |
Sarah Sharp | 9844197 | 2009-05-14 11:44:18 -0700 | [diff] [blame] | 2451 | /* |
| 2452 | * Check the compiler generated sizes of structures that must be laid |
| 2453 | * out in specific ways for hardware access. |
| 2454 | */ |
| 2455 | BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); |
| 2456 | BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8); |
| 2457 | BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8); |
| 2458 | /* xhci_device_control has eight fields, and also |
| 2459 | * embeds one xhci_slot_ctx and 31 xhci_ep_ctx |
| 2460 | */ |
Sarah Sharp | 9844197 | 2009-05-14 11:44:18 -0700 | [diff] [blame] | 2461 | BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8); |
| 2462 | BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8); |
| 2463 | BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8); |
| 2464 | BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8); |
| 2465 | BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8); |
| 2466 | /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */ |
| 2467 | BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8); |
| 2468 | BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 2469 | return 0; |
| 2470 | } |
| 2471 | module_init(xhci_hcd_init); |
| 2472 | |
| 2473 | static void __exit xhci_hcd_cleanup(void) |
| 2474 | { |
| 2475 | #ifdef CONFIG_PCI |
| 2476 | xhci_unregister_pci(); |
| 2477 | #endif |
| 2478 | } |
| 2479 | module_exit(xhci_hcd_cleanup); |