ehci-hcd.c 43 KB

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