ohci-hub.c 21 KB

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