ehci-hcd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Copyright (c) 2000-2004 by David Brownell
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/config.h>
  19. #ifdef CONFIG_USB_DEBUG
  20. #define DEBUG
  21. #else
  22. #undef DEBUG
  23. #endif
  24. #include <linux/module.h>
  25. #include <linux/pci.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/kernel.h>
  28. #include <linux/delay.h>
  29. #include <linux/ioport.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <linux/smp_lock.h>
  33. #include <linux/errno.h>
  34. #include <linux/init.h>
  35. #include <linux/timer.h>
  36. #include <linux/list.h>
  37. #include <linux/interrupt.h>
  38. #include <linux/reboot.h>
  39. #include <linux/usb.h>
  40. #include <linux/moduleparam.h>
  41. #include <linux/dma-mapping.h>
  42. #include "../core/hcd.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. /*-------------------------------------------------------------------------*/
  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. * HISTORY:
  63. *
  64. * 2004-05-10 Root hub and PCI suspend/resume support; remote wakeup. (db)
  65. * 2004-02-24 Replace pci_* with generic dma_* API calls (dsaxena@plexity.net)
  66. * 2003-12-29 Rewritten high speed iso transfer support (by Michal Sojka,
  67. * <sojkam@centrum.cz>, updates by DB).
  68. *
  69. * 2002-11-29 Correct handling for hw async_next register.
  70. * 2002-08-06 Handling for bulk and interrupt transfers is mostly shared;
  71. * only scheduling is different, no arbitrary limitations.
  72. * 2002-07-25 Sanity check PCI reads, mostly for better cardbus support,
  73. * clean up HC run state handshaking.
  74. * 2002-05-24 Preliminary FS/LS interrupts, using scheduling shortcuts
  75. * 2002-05-11 Clear TT errors for FS/LS ctrl/bulk. Fill in some other
  76. * missing pieces: enabling 64bit dma, handoff from BIOS/SMM.
  77. * 2002-05-07 Some error path cleanups to report better errors; wmb();
  78. * use non-CVS version id; better iso bandwidth claim.
  79. * 2002-04-19 Control/bulk/interrupt submit no longer uses giveback() on
  80. * errors in submit path. Bugfixes to interrupt scheduling/processing.
  81. * 2002-03-05 Initial high-speed ISO support; reduce ITD memory; shift
  82. * more checking to generic hcd framework (db). Make it work with
  83. * Philips EHCI; reduce PCI traffic; shorten IRQ path (Rory Bolt).
  84. * 2002-01-14 Minor cleanup; version synch.
  85. * 2002-01-08 Fix roothub handoff of FS/LS to companion controllers.
  86. * 2002-01-04 Control/Bulk queuing behaves.
  87. *
  88. * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
  89. * 2001-June Works with usb-storage and NEC EHCI on 2.4
  90. */
  91. #define DRIVER_VERSION "10 Dec 2004"
  92. #define DRIVER_AUTHOR "David Brownell"
  93. #define DRIVER_DESC "USB 2.0 'Enhanced' Host Controller (EHCI) Driver"
  94. static const char hcd_name [] = "ehci_hcd";
  95. #undef EHCI_VERBOSE_DEBUG
  96. #undef EHCI_URB_TRACE
  97. #ifdef DEBUG
  98. #define EHCI_STATS
  99. #endif
  100. /* magic numbers that can affect system performance */
  101. #define EHCI_TUNE_CERR 3 /* 0-3 qtd retries; 0 == don't stop */
  102. #define EHCI_TUNE_RL_HS 4 /* nak throttle; see 4.9 */
  103. #define EHCI_TUNE_RL_TT 0
  104. #define EHCI_TUNE_MULT_HS 1 /* 1-3 transactions/uframe; 4.10.3 */
  105. #define EHCI_TUNE_MULT_TT 1
  106. #define EHCI_TUNE_FLS 2 /* (small) 256 frame schedule */
  107. #define EHCI_IAA_JIFFIES (HZ/100) /* arbitrary; ~10 msec */
  108. #define EHCI_IO_JIFFIES (HZ/10) /* io watchdog > irq_thresh */
  109. #define EHCI_ASYNC_JIFFIES (HZ/20) /* async idle timeout */
  110. #define EHCI_SHRINK_JIFFIES (HZ/200) /* async qh unlink delay */
  111. /* Initial IRQ latency: faster than hw default */
  112. static int log2_irq_thresh = 0; // 0 to 6
  113. module_param (log2_irq_thresh, int, S_IRUGO);
  114. MODULE_PARM_DESC (log2_irq_thresh, "log2 IRQ latency, 1-64 microframes");
  115. /* initial park setting: slower than hw default */
  116. static unsigned park = 0;
  117. module_param (park, uint, S_IRUGO);
  118. MODULE_PARM_DESC (park, "park setting; 1-3 back-to-back async packets");
  119. #define INTR_MASK (STS_IAA | STS_FATAL | STS_PCD | STS_ERR | STS_INT)
  120. /*-------------------------------------------------------------------------*/
  121. #include "ehci.h"
  122. #include "ehci-dbg.c"
  123. /*-------------------------------------------------------------------------*/
  124. /*
  125. * handshake - spin reading hc until handshake completes or fails
  126. * @ptr: address of hc register to be read
  127. * @mask: bits to look at in result of read
  128. * @done: value of those bits when handshake succeeds
  129. * @usec: timeout in microseconds
  130. *
  131. * Returns negative errno, or zero on success
  132. *
  133. * Success happens when the "mask" bits have the specified value (hardware
  134. * handshake done). There are two failure modes: "usec" have passed (major
  135. * hardware flakeout), or the register reads as all-ones (hardware removed).
  136. *
  137. * That last failure should_only happen in cases like physical cardbus eject
  138. * before driver shutdown. But it also seems to be caused by bugs in cardbus
  139. * bridge shutdown: shutting down the bridge before the devices using it.
  140. */
  141. static int handshake (void __iomem *ptr, u32 mask, u32 done, int usec)
  142. {
  143. u32 result;
  144. do {
  145. result = readl (ptr);
  146. if (result == ~(u32)0) /* card removed */
  147. return -ENODEV;
  148. result &= mask;
  149. if (result == done)
  150. return 0;
  151. udelay (1);
  152. usec--;
  153. } while (usec > 0);
  154. return -ETIMEDOUT;
  155. }
  156. /* force HC to halt state from unknown (EHCI spec section 2.3) */
  157. static int ehci_halt (struct ehci_hcd *ehci)
  158. {
  159. u32 temp = readl (&ehci->regs->status);
  160. /* disable any irqs left enabled by previous code */
  161. writel (0, &ehci->regs->intr_enable);
  162. if ((temp & STS_HALT) != 0)
  163. return 0;
  164. temp = readl (&ehci->regs->command);
  165. temp &= ~CMD_RUN;
  166. writel (temp, &ehci->regs->command);
  167. return handshake (&ehci->regs->status, STS_HALT, STS_HALT, 16 * 125);
  168. }
  169. /* put TDI/ARC silicon into EHCI mode */
  170. static void tdi_reset (struct ehci_hcd *ehci)
  171. {
  172. u32 __iomem *reg_ptr;
  173. u32 tmp;
  174. reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + 0x68);
  175. tmp = readl (reg_ptr);
  176. tmp |= 0x3;
  177. writel (tmp, reg_ptr);
  178. }
  179. /* reset a non-running (STS_HALT == 1) controller */
  180. static int ehci_reset (struct ehci_hcd *ehci)
  181. {
  182. int retval;
  183. u32 command = readl (&ehci->regs->command);
  184. command |= CMD_RESET;
  185. dbg_cmd (ehci, "reset", command);
  186. writel (command, &ehci->regs->command);
  187. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  188. ehci->next_statechange = jiffies;
  189. retval = handshake (&ehci->regs->command, CMD_RESET, 0, 250 * 1000);
  190. if (retval)
  191. return retval;
  192. if (ehci_is_TDI(ehci))
  193. tdi_reset (ehci);
  194. return retval;
  195. }
  196. /* idle the controller (from running) */
  197. static void ehci_quiesce (struct ehci_hcd *ehci)
  198. {
  199. u32 temp;
  200. #ifdef DEBUG
  201. if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state))
  202. BUG ();
  203. #endif
  204. /* wait for any schedule enables/disables to take effect */
  205. temp = readl (&ehci->regs->command) << 10;
  206. temp &= STS_ASS | STS_PSS;
  207. if (handshake (&ehci->regs->status, STS_ASS | STS_PSS,
  208. temp, 16 * 125) != 0) {
  209. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  210. return;
  211. }
  212. /* then disable anything that's still active */
  213. temp = readl (&ehci->regs->command);
  214. temp &= ~(CMD_ASE | CMD_IAAD | CMD_PSE);
  215. writel (temp, &ehci->regs->command);
  216. /* hardware can take 16 microframes to turn off ... */
  217. if (handshake (&ehci->regs->status, STS_ASS | STS_PSS,
  218. 0, 16 * 125) != 0) {
  219. ehci_to_hcd(ehci)->state = HC_STATE_HALT;
  220. return;
  221. }
  222. }
  223. /*-------------------------------------------------------------------------*/
  224. static void ehci_work(struct ehci_hcd *ehci, struct pt_regs *regs);
  225. #include "ehci-hub.c"
  226. #include "ehci-mem.c"
  227. #include "ehci-q.c"
  228. #include "ehci-sched.c"
  229. /*-------------------------------------------------------------------------*/
  230. static void ehci_watchdog (unsigned long param)
  231. {
  232. struct ehci_hcd *ehci = (struct ehci_hcd *) param;
  233. unsigned long flags;
  234. spin_lock_irqsave (&ehci->lock, flags);
  235. /* lost IAA irqs wedge things badly; seen with a vt8235 */
  236. if (ehci->reclaim) {
  237. u32 status = readl (&ehci->regs->status);
  238. if (status & STS_IAA) {
  239. ehci_vdbg (ehci, "lost IAA\n");
  240. COUNT (ehci->stats.lost_iaa);
  241. writel (STS_IAA, &ehci->regs->status);
  242. ehci->reclaim_ready = 1;
  243. }
  244. }
  245. /* stop async processing after it's idled a bit */
  246. if (test_bit (TIMER_ASYNC_OFF, &ehci->actions))
  247. start_unlink_async (ehci, ehci->async);
  248. /* ehci could run by timer, without IRQs ... */
  249. ehci_work (ehci, NULL);
  250. spin_unlock_irqrestore (&ehci->lock, flags);
  251. }
  252. /* Reboot notifiers kick in for silicon on any bus (not just pci, etc).
  253. * This forcibly disables dma and IRQs, helping kexec and other cases
  254. * where the next system software may expect clean state.
  255. */
  256. static int
  257. ehci_reboot (struct notifier_block *self, unsigned long code, void *null)
  258. {
  259. struct ehci_hcd *ehci;
  260. ehci = container_of (self, struct ehci_hcd, reboot_notifier);
  261. (void) ehci_halt (ehci);
  262. /* make BIOS/etc use companion controller during reboot */
  263. writel (0, &ehci->regs->configured_flag);
  264. return 0;
  265. }
  266. static void ehci_port_power (struct ehci_hcd *ehci, int is_on)
  267. {
  268. unsigned port;
  269. if (!HCS_PPC (ehci->hcs_params))
  270. return;
  271. ehci_dbg (ehci, "...power%s ports...\n", is_on ? "up" : "down");
  272. for (port = HCS_N_PORTS (ehci->hcs_params); port > 0; )
  273. (void) ehci_hub_control(ehci_to_hcd(ehci),
  274. is_on ? SetPortFeature : ClearPortFeature,
  275. USB_PORT_FEAT_POWER,
  276. port--, NULL, 0);
  277. msleep(20);
  278. }
  279. /*-------------------------------------------------------------------------*/
  280. /*
  281. * ehci_work is called from some interrupts, timers, and so on.
  282. * it calls driver completion functions, after dropping ehci->lock.
  283. */
  284. static void ehci_work (struct ehci_hcd *ehci, struct pt_regs *regs)
  285. {
  286. timer_action_done (ehci, TIMER_IO_WATCHDOG);
  287. if (ehci->reclaim_ready)
  288. end_unlink_async (ehci, regs);
  289. /* another CPU may drop ehci->lock during a schedule scan while
  290. * it reports urb completions. this flag guards against bogus
  291. * attempts at re-entrant schedule scanning.
  292. */
  293. if (ehci->scanning)
  294. return;
  295. ehci->scanning = 1;
  296. scan_async (ehci, regs);
  297. if (ehci->next_uframe != -1)
  298. scan_periodic (ehci, regs);
  299. ehci->scanning = 0;
  300. /* the IO watchdog guards against hardware or driver bugs that
  301. * misplace IRQs, and should let us run completely without IRQs.
  302. * such lossage has been observed on both VT6202 and VT8235.
  303. */
  304. if (HC_IS_RUNNING (ehci_to_hcd(ehci)->state) &&
  305. (ehci->async->qh_next.ptr != NULL ||
  306. ehci->periodic_sched != 0))
  307. timer_action (ehci, TIMER_IO_WATCHDOG);
  308. }
  309. static void ehci_stop (struct usb_hcd *hcd)
  310. {
  311. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  312. ehci_dbg (ehci, "stop\n");
  313. /* Turn off port power on all root hub ports. */
  314. ehci_port_power (ehci, 0);
  315. /* no more interrupts ... */
  316. del_timer_sync (&ehci->watchdog);
  317. spin_lock_irq(&ehci->lock);
  318. if (HC_IS_RUNNING (hcd->state))
  319. ehci_quiesce (ehci);
  320. ehci_reset (ehci);
  321. writel (0, &ehci->regs->intr_enable);
  322. spin_unlock_irq(&ehci->lock);
  323. /* let companion controllers work when we aren't */
  324. writel (0, &ehci->regs->configured_flag);
  325. unregister_reboot_notifier (&ehci->reboot_notifier);
  326. remove_debug_files (ehci);
  327. /* root hub is shut down separately (first, when possible) */
  328. spin_lock_irq (&ehci->lock);
  329. if (ehci->async)
  330. ehci_work (ehci, NULL);
  331. spin_unlock_irq (&ehci->lock);
  332. ehci_mem_cleanup (ehci);
  333. #ifdef EHCI_STATS
  334. ehci_dbg (ehci, "irq normal %ld err %ld reclaim %ld (lost %ld)\n",
  335. ehci->stats.normal, ehci->stats.error, ehci->stats.reclaim,
  336. ehci->stats.lost_iaa);
  337. ehci_dbg (ehci, "complete %ld unlink %ld\n",
  338. ehci->stats.complete, ehci->stats.unlink);
  339. #endif
  340. dbg_status (ehci, "ehci_stop completed", readl (&ehci->regs->status));
  341. }
  342. /* one-time init, only for memory state */
  343. static int ehci_init(struct usb_hcd *hcd)
  344. {
  345. struct ehci_hcd *ehci = hcd_to_ehci(hcd);
  346. u32 temp;
  347. int retval;
  348. u32 hcc_params;
  349. spin_lock_init(&ehci->lock);
  350. init_timer(&ehci->watchdog);
  351. ehci->watchdog.function = ehci_watchdog;
  352. ehci->watchdog.data = (unsigned long) ehci;
  353. /*
  354. * hw default: 1K periodic list heads, one per frame.
  355. * periodic_size can shrink by USBCMD update if hcc_params allows.
  356. */
  357. ehci->periodic_size = DEFAULT_I_TDPS;
  358. if ((retval = ehci_mem_init(ehci, GFP_KERNEL)) < 0)
  359. return retval;
  360. /* controllers may cache some of the periodic schedule ... */
  361. hcc_params = readl(&ehci->caps->hcc_params);
  362. if (HCC_ISOC_CACHE(hcc_params)) // full frame cache
  363. ehci->i_thresh = 8;
  364. else // N microframes cached
  365. ehci->i_thresh = 2 + HCC_ISOC_THRES(hcc_params);
  366. ehci->reclaim = NULL;
  367. ehci->reclaim_ready = 0;
  368. ehci->next_uframe = -1;
  369. /*
  370. * dedicate a qh for the async ring head, since we couldn't unlink
  371. * a 'real' qh without stopping the async schedule [4.8]. use it
  372. * as the 'reclamation list head' too.
  373. * its dummy is used in hw_alt_next of many tds, to prevent the qh
  374. * from automatically advancing to the next td after short reads.
  375. */
  376. ehci->async->qh_next.qh = NULL;
  377. ehci->async->hw_next = QH_NEXT(ehci->async->qh_dma);
  378. ehci->async->hw_info1 = cpu_to_le32(QH_HEAD);
  379. ehci->async->hw_token = cpu_to_le32(QTD_STS_HALT);
  380. ehci->async->hw_qtd_next = EHCI_LIST_END;
  381. ehci->async->qh_state = QH_STATE_LINKED;
  382. ehci->async->hw_alt_next = QTD_NEXT(ehci->async->dummy->qtd_dma);
  383. /* clear interrupt enables, set irq latency */
  384. if (log2_irq_thresh < 0 || log2_irq_thresh > 6)
  385. log2_irq_thresh = 0;
  386. temp = 1 << (16 + log2_irq_thresh);
  387. if (HCC_CANPARK(hcc_params)) {
  388. /* HW default park == 3, on hardware that supports it (like
  389. * NVidia and ALI silicon), maximizes throughput on the async
  390. * schedule by avoiding QH fetches between transfers.
  391. *
  392. * With fast usb storage devices and NForce2, "park" seems to
  393. * make problems: throughput reduction (!), data errors...
  394. */
  395. if (park) {
  396. park = min(park, (unsigned) 3);
  397. temp |= CMD_PARK;
  398. temp |= park << 8;
  399. }
  400. ehci_dbg(ehci, "park %d\n", park);
  401. }
  402. if (HCC_PGM_FRAMELISTLEN(hcc_params)) {
  403. /* periodic schedule size can be smaller than default */
  404. temp &= ~(3 << 2);
  405. temp |= (EHCI_TUNE_FLS << 2);
  406. switch (EHCI_TUNE_FLS) {
  407. case 0: ehci->periodic_size = 1024; break;
  408. case 1: ehci->periodic_size = 512; break;
  409. case 2: ehci->periodic_size = 256; break;
  410. default: BUG();
  411. }
  412. }
  413. ehci->command = temp;
  414. ehci->reboot_notifier.notifier_call = ehci_reboot;
  415. register_reboot_notifier(&ehci->reboot_notifier);
  416. return 0;
  417. }
  418. /* start HC running; it's halted, ehci_init() has been run (once) */
  419. static int ehci_run (struct usb_hcd *hcd)
  420. {
  421. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  422. int retval;
  423. u32 temp;
  424. u32 hcc_params;
  425. /* EHCI spec section 4.1 */
  426. if ((retval = ehci_reset(ehci)) != 0) {
  427. unregister_reboot_notifier(&ehci->reboot_notifier);
  428. ehci_mem_cleanup(ehci);
  429. return retval;
  430. }
  431. writel(ehci->periodic_dma, &ehci->regs->frame_list);
  432. writel((u32)ehci->async->qh_dma, &ehci->regs->async_next);
  433. /*
  434. * hcc_params controls whether ehci->regs->segment must (!!!)
  435. * be used; it constrains QH/ITD/SITD and QTD locations.
  436. * pci_pool consistent memory always uses segment zero.
  437. * streaming mappings for I/O buffers, like pci_map_single(),
  438. * can return segments above 4GB, if the device allows.
  439. *
  440. * NOTE: the dma mask is visible through dma_supported(), so
  441. * drivers can pass this info along ... like NETIF_F_HIGHDMA,
  442. * Scsi_Host.highmem_io, and so forth. It's readonly to all
  443. * host side drivers though.
  444. */
  445. hcc_params = readl(&ehci->caps->hcc_params);
  446. if (HCC_64BIT_ADDR(hcc_params)) {
  447. writel(0, &ehci->regs->segment);
  448. #if 0
  449. // this is deeply broken on almost all architectures
  450. if (!dma_set_mask(hcd->self.controller, DMA_64BIT_MASK))
  451. ehci_info(ehci, "enabled 64bit DMA\n");
  452. #endif
  453. }
  454. // Philips, Intel, and maybe others need CMD_RUN before the
  455. // root hub will detect new devices (why?); NEC doesn't
  456. ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
  457. ehci->command |= CMD_RUN;
  458. writel (ehci->command, &ehci->regs->command);
  459. dbg_cmd (ehci, "init", ehci->command);
  460. /*
  461. * Start, enabling full USB 2.0 functionality ... usb 1.1 devices
  462. * are explicitly handed to companion controller(s), so no TT is
  463. * involved with the root hub. (Except where one is integrated,
  464. * and there's no companion controller unless maybe for USB OTG.)
  465. */
  466. hcd->state = HC_STATE_RUNNING;
  467. writel (FLAG_CF, &ehci->regs->configured_flag);
  468. readl (&ehci->regs->command); /* unblock posted writes */
  469. temp = HC_VERSION(readl (&ehci->caps->hc_capbase));
  470. ehci_info (ehci,
  471. "USB %x.%x started, EHCI %x.%02x, driver %s\n",
  472. ((ehci->sbrn & 0xf0)>>4), (ehci->sbrn & 0x0f),
  473. temp >> 8, temp & 0xff, DRIVER_VERSION);
  474. writel (INTR_MASK, &ehci->regs->intr_enable); /* Turn On Interrupts */
  475. /* GRR this is run-once init(), being done every time the HC starts.
  476. * So long as they're part of class devices, we can't do it init()
  477. * since the class device isn't created that early.
  478. */
  479. create_debug_files(ehci);
  480. return 0;
  481. }
  482. /*-------------------------------------------------------------------------*/
  483. static irqreturn_t ehci_irq (struct usb_hcd *hcd, struct pt_regs *regs)
  484. {
  485. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  486. u32 status;
  487. int bh;
  488. spin_lock (&ehci->lock);
  489. status = readl (&ehci->regs->status);
  490. /* e.g. cardbus physical eject */
  491. if (status == ~(u32) 0) {
  492. ehci_dbg (ehci, "device removed\n");
  493. goto dead;
  494. }
  495. status &= INTR_MASK;
  496. if (!status) { /* irq sharing? */
  497. spin_unlock(&ehci->lock);
  498. return IRQ_NONE;
  499. }
  500. /* clear (just) interrupts */
  501. writel (status, &ehci->regs->status);
  502. readl (&ehci->regs->command); /* unblock posted write */
  503. bh = 0;
  504. #ifdef EHCI_VERBOSE_DEBUG
  505. /* unrequested/ignored: Frame List Rollover */
  506. dbg_status (ehci, "irq", status);
  507. #endif
  508. /* INT, ERR, and IAA interrupt rates can be throttled */
  509. /* normal [4.15.1.2] or error [4.15.1.1] completion */
  510. if (likely ((status & (STS_INT|STS_ERR)) != 0)) {
  511. if (likely ((status & STS_ERR) == 0))
  512. COUNT (ehci->stats.normal);
  513. else
  514. COUNT (ehci->stats.error);
  515. bh = 1;
  516. }
  517. /* complete the unlinking of some qh [4.15.2.3] */
  518. if (status & STS_IAA) {
  519. COUNT (ehci->stats.reclaim);
  520. ehci->reclaim_ready = 1;
  521. bh = 1;
  522. }
  523. /* remote wakeup [4.3.1] */
  524. if ((status & STS_PCD) && hcd->remote_wakeup) {
  525. unsigned i = HCS_N_PORTS (ehci->hcs_params);
  526. /* resume root hub? */
  527. status = readl (&ehci->regs->command);
  528. if (!(status & CMD_RUN))
  529. writel (status | CMD_RUN, &ehci->regs->command);
  530. while (i--) {
  531. status = readl (&ehci->regs->port_status [i]);
  532. if (status & PORT_OWNER)
  533. continue;
  534. if (!(status & PORT_RESUME)
  535. || ehci->reset_done [i] != 0)
  536. continue;
  537. /* start 20 msec resume signaling from this port,
  538. * and make khubd collect PORT_STAT_C_SUSPEND to
  539. * stop that signaling.
  540. */
  541. ehci->reset_done [i] = jiffies + msecs_to_jiffies (20);
  542. ehci_dbg (ehci, "port %d remote wakeup\n", i + 1);
  543. usb_hcd_resume_root_hub(hcd);
  544. }
  545. }
  546. /* PCI errors [4.15.2.4] */
  547. if (unlikely ((status & STS_FATAL) != 0)) {
  548. /* bogus "fatal" IRQs appear on some chips... why? */
  549. status = readl (&ehci->regs->status);
  550. dbg_cmd (ehci, "fatal", readl (&ehci->regs->command));
  551. dbg_status (ehci, "fatal", status);
  552. if (status & STS_HALT) {
  553. ehci_err (ehci, "fatal error\n");
  554. dead:
  555. ehci_reset (ehci);
  556. writel (0, &ehci->regs->configured_flag);
  557. /* generic layer kills/unlinks all urbs, then
  558. * uses ehci_stop to clean up the rest
  559. */
  560. bh = 1;
  561. }
  562. }
  563. if (bh)
  564. ehci_work (ehci, regs);
  565. spin_unlock (&ehci->lock);
  566. return IRQ_HANDLED;
  567. }
  568. /*-------------------------------------------------------------------------*/
  569. /*
  570. * non-error returns are a promise to giveback() the urb later
  571. * we drop ownership so next owner (or urb unlink) can get it
  572. *
  573. * urb + dev is in hcd.self.controller.urb_list
  574. * we're queueing TDs onto software and hardware lists
  575. *
  576. * hcd-specific init for hcpriv hasn't been done yet
  577. *
  578. * NOTE: control, bulk, and interrupt share the same code to append TDs
  579. * to a (possibly active) QH, and the same QH scanning code.
  580. */
  581. static int ehci_urb_enqueue (
  582. struct usb_hcd *hcd,
  583. struct usb_host_endpoint *ep,
  584. struct urb *urb,
  585. gfp_t mem_flags
  586. ) {
  587. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  588. struct list_head qtd_list;
  589. INIT_LIST_HEAD (&qtd_list);
  590. switch (usb_pipetype (urb->pipe)) {
  591. // case PIPE_CONTROL:
  592. // case PIPE_BULK:
  593. default:
  594. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  595. return -ENOMEM;
  596. return submit_async (ehci, ep, urb, &qtd_list, mem_flags);
  597. case PIPE_INTERRUPT:
  598. if (!qh_urb_transaction (ehci, urb, &qtd_list, mem_flags))
  599. return -ENOMEM;
  600. return intr_submit (ehci, ep, urb, &qtd_list, mem_flags);
  601. case PIPE_ISOCHRONOUS:
  602. if (urb->dev->speed == USB_SPEED_HIGH)
  603. return itd_submit (ehci, urb, mem_flags);
  604. else
  605. return sitd_submit (ehci, urb, mem_flags);
  606. }
  607. }
  608. static void unlink_async (struct ehci_hcd *ehci, struct ehci_qh *qh)
  609. {
  610. /* if we need to use IAA and it's busy, defer */
  611. if (qh->qh_state == QH_STATE_LINKED
  612. && ehci->reclaim
  613. && HC_IS_RUNNING (ehci_to_hcd(ehci)->state)) {
  614. struct ehci_qh *last;
  615. for (last = ehci->reclaim;
  616. last->reclaim;
  617. last = last->reclaim)
  618. continue;
  619. qh->qh_state = QH_STATE_UNLINK_WAIT;
  620. last->reclaim = qh;
  621. /* bypass IAA if the hc can't care */
  622. } else if (!HC_IS_RUNNING (ehci_to_hcd(ehci)->state) && ehci->reclaim)
  623. end_unlink_async (ehci, NULL);
  624. /* something else might have unlinked the qh by now */
  625. if (qh->qh_state == QH_STATE_LINKED)
  626. start_unlink_async (ehci, qh);
  627. }
  628. /* remove from hardware lists
  629. * completions normally happen asynchronously
  630. */
  631. static int ehci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  632. {
  633. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  634. struct ehci_qh *qh;
  635. unsigned long flags;
  636. spin_lock_irqsave (&ehci->lock, flags);
  637. switch (usb_pipetype (urb->pipe)) {
  638. // case PIPE_CONTROL:
  639. // case PIPE_BULK:
  640. default:
  641. qh = (struct ehci_qh *) urb->hcpriv;
  642. if (!qh)
  643. break;
  644. unlink_async (ehci, qh);
  645. break;
  646. case PIPE_INTERRUPT:
  647. qh = (struct ehci_qh *) urb->hcpriv;
  648. if (!qh)
  649. break;
  650. switch (qh->qh_state) {
  651. case QH_STATE_LINKED:
  652. intr_deschedule (ehci, qh);
  653. /* FALL THROUGH */
  654. case QH_STATE_IDLE:
  655. qh_completions (ehci, qh, NULL);
  656. break;
  657. default:
  658. ehci_dbg (ehci, "bogus qh %p state %d\n",
  659. qh, qh->qh_state);
  660. goto done;
  661. }
  662. /* reschedule QH iff another request is queued */
  663. if (!list_empty (&qh->qtd_list)
  664. && HC_IS_RUNNING (hcd->state)) {
  665. int status;
  666. status = qh_schedule (ehci, qh);
  667. spin_unlock_irqrestore (&ehci->lock, flags);
  668. if (status != 0) {
  669. // shouldn't happen often, but ...
  670. // FIXME kill those tds' urbs
  671. err ("can't reschedule qh %p, err %d",
  672. qh, status);
  673. }
  674. return status;
  675. }
  676. break;
  677. case PIPE_ISOCHRONOUS:
  678. // itd or sitd ...
  679. // wait till next completion, do it then.
  680. // completion irqs can wait up to 1024 msec,
  681. break;
  682. }
  683. done:
  684. spin_unlock_irqrestore (&ehci->lock, flags);
  685. return 0;
  686. }
  687. /*-------------------------------------------------------------------------*/
  688. // bulk qh holds the data toggle
  689. static void
  690. ehci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  691. {
  692. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  693. unsigned long flags;
  694. struct ehci_qh *qh, *tmp;
  695. /* ASSERT: any requests/urbs are being unlinked */
  696. /* ASSERT: nobody can be submitting urbs for this any more */
  697. rescan:
  698. spin_lock_irqsave (&ehci->lock, flags);
  699. qh = ep->hcpriv;
  700. if (!qh)
  701. goto done;
  702. /* endpoints can be iso streams. for now, we don't
  703. * accelerate iso completions ... so spin a while.
  704. */
  705. if (qh->hw_info1 == 0) {
  706. ehci_vdbg (ehci, "iso delay\n");
  707. goto idle_timeout;
  708. }
  709. if (!HC_IS_RUNNING (hcd->state))
  710. qh->qh_state = QH_STATE_IDLE;
  711. switch (qh->qh_state) {
  712. case QH_STATE_LINKED:
  713. for (tmp = ehci->async->qh_next.qh;
  714. tmp && tmp != qh;
  715. tmp = tmp->qh_next.qh)
  716. continue;
  717. /* periodic qh self-unlinks on empty */
  718. if (!tmp)
  719. goto nogood;
  720. unlink_async (ehci, qh);
  721. /* FALL THROUGH */
  722. case QH_STATE_UNLINK: /* wait for hw to finish? */
  723. idle_timeout:
  724. spin_unlock_irqrestore (&ehci->lock, flags);
  725. schedule_timeout_uninterruptible(1);
  726. goto rescan;
  727. case QH_STATE_IDLE: /* fully unlinked */
  728. if (list_empty (&qh->qtd_list)) {
  729. qh_put (qh);
  730. break;
  731. }
  732. /* else FALL THROUGH */
  733. default:
  734. nogood:
  735. /* caller was supposed to have unlinked any requests;
  736. * that's not our job. just leak this memory.
  737. */
  738. ehci_err (ehci, "qh %p (#%02x) state %d%s\n",
  739. qh, ep->desc.bEndpointAddress, qh->qh_state,
  740. list_empty (&qh->qtd_list) ? "" : "(has tds)");
  741. break;
  742. }
  743. ep->hcpriv = NULL;
  744. done:
  745. spin_unlock_irqrestore (&ehci->lock, flags);
  746. return;
  747. }
  748. static int ehci_get_frame (struct usb_hcd *hcd)
  749. {
  750. struct ehci_hcd *ehci = hcd_to_ehci (hcd);
  751. return (readl (&ehci->regs->frame_index) >> 3) % ehci->periodic_size;
  752. }
  753. /*-------------------------------------------------------------------------*/
  754. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  755. MODULE_DESCRIPTION (DRIVER_INFO);
  756. MODULE_AUTHOR (DRIVER_AUTHOR);
  757. MODULE_LICENSE ("GPL");
  758. #ifdef CONFIG_PCI
  759. #include "ehci-pci.c"
  760. #endif
  761. #if !defined(CONFIG_PCI)
  762. #error "missing bus glue for ehci-hcd"
  763. #endif