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> |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 33 | #include <linux/of.h> |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 35 | #include <linux/sysfs.h> |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 36 | |
Chanwoo Choi | e6cf046 | 2016-12-26 20:37:38 +0900 | [diff] [blame] | 37 | #include "extcon.h" |
| 38 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 39 | #define SUPPORTED_CABLE_MAX 32 |
| 40 | #define CABLE_NAME_MAX 30 |
| 41 | |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 42 | struct __extcon_info { |
| 43 | unsigned int type; |
| 44 | unsigned int id; |
| 45 | const char *name; |
| 46 | |
| 47 | } extcon_info[] = { |
| 48 | [EXTCON_NONE] = { |
| 49 | .type = EXTCON_TYPE_MISC, |
| 50 | .id = EXTCON_NONE, |
| 51 | .name = "NONE", |
| 52 | }, |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 53 | |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 54 | /* USB external connector */ |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 55 | [EXTCON_USB] = { |
| 56 | .type = EXTCON_TYPE_USB, |
| 57 | .id = EXTCON_USB, |
| 58 | .name = "USB", |
| 59 | }, |
| 60 | [EXTCON_USB_HOST] = { |
| 61 | .type = EXTCON_TYPE_USB, |
| 62 | .id = EXTCON_USB_HOST, |
Chanwoo Choi | 86d6cda | 2017-01-07 05:17:36 +0900 | [diff] [blame] | 63 | .name = "USB-HOST", |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 64 | }, |
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 | /* Charging external connector */ |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 67 | [EXTCON_CHG_USB_SDP] = { |
| 68 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 69 | .id = EXTCON_CHG_USB_SDP, |
| 70 | .name = "SDP", |
| 71 | }, |
| 72 | [EXTCON_CHG_USB_DCP] = { |
| 73 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 74 | .id = EXTCON_CHG_USB_DCP, |
| 75 | .name = "DCP", |
| 76 | }, |
| 77 | [EXTCON_CHG_USB_CDP] = { |
| 78 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 79 | .id = EXTCON_CHG_USB_CDP, |
| 80 | .name = "CDP", |
| 81 | }, |
| 82 | [EXTCON_CHG_USB_ACA] = { |
| 83 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 84 | .id = EXTCON_CHG_USB_ACA, |
| 85 | .name = "ACA", |
| 86 | }, |
| 87 | [EXTCON_CHG_USB_FAST] = { |
| 88 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 89 | .id = EXTCON_CHG_USB_FAST, |
| 90 | .name = "FAST-CHARGER", |
| 91 | }, |
| 92 | [EXTCON_CHG_USB_SLOW] = { |
| 93 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 94 | .id = EXTCON_CHG_USB_SLOW, |
| 95 | .name = "SLOW-CHARGER", |
| 96 | }, |
Chanwoo Choi | 7fe95fb | 2016-08-05 18:15:46 +0900 | [diff] [blame] | 97 | [EXTCON_CHG_WPT] = { |
| 98 | .type = EXTCON_TYPE_CHG, |
| 99 | .id = EXTCON_CHG_WPT, |
| 100 | .name = "WPT", |
| 101 | }, |
Chanwoo Choi | 3c5f0e0 | 2017-01-02 13:03:03 +0900 | [diff] [blame] | 102 | [EXTCON_CHG_USB_PD] = { |
| 103 | .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB, |
| 104 | .id = EXTCON_CHG_USB_PD, |
| 105 | .name = "PD", |
| 106 | }, |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 107 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 108 | /* Jack external connector */ |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 109 | [EXTCON_JACK_MICROPHONE] = { |
| 110 | .type = EXTCON_TYPE_JACK, |
| 111 | .id = EXTCON_JACK_MICROPHONE, |
| 112 | .name = "MICROPHONE", |
| 113 | }, |
| 114 | [EXTCON_JACK_HEADPHONE] = { |
| 115 | .type = EXTCON_TYPE_JACK, |
| 116 | .id = EXTCON_JACK_HEADPHONE, |
| 117 | .name = "HEADPHONE", |
| 118 | }, |
| 119 | [EXTCON_JACK_LINE_IN] = { |
| 120 | .type = EXTCON_TYPE_JACK, |
| 121 | .id = EXTCON_JACK_LINE_IN, |
| 122 | .name = "LINE-IN", |
| 123 | }, |
| 124 | [EXTCON_JACK_LINE_OUT] = { |
| 125 | .type = EXTCON_TYPE_JACK, |
| 126 | .id = EXTCON_JACK_LINE_OUT, |
| 127 | .name = "LINE-OUT", |
| 128 | }, |
| 129 | [EXTCON_JACK_VIDEO_IN] = { |
| 130 | .type = EXTCON_TYPE_JACK, |
| 131 | .id = EXTCON_JACK_VIDEO_IN, |
| 132 | .name = "VIDEO-IN", |
| 133 | }, |
| 134 | [EXTCON_JACK_VIDEO_OUT] = { |
| 135 | .type = EXTCON_TYPE_JACK, |
| 136 | .id = EXTCON_JACK_VIDEO_OUT, |
| 137 | .name = "VIDEO-OUT", |
| 138 | }, |
| 139 | [EXTCON_JACK_SPDIF_IN] = { |
| 140 | .type = EXTCON_TYPE_JACK, |
| 141 | .id = EXTCON_JACK_SPDIF_IN, |
| 142 | .name = "SPDIF-IN", |
| 143 | }, |
| 144 | [EXTCON_JACK_SPDIF_OUT] = { |
| 145 | .type = EXTCON_TYPE_JACK, |
| 146 | .id = EXTCON_JACK_SPDIF_OUT, |
| 147 | .name = "SPDIF-OUT", |
| 148 | }, |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 149 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 150 | /* Display external connector */ |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 151 | [EXTCON_DISP_HDMI] = { |
| 152 | .type = EXTCON_TYPE_DISP, |
| 153 | .id = EXTCON_DISP_HDMI, |
| 154 | .name = "HDMI", |
| 155 | }, |
| 156 | [EXTCON_DISP_MHL] = { |
| 157 | .type = EXTCON_TYPE_DISP, |
| 158 | .id = EXTCON_DISP_MHL, |
| 159 | .name = "MHL", |
| 160 | }, |
| 161 | [EXTCON_DISP_DVI] = { |
| 162 | .type = EXTCON_TYPE_DISP, |
| 163 | .id = EXTCON_DISP_DVI, |
| 164 | .name = "DVI", |
| 165 | }, |
| 166 | [EXTCON_DISP_VGA] = { |
| 167 | .type = EXTCON_TYPE_DISP, |
| 168 | .id = EXTCON_DISP_VGA, |
| 169 | .name = "VGA", |
| 170 | }, |
Chris Zhong | 2164188 | 2016-07-22 01:13:02 +0900 | [diff] [blame] | 171 | [EXTCON_DISP_DP] = { |
| 172 | .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB, |
| 173 | .id = EXTCON_DISP_DP, |
| 174 | .name = "DP", |
| 175 | }, |
Chanwoo Choi | 9c0595d | 2016-08-05 17:49:23 +0900 | [diff] [blame] | 176 | [EXTCON_DISP_HMD] = { |
| 177 | .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB, |
| 178 | .id = EXTCON_DISP_HMD, |
| 179 | .name = "HMD", |
| 180 | }, |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 181 | |
Chanwoo Choi | 11eecf9 | 2015-10-03 14:15:13 +0900 | [diff] [blame] | 182 | /* Miscellaneous external connector */ |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 183 | [EXTCON_DOCK] = { |
| 184 | .type = EXTCON_TYPE_MISC, |
| 185 | .id = EXTCON_DOCK, |
| 186 | .name = "DOCK", |
| 187 | }, |
| 188 | [EXTCON_JIG] = { |
| 189 | .type = EXTCON_TYPE_MISC, |
| 190 | .id = EXTCON_JIG, |
| 191 | .name = "JIG", |
| 192 | }, |
| 193 | [EXTCON_MECHANICAL] = { |
| 194 | .type = EXTCON_TYPE_MISC, |
| 195 | .id = EXTCON_MECHANICAL, |
| 196 | .name = "MECHANICAL", |
| 197 | }, |
Chanwoo Choi | 8e9bc36 | 2015-05-19 19:58:49 +0900 | [diff] [blame] | 198 | |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 199 | { /* sentinel */ } |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 200 | }; |
| 201 | |
Chanwoo Choi | 20f7b53 | 2016-06-27 19:17:06 +0900 | [diff] [blame] | 202 | /** |
| 203 | * struct extcon_cable - An internal data for each cable of extcon device. |
| 204 | * @edev: The extcon device |
| 205 | * @cable_index: Index of this cable in the edev |
| 206 | * @attr_g: Attribute group for the cable |
| 207 | * @attr_name: "name" sysfs entry |
| 208 | * @attr_state: "state" sysfs entry |
| 209 | * @attrs: Array pointing to attr_name and attr_state for attr_g |
| 210 | */ |
| 211 | struct extcon_cable { |
| 212 | struct extcon_dev *edev; |
| 213 | int cable_index; |
| 214 | |
| 215 | struct attribute_group attr_g; |
| 216 | struct device_attribute attr_name; |
| 217 | struct device_attribute attr_state; |
| 218 | |
| 219 | struct attribute *attrs[3]; /* to be fed to attr_g.attrs */ |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 220 | |
| 221 | union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT]; |
| 222 | union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT]; |
| 223 | union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT]; |
| 224 | union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT]; |
Chanwoo Choi | 7f2a0a1 | 2016-07-25 21:15:19 +0900 | [diff] [blame] | 225 | |
| 226 | unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)]; |
| 227 | unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)]; |
| 228 | unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)]; |
| 229 | unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)]; |
Chanwoo Choi | 20f7b53 | 2016-06-27 19:17:06 +0900 | [diff] [blame] | 230 | }; |
| 231 | |
Mark Brown | be3a07f | 2012-06-05 16:14:38 +0100 | [diff] [blame] | 232 | static struct class *extcon_class; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 233 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 234 | static LIST_HEAD(extcon_dev_list); |
| 235 | static DEFINE_MUTEX(extcon_dev_list_lock); |
| 236 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 237 | /** |
| 238 | * check_mutually_exclusive - Check if new_state violates mutually_exclusive |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 239 | * condition. |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 240 | * @edev: the extcon device |
| 241 | * @new_state: new cable attach status for @edev |
| 242 | * |
| 243 | * Returns 0 if nothing violates. Returns the index + 1 for the first |
| 244 | * violated condition. |
| 245 | */ |
| 246 | static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state) |
| 247 | { |
| 248 | int i = 0; |
| 249 | |
| 250 | if (!edev->mutually_exclusive) |
| 251 | return 0; |
| 252 | |
| 253 | for (i = 0; edev->mutually_exclusive[i]; i++) { |
anish kumar | 28c0ada | 2012-08-30 00:35:10 +0530 | [diff] [blame] | 254 | int weight; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 255 | u32 correspondants = new_state & edev->mutually_exclusive[i]; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 256 | |
anish kumar | 28c0ada | 2012-08-30 00:35:10 +0530 | [diff] [blame] | 257 | /* calculate the total number of bits set */ |
| 258 | weight = hweight32(correspondants); |
| 259 | if (weight > 1) |
| 260 | return i + 1; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 266 | 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] | 267 | { |
| 268 | int i; |
| 269 | |
| 270 | /* Find the the index of extcon cable in edev->supported_cable */ |
| 271 | for (i = 0; i < edev->max_supported; i++) { |
| 272 | if (edev->supported_cable[i] == id) |
| 273 | return i; |
| 274 | } |
| 275 | |
| 276 | return -EINVAL; |
| 277 | } |
| 278 | |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 279 | static int get_extcon_type(unsigned int prop) |
| 280 | { |
| 281 | switch (prop) { |
| 282 | case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: |
| 283 | return EXTCON_TYPE_USB; |
| 284 | case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: |
| 285 | return EXTCON_TYPE_CHG; |
| 286 | case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: |
| 287 | return EXTCON_TYPE_JACK; |
| 288 | case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: |
| 289 | return EXTCON_TYPE_DISP; |
| 290 | default: |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index) |
| 296 | { |
| 297 | return !!(edev->state & BIT(index)); |
| 298 | } |
| 299 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 300 | static bool is_extcon_changed(struct extcon_dev *edev, int index, |
| 301 | bool new_state) |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 302 | { |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 303 | int state = !!(edev->state & BIT(index)); |
| 304 | return (state != new_state); |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 307 | static bool is_extcon_property_supported(unsigned int id, unsigned int prop) |
| 308 | { |
| 309 | int type; |
| 310 | |
| 311 | /* Check whether the property is supported or not. */ |
| 312 | type = get_extcon_type(prop); |
| 313 | if (type < 0) |
| 314 | return false; |
| 315 | |
| 316 | /* Check whether a specific extcon id supports the property or not. */ |
| 317 | return !!(extcon_info[id].type & type); |
| 318 | } |
| 319 | |
Chanwoo Choi | 7f2a0a1 | 2016-07-25 21:15:19 +0900 | [diff] [blame] | 320 | static int is_extcon_property_capability(struct extcon_dev *edev, |
| 321 | unsigned int id, int index,unsigned int prop) |
| 322 | { |
| 323 | struct extcon_cable *cable; |
| 324 | int type, ret; |
| 325 | |
| 326 | /* Check whether the property is supported or not. */ |
| 327 | type = get_extcon_type(prop); |
| 328 | if (type < 0) |
| 329 | return type; |
| 330 | |
| 331 | cable = &edev->cables[index]; |
| 332 | |
| 333 | switch (type) { |
| 334 | case EXTCON_TYPE_USB: |
| 335 | ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); |
| 336 | break; |
| 337 | case EXTCON_TYPE_CHG: |
| 338 | ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); |
| 339 | break; |
| 340 | case EXTCON_TYPE_JACK: |
| 341 | ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); |
| 342 | break; |
| 343 | case EXTCON_TYPE_DISP: |
| 344 | ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); |
| 345 | break; |
| 346 | default: |
| 347 | ret = -EINVAL; |
| 348 | } |
| 349 | |
| 350 | return ret; |
| 351 | } |
| 352 | |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 353 | static void init_property(struct extcon_dev *edev, unsigned int id, int index) |
| 354 | { |
| 355 | unsigned int type = extcon_info[id].type; |
| 356 | struct extcon_cable *cable = &edev->cables[index]; |
| 357 | |
| 358 | if (EXTCON_TYPE_USB & type) |
| 359 | memset(cable->usb_propval, 0, sizeof(cable->usb_propval)); |
| 360 | if (EXTCON_TYPE_CHG & type) |
| 361 | memset(cable->chg_propval, 0, sizeof(cable->chg_propval)); |
| 362 | if (EXTCON_TYPE_JACK & type) |
| 363 | memset(cable->jack_propval, 0, sizeof(cable->jack_propval)); |
| 364 | if (EXTCON_TYPE_DISP & type) |
| 365 | memset(cable->disp_propval, 0, sizeof(cable->disp_propval)); |
| 366 | } |
| 367 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 368 | static ssize_t state_show(struct device *dev, struct device_attribute *attr, |
| 369 | char *buf) |
| 370 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 371 | int i, count = 0; |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 372 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 373 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 374 | if (edev->max_supported == 0) |
| 375 | return sprintf(buf, "%u\n", edev->state); |
| 376 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 377 | for (i = 0; i < edev->max_supported; i++) { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 378 | count += sprintf(buf + count, "%s=%d\n", |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 379 | extcon_info[edev->supported_cable[i]].name, |
Chanwoo Choi | 70641a0 | 2017-03-29 19:18:36 +0900 | [diff] [blame] | 380 | !!(edev->state & BIT(i))); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | return count; |
| 384 | } |
Chanwoo Choi | 5d5321e | 2016-07-18 15:39:28 +0900 | [diff] [blame] | 385 | static DEVICE_ATTR_RO(state); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 386 | |
| 387 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
| 388 | char *buf) |
| 389 | { |
Jingoo Han | cb8bb3a | 2013-09-09 14:33:32 +0900 | [diff] [blame] | 390 | struct extcon_dev *edev = dev_get_drvdata(dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 391 | |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 392 | return sprintf(buf, "%s\n", edev->name); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 393 | } |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 394 | static DEVICE_ATTR_RO(name); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 395 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 396 | static ssize_t cable_name_show(struct device *dev, |
| 397 | struct device_attribute *attr, char *buf) |
| 398 | { |
| 399 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 400 | attr_name); |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 401 | int i = cable->cable_index; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 402 | |
| 403 | return sprintf(buf, "%s\n", |
Chanwoo Choi | 55e4e2f | 2016-07-11 16:34:52 +0900 | [diff] [blame] | 404 | extcon_info[cable->edev->supported_cable[i]].name); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static ssize_t cable_state_show(struct device *dev, |
| 408 | struct device_attribute *attr, char *buf) |
| 409 | { |
| 410 | struct extcon_cable *cable = container_of(attr, struct extcon_cable, |
| 411 | attr_state); |
| 412 | |
Roger Quadros | be052cc | 2015-07-07 16:06:15 +0300 | [diff] [blame] | 413 | int i = cable->cable_index; |
| 414 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 415 | return sprintf(buf, "%d\n", |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 416 | extcon_get_state(cable->edev, cable->edev->supported_cable[i])); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 417 | } |
| 418 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 419 | /** |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 420 | * extcon_sync() - Synchronize the states for both the attached/detached |
| 421 | * @edev: the extcon device that has the cable. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 422 | * |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 423 | * This function send a notification to synchronize the all states of a |
| 424 | * specific external connector |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 425 | */ |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 426 | int extcon_sync(struct extcon_dev *edev, unsigned int id) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 427 | { |
| 428 | char name_buf[120]; |
| 429 | char state_buf[120]; |
| 430 | char *prop_buf; |
| 431 | char *envp[3]; |
| 432 | int env_offset = 0; |
| 433 | int length; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 434 | int index; |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 435 | int state; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 436 | unsigned long flags; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 437 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 438 | if (!edev) |
| 439 | return -EINVAL; |
| 440 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 441 | index = find_cable_index_by_id(edev, id); |
| 442 | if (index < 0) |
| 443 | return index; |
| 444 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 445 | spin_lock_irqsave(&edev->lock, flags); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 446 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 447 | state = !!(edev->state & BIT(index)); |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Call functions in a raw notifier chain for the specific one |
| 451 | * external connector. |
| 452 | */ |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 453 | raw_notifier_call_chain(&edev->nh[index], state, edev); |
Roger Quadros | f7a8981 | 2015-07-06 17:46:58 +0300 | [diff] [blame] | 454 | |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 455 | /* |
| 456 | * Call functions in a raw notifier chain for the all supported |
| 457 | * external connectors. |
| 458 | */ |
| 459 | raw_notifier_call_chain(&edev->nh_all, state, edev); |
| 460 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 461 | /* This could be in interrupt handler */ |
| 462 | prop_buf = (char *)get_zeroed_page(GFP_ATOMIC); |
| 463 | if (!prop_buf) { |
| 464 | /* Unlock early before uevent */ |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 465 | spin_unlock_irqrestore(&edev->lock, flags); |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 466 | |
| 467 | dev_err(&edev->dev, "out of memory in extcon_set_state\n"); |
| 468 | kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE); |
| 469 | |
Pan Bian | e7d9dd5 | 2016-12-03 16:56:49 +0800 | [diff] [blame] | 470 | return -ENOMEM; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 471 | } |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 472 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 473 | length = name_show(&edev->dev, NULL, prop_buf); |
| 474 | if (length > 0) { |
| 475 | if (prop_buf[length - 1] == '\n') |
| 476 | prop_buf[length - 1] = 0; |
| 477 | snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf); |
| 478 | envp[env_offset++] = name_buf; |
| 479 | } |
| 480 | |
| 481 | length = state_show(&edev->dev, NULL, prop_buf); |
| 482 | if (length > 0) { |
| 483 | if (prop_buf[length - 1] == '\n') |
| 484 | prop_buf[length - 1] = 0; |
| 485 | snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf); |
| 486 | envp[env_offset++] = state_buf; |
| 487 | } |
| 488 | envp[env_offset] = NULL; |
| 489 | |
| 490 | /* Unlock early before uevent */ |
| 491 | spin_unlock_irqrestore(&edev->lock, flags); |
| 492 | kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp); |
| 493 | free_page((unsigned long)prop_buf); |
| 494 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 495 | return 0; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 496 | } |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 497 | EXPORT_SYMBOL_GPL(extcon_sync); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 498 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 499 | /** |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 500 | * extcon_get_state() - Get the state of a external connector. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 501 | * @edev: the extcon device that has the cable. |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 502 | * @id: the unique id of each external connector in extcon enumeration. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 503 | */ |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 504 | int extcon_get_state(struct extcon_dev *edev, const unsigned int id) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 505 | { |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 506 | int index, state; |
| 507 | unsigned long flags; |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 508 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 509 | if (!edev) |
| 510 | return -EINVAL; |
| 511 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 512 | index = find_cable_index_by_id(edev, id); |
| 513 | if (index < 0) |
| 514 | return index; |
| 515 | |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 516 | spin_lock_irqsave(&edev->lock, flags); |
| 517 | state = is_extcon_attached(edev, index); |
| 518 | spin_unlock_irqrestore(&edev->lock, flags); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 519 | |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 520 | return state; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 521 | } |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 522 | EXPORT_SYMBOL_GPL(extcon_get_state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 523 | |
| 524 | /** |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 525 | * extcon_set_state() - Set the state of a external connector. |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 526 | * without a notification. |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 527 | * @edev: the extcon device that has the cable. |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 528 | * @id: the unique id of each external connector |
| 529 | * in extcon enumeration. |
| 530 | * @state: the new cable status. The default semantics is |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 531 | * true: attached / false: detached. |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 532 | * |
| 533 | * This function only set the state of a external connector without |
| 534 | * a notification. To synchronize the data of a external connector, |
| 535 | * use extcon_set_state_sync() and extcon_sync(). |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 536 | */ |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 537 | int extcon_set_state(struct extcon_dev *edev, unsigned int id, |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 538 | bool cable_state) |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 539 | { |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 540 | unsigned long flags; |
| 541 | int index, ret = 0; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 542 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 543 | if (!edev) |
| 544 | return -EINVAL; |
| 545 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 546 | index = find_cable_index_by_id(edev, id); |
| 547 | if (index < 0) |
| 548 | return index; |
| 549 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 550 | spin_lock_irqsave(&edev->lock, flags); |
| 551 | |
| 552 | /* Check whether the external connector's state is changed. */ |
| 553 | if (!is_extcon_changed(edev, index, cable_state)) |
| 554 | goto out; |
| 555 | |
| 556 | if (check_mutually_exclusive(edev, |
| 557 | (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) { |
| 558 | ret = -EPERM; |
| 559 | goto out; |
| 560 | } |
| 561 | |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 562 | /* |
| 563 | * Initialize the value of extcon property before setting |
| 564 | * the detached state for an external connector. |
| 565 | */ |
| 566 | if (!cable_state) |
| 567 | init_property(edev, id, index); |
| 568 | |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 569 | /* Update the state for a external connector. */ |
| 570 | if (cable_state) |
| 571 | edev->state |= BIT(index); |
| 572 | else |
| 573 | edev->state &= ~(BIT(index)); |
| 574 | out: |
| 575 | spin_unlock_irqrestore(&edev->lock, flags); |
| 576 | |
| 577 | return ret; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 578 | } |
Chanwoo Choi | 575c2b86 | 2016-07-22 13:03:17 +0900 | [diff] [blame] | 579 | EXPORT_SYMBOL_GPL(extcon_set_state); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 580 | |
| 581 | /** |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 582 | * extcon_set_state_sync() - Set the state of a external connector |
| 583 | * with a notification. |
| 584 | * @edev: the extcon device that has the cable. |
| 585 | * @id: the unique id of each external connector |
| 586 | * in extcon enumeration. |
| 587 | * @state: the new cable status. The default semantics is |
| 588 | * true: attached / false: detached. |
| 589 | * |
| 590 | * This function set the state of external connector and synchronize the data |
| 591 | * by usning a notification. |
| 592 | */ |
| 593 | int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id, |
| 594 | bool cable_state) |
| 595 | { |
| 596 | int ret, index; |
| 597 | unsigned long flags; |
| 598 | |
| 599 | index = find_cable_index_by_id(edev, id); |
| 600 | if (index < 0) |
| 601 | return index; |
| 602 | |
| 603 | /* Check whether the external connector's state is changed. */ |
| 604 | spin_lock_irqsave(&edev->lock, flags); |
| 605 | ret = is_extcon_changed(edev, index, cable_state); |
| 606 | spin_unlock_irqrestore(&edev->lock, flags); |
| 607 | if (!ret) |
| 608 | return 0; |
| 609 | |
| 610 | ret = extcon_set_state(edev, id, cable_state); |
| 611 | if (ret < 0) |
| 612 | return ret; |
| 613 | |
| 614 | return extcon_sync(edev, id); |
| 615 | } |
| 616 | EXPORT_SYMBOL_GPL(extcon_set_state_sync); |
| 617 | |
| 618 | /** |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 619 | * extcon_get_property() - Get the property value of a specific cable. |
| 620 | * @edev: the extcon device that has the cable. |
| 621 | * @id: the unique id of each external connector |
| 622 | * in extcon enumeration. |
| 623 | * @prop: the property id among enum extcon_property. |
| 624 | * @prop_val: the pointer which store the value of property. |
| 625 | * |
| 626 | * When getting the property value of external connector, the external connector |
| 627 | * should be attached. If detached state, function just return 0 without |
| 628 | * property value. Also, the each property should be included in the list of |
| 629 | * supported properties according to the type of external connectors. |
| 630 | * |
| 631 | * Returns 0 if success or error number if fail |
| 632 | */ |
| 633 | int extcon_get_property(struct extcon_dev *edev, unsigned int id, |
| 634 | unsigned int prop, |
| 635 | union extcon_property_value *prop_val) |
| 636 | { |
| 637 | struct extcon_cable *cable; |
| 638 | unsigned long flags; |
| 639 | int index, ret = 0; |
| 640 | |
| 641 | *prop_val = (union extcon_property_value)(0); |
| 642 | |
| 643 | if (!edev) |
| 644 | return -EINVAL; |
| 645 | |
| 646 | /* Check whether the property is supported or not */ |
| 647 | if (!is_extcon_property_supported(id, prop)) |
| 648 | return -EINVAL; |
| 649 | |
| 650 | /* Find the cable index of external connector by using id */ |
| 651 | index = find_cable_index_by_id(edev, id); |
| 652 | if (index < 0) |
| 653 | return index; |
| 654 | |
| 655 | spin_lock_irqsave(&edev->lock, flags); |
| 656 | |
Chanwoo Choi | 7f2a0a1 | 2016-07-25 21:15:19 +0900 | [diff] [blame] | 657 | /* Check whether the property is available or not. */ |
| 658 | if (!is_extcon_property_capability(edev, id, index, prop)) { |
| 659 | spin_unlock_irqrestore(&edev->lock, flags); |
| 660 | return -EPERM; |
| 661 | } |
| 662 | |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 663 | /* |
| 664 | * Check whether the external connector is attached. |
| 665 | * If external connector is detached, the user can not |
| 666 | * get the property value. |
| 667 | */ |
| 668 | if (!is_extcon_attached(edev, index)) { |
| 669 | spin_unlock_irqrestore(&edev->lock, flags); |
| 670 | return 0; |
| 671 | } |
| 672 | |
| 673 | cable = &edev->cables[index]; |
| 674 | |
| 675 | /* Get the property value according to extcon type */ |
| 676 | switch (prop) { |
| 677 | case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: |
| 678 | *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN]; |
| 679 | break; |
| 680 | case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: |
| 681 | *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN]; |
| 682 | break; |
| 683 | case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: |
| 684 | *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN]; |
| 685 | break; |
| 686 | case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: |
| 687 | *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN]; |
| 688 | break; |
| 689 | default: |
| 690 | ret = -EINVAL; |
| 691 | break; |
| 692 | } |
| 693 | |
| 694 | spin_unlock_irqrestore(&edev->lock, flags); |
| 695 | |
| 696 | return ret; |
| 697 | } |
| 698 | EXPORT_SYMBOL_GPL(extcon_get_property); |
| 699 | |
| 700 | /** |
| 701 | * extcon_set_property() - Set the property value of a specific cable. |
| 702 | * @edev: the extcon device that has the cable. |
| 703 | * @id: the unique id of each external connector |
| 704 | * in extcon enumeration. |
| 705 | * @prop: the property id among enum extcon_property. |
| 706 | * @prop_val: the pointer including the new value of property. |
| 707 | * |
| 708 | * The each property should be included in the list of supported properties |
| 709 | * according to the type of external connectors. |
| 710 | * |
| 711 | * Returns 0 if success or error number if fail |
| 712 | */ |
| 713 | int extcon_set_property(struct extcon_dev *edev, unsigned int id, |
| 714 | unsigned int prop, |
| 715 | union extcon_property_value prop_val) |
| 716 | { |
| 717 | struct extcon_cable *cable; |
| 718 | unsigned long flags; |
| 719 | int index, ret = 0; |
| 720 | |
| 721 | if (!edev) |
| 722 | return -EINVAL; |
| 723 | |
| 724 | /* Check whether the property is supported or not */ |
| 725 | if (!is_extcon_property_supported(id, prop)) |
| 726 | return -EINVAL; |
| 727 | |
| 728 | /* Find the cable index of external connector by using id */ |
| 729 | index = find_cable_index_by_id(edev, id); |
| 730 | if (index < 0) |
| 731 | return index; |
| 732 | |
| 733 | spin_lock_irqsave(&edev->lock, flags); |
| 734 | |
Chanwoo Choi | 7f2a0a1 | 2016-07-25 21:15:19 +0900 | [diff] [blame] | 735 | /* Check whether the property is available or not. */ |
| 736 | if (!is_extcon_property_capability(edev, id, index, prop)) { |
| 737 | spin_unlock_irqrestore(&edev->lock, flags); |
| 738 | return -EPERM; |
| 739 | } |
| 740 | |
Chanwoo Choi | 792e7e9e | 2016-07-11 19:30:43 +0900 | [diff] [blame] | 741 | cable = &edev->cables[index]; |
| 742 | |
| 743 | /* Set the property value according to extcon type */ |
| 744 | switch (prop) { |
| 745 | case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX: |
| 746 | cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val; |
| 747 | break; |
| 748 | case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX: |
| 749 | cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val; |
| 750 | break; |
| 751 | case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX: |
| 752 | cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val; |
| 753 | break; |
| 754 | case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX: |
| 755 | cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val; |
| 756 | break; |
| 757 | default: |
| 758 | ret = -EINVAL; |
| 759 | break; |
| 760 | } |
| 761 | |
| 762 | spin_unlock_irqrestore(&edev->lock, flags); |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | EXPORT_SYMBOL_GPL(extcon_set_property); |
| 767 | |
| 768 | /** |
Chanwoo Choi | ab11af04 | 2016-07-22 13:16:34 +0900 | [diff] [blame] | 769 | * extcon_set_property_sync() - Set the property value of a specific cable |
| 770 | with a notification. |
| 771 | * @prop_val: the pointer including the new value of property. |
| 772 | * |
| 773 | * When setting the property value of external connector, the external connector |
| 774 | * should be attached. The each property should be included in the list of |
| 775 | * supported properties according to the type of external connectors. |
| 776 | * |
| 777 | * Returns 0 if success or error number if fail |
| 778 | */ |
| 779 | int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id, |
| 780 | unsigned int prop, |
| 781 | union extcon_property_value prop_val) |
| 782 | { |
| 783 | int ret; |
| 784 | |
| 785 | ret = extcon_set_property(edev, id, prop, prop_val); |
| 786 | if (ret < 0) |
| 787 | return ret; |
| 788 | |
| 789 | return extcon_sync(edev, id); |
| 790 | } |
| 791 | EXPORT_SYMBOL_GPL(extcon_set_property_sync); |
| 792 | |
| 793 | /** |
Chanwoo Choi | 7f2a0a1 | 2016-07-25 21:15:19 +0900 | [diff] [blame] | 794 | * extcon_get_property_capability() - Get the capability of property |
| 795 | * of an external connector. |
| 796 | * @edev: the extcon device that has the cable. |
| 797 | * @id: the unique id of each external connector |
| 798 | * in extcon enumeration. |
| 799 | * @prop: the property id among enum extcon_property. |
| 800 | * |
| 801 | * Returns 1 if the property is available or 0 if not available. |
| 802 | */ |
| 803 | int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id, |
| 804 | unsigned int prop) |
| 805 | { |
| 806 | int index; |
| 807 | |
| 808 | if (!edev) |
| 809 | return -EINVAL; |
| 810 | |
| 811 | /* Check whether the property is supported or not */ |
| 812 | if (!is_extcon_property_supported(id, prop)) |
| 813 | return -EINVAL; |
| 814 | |
| 815 | /* Find the cable index of external connector by using id */ |
| 816 | index = find_cable_index_by_id(edev, id); |
| 817 | if (index < 0) |
| 818 | return index; |
| 819 | |
| 820 | return is_extcon_property_capability(edev, id, index, prop); |
| 821 | } |
| 822 | EXPORT_SYMBOL_GPL(extcon_get_property_capability); |
| 823 | |
| 824 | /** |
| 825 | * extcon_set_property_capability() - Set the capability of a property |
| 826 | * of an external connector. |
| 827 | * @edev: the extcon device that has the cable. |
| 828 | * @id: the unique id of each external connector |
| 829 | * in extcon enumeration. |
| 830 | * @prop: the property id among enum extcon_property. |
| 831 | * |
| 832 | * This function set the capability of a property for an external connector |
| 833 | * to mark the bit in capability bitmap which mean the available state of |
| 834 | * a property. |
| 835 | * |
| 836 | * Returns 0 if success or error number if fail |
| 837 | */ |
| 838 | int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id, |
| 839 | unsigned int prop) |
| 840 | { |
| 841 | struct extcon_cable *cable; |
| 842 | int index, type, ret = 0; |
| 843 | |
| 844 | if (!edev) |
| 845 | return -EINVAL; |
| 846 | |
| 847 | /* Check whether the property is supported or not. */ |
| 848 | if (!is_extcon_property_supported(id, prop)) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | /* Find the cable index of external connector by using id. */ |
| 852 | index = find_cable_index_by_id(edev, id); |
| 853 | if (index < 0) |
| 854 | return index; |
| 855 | |
| 856 | type = get_extcon_type(prop); |
| 857 | if (type < 0) |
| 858 | return type; |
| 859 | |
| 860 | cable = &edev->cables[index]; |
| 861 | |
| 862 | switch (type) { |
| 863 | case EXTCON_TYPE_USB: |
| 864 | __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits); |
| 865 | break; |
| 866 | case EXTCON_TYPE_CHG: |
| 867 | __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits); |
| 868 | break; |
| 869 | case EXTCON_TYPE_JACK: |
| 870 | __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits); |
| 871 | break; |
| 872 | case EXTCON_TYPE_DISP: |
| 873 | __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits); |
| 874 | break; |
| 875 | default: |
| 876 | ret = -EINVAL; |
| 877 | } |
| 878 | |
| 879 | return ret; |
| 880 | } |
| 881 | EXPORT_SYMBOL_GPL(extcon_set_property_capability); |
| 882 | |
| 883 | /** |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 884 | * extcon_get_extcon_dev() - Get the extcon device instance from the name |
| 885 | * @extcon_name: The extcon name provided with extcon_dev_register() |
| 886 | */ |
| 887 | struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name) |
| 888 | { |
| 889 | struct extcon_dev *sd; |
| 890 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 891 | if (!extcon_name) |
| 892 | return ERR_PTR(-EINVAL); |
| 893 | |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 894 | mutex_lock(&extcon_dev_list_lock); |
| 895 | list_for_each_entry(sd, &extcon_dev_list, entry) { |
| 896 | if (!strcmp(sd->name, extcon_name)) |
| 897 | goto out; |
| 898 | } |
| 899 | sd = NULL; |
| 900 | out: |
| 901 | mutex_unlock(&extcon_dev_list_lock); |
| 902 | return sd; |
| 903 | } |
| 904 | EXPORT_SYMBOL_GPL(extcon_get_extcon_dev); |
| 905 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 906 | /** |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 907 | * extcon_register_notifier() - Register a notifiee to get notified by |
Chanwoo Choi | a75e1c7 | 2013-08-31 13:16:49 +0900 | [diff] [blame] | 908 | * any attach status changes from the extcon. |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 909 | * @edev: the extcon device that has the external connecotr. |
| 910 | * @id: the unique id of each external connector in extcon enumeration. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 911 | * @nb: a notifier block to be registered. |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 912 | * |
| 913 | * Note that the second parameter given to the callback of nb (val) is |
| 914 | * "old_state", not the current state. The current state can be retrieved |
| 915 | * by looking at the third pameter (edev pointer)'s state value. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 916 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 917 | int extcon_register_notifier(struct extcon_dev *edev, unsigned int id, |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 918 | struct notifier_block *nb) |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 919 | { |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 920 | unsigned long flags; |
Maninder Singh | 2c8116a | 2016-08-01 14:51:30 +0530 | [diff] [blame] | 921 | int ret, idx = -EINVAL; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 922 | |
Chanwoo Choi | 01b4c9a | 2016-12-19 21:02:33 +0900 | [diff] [blame] | 923 | if (!edev || !nb) |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 924 | return -EINVAL; |
| 925 | |
Chanwoo Choi | 01b4c9a | 2016-12-19 21:02:33 +0900 | [diff] [blame] | 926 | idx = find_cable_index_by_id(edev, id); |
| 927 | if (idx < 0) |
| 928 | return idx; |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 929 | |
Chanwoo Choi | 01b4c9a | 2016-12-19 21:02:33 +0900 | [diff] [blame] | 930 | spin_lock_irqsave(&edev->lock, flags); |
| 931 | ret = raw_notifier_chain_register(&edev->nh[idx], nb); |
| 932 | spin_unlock_irqrestore(&edev->lock, flags); |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 933 | |
| 934 | return ret; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 935 | } |
| 936 | EXPORT_SYMBOL_GPL(extcon_register_notifier); |
| 937 | |
| 938 | /** |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 939 | * extcon_unregister_notifier() - Unregister a notifiee from the extcon device. |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 940 | * @edev: the extcon device that has the external connecotr. |
| 941 | * @id: the unique id of each external connector in extcon enumeration. |
| 942 | * @nb: a notifier block to be registered. |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 943 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 944 | int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id, |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 945 | struct notifier_block *nb) |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 946 | { |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 947 | unsigned long flags; |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 948 | int ret, idx; |
| 949 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 950 | if (!edev || !nb) |
| 951 | return -EINVAL; |
| 952 | |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 953 | idx = find_cable_index_by_id(edev, id); |
Stephen Boyd | a05f44c | 2016-06-23 19:34:30 +0900 | [diff] [blame] | 954 | if (idx < 0) |
| 955 | return idx; |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 956 | |
| 957 | spin_lock_irqsave(&edev->lock, flags); |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 958 | ret = raw_notifier_chain_unregister(&edev->nh[idx], nb); |
Hans de Goede | 66bee35 | 2015-03-21 17:26:24 +0100 | [diff] [blame] | 959 | spin_unlock_irqrestore(&edev->lock, flags); |
| 960 | |
| 961 | return ret; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 962 | } |
| 963 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier); |
| 964 | |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 965 | /** |
| 966 | * extcon_register_notifier_all() - Register a notifier block for all connectors |
Markus Elfring | 826a47e | 2017-04-23 22:15:20 +0200 | [diff] [blame] | 967 | * @edev: the extcon device that has the external connector. |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 968 | * @nb: a notifier block to be registered. |
| 969 | * |
Markus Elfring | 826a47e | 2017-04-23 22:15:20 +0200 | [diff] [blame] | 970 | * This function registers a notifier block in order to receive the state |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 971 | * change of all supported external connectors from extcon device. |
Markus Elfring | 826a47e | 2017-04-23 22:15:20 +0200 | [diff] [blame] | 972 | * And the second parameter given to the callback of nb (val) is |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 973 | * the current state and third parameter is the edev pointer. |
| 974 | * |
| 975 | * Returns 0 if success or error number if fail |
| 976 | */ |
| 977 | int extcon_register_notifier_all(struct extcon_dev *edev, |
| 978 | struct notifier_block *nb) |
| 979 | { |
| 980 | unsigned long flags; |
| 981 | int ret; |
| 982 | |
| 983 | if (!edev || !nb) |
| 984 | return -EINVAL; |
| 985 | |
| 986 | spin_lock_irqsave(&edev->lock, flags); |
| 987 | ret = raw_notifier_chain_register(&edev->nh_all, nb); |
| 988 | spin_unlock_irqrestore(&edev->lock, flags); |
| 989 | |
| 990 | return ret; |
| 991 | } |
| 992 | EXPORT_SYMBOL_GPL(extcon_register_notifier_all); |
| 993 | |
| 994 | /** |
| 995 | * extcon_unregister_notifier_all() - Unregister a notifier block from extcon. |
| 996 | * @edev: the extcon device that has the external connecotr. |
| 997 | * @nb: a notifier block to be registered. |
| 998 | * |
| 999 | * Returns 0 if success or error number if fail |
| 1000 | */ |
| 1001 | int extcon_unregister_notifier_all(struct extcon_dev *edev, |
| 1002 | struct notifier_block *nb) |
| 1003 | { |
| 1004 | unsigned long flags; |
| 1005 | int ret; |
| 1006 | |
| 1007 | if (!edev || !nb) |
| 1008 | return -EINVAL; |
| 1009 | |
| 1010 | spin_lock_irqsave(&edev->lock, flags); |
| 1011 | ret = raw_notifier_chain_unregister(&edev->nh_all, nb); |
| 1012 | spin_unlock_irqrestore(&edev->lock, flags); |
| 1013 | |
| 1014 | return ret; |
| 1015 | } |
| 1016 | EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all); |
| 1017 | |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 1018 | static struct attribute *extcon_attrs[] = { |
| 1019 | &dev_attr_state.attr, |
| 1020 | &dev_attr_name.attr, |
| 1021 | NULL, |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1022 | }; |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 1023 | ATTRIBUTE_GROUPS(extcon); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1024 | |
| 1025 | static int create_extcon_class(void) |
| 1026 | { |
| 1027 | if (!extcon_class) { |
| 1028 | extcon_class = class_create(THIS_MODULE, "extcon"); |
| 1029 | if (IS_ERR(extcon_class)) |
| 1030 | return PTR_ERR(extcon_class); |
Greg Kroah-Hartman | af01da0 | 2013-07-24 15:05:10 -0700 | [diff] [blame] | 1031 | extcon_class->dev_groups = extcon_groups; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1037 | static void extcon_dev_release(struct device *dev) |
| 1038 | { |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1039 | } |
| 1040 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 1041 | static const char *muex_name = "mutually_exclusive"; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1042 | static void dummy_sysfs_dev_release(struct device *dev) |
| 1043 | { |
| 1044 | } |
| 1045 | |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 1046 | /* |
| 1047 | * extcon_dev_allocate() - Allocate the memory of extcon device. |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 1048 | * @supported_cable: Array of supported extcon ending with EXTCON_NONE. |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 1049 | * If supported_cable is NULL, cable name related APIs |
| 1050 | * are disabled. |
| 1051 | * |
| 1052 | * This function allocates the memory for extcon device without allocating |
| 1053 | * memory in each extcon provider driver and initialize default setting for |
| 1054 | * extcon device. |
| 1055 | * |
| 1056 | * Return the pointer of extcon device if success or ERR_PTR(err) if fail |
| 1057 | */ |
Chanwoo Choi | 73b6ecd | 2015-06-12 11:10:06 +0900 | [diff] [blame] | 1058 | struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable) |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 1059 | { |
| 1060 | struct extcon_dev *edev; |
| 1061 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 1062 | if (!supported_cable) |
| 1063 | return ERR_PTR(-EINVAL); |
| 1064 | |
Chanwoo Choi | a9af652 | 2014-04-24 19:46:49 +0900 | [diff] [blame] | 1065 | edev = kzalloc(sizeof(*edev), GFP_KERNEL); |
| 1066 | if (!edev) |
| 1067 | return ERR_PTR(-ENOMEM); |
| 1068 | |
| 1069 | edev->max_supported = 0; |
| 1070 | edev->supported_cable = supported_cable; |
| 1071 | |
| 1072 | return edev; |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * extcon_dev_free() - Free the memory of extcon device. |
| 1077 | * @edev: the extcon device to free |
| 1078 | */ |
| 1079 | void extcon_dev_free(struct extcon_dev *edev) |
| 1080 | { |
| 1081 | kfree(edev); |
| 1082 | } |
| 1083 | EXPORT_SYMBOL_GPL(extcon_dev_free); |
| 1084 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1085 | /** |
| 1086 | * extcon_dev_register() - Register a new extcon device |
| 1087 | * @edev : the new extcon device (should be allocated before calling) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1088 | * |
| 1089 | * Among the members of edev struct, please set the "user initializing data" |
| 1090 | * in any case and set the "optional callbacks" if required. However, please |
| 1091 | * do not set the values of "internal data", which are initialized by |
| 1092 | * this function. |
| 1093 | */ |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 1094 | int extcon_dev_register(struct extcon_dev *edev) |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1095 | { |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1096 | int ret, index = 0; |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 1097 | static atomic_t edev_no = ATOMIC_INIT(-1); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1098 | |
| 1099 | if (!extcon_class) { |
| 1100 | ret = create_extcon_class(); |
| 1101 | if (ret < 0) |
| 1102 | return ret; |
| 1103 | } |
| 1104 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 1105 | if (!edev || !edev->supported_cable) |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 1106 | return -EINVAL; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1107 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 1108 | for (; edev->supported_cable[index] != EXTCON_NONE; index++); |
| 1109 | |
| 1110 | edev->max_supported = index; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1111 | if (index > SUPPORTED_CABLE_MAX) { |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 1112 | dev_err(&edev->dev, |
| 1113 | "exceed the maximum number of supported cables\n"); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1114 | return -EINVAL; |
| 1115 | } |
| 1116 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1117 | edev->dev.class = extcon_class; |
| 1118 | edev->dev.release = extcon_dev_release; |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1119 | |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 1120 | edev->name = dev_name(edev->dev.parent); |
Chanwoo Choi | 42d7d75 | 2013-09-27 09:20:26 +0900 | [diff] [blame] | 1121 | if (IS_ERR_OR_NULL(edev->name)) { |
| 1122 | dev_err(&edev->dev, |
| 1123 | "extcon device name is null\n"); |
| 1124 | return -EINVAL; |
| 1125 | } |
Chanwoo Choi | 71c3ffa | 2015-04-15 15:02:01 +0900 | [diff] [blame] | 1126 | dev_set_name(&edev->dev, "extcon%lu", |
| 1127 | (unsigned long)atomic_inc_return(&edev_no)); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1128 | |
| 1129 | if (edev->max_supported) { |
| 1130 | char buf[10]; |
| 1131 | char *str; |
| 1132 | struct extcon_cable *cable; |
| 1133 | |
| 1134 | edev->cables = kzalloc(sizeof(struct extcon_cable) * |
| 1135 | edev->max_supported, GFP_KERNEL); |
| 1136 | if (!edev->cables) { |
| 1137 | ret = -ENOMEM; |
| 1138 | goto err_sysfs_alloc; |
| 1139 | } |
| 1140 | for (index = 0; index < edev->max_supported; index++) { |
| 1141 | cable = &edev->cables[index]; |
| 1142 | |
| 1143 | snprintf(buf, 10, "cable.%d", index); |
| 1144 | str = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 1145 | GFP_KERNEL); |
| 1146 | if (!str) { |
| 1147 | for (index--; index >= 0; index--) { |
| 1148 | cable = &edev->cables[index]; |
| 1149 | kfree(cable->attr_g.name); |
| 1150 | } |
| 1151 | ret = -ENOMEM; |
| 1152 | |
| 1153 | goto err_alloc_cables; |
| 1154 | } |
| 1155 | strcpy(str, buf); |
| 1156 | |
| 1157 | cable->edev = edev; |
| 1158 | cable->cable_index = index; |
| 1159 | cable->attrs[0] = &cable->attr_name.attr; |
| 1160 | cable->attrs[1] = &cable->attr_state.attr; |
| 1161 | cable->attrs[2] = NULL; |
| 1162 | cable->attr_g.name = str; |
| 1163 | cable->attr_g.attrs = cable->attrs; |
| 1164 | |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 1165 | sysfs_attr_init(&cable->attr_name.attr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1166 | cable->attr_name.attr.name = "name"; |
| 1167 | cable->attr_name.attr.mode = 0444; |
| 1168 | cable->attr_name.show = cable_name_show; |
| 1169 | |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 1170 | sysfs_attr_init(&cable->attr_state.attr); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1171 | cable->attr_state.attr.name = "state"; |
Chanwoo Choi | ea9dd9d | 2013-05-22 19:31:59 +0900 | [diff] [blame] | 1172 | cable->attr_state.attr.mode = 0444; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1173 | cable->attr_state.show = cable_state_show; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 1177 | if (edev->max_supported && edev->mutually_exclusive) { |
| 1178 | char buf[80]; |
| 1179 | char *name; |
| 1180 | |
| 1181 | /* Count the size of mutually_exclusive array */ |
| 1182 | for (index = 0; edev->mutually_exclusive[index]; index++) |
| 1183 | ; |
| 1184 | |
| 1185 | edev->attrs_muex = kzalloc(sizeof(struct attribute *) * |
| 1186 | (index + 1), GFP_KERNEL); |
| 1187 | if (!edev->attrs_muex) { |
| 1188 | ret = -ENOMEM; |
| 1189 | goto err_muex; |
| 1190 | } |
| 1191 | |
| 1192 | edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) * |
| 1193 | index, GFP_KERNEL); |
| 1194 | if (!edev->d_attrs_muex) { |
| 1195 | ret = -ENOMEM; |
| 1196 | kfree(edev->attrs_muex); |
| 1197 | goto err_muex; |
| 1198 | } |
| 1199 | |
| 1200 | for (index = 0; edev->mutually_exclusive[index]; index++) { |
| 1201 | sprintf(buf, "0x%x", edev->mutually_exclusive[index]); |
| 1202 | name = kzalloc(sizeof(char) * (strlen(buf) + 1), |
| 1203 | GFP_KERNEL); |
| 1204 | if (!name) { |
| 1205 | for (index--; index >= 0; index--) { |
| 1206 | kfree(edev->d_attrs_muex[index].attr. |
| 1207 | name); |
| 1208 | } |
| 1209 | kfree(edev->d_attrs_muex); |
| 1210 | kfree(edev->attrs_muex); |
| 1211 | ret = -ENOMEM; |
| 1212 | goto err_muex; |
| 1213 | } |
| 1214 | strcpy(name, buf); |
Mark Brown | 9baf322 | 2012-08-16 20:03:21 +0100 | [diff] [blame] | 1215 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 1216 | edev->d_attrs_muex[index].attr.name = name; |
| 1217 | edev->d_attrs_muex[index].attr.mode = 0000; |
| 1218 | edev->attrs_muex[index] = &edev->d_attrs_muex[index] |
| 1219 | .attr; |
| 1220 | } |
| 1221 | edev->attr_g_muex.name = muex_name; |
| 1222 | edev->attr_g_muex.attrs = edev->attrs_muex; |
| 1223 | |
| 1224 | } |
| 1225 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1226 | if (edev->max_supported) { |
| 1227 | edev->extcon_dev_type.groups = |
| 1228 | kzalloc(sizeof(struct attribute_group *) * |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 1229 | (edev->max_supported + 2), GFP_KERNEL); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1230 | if (!edev->extcon_dev_type.groups) { |
| 1231 | ret = -ENOMEM; |
| 1232 | goto err_alloc_groups; |
| 1233 | } |
| 1234 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1235 | edev->extcon_dev_type.name = dev_name(&edev->dev); |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1236 | edev->extcon_dev_type.release = dummy_sysfs_dev_release; |
| 1237 | |
| 1238 | for (index = 0; index < edev->max_supported; index++) |
| 1239 | edev->extcon_dev_type.groups[index] = |
| 1240 | &edev->cables[index].attr_g; |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 1241 | if (edev->mutually_exclusive) |
| 1242 | edev->extcon_dev_type.groups[index] = |
| 1243 | &edev->attr_g_muex; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1244 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1245 | edev->dev.type = &edev->extcon_dev_type; |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1246 | } |
| 1247 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1248 | ret = device_register(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1249 | if (ret) { |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1250 | put_device(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1251 | goto err_dev; |
| 1252 | } |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1253 | |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1254 | spin_lock_init(&edev->lock); |
Markus Elfring | 3f5071a | 2017-04-23 20:54:11 +0200 | [diff] [blame] | 1255 | edev->nh = devm_kcalloc(&edev->dev, edev->max_supported, |
| 1256 | sizeof(*edev->nh), GFP_KERNEL); |
Chanwoo Choi | 046050f | 2015-05-19 20:01:12 +0900 | [diff] [blame] | 1257 | if (!edev->nh) { |
| 1258 | ret = -ENOMEM; |
| 1259 | goto err_dev; |
| 1260 | } |
| 1261 | |
| 1262 | for (index = 0; index < edev->max_supported; index++) |
| 1263 | RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]); |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 1264 | |
Chanwoo Choi | 815429b | 2017-03-29 19:30:17 +0900 | [diff] [blame] | 1265 | RAW_INIT_NOTIFIER_HEAD(&edev->nh_all); |
| 1266 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1267 | dev_set_drvdata(&edev->dev, edev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1268 | edev->state = 0; |
Donggeun Kim | 74c5d09 | 2012-04-20 14:16:24 +0900 | [diff] [blame] | 1269 | |
| 1270 | mutex_lock(&extcon_dev_list_lock); |
| 1271 | list_add(&edev->entry, &extcon_dev_list); |
| 1272 | mutex_unlock(&extcon_dev_list_lock); |
| 1273 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1274 | return 0; |
| 1275 | |
| 1276 | err_dev: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1277 | if (edev->max_supported) |
| 1278 | kfree(edev->extcon_dev_type.groups); |
| 1279 | err_alloc_groups: |
MyungJoo Ham | bde68e6 | 2012-04-20 14:16:26 +0900 | [diff] [blame] | 1280 | if (edev->max_supported && edev->mutually_exclusive) { |
| 1281 | for (index = 0; edev->mutually_exclusive[index]; index++) |
| 1282 | kfree(edev->d_attrs_muex[index].attr.name); |
| 1283 | kfree(edev->d_attrs_muex); |
| 1284 | kfree(edev->attrs_muex); |
| 1285 | } |
| 1286 | err_muex: |
MyungJoo Ham | 806d9dd | 2012-04-20 14:16:25 +0900 | [diff] [blame] | 1287 | for (index = 0; index < edev->max_supported; index++) |
| 1288 | kfree(edev->cables[index].attr_g.name); |
| 1289 | err_alloc_cables: |
| 1290 | if (edev->max_supported) |
| 1291 | kfree(edev->cables); |
| 1292 | err_sysfs_alloc: |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1293 | return ret; |
| 1294 | } |
| 1295 | EXPORT_SYMBOL_GPL(extcon_dev_register); |
| 1296 | |
| 1297 | /** |
| 1298 | * extcon_dev_unregister() - Unregister the extcon device. |
Peter Meerwald | c338bb0 | 2012-08-23 09:11:54 +0900 | [diff] [blame] | 1299 | * @edev: the extcon device instance to be unregistered. |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1300 | * |
| 1301 | * Note that this does not call kfree(edev) because edev was not allocated |
| 1302 | * by this class. |
| 1303 | */ |
| 1304 | void extcon_dev_unregister(struct extcon_dev *edev) |
| 1305 | { |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 1306 | int index; |
| 1307 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 1308 | if (!edev) |
| 1309 | return; |
| 1310 | |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 1311 | mutex_lock(&extcon_dev_list_lock); |
| 1312 | list_del(&edev->entry); |
| 1313 | mutex_unlock(&extcon_dev_list_lock); |
| 1314 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1315 | if (IS_ERR_OR_NULL(get_device(&edev->dev))) { |
| 1316 | dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n", |
| 1317 | dev_name(&edev->dev)); |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 1318 | return; |
| 1319 | } |
| 1320 | |
Wang, Xiaoming | 7585ca0 | 2013-11-01 18:48:14 -0400 | [diff] [blame] | 1321 | device_unregister(&edev->dev); |
| 1322 | |
anish kumar | 57e7cd3 | 2012-10-22 09:43:33 +0900 | [diff] [blame] | 1323 | if (edev->mutually_exclusive && edev->max_supported) { |
| 1324 | for (index = 0; edev->mutually_exclusive[index]; |
| 1325 | index++) |
| 1326 | kfree(edev->d_attrs_muex[index].attr.name); |
| 1327 | kfree(edev->d_attrs_muex); |
| 1328 | kfree(edev->attrs_muex); |
| 1329 | } |
| 1330 | |
| 1331 | for (index = 0; index < edev->max_supported; index++) |
| 1332 | kfree(edev->cables[index].attr_g.name); |
| 1333 | |
| 1334 | if (edev->max_supported) { |
| 1335 | kfree(edev->extcon_dev_type.groups); |
| 1336 | kfree(edev->cables); |
| 1337 | } |
| 1338 | |
Chanwoo Choi | dae6165 | 2013-09-27 09:19:40 +0900 | [diff] [blame] | 1339 | put_device(&edev->dev); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1340 | } |
| 1341 | EXPORT_SYMBOL_GPL(extcon_dev_unregister); |
| 1342 | |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1343 | #ifdef CONFIG_OF |
| 1344 | /* |
| 1345 | * extcon_get_edev_by_phandle - Get the extcon device from devicetree |
| 1346 | * @dev - instance to the given device |
| 1347 | * @index - index into list of extcon_dev |
| 1348 | * |
| 1349 | * return the instance of extcon device |
| 1350 | */ |
| 1351 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) |
| 1352 | { |
| 1353 | struct device_node *node; |
| 1354 | struct extcon_dev *edev; |
| 1355 | |
Chanwoo Choi | 7eae43a | 2015-06-21 23:48:36 +0900 | [diff] [blame] | 1356 | if (!dev) |
| 1357 | return ERR_PTR(-EINVAL); |
| 1358 | |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1359 | if (!dev->of_node) { |
Stephen Boyd | e8752b7 | 2016-07-05 11:57:05 -0700 | [diff] [blame] | 1360 | dev_dbg(dev, "device does not have a device node entry\n"); |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1361 | return ERR_PTR(-EINVAL); |
| 1362 | } |
| 1363 | |
| 1364 | node = of_parse_phandle(dev->of_node, "extcon", index); |
| 1365 | if (!node) { |
Stephen Boyd | e8752b7 | 2016-07-05 11:57:05 -0700 | [diff] [blame] | 1366 | dev_dbg(dev, "failed to get phandle in %s node\n", |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1367 | dev->of_node->full_name); |
| 1368 | return ERR_PTR(-ENODEV); |
| 1369 | } |
| 1370 | |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 1371 | mutex_lock(&extcon_dev_list_lock); |
| 1372 | list_for_each_entry(edev, &extcon_dev_list, entry) { |
| 1373 | if (edev->dev.parent && edev->dev.parent->of_node == node) { |
| 1374 | mutex_unlock(&extcon_dev_list_lock); |
Peter Chen | 5d5c4c1 | 2016-07-01 18:41:55 +0900 | [diff] [blame] | 1375 | of_node_put(node); |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 1376 | return edev; |
| 1377 | } |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1378 | } |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 1379 | mutex_unlock(&extcon_dev_list_lock); |
Peter Chen | 5d5c4c1 | 2016-07-01 18:41:55 +0900 | [diff] [blame] | 1380 | of_node_put(node); |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1381 | |
Tomasz Figa | f841afb | 2014-10-16 15:11:44 +0200 | [diff] [blame] | 1382 | return ERR_PTR(-EPROBE_DEFER); |
Chanwoo Choi | 1ad94ff | 2014-03-18 19:55:46 +0900 | [diff] [blame] | 1383 | } |
| 1384 | #else |
| 1385 | struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index) |
| 1386 | { |
| 1387 | return ERR_PTR(-ENOSYS); |
| 1388 | } |
| 1389 | #endif /* CONFIG_OF */ |
| 1390 | EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle); |
| 1391 | |
Chanwoo Choi | 707d755 | 2015-04-15 13:57:51 +0900 | [diff] [blame] | 1392 | /** |
| 1393 | * extcon_get_edev_name() - Get the name of the extcon device. |
| 1394 | * @edev: the extcon device |
| 1395 | */ |
| 1396 | const char *extcon_get_edev_name(struct extcon_dev *edev) |
| 1397 | { |
| 1398 | return !edev ? NULL : edev->name; |
| 1399 | } |
| 1400 | |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1401 | static int __init extcon_class_init(void) |
| 1402 | { |
| 1403 | return create_extcon_class(); |
| 1404 | } |
| 1405 | module_init(extcon_class_init); |
| 1406 | |
| 1407 | static void __exit extcon_class_exit(void) |
| 1408 | { |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1409 | class_destroy(extcon_class); |
| 1410 | } |
| 1411 | module_exit(extcon_class_exit); |
| 1412 | |
Chanwoo Choi | 2a9de9c | 2015-04-24 19:16:05 +0900 | [diff] [blame] | 1413 | MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>"); |
MyungJoo Ham | de55d87 | 2012-04-20 14:16:22 +0900 | [diff] [blame] | 1414 | MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>"); |
| 1415 | MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>"); |
| 1416 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 1417 | MODULE_DESCRIPTION("External connector (extcon) class driver"); |
| 1418 | MODULE_LICENSE("GPL"); |