hcd-pci.c 16 KB

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