extcon-class.c 28 KB

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