Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / usb / core / hcd-pci.c
blobdc0ef310d31671b83d235fd8bb366c266395c223
1 /*
2 * (C) Copyright David Brownell 2000-2002
3 *
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
12 * for more details.
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>
23 #include <asm/io.h>
24 #include <asm/irq.h>
26 #ifdef CONFIG_USB_DEBUG
27 #define DEBUG
28 #else
29 #undef DEBUG
30 #endif
32 #include <linux/usb.h>
33 #include "hcd.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 */
44 /**
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;
60 void *base;
61 struct usb_hcd *hcd;
62 int retval, region;
63 char buf [8], *bufp = buf;
65 if (usb_disabled())
66 return -ENODEV;
68 if (!id || !(driver = (struct hc_driver *) id->driver_data))
69 return -EINVAL;
71 if (pci_enable_device (dev) < 0)
72 return -ENODEV;
74 if (!dev->irq) {
75 err ("Found HC with no IRQ. Check BIOS/PCI %s setup!",
76 dev->slot_name);
77 return -ENODEV;
80 if (driver->flags & HCD_MEMORY) { // EHCI, OHCI
81 region = 0;
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");
86 return -EBUSY;
88 base = ioremap_nocache (resource, len);
89 if (base == NULL) {
90 dbg ("error mapping memory");
91 retval = -EFAULT;
92 clean_1:
93 release_mem_region (resource, len);
94 err ("init %s fail, %d", dev->slot_name, retval);
95 return retval;
98 } else { // UHCI
99 resource = len = 0;
100 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
101 if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
102 continue;
104 resource = pci_resource_start (dev, region);
105 len = pci_resource_len (dev, region);
106 if (request_region (resource, len,
107 driver->description))
108 break;
110 if (region == PCI_ROM_RESOURCE) {
111 dbg ("no i/o regions available");
112 return -EBUSY;
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 ();
122 if (hcd == NULL){
123 dbg ("hcd alloc fail");
124 retval = -ENOMEM;
125 clean_2:
126 if (driver->flags & HCD_MEMORY) {
127 iounmap (base);
128 goto clean_1;
129 } else {
130 release_region (resource, len);
131 err ("init %s fail, %d", dev->slot_name, retval);
132 return retval;
135 pci_set_drvdata (dev, hcd);
136 hcd->driver = driver;
137 hcd->description = driver->description;
138 hcd->pdev = dev;
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) {
144 clean_3:
145 driver->hcd_free (hcd);
146 goto clean_2;
149 info ("%s @ %s, %s", hcd->description, dev->slot_name, dev->dev.name);
151 #ifndef __sparc__
152 sprintf (buf, "%d", dev->irq);
153 #else
154 bufp = __irq_itoa(dev->irq);
155 #endif
156 if (request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ, hcd->description, hcd)
157 != 0) {
158 err ("request interrupt %s failed", bufp);
159 retval = -EBUSY;
160 goto clean_3;
162 hcd->irq = dev->irq;
164 hcd->regs = base;
165 hcd->region = region;
166 info ("irq %s, %s %p", bufp,
167 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
168 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);
181 return retval;
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)
202 struct usb_hcd *hcd;
203 struct usb_device *hub;
205 hcd = pci_get_drvdata(dev);
206 if (!hcd)
207 return;
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) {
224 iounmap (hcd->regs);
225 release_mem_region (pci_resource_start (dev, 0),
226 pci_resource_len (dev, 0));
227 } else {
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);
241 #ifdef CONFIG_PM
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
254 * all kinds of hubs.
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)
278 struct usb_hcd *hcd;
279 int retval;
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:
287 // driver->suspend()
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);
295 return retval;
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)
307 struct usb_hcd *hcd;
308 int retval;
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);
317 retval = 0;
318 goto done;
321 retval = -EBUSY;
322 if (hcd->state != USB_STATE_SUSPENDED) {
323 dbg ("can't resume, not suspended!");
324 goto done;
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);
334 usb_hc_died (hcd);
335 // FIXME: recover, reset etc.
336 } else {
337 // FIXME for all connected devices, root-to-leaf:
338 // driver->resume ();
339 // proposed "new 2.5 driver model" will automate that
342 done:
343 atomic_dec (&hcd->resume_count);
344 return retval;
346 EXPORT_SYMBOL (usb_hcd_pci_resume);
348 #endif /* CONFIG_PM */