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 = driver->hcd_alloc ();
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->driver = driver;
148 hcd->description = driver->description;
149 hcd->self.bus_name = pci_name(dev);
150 #ifdef CONFIG_PCI_NAMES
151 hcd->product_desc = dev->pretty_name;
153 if (hcd->product_desc == NULL)
154 hcd->product_desc = "USB Host Controller";
156 hcd->self.controller = &dev->dev;
158 if ((retval = hcd_buffer_create (hcd)) != 0) {
164 dev_info (hcd->self.controller, "%s\n", hcd->product_desc);
166 /* till now HC has been in an indeterminate state ... */
167 if (driver->reset && (retval = driver->reset (hcd)) < 0) {
168 dev_err (hcd->self.controller, "can't reset\n");
171 hcd->state = USB_STATE_HALT;
173 pci_set_master (dev);
175 sprintf (buf, "%d", dev->irq);
177 bufp = __irq_itoa(dev->irq);
179 retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
180 hcd->description, hcd);
182 dev_err (hcd->self.controller,
183 "request interrupt %s failed\n", bufp);
188 dev_info (hcd->self.controller, "irq %s, %s 0x%lx\n", bufp,
189 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
192 usb_bus_init (&hcd->self);
193 hcd->self.op = &usb_hcd_operations;
194 hcd->self.release = &usb_hcd_release;
195 hcd->self.hcpriv = (void *) hcd;
196 init_timer (&hcd->rh_timer);
198 INIT_LIST_HEAD (&hcd->dev_list);
200 usb_register_bus (&hcd->self);
202 if ((retval = driver->start (hcd)) < 0) {
203 dev_err (hcd->self.controller, "init error %d\n", retval);
204 usb_hcd_pci_remove (dev);
209 pci_disable_device (dev);
212 EXPORT_SYMBOL (usb_hcd_pci_probe);
215 /* may be called without controller electrically present */
216 /* may be called with controller, bus, and devices active */
219 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
220 * @dev: USB Host Controller being removed
221 * Context: !in_interrupt()
223 * Reverses the effect of usb_hcd_pci_probe(), first invoking
224 * the HCD's stop() method. It is always called from a thread
225 * context, normally "rmmod", "apmd", or something similar.
227 * Store this function in the HCD's struct pci_driver as remove().
229 void usb_hcd_pci_remove (struct pci_dev *dev)
233 hcd = pci_get_drvdata(dev);
236 dev_info (hcd->self.controller, "remove, state %x\n", hcd->state);
241 if (HCD_IS_RUNNING (hcd->state))
242 hcd->state = USB_STATE_QUIESCING;
244 dev_dbg (hcd->self.controller, "roothub graceful disconnect\n");
245 usb_disconnect (&hcd->self.root_hub);
247 hcd->driver->stop (hcd);
248 hcd_buffer_destroy (hcd);
249 hcd->state = USB_STATE_HALT;
250 pci_set_drvdata(dev, NULL);
252 free_irq (hcd->irq, hcd);
253 if (hcd->driver->flags & HCD_MEMORY) {
255 release_mem_region (pci_resource_start (dev, 0),
256 pci_resource_len (dev, 0));
258 release_region (pci_resource_start (dev, hcd->region),
259 pci_resource_len (dev, hcd->region));
262 usb_deregister_bus (&hcd->self);
264 pci_disable_device(dev);
266 EXPORT_SYMBOL (usb_hcd_pci_remove);
271 static char __attribute_used__ *pci_state(u32 state)
277 case 3: return "D3hot";
278 case 4: return "D3cold";
284 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
285 * @dev: USB Host Controller being suspended
286 * @state: state that the controller is going into
288 * Store this function in the HCD's struct pci_driver as suspend().
290 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
296 hcd = pci_get_drvdata(dev);
298 /* even when the PCI layer rejects some of the PCI calls
299 * below, HCs can try global suspend and reduce DMA traffic.
300 * PM-sensitive HCDs may already have done this.
302 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
306 switch (hcd->state) {
308 /* entry if root hub wasn't yet suspended ... from sysfs,
309 * without autosuspend, or if USB_SUSPEND isn't configured.
311 case USB_STATE_RUNNING:
312 hcd->state = USB_STATE_QUIESCING;
313 retval = hcd->driver->suspend (hcd, state);
315 dev_dbg (hcd->self.controller,
316 "suspend fail, retval %d\n",
320 hcd->state = HCD_STATE_SUSPENDED;
323 /* entry with CONFIG_USB_SUSPEND, or hcds that autosuspend: the
324 * controller and/or root hub will already have been suspended,
325 * but it won't be ready for a PCI resume call.
327 * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will
328 * have been called, otherwise root hub timers still run ...
330 case HCD_STATE_SUSPENDED:
331 if (state <= dev->current_state)
334 /* no DMA or IRQs except in D0 */
335 if (!dev->current_state) {
336 pci_save_state (dev);
337 pci_disable_device (dev);
338 free_irq (hcd->irq, hcd);
342 dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n");
346 /* POLICY: ignore D1/D2/D3hot differences;
347 * we know D3hot will always work.
349 retval = pci_set_power_state (dev, state);
350 if (retval < 0 && state < 3) {
351 retval = pci_set_power_state (dev, 3);
356 dev_dbg (hcd->self.controller, "--> PCI %s\n",
357 pci_state(dev->current_state));
358 #ifdef CONFIG_USB_SUSPEND
359 pci_enable_wake (dev, state, hcd->remote_wakeup);
360 pci_enable_wake (dev, 4, hcd->remote_wakeup);
362 } else if (retval < 0) {
363 dev_dbg (&dev->dev, "PCI %s suspend fail, %d\n",
364 pci_state(state), retval);
365 (void) usb_hcd_pci_resume (dev);
370 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n",
376 /* update power_state **ONLY** to make sysfs happier */
378 dev->dev.power.power_state = state;
381 EXPORT_SYMBOL (usb_hcd_pci_suspend);
384 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
385 * @dev: USB Host Controller being resumed
387 * Store this function in the HCD's struct pci_driver as resume().
389 int usb_hcd_pci_resume (struct pci_dev *dev)
395 hcd = pci_get_drvdata(dev);
396 if (hcd->state != HCD_STATE_SUSPENDED) {
397 dev_dbg (hcd->self.controller,
398 "can't resume, not suspended!\n");
401 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
403 /* D3cold resume isn't usually reported this way... */
404 dev_dbg(hcd->self.controller, "resume from PCI %s%s\n",
405 pci_state(dev->current_state),
406 has_pci_pm ? "" : " (legacy)");
408 hcd->state = USB_STATE_RESUMING;
411 pci_set_power_state (dev, 0);
412 dev->dev.power.power_state = 0;
413 retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
414 hcd->description, hcd);
416 dev_err (hcd->self.controller,
417 "can't restore IRQ after resume!\n");
421 pci_restore_state (dev);
422 #ifdef CONFIG_USB_SUSPEND
423 pci_enable_wake (dev, dev->current_state, 0);
424 pci_enable_wake (dev, 4, 0);
427 retval = hcd->driver->resume (hcd);
428 if (!HCD_IS_RUNNING (hcd->state)) {
429 dev_dbg (hcd->self.controller,
430 "resume fail, retval %d\n", retval);
436 EXPORT_SYMBOL (usb_hcd_pci_resume);
438 #endif /* CONFIG_PM */