ehci-hcd.c 39 KB

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