class.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB Role Switch Support
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. * Author: Heikki Krogerus <[email protected]>
  7. * Hans de Goede <[email protected]>
  8. */
  9. #include <linux/usb/role.h>
  10. #include <linux/property.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/mutex.h>
  14. #include <linux/slab.h>
  15. static struct class *role_class;
  16. struct usb_role_switch {
  17. struct device dev;
  18. struct mutex lock; /* device lock*/
  19. enum usb_role role;
  20. /* From descriptor */
  21. struct device *usb2_port;
  22. struct device *usb3_port;
  23. struct device *udc;
  24. usb_role_switch_set_t set;
  25. usb_role_switch_get_t get;
  26. bool allow_userspace_control;
  27. };
  28. #define to_role_switch(d) container_of(d, struct usb_role_switch, dev)
  29. /**
  30. * usb_role_switch_set_role - Set USB role for a switch
  31. * @sw: USB role switch
  32. * @role: USB role to be switched to
  33. *
  34. * Set USB role @role for @sw.
  35. */
  36. int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role)
  37. {
  38. int ret;
  39. if (IS_ERR_OR_NULL(sw))
  40. return 0;
  41. mutex_lock(&sw->lock);
  42. ret = sw->set(sw, role);
  43. if (!ret) {
  44. sw->role = role;
  45. kobject_uevent(&sw->dev.kobj, KOBJ_CHANGE);
  46. }
  47. mutex_unlock(&sw->lock);
  48. return ret;
  49. }
  50. EXPORT_SYMBOL_GPL(usb_role_switch_set_role);
  51. /**
  52. * usb_role_switch_get_role - Get the USB role for a switch
  53. * @sw: USB role switch
  54. *
  55. * Depending on the role-switch-driver this function returns either a cached
  56. * value of the last set role, or reads back the actual value from the hardware.
  57. */
  58. enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw)
  59. {
  60. enum usb_role role;
  61. if (IS_ERR_OR_NULL(sw))
  62. return USB_ROLE_NONE;
  63. mutex_lock(&sw->lock);
  64. if (sw->get)
  65. role = sw->get(sw);
  66. else
  67. role = sw->role;
  68. mutex_unlock(&sw->lock);
  69. return role;
  70. }
  71. EXPORT_SYMBOL_GPL(usb_role_switch_get_role);
  72. static void *usb_role_switch_match(struct fwnode_handle *fwnode, const char *id,
  73. void *data)
  74. {
  75. struct device *dev;
  76. if (id && !fwnode_property_present(fwnode, id))
  77. return NULL;
  78. dev = class_find_device_by_fwnode(role_class, fwnode);
  79. return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
  80. }
  81. static struct usb_role_switch *
  82. usb_role_switch_is_parent(struct fwnode_handle *fwnode)
  83. {
  84. struct fwnode_handle *parent = fwnode_get_parent(fwnode);
  85. struct device *dev;
  86. if (!fwnode_property_present(parent, "usb-role-switch")) {
  87. fwnode_handle_put(parent);
  88. return NULL;
  89. }
  90. dev = class_find_device_by_fwnode(role_class, parent);
  91. fwnode_handle_put(parent);
  92. return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
  93. }
  94. /**
  95. * usb_role_switch_get - Find USB role switch linked with the caller
  96. * @dev: The caller device
  97. *
  98. * Finds and returns role switch linked with @dev. The reference count for the
  99. * found switch is incremented.
  100. */
  101. struct usb_role_switch *usb_role_switch_get(struct device *dev)
  102. {
  103. struct usb_role_switch *sw;
  104. sw = usb_role_switch_is_parent(dev_fwnode(dev));
  105. if (!sw)
  106. sw = device_connection_find_match(dev, "usb-role-switch", NULL,
  107. usb_role_switch_match);
  108. if (!IS_ERR_OR_NULL(sw))
  109. WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
  110. return sw;
  111. }
  112. EXPORT_SYMBOL_GPL(usb_role_switch_get);
  113. /**
  114. * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
  115. * @fwnode: The caller device node
  116. *
  117. * This is similar to the usb_role_switch_get() function above, but it searches
  118. * the switch using fwnode instead of device entry.
  119. */
  120. struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
  121. {
  122. struct usb_role_switch *sw;
  123. sw = usb_role_switch_is_parent(fwnode);
  124. if (!sw)
  125. sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
  126. NULL, usb_role_switch_match);
  127. if (!IS_ERR_OR_NULL(sw))
  128. WARN_ON(!try_module_get(sw->dev.parent->driver->owner));
  129. return sw;
  130. }
  131. EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
  132. /**
  133. * usb_role_switch_put - Release handle to a switch
  134. * @sw: USB Role Switch
  135. *
  136. * Decrement reference count for @sw.
  137. */
  138. void usb_role_switch_put(struct usb_role_switch *sw)
  139. {
  140. if (!IS_ERR_OR_NULL(sw)) {
  141. module_put(sw->dev.parent->driver->owner);
  142. put_device(&sw->dev);
  143. }
  144. }
  145. EXPORT_SYMBOL_GPL(usb_role_switch_put);
  146. /**
  147. * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
  148. * @fwnode: fwnode of the USB Role Switch
  149. *
  150. * Finds and returns role switch with @fwnode. The reference count for the
  151. * found switch is incremented.
  152. */
  153. struct usb_role_switch *
  154. usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
  155. {
  156. struct device *dev;
  157. if (!fwnode)
  158. return NULL;
  159. dev = class_find_device_by_fwnode(role_class, fwnode);
  160. if (dev)
  161. WARN_ON(!try_module_get(dev->parent->driver->owner));
  162. return dev ? to_role_switch(dev) : NULL;
  163. }
  164. EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
  165. static umode_t
  166. usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  167. {
  168. struct device *dev = kobj_to_dev(kobj);
  169. struct usb_role_switch *sw = to_role_switch(dev);
  170. if (sw->allow_userspace_control)
  171. return attr->mode;
  172. return 0;
  173. }
  174. static const char * const usb_roles[] = {
  175. [USB_ROLE_NONE] = "none",
  176. [USB_ROLE_HOST] = "host",
  177. [USB_ROLE_DEVICE] = "device",
  178. };
  179. const char *usb_role_string(enum usb_role role)
  180. {
  181. if (role < 0 || role >= ARRAY_SIZE(usb_roles))
  182. return "unknown";
  183. return usb_roles[role];
  184. }
  185. EXPORT_SYMBOL_GPL(usb_role_string);
  186. static ssize_t
  187. role_show(struct device *dev, struct device_attribute *attr, char *buf)
  188. {
  189. struct usb_role_switch *sw = to_role_switch(dev);
  190. enum usb_role role = usb_role_switch_get_role(sw);
  191. return sprintf(buf, "%s\n", usb_roles[role]);
  192. }
  193. static ssize_t role_store(struct device *dev, struct device_attribute *attr,
  194. const char *buf, size_t size)
  195. {
  196. struct usb_role_switch *sw = to_role_switch(dev);
  197. int ret;
  198. ret = sysfs_match_string(usb_roles, buf);
  199. if (ret < 0) {
  200. bool res;
  201. /* Extra check if the user wants to disable the switch */
  202. ret = kstrtobool(buf, &res);
  203. if (ret || res)
  204. return -EINVAL;
  205. }
  206. ret = usb_role_switch_set_role(sw, ret);
  207. if (ret)
  208. return ret;
  209. return size;
  210. }
  211. static DEVICE_ATTR_RW(role);
  212. static struct attribute *usb_role_switch_attrs[] = {
  213. &dev_attr_role.attr,
  214. NULL,
  215. };
  216. static const struct attribute_group usb_role_switch_group = {
  217. .is_visible = usb_role_switch_is_visible,
  218. .attrs = usb_role_switch_attrs,
  219. };
  220. static const struct attribute_group *usb_role_switch_groups[] = {
  221. &usb_role_switch_group,
  222. NULL,
  223. };
  224. static int
  225. usb_role_switch_uevent(struct device *dev, struct kobj_uevent_env *env)
  226. {
  227. int ret;
  228. ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
  229. if (ret)
  230. dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
  231. return ret;
  232. }
  233. static void usb_role_switch_release(struct device *dev)
  234. {
  235. struct usb_role_switch *sw = to_role_switch(dev);
  236. kfree(sw);
  237. }
  238. static const struct device_type usb_role_dev_type = {
  239. .name = "usb_role_switch",
  240. .groups = usb_role_switch_groups,
  241. .uevent = usb_role_switch_uevent,
  242. .release = usb_role_switch_release,
  243. };
  244. /**
  245. * usb_role_switch_register - Register USB Role Switch
  246. * @parent: Parent device for the switch
  247. * @desc: Description of the switch
  248. *
  249. * USB Role Switch is a device capable or choosing the role for USB connector.
  250. * On platforms where the USB controller is dual-role capable, the controller
  251. * driver will need to register the switch. On platforms where the USB host and
  252. * USB device controllers behind the connector are separate, there will be a
  253. * mux, and the driver for that mux will need to register the switch.
  254. *
  255. * Returns handle to a new role switch or ERR_PTR. The content of @desc is
  256. * copied.
  257. */
  258. struct usb_role_switch *
  259. usb_role_switch_register(struct device *parent,
  260. const struct usb_role_switch_desc *desc)
  261. {
  262. struct usb_role_switch *sw;
  263. int ret;
  264. if (!desc || !desc->set)
  265. return ERR_PTR(-EINVAL);
  266. sw = kzalloc(sizeof(*sw), GFP_KERNEL);
  267. if (!sw)
  268. return ERR_PTR(-ENOMEM);
  269. mutex_init(&sw->lock);
  270. sw->allow_userspace_control = desc->allow_userspace_control;
  271. sw->usb2_port = desc->usb2_port;
  272. sw->usb3_port = desc->usb3_port;
  273. sw->udc = desc->udc;
  274. sw->set = desc->set;
  275. sw->get = desc->get;
  276. sw->dev.parent = parent;
  277. sw->dev.fwnode = desc->fwnode;
  278. sw->dev.class = role_class;
  279. sw->dev.type = &usb_role_dev_type;
  280. dev_set_drvdata(&sw->dev, desc->driver_data);
  281. dev_set_name(&sw->dev, "%s-role-switch",
  282. desc->name ? desc->name : dev_name(parent));
  283. ret = device_register(&sw->dev);
  284. if (ret) {
  285. put_device(&sw->dev);
  286. return ERR_PTR(ret);
  287. }
  288. /* TODO: Symlinks for the host port and the device controller. */
  289. return sw;
  290. }
  291. EXPORT_SYMBOL_GPL(usb_role_switch_register);
  292. /**
  293. * usb_role_switch_unregister - Unregsiter USB Role Switch
  294. * @sw: USB Role Switch
  295. *
  296. * Unregister switch that was registered with usb_role_switch_register().
  297. */
  298. void usb_role_switch_unregister(struct usb_role_switch *sw)
  299. {
  300. if (!IS_ERR_OR_NULL(sw))
  301. device_unregister(&sw->dev);
  302. }
  303. EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
  304. /**
  305. * usb_role_switch_set_drvdata - Assign private data pointer to a switch
  306. * @sw: USB Role Switch
  307. * @data: Private data pointer
  308. */
  309. void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
  310. {
  311. dev_set_drvdata(&sw->dev, data);
  312. }
  313. EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
  314. /**
  315. * usb_role_switch_get_drvdata - Get the private data pointer of a switch
  316. * @sw: USB Role Switch
  317. */
  318. void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
  319. {
  320. return dev_get_drvdata(&sw->dev);
  321. }
  322. EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
  323. static int __init usb_roles_init(void)
  324. {
  325. role_class = class_create(THIS_MODULE, "usb_role");
  326. return PTR_ERR_OR_ZERO(role_class);
  327. }
  328. subsys_initcall(usb_roles_init);
  329. static void __exit usb_roles_exit(void)
  330. {
  331. class_destroy(role_class);
  332. }
  333. module_exit(usb_roles_exit);
  334. MODULE_AUTHOR("Heikki Krogerus <[email protected]>");
  335. MODULE_AUTHOR("Hans de Goede <[email protected]>");
  336. MODULE_LICENSE("GPL v2");
  337. MODULE_DESCRIPTION("USB Role Class");