ohci-hcd.c 27 KB

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