From patchwork Sat Nov 17 18:04:27 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Bianconi X-Patchwork-Id: 10687557 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 72CD514BD for ; Sat, 17 Nov 2018 18:04:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5942C2A946 for ; Sat, 17 Nov 2018 18:04:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4A4502A977; Sat, 17 Nov 2018 18:04:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 378D22A946 for ; Sat, 17 Nov 2018 18:04:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726600AbeKREWF (ORCPT ); Sat, 17 Nov 2018 23:22:05 -0500 Received: from mail-wr1-f67.google.com ([209.85.221.67]:45624 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726548AbeKREWE (ORCPT ); Sat, 17 Nov 2018 23:22:04 -0500 Received: by mail-wr1-f67.google.com with SMTP id v6so6095882wrr.12 for ; Sat, 17 Nov 2018 10:04:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5cRYmOFcuYsFNHN+6NibCnZ5al5pNXGKiYcyoE9Nrn8=; b=gfWPZOQs+SrKqcYRFtOfUCiIg1MQWERKI5hy44y5FbOBt+Z1Z5nwHvxY9SOh4F1z76 qSsLBwgywI722pA/Gzb/dbvQlsiXp2nCOGW3PH+8ciz4dkIRrdURdh4B5zZ5gnrSIPgg ycDHMiqk9GQzrAhhB5VMzuqwgBcAsrXMfvpjf4EDw07Pmqme3lEd0jxKNXdbUXfBMHOT /sZO3ULZbOcNMdmZPpnCEaA+Pg+viUHD5PVcH47nS+kCT9RyiFjENNPG94jUcH8IZvr2 r5hJxSnPfCMBchM2osoWMAlNscQg9v8cyyLgGEPjO7j4+1nReFNI8dRtGXUx0Rn4p5Pk 6tPg== X-Gm-Message-State: AGRZ1gK6/cIXEv5YKKhwpGThmV7twA/lwR0+oVsXBvHSdMMlZXR+FbAs mWbv3DItuzhL2AYDR5m1TOSxRA== X-Google-Smtp-Source: AJdET5dCz8DfeKXyXV071HBaV70PIloBkffGgXA63YTZ7GPYWkD43QitlHmcrAFJCTJUa/LbvJFEyA== X-Received: by 2002:adf:bb44:: with SMTP id x4mr13543043wrg.24.1542477876193; Sat, 17 Nov 2018 10:04:36 -0800 (PST) Received: from localhost.localdomain ([151.21.135.68]) by smtp.gmail.com with ESMTPSA id d4sm26640420wrp.89.2018.11.17.10.04.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Nov 2018 10:04:35 -0800 (PST) From: Lorenzo Bianconi To: jic23@kernel.org Cc: linux-iio@vger.kernel.org Subject: [PATCH v2] iio: imu: st_lsm6dsx: do not use a fixed read len in read_oneshot Date: Sat, 17 Nov 2018 19:04:27 +0100 Message-Id: <2eefd3a4e60701fef40f31eacd2e79c1cffe6311.1542477399.git.lorenzo.bianconi@redhat.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: References: MIME-Version: 1.0 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Generalize st_lsm6dsx_shub_read_oneshot in order to not use a fixed read length and take into account iio channel realbits for single read operations Signed-off-by: Lorenzo Bianconi --- Changes since v1: - allocate data buffer on the stack instead of using kmalloc - stop the sensor even if the read operation fails --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c index ee59b0cac84f..8e47dccdd40f 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c @@ -432,8 +432,8 @@ st_lsm6dsx_shub_read_oneshot(struct st_lsm6dsx_sensor *sensor, struct iio_chan_spec const *ch, int *val) { - int err, delay, len = ch->scan_type.realbits >> 3; - __le16 data; + int err, delay, len; + u8 data[4]; err = st_lsm6dsx_shub_set_enable(sensor, true); if (err < 0) @@ -442,15 +442,17 @@ st_lsm6dsx_shub_read_oneshot(struct st_lsm6dsx_sensor *sensor, delay = 1000000 / sensor->odr; usleep_range(delay, 2 * delay); - err = st_lsm6dsx_shub_read(sensor, ch->address, (u8 *)&data, len); - if (err < 0) - return err; + len = min_t(int, sizeof(data), ch->scan_type.realbits >> 3); + err = st_lsm6dsx_shub_read(sensor, ch->address, data, len); st_lsm6dsx_shub_set_enable(sensor, false); + if (err < 0) + return err; + switch (len) { case 2: - *val = (s16)le16_to_cpu(data); + *val = (s16)le16_to_cpu(*((__le16 *)data)); break; default: return -EINVAL;