ohci-hub.c 21 KB

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