aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/ABI/testing/sysfs-class-extcon26
-rw-r--r--drivers/Kconfig2
-rw-r--r--drivers/Makefile1
-rw-r--r--drivers/extcon/Kconfig17
-rw-r--r--drivers/extcon/Makefile5
-rw-r--r--drivers/extcon/extcon_class.c236
-rw-r--r--include/linux/extcon.h82
7 files changed, 369 insertions, 0 deletions
diff --git a/Documentation/ABI/testing/sysfs-class-extcon b/Documentation/ABI/testing/sysfs-class-extcon
new file mode 100644
index 000000000000..59a4b1c708d5
--- /dev/null
+++ b/Documentation/ABI/testing/sysfs-class-extcon
@@ -0,0 +1,26 @@
1What: /sys/class/extcon/.../
2Date: December 2011
3Contact: MyungJoo Ham <myungjoo.ham@samsung.com>
4Description:
5 Provide a place in sysfs for the extcon objects.
6 This allows accessing extcon specific variables.
7 The name of extcon object denoted as ... is the name given
8 with extcon_dev_register.
9
10What: /sys/class/extcon/.../name
11Date: December 2011
12Contact: MyungJoo Ham <myungjoo.ham@samsung.com>
13Description:
14 The /sys/class/extcon/.../name shows the name of the extcon
15 object. If the extcon object has an optional callback
16 "show_name" defined, the callback will provide the name with
17 this sysfs node.
18
19What: /sys/class/extcon/.../state
20Date: December 2011
21Contact: MyungJoo Ham <myungjoo.ham@samsung.com>
22Description:
23 The /sys/class/extcon/.../state shows the cable attach/detach
24 information of the corresponding extcon object. If the extcon
25 objecct has an optional callback "show_state" defined, the
26 callback will provide the name with this sysfs node.
diff --git a/drivers/Kconfig b/drivers/Kconfig
index d236aef7e59f..0233ad979b7d 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -140,4 +140,6 @@ source "drivers/virt/Kconfig"
140 140
141source "drivers/devfreq/Kconfig" 141source "drivers/devfreq/Kconfig"
142 142
143source "drivers/extcon/Kconfig"
144
143endmenu 145endmenu
diff --git a/drivers/Makefile b/drivers/Makefile
index 95952c82bf16..c41dfa92cd79 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -134,3 +134,4 @@ obj-$(CONFIG_VIRT_DRIVERS) += virt/
134obj-$(CONFIG_HYPERV) += hv/ 134obj-$(CONFIG_HYPERV) += hv/
135 135
136obj-$(CONFIG_PM_DEVFREQ) += devfreq/ 136obj-$(CONFIG_PM_DEVFREQ) += devfreq/
137obj-$(CONFIG_EXTCON) += extcon/
diff --git a/drivers/extcon/Kconfig b/drivers/extcon/Kconfig
new file mode 100644
index 000000000000..7932e1bd9b02
--- /dev/null
+++ b/drivers/extcon/Kconfig
@@ -0,0 +1,17 @@
1menuconfig EXTCON
2 tristate "External Connector Class (extcon) support"
3 help
4 Say Y here to enable external connector class (extcon) support.
5 This allows monitoring external connectors by userspace
6 via sysfs and uevent and supports external connectors with
7 multiple states; i.e., an extcon that may have multiple
8 cables attached. For example, an external connector of a device
9 may be used to connect an HDMI cable and a AC adaptor, and to
10 host USB ports. Many of 30-pin connectors including PDMI are
11 also good examples.
12
13if EXTCON
14
15comment "Extcon Device Drivers"
16
17endif # MULTISTATE_SWITCH
diff --git a/drivers/extcon/Makefile b/drivers/extcon/Makefile
new file mode 100644
index 000000000000..6bc69214fcd7
--- /dev/null
+++ b/drivers/extcon/Makefile
@@ -0,0 +1,5 @@
1#
2# Makefile for external connector class (extcon) devices
3#
4
5obj-$(CONFIG_EXTCON) += extcon_class.o
diff --git a/drivers/extcon/extcon_class.c b/drivers/extcon/extcon_class.c
new file mode 100644
index 000000000000..3c46368c279a
--- /dev/null
+++ b/drivers/extcon/extcon_class.c
@@ -0,0 +1,236 @@
1/*
2 * drivers/extcon/extcon_class.c
3 *
4 * External connector (extcon) class driver
5 *
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
9 *
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
13 *
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23*/
24
25#include <linux/module.h>
26#include <linux/types.h>
27#include <linux/init.h>
28#include <linux/device.h>
29#include <linux/fs.h>
30#include <linux/err.h>
31#include <linux/extcon.h>
32#include <linux/slab.h>
33
34struct class *extcon_class;
35#if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH)
36static struct class_compat *switch_class;
37#endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */
38
39static ssize_t state_show(struct device *dev, struct device_attribute *attr,
40 char *buf)
41{
42 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
43
44 if (edev->print_state) {
45 int ret = edev->print_state(edev, buf);
46
47 if (ret >= 0)
48 return ret;
49 /* Use default if failed */
50 }
51 return sprintf(buf, "%u\n", edev->state);
52}
53
54static ssize_t name_show(struct device *dev, struct device_attribute *attr,
55 char *buf)
56{
57 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
58
59 /* Optional callback given by the user */
60 if (edev->print_name) {
61 int ret = edev->print_name(edev, buf);
62 if (ret >= 0)
63 return ret;
64 }
65
66 return sprintf(buf, "%s\n", dev_name(edev->dev));
67}
68
69/**
70 * extcon_set_state() - Set the cable attach states of the extcon device.
71 * @edev: the extcon device
72 * @state: new cable attach status for @edev
73 *
74 * Changing the state sends uevent with environment variable containing
75 * the name of extcon device (envp[0]) and the state output (envp[1]).
76 * Tizen uses this format for extcon device to get events from ports.
77 * Android uses this format as well.
78 */
79void extcon_set_state(struct extcon_dev *edev, u32 state)
80{
81 char name_buf[120];
82 char state_buf[120];
83 char *prop_buf;
84 char *envp[3];
85 int env_offset = 0;
86 int length;
87
88 if (edev->state != state) {
89 edev->state = state;
90
91 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
92 if (prop_buf) {
93 length = name_show(edev->dev, NULL, prop_buf);
94 if (length > 0) {
95 if (prop_buf[length - 1] == '\n')
96 prop_buf[length - 1] = 0;
97 snprintf(name_buf, sizeof(name_buf),
98 "NAME=%s", prop_buf);
99 envp[env_offset++] = name_buf;
100 }
101 length = state_show(edev->dev, NULL, prop_buf);
102 if (length > 0) {
103 if (prop_buf[length - 1] == '\n')
104 prop_buf[length - 1] = 0;
105 snprintf(state_buf, sizeof(state_buf),
106 "STATE=%s", prop_buf);
107 envp[env_offset++] = state_buf;
108 }
109 envp[env_offset] = NULL;
110 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
111 free_page((unsigned long)prop_buf);
112 } else {
113 dev_err(edev->dev, "out of memory in extcon_set_state\n");
114 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
115 }
116 }
117}
118EXPORT_SYMBOL_GPL(extcon_set_state);
119
120static struct device_attribute extcon_attrs[] = {
121 __ATTR_RO(state),
122 __ATTR_RO(name),
123};
124
125static int create_extcon_class(void)
126{
127 if (!extcon_class) {
128 extcon_class = class_create(THIS_MODULE, "extcon");
129 if (IS_ERR(extcon_class))
130 return PTR_ERR(extcon_class);
131 extcon_class->dev_attrs = extcon_attrs;
132
133#if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH)
134 switch_class = class_compat_register("switch");
135 if (WARN(!switch_class, "cannot allocate"))
136 return -ENOMEM;
137#endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */
138 }
139
140 return 0;
141}
142
143static void extcon_cleanup(struct extcon_dev *edev, bool skip)
144{
145 if (!skip && get_device(edev->dev)) {
146 device_unregister(edev->dev);
147 put_device(edev->dev);
148 }
149
150 kfree(edev->dev);
151}
152
153static void extcon_dev_release(struct device *dev)
154{
155 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
156
157 extcon_cleanup(edev, true);
158}
159
160/**
161 * extcon_dev_register() - Register a new extcon device
162 * @edev : the new extcon device (should be allocated before calling)
163 * @dev : the parent device for this extcon device.
164 *
165 * Among the members of edev struct, please set the "user initializing data"
166 * in any case and set the "optional callbacks" if required. However, please
167 * do not set the values of "internal data", which are initialized by
168 * this function.
169 */
170int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
171{
172 int ret;
173
174 if (!extcon_class) {
175 ret = create_extcon_class();
176 if (ret < 0)
177 return ret;
178 }
179
180 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
181 edev->dev->parent = dev;
182 edev->dev->class = extcon_class;
183 edev->dev->release = extcon_dev_release;
184
185 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
186 ret = device_register(edev->dev);
187 if (ret) {
188 put_device(edev->dev);
189 goto err_dev;
190 }
191#if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH)
192 if (switch_class)
193 ret = class_compat_create_link(switch_class, edev->dev,
194 dev);
195#endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */
196
197 dev_set_drvdata(edev->dev, edev);
198 edev->state = 0;
199 return 0;
200
201err_dev:
202 kfree(edev->dev);
203 return ret;
204}
205EXPORT_SYMBOL_GPL(extcon_dev_register);
206
207/**
208 * extcon_dev_unregister() - Unregister the extcon device.
209 * @edev: the extcon device instance to be unregitered.
210 *
211 * Note that this does not call kfree(edev) because edev was not allocated
212 * by this class.
213 */
214void extcon_dev_unregister(struct extcon_dev *edev)
215{
216 extcon_cleanup(edev, false);
217}
218EXPORT_SYMBOL_GPL(extcon_dev_unregister);
219
220static int __init extcon_class_init(void)
221{
222 return create_extcon_class();
223}
224module_init(extcon_class_init);
225
226static void __exit extcon_class_exit(void)
227{
228 class_destroy(extcon_class);
229}
230module_exit(extcon_class_exit);
231
232MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
233MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
234MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
235MODULE_DESCRIPTION("External connector (extcon) class driver");
236MODULE_LICENSE("GPL");
diff --git a/include/linux/extcon.h b/include/linux/extcon.h
new file mode 100644
index 000000000000..9cb3aaaf67f5
--- /dev/null
+++ b/include/linux/extcon.h
@@ -0,0 +1,82 @@
1/*
2 * External connector (extcon) class driver
3 *
4 * Copyright (C) 2012 Samsung Electronics
5 * Author: Donggeun Kim <dg77.kim@samsung.com>
6 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
7 *
8 * based on switch class driver
9 * Copyright (C) 2008 Google, Inc.
10 * Author: Mike Lockwood <lockwood@android.com>
11 *
12 * This software is licensed under the terms of the GNU General Public
13 * License version 2, as published by the Free Software Foundation, and
14 * may be copied, distributed, and modified under those terms.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21*/
22
23#ifndef __LINUX_EXTCON_H__
24#define __LINUX_EXTCON_H__
25
26/**
27 * struct extcon_dev - An extcon device represents one external connector.
28 * @name The name of this extcon device. Parent device name is used
29 * if NULL.
30 * @print_name An optional callback to override the method to print the
31 * name of the extcon device.
32 * @print_state An optional callback to override the method to print the
33 * status of the extcon device.
34 * @dev Device of this extcon. Do not provide at register-time.
35 * @state Attach/detach state of this extcon. Do not provide at
36 * register-time
37 *
38 * In most cases, users only need to provide "User initializing data" of
39 * this struct when registering an extcon. In some exceptional cases,
40 * optional callbacks may be needed. However, the values in "internal data"
41 * are overwritten by register function.
42 */
43struct extcon_dev {
44 /* --- Optional user initializing data --- */
45 const char *name;
46
47 /* --- Optional callbacks to override class functions --- */
48 ssize_t (*print_name)(struct extcon_dev *edev, char *buf);
49 ssize_t (*print_state)(struct extcon_dev *edev, char *buf);
50
51 /* --- Internal data. Please do not set. --- */
52 struct device *dev;
53 u32 state;
54};
55
56#if IS_ENABLED(CONFIG_EXTCON)
57extern int extcon_dev_register(struct extcon_dev *edev, struct device *dev);
58extern void extcon_dev_unregister(struct extcon_dev *edev);
59
60static inline u32 extcon_get_state(struct extcon_dev *edev)
61{
62 return edev->state;
63}
64
65extern void extcon_set_state(struct extcon_dev *edev, u32 state);
66#else /* CONFIG_EXTCON */
67static inline int extcon_dev_register(struct extcon_dev *edev,
68 struct device *dev)
69{
70 return 0;
71}
72
73static inline void extcon_dev_unregister(struct extcon_dev *edev) { }
74
75static inline u32 extcon_get_state(struct extcon_dev *edev)
76{
77 return 0;
78}
79
80static inline void extcon_set_state(struct extcon_dev *edev, u32 state) { }
81#endif /* CONFIG_EXTCON */
82#endif /* __LINUX_EXTCON_H__ */