extcon-class.c 24 KB

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