2 * (C) Copyright David Brownell 2000-2002
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.
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
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.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/pci.h>
22 #include <linux/usb.h>
27 #ifdef CONFIG_PPC_PMAC
28 #include <asm/machdep.h>
29 #include <asm/pmac_feature.h>
30 #include <asm/pci-bridge.h>
38 /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
41 /*-------------------------------------------------------------------------*/
43 /* configure so an HC device and id are always provided */
44 /* always called with process context; sleeping is OK */
47 * usb_hcd_pci_probe - initialize PCI-based HCDs
48 * @dev: USB Host Controller being probed
49 * @id: pci hotplug id connecting controller to HCD framework
50 * Context: !in_interrupt()
52 * Allocates basic PCI resources for this USB host controller, and
53 * then invokes the start() method for the HCD associated with it
54 * through the hotplug entry's driver_data.
56 * Store this function in the HCD's struct pci_driver as probe().
58 int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
60 struct hc_driver *driver;
69 driver = (struct hc_driver *)id->driver_data;
73 if (pci_enable_device(dev) < 0)
75 dev->current_state = PCI_D0;
79 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
85 hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
91 if (driver->flags & HCD_MEMORY) {
93 hcd->rsrc_start = pci_resource_start(dev, 0);
94 hcd->rsrc_len = pci_resource_len(dev, 0);
95 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len,
96 driver->description)) {
97 dev_dbg(&dev->dev, "controller already in use\n");
101 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len);
102 if (hcd->regs == NULL) {
103 dev_dbg(&dev->dev, "error mapping memory\n");
112 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
113 if (!(pci_resource_flags(dev, region) &
117 hcd->rsrc_start = pci_resource_start(dev, region);
118 hcd->rsrc_len = pci_resource_len(dev, region);
119 if (request_region(hcd->rsrc_start, hcd->rsrc_len,
120 driver->description))
123 if (region == PCI_ROM_RESOURCE) {
124 dev_dbg(&dev->dev, "no i/o regions available\n");
132 retval = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED);
138 if (driver->flags & HCD_MEMORY) {
141 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
143 release_region(hcd->rsrc_start, hcd->rsrc_len);
147 pci_disable_device(dev);
148 dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
151 EXPORT_SYMBOL_GPL(usb_hcd_pci_probe);
154 /* may be called without controller electrically present */
155 /* may be called with controller, bus, and devices active */
158 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
159 * @dev: USB Host Controller being removed
160 * Context: !in_interrupt()
162 * Reverses the effect of usb_hcd_pci_probe(), first invoking
163 * the HCD's stop() method. It is always called from a thread
164 * context, normally "rmmod", "apmd", or something similar.
166 * Store this function in the HCD's struct pci_driver as remove().
168 void usb_hcd_pci_remove(struct pci_dev *dev)
172 hcd = pci_get_drvdata(dev);
177 if (hcd->driver->flags & HCD_MEMORY) {
179 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
181 release_region(hcd->rsrc_start, hcd->rsrc_len);
184 pci_disable_device(dev);
186 EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
192 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
193 * @dev: USB Host Controller being suspended
194 * @message: Power Management message describing this state transition
196 * Store this function in the HCD's struct pci_driver as .suspend.
198 int usb_hcd_pci_suspend(struct pci_dev *dev, pm_message_t message)
200 struct usb_hcd *hcd = pci_get_drvdata(dev);
205 /* Root hub suspend should have stopped all downstream traffic,
206 * and all bus master traffic. And done so for both the interface
207 * and the stub usb_device (which we check here). But maybe it
208 * didn't; writing sysfs power/state files ignores such rules...
210 * We must ignore the FREEZE vs SUSPEND distinction here, because
211 * otherwise the swsusp will save (and restore) garbage state.
213 if (!(hcd->state == HC_STATE_SUSPENDED ||
214 hcd->state == HC_STATE_HALT)) {
215 dev_warn(&dev->dev, "Root hub is not suspended\n");
220 /* We might already be suspended (runtime PM -- not yet written) */
221 if (dev->current_state != PCI_D0)
224 if (hcd->driver->pci_suspend) {
225 retval = hcd->driver->pci_suspend(hcd, message);
226 suspend_report_result(hcd->driver->pci_suspend, retval);
231 synchronize_irq(dev->irq);
233 /* Downstream ports from this root hub should already be quiesced, so
234 * there will be no DMA activity. Now we can shut down the upstream
235 * link (except maybe for PME# resume signaling) and enter some PCI
236 * low power state, if the hardware allows.
238 pci_disable_device(dev);
242 /* Don't fail on error to enable wakeup. We rely on pci code
243 * to reject requests the hardware can't implement, rather
244 * than coding the same thing.
246 wake = (hcd->state == HC_STATE_SUSPENDED &&
247 device_may_wakeup(&dev->dev));
248 w = pci_wake_from_d3(dev, wake);
251 dev_dbg(&dev->dev, "wakeup: %d\n", wake);
253 /* Don't change state if we don't need to */
254 if (message.event == PM_EVENT_FREEZE ||
255 message.event == PM_EVENT_PRETHAW) {
256 dev_dbg(&dev->dev, "--> no state change\n");
260 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
262 dev_dbg(&dev->dev, "--> PCI D0 legacy\n");
265 /* NOTE: dev->current_state becomes nonzero only here, and
266 * only for devices that support PCI PM. Also, exiting
267 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
268 * some device state (e.g. as part of clock reinit).
270 retval = pci_set_power_state(dev, PCI_D3hot);
271 suspend_report_result(pci_set_power_state, retval);
273 dev_dbg(&dev->dev, "--> PCI D3\n");
275 dev_dbg(&dev->dev, "PCI D3 suspend fail, %d\n",
277 pci_restore_state(dev);
281 #ifdef CONFIG_PPC_PMAC
283 /* Disable ASIC clocks for USB */
284 if (machine_is(powermac)) {
285 struct device_node *of_node;
287 of_node = pci_device_to_OF_node(dev);
289 pmac_call_feature(PMAC_FTR_USB_ENABLE,
298 EXPORT_SYMBOL_GPL(usb_hcd_pci_suspend);
301 * usb_hcd_pci_resume_early - resume a PCI-based HCD before IRQs are enabled
302 * @dev: USB Host Controller being resumed
304 * Store this function in the HCD's struct pci_driver as .resume_early.
306 int usb_hcd_pci_resume_early(struct pci_dev *dev)
308 pci_restore_state(dev);
311 EXPORT_SYMBOL_GPL(usb_hcd_pci_resume_early);
314 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
315 * @dev: USB Host Controller being resumed
317 * Store this function in the HCD's struct pci_driver as .resume.
319 int usb_hcd_pci_resume(struct pci_dev *dev)
324 #ifdef CONFIG_PPC_PMAC
325 /* Reenable ASIC clocks for USB */
326 if (machine_is(powermac)) {
327 struct device_node *of_node;
329 of_node = pci_device_to_OF_node(dev);
331 pmac_call_feature(PMAC_FTR_USB_ENABLE,
336 hcd = pci_get_drvdata(dev);
337 if (hcd->state != HC_STATE_SUSPENDED) {
338 dev_dbg(hcd->self.controller,
339 "can't resume, not suspended!\n");
343 pci_enable_wake(dev, PCI_D0, false);
345 retval = pci_enable_device(dev);
347 dev_err(&dev->dev, "can't re-enable after resume, %d!\n",
354 /* yes, ignore this result too... */
355 (void) pci_wake_from_d3(dev, 0);
357 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
359 if (hcd->driver->pci_resume) {
360 retval = hcd->driver->pci_resume(hcd);
362 dev_err(hcd->self.controller,
363 "PCI post-resume error %d!\n", retval);
369 EXPORT_SYMBOL_GPL(usb_hcd_pci_resume);
371 #endif /* CONFIG_PM */
374 * usb_hcd_pci_shutdown - shutdown host controller
375 * @dev: USB Host Controller being shutdown
377 void usb_hcd_pci_shutdown(struct pci_dev *dev)
381 hcd = pci_get_drvdata(dev);
385 if (hcd->driver->shutdown)
386 hcd->driver->shutdown(hcd);
388 EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);