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 common, but plenty of non-PCI HCs are used too */ 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 = PCI_D0; 71 dev->dev.power.power_state = PMSG_ON; 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 pci_set_master (dev); 125 126 retval = usb_add_hcd (hcd, dev->irq, SA_SHIRQ); 127 if (retval != 0) 128 goto err4; 129 return retval; 130 131 err4: 132 if (driver->flags & HCD_MEMORY) { 133 iounmap (hcd->regs); 134 err3: 135 release_mem_region (hcd->rsrc_start, hcd->rsrc_len); 136 } else 137 release_region (hcd->rsrc_start, hcd->rsrc_len); 138 err2: 139 usb_put_hcd (hcd); 140 err1: 141 pci_disable_device (dev); 142 dev_err (&dev->dev, "init %s fail, %d\n", pci_name(dev), retval); 143 return retval; 144 } 145 EXPORT_SYMBOL (usb_hcd_pci_probe); 146 147 148 /* may be called without controller electrically present */ 149 /* may be called with controller, bus, and devices active */ 150 151 /** 152 * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs 153 * @dev: USB Host Controller being removed 154 * Context: !in_interrupt() 155 * 156 * Reverses the effect of usb_hcd_pci_probe(), first invoking 157 * the HCD's stop() method. It is always called from a thread 158 * context, normally "rmmod", "apmd", or something similar. 159 * 160 * Store this function in the HCD's struct pci_driver as remove(). 161 */ 162 void usb_hcd_pci_remove (struct pci_dev *dev) 163 { 164 struct usb_hcd *hcd; 165 166 hcd = pci_get_drvdata(dev); 167 if (!hcd) 168 return; 169 170 usb_remove_hcd (hcd); 171 if (hcd->driver->flags & HCD_MEMORY) { 172 iounmap (hcd->regs); 173 release_mem_region (hcd->rsrc_start, hcd->rsrc_len); 174 } else { 175 release_region (hcd->rsrc_start, hcd->rsrc_len); 176 } 177 usb_put_hcd (hcd); 178 pci_disable_device(dev); 179 } 180 EXPORT_SYMBOL (usb_hcd_pci_remove); 181 182 183 #ifdef CONFIG_PM 184 185 /** 186 * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD 187 * @dev: USB Host Controller being suspended 188 * @message: semantics in flux 189 * 190 * Store this function in the HCD's struct pci_driver as suspend(). 191 */ 192 int usb_hcd_pci_suspend (struct pci_dev *dev, pm_message_t message) 193 { 194 struct usb_hcd *hcd; 195 int retval = 0; 196 int has_pci_pm; 197 198 hcd = pci_get_drvdata(dev); 199 200 /* FIXME until the generic PM interfaces change a lot more, this 201 * can't use PCI D1 and D2 states. For example, the confusion 202 * between messages and states will need to vanish, and messages 203 * will need to provide a target system state again. 204 * 205 * It'll be important to learn characteristics of the target state, 206 * especially on embedded hardware where the HCD will often be in 207 * charge of an external VBUS power supply and one or more clocks. 208 * Some target system states will leave them active; others won't. 209 * (With PCI, that's often handled by platform BIOS code.) 210 */ 211 212 /* even when the PCI layer rejects some of the PCI calls 213 * below, HCs can try global suspend and reduce DMA traffic. 214 * PM-sensitive HCDs may already have done this. 215 */ 216 has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); 217 218 switch (hcd->state) { 219 220 /* entry if root hub wasn't yet suspended ... from sysfs, 221 * without autosuspend, or if USB_SUSPEND isn't configured. 222 */ 223 case HC_STATE_RUNNING: 224 hcd->state = HC_STATE_QUIESCING; 225 retval = hcd->driver->suspend (hcd, message); 226 if (retval) { 227 dev_dbg (hcd->self.controller, 228 "suspend fail, retval %d\n", 229 retval); 230 break; 231 } 232 hcd->state = HC_STATE_SUSPENDED; 233 /* FALLTHROUGH */ 234 235 /* entry with CONFIG_USB_SUSPEND, or hcds that autosuspend: the 236 * controller and/or root hub will already have been suspended, 237 * but it won't be ready for a PCI resume call. 238 * 239 * FIXME only CONFIG_USB_SUSPEND guarantees hub_suspend() will 240 * have been called, otherwise root hub timers still run ... 241 */ 242 case HC_STATE_SUSPENDED: 243 /* no DMA or IRQs except when HC is active */ 244 if (dev->current_state == PCI_D0) { 245 free_irq (hcd->irq, hcd); 246 pci_save_state (dev); 247 pci_disable_device (dev); 248 } 249 250 if (!has_pci_pm) { 251 dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n"); 252 break; 253 } 254 255 /* NOTE: dev->current_state becomes nonzero only here, and 256 * only for devices that support PCI PM. Also, exiting 257 * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset 258 * some device state (e.g. as part of clock reinit). 259 */ 260 retval = pci_set_power_state (dev, PCI_D3hot); 261 if (retval == 0) { 262 dev_dbg (hcd->self.controller, "--> PCI D3\n"); 263 retval = pci_enable_wake (dev, PCI_D3hot, hcd->remote_wakeup); 264 if (retval) 265 break; 266 retval = pci_enable_wake (dev, PCI_D3cold, hcd->remote_wakeup); 267 } else if (retval < 0) { 268 dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n", 269 retval); 270 (void) usb_hcd_pci_resume (dev); 271 break; 272 } 273 break; 274 default: 275 dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n", 276 hcd->state); 277 WARN_ON(1); 278 retval = -EINVAL; 279 break; 280 } 281 282 /* update power_state **ONLY** to make sysfs happier */ 283 if (retval == 0) 284 dev->dev.power.power_state = message; 285 return retval; 286 } 287 EXPORT_SYMBOL (usb_hcd_pci_suspend); 288 289 /** 290 * usb_hcd_pci_resume - power management resume of a PCI-based HCD 291 * @dev: USB Host Controller being resumed 292 * 293 * Store this function in the HCD's struct pci_driver as resume(). 294 */ 295 int usb_hcd_pci_resume (struct pci_dev *dev) 296 { 297 struct usb_hcd *hcd; 298 int retval; 299 300 hcd = pci_get_drvdata(dev); 301 if (hcd->state != HC_STATE_SUSPENDED) { 302 dev_dbg (hcd->self.controller, 303 "can't resume, not suspended!\n"); 304 return 0; 305 } 306 307 /* NOTE: chip docs cover clean "real suspend" cases (what Linux 308 * calls "standby", "suspend to RAM", and so on). There are also 309 * dirty cases when swsusp fakes a suspend in "shutdown" mode. 310 */ 311 if (dev->current_state != PCI_D0) { 312 #ifdef DEBUG 313 int pci_pm; 314 u16 pmcr; 315 316 pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); 317 pci_read_config_word(dev, pci_pm + PCI_PM_CTRL, &pmcr); 318 pmcr &= PCI_PM_CTRL_STATE_MASK; 319 if (pmcr) { 320 /* Clean case: power to USB and to HC registers was 321 * maintained; remote wakeup is easy. 322 */ 323 dev_dbg(hcd->self.controller, "resume from PCI D%d\n", 324 pmcr); 325 } else { 326 /* Clean: HC lost Vcc power, D0 uninitialized 327 * + Vaux may have preserved port and transceiver 328 * state ... for remote wakeup from D3cold 329 * + or not; HCD must reinit + re-enumerate 330 * 331 * Dirty: D0 semi-initialized cases with swsusp 332 * + after BIOS init 333 * + after Linux init (HCD statically linked) 334 */ 335 dev_dbg(hcd->self.controller, 336 "PCI D0, from previous PCI D%d\n", 337 dev->current_state); 338 } 339 #endif 340 retval = pci_enable_wake (dev, dev->current_state, 0); 341 if (retval) { 342 dev_err(hcd->self.controller, 343 "can't enable_wake to %d, %d!\n", 344 dev->current_state, retval); 345 return retval; 346 } 347 retval = pci_enable_wake (dev, PCI_D3cold, 0); 348 if (retval) { 349 dev_err(hcd->self.controller, 350 "can't enable_wake to %d, %d!\n", 351 PCI_D3cold, retval); 352 return retval; 353 } 354 } else { 355 /* Same basic cases: clean (powered/not), dirty */ 356 dev_dbg(hcd->self.controller, "PCI legacy resume\n"); 357 } 358 359 /* NOTE: the PCI API itself is asymmetric here. We don't need to 360 * pci_set_power_state(PCI_D0) since that's part of re-enabling; 361 * but that won't re-enable bus mastering. Yet pci_disable_device() 362 * explicitly disables bus mastering... 363 */ 364 retval = pci_enable_device (dev); 365 if (retval < 0) { 366 dev_err (hcd->self.controller, 367 "can't re-enable after resume, %d!\n", retval); 368 return retval; 369 } 370 pci_set_master (dev); 371 pci_restore_state (dev); 372 373 dev->dev.power.power_state = PMSG_ON; 374 375 hcd->state = HC_STATE_RESUMING; 376 hcd->saw_irq = 0; 377 retval = request_irq (dev->irq, usb_hcd_irq, SA_SHIRQ, 378 hcd->irq_descr, hcd); 379 if (retval < 0) { 380 dev_err (hcd->self.controller, 381 "can't restore IRQ after resume!\n"); 382 usb_hc_died (hcd); 383 return retval; 384 } 385 386 retval = hcd->driver->resume (hcd); 387 if (!HC_IS_RUNNING (hcd->state)) { 388 dev_dbg (hcd->self.controller, 389 "resume fail, retval %d\n", retval); 390 usb_hc_died (hcd); 391 } 392 393 retval = pci_enable_device(dev); 394 return retval; 395 } 396 EXPORT_SYMBOL (usb_hcd_pci_resume); 397 398 #endif /* CONFIG_PM */ 399 400 401