1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * (C) Copyright David Brownell 2000-2002
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun #include <linux/module.h>
8*4882a593Smuzhiyun #include <linux/pci.h>
9*4882a593Smuzhiyun #include <linux/usb.h>
10*4882a593Smuzhiyun #include <linux/usb/hcd.h>
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #include <asm/io.h>
13*4882a593Smuzhiyun #include <asm/irq.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #ifdef CONFIG_PPC_PMAC
16*4882a593Smuzhiyun #include <asm/machdep.h>
17*4882a593Smuzhiyun #include <asm/pmac_feature.h>
18*4882a593Smuzhiyun #include <asm/prom.h>
19*4882a593Smuzhiyun #endif
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "usb.h"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * Coordinate handoffs between EHCI and companion controllers
28*4882a593Smuzhiyun * during EHCI probing and system resume.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun static DECLARE_RWSEM(companions_rwsem);
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #define CL_UHCI PCI_CLASS_SERIAL_USB_UHCI
34*4882a593Smuzhiyun #define CL_OHCI PCI_CLASS_SERIAL_USB_OHCI
35*4882a593Smuzhiyun #define CL_EHCI PCI_CLASS_SERIAL_USB_EHCI
36*4882a593Smuzhiyun
is_ohci_or_uhci(struct pci_dev * pdev)37*4882a593Smuzhiyun static inline int is_ohci_or_uhci(struct pci_dev *pdev)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun return pdev->class == CL_OHCI || pdev->class == CL_UHCI;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun typedef void (*companion_fn)(struct pci_dev *pdev, struct usb_hcd *hcd,
43*4882a593Smuzhiyun struct pci_dev *companion, struct usb_hcd *companion_hcd);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* Iterate over PCI devices in the same slot as pdev and call fn for each */
for_each_companion(struct pci_dev * pdev,struct usb_hcd * hcd,companion_fn fn)46*4882a593Smuzhiyun static void for_each_companion(struct pci_dev *pdev, struct usb_hcd *hcd,
47*4882a593Smuzhiyun companion_fn fn)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun struct pci_dev *companion;
50*4882a593Smuzhiyun struct usb_hcd *companion_hcd;
51*4882a593Smuzhiyun unsigned int slot = PCI_SLOT(pdev->devfn);
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Iterate through other PCI functions in the same slot.
55*4882a593Smuzhiyun * If the function's drvdata isn't set then it isn't bound to
56*4882a593Smuzhiyun * a USB host controller driver, so skip it.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun companion = NULL;
59*4882a593Smuzhiyun for_each_pci_dev(companion) {
60*4882a593Smuzhiyun if (companion->bus != pdev->bus ||
61*4882a593Smuzhiyun PCI_SLOT(companion->devfn) != slot)
62*4882a593Smuzhiyun continue;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun /*
65*4882a593Smuzhiyun * Companion device should be either UHCI,OHCI or EHCI host
66*4882a593Smuzhiyun * controller, otherwise skip.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun if (companion->class != CL_UHCI && companion->class != CL_OHCI &&
69*4882a593Smuzhiyun companion->class != CL_EHCI)
70*4882a593Smuzhiyun continue;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun companion_hcd = pci_get_drvdata(companion);
73*4882a593Smuzhiyun if (!companion_hcd || !companion_hcd->self.root_hub)
74*4882a593Smuzhiyun continue;
75*4882a593Smuzhiyun fn(pdev, hcd, companion, companion_hcd);
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun * We're about to add an EHCI controller, which will unceremoniously grab
81*4882a593Smuzhiyun * all the port connections away from its companions. To prevent annoying
82*4882a593Smuzhiyun * error messages, lock the companion's root hub and gracefully unconfigure
83*4882a593Smuzhiyun * it beforehand. Leave it locked until the EHCI controller is all set.
84*4882a593Smuzhiyun */
ehci_pre_add(struct pci_dev * pdev,struct usb_hcd * hcd,struct pci_dev * companion,struct usb_hcd * companion_hcd)85*4882a593Smuzhiyun static void ehci_pre_add(struct pci_dev *pdev, struct usb_hcd *hcd,
86*4882a593Smuzhiyun struct pci_dev *companion, struct usb_hcd *companion_hcd)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun struct usb_device *udev;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (is_ohci_or_uhci(companion)) {
91*4882a593Smuzhiyun udev = companion_hcd->self.root_hub;
92*4882a593Smuzhiyun usb_lock_device(udev);
93*4882a593Smuzhiyun usb_set_configuration(udev, 0);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /*
98*4882a593Smuzhiyun * Adding the EHCI controller has either succeeded or failed. Set the
99*4882a593Smuzhiyun * companion pointer accordingly, and in either case, reconfigure and
100*4882a593Smuzhiyun * unlock the root hub.
101*4882a593Smuzhiyun */
ehci_post_add(struct pci_dev * pdev,struct usb_hcd * hcd,struct pci_dev * companion,struct usb_hcd * companion_hcd)102*4882a593Smuzhiyun static void ehci_post_add(struct pci_dev *pdev, struct usb_hcd *hcd,
103*4882a593Smuzhiyun struct pci_dev *companion, struct usb_hcd *companion_hcd)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun struct usb_device *udev;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun if (is_ohci_or_uhci(companion)) {
108*4882a593Smuzhiyun if (dev_get_drvdata(&pdev->dev)) { /* Succeeded */
109*4882a593Smuzhiyun dev_dbg(&pdev->dev, "HS companion for %s\n",
110*4882a593Smuzhiyun dev_name(&companion->dev));
111*4882a593Smuzhiyun companion_hcd->self.hs_companion = &hcd->self;
112*4882a593Smuzhiyun }
113*4882a593Smuzhiyun udev = companion_hcd->self.root_hub;
114*4882a593Smuzhiyun usb_set_configuration(udev, 1);
115*4882a593Smuzhiyun usb_unlock_device(udev);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun /*
120*4882a593Smuzhiyun * We just added a non-EHCI controller. Find the EHCI controller to
121*4882a593Smuzhiyun * which it is a companion, and store a pointer to the bus structure.
122*4882a593Smuzhiyun */
non_ehci_add(struct pci_dev * pdev,struct usb_hcd * hcd,struct pci_dev * companion,struct usb_hcd * companion_hcd)123*4882a593Smuzhiyun static void non_ehci_add(struct pci_dev *pdev, struct usb_hcd *hcd,
124*4882a593Smuzhiyun struct pci_dev *companion, struct usb_hcd *companion_hcd)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun if (is_ohci_or_uhci(pdev) && companion->class == CL_EHCI) {
127*4882a593Smuzhiyun dev_dbg(&pdev->dev, "FS/LS companion for %s\n",
128*4882a593Smuzhiyun dev_name(&companion->dev));
129*4882a593Smuzhiyun hcd->self.hs_companion = &companion_hcd->self;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* We are removing an EHCI controller. Clear the companions' pointers. */
ehci_remove(struct pci_dev * pdev,struct usb_hcd * hcd,struct pci_dev * companion,struct usb_hcd * companion_hcd)134*4882a593Smuzhiyun static void ehci_remove(struct pci_dev *pdev, struct usb_hcd *hcd,
135*4882a593Smuzhiyun struct pci_dev *companion, struct usb_hcd *companion_hcd)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun if (is_ohci_or_uhci(companion))
138*4882a593Smuzhiyun companion_hcd->self.hs_companion = NULL;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun #ifdef CONFIG_PM
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* An EHCI controller must wait for its companions before resuming. */
ehci_wait_for_companions(struct pci_dev * pdev,struct usb_hcd * hcd,struct pci_dev * companion,struct usb_hcd * companion_hcd)144*4882a593Smuzhiyun static void ehci_wait_for_companions(struct pci_dev *pdev, struct usb_hcd *hcd,
145*4882a593Smuzhiyun struct pci_dev *companion, struct usb_hcd *companion_hcd)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun if (is_ohci_or_uhci(companion))
148*4882a593Smuzhiyun device_pm_wait_for_dev(&pdev->dev, &companion->dev);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun #endif /* CONFIG_PM */
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun /* configure so an HC device and id are always provided */
156*4882a593Smuzhiyun /* always called with process context; sleeping is OK */
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /**
159*4882a593Smuzhiyun * usb_hcd_pci_probe - initialize PCI-based HCDs
160*4882a593Smuzhiyun * @dev: USB Host Controller being probed
161*4882a593Smuzhiyun * @id: pci hotplug id connecting controller to HCD framework
162*4882a593Smuzhiyun * @driver: USB HC driver handle
163*4882a593Smuzhiyun * Context: !in_interrupt()
164*4882a593Smuzhiyun *
165*4882a593Smuzhiyun * Allocates basic PCI resources for this USB host controller, and
166*4882a593Smuzhiyun * then invokes the start() method for the HCD associated with it
167*4882a593Smuzhiyun * through the hotplug entry's driver_data.
168*4882a593Smuzhiyun *
169*4882a593Smuzhiyun * Store this function in the HCD's struct pci_driver as probe().
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Return: 0 if successful.
172*4882a593Smuzhiyun */
usb_hcd_pci_probe(struct pci_dev * dev,const struct pci_device_id * id,const struct hc_driver * driver)173*4882a593Smuzhiyun int usb_hcd_pci_probe(struct pci_dev *dev, const struct pci_device_id *id,
174*4882a593Smuzhiyun const struct hc_driver *driver)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun struct usb_hcd *hcd;
177*4882a593Smuzhiyun int retval;
178*4882a593Smuzhiyun int hcd_irq = 0;
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun if (usb_disabled())
181*4882a593Smuzhiyun return -ENODEV;
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (!id)
184*4882a593Smuzhiyun return -EINVAL;
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun if (!driver)
187*4882a593Smuzhiyun return -EINVAL;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (pci_enable_device(dev) < 0)
190*4882a593Smuzhiyun return -ENODEV;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /*
193*4882a593Smuzhiyun * The xHCI driver has its own irq management
194*4882a593Smuzhiyun * make sure irq setup is not touched for xhci in generic hcd code
195*4882a593Smuzhiyun */
196*4882a593Smuzhiyun if ((driver->flags & HCD_MASK) < HCD_USB3) {
197*4882a593Smuzhiyun retval = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY | PCI_IRQ_MSI);
198*4882a593Smuzhiyun if (retval < 0) {
199*4882a593Smuzhiyun dev_err(&dev->dev,
200*4882a593Smuzhiyun "Found HC with no IRQ. Check BIOS/PCI %s setup!\n",
201*4882a593Smuzhiyun pci_name(dev));
202*4882a593Smuzhiyun retval = -ENODEV;
203*4882a593Smuzhiyun goto disable_pci;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun hcd_irq = pci_irq_vector(dev, 0);
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun hcd = usb_create_hcd(driver, &dev->dev, pci_name(dev));
209*4882a593Smuzhiyun if (!hcd) {
210*4882a593Smuzhiyun retval = -ENOMEM;
211*4882a593Smuzhiyun goto free_irq_vectors;
212*4882a593Smuzhiyun }
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun hcd->amd_resume_bug = (usb_hcd_amd_remote_wakeup_quirk(dev) &&
215*4882a593Smuzhiyun driver->flags & (HCD_USB11 | HCD_USB3)) ? 1 : 0;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (driver->flags & HCD_MEMORY) {
218*4882a593Smuzhiyun /* EHCI, OHCI */
219*4882a593Smuzhiyun hcd->rsrc_start = pci_resource_start(dev, 0);
220*4882a593Smuzhiyun hcd->rsrc_len = pci_resource_len(dev, 0);
221*4882a593Smuzhiyun if (!devm_request_mem_region(&dev->dev, hcd->rsrc_start,
222*4882a593Smuzhiyun hcd->rsrc_len, driver->description)) {
223*4882a593Smuzhiyun dev_dbg(&dev->dev, "controller already in use\n");
224*4882a593Smuzhiyun retval = -EBUSY;
225*4882a593Smuzhiyun goto put_hcd;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun hcd->regs = devm_ioremap(&dev->dev, hcd->rsrc_start,
228*4882a593Smuzhiyun hcd->rsrc_len);
229*4882a593Smuzhiyun if (hcd->regs == NULL) {
230*4882a593Smuzhiyun dev_dbg(&dev->dev, "error mapping memory\n");
231*4882a593Smuzhiyun retval = -EFAULT;
232*4882a593Smuzhiyun goto put_hcd;
233*4882a593Smuzhiyun }
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun } else {
236*4882a593Smuzhiyun /* UHCI */
237*4882a593Smuzhiyun int region;
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun for (region = 0; region < PCI_STD_NUM_BARS; region++) {
240*4882a593Smuzhiyun if (!(pci_resource_flags(dev, region) &
241*4882a593Smuzhiyun IORESOURCE_IO))
242*4882a593Smuzhiyun continue;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun hcd->rsrc_start = pci_resource_start(dev, region);
245*4882a593Smuzhiyun hcd->rsrc_len = pci_resource_len(dev, region);
246*4882a593Smuzhiyun if (devm_request_region(&dev->dev, hcd->rsrc_start,
247*4882a593Smuzhiyun hcd->rsrc_len, driver->description))
248*4882a593Smuzhiyun break;
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun if (region == PCI_ROM_RESOURCE) {
251*4882a593Smuzhiyun dev_dbg(&dev->dev, "no i/o regions available\n");
252*4882a593Smuzhiyun retval = -EBUSY;
253*4882a593Smuzhiyun goto put_hcd;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun pci_set_master(dev);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* Note: dev_set_drvdata must be called while holding the rwsem */
260*4882a593Smuzhiyun if (dev->class == CL_EHCI) {
261*4882a593Smuzhiyun down_write(&companions_rwsem);
262*4882a593Smuzhiyun dev_set_drvdata(&dev->dev, hcd);
263*4882a593Smuzhiyun for_each_companion(dev, hcd, ehci_pre_add);
264*4882a593Smuzhiyun retval = usb_add_hcd(hcd, hcd_irq, IRQF_SHARED);
265*4882a593Smuzhiyun if (retval != 0)
266*4882a593Smuzhiyun dev_set_drvdata(&dev->dev, NULL);
267*4882a593Smuzhiyun for_each_companion(dev, hcd, ehci_post_add);
268*4882a593Smuzhiyun up_write(&companions_rwsem);
269*4882a593Smuzhiyun } else {
270*4882a593Smuzhiyun down_read(&companions_rwsem);
271*4882a593Smuzhiyun dev_set_drvdata(&dev->dev, hcd);
272*4882a593Smuzhiyun retval = usb_add_hcd(hcd, hcd_irq, IRQF_SHARED);
273*4882a593Smuzhiyun if (retval != 0)
274*4882a593Smuzhiyun dev_set_drvdata(&dev->dev, NULL);
275*4882a593Smuzhiyun else
276*4882a593Smuzhiyun for_each_companion(dev, hcd, non_ehci_add);
277*4882a593Smuzhiyun up_read(&companions_rwsem);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun if (retval != 0)
281*4882a593Smuzhiyun goto put_hcd;
282*4882a593Smuzhiyun device_wakeup_enable(hcd->self.controller);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun if (pci_dev_run_wake(dev))
285*4882a593Smuzhiyun pm_runtime_put_noidle(&dev->dev);
286*4882a593Smuzhiyun return retval;
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun put_hcd:
289*4882a593Smuzhiyun usb_put_hcd(hcd);
290*4882a593Smuzhiyun free_irq_vectors:
291*4882a593Smuzhiyun if ((driver->flags & HCD_MASK) < HCD_USB3)
292*4882a593Smuzhiyun pci_free_irq_vectors(dev);
293*4882a593Smuzhiyun disable_pci:
294*4882a593Smuzhiyun pci_disable_device(dev);
295*4882a593Smuzhiyun dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
296*4882a593Smuzhiyun return retval;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hcd_pci_probe);
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun /* may be called without controller electrically present */
302*4882a593Smuzhiyun /* may be called with controller, bus, and devices active */
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /**
305*4882a593Smuzhiyun * usb_hcd_pci_remove - shutdown processing for PCI-based HCDs
306*4882a593Smuzhiyun * @dev: USB Host Controller being removed
307*4882a593Smuzhiyun * Context: !in_interrupt()
308*4882a593Smuzhiyun *
309*4882a593Smuzhiyun * Reverses the effect of usb_hcd_pci_probe(), first invoking
310*4882a593Smuzhiyun * the HCD's stop() method. It is always called from a thread
311*4882a593Smuzhiyun * context, normally "rmmod", "apmd", or something similar.
312*4882a593Smuzhiyun *
313*4882a593Smuzhiyun * Store this function in the HCD's struct pci_driver as remove().
314*4882a593Smuzhiyun */
usb_hcd_pci_remove(struct pci_dev * dev)315*4882a593Smuzhiyun void usb_hcd_pci_remove(struct pci_dev *dev)
316*4882a593Smuzhiyun {
317*4882a593Smuzhiyun struct usb_hcd *hcd;
318*4882a593Smuzhiyun int hcd_driver_flags;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun hcd = pci_get_drvdata(dev);
321*4882a593Smuzhiyun if (!hcd)
322*4882a593Smuzhiyun return;
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun hcd_driver_flags = hcd->driver->flags;
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun if (pci_dev_run_wake(dev))
327*4882a593Smuzhiyun pm_runtime_get_noresume(&dev->dev);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /* Fake an interrupt request in order to give the driver a chance
330*4882a593Smuzhiyun * to test whether the controller hardware has been removed (e.g.,
331*4882a593Smuzhiyun * cardbus physical eject).
332*4882a593Smuzhiyun */
333*4882a593Smuzhiyun local_irq_disable();
334*4882a593Smuzhiyun usb_hcd_irq(0, hcd);
335*4882a593Smuzhiyun local_irq_enable();
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun /* Note: dev_set_drvdata must be called while holding the rwsem */
338*4882a593Smuzhiyun if (dev->class == CL_EHCI) {
339*4882a593Smuzhiyun down_write(&companions_rwsem);
340*4882a593Smuzhiyun for_each_companion(dev, hcd, ehci_remove);
341*4882a593Smuzhiyun usb_remove_hcd(hcd);
342*4882a593Smuzhiyun dev_set_drvdata(&dev->dev, NULL);
343*4882a593Smuzhiyun up_write(&companions_rwsem);
344*4882a593Smuzhiyun } else {
345*4882a593Smuzhiyun /* Not EHCI; just clear the companion pointer */
346*4882a593Smuzhiyun down_read(&companions_rwsem);
347*4882a593Smuzhiyun hcd->self.hs_companion = NULL;
348*4882a593Smuzhiyun usb_remove_hcd(hcd);
349*4882a593Smuzhiyun dev_set_drvdata(&dev->dev, NULL);
350*4882a593Smuzhiyun up_read(&companions_rwsem);
351*4882a593Smuzhiyun }
352*4882a593Smuzhiyun usb_put_hcd(hcd);
353*4882a593Smuzhiyun if ((hcd_driver_flags & HCD_MASK) < HCD_USB3)
354*4882a593Smuzhiyun pci_free_irq_vectors(dev);
355*4882a593Smuzhiyun pci_disable_device(dev);
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun /**
360*4882a593Smuzhiyun * usb_hcd_pci_shutdown - shutdown host controller
361*4882a593Smuzhiyun * @dev: USB Host Controller being shutdown
362*4882a593Smuzhiyun */
usb_hcd_pci_shutdown(struct pci_dev * dev)363*4882a593Smuzhiyun void usb_hcd_pci_shutdown(struct pci_dev *dev)
364*4882a593Smuzhiyun {
365*4882a593Smuzhiyun struct usb_hcd *hcd;
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun hcd = pci_get_drvdata(dev);
368*4882a593Smuzhiyun if (!hcd)
369*4882a593Smuzhiyun return;
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) &&
372*4882a593Smuzhiyun hcd->driver->shutdown) {
373*4882a593Smuzhiyun hcd->driver->shutdown(hcd);
374*4882a593Smuzhiyun if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
375*4882a593Smuzhiyun free_irq(hcd->irq, hcd);
376*4882a593Smuzhiyun pci_disable_device(dev);
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun #ifdef CONFIG_PM
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun #ifdef CONFIG_PPC_PMAC
powermac_set_asic(struct pci_dev * pci_dev,int enable)384*4882a593Smuzhiyun static void powermac_set_asic(struct pci_dev *pci_dev, int enable)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun /* Enanble or disable ASIC clocks for USB */
387*4882a593Smuzhiyun if (machine_is(powermac)) {
388*4882a593Smuzhiyun struct device_node *of_node;
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun of_node = pci_device_to_OF_node(pci_dev);
391*4882a593Smuzhiyun if (of_node)
392*4882a593Smuzhiyun pmac_call_feature(PMAC_FTR_USB_ENABLE,
393*4882a593Smuzhiyun of_node, 0, enable);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun #else
398*4882a593Smuzhiyun
powermac_set_asic(struct pci_dev * pci_dev,int enable)399*4882a593Smuzhiyun static inline void powermac_set_asic(struct pci_dev *pci_dev, int enable)
400*4882a593Smuzhiyun {}
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun #endif /* CONFIG_PPC_PMAC */
403*4882a593Smuzhiyun
check_root_hub_suspended(struct device * dev)404*4882a593Smuzhiyun static int check_root_hub_suspended(struct device *dev)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun struct usb_hcd *hcd = dev_get_drvdata(dev);
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun if (HCD_RH_RUNNING(hcd)) {
409*4882a593Smuzhiyun dev_warn(dev, "Root hub is not suspended\n");
410*4882a593Smuzhiyun return -EBUSY;
411*4882a593Smuzhiyun }
412*4882a593Smuzhiyun if (hcd->shared_hcd) {
413*4882a593Smuzhiyun hcd = hcd->shared_hcd;
414*4882a593Smuzhiyun if (HCD_RH_RUNNING(hcd)) {
415*4882a593Smuzhiyun dev_warn(dev, "Secondary root hub is not suspended\n");
416*4882a593Smuzhiyun return -EBUSY;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun return 0;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
suspend_common(struct device * dev,bool do_wakeup)422*4882a593Smuzhiyun static int suspend_common(struct device *dev, bool do_wakeup)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun struct pci_dev *pci_dev = to_pci_dev(dev);
425*4882a593Smuzhiyun struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
426*4882a593Smuzhiyun int retval;
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun /* Root hub suspend should have stopped all downstream traffic,
429*4882a593Smuzhiyun * and all bus master traffic. And done so for both the interface
430*4882a593Smuzhiyun * and the stub usb_device (which we check here). But maybe it
431*4882a593Smuzhiyun * didn't; writing sysfs power/state files ignores such rules...
432*4882a593Smuzhiyun */
433*4882a593Smuzhiyun retval = check_root_hub_suspended(dev);
434*4882a593Smuzhiyun if (retval)
435*4882a593Smuzhiyun return retval;
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun if (hcd->driver->pci_suspend && !HCD_DEAD(hcd)) {
438*4882a593Smuzhiyun /* Optimization: Don't suspend if a root-hub wakeup is
439*4882a593Smuzhiyun * pending and it would cause the HCD to wake up anyway.
440*4882a593Smuzhiyun */
441*4882a593Smuzhiyun if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
442*4882a593Smuzhiyun return -EBUSY;
443*4882a593Smuzhiyun if (do_wakeup && hcd->shared_hcd &&
444*4882a593Smuzhiyun HCD_WAKEUP_PENDING(hcd->shared_hcd))
445*4882a593Smuzhiyun return -EBUSY;
446*4882a593Smuzhiyun retval = hcd->driver->pci_suspend(hcd, do_wakeup);
447*4882a593Smuzhiyun suspend_report_result(hcd->driver->pci_suspend, retval);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* Check again in case wakeup raced with pci_suspend */
450*4882a593Smuzhiyun if ((retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) ||
451*4882a593Smuzhiyun (retval == 0 && do_wakeup && hcd->shared_hcd &&
452*4882a593Smuzhiyun HCD_WAKEUP_PENDING(hcd->shared_hcd))) {
453*4882a593Smuzhiyun if (hcd->driver->pci_resume)
454*4882a593Smuzhiyun hcd->driver->pci_resume(hcd, false);
455*4882a593Smuzhiyun retval = -EBUSY;
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun if (retval)
458*4882a593Smuzhiyun return retval;
459*4882a593Smuzhiyun }
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun /* If MSI-X is enabled, the driver will have synchronized all vectors
462*4882a593Smuzhiyun * in pci_suspend(). If MSI or legacy PCI is enabled, that will be
463*4882a593Smuzhiyun * synchronized here.
464*4882a593Smuzhiyun */
465*4882a593Smuzhiyun if (!hcd->msix_enabled)
466*4882a593Smuzhiyun synchronize_irq(pci_irq_vector(pci_dev, 0));
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /* Downstream ports from this root hub should already be quiesced, so
469*4882a593Smuzhiyun * there will be no DMA activity. Now we can shut down the upstream
470*4882a593Smuzhiyun * link (except maybe for PME# resume signaling). We'll enter a
471*4882a593Smuzhiyun * low power state during suspend_noirq, if the hardware allows.
472*4882a593Smuzhiyun */
473*4882a593Smuzhiyun pci_disable_device(pci_dev);
474*4882a593Smuzhiyun return retval;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun
resume_common(struct device * dev,int event)477*4882a593Smuzhiyun static int resume_common(struct device *dev, int event)
478*4882a593Smuzhiyun {
479*4882a593Smuzhiyun struct pci_dev *pci_dev = to_pci_dev(dev);
480*4882a593Smuzhiyun struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
481*4882a593Smuzhiyun int retval;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (HCD_RH_RUNNING(hcd) ||
484*4882a593Smuzhiyun (hcd->shared_hcd &&
485*4882a593Smuzhiyun HCD_RH_RUNNING(hcd->shared_hcd))) {
486*4882a593Smuzhiyun dev_dbg(dev, "can't resume, not suspended!\n");
487*4882a593Smuzhiyun return 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun retval = pci_enable_device(pci_dev);
491*4882a593Smuzhiyun if (retval < 0) {
492*4882a593Smuzhiyun dev_err(dev, "can't re-enable after resume, %d!\n", retval);
493*4882a593Smuzhiyun return retval;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun pci_set_master(pci_dev);
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun if (hcd->driver->pci_resume && !HCD_DEAD(hcd)) {
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /*
501*4882a593Smuzhiyun * Only EHCI controllers have to wait for their companions.
502*4882a593Smuzhiyun * No locking is needed because PCI controller drivers do not
503*4882a593Smuzhiyun * get unbound during system resume.
504*4882a593Smuzhiyun */
505*4882a593Smuzhiyun if (pci_dev->class == CL_EHCI && event != PM_EVENT_AUTO_RESUME)
506*4882a593Smuzhiyun for_each_companion(pci_dev, hcd,
507*4882a593Smuzhiyun ehci_wait_for_companions);
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun retval = hcd->driver->pci_resume(hcd,
510*4882a593Smuzhiyun event == PM_EVENT_RESTORE);
511*4882a593Smuzhiyun if (retval) {
512*4882a593Smuzhiyun dev_err(dev, "PCI post-resume error %d!\n", retval);
513*4882a593Smuzhiyun usb_hc_died(hcd);
514*4882a593Smuzhiyun }
515*4882a593Smuzhiyun }
516*4882a593Smuzhiyun return retval;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
520*4882a593Smuzhiyun
hcd_pci_suspend(struct device * dev)521*4882a593Smuzhiyun static int hcd_pci_suspend(struct device *dev)
522*4882a593Smuzhiyun {
523*4882a593Smuzhiyun return suspend_common(dev, device_may_wakeup(dev));
524*4882a593Smuzhiyun }
525*4882a593Smuzhiyun
hcd_pci_suspend_noirq(struct device * dev)526*4882a593Smuzhiyun static int hcd_pci_suspend_noirq(struct device *dev)
527*4882a593Smuzhiyun {
528*4882a593Smuzhiyun struct pci_dev *pci_dev = to_pci_dev(dev);
529*4882a593Smuzhiyun struct usb_hcd *hcd = pci_get_drvdata(pci_dev);
530*4882a593Smuzhiyun int retval;
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun retval = check_root_hub_suspended(dev);
533*4882a593Smuzhiyun if (retval)
534*4882a593Smuzhiyun return retval;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun pci_save_state(pci_dev);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun /* If the root hub is dead rather than suspended, disallow remote
539*4882a593Smuzhiyun * wakeup. usb_hc_died() should ensure that both hosts are marked as
540*4882a593Smuzhiyun * dying, so we only need to check the primary roothub.
541*4882a593Smuzhiyun */
542*4882a593Smuzhiyun if (HCD_DEAD(hcd))
543*4882a593Smuzhiyun device_set_wakeup_enable(dev, 0);
544*4882a593Smuzhiyun dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun /* Possibly enable remote wakeup,
547*4882a593Smuzhiyun * choose the appropriate low-power state, and go to that state.
548*4882a593Smuzhiyun */
549*4882a593Smuzhiyun retval = pci_prepare_to_sleep(pci_dev);
550*4882a593Smuzhiyun if (retval == -EIO) { /* Low-power not supported */
551*4882a593Smuzhiyun dev_dbg(dev, "--> PCI D0 legacy\n");
552*4882a593Smuzhiyun retval = 0;
553*4882a593Smuzhiyun } else if (retval == 0) {
554*4882a593Smuzhiyun dev_dbg(dev, "--> PCI %s\n",
555*4882a593Smuzhiyun pci_power_name(pci_dev->current_state));
556*4882a593Smuzhiyun } else {
557*4882a593Smuzhiyun suspend_report_result(pci_prepare_to_sleep, retval);
558*4882a593Smuzhiyun return retval;
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun powermac_set_asic(pci_dev, 0);
562*4882a593Smuzhiyun return retval;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun
hcd_pci_resume_noirq(struct device * dev)565*4882a593Smuzhiyun static int hcd_pci_resume_noirq(struct device *dev)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun powermac_set_asic(to_pci_dev(dev), 1);
568*4882a593Smuzhiyun return 0;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun
hcd_pci_resume(struct device * dev)571*4882a593Smuzhiyun static int hcd_pci_resume(struct device *dev)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun return resume_common(dev, PM_EVENT_RESUME);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun
hcd_pci_restore(struct device * dev)576*4882a593Smuzhiyun static int hcd_pci_restore(struct device *dev)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun return resume_common(dev, PM_EVENT_RESTORE);
579*4882a593Smuzhiyun }
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun #else
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun #define hcd_pci_suspend NULL
584*4882a593Smuzhiyun #define hcd_pci_suspend_noirq NULL
585*4882a593Smuzhiyun #define hcd_pci_resume_noirq NULL
586*4882a593Smuzhiyun #define hcd_pci_resume NULL
587*4882a593Smuzhiyun #define hcd_pci_restore NULL
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
590*4882a593Smuzhiyun
hcd_pci_runtime_suspend(struct device * dev)591*4882a593Smuzhiyun static int hcd_pci_runtime_suspend(struct device *dev)
592*4882a593Smuzhiyun {
593*4882a593Smuzhiyun int retval;
594*4882a593Smuzhiyun
595*4882a593Smuzhiyun retval = suspend_common(dev, true);
596*4882a593Smuzhiyun if (retval == 0)
597*4882a593Smuzhiyun powermac_set_asic(to_pci_dev(dev), 0);
598*4882a593Smuzhiyun dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
599*4882a593Smuzhiyun return retval;
600*4882a593Smuzhiyun }
601*4882a593Smuzhiyun
hcd_pci_runtime_resume(struct device * dev)602*4882a593Smuzhiyun static int hcd_pci_runtime_resume(struct device *dev)
603*4882a593Smuzhiyun {
604*4882a593Smuzhiyun int retval;
605*4882a593Smuzhiyun
606*4882a593Smuzhiyun powermac_set_asic(to_pci_dev(dev), 1);
607*4882a593Smuzhiyun retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
608*4882a593Smuzhiyun dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
609*4882a593Smuzhiyun return retval;
610*4882a593Smuzhiyun }
611*4882a593Smuzhiyun
612*4882a593Smuzhiyun const struct dev_pm_ops usb_hcd_pci_pm_ops = {
613*4882a593Smuzhiyun .suspend = hcd_pci_suspend,
614*4882a593Smuzhiyun .suspend_noirq = hcd_pci_suspend_noirq,
615*4882a593Smuzhiyun .resume_noirq = hcd_pci_resume_noirq,
616*4882a593Smuzhiyun .resume = hcd_pci_resume,
617*4882a593Smuzhiyun .freeze = hcd_pci_suspend,
618*4882a593Smuzhiyun .freeze_noirq = check_root_hub_suspended,
619*4882a593Smuzhiyun .thaw_noirq = NULL,
620*4882a593Smuzhiyun .thaw = hcd_pci_resume,
621*4882a593Smuzhiyun .poweroff = hcd_pci_suspend,
622*4882a593Smuzhiyun .poweroff_noirq = hcd_pci_suspend_noirq,
623*4882a593Smuzhiyun .restore_noirq = hcd_pci_resume_noirq,
624*4882a593Smuzhiyun .restore = hcd_pci_restore,
625*4882a593Smuzhiyun .runtime_suspend = hcd_pci_runtime_suspend,
626*4882a593Smuzhiyun .runtime_resume = hcd_pci_runtime_resume,
627*4882a593Smuzhiyun };
628*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun #endif /* CONFIG_PM */
631