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>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/pci.h>
26 #ifdef CONFIG_USB_DEBUG
32 #include <linux/usb.h>
36 /* PCI-based HCs are normal, but custom bus glue should be ok */
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
;
59 unsigned long resource
, len
;
63 char buf
[8], *bufp
= buf
;
68 if (!id
|| !(driver
= (struct hc_driver
*) id
->driver_data
))
71 if (pci_enable_device (dev
) < 0)
75 err ("Found HC with no IRQ. Check BIOS/PCI %s setup!",
80 if (driver
->flags
& HCD_MEMORY
) { // EHCI, OHCI
82 resource
= pci_resource_start (dev
, 0);
83 len
= pci_resource_len (dev
, 0);
84 if (!request_mem_region (resource
, len
, driver
->description
)) {
85 dbg ("controller already in use");
88 base
= ioremap_nocache (resource
, len
);
90 dbg ("error mapping memory");
93 release_mem_region (resource
, len
);
94 err ("init %s fail, %d", dev
->slot_name
, retval
);
100 for (region
= 0; region
< PCI_ROM_RESOURCE
; region
++) {
101 if (!(pci_resource_flags (dev
, region
) & IORESOURCE_IO
))
104 resource
= pci_resource_start (dev
, region
);
105 len
= pci_resource_len (dev
, region
);
106 if (request_region (resource
, len
,
107 driver
->description
))
110 if (region
== PCI_ROM_RESOURCE
) {
111 dbg ("no i/o regions available");
114 base
= (void *) resource
;
117 // driver->start(), later on, will transfer device from
118 // control by SMM/BIOS to control by Linux (if needed)
120 pci_set_master (dev
);
121 hcd
= driver
->hcd_alloc ();
123 dbg ("hcd alloc fail");
126 if (driver
->flags
& HCD_MEMORY
) {
130 release_region (resource
, len
);
131 err ("init %s fail, %d", dev
->slot_name
, retval
);
135 pci_set_drvdata (dev
, hcd
);
136 hcd
->driver
= driver
;
137 hcd
->description
= driver
->description
;
139 hcd
->self
.bus_name
= dev
->slot_name
;
140 hcd
->product_desc
= dev
->dev
.name
;
141 hcd
->controller
= &dev
->dev
;
143 if ((retval
= hcd_buffer_create (hcd
)) != 0) {
145 driver
->hcd_free (hcd
);
149 info ("%s @ %s, %s", hcd
->description
, dev
->slot_name
, dev
->dev
.name
);
152 sprintf (buf
, "%d", dev
->irq
);
154 bufp
= __irq_itoa(dev
->irq
);
156 if (request_irq (dev
->irq
, usb_hcd_irq
, SA_SHIRQ
, hcd
->description
, hcd
)
158 err ("request interrupt %s failed", bufp
);
165 hcd
->region
= region
;
166 info ("irq %s, %s %p", bufp
,
167 (driver
->flags
& HCD_MEMORY
) ? "pci mem" : "io base",
170 usb_bus_init (&hcd
->self
);
171 hcd
->self
.op
= &usb_hcd_operations
;
172 hcd
->self
.hcpriv
= (void *) hcd
;
174 INIT_LIST_HEAD (&hcd
->dev_list
);
176 usb_register_bus (&hcd
->self
);
178 if ((retval
= driver
->start (hcd
)) < 0)
179 usb_hcd_pci_remove (dev
);
183 EXPORT_SYMBOL (usb_hcd_pci_probe
);
186 /* may be called without controller electrically present */
187 /* may be called with controller, bus, and devices active */
190 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
191 * @dev: USB Host Controller being removed
192 * Context: !in_interrupt()
194 * Reverses the effect of usb_hcd_pci_probe(), first invoking
195 * the HCD's stop() method. It is always called from a thread
196 * context, normally "rmmod", "apmd", or something similar.
198 * Store this function in the HCD's struct pci_driver as remove().
200 void usb_hcd_pci_remove (struct pci_dev
*dev
)
203 struct usb_device
*hub
;
205 hcd
= pci_get_drvdata(dev
);
208 info ("remove: %s, state %x", hcd
->self
.bus_name
, hcd
->state
);
210 if (in_interrupt ()) BUG ();
212 hub
= hcd
->self
.root_hub
;
213 hcd
->state
= USB_STATE_QUIESCING
;
215 dbg ("%s: roothub graceful disconnect", hcd
->self
.bus_name
);
216 usb_disconnect (&hub
);
218 hcd
->driver
->stop (hcd
);
219 hcd_buffer_destroy (hcd
);
220 hcd
->state
= USB_STATE_HALT
;
222 free_irq (hcd
->irq
, hcd
);
223 if (hcd
->driver
->flags
& HCD_MEMORY
) {
225 release_mem_region (pci_resource_start (dev
, 0),
226 pci_resource_len (dev
, 0));
228 release_region (pci_resource_start (dev
, hcd
->region
),
229 pci_resource_len (dev
, hcd
->region
));
232 usb_deregister_bus (&hcd
->self
);
233 if (atomic_read (&hcd
->self
.refcnt
) != 1)
234 err ("usb_hcd_pci_remove %s, count != 1", hcd
->self
.bus_name
);
236 hcd
->driver
->hcd_free (hcd
);
238 EXPORT_SYMBOL (usb_hcd_pci_remove
);
244 * Some "sleep" power levels imply updating struct usb_driver
245 * to include a callback asking hcds to do their bit by checking
246 * if all the drivers can suspend. Gets involved with remote wakeup.
248 * If there are pending urbs, then HCs will need to access memory,
249 * causing extra power drain. New sleep()/wakeup() PM calls might
250 * be needed, beyond PCI suspend()/resume(). The root hub timer
251 * still be accessing memory though ...
253 * FIXME: USB should have some power budgeting support working with
256 * FIXME: This assumes only D0->D3 suspend and D3->D0 resume.
257 * D1 and D2 states should do something, yes?
259 * FIXME: Should provide generic enable_wake(), calling pci_enable_wake()
260 * for all supported states, so that USB remote wakeup can work for any
261 * devices that support it (and are connected via powered hubs).
263 * FIXME: resume doesn't seem to work right any more...
267 // 2.4 kernels have issued concurrent resumes (w/APM)
268 // we defend against that error; PCI doesn't yet.
271 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
272 * @dev: USB Host Controller being suspended
274 * Store this function in the HCD's struct pci_driver as suspend().
276 int usb_hcd_pci_suspend (struct pci_dev
*dev
, u32 state
)
281 hcd
= pci_get_drvdata(dev
);
282 info ("suspend %s to state %d", hcd
->self
.bus_name
, state
);
284 pci_save_state (dev
, hcd
->pci_state
);
286 // FIXME for all connected devices, leaf-to-root:
288 // proposed "new 2.5 driver model" will automate that
290 /* driver may want to disable DMA etc */
291 retval
= hcd
->driver
->suspend (hcd
, state
);
292 hcd
->state
= USB_STATE_SUSPENDED
;
294 pci_set_power_state (dev
, state
);
297 EXPORT_SYMBOL (usb_hcd_pci_suspend
);
300 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
301 * @dev: USB Host Controller being resumed
303 * Store this function in the HCD's struct pci_driver as resume().
305 int usb_hcd_pci_resume (struct pci_dev
*dev
)
310 hcd
= pci_get_drvdata(dev
);
311 info ("resume %s", hcd
->self
.bus_name
);
313 /* guard against multiple resumes (APM bug?) */
314 atomic_inc (&hcd
->resume_count
);
315 if (atomic_read (&hcd
->resume_count
) != 1) {
316 err ("concurrent PCI resumes for %s", hcd
->self
.bus_name
);
322 if (hcd
->state
!= USB_STATE_SUSPENDED
) {
323 dbg ("can't resume, not suspended!");
326 hcd
->state
= USB_STATE_RESUMING
;
328 pci_set_power_state (dev
, 0);
329 pci_restore_state (dev
, hcd
->pci_state
);
331 retval
= hcd
->driver
->resume (hcd
);
332 if (!HCD_IS_RUNNING (hcd
->state
)) {
333 dbg ("resume %s failure, retval %d", hcd
->self
.bus_name
, retval
);
335 // FIXME: recover, reset etc.
337 // FIXME for all connected devices, root-to-leaf:
338 // driver->resume ();
339 // proposed "new 2.5 driver model" will automate that
343 atomic_dec (&hcd
->resume_count
);
346 EXPORT_SYMBOL (usb_hcd_pci_resume
);
348 #endif /* CONFIG_PM */