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