From patchwork Fri Oct 8 08:17:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538254 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=OYmhEfwe; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzh1Pxlz9t0p for ; Fri, 8 Oct 2021 19:17:52 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233767AbhJHITo (ORCPT ); Fri, 8 Oct 2021 04:19:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233072AbhJHITk (ORCPT ); Fri, 8 Oct 2021 04:19:40 -0400 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5B88C061762 for ; Fri, 8 Oct 2021 01:17:45 -0700 (PDT) Received: by mail-wr1-x434.google.com with SMTP id r10so27144301wra.12 for ; Fri, 08 Oct 2021 01:17:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=JDZbXCl9bSoGyZWgwef05HDv8VNeS1jh792KUo3neRA=; b=OYmhEfweByggqpQU57fPOKHqFV9b2B7AkrLhzr6cE913ljzUap38E2A/Sf1/3qIrS9 EEJp33XQyD0KaM1D+/D57E/6ouWlMXcXk3Uy+oiNqT3Uvt7VBiOoScrAzwZ/tUdMEOqT O0Ym1rpYreAii4UkdKMOIHqU12zOpVRHEA+jB6X6aAYBEqpqVg+2HDeCYRfxC2zgJt6V mmJ58UDOJKxqxVe3jKx1SiM14C3Ytx5sfLcjY9qgLKWk2LXOF/mhDZBdCbvsT5N10qQT jno+owKxqWysxQSQSIDHwSYhgFFAyQM5/E8Kkalnki2LJEcKEo7rTFMqtM+XDifn0jH3 9v/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JDZbXCl9bSoGyZWgwef05HDv8VNeS1jh792KUo3neRA=; b=VUF05SfD+xlSgWNL8pfrC2rexqhGdyQn2o7Ah7ga+Slzu41lxu/+N8hjmMpKycNJVR g4xeZBn3BZk2AuODv2uoofJVSi9gn5gli/phJkKmm/7z7zglSWK9LgUF8bNtYsSbAzFY Z2Lq/rZUocK68z+XXN5CdqgyFivQG8jLnOtHxAOsFPfg9QQScBlpnDOAbNPZndIx9vig ALT00638Q+LFqvY/DjVMAayFp1/dBzF+ibmMlPkKHmUwVj7Y3wu0yr/+vXTj20wWv/tA vxBltCAmWUstLnSNhuYcVRKh9MoNhqKjyJfJunvOwktHAWqV7jbsMEchn6DnKwK68ADR 3trg== X-Gm-Message-State: AOAM531UMVlGwAKwLm/vOhcgwrs3In6CFa6XiJlHRDR8iGZ+cz58tEpM EmcuWfgeXKNb0Y0VKicQtE3lLg== X-Google-Smtp-Source: ABdhPJyZdS6TI7AjqLam2wc2FQYnHz+kaNFqNZoXR9Dh3OIT7M7DNUAdw6stcvV7m+6nd9Uat4gGww== X-Received: by 2002:a05:600c:2212:: with SMTP id z18mr472321wml.39.1633681064467; Fri, 08 Oct 2021 01:17:44 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:44 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 1/8] configfs: increase the item name length Date: Fri, 8 Oct 2021 10:17:32 +0200 Message-Id: <20211008081739.26807-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org 20 characters limit for item name is relatively small. Let's increase it to 32 to fit '04-committable-children' - a name we'll use in the sample code for committable items. Signed-off-by: Bartosz Golaszewski Acked-by: Linus Walleij Acked-by: Andy Shevchenko --- include/linux/configfs.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/configfs.h b/include/linux/configfs.h index 97cfd13bae51..e398fb2e83b2 100644 --- a/include/linux/configfs.h +++ b/include/linux/configfs.h @@ -25,7 +25,7 @@ #include /* struct kref */ #include /* struct mutex */ -#define CONFIGFS_ITEM_NAME_LEN 20 +#define CONFIGFS_ITEM_NAME_LEN 32 struct module; From patchwork Fri Oct 8 08:17:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538253 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=vGw3X8yO; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzg55QXz9t0k for ; Fri, 8 Oct 2021 19:17:51 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229877AbhJHITn (ORCPT ); Fri, 8 Oct 2021 04:19:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46222 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233463AbhJHITm (ORCPT ); Fri, 8 Oct 2021 04:19:42 -0400 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D144BC061760 for ; Fri, 8 Oct 2021 01:17:46 -0700 (PDT) Received: by mail-wr1-x433.google.com with SMTP id t2so27119123wrb.8 for ; Fri, 08 Oct 2021 01:17:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=N6iEJB4pcSblX4SdZfQ+pj8u1PpQ7EP4J6z216v/Emw=; b=vGw3X8yO89TItyj+jMgmELxwnXdGLcyFfexOwH/fc6JFQPHL/+8pAOe7Pl8wV2ZJzG RbH3vOUv4O9mCg6OlDscJwtdd97Lun/ak0v/RzFscaNocOU3nioAbzSkFMyz5PW9a7x1 c+/YqujqZHKvl1rKE562fwHUrzEgLv/EKkZuXDhfFFTr9lN2bBBe6O4WnzhTSeqolpCD Y7raqkZtQppZk7UJgZsfKZxD3zDrOSp5tvG+SOlxxJlANsQk+oZ4frMQInpmNJkxHo1N WMAntgt3zHGEHxVw+9L5OTvTMwQ2Mc6fSpkEj1R4q2Adkpv1cCedkT+DVVBGQyGCLs6L BNkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=N6iEJB4pcSblX4SdZfQ+pj8u1PpQ7EP4J6z216v/Emw=; b=55e+yLkDI84taMWWRBjGPOAA0yESGaN1hJwcF1diK/po66auk6yfCKVhpqvlfEhNeO t+PdtFavhA2Qgsg/HhtoUQDbrMlLBBl4jGGyCm3JjmFDQpqHv9qNevpQ14KJjjxnzijV LSq0mHNF9T5KluiovMLWjCMC6BX74+eLk6E5N/eUfGcBaxqrBjfq9Ego3GGTCHiTgvB3 F6akY/DdiKTd/ydXSe3jq6S6THDEyex17T4U+ump+nM3Z0QoHBgcIhAeodQ0JlNQnzcD j2K5QFXn2Ml8GYLMhpdOF9IUT1o2XyT7STHPXYRcJKt9S4Hl2JiGfoKp+Hj5iH6vkPJt nE7g== X-Gm-Message-State: AOAM533UrkPF08MDbOIzi4FI2UauMpcy48ZXxEUH85KrIbChFgZk2RZj dgVyO1pyJwIL1Mn4G7HGfXeQNg== X-Google-Smtp-Source: ABdhPJxo1cevac5/0rSCw2w5/xBAnVBjveraUMEl/Ke4EJgcGSnm5lbaUbum9WqkZEt7vylTFBZz9A== X-Received: by 2002:a05:600c:1d1f:: with SMTP id l31mr1971391wms.44.1633681065285; Fri, 08 Oct 2021 01:17:45 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:45 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 2/8] configfs: use (1UL << bit) for internal flags Date: Fri, 8 Oct 2021 10:17:33 +0200 Message-Id: <20211008081739.26807-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org For better readability and maintenance: use the (1UL << bit) for flag definitions. Signed-off-by: Bartosz Golaszewski Acked-by: Linus Walleij --- fs/configfs/configfs_internal.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/fs/configfs/configfs_internal.h b/fs/configfs/configfs_internal.h index c0395363eab9..0c7a3d857fde 100644 --- a/fs/configfs/configfs_internal.h +++ b/fs/configfs/configfs_internal.h @@ -44,16 +44,16 @@ struct configfs_dirent { struct configfs_fragment *s_frag; }; -#define CONFIGFS_ROOT 0x0001 -#define CONFIGFS_DIR 0x0002 -#define CONFIGFS_ITEM_ATTR 0x0004 -#define CONFIGFS_ITEM_BIN_ATTR 0x0008 -#define CONFIGFS_ITEM_LINK 0x0020 -#define CONFIGFS_USET_DIR 0x0040 -#define CONFIGFS_USET_DEFAULT 0x0080 -#define CONFIGFS_USET_DROPPING 0x0100 -#define CONFIGFS_USET_IN_MKDIR 0x0200 -#define CONFIGFS_USET_CREATING 0x0400 +#define CONFIGFS_ROOT (1UL << 0) +#define CONFIGFS_DIR (1UL << 1) +#define CONFIGFS_ITEM_ATTR (1UL << 2) +#define CONFIGFS_ITEM_BIN_ATTR (1UL << 3) +#define CONFIGFS_ITEM_LINK (1UL << 5) +#define CONFIGFS_USET_DIR (1UL << 6) +#define CONFIGFS_USET_DEFAULT (1UL << 7) +#define CONFIGFS_USET_DROPPING (1UL << 8) +#define CONFIGFS_USET_IN_MKDIR (1UL << 9) +#define CONFIGFS_USET_CREATING (1UL << 10) #define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR) extern struct mutex configfs_symlink_mutex; From patchwork Fri Oct 8 08:17:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538255 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=lWxtmJH9; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzj1zrbz9sP7 for ; Fri, 8 Oct 2021 19:17:53 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233846AbhJHITp (ORCPT ); Fri, 8 Oct 2021 04:19:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46236 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233541AbhJHITm (ORCPT ); Fri, 8 Oct 2021 04:19:42 -0400 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B414EC061755 for ; Fri, 8 Oct 2021 01:17:47 -0700 (PDT) Received: by mail-wr1-x42c.google.com with SMTP id t2so27119235wrb.8 for ; Fri, 08 Oct 2021 01:17:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pSX6xpIY0vqw/EiCB6+AarjCoI5aNC4wN0qJcbQixSM=; b=lWxtmJH9uZKH4sUkIcsXHWhBBCPn3ylJqvULz2tKtPKMtLNVdiHYlBPdCuwxKupSBx RCGVSKm3MHUI0paq2LYFlHM7lxzrvX1bv/ZvDM/Eq8+CBQHRAcoFkFgcL2kXnMDr8tfs gIQitncYuoAXiZBloxJ8lQ0n3C+WdeBAcgiuWUXRey7Uh8MDm4HLiUHuVBqeWwAzPJ5m /uBQbciVREUXJsXag3VYfHOCnqiSqioR1TAPjfiiUlqPHw4gQDrDALuF2kQv7/mLbJW0 QjelcH5HOnMNuy1sOXUjnr3O4+8P54skcLAtV2UZGslV43Lyg9xWhExH17Z4MJHDoLwW k3pQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pSX6xpIY0vqw/EiCB6+AarjCoI5aNC4wN0qJcbQixSM=; b=Ktzj/PJFxiG1lCXqprWEd6EvX2xOno0cJjrEpNstC8Xda/nKeRAlpZOtEvnVjC12bs uvUYfqvYiwCfRhYRJepocB+Yd6kB0PBHVwRgRraRJq4mPBHEieVXw7p1kKCZiOTLsbx5 qVrL0fjIAVd5Z0VfbCib7LKxNCDt/w3Xv0b3r60FT0KZ+FKDWGjloNems6zRxoq0vm/I p8skHyudiSmjvwouBOPj6hB7YPpITsoi69ItXCLOm0X3cWpYHwI2qXvjAkJCyUpSyfVp uJEBIvTFWH95ifVRn9tbTLJfRu/wnz4NwSiARx5auZgBBRyBEH9sPhZpGkYnw+d9Rlsc Cw+g== X-Gm-Message-State: AOAM532quwPyAAX1MaNaIvdkJut6KoZruxachAnPnd2xXewhLyTyLRHe +eVkMSy4DPfFq0CVpbzwkaPb5Q== X-Google-Smtp-Source: ABdhPJxChOvnWaWtDZLCGkgmgovLN6o7NJVOPVUN3zFR7rarAv88BSM8M5IznbPh1efoJO3Ln6z6sQ== X-Received: by 2002:a05:600c:4e93:: with SMTP id f19mr1934262wmq.185.1633681066133; Fri, 08 Oct 2021 01:17:46 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:45 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 3/8] configfs: implement committable items Date: Fri, 8 Oct 2021 10:17:34 +0200 Message-Id: <20211008081739.26807-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This implements configfs committable items. We mostly follow the documentation except that we extend config_group_ops with uncommit_item() callback for reverting the changes made by commit_item(). Each committable group has two sub-directories: pending and live. New items can only be created in pending/. Attributes can only be modified while the item is in pending/. Once it's ready to be committed, it must be moved over to live/ using the rename() system call. This is when the commit_item() function will be called. Implementation-wise: we reuse the default group mechanism to elegantly plug the new pseude-groups into configfs. The pending group inherits the parent group's operations so that config_items can be seamlesly created in it using the callbacks supplied by the user as part of the committable group itself. Signed-off-by: Bartosz Golaszewski Acked-by: Linus Walleij Acked-by: Andy Shevchenko --- Documentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 2 + fs/configfs/dir.c | 276 ++++++++++++++++++++++++- include/linux/configfs.h | 1 + 4 files changed, 275 insertions(+), 10 deletions(-) diff --git a/Documentation/filesystems/configfs.rst b/Documentation/filesystems/configfs.rst index 1d3d6f4a82a9..7e0e7c356450 100644 --- a/Documentation/filesystems/configfs.rst +++ b/Documentation/filesystems/configfs.rst @@ -290,6 +290,7 @@ config_item_type:: struct config_group *(*make_group)(struct config_group *group, const char *name); int (*commit_item)(struct config_item *item); + int (*uncommit_item)(struct config_item *item); void (*disconnect_notify)(struct config_group *group, struct config_item *item); void (*drop_item)(struct config_group *group, @@ -490,9 +491,6 @@ pass up an error. Committable Items ================= -Note: - Committable items are currently unimplemented. - Some config_items cannot have a valid initial state. That is, no default values can be specified for the item's attributes such that the item can do its work. Userspace must configure one or more attributes, @@ -532,4 +530,4 @@ method returns zero and the item is moved to the "live" directory. As rmdir(2) does not work in the "live" directory, an item must be shutdown, or "uncommitted". Again, this is done via rename(2), this time from the "live" directory back to the "pending" one. The subsystem -is notified by the ct_group_ops->uncommit_object() method. +is notified by the ct_group_ops->uncommit_item() method. diff --git a/fs/configfs/configfs_internal.h b/fs/configfs/configfs_internal.h index 0c7a3d857fde..7dc0f2754c7e 100644 --- a/fs/configfs/configfs_internal.h +++ b/fs/configfs/configfs_internal.h @@ -54,6 +54,8 @@ struct configfs_dirent { #define CONFIGFS_USET_DROPPING (1UL << 8) #define CONFIGFS_USET_IN_MKDIR (1UL << 9) #define CONFIGFS_USET_CREATING (1UL << 10) +#define CONFIGFS_GROUP_PENDING (1UL << 11) +#define CONFIGFS_GROUP_LIVE (1UL << 12) #define CONFIGFS_NOT_PINNED (CONFIGFS_ITEM_ATTR | CONFIGFS_ITEM_BIN_ATTR) extern struct mutex configfs_symlink_mutex; diff --git a/fs/configfs/dir.c b/fs/configfs/dir.c index 1466b5d01cbb..3251fb2ec010 100644 --- a/fs/configfs/dir.c +++ b/fs/configfs/dir.c @@ -42,6 +42,14 @@ static void configfs_d_iput(struct dentry * dentry, if (sd) { /* Coordinate with configfs_readdir */ spin_lock(&configfs_dirent_lock); + + /* + * Free memory allocated for the pending and live directories + * of committable groups. + */ + if (sd->s_type & (CONFIGFS_GROUP_PENDING | CONFIGFS_GROUP_LIVE)) + kfree(sd->s_element); + /* * Set sd->s_dentry to null only when this dentry is the one * that is going to be killed. Otherwise configfs_d_iput may @@ -833,6 +841,134 @@ static void configfs_detach_item(struct config_item *item) configfs_remove_dir(item); } +static bool is_committable_group(struct config_item *item) +{ + const struct config_item_type *type = item->ci_type; + + if (type && type->ct_group_ops && + type->ct_group_ops->commit_item && + type->ct_group_ops->uncommit_item) + return true; + + return false; +} + +struct pending_group_data { + struct config_group group; + struct config_item_type type; + struct configfs_group_operations group_ops; +}; + +struct live_group_data { + struct config_group group; + struct config_item_type type; +}; + +static int create_pending_group(struct config_item *parent_item, + struct configfs_fragment *frag) +{ + const struct config_item_type *parent_type = parent_item->ci_type; + struct pending_group_data *pending; + struct configfs_dirent *sd; + int ret; + + pending = kzalloc(sizeof(*pending), GFP_KERNEL); + if (!pending) + return -ENOMEM; + + /* + * Let's inherit the group_ops from the parent except for item + * committing and uncommitting. + */ + memcpy(&pending->group_ops, parent_type->ct_group_ops, + sizeof(struct configfs_group_operations)); + pending->type.ct_group_ops = &pending->group_ops; + pending->type.ct_group_ops->commit_item = NULL; + pending->type.ct_group_ops->uncommit_item = NULL; + + /* Let's directly reuse item_ops. */ + pending->type.ct_item_ops = parent_type->ct_item_ops; + pending->type.ct_owner = parent_type->ct_owner; + + config_group_init_type_name(&pending->group, "pending", &pending->type); + + ret = create_default_group(to_config_group(parent_item), + &pending->group, frag); + if (ret) { + kfree(pending); + return ret; + } + + link_group(to_config_group(parent_item), &pending->group); + + sd = pending->group.cg_item.ci_dentry->d_fsdata; + /* Allow creating config_items in 'pending' group. */ + sd->s_type |= (CONFIGFS_GROUP_PENDING | CONFIGFS_USET_DIR); + + return 0; +} + +static int create_live_group(struct config_item *parent_item, + struct configfs_fragment *frag) +{ + struct live_group_data *live; + struct configfs_dirent *sd; + int ret; + + live = kzalloc(sizeof(*live), GFP_KERNEL); + if (!live) + return -ENOMEM; + + live->type.ct_owner = parent_item->ci_type->ct_owner; + + config_group_init_type_name(&live->group, "live", &live->type); + + ret = create_default_group(to_config_group(parent_item), + &live->group, frag); + if (ret) { + kfree(live); + return ret; + } + + link_group(to_config_group(parent_item), &live->group); + + sd = live->group.cg_item.ci_dentry->d_fsdata; + sd->s_type |= CONFIGFS_GROUP_LIVE; + sd->s_type &= ~CONFIGFS_USET_DIR; + + return 0; +} + +static int create_committable_groups(struct config_item *parent_item, + struct configfs_fragment *frag) +{ + struct configfs_dirent *sd; + int ret; + + ret = create_pending_group(parent_item, frag); + if (ret) + return ret; + + ret = create_live_group(parent_item, frag); + if (ret) { + detach_groups(to_config_group(parent_item)); + return ret; + } + + /* Disallow creating items directly in the committable group. */ + sd = parent_item->ci_dentry->d_fsdata; + sd->s_type &= ~CONFIGFS_USET_DIR; + + return 0; +} + +static void dentry_mark_dead(struct config_item *item, struct dentry *dentry) +{ + configfs_detach_item(item); + d_inode(dentry)->i_flags |= S_DEAD; + dont_mount(dentry); +} + static int configfs_attach_group(struct config_item *parent_item, struct config_item *item, struct dentry *dentry, @@ -858,11 +994,15 @@ static int configfs_attach_group(struct config_item *parent_item, inode_lock_nested(d_inode(dentry), I_MUTEX_CHILD); configfs_adjust_dir_dirent_depth_before_populate(sd); ret = populate_groups(to_config_group(item), frag); - if (ret) { - configfs_detach_item(item); - d_inode(dentry)->i_flags |= S_DEAD; - dont_mount(dentry); + if (ret) + dentry_mark_dead(item, dentry); + + if (is_committable_group(item)) { + ret = create_committable_groups(item, frag); + if (ret) + dentry_mark_dead(item, dentry); } + configfs_adjust_dir_dirent_depth_after_populate(sd); inode_unlock(d_inode(dentry)); if (ret) @@ -939,6 +1079,8 @@ static void configfs_dump_one(struct configfs_dirent *sd, int level) type_print(CONFIGFS_USET_DIR); type_print(CONFIGFS_USET_DEFAULT); type_print(CONFIGFS_USET_DROPPING); + type_print(CONFIGFS_GROUP_PENDING); + type_print(CONFIGFS_GROUP_LIVE); #undef type_print } @@ -1241,6 +1383,27 @@ int configfs_depend_item_unlocked(struct configfs_subsystem *caller_subsys, } EXPORT_SYMBOL(configfs_depend_item_unlocked); +static int committable_item_check_live_group(const char *new_item, + struct configfs_dirent *parent_sd) +{ + struct configfs_dirent *sd, *live = NULL; + + list_for_each_entry(sd, &parent_sd->s_children, s_sibling) { + if (strcmp(configfs_get_name(sd), "live") == 0) + live = sd; + } + + if (WARN_ON(!live)) + return -ENOENT; /* Something's wrong in the configfs code. */ + + list_for_each_entry(sd, &live->s_children, s_sibling) { + if (configfs_dirent_exists(live, new_item)) + return -EEXIST; + } + + return 0; +} + static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, struct dentry *dentry, umode_t mode) { @@ -1250,7 +1413,7 @@ static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, struct config_item *item = NULL; struct config_item *parent_item; struct configfs_subsystem *subsys; - struct configfs_dirent *sd; + struct configfs_dirent *sd, *parent_sd; const struct config_item_type *type; struct module *subsys_owner = NULL, *new_item_owner = NULL; struct configfs_fragment *frag; @@ -1272,6 +1435,14 @@ static int configfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, goto out; } + if (sd->s_type & CONFIGFS_GROUP_PENDING) { + parent_sd = dentry->d_parent->d_parent->d_fsdata; + ret = committable_item_check_live_group(dentry->d_name.name, + parent_sd); + if (ret) + goto out; + } + frag = new_fragment(); if (!frag) { ret = -ENOMEM; @@ -1430,7 +1601,7 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) struct config_item *parent_item; struct config_item *item; struct configfs_subsystem *subsys; - struct configfs_dirent *sd; + struct configfs_dirent *sd, *parent_sd; struct configfs_fragment *frag; struct module *subsys_owner = NULL, *dead_item_owner = NULL; int ret; @@ -1449,6 +1620,12 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) return -EINVAL; } + parent_sd = dentry->d_parent->d_fsdata; + if (parent_sd->s_type & CONFIGFS_GROUP_LIVE) { + config_item_put(parent_item); + return -EPERM; + } + /* configfs_mkdir() shouldn't have allowed this */ BUG_ON(!subsys->su_group.cg_item.ci_type); subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner; @@ -1535,9 +1712,96 @@ static int configfs_rmdir(struct inode *dir, struct dentry *dentry) return 0; } +static int configfs_rename(struct user_namespace *mnt_userns, + struct inode *old_dir, struct dentry *old_dentry, + struct inode *new_dir, struct dentry *new_dentry, + unsigned int flags) +{ + struct configfs_dirent *sd, *old_parent_sd, *new_parent_sd; + struct dentry *old_parent_dentry, *new_parent_dentry; + struct dentry *committable_group_dentry; + struct config_item *committable_group_item, *item, *new_parent_item; + struct configfs_subsystem *committable_group_subsys; + struct configfs_group_operations *committable_group_ops; + int ret = 0; + + if (flags) + return -EINVAL; + + old_parent_dentry = old_dentry->d_parent; + new_parent_dentry = new_dentry->d_parent; + + sd = old_dentry->d_fsdata; + old_parent_sd = old_dentry->d_parent->d_fsdata; + new_parent_sd = new_dentry->d_parent->d_fsdata; + + if (!old_parent_sd || !new_parent_sd) + return -EPERM; + + /* + * Renaming must always be between a 'pending' and a 'live' group and + * both need to have the same parent. Changing the directory name is + * not allowed. + */ + if (!((old_parent_sd->s_type & CONFIGFS_GROUP_PENDING) && + (new_parent_sd->s_type & CONFIGFS_GROUP_LIVE)) && + !((old_parent_sd->s_type & CONFIGFS_GROUP_LIVE) && + (new_parent_sd->s_type & CONFIGFS_GROUP_PENDING))) + return -EPERM; + + if (old_parent_dentry->d_parent != new_parent_dentry->d_parent) + return -EPERM; + + if (strcmp(old_dentry->d_name.name, new_dentry->d_name.name)) + return -EPERM; + + committable_group_dentry = old_parent_dentry->d_parent; + /* + * Grab a reference to the committable group for the duration of + * this function. + */ + committable_group_item = + configfs_get_config_item(committable_group_dentry); + committable_group_subsys = + to_config_group(committable_group_item)->cg_subsys; + committable_group_ops = committable_group_item->ci_type->ct_group_ops; + + item = sd->s_element; + new_parent_item = new_parent_sd->s_element; + + if (WARN_ON(!is_committable_group(committable_group_item))) { + /* This would be a result of a programming error in configfs. */ + config_item_put(committable_group_item); + return -EPERM; + } + + mutex_lock(&committable_group_subsys->su_mutex); + + if ((old_parent_sd->s_type & CONFIGFS_GROUP_PENDING) && + (new_parent_sd->s_type & CONFIGFS_GROUP_LIVE)) + ret = committable_group_ops->commit_item(item); + else + ret = committable_group_ops->uncommit_item(item); + if (ret) + goto out; + + spin_lock(&configfs_dirent_lock); + new_dentry->d_fsdata = configfs_get(sd); + item->ci_parent = new_parent_item; + list_move(&sd->s_sibling, &new_parent_sd->s_children); + spin_unlock(&configfs_dirent_lock); + +out: + mutex_unlock(&committable_group_subsys->su_mutex); + config_item_put(committable_group_item); + + return ret; +} + const struct inode_operations configfs_dir_inode_operations = { .mkdir = configfs_mkdir, .rmdir = configfs_rmdir, + .rename = configfs_rename, .symlink = configfs_symlink, .unlink = configfs_unlink, .lookup = configfs_lookup, diff --git a/include/linux/configfs.h b/include/linux/configfs.h index e398fb2e83b2..3c3978258a1d 100644 --- a/include/linux/configfs.h +++ b/include/linux/configfs.h @@ -217,6 +217,7 @@ struct configfs_group_operations { struct config_item *(*make_item)(struct config_group *group, const char *name); struct config_group *(*make_group)(struct config_group *group, const char *name); int (*commit_item)(struct config_item *item); + int (*uncommit_item)(struct config_item *item); void (*disconnect_notify)(struct config_group *group, struct config_item *item); void (*drop_item)(struct config_group *group, struct config_item *item); }; From patchwork Fri Oct 8 08:17:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538256 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=2LnsEmQd; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzl6MZ7z9sP7 for ; Fri, 8 Oct 2021 19:17:55 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233932AbhJHITr (ORCPT ); Fri, 8 Oct 2021 04:19:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233716AbhJHITo (ORCPT ); Fri, 8 Oct 2021 04:19:44 -0400 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62BCFC061764 for ; Fri, 8 Oct 2021 01:17:48 -0700 (PDT) Received: by mail-wr1-x436.google.com with SMTP id v25so27077293wra.2 for ; Fri, 08 Oct 2021 01:17:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=4StP4zG1fEI9tjzWgHEnJNQemAlTt1GkDMlHKHuvYzA=; b=2LnsEmQd+THylAdhLg4go2sLdEBbDFvg3G4CDWZjhJKALT9HG+Bfz8weDwGFUAIGgb /rC22nlJJP9bqckAPzDpHFqzHkrU4oUWbwx1MhmuRaC5alaS4eaSx+lyf6rZNXzjXkvF gSThu6GkIAx0/c/WmRcHrl2dIw2FHIMUoEdqoyNhJK8Ch76kKWHpXbj81GbHf5/XFtY8 3/uhBC/xKLFdc2hFw1hZBb8QuHEEAWcRYJERRsAqDa/719zWleLcyuceCJJjfMC2a9p7 TAi2h6mv5C04evyta9a7tju1r1cBphXGFIKntxIHQYSnnp4QbsWKRYIkNaWlvnMFSUCl 3InA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=4StP4zG1fEI9tjzWgHEnJNQemAlTt1GkDMlHKHuvYzA=; b=1ST6qwj8jXdjos6aKPxyi/JvATKQwVhwhjZ/0wSekMJ838MHwlnCR9iq0xG+/7Z1/e m43afhlozXYjj5v3ccfUIL6GFN1gGkKFtuKJW0NjQ/1h4OyU3Qhi954JbAgiPed7h2nQ Rjwq2ZxPyhHa2kAx92Mc71PH+xLp9oYJcfx6T+wVZH+KCatI1yVA/yBejZ4K6wycAJxW tEKfBLZq0doL38dpLhvSthN61teMYsBpqrSYplZFsLGBRuS6h6xkXu+L/g4gXbrG0Gcw ANBzIU5Hn6In6gJr7bMYbHmsQEPetPyWycewmTt355JvzqgF7e3jSDi3VNWWzaKDO4v+ suuw== X-Gm-Message-State: AOAM531QTqIMfNrqeH6QJc2E1QiyInCcwnS0+BElFqmzoHhiCvWjeY2Y rXxQZDRQmZzbhEbljLc8oSNfgw== X-Google-Smtp-Source: ABdhPJy8j/Bag6TeZLPEVgNrkt6F+Q8SlQN5pr2pANzFWXeQZnqrf5dENflqTDTLorY+PZBMllWSGA== X-Received: by 2002:adf:a550:: with SMTP id j16mr2227261wrb.384.1633681066942; Fri, 08 Oct 2021 01:17:46 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:46 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 4/8] samples: configfs: add a committable group Date: Fri, 8 Oct 2021 10:17:35 +0200 Message-Id: <20211008081739.26807-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add an example of using committable items to configfs samples. Each config item has two attributes: read-write 'storeme' which works similarly to other examples in this file and a read-only 'committed' attribute which changes its value between false and true depending on whether it's committed or not at the moment. Signed-off-by: Bartosz Golaszewski Acked-by: Linus Walleij Acked-by: Andy Shevchenko --- samples/configfs/configfs_sample.c | 153 +++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) diff --git a/samples/configfs/configfs_sample.c b/samples/configfs/configfs_sample.c index 37a657b25d58..f0a1c4d847b1 100644 --- a/samples/configfs/configfs_sample.c +++ b/samples/configfs/configfs_sample.c @@ -313,6 +313,158 @@ static struct configfs_subsystem group_children_subsys = { /* ----------------------------------------------------------------- */ +/* + * 04-committable-children + * + * This is an example of a committable group. It's similar to the simple + * children example but each config_item has an additional 'committed' + * attribute which is read-only and is only modified when the config_item + * is moved from the 'pending' to the 'live' directory. + */ + +struct committable_child { + struct config_item item; + int storeme; + bool committed; +}; + +static inline struct committable_child * +to_committable_child(struct config_item *item) +{ + return container_of(item, struct committable_child, item); +} + +static ssize_t +committable_child_storeme_show(struct config_item *item, char *page) +{ + return sprintf(page, "%d\n", to_committable_child(item)->storeme); +} + +static ssize_t committable_child_storeme_store(struct config_item *item, + const char *page, size_t count) +{ + struct committable_child *child = to_committable_child(item); + int ret; + + if (child->committed) + return -EPERM; + + ret = kstrtoint(page, 10, &child->storeme); + if (ret) + return ret; + + return count; +} + +CONFIGFS_ATTR(committable_child_, storeme); + +static ssize_t +committable_child_committed_show(struct config_item *item, char *page) +{ + return sprintf(page, "%s\n", + to_committable_child(item)->committed ? "true" : "false"); +} + +CONFIGFS_ATTR_RO(committable_child_, committed); + +static struct configfs_attribute *committable_child_attrs[] = { + &committable_child_attr_storeme, + &committable_child_attr_committed, + NULL, +}; + +static void committable_child_release(struct config_item *item) +{ + kfree(to_committable_child(item)); +} + +static struct configfs_item_operations committable_child_item_ops = { + .release = committable_child_release, +}; + +static const struct config_item_type committable_child_type = { + .ct_item_ops = &committable_child_item_ops, + .ct_attrs = committable_child_attrs, + .ct_owner = THIS_MODULE, +}; + +struct committable_children { + struct config_group group; +}; + +static struct config_item * +committable_children_make_item(struct config_group *group, const char *name) +{ + struct committable_child *child; + + child = kzalloc(sizeof(*child), GFP_KERNEL); + if (!child) + return ERR_PTR(-ENOMEM); + + config_item_init_type_name(&child->item, name, &committable_child_type); + + return &child->item; +} + +static ssize_t +committable_children_description_show(struct config_item *item, char *page) +{ + return sprintf(page, +"[04-committable-children]\n" +"\n" +"This subsystem allows creation of committable config_items. The subsystem\n" +"has two subdirectories: pending and live. New config_items can only be\n" +"created in pending/ and they have one writable and readable attribute as\n" +"well as a single read-only attribute. The latter is only changed once the\n" +"item is 'committed'. This is done by moving the config_item (using\n" +"rename()) to the live/ directory. In this example, the storeme attribute\n" +"becomes 'read-only' once committed.\n"); +} + +CONFIGFS_ATTR_RO(committable_children_, description); + +static struct configfs_attribute *committable_children_attrs[] = { + &committable_children_attr_description, + NULL, +}; + +static int committable_children_commit_item(struct config_item *item) +{ + to_committable_child(item)->committed = true; + + return 0; +} + +static int committable_children_uncommit_item(struct config_item *item) +{ + to_committable_child(item)->committed = false; + + return 0; +} + +static struct configfs_group_operations committable_children_group_ops = { + .make_item = committable_children_make_item, + .commit_item = committable_children_commit_item, + .uncommit_item = committable_children_uncommit_item, +}; + +static const struct config_item_type committable_children_type = { + .ct_group_ops = &committable_children_group_ops, + .ct_attrs = committable_children_attrs, + .ct_owner = THIS_MODULE, +}; + +static struct configfs_subsystem committable_children_subsys = { + .su_group = { + .cg_item = { + .ci_namebuf = "04-committable-children", + .ci_type = &committable_children_type, + }, + }, +}; + +/* ----------------------------------------------------------------- */ + /* * We're now done with our subsystem definitions. * For convenience in this module, here's a list of them all. It @@ -324,6 +476,7 @@ static struct configfs_subsystem *example_subsys[] = { &childless_subsys.subsys, &simple_children_subsys, &group_children_subsys, + &committable_children_subsys, NULL, }; From patchwork Fri Oct 8 08:17:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538257 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=nuC4K88c; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzp03jpz9sP7 for ; Fri, 8 Oct 2021 19:17:57 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234025AbhJHITs (ORCPT ); Fri, 8 Oct 2021 04:19:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46230 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233769AbhJHITp (ORCPT ); Fri, 8 Oct 2021 04:19:45 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89360C061755 for ; Fri, 8 Oct 2021 01:17:49 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id v17so27104221wrv.9 for ; Fri, 08 Oct 2021 01:17:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ktMbKzP4vmLGfMCEdS6KXlNb9WIfUM8VgieVAX+i9DI=; b=nuC4K88cqlbelAQuCLXPQv5kdbskq9hVOSkAi2InAzYnZcuwHWeCPMZIy5/b62/iG7 6hdCZY1xmRa/KHkq4lvqZIUx7qpwdyfarWddV+l8t8CcjWJAJnGgEBZeG5NEWHKYYwD1 SyWgAe1+7zrnOA9OW5cbAGCQzWlXk9pae8HkefoJBddmpVv5FEbczFINDB1AthrsVeBH yDjcj8Ldq6DkmKXPoIN0NOWt6iThpZi+8qJHltidLLoPpEXXyVM05PIwiB0gGwkXCW7L UWuEpSfUTzRRR3r8KuDRzCjxlv802bXjsIKyY/fV2eZ0MgfObtRrdEygEIfz7G+5H/rp 4ZEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ktMbKzP4vmLGfMCEdS6KXlNb9WIfUM8VgieVAX+i9DI=; b=pYsFjsgWvboNQRyyyfL19ZLDlPmtNAVo4BRHZmnpyPao0b0vVnZl28LblrYZJXn+HK qJYo8vHbPE1h3Ec8N/kpOkfjM5QM35Tsj8IotHjfJLt/54rQ1kW48ahf1eopmdfTpFGW fi6qg18tIFC0C8PYutVURGHIFcNUrEDm1HtQlY21hlT+d5l6Bh3XLHWq6paQxz25Zrqn ZyESanevv15XKoY6jjBeHlgnOOe8WPpSpAEPvx+Uo4/7avSsWCPdtc3GAkTdBl6pBb3o QlBnPagyXLqvz6mrR6dUwaSlR7zKrvK68dzWTwQVYxpZSby8h+OkGfjd9w0O9gQM3GyR 2asw== X-Gm-Message-State: AOAM5321tNlP4YMP6LOvAUV1OKrbJ4x7sgRhmPaCdmlaG78SjOBZkSbD 1rOgsbVhnGIpIp/fwHwlOBScTQ== X-Google-Smtp-Source: ABdhPJxgfOHxu2IF58rJKov7EiY6DO47XvsJvnMwRppOB6MZWEl09Z3ypEuDMsAIMpyNJKwD7bsNMg== X-Received: by 2002:a05:600c:5128:: with SMTP id o40mr1900985wms.81.1633681067873; Fri, 08 Oct 2021 01:17:47 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:47 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski , Colin Ian King Subject: [PATCH v7 5/8] gpio: sim: new testing module Date: Fri, 8 Oct 2021 10:17:36 +0200 Message-Id: <20211008081739.26807-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Implement a new, modern GPIO testing module controlled by configfs attributes instead of module parameters. The goal of this driver is to provide a replacement for gpio-mockup that will be easily extensible with new features and doesn't require reloading the module to change the setup. Signed-off-by: Bartosz Golaszewski [Andy: Initialize attribute allocated on the heap] Signed-off-by: Andy Shevchenko [Colin: Fix dereference of free'd pointer config] Signed-off-by: Colin Ian King Reviewed-by: Andy Shevchenko Reviewed-by: Linus Walleij --- Documentation/admin-guide/gpio/gpio-sim.rst | 72 ++ drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-sim.c | 886 ++++++++++++++++++++ 4 files changed, 967 insertions(+) create mode 100644 Documentation/admin-guide/gpio/gpio-sim.rst create mode 100644 drivers/gpio/gpio-sim.c diff --git a/Documentation/admin-guide/gpio/gpio-sim.rst b/Documentation/admin-guide/gpio/gpio-sim.rst new file mode 100644 index 000000000000..08eac487e35e --- /dev/null +++ b/Documentation/admin-guide/gpio/gpio-sim.rst @@ -0,0 +1,72 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +Configfs GPIO Simulator +======================= + +The configfs GPIO Simulator (gpio-sim) provides a way to create simulated GPIO +chips for testing purposes. The lines exposed by these chips can be accessed +using the standard GPIO character device interface as well as manipulated +using sysfs attributes. + +Creating simulated chips +------------------------ + +The gpio-sim module registers a configfs subsystem called 'gpio-sim'. It's a +subsystem with committable items which means two subdirectories are created in +the filesystem: pending and live. For more information on configfs and +committable items, please refer to Documentation/filesystems/configfs.rst. + +In order to instantiate a new simulated chip, the user needs to mkdir() a new +directory in pending/. Inside each new directory, there's a set of attributes +that can be used to configure the new chip. Once the configuration is complete, +the user needs to use rename() to move the chip to the live/ directory. This +creates and registers the new device. + +In order to destroy a simulated chip, it has to be moved back to pending first +and then removed using rmdir(). + +Currently supported configuration attributes are: + + num_lines - an unsigned integer value defining the number of GPIO lines to + export + + label - a string defining the label for the GPIO chip + + line_names - a list of GPIO line names in the form of quoted strings + separated by commas, e.g.: '"foo", "bar", "", "foobar"'. The + number of strings doesn't have to be equal to the value set in + the num_lines attribute. If it's lower than the number of lines, + the remaining lines are unnamed. If it's larger, the superfluous + lines are ignored. A name of the form: '""' means the line + should be unnamed. + +Additionally two read-only attributes named 'chip_name' and 'dev_name' are +exposed in order to provide users with a mapping from configfs directories to +the actual devices created in the kernel. The former returns the name of the +GPIO device as assigned by gpiolib (i.e. "gpiochip0", "gpiochip1", etc.). The +latter returns the parent device name as defined by the gpio-sim driver (i.e. +"gpio-sim.0", "gpio-sim.1", etc.). This allows user-space to map the configfs +items both to the correct character device file as well as the associated entry +in sysfs. + +Simulated GPIO chips can also be defined in device-tree. The compatible string +must be: "gpio-simulator". Supported properties are: + + "gpio-sim,label" - chip label + + "gpio-sim,nr-gpios" - number of lines + +Other standard GPIO properties (like "gpio-line-names" and gpio-hog) are also +supported. + +Manipulating simulated lines +---------------------------- + +Each simulated GPIO chip creates a sysfs attribute group under its device +directory called 'line-ctrl'. Inside each group, there's a separate attribute +for each GPIO line. The name of the attribute is of the form 'gpioX' where X +is the line's offset in the chip. + +Reading from a line attribute returns the current value. Writing to it (0 or 1) +changes the configuration of the simulated pull-up/pull-down resistor +(1 - pull-up, 0 - pull-down). diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig index fae5141251e5..2631d60eb2eb 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -1680,6 +1680,14 @@ config GPIO_VIRTIO These virtual GPIOs can be routed to real GPIOs or attached to simulators on the host (like QEMU). +config GPIO_SIM + tristate "GPIO Simulator Module" + select IRQ_SIM + select CONFIGFS_FS + help + This enables the GPIO simulator - a configfs-based GPIO testing + driver. + endmenu endif diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile index fbcda637d5e1..1cb16c767237 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -133,6 +133,7 @@ obj-$(CONFIG_GPIO_SAMA5D2_PIOBU) += gpio-sama5d2-piobu.o obj-$(CONFIG_GPIO_SCH311X) += gpio-sch311x.o obj-$(CONFIG_GPIO_SCH) += gpio-sch.o obj-$(CONFIG_GPIO_SIFIVE) += gpio-sifive.o +obj-$(CONFIG_GPIO_SIM) += gpio-sim.o obj-$(CONFIG_GPIO_SIOX) += gpio-siox.o obj-$(CONFIG_GPIO_SL28CPLD) += gpio-sl28cpld.o obj-$(CONFIG_GPIO_SODAVILLE) += gpio-sodaville.o diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c new file mode 100644 index 000000000000..88c0e89d5d82 --- /dev/null +++ b/drivers/gpio/gpio-sim.c @@ -0,0 +1,886 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO testing driver based on configfs. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "gpiolib.h" + +static DEFINE_IDA(gpio_sim_ida); + +struct gpio_sim_chip { + struct gpio_chip gc; + unsigned long *directions; + unsigned long *values; + unsigned long *pulls; + struct irq_domain *irq_sim; + struct mutex lock; + struct attribute_group attr_group; +}; + +struct gpio_sim_attribute { + struct device_attribute dev_attr; + unsigned int offset; +}; + +static struct gpio_sim_attribute * +to_gpio_sim_attr(struct device_attribute *dev_attr) +{ + return container_of(dev_attr, struct gpio_sim_attribute, dev_attr); +} + +static int gpio_sim_apply_pull(struct gpio_sim_chip *chip, + unsigned int offset, int value) +{ + int curr_val, irq, irq_type, ret; + struct gpio_desc *desc; + struct gpio_chip *gc; + + gc = &chip->gc; + desc = &gc->gpiodev->descs[offset]; + + mutex_lock(&chip->lock); + + if (test_bit(FLAG_REQUESTED, &desc->flags) && + !test_bit(FLAG_IS_OUT, &desc->flags)) { + curr_val = !!test_bit(offset, chip->values); + if (curr_val == value) + goto set_pull; + + /* + * This is fine - it just means, nobody is listening + * for interrupts on this line, otherwise + * irq_create_mapping() would have been called from + * the to_irq() callback. + */ + irq = irq_find_mapping(chip->irq_sim, offset); + if (!irq) + goto set_value; + + irq_type = irq_get_trigger_type(irq); + + if ((value && (irq_type & IRQ_TYPE_EDGE_RISING)) || + (!value && (irq_type & IRQ_TYPE_EDGE_FALLING))) { + ret = irq_set_irqchip_state(irq, IRQCHIP_STATE_PENDING, + true); + if (ret) + goto set_pull; + } + } + +set_value: + /* Change the value unless we're actively driving the line. */ + if (!test_bit(FLAG_REQUESTED, &desc->flags) || + !test_bit(FLAG_IS_OUT, &desc->flags)) + __assign_bit(offset, chip->values, value); + +set_pull: + __assign_bit(offset, chip->pulls, value); + mutex_unlock(&chip->lock); + return 0; +} + +static int gpio_sim_get(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + int ret; + + mutex_lock(&chip->lock); + ret = !!test_bit(offset, chip->values); + mutex_unlock(&chip->lock); + + return ret; +} + +static void gpio_sim_set(struct gpio_chip *gc, unsigned int offset, int value) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __assign_bit(offset, chip->values, value); + mutex_unlock(&chip->lock); +} + +static int gpio_sim_get_multiple(struct gpio_chip *gc, + unsigned long *mask, unsigned long *bits) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + bitmap_copy(bits, chip->values, gc->ngpio); + mutex_unlock(&chip->lock); + + return 0; +} + +static void gpio_sim_set_multiple(struct gpio_chip *gc, + unsigned long *mask, unsigned long *bits) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + bitmap_copy(chip->values, bits, gc->ngpio); + mutex_unlock(&chip->lock); +} + +static int gpio_sim_direction_output(struct gpio_chip *gc, + unsigned int offset, int value) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __clear_bit(offset, chip->directions); + __assign_bit(offset, chip->values, value); + mutex_unlock(&chip->lock); + + return 0; +} + +static int gpio_sim_direction_input(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __set_bit(offset, chip->directions); + mutex_unlock(&chip->lock); + + return 0; +} + +static int gpio_sim_get_direction(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + int direction; + + mutex_lock(&chip->lock); + direction = !!test_bit(offset, chip->directions); + mutex_unlock(&chip->lock); + + return direction ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT; +} + +static int gpio_sim_set_config(struct gpio_chip *gc, + unsigned int offset, unsigned long config) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + switch (pinconf_to_config_param(config)) { + case PIN_CONFIG_BIAS_PULL_UP: + return gpio_sim_apply_pull(chip, offset, 1); + case PIN_CONFIG_BIAS_PULL_DOWN: + return gpio_sim_apply_pull(chip, offset, 0); + default: + break; + } + + return -ENOTSUPP; +} + +static int gpio_sim_to_irq(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + return irq_create_mapping(chip->irq_sim, offset); +} + +static void gpio_sim_free(struct gpio_chip *gc, unsigned int offset) +{ + struct gpio_sim_chip *chip = gpiochip_get_data(gc); + + mutex_lock(&chip->lock); + __assign_bit(offset, chip->values, !!test_bit(offset, chip->pulls)); + mutex_unlock(&chip->lock); +} + +static ssize_t gpio_sim_sysfs_line_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); + struct gpio_sim_chip *chip = dev_get_drvdata(dev); + int ret; + + mutex_lock(&chip->lock); + ret = sysfs_emit(buf, "%u\n", + !!test_bit(line_attr->offset, chip->values)); + mutex_unlock(&chip->lock); + + return ret; +} + +static ssize_t gpio_sim_sysfs_line_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t len) +{ + struct gpio_sim_attribute *line_attr = to_gpio_sim_attr(attr); + struct gpio_sim_chip *chip = dev_get_drvdata(dev); + int ret, val; + + if (len > 2 || (buf[0] != '0' && buf[0] != '1')) + return -EINVAL; + + val = buf[0] == '0' ? 0 : 1; + + ret = gpio_sim_apply_pull(chip, line_attr->offset, val); + if (ret) + return ret; + + return len; +} + +static void gpio_sim_mutex_destroy(void *data) +{ + struct mutex *lock = data; + + mutex_destroy(lock); +} + +static void gpio_sim_sysfs_remove(void *data) +{ + struct gpio_sim_chip *chip = data; + + sysfs_remove_group(&chip->gc.parent->kobj, &chip->attr_group); +} + +static int gpio_sim_setup_sysfs(struct gpio_sim_chip *chip) +{ + unsigned int i, num_lines = chip->gc.ngpio; + struct device *dev = chip->gc.parent; + struct gpio_sim_attribute *line_attr; + struct device_attribute *dev_attr; + struct attribute **attrs; + int ret; + + attrs = devm_kcalloc(dev, sizeof(*attrs), num_lines + 1, GFP_KERNEL); + if (!attrs) + return -ENOMEM; + + for (i = 0; i < num_lines; i++) { + line_attr = devm_kzalloc(dev, sizeof(*line_attr), GFP_KERNEL); + if (!line_attr) + return -ENOMEM; + + line_attr->offset = i; + + dev_attr = &line_attr->dev_attr; + sysfs_attr_init(&dev_attr->attr); + + dev_attr->attr.name = devm_kasprintf(dev, GFP_KERNEL, + "gpio%u", i); + if (!dev_attr->attr.name) + return -ENOMEM; + + dev_attr->attr.mode = 0644; + + dev_attr->show = gpio_sim_sysfs_line_show; + dev_attr->store = gpio_sim_sysfs_line_store; + + attrs[i] = &dev_attr->attr; + } + + chip->attr_group.name = "line-ctrl"; + chip->attr_group.attrs = attrs; + + ret = sysfs_create_group(&dev->kobj, &chip->attr_group); + if (ret) + return ret; + + return devm_add_action_or_reset(dev, gpio_sim_sysfs_remove, chip); +} + +static int gpio_sim_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct gpio_sim_chip *chip; + struct gpio_chip *gc; + const char *label; + u32 num_lines; + int ret; + + ret = device_property_read_u32(dev, "gpio-sim,nr-gpios", &num_lines); + if (ret) + return ret; + + ret = device_property_read_string(dev, "gpio-sim,label", &label); + if (ret) + label = dev_name(dev); + + chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL); + if (!chip) + return -ENOMEM; + + chip->directions = devm_bitmap_alloc(dev, num_lines, GFP_KERNEL); + if (!chip->directions) + return -ENOMEM; + + /* Default to input mode. */ + bitmap_fill(chip->directions, num_lines); + + chip->values = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); + if (!chip->values) + return -ENOMEM; + + chip->pulls = devm_bitmap_zalloc(dev, num_lines, GFP_KERNEL); + if (!chip->pulls) + return -ENOMEM; + + chip->irq_sim = devm_irq_domain_create_sim(dev, NULL, num_lines); + if (IS_ERR(chip->irq_sim)) + return PTR_ERR(chip->irq_sim); + + mutex_init(&chip->lock); + ret = devm_add_action_or_reset(dev, gpio_sim_mutex_destroy, + &chip->lock); + if (ret) + return ret; + + gc = &chip->gc; + gc->base = -1; + gc->ngpio = num_lines; + gc->label = label; + gc->owner = THIS_MODULE; + gc->parent = dev; + gc->get = gpio_sim_get; + gc->set = gpio_sim_set; + gc->get_multiple = gpio_sim_get_multiple; + gc->set_multiple = gpio_sim_set_multiple; + gc->direction_output = gpio_sim_direction_output; + gc->direction_input = gpio_sim_direction_input; + gc->get_direction = gpio_sim_get_direction; + gc->set_config = gpio_sim_set_config; + gc->to_irq = gpio_sim_to_irq; + gc->free = gpio_sim_free; + + ret = devm_gpiochip_add_data(dev, gc, chip); + if (ret) + return ret; + + /* Used by sysfs and configfs callbacks. */ + dev_set_drvdata(dev, chip); + + ret = gpio_sim_setup_sysfs(chip); + if (ret) + return ret; + + return 0; +} + +static const struct of_device_id gpio_sim_of_match[] = { + { .compatible = "gpio-simulator" }, + { } +}; +MODULE_DEVICE_TABLE(of, gpio_sim_of_match); + +static struct platform_driver gpio_sim_driver = { + .driver = { + .name = "gpio-sim", + .of_match_table = gpio_sim_of_match, + }, + .probe = gpio_sim_probe, +}; + +struct gpio_sim_chip_config { + struct config_item item; + + /* + * If pdev is NULL, the item is 'pending' (waiting for configuration). + * Once the pointer is assigned, the device has been created and the + * item is 'live'. + */ + struct platform_device *pdev; + int id; + + /* + * Each configfs filesystem operation is protected with the subsystem + * mutex. Each separate attribute is protected with the buffer mutex. + * This structure however can be modified by callbacks of different + * attributes so we need another lock. + */ + struct mutex lock; + + char label[32]; + unsigned int num_lines; + char **line_names; + unsigned int num_line_names; +}; + +static struct gpio_sim_chip_config * +to_gpio_sim_chip_config(struct config_item *item) +{ + return container_of(item, struct gpio_sim_chip_config, item); +} + +static ssize_t gpio_sim_config_dev_name_show(struct config_item *item, + char *page) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + struct platform_device *pdev; + int ret; + + mutex_lock(&config->lock); + pdev = config->pdev; + if (pdev) + ret = sprintf(page, "%s\n", dev_name(&pdev->dev)); + else + ret = sprintf(page, "gpio-sim.%d\n", config->id); + mutex_unlock(&config->lock); + + return ret; +} + +CONFIGFS_ATTR_RO(gpio_sim_config_, dev_name); + +static ssize_t gpio_sim_config_chip_name_show(struct config_item *item, + char *page) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + struct gpio_sim_chip *chip = NULL; + struct platform_device *pdev; + int ret; + + mutex_lock(&config->lock); + pdev = config->pdev; + if (pdev) + chip = dev_get_drvdata(&pdev->dev); + + if (chip) + ret = sprintf(page, "%s\n", dev_name(&chip->gc.gpiodev->dev)); + else + ret = sprintf(page, "none\n"); + mutex_unlock(&config->lock); + + return ret; +} + +CONFIGFS_ATTR_RO(gpio_sim_config_, chip_name); + +static ssize_t gpio_sim_config_label_show(struct config_item *item, char *page) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + int ret; + + mutex_lock(&config->lock); + ret = sprintf(page, "%s\n", config->label); + mutex_unlock(&config->lock); + + return ret; +} + +static ssize_t gpio_sim_config_label_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + char *dup, *trimmed; + int ret; + + mutex_lock(&config->lock); + + if (config->pdev) { + mutex_unlock(&config->lock); + return -EBUSY; + } + + dup = kstrndup(page, count, GFP_KERNEL); + if (!dup) { + mutex_unlock(&config->lock); + return -ENOMEM; + } + + trimmed = strstrip(dup); + ret = snprintf(config->label, sizeof(config->label), "%s", trimmed); + kfree(dup); + if (ret < 0) { + mutex_unlock(&config->lock); + return ret; + } + + mutex_unlock(&config->lock); + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_, label); + +static ssize_t gpio_sim_config_num_lines_show(struct config_item *item, + char *page) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + int ret; + + mutex_lock(&config->lock); + ret = sprintf(page, "%u\n", config->num_lines); + mutex_unlock(&config->lock); + + return ret; +} + +static ssize_t gpio_sim_config_num_lines_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + unsigned int num_lines; + int ret; + + mutex_lock(&config->lock); + + if (config->pdev) { + mutex_unlock(&config->lock); + return -EBUSY; + } + + ret = kstrtouint(page, 10, &num_lines); + if (ret) { + mutex_unlock(&config->lock); + return ret; + } + + if (num_lines == 0) { + mutex_unlock(&config->lock); + return -EINVAL; + } + + config->num_lines = num_lines; + + mutex_unlock(&config->lock); + return count; +} + +CONFIGFS_ATTR(gpio_sim_config_, num_lines); + +static ssize_t gpio_sim_config_line_names_show(struct config_item *item, + char *page) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + int ret, i, written = 0; + + mutex_lock(&config->lock); + + if (!config->line_names) { + mutex_unlock(&config->lock); + return sprintf(page, "\n"); + } + + for (i = 0; i < config->num_line_names; i++) { + ret = sprintf(page + written, + i < config->num_line_names - 1 ? "\"%s\", " : "\"%s\"\n", + config->line_names[i] ?: ""); + if (ret < 0) { + mutex_unlock(&config->lock); + return ret; + } + + written += ret; + } + + mutex_unlock(&config->lock); + return written; +} + +static ssize_t gpio_sim_config_line_names_store(struct config_item *item, + const char *page, size_t count) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + unsigned int num_new_names = 1, num_old_names, name_idx = 0; + bool in_quote = false, got_comma = true; + char **new_names, **old_names, *name, c; + const char *start = page; + size_t pos, name_len; + int err = -EINVAL; + + mutex_lock(&config->lock); + + if (config->pdev) { + mutex_unlock(&config->lock); + return -EBUSY; + } + + /* + * Line names are stored in a pointer array so that we can easily + * pass them down to the GPIO subsystem in a "gpio-line-names" + * property. + * + * Line names must be passed as a list of quoted names separated by + * commas, for example: '"foo", "bar", "foobar"'. + */ + + for (pos = 0; pos < count; pos++) { + /* + * Just count the commas and assume the number if strings + * equals the number of commas + 1. If the format is wrong + * we'll bail out anyway. + */ + if (page[pos] == ',') + num_new_names++; + } + + new_names = kcalloc(num_new_names, sizeof(char *), GFP_KERNEL); + if (!new_names) { + mutex_unlock(&config->lock); + return -ENOMEM; + } + + /* + * REVISIT There are more users in the kernel that want to split + * comma-separated strings into tokens. We should generalize it and + * put it into lib/. + */ + for (pos = 0; pos < count; pos++) { + c = page[pos]; + + if (in_quote) { + if (c == '"') { + /* This is the end of the name. */ + in_quote = got_comma = false; + name_len = (page + pos) - start; + if (name_len == 0) { + /* Name is empty (passed as ""). */ + name_idx++; + continue; + } + + name = kmemdup(start, name_len + 1, GFP_KERNEL); + if (!name) { + err = -ENOMEM; + goto err_out; + } + + name[name_len] = '\0'; + new_names[name_idx++] = name; + } + } else { + if (c == '"') { + /* Enforce separating names with commas. */ + if (!got_comma) + goto err_out; + + start = page + pos + 1; + in_quote = true; + } else if (c == ',') { + if (!got_comma) + got_comma = true; + else + /* Double commas are not allowed. */ + goto err_out; + } else if (!isspace(c)) { + goto err_out; + } + } + } + + /* + * End of input sanity checks, must not have a comma at the end and + * must have finished scanning the last name. + */ + if (in_quote || got_comma) + goto err_out; + + old_names = config->line_names; + num_old_names = config->num_line_names; + config->line_names = new_names; + config->num_line_names = num_new_names; + + mutex_unlock(&config->lock); + kfree_strarray(old_names, num_old_names); + return count; + +err_out: + mutex_unlock(&config->lock); + kfree_strarray(new_names, name_idx); + return err; +} + +CONFIGFS_ATTR(gpio_sim_config_, line_names); + +static struct configfs_attribute *gpio_sim_config_attrs[] = { + &gpio_sim_config_attr_dev_name, + &gpio_sim_config_attr_chip_name, + &gpio_sim_config_attr_label, + &gpio_sim_config_attr_num_lines, + &gpio_sim_config_attr_line_names, + NULL +}; + +static void gpio_sim_chip_config_release(struct config_item *item) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + + mutex_destroy(&config->lock); + ida_free(&gpio_sim_ida, config->id); + kfree_strarray(config->line_names, config->num_line_names); + kfree(config); +} + +static struct configfs_item_operations gpio_sim_config_item_ops = { + .release = gpio_sim_chip_config_release, +}; + +static const struct config_item_type gpio_sim_chip_config_type = { + .ct_item_ops = &gpio_sim_config_item_ops, + .ct_attrs = gpio_sim_config_attrs, + .ct_owner = THIS_MODULE, +}; + +static struct config_item * +gpio_sim_config_make_item(struct config_group *group, const char *name) +{ + struct gpio_sim_chip_config *config; + int id; + + config = kzalloc(sizeof(*config), GFP_KERNEL); + if (!config) + return ERR_PTR(-ENOMEM); + + id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); + if (id < 0) { + kfree(config); + return ERR_PTR(id); + } + + config_item_init_type_name(&config->item, name, + &gpio_sim_chip_config_type); + config->num_lines = 1; + config->id = id; + mutex_init(&config->lock); + + return &config->item; +} + +static int gpio_sim_config_commit_item(struct config_item *item) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + struct platform_device_info pdevinfo; + struct property_entry properties[4]; /* Max 3 properties + sentinel. */ + struct fwnode_handle *fwnode; + struct platform_device *pdev; + unsigned int prop_idx = 0; + + memset(&pdevinfo, 0, sizeof(pdevinfo)); + memset(properties, 0, sizeof(properties)); + + mutex_lock(&config->lock); + + properties[prop_idx++] = PROPERTY_ENTRY_U32("gpio-sim,nr-gpios", + config->num_lines); + + if (config->label[0] != '\0') + properties[prop_idx++] = PROPERTY_ENTRY_STRING("gpio-sim,label", + config->label); + + if (config->line_names) + properties[prop_idx++] = PROPERTY_ENTRY_STRING_ARRAY_LEN( + "gpio-line-names", + config->line_names, + config->num_line_names); + + fwnode = fwnode_create_software_node(properties, NULL); + if (IS_ERR(fwnode)) + return PTR_ERR(fwnode); + + pdevinfo.name = "gpio-sim"; + pdevinfo.fwnode = fwnode; + pdevinfo.id = config->id; + + pdev = platform_device_register_full(&pdevinfo); + if (IS_ERR(pdev)) { + fwnode_remove_software_node(fwnode); + mutex_unlock(&config->lock); + return PTR_ERR(pdev); + } + + config->pdev = pdev; + mutex_unlock(&config->lock); + + return 0; +} + +static int gpio_sim_config_uncommit_item(struct config_item *item) +{ + struct gpio_sim_chip_config *config = to_gpio_sim_chip_config(item); + struct fwnode_handle *fwnode; + + mutex_lock(&config->lock); + fwnode = dev_fwnode(&config->pdev->dev); + platform_device_unregister(config->pdev); + fwnode_remove_software_node(fwnode); + config->pdev = NULL; + mutex_unlock(&config->lock); + + return 0; +} + +static struct configfs_group_operations gpio_sim_config_group_ops = { + .make_item = gpio_sim_config_make_item, + .commit_item = gpio_sim_config_commit_item, + .uncommit_item = gpio_sim_config_uncommit_item, +}; + +static const struct config_item_type gpio_sim_config_type = { + .ct_group_ops = &gpio_sim_config_group_ops, + .ct_owner = THIS_MODULE, +}; + +static struct configfs_subsystem gpio_sim_config_subsys = { + .su_group = { + .cg_item = { + .ci_namebuf = "gpio-sim", + .ci_type = &gpio_sim_config_type, + }, + }, +}; + +static int __init gpio_sim_init(void) +{ + int ret; + + ret = platform_driver_register(&gpio_sim_driver); + if (ret) { + pr_err("Error %d while registering the platform driver\n", ret); + return ret; + } + + config_group_init(&gpio_sim_config_subsys.su_group); + mutex_init(&gpio_sim_config_subsys.su_mutex); + ret = configfs_register_subsystem(&gpio_sim_config_subsys); + if (ret) { + pr_err("Error %d while registering the configfs subsystem %s\n", + ret, gpio_sim_config_subsys.su_group.cg_item.ci_namebuf); + mutex_destroy(&gpio_sim_config_subsys.su_mutex); + platform_driver_unregister(&gpio_sim_driver); + return ret; + } + + return 0; +} +module_init(gpio_sim_init); + +static void __exit gpio_sim_exit(void) +{ + configfs_unregister_subsystem(&gpio_sim_config_subsys); + mutex_destroy(&gpio_sim_config_subsys.su_mutex); + platform_driver_unregister(&gpio_sim_driver); +} +module_exit(gpio_sim_exit); + +MODULE_AUTHOR("Bartosz Golaszewski "); +MODULE_DESCRIPTION("GPIO Simulator Module"); +MODULE_LICENSE("GPL"); From patchwork Fri Oct 8 08:17:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538258 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=Oti/1/Ax; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzv1nj1z9sP7 for ; Fri, 8 Oct 2021 19:18:03 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234364AbhJHIT4 (ORCPT ); Fri, 8 Oct 2021 04:19:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233823AbhJHITp (ORCPT ); Fri, 8 Oct 2021 04:19:45 -0400 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6054AC061766 for ; Fri, 8 Oct 2021 01:17:50 -0700 (PDT) Received: by mail-wr1-x429.google.com with SMTP id r10so27144883wra.12 for ; Fri, 08 Oct 2021 01:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RRLokZD/JzyOrQQERF9ZVv3TJYiF2A/gea/eW+3tWFo=; b=Oti/1/AxoJNyVgcDKym+is0D+kccGWhwTNBeX9bxjPQSNONvqmDYiLOD5Oez8LBAWX v/bV8klnRwH35TWQlSsm+jmg9IhEHURNccQ86STSg1C0WHkX2N4EzZGaBd+BXRkCGcRP lbQkwlR7GZnNWmh1dZ0nt5LfI/Fa9tebZaVPD84daEDlIucNZtc0S1L9YV/yuYKlXsBP qzo0YiRCBbH9MXiKp2wAXlLsa8i4bp2dCug7SRkPzB8UNeK4IWqIdKBfhneUUzy+lq9Q 5+NzAJ+/zb7sK0ZXm6xDEA9X8BFZ3RhdWZTzeJZd4Eqp2W9VBrp00HLe8wzD4mwfuGnu m24A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RRLokZD/JzyOrQQERF9ZVv3TJYiF2A/gea/eW+3tWFo=; b=SFIhukYXZ9f9LkUZqFOHwWN5z8ku/tCL32Wl1yTVnKQgAMtuwsa7I6XYIPWiAjI5AV T65b9j2jwAOJBYOsQASHb9c3sZurjX592LXEGM4jQlSxVzCWRWBZdHbBAD0o1NcjGfg1 4780s72rcF43piK3up2GT5p738viPglrS6zhmOadbgHIyWfB5XkhUnjwog114AVKOtyD 9vx1qkE+Jvh54vo0Z0NMUyfrQWpdLPnEODYqm5JG1RHo7QJnY7WJi9SOH6AaZEzlQabl FUmFp9gMYwlLF0dznrfbCgzPwZ1OlyLhVr51+miDIX4L5QAz4H+Yb3C7wCmjEYi20lnM Cx/Q== X-Gm-Message-State: AOAM530Qvbw9myi+wImOWJHUxWQDGqUcCmQEPZdtncnX3rs2V+iQEJbk /b0tjfsKGGQ8a1JSsIdZXaiElA== X-Google-Smtp-Source: ABdhPJyaFynlO5iLIOCrxTcP/NINtKd7/P0E6lhkCd7ZO1InRR6QWYnSdyPrUjkGBAM5NpfFjzPXkA== X-Received: by 2002:a05:6000:144e:: with SMTP id v14mr2273926wrx.228.1633681068681; Fri, 08 Oct 2021 01:17:48 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:48 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 6/8] selftests: gpio: provide a helper for reading chip info Date: Fri, 8 Oct 2021 10:17:37 +0200 Message-Id: <20211008081739.26807-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a simple program that allows to retrieve chip properties from the GPIO character device. This will be used in gpio-sim selftests. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-chip-info.c | 57 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-chip-info.c diff --git a/tools/testing/selftests/gpio/.gitignore b/tools/testing/selftests/gpio/.gitignore index a4969f7ee020..4ea4f58dab1a 100644 --- a/tools/testing/selftests/gpio/.gitignore +++ b/tools/testing/selftests/gpio/.gitignore @@ -1,2 +1,3 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev +gpio-chip-info diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index 39f2bbe8dd3d..84b48547f94c 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -2,6 +2,6 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info include ../lib.mk diff --git a/tools/testing/selftests/gpio/gpio-chip-info.c b/tools/testing/selftests/gpio/gpio-chip-info.c new file mode 100644 index 000000000000..4d26fa7c254a --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-chip-info.c @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO character device helper for reading chip information. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include + +static void print_usage(void) +{ + printf("usage:\n"); + printf(" gpio-chip-info [name|label|num-lines]\n"); +} + +int main(int argc, char **argv) +{ + struct gpiochip_info info; + int fd, ret; + + if (argc !=3) { + print_usage(); + return EXIT_FAILURE; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + perror("unable to open the GPIO chip"); + return EXIT_FAILURE; + } + + memset(&info, 0, sizeof(info)); + ret = ioctl(fd, GPIO_GET_CHIPINFO_IOCTL, &info); + if (ret) { + perror("chip info ioctl failed"); + return EXIT_FAILURE; + } + + if (strcmp(argv[2], "name") == 0) { + printf("%s\n", info.name); + } else if (strcmp(argv[2], "label") == 0) { + printf("%s\n", info.label); + } else if (strcmp(argv[2], "num-lines") == 0) { + printf("%u\n", info.lines); + } else { + fprintf(stderr, "unknown command: %s\n", argv[2]); + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} From patchwork Fri Oct 8 08:17:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538259 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=KN/7LWDa; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQgzx6BZ7z9sP7 for ; Fri, 8 Oct 2021 19:18:05 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233313AbhJHIT5 (ORCPT ); Fri, 8 Oct 2021 04:19:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233869AbhJHITq (ORCPT ); Fri, 8 Oct 2021 04:19:46 -0400 Received: from mail-wr1-x42b.google.com (mail-wr1-x42b.google.com [IPv6:2a00:1450:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2B7EC06176E for ; Fri, 8 Oct 2021 01:17:50 -0700 (PDT) Received: by mail-wr1-x42b.google.com with SMTP id k7so26991680wrd.13 for ; Fri, 08 Oct 2021 01:17:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=1XGPFAB413i4NSgL53tMC4PneOe4C16/1HV5Loq6Wys=; b=KN/7LWDaRYRSsJOmXpGlR61T+vUiSUgUV52J1zbB49eVbU21A0nWargFNSciJ9vCOH uVR/PeTvJ0uS+FFJARPLH91JUDhUqlDuHUMtc9GKbpn3UwD9SfesZbaP88UorrjJfyxJ Nu7kbwdoQ7EbSbZceFBZ32UnMe668+SjpFW7UgHSmdm1oXNWXVWZWzO9GrbLLFmEEahH qEwRmnjKm/LXJtDGef6vwOTT5P1z5sWpaArnLZWAHNl8EWOdcSX263AhNi6ryetFwfTu B8MSTArntaAZj2vIk6ytWeF+D1fWJXTrPLWanRrkWb8DZzBt8RUaVRVJhBktWhdmNhkI Eciw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=1XGPFAB413i4NSgL53tMC4PneOe4C16/1HV5Loq6Wys=; b=Mr0cyQ+0hEom+9W5mvX9qvwn83KvFRE5XXWPiRf11Cwjtcd0BWQQ/LTkIxNneatJjy x2a90Ogt+YHB2RET3o7bFjei6H7Md1BUGNyIRMcOeUehQfxfBO4nqDZ2a1edUJUvdZJV 6p+YC9yBh3d3cYjfdBITfDjrfr0O7MzIyLBm0XswznLyY/dGKWvES7re19lMBt6lxRy0 OvjlJajruJhmqo2NVb44s6OeaYKXdBVm3dHlMeIweAHN5gVKEzp0dsuXszlT8RiVZHPX Jc6R1/jhnvt0Bj/YoFHoekVrt/sLealxWD1NsSibn66LQrUtr04+ghV8z5ObqJOviG8E cdbQ== X-Gm-Message-State: AOAM530K+QfXADXJgiBuzjEXW6iB36tQzb6us6JrdiKQBuEob11g3VoN Wl1FfHmJR/QmcotYdrIveSzLZg== X-Google-Smtp-Source: ABdhPJw6Ksw5oVZRq328sfwc1ILw9WVuVxkYEHtEisDv8IU8/GDy3BEBPH1PdtI9N4f8NovuBJ6cBg== X-Received: by 2002:adf:a319:: with SMTP id c25mr2301868wrb.307.1633681069549; Fri, 08 Oct 2021 01:17:49 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:49 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 7/8] selftests: gpio: add a helper for reading GPIO line names Date: Fri, 8 Oct 2021 10:17:38 +0200 Message-Id: <20211008081739.26807-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a simple program that allows to read GPIO line names from the character device. This will be used in gpio-sim selftests. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/.gitignore | 1 + tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/gpio-line-name.c | 55 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/gpio/gpio-line-name.c diff --git a/tools/testing/selftests/gpio/.gitignore b/tools/testing/selftests/gpio/.gitignore index 4ea4f58dab1a..ededb077a3a6 100644 --- a/tools/testing/selftests/gpio/.gitignore +++ b/tools/testing/selftests/gpio/.gitignore @@ -1,3 +1,4 @@ # SPDX-License-Identifier: GPL-2.0-only gpio-mockup-cdev gpio-chip-info +gpio-line-name diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index 84b48547f94c..d7d8f1985d99 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -2,6 +2,6 @@ TEST_PROGS := gpio-mockup.sh TEST_FILES := gpio-mockup-sysfs.sh -TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info +TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name include ../lib.mk diff --git a/tools/testing/selftests/gpio/gpio-line-name.c b/tools/testing/selftests/gpio/gpio-line-name.c new file mode 100644 index 000000000000..a52e75bc37ba --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-line-name.c @@ -0,0 +1,55 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * GPIO character device helper for reading line names. + * + * Copyright (C) 2021 Bartosz Golaszewski + */ + +#include +#include +#include +#include +#include +#include +#include + +static void print_usage(void) +{ + printf("usage:\n"); + printf(" gpio-line-name \n"); +} + +int main(int argc, char **argv) +{ + struct gpio_v2_line_info info; + int fd, ret; + char *endp; + + if (argc != 3) { + print_usage(); + return EXIT_FAILURE; + } + + fd = open(argv[1], O_RDWR); + if (fd < 0) { + perror("unable to open the GPIO chip"); + return EXIT_FAILURE; + } + + memset(&info, 0, sizeof(info)); + info.offset = strtoul(argv[2], &endp, 10); + if (*endp != '\0') { + print_usage(); + return EXIT_FAILURE; + } + + ret = ioctl(fd, GPIO_V2_GET_LINEINFO_IOCTL, &info); + if (ret) { + perror("line info ioctl failed"); + return EXIT_FAILURE; + } + + printf("%s\n", info.name); + + return EXIT_SUCCESS; +} From patchwork Fri Oct 8 08:17:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1538260 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20210112.gappssmtp.com header.i=@bgdev-pl.20210112.gappssmtp.com header.a=rsa-sha256 header.s=20210112 header.b=44Onb2Ax; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=23.128.96.18; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by bilbo.ozlabs.org (Postfix) with ESMTP id 4HQh010v5hz9sP7 for ; Fri, 8 Oct 2021 19:18:09 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233992AbhJHIUC (ORCPT ); Fri, 8 Oct 2021 04:20:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234246AbhJHITz (ORCPT ); Fri, 8 Oct 2021 04:19:55 -0400 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E20F7C061774 for ; Fri, 8 Oct 2021 01:17:51 -0700 (PDT) Received: by mail-wr1-x431.google.com with SMTP id t8so27303450wri.1 for ; Fri, 08 Oct 2021 01:17:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jMwKeDx1xIluS3Y035lgALsVGvJn3c2xJ25VMV8QOGs=; b=44Onb2Ax7hY1L21iiLcu25b1dxpq5PF4lobpOLJpL1ydEwkGDGFJ8HHzuo4LoQfx8/ 5NgWQbUrpVT2qtOzQfslGWTEvnHFfXSWqVlFmlleqgkPfTfAlrOvHeaNi9uY0lVMROR8 QM3uORBIcxRruUAzzClKtQ3nCeQF3oovjPvbzBTQDNa7dh0RDpPS9Y8T4HW5FFHSxFkb Rn76uIcd9Y+jikIGysZdxQkCEe0PZBA+RkLAXLWOjqFV23ZX5uR2mMzvcjAj6qzwmZoE o/P50kWGtK9GSilGwQwix4bBS2IObgDVbXXGi3FZSCK+a5tAvsa26Lf91Ou5RkogA/oH q6og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jMwKeDx1xIluS3Y035lgALsVGvJn3c2xJ25VMV8QOGs=; b=JRRZEvOPUFZt9QZLKl+aySvY8WTVRypQrWuO4WPN/KxWwStlfOjB2BvpmcvMbAGdFa kgZEa0MUZ3QFdCUqHZO+s2/NMiHIwlKq1olmKeHCchY6j/aVaMAKf4KZ2zPo7nBsXW7I X/32LcOmrBKs+BGM1rRnv3sTbseSVVfkfGcVWU8T6cyQgC470THImw4fvgXp25OWfCUk RUduKIa/Ay1thN+mmXYbsre4coIESaM67BB5Qj+g4UKPQ5ia3ENFa0pMd3sHVD8XRGmI F5k7DRvnDHcppc/4v6AfViorHRuYSwYDjffepbzWj+Nl78vyjeH/Niw24rRvRMDldhAL Sh7g== X-Gm-Message-State: AOAM533LvUcPsot/5cSCoTjcgyBp0c5KUaFraYXtZCIm+p65691DX/px 1g0a5k8w8xycYsQ9Zgl+C+mjXA== X-Google-Smtp-Source: ABdhPJx4HOxuEXIhDRYtegBdnuxPDXO/0bwRSVBRg/yT/0C/fPwKiFK1fAfpZdSu6WLaEqaqOkF72Q== X-Received: by 2002:a5d:4601:: with SMTP id t1mr2243337wrq.298.1633681070436; Fri, 08 Oct 2021 01:17:50 -0700 (PDT) Received: from debian-brgl.home ([2a01:cb1d:334:ac00:7d50:ff5:f5c1:e225]) by smtp.gmail.com with ESMTPSA id i3sm1759530wrn.34.2021.10.08.01.17.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 01:17:50 -0700 (PDT) From: Bartosz Golaszewski To: Joel Becker , Christoph Hellwig , Shuah Khan , Linus Walleij , Andy Shevchenko , =?utf-8?q?Uwe_Kleine-K?= =?utf-8?q?=C3=B6nig?= , Geert Uytterhoeven , Kent Gibson , Jonathan Corbet , Greg Kroah-Hartman , Al Viro , Jack Winch , Viresh Kumar Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v7 8/8] selftests: gpio: add test cases for gpio-sim Date: Fri, 8 Oct 2021 10:17:39 +0200 Message-Id: <20211008081739.26807-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20211008081739.26807-1-brgl@bgdev.pl> References: <20211008081739.26807-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Add a set of tests for the new gpio-sim module. This is a pure shell test-suite and uses the helper programs available in the gpio selftests directory. These test-cases only test the functionalities exposed by the gpio-sim driver, not those handled by core gpiolib code. Signed-off-by: Bartosz Golaszewski --- tools/testing/selftests/gpio/Makefile | 2 +- tools/testing/selftests/gpio/config | 1 + tools/testing/selftests/gpio/gpio-sim.sh | 229 +++++++++++++++++++++++ 3 files changed, 231 insertions(+), 1 deletion(-) create mode 100755 tools/testing/selftests/gpio/gpio-sim.sh diff --git a/tools/testing/selftests/gpio/Makefile b/tools/testing/selftests/gpio/Makefile index d7d8f1985d99..4c6df61c76a8 100644 --- a/tools/testing/selftests/gpio/Makefile +++ b/tools/testing/selftests/gpio/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 -TEST_PROGS := gpio-mockup.sh +TEST_PROGS := gpio-mockup.sh gpio-sim.sh TEST_FILES := gpio-mockup-sysfs.sh TEST_GEN_PROGS_EXTENDED := gpio-mockup-cdev gpio-chip-info gpio-line-name diff --git a/tools/testing/selftests/gpio/config b/tools/testing/selftests/gpio/config index ce100342c20b..409a8532facc 100644 --- a/tools/testing/selftests/gpio/config +++ b/tools/testing/selftests/gpio/config @@ -1,3 +1,4 @@ CONFIG_GPIOLIB=y CONFIG_GPIO_CDEV=y CONFIG_GPIO_MOCKUP=m +CONFIG_GPIO_SIM=m diff --git a/tools/testing/selftests/gpio/gpio-sim.sh b/tools/testing/selftests/gpio/gpio-sim.sh new file mode 100755 index 000000000000..fcca6ec611f8 --- /dev/null +++ b/tools/testing/selftests/gpio/gpio-sim.sh @@ -0,0 +1,229 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# Copyright (C) 2021 Bartosz Golaszewski + +BASE_DIR=`dirname $0` +CONFIGFS_DIR="/sys/kernel/config/gpio-sim" +PENDING_DIR=$CONFIGFS_DIR/pending +LIVE_DIR=$CONFIGFS_DIR/live +MODULE="gpio-sim" + +fail() { + echo "$*" >&2 + echo "GPIO $MODULE test FAIL" + exit 1 +} + +skip() { + echo "$*" >&2 + echo "GPIO $MODULE test SKIP" + exit 4 +} + +configfs_cleanup() { + for DIR in `ls $LIVE_DIR`; do + mv $LIVE_DIR/$DIR $PENDING_DIR + done + + for DIR in `ls $PENDING_DIR`; do + rmdir $PENDING_DIR/$DIR + done +} + +create_pending_chip() { + local NAME="$1" + local LABEL="$2" + local NUM_LINES="$3" + local LINE_NAMES="$4" + local CHIP_DIR="$PENDING_DIR/$NAME" + + mkdir $CHIP_DIR + test -n "$LABEL" && echo $LABEL > $CHIP_DIR/label + test -n "$NUM_LINES" && echo $NUM_LINES > $CHIP_DIR/num_lines + if [ -n "$LINE_NAMES" ]; then + echo $LINE_NAMES 2> /dev/null > $CHIP_DIR/line_names + # This one can fail + if [ "$?" -ne "0" ]; then + return 1 + fi + fi +} + +create_live_chip() { + local CHIP_DIR="$PENDING_DIR/$1" + + create_pending_chip "$@" || fail "unable to create the chip configfs item" + mv $CHIP_DIR $LIVE_DIR || fail "unable to commit the chip configfs item" +} + +remove_pending_chip() { + local NAME="$1" + + rmdir $PENDING_DIR/$NAME || fail "unable to remove the chip configfs item" +} + +remove_live_chip() { + local NAME="$1" + + mv $LIVE_DIR/$NAME $PENDING_DIR || fail "unable to uncommit the chip configfs item" + remove_pending_chip "$@" +} + +configfs_chip_name() { + local CHIP="$1" + + cat $LIVE_DIR/$CHIP/chip_name 2> /dev/null || return 1 +} + +configfs_dev_name() { + local CHIP="$1" + + cat $LIVE_DIR/$CHIP/dev_name 2> /dev/null || return 1 +} + +get_chip_num_lines() { + local CHIP="$1" + + $BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP` num-lines +} + +get_chip_label() { + local CHIP="$1" + + $BASE_DIR/gpio-chip-info /dev/`configfs_chip_name $CHIP` label +} + +get_line_name() { + local CHIP="$1" + local OFFSET="$2" + + $BASE_DIR/gpio-line-name /dev/`configfs_chip_name $CHIP` $OFFSET +} + +sysfs_set_pull() { + local CHIP="$1" + local OFFSET="$2" + local PULL="$3" + local SYSFSPATH="/sys/devices/platform/`configfs_dev_name $CHIP`/line-ctrl/gpio$OFFSET" + + echo $PULL > $SYSFSPATH +} + +# Load the gpio-sim module. This will pull in configfs if needed too. +modprobe gpio-sim || skip "unable to load the gpio-sim module" +# Make sure configfs is mounted at /sys/kernel/config. Wait a bit if needed. +for IDX in `seq 5`; do + if [ "$IDX" -eq "5" ]; then + skip "configfs not mounted at /sys/kernel/config" + fi + + mountpoint -q /sys/kernel/config && break + sleep 0.1 +done +# If the module was already loaded: remove all previous chips +configfs_cleanup + +trap "exit 1" SIGTERM SIGINT +trap configfs_cleanup EXIT + +echo "1. chip_name and dev_name attributes" + +echo "1.1. Chip name is communicated to user" +create_live_chip chip +test -n `cat $LIVE_DIR/chip/chip_name` || fail "chip_name doesn't work" +remove_live_chip chip + +echo "1.2. chip_name returns 'none' if the chip is still pending" +create_pending_chip chip +test "`cat $PENDING_DIR/chip/chip_name`" = "none" || fail "chip_name doesn't return 'none' for a pending chip" +remove_pending_chip chip + +echo "1.3. Device name is communicated to user" +create_live_chip chip +test -n `cat $LIVE_DIR/chip/dev_name` || fail "dev_name doesn't work" +remove_live_chip chip + +echo "1.4. dev_name returns 'none' if chip is still pending" +create_pending_chip chip +test "`cat $PENDING_DIR/chip/dev_name`" = "none" || fail "dev_name doesn't return 'none' for a pending chip" +remove_pending_chip chip + +echo "2. Creating simulated chips" + +echo "2.1. Default number of lines is 1" +create_live_chip chip +test "`get_chip_num_lines chip`" = "1" || fail "default number of lines is not 1" +remove_live_chip chip + +echo "2.2. Number of lines can be specified" +create_live_chip chip test-label 16 +test "`get_chip_num_lines chip`" = "16" || fail "number of lines is not 16" +remove_live_chip chip + +echo "2.3. Label can be set" +create_live_chip chip foobar +test "`get_chip_label chip`" = "foobar" || fail "label is incorrect" +remove_live_chip chip + +echo "2.4. Label can be left empty" +create_live_chip chip +test -z "`cat $LIVE_DIR/chip/label`" || fail "label is not empty" +remove_live_chip chip + +echo "2.5. Line names can be configured" +create_live_chip chip test-label 16 '"foo", "", "bar"' +test "`get_line_name chip 0`" = "foo" || fail "line name is incorrect" +test "`get_line_name chip 2`" = "bar" || fail "line name is incorrect" +remove_live_chip chip + +echo "2.6. Errors in line names are detected" +create_pending_chip chip test-label 8 '"foo", bar' && fail "incorrect line name accepted" +remove_pending_chip chip +create_pending_chip chip test-label 8 '"foo" "bar"' && fail "incorrect line name accepted" +remove_pending_chip chip + +echo "2.7. Multiple chips can be created" +create_live_chip chip0 +create_live_chip chip1 +create_live_chip chip2 +remove_live_chip chip0 +remove_live_chip chip1 +remove_live_chip chip2 + +echo "3. Controlling simulated chips" + +echo "3.3. Pull can be set over sysfs" +create_live_chip chip test-label 8 +sysfs_set_pull chip 0 1 +$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip` 0 +test "$?" = "1" || fail "pull set incorrectly" +sysfs_set_pull chip 0 0 +$BASE_DIR/gpio-mockup-cdev /dev/`configfs_chip_name chip` 1 +test "$?" = "0" || fail "pull set incorrectly" +remove_live_chip chip + +echo "3.4. Incorrect input in sysfs is rejected" +create_live_chip chip test-label 8 +SYSFS_PATH="/sys/devices/platform/`configfs_dev_name chip`/line-ctrl/gpio0" +echo 2 > $SYSFS_PATH 2> /dev/null && fail "invalid input not detectec" +remove_live_chip chip + +echo "4. Simulated GPIO chips are functional" + +echo "4.1. Values can be read from sysfs" +create_live_chip chip test-label 8 +SYSFS_PATH="/sys/devices/platform/`configfs_dev_name chip`/line-ctrl/gpio0" +test `cat $SYSFS_PATH` = "0" || fail "incorrect value read from sysfs" +$BASE_DIR/gpio-mockup-cdev -s 1 /dev/`configfs_chip_name chip` 0 & +sleep 0.1 # FIXME Any better way? +test `cat $SYSFS_PATH` = "1" || fail "incorrect value read from sysfs" +kill $! +remove_live_chip chip + +echo "4.2. Bias settings work correctly" +create_live_chip chip test-label 8 +$BASE_DIR/gpio-mockup-cdev -b pull-up /dev/`configfs_chip_name chip` 0 +test `cat $SYSFS_PATH` = "1" || fail "bias setting does not work" +remove_live_chip chip + +echo "GPIO $MODULE test PASS"