ohci-hcd.c 30 KB

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