xref: /linux/drivers/usb/core/hcd-pci.c (revision dec1c62e91ba268ab2a6e339d4d7a59287d5eba1)
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 #endif
19 
20 #include "usb.h"
21 
22 
23 /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */
24 
25 /*
26  * Coordinate handoffs between EHCI and companion controllers
27  * during EHCI probing and system resume.
28  */
29 
30 static DECLARE_RWSEM(companions_rwsem);
31 
32 #define CL_UHCI		PCI_CLASS_SERIAL_USB_UHCI
33 #define CL_OHCI		PCI_CLASS_SERIAL_USB_OHCI
34 #define CL_EHCI		PCI_CLASS_SERIAL_USB_EHCI
35 
36 static inline int is_ohci_or_uhci(struct pci_dev *pdev)
37 {
38 	return pdev->class == CL_OHCI || pdev->class == CL_UHCI;
39 }
40 
41 typedef void (*companion_fn)(struct pci_dev *pdev, struct usb_hcd *hcd,
42 		struct pci_dev *companion, struct usb_hcd *companion_hcd);
43 
44 /* Iterate over PCI devices in the same slot as pdev and call fn for each */
45 static void for_each_companion(struct pci_dev *pdev, struct usb_hcd *hcd,
46 		companion_fn fn)
47 {
48 	struct pci_dev		*companion;
49 	struct usb_hcd		*companion_hcd;
50 	unsigned int		slot = PCI_SLOT(pdev->devfn);
51 
52 	/*
53 	 * Iterate through other PCI functions in the same slot.
54 	 * If the function's drvdata isn't set then it isn't bound to
55 	 * a USB host controller driver, so skip it.
56 	 */
57 	companion = NULL;
58 	for_each_pci_dev(companion) {
59 		if (companion->bus != pdev->bus ||
60 				PCI_SLOT(companion->devfn) != slot)
61 			continue;
62 
63 		/*
64 		 * Companion device should be either UHCI,OHCI or EHCI host
65 		 * controller, otherwise skip.
66 		 */
67 		if (companion->class != CL_UHCI && companion->class != CL_OHCI &&
68 				companion->class != CL_EHCI)
69 			continue;
70 
71 		companion_hcd = pci_get_drvdata(companion);
72 		if (!companion_hcd || !companion_hcd->self.root_hub)
73 			continue;
74 		fn(pdev, hcd, companion, companion_hcd);
75 	}
76 }
77 
78 /*
79  * We're about to add an EHCI controller, which will unceremoniously grab
80  * all the port connections away from its companions.  To prevent annoying
81  * error messages, lock the companion's root hub and gracefully unconfigure
82  * it beforehand.  Leave it locked until the EHCI controller is all set.
83  */
84 static void ehci_pre_add(struct pci_dev *pdev, struct usb_hcd *hcd,
85 		struct pci_dev *companion, struct usb_hcd *companion_hcd)
86 {
87 	struct usb_device *udev;
88 
89 	if (is_ohci_or_uhci(companion)) {
90 		udev = companion_hcd->self.root_hub;
91 		usb_lock_device(udev);
92 		usb_set_configuration(udev, 0);
93 	}
94 }
95 
96 /*
97  * Adding the EHCI controller has either succeeded or failed.  Set the
98  * companion pointer accordingly, and in either case, reconfigure and
99  * unlock the root hub.
100  */
101 static void ehci_post_add(struct pci_dev *pdev, struct usb_hcd *hcd,
102 		struct pci_dev *companion, struct usb_hcd *companion_hcd)
103 {
104 	struct usb_device *udev;
105 
106 	if (is_ohci_or_uhci(companion)) {
107 		if (dev_get_drvdata(&pdev->dev)) {	/* Succeeded */
108 			dev_dbg(&pdev->dev, "HS companion for %s\n",
109 					dev_name(&companion->dev));
110 			companion_hcd->self.hs_companion = &hcd->self;
111 		}
112 		udev = companion_hcd->self.root_hub;
113 		usb_set_configuration(udev, 1);
114 		usb_unlock_device(udev);
115 	}
116 }
117 
118 /*
119  * We just added a non-EHCI controller.  Find the EHCI controller to
120  * which it is a companion, and store a pointer to the bus structure.
121  */
122 static void non_ehci_add(struct pci_dev *pdev, struct usb_hcd *hcd,
123 		struct pci_dev *companion, struct usb_hcd *companion_hcd)
124 {
125 	if (is_ohci_or_uhci(pdev) && companion->class == CL_EHCI) {
126 		dev_dbg(&pdev->dev, "FS/LS companion for %s\n",
127 				dev_name(&companion->dev));
128 		hcd->self.hs_companion = &companion_hcd->self;
129 	}
130 }
131 
132 /* We are removing an EHCI controller.  Clear the companions' pointers. */
133 static void ehci_remove(struct pci_dev *pdev, struct usb_hcd *hcd,
134 		struct pci_dev *companion, struct usb_hcd *companion_hcd)
135 {
136 	if (is_ohci_or_uhci(companion))
137 		companion_hcd->self.hs_companion = NULL;
138 }
139 
140 #ifdef	CONFIG_PM
141 
142 /* An EHCI controller must wait for its companions before resuming. */
143 static void ehci_wait_for_companions(struct pci_dev *pdev, struct usb_hcd *hcd,
144 		struct pci_dev *companion, struct usb_hcd *companion_hcd)
145 {
146 	if (is_ohci_or_uhci(companion))
147 		device_pm_wait_for_dev(&pdev->dev, &companion->dev);
148 }
149 
150 #endif	/* CONFIG_PM */
151 
152 /*-------------------------------------------------------------------------*/
153 
154 /* configure so an HC device and id are always provided */
155 /* always called with process context; sleeping is OK */
156 
157 /**
158  * usb_hcd_pci_probe - initialize PCI-based HCDs
159  * @dev: USB Host Controller being probed
160  * @id: pci hotplug id connecting controller to HCD framework
161  * @driver: USB HC driver handle
162  *
163  * Context: task context, might sleep
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  */
173 int 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_STD_NUM_BARS) {
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 
288 put_hcd:
289 	usb_put_hcd(hcd);
290 free_irq_vectors:
291 	if ((driver->flags & HCD_MASK) < HCD_USB3)
292 		pci_free_irq_vectors(dev);
293 disable_pci:
294 	pci_disable_device(dev);
295 	dev_err(&dev->dev, "init %s fail, %d\n", pci_name(dev), retval);
296 	return retval;
297 }
298 EXPORT_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  *
308  * Context: task context, might sleep
309  *
310  * Reverses the effect of usb_hcd_pci_probe(), first invoking
311  * the HCD's stop() method.  It is always called from a thread
312  * context, normally "rmmod", "apmd", or something similar.
313  *
314  * Store this function in the HCD's struct pci_driver as remove().
315  */
316 void usb_hcd_pci_remove(struct pci_dev *dev)
317 {
318 	struct usb_hcd		*hcd;
319 	int			hcd_driver_flags;
320 
321 	hcd = pci_get_drvdata(dev);
322 	if (!hcd)
323 		return;
324 
325 	hcd_driver_flags = hcd->driver->flags;
326 
327 	if (pci_dev_run_wake(dev))
328 		pm_runtime_get_noresume(&dev->dev);
329 
330 	/* Fake an interrupt request in order to give the driver a chance
331 	 * to test whether the controller hardware has been removed (e.g.,
332 	 * cardbus physical eject).
333 	 */
334 	local_irq_disable();
335 	usb_hcd_irq(0, hcd);
336 	local_irq_enable();
337 
338 	/* Note: dev_set_drvdata must be called while holding the rwsem */
339 	if (dev->class == CL_EHCI) {
340 		down_write(&companions_rwsem);
341 		for_each_companion(dev, hcd, ehci_remove);
342 		usb_remove_hcd(hcd);
343 		dev_set_drvdata(&dev->dev, NULL);
344 		up_write(&companions_rwsem);
345 	} else {
346 		/* Not EHCI; just clear the companion pointer */
347 		down_read(&companions_rwsem);
348 		hcd->self.hs_companion = NULL;
349 		usb_remove_hcd(hcd);
350 		dev_set_drvdata(&dev->dev, NULL);
351 		up_read(&companions_rwsem);
352 	}
353 	usb_put_hcd(hcd);
354 	if ((hcd_driver_flags & HCD_MASK) < HCD_USB3)
355 		pci_free_irq_vectors(dev);
356 	pci_disable_device(dev);
357 }
358 EXPORT_SYMBOL_GPL(usb_hcd_pci_remove);
359 
360 /**
361  * usb_hcd_pci_shutdown - shutdown host controller
362  * @dev: USB Host Controller being shutdown
363  */
364 void usb_hcd_pci_shutdown(struct pci_dev *dev)
365 {
366 	struct usb_hcd		*hcd;
367 
368 	hcd = pci_get_drvdata(dev);
369 	if (!hcd)
370 		return;
371 
372 	if (test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags) &&
373 			hcd->driver->shutdown) {
374 		hcd->driver->shutdown(hcd);
375 		if (usb_hcd_is_primary_hcd(hcd) && hcd->irq > 0)
376 			free_irq(hcd->irq, hcd);
377 		pci_disable_device(dev);
378 	}
379 }
380 EXPORT_SYMBOL_GPL(usb_hcd_pci_shutdown);
381 
382 #ifdef	CONFIG_PM
383 
384 #ifdef	CONFIG_PPC_PMAC
385 static void powermac_set_asic(struct pci_dev *pci_dev, int enable)
386 {
387 	/* Enanble or disable ASIC clocks for USB */
388 	if (machine_is(powermac)) {
389 		struct device_node	*of_node;
390 
391 		of_node = pci_device_to_OF_node(pci_dev);
392 		if (of_node)
393 			pmac_call_feature(PMAC_FTR_USB_ENABLE,
394 					of_node, 0, enable);
395 	}
396 }
397 
398 #else
399 
400 static inline void powermac_set_asic(struct pci_dev *pci_dev, int enable)
401 {}
402 
403 #endif	/* CONFIG_PPC_PMAC */
404 
405 static int check_root_hub_suspended(struct device *dev)
406 {
407 	struct usb_hcd		*hcd = dev_get_drvdata(dev);
408 
409 	if (HCD_RH_RUNNING(hcd)) {
410 		dev_warn(dev, "Root hub is not suspended\n");
411 		return -EBUSY;
412 	}
413 	if (hcd->shared_hcd) {
414 		hcd = hcd->shared_hcd;
415 		if (HCD_RH_RUNNING(hcd)) {
416 			dev_warn(dev, "Secondary root hub is not suspended\n");
417 			return -EBUSY;
418 		}
419 	}
420 	return 0;
421 }
422 
423 static int suspend_common(struct device *dev, bool do_wakeup)
424 {
425 	struct pci_dev		*pci_dev = to_pci_dev(dev);
426 	struct usb_hcd		*hcd = pci_get_drvdata(pci_dev);
427 	int			retval;
428 
429 	/* Root hub suspend should have stopped all downstream traffic,
430 	 * and all bus master traffic.  And done so for both the interface
431 	 * and the stub usb_device (which we check here).  But maybe it
432 	 * didn't; writing sysfs power/state files ignores such rules...
433 	 */
434 	retval = check_root_hub_suspended(dev);
435 	if (retval)
436 		return retval;
437 
438 	if (hcd->driver->pci_suspend && !HCD_DEAD(hcd)) {
439 		/* Optimization: Don't suspend if a root-hub wakeup is
440 		 * pending and it would cause the HCD to wake up anyway.
441 		 */
442 		if (do_wakeup && HCD_WAKEUP_PENDING(hcd))
443 			return -EBUSY;
444 		if (do_wakeup && hcd->shared_hcd &&
445 				HCD_WAKEUP_PENDING(hcd->shared_hcd))
446 			return -EBUSY;
447 		retval = hcd->driver->pci_suspend(hcd, do_wakeup);
448 		suspend_report_result(dev, hcd->driver->pci_suspend, retval);
449 
450 		/* Check again in case wakeup raced with pci_suspend */
451 		if ((retval == 0 && do_wakeup && HCD_WAKEUP_PENDING(hcd)) ||
452 				(retval == 0 && do_wakeup && hcd->shared_hcd &&
453 				 HCD_WAKEUP_PENDING(hcd->shared_hcd))) {
454 			if (hcd->driver->pci_resume)
455 				hcd->driver->pci_resume(hcd, false);
456 			retval = -EBUSY;
457 		}
458 		if (retval)
459 			return retval;
460 	}
461 
462 	/* If MSI-X is enabled, the driver will have synchronized all vectors
463 	 * in pci_suspend(). If MSI or legacy PCI is enabled, that will be
464 	 * synchronized here.
465 	 */
466 	if (!hcd->msix_enabled)
467 		synchronize_irq(pci_irq_vector(pci_dev, 0));
468 
469 	/* Downstream ports from this root hub should already be quiesced, so
470 	 * there will be no DMA activity.  Now we can shut down the upstream
471 	 * link (except maybe for PME# resume signaling).  We'll enter a
472 	 * low power state during suspend_noirq, if the hardware allows.
473 	 */
474 	pci_disable_device(pci_dev);
475 	return retval;
476 }
477 
478 static int resume_common(struct device *dev, int event)
479 {
480 	struct pci_dev		*pci_dev = to_pci_dev(dev);
481 	struct usb_hcd		*hcd = pci_get_drvdata(pci_dev);
482 	int			retval;
483 
484 	if (HCD_RH_RUNNING(hcd) ||
485 			(hcd->shared_hcd &&
486 			 HCD_RH_RUNNING(hcd->shared_hcd))) {
487 		dev_dbg(dev, "can't resume, not suspended!\n");
488 		return 0;
489 	}
490 
491 	retval = pci_enable_device(pci_dev);
492 	if (retval < 0) {
493 		dev_err(dev, "can't re-enable after resume, %d!\n", retval);
494 		return retval;
495 	}
496 
497 	pci_set_master(pci_dev);
498 
499 	if (hcd->driver->pci_resume && !HCD_DEAD(hcd)) {
500 
501 		/*
502 		 * Only EHCI controllers have to wait for their companions.
503 		 * No locking is needed because PCI controller drivers do not
504 		 * get unbound during system resume.
505 		 */
506 		if (pci_dev->class == CL_EHCI && event != PM_EVENT_AUTO_RESUME)
507 			for_each_companion(pci_dev, hcd,
508 					ehci_wait_for_companions);
509 
510 		retval = hcd->driver->pci_resume(hcd,
511 				event == PM_EVENT_RESTORE);
512 		if (retval) {
513 			dev_err(dev, "PCI post-resume error %d!\n", retval);
514 			usb_hc_died(hcd);
515 		}
516 	}
517 	return retval;
518 }
519 
520 #ifdef	CONFIG_PM_SLEEP
521 
522 static int hcd_pci_suspend(struct device *dev)
523 {
524 	return suspend_common(dev, device_may_wakeup(dev));
525 }
526 
527 static int hcd_pci_suspend_noirq(struct device *dev)
528 {
529 	struct pci_dev		*pci_dev = to_pci_dev(dev);
530 	struct usb_hcd		*hcd = pci_get_drvdata(pci_dev);
531 	int			retval;
532 
533 	retval = check_root_hub_suspended(dev);
534 	if (retval)
535 		return retval;
536 
537 	pci_save_state(pci_dev);
538 
539 	/* If the root hub is dead rather than suspended, disallow remote
540 	 * wakeup.  usb_hc_died() should ensure that both hosts are marked as
541 	 * dying, so we only need to check the primary roothub.
542 	 */
543 	if (HCD_DEAD(hcd))
544 		device_set_wakeup_enable(dev, 0);
545 	dev_dbg(dev, "wakeup: %d\n", device_may_wakeup(dev));
546 
547 	/* Possibly enable remote wakeup,
548 	 * choose the appropriate low-power state, and go to that state.
549 	 */
550 	retval = pci_prepare_to_sleep(pci_dev);
551 	if (retval == -EIO) {		/* Low-power not supported */
552 		dev_dbg(dev, "--> PCI D0 legacy\n");
553 		retval = 0;
554 	} else if (retval == 0) {
555 		dev_dbg(dev, "--> PCI %s\n",
556 				pci_power_name(pci_dev->current_state));
557 	} else {
558 		suspend_report_result(dev, pci_prepare_to_sleep, retval);
559 		return retval;
560 	}
561 
562 	powermac_set_asic(pci_dev, 0);
563 	return retval;
564 }
565 
566 static int hcd_pci_resume_noirq(struct device *dev)
567 {
568 	powermac_set_asic(to_pci_dev(dev), 1);
569 	return 0;
570 }
571 
572 static int hcd_pci_resume(struct device *dev)
573 {
574 	return resume_common(dev, PM_EVENT_RESUME);
575 }
576 
577 static int hcd_pci_restore(struct device *dev)
578 {
579 	return resume_common(dev, PM_EVENT_RESTORE);
580 }
581 
582 #else
583 
584 #define hcd_pci_suspend		NULL
585 #define hcd_pci_suspend_noirq	NULL
586 #define hcd_pci_resume_noirq	NULL
587 #define hcd_pci_resume		NULL
588 #define hcd_pci_restore		NULL
589 
590 #endif	/* CONFIG_PM_SLEEP */
591 
592 static int hcd_pci_runtime_suspend(struct device *dev)
593 {
594 	int	retval;
595 
596 	retval = suspend_common(dev, true);
597 	if (retval == 0)
598 		powermac_set_asic(to_pci_dev(dev), 0);
599 	dev_dbg(dev, "hcd_pci_runtime_suspend: %d\n", retval);
600 	return retval;
601 }
602 
603 static int hcd_pci_runtime_resume(struct device *dev)
604 {
605 	int	retval;
606 
607 	powermac_set_asic(to_pci_dev(dev), 1);
608 	retval = resume_common(dev, PM_EVENT_AUTO_RESUME);
609 	dev_dbg(dev, "hcd_pci_runtime_resume: %d\n", retval);
610 	return retval;
611 }
612 
613 const struct dev_pm_ops usb_hcd_pci_pm_ops = {
614 	.suspend	= hcd_pci_suspend,
615 	.suspend_noirq	= hcd_pci_suspend_noirq,
616 	.resume_noirq	= hcd_pci_resume_noirq,
617 	.resume		= hcd_pci_resume,
618 	.freeze		= hcd_pci_suspend,
619 	.freeze_noirq	= check_root_hub_suspended,
620 	.thaw_noirq	= NULL,
621 	.thaw		= hcd_pci_resume,
622 	.poweroff	= hcd_pci_suspend,
623 	.poweroff_noirq	= hcd_pci_suspend_noirq,
624 	.restore_noirq	= hcd_pci_resume_noirq,
625 	.restore	= hcd_pci_restore,
626 	.runtime_suspend = hcd_pci_runtime_suspend,
627 	.runtime_resume	= hcd_pci_runtime_resume,
628 };
629 EXPORT_SYMBOL_GPL(usb_hcd_pci_pm_ops);
630 
631 #endif	/* CONFIG_PM */
632