1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for onboard USB hubs
5 * Copyright (c) 2022, Google LLC
8 #include <linux/device.h>
9 #include <linux/export.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/of_platform.h>
17 #include <linux/platform_device.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/suspend.h>
20 #include <linux/sysfs.h>
21 #include <linux/usb.h>
22 #include <linux/usb/hcd.h>
23 #include <linux/usb/of.h>
24 #include <linux/usb/onboard_hub.h>
26 static struct usb_device_driver onboard_hub_usbdev_driver;
28 /************************** Platform driver **************************/
31 struct usb_device *udev;
32 struct list_head list;
36 struct regulator *vdd;
38 bool always_powered_in_suspend;
41 struct list_head udev_list;
45 static int onboard_hub_power_on(struct onboard_hub *hub)
49 err = regulator_enable(hub->vdd);
51 dev_err(hub->dev, "failed to enable regulator: %d\n", err);
55 hub->is_powered_on = true;
60 static int onboard_hub_power_off(struct onboard_hub *hub)
64 err = regulator_disable(hub->vdd);
66 dev_err(hub->dev, "failed to disable regulator: %d\n", err);
70 hub->is_powered_on = false;
75 static int __maybe_unused onboard_hub_suspend(struct device *dev)
77 struct onboard_hub *hub = dev_get_drvdata(dev);
78 struct usbdev_node *node;
79 bool power_off = true;
81 if (hub->always_powered_in_suspend)
84 mutex_lock(&hub->lock);
86 list_for_each_entry(node, &hub->udev_list, list) {
87 if (!device_may_wakeup(node->udev->bus->controller))
90 if (usb_wakeup_enabled_descendants(node->udev)) {
96 mutex_unlock(&hub->lock);
101 return onboard_hub_power_off(hub);
104 static int __maybe_unused onboard_hub_resume(struct device *dev)
106 struct onboard_hub *hub = dev_get_drvdata(dev);
108 if (hub->is_powered_on)
111 return onboard_hub_power_on(hub);
114 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
116 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
119 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
121 struct usbdev_node *node;
125 mutex_lock(&hub->lock);
127 if (hub->going_away) {
132 node = kzalloc(sizeof(*node), GFP_KERNEL);
140 list_add(&node->list, &hub->udev_list);
142 mutex_unlock(&hub->lock);
144 get_udev_link_name(udev, link_name, sizeof(link_name));
145 WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name));
150 mutex_unlock(&hub->lock);
155 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
157 struct usbdev_node *node;
160 get_udev_link_name(udev, link_name, sizeof(link_name));
161 sysfs_remove_link(&hub->dev->kobj, link_name);
163 mutex_lock(&hub->lock);
165 list_for_each_entry(node, &hub->udev_list, list) {
166 if (node->udev == udev) {
167 list_del(&node->list);
173 mutex_unlock(&hub->lock);
176 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
179 const struct onboard_hub *hub = dev_get_drvdata(dev);
181 return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
184 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
185 const char *buf, size_t count)
187 struct onboard_hub *hub = dev_get_drvdata(dev);
191 ret = kstrtobool(buf, &val);
195 hub->always_powered_in_suspend = val;
199 static DEVICE_ATTR_RW(always_powered_in_suspend);
201 static struct attribute *onboard_hub_attrs[] = {
202 &dev_attr_always_powered_in_suspend.attr,
205 ATTRIBUTE_GROUPS(onboard_hub);
207 static int onboard_hub_probe(struct platform_device *pdev)
209 struct device *dev = &pdev->dev;
210 struct onboard_hub *hub;
213 hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
217 hub->vdd = devm_regulator_get(dev, "vdd");
218 if (IS_ERR(hub->vdd))
219 return PTR_ERR(hub->vdd);
222 mutex_init(&hub->lock);
223 INIT_LIST_HEAD(&hub->udev_list);
225 dev_set_drvdata(dev, hub);
227 err = onboard_hub_power_on(hub);
232 * The USB driver might have been detached from the USB devices by
233 * onboard_hub_remove(), make sure to re-attach it if needed.
235 err = driver_attach(&onboard_hub_usbdev_driver.drvwrap.driver);
237 onboard_hub_power_off(hub);
244 static int onboard_hub_remove(struct platform_device *pdev)
246 struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
247 struct usbdev_node *node;
248 struct usb_device *udev;
250 hub->going_away = true;
252 mutex_lock(&hub->lock);
254 /* unbind the USB devices to avoid dangling references to this device */
255 while (!list_empty(&hub->udev_list)) {
256 node = list_first_entry(&hub->udev_list, struct usbdev_node, list);
260 * Unbinding the driver will call onboard_hub_remove_usbdev(),
261 * which acquires hub->lock. We must release the lock first.
263 get_device(&udev->dev);
264 mutex_unlock(&hub->lock);
265 device_release_driver(&udev->dev);
266 put_device(&udev->dev);
267 mutex_lock(&hub->lock);
270 mutex_unlock(&hub->lock);
272 return onboard_hub_power_off(hub);
275 static const struct of_device_id onboard_hub_match[] = {
276 { .compatible = "usbbda,411" },
277 { .compatible = "usbbda,5411" },
278 { .compatible = "usbbda,414" },
279 { .compatible = "usbbda,5414" },
282 MODULE_DEVICE_TABLE(of, onboard_hub_match);
284 static bool of_is_onboard_usb_hub(const struct device_node *np)
286 return !!of_match_node(onboard_hub_match, np);
289 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops = {
290 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend, onboard_hub_resume)
293 static struct platform_driver onboard_hub_driver = {
294 .probe = onboard_hub_probe,
295 .remove = onboard_hub_remove,
298 .name = "onboard-usb-hub",
299 .of_match_table = onboard_hub_match,
300 .pm = pm_ptr(&onboard_hub_pm_ops),
301 .dev_groups = onboard_hub_groups,
305 /************************** USB driver **************************/
307 #define VENDOR_ID_REALTEK 0x0bda
310 * Returns the onboard_hub platform device that is associated with the USB
311 * device passed as parameter.
313 static struct onboard_hub *_find_onboard_hub(struct device *dev)
315 struct platform_device *pdev;
316 struct device_node *np;
317 struct onboard_hub *hub;
319 pdev = of_find_device_by_node(dev->of_node);
321 np = of_parse_phandle(dev->of_node, "companion-hub", 0);
323 dev_err(dev, "failed to find device node for companion hub\n");
324 return ERR_PTR(-EINVAL);
327 pdev = of_find_device_by_node(np);
331 return ERR_PTR(-ENODEV);
334 hub = dev_get_drvdata(&pdev->dev);
335 put_device(&pdev->dev);
338 * The presence of drvdata ('hub') indicates that the platform driver
339 * finished probing. This handles the case where (conceivably) we could
340 * be running at the exact same time as the platform driver's probe. If
341 * we detect the race we request probe deferral and we'll come back and
345 return ERR_PTR(-EPROBE_DEFER);
350 static int onboard_hub_usbdev_probe(struct usb_device *udev)
352 struct device *dev = &udev->dev;
353 struct onboard_hub *hub;
356 /* ignore supported hubs without device tree node */
360 hub = _find_onboard_hub(dev);
364 dev_set_drvdata(dev, hub);
366 err = onboard_hub_add_usbdev(hub, udev);
373 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
375 struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
377 onboard_hub_remove_usbdev(hub, udev);
380 static const struct usb_device_id onboard_hub_id_table[] = {
381 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
382 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
383 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
384 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
387 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
389 static struct usb_device_driver onboard_hub_usbdev_driver = {
390 .name = "onboard-usb-hub",
391 .probe = onboard_hub_usbdev_probe,
392 .disconnect = onboard_hub_usbdev_disconnect,
393 .generic_subclass = 1,
394 .supports_autosuspend = 1,
395 .id_table = onboard_hub_id_table,
398 /*** Helpers for creating/destroying platform devices for onboard hubs ***/
400 struct pdev_list_entry {
401 struct platform_device *pdev;
402 struct list_head node;
406 * onboard_hub_create_pdevs -- create platform devices for onboard USB hubs
407 * @parent_hub : parent hub to scan for connected onboard hubs
408 * @pdev_list : list of onboard hub platform devices owned by the parent hub
410 * Creates a platform device for each supported onboard hub that is connected to
411 * the given parent hub. To keep track of the platform devices they are added to
412 * a list that is owned by the parent hub.
414 void onboard_hub_create_pdevs(struct usb_device *parent_hub, struct list_head *pdev_list)
417 struct device_node *np, *npc;
418 struct platform_device *pdev = NULL;
419 struct pdev_list_entry *pdle;
421 INIT_LIST_HEAD(pdev_list);
423 for (i = 1; i <= parent_hub->maxchild; i++) {
424 np = usb_of_get_device_node(parent_hub, i);
428 if (!of_is_onboard_usb_hub(np))
431 npc = of_parse_phandle(np, "companion-hub", 0);
433 pdev = of_find_device_by_node(npc);
438 /* the companion hub already has a platform device, nothing to do here */
439 put_device(&pdev->dev);
443 pdev = of_platform_device_create(np, NULL, &parent_hub->dev);
445 dev_err(&parent_hub->dev,
446 "failed to create platform device for onboard hub '%pOF'\n", np);
450 pdle = devm_kzalloc(&pdev->dev, sizeof(*pdle), GFP_KERNEL);
452 of_platform_device_destroy(&pdev->dev, NULL);
457 list_add(&pdle->node, pdev_list);
463 EXPORT_SYMBOL_GPL(onboard_hub_create_pdevs);
466 * onboard_hub_destroy_pdevs -- free resources of onboard hub platform devices
467 * @pdev_list : list of onboard hub platform devices
469 * Destroys the platform devices in the given list and frees the memory associated
470 * with the list entry.
472 void onboard_hub_destroy_pdevs(struct list_head *pdev_list)
474 struct pdev_list_entry *pdle, *tmp;
476 list_for_each_entry_safe(pdle, tmp, pdev_list, node) {
477 list_del(&pdle->node);
478 of_platform_device_destroy(&pdle->pdev->dev, NULL);
481 EXPORT_SYMBOL_GPL(onboard_hub_destroy_pdevs);
483 /************************** Driver (de)registration **************************/
485 static int __init onboard_hub_init(void)
489 ret = platform_driver_register(&onboard_hub_driver);
493 ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
495 platform_driver_unregister(&onboard_hub_driver);
499 module_init(onboard_hub_init);
501 static void __exit onboard_hub_exit(void)
503 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
504 platform_driver_unregister(&onboard_hub_driver);
506 module_exit(onboard_hub_exit);
508 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
509 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
510 MODULE_LICENSE("GPL v2");