From patchwork Thu Mar 12 07:44:13 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 5991771 Return-Path: X-Original-To: patchwork-linux-input@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 326AF9F2A9 for ; Thu, 12 Mar 2015 07:52:06 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 891612034F for ; Thu, 12 Mar 2015 07:52:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9D410200CF for ; Thu, 12 Mar 2015 07:52:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754078AbbCLHvg (ORCPT ); Thu, 12 Mar 2015 03:51:36 -0400 Received: from mailout2.w1.samsung.com ([210.118.77.12]:64795 "EHLO mailout2.w1.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752806AbbCLHpW (ORCPT ); Thu, 12 Mar 2015 03:45:22 -0400 Received: from eucpsbgm2.samsung.com (unknown [203.254.199.245]) by mailout2.w1.samsung.com (Oracle Communications Messaging Server 7u4-24.01(7.0.4.24.0) 64bit (built Nov 17 2011)) with ESMTP id <0NL300FE19PQXF50@mailout2.w1.samsung.com>; Thu, 12 Mar 2015 07:49:02 +0000 (GMT) X-AuditID: cbfec7f5-b7fc86d0000066b7-e8-55014367da58 Received: from eusync2.samsung.com ( [203.254.199.212]) by eucpsbgm2.samsung.com (EUCPMTA) with SMTP id 09.2C.26295.76341055; Thu, 12 Mar 2015 07:42:31 +0000 (GMT) Received: from AMDC1943.digital.local ([106.116.151.171]) by eusync2.samsung.com (Oracle Communications Messaging Server 7u4-23.01(7.0.4.23.0) 64bit (built Aug 10 2011)) with ESMTPA id <0NL300KWS9IOZC60@eusync2.samsung.com>; Thu, 12 Mar 2015 07:45:01 +0000 (GMT) From: Krzysztof Kozlowski To: Jiri Kosina , Henrik Rydberg , linux-input@vger.kernel.org, linux-kernel@vger.kernel.org, Sebastian Reichel , Dmitry Eremin-Solenikov , David Woodhouse , linux-pm@vger.kernel.org Cc: Krzysztof Kozlowski Subject: [RESEND PATCH v6 15/22] power_supply: Increment power supply use counter when obtaining references Date: Thu, 12 Mar 2015 08:44:13 +0100 Message-id: <1426146260-30081-16-git-send-email-k.kozlowski@samsung.com> X-Mailer: git-send-email 1.9.1 In-reply-to: <1426146260-30081-1-git-send-email-k.kozlowski@samsung.com> References: <1426146260-30081-1-git-send-email-k.kozlowski@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFmphluLIzCtJLcpLzFFi42I5/e/4Fd10Z8ZQg993VCwmPXnPbDFx5WRm i91zFrNYvH5haHHz0zdWi8u75rBZfO49wmhx4ts0JovTu0scOD0+r3rF6rFz1l12j80rtDw2 repk8+jbsorR48yCI+wenzfJBbBHcdmkpOZklqUW6dslcGU0LetnLNgqWLHw/y3WBsbzfF2M nBwSAiYSy99MZIGwxSQu3FvPBmILCSxllJg3XaiLkQvI7mOS2HatjxEkwSZgLLF5+RI2kISI wHwmif5VJ1hBEswChhI/3/1h72Lk4BAWyJXYfC4cJMwioCqxdU87E0iYV8BDYseyMohdchIn j01mBQlzAoVfL+KBWOsuMXPZJ/YJjLwLGBlWMYqmliYXFCel5xrpFSfmFpfmpesl5+duYoSE 3NcdjEuPWR1iFOBgVOLhjehjCBViTSwrrsw9xCjBwawkwsthxRgqxJuSWFmVWpQfX1Sak1p8 iJGJg1OqgVG53Yl7Z8VfQ7O249d2Fh5875UfY3lhV8rXap7eMBOZy4ud97qzKNvmNPba73MM +SdWurrre7MFV4T0n7orZtmu3GuT1pyQjypu9Mr/8ln+tkbUxNmL576ZNlNX+vS3R+031I7u /F0g4r3pbUVquLChueFldjFFWVPpN+fZFgSfm/MtdtLXs0osxRmJhlrMRcWJAC5DXtYXAgAA Sender: linux-input-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-input@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Increment the power_supply.use_cnt usage counter on: - power_supply_get_by_phandle() - power_supply_get_by_name() and decrement it on power_supply_put() call. This helps tracking of valid usage of power supply instance by consumers. The usage counter itself also allows safe calling of power_supply_get_property-like functions even when driver unregisters this power supply. Signed-off-by: Krzysztof Kozlowski Reviewed-by: Bartlomiej Zolnierkiewicz --- drivers/power/power_supply_core.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c index b4ec14683b7f..2ed4a4a6b3c5 100644 --- a/drivers/power/power_supply_core.c +++ b/drivers/power/power_supply_core.c @@ -349,10 +349,16 @@ static int power_supply_match_device_by_name(struct device *dev, const void *dat */ struct power_supply *power_supply_get_by_name(const char *name) { + struct power_supply *psy = NULL; struct device *dev = class_find_device(power_supply_class, NULL, name, power_supply_match_device_by_name); - return dev ? dev_get_drvdata(dev) : NULL; + if (dev) { + psy = dev_get_drvdata(dev); + atomic_inc(&psy->use_cnt); + } + + return psy; } EXPORT_SYMBOL_GPL(power_supply_get_by_name); @@ -367,6 +373,7 @@ void power_supply_put(struct power_supply *psy) { might_sleep(); + atomic_dec(&psy->use_cnt); put_device(&psy->dev); } EXPORT_SYMBOL_GPL(power_supply_put); @@ -393,6 +400,7 @@ struct power_supply *power_supply_get_by_phandle(struct device_node *np, const char *property) { struct device_node *power_supply_np; + struct power_supply *psy = NULL; struct device *dev; power_supply_np = of_parse_phandle(np, property, 0); @@ -404,7 +412,12 @@ struct power_supply *power_supply_get_by_phandle(struct device_node *np, of_node_put(power_supply_np); - return dev ? dev_get_drvdata(dev) : NULL; + if (dev) { + psy = dev_get_drvdata(dev); + atomic_inc(&psy->use_cnt); + } + + return psy; } EXPORT_SYMBOL_GPL(power_supply_get_by_phandle); #endif /* CONFIG_OF */