2 * xHCI host controller driver
4 * Copyright (C) 2008 Intel Corp.
7 * Some code borrowed from the Linux EHCI driver.
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.
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
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.
23 #include <linux/irq.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
29 #define DRIVER_AUTHOR "Sarah Sharp"
30 #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
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");
37 /* TODO: copied from ehci-hcd.c - can this be refactored? */
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
45 * Returns negative errno, or zero on success
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).
51 static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
52 u32 mask, u32 done, int usec)
57 result = xhci_readl(xhci, ptr);
58 if (result == ~(u32)0) /* card removed */
70 * Force HC into halt state.
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?
78 int xhci_halt(struct xhci_hcd *xhci)
84 xhci_dbg(xhci, "// Halt the HC\n");
85 /* Disable all interrupts from the host controller */
87 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
91 cmd = xhci_readl(xhci, &xhci->op_regs->command);
93 xhci_writel(xhci, cmd, &xhci->op_regs->command);
95 return handshake(xhci, &xhci->op_regs->status,
96 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
100 * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
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.
106 int xhci_reset(struct xhci_hcd *xhci)
111 state = xhci_readl(xhci, &xhci->op_regs->status);
112 if ((state & STS_HALT) == 0) {
113 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
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;
124 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
128 * Stop the HC from processing the endpoint queues.
130 static void xhci_quiesce(struct xhci_hcd *xhci)
133 * Queues are per endpoint, so we need to disable an endpoint or slot.
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).
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.
144 * TODO: Implement after command ring code is done.
146 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
147 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
151 /* Set up MSI-X table for entry 0 (may claim other entries later) */
152 static int xhci_setup_msix(struct xhci_hcd *xhci)
155 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
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");
164 xhci->msix_entries[0].entry = 0;
166 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
168 xhci_err(xhci, "Failed to enable MSI-X\n");
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.
176 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
177 "xHCI", xhci_to_hcd(xhci));
179 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
182 xhci_dbg(xhci, "Finished setting up MSI-X\n");
186 pci_disable_msix(pdev);
188 kfree(xhci->msix_entries);
189 xhci->msix_entries = NULL;
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)
197 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
198 if (!xhci->msix_entries)
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");
210 * Initialize memory for HCD and xHC (one-time init).
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).
216 int xhci_init(struct usb_hcd *hcd)
218 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
221 xhci_dbg(xhci, "xhci_init\n");
222 spin_lock_init(&xhci->lock);
224 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
225 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
227 xhci_dbg(xhci, "xHCI has no QUIRKS\n");
229 retval = xhci_mem_init(xhci, GFP_KERNEL);
230 xhci_dbg(xhci, "Finished xhci_init\n");
236 * Called in interrupt context when there might be work
237 * queued on the event ring
239 * xhci->lock must be held by caller.
241 static void xhci_work(struct xhci_hcd *xhci)
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.
251 temp = xhci_readl(xhci, &xhci->op_regs->status);
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 */
257 /* Acknowledge the interrupt */
258 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
260 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
261 /* Flush posted writes */
262 xhci_readl(xhci, &xhci->ir_set->irq_pending);
264 /* FIXME this should be a delayed service routine that clears the EHB */
265 xhci_handle_event(xhci);
267 /* Clear the event handler busy flag (RW1C); the event ring should be empty. */
268 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
269 xhci_write_64(xhci, temp_64 | ERST_EHB, &xhci->ir_set->erst_dequeue);
270 /* Flush posted writes -- FIXME is this necessary? */
271 xhci_readl(xhci, &xhci->ir_set->irq_pending);
274 /*-------------------------------------------------------------------------*/
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.
281 irqreturn_t xhci_irq(struct usb_hcd *hcd)
283 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
287 spin_lock(&xhci->lock);
288 trb = xhci->event_ring->dequeue;
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);
292 if (temp == 0xffffffff && temp2 == 0xffffffff)
295 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
296 spin_unlock(&xhci->lock);
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);
309 if (temp & STS_FATAL) {
310 xhci_warn(xhci, "WARNING: Host System Error\n");
313 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
314 spin_unlock(&xhci->lock);
319 spin_unlock(&xhci->lock);
324 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
325 void xhci_event_ring_work(unsigned long arg)
330 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
333 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
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);
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);
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);
353 for (i = 0; i < MAX_HC_SLOTS; ++i) {
355 for (j = 0; j < 31; ++j) {
356 if (xhci->devs[i]->ep_rings[j]) {
357 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
358 xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
364 if (xhci->noops_submitted != NUM_TEST_NOOPS)
365 if (xhci_setup_one_noop(xhci))
366 xhci_ring_cmd_db(xhci);
367 spin_unlock_irqrestore(&xhci->lock, flags);
370 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
372 xhci_dbg(xhci, "Quit polling the event ring.\n");
377 * Start the HC after it was halted.
379 * This function is called by the USB core when the HC driver is added.
380 * Its opposite is xhci_stop().
382 * xhci_init() must be called once before this function can be called.
383 * Reset the HC, enable device slot contexts, program DCBAAP, and
384 * set command ring pointer and event ring pointer.
386 * Setup MSI-X vectors and enable interrupts.
388 int xhci_run(struct usb_hcd *hcd)
392 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
393 void (*doorbell)(struct xhci_hcd *) = NULL;
395 hcd->uses_new_polling = 1;
398 xhci_dbg(xhci, "xhci_run\n");
399 #if 0 /* FIXME: MSI not setup yet */
400 /* Do this at the very last minute */
401 ret = xhci_setup_msix(xhci);
407 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
408 init_timer(&xhci->event_ring_timer);
409 xhci->event_ring_timer.data = (unsigned long) xhci;
410 xhci->event_ring_timer.function = xhci_event_ring_work;
411 /* Poll the event ring */
412 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
414 xhci_dbg(xhci, "Setting event ring polling timer\n");
415 add_timer(&xhci->event_ring_timer);
418 xhci_dbg(xhci, "Command ring memory map follows:\n");
419 xhci_debug_ring(xhci, xhci->cmd_ring);
420 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
421 xhci_dbg_cmd_ptrs(xhci);
423 xhci_dbg(xhci, "ERST memory map follows:\n");
424 xhci_dbg_erst(xhci, &xhci->erst);
425 xhci_dbg(xhci, "Event ring:\n");
426 xhci_debug_ring(xhci, xhci->event_ring);
427 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
428 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
429 temp_64 &= ~ERST_PTR_MASK;
430 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
432 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
433 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
434 temp &= ~ER_IRQ_INTERVAL_MASK;
436 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
438 /* Set the HCD state before we enable the irqs */
439 hcd->state = HC_STATE_RUNNING;
440 temp = xhci_readl(xhci, &xhci->op_regs->command);
442 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
444 xhci_writel(xhci, temp, &xhci->op_regs->command);
446 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
447 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
448 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
449 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
450 &xhci->ir_set->irq_pending);
451 xhci_print_ir_set(xhci, xhci->ir_set, 0);
453 if (NUM_TEST_NOOPS > 0)
454 doorbell = xhci_setup_one_noop(xhci);
456 temp = xhci_readl(xhci, &xhci->op_regs->command);
458 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
460 xhci_writel(xhci, temp, &xhci->op_regs->command);
461 /* Flush PCI posted writes */
462 temp = xhci_readl(xhci, &xhci->op_regs->command);
463 xhci_dbg(xhci, "// @%p = 0x%x\n", &xhci->op_regs->command, temp);
467 xhci_dbg(xhci, "Finished xhci_run\n");
474 * This function is called by the USB core when the HC driver is removed.
475 * Its opposite is xhci_run().
477 * Disable device contexts, disable IRQs, and quiesce the HC.
478 * Reset the HC, finish any completed transactions, and cleanup memory.
480 void xhci_stop(struct usb_hcd *hcd)
483 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
485 spin_lock_irq(&xhci->lock);
486 if (HC_IS_RUNNING(hcd->state))
490 spin_unlock_irq(&xhci->lock);
492 #if 0 /* No MSI yet */
493 xhci_cleanup_msix(xhci);
495 #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
496 /* Tell the event ring poll function not to reschedule */
498 del_timer_sync(&xhci->event_ring_timer);
501 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
502 temp = xhci_readl(xhci, &xhci->op_regs->status);
503 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
504 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
505 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
506 &xhci->ir_set->irq_pending);
507 xhci_print_ir_set(xhci, xhci->ir_set, 0);
509 xhci_dbg(xhci, "cleaning up memory\n");
510 xhci_mem_cleanup(xhci);
511 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
512 xhci_readl(xhci, &xhci->op_regs->status));
516 * Shutdown HC (not bus-specific)
518 * This is called when the machine is rebooting or halting. We assume that the
519 * machine will be powered off, and the HC's internal state will be reset.
520 * Don't bother to free memory.
522 void xhci_shutdown(struct usb_hcd *hcd)
524 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
526 spin_lock_irq(&xhci->lock);
528 spin_unlock_irq(&xhci->lock);
531 xhci_cleanup_msix(xhci);
534 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
535 xhci_readl(xhci, &xhci->op_regs->status));
538 /*-------------------------------------------------------------------------*/
541 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
542 * HCDs. Find the index for an endpoint given its descriptor. Use the return
543 * value to right shift 1 for the bitmask.
545 * Index = (epnum * 2) + direction - 1,
546 * where direction = 0 for OUT, 1 for IN.
547 * For control endpoints, the IN index is used (OUT index is unused), so
548 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
550 unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
553 if (usb_endpoint_xfer_control(desc))
554 index = (unsigned int) (usb_endpoint_num(desc)*2);
556 index = (unsigned int) (usb_endpoint_num(desc)*2) +
557 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
561 /* Find the flag for this endpoint (for use in the control context). Use the
562 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
565 unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
567 return 1 << (xhci_get_endpoint_index(desc) + 1);
570 /* Compute the last valid endpoint context index. Basically, this is the
571 * endpoint index plus one. For slot contexts with more than valid endpoint,
572 * we find the most significant bit set in the added contexts flags.
573 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
574 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
576 static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
578 return fls(added_ctxs) - 1;
581 /* Returns 1 if the arguments are OK;
582 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
584 int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
585 struct usb_host_endpoint *ep, int check_ep, const char *func) {
586 if (!hcd || (check_ep && !ep) || !udev) {
587 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
592 printk(KERN_DEBUG "xHCI %s called for root hub\n",
596 if (!udev->slot_id) {
597 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
604 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
605 struct usb_device *udev, struct xhci_virt_device *virt_dev,
609 * Full speed devices may have a max packet size greater than 8 bytes, but the
610 * USB core doesn't know that until it reads the first 8 bytes of the
611 * descriptor. If the usb_device's max packet size changes after that point,
612 * we need to issue an evaluate context command and wait on it.
614 static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
615 unsigned int ep_index, struct urb *urb)
617 struct xhci_container_ctx *in_ctx;
618 struct xhci_container_ctx *out_ctx;
619 struct xhci_input_control_ctx *ctrl_ctx;
620 struct xhci_ep_ctx *ep_ctx;
622 int hw_max_packet_size;
625 out_ctx = xhci->devs[slot_id]->out_ctx;
626 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
627 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
628 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
629 if (hw_max_packet_size != max_packet_size) {
630 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
631 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
633 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
635 xhci_dbg(xhci, "Issuing evaluate context command.\n");
637 /* Set up the modified control endpoint 0 */
638 xhci_endpoint_copy(xhci, xhci->devs[slot_id], ep_index);
639 in_ctx = xhci->devs[slot_id]->in_ctx;
640 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
641 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
642 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
644 /* Set up the input context flags for the command */
645 /* FIXME: This won't work if a non-default control endpoint
646 * changes max packet sizes.
648 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
649 ctrl_ctx->add_flags = EP0_FLAG;
650 ctrl_ctx->drop_flags = 0;
652 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
653 xhci_dbg_ctx(xhci, in_ctx, ep_index);
654 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
655 xhci_dbg_ctx(xhci, out_ctx, ep_index);
657 ret = xhci_configure_endpoint(xhci, urb->dev,
658 xhci->devs[slot_id], true);
660 /* Clean up the input context for later use by bandwidth
663 ctrl_ctx->add_flags = SLOT_FLAG;
669 * non-error returns are a promise to giveback() the urb later
670 * we drop ownership so next owner (or urb unlink) can get it
672 int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
674 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
677 unsigned int slot_id, ep_index;
680 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
683 slot_id = urb->dev->slot_id;
684 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
686 if (!xhci->devs || !xhci->devs[slot_id]) {
688 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
692 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
694 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
698 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
699 /* Check to see if the max packet size for the default control
700 * endpoint changed during FS device enumeration
702 if (urb->dev->speed == USB_SPEED_FULL) {
703 ret = xhci_check_maxpacket(xhci, slot_id,
709 /* We have a spinlock and interrupts disabled, so we must pass
710 * atomic context to this function, which may allocate memory.
712 spin_lock_irqsave(&xhci->lock, flags);
713 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
715 spin_unlock_irqrestore(&xhci->lock, flags);
716 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
717 spin_lock_irqsave(&xhci->lock, flags);
718 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
720 spin_unlock_irqrestore(&xhci->lock, flags);
729 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
730 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
731 * should pick up where it left off in the TD, unless a Set Transfer Ring
732 * Dequeue Pointer is issued.
734 * The TRBs that make up the buffers for the canceled URB will be "removed" from
735 * the ring. Since the ring is a contiguous structure, they can't be physically
736 * removed. Instead, there are two options:
738 * 1) If the HC is in the middle of processing the URB to be canceled, we
739 * simply move the ring's dequeue pointer past those TRBs using the Set
740 * Transfer Ring Dequeue Pointer command. This will be the common case,
741 * when drivers timeout on the last submitted URB and attempt to cancel.
743 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
744 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
745 * HC will need to invalidate the any TRBs it has cached after the stop
746 * endpoint command, as noted in the xHCI 0.95 errata.
748 * 3) The TD may have completed by the time the Stop Endpoint Command
749 * completes, so software needs to handle that case too.
751 * This function should protect against the TD enqueueing code ringing the
752 * doorbell while this code is waiting for a Stop Endpoint command to complete.
753 * It also needs to account for multiple cancellations on happening at the same
754 * time for the same endpoint.
756 * Note that this function can be called in any context, or so says
757 * usb_hcd_unlink_urb()
759 int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
763 struct xhci_hcd *xhci;
765 unsigned int ep_index;
766 struct xhci_ring *ep_ring;
768 xhci = hcd_to_xhci(hcd);
769 spin_lock_irqsave(&xhci->lock, flags);
770 /* Make sure the URB hasn't completed or been unlinked already */
771 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
772 if (ret || !urb->hcpriv)
775 xhci_dbg(xhci, "Cancel URB %p\n", urb);
776 xhci_dbg(xhci, "Event ring:\n");
777 xhci_debug_ring(xhci, xhci->event_ring);
778 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
779 ep_ring = xhci->devs[urb->dev->slot_id]->ep_rings[ep_index];
780 xhci_dbg(xhci, "Endpoint ring:\n");
781 xhci_debug_ring(xhci, ep_ring);
782 td = (struct xhci_td *) urb->hcpriv;
784 ep_ring->cancels_pending++;
785 list_add_tail(&td->cancelled_td_list, &ep_ring->cancelled_td_list);
786 /* Queue a stop endpoint command, but only if this is
787 * the first cancellation to be handled.
789 if (ep_ring->cancels_pending == 1) {
790 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index);
791 xhci_ring_cmd_db(xhci);
794 spin_unlock_irqrestore(&xhci->lock, flags);
798 /* Drop an endpoint from a new bandwidth configuration for this device.
799 * Only one call to this function is allowed per endpoint before
800 * check_bandwidth() or reset_bandwidth() must be called.
801 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
802 * add the endpoint to the schedule with possibly new parameters denoted by a
803 * different endpoint descriptor in usb_host_endpoint.
804 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
807 * The USB core will not allow URBs to be queued to an endpoint that is being
808 * disabled, so there's no need for mutual exclusion to protect
809 * the xhci->devs[slot_id] structure.
811 int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
812 struct usb_host_endpoint *ep)
814 struct xhci_hcd *xhci;
815 struct xhci_container_ctx *in_ctx, *out_ctx;
816 struct xhci_input_control_ctx *ctrl_ctx;
817 struct xhci_slot_ctx *slot_ctx;
818 unsigned int last_ctx;
819 unsigned int ep_index;
820 struct xhci_ep_ctx *ep_ctx;
822 u32 new_add_flags, new_drop_flags, new_slot_info;
825 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
828 xhci = hcd_to_xhci(hcd);
829 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
831 drop_flag = xhci_get_endpoint_flag(&ep->desc);
832 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
833 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
834 __func__, drop_flag);
838 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
839 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
844 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
845 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
846 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
847 ep_index = xhci_get_endpoint_index(&ep->desc);
848 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
849 /* If the HC already knows the endpoint is disabled,
850 * or the HCD has noted it is disabled, ignore this request
852 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
853 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
854 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
859 ctrl_ctx->drop_flags |= drop_flag;
860 new_drop_flags = ctrl_ctx->drop_flags;
862 ctrl_ctx->add_flags = ~drop_flag;
863 new_add_flags = ctrl_ctx->add_flags;
865 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
866 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
867 /* Update the last valid endpoint context, if we deleted the last one */
868 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
869 slot_ctx->dev_info &= ~LAST_CTX_MASK;
870 slot_ctx->dev_info |= LAST_CTX(last_ctx);
872 new_slot_info = slot_ctx->dev_info;
874 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
876 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
877 (unsigned int) ep->desc.bEndpointAddress,
879 (unsigned int) new_drop_flags,
880 (unsigned int) new_add_flags,
881 (unsigned int) new_slot_info);
885 /* Add an endpoint to a new possible bandwidth configuration for this device.
886 * Only one call to this function is allowed per endpoint before
887 * check_bandwidth() or reset_bandwidth() must be called.
888 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
889 * add the endpoint to the schedule with possibly new parameters denoted by a
890 * different endpoint descriptor in usb_host_endpoint.
891 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
894 * The USB core will not allow URBs to be queued to an endpoint until the
895 * configuration or alt setting is installed in the device, so there's no need
896 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
898 int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
899 struct usb_host_endpoint *ep)
901 struct xhci_hcd *xhci;
902 struct xhci_container_ctx *in_ctx, *out_ctx;
903 unsigned int ep_index;
904 struct xhci_ep_ctx *ep_ctx;
905 struct xhci_slot_ctx *slot_ctx;
906 struct xhci_input_control_ctx *ctrl_ctx;
908 unsigned int last_ctx;
909 u32 new_add_flags, new_drop_flags, new_slot_info;
912 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
914 /* So we won't queue a reset ep command for a root hub */
918 xhci = hcd_to_xhci(hcd);
920 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
921 last_ctx = xhci_last_valid_endpoint(added_ctxs);
922 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
923 /* FIXME when we have to issue an evaluate endpoint command to
924 * deal with ep0 max packet size changing once we get the
927 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
928 __func__, added_ctxs);
932 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
933 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
938 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
939 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
940 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
941 ep_index = xhci_get_endpoint_index(&ep->desc);
942 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
943 /* If the HCD has already noted the endpoint is enabled,
944 * ignore this request.
946 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
947 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
953 * Configuration and alternate setting changes must be done in
954 * process context, not interrupt context (or so documenation
955 * for usb_set_interface() and usb_set_configuration() claim).
957 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
958 udev, ep, GFP_KERNEL) < 0) {
959 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
960 __func__, ep->desc.bEndpointAddress);
964 ctrl_ctx->add_flags |= added_ctxs;
965 new_add_flags = ctrl_ctx->add_flags;
967 /* If xhci_endpoint_disable() was called for this endpoint, but the
968 * xHC hasn't been notified yet through the check_bandwidth() call,
969 * this re-adds a new state for the endpoint from the new endpoint
970 * descriptors. We must drop and re-add this endpoint, so we leave the
973 new_drop_flags = ctrl_ctx->drop_flags;
975 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
976 /* Update the last valid endpoint context, if we just added one past */
977 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
978 slot_ctx->dev_info &= ~LAST_CTX_MASK;
979 slot_ctx->dev_info |= LAST_CTX(last_ctx);
981 new_slot_info = slot_ctx->dev_info;
983 /* Store the usb_device pointer for later use */
986 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
987 (unsigned int) ep->desc.bEndpointAddress,
989 (unsigned int) new_drop_flags,
990 (unsigned int) new_add_flags,
991 (unsigned int) new_slot_info);
995 static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
997 struct xhci_input_control_ctx *ctrl_ctx;
998 struct xhci_ep_ctx *ep_ctx;
999 struct xhci_slot_ctx *slot_ctx;
1002 /* When a device's add flag and drop flag are zero, any subsequent
1003 * configure endpoint command will leave that endpoint's state
1004 * untouched. Make sure we don't leave any old state in the input
1005 * endpoint contexts.
1007 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1008 ctrl_ctx->drop_flags = 0;
1009 ctrl_ctx->add_flags = 0;
1010 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1011 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1012 /* Endpoint 0 is always valid */
1013 slot_ctx->dev_info |= LAST_CTX(1);
1014 for (i = 1; i < 31; ++i) {
1015 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1016 ep_ctx->ep_info = 0;
1017 ep_ctx->ep_info2 = 0;
1019 ep_ctx->tx_info = 0;
1023 static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1024 struct usb_device *udev, struct xhci_virt_device *virt_dev)
1028 switch (virt_dev->cmd_status) {
1030 dev_warn(&udev->dev, "Not enough host controller resources "
1031 "for new device state.\n");
1033 /* FIXME: can we allocate more resources for the HC? */
1036 dev_warn(&udev->dev, "Not enough bandwidth "
1037 "for new device state.\n");
1039 /* FIXME: can we go back to the old state? */
1042 /* the HCD set up something wrong */
1043 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1045 "and endpoint is not disabled.\n");
1049 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1053 xhci_err(xhci, "ERROR: unexpected command completion "
1054 "code 0x%x.\n", virt_dev->cmd_status);
1061 static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
1062 struct usb_device *udev, struct xhci_virt_device *virt_dev)
1066 switch (virt_dev->cmd_status) {
1068 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1069 "context command.\n");
1073 dev_warn(&udev->dev, "WARN: slot not enabled for"
1074 "evaluate context command.\n");
1075 case COMP_CTX_STATE:
1076 dev_warn(&udev->dev, "WARN: invalid context state for "
1077 "evaluate context command.\n");
1078 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1082 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1086 xhci_err(xhci, "ERROR: unexpected command completion "
1087 "code 0x%x.\n", virt_dev->cmd_status);
1094 /* Issue a configure endpoint command or evaluate context command
1095 * and wait for it to finish.
1097 static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1098 struct usb_device *udev, struct xhci_virt_device *virt_dev,
1103 unsigned long flags;
1105 spin_lock_irqsave(&xhci->lock, flags);
1107 ret = xhci_queue_configure_endpoint(xhci, virt_dev->in_ctx->dma,
1110 ret = xhci_queue_evaluate_context(xhci, virt_dev->in_ctx->dma,
1113 spin_unlock_irqrestore(&xhci->lock, flags);
1114 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1117 xhci_ring_cmd_db(xhci);
1118 spin_unlock_irqrestore(&xhci->lock, flags);
1120 /* Wait for the configure endpoint command to complete */
1121 timeleft = wait_for_completion_interruptible_timeout(
1122 &virt_dev->cmd_completion,
1123 USB_CTRL_SET_TIMEOUT);
1124 if (timeleft <= 0) {
1125 xhci_warn(xhci, "%s while waiting for %s command\n",
1126 timeleft == 0 ? "Timeout" : "Signal",
1128 "configure endpoint" :
1129 "evaluate context");
1130 /* FIXME cancel the configure endpoint command */
1135 return xhci_configure_endpoint_result(xhci, udev, virt_dev);
1136 return xhci_evaluate_context_result(xhci, udev, virt_dev);
1139 /* Called after one or more calls to xhci_add_endpoint() or
1140 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1141 * to call xhci_reset_bandwidth().
1143 * Since we are in the middle of changing either configuration or
1144 * installing a new alt setting, the USB core won't allow URBs to be
1145 * enqueued for any endpoint on the old config or interface. Nothing
1146 * else should be touching the xhci->devs[slot_id] structure, so we
1147 * don't need to take the xhci->lock for manipulating that.
1149 int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1153 struct xhci_hcd *xhci;
1154 struct xhci_virt_device *virt_dev;
1155 struct xhci_input_control_ctx *ctrl_ctx;
1156 struct xhci_slot_ctx *slot_ctx;
1158 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1161 xhci = hcd_to_xhci(hcd);
1163 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
1164 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1168 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1169 virt_dev = xhci->devs[udev->slot_id];
1171 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
1172 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1173 ctrl_ctx->add_flags |= SLOT_FLAG;
1174 ctrl_ctx->add_flags &= ~EP0_FLAG;
1175 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1176 ctrl_ctx->drop_flags &= ~EP0_FLAG;
1177 xhci_dbg(xhci, "New Input Control Context:\n");
1178 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1179 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1180 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1182 ret = xhci_configure_endpoint(xhci, udev, virt_dev, false);
1184 /* Callee should call reset_bandwidth() */
1188 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
1189 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1190 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
1192 xhci_zero_in_ctx(xhci, virt_dev);
1193 /* Free any old rings */
1194 for (i = 1; i < 31; ++i) {
1195 if (virt_dev->new_ep_rings[i]) {
1196 xhci_ring_free(xhci, virt_dev->ep_rings[i]);
1197 virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
1198 virt_dev->new_ep_rings[i] = NULL;
1205 void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1207 struct xhci_hcd *xhci;
1208 struct xhci_virt_device *virt_dev;
1211 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
1214 xhci = hcd_to_xhci(hcd);
1216 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
1217 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
1221 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1222 virt_dev = xhci->devs[udev->slot_id];
1223 /* Free any rings allocated for added endpoints */
1224 for (i = 0; i < 31; ++i) {
1225 if (virt_dev->new_ep_rings[i]) {
1226 xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
1227 virt_dev->new_ep_rings[i] = NULL;
1230 xhci_zero_in_ctx(xhci, virt_dev);
1233 void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
1234 struct usb_device *udev, struct usb_host_endpoint *ep,
1235 unsigned int ep_index, struct xhci_ring *ep_ring)
1237 struct xhci_dequeue_state deq_state;
1239 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
1240 /* We need to move the HW's dequeue pointer past this TD,
1241 * or it will attempt to resend it on the next doorbell ring.
1243 xhci_find_new_dequeue_state(xhci, udev->slot_id,
1244 ep_index, ep_ring->stopped_td, &deq_state);
1246 xhci_dbg(xhci, "Queueing new dequeue state\n");
1247 xhci_queue_new_dequeue_state(xhci, ep_ring,
1249 ep_index, &deq_state);
1252 /* Deal with stalled endpoints. The core should have sent the control message
1253 * to clear the halt condition. However, we need to make the xHCI hardware
1254 * reset its sequence number, since a device will expect a sequence number of
1255 * zero after the halt condition is cleared.
1256 * Context: in_interrupt
1258 void xhci_endpoint_reset(struct usb_hcd *hcd,
1259 struct usb_host_endpoint *ep)
1261 struct xhci_hcd *xhci;
1262 struct usb_device *udev;
1263 unsigned int ep_index;
1264 unsigned long flags;
1266 struct xhci_ring *ep_ring;
1268 xhci = hcd_to_xhci(hcd);
1269 udev = (struct usb_device *) ep->hcpriv;
1270 /* Called with a root hub endpoint (or an endpoint that wasn't added
1271 * with xhci_add_endpoint()
1275 ep_index = xhci_get_endpoint_index(&ep->desc);
1276 ep_ring = xhci->devs[udev->slot_id]->ep_rings[ep_index];
1277 if (!ep_ring->stopped_td) {
1278 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1279 ep->desc.bEndpointAddress);
1282 if (usb_endpoint_xfer_control(&ep->desc)) {
1283 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1287 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1288 spin_lock_irqsave(&xhci->lock, flags);
1289 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
1291 * Can't change the ring dequeue pointer until it's transitioned to the
1292 * stopped state, which is only upon a successful reset endpoint
1293 * command. Better hope that last command worked!
1296 xhci_cleanup_stalled_ring(xhci, udev, ep, ep_index, ep_ring);
1297 kfree(ep_ring->stopped_td);
1298 xhci_ring_cmd_db(xhci);
1300 spin_unlock_irqrestore(&xhci->lock, flags);
1303 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1307 * At this point, the struct usb_device is about to go away, the device has
1308 * disconnected, and all traffic has been stopped and the endpoints have been
1309 * disabled. Free any HC data structures associated with that device.
1311 void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
1313 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1314 unsigned long flags;
1316 if (udev->slot_id == 0)
1319 spin_lock_irqsave(&xhci->lock, flags);
1320 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
1321 spin_unlock_irqrestore(&xhci->lock, flags);
1322 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1325 xhci_ring_cmd_db(xhci);
1326 spin_unlock_irqrestore(&xhci->lock, flags);
1328 * Event command completion handler will free any data structures
1329 * associated with the slot. XXX Can free sleep?
1334 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
1335 * timed out, or allocating memory failed. Returns 1 on success.
1337 int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
1339 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1340 unsigned long flags;
1344 spin_lock_irqsave(&xhci->lock, flags);
1345 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
1347 spin_unlock_irqrestore(&xhci->lock, flags);
1348 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1351 xhci_ring_cmd_db(xhci);
1352 spin_unlock_irqrestore(&xhci->lock, flags);
1354 /* XXX: how much time for xHC slot assignment? */
1355 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1356 USB_CTRL_SET_TIMEOUT);
1357 if (timeleft <= 0) {
1358 xhci_warn(xhci, "%s while waiting for a slot\n",
1359 timeleft == 0 ? "Timeout" : "Signal");
1360 /* FIXME cancel the enable slot request */
1364 if (!xhci->slot_id) {
1365 xhci_err(xhci, "Error while assigning device slot ID\n");
1368 /* xhci_alloc_virt_device() does not touch rings; no need to lock */
1369 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
1370 /* Disable slot, if we can do it without mem alloc */
1371 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
1372 spin_lock_irqsave(&xhci->lock, flags);
1373 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
1374 xhci_ring_cmd_db(xhci);
1375 spin_unlock_irqrestore(&xhci->lock, flags);
1378 udev->slot_id = xhci->slot_id;
1379 /* Is this a LS or FS device under a HS hub? */
1380 /* Hub or peripherial? */
1385 * Issue an Address Device command (which will issue a SetAddress request to
1387 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
1388 * we should only issue and wait on one address command at the same time.
1390 * We add one to the device address issued by the hardware because the USB core
1391 * uses address 1 for the root hubs (even though they're not really devices).
1393 int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
1395 unsigned long flags;
1397 struct xhci_virt_device *virt_dev;
1399 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1400 struct xhci_slot_ctx *slot_ctx;
1401 struct xhci_input_control_ctx *ctrl_ctx;
1404 if (!udev->slot_id) {
1405 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
1409 virt_dev = xhci->devs[udev->slot_id];
1411 /* If this is a Set Address to an unconfigured device, setup ep 0 */
1413 xhci_setup_addressable_virt_dev(xhci, udev);
1414 /* Otherwise, assume the core has the device configured how it wants */
1415 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1416 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
1418 spin_lock_irqsave(&xhci->lock, flags);
1419 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
1422 spin_unlock_irqrestore(&xhci->lock, flags);
1423 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1426 xhci_ring_cmd_db(xhci);
1427 spin_unlock_irqrestore(&xhci->lock, flags);
1429 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
1430 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1431 USB_CTRL_SET_TIMEOUT);
1432 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
1433 * the SetAddress() "recovery interval" required by USB and aborting the
1434 * command on a timeout.
1436 if (timeleft <= 0) {
1437 xhci_warn(xhci, "%s while waiting for a slot\n",
1438 timeleft == 0 ? "Timeout" : "Signal");
1439 /* FIXME cancel the address device command */
1443 switch (virt_dev->cmd_status) {
1444 case COMP_CTX_STATE:
1446 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
1451 dev_warn(&udev->dev, "Device not responding to set address.\n");
1455 xhci_dbg(xhci, "Successful Address Device command\n");
1458 xhci_err(xhci, "ERROR: unexpected command completion "
1459 "code 0x%x.\n", virt_dev->cmd_status);
1460 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1461 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
1468 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
1469 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
1470 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
1472 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
1473 (unsigned long long)
1474 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
1475 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
1476 (unsigned long long)virt_dev->out_ctx->dma);
1477 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1478 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
1479 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1480 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
1482 * USB core uses address 1 for the roothubs, so we add one to the
1483 * address given back to us by the HC.
1485 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
1486 udev->devnum = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
1487 /* Zero the input context control for later use */
1488 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1489 ctrl_ctx->add_flags = 0;
1490 ctrl_ctx->drop_flags = 0;
1492 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
1493 /* XXX Meh, not sure if anyone else but choose_address uses this. */
1494 set_bit(udev->devnum, udev->bus->devmap.devicemap);
1499 int xhci_get_frame(struct usb_hcd *hcd)
1501 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1502 /* EHCI mods by the periodic size. Why? */
1503 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
1506 MODULE_DESCRIPTION(DRIVER_DESC);
1507 MODULE_AUTHOR(DRIVER_AUTHOR);
1508 MODULE_LICENSE("GPL");
1510 static int __init xhci_hcd_init(void)
1515 retval = xhci_register_pci();
1518 printk(KERN_DEBUG "Problem registering PCI driver.");
1523 * Check the compiler generated sizes of structures that must be laid
1524 * out in specific ways for hardware access.
1526 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1527 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
1528 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
1529 /* xhci_device_control has eight fields, and also
1530 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
1532 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
1533 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
1534 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
1535 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
1536 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
1537 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
1538 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
1539 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
1542 module_init(xhci_hcd_init);
1544 static void __exit xhci_hcd_cleanup(void)
1547 xhci_unregister_pci();
1550 module_exit(xhci_hcd_cleanup);