class.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. struct usb_role_switch *sw;
  91. sw = device_connection_find_match(dev, "usb-role-switch", NULL,
  92. usb_role_switch_match);
  93. if (!IS_ERR_OR_NULL(sw))
  94. WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
  95. return sw;
  96. }
  97. EXPORT_SYMBOL_GPL(usb_role_switch_get);
  98. /**
  99. * usb_role_switch_put - Release handle to a switch
  100. * @sw: USB Role Switch
  101. *
  102. * Decrement reference count for @sw.
  103. */
  104. void usb_role_switch_put(struct usb_role_switch *sw)
  105. {
  106. if (!IS_ERR_OR_NULL(sw)) {
  107. module_put(sw->dev.parent->driver->owner);
  108. put_device(&sw->dev);
  109. }
  110. }
  111. EXPORT_SYMBOL_GPL(usb_role_switch_put);
  112. static umode_t
  113. usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  114. {
  115. struct device *dev = container_of(kobj, typeof(*dev), kobj);
  116. struct usb_role_switch *sw = to_role_switch(dev);
  117. if (sw->allow_userspace_control)
  118. return attr->mode;
  119. return 0;
  120. }
  121. static const char * const usb_roles[] = {
  122. [USB_ROLE_NONE] = "none",
  123. [USB_ROLE_HOST] = "host",
  124. [USB_ROLE_DEVICE] = "device",
  125. };
  126. static ssize_t
  127. role_show(struct device *dev, struct device_attribute *attr, char *buf)
  128. {
  129. struct usb_role_switch *sw = to_role_switch(dev);
  130. enum usb_role role = usb_role_switch_get_role(sw);
  131. return sprintf(buf, "%s\n", usb_roles[role]);
  132. }
  133. static ssize_t role_store(struct device *dev, struct device_attribute *attr,
  134. const char *buf, size_t size)
  135. {
  136. struct usb_role_switch *sw = to_role_switch(dev);
  137. int ret;
  138. ret = sysfs_match_string(usb_roles, buf);
  139. if (ret < 0) {
  140. bool res;
  141. /* Extra check if the user wants to disable the switch */
  142. ret = kstrtobool(buf, &res);
  143. if (ret || res)
  144. return -EINVAL;
  145. }
  146. ret = usb_role_switch_set_role(sw, ret);
  147. if (ret)
  148. return ret;
  149. return size;
  150. }
  151. static DEVICE_ATTR_RW(role);
  152. static struct attribute *usb_role_switch_attrs[] = {
  153. &dev_attr_role.attr,
  154. NULL,
  155. };
  156. static const struct attribute_group usb_role_switch_group = {
  157. .is_visible = usb_role_switch_is_visible,
  158. .attrs = usb_role_switch_attrs,
  159. };
  160. static const struct attribute_group *usb_role_switch_groups[] = {
  161. &usb_role_switch_group,
  162. NULL,
  163. };
  164. static int
  165. usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
  166. {
  167. int ret;
  168. ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
  169. if (ret)
  170. dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
  171. return ret;
  172. }
  173. static void usb_role_switch_release(struct device *dev)
  174. {
  175. struct usb_role_switch *sw = to_role_switch(dev);
  176. kfree(sw);
  177. }
  178. static const struct device_type usb_role_dev_type = {
  179. .name = "usb_role_switch",
  180. .groups = usb_role_switch_groups,
  181. .uevent = usb_role_switch_uevent,
  182. .release = usb_role_switch_release,
  183. };
  184. /**
  185. * usb_role_switch_register - Register USB Role Switch
  186. * @parent: Parent device for the switch
  187. * @desc: Description of the switch
  188. *
  189. * USB Role Switch is a device capable or choosing the role for USB connector.
  190. * On platforms where the USB controller is dual-role capable, the controller
  191. * driver will need to register the switch. On platforms where the USB host and
  192. * USB device controllers behind the connector are separate, there will be a
  193. * mux, and the driver for that mux will need to register the switch.
  194. *
  195. * Returns handle to a new role switch or ERR_PTR. The content of @desc is
  196. * copied.
  197. */
  198. struct usb_role_switch *
  199. usb_role_switch_register(struct device *parent,
  200. const struct usb_role_switch_desc *desc)
  201. {
  202. struct usb_role_switch *sw;
  203. int ret;
  204. if (!desc || !desc->set)
  205. return ERR_PTR(-EINVAL);
  206. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  207. if (!sw)
  208. return ERR_PTR(-ENOMEM);
  209. mutex_init(&sw->lock);
  210. sw->allow_userspace_control = desc->allow_userspace_control;
  211. sw->usb2_port = desc->usb2_port;
  212. sw->usb3_port = desc->usb3_port;
  213. sw->udc = desc->udc;
  214. sw->set = desc->set;
  215. sw->get = desc->get;
  216. sw->dev.parent = parent;
  217. sw->dev.class = role_class;
  218. sw->dev.type = &usb_role_dev_type;
  219. dev_set_name(&sw->dev, "%s-role-switch", dev_name(parent));
  220. ret = device_register(&sw->dev);
  221. if (ret) {
  222. put_device(&sw->dev);
  223. return ERR_PTR(ret);
  224. }
  225. /* TODO: Symlinks for the host port and the device controller. */
  226. return sw;
  227. }
  228. EXPORT_SYMBOL_GPL(usb_role_switch_register);
  229. /**
  230. * usb_role_switch_unregister - Unregsiter USB Role Switch
  231. * @sw: USB Role Switch
  232. *
  233. * Unregister switch that was registered with usb_role_switch_register().
  234. */
  235. void usb_role_switch_unregister(struct usb_role_switch *sw)
  236. {
  237. if (!IS_ERR_OR_NULL(sw))
  238. device_unregister(&sw->dev);
  239. }
  240. EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
  241. static int __init usb_roles_init(void)
  242. {
  243. role_class = class_create(THIS_MODULE, "usb_role");
  244. return PTR_ERR_OR_ZERO(role_class);
  245. }
  246. subsys_initcall(usb_roles_init);
  247. static void __exit usb_roles_exit(void)
  248. {
  249. class_destroy(role_class);
  250. }
  251. module_exit(usb_roles_exit);
  252. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  253. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  254. MODULE_LICENSE("GPL v2");
  255. MODULE_DESCRIPTION("USB Role Class");