extcon.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/extcon/extcon.c - External Connector (extcon) framework.
  4. *
  5. * Copyright (C) 2015 Samsung Electronics
  6. * Author: Chanwoo Choi <[email protected]>
  7. *
  8. * Copyright (C) 2012 Samsung Electronics
  9. * Author: Donggeun Kim <[email protected]>
  10. * Author: MyungJoo Ham <[email protected]>
  11. *
  12. * based on android/drivers/switch/switch_class.c
  13. * Copyright (C) 2008 Google, Inc.
  14. * Author: Mike Lockwood <[email protected]>
  15. */
  16. #include <linux/module.h>
  17. #include <linux/types.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/err.h>
  22. #include <linux/of.h>
  23. #include <linux/slab.h>
  24. #include <linux/sysfs.h>
  25. #include "extcon.h"
  26. #define SUPPORTED_CABLE_MAX 32
  27. static const struct __extcon_info {
  28. unsigned int type;
  29. unsigned int id;
  30. const char *name;
  31. } extcon_info[] = {
  32. [EXTCON_NONE] = {
  33. .type = EXTCON_TYPE_MISC,
  34. .id = EXTCON_NONE,
  35. .name = "NONE",
  36. },
  37. /* USB external connector */
  38. [EXTCON_USB] = {
  39. .type = EXTCON_TYPE_USB,
  40. .id = EXTCON_USB,
  41. .name = "USB",
  42. },
  43. [EXTCON_USB_HOST] = {
  44. .type = EXTCON_TYPE_USB,
  45. .id = EXTCON_USB_HOST,
  46. .name = "USB-HOST",
  47. },
  48. /* Charging external connector */
  49. [EXTCON_CHG_USB_SDP] = {
  50. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  51. .id = EXTCON_CHG_USB_SDP,
  52. .name = "SDP",
  53. },
  54. [EXTCON_CHG_USB_DCP] = {
  55. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  56. .id = EXTCON_CHG_USB_DCP,
  57. .name = "DCP",
  58. },
  59. [EXTCON_CHG_USB_CDP] = {
  60. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  61. .id = EXTCON_CHG_USB_CDP,
  62. .name = "CDP",
  63. },
  64. [EXTCON_CHG_USB_ACA] = {
  65. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  66. .id = EXTCON_CHG_USB_ACA,
  67. .name = "ACA",
  68. },
  69. [EXTCON_CHG_USB_FAST] = {
  70. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  71. .id = EXTCON_CHG_USB_FAST,
  72. .name = "FAST-CHARGER",
  73. },
  74. [EXTCON_CHG_USB_SLOW] = {
  75. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  76. .id = EXTCON_CHG_USB_SLOW,
  77. .name = "SLOW-CHARGER",
  78. },
  79. [EXTCON_CHG_WPT] = {
  80. .type = EXTCON_TYPE_CHG,
  81. .id = EXTCON_CHG_WPT,
  82. .name = "WPT",
  83. },
  84. [EXTCON_CHG_USB_PD] = {
  85. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  86. .id = EXTCON_CHG_USB_PD,
  87. .name = "PD",
  88. },
  89. /* Jack external connector */
  90. [EXTCON_JACK_MICROPHONE] = {
  91. .type = EXTCON_TYPE_JACK,
  92. .id = EXTCON_JACK_MICROPHONE,
  93. .name = "MICROPHONE",
  94. },
  95. [EXTCON_JACK_HEADPHONE] = {
  96. .type = EXTCON_TYPE_JACK,
  97. .id = EXTCON_JACK_HEADPHONE,
  98. .name = "HEADPHONE",
  99. },
  100. [EXTCON_JACK_LINE_IN] = {
  101. .type = EXTCON_TYPE_JACK,
  102. .id = EXTCON_JACK_LINE_IN,
  103. .name = "LINE-IN",
  104. },
  105. [EXTCON_JACK_LINE_OUT] = {
  106. .type = EXTCON_TYPE_JACK,
  107. .id = EXTCON_JACK_LINE_OUT,
  108. .name = "LINE-OUT",
  109. },
  110. [EXTCON_JACK_VIDEO_IN] = {
  111. .type = EXTCON_TYPE_JACK,
  112. .id = EXTCON_JACK_VIDEO_IN,
  113. .name = "VIDEO-IN",
  114. },
  115. [EXTCON_JACK_VIDEO_OUT] = {
  116. .type = EXTCON_TYPE_JACK,
  117. .id = EXTCON_JACK_VIDEO_OUT,
  118. .name = "VIDEO-OUT",
  119. },
  120. [EXTCON_JACK_SPDIF_IN] = {
  121. .type = EXTCON_TYPE_JACK,
  122. .id = EXTCON_JACK_SPDIF_IN,
  123. .name = "SPDIF-IN",
  124. },
  125. [EXTCON_JACK_SPDIF_OUT] = {
  126. .type = EXTCON_TYPE_JACK,
  127. .id = EXTCON_JACK_SPDIF_OUT,
  128. .name = "SPDIF-OUT",
  129. },
  130. /* Display external connector */
  131. [EXTCON_DISP_HDMI] = {
  132. .type = EXTCON_TYPE_DISP,
  133. .id = EXTCON_DISP_HDMI,
  134. .name = "HDMI",
  135. },
  136. [EXTCON_DISP_MHL] = {
  137. .type = EXTCON_TYPE_DISP,
  138. .id = EXTCON_DISP_MHL,
  139. .name = "MHL",
  140. },
  141. [EXTCON_DISP_DVI] = {
  142. .type = EXTCON_TYPE_DISP,
  143. .id = EXTCON_DISP_DVI,
  144. .name = "DVI",
  145. },
  146. [EXTCON_DISP_VGA] = {
  147. .type = EXTCON_TYPE_DISP,
  148. .id = EXTCON_DISP_VGA,
  149. .name = "VGA",
  150. },
  151. [EXTCON_DISP_DP] = {
  152. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  153. .id = EXTCON_DISP_DP,
  154. .name = "DP",
  155. },
  156. [EXTCON_DISP_HMD] = {
  157. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  158. .id = EXTCON_DISP_HMD,
  159. .name = "HMD",
  160. },
  161. [EXTCON_DISP_CVBS] = {
  162. .type = EXTCON_TYPE_DISP,
  163. .id = EXTCON_DISP_CVBS,
  164. .name = "CVBS",
  165. },
  166. [EXTCON_DISP_EDP] = {
  167. .type = EXTCON_TYPE_DISP,
  168. .id = EXTCON_DISP_EDP,
  169. .name = "EDP",
  170. },
  171. /* Miscellaneous external connector */
  172. [EXTCON_DOCK] = {
  173. .type = EXTCON_TYPE_MISC,
  174. .id = EXTCON_DOCK,
  175. .name = "DOCK",
  176. },
  177. [EXTCON_JIG] = {
  178. .type = EXTCON_TYPE_MISC,
  179. .id = EXTCON_JIG,
  180. .name = "JIG",
  181. },
  182. [EXTCON_MECHANICAL] = {
  183. .type = EXTCON_TYPE_MISC,
  184. .id = EXTCON_MECHANICAL,
  185. .name = "MECHANICAL",
  186. },
  187. { /* sentinel */ }
  188. };
  189. /**
  190. * struct extcon_cable - An internal data for an external connector.
  191. * @edev: the extcon device
  192. * @cable_index: the index of this cable in the edev
  193. * @attr_g: the attribute group for the cable
  194. * @attr_name: "name" sysfs entry
  195. * @attr_state: "state" sysfs entry
  196. * @attrs: the array pointing to attr_name and attr_state for attr_g
  197. * @usb_propval: the array of USB connector properties
  198. * @chg_propval: the array of charger connector properties
  199. * @jack_propval: the array of jack connector properties
  200. * @disp_propval: the array of display connector properties
  201. * @usb_bits: the bit array of the USB connector property capabilities
  202. * @chg_bits: the bit array of the charger connector property capabilities
  203. * @jack_bits: the bit array of the jack connector property capabilities
  204. * @disp_bits: the bit array of the display connector property capabilities
  205. */
  206. struct extcon_cable {
  207. struct extcon_dev *edev;
  208. int cable_index;
  209. struct attribute_group attr_g;
  210. struct device_attribute attr_name;
  211. struct device_attribute attr_state;
  212. struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
  213. union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
  214. union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
  215. union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
  216. union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
  217. unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
  218. unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
  219. unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
  220. unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
  221. };
  222. static struct class *extcon_class;
  223. static LIST_HEAD(extcon_dev_list);
  224. static DEFINE_MUTEX(extcon_dev_list_lock);
  225. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  226. {
  227. int i = 0;
  228. if (!edev->mutually_exclusive)
  229. return 0;
  230. for (i = 0; edev->mutually_exclusive[i]; i++) {
  231. int weight;
  232. u32 correspondants = new_state & edev->mutually_exclusive[i];
  233. /* calculate the total number of bits set */
  234. weight = hweight32(correspondants);
  235. if (weight > 1)
  236. return i + 1;
  237. }
  238. return 0;
  239. }
  240. static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
  241. {
  242. int i;
  243. /* Find the index of extcon cable in edev->supported_cable */
  244. for (i = 0; i < edev->max_supported; i++) {
  245. if (edev->supported_cable[i] == id)
  246. return i;
  247. }
  248. return -EINVAL;
  249. }
  250. static int get_extcon_type(unsigned int prop)
  251. {
  252. switch (prop) {
  253. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  254. return EXTCON_TYPE_USB;
  255. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  256. return EXTCON_TYPE_CHG;
  257. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  258. return EXTCON_TYPE_JACK;
  259. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  260. return EXTCON_TYPE_DISP;
  261. default:
  262. return -EINVAL;
  263. }
  264. }
  265. static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
  266. {
  267. return !!(edev->state & BIT(index));
  268. }
  269. static bool is_extcon_changed(struct extcon_dev *edev, int index,
  270. bool new_state)
  271. {
  272. int state = !!(edev->state & BIT(index));
  273. return (state != new_state);
  274. }
  275. static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
  276. {
  277. int type;
  278. /* Check whether the property is supported or not. */
  279. type = get_extcon_type(prop);
  280. if (type < 0)
  281. return false;
  282. /* Check whether a specific extcon id supports the property or not. */
  283. return !!(extcon_info[id].type & type);
  284. }
  285. static int is_extcon_property_capability(struct extcon_dev *edev,
  286. unsigned int id, int index,unsigned int prop)
  287. {
  288. struct extcon_cable *cable;
  289. int type, ret;
  290. /* Check whether the property is supported or not. */
  291. type = get_extcon_type(prop);
  292. if (type < 0)
  293. return type;
  294. cable = &edev->cables[index];
  295. switch (type) {
  296. case EXTCON_TYPE_USB:
  297. ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  298. break;
  299. case EXTCON_TYPE_CHG:
  300. ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  301. break;
  302. case EXTCON_TYPE_JACK:
  303. ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  304. break;
  305. case EXTCON_TYPE_DISP:
  306. ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  307. break;
  308. default:
  309. ret = -EINVAL;
  310. }
  311. return ret;
  312. }
  313. static void init_property(struct extcon_dev *edev, unsigned int id, int index)
  314. {
  315. unsigned int type = extcon_info[id].type;
  316. struct extcon_cable *cable = &edev->cables[index];
  317. if (EXTCON_TYPE_USB & type)
  318. memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
  319. if (EXTCON_TYPE_CHG & type)
  320. memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
  321. if (EXTCON_TYPE_JACK & type)
  322. memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
  323. if (EXTCON_TYPE_DISP & type)
  324. memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
  325. }
  326. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  327. char *buf)
  328. {
  329. int i, count = 0;
  330. struct extcon_dev *edev = dev_get_drvdata(dev);
  331. if (edev->max_supported == 0)
  332. return sprintf(buf, "%u\n", edev->state);
  333. for (i = 0; i < edev->max_supported; i++) {
  334. count += sprintf(buf + count, "%s=%d\n",
  335. extcon_info[edev->supported_cable[i]].name,
  336. !!(edev->state & BIT(i)));
  337. }
  338. return count;
  339. }
  340. static DEVICE_ATTR_RO(state);
  341. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  342. char *buf)
  343. {
  344. struct extcon_dev *edev = dev_get_drvdata(dev);
  345. return sprintf(buf, "%s\n", edev->name);
  346. }
  347. static DEVICE_ATTR_RO(name);
  348. static ssize_t cable_name_show(struct device *dev,
  349. struct device_attribute *attr, char *buf)
  350. {
  351. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  352. attr_name);
  353. int i = cable->cable_index;
  354. return sprintf(buf, "%s\n",
  355. extcon_info[cable->edev->supported_cable[i]].name);
  356. }
  357. static ssize_t cable_state_show(struct device *dev,
  358. struct device_attribute *attr, char *buf)
  359. {
  360. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  361. attr_state);
  362. int i = cable->cable_index;
  363. return sprintf(buf, "%d\n",
  364. extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
  365. }
  366. /**
  367. * extcon_sync() - Synchronize the state for an external connector.
  368. * @edev: the extcon device
  369. * @id: the unique id indicating an external connector
  370. *
  371. * Note that this function send a notification in order to synchronize
  372. * the state and property of an external connector.
  373. *
  374. * Returns 0 if success or error number if fail.
  375. */
  376. int extcon_sync(struct extcon_dev *edev, unsigned int id)
  377. {
  378. char name_buf[120];
  379. char state_buf[120];
  380. char *prop_buf;
  381. char *envp[3];
  382. int env_offset = 0;
  383. int length;
  384. int index;
  385. int state;
  386. unsigned long flags;
  387. if (!edev)
  388. return -EINVAL;
  389. index = find_cable_index_by_id(edev, id);
  390. if (index < 0)
  391. return index;
  392. spin_lock_irqsave(&edev->lock, flags);
  393. state = !!(edev->state & BIT(index));
  394. spin_unlock_irqrestore(&edev->lock, flags);
  395. /*
  396. * Call functions in a raw notifier chain for the specific one
  397. * external connector.
  398. */
  399. raw_notifier_call_chain(&edev->nh[index], state, edev);
  400. /*
  401. * Call functions in a raw notifier chain for the all supported
  402. * external connectors.
  403. */
  404. raw_notifier_call_chain(&edev->nh_all, state, edev);
  405. spin_lock_irqsave(&edev->lock, flags);
  406. /* This could be in interrupt handler */
  407. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  408. if (!prop_buf) {
  409. /* Unlock early before uevent */
  410. spin_unlock_irqrestore(&edev->lock, flags);
  411. dev_err(&edev->dev, "out of memory in extcon_set_state\n");
  412. kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
  413. return -ENOMEM;
  414. }
  415. length = name_show(&edev->dev, NULL, prop_buf);
  416. if (length > 0) {
  417. if (prop_buf[length - 1] == '\n')
  418. prop_buf[length - 1] = 0;
  419. snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
  420. envp[env_offset++] = name_buf;
  421. }
  422. length = state_show(&edev->dev, NULL, prop_buf);
  423. if (length > 0) {
  424. if (prop_buf[length - 1] == '\n')
  425. prop_buf[length - 1] = 0;
  426. snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
  427. envp[env_offset++] = state_buf;
  428. }
  429. envp[env_offset] = NULL;
  430. /* Unlock early before uevent */
  431. spin_unlock_irqrestore(&edev->lock, flags);
  432. kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
  433. free_page((unsigned long)prop_buf);
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(extcon_sync);
  437. /**
  438. * extcon_get_state() - Get the state of an external connector.
  439. * @edev: the extcon device
  440. * @id: the unique id indicating an external connector
  441. *
  442. * Returns 0 if success or error number if fail.
  443. */
  444. int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
  445. {
  446. int index, state;
  447. unsigned long flags;
  448. if (!edev)
  449. return -EINVAL;
  450. index = find_cable_index_by_id(edev, id);
  451. if (index < 0)
  452. return index;
  453. spin_lock_irqsave(&edev->lock, flags);
  454. state = is_extcon_attached(edev, index);
  455. spin_unlock_irqrestore(&edev->lock, flags);
  456. return state;
  457. }
  458. EXPORT_SYMBOL_GPL(extcon_get_state);
  459. /**
  460. * extcon_set_state() - Set the state of an external connector.
  461. * @edev: the extcon device
  462. * @id: the unique id indicating an external connector
  463. * @state: the new state of an external connector.
  464. * the default semantics is true: attached / false: detached.
  465. *
  466. * Note that this function set the state of an external connector without
  467. * a notification. To synchronize the state of an external connector,
  468. * have to use extcon_set_state_sync() and extcon_sync().
  469. *
  470. * Returns 0 if success or error number if fail.
  471. */
  472. int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
  473. {
  474. unsigned long flags;
  475. int index, ret = 0;
  476. if (!edev)
  477. return -EINVAL;
  478. index = find_cable_index_by_id(edev, id);
  479. if (index < 0)
  480. return index;
  481. spin_lock_irqsave(&edev->lock, flags);
  482. /* Check whether the external connector's state is changed. */
  483. if (!is_extcon_changed(edev, index, state))
  484. goto out;
  485. if (check_mutually_exclusive(edev,
  486. (edev->state & ~BIT(index)) | (state & BIT(index)))) {
  487. ret = -EPERM;
  488. goto out;
  489. }
  490. /*
  491. * Initialize the value of extcon property before setting
  492. * the detached state for an external connector.
  493. */
  494. if (!state)
  495. init_property(edev, id, index);
  496. /* Update the state for an external connector. */
  497. if (state)
  498. edev->state |= BIT(index);
  499. else
  500. edev->state &= ~(BIT(index));
  501. out:
  502. spin_unlock_irqrestore(&edev->lock, flags);
  503. return ret;
  504. }
  505. EXPORT_SYMBOL_GPL(extcon_set_state);
  506. /**
  507. * extcon_set_state_sync() - Set the state of an external connector with sync.
  508. * @edev: the extcon device
  509. * @id: the unique id indicating an external connector
  510. * @state: the new state of external connector.
  511. * the default semantics is true: attached / false: detached.
  512. *
  513. * Note that this function set the state of external connector
  514. * and synchronize the state by sending a notification.
  515. *
  516. * Returns 0 if success or error number if fail.
  517. */
  518. int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
  519. {
  520. int ret;
  521. ret = extcon_set_state(edev, id, state);
  522. if (ret < 0)
  523. return ret;
  524. return extcon_sync(edev, id);
  525. }
  526. EXPORT_SYMBOL_GPL(extcon_set_state_sync);
  527. /**
  528. * extcon_get_property() - Get the property value of an external connector.
  529. * @edev: the extcon device
  530. * @id: the unique id indicating an external connector
  531. * @prop: the property id indicating an extcon property
  532. * @prop_val: the pointer which store the value of extcon property
  533. *
  534. * Note that when getting the property value of external connector,
  535. * the external connector should be attached. If detached state, function
  536. * return 0 without property value. Also, the each property should be
  537. * included in the list of supported properties according to extcon type.
  538. *
  539. * Returns 0 if success or error number if fail.
  540. */
  541. int extcon_get_property(struct extcon_dev *edev, unsigned int id,
  542. unsigned int prop,
  543. union extcon_property_value *prop_val)
  544. {
  545. struct extcon_cable *cable;
  546. unsigned long flags;
  547. int index, ret = 0;
  548. *prop_val = (union extcon_property_value){0};
  549. if (!edev)
  550. return -EINVAL;
  551. /* Check whether the property is supported or not */
  552. if (!is_extcon_property_supported(id, prop))
  553. return -EINVAL;
  554. /* Find the cable index of external connector by using id */
  555. index = find_cable_index_by_id(edev, id);
  556. if (index < 0)
  557. return index;
  558. spin_lock_irqsave(&edev->lock, flags);
  559. /* Check whether the property is available or not. */
  560. if (!is_extcon_property_capability(edev, id, index, prop)) {
  561. spin_unlock_irqrestore(&edev->lock, flags);
  562. return -EPERM;
  563. }
  564. /*
  565. * Check whether the external connector is attached.
  566. * If external connector is detached, the user can not
  567. * get the property value.
  568. */
  569. if (!is_extcon_attached(edev, index)) {
  570. spin_unlock_irqrestore(&edev->lock, flags);
  571. return 0;
  572. }
  573. cable = &edev->cables[index];
  574. /* Get the property value according to extcon type */
  575. switch (prop) {
  576. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  577. *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
  578. break;
  579. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  580. *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
  581. break;
  582. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  583. *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
  584. break;
  585. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  586. *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
  587. break;
  588. default:
  589. ret = -EINVAL;
  590. break;
  591. }
  592. spin_unlock_irqrestore(&edev->lock, flags);
  593. return ret;
  594. }
  595. EXPORT_SYMBOL_GPL(extcon_get_property);
  596. /**
  597. * extcon_set_property() - Set the property value of an external connector.
  598. * @edev: the extcon device
  599. * @id: the unique id indicating an external connector
  600. * @prop: the property id indicating an extcon property
  601. * @prop_val: the pointer including the new value of extcon property
  602. *
  603. * Note that each property should be included in the list of supported
  604. * properties according to the extcon type.
  605. *
  606. * Returns 0 if success or error number if fail.
  607. */
  608. int extcon_set_property(struct extcon_dev *edev, unsigned int id,
  609. unsigned int prop,
  610. union extcon_property_value prop_val)
  611. {
  612. struct extcon_cable *cable;
  613. unsigned long flags;
  614. int index, ret = 0;
  615. if (!edev)
  616. return -EINVAL;
  617. /* Check whether the property is supported or not */
  618. if (!is_extcon_property_supported(id, prop))
  619. return -EINVAL;
  620. /* Find the cable index of external connector by using id */
  621. index = find_cable_index_by_id(edev, id);
  622. if (index < 0)
  623. return index;
  624. spin_lock_irqsave(&edev->lock, flags);
  625. /* Check whether the property is available or not. */
  626. if (!is_extcon_property_capability(edev, id, index, prop)) {
  627. spin_unlock_irqrestore(&edev->lock, flags);
  628. return -EPERM;
  629. }
  630. cable = &edev->cables[index];
  631. /* Set the property value according to extcon type */
  632. switch (prop) {
  633. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  634. cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
  635. break;
  636. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  637. cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
  638. break;
  639. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  640. cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
  641. break;
  642. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  643. cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
  644. break;
  645. default:
  646. ret = -EINVAL;
  647. break;
  648. }
  649. spin_unlock_irqrestore(&edev->lock, flags);
  650. return ret;
  651. }
  652. EXPORT_SYMBOL_GPL(extcon_set_property);
  653. /**
  654. * extcon_set_property_sync() - Set property of an external connector with sync.
  655. * @edev: the extcon device
  656. * @id: the unique id indicating an external connector
  657. * @prop: the property id indicating an extcon property
  658. * @prop_val: the pointer including the new value of extcon property
  659. *
  660. * Note that when setting the property value of external connector,
  661. * the external connector should be attached. The each property should
  662. * be included in the list of supported properties according to extcon type.
  663. *
  664. * Returns 0 if success or error number if fail.
  665. */
  666. int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
  667. unsigned int prop,
  668. union extcon_property_value prop_val)
  669. {
  670. int ret;
  671. ret = extcon_set_property(edev, id, prop, prop_val);
  672. if (ret < 0)
  673. return ret;
  674. return extcon_sync(edev, id);
  675. }
  676. EXPORT_SYMBOL_GPL(extcon_set_property_sync);
  677. /**
  678. * extcon_get_property_capability() - Get the capability of the property
  679. * for an external connector.
  680. * @edev: the extcon device
  681. * @id: the unique id indicating an external connector
  682. * @prop: the property id indicating an extcon property
  683. *
  684. * Returns 1 if the property is available or 0 if not available.
  685. */
  686. int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
  687. unsigned int prop)
  688. {
  689. int index;
  690. if (!edev)
  691. return -EINVAL;
  692. /* Check whether the property is supported or not */
  693. if (!is_extcon_property_supported(id, prop))
  694. return -EINVAL;
  695. /* Find the cable index of external connector by using id */
  696. index = find_cable_index_by_id(edev, id);
  697. if (index < 0)
  698. return index;
  699. return is_extcon_property_capability(edev, id, index, prop);
  700. }
  701. EXPORT_SYMBOL_GPL(extcon_get_property_capability);
  702. /**
  703. * extcon_set_property_capability() - Set the capability of the property
  704. * for an external connector.
  705. * @edev: the extcon device
  706. * @id: the unique id indicating an external connector
  707. * @prop: the property id indicating an extcon property
  708. *
  709. * Note that this function set the capability of the property
  710. * for an external connector in order to mark the bit in capability
  711. * bitmap which mean the available state of the property.
  712. *
  713. * Returns 0 if success or error number if fail.
  714. */
  715. int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
  716. unsigned int prop)
  717. {
  718. struct extcon_cable *cable;
  719. int index, type, ret = 0;
  720. if (!edev)
  721. return -EINVAL;
  722. /* Check whether the property is supported or not. */
  723. if (!is_extcon_property_supported(id, prop))
  724. return -EINVAL;
  725. /* Find the cable index of external connector by using id. */
  726. index = find_cable_index_by_id(edev, id);
  727. if (index < 0)
  728. return index;
  729. type = get_extcon_type(prop);
  730. if (type < 0)
  731. return type;
  732. cable = &edev->cables[index];
  733. switch (type) {
  734. case EXTCON_TYPE_USB:
  735. __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  736. break;
  737. case EXTCON_TYPE_CHG:
  738. __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  739. break;
  740. case EXTCON_TYPE_JACK:
  741. __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  742. break;
  743. case EXTCON_TYPE_DISP:
  744. __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  745. break;
  746. default:
  747. ret = -EINVAL;
  748. }
  749. return ret;
  750. }
  751. EXPORT_SYMBOL_GPL(extcon_set_property_capability);
  752. /**
  753. * extcon_get_extcon_dev() - Get the extcon device instance from the name.
  754. * @extcon_name: the extcon name provided with extcon_dev_register()
  755. *
  756. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  757. * NOTE: This function returns -EPROBE_DEFER so it may only be called from
  758. * probe() functions.
  759. */
  760. struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
  761. {
  762. struct extcon_dev *sd;
  763. if (!extcon_name)
  764. return ERR_PTR(-EINVAL);
  765. mutex_lock(&extcon_dev_list_lock);
  766. list_for_each_entry(sd, &extcon_dev_list, entry) {
  767. if (!strcmp(sd->name, extcon_name))
  768. goto out;
  769. }
  770. sd = ERR_PTR(-EPROBE_DEFER);
  771. out:
  772. mutex_unlock(&extcon_dev_list_lock);
  773. return sd;
  774. }
  775. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  776. /**
  777. * extcon_register_notifier() - Register a notifier block to get notified by
  778. * any state changes from the extcon.
  779. * @edev: the extcon device
  780. * @id: the unique id indicating an external connector
  781. * @nb: a notifier block to be registered
  782. *
  783. * Note that the second parameter given to the callback of nb (val) is
  784. * the current state of an external connector and the third pameter
  785. * is the pointer of extcon device.
  786. *
  787. * Returns 0 if success or error number if fail.
  788. */
  789. int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
  790. struct notifier_block *nb)
  791. {
  792. unsigned long flags;
  793. int ret, idx;
  794. if (!edev || !nb)
  795. return -EINVAL;
  796. idx = find_cable_index_by_id(edev, id);
  797. if (idx < 0)
  798. return idx;
  799. spin_lock_irqsave(&edev->lock, flags);
  800. ret = raw_notifier_chain_register(&edev->nh[idx], nb);
  801. spin_unlock_irqrestore(&edev->lock, flags);
  802. return ret;
  803. }
  804. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  805. /**
  806. * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
  807. * @edev: the extcon device
  808. * @id: the unique id indicating an external connector
  809. * @nb: a notifier block to be registered
  810. *
  811. * Returns 0 if success or error number if fail.
  812. */
  813. int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
  814. struct notifier_block *nb)
  815. {
  816. unsigned long flags;
  817. int ret, idx;
  818. if (!edev || !nb)
  819. return -EINVAL;
  820. idx = find_cable_index_by_id(edev, id);
  821. if (idx < 0)
  822. return idx;
  823. spin_lock_irqsave(&edev->lock, flags);
  824. ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
  825. spin_unlock_irqrestore(&edev->lock, flags);
  826. return ret;
  827. }
  828. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  829. /**
  830. * extcon_register_notifier_all() - Register a notifier block for all connectors.
  831. * @edev: the extcon device
  832. * @nb: a notifier block to be registered
  833. *
  834. * Note that this function registers a notifier block in order to receive
  835. * the state change of all supported external connectors from extcon device.
  836. * And the second parameter given to the callback of nb (val) is
  837. * the current state and the third pameter is the pointer of extcon device.
  838. *
  839. * Returns 0 if success or error number if fail.
  840. */
  841. int extcon_register_notifier_all(struct extcon_dev *edev,
  842. struct notifier_block *nb)
  843. {
  844. unsigned long flags;
  845. int ret;
  846. if (!edev || !nb)
  847. return -EINVAL;
  848. spin_lock_irqsave(&edev->lock, flags);
  849. ret = raw_notifier_chain_register(&edev->nh_all, nb);
  850. spin_unlock_irqrestore(&edev->lock, flags);
  851. return ret;
  852. }
  853. EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
  854. /**
  855. * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
  856. * @edev: the extcon device
  857. * @nb: a notifier block to be registered
  858. *
  859. * Returns 0 if success or error number if fail.
  860. */
  861. int extcon_unregister_notifier_all(struct extcon_dev *edev,
  862. struct notifier_block *nb)
  863. {
  864. unsigned long flags;
  865. int ret;
  866. if (!edev || !nb)
  867. return -EINVAL;
  868. spin_lock_irqsave(&edev->lock, flags);
  869. ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
  870. spin_unlock_irqrestore(&edev->lock, flags);
  871. return ret;
  872. }
  873. EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
  874. static struct attribute *extcon_attrs[] = {
  875. &dev_attr_state.attr,
  876. &dev_attr_name.attr,
  877. NULL,
  878. };
  879. ATTRIBUTE_GROUPS(extcon);
  880. static int create_extcon_class(void)
  881. {
  882. if (!extcon_class) {
  883. extcon_class = class_create(THIS_MODULE, "extcon");
  884. if (IS_ERR(extcon_class))
  885. return PTR_ERR(extcon_class);
  886. extcon_class->dev_groups = extcon_groups;
  887. }
  888. return 0;
  889. }
  890. static void extcon_dev_release(struct device *dev)
  891. {
  892. }
  893. static const char *muex_name = "mutually_exclusive";
  894. static void dummy_sysfs_dev_release(struct device *dev)
  895. {
  896. }
  897. /*
  898. * extcon_dev_allocate() - Allocate the memory of extcon device.
  899. * @supported_cable: the array of the supported external connectors
  900. * ending with EXTCON_NONE.
  901. *
  902. * Note that this function allocates the memory for extcon device
  903. * and initialize default setting for the extcon device.
  904. *
  905. * Returns the pointer memory of allocated extcon_dev if success
  906. * or ERR_PTR(err) if fail.
  907. */
  908. struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
  909. {
  910. struct extcon_dev *edev;
  911. if (!supported_cable)
  912. return ERR_PTR(-EINVAL);
  913. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  914. if (!edev)
  915. return ERR_PTR(-ENOMEM);
  916. edev->max_supported = 0;
  917. edev->supported_cable = supported_cable;
  918. return edev;
  919. }
  920. /*
  921. * extcon_dev_free() - Free the memory of extcon device.
  922. * @edev: the extcon device
  923. */
  924. void extcon_dev_free(struct extcon_dev *edev)
  925. {
  926. kfree(edev);
  927. }
  928. EXPORT_SYMBOL_GPL(extcon_dev_free);
  929. /**
  930. * extcon_dev_register() - Register an new extcon device
  931. * @edev: the extcon device to be registered
  932. *
  933. * Among the members of edev struct, please set the "user initializing data"
  934. * do not set the values of "internal data", which are initialized by
  935. * this function.
  936. *
  937. * Note that before calling this funciton, have to allocate the memory
  938. * of an extcon device by using the extcon_dev_allocate(). And the extcon
  939. * dev should include the supported_cable information.
  940. *
  941. * Returns 0 if success or error number if fail.
  942. */
  943. int extcon_dev_register(struct extcon_dev *edev)
  944. {
  945. int ret, index = 0;
  946. static atomic_t edev_no = ATOMIC_INIT(-1);
  947. if (!extcon_class) {
  948. ret = create_extcon_class();
  949. if (ret < 0)
  950. return ret;
  951. }
  952. if (!edev || !edev->supported_cable)
  953. return -EINVAL;
  954. for (; edev->supported_cable[index] != EXTCON_NONE; index++);
  955. edev->max_supported = index;
  956. if (index > SUPPORTED_CABLE_MAX) {
  957. dev_err(&edev->dev,
  958. "exceed the maximum number of supported cables\n");
  959. return -EINVAL;
  960. }
  961. edev->dev.class = extcon_class;
  962. edev->dev.release = extcon_dev_release;
  963. edev->name = dev_name(edev->dev.parent);
  964. if (IS_ERR_OR_NULL(edev->name)) {
  965. dev_err(&edev->dev,
  966. "extcon device name is null\n");
  967. return -EINVAL;
  968. }
  969. dev_set_name(&edev->dev, "extcon%lu",
  970. (unsigned long)atomic_inc_return(&edev_no));
  971. if (edev->max_supported) {
  972. char *str;
  973. struct extcon_cable *cable;
  974. edev->cables = kcalloc(edev->max_supported,
  975. sizeof(struct extcon_cable),
  976. GFP_KERNEL);
  977. if (!edev->cables) {
  978. ret = -ENOMEM;
  979. goto err_sysfs_alloc;
  980. }
  981. for (index = 0; index < edev->max_supported; index++) {
  982. cable = &edev->cables[index];
  983. str = kasprintf(GFP_KERNEL, "cable.%d", index);
  984. if (!str) {
  985. for (index--; index >= 0; index--) {
  986. cable = &edev->cables[index];
  987. kfree(cable->attr_g.name);
  988. }
  989. ret = -ENOMEM;
  990. goto err_alloc_cables;
  991. }
  992. cable->edev = edev;
  993. cable->cable_index = index;
  994. cable->attrs[0] = &cable->attr_name.attr;
  995. cable->attrs[1] = &cable->attr_state.attr;
  996. cable->attrs[2] = NULL;
  997. cable->attr_g.name = str;
  998. cable->attr_g.attrs = cable->attrs;
  999. sysfs_attr_init(&cable->attr_name.attr);
  1000. cable->attr_name.attr.name = "name";
  1001. cable->attr_name.attr.mode = 0444;
  1002. cable->attr_name.show = cable_name_show;
  1003. sysfs_attr_init(&cable->attr_state.attr);
  1004. cable->attr_state.attr.name = "state";
  1005. cable->attr_state.attr.mode = 0444;
  1006. cable->attr_state.show = cable_state_show;
  1007. }
  1008. }
  1009. if (edev->max_supported && edev->mutually_exclusive) {
  1010. char *name;
  1011. /* Count the size of mutually_exclusive array */
  1012. for (index = 0; edev->mutually_exclusive[index]; index++)
  1013. ;
  1014. edev->attrs_muex = kcalloc(index + 1,
  1015. sizeof(struct attribute *),
  1016. GFP_KERNEL);
  1017. if (!edev->attrs_muex) {
  1018. ret = -ENOMEM;
  1019. goto err_muex;
  1020. }
  1021. edev->d_attrs_muex = kcalloc(index,
  1022. sizeof(struct device_attribute),
  1023. GFP_KERNEL);
  1024. if (!edev->d_attrs_muex) {
  1025. ret = -ENOMEM;
  1026. kfree(edev->attrs_muex);
  1027. goto err_muex;
  1028. }
  1029. for (index = 0; edev->mutually_exclusive[index]; index++) {
  1030. name = kasprintf(GFP_KERNEL, "0x%x",
  1031. edev->mutually_exclusive[index]);
  1032. if (!name) {
  1033. for (index--; index >= 0; index--) {
  1034. kfree(edev->d_attrs_muex[index].attr.
  1035. name);
  1036. }
  1037. kfree(edev->d_attrs_muex);
  1038. kfree(edev->attrs_muex);
  1039. ret = -ENOMEM;
  1040. goto err_muex;
  1041. }
  1042. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  1043. edev->d_attrs_muex[index].attr.name = name;
  1044. edev->d_attrs_muex[index].attr.mode = 0000;
  1045. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  1046. .attr;
  1047. }
  1048. edev->attr_g_muex.name = muex_name;
  1049. edev->attr_g_muex.attrs = edev->attrs_muex;
  1050. }
  1051. if (edev->max_supported) {
  1052. edev->extcon_dev_type.groups =
  1053. kcalloc(edev->max_supported + 2,
  1054. sizeof(struct attribute_group *),
  1055. GFP_KERNEL);
  1056. if (!edev->extcon_dev_type.groups) {
  1057. ret = -ENOMEM;
  1058. goto err_alloc_groups;
  1059. }
  1060. edev->extcon_dev_type.name = dev_name(&edev->dev);
  1061. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  1062. for (index = 0; index < edev->max_supported; index++)
  1063. edev->extcon_dev_type.groups[index] =
  1064. &edev->cables[index].attr_g;
  1065. if (edev->mutually_exclusive)
  1066. edev->extcon_dev_type.groups[index] =
  1067. &edev->attr_g_muex;
  1068. edev->dev.type = &edev->extcon_dev_type;
  1069. }
  1070. spin_lock_init(&edev->lock);
  1071. if (edev->max_supported) {
  1072. edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
  1073. GFP_KERNEL);
  1074. if (!edev->nh) {
  1075. ret = -ENOMEM;
  1076. goto err_alloc_nh;
  1077. }
  1078. }
  1079. for (index = 0; index < edev->max_supported; index++)
  1080. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  1081. RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
  1082. dev_set_drvdata(&edev->dev, edev);
  1083. edev->state = 0;
  1084. ret = device_register(&edev->dev);
  1085. if (ret) {
  1086. put_device(&edev->dev);
  1087. goto err_dev;
  1088. }
  1089. mutex_lock(&extcon_dev_list_lock);
  1090. list_add(&edev->entry, &extcon_dev_list);
  1091. mutex_unlock(&extcon_dev_list_lock);
  1092. return 0;
  1093. err_dev:
  1094. if (edev->max_supported)
  1095. kfree(edev->nh);
  1096. err_alloc_nh:
  1097. if (edev->max_supported)
  1098. kfree(edev->extcon_dev_type.groups);
  1099. err_alloc_groups:
  1100. if (edev->max_supported && edev->mutually_exclusive) {
  1101. for (index = 0; edev->mutually_exclusive[index]; index++)
  1102. kfree(edev->d_attrs_muex[index].attr.name);
  1103. kfree(edev->d_attrs_muex);
  1104. kfree(edev->attrs_muex);
  1105. }
  1106. err_muex:
  1107. for (index = 0; index < edev->max_supported; index++)
  1108. kfree(edev->cables[index].attr_g.name);
  1109. err_alloc_cables:
  1110. if (edev->max_supported)
  1111. kfree(edev->cables);
  1112. err_sysfs_alloc:
  1113. return ret;
  1114. }
  1115. EXPORT_SYMBOL_GPL(extcon_dev_register);
  1116. /**
  1117. * extcon_dev_unregister() - Unregister the extcon device.
  1118. * @edev: the extcon device to be unregistered.
  1119. *
  1120. * Note that this does not call kfree(edev) because edev was not allocated
  1121. * by this class.
  1122. */
  1123. void extcon_dev_unregister(struct extcon_dev *edev)
  1124. {
  1125. int index;
  1126. if (!edev)
  1127. return;
  1128. mutex_lock(&extcon_dev_list_lock);
  1129. list_del(&edev->entry);
  1130. mutex_unlock(&extcon_dev_list_lock);
  1131. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  1132. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  1133. dev_name(&edev->dev));
  1134. return;
  1135. }
  1136. device_unregister(&edev->dev);
  1137. if (edev->mutually_exclusive && edev->max_supported) {
  1138. for (index = 0; edev->mutually_exclusive[index];
  1139. index++)
  1140. kfree(edev->d_attrs_muex[index].attr.name);
  1141. kfree(edev->d_attrs_muex);
  1142. kfree(edev->attrs_muex);
  1143. }
  1144. for (index = 0; index < edev->max_supported; index++)
  1145. kfree(edev->cables[index].attr_g.name);
  1146. if (edev->max_supported) {
  1147. kfree(edev->extcon_dev_type.groups);
  1148. kfree(edev->cables);
  1149. kfree(edev->nh);
  1150. }
  1151. put_device(&edev->dev);
  1152. }
  1153. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  1154. #ifdef CONFIG_OF
  1155. /*
  1156. * extcon_find_edev_by_node - Find the extcon device from devicetree.
  1157. * @node : OF node identifying edev
  1158. *
  1159. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  1160. */
  1161. struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
  1162. {
  1163. struct extcon_dev *edev;
  1164. mutex_lock(&extcon_dev_list_lock);
  1165. list_for_each_entry(edev, &extcon_dev_list, entry)
  1166. if (edev->dev.parent && edev->dev.parent->of_node == node)
  1167. goto out;
  1168. edev = ERR_PTR(-EPROBE_DEFER);
  1169. out:
  1170. mutex_unlock(&extcon_dev_list_lock);
  1171. return edev;
  1172. }
  1173. /*
  1174. * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
  1175. * @dev : the instance to the given device
  1176. * @index : the index into list of extcon_dev
  1177. *
  1178. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  1179. */
  1180. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1181. {
  1182. struct device_node *node;
  1183. struct extcon_dev *edev;
  1184. if (!dev)
  1185. return ERR_PTR(-EINVAL);
  1186. if (!dev->of_node) {
  1187. dev_dbg(dev, "device does not have a device node entry\n");
  1188. return ERR_PTR(-EINVAL);
  1189. }
  1190. node = of_parse_phandle(dev->of_node, "extcon", index);
  1191. if (!node) {
  1192. dev_dbg(dev, "failed to get phandle in %pOF node\n",
  1193. dev->of_node);
  1194. return ERR_PTR(-ENODEV);
  1195. }
  1196. edev = extcon_find_edev_by_node(node);
  1197. of_node_put(node);
  1198. return edev;
  1199. }
  1200. #else
  1201. struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
  1202. {
  1203. return ERR_PTR(-ENOSYS);
  1204. }
  1205. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1206. {
  1207. return ERR_PTR(-ENOSYS);
  1208. }
  1209. #endif /* CONFIG_OF */
  1210. EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
  1211. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  1212. /**
  1213. * extcon_get_edev_name() - Get the name of the extcon device.
  1214. * @edev: the extcon device
  1215. */
  1216. const char *extcon_get_edev_name(struct extcon_dev *edev)
  1217. {
  1218. return !edev ? NULL : edev->name;
  1219. }
  1220. EXPORT_SYMBOL_GPL(extcon_get_edev_name);
  1221. static int __init extcon_class_init(void)
  1222. {
  1223. return create_extcon_class();
  1224. }
  1225. module_init(extcon_class_init);
  1226. static void __exit extcon_class_exit(void)
  1227. {
  1228. class_destroy(extcon_class);
  1229. }
  1230. module_exit(extcon_class_exit);
  1231. MODULE_AUTHOR("Chanwoo Choi <[email protected]>");
  1232. MODULE_AUTHOR("MyungJoo Ham <[email protected]>");
  1233. MODULE_DESCRIPTION("External Connector (extcon) framework");
  1234. MODULE_LICENSE("GPL v2");