]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/usb/misc/onboard_usb_hub.c
KVM: clean up directives to compile out irqfds
[thirdparty/kernel/stable.git] / drivers / usb / misc / onboard_usb_hub.c
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
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
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
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
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
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
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
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
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
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
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
241 static int onboard_hub_probe(struct platform_device *pdev)
242 {
243 struct device *dev = &pdev->dev;
244 struct onboard_hub *hub;
245 unsigned int i;
246 int err;
247
248 hub = devm_kzalloc(dev, sizeof(*hub), GFP_KERNEL);
249 if (!hub)
250 return -ENOMEM;
251
252 hub->pdata = device_get_match_data(&pdev->dev);
253 if (!hub->pdata)
254 return -EINVAL;
255
256 if (hub->pdata->num_supplies > MAX_SUPPLIES)
257 return dev_err_probe(dev, -EINVAL, "max %zu supplies supported!\n",
258 MAX_SUPPLIES);
259
260 for (i = 0; i < hub->pdata->num_supplies; i++)
261 hub->supplies[i].supply = supply_names[i];
262
263 err = devm_regulator_bulk_get(dev, hub->pdata->num_supplies, hub->supplies);
264 if (err) {
265 dev_err(dev, "Failed to get regulator supplies: %d\n", err);
266 return err;
267 }
268
269 hub->reset_gpio = devm_gpiod_get_optional(dev, "reset",
270 GPIOD_OUT_HIGH);
271 if (IS_ERR(hub->reset_gpio))
272 return dev_err_probe(dev, PTR_ERR(hub->reset_gpio), "failed to get reset GPIO\n");
273
274 hub->dev = dev;
275 mutex_init(&hub->lock);
276 INIT_LIST_HEAD(&hub->udev_list);
277
278 dev_set_drvdata(dev, hub);
279
280 err = onboard_hub_power_on(hub);
281 if (err)
282 return err;
283
284 /*
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.
288 *
289 * This needs to be done deferred to avoid self-deadlocks on systems
290 * with nested onboard hubs.
291 */
292 schedule_work(&attach_usb_driver_work);
293
294 return 0;
295 }
296
297 static void onboard_hub_remove(struct platform_device *pdev)
298 {
299 struct onboard_hub *hub = dev_get_drvdata(&pdev->dev);
300 struct usbdev_node *node;
301 struct usb_device *udev;
302
303 hub->going_away = true;
304
305 mutex_lock(&hub->lock);
306
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);
310 udev = node->udev;
311
312 /*
313 * Unbinding the driver will call onboard_hub_remove_usbdev(),
314 * which acquires hub->lock. We must release the lock first.
315 */
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);
321 }
322
323 mutex_unlock(&hub->lock);
324
325 onboard_hub_power_off(hub);
326 }
327
328 MODULE_DEVICE_TABLE(of, onboard_hub_match);
329
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)
332 };
333
334 static struct platform_driver onboard_hub_driver = {
335 .probe = onboard_hub_probe,
336 .remove_new = onboard_hub_remove,
337
338 .driver = {
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,
343 },
344 };
345
346 /************************** USB driver **************************/
347
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
354
355 /*
356 * Returns the onboard_hub platform device that is associated with the USB
357 * device passed as parameter.
358 */
359 static struct onboard_hub *_find_onboard_hub(struct device *dev)
360 {
361 struct platform_device *pdev;
362 struct device_node *np;
363 struct onboard_hub *hub;
364
365 pdev = of_find_device_by_node(dev->of_node);
366 if (!pdev) {
367 np = of_parse_phandle(dev->of_node, "peer-hub", 0);
368 if (!np) {
369 dev_err(dev, "failed to find device node for peer hub\n");
370 return ERR_PTR(-EINVAL);
371 }
372
373 pdev = of_find_device_by_node(np);
374 of_node_put(np);
375
376 if (!pdev)
377 return ERR_PTR(-ENODEV);
378 }
379
380 hub = dev_get_drvdata(&pdev->dev);
381 put_device(&pdev->dev);
382
383 /*
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
388 * try again.
389 */
390 if (!hub)
391 return ERR_PTR(-EPROBE_DEFER);
392
393 return hub;
394 }
395
396 static int onboard_hub_usbdev_probe(struct usb_device *udev)
397 {
398 struct device *dev = &udev->dev;
399 struct onboard_hub *hub;
400 int err;
401
402 /* ignore supported hubs without device tree node */
403 if (!dev->of_node)
404 return -ENODEV;
405
406 hub = _find_onboard_hub(dev);
407 if (IS_ERR(hub))
408 return PTR_ERR(hub);
409
410 dev_set_drvdata(dev, hub);
411
412 err = onboard_hub_add_usbdev(hub, udev);
413 if (err)
414 return err;
415
416 return 0;
417 }
418
419 static void onboard_hub_usbdev_disconnect(struct usb_device *udev)
420 {
421 struct onboard_hub *hub = dev_get_drvdata(&udev->dev);
422
423 onboard_hub_remove_usbdev(hub, udev);
424 }
425
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 */
443 {}
444 };
445 MODULE_DEVICE_TABLE(usb, onboard_hub_id_table);
446
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,
454 };
455
456 static int __init onboard_hub_init(void)
457 {
458 int ret;
459
460 ret = usb_register_device_driver(&onboard_hub_usbdev_driver, THIS_MODULE);
461 if (ret)
462 return ret;
463
464 ret = platform_driver_register(&onboard_hub_driver);
465 if (ret)
466 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
467
468 return ret;
469 }
470 module_init(onboard_hub_init);
471
472 static void __exit onboard_hub_exit(void)
473 {
474 usb_deregister_device_driver(&onboard_hub_usbdev_driver);
475 platform_driver_unregister(&onboard_hub_driver);
476
477 cancel_work_sync(&attach_usb_driver_work);
478 }
479 module_exit(onboard_hub_exit);
480
481 MODULE_AUTHOR("Matthias Kaehlcke <mka@chromium.org>");
482 MODULE_DESCRIPTION("Driver for discrete onboard USB hubs");
483 MODULE_LICENSE("GPL v2");