1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Driver for onboard USB hubs
4  *
5  * Copyright (c) 2022, Google LLC
6  */
7 
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>
16 #include <linux/of.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>
27 
28 #include "onboard_usb_hub.h"
29 
30 static struct usb_device_driver onboard_hub_usbdev_driver;
31 
32 /************************** Platform driver **************************/
33 
34 struct usbdev_node {
35 	struct usb_device *udev;
36 	struct list_head list;
37 };
38 
39 struct onboard_hub {
40 	struct regulator *vdd;
41 	struct device *dev;
42 	const struct onboard_hub_pdata *pdata;
43 	struct gpio_desc *reset_gpio;
44 	bool always_powered_in_suspend;
45 	bool is_powered_on;
46 	bool going_away;
47 	struct list_head udev_list;
48 	struct work_struct attach_usb_driver_work;
49 	struct mutex lock;
50 };
51 
52 static int onboard_hub_power_on(struct onboard_hub *hub)
53 {
54 	int err;
55 
56 	err = regulator_enable(hub->vdd);
57 	if (err) {
58 		dev_err(hub->dev, "failed to enable regulator: %d\n", err);
59 		return err;
60 	}
61 
62 	fsleep(hub->pdata->reset_us);
63 	gpiod_set_value_cansleep(hub->reset_gpio, 0);
64 
65 	hub->is_powered_on = true;
66 
67 	return 0;
68 }
69 
70 static int onboard_hub_power_off(struct onboard_hub *hub)
71 {
72 	int err;
73 
74 	if (hub->reset_gpio) {
75 		gpiod_set_value_cansleep(hub->reset_gpio, 1);
76 		fsleep(hub->pdata->reset_us);
77 	}
78 
79 	err = regulator_disable(hub->vdd);
80 	if (err) {
81 		dev_err(hub->dev, "failed to disable regulator: %d\n", err);
82 		return err;
83 	}
84 
85 	hub->is_powered_on = false;
86 
87 	return 0;
88 }
89 
90 static int __maybe_unused onboard_hub_suspend(struct device *dev)
91 {
92 	struct onboard_hub *hub = dev_get_drvdata(dev);
93 	struct usbdev_node *node;
94 	bool power_off = true;
95 
96 	if (hub->always_powered_in_suspend)
97 		return 0;
98 
99 	mutex_lock(&hub->lock);
100 
101 	list_for_each_entry(node, &hub->udev_list, list) {
102 		if (!device_may_wakeup(node->udev->bus->controller))
103 			continue;
104 
105 		if (usb_wakeup_enabled_descendants(node->udev)) {
106 			power_off = false;
107 			break;
108 		}
109 	}
110 
111 	mutex_unlock(&hub->lock);
112 
113 	if (!power_off)
114 		return 0;
115 
116 	return onboard_hub_power_off(hub);
117 }
118 
119 static int __maybe_unused onboard_hub_resume(struct device *dev)
120 {
121 	struct onboard_hub *hub = dev_get_drvdata(dev);
122 
123 	if (hub->is_powered_on)
124 		return 0;
125 
126 	return onboard_hub_power_on(hub);
127 }
128 
129 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
130 {
131 	snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
132 }
133 
134 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
135 {
136 	struct usbdev_node *node;
137 	char link_name[64];
138 	int err;
139 
140 	mutex_lock(&hub->lock);
141 
142 	if (hub->going_away) {
143 		err = -EINVAL;
144 		goto error;
145 	}
146 
147 	node = kzalloc(sizeof(*node), GFP_KERNEL);
148 	if (!node) {
149 		err = -ENOMEM;
150 		goto error;
151 	}
152 
153 	node->udev = udev;
154 
155 	list_add(&node->list, &hub->udev_list);
156 
157 	mutex_unlock(&hub->lock);
158 
159 	get_udev_link_name(udev, link_name, sizeof(link_name));
160 	WARN_ON(sysfs_create_link(&hub->dev->kobj, &udev->dev.kobj, link_name));
161 
162 	return 0;
163 
164 error:
165 	mutex_unlock(&hub->lock);
166 
167 	return err;
168 }
169 
170 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
171 {
172 	struct usbdev_node *node;
173 	char link_name[64];
174 
175 	get_udev_link_name(udev, link_name, sizeof(link_name));
176 	sysfs_remove_link(&hub->dev->kobj, link_name);
177 
178 	mutex_lock(&hub->lock);
179 
180 	list_for_each_entry(node, &hub->udev_list, list) {
181 		if (node->udev == udev) {
182 			list_del(&node->list);
183 			kfree(node);
184 			break;
185 		}
186 	}
187 
188 	mutex_unlock(&hub->lock);
189 }
190 
191 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
192 			   char *buf)
193 {
194 	const struct onboard_hub *hub = dev_get_drvdata(dev);
195 
196 	return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
197 }
198 
199 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
200 			    const char *buf, size_t count)
201 {
202 	struct onboard_hub *hub = dev_get_drvdata(dev);
203 	bool val;
204 	int ret;
205 
206 	ret = kstrtobool(buf, &val);
207 	if (ret < 0)
208 		return ret;
209 
210 	hub->always_powered_in_suspend = val;
211 
212 	return count;
213 }
214 static DEVICE_ATTR_RW(always_powered_in_suspend);
215 
216 static struct attribute *onboard_hub_attrs[] = {
217 	&dev_attr_always_powered_in_suspend.attr,
218 	NULL,
219 };
220 ATTRIBUTE_GROUPS(onboard_hub);
221 
222 static void onboard_hub_attach_usb_driver(struct work_struct *work)
223 {
224 	int err;
225 
226 	err = driver_attach(&onboard_hub_usbdev_driver.drvwrap.driver);
227 	if (err)
228 		pr_err("Failed to attach USB driver: %d\n", err);
229 }
230 
231 static int onboard_hub_probe(struct platform_device *pdev)
232 {
233 	const struct of_device_id *of_id;
234 	struct device *dev = &pdev->dev;
235 	struct onboard_hub *hub;
236 	int err;
237 
238 	hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
239 	if (!hub)
240 		return -ENOMEM;
241 
242 	of_id = of_match_device(onboard_hub_match, &pdev->dev);
243 	if (!of_id)
244 		return -ENODEV;
245 
246 	hub->pdata = of_id->data;
247 	if (!hub->pdata)
248 		return -EINVAL;
249 
250 	hub->vdd = devm_regulator_get(dev, "vdd");
251 	if (IS_ERR(hub->vdd))
252 		return PTR_ERR(hub->vdd);
253 
254 	hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
255 						  GPIOD_OUT_HIGH);
256 	if (IS_ERR(hub->reset_gpio))
257 		return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
258 
259 	hub->dev = dev;
260 	mutex_init(&hub->lock);
261 	INIT_LIST_HEAD(&hub->udev_list);
262 
263 	dev_set_drvdata(dev, hub);
264 
265 	err = onboard_hub_power_on(hub);
266 	if (err)
267 		return err;
268 
269 	/*
270 	 * The USB driver might have been detached from the USB devices by
271 	 * onboard_hub_remove() (e.g. through an 'unbind' by userspace),
272 	 * make sure to re-attach it if needed.
273 	 *
274 	 * This needs to be done deferred to avoid self-deadlocks on systems
275 	 * with nested onboard hubs.
276 	 */
277 	INIT_WORK(&hub->attach_usb_driver_work, onboard_hub_attach_usb_driver);
278 	schedule_work(&hub->attach_usb_driver_work);
279 
280 	return 0;
281 }
282 
283 static int onboard_hub_remove(struct platform_device *pdev)
284 {
285 	struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
286 	struct usbdev_node *node;
287 	struct usb_device *udev;
288 
289 	hub->going_away = true;
290 
291 	if (&hub->attach_usb_driver_work != current_work())
292 		cancel_work_sync(&hub->attach_usb_driver_work);
293 
294 	mutex_lock(&hub->lock);
295 
296 	/* unbind the USB devices to avoid dangling references to this device */
297 	while (!list_empty(&hub->udev_list)) {
298 		node = list_first_entry(&hub->udev_list, struct usbdev_node, list);
299 		udev = node->udev;
300 
301 		/*
302 		 * Unbinding the driver will call onboard_hub_remove_usbdev(),
303 		 * which acquires hub->lock.  We must release the lock first.
304 		 */
305 		get_device(&udev->dev);
306 		mutex_unlock(&hub->lock);
307 		device_release_driver(&udev->dev);
308 		put_device(&udev->dev);
309 		mutex_lock(&hub->lock);
310 	}
311 
312 	mutex_unlock(&hub->lock);
313 
314 	return onboard_hub_power_off(hub);
315 }
316 
317 MODULE_DEVICE_TABLE(of, onboard_hub_match);
318 
319 static const struct dev_pm_ops __maybe_unused onboard_hub_pm_ops = {
320 	SET_LATE_SYSTEM_SLEEP_PM_OPS(onboard_hub_suspend, onboard_hub_resume)
321 };
322 
323 static struct platform_driver onboard_hub_driver = {
324 	.probe = onboard_hub_probe,
325 	.remove = onboard_hub_remove,
326 
327 	.driver = {
328 		.name = "onboard-usb-hub",
329 		.of_match_table = onboard_hub_match,
330 		.pm = pm_ptr(&onboard_hub_pm_ops),
331 		.dev_groups = onboard_hub_groups,
332 	},
333 };
334 
335 /************************** USB driver **************************/
336 
337 #define VENDOR_ID_MICROCHIP	0x0424
338 #define VENDOR_ID_REALTEK	0x0bda
339 #define VENDOR_ID_TI		0x0451
340 
341 /*
342  * Returns the onboard_hub platform device that is associated with the USB
343  * device passed as parameter.
344  */
345 static struct onboard_hub *_find_onboard_hub(struct device *dev)
346 {
347 	struct platform_device *pdev;
348 	struct device_node *np;
349 	struct onboard_hub *hub;
350 
351 	pdev = of_find_device_by_node(dev->of_node);
352 	if (!pdev) {
353 		np = of_parse_phandle(dev->of_node, "peer-hub", 0);
354 		if (!np) {
355 			dev_err(dev, "failed to find device node for peer hub\n");
356 			return ERR_PTR(-EINVAL);
357 		}
358 
359 		pdev = of_find_device_by_node(np);
360 		of_node_put(np);
361 
362 		if (!pdev)
363 			return ERR_PTR(-ENODEV);
364 	}
365 
366 	hub = dev_get_drvdata(&pdev->dev);
367 	put_device(&pdev->dev);
368 
369 	/*
370 	 * The presence of drvdata ('hub') indicates that the platform driver
371 	 * finished probing. This handles the case where (conceivably) we could
372 	 * be running at the exact same time as the platform driver's probe. If
373 	 * we detect the race we request probe deferral and we'll come back and
374 	 * try again.
375 	 */
376 	if (!hub)
377 		return ERR_PTR(-EPROBE_DEFER);
378 
379 	return hub;
380 }
381 
382 static int onboard_hub_usbdev_probe(struct usb_device *udev)
383 {
384 	struct device *dev = &udev->dev;
385 	struct onboard_hub *hub;
386 	int err;
387 
388 	/* ignore supported hubs without device tree node */
389 	if (!dev->of_node)
390 		return -ENODEV;
391 
392 	hub = _find_onboard_hub(dev);
393 	if (IS_ERR(hub))
394 		return PTR_ERR(hub);
395 
396 	dev_set_drvdata(dev, hub);
397 
398 	err = onboard_hub_add_usbdev(hub, udev);
399 	if (err)
400 		return err;
401 
402 	return 0;
403 }
404 
405 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
406 {
407 	struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
408 
409 	onboard_hub_remove_usbdev(hub, udev);
410 }
411 
412 static const struct usb_device_id onboard_hub_id_table[] = {
413 	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B 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 	{}
421 };
422 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
423 
424 static struct usb_device_driver onboard_hub_usbdev_driver = {
425 	.name = "onboard-usb-hub",
426 	.probe = onboard_hub_usbdev_probe,
427 	.disconnect = onboard_hub_usbdev_disconnect,
428 	.generic_subclass = 1,
429 	.supports_autosuspend =	1,
430 	.id_table = onboard_hub_id_table,
431 };
432 
433 static int __init onboard_hub_init(void)
434 {
435 	int ret;
436 
437 	ret = platform_driver_register(&onboard_hub_driver);
438 	if (ret)
439 		return ret;
440 
441 	ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
442 	if (ret)
443 		platform_driver_unregister(&onboard_hub_driver);
444 
445 	return ret;
446 }
447 module_init(onboard_hub_init);
448 
449 static void __exit onboard_hub_exit(void)
450 {
451 	usb_deregister_device_driver(&onboard_hub_usbdev_driver);
452 	platform_driver_unregister(&onboard_hub_driver);
453 }
454 module_exit(onboard_hub_exit);
455 
456 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
457 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
458 MODULE_LICENSE("GPL v2");
459