ehci-hcd.c 40 KB

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