onboard_usb_hub.c 11 KB

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