Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Input: ims-pcu - switch to using cleanup functions

Start using __free() and guard() primitives to simplify the code
and error handling.

Link: https://lore.kernel.org/r/20240610041813.722445-2-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+63 -74
+63 -74
drivers/input/misc/ims-pcu.c
··· 927 927 goto out; 928 928 } 929 929 930 - mutex_lock(&pcu->cmd_mutex); 931 - ims_pcu_handle_firmware_update(pcu, fw); 932 - mutex_unlock(&pcu->cmd_mutex); 930 + scoped_guard(mutex, &pcu->cmd_mutex) 931 + ims_pcu_handle_firmware_update(pcu, fw); 933 932 934 933 release_firmware(fw); 935 934 ··· 952 953 __le16 br_val = cpu_to_le16(value); 953 954 int error; 954 955 955 - mutex_lock(&pcu->cmd_mutex); 956 + guard(mutex)(&pcu->cmd_mutex); 956 957 957 958 error = ims_pcu_execute_command(pcu, SET_BRIGHTNESS, 958 959 &br_val, sizeof(br_val)); ··· 960 961 dev_warn(pcu->dev, 961 962 "Failed to set desired brightness %u, error: %d\n", 962 963 value, error); 963 - 964 - mutex_unlock(&pcu->cmd_mutex); 965 964 966 965 return error; 967 966 } ··· 974 977 int brightness; 975 978 int error; 976 979 977 - mutex_lock(&pcu->cmd_mutex); 980 + guard(mutex)(&pcu->cmd_mutex); 978 981 979 982 error = ims_pcu_execute_query(pcu, GET_BRIGHTNESS); 980 983 if (error) { ··· 987 990 brightness = 988 991 get_unaligned_le16(&pcu->cmd_buf[IMS_PCU_DATA_OFFSET]); 989 992 } 990 - 991 - mutex_unlock(&pcu->cmd_mutex); 992 993 993 994 return brightness; 994 995 } ··· 1067 1072 if (data_len > attr->field_length) 1068 1073 return -EINVAL; 1069 1074 1070 - error = mutex_lock_interruptible(&pcu->cmd_mutex); 1071 - if (error) 1072 - return error; 1075 + scoped_cond_guard(mutex, return -EINTR, &pcu->cmd_mutex) { 1076 + memset(field, 0, attr->field_length); 1077 + memcpy(field, buf, data_len); 1073 1078 1074 - memset(field, 0, attr->field_length); 1075 - memcpy(field, buf, data_len); 1079 + error = ims_pcu_set_info(pcu); 1076 1080 1077 - error = ims_pcu_set_info(pcu); 1081 + /* 1082 + * Even if update failed, let's fetch the info again as we just 1083 + * clobbered one of the fields. 1084 + */ 1085 + ims_pcu_get_info(pcu); 1078 1086 1079 - /* 1080 - * Even if update failed, let's fetch the info again as we just 1081 - * clobbered one of the fields. 1082 - */ 1083 - ims_pcu_get_info(pcu); 1087 + if (error) 1088 + return error; 1089 + } 1084 1090 1085 - mutex_unlock(&pcu->cmd_mutex); 1086 - 1087 - return error < 0 ? error : count; 1091 + return count; 1088 1092 } 1089 1093 1090 1094 #define IMS_PCU_ATTR(_field, _mode) \ ··· 1146 1152 { 1147 1153 struct usb_interface *intf = to_usb_interface(dev); 1148 1154 struct ims_pcu *pcu = usb_get_intfdata(intf); 1149 - const struct firmware *fw = NULL; 1150 1155 int value; 1151 1156 int error; 1152 1157 ··· 1156 1163 if (value != 1) 1157 1164 return -EINVAL; 1158 1165 1159 - error = mutex_lock_interruptible(&pcu->cmd_mutex); 1160 - if (error) 1161 - return error; 1162 - 1166 + const struct firmware *fw __free(firmware) = NULL; 1163 1167 error = request_ihex_firmware(&fw, IMS_PCU_FIRMWARE_NAME, pcu->dev); 1164 1168 if (error) { 1165 1169 dev_err(pcu->dev, "Failed to request firmware %s, error: %d\n", 1166 1170 IMS_PCU_FIRMWARE_NAME, error); 1167 - goto out; 1171 + return error; 1168 1172 } 1169 1173 1170 - /* 1171 - * If we are already in bootloader mode we can proceed with 1172 - * flashing the firmware. 1173 - * 1174 - * If we are in application mode, then we need to switch into 1175 - * bootloader mode, which will cause the device to disconnect 1176 - * and reconnect as different device. 1177 - */ 1178 - if (pcu->bootloader_mode) 1179 - error = ims_pcu_handle_firmware_update(pcu, fw); 1180 - else 1181 - error = ims_pcu_switch_to_bootloader(pcu); 1174 + scoped_cond_guard(mutex_intr, return -EINTR, &pcu->cmd_mutex) { 1175 + /* 1176 + * If we are already in bootloader mode we can proceed with 1177 + * flashing the firmware. 1178 + * 1179 + * If we are in application mode, then we need to switch into 1180 + * bootloader mode, which will cause the device to disconnect 1181 + * and reconnect as different device. 1182 + */ 1183 + if (pcu->bootloader_mode) 1184 + error = ims_pcu_handle_firmware_update(pcu, fw); 1185 + else 1186 + error = ims_pcu_switch_to_bootloader(pcu); 1182 1187 1183 - release_firmware(fw); 1188 + if (error) 1189 + return error; 1190 + } 1184 1191 1185 - out: 1186 - mutex_unlock(&pcu->cmd_mutex); 1187 - return error ?: count; 1192 + return count; 1188 1193 } 1189 1194 1190 1195 static DEVICE_ATTR(update_firmware, S_IWUSR, ··· 1292 1301 int error; 1293 1302 u8 data; 1294 1303 1295 - mutex_lock(&pcu->cmd_mutex); 1296 - error = ims_pcu_read_ofn_config(pcu, pcu->ofn_reg_addr, &data); 1297 - mutex_unlock(&pcu->cmd_mutex); 1298 - 1299 - if (error) 1300 - return error; 1304 + scoped_guard(mutex, &pcu->cmd_mutex) { 1305 + error = ims_pcu_read_ofn_config(pcu, pcu->ofn_reg_addr, &data); 1306 + if (error) 1307 + return error; 1308 + } 1301 1309 1302 1310 return sysfs_emit(buf, "%x\n", data); 1303 1311 } ··· 1314 1324 if (error) 1315 1325 return error; 1316 1326 1317 - mutex_lock(&pcu->cmd_mutex); 1318 - error = ims_pcu_write_ofn_config(pcu, pcu->ofn_reg_addr, value); 1319 - mutex_unlock(&pcu->cmd_mutex); 1327 + guard(mutex)(&pcu->cmd_mutex); 1320 1328 1321 - return error ?: count; 1329 + error = ims_pcu_write_ofn_config(pcu, pcu->ofn_reg_addr, value); 1330 + if (error) 1331 + return error; 1332 + 1333 + return count; 1322 1334 } 1323 1335 1324 1336 static DEVICE_ATTR(reg_data, S_IRUGO | S_IWUSR, ··· 1332 1340 { 1333 1341 struct usb_interface *intf = to_usb_interface(dev); 1334 1342 struct ims_pcu *pcu = usb_get_intfdata(intf); 1335 - int error; 1336 1343 1337 - mutex_lock(&pcu->cmd_mutex); 1338 - error = sysfs_emit(buf, "%x\n", pcu->ofn_reg_addr); 1339 - mutex_unlock(&pcu->cmd_mutex); 1344 + guard(mutex)(&pcu->cmd_mutex); 1340 1345 1341 - return error; 1346 + return sysfs_emit(buf, "%x\n", pcu->ofn_reg_addr); 1342 1347 } 1343 1348 1344 1349 static ssize_t ims_pcu_ofn_reg_addr_store(struct device *dev, ··· 1351 1362 if (error) 1352 1363 return error; 1353 1364 1354 - mutex_lock(&pcu->cmd_mutex); 1365 + guard(mutex)(&pcu->cmd_mutex); 1366 + 1355 1367 pcu->ofn_reg_addr = value; 1356 - mutex_unlock(&pcu->cmd_mutex); 1357 1368 1358 1369 return count; 1359 1370 } ··· 1378 1389 int error; 1379 1390 u8 data; 1380 1391 1381 - mutex_lock(&pcu->cmd_mutex); 1382 - error = ims_pcu_read_ofn_config(pcu, attr->addr, &data); 1383 - mutex_unlock(&pcu->cmd_mutex); 1384 - 1385 - if (error) 1386 - return error; 1392 + scoped_guard(mutex, &pcu->cmd_mutex) { 1393 + error = ims_pcu_read_ofn_config(pcu, attr->addr, &data); 1394 + if (error) 1395 + return error; 1396 + } 1387 1397 1388 1398 return sysfs_emit(buf, "%d\n", !!(data & (1 << attr->nr))); 1389 1399 } ··· 1406 1418 if (value > 1) 1407 1419 return -EINVAL; 1408 1420 1409 - mutex_lock(&pcu->cmd_mutex); 1421 + scoped_guard(mutex, &pcu->cmd_mutex) { 1422 + error = ims_pcu_read_ofn_config(pcu, attr->addr, &data); 1423 + if (error) 1424 + return error; 1410 1425 1411 - error = ims_pcu_read_ofn_config(pcu, attr->addr, &data); 1412 - if (!error) { 1413 1426 if (value) 1414 1427 data |= 1U << attr->nr; 1415 1428 else 1416 1429 data &= ~(1U << attr->nr); 1417 1430 1418 1431 error = ims_pcu_write_ofn_config(pcu, attr->addr, data); 1432 + if (error) 1433 + return error; 1419 1434 } 1420 1435 1421 - mutex_unlock(&pcu->cmd_mutex); 1422 - 1423 - return error ?: count; 1436 + return count; 1424 1437 } 1425 1438 1426 1439 #define IMS_PCU_OFN_BIT_ATTR(_field, _addr, _nr) \