ehci-hcd.c 41 KB

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