From patchwork Mon Apr 22 08:32:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 10910845 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 5002B14DB for ; Mon, 22 Apr 2019 08:33:07 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 43328285DA for ; Mon, 22 Apr 2019 08:33:07 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 36DDF286A8; Mon, 22 Apr 2019 08:33:07 +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,DKIM_SIGNED, DKIM_VALID,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 A9D66285DA for ; Mon, 22 Apr 2019 08:33:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726943AbfDVIdG (ORCPT ); Mon, 22 Apr 2019 04:33:06 -0400 Received: from mail-eopbgr680084.outbound.protection.outlook.com ([40.107.68.84]:21745 "EHLO NAM04-BN3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726305AbfDVIdG (ORCPT ); Mon, 22 Apr 2019 04:33:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.onmicrosoft.com; s=selector1-analog-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VvvsQMCjsasU5tAUu8hT/EiBUDeUN01UFrU/q/KvUxU=; b=cScyQXF2Esfd8gSg3Gz9WAaI8ZkUwIEq9ivxM9pZYPx1rRJtK5Sf405GrO7BuVkWYwJTz4VvcTb3dtWqIraRRM+JP+zes+4f9/ZryLZP9gABpwsraZicf1ChslILNNFlthhWvw4moovWqo/VcSE0Ac4MIilY9laH6Ug52q3epLQ= Received: from MWHPR03CA0032.namprd03.prod.outlook.com (2603:10b6:301:3b::21) by BL2PR03MB548.namprd03.prod.outlook.com (2a01:111:e400:c26::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1813.16; Mon, 22 Apr 2019 08:33:02 +0000 Received: from BL2NAM02FT064.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e46::206) by MWHPR03CA0032.outlook.office365.com (2603:10b6:301:3b::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.1813.11 via Frontend Transport; Mon, 22 Apr 2019 08:33:02 +0000 Authentication-Results: spf=pass (sender IP is 137.71.25.55) smtp.mailfrom=analog.com; linuxfoundation.org; dkim=none (message not signed) header.d=none;linuxfoundation.org; dmarc=bestguesspass action=none header.from=analog.com; Received-SPF: Pass (protection.outlook.com: domain of analog.com designates 137.71.25.55 as permitted sender) receiver=protection.outlook.com; client-ip=137.71.25.55; helo=nwd2mta1.analog.com; Received: from nwd2mta1.analog.com (137.71.25.55) by BL2NAM02FT064.mail.protection.outlook.com (10.152.77.119) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.1771.16 via Frontend Transport; Mon, 22 Apr 2019 08:33:01 +0000 Received: from NWD2HUBCAS7.ad.analog.com (nwd2hubcas7.ad.analog.com [10.64.69.107]) by nwd2mta1.analog.com (8.13.8/8.13.8) with ESMTP id x3M8X1Zw020948 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=OK); Mon, 22 Apr 2019 01:33:01 -0700 Received: from saturn.analog.com (10.50.1.244) by NWD2HUBCAS7.ad.analog.com (10.64.69.107) with Microsoft SMTP Server id 14.3.408.0; Mon, 22 Apr 2019 04:33:00 -0400 From: Alexandru Ardelean To: , CC: , , , , Alexandru Ardelean Subject: [PATCH 1/2] lib: add __sysfs_match_string_with_gaps() helper Date: Mon, 22 Apr 2019 11:32:56 +0300 Message-ID: <20190422083257.21805-1-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-ADIRoutedOnPrem: True X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:137.71.25.55;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(376002)(39860400002)(346002)(396003)(136003)(2980300002)(199004)(189003)(356004)(6666004)(48376002)(51416003)(7696005)(246002)(478600001)(305945005)(8676002)(70586007)(70206006)(50226002)(7636002)(107886003)(1076003)(16586007)(47776003)(316002)(50466002)(8936002)(110136005)(106002)(54906003)(186003)(36756003)(336012)(53416004)(126002)(476003)(77096007)(2616005)(426003)(26005)(86362001)(44832011)(5660300002)(2906002)(486006)(4326008);DIR:OUT;SFP:1101;SCL:1;SRVR:BL2PR03MB548;H:nwd2mta1.analog.com;FPR:;SPF:Pass;LANG:en;PTR:nwd2mail10.analog.com;A:1;MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cad51cb2-bc30-4e5a-fdf2-08d6c6fd21f1 X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600141)(711020)(4605104)(4709054)(2017052603328);SRVR:BL2PR03MB548; X-MS-TrafficTypeDiagnostic: BL2PR03MB548: X-Microsoft-Antispam-PRVS: X-Forefront-PRVS: 00159D1518 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: 2Nt9MsowzA0qnSeNuvjNsQC0Gx1trYK547g8sG1ug5Bcwpd+nBLgoWBgJoyc0lvfvYExVp8XFSGcGv44Y3cijyFR2Fi2UGW8oQIxHNtxUy1L85x50l2TigwAU3iZzFlsO8KpfDOoic+nHIz8yImxzoFr0E+OxvCfQzpJ7fsH344Bdqd5SZ2wPdpsQ3ZmUPW5OAn6xth/aTE0/zvvhSES9QtSiEVFvO9iuwOviaEHJ0hb5L6HCzOUWJLEW79BD5giXMx6FxmfURjCOMSpjCz7t4EITeDjuh1vwZsHF4S/vJT7pj9hYickS0PNFUNiF54GiJ5Qnc6RT+B2ZZIVVKUVx4odfwn9xs2UhsBygx7pabTd7OR6Kp0c6gy56Q+aDsv6j2bnnZxhLaLuVcEQq5EcZliC2u5b5g4hERIucJiLeJo= X-OriginatorOrg: analog.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2019 08:33:01.7035 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cad51cb2-bc30-4e5a-fdf2-08d6c6fd21f1 X-MS-Exchange-CrossTenant-Id: eaa689b4-8f87-40e0-9c6f-7228de4d754a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=eaa689b4-8f87-40e0-9c6f-7228de4d754a;Ip=[137.71.25.55];Helo=[nwd2mta1.analog.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2PR03MB548 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 This helper is similar to __sysfs_match_string() with the exception that it ignores NULL elements within the array. It takes an extra parameter (called `gaps`) which when true will ignore the NULL elements. When false, this function behaves exactly like `__sysfs_match_string()`. Signed-off-by: Alexandru Ardelean Acked-by: Greg Kroah-Hartman --- include/linux/string.h | 9 ++++++++- lib/string.c | 14 ++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/include/linux/string.h b/include/linux/string.h index 7927b875f80c..30595ed483dc 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -189,7 +189,14 @@ static inline int strtobool(const char *s, bool *res) } int match_string(const char * const *array, size_t n, const char *string); -int __sysfs_match_string(const char * const *array, size_t n, const char *s); +int __sysfs_match_string_with_gaps(const char * const *array, size_t n, + const char *str, bool gaps); + +static inline int __sysfs_match_string(const char * const *array, size_t n, + const char *str) +{ + return __sysfs_match_string_with_gaps(array, n, str, false); +} /** * sysfs_match_string - matches given string in an array diff --git a/lib/string.c b/lib/string.c index 38e4ca08e757..8ddac3cd292a 100644 --- a/lib/string.c +++ b/lib/string.c @@ -659,30 +659,36 @@ int match_string(const char * const *array, size_t n, const char *string) EXPORT_SYMBOL(match_string); /** - * __sysfs_match_string - matches given string in an array + * __sysfs_match_string_with_gaps - matches string in array ignoring NULLs * @array: array of strings * @n: number of strings in the array or -1 for NULL terminated arrays * @str: string to match with + * @gaps: boolean to ignore NULL elements within the array * * Returns index of @str in the @array or -EINVAL, just like match_string(). * Uses sysfs_streq instead of strcmp for matching. + * Ignores NULL pointers within the @array if @gaps is true. */ -int __sysfs_match_string(const char * const *array, size_t n, const char *str) +int __sysfs_match_string_with_gaps(const char * const *array, size_t n, + const char *str, bool gaps) { const char *item; int index; for (index = 0; index < n; index++) { item = array[index]; - if (!item) + if (!item) { + if (gaps) + continue; break; + } if (sysfs_streq(item, str)) return index; } return -EINVAL; } -EXPORT_SYMBOL(__sysfs_match_string); +EXPORT_SYMBOL(__sysfs_match_string_with_gaps); #ifndef __HAVE_ARCH_MEMSET /** From patchwork Mon Apr 22 08:32:57 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandru Ardelean X-Patchwork-Id: 10910847 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 16B7A13B5 for ; Mon, 22 Apr 2019 08:33:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0912E285DA for ; Mon, 22 Apr 2019 08:33:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F0C66286A1; Mon, 22 Apr 2019 08:33:14 +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,DKIM_SIGNED, DKIM_VALID,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 3E98028698 for ; Mon, 22 Apr 2019 08:33:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727014AbfDVIdJ (ORCPT ); Mon, 22 Apr 2019 04:33:09 -0400 Received: from mail-eopbgr800048.outbound.protection.outlook.com ([40.107.80.48]:39648 "EHLO NAM03-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726305AbfDVIdI (ORCPT ); Mon, 22 Apr 2019 04:33:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=analog.onmicrosoft.com; s=selector1-analog-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=1yBexuzVHOs4Xl9IDTEICF83tw/DsO3H8xqNPFXRUtw=; b=ILxsmNRNlp5U5QChzSirgch909xZx3wrsaWZSAjREPUYXcqbxbr5muWrrnSObKhyw784okAqg5galudx3juUwp8hLJi6Cr1FxDfuKGfrJeYJ0lDjOvd6vp+7bQX43ICQ6uqGg5JPR3Pyg8pIHwxo5+/726j9qoBKxA62eDIhUu4= Received: from BN8PR03CA0016.namprd03.prod.outlook.com (2603:10b6:408:94::29) by BL2PR03MB545.namprd03.prod.outlook.com (2a01:111:e400:c23::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1813.17; Mon, 22 Apr 2019 08:33:04 +0000 Received: from BL2NAM02FT056.eop-nam02.prod.protection.outlook.com (2a01:111:f400:7e46::207) by BN8PR03CA0016.outlook.office365.com (2603:10b6:408:94::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1813.12 via Frontend Transport; Mon, 22 Apr 2019 08:33:03 +0000 Authentication-Results: spf=pass (sender IP is 137.71.25.55) smtp.mailfrom=analog.com; linuxfoundation.org; dkim=none (message not signed) header.d=none;linuxfoundation.org; dmarc=bestguesspass action=none header.from=analog.com; Received-SPF: Pass (protection.outlook.com: domain of analog.com designates 137.71.25.55 as permitted sender) receiver=protection.outlook.com; client-ip=137.71.25.55; helo=nwd2mta1.analog.com; Received: from nwd2mta1.analog.com (137.71.25.55) by BL2NAM02FT056.mail.protection.outlook.com (10.152.77.221) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.1771.16 via Frontend Transport; Mon, 22 Apr 2019 08:33:03 +0000 Received: from NWD2HUBCAS7.ad.analog.com (nwd2hubcas7.ad.analog.com [10.64.69.107]) by nwd2mta1.analog.com (8.13.8/8.13.8) with ESMTP id x3M8X3im020958 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=OK); Mon, 22 Apr 2019 01:33:03 -0700 Received: from saturn.analog.com (10.50.1.244) by NWD2HUBCAS7.ad.analog.com (10.64.69.107) with Microsoft SMTP Server id 14.3.408.0; Mon, 22 Apr 2019 04:33:02 -0400 From: Alexandru Ardelean To: , CC: , , , , Lars-Peter Clausen , Alexandru Ardelean Subject: [PATCH 2/2] iio: Handle enumerated properties with gaps Date: Mon, 22 Apr 2019 11:32:57 +0300 Message-ID: <20190422083257.21805-2-alexandru.ardelean@analog.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190422083257.21805-1-alexandru.ardelean@analog.com> References: <20190422083257.21805-1-alexandru.ardelean@analog.com> MIME-Version: 1.0 X-ADIRoutedOnPrem: True X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:137.71.25.55;IPV:NLI;CTRY:US;EFV:NLI;SFV:NSPM;SFS:(10009020)(39860400002)(346002)(376002)(136003)(396003)(2980300002)(199004)(189003)(48376002)(4326008)(107886003)(106002)(486006)(478600001)(47776003)(50466002)(110136005)(54906003)(16586007)(316002)(186003)(7696005)(246002)(51416003)(1076003)(446003)(126002)(476003)(2616005)(53416004)(11346002)(426003)(336012)(44832011)(86362001)(356004)(5660300002)(8936002)(77096007)(8676002)(6666004)(76176011)(70586007)(2906002)(305945005)(7636002)(70206006)(50226002)(36756003)(26005);DIR:OUT;SFP:1101;SCL:1;SRVR:BL2PR03MB545;H:nwd2mta1.analog.com;FPR:;SPF:Pass;LANG:en;PTR:nwd2mail10.analog.com;A:1;MX:1; X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: eb835ff8-6bfc-4115-4257-08d6c6fd232a X-Microsoft-Antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600141)(711020)(4605104)(4709054)(2017052603328);SRVR:BL2PR03MB545; X-MS-TrafficTypeDiagnostic: BL2PR03MB545: X-Microsoft-Antispam-PRVS: X-Forefront-PRVS: 00159D1518 X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: 67ZiIUG28s+4Ump804UP/e/EDIcrWy0x1aS/xqsBcBki0X9Ghxa67po7GoRjF7O+5UYIINTGX8V1PWUAfHxTpf3AgQSPUa/4J4YEsaruvdSOD7D/RhdTnRTNJmnP5OJMm32Ld6IiOq+9AEwUbPI1Ga5ozCbdtdGoXDsLKgKi0FybZhaLp39JZ7c9tNb35O69hnchZOK2lWDF747laBAyxfr0sRQmZrDvLpes0vvXGdQzAV1ZdMfxAgDVvBZnENgPqvbs0CkSN7LYBshr2y2JFcD1sIl1VGSUh0UrPKkpK8S8XsT1NKXkVcFNaugLrTngmh+B/y8hQAk3RrUKtW62pPrr+ZbtQkpZBZ+TIW0d146ShYIJ4CaQ3T4LBqE/ZLSgJ3SQnskYUeFaLZSZPv9HI3ZNp71WYs/mBSERZzKBssY= X-OriginatorOrg: analog.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2019 08:33:03.7500 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eb835ff8-6bfc-4115-4257-08d6c6fd232a X-MS-Exchange-CrossTenant-Id: eaa689b4-8f87-40e0-9c6f-7228de4d754a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=eaa689b4-8f87-40e0-9c6f-7228de4d754a;Ip=[137.71.25.55];Helo=[nwd2mta1.analog.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL2PR03MB545 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 From: Lars-Peter Clausen 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 can not be used. Add support for such enums to the IIO enum helper functions. A reserved values is marked by setting its entry in the items array to NULL rather than the normal descriptive string value. Signed-off-by: Lars-Peter Clausen Signed-off-by: Alexandru Ardelean --- drivers/iio/industrialio-core.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index f2ebca65f964..c141a29bf446 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c @@ -447,8 +447,11 @@ 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'; @@ -469,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]); @@ -486,7 +489,8 @@ ssize_t iio_enum_write(struct iio_dev *indio_dev, if (!e->set) return -EINVAL; - ret = __sysfs_match_string(e->items, e->num_items, buf); + ret = __sysfs_match_string_with_gaps(e->items, e->num_items, + buf, true); if (ret < 0) return ret;