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/config.h>
21 #ifdef CONFIG_USB_DEBUG
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
32 #include <linux/usb.h>
36 /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
39 /*-------------------------------------------------------------------------*/
41 /* configure so an HC device and id are always provided */
42 /* always called with process context; sleeping is OK */
45 * usb_hcd_pci_probe - initialize PCI-based HCDs
46 * @dev: USB Host Controller being probed
47 * @id: pci hotplug id connecting controller to HCD framework
48 * Context: !in_interrupt()
50 * Allocates basic PCI resources for this USB host controller, and
51 * then invokes the start() method for the HCD associated with it
52 * through the hotplug entry's driver_data.
54 * Store this function in the HCD's struct pci_driver as probe().
56 int usb_hcd_pci_probe (struct pci_dev
*dev
, const struct pci_device_id
*id
)
58 struct hc_driver
*driver
;
65 if (!id
|| !(driver
= (struct hc_driver
*) id
->driver_data
))
68 if (pci_enable_device (dev
) < 0)
70 dev
->current_state
= PCI_D0
;
71 dev
->dev
.power
.power_state
= PMSG_ON
;
75 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
81 hcd
= usb_create_hcd (driver
, &dev
->dev
, pci_name(dev
));
87 if (driver
->flags
& HCD_MEMORY
) { // EHCI, OHCI
88 hcd
->rsrc_start
= pci_resource_start (dev
, 0);
89 hcd
->rsrc_len
= pci_resource_len (dev
, 0);
90 if (!request_mem_region (hcd
->rsrc_start
, hcd
->rsrc_len
,
91 driver
->description
)) {
92 dev_dbg (&dev
->dev
, "controller already in use\n");
96 hcd
->regs
= ioremap_nocache (hcd
->rsrc_start
, hcd
->rsrc_len
);
97 if (hcd
->regs
== NULL
) {
98 dev_dbg (&dev
->dev
, "error mapping memory\n");
106 for (region
= 0; region
< PCI_ROM_RESOURCE
; region
++) {
107 if (!(pci_resource_flags (dev
, region
) &
111 hcd
->rsrc_start
= pci_resource_start (dev
, region
);
112 hcd
->rsrc_len
= pci_resource_len (dev
, region
);
113 if (request_region (hcd
->rsrc_start
, hcd
->rsrc_len
,
114 driver
->description
))
117 if (region
== PCI_ROM_RESOURCE
) {
118 dev_dbg (&dev
->dev
, "no i/o regions available\n");
124 #ifdef CONFIG_PCI_NAMES
125 hcd
->product_desc
= dev
->pretty_name
;
128 pci_set_master (dev
);
130 retval
= usb_add_hcd (hcd
, dev
->irq
, SA_SHIRQ
);
136 if (driver
->flags
& HCD_MEMORY
) {
139 release_mem_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
141 release_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
145 pci_disable_device (dev
);
146 dev_err (&dev
->dev
, "init %s fail, %d\n", pci_name(dev
), retval
);
149 EXPORT_SYMBOL (usb_hcd_pci_probe
);
152 /* may be called without controller electrically present */
153 /* may be called with controller, bus, and devices active */
156 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
157 * @dev: USB Host Controller being removed
158 * Context: !in_interrupt()
160 * Reverses the effect of usb_hcd_pci_probe(), first invoking
161 * the HCD's stop() method. It is always called from a thread
162 * context, normally "rmmod", "apmd", or something similar.
164 * Store this function in the HCD's struct pci_driver as remove().
166 void usb_hcd_pci_remove (struct pci_dev
*dev
)
170 hcd
= pci_get_drvdata(dev
);
174 usb_remove_hcd (hcd
);
175 if (hcd
->driver
->flags
& HCD_MEMORY
) {
177 release_mem_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
179 release_region (hcd
->rsrc_start
, hcd
->rsrc_len
);
182 pci_disable_device(dev
);
184 EXPORT_SYMBOL (usb_hcd_pci_remove
);
190 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
191 * @dev: USB Host Controller being suspended
192 * @message: semantics in flux
194 * Store this function in the HCD's struct pci_driver as suspend().
196 int usb_hcd_pci_suspend (struct pci_dev
*dev
, pm_message_t message
)
202 hcd
= pci_get_drvdata(dev
);
204 /* FIXME until the generic PM interfaces change a lot more, this
205 * can't use PCI D1 and D2 states. For example, the confusion
206 * between messages and states will need to vanish, and messages
207 * will need to provide a target system state again.
209 * It'll be important to learn characteristics of the target state,
210 * especially on embedded hardware where the HCD will often be in
211 * charge of an external VBUS power supply and one or more clocks.
212 * Some target system states will leave them active; others won't.
213 * (With PCI, that's often handled by platform BIOS code.)
216 /* even when the PCI layer rejects some of the PCI calls
217 * below, HCs can try global suspend and reduce DMA traffic.
218 * PM-sensitive HCDs may already have done this.
220 has_pci_pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
222 switch (hcd
->state
) {
224 /* entry if root hub wasn't yet suspended ... from sysfs,
225 * without autosuspend, or if USB_SUSPEND isn't configured.
227 case HC_STATE_RUNNING
:
228 hcd
->state
= HC_STATE_QUIESCING
;
229 retval
= hcd
->driver
->suspend (hcd
, message
);
231 dev_dbg (hcd
->self
.controller
,
232 "suspend fail, retval %d\n",
236 hcd
->state
= HC_STATE_SUSPENDED
;
239 /* entry with CONFIG_USB_SUSPEND, or hcds that autosuspend: the
240 * controller and/or root hub will already have been suspended,
241 * but it won't be ready for a PCI resume call.
243 * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will
244 * have been called, otherwise root hub timers still run ...
246 case HC_STATE_SUSPENDED
:
247 /* no DMA or IRQs except when HC is active */
248 if (dev
->current_state
== PCI_D0
) {
249 free_irq (hcd
->irq
, hcd
);
250 pci_save_state (dev
);
251 pci_disable_device (dev
);
255 dev_dbg (hcd
->self
.controller
, "--> PCI D0/legacy\n");
259 /* NOTE: dev->current_state becomes nonzero only here, and
260 * only for devices that support PCI PM. Also, exiting
261 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset
262 * some device state (e.g. as part of clock reinit).
264 retval
= pci_set_power_state (dev
, PCI_D3hot
);
266 dev_dbg (hcd
->self
.controller
, "--> PCI D3\n");
267 pci_enable_wake (dev
, PCI_D3hot
, hcd
->remote_wakeup
);
268 pci_enable_wake (dev
, PCI_D3cold
, hcd
->remote_wakeup
);
269 } else if (retval
< 0) {
270 dev_dbg (&dev
->dev
, "PCI D3 suspend fail, %d\n",
272 (void) usb_hcd_pci_resume (dev
);
277 dev_dbg (hcd
->self
.controller
, "hcd state %d; not suspended\n",
284 /* update power_state **ONLY** to make sysfs happier */
286 dev
->dev
.power
.power_state
= message
;
289 EXPORT_SYMBOL (usb_hcd_pci_suspend
);
292 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
293 * @dev: USB Host Controller being resumed
295 * Store this function in the HCD's struct pci_driver as resume().
297 int usb_hcd_pci_resume (struct pci_dev
*dev
)
302 hcd
= pci_get_drvdata(dev
);
303 if (hcd
->state
!= HC_STATE_SUSPENDED
) {
304 dev_dbg (hcd
->self
.controller
,
305 "can't resume, not suspended!\n");
309 /* NOTE: chip docs cover clean "real suspend" cases (what Linux
310 * calls "standby", "suspend to RAM", and so on). There are also
311 * dirty cases when swsusp fakes a suspend in "shutdown" mode.
313 if (dev
->current_state
!= PCI_D0
) {
318 pci_pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
319 pci_read_config_word(dev
, pci_pm
+ PCI_PM_CTRL
, &pmcr
);
320 pmcr
&= PCI_PM_CTRL_STATE_MASK
;
322 /* Clean case: power to USB and to HC registers was
323 * maintained; remote wakeup is easy.
325 dev_dbg(hcd
->self
.controller
, "resume from PCI D%d\n",
328 /* Clean: HC lost Vcc power, D0 uninitialized
329 * + Vaux may have preserved port and transceiver
330 * state ... for remote wakeup from D3cold
331 * + or not; HCD must reinit + re-enumerate
333 * Dirty: D0 semi-initialized cases with swsusp
335 * + after Linux init (HCD statically linked)
337 dev_dbg(hcd
->self
.controller
,
338 "PCI D0, from previous PCI D%d\n",
342 pci_enable_wake (dev
, dev
->current_state
, 0);
343 pci_enable_wake (dev
, PCI_D3cold
, 0);
345 /* Same basic cases: clean (powered/not), dirty */
346 dev_dbg(hcd
->self
.controller
, "PCI legacy resume\n");
349 /* NOTE: the PCI API itself is asymmetric here. We don't need to
350 * pci_set_power_state(PCI_D0) since that's part of re-enabling;
351 * but that won't re-enable bus mastering. Yet pci_disable_device()
352 * explicitly disables bus mastering...
354 retval
= pci_enable_device (dev
);
356 dev_err (hcd
->self
.controller
,
357 "can't re-enable after resume, %d!\n", retval
);
360 pci_set_master (dev
);
361 pci_restore_state (dev
);
363 dev
->dev
.power
.power_state
= PMSG_ON
;
365 hcd
->state
= HC_STATE_RESUMING
;
367 retval
= request_irq (dev
->irq
, usb_hcd_irq
, SA_SHIRQ
,
368 hcd
->irq_descr
, hcd
);
370 dev_err (hcd
->self
.controller
,
371 "can't restore IRQ after resume!\n");
376 retval
= hcd
->driver
->resume (hcd
);
377 if (!HC_IS_RUNNING (hcd
->state
)) {
378 dev_dbg (hcd
->self
.controller
,
379 "resume fail, retval %d\n", retval
);
385 EXPORT_SYMBOL (usb_hcd_pci_resume
);
387 #endif /* CONFIG_PM */