.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * drivers/extcon/extcon.c - External Connector (extcon) framework. |
---|
3 | 4 | * |
---|
.. | .. |
---|
11 | 12 | * based on android/drivers/switch/switch_class.c |
---|
12 | 13 | * Copyright (C) 2008 Google, Inc. |
---|
13 | 14 | * Author: Mike Lockwood <lockwood@android.com> |
---|
14 | | - * |
---|
15 | | - * This software is licensed under the terms of the GNU General Public |
---|
16 | | - * License version 2, as published by the Free Software Foundation, and |
---|
17 | | - * may be copied, distributed, and modified under those terms. |
---|
18 | | - * |
---|
19 | | - * This program is distributed in the hope that it will be useful, |
---|
20 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | | - * GNU General Public License for more details. |
---|
23 | 15 | */ |
---|
24 | 16 | |
---|
25 | 17 | #include <linux/module.h> |
---|
.. | .. |
---|
33 | 25 | #include <linux/sysfs.h> |
---|
34 | 26 | |
---|
35 | 27 | #include "extcon.h" |
---|
36 | | -#ifdef CONFIG_ARCH_ROCKCHIP |
---|
37 | | -#include "../base/base.h" |
---|
38 | | -#endif |
---|
39 | 28 | |
---|
40 | 29 | #define SUPPORTED_CABLE_MAX 32 |
---|
41 | 30 | |
---|
.. | .. |
---|
183 | 172 | .id = EXTCON_DISP_HMD, |
---|
184 | 173 | .name = "HMD", |
---|
185 | 174 | }, |
---|
| 175 | + [EXTCON_DISP_CVBS] = { |
---|
| 176 | + .type = EXTCON_TYPE_DISP, |
---|
| 177 | + .id = EXTCON_DISP_CVBS, |
---|
| 178 | + .name = "CVBS", |
---|
| 179 | + }, |
---|
| 180 | + [EXTCON_DISP_EDP] = { |
---|
| 181 | + .type = EXTCON_TYPE_DISP, |
---|
| 182 | + .id = EXTCON_DISP_EDP, |
---|
| 183 | + .name = "EDP", |
---|
| 184 | + }, |
---|
186 | 185 | |
---|
187 | 186 | /* Miscellaneous external connector */ |
---|
188 | 187 | [EXTCON_DOCK] = { |
---|
.. | .. |
---|
212 | 211 | * @attr_name: "name" sysfs entry |
---|
213 | 212 | * @attr_state: "state" sysfs entry |
---|
214 | 213 | * @attrs: the array pointing to attr_name and attr_state for attr_g |
---|
| 214 | + * @usb_propval: the array of USB connector properties |
---|
| 215 | + * @chg_propval: the array of charger connector properties |
---|
| 216 | + * @jack_propval: the array of jack connector properties |
---|
| 217 | + * @disp_propval: the array of display connector properties |
---|
| 218 | + * @usb_bits: the bit array of the USB connector property capabilities |
---|
| 219 | + * @chg_bits: the bit array of the charger connector property capabilities |
---|
| 220 | + * @jack_bits: the bit array of the jack connector property capabilities |
---|
| 221 | + * @disp_bits: the bit array of the display connector property capabilities |
---|
215 | 222 | */ |
---|
216 | 223 | struct extcon_cable { |
---|
217 | 224 | struct extcon_dev *edev; |
---|
.. | .. |
---|
495 | 502 | } |
---|
496 | 503 | EXPORT_SYMBOL_GPL(extcon_sync); |
---|
497 | 504 | |
---|
498 | | -int extcon_blocking_sync(struct extcon_dev *edev, unsigned int id, bool val) |
---|
499 | | -{ |
---|
500 | | - int index; |
---|
501 | | - |
---|
502 | | - if (!edev) |
---|
503 | | - return -EINVAL; |
---|
504 | | - |
---|
505 | | - index = find_cable_index_by_id(edev, id); |
---|
506 | | - if (index < 0) |
---|
507 | | - return index; |
---|
508 | | - |
---|
509 | | - return blocking_notifier_call_chain(&edev->bnh[index], val, edev); |
---|
510 | | -} |
---|
511 | | -EXPORT_SYMBOL(extcon_blocking_sync); |
---|
512 | | - |
---|
513 | 505 | /** |
---|
514 | 506 | * extcon_get_state() - Get the state of an external connector. |
---|
515 | 507 | * @edev: the extcon device |
---|
.. | .. |
---|
651 | 643 | unsigned long flags; |
---|
652 | 644 | int index, ret = 0; |
---|
653 | 645 | |
---|
654 | | - *prop_val = (union extcon_property_value)(0); |
---|
| 646 | + *prop_val = (union extcon_property_value){0}; |
---|
655 | 647 | |
---|
656 | 648 | if (!edev) |
---|
657 | 649 | return -EINVAL; |
---|
.. | .. |
---|
931 | 923 | struct notifier_block *nb) |
---|
932 | 924 | { |
---|
933 | 925 | unsigned long flags; |
---|
934 | | - int ret, idx = -EINVAL; |
---|
| 926 | + int ret, idx; |
---|
935 | 927 | |
---|
936 | 928 | if (!edev || !nb) |
---|
937 | 929 | return -EINVAL; |
---|
.. | .. |
---|
947 | 939 | return ret; |
---|
948 | 940 | } |
---|
949 | 941 | EXPORT_SYMBOL_GPL(extcon_register_notifier); |
---|
950 | | - |
---|
951 | | -int extcon_register_blocking_notifier(struct extcon_dev *edev, unsigned int id, |
---|
952 | | - struct notifier_block *nb) |
---|
953 | | -{ |
---|
954 | | - int idx = -EINVAL; |
---|
955 | | - |
---|
956 | | - if (!edev || !nb) |
---|
957 | | - return -EINVAL; |
---|
958 | | - |
---|
959 | | - idx = find_cable_index_by_id(edev, id); |
---|
960 | | - if (idx < 0) |
---|
961 | | - return idx; |
---|
962 | | - |
---|
963 | | - return blocking_notifier_chain_register(&edev->bnh[idx], nb); |
---|
964 | | -} |
---|
965 | | -EXPORT_SYMBOL(extcon_register_blocking_notifier); |
---|
966 | | - |
---|
967 | | -int extcon_unregister_blocking_notifier(struct extcon_dev *edev, |
---|
968 | | - unsigned int id, struct notifier_block *nb) |
---|
969 | | -{ |
---|
970 | | - int idx; |
---|
971 | | - |
---|
972 | | - if (!edev || !nb) |
---|
973 | | - return -EINVAL; |
---|
974 | | - |
---|
975 | | - idx = find_cable_index_by_id(edev, id); |
---|
976 | | - if (idx < 0) |
---|
977 | | - return idx; |
---|
978 | | - |
---|
979 | | - return blocking_notifier_chain_unregister(&edev->bnh[idx], nb); |
---|
980 | | -} |
---|
981 | | -EXPORT_SYMBOL(extcon_unregister_blocking_notifier); |
---|
982 | 942 | |
---|
983 | 943 | /** |
---|
984 | 944 | * extcon_unregister_notifier() - Unregister a notifier block from the extcon. |
---|
.. | .. |
---|
1128 | 1088 | } |
---|
1129 | 1089 | EXPORT_SYMBOL_GPL(extcon_dev_free); |
---|
1130 | 1090 | |
---|
1131 | | -#ifdef CONFIG_ARCH_ROCKCHIP |
---|
1132 | | -static const char *extcon_get_link_name(struct extcon_dev *edev) |
---|
1133 | | -{ |
---|
1134 | | - const char *dot = strchr(edev->name, '.'); |
---|
1135 | | - const char *name = dot + 1; |
---|
1136 | | - |
---|
1137 | | - if (!dot || !name || !(*name)) |
---|
1138 | | - name = edev->name; |
---|
1139 | | - |
---|
1140 | | - return name; |
---|
1141 | | -} |
---|
1142 | | -#endif |
---|
1143 | | - |
---|
1144 | 1091 | /** |
---|
1145 | 1092 | * extcon_dev_register() - Register an new extcon device |
---|
1146 | 1093 | * @edev: the extcon device to be registered |
---|
.. | .. |
---|
1191 | 1138 | (unsigned long)atomic_inc_return(&edev_no)); |
---|
1192 | 1139 | |
---|
1193 | 1140 | if (edev->max_supported) { |
---|
1194 | | - char buf[10]; |
---|
1195 | 1141 | char *str; |
---|
1196 | 1142 | struct extcon_cable *cable; |
---|
1197 | 1143 | |
---|
.. | .. |
---|
1205 | 1151 | for (index = 0; index < edev->max_supported; index++) { |
---|
1206 | 1152 | cable = &edev->cables[index]; |
---|
1207 | 1153 | |
---|
1208 | | - snprintf(buf, 10, "cable.%d", index); |
---|
1209 | | - str = kzalloc(strlen(buf) + 1, |
---|
1210 | | - GFP_KERNEL); |
---|
| 1154 | + str = kasprintf(GFP_KERNEL, "cable.%d", index); |
---|
1211 | 1155 | if (!str) { |
---|
1212 | 1156 | for (index--; index >= 0; index--) { |
---|
1213 | 1157 | cable = &edev->cables[index]; |
---|
.. | .. |
---|
1217 | 1161 | |
---|
1218 | 1162 | goto err_alloc_cables; |
---|
1219 | 1163 | } |
---|
1220 | | - strcpy(str, buf); |
---|
1221 | 1164 | |
---|
1222 | 1165 | cable->edev = edev; |
---|
1223 | 1166 | cable->cable_index = index; |
---|
.. | .. |
---|
1240 | 1183 | } |
---|
1241 | 1184 | |
---|
1242 | 1185 | if (edev->max_supported && edev->mutually_exclusive) { |
---|
1243 | | - char buf[80]; |
---|
1244 | 1186 | char *name; |
---|
1245 | 1187 | |
---|
1246 | 1188 | /* Count the size of mutually_exclusive array */ |
---|
.. | .. |
---|
1265 | 1207 | } |
---|
1266 | 1208 | |
---|
1267 | 1209 | for (index = 0; edev->mutually_exclusive[index]; index++) { |
---|
1268 | | - sprintf(buf, "0x%x", edev->mutually_exclusive[index]); |
---|
1269 | | - name = kzalloc(strlen(buf) + 1, |
---|
1270 | | - GFP_KERNEL); |
---|
| 1210 | + name = kasprintf(GFP_KERNEL, "0x%x", |
---|
| 1211 | + edev->mutually_exclusive[index]); |
---|
1271 | 1212 | if (!name) { |
---|
1272 | 1213 | for (index--; index >= 0; index--) { |
---|
1273 | 1214 | kfree(edev->d_attrs_muex[index].attr. |
---|
.. | .. |
---|
1278 | 1219 | ret = -ENOMEM; |
---|
1279 | 1220 | goto err_muex; |
---|
1280 | 1221 | } |
---|
1281 | | - strcpy(name, buf); |
---|
1282 | 1222 | sysfs_attr_init(&edev->d_attrs_muex[index].attr); |
---|
1283 | 1223 | edev->d_attrs_muex[index].attr.name = name; |
---|
1284 | 1224 | edev->d_attrs_muex[index].attr.mode = 0000; |
---|
.. | .. |
---|
1313 | 1253 | edev->dev.type = &edev->extcon_dev_type; |
---|
1314 | 1254 | } |
---|
1315 | 1255 | |
---|
1316 | | - ret = device_register(&edev->dev); |
---|
1317 | | - if (ret) { |
---|
1318 | | - put_device(&edev->dev); |
---|
1319 | | - goto err_dev; |
---|
1320 | | - } |
---|
1321 | | - |
---|
1322 | 1256 | spin_lock_init(&edev->lock); |
---|
1323 | | - edev->nh = devm_kcalloc(&edev->dev, edev->max_supported, |
---|
1324 | | - sizeof(*edev->nh), GFP_KERNEL); |
---|
1325 | | - if (!edev->nh) { |
---|
1326 | | - ret = -ENOMEM; |
---|
1327 | | - device_unregister(&edev->dev); |
---|
1328 | | - goto err_dev; |
---|
1329 | | - } |
---|
1330 | | - |
---|
1331 | | - edev->bnh = devm_kzalloc(&edev->dev, |
---|
1332 | | - sizeof(*edev->bnh) * edev->max_supported, GFP_KERNEL); |
---|
1333 | | - if (!edev->bnh) { |
---|
1334 | | - ret = -ENOMEM; |
---|
1335 | | - goto err_dev; |
---|
| 1257 | + if (edev->max_supported) { |
---|
| 1258 | + edev->nh = kcalloc(edev->max_supported, sizeof(*edev->nh), |
---|
| 1259 | + GFP_KERNEL); |
---|
| 1260 | + if (!edev->nh) { |
---|
| 1261 | + ret = -ENOMEM; |
---|
| 1262 | + goto err_alloc_nh; |
---|
| 1263 | + } |
---|
1336 | 1264 | } |
---|
1337 | 1265 | |
---|
1338 | 1266 | for (index = 0; index < edev->max_supported; index++) |
---|
.. | .. |
---|
1343 | 1271 | dev_set_drvdata(&edev->dev, edev); |
---|
1344 | 1272 | edev->state = 0; |
---|
1345 | 1273 | |
---|
| 1274 | + ret = device_register(&edev->dev); |
---|
| 1275 | + if (ret) { |
---|
| 1276 | + put_device(&edev->dev); |
---|
| 1277 | + goto err_dev; |
---|
| 1278 | + } |
---|
| 1279 | + |
---|
1346 | 1280 | mutex_lock(&extcon_dev_list_lock); |
---|
1347 | 1281 | list_add(&edev->entry, &extcon_dev_list); |
---|
1348 | 1282 | mutex_unlock(&extcon_dev_list_lock); |
---|
1349 | 1283 | |
---|
1350 | | -#ifdef CONFIG_ARCH_ROCKCHIP |
---|
1351 | | - { |
---|
1352 | | - const char *name = extcon_get_link_name(edev); |
---|
1353 | | - |
---|
1354 | | - ret = sysfs_create_link_nowarn(&edev->dev.class->p->subsys.kobj, |
---|
1355 | | - &edev->dev.kobj, name); |
---|
1356 | | - if (ret) |
---|
1357 | | - dev_err(&edev->dev, |
---|
1358 | | - "failed to create extcon %s link\n", name); |
---|
1359 | | - } |
---|
1360 | | -#endif |
---|
1361 | | - |
---|
1362 | 1284 | return 0; |
---|
1363 | 1285 | |
---|
1364 | 1286 | err_dev: |
---|
| 1287 | + if (edev->max_supported) |
---|
| 1288 | + kfree(edev->nh); |
---|
| 1289 | +err_alloc_nh: |
---|
1365 | 1290 | if (edev->max_supported) |
---|
1366 | 1291 | kfree(edev->extcon_dev_type.groups); |
---|
1367 | 1292 | err_alloc_groups: |
---|
.. | .. |
---|
1406 | 1331 | return; |
---|
1407 | 1332 | } |
---|
1408 | 1333 | |
---|
1409 | | -#ifdef CONFIG_ARCH_ROCKCHIP |
---|
1410 | | - sysfs_delete_link(&edev->dev.class->p->subsys.kobj, |
---|
1411 | | - &edev->dev.kobj, extcon_get_link_name(edev)); |
---|
1412 | | -#endif |
---|
1413 | | - |
---|
1414 | 1334 | device_unregister(&edev->dev); |
---|
1415 | 1335 | |
---|
1416 | 1336 | if (edev->mutually_exclusive && edev->max_supported) { |
---|
.. | .. |
---|
1427 | 1347 | if (edev->max_supported) { |
---|
1428 | 1348 | kfree(edev->extcon_dev_type.groups); |
---|
1429 | 1349 | kfree(edev->cables); |
---|
| 1350 | + kfree(edev->nh); |
---|
1430 | 1351 | } |
---|
1431 | 1352 | |
---|
1432 | 1353 | put_device(&edev->dev); |
---|