hcd-pci.c 16 KB

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