xref: /OK3568_Linux_fs/kernel/drivers/extcon/extcon.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * drivers/extcon/extcon.c - External Connector (extcon) framework.
4  *
5  * Copyright (C) 2015 Samsung Electronics
6  * Author: Chanwoo Choi <cw00.choi@samsung.com>
7  *
8  * Copyright (C) 2012 Samsung Electronics
9  * Author: Donggeun Kim <dg77.kim@samsung.com>
10  * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
11  *
12  * based on android/drivers/switch/switch_class.c
13  * Copyright (C) 2008 Google, Inc.
14  * Author: Mike Lockwood <lockwood@android.com>
15  */
16 
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/device.h>
21 #include <linux/fs.h>
22 #include <linux/err.h>
23 #include <linux/of.h>
24 #include <linux/slab.h>
25 #include <linux/sysfs.h>
26 
27 #include "extcon.h"
28 
29 #define SUPPORTED_CABLE_MAX	32
30 
31 static const struct __extcon_info {
32 	unsigned int type;
33 	unsigned int id;
34 	const char *name;
35 
36 } extcon_info[] = {
37 	[EXTCON_NONE] = {
38 		.type = EXTCON_TYPE_MISC,
39 		.id = EXTCON_NONE,
40 		.name = "NONE",
41 	},
42 
43 	/* USB external connector */
44 	[EXTCON_USB] = {
45 		.type = EXTCON_TYPE_USB,
46 		.id = EXTCON_USB,
47 		.name = "USB",
48 	},
49 	[EXTCON_USB_HOST] = {
50 		.type = EXTCON_TYPE_USB,
51 		.id = EXTCON_USB_HOST,
52 		.name = "USB-HOST",
53 	},
54 	[EXTCON_USB_VBUS_EN] = {
55 		.type = EXTCON_TYPE_USB,
56 		.id = EXTCON_USB_VBUS_EN,
57 		.name = "USB_VBUS_EN",
58 	},
59 
60 	/* Charging external connector */
61 	[EXTCON_CHG_USB_SDP] = {
62 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
63 		.id = EXTCON_CHG_USB_SDP,
64 		.name = "SDP",
65 	},
66 	[EXTCON_CHG_USB_DCP] = {
67 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
68 		.id = EXTCON_CHG_USB_DCP,
69 		.name = "DCP",
70 	},
71 	[EXTCON_CHG_USB_CDP] = {
72 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 		.id = EXTCON_CHG_USB_CDP,
74 		.name = "CDP",
75 	},
76 	[EXTCON_CHG_USB_ACA] = {
77 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 		.id = EXTCON_CHG_USB_ACA,
79 		.name = "ACA",
80 	},
81 	[EXTCON_CHG_USB_FAST] = {
82 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 		.id = EXTCON_CHG_USB_FAST,
84 		.name = "FAST-CHARGER",
85 	},
86 	[EXTCON_CHG_USB_SLOW] = {
87 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
88 		.id = EXTCON_CHG_USB_SLOW,
89 		.name = "SLOW-CHARGER",
90 	},
91 	[EXTCON_CHG_WPT] = {
92 		.type = EXTCON_TYPE_CHG,
93 		.id = EXTCON_CHG_WPT,
94 		.name = "WPT",
95 	},
96 	[EXTCON_CHG_USB_PD] = {
97 		.type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
98 		.id = EXTCON_CHG_USB_PD,
99 		.name = "PD",
100 	},
101 
102 	/* Jack external connector */
103 	[EXTCON_JACK_MICROPHONE] = {
104 		.type = EXTCON_TYPE_JACK,
105 		.id = EXTCON_JACK_MICROPHONE,
106 		.name = "MICROPHONE",
107 	},
108 	[EXTCON_JACK_HEADPHONE] = {
109 		.type = EXTCON_TYPE_JACK,
110 		.id = EXTCON_JACK_HEADPHONE,
111 		.name = "HEADPHONE",
112 	},
113 	[EXTCON_JACK_LINE_IN] = {
114 		.type = EXTCON_TYPE_JACK,
115 		.id = EXTCON_JACK_LINE_IN,
116 		.name = "LINE-IN",
117 	},
118 	[EXTCON_JACK_LINE_OUT] = {
119 		.type = EXTCON_TYPE_JACK,
120 		.id = EXTCON_JACK_LINE_OUT,
121 		.name = "LINE-OUT",
122 	},
123 	[EXTCON_JACK_VIDEO_IN] = {
124 		.type = EXTCON_TYPE_JACK,
125 		.id = EXTCON_JACK_VIDEO_IN,
126 		.name = "VIDEO-IN",
127 	},
128 	[EXTCON_JACK_VIDEO_OUT] = {
129 		.type = EXTCON_TYPE_JACK,
130 		.id = EXTCON_JACK_VIDEO_OUT,
131 		.name = "VIDEO-OUT",
132 	},
133 	[EXTCON_JACK_SPDIF_IN] = {
134 		.type = EXTCON_TYPE_JACK,
135 		.id = EXTCON_JACK_SPDIF_IN,
136 		.name = "SPDIF-IN",
137 	},
138 	[EXTCON_JACK_SPDIF_OUT] = {
139 		.type = EXTCON_TYPE_JACK,
140 		.id = EXTCON_JACK_SPDIF_OUT,
141 		.name = "SPDIF-OUT",
142 	},
143 
144 	/* Display external connector */
145 	[EXTCON_DISP_HDMI] = {
146 		.type = EXTCON_TYPE_DISP,
147 		.id = EXTCON_DISP_HDMI,
148 		.name = "HDMI",
149 	},
150 	[EXTCON_DISP_MHL] = {
151 		.type = EXTCON_TYPE_DISP,
152 		.id = EXTCON_DISP_MHL,
153 		.name = "MHL",
154 	},
155 	[EXTCON_DISP_DVI] = {
156 		.type = EXTCON_TYPE_DISP,
157 		.id = EXTCON_DISP_DVI,
158 		.name = "DVI",
159 	},
160 	[EXTCON_DISP_VGA] = {
161 		.type = EXTCON_TYPE_DISP,
162 		.id = EXTCON_DISP_VGA,
163 		.name = "VGA",
164 	},
165 	[EXTCON_DISP_DP] = {
166 		.type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
167 		.id = EXTCON_DISP_DP,
168 		.name = "DP",
169 	},
170 	[EXTCON_DISP_HMD] = {
171 		.type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
172 		.id = EXTCON_DISP_HMD,
173 		.name = "HMD",
174 	},
175 	[EXTCON_DISP_CVBS] = {
176 		.type = EXTCON_TYPE_DISP,
177 		.id = EXTCON_DISP_CVBS,
178 		.name = "CVBS",
179 	},
180 	[EXTCON_DISP_EDP] = {
181 		.type = EXTCON_TYPE_DISP,
182 		.id = EXTCON_DISP_EDP,
183 		.name = "EDP",
184 	},
185 
186 	/* Miscellaneous external connector */
187 	[EXTCON_DOCK] = {
188 		.type = EXTCON_TYPE_MISC,
189 		.id = EXTCON_DOCK,
190 		.name = "DOCK",
191 	},
192 	[EXTCON_JIG] = {
193 		.type = EXTCON_TYPE_MISC,
194 		.id = EXTCON_JIG,
195 		.name = "JIG",
196 	},
197 	[EXTCON_MECHANICAL] = {
198 		.type = EXTCON_TYPE_MISC,
199 		.id = EXTCON_MECHANICAL,
200 		.name = "MECHANICAL",
201 	},
202 
203 	{ /* sentinel */ }
204 };
205 
206 /**
207  * struct extcon_cable - An internal data for an external connector.
208  * @edev:		the extcon device
209  * @cable_index:	the index of this cable in the edev
210  * @attr_g:		the attribute group for the cable
211  * @attr_name:		"name" sysfs entry
212  * @attr_state:		"state" sysfs entry
213  * @attrs:		the array pointing to attr_name and attr_state for attr_g
214  */
215 struct extcon_cable {
216 	struct extcon_dev *edev;
217 	int cable_index;
218 
219 	struct attribute_group attr_g;
220 	struct device_attribute attr_name;
221 	struct device_attribute attr_state;
222 
223 	struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
224 
225 	union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
226 	union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
227 	union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
228 	union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
229 
230 	unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
231 	unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
232 	unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
233 	unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
234 };
235 
236 static struct class *extcon_class;
237 
238 static LIST_HEAD(extcon_dev_list);
239 static DEFINE_MUTEX(extcon_dev_list_lock);
240 
check_mutually_exclusive(struct extcon_dev * edev,u32 new_state)241 static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
242 {
243 	int i = 0;
244 
245 	if (!edev->mutually_exclusive)
246 		return 0;
247 
248 	for (i = 0; edev->mutually_exclusive[i]; i++) {
249 		int weight;
250 		u32 correspondants = new_state & edev->mutually_exclusive[i];
251 
252 		/* calculate the total number of bits set */
253 		weight = hweight32(correspondants);
254 		if (weight > 1)
255 			return i + 1;
256 	}
257 
258 	return 0;
259 }
260 
find_cable_index_by_id(struct extcon_dev * edev,const unsigned int id)261 static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
262 {
263 	int i;
264 
265 	/* Find the the index of extcon cable in edev->supported_cable */
266 	for (i = 0; i < edev->max_supported; i++) {
267 		if (edev->supported_cable[i] == id)
268 			return i;
269 	}
270 
271 	return -EINVAL;
272 }
273 
get_extcon_type(unsigned int prop)274 static int get_extcon_type(unsigned int prop)
275 {
276 	switch (prop) {
277 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
278 		return EXTCON_TYPE_USB;
279 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
280 		return EXTCON_TYPE_CHG;
281 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
282 		return EXTCON_TYPE_JACK;
283 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
284 		return EXTCON_TYPE_DISP;
285 	default:
286 		return -EINVAL;
287 	}
288 }
289 
is_extcon_attached(struct extcon_dev * edev,unsigned int index)290 static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
291 {
292 	return !!(edev->state & BIT(index));
293 }
294 
is_extcon_changed(struct extcon_dev * edev,int index,bool new_state)295 static bool is_extcon_changed(struct extcon_dev *edev, int index,
296 				bool new_state)
297 {
298 	int state = !!(edev->state & BIT(index));
299 	return (state != new_state);
300 }
301 
is_extcon_property_supported(unsigned int id,unsigned int prop)302 static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
303 {
304 	int type;
305 
306 	/* Check whether the property is supported or not. */
307 	type = get_extcon_type(prop);
308 	if (type < 0)
309 		return false;
310 
311 	/* Check whether a specific extcon id supports the property or not. */
312 	return !!(extcon_info[id].type & type);
313 }
314 
is_extcon_property_capability(struct extcon_dev * edev,unsigned int id,int index,unsigned int prop)315 static int is_extcon_property_capability(struct extcon_dev *edev,
316 				unsigned int id, int index,unsigned int prop)
317 {
318 	struct extcon_cable *cable;
319 	int type, ret;
320 
321 	/* Check whether the property is supported or not. */
322 	type = get_extcon_type(prop);
323 	if (type < 0)
324 		return type;
325 
326 	cable = &edev->cables[index];
327 
328 	switch (type) {
329 	case EXTCON_TYPE_USB:
330 		ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
331 		break;
332 	case EXTCON_TYPE_CHG:
333 		ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
334 		break;
335 	case EXTCON_TYPE_JACK:
336 		ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
337 		break;
338 	case EXTCON_TYPE_DISP:
339 		ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
340 		break;
341 	default:
342 		ret = -EINVAL;
343 	}
344 
345 	return ret;
346 }
347 
init_property(struct extcon_dev * edev,unsigned int id,int index)348 static void init_property(struct extcon_dev *edev, unsigned int id, int index)
349 {
350 	unsigned int type = extcon_info[id].type;
351 	struct extcon_cable *cable = &edev->cables[index];
352 
353 	if (EXTCON_TYPE_USB & type)
354 		memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
355 	if (EXTCON_TYPE_CHG & type)
356 		memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
357 	if (EXTCON_TYPE_JACK & type)
358 		memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
359 	if (EXTCON_TYPE_DISP & type)
360 		memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
361 }
362 
state_show(struct device * dev,struct device_attribute * attr,char * buf)363 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
364 			  char *buf)
365 {
366 	int i, count = 0;
367 	struct extcon_dev *edev = dev_get_drvdata(dev);
368 
369 	if (edev->max_supported == 0)
370 		return sprintf(buf, "%u\n", edev->state);
371 
372 	for (i = 0; i < edev->max_supported; i++) {
373 		count += sprintf(buf + count, "%s=%d\n",
374 				extcon_info[edev->supported_cable[i]].name,
375 				 !!(edev->state & BIT(i)));
376 	}
377 
378 	return count;
379 }
380 static DEVICE_ATTR_RO(state);
381 
name_show(struct device * dev,struct device_attribute * attr,char * buf)382 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
383 		char *buf)
384 {
385 	struct extcon_dev *edev = dev_get_drvdata(dev);
386 
387 	return sprintf(buf, "%s\n", edev->name);
388 }
389 static DEVICE_ATTR_RO(name);
390 
cable_name_show(struct device * dev,struct device_attribute * attr,char * buf)391 static ssize_t cable_name_show(struct device *dev,
392 			       struct device_attribute *attr, char *buf)
393 {
394 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
395 						  attr_name);
396 	int i = cable->cable_index;
397 
398 	return sprintf(buf, "%s\n",
399 			extcon_info[cable->edev->supported_cable[i]].name);
400 }
401 
cable_state_show(struct device * dev,struct device_attribute * attr,char * buf)402 static ssize_t cable_state_show(struct device *dev,
403 				struct device_attribute *attr, char *buf)
404 {
405 	struct extcon_cable *cable = container_of(attr, struct extcon_cable,
406 						  attr_state);
407 
408 	int i = cable->cable_index;
409 
410 	return sprintf(buf, "%d\n",
411 		extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
412 }
413 
414 /**
415  * extcon_sync() - Synchronize the state for an external connector.
416  * @edev:	the extcon device
417  *
418  * Note that this function send a notification in order to synchronize
419  * the state and property of an external connector.
420  *
421  * Returns 0 if success or error number if fail.
422  */
extcon_sync(struct extcon_dev * edev,unsigned int id)423 int extcon_sync(struct extcon_dev *edev, unsigned int id)
424 {
425 	char name_buf[120];
426 	char state_buf[120];
427 	char *prop_buf;
428 	char *envp[3];
429 	int env_offset = 0;
430 	int length;
431 	int index;
432 	int state;
433 	unsigned long flags;
434 
435 	if (!edev)
436 		return -EINVAL;
437 
438 	index = find_cable_index_by_id(edev, id);
439 	if (index < 0)
440 		return index;
441 
442 	spin_lock_irqsave(&edev->lock, flags);
443 	state = !!(edev->state & BIT(index));
444 	spin_unlock_irqrestore(&edev->lock, flags);
445 
446 	/*
447 	 * Call functions in a raw notifier chain for the specific one
448 	 * external connector.
449 	 */
450 	raw_notifier_call_chain(&edev->nh[index], state, edev);
451 
452 	/*
453 	 * Call functions in a raw notifier chain for the all supported
454 	 * external connectors.
455 	 */
456 	raw_notifier_call_chain(&edev->nh_all, state, edev);
457 
458 	spin_lock_irqsave(&edev->lock, flags);
459 	/* This could be in interrupt handler */
460 	prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
461 	if (!prop_buf) {
462 		/* Unlock early before uevent */
463 		spin_unlock_irqrestore(&edev->lock, flags);
464 
465 		dev_err(&edev->dev, "out of memory in extcon_set_state\n");
466 		kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
467 
468 		return -ENOMEM;
469 	}
470 
471 	length = name_show(&edev->dev, NULL, prop_buf);
472 	if (length > 0) {
473 		if (prop_buf[length - 1] == '\n')
474 			prop_buf[length - 1] = 0;
475 		snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
476 		envp[env_offset++] = name_buf;
477 	}
478 
479 	length = state_show(&edev->dev, NULL, prop_buf);
480 	if (length > 0) {
481 		if (prop_buf[length - 1] == '\n')
482 			prop_buf[length - 1] = 0;
483 		snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
484 		envp[env_offset++] = state_buf;
485 	}
486 	envp[env_offset] = NULL;
487 
488 	/* Unlock early before uevent */
489 	spin_unlock_irqrestore(&edev->lock, flags);
490 	kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
491 	free_page((unsigned long)prop_buf);
492 
493 	return 0;
494 }
495 EXPORT_SYMBOL_GPL(extcon_sync);
496 
497 /**
498  * extcon_get_state() - Get the state of an external connector.
499  * @edev:	the extcon device
500  * @id:		the unique id indicating an external connector
501  *
502  * Returns 0 if success or error number if fail.
503  */
extcon_get_state(struct extcon_dev * edev,const unsigned int id)504 int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
505 {
506 	int index, state;
507 	unsigned long flags;
508 
509 	if (!edev)
510 		return -EINVAL;
511 
512 	index = find_cable_index_by_id(edev, id);
513 	if (index < 0)
514 		return index;
515 
516 	spin_lock_irqsave(&edev->lock, flags);
517 	state = is_extcon_attached(edev, index);
518 	spin_unlock_irqrestore(&edev->lock, flags);
519 
520 	return state;
521 }
522 EXPORT_SYMBOL_GPL(extcon_get_state);
523 
524 /**
525  * extcon_set_state() - Set the state of an external connector.
526  * @edev:	the extcon device
527  * @id:		the unique id indicating an external connector
528  * @state:	the new state of an external connector.
529  *		the default semantics is true: attached / false: detached.
530  *
531  * Note that this function set the state of an external connector without
532  * a notification. To synchronize the state of an external connector,
533  * have to use extcon_set_state_sync() and extcon_sync().
534  *
535  * Returns 0 if success or error number if fail.
536  */
extcon_set_state(struct extcon_dev * edev,unsigned int id,bool state)537 int extcon_set_state(struct extcon_dev *edev, unsigned int id, bool state)
538 {
539 	unsigned long flags;
540 	int index, ret = 0;
541 
542 	if (!edev)
543 		return -EINVAL;
544 
545 	index = find_cable_index_by_id(edev, id);
546 	if (index < 0)
547 		return index;
548 
549 	spin_lock_irqsave(&edev->lock, flags);
550 
551 	/* Check whether the external connector's state is changed. */
552 	if (!is_extcon_changed(edev, index, state))
553 		goto out;
554 
555 	if (check_mutually_exclusive(edev,
556 		(edev->state & ~BIT(index)) | (state & BIT(index)))) {
557 		ret = -EPERM;
558 		goto out;
559 	}
560 
561 	/*
562 	 * Initialize the value of extcon property before setting
563 	 * the detached state for an external connector.
564 	 */
565 	if (!state)
566 		init_property(edev, id, index);
567 
568 	/* Update the state for an external connector. */
569 	if (state)
570 		edev->state |= BIT(index);
571 	else
572 		edev->state &= ~(BIT(index));
573 out:
574 	spin_unlock_irqrestore(&edev->lock, flags);
575 
576 	return ret;
577 }
578 EXPORT_SYMBOL_GPL(extcon_set_state);
579 
580 /**
581  * extcon_set_state_sync() - Set the state of an external connector with sync.
582  * @edev:	the extcon device
583  * @id:		the unique id indicating an external connector
584  * @state:	the new state of external connector.
585  *		the default semantics is true: attached / false: detached.
586  *
587  * Note that this function set the state of external connector
588  * and synchronize the state by sending a notification.
589  *
590  * Returns 0 if success or error number if fail.
591  */
extcon_set_state_sync(struct extcon_dev * edev,unsigned int id,bool state)592 int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, bool state)
593 {
594 	int ret, index;
595 	unsigned long flags;
596 
597 	index = find_cable_index_by_id(edev, id);
598 	if (index < 0)
599 		return index;
600 
601 	/* Check whether the external connector's state is changed. */
602 	spin_lock_irqsave(&edev->lock, flags);
603 	ret = is_extcon_changed(edev, index, state);
604 	spin_unlock_irqrestore(&edev->lock, flags);
605 	if (!ret)
606 		return 0;
607 
608 	ret = extcon_set_state(edev, id, state);
609 	if (ret < 0)
610 		return ret;
611 
612 	return extcon_sync(edev, id);
613 }
614 EXPORT_SYMBOL_GPL(extcon_set_state_sync);
615 
616 /**
617  * extcon_get_property() - Get the property value of an external connector.
618  * @edev:	the extcon device
619  * @id:		the unique id indicating an external connector
620  * @prop:	the property id indicating an extcon property
621  * @prop_val:	the pointer which store the value of extcon property
622  *
623  * Note that when getting the property value of external connector,
624  * the external connector should be attached. If detached state, function
625  * return 0 without property value. Also, the each property should be
626  * included in the list of supported properties according to extcon type.
627  *
628  * Returns 0 if success or error number if fail.
629  */
extcon_get_property(struct extcon_dev * edev,unsigned int id,unsigned int prop,union extcon_property_value * prop_val)630 int extcon_get_property(struct extcon_dev *edev, unsigned int id,
631 				unsigned int prop,
632 				union extcon_property_value *prop_val)
633 {
634 	struct extcon_cable *cable;
635 	unsigned long flags;
636 	int index, ret = 0;
637 
638 	*prop_val = (union extcon_property_value){0};
639 
640 	if (!edev)
641 		return -EINVAL;
642 
643 	/* Check whether the property is supported or not */
644 	if (!is_extcon_property_supported(id, prop))
645 		return -EINVAL;
646 
647 	/* Find the cable index of external connector by using id */
648 	index = find_cable_index_by_id(edev, id);
649 	if (index < 0)
650 		return index;
651 
652 	spin_lock_irqsave(&edev->lock, flags);
653 
654 	/* Check whether the property is available or not. */
655 	if (!is_extcon_property_capability(edev, id, index, prop)) {
656 		spin_unlock_irqrestore(&edev->lock, flags);
657 		return -EPERM;
658 	}
659 
660 	/*
661 	 * Check whether the external connector is attached.
662 	 * If external connector is detached, the user can not
663 	 * get the property value.
664 	 */
665 	if (!is_extcon_attached(edev, index)) {
666 		spin_unlock_irqrestore(&edev->lock, flags);
667 		return 0;
668 	}
669 
670 	cable = &edev->cables[index];
671 
672 	/* Get the property value according to extcon type */
673 	switch (prop) {
674 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
675 		*prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
676 		break;
677 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
678 		*prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
679 		break;
680 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
681 		*prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
682 		break;
683 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
684 		*prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
685 		break;
686 	default:
687 		ret = -EINVAL;
688 		break;
689 	}
690 
691 	spin_unlock_irqrestore(&edev->lock, flags);
692 
693 	return ret;
694 }
695 EXPORT_SYMBOL_GPL(extcon_get_property);
696 
697 /**
698  * extcon_set_property() - Set the property value of an external connector.
699  * @edev:	the extcon device
700  * @id:		the unique id indicating an external connector
701  * @prop:	the property id indicating an extcon property
702  * @prop_val:	the pointer including the new value of extcon property
703  *
704  * Note that each property should be included in the list of supported
705  * properties according to the extcon type.
706  *
707  * Returns 0 if success or error number if fail.
708  */
extcon_set_property(struct extcon_dev * edev,unsigned int id,unsigned int prop,union extcon_property_value prop_val)709 int extcon_set_property(struct extcon_dev *edev, unsigned int id,
710 				unsigned int prop,
711 				union extcon_property_value prop_val)
712 {
713 	struct extcon_cable *cable;
714 	unsigned long flags;
715 	int index, ret = 0;
716 
717 	if (!edev)
718 		return -EINVAL;
719 
720 	/* Check whether the property is supported or not */
721 	if (!is_extcon_property_supported(id, prop))
722 		return -EINVAL;
723 
724 	/* Find the cable index of external connector by using id */
725 	index = find_cable_index_by_id(edev, id);
726 	if (index < 0)
727 		return index;
728 
729 	spin_lock_irqsave(&edev->lock, flags);
730 
731 	/* Check whether the property is available or not. */
732 	if (!is_extcon_property_capability(edev, id, index, prop)) {
733 		spin_unlock_irqrestore(&edev->lock, flags);
734 		return -EPERM;
735 	}
736 
737 	cable = &edev->cables[index];
738 
739 	/* Set the property value according to extcon type */
740 	switch (prop) {
741 	case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
742 		cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
743 		break;
744 	case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
745 		cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
746 		break;
747 	case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
748 		cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
749 		break;
750 	case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
751 		cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
752 		break;
753 	default:
754 		ret = -EINVAL;
755 		break;
756 	}
757 
758 	spin_unlock_irqrestore(&edev->lock, flags);
759 
760 	return ret;
761 }
762 EXPORT_SYMBOL_GPL(extcon_set_property);
763 
764 /**
765  * extcon_set_property_sync() - Set property of an external connector with sync.
766  * @prop_val:	the pointer including the new value of extcon property
767  *
768  * Note that when setting the property value of external connector,
769  * the external connector should be attached. The each property should
770  * be included in the list of supported properties according to extcon type.
771  *
772  * Returns 0 if success or error number if fail.
773  */
extcon_set_property_sync(struct extcon_dev * edev,unsigned int id,unsigned int prop,union extcon_property_value prop_val)774 int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
775 				unsigned int prop,
776 				union extcon_property_value prop_val)
777 {
778 	int ret;
779 
780 	ret = extcon_set_property(edev, id, prop, prop_val);
781 	if (ret < 0)
782 		return ret;
783 
784 	return extcon_sync(edev, id);
785 }
786 EXPORT_SYMBOL_GPL(extcon_set_property_sync);
787 
788 /**
789  * extcon_get_property_capability() - Get the capability of the property
790  *					for an external connector.
791  * @edev:	the extcon device
792  * @id:		the unique id indicating an external connector
793  * @prop:	the property id indicating an extcon property
794  *
795  * Returns 1 if the property is available or 0 if not available.
796  */
extcon_get_property_capability(struct extcon_dev * edev,unsigned int id,unsigned int prop)797 int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
798 					unsigned int prop)
799 {
800 	int index;
801 
802 	if (!edev)
803 		return -EINVAL;
804 
805 	/* Check whether the property is supported or not */
806 	if (!is_extcon_property_supported(id, prop))
807 		return -EINVAL;
808 
809 	/* Find the cable index of external connector by using id */
810 	index = find_cable_index_by_id(edev, id);
811 	if (index < 0)
812 		return index;
813 
814 	return is_extcon_property_capability(edev, id, index, prop);
815 }
816 EXPORT_SYMBOL_GPL(extcon_get_property_capability);
817 
818 /**
819  * extcon_set_property_capability() - Set the capability of the property
820  *					for an external connector.
821  * @edev:	the extcon device
822  * @id:		the unique id indicating an external connector
823  * @prop:	the property id indicating an extcon property
824  *
825  * Note that this function set the capability of the property
826  * for an external connector in order to mark the bit in capability
827  * bitmap which mean the available state of the property.
828  *
829  * Returns 0 if success or error number if fail.
830  */
extcon_set_property_capability(struct extcon_dev * edev,unsigned int id,unsigned int prop)831 int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
832 					unsigned int prop)
833 {
834 	struct extcon_cable *cable;
835 	int index, type, ret = 0;
836 
837 	if (!edev)
838 		return -EINVAL;
839 
840 	/* Check whether the property is supported or not. */
841 	if (!is_extcon_property_supported(id, prop))
842 		return -EINVAL;
843 
844 	/* Find the cable index of external connector by using id. */
845 	index = find_cable_index_by_id(edev, id);
846 	if (index < 0)
847 		return index;
848 
849 	type = get_extcon_type(prop);
850 	if (type < 0)
851 		return type;
852 
853 	cable = &edev->cables[index];
854 
855 	switch (type) {
856 	case EXTCON_TYPE_USB:
857 		__set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
858 		break;
859 	case EXTCON_TYPE_CHG:
860 		__set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
861 		break;
862 	case EXTCON_TYPE_JACK:
863 		__set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
864 		break;
865 	case EXTCON_TYPE_DISP:
866 		__set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
867 		break;
868 	default:
869 		ret = -EINVAL;
870 	}
871 
872 	return ret;
873 }
874 EXPORT_SYMBOL_GPL(extcon_set_property_capability);
875 
876 /**
877  * extcon_get_extcon_dev() - Get the extcon device instance from the name.
878  * @extcon_name:	the extcon name provided with extcon_dev_register()
879  *
880  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
881  */
extcon_get_extcon_dev(const char * extcon_name)882 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
883 {
884 	struct extcon_dev *sd;
885 
886 	if (!extcon_name)
887 		return ERR_PTR(-EINVAL);
888 
889 	mutex_lock(&extcon_dev_list_lock);
890 	list_for_each_entry(sd, &extcon_dev_list, entry) {
891 		if (!strcmp(sd->name, extcon_name))
892 			goto out;
893 	}
894 	sd = NULL;
895 out:
896 	mutex_unlock(&extcon_dev_list_lock);
897 	return sd;
898 }
899 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
900 
901 /**
902  * extcon_register_notifier() - Register a notifier block to get notified by
903  *				any state changes from the extcon.
904  * @edev:	the extcon device
905  * @id:		the unique id indicating an external connector
906  * @nb:		a notifier block to be registered
907  *
908  * Note that the second parameter given to the callback of nb (val) is
909  * the current state of an external connector and the third pameter
910  * is the pointer of extcon device.
911  *
912  * Returns 0 if success or error number if fail.
913  */
extcon_register_notifier(struct extcon_dev * edev,unsigned int id,struct notifier_block * nb)914 int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
915 			     struct notifier_block *nb)
916 {
917 	unsigned long flags;
918 	int ret, idx;
919 
920 	if (!edev || !nb)
921 		return -EINVAL;
922 
923 	idx = find_cable_index_by_id(edev, id);
924 	if (idx < 0)
925 		return idx;
926 
927 	spin_lock_irqsave(&edev->lock, flags);
928 	ret = raw_notifier_chain_register(&edev->nh[idx], nb);
929 	spin_unlock_irqrestore(&edev->lock, flags);
930 
931 	return ret;
932 }
933 EXPORT_SYMBOL_GPL(extcon_register_notifier);
934 
935 /**
936  * extcon_unregister_notifier() - Unregister a notifier block from the extcon.
937  * @edev:	the extcon device
938  * @id:		the unique id indicating an external connector
939  * @nb:		a notifier block to be registered
940  *
941  * Returns 0 if success or error number if fail.
942  */
extcon_unregister_notifier(struct extcon_dev * edev,unsigned int id,struct notifier_block * nb)943 int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
944 				struct notifier_block *nb)
945 {
946 	unsigned long flags;
947 	int ret, idx;
948 
949 	if (!edev || !nb)
950 		return -EINVAL;
951 
952 	idx = find_cable_index_by_id(edev, id);
953 	if (idx < 0)
954 		return idx;
955 
956 	spin_lock_irqsave(&edev->lock, flags);
957 	ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
958 	spin_unlock_irqrestore(&edev->lock, flags);
959 
960 	return ret;
961 }
962 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
963 
964 /**
965  * extcon_register_notifier_all() - Register a notifier block for all connectors.
966  * @edev:	the extcon device
967  * @nb:		a notifier block to be registered
968  *
969  * Note that this function registers a notifier block in order to receive
970  * the state change of all supported external connectors from extcon device.
971  * And the second parameter given to the callback of nb (val) is
972  * the current state and the third pameter is the pointer of extcon device.
973  *
974  * Returns 0 if success or error number if fail.
975  */
extcon_register_notifier_all(struct extcon_dev * edev,struct notifier_block * nb)976 int extcon_register_notifier_all(struct extcon_dev *edev,
977 				struct notifier_block *nb)
978 {
979 	unsigned long flags;
980 	int ret;
981 
982 	if (!edev || !nb)
983 		return -EINVAL;
984 
985 	spin_lock_irqsave(&edev->lock, flags);
986 	ret = raw_notifier_chain_register(&edev->nh_all, nb);
987 	spin_unlock_irqrestore(&edev->lock, flags);
988 
989 	return ret;
990 }
991 EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
992 
993 /**
994  * extcon_unregister_notifier_all() - Unregister a notifier block from extcon.
995  * @edev:	the extcon device
996  * @nb:		a notifier block to be registered
997  *
998  * Returns 0 if success or error number if fail.
999  */
extcon_unregister_notifier_all(struct extcon_dev * edev,struct notifier_block * nb)1000 int extcon_unregister_notifier_all(struct extcon_dev *edev,
1001 				struct notifier_block *nb)
1002 {
1003 	unsigned long flags;
1004 	int ret;
1005 
1006 	if (!edev || !nb)
1007 		return -EINVAL;
1008 
1009 	spin_lock_irqsave(&edev->lock, flags);
1010 	ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
1011 	spin_unlock_irqrestore(&edev->lock, flags);
1012 
1013 	return ret;
1014 }
1015 EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);
1016 
1017 static struct attribute *extcon_attrs[] = {
1018 	&dev_attr_state.attr,
1019 	&dev_attr_name.attr,
1020 	NULL,
1021 };
1022 ATTRIBUTE_GROUPS(extcon);
1023 
create_extcon_class(void)1024 static int create_extcon_class(void)
1025 {
1026 	if (!extcon_class) {
1027 		extcon_class = class_create(THIS_MODULE, "extcon");
1028 		if (IS_ERR(extcon_class))
1029 			return PTR_ERR(extcon_class);
1030 		extcon_class->dev_groups = extcon_groups;
1031 	}
1032 
1033 	return 0;
1034 }
1035 
extcon_dev_release(struct device * dev)1036 static void extcon_dev_release(struct device *dev)
1037 {
1038 }
1039 
1040 static const char *muex_name = "mutually_exclusive";
dummy_sysfs_dev_release(struct device * dev)1041 static void dummy_sysfs_dev_release(struct device *dev)
1042 {
1043 }
1044 
1045 /*
1046  * extcon_dev_allocate() - Allocate the memory of extcon device.
1047  * @supported_cable:	the array of the supported external connectors
1048  *			ending with EXTCON_NONE.
1049  *
1050  * Note that this function allocates the memory for extcon device
1051  * and initialize default setting for the extcon device.
1052  *
1053  * Returns the pointer memory of allocated extcon_dev if success
1054  * or ERR_PTR(err) if fail.
1055  */
extcon_dev_allocate(const unsigned int * supported_cable)1056 struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
1057 {
1058 	struct extcon_dev *edev;
1059 
1060 	if (!supported_cable)
1061 		return ERR_PTR(-EINVAL);
1062 
1063 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1064 	if (!edev)
1065 		return ERR_PTR(-ENOMEM);
1066 
1067 	edev->max_supported = 0;
1068 	edev->supported_cable = supported_cable;
1069 
1070 	return edev;
1071 }
1072 
1073 /*
1074  * extcon_dev_free() - Free the memory of extcon device.
1075  * @edev:	the extcon device
1076  */
extcon_dev_free(struct extcon_dev * edev)1077 void extcon_dev_free(struct extcon_dev *edev)
1078 {
1079 	kfree(edev);
1080 }
1081 EXPORT_SYMBOL_GPL(extcon_dev_free);
1082 
1083 /**
1084  * extcon_dev_register() - Register an new extcon device
1085  * @edev:	the extcon device to be registered
1086  *
1087  * Among the members of edev struct, please set the "user initializing data"
1088  * do not set the values of "internal data", which are initialized by
1089  * this function.
1090  *
1091  * Note that before calling this funciton, have to allocate the memory
1092  * of an extcon device by using the extcon_dev_allocate(). And the extcon
1093  * dev should include the supported_cable information.
1094  *
1095  * Returns 0 if success or error number if fail.
1096  */
extcon_dev_register(struct extcon_dev * edev)1097 int extcon_dev_register(struct extcon_dev *edev)
1098 {
1099 	int ret, index = 0;
1100 	static atomic_t edev_no = ATOMIC_INIT(-1);
1101 
1102 	if (!extcon_class) {
1103 		ret = create_extcon_class();
1104 		if (ret < 0)
1105 			return ret;
1106 	}
1107 
1108 	if (!edev || !edev->supported_cable)
1109 		return -EINVAL;
1110 
1111 	for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1112 
1113 	edev->max_supported = index;
1114 	if (index > SUPPORTED_CABLE_MAX) {
1115 		dev_err(&edev->dev,
1116 			"exceed the maximum number of supported cables\n");
1117 		return -EINVAL;
1118 	}
1119 
1120 	edev->dev.class = extcon_class;
1121 	edev->dev.release = extcon_dev_release;
1122 
1123 	edev->name = dev_name(edev->dev.parent);
1124 	if (IS_ERR_OR_NULL(edev->name)) {
1125 		dev_err(&edev->dev,
1126 			"extcon device name is null\n");
1127 		return -EINVAL;
1128 	}
1129 	dev_set_name(&edev->dev, "extcon%lu",
1130 			(unsigned long)atomic_inc_return(&edev_no));
1131 
1132 	if (edev->max_supported) {
1133 		char *str;
1134 		struct extcon_cable *cable;
1135 
1136 		edev->cables = kcalloc(edev->max_supported,
1137 				       sizeof(struct extcon_cable),
1138 				       GFP_KERNEL);
1139 		if (!edev->cables) {
1140 			ret = -ENOMEM;
1141 			goto err_sysfs_alloc;
1142 		}
1143 		for (index = 0; index < edev->max_supported; index++) {
1144 			cable = &edev->cables[index];
1145 
1146 			str = kasprintf(GFP_KERNEL, "cable.%d", index);
1147 			if (!str) {
1148 				for (index--; index >= 0; index--) {
1149 					cable = &edev->cables[index];
1150 					kfree(cable->attr_g.name);
1151 				}
1152 				ret = -ENOMEM;
1153 
1154 				goto err_alloc_cables;
1155 			}
1156 
1157 			cable->edev = edev;
1158 			cable->cable_index = index;
1159 			cable->attrs[0] = &cable->attr_name.attr;
1160 			cable->attrs[1] = &cable->attr_state.attr;
1161 			cable->attrs[2] = NULL;
1162 			cable->attr_g.name = str;
1163 			cable->attr_g.attrs = cable->attrs;
1164 
1165 			sysfs_attr_init(&cable->attr_name.attr);
1166 			cable->attr_name.attr.name = "name";
1167 			cable->attr_name.attr.mode = 0444;
1168 			cable->attr_name.show = cable_name_show;
1169 
1170 			sysfs_attr_init(&cable->attr_state.attr);
1171 			cable->attr_state.attr.name = "state";
1172 			cable->attr_state.attr.mode = 0444;
1173 			cable->attr_state.show = cable_state_show;
1174 		}
1175 	}
1176 
1177 	if (edev->max_supported && edev->mutually_exclusive) {
1178 		char *name;
1179 
1180 		/* Count the size of mutually_exclusive array */
1181 		for (index = 0; edev->mutually_exclusive[index]; index++)
1182 			;
1183 
1184 		edev->attrs_muex = kcalloc(index + 1,
1185 					   sizeof(struct attribute *),
1186 					   GFP_KERNEL);
1187 		if (!edev->attrs_muex) {
1188 			ret = -ENOMEM;
1189 			goto err_muex;
1190 		}
1191 
1192 		edev->d_attrs_muex = kcalloc(index,
1193 					     sizeof(struct device_attribute),
1194 					     GFP_KERNEL);
1195 		if (!edev->d_attrs_muex) {
1196 			ret = -ENOMEM;
1197 			kfree(edev->attrs_muex);
1198 			goto err_muex;
1199 		}
1200 
1201 		for (index = 0; edev->mutually_exclusive[index]; index++) {
1202 			name = kasprintf(GFP_KERNEL, "0x%x",
1203 					 edev->mutually_exclusive[index]);
1204 			if (!name) {
1205 				for (index--; index >= 0; index--) {
1206 					kfree(edev->d_attrs_muex[index].attr.
1207 					      name);
1208 				}
1209 				kfree(edev->d_attrs_muex);
1210 				kfree(edev->attrs_muex);
1211 				ret = -ENOMEM;
1212 				goto err_muex;
1213 			}
1214 			sysfs_attr_init(&edev->d_attrs_muex[index].attr);
1215 			edev->d_attrs_muex[index].attr.name = name;
1216 			edev->d_attrs_muex[index].attr.mode = 0000;
1217 			edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1218 							.attr;
1219 		}
1220 		edev->attr_g_muex.name = muex_name;
1221 		edev->attr_g_muex.attrs = edev->attrs_muex;
1222 
1223 	}
1224 
1225 	if (edev->max_supported) {
1226 		edev->extcon_dev_type.groups =
1227 			kcalloc(edev->max_supported + 2,
1228 				sizeof(struct attribute_group *),
1229 				GFP_KERNEL);
1230 		if (!edev->extcon_dev_type.groups) {
1231 			ret = -ENOMEM;
1232 			goto err_alloc_groups;
1233 		}
1234 
1235 		edev->extcon_dev_type.name = dev_name(&edev->dev);
1236 		edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1237 
1238 		for (index = 0; index < edev->max_supported; index++)
1239 			edev->extcon_dev_type.groups[index] =
1240 				&edev->cables[index].attr_g;
1241 		if (edev->mutually_exclusive)
1242 			edev->extcon_dev_type.groups[index] =
1243 				&edev->attr_g_muex;
1244 
1245 		edev->dev.type = &edev->extcon_dev_type;
1246 	}
1247 
1248 	spin_lock_init(&edev->lock);
1249 	if (edev->max_supported) {
1250 		edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh),
1251 				GFP_KERNEL);
1252 		if (!edev->nh) {
1253 			ret = -ENOMEM;
1254 			goto err_alloc_nh;
1255 		}
1256 	}
1257 
1258 	for (index = 0; index < edev->max_supported; index++)
1259 		RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
1260 
1261 	RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
1262 
1263 	dev_set_drvdata(&edev->dev, edev);
1264 	edev->state = 0;
1265 
1266 	ret = device_register(&edev->dev);
1267 	if (ret) {
1268 		put_device(&edev->dev);
1269 		goto err_dev;
1270 	}
1271 
1272 	mutex_lock(&extcon_dev_list_lock);
1273 	list_add(&edev->entry, &extcon_dev_list);
1274 	mutex_unlock(&extcon_dev_list_lock);
1275 
1276 	return 0;
1277 
1278 err_dev:
1279 	if (edev->max_supported)
1280 		kfree(edev->nh);
1281 err_alloc_nh:
1282 	if (edev->max_supported)
1283 		kfree(edev->extcon_dev_type.groups);
1284 err_alloc_groups:
1285 	if (edev->max_supported && edev->mutually_exclusive) {
1286 		for (index = 0; edev->mutually_exclusive[index]; index++)
1287 			kfree(edev->d_attrs_muex[index].attr.name);
1288 		kfree(edev->d_attrs_muex);
1289 		kfree(edev->attrs_muex);
1290 	}
1291 err_muex:
1292 	for (index = 0; index < edev->max_supported; index++)
1293 		kfree(edev->cables[index].attr_g.name);
1294 err_alloc_cables:
1295 	if (edev->max_supported)
1296 		kfree(edev->cables);
1297 err_sysfs_alloc:
1298 	return ret;
1299 }
1300 EXPORT_SYMBOL_GPL(extcon_dev_register);
1301 
1302 /**
1303  * extcon_dev_unregister() - Unregister the extcon device.
1304  * @edev:	the extcon device to be unregistered.
1305  *
1306  * Note that this does not call kfree(edev) because edev was not allocated
1307  * by this class.
1308  */
extcon_dev_unregister(struct extcon_dev * edev)1309 void extcon_dev_unregister(struct extcon_dev *edev)
1310 {
1311 	int index;
1312 
1313 	if (!edev)
1314 		return;
1315 
1316 	mutex_lock(&extcon_dev_list_lock);
1317 	list_del(&edev->entry);
1318 	mutex_unlock(&extcon_dev_list_lock);
1319 
1320 	if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1321 		dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1322 				dev_name(&edev->dev));
1323 		return;
1324 	}
1325 
1326 	device_unregister(&edev->dev);
1327 
1328 	if (edev->mutually_exclusive && edev->max_supported) {
1329 		for (index = 0; edev->mutually_exclusive[index];
1330 				index++)
1331 			kfree(edev->d_attrs_muex[index].attr.name);
1332 		kfree(edev->d_attrs_muex);
1333 		kfree(edev->attrs_muex);
1334 	}
1335 
1336 	for (index = 0; index < edev->max_supported; index++)
1337 		kfree(edev->cables[index].attr_g.name);
1338 
1339 	if (edev->max_supported) {
1340 		kfree(edev->extcon_dev_type.groups);
1341 		kfree(edev->cables);
1342 		kfree(edev->nh);
1343 	}
1344 
1345 	put_device(&edev->dev);
1346 }
1347 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1348 
1349 #ifdef CONFIG_OF
1350 
1351 /*
1352  * extcon_find_edev_by_node - Find the extcon device from devicetree.
1353  * @node	: OF node identifying edev
1354  *
1355  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1356  */
extcon_find_edev_by_node(struct device_node * node)1357 struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1358 {
1359 	struct extcon_dev *edev;
1360 
1361 	mutex_lock(&extcon_dev_list_lock);
1362 	list_for_each_entry(edev, &extcon_dev_list, entry)
1363 		if (edev->dev.parent && edev->dev.parent->of_node == node)
1364 			goto out;
1365 	edev = ERR_PTR(-EPROBE_DEFER);
1366 out:
1367 	mutex_unlock(&extcon_dev_list_lock);
1368 
1369 	return edev;
1370 }
1371 
1372 /*
1373  * extcon_get_edev_by_phandle - Get the extcon device from devicetree.
1374  * @dev		: the instance to the given device
1375  * @index	: the index into list of extcon_dev
1376  *
1377  * Return the pointer of extcon device if success or ERR_PTR(err) if fail.
1378  */
extcon_get_edev_by_phandle(struct device * dev,int index)1379 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1380 {
1381 	struct device_node *node;
1382 	struct extcon_dev *edev;
1383 
1384 	if (!dev)
1385 		return ERR_PTR(-EINVAL);
1386 
1387 	if (!dev->of_node) {
1388 		dev_dbg(dev, "device does not have a device node entry\n");
1389 		return ERR_PTR(-EINVAL);
1390 	}
1391 
1392 	node = of_parse_phandle(dev->of_node, "extcon", index);
1393 	if (!node) {
1394 		dev_dbg(dev, "failed to get phandle in %pOF node\n",
1395 			dev->of_node);
1396 		return ERR_PTR(-ENODEV);
1397 	}
1398 
1399 	edev = extcon_find_edev_by_node(node);
1400 	of_node_put(node);
1401 
1402 	return edev;
1403 }
1404 
1405 #else
1406 
extcon_find_edev_by_node(struct device_node * node)1407 struct extcon_dev *extcon_find_edev_by_node(struct device_node *node)
1408 {
1409 	return ERR_PTR(-ENOSYS);
1410 }
1411 
extcon_get_edev_by_phandle(struct device * dev,int index)1412 struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1413 {
1414 	return ERR_PTR(-ENOSYS);
1415 }
1416 
1417 #endif /* CONFIG_OF */
1418 
1419 EXPORT_SYMBOL_GPL(extcon_find_edev_by_node);
1420 EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1421 
1422 /**
1423  * extcon_get_edev_name() - Get the name of the extcon device.
1424  * @edev:	the extcon device
1425  */
extcon_get_edev_name(struct extcon_dev * edev)1426 const char *extcon_get_edev_name(struct extcon_dev *edev)
1427 {
1428 	return !edev ? NULL : edev->name;
1429 }
1430 EXPORT_SYMBOL_GPL(extcon_get_edev_name);
1431 
extcon_class_init(void)1432 static int __init extcon_class_init(void)
1433 {
1434 	return create_extcon_class();
1435 }
1436 module_init(extcon_class_init);
1437 
extcon_class_exit(void)1438 static void __exit extcon_class_exit(void)
1439 {
1440 	class_destroy(extcon_class);
1441 }
1442 module_exit(extcon_class_exit);
1443 
1444 MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
1445 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1446 MODULE_DESCRIPTION("External Connector (extcon) framework");
1447 MODULE_LICENSE("GPL v2");
1448