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 	gpiod_set_value_cansleep(hub->reset_gpio, 1);
75 
76 	err = regulator_disable(hub->vdd);
77 	if (err) {
78 		dev_err(hub->dev, "failed to disable regulator: %d\n", err);
79 		return err;
80 	}
81 
82 	hub->is_powered_on = false;
83 
84 	return 0;
85 }
86 
87 static int __maybe_unused onboard_hub_suspend(struct device *dev)
88 {
89 	struct onboard_hub *hub = dev_get_drvdata(dev);
90 	struct usbdev_node *node;
91 	bool power_off = true;
92 
93 	if (hub->always_powered_in_suspend)
94 		return 0;
95 
96 	mutex_lock(&hub->lock);
97 
98 	list_for_each_entry(node, &hub->udev_list, list) {
99 		if (!device_may_wakeup(node->udev->bus->controller))
100 			continue;
101 
102 		if (usb_wakeup_enabled_descendants(node->udev)) {
103 			power_off = false;
104 			break;
105 		}
106 	}
107 
108 	mutex_unlock(&hub->lock);
109 
110 	if (!power_off)
111 		return 0;
112 
113 	return onboard_hub_power_off(hub);
114 }
115 
116 static int __maybe_unused onboard_hub_resume(struct device *dev)
117 {
118 	struct onboard_hub *hub = dev_get_drvdata(dev);
119 
120 	if (hub->is_powered_on)
121 		return 0;
122 
123 	return onboard_hub_power_on(hub);
124 }
125 
126 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
127 {
128 	snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
129 }
130 
131 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
132 {
133 	struct usbdev_node *node;
134 	char link_name[64];
135 	int err;
136 
137 	mutex_lock(&hub->lock);
138 
139 	if (hub->going_away) {
140 		err = -EINVAL;
141 		goto error;
142 	}
143 
144 	node = kzalloc(sizeof(*node), GFP_KERNEL);
145 	if (!node) {
146 		err = -ENOMEM;
147 		goto error;
148 	}
149 
150 	node->udev = udev;
151 
152 	list_add(&node->list, &hub->udev_list);
153 
154 	mutex_unlock(&hub->lock);
155 
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));
158 
159 	return 0;
160 
161 error:
162 	mutex_unlock(&hub->lock);
163 
164 	return err;
165 }
166 
167 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
168 {
169 	struct usbdev_node *node;
170 	char link_name[64];
171 
172 	get_udev_link_name(udev, link_name, sizeof(link_name));
173 	sysfs_remove_link(&hub->dev->kobj, link_name);
174 
175 	mutex_lock(&hub->lock);
176 
177 	list_for_each_entry(node, &hub->udev_list, list) {
178 		if (node->udev == udev) {
179 			list_del(&node->list);
180 			kfree(node);
181 			break;
182 		}
183 	}
184 
185 	mutex_unlock(&hub->lock);
186 }
187 
188 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
189 			   char *buf)
190 {
191 	const struct onboard_hub *hub = dev_get_drvdata(dev);
192 
193 	return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
194 }
195 
196 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
197 			    const char *buf, size_t count)
198 {
199 	struct onboard_hub *hub = dev_get_drvdata(dev);
200 	bool val;
201 	int ret;
202 
203 	ret = kstrtobool(buf, &val);
204 	if (ret < 0)
205 		return ret;
206 
207 	hub->always_powered_in_suspend = val;
208 
209 	return count;
210 }
211 static DEVICE_ATTR_RW(always_powered_in_suspend);
212 
213 static struct attribute *onboard_hub_attrs[] = {
214 	&dev_attr_always_powered_in_suspend.attr,
215 	NULL,
216 };
217 ATTRIBUTE_GROUPS(onboard_hub);
218 
219 static void onboard_hub_attach_usb_driver(struct work_struct *work)
220 {
221 	int err;
222 
223 	err = driver_attach(&onboard_hub_usbdev_driver.drvwrap.driver);
224 	if (err)
225 		pr_err("Failed to attach USB driver: %d\n", err);
226 }
227 
228 static int onboard_hub_probe(struct platform_device *pdev)
229 {
230 	const struct of_device_id *of_id;
231 	struct device *dev = &pdev->dev;
232 	struct onboard_hub *hub;
233 	int err;
234 
235 	hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
236 	if (!hub)
237 		return -ENOMEM;
238 
239 	of_id = of_match_device(onboard_hub_match, &pdev->dev);
240 	if (!of_id)
241 		return -ENODEV;
242 
243 	hub->pdata = of_id->data;
244 	if (!hub->pdata)
245 		return -EINVAL;
246 
247 	hub->vdd = devm_regulator_get(dev, "vdd");
248 	if (IS_ERR(hub->vdd))
249 		return PTR_ERR(hub->vdd);
250 
251 	hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
252 						  GPIOD_OUT_HIGH);
253 	if (IS_ERR(hub->reset_gpio))
254 		return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
255 
256 	hub->dev = dev;
257 	mutex_init(&hub->lock);
258 	INIT_LIST_HEAD(&hub->udev_list);
259 
260 	dev_set_drvdata(dev, hub);
261 
262 	err = onboard_hub_power_on(hub);
263 	if (err)
264 		return err;
265 
266 	/*
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.
270 	 *
271 	 * This needs to be done deferred to avoid self-deadlocks on systems
272 	 * with nested onboard hubs.
273 	 */
274 	INIT_WORK(&hub->attach_usb_driver_work, onboard_hub_attach_usb_driver);
275 	schedule_work(&hub->attach_usb_driver_work);
276 
277 	return 0;
278 }
279 
280 static int onboard_hub_remove(struct platform_device *pdev)
281 {
282 	struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
283 	struct usbdev_node *node;
284 	struct usb_device *udev;
285 
286 	hub->going_away = true;
287 
288 	if (&hub->attach_usb_driver_work != current_work())
289 		cancel_work_sync(&hub->attach_usb_driver_work);
290 
291 	mutex_lock(&hub->lock);
292 
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);
296 		udev = node->udev;
297 
298 		/*
299 		 * Unbinding the driver will call onboard_hub_remove_usbdev(),
300 		 * which acquires hub->lock.  We must release the lock first.
301 		 */
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);
307 	}
308 
309 	mutex_unlock(&hub->lock);
310 
311 	return onboard_hub_power_off(hub);
312 }
313 
314 MODULE_DEVICE_TABLE(of, onboard_hub_match);
315 
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)
318 };
319 
320 static struct platform_driver onboard_hub_driver = {
321 	.probe = onboard_hub_probe,
322 	.remove = onboard_hub_remove,
323 
324 	.driver = {
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,
329 	},
330 };
331 
332 /************************** USB driver **************************/
333 
334 #define VENDOR_ID_MICROCHIP	0x0424
335 #define VENDOR_ID_REALTEK	0x0bda
336 #define VENDOR_ID_TI		0x0451
337 
338 /*
339  * Returns the onboard_hub platform device that is associated with the USB
340  * device passed as parameter.
341  */
342 static struct onboard_hub *_find_onboard_hub(struct device *dev)
343 {
344 	struct platform_device *pdev;
345 	struct device_node *np;
346 	struct onboard_hub *hub;
347 
348 	pdev = of_find_device_by_node(dev->of_node);
349 	if (!pdev) {
350 		np = of_parse_phandle(dev->of_node, "peer-hub", 0);
351 		if (!np) {
352 			dev_err(dev, "failed to find device node for peer hub\n");
353 			return ERR_PTR(-EINVAL);
354 		}
355 
356 		pdev = of_find_device_by_node(np);
357 		of_node_put(np);
358 
359 		if (!pdev)
360 			return ERR_PTR(-ENODEV);
361 	}
362 
363 	hub = dev_get_drvdata(&pdev->dev);
364 	put_device(&pdev->dev);
365 
366 	/*
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
371 	 * try again.
372 	 */
373 	if (!hub)
374 		return ERR_PTR(-EPROBE_DEFER);
375 
376 	return hub;
377 }
378 
379 static int onboard_hub_usbdev_probe(struct usb_device *udev)
380 {
381 	struct device *dev = &udev->dev;
382 	struct onboard_hub *hub;
383 	int err;
384 
385 	/* ignore supported hubs without device tree node */
386 	if (!dev->of_node)
387 		return -ENODEV;
388 
389 	hub = _find_onboard_hub(dev);
390 	if (IS_ERR(hub))
391 		return PTR_ERR(hub);
392 
393 	dev_set_drvdata(dev, hub);
394 
395 	err = onboard_hub_add_usbdev(hub, udev);
396 	if (err)
397 		return err;
398 
399 	return 0;
400 }
401 
402 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
403 {
404 	struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
405 
406 	onboard_hub_remove_usbdev(hub, udev);
407 }
408 
409 static const struct usb_device_id onboard_hub_id_table[] = {
410 	{ USB_DEVICE(VENDOR_ID_MICROCHIP, 0x2514) }, /* USB2514B USB 2.0 */
411 	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x0411) }, /* RTS5411 USB 3.1 */
412 	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x5411) }, /* RTS5411 USB 2.1 */
413 	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x0414) }, /* RTS5414 USB 3.2 */
414 	{ USB_DEVICE(VENDOR_ID_REALTEK, 0x5414) }, /* RTS5414 USB 2.1 */
415 	{ USB_DEVICE(VENDOR_ID_TI, 0x8140) }, /* TI USB8041 3.0 */
416 	{ USB_DEVICE(VENDOR_ID_TI, 0x8142) }, /* TI USB8041 2.0 */
417 	{}
418 };
419 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
420 
421 static struct usb_device_driver onboard_hub_usbdev_driver = {
422 	.name = "onboard-usb-hub",
423 	.probe = onboard_hub_usbdev_probe,
424 	.disconnect = onboard_hub_usbdev_disconnect,
425 	.generic_subclass = 1,
426 	.supports_autosuspend =	1,
427 	.id_table = onboard_hub_id_table,
428 };
429 
430 static int __init onboard_hub_init(void)
431 {
432 	int ret;
433 
434 	ret = platform_driver_register(&onboard_hub_driver);
435 	if (ret)
436 		return ret;
437 
438 	ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
439 	if (ret)
440 		platform_driver_unregister(&onboard_hub_driver);
441 
442 	return ret;
443 }
444 module_init(onboard_hub_init);
445 
446 static void __exit onboard_hub_exit(void)
447 {
448 	usb_deregister_device_driver(&onboard_hub_usbdev_driver);
449 	platform_driver_unregister(&onboard_hub_driver);
450 }
451 module_exit(onboard_hub_exit);
452 
453 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
454 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
455 MODULE_LICENSE("GPL v2");
456