extcon.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /*
  2. * drivers/extcon/extcon.c - External Connector (extcon) framework.
  3. *
  4. * External connector (extcon) class driver
  5. *
  6. * Copyright (C) 2015 Samsung Electronics
  7. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  8. *
  9. * Copyright (C) 2012 Samsung Electronics
  10. * Author: Donggeun Kim <dg77.kim@samsung.com>
  11. * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
  12. *
  13. * based on android/drivers/switch/switch_class.c
  14. * Copyright (C) 2008 Google, Inc.
  15. * Author: Mike Lockwood <lockwood@android.com>
  16. *
  17. * This software is licensed under the terms of the GNU General Public
  18. * License version 2, as published by the Free Software Foundation, and
  19. * may be copied, distributed, and modified under those terms.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/init.h>
  29. #include <linux/device.h>
  30. #include <linux/fs.h>
  31. #include <linux/err.h>
  32. #include <linux/extcon.h>
  33. #include <linux/of.h>
  34. #include <linux/slab.h>
  35. #include <linux/sysfs.h>
  36. #define SUPPORTED_CABLE_MAX 32
  37. #define CABLE_NAME_MAX 30
  38. static const char *extcon_name[] = {
  39. [EXTCON_NONE] = "NONE",
  40. /* USB external connector */
  41. [EXTCON_USB] = "USB",
  42. [EXTCON_USB_HOST] = "USB-HOST",
  43. /* Charging external connector */
  44. [EXTCON_CHG_USB_SDP] = "SDP",
  45. [EXTCON_CHG_USB_DCP] = "DCP",
  46. [EXTCON_CHG_USB_CDP] = "CDP",
  47. [EXTCON_CHG_USB_ACA] = "ACA",
  48. [EXTCON_CHG_USB_FAST] = "FAST-CHARGER",
  49. [EXTCON_CHG_USB_SLOW] = "SLOW-CHARGER",
  50. /* Jack external connector */
  51. [EXTCON_JACK_MICROPHONE] = "MICROPHONE",
  52. [EXTCON_JACK_HEADPHONE] = "HEADPHONE",
  53. [EXTCON_JACK_LINE_IN] = "LINE-IN",
  54. [EXTCON_JACK_LINE_OUT] = "LINE-OUT",
  55. [EXTCON_JACK_VIDEO_IN] = "VIDEO-IN",
  56. [EXTCON_JACK_VIDEO_OUT] = "VIDEO-OUT",
  57. [EXTCON_JACK_SPDIF_IN] = "SPDIF-IN",
  58. [EXTCON_JACK_SPDIF_OUT] = "SPDIF-OUT",
  59. /* Display external connector */
  60. [EXTCON_DISP_HDMI] = "HDMI",
  61. [EXTCON_DISP_MHL] = "MHL",
  62. [EXTCON_DISP_DVI] = "DVI",
  63. [EXTCON_DISP_VGA] = "VGA",
  64. /* Miscellaneous external connector */
  65. [EXTCON_DOCK] = "DOCK",
  66. [EXTCON_JIG] = "JIG",
  67. [EXTCON_MECHANICAL] = "MECHANICAL",
  68. NULL,
  69. };
  70. /**
  71. * struct extcon_cable - An internal data for each cable of extcon device.
  72. * @edev: The extcon device
  73. * @cable_index: Index of this cable in the edev
  74. * @attr_g: Attribute group for the cable
  75. * @attr_name: "name" sysfs entry
  76. * @attr_state: "state" sysfs entry
  77. * @attrs: Array pointing to attr_name and attr_state for attr_g
  78. */
  79. struct extcon_cable {
  80. struct extcon_dev *edev;
  81. int cable_index;
  82. struct attribute_group attr_g;
  83. struct device_attribute attr_name;
  84. struct device_attribute attr_state;
  85. struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
  86. };
  87. static struct class *extcon_class;
  88. #if defined(CONFIG_ANDROID)
  89. static struct class_compat *switch_class;
  90. #endif /* CONFIG_ANDROID */
  91. static LIST_HEAD(extcon_dev_list);
  92. static DEFINE_MUTEX(extcon_dev_list_lock);
  93. /**
  94. * check_mutually_exclusive - Check if new_state violates mutually_exclusive
  95. * condition.
  96. * @edev: the extcon device
  97. * @new_state: new cable attach status for @edev
  98. *
  99. * Returns 0 if nothing violates. Returns the index + 1 for the first
  100. * violated condition.
  101. */
  102. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  103. {
  104. int i = 0;
  105. if (!edev->mutually_exclusive)
  106. return 0;
  107. for (i = 0; edev->mutually_exclusive[i]; i++) {
  108. int weight;
  109. u32 correspondants = new_state & edev->mutually_exclusive[i];
  110. /* calculate the total number of bits set */
  111. weight = hweight32(correspondants);
  112. if (weight > 1)
  113. return i + 1;
  114. }
  115. return 0;
  116. }
  117. static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
  118. {
  119. int i;
  120. /* Find the the index of extcon cable in edev->supported_cable */
  121. for (i = 0; i < edev->max_supported; i++) {
  122. if (edev->supported_cable[i] == id)
  123. return i;
  124. }
  125. return -EINVAL;
  126. }
  127. static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
  128. {
  129. if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
  130. *attached = ((new >> idx) & 0x1) ? true : false;
  131. return true;
  132. }
  133. return false;
  134. }
  135. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  136. char *buf)
  137. {
  138. int i, count = 0;
  139. struct extcon_dev *edev = dev_get_drvdata(dev);
  140. if (edev->max_supported == 0)
  141. return sprintf(buf, "%u\n", edev->state);
  142. for (i = 0; i < edev->max_supported; i++) {
  143. count += sprintf(buf + count, "%s=%d\n",
  144. extcon_name[edev->supported_cable[i]],
  145. !!(edev->state & (1 << i)));
  146. }
  147. return count;
  148. }
  149. static ssize_t state_store(struct device *dev, struct device_attribute *attr,
  150. const char *buf, size_t count)
  151. {
  152. u32 state;
  153. ssize_t ret = 0;
  154. struct extcon_dev *edev = dev_get_drvdata(dev);
  155. ret = sscanf(buf, "0x%x", &state);
  156. if (ret == 0)
  157. ret = -EINVAL;
  158. else
  159. ret = extcon_set_state(edev, state);
  160. if (ret < 0)
  161. return ret;
  162. return count;
  163. }
  164. static DEVICE_ATTR_RW(state);
  165. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  166. char *buf)
  167. {
  168. struct extcon_dev *edev = dev_get_drvdata(dev);
  169. return sprintf(buf, "%s\n", edev->name);
  170. }
  171. static DEVICE_ATTR_RO(name);
  172. static ssize_t cable_name_show(struct device *dev,
  173. struct device_attribute *attr, char *buf)
  174. {
  175. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  176. attr_name);
  177. int i = cable->cable_index;
  178. return sprintf(buf, "%s\n",
  179. extcon_name[cable->edev->supported_cable[i]]);
  180. }
  181. static ssize_t cable_state_show(struct device *dev,
  182. struct device_attribute *attr, char *buf)
  183. {
  184. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  185. attr_state);
  186. int i = cable->cable_index;
  187. return sprintf(buf, "%d\n",
  188. extcon_get_cable_state_(cable->edev,
  189. cable->edev->supported_cable[i]));
  190. }
  191. /**
  192. * extcon_update_state() - Update the cable attach states of the extcon device
  193. * only for the masked bits.
  194. * @edev: the extcon device
  195. * @mask: the bit mask to designate updated bits.
  196. * @state: new cable attach status for @edev
  197. *
  198. * Changing the state sends uevent with environment variable containing
  199. * the name of extcon device (envp[0]) and the state output (envp[1]).
  200. * Tizen uses this format for extcon device to get events from ports.
  201. * Android uses this format as well.
  202. *
  203. * Note that the notifier provides which bits are changed in the state
  204. * variable with the val parameter (second) to the callback.
  205. */
  206. int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
  207. {
  208. char name_buf[120];
  209. char state_buf[120];
  210. char *prop_buf;
  211. char *envp[3];
  212. int env_offset = 0;
  213. int length;
  214. int index;
  215. unsigned long flags;
  216. bool attached;
  217. if (!edev)
  218. return -EINVAL;
  219. spin_lock_irqsave(&edev->lock, flags);
  220. if (edev->state != ((edev->state & ~mask) | (state & mask))) {
  221. u32 old_state;
  222. if (check_mutually_exclusive(edev, (edev->state & ~mask) |
  223. (state & mask))) {
  224. spin_unlock_irqrestore(&edev->lock, flags);
  225. return -EPERM;
  226. }
  227. old_state = edev->state;
  228. edev->state &= ~mask;
  229. edev->state |= state & mask;
  230. for (index = 0; index < edev->max_supported; index++) {
  231. if (is_extcon_changed(old_state, edev->state, index,
  232. &attached))
  233. raw_notifier_call_chain(&edev->nh[index],
  234. attached, edev);
  235. }
  236. /* This could be in interrupt handler */
  237. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  238. if (prop_buf) {
  239. length = name_show(&edev->dev, NULL, prop_buf);
  240. if (length > 0) {
  241. if (prop_buf[length - 1] == '\n')
  242. prop_buf[length - 1] = 0;
  243. snprintf(name_buf, sizeof(name_buf),
  244. "NAME=%s", prop_buf);
  245. envp[env_offset++] = name_buf;
  246. }
  247. length = state_show(&edev->dev, NULL, prop_buf);
  248. if (length > 0) {
  249. if (prop_buf[length - 1] == '\n')
  250. prop_buf[length - 1] = 0;
  251. snprintf(state_buf, sizeof(state_buf),
  252. "STATE=%s", prop_buf);
  253. envp[env_offset++] = state_buf;
  254. }
  255. envp[env_offset] = NULL;
  256. /* Unlock early before uevent */
  257. spin_unlock_irqrestore(&edev->lock, flags);
  258. kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
  259. free_page((unsigned long)prop_buf);
  260. } else {
  261. /* Unlock early before uevent */
  262. spin_unlock_irqrestore(&edev->lock, flags);
  263. dev_err(&edev->dev, "out of memory in extcon_set_state\n");
  264. kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
  265. }
  266. } else {
  267. /* No changes */
  268. spin_unlock_irqrestore(&edev->lock, flags);
  269. }
  270. return 0;
  271. }
  272. EXPORT_SYMBOL_GPL(extcon_update_state);
  273. /**
  274. * extcon_set_state() - Set the cable attach states of the extcon device.
  275. * @edev: the extcon device
  276. * @state: new cable attach status for @edev
  277. *
  278. * Note that notifier provides which bits are changed in the state
  279. * variable with the val parameter (second) to the callback.
  280. */
  281. int extcon_set_state(struct extcon_dev *edev, u32 state)
  282. {
  283. if (!edev)
  284. return -EINVAL;
  285. return extcon_update_state(edev, 0xffffffff, state);
  286. }
  287. EXPORT_SYMBOL_GPL(extcon_set_state);
  288. /**
  289. * extcon_get_cable_state_() - Get the status of a specific cable.
  290. * @edev: the extcon device that has the cable.
  291. * @id: the unique id of each external connector in extcon enumeration.
  292. */
  293. int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
  294. {
  295. int index;
  296. if (!edev)
  297. return -EINVAL;
  298. index = find_cable_index_by_id(edev, id);
  299. if (index < 0)
  300. return index;
  301. if (edev->max_supported && edev->max_supported <= index)
  302. return -EINVAL;
  303. return !!(edev->state & (1 << index));
  304. }
  305. EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
  306. /**
  307. * extcon_set_cable_state_() - Set the status of a specific cable.
  308. * @edev: the extcon device that has the cable.
  309. * @id: the unique id of each external connector
  310. * in extcon enumeration.
  311. * @state: the new cable status. The default semantics is
  312. * true: attached / false: detached.
  313. */
  314. int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
  315. bool cable_state)
  316. {
  317. u32 state;
  318. int index;
  319. if (!edev)
  320. return -EINVAL;
  321. index = find_cable_index_by_id(edev, id);
  322. if (index < 0)
  323. return index;
  324. if (edev->max_supported && edev->max_supported <= index)
  325. return -EINVAL;
  326. state = cable_state ? (1 << index) : 0;
  327. return extcon_update_state(edev, 1 << index, 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. if (!extcon_name)
  338. return ERR_PTR(-EINVAL);
  339. mutex_lock(&extcon_dev_list_lock);
  340. list_for_each_entry(sd, &extcon_dev_list, entry) {
  341. if (!strcmp(sd->name, extcon_name))
  342. goto out;
  343. }
  344. sd = NULL;
  345. out:
  346. mutex_unlock(&extcon_dev_list_lock);
  347. return sd;
  348. }
  349. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  350. /**
  351. * extcon_register_notifier() - Register a notifiee to get notified by
  352. * any attach status changes from the extcon.
  353. * @edev: the extcon device that has the external connecotr.
  354. * @id: the unique id of each external connector in extcon enumeration.
  355. * @nb: a notifier block to be registered.
  356. *
  357. * Note that the second parameter given to the callback of nb (val) is
  358. * "old_state", not the current state. The current state can be retrieved
  359. * by looking at the third pameter (edev pointer)'s state value.
  360. */
  361. int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
  362. struct notifier_block *nb)
  363. {
  364. unsigned long flags;
  365. int ret, idx;
  366. if (!nb)
  367. return -EINVAL;
  368. if (edev) {
  369. idx = find_cable_index_by_id(edev, id);
  370. if (idx < 0)
  371. return idx;
  372. spin_lock_irqsave(&edev->lock, flags);
  373. ret = raw_notifier_chain_register(&edev->nh[idx], nb);
  374. spin_unlock_irqrestore(&edev->lock, flags);
  375. } else {
  376. struct extcon_dev *extd;
  377. mutex_lock(&extcon_dev_list_lock);
  378. list_for_each_entry(extd, &extcon_dev_list, entry) {
  379. idx = find_cable_index_by_id(extd, id);
  380. if (idx >= 0)
  381. break;
  382. }
  383. mutex_unlock(&extcon_dev_list_lock);
  384. if (idx >= 0) {
  385. edev = extd;
  386. return extcon_register_notifier(extd, id, nb);
  387. } else {
  388. ret = -ENODEV;
  389. }
  390. }
  391. return ret;
  392. }
  393. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  394. /**
  395. * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
  396. * @edev: the extcon device that has the external connecotr.
  397. * @id: the unique id of each external connector in extcon enumeration.
  398. * @nb: a notifier block to be registered.
  399. */
  400. int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
  401. struct notifier_block *nb)
  402. {
  403. unsigned long flags;
  404. int ret, idx;
  405. if (!edev || !nb)
  406. return -EINVAL;
  407. idx = find_cable_index_by_id(edev, id);
  408. if (idx < 0)
  409. return idx;
  410. spin_lock_irqsave(&edev->lock, flags);
  411. ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
  412. spin_unlock_irqrestore(&edev->lock, flags);
  413. return ret;
  414. }
  415. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  416. static struct attribute *extcon_attrs[] = {
  417. &dev_attr_state.attr,
  418. &dev_attr_name.attr,
  419. NULL,
  420. };
  421. ATTRIBUTE_GROUPS(extcon);
  422. static int create_extcon_class(void)
  423. {
  424. if (!extcon_class) {
  425. extcon_class = class_create(THIS_MODULE, "extcon");
  426. if (IS_ERR(extcon_class))
  427. return PTR_ERR(extcon_class);
  428. extcon_class->dev_groups = extcon_groups;
  429. #if defined(CONFIG_ANDROID)
  430. switch_class = class_compat_register("switch");
  431. if (WARN(!switch_class, "cannot allocate"))
  432. return -ENOMEM;
  433. #endif /* CONFIG_ANDROID */
  434. }
  435. return 0;
  436. }
  437. static void extcon_dev_release(struct device *dev)
  438. {
  439. }
  440. static const char *muex_name = "mutually_exclusive";
  441. static void dummy_sysfs_dev_release(struct device *dev)
  442. {
  443. }
  444. /*
  445. * extcon_dev_allocate() - Allocate the memory of extcon device.
  446. * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
  447. * If supported_cable is NULL, cable name related APIs
  448. * are disabled.
  449. *
  450. * This function allocates the memory for extcon device without allocating
  451. * memory in each extcon provider driver and initialize default setting for
  452. * extcon device.
  453. *
  454. * Return the pointer of extcon device if success or ERR_PTR(err) if fail
  455. */
  456. struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
  457. {
  458. struct extcon_dev *edev;
  459. if (!supported_cable)
  460. return ERR_PTR(-EINVAL);
  461. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  462. if (!edev)
  463. return ERR_PTR(-ENOMEM);
  464. edev->max_supported = 0;
  465. edev->supported_cable = supported_cable;
  466. return edev;
  467. }
  468. /*
  469. * extcon_dev_free() - Free the memory of extcon device.
  470. * @edev: the extcon device to free
  471. */
  472. void extcon_dev_free(struct extcon_dev *edev)
  473. {
  474. kfree(edev);
  475. }
  476. EXPORT_SYMBOL_GPL(extcon_dev_free);
  477. /**
  478. * extcon_dev_register() - Register a new extcon device
  479. * @edev : the new extcon device (should be allocated before calling)
  480. *
  481. * Among the members of edev struct, please set the "user initializing data"
  482. * in any case and set the "optional callbacks" if required. However, please
  483. * do not set the values of "internal data", which are initialized by
  484. * this function.
  485. */
  486. int extcon_dev_register(struct extcon_dev *edev)
  487. {
  488. int ret, index = 0;
  489. static atomic_t edev_no = ATOMIC_INIT(-1);
  490. if (!extcon_class) {
  491. ret = create_extcon_class();
  492. if (ret < 0)
  493. return ret;
  494. }
  495. if (!edev || !edev->supported_cable)
  496. return -EINVAL;
  497. for (; edev->supported_cable[index] != EXTCON_NONE; index++);
  498. edev->max_supported = index;
  499. if (index > SUPPORTED_CABLE_MAX) {
  500. dev_err(&edev->dev,
  501. "exceed the maximum number of supported cables\n");
  502. return -EINVAL;
  503. }
  504. edev->dev.class = extcon_class;
  505. edev->dev.release = extcon_dev_release;
  506. edev->name = dev_name(edev->dev.parent);
  507. if (IS_ERR_OR_NULL(edev->name)) {
  508. dev_err(&edev->dev,
  509. "extcon device name is null\n");
  510. return -EINVAL;
  511. }
  512. dev_set_name(&edev->dev, "extcon%lu",
  513. (unsigned long)atomic_inc_return(&edev_no));
  514. if (edev->max_supported) {
  515. char buf[10];
  516. char *str;
  517. struct extcon_cable *cable;
  518. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  519. edev->max_supported, GFP_KERNEL);
  520. if (!edev->cables) {
  521. ret = -ENOMEM;
  522. goto err_sysfs_alloc;
  523. }
  524. for (index = 0; index < edev->max_supported; index++) {
  525. cable = &edev->cables[index];
  526. snprintf(buf, 10, "cable.%d", index);
  527. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  528. GFP_KERNEL);
  529. if (!str) {
  530. for (index--; index >= 0; index--) {
  531. cable = &edev->cables[index];
  532. kfree(cable->attr_g.name);
  533. }
  534. ret = -ENOMEM;
  535. goto err_alloc_cables;
  536. }
  537. strcpy(str, buf);
  538. cable->edev = edev;
  539. cable->cable_index = index;
  540. cable->attrs[0] = &cable->attr_name.attr;
  541. cable->attrs[1] = &cable->attr_state.attr;
  542. cable->attrs[2] = NULL;
  543. cable->attr_g.name = str;
  544. cable->attr_g.attrs = cable->attrs;
  545. sysfs_attr_init(&cable->attr_name.attr);
  546. cable->attr_name.attr.name = "name";
  547. cable->attr_name.attr.mode = 0444;
  548. cable->attr_name.show = cable_name_show;
  549. sysfs_attr_init(&cable->attr_state.attr);
  550. cable->attr_state.attr.name = "state";
  551. cable->attr_state.attr.mode = 0444;
  552. cable->attr_state.show = cable_state_show;
  553. }
  554. }
  555. if (edev->max_supported && edev->mutually_exclusive) {
  556. char buf[80];
  557. char *name;
  558. /* Count the size of mutually_exclusive array */
  559. for (index = 0; edev->mutually_exclusive[index]; index++)
  560. ;
  561. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  562. (index + 1), GFP_KERNEL);
  563. if (!edev->attrs_muex) {
  564. ret = -ENOMEM;
  565. goto err_muex;
  566. }
  567. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  568. index, GFP_KERNEL);
  569. if (!edev->d_attrs_muex) {
  570. ret = -ENOMEM;
  571. kfree(edev->attrs_muex);
  572. goto err_muex;
  573. }
  574. for (index = 0; edev->mutually_exclusive[index]; index++) {
  575. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  576. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  577. GFP_KERNEL);
  578. if (!name) {
  579. for (index--; index >= 0; index--) {
  580. kfree(edev->d_attrs_muex[index].attr.
  581. name);
  582. }
  583. kfree(edev->d_attrs_muex);
  584. kfree(edev->attrs_muex);
  585. ret = -ENOMEM;
  586. goto err_muex;
  587. }
  588. strcpy(name, buf);
  589. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  590. edev->d_attrs_muex[index].attr.name = name;
  591. edev->d_attrs_muex[index].attr.mode = 0000;
  592. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  593. .attr;
  594. }
  595. edev->attr_g_muex.name = muex_name;
  596. edev->attr_g_muex.attrs = edev->attrs_muex;
  597. }
  598. if (edev->max_supported) {
  599. edev->extcon_dev_type.groups =
  600. kzalloc(sizeof(struct attribute_group *) *
  601. (edev->max_supported + 2), GFP_KERNEL);
  602. if (!edev->extcon_dev_type.groups) {
  603. ret = -ENOMEM;
  604. goto err_alloc_groups;
  605. }
  606. edev->extcon_dev_type.name = dev_name(&edev->dev);
  607. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  608. for (index = 0; index < edev->max_supported; index++)
  609. edev->extcon_dev_type.groups[index] =
  610. &edev->cables[index].attr_g;
  611. if (edev->mutually_exclusive)
  612. edev->extcon_dev_type.groups[index] =
  613. &edev->attr_g_muex;
  614. edev->dev.type = &edev->extcon_dev_type;
  615. }
  616. ret = device_register(&edev->dev);
  617. if (ret) {
  618. put_device(&edev->dev);
  619. goto err_dev;
  620. }
  621. #if defined(CONFIG_ANDROID)
  622. if (switch_class)
  623. ret = class_compat_create_link(switch_class, &edev->dev, NULL);
  624. #endif /* CONFIG_ANDROID */
  625. spin_lock_init(&edev->lock);
  626. edev->nh = devm_kzalloc(&edev->dev,
  627. sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
  628. if (!edev->nh) {
  629. ret = -ENOMEM;
  630. goto err_dev;
  631. }
  632. for (index = 0; index < edev->max_supported; index++)
  633. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  634. dev_set_drvdata(&edev->dev, edev);
  635. edev->state = 0;
  636. mutex_lock(&extcon_dev_list_lock);
  637. list_add(&edev->entry, &extcon_dev_list);
  638. mutex_unlock(&extcon_dev_list_lock);
  639. return 0;
  640. err_dev:
  641. if (edev->max_supported)
  642. kfree(edev->extcon_dev_type.groups);
  643. err_alloc_groups:
  644. if (edev->max_supported && edev->mutually_exclusive) {
  645. for (index = 0; edev->mutually_exclusive[index]; index++)
  646. kfree(edev->d_attrs_muex[index].attr.name);
  647. kfree(edev->d_attrs_muex);
  648. kfree(edev->attrs_muex);
  649. }
  650. err_muex:
  651. for (index = 0; index < edev->max_supported; index++)
  652. kfree(edev->cables[index].attr_g.name);
  653. err_alloc_cables:
  654. if (edev->max_supported)
  655. kfree(edev->cables);
  656. err_sysfs_alloc:
  657. return ret;
  658. }
  659. EXPORT_SYMBOL_GPL(extcon_dev_register);
  660. /**
  661. * extcon_dev_unregister() - Unregister the extcon device.
  662. * @edev: the extcon device instance to be unregistered.
  663. *
  664. * Note that this does not call kfree(edev) because edev was not allocated
  665. * by this class.
  666. */
  667. void extcon_dev_unregister(struct extcon_dev *edev)
  668. {
  669. int index;
  670. if (!edev)
  671. return;
  672. mutex_lock(&extcon_dev_list_lock);
  673. list_del(&edev->entry);
  674. mutex_unlock(&extcon_dev_list_lock);
  675. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  676. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  677. dev_name(&edev->dev));
  678. return;
  679. }
  680. device_unregister(&edev->dev);
  681. if (edev->mutually_exclusive && edev->max_supported) {
  682. for (index = 0; edev->mutually_exclusive[index];
  683. index++)
  684. kfree(edev->d_attrs_muex[index].attr.name);
  685. kfree(edev->d_attrs_muex);
  686. kfree(edev->attrs_muex);
  687. }
  688. for (index = 0; index < edev->max_supported; index++)
  689. kfree(edev->cables[index].attr_g.name);
  690. if (edev->max_supported) {
  691. kfree(edev->extcon_dev_type.groups);
  692. kfree(edev->cables);
  693. }
  694. #if defined(CONFIG_ANDROID)
  695. if (switch_class)
  696. class_compat_remove_link(switch_class, &edev->dev, NULL);
  697. #endif
  698. put_device(&edev->dev);
  699. }
  700. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  701. #ifdef CONFIG_OF
  702. /*
  703. * extcon_get_edev_by_phandle - Get the extcon device from devicetree
  704. * @dev - instance to the given device
  705. * @index - index into list of extcon_dev
  706. *
  707. * return the instance of extcon device
  708. */
  709. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  710. {
  711. struct device_node *node;
  712. struct extcon_dev *edev;
  713. if (!dev)
  714. return ERR_PTR(-EINVAL);
  715. if (!dev->of_node) {
  716. dev_err(dev, "device does not have a device node entry\n");
  717. return ERR_PTR(-EINVAL);
  718. }
  719. node = of_parse_phandle(dev->of_node, "extcon", index);
  720. if (!node) {
  721. dev_err(dev, "failed to get phandle in %s node\n",
  722. dev->of_node->full_name);
  723. return ERR_PTR(-ENODEV);
  724. }
  725. mutex_lock(&extcon_dev_list_lock);
  726. list_for_each_entry(edev, &extcon_dev_list, entry) {
  727. if (edev->dev.parent && edev->dev.parent->of_node == node) {
  728. mutex_unlock(&extcon_dev_list_lock);
  729. of_node_put(node);
  730. return edev;
  731. }
  732. }
  733. mutex_unlock(&extcon_dev_list_lock);
  734. of_node_put(node);
  735. return ERR_PTR(-EPROBE_DEFER);
  736. }
  737. #else
  738. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  739. {
  740. return ERR_PTR(-ENOSYS);
  741. }
  742. #endif /* CONFIG_OF */
  743. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  744. /**
  745. * extcon_get_edev_name() - Get the name of the extcon device.
  746. * @edev: the extcon device
  747. */
  748. const char *extcon_get_edev_name(struct extcon_dev *edev)
  749. {
  750. return !edev ? NULL : edev->name;
  751. }
  752. static int __init extcon_class_init(void)
  753. {
  754. return create_extcon_class();
  755. }
  756. module_init(extcon_class_init);
  757. static void __exit extcon_class_exit(void)
  758. {
  759. #if defined(CONFIG_ANDROID)
  760. class_compat_unregister(switch_class);
  761. #endif
  762. class_destroy(extcon_class);
  763. }
  764. module_exit(extcon_class_exit);
  765. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  766. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  767. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  768. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  769. MODULE_DESCRIPTION("External connector (extcon) class driver");
  770. MODULE_LICENSE("GPL");