From patchwork Thu Mar 4 10:24:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447219 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=S22bEp/l; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnHM3wqMz9sW8 for ; Thu, 4 Mar 2021 21:32:15 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238537AbhCDKb2 (ORCPT ); Thu, 4 Mar 2021 05:31:28 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238517AbhCDKbQ (ORCPT ); Thu, 4 Mar 2021 05:31:16 -0500 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 8663AC061763 for ; Thu, 4 Mar 2021 02:30:08 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id w11so26994884wrr.10 for ; Thu, 04 Mar 2021 02:30:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tX+a5h0CwfebfCzPciEkOjoZw92f9+5OTwDwclahb9c=; b=S22bEp/lQ+wgPshXexE4a6L9ShjGvgoPQqJeo5HuJhaERXd8vvyoV8leavK+Nl/MlQ ClNlw7/fV5rwsH/XE8tNiuWYiBCnLyLcf2qtecFlqkIgjN1XmdLdHN4FVzL9eYe0k3v/ U0vZ03JT4aqZpEv+pfP+tgi1Y2IMrKcGcpKxgdX6CdJzJNC9nkGzMqCOiIQWyTvgpWDU zE8bs40cKa0tCqulW4V/HQnGifYlWeIJUyz3uFuNwNR0JqZE0ESm5hJAXq2Q6FuzeL/5 eZekcdY2mNNMbiBmLXlQmiNClRbfJSjiAXvyKBg+da4bs/t9CEHEO2YmjeH8p4AGQ90K dWyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tX+a5h0CwfebfCzPciEkOjoZw92f9+5OTwDwclahb9c=; b=oLALXXsLwbDOJ+eOdWq+67tMsU3PVrbNrn8stetH0wfN/QsLpLTC7RqYhi/2shqeA2 91z4NQ16dOCoT58yGTsgTrs4qjLh88zG0l0lawrF4pnhSDU6UxaecBfZvPy/fq5GevP/ PMlrmYsbTt+n6Zpszvy4kDlQu4XXm9c32J3ABaC5r5hu5KwlIAUgKi8cx/Eiu8K3D3D1 T5H1PTMxebI5DGa4FX0zMkapF73ghqM04YRtzli4pLqsCiUVw4MCXKjyPe75Iu/uXhBw jHPsZgJ0us3kxLS+9zBqgEDh19IKu0whEQhjVcyPtLILxi1UQ059A3JPl8IZP4ecNxhy jaOA== X-Gm-Message-State: AOAM5319IUe8CWnDf+Ty20tNRWP+zXVa8YaZsOyGXUZHSnETXDVfBZEB D988WA6uzdptAGjsR7xYstlcNA== X-Google-Smtp-Source: ABdhPJzfb3hQojJAvThJsbOa3ghhSHpJUZBeN8kOAVh9zhmVgfChPt5ilIUuxrIXEDA/bES0G1HONQ== X-Received: by 2002:a5d:638a:: with SMTP id p10mr3405074wru.286.1614853807255; Thu, 04 Mar 2021 02:30:07 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:07 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 01/12] configfs: increase the item name length Date: Thu, 4 Mar 2021 11:24:41 +0100 Message-Id: <20210304102452.21726-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 --- 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 2e8c69b43c64..4f76dcc08134 100644 --- a/include/linux/configfs.h +++ b/include/linux/configfs.h @@ -27,7 +27,7 @@ #include /* struct kref */ #include /* struct mutex */ -#define CONFIGFS_ITEM_NAME_LEN 20 +#define CONFIGFS_ITEM_NAME_LEN 32 struct module; From patchwork Thu Mar 4 10:24:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447220 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=jqFcOBFm; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnHy4byYz9sWk for ; Thu, 4 Mar 2021 21:32:46 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238589AbhCDKb7 (ORCPT ); Thu, 4 Mar 2021 05:31:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39592 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238520AbhCDKb2 (ORCPT ); Thu, 4 Mar 2021 05:31:28 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 26334C061765 for ; Thu, 4 Mar 2021 02:30:09 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id 7so27042935wrz.0 for ; Thu, 04 Mar 2021 02:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VTVqUZIbWOBx2vi73M7W0VDXvAw3I3R3JVZHB7Sln/A=; b=jqFcOBFmBW6OGc8pMVCLj1b0qqQuqt+YCkzk+Q27uBMQbo1iaTw6oEMoEV0AkHeBzR dWlkMEhqAPzVj08vhy2rqL3OqLGUdGOghAKhuAUT6MI+okAPLhtoAOucsEYnH2zbLaT3 9QMIOIg3wAPG++RHWS8WI+guEtK0StMdFlXIwa4Izj6Mp8z65Bh214qQsC8XcaUfdihR qFmhsDb26mfxrcaKHalzQtvIQQaAr8Too5zk0OPZW/tS96T7iFF8XoJdx6gLx55GKPCg Txiytv9H5miJ9VWDuZpIXpO/YYqrze0SMaethq2N5T5iiDXao32SdTfEkfL/ZmtLs1cH mHFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VTVqUZIbWOBx2vi73M7W0VDXvAw3I3R3JVZHB7Sln/A=; b=Vj++eoZ8WHCm8V2puCXRBcRS0SFXzVrM2a/YE+kg8FmAV0kYKV4wcOuf4RZ0tbGyHB fzaJ/S087ALD+AU2ZW6bYLtzmdyvEUpOnSOE+KoGZ0sW7aEoOvd4sFdmVfoHDHD8bqh/ 2fTDremzdFtgyktrBrw2VyWiJev9yX1DETfdpnKrtyssukRZ2FJx6+NAy1s4aqv0K8Xr +6ESiGkWTYsQAd8khSTSX/0kDNGJH4Uor86netrWjuDkPSxt3nZMfwtIXqejDOBKRvnH iv6iv/PZr+q1v+bbRl+npGipcDQeeEYIPEw13ypXR2iM8TYENJt3wwqZQ4yck/OLi5lE wm+Q== X-Gm-Message-State: AOAM531mlYQsyOtxQP1IgNWG4NVtX5PU+1/ExG2GsFrFK3lXshdyO8Mn ZOS5icPSJIazBH/UzitGIlgHCA== X-Google-Smtp-Source: ABdhPJwtsEIc4u6ANMWw8IOZ9a7RTFR2HMfg/vg90IRd5QlPPB3mPTtotQP5MkKc3Mdjx0AIJURabw== X-Received: by 2002:a05:6000:1803:: with SMTP id m3mr2828509wrh.50.1614853807980; Thu, 04 Mar 2021 02:30:07 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:07 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 02/12] configfs: use (1UL << bit) for internal flags Date: Thu, 4 Mar 2021 11:24:42 +0100 Message-Id: <20210304102452.21726-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 9a3aed249692..b495c9f043d4 100644 --- a/fs/configfs/configfs_internal.h +++ b/fs/configfs/configfs_internal.h @@ -46,16 +46,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 Thu Mar 4 10:24:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447221 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=et80zaVs; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnHy6vfWz9sPf for ; Thu, 4 Mar 2021 21:32:46 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238599AbhCDKcA (ORCPT ); Thu, 4 Mar 2021 05:32:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238528AbhCDKb2 (ORCPT ); Thu, 4 Mar 2021 05:31:28 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33C12C0613DA for ; Thu, 4 Mar 2021 02:30:10 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id j2so14170947wrx.9 for ; Thu, 04 Mar 2021 02:30:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cHXQevR0azbVrq0hWp2kTS40q+qv1kp+bdpZeu+oHxs=; b=et80zaVs61j418r525doP0ZvMuPK3cceexlaCpD+2kBxf92fj3/gDlhOsiYhTPIRRa dJ5NOsZ/dFG/p683+R8ZsMghSdq6MeL+JbthZUH8VDkhsEdT2z+CDwrZ5I45NDUCc4TX hp06KzBwsmAOTT/e/9FUSrtCNyKNd+oBLaOzlsf050NFunLKA6mSbXhXOr2yksDF6Kzc N30Ci6/dOAScI/mdV4X9/dJx5vsfz5IlbvRbbJPxzLK1VXD3/VSx0KAHew3CbMhQjTsB tD0sWV2b/Ug1JSjEt2jMM7aJJPC9vAco4W6UljgmmIPjgkjxoYLaTvMJBSUXhk0DrQYH DTsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cHXQevR0azbVrq0hWp2kTS40q+qv1kp+bdpZeu+oHxs=; b=jd33eAxGEPv08W8FMDXcBbVvy1Dsizv12ze8rO4COesbrgGGOkUWIw6631pdgCkSPe tk8hoHh6cYC03kh1PsUAZWrZNaNFMEhc57SdA24VXNf8xs+lHm0eCpw8wOnsCq66Kdzo RzkNkwGx0bj4mctp0HijX2DxD6aLV0sm3Lba+cudgIK3wrro3dQNVJtXopOtoyeV+AVM gZkm236xvbUgTHR367Qz5d1II74pGt7wO+yIqhDUXoY6C2K6saQRbK5wi64bm8SP/AOl RpXkveGXRHjn3O130iRlpPVjirLFePFLLpWMcRr95+pLOV47QrD7gRpAXPHaVgYc+XRa MbgQ== X-Gm-Message-State: AOAM531biMDcJ9WErhdqeTD5rTQrTxcGini/nOyDkcD83uJvC+DZLhk/ xnz6Dm9yG2F1bhmDncmRyRMcMw== X-Google-Smtp-Source: ABdhPJwMnA/5KMuii24nJYOh4Aeong3GymuW/F1zJHAxuEVgMUA4pGzKdABB+GHkiSwAbgV9HmApsg== X-Received: by 2002:adf:e5c4:: with SMTP id a4mr3299214wrn.174.1614853808775; Thu, 04 Mar 2021 02:30:08 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:08 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 03/12] configfs: implement committable items Date: Thu, 4 Mar 2021 11:24:43 +0100 Message-Id: <20210304102452.21726-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 --- Documentation/filesystems/configfs.rst | 6 +- fs/configfs/configfs_internal.h | 2 + fs/configfs/dir.c | 245 ++++++++++++++++++++++++- include/linux/configfs.h | 1 + 4 files changed, 245 insertions(+), 9 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 b495c9f043d4..41ac21c82bf5 100644 --- a/fs/configfs/configfs_internal.h +++ b/fs/configfs/configfs_internal.h @@ -56,6 +56,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 b6098e02e20b..f3c95c1d5278 100644 --- a/fs/configfs/dir.c +++ b/fs/configfs/dir.c @@ -656,6 +656,13 @@ static void detach_groups(struct config_group *group) inode_unlock(d_inode(child)); + /* + * 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); + d_delete(child); dput(child); } @@ -860,6 +867,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, @@ -885,11 +1020,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) @@ -966,6 +1105,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 } @@ -1457,7 +1598,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; @@ -1476,6 +1617,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; @@ -1562,9 +1709,97 @@ 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 = sd; + list_move(&sd->s_sibling, &new_parent_sd->s_children); + item->ci_parent = new_parent_item; + d_move(old_dentry, new_dentry); + 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 4f76dcc08134..ff6b0e408136 100644 --- a/include/linux/configfs.h +++ b/include/linux/configfs.h @@ -219,6 +219,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 Thu Mar 4 10:24:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447222 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=i+Zvg25x; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnHz3LVNz9sWt for ; Thu, 4 Mar 2021 21:32:47 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238555AbhCDKcA (ORCPT ); Thu, 4 Mar 2021 05:32:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238485AbhCDKbb (ORCPT ); Thu, 4 Mar 2021 05:31:31 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00B1FC0613DD for ; Thu, 4 Mar 2021 02:30:10 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id a18so18822308wrc.13 for ; Thu, 04 Mar 2021 02:30:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Bs0N+b7IbNFBoZCqiL+fVI6wVQVsEVA2VSnx/V3f4QI=; b=i+Zvg25xWRRu3Q+3OHqNie0KuSOiMHg6Had86XZY/5pU4nj64JjnzVPBvlXb100DOY 55fH5/xUvZDflCQt+qb9eJwDxrAMVcc7VTXf6LvuhVwMt1UI4r2jr3FJBwU3JDkwv7Wj HHaBp+Fcl37uh7LYBMPHYBrlascVC/XfrppRxQlYnItNZNgWlwOr6g4cybI0E7r+GYzS +3o5/Tdy1Xuc+yp9ld87CGZnogyIBpZpBLyYcUQQQ4UKcKjieo7ocJtNtt4mVUjHveiP 2bV+S5MkgpN53kZtGLSaMR+WRRpxvc3iOwYVGB0xQNFRR04gtt+9fr5q+r/0HBpEtkmV lAwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Bs0N+b7IbNFBoZCqiL+fVI6wVQVsEVA2VSnx/V3f4QI=; b=V706neAo3yy4XiABUvHGOmPDW145Q1R7qP9YA8ZzAQKdAAKq+f0HvzXxw510rrbrdr L4SsHiEjobQtTBDJm0Xb0kSHKYjn1TiWSxnoyjLt13XhIqrNzCg8lx0k8UBblMQQb3YY N2UDeOwxMskaSo6nOtZtuMA9L4ku4cCgj5RZX13x+qM0GFHo2YcvQuYGQyjn1XJPIT4y KV9PlS9b/GAwbgACokUYDHVZbjY61pJVRXKPndDTQBxyOIgy/I9BIvZ9p7swcJqvYohb VnUFLROxWMAubaUEeq9kB+cdO6+6qOp7sKskGbryd62x+Knf1PrFGrETnVWktDalw4Td WOKw== X-Gm-Message-State: AOAM532uZw3FJuwZv0EuaSA62g69ptKGQXsUJZ73OksnxG5EbBA1NW5W o+RrTMiG+LiJCsbOC7kRlmoQaQ== X-Google-Smtp-Source: ABdhPJzIhX1GD3MT99SO7tV3rh3J1lUvbyMcRtszgrTHe6AQgZOwY8ko4EmIITwwTNLJc76mCCH0Uw== X-Received: by 2002:adf:d236:: with SMTP id k22mr3317611wrh.144.1614853809697; Thu, 04 Mar 2021 02:30:09 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:09 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 04/12] samples: configfs: add a committable group Date: Thu, 4 Mar 2021 11:24:44 +0100 Message-Id: <20210304102452.21726-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 --- 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 f9008be7a8a1..9bef74e4369d 100644 --- a/samples/configfs/configfs_sample.c +++ b/samples/configfs/configfs_sample.c @@ -315,6 +315,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 @@ -326,6 +478,7 @@ static struct configfs_subsystem *example_subsys[] = { &childless_subsys.subsys, &simple_children_subsys, &group_children_subsys, + &committable_children_subsys, NULL, }; From patchwork Thu Mar 4 10:24:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447226 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=EnueNiX6; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJ14kQpz9sWk for ; Thu, 4 Mar 2021 21:32:49 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238577AbhCDKcC (ORCPT ); Thu, 4 Mar 2021 05:32:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238582AbhCDKb4 (ORCPT ); Thu, 4 Mar 2021 05:31:56 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 262C7C0613E0 for ; Thu, 4 Mar 2021 02:30:12 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id w7so7532981wmb.5 for ; Thu, 04 Mar 2021 02:30:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pCe90PiqGmxROg4pfqFFTH25K7JQCv+JP3yxH8AOnKc=; b=EnueNiX6NpYKxBrj0wY36yyGp38a1izgQNX8Hx89b9vCl82p35QuJoeFi0PlgUEKdu adhL2KupJr9zBnoE0Qj3UPOk5DXG8PTYa5do55m2LDWTrih9GhieMQGmMBel92zrhQMP SCOQ89LyCAgrDjvE/GGe6WITNj68OMGsE2uQ/aHKJvaYrYVEBdBMJ1ro6OAp/GkchOFM QLTa526fTJMiNYEzS4msmOreeOBYRGRZoGlmYhUax5bQZNM+bFN17TIrx4urSvta/XbJ cgxuZAvzP/tJdV8HivrZ+Tq3Op8aL12FAm2laj/AgEFHrerOZpyLfbUC1dbjTcSrQ0FY CEZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pCe90PiqGmxROg4pfqFFTH25K7JQCv+JP3yxH8AOnKc=; b=NYLGMS0OLf6cTYv1H6OwC25LLmlk+1ODbSiC8K0kGe/wMBZiMuhH6M/ReuBdretHlp OpiYKs9SYtwDh5GSbgSe7bB3xnNpUp5kiyGZa6noZGpBnneoUvlcTdAMCXxWO0ZQxRcf JwyxPo9SY4PF+VvBpYyeNxi5cgnsBlrq47llcLuQ9TqUcT6cyApZLFBgxrhm1/eys7l0 KNBaVQbmkdmQGGaGyCkl9mPaCUEG0UYBwNG8z86l9QOXTaNaK2jmUFWpGlg8QgmBRUWn 1y8oJdDNrxyy7QvpAdAQkgnDIkTk5nHBoP9S3Sx0NWnMa1jy0s61s9LNYJtSt/Xxic9S aAQQ== X-Gm-Message-State: AOAM533bBcErg4y6pgQCeBL7eFMCCUojb2TzQBpAgf2S6hoIXNxmbEze oFXE7qbuyZjJfJlhm5+89+icJw== X-Google-Smtp-Source: ABdhPJzXMkznue5E1BXwSAv/5Jnk3qYctYhg0S8yF4gHZKgRkHzuNTIh1oPe4Fqhe5HmfeqXk9SRtA== X-Received: by 2002:a1c:2016:: with SMTP id g22mr3213575wmg.137.1614853810754; Thu, 04 Mar 2021 02:30:10 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:10 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 05/12] lib: bitmap: remove the 'extern' keyword from function declarations Date: Thu, 4 Mar 2021 11:24:45 +0100 Message-Id: <20210304102452.21726-6-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski The 'extern' keyword doesn't have any benefits in header files. Remove it. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- include/linux/bitmap.h | 115 ++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 58 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 70a932470b2d..6939a8983026 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -118,54 +118,53 @@ * Allocation and deallocation of bitmap. * Provided in lib/bitmap.c to avoid circular dependency. */ -extern unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags); -extern unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags); -extern void bitmap_free(const unsigned long *bitmap); +unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags); +unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags); +void bitmap_free(const unsigned long *bitmap); /* * lib/bitmap.c provides these functions: */ -extern int __bitmap_equal(const unsigned long *bitmap1, - const unsigned long *bitmap2, unsigned int nbits); -extern bool __pure __bitmap_or_equal(const unsigned long *src1, - const unsigned long *src2, - const unsigned long *src3, - unsigned int nbits); -extern void __bitmap_complement(unsigned long *dst, const unsigned long *src, - unsigned int nbits); -extern void __bitmap_shift_right(unsigned long *dst, const unsigned long *src, - unsigned int shift, unsigned int nbits); -extern void __bitmap_shift_left(unsigned long *dst, const unsigned long *src, - unsigned int shift, unsigned int nbits); -extern void bitmap_cut(unsigned long *dst, const unsigned long *src, - unsigned int first, unsigned int cut, - unsigned int nbits); -extern int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, +int __bitmap_equal(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); +bool __pure __bitmap_or_equal(const unsigned long *src1, + const unsigned long *src2, + const unsigned long *src3, + unsigned int nbits); +void __bitmap_complement(unsigned long *dst, const unsigned long *src, + unsigned int nbits); +void __bitmap_shift_right(unsigned long *dst, const unsigned long *src, + unsigned int shift, unsigned int nbits); +void __bitmap_shift_left(unsigned long *dst, const unsigned long *src, + unsigned int shift, unsigned int nbits); +void bitmap_cut(unsigned long *dst, const unsigned long *src, + unsigned int first, unsigned int cut, unsigned int nbits); +int __bitmap_and(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); +void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); +void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); +int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); +void __bitmap_replace(unsigned long *dst, + const unsigned long *old, const unsigned long *new, + const unsigned long *mask, unsigned int nbits); +int __bitmap_intersects(const unsigned long *bitmap1, const unsigned long *bitmap2, unsigned int nbits); -extern void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, unsigned int nbits); -extern void __bitmap_xor(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, unsigned int nbits); -extern int __bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1, - const unsigned long *bitmap2, unsigned int nbits); -extern void __bitmap_replace(unsigned long *dst, - const unsigned long *old, const unsigned long *new, - const unsigned long *mask, unsigned int nbits); -extern int __bitmap_intersects(const unsigned long *bitmap1, - const unsigned long *bitmap2, unsigned int nbits); -extern int __bitmap_subset(const unsigned long *bitmap1, - const unsigned long *bitmap2, unsigned int nbits); -extern int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits); -extern void __bitmap_set(unsigned long *map, unsigned int start, int len); -extern void __bitmap_clear(unsigned long *map, unsigned int start, int len); - -extern unsigned long bitmap_find_next_zero_area_off(unsigned long *map, - unsigned long size, - unsigned long start, - unsigned int nr, - unsigned long align_mask, - unsigned long align_offset); +int __bitmap_subset(const unsigned long *bitmap1, + const unsigned long *bitmap2, unsigned int nbits); +int __bitmap_weight(const unsigned long *bitmap, unsigned int nbits); +void __bitmap_set(unsigned long *map, unsigned int start, int len); +void __bitmap_clear(unsigned long *map, unsigned int start, int len); + +unsigned long bitmap_find_next_zero_area_off(unsigned long *map, + unsigned long size, + unsigned long start, + unsigned int nr, + unsigned long align_mask, + unsigned long align_offset); /** * bitmap_find_next_zero_area - find a contiguous aligned zero area @@ -190,33 +189,33 @@ bitmap_find_next_zero_area(unsigned long *map, align_mask, 0); } -extern int bitmap_parse(const char *buf, unsigned int buflen, +int bitmap_parse(const char *buf, unsigned int buflen, unsigned long *dst, int nbits); -extern int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, +int bitmap_parse_user(const char __user *ubuf, unsigned int ulen, unsigned long *dst, int nbits); -extern int bitmap_parselist(const char *buf, unsigned long *maskp, +int bitmap_parselist(const char *buf, unsigned long *maskp, int nmaskbits); -extern int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, +int bitmap_parselist_user(const char __user *ubuf, unsigned int ulen, unsigned long *dst, int nbits); -extern void bitmap_remap(unsigned long *dst, const unsigned long *src, +void bitmap_remap(unsigned long *dst, const unsigned long *src, const unsigned long *old, const unsigned long *new, unsigned int nbits); -extern int bitmap_bitremap(int oldbit, +int bitmap_bitremap(int oldbit, const unsigned long *old, const unsigned long *new, int bits); -extern void bitmap_onto(unsigned long *dst, const unsigned long *orig, +void bitmap_onto(unsigned long *dst, const unsigned long *orig, const unsigned long *relmap, unsigned int bits); -extern void bitmap_fold(unsigned long *dst, const unsigned long *orig, +void bitmap_fold(unsigned long *dst, const unsigned long *orig, unsigned int sz, unsigned int nbits); -extern int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order); -extern void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order); -extern int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order); +int bitmap_find_free_region(unsigned long *bitmap, unsigned int bits, int order); +void bitmap_release_region(unsigned long *bitmap, unsigned int pos, int order); +int bitmap_allocate_region(unsigned long *bitmap, unsigned int pos, int order); #ifdef __BIG_ENDIAN -extern void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits); +void bitmap_copy_le(unsigned long *dst, const unsigned long *src, unsigned int nbits); #else #define bitmap_copy_le bitmap_copy #endif -extern unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits); -extern int bitmap_print_to_pagebuf(bool list, char *buf, +unsigned int bitmap_ord_to_pos(const unsigned long *bitmap, unsigned int ord, unsigned int nbits); +int bitmap_print_to_pagebuf(bool list, char *buf, const unsigned long *maskp, int nmaskbits); #define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) & (BITS_PER_LONG - 1))) @@ -265,9 +264,9 @@ static inline void bitmap_copy_clear_tail(unsigned long *dst, * therefore conversion is not needed when copying data from/to arrays of u32. */ #if BITS_PER_LONG == 64 -extern void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, +void bitmap_from_arr32(unsigned long *bitmap, const u32 *buf, unsigned int nbits); -extern void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, +void bitmap_to_arr32(u32 *buf, const unsigned long *bitmap, unsigned int nbits); #else #define bitmap_from_arr32(bitmap, buf, nbits) \ From patchwork Thu Mar 4 10:24:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447231 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=tuRBMG8H; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJb6VlBz9sW8 for ; Thu, 4 Mar 2021 21:33:19 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238687AbhCDKcc (ORCPT ); Thu, 4 Mar 2021 05:32:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238656AbhCDKcI (ORCPT ); Thu, 4 Mar 2021 05:32:08 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 058B6C0613E3 for ; Thu, 4 Mar 2021 02:30:13 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id k66so9130327wmf.1 for ; Thu, 04 Mar 2021 02:30:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jzLcn/9JFFu/zAnz2fz4llMxe0ZNESpsBe/AAdOPAB8=; b=tuRBMG8HzrbDNFK8qAYS22PR4XQ2So85s7DniK8OWce7W92hEoiJwjeBU2i2PY3X6l ouGt9uuEdG/TNzuKbXDdMBzv60SNx9ywfNVhzNiAo7JXpYnaKPPFL11M4k2tl7iFSrte ZXlJOQfCvCeEvhP5Xt9/W1FScPmHLQgb+PHAu3Tb8sSdiF8LUGhBpuPjC8OYpwIdJDaT 9+JTGwgtBqmhkH5eT0PzY0YZ+w2S7TY5fiHBnRcNEr5rmRDDOVv5KNekn9HZ8xJTwDjO 5WhJoGB6Q6g96ok0hzPlUyDW1B1RJyptPiXTZ84Amg1hKWsWGlLuwuY6lpZNoJTg6gTU jMhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jzLcn/9JFFu/zAnz2fz4llMxe0ZNESpsBe/AAdOPAB8=; b=LLRceZx9BwYWfaoPeA+3vniqbCRjEh+1hGGWomgOOFOqNlvG3TVP+I/tZR6ghmmpt4 Kl7YlGYMS7/f6blWSb301ECxyBh3cMSwgKiAVq7m1+uVh94FYO6aIofpL6tdde6Ki4w1 j+1o0Wo7tetPrSmR2nb8qd5zz2TAtINS5xF1+GFCUvplLW2oatFfU3IWTotOW0sBkO4A KYVSuoNKA9XtI/5LTk7MIKVrAaIigefEKscoVA9FzVCv/5wKGv2PqDqZZ4EcAEpj9Prv vUTsudJxxJ4Z1tT6F5P4m/lByvf/IQ2gzeIQ7juH+zlXh7gBxO+5er6Nvig8wM9MJufl mAgA== X-Gm-Message-State: AOAM532mBwHdHJb0ouXyrYorZXjZk7lKSj0deqoIt0CM0vGda7bK/h7B +EYalv05AvaDhmKGfkTbDiha8g== X-Google-Smtp-Source: ABdhPJwOYvL1KO3KRXjN8n30tg8GfdxYxQb08ru+C3Jv/q3rkh1v5cZ0T6iJXrCdfEmnn+bpte48GA== X-Received: by 2002:a05:600c:2204:: with SMTP id z4mr3303682wml.31.1614853811561; Thu, 04 Mar 2021 02:30:11 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:11 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 06/12] lib: bitmap: order includes alphabetically Date: Thu, 4 Mar 2021 11:24:46 +0100 Message-Id: <20210304102452.21726-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski For better readability and maintenance: order the includes in bitmap source files alphabetically. Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- include/linux/bitmap.h | 4 ++-- lib/bitmap.c | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 6939a8983026..3282db97e06c 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -4,10 +4,10 @@ #ifndef __ASSEMBLY__ -#include #include -#include #include +#include +#include /* * bitmaps provide bit arrays that consume one or more unsigned diff --git a/lib/bitmap.c b/lib/bitmap.c index 75006c4036e9..78f70d9007ad 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -3,17 +3,18 @@ * lib/bitmap.c * Helper functions for bitmap.h. */ -#include -#include -#include -#include + #include #include #include +#include +#include +#include #include #include #include #include +#include #include #include From patchwork Thu Mar 4 10:24:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447227 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=PkWRHiRL; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJ171tPz9sPf for ; Thu, 4 Mar 2021 21:32:49 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238602AbhCDKcB (ORCPT ); Thu, 4 Mar 2021 05:32:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238561AbhCDKbn (ORCPT ); Thu, 4 Mar 2021 05:31:43 -0500 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A4396C0613E4 for ; Thu, 4 Mar 2021 02:30:13 -0800 (PST) Received: by mail-wr1-x430.google.com with SMTP id b18so20548571wrn.6 for ; Thu, 04 Mar 2021 02:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zgmBZ3NhwrAFJO5K/6mUJqd+FqbPtvHWFuPSvB1rVno=; b=PkWRHiRLD+YMkJpjEQ+Auf3+aLRwhQw3pzL+dX7Olf+ktGDhvHpbFJzjIWK/lgsb7F iCIq5HbuqX7xOk8gIOL4B2oDf/kyb8pGKDNSb22rvwqmnq9uuLje5Hts6OzUKkuQuNFL gyPZz7WQL5e/Qg3Ypp/G/J40mGn8azHs8XsFCTBMIUtyPY2/gIplOlFxzyFAUvSfLLa+ HNn1YxF18H9EOZ0XAVu+A/FJVNZUMqT2RQe/QBp0sht0RT1z4Qz3s1bxE3fuxw93QP3S ToR6tfdbQ8k1j5PEGTRYK0puO9BliZ3qGUEg/rjMGxaywcb/mi9xJAmijZk/Itsy4Ki5 58nA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zgmBZ3NhwrAFJO5K/6mUJqd+FqbPtvHWFuPSvB1rVno=; b=h3tyMoXy1BVyshCHFL1zbVWr4Rp6gZpUFecJFOQqf8yPgfLfNL6son8LT+QgtZ2hAo FSwvDif9SlM4OsqnUms8SR+8EW4qThRIx60a9lOnhghwIMbs/uTqPK9uVcliUW9v1fKI MyEDC3QXXbzG8LFX93CfXWoz0N2JVymHuqNLUNAg2/pjgqniCi49zdxNaMtG4xTWtk5o rX5Wym+DdZlnDqrh/IaPE7mVAlXOvVqs7lUP1M/vns3qfM0Jq84cw/NfkYWPK04PFu09 l6O3XFmlvcogB8+S5zjwDxkYOeYihAx2SF9YsHm2Q/UX/J6Kp4LTrFyRw/eF8MdJlc/c yR6w== X-Gm-Message-State: AOAM532VYlt8hYpzptBNjUowtMha38xOLE3PrHlXMCxW23E24kJ0/D4b t5L8zTiDGC82U1w6YHbQj66N7A== X-Google-Smtp-Source: ABdhPJz9p7+9VIFUiU1DyC4PUnukE4hXhlfIEHq7OqPG6JVs8r3iYqSvgeNYXWJPTUXk95fiSH71Sw== X-Received: by 2002:adf:b60e:: with SMTP id f14mr3247833wre.99.1614853812430; Thu, 04 Mar 2021 02:30:12 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:12 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 07/12] lib: bitmap: provide devm_bitmap_alloc() and devm_bitmap_zalloc() Date: Thu, 4 Mar 2021 11:24:47 +0100 Message-Id: <20210304102452.21726-8-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Provide managed variants of bitmap_alloc() and bitmap_zalloc(). Signed-off-by: Bartosz Golaszewski Reviewed-by: Andy Shevchenko --- include/linux/bitmap.h | 10 ++++++++++ lib/bitmap.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/bitmap.h b/include/linux/bitmap.h index 3282db97e06c..e41c622db1b8 100644 --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -9,6 +9,8 @@ #include #include +struct device; + /* * bitmaps provide bit arrays that consume one or more unsigned * longs. The bitmap interface and available operations are listed @@ -122,6 +124,14 @@ unsigned long *bitmap_alloc(unsigned int nbits, gfp_t flags); unsigned long *bitmap_zalloc(unsigned int nbits, gfp_t flags); void bitmap_free(const unsigned long *bitmap); +/* + * Managed variants of the above. + */ +unsigned long *devm_bitmap_alloc(struct device *dev, + unsigned int nbits, gfp_t flags); +unsigned long *devm_bitmap_zalloc(struct device *dev, + unsigned int nbits, gfp_t flags); + /* * lib/bitmap.c provides these functions: */ diff --git a/lib/bitmap.c b/lib/bitmap.c index 78f70d9007ad..b4fd7fd084c6 100644 --- a/lib/bitmap.c +++ b/lib/bitmap.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -1263,6 +1264,38 @@ void bitmap_free(const unsigned long *bitmap) } EXPORT_SYMBOL(bitmap_free); +static void devm_bitmap_free(void *data) +{ + unsigned long *bitmap = data; + + bitmap_free(bitmap); +} + +unsigned long *devm_bitmap_alloc(struct device *dev, + unsigned int nbits, gfp_t flags) +{ + unsigned long *bitmap; + int ret; + + bitmap = bitmap_alloc(nbits, flags); + if (!bitmap) + return NULL; + + ret = devm_add_action_or_reset(dev, devm_bitmap_free, bitmap); + if (ret) + return NULL; + + return bitmap; +} +EXPORT_SYMBOL(devm_bitmap_alloc); + +unsigned long *devm_bitmap_zalloc(struct device *dev, + unsigned int nbits, gfp_t flags) +{ + return devm_bitmap_alloc(dev, nbits, flags | __GFP_ZERO); +} +EXPORT_SYMBOL(devm_bitmap_zalloc); + #if BITS_PER_LONG == 64 /** * bitmap_from_arr32 - copy the contents of u32 array of bits to bitmap From patchwork Thu Mar 4 10:24:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447223 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=tvmTdM0p; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJ04c2bz9sWk for ; Thu, 4 Mar 2021 21:32:48 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238572AbhCDKcC (ORCPT ); Thu, 4 Mar 2021 05:32:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238566AbhCDKbo (ORCPT ); Thu, 4 Mar 2021 05:31:44 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9072AC0613E9 for ; Thu, 4 Mar 2021 02:30:14 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id m7so798599wmq.0 for ; Thu, 04 Mar 2021 02:30:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ileJ8i8PtBYWO//WNWntFWU//j32QRx+BmjBpxepfLE=; b=tvmTdM0p73GfZuWLxHXiv2yDFa9iCw2qZ0WMOdYVQ/xrH0Detz9DXMI1vZ78cqq1rO FZJgQ1pFkmXtA7oDm61oRVjhgnQxG2Bexztzb171OBm6ll3JleAgBNUD54+Cux9bGGMs w4pUH0+gyWw7SI92E9v5XI+vV2rN+HRndqDl8PYgpzd2PPemcplAzZqeQS4grAknekbo YD74HYtEFaDRFtYPjGjI1ryuN4UTY9hJuj7q/fG3KkbN+37Jtr+QRay7IZdlw8mdNHn8 BAzK3GoQf2cHJBO2XYoITa9Y3dyPQG411MRIiW2wOmfsXPVqVFbjcnNDqPYVRY4rroxa 0gww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ileJ8i8PtBYWO//WNWntFWU//j32QRx+BmjBpxepfLE=; b=cZ0v/XynVCxomnAt8vFRKzlN5e08y67ITz9DhYQCFihRbzbOSEMBa9gMWV5QLBm764 guhqMdW0Vz+IcEhna4JYu5JCHLtLhThzkYrXKeuS6w0Un8S7l5JxvfeL5VHb81QDYCtx aiXo3OghNZ6kbJfNmZkiNjr5Fj7/F5BxoIHGETjc9VCBi0ubFnr976h4Kd3Z/8WoKYzv 8gMQWNhckQnQYeFPyG+WayXrtHgkUxhbOFV3ShYgA/YUq2CwMEhw8fJXLLCGWoLc5SgO aT3191Ai4narcR8J70OBPYrrDyWwceTqXJmLD7M0ygLIgixFdqNtcgRAkQ/HT0RmjWKd /deg== X-Gm-Message-State: AOAM530fsvettPYlEfmQd0qMEgRq9puU0hlF7AsrDN+PU02eJ22O7JhD xtnkv0uHLBh6jm6jBwtetsTFhQ== X-Google-Smtp-Source: ABdhPJwkM34kJi5nQesphQNuYENooFAw7OGpfBJegp+yS/GnfmJ1rzXhNx1/ymXUxz5/gZVJNcl9WQ== X-Received: by 2002:a1c:2e8f:: with SMTP id u137mr3197705wmu.178.1614853813151; Thu, 04 Mar 2021 02:30:13 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:12 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 08/12] drivers: export device_is_bound() Date: Thu, 4 Mar 2021 11:24:48 +0100 Message-Id: <20210304102452.21726-9-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski Export the symbol for device_is_bound() so that we can use it in gpio-sim to check if the simulated GPIO chip is bound before fetching its driver data from configfs callbacks in order to retrieve the name of the GPIO chip device. Signed-off-by: Bartosz Golaszewski --- drivers/base/dd.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/base/dd.c b/drivers/base/dd.c index 9179825ff646..c62c02e3490a 100644 --- a/drivers/base/dd.c +++ b/drivers/base/dd.c @@ -353,6 +353,7 @@ bool device_is_bound(struct device *dev) { return dev->p && klist_node_attached(&dev->p->knode_driver); } +EXPORT_SYMBOL_GPL(device_is_bound); static void driver_bound(struct device *dev) { From patchwork Thu Mar 4 10:24:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447228 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=xW5W/NR9; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJ22WGdz9sWt for ; Thu, 4 Mar 2021 21:32:50 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238544AbhCDKcC (ORCPT ); Thu, 4 Mar 2021 05:32:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238577AbhCDKbr (ORCPT ); Thu, 4 Mar 2021 05:31:47 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89E19C061797 for ; Thu, 4 Mar 2021 02:30:17 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id o7-20020a05600c4fc7b029010a0247d5f0so4254265wmq.1 for ; Thu, 04 Mar 2021 02:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yCWIrKl54RCFx8GrQqQlQePQo4V7mPlw65+kYdkruHs=; b=xW5W/NR9KpYNo0xU4NqtIBePcq+6Q/gJ9WCX06ULAuLzDTcsWyp5tn5rj9FvTQ3NlQ rz7qbrtrdvLMo5VG0sH11qn+MHqPSKhw+MNJccouVAEgmMhwt/NBjIlMDngj3X/WYma+ 6gk5in71irTgeD1UcsqCUyxjwJObCGyEUGmqWg4LxJXGsgHZfJLZ2iIi2Wrjq5M7chpL 2ebGPnaQy++r/+sTXjLCu9Y5xkQj5mhc1jEN7+USTrJuWVjjml4nQpjgoJURFN9frkI1 mHuV5HkL2rpYMzoFx009eHOHN2XNuL/r+bMfnFhYXNfKgurbziXhz2eg4ULFF3nHN0h1 XufQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yCWIrKl54RCFx8GrQqQlQePQo4V7mPlw65+kYdkruHs=; b=Y76hor/8MKK4KsLxwTkTSRXOZfnhK/BOpRm/4q4pG7Yq9fn5iMR8ZVWrXfEqlFxWV2 k/dklgkc+J9kdRxqk4XTF+2LQe6DC7Kn3yBzCr8Y3BeZ9V40btweiIH9YeRvPYenSybc CjaahqGs3D8ELTegqVw9F/9zy4j5w3V0E4+eP/WEKojNLtHjNBGmJcB6MR3Q+UcelOJy 9kA12mSu+guT1bOt+4r8cQ2XiYKVuxDmbspcf2J9CJ/DorMuBGyYId44YSzTuqZ/GOT0 Twjq6qvm/MNfQKzNgcVa5xufQwq59wJbN+/pMBcrJAiutSVSZbmLJKyTM4cJaOdE/3hd ZF+Q== X-Gm-Message-State: AOAM532PT3QqQVdOe0scKdcLsZ1g7V1XMXWFU00/b3A2Juho3ZKAarnD Ua0YRU9So1rGtQh1j8jnLSUZng== X-Google-Smtp-Source: ABdhPJz27pRYR80u8kJTr7vIWpJD3d+O3wO70yrk42jZHOdgDFAjaXA9wSp8ymBNq34BZLreAWYaMA== X-Received: by 2002:a05:600c:409:: with SMTP id q9mr3261421wmb.105.1614853813981; Thu, 04 Mar 2021 02:30:13 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:13 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 09/12] gpio: sim: new testing module Date: Thu, 4 Mar 2021 11:24:49 +0100 Message-Id: <20210304102452.21726-10-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 --- Documentation/admin-guide/gpio/gpio-sim.rst | 72 ++ drivers/gpio/Kconfig | 8 + drivers/gpio/Makefile | 1 + drivers/gpio/gpio-sim.c | 878 ++++++++++++++++++++ 4 files changed, 959 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 e3607ec4c2e8..b6b6150d0d04 100644 --- a/drivers/gpio/Kconfig +++ b/drivers/gpio/Kconfig @@ -1633,6 +1633,14 @@ config GPIO_MOCKUP tools/testing/selftests/gpio/gpio-mockup.sh. Reference the usage in it. +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 c58a90a3c3b1..d717aa85f8e1 100644 --- a/drivers/gpio/Makefile +++ b/drivers/gpio/Makefile @@ -130,6 +130,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..0ed297127bca --- /dev/null +++ b/drivers/gpio/gpio-sim.c @@ -0,0 +1,878 @@ +// 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" + +/* + * This normally should correspond with the number of attributes exposed over + * configfs + sentinel. + */ +#define GPIO_SIM_MAX_PROP 4 + +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 = sprintf(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; + + 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_zalloc(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", + }, + .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; + + /* + * 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 && device_is_bound(&pdev->dev)) + ret = sprintf(page, "%s\n", dev_name(&pdev->dev)); + else + ret = -ENODEV; + 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 platform_device *pdev; + struct gpio_sim_chip *chip; + int ret; + + mutex_lock(&config->lock); + pdev = config->pdev; + if (pdev && device_is_bound(&pdev->dev)) { + chip = dev_get_drvdata(&pdev->dev); + ret = sprintf(page, "%s\n", dev_name(&chip->gc.gpiodev->dev)); + } else { + ret = -ENODEV; + } + 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; + } + + /* + * FIXME If anyone knows a better way to parse that - please let me + * know. + */ + 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 = kzalloc(name_len + 1, GFP_KERNEL); + if (!name) { + err = -ENOMEM; + goto err_out; + } + + memcpy(name, start, name_len); + 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); + 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; + + config = kzalloc(sizeof(*config), GFP_KERNEL); + if (!config) + return ERR_PTR(-ENOMEM); + + config_item_init_type_name(&config->item, name, + &gpio_sim_chip_config_type); + config->num_lines = 1; + 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 property_entry properties[GPIO_SIM_MAX_PROP]; + struct platform_device_info pdevinfo; + 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); + + pdevinfo.id = ida_alloc(&gpio_sim_ida, GFP_KERNEL); + if (pdevinfo.id < 0) { + mutex_unlock(&config->lock); + return pdevinfo.id; + } + + pdevinfo.name = "gpio-sim"; + pdevinfo.properties = properties; + + pdev = platform_device_register_full(&pdevinfo); + if (IS_ERR(pdev)) { + ida_free(&gpio_sim_ida, pdevinfo.id); + 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); + int id; + + mutex_lock(&config->lock); + id = config->pdev->id; + platform_device_unregister(config->pdev); + config->pdev = NULL; + ida_free(&gpio_sim_ida, id); + 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 Thu Mar 4 10:24:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447224 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=gHpN5klU; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJ06vK5z9sPf for ; Thu, 4 Mar 2021 21:32:48 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238607AbhCDKcB (ORCPT ); Thu, 4 Mar 2021 05:32:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238572AbhCDKbo (ORCPT ); Thu, 4 Mar 2021 05:31:44 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EB32FC061788 for ; Thu, 4 Mar 2021 02:30:15 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id m7so798676wmq.0 for ; Thu, 04 Mar 2021 02:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5N+MBnaBVQGCO/pRFImYa4+XMDd5iqbeG7mNtUIosOw=; b=gHpN5klU7JQxNonCvCtMPp1igK591qAoysgQeeGZdcjlrws6Bp58/0R1uU1wAPHPEh 12xujkbkBQ1YDhyNQPmjh9HvqXy+74U4WRbaRhbKa/j9spySctNtmDhDu2ssrfIUj2sX NNcV2f1gfs1RyJR9h3pwB8jqsghUh/THuhF8/V6pvQO+adFFB9wQ25RF4xdFJueJOYtq HraM02Ebds+0qONLE4N8lxu770LXGqZWtdZ5uY5sYZeAy/ces5SNSbDE+OHklyyXD1UA a9vtBKQ6eclmRKm/jQ03RWVL4TTkp1ptuaxrpRJLn306h6FWQRxDlvPetAhRxdpL3FDn OGzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5N+MBnaBVQGCO/pRFImYa4+XMDd5iqbeG7mNtUIosOw=; b=K8x0XUcyMUtHjIruega+6t24UU7FFhB9YUqW/HeQRz14X1Bvn78CbYfNfBjhVEfmBd /TVmZmmxT56Sb2EvqbZuu40Hwa+6W7r8vd4lYdMVGGGE/SAet4G6Wn5pVoD2O9YN7rPH b7k5kdGbp9L9d1M6IuY/xHGj4O6VLKDaQhdZcznoVE8ma3qlxAK1NBgnlmWbpxrVJ9nw 3FOym5lBRElqOn/IKsvzwYYz4XFT7q1JXyAsUbnbO4n+9r6ig2Ap6rIaJsPInYKo70+m ra2tjUK0Ym7pim4LJRZhnXJWNyiyJeE+p46aUc/h9sz8fRHUhSGQJJ+JOvAytRTmKaSG EA6g== X-Gm-Message-State: AOAM531WG4UK46nErRByYaZSqO4GTty9lEVxE+jQ7zs8k0dGnFeEohQ5 DAXoQiJdnfgHGIcroqZV5I+e5g== X-Google-Smtp-Source: ABdhPJxGsvjrc2QvO6hHXj4n4kIhldbkPug4Ob8YItFemOynzGAlYw814WIwxJ8ZYo+M3I49ZbV27w== X-Received: by 2002:a05:600c:3550:: with SMTP id i16mr3222940wmq.170.1614853814702; Thu, 04 Mar 2021 02:30:14 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:14 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 10/12] selftests: gpio: provide a helper for reading chip info Date: Thu, 4 Mar 2021 11:24:50 +0100 Message-Id: <20210304102452.21726-11-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 Thu Mar 4 10:24:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447230 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=cRP+06aq; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJb4CMrz9sPf for ; Thu, 4 Mar 2021 21:33:19 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238685AbhCDKcb (ORCPT ); Thu, 4 Mar 2021 05:32:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39768 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238671AbhCDKcJ (ORCPT ); Thu, 4 Mar 2021 05:32:09 -0500 Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1AC7C061793 for ; Thu, 4 Mar 2021 02:30:16 -0800 (PST) Received: by mail-wm1-x330.google.com with SMTP id m7so798714wmq.0 for ; Thu, 04 Mar 2021 02:30:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jvONDvt0z0lDvIPYURd0mL7FvKxWbTnZ85Tag2PzNjQ=; b=cRP+06aq2xL14nQrl4MaDz4ppkm/L6hnQOKerm3lS92Pz/iQEdmyY0XGzMkz7Lgs6x B/BblaeXYey4aqFUVWohfJbHr4FmTL/RMZIHGc5Q4p6kdouMnuxuf/x54py6JcUNVFXx jgqw0HE8/fajgRpyMTSbj9DjfR7syL7F3dODxiVwX5oeHGUs6nGtmt1q280TNqDd8Cps MYq7yFh5OkPSvBCi6n++HK7ShXsnYo+RzLTWliTL86sEcnM0JmQiTqgMroGtAvONAuTY sAKky2BsZn4haLIxKtyCGzaSb+4jFK4Ff+1E/XXI/WzjrARDFWEpQ0PLv4SASmJ4QMjs LlEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jvONDvt0z0lDvIPYURd0mL7FvKxWbTnZ85Tag2PzNjQ=; b=piYd0ydrnLEMJhE4UIsj67SYrxXG1NovELje07pKbitbeDlzqNRxBcLezvCxoUwOrA mAB5zCp2rfiYv5EC9EnjXFJxY/Xm42Y48smiF+6y3w7fY7DckgopztMLusQliWWkKeGz qzDt+2AUtGIJAcbqik4rKh4hfH24NWKLU7d72BkwZUE5ps8SdibEliNMiViDZCd5ZIbt 2SWYvjfrxsO5doxl5jdFEDWKfADt5fRV+3bHXyZZ2VC6+j4sdJD+s+NLoX/CpMf1wLEg 8mnuYIfCqYXlrnRCl61n2FWYATI7qE8EwZnGYxX71Eg3JHxjocxF7N4MFZMAJlIjFuVj ZuWQ== X-Gm-Message-State: AOAM530t3QZx7UxnGA41F001v2X9JDyZceGBqBQ9Bu1UYHAG1EbESh/I URtCFTCnvZjEogQBaASnMWPax5jKhU+GfA== X-Google-Smtp-Source: ABdhPJzfc9BRGfSahto3YKKYnRFPnO7vWQ5WUUf+KGC0JT+woS1VxullKbwJpmqu5FwFCNaVFldpRA== X-Received: by 2002:a1c:c20a:: with SMTP id s10mr3203464wmf.144.1614853815558; Thu, 04 Mar 2021 02:30:15 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:15 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 11/12] selftests: gpio: add a helper for reading GPIO line names Date: Thu, 4 Mar 2021 11:24:51 +0100 Message-Id: <20210304102452.21726-12-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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 Thu Mar 4 10:24:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 1447225 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org 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=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=bm+cIjTx; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by ozlabs.org (Postfix) with ESMTP id 4DrnJ12N97z9sWt for ; Thu, 4 Mar 2021 21:32:49 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238587AbhCDKcD (ORCPT ); Thu, 4 Mar 2021 05:32:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238586AbhCDKb6 (ORCPT ); Thu, 4 Mar 2021 05:31:58 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBFF2C0617A9 for ; Thu, 4 Mar 2021 02:30:17 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id 7so27043511wrz.0 for ; Thu, 04 Mar 2021 02:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XNP0GOSaV6O38FdFUyX0Qx0YcxxArEepxK+Me+y1rLM=; b=bm+cIjTxkth9i1+gyCY7gEOOY/uhQ2lR2vLD0gXNi4iSQJSy6dAgKUmKhdlLO3lNub +ZUwYCb2CprGcCF+1M04Y/U/zl2IXZxJr17oz01qfua/DYletcuFUZuEKNruIr/ML4Zu HP7zun7OCGpENTpYnBritQ9eS0A4iiMyAmRd+TC6XGGa0daoxt0drrZwXBTnJ4ubXKQT wRsVhZBFjtGvARzTzV1lQZDcqfaaWmpm16I5ZkG/nsAEY3ENux5ttklSaBjdrHUdaUvC iqAvACDTQWQiAYWzX4GhHrZw6xh/WDaaDNfuFh8kBMFjo5DF2VJYO77W0paGyWXzCNT5 aq5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XNP0GOSaV6O38FdFUyX0Qx0YcxxArEepxK+Me+y1rLM=; b=ZyhFeEsg4sQ9aZCyQjvJoOxmiHX+WuRQRKoDQms5acyWxIkIMUq/uVYL/cYic79pJE ZopaARbSJidxAb3MT9mCn4R9YLUiipnLsF0OtEV9cqtlho5lphCxHK5KoUqee+9STaa6 n76sbYeERJI8CNEcOuOvT0KpQ10Vsk7zL+Yzc/Pk3jO7UNJtn79jVhpw9j9KekjUj1jB oYI+2Q48FO1jmxl2nPaBxG6AIdTyT3Agv2tdxjQ4xRESDyowlDP41BpXXSDH/9sw7uG6 Nw+rQ4pdQNGjmyfvKC/MEubvHQ3rC7Bflh9eVZSVnO09HwG+eWVcatQYe3kh0Mt0unJE RuUA== X-Gm-Message-State: AOAM5316qQpWpwJOZXGt8VAmACKit9iXTkELOKhKMpvk8aJzLiNfkH86 YiQ9DMETPPCw1BkVgRfAH9hpUA== X-Google-Smtp-Source: ABdhPJzXcOg37JPCvCRkmoG/VR1TucC/GjNfp6BeXHvfBBfTHfG4HjNy0I0yVw1jzcBbgW6isMF8wA== X-Received: by 2002:adf:a4d1:: with SMTP id h17mr3238842wrb.57.1614853816427; Thu, 04 Mar 2021 02:30:16 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id f7sm35501854wre.78.2021.03.04.02.30.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Mar 2021 02:30:16 -0800 (PST) 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 Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 12/12] selftests: gpio: add test cases for gpio-sim Date: Thu, 4 Mar 2021 11:24:52 +0100 Message-Id: <20210304102452.21726-13-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20210304102452.21726-1-brgl@bgdev.pl> References: <20210304102452.21726-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org From: Bartosz Golaszewski 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..9fd13ab8bec6 --- /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 an error if chip is still pending" +create_pending_chip chip +configfs_chip_name chip && fail "chip_name doesn't return error 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 an error if chip is still pending" +create_pending_chip chip +configfs_dev_name chip && fail "dev_name doesn't return error 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"