ohci-hcd.c 31 KB

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