patch-2_6_7-vs1_9_1_12
[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                    *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 *) 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         struct usb_device       *hub;
233
234         hcd = pci_get_drvdata(dev);
235         if (!hcd)
236                 return;
237         dev_info (hcd->self.controller, "remove, state %x\n", hcd->state);
238
239         if (in_interrupt ())
240                 BUG ();
241
242         hub = hcd->self.root_hub;
243         if (HCD_IS_RUNNING (hcd->state))
244                 hcd->state = USB_STATE_QUIESCING;
245
246         dev_dbg (hcd->self.controller, "roothub graceful disconnect\n");
247         usb_disconnect (&hub);
248
249         hcd->driver->stop (hcd);
250         hcd_buffer_destroy (hcd);
251         hcd->state = USB_STATE_HALT;
252         pci_set_drvdata (dev, 0);
253
254         free_irq (hcd->irq, hcd);
255         if (hcd->driver->flags & HCD_MEMORY) {
256                 iounmap (hcd->regs);
257                 release_mem_region (pci_resource_start (dev, 0),
258                         pci_resource_len (dev, 0));
259         } else {
260                 release_region (pci_resource_start (dev, hcd->region),
261                         pci_resource_len (dev, hcd->region));
262         }
263
264         usb_deregister_bus (&hcd->self);
265 }
266 EXPORT_SYMBOL (usb_hcd_pci_remove);
267
268
269 #ifdef  CONFIG_PM
270
271 /**
272  * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD
273  * @dev: USB Host Controller being suspended
274  * @state: state that the controller is going into
275  *
276  * Store this function in the HCD's struct pci_driver as suspend().
277  */
278 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
279 {
280         struct usb_hcd          *hcd;
281         int                     retval = 0;
282         int                     has_pci_pm;
283
284         hcd = pci_get_drvdata(dev);
285
286         /* even when the PCI layer rejects some of the PCI calls
287          * below, HCs can try global suspend and reduce DMA traffic.
288          * PM-sensitive HCDs may already have done this.
289          */
290         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
291         if (has_pci_pm)
292                 dev_dbg(hcd->self.controller, "suspend D%d --> D%d\n",
293                         dev->current_state, state);
294
295         switch (hcd->state) {
296         case USB_STATE_HALT:
297                 dev_dbg (hcd->self.controller, "halted; hcd not suspended\n");
298                 break;
299         case HCD_STATE_SUSPENDED:
300                 dev_dbg (hcd->self.controller, "hcd already suspended\n");
301                 break;
302         default:
303                 retval = hcd->driver->suspend (hcd, state);
304                 if (retval)
305                         dev_dbg (hcd->self.controller, 
306                                         "suspend fail, retval %d\n",
307                                         retval);
308                 else {
309                         hcd->state = HCD_STATE_SUSPENDED;
310                         pci_save_state (dev, hcd->pci_state);
311 #ifdef  CONFIG_USB_SUSPEND
312                         pci_enable_wake (dev, state, hcd->remote_wakeup);
313                         pci_enable_wake (dev, 4, hcd->remote_wakeup);
314 #endif
315                         /* no DMA or IRQs except in D0 */
316                         pci_disable_device (dev);
317                         free_irq (hcd->irq, hcd);
318                         
319                         if (has_pci_pm)
320                                 retval = pci_set_power_state (dev, state);
321                         dev->dev.power.power_state = state;
322                         if (retval < 0) {
323                                 dev_dbg (&dev->dev,
324                                                 "PCI suspend fail, %d\n",
325                                                 retval);
326                                 (void) usb_hcd_pci_resume (dev);
327                         }
328                 }
329         }
330         return retval;
331 }
332 EXPORT_SYMBOL (usb_hcd_pci_suspend);
333
334 /**
335  * usb_hcd_pci_resume - power management resume of a PCI-based HCD
336  * @dev: USB Host Controller being resumed
337  *
338  * Store this function in the HCD's struct pci_driver as resume().
339  */
340 int usb_hcd_pci_resume (struct pci_dev *dev)
341 {
342         struct usb_hcd          *hcd;
343         int                     retval;
344         int                     has_pci_pm;
345
346         hcd = pci_get_drvdata(dev);
347         has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
348         if (has_pci_pm)
349                 dev_dbg(hcd->self.controller, "resume from state D%d\n",
350                                 dev->current_state);
351
352         if (hcd->state != HCD_STATE_SUSPENDED) {
353                 dev_dbg (hcd->self.controller, 
354                                 "can't resume, not suspended!\n");
355                 return -EL3HLT;
356         }
357         hcd->state = USB_STATE_RESUMING;
358
359         if (has_pci_pm)
360                 pci_set_power_state (dev, 0);
361         dev->dev.power.power_state = 0;
362         retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ,
363                                 hcd->description, hcd);
364         if (retval < 0) {
365                 dev_err (hcd->self.controller,
366                         "can't restore IRQ after resume!\n");
367                 return retval;
368         }
369         pci_set_master (dev);
370         pci_restore_state (dev, hcd->pci_state);
371 #ifdef  CONFIG_USB_SUSPEND
372         pci_enable_wake (dev, dev->current_state, 0);
373         pci_enable_wake (dev, 4, 0);
374 #endif
375
376         retval = hcd->driver->resume (hcd);
377         if (!HCD_IS_RUNNING (hcd->state)) {
378                 dev_dbg (hcd->self.controller, 
379                                 "resume fail, retval %d\n", retval);
380                 usb_hc_died (hcd);
381         }
382
383         return retval;
384 }
385 EXPORT_SYMBOL (usb_hcd_pci_resume);
386
387 #endif  /* CONFIG_PM */
388
389