ehci-hcd.c 41 KB

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