ohci-hcd.c 27 KB

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