extcon_class.c 20 KB

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