class.c 11 KB

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