xref: /openbmc/linux/drivers/extcon/extcon.c (revision a05f44c8)
1 /*
2  *  drivers/extcon/extcon.c - External Connector (extcon) framework.
3  *
4  *  External connector (extcon) class driver
5  *
6  * Copyright (C) 2015 Samsung Electronics
7  * Author: Chanwoo Choi <cw00.choi@samsung.com>
8  *
9  * Copyright (C) 2012 Samsung Electronics
10  * Author: Donggeun Kim <dg77.kim@samsung.com>
11  * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
12  *
13  * based on android/drivers/switch/switch_class.c
14  * Copyright (C) 2008 Google, Inc.
15  * Author: Mike Lockwood <lockwood@android.com>
16  *
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.
20  *
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.
25  */
26 
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/init.h>
30 #include <linux/device.h>
31 #include <linux/fs.h>
32 #include <linux/err.h>
33 #include <linux/extcon.h>
34 #include <linux/of.h>
35 #include <linux/slab.h>
36 #include <linux/sysfs.h>
37 
38 #define SUPPORTED_CABLE_MAX	32
39 #define CABLE_NAME_MAX		30
40 
41 static const char *extcon_name[] =  {
42 	[EXTCON_NONE]			= "NONE",
43 
44 	/* USB external connector */
45 	[EXTCON_USB]			= "USB",
46 	[EXTCON_USB_HOST]		= "USB-HOST",
47 
48 	/* Charging external connector */
49 	[EXTCON_CHG_USB_SDP]		= "SDP",
50 	[EXTCON_CHG_USB_DCP]		= "DCP",
51 	[EXTCON_CHG_USB_CDP]		= "CDP",
52 	[EXTCON_CHG_USB_ACA]		= "ACA",
53 	[EXTCON_CHG_USB_FAST]		= "FAST-CHARGER",
54 	[EXTCON_CHG_USB_SLOW]		= "SLOW-CHARGER",
55 
56 	/* Jack external connector */
57 	[EXTCON_JACK_MICROPHONE]	= "MICROPHONE",
58 	[EXTCON_JACK_HEADPHONE]		= "HEADPHONE",
59 	[EXTCON_JACK_LINE_IN]		= "LINE-IN",
60 	[EXTCON_JACK_LINE_OUT]		= "LINE-OUT",
61 	[EXTCON_JACK_VIDEO_IN]		= "VIDEO-IN",
62 	[EXTCON_JACK_VIDEO_OUT]		= "VIDEO-OUT",
63 	[EXTCON_JACK_SPDIF_IN]		= "SPDIF-IN",
64 	[EXTCON_JACK_SPDIF_OUT]		= "SPDIF-OUT",
65 
66 	/* Display external connector */
67 	[EXTCON_DISP_HDMI]		= "HDMI",
68 	[EXTCON_DISP_MHL]		= "MHL",
69 	[EXTCON_DISP_DVI]		= "DVI",
70 	[EXTCON_DISP_VGA]		= "VGA",
71 
72 	/* Miscellaneous external connector */
73 	[EXTCON_DOCK]			= "DOCK",
74 	[EXTCON_JIG]			= "JIG",
75 	[EXTCON_MECHANICAL]		= "MECHANICAL",
76 
77 	NULL,
78 };
79 
80 static struct class *extcon_class;
81 #if defined(CONFIG_ANDROID)
82 static struct class_compat *switch_class;
83 #endif /* CONFIG_ANDROID */
84 
85 static LIST_HEAD(extcon_dev_list);
86 static DEFINE_MUTEX(extcon_dev_list_lock);
87 
88 /**
89  * check_mutually_exclusive - Check if new_state violates mutually_exclusive
90  *			      condition.
91  * @edev:	the extcon device
92  * @new_state:	new cable attach status for @edev
93  *
94  * Returns 0 if nothing violates. Returns the index + 1 for the first
95  * violated condition.
96  */
97 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
98 {
99 	int i = 0;
100 
101 	if (!edev->mutually_exclusive)
102 		return 0;
103 
104 	for (i = 0; edev->mutually_exclusive[i]; i++) {
105 		int weight;
106 		u32 correspondants = new_state & edev->mutually_exclusive[i];
107 
108 		/* calculate the total number of bits set */
109 		weight = hweight32(correspondants);
110 		if (weight > 1)
111 			return i + 1;
112 	}
113 
114 	return 0;
115 }
116 
117 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
118 {
119 	int i;
120 
121 	/* Find the the index of extcon cable in edev->supported_cable */
122 	for (i = 0; i < edev->max_supported; i++) {
123 		if (edev->supported_cable[i] == id)
124 			return i;
125 	}
126 
127 	return -EINVAL;
128 }
129 
130 static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached)
131 {
132 	if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) {
133 		*attached = ((new >> idx) & 0x1) ? true : false;
134 		return true;
135 	}
136 
137 	return false;
138 }
139 
140 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
141 			  char *buf)
142 {
143 	int i, count = 0;
144 	struct extcon_dev *edev = dev_get_drvdata(dev);
145 
146 	if (edev->max_supported == 0)
147 		return sprintf(buf, "%u\n", edev->state);
148 
149 	for (i = 0; i < edev->max_supported; i++) {
150 		count += sprintf(buf + count, "%s=%d\n",
151 				extcon_name[edev->supported_cable[i]],
152 				 !!(edev->state & (1 << i)));
153 	}
154 
155 	return count;
156 }
157 
158 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
159 			   const char *buf, size_t count)
160 {
161 	u32 state;
162 	ssize_t ret = 0;
163 	struct extcon_dev *edev = dev_get_drvdata(dev);
164 
165 	ret = sscanf(buf, "0x%x", &state);
166 	if (ret == 0)
167 		ret = -EINVAL;
168 	else
169 		ret = extcon_set_state(edev, state);
170 
171 	if (ret < 0)
172 		return ret;
173 
174 	return count;
175 }
176 static DEVICE_ATTR_RW(state);
177 
178 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
179 		char *buf)
180 {
181 	struct extcon_dev *edev = dev_get_drvdata(dev);
182 
183 	return sprintf(buf, "%s\n", edev->name);
184 }
185 static DEVICE_ATTR_RO(name);
186 
187 static ssize_t cable_name_show(struct device *dev,
188 			       struct device_attribute *attr, char *buf)
189 {
190 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
191 						  attr_name);
192 	int i = cable->cable_index;
193 
194 	return sprintf(buf, "%s\n",
195 			extcon_name[cable->edev->supported_cable[i]]);
196 }
197 
198 static ssize_t cable_state_show(struct device *dev,
199 				struct device_attribute *attr, char *buf)
200 {
201 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
202 						  attr_state);
203 
204 	int i = cable->cable_index;
205 
206 	return sprintf(buf, "%d\n",
207 		       extcon_get_cable_state_(cable->edev,
208 					       cable->edev->supported_cable[i]));
209 }
210 
211 /**
212  * extcon_update_state() - Update the cable attach states of the extcon device
213  *			   only for the masked bits.
214  * @edev:	the extcon device
215  * @mask:	the bit mask to designate updated bits.
216  * @state:	new cable attach status for @edev
217  *
218  * Changing the state sends uevent with environment variable containing
219  * the name of extcon device (envp[0]) and the state output (envp[1]).
220  * Tizen uses this format for extcon device to get events from ports.
221  * Android uses this format as well.
222  *
223  * Note that the notifier provides which bits are changed in the state
224  * variable with the val parameter (second) to the callback.
225  */
226 int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state)
227 {
228 	char name_buf[120];
229 	char state_buf[120];
230 	char *prop_buf;
231 	char *envp[3];
232 	int env_offset = 0;
233 	int length;
234 	int index;
235 	unsigned long flags;
236 	bool attached;
237 
238 	if (!edev)
239 		return -EINVAL;
240 
241 	spin_lock_irqsave(&edev->lock, flags);
242 
243 	if (edev->state != ((edev->state & ~mask) | (state & mask))) {
244 		u32 old_state;
245 
246 		if (check_mutually_exclusive(edev, (edev->state & ~mask) |
247 						   (state & mask))) {
248 			spin_unlock_irqrestore(&edev->lock, flags);
249 			return -EPERM;
250 		}
251 
252 		old_state = edev->state;
253 		edev->state &= ~mask;
254 		edev->state |= state & mask;
255 
256 		for (index = 0; index < edev->max_supported; index++) {
257 			if (is_extcon_changed(old_state, edev->state, index,
258 					      &attached))
259 				raw_notifier_call_chain(&edev->nh[index],
260 							attached, edev);
261 		}
262 
263 		/* This could be in interrupt handler */
264 		prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
265 		if (prop_buf) {
266 			length = name_show(&edev->dev, NULL, prop_buf);
267 			if (length > 0) {
268 				if (prop_buf[length - 1] == '\n')
269 					prop_buf[length - 1] = 0;
270 				snprintf(name_buf, sizeof(name_buf),
271 					"NAME=%s", prop_buf);
272 				envp[env_offset++] = name_buf;
273 			}
274 			length = state_show(&edev->dev, NULL, prop_buf);
275 			if (length > 0) {
276 				if (prop_buf[length - 1] == '\n')
277 					prop_buf[length - 1] = 0;
278 				snprintf(state_buf, sizeof(state_buf),
279 					"STATE=%s", prop_buf);
280 				envp[env_offset++] = state_buf;
281 			}
282 			envp[env_offset] = NULL;
283 			/* Unlock early before uevent */
284 			spin_unlock_irqrestore(&edev->lock, flags);
285 
286 			kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
287 			free_page((unsigned long)prop_buf);
288 		} else {
289 			/* Unlock early before uevent */
290 			spin_unlock_irqrestore(&edev->lock, flags);
291 
292 			dev_err(&edev->dev, "out of memory in extcon_set_state\n");
293 			kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
294 		}
295 	} else {
296 		/* No changes */
297 		spin_unlock_irqrestore(&edev->lock, flags);
298 	}
299 
300 	return 0;
301 }
302 EXPORT_SYMBOL_GPL(extcon_update_state);
303 
304 /**
305  * extcon_set_state() - Set the cable attach states of the extcon device.
306  * @edev:	the extcon device
307  * @state:	new cable attach status for @edev
308  *
309  * Note that notifier provides which bits are changed in the state
310  * variable with the val parameter (second) to the callback.
311  */
312 int extcon_set_state(struct extcon_dev *edev, u32 state)
313 {
314 	if (!edev)
315 		return -EINVAL;
316 
317 	return extcon_update_state(edev, 0xffffffff, state);
318 }
319 EXPORT_SYMBOL_GPL(extcon_set_state);
320 
321 /**
322  * extcon_get_cable_state_() - Get the status of a specific cable.
323  * @edev:	the extcon device that has the cable.
324  * @id:		the unique id of each external connector in extcon enumeration.
325  */
326 int extcon_get_cable_state_(struct extcon_dev *edev, const unsigned int id)
327 {
328 	int index;
329 
330 	if (!edev)
331 		return -EINVAL;
332 
333 	index = find_cable_index_by_id(edev, id);
334 	if (index < 0)
335 		return index;
336 
337 	if (edev->max_supported && edev->max_supported <= index)
338 		return -EINVAL;
339 
340 	return !!(edev->state & (1 << index));
341 }
342 EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
343 
344 /**
345  * extcon_set_cable_state_() - Set the status of a specific cable.
346  * @edev:		the extcon device that has the cable.
347  * @id:			the unique id of each external connector
348  *			in extcon enumeration.
349  * @state:		the new cable status. The default semantics is
350  *			true: attached / false: detached.
351  */
352 int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id,
353 				bool cable_state)
354 {
355 	u32 state;
356 	int index;
357 
358 	if (!edev)
359 		return -EINVAL;
360 
361 	index = find_cable_index_by_id(edev, id);
362 	if (index < 0)
363 		return index;
364 
365 	if (edev->max_supported && edev->max_supported <= index)
366 		return -EINVAL;
367 
368 	state = cable_state ? (1 << index) : 0;
369 	return extcon_update_state(edev, 1 << index, state);
370 }
371 EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
372 
373 /**
374  * extcon_get_extcon_dev() - Get the extcon device instance from the name
375  * @extcon_name:	The extcon name provided with extcon_dev_register()
376  */
377 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
378 {
379 	struct extcon_dev *sd;
380 
381 	if (!extcon_name)
382 		return ERR_PTR(-EINVAL);
383 
384 	mutex_lock(&extcon_dev_list_lock);
385 	list_for_each_entry(sd, &extcon_dev_list, entry) {
386 		if (!strcmp(sd->name, extcon_name))
387 			goto out;
388 	}
389 	sd = NULL;
390 out:
391 	mutex_unlock(&extcon_dev_list_lock);
392 	return sd;
393 }
394 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
395 
396 /**
397  * extcon_register_notifier() - Register a notifiee to get notified by
398  *				any attach status changes from the extcon.
399  * @edev:	the extcon device that has the external connecotr.
400  * @id:		the unique id of each external connector in extcon enumeration.
401  * @nb:		a notifier block to be registered.
402  *
403  * Note that the second parameter given to the callback of nb (val) is
404  * "old_state", not the current state. The current state can be retrieved
405  * by looking at the third pameter (edev pointer)'s state value.
406  */
407 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
408 			     struct notifier_block *nb)
409 {
410 	unsigned long flags;
411 	int ret, idx;
412 
413 	if (!nb)
414 		return -EINVAL;
415 
416 	if (edev) {
417 		idx = find_cable_index_by_id(edev, id);
418 		if (idx < 0)
419 			return idx;
420 
421 		spin_lock_irqsave(&edev->lock, flags);
422 		ret = raw_notifier_chain_register(&edev->nh[idx], nb);
423 		spin_unlock_irqrestore(&edev->lock, flags);
424 	} else {
425 		struct extcon_dev *extd;
426 
427 		mutex_lock(&extcon_dev_list_lock);
428 		list_for_each_entry(extd, &extcon_dev_list, entry) {
429 			idx = find_cable_index_by_id(extd, id);
430 			if (idx >= 0)
431 				break;
432 		}
433 		mutex_unlock(&extcon_dev_list_lock);
434 
435 		if (idx >= 0) {
436 			edev = extd;
437 			return extcon_register_notifier(extd, id, nb);
438 		} else {
439 			ret = -ENODEV;
440 		}
441 	}
442 
443 	return ret;
444 }
445 EXPORT_SYMBOL_GPL(extcon_register_notifier);
446 
447 /**
448  * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
449  * @edev:	the extcon device that has the external connecotr.
450  * @id:		the unique id of each external connector in extcon enumeration.
451  * @nb:		a notifier block to be registered.
452  */
453 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
454 				struct notifier_block *nb)
455 {
456 	unsigned long flags;
457 	int ret, idx;
458 
459 	if (!edev || !nb)
460 		return -EINVAL;
461 
462 	idx = find_cable_index_by_id(edev, id);
463 	if (idx < 0)
464 		return idx;
465 
466 	spin_lock_irqsave(&edev->lock, flags);
467 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
468 	spin_unlock_irqrestore(&edev->lock, flags);
469 
470 	return ret;
471 }
472 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
473 
474 static struct attribute *extcon_attrs[] = {
475 	&dev_attr_state.attr,
476 	&dev_attr_name.attr,
477 	NULL,
478 };
479 ATTRIBUTE_GROUPS(extcon);
480 
481 static int create_extcon_class(void)
482 {
483 	if (!extcon_class) {
484 		extcon_class = class_create(THIS_MODULE, "extcon");
485 		if (IS_ERR(extcon_class))
486 			return PTR_ERR(extcon_class);
487 		extcon_class->dev_groups = extcon_groups;
488 
489 #if defined(CONFIG_ANDROID)
490 		switch_class = class_compat_register("switch");
491 		if (WARN(!switch_class, "cannot allocate"))
492 			return -ENOMEM;
493 #endif /* CONFIG_ANDROID */
494 	}
495 
496 	return 0;
497 }
498 
499 static void extcon_dev_release(struct device *dev)
500 {
501 }
502 
503 static const char *muex_name = "mutually_exclusive";
504 static void dummy_sysfs_dev_release(struct device *dev)
505 {
506 }
507 
508 /*
509  * extcon_dev_allocate() - Allocate the memory of extcon device.
510  * @supported_cable:	Array of supported extcon ending with EXTCON_NONE.
511  *			If supported_cable is NULL, cable name related APIs
512  *			are disabled.
513  *
514  * This function allocates the memory for extcon device without allocating
515  * memory in each extcon provider driver and initialize default setting for
516  * extcon device.
517  *
518  * Return the pointer of extcon device if success or ERR_PTR(err) if fail
519  */
520 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
521 {
522 	struct extcon_dev *edev;
523 
524 	if (!supported_cable)
525 		return ERR_PTR(-EINVAL);
526 
527 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
528 	if (!edev)
529 		return ERR_PTR(-ENOMEM);
530 
531 	edev->max_supported = 0;
532 	edev->supported_cable = supported_cable;
533 
534 	return edev;
535 }
536 
537 /*
538  * extcon_dev_free() - Free the memory of extcon device.
539  * @edev:	the extcon device to free
540  */
541 void extcon_dev_free(struct extcon_dev *edev)
542 {
543 	kfree(edev);
544 }
545 EXPORT_SYMBOL_GPL(extcon_dev_free);
546 
547 static int devm_extcon_dev_match(struct device *dev, void *res, void *data)
548 {
549 	struct extcon_dev **r = res;
550 
551 	if (WARN_ON(!r || !*r))
552 		return 0;
553 
554 	return *r == data;
555 }
556 
557 static void devm_extcon_dev_release(struct device *dev, void *res)
558 {
559 	extcon_dev_free(*(struct extcon_dev **)res);
560 }
561 
562 /**
563  * devm_extcon_dev_allocate - Allocate managed extcon device
564  * @dev:		device owning the extcon device being created
565  * @supported_cable:	Array of supported extcon ending with EXTCON_NONE.
566  *			If supported_cable is NULL, cable name related APIs
567  *			are disabled.
568  *
569  * This function manages automatically the memory of extcon device using device
570  * resource management and simplify the control of freeing the memory of extcon
571  * device.
572  *
573  * Returns the pointer memory of allocated extcon_dev if success
574  * or ERR_PTR(err) if fail
575  */
576 struct extcon_dev *devm_extcon_dev_allocate(struct device *dev,
577 					const unsigned int *supported_cable)
578 {
579 	struct extcon_dev **ptr, *edev;
580 
581 	ptr = devres_alloc(devm_extcon_dev_release, sizeof(*ptr), GFP_KERNEL);
582 	if (!ptr)
583 		return ERR_PTR(-ENOMEM);
584 
585 	edev = extcon_dev_allocate(supported_cable);
586 	if (IS_ERR(edev)) {
587 		devres_free(ptr);
588 		return edev;
589 	}
590 
591 	edev->dev.parent = dev;
592 
593 	*ptr = edev;
594 	devres_add(dev, ptr);
595 
596 	return edev;
597 }
598 EXPORT_SYMBOL_GPL(devm_extcon_dev_allocate);
599 
600 void devm_extcon_dev_free(struct device *dev, struct extcon_dev *edev)
601 {
602 	WARN_ON(devres_release(dev, devm_extcon_dev_release,
603 			       devm_extcon_dev_match, edev));
604 }
605 EXPORT_SYMBOL_GPL(devm_extcon_dev_free);
606 
607 /**
608  * extcon_dev_register() - Register a new extcon device
609  * @edev	: the new extcon device (should be allocated before calling)
610  *
611  * Among the members of edev struct, please set the "user initializing data"
612  * in any case and set the "optional callbacks" if required. However, please
613  * do not set the values of "internal data", which are initialized by
614  * this function.
615  */
616 int extcon_dev_register(struct extcon_dev *edev)
617 {
618 	int ret, index = 0;
619 	static atomic_t edev_no = ATOMIC_INIT(-1);
620 
621 	if (!extcon_class) {
622 		ret = create_extcon_class();
623 		if (ret < 0)
624 			return ret;
625 	}
626 
627 	if (!edev || !edev->supported_cable)
628 		return -EINVAL;
629 
630 	for (; edev->supported_cable[index] != EXTCON_NONE; index++);
631 
632 	edev->max_supported = index;
633 	if (index > SUPPORTED_CABLE_MAX) {
634 		dev_err(&edev->dev,
635 			"exceed the maximum number of supported cables\n");
636 		return -EINVAL;
637 	}
638 
639 	edev->dev.class = extcon_class;
640 	edev->dev.release = extcon_dev_release;
641 
642 	edev->name = dev_name(edev->dev.parent);
643 	if (IS_ERR_OR_NULL(edev->name)) {
644 		dev_err(&edev->dev,
645 			"extcon device name is null\n");
646 		return -EINVAL;
647 	}
648 	dev_set_name(&edev->dev, "extcon%lu",
649 			(unsigned long)atomic_inc_return(&edev_no));
650 
651 	if (edev->max_supported) {
652 		char buf[10];
653 		char *str;
654 		struct extcon_cable *cable;
655 
656 		edev->cables = kzalloc(sizeof(struct extcon_cable) *
657 				       edev->max_supported, GFP_KERNEL);
658 		if (!edev->cables) {
659 			ret = -ENOMEM;
660 			goto err_sysfs_alloc;
661 		}
662 		for (index = 0; index < edev->max_supported; index++) {
663 			cable = &edev->cables[index];
664 
665 			snprintf(buf, 10, "cable.%d", index);
666 			str = kzalloc(sizeof(char) * (strlen(buf) + 1),
667 				      GFP_KERNEL);
668 			if (!str) {
669 				for (index--; index >= 0; index--) {
670 					cable = &edev->cables[index];
671 					kfree(cable->attr_g.name);
672 				}
673 				ret = -ENOMEM;
674 
675 				goto err_alloc_cables;
676 			}
677 			strcpy(str, buf);
678 
679 			cable->edev = edev;
680 			cable->cable_index = index;
681 			cable->attrs[0] = &cable->attr_name.attr;
682 			cable->attrs[1] = &cable->attr_state.attr;
683 			cable->attrs[2] = NULL;
684 			cable->attr_g.name = str;
685 			cable->attr_g.attrs = cable->attrs;
686 
687 			sysfs_attr_init(&cable->attr_name.attr);
688 			cable->attr_name.attr.name = "name";
689 			cable->attr_name.attr.mode = 0444;
690 			cable->attr_name.show = cable_name_show;
691 
692 			sysfs_attr_init(&cable->attr_state.attr);
693 			cable->attr_state.attr.name = "state";
694 			cable->attr_state.attr.mode = 0444;
695 			cable->attr_state.show = cable_state_show;
696 		}
697 	}
698 
699 	if (edev->max_supported && edev->mutually_exclusive) {
700 		char buf[80];
701 		char *name;
702 
703 		/* Count the size of mutually_exclusive array */
704 		for (index = 0; edev->mutually_exclusive[index]; index++)
705 			;
706 
707 		edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
708 					   (index + 1), GFP_KERNEL);
709 		if (!edev->attrs_muex) {
710 			ret = -ENOMEM;
711 			goto err_muex;
712 		}
713 
714 		edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
715 					     index, GFP_KERNEL);
716 		if (!edev->d_attrs_muex) {
717 			ret = -ENOMEM;
718 			kfree(edev->attrs_muex);
719 			goto err_muex;
720 		}
721 
722 		for (index = 0; edev->mutually_exclusive[index]; index++) {
723 			sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
724 			name = kzalloc(sizeof(char) * (strlen(buf) + 1),
725 				       GFP_KERNEL);
726 			if (!name) {
727 				for (index--; index >= 0; index--) {
728 					kfree(edev->d_attrs_muex[index].attr.
729 					      name);
730 				}
731 				kfree(edev->d_attrs_muex);
732 				kfree(edev->attrs_muex);
733 				ret = -ENOMEM;
734 				goto err_muex;
735 			}
736 			strcpy(name, buf);
737 			sysfs_attr_init(&edev->d_attrs_muex[index].attr);
738 			edev->d_attrs_muex[index].attr.name = name;
739 			edev->d_attrs_muex[index].attr.mode = 0000;
740 			edev->attrs_muex[index] = &edev->d_attrs_muex[index]
741 							.attr;
742 		}
743 		edev->attr_g_muex.name = muex_name;
744 		edev->attr_g_muex.attrs = edev->attrs_muex;
745 
746 	}
747 
748 	if (edev->max_supported) {
749 		edev->extcon_dev_type.groups =
750 			kzalloc(sizeof(struct attribute_group *) *
751 				(edev->max_supported + 2), GFP_KERNEL);
752 		if (!edev->extcon_dev_type.groups) {
753 			ret = -ENOMEM;
754 			goto err_alloc_groups;
755 		}
756 
757 		edev->extcon_dev_type.name = dev_name(&edev->dev);
758 		edev->extcon_dev_type.release = dummy_sysfs_dev_release;
759 
760 		for (index = 0; index < edev->max_supported; index++)
761 			edev->extcon_dev_type.groups[index] =
762 				&edev->cables[index].attr_g;
763 		if (edev->mutually_exclusive)
764 			edev->extcon_dev_type.groups[index] =
765 				&edev->attr_g_muex;
766 
767 		edev->dev.type = &edev->extcon_dev_type;
768 	}
769 
770 	ret = device_register(&edev->dev);
771 	if (ret) {
772 		put_device(&edev->dev);
773 		goto err_dev;
774 	}
775 #if defined(CONFIG_ANDROID)
776 	if (switch_class)
777 		ret = class_compat_create_link(switch_class, &edev->dev, NULL);
778 #endif /* CONFIG_ANDROID */
779 
780 	spin_lock_init(&edev->lock);
781 
782 	edev->nh = devm_kzalloc(&edev->dev,
783 			sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
784 	if (!edev->nh) {
785 		ret = -ENOMEM;
786 		goto err_dev;
787 	}
788 
789 	for (index = 0; index < edev->max_supported; index++)
790 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
791 
792 	dev_set_drvdata(&edev->dev, edev);
793 	edev->state = 0;
794 
795 	mutex_lock(&extcon_dev_list_lock);
796 	list_add(&edev->entry, &extcon_dev_list);
797 	mutex_unlock(&extcon_dev_list_lock);
798 
799 	return 0;
800 
801 err_dev:
802 	if (edev->max_supported)
803 		kfree(edev->extcon_dev_type.groups);
804 err_alloc_groups:
805 	if (edev->max_supported && edev->mutually_exclusive) {
806 		for (index = 0; edev->mutually_exclusive[index]; index++)
807 			kfree(edev->d_attrs_muex[index].attr.name);
808 		kfree(edev->d_attrs_muex);
809 		kfree(edev->attrs_muex);
810 	}
811 err_muex:
812 	for (index = 0; index < edev->max_supported; index++)
813 		kfree(edev->cables[index].attr_g.name);
814 err_alloc_cables:
815 	if (edev->max_supported)
816 		kfree(edev->cables);
817 err_sysfs_alloc:
818 	return ret;
819 }
820 EXPORT_SYMBOL_GPL(extcon_dev_register);
821 
822 /**
823  * extcon_dev_unregister() - Unregister the extcon device.
824  * @edev:	the extcon device instance to be unregistered.
825  *
826  * Note that this does not call kfree(edev) because edev was not allocated
827  * by this class.
828  */
829 void extcon_dev_unregister(struct extcon_dev *edev)
830 {
831 	int index;
832 
833 	if (!edev)
834 		return;
835 
836 	mutex_lock(&extcon_dev_list_lock);
837 	list_del(&edev->entry);
838 	mutex_unlock(&extcon_dev_list_lock);
839 
840 	if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
841 		dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
842 				dev_name(&edev->dev));
843 		return;
844 	}
845 
846 	device_unregister(&edev->dev);
847 
848 	if (edev->mutually_exclusive && edev->max_supported) {
849 		for (index = 0; edev->mutually_exclusive[index];
850 				index++)
851 			kfree(edev->d_attrs_muex[index].attr.name);
852 		kfree(edev->d_attrs_muex);
853 		kfree(edev->attrs_muex);
854 	}
855 
856 	for (index = 0; index < edev->max_supported; index++)
857 		kfree(edev->cables[index].attr_g.name);
858 
859 	if (edev->max_supported) {
860 		kfree(edev->extcon_dev_type.groups);
861 		kfree(edev->cables);
862 	}
863 
864 #if defined(CONFIG_ANDROID)
865 	if (switch_class)
866 		class_compat_remove_link(switch_class, &edev->dev, NULL);
867 #endif
868 	put_device(&edev->dev);
869 }
870 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
871 
872 static void devm_extcon_dev_unreg(struct device *dev, void *res)
873 {
874 	extcon_dev_unregister(*(struct extcon_dev **)res);
875 }
876 
877 /**
878  * devm_extcon_dev_register() - Resource-managed extcon_dev_register()
879  * @dev:	device to allocate extcon device
880  * @edev:	the new extcon device to register
881  *
882  * Managed extcon_dev_register() function. If extcon device is attached with
883  * this function, that extcon device is automatically unregistered on driver
884  * detach. Internally this function calls extcon_dev_register() function.
885  * To get more information, refer that function.
886  *
887  * If extcon device is registered with this function and the device needs to be
888  * unregistered separately, devm_extcon_dev_unregister() should be used.
889  *
890  * Returns 0 if success or negaive error number if failure.
891  */
892 int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev)
893 {
894 	struct extcon_dev **ptr;
895 	int ret;
896 
897 	ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL);
898 	if (!ptr)
899 		return -ENOMEM;
900 
901 	ret = extcon_dev_register(edev);
902 	if (ret) {
903 		devres_free(ptr);
904 		return ret;
905 	}
906 
907 	*ptr = edev;
908 	devres_add(dev, ptr);
909 
910 	return 0;
911 }
912 EXPORT_SYMBOL_GPL(devm_extcon_dev_register);
913 
914 /**
915  * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister()
916  * @dev:	device the extcon belongs to
917  * @edev:	the extcon device to unregister
918  *
919  * Unregister extcon device that is registered with devm_extcon_dev_register()
920  * function.
921  */
922 void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev)
923 {
924 	WARN_ON(devres_release(dev, devm_extcon_dev_unreg,
925 			       devm_extcon_dev_match, edev));
926 }
927 EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister);
928 
929 #ifdef CONFIG_OF
930 /*
931  * extcon_get_edev_by_phandle - Get the extcon device from devicetree
932  * @dev - instance to the given device
933  * @index - index into list of extcon_dev
934  *
935  * return the instance of extcon device
936  */
937 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
938 {
939 	struct device_node *node;
940 	struct extcon_dev *edev;
941 
942 	if (!dev)
943 		return ERR_PTR(-EINVAL);
944 
945 	if (!dev->of_node) {
946 		dev_err(dev, "device does not have a device node entry\n");
947 		return ERR_PTR(-EINVAL);
948 	}
949 
950 	node = of_parse_phandle(dev->of_node, "extcon", index);
951 	if (!node) {
952 		dev_err(dev, "failed to get phandle in %s node\n",
953 			dev->of_node->full_name);
954 		return ERR_PTR(-ENODEV);
955 	}
956 
957 	mutex_lock(&extcon_dev_list_lock);
958 	list_for_each_entry(edev, &extcon_dev_list, entry) {
959 		if (edev->dev.parent && edev->dev.parent->of_node == node) {
960 			mutex_unlock(&extcon_dev_list_lock);
961 			return edev;
962 		}
963 	}
964 	mutex_unlock(&extcon_dev_list_lock);
965 
966 	return ERR_PTR(-EPROBE_DEFER);
967 }
968 #else
969 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
970 {
971 	return ERR_PTR(-ENOSYS);
972 }
973 #endif /* CONFIG_OF */
974 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
975 
976 /**
977  * extcon_get_edev_name() - Get the name of the extcon device.
978  * @edev:	the extcon device
979  */
980 const char *extcon_get_edev_name(struct extcon_dev *edev)
981 {
982 	return !edev ? NULL : edev->name;
983 }
984 
985 static int __init extcon_class_init(void)
986 {
987 	return create_extcon_class();
988 }
989 module_init(extcon_class_init);
990 
991 static void __exit extcon_class_exit(void)
992 {
993 #if defined(CONFIG_ANDROID)
994 	class_compat_unregister(switch_class);
995 #endif
996 	class_destroy(extcon_class);
997 }
998 module_exit(extcon_class_exit);
999 
1000 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1001 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1002 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1003 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1004 MODULE_DESCRIPTION("External connector (extcon) class driver");
1005 MODULE_LICENSE("GPL");
1006