class.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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;
  114. struct device *dev;
  115. if (!fwnode_device_is_compatible(fwnode, "usb-b-connector"))
  116. return NULL;
  117. parent = fwnode_get_parent(fwnode);
  118. if (!fwnode_property_present(parent, "usb-role-switch")) {
  119. fwnode_handle_put(parent);
  120. return NULL;
  121. }
  122. dev = class_find_device_by_fwnode(&role_class, parent);
  123. fwnode_handle_put(parent);
  124. return dev ? to_role_switch(dev) : ERR_PTR(-EPROBE_DEFER);
  125. }
  126. /**
  127. * usb_role_switch_get - Find USB role switch linked with the caller
  128. * @dev: The caller device
  129. *
  130. * Finds and returns role switch linked with @dev. The reference count for the
  131. * found switch is incremented.
  132. */
  133. struct usb_role_switch *usb_role_switch_get(struct device *dev)
  134. {
  135. struct usb_role_switch *sw;
  136. sw = usb_role_switch_is_parent(dev_fwnode(dev));
  137. if (!sw)
  138. sw = device_connection_find_match(dev, "usb-role-switch", NULL,
  139. usb_role_switch_match);
  140. if (!IS_ERR_OR_NULL(sw))
  141. WARN_ON(!try_module_get(sw->module));
  142. return sw;
  143. }
  144. EXPORT_SYMBOL_GPL(usb_role_switch_get);
  145. /**
  146. * fwnode_usb_role_switch_get - Find USB role switch linked with the caller
  147. * @fwnode: The caller device node
  148. *
  149. * This is similar to the usb_role_switch_get() function above, but it searches
  150. * the switch using fwnode instead of device entry.
  151. */
  152. struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *fwnode)
  153. {
  154. struct usb_role_switch *sw;
  155. sw = usb_role_switch_is_parent(fwnode);
  156. if (!sw)
  157. sw = fwnode_connection_find_match(fwnode, "usb-role-switch",
  158. NULL, usb_role_switch_match);
  159. if (!IS_ERR_OR_NULL(sw))
  160. WARN_ON(!try_module_get(sw->module));
  161. return sw;
  162. }
  163. EXPORT_SYMBOL_GPL(fwnode_usb_role_switch_get);
  164. /**
  165. * usb_role_switch_put - Release handle to a switch
  166. * @sw: USB Role Switch
  167. *
  168. * Decrement reference count for @sw.
  169. */
  170. void usb_role_switch_put(struct usb_role_switch *sw)
  171. {
  172. if (!IS_ERR_OR_NULL(sw)) {
  173. module_put(sw->module);
  174. put_device(&sw->dev);
  175. }
  176. }
  177. EXPORT_SYMBOL_GPL(usb_role_switch_put);
  178. /**
  179. * usb_role_switch_find_by_fwnode - Find USB role switch with its fwnode
  180. * @fwnode: fwnode of the USB Role Switch
  181. *
  182. * Finds and returns role switch with @fwnode. The reference count for the
  183. * found switch is incremented.
  184. */
  185. struct usb_role_switch *
  186. usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode)
  187. {
  188. struct device *dev;
  189. struct usb_role_switch *sw = NULL;
  190. if (!fwnode)
  191. return NULL;
  192. dev = class_find_device_by_fwnode(&role_class, fwnode);
  193. if (dev) {
  194. sw = to_role_switch(dev);
  195. WARN_ON(!try_module_get(sw->module));
  196. }
  197. return sw;
  198. }
  199. EXPORT_SYMBOL_GPL(usb_role_switch_find_by_fwnode);
  200. static umode_t
  201. usb_role_switch_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  202. {
  203. struct device *dev = kobj_to_dev(kobj);
  204. struct usb_role_switch *sw = to_role_switch(dev);
  205. if (sw->allow_userspace_control)
  206. return attr->mode;
  207. return 0;
  208. }
  209. static const char * const usb_roles[] = {
  210. [USB_ROLE_NONE] = "none",
  211. [USB_ROLE_HOST] = "host",
  212. [USB_ROLE_DEVICE] = "device",
  213. };
  214. const char *usb_role_string(enum usb_role role)
  215. {
  216. if (role < 0 || role >= ARRAY_SIZE(usb_roles))
  217. return "unknown";
  218. return usb_roles[role];
  219. }
  220. EXPORT_SYMBOL_GPL(usb_role_string);
  221. static ssize_t
  222. role_show(struct device *dev, struct device_attribute *attr, char *buf)
  223. {
  224. struct usb_role_switch *sw = to_role_switch(dev);
  225. enum usb_role role = usb_role_switch_get_role(sw);
  226. return sprintf(buf, "%s\n", usb_roles[role]);
  227. }
  228. static ssize_t role_store(struct device *dev, struct device_attribute *attr,
  229. const char *buf, size_t size)
  230. {
  231. struct usb_role_switch *sw = to_role_switch(dev);
  232. int ret;
  233. ret = sysfs_match_string(usb_roles, buf);
  234. if (ret < 0) {
  235. bool res;
  236. /* Extra check if the user wants to disable the switch */
  237. ret = kstrtobool(buf, &res);
  238. if (ret || res)
  239. return -EINVAL;
  240. }
  241. ret = usb_role_switch_set_role(sw, ret);
  242. if (ret)
  243. return ret;
  244. return size;
  245. }
  246. static DEVICE_ATTR_RW(role);
  247. static struct attribute *usb_role_switch_attrs[] = {
  248. &dev_attr_role.attr,
  249. NULL,
  250. };
  251. static const struct attribute_group usb_role_switch_group = {
  252. .is_visible = usb_role_switch_is_visible,
  253. .attrs = usb_role_switch_attrs,
  254. };
  255. static const struct attribute_group *usb_role_switch_groups[] = {
  256. &usb_role_switch_group,
  257. NULL,
  258. };
  259. static int usb_role_switch_uevent(const struct device *dev, struct kobj_uevent_env *env)
  260. {
  261. int ret;
  262. ret = add_uevent_var(env, "USB_ROLE_SWITCH=%s", dev_name(dev));
  263. if (ret)
  264. dev_err(dev, "failed to add uevent USB_ROLE_SWITCH\n");
  265. return ret;
  266. }
  267. static void usb_role_switch_release(struct device *dev)
  268. {
  269. struct usb_role_switch *sw = to_role_switch(dev);
  270. mutex_destroy(&sw->lock);
  271. lockdep_unregister_key(&sw->key);
  272. kfree(sw);
  273. }
  274. static const struct device_type usb_role_dev_type = {
  275. .name = "usb_role_switch",
  276. .groups = usb_role_switch_groups,
  277. .uevent = usb_role_switch_uevent,
  278. .release = usb_role_switch_release,
  279. };
  280. /**
  281. * usb_role_switch_register - Register USB Role Switch
  282. * @parent: Parent device for the switch
  283. * @desc: Description of the switch
  284. *
  285. * USB Role Switch is a device capable or choosing the role for USB connector.
  286. * On platforms where the USB controller is dual-role capable, the controller
  287. * driver will need to register the switch. On platforms where the USB host and
  288. * USB device controllers behind the connector are separate, there will be a
  289. * mux, and the driver for that mux will need to register the switch.
  290. *
  291. * Returns handle to a new role switch or ERR_PTR. The content of @desc is
  292. * copied.
  293. */
  294. struct usb_role_switch *
  295. usb_role_switch_register(struct device *parent,
  296. const struct usb_role_switch_desc *desc)
  297. {
  298. struct usb_role_switch *sw;
  299. int ret;
  300. if (!desc || !desc->set)
  301. return ERR_PTR(-EINVAL);
  302. sw = kzalloc_obj(*sw);
  303. if (!sw)
  304. return ERR_PTR(-ENOMEM);
  305. lockdep_register_key(&sw->key);
  306. mutex_init_with_key(&sw->lock, &sw->key);
  307. sw->allow_userspace_control = desc->allow_userspace_control;
  308. sw->usb2_port = desc->usb2_port;
  309. sw->usb3_port = desc->usb3_port;
  310. sw->udc = desc->udc;
  311. sw->set = desc->set;
  312. sw->get = desc->get;
  313. sw->module = parent->driver->owner;
  314. sw->dev.parent = parent;
  315. sw->dev.fwnode = desc->fwnode;
  316. sw->dev.class = &role_class;
  317. sw->dev.type = &usb_role_dev_type;
  318. dev_set_drvdata(&sw->dev, desc->driver_data);
  319. dev_set_name(&sw->dev, "%s-role-switch",
  320. desc->name ? desc->name : dev_name(parent));
  321. sw->registered = true;
  322. ret = device_register(&sw->dev);
  323. if (ret) {
  324. sw->registered = false;
  325. put_device(&sw->dev);
  326. return ERR_PTR(ret);
  327. }
  328. if (dev_fwnode(&sw->dev)) {
  329. ret = component_add(&sw->dev, &connector_ops);
  330. if (ret)
  331. dev_warn(&sw->dev, "failed to add component\n");
  332. }
  333. /* TODO: Symlinks for the host port and the device controller. */
  334. return sw;
  335. }
  336. EXPORT_SYMBOL_GPL(usb_role_switch_register);
  337. /**
  338. * usb_role_switch_unregister - Unregsiter USB Role Switch
  339. * @sw: USB Role Switch
  340. *
  341. * Unregister switch that was registered with usb_role_switch_register().
  342. */
  343. void usb_role_switch_unregister(struct usb_role_switch *sw)
  344. {
  345. if (IS_ERR_OR_NULL(sw))
  346. return;
  347. sw->registered = false;
  348. if (dev_fwnode(&sw->dev))
  349. component_del(&sw->dev, &connector_ops);
  350. device_unregister(&sw->dev);
  351. }
  352. EXPORT_SYMBOL_GPL(usb_role_switch_unregister);
  353. /**
  354. * usb_role_switch_set_drvdata - Assign private data pointer to a switch
  355. * @sw: USB Role Switch
  356. * @data: Private data pointer
  357. */
  358. void usb_role_switch_set_drvdata(struct usb_role_switch *sw, void *data)
  359. {
  360. dev_set_drvdata(&sw->dev, data);
  361. }
  362. EXPORT_SYMBOL_GPL(usb_role_switch_set_drvdata);
  363. /**
  364. * usb_role_switch_get_drvdata - Get the private data pointer of a switch
  365. * @sw: USB Role Switch
  366. */
  367. void *usb_role_switch_get_drvdata(struct usb_role_switch *sw)
  368. {
  369. return dev_get_drvdata(&sw->dev);
  370. }
  371. EXPORT_SYMBOL_GPL(usb_role_switch_get_drvdata);
  372. static int __init usb_roles_init(void)
  373. {
  374. return class_register(&role_class);
  375. }
  376. subsys_initcall(usb_roles_init);
  377. static void __exit usb_roles_exit(void)
  378. {
  379. class_unregister(&role_class);
  380. }
  381. module_exit(usb_roles_exit);
  382. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  383. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  384. MODULE_LICENSE("GPL v2");
  385. MODULE_DESCRIPTION("USB Role Class");