ohci-hcd.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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. /* we can eliminate a (slow) ohci_readl()
  625. * if _only_ WDH caused this irq
  626. */
  627. if ((ohci->hcca->done_head != 0)
  628. && ! (hc32_to_cpup (ohci, &ohci->hcca->done_head)
  629. & 0x01)) {
  630. ints = OHCI_INTR_WDH;
  631. /* cardbus/... hardware gone before remove() */
  632. } else if ((ints = ohci_readl (ohci, &regs->intrstatus)) == ~(u32)0) {
  633. disable (ohci);
  634. ohci_dbg (ohci, "device removed!\n");
  635. return IRQ_HANDLED;
  636. /* interrupt for some other device? */
  637. } else if ((ints &= ohci_readl (ohci, &regs->intrenable)) == 0) {
  638. return IRQ_NOTMINE;
  639. }
  640. if (ints & OHCI_INTR_UE) {
  641. // e.g. due to PCI Master/Target Abort
  642. if (quirk_nec(ohci)) {
  643. /* Workaround for a silicon bug in some NEC chips used
  644. * in Apple's PowerBooks. Adapted from Darwin code.
  645. */
  646. ohci_err (ohci, "OHCI Unrecoverable Error, scheduling NEC chip restart\n");
  647. ohci_writel (ohci, OHCI_INTR_UE, &regs->intrdisable);
  648. schedule_work (&ohci->nec_work);
  649. } else {
  650. disable (ohci);
  651. ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
  652. }
  653. ohci_dump (ohci, 1);
  654. ohci_usb_reset (ohci);
  655. }
  656. if (ints & OHCI_INTR_RHSC) {
  657. ohci_vdbg(ohci, "rhsc\n");
  658. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  659. ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
  660. &regs->intrstatus);
  661. /* NOTE: Vendors didn't always make the same implementation
  662. * choices for RHSC. Many followed the spec; RHSC triggers
  663. * on an edge, like setting and maybe clearing a port status
  664. * change bit. With others it's level-triggered, active
  665. * until khubd clears all the port status change bits. We'll
  666. * always disable it here and rely on polling until khubd
  667. * re-enables it.
  668. */
  669. ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
  670. usb_hcd_poll_rh_status(hcd);
  671. }
  672. /* For connect and disconnect events, we expect the controller
  673. * to turn on RHSC along with RD. But for remote wakeup events
  674. * this might not happen.
  675. */
  676. else if (ints & OHCI_INTR_RD) {
  677. ohci_vdbg(ohci, "resume detect\n");
  678. ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
  679. hcd->poll_rh = 1;
  680. if (ohci->autostop) {
  681. spin_lock (&ohci->lock);
  682. ohci_rh_resume (ohci);
  683. spin_unlock (&ohci->lock);
  684. } else
  685. usb_hcd_resume_root_hub(hcd);
  686. }
  687. if (ints & OHCI_INTR_WDH) {
  688. if (HC_IS_RUNNING(hcd->state))
  689. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrdisable);
  690. spin_lock (&ohci->lock);
  691. dl_done_list (ohci);
  692. spin_unlock (&ohci->lock);
  693. if (HC_IS_RUNNING(hcd->state))
  694. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrenable);
  695. }
  696. if (quirk_zfmicro(ohci) && (ints & OHCI_INTR_SF)) {
  697. spin_lock(&ohci->lock);
  698. if (ohci->ed_to_check) {
  699. struct ed *ed = ohci->ed_to_check;
  700. if (check_ed(ohci, ed)) {
  701. /* HC thinks the TD list is empty; HCD knows
  702. * at least one TD is outstanding
  703. */
  704. if (--ohci->zf_delay == 0) {
  705. struct td *td = list_entry(
  706. ed->td_list.next,
  707. struct td, td_list);
  708. ohci_warn(ohci,
  709. "Reclaiming orphan TD %p\n",
  710. td);
  711. takeback_td(ohci, td);
  712. ohci->ed_to_check = NULL;
  713. }
  714. } else
  715. ohci->ed_to_check = NULL;
  716. }
  717. spin_unlock(&ohci->lock);
  718. }
  719. /* could track INTR_SO to reduce available PCI/... bandwidth */
  720. /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
  721. * when there's still unlinking to be done (next frame).
  722. */
  723. spin_lock (&ohci->lock);
  724. if (ohci->ed_rm_list)
  725. finish_unlinks (ohci, ohci_frame_no(ohci));
  726. if ((ints & OHCI_INTR_SF) != 0
  727. && !ohci->ed_rm_list
  728. && !ohci->ed_to_check
  729. && HC_IS_RUNNING(hcd->state))
  730. ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
  731. spin_unlock (&ohci->lock);
  732. if (HC_IS_RUNNING(hcd->state)) {
  733. ohci_writel (ohci, ints, &regs->intrstatus);
  734. ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
  735. // flush those writes
  736. (void) ohci_readl (ohci, &ohci->regs->control);
  737. }
  738. return IRQ_HANDLED;
  739. }
  740. /*-------------------------------------------------------------------------*/
  741. static void ohci_stop (struct usb_hcd *hcd)
  742. {
  743. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  744. ohci_dump (ohci, 1);
  745. flush_scheduled_work();
  746. ohci_usb_reset (ohci);
  747. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  748. free_irq(hcd->irq, hcd);
  749. hcd->irq = -1;
  750. if (quirk_zfmicro(ohci))
  751. del_timer(&ohci->unlink_watchdog);
  752. remove_debug_files (ohci);
  753. ohci_mem_cleanup (ohci);
  754. if (ohci->hcca) {
  755. dma_free_coherent (hcd->self.controller,
  756. sizeof *ohci->hcca,
  757. ohci->hcca, ohci->hcca_dma);
  758. ohci->hcca = NULL;
  759. ohci->hcca_dma = 0;
  760. }
  761. }
  762. /*-------------------------------------------------------------------------*/
  763. #if defined(CONFIG_PM) || defined(CONFIG_PCI)
  764. /* must not be called from interrupt context */
  765. static int ohci_restart (struct ohci_hcd *ohci)
  766. {
  767. int temp;
  768. int i;
  769. struct urb_priv *priv;
  770. spin_lock_irq(&ohci->lock);
  771. disable (ohci);
  772. /* Recycle any "live" eds/tds (and urbs). */
  773. if (!list_empty (&ohci->pending))
  774. ohci_dbg(ohci, "abort schedule...\n");
  775. list_for_each_entry (priv, &ohci->pending, pending) {
  776. struct urb *urb = priv->td[0]->urb;
  777. struct ed *ed = priv->ed;
  778. switch (ed->state) {
  779. case ED_OPER:
  780. ed->state = ED_UNLINK;
  781. ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
  782. ed_deschedule (ohci, ed);
  783. ed->ed_next = ohci->ed_rm_list;
  784. ed->ed_prev = NULL;
  785. ohci->ed_rm_list = ed;
  786. /* FALLTHROUGH */
  787. case ED_UNLINK:
  788. break;
  789. default:
  790. ohci_dbg(ohci, "bogus ed %p state %d\n",
  791. ed, ed->state);
  792. }
  793. if (!urb->unlinked)
  794. urb->unlinked = -ESHUTDOWN;
  795. }
  796. finish_unlinks (ohci, 0);
  797. spin_unlock_irq(&ohci->lock);
  798. /* paranoia, in case that didn't work: */
  799. /* empty the interrupt branches */
  800. for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
  801. for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
  802. /* no EDs to remove */
  803. ohci->ed_rm_list = NULL;
  804. /* empty control and bulk lists */
  805. ohci->ed_controltail = NULL;
  806. ohci->ed_bulktail = NULL;
  807. if ((temp = ohci_run (ohci)) < 0) {
  808. ohci_err (ohci, "can't restart, %d\n", temp);
  809. return temp;
  810. }
  811. ohci_dbg(ohci, "restart complete\n");
  812. return 0;
  813. }
  814. #endif
  815. /*-------------------------------------------------------------------------*/
  816. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  817. MODULE_AUTHOR (DRIVER_AUTHOR);
  818. MODULE_DESCRIPTION (DRIVER_INFO);
  819. MODULE_LICENSE ("GPL");
  820. #ifdef CONFIG_PCI
  821. #include "ohci-pci.c"
  822. #define PCI_DRIVER ohci_pci_driver
  823. #endif
  824. #ifdef CONFIG_SA1111
  825. #include "ohci-sa1111.c"
  826. #define SA1111_DRIVER ohci_hcd_sa1111_driver
  827. #endif
  828. #ifdef CONFIG_ARCH_S3C2410
  829. #include "ohci-s3c2410.c"
  830. #define PLATFORM_DRIVER ohci_hcd_s3c2410_driver
  831. #endif
  832. #ifdef CONFIG_ARCH_OMAP
  833. #include "ohci-omap.c"
  834. #define PLATFORM_DRIVER ohci_hcd_omap_driver
  835. #endif
  836. #ifdef CONFIG_ARCH_LH7A404
  837. #include "ohci-lh7a404.c"
  838. #define PLATFORM_DRIVER ohci_hcd_lh7a404_driver
  839. #endif
  840. #ifdef CONFIG_PXA27x
  841. #include "ohci-pxa27x.c"
  842. #define PLATFORM_DRIVER ohci_hcd_pxa27x_driver
  843. #endif
  844. #ifdef CONFIG_ARCH_EP93XX
  845. #include "ohci-ep93xx.c"
  846. #define PLATFORM_DRIVER ohci_hcd_ep93xx_driver
  847. #endif
  848. #ifdef CONFIG_SOC_AU1X00
  849. #include "ohci-au1xxx.c"
  850. #define PLATFORM_DRIVER ohci_hcd_au1xxx_driver
  851. #endif
  852. #ifdef CONFIG_PNX8550
  853. #include "ohci-pnx8550.c"
  854. #define PLATFORM_DRIVER ohci_hcd_pnx8550_driver
  855. #endif
  856. #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
  857. #include "ohci-ppc-soc.c"
  858. #define PLATFORM_DRIVER ohci_hcd_ppc_soc_driver
  859. #endif
  860. #ifdef CONFIG_ARCH_AT91
  861. #include "ohci-at91.c"
  862. #define PLATFORM_DRIVER ohci_hcd_at91_driver
  863. #endif
  864. #ifdef CONFIG_ARCH_PNX4008
  865. #include "ohci-pnx4008.c"
  866. #define PLATFORM_DRIVER usb_hcd_pnx4008_driver
  867. #endif
  868. #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
  869. #include "ohci-ppc-of.c"
  870. #define OF_PLATFORM_DRIVER ohci_hcd_ppc_of_driver
  871. #endif
  872. #ifdef CONFIG_PPC_PS3
  873. #include "ohci-ps3.c"
  874. #define PS3_SYSTEM_BUS_DRIVER ps3_ohci_driver
  875. #endif
  876. #ifdef CONFIG_USB_OHCI_HCD_SSB
  877. #include "ohci-ssb.c"
  878. #define SSB_OHCI_DRIVER ssb_ohci_driver
  879. #endif
  880. #if !defined(PCI_DRIVER) && \
  881. !defined(PLATFORM_DRIVER) && \
  882. !defined(OF_PLATFORM_DRIVER) && \
  883. !defined(SA1111_DRIVER) && \
  884. !defined(PS3_SYSTEM_BUS_DRIVER) && \
  885. !defined(SSB_OHCI_DRIVER)
  886. #error "missing bus glue for ohci-hcd"
  887. #endif
  888. static int __init ohci_hcd_mod_init(void)
  889. {
  890. int retval = 0;
  891. if (usb_disabled())
  892. return -ENODEV;
  893. printk (KERN_DEBUG "%s: " DRIVER_INFO "\n", hcd_name);
  894. pr_debug ("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
  895. sizeof (struct ed), sizeof (struct td));
  896. #ifdef PS3_SYSTEM_BUS_DRIVER
  897. retval = ps3_ohci_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  898. if (retval < 0)
  899. goto error_ps3;
  900. #endif
  901. #ifdef PLATFORM_DRIVER
  902. retval = platform_driver_register(&PLATFORM_DRIVER);
  903. if (retval < 0)
  904. goto error_platform;
  905. #endif
  906. #ifdef OF_PLATFORM_DRIVER
  907. retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
  908. if (retval < 0)
  909. goto error_of_platform;
  910. #endif
  911. #ifdef SA1111_DRIVER
  912. retval = sa1111_driver_register(&SA1111_DRIVER);
  913. if (retval < 0)
  914. goto error_sa1111;
  915. #endif
  916. #ifdef PCI_DRIVER
  917. retval = pci_register_driver(&PCI_DRIVER);
  918. if (retval < 0)
  919. goto error_pci;
  920. #endif
  921. #ifdef SSB_OHCI_DRIVER
  922. retval = ssb_driver_register(&SSB_OHCI_DRIVER);
  923. if (retval)
  924. goto error_ssb;
  925. #endif
  926. return retval;
  927. /* Error path */
  928. #ifdef SSB_OHCI_DRIVER
  929. error_ssb:
  930. #endif
  931. #ifdef PCI_DRIVER
  932. pci_unregister_driver(&PCI_DRIVER);
  933. error_pci:
  934. #endif
  935. #ifdef SA1111_DRIVER
  936. sa1111_driver_unregister(&SA1111_DRIVER);
  937. error_sa1111:
  938. #endif
  939. #ifdef OF_PLATFORM_DRIVER
  940. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  941. error_of_platform:
  942. #endif
  943. #ifdef PLATFORM_DRIVER
  944. platform_driver_unregister(&PLATFORM_DRIVER);
  945. error_platform:
  946. #endif
  947. #ifdef PS3_SYSTEM_BUS_DRIVER
  948. ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  949. error_ps3:
  950. #endif
  951. return retval;
  952. }
  953. module_init(ohci_hcd_mod_init);
  954. static void __exit ohci_hcd_mod_exit(void)
  955. {
  956. #ifdef SSB_OHCI_DRIVER
  957. ssb_driver_unregister(&SSB_OHCI_DRIVER);
  958. #endif
  959. #ifdef PCI_DRIVER
  960. pci_unregister_driver(&PCI_DRIVER);
  961. #endif
  962. #ifdef SA1111_DRIVER
  963. sa1111_driver_unregister(&SA1111_DRIVER);
  964. #endif
  965. #ifdef OF_PLATFORM_DRIVER
  966. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  967. #endif
  968. #ifdef PLATFORM_DRIVER
  969. platform_driver_unregister(&PLATFORM_DRIVER);
  970. #endif
  971. #ifdef PS3_SYSTEM_BUS_DRIVER
  972. ps3_ohci_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  973. #endif
  974. }
  975. module_exit(ohci_hcd_mod_exit);