extcon-class.c 23 KB

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