hcd-pci.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * (C) Copyright David Brownell 2000-2002
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the
  6. * Free Software Foundation; either version 2 of the License, or (at your
  7. * option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/usb.h>
  23. #include <linux/usb/hcd.h>
  24. #include <asm/io.h>
  25. #include <asm/irq.h>
  26. #ifdef CONFIG_PPC_PMAC
  27. #include <asm/machdep.h>
  28. #include <asm/pmac_feature.h>
  29. #include <asm/pci-bridge.h>
  30. #include <asm/prom.h>
  31. #endif
  32. #include "usb.h"
  33. /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
  34. #ifdef CONFIG_PM_SLEEP
  35. /* Coordinate handoffs between EHCI and companion controllers
  36. * during system resume
  37. */
  38. static DEFINE_MUTEX(companions_mutex);
  39. #define CL_UHCI PCI_CLASS_SERIAL_USB_UHCI
  40. #define CL_OHCI PCI_CLASS_SERIAL_USB_OHCI
  41. #define CL_EHCI PCI_CLASS_SERIAL_USB_EHCI
  42. enum companion_action {
  43. SET_HS_COMPANION, CLEAR_HS_COMPANION, WAIT_FOR_COMPANIONS
  44. };
  45. static void companion_common(struct pci_dev *pdev, struct usb_hcd *hcd,
  46. enum companion_action action)
  47. {
  48. struct pci_dev *companion;
  49. struct usb_hcd *companion_hcd;
  50. unsigned int slot = PCI_SLOT(pdev->devfn);
  51. /* Iterate through other PCI functions in the same slot.
  52. * If pdev is OHCI or UHCI then we are looking for EHCI, and
  53. * vice versa.
  54. */
  55. companion = NULL;
  56. for (;;) {
  57. companion = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, companion);
  58. if (!companion)
  59. break;
  60. if (companion->bus != pdev->bus ||
  61. PCI_SLOT(companion->devfn) != slot)
  62. continue;
  63. companion_hcd = pci_get_drvdata(companion);
  64. if (!companion_hcd)
  65. continue;
  66. /* For SET_HS_COMPANION, store a pointer to the EHCI bus in
  67. * the OHCI/UHCI companion bus structure.
  68. * For CLEAR_HS_COMPANION, clear the pointer to the EHCI bus
  69. * in the OHCI/UHCI companion bus structure.
  70. * For WAIT_FOR_COMPANIONS, wait until the OHCI/UHCI
  71. * companion controllers have fully resumed.
  72. */
  73. if ((pdev->class == CL_OHCI || pdev->class == CL_UHCI) &&
  74. companion->class == CL_EHCI) {
  75. /* action must be SET_HS_COMPANION */
  76. dev_dbg(&companion->dev, "HS companion for %s\n",
  77. dev_name(&pdev->dev));
  78. hcd->self.hs_companion = &companion_hcd->self;
  79. } else if (pdev->class == CL_EHCI &&
  80. (companion->class == CL_OHCI ||
  81. companion->class == CL_UHCI)) {
  82. switch (action) {
  83. case SET_HS_COMPANION:
  84. dev_dbg(&pdev->dev, "HS companion for %s\n",
  85. dev_name(&companion->dev));
  86. companion_hcd->self.hs_companion = &hcd->self;
  87. break;
  88. case CLEAR_HS_COMPANION:
  89. companion_hcd->self.hs_companion = NULL;
  90. break;
  91. case WAIT_FOR_COMPANIONS:
  92. device_pm_wait_for_dev(&pdev->dev,
  93. &companion->dev);
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. static void set_hs_companion(struct pci_dev *pdev, struct usb_hcd *hcd)
  100. {
  101. mutex_lock(&companions_mutex);
  102. dev_set_drvdata(&pdev->dev, hcd);
  103. companion_common(pdev, hcd, SET_HS_COMPANION);
  104. mutex_unlock(&companions_mutex);
  105. }
  106. static void clear_hs_companion(struct pci_dev *pdev, struct usb_hcd *hcd)
  107. {
  108. mutex_lock(&companions_mutex);
  109. dev_set_drvdata(&pdev->dev, NULL);
  110. /* If pdev is OHCI or UHCI, just clear its hs_companion pointer */
  111. if (pdev->class == CL_OHCI || pdev->class == CL_UHCI)
  112. hcd->self.hs_companion = NULL;
  113. /* Otherwise search for companion buses and clear their pointers */
  114. else
  115. companion_common(pdev, hcd, CLEAR_HS_COMPANION);
  116. mutex_unlock(&companions_mutex);
  117. }
  118. static void wait_for_companions(struct pci_dev *pdev, struct usb_hcd *hcd)
  119. {
  120. /* Only EHCI controllers need to wait.
  121. * No locking is needed because a controller cannot be resumed
  122. * while one of its companions is getting unbound.
  123. */
  124. if (pdev->class == CL_EHCI)
  125. companion_common(pdev, hcd, WAIT_FOR_COMPANIONS);
  126. }
  127. #else /* !CONFIG_PM_SLEEP */
  128. static inline void set_hs_companion(struct pci_dev *d, struct usb_hcd *h) {}
  129. static inline void clear_hs_companion(struct pci_dev *d, struct usb_hcd *h) {}
  130. static inline void wait_for_companions(struct pci_dev *d, struct usb_hcd *h) {}
  131. #endif /* !CONFIG_PM_SLEEP */
  132. /*-------------------------------------------------------------------------*/
  133. /* configure so an HC device and id are always provided */
  134. /* always called with process context; sleeping is OK */
  135. /**
  136. * usb_hcd_pci_probe - initialize PCI-based HCDs
  137. * @dev: USB Host Controller being probed
  138. * @id: pci hotplug id connecting controller to HCD framework
  139. * Context: !in_interrupt()
  140. *
  141. * Allocates basic PCI resources for this USB host controller, and
  142. * then invokes the start() method for the HCD associated with it
  143. * through the hotplug entry's driver_data.
  144. *
  145. * Store this function in the HCD's struct pci_driver as probe().
  146. */
  147. int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  148. {
  149. struct hc_driver *driver;
  150. struct usb_hcd *hcd;
  151. int retval;
  152. if (usb_disabled())
  153. return -ENODEV;
  154. if (!id)
  155. return -EINVAL;
  156. driver = (struct hc_driver *)id->driver_data;
  157. if (!driver)
  158. return -EINVAL;
  159. if (pci_enable_device(dev) < 0)
  160. return -ENODEV;
  161. dev->current_state = PCI_D0;
  162. if (!dev->irq) {
  163. dev_err(&dev->dev,
  164. "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
  165. pci_name(dev));
  166. retval = -ENODEV;
  167. goto err1;
  168. }
  169. hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
  170. if (!hcd) {
  171. retval = -ENOMEM;
  172. goto err1;
  173. }
  174. if (driver->flags & HCD_MEMORY) {
  175. /* EHCI, OHCI */
  176. hcd->rsrc_start = pci_resource_start(dev, 0);
  177. hcd->rsrc_len = pci_resource_len(dev, 0);
  178. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  179. driver->description)) {
  180. dev_dbg(&dev->dev, "controller already in use\n");
  181. retval = -EBUSY;
  182. goto err2;
  183. }
  184. hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
  185. if (hcd->regs == NULL) {
  186. dev_dbg(&dev->dev, "error mapping memory\n");
  187. retval = -EFAULT;
  188. goto err3;
  189. }
  190. } else {
  191. /* UHCI */
  192. int region;
  193. for (region = 0; region < PCI_ROM_RESOURCE; region++) {
  194. if (!(pci_resource_flags(dev, region) &
  195. IORESOURCE_IO))
  196. continue;
  197. hcd->rsrc_start = pci_resource_start(dev, region);
  198. hcd->rsrc_len = pci_resource_len(dev, region);
  199. if (request_region(hcd->rsrc_start, hcd->rsrc_len,
  200. driver->description))
  201. break;
  202. }
  203. if (region == PCI_ROM_RESOURCE) {
  204. dev_dbg(&dev->dev, "no i/o regions available\n");
  205. retval = -EBUSY;
  206. goto err2;
  207. }
  208. }
  209. pci_set_master(dev);
  210. retval = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED);
  211. if (retval != 0)
  212. goto err4;
  213. set_hs_companion(dev, hcd);
  214. return retval;
  215. err4:
  216. if (driver->flags & HCD_MEMORY) {
  217. iounmap(hcd->regs);
  218. err3:
  219. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  220. } else
  221. release_region(hcd->rsrc_start, hcd->rsrc_len);
  222. err2:
  223. clear_hs_companion(dev, hcd);
  224. usb_put_hcd(hcd);
  225. err1:
  226. pci_disable_device(dev);
  227. dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
  228. return retval;
  229. }
  230. EXPORT_SYMBOL_GPL(usb_hcd_pci_probe);
  231. /* may be called without controller electrically present */
  232. /* may be called with controller, bus, and devices active */
  233. /**
  234. * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
  235. * @dev: USB Host Controller being removed
  236. * Context: !in_interrupt()
  237. *
  238. * Reverses the effect of usb_hcd_pci_probe(), first invoking
  239. * the HCD's stop() method. It is always called from a thread
  240. * context, normally "rmmod", "apmd", or something similar.
  241. *
  242. * Store this function in the HCD's struct pci_driver as remove().
  243. */
  244. void usb_hcd_pci_remove(struct pci_dev *dev)
  245. {
  246. struct usb_hcd *hcd;
  247. hcd = pci_get_drvdata(dev);
  248. if (!hcd)
  249. return;
  250. usb_remove_hcd(hcd);
  251. if (hcd->driver->flags & HCD_MEMORY) {
  252. iounmap(hcd->regs);
  253. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  254. } else {
  255. release_region(hcd->rsrc_start, hcd->rsrc_len);
  256. }
  257. clear_hs_companion(dev, hcd);
  258. usb_put_hcd(hcd);
  259. pci_disable_device(dev);
  260. }
  261. EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
  262. /**
  263. * usb_hcd_pci_shutdown - shutdown host controller
  264. * @dev: USB Host Controller being shutdown
  265. */
  266. void usb_hcd_pci_shutdown(struct pci_dev *dev)
  267. {
  268. struct usb_hcd *hcd;
  269. hcd = pci_get_drvdata(dev);
  270. if (!hcd)
  271. return;
  272. if (hcd->driver->shutdown)
  273. hcd->driver->shutdown(hcd);
  274. }
  275. EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
  276. #ifdef CONFIG_PM_SLEEP
  277. static int check_root_hub_suspended(struct device *dev)
  278. {
  279. struct pci_dev *pci_dev = to_pci_dev(dev);
  280. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  281. if (!(hcd->state == HC_STATE_SUSPENDED ||
  282. hcd->state == HC_STATE_HALT)) {
  283. dev_warn(dev, "Root hub is not suspended\n");
  284. return -EBUSY;
  285. }
  286. return 0;
  287. }
  288. static int hcd_pci_suspend(struct device *dev)
  289. {
  290. struct pci_dev *pci_dev = to_pci_dev(dev);
  291. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  292. int retval;
  293. /* Root hub suspend should have stopped all downstream traffic,
  294. * and all bus master traffic. And done so for both the interface
  295. * and the stub usb_device (which we check here). But maybe it
  296. * didn't; writing sysfs power/state files ignores such rules...
  297. */
  298. retval = check_root_hub_suspended(dev);
  299. if (retval)
  300. return retval;
  301. /* We might already be suspended (runtime PM -- not yet written) */
  302. if (pci_dev->current_state != PCI_D0)
  303. return retval;
  304. if (hcd->driver->pci_suspend) {
  305. retval = hcd->driver->pci_suspend(hcd);
  306. suspend_report_result(hcd->driver->pci_suspend, retval);
  307. if (retval)
  308. return retval;
  309. }
  310. synchronize_irq(pci_dev->irq);
  311. /* Downstream ports from this root hub should already be quiesced, so
  312. * there will be no DMA activity. Now we can shut down the upstream
  313. * link (except maybe for PME# resume signaling). We'll enter a
  314. * low power state during suspend_noirq, if the hardware allows.
  315. */
  316. pci_disable_device(pci_dev);
  317. return retval;
  318. }
  319. static int hcd_pci_suspend_noirq(struct device *dev)
  320. {
  321. struct pci_dev *pci_dev = to_pci_dev(dev);
  322. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  323. int retval;
  324. retval = check_root_hub_suspended(dev);
  325. if (retval)
  326. return retval;
  327. pci_save_state(pci_dev);
  328. /* If the root hub is HALTed rather than SUSPENDed,
  329. * disallow remote wakeup.
  330. */
  331. if (hcd->state == HC_STATE_HALT)
  332. device_set_wakeup_enable(dev, 0);
  333. dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
  334. /* Possibly enable remote wakeup,
  335. * choose the appropriate low-power state, and go to that state.
  336. */
  337. retval = pci_prepare_to_sleep(pci_dev);
  338. if (retval == -EIO) { /* Low-power not supported */
  339. dev_dbg(dev, "--> PCI D0 legacy\n");
  340. retval = 0;
  341. } else if (retval == 0) {
  342. dev_dbg(dev, "--> PCI %s\n",
  343. pci_power_name(pci_dev->current_state));
  344. } else {
  345. suspend_report_result(pci_prepare_to_sleep, retval);
  346. return retval;
  347. }
  348. #ifdef CONFIG_PPC_PMAC
  349. /* Disable ASIC clocks for USB */
  350. if (machine_is(powermac)) {
  351. struct device_node *of_node;
  352. of_node = pci_device_to_OF_node(pci_dev);
  353. if (of_node)
  354. pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 0);
  355. }
  356. #endif
  357. return retval;
  358. }
  359. static int hcd_pci_resume_noirq(struct device *dev)
  360. {
  361. struct pci_dev *pci_dev = to_pci_dev(dev);
  362. #ifdef CONFIG_PPC_PMAC
  363. /* Reenable ASIC clocks for USB */
  364. if (machine_is(powermac)) {
  365. struct device_node *of_node;
  366. of_node = pci_device_to_OF_node(pci_dev);
  367. if (of_node)
  368. pmac_call_feature(PMAC_FTR_USB_ENABLE,
  369. of_node, 0, 1);
  370. }
  371. #endif
  372. /* Go back to D0 and disable remote wakeup */
  373. pci_back_from_sleep(pci_dev);
  374. return 0;
  375. }
  376. static int resume_common(struct device *dev, bool hibernated)
  377. {
  378. struct pci_dev *pci_dev = to_pci_dev(dev);
  379. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  380. int retval;
  381. if (hcd->state != HC_STATE_SUSPENDED) {
  382. dev_dbg(dev, "can't resume, not suspended!\n");
  383. return 0;
  384. }
  385. retval = pci_enable_device(pci_dev);
  386. if (retval < 0) {
  387. dev_err(dev, "can't re-enable after resume, %d!\n", retval);
  388. return retval;
  389. }
  390. pci_set_master(pci_dev);
  391. clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  392. if (hcd->driver->pci_resume) {
  393. /* This call should be made only during system resume,
  394. * not during runtime resume.
  395. */
  396. wait_for_companions(pci_dev, hcd);
  397. retval = hcd->driver->pci_resume(hcd, hibernated);
  398. if (retval) {
  399. dev_err(dev, "PCI post-resume error %d!\n", retval);
  400. usb_hc_died(hcd);
  401. }
  402. }
  403. return retval;
  404. }
  405. static int hcd_pci_resume(struct device *dev)
  406. {
  407. return resume_common(dev, false);
  408. }
  409. static int hcd_pci_restore(struct device *dev)
  410. {
  411. return resume_common(dev, true);
  412. }
  413. const struct dev_pm_ops usb_hcd_pci_pm_ops = {
  414. .suspend = hcd_pci_suspend,
  415. .suspend_noirq = hcd_pci_suspend_noirq,
  416. .resume_noirq = hcd_pci_resume_noirq,
  417. .resume = hcd_pci_resume,
  418. .freeze = check_root_hub_suspended,
  419. .freeze_noirq = check_root_hub_suspended,
  420. .thaw_noirq = NULL,
  421. .thaw = NULL,
  422. .poweroff = hcd_pci_suspend,
  423. .poweroff_noirq = hcd_pci_suspend_noirq,
  424. .restore_noirq = hcd_pci_resume_noirq,
  425. .restore = hcd_pci_restore,
  426. };
  427. EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
  428. #endif /* CONFIG_PM_SLEEP */