ohci-hcd.c 27 KB

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