hcd-pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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_DISABLED | 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. static int suspend_common(struct device *dev, bool do_wakeup)
  324. {
  325. struct pci_dev *pci_dev = to_pci_dev(dev);
  326. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  327. int retval;
  328. /* Root hub suspend should have stopped all downstream traffic,
  329. * and all bus master traffic. And done so for both the interface
  330. * and the stub usb_device (which we check here). But maybe it
  331. * didn't; writing sysfs power/state files ignores such rules...
  332. */
  333. retval = check_root_hub_suspended(dev);
  334. if (retval)
  335. return retval;
  336. if (hcd->driver->pci_suspend && !HCD_DEAD(hcd)) {
  337. /* Optimization: Don't suspend if a root-hub wakeup is
  338. * pending and it would cause the HCD to wake up anyway.
  339. */
  340. if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
  341. return -EBUSY;
  342. if (do_wakeup && hcd->shared_hcd &&
  343. HCD_WAKEUP_PENDING(hcd->shared_hcd))
  344. return -EBUSY;
  345. retval = hcd->driver->pci_suspend(hcd, do_wakeup);
  346. suspend_report_result(hcd->driver->pci_suspend, retval);
  347. /* Check again in case wakeup raced with pci_suspend */
  348. if ((retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) ||
  349. (retval == 0 && do_wakeup && hcd->shared_hcd &&
  350. HCD_WAKEUP_PENDING(hcd->shared_hcd))) {
  351. if (hcd->driver->pci_resume)
  352. hcd->driver->pci_resume(hcd, false);
  353. retval = -EBUSY;
  354. }
  355. if (retval)
  356. return retval;
  357. }
  358. /* If MSI-X is enabled, the driver will have synchronized all vectors
  359. * in pci_suspend(). If MSI or legacy PCI is enabled, that will be
  360. * synchronized here.
  361. */
  362. if (!hcd->msix_enabled)
  363. synchronize_irq(pci_dev->irq);
  364. /* Downstream ports from this root hub should already be quiesced, so
  365. * there will be no DMA activity. Now we can shut down the upstream
  366. * link (except maybe for PME# resume signaling). We'll enter a
  367. * low power state during suspend_noirq, if the hardware allows.
  368. */
  369. pci_disable_device(pci_dev);
  370. return retval;
  371. }
  372. static int resume_common(struct device *dev, int event)
  373. {
  374. struct pci_dev *pci_dev = to_pci_dev(dev);
  375. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  376. int retval;
  377. if (HCD_RH_RUNNING(hcd) ||
  378. (hcd->shared_hcd &&
  379. HCD_RH_RUNNING(hcd->shared_hcd))) {
  380. dev_dbg(dev, "can't resume, not suspended!\n");
  381. return 0;
  382. }
  383. retval = pci_enable_device(pci_dev);
  384. if (retval < 0) {
  385. dev_err(dev, "can't re-enable after resume, %d!\n", retval);
  386. return retval;
  387. }
  388. pci_set_master(pci_dev);
  389. clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  390. if (hcd->shared_hcd)
  391. clear_bit(HCD_FLAG_SAW_IRQ, &hcd->shared_hcd->flags);
  392. if (hcd->driver->pci_resume && !HCD_DEAD(hcd)) {
  393. if (event != PM_EVENT_AUTO_RESUME)
  394. wait_for_companions(pci_dev, hcd);
  395. retval = hcd->driver->pci_resume(hcd,
  396. event == PM_EVENT_RESTORE);
  397. if (retval) {
  398. dev_err(dev, "PCI post-resume error %d!\n", retval);
  399. if (hcd->shared_hcd)
  400. usb_hc_died(hcd->shared_hcd);
  401. usb_hc_died(hcd);
  402. }
  403. }
  404. return retval;
  405. }
  406. #ifdef CONFIG_PM_SLEEP
  407. static int hcd_pci_suspend(struct device *dev)
  408. {
  409. return suspend_common(dev, device_may_wakeup(dev));
  410. }
  411. static int hcd_pci_suspend_noirq(struct device *dev)
  412. {
  413. struct pci_dev *pci_dev = to_pci_dev(dev);
  414. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  415. int retval;
  416. retval = check_root_hub_suspended(dev);
  417. if (retval)
  418. return retval;
  419. pci_save_state(pci_dev);
  420. /*
  421. * Some systems crash if an EHCI controller is in D3 during
  422. * a sleep transition. We have to leave such controllers in D0.
  423. */
  424. if (hcd->broken_pci_sleep) {
  425. dev_dbg(dev, "Staying in PCI D0\n");
  426. return retval;
  427. }
  428. /* If the root hub is dead rather than suspended, disallow remote
  429. * wakeup. usb_hc_died() should ensure that both hosts are marked as
  430. * dying, so we only need to check the primary roothub.
  431. */
  432. if (HCD_DEAD(hcd))
  433. device_set_wakeup_enable(dev, 0);
  434. dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
  435. /* Possibly enable remote wakeup,
  436. * choose the appropriate low-power state, and go to that state.
  437. */
  438. retval = pci_prepare_to_sleep(pci_dev);
  439. if (retval == -EIO) { /* Low-power not supported */
  440. dev_dbg(dev, "--> PCI D0 legacy\n");
  441. retval = 0;
  442. } else if (retval == 0) {
  443. dev_dbg(dev, "--> PCI %s\n",
  444. pci_power_name(pci_dev->current_state));
  445. } else {
  446. suspend_report_result(pci_prepare_to_sleep, retval);
  447. return retval;
  448. }
  449. powermac_set_asic(pci_dev, 0);
  450. return retval;
  451. }
  452. static int hcd_pci_resume_noirq(struct device *dev)
  453. {
  454. struct pci_dev *pci_dev = to_pci_dev(dev);
  455. powermac_set_asic(pci_dev, 1);
  456. /* Go back to D0 and disable remote wakeup */
  457. pci_back_from_sleep(pci_dev);
  458. return 0;
  459. }
  460. static int hcd_pci_resume(struct device *dev)
  461. {
  462. return resume_common(dev, PM_EVENT_RESUME);
  463. }
  464. static int hcd_pci_restore(struct device *dev)
  465. {
  466. return resume_common(dev, PM_EVENT_RESTORE);
  467. }
  468. #else
  469. #define hcd_pci_suspend NULL
  470. #define hcd_pci_suspend_noirq NULL
  471. #define hcd_pci_resume_noirq NULL
  472. #define hcd_pci_resume NULL
  473. #define hcd_pci_restore NULL
  474. #endif /* CONFIG_PM_SLEEP */
  475. #ifdef CONFIG_PM_RUNTIME
  476. static int hcd_pci_runtime_suspend(struct device *dev)
  477. {
  478. int retval;
  479. retval = suspend_common(dev, true);
  480. if (retval == 0)
  481. powermac_set_asic(to_pci_dev(dev), 0);
  482. dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
  483. return retval;
  484. }
  485. static int hcd_pci_runtime_resume(struct device *dev)
  486. {
  487. int retval;
  488. powermac_set_asic(to_pci_dev(dev), 1);
  489. retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
  490. dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
  491. return retval;
  492. }
  493. #else
  494. #define hcd_pci_runtime_suspend NULL
  495. #define hcd_pci_runtime_resume NULL
  496. #endif /* CONFIG_PM_RUNTIME */
  497. const struct dev_pm_ops usb_hcd_pci_pm_ops = {
  498. .suspend = hcd_pci_suspend,
  499. .suspend_noirq = hcd_pci_suspend_noirq,
  500. .resume_noirq = hcd_pci_resume_noirq,
  501. .resume = hcd_pci_resume,
  502. .freeze = check_root_hub_suspended,
  503. .freeze_noirq = check_root_hub_suspended,
  504. .thaw_noirq = NULL,
  505. .thaw = NULL,
  506. .poweroff = hcd_pci_suspend,
  507. .poweroff_noirq = hcd_pci_suspend_noirq,
  508. .restore_noirq = hcd_pci_resume_noirq,
  509. .restore = hcd_pci_restore,
  510. .runtime_suspend = hcd_pci_runtime_suspend,
  511. .runtime_resume = hcd_pci_runtime_resume,
  512. };
  513. EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
  514. #endif /* CONFIG_PM */