ehci-hcd.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Enhanced Host Controller Interface (EHCI) driver for USB.
  4. *
  5. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  6. *
  7. * Copyright (c) 2000-2004 by David Brownell
  8. */
  9. #include <linux/module.h>
  10. #include <linux/pci.h>
  11. #include <linux/dmapool.h>
  12. #include <linux/kernel.h>
  13. #include <linux/delay.h>
  14. #include <linux/ioport.h>
  15. #include <linux/sched.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/list.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/hcd.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/slab.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/io.h>
  30. #include <asm/irq.h>
  31. #include <asm/unaligned.h>
  32. #if defined(CONFIG_PPC_PS3)
  33. #include <asm/firmware.h>
  34. #endif
  35. /*-------------------------------------------------------------------------*/
  36. /*
  37. * EHCI hc_driver implementation ... experimental, incomplete.
  38. * Based on the final 1.0 register interface specification.
  39. *
  40. * USB 2.0 shows up in upcoming www.pcmcia.org technology.
  41. * First was PCMCIA, like ISA; then CardBus, which is PCI.
  42. * Next comes "CardBay", using USB 2.0 signals.
  43. *
  44. * Contains additional contributions by Brad Hards, Rory Bolt, and others.
  45. * Special thanks to Intel and VIA for providing host controllers to
  46. * test this driver on, and Cypress (including In-System Design) for
  47. * providing early devices for those host controllers to talk to!
  48. */
  49. #define DRIVER_AUTHOR "David Brownell"
  50. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  51. static const char hcd_name [] = "ehci_hcd";
  52. #undef EHCI_URB_TRACE
  53. /* magic numbers that can affect system performance */
  54. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  55. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  56. #define EHCI_TUNE_RL_TT 0
  57. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  58. #define EHCI_TUNE_MULT_TT 1
  59. /*
  60. * Some drivers think it's safe to schedule isochronous transfers more than
  61. * 256 ms into the future (partly as a result of an old bug in the scheduling
  62. * code). In an attempt to avoid trouble, we will use a minimum scheduling
  63. * length of 512 frames instead of 256.
  64. */
  65. #define EHCI_TUNE_FLS 1 /* (medium) 512-frame schedule */
  66. /* Initial IRQ latency: faster than hw default */
  67. static int log2_irq_thresh = 0; // 0 to 6
  68. module_param (log2_irq_thresh, int, S_IRUGO);
  69. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  70. /* initial park setting: slower than hw default */
  71. static unsigned park = 0;
  72. module_param (park, uint, S_IRUGO);
  73. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  74. /* for flakey hardware, ignore overcurrent indicators */
  75. static bool ignore_oc;
  76. module_param (ignore_oc, bool, S_IRUGO);
  77. MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
  78. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  79. /*-------------------------------------------------------------------------*/
  80. #include "ehci.h"
  81. #include "pci-quirks.h"
  82. static void compute_tt_budget(u8 budget_table[EHCI_BANDWIDTH_SIZE],
  83. struct ehci_tt *tt);
  84. /*
  85. * The MosChip MCS9990 controller updates its microframe counter
  86. * a little before the frame counter, and occasionally we will read
  87. * the invalid intermediate value. Avoid problems by checking the
  88. * microframe number (the low-order 3 bits); if they are 0 then
  89. * re-read the register to get the correct value.
  90. */
  91. static unsigned ehci_moschip_read_frame_index(struct ehci_hcd *ehci)
  92. {
  93. unsigned uf;
  94. uf = ehci_readl(ehci, &ehci->regs->frame_index);
  95. if (unlikely((uf & 7) == 0))
  96. uf = ehci_readl(ehci, &ehci->regs->frame_index);
  97. return uf;
  98. }
  99. static inline unsigned ehci_read_frame_index(struct ehci_hcd *ehci)
  100. {
  101. if (ehci->frame_index_bug)
  102. return ehci_moschip_read_frame_index(ehci);
  103. return ehci_readl(ehci, &ehci->regs->frame_index);
  104. }
  105. #include "ehci-dbg.c"
  106. /*-------------------------------------------------------------------------*/
  107. /*
  108. * ehci_handshake - spin reading hc until handshake completes or fails
  109. * @ptr: address of hc register to be read
  110. * @mask: bits to look at in result of read
  111. * @done: value of those bits when handshake succeeds
  112. * @usec: timeout in microseconds
  113. *
  114. * Returns negative errno, or zero on success
  115. *
  116. * Success happens when the "mask" bits have the specified value (hardware
  117. * handshake done). There are two failure modes: "usec" have passed (major
  118. * hardware flakeout), or the register reads as all-ones (hardware removed).
  119. *
  120. * That last failure should_only happen in cases like physical cardbus eject
  121. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  122. * bridge shutdown: shutting down the bridge before the devices using it.
  123. */
  124. int ehci_handshake(struct ehci_hcd *ehci, void __iomem *ptr,
  125. u32 mask, u32 done, int usec)
  126. {
  127. u32 result;
  128. do {
  129. result = ehci_readl(ehci, ptr);
  130. if (result == ~(u32)0) /* card removed */
  131. return -ENODEV;
  132. result &= mask;
  133. if (result == done)
  134. return 0;
  135. udelay (1);
  136. usec--;
  137. } while (usec > 0);
  138. return -ETIMEDOUT;
  139. }
  140. EXPORT_SYMBOL_GPL(ehci_handshake);
  141. /* check TDI/ARC silicon is in host mode */
  142. static int tdi_in_host_mode (struct ehci_hcd *ehci)
  143. {
  144. u32 tmp;
  145. tmp = ehci_readl(ehci, &ehci->regs->usbmode);
  146. return (tmp & 3) == USBMODE_CM_HC;
  147. }
  148. /*
  149. * Force HC to halt state from unknown (EHCI spec section 2.3).
  150. * Must be called with interrupts enabled and the lock not held.
  151. */
  152. static int ehci_halt (struct ehci_hcd *ehci)
  153. {
  154. u32 temp;
  155. spin_lock_irq(&ehci->lock);
  156. /* disable any irqs left enabled by previous code */
  157. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  158. if (ehci_is_TDI(ehci) && !tdi_in_host_mode(ehci)) {
  159. spin_unlock_irq(&ehci->lock);
  160. return 0;
  161. }
  162. /*
  163. * This routine gets called during probe before ehci->command
  164. * has been initialized, so we can't rely on its value.
  165. */
  166. ehci->command &= ~CMD_RUN;
  167. temp = ehci_readl(ehci, &ehci->regs->command);
  168. temp &= ~(CMD_RUN | CMD_IAAD);
  169. ehci_writel(ehci, temp, &ehci->regs->command);
  170. spin_unlock_irq(&ehci->lock);
  171. synchronize_irq(ehci_to_hcd(ehci)->irq);
  172. return ehci_handshake(ehci, &ehci->regs->status,
  173. STS_HALT, STS_HALT, 16 * 125);
  174. }
  175. /* put TDI/ARC silicon into EHCI mode */
  176. static void tdi_reset (struct ehci_hcd *ehci)
  177. {
  178. u32 tmp;
  179. tmp = ehci_readl(ehci, &ehci->regs->usbmode);
  180. tmp |= USBMODE_CM_HC;
  181. /* The default byte access to MMR space is LE after
  182. * controller reset. Set the required endian mode
  183. * for transfer buffers to match the host microprocessor
  184. */
  185. if (ehci_big_endian_mmio(ehci))
  186. tmp |= USBMODE_BE;
  187. ehci_writel(ehci, tmp, &ehci->regs->usbmode);
  188. }
  189. /*
  190. * Reset a non-running (STS_HALT == 1) controller.
  191. * Must be called with interrupts enabled and the lock not held.
  192. */
  193. int ehci_reset(struct ehci_hcd *ehci)
  194. {
  195. int retval;
  196. u32 command = ehci_readl(ehci, &ehci->regs->command);
  197. /* If the EHCI debug controller is active, special care must be
  198. * taken before and after a host controller reset */
  199. if (ehci->debug && !dbgp_reset_prep(ehci_to_hcd(ehci)))
  200. ehci->debug = NULL;
  201. command |= CMD_RESET;
  202. dbg_cmd (ehci, "reset", command);
  203. ehci_writel(ehci, command, &ehci->regs->command);
  204. ehci->rh_state = EHCI_RH_HALTED;
  205. ehci->next_statechange = jiffies;
  206. retval = ehci_handshake(ehci, &ehci->regs->command,
  207. CMD_RESET, 0, 250 * 1000);
  208. if (ehci->has_hostpc) {
  209. ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
  210. &ehci->regs->usbmode_ex);
  211. ehci_writel(ehci, TXFIFO_DEFAULT, &ehci->regs->txfill_tuning);
  212. }
  213. if (retval)
  214. return retval;
  215. if (ehci_is_TDI(ehci))
  216. tdi_reset (ehci);
  217. if (ehci->debug)
  218. dbgp_external_startup(ehci_to_hcd(ehci));
  219. ehci->port_c_suspend = ehci->suspended_ports =
  220. ehci->resuming_ports = 0;
  221. return retval;
  222. }
  223. EXPORT_SYMBOL_GPL(ehci_reset);
  224. /*
  225. * Idle the controller (turn off the schedules).
  226. * Must be called with interrupts enabled and the lock not held.
  227. */
  228. static void ehci_quiesce (struct ehci_hcd *ehci)
  229. {
  230. u32 temp;
  231. if (ehci->rh_state != EHCI_RH_RUNNING)
  232. return;
  233. /* wait for any schedule enables/disables to take effect */
  234. temp = (ehci->command << 10) & (STS_ASS | STS_PSS);
  235. ehci_handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, temp,
  236. 16 * 125);
  237. /* then disable anything that's still active */
  238. spin_lock_irq(&ehci->lock);
  239. ehci->command &= ~(CMD_ASE | CMD_PSE);
  240. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  241. spin_unlock_irq(&ehci->lock);
  242. /* hardware can take 16 microframes to turn off ... */
  243. ehci_handshake(ehci, &ehci->regs->status, STS_ASS | STS_PSS, 0,
  244. 16 * 125);
  245. }
  246. /*-------------------------------------------------------------------------*/
  247. static void end_iaa_cycle(struct ehci_hcd *ehci);
  248. static void end_unlink_async(struct ehci_hcd *ehci);
  249. static void unlink_empty_async(struct ehci_hcd *ehci);
  250. static void ehci_work(struct ehci_hcd *ehci);
  251. static void start_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
  252. static void end_unlink_intr(struct ehci_hcd *ehci, struct ehci_qh *qh);
  253. static int ehci_port_power(struct ehci_hcd *ehci, int portnum, bool enable);
  254. #include "ehci-timer.c"
  255. #include "ehci-hub.c"
  256. #include "ehci-mem.c"
  257. #include "ehci-q.c"
  258. #include "ehci-sched.c"
  259. #include "ehci-sysfs.c"
  260. /*-------------------------------------------------------------------------*/
  261. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  262. * The firmware seems to think that powering off is a wakeup event!
  263. * This routine turns off remote wakeup and everything else, on all ports.
  264. */
  265. static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
  266. {
  267. int port = HCS_N_PORTS(ehci->hcs_params);
  268. while (port--) {
  269. spin_unlock_irq(&ehci->lock);
  270. ehci_port_power(ehci, port, false);
  271. spin_lock_irq(&ehci->lock);
  272. ehci_writel(ehci, PORT_RWC_BITS,
  273. &ehci->regs->port_status[port]);
  274. }
  275. }
  276. /*
  277. * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  278. * Must be called with interrupts enabled and the lock not held.
  279. */
  280. static void ehci_silence_controller(struct ehci_hcd *ehci)
  281. {
  282. ehci_halt(ehci);
  283. spin_lock_irq(&ehci->lock);
  284. ehci->rh_state = EHCI_RH_HALTED;
  285. ehci_turn_off_all_ports(ehci);
  286. /* make BIOS/etc use companion controller during reboot */
  287. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  288. /* unblock posted writes */
  289. ehci_readl(ehci, &ehci->regs->configured_flag);
  290. spin_unlock_irq(&ehci->lock);
  291. }
  292. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  293. * This forcibly disables dma and IRQs, helping kexec and other cases
  294. * where the next system software may expect clean state.
  295. */
  296. static void ehci_shutdown(struct usb_hcd *hcd)
  297. {
  298. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  299. /**
  300. * Protect the system from crashing at system shutdown in cases where
  301. * usb host is not added yet from OTG controller driver.
  302. * As ehci_setup() not done yet, so stop accessing registers or
  303. * variables initialized in ehci_setup()
  304. */
  305. if (!ehci->sbrn)
  306. return;
  307. spin_lock_irq(&ehci->lock);
  308. ehci->shutdown = true;
  309. ehci->rh_state = EHCI_RH_STOPPING;
  310. ehci->enabled_hrtimer_events = 0;
  311. spin_unlock_irq(&ehci->lock);
  312. ehci_silence_controller(ehci);
  313. hrtimer_cancel(&ehci->hrtimer);
  314. }
  315. /*-------------------------------------------------------------------------*/
  316. /*
  317. * ehci_work is called from some interrupts, timers, and so on.
  318. * it calls driver completion functions, after dropping ehci->lock.
  319. */
  320. static void ehci_work (struct ehci_hcd *ehci)
  321. {
  322. /* another CPU may drop ehci->lock during a schedule scan while
  323. * it reports urb completions. this flag guards against bogus
  324. * attempts at re-entrant schedule scanning.
  325. */
  326. if (ehci->scanning) {
  327. ehci->need_rescan = true;
  328. return;
  329. }
  330. ehci->scanning = true;
  331. rescan:
  332. ehci->need_rescan = false;
  333. if (ehci->async_count)
  334. scan_async(ehci);
  335. if (ehci->intr_count > 0)
  336. scan_intr(ehci);
  337. if (ehci->isoc_count > 0)
  338. scan_isoc(ehci);
  339. if (ehci->need_rescan)
  340. goto rescan;
  341. ehci->scanning = false;
  342. /* the IO watchdog guards against hardware or driver bugs that
  343. * misplace IRQs, and should let us run completely without IRQs.
  344. * such lossage has been observed on both VT6202 and VT8235.
  345. */
  346. turn_on_io_watchdog(ehci);
  347. }
  348. /*
  349. * Called when the ehci_hcd module is removed.
  350. */
  351. static void ehci_stop (struct usb_hcd *hcd)
  352. {
  353. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  354. ehci_dbg (ehci, "stop\n");
  355. /* no more interrupts ... */
  356. spin_lock_irq(&ehci->lock);
  357. ehci->enabled_hrtimer_events = 0;
  358. spin_unlock_irq(&ehci->lock);
  359. ehci_quiesce(ehci);
  360. ehci_silence_controller(ehci);
  361. ehci_reset (ehci);
  362. hrtimer_cancel(&ehci->hrtimer);
  363. remove_sysfs_files(ehci);
  364. remove_debug_files (ehci);
  365. /* root hub is shut down separately (first, when possible) */
  366. spin_lock_irq (&ehci->lock);
  367. end_free_itds(ehci);
  368. spin_unlock_irq (&ehci->lock);
  369. ehci_mem_cleanup (ehci);
  370. if (ehci->amd_pll_fix == 1)
  371. usb_amd_dev_put();
  372. dbg_status (ehci, "ehci_stop completed",
  373. ehci_readl(ehci, &ehci->regs->status));
  374. }
  375. /* one-time init, only for memory state */
  376. static int ehci_init(struct usb_hcd *hcd)
  377. {
  378. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  379. u32 temp;
  380. int retval;
  381. u32 hcc_params;
  382. struct ehci_qh_hw *hw;
  383. spin_lock_init(&ehci->lock);
  384. /*
  385. * keep io watchdog by default, those good HCDs could turn off it later
  386. */
  387. ehci->need_io_watchdog = 1;
  388. hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  389. ehci->hrtimer.function = ehci_hrtimer_func;
  390. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  391. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  392. /*
  393. * by default set standard 80% (== 100 usec/uframe) max periodic
  394. * bandwidth as required by USB 2.0
  395. */
  396. ehci->uframe_periodic_max = 100;
  397. /*
  398. * hw default: 1K periodic list heads, one per frame.
  399. * periodic_size can shrink by USBCMD update if hcc_params allows.
  400. */
  401. ehci->periodic_size = DEFAULT_I_TDPS;
  402. INIT_LIST_HEAD(&ehci->async_unlink);
  403. INIT_LIST_HEAD(&ehci->async_idle);
  404. INIT_LIST_HEAD(&ehci->intr_unlink_wait);
  405. INIT_LIST_HEAD(&ehci->intr_unlink);
  406. INIT_LIST_HEAD(&ehci->intr_qh_list);
  407. INIT_LIST_HEAD(&ehci->cached_itd_list);
  408. INIT_LIST_HEAD(&ehci->cached_sitd_list);
  409. INIT_LIST_HEAD(&ehci->tt_list);
  410. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  411. /* periodic schedule size can be smaller than default */
  412. switch (EHCI_TUNE_FLS) {
  413. case 0: ehci->periodic_size = 1024; break;
  414. case 1: ehci->periodic_size = 512; break;
  415. case 2: ehci->periodic_size = 256; break;
  416. default: BUG();
  417. }
  418. }
  419. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  420. return retval;
  421. /* controllers may cache some of the periodic schedule ... */
  422. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  423. ehci->i_thresh = 0;
  424. else // N microframes cached
  425. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  426. /*
  427. * dedicate a qh for the async ring head, since we couldn't unlink
  428. * a 'real' qh without stopping the async schedule [4.8]. use it
  429. * as the 'reclamation list head' too.
  430. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  431. * from automatically advancing to the next td after short reads.
  432. */
  433. ehci->async->qh_next.qh = NULL;
  434. hw = ehci->async->hw;
  435. hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
  436. hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
  437. #if defined(CONFIG_PPC_PS3)
  438. hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
  439. #endif
  440. hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  441. hw->hw_qtd_next = EHCI_LIST_END(ehci);
  442. ehci->async->qh_state = QH_STATE_LINKED;
  443. hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
  444. /* clear interrupt enables, set irq latency */
  445. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  446. log2_irq_thresh = 0;
  447. temp = 1 << (16 + log2_irq_thresh);
  448. if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
  449. ehci->has_ppcd = 1;
  450. ehci_dbg(ehci, "enable per-port change event\n");
  451. temp |= CMD_PPCEE;
  452. }
  453. if (HCC_CANPARK(hcc_params)) {
  454. /* HW default park == 3, on hardware that supports it (like
  455. * NVidia and ALI silicon), maximizes throughput on the async
  456. * schedule by avoiding QH fetches between transfers.
  457. *
  458. * With fast usb storage devices and NForce2, "park" seems to
  459. * make problems: throughput reduction (!), data errors...
  460. */
  461. if (park) {
  462. park = min(park, (unsigned) 3);
  463. temp |= CMD_PARK;
  464. temp |= park << 8;
  465. }
  466. ehci_dbg(ehci, "park %d\n", park);
  467. }
  468. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  469. /* periodic schedule size can be smaller than default */
  470. temp &= ~(3 << 2);
  471. temp |= (EHCI_TUNE_FLS << 2);
  472. }
  473. ehci->command = temp;
  474. /* Accept arbitrarily long scatter-gather lists */
  475. if (!hcd->localmem_pool)
  476. hcd->self.sg_tablesize = ~0;
  477. /* Prepare for unlinking active QHs */
  478. ehci->old_current = ~0;
  479. return 0;
  480. }
  481. /* start HC running; it's halted, ehci_init() has been run (once) */
  482. static int ehci_run (struct usb_hcd *hcd)
  483. {
  484. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  485. u32 temp;
  486. u32 hcc_params;
  487. hcd->uses_new_polling = 1;
  488. /* EHCI spec section 4.1 */
  489. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  490. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  491. /*
  492. * hcc_params controls whether ehci->regs->segment must (!!!)
  493. * be used; it constrains QH/ITD/SITD and QTD locations.
  494. * dma_pool consistent memory always uses segment zero.
  495. * streaming mappings for I/O buffers, like pci_map_single(),
  496. * can return segments above 4GB, if the device allows.
  497. *
  498. * NOTE: the dma mask is visible through dev->dma_mask, so
  499. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  500. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  501. * host side drivers though.
  502. */
  503. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  504. if (HCC_64BIT_ADDR(hcc_params)) {
  505. ehci_writel(ehci, 0, &ehci->regs->segment);
  506. #if 0
  507. // this is deeply broken on almost all architectures
  508. if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
  509. ehci_info(ehci, "enabled 64bit DMA\n");
  510. #endif
  511. }
  512. // Philips, Intel, and maybe others need CMD_RUN before the
  513. // root hub will detect new devices (why?); NEC doesn't
  514. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  515. ehci->command |= CMD_RUN;
  516. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  517. dbg_cmd (ehci, "init", ehci->command);
  518. /*
  519. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  520. * are explicitly handed to companion controller(s), so no TT is
  521. * involved with the root hub. (Except where one is integrated,
  522. * and there's no companion controller unless maybe for USB OTG.)
  523. *
  524. * Turning on the CF flag will transfer ownership of all ports
  525. * from the companions to the EHCI controller. If any of the
  526. * companions are in the middle of a port reset at the time, it
  527. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  528. * guarantees that no resets are in progress. After we set CF,
  529. * a short delay lets the hardware catch up; new resets shouldn't
  530. * be started before the port switching actions could complete.
  531. */
  532. down_write(&ehci_cf_port_reset_rwsem);
  533. ehci->rh_state = EHCI_RH_RUNNING;
  534. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  535. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  536. msleep(5);
  537. up_write(&ehci_cf_port_reset_rwsem);
  538. ehci->last_periodic_enable = ktime_get_real();
  539. temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  540. ehci_info (ehci,
  541. "USB %x.%x started, EHCI %x.%02x%s\n",
  542. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  543. temp >> 8, temp & 0xff,
  544. ignore_oc ? ", overcurrent ignored" : "");
  545. ehci_writel(ehci, INTR_MASK,
  546. &ehci->regs->intr_enable); /* Turn On Interrupts */
  547. /* GRR this is run-once init(), being done every time the HC starts.
  548. * So long as they're part of class devices, we can't do it init()
  549. * since the class device isn't created that early.
  550. */
  551. create_debug_files(ehci);
  552. create_sysfs_files(ehci);
  553. return 0;
  554. }
  555. int ehci_setup(struct usb_hcd *hcd)
  556. {
  557. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  558. int retval;
  559. ehci->regs = (void __iomem *)ehci->caps +
  560. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  561. dbg_hcs_params(ehci, "reset");
  562. dbg_hcc_params(ehci, "reset");
  563. /* cache this readonly data; minimize chip reads */
  564. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  565. ehci->sbrn = HCD_USB2;
  566. /* data structure init */
  567. retval = ehci_init(hcd);
  568. if (retval)
  569. return retval;
  570. retval = ehci_halt(ehci);
  571. if (retval) {
  572. ehci_mem_cleanup(ehci);
  573. return retval;
  574. }
  575. ehci_reset(ehci);
  576. return 0;
  577. }
  578. EXPORT_SYMBOL_GPL(ehci_setup);
  579. /*-------------------------------------------------------------------------*/
  580. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  581. {
  582. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  583. u32 status, masked_status, pcd_status = 0, cmd;
  584. int bh;
  585. unsigned long flags;
  586. /*
  587. * For threadirqs option we use spin_lock_irqsave() variant to prevent
  588. * deadlock with ehci hrtimer callback, because hrtimer callbacks run
  589. * in interrupt context even when threadirqs is specified. We can go
  590. * back to spin_lock() variant when hrtimer callbacks become threaded.
  591. */
  592. spin_lock_irqsave(&ehci->lock, flags);
  593. status = ehci_readl(ehci, &ehci->regs->status);
  594. /* e.g. cardbus physical eject */
  595. if (status == ~(u32) 0) {
  596. ehci_dbg (ehci, "device removed\n");
  597. goto dead;
  598. }
  599. /*
  600. * We don't use STS_FLR, but some controllers don't like it to
  601. * remain on, so mask it out along with the other status bits.
  602. */
  603. masked_status = status & (INTR_MASK | STS_FLR);
  604. /* Shared IRQ? */
  605. if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
  606. spin_unlock_irqrestore(&ehci->lock, flags);
  607. return IRQ_NONE;
  608. }
  609. /* clear (just) interrupts */
  610. ehci_writel(ehci, masked_status, &ehci->regs->status);
  611. cmd = ehci_readl(ehci, &ehci->regs->command);
  612. bh = 0;
  613. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  614. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  615. if (likely ((status & STS_ERR) == 0))
  616. COUNT (ehci->stats.normal);
  617. else
  618. COUNT (ehci->stats.error);
  619. bh = 1;
  620. }
  621. /* complete the unlinking of some qh [4.15.2.3] */
  622. if (status & STS_IAA) {
  623. /* Turn off the IAA watchdog */
  624. ehci->enabled_hrtimer_events &= ~BIT(EHCI_HRTIMER_IAA_WATCHDOG);
  625. /*
  626. * Mild optimization: Allow another IAAD to reset the
  627. * hrtimer, if one occurs before the next expiration.
  628. * In theory we could always cancel the hrtimer, but
  629. * tests show that about half the time it will be reset
  630. * for some other event anyway.
  631. */
  632. if (ehci->next_hrtimer_event == EHCI_HRTIMER_IAA_WATCHDOG)
  633. ++ehci->next_hrtimer_event;
  634. /* guard against (alleged) silicon errata */
  635. if (cmd & CMD_IAAD)
  636. ehci_dbg(ehci, "IAA with IAAD still set?\n");
  637. if (ehci->iaa_in_progress)
  638. COUNT(ehci->stats.iaa);
  639. end_iaa_cycle(ehci);
  640. }
  641. /* remote wakeup [4.3.1] */
  642. if (status & STS_PCD) {
  643. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  644. u32 ppcd = ~0;
  645. /* kick root hub later */
  646. pcd_status = status;
  647. /* resume root hub? */
  648. if (ehci->rh_state == EHCI_RH_SUSPENDED)
  649. usb_hcd_resume_root_hub(hcd);
  650. /* get per-port change detect bits */
  651. if (ehci->has_ppcd)
  652. ppcd = status >> 16;
  653. while (i--) {
  654. int pstatus;
  655. /* leverage per-port change bits feature */
  656. if (!(ppcd & (1 << i)))
  657. continue;
  658. pstatus = ehci_readl(ehci,
  659. &ehci->regs->port_status[i]);
  660. if (pstatus & PORT_OWNER)
  661. continue;
  662. if (!(test_bit(i, &ehci->suspended_ports) &&
  663. ((pstatus & PORT_RESUME) ||
  664. !(pstatus & PORT_SUSPEND)) &&
  665. (pstatus & PORT_PE) &&
  666. ehci->reset_done[i] == 0))
  667. continue;
  668. /* start USB_RESUME_TIMEOUT msec resume signaling from
  669. * this port, and make hub_wq collect
  670. * PORT_STAT_C_SUSPEND to stop that signaling.
  671. */
  672. ehci->reset_done[i] = jiffies +
  673. msecs_to_jiffies(USB_RESUME_TIMEOUT);
  674. set_bit(i, &ehci->resuming_ports);
  675. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  676. usb_hcd_start_port_resume(&hcd->self, i);
  677. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  678. }
  679. }
  680. /* PCI errors [4.15.2.4] */
  681. if (unlikely ((status & STS_FATAL) != 0)) {
  682. ehci_err(ehci, "fatal error\n");
  683. dbg_cmd(ehci, "fatal", cmd);
  684. dbg_status(ehci, "fatal", status);
  685. dead:
  686. usb_hc_died(hcd);
  687. /* Don't let the controller do anything more */
  688. ehci->shutdown = true;
  689. ehci->rh_state = EHCI_RH_STOPPING;
  690. ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  691. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  692. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  693. ehci_handle_controller_death(ehci);
  694. /* Handle completions when the controller stops */
  695. bh = 0;
  696. }
  697. if (bh)
  698. ehci_work (ehci);
  699. spin_unlock_irqrestore(&ehci->lock, flags);
  700. if (pcd_status)
  701. usb_hcd_poll_rh_status(hcd);
  702. return IRQ_HANDLED;
  703. }
  704. /*-------------------------------------------------------------------------*/
  705. /*
  706. * non-error returns are a promise to giveback() the urb later
  707. * we drop ownership so next owner (or urb unlink) can get it
  708. *
  709. * urb + dev is in hcd.self.controller.urb_list
  710. * we're queueing TDs onto software and hardware lists
  711. *
  712. * hcd-specific init for hcpriv hasn't been done yet
  713. *
  714. * NOTE: control, bulk, and interrupt share the same code to append TDs
  715. * to a (possibly active) QH, and the same QH scanning code.
  716. */
  717. static int ehci_urb_enqueue (
  718. struct usb_hcd *hcd,
  719. struct urb *urb,
  720. gfp_t mem_flags
  721. ) {
  722. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  723. struct list_head qtd_list;
  724. INIT_LIST_HEAD (&qtd_list);
  725. switch (usb_pipetype (urb->pipe)) {
  726. case PIPE_CONTROL:
  727. /* qh_completions() code doesn't handle all the fault cases
  728. * in multi-TD control transfers. Even 1KB is rare anyway.
  729. */
  730. if (urb->transfer_buffer_length > (16 * 1024))
  731. return -EMSGSIZE;
  732. /* FALLTHROUGH */
  733. /* case PIPE_BULK: */
  734. default:
  735. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  736. return -ENOMEM;
  737. return submit_async(ehci, urb, &qtd_list, mem_flags);
  738. case PIPE_INTERRUPT:
  739. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  740. return -ENOMEM;
  741. return intr_submit(ehci, urb, &qtd_list, mem_flags);
  742. case PIPE_ISOCHRONOUS:
  743. if (urb->dev->speed == USB_SPEED_HIGH)
  744. return itd_submit (ehci, urb, mem_flags);
  745. else
  746. return sitd_submit (ehci, urb, mem_flags);
  747. }
  748. }
  749. /* remove from hardware lists
  750. * completions normally happen asynchronously
  751. */
  752. static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  753. {
  754. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  755. struct ehci_qh *qh;
  756. unsigned long flags;
  757. int rc;
  758. spin_lock_irqsave (&ehci->lock, flags);
  759. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  760. if (rc)
  761. goto done;
  762. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  763. /*
  764. * We don't expedite dequeue for isochronous URBs.
  765. * Just wait until they complete normally or their
  766. * time slot expires.
  767. */
  768. } else {
  769. qh = (struct ehci_qh *) urb->hcpriv;
  770. qh->unlink_reason |= QH_UNLINK_REQUESTED;
  771. switch (qh->qh_state) {
  772. case QH_STATE_LINKED:
  773. if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)
  774. start_unlink_intr(ehci, qh);
  775. else
  776. start_unlink_async(ehci, qh);
  777. break;
  778. case QH_STATE_COMPLETING:
  779. qh->dequeue_during_giveback = 1;
  780. break;
  781. case QH_STATE_UNLINK:
  782. case QH_STATE_UNLINK_WAIT:
  783. /* already started */
  784. break;
  785. case QH_STATE_IDLE:
  786. /* QH might be waiting for a Clear-TT-Buffer */
  787. qh_completions(ehci, qh);
  788. break;
  789. }
  790. }
  791. done:
  792. spin_unlock_irqrestore (&ehci->lock, flags);
  793. return rc;
  794. }
  795. /*-------------------------------------------------------------------------*/
  796. // bulk qh holds the data toggle
  797. static void
  798. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  799. {
  800. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  801. unsigned long flags;
  802. struct ehci_qh *qh;
  803. /* ASSERT: any requests/urbs are being unlinked */
  804. /* ASSERT: nobody can be submitting urbs for this any more */
  805. rescan:
  806. spin_lock_irqsave (&ehci->lock, flags);
  807. qh = ep->hcpriv;
  808. if (!qh)
  809. goto done;
  810. /* endpoints can be iso streams. for now, we don't
  811. * accelerate iso completions ... so spin a while.
  812. */
  813. if (qh->hw == NULL) {
  814. struct ehci_iso_stream *stream = ep->hcpriv;
  815. if (!list_empty(&stream->td_list))
  816. goto idle_timeout;
  817. /* BUG_ON(!list_empty(&stream->free_list)); */
  818. reserve_release_iso_bandwidth(ehci, stream, -1);
  819. kfree(stream);
  820. goto done;
  821. }
  822. qh->unlink_reason |= QH_UNLINK_REQUESTED;
  823. switch (qh->qh_state) {
  824. case QH_STATE_LINKED:
  825. if (list_empty(&qh->qtd_list))
  826. qh->unlink_reason |= QH_UNLINK_QUEUE_EMPTY;
  827. else
  828. WARN_ON(1);
  829. if (usb_endpoint_type(&ep->desc) != USB_ENDPOINT_XFER_INT)
  830. start_unlink_async(ehci, qh);
  831. else
  832. start_unlink_intr(ehci, qh);
  833. /* FALL THROUGH */
  834. case QH_STATE_COMPLETING: /* already in unlinking */
  835. case QH_STATE_UNLINK: /* wait for hw to finish? */
  836. case QH_STATE_UNLINK_WAIT:
  837. idle_timeout:
  838. spin_unlock_irqrestore (&ehci->lock, flags);
  839. schedule_timeout_uninterruptible(1);
  840. goto rescan;
  841. case QH_STATE_IDLE: /* fully unlinked */
  842. if (qh->clearing_tt)
  843. goto idle_timeout;
  844. if (list_empty (&qh->qtd_list)) {
  845. if (qh->ps.bw_uperiod)
  846. reserve_release_intr_bandwidth(ehci, qh, -1);
  847. qh_destroy(ehci, qh);
  848. break;
  849. }
  850. /* fall through */
  851. default:
  852. /* caller was supposed to have unlinked any requests;
  853. * that's not our job. just leak this memory.
  854. */
  855. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  856. qh, ep->desc.bEndpointAddress, qh->qh_state,
  857. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  858. break;
  859. }
  860. done:
  861. ep->hcpriv = NULL;
  862. spin_unlock_irqrestore (&ehci->lock, flags);
  863. }
  864. static void
  865. ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  866. {
  867. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  868. struct ehci_qh *qh;
  869. int eptype = usb_endpoint_type(&ep->desc);
  870. int epnum = usb_endpoint_num(&ep->desc);
  871. int is_out = usb_endpoint_dir_out(&ep->desc);
  872. unsigned long flags;
  873. if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  874. return;
  875. spin_lock_irqsave(&ehci->lock, flags);
  876. qh = ep->hcpriv;
  877. /* For Bulk and Interrupt endpoints we maintain the toggle state
  878. * in the hardware; the toggle bits in udev aren't used at all.
  879. * When an endpoint is reset by usb_clear_halt() we must reset
  880. * the toggle bit in the QH.
  881. */
  882. if (qh) {
  883. if (!list_empty(&qh->qtd_list)) {
  884. WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  885. } else {
  886. /* The toggle value in the QH can't be updated
  887. * while the QH is active. Unlink it now;
  888. * re-linking will call qh_refresh().
  889. */
  890. usb_settoggle(qh->ps.udev, epnum, is_out, 0);
  891. qh->unlink_reason |= QH_UNLINK_REQUESTED;
  892. if (eptype == USB_ENDPOINT_XFER_BULK)
  893. start_unlink_async(ehci, qh);
  894. else
  895. start_unlink_intr(ehci, qh);
  896. }
  897. }
  898. spin_unlock_irqrestore(&ehci->lock, flags);
  899. }
  900. static int ehci_get_frame (struct usb_hcd *hcd)
  901. {
  902. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  903. return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
  904. }
  905. /*-------------------------------------------------------------------------*/
  906. /* Device addition and removal */
  907. static void ehci_remove_device(struct usb_hcd *hcd, struct usb_device *udev)
  908. {
  909. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  910. spin_lock_irq(&ehci->lock);
  911. drop_tt(udev);
  912. spin_unlock_irq(&ehci->lock);
  913. }
  914. /*-------------------------------------------------------------------------*/
  915. #ifdef CONFIG_PM
  916. /* suspend/resume, section 4.3 */
  917. /* These routines handle the generic parts of controller suspend/resume */
  918. int ehci_suspend(struct usb_hcd *hcd, bool do_wakeup)
  919. {
  920. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  921. if (time_before(jiffies, ehci->next_statechange))
  922. msleep(10);
  923. /*
  924. * Root hub was already suspended. Disable IRQ emission and
  925. * mark HW unaccessible. The PM and USB cores make sure that
  926. * the root hub is either suspended or stopped.
  927. */
  928. ehci_prepare_ports_for_controller_suspend(ehci, do_wakeup);
  929. spin_lock_irq(&ehci->lock);
  930. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  931. (void) ehci_readl(ehci, &ehci->regs->intr_enable);
  932. clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  933. spin_unlock_irq(&ehci->lock);
  934. synchronize_irq(hcd->irq);
  935. /* Check for race with a wakeup request */
  936. if (do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
  937. ehci_resume(hcd, false);
  938. return -EBUSY;
  939. }
  940. return 0;
  941. }
  942. EXPORT_SYMBOL_GPL(ehci_suspend);
  943. /* Returns 0 if power was preserved, 1 if power was lost */
  944. int ehci_resume(struct usb_hcd *hcd, bool force_reset)
  945. {
  946. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  947. if (time_before(jiffies, ehci->next_statechange))
  948. msleep(100);
  949. /* Mark hardware accessible again as we are back to full power by now */
  950. set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
  951. if (ehci->shutdown)
  952. return 0; /* Controller is dead */
  953. /*
  954. * If CF is still set and reset isn't forced
  955. * then we maintained suspend power.
  956. * Just undo the effect of ehci_suspend().
  957. */
  958. if (ehci_readl(ehci, &ehci->regs->configured_flag) == FLAG_CF &&
  959. !force_reset) {
  960. int mask = INTR_MASK;
  961. ehci_prepare_ports_for_controller_resume(ehci);
  962. spin_lock_irq(&ehci->lock);
  963. if (ehci->shutdown)
  964. goto skip;
  965. if (!hcd->self.root_hub->do_remote_wakeup)
  966. mask &= ~STS_PCD;
  967. ehci_writel(ehci, mask, &ehci->regs->intr_enable);
  968. ehci_readl(ehci, &ehci->regs->intr_enable);
  969. skip:
  970. spin_unlock_irq(&ehci->lock);
  971. return 0;
  972. }
  973. /*
  974. * Else reset, to cope with power loss or resume from hibernation
  975. * having let the firmware kick in during reboot.
  976. */
  977. usb_root_hub_lost_power(hcd->self.root_hub);
  978. (void) ehci_halt(ehci);
  979. (void) ehci_reset(ehci);
  980. spin_lock_irq(&ehci->lock);
  981. if (ehci->shutdown)
  982. goto skip;
  983. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  984. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  985. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  986. ehci->rh_state = EHCI_RH_SUSPENDED;
  987. spin_unlock_irq(&ehci->lock);
  988. return 1;
  989. }
  990. EXPORT_SYMBOL_GPL(ehci_resume);
  991. #endif
  992. /*-------------------------------------------------------------------------*/
  993. /*
  994. * Generic structure: This gets copied for platform drivers so that
  995. * individual entries can be overridden as needed.
  996. */
  997. static const struct hc_driver ehci_hc_driver = {
  998. .description = hcd_name,
  999. .product_desc = "EHCI Host Controller",
  1000. .hcd_priv_size = sizeof(struct ehci_hcd),
  1001. /*
  1002. * generic hardware linkage
  1003. */
  1004. .irq = ehci_irq,
  1005. .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
  1006. /*
  1007. * basic lifecycle operations
  1008. */
  1009. .reset = ehci_setup,
  1010. .start = ehci_run,
  1011. .stop = ehci_stop,
  1012. .shutdown = ehci_shutdown,
  1013. /*
  1014. * managing i/o requests and associated device resources
  1015. */
  1016. .urb_enqueue = ehci_urb_enqueue,
  1017. .urb_dequeue = ehci_urb_dequeue,
  1018. .endpoint_disable = ehci_endpoint_disable,
  1019. .endpoint_reset = ehci_endpoint_reset,
  1020. .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
  1021. /*
  1022. * scheduling support
  1023. */
  1024. .get_frame_number = ehci_get_frame,
  1025. /*
  1026. * root hub support
  1027. */
  1028. .hub_status_data = ehci_hub_status_data,
  1029. .hub_control = ehci_hub_control,
  1030. .bus_suspend = ehci_bus_suspend,
  1031. .bus_resume = ehci_bus_resume,
  1032. .relinquish_port = ehci_relinquish_port,
  1033. .port_handed_over = ehci_port_handed_over,
  1034. .get_resuming_ports = ehci_get_resuming_ports,
  1035. /*
  1036. * device support
  1037. */
  1038. .free_dev = ehci_remove_device,
  1039. };
  1040. void ehci_init_driver(struct hc_driver *drv,
  1041. const struct ehci_driver_overrides *over)
  1042. {
  1043. /* Copy the generic table to drv and then apply the overrides */
  1044. *drv = ehci_hc_driver;
  1045. if (over) {
  1046. drv->hcd_priv_size += over->extra_priv_size;
  1047. if (over->reset)
  1048. drv->reset = over->reset;
  1049. if (over->port_power)
  1050. drv->port_power = over->port_power;
  1051. }
  1052. }
  1053. EXPORT_SYMBOL_GPL(ehci_init_driver);
  1054. /*-------------------------------------------------------------------------*/
  1055. MODULE_DESCRIPTION(DRIVER_DESC);
  1056. MODULE_AUTHOR (DRIVER_AUTHOR);
  1057. MODULE_LICENSE ("GPL");
  1058. #ifdef CONFIG_USB_EHCI_SH
  1059. #include "ehci-sh.c"
  1060. #define PLATFORM_DRIVER ehci_hcd_sh_driver
  1061. #endif
  1062. #ifdef CONFIG_PPC_PS3
  1063. #include "ehci-ps3.c"
  1064. #define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
  1065. #endif
  1066. #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
  1067. #include "ehci-ppc-of.c"
  1068. #define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
  1069. #endif
  1070. #ifdef CONFIG_XPS_USB_HCD_XILINX
  1071. #include "ehci-xilinx-of.c"
  1072. #define XILINX_OF_PLATFORM_DRIVER ehci_hcd_xilinx_of_driver
  1073. #endif
  1074. #ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
  1075. #include "ehci-pmcmsp.c"
  1076. #define PLATFORM_DRIVER ehci_hcd_msp_driver
  1077. #endif
  1078. #ifdef CONFIG_SPARC_LEON
  1079. #include "ehci-grlib.c"
  1080. #define PLATFORM_DRIVER ehci_grlib_driver
  1081. #endif
  1082. #ifdef CONFIG_USB_EHCI_MV
  1083. #include "ehci-mv.c"
  1084. #define PLATFORM_DRIVER ehci_mv_driver
  1085. #endif
  1086. static int __init ehci_hcd_init(void)
  1087. {
  1088. int retval = 0;
  1089. if (usb_disabled())
  1090. return -ENODEV;
  1091. printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
  1092. set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1093. if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  1094. test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  1095. printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
  1096. " before uhci_hcd and ohci_hcd, not after\n");
  1097. pr_debug("%s: block sizes: qh %zd qtd %zd itd %zd sitd %zd\n",
  1098. hcd_name,
  1099. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  1100. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  1101. #ifdef CONFIG_DYNAMIC_DEBUG
  1102. ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
  1103. #endif
  1104. #ifdef PLATFORM_DRIVER
  1105. retval = platform_driver_register(&PLATFORM_DRIVER);
  1106. if (retval < 0)
  1107. goto clean0;
  1108. #endif
  1109. #ifdef PS3_SYSTEM_BUS_DRIVER
  1110. retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  1111. if (retval < 0)
  1112. goto clean2;
  1113. #endif
  1114. #ifdef OF_PLATFORM_DRIVER
  1115. retval = platform_driver_register(&OF_PLATFORM_DRIVER);
  1116. if (retval < 0)
  1117. goto clean3;
  1118. #endif
  1119. #ifdef XILINX_OF_PLATFORM_DRIVER
  1120. retval = platform_driver_register(&XILINX_OF_PLATFORM_DRIVER);
  1121. if (retval < 0)
  1122. goto clean4;
  1123. #endif
  1124. return retval;
  1125. #ifdef XILINX_OF_PLATFORM_DRIVER
  1126. /* platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER); */
  1127. clean4:
  1128. #endif
  1129. #ifdef OF_PLATFORM_DRIVER
  1130. platform_driver_unregister(&OF_PLATFORM_DRIVER);
  1131. clean3:
  1132. #endif
  1133. #ifdef PS3_SYSTEM_BUS_DRIVER
  1134. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  1135. clean2:
  1136. #endif
  1137. #ifdef PLATFORM_DRIVER
  1138. platform_driver_unregister(&PLATFORM_DRIVER);
  1139. clean0:
  1140. #endif
  1141. #ifdef CONFIG_DYNAMIC_DEBUG
  1142. debugfs_remove(ehci_debug_root);
  1143. ehci_debug_root = NULL;
  1144. #endif
  1145. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1146. return retval;
  1147. }
  1148. module_init(ehci_hcd_init);
  1149. static void __exit ehci_hcd_cleanup(void)
  1150. {
  1151. #ifdef XILINX_OF_PLATFORM_DRIVER
  1152. platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER);
  1153. #endif
  1154. #ifdef OF_PLATFORM_DRIVER
  1155. platform_driver_unregister(&OF_PLATFORM_DRIVER);
  1156. #endif
  1157. #ifdef PLATFORM_DRIVER
  1158. platform_driver_unregister(&PLATFORM_DRIVER);
  1159. #endif
  1160. #ifdef PS3_SYSTEM_BUS_DRIVER
  1161. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  1162. #endif
  1163. #ifdef CONFIG_DYNAMIC_DEBUG
  1164. debugfs_remove(ehci_debug_root);
  1165. #endif
  1166. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1167. }
  1168. module_exit(ehci_hcd_cleanup);