extcon-class.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  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. edev->dev.parent = dev;
  562. *ptr = edev;
  563. devres_add(dev, ptr);
  564. return edev;
  565. }
  566. EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
  567. void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
  568. {
  569. WARN_ON(devres_release(dev, devm_extcon_dev_release,
  570. devm_extcon_dev_match, edev));
  571. }
  572. EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
  573. /**
  574. * extcon_dev_register() - Register a new extcon device
  575. * @edev : the new extcon device (should be allocated before calling)
  576. *
  577. * Among the members of edev struct, please set the "user initializing data"
  578. * in any case and set the "optional callbacks" if required. However, please
  579. * do not set the values of "internal data", which are initialized by
  580. * this function.
  581. */
  582. int extcon_dev_register(struct extcon_dev *edev)
  583. {
  584. int ret, index = 0;
  585. if (!extcon_class) {
  586. ret = create_extcon_class();
  587. if (ret < 0)
  588. return ret;
  589. }
  590. if (edev->supported_cable) {
  591. /* Get size of array */
  592. for (index = 0; edev->supported_cable[index]; index++)
  593. ;
  594. edev->max_supported = index;
  595. } else {
  596. edev->max_supported = 0;
  597. }
  598. if (index > SUPPORTED_CABLE_MAX) {
  599. dev_err(&edev->dev, "extcon: maximum number of supported cables exceeded.\n");
  600. return -EINVAL;
  601. }
  602. edev->dev.class = extcon_class;
  603. edev->dev.release = extcon_dev_release;
  604. edev->name = edev->name ? edev->name : dev_name(edev->dev.parent);
  605. if (IS_ERR_OR_NULL(edev->name)) {
  606. dev_err(&edev->dev,
  607. "extcon device name is null\n");
  608. return -EINVAL;
  609. }
  610. dev_set_name(&edev->dev, "%s", edev->name);
  611. if (edev->max_supported) {
  612. char buf[10];
  613. char *str;
  614. struct extcon_cable *cable;
  615. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  616. edev->max_supported, GFP_KERNEL);
  617. if (!edev->cables) {
  618. ret = -ENOMEM;
  619. goto err_sysfs_alloc;
  620. }
  621. for (index = 0; index < edev->max_supported; index++) {
  622. cable = &edev->cables[index];
  623. snprintf(buf, 10, "cable.%d", index);
  624. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  625. GFP_KERNEL);
  626. if (!str) {
  627. for (index--; index >= 0; index--) {
  628. cable = &edev->cables[index];
  629. kfree(cable->attr_g.name);
  630. }
  631. ret = -ENOMEM;
  632. goto err_alloc_cables;
  633. }
  634. strcpy(str, buf);
  635. cable->edev = edev;
  636. cable->cable_index = index;
  637. cable->attrs[0] = &cable->attr_name.attr;
  638. cable->attrs[1] = &cable->attr_state.attr;
  639. cable->attrs[2] = NULL;
  640. cable->attr_g.name = str;
  641. cable->attr_g.attrs = cable->attrs;
  642. sysfs_attr_init(&cable->attr_name.attr);
  643. cable->attr_name.attr.name = "name";
  644. cable->attr_name.attr.mode = 0444;
  645. cable->attr_name.show = cable_name_show;
  646. sysfs_attr_init(&cable->attr_state.attr);
  647. cable->attr_state.attr.name = "state";
  648. cable->attr_state.attr.mode = 0444;
  649. cable->attr_state.show = cable_state_show;
  650. }
  651. }
  652. if (edev->max_supported && edev->mutually_exclusive) {
  653. char buf[80];
  654. char *name;
  655. /* Count the size of mutually_exclusive array */
  656. for (index = 0; edev->mutually_exclusive[index]; index++)
  657. ;
  658. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  659. (index + 1), GFP_KERNEL);
  660. if (!edev->attrs_muex) {
  661. ret = -ENOMEM;
  662. goto err_muex;
  663. }
  664. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  665. index, GFP_KERNEL);
  666. if (!edev->d_attrs_muex) {
  667. ret = -ENOMEM;
  668. kfree(edev->attrs_muex);
  669. goto err_muex;
  670. }
  671. for (index = 0; edev->mutually_exclusive[index]; index++) {
  672. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  673. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  674. GFP_KERNEL);
  675. if (!name) {
  676. for (index--; index >= 0; index--) {
  677. kfree(edev->d_attrs_muex[index].attr.
  678. name);
  679. }
  680. kfree(edev->d_attrs_muex);
  681. kfree(edev->attrs_muex);
  682. ret = -ENOMEM;
  683. goto err_muex;
  684. }
  685. strcpy(name, buf);
  686. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  687. edev->d_attrs_muex[index].attr.name = name;
  688. edev->d_attrs_muex[index].attr.mode = 0000;
  689. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  690. .attr;
  691. }
  692. edev->attr_g_muex.name = muex_name;
  693. edev->attr_g_muex.attrs = edev->attrs_muex;
  694. }
  695. if (edev->max_supported) {
  696. edev->extcon_dev_type.groups =
  697. kzalloc(sizeof(struct attribute_group *) *
  698. (edev->max_supported + 2), GFP_KERNEL);
  699. if (!edev->extcon_dev_type.groups) {
  700. ret = -ENOMEM;
  701. goto err_alloc_groups;
  702. }
  703. edev->extcon_dev_type.name = dev_name(&edev->dev);
  704. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  705. for (index = 0; index < edev->max_supported; index++)
  706. edev->extcon_dev_type.groups[index] =
  707. &edev->cables[index].attr_g;
  708. if (edev->mutually_exclusive)
  709. edev->extcon_dev_type.groups[index] =
  710. &edev->attr_g_muex;
  711. edev->dev.type = &edev->extcon_dev_type;
  712. }
  713. ret = device_register(&edev->dev);
  714. if (ret) {
  715. put_device(&edev->dev);
  716. goto err_dev;
  717. }
  718. #if defined(CONFIG_ANDROID)
  719. if (switch_class)
  720. ret = class_compat_create_link(switch_class, &edev->dev, NULL);
  721. #endif /* CONFIG_ANDROID */
  722. spin_lock_init(&edev->lock);
  723. RAW_INIT_NOTIFIER_HEAD(&edev->nh);
  724. dev_set_drvdata(&edev->dev, edev);
  725. edev->state = 0;
  726. mutex_lock(&extcon_dev_list_lock);
  727. list_add(&edev->entry, &extcon_dev_list);
  728. mutex_unlock(&extcon_dev_list_lock);
  729. return 0;
  730. err_dev:
  731. if (edev->max_supported)
  732. kfree(edev->extcon_dev_type.groups);
  733. err_alloc_groups:
  734. if (edev->max_supported && edev->mutually_exclusive) {
  735. for (index = 0; edev->mutually_exclusive[index]; index++)
  736. kfree(edev->d_attrs_muex[index].attr.name);
  737. kfree(edev->d_attrs_muex);
  738. kfree(edev->attrs_muex);
  739. }
  740. err_muex:
  741. for (index = 0; index < edev->max_supported; index++)
  742. kfree(edev->cables[index].attr_g.name);
  743. err_alloc_cables:
  744. if (edev->max_supported)
  745. kfree(edev->cables);
  746. err_sysfs_alloc:
  747. return ret;
  748. }
  749. EXPORT_SYMBOL_GPL(extcon_dev_register);
  750. /**
  751. * extcon_dev_unregister() - Unregister the extcon device.
  752. * @edev: the extcon device instance to be unregistered.
  753. *
  754. * Note that this does not call kfree(edev) because edev was not allocated
  755. * by this class.
  756. */
  757. void extcon_dev_unregister(struct extcon_dev *edev)
  758. {
  759. int index;
  760. mutex_lock(&extcon_dev_list_lock);
  761. list_del(&edev->entry);
  762. mutex_unlock(&extcon_dev_list_lock);
  763. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  764. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  765. dev_name(&edev->dev));
  766. return;
  767. }
  768. device_unregister(&edev->dev);
  769. if (edev->mutually_exclusive && edev->max_supported) {
  770. for (index = 0; edev->mutually_exclusive[index];
  771. index++)
  772. kfree(edev->d_attrs_muex[index].attr.name);
  773. kfree(edev->d_attrs_muex);
  774. kfree(edev->attrs_muex);
  775. }
  776. for (index = 0; index < edev->max_supported; index++)
  777. kfree(edev->cables[index].attr_g.name);
  778. if (edev->max_supported) {
  779. kfree(edev->extcon_dev_type.groups);
  780. kfree(edev->cables);
  781. }
  782. #if defined(CONFIG_ANDROID)
  783. if (switch_class)
  784. class_compat_remove_link(switch_class, &edev->dev, NULL);
  785. #endif
  786. put_device(&edev->dev);
  787. }
  788. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  789. static void devm_extcon_dev_unreg(struct device *dev, void *res)
  790. {
  791. extcon_dev_unregister(*(struct extcon_dev **)res);
  792. }
  793. /**
  794. * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
  795. * @dev: device to allocate extcon device
  796. * @edev: the new extcon device to register
  797. *
  798. * Managed extcon_dev_register() function. If extcon device is attached with
  799. * this function, that extcon device is automatically unregistered on driver
  800. * detach. Internally this function calls extcon_dev_register() function.
  801. * To get more information, refer that function.
  802. *
  803. * If extcon device is registered with this function and the device needs to be
  804. * unregistered separately, devm_extcon_dev_unregister() should be used.
  805. *
  806. * Returns 0 if success or negaive error number if failure.
  807. */
  808. int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
  809. {
  810. struct extcon_dev **ptr;
  811. int ret;
  812. ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
  813. if (!ptr)
  814. return -ENOMEM;
  815. ret = extcon_dev_register(edev);
  816. if (ret) {
  817. devres_free(ptr);
  818. return ret;
  819. }
  820. *ptr = edev;
  821. devres_add(dev, ptr);
  822. return 0;
  823. }
  824. EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
  825. /**
  826. * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
  827. * @dev: device the extcon belongs to
  828. * @edev: the extcon device to unregister
  829. *
  830. * Unregister extcon device that is registered with devm_extcon_dev_register()
  831. * function.
  832. */
  833. void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
  834. {
  835. WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
  836. devm_extcon_dev_match, edev));
  837. }
  838. EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
  839. #ifdef CONFIG_OF
  840. /*
  841. * extcon_get_edev_by_phandle - Get the extcon device from devicetree
  842. * @dev - instance to the given device
  843. * @index - index into list of extcon_dev
  844. *
  845. * return the instance of extcon device
  846. */
  847. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  848. {
  849. struct device_node *node;
  850. struct extcon_dev *edev;
  851. if (!dev->of_node) {
  852. dev_err(dev, "device does not have a device node entry\n");
  853. return ERR_PTR(-EINVAL);
  854. }
  855. node = of_parse_phandle(dev->of_node, "extcon", index);
  856. if (!node) {
  857. dev_err(dev, "failed to get phandle in %s node\n",
  858. dev->of_node->full_name);
  859. return ERR_PTR(-ENODEV);
  860. }
  861. edev = extcon_get_extcon_dev(node->name);
  862. if (!edev) {
  863. dev_err(dev, "unable to get extcon device : %s\n", node->name);
  864. return ERR_PTR(-ENODEV);
  865. }
  866. return edev;
  867. }
  868. #else
  869. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  870. {
  871. return ERR_PTR(-ENOSYS);
  872. }
  873. #endif /* CONFIG_OF */
  874. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  875. static int __init extcon_class_init(void)
  876. {
  877. return create_extcon_class();
  878. }
  879. module_init(extcon_class_init);
  880. static void __exit extcon_class_exit(void)
  881. {
  882. #if defined(CONFIG_ANDROID)
  883. class_compat_unregister(switch_class);
  884. #endif
  885. class_destroy(extcon_class);
  886. }
  887. module_exit(extcon_class_exit);
  888. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  889. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  890. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  891. MODULE_DESCRIPTION("External connector (extcon) class driver");
  892. MODULE_LICENSE("GPL");