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 void onboard_hub_attach_usb_driver(struct work_struct *work);
31 
32 static struct usb_device_driver onboard_hub_usbdev_driver;
33 static DECLARE_WORK(attach_usb_driver_work, onboard_hub_attach_usb_driver);
34 
35 /************************** Platform driver **************************/
36 
37 struct usbdev_node {
38 	struct usb_device *udev;
39 	struct list_head list;
40 };
41 
42 struct onboard_hub {
43 	struct regulator *vdd;
44 	struct device *dev;
45 	const struct onboard_hub_pdata *pdata;
46 	struct gpio_desc *reset_gpio;
47 	bool always_powered_in_suspend;
48 	bool is_powered_on;
49 	bool going_away;
50 	struct list_head udev_list;
51 	struct mutex lock;
52 };
53 
54 static int onboard_hub_power_on(struct onboard_hub *hub)
55 {
56 	int err;
57 
58 	err = regulator_enable(hub->vdd);
59 	if (err) {
60 		dev_err(hub->dev, "failed to enable regulator: %d\n", err);
61 		return err;
62 	}
63 
64 	fsleep(hub->pdata->reset_us);
65 	gpiod_set_value_cansleep(hub->reset_gpio, 0);
66 
67 	hub->is_powered_on = true;
68 
69 	return 0;
70 }
71 
72 static int onboard_hub_power_off(struct onboard_hub *hub)
73 {
74 	int err;
75 
76 	gpiod_set_value_cansleep(hub->reset_gpio, 1);
77 
78 	err = regulator_disable(hub->vdd);
79 	if (err) {
80 		dev_err(hub->dev, "failed to disable regulator: %d\n", err);
81 		return err;
82 	}
83 
84 	hub->is_powered_on = false;
85 
86 	return 0;
87 }
88 
89 static int __maybe_unused onboard_hub_suspend(struct device *dev)
90 {
91 	struct onboard_hub *hub = dev_get_drvdata(dev);
92 	struct usbdev_node *node;
93 	bool power_off = true;
94 
95 	if (hub->always_powered_in_suspend)
96 		return 0;
97 
98 	mutex_lock(&hub->lock);
99 
100 	list_for_each_entry(node, &hub->udev_list, list) {
101 		if (!device_may_wakeup(node->udev->bus->controller))
102 			continue;
103 
104 		if (usb_wakeup_enabled_descendants(node->udev)) {
105 			power_off = false;
106 			break;
107 		}
108 	}
109 
110 	mutex_unlock(&hub->lock);
111 
112 	if (!power_off)
113 		return 0;
114 
115 	return onboard_hub_power_off(hub);
116 }
117 
118 static int __maybe_unused onboard_hub_resume(struct device *dev)
119 {
120 	struct onboard_hub *hub = dev_get_drvdata(dev);
121 
122 	if (hub->is_powered_on)
123 		return 0;
124 
125 	return onboard_hub_power_on(hub);
126 }
127 
128 static inline void get_udev_link_name(const struct usb_device *udev, char *buf, size_t size)
129 {
130 	snprintf(buf, size, "usb_dev.%s", dev_name(&udev->dev));
131 }
132 
133 static int onboard_hub_add_usbdev(struct onboard_hub *hub, struct usb_device *udev)
134 {
135 	struct usbdev_node *node;
136 	char link_name[64];
137 	int err;
138 
139 	mutex_lock(&hub->lock);
140 
141 	if (hub->going_away) {
142 		err = -EINVAL;
143 		goto error;
144 	}
145 
146 	node = kzalloc(sizeof(*node), GFP_KERNEL);
147 	if (!node) {
148 		err = -ENOMEM;
149 		goto error;
150 	}
151 
152 	node->udev = udev;
153 
154 	list_add(&node->list, &hub->udev_list);
155 
156 	mutex_unlock(&hub->lock);
157 
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));
160 
161 	return 0;
162 
163 error:
164 	mutex_unlock(&hub->lock);
165 
166 	return err;
167 }
168 
169 static void onboard_hub_remove_usbdev(struct onboard_hub *hub, const struct usb_device *udev)
170 {
171 	struct usbdev_node *node;
172 	char link_name[64];
173 
174 	get_udev_link_name(udev, link_name, sizeof(link_name));
175 	sysfs_remove_link(&hub->dev->kobj, link_name);
176 
177 	mutex_lock(&hub->lock);
178 
179 	list_for_each_entry(node, &hub->udev_list, list) {
180 		if (node->udev == udev) {
181 			list_del(&node->list);
182 			kfree(node);
183 			break;
184 		}
185 	}
186 
187 	mutex_unlock(&hub->lock);
188 }
189 
190 static ssize_t always_powered_in_suspend_show(struct device *dev, struct device_attribute *attr,
191 			   char *buf)
192 {
193 	const struct onboard_hub *hub = dev_get_drvdata(dev);
194 
195 	return sysfs_emit(buf, "%d\n", hub->always_powered_in_suspend);
196 }
197 
198 static ssize_t always_powered_in_suspend_store(struct device *dev, struct device_attribute *attr,
199 			    const char *buf, size_t count)
200 {
201 	struct onboard_hub *hub = dev_get_drvdata(dev);
202 	bool val;
203 	int ret;
204 
205 	ret = kstrtobool(buf, &val);
206 	if (ret < 0)
207 		return ret;
208 
209 	hub->always_powered_in_suspend = val;
210 
211 	return count;
212 }
213 static DEVICE_ATTR_RW(always_powered_in_suspend);
214 
215 static struct attribute *onboard_hub_attrs[] = {
216 	&dev_attr_always_powered_in_suspend.attr,
217 	NULL,
218 };
219 ATTRIBUTE_GROUPS(onboard_hub);
220 
221 static void onboard_hub_attach_usb_driver(struct work_struct *work)
222 {
223 	int err;
224 
225 	err = driver_attach(&onboard_hub_usbdev_driver.drvwrap.driver);
226 	if (err)
227 		pr_err("Failed to attach USB driver: %d\n", err);
228 }
229 
230 static int onboard_hub_probe(struct platform_device *pdev)
231 {
232 	const struct of_device_id *of_id;
233 	struct device *dev = &pdev->dev;
234 	struct onboard_hub *hub;
235 	int err;
236 
237 	hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
238 	if (!hub)
239 		return -ENOMEM;
240 
241 	of_id = of_match_device(onboard_hub_match, &pdev->dev);
242 	if (!of_id)
243 		return -ENODEV;
244 
245 	hub->pdata = of_id->data;
246 	if (!hub->pdata)
247 		return -EINVAL;
248 
249 	hub->vdd = devm_regulator_get(dev, "vdd");
250 	if (IS_ERR(hub->vdd))
251 		return PTR_ERR(hub->vdd);
252 
253 	hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
254 						  GPIOD_OUT_HIGH);
255 	if (IS_ERR(hub->reset_gpio))
256 		return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
257 
258 	hub->dev = dev;
259 	mutex_init(&hub->lock);
260 	INIT_LIST_HEAD(&hub->udev_list);
261 
262 	dev_set_drvdata(dev, hub);
263 
264 	err = onboard_hub_power_on(hub);
265 	if (err)
266 		return err;
267 
268 	/*
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.
272 	 *
273 	 * This needs to be done deferred to avoid self-deadlocks on systems
274 	 * with nested onboard hubs.
275 	 */
276 	schedule_work(&attach_usb_driver_work);
277 
278 	return 0;
279 }
280 
281 static void onboard_hub_remove(struct platform_device *pdev)
282 {
283 	struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
284 	struct usbdev_node *node;
285 	struct usb_device *udev;
286 
287 	hub->going_away = true;
288 
289 	mutex_lock(&hub->lock);
290 
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);
294 		udev = node->udev;
295 
296 		/*
297 		 * Unbinding the driver will call onboard_hub_remove_usbdev(),
298 		 * which acquires hub->lock.  We must release the lock first.
299 		 */
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);
305 	}
306 
307 	mutex_unlock(&hub->lock);
308 
309 	onboard_hub_power_off(hub);
310 }
311 
312 MODULE_DEVICE_TABLE(of, onboard_hub_match);
313 
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)
316 };
317 
318 static struct platform_driver onboard_hub_driver = {
319 	.probe = onboard_hub_probe,
320 	.remove_new = onboard_hub_remove,
321 
322 	.driver = {
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,
327 	},
328 };
329 
330 /************************** USB driver **************************/
331 
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
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_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 */
422 	{}
423 };
424 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
425 
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,
433 };
434 
435 static int __init onboard_hub_init(void)
436 {
437 	int ret;
438 
439 	ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
440 	if (ret)
441 		return ret;
442 
443 	ret = platform_driver_register(&onboard_hub_driver);
444 	if (ret)
445 		usb_deregister_device_driver(&onboard_hub_usbdev_driver);
446 
447 	return ret;
448 }
449 module_init(onboard_hub_init);
450 
451 static void __exit onboard_hub_exit(void)
452 {
453 	usb_deregister_device_driver(&onboard_hub_usbdev_driver);
454 	platform_driver_unregister(&onboard_hub_driver);
455 
456 	cancel_work_sync(&attach_usb_driver_work);
457 }
458 module_exit(onboard_hub_exit);
459 
460 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
461 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
462 MODULE_LICENSE("GPL v2");
463