extcon_class.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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. /*
  33. * extcon_cable_name suggests the standard cable names for commonly used
  34. * cable types.
  35. *
  36. * However, please do not use extcon_cable_name directly for extcon_dev
  37. * struct's supported_cable pointer unless your device really supports
  38. * every single port-type of the following cable names. Please choose cable
  39. * names that are actually used in your extcon device.
  40. */
  41. const char *extcon_cable_name[] = {
  42. [EXTCON_USB] = "USB",
  43. [EXTCON_USB_HOST] = "USB-Host",
  44. [EXTCON_TA] = "TA",
  45. [EXTCON_FAST_CHARGER] = "Fast-charger",
  46. [EXTCON_SLOW_CHARGER] = "Slow-charger",
  47. [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
  48. [EXTCON_HDMI] = "HDMI",
  49. [EXTCON_MHL] = "MHL",
  50. [EXTCON_DVI] = "DVI",
  51. [EXTCON_VGA] = "VGA",
  52. [EXTCON_DOCK] = "Dock",
  53. [EXTCON_LINE_IN] = "Line-in",
  54. [EXTCON_LINE_OUT] = "Line-out",
  55. [EXTCON_MIC_IN] = "Microphone",
  56. [EXTCON_HEADPHONE_OUT] = "Headphone",
  57. [EXTCON_SPDIF_IN] = "SPDIF-in",
  58. [EXTCON_SPDIF_OUT] = "SPDIF-out",
  59. [EXTCON_VIDEO_IN] = "Video-in",
  60. [EXTCON_VIDEO_OUT] = "Video-out",
  61. NULL,
  62. };
  63. struct class *extcon_class;
  64. #if defined(CONFIG_ANDROID)
  65. static struct class_compat *switch_class;
  66. #endif /* CONFIG_ANDROID */
  67. static LIST_HEAD(extcon_dev_list);
  68. static DEFINE_MUTEX(extcon_dev_list_lock);
  69. /**
  70. * check_mutually_exclusive - Check if new_state violates mutually_exclusive
  71. * condition.
  72. * @edev: the extcon device
  73. * @new_state: new cable attach status for @edev
  74. *
  75. * Returns 0 if nothing violates. Returns the index + 1 for the first
  76. * violated condition.
  77. */
  78. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  79. {
  80. int i = 0;
  81. if (!edev->mutually_exclusive)
  82. return 0;
  83. for (i = 0; edev->mutually_exclusive[i]; i++) {
  84. int count = 0, j;
  85. u32 correspondants = new_state & edev->mutually_exclusive[i];
  86. u32 exp = 1;
  87. for (j = 0; j < 32; j++) {
  88. if (exp & correspondants)
  89. count++;
  90. if (count > 1)
  91. return i + 1;
  92. exp <<= 1;
  93. }
  94. }
  95. return 0;
  96. }
  97. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  98. char *buf)
  99. {
  100. int i, count = 0;
  101. struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
  102. if (edev->print_state) {
  103. int ret = edev->print_state(edev, buf);
  104. if (ret >= 0)
  105. return ret;
  106. /* Use default if failed */
  107. }
  108. if (edev->max_supported == 0)
  109. return sprintf(buf, "%u\n", edev->state);
  110. for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
  111. if (!edev->supported_cable[i])
  112. break;
  113. count += sprintf(buf + count, "%s=%d\n",
  114. edev->supported_cable[i],
  115. !!(edev->state & (1 << i)));
  116. }
  117. return count;
  118. }
  119. int extcon_set_state(struct extcon_dev *edev, u32 state);
  120. static ssize_t state_store(struct device *dev, struct device_attribute *attr,
  121. const char *buf, size_t count)
  122. {
  123. u32 state;
  124. ssize_t ret = 0;
  125. struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
  126. ret = sscanf(buf, "0x%x", &state);
  127. if (ret == 0)
  128. ret = -EINVAL;
  129. else
  130. ret = extcon_set_state(edev, state);
  131. if (ret < 0)
  132. return ret;
  133. return count;
  134. }
  135. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  136. char *buf)
  137. {
  138. struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
  139. /* Optional callback given by the user */
  140. if (edev->print_name) {
  141. int ret = edev->print_name(edev, buf);
  142. if (ret >= 0)
  143. return ret;
  144. }
  145. return sprintf(buf, "%s\n", dev_name(edev->dev));
  146. }
  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. static ssize_t cable_state_store(struct device *dev,
  165. struct device_attribute *attr, const char *buf,
  166. size_t count)
  167. {
  168. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  169. attr_state);
  170. int ret, state;
  171. ret = sscanf(buf, "%d", &state);
  172. if (ret == 0)
  173. ret = -EINVAL;
  174. else
  175. ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
  176. state);
  177. if (ret < 0)
  178. return ret;
  179. return count;
  180. }
  181. /**
  182. * extcon_update_state() - Update the cable attach states of the extcon device
  183. * only for the masked bits.
  184. * @edev: the extcon device
  185. * @mask: the bit mask to designate updated bits.
  186. * @state: new cable attach status for @edev
  187. *
  188. * Changing the state sends uevent with environment variable containing
  189. * the name of extcon device (envp[0]) and the state output (envp[1]).
  190. * Tizen uses this format for extcon device to get events from ports.
  191. * Android uses this format as well.
  192. *
  193. * Note that the notifier provides which bits are changed in the state
  194. * variable with the val parameter (second) to the callback.
  195. */
  196. int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
  197. {
  198. char name_buf[120];
  199. char state_buf[120];
  200. char *prop_buf;
  201. char *envp[3];
  202. int env_offset = 0;
  203. int length;
  204. unsigned long flags;
  205. spin_lock_irqsave(&edev->lock, flags);
  206. if (edev->state != ((edev->state & ~mask) | (state & mask))) {
  207. u32 old_state = edev->state;
  208. if (check_mutually_exclusive(edev, (edev->state & ~mask) |
  209. (state & mask))) {
  210. spin_unlock_irqrestore(&edev->lock, flags);
  211. return -EPERM;
  212. }
  213. edev->state &= ~mask;
  214. edev->state |= state & mask;
  215. raw_notifier_call_chain(&edev->nh, old_state, edev);
  216. /* This could be in interrupt handler */
  217. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  218. if (prop_buf) {
  219. length = name_show(edev->dev, NULL, prop_buf);
  220. if (length > 0) {
  221. if (prop_buf[length - 1] == '\n')
  222. prop_buf[length - 1] = 0;
  223. snprintf(name_buf, sizeof(name_buf),
  224. "NAME=%s", prop_buf);
  225. envp[env_offset++] = name_buf;
  226. }
  227. length = state_show(edev->dev, NULL, prop_buf);
  228. if (length > 0) {
  229. if (prop_buf[length - 1] == '\n')
  230. prop_buf[length - 1] = 0;
  231. snprintf(state_buf, sizeof(state_buf),
  232. "STATE=%s", prop_buf);
  233. envp[env_offset++] = state_buf;
  234. }
  235. envp[env_offset] = NULL;
  236. /* Unlock early before uevent */
  237. spin_unlock_irqrestore(&edev->lock, flags);
  238. kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
  239. free_page((unsigned long)prop_buf);
  240. } else {
  241. /* Unlock early before uevent */
  242. spin_unlock_irqrestore(&edev->lock, flags);
  243. dev_err(edev->dev, "out of memory in extcon_set_state\n");
  244. kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
  245. }
  246. } else {
  247. /* No changes */
  248. spin_unlock_irqrestore(&edev->lock, flags);
  249. }
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(extcon_update_state);
  253. /**
  254. * extcon_set_state() - Set the cable attach states of the extcon device.
  255. * @edev: the extcon device
  256. * @state: new cable attach status for @edev
  257. *
  258. * Note that notifier provides which bits are changed in the state
  259. * variable with the val parameter (second) to the callback.
  260. */
  261. int extcon_set_state(struct extcon_dev *edev, u32 state)
  262. {
  263. return extcon_update_state(edev, 0xffffffff, state);
  264. }
  265. EXPORT_SYMBOL_GPL(extcon_set_state);
  266. /**
  267. * extcon_find_cable_index() - Get the cable index based on the cable name.
  268. * @edev: the extcon device that has the cable.
  269. * @cable_name: cable name to be searched.
  270. *
  271. * Note that accessing a cable state based on cable_index is faster than
  272. * cable_name because using cable_name induces a loop with strncmp().
  273. * Thus, when get/set_cable_state is repeatedly used, using cable_index
  274. * is recommended.
  275. */
  276. int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
  277. {
  278. int i;
  279. if (edev->supported_cable) {
  280. for (i = 0; edev->supported_cable[i]; i++) {
  281. if (!strncmp(edev->supported_cable[i],
  282. cable_name, CABLE_NAME_MAX))
  283. return i;
  284. }
  285. }
  286. return -EINVAL;
  287. }
  288. EXPORT_SYMBOL_GPL(extcon_find_cable_index);
  289. /**
  290. * extcon_get_cable_state_() - Get the status of a specific cable.
  291. * @edev: the extcon device that has the cable.
  292. * @index: cable index that can be retrieved by extcon_find_cable_index().
  293. */
  294. int extcon_get_cable_state_(struct extcon_dev *edev, int index)
  295. {
  296. if (index < 0 || (edev->max_supported && edev->max_supported <= index))
  297. return -EINVAL;
  298. return !!(edev->state & (1 << index));
  299. }
  300. EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
  301. /**
  302. * extcon_get_cable_state() - Get the status of a specific cable.
  303. * @edev: the extcon device that has the cable.
  304. * @cable_name: cable name.
  305. *
  306. * Note that this is slower than extcon_get_cable_state_.
  307. */
  308. int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
  309. {
  310. return extcon_get_cable_state_(edev, extcon_find_cable_index
  311. (edev, cable_name));
  312. }
  313. EXPORT_SYMBOL_GPL(extcon_get_cable_state);
  314. /**
  315. * extcon_get_cable_state_() - Set the status of a specific cable.
  316. * @edev: the extcon device that has the cable.
  317. * @index: cable index that can be retrieved by extcon_find_cable_index().
  318. * @cable_state: the new cable status. The default semantics is
  319. * true: attached / false: detached.
  320. */
  321. int extcon_set_cable_state_(struct extcon_dev *edev,
  322. int index, bool cable_state)
  323. {
  324. u32 state;
  325. if (index < 0 || (edev->max_supported && edev->max_supported <= index))
  326. return -EINVAL;
  327. state = cable_state ? (1 << index) : 0;
  328. return extcon_update_state(edev, 1 << index, state);
  329. }
  330. EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
  331. /**
  332. * extcon_get_cable_state() - Set the status of a specific cable.
  333. * @edev: the extcon device that has the cable.
  334. * @cable_name: cable name.
  335. * @cable_state: the new cable status. The default semantics is
  336. * true: attached / false: detached.
  337. *
  338. * Note that this is slower than extcon_set_cable_state_.
  339. */
  340. int extcon_set_cable_state(struct extcon_dev *edev,
  341. const char *cable_name, bool cable_state)
  342. {
  343. return extcon_set_cable_state_(edev, extcon_find_cable_index
  344. (edev, cable_name), cable_state);
  345. }
  346. EXPORT_SYMBOL_GPL(extcon_set_cable_state);
  347. /**
  348. * extcon_get_extcon_dev() - Get the extcon device instance from the name
  349. * @extcon_name: The extcon name provided with extcon_dev_register()
  350. */
  351. struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
  352. {
  353. struct extcon_dev *sd;
  354. mutex_lock(&extcon_dev_list_lock);
  355. list_for_each_entry(sd, &extcon_dev_list, entry) {
  356. if (!strcmp(sd->name, extcon_name))
  357. goto out;
  358. }
  359. sd = NULL;
  360. out:
  361. mutex_unlock(&extcon_dev_list_lock);
  362. return sd;
  363. }
  364. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  365. static int _call_per_cable(struct notifier_block *nb, unsigned long val,
  366. void *ptr)
  367. {
  368. struct extcon_specific_cable_nb *obj = container_of(nb,
  369. struct extcon_specific_cable_nb, internal_nb);
  370. struct extcon_dev *edev = ptr;
  371. if ((val & (1 << obj->cable_index)) !=
  372. (edev->state & (1 << obj->cable_index))) {
  373. obj->previous_value = val;
  374. return obj->user_nb->notifier_call(obj->user_nb, val, ptr);
  375. }
  376. return NOTIFY_OK;
  377. }
  378. /**
  379. * extcon_register_interest() - Register a notifier for a state change of a
  380. * specific cable, not a entier set of cables of a
  381. * extcon device.
  382. * @obj: an empty extcon_specific_cable_nb object to be returned.
  383. * @extcon_name: the name of extcon device.
  384. * @cable_name: the target cable name.
  385. * @nb: the notifier block to get notified.
  386. *
  387. * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
  388. * the struct for you.
  389. *
  390. * extcon_register_interest is a helper function for those who want to get
  391. * notification for a single specific cable's status change. If a user wants
  392. * to get notification for any changes of all cables of a extcon device,
  393. * he/she should use the general extcon_register_notifier().
  394. *
  395. * Note that the second parameter given to the callback of nb (val) is
  396. * "old_state", not the current state. The current state can be retrieved
  397. * by looking at the third pameter (edev pointer)'s state value.
  398. */
  399. int extcon_register_interest(struct extcon_specific_cable_nb *obj,
  400. const char *extcon_name, const char *cable_name,
  401. struct notifier_block *nb)
  402. {
  403. if (!obj || !extcon_name || !cable_name || !nb)
  404. return -EINVAL;
  405. obj->edev = extcon_get_extcon_dev(extcon_name);
  406. if (!obj->edev)
  407. return -ENODEV;
  408. obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
  409. if (obj->cable_index < 0)
  410. return -ENODEV;
  411. obj->user_nb = nb;
  412. obj->internal_nb.notifier_call = _call_per_cable;
  413. return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
  414. }
  415. /**
  416. * extcon_unregister_interest() - Unregister the notifier registered by
  417. * extcon_register_interest().
  418. * @obj: the extcon_specific_cable_nb object returned by
  419. * extcon_register_interest().
  420. */
  421. int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
  422. {
  423. if (!obj)
  424. return -EINVAL;
  425. return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
  426. }
  427. /**
  428. * extcon_register_notifier() - Register a notifee to get notified by
  429. * any attach status changes from the extcon.
  430. * @edev: the extcon device.
  431. * @nb: a notifier block to be registered.
  432. *
  433. * Note that the second parameter given to the callback of nb (val) is
  434. * "old_state", not the current state. The current state can be retrieved
  435. * by looking at the third pameter (edev pointer)'s state value.
  436. */
  437. int extcon_register_notifier(struct extcon_dev *edev,
  438. struct notifier_block *nb)
  439. {
  440. return raw_notifier_chain_register(&edev->nh, nb);
  441. }
  442. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  443. /**
  444. * extcon_unregister_notifier() - Unregister a notifee from the extcon device.
  445. * @edev: the extcon device.
  446. * @nb: a registered notifier block to be unregistered.
  447. */
  448. int extcon_unregister_notifier(struct extcon_dev *edev,
  449. struct notifier_block *nb)
  450. {
  451. return raw_notifier_chain_unregister(&edev->nh, nb);
  452. }
  453. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  454. static struct device_attribute extcon_attrs[] = {
  455. __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
  456. __ATTR_RO(name),
  457. __ATTR_NULL,
  458. };
  459. static int create_extcon_class(void)
  460. {
  461. if (!extcon_class) {
  462. extcon_class = class_create(THIS_MODULE, "extcon");
  463. if (IS_ERR(extcon_class))
  464. return PTR_ERR(extcon_class);
  465. extcon_class->dev_attrs = extcon_attrs;
  466. #if defined(CONFIG_ANDROID)
  467. switch_class = class_compat_register("switch");
  468. if (WARN(!switch_class, "cannot allocate"))
  469. return -ENOMEM;
  470. #endif /* CONFIG_ANDROID */
  471. }
  472. return 0;
  473. }
  474. static void extcon_cleanup(struct extcon_dev *edev, bool skip)
  475. {
  476. mutex_lock(&extcon_dev_list_lock);
  477. list_del(&edev->entry);
  478. mutex_unlock(&extcon_dev_list_lock);
  479. if (!skip && get_device(edev->dev)) {
  480. int index;
  481. if (edev->mutually_exclusive && edev->max_supported) {
  482. for (index = 0; edev->mutually_exclusive[index];
  483. index++)
  484. kfree(edev->d_attrs_muex[index].attr.name);
  485. kfree(edev->d_attrs_muex);
  486. kfree(edev->attrs_muex);
  487. }
  488. for (index = 0; index < edev->max_supported; index++)
  489. kfree(edev->cables[index].attr_g.name);
  490. if (edev->max_supported) {
  491. kfree(edev->extcon_dev_type.groups);
  492. kfree(edev->cables);
  493. }
  494. device_unregister(edev->dev);
  495. put_device(edev->dev);
  496. }
  497. kfree(edev->dev);
  498. }
  499. static void extcon_dev_release(struct device *dev)
  500. {
  501. struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
  502. extcon_cleanup(edev, true);
  503. }
  504. static const char *muex_name = "mutually_exclusive";
  505. static void dummy_sysfs_dev_release(struct device *dev)
  506. {
  507. }
  508. /**
  509. * extcon_dev_register() - Register a new extcon device
  510. * @edev : the new extcon device (should be allocated before calling)
  511. * @dev : the parent device for this extcon device.
  512. *
  513. * Among the members of edev struct, please set the "user initializing data"
  514. * in any case and set the "optional callbacks" if required. However, please
  515. * do not set the values of "internal data", which are initialized by
  516. * this function.
  517. */
  518. int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
  519. {
  520. int ret, index = 0;
  521. if (!extcon_class) {
  522. ret = create_extcon_class();
  523. if (ret < 0)
  524. return ret;
  525. }
  526. if (edev->supported_cable) {
  527. /* Get size of array */
  528. for (index = 0; edev->supported_cable[index]; index++)
  529. ;
  530. edev->max_supported = index;
  531. } else {
  532. edev->max_supported = 0;
  533. }
  534. if (index > SUPPORTED_CABLE_MAX) {
  535. dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
  536. return -EINVAL;
  537. }
  538. edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  539. if (!edev->dev)
  540. return -ENOMEM;
  541. edev->dev->parent = dev;
  542. edev->dev->class = extcon_class;
  543. edev->dev->release = extcon_dev_release;
  544. dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
  545. if (edev->max_supported) {
  546. char buf[10];
  547. char *str;
  548. struct extcon_cable *cable;
  549. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  550. edev->max_supported, GFP_KERNEL);
  551. if (!edev->cables) {
  552. ret = -ENOMEM;
  553. goto err_sysfs_alloc;
  554. }
  555. for (index = 0; index < edev->max_supported; index++) {
  556. cable = &edev->cables[index];
  557. snprintf(buf, 10, "cable.%d", index);
  558. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  559. GFP_KERNEL);
  560. if (!str) {
  561. for (index--; index >= 0; index--) {
  562. cable = &edev->cables[index];
  563. kfree(cable->attr_g.name);
  564. }
  565. ret = -ENOMEM;
  566. goto err_alloc_cables;
  567. }
  568. strcpy(str, buf);
  569. cable->edev = edev;
  570. cable->cable_index = index;
  571. cable->attrs[0] = &cable->attr_name.attr;
  572. cable->attrs[1] = &cable->attr_state.attr;
  573. cable->attrs[2] = NULL;
  574. cable->attr_g.name = str;
  575. cable->attr_g.attrs = cable->attrs;
  576. cable->attr_name.attr.name = "name";
  577. cable->attr_name.attr.mode = 0444;
  578. cable->attr_name.show = cable_name_show;
  579. cable->attr_state.attr.name = "state";
  580. cable->attr_state.attr.mode = 0644;
  581. cable->attr_state.show = cable_state_show;
  582. cable->attr_state.store = cable_state_store;
  583. }
  584. }
  585. if (edev->max_supported && edev->mutually_exclusive) {
  586. char buf[80];
  587. char *name;
  588. /* Count the size of mutually_exclusive array */
  589. for (index = 0; edev->mutually_exclusive[index]; index++)
  590. ;
  591. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  592. (index + 1), GFP_KERNEL);
  593. if (!edev->attrs_muex) {
  594. ret = -ENOMEM;
  595. goto err_muex;
  596. }
  597. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  598. index, GFP_KERNEL);
  599. if (!edev->d_attrs_muex) {
  600. ret = -ENOMEM;
  601. kfree(edev->attrs_muex);
  602. goto err_muex;
  603. }
  604. for (index = 0; edev->mutually_exclusive[index]; index++) {
  605. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  606. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  607. GFP_KERNEL);
  608. if (!name) {
  609. for (index--; index >= 0; index--) {
  610. kfree(edev->d_attrs_muex[index].attr.
  611. name);
  612. }
  613. kfree(edev->d_attrs_muex);
  614. kfree(edev->attrs_muex);
  615. ret = -ENOMEM;
  616. goto err_muex;
  617. }
  618. strcpy(name, buf);
  619. edev->d_attrs_muex[index].attr.name = name;
  620. edev->d_attrs_muex[index].attr.mode = 0000;
  621. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  622. .attr;
  623. }
  624. edev->attr_g_muex.name = muex_name;
  625. edev->attr_g_muex.attrs = edev->attrs_muex;
  626. }
  627. if (edev->max_supported) {
  628. edev->extcon_dev_type.groups =
  629. kzalloc(sizeof(struct attribute_group *) *
  630. (edev->max_supported + 2), GFP_KERNEL);
  631. if (!edev->extcon_dev_type.groups) {
  632. ret = -ENOMEM;
  633. goto err_alloc_groups;
  634. }
  635. edev->extcon_dev_type.name = dev_name(edev->dev);
  636. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  637. for (index = 0; index < edev->max_supported; index++)
  638. edev->extcon_dev_type.groups[index] =
  639. &edev->cables[index].attr_g;
  640. if (edev->mutually_exclusive)
  641. edev->extcon_dev_type.groups[index] =
  642. &edev->attr_g_muex;
  643. edev->dev->type = &edev->extcon_dev_type;
  644. }
  645. ret = device_register(edev->dev);
  646. if (ret) {
  647. put_device(edev->dev);
  648. goto err_dev;
  649. }
  650. #if defined(CONFIG_ANDROID)
  651. if (switch_class)
  652. ret = class_compat_create_link(switch_class, edev->dev,
  653. dev);
  654. #endif /* CONFIG_ANDROID */
  655. spin_lock_init(&edev->lock);
  656. RAW_INIT_NOTIFIER_HEAD(&edev->nh);
  657. dev_set_drvdata(edev->dev, edev);
  658. edev->state = 0;
  659. mutex_lock(&extcon_dev_list_lock);
  660. list_add(&edev->entry, &extcon_dev_list);
  661. mutex_unlock(&extcon_dev_list_lock);
  662. return 0;
  663. err_dev:
  664. if (edev->max_supported)
  665. kfree(edev->extcon_dev_type.groups);
  666. err_alloc_groups:
  667. if (edev->max_supported && edev->mutually_exclusive) {
  668. for (index = 0; edev->mutually_exclusive[index]; index++)
  669. kfree(edev->d_attrs_muex[index].attr.name);
  670. kfree(edev->d_attrs_muex);
  671. kfree(edev->attrs_muex);
  672. }
  673. err_muex:
  674. for (index = 0; index < edev->max_supported; index++)
  675. kfree(edev->cables[index].attr_g.name);
  676. err_alloc_cables:
  677. if (edev->max_supported)
  678. kfree(edev->cables);
  679. err_sysfs_alloc:
  680. kfree(edev->dev);
  681. return ret;
  682. }
  683. EXPORT_SYMBOL_GPL(extcon_dev_register);
  684. /**
  685. * extcon_dev_unregister() - Unregister the extcon device.
  686. * @edev: the extcon device instance to be unregitered.
  687. *
  688. * Note that this does not call kfree(edev) because edev was not allocated
  689. * by this class.
  690. */
  691. void extcon_dev_unregister(struct extcon_dev *edev)
  692. {
  693. extcon_cleanup(edev, false);
  694. }
  695. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  696. static int __init extcon_class_init(void)
  697. {
  698. return create_extcon_class();
  699. }
  700. module_init(extcon_class_init);
  701. static void __exit extcon_class_exit(void)
  702. {
  703. class_destroy(extcon_class);
  704. }
  705. module_exit(extcon_class_exit);
  706. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  707. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  708. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  709. MODULE_DESCRIPTION("External connector (extcon) class driver");
  710. MODULE_LICENSE("GPL");