hcd-pci.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 err1;
  164. }
  165. hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
  166. if (!hcd) {
  167. retval = -ENOMEM;
  168. goto err1;
  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 err2;
  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 err3;
  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 err2;
  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 err4;
  209. set_hs_companion(dev, hcd);
  210. if (pci_dev_run_wake(dev))
  211. pm_runtime_put_noidle(&dev->dev);
  212. return retval;
  213. err4:
  214. if (driver->flags & HCD_MEMORY) {
  215. iounmap(hcd->regs);
  216. err3:
  217. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  218. } else
  219. release_region(hcd->rsrc_start, hcd->rsrc_len);
  220. err2:
  221. clear_hs_companion(dev, hcd);
  222. usb_put_hcd(hcd);
  223. err1:
  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_OPS
  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->state == HC_STATE_SUSPENDED ||
  308. hcd->state == HC_STATE_HALT)) {
  309. dev_warn(dev, "Root hub is not suspended\n");
  310. return -EBUSY;
  311. }
  312. return 0;
  313. }
  314. static int suspend_common(struct device *dev, bool do_wakeup)
  315. {
  316. struct pci_dev *pci_dev = to_pci_dev(dev);
  317. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  318. int retval;
  319. /* Root hub suspend should have stopped all downstream traffic,
  320. * and all bus master traffic. And done so for both the interface
  321. * and the stub usb_device (which we check here). But maybe it
  322. * didn't; writing sysfs power/state files ignores such rules...
  323. */
  324. retval = check_root_hub_suspended(dev);
  325. if (retval)
  326. return retval;
  327. if (hcd->driver->pci_suspend) {
  328. /* Optimization: Don't suspend if a root-hub wakeup is
  329. * pending and it would cause the HCD to wake up anyway.
  330. */
  331. if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
  332. return -EBUSY;
  333. retval = hcd->driver->pci_suspend(hcd, do_wakeup);
  334. suspend_report_result(hcd->driver->pci_suspend, retval);
  335. /* Check again in case wakeup raced with pci_suspend */
  336. if (retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) {
  337. if (hcd->driver->pci_resume)
  338. hcd->driver->pci_resume(hcd, false);
  339. retval = -EBUSY;
  340. }
  341. if (retval)
  342. return retval;
  343. }
  344. synchronize_irq(pci_dev->irq);
  345. /* Downstream ports from this root hub should already be quiesced, so
  346. * there will be no DMA activity. Now we can shut down the upstream
  347. * link (except maybe for PME# resume signaling). We'll enter a
  348. * low power state during suspend_noirq, if the hardware allows.
  349. */
  350. pci_disable_device(pci_dev);
  351. return retval;
  352. }
  353. static int resume_common(struct device *dev, int event)
  354. {
  355. struct pci_dev *pci_dev = to_pci_dev(dev);
  356. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  357. int retval;
  358. if (hcd->state != HC_STATE_SUSPENDED) {
  359. dev_dbg(dev, "can't resume, not suspended!\n");
  360. return 0;
  361. }
  362. retval = pci_enable_device(pci_dev);
  363. if (retval < 0) {
  364. dev_err(dev, "can't re-enable after resume, %d!\n", retval);
  365. return retval;
  366. }
  367. pci_set_master(pci_dev);
  368. clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
  369. if (hcd->driver->pci_resume) {
  370. if (event != PM_EVENT_AUTO_RESUME)
  371. wait_for_companions(pci_dev, hcd);
  372. retval = hcd->driver->pci_resume(hcd,
  373. event == PM_EVENT_RESTORE);
  374. if (retval) {
  375. dev_err(dev, "PCI post-resume error %d!\n", retval);
  376. usb_hc_died(hcd);
  377. }
  378. }
  379. return retval;
  380. }
  381. #ifdef CONFIG_PM_SLEEP
  382. static int hcd_pci_suspend(struct device *dev)
  383. {
  384. return suspend_common(dev, device_may_wakeup(dev));
  385. }
  386. static int hcd_pci_suspend_noirq(struct device *dev)
  387. {
  388. struct pci_dev *pci_dev = to_pci_dev(dev);
  389. struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
  390. int retval;
  391. retval = check_root_hub_suspended(dev);
  392. if (retval)
  393. return retval;
  394. pci_save_state(pci_dev);
  395. /* If the root hub is HALTed rather than SUSPENDed,
  396. * disallow remote wakeup.
  397. */
  398. if (hcd->state == HC_STATE_HALT)
  399. device_set_wakeup_enable(dev, 0);
  400. dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
  401. /* Possibly enable remote wakeup,
  402. * choose the appropriate low-power state, and go to that state.
  403. */
  404. retval = pci_prepare_to_sleep(pci_dev);
  405. if (retval == -EIO) { /* Low-power not supported */
  406. dev_dbg(dev, "--> PCI D0 legacy\n");
  407. retval = 0;
  408. } else if (retval == 0) {
  409. dev_dbg(dev, "--> PCI %s\n",
  410. pci_power_name(pci_dev->current_state));
  411. } else {
  412. suspend_report_result(pci_prepare_to_sleep, retval);
  413. return retval;
  414. }
  415. powermac_set_asic(pci_dev, 0);
  416. return retval;
  417. }
  418. static int hcd_pci_resume_noirq(struct device *dev)
  419. {
  420. struct pci_dev *pci_dev = to_pci_dev(dev);
  421. powermac_set_asic(pci_dev, 1);
  422. /* Go back to D0 and disable remote wakeup */
  423. pci_back_from_sleep(pci_dev);
  424. return 0;
  425. }
  426. static int hcd_pci_resume(struct device *dev)
  427. {
  428. return resume_common(dev, PM_EVENT_RESUME);
  429. }
  430. static int hcd_pci_restore(struct device *dev)
  431. {
  432. return resume_common(dev, PM_EVENT_RESTORE);
  433. }
  434. #else
  435. #define hcd_pci_suspend NULL
  436. #define hcd_pci_suspend_noirq NULL
  437. #define hcd_pci_resume_noirq NULL
  438. #define hcd_pci_resume NULL
  439. #define hcd_pci_restore NULL
  440. #endif /* CONFIG_PM_SLEEP */
  441. #ifdef CONFIG_PM_RUNTIME
  442. static int hcd_pci_runtime_suspend(struct device *dev)
  443. {
  444. int retval;
  445. retval = suspend_common(dev, true);
  446. if (retval == 0)
  447. powermac_set_asic(to_pci_dev(dev), 0);
  448. dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
  449. return retval;
  450. }
  451. static int hcd_pci_runtime_resume(struct device *dev)
  452. {
  453. int retval;
  454. powermac_set_asic(to_pci_dev(dev), 1);
  455. retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
  456. dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
  457. return retval;
  458. }
  459. #else
  460. #define hcd_pci_runtime_suspend NULL
  461. #define hcd_pci_runtime_resume NULL
  462. #endif /* CONFIG_PM_RUNTIME */
  463. const struct dev_pm_ops usb_hcd_pci_pm_ops = {
  464. .suspend = hcd_pci_suspend,
  465. .suspend_noirq = hcd_pci_suspend_noirq,
  466. .resume_noirq = hcd_pci_resume_noirq,
  467. .resume = hcd_pci_resume,
  468. .freeze = check_root_hub_suspended,
  469. .freeze_noirq = check_root_hub_suspended,
  470. .thaw_noirq = NULL,
  471. .thaw = NULL,
  472. .poweroff = hcd_pci_suspend,
  473. .poweroff_noirq = hcd_pci_suspend_noirq,
  474. .restore_noirq = hcd_pci_resume_noirq,
  475. .restore = hcd_pci_restore,
  476. .runtime_suspend = hcd_pci_runtime_suspend,
  477. .runtime_resume = hcd_pci_runtime_resume,
  478. };
  479. EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
  480. #endif /* CONFIG_PM_OPS */