Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
1da177e4 LT |
20 | #include <linux/kernel.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/pci.h> | |
23 | #include <asm/io.h> | |
24 | #include <asm/irq.h> | |
25 | #include <linux/usb.h> | |
5f827ea3 DB |
26 | |
27 | #include "usb.h" | |
1da177e4 LT |
28 | #include "hcd.h" |
29 | ||
30 | ||
c6053ecf | 31 | /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */ |
1da177e4 LT |
32 | |
33 | ||
34 | /*-------------------------------------------------------------------------*/ | |
35 | ||
36 | /* configure so an HC device and id are always provided */ | |
37 | /* always called with process context; sleeping is OK */ | |
38 | ||
39 | /** | |
40 | * usb_hcd_pci_probe - initialize PCI-based HCDs | |
41 | * @dev: USB Host Controller being probed | |
42 | * @id: pci hotplug id connecting controller to HCD framework | |
43 | * Context: !in_interrupt() | |
44 | * | |
45 | * Allocates basic PCI resources for this USB host controller, and | |
46 | * then invokes the start() method for the HCD associated with it | |
47 | * through the hotplug entry's driver_data. | |
48 | * | |
49 | * Store this function in the HCD's struct pci_driver as probe(). | |
50 | */ | |
51 | int usb_hcd_pci_probe (struct pci_dev *dev, const struct pci_device_id *id) | |
52 | { | |
53 | struct hc_driver *driver; | |
54 | struct usb_hcd *hcd; | |
55 | int retval; | |
56 | ||
57 | if (usb_disabled()) | |
58 | return -ENODEV; | |
59 | ||
60 | if (!id || !(driver = (struct hc_driver *) id->driver_data)) | |
61 | return -EINVAL; | |
62 | ||
63 | if (pci_enable_device (dev) < 0) | |
64 | return -ENODEV; | |
c6053ecf DB |
65 | dev->current_state = PCI_D0; |
66 | dev->dev.power.power_state = PMSG_ON; | |
1da177e4 LT |
67 | |
68 | if (!dev->irq) { | |
69 | dev_err (&dev->dev, | |
70 | "Found HC with no IRQ. Check BIOS/PCI %s setup!\n", | |
71 | pci_name(dev)); | |
72 | retval = -ENODEV; | |
73 | goto err1; | |
74 | } | |
75 | ||
76 | hcd = usb_create_hcd (driver, &dev->dev, pci_name(dev)); | |
77 | if (!hcd) { | |
78 | retval = -ENOMEM; | |
79 | goto err1; | |
80 | } | |
81 | ||
82 | if (driver->flags & HCD_MEMORY) { // EHCI, OHCI | |
83 | hcd->rsrc_start = pci_resource_start (dev, 0); | |
84 | hcd->rsrc_len = pci_resource_len (dev, 0); | |
85 | if (!request_mem_region (hcd->rsrc_start, hcd->rsrc_len, | |
86 | driver->description)) { | |
87 | dev_dbg (&dev->dev, "controller already in use\n"); | |
88 | retval = -EBUSY; | |
89 | goto err2; | |
90 | } | |
91 | hcd->regs = ioremap_nocache (hcd->rsrc_start, hcd->rsrc_len); | |
92 | if (hcd->regs == NULL) { | |
93 | dev_dbg (&dev->dev, "error mapping memory\n"); | |
94 | retval = -EFAULT; | |
95 | goto err3; | |
96 | } | |
97 | ||
98 | } else { // UHCI | |
99 | int region; | |
100 | ||
101 | for (region = 0; region < PCI_ROM_RESOURCE; region++) { | |
102 | if (!(pci_resource_flags (dev, region) & | |
103 | IORESOURCE_IO)) | |
104 | continue; | |
105 | ||
106 | hcd->rsrc_start = pci_resource_start (dev, region); | |
107 | hcd->rsrc_len = pci_resource_len (dev, region); | |
108 | if (request_region (hcd->rsrc_start, hcd->rsrc_len, | |
109 | driver->description)) | |
110 | break; | |
111 | } | |
112 | if (region == PCI_ROM_RESOURCE) { | |
113 | dev_dbg (&dev->dev, "no i/o regions available\n"); | |
114 | retval = -EBUSY; | |
115 | goto err1; | |
116 | } | |
117 | } | |
118 | ||
1da177e4 LT |
119 | pci_set_master (dev); |
120 | ||
121 | retval = usb_add_hcd (hcd, dev->irq, SA_SHIRQ); | |
122 | if (retval != 0) | |
123 | goto err4; | |
124 | return retval; | |
125 | ||
126 | err4: | |
127 | if (driver->flags & HCD_MEMORY) { | |
128 | iounmap (hcd->regs); | |
129 | err3: | |
130 | release_mem_region (hcd->rsrc_start, hcd->rsrc_len); | |
131 | } else | |
132 | release_region (hcd->rsrc_start, hcd->rsrc_len); | |
133 | err2: | |
134 | usb_put_hcd (hcd); | |
135 | err1: | |
136 | pci_disable_device (dev); | |
137 | dev_err (&dev->dev, "init %s fail, %d\n", pci_name(dev), retval); | |
138 | return retval; | |
139 | } | |
140 | EXPORT_SYMBOL (usb_hcd_pci_probe); | |
141 | ||
142 | ||
143 | /* may be called without controller electrically present */ | |
144 | /* may be called with controller, bus, and devices active */ | |
145 | ||
146 | /** | |
147 | * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs | |
148 | * @dev: USB Host Controller being removed | |
149 | * Context: !in_interrupt() | |
150 | * | |
151 | * Reverses the effect of usb_hcd_pci_probe(), first invoking | |
152 | * the HCD's stop() method. It is always called from a thread | |
153 | * context, normally "rmmod", "apmd", or something similar. | |
154 | * | |
155 | * Store this function in the HCD's struct pci_driver as remove(). | |
156 | */ | |
157 | void usb_hcd_pci_remove (struct pci_dev *dev) | |
158 | { | |
159 | struct usb_hcd *hcd; | |
160 | ||
161 | hcd = pci_get_drvdata(dev); | |
162 | if (!hcd) | |
163 | return; | |
164 | ||
165 | usb_remove_hcd (hcd); | |
166 | if (hcd->driver->flags & HCD_MEMORY) { | |
167 | iounmap (hcd->regs); | |
168 | release_mem_region (hcd->rsrc_start, hcd->rsrc_len); | |
169 | } else { | |
170 | release_region (hcd->rsrc_start, hcd->rsrc_len); | |
171 | } | |
172 | usb_put_hcd (hcd); | |
173 | pci_disable_device(dev); | |
174 | } | |
175 | EXPORT_SYMBOL (usb_hcd_pci_remove); | |
176 | ||
177 | ||
178 | #ifdef CONFIG_PM | |
179 | ||
1da177e4 LT |
180 | /** |
181 | * usb_hcd_pci_suspend - power management suspend of a PCI-based HCD | |
182 | * @dev: USB Host Controller being suspended | |
c6053ecf | 183 | * @message: semantics in flux |
1da177e4 LT |
184 | * |
185 | * Store this function in the HCD's struct pci_driver as suspend(). | |
186 | */ | |
c6053ecf | 187 | int usb_hcd_pci_suspend (struct pci_dev *dev, pm_message_t message) |
1da177e4 LT |
188 | { |
189 | struct usb_hcd *hcd; | |
190 | int retval = 0; | |
191 | int has_pci_pm; | |
192 | ||
193 | hcd = pci_get_drvdata(dev); | |
194 | ||
5f827ea3 DB |
195 | /* Root hub suspend should have stopped all downstream traffic, |
196 | * and all bus master traffic. And done so for both the interface | |
197 | * and the stub usb_device (which we check here). But maybe it | |
198 | * didn't; writing sysfs power/state files ignores such rules... | |
199 | * | |
200 | * We must ignore the FREEZE vs SUSPEND distinction here, because | |
201 | * otherwise the swsusp will save (and restore) garbage state. | |
202 | */ | |
203 | if (hcd->self.root_hub->dev.power.power_state.event == PM_EVENT_ON) | |
204 | return -EBUSY; | |
205 | ||
206 | if (hcd->driver->suspend) { | |
207 | retval = hcd->driver->suspend(hcd, message); | |
208 | if (retval) { | |
209 | dev_dbg (&dev->dev, "PCI pre-suspend fail, %d\n", | |
210 | retval); | |
211 | goto done; | |
212 | } | |
213 | } | |
214 | ||
c6053ecf DB |
215 | /* FIXME until the generic PM interfaces change a lot more, this |
216 | * can't use PCI D1 and D2 states. For example, the confusion | |
217 | * between messages and states will need to vanish, and messages | |
218 | * will need to provide a target system state again. | |
219 | * | |
220 | * It'll be important to learn characteristics of the target state, | |
221 | * especially on embedded hardware where the HCD will often be in | |
222 | * charge of an external VBUS power supply and one or more clocks. | |
223 | * Some target system states will leave them active; others won't. | |
224 | * (With PCI, that's often handled by platform BIOS code.) | |
225 | */ | |
226 | ||
1da177e4 LT |
227 | /* even when the PCI layer rejects some of the PCI calls |
228 | * below, HCs can try global suspend and reduce DMA traffic. | |
229 | * PM-sensitive HCDs may already have done this. | |
230 | */ | |
231 | has_pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); | |
1da177e4 | 232 | |
5f827ea3 DB |
233 | /* Downstream ports from this root hub should already be quiesced, so |
234 | * there will be no DMA activity. Now we can shut down the upstream | |
235 | * link (except maybe for PME# resume signaling) and enter some PCI | |
236 | * low power state, if the hardware allows. | |
1da177e4 | 237 | */ |
5f827ea3 | 238 | if (hcd->state == HC_STATE_SUSPENDED) { |
1da177e4 | 239 | |
c6053ecf DB |
240 | /* no DMA or IRQs except when HC is active */ |
241 | if (dev->current_state == PCI_D0) { | |
1da177e4 LT |
242 | pci_save_state (dev); |
243 | pci_disable_device (dev); | |
1da177e4 LT |
244 | } |
245 | ||
246 | if (!has_pci_pm) { | |
247 | dev_dbg (hcd->self.controller, "--> PCI D0/legacy\n"); | |
5f827ea3 | 248 | goto done; |
1da177e4 LT |
249 | } |
250 | ||
c6053ecf DB |
251 | /* NOTE: dev->current_state becomes nonzero only here, and |
252 | * only for devices that support PCI PM. Also, exiting | |
253 | * PCI_D3 (but not PCI_D1 or PCI_D2) is allowed to reset | |
254 | * some device state (e.g. as part of clock reinit). | |
1da177e4 | 255 | */ |
c6053ecf | 256 | retval = pci_set_power_state (dev, PCI_D3hot); |
1da177e4 | 257 | if (retval == 0) { |
c6053ecf | 258 | dev_dbg (hcd->self.controller, "--> PCI D3\n"); |
5f827ea3 DB |
259 | |
260 | /* Ignore these return values. We rely on pci code to | |
261 | * reject requests the hardware can't implement, rather | |
262 | * than coding the same thing. | |
263 | */ | |
264 | (void) pci_enable_wake (dev, PCI_D3hot, hcd->remote_wakeup); | |
265 | (void) pci_enable_wake (dev, PCI_D3cold, hcd->remote_wakeup); | |
266 | } else { | |
c6053ecf DB |
267 | dev_dbg (&dev->dev, "PCI D3 suspend fail, %d\n", |
268 | retval); | |
1da177e4 | 269 | (void) usb_hcd_pci_resume (dev); |
1da177e4 | 270 | } |
5f827ea3 DB |
271 | |
272 | } else { | |
1da177e4 LT |
273 | dev_dbg (hcd->self.controller, "hcd state %d; not suspended\n", |
274 | hcd->state); | |
c6053ecf | 275 | WARN_ON(1); |
1da177e4 | 276 | retval = -EINVAL; |
1da177e4 LT |
277 | } |
278 | ||
5f827ea3 | 279 | done: |
1da177e4 | 280 | if (retval == 0) |
5f827ea3 | 281 | dev->dev.power.power_state = PMSG_SUSPEND; |
1da177e4 LT |
282 | return retval; |
283 | } | |
284 | EXPORT_SYMBOL (usb_hcd_pci_suspend); | |
285 | ||
286 | /** | |
287 | * usb_hcd_pci_resume - power management resume of a PCI-based HCD | |
288 | * @dev: USB Host Controller being resumed | |
289 | * | |
290 | * Store this function in the HCD's struct pci_driver as resume(). | |
291 | */ | |
292 | int usb_hcd_pci_resume (struct pci_dev *dev) | |
293 | { | |
294 | struct usb_hcd *hcd; | |
295 | int retval; | |
1da177e4 LT |
296 | |
297 | hcd = pci_get_drvdata(dev); | |
298 | if (hcd->state != HC_STATE_SUSPENDED) { | |
299 | dev_dbg (hcd->self.controller, | |
300 | "can't resume, not suspended!\n"); | |
301 | return 0; | |
302 | } | |
1da177e4 | 303 | |
c6053ecf DB |
304 | /* NOTE: chip docs cover clean "real suspend" cases (what Linux |
305 | * calls "standby", "suspend to RAM", and so on). There are also | |
306 | * dirty cases when swsusp fakes a suspend in "shutdown" mode. | |
307 | */ | |
308 | if (dev->current_state != PCI_D0) { | |
309 | #ifdef DEBUG | |
310 | int pci_pm; | |
311 | u16 pmcr; | |
312 | ||
313 | pci_pm = pci_find_capability(dev, PCI_CAP_ID_PM); | |
314 | pci_read_config_word(dev, pci_pm + PCI_PM_CTRL, &pmcr); | |
315 | pmcr &= PCI_PM_CTRL_STATE_MASK; | |
316 | if (pmcr) { | |
317 | /* Clean case: power to USB and to HC registers was | |
318 | * maintained; remote wakeup is easy. | |
319 | */ | |
320 | dev_dbg(hcd->self.controller, "resume from PCI D%d\n", | |
321 | pmcr); | |
322 | } else { | |
323 | /* Clean: HC lost Vcc power, D0 uninitialized | |
324 | * + Vaux may have preserved port and transceiver | |
325 | * state ... for remote wakeup from D3cold | |
326 | * + or not; HCD must reinit + re-enumerate | |
327 | * | |
328 | * Dirty: D0 semi-initialized cases with swsusp | |
329 | * + after BIOS init | |
330 | * + after Linux init (HCD statically linked) | |
331 | */ | |
332 | dev_dbg(hcd->self.controller, | |
333 | "PCI D0, from previous PCI D%d\n", | |
334 | dev->current_state); | |
335 | } | |
336 | #endif | |
5f827ea3 DB |
337 | /* yes, ignore these results too... */ |
338 | (void) pci_enable_wake (dev, dev->current_state, 0); | |
339 | (void) pci_enable_wake (dev, PCI_D3cold, 0); | |
c6053ecf DB |
340 | } else { |
341 | /* Same basic cases: clean (powered/not), dirty */ | |
342 | dev_dbg(hcd->self.controller, "PCI legacy resume\n"); | |
343 | } | |
344 | ||
345 | /* NOTE: the PCI API itself is asymmetric here. We don't need to | |
346 | * pci_set_power_state(PCI_D0) since that's part of re-enabling; | |
347 | * but that won't re-enable bus mastering. Yet pci_disable_device() | |
348 | * explicitly disables bus mastering... | |
349 | */ | |
350 | retval = pci_enable_device (dev); | |
351 | if (retval < 0) { | |
352 | dev_err (hcd->self.controller, | |
353 | "can't re-enable after resume, %d!\n", retval); | |
354 | return retval; | |
355 | } | |
356 | pci_set_master (dev); | |
357 | pci_restore_state (dev); | |
358 | ||
359 | dev->dev.power.power_state = PMSG_ON; | |
1da177e4 | 360 | |
c6053ecf | 361 | hcd->saw_irq = 0; |
1da177e4 | 362 | |
5f827ea3 DB |
363 | if (hcd->driver->resume) { |
364 | retval = hcd->driver->resume(hcd); | |
365 | if (retval) { | |
366 | dev_err (hcd->self.controller, | |
367 | "PCI post-resume error %d!\n", retval); | |
368 | usb_hc_died (hcd); | |
369 | } | |
1da177e4 LT |
370 | } |
371 | ||
372 | return retval; | |
373 | } | |
374 | EXPORT_SYMBOL (usb_hcd_pci_resume); | |
375 | ||
376 | #endif /* CONFIG_PM */ | |
377 | ||
378 |