extcon.c 29 KB

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