ohci-hub.c 21 KB

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