init from v2.6.32.60
[mach-moxart.git] / drivers / usb / core / hcd-pci.c
blob5915651e66ddccde9fe171b1c5ad6deddc3237c5
1 /*
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
12 * for more details.
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>
24 #include <asm/io.h>
25 #include <asm/irq.h>
27 #ifdef CONFIG_PPC_PMAC
28 #include <asm/machdep.h>
29 #include <asm/pmac_feature.h>
30 #include <asm/pci-bridge.h>
31 #include <asm/prom.h>
32 #endif
34 #include "usb.h"
35 #include "hcd.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 */
46 /**
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;
61 struct usb_hcd *hcd;
62 int retval;
64 if (usb_disabled())
65 return -ENODEV;
67 if (!id)
68 return -EINVAL;
69 driver = (struct hc_driver *)id->driver_data;
70 if (!driver)
71 return -EINVAL;
73 if (pci_enable_device(dev) < 0)
74 return -ENODEV;
75 dev->current_state = PCI_D0;
77 if (!dev->irq) {
78 dev_err(&dev->dev,
79 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
80 pci_name(dev));
81 retval = -ENODEV;
82 goto err1;
85 hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
86 if (!hcd) {
87 retval = -ENOMEM;
88 goto err1;
91 if (driver->flags & HCD_MEMORY) {
92 /* EHCI, OHCI */
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");
98 retval = -EBUSY;
99 goto err2;
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");
104 retval = -EFAULT;
105 goto err3;
108 } else {
109 /* UHCI */
110 int region;
112 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
113 if (!(pci_resource_flags(dev, region) &
114 IORESOURCE_IO))
115 continue;
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))
121 break;
123 if (region == PCI_ROM_RESOURCE) {
124 dev_dbg(&dev->dev, "no i/o regions available\n");
125 retval = -EBUSY;
126 goto err1;
130 pci_set_master(dev);
132 retval = usb_add_hcd(hcd, dev->irq, IRQF_DISABLED | IRQF_SHARED);
133 if (retval != 0)
134 goto err4;
135 return retval;
137 err4:
138 if (driver->flags & HCD_MEMORY) {
139 iounmap(hcd->regs);
140 err3:
141 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
142 } else
143 release_region(hcd->rsrc_start, hcd->rsrc_len);
144 err2:
145 usb_put_hcd(hcd);
146 err1:
147 pci_disable_device(dev);
148 dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
149 return 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)
170 struct usb_hcd *hcd;
172 hcd = pci_get_drvdata(dev);
173 if (!hcd)
174 return;
176 usb_remove_hcd(hcd);
177 if (hcd->driver->flags & HCD_MEMORY) {
178 iounmap(hcd->regs);
179 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
180 } else {
181 release_region(hcd->rsrc_start, hcd->rsrc_len);
183 usb_put_hcd(hcd);
184 pci_disable_device(dev);
186 EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
189 * usb_hcd_pci_shutdown - shutdown host controller
190 * @dev: USB Host Controller being shutdown
192 void usb_hcd_pci_shutdown(struct pci_dev *dev)
194 struct usb_hcd *hcd;
196 hcd = pci_get_drvdata(dev);
197 if (!hcd)
198 return;
200 if (hcd->driver->shutdown) {
201 hcd->driver->shutdown(hcd);
202 pci_disable_device(dev);
205 EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
207 #ifdef CONFIG_PM_SLEEP
209 static int check_root_hub_suspended(struct device *dev)
211 struct pci_dev *pci_dev = to_pci_dev(dev);
212 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
214 if (!(hcd->state == HC_STATE_SUSPENDED ||
215 hcd->state == HC_STATE_HALT)) {
216 dev_warn(dev, "Root hub is not suspended\n");
217 return -EBUSY;
219 return 0;
222 static int hcd_pci_suspend(struct device *dev)
224 struct pci_dev *pci_dev = to_pci_dev(dev);
225 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
226 int retval;
228 /* Root hub suspend should have stopped all downstream traffic,
229 * and all bus master traffic. And done so for both the interface
230 * and the stub usb_device (which we check here). But maybe it
231 * didn't; writing sysfs power/state files ignores such rules...
233 retval = check_root_hub_suspended(dev);
234 if (retval)
235 return retval;
237 /* We might already be suspended (runtime PM -- not yet written) */
238 if (pci_dev->current_state != PCI_D0)
239 return retval;
241 if (hcd->driver->pci_suspend) {
242 retval = hcd->driver->pci_suspend(hcd);
243 suspend_report_result(hcd->driver->pci_suspend, retval);
244 if (retval)
245 return retval;
248 synchronize_irq(pci_dev->irq);
250 /* Downstream ports from this root hub should already be quiesced, so
251 * there will be no DMA activity. Now we can shut down the upstream
252 * link (except maybe for PME# resume signaling). We'll enter a
253 * low power state during suspend_noirq, if the hardware allows.
255 pci_disable_device(pci_dev);
256 return retval;
259 static int hcd_pci_suspend_noirq(struct device *dev)
261 struct pci_dev *pci_dev = to_pci_dev(dev);
262 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
263 int retval;
265 retval = check_root_hub_suspended(dev);
266 if (retval)
267 return retval;
269 pci_save_state(pci_dev);
271 /* If the root hub is HALTed rather than SUSPENDed,
272 * disallow remote wakeup.
274 if (hcd->state == HC_STATE_HALT)
275 device_set_wakeup_enable(dev, 0);
276 dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
278 /* Possibly enable remote wakeup,
279 * choose the appropriate low-power state, and go to that state.
281 retval = pci_prepare_to_sleep(pci_dev);
282 if (retval == -EIO) { /* Low-power not supported */
283 dev_dbg(dev, "--> PCI D0 legacy\n");
284 retval = 0;
285 } else if (retval == 0) {
286 dev_dbg(dev, "--> PCI %s\n",
287 pci_power_name(pci_dev->current_state));
288 } else {
289 suspend_report_result(pci_prepare_to_sleep, retval);
290 return retval;
293 #ifdef CONFIG_PPC_PMAC
294 /* Disable ASIC clocks for USB */
295 if (machine_is(powermac)) {
296 struct device_node *of_node;
298 of_node = pci_device_to_OF_node(pci_dev);
299 if (of_node)
300 pmac_call_feature(PMAC_FTR_USB_ENABLE, of_node, 0, 0);
302 #endif
303 return retval;
306 static int hcd_pci_resume_noirq(struct device *dev)
308 struct pci_dev *pci_dev = to_pci_dev(dev);
310 #ifdef CONFIG_PPC_PMAC
311 /* Reenable ASIC clocks for USB */
312 if (machine_is(powermac)) {
313 struct device_node *of_node;
315 of_node = pci_device_to_OF_node(pci_dev);
316 if (of_node)
317 pmac_call_feature(PMAC_FTR_USB_ENABLE,
318 of_node, 0, 1);
320 #endif
322 /* Go back to D0 and disable remote wakeup */
323 pci_back_from_sleep(pci_dev);
324 return 0;
327 static int resume_common(struct device *dev, bool hibernated)
329 struct pci_dev *pci_dev = to_pci_dev(dev);
330 struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
331 int retval;
333 if (hcd->state != HC_STATE_SUSPENDED) {
334 dev_dbg(dev, "can't resume, not suspended!\n");
335 return 0;
338 retval = pci_enable_device(pci_dev);
339 if (retval < 0) {
340 dev_err(dev, "can't re-enable after resume, %d!\n", retval);
341 return retval;
344 pci_set_master(pci_dev);
346 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
348 if (hcd->driver->pci_resume) {
349 retval = hcd->driver->pci_resume(hcd, hibernated);
350 if (retval) {
351 dev_err(dev, "PCI post-resume error %d!\n", retval);
352 usb_hc_died(hcd);
355 return retval;
358 static int hcd_pci_resume(struct device *dev)
360 return resume_common(dev, false);
363 static int hcd_pci_restore(struct device *dev)
365 return resume_common(dev, true);
368 struct dev_pm_ops usb_hcd_pci_pm_ops = {
369 .suspend = hcd_pci_suspend,
370 .suspend_noirq = hcd_pci_suspend_noirq,
371 .resume_noirq = hcd_pci_resume_noirq,
372 .resume = hcd_pci_resume,
373 .freeze = check_root_hub_suspended,
374 .freeze_noirq = check_root_hub_suspended,
375 .thaw_noirq = NULL,
376 .thaw = NULL,
377 .poweroff = hcd_pci_suspend,
378 .poweroff_noirq = hcd_pci_suspend_noirq,
379 .restore_noirq = hcd_pci_resume_noirq,
380 .restore = hcd_pci_restore,
382 EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
384 #endif /* CONFIG_PM_SLEEP */