Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

USB: improve port transitions when EHCI starts up

It seems to be getting more common recently for EHCI host controllers
to be probed after their companion UHCI or OHCI controllers. This may
be caused partly by splitting the ehci-pci driver out from ehci-hcd,
or it may be caused by changes in the way the kernel does driver
probing.

Regardless, it has a tendency to cause problems. When an EHCI
controller is initialized, it takes ownership of all the ports away
from the companions. In effect, it forcefully disconnects all the USB
devices that may already be using a companion controller.

This patch (as1672b) tries to make the transition more orderly by
deconfiguring the root hubs for all the companion controllers before
initializing the EHCI controller, and reconfiguring them afterward.
The result is a soft disconnect rather than a hard one.

Internally, the patch refactors the code involved in associating EHCI
controllers with their companions. The old approach, in which a
single function is called with an argument telling it what to do (the
companion_action enum), has been replaced with a scheme using multiple
callback functions, each performing a single task.

This patch won't solve all the problems people encounter when their
EHCI controllers start up, but it will at least reduce the number of
error messages generated by the unexpected disconnections.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Tested-by: Jenya Y <jy.gerstmaier@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Alan Stern and committed by
Greg Kroah-Hartman
05768918 4e9c8e5c

+130 -86
+130 -86
drivers/usb/core/hcd-pci.c
··· 37 37 38 38 /* PCI-based HCs are common, but plenty of non-PCI HCs are used too */ 39 39 40 - #ifdef CONFIG_PM_SLEEP 41 - 42 - /* Coordinate handoffs between EHCI and companion controllers 43 - * during system resume 40 + /* 41 + * Coordinate handoffs between EHCI and companion controllers 42 + * during EHCI probing and system resume. 44 43 */ 45 44 46 - static DEFINE_MUTEX(companions_mutex); 45 + static DECLARE_RWSEM(companions_rwsem); 47 46 48 47 #define CL_UHCI PCI_CLASS_SERIAL_USB_UHCI 49 48 #define CL_OHCI PCI_CLASS_SERIAL_USB_OHCI 50 49 #define CL_EHCI PCI_CLASS_SERIAL_USB_EHCI 51 50 52 - enum companion_action { 53 - SET_HS_COMPANION, CLEAR_HS_COMPANION, WAIT_FOR_COMPANIONS 54 - }; 51 + static inline int is_ohci_or_uhci(struct pci_dev *pdev) 52 + { 53 + return pdev->class == CL_OHCI || pdev->class == CL_UHCI; 54 + } 55 55 56 - static void companion_common(struct pci_dev *pdev, struct usb_hcd *hcd, 57 - enum companion_action action) 56 + typedef void (*companion_fn)(struct pci_dev *pdev, struct usb_hcd *hcd, 57 + struct pci_dev *companion, struct usb_hcd *companion_hcd); 58 + 59 + /* Iterate over PCI devices in the same slot as pdev and call fn for each */ 60 + static void for_each_companion(struct pci_dev *pdev, struct usb_hcd *hcd, 61 + companion_fn fn) 58 62 { 59 63 struct pci_dev *companion; 60 64 struct usb_hcd *companion_hcd; 61 65 unsigned int slot = PCI_SLOT(pdev->devfn); 62 66 63 - /* Iterate through other PCI functions in the same slot. 64 - * If pdev is OHCI or UHCI then we are looking for EHCI, and 65 - * vice versa. 67 + /* 68 + * Iterate through other PCI functions in the same slot. 69 + * If the function's drvdata isn't set then it isn't bound to 70 + * a USB host controller driver, so skip it. 66 71 */ 67 72 companion = NULL; 68 73 for_each_pci_dev(companion) { 69 74 if (companion->bus != pdev->bus || 70 75 PCI_SLOT(companion->devfn) != slot) 71 76 continue; 72 - 73 77 companion_hcd = pci_get_drvdata(companion); 74 78 if (!companion_hcd) 75 79 continue; 76 - 77 - /* For SET_HS_COMPANION, store a pointer to the EHCI bus in 78 - * the OHCI/UHCI companion bus structure. 79 - * For CLEAR_HS_COMPANION, clear the pointer to the EHCI bus 80 - * in the OHCI/UHCI companion bus structure. 81 - * For WAIT_FOR_COMPANIONS, wait until the OHCI/UHCI 82 - * companion controllers have fully resumed. 83 - */ 84 - 85 - if ((pdev->class == CL_OHCI || pdev->class == CL_UHCI) && 86 - companion->class == CL_EHCI) { 87 - /* action must be SET_HS_COMPANION */ 88 - dev_dbg(&companion->dev, "HS companion for %s\n", 89 - dev_name(&pdev->dev)); 90 - hcd->self.hs_companion = &companion_hcd->self; 91 - 92 - } else if (pdev->class == CL_EHCI && 93 - (companion->class == CL_OHCI || 94 - companion->class == CL_UHCI)) { 95 - switch (action) { 96 - case SET_HS_COMPANION: 97 - dev_dbg(&pdev->dev, "HS companion for %s\n", 98 - dev_name(&companion->dev)); 99 - companion_hcd->self.hs_companion = &hcd->self; 100 - break; 101 - case CLEAR_HS_COMPANION: 102 - companion_hcd->self.hs_companion = NULL; 103 - break; 104 - case WAIT_FOR_COMPANIONS: 105 - device_pm_wait_for_dev(&pdev->dev, 106 - &companion->dev); 107 - break; 108 - } 109 - } 80 + fn(pdev, hcd, companion, companion_hcd); 110 81 } 111 82 } 112 83 113 - static void set_hs_companion(struct pci_dev *pdev, struct usb_hcd *hcd) 84 + /* 85 + * We're about to add an EHCI controller, which will unceremoniously grab 86 + * all the port connections away from its companions. To prevent annoying 87 + * error messages, lock the companion's root hub and gracefully unconfigure 88 + * it beforehand. Leave it locked until the EHCI controller is all set. 89 + */ 90 + static void ehci_pre_add(struct pci_dev *pdev, struct usb_hcd *hcd, 91 + struct pci_dev *companion, struct usb_hcd *companion_hcd) 114 92 { 115 - mutex_lock(&companions_mutex); 116 - dev_set_drvdata(&pdev->dev, hcd); 117 - companion_common(pdev, hcd, SET_HS_COMPANION); 118 - mutex_unlock(&companions_mutex); 93 + struct usb_device *udev; 94 + 95 + if (is_ohci_or_uhci(companion)) { 96 + udev = companion_hcd->self.root_hub; 97 + usb_lock_device(udev); 98 + usb_set_configuration(udev, 0); 99 + } 119 100 } 120 101 121 - static void clear_hs_companion(struct pci_dev *pdev, struct usb_hcd *hcd) 102 + /* 103 + * Adding the EHCI controller has either succeeded or failed. Set the 104 + * companion pointer accordingly, and in either case, reconfigure and 105 + * unlock the root hub. 106 + */ 107 + static void ehci_post_add(struct pci_dev *pdev, struct usb_hcd *hcd, 108 + struct pci_dev *companion, struct usb_hcd *companion_hcd) 122 109 { 123 - mutex_lock(&companions_mutex); 124 - dev_set_drvdata(&pdev->dev, NULL); 110 + struct usb_device *udev; 125 111 126 - /* If pdev is OHCI or UHCI, just clear its hs_companion pointer */ 127 - if (pdev->class == CL_OHCI || pdev->class == CL_UHCI) 128 - hcd->self.hs_companion = NULL; 129 - 130 - /* Otherwise search for companion buses and clear their pointers */ 131 - else 132 - companion_common(pdev, hcd, CLEAR_HS_COMPANION); 133 - mutex_unlock(&companions_mutex); 112 + if (is_ohci_or_uhci(companion)) { 113 + if (dev_get_drvdata(&pdev->dev)) { /* Succeeded */ 114 + dev_dbg(&pdev->dev, "HS companion for %s\n", 115 + dev_name(&companion->dev)); 116 + companion_hcd->self.hs_companion = &hcd->self; 117 + } 118 + udev = companion_hcd->self.root_hub; 119 + usb_set_configuration(udev, 1); 120 + usb_unlock_device(udev); 121 + } 134 122 } 135 123 136 - static void wait_for_companions(struct pci_dev *pdev, struct usb_hcd *hcd) 124 + /* 125 + * We just added a non-EHCI controller. Find the EHCI controller to 126 + * which it is a companion, and store a pointer to the bus structure. 127 + */ 128 + static void non_ehci_add(struct pci_dev *pdev, struct usb_hcd *hcd, 129 + struct pci_dev *companion, struct usb_hcd *companion_hcd) 137 130 { 138 - /* Only EHCI controllers need to wait. 139 - * No locking is needed because a controller cannot be resumed 140 - * while one of its companions is getting unbound. 141 - */ 142 - if (pdev->class == CL_EHCI) 143 - companion_common(pdev, hcd, WAIT_FOR_COMPANIONS); 131 + if (is_ohci_or_uhci(pdev) && companion->class == CL_EHCI) { 132 + dev_dbg(&pdev->dev, "FS/LS companion for %s\n", 133 + dev_name(&companion->dev)); 134 + hcd->self.hs_companion = &companion_hcd->self; 135 + } 144 136 } 145 137 146 - #else /* !CONFIG_PM_SLEEP */ 138 + /* We are removing an EHCI controller. Clear the companions' pointers. */ 139 + static void ehci_remove(struct pci_dev *pdev, struct usb_hcd *hcd, 140 + struct pci_dev *companion, struct usb_hcd *companion_hcd) 141 + { 142 + if (is_ohci_or_uhci(companion)) 143 + companion_hcd->self.hs_companion = NULL; 144 + } 147 145 148 - static inline void set_hs_companion(struct pci_dev *d, struct usb_hcd *h) {} 149 - static inline void clear_hs_companion(struct pci_dev *d, struct usb_hcd *h) {} 150 - static inline void wait_for_companions(struct pci_dev *d, struct usb_hcd *h) {} 146 + #ifdef CONFIG_PM 151 147 152 - #endif /* !CONFIG_PM_SLEEP */ 148 + /* An EHCI controller must wait for its companions before resuming. */ 149 + static void ehci_wait_for_companions(struct pci_dev *pdev, struct usb_hcd *hcd, 150 + struct pci_dev *companion, struct usb_hcd *companion_hcd) 151 + { 152 + if (is_ohci_or_uhci(companion)) 153 + device_pm_wait_for_dev(&pdev->dev, &companion->dev); 154 + } 155 + 156 + #endif /* CONFIG_PM */ 153 157 154 158 /*-------------------------------------------------------------------------*/ 155 159 ··· 221 217 driver->description)) { 222 218 dev_dbg(&dev->dev, "controller already in use\n"); 223 219 retval = -EBUSY; 224 - goto clear_companion; 220 + goto put_hcd; 225 221 } 226 222 hcd->regs = ioremap_nocache(hcd->rsrc_start, hcd->rsrc_len); 227 223 if (hcd->regs == NULL) { ··· 248 244 if (region == PCI_ROM_RESOURCE) { 249 245 dev_dbg(&dev->dev, "no i/o regions available\n"); 250 246 retval = -EBUSY; 251 - goto clear_companion; 247 + goto put_hcd; 252 248 } 253 249 } 254 250 255 251 pci_set_master(dev); 256 252 257 - retval = usb_add_hcd(hcd, hcd_irq, IRQF_SHARED); 253 + /* Note: dev_set_drvdata must be called while holding the rwsem */ 254 + if (dev->class == CL_EHCI) { 255 + down_write(&companions_rwsem); 256 + dev_set_drvdata(&dev->dev, hcd); 257 + for_each_companion(dev, hcd, ehci_pre_add); 258 + retval = usb_add_hcd(hcd, hcd_irq, IRQF_SHARED); 259 + if (retval != 0) 260 + dev_set_drvdata(&dev->dev, NULL); 261 + for_each_companion(dev, hcd, ehci_post_add); 262 + up_write(&companions_rwsem); 263 + } else { 264 + down_read(&companions_rwsem); 265 + dev_set_drvdata(&dev->dev, hcd); 266 + retval = usb_add_hcd(hcd, hcd_irq, IRQF_SHARED); 267 + if (retval != 0) 268 + dev_set_drvdata(&dev->dev, NULL); 269 + else 270 + for_each_companion(dev, hcd, non_ehci_add); 271 + up_read(&companions_rwsem); 272 + } 273 + 258 274 if (retval != 0) 259 275 goto unmap_registers; 260 - set_hs_companion(dev, hcd); 261 276 262 277 if (pci_dev_run_wake(dev)) 263 278 pm_runtime_put_noidle(&dev->dev); ··· 289 266 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 290 267 } else 291 268 release_region(hcd->rsrc_start, hcd->rsrc_len); 292 - clear_companion: 293 - clear_hs_companion(dev, hcd); 269 + put_hcd: 294 270 usb_put_hcd(hcd); 295 271 disable_pci: 296 272 pci_disable_device(dev); ··· 332 310 usb_hcd_irq(0, hcd); 333 311 local_irq_enable(); 334 312 335 - usb_remove_hcd(hcd); 313 + /* Note: dev_set_drvdata must be called while holding the rwsem */ 314 + if (dev->class == CL_EHCI) { 315 + down_write(&companions_rwsem); 316 + for_each_companion(dev, hcd, ehci_remove); 317 + usb_remove_hcd(hcd); 318 + dev_set_drvdata(&dev->dev, NULL); 319 + up_write(&companions_rwsem); 320 + } else { 321 + /* Not EHCI; just clear the companion pointer */ 322 + down_read(&companions_rwsem); 323 + hcd->self.hs_companion = NULL; 324 + usb_remove_hcd(hcd); 325 + dev_set_drvdata(&dev->dev, NULL); 326 + up_read(&companions_rwsem); 327 + } 328 + 336 329 if (hcd->driver->flags & HCD_MEMORY) { 337 330 iounmap(hcd->regs); 338 331 release_mem_region(hcd->rsrc_start, hcd->rsrc_len); 339 332 } else { 340 333 release_region(hcd->rsrc_start, hcd->rsrc_len); 341 334 } 342 - clear_hs_companion(dev, hcd); 335 + 343 336 usb_put_hcd(hcd); 344 337 pci_disable_device(dev); 345 338 } ··· 500 463 pci_set_master(pci_dev); 501 464 502 465 if (hcd->driver->pci_resume && !HCD_DEAD(hcd)) { 503 - if (event != PM_EVENT_AUTO_RESUME) 504 - wait_for_companions(pci_dev, hcd); 466 + 467 + /* 468 + * Only EHCI controllers have to wait for their companions. 469 + * No locking is needed because PCI controller drivers do not 470 + * get unbound during system resume. 471 + */ 472 + if (pci_dev->class == CL_EHCI && event != PM_EVENT_AUTO_RESUME) 473 + for_each_companion(pci_dev, hcd, 474 + ehci_wait_for_companions); 505 475 506 476 retval = hcd->driver->pci_resume(hcd, 507 477 event == PM_EVENT_RESTORE);