extcon-class.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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/slab.h>
  32. #include <linux/sysfs.h>
  33. #include <linux/of.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. if (!obj || !cable_name || !nb)
  393. return -EINVAL;
  394. if (extcon_name) {
  395. obj->edev = extcon_get_extcon_dev(extcon_name);
  396. if (!obj->edev)
  397. return -ENODEV;
  398. obj->cable_index = extcon_find_cable_index(obj->edev,
  399. cable_name);
  400. if (obj->cable_index < 0)
  401. return obj->cable_index;
  402. obj->user_nb = nb;
  403. obj->internal_nb.notifier_call = _call_per_cable;
  404. return raw_notifier_chain_register(&obj->edev->nh,
  405. &obj->internal_nb);
  406. } else {
  407. struct class_dev_iter iter;
  408. struct extcon_dev *extd;
  409. struct device *dev;
  410. if (!extcon_class)
  411. return -ENODEV;
  412. class_dev_iter_init(&iter, extcon_class, NULL, NULL);
  413. while ((dev = class_dev_iter_next(&iter))) {
  414. extd = dev_get_drvdata(dev);
  415. if (extcon_find_cable_index(extd, cable_name) < 0)
  416. continue;
  417. class_dev_iter_exit(&iter);
  418. return extcon_register_interest(obj, extd->name,
  419. cable_name, nb);
  420. }
  421. return -ENODEV;
  422. }
  423. }
  424. EXPORT_SYMBOL_GPL(extcon_register_interest);
  425. /**
  426. * extcon_unregister_interest() - Unregister the notifier registered by
  427. * extcon_register_interest().
  428. * @obj: the extcon_specific_cable_nb object returned by
  429. * extcon_register_interest().
  430. */
  431. int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
  432. {
  433. if (!obj)
  434. return -EINVAL;
  435. return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
  436. }
  437. EXPORT_SYMBOL_GPL(extcon_unregister_interest);
  438. /**
  439. * extcon_register_notifier() - Register a notifiee to get notified by
  440. * any attach status changes from the extcon.
  441. * @edev: the extcon device.
  442. * @nb: a notifier block to be registered.
  443. *
  444. * Note that the second parameter given to the callback of nb (val) is
  445. * "old_state", not the current state. The current state can be retrieved
  446. * by looking at the third pameter (edev pointer)'s state value.
  447. */
  448. int extcon_register_notifier(struct extcon_dev *edev,
  449. struct notifier_block *nb)
  450. {
  451. return raw_notifier_chain_register(&edev->nh, nb);
  452. }
  453. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  454. /**
  455. * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
  456. * @edev: the extcon device.
  457. * @nb: a registered notifier block to be unregistered.
  458. */
  459. int extcon_unregister_notifier(struct extcon_dev *edev,
  460. struct notifier_block *nb)
  461. {
  462. return raw_notifier_chain_unregister(&edev->nh, nb);
  463. }
  464. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  465. static struct attribute *extcon_attrs[] = {
  466. &dev_attr_state.attr,
  467. &dev_attr_name.attr,
  468. NULL,
  469. };
  470. ATTRIBUTE_GROUPS(extcon);
  471. static int create_extcon_class(void)
  472. {
  473. if (!extcon_class) {
  474. extcon_class = class_create(THIS_MODULE, "extcon");
  475. if (IS_ERR(extcon_class))
  476. return PTR_ERR(extcon_class);
  477. extcon_class->dev_groups = extcon_groups;
  478. #if defined(CONFIG_ANDROID)
  479. switch_class = class_compat_register("switch");
  480. if (WARN(!switch_class, "cannot allocate"))
  481. return -ENOMEM;
  482. #endif /* CONFIG_ANDROID */
  483. }
  484. return 0;
  485. }
  486. static void extcon_dev_release(struct device *dev)
  487. {
  488. }
  489. static const char *muex_name = "mutually_exclusive";
  490. static void dummy_sysfs_dev_release(struct device *dev)
  491. {
  492. }
  493. /*
  494. * extcon_dev_allocate() - Allocate the memory of extcon device.
  495. * @supported_cable: Array of supported cable names ending with NULL.
  496. * If supported_cable is NULL, cable name related APIs
  497. * are disabled.
  498. *
  499. * This function allocates the memory for extcon device without allocating
  500. * memory in each extcon provider driver and initialize default setting for
  501. * extcon device.
  502. *
  503. * Return the pointer of extcon device if success or ERR_PTR(err) if fail
  504. */
  505. struct extcon_dev *extcon_dev_allocate(const char **supported_cable)
  506. {
  507. struct extcon_dev *edev;
  508. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  509. if (!edev)
  510. return ERR_PTR(-ENOMEM);
  511. edev->max_supported = 0;
  512. edev->supported_cable = supported_cable;
  513. return edev;
  514. }
  515. /*
  516. * extcon_dev_free() - Free the memory of extcon device.
  517. * @edev: the extcon device to free
  518. */
  519. void extcon_dev_free(struct extcon_dev *edev)
  520. {
  521. kfree(edev);
  522. }
  523. EXPORT_SYMBOL_GPL(extcon_dev_free);
  524. static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
  525. {
  526. struct extcon_dev **r = res;
  527. if (WARN_ON(!r || !*r))
  528. return 0;
  529. return *r == data;
  530. }
  531. static void devm_extcon_dev_release(struct device *dev, void *res)
  532. {
  533. extcon_dev_free(*(struct extcon_dev **)res);
  534. }
  535. /**
  536. * devm_extcon_dev_allocate - Allocate managed extcon device
  537. * @dev: device owning the extcon device being created
  538. * @supported_cable: Array of supported cable names ending with NULL.
  539. * If supported_cable is NULL, cable name related APIs
  540. * are disabled.
  541. *
  542. * This function manages automatically the memory of extcon device using device
  543. * resource management and simplify the control of freeing the memory of extcon
  544. * device.
  545. *
  546. * Returns the pointer memory of allocated extcon_dev if success
  547. * or ERR_PTR(err) if fail
  548. */
  549. struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
  550. const char **supported_cable)
  551. {
  552. struct extcon_dev **ptr, *edev;
  553. ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
  554. if (!ptr)
  555. return ERR_PTR(-ENOMEM);
  556. edev = extcon_dev_allocate(supported_cable);
  557. if (IS_ERR(edev)) {
  558. devres_free(ptr);
  559. return edev;
  560. }
  561. *ptr = edev;
  562. devres_add(dev, ptr);
  563. return edev;
  564. }
  565. EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
  566. void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
  567. {
  568. WARN_ON(devres_release(dev, devm_extcon_dev_release,
  569. devm_extcon_dev_match, edev));
  570. }
  571. EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
  572. /**
  573. * extcon_dev_register() - Register a new extcon device
  574. * @edev : the new extcon device (should be allocated before calling)
  575. *
  576. * Among the members of edev struct, please set the "user initializing data"
  577. * in any case and set the "optional callbacks" if required. However, please
  578. * do not set the values of "internal data", which are initialized by
  579. * this function.
  580. */
  581. int extcon_dev_register(struct extcon_dev *edev)
  582. {
  583. int ret, index = 0;
  584. if (!extcon_class) {
  585. ret = create_extcon_class();
  586. if (ret < 0)
  587. return ret;
  588. }
  589. if (edev->supported_cable) {
  590. /* Get size of array */
  591. for (index = 0; edev->supported_cable[index]; index++)
  592. ;
  593. edev->max_supported = index;
  594. } else {
  595. edev->max_supported = 0;
  596. }
  597. if (index > SUPPORTED_CABLE_MAX) {
  598. dev_err(&edev->dev, "extcon: maximum number of supported cables exceeded.\n");
  599. return -EINVAL;
  600. }
  601. edev->dev.class = extcon_class;
  602. edev->dev.release = extcon_dev_release;
  603. edev->name = edev->name ? edev->name : dev_name(edev->dev.parent);
  604. if (IS_ERR_OR_NULL(edev->name)) {
  605. dev_err(&edev->dev,
  606. "extcon device name is null\n");
  607. return -EINVAL;
  608. }
  609. dev_set_name(&edev->dev, "%s", edev->name);
  610. if (edev->max_supported) {
  611. char buf[10];
  612. char *str;
  613. struct extcon_cable *cable;
  614. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  615. edev->max_supported, GFP_KERNEL);
  616. if (!edev->cables) {
  617. ret = -ENOMEM;
  618. goto err_sysfs_alloc;
  619. }
  620. for (index = 0; index < edev->max_supported; index++) {
  621. cable = &edev->cables[index];
  622. snprintf(buf, 10, "cable.%d", index);
  623. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  624. GFP_KERNEL);
  625. if (!str) {
  626. for (index--; index >= 0; index--) {
  627. cable = &edev->cables[index];
  628. kfree(cable->attr_g.name);
  629. }
  630. ret = -ENOMEM;
  631. goto err_alloc_cables;
  632. }
  633. strcpy(str, buf);
  634. cable->edev = edev;
  635. cable->cable_index = index;
  636. cable->attrs[0] = &cable->attr_name.attr;
  637. cable->attrs[1] = &cable->attr_state.attr;
  638. cable->attrs[2] = NULL;
  639. cable->attr_g.name = str;
  640. cable->attr_g.attrs = cable->attrs;
  641. sysfs_attr_init(&cable->attr_name.attr);
  642. cable->attr_name.attr.name = "name";
  643. cable->attr_name.attr.mode = 0444;
  644. cable->attr_name.show = cable_name_show;
  645. sysfs_attr_init(&cable->attr_state.attr);
  646. cable->attr_state.attr.name = "state";
  647. cable->attr_state.attr.mode = 0444;
  648. cable->attr_state.show = cable_state_show;
  649. }
  650. }
  651. if (edev->max_supported && edev->mutually_exclusive) {
  652. char buf[80];
  653. char *name;
  654. /* Count the size of mutually_exclusive array */
  655. for (index = 0; edev->mutually_exclusive[index]; index++)
  656. ;
  657. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  658. (index + 1), GFP_KERNEL);
  659. if (!edev->attrs_muex) {
  660. ret = -ENOMEM;
  661. goto err_muex;
  662. }
  663. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  664. index, GFP_KERNEL);
  665. if (!edev->d_attrs_muex) {
  666. ret = -ENOMEM;
  667. kfree(edev->attrs_muex);
  668. goto err_muex;
  669. }
  670. for (index = 0; edev->mutually_exclusive[index]; index++) {
  671. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  672. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  673. GFP_KERNEL);
  674. if (!name) {
  675. for (index--; index >= 0; index--) {
  676. kfree(edev->d_attrs_muex[index].attr.
  677. name);
  678. }
  679. kfree(edev->d_attrs_muex);
  680. kfree(edev->attrs_muex);
  681. ret = -ENOMEM;
  682. goto err_muex;
  683. }
  684. strcpy(name, buf);
  685. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  686. edev->d_attrs_muex[index].attr.name = name;
  687. edev->d_attrs_muex[index].attr.mode = 0000;
  688. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  689. .attr;
  690. }
  691. edev->attr_g_muex.name = muex_name;
  692. edev->attr_g_muex.attrs = edev->attrs_muex;
  693. }
  694. if (edev->max_supported) {
  695. edev->extcon_dev_type.groups =
  696. kzalloc(sizeof(struct attribute_group *) *
  697. (edev->max_supported + 2), GFP_KERNEL);
  698. if (!edev->extcon_dev_type.groups) {
  699. ret = -ENOMEM;
  700. goto err_alloc_groups;
  701. }
  702. edev->extcon_dev_type.name = dev_name(&edev->dev);
  703. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  704. for (index = 0; index < edev->max_supported; index++)
  705. edev->extcon_dev_type.groups[index] =
  706. &edev->cables[index].attr_g;
  707. if (edev->mutually_exclusive)
  708. edev->extcon_dev_type.groups[index] =
  709. &edev->attr_g_muex;
  710. edev->dev.type = &edev->extcon_dev_type;
  711. }
  712. ret = device_register(&edev->dev);
  713. if (ret) {
  714. put_device(&edev->dev);
  715. goto err_dev;
  716. }
  717. #if defined(CONFIG_ANDROID)
  718. if (switch_class)
  719. ret = class_compat_create_link(switch_class, &edev->dev, NULL);
  720. #endif /* CONFIG_ANDROID */
  721. spin_lock_init(&edev->lock);
  722. RAW_INIT_NOTIFIER_HEAD(&edev->nh);
  723. dev_set_drvdata(&edev->dev, edev);
  724. edev->state = 0;
  725. mutex_lock(&extcon_dev_list_lock);
  726. list_add(&edev->entry, &extcon_dev_list);
  727. mutex_unlock(&extcon_dev_list_lock);
  728. return 0;
  729. err_dev:
  730. if (edev->max_supported)
  731. kfree(edev->extcon_dev_type.groups);
  732. err_alloc_groups:
  733. if (edev->max_supported && edev->mutually_exclusive) {
  734. for (index = 0; edev->mutually_exclusive[index]; index++)
  735. kfree(edev->d_attrs_muex[index].attr.name);
  736. kfree(edev->d_attrs_muex);
  737. kfree(edev->attrs_muex);
  738. }
  739. err_muex:
  740. for (index = 0; index < edev->max_supported; index++)
  741. kfree(edev->cables[index].attr_g.name);
  742. err_alloc_cables:
  743. if (edev->max_supported)
  744. kfree(edev->cables);
  745. err_sysfs_alloc:
  746. return ret;
  747. }
  748. EXPORT_SYMBOL_GPL(extcon_dev_register);
  749. /**
  750. * extcon_dev_unregister() - Unregister the extcon device.
  751. * @edev: the extcon device instance to be unregistered.
  752. *
  753. * Note that this does not call kfree(edev) because edev was not allocated
  754. * by this class.
  755. */
  756. void extcon_dev_unregister(struct extcon_dev *edev)
  757. {
  758. int index;
  759. mutex_lock(&extcon_dev_list_lock);
  760. list_del(&edev->entry);
  761. mutex_unlock(&extcon_dev_list_lock);
  762. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  763. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  764. dev_name(&edev->dev));
  765. return;
  766. }
  767. device_unregister(&edev->dev);
  768. if (edev->mutually_exclusive && edev->max_supported) {
  769. for (index = 0; edev->mutually_exclusive[index];
  770. index++)
  771. kfree(edev->d_attrs_muex[index].attr.name);
  772. kfree(edev->d_attrs_muex);
  773. kfree(edev->attrs_muex);
  774. }
  775. for (index = 0; index < edev->max_supported; index++)
  776. kfree(edev->cables[index].attr_g.name);
  777. if (edev->max_supported) {
  778. kfree(edev->extcon_dev_type.groups);
  779. kfree(edev->cables);
  780. }
  781. #if defined(CONFIG_ANDROID)
  782. if (switch_class)
  783. class_compat_remove_link(switch_class, &edev->dev, NULL);
  784. #endif
  785. put_device(&edev->dev);
  786. }
  787. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  788. static void devm_extcon_dev_unreg(struct device *dev, void *res)
  789. {
  790. extcon_dev_unregister(*(struct extcon_dev **)res);
  791. }
  792. /**
  793. * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
  794. * @dev: device to allocate extcon device
  795. * @edev: the new extcon device to register
  796. *
  797. * Managed extcon_dev_register() function. If extcon device is attached with
  798. * this function, that extcon device is automatically unregistered on driver
  799. * detach. Internally this function calls extcon_dev_register() function.
  800. * To get more information, refer that function.
  801. *
  802. * If extcon device is registered with this function and the device needs to be
  803. * unregistered separately, devm_extcon_dev_unregister() should be used.
  804. *
  805. * Returns 0 if success or negaive error number if failure.
  806. */
  807. int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
  808. {
  809. struct extcon_dev **ptr;
  810. int ret;
  811. ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
  812. if (!ptr)
  813. return -ENOMEM;
  814. ret = extcon_dev_register(edev);
  815. if (ret) {
  816. devres_free(ptr);
  817. return ret;
  818. }
  819. *ptr = edev;
  820. devres_add(dev, ptr);
  821. return 0;
  822. }
  823. EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
  824. /**
  825. * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
  826. * @dev: device the extcon belongs to
  827. * @edev: the extcon device to unregister
  828. *
  829. * Unregister extcon device that is registered with devm_extcon_dev_register()
  830. * function.
  831. */
  832. void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
  833. {
  834. WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
  835. devm_extcon_dev_match, edev));
  836. }
  837. EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
  838. #ifdef CONFIG_OF
  839. /*
  840. * extcon_get_edev_by_phandle - Get the extcon device from devicetree
  841. * @dev - instance to the given device
  842. * @index - index into list of extcon_dev
  843. *
  844. * return the instance of extcon device
  845. */
  846. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  847. {
  848. struct device_node *node;
  849. struct extcon_dev *edev;
  850. if (!dev->of_node) {
  851. dev_err(dev, "device does not have a device node entry\n");
  852. return ERR_PTR(-EINVAL);
  853. }
  854. node = of_parse_phandle(dev->of_node, "extcon", index);
  855. if (!node) {
  856. dev_err(dev, "failed to get phandle in %s node\n",
  857. dev->of_node->full_name);
  858. return ERR_PTR(-ENODEV);
  859. }
  860. edev = extcon_get_extcon_dev(node->name);
  861. if (!edev) {
  862. dev_err(dev, "unable to get extcon device : %s\n", node->name);
  863. return ERR_PTR(-ENODEV);
  864. }
  865. return edev;
  866. }
  867. #else
  868. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  869. {
  870. return ERR_PTR(-ENOSYS);
  871. }
  872. #endif /* CONFIG_OF */
  873. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  874. static int __init extcon_class_init(void)
  875. {
  876. return create_extcon_class();
  877. }
  878. module_init(extcon_class_init);
  879. static void __exit extcon_class_exit(void)
  880. {
  881. #if defined(CONFIG_ANDROID)
  882. class_compat_unregister(switch_class);
  883. #endif
  884. class_destroy(extcon_class);
  885. }
  886. module_exit(extcon_class_exit);
  887. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  888. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  889. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  890. MODULE_DESCRIPTION("External connector (extcon) class driver");
  891. MODULE_LICENSE("GPL");