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"
31 * Use generic names, as the actual names might differ between hubs. If a new
32 * hub requires more than the currently supported supplies, add a new one here.
34 static const char * const supply_names
[] = {
39 #define MAX_SUPPLIES ARRAY_SIZE(supply_names)
41 static void onboard_hub_attach_usb_driver(struct work_struct
*work
);
43 static struct usb_device_driver onboard_hub_usbdev_driver
;
44 static DECLARE_WORK(attach_usb_driver_work
, onboard_hub_attach_usb_driver
);
46 /************************** Platform driver **************************/
49 struct usb_device
*udev
;
50 struct list_head list
;
54 struct regulator_bulk_data supplies
[MAX_SUPPLIES
];
56 const struct onboard_hub_pdata
*pdata
;
57 struct gpio_desc
*reset_gpio
;
58 bool always_powered_in_suspend
;
61 struct list_head udev_list
;
65 static int onboard_hub_power_on(struct onboard_hub
*hub
)
69 err
= regulator_bulk_enable(hub
->pdata
->num_supplies
, hub
->supplies
);
71 dev_err(hub
->dev
, "failed to enable supplies: %d\n", err
);
75 fsleep(hub
->pdata
->reset_us
);
76 gpiod_set_value_cansleep(hub
->reset_gpio
, 0);
78 hub
->is_powered_on
= true;
83 static int onboard_hub_power_off(struct onboard_hub
*hub
)
87 gpiod_set_value_cansleep(hub
->reset_gpio
, 1);
89 err
= regulator_bulk_disable(hub
->pdata
->num_supplies
, hub
->supplies
);
91 dev_err(hub
->dev
, "failed to disable supplies: %d\n", err
);
95 hub
->is_powered_on
= false;
100 static int __maybe_unused
onboard_hub_suspend(struct device
*dev
)
102 struct onboard_hub
*hub
= dev_get_drvdata(dev
);
103 struct usbdev_node
*node
;
104 bool power_off
= true;
106 if (hub
->always_powered_in_suspend
)
109 mutex_lock(&hub
->lock
);
111 list_for_each_entry(node
, &hub
->udev_list
, list
) {
112 if (!device_may_wakeup(node
->udev
->bus
->controller
))
115 if (usb_wakeup_enabled_descendants(node
->udev
)) {
121 mutex_unlock(&hub
->lock
);
126 return onboard_hub_power_off(hub
);
129 static int __maybe_unused
onboard_hub_resume(struct device
*dev
)
131 struct onboard_hub
*hub
= dev_get_drvdata(dev
);
133 if (hub
->is_powered_on
)
136 return onboard_hub_power_on(hub
);
139 static inline void get_udev_link_name(const struct usb_device
*udev
, char *buf
, size_t size
)
141 snprintf(buf
, size
, "usb_dev.%s", dev_name(&udev
->dev
));
144 static int onboard_hub_add_usbdev(struct onboard_hub
*hub
, struct usb_device
*udev
)
146 struct usbdev_node
*node
;
150 mutex_lock(&hub
->lock
);
152 if (hub
->going_away
) {
157 node
= kzalloc(sizeof(*node
), GFP_KERNEL
);
165 list_add(&node
->list
, &hub
->udev_list
);
167 mutex_unlock(&hub
->lock
);
169 get_udev_link_name(udev
, link_name
, sizeof(link_name
));
170 WARN_ON(sysfs_create_link(&hub
->dev
->kobj
, &udev
->dev
.kobj
, link_name
));
175 mutex_unlock(&hub
->lock
);
180 static void onboard_hub_remove_usbdev(struct onboard_hub
*hub
, const struct usb_device
*udev
)
182 struct usbdev_node
*node
;
185 get_udev_link_name(udev
, link_name
, sizeof(link_name
));
186 sysfs_remove_link(&hub
->dev
->kobj
, link_name
);
188 mutex_lock(&hub
->lock
);
190 list_for_each_entry(node
, &hub
->udev_list
, list
) {
191 if (node
->udev
== udev
) {
192 list_del(&node
->list
);
198 mutex_unlock(&hub
->lock
);
201 static ssize_t
always_powered_in_suspend_show(struct device
*dev
, struct device_attribute
*attr
,
204 const struct onboard_hub
*hub
= dev_get_drvdata(dev
);
206 return sysfs_emit(buf
, "%d\n", hub
->always_powered_in_suspend
);
209 static ssize_t
always_powered_in_suspend_store(struct device
*dev
, struct device_attribute
*attr
,
210 const char *buf
, size_t count
)
212 struct onboard_hub
*hub
= dev_get_drvdata(dev
);
216 ret
= kstrtobool(buf
, &val
);
220 hub
->always_powered_in_suspend
= val
;
224 static DEVICE_ATTR_RW(always_powered_in_suspend
);
226 static struct attribute
*onboard_hub_attrs
[] = {
227 &dev_attr_always_powered_in_suspend
.attr
,
230 ATTRIBUTE_GROUPS(onboard_hub
);
232 static void onboard_hub_attach_usb_driver(struct work_struct
*work
)
236 err
= driver_attach(&onboard_hub_usbdev_driver
.drvwrap
.driver
);
238 pr_err("Failed to attach USB driver: %d\n", err
);
241 static int onboard_hub_probe(struct platform_device
*pdev
)
243 struct device
*dev
= &pdev
->dev
;
244 struct onboard_hub
*hub
;
248 hub
= devm_kzalloc(dev
, sizeof(*hub
), GFP_KERNEL
);
252 hub
->pdata
= device_get_match_data(&pdev
->dev
);
256 if (hub
->pdata
->num_supplies
> MAX_SUPPLIES
)
257 return dev_err_probe(dev
, -EINVAL
, "max %zu supplies supported!\n",
260 for (i
= 0; i
< hub
->pdata
->num_supplies
; i
++)
261 hub
->supplies
[i
].supply
= supply_names
[i
];
263 err
= devm_regulator_bulk_get(dev
, hub
->pdata
->num_supplies
, hub
->supplies
);
265 dev_err(dev
, "Failed to get regulator supplies: %d\n", err
);
269 hub
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset",
271 if (IS_ERR(hub
->reset_gpio
))
272 return dev_err_probe(dev
, PTR_ERR(hub
->reset_gpio
), "failed to get reset GPIO\n");
275 mutex_init(&hub
->lock
);
276 INIT_LIST_HEAD(&hub
->udev_list
);
278 dev_set_drvdata(dev
, hub
);
280 err
= onboard_hub_power_on(hub
);
285 * The USB driver might have been detached from the USB devices by
286 * onboard_hub_remove() (e.g. through an 'unbind' by userspace),
287 * make sure to re-attach it if needed.
289 * This needs to be done deferred to avoid self-deadlocks on systems
290 * with nested onboard hubs.
292 schedule_work(&attach_usb_driver_work
);
297 static void onboard_hub_remove(struct platform_device
*pdev
)
299 struct onboard_hub
*hub
= dev_get_drvdata(&pdev
->dev
);
300 struct usbdev_node
*node
;
301 struct usb_device
*udev
;
303 hub
->going_away
= true;
305 mutex_lock(&hub
->lock
);
307 /* unbind the USB devices to avoid dangling references to this device */
308 while (!list_empty(&hub
->udev_list
)) {
309 node
= list_first_entry(&hub
->udev_list
, struct usbdev_node
, list
);
313 * Unbinding the driver will call onboard_hub_remove_usbdev(),
314 * which acquires hub->lock. We must release the lock first.
316 get_device(&udev
->dev
);
317 mutex_unlock(&hub
->lock
);
318 device_release_driver(&udev
->dev
);
319 put_device(&udev
->dev
);
320 mutex_lock(&hub
->lock
);
323 mutex_unlock(&hub
->lock
);
325 onboard_hub_power_off(hub
);
328 MODULE_DEVICE_TABLE(of
, onboard_hub_match
);
330 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops
= {
331 SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend
, onboard_hub_resume
)
334 static struct platform_driver onboard_hub_driver
= {
335 .probe
= onboard_hub_probe
,
336 .remove_new
= onboard_hub_remove
,
339 .name
= "onboard-usb-hub",
340 .of_match_table
= onboard_hub_match
,
341 .pm
= pm_ptr(&onboard_hub_pm_ops
),
342 .dev_groups
= onboard_hub_groups
,
346 /************************** USB driver **************************/
348 #define VENDOR_ID_CYPRESS 0x04b4
349 #define VENDOR_ID_GENESYS 0x05e3
350 #define VENDOR_ID_MICROCHIP 0x0424
351 #define VENDOR_ID_REALTEK 0x0bda
352 #define VENDOR_ID_TI 0x0451
353 #define VENDOR_ID_VIA 0x2109
356 * Returns the onboard_hub platform device that is associated with the USB
357 * device passed as parameter.
359 static struct onboard_hub
*_find_onboard_hub(struct device
*dev
)
361 struct platform_device
*pdev
;
362 struct device_node
*np
;
363 struct onboard_hub
*hub
;
365 pdev
= of_find_device_by_node(dev
->of_node
);
367 np
= of_parse_phandle(dev
->of_node
, "peer-hub", 0);
369 dev_err(dev
, "failed to find device node for peer hub\n");
370 return ERR_PTR(-EINVAL
);
373 pdev
= of_find_device_by_node(np
);
377 return ERR_PTR(-ENODEV
);
380 hub
= dev_get_drvdata(&pdev
->dev
);
381 put_device(&pdev
->dev
);
384 * The presence of drvdata ('hub') indicates that the platform driver
385 * finished probing. This handles the case where (conceivably) we could
386 * be running at the exact same time as the platform driver's probe. If
387 * we detect the race we request probe deferral and we'll come back and
391 return ERR_PTR(-EPROBE_DEFER
);
396 static int onboard_hub_usbdev_probe(struct usb_device
*udev
)
398 struct device
*dev
= &udev
->dev
;
399 struct onboard_hub
*hub
;
402 /* ignore supported hubs without device tree node */
406 hub
= _find_onboard_hub(dev
);
410 dev_set_drvdata(dev
, hub
);
412 err
= onboard_hub_add_usbdev(hub
, udev
);
419 static void onboard_hub_usbdev_disconnect(struct usb_device
*udev
)
421 struct onboard_hub
*hub
= dev_get_drvdata(&udev
->dev
);
423 onboard_hub_remove_usbdev(hub
, udev
);
426 static const struct usb_device_id onboard_hub_id_table
[] = {
427 { USB_DEVICE(VENDOR_ID_CYPRESS
, 0x6504) }, /* CYUSB33{0,1,2}x/CYUSB230x 3.0 */
428 { USB_DEVICE(VENDOR_ID_CYPRESS
, 0x6506) }, /* CYUSB33{0,1,2}x/CYUSB230x 2.0 */
429 { USB_DEVICE(VENDOR_ID_GENESYS
, 0x0608) }, /* Genesys Logic GL850G USB 2.0 */
430 { USB_DEVICE(VENDOR_ID_GENESYS
, 0x0610) }, /* Genesys Logic GL852G USB 2.0 */
431 { USB_DEVICE(VENDOR_ID_GENESYS
, 0x0620) }, /* Genesys Logic GL3523 USB 3.1 */
432 { USB_DEVICE(VENDOR_ID_MICROCHIP
, 0x2412) }, /* USB2412 USB 2.0 */
433 { USB_DEVICE(VENDOR_ID_MICROCHIP
, 0x2514) }, /* USB2514B USB 2.0 */
434 { USB_DEVICE(VENDOR_ID_MICROCHIP
, 0x2517) }, /* USB2517 USB 2.0 */
435 { USB_DEVICE(VENDOR_ID_REALTEK
, 0x0411) }, /* RTS5411 USB 3.1 */
436 { USB_DEVICE(VENDOR_ID_REALTEK
, 0x5411) }, /* RTS5411 USB 2.1 */
437 { USB_DEVICE(VENDOR_ID_REALTEK
, 0x0414) }, /* RTS5414 USB 3.2 */
438 { USB_DEVICE(VENDOR_ID_REALTEK
, 0x5414) }, /* RTS5414 USB 2.1 */
439 { USB_DEVICE(VENDOR_ID_TI
, 0x8140) }, /* TI USB8041 3.0 */
440 { USB_DEVICE(VENDOR_ID_TI
, 0x8142) }, /* TI USB8041 2.0 */
441 { USB_DEVICE(VENDOR_ID_VIA
, 0x0817) }, /* VIA VL817 3.1 */
442 { USB_DEVICE(VENDOR_ID_VIA
, 0x2817) }, /* VIA VL817 2.0 */
445 MODULE_DEVICE_TABLE(usb
, onboard_hub_id_table
);
447 static struct usb_device_driver onboard_hub_usbdev_driver
= {
448 .name
= "onboard-usb-hub",
449 .probe
= onboard_hub_usbdev_probe
,
450 .disconnect
= onboard_hub_usbdev_disconnect
,
451 .generic_subclass
= 1,
452 .supports_autosuspend
= 1,
453 .id_table
= onboard_hub_id_table
,
456 static int __init
onboard_hub_init(void)
460 ret
= usb_register_device_driver(&onboard_hub_usbdev_driver
, THIS_MODULE
);
464 ret
= platform_driver_register(&onboard_hub_driver
);
466 usb_deregister_device_driver(&onboard_hub_usbdev_driver
);
470 module_init(onboard_hub_init
);
472 static void __exit
onboard_hub_exit(void)
474 usb_deregister_device_driver(&onboard_hub_usbdev_driver
);
475 platform_driver_unregister(&onboard_hub_driver
);
477 cancel_work_sync(&attach_usb_driver_work
);
479 module_exit(onboard_hub_exit
);
481 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
482 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
483 MODULE_LICENSE("GPL v2");