xhci-hcd.c 50 KB

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