Lines Matching +full:usb +full:- +full:hub
1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for onboard USB hubs
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/usb/onboard_hub.h>
52 static int onboard_hub_power_on(struct onboard_hub *hub) in onboard_hub_power_on() argument
56 err = regulator_enable(hub->vdd); in onboard_hub_power_on()
58 dev_err(hub->dev, "failed to enable regulator: %d\n", err); in onboard_hub_power_on()
62 fsleep(hub->pdata->reset_us); in onboard_hub_power_on()
63 gpiod_set_value_cansleep(hub->reset_gpio, 0); in onboard_hub_power_on()
65 hub->is_powered_on = true; in onboard_hub_power_on()
70 static int onboard_hub_power_off(struct onboard_hub *hub) in onboard_hub_power_off() argument
74 gpiod_set_value_cansleep(hub->reset_gpio, 1); in onboard_hub_power_off()
76 err = regulator_disable(hub->vdd); in onboard_hub_power_off()
78 dev_err(hub->dev, "failed to disable regulator: %d\n", err); in onboard_hub_power_off()
82 hub->is_powered_on = false; in onboard_hub_power_off()
89 struct onboard_hub *hub = dev_get_drvdata(dev); in onboard_hub_suspend() local
93 if (hub->always_powered_in_suspend) in onboard_hub_suspend()
96 mutex_lock(&hub->lock); in onboard_hub_suspend()
98 list_for_each_entry(node, &hub->udev_list, list) { in onboard_hub_suspend()
99 if (!device_may_wakeup(node->udev->bus->controller)) in onboard_hub_suspend()
102 if (usb_wakeup_enabled_descendants(node->udev)) { in onboard_hub_suspend()
108 mutex_unlock(&hub->lock); in onboard_hub_suspend()
113 return onboard_hub_power_off(hub); in onboard_hub_suspend()
118 struct onboard_hub *hub = dev_get_drvdata(dev); in onboard_hub_resume() local
120 if (hub->is_powered_on) in onboard_hub_resume()
123 return onboard_hub_power_on(hub); in onboard_hub_resume()
128 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev)); in get_udev_link_name()
131 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev) in onboard_hub_add_usbdev() argument
137 mutex_lock(&hub->lock); in onboard_hub_add_usbdev()
139 if (hub->going_away) { in onboard_hub_add_usbdev()
140 err = -EINVAL; in onboard_hub_add_usbdev()
146 err = -ENOMEM; in onboard_hub_add_usbdev()
150 node->udev = udev; in onboard_hub_add_usbdev()
152 list_add(&node->list, &hub->udev_list); in onboard_hub_add_usbdev()
154 mutex_unlock(&hub->lock); in onboard_hub_add_usbdev()
157 WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name)); in onboard_hub_add_usbdev()
162 mutex_unlock(&hub->lock); in onboard_hub_add_usbdev()
167 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev) in onboard_hub_remove_usbdev() argument
173 sysfs_remove_link(&hub->dev->kobj, link_name); in onboard_hub_remove_usbdev()
175 mutex_lock(&hub->lock); in onboard_hub_remove_usbdev()
177 list_for_each_entry(node, &hub->udev_list, list) { in onboard_hub_remove_usbdev()
178 if (node->udev == udev) { in onboard_hub_remove_usbdev()
179 list_del(&node->list); in onboard_hub_remove_usbdev()
185 mutex_unlock(&hub->lock); in onboard_hub_remove_usbdev()
191 const struct onboard_hub *hub = dev_get_drvdata(dev); in always_powered_in_suspend_show() local
193 return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend); in always_powered_in_suspend_show()
199 struct onboard_hub *hub = dev_get_drvdata(dev); in always_powered_in_suspend_store() local
207 hub->always_powered_in_suspend = val; in always_powered_in_suspend_store()
225 pr_err("Failed to attach USB driver: %d\n", err); in onboard_hub_attach_usb_driver()
231 struct device *dev = &pdev->dev; in onboard_hub_probe()
232 struct onboard_hub *hub; in onboard_hub_probe() local
235 hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL); in onboard_hub_probe()
236 if (!hub) in onboard_hub_probe()
237 return -ENOMEM; in onboard_hub_probe()
239 of_id = of_match_device(onboard_hub_match, &pdev->dev); in onboard_hub_probe()
241 return -ENODEV; in onboard_hub_probe()
243 hub->pdata = of_id->data; in onboard_hub_probe()
244 if (!hub->pdata) in onboard_hub_probe()
245 return -EINVAL; in onboard_hub_probe()
247 hub->vdd = devm_regulator_get(dev, "vdd"); in onboard_hub_probe()
248 if (IS_ERR(hub->vdd)) in onboard_hub_probe()
249 return PTR_ERR(hub->vdd); in onboard_hub_probe()
251 hub->reset_gpio = devm_gpiod_get_optional(dev, "reset", in onboard_hub_probe()
253 if (IS_ERR(hub->reset_gpio)) in onboard_hub_probe()
254 return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n"); in onboard_hub_probe()
256 hub->dev = dev; in onboard_hub_probe()
257 mutex_init(&hub->lock); in onboard_hub_probe()
258 INIT_LIST_HEAD(&hub->udev_list); in onboard_hub_probe()
260 dev_set_drvdata(dev, hub); in onboard_hub_probe()
262 err = onboard_hub_power_on(hub); in onboard_hub_probe()
267 * The USB driver might have been detached from the USB devices by in onboard_hub_probe()
269 * make sure to re-attach it if needed. in onboard_hub_probe()
271 * This needs to be done deferred to avoid self-deadlocks on systems in onboard_hub_probe()
274 INIT_WORK(&hub->attach_usb_driver_work, onboard_hub_attach_usb_driver); in onboard_hub_probe()
275 schedule_work(&hub->attach_usb_driver_work); in onboard_hub_probe()
282 struct onboard_hub *hub = dev_get_drvdata(&pdev->dev); in onboard_hub_remove() local
286 hub->going_away = true; in onboard_hub_remove()
288 if (&hub->attach_usb_driver_work != current_work()) in onboard_hub_remove()
289 cancel_work_sync(&hub->attach_usb_driver_work); in onboard_hub_remove()
291 mutex_lock(&hub->lock); in onboard_hub_remove()
293 /* unbind the USB devices to avoid dangling references to this device */ in onboard_hub_remove()
294 while (!list_empty(&hub->udev_list)) { in onboard_hub_remove()
295 node = list_first_entry(&hub->udev_list, struct usbdev_node, list); in onboard_hub_remove()
296 udev = node->udev; in onboard_hub_remove()
300 * which acquires hub->lock. We must release the lock first. in onboard_hub_remove()
302 get_device(&udev->dev); in onboard_hub_remove()
303 mutex_unlock(&hub->lock); in onboard_hub_remove()
304 device_release_driver(&udev->dev); in onboard_hub_remove()
305 put_device(&udev->dev); in onboard_hub_remove()
306 mutex_lock(&hub->lock); in onboard_hub_remove()
309 mutex_unlock(&hub->lock); in onboard_hub_remove()
311 return onboard_hub_power_off(hub); in onboard_hub_remove()
325 .name = "onboard-usb-hub",
332 /************************** USB driver **************************/
339 * Returns the onboard_hub platform device that is associated with the USB
346 struct onboard_hub *hub; in _find_onboard_hub() local
348 pdev = of_find_device_by_node(dev->of_node); in _find_onboard_hub()
350 np = of_parse_phandle(dev->of_node, "peer-hub", 0); in _find_onboard_hub()
352 dev_err(dev, "failed to find device node for peer hub\n"); in _find_onboard_hub()
353 return ERR_PTR(-EINVAL); in _find_onboard_hub()
360 return ERR_PTR(-ENODEV); in _find_onboard_hub()
363 hub = dev_get_drvdata(&pdev->dev); in _find_onboard_hub()
364 put_device(&pdev->dev); in _find_onboard_hub()
367 * The presence of drvdata ('hub') indicates that the platform driver in _find_onboard_hub()
373 if (!hub) in _find_onboard_hub()
374 return ERR_PTR(-EPROBE_DEFER); in _find_onboard_hub()
376 return hub; in _find_onboard_hub()
381 struct device *dev = &udev->dev; in onboard_hub_usbdev_probe()
382 struct onboard_hub *hub; in onboard_hub_usbdev_probe() local
386 if (!dev->of_node) in onboard_hub_usbdev_probe()
387 return -ENODEV; in onboard_hub_usbdev_probe()
389 hub = _find_onboard_hub(dev); in onboard_hub_usbdev_probe()
390 if (IS_ERR(hub)) in onboard_hub_usbdev_probe()
391 return PTR_ERR(hub); in onboard_hub_usbdev_probe()
393 dev_set_drvdata(dev, hub); in onboard_hub_usbdev_probe()
395 err = onboard_hub_add_usbdev(hub, udev); in onboard_hub_usbdev_probe()
404 struct onboard_hub *hub = dev_get_drvdata(&udev->dev); in onboard_hub_usbdev_disconnect() local
406 onboard_hub_remove_usbdev(hub, udev); in onboard_hub_usbdev_disconnect()
410 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 */
411 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
412 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
413 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
414 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
419 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
422 .name = "onboard-usb-hub",
454 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");