extcon.c 28 KB

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