extcon.c 36 KB

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