class.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  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 <linux/usb/pd_vdo.h>
  14. #include <linux/android_kabi.h>
  15. #include "bus.h"
  16. struct typec_plug {
  17. struct device dev;
  18. enum typec_plug_index index;
  19. struct ida mode_ids;
  20. int num_altmodes;
  21. ANDROID_KABI_RESERVE(1);
  22. };
  23. struct typec_cable {
  24. struct device dev;
  25. enum typec_plug_type type;
  26. struct usb_pd_identity *identity;
  27. unsigned int active:1;
  28. u16 pd_revision; /* 0300H = "3.0" */
  29. ANDROID_KABI_RESERVE(1);
  30. };
  31. struct typec_partner {
  32. struct device dev;
  33. unsigned int usb_pd:1;
  34. struct usb_pd_identity *identity;
  35. enum typec_accessory accessory;
  36. struct ida mode_ids;
  37. int num_altmodes;
  38. u16 pd_revision; /* 0300H = "3.0" */
  39. enum usb_pd_svdm_ver svdm_version;
  40. ANDROID_KABI_RESERVE(1);
  41. };
  42. struct typec_port {
  43. unsigned int id;
  44. struct device dev;
  45. struct ida mode_ids;
  46. int prefer_role;
  47. enum typec_data_role data_role;
  48. enum typec_role pwr_role;
  49. enum typec_role vconn_role;
  50. enum typec_pwr_opmode pwr_opmode;
  51. enum typec_port_type port_type;
  52. struct mutex port_type_lock;
  53. enum typec_orientation orientation;
  54. struct typec_switch *sw;
  55. struct typec_mux *mux;
  56. const struct typec_capability *cap;
  57. const struct typec_operations *ops;
  58. ANDROID_KABI_RESERVE(1);
  59. };
  60. #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
  61. #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
  62. #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
  63. #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
  64. static const struct device_type typec_partner_dev_type;
  65. static const struct device_type typec_cable_dev_type;
  66. static const struct device_type typec_plug_dev_type;
  67. #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
  68. #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
  69. #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
  70. static DEFINE_IDA(typec_index_ida);
  71. static struct class *typec_class;
  72. /* ------------------------------------------------------------------------- */
  73. /* Common attributes */
  74. static const char * const typec_accessory_modes[] = {
  75. [TYPEC_ACCESSORY_NONE] = "none",
  76. [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
  77. [TYPEC_ACCESSORY_DEBUG] = "debug",
  78. };
  79. /* Product types defined in USB PD Specification R3.0 V2.0 */
  80. static const char * const product_type_ufp[8] = {
  81. [IDH_PTYPE_NOT_UFP] = "not_ufp",
  82. [IDH_PTYPE_HUB] = "hub",
  83. [IDH_PTYPE_PERIPH] = "peripheral",
  84. [IDH_PTYPE_PSD] = "psd",
  85. [IDH_PTYPE_AMA] = "ama",
  86. };
  87. static const char * const product_type_dfp[8] = {
  88. [IDH_PTYPE_NOT_DFP] = "not_dfp",
  89. [IDH_PTYPE_DFP_HUB] = "hub",
  90. [IDH_PTYPE_DFP_HOST] = "host",
  91. [IDH_PTYPE_DFP_PB] = "power_brick",
  92. };
  93. static const char * const product_type_cable[8] = {
  94. [IDH_PTYPE_NOT_CABLE] = "not_cable",
  95. [IDH_PTYPE_PCABLE] = "passive",
  96. [IDH_PTYPE_ACABLE] = "active",
  97. [IDH_PTYPE_VPD] = "vpd",
  98. };
  99. static struct usb_pd_identity *get_pd_identity(struct device *dev)
  100. {
  101. if (is_typec_partner(dev)) {
  102. struct typec_partner *partner = to_typec_partner(dev);
  103. return partner->identity;
  104. } else if (is_typec_cable(dev)) {
  105. struct typec_cable *cable = to_typec_cable(dev);
  106. return cable->identity;
  107. }
  108. return NULL;
  109. }
  110. static const char *get_pd_product_type(struct device *dev)
  111. {
  112. struct typec_port *port = to_typec_port(dev->parent);
  113. struct usb_pd_identity *id = get_pd_identity(dev);
  114. const char *ptype = NULL;
  115. if (is_typec_partner(dev)) {
  116. if (!id)
  117. return NULL;
  118. if (port->data_role == TYPEC_HOST)
  119. ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)];
  120. else
  121. ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)];
  122. } else if (is_typec_cable(dev)) {
  123. if (id)
  124. ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)];
  125. else
  126. ptype = to_typec_cable(dev)->active ?
  127. product_type_cable[IDH_PTYPE_ACABLE] :
  128. product_type_cable[IDH_PTYPE_PCABLE];
  129. }
  130. return ptype;
  131. }
  132. static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
  133. char *buf)
  134. {
  135. struct usb_pd_identity *id = get_pd_identity(dev);
  136. return sprintf(buf, "0x%08x\n", id->id_header);
  137. }
  138. static DEVICE_ATTR_RO(id_header);
  139. static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
  140. char *buf)
  141. {
  142. struct usb_pd_identity *id = get_pd_identity(dev);
  143. return sprintf(buf, "0x%08x\n", id->cert_stat);
  144. }
  145. static DEVICE_ATTR_RO(cert_stat);
  146. static ssize_t product_show(struct device *dev, struct device_attribute *attr,
  147. char *buf)
  148. {
  149. struct usb_pd_identity *id = get_pd_identity(dev);
  150. return sprintf(buf, "0x%08x\n", id->product);
  151. }
  152. static DEVICE_ATTR_RO(product);
  153. static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr,
  154. char *buf)
  155. {
  156. struct usb_pd_identity *id = get_pd_identity(dev);
  157. return sysfs_emit(buf, "0x%08x\n", id->vdo[0]);
  158. }
  159. static DEVICE_ATTR_RO(product_type_vdo1);
  160. static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr,
  161. char *buf)
  162. {
  163. struct usb_pd_identity *id = get_pd_identity(dev);
  164. return sysfs_emit(buf, "0x%08x\n", id->vdo[1]);
  165. }
  166. static DEVICE_ATTR_RO(product_type_vdo2);
  167. static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr,
  168. char *buf)
  169. {
  170. struct usb_pd_identity *id = get_pd_identity(dev);
  171. return sysfs_emit(buf, "0x%08x\n", id->vdo[2]);
  172. }
  173. static DEVICE_ATTR_RO(product_type_vdo3);
  174. static struct attribute *usb_pd_id_attrs[] = {
  175. &dev_attr_id_header.attr,
  176. &dev_attr_cert_stat.attr,
  177. &dev_attr_product.attr,
  178. &dev_attr_product_type_vdo1.attr,
  179. &dev_attr_product_type_vdo2.attr,
  180. &dev_attr_product_type_vdo3.attr,
  181. NULL
  182. };
  183. static const struct attribute_group usb_pd_id_group = {
  184. .name = "identity",
  185. .attrs = usb_pd_id_attrs,
  186. };
  187. static const struct attribute_group *usb_pd_id_groups[] = {
  188. &usb_pd_id_group,
  189. NULL,
  190. };
  191. static void typec_product_type_notify(struct device *dev)
  192. {
  193. char *envp[2] = { };
  194. const char *ptype;
  195. ptype = get_pd_product_type(dev);
  196. if (!ptype)
  197. return;
  198. sysfs_notify(&dev->kobj, NULL, "type");
  199. envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype);
  200. if (!envp[0])
  201. return;
  202. kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp);
  203. kfree(envp[0]);
  204. }
  205. static void typec_report_identity(struct device *dev)
  206. {
  207. sysfs_notify(&dev->kobj, "identity", "id_header");
  208. sysfs_notify(&dev->kobj, "identity", "cert_stat");
  209. sysfs_notify(&dev->kobj, "identity", "product");
  210. sysfs_notify(&dev->kobj, "identity", "product_type_vdo1");
  211. sysfs_notify(&dev->kobj, "identity", "product_type_vdo2");
  212. sysfs_notify(&dev->kobj, "identity", "product_type_vdo3");
  213. typec_product_type_notify(dev);
  214. }
  215. static ssize_t
  216. type_show(struct device *dev, struct device_attribute *attr, char *buf)
  217. {
  218. const char *ptype;
  219. ptype = get_pd_product_type(dev);
  220. if (!ptype)
  221. return 0;
  222. return sysfs_emit(buf, "%s\n", ptype);
  223. }
  224. static DEVICE_ATTR_RO(type);
  225. static ssize_t usb_power_delivery_revision_show(struct device *dev,
  226. struct device_attribute *attr,
  227. char *buf);
  228. static DEVICE_ATTR_RO(usb_power_delivery_revision);
  229. /* ------------------------------------------------------------------------- */
  230. /* Alternate Modes */
  231. static int altmode_match(struct device *dev, void *data)
  232. {
  233. struct typec_altmode *adev = to_typec_altmode(dev);
  234. struct typec_device_id *id = data;
  235. if (!is_typec_altmode(dev))
  236. return 0;
  237. return ((adev->svid == id->svid) && (adev->mode == id->mode));
  238. }
  239. static void typec_altmode_set_partner(struct altmode *altmode)
  240. {
  241. struct typec_altmode *adev = &altmode->adev;
  242. struct typec_device_id id = { adev->svid, adev->mode, };
  243. struct typec_port *port = typec_altmode2port(adev);
  244. struct altmode *partner;
  245. struct device *dev;
  246. dev = device_find_child(&port->dev, &id, altmode_match);
  247. if (!dev)
  248. return;
  249. /* Bind the port alt mode to the partner/plug alt mode. */
  250. partner = to_altmode(to_typec_altmode(dev));
  251. altmode->partner = partner;
  252. /* Bind the partner/plug alt mode to the port alt mode. */
  253. if (is_typec_plug(adev->dev.parent)) {
  254. struct typec_plug *plug = to_typec_plug(adev->dev.parent);
  255. partner->plug[plug->index] = altmode;
  256. } else {
  257. partner->partner = altmode;
  258. }
  259. }
  260. static void typec_altmode_put_partner(struct altmode *altmode)
  261. {
  262. struct altmode *partner = altmode->partner;
  263. struct typec_altmode *adev;
  264. if (!partner)
  265. return;
  266. adev = &partner->adev;
  267. if (is_typec_plug(adev->dev.parent)) {
  268. struct typec_plug *plug = to_typec_plug(adev->dev.parent);
  269. partner->plug[plug->index] = NULL;
  270. } else {
  271. partner->partner = NULL;
  272. }
  273. put_device(&adev->dev);
  274. }
  275. /**
  276. * typec_altmode_update_active - Report Enter/Exit mode
  277. * @adev: Handle to the alternate mode
  278. * @active: True when the mode has been entered
  279. *
  280. * If a partner or cable plug executes Enter/Exit Mode command successfully, the
  281. * drivers use this routine to report the updated state of the mode.
  282. */
  283. void typec_altmode_update_active(struct typec_altmode *adev, bool active)
  284. {
  285. char dir[6];
  286. if (adev->active == active)
  287. return;
  288. if (!is_typec_port(adev->dev.parent) && adev->dev.driver) {
  289. if (!active)
  290. module_put(adev->dev.driver->owner);
  291. else
  292. WARN_ON(!try_module_get(adev->dev.driver->owner));
  293. }
  294. adev->active = active;
  295. snprintf(dir, sizeof(dir), "mode%d", adev->mode);
  296. sysfs_notify(&adev->dev.kobj, dir, "active");
  297. sysfs_notify(&adev->dev.kobj, NULL, "active");
  298. kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE);
  299. }
  300. EXPORT_SYMBOL_GPL(typec_altmode_update_active);
  301. /**
  302. * typec_altmode2port - Alternate Mode to USB Type-C port
  303. * @alt: The Alternate Mode
  304. *
  305. * Returns handle to the port that a cable plug or partner with @alt is
  306. * connected to.
  307. */
  308. struct typec_port *typec_altmode2port(struct typec_altmode *alt)
  309. {
  310. if (is_typec_plug(alt->dev.parent))
  311. return to_typec_port(alt->dev.parent->parent->parent);
  312. if (is_typec_partner(alt->dev.parent))
  313. return to_typec_port(alt->dev.parent->parent);
  314. if (is_typec_port(alt->dev.parent))
  315. return to_typec_port(alt->dev.parent);
  316. return NULL;
  317. }
  318. EXPORT_SYMBOL_GPL(typec_altmode2port);
  319. static ssize_t
  320. vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
  321. {
  322. struct typec_altmode *alt = to_typec_altmode(dev);
  323. return sprintf(buf, "0x%08x\n", alt->vdo);
  324. }
  325. static DEVICE_ATTR_RO(vdo);
  326. static ssize_t
  327. description_show(struct device *dev, struct device_attribute *attr, char *buf)
  328. {
  329. struct typec_altmode *alt = to_typec_altmode(dev);
  330. return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
  331. }
  332. static DEVICE_ATTR_RO(description);
  333. static ssize_t
  334. active_show(struct device *dev, struct device_attribute *attr, char *buf)
  335. {
  336. struct typec_altmode *alt = to_typec_altmode(dev);
  337. return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
  338. }
  339. static ssize_t active_store(struct device *dev, struct device_attribute *attr,
  340. const char *buf, size_t size)
  341. {
  342. struct typec_altmode *adev = to_typec_altmode(dev);
  343. struct altmode *altmode = to_altmode(adev);
  344. bool enter;
  345. int ret;
  346. ret = kstrtobool(buf, &enter);
  347. if (ret)
  348. return ret;
  349. if (adev->active == enter)
  350. return size;
  351. if (is_typec_port(adev->dev.parent)) {
  352. typec_altmode_update_active(adev, enter);
  353. /* Make sure that the partner exits the mode before disabling */
  354. if (altmode->partner && !enter && altmode->partner->adev.active)
  355. typec_altmode_exit(&altmode->partner->adev);
  356. } else if (altmode->partner) {
  357. if (enter && !altmode->partner->adev.active) {
  358. dev_warn(dev, "port has the mode disabled\n");
  359. return -EPERM;
  360. }
  361. }
  362. /* Note: If there is no driver, the mode will not be entered */
  363. if (adev->ops && adev->ops->activate) {
  364. ret = adev->ops->activate(adev, enter);
  365. if (ret)
  366. return ret;
  367. }
  368. return size;
  369. }
  370. static DEVICE_ATTR_RW(active);
  371. static ssize_t
  372. supported_roles_show(struct device *dev, struct device_attribute *attr,
  373. char *buf)
  374. {
  375. struct altmode *alt = to_altmode(to_typec_altmode(dev));
  376. ssize_t ret;
  377. switch (alt->roles) {
  378. case TYPEC_PORT_SRC:
  379. ret = sprintf(buf, "source\n");
  380. break;
  381. case TYPEC_PORT_SNK:
  382. ret = sprintf(buf, "sink\n");
  383. break;
  384. case TYPEC_PORT_DRP:
  385. default:
  386. ret = sprintf(buf, "source sink\n");
  387. break;
  388. }
  389. return ret;
  390. }
  391. static DEVICE_ATTR_RO(supported_roles);
  392. static ssize_t
  393. mode_show(struct device *dev, struct device_attribute *attr, char *buf)
  394. {
  395. struct typec_altmode *adev = to_typec_altmode(dev);
  396. return sprintf(buf, "%u\n", adev->mode);
  397. }
  398. static DEVICE_ATTR_RO(mode);
  399. static ssize_t
  400. svid_show(struct device *dev, struct device_attribute *attr, char *buf)
  401. {
  402. struct typec_altmode *adev = to_typec_altmode(dev);
  403. return sprintf(buf, "%04x\n", adev->svid);
  404. }
  405. static DEVICE_ATTR_RO(svid);
  406. static struct attribute *typec_altmode_attrs[] = {
  407. &dev_attr_active.attr,
  408. &dev_attr_mode.attr,
  409. &dev_attr_svid.attr,
  410. &dev_attr_vdo.attr,
  411. NULL
  412. };
  413. static umode_t typec_altmode_attr_is_visible(struct kobject *kobj,
  414. struct attribute *attr, int n)
  415. {
  416. struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj));
  417. if (attr == &dev_attr_active.attr)
  418. if (!adev->ops || !adev->ops->activate)
  419. return 0444;
  420. return attr->mode;
  421. }
  422. static const struct attribute_group typec_altmode_group = {
  423. .is_visible = typec_altmode_attr_is_visible,
  424. .attrs = typec_altmode_attrs,
  425. };
  426. static const struct attribute_group *typec_altmode_groups[] = {
  427. &typec_altmode_group,
  428. NULL
  429. };
  430. static int altmode_id_get(struct device *dev)
  431. {
  432. struct ida *ids;
  433. if (is_typec_partner(dev))
  434. ids = &to_typec_partner(dev)->mode_ids;
  435. else if (is_typec_plug(dev))
  436. ids = &to_typec_plug(dev)->mode_ids;
  437. else
  438. ids = &to_typec_port(dev)->mode_ids;
  439. return ida_simple_get(ids, 0, 0, GFP_KERNEL);
  440. }
  441. static void altmode_id_remove(struct device *dev, int id)
  442. {
  443. struct ida *ids;
  444. if (is_typec_partner(dev))
  445. ids = &to_typec_partner(dev)->mode_ids;
  446. else if (is_typec_plug(dev))
  447. ids = &to_typec_plug(dev)->mode_ids;
  448. else
  449. ids = &to_typec_port(dev)->mode_ids;
  450. ida_simple_remove(ids, id);
  451. }
  452. static void typec_altmode_release(struct device *dev)
  453. {
  454. struct altmode *alt = to_altmode(to_typec_altmode(dev));
  455. typec_altmode_put_partner(alt);
  456. altmode_id_remove(alt->adev.dev.parent, alt->id);
  457. kfree(alt);
  458. }
  459. const struct device_type typec_altmode_dev_type = {
  460. .name = "typec_alternate_mode",
  461. .groups = typec_altmode_groups,
  462. .release = typec_altmode_release,
  463. };
  464. static struct typec_altmode *
  465. typec_register_altmode(struct device *parent,
  466. const struct typec_altmode_desc *desc)
  467. {
  468. unsigned int id = altmode_id_get(parent);
  469. bool is_port = is_typec_port(parent);
  470. struct altmode *alt;
  471. int ret;
  472. alt = kzalloc(sizeof(*alt), GFP_KERNEL);
  473. if (!alt) {
  474. altmode_id_remove(parent, id);
  475. return ERR_PTR(-ENOMEM);
  476. }
  477. alt->adev.svid = desc->svid;
  478. alt->adev.mode = desc->mode;
  479. alt->adev.vdo = desc->vdo;
  480. alt->roles = desc->roles;
  481. alt->id = id;
  482. alt->attrs[0] = &dev_attr_vdo.attr;
  483. alt->attrs[1] = &dev_attr_description.attr;
  484. alt->attrs[2] = &dev_attr_active.attr;
  485. if (is_port) {
  486. alt->attrs[3] = &dev_attr_supported_roles.attr;
  487. alt->adev.active = true; /* Enabled by default */
  488. }
  489. sprintf(alt->group_name, "mode%d", desc->mode);
  490. alt->group.name = alt->group_name;
  491. alt->group.attrs = alt->attrs;
  492. alt->groups[0] = &alt->group;
  493. alt->adev.dev.parent = parent;
  494. alt->adev.dev.groups = alt->groups;
  495. alt->adev.dev.type = &typec_altmode_dev_type;
  496. dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id);
  497. /* Link partners and plugs with the ports */
  498. if (!is_port)
  499. typec_altmode_set_partner(alt);
  500. /* The partners are bind to drivers */
  501. if (is_typec_partner(parent))
  502. alt->adev.dev.bus = &typec_bus;
  503. /* Plug alt modes need a class to generate udev events. */
  504. if (is_typec_plug(parent))
  505. alt->adev.dev.class = typec_class;
  506. ret = device_register(&alt->adev.dev);
  507. if (ret) {
  508. dev_err(parent, "failed to register alternate mode (%d)\n",
  509. ret);
  510. put_device(&alt->adev.dev);
  511. return ERR_PTR(ret);
  512. }
  513. return &alt->adev;
  514. }
  515. /**
  516. * typec_unregister_altmode - Unregister Alternate Mode
  517. * @adev: The alternate mode to be unregistered
  518. *
  519. * Unregister device created with typec_partner_register_altmode(),
  520. * typec_plug_register_altmode() or typec_port_register_altmode().
  521. */
  522. void typec_unregister_altmode(struct typec_altmode *adev)
  523. {
  524. if (IS_ERR_OR_NULL(adev))
  525. return;
  526. typec_mux_put(to_altmode(adev)->mux);
  527. device_unregister(&adev->dev);
  528. }
  529. EXPORT_SYMBOL_GPL(typec_unregister_altmode);
  530. /* ------------------------------------------------------------------------- */
  531. /* Type-C Partners */
  532. static ssize_t accessory_mode_show(struct device *dev,
  533. struct device_attribute *attr,
  534. char *buf)
  535. {
  536. struct typec_partner *p = to_typec_partner(dev);
  537. return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
  538. }
  539. static DEVICE_ATTR_RO(accessory_mode);
  540. static ssize_t supports_usb_power_delivery_show(struct device *dev,
  541. struct device_attribute *attr,
  542. char *buf)
  543. {
  544. struct typec_partner *p = to_typec_partner(dev);
  545. return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
  546. }
  547. static DEVICE_ATTR_RO(supports_usb_power_delivery);
  548. static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr,
  549. char *buf)
  550. {
  551. struct typec_partner *partner;
  552. struct typec_plug *plug;
  553. int num_altmodes;
  554. if (is_typec_partner(dev)) {
  555. partner = to_typec_partner(dev);
  556. num_altmodes = partner->num_altmodes;
  557. } else if (is_typec_plug(dev)) {
  558. plug = to_typec_plug(dev);
  559. num_altmodes = plug->num_altmodes;
  560. } else {
  561. return 0;
  562. }
  563. return sysfs_emit(buf, "%d\n", num_altmodes);
  564. }
  565. static DEVICE_ATTR_RO(number_of_alternate_modes);
  566. static struct attribute *typec_partner_attrs[] = {
  567. &dev_attr_accessory_mode.attr,
  568. &dev_attr_supports_usb_power_delivery.attr,
  569. &dev_attr_number_of_alternate_modes.attr,
  570. &dev_attr_type.attr,
  571. &dev_attr_usb_power_delivery_revision.attr,
  572. NULL
  573. };
  574. static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  575. {
  576. struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj));
  577. if (attr == &dev_attr_number_of_alternate_modes.attr) {
  578. if (partner->num_altmodes < 0)
  579. return 0;
  580. }
  581. if (attr == &dev_attr_type.attr)
  582. if (!get_pd_product_type(kobj_to_dev(kobj)))
  583. return 0;
  584. return attr->mode;
  585. }
  586. static const struct attribute_group typec_partner_group = {
  587. .is_visible = typec_partner_attr_is_visible,
  588. .attrs = typec_partner_attrs
  589. };
  590. static const struct attribute_group *typec_partner_groups[] = {
  591. &typec_partner_group,
  592. NULL
  593. };
  594. static void typec_partner_release(struct device *dev)
  595. {
  596. struct typec_partner *partner = to_typec_partner(dev);
  597. ida_destroy(&partner->mode_ids);
  598. kfree(partner);
  599. }
  600. static const struct device_type typec_partner_dev_type = {
  601. .name = "typec_partner",
  602. .groups = typec_partner_groups,
  603. .release = typec_partner_release,
  604. };
  605. /**
  606. * typec_partner_set_identity - Report result from Discover Identity command
  607. * @partner: The partner updated identity values
  608. *
  609. * This routine is used to report that the result of Discover Identity USB power
  610. * delivery command has become available.
  611. */
  612. int typec_partner_set_identity(struct typec_partner *partner)
  613. {
  614. if (!partner->identity)
  615. return -EINVAL;
  616. typec_report_identity(&partner->dev);
  617. return 0;
  618. }
  619. EXPORT_SYMBOL_GPL(typec_partner_set_identity);
  620. /**
  621. * typec_partner_set_pd_revision - Set the PD revision supported by the partner
  622. * @partner: The partner to be updated.
  623. * @pd_revision: USB Power Delivery Specification Revision supported by partner
  624. *
  625. * This routine is used to report that the PD revision of the port partner has
  626. * become available.
  627. */
  628. void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision)
  629. {
  630. if (partner->pd_revision == pd_revision)
  631. return;
  632. partner->pd_revision = pd_revision;
  633. sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision");
  634. if (pd_revision != 0 && !partner->usb_pd) {
  635. partner->usb_pd = 1;
  636. sysfs_notify(&partner->dev.kobj, NULL,
  637. "supports_usb_power_delivery");
  638. }
  639. kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE);
  640. }
  641. EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision);
  642. /**
  643. * typec_partner_set_num_altmodes - Set the number of available partner altmodes
  644. * @partner: The partner to be updated.
  645. * @num_altmodes: The number of altmodes we want to specify as available.
  646. *
  647. * This routine is used to report the number of alternate modes supported by the
  648. * partner. This value is *not* enforced in alternate mode registration routines.
  649. *
  650. * @partner.num_altmodes is set to -1 on partner registration, denoting that
  651. * a valid value has not been set for it yet.
  652. *
  653. * Returns 0 on success or negative error number on failure.
  654. */
  655. int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes)
  656. {
  657. int ret;
  658. if (num_altmodes < 0)
  659. return -EINVAL;
  660. partner->num_altmodes = num_altmodes;
  661. ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group);
  662. if (ret < 0)
  663. return ret;
  664. sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes");
  665. return 0;
  666. }
  667. EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes);
  668. /**
  669. * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
  670. * @partner: USB Type-C Partner that supports the alternate mode
  671. * @desc: Description of the alternate mode
  672. *
  673. * This routine is used to register each alternate mode individually that
  674. * @partner has listed in response to Discover SVIDs command. The modes for a
  675. * SVID listed in response to Discover Modes command need to be listed in an
  676. * array in @desc.
  677. *
  678. * Returns handle to the alternate mode on success or ERR_PTR on failure.
  679. */
  680. struct typec_altmode *
  681. typec_partner_register_altmode(struct typec_partner *partner,
  682. const struct typec_altmode_desc *desc)
  683. {
  684. return typec_register_altmode(&partner->dev, desc);
  685. }
  686. EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
  687. /**
  688. * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version
  689. * @partner: USB Type-C Partner that supports SVDM
  690. * @svdm_version: Negotiated SVDM Version
  691. *
  692. * This routine is used to save the negotiated SVDM Version.
  693. */
  694. void typec_partner_set_svdm_version(struct typec_partner *partner,
  695. enum usb_pd_svdm_ver svdm_version)
  696. {
  697. partner->svdm_version = svdm_version;
  698. }
  699. EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version);
  700. /**
  701. * typec_register_partner - Register a USB Type-C Partner
  702. * @port: The USB Type-C Port the partner is connected to
  703. * @desc: Description of the partner
  704. *
  705. * Registers a device for USB Type-C Partner described in @desc.
  706. *
  707. * Returns handle to the partner on success or ERR_PTR on failure.
  708. */
  709. struct typec_partner *typec_register_partner(struct typec_port *port,
  710. struct typec_partner_desc *desc)
  711. {
  712. struct typec_partner *partner;
  713. int ret;
  714. partner = kzalloc(sizeof(*partner), GFP_KERNEL);
  715. if (!partner)
  716. return ERR_PTR(-ENOMEM);
  717. ida_init(&partner->mode_ids);
  718. partner->usb_pd = desc->usb_pd;
  719. partner->accessory = desc->accessory;
  720. partner->num_altmodes = -1;
  721. partner->pd_revision = desc->pd_revision;
  722. partner->svdm_version = port->cap->svdm_version;
  723. if (desc->identity) {
  724. /*
  725. * Creating directory for the identity only if the driver is
  726. * able to provide data to it.
  727. */
  728. partner->dev.groups = usb_pd_id_groups;
  729. partner->identity = desc->identity;
  730. }
  731. partner->dev.class = typec_class;
  732. partner->dev.parent = &port->dev;
  733. partner->dev.type = &typec_partner_dev_type;
  734. dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
  735. ret = device_register(&partner->dev);
  736. if (ret) {
  737. dev_err(&port->dev, "failed to register partner (%d)\n", ret);
  738. put_device(&partner->dev);
  739. return ERR_PTR(ret);
  740. }
  741. return partner;
  742. }
  743. EXPORT_SYMBOL_GPL(typec_register_partner);
  744. /**
  745. * typec_unregister_partner - Unregister a USB Type-C Partner
  746. * @partner: The partner to be unregistered
  747. *
  748. * Unregister device created with typec_register_partner().
  749. */
  750. void typec_unregister_partner(struct typec_partner *partner)
  751. {
  752. if (!IS_ERR_OR_NULL(partner))
  753. device_unregister(&partner->dev);
  754. }
  755. EXPORT_SYMBOL_GPL(typec_unregister_partner);
  756. /* ------------------------------------------------------------------------- */
  757. /* Type-C Cable Plugs */
  758. static void typec_plug_release(struct device *dev)
  759. {
  760. struct typec_plug *plug = to_typec_plug(dev);
  761. ida_destroy(&plug->mode_ids);
  762. kfree(plug);
  763. }
  764. static struct attribute *typec_plug_attrs[] = {
  765. &dev_attr_number_of_alternate_modes.attr,
  766. NULL
  767. };
  768. static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n)
  769. {
  770. struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj));
  771. if (attr == &dev_attr_number_of_alternate_modes.attr) {
  772. if (plug->num_altmodes < 0)
  773. return 0;
  774. }
  775. return attr->mode;
  776. }
  777. static const struct attribute_group typec_plug_group = {
  778. .is_visible = typec_plug_attr_is_visible,
  779. .attrs = typec_plug_attrs
  780. };
  781. static const struct attribute_group *typec_plug_groups[] = {
  782. &typec_plug_group,
  783. NULL
  784. };
  785. static const struct device_type typec_plug_dev_type = {
  786. .name = "typec_plug",
  787. .groups = typec_plug_groups,
  788. .release = typec_plug_release,
  789. };
  790. /**
  791. * typec_plug_set_num_altmodes - Set the number of available plug altmodes
  792. * @plug: The plug to be updated.
  793. * @num_altmodes: The number of altmodes we want to specify as available.
  794. *
  795. * This routine is used to report the number of alternate modes supported by the
  796. * plug. This value is *not* enforced in alternate mode registration routines.
  797. *
  798. * @plug.num_altmodes is set to -1 on plug registration, denoting that
  799. * a valid value has not been set for it yet.
  800. *
  801. * Returns 0 on success or negative error number on failure.
  802. */
  803. int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes)
  804. {
  805. int ret;
  806. if (num_altmodes < 0)
  807. return -EINVAL;
  808. plug->num_altmodes = num_altmodes;
  809. ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group);
  810. if (ret < 0)
  811. return ret;
  812. sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes");
  813. return 0;
  814. }
  815. EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes);
  816. /**
  817. * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
  818. * @plug: USB Type-C Cable Plug that supports the alternate mode
  819. * @desc: Description of the alternate mode
  820. *
  821. * This routine is used to register each alternate mode individually that @plug
  822. * has listed in response to Discover SVIDs command. The modes for a SVID that
  823. * the plug lists in response to Discover Modes command need to be listed in an
  824. * array in @desc.
  825. *
  826. * Returns handle to the alternate mode on success or ERR_PTR on failure.
  827. */
  828. struct typec_altmode *
  829. typec_plug_register_altmode(struct typec_plug *plug,
  830. const struct typec_altmode_desc *desc)
  831. {
  832. return typec_register_altmode(&plug->dev, desc);
  833. }
  834. EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
  835. /**
  836. * typec_register_plug - Register a USB Type-C Cable Plug
  837. * @cable: USB Type-C Cable with the plug
  838. * @desc: Description of the cable plug
  839. *
  840. * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
  841. * Cable Plug represents a plug with electronics in it that can response to USB
  842. * Power Delivery SOP Prime or SOP Double Prime packages.
  843. *
  844. * Returns handle to the cable plug on success or ERR_PTR on failure.
  845. */
  846. struct typec_plug *typec_register_plug(struct typec_cable *cable,
  847. struct typec_plug_desc *desc)
  848. {
  849. struct typec_plug *plug;
  850. char name[8];
  851. int ret;
  852. plug = kzalloc(sizeof(*plug), GFP_KERNEL);
  853. if (!plug)
  854. return ERR_PTR(-ENOMEM);
  855. sprintf(name, "plug%d", desc->index);
  856. ida_init(&plug->mode_ids);
  857. plug->num_altmodes = -1;
  858. plug->index = desc->index;
  859. plug->dev.class = typec_class;
  860. plug->dev.parent = &cable->dev;
  861. plug->dev.type = &typec_plug_dev_type;
  862. dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
  863. ret = device_register(&plug->dev);
  864. if (ret) {
  865. dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
  866. put_device(&plug->dev);
  867. return ERR_PTR(ret);
  868. }
  869. return plug;
  870. }
  871. EXPORT_SYMBOL_GPL(typec_register_plug);
  872. /**
  873. * typec_unregister_plug - Unregister a USB Type-C Cable Plug
  874. * @plug: The cable plug to be unregistered
  875. *
  876. * Unregister device created with typec_register_plug().
  877. */
  878. void typec_unregister_plug(struct typec_plug *plug)
  879. {
  880. if (!IS_ERR_OR_NULL(plug))
  881. device_unregister(&plug->dev);
  882. }
  883. EXPORT_SYMBOL_GPL(typec_unregister_plug);
  884. /* Type-C Cables */
  885. static const char * const typec_plug_types[] = {
  886. [USB_PLUG_NONE] = "unknown",
  887. [USB_PLUG_TYPE_A] = "type-a",
  888. [USB_PLUG_TYPE_B] = "type-b",
  889. [USB_PLUG_TYPE_C] = "type-c",
  890. [USB_PLUG_CAPTIVE] = "captive",
  891. };
  892. static ssize_t plug_type_show(struct device *dev,
  893. struct device_attribute *attr, char *buf)
  894. {
  895. struct typec_cable *cable = to_typec_cable(dev);
  896. return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
  897. }
  898. static DEVICE_ATTR_RO(plug_type);
  899. static struct attribute *typec_cable_attrs[] = {
  900. &dev_attr_type.attr,
  901. &dev_attr_plug_type.attr,
  902. &dev_attr_usb_power_delivery_revision.attr,
  903. NULL
  904. };
  905. ATTRIBUTE_GROUPS(typec_cable);
  906. static void typec_cable_release(struct device *dev)
  907. {
  908. struct typec_cable *cable = to_typec_cable(dev);
  909. kfree(cable);
  910. }
  911. static const struct device_type typec_cable_dev_type = {
  912. .name = "typec_cable",
  913. .groups = typec_cable_groups,
  914. .release = typec_cable_release,
  915. };
  916. static int cable_match(struct device *dev, void *data)
  917. {
  918. return is_typec_cable(dev);
  919. }
  920. /**
  921. * typec_cable_get - Get a reference to the USB Type-C cable
  922. * @port: The USB Type-C Port the cable is connected to
  923. *
  924. * The caller must decrement the reference count with typec_cable_put() after
  925. * use.
  926. */
  927. struct typec_cable *typec_cable_get(struct typec_port *port)
  928. {
  929. struct device *dev;
  930. dev = device_find_child(&port->dev, NULL, cable_match);
  931. if (!dev)
  932. return NULL;
  933. return to_typec_cable(dev);
  934. }
  935. EXPORT_SYMBOL_GPL(typec_cable_get);
  936. /**
  937. * typec_cable_put - Decrement the reference count on USB Type-C cable
  938. * @cable: The USB Type-C cable
  939. */
  940. void typec_cable_put(struct typec_cable *cable)
  941. {
  942. put_device(&cable->dev);
  943. }
  944. EXPORT_SYMBOL_GPL(typec_cable_put);
  945. /**
  946. * typec_cable_is_active - Check is the USB Type-C cable active or passive
  947. * @cable: The USB Type-C Cable
  948. *
  949. * Return 1 if the cable is active or 0 if it's passive.
  950. */
  951. int typec_cable_is_active(struct typec_cable *cable)
  952. {
  953. return cable->active;
  954. }
  955. EXPORT_SYMBOL_GPL(typec_cable_is_active);
  956. /**
  957. * typec_cable_set_identity - Report result from Discover Identity command
  958. * @cable: The cable updated identity values
  959. *
  960. * This routine is used to report that the result of Discover Identity USB power
  961. * delivery command has become available.
  962. */
  963. int typec_cable_set_identity(struct typec_cable *cable)
  964. {
  965. if (!cable->identity)
  966. return -EINVAL;
  967. typec_report_identity(&cable->dev);
  968. return 0;
  969. }
  970. EXPORT_SYMBOL_GPL(typec_cable_set_identity);
  971. /**
  972. * typec_register_cable - Register a USB Type-C Cable
  973. * @port: The USB Type-C Port the cable is connected to
  974. * @desc: Description of the cable
  975. *
  976. * Registers a device for USB Type-C Cable described in @desc. The cable will be
  977. * parent for the optional cable plug devises.
  978. *
  979. * Returns handle to the cable on success or ERR_PTR on failure.
  980. */
  981. struct typec_cable *typec_register_cable(struct typec_port *port,
  982. struct typec_cable_desc *desc)
  983. {
  984. struct typec_cable *cable;
  985. int ret;
  986. cable = kzalloc(sizeof(*cable), GFP_KERNEL);
  987. if (!cable)
  988. return ERR_PTR(-ENOMEM);
  989. cable->type = desc->type;
  990. cable->active = desc->active;
  991. cable->pd_revision = desc->pd_revision;
  992. if (desc->identity) {
  993. /*
  994. * Creating directory for the identity only if the driver is
  995. * able to provide data to it.
  996. */
  997. cable->dev.groups = usb_pd_id_groups;
  998. cable->identity = desc->identity;
  999. }
  1000. cable->dev.class = typec_class;
  1001. cable->dev.parent = &port->dev;
  1002. cable->dev.type = &typec_cable_dev_type;
  1003. dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
  1004. ret = device_register(&cable->dev);
  1005. if (ret) {
  1006. dev_err(&port->dev, "failed to register cable (%d)\n", ret);
  1007. put_device(&cable->dev);
  1008. return ERR_PTR(ret);
  1009. }
  1010. return cable;
  1011. }
  1012. EXPORT_SYMBOL_GPL(typec_register_cable);
  1013. /**
  1014. * typec_unregister_cable - Unregister a USB Type-C Cable
  1015. * @cable: The cable to be unregistered
  1016. *
  1017. * Unregister device created with typec_register_cable().
  1018. */
  1019. void typec_unregister_cable(struct typec_cable *cable)
  1020. {
  1021. if (!IS_ERR_OR_NULL(cable))
  1022. device_unregister(&cable->dev);
  1023. }
  1024. EXPORT_SYMBOL_GPL(typec_unregister_cable);
  1025. /* ------------------------------------------------------------------------- */
  1026. /* USB Type-C ports */
  1027. static const char * const typec_orientations[] = {
  1028. [TYPEC_ORIENTATION_NONE] = "unknown",
  1029. [TYPEC_ORIENTATION_NORMAL] = "normal",
  1030. [TYPEC_ORIENTATION_REVERSE] = "reverse",
  1031. };
  1032. static const char * const typec_roles[] = {
  1033. [TYPEC_SINK] = "sink",
  1034. [TYPEC_SOURCE] = "source",
  1035. };
  1036. static const char * const typec_data_roles[] = {
  1037. [TYPEC_DEVICE] = "device",
  1038. [TYPEC_HOST] = "host",
  1039. };
  1040. static const char * const typec_port_power_roles[] = {
  1041. [TYPEC_PORT_SRC] = "source",
  1042. [TYPEC_PORT_SNK] = "sink",
  1043. [TYPEC_PORT_DRP] = "dual",
  1044. };
  1045. static const char * const typec_port_data_roles[] = {
  1046. [TYPEC_PORT_DFP] = "host",
  1047. [TYPEC_PORT_UFP] = "device",
  1048. [TYPEC_PORT_DRD] = "dual",
  1049. };
  1050. static const char * const typec_port_types_drp[] = {
  1051. [TYPEC_PORT_SRC] = "dual [source] sink",
  1052. [TYPEC_PORT_SNK] = "dual source [sink]",
  1053. [TYPEC_PORT_DRP] = "[dual] source sink",
  1054. };
  1055. static ssize_t
  1056. preferred_role_store(struct device *dev, struct device_attribute *attr,
  1057. const char *buf, size_t size)
  1058. {
  1059. struct typec_port *port = to_typec_port(dev);
  1060. int role;
  1061. int ret;
  1062. if (port->cap->type != TYPEC_PORT_DRP) {
  1063. dev_dbg(dev, "Preferred role only supported with DRP ports\n");
  1064. return -EOPNOTSUPP;
  1065. }
  1066. if (!port->ops || !port->ops->try_role) {
  1067. dev_dbg(dev, "Setting preferred role not supported\n");
  1068. return -EOPNOTSUPP;
  1069. }
  1070. role = sysfs_match_string(typec_roles, buf);
  1071. if (role < 0) {
  1072. if (sysfs_streq(buf, "none"))
  1073. role = TYPEC_NO_PREFERRED_ROLE;
  1074. else
  1075. return -EINVAL;
  1076. }
  1077. ret = port->ops->try_role(port, role);
  1078. if (ret)
  1079. return ret;
  1080. port->prefer_role = role;
  1081. return size;
  1082. }
  1083. static ssize_t
  1084. preferred_role_show(struct device *dev, struct device_attribute *attr,
  1085. char *buf)
  1086. {
  1087. struct typec_port *port = to_typec_port(dev);
  1088. if (port->cap->type != TYPEC_PORT_DRP)
  1089. return 0;
  1090. if (port->prefer_role < 0)
  1091. return 0;
  1092. return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
  1093. }
  1094. static DEVICE_ATTR_RW(preferred_role);
  1095. static ssize_t data_role_store(struct device *dev,
  1096. struct device_attribute *attr,
  1097. const char *buf, size_t size)
  1098. {
  1099. struct typec_port *port = to_typec_port(dev);
  1100. int ret;
  1101. if (!port->ops || !port->ops->dr_set) {
  1102. dev_dbg(dev, "data role swapping not supported\n");
  1103. return -EOPNOTSUPP;
  1104. }
  1105. ret = sysfs_match_string(typec_data_roles, buf);
  1106. if (ret < 0)
  1107. return ret;
  1108. mutex_lock(&port->port_type_lock);
  1109. if (port->cap->data != TYPEC_PORT_DRD) {
  1110. ret = -EOPNOTSUPP;
  1111. goto unlock_and_ret;
  1112. }
  1113. ret = port->ops->dr_set(port, ret);
  1114. if (ret)
  1115. goto unlock_and_ret;
  1116. ret = size;
  1117. unlock_and_ret:
  1118. mutex_unlock(&port->port_type_lock);
  1119. return ret;
  1120. }
  1121. static ssize_t data_role_show(struct device *dev,
  1122. struct device_attribute *attr, char *buf)
  1123. {
  1124. struct typec_port *port = to_typec_port(dev);
  1125. if (port->cap->data == TYPEC_PORT_DRD)
  1126. return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
  1127. "[host] device" : "host [device]");
  1128. return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
  1129. }
  1130. static DEVICE_ATTR_RW(data_role);
  1131. static ssize_t power_role_store(struct device *dev,
  1132. struct device_attribute *attr,
  1133. const char *buf, size_t size)
  1134. {
  1135. struct typec_port *port = to_typec_port(dev);
  1136. int ret;
  1137. if (!port->ops || !port->ops->pr_set) {
  1138. dev_dbg(dev, "power role swapping not supported\n");
  1139. return -EOPNOTSUPP;
  1140. }
  1141. if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
  1142. dev_dbg(dev, "partner unable to swap power role\n");
  1143. return -EIO;
  1144. }
  1145. ret = sysfs_match_string(typec_roles, buf);
  1146. if (ret < 0)
  1147. return ret;
  1148. mutex_lock(&port->port_type_lock);
  1149. if (port->port_type != TYPEC_PORT_DRP) {
  1150. dev_dbg(dev, "port type fixed at \"%s\"",
  1151. typec_port_power_roles[port->port_type]);
  1152. ret = -EOPNOTSUPP;
  1153. goto unlock_and_ret;
  1154. }
  1155. ret = port->ops->pr_set(port, ret);
  1156. if (ret)
  1157. goto unlock_and_ret;
  1158. ret = size;
  1159. unlock_and_ret:
  1160. mutex_unlock(&port->port_type_lock);
  1161. return ret;
  1162. }
  1163. static ssize_t power_role_show(struct device *dev,
  1164. struct device_attribute *attr, char *buf)
  1165. {
  1166. struct typec_port *port = to_typec_port(dev);
  1167. if (port->cap->type == TYPEC_PORT_DRP)
  1168. return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
  1169. "[source] sink" : "source [sink]");
  1170. return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
  1171. }
  1172. static DEVICE_ATTR_RW(power_role);
  1173. static ssize_t
  1174. port_type_store(struct device *dev, struct device_attribute *attr,
  1175. const char *buf, size_t size)
  1176. {
  1177. struct typec_port *port = to_typec_port(dev);
  1178. int ret;
  1179. enum typec_port_type type;
  1180. if (port->cap->type != TYPEC_PORT_DRP ||
  1181. !port->ops || !port->ops->port_type_set) {
  1182. dev_dbg(dev, "changing port type not supported\n");
  1183. return -EOPNOTSUPP;
  1184. }
  1185. ret = sysfs_match_string(typec_port_power_roles, buf);
  1186. if (ret < 0)
  1187. return ret;
  1188. type = ret;
  1189. mutex_lock(&port->port_type_lock);
  1190. if (port->port_type == type) {
  1191. ret = size;
  1192. goto unlock_and_ret;
  1193. }
  1194. ret = port->ops->port_type_set(port, type);
  1195. if (ret)
  1196. goto unlock_and_ret;
  1197. port->port_type = type;
  1198. ret = size;
  1199. unlock_and_ret:
  1200. mutex_unlock(&port->port_type_lock);
  1201. return ret;
  1202. }
  1203. static ssize_t
  1204. port_type_show(struct device *dev, struct device_attribute *attr,
  1205. char *buf)
  1206. {
  1207. struct typec_port *port = to_typec_port(dev);
  1208. if (port->cap->type == TYPEC_PORT_DRP)
  1209. return sprintf(buf, "%s\n",
  1210. typec_port_types_drp[port->port_type]);
  1211. return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
  1212. }
  1213. static DEVICE_ATTR_RW(port_type);
  1214. static const char * const typec_pwr_opmodes[] = {
  1215. [TYPEC_PWR_MODE_USB] = "default",
  1216. [TYPEC_PWR_MODE_1_5A] = "1.5A",
  1217. [TYPEC_PWR_MODE_3_0A] = "3.0A",
  1218. [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
  1219. };
  1220. static ssize_t power_operation_mode_show(struct device *dev,
  1221. struct device_attribute *attr,
  1222. char *buf)
  1223. {
  1224. struct typec_port *port = to_typec_port(dev);
  1225. return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
  1226. }
  1227. static DEVICE_ATTR_RO(power_operation_mode);
  1228. static ssize_t vconn_source_store(struct device *dev,
  1229. struct device_attribute *attr,
  1230. const char *buf, size_t size)
  1231. {
  1232. struct typec_port *port = to_typec_port(dev);
  1233. bool source;
  1234. int ret;
  1235. if (!port->cap->pd_revision) {
  1236. dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
  1237. return -EOPNOTSUPP;
  1238. }
  1239. if (!port->ops || !port->ops->vconn_set) {
  1240. dev_dbg(dev, "VCONN swapping not supported\n");
  1241. return -EOPNOTSUPP;
  1242. }
  1243. ret = kstrtobool(buf, &source);
  1244. if (ret)
  1245. return ret;
  1246. ret = port->ops->vconn_set(port, (enum typec_role)source);
  1247. if (ret)
  1248. return ret;
  1249. return size;
  1250. }
  1251. static ssize_t vconn_source_show(struct device *dev,
  1252. struct device_attribute *attr, char *buf)
  1253. {
  1254. struct typec_port *port = to_typec_port(dev);
  1255. return sprintf(buf, "%s\n",
  1256. port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
  1257. }
  1258. static DEVICE_ATTR_RW(vconn_source);
  1259. static ssize_t supported_accessory_modes_show(struct device *dev,
  1260. struct device_attribute *attr,
  1261. char *buf)
  1262. {
  1263. struct typec_port *port = to_typec_port(dev);
  1264. ssize_t ret = 0;
  1265. int i;
  1266. for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
  1267. if (port->cap->accessory[i])
  1268. ret += sprintf(buf + ret, "%s ",
  1269. typec_accessory_modes[port->cap->accessory[i]]);
  1270. }
  1271. if (!ret)
  1272. return sprintf(buf, "none\n");
  1273. buf[ret - 1] = '\n';
  1274. return ret;
  1275. }
  1276. static DEVICE_ATTR_RO(supported_accessory_modes);
  1277. static ssize_t usb_typec_revision_show(struct device *dev,
  1278. struct device_attribute *attr,
  1279. char *buf)
  1280. {
  1281. struct typec_port *port = to_typec_port(dev);
  1282. u16 rev = port->cap->revision;
  1283. return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
  1284. }
  1285. static DEVICE_ATTR_RO(usb_typec_revision);
  1286. static ssize_t usb_power_delivery_revision_show(struct device *dev,
  1287. struct device_attribute *attr,
  1288. char *buf)
  1289. {
  1290. u16 rev = 0;
  1291. if (is_typec_partner(dev)) {
  1292. struct typec_partner *partner = to_typec_partner(dev);
  1293. rev = partner->pd_revision;
  1294. } else if (is_typec_cable(dev)) {
  1295. struct typec_cable *cable = to_typec_cable(dev);
  1296. rev = cable->pd_revision;
  1297. } else if (is_typec_port(dev)) {
  1298. struct typec_port *p = to_typec_port(dev);
  1299. rev = p->cap->pd_revision;
  1300. }
  1301. return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
  1302. }
  1303. static ssize_t orientation_show(struct device *dev,
  1304. struct device_attribute *attr,
  1305. char *buf)
  1306. {
  1307. struct typec_port *port = to_typec_port(dev);
  1308. return sprintf(buf, "%s\n", typec_orientations[port->orientation]);
  1309. }
  1310. static DEVICE_ATTR_RO(orientation);
  1311. static struct attribute *typec_attrs[] = {
  1312. &dev_attr_data_role.attr,
  1313. &dev_attr_power_operation_mode.attr,
  1314. &dev_attr_power_role.attr,
  1315. &dev_attr_preferred_role.attr,
  1316. &dev_attr_supported_accessory_modes.attr,
  1317. &dev_attr_usb_power_delivery_revision.attr,
  1318. &dev_attr_usb_typec_revision.attr,
  1319. &dev_attr_vconn_source.attr,
  1320. &dev_attr_port_type.attr,
  1321. &dev_attr_orientation.attr,
  1322. NULL,
  1323. };
  1324. static umode_t typec_attr_is_visible(struct kobject *kobj,
  1325. struct attribute *attr, int n)
  1326. {
  1327. struct typec_port *port = to_typec_port(kobj_to_dev(kobj));
  1328. if (attr == &dev_attr_data_role.attr) {
  1329. if (port->cap->data != TYPEC_PORT_DRD ||
  1330. !port->ops || !port->ops->dr_set)
  1331. return 0444;
  1332. } else if (attr == &dev_attr_power_role.attr) {
  1333. if (port->cap->type != TYPEC_PORT_DRP ||
  1334. !port->ops || !port->ops->pr_set)
  1335. return 0444;
  1336. } else if (attr == &dev_attr_vconn_source.attr) {
  1337. if (!port->cap->pd_revision ||
  1338. !port->ops || !port->ops->vconn_set)
  1339. return 0444;
  1340. } else if (attr == &dev_attr_preferred_role.attr) {
  1341. if (port->cap->type != TYPEC_PORT_DRP ||
  1342. !port->ops || !port->ops->try_role)
  1343. return 0444;
  1344. } else if (attr == &dev_attr_port_type.attr) {
  1345. if (!port->ops || !port->ops->port_type_set)
  1346. return 0;
  1347. if (port->cap->type != TYPEC_PORT_DRP)
  1348. return 0444;
  1349. } else if (attr == &dev_attr_orientation.attr) {
  1350. if (port->cap->orientation_aware)
  1351. return 0444;
  1352. return 0;
  1353. }
  1354. return attr->mode;
  1355. }
  1356. static const struct attribute_group typec_group = {
  1357. .is_visible = typec_attr_is_visible,
  1358. .attrs = typec_attrs,
  1359. };
  1360. static const struct attribute_group *typec_groups[] = {
  1361. &typec_group,
  1362. NULL
  1363. };
  1364. static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
  1365. {
  1366. int ret;
  1367. ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
  1368. if (ret)
  1369. dev_err(dev, "failed to add uevent TYPEC_PORT\n");
  1370. return ret;
  1371. }
  1372. static void typec_release(struct device *dev)
  1373. {
  1374. struct typec_port *port = to_typec_port(dev);
  1375. ida_simple_remove(&typec_index_ida, port->id);
  1376. ida_destroy(&port->mode_ids);
  1377. typec_switch_put(port->sw);
  1378. typec_mux_put(port->mux);
  1379. kfree(port->cap);
  1380. kfree(port);
  1381. }
  1382. const struct device_type typec_port_dev_type = {
  1383. .name = "typec_port",
  1384. .groups = typec_groups,
  1385. .uevent = typec_uevent,
  1386. .release = typec_release,
  1387. };
  1388. /* --------------------------------------- */
  1389. /* Driver callbacks to report role updates */
  1390. static int partner_match(struct device *dev, void *data)
  1391. {
  1392. return is_typec_partner(dev);
  1393. }
  1394. /**
  1395. * typec_set_data_role - Report data role change
  1396. * @port: The USB Type-C Port where the role was changed
  1397. * @role: The new data role
  1398. *
  1399. * This routine is used by the port drivers to report data role changes.
  1400. */
  1401. void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
  1402. {
  1403. struct device *partner_dev;
  1404. if (port->data_role == role)
  1405. return;
  1406. port->data_role = role;
  1407. sysfs_notify(&port->dev.kobj, NULL, "data_role");
  1408. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1409. partner_dev = device_find_child(&port->dev, NULL, partner_match);
  1410. if (!partner_dev)
  1411. return;
  1412. if (to_typec_partner(partner_dev)->identity)
  1413. typec_product_type_notify(partner_dev);
  1414. put_device(partner_dev);
  1415. }
  1416. EXPORT_SYMBOL_GPL(typec_set_data_role);
  1417. /**
  1418. * typec_set_pwr_role - Report power role change
  1419. * @port: The USB Type-C Port where the role was changed
  1420. * @role: The new data role
  1421. *
  1422. * This routine is used by the port drivers to report power role changes.
  1423. */
  1424. void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
  1425. {
  1426. if (port->pwr_role == role)
  1427. return;
  1428. port->pwr_role = role;
  1429. sysfs_notify(&port->dev.kobj, NULL, "power_role");
  1430. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1431. }
  1432. EXPORT_SYMBOL_GPL(typec_set_pwr_role);
  1433. /**
  1434. * typec_set_vconn_role - Report VCONN source change
  1435. * @port: The USB Type-C Port which VCONN role changed
  1436. * @role: Source when @port is sourcing VCONN, or Sink when it's not
  1437. *
  1438. * This routine is used by the port drivers to report if the VCONN source is
  1439. * changes.
  1440. */
  1441. void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
  1442. {
  1443. if (port->vconn_role == role)
  1444. return;
  1445. port->vconn_role = role;
  1446. sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
  1447. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1448. }
  1449. EXPORT_SYMBOL_GPL(typec_set_vconn_role);
  1450. /**
  1451. * typec_set_pwr_opmode - Report changed power operation mode
  1452. * @port: The USB Type-C Port where the mode was changed
  1453. * @opmode: New power operation mode
  1454. *
  1455. * This routine is used by the port drivers to report changed power operation
  1456. * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
  1457. * Type-C specification, and "USB Power Delivery" when the power levels are
  1458. * negotiated with methods defined in USB Power Delivery specification.
  1459. */
  1460. void typec_set_pwr_opmode(struct typec_port *port,
  1461. enum typec_pwr_opmode opmode)
  1462. {
  1463. struct device *partner_dev;
  1464. if (port->pwr_opmode == opmode)
  1465. return;
  1466. port->pwr_opmode = opmode;
  1467. sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
  1468. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1469. partner_dev = device_find_child(&port->dev, NULL, partner_match);
  1470. if (partner_dev) {
  1471. struct typec_partner *partner = to_typec_partner(partner_dev);
  1472. if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
  1473. partner->usb_pd = 1;
  1474. sysfs_notify(&partner_dev->kobj, NULL,
  1475. "supports_usb_power_delivery");
  1476. }
  1477. put_device(partner_dev);
  1478. }
  1479. }
  1480. EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
  1481. /**
  1482. * typec_find_pwr_opmode - Get the typec power operation mode capability
  1483. * @name: power operation mode string
  1484. *
  1485. * This routine is used to find the typec_pwr_opmode by its string @name.
  1486. *
  1487. * Returns typec_pwr_opmode if success, otherwise negative error code.
  1488. */
  1489. int typec_find_pwr_opmode(const char *name)
  1490. {
  1491. return match_string(typec_pwr_opmodes,
  1492. ARRAY_SIZE(typec_pwr_opmodes), name);
  1493. }
  1494. EXPORT_SYMBOL_GPL(typec_find_pwr_opmode);
  1495. /**
  1496. * typec_find_orientation - Convert orientation string to enum typec_orientation
  1497. * @name: Orientation string
  1498. *
  1499. * This routine is used to find the typec_orientation by its string name @name.
  1500. *
  1501. * Returns the orientation value on success, otherwise negative error code.
  1502. */
  1503. int typec_find_orientation(const char *name)
  1504. {
  1505. return match_string(typec_orientations, ARRAY_SIZE(typec_orientations),
  1506. name);
  1507. }
  1508. EXPORT_SYMBOL_GPL(typec_find_orientation);
  1509. /**
  1510. * typec_find_port_power_role - Get the typec port power capability
  1511. * @name: port power capability string
  1512. *
  1513. * This routine is used to find the typec_port_type by its string name.
  1514. *
  1515. * Returns typec_port_type if success, otherwise negative error code.
  1516. */
  1517. int typec_find_port_power_role(const char *name)
  1518. {
  1519. return match_string(typec_port_power_roles,
  1520. ARRAY_SIZE(typec_port_power_roles), name);
  1521. }
  1522. EXPORT_SYMBOL_GPL(typec_find_port_power_role);
  1523. /**
  1524. * typec_find_power_role - Find the typec one specific power role
  1525. * @name: power role string
  1526. *
  1527. * This routine is used to find the typec_role by its string name.
  1528. *
  1529. * Returns typec_role if success, otherwise negative error code.
  1530. */
  1531. int typec_find_power_role(const char *name)
  1532. {
  1533. return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
  1534. }
  1535. EXPORT_SYMBOL_GPL(typec_find_power_role);
  1536. /**
  1537. * typec_find_port_data_role - Get the typec port data capability
  1538. * @name: port data capability string
  1539. *
  1540. * This routine is used to find the typec_port_data by its string name.
  1541. *
  1542. * Returns typec_port_data if success, otherwise negative error code.
  1543. */
  1544. int typec_find_port_data_role(const char *name)
  1545. {
  1546. return match_string(typec_port_data_roles,
  1547. ARRAY_SIZE(typec_port_data_roles), name);
  1548. }
  1549. EXPORT_SYMBOL_GPL(typec_find_port_data_role);
  1550. /* ------------------------------------------ */
  1551. /* API for Multiplexer/DeMultiplexer Switches */
  1552. /**
  1553. * typec_set_orientation - Set USB Type-C cable plug orientation
  1554. * @port: USB Type-C Port
  1555. * @orientation: USB Type-C cable plug orientation
  1556. *
  1557. * Set cable plug orientation for @port.
  1558. */
  1559. int typec_set_orientation(struct typec_port *port,
  1560. enum typec_orientation orientation)
  1561. {
  1562. int ret;
  1563. ret = typec_switch_set(port->sw, orientation);
  1564. if (ret)
  1565. return ret;
  1566. port->orientation = orientation;
  1567. sysfs_notify(&port->dev.kobj, NULL, "orientation");
  1568. kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
  1569. return 0;
  1570. }
  1571. EXPORT_SYMBOL_GPL(typec_set_orientation);
  1572. /**
  1573. * typec_get_orientation - Get USB Type-C cable plug orientation
  1574. * @port: USB Type-C Port
  1575. *
  1576. * Get current cable plug orientation for @port.
  1577. */
  1578. enum typec_orientation typec_get_orientation(struct typec_port *port)
  1579. {
  1580. return port->orientation;
  1581. }
  1582. EXPORT_SYMBOL_GPL(typec_get_orientation);
  1583. /**
  1584. * typec_set_mode - Set mode of operation for USB Type-C connector
  1585. * @port: USB Type-C connector
  1586. * @mode: Accessory Mode, USB Operation or Safe State
  1587. *
  1588. * Configure @port for Accessory Mode @mode. This function will configure the
  1589. * muxes needed for @mode.
  1590. */
  1591. int typec_set_mode(struct typec_port *port, int mode)
  1592. {
  1593. struct typec_mux_state state = { };
  1594. state.mode = mode;
  1595. return typec_mux_set(port->mux, &state);
  1596. }
  1597. EXPORT_SYMBOL_GPL(typec_set_mode);
  1598. /* --------------------------------------- */
  1599. /**
  1600. * typec_get_negotiated_svdm_version - Get negotiated SVDM Version
  1601. * @port: USB Type-C Port.
  1602. *
  1603. * Get the negotiated SVDM Version. The Version is set to the port default
  1604. * value stored in typec_capability on partner registration, and updated after
  1605. * a successful Discover Identity if the negotiated value is less than the
  1606. * default value.
  1607. *
  1608. * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV.
  1609. */
  1610. int typec_get_negotiated_svdm_version(struct typec_port *port)
  1611. {
  1612. enum usb_pd_svdm_ver svdm_version;
  1613. struct device *partner_dev;
  1614. partner_dev = device_find_child(&port->dev, NULL, partner_match);
  1615. if (!partner_dev)
  1616. return -ENODEV;
  1617. svdm_version = to_typec_partner(partner_dev)->svdm_version;
  1618. put_device(partner_dev);
  1619. return svdm_version;
  1620. }
  1621. EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version);
  1622. /**
  1623. * typec_get_drvdata - Return private driver data pointer
  1624. * @port: USB Type-C port
  1625. */
  1626. void *typec_get_drvdata(struct typec_port *port)
  1627. {
  1628. return dev_get_drvdata(&port->dev);
  1629. }
  1630. EXPORT_SYMBOL_GPL(typec_get_drvdata);
  1631. /**
  1632. * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
  1633. * @port: USB Type-C Port that supports the alternate mode
  1634. * @desc: Description of the alternate mode
  1635. *
  1636. * This routine is used to register an alternate mode that @port is capable of
  1637. * supporting.
  1638. *
  1639. * Returns handle to the alternate mode on success or ERR_PTR on failure.
  1640. */
  1641. struct typec_altmode *
  1642. typec_port_register_altmode(struct typec_port *port,
  1643. const struct typec_altmode_desc *desc)
  1644. {
  1645. struct typec_altmode *adev;
  1646. struct typec_mux *mux;
  1647. mux = typec_mux_get(&port->dev, desc);
  1648. if (IS_ERR(mux))
  1649. return ERR_CAST(mux);
  1650. adev = typec_register_altmode(&port->dev, desc);
  1651. if (IS_ERR(adev))
  1652. typec_mux_put(mux);
  1653. else
  1654. to_altmode(adev)->mux = mux;
  1655. return adev;
  1656. }
  1657. EXPORT_SYMBOL_GPL(typec_port_register_altmode);
  1658. /**
  1659. * typec_register_port - Register a USB Type-C Port
  1660. * @parent: Parent device
  1661. * @cap: Description of the port
  1662. *
  1663. * Registers a device for USB Type-C Port described in @cap.
  1664. *
  1665. * Returns handle to the port on success or ERR_PTR on failure.
  1666. */
  1667. struct typec_port *typec_register_port(struct device *parent,
  1668. const struct typec_capability *cap)
  1669. {
  1670. struct typec_port *port;
  1671. int ret;
  1672. int id;
  1673. port = kzalloc(sizeof(*port), GFP_KERNEL);
  1674. if (!port)
  1675. return ERR_PTR(-ENOMEM);
  1676. id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
  1677. if (id < 0) {
  1678. kfree(port);
  1679. return ERR_PTR(id);
  1680. }
  1681. switch (cap->type) {
  1682. case TYPEC_PORT_SRC:
  1683. port->pwr_role = TYPEC_SOURCE;
  1684. port->vconn_role = TYPEC_SOURCE;
  1685. break;
  1686. case TYPEC_PORT_SNK:
  1687. port->pwr_role = TYPEC_SINK;
  1688. port->vconn_role = TYPEC_SINK;
  1689. break;
  1690. case TYPEC_PORT_DRP:
  1691. if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
  1692. port->pwr_role = cap->prefer_role;
  1693. else
  1694. port->pwr_role = TYPEC_SINK;
  1695. break;
  1696. }
  1697. switch (cap->data) {
  1698. case TYPEC_PORT_DFP:
  1699. port->data_role = TYPEC_HOST;
  1700. break;
  1701. case TYPEC_PORT_UFP:
  1702. port->data_role = TYPEC_DEVICE;
  1703. break;
  1704. case TYPEC_PORT_DRD:
  1705. if (cap->prefer_role == TYPEC_SOURCE)
  1706. port->data_role = TYPEC_HOST;
  1707. else
  1708. port->data_role = TYPEC_DEVICE;
  1709. break;
  1710. }
  1711. ida_init(&port->mode_ids);
  1712. mutex_init(&port->port_type_lock);
  1713. port->id = id;
  1714. port->ops = cap->ops;
  1715. port->port_type = cap->type;
  1716. port->prefer_role = cap->prefer_role;
  1717. device_initialize(&port->dev);
  1718. port->dev.class = typec_class;
  1719. port->dev.parent = parent;
  1720. port->dev.fwnode = cap->fwnode;
  1721. port->dev.type = &typec_port_dev_type;
  1722. dev_set_name(&port->dev, "port%d", id);
  1723. dev_set_drvdata(&port->dev, cap->driver_data);
  1724. port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
  1725. if (!port->cap) {
  1726. put_device(&port->dev);
  1727. return ERR_PTR(-ENOMEM);
  1728. }
  1729. port->sw = typec_switch_get(&port->dev);
  1730. if (IS_ERR(port->sw)) {
  1731. ret = PTR_ERR(port->sw);
  1732. put_device(&port->dev);
  1733. return ERR_PTR(ret);
  1734. }
  1735. port->mux = typec_mux_get(&port->dev, NULL);
  1736. if (IS_ERR(port->mux)) {
  1737. ret = PTR_ERR(port->mux);
  1738. put_device(&port->dev);
  1739. return ERR_PTR(ret);
  1740. }
  1741. ret = device_add(&port->dev);
  1742. if (ret) {
  1743. dev_err(parent, "failed to register port (%d)\n", ret);
  1744. put_device(&port->dev);
  1745. return ERR_PTR(ret);
  1746. }
  1747. return port;
  1748. }
  1749. EXPORT_SYMBOL_GPL(typec_register_port);
  1750. /**
  1751. * typec_unregister_port - Unregister a USB Type-C Port
  1752. * @port: The port to be unregistered
  1753. *
  1754. * Unregister device created with typec_register_port().
  1755. */
  1756. void typec_unregister_port(struct typec_port *port)
  1757. {
  1758. if (!IS_ERR_OR_NULL(port))
  1759. device_unregister(&port->dev);
  1760. }
  1761. EXPORT_SYMBOL_GPL(typec_unregister_port);
  1762. static int __init typec_init(void)
  1763. {
  1764. int ret;
  1765. ret = bus_register(&typec_bus);
  1766. if (ret)
  1767. return ret;
  1768. ret = class_register(&typec_mux_class);
  1769. if (ret)
  1770. goto err_unregister_bus;
  1771. typec_class = class_create(THIS_MODULE, "typec");
  1772. if (IS_ERR(typec_class)) {
  1773. ret = PTR_ERR(typec_class);
  1774. goto err_unregister_mux_class;
  1775. }
  1776. return 0;
  1777. err_unregister_mux_class:
  1778. class_unregister(&typec_mux_class);
  1779. err_unregister_bus:
  1780. bus_unregister(&typec_bus);
  1781. return ret;
  1782. }
  1783. subsys_initcall(typec_init);
  1784. static void __exit typec_exit(void)
  1785. {
  1786. class_destroy(typec_class);
  1787. ida_destroy(&typec_index_ida);
  1788. bus_unregister(&typec_bus);
  1789. class_unregister(&typec_mux_class);
  1790. }
  1791. module_exit(typec_exit);
  1792. MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
  1793. MODULE_LICENSE("GPL v2");
  1794. MODULE_DESCRIPTION("USB Type-C Connector Class");