ohci-hcd.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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/smp_lock.h>
  29. #include <linux/errno.h>
  30. #include <linux/init.h>
  31. #include <linux/timer.h>
  32. #include <linux/list.h>
  33. #include <linux/usb.h>
  34. #include <linux/usb/otg.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/dmapool.h>
  37. #include <linux/reboot.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. #include "ohci-hub.c"
  70. #include "ohci-dbg.c"
  71. #include "ohci-mem.c"
  72. #include "ohci-q.c"
  73. /*
  74. * On architectures with edge-triggered interrupts we must never return
  75. * IRQ_NONE.
  76. */
  77. #if defined(CONFIG_SA1111) /* ... or other edge-triggered systems */
  78. #define IRQ_NOTMINE IRQ_HANDLED
  79. #else
  80. #define IRQ_NOTMINE IRQ_NONE
  81. #endif
  82. /* Some boards misreport power switching/overcurrent */
  83. static int distrust_firmware = 1;
  84. module_param (distrust_firmware, bool, 0);
  85. MODULE_PARM_DESC (distrust_firmware,
  86. "true to distrust firmware power/overcurrent setup");
  87. /* Some boards leave IR set wrongly, since they fail BIOS/SMM handshakes */
  88. static int no_handshake = 0;
  89. module_param (no_handshake, bool, 0);
  90. MODULE_PARM_DESC (no_handshake, "true (not default) disables BIOS handshake");
  91. /*-------------------------------------------------------------------------*/
  92. /*
  93. * queue up an urb for anything except the root hub
  94. */
  95. static int ohci_urb_enqueue (
  96. struct usb_hcd *hcd,
  97. struct usb_host_endpoint *ep,
  98. struct urb *urb,
  99. gfp_t mem_flags
  100. ) {
  101. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  102. struct ed *ed;
  103. urb_priv_t *urb_priv;
  104. unsigned int pipe = urb->pipe;
  105. int i, size = 0;
  106. unsigned long flags;
  107. int retval = 0;
  108. #ifdef OHCI_VERBOSE_DEBUG
  109. urb_print (urb, "SUB", usb_pipein (pipe));
  110. #endif
  111. /* every endpoint has a ed, locate and maybe (re)initialize it */
  112. if (! (ed = ed_get (ohci, ep, urb->dev, pipe, urb->interval)))
  113. return -ENOMEM;
  114. /* for the private part of the URB we need the number of TDs (size) */
  115. switch (ed->type) {
  116. case PIPE_CONTROL:
  117. /* td_submit_urb() doesn't yet handle these */
  118. if (urb->transfer_buffer_length > 4096)
  119. return -EMSGSIZE;
  120. /* 1 TD for setup, 1 for ACK, plus ... */
  121. size = 2;
  122. /* FALLTHROUGH */
  123. // case PIPE_INTERRUPT:
  124. // case PIPE_BULK:
  125. default:
  126. /* one TD for every 4096 Bytes (can be upto 8K) */
  127. size += urb->transfer_buffer_length / 4096;
  128. /* ... and for any remaining bytes ... */
  129. if ((urb->transfer_buffer_length % 4096) != 0)
  130. size++;
  131. /* ... and maybe a zero length packet to wrap it up */
  132. if (size == 0)
  133. size++;
  134. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  135. && (urb->transfer_buffer_length
  136. % usb_maxpacket (urb->dev, pipe,
  137. usb_pipeout (pipe))) == 0)
  138. size++;
  139. break;
  140. case PIPE_ISOCHRONOUS: /* number of packets from URB */
  141. size = urb->number_of_packets;
  142. break;
  143. }
  144. /* allocate the private part of the URB */
  145. urb_priv = kmalloc (sizeof (urb_priv_t) + size * sizeof (struct td *),
  146. mem_flags);
  147. if (!urb_priv)
  148. return -ENOMEM;
  149. memset (urb_priv, 0, sizeof (urb_priv_t) + size * sizeof (struct td *));
  150. INIT_LIST_HEAD (&urb_priv->pending);
  151. urb_priv->length = size;
  152. urb_priv->ed = ed;
  153. /* allocate the TDs (deferring hash chain updates) */
  154. for (i = 0; i < size; i++) {
  155. urb_priv->td [i] = td_alloc (ohci, mem_flags);
  156. if (!urb_priv->td [i]) {
  157. urb_priv->length = i;
  158. urb_free_priv (ohci, urb_priv);
  159. return -ENOMEM;
  160. }
  161. }
  162. spin_lock_irqsave (&ohci->lock, flags);
  163. /* don't submit to a dead HC */
  164. if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
  165. retval = -ENODEV;
  166. goto fail;
  167. }
  168. if (!HC_IS_RUNNING(hcd->state)) {
  169. retval = -ENODEV;
  170. goto fail;
  171. }
  172. /* in case of unlink-during-submit */
  173. spin_lock (&urb->lock);
  174. if (urb->status != -EINPROGRESS) {
  175. spin_unlock (&urb->lock);
  176. urb->hcpriv = urb_priv;
  177. finish_urb (ohci, urb);
  178. retval = 0;
  179. goto fail;
  180. }
  181. /* schedule the ed if needed */
  182. if (ed->state == ED_IDLE) {
  183. retval = ed_schedule (ohci, ed);
  184. if (retval < 0)
  185. goto fail0;
  186. if (ed->type == PIPE_ISOCHRONOUS) {
  187. u16 frame = ohci_frame_no(ohci);
  188. /* delay a few frames before the first TD */
  189. frame += max_t (u16, 8, ed->interval);
  190. frame &= ~(ed->interval - 1);
  191. frame |= ed->branch;
  192. urb->start_frame = frame;
  193. /* yes, only URB_ISO_ASAP is supported, and
  194. * urb->start_frame is never used as input.
  195. */
  196. }
  197. } else if (ed->type == PIPE_ISOCHRONOUS)
  198. urb->start_frame = ed->last_iso + ed->interval;
  199. /* fill the TDs and link them to the ed; and
  200. * enable that part of the schedule, if needed
  201. * and update count of queued periodic urbs
  202. */
  203. urb->hcpriv = urb_priv;
  204. td_submit_urb (ohci, urb);
  205. fail0:
  206. spin_unlock (&urb->lock);
  207. fail:
  208. if (retval)
  209. urb_free_priv (ohci, urb_priv);
  210. spin_unlock_irqrestore (&ohci->lock, flags);
  211. return retval;
  212. }
  213. /*
  214. * decouple the URB from the HC queues (TDs, urb_priv); it's
  215. * already marked using urb->status. reporting is always done
  216. * asynchronously, and we might be dealing with an urb that's
  217. * partially transferred, or an ED with other urbs being unlinked.
  218. */
  219. static int ohci_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
  220. {
  221. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  222. unsigned long flags;
  223. #ifdef OHCI_VERBOSE_DEBUG
  224. urb_print (urb, "UNLINK", 1);
  225. #endif
  226. spin_lock_irqsave (&ohci->lock, flags);
  227. if (HC_IS_RUNNING(hcd->state)) {
  228. urb_priv_t *urb_priv;
  229. /* Unless an IRQ completed the unlink while it was being
  230. * handed to us, flag it for unlink and giveback, and force
  231. * some upcoming INTR_SF to call finish_unlinks()
  232. */
  233. urb_priv = urb->hcpriv;
  234. if (urb_priv) {
  235. if (urb_priv->ed->state == ED_OPER)
  236. start_ed_unlink (ohci, urb_priv->ed);
  237. }
  238. } else {
  239. /*
  240. * with HC dead, we won't respect hc queue pointers
  241. * any more ... just clean up every urb's memory.
  242. */
  243. if (urb->hcpriv)
  244. finish_urb (ohci, urb);
  245. }
  246. spin_unlock_irqrestore (&ohci->lock, flags);
  247. return 0;
  248. }
  249. /*-------------------------------------------------------------------------*/
  250. /* frees config/altsetting state for endpoints,
  251. * including ED memory, dummy TD, and bulk/intr data toggle
  252. */
  253. static void
  254. ohci_endpoint_disable (struct usb_hcd *hcd, struct usb_host_endpoint *ep)
  255. {
  256. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  257. unsigned long flags;
  258. struct ed *ed = ep->hcpriv;
  259. unsigned limit = 1000;
  260. /* ASSERT: any requests/urbs are being unlinked */
  261. /* ASSERT: nobody can be submitting urbs for this any more */
  262. if (!ed)
  263. return;
  264. rescan:
  265. spin_lock_irqsave (&ohci->lock, flags);
  266. if (!HC_IS_RUNNING (hcd->state)) {
  267. sanitize:
  268. ed->state = ED_IDLE;
  269. finish_unlinks (ohci, 0);
  270. }
  271. switch (ed->state) {
  272. case ED_UNLINK: /* wait for hw to finish? */
  273. /* major IRQ delivery trouble loses INTR_SF too... */
  274. if (limit-- == 0) {
  275. ohci_warn (ohci, "IRQ INTR_SF lossage\n");
  276. goto sanitize;
  277. }
  278. spin_unlock_irqrestore (&ohci->lock, flags);
  279. schedule_timeout_uninterruptible(1);
  280. goto rescan;
  281. case ED_IDLE: /* fully unlinked */
  282. if (list_empty (&ed->td_list)) {
  283. td_free (ohci, ed->dummy);
  284. ed_free (ohci, ed);
  285. break;
  286. }
  287. /* else FALL THROUGH */
  288. default:
  289. /* caller was supposed to have unlinked any requests;
  290. * that's not our job. can't recover; must leak ed.
  291. */
  292. ohci_err (ohci, "leak ed %p (#%02x) state %d%s\n",
  293. ed, ep->desc.bEndpointAddress, ed->state,
  294. list_empty (&ed->td_list) ? "" : " (has tds)");
  295. td_free (ohci, ed->dummy);
  296. break;
  297. }
  298. ep->hcpriv = NULL;
  299. spin_unlock_irqrestore (&ohci->lock, flags);
  300. return;
  301. }
  302. static int ohci_get_frame (struct usb_hcd *hcd)
  303. {
  304. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  305. return ohci_frame_no(ohci);
  306. }
  307. static void ohci_usb_reset (struct ohci_hcd *ohci)
  308. {
  309. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  310. ohci->hc_control &= OHCI_CTRL_RWC;
  311. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  312. }
  313. /* ohci_shutdown forcibly disables IRQs and DMA, helping kexec and
  314. * other cases where the next software may expect clean state from the
  315. * "firmware". this is bus-neutral, unlike shutdown() methods.
  316. */
  317. static void
  318. ohci_shutdown (struct usb_hcd *hcd)
  319. {
  320. struct ohci_hcd *ohci;
  321. ohci = hcd_to_ohci (hcd);
  322. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  323. ohci_usb_reset (ohci);
  324. /* flush the writes */
  325. (void) ohci_readl (ohci, &ohci->regs->control);
  326. }
  327. /*-------------------------------------------------------------------------*
  328. * HC functions
  329. *-------------------------------------------------------------------------*/
  330. /* init memory, and kick BIOS/SMM off */
  331. static int ohci_init (struct ohci_hcd *ohci)
  332. {
  333. int ret;
  334. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  335. disable (ohci);
  336. ohci->regs = hcd->regs;
  337. /* REVISIT this BIOS handshake is now moved into PCI "quirks", and
  338. * was never needed for most non-PCI systems ... remove the code?
  339. */
  340. #ifndef IR_DISABLE
  341. /* SMM owns the HC? not for long! */
  342. if (!no_handshake && ohci_readl (ohci,
  343. &ohci->regs->control) & OHCI_CTRL_IR) {
  344. u32 temp;
  345. ohci_dbg (ohci, "USB HC TakeOver from BIOS/SMM\n");
  346. /* this timeout is arbitrary. we make it long, so systems
  347. * depending on usb keyboards may be usable even if the
  348. * BIOS/SMM code seems pretty broken.
  349. */
  350. temp = 500; /* arbitrary: five seconds */
  351. ohci_writel (ohci, OHCI_INTR_OC, &ohci->regs->intrenable);
  352. ohci_writel (ohci, OHCI_OCR, &ohci->regs->cmdstatus);
  353. while (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_IR) {
  354. msleep (10);
  355. if (--temp == 0) {
  356. ohci_err (ohci, "USB HC takeover failed!"
  357. " (BIOS/SMM bug)\n");
  358. return -EBUSY;
  359. }
  360. }
  361. ohci_usb_reset (ohci);
  362. }
  363. #endif
  364. /* Disable HC interrupts */
  365. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  366. /* flush the writes, and save key bits like RWC */
  367. if (ohci_readl (ohci, &ohci->regs->control) & OHCI_CTRL_RWC)
  368. ohci->hc_control |= OHCI_CTRL_RWC;
  369. /* Read the number of ports unless overridden */
  370. if (ohci->num_ports == 0)
  371. ohci->num_ports = roothub_a(ohci) & RH_A_NDP;
  372. if (ohci->hcca)
  373. return 0;
  374. ohci->hcca = dma_alloc_coherent (hcd->self.controller,
  375. sizeof *ohci->hcca, &ohci->hcca_dma, 0);
  376. if (!ohci->hcca)
  377. return -ENOMEM;
  378. if ((ret = ohci_mem_init (ohci)) < 0)
  379. ohci_stop (hcd);
  380. else {
  381. create_debug_files (ohci);
  382. }
  383. return ret;
  384. }
  385. /*-------------------------------------------------------------------------*/
  386. /* Start an OHCI controller, set the BUS operational
  387. * resets USB and controller
  388. * enable interrupts
  389. */
  390. static int ohci_run (struct ohci_hcd *ohci)
  391. {
  392. u32 mask, temp;
  393. int first = ohci->fminterval == 0;
  394. struct usb_hcd *hcd = ohci_to_hcd(ohci);
  395. disable (ohci);
  396. /* boot firmware should have set this up (5.1.1.3.1) */
  397. if (first) {
  398. temp = ohci_readl (ohci, &ohci->regs->fminterval);
  399. ohci->fminterval = temp & 0x3fff;
  400. if (ohci->fminterval != FI)
  401. ohci_dbg (ohci, "fminterval delta %d\n",
  402. ohci->fminterval - FI);
  403. ohci->fminterval |= FSMP (ohci->fminterval) << 16;
  404. /* also: power/overcurrent flags in roothub.a */
  405. }
  406. /* Reset USB nearly "by the book". RemoteWakeupConnected was
  407. * saved if boot firmware (BIOS/SMM/...) told us it's connected,
  408. * or if bus glue did the same (e.g. for PCI add-in cards with
  409. * PCI PM support).
  410. */
  411. ohci_dbg (ohci, "resetting from state '%s', control = 0x%x\n",
  412. hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
  413. ohci_readl (ohci, &ohci->regs->control));
  414. if ((ohci->hc_control & OHCI_CTRL_RWC) != 0
  415. && !device_may_wakeup(hcd->self.controller))
  416. device_init_wakeup(hcd->self.controller, 1);
  417. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  418. case OHCI_USB_OPER:
  419. temp = 0;
  420. break;
  421. case OHCI_USB_SUSPEND:
  422. case OHCI_USB_RESUME:
  423. ohci->hc_control &= OHCI_CTRL_RWC;
  424. ohci->hc_control |= OHCI_USB_RESUME;
  425. temp = 10 /* msec wait */;
  426. break;
  427. // case OHCI_USB_RESET:
  428. default:
  429. ohci->hc_control &= OHCI_CTRL_RWC;
  430. ohci->hc_control |= OHCI_USB_RESET;
  431. temp = 50 /* msec wait */;
  432. break;
  433. }
  434. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  435. // flush the writes
  436. (void) ohci_readl (ohci, &ohci->regs->control);
  437. msleep(temp);
  438. temp = roothub_a (ohci);
  439. if (!(temp & RH_A_NPS)) {
  440. /* power down each port */
  441. for (temp = 0; temp < ohci->num_ports; temp++)
  442. ohci_writel (ohci, RH_PS_LSDA,
  443. &ohci->regs->roothub.portstatus [temp]);
  444. }
  445. // flush those writes
  446. (void) ohci_readl (ohci, &ohci->regs->control);
  447. memset (ohci->hcca, 0, sizeof (struct ohci_hcca));
  448. /* 2msec timelimit here means no irqs/preempt */
  449. spin_lock_irq (&ohci->lock);
  450. retry:
  451. /* HC Reset requires max 10 us delay */
  452. ohci_writel (ohci, OHCI_HCR, &ohci->regs->cmdstatus);
  453. temp = 30; /* ... allow extra time */
  454. while ((ohci_readl (ohci, &ohci->regs->cmdstatus) & OHCI_HCR) != 0) {
  455. if (--temp == 0) {
  456. spin_unlock_irq (&ohci->lock);
  457. ohci_err (ohci, "USB HC reset timed out!\n");
  458. return -1;
  459. }
  460. udelay (1);
  461. }
  462. /* now we're in the SUSPEND state ... must go OPERATIONAL
  463. * within 2msec else HC enters RESUME
  464. *
  465. * ... but some hardware won't init fmInterval "by the book"
  466. * (SiS, OPTi ...), so reset again instead. SiS doesn't need
  467. * this if we write fmInterval after we're OPERATIONAL.
  468. * Unclear about ALi, ServerWorks, and others ... this could
  469. * easily be a longstanding bug in chip init on Linux.
  470. */
  471. if (ohci->flags & OHCI_QUIRK_INITRESET) {
  472. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  473. // flush those writes
  474. (void) ohci_readl (ohci, &ohci->regs->control);
  475. }
  476. /* Tell the controller where the control and bulk lists are
  477. * The lists are empty now. */
  478. ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
  479. ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
  480. /* a reset clears this */
  481. ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
  482. periodic_reinit (ohci);
  483. /* some OHCI implementations are finicky about how they init.
  484. * bogus values here mean not even enumeration could work.
  485. */
  486. if ((ohci_readl (ohci, &ohci->regs->fminterval) & 0x3fff0000) == 0
  487. || !ohci_readl (ohci, &ohci->regs->periodicstart)) {
  488. if (!(ohci->flags & OHCI_QUIRK_INITRESET)) {
  489. ohci->flags |= OHCI_QUIRK_INITRESET;
  490. ohci_dbg (ohci, "enabling initreset quirk\n");
  491. goto retry;
  492. }
  493. spin_unlock_irq (&ohci->lock);
  494. ohci_err (ohci, "init err (%08x %04x)\n",
  495. ohci_readl (ohci, &ohci->regs->fminterval),
  496. ohci_readl (ohci, &ohci->regs->periodicstart));
  497. return -EOVERFLOW;
  498. }
  499. /* use rhsc irqs after khubd is fully initialized */
  500. hcd->poll_rh = 1;
  501. hcd->uses_new_polling = 1;
  502. /* start controller operations */
  503. ohci->hc_control &= OHCI_CTRL_RWC;
  504. ohci->hc_control |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
  505. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  506. hcd->state = HC_STATE_RUNNING;
  507. /* wake on ConnectStatusChange, matching external hubs */
  508. ohci_writel (ohci, RH_HS_DRWE, &ohci->regs->roothub.status);
  509. /* Choose the interrupts we care about now, others later on demand */
  510. mask = OHCI_INTR_INIT;
  511. ohci_writel (ohci, ~0, &ohci->regs->intrstatus);
  512. ohci_writel (ohci, mask, &ohci->regs->intrenable);
  513. /* handle root hub init quirks ... */
  514. temp = roothub_a (ohci);
  515. temp &= ~(RH_A_PSM | RH_A_OCPM);
  516. if (ohci->flags & OHCI_QUIRK_SUPERIO) {
  517. /* NSC 87560 and maybe others */
  518. temp |= RH_A_NOCP;
  519. temp &= ~(RH_A_POTPGT | RH_A_NPS);
  520. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  521. } else if ((ohci->flags & OHCI_QUIRK_AMD756) || distrust_firmware) {
  522. /* hub power always on; required for AMD-756 and some
  523. * Mac platforms. ganged overcurrent reporting, if any.
  524. */
  525. temp |= RH_A_NPS;
  526. ohci_writel (ohci, temp, &ohci->regs->roothub.a);
  527. }
  528. ohci_writel (ohci, RH_HS_LPSC, &ohci->regs->roothub.status);
  529. ohci_writel (ohci, (temp & RH_A_NPS) ? 0 : RH_B_PPCM,
  530. &ohci->regs->roothub.b);
  531. // flush those writes
  532. (void) ohci_readl (ohci, &ohci->regs->control);
  533. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  534. spin_unlock_irq (&ohci->lock);
  535. // POTPGT delay is bits 24-31, in 2 ms units.
  536. mdelay ((temp >> 23) & 0x1fe);
  537. hcd->state = HC_STATE_RUNNING;
  538. ohci_dump (ohci, 1);
  539. return 0;
  540. }
  541. /*-------------------------------------------------------------------------*/
  542. /* an interrupt happens */
  543. static irqreturn_t ohci_irq (struct usb_hcd *hcd)
  544. {
  545. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  546. struct ohci_regs __iomem *regs = ohci->regs;
  547. int ints;
  548. /* we can eliminate a (slow) ohci_readl()
  549. if _only_ WDH caused this irq */
  550. if ((ohci->hcca->done_head != 0)
  551. && ! (hc32_to_cpup (ohci, &ohci->hcca->done_head)
  552. & 0x01)) {
  553. ints = OHCI_INTR_WDH;
  554. /* cardbus/... hardware gone before remove() */
  555. } else if ((ints = ohci_readl (ohci, &regs->intrstatus)) == ~(u32)0) {
  556. disable (ohci);
  557. ohci_dbg (ohci, "device removed!\n");
  558. return IRQ_HANDLED;
  559. /* interrupt for some other device? */
  560. } else if ((ints &= ohci_readl (ohci, &regs->intrenable)) == 0) {
  561. return IRQ_NOTMINE;
  562. }
  563. if (ints & OHCI_INTR_UE) {
  564. disable (ohci);
  565. ohci_err (ohci, "OHCI Unrecoverable Error, disabled\n");
  566. // e.g. due to PCI Master/Target Abort
  567. ohci_dump (ohci, 1);
  568. ohci_usb_reset (ohci);
  569. }
  570. if (ints & OHCI_INTR_RHSC) {
  571. ohci_vdbg(ohci, "rhsc\n");
  572. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  573. ohci_writel(ohci, OHCI_INTR_RD | OHCI_INTR_RHSC,
  574. &regs->intrstatus);
  575. /* NOTE: Vendors didn't always make the same implementation
  576. * choices for RHSC. Many followed the spec; RHSC triggers
  577. * on an edge, like setting and maybe clearing a port status
  578. * change bit. With others it's level-triggered, active
  579. * until khubd clears all the port status change bits. We'll
  580. * always disable it here and rely on polling until khubd
  581. * re-enables it.
  582. */
  583. ohci_writel(ohci, OHCI_INTR_RHSC, &regs->intrdisable);
  584. usb_hcd_poll_rh_status(hcd);
  585. }
  586. /* For connect and disconnect events, we expect the controller
  587. * to turn on RHSC along with RD. But for remote wakeup events
  588. * this might not happen.
  589. */
  590. else if (ints & OHCI_INTR_RD) {
  591. ohci_vdbg(ohci, "resume detect\n");
  592. ohci_writel(ohci, OHCI_INTR_RD, &regs->intrstatus);
  593. hcd->poll_rh = 1;
  594. if (ohci->autostop) {
  595. spin_lock (&ohci->lock);
  596. ohci_rh_resume (ohci);
  597. spin_unlock (&ohci->lock);
  598. } else
  599. usb_hcd_resume_root_hub(hcd);
  600. }
  601. if (ints & OHCI_INTR_WDH) {
  602. if (HC_IS_RUNNING(hcd->state))
  603. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrdisable);
  604. spin_lock (&ohci->lock);
  605. dl_done_list (ohci);
  606. spin_unlock (&ohci->lock);
  607. if (HC_IS_RUNNING(hcd->state))
  608. ohci_writel (ohci, OHCI_INTR_WDH, &regs->intrenable);
  609. }
  610. /* could track INTR_SO to reduce available PCI/... bandwidth */
  611. /* handle any pending URB/ED unlinks, leaving INTR_SF enabled
  612. * when there's still unlinking to be done (next frame).
  613. */
  614. spin_lock (&ohci->lock);
  615. if (ohci->ed_rm_list)
  616. finish_unlinks (ohci, ohci_frame_no(ohci));
  617. if ((ints & OHCI_INTR_SF) != 0 && !ohci->ed_rm_list
  618. && HC_IS_RUNNING(hcd->state))
  619. ohci_writel (ohci, OHCI_INTR_SF, &regs->intrdisable);
  620. spin_unlock (&ohci->lock);
  621. if (HC_IS_RUNNING(hcd->state)) {
  622. ohci_writel (ohci, ints, &regs->intrstatus);
  623. ohci_writel (ohci, OHCI_INTR_MIE, &regs->intrenable);
  624. // flush those writes
  625. (void) ohci_readl (ohci, &ohci->regs->control);
  626. }
  627. return IRQ_HANDLED;
  628. }
  629. /*-------------------------------------------------------------------------*/
  630. static void ohci_stop (struct usb_hcd *hcd)
  631. {
  632. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  633. ohci_dbg (ohci, "stop %s controller (state 0x%02x)\n",
  634. hcfs2string (ohci->hc_control & OHCI_CTRL_HCFS),
  635. hcd->state);
  636. ohci_dump (ohci, 1);
  637. flush_scheduled_work();
  638. ohci_usb_reset (ohci);
  639. ohci_writel (ohci, OHCI_INTR_MIE, &ohci->regs->intrdisable);
  640. free_irq(hcd->irq, hcd);
  641. hcd->irq = -1;
  642. remove_debug_files (ohci);
  643. ohci_mem_cleanup (ohci);
  644. if (ohci->hcca) {
  645. dma_free_coherent (hcd->self.controller,
  646. sizeof *ohci->hcca,
  647. ohci->hcca, ohci->hcca_dma);
  648. ohci->hcca = NULL;
  649. ohci->hcca_dma = 0;
  650. }
  651. }
  652. /*-------------------------------------------------------------------------*/
  653. /* must not be called from interrupt context */
  654. #ifdef CONFIG_PM
  655. static int ohci_restart (struct ohci_hcd *ohci)
  656. {
  657. int temp;
  658. int i;
  659. struct urb_priv *priv;
  660. /* mark any devices gone, so they do nothing till khubd disconnects.
  661. * recycle any "live" eds/tds (and urbs) right away.
  662. * later, khubd disconnect processing will recycle the other state,
  663. * (either as disconnect/reconnect, or maybe someday as a reset).
  664. */
  665. spin_lock_irq(&ohci->lock);
  666. disable (ohci);
  667. usb_root_hub_lost_power(ohci_to_hcd(ohci)->self.root_hub);
  668. if (!list_empty (&ohci->pending))
  669. ohci_dbg(ohci, "abort schedule...\n");
  670. list_for_each_entry (priv, &ohci->pending, pending) {
  671. struct urb *urb = priv->td[0]->urb;
  672. struct ed *ed = priv->ed;
  673. switch (ed->state) {
  674. case ED_OPER:
  675. ed->state = ED_UNLINK;
  676. ed->hwINFO |= cpu_to_hc32(ohci, ED_DEQUEUE);
  677. ed_deschedule (ohci, ed);
  678. ed->ed_next = ohci->ed_rm_list;
  679. ed->ed_prev = NULL;
  680. ohci->ed_rm_list = ed;
  681. /* FALLTHROUGH */
  682. case ED_UNLINK:
  683. break;
  684. default:
  685. ohci_dbg(ohci, "bogus ed %p state %d\n",
  686. ed, ed->state);
  687. }
  688. spin_lock (&urb->lock);
  689. urb->status = -ESHUTDOWN;
  690. spin_unlock (&urb->lock);
  691. }
  692. finish_unlinks (ohci, 0);
  693. spin_unlock_irq(&ohci->lock);
  694. /* paranoia, in case that didn't work: */
  695. /* empty the interrupt branches */
  696. for (i = 0; i < NUM_INTS; i++) ohci->load [i] = 0;
  697. for (i = 0; i < NUM_INTS; i++) ohci->hcca->int_table [i] = 0;
  698. /* no EDs to remove */
  699. ohci->ed_rm_list = NULL;
  700. /* empty control and bulk lists */
  701. ohci->ed_controltail = NULL;
  702. ohci->ed_bulktail = NULL;
  703. if ((temp = ohci_run (ohci)) < 0) {
  704. ohci_err (ohci, "can't restart, %d\n", temp);
  705. return temp;
  706. } else {
  707. /* here we "know" root ports should always stay powered,
  708. * and that if we try to turn them back on the root hub
  709. * will respond to CSC processing.
  710. */
  711. i = ohci->num_ports;
  712. while (i--)
  713. ohci_writel (ohci, RH_PS_PSS,
  714. &ohci->regs->roothub.portstatus [i]);
  715. ohci_dbg (ohci, "restart complete\n");
  716. }
  717. return 0;
  718. }
  719. #endif
  720. /*-------------------------------------------------------------------------*/
  721. #define DRIVER_INFO DRIVER_VERSION " " DRIVER_DESC
  722. MODULE_AUTHOR (DRIVER_AUTHOR);
  723. MODULE_DESCRIPTION (DRIVER_INFO);
  724. MODULE_LICENSE ("GPL");
  725. #ifdef CONFIG_PCI
  726. #include "ohci-pci.c"
  727. #define PCI_DRIVER ohci_pci_driver
  728. #endif
  729. #ifdef CONFIG_SA1111
  730. #include "ohci-sa1111.c"
  731. #define SA1111_DRIVER ohci_hcd_sa1111_driver
  732. #endif
  733. #ifdef CONFIG_ARCH_S3C2410
  734. #include "ohci-s3c2410.c"
  735. #define PLATFORM_DRIVER ohci_hcd_s3c2410_driver
  736. #endif
  737. #ifdef CONFIG_ARCH_OMAP
  738. #include "ohci-omap.c"
  739. #define PLATFORM_DRIVER ohci_hcd_omap_driver
  740. #endif
  741. #ifdef CONFIG_ARCH_LH7A404
  742. #include "ohci-lh7a404.c"
  743. #define PLATFORM_DRIVER ohci_hcd_lh7a404_driver
  744. #endif
  745. #ifdef CONFIG_PXA27x
  746. #include "ohci-pxa27x.c"
  747. #define PLATFORM_DRIVER ohci_hcd_pxa27x_driver
  748. #endif
  749. #ifdef CONFIG_ARCH_EP93XX
  750. #include "ohci-ep93xx.c"
  751. #define PLATFORM_DRIVER ohci_hcd_ep93xx_driver
  752. #endif
  753. #ifdef CONFIG_SOC_AU1X00
  754. #include "ohci-au1xxx.c"
  755. #define PLATFORM_DRIVER ohci_hcd_au1xxx_driver
  756. #endif
  757. #ifdef CONFIG_PNX8550
  758. #include "ohci-pnx8550.c"
  759. #define PLATFORM_DRIVER ohci_hcd_pnx8550_driver
  760. #endif
  761. #ifdef CONFIG_USB_OHCI_HCD_PPC_SOC
  762. #include "ohci-ppc-soc.c"
  763. #define PLATFORM_DRIVER ohci_hcd_ppc_soc_driver
  764. #endif
  765. #ifdef CONFIG_ARCH_AT91
  766. #include "ohci-at91.c"
  767. #define PLATFORM_DRIVER ohci_hcd_at91_driver
  768. #endif
  769. #ifdef CONFIG_ARCH_PNX4008
  770. #include "ohci-pnx4008.c"
  771. #define PLATFORM_DRIVER usb_hcd_pnx4008_driver
  772. #endif
  773. #ifdef CONFIG_USB_OHCI_HCD_PPC_OF
  774. #include "ohci-ppc-of.c"
  775. #define OF_PLATFORM_DRIVER ohci_hcd_ppc_of_driver
  776. #endif
  777. #ifdef CONFIG_PPC_PS3
  778. #include "ohci-ps3.c"
  779. #define PS3_SYSTEM_BUS_DRIVER ps3_ohci_sb_driver
  780. #endif
  781. #if !defined(PCI_DRIVER) && \
  782. !defined(PLATFORM_DRIVER) && \
  783. !defined(OF_PLATFORM_DRIVER) && \
  784. !defined(SA1111_DRIVER) && \
  785. !defined(PS3_SYSTEM_BUS_DRIVER)
  786. #error "missing bus glue for ohci-hcd"
  787. #endif
  788. static int __init ohci_hcd_mod_init(void)
  789. {
  790. int retval = 0;
  791. if (usb_disabled())
  792. return -ENODEV;
  793. printk (KERN_DEBUG "%s: " DRIVER_INFO "\n", hcd_name);
  794. pr_debug ("%s: block sizes: ed %Zd td %Zd\n", hcd_name,
  795. sizeof (struct ed), sizeof (struct td));
  796. #ifdef PS3_SYSTEM_BUS_DRIVER
  797. retval = ps3_system_bus_driver_register(&PS3_SYSTEM_BUS_DRIVER);
  798. if (retval < 0)
  799. goto error_ps3;
  800. #endif
  801. #ifdef PLATFORM_DRIVER
  802. retval = platform_driver_register(&PLATFORM_DRIVER);
  803. if (retval < 0)
  804. goto error_platform;
  805. #endif
  806. #ifdef OF_PLATFORM_DRIVER
  807. retval = of_register_platform_driver(&OF_PLATFORM_DRIVER);
  808. if (retval < 0)
  809. goto error_of_platform;
  810. #endif
  811. #ifdef SA1111_DRIVER
  812. retval = sa1111_driver_register(&SA1111_DRIVER);
  813. if (retval < 0)
  814. goto error_sa1111;
  815. #endif
  816. #ifdef PCI_DRIVER
  817. retval = pci_register_driver(&PCI_DRIVER);
  818. if (retval < 0)
  819. goto error_pci;
  820. #endif
  821. return retval;
  822. /* Error path */
  823. #ifdef PCI_DRIVER
  824. error_pci:
  825. #endif
  826. #ifdef SA1111_DRIVER
  827. sa1111_driver_unregister(&SA1111_DRIVER);
  828. error_sa1111:
  829. #endif
  830. #ifdef OF_PLATFORM_DRIVER
  831. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  832. error_of_platform:
  833. #endif
  834. #ifdef PLATFORM_DRIVER
  835. platform_driver_unregister(&PLATFORM_DRIVER);
  836. error_platform:
  837. #endif
  838. #ifdef PS3_SYSTEM_BUS_DRIVER
  839. ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  840. error_ps3:
  841. #endif
  842. return retval;
  843. }
  844. module_init(ohci_hcd_mod_init);
  845. static void __exit ohci_hcd_mod_exit(void)
  846. {
  847. #ifdef PCI_DRIVER
  848. pci_unregister_driver(&PCI_DRIVER);
  849. #endif
  850. #ifdef SA1111_DRIVER
  851. sa1111_driver_unregister(&SA1111_DRIVER);
  852. #endif
  853. #ifdef OF_PLATFORM_DRIVER
  854. of_unregister_platform_driver(&OF_PLATFORM_DRIVER);
  855. #endif
  856. #ifdef PLATFORM_DRIVER
  857. platform_driver_unregister(&PLATFORM_DRIVER);
  858. #endif
  859. #ifdef PS3_SYSTEM_BUS_DRIVER
  860. ps3_system_bus_driver_unregister(&PS3_SYSTEM_BUS_DRIVER);
  861. #endif
  862. }
  863. module_exit(ohci_hcd_mod_exit);