ohci-hub.c 21 KB

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