extcon.c 37 KB

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