Received: by 2002:ab2:6309:0:b0:1fb:d597:ff75 with SMTP id s9csp449229lqt; Thu, 6 Jun 2024 08:14:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUulmirtcd8GV7IvvWGTW1Vlw10Xg4EPB7vuJhJKZ+YhA3RsbmoDzVtjPriyirl272SmUVerOqZ+lnvDyvy5rJypNp/t1FuhZ89yX+V7g== X-Google-Smtp-Source: AGHT+IEND04Z7dzG9Re6N3wy366mW2fmvZYjickZvMsOh88movfL7rpOf//SjVPd+2HC2+8ldZ/L X-Received: by 2002:a17:906:ecef:b0:a5c:dbec:7b5c with SMTP id a640c23a62f3a-a69a023b050mr341483666b.77.1717686853026; Thu, 06 Jun 2024 08:14:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717686853; cv=pass; d=google.com; s=arc-20160816; b=TnJMo9PEEljCASmiBpngbIbg36wej9hTLUO9xsN92Ir7DCYsbDPgcMN0b6PRFWAVcW bUz+pJh1Ft0dEqfc0l30Mjy894iq9UQ1gSjoqo7Jfy6kgFF2+UEOtU5o35TVgkNHUrgq pQkEhX9hyPQr9B7dNROhXeqNpY7BBpj/CIp3GOdRp1KNvNJXwvJT/7mcJY4kTm4ptfi1 /PyUizVYMqg6njoL6WInfjHVTmctFlr2pj/SUFSXERGO0aaLS7fXmQX1NlGAaY7SVYu4 09OZdbkPP1iHX1/fUT13ZPwMj8/AqPSIgupTD6aOtg5yNlnrcflPt1BQKJbi3rtQiERf 5BSQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=wJJGj+qXMs0JZ3qxk+d8HgpSH1Pc2ilFrjBQQHIt/j0=; fh=yA3vuFiZmJfoItPaS2BpWDAzGgfs+KcnNwQwMnTjy1k=; b=DIyKrxyttJl7qvq1F5P7Yop/CSoo0Tbg57ShQ6K1/duG01Y9gWV7I5/A87HXx703dF nhVXZPoSb67u6VAD2tA+aLMa8CB51JtvsGgpvJaQsSPMwArlUYd53kMWw7CrqlD0hKiG fIO++jJOVxu/1bnuZ+NkZXSkmEw/alMJ3RD9gdMGIq8hME/vVVdXopYzFCTpP/XUtPeE Ut7JLszyVGdgCE2SHgbxdE5QaR4W9O2sFKB5he2rQuLVmVXiiLpVK09TIf8Oqu7Q1l9J C1MliCdZiUwkueCikU2IbinMbSyJj2RPb3bvFN4YAw9y4qojNfO7ENqfpqnHA24yO2vH cCyA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=AnIErl2o; arc=pass (i=1 spf=pass spfdomain=weissschuh.net dkim=pass dkdomain=weissschuh.net dmarc=pass fromdomain=weissschuh.net); spf=pass (google.com: domain of linux-kernel+bounces-204532-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204532-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=weissschuh.net Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a6c8070c0a5si75937666b.605.2024.06.06.08.14.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jun 2024 08:14:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-204532-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@weissschuh.net header.s=mail header.b=AnIErl2o; arc=pass (i=1 spf=pass spfdomain=weissschuh.net dkim=pass dkdomain=weissschuh.net dmarc=pass fromdomain=weissschuh.net); spf=pass (google.com: domain of linux-kernel+bounces-204532-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-204532-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=weissschuh.net Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 685351F21DB7 for ; Thu, 6 Jun 2024 15:14:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 99ED01A2548; Thu, 6 Jun 2024 14:51:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="AnIErl2o" Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (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 3E7441A254D; Thu, 6 Jun 2024 14:51:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717685470; cv=none; b=Gvl3e6N6YsTltKo5PicOp25S7hmt8Ebf3SfpypG2c27IUGmEKkSiT4rAL+VH3OTrrl+VI9uysYu6pdw6CFVdST9JZ1ncnl0K8Kj+IKsBi+p9CDGcs00+jb3/3r8LRpxfFPjAAimngFVU5sRJeXNLEShitsUBIpyIACnEUrpHmoQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717685470; c=relaxed/simple; bh=vwWqc2ww6qRY4a64w7AUc9FhU7/PBgP1tS+sCDjXi0Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=O0I0liuJTuwk/CTr3zYm+F1tbC4RymFw9nfMlen7kGoN4p1TOJ74KeQlwRAbzyVFI4QRjFWNbJrjdorzyf3E2FtCIor+LinxgUYULfQLZ9PViYsa67qRVold/YB6ZKO+jT25FOQRZ47oeZrD5GklvBURcnMTGZHo0ZmtH6svXG0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=AnIErl2o; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1717685463; bh=vwWqc2ww6qRY4a64w7AUc9FhU7/PBgP1tS+sCDjXi0Y=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=AnIErl2oMBeAR4e77l1N7JLEO5uFcj11YtqpwtJckWzBh3/1YNPKA0GUr86xkyInv tJsUoJUT31Kf6hORuqCmBxAdC45Ez2OsJhCLZkcjCQsgqUkKv51FQKz1vUPxG4/3rR ypNHbVUsuNALruun0GQ74N1tdn7kefp8xc6WHuDs= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Thu, 06 Jun 2024 16:50:56 +0200 Subject: [PATCH RFC 2/6] power: supply: core: avoid iterating properties directly Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Message-Id: <20240606-power-supply-extensions-v1-2-b45669290bdc@weissschuh.net> References: <20240606-power-supply-extensions-v1-0-b45669290bdc@weissschuh.net> In-Reply-To: <20240606-power-supply-extensions-v1-0-b45669290bdc@weissschuh.net> To: Sebastian Reichel , Jeremy Soller , System76 Product Development , Hans de Goede , =?utf-8?q?Ilpo_J=C3=A4rvinen?= Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, platform-driver-x86@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.13.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1717685463; l=6729; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=vwWqc2ww6qRY4a64w7AUc9FhU7/PBgP1tS+sCDjXi0Y=; b=AKhHlOHxhchn70B7VvsUdf3SzA2iNKVbOy/YkVzmZnUxt1KaprueymAKEJUGrwPm4uH7ZpX48 jwcsQHVH/IBBZAqn+5N7bJwOv8uxhPKiL+W10aAQA7xvNrm6yaei+wf X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= With the introduction of power supply extension, it will not be enough to iterate the properties on the struct power_supply directly. Instead introduce a helper power_supply_has_property() which ill handle properties added by extensions. Signed-off-by: Thomas Weißschuh --- drivers/power/supply/power_supply.h | 3 ++ drivers/power/supply/power_supply_core.c | 10 +++---- drivers/power/supply/power_supply_hwmon.c | 48 +++++++++++++++---------------- drivers/power/supply/power_supply_sysfs.c | 14 +++------ 4 files changed, 36 insertions(+), 39 deletions(-) diff --git a/drivers/power/supply/power_supply.h b/drivers/power/supply/power_supply.h index 3cbafc58bdad..622be1f0a180 100644 --- a/drivers/power/supply/power_supply.h +++ b/drivers/power/supply/power_supply.h @@ -13,6 +13,9 @@ struct device; struct device_type; struct power_supply; +extern bool power_supply_has_property(const struct power_supply_desc *psy_desc, + enum power_supply_property psp); + #ifdef CONFIG_SYSFS extern void power_supply_init_attrs(void); diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c index fefe938c9342..d57ecdd966e0 100644 --- a/drivers/power/supply/power_supply_core.c +++ b/drivers/power/supply/power_supply_core.c @@ -1183,8 +1183,8 @@ bool power_supply_battery_bti_in_range(struct power_supply_battery_info *info, } EXPORT_SYMBOL_GPL(power_supply_battery_bti_in_range); -static bool psy_has_property(const struct power_supply_desc *psy_desc, - enum power_supply_property psp) +bool power_supply_has_property(const struct power_supply_desc *psy_desc, + enum power_supply_property psp) { bool found = false; int i; @@ -1209,7 +1209,7 @@ int power_supply_get_property(struct power_supply *psy, return -ENODEV; } - if (psy_has_property(psy->desc, psp)) + if (power_supply_has_property(psy->desc, psp)) return psy->desc->get_property(psy, psp, val); else if (power_supply_battery_info_has_prop(psy->battery_info, psp)) return power_supply_battery_info_get_prop(psy->battery_info, psp, val); @@ -1308,7 +1308,7 @@ static int psy_register_thermal(struct power_supply *psy) return 0; /* Register battery zone device psy reports temperature */ - if (psy_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) { + if (power_supply_has_property(psy->desc, POWER_SUPPLY_PROP_TEMP)) { /* Prefer our hwmon device and avoid duplicates */ struct thermal_zone_params tzp = { .no_hwmon = IS_ENABLED(CONFIG_POWER_SUPPLY_HWMON) @@ -1361,7 +1361,7 @@ __power_supply_register(struct device *parent, pr_warn("%s: Expected proper parent device for '%s'\n", __func__, desc->name); - if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && + if (power_supply_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) && (!desc->usb_types || !desc->num_usb_types)) return ERR_PTR(-EINVAL); diff --git a/drivers/power/supply/power_supply_hwmon.c b/drivers/power/supply/power_supply_hwmon.c index c97893d4c25e..2ecbe4a74c25 100644 --- a/drivers/power/supply/power_supply_hwmon.c +++ b/drivers/power/supply/power_supply_hwmon.c @@ -8,6 +8,8 @@ #include #include +#include "power_supply.h" + struct power_supply_hwmon { struct power_supply *psy; unsigned long *props; @@ -324,9 +326,26 @@ static const struct hwmon_chip_info power_supply_hwmon_chip_info = { .info = power_supply_hwmon_info, }; +static const enum power_supply_property power_supply_hwmon_props[] = { + POWER_SUPPLY_PROP_CURRENT_AVG, + POWER_SUPPLY_PROP_CURRENT_MAX, + POWER_SUPPLY_PROP_CURRENT_NOW, + POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_TEMP_MAX, + POWER_SUPPLY_PROP_TEMP_MIN, + POWER_SUPPLY_PROP_TEMP_ALERT_MIN, + POWER_SUPPLY_PROP_TEMP_ALERT_MAX, + POWER_SUPPLY_PROP_TEMP_AMBIENT, + POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN, + POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX, + POWER_SUPPLY_PROP_VOLTAGE_AVG, + POWER_SUPPLY_PROP_VOLTAGE_MIN, + POWER_SUPPLY_PROP_VOLTAGE_MAX, + POWER_SUPPLY_PROP_VOLTAGE_NOW, +}; + int power_supply_add_hwmon_sysfs(struct power_supply *psy) { - const struct power_supply_desc *desc = psy->desc; struct power_supply_hwmon *psyhw; struct device *dev = &psy->dev; struct device *hwmon; @@ -352,30 +371,11 @@ int power_supply_add_hwmon_sysfs(struct power_supply *psy) goto error; } - for (i = 0; i < desc->num_properties; i++) { - const enum power_supply_property prop = desc->properties[i]; - - switch (prop) { - case POWER_SUPPLY_PROP_CURRENT_AVG: - case POWER_SUPPLY_PROP_CURRENT_MAX: - case POWER_SUPPLY_PROP_CURRENT_NOW: - case POWER_SUPPLY_PROP_TEMP: - case POWER_SUPPLY_PROP_TEMP_MAX: - case POWER_SUPPLY_PROP_TEMP_MIN: - case POWER_SUPPLY_PROP_TEMP_ALERT_MIN: - case POWER_SUPPLY_PROP_TEMP_ALERT_MAX: - case POWER_SUPPLY_PROP_TEMP_AMBIENT: - case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MIN: - case POWER_SUPPLY_PROP_TEMP_AMBIENT_ALERT_MAX: - case POWER_SUPPLY_PROP_VOLTAGE_AVG: - case POWER_SUPPLY_PROP_VOLTAGE_MIN: - case POWER_SUPPLY_PROP_VOLTAGE_MAX: - case POWER_SUPPLY_PROP_VOLTAGE_NOW: + for (i = 0; i < ARRAY_SIZE(power_supply_hwmon_props); i++) { + const enum power_supply_property prop = power_supply_hwmon_props[i]; + + if (power_supply_has_property(psy->desc, prop)) set_bit(prop, psyhw->props); - break; - default: - break; - } } name = psy->desc->name; diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c index 3e63d165b2f7..abd44ebfe6fe 100644 --- a/drivers/power/supply/power_supply_sysfs.c +++ b/drivers/power/supply/power_supply_sysfs.c @@ -367,7 +367,6 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, struct device *dev = kobj_to_dev(kobj); struct power_supply *psy = dev_get_drvdata(dev); umode_t mode = S_IRUSR | S_IRGRP | S_IROTH; - int i; if (!power_supply_attrs[attrno].prop_name) return 0; @@ -375,15 +374,10 @@ static umode_t power_supply_attr_is_visible(struct kobject *kobj, if (attrno == POWER_SUPPLY_PROP_TYPE) return mode; - for (i = 0; i < psy->desc->num_properties; i++) { - int property = psy->desc->properties[i]; - - if (property == attrno) { - if (power_supply_property_is_writeable(psy, property) > 0) - mode |= S_IWUSR; - - return mode; - } + if (power_supply_has_property(psy->desc, attrno)) { + if (power_supply_property_is_writeable(psy, attrno) > 0) + mode |= S_IWUSR; + return mode; } if (power_supply_battery_info_has_prop(psy->battery_info, attrno)) -- 2.45.2