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> |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 33 | #include <linux/sysfs.h> |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 34 | #include <linux/of.h> |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 35 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 36 | /* |
| 37 | * extcon_cable_name suggests the standard cable names for commonly used |
| 38 | * cable types. |
| 39 | * |
| 40 | * However, please do not use extcon_cable_name directly for extcon_dev |
| 41 | * struct's supported_cable pointer unless your device really supports |
| 42 | * every single port-type of the following cable names. Please choose cable |
| 43 | * names that are actually used in your extcon device. |
| 44 | */ |
anish kumar | 0cf6ad8 | 2012-08-30 00:35:09 +0530 | [diff] [blame] | 45 | const char extcon_cable_name[][CABLE_NAME_MAX + 1] = { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 46 | [EXTCON_USB] = "USB", |
| 47 | [EXTCON_USB_HOST] = "USB-Host", |
| 48 | [EXTCON_TA] = "TA", |
| 49 | [EXTCON_FAST_CHARGER] = "Fast-charger", |
| 50 | [EXTCON_SLOW_CHARGER] = "Slow-charger", |
| 51 | [EXTCON_CHARGE_DOWNSTREAM] = "Charge-downstream", |
| 52 | [EXTCON_HDMI] = "HDMI", |
| 53 | [EXTCON_MHL] = "MHL", |
| 54 | [EXTCON_DVI] = "DVI", |
| 55 | [EXTCON_VGA] = "VGA", |
| 56 | [EXTCON_DOCK] = "Dock", |
| 57 | [EXTCON_LINE_IN] = "Line-in", |
| 58 | [EXTCON_LINE_OUT] = "Line-out", |
| 59 | [EXTCON_MIC_IN] = "Microphone", |
| 60 | [EXTCON_HEADPHONE_OUT] = "Headphone", |
| 61 | [EXTCON_SPDIF_IN] = "SPDIF-in", |
| 62 | [EXTCON_SPDIF_OUT] = "SPDIF-out", |
| 63 | [EXTCON_VIDEO_IN] = "Video-in", |
| 64 | [EXTCON_VIDEO_OUT] = "Video-out", |
Mark Brown | 0e1507c | 2012-05-02 10:38:51 +0100 | [diff] [blame] | 65 | [EXTCON_MECHANICAL] = "Mechanical", |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 66 | }; |
| 67 | |
Mark Brown | be3a07f | 2012-06-05 16:14:38 +0100 | [diff] [blame] | 68 | static struct class *extcon_class; |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 69 | #if defined(CONFIG_ANDROID) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 70 | static struct class_compat *switch_class; |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 71 | #endif /* CONFIG_ANDROID */ |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 72 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 73 | static LIST_HEAD(extcon_dev_list); |
| 74 | static DEFINE_MUTEX(extcon_dev_list_lock); |
| 75 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 76 | /** |
| 77 | * check_mutually_exclusive - Check if new_state violates mutually_exclusive |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 78 | * condition. |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 79 | * @edev: the extcon device |
| 80 | * @new_state: new cable attach status for @edev |
| 81 | * |
| 82 | * Returns 0 if nothing violates. Returns the index + 1 for the first |
| 83 | * violated condition. |
| 84 | */ |
| 85 | static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) |
| 86 | { |
| 87 | int i = 0; |
| 88 | |
| 89 | if (!edev->mutually_exclusive) |
| 90 | return 0; |
| 91 | |
| 92 | for (i = 0; edev->mutually_exclusive[i]; i++) { |
anish kumar | 28c0ada | 2012-08-30 00:35:10 +0530 | [diff] [blame] | 93 | int weight; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 94 | u32 correspondants = new_state & edev->mutually_exclusive[i]; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 95 | |
anish kumar | 28c0ada | 2012-08-30 00:35:10 +0530 | [diff] [blame] | 96 | /* calculate the total number of bits set */ |
| 97 | weight = hweight32(correspondants); |
| 98 | if (weight > 1) |
| 99 | return i + 1; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 105 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 106 | char *buf) |
| 107 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 108 | int i, count = 0; |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 109 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 110 | |
| 111 | if (edev->print_state) { |
| 112 | int ret = edev->print_state(edev, buf); |
| 113 | |
| 114 | if (ret >= 0) |
| 115 | return ret; |
| 116 | /* Use default if failed */ |
| 117 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 118 | |
| 119 | if (edev->max_supported == 0) |
| 120 | return sprintf(buf, "%u\n", edev->state); |
| 121 | |
| 122 | for (i = 0; i < SUPPORTED_CABLE_MAX; i++) { |
| 123 | if (!edev->supported_cable[i]) |
| 124 | break; |
| 125 | count += sprintf(buf + count, "%s=%d\n", |
| 126 | edev->supported_cable[i], |
| 127 | !!(edev->state & (1 << i))); |
| 128 | } |
| 129 | |
| 130 | return count; |
| 131 | } |
| 132 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 133 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 134 | const char *buf, size_t count) |
| 135 | { |
| 136 | u32 state; |
| 137 | ssize_t ret = 0; |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 138 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 139 | |
| 140 | ret = sscanf(buf, "0x%x", &state); |
| 141 | if (ret == 0) |
| 142 | ret = -EINVAL; |
| 143 | else |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 144 | ret = extcon_set_state(edev, state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 145 | |
| 146 | if (ret < 0) |
| 147 | return ret; |
| 148 | |
| 149 | return count; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 150 | } |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 151 | static DEVICE_ATTR_RW(state); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 152 | |
| 153 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 154 | char *buf) |
| 155 | { |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 156 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 157 | |
| 158 | /* Optional callback given by the user */ |
| 159 | if (edev->print_name) { |
| 160 | int ret = edev->print_name(edev, buf); |
| 161 | if (ret >= 0) |
| 162 | return ret; |
| 163 | } |
| 164 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 165 | return sprintf(buf, "%s\n", dev_name(&edev->dev)); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 166 | } |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 167 | static DEVICE_ATTR_RO(name); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 168 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 169 | static ssize_t cable_name_show(struct device *dev, |
| 170 | struct device_attribute *attr, char *buf) |
| 171 | { |
| 172 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 173 | attr_name); |
| 174 | |
| 175 | return sprintf(buf, "%s\n", |
| 176 | cable->edev->supported_cable[cable->cable_index]); |
| 177 | } |
| 178 | |
| 179 | static ssize_t cable_state_show(struct device *dev, |
| 180 | struct device_attribute *attr, char *buf) |
| 181 | { |
| 182 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 183 | attr_state); |
| 184 | |
| 185 | return sprintf(buf, "%d\n", |
| 186 | extcon_get_cable_state_(cable->edev, |
| 187 | cable->cable_index)); |
| 188 | } |
| 189 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 190 | /** |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 191 | * extcon_update_state() - Update the cable attach states of the extcon device |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 192 | * only for the masked bits. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 193 | * @edev: the extcon device |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 194 | * @mask: the bit mask to designate updated bits. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 195 | * @state: new cable attach status for @edev |
| 196 | * |
| 197 | * Changing the state sends uevent with environment variable containing |
| 198 | * the name of extcon device (envp[0]) and the state output (envp[1]). |
| 199 | * Tizen uses this format for extcon device to get events from ports. |
| 200 | * Android uses this format as well. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 201 | * |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 202 | * Note that the notifier provides which bits are changed in the state |
| 203 | * variable with the val parameter (second) to the callback. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 204 | */ |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 205 | int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 206 | { |
| 207 | char name_buf[120]; |
| 208 | char state_buf[120]; |
| 209 | char *prop_buf; |
| 210 | char *envp[3]; |
| 211 | int env_offset = 0; |
| 212 | int length; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 213 | unsigned long flags; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 214 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 215 | spin_lock_irqsave(&edev->lock, flags); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 216 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 217 | if (edev->state != ((edev->state & ~mask) | (state & mask))) { |
| 218 | u32 old_state = edev->state; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 219 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 220 | if (check_mutually_exclusive(edev, (edev->state & ~mask) | |
| 221 | (state & mask))) { |
| 222 | spin_unlock_irqrestore(&edev->lock, flags); |
| 223 | return -EPERM; |
| 224 | } |
| 225 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 226 | edev->state &= ~mask; |
| 227 | edev->state |= state & mask; |
| 228 | |
| 229 | raw_notifier_call_chain(&edev->nh, old_state, edev); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 230 | /* This could be in interrupt handler */ |
| 231 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 232 | if (prop_buf) { |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 233 | length = name_show(&edev->dev, NULL, prop_buf); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 234 | if (length > 0) { |
| 235 | if (prop_buf[length - 1] == '\n') |
| 236 | prop_buf[length - 1] = 0; |
| 237 | snprintf(name_buf, sizeof(name_buf), |
| 238 | "NAME=%s", prop_buf); |
| 239 | envp[env_offset++] = name_buf; |
| 240 | } |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 241 | length = state_show(&edev->dev, NULL, prop_buf); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 242 | if (length > 0) { |
| 243 | if (prop_buf[length - 1] == '\n') |
| 244 | prop_buf[length - 1] = 0; |
| 245 | snprintf(state_buf, sizeof(state_buf), |
| 246 | "STATE=%s", prop_buf); |
| 247 | envp[env_offset++] = state_buf; |
| 248 | } |
| 249 | envp[env_offset] = NULL; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 250 | /* Unlock early before uevent */ |
| 251 | spin_unlock_irqrestore(&edev->lock, flags); |
| 252 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 253 | kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 254 | free_page((unsigned long)prop_buf); |
| 255 | } else { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 256 | /* Unlock early before uevent */ |
| 257 | spin_unlock_irqrestore(&edev->lock, flags); |
| 258 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 259 | dev_err(&edev->dev, "out of memory in extcon_set_state\n"); |
| 260 | kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 261 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 262 | } else { |
| 263 | /* No changes */ |
| 264 | spin_unlock_irqrestore(&edev->lock, flags); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 265 | } |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 266 | |
| 267 | return 0; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 268 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 269 | EXPORT_SYMBOL_GPL(extcon_update_state); |
| 270 | |
| 271 | /** |
| 272 | * extcon_set_state() - Set the cable attach states of the extcon device. |
| 273 | * @edev: the extcon device |
| 274 | * @state: new cable attach status for @edev |
| 275 | * |
| 276 | * Note that notifier provides which bits are changed in the state |
| 277 | * variable with the val parameter (second) to the callback. |
| 278 | */ |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 279 | int extcon_set_state(struct extcon_dev *edev, u32 state) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 280 | { |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 281 | return extcon_update_state(edev, 0xffffffff, state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 282 | } |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 283 | EXPORT_SYMBOL_GPL(extcon_set_state); |
| 284 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 285 | /** |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 286 | * extcon_find_cable_index() - Get the cable index based on the cable name. |
| 287 | * @edev: the extcon device that has the cable. |
| 288 | * @cable_name: cable name to be searched. |
| 289 | * |
| 290 | * Note that accessing a cable state based on cable_index is faster than |
| 291 | * cable_name because using cable_name induces a loop with strncmp(). |
| 292 | * Thus, when get/set_cable_state is repeatedly used, using cable_index |
| 293 | * is recommended. |
| 294 | */ |
| 295 | int extcon_find_cable_index(struct extcon_dev *edev, const char *cable_name) |
| 296 | { |
| 297 | int i; |
| 298 | |
| 299 | if (edev->supported_cable) { |
| 300 | for (i = 0; edev->supported_cable[i]; i++) { |
| 301 | if (!strncmp(edev->supported_cable[i], |
| 302 | cable_name, CABLE_NAME_MAX)) |
| 303 | return i; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(extcon_find_cable_index); |
| 310 | |
| 311 | /** |
| 312 | * extcon_get_cable_state_() - Get the status of a specific cable. |
| 313 | * @edev: the extcon device that has the cable. |
| 314 | * @index: cable index that can be retrieved by extcon_find_cable_index(). |
| 315 | */ |
| 316 | int extcon_get_cable_state_(struct extcon_dev *edev, int index) |
| 317 | { |
| 318 | if (index < 0 || (edev->max_supported && edev->max_supported <= index)) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | return !!(edev->state & (1 << index)); |
| 322 | } |
| 323 | EXPORT_SYMBOL_GPL(extcon_get_cable_state_); |
| 324 | |
| 325 | /** |
| 326 | * extcon_get_cable_state() - Get the status of a specific cable. |
| 327 | * @edev: the extcon device that has the cable. |
| 328 | * @cable_name: cable name. |
| 329 | * |
| 330 | * Note that this is slower than extcon_get_cable_state_. |
| 331 | */ |
| 332 | int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name) |
| 333 | { |
| 334 | return extcon_get_cable_state_(edev, extcon_find_cable_index |
| 335 | (edev, cable_name)); |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(extcon_get_cable_state); |
| 338 | |
| 339 | /** |
Axel Lin | 909f9ec | 2012-10-04 09:53:45 +0900 | [diff] [blame] | 340 | * extcon_set_cable_state_() - Set the status of a specific cable. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 341 | * @edev: the extcon device that has the cable. |
| 342 | * @index: cable index that can be retrieved by |
| 343 | * extcon_find_cable_index(). |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 344 | * @cable_state: the new cable status. The default semantics is |
| 345 | * true: attached / false: detached. |
| 346 | */ |
| 347 | int extcon_set_cable_state_(struct extcon_dev *edev, |
| 348 | int index, bool cable_state) |
| 349 | { |
| 350 | u32 state; |
| 351 | |
| 352 | if (index < 0 || (edev->max_supported && edev->max_supported <= index)) |
| 353 | return -EINVAL; |
| 354 | |
| 355 | state = cable_state ? (1 << index) : 0; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 356 | return extcon_update_state(edev, 1 << index, state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 357 | } |
| 358 | EXPORT_SYMBOL_GPL(extcon_set_cable_state_); |
| 359 | |
| 360 | /** |
Axel Lin | 909f9ec | 2012-10-04 09:53:45 +0900 | [diff] [blame] | 361 | * extcon_set_cable_state() - Set the status of a specific cable. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 362 | * @edev: the extcon device that has the cable. |
| 363 | * @cable_name: cable name. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 364 | * @cable_state: the new cable status. The default semantics is |
| 365 | * true: attached / false: detached. |
| 366 | * |
| 367 | * Note that this is slower than extcon_set_cable_state_. |
| 368 | */ |
| 369 | int extcon_set_cable_state(struct extcon_dev *edev, |
| 370 | const char *cable_name, bool cable_state) |
| 371 | { |
| 372 | return extcon_set_cable_state_(edev, extcon_find_cable_index |
| 373 | (edev, cable_name), cable_state); |
| 374 | } |
| 375 | EXPORT_SYMBOL_GPL(extcon_set_cable_state); |
| 376 | |
| 377 | /** |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 378 | * extcon_get_extcon_dev() - Get the extcon device instance from the name |
| 379 | * @extcon_name: The extcon name provided with extcon_dev_register() |
| 380 | */ |
| 381 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) |
| 382 | { |
| 383 | struct extcon_dev *sd; |
| 384 | |
| 385 | mutex_lock(&extcon_dev_list_lock); |
| 386 | list_for_each_entry(sd, &extcon_dev_list, entry) { |
| 387 | if (!strcmp(sd->name, extcon_name)) |
| 388 | goto out; |
| 389 | } |
| 390 | sd = NULL; |
| 391 | out: |
| 392 | mutex_unlock(&extcon_dev_list_lock); |
| 393 | return sd; |
| 394 | } |
| 395 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); |
| 396 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 397 | static int _call_per_cable(struct notifier_block *nb, unsigned long val, |
| 398 | void *ptr) |
| 399 | { |
| 400 | struct extcon_specific_cable_nb *obj = container_of(nb, |
| 401 | struct extcon_specific_cable_nb, internal_nb); |
| 402 | struct extcon_dev *edev = ptr; |
| 403 | |
| 404 | if ((val & (1 << obj->cable_index)) != |
| 405 | (edev->state & (1 << obj->cable_index))) { |
Chanwoo Choi | f4cce69 | 2012-04-27 15:17:28 +0900 | [diff] [blame] | 406 | bool cable_state = true; |
| 407 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 408 | obj->previous_value = val; |
Chanwoo Choi | f4cce69 | 2012-04-27 15:17:28 +0900 | [diff] [blame] | 409 | |
| 410 | if (val & (1 << obj->cable_index)) |
| 411 | cable_state = false; |
| 412 | |
| 413 | return obj->user_nb->notifier_call(obj->user_nb, |
| 414 | cable_state, ptr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | return NOTIFY_OK; |
| 418 | } |
| 419 | |
| 420 | /** |
| 421 | * extcon_register_interest() - Register a notifier for a state change of a |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 422 | * specific cable, not an entier set of cables of a |
| 423 | * extcon device. |
| 424 | * @obj: an empty extcon_specific_cable_nb object to be returned. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 425 | * @extcon_name: the name of extcon device. |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 426 | * if NULL, extcon_register_interest will register |
| 427 | * every cable with the target cable_name given. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 428 | * @cable_name: the target cable name. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 429 | * @nb: the notifier block to get notified. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 430 | * |
| 431 | * Provide an empty extcon_specific_cable_nb. extcon_register_interest() sets |
| 432 | * the struct for you. |
| 433 | * |
| 434 | * extcon_register_interest is a helper function for those who want to get |
| 435 | * notification for a single specific cable's status change. If a user wants |
| 436 | * to get notification for any changes of all cables of a extcon device, |
| 437 | * he/she should use the general extcon_register_notifier(). |
| 438 | * |
| 439 | * Note that the second parameter given to the callback of nb (val) is |
| 440 | * "old_state", not the current state. The current state can be retrieved |
| 441 | * by looking at the third pameter (edev pointer)'s state value. |
| 442 | */ |
| 443 | int extcon_register_interest(struct extcon_specific_cable_nb *obj, |
| 444 | const char *extcon_name, const char *cable_name, |
| 445 | struct notifier_block *nb) |
| 446 | { |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 447 | if (!obj || !cable_name || !nb) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 448 | return -EINVAL; |
| 449 | |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 450 | if (extcon_name) { |
| 451 | obj->edev = extcon_get_extcon_dev(extcon_name); |
| 452 | if (!obj->edev) |
| 453 | return -ENODEV; |
| 454 | |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 455 | obj->cable_index = extcon_find_cable_index(obj->edev, |
| 456 | cable_name); |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 457 | if (obj->cable_index < 0) |
Sachin Kamat | c0c078c | 2012-11-20 16:30:31 +0900 | [diff] [blame] | 458 | return obj->cable_index; |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 459 | |
| 460 | obj->user_nb = nb; |
| 461 | |
| 462 | obj->internal_nb.notifier_call = _call_per_cable; |
| 463 | |
Chanwoo Choi | c2275d2 | 2013-08-23 10:21:37 +0900 | [diff] [blame] | 464 | return raw_notifier_chain_register(&obj->edev->nh, |
| 465 | &obj->internal_nb); |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 466 | } else { |
| 467 | struct class_dev_iter iter; |
| 468 | struct extcon_dev *extd; |
| 469 | struct device *dev; |
| 470 | |
| 471 | if (!extcon_class) |
| 472 | return -ENODEV; |
| 473 | class_dev_iter_init(&iter, extcon_class, NULL, NULL); |
| 474 | while ((dev = class_dev_iter_next(&iter))) { |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 475 | extd = dev_get_drvdata(dev); |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 476 | |
| 477 | if (extcon_find_cable_index(extd, cable_name) < 0) |
| 478 | continue; |
| 479 | |
| 480 | class_dev_iter_exit(&iter); |
| 481 | return extcon_register_interest(obj, extd->name, |
| 482 | cable_name, nb); |
| 483 | } |
| 484 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 485 | return -ENODEV; |
Jenny TC | 4f2de3b | 2012-10-18 21:00:32 +0900 | [diff] [blame] | 486 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 487 | } |
Kishon Vijay Abraham I | 9c8a013 | 2013-06-04 01:13:38 +0900 | [diff] [blame] | 488 | EXPORT_SYMBOL_GPL(extcon_register_interest); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 489 | |
| 490 | /** |
| 491 | * extcon_unregister_interest() - Unregister the notifier registered by |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 492 | * extcon_register_interest(). |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 493 | * @obj: the extcon_specific_cable_nb object returned by |
| 494 | * extcon_register_interest(). |
| 495 | */ |
| 496 | int extcon_unregister_interest(struct extcon_specific_cable_nb *obj) |
| 497 | { |
| 498 | if (!obj) |
| 499 | return -EINVAL; |
| 500 | |
| 501 | return raw_notifier_chain_unregister(&obj->edev->nh, &obj->internal_nb); |
| 502 | } |
Kishon Vijay Abraham I | 9c8a013 | 2013-06-04 01:13:38 +0900 | [diff] [blame] | 503 | EXPORT_SYMBOL_GPL(extcon_unregister_interest); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 504 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 505 | /** |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 506 | * extcon_register_notifier() - Register a notifiee to get notified by |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 507 | * any attach status changes from the extcon. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 508 | * @edev: the extcon device. |
| 509 | * @nb: a notifier block to be registered. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 510 | * |
| 511 | * Note that the second parameter given to the callback of nb (val) is |
| 512 | * "old_state", not the current state. The current state can be retrieved |
| 513 | * by looking at the third pameter (edev pointer)'s state value. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 514 | */ |
| 515 | int extcon_register_notifier(struct extcon_dev *edev, |
| 516 | struct notifier_block *nb) |
| 517 | { |
| 518 | return raw_notifier_chain_register(&edev->nh, nb); |
| 519 | } |
| 520 | EXPORT_SYMBOL_GPL(extcon_register_notifier); |
| 521 | |
| 522 | /** |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 523 | * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 524 | * @edev: the extcon device. |
| 525 | * @nb: a registered notifier block to be unregistered. |
| 526 | */ |
| 527 | int extcon_unregister_notifier(struct extcon_dev *edev, |
| 528 | struct notifier_block *nb) |
| 529 | { |
| 530 | return raw_notifier_chain_unregister(&edev->nh, nb); |
| 531 | } |
| 532 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); |
| 533 | |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 534 | static struct attribute *extcon_attrs[] = { |
| 535 | &dev_attr_state.attr, |
| 536 | &dev_attr_name.attr, |
| 537 | NULL, |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 538 | }; |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 539 | ATTRIBUTE_GROUPS(extcon); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 540 | |
| 541 | static int create_extcon_class(void) |
| 542 | { |
| 543 | if (!extcon_class) { |
| 544 | extcon_class = class_create(THIS_MODULE, "extcon"); |
| 545 | if (IS_ERR(extcon_class)) |
| 546 | return PTR_ERR(extcon_class); |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 547 | extcon_class->dev_groups = extcon_groups; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 548 | |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 549 | #if defined(CONFIG_ANDROID) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 550 | switch_class = class_compat_register("switch"); |
| 551 | if (WARN(!switch_class, "cannot allocate")) |
| 552 | return -ENOMEM; |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 553 | #endif /* CONFIG_ANDROID */ |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 559 | static void extcon_dev_release(struct device *dev) |
| 560 | { |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 561 | } |
| 562 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 563 | static const char *muex_name = "mutually_exclusive"; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 564 | static void dummy_sysfs_dev_release(struct device *dev) |
| 565 | { |
| 566 | } |
| 567 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 568 | /** |
| 569 | * extcon_dev_register() - Register a new extcon device |
| 570 | * @edev : the new extcon device (should be allocated before calling) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 571 | * |
| 572 | * Among the members of edev struct, please set the "user initializing data" |
| 573 | * in any case and set the "optional callbacks" if required. However, please |
| 574 | * do not set the values of "internal data", which are initialized by |
| 575 | * this function. |
| 576 | */ |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 577 | int extcon_dev_register(struct extcon_dev *edev) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 578 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 579 | int ret, index = 0; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 580 | |
| 581 | if (!extcon_class) { |
| 582 | ret = create_extcon_class(); |
| 583 | if (ret < 0) |
| 584 | return ret; |
| 585 | } |
| 586 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 587 | if (edev->supported_cable) { |
| 588 | /* Get size of array */ |
| 589 | for (index = 0; edev->supported_cable[index]; index++) |
| 590 | ; |
| 591 | edev->max_supported = index; |
| 592 | } else { |
| 593 | edev->max_supported = 0; |
| 594 | } |
| 595 | |
| 596 | if (index > SUPPORTED_CABLE_MAX) { |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 597 | dev_err(&edev->dev, "extcon: maximum number of supported cables exceeded.\n"); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 601 | edev->dev.class = extcon_class; |
| 602 | edev->dev.release = extcon_dev_release; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 603 | |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 604 | edev->name = edev->name ? edev->name : dev_name(edev->dev.parent); |
| 605 | if (IS_ERR_OR_NULL(edev->name)) { |
| 606 | dev_err(&edev->dev, |
| 607 | "extcon device name is null\n"); |
| 608 | return -EINVAL; |
| 609 | } |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 610 | dev_set_name(&edev->dev, "%s", edev->name); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 611 | |
| 612 | if (edev->max_supported) { |
| 613 | char buf[10]; |
| 614 | char *str; |
| 615 | struct extcon_cable *cable; |
| 616 | |
| 617 | edev->cables = kzalloc(sizeof(struct extcon_cable) * |
| 618 | edev->max_supported, GFP_KERNEL); |
| 619 | if (!edev->cables) { |
| 620 | ret = -ENOMEM; |
| 621 | goto err_sysfs_alloc; |
| 622 | } |
| 623 | for (index = 0; index < edev->max_supported; index++) { |
| 624 | cable = &edev->cables[index]; |
| 625 | |
| 626 | snprintf(buf, 10, "cable.%d", index); |
| 627 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 628 | GFP_KERNEL); |
| 629 | if (!str) { |
| 630 | for (index--; index >= 0; index--) { |
| 631 | cable = &edev->cables[index]; |
| 632 | kfree(cable->attr_g.name); |
| 633 | } |
| 634 | ret = -ENOMEM; |
| 635 | |
| 636 | goto err_alloc_cables; |
| 637 | } |
| 638 | strcpy(str, buf); |
| 639 | |
| 640 | cable->edev = edev; |
| 641 | cable->cable_index = index; |
| 642 | cable->attrs[0] = &cable->attr_name.attr; |
| 643 | cable->attrs[1] = &cable->attr_state.attr; |
| 644 | cable->attrs[2] = NULL; |
| 645 | cable->attr_g.name = str; |
| 646 | cable->attr_g.attrs = cable->attrs; |
| 647 | |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 648 | sysfs_attr_init(&cable->attr_name.attr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 649 | cable->attr_name.attr.name = "name"; |
| 650 | cable->attr_name.attr.mode = 0444; |
| 651 | cable->attr_name.show = cable_name_show; |
| 652 | |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 653 | sysfs_attr_init(&cable->attr_state.attr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 654 | cable->attr_state.attr.name = "state"; |
Chanwoo Choi | ea9dd9d | 2013-05-22 19:31:59 +0900 | [diff] [blame] | 655 | cable->attr_state.attr.mode = 0444; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 656 | cable->attr_state.show = cable_state_show; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 660 | if (edev->max_supported && edev->mutually_exclusive) { |
| 661 | char buf[80]; |
| 662 | char *name; |
| 663 | |
| 664 | /* Count the size of mutually_exclusive array */ |
| 665 | for (index = 0; edev->mutually_exclusive[index]; index++) |
| 666 | ; |
| 667 | |
| 668 | edev->attrs_muex = kzalloc(sizeof(struct attribute *) * |
| 669 | (index + 1), GFP_KERNEL); |
| 670 | if (!edev->attrs_muex) { |
| 671 | ret = -ENOMEM; |
| 672 | goto err_muex; |
| 673 | } |
| 674 | |
| 675 | edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * |
| 676 | index, GFP_KERNEL); |
| 677 | if (!edev->d_attrs_muex) { |
| 678 | ret = -ENOMEM; |
| 679 | kfree(edev->attrs_muex); |
| 680 | goto err_muex; |
| 681 | } |
| 682 | |
| 683 | for (index = 0; edev->mutually_exclusive[index]; index++) { |
| 684 | sprintf(buf, "0x%x", edev->mutually_exclusive[index]); |
| 685 | name = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 686 | GFP_KERNEL); |
| 687 | if (!name) { |
| 688 | for (index--; index >= 0; index--) { |
| 689 | kfree(edev->d_attrs_muex[index].attr. |
| 690 | name); |
| 691 | } |
| 692 | kfree(edev->d_attrs_muex); |
| 693 | kfree(edev->attrs_muex); |
| 694 | ret = -ENOMEM; |
| 695 | goto err_muex; |
| 696 | } |
| 697 | strcpy(name, buf); |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 698 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 699 | edev->d_attrs_muex[index].attr.name = name; |
| 700 | edev->d_attrs_muex[index].attr.mode = 0000; |
| 701 | edev->attrs_muex[index] = &edev->d_attrs_muex[index] |
| 702 | .attr; |
| 703 | } |
| 704 | edev->attr_g_muex.name = muex_name; |
| 705 | edev->attr_g_muex.attrs = edev->attrs_muex; |
| 706 | |
| 707 | } |
| 708 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 709 | if (edev->max_supported) { |
| 710 | edev->extcon_dev_type.groups = |
| 711 | kzalloc(sizeof(struct attribute_group *) * |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 712 | (edev->max_supported + 2), GFP_KERNEL); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 713 | if (!edev->extcon_dev_type.groups) { |
| 714 | ret = -ENOMEM; |
| 715 | goto err_alloc_groups; |
| 716 | } |
| 717 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 718 | edev->extcon_dev_type.name = dev_name(&edev->dev); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 719 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; |
| 720 | |
| 721 | for (index = 0; index < edev->max_supported; index++) |
| 722 | edev->extcon_dev_type.groups[index] = |
| 723 | &edev->cables[index].attr_g; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 724 | if (edev->mutually_exclusive) |
| 725 | edev->extcon_dev_type.groups[index] = |
| 726 | &edev->attr_g_muex; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 727 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 728 | edev->dev.type = &edev->extcon_dev_type; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 729 | } |
| 730 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 731 | ret = device_register(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 732 | if (ret) { |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 733 | put_device(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 734 | goto err_dev; |
| 735 | } |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 736 | #if defined(CONFIG_ANDROID) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 737 | if (switch_class) |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 738 | ret = class_compat_create_link(switch_class, &edev->dev, NULL); |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 739 | #endif /* CONFIG_ANDROID */ |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 740 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 741 | spin_lock_init(&edev->lock); |
| 742 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 743 | RAW_INIT_NOTIFIER_HEAD(&edev->nh); |
| 744 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 745 | dev_set_drvdata(&edev->dev, edev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 746 | edev->state = 0; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 747 | |
| 748 | mutex_lock(&extcon_dev_list_lock); |
| 749 | list_add(&edev->entry, &extcon_dev_list); |
| 750 | mutex_unlock(&extcon_dev_list_lock); |
| 751 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 752 | return 0; |
| 753 | |
| 754 | err_dev: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 755 | if (edev->max_supported) |
| 756 | kfree(edev->extcon_dev_type.groups); |
| 757 | err_alloc_groups: |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 758 | if (edev->max_supported && edev->mutually_exclusive) { |
| 759 | for (index = 0; edev->mutually_exclusive[index]; index++) |
| 760 | kfree(edev->d_attrs_muex[index].attr.name); |
| 761 | kfree(edev->d_attrs_muex); |
| 762 | kfree(edev->attrs_muex); |
| 763 | } |
| 764 | err_muex: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 765 | for (index = 0; index < edev->max_supported; index++) |
| 766 | kfree(edev->cables[index].attr_g.name); |
| 767 | err_alloc_cables: |
| 768 | if (edev->max_supported) |
| 769 | kfree(edev->cables); |
| 770 | err_sysfs_alloc: |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 771 | return ret; |
| 772 | } |
| 773 | EXPORT_SYMBOL_GPL(extcon_dev_register); |
| 774 | |
| 775 | /** |
| 776 | * extcon_dev_unregister() - Unregister the extcon device. |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 777 | * @edev: the extcon device instance to be unregistered. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 778 | * |
| 779 | * Note that this does not call kfree(edev) because edev was not allocated |
| 780 | * by this class. |
| 781 | */ |
| 782 | void extcon_dev_unregister(struct extcon_dev *edev) |
| 783 | { |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 784 | int index; |
| 785 | |
| 786 | mutex_lock(&extcon_dev_list_lock); |
| 787 | list_del(&edev->entry); |
| 788 | mutex_unlock(&extcon_dev_list_lock); |
| 789 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 790 | if (IS_ERR_OR_NULL(get_device(&edev->dev))) { |
| 791 | dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", |
| 792 | dev_name(&edev->dev)); |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 793 | return; |
| 794 | } |
| 795 | |
Wang, Xiaoming | 7585ca0 | 2013-11-01 18:48:14 -0400 | [diff] [blame] | 796 | device_unregister(&edev->dev); |
| 797 | |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 798 | if (edev->mutually_exclusive && edev->max_supported) { |
| 799 | for (index = 0; edev->mutually_exclusive[index]; |
| 800 | index++) |
| 801 | kfree(edev->d_attrs_muex[index].attr.name); |
| 802 | kfree(edev->d_attrs_muex); |
| 803 | kfree(edev->attrs_muex); |
| 804 | } |
| 805 | |
| 806 | for (index = 0; index < edev->max_supported; index++) |
| 807 | kfree(edev->cables[index].attr_g.name); |
| 808 | |
| 809 | if (edev->max_supported) { |
| 810 | kfree(edev->extcon_dev_type.groups); |
| 811 | kfree(edev->cables); |
| 812 | } |
| 813 | |
| 814 | #if defined(CONFIG_ANDROID) |
| 815 | if (switch_class) |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 816 | class_compat_remove_link(switch_class, &edev->dev, NULL); |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 817 | #endif |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 818 | put_device(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 819 | } |
| 820 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); |
| 821 | |
Sangjung Woo | 1111244 | 2014-04-21 19:10:08 +0900 | [diff] [blame] | 822 | static void devm_extcon_dev_unreg(struct device *dev, void *res) |
| 823 | { |
| 824 | extcon_dev_unregister(*(struct extcon_dev **)res); |
| 825 | } |
| 826 | |
| 827 | static int devm_extcon_dev_match(struct device *dev, void *res, void *data) |
| 828 | { |
| 829 | struct extcon_dev **r = res; |
| 830 | |
| 831 | if (!r || !*r) { |
| 832 | WARN_ON(!r || !*r); |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | return *r == data; |
| 837 | } |
| 838 | |
| 839 | /** |
| 840 | * devm_extcon_dev_register() - Resource-managed extcon_dev_register() |
| 841 | * @dev: device to allocate extcon device |
| 842 | * @edev: the new extcon device to register |
| 843 | * |
| 844 | * Managed extcon_dev_register() function. If extcon device is attached with |
| 845 | * this function, that extcon device is automatically unregistered on driver |
| 846 | * detach. Internally this function calls extcon_dev_register() function. |
| 847 | * To get more information, refer that function. |
| 848 | * |
| 849 | * If extcon device is registered with this function and the device needs to be |
| 850 | * unregistered separately, devm_extcon_dev_unregister() should be used. |
| 851 | * |
| 852 | * Returns 0 if success or negaive error number if failure. |
| 853 | */ |
| 854 | int devm_extcon_dev_register(struct device *dev, struct extcon_dev *edev) |
| 855 | { |
| 856 | struct extcon_dev **ptr; |
| 857 | int ret; |
| 858 | |
| 859 | ptr = devres_alloc(devm_extcon_dev_unreg, sizeof(*ptr), GFP_KERNEL); |
| 860 | if (!ptr) |
| 861 | return -ENOMEM; |
| 862 | |
| 863 | ret = extcon_dev_register(edev); |
| 864 | if (ret) { |
| 865 | devres_free(ptr); |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | *ptr = edev; |
| 870 | devres_add(dev, ptr); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | EXPORT_SYMBOL_GPL(devm_extcon_dev_register); |
| 875 | |
| 876 | /** |
| 877 | * devm_extcon_dev_unregister() - Resource-managed extcon_dev_unregister() |
| 878 | * @dev: device the extcon belongs to |
| 879 | * @edev: the extcon device to unregister |
| 880 | * |
| 881 | * Unregister extcon device that is registered with devm_extcon_dev_register() |
| 882 | * function. |
| 883 | */ |
| 884 | void devm_extcon_dev_unregister(struct device *dev, struct extcon_dev *edev) |
| 885 | { |
| 886 | WARN_ON(devres_release(dev, devm_extcon_dev_unreg, |
| 887 | devm_extcon_dev_match, edev)); |
| 888 | } |
| 889 | EXPORT_SYMBOL_GPL(devm_extcon_dev_unregister); |
| 890 | |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 891 | #ifdef CONFIG_OF |
| 892 | /* |
| 893 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree |
| 894 | * @dev - instance to the given device |
| 895 | * @index - index into list of extcon_dev |
| 896 | * |
| 897 | * return the instance of extcon device |
| 898 | */ |
| 899 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) |
| 900 | { |
| 901 | struct device_node *node; |
| 902 | struct extcon_dev *edev; |
| 903 | |
| 904 | if (!dev->of_node) { |
| 905 | dev_err(dev, "device does not have a device node entry\n"); |
| 906 | return ERR_PTR(-EINVAL); |
| 907 | } |
| 908 | |
| 909 | node = of_parse_phandle(dev->of_node, "extcon", index); |
| 910 | if (!node) { |
| 911 | dev_err(dev, "failed to get phandle in %s node\n", |
| 912 | dev->of_node->full_name); |
| 913 | return ERR_PTR(-ENODEV); |
| 914 | } |
| 915 | |
| 916 | edev = extcon_get_extcon_dev(node->name); |
| 917 | if (!edev) { |
| 918 | dev_err(dev, "unable to get extcon device : %s\n", node->name); |
| 919 | return ERR_PTR(-ENODEV); |
| 920 | } |
| 921 | |
| 922 | return edev; |
| 923 | } |
| 924 | #else |
| 925 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) |
| 926 | { |
| 927 | return ERR_PTR(-ENOSYS); |
| 928 | } |
| 929 | #endif /* CONFIG_OF */ |
| 930 | EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); |
| 931 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 932 | static int __init extcon_class_init(void) |
| 933 | { |
| 934 | return create_extcon_class(); |
| 935 | } |
| 936 | module_init(extcon_class_init); |
| 937 | |
| 938 | static void __exit extcon_class_exit(void) |
| 939 | { |
Peter Huewe | 0dc77b6 | 2012-09-24 15:32:31 +0900 | [diff] [blame] | 940 | #if defined(CONFIG_ANDROID) |
| 941 | class_compat_unregister(switch_class); |
| 942 | #endif |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 943 | class_destroy(extcon_class); |
| 944 | } |
| 945 | module_exit(extcon_class_exit); |
| 946 | |
| 947 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 948 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); |
| 949 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 950 | MODULE_DESCRIPTION("External connector (extcon) class driver"); |
| 951 | MODULE_LICENSE("GPL"); |