From nobody Wed Dec 17 19:22:44 2025 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 EB7182222C3 for ; Mon, 7 Apr 2025 04:30:56 +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=1744000258; cv=none; b=M0aya/2cB1PjgzghV+3+vtyWgfGscVu3TFb9ImiXBLGMj6nb/xMQyAH+E295xs2Sm/VS3DH0akh7j8sTjLhCFclLTOk95Wtnpd9QRpXhcO7p8Knag/x4xxaoRjnYQSbqUM8hqVkk7NWBbtiNz4KH6qa2O1pzj3NLRBzuagEEXNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744000258; c=relaxed/simple; bh=TbAKJXmX6g7xhJYJocuFyn/R1DEc55FoQ9fLzC8oylM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=shVFdYAgZlNt0xOvreeAATRC7MeaNjtWnjbcB4QIslo2QZJrCO8/67RkS/flqvYn1oxqgfoZmkB15kz0+46R3ZhmDcAebfvrt8yZ65dRbZFjNpCVLvMie4nrYphBpKDxgOcUd6i5yKMKE2jA5jtN4cTKhqREFrxRQ4RMpZxmM7U= 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=PKBQcJdw; 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="PKBQcJdw" Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (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 AED9A3F181 for ; Mon, 7 Apr 2025 04:30:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=canonical.com; s=20210705; t=1744000254; bh=olpAErLRuaUJTw8reFG3lTyrFxmkOqkGE5UK7pclqas=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PKBQcJdw0tkRa5gzKWAzX19sW7ScmuyC21FnmjfBE47Bv1WGZTw0Z0Q5XUR1ePGP6 7EUL0gYNwINUo95RqKjdpumrczRLl8AEj1/DIVTRwZql3UBW2dUe22KxBopn56MqyH BsmcaJKwp24kPB2Ig48h63X40Hgh2TlDrcTdswScJcPdNa+1sx/f18FfFDiQ8AOueA 4H2mCfuvhXqsOItP8T7XzrncKoPxR+CJG0dnAtVdT2til4wSzCC4cgfI/6uB0VRb66 16DeWTmQnD2cNTqktFMi2W1HAASV4i+34ZyIg68f4mfqYwa9jt+5FRVO4T70BT7EaM OzEswivOCf42w== Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-22651aca434so37647665ad.1 for ; Sun, 06 Apr 2025 21:30:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744000253; x=1744605053; 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=olpAErLRuaUJTw8reFG3lTyrFxmkOqkGE5UK7pclqas=; b=ArQFeTeQMngOdciUb+b5+J07b9V91o0IJMX/p5PxAlss0zxrLIqukVPbwy8Okgil4E gNz7Tn15mi7xMRKxg8C6WkOuLlEPbMcAP5xcAhtpLeH4l7D5MKG9gGzMMS5OfHjOV5+Z ixWqGlKgi6bIOeVJcelI42JxhbRCqbtIV9eQP7TgchKF6gDe20nL/7WoBx8PXdK/8sce SpPbuScLFVocjl/cEIt62kAHt+LXLRpWXOPk9aJE+aIx7z0M+Ht+1ZLaiqZOxy/ONZ2o rSwCQAHGN+MCpJ80Y3H8IdmxK4GvaXkWzUUKWRmuJ5aBbIjGOYMfjSgycCytlvlseG2y Bq9g== X-Forwarded-Encrypted: i=1; AJvYcCUGJIYXSvDbmdD/b/fskebIOLT9b3u3mN8KFKnyZrNNdU84DvC5/H7ZWr9ru6Z0dLi+EzMSQ1aw0Y5ECEg=@vger.kernel.org X-Gm-Message-State: AOJu0Yyv6wOBFSoggbQgTrl8l4OQ1O6VgaaqVqd6QY+oVroYr3kOAB18 3xzhzXTAFb031/Lw2A7y7qhmQxypLpqcS6gByXV01pBTrxzF+HJspN+KeVXbb6fhADL8guCxkZ+ yWS8Pd+mdT+IqZfppoxdYySM5VEPaQ3h8NbIIRlWNkucQh9WWnfFjjMxoRS5dIFgdQnIU+MNBKB xONw== X-Gm-Gg: ASbGncsVoSeACRKMszbVDWJYS2jVxsX9X4qiWbjmT95qo+1i4Anu0Z0BDaFvvrEQFpQ llRusONTnRKVhs2Eqn4cA9xcq1sdXpd7ZFOlGrCI78Ko/vCHMm7Y2sbaL2k73waGvrtQjoTIZJx U+InDCKa1QkMIJRek9ALDlRYdR61CD4bePcb4T55YbpIEAQfRnZck2Vz5j60f4rVMirWbqqtk1U 3zykXdLvx5TbgtVTaRVDg4JtKF34oBAf615CP7bvyFTPgrazecfF9Vub3wKZpuqUDvuu8b4Y3nc NpO2DlkIYQPAJCJMwr68lrjr/cmheZjoHQ== X-Received: by 2002:a17:903:1b2d:b0:216:7926:8d69 with SMTP id d9443c01a7336-22a8a8dd69emr130201335ad.47.1744000253112; Sun, 06 Apr 2025 21:30:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEeDelhsi3pljdEQEJi4TRSqlIHVRt8P+++1nYrhtJHgIUj2n3n/8C4lyr1PQisWmgPh4rxog== X-Received: by 2002:a17:903:1b2d:b0:216:7926:8d69 with SMTP id d9443c01a7336-22a8a8dd69emr130201035ad.47.1744000252685; Sun, 06 Apr 2025 21:30:52 -0700 (PDT) Received: from localhost.localdomain ([240f:74:7be:1:5985:1f8b:863f:3722]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-22978670dbbsm70839525ad.209.2025.04.06.21.30.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Apr 2025 21:30:52 -0700 (PDT) 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 v7 3/9] gpio: aggregator: add gpio_aggregator_{alloc,free}() Date: Mon, 7 Apr 2025 13:30:13 +0900 Message-ID: <20250407043019.4105613-4-koichiro.den@canonical.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250407043019.4105613-1-koichiro.den@canonical.com> References: <20250407043019.4105613-1-koichiro.den@canonical.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 ff5cd5eaa550..6f933a76b2b9 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[]; }; =20 static DEFINE_MUTEX(gpio_aggregator_lock); /* protects idr */ static DEFINE_IDR(gpio_aggregator_idr); =20 +static int gpio_aggregator_alloc(struct gpio_aggregator **aggr, size_t arg= _size) +{ + int ret; + + struct gpio_aggregator *new __free(kfree) =3D kzalloc( + sizeof(*new) + arg_size, GFP_KERNEL); + if (!new) + return -ENOMEM; + + scoped_guard(mutex, &gpio_aggregator_lock) + ret =3D idr_alloc(&gpio_aggregator_idr, new, 0, 0, GFP_KERNEL); + + if (ret < 0) + return ret; + + new->id =3D ret; + *aggr =3D no_free_ptr(new); + return 0; +} + +static void gpio_aggregator_free(struct gpio_aggregator *aggr) +{ + scoped_guard(mutex, &gpio_aggregator_lock) + idr_remove(&gpio_aggregator_idr, aggr->id); + + kfree(aggr); +} + static int gpio_aggregator_add_gpio(struct gpio_aggregator *aggr, const char *key, int hwnum, unsigned int *n) { @@ -454,17 +483,15 @@ static ssize_t gpio_aggregator_new_device_store(struc= t device_driver *driver, { struct gpio_aggregator *aggr; struct platform_device *pdev; - int res, id; + int res; =20 if (!try_module_get(THIS_MODULE)) return -ENOENT; =20 /* kernfs guarantees string termination, so count + 1 is safe */ - aggr =3D kzalloc(sizeof(*aggr) + count + 1, GFP_KERNEL); - if (!aggr) { - res =3D -ENOMEM; + res =3D gpio_aggregator_alloc(&aggr, count + 1); + if (res) goto put_module; - } =20 memcpy(aggr->args, buf, count + 1); =20 @@ -475,19 +502,10 @@ static ssize_t gpio_aggregator_new_device_store(struc= t device_driver *driver, goto free_ga; } =20 - mutex_lock(&gpio_aggregator_lock); - id =3D idr_alloc(&gpio_aggregator_idr, aggr, 0, 0, GFP_KERNEL); - mutex_unlock(&gpio_aggregator_lock); - - if (id < 0) { - res =3D id; - goto free_table; - } - - aggr->lookups->dev_id =3D kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, id); + aggr->lookups->dev_id =3D kasprintf(GFP_KERNEL, "%s.%d", DRV_NAME, aggr->= id); if (!aggr->lookups->dev_id) { res =3D -ENOMEM; - goto remove_idr; + goto free_table; } =20 res =3D gpio_aggregator_parse(aggr); @@ -496,7 +514,7 @@ static ssize_t gpio_aggregator_new_device_store(struct = device_driver *driver, =20 gpiod_add_lookup_table(aggr->lookups); =20 - pdev =3D platform_device_register_simple(DRV_NAME, id, NULL, 0); + pdev =3D platform_device_register_simple(DRV_NAME, aggr->id, NULL, 0); if (IS_ERR(pdev)) { res =3D PTR_ERR(pdev); goto remove_table; @@ -510,14 +528,10 @@ static ssize_t gpio_aggregator_new_device_store(struc= t device_driver *driver, 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); + gpio_aggregator_free(aggr); put_module: module_put(THIS_MODULE); return res; --=20 2.45.2