From patchwork Mon Feb 17 14:35:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 865999 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A8EF223302 for ; Mon, 17 Feb 2025 14:36:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802966; cv=none; b=WntoKD6FYtaRjuoz/7tpE1Y2rOiaJtAWB3fMsR0jJDrJALJpCRcFS+KBKEQGvzFGj5BGXO7MCoC87pC2mV7+gW1MpoMt+Kv+jMIjczYAPHtEayTrYeJ4ubXhIm+Y9/blYKSSDlmVceU4Se0Dehdhfq96FiN4idmZi6TJPrD+G64= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802966; c=relaxed/simple; bh=d0seFRCbsm6obDyggndTpMQD/bofnkskpXmGDrRtX50=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ox0+iu8eGHlEhhahdWIQG9LoBWQDWIDt4MXbjn1f16oQdk8p4/khuxgPs9xy1ETevJ4ogB8id5jn89MBr93eoF09Pquheb6ag0pS5g/mu9mli1afQMq0vC03zpJOoACwOTHS2BulmF3LykDLs36KerSehAB2arj4oVFYPy19On4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=MV+UwdEA; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="MV+UwdEA" Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id 558613FA53 for ; Mon, 17 Feb 2025 14:36:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1739802962; bh=DFCOc2ChWOWoHT2jvhggwm0YHMFaUQk1iMuzmg5CkXY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MV+UwdEAiO67a51qlUkN7QYMJgyHKuzChZWAdhkEYM6yVLyGiqI5dEbB48M2rinLV /57QSdCJ6VKHwT/PPESt0XcLeUMdlFUWekRQViKsc6zwO7bECwaXflCfu6aQYk/PUa wcvgZ91ax2CjxBmjqnYAg+tqSs1QnGRH1JeMos9OAwL0RJA9BzqRA8jmr4ju6cA8/D HkmdNnhrEQDT9Uju5eo5mtfpnz5Tey+zRZoUmfgDLE3IkCHfN13ZM/binJqusT2RKw 80ZXzXOktE7CVQHYEmVGwHLkP0nTOkJh9iTpqgpyrVXWp1/PmPVGdmNJXsPJGJXW4s vb/o8laqICLYw== Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2fc1a4c14d4so8208125a91.0 for ; Mon, 17 Feb 2025 06:36:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739802961; x=1740407761; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DFCOc2ChWOWoHT2jvhggwm0YHMFaUQk1iMuzmg5CkXY=; b=U/amCvVNJEPUAaZRpeU0TuWIvHzodnQ0dR1fGdXGM3otUifC6tZUAu/VKLXzd0Lpo7 1GQB4IsVe2D+eew+yVzP2H4u9I1a6qPMMoYiRGxOSvMkF9kcxAlzjt112LBZIFlX38ug yBhYG4TUIlePoow5m55uE7vI4oPct81PQiyjn6ktGFNSdAeu70TrMPmDASqxPOiJ47d2 ql9LazXdiy25FJ8Gu01AowU6HiUk8lHGsPq0qLiOYPD9yxr35pyYCtt/zpKmtttAxvje 77AuYY0A3ogXBmZqCUjCQj5g0FXLmmZ6P+Nb97eH3tpc1Qr3QBG7KcAZJcRVfLAE3xZX pSlg== X-Gm-Message-State: AOJu0YxsHzpj886lp/vGUxWD/H0ETcBHkNjuaT9tnX6rqvCKiZuNfur6 3iunt/e1geCG27Y2pCimekKrJp9f75OhYEz6qLNmoqPi7vc82NvhD0WsVi8C2TPW38K5Sdc3Q2B rjabK+TmO/v1B4QrLTqoFGC/SwhnfEuy5KXmgUPSMglhT8ZrLbetjcuReRwHGPZ9nQpkN4++VSs YjhgmxKYg= X-Gm-Gg: ASbGnctQz0CzVMxtUvpeqA7rdswBJWcA2Zc+gqmPH7r8a8NyexEaP5wdRh+qGFDDjoU r/6IrwSNytDiRi+vBR9MCQZxG3svp+TfjnTX+lFf5x3B8X0EG+6L5eqwO5QYuTY6qgavtKaqvhM w89K0sMypKLKRlDOPFInJWhzgJg0ae2vZYqBEJHUPT0xOA7CS7Vx1t7H27jHmJEJgfnCDHiqC5W mqK+Q00uXg1QqmkhpwBUwaJJE33Fd/Qd0ZPWUcLfLkH52Nu5Ait5ZN6X20zqkIe2Xo3gwPQfJir QNIQiRiRszLJeeTcizy4Jcc= X-Received: by 2002:a17:90b:2f86:b0:2ee:e518:c1cb with SMTP id 98e67ed59e1d1-2fc40d14c6emr16850262a91.7.1739802960775; Mon, 17 Feb 2025 06:36:00 -0800 (PST) X-Google-Smtp-Source: AGHT+IEWwPHNmmXaHJ5chrpk4ksWCsVxcXmVulahEqAC+Tsa1D6AEEkUl9TS6Xitb3nCoTWjymM1cQ== X-Received: by 2002:a17:90b:2f86:b0:2ee:e518:c1cb with SMTP id 98e67ed59e1d1-2fc40d14c6emr16850227a91.7.1739802960423; Mon, 17 Feb 2025 06:36:00 -0800 (PST) Received: from localhost.localdomain ([240f:74:7be:1:a6da:1fd8:6ba3:4cf3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fbf98b3305sm10057757a91.6.2025.02.17.06.35.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 06:36:00 -0800 (PST) From: Koichiro Den To: linux-gpio@vger.kernel.org Cc: brgl@bgdev.pl, geert+renesas@glider.be, linus.walleij@linaro.org, maciej.borzecki@canonical.com, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/9] gpio: aggregator: protect driver attr handlers against module unload Date: Mon, 17 Feb 2025 23:35:23 +0900 Message-ID: <20250217143531.541185-2-koichiro.den@canonical.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250217143531.541185-1-koichiro.den@canonical.com> References: <20250217143531.541185-1-koichiro.den@canonical.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Both new_device_store and delete_device_store touch module global resources (e.g. gpio_aggregator_lock). To prevent race conditions with module unload, a reference needs to be held. Add try_module_get() in these handlers. For new_device_store, this eliminates what appears to be the most dangerous scenario: if an id is allocated from gpio_aggregator_idr but platform_device_register has not yet been called or completed, a concurrent module unload could fail to unregister/delete the device, leaving behind a dangling platform device/GPIO forwarder. This can result in various issues. The following simple reproducer demonstrates these problems: #!/bin/bash while :; do # note: whether 'gpiochip0 0' exists or not does not matter. echo 'gpiochip0 0' > /sys/bus/platform/drivers/gpio-aggregator/new_device done & while :; do modprobe gpio-aggregator modprobe -r gpio-aggregator done & wait Starting with the following warning, several kinds of warnings will appear and the system may become unstable: ------------[ cut here ]------------ list_del corruption, ffff888103e2e980->next is LIST_POISON1 (dead000000000100) WARNING: CPU: 1 PID: 1327 at lib/list_debug.c:56 __list_del_entry_valid_or_report+0xa3/0x120 [...] RIP: 0010:__list_del_entry_valid_or_report+0xa3/0x120 [...] Call Trace: ? __list_del_entry_valid_or_report+0xa3/0x120 ? __warn.cold+0x93/0xf2 ? __list_del_entry_valid_or_report+0xa3/0x120 ? report_bug+0xe6/0x170 ? __irq_work_queue_local+0x39/0xe0 ? handle_bug+0x58/0x90 ? exc_invalid_op+0x13/0x60 ? asm_exc_invalid_op+0x16/0x20 ? __list_del_entry_valid_or_report+0xa3/0x120 gpiod_remove_lookup_table+0x22/0x60 new_device_store+0x315/0x350 [gpio_aggregator] kernfs_fop_write_iter+0x137/0x1f0 vfs_write+0x262/0x430 ksys_write+0x60/0xd0 do_syscall_64+0x6c/0x180 entry_SYSCALL_64_after_hwframe+0x76/0x7e [...] ---[ end trace 0000000000000000 ]--- Fixes: 828546e24280 ("gpio: Add GPIO Aggregator") Signed-off-by: Koichiro Den --- drivers/gpio/gpio-aggregator.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c index 65f41cc3eafc..d668ddb2e81d 100644 --- a/drivers/gpio/gpio-aggregator.c +++ b/drivers/gpio/gpio-aggregator.c @@ -119,10 +119,15 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, struct platform_device *pdev; int res, id; + if (!try_module_get(THIS_MODULE)) + return -ENOENT; + /* kernfs guarantees string termination, so count + 1 is safe */ aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL); - if (!aggr) - return -ENOMEM; + if (!aggr) { + res = -ENOMEM; + goto put_module; + } memcpy(aggr->args, buf, count + 1); @@ -161,6 +166,7 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, } aggr->pdev = pdev; + module_put(THIS_MODULE); return count; remove_table: @@ -175,6 +181,8 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, kfree(aggr->lookups); free_ga: kfree(aggr); +put_module: + module_put(THIS_MODULE); return res; } @@ -203,13 +211,19 @@ static ssize_t delete_device_store(struct device_driver *driver, if (error) return error; + if (!try_module_get(THIS_MODULE)) + return -ENOENT; + mutex_lock(&gpio_aggregator_lock); aggr = idr_remove(&gpio_aggregator_idr, id); mutex_unlock(&gpio_aggregator_lock); - if (!aggr) + if (!aggr) { + module_put(THIS_MODULE); return -ENOENT; + } gpio_aggregator_free(aggr); + module_put(THIS_MODULE); return count; } static DRIVER_ATTR_WO(delete_device); From patchwork Mon Feb 17 14:35:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 865998 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 37BBF2248A0 for ; Mon, 17 Feb 2025 14:36:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802970; cv=none; b=kEjCFICGvDYDzIid2DQmdxWqm8kx1NS71kzivscdo83+QfqttXxCRB/MwqhE2oWv11wzfPLsXipG2eIY6IqOK3bOPuAALLmRUeSbBCRJc+WFbg36qD47rq6BNYvAB9y/sn2firYZs2MbR6DLAvm/ifmaVudg+vlpfutniXiM7Sg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802970; c=relaxed/simple; bh=aLA3ZVDhrDy3RNwYW++kaJn2SQ84AkvFmu6stXF9mto=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dPetsZHKLWz8N6nzuEVQ/H7v7qbqk/Fa+iw4BFGDnft0OSMOw29NQvE0Dba44C3WGUqUIkIraJVYsp6rGCiILU6m0xi0rrINn8hgCjAUpKqsYUaVEN0M55sEAKZJfclW3DuO7GHiXPH/n5YpqC8itF/gmW8VKmu5jj7YUxndnqA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=dwtv0Giy; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="dwtv0Giy" Received: from mail-pl1-f198.google.com (mail-pl1-f198.google.com [209.85.214.198]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id D3EF33F868 for ; Mon, 17 Feb 2025 14:36:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1739802967; bh=gXHg3uCoBKmQSJJ70+DeTAJMIxaEY0B5wJevniOwqIM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dwtv0GiyNsc/tsL6hc3qJT1uU/XMRJv/68dd1mEA1//N6T9WqGrTkgCDtaBF79oc9 RIuecyayTjC4bwBhylQ1G4UFte5xNzrerda/bHtud73Fr5VNw7mCOhbWfuf0j+59CI Kd5Va9gZNhpsLJ0X/U/ZpnRP2E9g1i7dEWw4fLr+72leLzoDvGPz0K069Yy561E8qv Ul87dTGoXGMRzUTrGikjf8gwrvBt8GjhLqop4zKouGWqfDEpG+EOKneYsQtK6QLqW1 ATkBCRKtSlvOk+bJt9DNSoC40i4bOlzjPf2b25kKWFNkFW18GRSdIEkGt5KQZV++sk 12OM+xRXNApCA== Received: by mail-pl1-f198.google.com with SMTP id d9443c01a7336-220cd43c75aso152627645ad.3 for ; Mon, 17 Feb 2025 06:36:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739802966; x=1740407766; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gXHg3uCoBKmQSJJ70+DeTAJMIxaEY0B5wJevniOwqIM=; b=HaaRCtN5LdBbP46nHj/aZcrSSE5Eau+/Y7rKXs072yvR29a2uRl9Q1gEc4/hYdo01a XQxqqQOLeaIt/CNM0da2WzEfFdB+gL5FZw/XcBGIpo+t0PkI8sjEURWmQ5/4PffvZ5yY OWDhR083/Fz0WBRh7g8nVUZyLprqT/NsDJHfLOMicljp/VIH3hPypxSw5v8isb/x7dXL JJectZG3vvRdT2atxYoTHL3Sf2id52kg/faBfWYigcwXiaf05DBJ1UPRugmyHpkoRzCY P9uOQoAFV2OBhV8x+7gTTV5QFdV14SZdGy/Rlyd03tn0Y78u1UoeJo4k9XOias0086iX GfSQ== X-Gm-Message-State: AOJu0YyzLEm394EXiXjFRL7IEFeWB0eNTQhS0HYMQmW0CpI0E7Haj1WC BXBCaisMYfunZzgqD1sSkHvdo3ItZKKk3RFAq1TP/vWEdKpywuTsg2xILoE1LRbHc6ar5cAOeeC j69R4eR+rNGW4bD+dkxe8fgMduB3CSVSowjbxKrbU6znxG2JsHeIcxonm0xrY1+m+GdnlAQq9YX NQCJYlTfs= X-Gm-Gg: ASbGncsjN0/xU+mx9muEvYTsHSxJRowX4SHRGaRvJQwcOrrl3EbO6Oa/A5AsJHihCiR /yrvrkGnmjVwDYVtw3vl9iK18TIhshVzq2vML7ItegnmDvk/W4XKCreO/dW7fnXxeGuJlTZCL2K Ctd1PrVqG+9oQwjt60BcGxavLaXtJOodIuut7TX7defxkSUdqoOSVZsG3Bq2tqltPsoiRzxaa6P omUeSDeR3xHO+z4cJS81lX1Tdlnb3zC7CpwP01OvVWQsxtTcqGY7kVDo2t5tlgXXVjWM/9xrJZt pdkRUL8vIvBbSQ4kooEESBw= X-Received: by 2002:a17:902:d542:b0:215:f1c2:fcc4 with SMTP id d9443c01a7336-221040bc31bmr169176295ad.41.1739802965887; Mon, 17 Feb 2025 06:36:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHCZIJuJa6JJ2WId5wv4vsQnYCm/8htK948V0rYbd/ALkX5nVYWeDQNlPKzKTrGrQrpSd6IAw== X-Received: by 2002:a17:902:d542:b0:215:f1c2:fcc4 with SMTP id d9443c01a7336-221040bc31bmr169175965ad.41.1739802965628; Mon, 17 Feb 2025 06:36:05 -0800 (PST) Received: from localhost.localdomain ([240f:74:7be:1:a6da:1fd8:6ba3:4cf3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fbf98b3305sm10057757a91.6.2025.02.17.06.36.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 06:36:05 -0800 (PST) From: Koichiro Den To: linux-gpio@vger.kernel.org Cc: brgl@bgdev.pl, geert+renesas@glider.be, linus.walleij@linaro.org, maciej.borzecki@canonical.com, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/9] gpio: aggregator: add aggr_alloc()/aggr_free() Date: Mon, 17 Feb 2025 23:35:25 +0900 Message-ID: <20250217143531.541185-4-koichiro.den@canonical.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250217143531.541185-1-koichiro.den@canonical.com> References: <20250217143531.541185-1-koichiro.den@canonical.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Prepare for the upcoming configfs interface. These functions will be used by both the existing sysfs interface and the new configfs interface, reducing code duplication. No functional change. Signed-off-by: Koichiro Den --- drivers/gpio/gpio-aggregator.c | 58 +++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c index 893cd56de867..a93b7d3de929 100644 --- a/drivers/gpio/gpio-aggregator.c +++ b/drivers/gpio/gpio-aggregator.c @@ -36,12 +36,41 @@ struct gpio_aggregator { struct gpiod_lookup_table *lookups; struct platform_device *pdev; + int id; char args[]; }; static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */ static DEFINE_IDR(gpio_aggregator_idr); +static int aggr_alloc(struct gpio_aggregator **aggr, size_t arg_size) +{ + struct gpio_aggregator *new __free(kfree) = NULL; + int ret; + + new = kzalloc(sizeof(*new) + arg_size, GFP_KERNEL); + if (!new) + return -ENOMEM; + + mutex_lock(&gpio_aggregator_lock); + ret = idr_alloc(&gpio_aggregator_idr, new, 0, 0, GFP_KERNEL); + mutex_unlock(&gpio_aggregator_lock); + if (ret < 0) + return ret; + + new->id = ret; + *aggr = no_free_ptr(new); + return 0; +} + +static void aggr_free(struct gpio_aggregator *aggr) +{ + mutex_lock(&gpio_aggregator_lock); + idr_remove(&gpio_aggregator_idr, aggr->id); + mutex_unlock(&gpio_aggregator_lock); + kfree(aggr); +} + static int aggr_add_gpio(struct gpio_aggregator *aggr, const char *key, int hwnum, unsigned int *n) { @@ -444,17 +473,15 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, { struct gpio_aggregator *aggr; struct platform_device *pdev; - int res, id; + int res; if (!try_module_get(THIS_MODULE)) return -ENOENT; /* kernfs guarantees string termination, so count + 1 is safe */ - aggr = kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL); - if (!aggr) { - res = -ENOMEM; + res = aggr_alloc(&aggr, count + 1); + if (res) goto put_module; - } memcpy(aggr->args, buf, count + 1); @@ -465,19 +492,10 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, goto free_ga; } - mutex_lock(&gpio_aggregator_lock); - id = idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL); - mutex_unlock(&gpio_aggregator_lock); - - if (id < 0) { - res = id; - goto free_table; - } - - aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id); + aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->id); if (!aggr->lookups->dev_id) { res = -ENOMEM; - goto remove_idr; + goto free_table; } res = aggr_parse(aggr); @@ -486,7 +504,7 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, gpiod_add_lookup_table(aggr->lookups); - pdev = platform_device_register_simple(DRV_NAME, id, NULL, 0); + pdev = platform_device_register_simple(DRV_NAME, aggr->id, NULL, 0); if (IS_ERR(pdev)) { res = PTR_ERR(pdev); goto remove_table; @@ -500,14 +518,10 @@ static ssize_t new_device_store(struct device_driver *driver, const char *buf, gpiod_remove_lookup_table(aggr->lookups); free_dev_id: kfree(aggr->lookups->dev_id); -remove_idr: - mutex_lock(&gpio_aggregator_lock); - idr_remove(&gpio_aggregator_idr, id); - mutex_unlock(&gpio_aggregator_lock); free_table: kfree(aggr->lookups); free_ga: - kfree(aggr); + aggr_free(aggr); put_module: module_put(THIS_MODULE); return res; From patchwork Mon Feb 17 14:35:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 865996 Received: from smtp-relay-internal-0.canonical.com (smtp-relay-internal-0.canonical.com [185.125.188.122]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19E4C225415 for ; Mon, 17 Feb 2025 14:36:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.122 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802985; cv=none; b=cXuPHRi+Wee/BsiymaberVw9BTi3Nhlb4erqfI8nGhA5SUw5uAIDG9u3aWLVMni8y30gLbLEvU9506lP4sDvgBgxuauesem5jZtYiwnJUxEFBLsJBzTNFzPrZiwE9G/A9KaAodly7Mys2cuF34+/skMd6DpF5bpa0bzPewgu5kA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802985; c=relaxed/simple; bh=uxdDc8NwSSoU/KhYSVoLNE3SLLqdDtG3WoHdkVTCuPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YgaqHvLPOVjGzwWu9+dgHv+7jaRYzM/HV1gy6tNIx9i17u8A2P9KuYXlbGJ8twg0a4sIloOi81ATuuKDjlI6PLoZ4VZrfOicaNl3NtdDIZvJ0jgDXBlt0qFxzOU0Ng8n7nGSpd9bFlm44RQQdmpZH1Dn2UEX2abPN+Xav+wg3BI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=DOE7aMFC; arc=none smtp.client-ip=185.125.188.122 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="DOE7aMFC" Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-0.canonical.com (Postfix) with ESMTPS id D58203F84C for ; Mon, 17 Feb 2025 14:36:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1739802975; bh=v4sbOHRoeQS1MOSALsPLtSJ65COLeoaPvTmtbuEu58k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DOE7aMFC/2UBE51wK5xkcllk38NYaS8NkY8Jz69QyOBK9mQ0L2WRSVtKnHnXS9Sj/ 9adYN6H6wL9Dq4Dbbg9TMxoYRk7+wwG3WmoHtOwhCmXPyE3KDLmxOY8RG0xTcy+CpH R0kyrR9q/Ju/l5MBjcv3wkThvkL252PxJhY9t7KE+xmJg46JQHTqxNzzxlNkw06goG mThkHYkcptWwwPDVPQxndRHxrcJtMEQVDzF5HSydzMpVGhfaBNQjOF+/xLpGNPby4X 8XWaR3Xn8wPzIGjx+0y9dyp6qO4xDDPhdFmZcQOB5jjuNLapMNtcNyhPEDwxOKtrIr kqhKUbFNjUAjQ== Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-2fbfa786a1aso14003459a91.3 for ; Mon, 17 Feb 2025 06:36:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739802973; x=1740407773; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v4sbOHRoeQS1MOSALsPLtSJ65COLeoaPvTmtbuEu58k=; b=HHwyutiHxjVTT2yMMA7mDvuUuvbDUf7/e+CZwHyFHvRPt3lmE9cYJCLW+JaT0wsGA+ 5klVQatPjaa9XrqwVCzZmIClDEYLUDK6NBlZLT0Btt/SymWFgTL6Nm8zO6fikvAQxne8 132lkmJj8HeUn1fN6vjYiQEDKXWGAnW1fT1QsRXaRzGfpRGKboMmFXH1gEDmedEgk5mR IXDZh1Qj02WXTiZo/rGRIkkkQpGjKqWteiRh9wFXc+IahJZKG4XCX8MQP4BrN6u63E8J /kt57xJKsHiGQ5L6AYTXt9x0Ahyv3VbrpfSOVuBpj/MwUMjmpVecfAXtVpISKMjjmKya ahpQ== X-Gm-Message-State: AOJu0YyWGK6eVDHb50mwk9Orj/rD+H+E6Jy+OLhgbHQOBIeHZkFsq0ZQ 8YUpsMnEuHd1f+tCQKvmqAU8idD0lOjnDi5zbcmXvMUftwoZz9+0dXJE1KzKtFtDFol/v3cZEqf CGY+WbVs+VKqpBa9rojFJMdIeNQ4nP4+9QGoTCixW8fBCAZmyjwXzOXb21JRSBshwZJT9fLMmO/ x1eUtmbUo= X-Gm-Gg: ASbGncuWqaZrccZW8gsIUvSw3fFFnXx1DZpIfo4El4ftD9NJbXLapXNWZGJyfELJq4J v8fIHK6rKTwD+pzM65NrmQDZVI1XzaBF7Nlohqy9CYxA8wPKZP76c66PsWuXfl7m8MzsVNzzH53 D5Lij1cBiwSpud4GhWnq0jS5b/7TtSdZIpMeAuR/pjJGl/dUzvtU4wLIXHN112/4ayeFzLp4YFV 5Pa8qVPjukUsAPuyZH28z0yFZM/D9awMkAZBP4IRZu9oZ7IZrd+DaOavCyHjKA/Oy/Iz9t4QukA lcQbBX7sztdnMVthB9dF66I= X-Received: by 2002:a17:90b:2fc3:b0:2ea:b564:4b31 with SMTP id 98e67ed59e1d1-2fc40f21c49mr14264471a91.19.1739802973318; Mon, 17 Feb 2025 06:36:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IG/i6ZhPeP35hWJ24mm/LIhcXbBPRykLdefs72sMWFjgXDC7lFQiPxRDAVbpwrRAEzufAxZDw== X-Received: by 2002:a17:90b:2fc3:b0:2ea:b564:4b31 with SMTP id 98e67ed59e1d1-2fc40f21c49mr14264311a91.19.1739802971508; Mon, 17 Feb 2025 06:36:11 -0800 (PST) Received: from localhost.localdomain ([240f:74:7be:1:a6da:1fd8:6ba3:4cf3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fbf98b3305sm10057757a91.6.2025.02.17.06.36.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 06:36:11 -0800 (PST) From: Koichiro Den To: linux-gpio@vger.kernel.org Cc: brgl@bgdev.pl, geert+renesas@glider.be, linus.walleij@linaro.org, maciej.borzecki@canonical.com, linux-kernel@vger.kernel.org Subject: [PATCH v4 5/9] gpio: aggregator: add 'name' attribute for custom GPIO line names Date: Mon, 17 Feb 2025 23:35:27 +0900 Message-ID: <20250217143531.541185-6-koichiro.den@canonical.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250217143531.541185-1-koichiro.den@canonical.com> References: <20250217143531.541185-1-koichiro.den@canonical.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Previously, there was no way to assign names to GPIO lines exported through the aggregator when the device was created via sysfs. Allow users to set custom line names via a 'name' attribute and expose them using swnode. Signed-off-by: Koichiro Den --- drivers/gpio/gpio-aggregator.c | 84 ++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 3 deletions(-) diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c index ec102453817b..692d90246674 100644 --- a/drivers/gpio/gpio-aggregator.c +++ b/drivers/gpio/gpio-aggregator.c @@ -61,6 +61,8 @@ struct gpio_aggregator_line { /* Line index within the aggregator device */ unsigned int idx; + /* Custom name for the virtual line */ + const char *name; /* GPIO chip label or line name */ const char *key; /* Can be negative to indicate lookup by line name */ @@ -530,10 +532,40 @@ to_gpio_aggregator_line(struct config_item *item) return container_of(group, struct gpio_aggregator_line, group); } +static struct fwnode_handle *aggr_make_device_swnode(struct gpio_aggregator *aggr) +{ + const char **line_names __free(kfree) = NULL; + struct property_entry properties[2]; + struct gpio_aggregator_line *line; + size_t num_lines; + int n = 0; + + memset(properties, 0, sizeof(properties)); + + num_lines = aggr_count_lines(aggr); + if (num_lines == 0) + return NULL; + + line_names = kcalloc(num_lines, sizeof(*line_names), GFP_KERNEL); + if (!line_names) + return ERR_PTR(-ENOMEM); + + /* The list is always sorted as new elements are inserted in order. */ + list_for_each_entry(line, &aggr->list_head, entry) + line_names[n++] = line->name ?: ""; + + properties[0] = PROPERTY_ENTRY_STRING_ARRAY_LEN( + "gpio-line-names", + line_names, num_lines); + + return fwnode_create_software_node(properties, NULL); +} + static int aggr_activate(struct gpio_aggregator *aggr) { struct platform_device_info pdevinfo; struct gpio_aggregator_line *line; + struct fwnode_handle *swnode; unsigned int n = 0; int ret = 0; @@ -545,9 +577,14 @@ static int aggr_activate(struct gpio_aggregator *aggr) if (!aggr->lookups) return -ENOMEM; + swnode = aggr_make_device_swnode(aggr); + if (IS_ERR(swnode)) + goto err_remove_lookups; + memset(&pdevinfo, 0, sizeof(pdevinfo)); pdevinfo.name = DRV_NAME; pdevinfo.id = aggr->id; + pdevinfo.fwnode = swnode; /* The list is always sorted as new elements are inserted in order. */ list_for_each_entry(line, &aggr->list_head, entry) { @@ -559,7 +596,7 @@ static int aggr_activate(struct gpio_aggregator *aggr) */ if (!line->key || line->idx != n) { ret = -EINVAL; - goto err_remove_lookups; + goto err_remove_swnode; } if (line->offset < 0) @@ -567,13 +604,13 @@ static int aggr_activate(struct gpio_aggregator *aggr) else ret = aggr_add_gpio(aggr, line->key, line->offset, &n); if (ret) - goto err_remove_lookups; + goto err_remove_swnode; } aggr->lookups->dev_id = kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->id); if (!aggr->lookups->dev_id) { ret = -ENOMEM; - goto err_remove_lookups; + goto err_remove_swnode; } gpiod_add_lookup_table(aggr->lookups); @@ -587,6 +624,8 @@ static int aggr_activate(struct gpio_aggregator *aggr) err_remove_lookup_table: kfree(aggr->lookups->dev_id); gpiod_remove_lookup_table(aggr->lookups); +err_remove_swnode: + fwnode_remove_software_node(swnode); err_remove_lookups: kfree(aggr->lookups); @@ -658,6 +697,43 @@ gpio_aggr_line_key_store(struct config_item *item, const char *page, } CONFIGFS_ATTR(gpio_aggr_line_, key); +static ssize_t +gpio_aggr_line_name_show(struct config_item *item, char *page) +{ + struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); + struct gpio_aggregator *aggr = line->parent; + + guard(mutex)(&aggr->lock); + + return sysfs_emit(page, "%s\n", line->name ?: ""); +} + +static ssize_t +gpio_aggr_line_name_store(struct config_item *item, const char *page, + size_t count) +{ + struct gpio_aggregator_line *line = to_gpio_aggregator_line(item); + struct gpio_aggregator *aggr = line->parent; + + char *name __free(kfree) = kstrndup(skip_spaces(page), count, + GFP_KERNEL); + if (!name) + return -ENOMEM; + + strim(name); + + guard(mutex)(&aggr->lock); + + if (aggr_is_active(aggr)) + return -EBUSY; + + kfree(line->name); + line->name = no_free_ptr(name); + + return count; +} +CONFIGFS_ATTR(gpio_aggr_line_, name); + static ssize_t gpio_aggr_line_offset_show(struct config_item *item, char *page) { @@ -707,6 +783,7 @@ CONFIGFS_ATTR(gpio_aggr_line_, offset); static struct configfs_attribute *gpio_aggr_line_attrs[] = { &gpio_aggr_line_attr_key, + &gpio_aggr_line_attr_name, &gpio_aggr_line_attr_offset, NULL }; @@ -795,6 +872,7 @@ gpio_aggr_line_release(struct config_item *item) aggr_line_del(aggr, line); kfree(line->key); + kfree(line->name); kfree(line); } From patchwork Mon Feb 17 14:35:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 865997 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7529C223324 for ; Mon, 17 Feb 2025 14:36:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802980; cv=none; b=QbwuKm4mNGU9nWb+eaRmNI96JSapWu+OiwaXWO5UUS5piWsZWhyXqSa4AApbAv9JOk7ZsoKOzq1w3p8OBT90/gbq26H6QlbBxoI1dz5nOkZLwYjnRVl4zoxiNb72OWpm65hJI51/cieyxHnB3EfQTosHYql1iPNE7i7yke5k5As= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802980; c=relaxed/simple; bh=iH8P3Qo2jiFyP4RGsTFt/HsQ3VlXvEit9tWzKXSKuck=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CVGo5L2RDU8S+VwQ6wbfEON+MOBrItdiW2AfnQ5F0AG1hgWMmJuUX13EXX3IrPnXQ4z5VhqeeEK95HnMDeqkzObxkKTxKzjbsYgWf+anhvHrTIj3BM9MvQvrsq+OrT9TOo/0ae83lYsWuKhUfLZS1hxX23s9kUOIgfR1LU85X/E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=u3i9lFTn; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="u3i9lFTn" Received: from mail-pj1-f70.google.com (mail-pj1-f70.google.com [209.85.216.70]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id D52C140133 for ; Mon, 17 Feb 2025 14:36:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1739802976; bh=FjdYUqaie9BiRKi7ISU7HN1qGBGl47EuEmlrMDzrJcg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u3i9lFTn29dtnRj+nr8uk/jjB+ebZgPXh3m8SibRXu3Uu5mp6D7CqIjgcFEaYAweF KoVUL95j8e/mYKmSIb4D/hYzEdEBWUqEiyAqyLEBT/mrXE0kr0Cagz/+zdWJZIsjTc ZjEgUkYRTLq0GPf30E8u+4LIA+hVBAqhPLltfL546K36oX+ETpqb3889xo/PvyRs8P 1E8W0TbVlbEfyIkT/MLTDmsEjiMY0+trneur6LSuvFZ5dMzYmmoOyojsUwPEeV8pGr Nk/H6Fs1LTloJvH5N82wzYYRZmC6uKdcnjn6gpKM7ws8ExfZ9ve5JSc/TpBMT7x20J SzNHbsOqBg8jw== Received: by mail-pj1-f70.google.com with SMTP id 98e67ed59e1d1-2fc0bc05afdso8946060a91.0 for ; Mon, 17 Feb 2025 06:36:16 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739802975; x=1740407775; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=FjdYUqaie9BiRKi7ISU7HN1qGBGl47EuEmlrMDzrJcg=; b=CMVFx7CiLHEBqw7YUqozUhp/ZDswQuGO4kT6HwRCyUw6l0xf4SsWyg8qJzxvLgNRET jNX8liUqO0tw2478vOMv6eB0MW3lmdZLIaugcPDi10Z4lBhbN2c7+lut1+xM8bhaPeq9 mb1SHFJjXwGy0BHYjljte/X+wXlmr5IN7wZc/CN/3SAPcOTfCKRXZbWJx21VYmM7aWAi MIAtwJGzSMAgheoSlEODUX9RsjAotDGBU/oQBXBKAUOyB3SBZu+EL/6VeYZXgOK5eZE3 F+KC6VlLri881EjiBreTfuAckgMNUExQdRW+pkEicLR28vU1c1vXrXBVDcZr6M6fJwXJ UzJA== X-Gm-Message-State: AOJu0YygifNwQX1lLwgGg0LNfw8SPw8IWNnp/j+zKa+wOPk/Wddsr7GC lIcCNVGItDj/RsQHvIIzZPQkTSZazYMH17xpyeS6Boik8LqVd6t31MIYSPFNWwJPIsv7KYFr6M3 WdX2LegIHV7IqqSZQMSUVFlsuszph5Ca7mzW/lOXUITZQU3+DKSBJVBqztmfmNak/YmAwq0+F9Z p81w8w5oU= X-Gm-Gg: ASbGnctoIctFsNY2YGNjkeXE1q8thiB6ILPHamnnLVhVcMtVCRakAB+cGsq2JRgEAp2 jtr45hyPC5PmPo9Teka+6NPTh0/hvZeO1QAsADFIiNVdHt1nj3d+WUk0mKkKtNIgfolwpjsp5eC RWanX0yuZO9VnnKyvJPINBq9wAAZCni0e8sO+KOZBIA0Odyjtg8FNiyHK+n6AEyrXlzkOUxIMT0 HCVGbGmgYeGvyTBzdPZeF1s3cwQT1tyTG6pKyl4bSUumgxTnhl+z4CLkhsZC74nopFOklwovC1L /KhNRmnlqkP59Yp4Xhq28cI= X-Received: by 2002:a17:90b:3812:b0:2f4:423a:8fb2 with SMTP id 98e67ed59e1d1-2fc40f21df3mr15661127a91.20.1739802975191; Mon, 17 Feb 2025 06:36:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZKsGQGj5NNzfd+iZhP7H4YbLKGbz+dWsQlDfkLsBVAGiKwA+5hq+mhIdi3+Xkw1F8OltQCQ== X-Received: by 2002:a17:90b:3812:b0:2f4:423a:8fb2 with SMTP id 98e67ed59e1d1-2fc40f21df3mr15661097a91.20.1739802974889; Mon, 17 Feb 2025 06:36:14 -0800 (PST) Received: from localhost.localdomain ([240f:74:7be:1:a6da:1fd8:6ba3:4cf3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fbf98b3305sm10057757a91.6.2025.02.17.06.36.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 06:36:14 -0800 (PST) From: Koichiro Den To: linux-gpio@vger.kernel.org Cc: brgl@bgdev.pl, geert+renesas@glider.be, linus.walleij@linaro.org, maciej.borzecki@canonical.com, linux-kernel@vger.kernel.org Subject: [PATCH v4 6/9] gpio: aggregator: rename 'name' to 'key' in aggr_parse() Date: Mon, 17 Feb 2025 23:35:28 +0900 Message-ID: <20250217143531.541185-7-koichiro.den@canonical.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250217143531.541185-1-koichiro.den@canonical.com> References: <20250217143531.541185-1-koichiro.den@canonical.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Rename the local variable 'name' in aggr_parse() to 'key' because struct gpio_aggregator_line now uses the 'name' field for the custom line name and the local variable actually represents a 'key'. This change prepares for the next but one commit. No functional change. Reviewed-by: Geert Uytterhoeven Signed-off-by: Koichiro Den --- drivers/gpio/gpio-aggregator.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/gpio/gpio-aggregator.c b/drivers/gpio/gpio-aggregator.c index 692d90246674..2e993c9a7ce5 100644 --- a/drivers/gpio/gpio-aggregator.c +++ b/drivers/gpio/gpio-aggregator.c @@ -988,7 +988,7 @@ static struct configfs_subsystem gpio_aggr_subsys = { static int aggr_parse(struct gpio_aggregator *aggr) { char *args = skip_spaces(aggr->args); - char *name, *offsets, *p; + char *key, *offsets, *p; unsigned int i, n = 0; int error = 0; @@ -997,18 +997,18 @@ static int aggr_parse(struct gpio_aggregator *aggr) if (!bitmap) return -ENOMEM; - args = next_arg(args, &name, &p); + args = next_arg(args, &key, &p); while (*args) { args = next_arg(args, &offsets, &p); p = get_options(offsets, 0, &error); if (error == 0 || *p) { /* Named GPIO line */ - error = aggr_add_gpio(aggr, name, U16_MAX, &n); + error = aggr_add_gpio(aggr, key, U16_MAX, &n); if (error) return error; - name = offsets; + key = offsets; continue; } @@ -1020,12 +1020,12 @@ static int aggr_parse(struct gpio_aggregator *aggr) } for_each_set_bit(i, bitmap, AGGREGATOR_MAX_GPIOS) { - error = aggr_add_gpio(aggr, name, i, &n); + error = aggr_add_gpio(aggr, key, i, &n); if (error) return error; } - args = next_arg(args, &name, &p); + args = next_arg(args, &key, &p); } if (!n) { From patchwork Mon Feb 17 14:35:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Koichiro Den X-Patchwork-Id: 865995 Received: from smtp-relay-internal-1.canonical.com (smtp-relay-internal-1.canonical.com [185.125.188.123]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86CF2224882 for ; Mon, 17 Feb 2025 14:36:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.125.188.123 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802990; cv=none; b=FUWOjrOnxk52r4+nVLyarPdeMV38Hv1nUR5Dqc+lU15TAbn7BlHXzrybO+Cfhs1oAp7Z5+G/q8R+PPiYcXsjdDuK3qRiWiX/HmoPblr5sVvFCBQeXb1ee8iNWMOt3cTnq5bdURohROjedx/20+Humj0a/kIIRIQzFoc6G0iCMqA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739802990; c=relaxed/simple; bh=5z75klJulPBK3euVFo5jCw9QXY35/qZ4NCunDCR/9Jo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UsgHUzFniYcov5qYWgjGdjQHaGMFjRBIAKA26KWU56/Zekk/XeoJCIUxWsRm9aQ1N2C722jM3EuUTYaExDFAVRnkkmOEEZks3ifrWiQl9QJvMoew6wbPgnTCiTF9Hmg4qe1PJLieBiwpu3qzLOX2uk1wy/mLCgtbpHs46P23m+s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com; spf=pass smtp.mailfrom=canonical.com; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b=OWnYaXgV; arc=none smtp.client-ip=185.125.188.123 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=canonical.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=canonical.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=canonical.com header.i=@canonical.com header.b="OWnYaXgV" Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by smtp-relay-internal-1.canonical.com (Postfix) with ESMTPS id 87EBC3F85E for ; Mon, 17 Feb 2025 14:36:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1739802986; bh=VbN0B6Oenuw6+py3+N6wdefqtqSm9CuUWAW6b8DJ97M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OWnYaXgVeWzJsUGpq8OiHdHL99+5lGb5APfiR1EcOtj4oe/ZO/5T2nOlT28eBjTsO cYOTsflXHVDWZITJbAg43+PbYRy32EhDV080skuMFVJUvyJ5saa4QLsQjkiwfVSXWi 9W1x2ai1fW0KZE1bmBzW7OZ4CFLQ13vxkoqLVy2HRrhJ3Ze94t2hWMWUr8sQb0gcU1 GAiWRqb3zlyvsNkFiNisA1er8Es7RgcNjdVF4Fls6IAH7hy++fhMtpUko1MLd+c/MK LtYS/qLgeKcTGFlKcJTtK54dmn7lBImUJXz2N0926pmOS7X5zC/43nxbPfFcnJkrcN giUZ3sLadqO+Q== Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-2fc1c7c8396so7767816a91.0 for ; Mon, 17 Feb 2025 06:36:26 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739802983; x=1740407783; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VbN0B6Oenuw6+py3+N6wdefqtqSm9CuUWAW6b8DJ97M=; b=DeTHkh4Gmro6LHUU+o/Q0AFaEcIM626uwvN44vZOTD4XVPygG8o2dMcgF0P9BDZrnK qMLO6N9vFcEqNpVvwOzbclqN/ZGl32p+3UuMygPpgc3xwU0t6PX+7LZ1cylRugejhLYA eZ04He3plf7f94AleMOQ/t8T+uVqOqxt1br7UZPhTc2L5a4nCRbG42EacRByOpBBgyLO nicGZ4hKfUjaa6V4EqHGUf8K3y4t8nRyENd45zojs58iQrA0vHRel7rGPnSERvvy2O17 Tqr+CpxYrr3oshjadb0lfw5UnESH7GlLt6B8unKv/f0Fw1d7e8B/4qJyMFqmTYBw43BJ P9xQ== X-Gm-Message-State: AOJu0YwsJWa+enZV6sxUMfpTI6AcssN+foGaQOoBpiFw6otKOp8IXaVL avezuXrKy1GXXiPnEKNocZ168kzEwvUkX2feLL1yIo0vbeGMZ8uG3NeHVzridR1pfqlvSBlHpGp NwkT7zRiBJoNM3ppuTDPfkvFY6H3vxsFY4aemKb09r4NmnhCQUVBBplv9TlUDHiiM7ADh6iUIF4 a3bLqe4AY= X-Gm-Gg: ASbGncsIN1UAACp4hwJ5Px07SlXKjpM/ucH73sE3iaHCSzR6euICDRN3Is2ajbIWSYp 2R7f1c/+5KcDD0jbwm/DVFM5XRIUOCt0Nb+9yrxAbuH2Tveo6x2hXsGmgWMX5WQLh/WgvRpmT3+ rwtsG2/zqiGljqJmqd0QyX9Y/+k5VTwdhIJHds4QSpoNYHXW3wCa+hGfsqO15SXC7XQf+KIQUla JMMqak7GX/C35+wV9UeByAuugXmHjU2FkKlfZOftVY1b6PZ8nEEf5D5jih6G6Wi5d8++KFTTvzV 6Df1qIABbW5bgOVsYLfLmmw= X-Received: by 2002:a17:90b:1651:b0:2ea:a9ac:eee1 with SMTP id 98e67ed59e1d1-2fc40f10402mr15930227a91.10.1739802983294; Mon, 17 Feb 2025 06:36:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IGn8ODnVz5U27ivcZIXnDcIe3tj3Acy3nD1kW2qRgArMBY75CRJbzFbMz5F3/bth1Q+uY2Mow== X-Received: by 2002:a17:90b:1651:b0:2ea:a9ac:eee1 with SMTP id 98e67ed59e1d1-2fc40f10402mr15930181a91.10.1739802982871; Mon, 17 Feb 2025 06:36:22 -0800 (PST) Received: from localhost.localdomain ([240f:74:7be:1:a6da:1fd8:6ba3:4cf3]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2fbf98b3305sm10057757a91.6.2025.02.17.06.36.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Feb 2025 06:36:22 -0800 (PST) From: Koichiro Den To: linux-gpio@vger.kernel.org Cc: brgl@bgdev.pl, geert+renesas@glider.be, linus.walleij@linaro.org, maciej.borzecki@canonical.com, linux-kernel@vger.kernel.org Subject: [PATCH v4 9/9] Documentation: gpio: document configfs interface for gpio-aggregator Date: Mon, 17 Feb 2025 23:35:31 +0900 Message-ID: <20250217143531.541185-10-koichiro.den@canonical.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250217143531.541185-1-koichiro.den@canonical.com> References: <20250217143531.541185-1-koichiro.den@canonical.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add documentation for the newly added configfs-based interface for GPIO aggregator. Signed-off-by: Koichiro Den --- .../admin-guide/gpio/gpio-aggregator.rst | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) diff --git a/Documentation/admin-guide/gpio/gpio-aggregator.rst b/Documentation/admin-guide/gpio/gpio-aggregator.rst index 5cd1e7221756..8374a9df9105 100644 --- a/Documentation/admin-guide/gpio/gpio-aggregator.rst +++ b/Documentation/admin-guide/gpio/gpio-aggregator.rst @@ -69,6 +69,113 @@ write-only attribute files in sysfs. $ echo gpio-aggregator.0 > delete_device +Aggregating GPIOs using Configfs +-------------------------------- + +**Group:** ``/config/gpio-aggregator`` + + This is the root directory of the gpio-aggregator configfs tree. + +**Group:** ``/config/gpio-aggregator/`` + + This directory represents a GPIO aggregator device. You can assign any + name to ```` (e.g. ``agg0``), except names starting with + ``_sysfs`` prefix, which are reserved for auto-generated configfs + entries corresponding to devices created via Sysfs. + +**Attribute:** ``/config/gpio-aggregator//live`` + + The ``live`` attribute allows to trigger the actual creation of the device + once it's fully configured. Accepted values are: + + * ``1``, ``yes``, ``true`` : enable the virtual device + * ``0``, ``no``, ``false`` : disable the virtual device + +**Attribute:** ``/config/gpio-aggregator//dev_name`` + + The read-only ``dev_name`` attribute exposes the name of the device as it + will appear in the system on the platform bus (e.g. ``gpio-aggregator.0``). + This is useful for identifying a character device for the newly created + aggregator. If it's ``gpio-aggregator.0``, + ``/sys/devices/platform/gpio-aggregator.0/gpiochipX`` path tells you that the + GPIO device id is ``X``. + +You must create subdirectories for each virtual line you want to +instantiate, named exactly as ``line0``, ``line1``, ..., ``lineY``, when +you want to instantiate ``Y+1`` (Y >= 0) lines. Configure all lines before +activating the device by setting ``live`` to 1. + +**Group:** ``/config/gpio-aggregator///`` + + This directory represents a GPIO line to include in the aggregator. + +**Attribute:** ``/config/gpio-aggregator///key`` + +**Attribute:** ``/config/gpio-aggregator///offset`` + + The default values after creating the ```` directory are: + + * ``key`` : + * ``offset`` : -1 + + ``key`` must always be explicitly configured, while ``offset`` depends. + Two configuration patterns exist for each ````: + + (a). For lookup by GPIO line name: + + * Set ``key`` to the line name. + * Ensure ``offset`` remains -1 (the default). + + (b). For lookup by GPIO chip name and the line offset within the chip: + + * Set ``key`` to the chip name. + * Set ``offset`` to the line offset (0 <= ``offset`` < 65535). + +**Attribute:** ``/config/gpio-aggregator///name`` + + The ``name`` attribute sets a custom name for lineY. If left unset, the + line will remain unnamed. + +Once the configuration is done, the ``'live'`` attribute must be set to 1 +in order to instantiate the aggregator device. It can be set back to 0 to +destroy the virtual device. The module will synchronously wait for the new +aggregator device to be successfully probed and if this doesn't happen, writing +to ``'live'`` will result in an error. This is a different behaviour from the +case when you create it using sysfs ``new_device`` interface. + +.. note:: + + For aggregators created via Sysfs, the configfs entries are + auto-generated and appear as ``/config/gpio-aggregator/_sysfs./``. You + cannot add or remove line directories with mkdir(2)/rmdir(2). To modify + lines, you must use the "delete_device" interface to tear down the + existing device and reconfigure it from scratch. However, you can still + toggle the aggregator with the ``live`` attribute and adjust the + ``key``, ``offset``, and ``name`` attributes for each line when ``live`` + is set to 0 by hand (i.e. it's not waiting for deferred probe). + +Sample configuration commands +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: sh + + # Create a directory for an aggregator device + $ mkdir /sys/kernel/config/gpio-aggregator/agg0 + + # Configure each line + $ mkdir /sys/kernel/config/gpio-aggregator/agg0/line0 + $ echo gpiochip0 > /sys/kernel/config/gpio-aggregator/agg0/line0/key + $ echo 6 > /sys/kernel/config/gpio-aggregator/agg0/line0/offset + $ echo test0 > /sys/kernel/config/gpio-aggregator/agg0/line0/name + $ mkdir /sys/kernel/config/gpio-aggregator/agg0/line1 + $ echo gpiochip0 > /sys/kernel/config/gpio-aggregator/agg0/line1/key + $ echo 7 > /sys/kernel/config/gpio-aggregator/agg0/line1/offset + $ echo test1 > /sys/kernel/config/gpio-aggregator/agg0/line1/name + + # Activate the aggregator device + $ echo 1 > /sys/kernel/config/gpio-aggregator/agg0/live + + Generic GPIO Driver -------------------