ehci-hcd.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. /*
  2. * Enhanced Host Controller Interface (EHCI) driver for USB.
  3. *
  4. * Maintainer: Alan Stern <stern@rowland.harvard.edu>
  5. *
  6. * Copyright (c) 2000-2004 by David Brownell
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  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/module.h>
  23. #include <linux/pci.h>
  24. #include <linux/dmapool.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <linux/sched.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/errno.h>
  31. #include <linux/init.h>
  32. #include <linux/timer.h>
  33. #include <linux/ktime.h>
  34. #include <linux/list.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/usb.h>
  37. #include <linux/usb/hcd.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/debugfs.h>
  41. #include <linux/slab.h>
  42. #include <linux/uaccess.h>
  43. #include <asm/byteorder.h>
  44. #include <asm/io.h>
  45. #include <asm/irq.h>
  46. #include <asm/unaligned.h>
  47. #if defined(CONFIG_PPC_PS3)
  48. #include <asm/firmware.h>
  49. #endif
  50. /*-------------------------------------------------------------------------*/
  51. /*
  52. * EHCI hc_driver implementation ... experimental, incomplete.
  53. * Based on the final 1.0 register interface specification.
  54. *
  55. * USB 2.0 shows up in upcoming www.pcmcia.org technology.
  56. * First was PCMCIA, like ISA; then CardBus, which is PCI.
  57. * Next comes "CardBay", using USB 2.0 signals.
  58. *
  59. * Contains additional contributions by Brad Hards, Rory Bolt, and others.
  60. * Special thanks to Intel and VIA for providing host controllers to
  61. * test this driver on, and Cypress (including In-System Design) for
  62. * providing early devices for those host controllers to talk to!
  63. */
  64. #define DRIVER_AUTHOR "David Brownell"
  65. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  66. static const char hcd_name [] = "ehci_hcd";
  67. #undef VERBOSE_DEBUG
  68. #undef EHCI_URB_TRACE
  69. #ifdef DEBUG
  70. #define EHCI_STATS
  71. #endif
  72. /* magic numbers that can affect system performance */
  73. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  74. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  75. #define EHCI_TUNE_RL_TT 0
  76. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  77. #define EHCI_TUNE_MULT_TT 1
  78. /*
  79. * Some drivers think it's safe to schedule isochronous transfers more than
  80. * 256 ms into the future (partly as a result of an old bug in the scheduling
  81. * code). In an attempt to avoid trouble, we will use a minimum scheduling
  82. * length of 512 frames instead of 256.
  83. */
  84. #define EHCI_TUNE_FLS 1 /* (medium) 512-frame schedule */
  85. #define EHCI_IAA_MSECS 100 /* arbitrary */
  86. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  87. #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
  88. #define EHCI_SHRINK_JIFFIES (DIV_ROUND_UP(HZ, 200) + 1)
  89. /* 5-ms async qh unlink delay */
  90. /* Initial IRQ latency: faster than hw default */
  91. static int log2_irq_thresh = 0; // 0 to 6
  92. module_param (log2_irq_thresh, int, S_IRUGO);
  93. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  94. /* initial park setting: slower than hw default */
  95. static unsigned park = 0;
  96. module_param (park, uint, S_IRUGO);
  97. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  98. /* for flakey hardware, ignore overcurrent indicators */
  99. static bool ignore_oc = 0;
  100. module_param (ignore_oc, bool, S_IRUGO);
  101. MODULE_PARM_DESC (ignore_oc, "ignore bogus hardware overcurrent indications");
  102. /* for link power management(LPM) feature */
  103. static unsigned int hird;
  104. module_param(hird, int, S_IRUGO);
  105. MODULE_PARM_DESC(hird, "host initiated resume duration, +1 for each 75us");
  106. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  107. /*-------------------------------------------------------------------------*/
  108. #include "ehci.h"
  109. #include "ehci-dbg.c"
  110. #include "pci-quirks.h"
  111. /*-------------------------------------------------------------------------*/
  112. static void
  113. timer_action(struct ehci_hcd *ehci, enum ehci_timer_action action)
  114. {
  115. /* Don't override timeouts which shrink or (later) disable
  116. * the async ring; just the I/O watchdog. Note that if a
  117. * SHRINK were pending, OFF would never be requested.
  118. */
  119. if (timer_pending(&ehci->watchdog)
  120. && ((BIT(TIMER_ASYNC_SHRINK) | BIT(TIMER_ASYNC_OFF))
  121. & ehci->actions))
  122. return;
  123. if (!test_and_set_bit(action, &ehci->actions)) {
  124. unsigned long t;
  125. switch (action) {
  126. case TIMER_IO_WATCHDOG:
  127. if (!ehci->need_io_watchdog)
  128. return;
  129. t = EHCI_IO_JIFFIES;
  130. break;
  131. case TIMER_ASYNC_OFF:
  132. t = EHCI_ASYNC_JIFFIES;
  133. break;
  134. /* case TIMER_ASYNC_SHRINK: */
  135. default:
  136. t = EHCI_SHRINK_JIFFIES;
  137. break;
  138. }
  139. mod_timer(&ehci->watchdog, t + jiffies);
  140. }
  141. }
  142. /*-------------------------------------------------------------------------*/
  143. /*
  144. * handshake - spin reading hc until handshake completes or fails
  145. * @ptr: address of hc register to be read
  146. * @mask: bits to look at in result of read
  147. * @done: value of those bits when handshake succeeds
  148. * @usec: timeout in microseconds
  149. *
  150. * Returns negative errno, or zero on success
  151. *
  152. * Success happens when the "mask" bits have the specified value (hardware
  153. * handshake done). There are two failure modes: "usec" have passed (major
  154. * hardware flakeout), or the register reads as all-ones (hardware removed).
  155. *
  156. * That last failure should_only happen in cases like physical cardbus eject
  157. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  158. * bridge shutdown: shutting down the bridge before the devices using it.
  159. */
  160. static int handshake (struct ehci_hcd *ehci, void __iomem *ptr,
  161. u32 mask, u32 done, int usec)
  162. {
  163. u32 result;
  164. do {
  165. result = ehci_readl(ehci, ptr);
  166. if (result == ~(u32)0) /* card removed */
  167. return -ENODEV;
  168. result &= mask;
  169. if (result == done)
  170. return 0;
  171. udelay (1);
  172. usec--;
  173. } while (usec > 0);
  174. return -ETIMEDOUT;
  175. }
  176. /* check TDI/ARC silicon is in host mode */
  177. static int tdi_in_host_mode (struct ehci_hcd *ehci)
  178. {
  179. u32 __iomem *reg_ptr;
  180. u32 tmp;
  181. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + USBMODE);
  182. tmp = ehci_readl(ehci, reg_ptr);
  183. return (tmp & 3) == USBMODE_CM_HC;
  184. }
  185. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  186. static int ehci_halt (struct ehci_hcd *ehci)
  187. {
  188. u32 temp = ehci_readl(ehci, &ehci->regs->status);
  189. /* disable any irqs left enabled by previous code */
  190. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  191. if (ehci_is_TDI(ehci) && tdi_in_host_mode(ehci) == 0) {
  192. return 0;
  193. }
  194. if ((temp & STS_HALT) != 0)
  195. return 0;
  196. temp = ehci_readl(ehci, &ehci->regs->command);
  197. temp &= ~CMD_RUN;
  198. ehci_writel(ehci, temp, &ehci->regs->command);
  199. return handshake (ehci, &ehci->regs->status,
  200. STS_HALT, STS_HALT, 16 * 125);
  201. }
  202. #if defined(CONFIG_USB_SUSPEND) && defined(CONFIG_PPC_PS3)
  203. /*
  204. * The EHCI controller of the Cell Super Companion Chip used in the
  205. * PS3 will stop the root hub after all root hub ports are suspended.
  206. * When in this condition handshake will return -ETIMEDOUT. The
  207. * STS_HLT bit will not be set, so inspection of the frame index is
  208. * used here to test for the condition. If the condition is found
  209. * return success to allow the USB suspend to complete.
  210. */
  211. static int handshake_for_broken_root_hub(struct ehci_hcd *ehci,
  212. void __iomem *ptr, u32 mask, u32 done,
  213. int usec)
  214. {
  215. unsigned int old_index;
  216. int error;
  217. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  218. return -ETIMEDOUT;
  219. old_index = ehci_read_frame_index(ehci);
  220. error = handshake(ehci, ptr, mask, done, usec);
  221. if (error == -ETIMEDOUT && ehci_read_frame_index(ehci) == old_index)
  222. return 0;
  223. return error;
  224. }
  225. #else
  226. static int handshake_for_broken_root_hub(struct ehci_hcd *ehci,
  227. void __iomem *ptr, u32 mask, u32 done,
  228. int usec)
  229. {
  230. return -ETIMEDOUT;
  231. }
  232. #endif
  233. static int handshake_on_error_set_halt(struct ehci_hcd *ehci, void __iomem *ptr,
  234. u32 mask, u32 done, int usec)
  235. {
  236. int error;
  237. error = handshake(ehci, ptr, mask, done, usec);
  238. if (error == -ETIMEDOUT)
  239. error = handshake_for_broken_root_hub(ehci, ptr, mask, done,
  240. usec);
  241. if (error) {
  242. ehci_halt(ehci);
  243. ehci->rh_state = EHCI_RH_HALTED;
  244. ehci_err(ehci, "force halt; handshake %pK %08x %08x -> %d\n",
  245. ptr, mask, done, error);
  246. }
  247. return error;
  248. }
  249. /* put TDI/ARC silicon into EHCI mode */
  250. static void tdi_reset (struct ehci_hcd *ehci)
  251. {
  252. u32 __iomem *reg_ptr;
  253. u32 tmp;
  254. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + USBMODE);
  255. tmp = ehci_readl(ehci, reg_ptr);
  256. tmp |= USBMODE_CM_HC;
  257. /* The default byte access to MMR space is LE after
  258. * controller reset. Set the required endian mode
  259. * for transfer buffers to match the host microprocessor
  260. */
  261. if (ehci_big_endian_mmio(ehci))
  262. tmp |= USBMODE_BE;
  263. ehci_writel(ehci, tmp, reg_ptr);
  264. }
  265. /* reset a non-running (STS_HALT == 1) controller */
  266. static int ehci_reset (struct ehci_hcd *ehci)
  267. {
  268. int retval;
  269. u32 command = ehci_readl(ehci, &ehci->regs->command);
  270. /* If the EHCI debug controller is active, special care must be
  271. * taken before and after a host controller reset */
  272. if (ehci->debug && !dbgp_reset_prep())
  273. ehci->debug = NULL;
  274. command |= CMD_RESET;
  275. dbg_cmd (ehci, "reset", command);
  276. ehci_writel(ehci, command, &ehci->regs->command);
  277. ehci->rh_state = EHCI_RH_HALTED;
  278. ehci->next_statechange = jiffies;
  279. retval = handshake (ehci, &ehci->regs->command,
  280. CMD_RESET, 0, 250 * 1000);
  281. if (ehci->has_hostpc) {
  282. ehci_writel(ehci, USBMODE_EX_HC | USBMODE_EX_VBPS,
  283. (u32 __iomem *)(((u8 *)ehci->regs) + USBMODE_EX));
  284. ehci_writel(ehci, TXFIFO_DEFAULT,
  285. (u32 __iomem *)(((u8 *)ehci->regs) + TXFILLTUNING));
  286. }
  287. if (retval)
  288. return retval;
  289. if (ehci_is_TDI(ehci))
  290. tdi_reset (ehci);
  291. if (ehci->debug)
  292. dbgp_external_startup();
  293. ehci->port_c_suspend = ehci->suspended_ports =
  294. ehci->resuming_ports = 0;
  295. return retval;
  296. }
  297. /* idle the controller (from running) */
  298. static void ehci_quiesce (struct ehci_hcd *ehci)
  299. {
  300. u32 temp;
  301. #ifdef DEBUG
  302. if (ehci->rh_state != EHCI_RH_RUNNING)
  303. BUG ();
  304. #endif
  305. /* wait for any schedule enables/disables to take effect */
  306. temp = ehci_readl(ehci, &ehci->regs->command) << 10;
  307. temp &= STS_ASS | STS_PSS;
  308. if (handshake_on_error_set_halt(ehci, &ehci->regs->status,
  309. STS_ASS | STS_PSS, temp, 16 * 125))
  310. return;
  311. /* then disable anything that's still active */
  312. temp = ehci_readl(ehci, &ehci->regs->command);
  313. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  314. ehci_writel(ehci, temp, &ehci->regs->command);
  315. /* hardware can take 16 microframes to turn off ... */
  316. handshake_on_error_set_halt(ehci, &ehci->regs->status,
  317. STS_ASS | STS_PSS, 0, 16 * 125);
  318. }
  319. /*-------------------------------------------------------------------------*/
  320. static void end_unlink_async(struct ehci_hcd *ehci);
  321. static void ehci_work(struct ehci_hcd *ehci);
  322. #include "ehci-hub.c"
  323. #include "ehci-lpm.c"
  324. #include "ehci-mem.c"
  325. #include "ehci-q.c"
  326. #include "ehci-sched.c"
  327. #include "ehci-sysfs.c"
  328. /*-------------------------------------------------------------------------*/
  329. static void ehci_iaa_watchdog(unsigned long param)
  330. {
  331. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  332. unsigned long flags;
  333. spin_lock_irqsave (&ehci->lock, flags);
  334. /* Lost IAA irqs wedge things badly; seen first with a vt8235.
  335. * So we need this watchdog, but must protect it against both
  336. * (a) SMP races against real IAA firing and retriggering, and
  337. * (b) clean HC shutdown, when IAA watchdog was pending.
  338. */
  339. if (ehci->reclaim
  340. && !timer_pending(&ehci->iaa_watchdog)
  341. && ehci->rh_state == EHCI_RH_RUNNING) {
  342. u32 cmd, status;
  343. /* If we get here, IAA is *REALLY* late. It's barely
  344. * conceivable that the system is so busy that CMD_IAAD
  345. * is still legitimately set, so let's be sure it's
  346. * clear before we read STS_IAA. (The HC should clear
  347. * CMD_IAAD when it sets STS_IAA.)
  348. */
  349. cmd = ehci_readl(ehci, &ehci->regs->command);
  350. if (cmd & CMD_IAAD)
  351. ehci_writel(ehci, cmd & ~CMD_IAAD,
  352. &ehci->regs->command);
  353. /* If IAA is set here it either legitimately triggered
  354. * before we cleared IAAD above (but _way_ late, so we'll
  355. * still count it as lost) ... or a silicon erratum:
  356. * - VIA seems to set IAA without triggering the IRQ;
  357. * - IAAD potentially cleared without setting IAA.
  358. */
  359. status = ehci_readl(ehci, &ehci->regs->status);
  360. if ((status & STS_IAA) || !(cmd & CMD_IAAD)) {
  361. COUNT (ehci->stats.lost_iaa);
  362. ehci_writel(ehci, STS_IAA, &ehci->regs->status);
  363. }
  364. ehci_vdbg(ehci, "IAA watchdog: status %x cmd %x\n",
  365. status, cmd);
  366. end_unlink_async(ehci);
  367. }
  368. spin_unlock_irqrestore(&ehci->lock, flags);
  369. }
  370. static void ehci_watchdog(unsigned long param)
  371. {
  372. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  373. unsigned long flags;
  374. spin_lock_irqsave(&ehci->lock, flags);
  375. /* stop async processing after it's idled a bit */
  376. if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
  377. start_unlink_async (ehci, ehci->async);
  378. /* ehci could run by timer, without IRQs ... */
  379. ehci_work (ehci);
  380. spin_unlock_irqrestore (&ehci->lock, flags);
  381. }
  382. /* On some systems, leaving remote wakeup enabled prevents system shutdown.
  383. * The firmware seems to think that powering off is a wakeup event!
  384. * This routine turns off remote wakeup and everything else, on all ports.
  385. */
  386. static void ehci_turn_off_all_ports(struct ehci_hcd *ehci)
  387. {
  388. int port = HCS_N_PORTS(ehci->hcs_params);
  389. while (port--)
  390. ehci_writel(ehci, PORT_RWC_BITS,
  391. &ehci->regs->port_status[port]);
  392. }
  393. /*
  394. * Halt HC, turn off all ports, and let the BIOS use the companion controllers.
  395. * Should be called with ehci->lock held.
  396. */
  397. static void ehci_silence_controller(struct ehci_hcd *ehci)
  398. {
  399. ehci_halt(ehci);
  400. ehci_turn_off_all_ports(ehci);
  401. /* make BIOS/etc use companion controller during reboot */
  402. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  403. /* unblock posted writes */
  404. ehci_readl(ehci, &ehci->regs->configured_flag);
  405. }
  406. /* ehci_shutdown kick in for silicon on any bus (not just pci, etc).
  407. * This forcibly disables dma and IRQs, helping kexec and other cases
  408. * where the next system software may expect clean state.
  409. */
  410. static void ehci_shutdown(struct usb_hcd *hcd)
  411. {
  412. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  413. del_timer_sync(&ehci->watchdog);
  414. del_timer_sync(&ehci->iaa_watchdog);
  415. spin_lock_irq(&ehci->lock);
  416. ehci_silence_controller(ehci);
  417. spin_unlock_irq(&ehci->lock);
  418. }
  419. static void __maybe_unused ehci_port_power (struct ehci_hcd *ehci, int is_on)
  420. {
  421. unsigned port;
  422. if (!HCS_PPC (ehci->hcs_params))
  423. return;
  424. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  425. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  426. (void) ehci_hub_control(ehci_to_hcd(ehci),
  427. is_on ? SetPortFeature : ClearPortFeature,
  428. USB_PORT_FEAT_POWER,
  429. port--, NULL, 0);
  430. /* Flush those writes */
  431. ehci_readl(ehci, &ehci->regs->command);
  432. msleep(20);
  433. }
  434. /*-------------------------------------------------------------------------*/
  435. /*
  436. * ehci_work is called from some interrupts, timers, and so on.
  437. * it calls driver completion functions, after dropping ehci->lock.
  438. */
  439. static void ehci_work (struct ehci_hcd *ehci)
  440. {
  441. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  442. /* another CPU may drop ehci->lock during a schedule scan while
  443. * it reports urb completions. this flag guards against bogus
  444. * attempts at re-entrant schedule scanning.
  445. */
  446. if (ehci->scanning)
  447. return;
  448. ehci->scanning = 1;
  449. scan_async (ehci);
  450. if (ehci->next_uframe != -1)
  451. scan_periodic (ehci);
  452. ehci->scanning = 0;
  453. /* the IO watchdog guards against hardware or driver bugs that
  454. * misplace IRQs, and should let us run completely without IRQs.
  455. * such lossage has been observed on both VT6202 and VT8235.
  456. */
  457. if (ehci->rh_state == EHCI_RH_RUNNING &&
  458. (ehci->async->qh_next.ptr != NULL ||
  459. ehci->periodic_sched != 0))
  460. timer_action (ehci, TIMER_IO_WATCHDOG);
  461. }
  462. /*
  463. * Called when the ehci_hcd module is removed.
  464. */
  465. static void ehci_stop (struct usb_hcd *hcd)
  466. {
  467. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  468. ehci_dbg (ehci, "stop\n");
  469. /* no more interrupts ... */
  470. del_timer_sync (&ehci->watchdog);
  471. del_timer_sync(&ehci->iaa_watchdog);
  472. spin_lock_irq(&ehci->lock);
  473. if (ehci->rh_state == EHCI_RH_RUNNING)
  474. ehci_quiesce (ehci);
  475. ehci_silence_controller(ehci);
  476. ehci_reset (ehci);
  477. spin_unlock_irq(&ehci->lock);
  478. remove_sysfs_files(ehci);
  479. remove_debug_files (ehci);
  480. /* root hub is shut down separately (first, when possible) */
  481. spin_lock_irq (&ehci->lock);
  482. if (ehci->async) {
  483. /*
  484. * TODO: Observed that ehci->async next ptr is not
  485. * NULL sometimes which leads to crash in mem_cleanup.
  486. * Root cause is not yet known why this messup is
  487. * happenning.
  488. * The follwing workaround fixes the crash caused
  489. * by this temporarily.
  490. * check if async next ptr is not NULL and unlink
  491. * explictly.
  492. */
  493. if (ehci->async->qh_next.ptr != NULL)
  494. start_unlink_async(ehci, ehci->async->qh_next.qh);
  495. ehci_work (ehci);
  496. }
  497. spin_unlock_irq (&ehci->lock);
  498. ehci_mem_cleanup (ehci);
  499. if (ehci->amd_pll_fix == 1)
  500. usb_amd_dev_put();
  501. #ifdef EHCI_STATS
  502. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  503. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  504. ehci->stats.lost_iaa);
  505. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  506. ehci->stats.complete, ehci->stats.unlink);
  507. #endif
  508. dbg_status (ehci, "ehci_stop completed",
  509. ehci_readl(ehci, &ehci->regs->status));
  510. }
  511. /* one-time init, only for memory state */
  512. static int ehci_init(struct usb_hcd *hcd)
  513. {
  514. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  515. u32 temp;
  516. int retval;
  517. u32 hcc_params;
  518. struct ehci_qh_hw *hw;
  519. spin_lock_init(&ehci->lock);
  520. /*
  521. * keep io watchdog by default, those good HCDs could turn off it later
  522. */
  523. ehci->need_io_watchdog = 1;
  524. init_timer(&ehci->watchdog);
  525. ehci->watchdog.function = ehci_watchdog;
  526. ehci->watchdog.data = (unsigned long) ehci;
  527. init_timer(&ehci->iaa_watchdog);
  528. ehci->iaa_watchdog.function = ehci_iaa_watchdog;
  529. ehci->iaa_watchdog.data = (unsigned long) ehci;
  530. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  531. /*
  532. * by default set standard 80% (== 100 usec/uframe) max periodic
  533. * bandwidth as required by USB 2.0
  534. */
  535. ehci->uframe_periodic_max = 100;
  536. /*
  537. * hw default: 1K periodic list heads, one per frame.
  538. * periodic_size can shrink by USBCMD update if hcc_params allows.
  539. */
  540. ehci->periodic_size = DEFAULT_I_TDPS;
  541. INIT_LIST_HEAD(&ehci->cached_itd_list);
  542. INIT_LIST_HEAD(&ehci->cached_sitd_list);
  543. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  544. /* periodic schedule size can be smaller than default */
  545. switch (EHCI_TUNE_FLS) {
  546. case 0: ehci->periodic_size = 1024; break;
  547. case 1: ehci->periodic_size = 512; break;
  548. case 2: ehci->periodic_size = 256; break;
  549. default: BUG();
  550. }
  551. }
  552. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  553. return retval;
  554. /* controllers may cache some of the periodic schedule ... */
  555. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  556. ehci->i_thresh = 2 + 8;
  557. else // N microframes cached
  558. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  559. ehci->reclaim = NULL;
  560. ehci->next_uframe = -1;
  561. ehci->clock_frame = -1;
  562. /*
  563. * dedicate a qh for the async ring head, since we couldn't unlink
  564. * a 'real' qh without stopping the async schedule [4.8]. use it
  565. * as the 'reclamation list head' too.
  566. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  567. * from automatically advancing to the next td after short reads.
  568. */
  569. ehci->async->qh_next.qh = NULL;
  570. hw = ehci->async->hw;
  571. hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
  572. hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
  573. #if defined(CONFIG_PPC_PS3)
  574. hw->hw_info1 |= cpu_to_hc32(ehci, (1 << 7)); /* I = 1 */
  575. #endif
  576. hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  577. hw->hw_qtd_next = EHCI_LIST_END(ehci);
  578. ehci->async->qh_state = QH_STATE_LINKED;
  579. hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
  580. /* clear interrupt enables, set irq latency */
  581. log2_irq_thresh = ehci->log2_irq_thresh;
  582. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  583. log2_irq_thresh = 0;
  584. temp = 1 << (16 + log2_irq_thresh);
  585. if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
  586. ehci->has_ppcd = 1;
  587. ehci_dbg(ehci, "enable per-port change event\n");
  588. temp |= CMD_PPCEE;
  589. }
  590. if (HCC_CANPARK(hcc_params)) {
  591. /* HW default park == 3, on hardware that supports it (like
  592. * NVidia and ALI silicon), maximizes throughput on the async
  593. * schedule by avoiding QH fetches between transfers.
  594. *
  595. * With fast usb storage devices and NForce2, "park" seems to
  596. * make problems: throughput reduction (!), data errors...
  597. */
  598. if (park) {
  599. park = min(park, (unsigned) 3);
  600. temp |= CMD_PARK;
  601. temp |= park << 8;
  602. }
  603. ehci_dbg(ehci, "park %d\n", park);
  604. }
  605. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  606. /* periodic schedule size can be smaller than default */
  607. temp &= ~(3 << 2);
  608. temp |= (EHCI_TUNE_FLS << 2);
  609. }
  610. if (HCC_LPM(hcc_params)) {
  611. /* support link power management EHCI 1.1 addendum */
  612. ehci_dbg(ehci, "support lpm\n");
  613. ehci->has_lpm = 1;
  614. if (hird > 0xf) {
  615. ehci_dbg(ehci, "hird %d invalid, use default 0",
  616. hird);
  617. hird = 0;
  618. }
  619. temp |= hird << 24;
  620. }
  621. ehci->command = temp;
  622. /* Accept arbitrarily long scatter-gather lists */
  623. if (!(hcd->driver->flags & HCD_LOCAL_MEM))
  624. hcd->self.sg_tablesize = ~0;
  625. return 0;
  626. }
  627. /* start HC running; it's halted, ehci_init() has been run (once) */
  628. static int __maybe_unused ehci_run (struct usb_hcd *hcd)
  629. {
  630. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  631. u32 temp;
  632. u32 hcc_params;
  633. hcd->uses_new_polling = 1;
  634. /* EHCI spec section 4.1 */
  635. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  636. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  637. /*
  638. * hcc_params controls whether ehci->regs->segment must (!!!)
  639. * be used; it constrains QH/ITD/SITD and QTD locations.
  640. * pci_pool consistent memory always uses segment zero.
  641. * streaming mappings for I/O buffers, like pci_map_single(),
  642. * can return segments above 4GB, if the device allows.
  643. *
  644. * NOTE: the dma mask is visible through dma_supported(), so
  645. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  646. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  647. * host side drivers though.
  648. */
  649. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  650. if (HCC_64BIT_ADDR(hcc_params)) {
  651. ehci_writel(ehci, 0, &ehci->regs->segment);
  652. #if 0
  653. // this is deeply broken on almost all architectures
  654. if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
  655. ehci_info(ehci, "enabled 64bit DMA\n");
  656. #endif
  657. }
  658. // Philips, Intel, and maybe others need CMD_RUN before the
  659. // root hub will detect new devices (why?); NEC doesn't
  660. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  661. ehci->command |= CMD_RUN;
  662. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  663. dbg_cmd (ehci, "init", ehci->command);
  664. /*
  665. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  666. * are explicitly handed to companion controller(s), so no TT is
  667. * involved with the root hub. (Except where one is integrated,
  668. * and there's no companion controller unless maybe for USB OTG.)
  669. *
  670. * Turning on the CF flag will transfer ownership of all ports
  671. * from the companions to the EHCI controller. If any of the
  672. * companions are in the middle of a port reset at the time, it
  673. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  674. * guarantees that no resets are in progress. After we set CF,
  675. * a short delay lets the hardware catch up; new resets shouldn't
  676. * be started before the port switching actions could complete.
  677. */
  678. down_write(&ehci_cf_port_reset_rwsem);
  679. ehci->rh_state = EHCI_RH_RUNNING;
  680. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  681. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  682. msleep(5);
  683. up_write(&ehci_cf_port_reset_rwsem);
  684. ehci->last_periodic_enable = ktime_get_real();
  685. temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  686. ehci_info (ehci,
  687. "USB %x.%x started, EHCI %x.%02x%s\n",
  688. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  689. temp >> 8, temp & 0xff,
  690. ignore_oc ? ", overcurrent ignored" : "");
  691. ehci_writel(ehci, INTR_MASK,
  692. &ehci->regs->intr_enable); /* Turn On Interrupts */
  693. /* GRR this is run-once init(), being done every time the HC starts.
  694. * So long as they're part of class devices, we can't do it init()
  695. * since the class device isn't created that early.
  696. */
  697. create_debug_files(ehci);
  698. create_sysfs_files(ehci);
  699. return 0;
  700. }
  701. static int __maybe_unused ehci_setup (struct usb_hcd *hcd)
  702. {
  703. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  704. int retval;
  705. ehci->regs = (void __iomem *)ehci->caps +
  706. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  707. dbg_hcs_params(ehci, "reset");
  708. dbg_hcc_params(ehci, "reset");
  709. /* cache this readonly data; minimize chip reads */
  710. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  711. ehci->sbrn = HCD_USB2;
  712. retval = ehci_halt(ehci);
  713. if (retval)
  714. return retval;
  715. /* data structure init */
  716. retval = ehci_init(hcd);
  717. if (retval)
  718. return retval;
  719. ehci_reset(ehci);
  720. return 0;
  721. }
  722. /*-------------------------------------------------------------------------*/
  723. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  724. {
  725. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  726. u32 status, masked_status, pcd_status = 0, cmd;
  727. int bh;
  728. spin_lock (&ehci->lock);
  729. status = ehci_readl(ehci, &ehci->regs->status);
  730. /* e.g. cardbus physical eject */
  731. if (status == ~(u32) 0) {
  732. ehci_dbg (ehci, "device removed\n");
  733. goto dead;
  734. }
  735. /*
  736. * We don't use STS_FLR, but some controllers don't like it to
  737. * remain on, so mask it out along with the other status bits.
  738. */
  739. masked_status = status & (INTR_MASK | STS_FLR);
  740. /* Shared IRQ? */
  741. if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
  742. spin_unlock(&ehci->lock);
  743. return IRQ_NONE;
  744. }
  745. /* clear (just) interrupts */
  746. ehci_writel(ehci, masked_status, &ehci->regs->status);
  747. cmd = ehci_readl(ehci, &ehci->regs->command);
  748. bh = 0;
  749. #ifdef VERBOSE_DEBUG
  750. /* unrequested/ignored: Frame List Rollover */
  751. dbg_status (ehci, "irq", status);
  752. #endif
  753. /* INT, ERR, and IAA interrupt rates can be throttled */
  754. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  755. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  756. if (likely ((status & STS_ERR) == 0))
  757. COUNT (ehci->stats.normal);
  758. else
  759. COUNT (ehci->stats.error);
  760. bh = 1;
  761. }
  762. /* complete the unlinking of some qh [4.15.2.3] */
  763. if (status & STS_IAA) {
  764. /* guard against (alleged) silicon errata */
  765. if (cmd & CMD_IAAD) {
  766. ehci_writel(ehci, cmd & ~CMD_IAAD,
  767. &ehci->regs->command);
  768. ehci_dbg(ehci, "IAA with IAAD still set?\n");
  769. }
  770. if (ehci->reclaim) {
  771. COUNT(ehci->stats.reclaim);
  772. end_unlink_async(ehci);
  773. } else
  774. ehci_dbg(ehci, "IAA with nothing to reclaim?\n");
  775. }
  776. /* remote wakeup [4.3.1] */
  777. if (status & STS_PCD) {
  778. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  779. u32 ppcd = 0;
  780. /* kick root hub later */
  781. pcd_status = status;
  782. /* resume root hub? */
  783. if (ehci->rh_state == EHCI_RH_SUSPENDED)
  784. usb_hcd_resume_root_hub(hcd);
  785. /* get per-port change detect bits */
  786. if (ehci->has_ppcd)
  787. ppcd = status >> 16;
  788. while (i--) {
  789. int pstatus;
  790. /* leverage per-port change bits feature */
  791. if (ehci->has_ppcd && !(ppcd & (1 << i)))
  792. continue;
  793. pstatus = ehci_readl(ehci,
  794. &ehci->regs->port_status[i]);
  795. /*set RS bit in case of remote wakeup*/
  796. if (ehci_is_TDI(ehci) && !(cmd & CMD_RUN) &&
  797. (pstatus & PORT_SUSPEND))
  798. ehci_writel(ehci, cmd | CMD_RUN,
  799. &ehci->regs->command);
  800. if (pstatus & PORT_OWNER)
  801. continue;
  802. if (!(test_bit(i, &ehci->suspended_ports) &&
  803. ((pstatus & PORT_RESUME) ||
  804. !(pstatus & PORT_SUSPEND)) &&
  805. (pstatus & PORT_PE) &&
  806. ehci->reset_done[i] == 0))
  807. continue;
  808. /* start 20 msec resume signaling from this port,
  809. * and make khubd collect PORT_STAT_C_SUSPEND to
  810. * stop that signaling. Use 5 ms extra for safety,
  811. * like usb_port_resume() does.
  812. */
  813. ehci->reset_done[i] = jiffies + msecs_to_jiffies(25);
  814. set_bit(i, &ehci->resuming_ports);
  815. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  816. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  817. }
  818. }
  819. /* PCI errors [4.15.2.4] */
  820. if (unlikely ((status & STS_FATAL) != 0)) {
  821. ehci_err(ehci, "fatal error\n");
  822. if (hcd->driver->dump_regs) {
  823. hcd->driver->dump_regs(hcd);
  824. panic("System error\n");
  825. }
  826. dbg_cmd(ehci, "fatal", cmd);
  827. dbg_status(ehci, "fatal", status);
  828. ehci_halt(ehci);
  829. dead:
  830. ehci_reset(ehci);
  831. ehci_writel(ehci, 0, &ehci->regs->configured_flag);
  832. usb_hc_died(hcd);
  833. /* generic layer kills/unlinks all urbs, then
  834. * uses ehci_stop to clean up the rest
  835. */
  836. bh = 1;
  837. }
  838. if (bh)
  839. ehci_work (ehci);
  840. spin_unlock (&ehci->lock);
  841. if (pcd_status)
  842. usb_hcd_poll_rh_status(hcd);
  843. return IRQ_HANDLED;
  844. }
  845. /*-------------------------------------------------------------------------*/
  846. /*
  847. * non-error returns are a promise to giveback() the urb later
  848. * we drop ownership so next owner (or urb unlink) can get it
  849. *
  850. * urb + dev is in hcd.self.controller.urb_list
  851. * we're queueing TDs onto software and hardware lists
  852. *
  853. * hcd-specific init for hcpriv hasn't been done yet
  854. *
  855. * NOTE: control, bulk, and interrupt share the same code to append TDs
  856. * to a (possibly active) QH, and the same QH scanning code.
  857. */
  858. static int ehci_urb_enqueue (
  859. struct usb_hcd *hcd,
  860. struct urb *urb,
  861. gfp_t mem_flags
  862. ) {
  863. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  864. struct list_head qtd_list;
  865. INIT_LIST_HEAD (&qtd_list);
  866. switch (usb_pipetype (urb->pipe)) {
  867. case PIPE_CONTROL:
  868. /* qh_completions() code doesn't handle all the fault cases
  869. * in multi-TD control transfers. Even 1KB is rare anyway.
  870. */
  871. if (urb->transfer_buffer_length > (16 * 1024))
  872. return -EMSGSIZE;
  873. /* FALLTHROUGH */
  874. /* case PIPE_BULK: */
  875. default:
  876. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  877. return -ENOMEM;
  878. return submit_async(ehci, urb, &qtd_list, mem_flags);
  879. case PIPE_INTERRUPT:
  880. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  881. return -ENOMEM;
  882. return intr_submit(ehci, urb, &qtd_list, mem_flags);
  883. case PIPE_ISOCHRONOUS:
  884. if (urb->dev->speed == USB_SPEED_HIGH)
  885. return itd_submit (ehci, urb, mem_flags);
  886. else
  887. return sitd_submit (ehci, urb, mem_flags);
  888. }
  889. }
  890. static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  891. {
  892. /* failfast */
  893. if (ehci->rh_state != EHCI_RH_RUNNING && ehci->reclaim)
  894. end_unlink_async(ehci);
  895. /* If the QH isn't linked then there's nothing we can do
  896. * unless we were called during a giveback, in which case
  897. * qh_completions() has to deal with it.
  898. */
  899. if (qh->qh_state != QH_STATE_LINKED) {
  900. if (qh->qh_state == QH_STATE_COMPLETING)
  901. qh->needs_rescan = 1;
  902. return;
  903. }
  904. /* defer till later if busy */
  905. if (ehci->reclaim) {
  906. struct ehci_qh *last;
  907. for (last = ehci->reclaim;
  908. last->reclaim;
  909. last = last->reclaim)
  910. continue;
  911. qh->qh_state = QH_STATE_UNLINK_WAIT;
  912. last->reclaim = qh;
  913. /* start IAA cycle */
  914. } else
  915. start_unlink_async (ehci, qh);
  916. }
  917. /* remove from hardware lists
  918. * completions normally happen asynchronously
  919. */
  920. static int ehci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  921. {
  922. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  923. struct ehci_qh *qh;
  924. unsigned long flags;
  925. int rc;
  926. spin_lock_irqsave (&ehci->lock, flags);
  927. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  928. if (rc)
  929. goto done;
  930. switch (usb_pipetype (urb->pipe)) {
  931. // case PIPE_CONTROL:
  932. // case PIPE_BULK:
  933. default:
  934. qh = (struct ehci_qh *) urb->hcpriv;
  935. if (!qh)
  936. break;
  937. switch (qh->qh_state) {
  938. case QH_STATE_LINKED:
  939. case QH_STATE_COMPLETING:
  940. unlink_async(ehci, qh);
  941. break;
  942. case QH_STATE_UNLINK:
  943. case QH_STATE_UNLINK_WAIT:
  944. /* already started */
  945. break;
  946. case QH_STATE_IDLE:
  947. /* QH might be waiting for a Clear-TT-Buffer */
  948. qh_completions(ehci, qh);
  949. break;
  950. }
  951. break;
  952. case PIPE_INTERRUPT:
  953. qh = (struct ehci_qh *) urb->hcpriv;
  954. if (!qh)
  955. break;
  956. switch (qh->qh_state) {
  957. case QH_STATE_LINKED:
  958. case QH_STATE_COMPLETING:
  959. intr_deschedule (ehci, qh);
  960. break;
  961. case QH_STATE_IDLE:
  962. qh_completions (ehci, qh);
  963. break;
  964. default:
  965. ehci_dbg (ehci, "bogus qh %pK state %d\n",
  966. qh, qh->qh_state);
  967. goto done;
  968. }
  969. break;
  970. case PIPE_ISOCHRONOUS:
  971. // itd or sitd ...
  972. // wait till next completion, do it then.
  973. // completion irqs can wait up to 1024 msec,
  974. break;
  975. }
  976. done:
  977. spin_unlock_irqrestore (&ehci->lock, flags);
  978. return rc;
  979. }
  980. /*-------------------------------------------------------------------------*/
  981. // bulk qh holds the data toggle
  982. static void
  983. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  984. {
  985. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  986. unsigned long flags;
  987. struct ehci_qh *qh, *tmp;
  988. /* ASSERT: any requests/urbs are being unlinked */
  989. /* ASSERT: nobody can be submitting urbs for this any more */
  990. rescan:
  991. spin_lock_irqsave (&ehci->lock, flags);
  992. qh = ep->hcpriv;
  993. if (!qh)
  994. goto done;
  995. /* endpoints can be iso streams. for now, we don't
  996. * accelerate iso completions ... so spin a while.
  997. */
  998. if (qh->hw == NULL) {
  999. ehci_vdbg (ehci, "iso delay\n");
  1000. goto idle_timeout;
  1001. }
  1002. if (ehci->rh_state != EHCI_RH_RUNNING)
  1003. qh->qh_state = QH_STATE_IDLE;
  1004. switch (qh->qh_state) {
  1005. case QH_STATE_LINKED:
  1006. case QH_STATE_COMPLETING:
  1007. for (tmp = ehci->async->qh_next.qh;
  1008. tmp && tmp != qh;
  1009. tmp = tmp->qh_next.qh)
  1010. continue;
  1011. /* periodic qh self-unlinks on empty, and a COMPLETING qh
  1012. * may already be unlinked.
  1013. */
  1014. if (tmp)
  1015. unlink_async(ehci, qh);
  1016. /* FALL THROUGH */
  1017. case QH_STATE_UNLINK: /* wait for hw to finish? */
  1018. case QH_STATE_UNLINK_WAIT:
  1019. idle_timeout:
  1020. spin_unlock_irqrestore (&ehci->lock, flags);
  1021. schedule_timeout_uninterruptible(1);
  1022. goto rescan;
  1023. case QH_STATE_IDLE: /* fully unlinked */
  1024. if (qh->clearing_tt)
  1025. goto idle_timeout;
  1026. if (list_empty (&qh->qtd_list)) {
  1027. qh_put (qh);
  1028. break;
  1029. }
  1030. /* else FALL THROUGH */
  1031. default:
  1032. /* caller was supposed to have unlinked any requests;
  1033. * that's not our job. just leak this memory.
  1034. */
  1035. ehci_err (ehci, "qh %pK (#%02x) state %d%s\n",
  1036. qh, ep->desc.bEndpointAddress, qh->qh_state,
  1037. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  1038. break;
  1039. }
  1040. ep->hcpriv = NULL;
  1041. done:
  1042. spin_unlock_irqrestore (&ehci->lock, flags);
  1043. }
  1044. static void __maybe_unused
  1045. ehci_endpoint_reset(struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  1046. {
  1047. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  1048. struct ehci_qh *qh;
  1049. int eptype = usb_endpoint_type(&ep->desc);
  1050. int epnum = usb_endpoint_num(&ep->desc);
  1051. int is_out = usb_endpoint_dir_out(&ep->desc);
  1052. unsigned long flags;
  1053. if (eptype != USB_ENDPOINT_XFER_BULK && eptype != USB_ENDPOINT_XFER_INT)
  1054. return;
  1055. spin_lock_irqsave(&ehci->lock, flags);
  1056. qh = ep->hcpriv;
  1057. /* For Bulk and Interrupt endpoints we maintain the toggle state
  1058. * in the hardware; the toggle bits in udev aren't used at all.
  1059. * When an endpoint is reset by usb_clear_halt() we must reset
  1060. * the toggle bit in the QH.
  1061. */
  1062. if (qh) {
  1063. usb_settoggle(qh->dev, epnum, is_out, 0);
  1064. if (!list_empty(&qh->qtd_list)) {
  1065. WARN_ONCE(1, "clear_halt for a busy endpoint\n");
  1066. } else if (qh->qh_state == QH_STATE_LINKED ||
  1067. qh->qh_state == QH_STATE_COMPLETING) {
  1068. /* The toggle value in the QH can't be updated
  1069. * while the QH is active. Unlink it now;
  1070. * re-linking will call qh_refresh().
  1071. */
  1072. if (eptype == USB_ENDPOINT_XFER_BULK)
  1073. unlink_async(ehci, qh);
  1074. else
  1075. intr_deschedule(ehci, qh);
  1076. }
  1077. }
  1078. spin_unlock_irqrestore(&ehci->lock, flags);
  1079. }
  1080. static int ehci_get_frame (struct usb_hcd *hcd)
  1081. {
  1082. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  1083. return (ehci_read_frame_index(ehci) >> 3) % ehci->periodic_size;
  1084. }
  1085. /*-------------------------------------------------------------------------*/
  1086. MODULE_DESCRIPTION(DRIVER_DESC);
  1087. MODULE_AUTHOR (DRIVER_AUTHOR);
  1088. MODULE_LICENSE ("GPL");
  1089. #ifdef CONFIG_PCI
  1090. #include "ehci-pci.c"
  1091. #define PCI_DRIVER ehci_pci_driver
  1092. #endif
  1093. #ifdef CONFIG_PPC_PS3
  1094. #include "ehci-ps3.c"
  1095. #define PS3_SYSTEM_BUS_DRIVER ps3_ehci_driver
  1096. #endif
  1097. #ifdef CONFIG_USB_EHCI_HCD_PPC_OF
  1098. #include "ehci-ppc-of.c"
  1099. #define OF_PLATFORM_DRIVER ehci_hcd_ppc_of_driver
  1100. #endif
  1101. #ifdef CONFIG_XPS_USB_HCD_XILINX
  1102. #include "ehci-xilinx-of.c"
  1103. #define XILINX_OF_PLATFORM_DRIVER ehci_hcd_xilinx_of_driver
  1104. #endif
  1105. #ifdef CONFIG_USB_EHCI_FSL
  1106. #include "ehci-fsl.c"
  1107. #define PLATFORM_DRIVER_PRESENT
  1108. #endif
  1109. #ifdef CONFIG_USB_EHCI_MXC
  1110. #include "ehci-mxc.c"
  1111. #define PLATFORM_DRIVER_PRESENT
  1112. #endif
  1113. #ifdef CONFIG_USB_EHCI_SH
  1114. #include "ehci-sh.c"
  1115. #define PLATFORM_DRIVER_PRESENT
  1116. #endif
  1117. #ifdef CONFIG_MIPS_ALCHEMY
  1118. #include "ehci-au1xxx.c"
  1119. #define PLATFORM_DRIVER_PRESENT
  1120. #endif
  1121. #ifdef CONFIG_USB_EHCI_HCD_OMAP
  1122. #include "ehci-omap.c"
  1123. #define PLATFORM_DRIVER_PRESENT
  1124. #endif
  1125. #ifdef CONFIG_PLAT_ORION
  1126. #include "ehci-orion.c"
  1127. #define PLATFORM_DRIVER_PRESENT
  1128. #endif
  1129. #ifdef CONFIG_ARCH_IXP4XX
  1130. #include "ehci-ixp4xx.c"
  1131. #define PLATFORM_DRIVER_PRESENT
  1132. #endif
  1133. #ifdef CONFIG_USB_W90X900_EHCI
  1134. #include "ehci-w90x900.c"
  1135. #define PLATFORM_DRIVER_PRESENT
  1136. #endif
  1137. #ifdef CONFIG_ARCH_AT91
  1138. #include "ehci-atmel.c"
  1139. #define PLATFORM_DRIVER_PRESENT
  1140. #endif
  1141. #ifdef CONFIG_USB_OCTEON_EHCI
  1142. #include "ehci-octeon.c"
  1143. #define PLATFORM_DRIVER_PRESENT
  1144. #endif
  1145. #ifdef CONFIG_USB_CNS3XXX_EHCI
  1146. #include "ehci-cns3xxx.c"
  1147. #define PLATFORM_DRIVER_PRESENT
  1148. #endif
  1149. #ifdef CONFIG_ARCH_VT8500
  1150. #include "ehci-vt8500.c"
  1151. #define PLATFORM_DRIVER_PRESENT
  1152. #endif
  1153. #ifdef CONFIG_PLAT_SPEAR
  1154. #include "ehci-spear.c"
  1155. #define PLATFORM_DRIVER_PRESENT
  1156. #endif
  1157. #ifdef CONFIG_USB_EHCI_MSM_72K
  1158. #include "ehci-msm72k.c"
  1159. #define PLATFORM_DRIVER_PRESENT
  1160. #endif
  1161. #ifdef CONFIG_USB_EHCI_MSM
  1162. #include "ehci-msm.c"
  1163. #include "ehci-msm2.c"
  1164. #define PLATFORM_DRIVER_PRESENT
  1165. #endif
  1166. #ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
  1167. #include "ehci-pmcmsp.c"
  1168. #define PLATFORM_DRIVER_PRESENT
  1169. #endif
  1170. #ifdef CONFIG_USB_EHCI_TEGRA
  1171. #include "ehci-tegra.c"
  1172. #define PLATFORM_DRIVER_PRESENT
  1173. #endif
  1174. #ifdef CONFIG_USB_EHCI_S5P
  1175. #include "ehci-s5p.c"
  1176. #define PLATFORM_DRIVER_PRESENT
  1177. #endif
  1178. #ifdef CONFIG_USB_EHCI_ATH79
  1179. #include "ehci-ath79.c"
  1180. #define PLATFORM_DRIVER_PRESENT
  1181. #endif
  1182. #ifdef CONFIG_SPARC_LEON
  1183. #include "ehci-grlib.c"
  1184. #define PLATFORM_DRIVER_PRESENT
  1185. #endif
  1186. #ifdef CONFIG_USB_EHCI_MSM_HSIC
  1187. #include "ehci-msm-hsic.c"
  1188. #define PLATFORM_DRIVER_PRESENT
  1189. #endif
  1190. #ifdef CONFIG_USB_PXA168_EHCI
  1191. #include "ehci-pxa168.c"
  1192. #define PLATFORM_DRIVER_PRESENT
  1193. #endif
  1194. #ifdef CONFIG_CPU_XLR
  1195. #include "ehci-xls.c"
  1196. #define PLATFORM_DRIVER_PRESENT
  1197. #endif
  1198. #ifdef CONFIG_USB_EHCI_MV
  1199. #include "ehci-mv.c"
  1200. #define PLATFORM_DRIVER_PRESENT
  1201. #endif
  1202. #ifdef CONFIG_MACH_LOONGSON1
  1203. #include "ehci-ls1x.c"
  1204. #define PLATFORM_DRIVER_PRESENT
  1205. #endif
  1206. #ifdef CONFIG_USB_EHCI_HCD_PLATFORM
  1207. #include "ehci-platform.c"
  1208. #define PLATFORM_DRIVER_PRESENT
  1209. #endif
  1210. #if !defined(PCI_DRIVER) && !defined(PLATFORM_DRIVER_PRESENT) && \
  1211. !defined(PS3_SYSTEM_BUS_DRIVER) && !defined(OF_PLATFORM_DRIVER) && \
  1212. !defined(XILINX_OF_PLATFORM_DRIVER)
  1213. #error "missing bus glue for ehci-hcd"
  1214. #endif
  1215. static struct platform_driver *plat_drivers[] = {
  1216. #ifdef CONFIG_USB_EHCI_FSL
  1217. &ehci_fsl_driver,
  1218. #endif
  1219. #ifdef CONFIG_USB_EHCI_MXC
  1220. &ehci_mxc_driver,
  1221. #endif
  1222. #ifdef CONFIG_CPU_SUBTYPE_SH7786
  1223. &ehci_hcd_sh_driver,
  1224. #endif
  1225. #ifdef CONFIG_SOC_AU1200
  1226. &ehci_hcd_au1xxx_driver,
  1227. #endif
  1228. #ifdef CONFIG_USB_EHCI_HCD_OMAP
  1229. &ehci_hcd_omap_driver,
  1230. #endif
  1231. #ifdef CONFIG_PLAT_ORION
  1232. &ehci_orion_driver,
  1233. #endif
  1234. #ifdef CONFIG_ARCH_IXP4XX
  1235. &ixp4xx_ehci_driver,
  1236. #endif
  1237. #ifdef CONFIG_USB_W90X900_EHCI
  1238. &ehci_hcd_w90x900_driver,
  1239. #endif
  1240. #ifdef CONFIG_ARCH_AT91
  1241. &ehci_atmel_driver,
  1242. #endif
  1243. #ifdef CONFIG_USB_OCTEON_EHCI
  1244. &ehci_octeon_driver,
  1245. #endif
  1246. #ifdef CONFIG_USB_CNS3XXX_EHCI
  1247. &cns3xxx_ehci_driver,
  1248. #endif
  1249. #ifdef CONFIG_ARCH_VT8500
  1250. &vt8500_ehci_driver,
  1251. #endif
  1252. #ifdef CONFIG_PLAT_SPEAR
  1253. &spear_ehci_hcd_driver,
  1254. #endif
  1255. #ifdef CONFIG_USB_EHCI_HCD_PMC_MSP
  1256. &ehci_hcd_msp_driver
  1257. #endif
  1258. #ifdef CONFIG_USB_EHCI_TEGRA
  1259. &tegra_ehci_driver
  1260. #endif
  1261. #ifdef CONFIG_USB_EHCI_S5P
  1262. &s5p_ehci_driver
  1263. #endif
  1264. #ifdef CONFIG_USB_EHCI_ATH79
  1265. &ehci_ath79_driver
  1266. #endif
  1267. #ifdef CONFIG_SPARC_LEON
  1268. &ehci_grlib_driver
  1269. #endif
  1270. #if defined(CONFIG_USB_EHCI_MSM_72K) || defined(CONFIG_USB_EHCI_MSM)
  1271. &ehci_msm_driver,
  1272. #endif
  1273. #ifdef CONFIG_USB_EHCI_MSM_HSIC
  1274. &ehci_msm_hsic_driver,
  1275. #endif
  1276. #ifdef CONFIG_USB_EHCI_MSM
  1277. &ehci_msm2_driver,
  1278. #endif
  1279. #ifdef CONFIG_USB_PXA168_EHCI
  1280. &ehci_pxa168_driver,
  1281. #endif
  1282. #ifdef CONFIG_CPU_XLR
  1283. &ehci_xls_driver,
  1284. #endif
  1285. #ifdef CONFIG_USB_EHCI_MV
  1286. &ehci_mv_driver,
  1287. #endif
  1288. #ifdef CONFIG_MACH_LOONGSON1
  1289. &ehci_ls1x_driver,
  1290. #endif
  1291. #ifdef CONFIG_USB_EHCI_HCD_PLATFORM
  1292. &ehci_platform_driver,
  1293. #endif
  1294. };
  1295. static int __init ehci_hcd_init(void)
  1296. {
  1297. int i, retval = 0;
  1298. if (usb_disabled())
  1299. return -ENODEV;
  1300. printk(KERN_INFO "%s: " DRIVER_DESC "\n", hcd_name);
  1301. set_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1302. if (test_bit(USB_UHCI_LOADED, &usb_hcds_loaded) ||
  1303. test_bit(USB_OHCI_LOADED, &usb_hcds_loaded))
  1304. printk(KERN_WARNING "Warning! ehci_hcd should always be loaded"
  1305. " before uhci_hcd and ohci_hcd, not after\n");
  1306. pr_debug("%s: block sizes: qh %Zd qtd %Zd itd %Zd sitd %Zd\n",
  1307. hcd_name,
  1308. sizeof(struct ehci_qh), sizeof(struct ehci_qtd),
  1309. sizeof(struct ehci_itd), sizeof(struct ehci_sitd));
  1310. #ifdef DEBUG
  1311. ehci_debug_root = debugfs_create_dir("ehci", usb_debug_root);
  1312. if (!ehci_debug_root) {
  1313. retval = -ENOENT;
  1314. goto err_debug;
  1315. }
  1316. #endif
  1317. for (i = 0; i < ARRAY_SIZE(plat_drivers); i++) {
  1318. retval = platform_driver_register(plat_drivers[i]);
  1319. if (retval) {
  1320. while (--i >= 0)
  1321. platform_driver_unregister(plat_drivers[i]);
  1322. goto clean0;
  1323. }
  1324. }
  1325. #ifdef PCI_DRIVER
  1326. retval = pci_register_driver(&PCI_DRIVER);
  1327. if (retval < 0)
  1328. goto clean1;
  1329. #endif
  1330. #ifdef PS3_SYSTEM_BUS_DRIVER
  1331. retval = ps3_ehci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  1332. if (retval < 0)
  1333. goto clean2;
  1334. #endif
  1335. #ifdef OF_PLATFORM_DRIVER
  1336. retval = platform_driver_register(&OF_PLATFORM_DRIVER);
  1337. if (retval < 0)
  1338. goto clean3;
  1339. #endif
  1340. #ifdef XILINX_OF_PLATFORM_DRIVER
  1341. retval = platform_driver_register(&XILINX_OF_PLATFORM_DRIVER);
  1342. if (retval < 0)
  1343. goto clean4;
  1344. #endif
  1345. return retval;
  1346. #ifdef XILINX_OF_PLATFORM_DRIVER
  1347. /* platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER); */
  1348. clean4:
  1349. #endif
  1350. #ifdef OF_PLATFORM_DRIVER
  1351. platform_driver_unregister(&OF_PLATFORM_DRIVER);
  1352. clean3:
  1353. #endif
  1354. #ifdef PS3_SYSTEM_BUS_DRIVER
  1355. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  1356. clean2:
  1357. #endif
  1358. #ifdef PCI_DRIVER
  1359. pci_unregister_driver(&PCI_DRIVER);
  1360. clean1:
  1361. #endif
  1362. for (i = 0; i < ARRAY_SIZE(plat_drivers); i++)
  1363. platform_driver_unregister(plat_drivers[i]);
  1364. clean0:
  1365. #ifdef DEBUG
  1366. debugfs_remove(ehci_debug_root);
  1367. ehci_debug_root = NULL;
  1368. err_debug:
  1369. #endif
  1370. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1371. return retval;
  1372. }
  1373. module_init(ehci_hcd_init);
  1374. static void __exit ehci_hcd_cleanup(void)
  1375. {
  1376. int i;
  1377. #ifdef XILINX_OF_PLATFORM_DRIVER
  1378. platform_driver_unregister(&XILINX_OF_PLATFORM_DRIVER);
  1379. #endif
  1380. #ifdef OF_PLATFORM_DRIVER
  1381. platform_driver_unregister(&OF_PLATFORM_DRIVER);
  1382. #endif
  1383. for (i = 0; i < ARRAY_SIZE(plat_drivers); i++)
  1384. platform_driver_unregister(plat_drivers[i]);
  1385. #ifdef PCI_DRIVER
  1386. pci_unregister_driver(&PCI_DRIVER);
  1387. #endif
  1388. #ifdef PS3_SYSTEM_BUS_DRIVER
  1389. ps3_ehci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  1390. #endif
  1391. #ifdef DEBUG
  1392. debugfs_remove(ehci_debug_root);
  1393. #endif
  1394. clear_bit(USB_EHCI_LOADED, &usb_hcds_loaded);
  1395. }
  1396. module_exit(ehci_hcd_cleanup);