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 /* 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)
73 dev->current_state = 0;
74 dev->dev.power.power_state = 0;
78 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
84 if (driver->flags & HCD_MEMORY) { // EHCI, OHCI
86 resource = pci_resource_start (dev, 0);
87 len = pci_resource_len (dev, 0);
88 if (!request_mem_region (resource, len, driver->description)) {
89 dev_dbg (&dev->dev, "controller already in use\n");
93 base = ioremap_nocache (resource, len);
95 dev_dbg (&dev->dev, "error mapping memory\n");
98 release_mem_region (resource, len);
99 dev_err (&dev->dev, "init %s fail, %d\n",
100 pci_name(dev), retval);
106 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
107 if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
110 resource = pci_resource_start (dev, region);
111 len = pci_resource_len (dev, region);
112 if (request_region (resource, len,
113 driver->description))
116 if (region == PCI_ROM_RESOURCE) {
117 dev_dbg (&dev->dev, "no i/o regions available\n");
121 base = (void __iomem *) resource;
124 // driver->reset(), later on, will transfer device from
125 // control by SMM/BIOS to control by Linux (if needed)
127 hcd = usb_create_hcd (driver);
129 dev_dbg (&dev->dev, "hcd alloc fail\n");
132 if (driver->flags & HCD_MEMORY) {
136 release_region (resource, len);
137 dev_err (&dev->dev, "init %s fail, %d\n",
138 pci_name(dev), retval);
142 // hcd zeroed everything
144 hcd->region = region;
146 pci_set_drvdata (dev, hcd);
147 hcd->self.bus_name = pci_name(dev);
148 #ifdef CONFIG_PCI_NAMES
149 hcd->product_desc = dev->pretty_name;
151 hcd->self.controller = &dev->dev;
153 if ((retval = hcd_buffer_create (hcd)) != 0) {
155 pci_set_drvdata (dev, NULL);
160 dev_info (hcd->self.controller, "%s\n", hcd->product_desc);
162 /* till now HC has been in an indeterminate state ... */
163 if (driver->reset && (retval = driver->reset (hcd)) < 0) {
164 dev_err (hcd->self.controller, "can't reset\n");
168 pci_set_master (dev);
170 sprintf (buf, "%d", dev->irq);
172 bufp = __irq_itoa(dev->irq);
174 retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
175 hcd->driver->description, hcd);
177 dev_err (hcd->self.controller,
178 "request interrupt %s failed\n", bufp);
183 dev_info (hcd->self.controller, "irq %s, %s 0x%lx\n", bufp,
184 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
187 usb_register_bus (&hcd->self);
189 if ((retval = driver->start (hcd)) < 0) {
190 dev_err (hcd->self.controller, "init error %d\n", retval);
191 usb_hcd_pci_remove (dev);
196 pci_disable_device (dev);
199 EXPORT_SYMBOL (usb_hcd_pci_probe);
202 /* may be called without controller electrically present */
203 /* may be called with controller, bus, and devices active */
206 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
207 * @dev: USB Host Controller being removed
208 * Context: !in_interrupt()
210 * Reverses the effect of usb_hcd_pci_probe(), first invoking
211 * the HCD's stop() method. It is always called from a thread
212 * context, normally "rmmod", "apmd", or something similar.
214 * Store this function in the HCD's struct pci_driver as remove().
216 void usb_hcd_pci_remove (struct pci_dev *dev)
220 hcd = pci_get_drvdata(dev);
223 dev_info (hcd->self.controller, "remove, state %x\n", hcd->state);
228 if (HCD_IS_RUNNING (hcd->state))
229 hcd->state = USB_STATE_QUIESCING;
231 dev_dbg (hcd->self.controller, "roothub graceful disconnect\n");
232 usb_disconnect (&hcd->self.root_hub);
234 hcd->driver->stop (hcd);
235 hcd_buffer_destroy (hcd);
236 hcd->state = USB_STATE_HALT;
237 pci_set_drvdata(dev, NULL);
239 free_irq (hcd->irq, hcd);
240 if (hcd->driver->flags & HCD_MEMORY) {
242 release_mem_region (pci_resource_start (dev, 0),
243 pci_resource_len (dev, 0));
245 release_region (pci_resource_start (dev, hcd->region),
246 pci_resource_len (dev, hcd->region));
249 usb_deregister_bus (&hcd->self);
251 pci_disable_device(dev);
253 EXPORT_SYMBOL (usb_hcd_pci_remove);
258 static char __attribute_used__ *pci_state(u32 state)
264 case 3: return "D3hot";
265 case 4: return "D3cold";
271 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
272 * @dev: USB Host Controller being suspended
273 * @state: state that the controller is going into
275 * Store this function in the HCD's struct pci_driver as suspend().
277 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
283 hcd = pci_get_drvdata(dev);
285 /* even when the PCI layer rejects some of the PCI calls
286 * below, HCs can try global suspend and reduce DMA traffic.
287 * PM-sensitive HCDs may already have done this.
289 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
293 switch (hcd->state) {
295 /* entry if root hub wasn't yet suspended ... from sysfs,
296 * without autosuspend, or if USB_SUSPEND isn't configured.
298 case USB_STATE_RUNNING:
299 hcd->state = USB_STATE_QUIESCING;
300 retval = hcd->driver->suspend (hcd, state);
302 dev_dbg (hcd->self.controller,
303 "suspend fail, retval %d\n",
307 hcd->state = HCD_STATE_SUSPENDED;
310 /* entry with CONFIG_USB_SUSPEND, or hcds that autosuspend: the
311 * controller and/or root hub will already have been suspended,
312 * but it won't be ready for a PCI resume call.
314 * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will
315 * have been called, otherwise root hub timers still run ...
317 case HCD_STATE_SUSPENDED:
318 if (state <= dev->current_state)
321 /* no DMA or IRQs except in D0 */
322 if (!dev->current_state) {
323 pci_save_state (dev);
324 pci_disable_device (dev);
325 free_irq (hcd->irq, hcd);
329 dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n");
333 /* POLICY: ignore D1/D2/D3hot differences;
334 * we know D3hot will always work.
336 retval = pci_set_power_state (dev, state);
337 if (retval < 0 && state < 3) {
338 retval = pci_set_power_state (dev, 3);
343 dev_dbg (hcd->self.controller, "--> PCI %s\n",
344 pci_state(dev->current_state));
345 #ifdef CONFIG_USB_SUSPEND
346 pci_enable_wake (dev, state, hcd->remote_wakeup);
347 pci_enable_wake (dev, 4, hcd->remote_wakeup);
349 } else if (retval < 0) {
350 dev_dbg (&dev->dev, "PCI %s suspend fail, %d\n",
351 pci_state(state), retval);
352 (void) usb_hcd_pci_resume (dev);
357 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n",
363 /* update power_state **ONLY** to make sysfs happier */
365 dev->dev.power.power_state = state;
368 EXPORT_SYMBOL (usb_hcd_pci_suspend);
371 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
372 * @dev: USB Host Controller being resumed
374 * Store this function in the HCD's struct pci_driver as resume().
376 int usb_hcd_pci_resume (struct pci_dev *dev)
382 hcd = pci_get_drvdata(dev);
383 if (hcd->state != HCD_STATE_SUSPENDED) {
384 dev_dbg (hcd->self.controller,
385 "can't resume, not suspended!\n");
388 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
390 /* D3cold resume isn't usually reported this way... */
391 dev_dbg(hcd->self.controller, "resume from PCI %s%s\n",
392 pci_state(dev->current_state),
393 has_pci_pm ? "" : " (legacy)");
395 hcd->state = USB_STATE_RESUMING;
398 pci_set_power_state (dev, 0);
399 dev->dev.power.power_state = 0;
400 retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
401 hcd->driver->description, hcd);
403 dev_err (hcd->self.controller,
404 "can't restore IRQ after resume!\n");
408 pci_restore_state (dev);
409 #ifdef CONFIG_USB_SUSPEND
410 pci_enable_wake (dev, dev->current_state, 0);
411 pci_enable_wake (dev, 4, 0);
414 retval = hcd->driver->resume (hcd);
415 if (!HCD_IS_RUNNING (hcd->state)) {
416 dev_dbg (hcd->self.controller,
417 "resume fail, retval %d\n", retval);
423 EXPORT_SYMBOL (usb_hcd_pci_resume);
425 #endif /* CONFIG_PM */