xhci-hcd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. #include <linux/irq.h>
  23. #include <linux/module.h>
  24. #include "xhci.h"
  25. #define DRIVER_AUTHOR "Sarah Sharp"
  26. #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
  27. /* TODO: copied from ehci-hcd.c - can this be refactored? */
  28. /*
  29. * handshake - spin reading hc until handshake completes or fails
  30. * @ptr: address of hc register to be read
  31. * @mask: bits to look at in result of read
  32. * @done: value of those bits when handshake succeeds
  33. * @usec: timeout in microseconds
  34. *
  35. * Returns negative errno, or zero on success
  36. *
  37. * Success happens when the "mask" bits have the specified value (hardware
  38. * handshake done). There are two failure modes: "usec" have passed (major
  39. * hardware flakeout), or the register reads as all-ones (hardware removed).
  40. */
  41. static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
  42. u32 mask, u32 done, int usec)
  43. {
  44. u32 result;
  45. do {
  46. result = xhci_readl(xhci, ptr);
  47. if (result == ~(u32)0) /* card removed */
  48. return -ENODEV;
  49. result &= mask;
  50. if (result == done)
  51. return 0;
  52. udelay(1);
  53. usec--;
  54. } while (usec > 0);
  55. return -ETIMEDOUT;
  56. }
  57. /*
  58. * Force HC into halt state.
  59. *
  60. * Disable any IRQs and clear the run/stop bit.
  61. * HC will complete any current and actively pipelined transactions, and
  62. * should halt within 16 microframes of the run/stop bit being cleared.
  63. * Read HC Halted bit in the status register to see when the HC is finished.
  64. * XXX: shouldn't we set HC_STATE_HALT here somewhere?
  65. */
  66. int xhci_halt(struct xhci_hcd *xhci)
  67. {
  68. u32 halted;
  69. u32 cmd;
  70. u32 mask;
  71. xhci_dbg(xhci, "// Halt the HC\n");
  72. /* Disable all interrupts from the host controller */
  73. mask = ~(XHCI_IRQS);
  74. halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
  75. if (!halted)
  76. mask &= ~CMD_RUN;
  77. cmd = xhci_readl(xhci, &xhci->op_regs->command);
  78. cmd &= mask;
  79. xhci_writel(xhci, cmd, &xhci->op_regs->command);
  80. return handshake(xhci, &xhci->op_regs->status,
  81. STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
  82. }
  83. /*
  84. * Reset a halted HC, and set the internal HC state to HC_STATE_HALT.
  85. *
  86. * This resets pipelines, timers, counters, state machines, etc.
  87. * Transactions will be terminated immediately, and operational registers
  88. * will be set to their defaults.
  89. */
  90. int xhci_reset(struct xhci_hcd *xhci)
  91. {
  92. u32 command;
  93. u32 state;
  94. state = xhci_readl(xhci, &xhci->op_regs->status);
  95. BUG_ON((state & STS_HALT) == 0);
  96. xhci_dbg(xhci, "// Reset the HC\n");
  97. command = xhci_readl(xhci, &xhci->op_regs->command);
  98. command |= CMD_RESET;
  99. xhci_writel(xhci, command, &xhci->op_regs->command);
  100. /* XXX: Why does EHCI set this here? Shouldn't other code do this? */
  101. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  102. return handshake(xhci, &xhci->op_regs->command, CMD_RESET, 0, 250 * 1000);
  103. }
  104. /*
  105. * Stop the HC from processing the endpoint queues.
  106. */
  107. static void xhci_quiesce(struct xhci_hcd *xhci)
  108. {
  109. /*
  110. * Queues are per endpoint, so we need to disable an endpoint or slot.
  111. *
  112. * To disable a slot, we need to insert a disable slot command on the
  113. * command ring and ring the doorbell. This will also free any internal
  114. * resources associated with the slot (which might not be what we want).
  115. *
  116. * A Release Endpoint command sounds better - doesn't free internal HC
  117. * memory, but removes the endpoints from the schedule and releases the
  118. * bandwidth, disables the doorbells, and clears the endpoint enable
  119. * flag. Usually used prior to a set interface command.
  120. *
  121. * TODO: Implement after command ring code is done.
  122. */
  123. BUG_ON(!HC_IS_RUNNING(xhci_to_hcd(xhci)->state));
  124. xhci_dbg(xhci, "Finished quiescing -- code not written yet\n");
  125. }
  126. #if 0
  127. /* Set up MSI-X table for entry 0 (may claim other entries later) */
  128. static int xhci_setup_msix(struct xhci_hcd *xhci)
  129. {
  130. int ret;
  131. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  132. xhci->msix_count = 0;
  133. /* XXX: did I do this right? ixgbe does kcalloc for more than one */
  134. xhci->msix_entries = kmalloc(sizeof(struct msix_entry), GFP_KERNEL);
  135. if (!xhci->msix_entries) {
  136. xhci_err(xhci, "Failed to allocate MSI-X entries\n");
  137. return -ENOMEM;
  138. }
  139. xhci->msix_entries[0].entry = 0;
  140. ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
  141. if (ret) {
  142. xhci_err(xhci, "Failed to enable MSI-X\n");
  143. goto free_entries;
  144. }
  145. /*
  146. * Pass the xhci pointer value as the request_irq "cookie".
  147. * If more irqs are added, this will need to be unique for each one.
  148. */
  149. ret = request_irq(xhci->msix_entries[0].vector, &xhci_irq, 0,
  150. "xHCI", xhci_to_hcd(xhci));
  151. if (ret) {
  152. xhci_err(xhci, "Failed to allocate MSI-X interrupt\n");
  153. goto disable_msix;
  154. }
  155. xhci_dbg(xhci, "Finished setting up MSI-X\n");
  156. return 0;
  157. disable_msix:
  158. pci_disable_msix(pdev);
  159. free_entries:
  160. kfree(xhci->msix_entries);
  161. xhci->msix_entries = NULL;
  162. return ret;
  163. }
  164. /* XXX: code duplication; can xhci_setup_msix call this? */
  165. /* Free any IRQs and disable MSI-X */
  166. static void xhci_cleanup_msix(struct xhci_hcd *xhci)
  167. {
  168. struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
  169. if (!xhci->msix_entries)
  170. return;
  171. free_irq(xhci->msix_entries[0].vector, xhci);
  172. pci_disable_msix(pdev);
  173. kfree(xhci->msix_entries);
  174. xhci->msix_entries = NULL;
  175. xhci_dbg(xhci, "Finished cleaning up MSI-X\n");
  176. }
  177. #endif
  178. /*
  179. * Initialize memory for HCD and xHC (one-time init).
  180. *
  181. * Program the PAGESIZE register, initialize the device context array, create
  182. * device contexts (?), set up a command ring segment (or two?), create event
  183. * ring (one for now).
  184. */
  185. int xhci_init(struct usb_hcd *hcd)
  186. {
  187. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  188. int retval = 0;
  189. xhci_dbg(xhci, "xhci_init\n");
  190. spin_lock_init(&xhci->lock);
  191. retval = xhci_mem_init(xhci, GFP_KERNEL);
  192. xhci_dbg(xhci, "Finished xhci_init\n");
  193. return retval;
  194. }
  195. /*
  196. * Called in interrupt context when there might be work
  197. * queued on the event ring
  198. *
  199. * xhci->lock must be held by caller.
  200. */
  201. static void xhci_work(struct xhci_hcd *xhci)
  202. {
  203. u32 temp;
  204. /*
  205. * Clear the op reg interrupt status first,
  206. * so we can receive interrupts from other MSI-X interrupters.
  207. * Write 1 to clear the interrupt status.
  208. */
  209. temp = xhci_readl(xhci, &xhci->op_regs->status);
  210. temp |= STS_EINT;
  211. xhci_writel(xhci, temp, &xhci->op_regs->status);
  212. /* FIXME when MSI-X is supported and there are multiple vectors */
  213. /* Clear the MSI-X event interrupt status */
  214. /* Acknowledge the interrupt */
  215. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  216. temp |= 0x3;
  217. xhci_writel(xhci, temp, &xhci->ir_set->irq_pending);
  218. /* Flush posted writes */
  219. xhci_readl(xhci, &xhci->ir_set->irq_pending);
  220. /* FIXME this should be a delayed service routine that clears the EHB */
  221. handle_event(xhci);
  222. /* Clear the event handler busy flag; the event ring should be empty. */
  223. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  224. xhci_writel(xhci, temp & ~ERST_EHB, &xhci->ir_set->erst_dequeue[0]);
  225. /* Flush posted writes -- FIXME is this necessary? */
  226. xhci_readl(xhci, &xhci->ir_set->irq_pending);
  227. }
  228. /*-------------------------------------------------------------------------*/
  229. /*
  230. * xHCI spec says we can get an interrupt, and if the HC has an error condition,
  231. * we might get bad data out of the event ring. Section 4.10.2.7 has a list of
  232. * indicators of an event TRB error, but we check the status *first* to be safe.
  233. */
  234. irqreturn_t xhci_irq(struct usb_hcd *hcd)
  235. {
  236. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  237. u32 temp, temp2;
  238. spin_lock(&xhci->lock);
  239. /* Check if the xHC generated the interrupt, or the irq is shared */
  240. temp = xhci_readl(xhci, &xhci->op_regs->status);
  241. temp2 = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  242. if (!(temp & STS_EINT) && !ER_IRQ_PENDING(temp2)) {
  243. spin_unlock(&xhci->lock);
  244. return IRQ_NONE;
  245. }
  246. temp = xhci_readl(xhci, &xhci->op_regs->status);
  247. if (temp & STS_FATAL) {
  248. xhci_warn(xhci, "WARNING: Host System Error\n");
  249. xhci_halt(xhci);
  250. xhci_to_hcd(xhci)->state = HC_STATE_HALT;
  251. return -ESHUTDOWN;
  252. }
  253. xhci_work(xhci);
  254. spin_unlock(&xhci->lock);
  255. return IRQ_HANDLED;
  256. }
  257. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  258. void event_ring_work(unsigned long arg)
  259. {
  260. unsigned long flags;
  261. int temp;
  262. struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
  263. int i, j;
  264. xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
  265. spin_lock_irqsave(&xhci->lock, flags);
  266. temp = xhci_readl(xhci, &xhci->op_regs->status);
  267. xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
  268. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  269. xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
  270. xhci_dbg(xhci, "No-op commands handled = %d\n", xhci->noops_handled);
  271. xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
  272. xhci->error_bitmask = 0;
  273. xhci_dbg(xhci, "Event ring:\n");
  274. xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
  275. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  276. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  277. temp &= ERST_PTR_MASK;
  278. xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
  279. xhci_dbg(xhci, "Command ring:\n");
  280. xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
  281. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  282. xhci_dbg_cmd_ptrs(xhci);
  283. for (i = 0; i < MAX_HC_SLOTS; ++i) {
  284. if (xhci->devs[i]) {
  285. for (j = 0; j < 31; ++j) {
  286. if (xhci->devs[i]->ep_rings[j]) {
  287. xhci_dbg(xhci, "Dev %d endpoint ring %d:\n", i, j);
  288. xhci_debug_segment(xhci, xhci->devs[i]->ep_rings[j]->deq_seg);
  289. }
  290. }
  291. }
  292. }
  293. if (xhci->noops_submitted != NUM_TEST_NOOPS)
  294. if (setup_one_noop(xhci))
  295. ring_cmd_db(xhci);
  296. spin_unlock_irqrestore(&xhci->lock, flags);
  297. if (!xhci->zombie)
  298. mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
  299. else
  300. xhci_dbg(xhci, "Quit polling the event ring.\n");
  301. }
  302. #endif
  303. /*
  304. * Start the HC after it was halted.
  305. *
  306. * This function is called by the USB core when the HC driver is added.
  307. * Its opposite is xhci_stop().
  308. *
  309. * xhci_init() must be called once before this function can be called.
  310. * Reset the HC, enable device slot contexts, program DCBAAP, and
  311. * set command ring pointer and event ring pointer.
  312. *
  313. * Setup MSI-X vectors and enable interrupts.
  314. */
  315. int xhci_run(struct usb_hcd *hcd)
  316. {
  317. u32 temp;
  318. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  319. void (*doorbell)(struct xhci_hcd *) = NULL;
  320. hcd->uses_new_polling = 1;
  321. hcd->poll_rh = 0;
  322. xhci_dbg(xhci, "xhci_run\n");
  323. #if 0 /* FIXME: MSI not setup yet */
  324. /* Do this at the very last minute */
  325. ret = xhci_setup_msix(xhci);
  326. if (!ret)
  327. return ret;
  328. return -ENOSYS;
  329. #endif
  330. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  331. init_timer(&xhci->event_ring_timer);
  332. xhci->event_ring_timer.data = (unsigned long) xhci;
  333. xhci->event_ring_timer.function = event_ring_work;
  334. /* Poll the event ring */
  335. xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
  336. xhci->zombie = 0;
  337. xhci_dbg(xhci, "Setting event ring polling timer\n");
  338. add_timer(&xhci->event_ring_timer);
  339. #endif
  340. xhci_dbg(xhci, "// Set the interrupt modulation register\n");
  341. temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
  342. temp &= 0xffff;
  343. temp |= (u32) 160;
  344. xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
  345. /* Set the HCD state before we enable the irqs */
  346. hcd->state = HC_STATE_RUNNING;
  347. temp = xhci_readl(xhci, &xhci->op_regs->command);
  348. temp |= (CMD_EIE);
  349. xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
  350. temp);
  351. xhci_writel(xhci, temp, &xhci->op_regs->command);
  352. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  353. xhci_dbg(xhci, "// Enabling event ring interrupter 0x%x"
  354. " by writing 0x%x to irq_pending\n",
  355. (unsigned int) xhci->ir_set,
  356. (unsigned int) ER_IRQ_ENABLE(temp));
  357. xhci_writel(xhci, ER_IRQ_ENABLE(temp),
  358. &xhci->ir_set->irq_pending);
  359. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  360. if (NUM_TEST_NOOPS > 0)
  361. doorbell = setup_one_noop(xhci);
  362. xhci_dbg(xhci, "Command ring memory map follows:\n");
  363. xhci_debug_ring(xhci, xhci->cmd_ring);
  364. xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
  365. xhci_dbg_cmd_ptrs(xhci);
  366. xhci_dbg(xhci, "ERST memory map follows:\n");
  367. xhci_dbg_erst(xhci, &xhci->erst);
  368. xhci_dbg(xhci, "Event ring:\n");
  369. xhci_debug_ring(xhci, xhci->event_ring);
  370. xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
  371. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[1]);
  372. xhci_dbg(xhci, "ERST deq upper = 0x%x\n", temp);
  373. temp = xhci_readl(xhci, &xhci->ir_set->erst_dequeue[0]);
  374. temp &= ERST_PTR_MASK;
  375. xhci_dbg(xhci, "ERST deq = 0x%x\n", temp);
  376. temp = xhci_readl(xhci, &xhci->op_regs->command);
  377. temp |= (CMD_RUN);
  378. xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
  379. temp);
  380. xhci_writel(xhci, temp, &xhci->op_regs->command);
  381. /* Flush PCI posted writes */
  382. temp = xhci_readl(xhci, &xhci->op_regs->command);
  383. xhci_dbg(xhci, "// @%x = 0x%x\n",
  384. (unsigned int) &xhci->op_regs->command, temp);
  385. if (doorbell)
  386. (*doorbell)(xhci);
  387. xhci_dbg(xhci, "Finished xhci_run\n");
  388. return 0;
  389. }
  390. /*
  391. * Stop xHCI driver.
  392. *
  393. * This function is called by the USB core when the HC driver is removed.
  394. * Its opposite is xhci_run().
  395. *
  396. * Disable device contexts, disable IRQs, and quiesce the HC.
  397. * Reset the HC, finish any completed transactions, and cleanup memory.
  398. */
  399. void xhci_stop(struct usb_hcd *hcd)
  400. {
  401. u32 temp;
  402. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  403. spin_lock_irq(&xhci->lock);
  404. if (HC_IS_RUNNING(hcd->state))
  405. xhci_quiesce(xhci);
  406. xhci_halt(xhci);
  407. xhci_reset(xhci);
  408. spin_unlock_irq(&xhci->lock);
  409. #if 0 /* No MSI yet */
  410. xhci_cleanup_msix(xhci);
  411. #endif
  412. #ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
  413. /* Tell the event ring poll function not to reschedule */
  414. xhci->zombie = 1;
  415. del_timer_sync(&xhci->event_ring_timer);
  416. #endif
  417. xhci_dbg(xhci, "// Disabling event ring interrupts\n");
  418. temp = xhci_readl(xhci, &xhci->op_regs->status);
  419. xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
  420. temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
  421. xhci_writel(xhci, ER_IRQ_DISABLE(temp),
  422. &xhci->ir_set->irq_pending);
  423. xhci_print_ir_set(xhci, xhci->ir_set, 0);
  424. xhci_dbg(xhci, "cleaning up memory\n");
  425. xhci_mem_cleanup(xhci);
  426. xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
  427. xhci_readl(xhci, &xhci->op_regs->status));
  428. }
  429. /*
  430. * Shutdown HC (not bus-specific)
  431. *
  432. * This is called when the machine is rebooting or halting. We assume that the
  433. * machine will be powered off, and the HC's internal state will be reset.
  434. * Don't bother to free memory.
  435. */
  436. void xhci_shutdown(struct usb_hcd *hcd)
  437. {
  438. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  439. spin_lock_irq(&xhci->lock);
  440. xhci_halt(xhci);
  441. spin_unlock_irq(&xhci->lock);
  442. #if 0
  443. xhci_cleanup_msix(xhci);
  444. #endif
  445. xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
  446. xhci_readl(xhci, &xhci->op_regs->status));
  447. }
  448. /*-------------------------------------------------------------------------*/
  449. /*
  450. * At this point, the struct usb_device is about to go away, the device has
  451. * disconnected, and all traffic has been stopped and the endpoints have been
  452. * disabled. Free any HC data structures associated with that device.
  453. */
  454. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  455. {
  456. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  457. unsigned long flags;
  458. if (udev->slot_id == 0)
  459. return;
  460. spin_lock_irqsave(&xhci->lock, flags);
  461. if (queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  462. spin_unlock_irqrestore(&xhci->lock, flags);
  463. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  464. return;
  465. }
  466. ring_cmd_db(xhci);
  467. spin_unlock_irqrestore(&xhci->lock, flags);
  468. /*
  469. * Event command completion handler will free any data structures
  470. * associated with the slot
  471. */
  472. }
  473. /*
  474. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  475. * timed out, or allocating memory failed. Returns 1 on success.
  476. */
  477. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  478. {
  479. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  480. unsigned long flags;
  481. int timeleft;
  482. int ret;
  483. spin_lock_irqsave(&xhci->lock, flags);
  484. ret = queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  485. if (ret) {
  486. spin_unlock_irqrestore(&xhci->lock, flags);
  487. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  488. return 0;
  489. }
  490. ring_cmd_db(xhci);
  491. spin_unlock_irqrestore(&xhci->lock, flags);
  492. /* XXX: how much time for xHC slot assignment? */
  493. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  494. USB_CTRL_SET_TIMEOUT);
  495. if (timeleft <= 0) {
  496. xhci_warn(xhci, "%s while waiting for a slot\n",
  497. timeleft == 0 ? "Timeout" : "Signal");
  498. /* FIXME cancel the enable slot request */
  499. return 0;
  500. }
  501. spin_lock_irqsave(&xhci->lock, flags);
  502. if (!xhci->slot_id) {
  503. xhci_err(xhci, "Error while assigning device slot ID\n");
  504. spin_unlock_irqrestore(&xhci->lock, flags);
  505. return 0;
  506. }
  507. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
  508. /* Disable slot, if we can do it without mem alloc */
  509. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  510. if (!queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  511. ring_cmd_db(xhci);
  512. spin_unlock_irqrestore(&xhci->lock, flags);
  513. return 0;
  514. }
  515. udev->slot_id = xhci->slot_id;
  516. /* Is this a LS or FS device under a HS hub? */
  517. /* Hub or peripherial? */
  518. spin_unlock_irqrestore(&xhci->lock, flags);
  519. return 1;
  520. }
  521. /*
  522. * Issue an Address Device command (which will issue a SetAddress request to
  523. * the device).
  524. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  525. * we should only issue and wait on one address command at the same time.
  526. *
  527. * We add one to the device address issued by the hardware because the USB core
  528. * uses address 1 for the root hubs (even though they're not really devices).
  529. */
  530. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  531. {
  532. unsigned long flags;
  533. int timeleft;
  534. struct xhci_virt_device *virt_dev;
  535. int ret = 0;
  536. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  537. u32 temp;
  538. if (!udev->slot_id) {
  539. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  540. return -EINVAL;
  541. }
  542. spin_lock_irqsave(&xhci->lock, flags);
  543. virt_dev = xhci->devs[udev->slot_id];
  544. /* If this is a Set Address to an unconfigured device, setup ep 0 */
  545. if (!udev->config)
  546. xhci_setup_addressable_virt_dev(xhci, udev);
  547. /* Otherwise, assume the core has the device configured how it wants */
  548. ret = queue_address_device(xhci, virt_dev->in_ctx_dma, udev->slot_id);
  549. if (ret) {
  550. spin_unlock_irqrestore(&xhci->lock, flags);
  551. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  552. return ret;
  553. }
  554. ring_cmd_db(xhci);
  555. spin_unlock_irqrestore(&xhci->lock, flags);
  556. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  557. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  558. USB_CTRL_SET_TIMEOUT);
  559. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  560. * the SetAddress() "recovery interval" required by USB and aborting the
  561. * command on a timeout.
  562. */
  563. if (timeleft <= 0) {
  564. xhci_warn(xhci, "%s while waiting for a slot\n",
  565. timeleft == 0 ? "Timeout" : "Signal");
  566. /* FIXME cancel the address device command */
  567. return -ETIME;
  568. }
  569. spin_lock_irqsave(&xhci->lock, flags);
  570. switch (virt_dev->cmd_status) {
  571. case COMP_CTX_STATE:
  572. case COMP_EBADSLT:
  573. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  574. udev->slot_id);
  575. ret = -EINVAL;
  576. break;
  577. case COMP_TX_ERR:
  578. dev_warn(&udev->dev, "Device not responding to set address.\n");
  579. ret = -EPROTO;
  580. break;
  581. case COMP_SUCCESS:
  582. xhci_dbg(xhci, "Successful Address Device command\n");
  583. break;
  584. default:
  585. xhci_err(xhci, "ERROR: unexpected command completion "
  586. "code 0x%x.\n", virt_dev->cmd_status);
  587. ret = -EINVAL;
  588. break;
  589. }
  590. if (ret) {
  591. spin_unlock_irqrestore(&xhci->lock, flags);
  592. return ret;
  593. }
  594. temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[0]);
  595. xhci_dbg(xhci, "Op regs DCBAA ptr[0] = %#08x\n", temp);
  596. temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[1]);
  597. xhci_dbg(xhci, "Op regs DCBAA ptr[1] = %#08x\n", temp);
  598. xhci_dbg(xhci, "Slot ID %d dcbaa entry[0] @%08x = %#08x\n",
  599. udev->slot_id,
  600. (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id],
  601. xhci->dcbaa->dev_context_ptrs[2*udev->slot_id]);
  602. xhci_dbg(xhci, "Slot ID %d dcbaa entry[1] @%08x = %#08x\n",
  603. udev->slot_id,
  604. (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1],
  605. xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1]);
  606. xhci_dbg(xhci, "Output Context DMA address = %#08x\n",
  607. virt_dev->out_ctx_dma);
  608. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  609. xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
  610. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  611. xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
  612. /*
  613. * USB core uses address 1 for the roothubs, so we add one to the
  614. * address given back to us by the HC.
  615. */
  616. udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
  617. /* FIXME: Zero the input context control for later use? */
  618. spin_unlock_irqrestore(&xhci->lock, flags);
  619. xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
  620. /* XXX Meh, not sure if anyone else but choose_address uses this. */
  621. set_bit(udev->devnum, udev->bus->devmap.devicemap);
  622. return 0;
  623. }
  624. int xhci_get_frame(struct usb_hcd *hcd)
  625. {
  626. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  627. /* EHCI mods by the periodic size. Why? */
  628. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  629. }
  630. MODULE_DESCRIPTION(DRIVER_DESC);
  631. MODULE_AUTHOR(DRIVER_AUTHOR);
  632. MODULE_LICENSE("GPL");
  633. static int __init xhci_hcd_init(void)
  634. {
  635. #ifdef CONFIG_PCI
  636. int retval = 0;
  637. retval = xhci_register_pci();
  638. if (retval < 0) {
  639. printk(KERN_DEBUG "Problem registering PCI driver.");
  640. return retval;
  641. }
  642. #endif
  643. return 0;
  644. }
  645. module_init(xhci_hcd_init);
  646. static void __exit xhci_hcd_cleanup(void)
  647. {
  648. #ifdef CONFIG_PCI
  649. xhci_unregister_pci();
  650. #endif
  651. }
  652. module_exit(xhci_hcd_cleanup);