roles.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB Role Switch Support
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7. * Hans de Goede <hdegoede@redhat.com>
  8. */
  9. #include <linux/usb/role.h>
  10. #include <linux/device.h>
  11. #include <linux/module.h>
  12. #include <linux/mutex.h>
  13. #include <linux/slab.h>
  14. static struct class *role_class;
  15. struct usb_role_switch {
  16. struct device dev;
  17. struct mutex lock; /* device lock*/
  18. enum usb_role role;
  19. /* From descriptor */
  20. struct device *usb2_port;
  21. struct device *usb3_port;
  22. struct device *udc;
  23. usb_role_switch_set_t set;
  24. usb_role_switch_get_t get;
  25. bool allow_userspace_control;
  26. };
  27. #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
  28. /**
  29. * usb_role_switch_set_role - Set USB role for a switch
  30. * @sw: USB role switch
  31. * @role: USB role to be switched to
  32. *
  33. * Set USB role @role for @sw.
  34. */
  35. int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
  36. {
  37. int ret;
  38. if (IS_ERR_OR_NULL(sw))
  39. return 0;
  40. mutex_lock(&sw->lock);
  41. ret = sw->set(sw->dev.parent, role);
  42. if (!ret)
  43. sw->role = role;
  44. mutex_unlock(&sw->lock);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
  48. /**
  49. * usb_role_switch_get_role - Get the USB role for a switch
  50. * @sw: USB role switch
  51. *
  52. * Depending on the role-switch-driver this function returns either a cached
  53. * value of the last set role, or reads back the actual value from the hardware.
  54. */
  55. enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
  56. {
  57. enum usb_role role;
  58. if (IS_ERR_OR_NULL(sw))
  59. return USB_ROLE_NONE;
  60. mutex_lock(&sw->lock);
  61. if (sw->get)
  62. role = sw->get(sw->dev.parent);
  63. else
  64. role = sw->role;
  65. mutex_unlock(&sw->lock);
  66. return role;
  67. }
  68. EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
  69. static int __switch_match(struct device *dev, const void *name)
  70. {
  71. return !strcmp((const char *)name, dev_name(dev));
  72. }
  73. static void *usb_role_switch_match(struct device_connection *con, int ep,
  74. void *data)
  75. {
  76. struct device *dev;
  77. dev = class_find_device(role_class, NULL, con->endpoint[ep],
  78. __switch_match);
  79. return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
  80. }
  81. /**
  82. * usb_role_switch_get - Find USB role switch linked with the caller
  83. * @dev: The caller device
  84. *
  85. * Finds and returns role switch linked with @dev. The reference count for the
  86. * found switch is incremented.
  87. */
  88. struct usb_role_switch *usb_role_switch_get(struct device *dev)
  89. {
  90. return device_connection_find_match(dev, "usb-role-switch", NULL,
  91. usb_role_switch_match);
  92. }
  93. EXPORT_SYMBOL_GPL(usb_role_switch_get);
  94. /**
  95. * usb_role_switch_put - Release handle to a switch
  96. * @sw: USB Role Switch
  97. *
  98. * Decrement reference count for @sw.
  99. */
  100. void usb_role_switch_put(struct usb_role_switch *sw)
  101. {
  102. if (!IS_ERR_OR_NULL(sw))
  103. put_device(&sw->dev);
  104. }
  105. EXPORT_SYMBOL_GPL(usb_role_switch_put);
  106. static umode_t
  107. usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  108. {
  109. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  110. struct usb_role_switch *sw = to_role_switch(dev);
  111. if (sw->allow_userspace_control)
  112. return attr->mode;
  113. return 0;
  114. }
  115. static const char * const usb_roles[] = {
  116. [USB_ROLE_NONE] = "none",
  117. [USB_ROLE_HOST] = "host",
  118. [USB_ROLE_DEVICE] = "device",
  119. };
  120. static ssize_t
  121. role_show(struct device *dev, struct device_attribute *attr, char *buf)
  122. {
  123. struct usb_role_switch *sw = to_role_switch(dev);
  124. enum usb_role role = usb_role_switch_get_role(sw);
  125. return sprintf(buf, "%s\n", usb_roles[role]);
  126. }
  127. static ssize_t role_store(struct device *dev, struct device_attribute *attr,
  128. const char *buf, size_t size)
  129. {
  130. struct usb_role_switch *sw = to_role_switch(dev);
  131. int ret;
  132. ret = sysfs_match_string(usb_roles, buf);
  133. if (ret < 0) {
  134. bool res;
  135. /* Extra check if the user wants to disable the switch */
  136. ret = kstrtobool(buf, &res);
  137. if (ret || res)
  138. return -EINVAL;
  139. }
  140. ret = usb_role_switch_set_role(sw, ret);
  141. if (ret)
  142. return ret;
  143. return size;
  144. }
  145. static DEVICE_ATTR_RW(role);
  146. static struct attribute *usb_role_switch_attrs[] = {
  147. &dev_attr_role.attr,
  148. NULL,
  149. };
  150. static const struct attribute_group usb_role_switch_group = {
  151. .is_visible = usb_role_switch_is_visible,
  152. .attrs = usb_role_switch_attrs,
  153. };
  154. static const struct attribute_group *usb_role_switch_groups[] = {
  155. &usb_role_switch_group,
  156. NULL,
  157. };
  158. static int
  159. usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
  160. {
  161. int ret;
  162. ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
  163. if (ret)
  164. dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
  165. return ret;
  166. }
  167. static void usb_role_switch_release(struct device *dev)
  168. {
  169. struct usb_role_switch *sw = to_role_switch(dev);
  170. kfree(sw);
  171. }
  172. static const struct device_type usb_role_dev_type = {
  173. .name = "usb_role_switch",
  174. .groups = usb_role_switch_groups,
  175. .uevent = usb_role_switch_uevent,
  176. .release = usb_role_switch_release,
  177. };
  178. /**
  179. * usb_role_switch_register - Register USB Role Switch
  180. * @parent: Parent device for the switch
  181. * @desc: Description of the switch
  182. *
  183. * USB Role Switch is a device capable or choosing the role for USB connector.
  184. * On platforms where the USB controller is dual-role capable, the controller
  185. * driver will need to register the switch. On platforms where the USB host and
  186. * USB device controllers behind the connector are separate, there will be a
  187. * mux, and the driver for that mux will need to register the switch.
  188. *
  189. * Returns handle to a new role switch or ERR_PTR. The content of @desc is
  190. * copied.
  191. */
  192. struct usb_role_switch *
  193. usb_role_switch_register(struct device *parent,
  194. const struct usb_role_switch_desc *desc)
  195. {
  196. struct usb_role_switch *sw;
  197. int ret;
  198. if (!desc || !desc->set)
  199. return ERR_PTR(-EINVAL);
  200. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  201. if (!sw)
  202. return ERR_PTR(-ENOMEM);
  203. mutex_init(&sw->lock);
  204. sw->allow_userspace_control = desc->allow_userspace_control;
  205. sw->usb2_port = desc->usb2_port;
  206. sw->usb3_port = desc->usb3_port;
  207. sw->udc = desc->udc;
  208. sw->set = desc->set;
  209. sw->get = desc->get;
  210. sw->dev.parent = parent;
  211. sw->dev.class = role_class;
  212. sw->dev.type = &usb_role_dev_type;
  213. dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
  214. ret = device_register(&sw->dev);
  215. if (ret) {
  216. put_device(&sw->dev);
  217. return ERR_PTR(ret);
  218. }
  219. /* TODO: Symlinks for the host port and the device controller. */
  220. return sw;
  221. }
  222. EXPORT_SYMBOL_GPL(usb_role_switch_register);
  223. /**
  224. * usb_role_switch_unregister - Unregsiter USB Role Switch
  225. * @sw: USB Role Switch
  226. *
  227. * Unregister switch that was registered with usb_role_switch_register().
  228. */
  229. void usb_role_switch_unregister(struct usb_role_switch *sw)
  230. {
  231. if (!IS_ERR_OR_NULL(sw))
  232. device_unregister(&sw->dev);
  233. }
  234. EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
  235. static int __init usb_roles_init(void)
  236. {
  237. role_class = class_create(THIS_MODULE, "usb_role");
  238. return PTR_ERR_OR_ZERO(role_class);
  239. }
  240. subsys_initcall(usb_roles_init);
  241. static void __exit usb_roles_exit(void)
  242. {
  243. class_destroy(role_class);
  244. }
  245. module_exit(usb_roles_exit);
  246. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  247. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  248. MODULE_LICENSE("GPL v2");
  249. MODULE_DESCRIPTION("USB Role Class");