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