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 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 34 | /* |
| 35 | * extcon_cable_name suggests the standard cable names for commonly used |
| 36 | * cable types. |
| 37 | * |
| 38 | * However, please do not use extcon_cable_name directly for extcon_dev |
| 39 | * struct's supported_cable pointer unless your device really supports |
| 40 | * every single port-type of the following cable names. Please choose cable |
| 41 | * names that are actually used in your extcon device. |
| 42 | */ |
| 43 | const char *extcon_cable_name[] = { |
| 44 | [EXTCON_USB] = "USB", |
| 45 | [EXTCON_USB_HOST] = "USB-Host", |
| 46 | [EXTCON_TA] = "TA", |
| 47 | [EXTCON_FAST_CHARGER] = "Fast-charger", |
| 48 | [EXTCON_SLOW_CHARGER] = "Slow-charger", |
| 49 | [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream", |
| 50 | [EXTCON_HDMI] = "HDMI", |
| 51 | [EXTCON_MHL] = "MHL", |
| 52 | [EXTCON_DVI] = "DVI", |
| 53 | [EXTCON_VGA] = "VGA", |
| 54 | [EXTCON_DOCK] = "Dock", |
| 55 | [EXTCON_LINE_IN] = "Line-in", |
| 56 | [EXTCON_LINE_OUT] = "Line-out", |
| 57 | [EXTCON_MIC_IN] = "Microphone", |
| 58 | [EXTCON_HEADPHONE_OUT] = "Headphone", |
| 59 | [EXTCON_SPDIF_IN] = "SPDIF-in", |
| 60 | [EXTCON_SPDIF_OUT] = "SPDIF-out", |
| 61 | [EXTCON_VIDEO_IN] = "Video-in", |
| 62 | [EXTCON_VIDEO_OUT] = "Video-out", |
| 63 | |
| 64 | NULL, |
| 65 | }; |
| 66 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 67 | struct class *extcon_class; |
| 68 | #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH) |
| 69 | static struct class_compat *switch_class; |
| 70 | #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */ |
| 71 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 72 | static LIST_HEAD(extcon_dev_list); |
| 73 | static DEFINE_MUTEX(extcon_dev_list_lock); |
| 74 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 75 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 76 | char *buf) |
| 77 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 78 | int i, count = 0; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 79 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 80 | |
| 81 | if (edev->print_state) { |
| 82 | int ret = edev->print_state(edev, buf); |
| 83 | |
| 84 | if (ret >= 0) |
| 85 | return ret; |
| 86 | /* Use default if failed */ |
| 87 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 88 | |
| 89 | if (edev->max_supported == 0) |
| 90 | return sprintf(buf, "%u\n", edev->state); |
| 91 | |
| 92 | for (i = 0; i < SUPPORTED_CABLE_MAX; i++) { |
| 93 | if (!edev->supported_cable[i]) |
| 94 | break; |
| 95 | count += sprintf(buf + count, "%s=%d\n", |
| 96 | edev->supported_cable[i], |
| 97 | !!(edev->state & (1 << i))); |
| 98 | } |
| 99 | |
| 100 | return count; |
| 101 | } |
| 102 | |
| 103 | void extcon_set_state(struct extcon_dev *edev, u32 state); |
| 104 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 105 | const char *buf, size_t count) |
| 106 | { |
| 107 | u32 state; |
| 108 | ssize_t ret = 0; |
| 109 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 110 | |
| 111 | ret = sscanf(buf, "0x%x", &state); |
| 112 | if (ret == 0) |
| 113 | ret = -EINVAL; |
| 114 | else |
| 115 | extcon_set_state(edev, state); |
| 116 | |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | |
| 120 | return count; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 124 | char *buf) |
| 125 | { |
| 126 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 127 | |
| 128 | /* Optional callback given by the user */ |
| 129 | if (edev->print_name) { |
| 130 | int ret = edev->print_name(edev, buf); |
| 131 | if (ret >= 0) |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | return sprintf(buf, "%s\n", dev_name(edev->dev)); |
| 136 | } |
| 137 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 138 | static ssize_t cable_name_show(struct device *dev, |
| 139 | struct device_attribute *attr, char *buf) |
| 140 | { |
| 141 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 142 | attr_name); |
| 143 | |
| 144 | return sprintf(buf, "%s\n", |
| 145 | cable->edev->supported_cable[cable->cable_index]); |
| 146 | } |
| 147 | |
| 148 | static ssize_t cable_state_show(struct device *dev, |
| 149 | struct device_attribute *attr, char *buf) |
| 150 | { |
| 151 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 152 | attr_state); |
| 153 | |
| 154 | return sprintf(buf, "%d\n", |
| 155 | extcon_get_cable_state_(cable->edev, |
| 156 | cable->cable_index)); |
| 157 | } |
| 158 | |
| 159 | static ssize_t cable_state_store(struct device *dev, |
| 160 | struct device_attribute *attr, const char *buf, |
| 161 | size_t count) |
| 162 | { |
| 163 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 164 | attr_state); |
| 165 | int ret, state; |
| 166 | |
| 167 | ret = sscanf(buf, "%d", &state); |
| 168 | if (ret == 0) |
| 169 | ret = -EINVAL; |
| 170 | else |
| 171 | ret = extcon_set_cable_state_(cable->edev, cable->cable_index, |
| 172 | state); |
| 173 | |
| 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | return count; |
| 177 | } |
| 178 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 179 | /** |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 180 | * extcon_update_state() - Update the cable attach states of the extcon device |
| 181 | * only for the masked bits. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 182 | * @edev: the extcon device |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 183 | * @mask: the bit mask to designate updated bits. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 184 | * @state: new cable attach status for @edev |
| 185 | * |
| 186 | * Changing the state sends uevent with environment variable containing |
| 187 | * the name of extcon device (envp[0]) and the state output (envp[1]). |
| 188 | * Tizen uses this format for extcon device to get events from ports. |
| 189 | * Android uses this format as well. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 190 | * |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 191 | * Note that the notifier provides which bits are changed in the state |
| 192 | * variable with the val parameter (second) to the callback. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 193 | */ |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 194 | void extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 195 | { |
| 196 | char name_buf[120]; |
| 197 | char state_buf[120]; |
| 198 | char *prop_buf; |
| 199 | char *envp[3]; |
| 200 | int env_offset = 0; |
| 201 | int length; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 202 | unsigned long flags; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 203 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 204 | spin_lock_irqsave(&edev->lock, flags); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 205 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 206 | if (edev->state != ((edev->state & ~mask) | (state & mask))) { |
| 207 | u32 old_state = edev->state; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 208 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 209 | edev->state &= ~mask; |
| 210 | edev->state |= state & mask; |
| 211 | |
| 212 | raw_notifier_call_chain(&edev->nh, old_state, edev); |
| 213 | |
| 214 | /* This could be in interrupt handler */ |
| 215 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 216 | if (prop_buf) { |
| 217 | length = name_show(edev->dev, NULL, prop_buf); |
| 218 | if (length > 0) { |
| 219 | if (prop_buf[length - 1] == '\n') |
| 220 | prop_buf[length - 1] = 0; |
| 221 | snprintf(name_buf, sizeof(name_buf), |
| 222 | "NAME=%s", prop_buf); |
| 223 | envp[env_offset++] = name_buf; |
| 224 | } |
| 225 | length = state_show(edev->dev, NULL, prop_buf); |
| 226 | if (length > 0) { |
| 227 | if (prop_buf[length - 1] == '\n') |
| 228 | prop_buf[length - 1] = 0; |
| 229 | snprintf(state_buf, sizeof(state_buf), |
| 230 | "STATE=%s", prop_buf); |
| 231 | envp[env_offset++] = state_buf; |
| 232 | } |
| 233 | envp[env_offset] = NULL; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 234 | /* Unlock early before uevent */ |
| 235 | spin_unlock_irqrestore(&edev->lock, flags); |
| 236 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 237 | kobject_uevent_env(&edev->dev->kobj, KOBJ_CHANGE, envp); |
| 238 | free_page((unsigned long)prop_buf); |
| 239 | } else { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 240 | /* Unlock early before uevent */ |
| 241 | spin_unlock_irqrestore(&edev->lock, flags); |
| 242 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 243 | dev_err(edev->dev, "out of memory in extcon_set_state\n"); |
| 244 | kobject_uevent(&edev->dev->kobj, KOBJ_CHANGE); |
| 245 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 246 | } else { |
| 247 | /* No changes */ |
| 248 | spin_unlock_irqrestore(&edev->lock, flags); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 249 | } |
| 250 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 251 | EXPORT_SYMBOL_GPL(extcon_update_state); |
| 252 | |
| 253 | /** |
| 254 | * extcon_set_state() - Set the cable attach states of the extcon device. |
| 255 | * @edev: the extcon device |
| 256 | * @state: new cable attach status for @edev |
| 257 | * |
| 258 | * Note that notifier provides which bits are changed in the state |
| 259 | * variable with the val parameter (second) to the callback. |
| 260 | */ |
| 261 | void extcon_set_state(struct extcon_dev *edev, u32 state) |
| 262 | { |
| 263 | extcon_update_state(edev, 0xffffffff, state); |
| 264 | } |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 265 | EXPORT_SYMBOL_GPL(extcon_set_state); |
| 266 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 267 | /** |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 268 | * extcon_find_cable_index() - Get the cable index based on the cable name. |
| 269 | * @edev: the extcon device that has the cable. |
| 270 | * @cable_name: cable name to be searched. |
| 271 | * |
| 272 | * Note that accessing a cable state based on cable_index is faster than |
| 273 | * cable_name because using cable_name induces a loop with strncmp(). |
| 274 | * Thus, when get/set_cable_state is repeatedly used, using cable_index |
| 275 | * is recommended. |
| 276 | */ |
| 277 | int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name) |
| 278 | { |
| 279 | int i; |
| 280 | |
| 281 | if (edev->supported_cable) { |
| 282 | for (i = 0; edev->supported_cable[i]; i++) { |
| 283 | if (!strncmp(edev->supported_cable[i], |
| 284 | cable_name, CABLE_NAME_MAX)) |
| 285 | return i; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return -EINVAL; |
| 290 | } |
| 291 | EXPORT_SYMBOL_GPL(extcon_find_cable_index); |
| 292 | |
| 293 | /** |
| 294 | * extcon_get_cable_state_() - Get the status of a specific cable. |
| 295 | * @edev: the extcon device that has the cable. |
| 296 | * @index: cable index that can be retrieved by extcon_find_cable_index(). |
| 297 | */ |
| 298 | int extcon_get_cable_state_(struct extcon_dev *edev, int index) |
| 299 | { |
| 300 | if (index < 0 || (edev->max_supported && edev->max_supported <= index)) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | return !!(edev->state & (1 << index)); |
| 304 | } |
| 305 | EXPORT_SYMBOL_GPL(extcon_get_cable_state_); |
| 306 | |
| 307 | /** |
| 308 | * extcon_get_cable_state() - Get the status of a specific cable. |
| 309 | * @edev: the extcon device that has the cable. |
| 310 | * @cable_name: cable name. |
| 311 | * |
| 312 | * Note that this is slower than extcon_get_cable_state_. |
| 313 | */ |
| 314 | int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name) |
| 315 | { |
| 316 | return extcon_get_cable_state_(edev, extcon_find_cable_index |
| 317 | (edev, cable_name)); |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(extcon_get_cable_state); |
| 320 | |
| 321 | /** |
| 322 | * extcon_get_cable_state_() - Set the status of a specific cable. |
| 323 | * @edev: the extcon device that has the cable. |
| 324 | * @index: cable index that can be retrieved by extcon_find_cable_index(). |
| 325 | * @cable_state: the new cable status. The default semantics is |
| 326 | * true: attached / false: detached. |
| 327 | */ |
| 328 | int extcon_set_cable_state_(struct extcon_dev *edev, |
| 329 | int index, bool cable_state) |
| 330 | { |
| 331 | u32 state; |
| 332 | |
| 333 | if (index < 0 || (edev->max_supported && edev->max_supported <= index)) |
| 334 | return -EINVAL; |
| 335 | |
| 336 | state = cable_state ? (1 << index) : 0; |
| 337 | extcon_update_state(edev, 1 << index, state); |
| 338 | return 0; |
| 339 | } |
| 340 | EXPORT_SYMBOL_GPL(extcon_set_cable_state_); |
| 341 | |
| 342 | /** |
| 343 | * extcon_get_cable_state() - Set the status of a specific cable. |
| 344 | * @edev: the extcon device that has the cable. |
| 345 | * @cable_name: cable name. |
| 346 | * @cable_state: the new cable status. The default semantics is |
| 347 | * true: attached / false: detached. |
| 348 | * |
| 349 | * Note that this is slower than extcon_set_cable_state_. |
| 350 | */ |
| 351 | int extcon_set_cable_state(struct extcon_dev *edev, |
| 352 | const char *cable_name, bool cable_state) |
| 353 | { |
| 354 | return extcon_set_cable_state_(edev, extcon_find_cable_index |
| 355 | (edev, cable_name), cable_state); |
| 356 | } |
| 357 | EXPORT_SYMBOL_GPL(extcon_set_cable_state); |
| 358 | |
| 359 | /** |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 360 | * extcon_get_extcon_dev() - Get the extcon device instance from the name |
| 361 | * @extcon_name: The extcon name provided with extcon_dev_register() |
| 362 | */ |
| 363 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) |
| 364 | { |
| 365 | struct extcon_dev *sd; |
| 366 | |
| 367 | mutex_lock(&extcon_dev_list_lock); |
| 368 | list_for_each_entry(sd, &extcon_dev_list, entry) { |
| 369 | if (!strcmp(sd->name, extcon_name)) |
| 370 | goto out; |
| 371 | } |
| 372 | sd = NULL; |
| 373 | out: |
| 374 | mutex_unlock(&extcon_dev_list_lock); |
| 375 | return sd; |
| 376 | } |
| 377 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); |
| 378 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 379 | static int _call_per_cable(struct notifier_block *nb, unsigned long val, |
| 380 | void *ptr) |
| 381 | { |
| 382 | struct extcon_specific_cable_nb *obj = container_of(nb, |
| 383 | struct extcon_specific_cable_nb, internal_nb); |
| 384 | struct extcon_dev *edev = ptr; |
| 385 | |
| 386 | if ((val & (1 << obj->cable_index)) != |
| 387 | (edev->state & (1 << obj->cable_index))) { |
| 388 | obj->previous_value = val; |
| 389 | return obj->user_nb->notifier_call(obj->user_nb, val, ptr); |
| 390 | } |
| 391 | |
| 392 | return NOTIFY_OK; |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * extcon_register_interest() - Register a notifier for a state change of a |
| 397 | * specific cable, not a entier set of cables of a |
| 398 | * extcon device. |
| 399 | * @obj: an empty extcon_specific_cable_nb object to be returned. |
| 400 | * @extcon_name: the name of extcon device. |
| 401 | * @cable_name: the target cable name. |
| 402 | * @nb: the notifier block to get notified. |
| 403 | * |
| 404 | * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets |
| 405 | * the struct for you. |
| 406 | * |
| 407 | * extcon_register_interest is a helper function for those who want to get |
| 408 | * notification for a single specific cable's status change. If a user wants |
| 409 | * to get notification for any changes of all cables of a extcon device, |
| 410 | * he/she should use the general extcon_register_notifier(). |
| 411 | * |
| 412 | * Note that the second parameter given to the callback of nb (val) is |
| 413 | * "old_state", not the current state. The current state can be retrieved |
| 414 | * by looking at the third pameter (edev pointer)'s state value. |
| 415 | */ |
| 416 | int extcon_register_interest(struct extcon_specific_cable_nb *obj, |
| 417 | const char *extcon_name, const char *cable_name, |
| 418 | struct notifier_block *nb) |
| 419 | { |
| 420 | if (!obj || !extcon_name || !cable_name || !nb) |
| 421 | return -EINVAL; |
| 422 | |
| 423 | obj->edev = extcon_get_extcon_dev(extcon_name); |
| 424 | if (!obj->edev) |
| 425 | return -ENODEV; |
| 426 | |
| 427 | obj->cable_index = extcon_find_cable_index(obj->edev, cable_name); |
| 428 | if (obj->cable_index < 0) |
| 429 | return -ENODEV; |
| 430 | |
| 431 | obj->user_nb = nb; |
| 432 | |
| 433 | obj->internal_nb.notifier_call = _call_per_cable; |
| 434 | |
| 435 | return raw_notifier_chain_register(&obj->edev->nh, &obj->internal_nb); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * extcon_unregister_interest() - Unregister the notifier registered by |
| 440 | * extcon_register_interest(). |
| 441 | * @obj: the extcon_specific_cable_nb object returned by |
| 442 | * extcon_register_interest(). |
| 443 | */ |
| 444 | int extcon_unregister_interest(struct extcon_specific_cable_nb *obj) |
| 445 | { |
| 446 | if (!obj) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb); |
| 450 | } |
| 451 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 452 | /** |
| 453 | * extcon_register_notifier() - Register a notifee to get notified by |
| 454 | * any attach status changes from the extcon. |
| 455 | * @edev: the extcon device. |
| 456 | * @nb: a notifier block to be registered. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 457 | * |
| 458 | * Note that the second parameter given to the callback of nb (val) is |
| 459 | * "old_state", not the current state. The current state can be retrieved |
| 460 | * by looking at the third pameter (edev pointer)'s state value. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 461 | */ |
| 462 | int extcon_register_notifier(struct extcon_dev *edev, |
| 463 | struct notifier_block *nb) |
| 464 | { |
| 465 | return raw_notifier_chain_register(&edev->nh, nb); |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(extcon_register_notifier); |
| 468 | |
| 469 | /** |
| 470 | * extcon_unregister_notifier() - Unregister a notifee from the extcon device. |
| 471 | * @edev: the extcon device. |
| 472 | * @nb: a registered notifier block to be unregistered. |
| 473 | */ |
| 474 | int extcon_unregister_notifier(struct extcon_dev *edev, |
| 475 | struct notifier_block *nb) |
| 476 | { |
| 477 | return raw_notifier_chain_unregister(&edev->nh, nb); |
| 478 | } |
| 479 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); |
| 480 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 481 | static struct device_attribute extcon_attrs[] = { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 482 | __ATTR(state, S_IRUGO | S_IWUSR, state_show, state_store), |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 483 | __ATTR_RO(name), |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 484 | __ATTR_NULL, |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 485 | }; |
| 486 | |
| 487 | static int create_extcon_class(void) |
| 488 | { |
| 489 | if (!extcon_class) { |
| 490 | extcon_class = class_create(THIS_MODULE, "extcon"); |
| 491 | if (IS_ERR(extcon_class)) |
| 492 | return PTR_ERR(extcon_class); |
| 493 | extcon_class->dev_attrs = extcon_attrs; |
| 494 | |
| 495 | #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH) |
| 496 | switch_class = class_compat_register("switch"); |
| 497 | if (WARN(!switch_class, "cannot allocate")) |
| 498 | return -ENOMEM; |
| 499 | #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */ |
| 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static void extcon_cleanup(struct extcon_dev *edev, bool skip) |
| 506 | { |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 507 | mutex_lock(&extcon_dev_list_lock); |
| 508 | list_del(&edev->entry); |
| 509 | mutex_unlock(&extcon_dev_list_lock); |
| 510 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 511 | if (!skip && get_device(edev->dev)) { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 512 | int index; |
| 513 | |
| 514 | for (index = 0; index < edev->max_supported; index++) |
| 515 | kfree(edev->cables[index].attr_g.name); |
| 516 | |
| 517 | if (edev->max_supported) { |
| 518 | kfree(edev->extcon_dev_type.groups); |
| 519 | kfree(edev->cables); |
| 520 | } |
| 521 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 522 | device_unregister(edev->dev); |
| 523 | put_device(edev->dev); |
| 524 | } |
| 525 | |
| 526 | kfree(edev->dev); |
| 527 | } |
| 528 | |
| 529 | static void extcon_dev_release(struct device *dev) |
| 530 | { |
| 531 | struct extcon_dev *edev = (struct extcon_dev *) dev_get_drvdata(dev); |
| 532 | |
| 533 | extcon_cleanup(edev, true); |
| 534 | } |
| 535 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 536 | static void dummy_sysfs_dev_release(struct device *dev) |
| 537 | { |
| 538 | } |
| 539 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 540 | /** |
| 541 | * extcon_dev_register() - Register a new extcon device |
| 542 | * @edev : the new extcon device (should be allocated before calling) |
| 543 | * @dev : the parent device for this extcon device. |
| 544 | * |
| 545 | * Among the members of edev struct, please set the "user initializing data" |
| 546 | * in any case and set the "optional callbacks" if required. However, please |
| 547 | * do not set the values of "internal data", which are initialized by |
| 548 | * this function. |
| 549 | */ |
| 550 | int extcon_dev_register(struct extcon_dev *edev, struct device *dev) |
| 551 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 552 | int ret, index = 0; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 553 | |
| 554 | if (!extcon_class) { |
| 555 | ret = create_extcon_class(); |
| 556 | if (ret < 0) |
| 557 | return ret; |
| 558 | } |
| 559 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 560 | if (edev->supported_cable) { |
| 561 | /* Get size of array */ |
| 562 | for (index = 0; edev->supported_cable[index]; index++) |
| 563 | ; |
| 564 | edev->max_supported = index; |
| 565 | } else { |
| 566 | edev->max_supported = 0; |
| 567 | } |
| 568 | |
| 569 | if (index > SUPPORTED_CABLE_MAX) { |
| 570 | dev_err(edev->dev, "extcon: maximum number of supported cables exceeded.\n"); |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 574 | edev->dev = kzalloc(sizeof(struct device), GFP_KERNEL); |
| 575 | edev->dev->parent = dev; |
| 576 | edev->dev->class = extcon_class; |
| 577 | edev->dev->release = extcon_dev_release; |
| 578 | |
| 579 | dev_set_name(edev->dev, edev->name ? edev->name : dev_name(dev)); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 580 | |
| 581 | if (edev->max_supported) { |
| 582 | char buf[10]; |
| 583 | char *str; |
| 584 | struct extcon_cable *cable; |
| 585 | |
| 586 | edev->cables = kzalloc(sizeof(struct extcon_cable) * |
| 587 | edev->max_supported, GFP_KERNEL); |
| 588 | if (!edev->cables) { |
| 589 | ret = -ENOMEM; |
| 590 | goto err_sysfs_alloc; |
| 591 | } |
| 592 | for (index = 0; index < edev->max_supported; index++) { |
| 593 | cable = &edev->cables[index]; |
| 594 | |
| 595 | snprintf(buf, 10, "cable.%d", index); |
| 596 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 597 | GFP_KERNEL); |
| 598 | if (!str) { |
| 599 | for (index--; index >= 0; index--) { |
| 600 | cable = &edev->cables[index]; |
| 601 | kfree(cable->attr_g.name); |
| 602 | } |
| 603 | ret = -ENOMEM; |
| 604 | |
| 605 | goto err_alloc_cables; |
| 606 | } |
| 607 | strcpy(str, buf); |
| 608 | |
| 609 | cable->edev = edev; |
| 610 | cable->cable_index = index; |
| 611 | cable->attrs[0] = &cable->attr_name.attr; |
| 612 | cable->attrs[1] = &cable->attr_state.attr; |
| 613 | cable->attrs[2] = NULL; |
| 614 | cable->attr_g.name = str; |
| 615 | cable->attr_g.attrs = cable->attrs; |
| 616 | |
| 617 | cable->attr_name.attr.name = "name"; |
| 618 | cable->attr_name.attr.mode = 0444; |
| 619 | cable->attr_name.show = cable_name_show; |
| 620 | |
| 621 | cable->attr_state.attr.name = "state"; |
| 622 | cable->attr_state.attr.mode = 0644; |
| 623 | cable->attr_state.show = cable_state_show; |
| 624 | cable->attr_state.store = cable_state_store; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | if (edev->max_supported) { |
| 629 | edev->extcon_dev_type.groups = |
| 630 | kzalloc(sizeof(struct attribute_group *) * |
| 631 | (edev->max_supported + 1), GFP_KERNEL); |
| 632 | if (!edev->extcon_dev_type.groups) { |
| 633 | ret = -ENOMEM; |
| 634 | goto err_alloc_groups; |
| 635 | } |
| 636 | |
| 637 | edev->extcon_dev_type.name = dev_name(edev->dev); |
| 638 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; |
| 639 | |
| 640 | for (index = 0; index < edev->max_supported; index++) |
| 641 | edev->extcon_dev_type.groups[index] = |
| 642 | &edev->cables[index].attr_g; |
| 643 | |
| 644 | edev->dev->type = &edev->extcon_dev_type; |
| 645 | } |
| 646 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 647 | ret = device_register(edev->dev); |
| 648 | if (ret) { |
| 649 | put_device(edev->dev); |
| 650 | goto err_dev; |
| 651 | } |
| 652 | #if defined(CONFIG_ANDROID) && !defined(CONFIG_ANDROID_SWITCH) |
| 653 | if (switch_class) |
| 654 | ret = class_compat_create_link(switch_class, edev->dev, |
| 655 | dev); |
| 656 | #endif /* CONFIG_ANDROID && !defined(CONFIG_ANDROID_SWITCH) */ |
| 657 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 658 | spin_lock_init(&edev->lock); |
| 659 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 660 | RAW_INIT_NOTIFIER_HEAD(&edev->nh); |
| 661 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 662 | dev_set_drvdata(edev->dev, edev); |
| 663 | edev->state = 0; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 664 | |
| 665 | mutex_lock(&extcon_dev_list_lock); |
| 666 | list_add(&edev->entry, &extcon_dev_list); |
| 667 | mutex_unlock(&extcon_dev_list_lock); |
| 668 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 669 | return 0; |
| 670 | |
| 671 | err_dev: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame^] | 672 | if (edev->max_supported) |
| 673 | kfree(edev->extcon_dev_type.groups); |
| 674 | err_alloc_groups: |
| 675 | for (index = 0; index < edev->max_supported; index++) |
| 676 | kfree(edev->cables[index].attr_g.name); |
| 677 | err_alloc_cables: |
| 678 | if (edev->max_supported) |
| 679 | kfree(edev->cables); |
| 680 | err_sysfs_alloc: |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 681 | kfree(edev->dev); |
| 682 | return ret; |
| 683 | } |
| 684 | EXPORT_SYMBOL_GPL(extcon_dev_register); |
| 685 | |
| 686 | /** |
| 687 | * extcon_dev_unregister() - Unregister the extcon device. |
| 688 | * @edev: the extcon device instance to be unregitered. |
| 689 | * |
| 690 | * Note that this does not call kfree(edev) because edev was not allocated |
| 691 | * by this class. |
| 692 | */ |
| 693 | void extcon_dev_unregister(struct extcon_dev *edev) |
| 694 | { |
| 695 | extcon_cleanup(edev, false); |
| 696 | } |
| 697 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); |
| 698 | |
| 699 | static int __init extcon_class_init(void) |
| 700 | { |
| 701 | return create_extcon_class(); |
| 702 | } |
| 703 | module_init(extcon_class_init); |
| 704 | |
| 705 | static void __exit extcon_class_exit(void) |
| 706 | { |
| 707 | class_destroy(extcon_class); |
| 708 | } |
| 709 | module_exit(extcon_class_exit); |
| 710 | |
| 711 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 712 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); |
| 713 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 714 | MODULE_DESCRIPTION("External connector (extcon) class driver"); |
| 715 | MODULE_LICENSE("GPL"); |