hcd-pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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_each_pci_dev(companion) {
  57. if (companion->bus != pdev->bus ||
  58. PCI_SLOT(companion->devfn) != slot)
  59. continue;
  60. companion_hcd = pci_get_drvdata(companion);
  61. if (!companion_hcd)
  62. continue;
  63. /* For SET_HS_COMPANION, store a pointer to the EHCI bus in
  64. * the OHCI/UHCI companion bus structure.
  65. * For CLEAR_HS_COMPANION, clear the pointer to the EHCI bus
  66. * in the OHCI/UHCI companion bus structure.
  67. * For WAIT_FOR_COMPANIONS, wait until the OHCI/UHCI
  68. * companion controllers have fully resumed.
  69. */
  70. if ((pdev->class == CL_OHCI || pdev->class == CL_UHCI) &&
  71. companion->class == CL_EHCI) {
  72. /* action must be SET_HS_COMPANION */
  73. dev_dbg(&companion->dev, "HS companion for %s\n",
  74. dev_name(&pdev->dev));
  75. hcd->self.hs_companion = &companion_hcd->self;
  76. } else if (pdev->class == CL_EHCI &&
  77. (companion->class == CL_OHCI ||
  78. companion->class == CL_UHCI)) {
  79. switch (action) {
  80. case SET_HS_COMPANION:
  81. dev_dbg(&pdev->dev, "HS companion for %s\n",
  82. dev_name(&companion->dev));
  83. companion_hcd->self.hs_companion = &hcd->self;
  84. break;
  85. case CLEAR_HS_COMPANION:
  86. companion_hcd->self.hs_companion = NULL;
  87. break;
  88. case WAIT_FOR_COMPANIONS:
  89. device_pm_wait_for_dev(&pdev->dev,
  90. &companion->dev);
  91. break;
  92. }
  93. }
  94. }
  95. }
  96. static void set_hs_companion(struct pci_dev *pdev, struct usb_hcd *hcd)
  97. {
  98. mutex_lock(&companions_mutex);
  99. dev_set_drvdata(&pdev->dev, hcd);
  100. companion_common(pdev, hcd, SET_HS_COMPANION);
  101. mutex_unlock(&companions_mutex);
  102. }
  103. static void clear_hs_companion(struct pci_dev *pdev, struct usb_hcd *hcd)
  104. {
  105. mutex_lock(&companions_mutex);
  106. dev_set_drvdata(&pdev->dev, NULL);
  107. /* If pdev is OHCI or UHCI, just clear its hs_companion pointer */
  108. if (pdev->class == CL_OHCI || pdev->class == CL_UHCI)
  109. hcd->self.hs_companion = NULL;
  110. /* Otherwise search for companion buses and clear their pointers */
  111. else
  112. companion_common(pdev, hcd, CLEAR_HS_COMPANION);
  113. mutex_unlock(&companions_mutex);
  114. }
  115. static void wait_for_companions(struct pci_dev *pdev, struct usb_hcd *hcd)
  116. {
  117. /* Only EHCI controllers need to wait.
  118. * No locking is needed because a controller cannot be resumed
  119. * while one of its companions is getting unbound.
  120. */
  121. if (pdev->class == CL_EHCI)
  122. companion_common(pdev, hcd, WAIT_FOR_COMPANIONS);
  123. }
  124. #else /* !CONFIG_PM_SLEEP */
  125. static inline void set_hs_companion(struct pci_dev *d, struct usb_hcd *h) {}
  126. static inline void clear_hs_companion(struct pci_dev *d, struct usb_hcd *h) {}
  127. static inline void wait_for_companions(struct pci_dev *d, struct usb_hcd *h) {}
  128. #endif /* !CONFIG_PM_SLEEP */
  129. /*-------------------------------------------------------------------------*/
  130. /* configure so an HC device and id are always provided */
  131. /* always called with process context; sleeping is OK */
  132. /**
  133. * usb_hcd_pci_probe - initialize PCI-based HCDs
  134. * @dev: USB Host Controller being probed
  135. * @id: pci hotplug id connecting controller to HCD framework
  136. * Context: !in_interrupt()
  137. *
  138. * Allocates basic PCI resources for this USB host controller, and
  139. * then invokes the start() method for the HCD associated with it
  140. * through the hotplug entry's driver_data.
  141. *
  142. * Store this function in the HCD's struct pci_driver as probe().
  143. */
  144. int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
  145. {
  146. struct hc_driver *driver;
  147. struct usb_hcd *hcd;
  148. int retval;
  149. if (usb_disabled())
  150. return -ENODEV;
  151. if (!id)
  152. return -EINVAL;
  153. driver = (struct hc_driver *)id->driver_data;
  154. if (!driver)
  155. return -EINVAL;
  156. if (pci_enable_device(dev) < 0)
  157. return -ENODEV;
  158. dev->current_state = PCI_D0;
  159. if (!dev->irq) {
  160. dev_err(&dev->dev,
  161. "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
  162. pci_name(dev));
  163. retval = -ENODEV;
  164. goto err1;
  165. }
  166. hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
  167. if (!hcd) {
  168. retval = -ENOMEM;
  169. goto err1;
  170. }
  171. if (driver->flags & HCD_MEMORY) {
  172. /* EHCI, OHCI */
  173. hcd->rsrc_start = pci_resource_start(dev, 0);
  174. hcd->rsrc_len = pci_resource_len(dev, 0);
  175. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  176. driver->description)) {
  177. dev_dbg(&dev->dev, "controller already in use\n");
  178. retval = -EBUSY;
  179. goto err2;
  180. }
  181. hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
  182. if (hcd->regs == NULL) {
  183. dev_dbg(&dev->dev, "error mapping memory\n");
  184. retval = -EFAULT;
  185. goto err3;
  186. }
  187. } else {
  188. /* UHCI */
  189. int region;
  190. for (region = 0; region < PCI_ROM_RESOURCE; region++) {
  191. if (!(pci_resource_flags(dev, region) &
  192. IORESOURCE_IO))
  193. continue;
  194. hcd->rsrc_start = pci_resource_start(dev, region);
  195. hcd->rsrc_len = pci_resource_len(dev, region);
  196. if (request_region(hcd->rsrc_start, hcd->rsrc_len,
  197. driver->description))
  198. break;
  199. }
  200. if (region == PCI_ROM_RESOURCE) {
  201. dev_dbg(&dev->dev, "no i/o regions available\n");
  202. retval = -EBUSY;
  203. goto err2;
  204. }
  205. }
  206. pci_set_master(dev);
  207. retval = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED);
  208. if (retval != 0)
  209. goto err4;
  210. set_hs_companion(dev, hcd);
  211. if (pci_dev_run_wake(dev))
  212. pm_runtime_put_noidle(&dev->dev);
  213. return retval;
  214. err4:
  215. if (driver->flags & HCD_MEMORY) {
  216. iounmap(hcd->regs);
  217. err3:
  218. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  219. } else
  220. release_region(hcd->rsrc_start, hcd->rsrc_len);
  221. err2:
  222. clear_hs_companion(dev, hcd);
  223. usb_put_hcd(hcd);
  224. err1:
  225. pci_disable_device(dev);
  226. dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
  227. return retval;
  228. }
  229. EXPORT_SYMBOL_GPL(usb_hcd_pci_probe);
  230. /* may be called without controller electrically present */
  231. /* may be called with controller, bus, and devices active */
  232. /**
  233. * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
  234. * @dev: USB Host Controller being removed
  235. * Context: !in_interrupt()
  236. *
  237. * Reverses the effect of usb_hcd_pci_probe(), first invoking
  238. * the HCD's stop() method. It is always called from a thread
  239. * context, normally "rmmod", "apmd", or something similar.
  240. *
  241. * Store this function in the HCD's struct pci_driver as remove().
  242. */
  243. void usb_hcd_pci_remove(struct pci_dev *dev)
  244. {
  245. struct usb_hcd *hcd;
  246. hcd = pci_get_drvdata(dev);
  247. if (!hcd)
  248. return;
  249. if (pci_dev_run_wake(dev))
  250. pm_runtime_get_noresume(&dev->dev);
  251. /* Fake an interrupt request in order to give the driver a chance
  252. * to test whether the controller hardware has been removed (e.g.,
  253. * cardbus physical eject).
  254. */
  255. local_irq_disable();
  256. usb_hcd_irq(0, hcd);
  257. local_irq_enable();
  258. usb_remove_hcd(hcd);
  259. if (hcd->driver->flags & HCD_MEMORY) {
  260. iounmap(hcd->regs);
  261. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  262. } else {
  263. release_region(hcd->rsrc_start, hcd->rsrc_len);
  264. }
  265. clear_hs_companion(dev, hcd);
  266. usb_put_hcd(hcd);
  267. pci_disable_device(dev);
  268. }
  269. EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
  270. /**
  271. * usb_hcd_pci_shutdown - shutdown host controller
  272. * @dev: USB Host Controller being shutdown
  273. */
  274. void usb_hcd_pci_shutdown(struct pci_dev *dev)
  275. {
  276. struct usb_hcd *hcd;
  277. hcd = pci_get_drvdata(dev);
  278. if (!hcd)
  279. return;
  280. if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) &&
  281. hcd->driver->shutdown)
  282. hcd->driver->shutdown(hcd);
  283. }
  284. EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
  285. #ifdef CONFIG_PM_OPS
  286. #ifdef CONFIG_PPC_PMAC
  287. static void powermac_set_asic(struct pci_dev *pci_dev, int enable)
  288. {
  289. /* Enanble or disable ASIC clocks for USB */
  290. if (machine_is(powermac)) {
  291. struct device_node *of_node;
  292. of_node = pci_device_to_OF_node(pci_dev);
  293. if (of_node)
  294. pmac_call_feature(PMAC_FTR_USB_ENABLE,
  295. of_node, 0, enable);
  296. }
  297. }
  298. #else
  299. static inline void powermac_set_asic(struct pci_dev *pci_dev, int enable)
  300. {}
  301. #endif /* CONFIG_PPC_PMAC */
  302. static int check_root_hub_suspended(struct device *dev)
  303. {
  304. struct pci_dev *pci_dev = to_pci_dev(dev);
  305. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  306. if (!(hcd->state == HC_STATE_SUSPENDED ||
  307. hcd->state == HC_STATE_HALT)) {
  308. dev_warn(dev, "Root hub is not suspended\n");
  309. return -EBUSY;
  310. }
  311. return 0;
  312. }
  313. static int suspend_common(struct device *dev, bool do_wakeup)
  314. {
  315. struct pci_dev *pci_dev = to_pci_dev(dev);
  316. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  317. int retval;
  318. /* Root hub suspend should have stopped all downstream traffic,
  319. * and all bus master traffic. And done so for both the interface
  320. * and the stub usb_device (which we check here). But maybe it
  321. * didn't; writing sysfs power/state files ignores such rules...
  322. */
  323. retval = check_root_hub_suspended(dev);
  324. if (retval)
  325. return retval;
  326. if (hcd->driver->pci_suspend) {
  327. /* Optimization: Don't suspend if a root-hub wakeup is
  328. * pending and it would cause the HCD to wake up anyway.
  329. */
  330. if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
  331. return -EBUSY;
  332. retval = hcd->driver->pci_suspend(hcd, do_wakeup);
  333. suspend_report_result(hcd->driver->pci_suspend, retval);
  334. /* Check again in case wakeup raced with pci_suspend */
  335. if (retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
  336. if (hcd->driver->pci_resume)
  337. hcd->driver->pci_resume(hcd, false);
  338. retval = -EBUSY;
  339. }
  340. if (retval)
  341. return retval;
  342. }
  343. synchronize_irq(pci_dev->irq);
  344. /* Downstream ports from this root hub should already be quiesced, so
  345. * there will be no DMA activity. Now we can shut down the upstream
  346. * link (except maybe for PME# resume signaling). We'll enter a
  347. * low power state during suspend_noirq, if the hardware allows.
  348. */
  349. pci_disable_device(pci_dev);
  350. return retval;
  351. }
  352. static int resume_common(struct device *dev, int event)
  353. {
  354. struct pci_dev *pci_dev = to_pci_dev(dev);
  355. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  356. int retval;
  357. if (hcd->state != HC_STATE_SUSPENDED) {
  358. dev_dbg(dev, "can't resume, not suspended!\n");
  359. return 0;
  360. }
  361. retval = pci_enable_device(pci_dev);
  362. if (retval < 0) {
  363. dev_err(dev, "can't re-enable after resume, %d!\n", retval);
  364. return retval;
  365. }
  366. pci_set_master(pci_dev);
  367. clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  368. if (hcd->driver->pci_resume) {
  369. if (event != PM_EVENT_AUTO_RESUME)
  370. wait_for_companions(pci_dev, hcd);
  371. retval = hcd->driver->pci_resume(hcd,
  372. event == PM_EVENT_RESTORE);
  373. if (retval) {
  374. dev_err(dev, "PCI post-resume error %d!\n", retval);
  375. usb_hc_died(hcd);
  376. }
  377. }
  378. return retval;
  379. }
  380. #ifdef CONFIG_PM_SLEEP
  381. static int hcd_pci_suspend(struct device *dev)
  382. {
  383. return suspend_common(dev, device_may_wakeup(dev));
  384. }
  385. static int hcd_pci_suspend_noirq(struct device *dev)
  386. {
  387. struct pci_dev *pci_dev = to_pci_dev(dev);
  388. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  389. int retval;
  390. retval = check_root_hub_suspended(dev);
  391. if (retval)
  392. return retval;
  393. pci_save_state(pci_dev);
  394. /* If the root hub is HALTed rather than SUSPENDed,
  395. * disallow remote wakeup.
  396. */
  397. if (hcd->state == HC_STATE_HALT)
  398. device_set_wakeup_enable(dev, 0);
  399. dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
  400. /* Possibly enable remote wakeup,
  401. * choose the appropriate low-power state, and go to that state.
  402. */
  403. retval = pci_prepare_to_sleep(pci_dev);
  404. if (retval == -EIO) { /* Low-power not supported */
  405. dev_dbg(dev, "--> PCI D0 legacy\n");
  406. retval = 0;
  407. } else if (retval == 0) {
  408. dev_dbg(dev, "--> PCI %s\n",
  409. pci_power_name(pci_dev->current_state));
  410. } else {
  411. suspend_report_result(pci_prepare_to_sleep, retval);
  412. return retval;
  413. }
  414. powermac_set_asic(pci_dev, 0);
  415. return retval;
  416. }
  417. static int hcd_pci_resume_noirq(struct device *dev)
  418. {
  419. struct pci_dev *pci_dev = to_pci_dev(dev);
  420. powermac_set_asic(pci_dev, 1);
  421. /* Go back to D0 and disable remote wakeup */
  422. pci_back_from_sleep(pci_dev);
  423. return 0;
  424. }
  425. static int hcd_pci_resume(struct device *dev)
  426. {
  427. return resume_common(dev, PM_EVENT_RESUME);
  428. }
  429. static int hcd_pci_restore(struct device *dev)
  430. {
  431. return resume_common(dev, PM_EVENT_RESTORE);
  432. }
  433. #else
  434. #define hcd_pci_suspend NULL
  435. #define hcd_pci_suspend_noirq NULL
  436. #define hcd_pci_resume_noirq NULL
  437. #define hcd_pci_resume NULL
  438. #define hcd_pci_restore NULL
  439. #endif /* CONFIG_PM_SLEEP */
  440. #ifdef CONFIG_PM_RUNTIME
  441. static int hcd_pci_runtime_suspend(struct device *dev)
  442. {
  443. int retval;
  444. retval = suspend_common(dev, true);
  445. if (retval == 0)
  446. powermac_set_asic(to_pci_dev(dev), 0);
  447. dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
  448. return retval;
  449. }
  450. static int hcd_pci_runtime_resume(struct device *dev)
  451. {
  452. int retval;
  453. powermac_set_asic(to_pci_dev(dev), 1);
  454. retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
  455. dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
  456. return retval;
  457. }
  458. #else
  459. #define hcd_pci_runtime_suspend NULL
  460. #define hcd_pci_runtime_resume NULL
  461. #endif /* CONFIG_PM_RUNTIME */
  462. const struct dev_pm_ops usb_hcd_pci_pm_ops = {
  463. .suspend = hcd_pci_suspend,
  464. .suspend_noirq = hcd_pci_suspend_noirq,
  465. .resume_noirq = hcd_pci_resume_noirq,
  466. .resume = hcd_pci_resume,
  467. .freeze = check_root_hub_suspended,
  468. .freeze_noirq = check_root_hub_suspended,
  469. .thaw_noirq = NULL,
  470. .thaw = NULL,
  471. .poweroff = hcd_pci_suspend,
  472. .poweroff_noirq = hcd_pci_suspend_noirq,
  473. .restore_noirq = hcd_pci_resume_noirq,
  474. .restore = hcd_pci_restore,
  475. .runtime_suspend = hcd_pci_runtime_suspend,
  476. .runtime_resume = hcd_pci_runtime_resume,
  477. };
  478. EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
  479. #endif /* CONFIG_PM_OPS */