ohci-hub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 update_done_list(struct ohci_hcd *);
  37. static void ohci_work(struct ohci_hcd *);
  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. update_done_list(ohci);
  81. ohci_work(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. if (rc == 0) {
  277. del_timer_sync(&ohci->io_watchdog);
  278. ohci->prev_frame_no = IO_WATCHDOG_OFF;
  279. }
  280. return rc;
  281. }
  282. static int ohci_bus_resume (struct usb_hcd *hcd)
  283. {
  284. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  285. int rc;
  286. if (time_before (jiffies, ohci->next_statechange))
  287. msleep(5);
  288. spin_lock_irq (&ohci->lock);
  289. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  290. rc = -ESHUTDOWN;
  291. else
  292. rc = ohci_rh_resume (ohci);
  293. spin_unlock_irq (&ohci->lock);
  294. /* poll until we know a device is connected or we autostop */
  295. if (rc == 0)
  296. usb_hcd_poll_rh_status(hcd);
  297. return rc;
  298. }
  299. /* Carry out polling-, autostop-, and autoresume-related state changes */
  300. static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
  301. int any_connected, int rhsc_status)
  302. {
  303. int poll_rh = 1;
  304. int rhsc_enable;
  305. /* Some broken controllers never turn off RHSC in the interrupt
  306. * status register. For their sake we won't re-enable RHSC
  307. * interrupts if the interrupt bit is already active.
  308. */
  309. rhsc_enable = ohci_readl(ohci, &ohci->regs->intrenable) &
  310. OHCI_INTR_RHSC;
  311. switch (ohci->hc_control & OHCI_CTRL_HCFS) {
  312. case OHCI_USB_OPER:
  313. /* If no status changes are pending, enable RHSC interrupts. */
  314. if (!rhsc_enable && !rhsc_status && !changed) {
  315. rhsc_enable = OHCI_INTR_RHSC;
  316. ohci_writel(ohci, rhsc_enable, &ohci->regs->intrenable);
  317. }
  318. /* Keep on polling until we know a device is connected
  319. * and RHSC is enabled, or until we autostop.
  320. */
  321. if (!ohci->autostop) {
  322. if (any_connected ||
  323. !device_may_wakeup(&ohci_to_hcd(ohci)
  324. ->self.root_hub->dev)) {
  325. if (rhsc_enable)
  326. poll_rh = 0;
  327. } else {
  328. ohci->autostop = 1;
  329. ohci->next_statechange = jiffies + HZ;
  330. }
  331. /* if no devices have been attached for one second, autostop */
  332. } else {
  333. if (changed || any_connected) {
  334. ohci->autostop = 0;
  335. ohci->next_statechange = jiffies +
  336. STATECHANGE_DELAY;
  337. } else if (time_after_eq(jiffies,
  338. ohci->next_statechange)
  339. && !ohci->ed_rm_list
  340. && !(ohci->hc_control &
  341. OHCI_SCHED_ENABLES)) {
  342. ohci_rh_suspend(ohci, 1);
  343. if (rhsc_enable)
  344. poll_rh = 0;
  345. }
  346. }
  347. break;
  348. case OHCI_USB_SUSPEND:
  349. case OHCI_USB_RESUME:
  350. /* if there is a port change, autostart or ask to be resumed */
  351. if (changed) {
  352. if (ohci->autostop)
  353. ohci_rh_resume(ohci);
  354. else
  355. usb_hcd_resume_root_hub(ohci_to_hcd(ohci));
  356. /* If remote wakeup is disabled, stop polling */
  357. } else if (!ohci->autostop &&
  358. !ohci_to_hcd(ohci)->self.root_hub->
  359. do_remote_wakeup) {
  360. poll_rh = 0;
  361. } else {
  362. /* If no status changes are pending,
  363. * enable RHSC interrupts
  364. */
  365. if (!rhsc_enable && !rhsc_status) {
  366. rhsc_enable = OHCI_INTR_RHSC;
  367. ohci_writel(ohci, rhsc_enable,
  368. &ohci->regs->intrenable);
  369. }
  370. /* Keep polling until RHSC is enabled */
  371. if (rhsc_enable)
  372. poll_rh = 0;
  373. }
  374. break;
  375. }
  376. return poll_rh;
  377. }
  378. #else /* CONFIG_PM */
  379. static inline int ohci_rh_resume(struct ohci_hcd *ohci)
  380. {
  381. return 0;
  382. }
  383. /* Carry out polling-related state changes.
  384. * autostop isn't used when CONFIG_PM is turned off.
  385. */
  386. static int ohci_root_hub_state_changes(struct ohci_hcd *ohci, int changed,
  387. int any_connected, int rhsc_status)
  388. {
  389. /* If RHSC is enabled, don't poll */
  390. if (ohci_readl(ohci, &ohci->regs->intrenable) & OHCI_INTR_RHSC)
  391. return 0;
  392. /* If status changes are pending, continue polling.
  393. * Conversely, if no status changes are pending but the RHSC
  394. * status bit was set, then RHSC may be broken so continue polling.
  395. */
  396. if (changed || rhsc_status)
  397. return 1;
  398. /* It's safe to re-enable RHSC interrupts */
  399. ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrenable);
  400. return 0;
  401. }
  402. #endif /* CONFIG_PM */
  403. /*-------------------------------------------------------------------------*/
  404. /* build "status change" packet (one or two bytes) from HC registers */
  405. int ohci_hub_status_data(struct usb_hcd *hcd, char *buf)
  406. {
  407. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  408. int i, changed = 0, length = 1;
  409. int any_connected = 0;
  410. int rhsc_status;
  411. unsigned long flags;
  412. spin_lock_irqsave (&ohci->lock, flags);
  413. if (!HCD_HW_ACCESSIBLE(hcd))
  414. goto done;
  415. /* undocumented erratum seen on at least rev D */
  416. if ((ohci->flags & OHCI_QUIRK_AMD756)
  417. && (roothub_a (ohci) & RH_A_NDP) > MAX_ROOT_PORTS) {
  418. ohci_warn (ohci, "bogus NDP, rereads as NDP=%d\n",
  419. ohci_readl (ohci, &ohci->regs->roothub.a) & RH_A_NDP);
  420. /* retry later; "should not happen" */
  421. goto done;
  422. }
  423. /* init status */
  424. if (roothub_status (ohci) & (RH_HS_LPSC | RH_HS_OCIC))
  425. buf [0] = changed = 1;
  426. else
  427. buf [0] = 0;
  428. if (ohci->num_ports > 7) {
  429. buf [1] = 0;
  430. length++;
  431. }
  432. /* Clear the RHSC status flag before reading the port statuses */
  433. ohci_writel(ohci, OHCI_INTR_RHSC, &ohci->regs->intrstatus);
  434. rhsc_status = ohci_readl(ohci, &ohci->regs->intrstatus) &
  435. OHCI_INTR_RHSC;
  436. /* look at each port */
  437. for (i = 0; i < ohci->num_ports; i++) {
  438. u32 status = roothub_portstatus (ohci, i);
  439. /* can't autostop if ports are connected */
  440. any_connected |= (status & RH_PS_CCS);
  441. if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
  442. | RH_PS_OCIC | RH_PS_PRSC)) {
  443. changed = 1;
  444. if (i < 7)
  445. buf [0] |= 1 << (i + 1);
  446. else
  447. buf [1] |= 1 << (i - 7);
  448. }
  449. }
  450. if (ohci_root_hub_state_changes(ohci, changed,
  451. any_connected, rhsc_status))
  452. set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  453. else
  454. clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
  455. done:
  456. spin_unlock_irqrestore (&ohci->lock, flags);
  457. return changed ? length : 0;
  458. }
  459. EXPORT_SYMBOL_GPL(ohci_hub_status_data);
  460. /*-------------------------------------------------------------------------*/
  461. static void
  462. ohci_hub_descriptor (
  463. struct ohci_hcd *ohci,
  464. struct usb_hub_descriptor *desc
  465. ) {
  466. u32 rh = roothub_a (ohci);
  467. u16 temp;
  468. desc->bDescriptorType = USB_DT_HUB;
  469. desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
  470. desc->bHubContrCurrent = 0;
  471. desc->bNbrPorts = ohci->num_ports;
  472. temp = 1 + (ohci->num_ports / 8);
  473. desc->bDescLength = 7 + 2 * temp;
  474. temp = HUB_CHAR_COMMON_LPSM | HUB_CHAR_COMMON_OCPM;
  475. if (rh & RH_A_NPS) /* no power switching? */
  476. temp |= HUB_CHAR_NO_LPSM;
  477. if (rh & RH_A_PSM) /* per-port power switching? */
  478. temp |= HUB_CHAR_INDV_PORT_LPSM;
  479. if (rh & RH_A_NOCP) /* no overcurrent reporting? */
  480. temp |= HUB_CHAR_NO_OCPM;
  481. else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
  482. temp |= HUB_CHAR_INDV_PORT_OCPM;
  483. desc->wHubCharacteristics = cpu_to_le16(temp);
  484. /* ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  485. rh = roothub_b (ohci);
  486. memset(desc->u.hs.DeviceRemovable, 0xff,
  487. sizeof(desc->u.hs.DeviceRemovable));
  488. desc->u.hs.DeviceRemovable[0] = rh & RH_B_DR;
  489. if (ohci->num_ports > 7) {
  490. desc->u.hs.DeviceRemovable[1] = (rh & RH_B_DR) >> 8;
  491. desc->u.hs.DeviceRemovable[2] = 0xff;
  492. } else
  493. desc->u.hs.DeviceRemovable[1] = 0xff;
  494. }
  495. /*-------------------------------------------------------------------------*/
  496. #ifdef CONFIG_USB_OTG
  497. static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
  498. {
  499. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  500. u32 status;
  501. if (!port)
  502. return -EINVAL;
  503. port--;
  504. /* start port reset before HNP protocol times out */
  505. status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
  506. if (!(status & RH_PS_CCS))
  507. return -ENODEV;
  508. /* hub_wq will finish the reset later */
  509. ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
  510. return 0;
  511. }
  512. #else
  513. #define ohci_start_port_reset NULL
  514. #endif
  515. /*-------------------------------------------------------------------------*/
  516. /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
  517. * not necessarily continuous ... to guard against resume signaling.
  518. */
  519. #define PORT_RESET_MSEC 50
  520. /* this timer value might be vendor-specific ... */
  521. #define PORT_RESET_HW_MSEC 10
  522. /* wrap-aware logic morphed from <linux/jiffies.h> */
  523. #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
  524. /* called from some task, normally hub_wq */
  525. static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
  526. {
  527. __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
  528. u32 temp = 0;
  529. u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
  530. u16 reset_done = now + PORT_RESET_MSEC;
  531. int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
  532. /* build a "continuous enough" reset signal, with up to
  533. * 3msec gap between pulses. scheduler HZ==100 must work;
  534. * this might need to be deadline-scheduled.
  535. */
  536. do {
  537. int limit_2;
  538. /* spin until any current reset finishes */
  539. limit_2 = PORT_RESET_HW_MSEC * 2;
  540. while (--limit_2 >= 0) {
  541. temp = ohci_readl (ohci, portstat);
  542. /* handle e.g. CardBus eject */
  543. if (temp == ~(u32)0)
  544. return -ESHUTDOWN;
  545. if (!(temp & RH_PS_PRS))
  546. break;
  547. udelay (500);
  548. }
  549. /* timeout (a hardware error) has been observed when
  550. * EHCI sets CF while this driver is resetting a port;
  551. * presumably other disconnect paths might do it too.
  552. */
  553. if (limit_2 < 0) {
  554. ohci_dbg(ohci,
  555. "port[%d] reset timeout, stat %08x\n",
  556. port, temp);
  557. break;
  558. }
  559. if (!(temp & RH_PS_CCS))
  560. break;
  561. if (temp & RH_PS_PRSC)
  562. ohci_writel (ohci, RH_PS_PRSC, portstat);
  563. /* start the next reset, sleep till it's probably done */
  564. ohci_writel (ohci, RH_PS_PRS, portstat);
  565. msleep(PORT_RESET_HW_MSEC);
  566. now = ohci_readl(ohci, &ohci->regs->fmnumber);
  567. } while (tick_before(now, reset_done) && --limit_1 >= 0);
  568. /* caller synchronizes using PRSC ... and handles PRS
  569. * still being set when this returns.
  570. */
  571. return 0;
  572. }
  573. int ohci_hub_control(
  574. struct usb_hcd *hcd,
  575. u16 typeReq,
  576. u16 wValue,
  577. u16 wIndex,
  578. char *buf,
  579. u16 wLength
  580. ) {
  581. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  582. int ports = ohci->num_ports;
  583. u32 temp;
  584. int retval = 0;
  585. if (unlikely(!HCD_HW_ACCESSIBLE(hcd)))
  586. return -ESHUTDOWN;
  587. switch (typeReq) {
  588. case ClearHubFeature:
  589. switch (wValue) {
  590. case C_HUB_OVER_CURRENT:
  591. ohci_writel (ohci, RH_HS_OCIC,
  592. &ohci->regs->roothub.status);
  593. case C_HUB_LOCAL_POWER:
  594. break;
  595. default:
  596. goto error;
  597. }
  598. break;
  599. case ClearPortFeature:
  600. if (!wIndex || wIndex > ports)
  601. goto error;
  602. wIndex--;
  603. switch (wValue) {
  604. case USB_PORT_FEAT_ENABLE:
  605. temp = RH_PS_CCS;
  606. break;
  607. case USB_PORT_FEAT_C_ENABLE:
  608. temp = RH_PS_PESC;
  609. break;
  610. case USB_PORT_FEAT_SUSPEND:
  611. temp = RH_PS_POCI;
  612. break;
  613. case USB_PORT_FEAT_C_SUSPEND:
  614. temp = RH_PS_PSSC;
  615. break;
  616. case USB_PORT_FEAT_POWER:
  617. temp = RH_PS_LSDA;
  618. break;
  619. case USB_PORT_FEAT_C_CONNECTION:
  620. temp = RH_PS_CSC;
  621. break;
  622. case USB_PORT_FEAT_C_OVER_CURRENT:
  623. temp = RH_PS_OCIC;
  624. break;
  625. case USB_PORT_FEAT_C_RESET:
  626. temp = RH_PS_PRSC;
  627. break;
  628. default:
  629. goto error;
  630. }
  631. ohci_writel (ohci, temp,
  632. &ohci->regs->roothub.portstatus [wIndex]);
  633. // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
  634. break;
  635. case GetHubDescriptor:
  636. ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
  637. break;
  638. case GetHubStatus:
  639. temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
  640. put_unaligned_le32(temp, buf);
  641. break;
  642. case GetPortStatus:
  643. if (!wIndex || wIndex > ports)
  644. goto error;
  645. wIndex--;
  646. temp = roothub_portstatus (ohci, wIndex);
  647. put_unaligned_le32(temp, buf);
  648. if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
  649. dbg_port(ohci, "GetStatus", wIndex, temp);
  650. break;
  651. case SetHubFeature:
  652. switch (wValue) {
  653. case C_HUB_OVER_CURRENT:
  654. // FIXME: this can be cleared, yes?
  655. case C_HUB_LOCAL_POWER:
  656. break;
  657. default:
  658. goto error;
  659. }
  660. break;
  661. case SetPortFeature:
  662. if (!wIndex || wIndex > ports)
  663. goto error;
  664. wIndex--;
  665. switch (wValue) {
  666. case USB_PORT_FEAT_SUSPEND:
  667. #ifdef CONFIG_USB_OTG
  668. if (hcd->self.otg_port == (wIndex + 1)
  669. && hcd->self.b_hnp_enable)
  670. ohci->start_hnp(ohci);
  671. else
  672. #endif
  673. ohci_writel (ohci, RH_PS_PSS,
  674. &ohci->regs->roothub.portstatus [wIndex]);
  675. break;
  676. case USB_PORT_FEAT_POWER:
  677. ohci_writel (ohci, RH_PS_PPS,
  678. &ohci->regs->roothub.portstatus [wIndex]);
  679. break;
  680. case USB_PORT_FEAT_RESET:
  681. retval = root_port_reset (ohci, wIndex);
  682. break;
  683. default:
  684. goto error;
  685. }
  686. break;
  687. default:
  688. error:
  689. /* "protocol stall" on error */
  690. retval = -EPIPE;
  691. }
  692. return retval;
  693. }
  694. EXPORT_SYMBOL_GPL(ohci_hub_control);