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 *) 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)
232 struct usb_device *hub;
234 hcd = pci_get_drvdata(dev);
237 dev_info (hcd->self.controller, "remove, state %x\n", hcd->state);
242 hub = hcd->self.root_hub;
243 if (HCD_IS_RUNNING (hcd->state))
244 hcd->state = USB_STATE_QUIESCING;
246 dev_dbg (hcd->self.controller, "roothub graceful disconnect\n");
247 usb_disconnect (&hub);
249 hcd->driver->stop (hcd);
250 hcd_buffer_destroy (hcd);
251 hcd->state = USB_STATE_HALT;
252 pci_set_drvdata (dev, 0);
254 free_irq (hcd->irq, hcd);
255 if (hcd->driver->flags & HCD_MEMORY) {
257 release_mem_region (pci_resource_start (dev, 0),
258 pci_resource_len (dev, 0));
260 release_region (pci_resource_start (dev, hcd->region),
261 pci_resource_len (dev, hcd->region));
264 usb_deregister_bus (&hcd->self);
266 EXPORT_SYMBOL (usb_hcd_pci_remove);
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
276 * Store this function in the HCD's struct pci_driver as suspend().
278 int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state)
284 hcd = pci_get_drvdata(dev);
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.
290 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
292 dev_dbg(hcd->self.controller, "suspend D%d --> D%d\n",
293 dev->current_state, state);
295 switch (hcd->state) {
297 dev_dbg (hcd->self.controller, "halted; hcd not suspended\n");
299 case HCD_STATE_SUSPENDED:
300 dev_dbg (hcd->self.controller, "hcd already suspended\n");
303 retval = hcd->driver->suspend (hcd, state);
305 dev_dbg (hcd->self.controller,
306 "suspend fail, retval %d\n",
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);
315 /* no DMA or IRQs except in D0 */
316 pci_disable_device (dev);
317 free_irq (hcd->irq, hcd);
320 retval = pci_set_power_state (dev, state);
321 dev->dev.power.power_state = state;
324 "PCI suspend fail, %d\n",
326 (void) usb_hcd_pci_resume (dev);
332 EXPORT_SYMBOL (usb_hcd_pci_suspend);
335 * usb_hcd_pci_resume - power management resume of a PCI-based HCD
336 * @dev: USB Host Controller being resumed
338 * Store this function in the HCD's struct pci_driver as resume().
340 int usb_hcd_pci_resume (struct pci_dev *dev)
346 hcd = pci_get_drvdata(dev);
347 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM);
349 dev_dbg(hcd->self.controller, "resume from state D%d\n",
352 if (hcd->state != HCD_STATE_SUSPENDED) {
353 dev_dbg (hcd->self.controller,
354 "can't resume, not suspended!\n");
357 hcd->state = USB_STATE_RESUMING;
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);
365 dev_err (hcd->self.controller,
366 "can't restore IRQ after resume!\n");
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);
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);
385 EXPORT_SYMBOL (usb_hcd_pci_resume);
387 #endif /* CONFIG_PM */