2 * (C) Copyright David Brownell 2000-2002
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.
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
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.
19 #include <linux/config.h>
21 #ifdef CONFIG_USB_DEBUG
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
32 #include <linux/usb.h>
36 /* PCI-based HCs are normal, but custom bus glue should be ok */
39 /*-------------------------------------------------------------------------*/
41 static void hcd_pci_release(struct usb_bus
*bus
)
43 struct usb_hcd
*hcd
= bus
->hcpriv
;
46 hcd
->driver
->hcd_free(hcd
);
49 /* configure so an HC device and id are always provided */
50 /* always called with process context; sleeping is OK */
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()
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.
62 * Store this function in the HCD's struct pci_driver as probe().
64 int usb_hcd_pci_probe (struct pci_dev
*dev
, const struct pci_device_id
*id
)
66 struct hc_driver
*driver
;
67 unsigned long resource
, len
;
71 char buf
[8], *bufp
= buf
;
76 if (!id
|| !(driver
= (struct hc_driver
*) id
->driver_data
))
79 if (pci_enable_device (dev
) < 0)
84 "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
89 if (driver
->flags
& HCD_MEMORY
) { // EHCI, OHCI
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");
97 base
= ioremap_nocache (resource
, len
);
99 dev_dbg (&dev
->dev
, "error mapping memory\n");
102 release_mem_region (resource
, len
);
103 dev_err (&dev
->dev
, "init %s fail, %d\n",
104 pci_name(dev
), retval
);
110 for (region
= 0; region
< PCI_ROM_RESOURCE
; region
++) {
111 if (!(pci_resource_flags (dev
, region
) & IORESOURCE_IO
))
114 resource
= pci_resource_start (dev
, region
);
115 len
= pci_resource_len (dev
, region
);
116 if (request_region (resource
, len
,
117 driver
->description
))
120 if (region
== PCI_ROM_RESOURCE
) {
121 dev_dbg (&dev
->dev
, "no i/o regions available\n");
124 base
= (void __iomem
*) resource
;
127 // driver->reset(), later on, will transfer device from
128 // control by SMM/BIOS to control by Linux (if needed)
130 hcd
= driver
->hcd_alloc ();
132 dev_dbg (&dev
->dev
, "hcd alloc fail\n");
135 if (driver
->flags
& HCD_MEMORY
) {
139 release_region (resource
, len
);
140 dev_err (&dev
->dev
, "init %s fail, %d\n",
141 pci_name(dev
), retval
);
145 // hcd zeroed everything
147 hcd
->region
= region
;
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
;
156 if (hcd
->product_desc
== NULL
)
157 hcd
->product_desc
= "USB Host Controller";
159 hcd
->self
.controller
= &dev
->dev
;
161 if ((retval
= hcd_buffer_create (hcd
)) != 0) {
163 driver
->hcd_free (hcd
);
167 dev_info (hcd
->self
.controller
, "%s\n", hcd
->product_desc
);
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");
174 hcd
->state
= USB_STATE_HALT
;
176 pci_set_master (dev
);
178 sprintf (buf
, "%d", dev
->irq
);
180 bufp
= __irq_itoa(dev
->irq
);
182 retval
= request_irq (dev
->irq
, usb_hcd_irq
, SA_SHIRQ
,
183 hcd
->description
, hcd
);
185 dev_err (hcd
->self
.controller
,
186 "request interrupt %s failed\n", bufp
);
191 dev_info (hcd
->self
.controller
, "irq %s, %s %p\n", bufp
,
192 (driver
->flags
& HCD_MEMORY
) ? "pci mem" : "io base",
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
);
201 INIT_LIST_HEAD (&hcd
->dev_list
);
203 usb_register_bus (&hcd
->self
);
205 if ((retval
= driver
->start (hcd
)) < 0) {
206 dev_err (hcd
->self
.controller
, "init error %d\n", retval
);
207 usb_hcd_pci_remove (dev
);
212 EXPORT_SYMBOL (usb_hcd_pci_probe
);
215 /* may be called without controller electrically present */
216 /* may be called with controller, bus, and devices active */
219 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
220 * @dev: USB Host Controller being removed
221 * Context: !in_interrupt()
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.
227 * Store this function in the HCD's struct pci_driver as remove().
229 void usb_hcd_pci_remove (struct pci_dev
*dev
)
233 hcd
= pci_get_drvdata(dev
);
236 dev_info (hcd
->self
.controller
, "remove, state %x\n", hcd
->state
);
241 if (HCD_IS_RUNNING (hcd
->state
))
242 hcd
->state
= USB_STATE_QUIESCING
;
244 dev_dbg (hcd
->self
.controller
, "roothub graceful disconnect\n");
245 usb_disconnect (&hcd
->self
.root_hub
);
247 hcd
->driver
->stop (hcd
);
248 hcd_buffer_destroy (hcd
);
249 hcd
->state
= USB_STATE_HALT
;
250 pci_set_drvdata(dev
, NULL
);
252 free_irq (hcd
->irq
, hcd
);
253 if (hcd
->driver
->flags
& HCD_MEMORY
) {
255 release_mem_region (pci_resource_start (dev
, 0),
256 pci_resource_len (dev
, 0));
258 release_region (pci_resource_start (dev
, hcd
->region
),
259 pci_resource_len (dev
, hcd
->region
));
262 usb_deregister_bus (&hcd
->self
);
264 EXPORT_SYMBOL (usb_hcd_pci_remove
);
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
274 * Store this function in the HCD's struct pci_driver as suspend().
276 int usb_hcd_pci_suspend (struct pci_dev
*dev
, u32 state
)
282 hcd
= pci_get_drvdata(dev
);
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.
288 has_pci_pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
290 dev_dbg(hcd
->self
.controller
, "suspend D%d --> D%d\n",
291 dev
->current_state
, state
);
293 switch (hcd
->state
) {
295 dev_dbg (hcd
->self
.controller
, "halted; hcd not suspended\n");
297 case HCD_STATE_SUSPENDED
:
298 dev_dbg (hcd
->self
.controller
, "hcd already suspended\n");
301 retval
= hcd
->driver
->suspend (hcd
, state
);
303 dev_dbg (hcd
->self
.controller
,
304 "suspend fail, retval %d\n",
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
);
313 /* no DMA or IRQs except in D0 */
314 pci_disable_device (dev
);
315 free_irq (hcd
->irq
, hcd
);
318 retval
= pci_set_power_state (dev
, state
);
319 dev
->dev
.power
.power_state
= state
;
322 "PCI suspend fail, %d\n",
324 (void) usb_hcd_pci_resume (dev
);
330 EXPORT_SYMBOL (usb_hcd_pci_suspend
);
333 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
334 * @dev: USB Host Controller being resumed
336 * Store this function in the HCD's struct pci_driver as resume().
338 int usb_hcd_pci_resume (struct pci_dev
*dev
)
344 hcd
= pci_get_drvdata(dev
);
345 has_pci_pm
= pci_find_capability(dev
, PCI_CAP_ID_PM
);
347 dev_dbg(hcd
->self
.controller
, "resume from state D%d\n",
350 if (hcd
->state
!= HCD_STATE_SUSPENDED
) {
351 dev_dbg (hcd
->self
.controller
,
352 "can't resume, not suspended!\n");
355 hcd
->state
= USB_STATE_RESUMING
;
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
);
363 dev_err (hcd
->self
.controller
,
364 "can't restore IRQ after resume!\n");
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);
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
);
383 EXPORT_SYMBOL (usb_hcd_pci_resume
);
385 #endif /* CONFIG_PM */