blob: 50ab525f65be29995716f6c5ab276fed2d9f1f83 [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
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 */
45static 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 */
72int 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 */
100int xhci_reset(struct xhci_hcd *xhci)
101{
102 u32 command;
103 u32 state;
104
105 state = xhci_readl(xhci, &xhci->op_regs->status);
106 BUG_ON((state & STS_HALT) == 0);
107
108 xhci_dbg(xhci, "// Reset the HC\n");
109 command = xhci_readl(xhci, &xhci->op_regs->command);
110 command |= CMD_RESET;
111 xhci_writel(xhci, command, &xhci->op_regs->command);
112 /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
113 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
114
115 return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
116}
117
118/*
119 * Stop the HC from processing the endpoint queues.
120 */
121static void xhci_quiesce(struct xhci_hcd *xhci)
122{
123 /*
124 * Queues are per endpoint, so we need to disable an endpoint or slot.
125 *
126 * To disable a slot, we need to insert a disable slot command on the
127 * command ring and ring the doorbell. This will also free any internal
128 * resources associated with the slot (which might not be what we want).
129 *
130 * A Release Endpoint command sounds better - doesn't free internal HC
131 * memory, but removes the endpoints from the schedule and releases the
132 * bandwidth, disables the doorbells, and clears the endpoint enable
133 * flag. Usually used prior to a set interface command.
134 *
135 * TODO: Implement after command ring code is done.
136 */
137 BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
138 xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
139}
140
141#if 0
142/* Set up MSI-X table for entry 0 (may claim other entries later) */
143static int xhci_setup_msix(struct xhci_hcd *xhci)
144{
145 int ret;
146 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
147
148 xhci->msix_count = 0;
149 /* XXX: did I do this right? ixgbe does kcalloc for more than one */
150 xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
151 if (!xhci->msix_entries) {
152 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
153 return -ENOMEM;
154 }
155 xhci->msix_entries[0].entry = 0;
156
157 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
158 if (ret) {
159 xhci_err(xhci, "Failed to enable MSI-X\n");
160 goto free_entries;
161 }
162
163 /*
164 * Pass the xhci pointer value as the request_irq "cookie".
165 * If more irqs are added, this will need to be unique for each one.
166 */
167 ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
168 "xHCI", xhci_to_hcd(xhci));
169 if (ret) {
170 xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
171 goto disable_msix;
172 }
173 xhci_dbg(xhci, "Finished setting up MSI-X\n");
174 return 0;
175
176disable_msix:
177 pci_disable_msix(pdev);
178free_entries:
179 kfree(xhci->msix_entries);
180 xhci->msix_entries = NULL;
181 return ret;
182}
183
184/* XXX: code duplication; can xhci_setup_msix call this? */
185/* Free any IRQs and disable MSI-X */
186static void xhci_cleanup_msix(struct xhci_hcd *xhci)
187{
188 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
189 if (!xhci->msix_entries)
190 return;
191
192 free_irq(xhci->msix_entries[0].vector, xhci);
193 pci_disable_msix(pdev);
194 kfree(xhci->msix_entries);
195 xhci->msix_entries = NULL;
196 xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
197}
198#endif
199
200/*
201 * Initialize memory for HCD and xHC (one-time init).
202 *
203 * Program the PAGESIZE register, initialize the device context array, create
204 * device contexts (?), set up a command ring segment (or two?), create event
205 * ring (one for now).
206 */
207int xhci_init(struct usb_hcd *hcd)
208{
209 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
210 int retval = 0;
211
212 xhci_dbg(xhci, "xhci_init\n");
213 spin_lock_init(&xhci->lock);
214 retval = xhci_mem_init(xhci, GFP_KERNEL);
215 xhci_dbg(xhci, "Finished xhci_init\n");
216
217 return retval;
218}
219
220/*
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700221 * Called in interrupt context when there might be work
222 * queued on the event ring
223 *
224 * xhci->lock must be held by caller.
225 */
226static void xhci_work(struct xhci_hcd *xhci)
227{
228 u32 temp;
229
230 /*
231 * Clear the op reg interrupt status first,
232 * so we can receive interrupts from other MSI-X interrupters.
233 * Write 1 to clear the interrupt status.
234 */
235 temp = xhci_readl(xhci, &xhci->op_regs->status);
236 temp |= STS_EINT;
237 xhci_writel(xhci, temp, &xhci->op_regs->status);
238 /* FIXME when MSI-X is supported and there are multiple vectors */
239 /* Clear the MSI-X event interrupt status */
240
241 /* Acknowledge the interrupt */
242 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
243 temp |= 0x3;
244 xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
245 /* Flush posted writes */
246 xhci_readl(xhci, &xhci->ir_set->irq_pending);
247
248 /* FIXME this should be a delayed service routine that clears the EHB */
249 handle_event(xhci);
250
251 /* Clear the event handler busy flag; the event ring should be empty. */
252 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
253 xhci_writel(xhci, temp & ~ERST_EHB, &xhci->ir_set->erst_dequeue[0]);
254 /* Flush posted writes -- FIXME is this necessary? */
255 xhci_readl(xhci, &xhci->ir_set->irq_pending);
256}
257
258/*-------------------------------------------------------------------------*/
259
260/*
261 * xHCI spec says we can get an interrupt, and if the HC has an error condition,
262 * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
263 * indicators of an event TRB error, but we check the status *first* to be safe.
264 */
265irqreturn_t xhci_irq(struct usb_hcd *hcd)
266{
267 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
268 u32 temp, temp2;
269
270 spin_lock(&xhci->lock);
271 /* Check if the xHC generated the interrupt, or the irq is shared */
272 temp = xhci_readl(xhci, &xhci->op_regs->status);
273 temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
274 if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
275 spin_unlock(&xhci->lock);
276 return IRQ_NONE;
277 }
278
279 temp = xhci_readl(xhci, &xhci->op_regs->status);
280 if (temp & STS_FATAL) {
281 xhci_warn(xhci, "WARNING: Host System Error\n");
282 xhci_halt(xhci);
283 xhci_to_hcd(xhci)->state = HC_STATE_HALT;
284 return -ESHUTDOWN;
285 }
286
287 xhci_work(xhci);
288 spin_unlock(&xhci->lock);
289
290 return IRQ_HANDLED;
291}
292
293#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
294void event_ring_work(unsigned long arg)
295{
296 unsigned long flags;
297 int temp;
298 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
299 int i, j;
300
301 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
302
303 spin_lock_irqsave(&xhci->lock, flags);
304 temp = xhci_readl(xhci, &xhci->op_regs->status);
305 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
306 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
307 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
308 xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
309 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
310 xhci->error_bitmask = 0;
311 xhci_dbg(xhci, "Event ring:\n");
312 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
313 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
314 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
315 temp &= ERST_PTR_MASK;
316 xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
317 xhci_dbg(xhci, "Command ring:\n");
318 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
319 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
320 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700321 for (i = 0; i < MAX_HC_SLOTS; ++i) {
322 if (xhci->devs[i]) {
323 for (j = 0; j < 31; ++j) {
324 if (xhci->devs[i]->ep_rings[j]) {
325 xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
326 xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
327 }
328 }
329 }
330 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700331
332 if (xhci->noops_submitted != NUM_TEST_NOOPS)
333 if (setup_one_noop(xhci))
334 ring_cmd_db(xhci);
335 spin_unlock_irqrestore(&xhci->lock, flags);
336
337 if (!xhci->zombie)
338 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
339 else
340 xhci_dbg(xhci, "Quit polling the event ring.\n");
341}
342#endif
343
344/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700345 * Start the HC after it was halted.
346 *
347 * This function is called by the USB core when the HC driver is added.
348 * Its opposite is xhci_stop().
349 *
350 * xhci_init() must be called once before this function can be called.
351 * Reset the HC, enable device slot contexts, program DCBAAP, and
352 * set command ring pointer and event ring pointer.
353 *
354 * Setup MSI-X vectors and enable interrupts.
355 */
356int xhci_run(struct usb_hcd *hcd)
357{
358 u32 temp;
359 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700360 void (*doorbell)(struct xhci_hcd *) = NULL;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700361
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700362 hcd->uses_new_polling = 1;
363 hcd->poll_rh = 0;
364
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700365 xhci_dbg(xhci, "xhci_run\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700366#if 0 /* FIXME: MSI not setup yet */
367 /* Do this at the very last minute */
368 ret = xhci_setup_msix(xhci);
369 if (!ret)
370 return ret;
371
372 return -ENOSYS;
373#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700374#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
375 init_timer(&xhci->event_ring_timer);
376 xhci->event_ring_timer.data = (unsigned long) xhci;
377 xhci->event_ring_timer.function = event_ring_work;
378 /* Poll the event ring */
379 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
380 xhci->zombie = 0;
381 xhci_dbg(xhci, "Setting event ring polling timer\n");
382 add_timer(&xhci->event_ring_timer);
383#endif
384
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700385 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
386 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
387 temp &= 0xffff;
388 temp |= (u32) 160;
389 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
390
391 /* Set the HCD state before we enable the irqs */
392 hcd->state = HC_STATE_RUNNING;
393 temp = xhci_readl(xhci, &xhci->op_regs->command);
394 temp |= (CMD_EIE);
395 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
396 temp);
397 xhci_writel(xhci, temp, &xhci->op_regs->command);
398
399 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
400 xhci_dbg(xhci, "// Enabling event ring interrupter 0x%x"
401 " by writing 0x%x to irq_pending\n",
402 (unsigned int) xhci->ir_set,
403 (unsigned int) ER_IRQ_ENABLE(temp));
404 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
405 &xhci->ir_set->irq_pending);
406 xhci_print_ir_set(xhci, xhci->ir_set, 0);
407
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700408 if (NUM_TEST_NOOPS > 0)
409 doorbell = setup_one_noop(xhci);
410
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700411 xhci_dbg(xhci, "Command ring memory map follows:\n");
412 xhci_debug_ring(xhci, xhci->cmd_ring);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700413 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
414 xhci_dbg_cmd_ptrs(xhci);
415
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700416 xhci_dbg(xhci, "ERST memory map follows:\n");
417 xhci_dbg_erst(xhci, &xhci->erst);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700418 xhci_dbg(xhci, "Event ring:\n");
419 xhci_debug_ring(xhci, xhci->event_ring);
420 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
421 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[1]);
422 xhci_dbg(xhci, "ERST deq upper = 0x%x\n", temp);
423 temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
424 temp &= ERST_PTR_MASK;
425 xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
Sarah Sharp0ebbab32009-04-27 19:52:34 -0700426
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700427 temp = xhci_readl(xhci, &xhci->op_regs->command);
428 temp |= (CMD_RUN);
429 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
430 temp);
431 xhci_writel(xhci, temp, &xhci->op_regs->command);
432 /* Flush PCI posted writes */
433 temp = xhci_readl(xhci, &xhci->op_regs->command);
434 xhci_dbg(xhci, "// @%x = 0x%x\n",
435 (unsigned int) &xhci->op_regs->command, temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700436 if (doorbell)
437 (*doorbell)(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700438
439 xhci_dbg(xhci, "Finished xhci_run\n");
440 return 0;
441}
442
443/*
444 * Stop xHCI driver.
445 *
446 * This function is called by the USB core when the HC driver is removed.
447 * Its opposite is xhci_run().
448 *
449 * Disable device contexts, disable IRQs, and quiesce the HC.
450 * Reset the HC, finish any completed transactions, and cleanup memory.
451 */
452void xhci_stop(struct usb_hcd *hcd)
453{
454 u32 temp;
455 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
456
457 spin_lock_irq(&xhci->lock);
458 if (HC_IS_RUNNING(hcd->state))
459 xhci_quiesce(xhci);
460 xhci_halt(xhci);
461 xhci_reset(xhci);
462 spin_unlock_irq(&xhci->lock);
463
464#if 0 /* No MSI yet */
465 xhci_cleanup_msix(xhci);
466#endif
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700467#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
468 /* Tell the event ring poll function not to reschedule */
469 xhci->zombie = 1;
470 del_timer_sync(&xhci->event_ring_timer);
471#endif
472
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700473 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
474 temp = xhci_readl(xhci, &xhci->op_regs->status);
475 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
476 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
477 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
478 &xhci->ir_set->irq_pending);
479 xhci_print_ir_set(xhci, xhci->ir_set, 0);
480
481 xhci_dbg(xhci, "cleaning up memory\n");
482 xhci_mem_cleanup(xhci);
483 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
484 xhci_readl(xhci, &xhci->op_regs->status));
485}
486
487/*
488 * Shutdown HC (not bus-specific)
489 *
490 * This is called when the machine is rebooting or halting. We assume that the
491 * machine will be powered off, and the HC's internal state will be reset.
492 * Don't bother to free memory.
493 */
494void xhci_shutdown(struct usb_hcd *hcd)
495{
496 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
497
498 spin_lock_irq(&xhci->lock);
499 xhci_halt(xhci);
500 spin_unlock_irq(&xhci->lock);
501
502#if 0
503 xhci_cleanup_msix(xhci);
504#endif
505
506 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
507 xhci_readl(xhci, &xhci->op_regs->status));
508}
509
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700510/*-------------------------------------------------------------------------*/
511
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700512/**
513 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
514 * HCDs. Find the index for an endpoint given its descriptor. Use the return
515 * value to right shift 1 for the bitmask.
516 *
517 * Index = (epnum * 2) + direction - 1,
518 * where direction = 0 for OUT, 1 for IN.
519 * For control endpoints, the IN index is used (OUT index is unused), so
520 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
521 */
522unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
523{
524 unsigned int index;
525 if (usb_endpoint_xfer_control(desc))
526 index = (unsigned int) (usb_endpoint_num(desc)*2);
527 else
528 index = (unsigned int) (usb_endpoint_num(desc)*2) +
529 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
530 return index;
531}
532
Sarah Sharpf94e01862009-04-27 19:58:38 -0700533/* Find the flag for this endpoint (for use in the control context). Use the
534 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
535 * bit 1, etc.
536 */
537unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
538{
539 return 1 << (xhci_get_endpoint_index(desc) + 1);
540}
541
542/* Compute the last valid endpoint context index. Basically, this is the
543 * endpoint index plus one. For slot contexts with more than valid endpoint,
544 * we find the most significant bit set in the added contexts flags.
545 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
546 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
547 */
548static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
549{
550 return fls(added_ctxs) - 1;
551}
552
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -0700553/* Returns 1 if the arguments are OK;
554 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
555 */
556int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
557 struct usb_host_endpoint *ep, int check_ep, const char *func) {
558 if (!hcd || (check_ep && !ep) || !udev) {
559 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
560 func);
561 return -EINVAL;
562 }
563 if (!udev->parent) {
564 printk(KERN_DEBUG "xHCI %s called for root hub\n",
565 func);
566 return 0;
567 }
568 if (!udev->slot_id) {
569 printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
570 func);
571 return -EINVAL;
572 }
573 return 1;
574}
575
576/*
577 * non-error returns are a promise to giveback() the urb later
578 * we drop ownership so next owner (or urb unlink) can get it
579 */
580int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
581{
582 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
583 unsigned long flags;
584 int ret = 0;
585 unsigned int slot_id, ep_index;
586
587 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
588 return -EINVAL;
589
590 slot_id = urb->dev->slot_id;
591 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
592 /* Only support ep 0 control transfers for now */
593 if (ep_index != 0) {
594 xhci_dbg(xhci, "WARN: urb submitted to unsupported ep %x\n",
595 urb->ep->desc.bEndpointAddress);
596 return -ENOSYS;
597 }
598
599 spin_lock_irqsave(&xhci->lock, flags);
600 if (!xhci->devs || !xhci->devs[slot_id]) {
601 if (!in_interrupt())
602 dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
603 return -EINVAL;
604 }
605 if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
606 if (!in_interrupt())
607 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
608 ret = -ESHUTDOWN;
609 goto exit;
610 }
611 ret = queue_ctrl_tx(xhci, mem_flags, urb, slot_id, ep_index);
612exit:
613 spin_unlock_irqrestore(&xhci->lock, flags);
614 return ret;
615}
616
617/* Remove from hardware lists
618 * completions normally happen asynchronously
619 */
620int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
621{
622 return -ENOSYS;
623}
624
Sarah Sharpf94e01862009-04-27 19:58:38 -0700625/* Drop an endpoint from a new bandwidth configuration for this device.
626 * Only one call to this function is allowed per endpoint before
627 * check_bandwidth() or reset_bandwidth() must be called.
628 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
629 * add the endpoint to the schedule with possibly new parameters denoted by a
630 * different endpoint descriptor in usb_host_endpoint.
631 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
632 * not allowed.
633 */
634int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
635 struct usb_host_endpoint *ep)
636{
637 unsigned long flags;
638 struct xhci_hcd *xhci;
639 struct xhci_device_control *in_ctx;
640 unsigned int last_ctx;
641 unsigned int ep_index;
642 struct xhci_ep_ctx *ep_ctx;
643 u32 drop_flag;
644 u32 new_add_flags, new_drop_flags, new_slot_info;
645 int ret;
646
647 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
648 xhci_dbg(xhci, "%s called for udev %#x\n", __func__, (unsigned int) udev);
649 if (ret <= 0)
650 return ret;
651 xhci = hcd_to_xhci(hcd);
652
653 drop_flag = xhci_get_endpoint_flag(&ep->desc);
654 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
655 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
656 __func__, drop_flag);
657 return 0;
658 }
659
660 spin_lock_irqsave(&xhci->lock, flags);
661 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
662 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
663 __func__);
664 spin_unlock_irqrestore(&xhci->lock, flags);
665 return -EINVAL;
666 }
667
668 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
669 ep_index = xhci_get_endpoint_index(&ep->desc);
670 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
671 /* If the HC already knows the endpoint is disabled,
672 * or the HCD has noted it is disabled, ignore this request
673 */
674 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
675 in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
676 xhci_warn(xhci, "xHCI %s called with disabled ep %#x\n",
677 __func__, (unsigned int) ep);
678 spin_unlock_irqrestore(&xhci->lock, flags);
679 return 0;
680 }
681
682 in_ctx->drop_flags |= drop_flag;
683 new_drop_flags = in_ctx->drop_flags;
684
685 in_ctx->add_flags = ~drop_flag;
686 new_add_flags = in_ctx->add_flags;
687
688 last_ctx = xhci_last_valid_endpoint(in_ctx->add_flags);
689 /* Update the last valid endpoint context, if we deleted the last one */
690 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
691 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
692 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
693 }
694 new_slot_info = in_ctx->slot.dev_info;
695
696 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
697
698 spin_unlock_irqrestore(&xhci->lock, flags);
699
700 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
701 (unsigned int) ep->desc.bEndpointAddress,
702 udev->slot_id,
703 (unsigned int) new_drop_flags,
704 (unsigned int) new_add_flags,
705 (unsigned int) new_slot_info);
706 return 0;
707}
708
709/* Add an endpoint to a new possible bandwidth configuration for this device.
710 * Only one call to this function is allowed per endpoint before
711 * check_bandwidth() or reset_bandwidth() must be called.
712 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
713 * add the endpoint to the schedule with possibly new parameters denoted by a
714 * different endpoint descriptor in usb_host_endpoint.
715 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
716 * not allowed.
717 */
718int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
719 struct usb_host_endpoint *ep)
720{
721 unsigned long flags;
722 struct xhci_hcd *xhci;
723 struct xhci_device_control *in_ctx;
724 unsigned int ep_index;
725 struct xhci_ep_ctx *ep_ctx;
726 u32 added_ctxs;
727 unsigned int last_ctx;
728 u32 new_add_flags, new_drop_flags, new_slot_info;
729 int ret = 0;
730
731 ret = xhci_check_args(hcd, udev, ep, 1, __func__);
732 if (ret <= 0)
733 return ret;
734 xhci = hcd_to_xhci(hcd);
735
736 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
737 last_ctx = xhci_last_valid_endpoint(added_ctxs);
738 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
739 /* FIXME when we have to issue an evaluate endpoint command to
740 * deal with ep0 max packet size changing once we get the
741 * descriptors
742 */
743 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
744 __func__, added_ctxs);
745 return 0;
746 }
747
748 spin_lock_irqsave(&xhci->lock, flags);
749 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
750 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
751 __func__);
752 spin_unlock_irqrestore(&xhci->lock, flags);
753 return -EINVAL;
754 }
755
756 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
757 ep_index = xhci_get_endpoint_index(&ep->desc);
758 ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
759 /* If the HCD has already noted the endpoint is enabled,
760 * ignore this request.
761 */
762 if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
763 xhci_warn(xhci, "xHCI %s called with enabled ep %#x\n",
764 __func__, (unsigned int) ep);
765 spin_unlock_irqrestore(&xhci->lock, flags);
766 return 0;
767 }
768
769 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id], udev, ep) < 0) {
770 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
771 __func__, ep->desc.bEndpointAddress);
772 spin_unlock_irqrestore(&xhci->lock, flags);
773 return -ENOMEM;
774 }
775
776 in_ctx->add_flags |= added_ctxs;
777 new_add_flags = in_ctx->add_flags;
778
779 /* If xhci_endpoint_disable() was called for this endpoint, but the
780 * xHC hasn't been notified yet through the check_bandwidth() call,
781 * this re-adds a new state for the endpoint from the new endpoint
782 * descriptors. We must drop and re-add this endpoint, so we leave the
783 * drop flags alone.
784 */
785 new_drop_flags = in_ctx->drop_flags;
786
787 /* Update the last valid endpoint context, if we just added one past */
788 if ((in_ctx->slot.dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
789 in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
790 in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
791 }
792 new_slot_info = in_ctx->slot.dev_info;
793 spin_unlock_irqrestore(&xhci->lock, flags);
794
795 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
796 (unsigned int) ep->desc.bEndpointAddress,
797 udev->slot_id,
798 (unsigned int) new_drop_flags,
799 (unsigned int) new_add_flags,
800 (unsigned int) new_slot_info);
801 return 0;
802}
803
804static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
805{
806 struct xhci_ep_ctx *ep_ctx;
807 int i;
808
809 /* When a device's add flag and drop flag are zero, any subsequent
810 * configure endpoint command will leave that endpoint's state
811 * untouched. Make sure we don't leave any old state in the input
812 * endpoint contexts.
813 */
814 virt_dev->in_ctx->drop_flags = 0;
815 virt_dev->in_ctx->add_flags = 0;
816 virt_dev->in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
817 /* Endpoint 0 is always valid */
818 virt_dev->in_ctx->slot.dev_info |= LAST_CTX(1);
819 for (i = 1; i < 31; ++i) {
820 ep_ctx = &virt_dev->in_ctx->ep[i];
821 ep_ctx->ep_info = 0;
822 ep_ctx->ep_info2 = 0;
823 ep_ctx->deq[0] = 0;
824 ep_ctx->deq[1] = 0;
825 ep_ctx->tx_info = 0;
826 }
827}
828
829int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
830{
831 int i;
832 int ret = 0;
833 int timeleft;
834 unsigned long flags;
835 struct xhci_hcd *xhci;
836 struct xhci_virt_device *virt_dev;
837
838 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
839 if (ret <= 0)
840 return ret;
841 xhci = hcd_to_xhci(hcd);
842
843 spin_lock_irqsave(&xhci->lock, flags);
844 if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
845 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
846 __func__);
847 spin_unlock_irqrestore(&xhci->lock, flags);
848 return -EINVAL;
849 }
850 xhci_dbg(xhci, "%s called for udev %#x\n", __func__, (unsigned int) udev);
851 virt_dev = xhci->devs[udev->slot_id];
852
853 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
854 virt_dev->in_ctx->add_flags |= SLOT_FLAG;
855 virt_dev->in_ctx->add_flags &= ~EP0_FLAG;
856 virt_dev->in_ctx->drop_flags &= ~SLOT_FLAG;
857 virt_dev->in_ctx->drop_flags &= ~EP0_FLAG;
858 xhci_dbg(xhci, "New Input Control Context:\n");
859 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
860 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
861
862 ret = queue_configure_endpoint(xhci, virt_dev->in_ctx_dma, udev->slot_id);
863 if (ret < 0) {
864 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
865 spin_unlock_irqrestore(&xhci->lock, flags);
866 return -ENOMEM;
867 }
868 ring_cmd_db(xhci);
869 spin_unlock_irqrestore(&xhci->lock, flags);
870
871 /* Wait for the configure endpoint command to complete */
872 timeleft = wait_for_completion_interruptible_timeout(
873 &virt_dev->cmd_completion,
874 USB_CTRL_SET_TIMEOUT);
875 if (timeleft <= 0) {
876 xhci_warn(xhci, "%s while waiting for configure endpoint command\n",
877 timeleft == 0 ? "Timeout" : "Signal");
878 /* FIXME cancel the configure endpoint command */
879 return -ETIME;
880 }
881
882 spin_lock_irqsave(&xhci->lock, flags);
883 switch (virt_dev->cmd_status) {
884 case COMP_ENOMEM:
885 dev_warn(&udev->dev, "Not enough host controller resources "
886 "for new device state.\n");
887 ret = -ENOMEM;
888 /* FIXME: can we allocate more resources for the HC? */
889 break;
890 case COMP_BW_ERR:
891 dev_warn(&udev->dev, "Not enough bandwidth "
892 "for new device state.\n");
893 ret = -ENOSPC;
894 /* FIXME: can we go back to the old state? */
895 break;
896 case COMP_TRB_ERR:
897 /* the HCD set up something wrong */
898 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, "
899 "and endpoint is not disabled.\n");
900 ret = -EINVAL;
901 break;
902 case COMP_SUCCESS:
903 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
904 break;
905 default:
906 xhci_err(xhci, "ERROR: unexpected command completion "
907 "code 0x%x.\n", virt_dev->cmd_status);
908 ret = -EINVAL;
909 break;
910 }
911 if (ret) {
912 /* Callee should call reset_bandwidth() */
913 spin_unlock_irqrestore(&xhci->lock, flags);
914 return ret;
915 }
916
917 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
918 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma,
919 LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
920
921 xhci_zero_in_ctx(virt_dev);
922 /* Free any old rings */
923 for (i = 1; i < 31; ++i) {
924 if (virt_dev->new_ep_rings[i]) {
925 xhci_ring_free(xhci, virt_dev->ep_rings[i]);
926 virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
927 virt_dev->new_ep_rings[i] = NULL;
928 }
929 }
930
931 spin_unlock_irqrestore(&xhci->lock, flags);
932
933 return ret;
934}
935
936void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
937{
938 unsigned long flags;
939 struct xhci_hcd *xhci;
940 struct xhci_virt_device *virt_dev;
941 int i, ret;
942
943 ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
944 if (ret <= 0)
945 return;
946 xhci = hcd_to_xhci(hcd);
947
948 spin_lock_irqsave(&xhci->lock, flags);
949 if (!xhci->devs || !xhci->devs[udev->slot_id]) {
950 xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
951 __func__);
952 spin_unlock_irqrestore(&xhci->lock, flags);
953 return;
954 }
955 xhci_dbg(xhci, "%s called for udev %#x\n", __func__, (unsigned int) udev);
956 virt_dev = xhci->devs[udev->slot_id];
957 /* Free any rings allocated for added endpoints */
958 for (i = 0; i < 31; ++i) {
959 if (virt_dev->new_ep_rings[i]) {
960 xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
961 virt_dev->new_ep_rings[i] = NULL;
962 }
963 }
964 xhci_zero_in_ctx(virt_dev);
965 spin_unlock_irqrestore(&xhci->lock, flags);
966}
967
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700968/*
969 * At this point, the struct usb_device is about to go away, the device has
970 * disconnected, and all traffic has been stopped and the endpoints have been
971 * disabled. Free any HC data structures associated with that device.
972 */
973void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
974{
975 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
976 unsigned long flags;
977
978 if (udev->slot_id == 0)
979 return;
980
981 spin_lock_irqsave(&xhci->lock, flags);
982 if (queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
983 spin_unlock_irqrestore(&xhci->lock, flags);
984 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
985 return;
986 }
987 ring_cmd_db(xhci);
988 spin_unlock_irqrestore(&xhci->lock, flags);
989 /*
990 * Event command completion handler will free any data structures
991 * associated with the slot
992 */
993}
994
995/*
996 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
997 * timed out, or allocating memory failed. Returns 1 on success.
998 */
999int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
1000{
1001 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1002 unsigned long flags;
1003 int timeleft;
1004 int ret;
1005
1006 spin_lock_irqsave(&xhci->lock, flags);
1007 ret = queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
1008 if (ret) {
1009 spin_unlock_irqrestore(&xhci->lock, flags);
1010 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1011 return 0;
1012 }
1013 ring_cmd_db(xhci);
1014 spin_unlock_irqrestore(&xhci->lock, flags);
1015
1016 /* XXX: how much time for xHC slot assignment? */
1017 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1018 USB_CTRL_SET_TIMEOUT);
1019 if (timeleft <= 0) {
1020 xhci_warn(xhci, "%s while waiting for a slot\n",
1021 timeleft == 0 ? "Timeout" : "Signal");
1022 /* FIXME cancel the enable slot request */
1023 return 0;
1024 }
1025
1026 spin_lock_irqsave(&xhci->lock, flags);
1027 if (!xhci->slot_id) {
1028 xhci_err(xhci, "Error while assigning device slot ID\n");
1029 spin_unlock_irqrestore(&xhci->lock, flags);
1030 return 0;
1031 }
1032 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
1033 /* Disable slot, if we can do it without mem alloc */
1034 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
1035 if (!queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
1036 ring_cmd_db(xhci);
1037 spin_unlock_irqrestore(&xhci->lock, flags);
1038 return 0;
1039 }
1040 udev->slot_id = xhci->slot_id;
1041 /* Is this a LS or FS device under a HS hub? */
1042 /* Hub or peripherial? */
1043 spin_unlock_irqrestore(&xhci->lock, flags);
1044 return 1;
1045}
1046
1047/*
1048 * Issue an Address Device command (which will issue a SetAddress request to
1049 * the device).
1050 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
1051 * we should only issue and wait on one address command at the same time.
1052 *
1053 * We add one to the device address issued by the hardware because the USB core
1054 * uses address 1 for the root hubs (even though they're not really devices).
1055 */
1056int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
1057{
1058 unsigned long flags;
1059 int timeleft;
1060 struct xhci_virt_device *virt_dev;
1061 int ret = 0;
1062 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1063 u32 temp;
1064
1065 if (!udev->slot_id) {
1066 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
1067 return -EINVAL;
1068 }
1069
1070 spin_lock_irqsave(&xhci->lock, flags);
1071 virt_dev = xhci->devs[udev->slot_id];
1072
1073 /* If this is a Set Address to an unconfigured device, setup ep 0 */
1074 if (!udev->config)
1075 xhci_setup_addressable_virt_dev(xhci, udev);
1076 /* Otherwise, assume the core has the device configured how it wants */
1077
1078 ret = queue_address_device(xhci, virt_dev->in_ctx_dma, udev->slot_id);
1079 if (ret) {
1080 spin_unlock_irqrestore(&xhci->lock, flags);
1081 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
1082 return ret;
1083 }
1084 ring_cmd_db(xhci);
1085 spin_unlock_irqrestore(&xhci->lock, flags);
1086
1087 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
1088 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
1089 USB_CTRL_SET_TIMEOUT);
1090 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
1091 * the SetAddress() "recovery interval" required by USB and aborting the
1092 * command on a timeout.
1093 */
1094 if (timeleft <= 0) {
1095 xhci_warn(xhci, "%s while waiting for a slot\n",
1096 timeleft == 0 ? "Timeout" : "Signal");
1097 /* FIXME cancel the address device command */
1098 return -ETIME;
1099 }
1100
1101 spin_lock_irqsave(&xhci->lock, flags);
1102 switch (virt_dev->cmd_status) {
1103 case COMP_CTX_STATE:
1104 case COMP_EBADSLT:
1105 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
1106 udev->slot_id);
1107 ret = -EINVAL;
1108 break;
1109 case COMP_TX_ERR:
1110 dev_warn(&udev->dev, "Device not responding to set address.\n");
1111 ret = -EPROTO;
1112 break;
1113 case COMP_SUCCESS:
1114 xhci_dbg(xhci, "Successful Address Device command\n");
1115 break;
1116 default:
1117 xhci_err(xhci, "ERROR: unexpected command completion "
1118 "code 0x%x.\n", virt_dev->cmd_status);
1119 ret = -EINVAL;
1120 break;
1121 }
1122 if (ret) {
1123 spin_unlock_irqrestore(&xhci->lock, flags);
1124 return ret;
1125 }
1126 temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[0]);
1127 xhci_dbg(xhci, "Op regs DCBAA ptr[0] = %#08x\n", temp);
1128 temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[1]);
1129 xhci_dbg(xhci, "Op regs DCBAA ptr[1] = %#08x\n", temp);
1130 xhci_dbg(xhci, "Slot ID %d dcbaa entry[0] @%08x = %#08x\n",
1131 udev->slot_id,
1132 (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id],
1133 xhci->dcbaa->dev_context_ptrs[2*udev->slot_id]);
1134 xhci_dbg(xhci, "Slot ID %d dcbaa entry[1] @%08x = %#08x\n",
1135 udev->slot_id,
1136 (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1],
1137 xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1]);
1138 xhci_dbg(xhci, "Output Context DMA address = %#08x\n",
1139 virt_dev->out_ctx_dma);
1140 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
1141 xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
1142 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
1143 xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
1144 /*
1145 * USB core uses address 1 for the roothubs, so we add one to the
1146 * address given back to us by the HC.
1147 */
1148 udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001149 /* Zero the input context control for later use */
1150 virt_dev->in_ctx->add_flags = 0;
1151 virt_dev->in_ctx->drop_flags = 0;
1152 /* Mirror flags in the output context for future ep enable/disable */
1153 virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
1154 virt_dev->out_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07001155 spin_unlock_irqrestore(&xhci->lock, flags);
1156
1157 xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
1158 /* XXX Meh, not sure if anyone else but choose_address uses this. */
1159 set_bit(udev->devnum, udev->bus->devmap.devicemap);
1160
1161 return 0;
1162}
1163
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001164int xhci_get_frame(struct usb_hcd *hcd)
1165{
1166 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1167 /* EHCI mods by the periodic size. Why? */
1168 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
1169}
1170
1171MODULE_DESCRIPTION(DRIVER_DESC);
1172MODULE_AUTHOR(DRIVER_AUTHOR);
1173MODULE_LICENSE("GPL");
1174
1175static int __init xhci_hcd_init(void)
1176{
1177#ifdef CONFIG_PCI
1178 int retval = 0;
1179
1180 retval = xhci_register_pci();
1181
1182 if (retval < 0) {
1183 printk(KERN_DEBUG "Problem registering PCI driver.");
1184 return retval;
1185 }
1186#endif
1187 return 0;
1188}
1189module_init(xhci_hcd_init);
1190
1191static void __exit xhci_hcd_cleanup(void)
1192{
1193#ifdef CONFIG_PCI
1194 xhci_unregister_pci();
1195#endif
1196}
1197module_exit(xhci_hcd_cleanup);