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