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/gpio/consumer.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
17 #include <linux/of_platform.h>
18 #include <linux/platform_device.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/slab.h>
21 #include <linux/suspend.h>
22 #include <linux/sysfs.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
25 #include <linux/usb/onboard_hub.h>
26 #include <linux/workqueue.h>
28 #include "onboard_usb_hub.h"
30 static struct usb_device_driver onboard_hub_usbdev_driver;
32 /************************** Platform driver **************************/
35 struct usb_device *udev;
36 struct list_head list;
40 struct regulator *vdd;
42 const struct onboard_hub_pdata *pdata;
43 struct gpio_desc *reset_gpio;
44 bool always_powered_in_suspend;
47 struct list_head udev_list;
48 struct work_struct attach_usb_driver_work;
52 static int onboard_hub_power_on(struct onboard_hub *hub)
56 err = regulator_enable(hub->vdd);
58 dev_err(hub->dev, "failed to enable regulator: %d\n", err);
62 fsleep(hub->pdata->reset_us);
63 gpiod_set_value_cansleep(hub->reset_gpio, 0);
65 hub->is_powered_on = true;
70 static int onboard_hub_power_off(struct onboard_hub *hub)
74 gpiod_set_value_cansleep(hub->reset_gpio, 1);
76 err = regulator_disable(hub->vdd);
78 dev_err(hub->dev, "failed to disable regulator: %d\n", err);
82 hub->is_powered_on = false;
87 static int __maybe_unused onboard_hub_suspend(struct device *dev)
89 struct onboard_hub *hub = dev_get_drvdata(dev);
90 struct usbdev_node *node;
91 bool power_off = true;
93 if (hub->always_powered_in_suspend)
96 mutex_lock(&hub->lock);
98 list_for_each_entry(node, &hub->udev_list, list) {
99 if (!device_may_wakeup(node->udev->bus->controller))
102 if (usb_wakeup_enabled_descendants(node->udev)) {
108 mutex_unlock(&hub->lock);
113 return onboard_hub_power_off(hub);
116 static int __maybe_unused onboard_hub_resume(struct device *dev)
118 struct onboard_hub *hub = dev_get_drvdata(dev);
120 if (hub->is_powered_on)
123 return onboard_hub_power_on(hub);
126 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
128 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
131 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
133 struct usbdev_node *node;
137 mutex_lock(&hub->lock);
139 if (hub->going_away) {
144 node = kzalloc(sizeof(*node), GFP_KERNEL);
152 list_add(&node->list, &hub->udev_list);
154 mutex_unlock(&hub->lock);
156 get_udev_link_name(udev, link_name, sizeof(link_name));
157 WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name));
162 mutex_unlock(&hub->lock);
167 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
169 struct usbdev_node *node;
172 get_udev_link_name(udev, link_name, sizeof(link_name));
173 sysfs_remove_link(&hub->dev->kobj, link_name);
175 mutex_lock(&hub->lock);
177 list_for_each_entry(node, &hub->udev_list, list) {
178 if (node->udev == udev) {
179 list_del(&node->list);
185 mutex_unlock(&hub->lock);
188 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
191 const struct onboard_hub *hub = dev_get_drvdata(dev);
193 return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
196 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
197 const char *buf, size_t count)
199 struct onboard_hub *hub = dev_get_drvdata(dev);
203 ret = kstrtobool(buf, &val);
207 hub->always_powered_in_suspend = val;
211 static DEVICE_ATTR_RW(always_powered_in_suspend);
213 static struct attribute *onboard_hub_attrs[] = {
214 &dev_attr_always_powered_in_suspend.attr,
217 ATTRIBUTE_GROUPS(onboard_hub);
219 static void onboard_hub_attach_usb_driver(struct work_struct *work)
223 err = driver_attach(&onboard_hub_usbdev_driver.drvwrap.driver);
225 pr_err("Failed to attach USB driver: %d\n", err);
228 static int onboard_hub_probe(struct platform_device *pdev)
230 const struct of_device_id *of_id;
231 struct device *dev = &pdev->dev;
232 struct onboard_hub *hub;
235 hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
239 of_id = of_match_device(onboard_hub_match, &pdev->dev);
243 hub->pdata = of_id->data;
247 hub->vdd = devm_regulator_get(dev, "vdd");
248 if (IS_ERR(hub->vdd))
249 return PTR_ERR(hub->vdd);
251 hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
253 if (IS_ERR(hub->reset_gpio))
254 return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
257 mutex_init(&hub->lock);
258 INIT_LIST_HEAD(&hub->udev_list);
260 dev_set_drvdata(dev, hub);
262 err = onboard_hub_power_on(hub);
267 * The USB driver might have been detached from the USB devices by
268 * onboard_hub_remove() (e.g. through an 'unbind' by userspace),
269 * make sure to re-attach it if needed.
271 * This needs to be done deferred to avoid self-deadlocks on systems
272 * with nested onboard hubs.
274 INIT_WORK(&hub->attach_usb_driver_work, onboard_hub_attach_usb_driver);
275 schedule_work(&hub->attach_usb_driver_work);
280 static int onboard_hub_remove(struct platform_device *pdev)
282 struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
283 struct usbdev_node *node;
284 struct usb_device *udev;
286 hub->going_away = true;
288 if (&hub->attach_usb_driver_work != current_work())
289 cancel_work_sync(&hub->attach_usb_driver_work);
291 mutex_lock(&hub->lock);
293 /* unbind the USB devices to avoid dangling references to this device */
294 while (!list_empty(&hub->udev_list)) {
295 node = list_first_entry(&hub->udev_list, struct usbdev_node, list);
299 * Unbinding the driver will call onboard_hub_remove_usbdev(),
300 * which acquires hub->lock. We must release the lock first.
302 get_device(&udev->dev);
303 mutex_unlock(&hub->lock);
304 device_release_driver(&udev->dev);
305 put_device(&udev->dev);
306 mutex_lock(&hub->lock);
309 mutex_unlock(&hub->lock);
311 return onboard_hub_power_off(hub);
314 MODULE_DEVICE_TABLE(of, onboard_hub_match);
316 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops = {
317 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend, onboard_hub_resume)
320 static struct platform_driver onboard_hub_driver = {
321 .probe = onboard_hub_probe,
322 .remove = onboard_hub_remove,
325 .name = "onboard-usb-hub",
326 .of_match_table = onboard_hub_match,
327 .pm = pm_ptr(&onboard_hub_pm_ops),
328 .dev_groups = onboard_hub_groups,
332 /************************** USB driver **************************/
334 #define VENDOR_ID_GENESYS 0x05e3
335 #define VENDOR_ID_MICROCHIP 0x0424
336 #define VENDOR_ID_REALTEK 0x0bda
337 #define VENDOR_ID_TI 0x0451
340 * Returns the onboard_hub platform device that is associated with the USB
341 * device passed as parameter.
343 static struct onboard_hub *_find_onboard_hub(struct device *dev)
345 struct platform_device *pdev;
346 struct device_node *np;
347 struct onboard_hub *hub;
349 pdev = of_find_device_by_node(dev->of_node);
351 np = of_parse_phandle(dev->of_node, "peer-hub", 0);
353 dev_err(dev, "failed to find device node for peer hub\n");
354 return ERR_PTR(-EINVAL);
357 pdev = of_find_device_by_node(np);
361 return ERR_PTR(-ENODEV);
364 hub = dev_get_drvdata(&pdev->dev);
365 put_device(&pdev->dev);
368 * The presence of drvdata ('hub') indicates that the platform driver
369 * finished probing. This handles the case where (conceivably) we could
370 * be running at the exact same time as the platform driver's probe. If
371 * we detect the race we request probe deferral and we'll come back and
375 return ERR_PTR(-EPROBE_DEFER);
380 static int onboard_hub_usbdev_probe(struct usb_device *udev)
382 struct device *dev = &udev->dev;
383 struct onboard_hub *hub;
386 /* ignore supported hubs without device tree node */
390 hub = _find_onboard_hub(dev);
394 dev_set_drvdata(dev, hub);
396 err = onboard_hub_add_usbdev(hub, udev);
403 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
405 struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
407 onboard_hub_remove_usbdev(hub, udev);
410 static const struct usb_device_id onboard_hub_id_table[] = {
411 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 */
412 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 */
413 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
414 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
415 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
416 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
417 { USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 */
418 { USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 */
421 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
423 static struct usb_device_driver onboard_hub_usbdev_driver = {
424 .name = "onboard-usb-hub",
425 .probe = onboard_hub_usbdev_probe,
426 .disconnect = onboard_hub_usbdev_disconnect,
427 .generic_subclass = 1,
428 .supports_autosuspend = 1,
429 .id_table = onboard_hub_id_table,
432 static int __init onboard_hub_init(void)
436 ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
440 ret = platform_driver_register(&onboard_hub_driver);
442 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
446 module_init(onboard_hub_init);
448 static void __exit onboard_hub_exit(void)
450 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
451 platform_driver_unregister(&onboard_hub_driver);
453 module_exit(onboard_hub_exit);
455 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
456 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
457 MODULE_LICENSE("GPL v2");