hcd-pci.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 || !companion_hcd->self.root_hub)
  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. int hcd_irq = 0;
  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. /*
  160. * The xHCI driver has its own irq management
  161. * make sure irq setup is not touched for xhci in generic hcd code
  162. */
  163. if ((driver->flags & HCD_MASK) != HCD_USB3) {
  164. if (!dev->irq) {
  165. dev_err(&dev->dev,
  166. "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
  167. pci_name(dev));
  168. retval = -ENODEV;
  169. goto disable_pci;
  170. }
  171. hcd_irq = dev->irq;
  172. }
  173. hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
  174. if (!hcd) {
  175. retval = -ENOMEM;
  176. goto disable_pci;
  177. }
  178. if (driver->flags & HCD_MEMORY) {
  179. /* EHCI, OHCI */
  180. hcd->rsrc_start = pci_resource_start(dev, 0);
  181. hcd->rsrc_len = pci_resource_len(dev, 0);
  182. if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
  183. driver->description)) {
  184. dev_dbg(&dev->dev, "controller already in use\n");
  185. retval = -EBUSY;
  186. goto clear_companion;
  187. }
  188. hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
  189. if (hcd->regs == NULL) {
  190. dev_dbg(&dev->dev, "error mapping memory\n");
  191. retval = -EFAULT;
  192. goto release_mem_region;
  193. }
  194. } else {
  195. /* UHCI */
  196. int region;
  197. for (region = 0; region < PCI_ROM_RESOURCE; region++) {
  198. if (!(pci_resource_flags(dev, region) &
  199. IORESOURCE_IO))
  200. continue;
  201. hcd->rsrc_start = pci_resource_start(dev, region);
  202. hcd->rsrc_len = pci_resource_len(dev, region);
  203. if (request_region(hcd->rsrc_start, hcd->rsrc_len,
  204. driver->description))
  205. break;
  206. }
  207. if (region == PCI_ROM_RESOURCE) {
  208. dev_dbg(&dev->dev, "no i/o regions available\n");
  209. retval = -EBUSY;
  210. goto clear_companion;
  211. }
  212. }
  213. pci_set_master(dev);
  214. retval = usb_add_hcd(hcd, hcd_irq, IRQF_SHARED);
  215. if (retval != 0)
  216. goto unmap_registers;
  217. set_hs_companion(dev, hcd);
  218. if (pci_dev_run_wake(dev))
  219. pm_runtime_put_noidle(&dev->dev);
  220. return retval;
  221. unmap_registers:
  222. if (driver->flags & HCD_MEMORY) {
  223. iounmap(hcd->regs);
  224. release_mem_region:
  225. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  226. } else
  227. release_region(hcd->rsrc_start, hcd->rsrc_len);
  228. clear_companion:
  229. clear_hs_companion(dev, hcd);
  230. usb_put_hcd(hcd);
  231. disable_pci:
  232. pci_disable_device(dev);
  233. dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
  234. return retval;
  235. }
  236. EXPORT_SYMBOL_GPL(usb_hcd_pci_probe);
  237. /* may be called without controller electrically present */
  238. /* may be called with controller, bus, and devices active */
  239. /**
  240. * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
  241. * @dev: USB Host Controller being removed
  242. * Context: !in_interrupt()
  243. *
  244. * Reverses the effect of usb_hcd_pci_probe(), first invoking
  245. * the HCD's stop() method. It is always called from a thread
  246. * context, normally "rmmod", "apmd", or something similar.
  247. *
  248. * Store this function in the HCD's struct pci_driver as remove().
  249. */
  250. void usb_hcd_pci_remove(struct pci_dev *dev)
  251. {
  252. struct usb_hcd *hcd;
  253. hcd = pci_get_drvdata(dev);
  254. if (!hcd)
  255. return;
  256. if (pci_dev_run_wake(dev))
  257. pm_runtime_get_noresume(&dev->dev);
  258. /* Fake an interrupt request in order to give the driver a chance
  259. * to test whether the controller hardware has been removed (e.g.,
  260. * cardbus physical eject).
  261. */
  262. local_irq_disable();
  263. usb_hcd_irq(0, hcd);
  264. local_irq_enable();
  265. usb_remove_hcd(hcd);
  266. if (hcd->driver->flags & HCD_MEMORY) {
  267. iounmap(hcd->regs);
  268. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  269. } else {
  270. release_region(hcd->rsrc_start, hcd->rsrc_len);
  271. }
  272. clear_hs_companion(dev, hcd);
  273. usb_put_hcd(hcd);
  274. pci_disable_device(dev);
  275. }
  276. EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
  277. /**
  278. * usb_hcd_pci_shutdown - shutdown host controller
  279. * @dev: USB Host Controller being shutdown
  280. */
  281. void usb_hcd_pci_shutdown(struct pci_dev *dev)
  282. {
  283. struct usb_hcd *hcd;
  284. hcd = pci_get_drvdata(dev);
  285. if (!hcd)
  286. return;
  287. if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) &&
  288. hcd->driver->shutdown) {
  289. hcd->driver->shutdown(hcd);
  290. pci_disable_device(dev);
  291. }
  292. }
  293. EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
  294. #ifdef CONFIG_PM
  295. #ifdef CONFIG_PPC_PMAC
  296. static void powermac_set_asic(struct pci_dev *pci_dev, int enable)
  297. {
  298. /* Enanble or disable ASIC clocks for USB */
  299. if (machine_is(powermac)) {
  300. struct device_node *of_node;
  301. of_node = pci_device_to_OF_node(pci_dev);
  302. if (of_node)
  303. pmac_call_feature(PMAC_FTR_USB_ENABLE,
  304. of_node, 0, enable);
  305. }
  306. }
  307. #else
  308. static inline void powermac_set_asic(struct pci_dev *pci_dev, int enable)
  309. {}
  310. #endif /* CONFIG_PPC_PMAC */
  311. static int check_root_hub_suspended(struct device *dev)
  312. {
  313. struct pci_dev *pci_dev = to_pci_dev(dev);
  314. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  315. if (HCD_RH_RUNNING(hcd)) {
  316. dev_warn(dev, "Root hub is not suspended\n");
  317. return -EBUSY;
  318. }
  319. if (hcd->shared_hcd) {
  320. hcd = hcd->shared_hcd;
  321. if (HCD_RH_RUNNING(hcd)) {
  322. dev_warn(dev, "Secondary root hub is not suspended\n");
  323. return -EBUSY;
  324. }
  325. }
  326. return 0;
  327. }
  328. #if defined(CONFIG_PM_SLEEP) || defined(CONFIG_PM_RUNTIME)
  329. static int suspend_common(struct device *dev, bool do_wakeup)
  330. {
  331. struct pci_dev *pci_dev = to_pci_dev(dev);
  332. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  333. int retval;
  334. /* Root hub suspend should have stopped all downstream traffic,
  335. * and all bus master traffic. And done so for both the interface
  336. * and the stub usb_device (which we check here). But maybe it
  337. * didn't; writing sysfs power/state files ignores such rules...
  338. */
  339. retval = check_root_hub_suspended(dev);
  340. if (retval)
  341. return retval;
  342. if (hcd->driver->pci_suspend && !HCD_DEAD(hcd)) {
  343. /* Optimization: Don't suspend if a root-hub wakeup is
  344. * pending and it would cause the HCD to wake up anyway.
  345. */
  346. if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
  347. return -EBUSY;
  348. if (do_wakeup && hcd->shared_hcd &&
  349. HCD_WAKEUP_PENDING(hcd->shared_hcd))
  350. return -EBUSY;
  351. retval = hcd->driver->pci_suspend(hcd, do_wakeup);
  352. suspend_report_result(hcd->driver->pci_suspend, retval);
  353. /* Check again in case wakeup raced with pci_suspend */
  354. if ((retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) ||
  355. (retval == 0 && do_wakeup && hcd->shared_hcd &&
  356. HCD_WAKEUP_PENDING(hcd->shared_hcd))) {
  357. if (hcd->driver->pci_resume)
  358. hcd->driver->pci_resume(hcd, false);
  359. retval = -EBUSY;
  360. }
  361. if (retval)
  362. return retval;
  363. }
  364. /* If MSI-X is enabled, the driver will have synchronized all vectors
  365. * in pci_suspend(). If MSI or legacy PCI is enabled, that will be
  366. * synchronized here.
  367. */
  368. if (!hcd->msix_enabled)
  369. synchronize_irq(pci_dev->irq);
  370. /* Downstream ports from this root hub should already be quiesced, so
  371. * there will be no DMA activity. Now we can shut down the upstream
  372. * link (except maybe for PME# resume signaling). We'll enter a
  373. * low power state during suspend_noirq, if the hardware allows.
  374. */
  375. pci_disable_device(pci_dev);
  376. return retval;
  377. }
  378. static int resume_common(struct device *dev, int event)
  379. {
  380. struct pci_dev *pci_dev = to_pci_dev(dev);
  381. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  382. int retval;
  383. if (HCD_RH_RUNNING(hcd) ||
  384. (hcd->shared_hcd &&
  385. HCD_RH_RUNNING(hcd->shared_hcd))) {
  386. dev_dbg(dev, "can't resume, not suspended!\n");
  387. return 0;
  388. }
  389. retval = pci_enable_device(pci_dev);
  390. if (retval < 0) {
  391. dev_err(dev, "can't re-enable after resume, %d!\n", retval);
  392. return retval;
  393. }
  394. pci_set_master(pci_dev);
  395. if (hcd->driver->pci_resume && !HCD_DEAD(hcd)) {
  396. if (event != PM_EVENT_AUTO_RESUME)
  397. wait_for_companions(pci_dev, hcd);
  398. retval = hcd->driver->pci_resume(hcd,
  399. event == PM_EVENT_RESTORE);
  400. if (retval) {
  401. dev_err(dev, "PCI post-resume error %d!\n", retval);
  402. if (hcd->shared_hcd)
  403. usb_hc_died(hcd->shared_hcd);
  404. usb_hc_died(hcd);
  405. }
  406. }
  407. return retval;
  408. }
  409. #endif /* SLEEP || RUNTIME */
  410. #ifdef CONFIG_PM_SLEEP
  411. static int hcd_pci_suspend(struct device *dev)
  412. {
  413. return suspend_common(dev, device_may_wakeup(dev));
  414. }
  415. static int hcd_pci_suspend_noirq(struct device *dev)
  416. {
  417. struct pci_dev *pci_dev = to_pci_dev(dev);
  418. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  419. int retval;
  420. retval = check_root_hub_suspended(dev);
  421. if (retval)
  422. return retval;
  423. pci_save_state(pci_dev);
  424. /* If the root hub is dead rather than suspended, disallow remote
  425. * wakeup. usb_hc_died() should ensure that both hosts are marked as
  426. * dying, so we only need to check the primary roothub.
  427. */
  428. if (HCD_DEAD(hcd))
  429. device_set_wakeup_enable(dev, 0);
  430. dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
  431. /* Possibly enable remote wakeup,
  432. * choose the appropriate low-power state, and go to that state.
  433. */
  434. retval = pci_prepare_to_sleep(pci_dev);
  435. if (retval == -EIO) { /* Low-power not supported */
  436. dev_dbg(dev, "--> PCI D0 legacy\n");
  437. retval = 0;
  438. } else if (retval == 0) {
  439. dev_dbg(dev, "--> PCI %s\n",
  440. pci_power_name(pci_dev->current_state));
  441. } else {
  442. suspend_report_result(pci_prepare_to_sleep, retval);
  443. return retval;
  444. }
  445. powermac_set_asic(pci_dev, 0);
  446. return retval;
  447. }
  448. static int hcd_pci_resume_noirq(struct device *dev)
  449. {
  450. struct pci_dev *pci_dev = to_pci_dev(dev);
  451. powermac_set_asic(pci_dev, 1);
  452. /* Go back to D0 and disable remote wakeup */
  453. pci_back_from_sleep(pci_dev);
  454. return 0;
  455. }
  456. static int hcd_pci_resume(struct device *dev)
  457. {
  458. return resume_common(dev, PM_EVENT_RESUME);
  459. }
  460. static int hcd_pci_restore(struct device *dev)
  461. {
  462. return resume_common(dev, PM_EVENT_RESTORE);
  463. }
  464. #else
  465. #define hcd_pci_suspend NULL
  466. #define hcd_pci_suspend_noirq NULL
  467. #define hcd_pci_resume_noirq NULL
  468. #define hcd_pci_resume NULL
  469. #define hcd_pci_restore NULL
  470. #endif /* CONFIG_PM_SLEEP */
  471. #ifdef CONFIG_PM_RUNTIME
  472. static int hcd_pci_runtime_suspend(struct device *dev)
  473. {
  474. int retval;
  475. retval = suspend_common(dev, true);
  476. if (retval == 0)
  477. powermac_set_asic(to_pci_dev(dev), 0);
  478. dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
  479. return retval;
  480. }
  481. static int hcd_pci_runtime_resume(struct device *dev)
  482. {
  483. int retval;
  484. powermac_set_asic(to_pci_dev(dev), 1);
  485. retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
  486. dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
  487. return retval;
  488. }
  489. #else
  490. #define hcd_pci_runtime_suspend NULL
  491. #define hcd_pci_runtime_resume NULL
  492. #endif /* CONFIG_PM_RUNTIME */
  493. const struct dev_pm_ops usb_hcd_pci_pm_ops = {
  494. .suspend = hcd_pci_suspend,
  495. .suspend_noirq = hcd_pci_suspend_noirq,
  496. .resume_noirq = hcd_pci_resume_noirq,
  497. .resume = hcd_pci_resume,
  498. .freeze = check_root_hub_suspended,
  499. .freeze_noirq = check_root_hub_suspended,
  500. .thaw_noirq = NULL,
  501. .thaw = NULL,
  502. .poweroff = hcd_pci_suspend,
  503. .poweroff_noirq = hcd_pci_suspend_noirq,
  504. .restore_noirq = hcd_pci_resume_noirq,
  505. .restore = hcd_pci_restore,
  506. .runtime_suspend = hcd_pci_runtime_suspend,
  507. .runtime_resume = hcd_pci_runtime_resume,
  508. };
  509. EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
  510. #endif /* CONFIG_PM */