ohci-hub.c 21 KB

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