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: melfas_mip4 - switch to using cleanup functions

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

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+44 -77
+44 -77
drivers/input/touchscreen/melfas_mip4.c
··· 881 881 const u8 *data, int length, u16 buf_addr) 882 882 { 883 883 u8 cmd[6]; 884 - u8 *data_buf; 885 - u16 buf_offset; 886 884 int ret; 887 885 int error; 888 886 ··· 893 895 return -EINVAL; 894 896 } 895 897 896 - data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL); 898 + u8 *data_buf __free(kfree) = kmalloc(2 + MIP4_BL_PACKET_SIZE, 899 + GFP_KERNEL); 897 900 if (!data_buf) 898 901 return -ENOMEM; 899 902 ··· 907 908 error = ret < 0 ? ret : -EIO; 908 909 dev_err(&ts->client->dev, 909 910 "Failed to send write page address: %d\n", error); 910 - goto out; 911 + return error; 911 912 } 912 913 913 914 /* Size */ ··· 919 920 error = ret < 0 ? ret : -EIO; 920 921 dev_err(&ts->client->dev, 921 922 "Failed to send write page size: %d\n", error); 922 - goto out; 923 + return error; 923 924 } 924 925 925 926 /* Data */ 926 - for (buf_offset = 0; 927 + for (int buf_offset = 0; 927 928 buf_offset < length; 928 929 buf_offset += MIP4_BL_PACKET_SIZE) { 929 930 dev_dbg(&ts->client->dev, ··· 938 939 dev_err(&ts->client->dev, 939 940 "Failed to read chunk at %#04x (size %d): %d\n", 940 941 buf_offset, MIP4_BL_PACKET_SIZE, error); 941 - goto out; 942 + return error; 942 943 } 943 944 } 944 945 ··· 951 952 error = ret < 0 ? ret : -EIO; 952 953 dev_err(&ts->client->dev, 953 954 "Failed to send 'write' command: %d\n", error); 954 - goto out; 955 + return error; 955 956 } 956 957 957 958 /* Status */ 958 959 error = mip4_bl_read_status(ts); 960 + if (error) 961 + return error; 959 962 960 - out: 961 - kfree(data_buf); 962 - return error ? error : 0; 963 + return 0; 963 964 } 964 965 965 966 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset, 966 967 const u8 *data, int length, int buf_addr) 967 968 { 968 969 u8 cmd[8]; 969 - u8 *read_buf; 970 - int buf_offset; 971 - struct i2c_msg msg[] = { 972 - { 973 - .addr = ts->client->addr, 974 - .flags = 0, 975 - .buf = cmd, 976 - .len = 2, 977 - }, { 978 - .addr = ts->client->addr, 979 - .flags = I2C_M_RD, 980 - .len = MIP4_BL_PACKET_SIZE, 981 - }, 982 - }; 983 970 int ret; 984 971 int error; 985 972 ··· 1014 1029 return error; 1015 1030 1016 1031 /* Read */ 1017 - msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL); 1032 + u8 *read_buf __free(kfree) = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL); 1018 1033 if (!read_buf) 1019 1034 return -ENOMEM; 1020 1035 1021 - for (buf_offset = 0; 1036 + struct i2c_msg msg[] = { 1037 + { 1038 + .addr = ts->client->addr, 1039 + .flags = 0, 1040 + .buf = cmd, 1041 + .len = 2, 1042 + }, { 1043 + .addr = ts->client->addr, 1044 + .flags = I2C_M_RD, 1045 + .buf = read_buf, 1046 + .len = MIP4_BL_PACKET_SIZE, 1047 + }, 1048 + }; 1049 + 1050 + for (int buf_offset = 0; 1022 1051 buf_offset < length; 1023 1052 buf_offset += MIP4_BL_PACKET_SIZE) { 1024 1053 dev_dbg(&ts->client->dev, ··· 1045 1046 dev_err(&ts->client->dev, 1046 1047 "Failed to read chunk at %#04x (size %d): %d\n", 1047 1048 buf_offset, MIP4_BL_PACKET_SIZE, error); 1048 - break; 1049 + return error; 1049 1050 } 1050 1051 1051 1052 if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) { ··· 1063 1064 DUMP_PREFIX_OFFSET, 16, 1, 1064 1065 read_buf, MIP4_BL_PAGE_SIZE, false); 1065 1066 #endif 1066 - error = -EINVAL; 1067 - break; 1067 + return -EINVAL; 1068 1068 } 1069 1069 } 1070 1070 1071 - kfree(read_buf); 1072 - return error ? error : 0; 1071 + return 0; 1073 1072 } 1074 1073 1075 1074 /* ··· 1287 1290 { 1288 1291 struct i2c_client *client = to_i2c_client(dev); 1289 1292 struct mip4_ts *ts = i2c_get_clientdata(client); 1290 - const struct firmware *fw; 1291 1293 int error; 1292 1294 1295 + const struct firmware *fw __free(firmware) = NULL; 1293 1296 error = request_firmware(&fw, ts->fw_name, dev); 1294 1297 if (error) { 1295 1298 dev_err(&ts->client->dev, ··· 1303 1306 * userspace opening and closing the device and also suspend/resume 1304 1307 * transitions. 1305 1308 */ 1306 - mutex_lock(&ts->input->mutex); 1309 + guard(mutex)(&ts->input->mutex); 1307 1310 1308 1311 error = mip4_execute_fw_update(ts, fw); 1309 - 1310 - mutex_unlock(&ts->input->mutex); 1311 - 1312 - release_firmware(fw); 1313 - 1314 1312 if (error) { 1315 1313 dev_err(&ts->client->dev, 1316 1314 "Firmware update failed: %d\n", error); ··· 1323 1331 { 1324 1332 struct i2c_client *client = to_i2c_client(dev); 1325 1333 struct mip4_ts *ts = i2c_get_clientdata(client); 1326 - size_t count; 1327 1334 1328 1335 /* Take lock to prevent racing with firmware update */ 1329 - mutex_lock(&ts->input->mutex); 1336 + guard(mutex)(&ts->input->mutex); 1330 1337 1331 - count = sysfs_emit(buf, "%04X %04X %04X %04X\n", 1332 - ts->fw_version.boot, ts->fw_version.core, 1333 - ts->fw_version.app, ts->fw_version.param); 1334 - 1335 - mutex_unlock(&ts->input->mutex); 1336 - 1337 - return count; 1338 + return sysfs_emit(buf, "%04X %04X %04X %04X\n", 1339 + ts->fw_version.boot, ts->fw_version.core, 1340 + ts->fw_version.app, ts->fw_version.param); 1338 1341 } 1339 1342 1340 1343 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL); ··· 1340 1353 { 1341 1354 struct i2c_client *client = to_i2c_client(dev); 1342 1355 struct mip4_ts *ts = i2c_get_clientdata(client); 1343 - size_t count; 1344 1356 1345 1357 /* Take lock to prevent racing with firmware update */ 1346 - mutex_lock(&ts->input->mutex); 1358 + guard(mutex)(&ts->input->mutex); 1347 1359 1348 1360 /* 1349 1361 * product_name shows the name or version of the hardware 1350 1362 * paired with current firmware in the chip. 1351 1363 */ 1352 - count = sysfs_emit(buf, "%.*s\n", 1353 - (int)sizeof(ts->product_name), ts->product_name); 1354 - 1355 - mutex_unlock(&ts->input->mutex); 1356 - 1357 - return count; 1364 + return sysfs_emit(buf, "%.*s\n", 1365 + (int)sizeof(ts->product_name), ts->product_name); 1358 1366 } 1359 1367 1360 1368 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL); ··· 1360 1378 { 1361 1379 struct i2c_client *client = to_i2c_client(dev); 1362 1380 struct mip4_ts *ts = i2c_get_clientdata(client); 1363 - size_t count; 1364 1381 1365 - mutex_lock(&ts->input->mutex); 1382 + guard(mutex)(&ts->input->mutex); 1366 1383 1367 - count = sysfs_emit(buf, "%04X\n", ts->product_id); 1368 - 1369 - mutex_unlock(&ts->input->mutex); 1370 - 1371 - return count; 1384 + return sysfs_emit(buf, "%04X\n", ts->product_id); 1372 1385 } 1373 1386 1374 1387 static DEVICE_ATTR(product_id, S_IRUGO, mip4_sysfs_read_product_id, NULL); ··· 1374 1397 { 1375 1398 struct i2c_client *client = to_i2c_client(dev); 1376 1399 struct mip4_ts *ts = i2c_get_clientdata(client); 1377 - size_t count; 1378 1400 1379 - mutex_lock(&ts->input->mutex); 1401 + guard(mutex)(&ts->input->mutex); 1380 1402 1381 - count = sysfs_emit(buf, "%.*s\n", 1382 - (int)sizeof(ts->ic_name), ts->ic_name); 1383 - 1384 - mutex_unlock(&ts->input->mutex); 1385 - 1386 - return count; 1403 + return sysfs_emit(buf, "%.*s\n", (int)sizeof(ts->ic_name), ts->ic_name); 1387 1404 } 1388 1405 1389 1406 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL); ··· 1491 1520 struct mip4_ts *ts = i2c_get_clientdata(client); 1492 1521 struct input_dev *input = ts->input; 1493 1522 1494 - mutex_lock(&input->mutex); 1523 + guard(mutex)(&input->mutex); 1495 1524 1496 1525 if (device_may_wakeup(dev)) 1497 1526 ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0; 1498 1527 else if (input_device_enabled(input)) 1499 1528 mip4_disable(ts); 1500 - 1501 - mutex_unlock(&input->mutex); 1502 1529 1503 1530 return 0; 1504 1531 } ··· 1507 1538 struct mip4_ts *ts = i2c_get_clientdata(client); 1508 1539 struct input_dev *input = ts->input; 1509 1540 1510 - mutex_lock(&input->mutex); 1541 + guard(mutex)(&input->mutex); 1511 1542 1512 1543 if (ts->wake_irq_enabled) 1513 1544 disable_irq_wake(client->irq); 1514 1545 else if (input_device_enabled(input)) 1515 1546 mip4_enable(ts); 1516 - 1517 - mutex_unlock(&input->mutex); 1518 1547 1519 1548 return 0; 1520 1549 }