extcon.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  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(sizeof(*edev), GFP_KERNEL);
  916. if (!edev)
  917. return ERR_PTR(-ENOMEM);
  918. edev->max_supported = 0;
  919. edev->supported_cable = supported_cable;
  920. return edev;
  921. }
  922. /*
  923. * extcon_dev_free() - Free the memory of extcon device.
  924. * @edev: the extcon device
  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 = kcalloc(edev->max_supported, sizeof(*edev->cables),
  947. GFP_KERNEL);
  948. if (!edev->cables)
  949. return -ENOMEM;
  950. for (index = 0; index < edev->max_supported; index++) {
  951. cable = &edev->cables[index];
  952. str = kasprintf(GFP_KERNEL, "cable.%d", index);
  953. if (!str) {
  954. for (index--; index >= 0; index--) {
  955. cable = &edev->cables[index];
  956. kfree(cable->attr_g.name);
  957. }
  958. kfree(edev->cables);
  959. return -ENOMEM;
  960. }
  961. cable->edev = edev;
  962. cable->cable_index = index;
  963. cable->attrs[0] = &cable->attr_name.attr;
  964. cable->attrs[1] = &cable->attr_state.attr;
  965. cable->attrs[2] = NULL;
  966. cable->attr_g.name = str;
  967. cable->attr_g.attrs = cable->attrs;
  968. sysfs_attr_init(&cable->attr_name.attr);
  969. cable->attr_name.attr.name = "name";
  970. cable->attr_name.attr.mode = 0444;
  971. cable->attr_name.show = cable_name_show;
  972. sysfs_attr_init(&cable->attr_state.attr);
  973. cable->attr_state.attr.name = "state";
  974. cable->attr_state.attr.mode = 0444;
  975. cable->attr_state.show = cable_state_show;
  976. }
  977. return 0;
  978. }
  979. /**
  980. * extcon_alloc_muex() - alloc the mutual exclusive for extcon device
  981. * @edev: extcon device
  982. *
  983. * Returns 0 if success or error number if fail.
  984. */
  985. static int extcon_alloc_muex(struct extcon_dev *edev)
  986. {
  987. char *name;
  988. int index;
  989. if (!edev)
  990. return -EINVAL;
  991. if (!(edev->max_supported && edev->mutually_exclusive))
  992. return 0;
  993. /* Count the size of mutually_exclusive array */
  994. for (index = 0; edev->mutually_exclusive[index]; index++)
  995. ;
  996. edev->attrs_muex = kcalloc(index + 1, sizeof(*edev->attrs_muex),
  997. GFP_KERNEL);
  998. if (!edev->attrs_muex)
  999. return -ENOMEM;
  1000. edev->d_attrs_muex = kcalloc(index, sizeof(*edev->d_attrs_muex),
  1001. GFP_KERNEL);
  1002. if (!edev->d_attrs_muex) {
  1003. kfree(edev->attrs_muex);
  1004. return -ENOMEM;
  1005. }
  1006. for (index = 0; edev->mutually_exclusive[index]; index++) {
  1007. name = kasprintf(GFP_KERNEL, "0x%x",
  1008. edev->mutually_exclusive[index]);
  1009. if (!name) {
  1010. for (index--; index >= 0; index--)
  1011. kfree(edev->d_attrs_muex[index].attr.name);
  1012. kfree(edev->d_attrs_muex);
  1013. kfree(edev->attrs_muex);
  1014. return -ENOMEM;
  1015. }
  1016. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  1017. edev->d_attrs_muex[index].attr.name = name;
  1018. edev->d_attrs_muex[index].attr.mode = 0000;
  1019. edev->attrs_muex[index] = &edev->d_attrs_muex[index].attr;
  1020. }
  1021. edev->attr_g_muex.name = muex_name;
  1022. edev->attr_g_muex.attrs = edev->attrs_muex;
  1023. return 0;
  1024. }
  1025. /**
  1026. * extcon_alloc_groups() - alloc the groups for extcon device
  1027. * @edev: extcon device
  1028. *
  1029. * Returns 0 if success or error number if fail.
  1030. */
  1031. static int extcon_alloc_groups(struct extcon_dev *edev)
  1032. {
  1033. int index;
  1034. if (!edev)
  1035. return -EINVAL;
  1036. if (!edev->max_supported)
  1037. return 0;
  1038. edev->extcon_dev_type.groups = kcalloc(edev->max_supported + 2,
  1039. sizeof(*edev->extcon_dev_type.groups),
  1040. GFP_KERNEL);
  1041. if (!edev->extcon_dev_type.groups)
  1042. return -ENOMEM;
  1043. edev->extcon_dev_type.name = dev_name(&edev->dev);
  1044. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  1045. for (index = 0; index < edev->max_supported; index++)
  1046. edev->extcon_dev_type.groups[index] = &edev->cables[index].attr_g;
  1047. if (edev->mutually_exclusive)
  1048. edev->extcon_dev_type.groups[index] = &edev->attr_g_muex;
  1049. edev->dev.type = &edev->extcon_dev_type;
  1050. return 0;
  1051. }
  1052. /**
  1053. * extcon_dev_register() - Register an new extcon device
  1054. * @edev: the extcon device to be registered
  1055. *
  1056. * Among the members of edev struct, please set the "user initializing data"
  1057. * do not set the values of "internal data", which are initialized by
  1058. * this function.
  1059. *
  1060. * Note that before calling this funciton, have to allocate the memory
  1061. * of an extcon device by using the extcon_dev_allocate(). And the extcon
  1062. * dev should include the supported_cable information.
  1063. *
  1064. * Returns 0 if success or error number if fail.
  1065. */
  1066. int extcon_dev_register(struct extcon_dev *edev)
  1067. {
  1068. int ret, index;
  1069. ret = create_extcon_class();
  1070. if (ret < 0)
  1071. return ret;
  1072. if (!edev || !edev->supported_cable)
  1073. return -EINVAL;
  1074. for (index = 0; edev->supported_cable[index] != EXTCON_NONE; index++);
  1075. edev->max_supported = index;
  1076. if (index > SUPPORTED_CABLE_MAX) {
  1077. dev_err(&edev->dev,
  1078. "exceed the maximum number of supported cables\n");
  1079. return -EINVAL;
  1080. }
  1081. edev->dev.class = extcon_class;
  1082. edev->dev.release = extcon_dev_release;
  1083. edev->name = dev_name(edev->dev.parent);
  1084. if (IS_ERR_OR_NULL(edev->name)) {
  1085. dev_err(&edev->dev,
  1086. "extcon device name is null\n");
  1087. return -EINVAL;
  1088. }
  1089. ret = ida_alloc(&extcon_dev_ids, GFP_KERNEL);
  1090. if (ret < 0)
  1091. return ret;
  1092. edev->id = ret;
  1093. ret = extcon_alloc_cables(edev);
  1094. if (ret < 0)
  1095. goto err_alloc_cables;
  1096. ret = extcon_alloc_muex(edev);
  1097. if (ret < 0)
  1098. goto err_alloc_muex;
  1099. ret = extcon_alloc_groups(edev);
  1100. if (ret < 0)
  1101. goto err_alloc_groups;
  1102. spin_lock_init(&edev->lock);
  1103. if (edev->max_supported) {
  1104. edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
  1105. GFP_KERNEL);
  1106. if (!edev->nh) {
  1107. ret = -ENOMEM;
  1108. goto err_alloc_nh;
  1109. }
  1110. }
  1111. for (index = 0; index < edev->max_supported; index++)
  1112. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  1113. RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
  1114. dev_set_drvdata(&edev->dev, edev);
  1115. dev_set_name(&edev->dev, "extcon%d", edev->id);
  1116. edev->state = 0;
  1117. ret = device_register(&edev->dev);
  1118. if (ret) {
  1119. put_device(&edev->dev);
  1120. goto err_dev;
  1121. }
  1122. mutex_lock(&extcon_dev_list_lock);
  1123. list_add(&edev->entry, &extcon_dev_list);
  1124. mutex_unlock(&extcon_dev_list_lock);
  1125. return 0;
  1126. err_dev:
  1127. if (edev->max_supported)
  1128. kfree(edev->nh);
  1129. err_alloc_nh:
  1130. if (edev->max_supported)
  1131. kfree(edev->extcon_dev_type.groups);
  1132. err_alloc_groups:
  1133. if (edev->max_supported && edev->mutually_exclusive) {
  1134. for (index = 0; edev->mutually_exclusive[index]; index++)
  1135. kfree(edev->d_attrs_muex[index].attr.name);
  1136. kfree(edev->d_attrs_muex);
  1137. kfree(edev->attrs_muex);
  1138. }
  1139. err_alloc_muex:
  1140. for (index = 0; index < edev->max_supported; index++)
  1141. kfree(edev->cables[index].attr_g.name);
  1142. if (edev->max_supported)
  1143. kfree(edev->cables);
  1144. err_alloc_cables:
  1145. ida_free(&extcon_dev_ids, edev->id);
  1146. return ret;
  1147. }
  1148. EXPORT_SYMBOL_GPL(extcon_dev_register);
  1149. /**
  1150. * extcon_dev_unregister() - Unregister the extcon device.
  1151. * @edev: the extcon device to be unregistered.
  1152. *
  1153. * Note that this does not call kfree(edev) because edev was not allocated
  1154. * by this class.
  1155. */
  1156. void extcon_dev_unregister(struct extcon_dev *edev)
  1157. {
  1158. int index;
  1159. if (!edev)
  1160. return;
  1161. mutex_lock(&extcon_dev_list_lock);
  1162. list_del(&edev->entry);
  1163. mutex_unlock(&extcon_dev_list_lock);
  1164. if (!get_device(&edev->dev)) {
  1165. dev_err(&edev->dev, "Failed to unregister extcon_dev\n");
  1166. return;
  1167. }
  1168. ida_free(&extcon_dev_ids, edev->id);
  1169. device_unregister(&edev->dev);
  1170. if (edev->mutually_exclusive && edev->max_supported) {
  1171. for (index = 0; edev->mutually_exclusive[index];
  1172. index++)
  1173. kfree(edev->d_attrs_muex[index].attr.name);
  1174. kfree(edev->d_attrs_muex);
  1175. kfree(edev->attrs_muex);
  1176. }
  1177. for (index = 0; index < edev->max_supported; index++)
  1178. kfree(edev->cables[index].attr_g.name);
  1179. if (edev->max_supported) {
  1180. kfree(edev->extcon_dev_type.groups);
  1181. kfree(edev->cables);
  1182. kfree(edev->nh);
  1183. }
  1184. put_device(&edev->dev);
  1185. }
  1186. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  1187. #ifdef CONFIG_OF
  1188. /*
  1189. * extcon_find_edev_by_node - Find the extcon device from devicetree.
  1190. * @node : OF node identifying edev
  1191. *
  1192. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  1193. */
  1194. struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
  1195. {
  1196. struct extcon_dev *edev;
  1197. mutex_lock(&extcon_dev_list_lock);
  1198. list_for_each_entry(edev, &extcon_dev_list, entry)
  1199. if (edev->dev.parent && device_match_of_node(edev->dev.parent, node))
  1200. goto out;
  1201. edev = ERR_PTR(-EPROBE_DEFER);
  1202. out:
  1203. mutex_unlock(&extcon_dev_list_lock);
  1204. return edev;
  1205. }
  1206. /*
  1207. * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
  1208. * @dev : the instance to the given device
  1209. * @index : the index into list of extcon_dev
  1210. *
  1211. * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
  1212. */
  1213. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1214. {
  1215. struct device_node *node, *np = dev_of_node(dev);
  1216. struct extcon_dev *edev;
  1217. if (!np) {
  1218. dev_dbg(dev, "device does not have a device node entry\n");
  1219. return ERR_PTR(-EINVAL);
  1220. }
  1221. node = of_parse_phandle(np, "extcon", index);
  1222. if (!node) {
  1223. dev_dbg(dev, "failed to get phandle in %pOF node\n", np);
  1224. return ERR_PTR(-ENODEV);
  1225. }
  1226. edev = extcon_find_edev_by_node(node);
  1227. of_node_put(node);
  1228. return edev;
  1229. }
  1230. #else
  1231. struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
  1232. {
  1233. return ERR_PTR(-ENOSYS);
  1234. }
  1235. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1236. {
  1237. return ERR_PTR(-ENOSYS);
  1238. }
  1239. #endif /* CONFIG_OF */
  1240. EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
  1241. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  1242. /**
  1243. * extcon_get_edev_name() - Get the name of the extcon device.
  1244. * @edev: the extcon device
  1245. */
  1246. const char *extcon_get_edev_name(struct extcon_dev *edev)
  1247. {
  1248. return !edev ? NULL : edev->name;
  1249. }
  1250. EXPORT_SYMBOL_GPL(extcon_get_edev_name);
  1251. static int __init extcon_class_init(void)
  1252. {
  1253. return create_extcon_class();
  1254. }
  1255. module_init(extcon_class_init);
  1256. static void __exit extcon_class_exit(void)
  1257. {
  1258. class_destroy(extcon_class);
  1259. }
  1260. module_exit(extcon_class_exit);
  1261. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  1262. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1263. MODULE_DESCRIPTION("External Connector (extcon) framework");
  1264. MODULE_LICENSE("GPL v2");