0a273a8fe121e2d822732ed693831bc4730c2806
[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 = driver->hcd_alloc ();
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->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;
152 #else
153         if (hcd->product_desc == NULL)
154                 hcd->product_desc = "USB Host Controller";
155 #endif
156         hcd->self.controller = &dev->dev;
157
158         if ((retval = hcd_buffer_create (hcd)) != 0) {
159 clean_3:
160                 kfree (hcd);
161                 goto clean_2;
162         }
163
164         dev_info (hcd->self.controller, "%s\n", hcd->product_desc);
165
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");
169                 goto clean_3;
170         }
171         hcd->state = USB_STATE_HALT;
172
173         pci_set_master (dev);
174 #ifndef __sparc__
175         sprintf (buf, "%d", dev->irq);
176 #else
177         bufp = __irq_itoa(dev->irq);
178 #endif
179         retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
180                                 hcd->description, hcd);
181         if (retval != 0) {
182                 dev_err (hcd->self.controller,
183                                 "request interrupt %s failed\n", bufp);
184                 goto clean_3;
185         }
186         hcd->irq = dev->irq;
187
188         dev_info (hcd->self.controller, "irq %s, %s 0x%lx\n", bufp,
189                 (driver->flags & HCD_MEMORY) ? "pci mem" : "io base",
190                 resource);
191
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);
197
198         INIT_LIST_HEAD (&hcd->dev_list);
199
200         usb_register_bus (&hcd->self);
201
202         if ((retval = driver->start (hcd)) < 0) {
203                 dev_err (hcd->self.controller, "init error %d\n", retval);
204                 usb_hcd_pci_remove (dev);
205         }
206
207 done:
208         if (retval != 0)
209                 pci_disable_device (dev);
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         pci_disable_device(dev);
265 }
266 EXPORT_SYMBOL (usb_hcd_pci_remove);
267
268
269 #ifdef  CONFIG_PM
270
271 static char __attribute_used__ *pci_state(u32 state)
272 {
273         switch (state) {
274         case 0:         return "D0";
275         case 1:         return "D1";
276         case 2:         return "D2";
277         case 3:         return "D3hot";
278         case 4:         return "D3cold";
279         }
280         return NULL;
281 }
282
283 /**
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
287  *
288  * Store this function in the HCD's struct pci_driver as suspend().
289  */
290 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
291 {
292         struct usb_hcd          *hcd;
293         int                     retval = 0;
294         int                     has_pci_pm;
295
296         hcd = pci_get_drvdata(dev);
297
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.
301          */
302         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
303         if (state > 4)
304                 state = 4;
305
306         switch (hcd->state) {
307
308         /* entry if root hub wasn't yet suspended ... from sysfs,
309          * without autosuspend, or if USB_SUSPEND isn't configured.
310          */
311         case USB_STATE_RUNNING:
312                 hcd->state = USB_STATE_QUIESCING;
313                 retval = hcd->driver->suspend (hcd, state);
314                 if (retval) {
315                         dev_dbg (hcd->self.controller, 
316                                         "suspend fail, retval %d\n",
317                                         retval);
318                         break;
319                 }
320                 hcd->state = HCD_STATE_SUSPENDED;
321                 /* FALLTHROUGH */
322
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.
326          *
327          * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will
328          * have been called, otherwise root hub timers still run ...
329          */
330         case HCD_STATE_SUSPENDED:
331                 if (state <= dev->current_state)
332                         break;
333
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);
339                 }
340
341                 if (!has_pci_pm) {
342                         dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n");
343                         break;
344                 }
345
346                 /* POLICY: ignore D1/D2/D3hot differences;
347                  * we know D3hot will always work.
348                  */
349                 retval = pci_set_power_state (dev, state);
350                 if (retval < 0 && state < 3) {
351                         retval = pci_set_power_state (dev, 3);
352                         if (retval == 0)
353                                 state = 3;
354                 }
355                 if (retval == 0) {
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);
361 #endif
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);
366                         break;
367                 }
368                 break;
369         default:
370                 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n",
371                         hcd->state);
372                 retval = -EINVAL;
373                 break;
374         }
375
376         /* update power_state **ONLY** to make sysfs happier */
377         if (retval == 0)
378                 dev->dev.power.power_state = state;
379         return retval;
380 }
381 EXPORT_SYMBOL (usb_hcd_pci_suspend);
382
383 /**
384  * usb_hcd_pci_resume - power management resume of a PCI-based HCD
385  * @dev: USB Host Controller being resumed
386  *
387  * Store this function in the HCD's struct pci_driver as resume().
388  */
389 int usb_hcd_pci_resume (struct pci_dev *dev)
390 {
391         struct usb_hcd          *hcd;
392         int                     retval;
393         int                     has_pci_pm;
394
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");
399                 return 0;
400         }
401         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
402
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)");
407
408         hcd->state = USB_STATE_RESUMING;
409
410         if (has_pci_pm)
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);
415         if (retval < 0) {
416                 dev_err (hcd->self.controller,
417                         "can't restore IRQ after resume!\n");
418                 return retval;
419         }
420         hcd->saw_irq = 0;
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);
425 #endif
426
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);
431                 usb_hc_died (hcd);
432         }
433
434         return retval;
435 }
436 EXPORT_SYMBOL (usb_hcd_pci_resume);
437
438 #endif  /* CONFIG_PM */
439
440