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 | |
| 23 | #include <linux/irq.h> |
| 24 | #include <linux/module.h> |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 25 | #include <linux/moduleparam.h> |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 26 | |
| 27 | #include "xhci.h" |
| 28 | |
| 29 | #define DRIVER_AUTHOR "Sarah Sharp" |
| 30 | #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver" |
| 31 | |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 32 | /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */ |
| 33 | static int link_quirk; |
| 34 | module_param(link_quirk, int, S_IRUGO | S_IWUSR); |
| 35 | MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB"); |
| 36 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 37 | /* TODO: copied from ehci-hcd.c - can this be refactored? */ |
| 38 | /* |
| 39 | * handshake - spin reading hc until handshake completes or fails |
| 40 | * @ptr: address of hc register to be read |
| 41 | * @mask: bits to look at in result of read |
| 42 | * @done: value of those bits when handshake succeeds |
| 43 | * @usec: timeout in microseconds |
| 44 | * |
| 45 | * Returns negative errno, or zero on success |
| 46 | * |
| 47 | * Success happens when the "mask" bits have the specified value (hardware |
| 48 | * handshake done). There are two failure modes: "usec" have passed (major |
| 49 | * hardware flakeout), or the register reads as all-ones (hardware removed). |
| 50 | */ |
| 51 | static int handshake(struct xhci_hcd *xhci, void __iomem *ptr, |
| 52 | u32 mask, u32 done, int usec) |
| 53 | { |
| 54 | u32 result; |
| 55 | |
| 56 | do { |
| 57 | result = xhci_readl(xhci, ptr); |
| 58 | if (result == ~(u32)0) /* card removed */ |
| 59 | return -ENODEV; |
| 60 | result &= mask; |
| 61 | if (result == done) |
| 62 | return 0; |
| 63 | udelay(1); |
| 64 | usec--; |
| 65 | } while (usec > 0); |
| 66 | return -ETIMEDOUT; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Force HC into halt state. |
| 71 | * |
| 72 | * Disable any IRQs and clear the run/stop bit. |
| 73 | * HC will complete any current and actively pipelined transactions, and |
| 74 | * should halt within 16 microframes of the run/stop bit being cleared. |
| 75 | * Read HC Halted bit in the status register to see when the HC is finished. |
| 76 | * XXX: shouldn't we set HC_STATE_HALT here somewhere? |
| 77 | */ |
| 78 | int xhci_halt(struct xhci_hcd *xhci) |
| 79 | { |
| 80 | u32 halted; |
| 81 | u32 cmd; |
| 82 | u32 mask; |
| 83 | |
| 84 | xhci_dbg(xhci, "// Halt the HC\n"); |
| 85 | /* Disable all interrupts from the host controller */ |
| 86 | mask = ~(XHCI_IRQS); |
| 87 | halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT; |
| 88 | if (!halted) |
| 89 | mask &= ~CMD_RUN; |
| 90 | |
| 91 | cmd = xhci_readl(xhci, &xhci->op_regs->command); |
| 92 | cmd &= mask; |
| 93 | xhci_writel(xhci, cmd, &xhci->op_regs->command); |
| 94 | |
| 95 | return handshake(xhci, &xhci->op_regs->status, |
| 96 | STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Reset a halted HC, and set the internal HC state to HC_STATE_HALT. |
| 101 | * |
| 102 | * This resets pipelines, timers, counters, state machines, etc. |
| 103 | * Transactions will be terminated immediately, and operational registers |
| 104 | * will be set to their defaults. |
| 105 | */ |
| 106 | int xhci_reset(struct xhci_hcd *xhci) |
| 107 | { |
| 108 | u32 command; |
| 109 | u32 state; |
| 110 | |
| 111 | state = xhci_readl(xhci, &xhci->op_regs->status); |
Sarah Sharp | d3512f6 | 2009-07-27 12:03:50 -0700 | [diff] [blame] | 112 | if ((state & STS_HALT) == 0) { |
| 113 | xhci_warn(xhci, "Host controller not halted, aborting reset.\n"); |
| 114 | return 0; |
| 115 | } |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 116 | |
| 117 | xhci_dbg(xhci, "// Reset the HC\n"); |
| 118 | command = xhci_readl(xhci, &xhci->op_regs->command); |
| 119 | command |= CMD_RESET; |
| 120 | xhci_writel(xhci, command, &xhci->op_regs->command); |
| 121 | /* XXX: Why does EHCI set this here? Shouldn't other code do this? */ |
| 122 | xhci_to_hcd(xhci)->state = HC_STATE_HALT; |
| 123 | |
| 124 | return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Stop the HC from processing the endpoint queues. |
| 129 | */ |
| 130 | static void xhci_quiesce(struct xhci_hcd *xhci) |
| 131 | { |
| 132 | /* |
| 133 | * Queues are per endpoint, so we need to disable an endpoint or slot. |
| 134 | * |
| 135 | * To disable a slot, we need to insert a disable slot command on the |
| 136 | * command ring and ring the doorbell. This will also free any internal |
| 137 | * resources associated with the slot (which might not be what we want). |
| 138 | * |
| 139 | * A Release Endpoint command sounds better - doesn't free internal HC |
| 140 | * memory, but removes the endpoints from the schedule and releases the |
| 141 | * bandwidth, disables the doorbells, and clears the endpoint enable |
| 142 | * flag. Usually used prior to a set interface command. |
| 143 | * |
| 144 | * TODO: Implement after command ring code is done. |
| 145 | */ |
| 146 | BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state)); |
| 147 | xhci_dbg(xhci, "Finished quiescing -- code not written yet\n"); |
| 148 | } |
| 149 | |
| 150 | #if 0 |
| 151 | /* Set up MSI-X table for entry 0 (may claim other entries later) */ |
| 152 | static int xhci_setup_msix(struct xhci_hcd *xhci) |
| 153 | { |
| 154 | int ret; |
| 155 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
| 156 | |
| 157 | xhci->msix_count = 0; |
| 158 | /* XXX: did I do this right? ixgbe does kcalloc for more than one */ |
| 159 | xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL); |
| 160 | if (!xhci->msix_entries) { |
| 161 | xhci_err(xhci, "Failed to allocate MSI-X entries\n"); |
| 162 | return -ENOMEM; |
| 163 | } |
| 164 | xhci->msix_entries[0].entry = 0; |
| 165 | |
| 166 | ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count); |
| 167 | if (ret) { |
| 168 | xhci_err(xhci, "Failed to enable MSI-X\n"); |
| 169 | goto free_entries; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Pass the xhci pointer value as the request_irq "cookie". |
| 174 | * If more irqs are added, this will need to be unique for each one. |
| 175 | */ |
| 176 | ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0, |
| 177 | "xHCI", xhci_to_hcd(xhci)); |
| 178 | if (ret) { |
| 179 | xhci_err(xhci, "Failed to allocate MSI-X interrupt\n"); |
| 180 | goto disable_msix; |
| 181 | } |
| 182 | xhci_dbg(xhci, "Finished setting up MSI-X\n"); |
| 183 | return 0; |
| 184 | |
| 185 | disable_msix: |
| 186 | pci_disable_msix(pdev); |
| 187 | free_entries: |
| 188 | kfree(xhci->msix_entries); |
| 189 | xhci->msix_entries = NULL; |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | /* XXX: code duplication; can xhci_setup_msix call this? */ |
| 194 | /* Free any IRQs and disable MSI-X */ |
| 195 | static void xhci_cleanup_msix(struct xhci_hcd *xhci) |
| 196 | { |
| 197 | struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller); |
| 198 | if (!xhci->msix_entries) |
| 199 | return; |
| 200 | |
| 201 | free_irq(xhci->msix_entries[0].vector, xhci); |
| 202 | pci_disable_msix(pdev); |
| 203 | kfree(xhci->msix_entries); |
| 204 | xhci->msix_entries = NULL; |
| 205 | xhci_dbg(xhci, "Finished cleaning up MSI-X\n"); |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | /* |
| 210 | * Initialize memory for HCD and xHC (one-time init). |
| 211 | * |
| 212 | * Program the PAGESIZE register, initialize the device context array, create |
| 213 | * device contexts (?), set up a command ring segment (or two?), create event |
| 214 | * ring (one for now). |
| 215 | */ |
| 216 | int xhci_init(struct usb_hcd *hcd) |
| 217 | { |
| 218 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 219 | int retval = 0; |
| 220 | |
| 221 | xhci_dbg(xhci, "xhci_init\n"); |
| 222 | spin_lock_init(&xhci->lock); |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 223 | if (link_quirk) { |
| 224 | xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n"); |
| 225 | xhci->quirks |= XHCI_LINK_TRB_QUIRK; |
| 226 | } else { |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 227 | xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n"); |
Sarah Sharp | b0567b3 | 2009-08-07 14:04:36 -0700 | [diff] [blame] | 228 | } |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 229 | retval = xhci_mem_init(xhci, GFP_KERNEL); |
| 230 | xhci_dbg(xhci, "Finished xhci_init\n"); |
| 231 | |
| 232 | return retval; |
| 233 | } |
| 234 | |
| 235 | /* |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 236 | * Called in interrupt context when there might be work |
| 237 | * queued on the event ring |
| 238 | * |
| 239 | * xhci->lock must be held by caller. |
| 240 | */ |
| 241 | static void xhci_work(struct xhci_hcd *xhci) |
| 242 | { |
| 243 | u32 temp; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 244 | u64 temp_64; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 245 | |
| 246 | /* |
| 247 | * Clear the op reg interrupt status first, |
| 248 | * so we can receive interrupts from other MSI-X interrupters. |
| 249 | * Write 1 to clear the interrupt status. |
| 250 | */ |
| 251 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 252 | temp |= STS_EINT; |
| 253 | xhci_writel(xhci, temp, &xhci->op_regs->status); |
| 254 | /* FIXME when MSI-X is supported and there are multiple vectors */ |
| 255 | /* Clear the MSI-X event interrupt status */ |
| 256 | |
| 257 | /* Acknowledge the interrupt */ |
| 258 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 259 | temp |= 0x3; |
| 260 | xhci_writel(xhci, temp, &xhci->ir_set->irq_pending); |
| 261 | /* Flush posted writes */ |
| 262 | xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 263 | |
| 264 | /* FIXME this should be a delayed service routine that clears the EHB */ |
Stephen Rothwell | b7258a4 | 2009-04-29 19:02:47 -0700 | [diff] [blame] | 265 | xhci_handle_event(xhci); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 266 | |
Sarah Sharp | 2d83109 | 2009-07-27 12:03:40 -0700 | [diff] [blame] | 267 | /* 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] | 268 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
Sarah Sharp | 2d83109 | 2009-07-27 12:03:40 -0700 | [diff] [blame] | 269 | 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] | 270 | /* Flush posted writes -- FIXME is this necessary? */ |
| 271 | xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 272 | } |
| 273 | |
| 274 | /*-------------------------------------------------------------------------*/ |
| 275 | |
| 276 | /* |
| 277 | * xHCI spec says we can get an interrupt, and if the HC has an error condition, |
| 278 | * we might get bad data out of the event ring. Section 4.10.2.7 has a list of |
| 279 | * indicators of an event TRB error, but we check the status *first* to be safe. |
| 280 | */ |
| 281 | irqreturn_t xhci_irq(struct usb_hcd *hcd) |
| 282 | { |
| 283 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 284 | u32 temp, temp2; |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 285 | union xhci_trb *trb; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 286 | |
| 287 | spin_lock(&xhci->lock); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 288 | trb = xhci->event_ring->dequeue; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 289 | /* Check if the xHC generated the interrupt, or the irq is shared */ |
| 290 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 291 | temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
Sarah Sharp | fcf8f57 | 2009-07-27 12:04:01 -0700 | [diff] [blame] | 292 | if (temp == 0xffffffff && temp2 == 0xffffffff) |
| 293 | goto hw_died; |
| 294 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 295 | if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) { |
| 296 | spin_unlock(&xhci->lock); |
| 297 | return IRQ_NONE; |
| 298 | } |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 299 | xhci_dbg(xhci, "op reg status = %08x\n", temp); |
| 300 | xhci_dbg(xhci, "ir set irq_pending = %08x\n", temp2); |
| 301 | xhci_dbg(xhci, "Event ring dequeue ptr:\n"); |
| 302 | xhci_dbg(xhci, "@%llx %08x %08x %08x %08x\n", |
| 303 | (unsigned long long)xhci_trb_virt_to_dma(xhci->event_ring->deq_seg, trb), |
| 304 | lower_32_bits(trb->link.segment_ptr), |
| 305 | upper_32_bits(trb->link.segment_ptr), |
| 306 | (unsigned int) trb->link.intr_target, |
| 307 | (unsigned int) trb->link.control); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 308 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 309 | if (temp & STS_FATAL) { |
| 310 | xhci_warn(xhci, "WARNING: Host System Error\n"); |
| 311 | xhci_halt(xhci); |
Sarah Sharp | fcf8f57 | 2009-07-27 12:04:01 -0700 | [diff] [blame] | 312 | hw_died: |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 313 | xhci_to_hcd(xhci)->state = HC_STATE_HALT; |
Sarah Sharp | c96a2b8 | 2009-04-29 19:05:40 -0700 | [diff] [blame] | 314 | spin_unlock(&xhci->lock); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 315 | return -ESHUTDOWN; |
| 316 | } |
| 317 | |
| 318 | xhci_work(xhci); |
| 319 | spin_unlock(&xhci->lock); |
| 320 | |
| 321 | return IRQ_HANDLED; |
| 322 | } |
| 323 | |
| 324 | #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 325 | void xhci_event_ring_work(unsigned long arg) |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 326 | { |
| 327 | unsigned long flags; |
| 328 | int temp; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 329 | u64 temp_64; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 330 | struct xhci_hcd *xhci = (struct xhci_hcd *) arg; |
| 331 | int i, j; |
| 332 | |
| 333 | xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies); |
| 334 | |
| 335 | spin_lock_irqsave(&xhci->lock, flags); |
| 336 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 337 | xhci_dbg(xhci, "op reg status = 0x%x\n", temp); |
| 338 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 339 | xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp); |
| 340 | xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled); |
| 341 | xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask); |
| 342 | xhci->error_bitmask = 0; |
| 343 | xhci_dbg(xhci, "Event ring:\n"); |
| 344 | xhci_debug_segment(xhci, xhci->event_ring->deq_seg); |
| 345 | xhci_dbg_ring_ptrs(xhci, xhci->event_ring); |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 346 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
| 347 | temp_64 &= ~ERST_PTR_MASK; |
| 348 | xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 349 | xhci_dbg(xhci, "Command ring:\n"); |
| 350 | xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg); |
| 351 | xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring); |
| 352 | xhci_dbg_cmd_ptrs(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 353 | for (i = 0; i < MAX_HC_SLOTS; ++i) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 354 | if (!xhci->devs[i]) |
| 355 | continue; |
| 356 | for (j = 0; j < 31; ++j) { |
| 357 | struct xhci_ring *ring = xhci->devs[i]->eps[j].ring; |
| 358 | if (!ring) |
| 359 | continue; |
| 360 | xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j); |
| 361 | xhci_debug_segment(xhci, ring->deq_seg); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 364 | |
| 365 | if (xhci->noops_submitted != NUM_TEST_NOOPS) |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 366 | if (xhci_setup_one_noop(xhci)) |
| 367 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 368 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 369 | |
| 370 | if (!xhci->zombie) |
| 371 | mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ); |
| 372 | else |
| 373 | xhci_dbg(xhci, "Quit polling the event ring.\n"); |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | /* |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 378 | * Start the HC after it was halted. |
| 379 | * |
| 380 | * This function is called by the USB core when the HC driver is added. |
| 381 | * Its opposite is xhci_stop(). |
| 382 | * |
| 383 | * xhci_init() must be called once before this function can be called. |
| 384 | * Reset the HC, enable device slot contexts, program DCBAAP, and |
| 385 | * set command ring pointer and event ring pointer. |
| 386 | * |
| 387 | * Setup MSI-X vectors and enable interrupts. |
| 388 | */ |
| 389 | int xhci_run(struct usb_hcd *hcd) |
| 390 | { |
| 391 | u32 temp; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 392 | u64 temp_64; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 393 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 394 | void (*doorbell)(struct xhci_hcd *) = NULL; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 395 | |
Sarah Sharp | 0f2a793 | 2009-04-27 19:57:12 -0700 | [diff] [blame] | 396 | hcd->uses_new_polling = 1; |
| 397 | hcd->poll_rh = 0; |
| 398 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 399 | xhci_dbg(xhci, "xhci_run\n"); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 400 | #if 0 /* FIXME: MSI not setup yet */ |
| 401 | /* Do this at the very last minute */ |
| 402 | ret = xhci_setup_msix(xhci); |
| 403 | if (!ret) |
| 404 | return ret; |
| 405 | |
| 406 | return -ENOSYS; |
| 407 | #endif |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 408 | #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING |
| 409 | init_timer(&xhci->event_ring_timer); |
| 410 | xhci->event_ring_timer.data = (unsigned long) xhci; |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 411 | xhci->event_ring_timer.function = xhci_event_ring_work; |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 412 | /* Poll the event ring */ |
| 413 | xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ; |
| 414 | xhci->zombie = 0; |
| 415 | xhci_dbg(xhci, "Setting event ring polling timer\n"); |
| 416 | add_timer(&xhci->event_ring_timer); |
| 417 | #endif |
| 418 | |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 419 | xhci_dbg(xhci, "Command ring memory map follows:\n"); |
| 420 | xhci_debug_ring(xhci, xhci->cmd_ring); |
| 421 | xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring); |
| 422 | xhci_dbg_cmd_ptrs(xhci); |
| 423 | |
| 424 | xhci_dbg(xhci, "ERST memory map follows:\n"); |
| 425 | xhci_dbg_erst(xhci, &xhci->erst); |
| 426 | xhci_dbg(xhci, "Event ring:\n"); |
| 427 | xhci_debug_ring(xhci, xhci->event_ring); |
| 428 | xhci_dbg_ring_ptrs(xhci, xhci->event_ring); |
| 429 | temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue); |
| 430 | temp_64 &= ~ERST_PTR_MASK; |
| 431 | xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64); |
| 432 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 433 | xhci_dbg(xhci, "// Set the interrupt modulation register\n"); |
| 434 | temp = xhci_readl(xhci, &xhci->ir_set->irq_control); |
Sarah Sharp | a4d8830 | 2009-05-14 11:44:26 -0700 | [diff] [blame] | 435 | temp &= ~ER_IRQ_INTERVAL_MASK; |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 436 | temp |= (u32) 160; |
| 437 | xhci_writel(xhci, temp, &xhci->ir_set->irq_control); |
| 438 | |
| 439 | /* Set the HCD state before we enable the irqs */ |
| 440 | hcd->state = HC_STATE_RUNNING; |
| 441 | temp = xhci_readl(xhci, &xhci->op_regs->command); |
| 442 | temp |= (CMD_EIE); |
| 443 | xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n", |
| 444 | temp); |
| 445 | xhci_writel(xhci, temp, &xhci->op_regs->command); |
| 446 | |
| 447 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 448 | xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n", |
| 449 | xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp)); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 450 | xhci_writel(xhci, ER_IRQ_ENABLE(temp), |
| 451 | &xhci->ir_set->irq_pending); |
| 452 | xhci_print_ir_set(xhci, xhci->ir_set, 0); |
| 453 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 454 | if (NUM_TEST_NOOPS > 0) |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 455 | doorbell = xhci_setup_one_noop(xhci); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 456 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 457 | temp = xhci_readl(xhci, &xhci->op_regs->command); |
| 458 | temp |= (CMD_RUN); |
| 459 | xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n", |
| 460 | temp); |
| 461 | xhci_writel(xhci, temp, &xhci->op_regs->command); |
| 462 | /* Flush PCI posted writes */ |
| 463 | temp = xhci_readl(xhci, &xhci->op_regs->command); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 464 | xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp); |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 465 | if (doorbell) |
| 466 | (*doorbell)(xhci); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 467 | |
| 468 | xhci_dbg(xhci, "Finished xhci_run\n"); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Stop xHCI driver. |
| 474 | * |
| 475 | * This function is called by the USB core when the HC driver is removed. |
| 476 | * Its opposite is xhci_run(). |
| 477 | * |
| 478 | * Disable device contexts, disable IRQs, and quiesce the HC. |
| 479 | * Reset the HC, finish any completed transactions, and cleanup memory. |
| 480 | */ |
| 481 | void xhci_stop(struct usb_hcd *hcd) |
| 482 | { |
| 483 | u32 temp; |
| 484 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 485 | |
| 486 | spin_lock_irq(&xhci->lock); |
| 487 | if (HC_IS_RUNNING(hcd->state)) |
| 488 | xhci_quiesce(xhci); |
| 489 | xhci_halt(xhci); |
| 490 | xhci_reset(xhci); |
| 491 | spin_unlock_irq(&xhci->lock); |
| 492 | |
| 493 | #if 0 /* No MSI yet */ |
| 494 | xhci_cleanup_msix(xhci); |
| 495 | #endif |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 496 | #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING |
| 497 | /* Tell the event ring poll function not to reschedule */ |
| 498 | xhci->zombie = 1; |
| 499 | del_timer_sync(&xhci->event_ring_timer); |
| 500 | #endif |
| 501 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 502 | xhci_dbg(xhci, "// Disabling event ring interrupts\n"); |
| 503 | temp = xhci_readl(xhci, &xhci->op_regs->status); |
| 504 | xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status); |
| 505 | temp = xhci_readl(xhci, &xhci->ir_set->irq_pending); |
| 506 | xhci_writel(xhci, ER_IRQ_DISABLE(temp), |
| 507 | &xhci->ir_set->irq_pending); |
| 508 | xhci_print_ir_set(xhci, xhci->ir_set, 0); |
| 509 | |
| 510 | xhci_dbg(xhci, "cleaning up memory\n"); |
| 511 | xhci_mem_cleanup(xhci); |
| 512 | xhci_dbg(xhci, "xhci_stop completed - status = %x\n", |
| 513 | xhci_readl(xhci, &xhci->op_regs->status)); |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Shutdown HC (not bus-specific) |
| 518 | * |
| 519 | * This is called when the machine is rebooting or halting. We assume that the |
| 520 | * machine will be powered off, and the HC's internal state will be reset. |
| 521 | * Don't bother to free memory. |
| 522 | */ |
| 523 | void xhci_shutdown(struct usb_hcd *hcd) |
| 524 | { |
| 525 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 526 | |
| 527 | spin_lock_irq(&xhci->lock); |
| 528 | xhci_halt(xhci); |
| 529 | spin_unlock_irq(&xhci->lock); |
| 530 | |
| 531 | #if 0 |
| 532 | xhci_cleanup_msix(xhci); |
| 533 | #endif |
| 534 | |
| 535 | xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n", |
| 536 | xhci_readl(xhci, &xhci->op_regs->status)); |
| 537 | } |
| 538 | |
Sarah Sharp | 7f84eef | 2009-04-27 19:53:56 -0700 | [diff] [blame] | 539 | /*-------------------------------------------------------------------------*/ |
| 540 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 541 | /** |
| 542 | * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and |
| 543 | * HCDs. Find the index for an endpoint given its descriptor. Use the return |
| 544 | * value to right shift 1 for the bitmask. |
| 545 | * |
| 546 | * Index = (epnum * 2) + direction - 1, |
| 547 | * where direction = 0 for OUT, 1 for IN. |
| 548 | * For control endpoints, the IN index is used (OUT index is unused), so |
| 549 | * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) |
| 550 | */ |
| 551 | unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc) |
| 552 | { |
| 553 | unsigned int index; |
| 554 | if (usb_endpoint_xfer_control(desc)) |
| 555 | index = (unsigned int) (usb_endpoint_num(desc)*2); |
| 556 | else |
| 557 | index = (unsigned int) (usb_endpoint_num(desc)*2) + |
| 558 | (usb_endpoint_dir_in(desc) ? 1 : 0) - 1; |
| 559 | return index; |
| 560 | } |
| 561 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 562 | /* Find the flag for this endpoint (for use in the control context). Use the |
| 563 | * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is |
| 564 | * bit 1, etc. |
| 565 | */ |
| 566 | unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc) |
| 567 | { |
| 568 | return 1 << (xhci_get_endpoint_index(desc) + 1); |
| 569 | } |
| 570 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 571 | /* Find the flag for this endpoint (for use in the control context). Use the |
| 572 | * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is |
| 573 | * bit 1, etc. |
| 574 | */ |
| 575 | unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index) |
| 576 | { |
| 577 | return 1 << (ep_index + 1); |
| 578 | } |
| 579 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 580 | /* Compute the last valid endpoint context index. Basically, this is the |
| 581 | * endpoint index plus one. For slot contexts with more than valid endpoint, |
| 582 | * we find the most significant bit set in the added contexts flags. |
| 583 | * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000 |
| 584 | * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one. |
| 585 | */ |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 586 | unsigned int xhci_last_valid_endpoint(u32 added_ctxs) |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 587 | { |
| 588 | return fls(added_ctxs) - 1; |
| 589 | } |
| 590 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 591 | /* Returns 1 if the arguments are OK; |
| 592 | * returns 0 this is a root hub; returns -EINVAL for NULL pointers. |
| 593 | */ |
| 594 | int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev, |
| 595 | struct usb_host_endpoint *ep, int check_ep, const char *func) { |
| 596 | if (!hcd || (check_ep && !ep) || !udev) { |
| 597 | printk(KERN_DEBUG "xHCI %s called with invalid args\n", |
| 598 | func); |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | if (!udev->parent) { |
| 602 | printk(KERN_DEBUG "xHCI %s called for root hub\n", |
| 603 | func); |
| 604 | return 0; |
| 605 | } |
| 606 | if (!udev->slot_id) { |
| 607 | printk(KERN_DEBUG "xHCI %s called with unaddressed device\n", |
| 608 | func); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | return 1; |
| 612 | } |
| 613 | |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 614 | static int xhci_configure_endpoint(struct xhci_hcd *xhci, |
| 615 | struct usb_device *udev, struct xhci_virt_device *virt_dev, |
| 616 | bool ctx_change); |
| 617 | |
| 618 | /* |
| 619 | * Full speed devices may have a max packet size greater than 8 bytes, but the |
| 620 | * USB core doesn't know that until it reads the first 8 bytes of the |
| 621 | * descriptor. If the usb_device's max packet size changes after that point, |
| 622 | * we need to issue an evaluate context command and wait on it. |
| 623 | */ |
| 624 | static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id, |
| 625 | unsigned int ep_index, struct urb *urb) |
| 626 | { |
| 627 | struct xhci_container_ctx *in_ctx; |
| 628 | struct xhci_container_ctx *out_ctx; |
| 629 | struct xhci_input_control_ctx *ctrl_ctx; |
| 630 | struct xhci_ep_ctx *ep_ctx; |
| 631 | int max_packet_size; |
| 632 | int hw_max_packet_size; |
| 633 | int ret = 0; |
| 634 | |
| 635 | out_ctx = xhci->devs[slot_id]->out_ctx; |
| 636 | ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); |
| 637 | hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2); |
| 638 | max_packet_size = urb->dev->ep0.desc.wMaxPacketSize; |
| 639 | if (hw_max_packet_size != max_packet_size) { |
| 640 | xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n"); |
| 641 | xhci_dbg(xhci, "Max packet size in usb_device = %d\n", |
| 642 | max_packet_size); |
| 643 | xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n", |
| 644 | hw_max_packet_size); |
| 645 | xhci_dbg(xhci, "Issuing evaluate context command.\n"); |
| 646 | |
| 647 | /* Set up the modified control endpoint 0 */ |
| 648 | xhci_endpoint_copy(xhci, xhci->devs[slot_id], ep_index); |
| 649 | in_ctx = xhci->devs[slot_id]->in_ctx; |
| 650 | ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); |
| 651 | ep_ctx->ep_info2 &= ~MAX_PACKET_MASK; |
| 652 | ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size); |
| 653 | |
| 654 | /* Set up the input context flags for the command */ |
| 655 | /* FIXME: This won't work if a non-default control endpoint |
| 656 | * changes max packet sizes. |
| 657 | */ |
| 658 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
| 659 | ctrl_ctx->add_flags = EP0_FLAG; |
| 660 | ctrl_ctx->drop_flags = 0; |
| 661 | |
| 662 | xhci_dbg(xhci, "Slot %d input context\n", slot_id); |
| 663 | xhci_dbg_ctx(xhci, in_ctx, ep_index); |
| 664 | xhci_dbg(xhci, "Slot %d output context\n", slot_id); |
| 665 | xhci_dbg_ctx(xhci, out_ctx, ep_index); |
| 666 | |
| 667 | ret = xhci_configure_endpoint(xhci, urb->dev, |
| 668 | xhci->devs[slot_id], true); |
| 669 | |
| 670 | /* Clean up the input context for later use by bandwidth |
| 671 | * functions. |
| 672 | */ |
| 673 | ctrl_ctx->add_flags = SLOT_FLAG; |
| 674 | } |
| 675 | return ret; |
| 676 | } |
| 677 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 678 | /* |
| 679 | * non-error returns are a promise to giveback() the urb later |
| 680 | * we drop ownership so next owner (or urb unlink) can get it |
| 681 | */ |
| 682 | int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags) |
| 683 | { |
| 684 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 685 | unsigned long flags; |
| 686 | int ret = 0; |
| 687 | unsigned int slot_id, ep_index; |
| 688 | |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 689 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 690 | if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0) |
| 691 | return -EINVAL; |
| 692 | |
| 693 | slot_id = urb->dev->slot_id; |
| 694 | ep_index = xhci_get_endpoint_index(&urb->ep->desc); |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 695 | |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 696 | if (!xhci->devs || !xhci->devs[slot_id]) { |
| 697 | if (!in_interrupt()) |
| 698 | 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] | 699 | ret = -EINVAL; |
| 700 | goto exit; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 701 | } |
| 702 | if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) { |
| 703 | if (!in_interrupt()) |
| 704 | xhci_dbg(xhci, "urb submitted during PCI suspend\n"); |
| 705 | ret = -ESHUTDOWN; |
| 706 | goto exit; |
| 707 | } |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 708 | if (usb_endpoint_xfer_control(&urb->ep->desc)) { |
| 709 | /* Check to see if the max packet size for the default control |
| 710 | * endpoint changed during FS device enumeration |
| 711 | */ |
| 712 | if (urb->dev->speed == USB_SPEED_FULL) { |
| 713 | ret = xhci_check_maxpacket(xhci, slot_id, |
| 714 | ep_index, urb); |
| 715 | if (ret < 0) |
| 716 | return ret; |
| 717 | } |
| 718 | |
Sarah Sharp | b11069f | 2009-07-27 12:03:23 -0700 | [diff] [blame] | 719 | /* We have a spinlock and interrupts disabled, so we must pass |
| 720 | * atomic context to this function, which may allocate memory. |
| 721 | */ |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 722 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | b11069f | 2009-07-27 12:03:23 -0700 | [diff] [blame] | 723 | ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb, |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 724 | slot_id, ep_index); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 725 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 726 | } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) { |
| 727 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | b11069f | 2009-07-27 12:03:23 -0700 | [diff] [blame] | 728 | ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb, |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 729 | slot_id, ep_index); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 730 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 624defa | 2009-09-02 12:14:28 -0700 | [diff] [blame] | 731 | } else if (usb_endpoint_xfer_int(&urb->ep->desc)) { |
| 732 | spin_lock_irqsave(&xhci->lock, flags); |
| 733 | ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb, |
| 734 | slot_id, ep_index); |
| 735 | spin_unlock_irqrestore(&xhci->lock, flags); |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 736 | } else { |
Sarah Sharp | b10de14 | 2009-04-27 19:58:50 -0700 | [diff] [blame] | 737 | ret = -EINVAL; |
Sarah Sharp | 2d3f1fa | 2009-08-07 14:04:49 -0700 | [diff] [blame] | 738 | } |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 739 | exit: |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 740 | return ret; |
| 741 | } |
| 742 | |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 743 | /* |
| 744 | * Remove the URB's TD from the endpoint ring. This may cause the HC to stop |
| 745 | * USB transfers, potentially stopping in the middle of a TRB buffer. The HC |
| 746 | * should pick up where it left off in the TD, unless a Set Transfer Ring |
| 747 | * Dequeue Pointer is issued. |
| 748 | * |
| 749 | * The TRBs that make up the buffers for the canceled URB will be "removed" from |
| 750 | * the ring. Since the ring is a contiguous structure, they can't be physically |
| 751 | * removed. Instead, there are two options: |
| 752 | * |
| 753 | * 1) If the HC is in the middle of processing the URB to be canceled, we |
| 754 | * simply move the ring's dequeue pointer past those TRBs using the Set |
| 755 | * Transfer Ring Dequeue Pointer command. This will be the common case, |
| 756 | * when drivers timeout on the last submitted URB and attempt to cancel. |
| 757 | * |
| 758 | * 2) If the HC is in the middle of a different TD, we turn the TRBs into a |
| 759 | * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The |
| 760 | * HC will need to invalidate the any TRBs it has cached after the stop |
| 761 | * endpoint command, as noted in the xHCI 0.95 errata. |
| 762 | * |
| 763 | * 3) The TD may have completed by the time the Stop Endpoint Command |
| 764 | * completes, so software needs to handle that case too. |
| 765 | * |
| 766 | * This function should protect against the TD enqueueing code ringing the |
| 767 | * doorbell while this code is waiting for a Stop Endpoint command to complete. |
| 768 | * It also needs to account for multiple cancellations on happening at the same |
| 769 | * time for the same endpoint. |
| 770 | * |
| 771 | * Note that this function can be called in any context, or so says |
| 772 | * usb_hcd_unlink_urb() |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 773 | */ |
| 774 | int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status) |
| 775 | { |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 776 | unsigned long flags; |
| 777 | int ret; |
| 778 | struct xhci_hcd *xhci; |
| 779 | struct xhci_td *td; |
| 780 | unsigned int ep_index; |
| 781 | struct xhci_ring *ep_ring; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 782 | struct xhci_virt_ep *ep; |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 783 | |
| 784 | xhci = hcd_to_xhci(hcd); |
| 785 | spin_lock_irqsave(&xhci->lock, flags); |
| 786 | /* Make sure the URB hasn't completed or been unlinked already */ |
| 787 | ret = usb_hcd_check_unlink_urb(hcd, urb, status); |
| 788 | if (ret || !urb->hcpriv) |
| 789 | goto done; |
| 790 | |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 791 | xhci_dbg(xhci, "Cancel URB %p\n", urb); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 792 | xhci_dbg(xhci, "Event ring:\n"); |
| 793 | xhci_debug_ring(xhci, xhci->event_ring); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 794 | ep_index = xhci_get_endpoint_index(&urb->ep->desc); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 795 | ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index]; |
| 796 | ep_ring = ep->ring; |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 797 | xhci_dbg(xhci, "Endpoint ring:\n"); |
| 798 | xhci_debug_ring(xhci, ep_ring); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 799 | td = (struct xhci_td *) urb->hcpriv; |
| 800 | |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 801 | ep->cancels_pending++; |
| 802 | list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 803 | /* Queue a stop endpoint command, but only if this is |
| 804 | * the first cancellation to be handled. |
| 805 | */ |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 806 | if (ep->cancels_pending == 1) { |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 807 | xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index); |
| 808 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | ae63674 | 2009-04-29 19:02:31 -0700 | [diff] [blame] | 809 | } |
| 810 | done: |
| 811 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 812 | return ret; |
Sarah Sharp | d0e96f5 | 2009-04-27 19:58:01 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 815 | /* Drop an endpoint from a new bandwidth configuration for this device. |
| 816 | * Only one call to this function is allowed per endpoint before |
| 817 | * check_bandwidth() or reset_bandwidth() must be called. |
| 818 | * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will |
| 819 | * add the endpoint to the schedule with possibly new parameters denoted by a |
| 820 | * different endpoint descriptor in usb_host_endpoint. |
| 821 | * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is |
| 822 | * not allowed. |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 823 | * |
| 824 | * The USB core will not allow URBs to be queued to an endpoint that is being |
| 825 | * disabled, so there's no need for mutual exclusion to protect |
| 826 | * the xhci->devs[slot_id] structure. |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 827 | */ |
| 828 | int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev, |
| 829 | struct usb_host_endpoint *ep) |
| 830 | { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 831 | struct xhci_hcd *xhci; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 832 | struct xhci_container_ctx *in_ctx, *out_ctx; |
| 833 | struct xhci_input_control_ctx *ctrl_ctx; |
| 834 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 835 | unsigned int last_ctx; |
| 836 | unsigned int ep_index; |
| 837 | struct xhci_ep_ctx *ep_ctx; |
| 838 | u32 drop_flag; |
| 839 | u32 new_add_flags, new_drop_flags, new_slot_info; |
| 840 | int ret; |
| 841 | |
| 842 | ret = xhci_check_args(hcd, udev, ep, 1, __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 843 | if (ret <= 0) |
| 844 | return ret; |
| 845 | xhci = hcd_to_xhci(hcd); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 846 | xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 847 | |
| 848 | drop_flag = xhci_get_endpoint_flag(&ep->desc); |
| 849 | if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) { |
| 850 | xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n", |
| 851 | __func__, drop_flag); |
| 852 | return 0; |
| 853 | } |
| 854 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 855 | if (!xhci->devs || !xhci->devs[udev->slot_id]) { |
| 856 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 857 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 858 | return -EINVAL; |
| 859 | } |
| 860 | |
| 861 | in_ctx = xhci->devs[udev->slot_id]->in_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 862 | out_ctx = xhci->devs[udev->slot_id]->out_ctx; |
| 863 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 864 | ep_index = xhci_get_endpoint_index(&ep->desc); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 865 | ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 866 | /* If the HC already knows the endpoint is disabled, |
| 867 | * or the HCD has noted it is disabled, ignore this request |
| 868 | */ |
| 869 | if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED || |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 870 | ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) { |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 871 | xhci_warn(xhci, "xHCI %s called with disabled ep %p\n", |
| 872 | __func__, ep); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 873 | return 0; |
| 874 | } |
| 875 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 876 | ctrl_ctx->drop_flags |= drop_flag; |
| 877 | new_drop_flags = ctrl_ctx->drop_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 878 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 879 | ctrl_ctx->add_flags = ~drop_flag; |
| 880 | new_add_flags = ctrl_ctx->add_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 881 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 882 | last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags); |
| 883 | slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 884 | /* Update the last valid endpoint context, if we deleted the last one */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 885 | if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) { |
| 886 | slot_ctx->dev_info &= ~LAST_CTX_MASK; |
| 887 | slot_ctx->dev_info |= LAST_CTX(last_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 888 | } |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 889 | new_slot_info = slot_ctx->dev_info; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 890 | |
| 891 | xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep); |
| 892 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 893 | xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n", |
| 894 | (unsigned int) ep->desc.bEndpointAddress, |
| 895 | udev->slot_id, |
| 896 | (unsigned int) new_drop_flags, |
| 897 | (unsigned int) new_add_flags, |
| 898 | (unsigned int) new_slot_info); |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | /* Add an endpoint to a new possible bandwidth configuration for this device. |
| 903 | * Only one call to this function is allowed per endpoint before |
| 904 | * check_bandwidth() or reset_bandwidth() must be called. |
| 905 | * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will |
| 906 | * add the endpoint to the schedule with possibly new parameters denoted by a |
| 907 | * different endpoint descriptor in usb_host_endpoint. |
| 908 | * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is |
| 909 | * not allowed. |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 910 | * |
| 911 | * The USB core will not allow URBs to be queued to an endpoint until the |
| 912 | * configuration or alt setting is installed in the device, so there's no need |
| 913 | * for mutual exclusion to protect the xhci->devs[slot_id] structure. |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 914 | */ |
| 915 | int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev, |
| 916 | struct usb_host_endpoint *ep) |
| 917 | { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 918 | struct xhci_hcd *xhci; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 919 | struct xhci_container_ctx *in_ctx, *out_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 920 | unsigned int ep_index; |
| 921 | struct xhci_ep_ctx *ep_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 922 | struct xhci_slot_ctx *slot_ctx; |
| 923 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 924 | u32 added_ctxs; |
| 925 | unsigned int last_ctx; |
| 926 | u32 new_add_flags, new_drop_flags, new_slot_info; |
| 927 | int ret = 0; |
| 928 | |
| 929 | ret = xhci_check_args(hcd, udev, ep, 1, __func__); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 930 | if (ret <= 0) { |
| 931 | /* So we won't queue a reset ep command for a root hub */ |
| 932 | ep->hcpriv = NULL; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 933 | return ret; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 934 | } |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 935 | xhci = hcd_to_xhci(hcd); |
| 936 | |
| 937 | added_ctxs = xhci_get_endpoint_flag(&ep->desc); |
| 938 | last_ctx = xhci_last_valid_endpoint(added_ctxs); |
| 939 | if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) { |
| 940 | /* FIXME when we have to issue an evaluate endpoint command to |
| 941 | * deal with ep0 max packet size changing once we get the |
| 942 | * descriptors |
| 943 | */ |
| 944 | xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n", |
| 945 | __func__, added_ctxs); |
| 946 | return 0; |
| 947 | } |
| 948 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 949 | if (!xhci->devs || !xhci->devs[udev->slot_id]) { |
| 950 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 951 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 952 | return -EINVAL; |
| 953 | } |
| 954 | |
| 955 | in_ctx = xhci->devs[udev->slot_id]->in_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 956 | out_ctx = xhci->devs[udev->slot_id]->out_ctx; |
| 957 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 958 | ep_index = xhci_get_endpoint_index(&ep->desc); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 959 | ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 960 | /* If the HCD has already noted the endpoint is enabled, |
| 961 | * ignore this request. |
| 962 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 963 | if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) { |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 964 | xhci_warn(xhci, "xHCI %s called with enabled ep %p\n", |
| 965 | __func__, ep); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 966 | return 0; |
| 967 | } |
| 968 | |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 969 | /* |
| 970 | * Configuration and alternate setting changes must be done in |
| 971 | * process context, not interrupt context (or so documenation |
| 972 | * for usb_set_interface() and usb_set_configuration() claim). |
| 973 | */ |
| 974 | if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id], |
| 975 | udev, ep, GFP_KERNEL) < 0) { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 976 | dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n", |
| 977 | __func__, ep->desc.bEndpointAddress); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 978 | return -ENOMEM; |
| 979 | } |
| 980 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 981 | ctrl_ctx->add_flags |= added_ctxs; |
| 982 | new_add_flags = ctrl_ctx->add_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 983 | |
| 984 | /* If xhci_endpoint_disable() was called for this endpoint, but the |
| 985 | * xHC hasn't been notified yet through the check_bandwidth() call, |
| 986 | * this re-adds a new state for the endpoint from the new endpoint |
| 987 | * descriptors. We must drop and re-add this endpoint, so we leave the |
| 988 | * drop flags alone. |
| 989 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 990 | new_drop_flags = ctrl_ctx->drop_flags; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 991 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 992 | slot_ctx = xhci_get_slot_ctx(xhci, in_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 993 | /* Update the last valid endpoint context, if we just added one past */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 994 | if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) { |
| 995 | slot_ctx->dev_info &= ~LAST_CTX_MASK; |
| 996 | slot_ctx->dev_info |= LAST_CTX(last_ctx); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 997 | } |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 998 | new_slot_info = slot_ctx->dev_info; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 999 | |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1000 | /* Store the usb_device pointer for later use */ |
| 1001 | ep->hcpriv = udev; |
| 1002 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1003 | xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n", |
| 1004 | (unsigned int) ep->desc.bEndpointAddress, |
| 1005 | udev->slot_id, |
| 1006 | (unsigned int) new_drop_flags, |
| 1007 | (unsigned int) new_add_flags, |
| 1008 | (unsigned int) new_slot_info); |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1012 | 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] | 1013 | { |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1014 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1015 | struct xhci_ep_ctx *ep_ctx; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1016 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1017 | int i; |
| 1018 | |
| 1019 | /* When a device's add flag and drop flag are zero, any subsequent |
| 1020 | * configure endpoint command will leave that endpoint's state |
| 1021 | * untouched. Make sure we don't leave any old state in the input |
| 1022 | * endpoint contexts. |
| 1023 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1024 | ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); |
| 1025 | ctrl_ctx->drop_flags = 0; |
| 1026 | ctrl_ctx->add_flags = 0; |
| 1027 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); |
| 1028 | slot_ctx->dev_info &= ~LAST_CTX_MASK; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1029 | /* Endpoint 0 is always valid */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1030 | slot_ctx->dev_info |= LAST_CTX(1); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1031 | for (i = 1; i < 31; ++i) { |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1032 | ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1033 | ep_ctx->ep_info = 0; |
| 1034 | ep_ctx->ep_info2 = 0; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 1035 | ep_ctx->deq = 0; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1036 | ep_ctx->tx_info = 0; |
| 1037 | } |
| 1038 | } |
| 1039 | |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1040 | static int xhci_configure_endpoint_result(struct xhci_hcd *xhci, |
| 1041 | struct usb_device *udev, struct xhci_virt_device *virt_dev) |
| 1042 | { |
| 1043 | int ret; |
| 1044 | |
| 1045 | switch (virt_dev->cmd_status) { |
| 1046 | case COMP_ENOMEM: |
| 1047 | dev_warn(&udev->dev, "Not enough host controller resources " |
| 1048 | "for new device state.\n"); |
| 1049 | ret = -ENOMEM; |
| 1050 | /* FIXME: can we allocate more resources for the HC? */ |
| 1051 | break; |
| 1052 | case COMP_BW_ERR: |
| 1053 | dev_warn(&udev->dev, "Not enough bandwidth " |
| 1054 | "for new device state.\n"); |
| 1055 | ret = -ENOSPC; |
| 1056 | /* FIXME: can we go back to the old state? */ |
| 1057 | break; |
| 1058 | case COMP_TRB_ERR: |
| 1059 | /* the HCD set up something wrong */ |
| 1060 | dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, " |
| 1061 | "add flag = 1, " |
| 1062 | "and endpoint is not disabled.\n"); |
| 1063 | ret = -EINVAL; |
| 1064 | break; |
| 1065 | case COMP_SUCCESS: |
| 1066 | dev_dbg(&udev->dev, "Successful Endpoint Configure command\n"); |
| 1067 | ret = 0; |
| 1068 | break; |
| 1069 | default: |
| 1070 | xhci_err(xhci, "ERROR: unexpected command completion " |
| 1071 | "code 0x%x.\n", virt_dev->cmd_status); |
| 1072 | ret = -EINVAL; |
| 1073 | break; |
| 1074 | } |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | static int xhci_evaluate_context_result(struct xhci_hcd *xhci, |
| 1079 | struct usb_device *udev, struct xhci_virt_device *virt_dev) |
| 1080 | { |
| 1081 | int ret; |
| 1082 | |
| 1083 | switch (virt_dev->cmd_status) { |
| 1084 | case COMP_EINVAL: |
| 1085 | dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate " |
| 1086 | "context command.\n"); |
| 1087 | ret = -EINVAL; |
| 1088 | break; |
| 1089 | case COMP_EBADSLT: |
| 1090 | dev_warn(&udev->dev, "WARN: slot not enabled for" |
| 1091 | "evaluate context command.\n"); |
| 1092 | case COMP_CTX_STATE: |
| 1093 | dev_warn(&udev->dev, "WARN: invalid context state for " |
| 1094 | "evaluate context command.\n"); |
| 1095 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1); |
| 1096 | ret = -EINVAL; |
| 1097 | break; |
| 1098 | case COMP_SUCCESS: |
| 1099 | dev_dbg(&udev->dev, "Successful evaluate context command\n"); |
| 1100 | ret = 0; |
| 1101 | break; |
| 1102 | default: |
| 1103 | xhci_err(xhci, "ERROR: unexpected command completion " |
| 1104 | "code 0x%x.\n", virt_dev->cmd_status); |
| 1105 | ret = -EINVAL; |
| 1106 | break; |
| 1107 | } |
| 1108 | return ret; |
| 1109 | } |
| 1110 | |
| 1111 | /* Issue a configure endpoint command or evaluate context command |
| 1112 | * and wait for it to finish. |
| 1113 | */ |
| 1114 | static int xhci_configure_endpoint(struct xhci_hcd *xhci, |
| 1115 | struct usb_device *udev, struct xhci_virt_device *virt_dev, |
| 1116 | bool ctx_change) |
| 1117 | { |
| 1118 | int ret; |
| 1119 | int timeleft; |
| 1120 | unsigned long flags; |
| 1121 | |
| 1122 | spin_lock_irqsave(&xhci->lock, flags); |
| 1123 | if (!ctx_change) |
| 1124 | ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx->dma, |
| 1125 | udev->slot_id); |
| 1126 | else |
| 1127 | ret = xhci_queue_evaluate_context(xhci, virt_dev->in_ctx->dma, |
| 1128 | udev->slot_id); |
| 1129 | if (ret < 0) { |
| 1130 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1131 | xhci_dbg(xhci, "FIXME allocate a new ring segment\n"); |
| 1132 | return -ENOMEM; |
| 1133 | } |
| 1134 | xhci_ring_cmd_db(xhci); |
| 1135 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1136 | |
| 1137 | /* Wait for the configure endpoint command to complete */ |
| 1138 | timeleft = wait_for_completion_interruptible_timeout( |
| 1139 | &virt_dev->cmd_completion, |
| 1140 | USB_CTRL_SET_TIMEOUT); |
| 1141 | if (timeleft <= 0) { |
| 1142 | xhci_warn(xhci, "%s while waiting for %s command\n", |
| 1143 | timeleft == 0 ? "Timeout" : "Signal", |
| 1144 | ctx_change == 0 ? |
| 1145 | "configure endpoint" : |
| 1146 | "evaluate context"); |
| 1147 | /* FIXME cancel the configure endpoint command */ |
| 1148 | return -ETIME; |
| 1149 | } |
| 1150 | |
| 1151 | if (!ctx_change) |
| 1152 | return xhci_configure_endpoint_result(xhci, udev, virt_dev); |
| 1153 | return xhci_evaluate_context_result(xhci, udev, virt_dev); |
| 1154 | } |
| 1155 | |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1156 | /* Called after one or more calls to xhci_add_endpoint() or |
| 1157 | * xhci_drop_endpoint(). If this call fails, the USB core is expected |
| 1158 | * to call xhci_reset_bandwidth(). |
| 1159 | * |
| 1160 | * Since we are in the middle of changing either configuration or |
| 1161 | * installing a new alt setting, the USB core won't allow URBs to be |
| 1162 | * enqueued for any endpoint on the old config or interface. Nothing |
| 1163 | * else should be touching the xhci->devs[slot_id] structure, so we |
| 1164 | * don't need to take the xhci->lock for manipulating that. |
| 1165 | */ |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1166 | int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) |
| 1167 | { |
| 1168 | int i; |
| 1169 | int ret = 0; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1170 | struct xhci_hcd *xhci; |
| 1171 | struct xhci_virt_device *virt_dev; |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1172 | struct xhci_input_control_ctx *ctrl_ctx; |
| 1173 | struct xhci_slot_ctx *slot_ctx; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1174 | |
| 1175 | ret = xhci_check_args(hcd, udev, NULL, 0, __func__); |
| 1176 | if (ret <= 0) |
| 1177 | return ret; |
| 1178 | xhci = hcd_to_xhci(hcd); |
| 1179 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1180 | if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) { |
| 1181 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 1182 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1183 | return -EINVAL; |
| 1184 | } |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1185 | xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1186 | virt_dev = xhci->devs[udev->slot_id]; |
| 1187 | |
| 1188 | /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1189 | ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); |
| 1190 | ctrl_ctx->add_flags |= SLOT_FLAG; |
| 1191 | ctrl_ctx->add_flags &= ~EP0_FLAG; |
| 1192 | ctrl_ctx->drop_flags &= ~SLOT_FLAG; |
| 1193 | ctrl_ctx->drop_flags &= ~EP0_FLAG; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1194 | xhci_dbg(xhci, "New Input Control Context:\n"); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1195 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx); |
| 1196 | xhci_dbg_ctx(xhci, virt_dev->in_ctx, |
| 1197 | LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1198 | |
Sarah Sharp | f2217e8 | 2009-08-07 14:04:43 -0700 | [diff] [blame] | 1199 | ret = xhci_configure_endpoint(xhci, udev, virt_dev, false); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1200 | if (ret) { |
| 1201 | /* Callee should call reset_bandwidth() */ |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1202 | return ret; |
| 1203 | } |
| 1204 | |
| 1205 | xhci_dbg(xhci, "Output context after successful config ep cmd:\n"); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1206 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, |
| 1207 | LAST_CTX_TO_EP_NUM(slot_ctx->dev_info)); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1208 | |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1209 | xhci_zero_in_ctx(xhci, virt_dev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1210 | /* Free any old rings */ |
| 1211 | for (i = 1; i < 31; ++i) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1212 | if (virt_dev->eps[i].new_ring) { |
| 1213 | xhci_ring_free(xhci, virt_dev->eps[i].ring); |
| 1214 | virt_dev->eps[i].ring = virt_dev->eps[i].new_ring; |
| 1215 | virt_dev->eps[i].new_ring = NULL; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1216 | } |
| 1217 | } |
| 1218 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1219 | return ret; |
| 1220 | } |
| 1221 | |
| 1222 | void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev) |
| 1223 | { |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1224 | struct xhci_hcd *xhci; |
| 1225 | struct xhci_virt_device *virt_dev; |
| 1226 | int i, ret; |
| 1227 | |
| 1228 | ret = xhci_check_args(hcd, udev, NULL, 0, __func__); |
| 1229 | if (ret <= 0) |
| 1230 | return; |
| 1231 | xhci = hcd_to_xhci(hcd); |
| 1232 | |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1233 | if (!xhci->devs || !xhci->devs[udev->slot_id]) { |
| 1234 | xhci_warn(xhci, "xHCI %s called with unaddressed device\n", |
| 1235 | __func__); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1236 | return; |
| 1237 | } |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1238 | xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1239 | virt_dev = xhci->devs[udev->slot_id]; |
| 1240 | /* Free any rings allocated for added endpoints */ |
| 1241 | for (i = 0; i < 31; ++i) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1242 | if (virt_dev->eps[i].new_ring) { |
| 1243 | xhci_ring_free(xhci, virt_dev->eps[i].new_ring); |
| 1244 | virt_dev->eps[i].new_ring = NULL; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1245 | } |
| 1246 | } |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1247 | xhci_zero_in_ctx(xhci, virt_dev); |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1250 | void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci, |
| 1251 | unsigned int slot_id, unsigned int ep_index, |
| 1252 | struct xhci_dequeue_state *deq_state) |
| 1253 | { |
| 1254 | struct xhci_container_ctx *in_ctx; |
| 1255 | struct xhci_input_control_ctx *ctrl_ctx; |
| 1256 | struct xhci_ep_ctx *ep_ctx; |
| 1257 | u32 added_ctxs; |
| 1258 | dma_addr_t addr; |
| 1259 | |
| 1260 | xhci_endpoint_copy(xhci, xhci->devs[slot_id], ep_index); |
| 1261 | in_ctx = xhci->devs[slot_id]->in_ctx; |
| 1262 | ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index); |
| 1263 | addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg, |
| 1264 | deq_state->new_deq_ptr); |
| 1265 | if (addr == 0) { |
| 1266 | xhci_warn(xhci, "WARN Cannot submit config ep after " |
| 1267 | "reset ep command\n"); |
| 1268 | xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n", |
| 1269 | deq_state->new_deq_seg, |
| 1270 | deq_state->new_deq_ptr); |
| 1271 | return; |
| 1272 | } |
| 1273 | ep_ctx->deq = addr | deq_state->new_cycle_state; |
| 1274 | |
| 1275 | xhci_slot_copy(xhci, xhci->devs[slot_id]); |
| 1276 | |
| 1277 | ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx); |
| 1278 | added_ctxs = xhci_get_endpoint_flag_from_index(ep_index); |
| 1279 | ctrl_ctx->add_flags = added_ctxs | SLOT_FLAG; |
| 1280 | ctrl_ctx->drop_flags = added_ctxs; |
| 1281 | |
| 1282 | xhci_dbg(xhci, "Slot ID %d Input Context:\n", slot_id); |
| 1283 | xhci_dbg_ctx(xhci, in_ctx, ep_index); |
| 1284 | } |
| 1285 | |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1286 | void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1287 | struct usb_device *udev, unsigned int ep_index) |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1288 | { |
| 1289 | struct xhci_dequeue_state deq_state; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1290 | struct xhci_virt_ep *ep; |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1291 | |
| 1292 | xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n"); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1293 | ep = &xhci->devs[udev->slot_id]->eps[ep_index]; |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1294 | /* We need to move the HW's dequeue pointer past this TD, |
| 1295 | * or it will attempt to resend it on the next doorbell ring. |
| 1296 | */ |
| 1297 | xhci_find_new_dequeue_state(xhci, udev->slot_id, |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1298 | ep_index, ep->stopped_td, |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1299 | &deq_state); |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1300 | |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1301 | /* HW with the reset endpoint quirk will use the saved dequeue state to |
| 1302 | * issue a configure endpoint command later. |
| 1303 | */ |
| 1304 | if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) { |
| 1305 | xhci_dbg(xhci, "Queueing new dequeue state\n"); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1306 | xhci_queue_new_dequeue_state(xhci, udev->slot_id, |
Sarah Sharp | ac9d8fe | 2009-08-07 14:04:55 -0700 | [diff] [blame] | 1307 | ep_index, &deq_state); |
| 1308 | } else { |
| 1309 | /* Better hope no one uses the input context between now and the |
| 1310 | * reset endpoint completion! |
| 1311 | */ |
| 1312 | xhci_dbg(xhci, "Setting up input context for " |
| 1313 | "configure endpoint command\n"); |
| 1314 | xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id, |
| 1315 | ep_index, &deq_state); |
| 1316 | } |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1317 | } |
| 1318 | |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1319 | /* Deal with stalled endpoints. The core should have sent the control message |
| 1320 | * to clear the halt condition. However, we need to make the xHCI hardware |
| 1321 | * reset its sequence number, since a device will expect a sequence number of |
| 1322 | * zero after the halt condition is cleared. |
| 1323 | * Context: in_interrupt |
| 1324 | */ |
| 1325 | void xhci_endpoint_reset(struct usb_hcd *hcd, |
| 1326 | struct usb_host_endpoint *ep) |
| 1327 | { |
| 1328 | struct xhci_hcd *xhci; |
| 1329 | struct usb_device *udev; |
| 1330 | unsigned int ep_index; |
| 1331 | unsigned long flags; |
| 1332 | int ret; |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1333 | struct xhci_virt_ep *virt_ep; |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1334 | |
| 1335 | xhci = hcd_to_xhci(hcd); |
| 1336 | udev = (struct usb_device *) ep->hcpriv; |
| 1337 | /* Called with a root hub endpoint (or an endpoint that wasn't added |
| 1338 | * with xhci_add_endpoint() |
| 1339 | */ |
| 1340 | if (!ep->hcpriv) |
| 1341 | return; |
| 1342 | ep_index = xhci_get_endpoint_index(&ep->desc); |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1343 | virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index]; |
| 1344 | if (!virt_ep->stopped_td) { |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 1345 | xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n", |
| 1346 | ep->desc.bEndpointAddress); |
| 1347 | return; |
| 1348 | } |
Sarah Sharp | 82d1009 | 2009-08-07 14:04:52 -0700 | [diff] [blame] | 1349 | if (usb_endpoint_xfer_control(&ep->desc)) { |
| 1350 | xhci_dbg(xhci, "Control endpoint stall already handled.\n"); |
| 1351 | return; |
| 1352 | } |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1353 | |
| 1354 | xhci_dbg(xhci, "Queueing reset endpoint command\n"); |
| 1355 | spin_lock_irqsave(&xhci->lock, flags); |
| 1356 | ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index); |
Sarah Sharp | c92bcfa | 2009-07-27 12:05:21 -0700 | [diff] [blame] | 1357 | /* |
| 1358 | * Can't change the ring dequeue pointer until it's transitioned to the |
| 1359 | * stopped state, which is only upon a successful reset endpoint |
| 1360 | * command. Better hope that last command worked! |
| 1361 | */ |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1362 | if (!ret) { |
Sarah Sharp | 63a0d9a | 2009-09-04 10:53:09 -0700 | [diff] [blame^] | 1363 | xhci_cleanup_stalled_ring(xhci, udev, ep_index); |
| 1364 | kfree(virt_ep->stopped_td); |
Sarah Sharp | a1587d9 | 2009-07-27 12:03:15 -0700 | [diff] [blame] | 1365 | xhci_ring_cmd_db(xhci); |
| 1366 | } |
| 1367 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1368 | |
| 1369 | if (ret) |
| 1370 | xhci_warn(xhci, "FIXME allocate a new ring segment\n"); |
| 1371 | } |
| 1372 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1373 | /* |
| 1374 | * At this point, the struct usb_device is about to go away, the device has |
| 1375 | * disconnected, and all traffic has been stopped and the endpoints have been |
| 1376 | * disabled. Free any HC data structures associated with that device. |
| 1377 | */ |
| 1378 | void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev) |
| 1379 | { |
| 1380 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 1381 | unsigned long flags; |
| 1382 | |
| 1383 | if (udev->slot_id == 0) |
| 1384 | return; |
| 1385 | |
| 1386 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1387 | if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) { |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1388 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1389 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 1390 | return; |
| 1391 | } |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1392 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1393 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1394 | /* |
| 1395 | * Event command completion handler will free any data structures |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1396 | * associated with the slot. XXX Can free sleep? |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1397 | */ |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Returns 0 if the xHC ran out of device slots, the Enable Slot command |
| 1402 | * timed out, or allocating memory failed. Returns 1 on success. |
| 1403 | */ |
| 1404 | int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev) |
| 1405 | { |
| 1406 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 1407 | unsigned long flags; |
| 1408 | int timeleft; |
| 1409 | int ret; |
| 1410 | |
| 1411 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1412 | ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1413 | if (ret) { |
| 1414 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1415 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 1416 | return 0; |
| 1417 | } |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1418 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1419 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1420 | |
| 1421 | /* XXX: how much time for xHC slot assignment? */ |
| 1422 | timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev, |
| 1423 | USB_CTRL_SET_TIMEOUT); |
| 1424 | if (timeleft <= 0) { |
| 1425 | xhci_warn(xhci, "%s while waiting for a slot\n", |
| 1426 | timeleft == 0 ? "Timeout" : "Signal"); |
| 1427 | /* FIXME cancel the enable slot request */ |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1431 | if (!xhci->slot_id) { |
| 1432 | xhci_err(xhci, "Error while assigning device slot ID\n"); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1433 | return 0; |
| 1434 | } |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1435 | /* xhci_alloc_virt_device() does not touch rings; no need to lock */ |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1436 | if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) { |
| 1437 | /* Disable slot, if we can do it without mem alloc */ |
| 1438 | xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n"); |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1439 | spin_lock_irqsave(&xhci->lock, flags); |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1440 | if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) |
| 1441 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1442 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1443 | return 0; |
| 1444 | } |
| 1445 | udev->slot_id = xhci->slot_id; |
| 1446 | /* Is this a LS or FS device under a HS hub? */ |
| 1447 | /* Hub or peripherial? */ |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1448 | return 1; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * Issue an Address Device command (which will issue a SetAddress request to |
| 1453 | * the device). |
| 1454 | * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so |
| 1455 | * we should only issue and wait on one address command at the same time. |
| 1456 | * |
| 1457 | * We add one to the device address issued by the hardware because the USB core |
| 1458 | * uses address 1 for the root hubs (even though they're not really devices). |
| 1459 | */ |
| 1460 | int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev) |
| 1461 | { |
| 1462 | unsigned long flags; |
| 1463 | int timeleft; |
| 1464 | struct xhci_virt_device *virt_dev; |
| 1465 | int ret = 0; |
| 1466 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1467 | struct xhci_slot_ctx *slot_ctx; |
| 1468 | struct xhci_input_control_ctx *ctrl_ctx; |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 1469 | u64 temp_64; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1470 | |
| 1471 | if (!udev->slot_id) { |
| 1472 | xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id); |
| 1473 | return -EINVAL; |
| 1474 | } |
| 1475 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1476 | virt_dev = xhci->devs[udev->slot_id]; |
| 1477 | |
| 1478 | /* If this is a Set Address to an unconfigured device, setup ep 0 */ |
| 1479 | if (!udev->config) |
| 1480 | xhci_setup_addressable_virt_dev(xhci, udev); |
| 1481 | /* Otherwise, assume the core has the device configured how it wants */ |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 1482 | xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1483 | xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1484 | |
Sarah Sharp | f88ba78 | 2009-05-14 11:44:22 -0700 | [diff] [blame] | 1485 | spin_lock_irqsave(&xhci->lock, flags); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1486 | ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma, |
| 1487 | udev->slot_id); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1488 | if (ret) { |
| 1489 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1490 | xhci_dbg(xhci, "FIXME: allocate a command ring segment\n"); |
| 1491 | return ret; |
| 1492 | } |
Sarah Sharp | 23e3be1 | 2009-04-29 19:05:20 -0700 | [diff] [blame] | 1493 | xhci_ring_cmd_db(xhci); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1494 | spin_unlock_irqrestore(&xhci->lock, flags); |
| 1495 | |
| 1496 | /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */ |
| 1497 | timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev, |
| 1498 | USB_CTRL_SET_TIMEOUT); |
| 1499 | /* FIXME: From section 4.3.4: "Software shall be responsible for timing |
| 1500 | * the SetAddress() "recovery interval" required by USB and aborting the |
| 1501 | * command on a timeout. |
| 1502 | */ |
| 1503 | if (timeleft <= 0) { |
| 1504 | xhci_warn(xhci, "%s while waiting for a slot\n", |
| 1505 | timeleft == 0 ? "Timeout" : "Signal"); |
| 1506 | /* FIXME cancel the address device command */ |
| 1507 | return -ETIME; |
| 1508 | } |
| 1509 | |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1510 | switch (virt_dev->cmd_status) { |
| 1511 | case COMP_CTX_STATE: |
| 1512 | case COMP_EBADSLT: |
| 1513 | xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n", |
| 1514 | udev->slot_id); |
| 1515 | ret = -EINVAL; |
| 1516 | break; |
| 1517 | case COMP_TX_ERR: |
| 1518 | dev_warn(&udev->dev, "Device not responding to set address.\n"); |
| 1519 | ret = -EPROTO; |
| 1520 | break; |
| 1521 | case COMP_SUCCESS: |
| 1522 | xhci_dbg(xhci, "Successful Address Device command\n"); |
| 1523 | break; |
| 1524 | default: |
| 1525 | xhci_err(xhci, "ERROR: unexpected command completion " |
| 1526 | "code 0x%x.\n", virt_dev->cmd_status); |
Sarah Sharp | 66e49d8 | 2009-07-27 12:03:46 -0700 | [diff] [blame] | 1527 | xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1528 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1529 | ret = -EINVAL; |
| 1530 | break; |
| 1531 | } |
| 1532 | if (ret) { |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1533 | return ret; |
| 1534 | } |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 1535 | temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr); |
| 1536 | xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64); |
| 1537 | xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n", |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1538 | udev->slot_id, |
Sarah Sharp | 8e595a5 | 2009-07-27 12:03:31 -0700 | [diff] [blame] | 1539 | &xhci->dcbaa->dev_context_ptrs[udev->slot_id], |
| 1540 | (unsigned long long) |
| 1541 | xhci->dcbaa->dev_context_ptrs[udev->slot_id]); |
Greg Kroah-Hartman | 700e205 | 2009-04-29 19:14:08 -0700 | [diff] [blame] | 1542 | xhci_dbg(xhci, "Output Context DMA address = %#08llx\n", |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1543 | (unsigned long long)virt_dev->out_ctx->dma); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1544 | xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1545 | xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1546 | xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id); |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1547 | xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2); |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1548 | /* |
| 1549 | * USB core uses address 1 for the roothubs, so we add one to the |
| 1550 | * address given back to us by the HC. |
| 1551 | */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1552 | slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx); |
| 1553 | udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1; |
Sarah Sharp | f94e0186 | 2009-04-27 19:58:38 -0700 | [diff] [blame] | 1554 | /* Zero the input context control for later use */ |
John Youn | d115b04 | 2009-07-27 12:05:15 -0700 | [diff] [blame] | 1555 | ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx); |
| 1556 | ctrl_ctx->add_flags = 0; |
| 1557 | ctrl_ctx->drop_flags = 0; |
Sarah Sharp | 3ffbba9 | 2009-04-27 19:57:38 -0700 | [diff] [blame] | 1558 | |
| 1559 | xhci_dbg(xhci, "Device address = %d\n", udev->devnum); |
| 1560 | /* XXX Meh, not sure if anyone else but choose_address uses this. */ |
| 1561 | set_bit(udev->devnum, udev->bus->devmap.devicemap); |
| 1562 | |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 1566 | int xhci_get_frame(struct usb_hcd *hcd) |
| 1567 | { |
| 1568 | struct xhci_hcd *xhci = hcd_to_xhci(hcd); |
| 1569 | /* EHCI mods by the periodic size. Why? */ |
| 1570 | return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3; |
| 1571 | } |
| 1572 | |
| 1573 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 1574 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 1575 | MODULE_LICENSE("GPL"); |
| 1576 | |
| 1577 | static int __init xhci_hcd_init(void) |
| 1578 | { |
| 1579 | #ifdef CONFIG_PCI |
| 1580 | int retval = 0; |
| 1581 | |
| 1582 | retval = xhci_register_pci(); |
| 1583 | |
| 1584 | if (retval < 0) { |
| 1585 | printk(KERN_DEBUG "Problem registering PCI driver."); |
| 1586 | return retval; |
| 1587 | } |
| 1588 | #endif |
Sarah Sharp | 9844197 | 2009-05-14 11:44:18 -0700 | [diff] [blame] | 1589 | /* |
| 1590 | * Check the compiler generated sizes of structures that must be laid |
| 1591 | * out in specific ways for hardware access. |
| 1592 | */ |
| 1593 | BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); |
| 1594 | BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8); |
| 1595 | BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8); |
| 1596 | /* xhci_device_control has eight fields, and also |
| 1597 | * embeds one xhci_slot_ctx and 31 xhci_ep_ctx |
| 1598 | */ |
Sarah Sharp | 9844197 | 2009-05-14 11:44:18 -0700 | [diff] [blame] | 1599 | BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8); |
| 1600 | BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8); |
| 1601 | BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8); |
| 1602 | BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8); |
| 1603 | BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8); |
| 1604 | /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */ |
| 1605 | BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8); |
| 1606 | BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8); |
Sarah Sharp | 66d4ead | 2009-04-27 19:52:28 -0700 | [diff] [blame] | 1607 | return 0; |
| 1608 | } |
| 1609 | module_init(xhci_hcd_init); |
| 1610 | |
| 1611 | static void __exit xhci_hcd_cleanup(void) |
| 1612 | { |
| 1613 | #ifdef CONFIG_PCI |
| 1614 | xhci_unregister_pci(); |
| 1615 | #endif |
| 1616 | } |
| 1617 | module_exit(xhci_hcd_cleanup); |