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