class.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * USB Type-C Connector Class
  4. *
  5. * Copyright (C) 2017, Intel Corporation
  6. * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/module.h>
  10. #include <linux/mutex.h>
  11. #include <linux/property.h>
  12. #include <linux/slab.h>
  13. #include "bus.h"
  14. struct typec_plug {
  15. struct device dev;
  16. enum typec_plug_index index;
  17. struct ida mode_ids;
  18. };
  19. struct typec_cable {
  20. struct device dev;
  21. enum typec_plug_type type;
  22. struct usb_pd_identity *identity;
  23. unsigned int active:1;
  24. };
  25. struct typec_partner {
  26. struct device dev;
  27. unsigned int usb_pd:1;
  28. struct usb_pd_identity *identity;
  29. enum typec_accessory accessory;
  30. struct ida mode_ids;
  31. };
  32. struct typec_port {
  33. unsigned int id;
  34. struct device dev;
  35. struct ida mode_ids;
  36. int prefer_role;
  37. enum typec_data_role data_role;
  38. enum typec_role pwr_role;
  39. enum typec_role vconn_role;
  40. enum typec_pwr_opmode pwr_opmode;
  41. enum typec_port_type port_type;
  42. struct mutex port_type_lock;
  43. enum typec_orientation orientation;
  44. struct typec_switch *sw;
  45. struct typec_mux *mux;
  46. const struct typec_capability *cap;
  47. };
  48. #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
  49. #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
  50. #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
  51. #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
  52. static const struct device_type typec_partner_dev_type;
  53. static const struct device_type typec_cable_dev_type;
  54. static const struct device_type typec_plug_dev_type;
  55. #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
  56. #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
  57. #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
  58. static DEFINE_IDA(typec_index_ida);
  59. static struct class *typec_class;
  60. /* ------------------------------------------------------------------------- */
  61. /* Common attributes */
  62. static const char * const typec_accessory_modes[] = {
  63. [TYPEC_ACCESSORY_NONE] = "none",
  64. [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
  65. [TYPEC_ACCESSORY_DEBUG] = "debug",
  66. };
  67. static struct usb_pd_identity *get_pd_identity(struct device *dev)
  68. {
  69. if (is_typec_partner(dev)) {
  70. struct typec_partner *partner = to_typec_partner(dev);
  71. return partner->identity;
  72. } else if (is_typec_cable(dev)) {
  73. struct typec_cable *cable = to_typec_cable(dev);
  74. return cable->identity;
  75. }
  76. return NULL;
  77. }
  78. static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
  79. char *buf)
  80. {
  81. struct usb_pd_identity *id = get_pd_identity(dev);
  82. return sprintf(buf, "0x%08x\n", id->id_header);
  83. }
  84. static DEVICE_ATTR_RO(id_header);
  85. static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
  86. char *buf)
  87. {
  88. struct usb_pd_identity *id = get_pd_identity(dev);
  89. return sprintf(buf, "0x%08x\n", id->cert_stat);
  90. }
  91. static DEVICE_ATTR_RO(cert_stat);
  92. static ssize_t product_show(struct device *dev, struct device_attribute *attr,
  93. char *buf)
  94. {
  95. struct usb_pd_identity *id = get_pd_identity(dev);
  96. return sprintf(buf, "0x%08x\n", id->product);
  97. }
  98. static DEVICE_ATTR_RO(product);
  99. static struct attribute *usb_pd_id_attrs[] = {
  100. &dev_attr_id_header.attr,
  101. &dev_attr_cert_stat.attr,
  102. &dev_attr_product.attr,
  103. NULL
  104. };
  105. static const struct attribute_group usb_pd_id_group = {
  106. .name = "identity",
  107. .attrs = usb_pd_id_attrs,
  108. };
  109. static const struct attribute_group *usb_pd_id_groups[] = {
  110. &usb_pd_id_group,
  111. NULL,
  112. };
  113. static void typec_report_identity(struct device *dev)
  114. {
  115. sysfs_notify(&dev->kobj, "identity", "id_header");
  116. sysfs_notify(&dev->kobj, "identity", "cert_stat");
  117. sysfs_notify(&dev->kobj, "identity", "product");
  118. }
  119. /* ------------------------------------------------------------------------- */
  120. /* Alternate Modes */
  121. static int altmode_match(struct device *dev, void *data)
  122. {
  123. struct typec_altmode *adev = to_typec_altmode(dev);
  124. struct typec_device_id *id = data;
  125. if (!is_typec_altmode(dev))
  126. return 0;
  127. return ((adev->svid == id->svid) && (adev->mode == id->mode));
  128. }
  129. static void typec_altmode_set_partner(struct altmode *altmode)
  130. {
  131. struct typec_altmode *adev = &altmode->adev;
  132. struct typec_device_id id = { adev->svid, adev->mode, };
  133. struct typec_port *port = typec_altmode2port(adev);
  134. struct altmode *partner;
  135. struct device *dev;
  136. dev = device_find_child(&port->dev, &id, altmode_match);
  137. if (!dev)
  138. return;
  139. /* Bind the port alt mode to the partner/plug alt mode. */
  140. partner = to_altmode(to_typec_altmode(dev));
  141. altmode->partner = partner;
  142. /* Bind the partner/plug alt mode to the port alt mode. */
  143. if (is_typec_plug(adev->dev.parent)) {
  144. struct typec_plug *plug = to_typec_plug(adev->dev.parent);
  145. partner->plug[plug->index] = altmode;
  146. } else {
  147. partner->partner = altmode;
  148. }
  149. }
  150. static void typec_altmode_put_partner(struct altmode *altmode)
  151. {
  152. struct altmode *partner = altmode->partner;
  153. struct typec_altmode *adev;
  154. if (!partner)
  155. return;
  156. adev = &partner->adev;
  157. if (is_typec_plug(adev->dev.parent)) {
  158. struct typec_plug *plug = to_typec_plug(adev->dev.parent);
  159. partner->plug[plug->index] = NULL;
  160. } else {
  161. partner->partner = NULL;
  162. }
  163. put_device(&adev->dev);
  164. }
  165. static void *typec_port_match(struct device_connection *con, int ep, void *data)
  166. {
  167. struct device *dev;
  168. /*
  169. * FIXME: Check does the fwnode supports the requested SVID. If it does
  170. * we need to return ERR_PTR(-PROBE_DEFER) when there is no device.
  171. */
  172. if (con->fwnode)
  173. return class_find_device_by_fwnode(typec_class, con->fwnode);
  174. dev = class_find_device_by_name(typec_class, con->endpoint[ep]);
  175. return dev ? dev : ERR_PTR(-EPROBE_DEFER);
  176. }
  177. struct typec_altmode *
  178. typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
  179. struct notifier_block *nb)
  180. {
  181. struct typec_device_id id = { svid, mode, };
  182. struct device *altmode_dev;
  183. struct device *port_dev;
  184. struct altmode *altmode;
  185. int ret;
  186. /* Find the port linked to the caller */
  187. port_dev = device_connection_find_match(dev, NULL, NULL,
  188. typec_port_match);
  189. if (IS_ERR_OR_NULL(port_dev))
  190. return port_dev ? ERR_CAST(port_dev) : ERR_PTR(-ENODEV);
  191. /* Find the altmode with matching svid */
  192. altmode_dev = device_find_child(port_dev, &id, altmode_match);
  193. put_device(port_dev);
  194. if (!altmode_dev)
  195. return ERR_PTR(-ENODEV);
  196. altmode = to_altmode(to_typec_altmode(altmode_dev));
  197. /* Register notifier */
  198. ret = blocking_notifier_chain_register(&altmode->nh, nb);
  199. if (ret) {
  200. put_device(altmode_dev);
  201. return ERR_PTR(ret);
  202. }
  203. return &altmode->adev;
  204. }
  205. EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
  206. void typec_altmode_unregister_notifier(struct typec_altmode *adev,
  207. struct notifier_block *nb)
  208. {
  209. struct altmode *altmode = to_altmode(adev);
  210. blocking_notifier_chain_unregister(&altmode->nh, nb);
  211. put_device(&adev->dev);
  212. }
  213. EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
  214. /**
  215. * typec_altmode_update_active - Report Enter/Exit mode
  216. * @adev: Handle to the alternate mode
  217. * @active: True when the mode has been entered
  218. *
  219. * If a partner or cable plug executes Enter/Exit Mode command successfully, the
  220. * drivers use this routine to report the updated state of the mode.
  221. */
  222. void typec_altmode_update_active(struct typec_altmode *adev, bool active)
  223. {
  224. char dir[6];
  225. if (adev->active == active)
  226. return;
  227. if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
  228. if (!active)
  229. module_put(adev->dev.driver->owner);
  230. else
  231. WARN_ON(!try_module_get(adev->dev.driver->owner));
  232. }
  233. adev->active = active;
  234. snprintf(dir, sizeof(dir), "mode%d", adev->mode);
  235. sysfs_notify(&adev->dev.kobj, dir, "active");
  236. sysfs_notify(&adev->dev.kobj, NULL, "active");
  237. kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
  238. }
  239. EXPORT_SYMBOL_GPL(typec_altmode_update_active);
  240. /**
  241. * typec_altmode2port - Alternate Mode to USB Type-C port
  242. * @alt: The Alternate Mode
  243. *
  244. * Returns handle to the port that a cable plug or partner with @alt is
  245. * connected to.
  246. */
  247. struct typec_port *typec_altmode2port(struct typec_altmode *alt)
  248. {
  249. if (is_typec_plug(alt->dev.parent))
  250. return to_typec_port(alt->dev.parent->parent->parent);
  251. if (is_typec_partner(alt->dev.parent))
  252. return to_typec_port(alt->dev.parent->parent);
  253. if (is_typec_port(alt->dev.parent))
  254. return to_typec_port(alt->dev.parent);
  255. return NULL;
  256. }
  257. EXPORT_SYMBOL_GPL(typec_altmode2port);
  258. static ssize_t
  259. vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
  260. {
  261. struct typec_altmode *alt = to_typec_altmode(dev);
  262. return sprintf(buf, "0x%08x\n", alt->vdo);
  263. }
  264. static DEVICE_ATTR_RO(vdo);
  265. static ssize_t
  266. description_show(struct device *dev, struct device_attribute *attr, char *buf)
  267. {
  268. struct typec_altmode *alt = to_typec_altmode(dev);
  269. return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
  270. }
  271. static DEVICE_ATTR_RO(description);
  272. static ssize_t
  273. active_show(struct device *dev, struct device_attribute *attr, char *buf)
  274. {
  275. struct typec_altmode *alt = to_typec_altmode(dev);
  276. return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
  277. }
  278. static ssize_t active_store(struct device *dev, struct device_attribute *attr,
  279. const char *buf, size_t size)
  280. {
  281. struct typec_altmode *adev = to_typec_altmode(dev);
  282. struct altmode *altmode = to_altmode(adev);
  283. bool enter;
  284. int ret;
  285. ret = kstrtobool(buf, &enter);
  286. if (ret)
  287. return ret;
  288. if (adev->active == enter)
  289. return size;
  290. if (is_typec_port(adev->dev.parent)) {
  291. typec_altmode_update_active(adev, enter);
  292. /* Make sure that the partner exits the mode before disabling */
  293. if (altmode->partner && !enter && altmode->partner->adev.active)
  294. typec_altmode_exit(&altmode->partner->adev);
  295. } else if (altmode->partner) {
  296. if (enter && !altmode->partner->adev.active) {
  297. dev_warn(dev, "port has the mode disabled\n");
  298. return -EPERM;
  299. }
  300. }
  301. /* Note: If there is no driver, the mode will not be entered */
  302. if (adev->ops && adev->ops->activate) {
  303. ret = adev->ops->activate(adev, enter);
  304. if (ret)
  305. return ret;
  306. }
  307. return size;
  308. }
  309. static DEVICE_ATTR_RW(active);
  310. static ssize_t
  311. supported_roles_show(struct device *dev, struct device_attribute *attr,
  312. char *buf)
  313. {
  314. struct altmode *alt = to_altmode(to_typec_altmode(dev));
  315. ssize_t ret;
  316. switch (alt->roles) {
  317. case TYPEC_PORT_SRC:
  318. ret = sprintf(buf, "source\n");
  319. break;
  320. case TYPEC_PORT_SNK:
  321. ret = sprintf(buf, "sink\n");
  322. break;
  323. case TYPEC_PORT_DRP:
  324. default:
  325. ret = sprintf(buf, "source sink\n");
  326. break;
  327. }
  328. return ret;
  329. }
  330. static DEVICE_ATTR_RO(supported_roles);
  331. static ssize_t
  332. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  333. {
  334. struct typec_altmode *adev = to_typec_altmode(dev);
  335. return sprintf(buf, "%u\n", adev->mode);
  336. }
  337. static DEVICE_ATTR_RO(mode);
  338. static ssize_t
  339. svid_show(struct device *dev, struct device_attribute *attr, char *buf)
  340. {
  341. struct typec_altmode *adev = to_typec_altmode(dev);
  342. return sprintf(buf, "%04x\n", adev->svid);
  343. }
  344. static DEVICE_ATTR_RO(svid);
  345. static struct attribute *typec_altmode_attrs[] = {
  346. &dev_attr_active.attr,
  347. &dev_attr_mode.attr,
  348. &dev_attr_svid.attr,
  349. &dev_attr_vdo.attr,
  350. NULL
  351. };
  352. ATTRIBUTE_GROUPS(typec_altmode);
  353. static int altmode_id_get(struct device *dev)
  354. {
  355. struct ida *ids;
  356. if (is_typec_partner(dev))
  357. ids = &to_typec_partner(dev)->mode_ids;
  358. else if (is_typec_plug(dev))
  359. ids = &to_typec_plug(dev)->mode_ids;
  360. else
  361. ids = &to_typec_port(dev)->mode_ids;
  362. return ida_simple_get(ids, 0, 0, GFP_KERNEL);
  363. }
  364. static void altmode_id_remove(struct device *dev, int id)
  365. {
  366. struct ida *ids;
  367. if (is_typec_partner(dev))
  368. ids = &to_typec_partner(dev)->mode_ids;
  369. else if (is_typec_plug(dev))
  370. ids = &to_typec_plug(dev)->mode_ids;
  371. else
  372. ids = &to_typec_port(dev)->mode_ids;
  373. ida_simple_remove(ids, id);
  374. }
  375. static void typec_altmode_release(struct device *dev)
  376. {
  377. struct altmode *alt = to_altmode(to_typec_altmode(dev));
  378. typec_altmode_put_partner(alt);
  379. altmode_id_remove(alt->adev.dev.parent, alt->id);
  380. kfree(alt);
  381. }
  382. const struct device_type typec_altmode_dev_type = {
  383. .name = "typec_alternate_mode",
  384. .groups = typec_altmode_groups,
  385. .release = typec_altmode_release,
  386. };
  387. static struct typec_altmode *
  388. typec_register_altmode(struct device *parent,
  389. const struct typec_altmode_desc *desc)
  390. {
  391. unsigned int id = altmode_id_get(parent);
  392. bool is_port = is_typec_port(parent);
  393. struct altmode *alt;
  394. int ret;
  395. alt = kzalloc(sizeof(*alt), GFP_KERNEL);
  396. if (!alt) {
  397. altmode_id_remove(parent, id);
  398. return ERR_PTR(-ENOMEM);
  399. }
  400. alt->adev.svid = desc->svid;
  401. alt->adev.mode = desc->mode;
  402. alt->adev.vdo = desc->vdo;
  403. alt->roles = desc->roles;
  404. alt->id = id;
  405. alt->attrs[0] = &dev_attr_vdo.attr;
  406. alt->attrs[1] = &dev_attr_description.attr;
  407. alt->attrs[2] = &dev_attr_active.attr;
  408. if (is_port) {
  409. alt->attrs[3] = &dev_attr_supported_roles.attr;
  410. alt->adev.active = true; /* Enabled by default */
  411. }
  412. sprintf(alt->group_name, "mode%d", desc->mode);
  413. alt->group.name = alt->group_name;
  414. alt->group.attrs = alt->attrs;
  415. alt->groups[0] = &alt->group;
  416. alt->adev.dev.parent = parent;
  417. alt->adev.dev.groups = alt->groups;
  418. alt->adev.dev.type = &typec_altmode_dev_type;
  419. dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
  420. /* Link partners and plugs with the ports */
  421. if (is_port)
  422. BLOCKING_INIT_NOTIFIER_HEAD(&alt->nh);
  423. else
  424. typec_altmode_set_partner(alt);
  425. /* The partners are bind to drivers */
  426. if (is_typec_partner(parent))
  427. alt->adev.dev.bus = &typec_bus;
  428. ret = device_register(&alt->adev.dev);
  429. if (ret) {
  430. dev_err(parent, "failed to register alternate mode (%d)\n",
  431. ret);
  432. put_device(&alt->adev.dev);
  433. return ERR_PTR(ret);
  434. }
  435. return &alt->adev;
  436. }
  437. /**
  438. * typec_unregister_altmode - Unregister Alternate Mode
  439. * @adev: The alternate mode to be unregistered
  440. *
  441. * Unregister device created with typec_partner_register_altmode(),
  442. * typec_plug_register_altmode() or typec_port_register_altmode().
  443. */
  444. void typec_unregister_altmode(struct typec_altmode *adev)
  445. {
  446. if (IS_ERR_OR_NULL(adev))
  447. return;
  448. typec_mux_put(to_altmode(adev)->mux);
  449. device_unregister(&adev->dev);
  450. }
  451. EXPORT_SYMBOL_GPL(typec_unregister_altmode);
  452. /* ------------------------------------------------------------------------- */
  453. /* Type-C Partners */
  454. static ssize_t accessory_mode_show(struct device *dev,
  455. struct device_attribute *attr,
  456. char *buf)
  457. {
  458. struct typec_partner *p = to_typec_partner(dev);
  459. return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
  460. }
  461. static DEVICE_ATTR_RO(accessory_mode);
  462. static ssize_t supports_usb_power_delivery_show(struct device *dev,
  463. struct device_attribute *attr,
  464. char *buf)
  465. {
  466. struct typec_partner *p = to_typec_partner(dev);
  467. return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
  468. }
  469. static DEVICE_ATTR_RO(supports_usb_power_delivery);
  470. static struct attribute *typec_partner_attrs[] = {
  471. &dev_attr_accessory_mode.attr,
  472. &dev_attr_supports_usb_power_delivery.attr,
  473. NULL
  474. };
  475. ATTRIBUTE_GROUPS(typec_partner);
  476. static void typec_partner_release(struct device *dev)
  477. {
  478. struct typec_partner *partner = to_typec_partner(dev);
  479. ida_destroy(&partner->mode_ids);
  480. kfree(partner);
  481. }
  482. static const struct device_type typec_partner_dev_type = {
  483. .name = "typec_partner",
  484. .groups = typec_partner_groups,
  485. .release = typec_partner_release,
  486. };
  487. /**
  488. * typec_partner_set_identity - Report result from Discover Identity command
  489. * @partner: The partner updated identity values
  490. *
  491. * This routine is used to report that the result of Discover Identity USB power
  492. * delivery command has become available.
  493. */
  494. int typec_partner_set_identity(struct typec_partner *partner)
  495. {
  496. if (!partner->identity)
  497. return -EINVAL;
  498. typec_report_identity(&partner->dev);
  499. return 0;
  500. }
  501. EXPORT_SYMBOL_GPL(typec_partner_set_identity);
  502. /**
  503. * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
  504. * @partner: USB Type-C Partner that supports the alternate mode
  505. * @desc: Description of the alternate mode
  506. *
  507. * This routine is used to register each alternate mode individually that
  508. * @partner has listed in response to Discover SVIDs command. The modes for a
  509. * SVID listed in response to Discover Modes command need to be listed in an
  510. * array in @desc.
  511. *
  512. * Returns handle to the alternate mode on success or NULL on failure.
  513. */
  514. struct typec_altmode *
  515. typec_partner_register_altmode(struct typec_partner *partner,
  516. const struct typec_altmode_desc *desc)
  517. {
  518. return typec_register_altmode(&partner->dev, desc);
  519. }
  520. EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
  521. /**
  522. * typec_register_partner - Register a USB Type-C Partner
  523. * @port: The USB Type-C Port the partner is connected to
  524. * @desc: Description of the partner
  525. *
  526. * Registers a device for USB Type-C Partner described in @desc.
  527. *
  528. * Returns handle to the partner on success or ERR_PTR on failure.
  529. */
  530. struct typec_partner *typec_register_partner(struct typec_port *port,
  531. struct typec_partner_desc *desc)
  532. {
  533. struct typec_partner *partner;
  534. int ret;
  535. partner = kzalloc(sizeof(*partner), GFP_KERNEL);
  536. if (!partner)
  537. return ERR_PTR(-ENOMEM);
  538. ida_init(&partner->mode_ids);
  539. partner->usb_pd = desc->usb_pd;
  540. partner->accessory = desc->accessory;
  541. if (desc->identity) {
  542. /*
  543. * Creating directory for the identity only if the driver is
  544. * able to provide data to it.
  545. */
  546. partner->dev.groups = usb_pd_id_groups;
  547. partner->identity = desc->identity;
  548. }
  549. partner->dev.class = typec_class;
  550. partner->dev.parent = &port->dev;
  551. partner->dev.type = &typec_partner_dev_type;
  552. dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
  553. ret = device_register(&partner->dev);
  554. if (ret) {
  555. dev_err(&port->dev, "failed to register partner (%d)\n", ret);
  556. put_device(&partner->dev);
  557. return ERR_PTR(ret);
  558. }
  559. return partner;
  560. }
  561. EXPORT_SYMBOL_GPL(typec_register_partner);
  562. /**
  563. * typec_unregister_partner - Unregister a USB Type-C Partner
  564. * @partner: The partner to be unregistered
  565. *
  566. * Unregister device created with typec_register_partner().
  567. */
  568. void typec_unregister_partner(struct typec_partner *partner)
  569. {
  570. if (!IS_ERR_OR_NULL(partner))
  571. device_unregister(&partner->dev);
  572. }
  573. EXPORT_SYMBOL_GPL(typec_unregister_partner);
  574. /* ------------------------------------------------------------------------- */
  575. /* Type-C Cable Plugs */
  576. static void typec_plug_release(struct device *dev)
  577. {
  578. struct typec_plug *plug = to_typec_plug(dev);
  579. ida_destroy(&plug->mode_ids);
  580. kfree(plug);
  581. }
  582. static const struct device_type typec_plug_dev_type = {
  583. .name = "typec_plug",
  584. .release = typec_plug_release,
  585. };
  586. /**
  587. * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
  588. * @plug: USB Type-C Cable Plug that supports the alternate mode
  589. * @desc: Description of the alternate mode
  590. *
  591. * This routine is used to register each alternate mode individually that @plug
  592. * has listed in response to Discover SVIDs command. The modes for a SVID that
  593. * the plug lists in response to Discover Modes command need to be listed in an
  594. * array in @desc.
  595. *
  596. * Returns handle to the alternate mode on success or ERR_PTR on failure.
  597. */
  598. struct typec_altmode *
  599. typec_plug_register_altmode(struct typec_plug *plug,
  600. const struct typec_altmode_desc *desc)
  601. {
  602. return typec_register_altmode(&plug->dev, desc);
  603. }
  604. EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
  605. /**
  606. * typec_register_plug - Register a USB Type-C Cable Plug
  607. * @cable: USB Type-C Cable with the plug
  608. * @desc: Description of the cable plug
  609. *
  610. * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
  611. * Cable Plug represents a plug with electronics in it that can response to USB
  612. * Power Delivery SOP Prime or SOP Double Prime packages.
  613. *
  614. * Returns handle to the cable plug on success or ERR_PTR on failure.
  615. */
  616. struct typec_plug *typec_register_plug(struct typec_cable *cable,
  617. struct typec_plug_desc *desc)
  618. {
  619. struct typec_plug *plug;
  620. char name[8];
  621. int ret;
  622. plug = kzalloc(sizeof(*plug), GFP_KERNEL);
  623. if (!plug)
  624. return ERR_PTR(-ENOMEM);
  625. sprintf(name, "plug%d", desc->index);
  626. ida_init(&plug->mode_ids);
  627. plug->index = desc->index;
  628. plug->dev.class = typec_class;
  629. plug->dev.parent = &cable->dev;
  630. plug->dev.type = &typec_plug_dev_type;
  631. dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
  632. ret = device_register(&plug->dev);
  633. if (ret) {
  634. dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
  635. put_device(&plug->dev);
  636. return ERR_PTR(ret);
  637. }
  638. return plug;
  639. }
  640. EXPORT_SYMBOL_GPL(typec_register_plug);
  641. /**
  642. * typec_unregister_plug - Unregister a USB Type-C Cable Plug
  643. * @plug: The cable plug to be unregistered
  644. *
  645. * Unregister device created with typec_register_plug().
  646. */
  647. void typec_unregister_plug(struct typec_plug *plug)
  648. {
  649. if (!IS_ERR_OR_NULL(plug))
  650. device_unregister(&plug->dev);
  651. }
  652. EXPORT_SYMBOL_GPL(typec_unregister_plug);
  653. /* Type-C Cables */
  654. static ssize_t
  655. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  656. {
  657. struct typec_cable *cable = to_typec_cable(dev);
  658. return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
  659. }
  660. static DEVICE_ATTR_RO(type);
  661. static const char * const typec_plug_types[] = {
  662. [USB_PLUG_NONE] = "unknown",
  663. [USB_PLUG_TYPE_A] = "type-a",
  664. [USB_PLUG_TYPE_B] = "type-b",
  665. [USB_PLUG_TYPE_C] = "type-c",
  666. [USB_PLUG_CAPTIVE] = "captive",
  667. };
  668. static ssize_t plug_type_show(struct device *dev,
  669. struct device_attribute *attr, char *buf)
  670. {
  671. struct typec_cable *cable = to_typec_cable(dev);
  672. return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
  673. }
  674. static DEVICE_ATTR_RO(plug_type);
  675. static struct attribute *typec_cable_attrs[] = {
  676. &dev_attr_type.attr,
  677. &dev_attr_plug_type.attr,
  678. NULL
  679. };
  680. ATTRIBUTE_GROUPS(typec_cable);
  681. static void typec_cable_release(struct device *dev)
  682. {
  683. struct typec_cable *cable = to_typec_cable(dev);
  684. kfree(cable);
  685. }
  686. static const struct device_type typec_cable_dev_type = {
  687. .name = "typec_cable",
  688. .groups = typec_cable_groups,
  689. .release = typec_cable_release,
  690. };
  691. /**
  692. * typec_cable_set_identity - Report result from Discover Identity command
  693. * @cable: The cable updated identity values
  694. *
  695. * This routine is used to report that the result of Discover Identity USB power
  696. * delivery command has become available.
  697. */
  698. int typec_cable_set_identity(struct typec_cable *cable)
  699. {
  700. if (!cable->identity)
  701. return -EINVAL;
  702. typec_report_identity(&cable->dev);
  703. return 0;
  704. }
  705. EXPORT_SYMBOL_GPL(typec_cable_set_identity);
  706. /**
  707. * typec_register_cable - Register a USB Type-C Cable
  708. * @port: The USB Type-C Port the cable is connected to
  709. * @desc: Description of the cable
  710. *
  711. * Registers a device for USB Type-C Cable described in @desc. The cable will be
  712. * parent for the optional cable plug devises.
  713. *
  714. * Returns handle to the cable on success or ERR_PTR on failure.
  715. */
  716. struct typec_cable *typec_register_cable(struct typec_port *port,
  717. struct typec_cable_desc *desc)
  718. {
  719. struct typec_cable *cable;
  720. int ret;
  721. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  722. if (!cable)
  723. return ERR_PTR(-ENOMEM);
  724. cable->type = desc->type;
  725. cable->active = desc->active;
  726. if (desc->identity) {
  727. /*
  728. * Creating directory for the identity only if the driver is
  729. * able to provide data to it.
  730. */
  731. cable->dev.groups = usb_pd_id_groups;
  732. cable->identity = desc->identity;
  733. }
  734. cable->dev.class = typec_class;
  735. cable->dev.parent = &port->dev;
  736. cable->dev.type = &typec_cable_dev_type;
  737. dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
  738. ret = device_register(&cable->dev);
  739. if (ret) {
  740. dev_err(&port->dev, "failed to register cable (%d)\n", ret);
  741. put_device(&cable->dev);
  742. return ERR_PTR(ret);
  743. }
  744. return cable;
  745. }
  746. EXPORT_SYMBOL_GPL(typec_register_cable);
  747. /**
  748. * typec_unregister_cable - Unregister a USB Type-C Cable
  749. * @cable: The cable to be unregistered
  750. *
  751. * Unregister device created with typec_register_cable().
  752. */
  753. void typec_unregister_cable(struct typec_cable *cable)
  754. {
  755. if (!IS_ERR_OR_NULL(cable))
  756. device_unregister(&cable->dev);
  757. }
  758. EXPORT_SYMBOL_GPL(typec_unregister_cable);
  759. /* ------------------------------------------------------------------------- */
  760. /* USB Type-C ports */
  761. static const char * const typec_roles[] = {
  762. [TYPEC_SINK] = "sink",
  763. [TYPEC_SOURCE] = "source",
  764. };
  765. static const char * const typec_data_roles[] = {
  766. [TYPEC_DEVICE] = "device",
  767. [TYPEC_HOST] = "host",
  768. };
  769. static const char * const typec_port_power_roles[] = {
  770. [TYPEC_PORT_SRC] = "source",
  771. [TYPEC_PORT_SNK] = "sink",
  772. [TYPEC_PORT_DRP] = "dual",
  773. };
  774. static const char * const typec_port_data_roles[] = {
  775. [TYPEC_PORT_DFP] = "host",
  776. [TYPEC_PORT_UFP] = "device",
  777. [TYPEC_PORT_DRD] = "dual",
  778. };
  779. static const char * const typec_port_types_drp[] = {
  780. [TYPEC_PORT_SRC] = "dual [source] sink",
  781. [TYPEC_PORT_SNK] = "dual source [sink]",
  782. [TYPEC_PORT_DRP] = "[dual] source sink",
  783. };
  784. static ssize_t
  785. preferred_role_store(struct device *dev, struct device_attribute *attr,
  786. const char *buf, size_t size)
  787. {
  788. struct typec_port *port = to_typec_port(dev);
  789. int role;
  790. int ret;
  791. if (port->cap->type != TYPEC_PORT_DRP) {
  792. dev_dbg(dev, "Preferred role only supported with DRP ports\n");
  793. return -EOPNOTSUPP;
  794. }
  795. if (!port->cap->try_role) {
  796. dev_dbg(dev, "Setting preferred role not supported\n");
  797. return -EOPNOTSUPP;
  798. }
  799. role = sysfs_match_string(typec_roles, buf);
  800. if (role < 0) {
  801. if (sysfs_streq(buf, "none"))
  802. role = TYPEC_NO_PREFERRED_ROLE;
  803. else
  804. return -EINVAL;
  805. }
  806. ret = port->cap->try_role(port->cap, role);
  807. if (ret)
  808. return ret;
  809. port->prefer_role = role;
  810. return size;
  811. }
  812. static ssize_t
  813. preferred_role_show(struct device *dev, struct device_attribute *attr,
  814. char *buf)
  815. {
  816. struct typec_port *port = to_typec_port(dev);
  817. if (port->cap->type != TYPEC_PORT_DRP)
  818. return 0;
  819. if (port->prefer_role < 0)
  820. return 0;
  821. return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
  822. }
  823. static DEVICE_ATTR_RW(preferred_role);
  824. static ssize_t data_role_store(struct device *dev,
  825. struct device_attribute *attr,
  826. const char *buf, size_t size)
  827. {
  828. struct typec_port *port = to_typec_port(dev);
  829. int ret;
  830. if (!port->cap->dr_set) {
  831. dev_dbg(dev, "data role swapping not supported\n");
  832. return -EOPNOTSUPP;
  833. }
  834. ret = sysfs_match_string(typec_data_roles, buf);
  835. if (ret < 0)
  836. return ret;
  837. mutex_lock(&port->port_type_lock);
  838. if (port->cap->data != TYPEC_PORT_DRD) {
  839. ret = -EOPNOTSUPP;
  840. goto unlock_and_ret;
  841. }
  842. ret = port->cap->dr_set(port->cap, ret);
  843. if (ret)
  844. goto unlock_and_ret;
  845. ret = size;
  846. unlock_and_ret:
  847. mutex_unlock(&port->port_type_lock);
  848. return ret;
  849. }
  850. static ssize_t data_role_show(struct device *dev,
  851. struct device_attribute *attr, char *buf)
  852. {
  853. struct typec_port *port = to_typec_port(dev);
  854. if (port->cap->data == TYPEC_PORT_DRD)
  855. return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
  856. "[host] device" : "host [device]");
  857. return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
  858. }
  859. static DEVICE_ATTR_RW(data_role);
  860. static ssize_t power_role_store(struct device *dev,
  861. struct device_attribute *attr,
  862. const char *buf, size_t size)
  863. {
  864. struct typec_port *port = to_typec_port(dev);
  865. int ret;
  866. if (!port->cap->pd_revision) {
  867. dev_dbg(dev, "USB Power Delivery not supported\n");
  868. return -EOPNOTSUPP;
  869. }
  870. if (!port->cap->pr_set) {
  871. dev_dbg(dev, "power role swapping not supported\n");
  872. return -EOPNOTSUPP;
  873. }
  874. if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
  875. dev_dbg(dev, "partner unable to swap power role\n");
  876. return -EIO;
  877. }
  878. ret = sysfs_match_string(typec_roles, buf);
  879. if (ret < 0)
  880. return ret;
  881. mutex_lock(&port->port_type_lock);
  882. if (port->port_type != TYPEC_PORT_DRP) {
  883. dev_dbg(dev, "port type fixed at \"%s\"",
  884. typec_port_power_roles[port->port_type]);
  885. ret = -EOPNOTSUPP;
  886. goto unlock_and_ret;
  887. }
  888. ret = port->cap->pr_set(port->cap, ret);
  889. if (ret)
  890. goto unlock_and_ret;
  891. ret = size;
  892. unlock_and_ret:
  893. mutex_unlock(&port->port_type_lock);
  894. return ret;
  895. }
  896. static ssize_t power_role_show(struct device *dev,
  897. struct device_attribute *attr, char *buf)
  898. {
  899. struct typec_port *port = to_typec_port(dev);
  900. if (port->cap->type == TYPEC_PORT_DRP)
  901. return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
  902. "[source] sink" : "source [sink]");
  903. return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
  904. }
  905. static DEVICE_ATTR_RW(power_role);
  906. static ssize_t
  907. port_type_store(struct device *dev, struct device_attribute *attr,
  908. const char *buf, size_t size)
  909. {
  910. struct typec_port *port = to_typec_port(dev);
  911. int ret;
  912. enum typec_port_type type;
  913. if (!port->cap->port_type_set || port->cap->type != TYPEC_PORT_DRP) {
  914. dev_dbg(dev, "changing port type not supported\n");
  915. return -EOPNOTSUPP;
  916. }
  917. ret = sysfs_match_string(typec_port_power_roles, buf);
  918. if (ret < 0)
  919. return ret;
  920. type = ret;
  921. mutex_lock(&port->port_type_lock);
  922. if (port->port_type == type) {
  923. ret = size;
  924. goto unlock_and_ret;
  925. }
  926. ret = port->cap->port_type_set(port->cap, type);
  927. if (ret)
  928. goto unlock_and_ret;
  929. port->port_type = type;
  930. ret = size;
  931. unlock_and_ret:
  932. mutex_unlock(&port->port_type_lock);
  933. return ret;
  934. }
  935. static ssize_t
  936. port_type_show(struct device *dev, struct device_attribute *attr,
  937. char *buf)
  938. {
  939. struct typec_port *port = to_typec_port(dev);
  940. if (port->cap->type == TYPEC_PORT_DRP)
  941. return sprintf(buf, "%s\n",
  942. typec_port_types_drp[port->port_type]);
  943. return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
  944. }
  945. static DEVICE_ATTR_RW(port_type);
  946. static const char * const typec_pwr_opmodes[] = {
  947. [TYPEC_PWR_MODE_USB] = "default",
  948. [TYPEC_PWR_MODE_1_5A] = "1.5A",
  949. [TYPEC_PWR_MODE_3_0A] = "3.0A",
  950. [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
  951. };
  952. static ssize_t power_operation_mode_show(struct device *dev,
  953. struct device_attribute *attr,
  954. char *buf)
  955. {
  956. struct typec_port *port = to_typec_port(dev);
  957. return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
  958. }
  959. static DEVICE_ATTR_RO(power_operation_mode);
  960. static ssize_t vconn_source_store(struct device *dev,
  961. struct device_attribute *attr,
  962. const char *buf, size_t size)
  963. {
  964. struct typec_port *port = to_typec_port(dev);
  965. bool source;
  966. int ret;
  967. if (!port->cap->pd_revision) {
  968. dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
  969. return -EOPNOTSUPP;
  970. }
  971. if (!port->cap->vconn_set) {
  972. dev_dbg(dev, "VCONN swapping not supported\n");
  973. return -EOPNOTSUPP;
  974. }
  975. ret = kstrtobool(buf, &source);
  976. if (ret)
  977. return ret;
  978. ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
  979. if (ret)
  980. return ret;
  981. return size;
  982. }
  983. static ssize_t vconn_source_show(struct device *dev,
  984. struct device_attribute *attr, char *buf)
  985. {
  986. struct typec_port *port = to_typec_port(dev);
  987. return sprintf(buf, "%s\n",
  988. port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
  989. }
  990. static DEVICE_ATTR_RW(vconn_source);
  991. static ssize_t supported_accessory_modes_show(struct device *dev,
  992. struct device_attribute *attr,
  993. char *buf)
  994. {
  995. struct typec_port *port = to_typec_port(dev);
  996. ssize_t ret = 0;
  997. int i;
  998. for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
  999. if (port->cap->accessory[i])
  1000. ret += sprintf(buf + ret, "%s ",
  1001. typec_accessory_modes[port->cap->accessory[i]]);
  1002. }
  1003. if (!ret)
  1004. return sprintf(buf, "none\n");
  1005. buf[ret - 1] = '\n';
  1006. return ret;
  1007. }
  1008. static DEVICE_ATTR_RO(supported_accessory_modes);
  1009. static ssize_t usb_typec_revision_show(struct device *dev,
  1010. struct device_attribute *attr,
  1011. char *buf)
  1012. {
  1013. struct typec_port *port = to_typec_port(dev);
  1014. u16 rev = port->cap->revision;
  1015. return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
  1016. }
  1017. static DEVICE_ATTR_RO(usb_typec_revision);
  1018. static ssize_t usb_power_delivery_revision_show(struct device *dev,
  1019. struct device_attribute *attr,
  1020. char *buf)
  1021. {
  1022. struct typec_port *p = to_typec_port(dev);
  1023. return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
  1024. }
  1025. static DEVICE_ATTR_RO(usb_power_delivery_revision);
  1026. static struct attribute *typec_attrs[] = {
  1027. &dev_attr_data_role.attr,
  1028. &dev_attr_power_operation_mode.attr,
  1029. &dev_attr_power_role.attr,
  1030. &dev_attr_preferred_role.attr,
  1031. &dev_attr_supported_accessory_modes.attr,
  1032. &dev_attr_usb_power_delivery_revision.attr,
  1033. &dev_attr_usb_typec_revision.attr,
  1034. &dev_attr_vconn_source.attr,
  1035. &dev_attr_port_type.attr,
  1036. NULL,
  1037. };
  1038. ATTRIBUTE_GROUPS(typec);
  1039. static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
  1040. {
  1041. int ret;
  1042. ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
  1043. if (ret)
  1044. dev_err(dev, "failed to add uevent TYPEC_PORT\n");
  1045. return ret;
  1046. }
  1047. static void typec_release(struct device *dev)
  1048. {
  1049. struct typec_port *port = to_typec_port(dev);
  1050. ida_simple_remove(&typec_index_ida, port->id);
  1051. ida_destroy(&port->mode_ids);
  1052. typec_switch_put(port->sw);
  1053. typec_mux_put(port->mux);
  1054. kfree(port);
  1055. }
  1056. const struct device_type typec_port_dev_type = {
  1057. .name = "typec_port",
  1058. .groups = typec_groups,
  1059. .uevent = typec_uevent,
  1060. .release = typec_release,
  1061. };
  1062. /* --------------------------------------- */
  1063. /* Driver callbacks to report role updates */
  1064. /**
  1065. * typec_set_data_role - Report data role change
  1066. * @port: The USB Type-C Port where the role was changed
  1067. * @role: The new data role
  1068. *
  1069. * This routine is used by the port drivers to report data role changes.
  1070. */
  1071. void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
  1072. {
  1073. if (port->data_role == role)
  1074. return;
  1075. port->data_role = role;
  1076. sysfs_notify(&port->dev.kobj, NULL, "data_role");
  1077. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1078. }
  1079. EXPORT_SYMBOL_GPL(typec_set_data_role);
  1080. /**
  1081. * typec_set_pwr_role - Report power role change
  1082. * @port: The USB Type-C Port where the role was changed
  1083. * @role: The new data role
  1084. *
  1085. * This routine is used by the port drivers to report power role changes.
  1086. */
  1087. void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
  1088. {
  1089. if (port->pwr_role == role)
  1090. return;
  1091. port->pwr_role = role;
  1092. sysfs_notify(&port->dev.kobj, NULL, "power_role");
  1093. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1094. }
  1095. EXPORT_SYMBOL_GPL(typec_set_pwr_role);
  1096. /**
  1097. * typec_set_vconn_role - Report VCONN source change
  1098. * @port: The USB Type-C Port which VCONN role changed
  1099. * @role: Source when @port is sourcing VCONN, or Sink when it's not
  1100. *
  1101. * This routine is used by the port drivers to report if the VCONN source is
  1102. * changes.
  1103. */
  1104. void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
  1105. {
  1106. if (port->vconn_role == role)
  1107. return;
  1108. port->vconn_role = role;
  1109. sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
  1110. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1111. }
  1112. EXPORT_SYMBOL_GPL(typec_set_vconn_role);
  1113. static int partner_match(struct device *dev, void *data)
  1114. {
  1115. return is_typec_partner(dev);
  1116. }
  1117. /**
  1118. * typec_set_pwr_opmode - Report changed power operation mode
  1119. * @port: The USB Type-C Port where the mode was changed
  1120. * @opmode: New power operation mode
  1121. *
  1122. * This routine is used by the port drivers to report changed power operation
  1123. * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
  1124. * Type-C specification, and "USB Power Delivery" when the power levels are
  1125. * negotiated with methods defined in USB Power Delivery specification.
  1126. */
  1127. void typec_set_pwr_opmode(struct typec_port *port,
  1128. enum typec_pwr_opmode opmode)
  1129. {
  1130. struct device *partner_dev;
  1131. if (port->pwr_opmode == opmode)
  1132. return;
  1133. port->pwr_opmode = opmode;
  1134. sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
  1135. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1136. partner_dev = device_find_child(&port->dev, NULL, partner_match);
  1137. if (partner_dev) {
  1138. struct typec_partner *partner = to_typec_partner(partner_dev);
  1139. if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
  1140. partner->usb_pd = 1;
  1141. sysfs_notify(&partner_dev->kobj, NULL,
  1142. "supports_usb_power_delivery");
  1143. }
  1144. put_device(partner_dev);
  1145. }
  1146. }
  1147. EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
  1148. /**
  1149. * typec_find_port_power_role - Get the typec port power capability
  1150. * @name: port power capability string
  1151. *
  1152. * This routine is used to find the typec_port_type by its string name.
  1153. *
  1154. * Returns typec_port_type if success, otherwise negative error code.
  1155. */
  1156. int typec_find_port_power_role(const char *name)
  1157. {
  1158. return match_string(typec_port_power_roles,
  1159. ARRAY_SIZE(typec_port_power_roles), name);
  1160. }
  1161. EXPORT_SYMBOL_GPL(typec_find_port_power_role);
  1162. /**
  1163. * typec_find_power_role - Find the typec one specific power role
  1164. * @name: power role string
  1165. *
  1166. * This routine is used to find the typec_role by its string name.
  1167. *
  1168. * Returns typec_role if success, otherwise negative error code.
  1169. */
  1170. int typec_find_power_role(const char *name)
  1171. {
  1172. return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
  1173. }
  1174. EXPORT_SYMBOL_GPL(typec_find_power_role);
  1175. /**
  1176. * typec_find_port_data_role - Get the typec port data capability
  1177. * @name: port data capability string
  1178. *
  1179. * This routine is used to find the typec_port_data by its string name.
  1180. *
  1181. * Returns typec_port_data if success, otherwise negative error code.
  1182. */
  1183. int typec_find_port_data_role(const char *name)
  1184. {
  1185. return match_string(typec_port_data_roles,
  1186. ARRAY_SIZE(typec_port_data_roles), name);
  1187. }
  1188. EXPORT_SYMBOL_GPL(typec_find_port_data_role);
  1189. /* ------------------------------------------ */
  1190. /* API for Multiplexer/DeMultiplexer Switches */
  1191. /**
  1192. * typec_set_orientation - Set USB Type-C cable plug orientation
  1193. * @port: USB Type-C Port
  1194. * @orientation: USB Type-C cable plug orientation
  1195. *
  1196. * Set cable plug orientation for @port.
  1197. */
  1198. int typec_set_orientation(struct typec_port *port,
  1199. enum typec_orientation orientation)
  1200. {
  1201. int ret;
  1202. if (port->sw) {
  1203. ret = port->sw->set(port->sw, orientation);
  1204. if (ret)
  1205. return ret;
  1206. }
  1207. port->orientation = orientation;
  1208. return 0;
  1209. }
  1210. EXPORT_SYMBOL_GPL(typec_set_orientation);
  1211. /**
  1212. * typec_get_orientation - Get USB Type-C cable plug orientation
  1213. * @port: USB Type-C Port
  1214. *
  1215. * Get current cable plug orientation for @port.
  1216. */
  1217. enum typec_orientation typec_get_orientation(struct typec_port *port)
  1218. {
  1219. return port->orientation;
  1220. }
  1221. EXPORT_SYMBOL_GPL(typec_get_orientation);
  1222. /**
  1223. * typec_set_mode - Set mode of operation for USB Type-C connector
  1224. * @port: USB Type-C connector
  1225. * @mode: Accessory Mode, USB Operation or Safe State
  1226. *
  1227. * Configure @port for Accessory Mode @mode. This function will configure the
  1228. * muxes needed for @mode.
  1229. */
  1230. int typec_set_mode(struct typec_port *port, int mode)
  1231. {
  1232. return port->mux ? port->mux->set(port->mux, mode) : 0;
  1233. }
  1234. EXPORT_SYMBOL_GPL(typec_set_mode);
  1235. /* --------------------------------------- */
  1236. /**
  1237. * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
  1238. * @port: USB Type-C Port that supports the alternate mode
  1239. * @desc: Description of the alternate mode
  1240. *
  1241. * This routine is used to register an alternate mode that @port is capable of
  1242. * supporting.
  1243. *
  1244. * Returns handle to the alternate mode on success or ERR_PTR on failure.
  1245. */
  1246. struct typec_altmode *
  1247. typec_port_register_altmode(struct typec_port *port,
  1248. const struct typec_altmode_desc *desc)
  1249. {
  1250. struct typec_altmode *adev;
  1251. struct typec_mux *mux;
  1252. mux = typec_mux_get(&port->dev, desc);
  1253. if (IS_ERR(mux))
  1254. return ERR_CAST(mux);
  1255. adev = typec_register_altmode(&port->dev, desc);
  1256. if (IS_ERR(adev))
  1257. typec_mux_put(mux);
  1258. else
  1259. to_altmode(adev)->mux = mux;
  1260. return adev;
  1261. }
  1262. EXPORT_SYMBOL_GPL(typec_port_register_altmode);
  1263. /**
  1264. * typec_register_port - Register a USB Type-C Port
  1265. * @parent: Parent device
  1266. * @cap: Description of the port
  1267. *
  1268. * Registers a device for USB Type-C Port described in @cap.
  1269. *
  1270. * Returns handle to the port on success or ERR_PTR on failure.
  1271. */
  1272. struct typec_port *typec_register_port(struct device *parent,
  1273. const struct typec_capability *cap)
  1274. {
  1275. struct typec_port *port;
  1276. int ret;
  1277. int id;
  1278. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1279. if (!port)
  1280. return ERR_PTR(-ENOMEM);
  1281. id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
  1282. if (id < 0) {
  1283. kfree(port);
  1284. return ERR_PTR(id);
  1285. }
  1286. switch (cap->type) {
  1287. case TYPEC_PORT_SRC:
  1288. port->pwr_role = TYPEC_SOURCE;
  1289. port->vconn_role = TYPEC_SOURCE;
  1290. break;
  1291. case TYPEC_PORT_SNK:
  1292. port->pwr_role = TYPEC_SINK;
  1293. port->vconn_role = TYPEC_SINK;
  1294. break;
  1295. case TYPEC_PORT_DRP:
  1296. if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
  1297. port->pwr_role = cap->prefer_role;
  1298. else
  1299. port->pwr_role = TYPEC_SINK;
  1300. break;
  1301. }
  1302. switch (cap->data) {
  1303. case TYPEC_PORT_DFP:
  1304. port->data_role = TYPEC_HOST;
  1305. break;
  1306. case TYPEC_PORT_UFP:
  1307. port->data_role = TYPEC_DEVICE;
  1308. break;
  1309. case TYPEC_PORT_DRD:
  1310. if (cap->prefer_role == TYPEC_SOURCE)
  1311. port->data_role = TYPEC_HOST;
  1312. else
  1313. port->data_role = TYPEC_DEVICE;
  1314. break;
  1315. }
  1316. ida_init(&port->mode_ids);
  1317. mutex_init(&port->port_type_lock);
  1318. port->id = id;
  1319. port->cap = cap;
  1320. port->port_type = cap->type;
  1321. port->prefer_role = cap->prefer_role;
  1322. device_initialize(&port->dev);
  1323. port->dev.class = typec_class;
  1324. port->dev.parent = parent;
  1325. port->dev.fwnode = cap->fwnode;
  1326. port->dev.type = &typec_port_dev_type;
  1327. dev_set_name(&port->dev, "port%d", id);
  1328. port->sw = typec_switch_get(&port->dev);
  1329. if (IS_ERR(port->sw)) {
  1330. ret = PTR_ERR(port->sw);
  1331. put_device(&port->dev);
  1332. return ERR_PTR(ret);
  1333. }
  1334. port->mux = typec_mux_get(&port->dev, NULL);
  1335. if (IS_ERR(port->mux)) {
  1336. ret = PTR_ERR(port->mux);
  1337. put_device(&port->dev);
  1338. return ERR_PTR(ret);
  1339. }
  1340. ret = device_add(&port->dev);
  1341. if (ret) {
  1342. dev_err(parent, "failed to register port (%d)\n", ret);
  1343. put_device(&port->dev);
  1344. return ERR_PTR(ret);
  1345. }
  1346. return port;
  1347. }
  1348. EXPORT_SYMBOL_GPL(typec_register_port);
  1349. /**
  1350. * typec_unregister_port - Unregister a USB Type-C Port
  1351. * @port: The port to be unregistered
  1352. *
  1353. * Unregister device created with typec_register_port().
  1354. */
  1355. void typec_unregister_port(struct typec_port *port)
  1356. {
  1357. if (!IS_ERR_OR_NULL(port))
  1358. device_unregister(&port->dev);
  1359. }
  1360. EXPORT_SYMBOL_GPL(typec_unregister_port);
  1361. static int __init typec_init(void)
  1362. {
  1363. int ret;
  1364. ret = bus_register(&typec_bus);
  1365. if (ret)
  1366. return ret;
  1367. ret = class_register(&typec_mux_class);
  1368. if (ret)
  1369. goto err_unregister_bus;
  1370. typec_class = class_create(THIS_MODULE, "typec");
  1371. if (IS_ERR(typec_class)) {
  1372. ret = PTR_ERR(typec_class);
  1373. goto err_unregister_mux_class;
  1374. }
  1375. return 0;
  1376. err_unregister_mux_class:
  1377. class_unregister(&typec_mux_class);
  1378. err_unregister_bus:
  1379. bus_unregister(&typec_bus);
  1380. return ret;
  1381. }
  1382. subsys_initcall(typec_init);
  1383. static void __exit typec_exit(void)
  1384. {
  1385. class_destroy(typec_class);
  1386. ida_destroy(&typec_index_ida);
  1387. bus_unregister(&typec_bus);
  1388. class_unregister(&typec_mux_class);
  1389. }
  1390. module_exit(typec_exit);
  1391. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  1392. MODULE_LICENSE("GPL v2");
  1393. MODULE_DESCRIPTION("USB Type-C Connector Class");