2 * drivers/extcon/extcon_class.c
4 * External connector (extcon) class driver
6 * Copyright (C) 2012 Samsung Electronics
7 * Author: Donggeun Kim <dg77.kim@samsung.com>
8 * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
10 * based on android/drivers/switch/switch_class.c
11 * Copyright (C) 2008 Google, Inc.
12 * Author: Mike Lockwood <lockwood@android.com>
14 * This software is licensed under the terms of the GNU General Public
15 * License version 2, as published by the Free Software Foundation, and
16 * may be copied, distributed, and modified under those terms.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
30 #include <linux/err.h>
31 #include <linux/extcon.h>
32 #include <linux/slab.h>
34 struct class *extcon_class;
35 #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH)
36 static struct class_compat *switch_class;
37 #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */
39 static LIST_HEAD(extcon_dev_list);
40 static DEFINE_MUTEX(extcon_dev_list_lock);
42 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
45 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
47 if (edev->print_state) {
48 int ret = edev->print_state(edev, buf);
52 /* Use default if failed */
54 return sprintf(buf, "%u\n", edev->state);
57 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
60 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
62 /* Optional callback given by the user */
63 if (edev->print_name) {
64 int ret = edev->print_name(edev, buf);
69 return sprintf(buf, "%s\n", dev_name(edev->dev));
73 * extcon_set_state() - Set the cable attach states of the extcon device.
74 * @edev: the extcon device
75 * @state: new cable attach status for @edev
77 * Changing the state sends uevent with environment variable containing
78 * the name of extcon device (envp[0]) and the state output (envp[1]).
79 * Tizen uses this format for extcon device to get events from ports.
80 * Android uses this format as well.
82 * Note that notifier provides the which bits are changes in the state
83 * variable with "val" to the callback.
85 void extcon_set_state(struct extcon_dev *edev, u32 state)
93 u32 old_state = edev->state;
95 if (edev->state != state) {
98 raw_notifier_call_chain(&edev->nh, old_state ^ edev->state,
101 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
103 length = name_show(edev->dev, NULL, prop_buf);
105 if (prop_buf[length - 1] == '\n')
106 prop_buf[length - 1] = 0;
107 snprintf(name_buf, sizeof(name_buf),
108 "NAME=%s", prop_buf);
109 envp[env_offset++] = name_buf;
111 length = state_show(edev->dev, NULL, prop_buf);
113 if (prop_buf[length - 1] == '\n')
114 prop_buf[length - 1] = 0;
115 snprintf(state_buf, sizeof(state_buf),
116 "STATE=%s", prop_buf);
117 envp[env_offset++] = state_buf;
119 envp[env_offset] = NULL;
120 kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp);
121 free_page((unsigned long)prop_buf);
123 dev_err(edev->dev, "out of memory in extcon_set_state\n");
124 kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE);
128 EXPORT_SYMBOL_GPL(extcon_set_state);
131 * extcon_get_extcon_dev() - Get the extcon device instance from the name
132 * @extcon_name: The extcon name provided with extcon_dev_register()
134 struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
136 struct extcon_dev *sd;
138 mutex_lock(&extcon_dev_list_lock);
139 list_for_each_entry(sd, &extcon_dev_list, entry) {
140 if (!strcmp(sd->name, extcon_name))
145 mutex_unlock(&extcon_dev_list_lock);
148 EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
151 * extcon_register_notifier() - Register a notifee to get notified by
152 * any attach status changes from the extcon.
153 * @edev: the extcon device.
154 * @nb: a notifier block to be registered.
156 int extcon_register_notifier(struct extcon_dev *edev,
157 struct notifier_block *nb)
159 return raw_notifier_chain_register(&edev->nh, nb);
161 EXPORT_SYMBOL_GPL(extcon_register_notifier);
164 * extcon_unregister_notifier() - Unregister a notifee from the extcon device.
165 * @edev: the extcon device.
166 * @nb: a registered notifier block to be unregistered.
168 int extcon_unregister_notifier(struct extcon_dev *edev,
169 struct notifier_block *nb)
171 return raw_notifier_chain_unregister(&edev->nh, nb);
173 EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
175 static struct device_attribute extcon_attrs[] = {
180 static int create_extcon_class(void)
183 extcon_class = class_create(THIS_MODULE, "extcon");
184 if (IS_ERR(extcon_class))
185 return PTR_ERR(extcon_class);
186 extcon_class->dev_attrs = extcon_attrs;
188 #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH)
189 switch_class = class_compat_register("switch");
190 if (WARN(!switch_class, "cannot allocate"))
192 #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */
198 static void extcon_cleanup(struct extcon_dev *edev, bool skip)
200 mutex_lock(&extcon_dev_list_lock);
201 list_del(&edev->entry);
202 mutex_unlock(&extcon_dev_list_lock);
204 if (!skip && get_device(edev->dev)) {
205 device_unregister(edev->dev);
206 put_device(edev->dev);
212 static void extcon_dev_release(struct device *dev)
214 struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev);
216 extcon_cleanup(edev, true);
220 * extcon_dev_register() - Register a new extcon device
221 * @edev : the new extcon device (should be allocated before calling)
222 * @dev : the parent device for this extcon device.
224 * Among the members of edev struct, please set the "user initializing data"
225 * in any case and set the "optional callbacks" if required. However, please
226 * do not set the values of "internal data", which are initialized by
229 int extcon_dev_register(struct extcon_dev *edev, struct device *dev)
234 ret = create_extcon_class();
239 edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
240 edev->dev->parent = dev;
241 edev->dev->class = extcon_class;
242 edev->dev->release = extcon_dev_release;
244 dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev));
245 ret = device_register(edev->dev);
247 put_device(edev->dev);
250 #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH)
252 ret = class_compat_create_link(switch_class, edev->dev,
254 #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */
256 RAW_INIT_NOTIFIER_HEAD(&edev->nh);
258 dev_set_drvdata(edev->dev, edev);
261 mutex_lock(&extcon_dev_list_lock);
262 list_add(&edev->entry, &extcon_dev_list);
263 mutex_unlock(&extcon_dev_list_lock);
271 EXPORT_SYMBOL_GPL(extcon_dev_register);
274 * extcon_dev_unregister() - Unregister the extcon device.
275 * @edev: the extcon device instance to be unregitered.
277 * Note that this does not call kfree(edev) because edev was not allocated
280 void extcon_dev_unregister(struct extcon_dev *edev)
282 extcon_cleanup(edev, false);
284 EXPORT_SYMBOL_GPL(extcon_dev_unregister);
286 static int __init extcon_class_init(void)
288 return create_extcon_class();
290 module_init(extcon_class_init);
292 static void __exit extcon_class_exit(void)
294 class_destroy(extcon_class);
296 module_exit(extcon_class_exit);
298 MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
299 MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
300 MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
301 MODULE_DESCRIPTION("External connector (extcon) class driver");
302 MODULE_LICENSE("GPL");