extcon.c 35 KB

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