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