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