ehci-hcd.c 41 KB

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