extcon_class.c 22 KB

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