ohci-hub.c 20 KB

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