extcon.c 29 KB

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