extcon.c 36 KB

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