From patchwork Thu Dec 7 09:42:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lars-Peter Clausen X-Patchwork-Id: 10097913 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 0AD5860329 for ; Thu, 7 Dec 2017 09:42:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EFF882A3D0 for ; Thu, 7 Dec 2017 09:42:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E3A462A3D2; Thu, 7 Dec 2017 09:42:19 +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=-6.9 required=2.0 tests=BAYES_00,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 CFFD92A3D0 for ; Thu, 7 Dec 2017 09:42:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750967AbdLGJmS (ORCPT ); Thu, 7 Dec 2017 04:42:18 -0500 Received: from www381.your-server.de ([78.46.137.84]:51529 "EHLO www381.your-server.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750899AbdLGJmR (ORCPT ); Thu, 7 Dec 2017 04:42:17 -0500 Received: from [88.198.220.130] (helo=sslproxy01.your-server.de) by www381.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.85_2) (envelope-from ) id 1eMsh5-0004dF-5V; Thu, 07 Dec 2017 10:42:15 +0100 Received: from [2003:86:2c29:b900:8200:bff:fe9b:6612] (helo=lars-laptop.ad.analog.com) by sslproxy01.your-server.de with esmtpsa (TLSv1.2:DHE-RSA-AES128-GCM-SHA256:128) (Exim 4.84_2) (envelope-from ) id 1eMsh4-0004ba-UN; Thu, 07 Dec 2017 10:42:15 +0100 From: Lars-Peter Clausen To: Jonathan Cameron Cc: Hartmut Knaack , Peter Meerwald-Stadler , Andy Shevchenko , linux-iio@vger.kernel.org, Lars-Peter Clausen Subject: [PATCH] iio: Handle enumerated properties with gaps Date: Thu, 7 Dec 2017 10:42:09 +0100 Message-Id: <20171207094209.14748-1-lars@metafoo.de> X-Mailer: git-send-email 2.11.0 X-Authenticated-Sender: lars@metafoo.de X-Virus-Scanned: Clear (ClamAV 0.99.2/24107/Thu Dec 7 03:16:40 2017) 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 Some enums might have gaps or reserved values in the middle of their value range. E.g. consider a 2-bit enum where the values 0, 1 and 3 have a meaning, but 2 is a reserved value and must not be used. Add support for such enums to the IIO enum helper functions. A reserved value is marked by setting its entry in the items array to NULL rather than the normal descriptive string value. Unfortunately that means we can no longer use __sysfs_match_string() since it treats a NULL entry as the array terminator. But the amount of boilerplate is manageable. Signed-off-by: Lars-Peter Clausen --- Open for suggestions on how to massage __sysfs_match_string() into handling this use-case. --- drivers/iio/industrialio-core.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index 2e8e36f9da61..e69bef13d005 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -446,8 +446,12 @@ ssize_t iio_enum_available_read(struct iio_dev *indio_dev, if (!e->num_items) return 0; - for (i = 0; i < e->num_items; ++i) + for (i = 0; i < e->num_items; ++i) { + if (!e->items[i]) + continue; + len += scnprintf(buf + len, PAGE_SIZE - len, "%s ", e->items[i]); + } /* replace last space with a newline */ buf[len - 1] = '\n'; @@ -468,7 +472,7 @@ ssize_t iio_enum_read(struct iio_dev *indio_dev, i = e->get(indio_dev, chan); if (i < 0) return i; - else if (i >= e->num_items) + else if (i >= e->num_items || !e->items[i]) return -EINVAL; return snprintf(buf, PAGE_SIZE, "%s\n", e->items[i]); @@ -480,16 +484,21 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev, size_t len) { const struct iio_enum *e = (const struct iio_enum *)priv; + unsigned int i; int ret; if (!e->set) return -EINVAL; - ret = __sysfs_match_string(e->items, e->num_items, buf); - if (ret < 0) - return ret; + for (i = 0; i < e->num_items; i++) { + if (e->items[i] && sysfs_streq(buf, e->items[i])) + break; + } + + if (i == e->num_items) + return -EINVAL; - ret = e->set(indio_dev, chan, ret); + ret = e->set(indio_dev, chan, i); return ret ? ret : len; } EXPORT_SYMBOL_GPL(iio_enum_write);