extcon.c 36 KB

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