MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 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 | |
| 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) */ |
| 38 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 39 | static LIST_HEAD(extcon_dev_list); |
| 40 | static DEFINE_MUTEX(extcon_dev_list_lock); |
| 41 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 42 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 43 | char *buf) |
| 44 | { |
| 45 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 46 | |
| 47 | if (edev->print_state) { |
| 48 | int ret = edev->print_state(edev, buf); |
| 49 | |
| 50 | if (ret >= 0) |
| 51 | return ret; |
| 52 | /* Use default if failed */ |
| 53 | } |
| 54 | return sprintf(buf, "%u\n", edev->state); |
| 55 | } |
| 56 | |
| 57 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 58 | char *buf) |
| 59 | { |
| 60 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 61 | |
| 62 | /* Optional callback given by the user */ |
| 63 | if (edev->print_name) { |
| 64 | int ret = edev->print_name(edev, buf); |
| 65 | if (ret >= 0) |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | return sprintf(buf, "%s\n", dev_name(edev->dev)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 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 |
| 76 | * |
| 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. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 81 | * |
| 82 | * Note that notifier provides the which bits are changes in the state |
| 83 | * variable with "val" to the callback. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 84 | */ |
| 85 | void extcon_set_state(struct extcon_dev *edev, u32 state) |
| 86 | { |
| 87 | char name_buf[120]; |
| 88 | char state_buf[120]; |
| 89 | char *prop_buf; |
| 90 | char *envp[3]; |
| 91 | int env_offset = 0; |
| 92 | int length; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 93 | u32 old_state = edev->state; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 94 | |
| 95 | if (edev->state != state) { |
| 96 | edev->state = state; |
| 97 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 98 | raw_notifier_call_chain(&edev->nh, old_state ^ edev->state, |
| 99 | edev); |
| 100 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 101 | prop_buf = (char *)get_zeroed_page(GFP_KERNEL); |
| 102 | if (prop_buf) { |
| 103 | length = name_show(edev->dev, NULL, prop_buf); |
| 104 | if (length > 0) { |
| 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; |
| 110 | } |
| 111 | length = state_show(edev->dev, NULL, prop_buf); |
| 112 | if (length > 0) { |
| 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; |
| 118 | } |
| 119 | envp[env_offset] = NULL; |
| 120 | kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp); |
| 121 | free_page((unsigned long)prop_buf); |
| 122 | } else { |
| 123 | dev_err(edev->dev, "out of memory in extcon_set_state\n"); |
| 124 | kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(extcon_set_state); |
| 129 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 130 | /** |
| 131 | * extcon_get_extcon_dev() - Get the extcon device instance from the name |
| 132 | * @extcon_name: The extcon name provided with extcon_dev_register() |
| 133 | */ |
| 134 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) |
| 135 | { |
| 136 | struct extcon_dev *sd; |
| 137 | |
| 138 | mutex_lock(&extcon_dev_list_lock); |
| 139 | list_for_each_entry(sd, &extcon_dev_list, entry) { |
| 140 | if (!strcmp(sd->name, extcon_name)) |
| 141 | goto out; |
| 142 | } |
| 143 | sd = NULL; |
| 144 | out: |
| 145 | mutex_unlock(&extcon_dev_list_lock); |
| 146 | return sd; |
| 147 | } |
| 148 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); |
| 149 | |
| 150 | /** |
| 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. |
| 155 | */ |
| 156 | int extcon_register_notifier(struct extcon_dev *edev, |
| 157 | struct notifier_block *nb) |
| 158 | { |
| 159 | return raw_notifier_chain_register(&edev->nh, nb); |
| 160 | } |
| 161 | EXPORT_SYMBOL_GPL(extcon_register_notifier); |
| 162 | |
| 163 | /** |
| 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. |
| 167 | */ |
| 168 | int extcon_unregister_notifier(struct extcon_dev *edev, |
| 169 | struct notifier_block *nb) |
| 170 | { |
| 171 | return raw_notifier_chain_unregister(&edev->nh, nb); |
| 172 | } |
| 173 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); |
| 174 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 175 | static struct device_attribute extcon_attrs[] = { |
| 176 | __ATTR_RO(state), |
| 177 | __ATTR_RO(name), |
| 178 | }; |
| 179 | |
| 180 | static int create_extcon_class(void) |
| 181 | { |
| 182 | if (!extcon_class) { |
| 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; |
| 187 | |
| 188 | #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH) |
| 189 | switch_class = class_compat_register("switch"); |
| 190 | if (WARN(!switch_class, "cannot allocate")) |
| 191 | return -ENOMEM; |
| 192 | #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */ |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | static void extcon_cleanup(struct extcon_dev *edev, bool skip) |
| 199 | { |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 200 | mutex_lock(&extcon_dev_list_lock); |
| 201 | list_del(&edev->entry); |
| 202 | mutex_unlock(&extcon_dev_list_lock); |
| 203 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 204 | if (!skip && get_device(edev->dev)) { |
| 205 | device_unregister(edev->dev); |
| 206 | put_device(edev->dev); |
| 207 | } |
| 208 | |
| 209 | kfree(edev->dev); |
| 210 | } |
| 211 | |
| 212 | static void extcon_dev_release(struct device *dev) |
| 213 | { |
| 214 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 215 | |
| 216 | extcon_cleanup(edev, true); |
| 217 | } |
| 218 | |
| 219 | /** |
| 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. |
| 223 | * |
| 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 |
| 227 | * this function. |
| 228 | */ |
| 229 | int extcon_dev_register(struct extcon_dev *edev, struct device *dev) |
| 230 | { |
| 231 | int ret; |
| 232 | |
| 233 | if (!extcon_class) { |
| 234 | ret = create_extcon_class(); |
| 235 | if (ret < 0) |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 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; |
| 243 | |
| 244 | dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev)); |
| 245 | ret = device_register(edev->dev); |
| 246 | if (ret) { |
| 247 | put_device(edev->dev); |
| 248 | goto err_dev; |
| 249 | } |
| 250 | #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH) |
| 251 | if (switch_class) |
| 252 | ret = class_compat_create_link(switch_class, edev->dev, |
| 253 | dev); |
| 254 | #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */ |
| 255 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 256 | RAW_INIT_NOTIFIER_HEAD(&edev->nh); |
| 257 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 258 | dev_set_drvdata(edev->dev, edev); |
| 259 | edev->state = 0; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame^] | 260 | |
| 261 | mutex_lock(&extcon_dev_list_lock); |
| 262 | list_add(&edev->entry, &extcon_dev_list); |
| 263 | mutex_unlock(&extcon_dev_list_lock); |
| 264 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 265 | return 0; |
| 266 | |
| 267 | err_dev: |
| 268 | kfree(edev->dev); |
| 269 | return ret; |
| 270 | } |
| 271 | EXPORT_SYMBOL_GPL(extcon_dev_register); |
| 272 | |
| 273 | /** |
| 274 | * extcon_dev_unregister() - Unregister the extcon device. |
| 275 | * @edev: the extcon device instance to be unregitered. |
| 276 | * |
| 277 | * Note that this does not call kfree(edev) because edev was not allocated |
| 278 | * by this class. |
| 279 | */ |
| 280 | void extcon_dev_unregister(struct extcon_dev *edev) |
| 281 | { |
| 282 | extcon_cleanup(edev, false); |
| 283 | } |
| 284 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); |
| 285 | |
| 286 | static int __init extcon_class_init(void) |
| 287 | { |
| 288 | return create_extcon_class(); |
| 289 | } |
| 290 | module_init(extcon_class_init); |
| 291 | |
| 292 | static void __exit extcon_class_exit(void) |
| 293 | { |
| 294 | class_destroy(extcon_class); |
| 295 | } |
| 296 | module_exit(extcon_class_exit); |
| 297 | |
| 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"); |