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 normal, but custom bus glue should be ok */
39 /*-------------------------------------------------------------------------*/
41 static void hcd_pci_release(struct usb_bus *bus)
43 struct usb_hcd *hcd = bus->hcpriv;
46 hcd->driver->hcd_free(hcd);
49 /* configure so an HC device and id are always provided */
50 /* always called with process context; sleeping is OK */
53 * usb_hcd_pci_probe - initialize PCI-based HCDs
54 * @dev: USB Host Controller being probed
55 * @id: pci hotplug id connecting controller to HCD framework
56 * Context: !in_interrupt()
58 * Allocates basic PCI resources for this USB host controller, and
59 * then invokes the start() method for the HCD associated with it
60 * through the hotplug entry's driver_data.
62 * Store this function in the HCD's struct pci_driver as probe().
64 int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
66 struct hc_driver *driver;
67 unsigned long resource, len;
71 char buf [8], *bufp = buf;
76 if (!id || !(driver = (struct hc_driver *) id->driver_data))
79 if (pci_enable_device (dev) < 0)
83 err ("Found HC with no IRQ. Check BIOS/PCI %s setup!",
88 if (driver->flags & HCD_MEMORY) { // EHCI, OHCI
90 resource = pci_resource_start (dev, 0);
91 len = pci_resource_len (dev, 0);
92 if (!request_mem_region (resource, len, driver->description)) {
93 dbg ("controller already in use");
96 base = ioremap_nocache (resource, len);
98 dbg ("error mapping memory");
101 release_mem_region (resource, len);
102 err ("init %s fail, %d", pci_name(dev), retval);
108 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
109 if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
112 resource = pci_resource_start (dev, region);
113 len = pci_resource_len (dev, region);
114 if (request_region (resource, len,
115 driver->description))
118 if (region == PCI_ROM_RESOURCE) {
119 dbg ("no i/o regions available");
122 base = (void *) resource;
125 // driver->start(), later on, will transfer device from
126 // control by SMM/BIOS to control by Linux (if needed)
128 pci_set_master (dev);
129 hcd = driver->hcd_alloc ();
131 dbg ("hcd alloc fail");
134 if (driver->flags & HCD_MEMORY) {
138 release_region (resource, len);
139 err ("init %s fail, %d", pci_name(dev), retval);
143 pci_set_drvdata (dev, hcd);
144 hcd->driver = driver;
145 hcd->description = driver->description;
147 hcd->self.bus_name = pci_name(dev);
148 hcd->product_desc = dev->dev.name;
149 hcd->self.controller = &dev->dev;
150 hcd->controller = hcd->self.controller;
152 if ((retval = hcd_buffer_create (hcd)) != 0) {
154 driver->hcd_free (hcd);
158 dev_info (hcd->controller, "%s\n", hcd->product_desc);
161 sprintf (buf, "%d", dev->irq);
163 bufp = __irq_itoa(dev->irq);
165 if (request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ, hcd->description, hcd)
167 dev_err (hcd->controller,
168 "request interrupt %s failed\n", bufp);
175 hcd->region = region;
176 dev_info (hcd->controller, "irq %s, %s %p\n", bufp,
177 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
180 usb_bus_init (&hcd->self);
181 hcd->self.op = &usb_hcd_operations;
182 hcd->self.hcpriv = (void *) hcd;
183 hcd->self.release = &hcd_pci_release;
185 INIT_LIST_HEAD (&hcd->dev_list);
187 usb_register_bus (&hcd->self);
189 if ((retval = driver->start (hcd)) < 0)
190 usb_hcd_pci_remove (dev);
194 EXPORT_SYMBOL (usb_hcd_pci_probe);
197 /* may be called without controller electrically present */
198 /* may be called with controller, bus, and devices active */
201 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
202 * @dev: USB Host Controller being removed
203 * Context: !in_interrupt()
205 * Reverses the effect of usb_hcd_pci_probe(), first invoking
206 * the HCD's stop() method. It is always called from a thread
207 * context, normally "rmmod", "apmd", or something similar.
209 * Store this function in the HCD's struct pci_driver as remove().
211 void usb_hcd_pci_remove (struct pci_dev *dev)
214 struct usb_device *hub;
216 hcd = pci_get_drvdata(dev);
219 dev_info (hcd->controller, "remove, state %x\n", hcd->state);
224 hub = hcd->self.root_hub;
225 hcd->state = USB_STATE_QUIESCING;
227 dev_dbg (hcd->controller, "roothub graceful disconnect\n");
228 usb_disconnect (&hub);
230 hcd->driver->stop (hcd);
231 hcd_buffer_destroy (hcd);
232 hcd->state = USB_STATE_HALT;
233 pci_set_drvdata (dev, 0);
235 free_irq (hcd->irq, hcd);
236 if (hcd->driver->flags & HCD_MEMORY) {
238 release_mem_region (pci_resource_start (dev, 0),
239 pci_resource_len (dev, 0));
241 release_region (pci_resource_start (dev, hcd->region),
242 pci_resource_len (dev, hcd->region));
245 usb_deregister_bus (&hcd->self);
247 EXPORT_SYMBOL (usb_hcd_pci_remove);
253 * Some "sleep" power levels imply updating struct usb_driver
254 * to include a callback asking hcds to do their bit by checking
255 * if all the drivers can suspend. Gets involved with remote wakeup.
257 * If there are pending urbs, then HCs will need to access memory,
258 * causing extra power drain. New sleep()/wakeup() PM calls might
259 * be needed, beyond PCI suspend()/resume(). The root hub timer
260 * still be accessing memory though ...
262 * FIXME: USB should have some power budgeting support working with
265 * FIXME: This assumes only D0->D3 suspend and D3->D0 resume.
266 * D1 and D2 states should do something, yes?
268 * FIXME: Should provide generic enable_wake(), calling pci_enable_wake()
269 * for all supported states, so that USB remote wakeup can work for any
270 * devices that support it (and are connected via powered hubs).
272 * FIXME: resume doesn't seem to work right any more...
276 // 2.4 kernels have issued concurrent resumes (w/APM)
277 // we defend against that error; PCI doesn't yet.
280 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
281 * @dev: USB Host Controller being suspended
282 * @state: state that the controller is going into
284 * Store this function in the HCD's struct pci_driver as suspend().
286 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
291 hcd = pci_get_drvdata(dev);
292 dev_info (hcd->controller, "suspend to state %d\n", state);
294 pci_save_state (dev, hcd->pci_state);
296 // FIXME for all connected devices, leaf-to-root:
298 // proposed "new 2.5 driver model" will automate that
300 /* driver may want to disable DMA etc */
301 retval = hcd->driver->suspend (hcd, state);
302 hcd->state = USB_STATE_SUSPENDED;
304 pci_set_power_state (dev, state);
307 EXPORT_SYMBOL (usb_hcd_pci_suspend);
310 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
311 * @dev: USB Host Controller being resumed
313 * Store this function in the HCD's struct pci_driver as resume().
315 int usb_hcd_pci_resume (struct pci_dev *dev)
320 hcd = pci_get_drvdata(dev);
321 dev_info (hcd->controller, "resume\n");
323 /* guard against multiple resumes (APM bug?) */
324 atomic_inc (&hcd->resume_count);
325 if (atomic_read (&hcd->resume_count) != 1) {
326 dev_err (hcd->controller, "concurrent PCI resumes\n");
332 if (hcd->state != USB_STATE_SUSPENDED) {
333 dev_dbg (hcd->controller, "can't resume, not suspended!\n");
336 hcd->state = USB_STATE_RESUMING;
338 pci_set_power_state (dev, 0);
339 pci_restore_state (dev, hcd->pci_state);
341 retval = hcd->driver->resume (hcd);
342 if (!HCD_IS_RUNNING (hcd->state)) {
343 dev_dbg (hcd->controller, "resume fail, retval %d\n", retval);
345 // FIXME: recover, reset etc.
347 // FIXME for all connected devices, root-to-leaf:
348 // driver->resume ();
349 // proposed "new 2.5 driver model" will automate that
353 atomic_dec (&hcd->resume_count);
356 EXPORT_SYMBOL (usb_hcd_pci_resume);
358 #endif /* CONFIG_PM */