Extcon: Staticise extcon_class
[linux-2.6/libata-dev.git] / drivers / extcon / extcon_class.c
blob99f7227834e36c5835827f9863ffe0aaeb432207
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>
35 * extcon_cable_name suggests the standard cable names for commonly used
36 * cable types.
38 * However, please do not use extcon_cable_name directly for extcon_dev
39 * struct's supported_cable pointer unless your device really supports
40 * every single port-type of the following cable names. Please choose cable
41 * names that are actually used in your extcon device.
43 const char *extcon_cable_name[] = {
44 [EXTCON_USB] = "USB",
45 [EXTCON_USB_HOST] = "USB-Host",
46 [EXTCON_TA] = "TA",
47 [EXTCON_FAST_CHARGER] = "Fast-charger",
48 [EXTCON_SLOW_CHARGER] = "Slow-charger",
49 [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream",
50 [EXTCON_HDMI] = "HDMI",
51 [EXTCON_MHL] = "MHL",
52 [EXTCON_DVI] = "DVI",
53 [EXTCON_VGA] = "VGA",
54 [EXTCON_DOCK] = "Dock",
55 [EXTCON_LINE_IN] = "Line-in",
56 [EXTCON_LINE_OUT] = "Line-out",
57 [EXTCON_MIC_IN] = "Microphone",
58 [EXTCON_HEADPHONE_OUT] = "Headphone",
59 [EXTCON_SPDIF_IN] = "SPDIF-in",
60 [EXTCON_SPDIF_OUT] = "SPDIF-out",
61 [EXTCON_VIDEO_IN] = "Video-in",
62 [EXTCON_VIDEO_OUT] = "Video-out",
63 [EXTCON_MECHANICAL] = "Mechanical",
65 NULL,
68 static struct class *extcon_class;
69 #if defined(CONFIG_ANDROID)
70 static struct class_compat *switch_class;
71 #endif /* CONFIG_ANDROID */
73 static LIST_HEAD(extcon_dev_list);
74 static DEFINE_MUTEX(extcon_dev_list_lock);
76 /**
77 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
78 * condition.
79 * @edev: the extcon device
80 * @new_state: new cable attach status for @edev
82 * Returns 0 if nothing violates. Returns the index + 1 for the first
83 * violated condition.
85 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
87 int i = 0;
89 if (!edev->mutually_exclusive)
90 return 0;
92 for (i = 0; edev->mutually_exclusive[i]; i++) {
93 int count = 0, j;
94 u32 correspondants = new_state & edev->mutually_exclusive[i];
95 u32 exp = 1;
97 for (j = 0; j < 32; j++) {
98 if (exp & correspondants)
99 count++;
100 if (count > 1)
101 return i + 1;
102 exp <<= 1;
106 return 0;
109 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
110 char *buf)
112 int i, count = 0;
113 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
115 if (edev->print_state) {
116 int ret = edev->print_state(edev, buf);
118 if (ret >= 0)
119 return ret;
120 /* Use default if failed */
123 if (edev->max_supported == 0)
124 return sprintf(buf, "%u\n", edev->state);
126 for (i = 0; i < SUPPORTED_CABLE_MAX; i++) {
127 if (!edev->supported_cable[i])
128 break;
129 count += sprintf(buf + count, "%s=%d\n",
130 edev->supported_cable[i],
131 !!(edev->state & (1 << i)));
134 return count;
137 int extcon_set_state(struct extcon_dev *edev, u32 state);
138 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
139 const char *buf, size_t count)
141 u32 state;
142 ssize_t ret = 0;
143 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
145 ret = sscanf(buf, "0x%x", &state);
146 if (ret == 0)
147 ret = -EINVAL;
148 else
149 ret = extcon_set_state(edev, state);
151 if (ret < 0)
152 return ret;
154 return count;
157 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
158 char *buf)
160 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
162 /* Optional callback given by the user */
163 if (edev->print_name) {
164 int ret = edev->print_name(edev, buf);
165 if (ret >= 0)
166 return ret;
169 return sprintf(buf, "%s\n", dev_name(edev->dev));
172 static ssize_t cable_name_show(struct device *dev,
173 struct device_attribute *attr, char *buf)
175 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
176 attr_name);
178 return sprintf(buf, "%s\n",
179 cable->edev->supported_cable[cable->cable_index]);
182 static ssize_t cable_state_show(struct device *dev,
183 struct device_attribute *attr, char *buf)
185 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
186 attr_state);
188 return sprintf(buf, "%d\n",
189 extcon_get_cable_state_(cable->edev,
190 cable->cable_index));
193 static ssize_t cable_state_store(struct device *dev,
194 struct device_attribute *attr, const char *buf,
195 size_t count)
197 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
198 attr_state);
199 int ret, state;
201 ret = sscanf(buf, "%d", &state);
202 if (ret == 0)
203 ret = -EINVAL;
204 else
205 ret = extcon_set_cable_state_(cable->edev, cable->cable_index,
206 state);
208 if (ret < 0)
209 return ret;
210 return count;
214 * extcon_update_state() - Update the cable attach states of the extcon device
215 * only for the masked bits.
216 * @edev: the extcon device
217 * @mask: the bit mask to designate updated bits.
218 * @state: new cable attach status for @edev
220 * Changing the state sends uevent with environment variable containing
221 * the name of extcon device (envp[0]) and the state output (envp[1]).
222 * Tizen uses this format for extcon device to get events from ports.
223 * Android uses this format as well.
225 * Note that the notifier provides which bits are changed in the state
226 * variable with the val parameter (second) to the callback.
228 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
230 char name_buf[120];
231 char state_buf[120];
232 char *prop_buf;
233 char *envp[3];
234 int env_offset = 0;
235 int length;
236 unsigned long flags;
238 spin_lock_irqsave(&edev->lock, flags);
240 if (edev->state != ((edev->state & ~mask) | (state & mask))) {
241 u32 old_state = edev->state;
243 if (check_mutually_exclusive(edev, (edev->state & ~mask) |
244 (state & mask))) {
245 spin_unlock_irqrestore(&edev->lock, flags);
246 return -EPERM;
249 edev->state &= ~mask;
250 edev->state |= state & mask;
252 raw_notifier_call_chain(&edev->nh, old_state, edev);
254 /* This could be in interrupt handler */
255 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
256 if (prop_buf) {
257 length = name_show(edev->dev, NULL, prop_buf);
258 if (length > 0) {
259 if (prop_buf[length - 1] == '\n')
260 prop_buf[length - 1] = 0;
261 snprintf(name_buf, sizeof(name_buf),
262 "NAME=%s", prop_buf);
263 envp[env_offset++] = name_buf;
265 length = state_show(edev->dev, NULL, prop_buf);
266 if (length > 0) {
267 if (prop_buf[length - 1] == '\n')
268 prop_buf[length - 1] = 0;
269 snprintf(state_buf, sizeof(state_buf),
270 "STATE=%s", prop_buf);
271 envp[env_offset++] = state_buf;
273 envp[env_offset] = NULL;
274 /* Unlock early before uevent */
275 spin_unlock_irqrestore(&edev->lock, flags);
277 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
278 free_page((unsigned long)prop_buf);
279 } else {
280 /* Unlock early before uevent */
281 spin_unlock_irqrestore(&edev->lock, flags);
283 dev_err(edev->dev, "out of memory in extcon_set_state\n");
284 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
286 } else {
287 /* No changes */
288 spin_unlock_irqrestore(&edev->lock, flags);
291 return 0;
293 EXPORT_SYMBOL_GPL(extcon_update_state);
296 * extcon_set_state() - Set the cable attach states of the extcon device.
297 * @edev: the extcon device
298 * @state: new cable attach status for @edev
300 * Note that notifier provides which bits are changed in the state
301 * variable with the val parameter (second) to the callback.
303 int extcon_set_state(struct extcon_dev *edev, u32 state)
305 return extcon_update_state(edev, 0xffffffff, state);
307 EXPORT_SYMBOL_GPL(extcon_set_state);
310 * extcon_find_cable_index() - Get the cable index based on the cable name.
311 * @edev: the extcon device that has the cable.
312 * @cable_name: cable name to be searched.
314 * Note that accessing a cable state based on cable_index is faster than
315 * cable_name because using cable_name induces a loop with strncmp().
316 * Thus, when get/set_cable_state is repeatedly used, using cable_index
317 * is recommended.
319 int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name)
321 int i;
323 if (edev->supported_cable) {
324 for (i = 0; edev->supported_cable[i]; i++) {
325 if (!strncmp(edev->supported_cable[i],
326 cable_name, CABLE_NAME_MAX))
327 return i;
331 return -EINVAL;
333 EXPORT_SYMBOL_GPL(extcon_find_cable_index);
336 * extcon_get_cable_state_() - Get the status of a specific cable.
337 * @edev: the extcon device that has the cable.
338 * @index: cable index that can be retrieved by extcon_find_cable_index().
340 int extcon_get_cable_state_(struct extcon_dev *edev, int index)
342 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
343 return -EINVAL;
345 return !!(edev->state & (1 << index));
347 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
350 * extcon_get_cable_state() - Get the status of a specific cable.
351 * @edev: the extcon device that has the cable.
352 * @cable_name: cable name.
354 * Note that this is slower than extcon_get_cable_state_.
356 int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
358 return extcon_get_cable_state_(edev, extcon_find_cable_index
359 (edev, cable_name));
361 EXPORT_SYMBOL_GPL(extcon_get_cable_state);
364 * extcon_get_cable_state_() - Set the status of a specific cable.
365 * @edev: the extcon device that has the cable.
366 * @index: cable index that can be retrieved by extcon_find_cable_index().
367 * @cable_state: the new cable status. The default semantics is
368 * true: attached / false: detached.
370 int extcon_set_cable_state_(struct extcon_dev *edev,
371 int index, bool cable_state)
373 u32 state;
375 if (index < 0 || (edev->max_supported && edev->max_supported <= index))
376 return -EINVAL;
378 state = cable_state ? (1 << index) : 0;
379 return extcon_update_state(edev, 1 << index, state);
381 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
384 * extcon_get_cable_state() - Set the status of a specific cable.
385 * @edev: the extcon device that has the cable.
386 * @cable_name: cable name.
387 * @cable_state: the new cable status. The default semantics is
388 * true: attached / false: detached.
390 * Note that this is slower than extcon_set_cable_state_.
392 int extcon_set_cable_state(struct extcon_dev *edev,
393 const char *cable_name, bool cable_state)
395 return extcon_set_cable_state_(edev, extcon_find_cable_index
396 (edev, cable_name), cable_state);
398 EXPORT_SYMBOL_GPL(extcon_set_cable_state);
401 * extcon_get_extcon_dev() - Get the extcon device instance from the name
402 * @extcon_name: The extcon name provided with extcon_dev_register()
404 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
406 struct extcon_dev *sd;
408 mutex_lock(&extcon_dev_list_lock);
409 list_for_each_entry(sd, &extcon_dev_list, entry) {
410 if (!strcmp(sd->name, extcon_name))
411 goto out;
413 sd = NULL;
414 out:
415 mutex_unlock(&extcon_dev_list_lock);
416 return sd;
418 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
420 static int _call_per_cable(struct notifier_block *nb, unsigned long val,
421 void *ptr)
423 struct extcon_specific_cable_nb *obj = container_of(nb,
424 struct extcon_specific_cable_nb, internal_nb);
425 struct extcon_dev *edev = ptr;
427 if ((val & (1 << obj->cable_index)) !=
428 (edev->state & (1 << obj->cable_index))) {
429 bool cable_state = true;
431 obj->previous_value = val;
433 if (val & (1 << obj->cable_index))
434 cable_state = false;
436 return obj->user_nb->notifier_call(obj->user_nb,
437 cable_state, ptr);
440 return NOTIFY_OK;
444 * extcon_register_interest() - Register a notifier for a state change of a
445 * specific cable, not a entier set of cables of a
446 * extcon device.
447 * @obj: an empty extcon_specific_cable_nb object to be returned.
448 * @extcon_name: the name of extcon device.
449 * @cable_name: the target cable name.
450 * @nb: the notifier block to get notified.
452 * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets
453 * the struct for you.
455 * extcon_register_interest is a helper function for those who want to get
456 * notification for a single specific cable's status change. If a user wants
457 * to get notification for any changes of all cables of a extcon device,
458 * he/she should use the general extcon_register_notifier().
460 * Note that the second parameter given to the callback of nb (val) is
461 * "old_state", not the current state. The current state can be retrieved
462 * by looking at the third pameter (edev pointer)'s state value.
464 int extcon_register_interest(struct extcon_specific_cable_nb *obj,
465 const char *extcon_name, const char *cable_name,
466 struct notifier_block *nb)
468 if (!obj || !extcon_name || !cable_name || !nb)
469 return -EINVAL;
471 obj->edev = extcon_get_extcon_dev(extcon_name);
472 if (!obj->edev)
473 return -ENODEV;
475 obj->cable_index = extcon_find_cable_index(obj->edev, cable_name);
476 if (obj->cable_index < 0)
477 return -ENODEV;
479 obj->user_nb = nb;
481 obj->internal_nb.notifier_call = _call_per_cable;
483 return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb);
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);
501 * extcon_register_notifier() - Register a notifee to get notified by
502 * any attach status changes from the extcon.
503 * @edev: the extcon device.
504 * @nb: a notifier block to be registered.
506 * Note that the second parameter given to the callback of nb (val) is
507 * "old_state", not the current state. The current state can be retrieved
508 * by looking at the third pameter (edev pointer)'s state value.
510 int extcon_register_notifier(struct extcon_dev *edev,
511 struct notifier_block *nb)
513 return raw_notifier_chain_register(&edev->nh, nb);
515 EXPORT_SYMBOL_GPL(extcon_register_notifier);
518 * extcon_unregister_notifier() - Unregister a notifee from the extcon device.
519 * @edev: the extcon device.
520 * @nb: a registered notifier block to be unregistered.
522 int extcon_unregister_notifier(struct extcon_dev *edev,
523 struct notifier_block *nb)
525 return raw_notifier_chain_unregister(&edev->nh, nb);
527 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
529 static struct device_attribute extcon_attrs[] = {
530 __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store),
531 __ATTR_RO(name),
532 __ATTR_NULL,
535 static int create_extcon_class(void)
537 if (!extcon_class) {
538 extcon_class = class_create(THIS_MODULE, "extcon");
539 if (IS_ERR(extcon_class))
540 return PTR_ERR(extcon_class);
541 extcon_class->dev_attrs = extcon_attrs;
543 #if defined(CONFIG_ANDROID)
544 switch_class = class_compat_register("switch");
545 if (WARN(!switch_class, "cannot allocate"))
546 return -ENOMEM;
547 #endif /* CONFIG_ANDROID */
550 return 0;
553 static void extcon_cleanup(struct extcon_dev *edev, bool skip)
555 mutex_lock(&extcon_dev_list_lock);
556 list_del(&edev->entry);
557 mutex_unlock(&extcon_dev_list_lock);
559 if (!skip && get_device(edev->dev)) {
560 int index;
562 if (edev->mutually_exclusive && edev->max_supported) {
563 for (index = 0; edev->mutually_exclusive[index];
564 index++)
565 kfree(edev->d_attrs_muex[index].attr.name);
566 kfree(edev->d_attrs_muex);
567 kfree(edev->attrs_muex);
570 for (index = 0; index < edev->max_supported; index++)
571 kfree(edev->cables[index].attr_g.name);
573 if (edev->max_supported) {
574 kfree(edev->extcon_dev_type.groups);
575 kfree(edev->cables);
578 device_unregister(edev->dev);
579 put_device(edev->dev);
582 kfree(edev->dev);
585 static void extcon_dev_release(struct device *dev)
587 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
589 extcon_cleanup(edev, true);
592 static const char *muex_name = "mutually_exclusive";
593 static void dummy_sysfs_dev_release(struct device *dev)
598 * extcon_dev_register() - Register a new extcon device
599 * @edev : the new extcon device (should be allocated before calling)
600 * @dev : the parent device for this extcon device.
602 * Among the members of edev struct, please set the "user initializing data"
603 * in any case and set the "optional callbacks" if required. However, please
604 * do not set the values of "internal data", which are initialized by
605 * this function.
607 int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
609 int ret, index = 0;
611 if (!extcon_class) {
612 ret = create_extcon_class();
613 if (ret < 0)
614 return ret;
617 if (edev->supported_cable) {
618 /* Get size of array */
619 for (index = 0; edev->supported_cable[index]; index++)
621 edev->max_supported = index;
622 } else {
623 edev->max_supported = 0;
626 if (index > SUPPORTED_CABLE_MAX) {
627 dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n");
628 return -EINVAL;
631 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
632 if (!edev->dev)
633 return -ENOMEM;
634 edev->dev->parent = dev;
635 edev->dev->class = extcon_class;
636 edev->dev->release = extcon_dev_release;
638 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
640 if (edev->max_supported) {
641 char buf[10];
642 char *str;
643 struct extcon_cable *cable;
645 edev->cables = kzalloc(sizeof(struct extcon_cable) *
646 edev->max_supported, GFP_KERNEL);
647 if (!edev->cables) {
648 ret = -ENOMEM;
649 goto err_sysfs_alloc;
651 for (index = 0; index < edev->max_supported; index++) {
652 cable = &edev->cables[index];
654 snprintf(buf, 10, "cable.%d", index);
655 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
656 GFP_KERNEL);
657 if (!str) {
658 for (index--; index >= 0; index--) {
659 cable = &edev->cables[index];
660 kfree(cable->attr_g.name);
662 ret = -ENOMEM;
664 goto err_alloc_cables;
666 strcpy(str, buf);
668 cable->edev = edev;
669 cable->cable_index = index;
670 cable->attrs[0] = &cable->attr_name.attr;
671 cable->attrs[1] = &cable->attr_state.attr;
672 cable->attrs[2] = NULL;
673 cable->attr_g.name = str;
674 cable->attr_g.attrs = cable->attrs;
676 cable->attr_name.attr.name = "name";
677 cable->attr_name.attr.mode = 0444;
678 cable->attr_name.show = cable_name_show;
680 cable->attr_state.attr.name = "state";
681 cable->attr_state.attr.mode = 0644;
682 cable->attr_state.show = cable_state_show;
683 cable->attr_state.store = cable_state_store;
687 if (edev->max_supported && edev->mutually_exclusive) {
688 char buf[80];
689 char *name;
691 /* Count the size of mutually_exclusive array */
692 for (index = 0; edev->mutually_exclusive[index]; index++)
695 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
696 (index + 1), GFP_KERNEL);
697 if (!edev->attrs_muex) {
698 ret = -ENOMEM;
699 goto err_muex;
702 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
703 index, GFP_KERNEL);
704 if (!edev->d_attrs_muex) {
705 ret = -ENOMEM;
706 kfree(edev->attrs_muex);
707 goto err_muex;
710 for (index = 0; edev->mutually_exclusive[index]; index++) {
711 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
712 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
713 GFP_KERNEL);
714 if (!name) {
715 for (index--; index >= 0; index--) {
716 kfree(edev->d_attrs_muex[index].attr.
717 name);
719 kfree(edev->d_attrs_muex);
720 kfree(edev->attrs_muex);
721 ret = -ENOMEM;
722 goto err_muex;
724 strcpy(name, buf);
725 edev->d_attrs_muex[index].attr.name = name;
726 edev->d_attrs_muex[index].attr.mode = 0000;
727 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
728 .attr;
730 edev->attr_g_muex.name = muex_name;
731 edev->attr_g_muex.attrs = edev->attrs_muex;
735 if (edev->max_supported) {
736 edev->extcon_dev_type.groups =
737 kzalloc(sizeof(struct attribute_group *) *
738 (edev->max_supported + 2), GFP_KERNEL);
739 if (!edev->extcon_dev_type.groups) {
740 ret = -ENOMEM;
741 goto err_alloc_groups;
744 edev->extcon_dev_type.name = dev_name(edev->dev);
745 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
747 for (index = 0; index < edev->max_supported; index++)
748 edev->extcon_dev_type.groups[index] =
749 &edev->cables[index].attr_g;
750 if (edev->mutually_exclusive)
751 edev->extcon_dev_type.groups[index] =
752 &edev->attr_g_muex;
754 edev->dev->type = &edev->extcon_dev_type;
757 ret = device_register(edev->dev);
758 if (ret) {
759 put_device(edev->dev);
760 goto err_dev;
762 #if defined(CONFIG_ANDROID)
763 if (switch_class)
764 ret = class_compat_create_link(switch_class, edev->dev,
765 dev);
766 #endif /* CONFIG_ANDROID */
768 spin_lock_init(&edev->lock);
770 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
772 dev_set_drvdata(edev->dev, edev);
773 edev->state = 0;
775 mutex_lock(&extcon_dev_list_lock);
776 list_add(&edev->entry, &extcon_dev_list);
777 mutex_unlock(&extcon_dev_list_lock);
779 return 0;
781 err_dev:
782 if (edev->max_supported)
783 kfree(edev->extcon_dev_type.groups);
784 err_alloc_groups:
785 if (edev->max_supported && edev->mutually_exclusive) {
786 for (index = 0; edev->mutually_exclusive[index]; index++)
787 kfree(edev->d_attrs_muex[index].attr.name);
788 kfree(edev->d_attrs_muex);
789 kfree(edev->attrs_muex);
791 err_muex:
792 for (index = 0; index < edev->max_supported; index++)
793 kfree(edev->cables[index].attr_g.name);
794 err_alloc_cables:
795 if (edev->max_supported)
796 kfree(edev->cables);
797 err_sysfs_alloc:
798 kfree(edev->dev);
799 return ret;
801 EXPORT_SYMBOL_GPL(extcon_dev_register);
804 * extcon_dev_unregister() - Unregister the extcon device.
805 * @edev: the extcon device instance to be unregitered.
807 * Note that this does not call kfree(edev) because edev was not allocated
808 * by this class.
810 void extcon_dev_unregister(struct extcon_dev *edev)
812 extcon_cleanup(edev, false);
814 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
816 static int __init extcon_class_init(void)
818 return create_extcon_class();
820 module_init(extcon_class_init);
822 static void __exit extcon_class_exit(void)
824 class_destroy(extcon_class);
826 module_exit(extcon_class_exit);
828 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
829 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
830 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
831 MODULE_DESCRIPTION("External connector (extcon) class driver");
832 MODULE_LICENSE("GPL");