bus.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * Bus for USB Type-C Alternate Modes
  4. *
  5. * Copyright (C) 2018 Intel Corporation
  6. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7. */
  8. #include <linux/usb/pd_vdo.h>
  9. #include "bus.h"
  10. static inline int typec_altmode_set_mux(struct altmode *alt, u8 state)
  11. {
  12. return alt->mux ? alt->mux->set(alt->mux, state) : 0;
  13. }
  14. static int typec_altmode_set_state(struct typec_altmode *adev, int state)
  15. {
  16. bool is_port = is_typec_port(adev->dev.parent);
  17. struct altmode *port_altmode;
  18. int ret;
  19. port_altmode = is_port ? to_altmode(adev) : to_altmode(adev)->partner;
  20. ret = typec_altmode_set_mux(port_altmode, state);
  21. if (ret)
  22. return ret;
  23. blocking_notifier_call_chain(&port_altmode->nh, state, NULL);
  24. return 0;
  25. }
  26. /* -------------------------------------------------------------------------- */
  27. /* Common API */
  28. /**
  29. * typec_altmode_notify - Communication between the OS and alternate mode driver
  30. * @adev: Handle to the alternate mode
  31. * @conf: Alternate mode specific configuration value
  32. * @data: Alternate mode specific data
  33. *
  34. * The primary purpose for this function is to allow the alternate mode drivers
  35. * to tell which pin configuration has been negotiated with the partner. That
  36. * information will then be used for example to configure the muxes.
  37. * Communication to the other direction is also possible, and low level device
  38. * drivers can also send notifications to the alternate mode drivers. The actual
  39. * communication will be specific for every SVID.
  40. */
  41. int typec_altmode_notify(struct typec_altmode *adev,
  42. unsigned long conf, void *data)
  43. {
  44. bool is_port;
  45. struct altmode *altmode;
  46. struct altmode *partner;
  47. int ret;
  48. if (!adev)
  49. return 0;
  50. altmode = to_altmode(adev);
  51. if (!altmode->partner)
  52. return -ENODEV;
  53. is_port = is_typec_port(adev->dev.parent);
  54. partner = altmode->partner;
  55. ret = typec_altmode_set_mux(is_port ? altmode : partner, (u8)conf);
  56. if (ret)
  57. return ret;
  58. blocking_notifier_call_chain(is_port ? &altmode->nh : &partner->nh,
  59. conf, data);
  60. if (partner->adev.ops && partner->adev.ops->notify)
  61. return partner->adev.ops->notify(&partner->adev, conf, data);
  62. return 0;
  63. }
  64. EXPORT_SYMBOL_GPL(typec_altmode_notify);
  65. /**
  66. * typec_altmode_enter - Enter Mode
  67. * @adev: The alternate mode
  68. *
  69. * The alternate mode drivers use this function to enter mode. The port drivers
  70. * use this to inform the alternate mode drivers that the partner has initiated
  71. * Enter Mode command.
  72. */
  73. int typec_altmode_enter(struct typec_altmode *adev)
  74. {
  75. struct altmode *partner = to_altmode(adev)->partner;
  76. struct typec_altmode *pdev = &partner->adev;
  77. int ret;
  78. if (!adev || adev->active)
  79. return 0;
  80. if (!pdev->ops || !pdev->ops->enter)
  81. return -EOPNOTSUPP;
  82. /* Moving to USB Safe State */
  83. ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE);
  84. if (ret)
  85. return ret;
  86. /* Enter Mode */
  87. return pdev->ops->enter(pdev);
  88. }
  89. EXPORT_SYMBOL_GPL(typec_altmode_enter);
  90. /**
  91. * typec_altmode_exit - Exit Mode
  92. * @adev: The alternate mode
  93. *
  94. * The partner of @adev has initiated Exit Mode command.
  95. */
  96. int typec_altmode_exit(struct typec_altmode *adev)
  97. {
  98. struct altmode *partner = to_altmode(adev)->partner;
  99. struct typec_altmode *pdev = &partner->adev;
  100. int ret;
  101. if (!adev || !adev->active)
  102. return 0;
  103. if (!pdev->ops || !pdev->ops->enter)
  104. return -EOPNOTSUPP;
  105. /* Moving to USB Safe State */
  106. ret = typec_altmode_set_state(adev, TYPEC_STATE_SAFE);
  107. if (ret)
  108. return ret;
  109. /* Exit Mode command */
  110. return pdev->ops->exit(pdev);
  111. }
  112. EXPORT_SYMBOL_GPL(typec_altmode_exit);
  113. /**
  114. * typec_altmode_attention - Attention command
  115. * @adev: The alternate mode
  116. * @vdo: VDO for the Attention command
  117. *
  118. * Notifies the partner of @adev about Attention command.
  119. */
  120. void typec_altmode_attention(struct typec_altmode *adev, u32 vdo)
  121. {
  122. struct typec_altmode *pdev = &to_altmode(adev)->partner->adev;
  123. if (pdev->ops && pdev->ops->attention)
  124. pdev->ops->attention(pdev, vdo);
  125. }
  126. EXPORT_SYMBOL_GPL(typec_altmode_attention);
  127. /**
  128. * typec_altmode_vdm - Send Vendor Defined Messages (VDM) to the partner
  129. * @adev: Alternate mode handle
  130. * @header: VDM Header
  131. * @vdo: Array of Vendor Defined Data Objects
  132. * @count: Number of Data Objects
  133. *
  134. * The alternate mode drivers use this function for SVID specific communication
  135. * with the partner. The port drivers use it to deliver the Structured VDMs
  136. * received from the partners to the alternate mode drivers.
  137. */
  138. int typec_altmode_vdm(struct typec_altmode *adev,
  139. const u32 header, const u32 *vdo, int count)
  140. {
  141. struct typec_altmode *pdev;
  142. struct altmode *altmode;
  143. if (!adev)
  144. return 0;
  145. altmode = to_altmode(adev);
  146. if (!altmode->partner)
  147. return -ENODEV;
  148. pdev = &altmode->partner->adev;
  149. if (!pdev->ops || !pdev->ops->vdm)
  150. return -EOPNOTSUPP;
  151. return pdev->ops->vdm(pdev, header, vdo, count);
  152. }
  153. EXPORT_SYMBOL_GPL(typec_altmode_vdm);
  154. const struct typec_altmode *
  155. typec_altmode_get_partner(struct typec_altmode *adev)
  156. {
  157. return &to_altmode(adev)->partner->adev;
  158. }
  159. EXPORT_SYMBOL_GPL(typec_altmode_get_partner);
  160. /* -------------------------------------------------------------------------- */
  161. /* API for the alternate mode drivers */
  162. /**
  163. * typec_altmode_get_plug - Find cable plug alternate mode
  164. * @adev: Handle to partner alternate mode
  165. * @index: Cable plug index
  166. *
  167. * Increment reference count for cable plug alternate mode device. Returns
  168. * handle to the cable plug alternate mode, or NULL if none is found.
  169. */
  170. struct typec_altmode *typec_altmode_get_plug(struct typec_altmode *adev,
  171. enum typec_plug_index index)
  172. {
  173. struct altmode *port = to_altmode(adev)->partner;
  174. if (port->plug[index]) {
  175. get_device(&port->plug[index]->adev.dev);
  176. return &port->plug[index]->adev;
  177. }
  178. return NULL;
  179. }
  180. EXPORT_SYMBOL_GPL(typec_altmode_get_plug);
  181. /**
  182. * typec_altmode_put_plug - Decrement cable plug alternate mode reference count
  183. * @plug: Handle to the cable plug alternate mode
  184. */
  185. void typec_altmode_put_plug(struct typec_altmode *plug)
  186. {
  187. if (plug)
  188. put_device(&plug->dev);
  189. }
  190. EXPORT_SYMBOL_GPL(typec_altmode_put_plug);
  191. int __typec_altmode_register_driver(struct typec_altmode_driver *drv,
  192. struct module *module)
  193. {
  194. if (!drv->probe)
  195. return -EINVAL;
  196. drv->driver.owner = module;
  197. drv->driver.bus = &typec_bus;
  198. return driver_register(&drv->driver);
  199. }
  200. EXPORT_SYMBOL_GPL(__typec_altmode_register_driver);
  201. void typec_altmode_unregister_driver(struct typec_altmode_driver *drv)
  202. {
  203. driver_unregister(&drv->driver);
  204. }
  205. EXPORT_SYMBOL_GPL(typec_altmode_unregister_driver);
  206. /* -------------------------------------------------------------------------- */
  207. /* API for the port drivers */
  208. /**
  209. * typec_match_altmode - Match SVID and mode to an array of alternate modes
  210. * @altmodes: Array of alternate modes
  211. * @n: Number of elements in the array, or -1 for NULL terminated arrays
  212. * @svid: Standard or Vendor ID to match with
  213. * @mode: Mode to match with
  214. *
  215. * Return pointer to an alternate mode with SVID matching @svid, or NULL when no
  216. * match is found.
  217. */
  218. struct typec_altmode *typec_match_altmode(struct typec_altmode **altmodes,
  219. size_t n, u16 svid, u8 mode)
  220. {
  221. int i;
  222. for (i = 0; i < n; i++) {
  223. if (!altmodes[i])
  224. break;
  225. if (altmodes[i]->svid == svid && altmodes[i]->mode == mode)
  226. return altmodes[i];
  227. }
  228. return NULL;
  229. }
  230. EXPORT_SYMBOL_GPL(typec_match_altmode);
  231. /* -------------------------------------------------------------------------- */
  232. static ssize_t
  233. description_show(struct device *dev, struct device_attribute *attr, char *buf)
  234. {
  235. struct typec_altmode *alt = to_typec_altmode(dev);
  236. return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
  237. }
  238. static DEVICE_ATTR_RO(description);
  239. static struct attribute *typec_attrs[] = {
  240. &dev_attr_description.attr,
  241. NULL
  242. };
  243. ATTRIBUTE_GROUPS(typec);
  244. static int typec_match(struct device *dev, struct device_driver *driver)
  245. {
  246. struct typec_altmode_driver *drv = to_altmode_driver(driver);
  247. struct typec_altmode *altmode = to_typec_altmode(dev);
  248. const struct typec_device_id *id;
  249. for (id = drv->id_table; id->svid; id++)
  250. if (id->svid == altmode->svid &&
  251. (id->mode == TYPEC_ANY_MODE || id->mode == altmode->mode))
  252. return 1;
  253. return 0;
  254. }
  255. static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
  256. {
  257. struct typec_altmode *altmode = to_typec_altmode(dev);
  258. if (add_uevent_var(env, "SVID=%04X", altmode->svid))
  259. return -ENOMEM;
  260. if (add_uevent_var(env, "MODE=%u", altmode->mode))
  261. return -ENOMEM;
  262. return add_uevent_var(env, "MODALIAS=typec:id%04Xm%02X",
  263. altmode->svid, altmode->mode);
  264. }
  265. static int typec_altmode_create_links(struct altmode *alt)
  266. {
  267. struct device *port_dev = &alt->partner->adev.dev;
  268. struct device *dev = &alt->adev.dev;
  269. int err;
  270. err = sysfs_create_link(&dev->kobj, &port_dev->kobj, "port");
  271. if (err)
  272. return err;
  273. err = sysfs_create_link(&port_dev->kobj, &dev->kobj, "partner");
  274. if (err)
  275. sysfs_remove_link(&dev->kobj, "port");
  276. return err;
  277. }
  278. static void typec_altmode_remove_links(struct altmode *alt)
  279. {
  280. sysfs_remove_link(&alt->partner->adev.dev.kobj, "partner");
  281. sysfs_remove_link(&alt->adev.dev.kobj, "port");
  282. }
  283. static int typec_probe(struct device *dev)
  284. {
  285. struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
  286. struct typec_altmode *adev = to_typec_altmode(dev);
  287. struct altmode *altmode = to_altmode(adev);
  288. int ret;
  289. /* Fail if the port does not support the alternate mode */
  290. if (!altmode->partner)
  291. return -ENODEV;
  292. ret = typec_altmode_create_links(altmode);
  293. if (ret) {
  294. dev_warn(dev, "failed to create symlinks\n");
  295. return ret;
  296. }
  297. ret = drv->probe(adev);
  298. if (ret)
  299. typec_altmode_remove_links(altmode);
  300. return ret;
  301. }
  302. static int typec_remove(struct device *dev)
  303. {
  304. struct typec_altmode_driver *drv = to_altmode_driver(dev->driver);
  305. struct typec_altmode *adev = to_typec_altmode(dev);
  306. struct altmode *altmode = to_altmode(adev);
  307. typec_altmode_remove_links(altmode);
  308. if (drv->remove)
  309. drv->remove(to_typec_altmode(dev));
  310. if (adev->active) {
  311. WARN_ON(typec_altmode_set_state(adev, TYPEC_STATE_SAFE));
  312. typec_altmode_update_active(adev, false);
  313. }
  314. adev->desc = NULL;
  315. adev->ops = NULL;
  316. return 0;
  317. }
  318. struct bus_type typec_bus = {
  319. .name = "typec",
  320. .dev_groups = typec_groups,
  321. .match = typec_match,
  322. .uevent = typec_uevent,
  323. .probe = typec_probe,
  324. .remove = typec_remove,
  325. };