ohci-hub.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. * This file is licenced under GPL
  8. */
  9. /*-------------------------------------------------------------------------*/
  10. /*
  11. * OHCI Root Hub ... the nonsharable stuff
  12. */
  13. #define dbg_port(hc,label,num,value) \
  14. ohci_dbg (hc, \
  15. "%s roothub.portstatus [%d] " \
  16. "= 0x%08x%s%s%s%s%s%s%s%s%s%s%s%s\n", \
  17. label, num, temp, \
  18. (temp & RH_PS_PRSC) ? " PRSC" : "", \
  19. (temp & RH_PS_OCIC) ? " OCIC" : "", \
  20. (temp & RH_PS_PSSC) ? " PSSC" : "", \
  21. (temp & RH_PS_PESC) ? " PESC" : "", \
  22. (temp & RH_PS_CSC) ? " CSC" : "", \
  23. \
  24. (temp & RH_PS_LSDA) ? " LSDA" : "", \
  25. (temp & RH_PS_PPS) ? " PPS" : "", \
  26. (temp & RH_PS_PRS) ? " PRS" : "", \
  27. (temp & RH_PS_POCI) ? " POCI" : "", \
  28. (temp & RH_PS_PSS) ? " PSS" : "", \
  29. \
  30. (temp & RH_PS_PES) ? " PES" : "", \
  31. (temp & RH_PS_CCS) ? " CCS" : "" \
  32. );
  33. /*-------------------------------------------------------------------------*/
  34. #define OHCI_SCHED_ENABLES \
  35. (OHCI_CTRL_CLE|OHCI_CTRL_BLE|OHCI_CTRL_PLE|OHCI_CTRL_IE)
  36. static void dl_done_list (struct ohci_hcd *);
  37. static void finish_unlinks (struct ohci_hcd *, u16);
  38. #ifdef CONFIG_PM
  39. static int ohci_rh_suspend (struct ohci_hcd *ohci, int autostop)
  40. __releases(ohci->lock)
  41. __acquires(ohci->lock)
  42. {
  43. int status = 0;
  44. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  45. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  46. case OHCI_USB_RESUME:
  47. ohci_dbg (ohci, "resume/suspend?\n");
  48. ohci->hc_control &= ~OHCI_CTRL_HCFS;
  49. ohci->hc_control |= OHCI_USB_RESET;
  50. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  51. (void) ohci_readl (ohci, &ohci->regs->control);
  52. /* FALL THROUGH */
  53. case OHCI_USB_RESET:
  54. status = -EBUSY;
  55. ohci_dbg (ohci, "needs reinit!\n");
  56. goto done;
  57. case OHCI_USB_SUSPEND:
  58. if (!ohci->autostop) {
  59. ohci_dbg (ohci, "already suspended\n");
  60. goto done;
  61. }
  62. }
  63. ohci_dbg (ohci, "%s root hub\n",
  64. autostop ? "auto-stop" : "suspend");
  65. /* First stop any processing */
  66. if (!autostop && (ohci->hc_control & OHCI_SCHED_ENABLES)) {
  67. ohci->hc_control &= ~OHCI_SCHED_ENABLES;
  68. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  69. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  70. ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrstatus);
  71. /* sched disables take effect on the next frame,
  72. * then the last WDH could take 6+ msec
  73. */
  74. ohci_dbg (ohci, "stopping schedules ...\n");
  75. ohci->autostop = 0;
  76. spin_unlock_irq (&ohci->lock);
  77. msleep (8);
  78. spin_lock_irq (&ohci->lock);
  79. }
  80. dl_done_list (ohci);
  81. finish_unlinks (ohci, ohci_frame_no(ohci));
  82. /* maybe resume can wake root hub */
  83. if (ohci_to_hcd(ohci)->self.root_hub->do_remote_wakeup || autostop) {
  84. ohci->hc_control |= OHCI_CTRL_RWE;
  85. } else {
  86. ohci_writel(ohci, OHCI_INTR_RHSC | OHCI_INTR_RD,
  87. &ohci->regs->intrdisable);
  88. ohci->hc_control &= ~OHCI_CTRL_RWE;
  89. }
  90. /* Suspend hub ... this is the "global (to this bus) suspend" mode,
  91. * which doesn't imply ports will first be individually suspended.
  92. */
  93. ohci->hc_control &= ~OHCI_CTRL_HCFS;
  94. ohci->hc_control |= OHCI_USB_SUSPEND;
  95. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  96. (void) ohci_readl (ohci, &ohci->regs->control);
  97. /* no resumes until devices finish suspending */
  98. if (!autostop) {
  99. ohci->next_statechange = jiffies + msecs_to_jiffies (5);
  100. ohci->autostop = 0;
  101. ohci->rh_state = OHCI_RH_SUSPENDED;
  102. }
  103. done:
  104. return status;
  105. }
  106. static inline struct ed *find_head (struct ed *ed)
  107. {
  108. /* for bulk and control lists */
  109. while (ed->ed_prev)
  110. ed = ed->ed_prev;
  111. return ed;
  112. }
  113. /* caller has locked the root hub */
  114. static int ohci_rh_resume (struct ohci_hcd *ohci)
  115. __releases(ohci->lock)
  116. __acquires(ohci->lock)
  117. {
  118. struct usb_hcd *hcd = ohci_to_hcd (ohci);
  119. u32 temp, enables;
  120. int status = -EINPROGRESS;
  121. int autostopped = ohci->autostop;
  122. ohci->autostop = 0;
  123. ohci->hc_control = ohci_readl (ohci, &ohci->regs->control);
  124. if (ohci->hc_control & (OHCI_CTRL_IR | OHCI_SCHED_ENABLES)) {
  125. /* this can happen after resuming a swsusp snapshot */
  126. if (ohci->rh_state != OHCI_RH_RUNNING) {
  127. ohci_dbg (ohci, "BIOS/SMM active, control %03x\n",
  128. ohci->hc_control);
  129. status = -EBUSY;
  130. /* this happens when pmcore resumes HC then root */
  131. } else {
  132. ohci_dbg (ohci, "duplicate resume\n");
  133. status = 0;
  134. }
  135. } else switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  136. case OHCI_USB_SUSPEND:
  137. ohci->hc_control &= ~(OHCI_CTRL_HCFS|OHCI_SCHED_ENABLES);
  138. ohci->hc_control |= OHCI_USB_RESUME;
  139. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  140. (void) ohci_readl (ohci, &ohci->regs->control);
  141. ohci_dbg (ohci, "%s root hub\n",
  142. autostopped ? "auto-start" : "resume");
  143. break;
  144. case OHCI_USB_RESUME:
  145. /* HCFS changes sometime after INTR_RD */
  146. ohci_dbg(ohci, "%swakeup root hub\n",
  147. autostopped ? "auto-" : "");
  148. break;
  149. case OHCI_USB_OPER:
  150. /* this can happen after resuming a swsusp snapshot */
  151. ohci_dbg (ohci, "snapshot resume? reinit\n");
  152. status = -EBUSY;
  153. break;
  154. default: /* RESET, we lost power */
  155. ohci_dbg (ohci, "lost power\n");
  156. status = -EBUSY;
  157. }
  158. if (status == -EBUSY) {
  159. if (!autostopped) {
  160. spin_unlock_irq (&ohci->lock);
  161. (void) ohci_init (ohci);
  162. status = ohci_restart (ohci);
  163. usb_root_hub_lost_power(hcd->self.root_hub);
  164. spin_lock_irq (&ohci->lock);
  165. }
  166. return status;
  167. }
  168. if (status != -EINPROGRESS)
  169. return status;
  170. if (autostopped)
  171. goto skip_resume;
  172. spin_unlock_irq (&ohci->lock);
  173. /* Some controllers (lucent erratum) need extra-long delays */
  174. msleep (20 /* usb 11.5.1.10 */ + 12 /* 32 msec counter */ + 1);
  175. temp = ohci_readl (ohci, &ohci->regs->control);
  176. temp &= OHCI_CTRL_HCFS;
  177. if (temp != OHCI_USB_RESUME) {
  178. ohci_err (ohci, "controller won't resume\n");
  179. spin_lock_irq(&ohci->lock);
  180. return -EBUSY;
  181. }
  182. /* disable old schedule state, reinit from scratch */
  183. ohci_writel (ohci, 0, &ohci->regs->ed_controlhead);
  184. ohci_writel (ohci, 0, &ohci->regs->ed_controlcurrent);
  185. ohci_writel (ohci, 0, &ohci->regs->ed_bulkhead);
  186. ohci_writel (ohci, 0, &ohci->regs->ed_bulkcurrent);
  187. ohci_writel (ohci, 0, &ohci->regs->ed_periodcurrent);
  188. ohci_writel (ohci, (u32) ohci->hcca_dma, &ohci->regs->hcca);
  189. /* Sometimes PCI D3 suspend trashes frame timings ... */
  190. periodic_reinit (ohci);
  191. /* the following code is executed with ohci->lock held and
  192. * irqs disabled if and only if autostopped is true
  193. */
  194. skip_resume:
  195. /* interrupts might have been disabled */
  196. ohci_writel (ohci, OHCI_INTR_INIT, &ohci->regs->intrenable);
  197. if (ohci->ed_rm_list)
  198. ohci_writel (ohci, OHCI_INTR_SF, &ohci->regs->intrenable);
  199. /* Then re-enable operations */
  200. ohci_writel (ohci, OHCI_USB_OPER, &ohci->regs->control);
  201. (void) ohci_readl (ohci, &ohci->regs->control);
  202. if (!autostopped)
  203. msleep (3);
  204. temp = ohci->hc_control;
  205. temp &= OHCI_CTRL_RWC;
  206. temp |= OHCI_CONTROL_INIT | OHCI_USB_OPER;
  207. ohci->hc_control = temp;
  208. ohci_writel (ohci, temp, &ohci->regs->control);
  209. (void) ohci_readl (ohci, &ohci->regs->control);
  210. /* TRSMRCY */
  211. if (!autostopped) {
  212. msleep (10);
  213. spin_lock_irq (&ohci->lock);
  214. }
  215. /* now ohci->lock is always held and irqs are always disabled */
  216. /* keep it alive for more than ~5x suspend + resume costs */
  217. ohci->next_statechange = jiffies + STATECHANGE_DELAY;
  218. /* maybe turn schedules back on */
  219. enables = 0;
  220. temp = 0;
  221. if (!ohci->ed_rm_list) {
  222. if (ohci->ed_controltail) {
  223. ohci_writel (ohci,
  224. find_head (ohci->ed_controltail)->dma,
  225. &ohci->regs->ed_controlhead);
  226. enables |= OHCI_CTRL_CLE;
  227. temp |= OHCI_CLF;
  228. }
  229. if (ohci->ed_bulktail) {
  230. ohci_writel (ohci, find_head (ohci->ed_bulktail)->dma,
  231. &ohci->regs->ed_bulkhead);
  232. enables |= OHCI_CTRL_BLE;
  233. temp |= OHCI_BLF;
  234. }
  235. }
  236. if (hcd->self.bandwidth_isoc_reqs || hcd->self.bandwidth_int_reqs)
  237. enables |= OHCI_CTRL_PLE|OHCI_CTRL_IE;
  238. if (enables) {
  239. ohci_dbg (ohci, "restarting schedules ... %08x\n", enables);
  240. ohci->hc_control |= enables;
  241. ohci_writel (ohci, ohci->hc_control, &ohci->regs->control);
  242. if (temp)
  243. ohci_writel (ohci, temp, &ohci->regs->cmdstatus);
  244. (void) ohci_readl (ohci, &ohci->regs->control);
  245. }
  246. ohci->rh_state = OHCI_RH_RUNNING;
  247. return 0;
  248. }
  249. static int ohci_bus_suspend (struct usb_hcd *hcd)
  250. {
  251. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  252. int rc;
  253. spin_lock_irq (&ohci->lock);
  254. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  255. rc = -ESHUTDOWN;
  256. else
  257. rc = ohci_rh_suspend (ohci, 0);
  258. spin_unlock_irq (&ohci->lock);
  259. return rc;
  260. }
  261. static int ohci_bus_resume (struct usb_hcd *hcd)
  262. {
  263. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  264. int rc;
  265. if (time_before (jiffies, ohci->next_statechange))
  266. msleep(5);
  267. spin_lock_irq (&ohci->lock);
  268. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  269. rc = -ESHUTDOWN;
  270. else
  271. rc = ohci_rh_resume (ohci);
  272. spin_unlock_irq (&ohci->lock);
  273. /* poll until we know a device is connected or we autostop */
  274. if (rc == 0)
  275. usb_hcd_poll_rh_status(hcd);
  276. return rc;
  277. }
  278. /* Carry out polling-, autostop-, and autoresume-related state changes */
  279. static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
  280. int any_connected, int rhsc_status)
  281. {
  282. int poll_rh = 1;
  283. int rhsc_enable;
  284. /* Some broken controllers never turn off RHSC in the interrupt
  285. * status register. For their sake we won't re-enable RHSC
  286. * interrupts if the interrupt bit is already active.
  287. */
  288. rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
  289. OHCI_INTR_RHSC;
  290. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  291. case OHCI_USB_OPER:
  292. /* If no status changes are pending, enable RHSC interrupts. */
  293. if (!rhsc_enable && !rhsc_status && !changed) {
  294. rhsc_enable = OHCI_INTR_RHSC;
  295. ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
  296. }
  297. /* Keep on polling until we know a device is connected
  298. * and RHSC is enabled, or until we autostop.
  299. */
  300. if (!ohci->autostop) {
  301. if (any_connected ||
  302. !device_may_wakeup(&ohci_to_hcd(ohci)
  303. ->self.root_hub->dev)) {
  304. if (rhsc_enable)
  305. poll_rh = 0;
  306. } else {
  307. ohci->autostop = 1;
  308. ohci->next_statechange = jiffies + HZ;
  309. }
  310. /* if no devices have been attached for one second, autostop */
  311. } else {
  312. if (changed || any_connected) {
  313. ohci->autostop = 0;
  314. ohci->next_statechange = jiffies +
  315. STATECHANGE_DELAY;
  316. } else if (time_after_eq(jiffies,
  317. ohci->next_statechange)
  318. && !ohci->ed_rm_list
  319. && !(ohci->hc_control &
  320. OHCI_SCHED_ENABLES)) {
  321. ohci_rh_suspend(ohci, 1);
  322. if (rhsc_enable)
  323. poll_rh = 0;
  324. }
  325. }
  326. break;
  327. case OHCI_USB_SUSPEND:
  328. case OHCI_USB_RESUME:
  329. /* if there is a port change, autostart or ask to be resumed */
  330. if (changed) {
  331. if (ohci->autostop)
  332. ohci_rh_resume(ohci);
  333. else
  334. usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
  335. /* If remote wakeup is disabled, stop polling */
  336. } else if (!ohci->autostop &&
  337. !ohci_to_hcd(ohci)->self.root_hub->
  338. do_remote_wakeup) {
  339. poll_rh = 0;
  340. } else {
  341. /* If no status changes are pending,
  342. * enable RHSC interrupts
  343. */
  344. if (!rhsc_enable && !rhsc_status) {
  345. rhsc_enable = OHCI_INTR_RHSC;
  346. ohci_writel(ohci, rhsc_enable,
  347. &ohci->regs->intrenable);
  348. }
  349. /* Keep polling until RHSC is enabled */
  350. if (rhsc_enable)
  351. poll_rh = 0;
  352. }
  353. break;
  354. }
  355. return poll_rh;
  356. }
  357. #else /* CONFIG_PM */
  358. static inline int ohci_rh_resume(struct ohci_hcd *ohci)
  359. {
  360. return 0;
  361. }
  362. /* Carry out polling-related state changes.
  363. * autostop isn't used when CONFIG_PM is turned off.
  364. */
  365. static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
  366. int any_connected, int rhsc_status)
  367. {
  368. /* If RHSC is enabled, don't poll */
  369. if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
  370. return 0;
  371. /* If status changes are pending, continue polling.
  372. * Conversely, if no status changes are pending but the RHSC
  373. * status bit was set, then RHSC may be broken so continue polling.
  374. */
  375. if (changed || rhsc_status)
  376. return 1;
  377. /* It's safe to re-enable RHSC interrupts */
  378. ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
  379. return 0;
  380. }
  381. #endif /* CONFIG_PM */
  382. /*-------------------------------------------------------------------------*/
  383. /* build "status change" packet (one or two bytes) from HC registers */
  384. static int
  385. ohci_hub_status_data (struct usb_hcd *hcd, char *buf)
  386. {
  387. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  388. int i, changed = 0, length = 1;
  389. int any_connected = 0;
  390. int rhsc_status;
  391. unsigned long flags;
  392. spin_lock_irqsave (&ohci->lock, flags);
  393. if (!HCD_HW_ACCESSIBLE(hcd))
  394. goto done;
  395. /* undocumented erratum seen on at least rev D */
  396. if ((ohci->flags & OHCI_QUIRK_AMD756)
  397. && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
  398. ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
  399. ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
  400. /* retry later; "should not happen" */
  401. goto done;
  402. }
  403. /* init status */
  404. if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
  405. buf [0] = changed = 1;
  406. else
  407. buf [0] = 0;
  408. if (ohci->num_ports > 7) {
  409. buf [1] = 0;
  410. length++;
  411. }
  412. /* Clear the RHSC status flag before reading the port statuses */
  413. ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
  414. rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
  415. OHCI_INTR_RHSC;
  416. /* look at each port */
  417. for (i = 0; i < ohci->num_ports; i++) {
  418. u32 status = roothub_portstatus (ohci, i);
  419. /* can't autostop if ports are connected */
  420. any_connected |= (status & RH_PS_CCS);
  421. if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
  422. | RH_PS_OCIC | RH_PS_PRSC)) {
  423. changed = 1;
  424. if (i < 7)
  425. buf [0] |= 1 << (i + 1);
  426. else
  427. buf [1] |= 1 << (i - 7);
  428. }
  429. }
  430. if (ohci_root_hub_state_changes(ohci, changed,
  431. any_connected, rhsc_status))
  432. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  433. else
  434. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  435. done:
  436. spin_unlock_irqrestore (&ohci->lock, flags);
  437. return changed ? length : 0;
  438. }
  439. /*-------------------------------------------------------------------------*/
  440. static void
  441. ohci_hub_descriptor (
  442. struct ohci_hcd *ohci,
  443. struct usb_hub_descriptor *desc
  444. ) {
  445. u32 rh = roothub_a (ohci);
  446. u16 temp;
  447. desc->bDescriptorType = 0x29;
  448. desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
  449. desc->bHubContrCurrent = 0;
  450. desc->bNbrPorts = ohci->num_ports;
  451. temp = 1 + (ohci->num_ports / 8);
  452. desc->bDescLength = 7 + 2 * temp;
  453. temp = 0;
  454. if (rh & RH_A_NPS) /* no power switching? */
  455. temp |= 0x0002;
  456. if (rh & RH_A_PSM) /* per-port power switching? */
  457. temp |= 0x0001;
  458. if (rh & RH_A_NOCP) /* no overcurrent reporting? */
  459. temp |= 0x0010;
  460. else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
  461. temp |= 0x0008;
  462. desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
  463. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  464. rh = roothub_b (ohci);
  465. memset(desc->u.hs.DeviceRemovable, 0xff,
  466. sizeof(desc->u.hs.DeviceRemovable));
  467. desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
  468. if (ohci->num_ports > 7) {
  469. desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
  470. desc->u.hs.DeviceRemovable[2] = 0xff;
  471. } else
  472. desc->u.hs.DeviceRemovable[1] = 0xff;
  473. }
  474. /*-------------------------------------------------------------------------*/
  475. #ifdef CONFIG_USB_OTG
  476. static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
  477. {
  478. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  479. u32 status;
  480. if (!port)
  481. return -EINVAL;
  482. port--;
  483. /* start port reset before HNP protocol times out */
  484. status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
  485. if (!(status & RH_PS_CCS))
  486. return -ENODEV;
  487. /* khubd will finish the reset later */
  488. ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
  489. return 0;
  490. }
  491. #else
  492. #define ohci_start_port_reset NULL
  493. #endif
  494. /*-------------------------------------------------------------------------*/
  495. /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
  496. * not necessarily continuous ... to guard against resume signaling.
  497. * The short timeout is safe for non-root hubs, and is backward-compatible
  498. * with earlier Linux hosts.
  499. */
  500. #ifdef CONFIG_USB_SUSPEND
  501. #define PORT_RESET_MSEC 50
  502. #else
  503. #define PORT_RESET_MSEC 10
  504. #endif
  505. /* this timer value might be vendor-specific ... */
  506. #define PORT_RESET_HW_MSEC 10
  507. /* wrap-aware logic morphed from <linux/jiffies.h> */
  508. #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
  509. /* called from some task, normally khubd */
  510. static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
  511. {
  512. __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
  513. u32 temp = 0;
  514. u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
  515. u16 reset_done = now + PORT_RESET_MSEC;
  516. int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
  517. /* build a "continuous enough" reset signal, with up to
  518. * 3msec gap between pulses. scheduler HZ==100 must work;
  519. * this might need to be deadline-scheduled.
  520. */
  521. do {
  522. int limit_2;
  523. /* spin until any current reset finishes */
  524. limit_2 = PORT_RESET_HW_MSEC * 2;
  525. while (--limit_2 >= 0) {
  526. temp = ohci_readl (ohci, portstat);
  527. /* handle e.g. CardBus eject */
  528. if (temp == ~(u32)0)
  529. return -ESHUTDOWN;
  530. if (!(temp & RH_PS_PRS))
  531. break;
  532. udelay (500);
  533. }
  534. /* timeout (a hardware error) has been observed when
  535. * EHCI sets CF while this driver is resetting a port;
  536. * presumably other disconnect paths might do it too.
  537. */
  538. if (limit_2 < 0) {
  539. ohci_dbg(ohci,
  540. "port[%d] reset timeout, stat %08x\n",
  541. port, temp);
  542. break;
  543. }
  544. if (!(temp & RH_PS_CCS))
  545. break;
  546. if (temp & RH_PS_PRSC)
  547. ohci_writel (ohci, RH_PS_PRSC, portstat);
  548. /* start the next reset, sleep till it's probably done */
  549. ohci_writel (ohci, RH_PS_PRS, portstat);
  550. msleep(PORT_RESET_HW_MSEC);
  551. now = ohci_readl(ohci, &ohci->regs->fmnumber);
  552. } while (tick_before(now, reset_done) && --limit_1 >= 0);
  553. /* caller synchronizes using PRSC ... and handles PRS
  554. * still being set when this returns.
  555. */
  556. return 0;
  557. }
  558. static int ohci_hub_control (
  559. struct usb_hcd *hcd,
  560. u16 typeReq,
  561. u16 wValue,
  562. u16 wIndex,
  563. char *buf,
  564. u16 wLength
  565. ) {
  566. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  567. int ports = ohci->num_ports;
  568. u32 temp;
  569. int retval = 0;
  570. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  571. return -ESHUTDOWN;
  572. switch (typeReq) {
  573. case ClearHubFeature:
  574. switch (wValue) {
  575. case C_HUB_OVER_CURRENT:
  576. ohci_writel (ohci, RH_HS_OCIC,
  577. &ohci->regs->roothub.status);
  578. case C_HUB_LOCAL_POWER:
  579. break;
  580. default:
  581. goto error;
  582. }
  583. break;
  584. case ClearPortFeature:
  585. if (!wIndex || wIndex > ports)
  586. goto error;
  587. wIndex--;
  588. switch (wValue) {
  589. case USB_PORT_FEAT_ENABLE:
  590. temp = RH_PS_CCS;
  591. break;
  592. case USB_PORT_FEAT_C_ENABLE:
  593. temp = RH_PS_PESC;
  594. break;
  595. case USB_PORT_FEAT_SUSPEND:
  596. temp = RH_PS_POCI;
  597. break;
  598. case USB_PORT_FEAT_C_SUSPEND:
  599. temp = RH_PS_PSSC;
  600. break;
  601. case USB_PORT_FEAT_POWER:
  602. temp = RH_PS_LSDA;
  603. break;
  604. case USB_PORT_FEAT_C_CONNECTION:
  605. temp = RH_PS_CSC;
  606. break;
  607. case USB_PORT_FEAT_C_OVER_CURRENT:
  608. temp = RH_PS_OCIC;
  609. break;
  610. case USB_PORT_FEAT_C_RESET:
  611. temp = RH_PS_PRSC;
  612. break;
  613. default:
  614. goto error;
  615. }
  616. ohci_writel (ohci, temp,
  617. &ohci->regs->roothub.portstatus [wIndex]);
  618. // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
  619. break;
  620. case GetHubDescriptor:
  621. ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
  622. break;
  623. case GetHubStatus:
  624. temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
  625. put_unaligned_le32(temp, buf);
  626. break;
  627. case GetPortStatus:
  628. if (!wIndex || wIndex > ports)
  629. goto error;
  630. wIndex--;
  631. temp = roothub_portstatus (ohci, wIndex);
  632. put_unaligned_le32(temp, buf);
  633. #ifndef OHCI_VERBOSE_DEBUG
  634. if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
  635. #endif
  636. dbg_port (ohci, "GetStatus", wIndex, temp);
  637. break;
  638. case SetHubFeature:
  639. switch (wValue) {
  640. case C_HUB_OVER_CURRENT:
  641. // FIXME: this can be cleared, yes?
  642. case C_HUB_LOCAL_POWER:
  643. break;
  644. default:
  645. goto error;
  646. }
  647. break;
  648. case SetPortFeature:
  649. if (!wIndex || wIndex > ports)
  650. goto error;
  651. wIndex--;
  652. switch (wValue) {
  653. case USB_PORT_FEAT_SUSPEND:
  654. #ifdef CONFIG_USB_OTG
  655. if (hcd->self.otg_port == (wIndex + 1)
  656. && hcd->self.b_hnp_enable)
  657. ohci->start_hnp(ohci);
  658. else
  659. #endif
  660. ohci_writel (ohci, RH_PS_PSS,
  661. &ohci->regs->roothub.portstatus [wIndex]);
  662. break;
  663. case USB_PORT_FEAT_POWER:
  664. ohci_writel (ohci, RH_PS_PPS,
  665. &ohci->regs->roothub.portstatus [wIndex]);
  666. break;
  667. case USB_PORT_FEAT_RESET:
  668. retval = root_port_reset (ohci, wIndex);
  669. break;
  670. default:
  671. goto error;
  672. }
  673. break;
  674. default:
  675. error:
  676. /* "protocol stall" on error */
  677. retval = -EPIPE;
  678. }
  679. return retval;
  680. }