extcon-class.c 22 KB

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