Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp36769imu; Thu, 20 Dec 2018 16:23:13 -0800 (PST) X-Google-Smtp-Source: ALg8bN4M9yFp96RiUraAC73uk35cBculGdaxeGNzwwphG+dbR3q7YqMmx9RNKLGu90aKemD6Kn2J X-Received: by 2002:a63:554b:: with SMTP id f11mr319164pgm.37.1545351793323; Thu, 20 Dec 2018 16:23:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545351793; cv=none; d=google.com; s=arc-20160816; b=VGdi3+Ri1nJVH0PeYDfStZWeblz2qLd1YL69IA6Wt2O6Iyvad7RQ7uQ9/V3GkCScPe nhRO4kbbQW3B1LP38YS0tVsro9P2+Q6a/2a3F2GoYyBLvTHZhZfe3iRCmNsOh21ZMm0U 43C+psRKseBc3Y0pg/iyzaC+RAnc4cR58Vk87trXIQqHWZOEmqguODK0NsKmo5iLdcra jWresiPC5do4JD24yV6hI9ZVfwfnLlyJxNOxfUMBoYPu6hrlT6wLaiuL+yp1m+9hY2ig GNM3JIs7oJZUHDCM0nwljCKr5o5oOvsl9AQU8tqVgBwbcAKb7mXGN2XaCevEv+X3BU+J rlgw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=SWYGAaBV+lIGk2inVlNOBOzTdmy5vHDBcX/Fnid+RCU=; b=oYfbZ6MCnRrhtFqrnoNBh6kyflfYwB+J5Om7CFno4vXj0E14zHOp+r6gJP8YN6EfmP N08ZYMJczNHLzJHfata21Th1QgWChOVfwK82DiV1fPOeVFdGn5LJoIYT1qZM1FSSs3pf SgRi5RtdcLtwNjQ3rfrlEaEMpF1F3n+qafulQ5qqIXAwd/JmE0e7gWbBrcSh37GJh2yZ us+xt9CeC01SN6VpQQBbLbp7v4XKoO9DpwEixktoZvcL+NeyqhUMsli5YEbl23TebzcF cBmINvWPuLR4oD2Fur/Bz79hPjxTM/Esfot88P7QDX9U+khMUo901TxSrYm6IaKT8EaG uTQQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=f9wikpz2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i13si19746268pgj.199.2018.12.20.16.22.57; Thu, 20 Dec 2018 16:23:13 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=f9wikpz2; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387796AbeLTRUK (ORCPT + 99 others); Thu, 20 Dec 2018 12:20:10 -0500 Received: from mail-ed1-f68.google.com ([209.85.208.68]:35272 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733204AbeLTRUI (ORCPT ); Thu, 20 Dec 2018 12:20:08 -0500 Received: by mail-ed1-f68.google.com with SMTP id x30so2413871edx.2 for ; Thu, 20 Dec 2018 09:20:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SWYGAaBV+lIGk2inVlNOBOzTdmy5vHDBcX/Fnid+RCU=; b=f9wikpz23YhXrLgQgEuca2sl9XC/rtK7H+0ZaBpvabppDZj6MbuoVL+euAQZi2JMDZ Xna99BCOuTtYHyiVVwZcu18s91+A6pVTSGWAQVgeXMmbx4IlzFl08wZWIeJXUsFCof5Y bLqY0n19jtu7rEU0FpvT5c1ZPhSUY+1Nt1nYpM//TDguj+1dEwvj5w1oh2ZhuU0BvJmx +jgxGlBzZ/Y7Tg3XUEpOQU3j1XFQac1RhDVpbumLFTlnJkxmBpvArPTha/rwbVadQkkT uPGI69CLYfTSwSNMrs4fMbB26BzMKSD0TQ/hfhjq46v991u4B7IvGGCRXvnC3YdMf681 MbMQ== 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=SWYGAaBV+lIGk2inVlNOBOzTdmy5vHDBcX/Fnid+RCU=; b=bCp0kRZcdJBoDjkIAMSpy3Vyd0PFaHngOxGP1CxBaDnbQG1d1tiR8DeYyGRSSyWqbb S5KG9IUdfKnj8Co6Ccw9q8Ru8a4+euFVsF57gtQ4tSW7EeFCKcbtmvwcel4h28FfSj/z iJSYZ7yGDa4SOnsP7D0LyLcM5DEppZQJlZ22ij+i7ZezCbYXjUDBDhaR9P+yptDfXx1g gk+cwfLNxEA+QIN0rMfWdnEB3e9QOCo3PbuGJUvfyOsv4wuzkQojp4cZekPPbmNWP7Df EqsufG3SCiKmprCPFpDJJLr/aH9uofTDudbun44srSvPL6lHOU9FPDSlSVhbkIdBySOA 48VA== X-Gm-Message-State: AA+aEWYHEX3Pi5AS3w6dUwWZ4wMHwfElQ38CCsdpAclbYTCUKOwRUuky IU5k09OTw34Z5qO51l2rEF4= X-Received: by 2002:a05:6402:121a:: with SMTP id c26mr17368750edw.104.1545326406355; Thu, 20 Dec 2018 09:20:06 -0800 (PST) Received: from localhost (pD9E51040.dip0.t-ipconnect.de. [217.229.16.64]) by smtp.gmail.com with ESMTPSA id u7-v6sm2302464ejb.65.2018.12.20.09.20.05 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 20 Dec 2018 09:20:05 -0800 (PST) From: Thierry Reding To: Jassi Brar Cc: linux-kernel@vger.kernel.org, Andy Gospodarek , Bjorn Andersson , Caesar Wang , CK Hu , Dong Aisheng , Eric Anholt , Fabien Dessenne , Florian Fainelli , Georgi Djakov , Houlong Wei , HS Liao , Kaihua Zhong , Kevin Wangtao , Lee Jones , Leo Yan , Ley Foon Tan , Ludovic Barre , Neil Armstrong , Nishanth Menon , Oleksij Rempel , Ray Jui , Rob Rice , Scott Branden , Sibi Sankar , Stefan Wahren , Sudeep Holla , Suman Anna , Tony Lindgren , Vikram Prakash , Vladimir Zapolskiy Subject: [PATCH v3 01/19] mailbox: Add device-managed registration functions Date: Thu, 20 Dec 2018 18:19:44 +0100 Message-Id: <20181220172002.9016-2-thierry.reding@gmail.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181220172002.9016-1-thierry.reding@gmail.com> References: <20181220172002.9016-1-thierry.reding@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thierry Reding Add device-managed equivalents of the mbox_controller_register() and mbox_controller_unregister() functions that can be used to have the devres infrastructure automatically unregister mailbox controllers on driver probe failure or driver removal. This can help remove a lot of boiler plate code from drivers. Reviewed-by: Bjorn Andersson Reviewed-by: Sudeep Holla Signed-off-by: Thierry Reding --- Changes in v2: - fix indirection in device-managed pointer (now runtime-tested) drivers/mailbox/mailbox.c | 70 ++++++++++++++++++++++++++++++ include/linux/mailbox_controller.h | 5 +++ 2 files changed, 75 insertions(+) diff --git a/drivers/mailbox/mailbox.c b/drivers/mailbox/mailbox.c index 674b35f402f5..08ce9a1ab53a 100644 --- a/drivers/mailbox/mailbox.c +++ b/drivers/mailbox/mailbox.c @@ -515,3 +515,73 @@ void mbox_controller_unregister(struct mbox_controller *mbox) mutex_unlock(&con_mutex); } EXPORT_SYMBOL_GPL(mbox_controller_unregister); + +static void __devm_mbox_controller_unregister(struct device *dev, void *res) +{ + struct mbox_controller **mbox = res; + + mbox_controller_unregister(*mbox); +} + +static int devm_mbox_controller_match(struct device *dev, void *res, void *data) +{ + struct mbox_controller **mbox = res; + + if (WARN_ON(!mbox || !*mbox)) + return 0; + + return *mbox == data; +} + +/** + * devm_mbox_controller_register() - managed mbox_controller_register() + * @dev: device owning the mailbox controller being registered + * @mbox: mailbox controller being registered + * + * This function adds a device-managed resource that will make sure that the + * mailbox controller, which is registered using mbox_controller_register() + * as part of this function, will be unregistered along with the rest of + * device-managed resources upon driver probe failure or driver removal. + * + * Returns 0 on success or a negative error code on failure. + */ +int devm_mbox_controller_register(struct device *dev, + struct mbox_controller *mbox) +{ + struct mbox_controller **ptr; + int err; + + ptr = devres_alloc(__devm_mbox_controller_unregister, sizeof(*ptr), + GFP_KERNEL); + if (!ptr) + return -ENOMEM; + + err = mbox_controller_register(mbox); + if (err < 0) { + devres_free(ptr); + return err; + } + + devres_add(dev, ptr); + *ptr = mbox; + + return 0; +} +EXPORT_SYMBOL_GPL(devm_mbox_controller_register); + +/** + * devm_mbox_controller_unregister() - managed mbox_controller_unregister() + * @dev: device owning the mailbox controller being unregistered + * @mbox: mailbox controller being unregistered + * + * This function unregisters the mailbox controller and removes the device- + * managed resource that was set up to automatically unregister the mailbox + * controller on driver probe failure or driver removal. It's typically not + * necessary to call this function. + */ +void devm_mbox_controller_unregister(struct device *dev, struct mbox_controller *mbox) +{ + WARN_ON(devres_release(dev, __devm_mbox_controller_unregister, + devm_mbox_controller_match, mbox)); +} +EXPORT_SYMBOL_GPL(devm_mbox_controller_unregister); diff --git a/include/linux/mailbox_controller.h b/include/linux/mailbox_controller.h index 74deadb42d76..9b0b21207345 100644 --- a/include/linux/mailbox_controller.h +++ b/include/linux/mailbox_controller.h @@ -131,4 +131,9 @@ void mbox_controller_unregister(struct mbox_controller *mbox); /* can sleep */ void mbox_chan_received_data(struct mbox_chan *chan, void *data); /* atomic */ void mbox_chan_txdone(struct mbox_chan *chan, int r); /* atomic */ +int devm_mbox_controller_register(struct device *dev, + struct mbox_controller *mbox); +void devm_mbox_controller_unregister(struct device *dev, + struct mbox_controller *mbox); + #endif /* __MAILBOX_CONTROLLER_H */ -- 2.19.1