]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
Input: melfas_mip4 - switch to using cleanup functions
authorDmitry Torokhov <dmitry.torokhov@gmail.com>
Wed, 29 May 2024 18:58:26 +0000 (11:58 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Wed, 25 Mar 2026 04:14:54 +0000 (21:14 -0700)
Start using __free() and guard() primitives to simplify the code
and error handling.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
drivers/input/touchscreen/melfas_mip4.c

index 8698842199089e8906d4ebdaf5d80c5c81854ad5..10fccf4e5bb17e76918843b59064200b5c412eba 100644 (file)
@@ -881,8 +881,6 @@ static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
                                const u8 *data, int length, u16 buf_addr)
 {
        u8 cmd[6];
-       u8 *data_buf;
-       u16 buf_offset;
        int ret;
        int error;
 
@@ -895,7 +893,8 @@ static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
                return -EINVAL;
        }
 
-       data_buf = kmalloc(2 + MIP4_BL_PACKET_SIZE, GFP_KERNEL);
+       u8 *data_buf __free(kfree) = kmalloc(2 + MIP4_BL_PACKET_SIZE,
+                                            GFP_KERNEL);
        if (!data_buf)
                return -ENOMEM;
 
@@ -908,7 +907,7 @@ static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
                error = ret < 0 ? ret : -EIO;
                dev_err(&ts->client->dev,
                        "Failed to send write page address: %d\n", error);
-               goto out;
+               return error;
        }
 
        /* Size */
@@ -920,11 +919,11 @@ static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
                error = ret < 0 ? ret : -EIO;
                dev_err(&ts->client->dev,
                        "Failed to send write page size: %d\n", error);
-               goto out;
+               return error;
        }
 
        /* Data */
