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 void onboard_hub_attach_usb_driver(struct work_struct *work);
32 static struct usb_device_driver onboard_hub_usbdev_driver;
33 static DECLARE_WORK(attach_usb_driver_work, onboard_hub_attach_usb_driver);
35 /************************** Platform driver **************************/
38 struct usb_device *udev;
39 struct list_head list;
43 struct regulator *vdd;
45 const struct onboard_hub_pdata *pdata;
46 struct gpio_desc *reset_gpio;
47 bool always_powered_in_suspend;
50 struct list_head udev_list;
54 static int onboard_hub_power_on(struct onboard_hub *hub)
58 err = regulator_enable(hub->vdd);
60 dev_err(hub->dev, "failed to enable regulator: %d\n", err);
64 fsleep(hub->pdata->reset_us);
65 gpiod_set_value_cansleep(hub->reset_gpio, 0);
67 hub->is_powered_on = true;
72 static int onboard_hub_power_off(struct onboard_hub *hub)
76 gpiod_set_value_cansleep(hub->reset_gpio, 1);
78 err = regulator_disable(hub->vdd);
80 dev_err(hub->dev, "failed to disable regulator: %d\n", err);
84 hub->is_powered_on = false;
89 static int __maybe_unused onboard_hub_suspend(struct device *dev)
91 struct onboard_hub *hub = dev_get_drvdata(dev);
92 struct usbdev_node *node;
93 bool power_off = true;
95 if (hub->always_powered_in_suspend)
98 mutex_lock(&hub->lock);
100 list_for_each_entry(node, &hub->udev_list, list) {
101 if (!device_may_wakeup(node->udev->bus->controller))
104 if (usb_wakeup_enabled_descendants(node->udev)) {
110 mutex_unlock(&hub->lock);
115 return onboard_hub_power_off(hub);
118 static int __maybe_unused onboard_hub_resume(struct device *dev)
120 struct onboard_hub *hub = dev_get_drvdata(dev);
122 if (hub->is_powered_on)
125 return onboard_hub_power_on(hub);
128 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
130 snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
133 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
135 struct usbdev_node *node;
139 mutex_lock(&hub->lock);
141 if (hub->going_away) {
146 node = kzalloc(sizeof(*node), GFP_KERNEL);
154 list_add(&node->list, &hub->udev_list);
156 mutex_unlock(&hub->lock);
158 get_udev_link_name(udev, link_name, sizeof(link_name));
159 WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name));
164 mutex_unlock(&hub->lock);
169 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
171 struct usbdev_node *node;
174 get_udev_link_name(udev, link_name, sizeof(link_name));
175 sysfs_remove_link(&hub->dev->kobj, link_name);
177 mutex_lock(&hub->lock);
179 list_for_each_entry(node, &hub->udev_list, list) {
180 if (node->udev == udev) {
181 list_del(&node->list);
187 mutex_unlock(&hub->lock);
190 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
193 const struct onboard_hub *hub = dev_get_drvdata(dev);
195 return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
198 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t count)
201 struct onboard_hub *hub = dev_get_drvdata(dev);
205 ret = kstrtobool(buf, &val);
209 hub->always_powered_in_suspend = val;
213 static DEVICE_ATTR_RW(always_powered_in_suspend);
215 static struct attribute *onboard_hub_attrs[] = {
216 &dev_attr_always_powered_in_suspend.attr,
219 ATTRIBUTE_GROUPS(onboard_hub);
221 static void onboard_hub_attach_usb_driver(struct work_struct *work)
225 err = driver_attach(&onboard_hub_usbdev_driver.drvwrap.driver);
227 pr_err("Failed to attach USB driver: %d\n", err);
230 static int onboard_hub_probe(struct platform_device *pdev)
232 const struct of_device_id *of_id;
233 struct device *dev = &pdev->dev;
234 struct onboard_hub *hub;
237 hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
241 of_id = of_match_device(onboard_hub_match, &pdev->dev);
245 hub->pdata = of_id->data;
249 hub->vdd = devm_regulator_get(dev, "vdd");
250 if (IS_ERR(hub->vdd))
251 return PTR_ERR(hub->vdd);
253 hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
255 if (IS_ERR(hub->reset_gpio))
256 return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
259 mutex_init(&hub->lock);
260 INIT_LIST_HEAD(&hub->udev_list);
262 dev_set_drvdata(dev, hub);
264 err = onboard_hub_power_on(hub);
269 * The USB driver might have been detached from the USB devices by
270 * onboard_hub_remove() (e.g. through an 'unbind' by userspace),
271 * make sure to re-attach it if needed.
273 * This needs to be done deferred to avoid self-deadlocks on systems
274 * with nested onboard hubs.
276 schedule_work(&attach_usb_driver_work);
281 static int onboard_hub_remove(struct platform_device *pdev)
283 struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
284 struct usbdev_node *node;
285 struct usb_device *udev;
287 hub->going_away = true;
289 mutex_lock(&hub->lock);
291 /* unbind the USB devices to avoid dangling references to this device */
292 while (!list_empty(&hub->udev_list)) {
293 node = list_first_entry(&hub->udev_list, struct usbdev_node, list);
297 * Unbinding the driver will call onboard_hub_remove_usbdev(),
298 * which acquires hub->lock. We must release the lock first.
300 get_device(&udev->dev);
301 mutex_unlock(&hub->lock);
302 device_release_driver(&udev->dev);
303 put_device(&udev->dev);
304 mutex_lock(&hub->lock);
307 mutex_unlock(&hub->lock);
309 return onboard_hub_power_off(hub);
312 MODULE_DEVICE_TABLE(of, onboard_hub_match);
314 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops = {
315 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend, onboard_hub_resume)
318 static struct platform_driver onboard_hub_driver = {
319 .probe = onboard_hub_probe,
320 .remove = onboard_hub_remove,
323 .name = "onboard-usb-hub",
324 .of_match_table = onboard_hub_match,
325 .pm = pm_ptr(&onboard_hub_pm_ops),
326 .dev_groups = onboard_hub_groups,
330 /************************** USB driver **************************/
332 #define VENDOR_ID_GENESYS 0x05e3
333 #define VENDOR_ID_MICROCHIP 0x0424
334 #define VENDOR_ID_REALTEK 0x0bda
335 #define VENDOR_ID_TI 0x0451
336 #define VENDOR_ID_VIA 0x2109
339 * Returns the onboard_hub platform device that is associated with the USB
340 * device passed as parameter.
342 static struct onboard_hub *_find_onboard_hub(struct device *dev)
344 struct platform_device *pdev;
345 struct device_node *np;
346 struct onboard_hub *hub;
348 pdev = of_find_device_by_node(dev->of_node);
350 np = of_parse_phandle(dev->of_node, "peer-hub", 0);
352 dev_err(dev, "failed to find device node for peer hub\n");
353 return ERR_PTR(-EINVAL);
356 pdev = of_find_device_by_node(np);
360 return ERR_PTR(-ENODEV);
363 hub = dev_get_drvdata(&pdev->dev);
364 put_device(&pdev->dev);
367 * The presence of drvdata ('hub') indicates that the platform driver
368 * finished probing. This handles the case where (conceivably) we could
369 * be running at the exact same time as the platform driver's probe. If
370 * we detect the race we request probe deferral and we'll come back and
374 return ERR_PTR(-EPROBE_DEFER);
379 static int onboard_hub_usbdev_probe(struct usb_device *udev)
381 struct device *dev = &udev->dev;
382 struct onboard_hub *hub;
385 /* ignore supported hubs without device tree node */
389 hub = _find_onboard_hub(dev);
393 dev_set_drvdata(dev, hub);
395 err = onboard_hub_add_usbdev(hub, udev);
402 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
404 struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
406 onboard_hub_remove_usbdev(hub, udev);
409 static const struct usb_device_id onboard_hub_id_table[] = {
410 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0608) }, /* Genesys Logic GL850G USB 2.0 */
411 { USB_DEVICE(VENDOR_ID_GENESYS, 0x0610) }, /* Genesys Logic GL852G USB 2.0 */
412 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 */
413 { USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2517) }, /* USB2517 USB 2.0 */
414 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
415 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
416 { USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
417 { USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
418 { USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 */
419 { USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 */
420 { USB_DEVICE(VENDOR_ID_VIA, 0x0817) }, /* VIA VL817 3.1 */
421 { USB_DEVICE(VENDOR_ID_VIA, 0x2817) }, /* VIA VL817 2.0 */
424 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
426 static struct usb_device_driver onboard_hub_usbdev_driver = {
427 .name = "onboard-usb-hub",
428 .probe = onboard_hub_usbdev_probe,
429 .disconnect = onboard_hub_usbdev_disconnect,
430 .generic_subclass = 1,
431 .supports_autosuspend = 1,
432 .id_table = onboard_hub_id_table,
435 static int __init onboard_hub_init(void)
439 ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
443 ret = platform_driver_register(&onboard_hub_driver);
445 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
449 module_init(onboard_hub_init);
451 static void __exit onboard_hub_exit(void)
453 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
454 platform_driver_unregister(&onboard_hub_driver);
456 cancel_work_sync(&attach_usb_driver_work);
458 module_exit(onboard_hub_exit);
461 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
462 MODULE_LICENSE("GPL v2");