xhci-hcd.c 51 KB

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