ohci-hcd.c 27 KB

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