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