Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 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 | struct usb_hcd *hcd; |
| 60 | int retval; |
| 61 | |
| 62 | if (usb_disabled()) |
| 63 | return -ENODEV; |
| 64 | |
| 65 | if (!id || !(driver = (struct hc_driver *) id->driver_data)) |
| 66 | return -EINVAL; |
| 67 | |
| 68 | if (pci_enable_device (dev) < 0) |
| 69 | return -ENODEV; |
| 70 | dev->current_state = 0; |
| 71 | dev->dev.power.power_state = 0; |
| 72 | |
| 73 | if (!dev->irq) { |
| 74 | dev_err (&dev->dev, |
| 75 | "Found HC with no IRQ. Check BIOS/PCI %s setup!\n", |
| 76 | pci_name(dev)); |
| 77 | retval = -ENODEV; |
| 78 | goto err1; |
| 79 | } |
| 80 | |
| 81 | hcd = usb_create_hcd (driver, &dev->dev, pci_name(dev)); |
| 82 | if (!hcd) { |
| 83 | retval = -ENOMEM; |
| 84 | goto err1; |
| 85 | } |
| 86 | |
| 87 | if (driver->flags & HCD_MEMORY) { // EHCI, OHCI |
| 88 | hcd->rsrc_start = pci_resource_start (dev, 0); |
| 89 | hcd->rsrc_len = pci_resource_len (dev, 0); |
| 90 | if (!request_mem_region (hcd->rsrc_start, hcd->rsrc_len, |
| 91 | driver->description)) { |
| 92 | dev_dbg (&dev->dev, "controller already in use\n"); |
| 93 | retval = -EBUSY; |
| 94 | goto err2; |
| 95 | } |
| 96 | hcd->regs = ioremap_nocache (hcd->rsrc_start, hcd->rsrc_len); |
| 97 | if (hcd->regs == NULL) { |
| 98 | dev_dbg (&dev->dev, "error mapping memory\n"); |
| 99 | retval = -EFAULT; |
| 100 | goto err3; |
| 101 | } |
| 102 | |
| 103 | } else { // UHCI |
| 104 | int region; |
| 105 | |
| 106 | for (region = 0; region < PCI_ROM_RESOURCE; region++) { |
| 107 | if (!(pci_resource_flags (dev, region) & |
| 108 | IORESOURCE_IO)) |
| 109 | continue; |
| 110 | |
| 111 | hcd->rsrc_start = pci_resource_start (dev, region); |
| 112 | hcd->rsrc_len = pci_resource_len (dev, region); |
| 113 | if (request_region (hcd->rsrc_start, hcd->rsrc_len, |
| 114 | driver->description)) |
| 115 | break; |
| 116 | } |
| 117 | if (region == PCI_ROM_RESOURCE) { |
| 118 | dev_dbg (&dev->dev, "no i/o regions available\n"); |
| 119 | retval = -EBUSY; |
| 120 | goto err1; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #ifdef CONFIG_PCI_NAMES |
| 125 | hcd->product_desc = dev->pretty_name; |
| 126 | #endif |
| 127 | |
| 128 | pci_set_master (dev); |
| 129 | |
| 130 | retval = usb_add_hcd (hcd, dev->irq, SA_SHIRQ); |
| 131 | if (retval != 0) |
| 132 | goto err4; |
| 133 | return retval; |
| 134 | |
| 135 | err4: |
| 136 | if (driver->flags & HCD_MEMORY) { |
| 137 | iounmap (hcd->regs); |
| 138 | err3: |
| 139 | release_mem_region (hcd->rsrc_start, hcd->rsrc_len); |
| 140 | } else |
| 141 | release_region (hcd->rsrc_start, hcd->rsrc_len); |
| 142 | err2: |
| 143 | usb_put_hcd (hcd); |
| 144 | err1: |
| 145 | pci_disable_device (dev); |
| 146 | dev_err (&dev->dev, "init %s fail, %d\n", pci_name(dev), retval); |
| 147 | return retval; |
| 148 | } |
| 149 | EXPORT_SYMBOL (usb_hcd_pci_probe); |
| 150 | |
| 151 | |
| 152 | /* may be called without controller electrically present */ |
| 153 | /* may be called with controller, bus, and devices active */ |
| 154 | |
| 155 | /** |
| 156 | * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs |
| 157 | * @dev: USB Host Controller being removed |
| 158 | * Context: !in_interrupt() |
| 159 | * |
| 160 | * Reverses the effect of usb_hcd_pci_probe(), first invoking |
| 161 | * the HCD's stop() method. It is always called from a thread |
| 162 | * context, normally "rmmod", "apmd", or something similar. |
| 163 | * |
| 164 | * Store this function in the HCD's struct pci_driver as remove(). |
| 165 | */ |
| 166 | void usb_hcd_pci_remove (struct pci_dev *dev) |
| 167 | { |
| 168 | struct usb_hcd *hcd; |
| 169 | |
| 170 | hcd = pci_get_drvdata(dev); |
| 171 | if (!hcd) |
| 172 | return; |
| 173 | |
| 174 | usb_remove_hcd (hcd); |
| 175 | if (hcd->driver->flags & HCD_MEMORY) { |
| 176 | iounmap (hcd->regs); |
| 177 | release_mem_region (hcd->rsrc_start, hcd->rsrc_len); |
| 178 | } else { |
| 179 | release_region (hcd->rsrc_start, hcd->rsrc_len); |
| 180 | } |
| 181 | usb_put_hcd (hcd); |
| 182 | pci_disable_device(dev); |
| 183 | } |
| 184 | EXPORT_SYMBOL (usb_hcd_pci_remove); |
| 185 | |
| 186 | |
| 187 | #ifdef CONFIG_PM |
| 188 | |
| 189 | static char __attribute_used__ *pci_state(u32 state) |
| 190 | { |
| 191 | switch (state) { |
| 192 | case 0: return "D0"; |
| 193 | case 1: return "D1"; |
| 194 | case 2: return "D2"; |
| 195 | case 3: return "D3hot"; |
| 196 | case 4: return "D3cold"; |
| 197 | } |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD |
| 203 | * @dev: USB Host Controller being suspended |
| 204 | * @state: state that the controller is going into |
| 205 | * |
| 206 | * Store this function in the HCD's struct pci_driver as suspend(). |
| 207 | */ |
| 208 | int usb_hcd_pci_suspend (struct pci_dev *dev, u32 state) |
| 209 | { |
| 210 | struct usb_hcd *hcd; |
| 211 | int retval = 0; |
| 212 | int has_pci_pm; |
| 213 | |
| 214 | hcd = pci_get_drvdata(dev); |
| 215 | |
| 216 | /* even when the PCI layer rejects some of the PCI calls |
| 217 | * below, HCs can try global suspend and reduce DMA traffic. |
| 218 | * PM-sensitive HCDs may already have done this. |
| 219 | */ |
| 220 | has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); |
| 221 | if (state > 4) |
| 222 | state = 4; |
| 223 | |
| 224 | switch (hcd->state) { |
| 225 | |
| 226 | /* entry if root hub wasn't yet suspended ... from sysfs, |
| 227 | * without autosuspend, or if USB_SUSPEND isn't configured. |
| 228 | */ |
| 229 | case HC_STATE_RUNNING: |
| 230 | hcd->state = HC_STATE_QUIESCING; |
| 231 | retval = hcd->driver->suspend (hcd, state); |
| 232 | if (retval) { |
| 233 | dev_dbg (hcd->self.controller, |
| 234 | "suspend fail, retval %d\n", |
| 235 | retval); |
| 236 | break; |
| 237 | } |
| 238 | hcd->state = HC_STATE_SUSPENDED; |
| 239 | /* FALLTHROUGH */ |
| 240 | |
| 241 | /* entry with CONFIG_USB_SUSPEND, or hcds that autosuspend: the |
| 242 | * controller and/or root hub will already have been suspended, |
| 243 | * but it won't be ready for a PCI resume call. |
| 244 | * |
| 245 | * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will |
| 246 | * have been called, otherwise root hub timers still run ... |
| 247 | */ |
| 248 | case HC_STATE_SUSPENDED: |
| 249 | if (state <= dev->current_state) |
| 250 | break; |
| 251 | |
| 252 | /* no DMA or IRQs except in D0 */ |
| 253 | if (!dev->current_state) { |
| 254 | pci_save_state (dev); |
| 255 | pci_disable_device (dev); |
| 256 | free_irq (hcd->irq, hcd); |
| 257 | } |
| 258 | |
| 259 | if (!has_pci_pm) { |
| 260 | dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n"); |
| 261 | break; |
| 262 | } |
| 263 | |
| 264 | /* POLICY: ignore D1/D2/D3hot differences; |
| 265 | * we know D3hot will always work. |
| 266 | */ |
| 267 | retval = pci_set_power_state (dev, state); |
| 268 | if (retval < 0 && state < 3) { |
| 269 | retval = pci_set_power_state (dev, 3); |
| 270 | if (retval == 0) |
| 271 | state = 3; |
| 272 | } |
| 273 | if (retval == 0) { |
| 274 | dev_dbg (hcd->self.controller, "--> PCI %s\n", |
| 275 | pci_state(dev->current_state)); |
| 276 | #ifdef CONFIG_USB_SUSPEND |
| 277 | pci_enable_wake (dev, state, hcd->remote_wakeup); |
| 278 | pci_enable_wake (dev, 4, hcd->remote_wakeup); |
| 279 | #endif |
| 280 | } else if (retval < 0) { |
| 281 | dev_dbg (&dev->dev, "PCI %s suspend fail, %d\n", |
| 282 | pci_state(state), retval); |
| 283 | (void) usb_hcd_pci_resume (dev); |
| 284 | break; |
| 285 | } |
| 286 | break; |
| 287 | default: |
| 288 | dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n", |
| 289 | hcd->state); |
| 290 | retval = -EINVAL; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | /* update power_state **ONLY** to make sysfs happier */ |
| 295 | if (retval == 0) |
| 296 | dev->dev.power.power_state = state; |
| 297 | return retval; |
| 298 | } |
| 299 | EXPORT_SYMBOL (usb_hcd_pci_suspend); |
| 300 | |
| 301 | /** |
| 302 | * usb_hcd_pci_resume - power management resume of a PCI-based HCD |
| 303 | * @dev: USB Host Controller being resumed |
| 304 | * |
| 305 | * Store this function in the HCD's struct pci_driver as resume(). |
| 306 | */ |
| 307 | int usb_hcd_pci_resume (struct pci_dev *dev) |
| 308 | { |
| 309 | struct usb_hcd *hcd; |
| 310 | int retval; |
| 311 | int has_pci_pm; |
| 312 | |
| 313 | hcd = pci_get_drvdata(dev); |
| 314 | if (hcd->state != HC_STATE_SUSPENDED) { |
| 315 | dev_dbg (hcd->self.controller, |
| 316 | "can't resume, not suspended!\n"); |
| 317 | return 0; |
| 318 | } |
| 319 | has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); |
| 320 | |
| 321 | /* D3cold resume isn't usually reported this way... */ |
| 322 | dev_dbg(hcd->self.controller, "resume from PCI %s%s\n", |
| 323 | pci_state(dev->current_state), |
| 324 | has_pci_pm ? "" : " (legacy)"); |
| 325 | |
| 326 | hcd->state = HC_STATE_RESUMING; |
| 327 | |
| 328 | if (has_pci_pm) |
| 329 | pci_set_power_state (dev, 0); |
| 330 | dev->dev.power.power_state = 0; |
| 331 | retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ, |
| 332 | hcd->driver->description, hcd); |
| 333 | if (retval < 0) { |
| 334 | dev_err (hcd->self.controller, |
| 335 | "can't restore IRQ after resume!\n"); |
| 336 | return retval; |
| 337 | } |
| 338 | hcd->saw_irq = 0; |
| 339 | pci_restore_state (dev); |
| 340 | #ifdef CONFIG_USB_SUSPEND |
| 341 | pci_enable_wake (dev, dev->current_state, 0); |
| 342 | pci_enable_wake (dev, 4, 0); |
| 343 | #endif |
| 344 | |
| 345 | retval = hcd->driver->resume (hcd); |
| 346 | if (!HC_IS_RUNNING (hcd->state)) { |
| 347 | dev_dbg (hcd->self.controller, |
| 348 | "resume fail, retval %d\n", retval); |
| 349 | usb_hc_died (hcd); |
| 350 | } |
| 351 | |
| 352 | return retval; |
| 353 | } |
| 354 | EXPORT_SYMBOL (usb_hcd_pci_resume); |
| 355 | |
| 356 | #endif /* CONFIG_PM */ |
| 357 | |
| 358 | |