blob: 99c0b9fe08fe0fb1019efb3e102a30c38eaf51bf [file] [log] [blame]
MyungJoo Hamde55d872012-04-20 14:16:22 +09001/*
Chanwoo Choib9ec23c2015-04-24 14:48:52 +09002 * drivers/extcon/extcon.c - External Connector (extcon) framework.
MyungJoo Hamde55d872012-04-20 14:16:22 +09003 *
4 * External connector (extcon) class driver
5 *
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09006 * Copyright (C) 2015 Samsung Electronics
7 * Author: Chanwoo Choi <cw00.choi@samsung.com>
8 *
MyungJoo Hamde55d872012-04-20 14:16:22 +09009 * 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.
Chanwoo Choib9ec23c2015-04-24 14:48:52 +090025 */
MyungJoo Hamde55d872012-04-20 14:16:22 +090026
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>
Tomasz Figaf841afb2014-10-16 15:11:44 +020034#include <linux/of.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090035#include <linux/slab.h>
Mark Brown9baf3222012-08-16 20:03:21 +010036#include <linux/sysfs.h>
MyungJoo Hamde55d872012-04-20 14:16:22 +090037
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +090038#define SUPPORTED_CABLE_MAX 32
39#define CABLE_NAME_MAX 30
40
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090041struct __extcon_info {
42 unsigned int type;
43 unsigned int id;
44 const char *name;
45
46} extcon_info[] = {
47 [EXTCON_NONE] = {
48 .type = EXTCON_TYPE_MISC,
49 .id = EXTCON_NONE,
50 .name = "NONE",
51 },
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +090052
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090053 /* USB external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090054 [EXTCON_USB] = {
55 .type = EXTCON_TYPE_USB,
56 .id = EXTCON_USB,
57 .name = "USB",
58 },
59 [EXTCON_USB_HOST] = {
60 .type = EXTCON_TYPE_USB,
61 .id = EXTCON_USB_HOST,
62 .name = "USB_HOST",
63 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +090064
Chanwoo Choi11eecf92015-10-03 14:15:13 +090065 /* Charging external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +090066 [EXTCON_CHG_USB_SDP] = {
67 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
68 .id = EXTCON_CHG_USB_SDP,
69 .name = "SDP",
70 },
71 [EXTCON_CHG_USB_DCP] = {
72 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
73 .id = EXTCON_CHG_USB_DCP,
74 .name = "DCP",
75 },
76 [EXTCON_CHG_USB_CDP] = {
77 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
78 .id = EXTCON_CHG_USB_CDP,
79 .name = "CDP",
80 },
81 [EXTCON_CHG_USB_ACA] = {
82 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
83 .id = EXTCON_CHG_USB_ACA,
84 .name = "ACA",
85 },
86 [EXTCON_CHG_USB_FAST] = {
87 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
88 .id = EXTCON_CHG_USB_FAST,
89 .name = "FAST-CHARGER",
90 },
91 [EXTCON_CHG_USB_SLOW] = {
92 .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
93 .id = EXTCON_CHG_USB_SLOW,
94 .name = "SLOW-CHARGER",
95 },
Chanwoo Choi7fe95fb2016-08-05 18:15:46 +090096 [EXTCON_CHG_WPT] = {
97 .type = EXTCON_TYPE_CHG,
98 .id = EXTCON_CHG_WPT,
99 .name = "WPT",
100 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900101
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900102 /* Jack external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900103 [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 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900143
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900144 /* Display external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900145 [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 },
Chris Zhong21641882016-07-22 01:13:02 +0900165 [EXTCON_DISP_DP] = {
166 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
167 .id = EXTCON_DISP_DP,
168 .name = "DP",
169 },
Chanwoo Choi9c0595d2016-08-05 17:49:23 +0900170 [EXTCON_DISP_HMD] = {
171 .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
172 .id = EXTCON_DISP_HMD,
173 .name = "HMD",
174 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900175
Chanwoo Choi11eecf92015-10-03 14:15:13 +0900176 /* Miscellaneous external connector */
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900177 [EXTCON_DOCK] = {
178 .type = EXTCON_TYPE_MISC,
179 .id = EXTCON_DOCK,
180 .name = "DOCK",
181 },
182 [EXTCON_JIG] = {
183 .type = EXTCON_TYPE_MISC,
184 .id = EXTCON_JIG,
185 .name = "JIG",
186 },
187 [EXTCON_MECHANICAL] = {
188 .type = EXTCON_TYPE_MISC,
189 .id = EXTCON_MECHANICAL,
190 .name = "MECHANICAL",
191 },
Chanwoo Choi8e9bc362015-05-19 19:58:49 +0900192
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900193 { /* sentinel */ }
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900194};
195
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900196/**
197 * struct extcon_cable - An internal data for each cable of extcon device.
198 * @edev: The extcon device
199 * @cable_index: Index of this cable in the edev
200 * @attr_g: Attribute group for the cable
201 * @attr_name: "name" sysfs entry
202 * @attr_state: "state" sysfs entry
203 * @attrs: Array pointing to attr_name and attr_state for attr_g
204 */
205struct extcon_cable {
206 struct extcon_dev *edev;
207 int cable_index;
208
209 struct attribute_group attr_g;
210 struct device_attribute attr_name;
211 struct device_attribute attr_state;
212
213 struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900214
215 union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
216 union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
217 union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
218 union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900219
220 unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
221 unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
222 unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
223 unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
Chanwoo Choi20f7b532016-06-27 19:17:06 +0900224};
225
Mark Brownbe3a07f2012-06-05 16:14:38 +0100226static struct class *extcon_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900227#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900228static struct class_compat *switch_class;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +0900229#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +0900230
Donggeun Kim74c5d092012-04-20 14:16:24 +0900231static LIST_HEAD(extcon_dev_list);
232static DEFINE_MUTEX(extcon_dev_list_lock);
233
MyungJoo Hambde68e62012-04-20 14:16:26 +0900234/**
235 * check_mutually_exclusive - Check if new_state violates mutually_exclusive
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900236 * condition.
MyungJoo Hambde68e62012-04-20 14:16:26 +0900237 * @edev: the extcon device
238 * @new_state: new cable attach status for @edev
239 *
240 * Returns 0 if nothing violates. Returns the index + 1 for the first
241 * violated condition.
242 */
243static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
244{
245 int i = 0;
246
247 if (!edev->mutually_exclusive)
248 return 0;
249
250 for (i = 0; edev->mutually_exclusive[i]; i++) {
anish kumar28c0ada2012-08-30 00:35:10 +0530251 int weight;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900252 u32 correspondants = new_state & edev->mutually_exclusive[i];
MyungJoo Hambde68e62012-04-20 14:16:26 +0900253
anish kumar28c0ada2012-08-30 00:35:10 +0530254 /* calculate the total number of bits set */
255 weight = hweight32(correspondants);
256 if (weight > 1)
257 return i + 1;
MyungJoo Hambde68e62012-04-20 14:16:26 +0900258 }
259
260 return 0;
261}
262
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900263static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900264{
265 int i;
266
267 /* Find the the index of extcon cable in edev->supported_cable */
268 for (i = 0; i < edev->max_supported; i++) {
269 if (edev->supported_cable[i] == id)
270 return i;
271 }
272
273 return -EINVAL;
274}
275
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900276static int get_extcon_type(unsigned int prop)
277{
278 switch (prop) {
279 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
280 return EXTCON_TYPE_USB;
281 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
282 return EXTCON_TYPE_CHG;
283 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
284 return EXTCON_TYPE_JACK;
285 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
286 return EXTCON_TYPE_DISP;
287 default:
288 return -EINVAL;
289 }
290}
291
292static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
293{
294 return !!(edev->state & BIT(index));
295}
296
Chanwoo Choiab11af042016-07-22 13:16:34 +0900297static bool is_extcon_changed(struct extcon_dev *edev, int index,
298 bool new_state)
Chanwoo Choi046050f2015-05-19 20:01:12 +0900299{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900300 int state = !!(edev->state & BIT(index));
301 return (state != new_state);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900302}
303
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900304static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
305{
306 int type;
307
308 /* Check whether the property is supported or not. */
309 type = get_extcon_type(prop);
310 if (type < 0)
311 return false;
312
313 /* Check whether a specific extcon id supports the property or not. */
314 return !!(extcon_info[id].type & type);
315}
316
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900317static int is_extcon_property_capability(struct extcon_dev *edev,
318 unsigned int id, int index,unsigned int prop)
319{
320 struct extcon_cable *cable;
321 int type, ret;
322
323 /* Check whether the property is supported or not. */
324 type = get_extcon_type(prop);
325 if (type < 0)
326 return type;
327
328 cable = &edev->cables[index];
329
330 switch (type) {
331 case EXTCON_TYPE_USB:
332 ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
333 break;
334 case EXTCON_TYPE_CHG:
335 ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
336 break;
337 case EXTCON_TYPE_JACK:
338 ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
339 break;
340 case EXTCON_TYPE_DISP:
341 ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
342 break;
343 default:
344 ret = -EINVAL;
345 }
346
347 return ret;
348}
349
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900350static void init_property(struct extcon_dev *edev, unsigned int id, int index)
351{
352 unsigned int type = extcon_info[id].type;
353 struct extcon_cable *cable = &edev->cables[index];
354
355 if (EXTCON_TYPE_USB & type)
356 memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
357 if (EXTCON_TYPE_CHG & type)
358 memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
359 if (EXTCON_TYPE_JACK & type)
360 memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
361 if (EXTCON_TYPE_DISP & type)
362 memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
363}
364
MyungJoo Hamde55d872012-04-20 14:16:22 +0900365static ssize_t state_show(struct device *dev, struct device_attribute *attr,
366 char *buf)
367{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900368 int i, count = 0;
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900369 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900370
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900371 if (edev->max_supported == 0)
372 return sprintf(buf, "%u\n", edev->state);
373
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900374 for (i = 0; i < edev->max_supported; i++) {
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900375 count += sprintf(buf + count, "%s=%d\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900376 extcon_info[edev->supported_cable[i]].name,
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900377 !!(edev->state & (1 << i)));
378 }
379
380 return count;
381}
Chanwoo Choi5d5321e2016-07-18 15:39:28 +0900382static DEVICE_ATTR_RO(state);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900383
384static ssize_t name_show(struct device *dev, struct device_attribute *attr,
385 char *buf)
386{
Jingoo Hancb8bb3a2013-09-09 14:33:32 +0900387 struct extcon_dev *edev = dev_get_drvdata(dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900388
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +0900389 return sprintf(buf, "%s\n", edev->name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900390}
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700391static DEVICE_ATTR_RO(name);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900392
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900393static ssize_t cable_name_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
395{
396 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
397 attr_name);
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900398 int i = cable->cable_index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900399
400 return sprintf(buf, "%s\n",
Chanwoo Choi55e4e2f2016-07-11 16:34:52 +0900401 extcon_info[cable->edev->supported_cable[i]].name);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900402}
403
404static ssize_t cable_state_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406{
407 struct extcon_cable *cable = container_of(attr, struct extcon_cable,
408 attr_state);
409
Roger Quadrosbe052cc2015-07-07 16:06:15 +0300410 int i = cable->cable_index;
411
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900412 return sprintf(buf, "%d\n",
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900413 extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900414}
415
MyungJoo Hamde55d872012-04-20 14:16:22 +0900416/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900417 * extcon_sync() - Synchronize the states for both the attached/detached
418 * @edev: the extcon device that has the cable.
MyungJoo Hamde55d872012-04-20 14:16:22 +0900419 *
Chanwoo Choiab11af042016-07-22 13:16:34 +0900420 * This function send a notification to synchronize the all states of a
421 * specific external connector
MyungJoo Hamde55d872012-04-20 14:16:22 +0900422 */
Chanwoo Choiab11af042016-07-22 13:16:34 +0900423int extcon_sync(struct extcon_dev *edev, unsigned int id)
MyungJoo Hamde55d872012-04-20 14:16:22 +0900424{
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;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900431 int index;
Chanwoo Choiab11af042016-07-22 13:16:34 +0900432 int state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900433 unsigned long flags;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900434
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900435 if (!edev)
436 return -EINVAL;
437
Chanwoo Choiab11af042016-07-22 13:16:34 +0900438 index = find_cable_index_by_id(edev, id);
439 if (index < 0)
440 return index;
441
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900442 spin_lock_irqsave(&edev->lock, flags);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900443
Chanwoo Choiab11af042016-07-22 13:16:34 +0900444 state = !!(edev->state & BIT(index));
445 raw_notifier_call_chain(&edev->nh[index], state, edev);
Roger Quadrosf7a89812015-07-06 17:46:58 +0300446
Chanwoo Choiab11af042016-07-22 13:16:34 +0900447 /* This could be in interrupt handler */
448 prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
449 if (!prop_buf) {
450 /* Unlock early before uevent */
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900451 spin_unlock_irqrestore(&edev->lock, flags);
Chanwoo Choiab11af042016-07-22 13:16:34 +0900452
453 dev_err(&edev->dev, "out of memory in extcon_set_state\n");
454 kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
455
Pan Bian86820a102016-12-03 16:56:49 +0800456 return -ENOMEM;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900457 }
MyungJoo Hambde68e62012-04-20 14:16:26 +0900458
Chanwoo Choiab11af042016-07-22 13:16:34 +0900459 length = name_show(&edev->dev, NULL, prop_buf);
460 if (length > 0) {
461 if (prop_buf[length - 1] == '\n')
462 prop_buf[length - 1] = 0;
463 snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
464 envp[env_offset++] = name_buf;
465 }
466
467 length = state_show(&edev->dev, NULL, prop_buf);
468 if (length > 0) {
469 if (prop_buf[length - 1] == '\n')
470 prop_buf[length - 1] = 0;
471 snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
472 envp[env_offset++] = state_buf;
473 }
474 envp[env_offset] = NULL;
475
476 /* Unlock early before uevent */
477 spin_unlock_irqrestore(&edev->lock, flags);
478 kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
479 free_page((unsigned long)prop_buf);
480
MyungJoo Hambde68e62012-04-20 14:16:26 +0900481 return 0;
MyungJoo Hamde55d872012-04-20 14:16:22 +0900482}
Chanwoo Choiab11af042016-07-22 13:16:34 +0900483EXPORT_SYMBOL_GPL(extcon_sync);
MyungJoo Hamde55d872012-04-20 14:16:22 +0900484
Liangliang Lu7e9fffa2018-06-26 12:45:14 +0800485int extcon_blocking_sync(struct extcon_dev *edev, unsigned int id, u8 val)
Mayank Rana1e466ab2017-07-06 16:42:06 -0700486{
487 int index;
488
489 if (!edev)
490 return -EINVAL;
491
492 index = find_cable_index_by_id(edev, id);
493 if (index < 0)
494 return index;
495
496 return blocking_notifier_call_chain(&edev->bnh[index], val, edev);
497}
498EXPORT_SYMBOL(extcon_blocking_sync);
499
Donggeun Kim74c5d092012-04-20 14:16:24 +0900500/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900501 * extcon_get_state() - Get the state of a external connector.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900502 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900503 * @id: the unique id of each external connector in extcon enumeration.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900504 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900505int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900506{
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900507 int index, state;
508 unsigned long flags;
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900509
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900510 if (!edev)
511 return -EINVAL;
512
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900513 index = find_cable_index_by_id(edev, id);
514 if (index < 0)
515 return index;
516
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900517 spin_lock_irqsave(&edev->lock, flags);
518 state = is_extcon_attached(edev, index);
519 spin_unlock_irqrestore(&edev->lock, flags);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900520
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900521 return state;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900522}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900523EXPORT_SYMBOL_GPL(extcon_get_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900524
525/**
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900526 * extcon_set_state() - Set the state of a external connector.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900527 * without a notification.
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900528 * @edev: the extcon device that has the cable.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900529 * @id: the unique id of each external connector
530 * in extcon enumeration.
531 * @state: the new cable status. The default semantics is
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900532 * true: attached / false: detached.
Chanwoo Choiab11af042016-07-22 13:16:34 +0900533 *
534 * This function only set the state of a external connector without
535 * a notification. To synchronize the data of a external connector,
536 * use extcon_set_state_sync() and extcon_sync().
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900537 */
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900538int extcon_set_state(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900539 bool cable_state)
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900540{
Chanwoo Choiab11af042016-07-22 13:16:34 +0900541 unsigned long flags;
542 int index, ret = 0;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900543
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900544 if (!edev)
545 return -EINVAL;
546
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +0900547 index = find_cable_index_by_id(edev, id);
548 if (index < 0)
549 return index;
550
Chanwoo Choiab11af042016-07-22 13:16:34 +0900551 spin_lock_irqsave(&edev->lock, flags);
552
553 /* Check whether the external connector's state is changed. */
554 if (!is_extcon_changed(edev, index, cable_state))
555 goto out;
556
557 if (check_mutually_exclusive(edev,
558 (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
559 ret = -EPERM;
560 goto out;
561 }
562
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900563 /*
564 * Initialize the value of extcon property before setting
565 * the detached state for an external connector.
566 */
567 if (!cable_state)
568 init_property(edev, id, index);
569
Chanwoo Choiab11af042016-07-22 13:16:34 +0900570 /* Update the state for a external connector. */
571 if (cable_state)
572 edev->state |= BIT(index);
573 else
574 edev->state &= ~(BIT(index));
575out:
576 spin_unlock_irqrestore(&edev->lock, flags);
577
578 return ret;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900579}
Chanwoo Choi575c2b862016-07-22 13:03:17 +0900580EXPORT_SYMBOL_GPL(extcon_set_state);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900581
582/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900583 * extcon_set_state_sync() - Set the state of a external connector
584 * with a notification.
585 * @edev: the extcon device that has the cable.
586 * @id: the unique id of each external connector
587 * in extcon enumeration.
588 * @state: the new cable status. The default semantics is
589 * true: attached / false: detached.
590 *
591 * This function set the state of external connector and synchronize the data
592 * by usning a notification.
593 */
594int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
595 bool cable_state)
596{
597 int ret, index;
598 unsigned long flags;
599
600 index = find_cable_index_by_id(edev, id);
601 if (index < 0)
602 return index;
603
604 /* Check whether the external connector's state is changed. */
605 spin_lock_irqsave(&edev->lock, flags);
606 ret = is_extcon_changed(edev, index, cable_state);
607 spin_unlock_irqrestore(&edev->lock, flags);
608 if (!ret)
609 return 0;
610
611 ret = extcon_set_state(edev, id, cable_state);
612 if (ret < 0)
613 return ret;
614
615 return extcon_sync(edev, id);
616}
617EXPORT_SYMBOL_GPL(extcon_set_state_sync);
618
619/**
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900620 * extcon_get_property() - Get the property value of a specific cable.
621 * @edev: the extcon device that has the cable.
622 * @id: the unique id of each external connector
623 * in extcon enumeration.
624 * @prop: the property id among enum extcon_property.
625 * @prop_val: the pointer which store the value of property.
626 *
627 * When getting the property value of external connector, the external connector
628 * should be attached. If detached state, function just return 0 without
629 * property value. Also, the each property should be included in the list of
630 * supported properties according to the type of external connectors.
631 *
632 * Returns 0 if success or error number if fail
633 */
634int extcon_get_property(struct extcon_dev *edev, unsigned int id,
635 unsigned int prop,
636 union extcon_property_value *prop_val)
637{
638 struct extcon_cable *cable;
639 unsigned long flags;
640 int index, ret = 0;
641
642 *prop_val = (union extcon_property_value)(0);
643
644 if (!edev)
645 return -EINVAL;
646
647 /* Check whether the property is supported or not */
648 if (!is_extcon_property_supported(id, prop))
649 return -EINVAL;
650
651 /* Find the cable index of external connector by using id */
652 index = find_cable_index_by_id(edev, id);
653 if (index < 0)
654 return index;
655
656 spin_lock_irqsave(&edev->lock, flags);
657
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900658 /* Check whether the property is available or not. */
659 if (!is_extcon_property_capability(edev, id, index, prop)) {
660 spin_unlock_irqrestore(&edev->lock, flags);
661 return -EPERM;
662 }
663
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900664 /*
665 * Check whether the external connector is attached.
666 * If external connector is detached, the user can not
667 * get the property value.
668 */
669 if (!is_extcon_attached(edev, index)) {
670 spin_unlock_irqrestore(&edev->lock, flags);
671 return 0;
672 }
673
674 cable = &edev->cables[index];
675
676 /* Get the property value according to extcon type */
677 switch (prop) {
678 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
679 *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
680 break;
681 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
682 *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
683 break;
684 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
685 *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
686 break;
687 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
688 *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
689 break;
690 default:
691 ret = -EINVAL;
692 break;
693 }
694
695 spin_unlock_irqrestore(&edev->lock, flags);
696
697 return ret;
698}
699EXPORT_SYMBOL_GPL(extcon_get_property);
700
701/**
702 * extcon_set_property() - Set the property value of a specific cable.
703 * @edev: the extcon device that has the cable.
704 * @id: the unique id of each external connector
705 * in extcon enumeration.
706 * @prop: the property id among enum extcon_property.
707 * @prop_val: the pointer including the new value of property.
708 *
709 * The each property should be included in the list of supported properties
710 * according to the type of external connectors.
711 *
712 * Returns 0 if success or error number if fail
713 */
714int extcon_set_property(struct extcon_dev *edev, unsigned int id,
715 unsigned int prop,
716 union extcon_property_value prop_val)
717{
718 struct extcon_cable *cable;
719 unsigned long flags;
720 int index, ret = 0;
721
722 if (!edev)
723 return -EINVAL;
724
725 /* Check whether the property is supported or not */
726 if (!is_extcon_property_supported(id, prop))
727 return -EINVAL;
728
729 /* Find the cable index of external connector by using id */
730 index = find_cable_index_by_id(edev, id);
731 if (index < 0)
732 return index;
733
734 spin_lock_irqsave(&edev->lock, flags);
735
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900736 /* Check whether the property is available or not. */
737 if (!is_extcon_property_capability(edev, id, index, prop)) {
738 spin_unlock_irqrestore(&edev->lock, flags);
739 return -EPERM;
740 }
741
Chanwoo Choi792e7e9e2016-07-11 19:30:43 +0900742 cable = &edev->cables[index];
743
744 /* Set the property value according to extcon type */
745 switch (prop) {
746 case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
747 cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
748 break;
749 case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
750 cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
751 break;
752 case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
753 cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
754 break;
755 case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
756 cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
757 break;
758 default:
759 ret = -EINVAL;
760 break;
761 }
762
763 spin_unlock_irqrestore(&edev->lock, flags);
764
765 return ret;
766}
767EXPORT_SYMBOL_GPL(extcon_set_property);
768
769/**
Chanwoo Choiab11af042016-07-22 13:16:34 +0900770 * extcon_set_property_sync() - Set the property value of a specific cable
771 with a notification.
772 * @prop_val: the pointer including the new value of property.
773 *
774 * When setting the property value of external connector, the external connector
775 * should be attached. The each property should be included in the list of
776 * supported properties according to the type of external connectors.
777 *
778 * Returns 0 if success or error number if fail
779 */
780int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
781 unsigned int prop,
782 union extcon_property_value prop_val)
783{
784 int ret;
785
786 ret = extcon_set_property(edev, id, prop, prop_val);
787 if (ret < 0)
788 return ret;
789
790 return extcon_sync(edev, id);
791}
792EXPORT_SYMBOL_GPL(extcon_set_property_sync);
793
794/**
Chanwoo Choi7f2a0a12016-07-25 21:15:19 +0900795 * extcon_get_property_capability() - Get the capability of property
796 * of an external connector.
797 * @edev: the extcon device that has the cable.
798 * @id: the unique id of each external connector
799 * in extcon enumeration.
800 * @prop: the property id among enum extcon_property.
801 *
802 * Returns 1 if the property is available or 0 if not available.
803 */
804int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
805 unsigned int prop)
806{
807 int index;
808
809 if (!edev)
810 return -EINVAL;
811
812 /* Check whether the property is supported or not */
813 if (!is_extcon_property_supported(id, prop))
814 return -EINVAL;
815
816 /* Find the cable index of external connector by using id */
817 index = find_cable_index_by_id(edev, id);
818 if (index < 0)
819 return index;
820
821 return is_extcon_property_capability(edev, id, index, prop);
822}
823EXPORT_SYMBOL_GPL(extcon_get_property_capability);
824
825/**
826 * extcon_set_property_capability() - Set the capability of a property
827 * of an external connector.
828 * @edev: the extcon device that has the cable.
829 * @id: the unique id of each external connector
830 * in extcon enumeration.
831 * @prop: the property id among enum extcon_property.
832 *
833 * This function set the capability of a property for an external connector
834 * to mark the bit in capability bitmap which mean the available state of
835 * a property.
836 *
837 * Returns 0 if success or error number if fail
838 */
839int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
840 unsigned int prop)
841{
842 struct extcon_cable *cable;
843 int index, type, ret = 0;
844
845 if (!edev)
846 return -EINVAL;
847
848 /* Check whether the property is supported or not. */
849 if (!is_extcon_property_supported(id, prop))
850 return -EINVAL;
851
852 /* Find the cable index of external connector by using id. */
853 index = find_cable_index_by_id(edev, id);
854 if (index < 0)
855 return index;
856
857 type = get_extcon_type(prop);
858 if (type < 0)
859 return type;
860
861 cable = &edev->cables[index];
862
863 switch (type) {
864 case EXTCON_TYPE_USB:
865 __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
866 break;
867 case EXTCON_TYPE_CHG:
868 __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
869 break;
870 case EXTCON_TYPE_JACK:
871 __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
872 break;
873 case EXTCON_TYPE_DISP:
874 __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
875 break;
876 default:
877 ret = -EINVAL;
878 }
879
880 return ret;
881}
882EXPORT_SYMBOL_GPL(extcon_set_property_capability);
883
884/**
Donggeun Kim74c5d092012-04-20 14:16:24 +0900885 * extcon_get_extcon_dev() - Get the extcon device instance from the name
886 * @extcon_name: The extcon name provided with extcon_dev_register()
887 */
888struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
889{
890 struct extcon_dev *sd;
891
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900892 if (!extcon_name)
893 return ERR_PTR(-EINVAL);
894
Donggeun Kim74c5d092012-04-20 14:16:24 +0900895 mutex_lock(&extcon_dev_list_lock);
896 list_for_each_entry(sd, &extcon_dev_list, entry) {
897 if (!strcmp(sd->name, extcon_name))
898 goto out;
899 }
900 sd = NULL;
901out:
902 mutex_unlock(&extcon_dev_list_lock);
903 return sd;
904}
905EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
906
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900907/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900908 * extcon_register_notifier() - Register a notifiee to get notified by
Chanwoo Choia75e1c72013-08-31 13:16:49 +0900909 * any attach status changes from the extcon.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900910 * @edev: the extcon device that has the external connecotr.
911 * @id: the unique id of each external connector in extcon enumeration.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900912 * @nb: a notifier block to be registered.
MyungJoo Ham806d9dd2012-04-20 14:16:25 +0900913 *
914 * Note that the second parameter given to the callback of nb (val) is
915 * "old_state", not the current state. The current state can be retrieved
916 * by looking at the third pameter (edev pointer)'s state value.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900917 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900918int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900919 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900920{
Hans de Goede66bee352015-03-21 17:26:24 +0100921 unsigned long flags;
Maninder Singh2c8116a2016-08-01 14:51:30 +0530922 int ret, idx = -EINVAL;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900923
Chanwoo Choic4a77532016-12-19 21:02:33 +0900924 if (!edev || !nb)
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900925 return -EINVAL;
926
Chanwoo Choic4a77532016-12-19 21:02:33 +0900927 idx = find_cable_index_by_id(edev, id);
928 if (idx < 0)
929 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100930
Chanwoo Choic4a77532016-12-19 21:02:33 +0900931 spin_lock_irqsave(&edev->lock, flags);
932 ret = raw_notifier_chain_register(&edev->nh[idx], nb);
933 spin_unlock_irqrestore(&edev->lock, flags);
Hans de Goede66bee352015-03-21 17:26:24 +0100934
935 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900936}
937EXPORT_SYMBOL_GPL(extcon_register_notifier);
938
Mayank Rana1e466ab2017-07-06 16:42:06 -0700939int extcon_register_blocking_notifier(struct extcon_dev *edev, unsigned int id,
940 struct notifier_block *nb)
941{
942 int idx = -EINVAL;
943
944 if (!edev || !nb)
945 return -EINVAL;
946
947 idx = find_cable_index_by_id(edev, id);
948 if (idx < 0)
949 return idx;
950
951 return blocking_notifier_chain_register(&edev->bnh[idx], nb);
952}
953EXPORT_SYMBOL(extcon_register_blocking_notifier);
954
955int extcon_unregister_blocking_notifier(struct extcon_dev *edev,
956 unsigned int id, struct notifier_block *nb)
957{
958 int idx;
959
960 if (!edev || !nb)
961 return -EINVAL;
962
963 idx = find_cable_index_by_id(edev, id);
964 if (idx < 0)
965 return idx;
966
967 return blocking_notifier_chain_unregister(&edev->bnh[idx], nb);
968}
969EXPORT_SYMBOL(extcon_unregister_blocking_notifier);
970
Donggeun Kim74c5d092012-04-20 14:16:24 +0900971/**
Peter Meerwaldc338bb02012-08-23 09:11:54 +0900972 * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
Chanwoo Choi046050f2015-05-19 20:01:12 +0900973 * @edev: the extcon device that has the external connecotr.
974 * @id: the unique id of each external connector in extcon enumeration.
975 * @nb: a notifier block to be registered.
Donggeun Kim74c5d092012-04-20 14:16:24 +0900976 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +0900977int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
Chanwoo Choi046050f2015-05-19 20:01:12 +0900978 struct notifier_block *nb)
Donggeun Kim74c5d092012-04-20 14:16:24 +0900979{
Hans de Goede66bee352015-03-21 17:26:24 +0100980 unsigned long flags;
Chanwoo Choi046050f2015-05-19 20:01:12 +0900981 int ret, idx;
982
Chanwoo Choi7eae43a2015-06-21 23:48:36 +0900983 if (!edev || !nb)
984 return -EINVAL;
985
Chanwoo Choi046050f2015-05-19 20:01:12 +0900986 idx = find_cable_index_by_id(edev, id);
Stephen Boyda05f44c2016-06-23 19:34:30 +0900987 if (idx < 0)
988 return idx;
Hans de Goede66bee352015-03-21 17:26:24 +0100989
990 spin_lock_irqsave(&edev->lock, flags);
Chanwoo Choi046050f2015-05-19 20:01:12 +0900991 ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
Hans de Goede66bee352015-03-21 17:26:24 +0100992 spin_unlock_irqrestore(&edev->lock, flags);
993
994 return ret;
Donggeun Kim74c5d092012-04-20 14:16:24 +0900995}
996EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
997
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -0700998static struct attribute *extcon_attrs[] = {
999 &dev_attr_state.attr,
1000 &dev_attr_name.attr,
1001 NULL,
MyungJoo Hamde55d872012-04-20 14:16:22 +09001002};
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001003ATTRIBUTE_GROUPS(extcon);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001004
1005static int create_extcon_class(void)
1006{
1007 if (!extcon_class) {
1008 extcon_class = class_create(THIS_MODULE, "extcon");
1009 if (IS_ERR(extcon_class))
1010 return PTR_ERR(extcon_class);
Greg Kroah-Hartmanaf01da02013-07-24 15:05:10 -07001011 extcon_class->dev_groups = extcon_groups;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001012
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001013#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001014 switch_class = class_compat_register("switch");
1015 if (WARN(!switch_class, "cannot allocate"))
1016 return -ENOMEM;
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001017#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +09001018 }
1019
1020 return 0;
1021}
1022
MyungJoo Hamde55d872012-04-20 14:16:22 +09001023static void extcon_dev_release(struct device *dev)
1024{
MyungJoo Hamde55d872012-04-20 14:16:22 +09001025}
1026
MyungJoo Hambde68e62012-04-20 14:16:26 +09001027static const char *muex_name = "mutually_exclusive";
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001028static void dummy_sysfs_dev_release(struct device *dev)
1029{
1030}
1031
Chanwoo Choia9af6522014-04-24 19:46:49 +09001032/*
1033 * extcon_dev_allocate() - Allocate the memory of extcon device.
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001034 * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
Chanwoo Choia9af6522014-04-24 19:46:49 +09001035 * If supported_cable is NULL, cable name related APIs
1036 * are disabled.
1037 *
1038 * This function allocates the memory for extcon device without allocating
1039 * memory in each extcon provider driver and initialize default setting for
1040 * extcon device.
1041 *
1042 * Return the pointer of extcon device if success or ERR_PTR(err) if fail
1043 */
Chanwoo Choi73b6ecd2015-06-12 11:10:06 +09001044struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
Chanwoo Choia9af6522014-04-24 19:46:49 +09001045{
1046 struct extcon_dev *edev;
1047
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001048 if (!supported_cable)
1049 return ERR_PTR(-EINVAL);
1050
Chanwoo Choia9af6522014-04-24 19:46:49 +09001051 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
1052 if (!edev)
1053 return ERR_PTR(-ENOMEM);
1054
1055 edev->max_supported = 0;
1056 edev->supported_cable = supported_cable;
1057
1058 return edev;
1059}
1060
1061/*
1062 * extcon_dev_free() - Free the memory of extcon device.
1063 * @edev: the extcon device to free
1064 */
1065void extcon_dev_free(struct extcon_dev *edev)
1066{
1067 kfree(edev);
1068}
1069EXPORT_SYMBOL_GPL(extcon_dev_free);
1070
MyungJoo Hamde55d872012-04-20 14:16:22 +09001071/**
1072 * extcon_dev_register() - Register a new extcon device
1073 * @edev : the new extcon device (should be allocated before calling)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001074 *
1075 * Among the members of edev struct, please set the "user initializing data"
1076 * in any case and set the "optional callbacks" if required. However, please
1077 * do not set the values of "internal data", which are initialized by
1078 * this function.
1079 */
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001080int extcon_dev_register(struct extcon_dev *edev)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001081{
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001082 int ret, index = 0;
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001083 static atomic_t edev_no = ATOMIC_INIT(-1);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001084
1085 if (!extcon_class) {
1086 ret = create_extcon_class();
1087 if (ret < 0)
1088 return ret;
1089 }
1090
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001091 if (!edev || !edev->supported_cable)
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001092 return -EINVAL;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001093
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001094 for (; edev->supported_cable[index] != EXTCON_NONE; index++);
1095
1096 edev->max_supported = index;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001097 if (index > SUPPORTED_CABLE_MAX) {
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001098 dev_err(&edev->dev,
1099 "exceed the maximum number of supported cables\n");
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001100 return -EINVAL;
1101 }
1102
Chanwoo Choidae61652013-09-27 09:19:40 +09001103 edev->dev.class = extcon_class;
1104 edev->dev.release = extcon_dev_release;
MyungJoo Hamde55d872012-04-20 14:16:22 +09001105
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001106 edev->name = dev_name(edev->dev.parent);
Chanwoo Choi42d7d752013-09-27 09:20:26 +09001107 if (IS_ERR_OR_NULL(edev->name)) {
1108 dev_err(&edev->dev,
1109 "extcon device name is null\n");
1110 return -EINVAL;
1111 }
Chanwoo Choi71c3ffa2015-04-15 15:02:01 +09001112 dev_set_name(&edev->dev, "extcon%lu",
1113 (unsigned long)atomic_inc_return(&edev_no));
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001114
1115 if (edev->max_supported) {
1116 char buf[10];
1117 char *str;
1118 struct extcon_cable *cable;
1119
1120 edev->cables = kzalloc(sizeof(struct extcon_cable) *
1121 edev->max_supported, GFP_KERNEL);
1122 if (!edev->cables) {
1123 ret = -ENOMEM;
1124 goto err_sysfs_alloc;
1125 }
1126 for (index = 0; index < edev->max_supported; index++) {
1127 cable = &edev->cables[index];
1128
1129 snprintf(buf, 10, "cable.%d", index);
1130 str = kzalloc(sizeof(char) * (strlen(buf) + 1),
1131 GFP_KERNEL);
1132 if (!str) {
1133 for (index--; index >= 0; index--) {
1134 cable = &edev->cables[index];
1135 kfree(cable->attr_g.name);
1136 }
1137 ret = -ENOMEM;
1138
1139 goto err_alloc_cables;
1140 }
1141 strcpy(str, buf);
1142
1143 cable->edev = edev;
1144 cable->cable_index = index;
1145 cable->attrs[0] = &cable->attr_name.attr;
1146 cable->attrs[1] = &cable->attr_state.attr;
1147 cable->attrs[2] = NULL;
1148 cable->attr_g.name = str;
1149 cable->attr_g.attrs = cable->attrs;
1150
Mark Brown9baf3222012-08-16 20:03:21 +01001151 sysfs_attr_init(&cable->attr_name.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001152 cable->attr_name.attr.name = "name";
1153 cable->attr_name.attr.mode = 0444;
1154 cable->attr_name.show = cable_name_show;
1155
Mark Brown9baf3222012-08-16 20:03:21 +01001156 sysfs_attr_init(&cable->attr_state.attr);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001157 cable->attr_state.attr.name = "state";
Chanwoo Choiea9dd9d2013-05-22 19:31:59 +09001158 cable->attr_state.attr.mode = 0444;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001159 cable->attr_state.show = cable_state_show;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001160 }
1161 }
1162
MyungJoo Hambde68e62012-04-20 14:16:26 +09001163 if (edev->max_supported && edev->mutually_exclusive) {
1164 char buf[80];
1165 char *name;
1166
1167 /* Count the size of mutually_exclusive array */
1168 for (index = 0; edev->mutually_exclusive[index]; index++)
1169 ;
1170
1171 edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
1172 (index + 1), GFP_KERNEL);
1173 if (!edev->attrs_muex) {
1174 ret = -ENOMEM;
1175 goto err_muex;
1176 }
1177
1178 edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
1179 index, GFP_KERNEL);
1180 if (!edev->d_attrs_muex) {
1181 ret = -ENOMEM;
1182 kfree(edev->attrs_muex);
1183 goto err_muex;
1184 }
1185
1186 for (index = 0; edev->mutually_exclusive[index]; index++) {
1187 sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
1188 name = kzalloc(sizeof(char) * (strlen(buf) + 1),
1189 GFP_KERNEL);
1190 if (!name) {
1191 for (index--; index >= 0; index--) {
1192 kfree(edev->d_attrs_muex[index].attr.
1193 name);
1194 }
1195 kfree(edev->d_attrs_muex);
1196 kfree(edev->attrs_muex);
1197 ret = -ENOMEM;
1198 goto err_muex;
1199 }
1200 strcpy(name, buf);
Mark Brown9baf3222012-08-16 20:03:21 +01001201 sysfs_attr_init(&edev->d_attrs_muex[index].attr);
MyungJoo Hambde68e62012-04-20 14:16:26 +09001202 edev->d_attrs_muex[index].attr.name = name;
1203 edev->d_attrs_muex[index].attr.mode = 0000;
1204 edev->attrs_muex[index] = &edev->d_attrs_muex[index]
1205 .attr;
1206 }
1207 edev->attr_g_muex.name = muex_name;
1208 edev->attr_g_muex.attrs = edev->attrs_muex;
1209
1210 }
1211
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001212 if (edev->max_supported) {
1213 edev->extcon_dev_type.groups =
1214 kzalloc(sizeof(struct attribute_group *) *
MyungJoo Hambde68e62012-04-20 14:16:26 +09001215 (edev->max_supported + 2), GFP_KERNEL);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001216 if (!edev->extcon_dev_type.groups) {
1217 ret = -ENOMEM;
1218 goto err_alloc_groups;
1219 }
1220
Chanwoo Choidae61652013-09-27 09:19:40 +09001221 edev->extcon_dev_type.name = dev_name(&edev->dev);
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001222 edev->extcon_dev_type.release = dummy_sysfs_dev_release;
1223
1224 for (index = 0; index < edev->max_supported; index++)
1225 edev->extcon_dev_type.groups[index] =
1226 &edev->cables[index].attr_g;
MyungJoo Hambde68e62012-04-20 14:16:26 +09001227 if (edev->mutually_exclusive)
1228 edev->extcon_dev_type.groups[index] =
1229 &edev->attr_g_muex;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001230
Chanwoo Choidae61652013-09-27 09:19:40 +09001231 edev->dev.type = &edev->extcon_dev_type;
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001232 }
1233
Chanwoo Choidae61652013-09-27 09:19:40 +09001234 ret = device_register(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001235 if (ret) {
Chanwoo Choidae61652013-09-27 09:19:40 +09001236 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001237 goto err_dev;
1238 }
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001239#if defined(CONFIG_ANDROID)
MyungJoo Hamde55d872012-04-20 14:16:22 +09001240 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001241 ret = class_compat_create_link(switch_class, &edev->dev, NULL);
MyungJoo Ham449a2bf2012-04-23 20:19:57 +09001242#endif /* CONFIG_ANDROID */
MyungJoo Hamde55d872012-04-20 14:16:22 +09001243
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001244 spin_lock_init(&edev->lock);
1245
Chanwoo Choi046050f2015-05-19 20:01:12 +09001246 edev->nh = devm_kzalloc(&edev->dev,
1247 sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
1248 if (!edev->nh) {
1249 ret = -ENOMEM;
1250 goto err_dev;
1251 }
1252
Mayank Rana1e466ab2017-07-06 16:42:06 -07001253 edev->bnh = devm_kzalloc(&edev->dev,
1254 sizeof(*edev->bnh) * edev->max_supported, GFP_KERNEL);
1255 if (!edev->bnh) {
1256 ret = -ENOMEM;
1257 goto err_dev;
1258 }
1259
Chanwoo Choi046050f2015-05-19 20:01:12 +09001260 for (index = 0; index < edev->max_supported; index++)
1261 RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
Donggeun Kim74c5d092012-04-20 14:16:24 +09001262
Chanwoo Choidae61652013-09-27 09:19:40 +09001263 dev_set_drvdata(&edev->dev, edev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001264 edev->state = 0;
Donggeun Kim74c5d092012-04-20 14:16:24 +09001265
1266 mutex_lock(&extcon_dev_list_lock);
1267 list_add(&edev->entry, &extcon_dev_list);
1268 mutex_unlock(&extcon_dev_list_lock);
1269
MyungJoo Hamde55d872012-04-20 14:16:22 +09001270 return 0;
1271
1272err_dev:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001273 if (edev->max_supported)
1274 kfree(edev->extcon_dev_type.groups);
1275err_alloc_groups:
MyungJoo Hambde68e62012-04-20 14:16:26 +09001276 if (edev->max_supported && edev->mutually_exclusive) {
1277 for (index = 0; edev->mutually_exclusive[index]; index++)
1278 kfree(edev->d_attrs_muex[index].attr.name);
1279 kfree(edev->d_attrs_muex);
1280 kfree(edev->attrs_muex);
1281 }
1282err_muex:
MyungJoo Ham806d9dd2012-04-20 14:16:25 +09001283 for (index = 0; index < edev->max_supported; index++)
1284 kfree(edev->cables[index].attr_g.name);
1285err_alloc_cables:
1286 if (edev->max_supported)
1287 kfree(edev->cables);
1288err_sysfs_alloc:
MyungJoo Hamde55d872012-04-20 14:16:22 +09001289 return ret;
1290}
1291EXPORT_SYMBOL_GPL(extcon_dev_register);
1292
1293/**
1294 * extcon_dev_unregister() - Unregister the extcon device.
Peter Meerwaldc338bb02012-08-23 09:11:54 +09001295 * @edev: the extcon device instance to be unregistered.
MyungJoo Hamde55d872012-04-20 14:16:22 +09001296 *
1297 * Note that this does not call kfree(edev) because edev was not allocated
1298 * by this class.
1299 */
1300void extcon_dev_unregister(struct extcon_dev *edev)
1301{
anish kumar57e7cd32012-10-22 09:43:33 +09001302 int index;
1303
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001304 if (!edev)
1305 return;
1306
anish kumar57e7cd32012-10-22 09:43:33 +09001307 mutex_lock(&extcon_dev_list_lock);
1308 list_del(&edev->entry);
1309 mutex_unlock(&extcon_dev_list_lock);
1310
Chanwoo Choidae61652013-09-27 09:19:40 +09001311 if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
1312 dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
1313 dev_name(&edev->dev));
anish kumar57e7cd32012-10-22 09:43:33 +09001314 return;
1315 }
1316
Wang, Xiaoming7585ca02013-11-01 18:48:14 -04001317 device_unregister(&edev->dev);
1318
anish kumar57e7cd32012-10-22 09:43:33 +09001319 if (edev->mutually_exclusive && edev->max_supported) {
1320 for (index = 0; edev->mutually_exclusive[index];
1321 index++)
1322 kfree(edev->d_attrs_muex[index].attr.name);
1323 kfree(edev->d_attrs_muex);
1324 kfree(edev->attrs_muex);
1325 }
1326
1327 for (index = 0; index < edev->max_supported; index++)
1328 kfree(edev->cables[index].attr_g.name);
1329
1330 if (edev->max_supported) {
1331 kfree(edev->extcon_dev_type.groups);
1332 kfree(edev->cables);
1333 }
1334
1335#if defined(CONFIG_ANDROID)
1336 if (switch_class)
Chanwoo Choidae61652013-09-27 09:19:40 +09001337 class_compat_remove_link(switch_class, &edev->dev, NULL);
anish kumar57e7cd32012-10-22 09:43:33 +09001338#endif
Chanwoo Choidae61652013-09-27 09:19:40 +09001339 put_device(&edev->dev);
MyungJoo Hamde55d872012-04-20 14:16:22 +09001340}
1341EXPORT_SYMBOL_GPL(extcon_dev_unregister);
1342
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001343#ifdef CONFIG_OF
1344/*
1345 * extcon_get_edev_by_phandle - Get the extcon device from devicetree
1346 * @dev - instance to the given device
1347 * @index - index into list of extcon_dev
1348 *
1349 * return the instance of extcon device
1350 */
1351struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1352{
1353 struct device_node *node;
1354 struct extcon_dev *edev;
1355
Chanwoo Choi7eae43a2015-06-21 23:48:36 +09001356 if (!dev)
1357 return ERR_PTR(-EINVAL);
1358
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001359 if (!dev->of_node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001360 dev_dbg(dev, "device does not have a device node entry\n");
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001361 return ERR_PTR(-EINVAL);
1362 }
1363
1364 node = of_parse_phandle(dev->of_node, "extcon", index);
1365 if (!node) {
Stephen Boyde8752b72016-07-05 11:57:05 -07001366 dev_dbg(dev, "failed to get phandle in %s node\n",
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001367 dev->of_node->full_name);
1368 return ERR_PTR(-ENODEV);
1369 }
1370
Tomasz Figaf841afb2014-10-16 15:11:44 +02001371 mutex_lock(&extcon_dev_list_lock);
1372 list_for_each_entry(edev, &extcon_dev_list, entry) {
1373 if (edev->dev.parent && edev->dev.parent->of_node == node) {
1374 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001375 of_node_put(node);
Tomasz Figaf841afb2014-10-16 15:11:44 +02001376 return edev;
1377 }
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001378 }
Tomasz Figaf841afb2014-10-16 15:11:44 +02001379 mutex_unlock(&extcon_dev_list_lock);
Peter Chen5d5c4c12016-07-01 18:41:55 +09001380 of_node_put(node);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001381
Tomasz Figaf841afb2014-10-16 15:11:44 +02001382 return ERR_PTR(-EPROBE_DEFER);
Chanwoo Choi1ad94ff2014-03-18 19:55:46 +09001383}
1384#else
1385struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
1386{
1387 return ERR_PTR(-ENOSYS);
1388}
1389#endif /* CONFIG_OF */
1390EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
1391
Chanwoo Choi707d7552015-04-15 13:57:51 +09001392/**
1393 * extcon_get_edev_name() - Get the name of the extcon device.
1394 * @edev: the extcon device
1395 */
1396const char *extcon_get_edev_name(struct extcon_dev *edev)
1397{
1398 return !edev ? NULL : edev->name;
1399}
1400
MyungJoo Hamde55d872012-04-20 14:16:22 +09001401static int __init extcon_class_init(void)
1402{
1403 return create_extcon_class();
1404}
1405module_init(extcon_class_init);
1406
1407static void __exit extcon_class_exit(void)
1408{
Peter Huewe0dc77b62012-09-24 15:32:31 +09001409#if defined(CONFIG_ANDROID)
1410 class_compat_unregister(switch_class);
1411#endif
MyungJoo Hamde55d872012-04-20 14:16:22 +09001412 class_destroy(extcon_class);
1413}
1414module_exit(extcon_class_exit);
1415
Chanwoo Choi2a9de9c2015-04-24 19:16:05 +09001416MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
MyungJoo Hamde55d872012-04-20 14:16:22 +09001417MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
1418MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
1419MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
1420MODULE_DESCRIPTION("External connector (extcon) class driver");
1421MODULE_LICENSE("GPL");