extcon.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. /*
  2. * drivers/extcon/extcon.c - External Connector (extcon) framework.
  3. *
  4. * External connector (extcon) class driver
  5. *
  6. * Copyright (C) 2015 Samsung Electronics
  7. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  8. *
  9. * Copyright (C) 2012 Samsung Electronics
  10. * Author: Donggeun Kim <dg77.kim@samsung.com>
  11. * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
  12. *
  13. * based on android/drivers/switch/switch_class.c
  14. * Copyright (C) 2008 Google, Inc.
  15. * Author: Mike Lockwood <lockwood@android.com>
  16. *
  17. * This software is licensed under the terms of the GNU General Public
  18. * License version 2, as published by the Free Software Foundation, and
  19. * may be copied, distributed, and modified under those terms.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/init.h>
  29. #include <linux/device.h>
  30. #include <linux/fs.h>
  31. #include <linux/err.h>
  32. #include <linux/extcon.h>
  33. #include <linux/of.h>
  34. #include <linux/slab.h>
  35. #include <linux/sysfs.h>
  36. #define SUPPORTED_CABLE_MAX 32
  37. #define CABLE_NAME_MAX 30
  38. static const char *extcon_name[] = {
  39. [EXTCON_NONE] = "NONE",
  40. /* USB external connector */
  41. [EXTCON_USB] = "USB",
  42. [EXTCON_USB_HOST] = "USB-HOST",
  43. /* Charging external connector */
  44. [EXTCON_CHG_USB_SDP] = "SDP",
  45. [EXTCON_CHG_USB_DCP] = "DCP",
  46. [EXTCON_CHG_USB_CDP] = "CDP",
  47. [EXTCON_CHG_USB_ACA] = "ACA",
  48. [EXTCON_CHG_USB_FAST] = "FAST-CHARGER",
  49. [EXTCON_CHG_USB_SLOW] = "SLOW-CHARGER",
  50. /* Jack external connector */
  51. [EXTCON_JACK_MICROPHONE] = "MICROPHONE",
  52. [EXTCON_JACK_HEADPHONE] = "HEADPHONE",
  53. [EXTCON_JACK_LINE_IN] = "LINE-IN",
  54. [EXTCON_JACK_LINE_OUT] = "LINE-OUT",
  55. [EXTCON_JACK_VIDEO_IN] = "VIDEO-IN",
  56. [EXTCON_JACK_VIDEO_OUT] = "VIDEO-OUT",
  57. [EXTCON_JACK_SPDIF_IN] = "SPDIF-IN",
  58. [EXTCON_JACK_SPDIF_OUT] = "SPDIF-OUT",
  59. /* Display external connector */
  60. [EXTCON_DISP_HDMI] = "HDMI",
  61. [EXTCON_DISP_MHL] = "MHL",
  62. [EXTCON_DISP_DVI] = "DVI",
  63. [EXTCON_DISP_VGA] = "VGA",
  64. /* Miscellaneous external connector */
  65. [EXTCON_DOCK] = "DOCK",
  66. [EXTCON_JIG] = "JIG",
  67. [EXTCON_MECHANICAL] = "MECHANICAL",
  68. NULL,
  69. };
  70. static struct class *extcon_class;
  71. #if defined(CONFIG_ANDROID)
  72. static struct class_compat *switch_class;
  73. #endif /* CONFIG_ANDROID */
  74. static LIST_HEAD(extcon_dev_list);
  75. static DEFINE_MUTEX(extcon_dev_list_lock);
  76. /**
  77. * check_mutually_exclusive - Check if new_state violates mutually_exclusive
  78. * condition.
  79. * @edev: the extcon device
  80. * @new_state: new cable attach status for @edev
  81. *
  82. * Returns 0 if nothing violates. Returns the index + 1 for the first
  83. * violated condition.
  84. */
  85. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  86. {
  87. int i = 0;
  88. if (!edev->mutually_exclusive)
  89. return 0;
  90. for (i = 0; edev->mutually_exclusive[i]; i++) {
  91. int weight;
  92. u32 correspondants = new_state & edev->mutually_exclusive[i];
  93. /* calculate the total number of bits set */
  94. weight = hweight32(correspondants);
  95. if (weight > 1)
  96. return i + 1;
  97. }
  98. return 0;
  99. }
  100. static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
  101. {
  102. int i;
  103. /* Find the the index of extcon cable in edev->supported_cable */
  104. for (i = 0; i < edev->max_supported; i++) {
  105. if (edev->supported_cable[i] == id)
  106. return i;
  107. }
  108. return -EINVAL;
  109. }
  110. static int find_cable_id_by_name(struct extcon_dev *edev, const char *name)
  111. {
  112. int id = -EINVAL;
  113. int i = 0;
  114. /* Find the id of extcon cable */
  115. while (extcon_name[i]) {
  116. if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
  117. id = i;
  118. break;
  119. }
  120. i++;
  121. }
  122. return id;
  123. }
  124. static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
  125. {
  126. int id;
  127. if (edev->max_supported == 0)
  128. return -EINVAL;
  129. /* Find the the number of extcon cable */
  130. id = find_cable_id_by_name(edev, name);
  131. if (id < 0)
  132. return id;
  133. return find_cable_index_by_id(edev, id);
  134. }
  135. static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
  136. {
  137. if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
  138. *attached = ((new >> idx) & 0x1) ? true : false;
  139. return true;
  140. }
  141. return false;
  142. }
  143. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  144. char *buf)
  145. {
  146. int i, count = 0;
  147. struct extcon_dev *edev = dev_get_drvdata(dev);
  148. if (edev->max_supported == 0)
  149. return sprintf(buf, "%u\n", edev->state);
  150. for (i = 0; i < edev->max_supported; i++) {
  151. count += sprintf(buf + count, "%s=%d\n",
  152. extcon_name[edev->supported_cable[i]],
  153. !!(edev->state & (1 << i)));
  154. }
  155. return count;
  156. }
  157. static ssize_t state_store(struct device *dev, struct device_attribute *attr,
  158. const char *buf, size_t count)
  159. {
  160. u32 state;
  161. ssize_t ret = 0;
  162. struct extcon_dev *edev = dev_get_drvdata(dev);
  163. ret = sscanf(buf, "0x%x", &state);
  164. if (ret == 0)
  165. ret = -EINVAL;
  166. else
  167. ret = extcon_set_state(edev, state);
  168. if (ret < 0)
  169. return ret;
  170. return count;
  171. }
  172. static DEVICE_ATTR_RW(state);
  173. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  174. char *buf)
  175. {
  176. struct extcon_dev *edev = dev_get_drvdata(dev);
  177. return sprintf(buf, "%s\n", edev->name);
  178. }
  179. static DEVICE_ATTR_RO(name);
  180. static ssize_t cable_name_show(struct device *dev,
  181. struct device_attribute *attr, char *buf)
  182. {
  183. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  184. attr_name);
  185. int i = cable->cable_index;
  186. return sprintf(buf, "%s\n",
  187. extcon_name[cable->edev->supported_cable[i]]);
  188. }
  189. static ssize_t cable_state_show(struct device *dev,
  190. struct device_attribute *attr, char *buf)
  191. {
  192. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  193. attr_state);
  194. int i = cable->cable_index;
  195. return sprintf(buf, "%d\n",
  196. extcon_get_cable_state_(cable->edev,
  197. cable->edev->supported_cable[i]));
  198. }
  199. /**
  200. * extcon_update_state() - Update the cable attach states of the extcon device
  201. * only for the masked bits.
  202. * @edev: the extcon device
  203. * @mask: the bit mask to designate updated bits.
  204. * @state: new cable attach status for @edev
  205. *
  206. * Changing the state sends uevent with environment variable containing
  207. * the name of extcon device (envp[0]) and the state output (envp[1]).
  208. * Tizen uses this format for extcon device to get events from ports.
  209. * Android uses this format as well.
  210. *
  211. * Note that the notifier provides which bits are changed in the state
  212. * variable with the val parameter (second) to the callback.
  213. */
  214. int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
  215. {
  216. char name_buf[120];
  217. char state_buf[120];
  218. char *prop_buf;
  219. char *envp[3];
  220. int env_offset = 0;
  221. int length;
  222. int index;
  223. unsigned long flags;
  224. bool attached;
  225. if (!edev)
  226. return -EINVAL;
  227. spin_lock_irqsave(&edev->lock, flags);
  228. if (edev->state != ((edev->state & ~mask) | (state & mask))) {
  229. u32 old_state;
  230. if (check_mutually_exclusive(edev, (edev->state & ~mask) |
  231. (state & mask))) {
  232. spin_unlock_irqrestore(&edev->lock, flags);
  233. return -EPERM;
  234. }
  235. old_state = edev->state;
  236. edev->state &= ~mask;
  237. edev->state |= state & mask;
  238. for (index = 0; index < edev->max_supported; index++) {
  239. if (is_extcon_changed(old_state, edev->state, index,
  240. &attached))
  241. raw_notifier_call_chain(&edev->nh[index],
  242. attached, edev);
  243. }
  244. /* This could be in interrupt handler */
  245. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  246. if (prop_buf) {
  247. length = name_show(&edev->dev, NULL, prop_buf);
  248. if (length > 0) {
  249. if (prop_buf[length - 1] == '\n')
  250. prop_buf[length - 1] = 0;
  251. snprintf(name_buf, sizeof(name_buf),
  252. "NAME=%s", prop_buf);
  253. envp[env_offset++] = name_buf;
  254. }
  255. length = state_show(&edev->dev, NULL, prop_buf);
  256. if (length > 0) {
  257. if (prop_buf[length - 1] == '\n')
  258. prop_buf[length - 1] = 0;
  259. snprintf(state_buf, sizeof(state_buf),
  260. "STATE=%s", prop_buf);
  261. envp[env_offset++] = state_buf;
  262. }
  263. envp[env_offset] = NULL;
  264. /* Unlock early before uevent */
  265. spin_unlock_irqrestore(&edev->lock, flags);
  266. kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
  267. free_page((unsigned long)prop_buf);
  268. } else {
  269. /* Unlock early before uevent */
  270. spin_unlock_irqrestore(&edev->lock, flags);
  271. dev_err(&edev->dev, "out of memory in extcon_set_state\n");
  272. kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
  273. }
  274. } else {
  275. /* No changes */
  276. spin_unlock_irqrestore(&edev->lock, flags);
  277. }
  278. return 0;
  279. }
  280. EXPORT_SYMBOL_GPL(extcon_update_state);
  281. /**
  282. * extcon_set_state() - Set the cable attach states of the extcon device.
  283. * @edev: the extcon device
  284. * @state: new cable attach status for @edev
  285. *
  286. * Note that notifier provides which bits are changed in the state
  287. * variable with the val parameter (second) to the callback.
  288. */
  289. int extcon_set_state(struct extcon_dev *edev, u32 state)
  290. {
  291. if (!edev)
  292. return -EINVAL;
  293. return extcon_update_state(edev, 0xffffffff, state);
  294. }
  295. EXPORT_SYMBOL_GPL(extcon_set_state);
  296. /**
  297. * extcon_get_cable_state_() - Get the status of a specific cable.
  298. * @edev: the extcon device that has the cable.
  299. * @id: the unique id of each external connector in extcon enumeration.
  300. */
  301. int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
  302. {
  303. int index;
  304. if (!edev)
  305. return -EINVAL;
  306. index = find_cable_index_by_id(edev, id);
  307. if (index < 0)
  308. return index;
  309. if (edev->max_supported && edev->max_supported <= index)
  310. return -EINVAL;
  311. return !!(edev->state & (1 << index));
  312. }
  313. EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
  314. /**
  315. * extcon_get_cable_state() - Get the status of a specific cable.
  316. * @edev: the extcon device that has the cable.
  317. * @cable_name: cable name.
  318. *
  319. * Note that this is slower than extcon_get_cable_state_.
  320. */
  321. int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
  322. {
  323. int id;
  324. id = find_cable_id_by_name(edev, cable_name);
  325. if (id < 0)
  326. return id;
  327. return extcon_get_cable_state_(edev, id);
  328. }
  329. EXPORT_SYMBOL_GPL(extcon_get_cable_state);
  330. /**
  331. * extcon_set_cable_state_() - Set the status of a specific cable.
  332. * @edev: the extcon device that has the cable.
  333. * @id: the unique id of each external connector
  334. * in extcon enumeration.
  335. * @state: the new cable status. The default semantics is
  336. * true: attached / false: detached.
  337. */
  338. int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
  339. bool cable_state)
  340. {
  341. u32 state;
  342. int index;
  343. if (!edev)
  344. return -EINVAL;
  345. index = find_cable_index_by_id(edev, id);
  346. if (index < 0)
  347. return index;
  348. if (edev->max_supported && edev->max_supported <= index)
  349. return -EINVAL;
  350. state = cable_state ? (1 << index) : 0;
  351. return extcon_update_state(edev, 1 << index, state);
  352. }
  353. EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
  354. /**
  355. * extcon_set_cable_state() - Set the status of a specific cable.
  356. * @edev: the extcon device that has the cable.
  357. * @cable_name: cable name.
  358. * @cable_state: the new cable status. The default semantics is
  359. * true: attached / false: detached.
  360. *
  361. * Note that this is slower than extcon_set_cable_state_.
  362. */
  363. int extcon_set_cable_state(struct extcon_dev *edev,
  364. const char *cable_name, bool cable_state)
  365. {
  366. int id;
  367. id = find_cable_id_by_name(edev, cable_name);
  368. if (id < 0)
  369. return id;
  370. return extcon_set_cable_state_(edev, id, cable_state);
  371. }
  372. EXPORT_SYMBOL_GPL(extcon_set_cable_state);
  373. /**
  374. * extcon_get_extcon_dev() - Get the extcon device instance from the name
  375. * @extcon_name: The extcon name provided with extcon_dev_register()
  376. */
  377. struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
  378. {
  379. struct extcon_dev *sd;
  380. if (!extcon_name)
  381. return ERR_PTR(-EINVAL);
  382. mutex_lock(&extcon_dev_list_lock);
  383. list_for_each_entry(sd, &extcon_dev_list, entry) {
  384. if (!strcmp(sd->name, extcon_name))
  385. goto out;
  386. }
  387. sd = NULL;
  388. out:
  389. mutex_unlock(&extcon_dev_list_lock);
  390. return sd;
  391. }
  392. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  393. /**
  394. * extcon_register_interest() - Register a notifier for a state change of a
  395. * specific cable, not an entier set of cables of a
  396. * extcon device.
  397. * @obj: an empty extcon_specific_cable_nb object to be returned.
  398. * @extcon_name: the name of extcon device.
  399. * if NULL, extcon_register_interest will register
  400. * every cable with the target cable_name given.
  401. * @cable_name: the target cable name.
  402. * @nb: the notifier block to get notified.
  403. *
  404. * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
  405. * the struct for you.
  406. *
  407. * extcon_register_interest is a helper function for those who want to get
  408. * notification for a single specific cable's status change. If a user wants
  409. * to get notification for any changes of all cables of a extcon device,
  410. * he/she should use the general extcon_register_notifier().
  411. *
  412. * Note that the second parameter given to the callback of nb (val) is
  413. * "old_state", not the current state. The current state can be retrieved
  414. * by looking at the third pameter (edev pointer)'s state value.
  415. */
  416. int extcon_register_interest(struct extcon_specific_cable_nb *obj,
  417. const char *extcon_name, const char *cable_name,
  418. struct notifier_block *nb)
  419. {
  420. unsigned long flags;
  421. int ret;
  422. if (!obj || !cable_name || !nb)
  423. return -EINVAL;
  424. if (extcon_name) {
  425. obj->edev = extcon_get_extcon_dev(extcon_name);
  426. if (!obj->edev)
  427. return -ENODEV;
  428. obj->cable_index = find_cable_index_by_name(obj->edev,
  429. cable_name);
  430. if (obj->cable_index < 0)
  431. return obj->cable_index;
  432. obj->user_nb = nb;
  433. spin_lock_irqsave(&obj->edev->lock, flags);
  434. ret = raw_notifier_chain_register(
  435. &obj->edev->nh[obj->cable_index],
  436. obj->user_nb);
  437. spin_unlock_irqrestore(&obj->edev->lock, flags);
  438. } else {
  439. struct class_dev_iter iter;
  440. struct extcon_dev *extd;
  441. struct device *dev;
  442. if (!extcon_class)
  443. return -ENODEV;
  444. class_dev_iter_init(&iter, extcon_class, NULL, NULL);
  445. while ((dev = class_dev_iter_next(&iter))) {
  446. extd = dev_get_drvdata(dev);
  447. if (find_cable_index_by_name(extd, cable_name) < 0)
  448. continue;
  449. class_dev_iter_exit(&iter);
  450. return extcon_register_interest(obj, extd->name,
  451. cable_name, nb);
  452. }
  453. ret = -ENODEV;
  454. }
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(extcon_register_interest);
  458. /**
  459. * extcon_unregister_interest() - Unregister the notifier registered by
  460. * extcon_register_interest().
  461. * @obj: the extcon_specific_cable_nb object returned by
  462. * extcon_register_interest().
  463. */
  464. int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
  465. {
  466. unsigned long flags;
  467. int ret;
  468. if (!obj)
  469. return -EINVAL;
  470. spin_lock_irqsave(&obj->edev->lock, flags);
  471. ret = raw_notifier_chain_unregister(
  472. &obj->edev->nh[obj->cable_index], obj->user_nb);
  473. spin_unlock_irqrestore(&obj->edev->lock, flags);
  474. return ret;
  475. }
  476. EXPORT_SYMBOL_GPL(extcon_unregister_interest);
  477. /**
  478. * extcon_register_notifier() - Register a notifiee to get notified by
  479. * any attach status changes from the extcon.
  480. * @edev: the extcon device that has the external connecotr.
  481. * @id: the unique id of each external connector in extcon enumeration.
  482. * @nb: a notifier block to be registered.
  483. *
  484. * Note that the second parameter given to the callback of nb (val) is
  485. * "old_state", not the current state. The current state can be retrieved
  486. * by looking at the third pameter (edev pointer)'s state value.
  487. */
  488. int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
  489. struct notifier_block *nb)
  490. {
  491. unsigned long flags;
  492. int ret, idx;
  493. if (!edev || !nb)
  494. return -EINVAL;
  495. idx = find_cable_index_by_id(edev, id);
  496. spin_lock_irqsave(&edev->lock, flags);
  497. ret = raw_notifier_chain_register(&edev->nh[idx], nb);
  498. spin_unlock_irqrestore(&edev->lock, flags);
  499. return ret;
  500. }
  501. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  502. /**
  503. * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
  504. * @edev: the extcon device that has the external connecotr.
  505. * @id: the unique id of each external connector in extcon enumeration.
  506. * @nb: a notifier block to be registered.
  507. */
  508. int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
  509. struct notifier_block *nb)
  510. {
  511. unsigned long flags;
  512. int ret, idx;
  513. if (!edev || !nb)
  514. return -EINVAL;
  515. idx = find_cable_index_by_id(edev, id);
  516. spin_lock_irqsave(&edev->lock, flags);
  517. ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
  518. spin_unlock_irqrestore(&edev->lock, flags);
  519. return ret;
  520. }
  521. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  522. static struct attribute *extcon_attrs[] = {
  523. &dev_attr_state.attr,
  524. &dev_attr_name.attr,
  525. NULL,
  526. };
  527. ATTRIBUTE_GROUPS(extcon);
  528. static int create_extcon_class(void)
  529. {
  530. if (!extcon_class) {
  531. extcon_class = class_create(THIS_MODULE, "extcon");
  532. if (IS_ERR(extcon_class))
  533. return PTR_ERR(extcon_class);
  534. extcon_class->dev_groups = extcon_groups;
  535. #if defined(CONFIG_ANDROID)
  536. switch_class = class_compat_register("switch");
  537. if (WARN(!switch_class, "cannot allocate"))
  538. return -ENOMEM;
  539. #endif /* CONFIG_ANDROID */
  540. }
  541. return 0;
  542. }
  543. static void extcon_dev_release(struct device *dev)
  544. {
  545. }
  546. static const char *muex_name = "mutually_exclusive";
  547. static void dummy_sysfs_dev_release(struct device *dev)
  548. {
  549. }
  550. /*
  551. * extcon_dev_allocate() - Allocate the memory of extcon device.
  552. * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
  553. * If supported_cable is NULL, cable name related APIs
  554. * are disabled.
  555. *
  556. * This function allocates the memory for extcon device without allocating
  557. * memory in each extcon provider driver and initialize default setting for
  558. * extcon device.
  559. *
  560. * Return the pointer of extcon device if success or ERR_PTR(err) if fail
  561. */
  562. struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
  563. {
  564. struct extcon_dev *edev;
  565. if (!supported_cable)
  566. return ERR_PTR(-EINVAL);
  567. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  568. if (!edev)
  569. return ERR_PTR(-ENOMEM);
  570. edev->max_supported = 0;
  571. edev->supported_cable = supported_cable;
  572. return edev;
  573. }
  574. /*
  575. * extcon_dev_free() - Free the memory of extcon device.
  576. * @edev: the extcon device to free
  577. */
  578. void extcon_dev_free(struct extcon_dev *edev)
  579. {
  580. kfree(edev);
  581. }
  582. EXPORT_SYMBOL_GPL(extcon_dev_free);
  583. static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
  584. {
  585. struct extcon_dev **r = res;
  586. if (WARN_ON(!r || !*r))
  587. return 0;
  588. return *r == data;
  589. }
  590. static void devm_extcon_dev_release(struct device *dev, void *res)
  591. {
  592. extcon_dev_free(*(struct extcon_dev **)res);
  593. }
  594. /**
  595. * devm_extcon_dev_allocate - Allocate managed extcon device
  596. * @dev: device owning the extcon device being created
  597. * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
  598. * If supported_cable is NULL, cable name related APIs
  599. * are disabled.
  600. *
  601. * This function manages automatically the memory of extcon device using device
  602. * resource management and simplify the control of freeing the memory of extcon
  603. * device.
  604. *
  605. * Returns the pointer memory of allocated extcon_dev if success
  606. * or ERR_PTR(err) if fail
  607. */
  608. struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
  609. const unsigned int *supported_cable)
  610. {
  611. struct extcon_dev **ptr, *edev;
  612. ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
  613. if (!ptr)
  614. return ERR_PTR(-ENOMEM);
  615. edev = extcon_dev_allocate(supported_cable);
  616. if (IS_ERR(edev)) {
  617. devres_free(ptr);
  618. return edev;
  619. }
  620. edev->dev.parent = dev;
  621. *ptr = edev;
  622. devres_add(dev, ptr);
  623. return edev;
  624. }
  625. EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
  626. void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
  627. {
  628. WARN_ON(devres_release(dev, devm_extcon_dev_release,
  629. devm_extcon_dev_match, edev));
  630. }
  631. EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
  632. /**
  633. * extcon_dev_register() - Register a new extcon device
  634. * @edev : the new extcon device (should be allocated before calling)
  635. *
  636. * Among the members of edev struct, please set the "user initializing data"
  637. * in any case and set the "optional callbacks" if required. However, please
  638. * do not set the values of "internal data", which are initialized by
  639. * this function.
  640. */
  641. int extcon_dev_register(struct extcon_dev *edev)
  642. {
  643. int ret, index = 0;
  644. static atomic_t edev_no = ATOMIC_INIT(-1);
  645. if (!extcon_class) {
  646. ret = create_extcon_class();
  647. if (ret < 0)
  648. return ret;
  649. }
  650. if (!edev || !edev->supported_cable)
  651. return -EINVAL;
  652. for (; edev->supported_cable[index] != EXTCON_NONE; index++);
  653. edev->max_supported = index;
  654. if (index > SUPPORTED_CABLE_MAX) {
  655. dev_err(&edev->dev,
  656. "exceed the maximum number of supported cables\n");
  657. return -EINVAL;
  658. }
  659. edev->dev.class = extcon_class;
  660. edev->dev.release = extcon_dev_release;
  661. edev->name = dev_name(edev->dev.parent);
  662. if (IS_ERR_OR_NULL(edev->name)) {
  663. dev_err(&edev->dev,
  664. "extcon device name is null\n");
  665. return -EINVAL;
  666. }
  667. dev_set_name(&edev->dev, "extcon%lu",
  668. (unsigned long)atomic_inc_return(&edev_no));
  669. if (edev->max_supported) {
  670. char buf[10];
  671. char *str;
  672. struct extcon_cable *cable;
  673. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  674. edev->max_supported, GFP_KERNEL);
  675. if (!edev->cables) {
  676. ret = -ENOMEM;
  677. goto err_sysfs_alloc;
  678. }
  679. for (index = 0; index < edev->max_supported; index++) {
  680. cable = &edev->cables[index];
  681. snprintf(buf, 10, "cable.%d", index);
  682. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  683. GFP_KERNEL);
  684. if (!str) {
  685. for (index--; index >= 0; index--) {
  686. cable = &edev->cables[index];
  687. kfree(cable->attr_g.name);
  688. }
  689. ret = -ENOMEM;
  690. goto err_alloc_cables;
  691. }
  692. strcpy(str, buf);
  693. cable->edev = edev;
  694. cable->cable_index = index;
  695. cable->attrs[0] = &cable->attr_name.attr;
  696. cable->attrs[1] = &cable->attr_state.attr;
  697. cable->attrs[2] = NULL;
  698. cable->attr_g.name = str;
  699. cable->attr_g.attrs = cable->attrs;
  700. sysfs_attr_init(&cable->attr_name.attr);
  701. cable->attr_name.attr.name = "name";
  702. cable->attr_name.attr.mode = 0444;
  703. cable->attr_name.show = cable_name_show;
  704. sysfs_attr_init(&cable->attr_state.attr);
  705. cable->attr_state.attr.name = "state";
  706. cable->attr_state.attr.mode = 0444;
  707. cable->attr_state.show = cable_state_show;
  708. }
  709. }
  710. if (edev->max_supported && edev->mutually_exclusive) {
  711. char buf[80];
  712. char *name;
  713. /* Count the size of mutually_exclusive array */
  714. for (index = 0; edev->mutually_exclusive[index]; index++)
  715. ;
  716. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  717. (index + 1), GFP_KERNEL);
  718. if (!edev->attrs_muex) {
  719. ret = -ENOMEM;
  720. goto err_muex;
  721. }
  722. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  723. index, GFP_KERNEL);
  724. if (!edev->d_attrs_muex) {
  725. ret = -ENOMEM;
  726. kfree(edev->attrs_muex);
  727. goto err_muex;
  728. }
  729. for (index = 0; edev->mutually_exclusive[index]; index++) {
  730. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  731. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  732. GFP_KERNEL);
  733. if (!name) {
  734. for (index--; index >= 0; index--) {
  735. kfree(edev->d_attrs_muex[index].attr.
  736. name);
  737. }
  738. kfree(edev->d_attrs_muex);
  739. kfree(edev->attrs_muex);
  740. ret = -ENOMEM;
  741. goto err_muex;
  742. }
  743. strcpy(name, buf);
  744. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  745. edev->d_attrs_muex[index].attr.name = name;
  746. edev->d_attrs_muex[index].attr.mode = 0000;
  747. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  748. .attr;
  749. }
  750. edev->attr_g_muex.name = muex_name;
  751. edev->attr_g_muex.attrs = edev->attrs_muex;
  752. }
  753. if (edev->max_supported) {
  754. edev->extcon_dev_type.groups =
  755. kzalloc(sizeof(struct attribute_group *) *
  756. (edev->max_supported + 2), GFP_KERNEL);
  757. if (!edev->extcon_dev_type.groups) {
  758. ret = -ENOMEM;
  759. goto err_alloc_groups;
  760. }
  761. edev->extcon_dev_type.name = dev_name(&edev->dev);
  762. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  763. for (index = 0; index < edev->max_supported; index++)
  764. edev->extcon_dev_type.groups[index] =
  765. &edev->cables[index].attr_g;
  766. if (edev->mutually_exclusive)
  767. edev->extcon_dev_type.groups[index] =
  768. &edev->attr_g_muex;
  769. edev->dev.type = &edev->extcon_dev_type;
  770. }
  771. ret = device_register(&edev->dev);
  772. if (ret) {
  773. put_device(&edev->dev);
  774. goto err_dev;
  775. }
  776. #if defined(CONFIG_ANDROID)
  777. if (switch_class)
  778. ret = class_compat_create_link(switch_class, &edev->dev, NULL);
  779. #endif /* CONFIG_ANDROID */
  780. spin_lock_init(&edev->lock);
  781. edev->nh = devm_kzalloc(&edev->dev,
  782. sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
  783. if (!edev->nh) {
  784. ret = -ENOMEM;
  785. goto err_dev;
  786. }
  787. for (index = 0; index < edev->max_supported; index++)
  788. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  789. dev_set_drvdata(&edev->dev, edev);
  790. edev->state = 0;
  791. mutex_lock(&extcon_dev_list_lock);
  792. list_add(&edev->entry, &extcon_dev_list);
  793. mutex_unlock(&extcon_dev_list_lock);
  794. return 0;
  795. err_dev:
  796. if (edev->max_supported)
  797. kfree(edev->extcon_dev_type.groups);
  798. err_alloc_groups:
  799. if (edev->max_supported && edev->mutually_exclusive) {
  800. for (index = 0; edev->mutually_exclusive[index]; index++)
  801. kfree(edev->d_attrs_muex[index].attr.name);
  802. kfree(edev->d_attrs_muex);
  803. kfree(edev->attrs_muex);
  804. }
  805. err_muex:
  806. for (index = 0; index < edev->max_supported; index++)
  807. kfree(edev->cables[index].attr_g.name);
  808. err_alloc_cables:
  809. if (edev->max_supported)
  810. kfree(edev->cables);
  811. err_sysfs_alloc:
  812. return ret;
  813. }
  814. EXPORT_SYMBOL_GPL(extcon_dev_register);
  815. /**
  816. * extcon_dev_unregister() - Unregister the extcon device.
  817. * @edev: the extcon device instance to be unregistered.
  818. *
  819. * Note that this does not call kfree(edev) because edev was not allocated
  820. * by this class.
  821. */
  822. void extcon_dev_unregister(struct extcon_dev *edev)
  823. {
  824. int index;
  825. if (!edev)
  826. return;
  827. mutex_lock(&extcon_dev_list_lock);
  828. list_del(&edev->entry);
  829. mutex_unlock(&extcon_dev_list_lock);
  830. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  831. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  832. dev_name(&edev->dev));
  833. return;
  834. }
  835. device_unregister(&edev->dev);
  836. if (edev->mutually_exclusive && edev->max_supported) {
  837. for (index = 0; edev->mutually_exclusive[index];
  838. index++)
  839. kfree(edev->d_attrs_muex[index].attr.name);
  840. kfree(edev->d_attrs_muex);
  841. kfree(edev->attrs_muex);
  842. }
  843. for (index = 0; index < edev->max_supported; index++)
  844. kfree(edev->cables[index].attr_g.name);
  845. if (edev->max_supported) {
  846. kfree(edev->extcon_dev_type.groups);
  847. kfree(edev->cables);
  848. }
  849. #if defined(CONFIG_ANDROID)
  850. if (switch_class)
  851. class_compat_remove_link(switch_class, &edev->dev, NULL);
  852. #endif
  853. put_device(&edev->dev);
  854. }
  855. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  856. static void devm_extcon_dev_unreg(struct device *dev, void *res)
  857. {
  858. extcon_dev_unregister(*(struct extcon_dev **)res);
  859. }
  860. /**
  861. * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
  862. * @dev: device to allocate extcon device
  863. * @edev: the new extcon device to register
  864. *
  865. * Managed extcon_dev_register() function. If extcon device is attached with
  866. * this function, that extcon device is automatically unregistered on driver
  867. * detach. Internally this function calls extcon_dev_register() function.
  868. * To get more information, refer that function.
  869. *
  870. * If extcon device is registered with this function and the device needs to be
  871. * unregistered separately, devm_extcon_dev_unregister() should be used.
  872. *
  873. * Returns 0 if success or negaive error number if failure.
  874. */
  875. int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
  876. {
  877. struct extcon_dev **ptr;
  878. int ret;
  879. ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
  880. if (!ptr)
  881. return -ENOMEM;
  882. ret = extcon_dev_register(edev);
  883. if (ret) {
  884. devres_free(ptr);
  885. return ret;
  886. }
  887. *ptr = edev;
  888. devres_add(dev, ptr);
  889. return 0;
  890. }
  891. EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
  892. /**
  893. * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
  894. * @dev: device the extcon belongs to
  895. * @edev: the extcon device to unregister
  896. *
  897. * Unregister extcon device that is registered with devm_extcon_dev_register()
  898. * function.
  899. */
  900. void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
  901. {
  902. WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
  903. devm_extcon_dev_match, edev));
  904. }
  905. EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
  906. #ifdef CONFIG_OF
  907. /*
  908. * extcon_get_edev_by_phandle - Get the extcon device from devicetree
  909. * @dev - instance to the given device
  910. * @index - index into list of extcon_dev
  911. *
  912. * return the instance of extcon device
  913. */
  914. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  915. {
  916. struct device_node *node;
  917. struct extcon_dev *edev;
  918. if (!dev)
  919. return ERR_PTR(-EINVAL);
  920. if (!dev->of_node) {
  921. dev_err(dev, "device does not have a device node entry\n");
  922. return ERR_PTR(-EINVAL);
  923. }
  924. node = of_parse_phandle(dev->of_node, "extcon", index);
  925. if (!node) {
  926. dev_err(dev, "failed to get phandle in %s node\n",
  927. dev->of_node->full_name);
  928. return ERR_PTR(-ENODEV);
  929. }
  930. mutex_lock(&extcon_dev_list_lock);
  931. list_for_each_entry(edev, &extcon_dev_list, entry) {
  932. if (edev->dev.parent && edev->dev.parent->of_node == node) {
  933. mutex_unlock(&extcon_dev_list_lock);
  934. return edev;
  935. }
  936. }
  937. mutex_unlock(&extcon_dev_list_lock);
  938. return ERR_PTR(-EPROBE_DEFER);
  939. }
  940. #else
  941. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  942. {
  943. return ERR_PTR(-ENOSYS);
  944. }
  945. #endif /* CONFIG_OF */
  946. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  947. /**
  948. * extcon_get_edev_name() - Get the name of the extcon device.
  949. * @edev: the extcon device
  950. */
  951. const char *extcon_get_edev_name(struct extcon_dev *edev)
  952. {
  953. return !edev ? NULL : edev->name;
  954. }
  955. static int __init extcon_class_init(void)
  956. {
  957. return create_extcon_class();
  958. }
  959. module_init(extcon_class_init);
  960. static void __exit extcon_class_exit(void)
  961. {
  962. #if defined(CONFIG_ANDROID)
  963. class_compat_unregister(switch_class);
  964. #endif
  965. class_destroy(extcon_class);
  966. }
  967. module_exit(extcon_class_exit);
  968. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  969. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  970. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  971. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  972. MODULE_DESCRIPTION("External connector (extcon) class driver");
  973. MODULE_LICENSE("GPL");