vserver 1.9.3
[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 static void hcd_pci_release(struct usb_bus *bus)
42 {
43         struct usb_hcd *hcd = bus->hcpriv;
44
45         if (hcd)
46                 hcd->driver->hcd_free(hcd);
47 }
48
49 /* configure so an HC device and id are always provided */
50 /* always called with process context; sleeping is OK */
51
52 /**
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()
57  *
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.
61  *
62  * Store this function in the HCD's struct pci_driver as probe().
63  */
64 int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id)
65 {
66         struct hc_driver        *driver;
67         unsigned long           resource, len;
68         void __iomem            *base;
69         struct usb_hcd          *hcd;
70         int                     retval, region;
71         char                    buf [8], *bufp = buf;
72
73         if (usb_disabled())
74                 return -ENODEV;
75
76         if (!id || !(driver = (struct hc_driver *) id->driver_data))
77                 return -EINVAL;
78
79         if (pci_enable_device (dev) < 0)
80                 return -ENODEV;
81         
82         if (!dev->irq) {
83                 dev_err (&dev->dev,
84                         "Found HC with no IRQ.  Check BIOS/PCI %s setup!\n",
85                         pci_name(dev));
86                 return -ENODEV;
87         }
88         
89         if (driver->flags & HCD_MEMORY) {       // EHCI, OHCI
90                 region = 0;
91                 resource = pci_resource_start (dev, 0);
92                 len = pci_resource_len (dev, 0);
93                 if (!request_mem_region (resource, len, driver->description)) {
94                         dev_dbg (&dev->dev, "controller already in use\n");
95                         return -EBUSY;
96                 }
97                 base = ioremap_nocache (resource, len);
98                 if (base == NULL) {
99                         dev_dbg (&dev->dev, "error mapping memory\n");
100                         retval = -EFAULT;
101 clean_1:
102                         release_mem_region (resource, len);
103                         dev_err (&dev->dev, "init %s fail, %d\n",
104                                 pci_name(dev), retval);
105                         return retval;
106                 }
107
108         } else {                                // UHCI
109                 resource = len = 0;
110                 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
111                         if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
112                                 continue;
113
114                         resource = pci_resource_start (dev, region);
115                         len = pci_resource_len (dev, region);
116                         if (request_region (resource, len,
117                                         driver->description))
118                                 break;
119                 }
120                 if (region == PCI_ROM_RESOURCE) {
121                         dev_dbg (&dev->dev, "no i/o regions available\n");
122                         return -EBUSY;
123                 }
124                 base = (void __iomem *) resource;
125         }
126
127         // driver->reset(), later on, will transfer device from
128         // control by SMM/BIOS to control by Linux (if needed)
129
130         hcd = driver->hcd_alloc ();
131         if (hcd == NULL){
132                 dev_dbg (&dev->dev, "hcd alloc fail\n");
133                 retval = -ENOMEM;
134 clean_2:
135                 if (driver->flags & HCD_MEMORY) {
136                         iounmap (base);
137                         goto clean_1;
138                 } else {
139                         release_region (resource, len);
140                         dev_err (&dev->dev, "init %s fail, %d\n",
141                                 pci_name(dev), retval);
142                         return retval;
143                 }
144         }
145         // hcd zeroed everything
146         hcd->regs = base;
147         hcd->region = region;
148
149         pci_set_drvdata (dev, hcd);
150         hcd->driver = driver;
151         hcd->description = driver->description;
152         hcd->self.bus_name = pci_name(dev);
153 #ifdef CONFIG_PCI_NAMES
154         hcd->product_desc = dev->pretty_name;
155 #else
156         if (hcd->product_desc == NULL)
157                 hcd->product_desc = "USB Host Controller";
158 #endif
159         hcd->self.controller = &dev->dev;
160
161         if ((retval = hcd_buffer_create (hcd)) != 0) {
162 clean_3:
163                 driver->hcd_free (hcd);
164                 goto clean_2;
165         }
166
167         dev_info (hcd->self.controller, "%s\n", hcd->product_desc);
168
169         /* till now HC has been in an indeterminate state ... */
170         if (driver->reset && (retval = driver->reset (hcd)) < 0) {
171                 dev_err (hcd->self.controller, "can't reset\n");
172                 goto clean_3;
173         }
174         hcd->state = USB_STATE_HALT;
175
176         pci_set_master (dev);
177 #ifndef __sparc__
178         sprintf (buf, "%d", dev->irq);
179 #else
180         bufp = __irq_itoa(dev->irq);
181 #endif
182         retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
183                                 hcd->description, hcd);
184         if (retval != 0) {
185                 dev_err (hcd->self.controller,
186                                 "request interrupt %s failed\n", bufp);
187                 goto clean_3;
188         }
189         hcd->irq = dev->irq;
190
191         dev_info (hcd->self.controller, "irq %s, %s %p\n", bufp,
192                 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
193                 base);
194
195         usb_bus_init (&hcd->self);
196         hcd->self.op = &usb_hcd_operations;
197         hcd->self.hcpriv = (void *) hcd;
198         hcd->self.release = &hcd_pci_release;
199         init_timer (&hcd->rh_timer);
200
201         INIT_LIST_HEAD (&hcd->dev_list);
202
203         usb_register_bus (&hcd->self);
204
205         if ((retval = driver->start (hcd)) < 0) {
206                 dev_err (hcd->self.controller, "init error %d\n", retval);
207                 usb_hcd_pci_remove (dev);
208         }
209
210         return retval;
211
212 EXPORT_SYMBOL (usb_hcd_pci_probe);
213
214
215 /* may be called without controller electrically present */
216 /* may be called with controller, bus, and devices active */
217
218 /**
219  * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
220  * @dev: USB Host Controller being removed
221  * Context: !in_interrupt()
222  *
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.
226  *
227  * Store this function in the HCD's struct pci_driver as remove().
228  */
229 void usb_hcd_pci_remove (struct pci_dev *dev)
230 {
231         struct usb_hcd          *hcd;
232
233         hcd = pci_get_drvdata(dev);
234         if (!hcd)
235                 return;
236         dev_info (hcd->self.controller, "remove, state %x\n", hcd->state);
237
238         if (in_interrupt ())
239                 BUG ();
240
241         if (HCD_IS_RUNNING (hcd->state))
242                 hcd->state = USB_STATE_QUIESCING;
243
244         dev_dbg (hcd->self.controller, "roothub graceful disconnect\n");
245         usb_disconnect (&hcd->self.root_hub);
246
247         hcd->driver->stop (hcd);
248         hcd_buffer_destroy (hcd);
249         hcd->state = USB_STATE_HALT;
250         pci_set_drvdata(dev, NULL);
251
252         free_irq (hcd->irq, hcd);
253         if (hcd->driver->flags & HCD_MEMORY) {
254                 iounmap (hcd->regs);
255                 release_mem_region (pci_resource_start (dev, 0),
256                         pci_resource_len (dev, 0));
257         } else {
258                 release_region (pci_resource_start (dev, hcd->region),
259                         pci_resource_len (dev, hcd->region));
260         }
261
262         usb_deregister_bus (&hcd->self);
263 }
264 EXPORT_SYMBOL (usb_hcd_pci_remove);
265
266
267 #ifdef  CONFIG_PM
268
269 /**
270  * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
271  * @dev: USB Host Controller being suspended
272  * @state: state that the controller is going into
273  *
274  * Store this function in the HCD's struct pci_driver as suspend().
275  */
276 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
277 {
278         struct usb_hcd          *hcd;
279         int                     retval = 0;
280         int                     has_pci_pm;
281
282         hcd = pci_get_drvdata(dev);
283
284         /* even when the PCI layer rejects some of the PCI calls
285          * below, HCs can try global suspend and reduce DMA traffic.
286          * PM-sensitive HCDs may already have done this.
287          */
288         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
289         if (has_pci_pm)
290                 dev_dbg(hcd->self.controller, "suspend D%d --> D%d\n",
291                         dev->current_state, state);
292
293         switch (hcd->state) {
294         case USB_STATE_HALT:
295                 dev_dbg (hcd->self.controller, "halted; hcd not suspended\n");
296                 break;
297         case HCD_STATE_SUSPENDED:
298                 dev_dbg (hcd->self.controller, "hcd already suspended\n");
299                 break;
300         default:
301                 retval = hcd->driver->suspend (hcd, state);
302                 if (retval)
303                         dev_dbg (hcd->self.controller, 
304                                         "suspend fail, retval %d\n",
305                                         retval);
306                 else {
307                         hcd->state = HCD_STATE_SUSPENDED;
308                         pci_save_state (dev, hcd->pci_state);
309 #ifdef  CONFIG_USB_SUSPEND
310                         pci_enable_wake (dev, state, hcd->remote_wakeup);
311                         pci_enable_wake (dev, 4, hcd->remote_wakeup);
312 #endif
313                         /* no DMA or IRQs except in D0 */
314                         pci_disable_device (dev);
315                         free_irq (hcd->irq, hcd);
316                         
317                         if (has_pci_pm)
318                                 retval = pci_set_power_state (dev, state);
319                         dev->dev.power.power_state = state;
320                         if (retval < 0) {
321                                 dev_dbg (&dev->dev,
322                                                 "PCI suspend fail, %d\n",
323                                                 retval);
324                                 (void) usb_hcd_pci_resume (dev);
325                         }
326                 }
327         }
328         return retval;
329 }
330 EXPORT_SYMBOL (usb_hcd_pci_suspend);
331
332 /**
333  * usb_hcd_pci_resume - power management resume of a PCI-based HCD
334  * @dev: USB Host Controller being resumed
335  *
336  * Store this function in the HCD's struct pci_driver as resume().
337  */
338 int usb_hcd_pci_resume (struct pci_dev *dev)
339 {
340         struct usb_hcd          *hcd;
341         int                     retval;
342         int                     has_pci_pm;
343
344         hcd = pci_get_drvdata(dev);
345         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
346         if (has_pci_pm)
347                 dev_dbg(hcd->self.controller, "resume from state D%d\n",
348                                 dev->current_state);
349
350         if (hcd->state != HCD_STATE_SUSPENDED) {
351                 dev_dbg (hcd->self.controller, 
352                                 "can't resume, not suspended!\n");
353                 return -EL3HLT;
354         }
355         hcd->state = USB_STATE_RESUMING;
356
357         if (has_pci_pm)
358                 pci_set_power_state (dev, 0);
359         dev->dev.power.power_state = 0;
360         retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
361                                 hcd->description, hcd);
362         if (retval < 0) {
363                 dev_err (hcd->self.controller,
364                         "can't restore IRQ after resume!\n");
365                 return retval;
366         }
367         pci_set_master (dev);
368         pci_restore_state (dev, hcd->pci_state);
369 #ifdef  CONFIG_USB_SUSPEND
370         pci_enable_wake (dev, dev->current_state, 0);
371         pci_enable_wake (dev, 4, 0);
372 #endif
373
374         retval = hcd->driver->resume (hcd);
375         if (!HCD_IS_RUNNING (hcd->state)) {
376                 dev_dbg (hcd->self.controller, 
377                                 "resume fail, retval %d\n", retval);
378                 usb_hc_died (hcd);
379         }
380
381         return retval;
382 }
383 EXPORT_SYMBOL (usb_hcd_pci_resume);
384
385 #endif  /* CONFIG_PM */
386
387