-       for (buf_offset = 0;
+       for (int buf_offset = 0;
             buf_offset < length;
             buf_offset += MIP4_BL_PACKET_SIZE) {
                dev_dbg(&ts->client->dev,
@@ -939,7 +938,7 @@ static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
                        dev_err(&ts->client->dev,
                                "Failed to read chunk at %#04x (size %d): %d\n",
                                buf_offset, MIP4_BL_PACKET_SIZE, error);
-                       goto out;
+                       return error;
                }
        }
 
@@ -952,35 +951,21 @@ static int mip4_bl_program_page(struct mip4_ts *ts, int offset,
                error = ret < 0 ? ret : -EIO;
                dev_err(&ts->client->dev,
                        "Failed to send 'write' command: %d\n", error);
-               goto out;
+               return error;
        }
 
        /* Status */
        error = mip4_bl_read_status(ts);
+       if (error)
+               return error;
 
-out:
-       kfree(data_buf);
-       return error ? error : 0;
+       return 0;
 }
 
 static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
                               const u8 *data, int length, int buf_addr)
 {
        u8 cmd[8];
-       u8 *read_buf;
-       int buf_offset;
-       struct i2c_msg msg[] = {
-               {
-                       .addr = ts->client->addr,
-                       .flags = 0,
-                       .buf = cmd,
-                       .len = 2,
-               }, {
-                       .addr = ts->client->addr,
-                       .flags = I2C_M_RD,
-                       .len = MIP4_BL_PACKET_SIZE,
-               },
-       };
        int ret;
        int error;
 
@@ -1029,11 +1014,25 @@ static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
                return error;
 
        /* Read */
-       msg[1].buf = read_buf = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
+       u8 *read_buf __free(kfree) = kmalloc(MIP4_BL_PACKET_SIZE, GFP_KERNEL);
        if (!read_buf)
                return -ENOMEM;
 
-       for (buf_offset = 0;
+       struct i2c_msg msg[] = {
+               {
+                       .addr = ts->client->addr,
+                       .flags = 0,
+                       .buf = cmd,
+                       .len = 2,
+               }, {
+                       .addr = ts->client->addr,
+                       .flags = I2C_M_RD,
+                       .buf = read_buf,
+                       .len = MIP4_BL_PACKET_SIZE,
+               },
+       };
+
+       for (int buf_offset = 0;
             buf_offset < length;
             buf_offset += MIP4_BL_PACKET_SIZE) {
                dev_dbg(&ts->client->dev,
@@ -1046,7 +1045,7 @@ static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
                        dev_err(&ts->client->dev,
                                "Failed to read chunk at %#04x (size %d): %d\n",
                                buf_offset, MIP4_BL_PACKET_SIZE, error);
-                       break;
+                       return error;
                }
 
                if (memcmp(&data[buf_offset], read_buf, MIP4_BL_PACKET_SIZE)) {
@@ -1064,13 +1063,11 @@ static int mip4_bl_verify_page(struct mip4_ts *ts, int offset,
                                       DUMP_PREFIX_OFFSET, 16, 1,
                                       read_buf, MIP4_BL_PAGE_SIZE, false);
 #endif
-                       error = -EINVAL;
-                       break;
+                       return -EINVAL;
                }
        }
 
-       kfree(read_buf);
-       return error ? error : 0;
+       return 0;
 }
 
 /*
@@ -1290,9 +1287,9 @@ static ssize_t mip4_sysfs_fw_update(struct device *dev,
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct mip4_ts *ts = i2c_get_clientdata(client);
-       const struct firmware *fw;
        int error;
 
+       const struct firmware *fw __free(firmware) = NULL;
        error = request_firmware(&fw, ts->fw_name, dev);
        if (error) {
                dev_err(&ts->client->dev,
@@ -1306,14 +1303,9 @@ static ssize_t mip4_sysfs_fw_update(struct device *dev,
         * userspace opening and closing the device and also suspend/resume
         * transitions.
         */
-       mutex_lock(&ts->input->mutex);
+       guard(mutex)(&ts->input->mutex);
 
        error = mip4_execute_fw_update(ts, fw);
-
-       mutex_unlock(&ts->input->mutex);
-
-       release_firmware(fw);
-
        if (error) {
                dev_err(&ts->client->dev,
                        "Firmware update failed: %d\n", error);
@@ -1331,18 +1323,13 @@ static ssize_t mip4_sysfs_read_fw_version(struct device *dev,
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct mip4_ts *ts = i2c_get_clientdata(client);
-       size_t count;
 
        /* Take lock to prevent racing with firmware update */
-       mutex_lock(&ts->input->mutex);
-
-       count = sysfs_emit(buf, "%04X %04X %04X %04X\n",
-                          ts->fw_version.boot, ts->fw_version.core,
-                          ts->fw_version.app, ts->fw_version.param);
+       guard(mutex)(&ts->input->mutex);
 
-       mutex_unlock(&ts->input->mutex);
-
-       return count;
+       return sysfs_emit(buf, "%04X %04X %04X %04X\n",
+                         ts->fw_version.boot, ts->fw_version.core,
+                         ts->fw_version.app, ts->fw_version.param);
 }
 
 static DEVICE_ATTR(fw_version, S_IRUGO, mip4_sysfs_read_fw_version, NULL);
@@ -1353,21 +1340,16 @@ static ssize_t mip4_sysfs_read_hw_version(struct device *dev,
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct mip4_ts *ts = i2c_get_clientdata(client);
-       size_t count;
 
        /* Take lock to prevent racing with firmware update */
-       mutex_lock(&ts->input->mutex);
+       guard(mutex)(&ts->input->mutex);
 
        /*
         * product_name shows the name or version of the hardware
         * paired with current firmware in the chip.
         */
-       count = sysfs_emit(buf, "%.*s\n",
-                          (int)sizeof(ts->product_name), ts->product_name);
-
-       mutex_unlock(&ts->input->mutex);
-
-       return count;
+       return sysfs_emit(buf, "%.*s\n",
+                         (int)sizeof(ts->product_name), ts->product_name);
 }
 
 static DEVICE_ATTR(hw_version, S_IRUGO, mip4_sysfs_read_hw_version, NULL);
@@ -1378,15 +1360,10 @@ static ssize_t mip4_sysfs_read_product_id(struct device *dev,
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct mip4_ts *ts = i2c_get_clientdata(client);
-       size_t count;
-
-       mutex_lock(&ts->input->mutex);
 
-       count = sysfs_emit(buf, "%04X\n", ts->product_id);
+       guard(mutex)(&ts->input->mutex);
 
-       mutex_unlock(&ts->input->mutex);
-
-       return count;
+       return sysfs_emit(buf, "%04X\n", ts->product_id);
 }
 
 static DEVICE_ATTR(product_id, S_IRUGO, mip4_sysfs_read_product_id, NULL);
@@ -1397,16 +1374,10 @@ static ssize_t mip4_sysfs_read_ic_name(struct device *dev,
 {
        struct i2c_client *client = to_i2c_client(dev);
        struct mip4_ts *ts = i2c_get_clientdata(client);
-       size_t count;
-
-       mutex_lock(&ts->input->mutex);
-
-       count = sysfs_emit(buf, "%.*s\n",
-                          (int)sizeof(ts->ic_name), ts->ic_name);
 
-       mutex_unlock(&ts->input->mutex);
+       guard(mutex)(&ts->input->mutex);
 
-       return count;
+       return sysfs_emit(buf, "%.*s\n", (int)sizeof(ts->ic_name), ts->ic_name);
 }
 
 static DEVICE_ATTR(ic_name, S_IRUGO, mip4_sysfs_read_ic_name, NULL);
@@ -1520,15 +1491,13 @@ static int mip4_suspend(struct device *dev)
        struct mip4_ts *ts = i2c_get_clientdata(client);
        struct input_dev *input = ts->input;
 
-       mutex_lock(&input->mutex);
+       guard(mutex)(&input->mutex);
 
        if (device_may_wakeup(dev))
                ts->wake_irq_enabled = enable_irq_wake(client->irq) == 0;
        else if (input_device_enabled(input))
                mip4_disable(ts);
 
-       mutex_unlock(&input->mutex);
-
        return 0;
 }
 
@@ -1538,15 +1507,13 @@ static int mip4_resume(struct device *dev)
        struct mip4_ts *ts = i2c_get_clientdata(client);
        struct input_dev *input = ts->input;
 
-       mutex_lock(&input->mutex);
+       guard(mutex)(&input->mutex);
 
        if (ts->wake_irq_enabled)
                disable_irq_wake(client->irq);
        else if (input_device_enabled(input))
                mip4_enable(ts);
 
-       mutex_unlock(&input->mutex);
-
        return 0;
 }