vserver 1.9.5.x5
[linux-2.6.git] / drivers / usb / core / hcd-pci.c
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.
8  *
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.
13  *
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.
17  */
18
19 #include <linux/config.h>
20
21 #ifdef CONFIG_USB_DEBUG
22         #define DEBUG
23 #else
24         #undef DEBUG
25 #endif
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <linux/usb.h>
33 #include "hcd.h"
34
35
36 /* PCI-based HCs are normal, but custom bus glue should be ok */
37
38
39 /*-------------------------------------------------------------------------*/
40
41 /* configure so an HC device and id are always provided */
42 /* always called with process context; sleeping is OK */
43
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()
49  *
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.
53  *
54  * Store this function in the HCD's struct pci_driver as probe().
55  */
56 int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
57 {
58         struct hc_driver        *driver;
59         unsigned long           resource, len;
60         void __iomem            *base;
61         struct usb_hcd          *hcd;
62         int                     retval, region;
63         char                    buf [8], *bufp = buf;
64
65         if (usb_disabled())
66                 return -ENODEV;
67
68         if (!id || !(driver = (struct hc_driver *) id->driver_data))
69                 return -EINVAL;
70
71         if (pci_enable_device (dev) < 0)
72                 return -ENODEV;
73         dev->current_state = 0;
74         dev->dev.power.power_state = 0;
75         
76         if (!dev->irq) {
77                 dev_err (&dev->dev,
78                         "Found HC with no IRQ.  Check BIOS/PCI %s setup!\n",
79                         pci_name(dev));
80                 retval = -ENODEV;
81                 goto done;
82         }
83         
84         if (driver->flags & HCD_MEMORY) {       // EHCI, OHCI
85                 region = 0;
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");
90                         retval = -EBUSY;
91                         goto done;
92                 }
93                 base = ioremap_nocache (resource, len);
94                 if (base == NULL) {
95                         dev_dbg (&dev->dev, "error mapping memory\n");
96                         retval = -EFAULT;
97 clean_1:
98                         release_mem_region (resource, len);
99                         dev_err (&dev->dev, "init %s fail, %d\n",
100                                 pci_name(dev), retval);
101                         goto done;
102                 }
103
104         } else {                                // UHCI
105                 resource = len = 0;
106                 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
107                         if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
108                                 continue;
109
110                         resource = pci_resource_start (dev, region);
111                         len = pci_resource_len (dev, region);
112                         if (request_region (resource, len,
113                                         driver->description))
114                                 break;
115                 }
116                 if (region == PCI_ROM_RESOURCE) {
117                         dev_dbg (&dev->dev, "no i/o regions available\n");
118                         retval = -EBUSY;
119                         goto done;
120                 }
121                 base = (void __iomem *) resource;
122         }
123
124         // driver->reset(), later on, will transfer device from
125         // control by SMM/BIOS to control by Linux (if needed)
126
127         hcd = usb_create_hcd (driver);
128         if (hcd == NULL){
129                 dev_dbg (&dev->dev, "hcd alloc fail\n");
130                 retval = -ENOMEM;
131 clean_2:
132                 if (driver->flags & HCD_MEMORY) {
133                         iounmap (base);
134                         goto clean_1;
135                 } else {
136                         release_region (resource, len);
137                         dev_err (&dev->dev, "init %s fail, %d\n",
138                                 pci_name(dev), retval);
139                         goto done;
140                 }
141         }
142         // hcd zeroed everything
143         hcd->regs = base;
144         hcd->region = region;
145
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;
150 #endif
151         hcd->self.controller = &dev->dev;
152
153         if ((retval = hcd_buffer_create (hcd)) != 0) {
154 clean_3:
155                 pci_set_drvdata (dev, NULL);
156                 usb_put_hcd (hcd);
157                 goto clean_2;
158         }
159
160         dev_info (hcd->self.controller, "%s\n", hcd->product_desc);
161
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");
165                 goto clean_3;
166         }
167
168         pci_set_master (dev);
169 #ifndef __sparc__
170         sprintf (buf, "%d", dev->irq);
171 #else
172         bufp = __irq_itoa(dev->irq);
173 #endif
174         retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
175                                 hcd->driver->description, hcd);
176         if (retval != 0) {
177                 dev_err (hcd->self.controller,
178                                 "request interrupt %s failed\n", bufp);
179                 goto clean_3;
180         }
181         hcd->irq = dev->irq;
182
183         dev_info (hcd->self.controller, "irq %s, %s 0x%lx\n", bufp,
184                 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
185                 resource);
186
187         usb_register_bus (&hcd->self);
188
189         if ((retval = driver->start (hcd)) < 0) {
190                 dev_err (hcd->self.controller, "init error %d\n", retval);
191                 usb_hcd_pci_remove (dev);
192         }
193
194 done:
195         if (retval != 0)
196                 pci_disable_device (dev);
197         return retval;
198
199 EXPORT_SYMBOL (usb_hcd_pci_probe);
200
201
202 /* may be called without controller electrically present */
203 /* may be called with controller, bus, and devices active */
204
205 /**
206  * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
207  * @dev: USB Host Controller being removed
208  * Context: !in_interrupt()
209  *
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.
213  *
214  * Store this function in the HCD's struct pci_driver as remove().
215  */
216 void usb_hcd_pci_remove (struct pci_dev *dev)
217 {
218         struct usb_hcd          *hcd;
219
220         hcd = pci_get_drvdata(dev);
221         if (!hcd)
222                 return;
223         dev_info (hcd->self.controller, "remove, state %x\n", hcd->state);
224
225         if (in_interrupt ())
226                 BUG ();
227
228         if (HCD_IS_RUNNING (hcd->state))
229                 hcd->state = USB_STATE_QUIESCING;
230
231         dev_dbg (hcd->self.controller, "roothub graceful disconnect\n");
232         usb_disconnect (&hcd->self.root_hub);
233
234         hcd->driver->stop (hcd);
235         hcd_buffer_destroy (hcd);
236         hcd->state = USB_STATE_HALT;
237         pci_set_drvdata(dev, NULL);
238
239         free_irq (hcd->irq, hcd);
240         if (hcd->driver->flags & HCD_MEMORY) {
241                 iounmap (hcd->regs);
242                 release_mem_region (pci_resource_start (dev, 0),
243                         pci_resource_len (dev, 0));
244         } else {
245                 release_region (pci_resource_start (dev, hcd->region),
246                         pci_resource_len (dev, hcd->region));
247         }
248
249         usb_deregister_bus (&hcd->self);
250
251         pci_disable_device(dev);
252 }
253 EXPORT_SYMBOL (usb_hcd_pci_remove);
254
255
256 #ifdef  CONFIG_PM
257
258 static char __attribute_used__ *pci_state(u32 state)
259 {
260         switch (state) {
261         case 0:         return "D0";
262         case 1:         return "D1";
263         case 2:         return "D2";
264         case 3:         return "D3hot";
265         case 4:         return "D3cold";
266         }
267         return NULL;
268 }
269
270 /**
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
274  *
275  * Store this function in the HCD's struct pci_driver as suspend().
276  */
277 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
278 {
279         struct usb_hcd          *hcd;
280         int                     retval = 0;
281         int                     has_pci_pm;
282
283         hcd = pci_get_drvdata(dev);
284
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.
288          */
289         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
290         if (state > 4)
291                 state = 4;
292
293         switch (hcd->state) {
294
295         /* entry if root hub wasn't yet suspended ... from sysfs,
296          * without autosuspend, or if USB_SUSPEND isn't configured.
297          */
298         case USB_STATE_RUNNING:
299                 hcd->state = USB_STATE_QUIESCING;
300                 retval = hcd->driver->suspend (hcd, state);
301                 if (retval) {
302                         dev_dbg (hcd->self.controller, 
303                                         "suspend fail, retval %d\n",
304                                         retval);
305                         break;
306                 }
307                 hcd->state = HCD_STATE_SUSPENDED;
308                 /* FALLTHROUGH */
309
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.
313          *
314          * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will
315          * have been called, otherwise root hub timers still run ...
316          */
317         case HCD_STATE_SUSPENDED:
318                 if (state <= dev->current_state)
319                         break;
320
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);
326                 }
327
328                 if (!has_pci_pm) {
329                         dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n");
330                         break;
331                 }
332
333                 /* POLICY: ignore D1/D2/D3hot differences;
334                  * we know D3hot will always work.
335                  */
336                 retval = pci_set_power_state (dev, state);
337                 if (retval < 0 && state < 3) {
338                         retval = pci_set_power_state (dev, 3);
339                         if (retval == 0)
340                                 state = 3;
341                 }
342                 if (retval == 0) {
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);
348 #endif
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);
353                         break;
354                 }
355                 break;
356         default:
357                 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n",
358                         hcd->state);
359                 retval = -EINVAL;
360                 break;
361         }
362
363         /* update power_state **ONLY** to make sysfs happier */
364         if (retval == 0)
365                 dev->dev.power.power_state = state;
366         return retval;
367 }
368 EXPORT_SYMBOL (usb_hcd_pci_suspend);
369
370 /**
371  * usb_hcd_pci_resume - power management resume of a PCI-based HCD
372  * @dev: USB Host Controller being resumed
373  *
374  * Store this function in the HCD's struct pci_driver as resume().
375  */
376 int usb_hcd_pci_resume (struct pci_dev *dev)
377 {
378         struct usb_hcd          *hcd;
379         int                     retval;
380         int                     has_pci_pm;
381
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");
386                 return 0;
387         }
388         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
389
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)");
394
395         hcd->state = USB_STATE_RESUMING;
396
397         if (has_pci_pm)
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);
402         if (retval < 0) {
403                 dev_err (hcd->self.controller,
404                         "can't restore IRQ after resume!\n");
405                 return retval;
406         }
407         hcd->saw_irq = 0;
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);
412 #endif
413
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);
418                 usb_hc_died (hcd);
419         }
420
421         return retval;
422 }
423 EXPORT_SYMBOL (usb_hcd_pci_resume);
424
425 #endif  /* CONFIG_PM */
426
427