0054c7cf7a35e9f9e9f18259f8525b93291c0f51
[linux-flexiantxendom0-3.2.10.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                    *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                 err ("Found HC with no IRQ.  Check BIOS/PCI %s setup!",
84                         pci_name(dev));
85                 return -ENODEV;
86         }
87         
88         if (driver->flags & HCD_MEMORY) {       // EHCI, OHCI
89                 region = 0;
90                 resource = pci_resource_start (dev, 0);
91                 len = pci_resource_len (dev, 0);
92                 if (!request_mem_region (resource, len, driver->description)) {
93                         dbg ("controller already in use");
94                         return -EBUSY;
95                 }
96                 base = ioremap_nocache (resource, len);
97                 if (base == NULL) {
98                         dbg ("error mapping memory");
99                         retval = -EFAULT;
100 clean_1:
101                         release_mem_region (resource, len);
102                         err ("init %s fail, %d", pci_name(dev), retval);
103                         return retval;
104                 }
105
106         } else {                                // UHCI
107                 resource = len = 0;
108                 for (region = 0; region < PCI_ROM_RESOURCE; region++) {
109                         if (!(pci_resource_flags (dev, region) & IORESOURCE_IO))
110                                 continue;
111
112                         resource = pci_resource_start (dev, region);
113                         len = pci_resource_len (dev, region);
114                         if (request_region (resource, len,
115                                         driver->description))
116                                 break;
117                 }
118                 if (region == PCI_ROM_RESOURCE) {
119                         dbg ("no i/o regions available");
120                         return -EBUSY;
121                 }
122                 base = (void *) resource;
123         }
124
125         // driver->start(), later on, will transfer device from
126         // control by SMM/BIOS to control by Linux (if needed)
127
128         pci_set_master (dev);
129         hcd = driver->hcd_alloc ();
130         if (hcd == NULL){
131                 dbg ("hcd alloc fail");
132                 retval = -ENOMEM;
133 clean_2:
134                 if (driver->flags & HCD_MEMORY) {
135                         iounmap (base);
136                         goto clean_1;
137                 } else {
138                         release_region (resource, len);
139                         err ("init %s fail, %d", pci_name(dev), retval);
140                         return retval;
141                 }
142         }
143         pci_set_drvdata (dev, hcd);
144         hcd->driver = driver;
145         hcd->description = driver->description;
146         hcd->pdev = dev;
147         hcd->self.bus_name = pci_name(dev);
148         hcd->product_desc = dev->dev.name;
149         hcd->self.controller = &dev->dev;
150         hcd->controller = hcd->self.controller;
151
152         if ((retval = hcd_buffer_create (hcd)) != 0) {
153 clean_3:
154                 driver->hcd_free (hcd);
155                 goto clean_2;
156         }
157
158         dev_info (hcd->controller, "%s\n", hcd->product_desc);
159
160 #ifndef __sparc__
161         sprintf (buf, "%d", dev->irq);
162 #else
163         bufp = __irq_itoa(dev->irq);
164 #endif
165         if (request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ, hcd->description, hcd)
166                         != 0) {
167                 dev_err (hcd->controller,
168                                 "request interrupt %s failed\n", bufp);
169                 retval = -EBUSY;
170                 goto clean_3;
171         }
172         hcd->irq = dev->irq;
173
174         hcd->regs = base;
175         hcd->region = region;
176         dev_info (hcd->controller, "irq %s, %s %p\n", bufp,
177                 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
178                 base);
179
180         usb_bus_init (&hcd->self);
181         hcd->self.op = &usb_hcd_operations;
182         hcd->self.hcpriv = (void *) hcd;
183         hcd->self.release = &hcd_pci_release;
184
185         INIT_LIST_HEAD (&hcd->dev_list);
186
187         usb_register_bus (&hcd->self);
188
189         if ((retval = driver->start (hcd)) < 0)
190                 usb_hcd_pci_remove (dev);
191
192         return retval;
193
194 EXPORT_SYMBOL (usb_hcd_pci_probe);
195
196
197 /* may be called without controller electrically present */
198 /* may be called with controller, bus, and devices active */
199
200 /**
201  * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
202  * @dev: USB Host Controller being removed
203  * Context: !in_interrupt()
204  *
205  * Reverses the effect of usb_hcd_pci_probe(), first invoking
206  * the HCD's stop() method.  It is always called from a thread
207  * context, normally "rmmod", "apmd", or something similar.
208  *
209  * Store this function in the HCD's struct pci_driver as remove().
210  */
211 void usb_hcd_pci_remove (struct pci_dev *dev)
212 {
213         struct usb_hcd          *hcd;
214         struct usb_device       *hub;
215
216         hcd = pci_get_drvdata(dev);
217         if (!hcd)
218                 return;
219         dev_info (hcd->controller, "remove, state %x\n", hcd->state);
220
221         if (in_interrupt ())
222                 BUG ();
223
224         hub = hcd->self.root_hub;
225         hcd->state = USB_STATE_QUIESCING;
226
227         dev_dbg (hcd->controller, "roothub graceful disconnect\n");
228         usb_disconnect (&hub);
229
230         hcd->driver->stop (hcd);
231         hcd_buffer_destroy (hcd);
232         hcd->state = USB_STATE_HALT;
233         pci_set_drvdata (dev, 0);
234
235         free_irq (hcd->irq, hcd);
236         if (hcd->driver->flags & HCD_MEMORY) {
237                 iounmap (hcd->regs);
238                 release_mem_region (pci_resource_start (dev, 0),
239                         pci_resource_len (dev, 0));
240         } else {
241                 release_region (pci_resource_start (dev, hcd->region),
242                         pci_resource_len (dev, hcd->region));
243         }
244
245         usb_deregister_bus (&hcd->self);
246 }
247 EXPORT_SYMBOL (usb_hcd_pci_remove);
248
249
250 #ifdef  CONFIG_PM
251
252 /*
253  * Some "sleep" power levels imply updating struct usb_driver
254  * to include a callback asking hcds to do their bit by checking
255  * if all the drivers can suspend.  Gets involved with remote wakeup.
256  *
257  * If there are pending urbs, then HCs will need to access memory,
258  * causing extra power drain.  New sleep()/wakeup() PM calls might
259  * be needed, beyond PCI suspend()/resume().  The root hub timer
260  * still be accessing memory though ...
261  *
262  * FIXME:  USB should have some power budgeting support working with
263  * all kinds of hubs.
264  *
265  * FIXME:  This assumes only D0->D3 suspend and D3->D0 resume.
266  * D1 and D2 states should do something, yes?
267  *
268  * FIXME:  Should provide generic enable_wake(), calling pci_enable_wake()
269  * for all supported states, so that USB remote wakeup can work for any
270  * devices that support it (and are connected via powered hubs).
271  *
272  * FIXME:  resume doesn't seem to work right any more...
273  */
274
275
276 // 2.4 kernels have issued concurrent resumes (w/APM)
277 // we defend against that error; PCI doesn't yet.
278
279 /**
280  * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
281  * @dev: USB Host Controller being suspended
282  * @state: state that the controller is going into
283  *
284  * Store this function in the HCD's struct pci_driver as suspend().
285  */
286 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
287 {
288         struct usb_hcd          *hcd;
289         int                     retval;
290
291         hcd = pci_get_drvdata(dev);
292         dev_info (hcd->controller, "suspend to state %d\n", state);
293
294         pci_save_state (dev, hcd->pci_state);
295
296         // FIXME for all connected devices, leaf-to-root:
297         // driver->suspend()
298         // proposed "new 2.5 driver model" will automate that
299
300         /* driver may want to disable DMA etc */
301         retval = hcd->driver->suspend (hcd, state);
302         hcd->state = USB_STATE_SUSPENDED;
303
304         pci_set_power_state (dev, state);
305         return retval;
306 }
307 EXPORT_SYMBOL (usb_hcd_pci_suspend);
308
309 /**
310  * usb_hcd_pci_resume - power management resume of a PCI-based HCD
311  * @dev: USB Host Controller being resumed
312  *
313  * Store this function in the HCD's struct pci_driver as resume().
314  */
315 int usb_hcd_pci_resume (struct pci_dev *dev)
316 {
317         struct usb_hcd          *hcd;
318         int                     retval;
319
320         hcd = pci_get_drvdata(dev);
321         dev_info (hcd->controller, "resume\n");
322
323         /* guard against multiple resumes (APM bug?) */
324         atomic_inc (&hcd->resume_count);
325         if (atomic_read (&hcd->resume_count) != 1) {
326                 dev_err (hcd->controller, "concurrent PCI resumes\n");
327                 retval = 0;
328                 goto done;
329         }
330
331         retval = -EBUSY;
332         if (hcd->state != USB_STATE_SUSPENDED) {
333                 dev_dbg (hcd->controller, "can't resume, not suspended!\n");
334                 goto done;
335         }
336         hcd->state = USB_STATE_RESUMING;
337
338         pci_set_power_state (dev, 0);
339         pci_restore_state (dev, hcd->pci_state);
340
341         retval = hcd->driver->resume (hcd);
342         if (!HCD_IS_RUNNING (hcd->state)) {
343                 dev_dbg (hcd->controller, "resume fail, retval %d\n", retval);
344                 usb_hc_died (hcd);
345 // FIXME:  recover, reset etc.
346         } else {
347                 // FIXME for all connected devices, root-to-leaf:
348                 // driver->resume ();
349                 // proposed "new 2.5 driver model" will automate that
350         }
351
352 done:
353         atomic_dec (&hcd->resume_count);
354         return retval;
355 }
356 EXPORT_SYMBOL (usb_hcd_pci_resume);
357
358 #endif  /* CONFIG_PM */
359
360