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