extcon: adc-jack: Use power efficient workqueue
[linux/fpc-iii.git] / drivers / extcon / extcon-class.c
blob1446152bf2e9a618b156c135f9bc3d849664dfae
1 /*
2 * drivers/extcon/extcon_class.c
4 * External connector (extcon) class driver
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
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.
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.
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
29 #include <linux/fs.h>
30 #include <linux/err.h>
31 #include <linux/extcon.h>
32 #include <linux/slab.h>
33 #include <linux/sysfs.h>
36 * extcon_cable_name suggests the standard cable names for commonly used
37 * cable types.
39 * However, please do not use extcon_cable_name directly for extcon_dev
40 * struct's supported_cable pointer unless your device really supports
41 * every single port-type of the following cable names. Please choose cable
42 * names that are actually used in your extcon device.
44 const char extcon_cable_name[][CABLE_NAME_MAX + 1] = {
45 [EXTCON_USB] = "USB",
46 [EXTCON_USB_HOST] = "USB-Host",
47 [EXTCON_TA] = "TA",
48 [EXTCON_FAST_CHARGER] = "Fast-charger",
49 [EXTCON_SLOW_CHARGER] = "Slow-charger",
50 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
51 [EXTCON_HDMI] = "HDMI",
52 [EXTCON_MHL] = "MHL",
53 [EXTCON_DVI] = "DVI",
54 [EXTCON_VGA] = "VGA",
55 [EXTCON_DOCK] = "Dock",
56 [EXTCON_LINE_IN] = "Line-in",
57 [EXTCON_LINE_OUT] = "Line-out",
58 [EXTCON_MIC_IN] = "Microphone",
59 [EXTCON_HEADPHONE_OUT] = "Headphone",
60 [EXTCON_SPDIF_IN] = "SPDIF-in",
61 [EXTCON_SPDIF_OUT] = "SPDIF-out",
62 [EXTCON_VIDEO_IN] = "Video-in",
63 [EXTCON_VIDEO_OUT] = "Video-out",
64 [EXTCON_MECHANICAL] = "Mechanical",
67 static struct class *extcon_class;
68 #if defined(CONFIG_ANDROID)
69 static struct class_compat *switch_class;
70 #endif /* CONFIG_ANDROID */
72 static LIST_HEAD(extcon_dev_list);
73 static DEFINE_MUTEX(extcon_dev_list_lock);
75 /**
76 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
77 * condition.
78 * @edev: the extcon device
79 * @new_state: new cable attach status for @edev
81 * Returns 0 if nothing violates. Returns the index + 1 for the first
82 * violated condition.
84 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
86 int i = 0;
88 if (!edev->mutually_exclusive)
89 return 0;
91 for (i = 0; edev->mutually_exclusive[i]; i++) {
92 int weight;
93 u32 correspondants = new_state & edev->mutually_exclusive[i];
95 /* calculate the total number of bits set */
96 weight = hweight32(correspondants);
97 if (weight > 1)
98 return i + 1;
101 return 0;
104 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
105 char *buf)
107 int i, count = 0;
108 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
110 if (edev->print_state) {
111 int ret = edev->print_state(edev, buf);
113 if (ret >= 0)
114 return ret;
115 /* Use default if failed */
118 if (edev->max_supported == 0)
119 return sprintf(buf, "%u\n", edev->state);
121 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
122 if (!edev->supported_cable[i])
123 break;
124 count += sprintf(buf + count, "%s=%d\n",
125 edev->supported_cable[i],
126 !!(edev->state & (1 << i)));
129 return count;
132 int extcon_set_state(struct extcon_dev *edev, u32 state);
133 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
134 const char *buf, size_t count)
136 u32 state;
137 ssize_t ret = 0;
138 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
140 ret = sscanf(buf, "0x%x", &state);
141 if (ret == 0)
142 ret = -EINVAL;
143 else
144 ret = extcon_set_state(edev, state);
146 if (ret < 0)
147 return ret;
149 return count;
152 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
153 char *buf)
155 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
157 /* Optional callback given by the user */
158 if (edev->print_name) {
159 int ret = edev->print_name(edev, buf);
160 if (ret >= 0)
161 return ret;
164 return sprintf(buf, "%s\n", dev_name(edev->dev));
167 static ssize_t cable_name_show(struct device *dev,
168 struct device_attribute *attr, char *buf)
170 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
171 attr_name);
173 return sprintf(buf, "%s\n",
174 cable->edev->supported_cable[cable->cable_index]);
177 static ssize_t cable_state_show(struct device *dev,
178 struct device_attribute *attr, char *buf)
180 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
181 attr_state);
183 return sprintf(buf, "%d\n",
184 extcon_get_cable_state_(cable->edev,
185 cable->cable_index));
189 * extcon_update_state() - Update the cable attach states of the extcon device
190 * only for the masked bits.
191 * @edev: the extcon device
192 * @mask: the bit mask to designate updated bits.
193 * @state: new cable attach status for @edev
195 * Changing the state sends uevent with environment variable containing
196 * the name of extcon device (envp[0]) and the state output (envp[1]).
197 * Tizen uses this format for extcon device to get events from ports.
198 * Android uses this format as well.
200 * Note that the notifier provides which bits are changed in the state
201 * variable with the val parameter (second) to the callback.
203 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
205 char name_buf[120];
206 char state_buf[120];
207 char *prop_buf;
208 char *envp[3];
209 int env_offset = 0;
210 int length;
211 unsigned long flags;
213 spin_lock_irqsave(&edev->lock, flags);
215 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
216 u32 old_state = edev->state;
218 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
219 (state & mask))) {
220 spin_unlock_irqrestore(&edev->lock, flags);
221 return -EPERM;
224 edev->state &= ~mask;
225 edev->state |= state & mask;
227 raw_notifier_call_chain(&edev->nh, old_state, edev);
229 /* This could be in interrupt handler */
230 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
231 if (prop_buf) {
232 length = name_show(edev->dev, NULL, prop_buf);
233 if (length > 0) {
234 if (prop_buf[length - 1] == '\n')
235 prop_buf[length - 1] = 0;
236 snprintf(name_buf, sizeof(name_buf),
237 "NAME=%s", prop_buf);
238 envp[env_offset++] = name_buf;
240 length = state_show(edev->dev, NULL, prop_buf);
241 if (length > 0) {
242 if (prop_buf[length - 1] == '\n')
243 prop_buf[length - 1] = 0;
244 snprintf(state_buf, sizeof(state_buf),
245 "STATE=%s", prop_buf);
246 envp[env_offset++] = state_buf;
248 envp[env_offset] = NULL;
249 /* Unlock early before uevent */
250 spin_unlock_irqrestore(&edev->lock, flags);
252 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
253 free_page((unsigned long)prop_buf);
254 } else {
255 /* Unlock early before uevent */
256 spin_unlock_irqrestore(&edev->lock, flags);
258 dev_err(edev->dev, "out of memory in extcon_set_state\n");
259 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
261 } else {
262 /* No changes */
263 spin_unlock_irqrestore(&edev->lock, flags);
266 return 0;
268 EXPORT_SYMBOL_GPL(extcon_update_state);
271 * extcon_set_state() - Set the cable attach states of the extcon device.
272 * @edev: the extcon device
273 * @state: new cable attach status for @edev
275 * Note that notifier provides which bits are changed in the state
276 * variable with the val parameter (second) to the callback.
278 int extcon_set_state(struct extcon_dev *edev, u32 state)
280 return extcon_update_state(edev, 0xffffffff, state);
282 EXPORT_SYMBOL_GPL(extcon_set_state);
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.
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.
294 int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
296 int i;
298 if (edev->supported_cable) {
299 for (i = 0; edev->supported_cable[i]; i++) {
300 if (!strncmp(edev->supported_cable[i],
301 cable_name, CABLE_NAME_MAX))
302 return i;
306 return -EINVAL;
308 EXPORT_SYMBOL_GPL(extcon_find_cable_index);
311 * extcon_get_cable_state_() - Get the status of a specific cable.
312 * @edev: the extcon device that has the cable.
313 * @index: cable index that can be retrieved by extcon_find_cable_index().
315 int extcon_get_cable_state_(struct extcon_dev *edev, int index)
317 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
318 return -EINVAL;
320 return !!(edev->state & (1 << index));
322 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
325 * extcon_get_cable_state() - Get the status of a specific cable.
326 * @edev: the extcon device that has the cable.
327 * @cable_name: cable name.
329 * Note that this is slower than extcon_get_cable_state_.
331 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
333 return extcon_get_cable_state_(edev, extcon_find_cable_index
334 (edev, cable_name));
336 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
339 * extcon_set_cable_state_() - Set the status of a specific cable.
340 * @edev: the extcon device that has the cable.
341 * @index: cable index that can be retrieved by extcon_find_cable_index().
342 * @cable_state: the new cable status. The default semantics is
343 * true: attached / false: detached.
345 int extcon_set_cable_state_(struct extcon_dev *edev,
346 int index, bool cable_state)
348 u32 state;
350 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
351 return -EINVAL;
353 state = cable_state ? (1 << index) : 0;
354 return extcon_update_state(edev, 1 << index, state);
356 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
359 * extcon_set_cable_state() - Set the status of a specific cable.
360 * @edev: the extcon device that has the cable.
361 * @cable_name: cable name.
362 * @cable_state: the new cable status. The default semantics is
363 * true: attached / false: detached.
365 * Note that this is slower than extcon_set_cable_state_.
367 int extcon_set_cable_state(struct extcon_dev *edev,
368 const char *cable_name, bool cable_state)
370 return extcon_set_cable_state_(edev, extcon_find_cable_index
371 (edev, cable_name), cable_state);
373 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
376 * extcon_get_extcon_dev() - Get the extcon device instance from the name
377 * @extcon_name: The extcon name provided with extcon_dev_register()
379 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
381 struct extcon_dev *sd;
383 mutex_lock(&extcon_dev_list_lock);
384 list_for_each_entry(sd, &extcon_dev_list, entry) {
385 if (!strcmp(sd->name, extcon_name))
386 goto out;
388 sd = NULL;
389 out:
390 mutex_unlock(&extcon_dev_list_lock);
391 return sd;
393 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
395 static int _call_per_cable(struct notifier_block *nb, unsigned long val,
396 void *ptr)
398 struct extcon_specific_cable_nb *obj = container_of(nb,
399 struct extcon_specific_cable_nb, internal_nb);
400 struct extcon_dev *edev = ptr;
402 if ((val & (1 << obj->cable_index)) !=
403 (edev->state & (1 << obj->cable_index))) {
404 bool cable_state = true;
406 obj->previous_value = val;
408 if (val & (1 << obj->cable_index))
409 cable_state = false;
411 return obj->user_nb->notifier_call(obj->user_nb,
412 cable_state, ptr);
415 return NOTIFY_OK;
419 * extcon_register_interest() - Register a notifier for a state change of a
420 * specific cable, not an entier set of cables of a
421 * extcon device.
422 * @obj: an empty extcon_specific_cable_nb object to be returned.
423 * @extcon_name: the name of extcon device.
424 * if NULL, extcon_register_interest will register
425 * every cable with the target cable_name given.
426 * @cable_name: the target cable name.
427 * @nb: the notifier block to get notified.
429 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
430 * the struct for you.
432 * extcon_register_interest is a helper function for those who want to get
433 * notification for a single specific cable's status change. If a user wants
434 * to get notification for any changes of all cables of a extcon device,
435 * he/she should use the general extcon_register_notifier().
437 * Note that the second parameter given to the callback of nb (val) is
438 * "old_state", not the current state. The current state can be retrieved
439 * by looking at the third pameter (edev pointer)'s state value.
441 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
442 const char *extcon_name, const char *cable_name,
443 struct notifier_block *nb)
445 if (!obj || !cable_name || !nb)
446 return -EINVAL;
448 if (extcon_name) {
449 obj->edev = extcon_get_extcon_dev(extcon_name);
450 if (!obj->edev)
451 return -ENODEV;
453 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
454 if (obj->cable_index < 0)
455 return obj->cable_index;
457 obj->user_nb = nb;
459 obj->internal_nb.notifier_call = _call_per_cable;
461 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
462 } else {
463 struct class_dev_iter iter;
464 struct extcon_dev *extd;
465 struct device *dev;
467 if (!extcon_class)
468 return -ENODEV;
469 class_dev_iter_init(&iter, extcon_class, NULL, NULL);
470 while ((dev = class_dev_iter_next(&iter))) {
471 extd = (struct extcon_dev *)dev_get_drvdata(dev);
473 if (extcon_find_cable_index(extd, cable_name) < 0)
474 continue;
476 class_dev_iter_exit(&iter);
477 return extcon_register_interest(obj, extd->name,
478 cable_name, nb);
481 return -ENODEV;
484 EXPORT_SYMBOL_GPL(extcon_register_interest);
487 * extcon_unregister_interest() - Unregister the notifier registered by
488 * extcon_register_interest().
489 * @obj: the extcon_specific_cable_nb object returned by
490 * extcon_register_interest().
492 int extcon_unregister_interest(struct extcon_specific_cable_nb *obj)
494 if (!obj)
495 return -EINVAL;
497 return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb);
499 EXPORT_SYMBOL_GPL(extcon_unregister_interest);
502 * extcon_register_notifier() - Register a notifiee to get notified by
503 * any attach status changes from the extcon.
504 * @edev: the extcon device.
505 * @nb: a notifier block to be registered.
507 * Note that the second parameter given to the callback of nb (val) is
508 * "old_state", not the current state. The current state can be retrieved
509 * by looking at the third pameter (edev pointer)'s state value.
511 int extcon_register_notifier(struct extcon_dev *edev,
512 struct notifier_block *nb)
514 return raw_notifier_chain_register(&edev->nh, nb);
516 EXPORT_SYMBOL_GPL(extcon_register_notifier);
519 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
520 * @edev: the extcon device.
521 * @nb: a registered notifier block to be unregistered.
523 int extcon_unregister_notifier(struct extcon_dev *edev,
524 struct notifier_block *nb)
526 return raw_notifier_chain_unregister(&edev->nh, nb);
528 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
530 static struct device_attribute extcon_attrs[] = {
531 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
532 __ATTR_RO(name),
533 __ATTR_NULL,
536 static int create_extcon_class(void)
538 if (!extcon_class) {
539 extcon_class = class_create(THIS_MODULE, "extcon");
540 if (IS_ERR(extcon_class))
541 return PTR_ERR(extcon_class);
542 extcon_class->dev_attrs = extcon_attrs;
544 #if defined(CONFIG_ANDROID)
545 switch_class = class_compat_register("switch");
546 if (WARN(!switch_class, "cannot allocate"))
547 return -ENOMEM;
548 #endif /* CONFIG_ANDROID */
551 return 0;
554 static void extcon_dev_release(struct device *dev)
556 kfree(dev);
559 static const char *muex_name = "mutually_exclusive";
560 static void dummy_sysfs_dev_release(struct device *dev)
565 * extcon_dev_register() - Register a new extcon device
566 * @edev : the new extcon device (should be allocated before calling)
567 * @dev : the parent device for this extcon device.
569 * Among the members of edev struct, please set the "user initializing data"
570 * in any case and set the "optional callbacks" if required. However, please
571 * do not set the values of "internal data", which are initialized by
572 * this function.
574 int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
576 int ret, index = 0;
578 if (!extcon_class) {
579 ret = create_extcon_class();
580 if (ret < 0)
581 return ret;
584 if (edev->supported_cable) {
585 /* Get size of array */
586 for (index = 0; edev->supported_cable[index]; index++)
588 edev->max_supported = index;
589 } else {
590 edev->max_supported = 0;
593 if (index > SUPPORTED_CABLE_MAX) {
594 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
595 return -EINVAL;
598 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
599 if (!edev->dev)
600 return -ENOMEM;
601 edev->dev->parent = dev;
602 edev->dev->class = extcon_class;
603 edev->dev->release = extcon_dev_release;
605 edev->name = edev->name ? edev->name : dev_name(dev);
606 dev_set_name(edev->dev, "%s", edev->name);
608 if (edev->max_supported) {
609 char buf[10];
610 char *str;
611 struct extcon_cable *cable;
613 edev->cables = kzalloc(sizeof(struct extcon_cable) *
614 edev->max_supported, GFP_KERNEL);
615 if (!edev->cables) {
616 ret = -ENOMEM;
617 goto err_sysfs_alloc;
619 for (index = 0; index < edev->max_supported; index++) {
620 cable = &edev->cables[index];
622 snprintf(buf, 10, "cable.%d", index);
623 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
624 GFP_KERNEL);
625 if (!str) {
626 for (index--; index >= 0; index--) {
627 cable = &edev->cables[index];
628 kfree(cable->attr_g.name);
630 ret = -ENOMEM;
632 goto err_alloc_cables;
634 strcpy(str, buf);
636 cable->edev = edev;
637 cable->cable_index = index;
638 cable->attrs[0] = &cable->attr_name.attr;
639 cable->attrs[1] = &cable->attr_state.attr;
640 cable->attrs[2] = NULL;
641 cable->attr_g.name = str;
642 cable->attr_g.attrs = cable->attrs;
644 sysfs_attr_init(&cable->attr_name.attr);
645 cable->attr_name.attr.name = "name";
646 cable->attr_name.attr.mode = 0444;
647 cable->attr_name.show = cable_name_show;
649 sysfs_attr_init(&cable->attr_state.attr);
650 cable->attr_state.attr.name = "state";
651 cable->attr_state.attr.mode = 0444;
652 cable->attr_state.show = cable_state_show;
656 if (edev->max_supported && edev->mutually_exclusive) {
657 char buf[80];
658 char *name;
660 /* Count the size of mutually_exclusive array */
661 for (index = 0; edev->mutually_exclusive[index]; index++)
664 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
665 (index + 1), GFP_KERNEL);
666 if (!edev->attrs_muex) {
667 ret = -ENOMEM;
668 goto err_muex;
671 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
672 index, GFP_KERNEL);
673 if (!edev->d_attrs_muex) {
674 ret = -ENOMEM;
675 kfree(edev->attrs_muex);
676 goto err_muex;
679 for (index = 0; edev->mutually_exclusive[index]; index++) {
680 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
681 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
682 GFP_KERNEL);
683 if (!name) {
684 for (index--; index >= 0; index--) {
685 kfree(edev->d_attrs_muex[index].attr.
686 name);
688 kfree(edev->d_attrs_muex);
689 kfree(edev->attrs_muex);
690 ret = -ENOMEM;
691 goto err_muex;
693 strcpy(name, buf);
694 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
695 edev->d_attrs_muex[index].attr.name = name;
696 edev->d_attrs_muex[index].attr.mode = 0000;
697 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
698 .attr;
700 edev->attr_g_muex.name = muex_name;
701 edev->attr_g_muex.attrs = edev->attrs_muex;
705 if (edev->max_supported) {
706 edev->extcon_dev_type.groups =
707 kzalloc(sizeof(struct attribute_group *) *
708 (edev->max_supported + 2), GFP_KERNEL);
709 if (!edev->extcon_dev_type.groups) {
710 ret = -ENOMEM;
711 goto err_alloc_groups;
714 edev->extcon_dev_type.name = dev_name(edev->dev);
715 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
717 for (index = 0; index < edev->max_supported; index++)
718 edev->extcon_dev_type.groups[index] =
719 &edev->cables[index].attr_g;
720 if (edev->mutually_exclusive)
721 edev->extcon_dev_type.groups[index] =
722 &edev->attr_g_muex;
724 edev->dev->type = &edev->extcon_dev_type;
727 ret = device_register(edev->dev);
728 if (ret) {
729 put_device(edev->dev);
730 goto err_dev;
732 #if defined(CONFIG_ANDROID)
733 if (switch_class)
734 ret = class_compat_create_link(switch_class, edev->dev,
735 NULL);
736 #endif /* CONFIG_ANDROID */
738 spin_lock_init(&edev->lock);
740 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
742 dev_set_drvdata(edev->dev, edev);
743 edev->state = 0;
745 mutex_lock(&extcon_dev_list_lock);
746 list_add(&edev->entry, &extcon_dev_list);
747 mutex_unlock(&extcon_dev_list_lock);
749 return 0;
751 err_dev:
752 if (edev->max_supported)
753 kfree(edev->extcon_dev_type.groups);
754 err_alloc_groups:
755 if (edev->max_supported && edev->mutually_exclusive) {
756 for (index = 0; edev->mutually_exclusive[index]; index++)
757 kfree(edev->d_attrs_muex[index].attr.name);
758 kfree(edev->d_attrs_muex);
759 kfree(edev->attrs_muex);
761 err_muex:
762 for (index = 0; index < edev->max_supported; index++)
763 kfree(edev->cables[index].attr_g.name);
764 err_alloc_cables:
765 if (edev->max_supported)
766 kfree(edev->cables);
767 err_sysfs_alloc:
768 kfree(edev->dev);
769 return ret;
771 EXPORT_SYMBOL_GPL(extcon_dev_register);
774 * extcon_dev_unregister() - Unregister the extcon device.
775 * @edev: the extcon device instance to be unregistered.
777 * Note that this does not call kfree(edev) because edev was not allocated
778 * by this class.
780 void extcon_dev_unregister(struct extcon_dev *edev)
782 int index;
784 mutex_lock(&extcon_dev_list_lock);
785 list_del(&edev->entry);
786 mutex_unlock(&extcon_dev_list_lock);
788 if (IS_ERR_OR_NULL(get_device(edev->dev))) {
789 dev_err(edev->dev, "Failed to unregister extcon_dev (%s)\n",
790 dev_name(edev->dev));
791 return;
794 if (edev->mutually_exclusive && edev->max_supported) {
795 for (index = 0; edev->mutually_exclusive[index];
796 index++)
797 kfree(edev->d_attrs_muex[index].attr.name);
798 kfree(edev->d_attrs_muex);
799 kfree(edev->attrs_muex);
802 for (index = 0; index < edev->max_supported; index++)
803 kfree(edev->cables[index].attr_g.name);
805 if (edev->max_supported) {
806 kfree(edev->extcon_dev_type.groups);
807 kfree(edev->cables);
810 #if defined(CONFIG_ANDROID)
811 if (switch_class)
812 class_compat_remove_link(switch_class, edev->dev, NULL);
813 #endif
814 device_unregister(edev->dev);
815 put_device(edev->dev);
817 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
819 static int __init extcon_class_init(void)
821 return create_extcon_class();
823 module_init(extcon_class_init);
825 static void __exit extcon_class_exit(void)
827 #if defined(CONFIG_ANDROID)
828 class_compat_unregister(switch_class);
829 #endif
830 class_destroy(extcon_class);
832 module_exit(extcon_class_exit);
834 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
835 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
836 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
837 MODULE_DESCRIPTION("External connector (extcon) class driver");
838 MODULE_LICENSE("GPL");