ohci-hcd.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /*
  2. * OHCI HCD (Host Controller Driver) for USB.
  3. *
  4. * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
  5. * (C) Copyright 2000-2004 David Brownell <dbrownell@users.sourceforge.net>
  6. *
  7. * [ Initialisation is based on Linus' ]
  8. * [ uhci code and gregs ohci fragments ]
  9. * [ (C) Copyright 1999 Linus Torvalds ]
  10. * [ (C) Copyright 1999 Gregory P. Smith]
  11. *
  12. *
  13. * OHCI is the main "non-Intel/VIA" standard for USB 1.1 host controller
  14. * interfaces (though some non-x86 Intel chips use it). It supports
  15. * smarter hardware than UHCI. A download link for the spec available
  16. * through the http://www.usb.org website.
  17. *
  18. * This file is licenced under the GPL.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/pci.h>
  23. #include <linux/kernel.h>
  24. #include <linux/delay.h>
  25. #include <linux/ioport.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include <linux/errno.h>
  29. #include <linux/init.h>
  30. #include <linux/timer.h>
  31. #include <linux/list.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb/otg.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/dmapool.h>
  36. #include <linux/reboot.h>
  37. #include <linux/workqueue.h>
  38. #include <asm/io.h>
  39. #include <asm/irq.h>
  40. #include <asm/system.h>
  41. #include <asm/unaligned.h>
  42. #include <asm/byteorder.h>
  43. #include "../core/hcd.h"
  44. #define DRIVER_VERSION "2006 August 04"
  45. #define DRIVER_AUTHOR "Roman Weissgaerber, David Brownell"
  46. #define DRIVER_DESC "USB 1.1 'Open' Host Controller (OHCI) Driver"
  47. /*-------------------------------------------------------------------------*/
  48. #undef OHCI_VERBOSE_DEBUG /* not always helpful */
  49. /* For initializing controller (mask in an HCFS mode too) */
  50. #define OHCI_CONTROL_INIT OHCI_CTRL_CBSR
  51. #define OHCI_INTR_INIT \
  52. (OHCI_INTR_MIE | OHCI_INTR_RHSC | OHCI_INTR_UE \
  53. | OHCI_INTR_RD | OHCI_INTR_WDH)
  54. #ifdef __hppa__
  55. /* On PA-RISC, PDC can leave IR set incorrectly; ignore it there. */
  56. #define IR_DISABLE
  57. #endif
  58. #ifdef CONFIG_ARCH_OMAP
  59. /* OMAP doesn't support IR (no SMM; not needed) */
  60. #define IR_DISABLE
  61. #endif
  62. /*-------------------------------------------------------------------------*/
  63. static const char hcd_name [] = "ohci_hcd";
  64. #define STATECHANGE_DELAY msecs_to_jiffies(300)
  65. #include "ohci.h"
  66. static void ohci_dump (struct ohci_hcd *ohci, int verbose);
  67. static int ohci_init (struct ohci_hcd *ohci);
  68. static void ohci_stop (struct usb_hcd *hcd);
  69. #if defined(CONFIG_PM) || defined(CONFIG_PCI)
  70. static int ohci_restart (struct ohci_hcd *ohci);
  71. #endif
  72. #include "ohci-hub.c"
  73. #include "ohci-dbg.c"
  74. #include "ohci-mem.c"
  75. #include "ohci-q.c"
  76. /*
  77. * On architectures with edge-triggered interrupts we must never return
  78. * IRQ_NONE.
  79. */
  80. #if defined(CONFIG_SA1111) /* ... or other edge-triggered systems */
  81. #define IRQ_NOTMINE IRQ_HANDLED
  82. #else
  83. #define IRQ_NOTMINE IRQ_NONE
  84. #endif
  85. /* Some boards misreport power switching/overcurrent */
  86. static int distrust_firmware = 1;
  87. module_param (distrust_firmware, bool, 0);
  88. MODULE_PARM_DESC (distrust_firmware,
  89. "true to distrust firmware power/overcurrent setup");
  90. /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
  91. static int no_handshake = 0;
  92. module_param (no_handshake, bool, 0);
  93. MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
  94. /*-------------------------------------------------------------------------*/
  95. /*
  96. * queue up an urb for anything except the root hub
  97. */
  98. static int ohci_urb_enqueue (
  99. struct usb_hcd *hcd,
  100. struct urb *urb,
  101. gfp_t mem_flags
  102. ) {
  103. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  104. struct ed *ed;
  105. urb_priv_t *urb_priv;
  106. unsigned int pipe = urb->pipe;
  107. int i, size = 0;
  108. unsigned long flags;
  109. int retval = 0;
  110. #ifdef OHCI_VERBOSE_DEBUG
  111. urb_print(urb, "SUB", usb_pipein(pipe), -EINPROGRESS);
  112. #endif
  113. /* every endpoint has a ed, locate and maybe (re)initialize it */
  114. if (! (ed = ed_get (ohci, urb->ep, urb->dev, pipe, urb->interval)))
  115. return -ENOMEM;
  116. /* for the private part of the URB we need the number of TDs (size) */
  117. switch (ed->type) {
  118. case PIPE_CONTROL:
  119. /* td_submit_urb() doesn't yet handle these */
  120. if (urb->transfer_buffer_length > 4096)
  121. return -EMSGSIZE;
  122. /* 1 TD for setup, 1 for ACK, plus ... */
  123. size = 2;
  124. /* FALLTHROUGH */
  125. // case PIPE_INTERRUPT:
  126. // case PIPE_BULK:
  127. default:
  128. /* one TD for every 4096 Bytes (can be upto 8K) */
  129. size += urb->transfer_buffer_length / 4096;
  130. /* ... and for any remaining bytes ... */
  131. if ((urb->transfer_buffer_length % 4096) != 0)
  132. size++;
  133. /* ... and maybe a zero length packet to wrap it up */
  134. if (size == 0)
  135. size++;
  136. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  137. && (urb->transfer_buffer_length
  138. % usb_maxpacket (urb->dev, pipe,
  139. usb_pipeout (pipe))) == 0)
  140. size++;
  141. break;
  142. case PIPE_ISOCHRONOUS: /* number of packets from URB */
  143. size = urb->number_of_packets;
  144. break;
  145. }
  146. /* allocate the private part of the URB */
  147. urb_priv = kzalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
  148. mem_flags);
  149. if (!urb_priv)
  150. return -ENOMEM;
  151. INIT_LIST_HEAD (&urb_priv->pending);
  152. urb_priv->length = size;
  153. urb_priv->ed = ed;
  154. /* allocate the TDs (deferring hash chain updates) */
  155. for (i = 0; i < size; i++) {
  156. urb_priv->td [i] = td_alloc (ohci, mem_flags);
  157. if (!urb_priv->td [i]) {
  158. urb_priv->length = i;
  159. urb_free_priv (ohci, urb_priv);
  160. return -ENOMEM;
  161. }
  162. }
  163. spin_lock_irqsave (&ohci->lock, flags);
  164. /* don't submit to a dead HC */
  165. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  166. retval = -ENODEV;
  167. goto fail;
  168. }
  169. if (!HC_IS_RUNNING(hcd->state)) {
  170. retval = -ENODEV;
  171. goto fail;
  172. }
  173. retval = usb_hcd_link_urb_to_ep(hcd, urb);
  174. if (retval)
  175. goto fail;
  176. /* schedule the ed if needed */
  177. if (ed->state == ED_IDLE) {
  178. retval = ed_schedule (ohci, ed);
  179. if (retval < 0) {
  180. usb_hcd_unlink_urb_from_ep(hcd, urb);
  181. goto fail;
  182. }
  183. if (ed->type == PIPE_ISOCHRONOUS) {
  184. u16 frame = ohci_frame_no(ohci);
  185. /* delay a few frames before the first TD */
  186. frame += max_t (u16, 8, ed->interval);
  187. frame &= ~(ed->interval - 1);
  188. frame |= ed->branch;
  189. urb->start_frame = frame;
  190. /* yes, only URB_ISO_ASAP is supported, and
  191. * urb->start_frame is never used as input.
  192. */
  193. }
  194. } else if (ed->type == PIPE_ISOCHRONOUS)
  195. urb->start_frame = ed->last_iso + ed->interval;
  196. /* fill the TDs and link them to the ed; and
  197. * enable that part of the schedule, if needed
  198. * and update count of queued periodic urbs
  199. */
  200. urb->hcpriv = urb_priv;
  201. td_submit_urb (ohci, urb);
  202. fail:
  203. if (retval)
  204. urb_free_priv (ohci, urb_priv);
  205. spin_unlock_irqrestore (&ohci->lock, flags);
  206. return retval;
  207. }
  208. /*
  209. * decouple the URB from the HC queues (TDs, urb_priv).
  210. * reporting is always done
  211. * asynchronously, and we might be dealing with an urb that's
  212. * partially transferred, or an ED with other urbs being unlinked.
  213. */
  214. static int ohci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  215. {
  216. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  217. unsigned long flags;
  218. int rc;
  219. #ifdef OHCI_VERBOSE_DEBUG
  220. urb_print(urb, "UNLINK", 1, status);
  221. #endif
  222. spin_lock_irqsave (&ohci->lock, flags);
  223. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  224. if (rc) {
  225. ; /* Do nothing */
  226. } else if (HC_IS_RUNNING(hcd->state)) {
  227. urb_priv_t *urb_priv;
  228. /* Unless an IRQ completed the unlink while it was being
  229. * handed to us, flag it for unlink and giveback, and force
  230. * some upcoming INTR_SF to call finish_unlinks()
  231. */
  232. urb_priv = urb->hcpriv;
  233. if (urb_priv) {
  234. if (urb_priv->ed->state == ED_OPER)
  235. start_ed_unlink (ohci, urb_priv->ed);
  236. }
  237. } else {
  238. /*
  239. * with HC dead, we won't respect hc queue pointers
  240. * any more ... just clean up every urb's memory.
  241. */
  242. if (urb->hcpriv)
  243. finish_urb(ohci, urb, status);
  244. }
  245. spin_unlock_irqrestore (&ohci->lock, flags);
  246. return rc;
  247. }
  248. /*-------------------------------------------------------------------------*/
  249. /* frees config/altsetting state for endpoints,
  250. * including ED memory, dummy TD, and bulk/intr data toggle
  251. */
  252. static void
  253. ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  254. {
  255. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  256. unsigned long flags;
  257. struct ed *ed = ep->hcpriv;
  258. unsigned limit = 1000;
  259. /* ASSERT: any requests/urbs are being unlinked */
  260. /* ASSERT: nobody can be submitting urbs for this any more */
  261. if (!ed)
  262. return;
  263. rescan:
  264. spin_lock_irqsave (&ohci->lock, flags);
  265. if (!HC_IS_RUNNING (hcd->state)) {
  266. sanitize:
  267. ed->state = ED_IDLE;
  268. if (quirk_zfmicro(ohci) && ed->type == PIPE_INTERRUPT)
  269. ohci->eds_scheduled--;
  270. finish_unlinks (ohci, 0);
  271. }
  272. switch (ed->state) {
  273. case ED_UNLINK: /* wait for hw to finish? */
  274. /* major IRQ delivery trouble loses INTR_SF too... */
  275. if (limit-- == 0) {
  276. ohci_warn(ohci, "ED unlink timeout\n");
  277. if (quirk_zfmicro(ohci)) {
  278. ohci_warn(ohci, "Attempting ZF TD recovery\n");
  279. ohci->ed_to_check = ed;
  280. ohci->zf_delay = 2;
  281. }
  282. goto sanitize;
  283. }
  284. spin_unlock_irqrestore (&ohci->lock, flags);
  285. schedule_timeout_uninterruptible(1);
  286. goto rescan;
  287. case ED_IDLE: /* fully unlinked */
  288. if (list_empty (&ed->td_list)) {
  289. td_free (ohci, ed->dummy);
  290. ed_free (ohci, ed);
  291. break;
  292. }
  293. /* else FALL THROUGH */
  294. default:
  295. /* caller was supposed to have unlinked any requests;
  296. * that's not our job. can't recover; must leak ed.
  297. */
  298. ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
  299. ed, ep->desc.bEndpointAddress, ed->state,
  300. list_empty (&ed->td_list) ? "" : " (has tds)");
  301. td_free (ohci, ed->dummy);
  302. break;
  303. }
  304. ep->hcpriv = NULL;
  305. spin_unlock_irqrestore (&ohci->lock, flags);
  306. return;
  307. }
  308. static int ohci_get_frame (struct usb_hcd *hcd)
  309. {
  310. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  311. return ohci_frame_no(ohci);
  312. }
  313. static void ohci_usb_reset (struct ohci_hcd *ohci)
  314. {
  315. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  316. ohci->hc_control &= OHCI_CTRL_RWC;
  317. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  318. }
  319. /* ohci_shutdown forcibly disables IRQs and DMA, helping kexec and
  320. * other cases where the next software may expect clean state from the
  321. * "firmware". this is bus-neutral, unlike shutdown() methods.
  322. */
  323. static void
  324. ohci_shutdown (struct usb_hcd *hcd)
  325. {
  326. struct ohci_hcd *ohci;
  327. ohci = hcd_to_ohci (hcd);
  328. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  329. ohci_usb_reset (ohci);
  330. /* flush the writes */
  331. (void) ohci_readl (ohci, &ohci->regs->control);
  332. }
  333. static int check_ed(struct ohci_hcd *ohci, struct ed *ed)
  334. {
  335. return (hc32_to_cpu(ohci, ed->hwINFO) & ED_IN) != 0
  336. && (hc32_to_cpu(ohci, ed->hwHeadP) & TD_MASK)
  337. == (hc32_to_cpu(ohci, ed->hwTailP) & TD_MASK)
  338. && !list_empty(&ed->td_list);
  339. }
  340. /* ZF Micro watchdog timer callback. The ZF Micro chipset sometimes completes
  341. * an interrupt TD but neglects to add it to the donelist. On systems with
  342. * this chipset, we need to periodically check the state of the queues to look
  343. * for such "lost" TDs.
  344. */
  345. static void unlink_watchdog_func(unsigned long _ohci)
  346. {
  347. unsigned long flags;
  348. unsigned max;
  349. unsigned seen_count = 0;
  350. unsigned i;
  351. struct ed **seen = NULL;
  352. struct ohci_hcd *ohci = (struct ohci_hcd *) _ohci;
  353. spin_lock_irqsave(&ohci->lock, flags);
  354. max = ohci->eds_scheduled;
  355. if (!max)
  356. goto done;
  357. if (ohci->ed_to_check)
  358. goto out;
  359. seen = kcalloc(max, sizeof *seen, GFP_ATOMIC);
  360. if (!seen)
  361. goto out;
  362. for (i = 0; i < NUM_INTS; i++) {
  363. struct ed *ed = ohci->periodic[i];
  364. while (ed) {
  365. unsigned temp;
  366. /* scan this branch of the periodic schedule tree */
  367. for (temp = 0; temp < seen_count; temp++) {
  368. if (seen[temp] == ed) {
  369. /* we've checked it and what's after */
  370. ed = NULL;
  371. break;
  372. }
  373. }
  374. if (!ed)
  375. break;
  376. seen[seen_count++] = ed;
  377. if (!check_ed(ohci, ed)) {
  378. ed = ed->ed_next;
  379. continue;
  380. }
  381. /* HC's TD list is empty, but HCD sees at least one
  382. * TD that's not been sent through the donelist.
  383. */
  384. ohci->ed_to_check = ed;
  385. ohci->zf_delay = 2;
  386. /* The HC may wait until the next frame to report the
  387. * TD as done through the donelist and INTR_WDH. (We
  388. * just *assume* it's not a multi-TD interrupt URB;
  389. * those could defer the IRQ more than one frame, using
  390. * DI...) Check again after the next INTR_SF.
  391. */
  392. ohci_writel(ohci, OHCI_INTR_SF,
  393. &ohci->regs->intrstatus);
  394. ohci_writel(ohci, OHCI_INTR_SF,
  395. &ohci->regs->intrenable);
  396. /* flush those writes */
  397. (void) ohci_readl(ohci, &ohci->regs->control);
  398. goto out;
  399. }
  400. }
  401. out:
  402. kfree(seen);
  403. if (ohci->eds_scheduled)
  404. mod_timer(&ohci->unlink_watchdog, round_jiffies_relative(HZ));
  405. done:
  406. spin_unlock_irqrestore(&ohci->lock, flags);
  407. }
  408. /*-------------------------------------------------------------------------*
  409. * HC functions
  410. *-------------------------------------------------------------------------*/
  411. /* init memory, and kick BIOS/SMM off */
  412. static int ohci_init (struct ohci_hcd *ohci)
  413. {
  414. int ret;
  415. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  416. disable (ohci);
  417. ohci->regs = hcd->regs;
  418. /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
  419. * was never needed for most non-PCI systems ... remove the code?
  420. */
  421. #ifndef IR_DISABLE
  422. /* SMM owns the HC? not for long! */
  423. if (!no_handshake && ohci_readl (ohci,
  424. &ohci->regs->control) & OHCI_CTRL_IR) {
  425. u32 temp;
  426. ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
  427. /* this timeout is arbitrary. we make it long, so systems
  428. * depending on usb keyboards may be usable even if the
  429. * BIOS/SMM code seems pretty broken.
  430. */
  431. temp = 500; /* arbitrary: five seconds */
  432. ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
  433. ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
  434. while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
  435. msleep (10);
  436. if (--temp == 0) {
  437. ohci_err (ohci, "USB HC takeover failed!"
  438. " (BIOS/SMM bug)\n");
  439. return -EBUSY;
  440. }
  441. }
  442. ohci_usb_reset (ohci);
  443. }
  444. #endif
  445. /* Disable HC interrupts */
  446. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  447. /* flush the writes, and save key bits like RWC */
  448. if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
  449. ohci->hc_control |= OHCI_CTRL_RWC;
  450. /* Read the number of ports unless overridden */
  451. if (ohci->num_ports == 0)
  452. ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
  453. if (ohci->hcca)
  454. return 0;
  455. ohci->hcca = dma_alloc_coherent (hcd->self.controller,
  456. sizeof *ohci->hcca, &ohci->hcca_dma, 0);
  457. if (!ohci->hcca)
  458. return -ENOMEM;
  459. if ((ret = ohci_mem_init (ohci)) < 0)
  460. ohci_stop (hcd);
  461. else {
  462. create_debug_files (ohci);
  463. }
  464. return ret;
  465. }
  466. /*-------------------------------------------------------------------------*/
  467. /* Start an OHCI controller, set the BUS operational
  468. * resets USB and controller
  469. * enable interrupts
  470. */
  471. static int ohci_run (struct ohci_hcd *ohci)
  472. {
  473. u32 mask, temp;
  474. int first = ohci->fminterval == 0;
  475. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  476. disable (ohci);
  477. /* boot firmware should have set this up (5.1.1.3.1) */
  478. if (first) {
  479. temp = ohci_readl (ohci, &ohci->regs->fminterval);
  480. ohci->fminterval = temp & 0x3fff;
  481. if (ohci->fminterval != FI)
  482. ohci_dbg (ohci, "fminterval delta %d\n",
  483. ohci->fminterval - FI);
  484. ohci->fminterval |= FSMP (ohci->fminterval) << 16;
  485. /* also: power/overcurrent flags in roothub.a */
  486. }
  487. /* Reset USB nearly "by the book". RemoteWakeupConnected was
  488. * saved if boot firmware (BIOS/SMM/...) told us it's connected,
  489. * or if bus glue did the same (e.g. for PCI add-in cards with
  490. * PCI PM support).
  491. */
  492. if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
  493. && !device_may_wakeup(hcd->self.controller))
  494. device_init_wakeup(hcd->self.controller, 1);
  495. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  496. case OHCI_USB_OPER:
  497. temp = 0;
  498. break;
  499. case OHCI_USB_SUSPEND:
  500. case OHCI_USB_RESUME:
  501. ohci->hc_control &= OHCI_CTRL_RWC;
  502. ohci->hc_control |= OHCI_USB_RESUME;
  503. temp = 10 /* msec wait */;
  504. break;
  505. // case OHCI_USB_RESET:
  506. default:
  507. ohci->hc_control &= OHCI_CTRL_RWC;
  508. ohci->hc_control |= OHCI_USB_RESET;
  509. temp = 50 /* msec wait */;
  510. break;
  511. }
  512. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  513. // flush the writes
  514. (void) ohci_readl (ohci, &ohci->regs->control);
  515. msleep(temp);
  516. memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
  517. /* 2msec timelimit here means no irqs/preempt */
  518. spin_lock_irq (&ohci->lock);
  519. retry:
  520. /* HC Reset requires max 10 us delay */
  521. ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
  522. temp = 30; /* ... allow extra time */
  523. while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
  524. if (--temp == 0) {
  525. spin_unlock_irq (&ohci->lock);
  526. ohci_err (ohci, "USB HC reset timed out!\n");
  527. return -1;
  528. }
  529. udelay (1);
  530. }
  531. /* now we're in the SUSPEND state ... must go OPERATIONAL
  532. * within 2msec else HC enters RESUME
  533. *
  534. * ... but some hardware won't init fmInterval "by the book"
  535. * (SiS, OPTi ...), so reset again instead. SiS doesn't need
  536. * this if we write fmInterval after we're OPERATIONAL.
  537. * Unclear about ALi, ServerWorks, and others ... this could
  538. * easily be a longstanding bug in chip init on Linux.
  539. */
  540. if (ohci->flags & OHCI_QUIRK_INITRESET) {
  541. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  542. // flush those writes
  543. (void) ohci_readl (ohci, &ohci->regs->control);
  544. }
  545. /* Tell the controller where the control and bulk lists are
  546. * The lists are empty now. */
  547. ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
  548. ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
  549. /* a reset clears this */
  550. ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
  551. periodic_reinit (ohci);
  552. /* some OHCI implementations are finicky about how they init.
  553. * bogus values here mean not even enumeration could work.
  554. */
  555. if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
  556. || !ohci_readl (ohci, &ohci->regs->periodicstart)) {
  557. if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
  558. ohci->flags |= OHCI_QUIRK_INITRESET;
  559. ohci_dbg (ohci, "enabling initreset quirk\n");
  560. goto retry;
  561. }
  562. spin_unlock_irq (&ohci->lock);
  563. ohci_err (ohci, "init err (%08x %04x)\n",
  564. ohci_readl (ohci, &ohci->regs->fminterval),
  565. ohci_readl (ohci, &ohci->regs->periodicstart));
  566. return -EOVERFLOW;
  567. }
  568. /* use rhsc irqs after khubd is fully initialized */
  569. hcd->poll_rh = 1;
  570. hcd->uses_new_polling = 1;
  571. /* start controller operations */
  572. ohci->hc_control &= OHCI_CTRL_RWC;
  573. ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
  574. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  575. hcd->state = HC_STATE_RUNNING;
  576. /* wake on ConnectStatusChange, matching external hubs */
  577. ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
  578. /* Choose the interrupts we care about now, others later on demand */
  579. mask = OHCI_INTR_INIT;
  580. ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
  581. ohci_writel (ohci, mask, &ohci->regs->intrenable);
  582. /* handle root hub init quirks ... */
  583. temp = roothub_a (ohci);
  584. temp &= ~(RH_A_PSM | RH_A_OCPM);
  585. if (ohci->flags & OHCI_QUIRK_SUPERIO) {
  586. /* NSC 87560 and maybe others */
  587. temp |= RH_A_NOCP;
  588. temp &= ~(RH_A_POTPGT | RH_A_NPS);
  589. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  590. } else if ((ohci->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
  591. /* hub power always on; required for AMD-756 and some
  592. * Mac platforms. ganged overcurrent reporting, if any.
  593. */
  594. temp |= RH_A_NPS;
  595. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  596. }
  597. ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
  598. ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM,
  599. &ohci->regs->roothub.b);
  600. // flush those writes
  601. (void) ohci_readl (ohci, &ohci->regs->control);
  602. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  603. spin_unlock_irq (&ohci->lock);
  604. // POTPGT delay is bits 24-31, in 2 ms units.
  605. mdelay ((temp >> 23) & 0x1fe);
  606. hcd->state = HC_STATE_RUNNING;
  607. if (quirk_zfmicro(ohci)) {
  608. /* Create timer to watch for bad queue state on ZF Micro */
  609. setup_timer(&ohci->unlink_watchdog, unlink_watchdog_func,
  610. (unsigned long) ohci);
  611. ohci->eds_scheduled = 0;
  612. ohci->ed_to_check = NULL;
  613. }
  614. ohci_dump (ohci, 1);
  615. return 0;
  616. }
  617. /*-------------------------------------------------------------------------*/
  618. /* an interrupt happens */
  619. static irqreturn_t ohci_irq (struct usb_hcd *hcd)
  620. {
  621. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  622. struct ohci_regs __iomem *regs = ohci->regs;
  623. int ints;
  624. /* Read interrupt status (and flush pending writes). We ignore the
  625. * optimization of checking the LSB of hcca->done_head; it doesn't
  626. * work on all systems (edge triggering for OHCI can be a factor).
  627. */
  628. ints = ohci_readl(ohci, &regs->intrstatus);
  629. /* Check for an all 1's result which is a typical consequence
  630. * of dead, unclocked, or unplugged (CardBus...) devices
  631. */
  632. if (ints == ~(u32)0) {
  633. disable (ohci);
  634. ohci_dbg (ohci, "device removed!\n");
  635. return IRQ_HANDLED;
  636. }
  637. /* We only care about interrupts that are enabled */
  638. ints &= ohci_readl(ohci, &regs->intrenable);
  639. /* interrupt for some other device? */
  640. if (ints == 0)
  641. return IRQ_NOTMINE;
  642. if (ints & OHCI_INTR_UE) {
  643. // e.g. due to PCI Master/Target Abort
  644. if (quirk_nec(ohci)) {
  645. /* Workaround for a silicon bug in some NEC chips used
  646. * in Apple's PowerBooks. Adapted from Darwin code.
  647. */
  648. ohci_err (ohci, "OHCI Unrecoverable Error, scheduling NEC chip restart\n");
  649. ohci_writel (ohci, OHCI_INTR_UE, &regs->intrdisable);
  650. schedule_work (&ohci->nec_work);
  651. } else {
  652. disable (ohci);
  653. ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
  654. }
  655. ohci_dump (ohci, 1);
  656. ohci_usb_reset (ohci);
  657. }
  658. if (ints & OHCI_INTR_RHSC) {
  659. ohci_vdbg(ohci, "rhsc\n");
  660. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  661. ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
  662. &regs->intrstatus);
  663. /* NOTE: Vendors didn't always make the same implementation
  664. * choices for RHSC. Many followed the spec; RHSC triggers
  665. * on an edge, like setting and maybe clearing a port status
  666. * change bit. With others it's level-triggered, active
  667. * until khubd clears all the port status change bits. We'll
  668. * always disable it here and rely on polling until khubd
  669. * re-enables it.
  670. */
  671. ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
  672. usb_hcd_poll_rh_status(hcd);
  673. }
  674. /* For connect and disconnect events, we expect the controller
  675. * to turn on RHSC along with RD. But for remote wakeup events
  676. * this might not happen.
  677. */
  678. else if (ints & OHCI_INTR_RD) {
  679. ohci_vdbg(ohci, "resume detect\n");
  680. ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
  681. hcd->poll_rh = 1;
  682. if (ohci->autostop) {
  683. spin_lock (&ohci->lock);
  684. ohci_rh_resume (ohci);
  685. spin_unlock (&ohci->lock);
  686. } else
  687. usb_hcd_resume_root_hub(hcd);
  688. }
  689. if (ints & OHCI_INTR_WDH) {
  690. if (HC_IS_RUNNING(hcd->state))
  691. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrdisable);
  692. spin_lock (&ohci->lock);
  693. dl_done_list (ohci);
  694. spin_unlock (&ohci->lock);
  695. if (HC_IS_RUNNING(hcd->state))
  696. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrenable);
  697. }
  698. if (quirk_zfmicro(ohci) && (ints & OHCI_INTR_SF)) {
  699. spin_lock(&ohci->lock);
  700. if (ohci->ed_to_check) {
  701. struct ed *ed = ohci->ed_to_check;
  702. if (check_ed(ohci, ed)) {
  703. /* HC thinks the TD list is empty; HCD knows
  704. * at least one TD is outstanding
  705. */
  706. if (--ohci->zf_delay == 0) {
  707. struct td *td = list_entry(
  708. ed->td_list.next,
  709. struct td, td_list);
  710. ohci_warn(ohci,
  711. "Reclaiming orphan TD %p\n",
  712. td);
  713. takeback_td(ohci, td);
  714. ohci->ed_to_check = NULL;
  715. }
  716. } else
  717. ohci->ed_to_check = NULL;
  718. }
  719. spin_unlock(&ohci->lock);
  720. }
  721. /* could track INTR_SO to reduce available PCI/... bandwidth */
  722. /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
  723. * when there's still unlinking to be done (next frame).
  724. */
  725. spin_lock (&ohci->lock);
  726. if (ohci->ed_rm_list)
  727. finish_unlinks (ohci, ohci_frame_no(ohci));
  728. if ((ints & OHCI_INTR_SF) != 0
  729. && !ohci->ed_rm_list
  730. && !ohci->ed_to_check
  731. && HC_IS_RUNNING(hcd->state))
  732. ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
  733. spin_unlock (&ohci->lock);
  734. if (HC_IS_RUNNING(hcd->state)) {
  735. ohci_writel (ohci, ints, &regs->intrstatus);
  736. ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
  737. // flush those writes
  738. (void) ohci_readl (ohci, &ohci->regs->control);
  739. }
  740. return IRQ_HANDLED;
  741. }
  742. /*-------------------------------------------------------------------------*/
  743. static void ohci_stop (struct usb_hcd *hcd)
  744. {
  745. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  746. ohci_dump (ohci, 1);
  747. flush_scheduled_work();
  748. ohci_usb_reset (ohci);
  749. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  750. free_irq(hcd->irq, hcd);
  751. hcd->irq = -1;
  752. if (quirk_zfmicro(ohci))
  753. del_timer(&ohci->unlink_watchdog);
  754. remove_debug_files (ohci);
  755. ohci_mem_cleanup (ohci);
  756. if (ohci->hcca) {
  757. dma_free_coherent (hcd->self.controller,
  758. sizeof *ohci->hcca,
  759. ohci->hcca, ohci->hcca_dma);
  760. ohci->hcca = NULL;
  761. ohci->hcca_dma = 0;
  762. }
  763. }
  764. /*-------------------------------------------------------------------------*/
  765. #if defined(CONFIG_PM) || defined(CONFIG_PCI)
  766. /* must not be called from interrupt context */
  767. static int ohci_restart (struct ohci_hcd *ohci)
  768. {
  769. int temp;
  770. int i;
  771. struct urb_priv *priv;
  772. spin_lock_irq(&ohci->lock);
  773. disable (ohci);
  774. /* Recycle any "live" eds/tds (and urbs). */
  775. if (!list_empty (&ohci->pending))
  776. ohci_dbg(ohci, "abort schedule...\n");
  777. list_for_each_entry (priv, &ohci->pending, pending) {
  778. struct urb *urb = priv->td[0]->urb;
  779. struct ed *ed = priv->ed;
  780. switch (ed->state) {
  781. case ED_OPER:
  782. ed->state = ED_UNLINK;
  783. ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
  784. ed_deschedule (ohci, ed);
  785. ed->ed_next = ohci->ed_rm_list;
  786. ed->ed_prev = NULL;
  787. ohci->ed_rm_list = ed;
  788. /* FALLTHROUGH */
  789. case ED_UNLINK:
  790. break;
  791. default:
  792. ohci_dbg(ohci, "bogus ed %p state %d\n",
  793. ed, ed->state);
  794. }
  795. if (!urb->unlinked)
  796. urb->unlinked = -ESHUTDOWN;
  797. }
  798. finish_unlinks (ohci, 0);
  799. spin_unlock_irq(&ohci->lock);
  800. /* paranoia, in case that didn't work: */
  801. /* empty the interrupt branches */
  802. for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
  803. for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
  804. /* no EDs to remove */
  805. ohci->ed_rm_list = NULL;
  806. /* empty control and bulk lists */
  807. ohci->ed_controltail = NULL;
  808. ohci->ed_bulktail = NULL;
  809. if ((temp = ohci_run (ohci)) < 0) {
  810. ohci_err (ohci, "can't restart, %d\n", temp);
  811. return temp;
  812. }
  813. ohci_dbg(ohci, "restart complete\n");
  814. return 0;
  815. }
  816. #endif
  817. /*-------------------------------------------------------------------------*/
  818. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  819. MODULE_AUTHOR (DRIVER_AUTHOR);
  820. MODULE_DESCRIPTION (DRIVER_INFO);
  821. MODULE_LICENSE ("GPL");
  822. #ifdef CONFIG_PCI
  823. #include "ohci-pci.c"
  824. #define PCI_DRIVER ohci_pci_driver
  825. #endif
  826. #ifdef CONFIG_SA1111
  827. #include "ohci-sa1111.c"
  828. #define SA1111_DRIVER ohci_hcd_sa1111_driver
  829. #endif
  830. #ifdef CONFIG_ARCH_S3C2410
  831. #include "ohci-s3c2410.c"
  832. #define PLATFORM_DRIVER ohci_hcd_s3c2410_driver
  833. #endif
  834. #ifdef CONFIG_ARCH_OMAP
  835. #include "ohci-omap.c"
  836. #define PLATFORM_DRIVER ohci_hcd_omap_driver
  837. #endif
  838. #ifdef CONFIG_ARCH_LH7A404
  839. #include "ohci-lh7a404.c"
  840. #define PLATFORM_DRIVER ohci_hcd_lh7a404_driver
  841. #endif
  842. #ifdef CONFIG_PXA27x
  843. #include "ohci-pxa27x.c"
  844. #define PLATFORM_DRIVER ohci_hcd_pxa27x_driver
  845. #endif
  846. #ifdef CONFIG_ARCH_EP93XX
  847. #include "ohci-ep93xx.c"
  848. #define PLATFORM_DRIVER ohci_hcd_ep93xx_driver
  849. #endif
  850. #ifdef CONFIG_SOC_AU1X00
  851. #include "ohci-au1xxx.c"
  852. #define PLATFORM_DRIVER ohci_hcd_au1xxx_driver
  853. #endif
  854. #ifdef CONFIG_PNX8550
  855. #include "ohci-pnx8550.c"
  856. #define PLATFORM_DRIVER ohci_hcd_pnx8550_driver
  857. #endif
  858. #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
  859. #include "ohci-ppc-soc.c"
  860. #define PLATFORM_DRIVER ohci_hcd_ppc_soc_driver
  861. #endif
  862. #ifdef CONFIG_ARCH_AT91
  863. #include "ohci-at91.c"
  864. #define PLATFORM_DRIVER ohci_hcd_at91_driver
  865. #endif
  866. #ifdef CONFIG_ARCH_PNX4008
  867. #include "ohci-pnx4008.c"
  868. #define PLATFORM_DRIVER usb_hcd_pnx4008_driver
  869. #endif
  870. #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
  871. #include "ohci-ppc-of.c"
  872. #define OF_PLATFORM_DRIVER ohci_hcd_ppc_of_driver
  873. #endif
  874. #ifdef CONFIG_PPC_PS3
  875. #include "ohci-ps3.c"
  876. #define PS3_SYSTEM_BUS_DRIVER ps3_ohci_driver
  877. #endif
  878. #ifdef CONFIG_USB_OHCI_HCD_SSB
  879. #include "ohci-ssb.c"
  880. #define SSB_OHCI_DRIVER ssb_ohci_driver
  881. #endif
  882. #if !defined(PCI_DRIVER) && \
  883. !defined(PLATFORM_DRIVER) && \
  884. !defined(OF_PLATFORM_DRIVER) && \
  885. !defined(SA1111_DRIVER) && \
  886. !defined(PS3_SYSTEM_BUS_DRIVER) && \
  887. !defined(SSB_OHCI_DRIVER)
  888. #error "missing bus glue for ohci-hcd"
  889. #endif
  890. static int __init ohci_hcd_mod_init(void)
  891. {
  892. int retval = 0;
  893. if (usb_disabled())
  894. return -ENODEV;
  895. printk (KERN_DEBUG "%s: " DRIVER_INFO "\n", hcd_name);
  896. pr_debug ("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
  897. sizeof (struct ed), sizeof (struct td));
  898. #ifdef PS3_SYSTEM_BUS_DRIVER
  899. retval = ps3_ohci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  900. if (retval < 0)
  901. goto error_ps3;
  902. #endif
  903. #ifdef PLATFORM_DRIVER
  904. retval = platform_driver_register(&PLATFORM_DRIVER);
  905. if (retval < 0)
  906. goto error_platform;
  907. #endif
  908. #ifdef OF_PLATFORM_DRIVER
  909. retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
  910. if (retval < 0)
  911. goto error_of_platform;
  912. #endif
  913. #ifdef SA1111_DRIVER
  914. retval = sa1111_driver_register(&SA1111_DRIVER);
  915. if (retval < 0)
  916. goto error_sa1111;
  917. #endif
  918. #ifdef PCI_DRIVER
  919. retval = pci_register_driver(&PCI_DRIVER);
  920. if (retval < 0)
  921. goto error_pci;
  922. #endif
  923. #ifdef SSB_OHCI_DRIVER
  924. retval = ssb_driver_register(&SSB_OHCI_DRIVER);
  925. if (retval)
  926. goto error_ssb;
  927. #endif
  928. return retval;
  929. /* Error path */
  930. #ifdef SSB_OHCI_DRIVER
  931. error_ssb:
  932. #endif
  933. #ifdef PCI_DRIVER
  934. pci_unregister_driver(&PCI_DRIVER);
  935. error_pci:
  936. #endif
  937. #ifdef SA1111_DRIVER
  938. sa1111_driver_unregister(&SA1111_DRIVER);
  939. error_sa1111:
  940. #endif
  941. #ifdef OF_PLATFORM_DRIVER
  942. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  943. error_of_platform:
  944. #endif
  945. #ifdef PLATFORM_DRIVER
  946. platform_driver_unregister(&PLATFORM_DRIVER);
  947. error_platform:
  948. #endif
  949. #ifdef PS3_SYSTEM_BUS_DRIVER
  950. ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  951. error_ps3:
  952. #endif
  953. return retval;
  954. }
  955. module_init(ohci_hcd_mod_init);
  956. static void __exit ohci_hcd_mod_exit(void)
  957. {
  958. #ifdef SSB_OHCI_DRIVER
  959. ssb_driver_unregister(&SSB_OHCI_DRIVER);
  960. #endif
  961. #ifdef PCI_DRIVER
  962. pci_unregister_driver(&PCI_DRIVER);
  963. #endif
  964. #ifdef SA1111_DRIVER
  965. sa1111_driver_unregister(&SA1111_DRIVER);
  966. #endif
  967. #ifdef OF_PLATFORM_DRIVER
  968. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  969. #endif
  970. #ifdef PLATFORM_DRIVER
  971. platform_driver_unregister(&PLATFORM_DRIVER);
  972. #endif
  973. #ifdef PS3_SYSTEM_BUS_DRIVER
  974. ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  975. #endif
  976. }
  977. module_exit(ohci_hcd_mod_exit);