ohci-hub.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. /* Some broken controllers never turn off RHCS in the interrupt
  423. * status register. For their sake we won't re-enable RHSC
  424. * interrupts if the flag is already set.
  425. */
  426. if (ohci_readl(ohci, &ohci->regs->intrstatus) & OHCI_INTR_RHSC)
  427. changed = 1;
  428. /* look at each port */
  429. for (i = 0; i < ohci->num_ports; i++) {
  430. u32 status = roothub_portstatus (ohci, i);
  431. /* can't autostop if ports are connected */
  432. any_connected |= (status & RH_PS_CCS);
  433. if (status & (RH_PS_CSC | RH_PS_PESC | RH_PS_PSSC
  434. | RH_PS_OCIC | RH_PS_PRSC)) {
  435. changed = 1;
  436. if (i < 7)
  437. buf [0] |= 1 << (i + 1);
  438. else
  439. buf [1] |= 1 << (i - 7);
  440. }
  441. }
  442. hcd->poll_rh = ohci_root_hub_state_changes(ohci, changed,
  443. any_connected);
  444. done:
  445. spin_unlock_irqrestore (&ohci->lock, flags);
  446. return changed ? length : 0;
  447. }
  448. /*-------------------------------------------------------------------------*/
  449. static void
  450. ohci_hub_descriptor (
  451. struct ohci_hcd *ohci,
  452. struct usb_hub_descriptor *desc
  453. ) {
  454. u32 rh = roothub_a (ohci);
  455. u16 temp;
  456. desc->bDescriptorType = 0x29;
  457. desc->bPwrOn2PwrGood = (rh & RH_A_POTPGT) >> 24;
  458. desc->bHubContrCurrent = 0;
  459. desc->bNbrPorts = ohci->num_ports;
  460. temp = 1 + (ohci->num_ports / 8);
  461. desc->bDescLength = 7 + 2 * temp;
  462. temp = 0;
  463. if (rh & RH_A_NPS) /* no power switching? */
  464. temp |= 0x0002;
  465. if (rh & RH_A_PSM) /* per-port power switching? */
  466. temp |= 0x0001;
  467. if (rh & RH_A_NOCP) /* no overcurrent reporting? */
  468. temp |= 0x0010;
  469. else if (rh & RH_A_OCPM) /* per-port overcurrent reporting? */
  470. temp |= 0x0008;
  471. desc->wHubCharacteristics = (__force __u16)cpu_to_hc16(ohci, temp);
  472. /* two bitmaps: ports removable, and usb 1.0 legacy PortPwrCtrlMask */
  473. rh = roothub_b (ohci);
  474. memset(desc->bitmap, 0xff, sizeof(desc->bitmap));
  475. desc->bitmap [0] = rh & RH_B_DR;
  476. if (ohci->num_ports > 7) {
  477. desc->bitmap [1] = (rh & RH_B_DR) >> 8;
  478. desc->bitmap [2] = 0xff;
  479. } else
  480. desc->bitmap [1] = 0xff;
  481. }
  482. /*-------------------------------------------------------------------------*/
  483. #ifdef CONFIG_USB_OTG
  484. static int ohci_start_port_reset (struct usb_hcd *hcd, unsigned port)
  485. {
  486. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  487. u32 status;
  488. if (!port)
  489. return -EINVAL;
  490. port--;
  491. /* start port reset before HNP protocol times out */
  492. status = ohci_readl(ohci, &ohci->regs->roothub.portstatus [port]);
  493. if (!(status & RH_PS_CCS))
  494. return -ENODEV;
  495. /* khubd will finish the reset later */
  496. ohci_writel(ohci, RH_PS_PRS, &ohci->regs->roothub.portstatus [port]);
  497. return 0;
  498. }
  499. static void start_hnp(struct ohci_hcd *ohci);
  500. #else
  501. #define ohci_start_port_reset NULL
  502. #endif
  503. /*-------------------------------------------------------------------------*/
  504. /* See usb 7.1.7.5: root hubs must issue at least 50 msec reset signaling,
  505. * not necessarily continuous ... to guard against resume signaling.
  506. * The short timeout is safe for non-root hubs, and is backward-compatible
  507. * with earlier Linux hosts.
  508. */
  509. #ifdef CONFIG_USB_SUSPEND
  510. #define PORT_RESET_MSEC 50
  511. #else
  512. #define PORT_RESET_MSEC 10
  513. #endif
  514. /* this timer value might be vendor-specific ... */
  515. #define PORT_RESET_HW_MSEC 10
  516. /* wrap-aware logic morphed from <linux/jiffies.h> */
  517. #define tick_before(t1,t2) ((s16)(((s16)(t1))-((s16)(t2))) < 0)
  518. /* called from some task, normally khubd */
  519. static inline int root_port_reset (struct ohci_hcd *ohci, unsigned port)
  520. {
  521. __hc32 __iomem *portstat = &ohci->regs->roothub.portstatus [port];
  522. u32 temp = 0;
  523. u16 now = ohci_readl(ohci, &ohci->regs->fmnumber);
  524. u16 reset_done = now + PORT_RESET_MSEC;
  525. int limit_1 = DIV_ROUND_UP(PORT_RESET_MSEC, PORT_RESET_HW_MSEC);
  526. /* build a "continuous enough" reset signal, with up to
  527. * 3msec gap between pulses. scheduler HZ==100 must work;
  528. * this might need to be deadline-scheduled.
  529. */
  530. do {
  531. int limit_2;
  532. /* spin until any current reset finishes */
  533. limit_2 = PORT_RESET_HW_MSEC * 2;
  534. while (--limit_2 >= 0) {
  535. temp = ohci_readl (ohci, portstat);
  536. /* handle e.g. CardBus eject */
  537. if (temp == ~(u32)0)
  538. return -ESHUTDOWN;
  539. if (!(temp & RH_PS_PRS))
  540. break;
  541. udelay (500);
  542. }
  543. /* timeout (a hardware error) has been observed when
  544. * EHCI sets CF while this driver is resetting a port;
  545. * presumably other disconnect paths might do it too.
  546. */
  547. if (limit_2 < 0) {
  548. ohci_dbg(ohci,
  549. "port[%d] reset timeout, stat %08x\n",
  550. port, temp);
  551. break;
  552. }
  553. if (!(temp & RH_PS_CCS))
  554. break;
  555. if (temp & RH_PS_PRSC)
  556. ohci_writel (ohci, RH_PS_PRSC, portstat);
  557. /* start the next reset, sleep till it's probably done */
  558. ohci_writel (ohci, RH_PS_PRS, portstat);
  559. msleep(PORT_RESET_HW_MSEC);
  560. now = ohci_readl(ohci, &ohci->regs->fmnumber);
  561. } while (tick_before(now, reset_done) && --limit_1 >= 0);
  562. /* caller synchronizes using PRSC ... and handles PRS
  563. * still being set when this returns.
  564. */
  565. return 0;
  566. }
  567. static int ohci_hub_control (
  568. struct usb_hcd *hcd,
  569. u16 typeReq,
  570. u16 wValue,
  571. u16 wIndex,
  572. char *buf,
  573. u16 wLength
  574. ) {
  575. struct ohci_hcd *ohci = hcd_to_ohci (hcd);
  576. int ports = hcd_to_bus (hcd)->root_hub->maxchild;
  577. u32 temp;
  578. int retval = 0;
  579. if (unlikely(!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)))
  580. return -ESHUTDOWN;
  581. switch (typeReq) {
  582. case ClearHubFeature:
  583. switch (wValue) {
  584. case C_HUB_OVER_CURRENT:
  585. ohci_writel (ohci, RH_HS_OCIC,
  586. &ohci->regs->roothub.status);
  587. case C_HUB_LOCAL_POWER:
  588. break;
  589. default:
  590. goto error;
  591. }
  592. break;
  593. case ClearPortFeature:
  594. if (!wIndex || wIndex > ports)
  595. goto error;
  596. wIndex--;
  597. switch (wValue) {
  598. case USB_PORT_FEAT_ENABLE:
  599. temp = RH_PS_CCS;
  600. break;
  601. case USB_PORT_FEAT_C_ENABLE:
  602. temp = RH_PS_PESC;
  603. break;
  604. case USB_PORT_FEAT_SUSPEND:
  605. temp = RH_PS_POCI;
  606. break;
  607. case USB_PORT_FEAT_C_SUSPEND:
  608. temp = RH_PS_PSSC;
  609. break;
  610. case USB_PORT_FEAT_POWER:
  611. temp = RH_PS_LSDA;
  612. break;
  613. case USB_PORT_FEAT_C_CONNECTION:
  614. temp = RH_PS_CSC;
  615. break;
  616. case USB_PORT_FEAT_C_OVER_CURRENT:
  617. temp = RH_PS_OCIC;
  618. break;
  619. case USB_PORT_FEAT_C_RESET:
  620. temp = RH_PS_PRSC;
  621. break;
  622. default:
  623. goto error;
  624. }
  625. ohci_writel (ohci, temp,
  626. &ohci->regs->roothub.portstatus [wIndex]);
  627. // ohci_readl (ohci, &ohci->regs->roothub.portstatus [wIndex]);
  628. break;
  629. case GetHubDescriptor:
  630. ohci_hub_descriptor (ohci, (struct usb_hub_descriptor *) buf);
  631. break;
  632. case GetHubStatus:
  633. temp = roothub_status (ohci) & ~(RH_HS_CRWE | RH_HS_DRWE);
  634. put_unaligned_le32(temp, buf);
  635. break;
  636. case GetPortStatus:
  637. if (!wIndex || wIndex > ports)
  638. goto error;
  639. wIndex--;
  640. temp = roothub_portstatus (ohci, wIndex);
  641. put_unaligned_le32(temp, buf);
  642. #ifndef OHCI_VERBOSE_DEBUG
  643. if (*(u16*)(buf+2)) /* only if wPortChange is interesting */
  644. #endif
  645. dbg_port (ohci, "GetStatus", wIndex, temp);
  646. break;
  647. case SetHubFeature:
  648. switch (wValue) {
  649. case C_HUB_OVER_CURRENT:
  650. // FIXME: this can be cleared, yes?
  651. case C_HUB_LOCAL_POWER:
  652. break;
  653. default:
  654. goto error;
  655. }
  656. break;
  657. case SetPortFeature:
  658. if (!wIndex || wIndex > ports)
  659. goto error;
  660. wIndex--;
  661. switch (wValue) {
  662. case USB_PORT_FEAT_SUSPEND:
  663. #ifdef CONFIG_USB_OTG
  664. if (hcd->self.otg_port == (wIndex + 1)
  665. && hcd->self.b_hnp_enable)
  666. start_hnp(ohci);
  667. else
  668. #endif
  669. ohci_writel (ohci, RH_PS_PSS,
  670. &ohci->regs->roothub.portstatus [wIndex]);
  671. break;
  672. case USB_PORT_FEAT_POWER:
  673. ohci_writel (ohci, RH_PS_PPS,
  674. &ohci->regs->roothub.portstatus [wIndex]);
  675. break;
  676. case USB_PORT_FEAT_RESET:
  677. retval = root_port_reset (ohci, wIndex);
  678. break;
  679. default:
  680. goto error;
  681. }
  682. break;
  683. default:
  684. error:
  685. /* "protocol stall" on error */
  686. retval = -EPIPE;
  687. }
  688. return retval;
  689. }