From patchwork Sun Nov 11 22:28:28 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Bianconi X-Patchwork-Id: 10677909 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 EE98E139B for ; Sun, 11 Nov 2018 23:48:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DE81529FE3 for ; Sun, 11 Nov 2018 23:48:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D1BB129FEF; Sun, 11 Nov 2018 23:48:59 +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 7EFE829FE3 for ; Sun, 11 Nov 2018 23:48:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733080AbeKLISm (ORCPT ); Mon, 12 Nov 2018 03:18:42 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:50250 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733030AbeKLISl (ORCPT ); Mon, 12 Nov 2018 03:18:41 -0500 Received: by mail-wm1-f66.google.com with SMTP id 124-v6so6736163wmw.0 for ; Sun, 11 Nov 2018 14:28:44 -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=WE0Zo3qyA6ieweIaGTBRgLeJfLIK3hieBQvrDYc71GI=; b=dEw3r9bkv02AAeZ86jVDUCaTjYGz0u1Ue4+cjKisQL+EWAqzxXhrNm91pdqJD1ijO1 sMiYetudRO9EoNVH5OIqkIAG6Nk8r5VnHi45K3F9UMHwYAx4i4P9ElFN1PNp/mriy5fo lRpYorfdW7tXelhEXvhRtYPX1SdvCxgwO3lADB5UxKkAW0pZDt1YFZp2IRamEUQGDhXw a1RhIe08JL++jTyOfUvjZbSk3Chm2c7UjzgolkzQMWeycF1s2AKy/w0V23ujPmYAKafH EbymhDQ4aZa8kj6e4/NC8Dc+uGymunCSIGP2pduAXv86FkkgdffZvj70zaEwPvlKcK15 7KOg== X-Gm-Message-State: AGRZ1gJ1lYh4NGq94FxEGp5vlaTTlsiiGIV9DaIUAiX0gXG+lLdBSrgB Fclk/Hw6n5izUosaotHPL9CRFA== X-Google-Smtp-Source: AJdET5dJQKgQ2Kr8xutwdxzVVYotmAXZH5YUrYbX2xpS9cDn9GlEEbkhnOo+VqQBVXYiNhJG9VTLVg== X-Received: by 2002:a1c:9706:: with SMTP id z6-v6mr6191697wmd.86.1541975323938; Sun, 11 Nov 2018 14:28:43 -0800 (PST) Received: from localhost.localdomain ([151.66.8.224]) by smtp.gmail.com with ESMTPSA id 6-v6sm3849939wmg.19.2018.11.11.14.28.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 11 Nov 2018 14:28:43 -0800 (PST) From: Lorenzo Bianconi To: jic23@kernel.org Cc: linux-iio@vger.kernel.org Subject: [PATCH] iio: imu: st_lsm6dsx: do not use a fixed read len in read_oneshot Date: Sun, 11 Nov 2018 23:28:28 +0100 Message-Id: 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 --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c | 31 +++++++++++++------- 1 file changed, 20 insertions(+), 11 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..b908df0380b3 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_shub.c @@ -27,6 +27,7 @@ #include #include #include +#include #include "st_lsm6dsx.h" @@ -432,31 +433,39 @@ 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 ret, delay, len = ch->scan_type.realbits >> 3; + u8 *data; - err = st_lsm6dsx_shub_set_enable(sensor, true); - if (err < 0) - return err; + ret = st_lsm6dsx_shub_set_enable(sensor, true); + if (ret < 0) + return ret; + + data = kmalloc(len, GFP_KERNEL); + if (!data) + return -ENOMEM; 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; + ret = st_lsm6dsx_shub_read(sensor, ch->address, data, len); + if (ret < 0) + goto out; st_lsm6dsx_shub_set_enable(sensor, false); switch (len) { case 2: - *val = (s16)le16_to_cpu(data); + *val = (s16)le16_to_cpu(*((__le16 *)data)); + ret = IIO_VAL_INT; break; default: - return -EINVAL; + ret = -EINVAL; + break; } - return IIO_VAL_INT; +out: + kfree(data); + return ret; } static int