RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / usb / core / hcd-pci.c
blobf66dd2789d5492d949358d23557d1b3ab26e0b6a
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 /* Fake an interrupt request in order to give the driver a chance
177 * to test whether the controller hardware has been removed (e.g.,
178 * cardbus physical eject).
180 local_irq_disable();
181 usb_hcd_irq(0, hcd);
182 local_irq_enable();
184 usb_remove_hcd(hcd);
185 if (hcd->driver->flags & HCD_MEMORY) {
186 iounmap(hcd->regs);
187 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
188 } else {
189 release_region(hcd->rsrc_start, hcd->rsrc_len);
191 usb_put_hcd(hcd);
192 pci_disable_device(dev);
194 EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
197 #ifdef CONFIG_PM
200 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
201 * @dev: USB Host Controller being suspended
202 * @message: semantics in flux
204 * Store this function in the HCD's struct pci_driver as suspend().
206 int usb_hcd_pci_suspend(struct pci_dev *dev, pm_message_t message)
208 struct usb_hcd *hcd;
209 int retval = 0;
210 int has_pci_pm;
212 hcd = pci_get_drvdata(dev);
214 /* Root hub suspend should have stopped all downstream traffic,
215 * and all bus master traffic. And done so for both the interface
216 * and the stub usb_device (which we check here). But maybe it
217 * didn't; writing sysfs power/state files ignores such rules...
219 * We must ignore the FREEZE vs SUSPEND distinction here, because
220 * otherwise the swsusp will save (and restore) garbage state.
222 if (!(hcd->state == HC_STATE_SUSPENDED ||
223 hcd->state == HC_STATE_HALT))
224 return -EBUSY;
226 if (hcd->driver->pci_suspend) {
227 retval = hcd->driver->pci_suspend(hcd, message);
228 suspend_report_result(hcd->driver->pci_suspend, retval);
229 if (retval)
230 goto done;
232 synchronize_irq(dev->irq);
234 /* FIXME until the generic PM interfaces change a lot more, this
235 * can't use PCI D1 and D2 states. For example, the confusion
236 * between messages and states will need to vanish, and messages
237 * will need to provide a target system state again.
239 * It'll be important to learn characteristics of the target state,
240 * especially on embedded hardware where the HCD will often be in
241 * charge of an external VBUS power supply and one or more clocks.
242 * Some target system states will leave them active; others won't.
243 * (With PCI, that's often handled by platform BIOS code.)
246 /* even when the PCI layer rejects some of the PCI calls
247 * below, HCs can try global suspend and reduce DMA traffic.
248 * PM-sensitive HCDs may already have done this.
250 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
252 /* Downstream ports from this root hub should already be quiesced, so
253 * there will be no DMA activity. Now we can shut down the upstream
254 * link (except maybe for PME# resume signaling) and enter some PCI
255 * low power state, if the hardware allows.
257 if (hcd->state == HC_STATE_SUSPENDED) {
259 /* no DMA or IRQs except when HC is active */
260 if (dev->current_state == PCI_D0) {
261 pci_save_state(dev);
262 pci_disable_device(dev);
265 if (message.event == PM_EVENT_FREEZE ||
266 message.event == PM_EVENT_PRETHAW) {
267 dev_dbg(hcd->self.controller, "--> no state change\n");
268 goto done;
271 if (!has_pci_pm) {
272 dev_dbg(hcd->self.controller, "--> PCI D0/legacy\n");
273 goto done;
276 /* NOTE: dev->current_state becomes nonzero only here, and
277 * only for devices that support PCI PM. Also, exiting
278 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
279 * some device state (e.g. as part of clock reinit).
281 retval = pci_set_power_state(dev, PCI_D3hot);
282 suspend_report_result(pci_set_power_state, retval);
283 if (retval == 0) {
284 int wake = device_can_wakeup(&hcd->self.root_hub->dev);
286 wake = wake && device_may_wakeup(hcd->self.controller);
288 dev_dbg(hcd->self.controller, "--> PCI D3%s\n",
289 wake ? "/wakeup" : "");
291 /* Ignore these return values. We rely on pci code to
292 * reject requests the hardware can't implement, rather
293 * than coding the same thing.
295 (void) pci_enable_wake(dev, PCI_D3hot, wake);
296 (void) pci_enable_wake(dev, PCI_D3cold, wake);
297 } else {
298 dev_dbg(&dev->dev, "PCI D3 suspend fail, %d\n",
299 retval);
300 (void) usb_hcd_pci_resume(dev);
303 } else if (hcd->state != HC_STATE_HALT) {
304 dev_dbg(hcd->self.controller, "hcd state %d; not suspended\n",
305 hcd->state);
306 WARN_ON(1);
307 retval = -EINVAL;
310 done:
311 if (retval == 0) {
312 #ifdef CONFIG_PPC_PMAC
313 /* Disable ASIC clocks for USB */
314 if (machine_is(powermac)) {
315 struct device_node *of_node;
317 of_node = pci_device_to_OF_node(dev);
318 if (of_node)
319 pmac_call_feature(PMAC_FTR_USB_ENABLE,
320 of_node, 0, 0);
322 #endif
325 return retval;
327 EXPORT_SYMBOL_GPL(usb_hcd_pci_suspend);
330 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
331 * @dev: USB Host Controller being resumed
333 * Store this function in the HCD's struct pci_driver as .resume.
335 int usb_hcd_pci_resume(struct pci_dev *dev)
337 struct usb_hcd *hcd;
338 int retval;
340 hcd = pci_get_drvdata(dev);
341 if (hcd->state != HC_STATE_SUSPENDED) {
342 dev_dbg(hcd->self.controller,
343 "can't resume, not suspended!\n");
344 return 0;
347 #ifdef CONFIG_PPC_PMAC
348 /* Reenable ASIC clocks for USB */
349 if (machine_is(powermac)) {
350 struct device_node *of_node;
352 of_node = pci_device_to_OF_node(dev);
353 if (of_node)
354 pmac_call_feature(PMAC_FTR_USB_ENABLE,
355 of_node, 0, 1);
357 #endif
359 /* NOTE: chip docs cover clean "real suspend" cases (what Linux
360 * calls "standby", "suspend to RAM", and so on). There are also
361 * dirty cases when swsusp fakes a suspend in "shutdown" mode.
363 if (dev->current_state != PCI_D0) {
364 #ifdef DEBUG
365 int pci_pm;
366 u16 pmcr;
368 pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
369 pci_read_config_word(dev, pci_pm + PCI_PM_CTRL, &pmcr);
370 pmcr &= PCI_PM_CTRL_STATE_MASK;
371 if (pmcr) {
372 /* Clean case: power to USB and to HC registers was
373 * maintained; remote wakeup is easy.
375 dev_dbg(hcd->self.controller, "resume from PCI D%d\n",
376 pmcr);
377 } else {
378 /* Clean: HC lost Vcc power, D0 uninitialized
379 * + Vaux may have preserved port and transceiver
380 * state ... for remote wakeup from D3cold
381 * + or not; HCD must reinit + re-enumerate
383 * Dirty: D0 semi-initialized cases with swsusp
384 * + after BIOS init
385 * + after Linux init (HCD statically linked)
387 dev_dbg(hcd->self.controller,
388 "PCI D0, from previous PCI D%d\n",
389 dev->current_state);
391 #endif
392 /* yes, ignore these results too... */
393 (void) pci_enable_wake(dev, dev->current_state, 0);
394 (void) pci_enable_wake(dev, PCI_D3cold, 0);
395 } else {
396 /* Same basic cases: clean (powered/not), dirty */
397 dev_dbg(hcd->self.controller, "PCI legacy resume\n");
400 /* NOTE: the PCI API itself is asymmetric here. We don't need to
401 * pci_set_power_state(PCI_D0) since that's part of re-enabling;
402 * but that won't re-enable bus mastering. Yet pci_disable_device()
403 * explicitly disables bus mastering...
405 retval = pci_enable_device(dev);
406 if (retval < 0) {
407 dev_err(hcd->self.controller,
408 "can't re-enable after resume, %d!\n", retval);
409 return retval;
411 pci_set_master(dev);
412 pci_restore_state(dev);
414 clear_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
416 if (hcd->driver->pci_resume) {
417 retval = hcd->driver->pci_resume(hcd);
418 if (retval) {
419 dev_err(hcd->self.controller,
420 "PCI post-resume error %d!\n", retval);
421 usb_hc_died(hcd);
425 return retval;
427 EXPORT_SYMBOL_GPL(usb_hcd_pci_resume);
429 #endif /* CONFIG_PM */
432 * usb_hcd_pci_shutdown - shutdown host controller
433 * @dev: USB Host Controller being shutdown
435 void usb_hcd_pci_shutdown(struct pci_dev *dev)
437 struct usb_hcd *hcd;
439 hcd = pci_get_drvdata(dev);
440 if (!hcd)
441 return;
443 if (hcd->driver->shutdown)
444 hcd->driver->shutdown(hcd);
446 EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);