extcon.c 28 KB

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