ehci-hcd.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  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. spin_unlock_irq (&ehci->lock);
  428. ehci_mem_cleanup (ehci);
  429. if (ehci->amd_pll_fix == 1)
  430. usb_amd_dev_put();
  431. #ifdef EHCI_STATS
  432. ehci_dbg(ehci, "irq normal %ld err %ld iaa %ld (lost %ld)\n",
  433. ehci->stats.normal, ehci->stats.error, ehci->stats.iaa,
  434. ehci->stats.lost_iaa);
  435. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  436. ehci->stats.complete, ehci->stats.unlink);
  437. #endif
  438. dbg_status (ehci, "ehci_stop completed",
  439. ehci_readl(ehci, &ehci->regs->status));
  440. }
  441. /* one-time init, only for memory state */
  442. static int ehci_init(struct usb_hcd *hcd)
  443. {
  444. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  445. u32 temp;
  446. int retval;
  447. u32 hcc_params;
  448. struct ehci_qh_hw *hw;
  449. spin_lock_init(&ehci->lock);
  450. /*
  451. * keep io watchdog by default, those good HCDs could turn off it later
  452. */
  453. ehci->need_io_watchdog = 1;
  454. init_timer(&ehci->watchdog);
  455. ehci->watchdog.function = ehci_watchdog;
  456. ehci->watchdog.data = (unsigned long) ehci;
  457. init_timer(&ehci->iaa_watchdog);
  458. ehci->iaa_watchdog.function = ehci_iaa_watchdog;
  459. ehci->iaa_watchdog.data = (unsigned long) ehci;
  460. hrtimer_init(&ehci->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  461. ehci->hrtimer.function = ehci_hrtimer_func;
  462. ehci->next_hrtimer_event = EHCI_HRTIMER_NO_EVENT;
  463. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  464. /*
  465. * by default set standard 80% (== 100 usec/uframe) max periodic
  466. * bandwidth as required by USB 2.0
  467. */
  468. ehci->uframe_periodic_max = 100;
  469. /*
  470. * hw default: 1K periodic list heads, one per frame.
  471. * periodic_size can shrink by USBCMD update if hcc_params allows.
  472. */
  473. ehci->periodic_size = DEFAULT_I_TDPS;
  474. INIT_LIST_HEAD(&ehci->cached_itd_list);
  475. INIT_LIST_HEAD(&ehci->cached_sitd_list);
  476. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  477. /* periodic schedule size can be smaller than default */
  478. switch (EHCI_TUNE_FLS) {
  479. case 0: ehci->periodic_size = 1024; break;
  480. case 1: ehci->periodic_size = 512; break;
  481. case 2: ehci->periodic_size = 256; break;
  482. default: BUG();
  483. }
  484. }
  485. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  486. return retval;
  487. /* controllers may cache some of the periodic schedule ... */
  488. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  489. ehci->i_thresh = 2 + 8;
  490. else // N microframes cached
  491. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  492. ehci->next_uframe = -1;
  493. ehci->clock_frame = -1;
  494. /*
  495. * dedicate a qh for the async ring head, since we couldn't unlink
  496. * a 'real' qh without stopping the async schedule [4.8]. use it
  497. * as the 'reclamation list head' too.
  498. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  499. * from automatically advancing to the next td after short reads.
  500. */
  501. ehci->async->qh_next.qh = NULL;
  502. hw = ehci->async->hw;
  503. hw->hw_next = QH_NEXT(ehci, ehci->async->qh_dma);
  504. hw->hw_info1 = cpu_to_hc32(ehci, QH_HEAD);
  505. #if defined(CONFIG_PPC_PS3)
  506. hw->hw_info1 |= cpu_to_hc32(ehci, QH_INACTIVATE);
  507. #endif
  508. hw->hw_token = cpu_to_hc32(ehci, QTD_STS_HALT);
  509. hw->hw_qtd_next = EHCI_LIST_END(ehci);
  510. ehci->async->qh_state = QH_STATE_LINKED;
  511. hw->hw_alt_next = QTD_NEXT(ehci, ehci->async->dummy->qtd_dma);
  512. /* clear interrupt enables, set irq latency */
  513. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  514. log2_irq_thresh = 0;
  515. temp = 1 << (16 + log2_irq_thresh);
  516. if (HCC_PER_PORT_CHANGE_EVENT(hcc_params)) {
  517. ehci->has_ppcd = 1;
  518. ehci_dbg(ehci, "enable per-port change event\n");
  519. temp |= CMD_PPCEE;
  520. }
  521. if (HCC_CANPARK(hcc_params)) {
  522. /* HW default park == 3, on hardware that supports it (like
  523. * NVidia and ALI silicon), maximizes throughput on the async
  524. * schedule by avoiding QH fetches between transfers.
  525. *
  526. * With fast usb storage devices and NForce2, "park" seems to
  527. * make problems: throughput reduction (!), data errors...
  528. */
  529. if (park) {
  530. park = min(park, (unsigned) 3);
  531. temp |= CMD_PARK;
  532. temp |= park << 8;
  533. }
  534. ehci_dbg(ehci, "park %d\n", park);
  535. }
  536. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  537. /* periodic schedule size can be smaller than default */
  538. temp &= ~(3 << 2);
  539. temp |= (EHCI_TUNE_FLS << 2);
  540. }
  541. if (HCC_LPM(hcc_params)) {
  542. /* support link power management EHCI 1.1 addendum */
  543. ehci_dbg(ehci, "support lpm\n");
  544. ehci->has_lpm = 1;
  545. if (hird > 0xf) {
  546. ehci_dbg(ehci, "hird %d invalid, use default 0",
  547. hird);
  548. hird = 0;
  549. }
  550. temp |= hird << 24;
  551. }
  552. ehci->command = temp;
  553. /* Accept arbitrarily long scatter-gather lists */
  554. if (!(hcd->driver->flags & HCD_LOCAL_MEM))
  555. hcd->self.sg_tablesize = ~0;
  556. return 0;
  557. }
  558. /* start HC running; it's halted, ehci_init() has been run (once) */
  559. static int ehci_run (struct usb_hcd *hcd)
  560. {
  561. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  562. u32 temp;
  563. u32 hcc_params;
  564. hcd->uses_new_polling = 1;
  565. /* EHCI spec section 4.1 */
  566. ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
  567. ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
  568. /*
  569. * hcc_params controls whether ehci->regs->segment must (!!!)
  570. * be used; it constrains QH/ITD/SITD and QTD locations.
  571. * pci_pool consistent memory always uses segment zero.
  572. * streaming mappings for I/O buffers, like pci_map_single(),
  573. * can return segments above 4GB, if the device allows.
  574. *
  575. * NOTE: the dma mask is visible through dma_supported(), so
  576. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  577. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  578. * host side drivers though.
  579. */
  580. hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
  581. if (HCC_64BIT_ADDR(hcc_params)) {
  582. ehci_writel(ehci, 0, &ehci->regs->segment);
  583. #if 0
  584. // this is deeply broken on almost all architectures
  585. if (!dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64)))
  586. ehci_info(ehci, "enabled 64bit DMA\n");
  587. #endif
  588. }
  589. // Philips, Intel, and maybe others need CMD_RUN before the
  590. // root hub will detect new devices (why?); NEC doesn't
  591. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  592. ehci->command |= CMD_RUN;
  593. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  594. dbg_cmd (ehci, "init", ehci->command);
  595. /*
  596. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  597. * are explicitly handed to companion controller(s), so no TT is
  598. * involved with the root hub. (Except where one is integrated,
  599. * and there's no companion controller unless maybe for USB OTG.)
  600. *
  601. * Turning on the CF flag will transfer ownership of all ports
  602. * from the companions to the EHCI controller. If any of the
  603. * companions are in the middle of a port reset at the time, it
  604. * could cause trouble. Write-locking ehci_cf_port_reset_rwsem
  605. * guarantees that no resets are in progress. After we set CF,
  606. * a short delay lets the hardware catch up; new resets shouldn't
  607. * be started before the port switching actions could complete.
  608. */
  609. down_write(&ehci_cf_port_reset_rwsem);
  610. ehci->rh_state = EHCI_RH_RUNNING;
  611. ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
  612. ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
  613. msleep(5);
  614. up_write(&ehci_cf_port_reset_rwsem);
  615. ehci->last_periodic_enable = ktime_get_real();
  616. temp = HC_VERSION(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  617. ehci_info (ehci,
  618. "USB %x.%x started, EHCI %x.%02x%s\n",
  619. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  620. temp >> 8, temp & 0xff,
  621. ignore_oc ? ", overcurrent ignored" : "");
  622. ehci_writel(ehci, INTR_MASK,
  623. &ehci->regs->intr_enable); /* Turn On Interrupts */
  624. /* GRR this is run-once init(), being done every time the HC starts.
  625. * So long as they're part of class devices, we can't do it init()
  626. * since the class device isn't created that early.
  627. */
  628. create_debug_files(ehci);
  629. create_sysfs_files(ehci);
  630. return 0;
  631. }
  632. static int ehci_setup(struct usb_hcd *hcd)
  633. {
  634. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  635. int retval;
  636. ehci->regs = (void __iomem *)ehci->caps +
  637. HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
  638. dbg_hcs_params(ehci, "reset");
  639. dbg_hcc_params(ehci, "reset");
  640. /* cache this readonly data; minimize chip reads */
  641. ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
  642. ehci->sbrn = HCD_USB2;
  643. /* data structure init */
  644. retval = ehci_init(hcd);
  645. if (retval)
  646. return retval;
  647. retval = ehci_halt(ehci);
  648. if (retval)
  649. return retval;
  650. if (ehci_is_TDI(ehci))
  651. tdi_reset(ehci);
  652. ehci_reset(ehci);
  653. return 0;
  654. }
  655. /*-------------------------------------------------------------------------*/
  656. static irqreturn_t ehci_irq (struct usb_hcd *hcd)
  657. {
  658. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  659. u32 status, masked_status, pcd_status = 0, cmd;
  660. int bh;
  661. spin_lock (&ehci->lock);
  662. status = ehci_readl(ehci, &ehci->regs->status);
  663. /* e.g. cardbus physical eject */
  664. if (status == ~(u32) 0) {
  665. ehci_dbg (ehci, "device removed\n");
  666. goto dead;
  667. }
  668. /*
  669. * We don't use STS_FLR, but some controllers don't like it to
  670. * remain on, so mask it out along with the other status bits.
  671. */
  672. masked_status = status & (INTR_MASK | STS_FLR);
  673. /* Shared IRQ? */
  674. if (!masked_status || unlikely(ehci->rh_state == EHCI_RH_HALTED)) {
  675. spin_unlock(&ehci->lock);
  676. return IRQ_NONE;
  677. }
  678. /* clear (just) interrupts */
  679. ehci_writel(ehci, masked_status, &ehci->regs->status);
  680. cmd = ehci_readl(ehci, &ehci->regs->command);
  681. bh = 0;
  682. #ifdef VERBOSE_DEBUG
  683. /* unrequested/ignored: Frame List Rollover */
  684. dbg_status (ehci, "irq", status);
  685. #endif
  686. /* INT, ERR, and IAA interrupt rates can be throttled */
  687. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  688. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  689. if (likely ((status & STS_ERR) == 0))
  690. COUNT (ehci->stats.normal);
  691. else
  692. COUNT (ehci->stats.error);
  693. bh = 1;
  694. }
  695. /* complete the unlinking of some qh [4.15.2.3] */
  696. if (status & STS_IAA) {
  697. /* guard against (alleged) silicon errata */
  698. if (cmd & CMD_IAAD)
  699. ehci_dbg(ehci, "IAA with IAAD still set?\n");
  700. if (ehci->async_unlink) {
  701. COUNT(ehci->stats.iaa);
  702. end_unlink_async(ehci);
  703. } else
  704. ehci_dbg(ehci, "IAA with nothing unlinked?\n");
  705. }
  706. /* remote wakeup [4.3.1] */
  707. if (status & STS_PCD) {
  708. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  709. u32 ppcd = 0;
  710. /* kick root hub later */
  711. pcd_status = status;
  712. /* resume root hub? */
  713. if (ehci->rh_state == EHCI_RH_SUSPENDED)
  714. usb_hcd_resume_root_hub(hcd);
  715. /* get per-port change detect bits */
  716. if (ehci->has_ppcd)
  717. ppcd = status >> 16;
  718. while (i--) {
  719. int pstatus;
  720. /* leverage per-port change bits feature */
  721. if (ehci->has_ppcd && !(ppcd & (1 << i)))
  722. continue;
  723. pstatus = ehci_readl(ehci,
  724. &ehci->regs->port_status[i]);
  725. if (pstatus & PORT_OWNER)
  726. continue;
  727. if (!(test_bit(i, &ehci->suspended_ports) &&
  728. ((pstatus & PORT_RESUME) ||
  729. !(pstatus & PORT_SUSPEND)) &&
  730. (pstatus & PORT_PE) &&
  731. ehci->reset_done[i] == 0))
  732. continue;
  733. /* start 20 msec resume signaling from this port,
  734. * and make khubd collect PORT_STAT_C_SUSPEND to
  735. * stop that signaling. Use 5 ms extra for safety,
  736. * like usb_port_resume() does.
  737. */
  738. ehci->reset_done[i] = jiffies + msecs_to_jiffies(25);
  739. set_bit(i, &ehci->resuming_ports);
  740. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  741. mod_timer(&hcd->rh_timer, ehci->reset_done[i]);
  742. }
  743. }
  744. /* PCI errors [4.15.2.4] */
  745. if (unlikely ((status & STS_FATAL) != 0)) {
  746. ehci_err(ehci, "fatal error\n");
  747. dbg_cmd(ehci, "fatal", cmd);
  748. dbg_status(ehci, "fatal", status);
  749. dead:
  750. usb_hc_died(hcd);
  751. /* Don't let the controller do anything more */
  752. ehci->rh_state = EHCI_RH_STOPPING;
  753. ehci->command &= ~(CMD_RUN | CMD_ASE | CMD_PSE);
  754. ehci_writel(ehci, ehci->command, &ehci->regs->command);
  755. ehci_writel(ehci, 0, &ehci->regs->intr_enable);
  756. ehci_handle_controller_death(ehci);
  757. /* Handle completions when the controller stops */
  758. bh = 0;
  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. start_unlink_intr(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. start_unlink_intr(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 */