2 * drivers/extcon/extcon.c - External Connector (extcon) framework.
4 * External connector (extcon) class driver
6 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
9 * Copyright (C) 2012 Samsung Electronics
10 * Author: Donggeun Kim <dg77.kim@samsung.com>
11 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
13 * based on android/drivers/switch/switch_class.c
14 * Copyright (C) 2008 Google, Inc.
15 * Author: Mike Lockwood <lockwood@android.com>
17 * This software is licensed under the terms of the GNU General Public
18 * License version 2, as published by the Free Software Foundation, and
19 * may be copied, distributed, and modified under those terms.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/init.h>
30 #include <linux/device.h>
32 #include <linux/err.h>
33 #include <linux/extcon.h>
35 #include <linux/slab.h>
36 #include <linux/sysfs.h>
38 #define SUPPORTED_CABLE_MAX 32
39 #define CABLE_NAME_MAX 30
41 static const char *extcon_name[] = {
43 [EXTCON_USB_HOST] = "USB-Host",
45 [EXTCON_FAST_CHARGER] = "Fast-charger",
46 [EXTCON_SLOW_CHARGER] = "Slow-charger",
47 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
48 [EXTCON_LINE_IN] = "Line-in",
49 [EXTCON_LINE_OUT] = "Line-out",
50 [EXTCON_MICROPHONE] = "Microphone",
51 [EXTCON_HEADPHONE] = "Headphone",
52 [EXTCON_HDMI] = "HDMI",
56 [EXTCON_SPDIF_IN] = "SPDIF-in",
57 [EXTCON_SPDIF_OUT] = "SPDIF-out",
58 [EXTCON_VIDEO_IN] = "Video-in",
59 [EXTCON_VIDEO_OUT] = "Video-out",
60 [EXTCON_DOCK] = "Dock",
62 [EXTCON_MECHANICAL] = "Mechanical",
66 static struct class *extcon_class;
67 #if defined(CONFIG_ANDROID)
68 static struct class_compat *switch_class;
69 #endif /* CONFIG_ANDROID */
71 static LIST_HEAD(extcon_dev_list);
72 static DEFINE_MUTEX(extcon_dev_list_lock);
75 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
77 * @edev: the extcon device
78 * @new_state: new cable attach status for @edev
80 * Returns 0 if nothing violates. Returns the index + 1 for the first
83 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
87 if (!edev->mutually_exclusive)
90 for (i = 0; edev->mutually_exclusive[i]; i++) {
92 u32 correspondants = new_state & edev->mutually_exclusive[i];
94 /* calculate the total number of bits set */
95 weight = hweight32(correspondants);
103 static int find_cable_index_by_id(struct extcon_dev *edev, const enum extcon id)
107 /* Find the the index of extcon cable in edev->supported_cable */
108 for (i = 0; i < edev->max_supported; i++) {
109 if (edev->supported_cable[i] == id)
116 static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
118 enum extcon id = EXTCON_NONE;
121 if (edev->max_supported == 0)
124 /* Find the the number of extcon cable */
125 for (i = 0; i < EXTCON_END; i++) {
128 if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
134 if (id == EXTCON_NONE)
137 return find_cable_index_by_id(edev, id);
140 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
144 struct extcon_dev *edev = dev_get_drvdata(dev);
146 if (edev->print_state) {
147 int ret = edev->print_state(edev, buf);
151 /* Use default if failed */
154 if (edev->max_supported == 0)
155 return sprintf(buf, "%u\n", edev->state);
157 for (i = 0; i < edev->max_supported; i++) {
158 count += sprintf(buf + count, "%s=%d\n",
159 extcon_name[edev->supported_cable[i]],
160 !!(edev->state & (1 << i)));
166 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
171 struct extcon_dev *edev = dev_get_drvdata(dev);
173 ret = sscanf(buf, "0x%x", &state);
177 ret = extcon_set_state(edev, state);
184 static DEVICE_ATTR_RW(state);
186 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
189 struct extcon_dev *edev = dev_get_drvdata(dev);
191 /* Optional callback given by the user */
192 if (edev->print_name) {
193 int ret = edev->print_name(edev, buf);
199 return sprintf(buf, "%s\n", edev->name);
201 static DEVICE_ATTR_RO(name);
203 static ssize_t cable_name_show(struct device *dev,
204 struct device_attribute *attr, char *buf)
206 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
208 int i = cable->cable_index;
210 return sprintf(buf, "%s\n",
211 extcon_name[cable->edev->supported_cable[i]]);
214 static ssize_t cable_state_show(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
220 return sprintf(buf, "%d\n",
221 extcon_get_cable_state_(cable->edev,
222 cable->cable_index));
226 * extcon_update_state() - Update the cable attach states of the extcon device
227 * only for the masked bits.
228 * @edev: the extcon device
229 * @mask: the bit mask to designate updated bits.
230 * @state: new cable attach status for @edev
232 * Changing the state sends uevent with environment variable containing
233 * the name of extcon device (envp[0]) and the state output (envp[1]).
234 * Tizen uses this format for extcon device to get events from ports.
235 * Android uses this format as well.
237 * Note that the notifier provides which bits are changed in the state
238 * variable with the val parameter (second) to the callback.
240 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
250 spin_lock_irqsave(&edev->lock, flags);
252 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
253 u32 old_state = edev->state;
255 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
257 spin_unlock_irqrestore(&edev->lock, flags);
261 edev->state &= ~mask;
262 edev->state |= state & mask;
264 raw_notifier_call_chain(&edev->nh, old_state, edev);
265 /* This could be in interrupt handler */
266 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
268 length = name_show(&edev->dev, NULL, prop_buf);
270 if (prop_buf[length - 1] == '\n')
271 prop_buf[length - 1] = 0;
272 snprintf(name_buf, sizeof(name_buf),
273 "NAME=%s", prop_buf);
274 envp[env_offset++] = name_buf;
276 length = state_show(&edev->dev, NULL, prop_buf);
278 if (prop_buf[length - 1] == '\n')
279 prop_buf[length - 1] = 0;
280 snprintf(state_buf, sizeof(state_buf),
281 "STATE=%s", prop_buf);
282 envp[env_offset++] = state_buf;
284 envp[env_offset] = NULL;
285 /* Unlock early before uevent */
286 spin_unlock_irqrestore(&edev->lock, flags);
288 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
289 free_page((unsigned long)prop_buf);
291 /* Unlock early before uevent */
292 spin_unlock_irqrestore(&edev->lock, flags);
294 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
295 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
299 spin_unlock_irqrestore(&edev->lock, flags);
304 EXPORT_SYMBOL_GPL(extcon_update_state);
307 * extcon_set_state() - Set the cable attach states of the extcon device.
308 * @edev: the extcon device
309 * @state: new cable attach status for @edev
311 * Note that notifier provides which bits are changed in the state
312 * variable with the val parameter (second) to the callback.
314 int extcon_set_state(struct extcon_dev *edev, u32 state)
316 return extcon_update_state(edev, 0xffffffff, state);
318 EXPORT_SYMBOL_GPL(extcon_set_state);
321 * extcon_get_cable_state_() - Get the status of a specific cable.
322 * @edev: the extcon device that has the cable.
323 * @id: the unique id of each external connector in extcon enumeration.
325 int extcon_get_cable_state_(struct extcon_dev *edev, const enum extcon id)
329 index = find_cable_index_by_id(edev, id);
333 if (edev->max_supported && edev->max_supported <= index)
336 return !!(edev->state & (1 << index));
338 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
341 * extcon_get_cable_state() - Get the status of a specific cable.
342 * @edev: the extcon device that has the cable.
343 * @cable_name: cable name.
345 * Note that this is slower than extcon_get_cable_state_.
347 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
349 return extcon_get_cable_state_(edev, find_cable_index_by_name
352 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
355 * extcon_set_cable_state_() - Set the status of a specific cable.
356 * @edev: the extcon device that has the cable.
357 * @id: the unique id of each external connector
358 * in extcon enumeration.
359 * @state: the new cable status. The default semantics is
360 * true: attached / false: detached.
362 int extcon_set_cable_state_(struct extcon_dev *edev, enum extcon id,
368 index = find_cable_index_by_id(edev, id);
372 if (edev->max_supported && edev->max_supported <= index)
375 state = cable_state ? (1 << index) : 0;
376 return extcon_update_state(edev, 1 << index, state);
378 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
381 * extcon_set_cable_state() - Set the status of a specific cable.
382 * @edev: the extcon device that has the cable.
383 * @cable_name: cable name.
384 * @cable_state: the new cable status. The default semantics is
385 * true: attached / false: detached.
387 * Note that this is slower than extcon_set_cable_state_.
389 int extcon_set_cable_state(struct extcon_dev *edev,
390 const char *cable_name, bool cable_state)
392 return extcon_set_cable_state_(edev, find_cable_index_by_name
393 (edev, cable_name), cable_state);
395 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
398 * extcon_get_extcon_dev() - Get the extcon device instance from the name
399 * @extcon_name: The extcon name provided with extcon_dev_register()
401 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
403 struct extcon_dev *sd;
405 mutex_lock(&extcon_dev_list_lock);
406 list_for_each_entry(sd, &extcon_dev_list, entry) {
407 if (!strcmp(sd->name, extcon_name))
412 mutex_unlock(&extcon_dev_list_lock);
415 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
417 static int _call_per_cable(struct notifier_block *nb, unsigned long val,
420 struct extcon_specific_cable_nb *obj = container_of(nb,
421 struct extcon_specific_cable_nb, internal_nb);
422 struct extcon_dev *edev = ptr;
424 if ((val & (1 << obj->cable_index)) !=
425 (edev->state & (1 << obj->cable_index))) {
426 bool cable_state = true;
428 obj->previous_value = val;
430 if (val & (1 << obj->cable_index))
433 return obj->user_nb->notifier_call(obj->user_nb,
441 * extcon_register_interest() - Register a notifier for a state change of a
442 * specific cable, not an entier set of cables of a
444 * @obj: an empty extcon_specific_cable_nb object to be returned.
445 * @extcon_name: the name of extcon device.
446 * if NULL, extcon_register_interest will register
447 * every cable with the target cable_name given.
448 * @cable_name: the target cable name.
449 * @nb: the notifier block to get notified.
451 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
452 * the struct for you.
454 * extcon_register_interest is a helper function for those who want to get
455 * notification for a single specific cable's status change. If a user wants
456 * to get notification for any changes of all cables of a extcon device,
457 * he/she should use the general extcon_register_notifier().
459 * Note that the second parameter given to the callback of nb (val) is
460 * "old_state", not the current state. The current state can be retrieved
461 * by looking at the third pameter (edev pointer)'s state value.
463 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
464 const char *extcon_name, const char *cable_name,
465 struct notifier_block *nb)
470 if (!obj || !cable_name || !nb)
474 obj->edev = extcon_get_extcon_dev(extcon_name);
478 obj->cable_index = find_cable_index_by_name(obj->edev,
480 if (obj->cable_index < 0)
481 return obj->cable_index;
485 obj->internal_nb.notifier_call = _call_per_cable;
487 spin_lock_irqsave(&obj->edev->lock, flags);
488 ret = raw_notifier_chain_register(&obj->edev->nh,
490 spin_unlock_irqrestore(&obj->edev->lock, flags);
492 struct class_dev_iter iter;
493 struct extcon_dev *extd;
498 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
499 while ((dev = class_dev_iter_next(&iter))) {
500 extd = dev_get_drvdata(dev);
502 if (find_cable_index_by_name(extd, cable_name) < 0)
505 class_dev_iter_exit(&iter);
506 return extcon_register_interest(obj, extd->name,
515 EXPORT_SYMBOL_GPL(extcon_register_interest);
518 * extcon_unregister_interest() - Unregister the notifier registered by
519 * extcon_register_interest().
520 * @obj: the extcon_specific_cable_nb object returned by
521 * extcon_register_interest().
523 int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
531 spin_lock_irqsave(&obj->edev->lock, flags);
532 ret = raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
533 spin_unlock_irqrestore(&obj->edev->lock, flags);
537 EXPORT_SYMBOL_GPL(extcon_unregister_interest);
540 * extcon_register_notifier() - Register a notifiee to get notified by
541 * any attach status changes from the extcon.
542 * @edev: the extcon device.
543 * @nb: a notifier block to be registered.
545 * Note that the second parameter given to the callback of nb (val) is
546 * "old_state", not the current state. The current state can be retrieved
547 * by looking at the third pameter (edev pointer)'s state value.
549 int extcon_register_notifier(struct extcon_dev *edev,
550 struct notifier_block *nb)
555 spin_lock_irqsave(&edev->lock, flags);
556 ret = raw_notifier_chain_register(&edev->nh, nb);
557 spin_unlock_irqrestore(&edev->lock, flags);
561 EXPORT_SYMBOL_GPL(extcon_register_notifier);
564 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
565 * @edev: the extcon device.
566 * @nb: a registered notifier block to be unregistered.
568 int extcon_unregister_notifier(struct extcon_dev *edev,
569 struct notifier_block *nb)
574 spin_lock_irqsave(&edev->lock, flags);
575 ret = raw_notifier_chain_unregister(&edev->nh, nb);
576 spin_unlock_irqrestore(&edev->lock, flags);
580 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
582 static struct attribute *extcon_attrs[] = {
583 &dev_attr_state.attr,
587 ATTRIBUTE_GROUPS(extcon);
589 static int create_extcon_class(void)
592 extcon_class = class_create(THIS_MODULE, "extcon");
593 if (IS_ERR(extcon_class))
594 return PTR_ERR(extcon_class);
595 extcon_class->dev_groups = extcon_groups;
597 #if defined(CONFIG_ANDROID)
598 switch_class = class_compat_register("switch");
599 if (WARN(!switch_class, "cannot allocate"))
601 #endif /* CONFIG_ANDROID */
607 static void extcon_dev_release(struct device *dev)
611 static const char *muex_name = "mutually_exclusive";
612 static void dummy_sysfs_dev_release(struct device *dev)
617 * extcon_dev_allocate() - Allocate the memory of extcon device.
618 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
619 * If supported_cable is NULL, cable name related APIs
622 * This function allocates the memory for extcon device without allocating
623 * memory in each extcon provider driver and initialize default setting for
626 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
628 struct extcon_dev *extcon_dev_allocate(const enum extcon *supported_cable)
630 struct extcon_dev *edev;
632 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
634 return ERR_PTR(-ENOMEM);
636 edev->max_supported = 0;
637 edev->supported_cable = supported_cable;
643 * extcon_dev_free() - Free the memory of extcon device.
644 * @edev: the extcon device to free
646 void extcon_dev_free(struct extcon_dev *edev)
650 EXPORT_SYMBOL_GPL(extcon_dev_free);
652 static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
654 struct extcon_dev **r = res;
656 if (WARN_ON(!r || !*r))
662 static void devm_extcon_dev_release(struct device *dev, void *res)
664 extcon_dev_free(*(struct extcon_dev **)res);
668 * devm_extcon_dev_allocate - Allocate managed extcon device
669 * @dev: device owning the extcon device being created
670 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
671 * If supported_cable is NULL, cable name related APIs
674 * This function manages automatically the memory of extcon device using device
675 * resource management and simplify the control of freeing the memory of extcon
678 * Returns the pointer memory of allocated extcon_dev if success
679 * or ERR_PTR(err) if fail
681 struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
682 const enum extcon *supported_cable)
684 struct extcon_dev **ptr, *edev;
686 ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
688 return ERR_PTR(-ENOMEM);
690 edev = extcon_dev_allocate(supported_cable);
696 edev->dev.parent = dev;
699 devres_add(dev, ptr);
703 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
705 void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
707 WARN_ON(devres_release(dev, devm_extcon_dev_release,
708 devm_extcon_dev_match, edev));
710 EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
713 * extcon_dev_register() - Register a new extcon device
714 * @edev : the new extcon device (should be allocated before calling)
716 * Among the members of edev struct, please set the "user initializing data"
717 * in any case and set the "optional callbacks" if required. However, please
718 * do not set the values of "internal data", which are initialized by
721 int extcon_dev_register(struct extcon_dev *edev)
724 static atomic_t edev_no = ATOMIC_INIT(-1);
727 ret = create_extcon_class();
732 if (!edev->supported_cable)
735 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
737 edev->max_supported = index;
738 if (index > SUPPORTED_CABLE_MAX) {
740 "exceed the maximum number of supported cables\n");
744 edev->dev.class = extcon_class;
745 edev->dev.release = extcon_dev_release;
747 edev->name = dev_name(edev->dev.parent);
748 if (IS_ERR_OR_NULL(edev->name)) {
750 "extcon device name is null\n");
753 dev_set_name(&edev->dev, "extcon%lu",
754 (unsigned long)atomic_inc_return(&edev_no));
756 if (edev->max_supported) {
759 struct extcon_cable *cable;
761 edev->cables = kzalloc(sizeof(struct extcon_cable) *
762 edev->max_supported, GFP_KERNEL);
765 goto err_sysfs_alloc;
767 for (index = 0; index < edev->max_supported; index++) {
768 cable = &edev->cables[index];
770 snprintf(buf, 10, "cable.%d", index);
771 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
774 for (index--; index >= 0; index--) {
775 cable = &edev->cables[index];
776 kfree(cable->attr_g.name);
780 goto err_alloc_cables;
785 cable->cable_index = index;
786 cable->attrs[0] = &cable->attr_name.attr;
787 cable->attrs[1] = &cable->attr_state.attr;
788 cable->attrs[2] = NULL;
789 cable->attr_g.name = str;
790 cable->attr_g.attrs = cable->attrs;
792 sysfs_attr_init(&cable->attr_name.attr);
793 cable->attr_name.attr.name = "name";
794 cable->attr_name.attr.mode = 0444;
795 cable->attr_name.show = cable_name_show;
797 sysfs_attr_init(&cable->attr_state.attr);
798 cable->attr_state.attr.name = "state";
799 cable->attr_state.attr.mode = 0444;
800 cable->attr_state.show = cable_state_show;
804 if (edev->max_supported && edev->mutually_exclusive) {
808 /* Count the size of mutually_exclusive array */
809 for (index = 0; edev->mutually_exclusive[index]; index++)
812 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
813 (index + 1), GFP_KERNEL);
814 if (!edev->attrs_muex) {
819 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
821 if (!edev->d_attrs_muex) {
823 kfree(edev->attrs_muex);
827 for (index = 0; edev->mutually_exclusive[index]; index++) {
828 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
829 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
832 for (index--; index >= 0; index--) {
833 kfree(edev->d_attrs_muex[index].attr.
836 kfree(edev->d_attrs_muex);
837 kfree(edev->attrs_muex);
842 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
843 edev->d_attrs_muex[index].attr.name = name;
844 edev->d_attrs_muex[index].attr.mode = 0000;
845 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
848 edev->attr_g_muex.name = muex_name;
849 edev->attr_g_muex.attrs = edev->attrs_muex;
853 if (edev->max_supported) {
854 edev->extcon_dev_type.groups =
855 kzalloc(sizeof(struct attribute_group *) *
856 (edev->max_supported + 2), GFP_KERNEL);
857 if (!edev->extcon_dev_type.groups) {
859 goto err_alloc_groups;
862 edev->extcon_dev_type.name = dev_name(&edev->dev);
863 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
865 for (index = 0; index < edev->max_supported; index++)
866 edev->extcon_dev_type.groups[index] =
867 &edev->cables[index].attr_g;
868 if (edev->mutually_exclusive)
869 edev->extcon_dev_type.groups[index] =
872 edev->dev.type = &edev->extcon_dev_type;
875 ret = device_register(&edev->dev);
877 put_device(&edev->dev);
880 #if defined(CONFIG_ANDROID)
882 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
883 #endif /* CONFIG_ANDROID */
885 spin_lock_init(&edev->lock);
887 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
889 dev_set_drvdata(&edev->dev, edev);
892 mutex_lock(&extcon_dev_list_lock);
893 list_add(&edev->entry, &extcon_dev_list);
894 mutex_unlock(&extcon_dev_list_lock);
899 if (edev->max_supported)
900 kfree(edev->extcon_dev_type.groups);
902 if (edev->max_supported && edev->mutually_exclusive) {
903 for (index = 0; edev->mutually_exclusive[index]; index++)
904 kfree(edev->d_attrs_muex[index].attr.name);
905 kfree(edev->d_attrs_muex);
906 kfree(edev->attrs_muex);
909 for (index = 0; index < edev->max_supported; index++)
910 kfree(edev->cables[index].attr_g.name);
912 if (edev->max_supported)
917 EXPORT_SYMBOL_GPL(extcon_dev_register);
920 * extcon_dev_unregister() - Unregister the extcon device.
921 * @edev: the extcon device instance to be unregistered.
923 * Note that this does not call kfree(edev) because edev was not allocated
926 void extcon_dev_unregister(struct extcon_dev *edev)
930 mutex_lock(&extcon_dev_list_lock);
931 list_del(&edev->entry);
932 mutex_unlock(&extcon_dev_list_lock);
934 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
935 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
936 dev_name(&edev->dev));
940 device_unregister(&edev->dev);
942 if (edev->mutually_exclusive && edev->max_supported) {
943 for (index = 0; edev->mutually_exclusive[index];
945 kfree(edev->d_attrs_muex[index].attr.name);
946 kfree(edev->d_attrs_muex);
947 kfree(edev->attrs_muex);
950 for (index = 0; index < edev->max_supported; index++)
951 kfree(edev->cables[index].attr_g.name);
953 if (edev->max_supported) {
954 kfree(edev->extcon_dev_type.groups);
958 #if defined(CONFIG_ANDROID)
960 class_compat_remove_link(switch_class, &edev->dev, NULL);
962 put_device(&edev->dev);
964 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
966 static void devm_extcon_dev_unreg(struct device *dev, void *res)
968 extcon_dev_unregister(*(struct extcon_dev **)res);
972 * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
973 * @dev: device to allocate extcon device
974 * @edev: the new extcon device to register
976 * Managed extcon_dev_register() function. If extcon device is attached with
977 * this function, that extcon device is automatically unregistered on driver
978 * detach. Internally this function calls extcon_dev_register() function.
979 * To get more information, refer that function.
981 * If extcon device is registered with this function and the device needs to be
982 * unregistered separately, devm_extcon_dev_unregister() should be used.
984 * Returns 0 if success or negaive error number if failure.
986 int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
988 struct extcon_dev **ptr;
991 ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
995 ret = extcon_dev_register(edev);
1002 devres_add(dev, ptr);
1006 EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
1009 * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
1010 * @dev: device the extcon belongs to
1011 * @edev: the extcon device to unregister
1013 * Unregister extcon device that is registered with devm_extcon_dev_register()
1016 void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
1018 WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
1019 devm_extcon_dev_match, edev));
1021 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
1025 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1026 * @dev - instance to the given device
1027 * @index - index into list of extcon_dev
1029 * return the instance of extcon device
1031 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1033 struct device_node *node;
1034 struct extcon_dev *edev;
1036 if (!dev->of_node) {
1037 dev_err(dev, "device does not have a device node entry\n");
1038 return ERR_PTR(-EINVAL);
1041 node = of_parse_phandle(dev->of_node, "extcon", index);
1043 dev_err(dev, "failed to get phandle in %s node\n",
1044 dev->of_node->full_name);
1045 return ERR_PTR(-ENODEV);
1048 mutex_lock(&extcon_dev_list_lock);
1049 list_for_each_entry(edev, &extcon_dev_list, entry) {
1050 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1051 mutex_unlock(&extcon_dev_list_lock);
1055 mutex_unlock(&extcon_dev_list_lock);
1057 return ERR_PTR(-EPROBE_DEFER);
1060 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1062 return ERR_PTR(-ENOSYS);
1064 #endif /* CONFIG_OF */
1065 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1068 * extcon_get_edev_name() - Get the name of the extcon device.
1069 * @edev: the extcon device
1071 const char *extcon_get_edev_name(struct extcon_dev *edev)
1073 return !edev ? NULL : edev->name;
1076 static int __init extcon_class_init(void)
1078 return create_extcon_class();
1080 module_init(extcon_class_init);
1082 static void __exit extcon_class_exit(void)
1084 #if defined(CONFIG_ANDROID)
1085 class_compat_unregister(switch_class);
1087 class_destroy(extcon_class);
1089 module_exit(extcon_class_exit);
1091 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1092 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1093 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1094 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1095 MODULE_DESCRIPTION("External connector (extcon) class driver");
1096 MODULE_LICENSE("GPL");