extcon.c 29 KB

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