xhci-hcd.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
  451. * HCDs. Find the index for an endpoint given its descriptor. Use the return
  452. * value to right shift 1 for the bitmask.
  453. *
  454. * Index = (epnum * 2) + direction - 1,
  455. * where direction = 0 for OUT, 1 for IN.
  456. * For control endpoints, the IN index is used (OUT index is unused), so
  457. * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
  458. */
  459. unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
  460. {
  461. unsigned int index;
  462. if (usb_endpoint_xfer_control(desc))
  463. index = (unsigned int) (usb_endpoint_num(desc)*2);
  464. else
  465. index = (unsigned int) (usb_endpoint_num(desc)*2) +
  466. (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
  467. return index;
  468. }
  469. /* Find the flag for this endpoint (for use in the control context). Use the
  470. * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
  471. * bit 1, etc.
  472. */
  473. unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
  474. {
  475. return 1 << (xhci_get_endpoint_index(desc) + 1);
  476. }
  477. /* Compute the last valid endpoint context index. Basically, this is the
  478. * endpoint index plus one. For slot contexts with more than valid endpoint,
  479. * we find the most significant bit set in the added contexts flags.
  480. * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
  481. * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
  482. */
  483. static inline unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
  484. {
  485. return fls(added_ctxs) - 1;
  486. }
  487. /* Returns 1 if the arguments are OK;
  488. * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
  489. */
  490. int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
  491. struct usb_host_endpoint *ep, int check_ep, const char *func) {
  492. if (!hcd || (check_ep && !ep) || !udev) {
  493. printk(KERN_DEBUG "xHCI %s called with invalid args\n",
  494. func);
  495. return -EINVAL;
  496. }
  497. if (!udev->parent) {
  498. printk(KERN_DEBUG "xHCI %s called for root hub\n",
  499. func);
  500. return 0;
  501. }
  502. if (!udev->slot_id) {
  503. printk(KERN_DEBUG "xHCI %s called with unaddressed device\n",
  504. func);
  505. return -EINVAL;
  506. }
  507. return 1;
  508. }
  509. /*
  510. * non-error returns are a promise to giveback() the urb later
  511. * we drop ownership so next owner (or urb unlink) can get it
  512. */
  513. int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
  514. {
  515. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  516. unsigned long flags;
  517. int ret = 0;
  518. unsigned int slot_id, ep_index;
  519. if (!urb || xhci_check_args(hcd, urb->dev, urb->ep, true, __func__) <= 0)
  520. return -EINVAL;
  521. slot_id = urb->dev->slot_id;
  522. ep_index = xhci_get_endpoint_index(&urb->ep->desc);
  523. spin_lock_irqsave(&xhci->lock, flags);
  524. if (!xhci->devs || !xhci->devs[slot_id]) {
  525. if (!in_interrupt())
  526. dev_warn(&urb->dev->dev, "WARN: urb submitted for dev with no Slot ID\n");
  527. return -EINVAL;
  528. }
  529. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  530. if (!in_interrupt())
  531. xhci_dbg(xhci, "urb submitted during PCI suspend\n");
  532. ret = -ESHUTDOWN;
  533. goto exit;
  534. }
  535. if (usb_endpoint_xfer_control(&urb->ep->desc))
  536. ret = queue_ctrl_tx(xhci, mem_flags, urb, slot_id, ep_index);
  537. else if (usb_endpoint_xfer_bulk(&urb->ep->desc))
  538. ret = queue_bulk_tx(xhci, mem_flags, urb, slot_id, ep_index);
  539. else
  540. ret = -EINVAL;
  541. exit:
  542. spin_unlock_irqrestore(&xhci->lock, flags);
  543. return ret;
  544. }
  545. /* Remove from hardware lists
  546. * completions normally happen asynchronously
  547. */
  548. int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  549. {
  550. return -ENOSYS;
  551. }
  552. /* Drop an endpoint from a new bandwidth configuration for this device.
  553. * Only one call to this function is allowed per endpoint before
  554. * check_bandwidth() or reset_bandwidth() must be called.
  555. * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
  556. * add the endpoint to the schedule with possibly new parameters denoted by a
  557. * different endpoint descriptor in usb_host_endpoint.
  558. * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
  559. * not allowed.
  560. */
  561. int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
  562. struct usb_host_endpoint *ep)
  563. {
  564. unsigned long flags;
  565. struct xhci_hcd *xhci;
  566. struct xhci_device_control *in_ctx;
  567. unsigned int last_ctx;
  568. unsigned int ep_index;
  569. struct xhci_ep_ctx *ep_ctx;
  570. u32 drop_flag;
  571. u32 new_add_flags, new_drop_flags, new_slot_info;
  572. int ret;
  573. ret = xhci_check_args(hcd, udev, ep, 1, __func__);
  574. xhci_dbg(xhci, "%s called for udev %#x\n", __func__, (unsigned int) udev);
  575. if (ret <= 0)
  576. return ret;
  577. xhci = hcd_to_xhci(hcd);
  578. drop_flag = xhci_get_endpoint_flag(&ep->desc);
  579. if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
  580. xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
  581. __func__, drop_flag);
  582. return 0;
  583. }
  584. spin_lock_irqsave(&xhci->lock, flags);
  585. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  586. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  587. __func__);
  588. spin_unlock_irqrestore(&xhci->lock, flags);
  589. return -EINVAL;
  590. }
  591. in_ctx = xhci->devs[udev->slot_id]->in_ctx;
  592. ep_index = xhci_get_endpoint_index(&ep->desc);
  593. ep_ctx = &xhci->devs[udev->slot_id]->out_ctx->ep[ep_index];
  594. /* If the HC already knows the endpoint is disabled,
  595. * or the HCD has noted it is disabled, ignore this request
  596. */
  597. if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
  598. in_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
  599. xhci_warn(xhci, "xHCI %s called with disabled ep %#x\n",
  600. __func__, (unsigned int) ep);
  601. spin_unlock_irqrestore(&xhci->lock, flags);
  602. return 0;
  603. }
  604. in_ctx->drop_flags |= drop_flag;
  605. new_drop_flags = in_ctx->drop_flags;
  606. in_ctx->add_flags = ~drop_flag;
  607. new_add_flags = in_ctx->add_flags;
  608. last_ctx = xhci_last_valid_endpoint(in_ctx->add_flags);
  609. /* Update the last valid endpoint context, if we deleted the last one */
  610. if ((in_ctx->slot.dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
  611. in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
  612. in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
  613. }
  614. new_slot_info = in_ctx->slot.dev_info;
  615. xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
  616. spin_unlock_irqrestore(&xhci->lock, flags);
  617. xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  618. (unsigned int) ep->desc.bEndpointAddress,
  619. udev->slot_id,
  620. (unsigned int) new_drop_flags,
  621. (unsigned int) new_add_flags,
  622. (unsigned int) new_slot_info);
  623. return 0;
  624. }
  625. /* Add an endpoint to a new possible 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. */
  634. int xhci_add_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 ep_index;
  641. struct xhci_ep_ctx *ep_ctx;
  642. u32 added_ctxs;
  643. unsigned int last_ctx;
  644. u32 new_add_flags, new_drop_flags, new_slot_info;
  645. int ret = 0;
  646. ret = xhci_check_args(hcd, udev, ep, 1, __func__);
  647. if (ret <= 0)
  648. return ret;
  649. xhci = hcd_to_xhci(hcd);
  650. added_ctxs = xhci_get_endpoint_flag(&ep->desc);
  651. last_ctx = xhci_last_valid_endpoint(added_ctxs);
  652. if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
  653. /* FIXME when we have to issue an evaluate endpoint command to
  654. * deal with ep0 max packet size changing once we get the
  655. * descriptors
  656. */
  657. xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
  658. __func__, added_ctxs);
  659. return 0;
  660. }
  661. spin_lock_irqsave(&xhci->lock, flags);
  662. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  663. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  664. __func__);
  665. spin_unlock_irqrestore(&xhci->lock, flags);
  666. return -EINVAL;
  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 HCD has already noted the endpoint is enabled,
  672. * ignore this request.
  673. */
  674. if (in_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
  675. xhci_warn(xhci, "xHCI %s called with enabled ep %#x\n",
  676. __func__, (unsigned int) ep);
  677. spin_unlock_irqrestore(&xhci->lock, flags);
  678. return 0;
  679. }
  680. if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id], udev, ep) < 0) {
  681. dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
  682. __func__, ep->desc.bEndpointAddress);
  683. spin_unlock_irqrestore(&xhci->lock, flags);
  684. return -ENOMEM;
  685. }
  686. in_ctx->add_flags |= added_ctxs;
  687. new_add_flags = in_ctx->add_flags;
  688. /* If xhci_endpoint_disable() was called for this endpoint, but the
  689. * xHC hasn't been notified yet through the check_bandwidth() call,
  690. * this re-adds a new state for the endpoint from the new endpoint
  691. * descriptors. We must drop and re-add this endpoint, so we leave the
  692. * drop flags alone.
  693. */
  694. new_drop_flags = in_ctx->drop_flags;
  695. /* Update the last valid endpoint context, if we just added one past */
  696. if ((in_ctx->slot.dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
  697. in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
  698. in_ctx->slot.dev_info |= LAST_CTX(last_ctx);
  699. }
  700. new_slot_info = in_ctx->slot.dev_info;
  701. spin_unlock_irqrestore(&xhci->lock, flags);
  702. xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
  703. (unsigned int) ep->desc.bEndpointAddress,
  704. udev->slot_id,
  705. (unsigned int) new_drop_flags,
  706. (unsigned int) new_add_flags,
  707. (unsigned int) new_slot_info);
  708. return 0;
  709. }
  710. static void xhci_zero_in_ctx(struct xhci_virt_device *virt_dev)
  711. {
  712. struct xhci_ep_ctx *ep_ctx;
  713. int i;
  714. /* When a device's add flag and drop flag are zero, any subsequent
  715. * configure endpoint command will leave that endpoint's state
  716. * untouched. Make sure we don't leave any old state in the input
  717. * endpoint contexts.
  718. */
  719. virt_dev->in_ctx->drop_flags = 0;
  720. virt_dev->in_ctx->add_flags = 0;
  721. virt_dev->in_ctx->slot.dev_info &= ~LAST_CTX_MASK;
  722. /* Endpoint 0 is always valid */
  723. virt_dev->in_ctx->slot.dev_info |= LAST_CTX(1);
  724. for (i = 1; i < 31; ++i) {
  725. ep_ctx = &virt_dev->in_ctx->ep[i];
  726. ep_ctx->ep_info = 0;
  727. ep_ctx->ep_info2 = 0;
  728. ep_ctx->deq[0] = 0;
  729. ep_ctx->deq[1] = 0;
  730. ep_ctx->tx_info = 0;
  731. }
  732. }
  733. int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  734. {
  735. int i;
  736. int ret = 0;
  737. int timeleft;
  738. unsigned long flags;
  739. struct xhci_hcd *xhci;
  740. struct xhci_virt_device *virt_dev;
  741. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  742. if (ret <= 0)
  743. return ret;
  744. xhci = hcd_to_xhci(hcd);
  745. spin_lock_irqsave(&xhci->lock, flags);
  746. if (!udev->slot_id || !xhci->devs || !xhci->devs[udev->slot_id]) {
  747. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  748. __func__);
  749. spin_unlock_irqrestore(&xhci->lock, flags);
  750. return -EINVAL;
  751. }
  752. xhci_dbg(xhci, "%s called for udev %#x\n", __func__, (unsigned int) udev);
  753. virt_dev = xhci->devs[udev->slot_id];
  754. /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
  755. virt_dev->in_ctx->add_flags |= SLOT_FLAG;
  756. virt_dev->in_ctx->add_flags &= ~EP0_FLAG;
  757. virt_dev->in_ctx->drop_flags &= ~SLOT_FLAG;
  758. virt_dev->in_ctx->drop_flags &= ~EP0_FLAG;
  759. xhci_dbg(xhci, "New Input Control Context:\n");
  760. xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma,
  761. LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
  762. ret = queue_configure_endpoint(xhci, virt_dev->in_ctx_dma, udev->slot_id);
  763. if (ret < 0) {
  764. xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
  765. spin_unlock_irqrestore(&xhci->lock, flags);
  766. return -ENOMEM;
  767. }
  768. ring_cmd_db(xhci);
  769. spin_unlock_irqrestore(&xhci->lock, flags);
  770. /* Wait for the configure endpoint command to complete */
  771. timeleft = wait_for_completion_interruptible_timeout(
  772. &virt_dev->cmd_completion,
  773. USB_CTRL_SET_TIMEOUT);
  774. if (timeleft <= 0) {
  775. xhci_warn(xhci, "%s while waiting for configure endpoint command\n",
  776. timeleft == 0 ? "Timeout" : "Signal");
  777. /* FIXME cancel the configure endpoint command */
  778. return -ETIME;
  779. }
  780. spin_lock_irqsave(&xhci->lock, flags);
  781. switch (virt_dev->cmd_status) {
  782. case COMP_ENOMEM:
  783. dev_warn(&udev->dev, "Not enough host controller resources "
  784. "for new device state.\n");
  785. ret = -ENOMEM;
  786. /* FIXME: can we allocate more resources for the HC? */
  787. break;
  788. case COMP_BW_ERR:
  789. dev_warn(&udev->dev, "Not enough bandwidth "
  790. "for new device state.\n");
  791. ret = -ENOSPC;
  792. /* FIXME: can we go back to the old state? */
  793. break;
  794. case COMP_TRB_ERR:
  795. /* the HCD set up something wrong */
  796. dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, add flag = 1, "
  797. "and endpoint is not disabled.\n");
  798. ret = -EINVAL;
  799. break;
  800. case COMP_SUCCESS:
  801. dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
  802. break;
  803. default:
  804. xhci_err(xhci, "ERROR: unexpected command completion "
  805. "code 0x%x.\n", virt_dev->cmd_status);
  806. ret = -EINVAL;
  807. break;
  808. }
  809. if (ret) {
  810. /* Callee should call reset_bandwidth() */
  811. spin_unlock_irqrestore(&xhci->lock, flags);
  812. return ret;
  813. }
  814. xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
  815. xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma,
  816. LAST_CTX_TO_EP_NUM(virt_dev->in_ctx->slot.dev_info));
  817. xhci_zero_in_ctx(virt_dev);
  818. /* Free any old rings */
  819. for (i = 1; i < 31; ++i) {
  820. if (virt_dev->new_ep_rings[i]) {
  821. xhci_ring_free(xhci, virt_dev->ep_rings[i]);
  822. virt_dev->ep_rings[i] = virt_dev->new_ep_rings[i];
  823. virt_dev->new_ep_rings[i] = NULL;
  824. }
  825. }
  826. spin_unlock_irqrestore(&xhci->lock, flags);
  827. return ret;
  828. }
  829. void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
  830. {
  831. unsigned long flags;
  832. struct xhci_hcd *xhci;
  833. struct xhci_virt_device *virt_dev;
  834. int i, ret;
  835. ret = xhci_check_args(hcd, udev, NULL, 0, __func__);
  836. if (ret <= 0)
  837. return;
  838. xhci = hcd_to_xhci(hcd);
  839. spin_lock_irqsave(&xhci->lock, flags);
  840. if (!xhci->devs || !xhci->devs[udev->slot_id]) {
  841. xhci_warn(xhci, "xHCI %s called with unaddressed device\n",
  842. __func__);
  843. spin_unlock_irqrestore(&xhci->lock, flags);
  844. return;
  845. }
  846. xhci_dbg(xhci, "%s called for udev %#x\n", __func__, (unsigned int) udev);
  847. virt_dev = xhci->devs[udev->slot_id];
  848. /* Free any rings allocated for added endpoints */
  849. for (i = 0; i < 31; ++i) {
  850. if (virt_dev->new_ep_rings[i]) {
  851. xhci_ring_free(xhci, virt_dev->new_ep_rings[i]);
  852. virt_dev->new_ep_rings[i] = NULL;
  853. }
  854. }
  855. xhci_zero_in_ctx(virt_dev);
  856. spin_unlock_irqrestore(&xhci->lock, flags);
  857. }
  858. /*
  859. * At this point, the struct usb_device is about to go away, the device has
  860. * disconnected, and all traffic has been stopped and the endpoints have been
  861. * disabled. Free any HC data structures associated with that device.
  862. */
  863. void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
  864. {
  865. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  866. unsigned long flags;
  867. if (udev->slot_id == 0)
  868. return;
  869. spin_lock_irqsave(&xhci->lock, flags);
  870. if (queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
  871. spin_unlock_irqrestore(&xhci->lock, flags);
  872. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  873. return;
  874. }
  875. ring_cmd_db(xhci);
  876. spin_unlock_irqrestore(&xhci->lock, flags);
  877. /*
  878. * Event command completion handler will free any data structures
  879. * associated with the slot
  880. */
  881. }
  882. /*
  883. * Returns 0 if the xHC ran out of device slots, the Enable Slot command
  884. * timed out, or allocating memory failed. Returns 1 on success.
  885. */
  886. int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
  887. {
  888. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  889. unsigned long flags;
  890. int timeleft;
  891. int ret;
  892. spin_lock_irqsave(&xhci->lock, flags);
  893. ret = queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
  894. if (ret) {
  895. spin_unlock_irqrestore(&xhci->lock, flags);
  896. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  897. return 0;
  898. }
  899. ring_cmd_db(xhci);
  900. spin_unlock_irqrestore(&xhci->lock, flags);
  901. /* XXX: how much time for xHC slot assignment? */
  902. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  903. USB_CTRL_SET_TIMEOUT);
  904. if (timeleft <= 0) {
  905. xhci_warn(xhci, "%s while waiting for a slot\n",
  906. timeleft == 0 ? "Timeout" : "Signal");
  907. /* FIXME cancel the enable slot request */
  908. return 0;
  909. }
  910. spin_lock_irqsave(&xhci->lock, flags);
  911. if (!xhci->slot_id) {
  912. xhci_err(xhci, "Error while assigning device slot ID\n");
  913. spin_unlock_irqrestore(&xhci->lock, flags);
  914. return 0;
  915. }
  916. if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_KERNEL)) {
  917. /* Disable slot, if we can do it without mem alloc */
  918. xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
  919. if (!queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
  920. ring_cmd_db(xhci);
  921. spin_unlock_irqrestore(&xhci->lock, flags);
  922. return 0;
  923. }
  924. udev->slot_id = xhci->slot_id;
  925. /* Is this a LS or FS device under a HS hub? */
  926. /* Hub or peripherial? */
  927. spin_unlock_irqrestore(&xhci->lock, flags);
  928. return 1;
  929. }
  930. /*
  931. * Issue an Address Device command (which will issue a SetAddress request to
  932. * the device).
  933. * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
  934. * we should only issue and wait on one address command at the same time.
  935. *
  936. * We add one to the device address issued by the hardware because the USB core
  937. * uses address 1 for the root hubs (even though they're not really devices).
  938. */
  939. int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
  940. {
  941. unsigned long flags;
  942. int timeleft;
  943. struct xhci_virt_device *virt_dev;
  944. int ret = 0;
  945. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  946. u32 temp;
  947. if (!udev->slot_id) {
  948. xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
  949. return -EINVAL;
  950. }
  951. spin_lock_irqsave(&xhci->lock, flags);
  952. virt_dev = xhci->devs[udev->slot_id];
  953. /* If this is a Set Address to an unconfigured device, setup ep 0 */
  954. if (!udev->config)
  955. xhci_setup_addressable_virt_dev(xhci, udev);
  956. /* Otherwise, assume the core has the device configured how it wants */
  957. ret = queue_address_device(xhci, virt_dev->in_ctx_dma, udev->slot_id);
  958. if (ret) {
  959. spin_unlock_irqrestore(&xhci->lock, flags);
  960. xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
  961. return ret;
  962. }
  963. ring_cmd_db(xhci);
  964. spin_unlock_irqrestore(&xhci->lock, flags);
  965. /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
  966. timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
  967. USB_CTRL_SET_TIMEOUT);
  968. /* FIXME: From section 4.3.4: "Software shall be responsible for timing
  969. * the SetAddress() "recovery interval" required by USB and aborting the
  970. * command on a timeout.
  971. */
  972. if (timeleft <= 0) {
  973. xhci_warn(xhci, "%s while waiting for a slot\n",
  974. timeleft == 0 ? "Timeout" : "Signal");
  975. /* FIXME cancel the address device command */
  976. return -ETIME;
  977. }
  978. spin_lock_irqsave(&xhci->lock, flags);
  979. switch (virt_dev->cmd_status) {
  980. case COMP_CTX_STATE:
  981. case COMP_EBADSLT:
  982. xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
  983. udev->slot_id);
  984. ret = -EINVAL;
  985. break;
  986. case COMP_TX_ERR:
  987. dev_warn(&udev->dev, "Device not responding to set address.\n");
  988. ret = -EPROTO;
  989. break;
  990. case COMP_SUCCESS:
  991. xhci_dbg(xhci, "Successful Address Device command\n");
  992. break;
  993. default:
  994. xhci_err(xhci, "ERROR: unexpected command completion "
  995. "code 0x%x.\n", virt_dev->cmd_status);
  996. ret = -EINVAL;
  997. break;
  998. }
  999. if (ret) {
  1000. spin_unlock_irqrestore(&xhci->lock, flags);
  1001. return ret;
  1002. }
  1003. temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[0]);
  1004. xhci_dbg(xhci, "Op regs DCBAA ptr[0] = %#08x\n", temp);
  1005. temp = xhci_readl(xhci, &xhci->op_regs->dcbaa_ptr[1]);
  1006. xhci_dbg(xhci, "Op regs DCBAA ptr[1] = %#08x\n", temp);
  1007. xhci_dbg(xhci, "Slot ID %d dcbaa entry[0] @%08x = %#08x\n",
  1008. udev->slot_id,
  1009. (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id],
  1010. xhci->dcbaa->dev_context_ptrs[2*udev->slot_id]);
  1011. xhci_dbg(xhci, "Slot ID %d dcbaa entry[1] @%08x = %#08x\n",
  1012. udev->slot_id,
  1013. (unsigned int) &xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1],
  1014. xhci->dcbaa->dev_context_ptrs[2*udev->slot_id+1]);
  1015. xhci_dbg(xhci, "Output Context DMA address = %#08x\n",
  1016. virt_dev->out_ctx_dma);
  1017. xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
  1018. xhci_dbg_ctx(xhci, virt_dev->in_ctx, virt_dev->in_ctx_dma, 2);
  1019. xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
  1020. xhci_dbg_ctx(xhci, virt_dev->out_ctx, virt_dev->out_ctx_dma, 2);
  1021. /*
  1022. * USB core uses address 1 for the roothubs, so we add one to the
  1023. * address given back to us by the HC.
  1024. */
  1025. udev->devnum = (virt_dev->out_ctx->slot.dev_state & DEV_ADDR_MASK) + 1;
  1026. /* Zero the input context control for later use */
  1027. virt_dev->in_ctx->add_flags = 0;
  1028. virt_dev->in_ctx->drop_flags = 0;
  1029. /* Mirror flags in the output context for future ep enable/disable */
  1030. virt_dev->out_ctx->add_flags = SLOT_FLAG | EP0_FLAG;
  1031. virt_dev->out_ctx->drop_flags = 0;
  1032. spin_unlock_irqrestore(&xhci->lock, flags);
  1033. xhci_dbg(xhci, "Device address = %d\n", udev->devnum);
  1034. /* XXX Meh, not sure if anyone else but choose_address uses this. */
  1035. set_bit(udev->devnum, udev->bus->devmap.devicemap);
  1036. return 0;
  1037. }
  1038. int xhci_get_frame(struct usb_hcd *hcd)
  1039. {
  1040. struct xhci_hcd *xhci = hcd_to_xhci(hcd);
  1041. /* EHCI mods by the periodic size. Why? */
  1042. return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
  1043. }
  1044. MODULE_DESCRIPTION(DRIVER_DESC);
  1045. MODULE_AUTHOR(DRIVER_AUTHOR);
  1046. MODULE_LICENSE("GPL");
  1047. static int __init xhci_hcd_init(void)
  1048. {
  1049. #ifdef CONFIG_PCI
  1050. int retval = 0;
  1051. retval = xhci_register_pci();
  1052. if (retval < 0) {
  1053. printk(KERN_DEBUG "Problem registering PCI driver.");
  1054. return retval;
  1055. }
  1056. #endif
  1057. return 0;
  1058. }
  1059. module_init(xhci_hcd_init);
  1060. static void __exit xhci_hcd_cleanup(void)
  1061. {
  1062. #ifdef CONFIG_PCI
  1063. xhci_unregister_pci();
  1064. #endif
  1065. }
  1066. module_exit(xhci_hcd_cleanup);