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 | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 42 | [EXTCON_NONE] = "NONE", |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 43 | |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 44 | /* USB external connector */ |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 45 | [EXTCON_USB] = "USB", |
| 46 | [EXTCON_USB_HOST] = "USB-HOST", |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 47 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 48 | /* Charging external connector */ |
| 49 | [EXTCON_CHG_USB_SDP] = "SDP", |
| 50 | [EXTCON_CHG_USB_DCP] = "DCP", |
| 51 | [EXTCON_CHG_USB_CDP] = "CDP", |
| 52 | [EXTCON_CHG_USB_ACA] = "ACA", |
| 53 | [EXTCON_CHG_USB_FAST] = "FAST-CHARGER", |
| 54 | [EXTCON_CHG_USB_SLOW] = "SLOW-CHARGER", |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 55 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 56 | /* Jack external connector */ |
| 57 | [EXTCON_JACK_MICROPHONE] = "MICROPHONE", |
| 58 | [EXTCON_JACK_HEADPHONE] = "HEADPHONE", |
| 59 | [EXTCON_JACK_LINE_IN] = "LINE-IN", |
| 60 | [EXTCON_JACK_LINE_OUT] = "LINE-OUT", |
| 61 | [EXTCON_JACK_VIDEO_IN] = "VIDEO-IN", |
| 62 | [EXTCON_JACK_VIDEO_OUT] = "VIDEO-OUT", |
| 63 | [EXTCON_JACK_SPDIF_IN] = "SPDIF-IN", |
| 64 | [EXTCON_JACK_SPDIF_OUT] = "SPDIF-OUT", |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 65 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 66 | /* Display external connector */ |
| 67 | [EXTCON_DISP_HDMI] = "HDMI", |
| 68 | [EXTCON_DISP_MHL] = "MHL", |
| 69 | [EXTCON_DISP_DVI] = "DVI", |
| 70 | [EXTCON_DISP_VGA] = "VGA", |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 71 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 72 | /* Miscellaneous external connector */ |
| 73 | [EXTCON_DOCK] = "DOCK", |
| 74 | [EXTCON_JIG] = "JIG", |
| 75 | [EXTCON_MECHANICAL] = "MECHANICAL", |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 76 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 77 | NULL, |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 78 | }; |
| 79 | |
Chanwoo Choi | 20f7b53 | 2016-06-27 19:17:06 +0900 | [diff] [blame] | 80 | /** |
| 81 | * struct extcon_cable - An internal data for each cable of extcon device. |
| 82 | * @edev: The extcon device |
| 83 | * @cable_index: Index of this cable in the edev |
| 84 | * @attr_g: Attribute group for the cable |
| 85 | * @attr_name: "name" sysfs entry |
| 86 | * @attr_state: "state" sysfs entry |
| 87 | * @attrs: Array pointing to attr_name and attr_state for attr_g |
| 88 | */ |
| 89 | struct extcon_cable { |
| 90 | struct extcon_dev *edev; |
| 91 | int cable_index; |
| 92 | |
| 93 | struct attribute_group attr_g; |
| 94 | struct device_attribute attr_name; |
| 95 | struct device_attribute attr_state; |
| 96 | |
| 97 | struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ |
| 98 | }; |
| 99 | |
Mark Brown | be3a07f | 2012-06-05 16:14:38 +0100 | [diff] [blame] | 100 | static struct class *extcon_class; |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 101 | #if defined(CONFIG_ANDROID) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 102 | static struct class_compat *switch_class; |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 103 | #endif /* CONFIG_ANDROID */ |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 104 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 105 | static LIST_HEAD(extcon_dev_list); |
| 106 | static DEFINE_MUTEX(extcon_dev_list_lock); |
| 107 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 108 | /** |
| 109 | * check_mutually_exclusive - Check if new_state violates mutually_exclusive |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 110 | * condition. |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 111 | * @edev: the extcon device |
| 112 | * @new_state: new cable attach status for @edev |
| 113 | * |
| 114 | * Returns 0 if nothing violates. Returns the index + 1 for the first |
| 115 | * violated condition. |
| 116 | */ |
| 117 | static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) |
| 118 | { |
| 119 | int i = 0; |
| 120 | |
| 121 | if (!edev->mutually_exclusive) |
| 122 | return 0; |
| 123 | |
| 124 | for (i = 0; edev->mutually_exclusive[i]; i++) { |
anish kumar | 28c0ada | 2012-08-30 00:35:10 +0530 | [diff] [blame] | 125 | int weight; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 126 | u32 correspondants = new_state & edev->mutually_exclusive[i]; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 127 | |
anish kumar | 28c0ada | 2012-08-30 00:35:10 +0530 | [diff] [blame] | 128 | /* calculate the total number of bits set */ |
| 129 | weight = hweight32(correspondants); |
| 130 | if (weight > 1) |
| 131 | return i + 1; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 137 | 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] | 138 | { |
| 139 | int i; |
| 140 | |
| 141 | /* Find the the index of extcon cable in edev->supported_cable */ |
| 142 | for (i = 0; i < edev->max_supported; i++) { |
| 143 | if (edev->supported_cable[i] == id) |
| 144 | return i; |
| 145 | } |
| 146 | |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 150 | static bool is_extcon_changed(u32 prev, u32 new, int idx, bool *attached) |
| 151 | { |
| 152 | if (((prev >> idx) & 0x1) != ((new >> idx) & 0x1)) { |
Hans de Goede | f4513b0 | 2015-08-24 00:35:36 +0200 | [diff] [blame] | 153 | *attached = ((new >> idx) & 0x1) ? true : false; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 154 | return true; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 160 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 161 | char *buf) |
| 162 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 163 | int i, count = 0; |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 164 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 165 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 166 | if (edev->max_supported == 0) |
| 167 | return sprintf(buf, "%u\n", edev->state); |
| 168 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 169 | for (i = 0; i < edev->max_supported; i++) { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 170 | count += sprintf(buf + count, "%s=%d\n", |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 171 | extcon_name[edev->supported_cable[i]], |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 172 | !!(edev->state & (1 << i))); |
| 173 | } |
| 174 | |
| 175 | return count; |
| 176 | } |
| 177 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 178 | static ssize_t state_store(struct device *dev, struct device_attribute *attr, |
| 179 | const char *buf, size_t count) |
| 180 | { |
| 181 | u32 state; |
| 182 | ssize_t ret = 0; |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 183 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 184 | |
| 185 | ret = sscanf(buf, "0x%x", &state); |
| 186 | if (ret == 0) |
| 187 | ret = -EINVAL; |
| 188 | else |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 189 | ret = extcon_set_state(edev, state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 190 | |
| 191 | if (ret < 0) |
| 192 | return ret; |
| 193 | |
| 194 | return count; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 195 | } |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 196 | static DEVICE_ATTR_RW(state); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 197 | |
| 198 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 199 | char *buf) |
| 200 | { |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 201 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 202 | |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 203 | return sprintf(buf, "%s\n", edev->name); |
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_RO(name); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 206 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 207 | static ssize_t cable_name_show(struct device *dev, |
| 208 | struct device_attribute *attr, char *buf) |
| 209 | { |
| 210 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 211 | attr_name); |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 212 | int i = cable->cable_index; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 213 | |
| 214 | return sprintf(buf, "%s\n", |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 215 | extcon_name[cable->edev->supported_cable[i]]); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static ssize_t cable_state_show(struct device *dev, |
| 219 | struct device_attribute *attr, char *buf) |
| 220 | { |
| 221 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 222 | attr_state); |
| 223 | |
Roger Quadros | be052cc | 2015-07-07 16:06:15 +0300 | [diff] [blame] | 224 | int i = cable->cable_index; |
| 225 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 226 | return sprintf(buf, "%d\n", |
| 227 | extcon_get_cable_state_(cable->edev, |
Roger Quadros | be052cc | 2015-07-07 16:06:15 +0300 | [diff] [blame] | 228 | cable->edev->supported_cable[i])); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 229 | } |
| 230 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 231 | /** |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 232 | * extcon_update_state() - Update the cable attach states of the extcon device |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 233 | * only for the masked bits. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 234 | * @edev: the extcon device |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 235 | * @mask: the bit mask to designate updated bits. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 236 | * @state: new cable attach status for @edev |
| 237 | * |
| 238 | * Changing the state sends uevent with environment variable containing |
| 239 | * the name of extcon device (envp[0]) and the state output (envp[1]). |
| 240 | * Tizen uses this format for extcon device to get events from ports. |
| 241 | * Android uses this format as well. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 242 | * |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 243 | * Note that the notifier provides which bits are changed in the state |
| 244 | * variable with the val parameter (second) to the callback. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 245 | */ |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 246 | int extcon_update_state(struct extcon_dev *edev, u32 mask, u32 state) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 247 | { |
| 248 | char name_buf[120]; |
| 249 | char state_buf[120]; |
| 250 | char *prop_buf; |
| 251 | char *envp[3]; |
| 252 | int env_offset = 0; |
| 253 | int length; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 254 | int index; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 255 | unsigned long flags; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 256 | bool attached; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 257 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 258 | if (!edev) |
| 259 | return -EINVAL; |
| 260 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 261 | spin_lock_irqsave(&edev->lock, flags); |
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 | if (edev->state != ((edev->state & ~mask) | (state & mask))) { |
Roger Quadros | f7a8981 | 2015-07-06 17:46:58 +0300 | [diff] [blame] | 264 | u32 old_state; |
| 265 | |
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 | |
Roger Quadros | f7a8981 | 2015-07-06 17:46:58 +0300 | [diff] [blame] | 272 | old_state = edev->state; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 273 | edev->state &= ~mask; |
| 274 | edev->state |= state & mask; |
| 275 | |
Roger Quadros | f7a8981 | 2015-07-06 17:46:58 +0300 | [diff] [blame] | 276 | for (index = 0; index < edev->max_supported; index++) { |
| 277 | if (is_extcon_changed(old_state, edev->state, index, |
| 278 | &attached)) |
| 279 | raw_notifier_call_chain(&edev->nh[index], |
| 280 | attached, edev); |
| 281 | } |
| 282 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 283 | /* This could be in interrupt handler */ |
| 284 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 285 | if (prop_buf) { |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 286 | length = name_show(&edev->dev, NULL, prop_buf); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 287 | if (length > 0) { |
| 288 | if (prop_buf[length - 1] == '\n') |
| 289 | prop_buf[length - 1] = 0; |
| 290 | snprintf(name_buf, sizeof(name_buf), |
| 291 | "NAME=%s", prop_buf); |
| 292 | envp[env_offset++] = name_buf; |
| 293 | } |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 294 | length = state_show(&edev->dev, NULL, prop_buf); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 295 | if (length > 0) { |
| 296 | if (prop_buf[length - 1] == '\n') |
| 297 | prop_buf[length - 1] = 0; |
| 298 | snprintf(state_buf, sizeof(state_buf), |
| 299 | "STATE=%s", prop_buf); |
| 300 | envp[env_offset++] = state_buf; |
| 301 | } |
| 302 | envp[env_offset] = NULL; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 303 | /* Unlock early before uevent */ |
| 304 | spin_unlock_irqrestore(&edev->lock, flags); |
| 305 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 306 | kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 307 | free_page((unsigned long)prop_buf); |
| 308 | } else { |
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 | dev_err(&edev->dev, "out of memory in extcon_set_state\n"); |
| 313 | kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 314 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 315 | } else { |
| 316 | /* No changes */ |
| 317 | spin_unlock_irqrestore(&edev->lock, flags); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 318 | } |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 319 | |
| 320 | return 0; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 321 | } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 322 | EXPORT_SYMBOL_GPL(extcon_update_state); |
| 323 | |
| 324 | /** |
| 325 | * extcon_set_state() - Set the cable attach states of the extcon device. |
| 326 | * @edev: the extcon device |
| 327 | * @state: new cable attach status for @edev |
| 328 | * |
| 329 | * Note that notifier provides which bits are changed in the state |
| 330 | * variable with the val parameter (second) to the callback. |
| 331 | */ |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 332 | int extcon_set_state(struct extcon_dev *edev, u32 state) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 333 | { |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 334 | if (!edev) |
| 335 | return -EINVAL; |
| 336 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 337 | return extcon_update_state(edev, 0xffffffff, state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 338 | } |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 339 | EXPORT_SYMBOL_GPL(extcon_set_state); |
| 340 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 341 | /** |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 342 | * extcon_get_cable_state_() - Get the status of a specific cable. |
| 343 | * @edev: the extcon device that has the cable. |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 344 | * @id: the unique id of each external connector in extcon enumeration. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 345 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 346 | 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] | 347 | { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 348 | int index; |
| 349 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 350 | if (!edev) |
| 351 | return -EINVAL; |
| 352 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 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 | /** |
Axel Lin | 909f9ec | 2012-10-04 09:53:45 +0900 | [diff] [blame] | 365 | * extcon_set_cable_state_() - Set the status of a specific cable. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 366 | * @edev: the extcon device that has the cable. |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 367 | * @id: the unique id of each external connector |
| 368 | * in extcon enumeration. |
| 369 | * @state: the new cable status. The default semantics is |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 370 | * true: attached / false: detached. |
| 371 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 372 | int extcon_set_cable_state_(struct extcon_dev *edev, unsigned int id, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 373 | bool cable_state) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 374 | { |
| 375 | u32 state; |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 376 | int index; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 377 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 378 | if (!edev) |
| 379 | return -EINVAL; |
| 380 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 381 | index = find_cable_index_by_id(edev, id); |
| 382 | if (index < 0) |
| 383 | return index; |
| 384 | |
| 385 | if (edev->max_supported && edev->max_supported <= index) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 386 | return -EINVAL; |
| 387 | |
| 388 | state = cable_state ? (1 << index) : 0; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 389 | return extcon_update_state(edev, 1 << index, state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 390 | } |
| 391 | EXPORT_SYMBOL_GPL(extcon_set_cable_state_); |
| 392 | |
| 393 | /** |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 394 | * extcon_get_extcon_dev() - Get the extcon device instance from the name |
| 395 | * @extcon_name: The extcon name provided with extcon_dev_register() |
| 396 | */ |
| 397 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) |
| 398 | { |
| 399 | struct extcon_dev *sd; |
| 400 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 401 | if (!extcon_name) |
| 402 | return ERR_PTR(-EINVAL); |
| 403 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 404 | mutex_lock(&extcon_dev_list_lock); |
| 405 | list_for_each_entry(sd, &extcon_dev_list, entry) { |
| 406 | if (!strcmp(sd->name, extcon_name)) |
| 407 | goto out; |
| 408 | } |
| 409 | sd = NULL; |
| 410 | out: |
| 411 | mutex_unlock(&extcon_dev_list_lock); |
| 412 | return sd; |
| 413 | } |
| 414 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); |
| 415 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 416 | /** |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 417 | * extcon_register_notifier() - Register a notifiee to get notified by |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 418 | * any attach status changes from the extcon. |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 419 | * @edev: the extcon device that has the external connecotr. |
| 420 | * @id: the unique id of each external connector in extcon enumeration. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 421 | * @nb: a notifier block to be registered. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 422 | * |
| 423 | * Note that the second parameter given to the callback of nb (val) is |
| 424 | * "old_state", not the current state. The current state can be retrieved |
| 425 | * by looking at the third pameter (edev pointer)'s state value. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 426 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 427 | int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 428 | struct notifier_block *nb) |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 429 | { |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 430 | unsigned long flags; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 431 | int ret, idx; |
| 432 | |
Chanwoo Choi | 830ae44 | 2016-05-31 17:32:30 +0900 | [diff] [blame] | 433 | if (!nb) |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 434 | return -EINVAL; |
| 435 | |
Chanwoo Choi | 830ae44 | 2016-05-31 17:32:30 +0900 | [diff] [blame] | 436 | if (edev) { |
| 437 | idx = find_cable_index_by_id(edev, id); |
Stephen Boyd | a05f44c | 2016-06-23 19:34:30 +0900 | [diff] [blame] | 438 | if (idx < 0) |
| 439 | return idx; |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 440 | |
Chanwoo Choi | 830ae44 | 2016-05-31 17:32:30 +0900 | [diff] [blame] | 441 | spin_lock_irqsave(&edev->lock, flags); |
| 442 | ret = raw_notifier_chain_register(&edev->nh[idx], nb); |
| 443 | spin_unlock_irqrestore(&edev->lock, flags); |
| 444 | } else { |
| 445 | struct extcon_dev *extd; |
| 446 | |
| 447 | mutex_lock(&extcon_dev_list_lock); |
| 448 | list_for_each_entry(extd, &extcon_dev_list, entry) { |
| 449 | idx = find_cable_index_by_id(extd, id); |
| 450 | if (idx >= 0) |
| 451 | break; |
| 452 | } |
| 453 | mutex_unlock(&extcon_dev_list_lock); |
| 454 | |
| 455 | if (idx >= 0) { |
| 456 | edev = extd; |
| 457 | return extcon_register_notifier(extd, id, nb); |
| 458 | } else { |
| 459 | ret = -ENODEV; |
| 460 | } |
| 461 | } |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 462 | |
| 463 | return ret; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 464 | } |
| 465 | EXPORT_SYMBOL_GPL(extcon_register_notifier); |
| 466 | |
| 467 | /** |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 468 | * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 469 | * @edev: the extcon device that has the external connecotr. |
| 470 | * @id: the unique id of each external connector in extcon enumeration. |
| 471 | * @nb: a notifier block to be registered. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 472 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 473 | int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 474 | struct notifier_block *nb) |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 475 | { |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 476 | unsigned long flags; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 477 | int ret, idx; |
| 478 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 479 | if (!edev || !nb) |
| 480 | return -EINVAL; |
| 481 | |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 482 | idx = find_cable_index_by_id(edev, id); |
Stephen Boyd | a05f44c | 2016-06-23 19:34:30 +0900 | [diff] [blame] | 483 | if (idx < 0) |
| 484 | return idx; |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 485 | |
| 486 | spin_lock_irqsave(&edev->lock, flags); |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 487 | ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 488 | spin_unlock_irqrestore(&edev->lock, flags); |
| 489 | |
| 490 | return ret; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 491 | } |
| 492 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); |
| 493 | |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 494 | static struct attribute *extcon_attrs[] = { |
| 495 | &dev_attr_state.attr, |
| 496 | &dev_attr_name.attr, |
| 497 | NULL, |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 498 | }; |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 499 | ATTRIBUTE_GROUPS(extcon); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 500 | |
| 501 | static int create_extcon_class(void) |
| 502 | { |
| 503 | if (!extcon_class) { |
| 504 | extcon_class = class_create(THIS_MODULE, "extcon"); |
| 505 | if (IS_ERR(extcon_class)) |
| 506 | return PTR_ERR(extcon_class); |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 507 | extcon_class->dev_groups = extcon_groups; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 508 | |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 509 | #if defined(CONFIG_ANDROID) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 510 | switch_class = class_compat_register("switch"); |
| 511 | if (WARN(!switch_class, "cannot allocate")) |
| 512 | return -ENOMEM; |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 513 | #endif /* CONFIG_ANDROID */ |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 519 | static void extcon_dev_release(struct device *dev) |
| 520 | { |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 521 | } |
| 522 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 523 | static const char *muex_name = "mutually_exclusive"; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 524 | static void dummy_sysfs_dev_release(struct device *dev) |
| 525 | { |
| 526 | } |
| 527 | |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 528 | /* |
| 529 | * extcon_dev_allocate() - Allocate the memory of extcon device. |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 530 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 531 | * If supported_cable is NULL, cable name related APIs |
| 532 | * are disabled. |
| 533 | * |
| 534 | * This function allocates the memory for extcon device without allocating |
| 535 | * memory in each extcon provider driver and initialize default setting for |
| 536 | * extcon device. |
| 537 | * |
| 538 | * Return the pointer of extcon device if success or ERR_PTR(err) if fail |
| 539 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 540 | struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 541 | { |
| 542 | struct extcon_dev *edev; |
| 543 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 544 | if (!supported_cable) |
| 545 | return ERR_PTR(-EINVAL); |
| 546 | |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 547 | edev = kzalloc(sizeof(*edev), GFP_KERNEL); |
| 548 | if (!edev) |
| 549 | return ERR_PTR(-ENOMEM); |
| 550 | |
| 551 | edev->max_supported = 0; |
| 552 | edev->supported_cable = supported_cable; |
| 553 | |
| 554 | return edev; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * extcon_dev_free() - Free the memory of extcon device. |
| 559 | * @edev: the extcon device to free |
| 560 | */ |
| 561 | void extcon_dev_free(struct extcon_dev *edev) |
| 562 | { |
| 563 | kfree(edev); |
| 564 | } |
| 565 | EXPORT_SYMBOL_GPL(extcon_dev_free); |
| 566 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 567 | /** |
| 568 | * extcon_dev_register() - Register a new extcon device |
| 569 | * @edev : the new extcon device (should be allocated before calling) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 570 | * |
| 571 | * Among the members of edev struct, please set the "user initializing data" |
| 572 | * in any case and set the "optional callbacks" if required. However, please |
| 573 | * do not set the values of "internal data", which are initialized by |
| 574 | * this function. |
| 575 | */ |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 576 | int extcon_dev_register(struct extcon_dev *edev) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 577 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 578 | int ret, index = 0; |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 579 | static atomic_t edev_no = ATOMIC_INIT(-1); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 580 | |
| 581 | if (!extcon_class) { |
| 582 | ret = create_extcon_class(); |
| 583 | if (ret < 0) |
| 584 | return ret; |
| 585 | } |
| 586 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 587 | if (!edev || !edev->supported_cable) |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 588 | return -EINVAL; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 589 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 590 | for (; edev->supported_cable[index] != EXTCON_NONE; index++); |
| 591 | |
| 592 | edev->max_supported = index; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 593 | if (index > SUPPORTED_CABLE_MAX) { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 594 | dev_err(&edev->dev, |
| 595 | "exceed the maximum number of supported cables\n"); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 596 | return -EINVAL; |
| 597 | } |
| 598 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 599 | edev->dev.class = extcon_class; |
| 600 | edev->dev.release = extcon_dev_release; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 601 | |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 602 | edev->name = dev_name(edev->dev.parent); |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 603 | if (IS_ERR_OR_NULL(edev->name)) { |
| 604 | dev_err(&edev->dev, |
| 605 | "extcon device name is null\n"); |
| 606 | return -EINVAL; |
| 607 | } |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 608 | dev_set_name(&edev->dev, "extcon%lu", |
| 609 | (unsigned long)atomic_inc_return(&edev_no)); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 610 | |
| 611 | if (edev->max_supported) { |
| 612 | char buf[10]; |
| 613 | char *str; |
| 614 | struct extcon_cable *cable; |
| 615 | |
| 616 | edev->cables = kzalloc(sizeof(struct extcon_cable) * |
| 617 | edev->max_supported, GFP_KERNEL); |
| 618 | if (!edev->cables) { |
| 619 | ret = -ENOMEM; |
| 620 | goto err_sysfs_alloc; |
| 621 | } |
| 622 | for (index = 0; index < edev->max_supported; index++) { |
| 623 | cable = &edev->cables[index]; |
| 624 | |
| 625 | snprintf(buf, 10, "cable.%d", index); |
| 626 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 627 | GFP_KERNEL); |
| 628 | if (!str) { |
| 629 | for (index--; index >= 0; index--) { |
| 630 | cable = &edev->cables[index]; |
| 631 | kfree(cable->attr_g.name); |
| 632 | } |
| 633 | ret = -ENOMEM; |
| 634 | |
| 635 | goto err_alloc_cables; |
| 636 | } |
| 637 | strcpy(str, buf); |
| 638 | |
| 639 | cable->edev = edev; |
| 640 | cable->cable_index = index; |
| 641 | cable->attrs[0] = &cable->attr_name.attr; |
| 642 | cable->attrs[1] = &cable->attr_state.attr; |
| 643 | cable->attrs[2] = NULL; |
| 644 | cable->attr_g.name = str; |
| 645 | cable->attr_g.attrs = cable->attrs; |
| 646 | |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 647 | sysfs_attr_init(&cable->attr_name.attr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 648 | cable->attr_name.attr.name = "name"; |
| 649 | cable->attr_name.attr.mode = 0444; |
| 650 | cable->attr_name.show = cable_name_show; |
| 651 | |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 652 | sysfs_attr_init(&cable->attr_state.attr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 653 | cable->attr_state.attr.name = "state"; |
Chanwoo Choi | ea9dd9d | 2013-05-22 19:31:59 +0900 | [diff] [blame] | 654 | cable->attr_state.attr.mode = 0444; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 655 | cable->attr_state.show = cable_state_show; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 656 | } |
| 657 | } |
| 658 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 659 | if (edev->max_supported && edev->mutually_exclusive) { |
| 660 | char buf[80]; |
| 661 | char *name; |
| 662 | |
| 663 | /* Count the size of mutually_exclusive array */ |
| 664 | for (index = 0; edev->mutually_exclusive[index]; index++) |
| 665 | ; |
| 666 | |
| 667 | edev->attrs_muex = kzalloc(sizeof(struct attribute *) * |
| 668 | (index + 1), GFP_KERNEL); |
| 669 | if (!edev->attrs_muex) { |
| 670 | ret = -ENOMEM; |
| 671 | goto err_muex; |
| 672 | } |
| 673 | |
| 674 | edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * |
| 675 | index, GFP_KERNEL); |
| 676 | if (!edev->d_attrs_muex) { |
| 677 | ret = -ENOMEM; |
| 678 | kfree(edev->attrs_muex); |
| 679 | goto err_muex; |
| 680 | } |
| 681 | |
| 682 | for (index = 0; edev->mutually_exclusive[index]; index++) { |
| 683 | sprintf(buf, "0x%x", edev->mutually_exclusive[index]); |
| 684 | name = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 685 | GFP_KERNEL); |
| 686 | if (!name) { |
| 687 | for (index--; index >= 0; index--) { |
| 688 | kfree(edev->d_attrs_muex[index].attr. |
| 689 | name); |
| 690 | } |
| 691 | kfree(edev->d_attrs_muex); |
| 692 | kfree(edev->attrs_muex); |
| 693 | ret = -ENOMEM; |
| 694 | goto err_muex; |
| 695 | } |
| 696 | strcpy(name, buf); |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 697 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 698 | edev->d_attrs_muex[index].attr.name = name; |
| 699 | edev->d_attrs_muex[index].attr.mode = 0000; |
| 700 | edev->attrs_muex[index] = &edev->d_attrs_muex[index] |
| 701 | .attr; |
| 702 | } |
| 703 | edev->attr_g_muex.name = muex_name; |
| 704 | edev->attr_g_muex.attrs = edev->attrs_muex; |
| 705 | |
| 706 | } |
| 707 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 708 | if (edev->max_supported) { |
| 709 | edev->extcon_dev_type.groups = |
| 710 | kzalloc(sizeof(struct attribute_group *) * |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 711 | (edev->max_supported + 2), GFP_KERNEL); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 712 | if (!edev->extcon_dev_type.groups) { |
| 713 | ret = -ENOMEM; |
| 714 | goto err_alloc_groups; |
| 715 | } |
| 716 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 717 | edev->extcon_dev_type.name = dev_name(&edev->dev); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 718 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; |
| 719 | |
| 720 | for (index = 0; index < edev->max_supported; index++) |
| 721 | edev->extcon_dev_type.groups[index] = |
| 722 | &edev->cables[index].attr_g; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 723 | if (edev->mutually_exclusive) |
| 724 | edev->extcon_dev_type.groups[index] = |
| 725 | &edev->attr_g_muex; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 726 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 727 | edev->dev.type = &edev->extcon_dev_type; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 728 | } |
| 729 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 730 | ret = device_register(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 731 | if (ret) { |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 732 | put_device(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 733 | goto err_dev; |
| 734 | } |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 735 | #if defined(CONFIG_ANDROID) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 736 | if (switch_class) |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 737 | ret = class_compat_create_link(switch_class, &edev->dev, NULL); |
MyungJoo Ham | 449a2bf | 2012-04-23 20:19:57 +0900 | [diff] [blame] | 738 | #endif /* CONFIG_ANDROID */ |
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 | spin_lock_init(&edev->lock); |
| 741 | |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 742 | edev->nh = devm_kzalloc(&edev->dev, |
| 743 | sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL); |
| 744 | if (!edev->nh) { |
| 745 | ret = -ENOMEM; |
| 746 | goto err_dev; |
| 747 | } |
| 748 | |
| 749 | for (index = 0; index < edev->max_supported; index++) |
| 750 | RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 751 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 752 | dev_set_drvdata(&edev->dev, edev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 753 | edev->state = 0; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 754 | |
| 755 | mutex_lock(&extcon_dev_list_lock); |
| 756 | list_add(&edev->entry, &extcon_dev_list); |
| 757 | mutex_unlock(&extcon_dev_list_lock); |
| 758 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 759 | return 0; |
| 760 | |
| 761 | err_dev: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 762 | if (edev->max_supported) |
| 763 | kfree(edev->extcon_dev_type.groups); |
| 764 | err_alloc_groups: |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 765 | if (edev->max_supported && edev->mutually_exclusive) { |
| 766 | for (index = 0; edev->mutually_exclusive[index]; index++) |
| 767 | kfree(edev->d_attrs_muex[index].attr.name); |
| 768 | kfree(edev->d_attrs_muex); |
| 769 | kfree(edev->attrs_muex); |
| 770 | } |
| 771 | err_muex: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 772 | for (index = 0; index < edev->max_supported; index++) |
| 773 | kfree(edev->cables[index].attr_g.name); |
| 774 | err_alloc_cables: |
| 775 | if (edev->max_supported) |
| 776 | kfree(edev->cables); |
| 777 | err_sysfs_alloc: |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 778 | return ret; |
| 779 | } |
| 780 | EXPORT_SYMBOL_GPL(extcon_dev_register); |
| 781 | |
| 782 | /** |
| 783 | * extcon_dev_unregister() - Unregister the extcon device. |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 784 | * @edev: the extcon device instance to be unregistered. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 785 | * |
| 786 | * Note that this does not call kfree(edev) because edev was not allocated |
| 787 | * by this class. |
| 788 | */ |
| 789 | void extcon_dev_unregister(struct extcon_dev *edev) |
| 790 | { |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 791 | int index; |
| 792 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 793 | if (!edev) |
| 794 | return; |
| 795 | |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 796 | mutex_lock(&extcon_dev_list_lock); |
| 797 | list_del(&edev->entry); |
| 798 | mutex_unlock(&extcon_dev_list_lock); |
| 799 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 800 | if (IS_ERR_OR_NULL(get_device(&edev->dev))) { |
| 801 | dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", |
| 802 | dev_name(&edev->dev)); |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | |
Wang, Xiaoming | 7585ca0 | 2013-11-01 18:48:14 -0400 | [diff] [blame] | 806 | device_unregister(&edev->dev); |
| 807 | |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 808 | if (edev->mutually_exclusive && edev->max_supported) { |
| 809 | for (index = 0; edev->mutually_exclusive[index]; |
| 810 | index++) |
| 811 | kfree(edev->d_attrs_muex[index].attr.name); |
| 812 | kfree(edev->d_attrs_muex); |
| 813 | kfree(edev->attrs_muex); |
| 814 | } |
| 815 | |
| 816 | for (index = 0; index < edev->max_supported; index++) |
| 817 | kfree(edev->cables[index].attr_g.name); |
| 818 | |
| 819 | if (edev->max_supported) { |
| 820 | kfree(edev->extcon_dev_type.groups); |
| 821 | kfree(edev->cables); |
| 822 | } |
| 823 | |
| 824 | #if defined(CONFIG_ANDROID) |
| 825 | if (switch_class) |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 826 | class_compat_remove_link(switch_class, &edev->dev, NULL); |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 827 | #endif |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 828 | put_device(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 829 | } |
| 830 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); |
| 831 | |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 832 | #ifdef CONFIG_OF |
| 833 | /* |
| 834 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree |
| 835 | * @dev - instance to the given device |
| 836 | * @index - index into list of extcon_dev |
| 837 | * |
| 838 | * return the instance of extcon device |
| 839 | */ |
| 840 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) |
| 841 | { |
| 842 | struct device_node *node; |
| 843 | struct extcon_dev *edev; |
| 844 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 845 | if (!dev) |
| 846 | return ERR_PTR(-EINVAL); |
| 847 | |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 848 | if (!dev->of_node) { |
| 849 | dev_err(dev, "device does not have a device node entry\n"); |
| 850 | return ERR_PTR(-EINVAL); |
| 851 | } |
| 852 | |
| 853 | node = of_parse_phandle(dev->of_node, "extcon", index); |
| 854 | if (!node) { |
| 855 | dev_err(dev, "failed to get phandle in %s node\n", |
| 856 | dev->of_node->full_name); |
| 857 | return ERR_PTR(-ENODEV); |
| 858 | } |
| 859 | |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 860 | mutex_lock(&extcon_dev_list_lock); |
| 861 | list_for_each_entry(edev, &extcon_dev_list, entry) { |
| 862 | if (edev->dev.parent && edev->dev.parent->of_node == node) { |
| 863 | mutex_unlock(&extcon_dev_list_lock); |
Peter Chen | 5d5c4c1 | 2016-07-01 18:41:55 +0900 | [diff] [blame] | 864 | of_node_put(node); |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 865 | return edev; |
| 866 | } |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 867 | } |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 868 | mutex_unlock(&extcon_dev_list_lock); |
Peter Chen | 5d5c4c1 | 2016-07-01 18:41:55 +0900 | [diff] [blame] | 869 | of_node_put(node); |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 870 | |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 871 | return ERR_PTR(-EPROBE_DEFER); |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 872 | } |
| 873 | #else |
| 874 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) |
| 875 | { |
| 876 | return ERR_PTR(-ENOSYS); |
| 877 | } |
| 878 | #endif /* CONFIG_OF */ |
| 879 | EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); |
| 880 | |
Chanwoo Choi | 707d755 | 2015-04-15 13:57:51 +0900 | [diff] [blame] | 881 | /** |
| 882 | * extcon_get_edev_name() - Get the name of the extcon device. |
| 883 | * @edev: the extcon device |
| 884 | */ |
| 885 | const char *extcon_get_edev_name(struct extcon_dev *edev) |
| 886 | { |
| 887 | return !edev ? NULL : edev->name; |
| 888 | } |
| 889 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 890 | static int __init extcon_class_init(void) |
| 891 | { |
| 892 | return create_extcon_class(); |
| 893 | } |
| 894 | module_init(extcon_class_init); |
| 895 | |
| 896 | static void __exit extcon_class_exit(void) |
| 897 | { |
Peter Huewe | 0dc77b6 | 2012-09-24 15:32:31 +0900 | [diff] [blame] | 898 | #if defined(CONFIG_ANDROID) |
| 899 | class_compat_unregister(switch_class); |
| 900 | #endif |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 901 | class_destroy(extcon_class); |
| 902 | } |
| 903 | module_exit(extcon_class_exit); |
| 904 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 905 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 906 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 907 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); |
| 908 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 909 | MODULE_DESCRIPTION("External connector (extcon) class driver"); |
| 910 | MODULE_LICENSE("GPL"); |