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