ohci-hub.c 20 KB

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