Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp2466926lqb; Tue, 28 May 2024 00:02:49 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVWFAmyQ17+dkGofWEWdFhJ5fPh0ep7pQbylOpP570gruibL01vWhGq4mFpI+AnxrcvdJP32cf75Wv+I9NA+WNYlCoWv4XiFIR9jtRoDQ== X-Google-Smtp-Source: AGHT+IFTiuEQUJ0Cmfrcrme35oc3yhdY6Qz+aCfWVZrFTyVXuiD2wvvwzYYHadhBKOWGnl7bbRYP X-Received: by 2002:a17:906:dfc5:b0:a59:ccc3:544 with SMTP id a640c23a62f3a-a623e6d584fmr1222189566b.2.1716879769373; Tue, 28 May 2024 00:02:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716879769; cv=pass; d=google.com; s=arc-20160816; b=rOoTg+S5gik+B01r2gZhb0AQlx56guUpq9lte+XyCVHpnl7FFPK/hbmdFXKdbDyw/t 4BVe+SBsbW+H188TbsRWN4gUvbyTlbSku9x4d+0a7JodPnvhPR6FGM0tGSPQC5DHMxKt JgK/t1assvko4yYsZ5ekudTzCqxdpGMT/O4W/YG7akezFvvAVec5WL0GT549p2JN5LrR tImlPHgWd6ZnqRGyTHQzZE9owVhrL/nfZHhKjL5G+y8gml3TbYGUjq/PgYUFb8QTO5g3 pCOR7SmMbxibqxVGhtiH6c7fl41Up9f2XPno/WJMn1ynE+BWgJO929c+cdcvmoXQ2h68 YDeg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:feedback-id:dkim-signature:dkim-signature; bh=1y5ZIKidcSJ1U8GECBO90D6MU52bNNu/qP9jzcpqqYw=; fh=KBOOsaW3YEneVcYsdRdNlUHz1Q41m6vPI9Uc3mM7Mz4=; b=ckMxUTqB+c3TS/jVDK4EOzL/TeC5TI1FAsnEML6FJPHPqeSjZ+Df5g08yK0dWcFUAx OMR1qSdJ5iQXlnHg1z2iPjtff/dxsEBidmDqGx/tuv0n/d5Ue6Qu0YzwvdFq+TeJcJ6q btMLl5Rd1ZK3I4H3hdMxyOYndrt68iSSudfso2kpQYwJ739dp9RA3Zf1Op6tOXwcadS5 Gojo4ZmMBGK0CuWKWvjQ+jGtkZ6buqf4kI9UvKcw9KDUBYncY0vWxUDZIYKXQUO5VjxR uyobcq/wddx1BYz/hlCuPnc6K4zFExBvmgVZVyTS6zROT/UxbhGoEcuIVoshsmLZ0AMH VPhA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ljones.dev header.s=fm2 header.b=BkS7uFZ1; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=WN12QDGw; arc=pass (i=1 dkim=pass dkdomain=ljones.dev dkim=pass dkdomain=messagingengine.com); spf=pass (google.com: domain of linux-kernel+bounces-191615-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-191615-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id 4fb4d7f45d1cf-579b633f20dsi3409351a12.517.2024.05.28.00.02.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 00:02:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-191615-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@ljones.dev header.s=fm2 header.b=BkS7uFZ1; dkim=pass header.i=@messagingengine.com header.s=fm1 header.b=WN12QDGw; arc=pass (i=1 dkim=pass dkdomain=ljones.dev dkim=pass dkdomain=messagingengine.com); spf=pass (google.com: domain of linux-kernel+bounces-191615-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-191615-linux.lists.archive=gmail.com@vger.kernel.org" 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 902BA1F227D3 for ; Tue, 28 May 2024 01:37:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D98D01C68E; Tue, 28 May 2024 01:36:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ljones.dev header.i=@ljones.dev header.b="BkS7uFZ1"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="WN12QDGw" Received: from fout2-smtp.messagingengine.com (fout2-smtp.messagingengine.com [103.168.172.145]) (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 B6BD3EEDB; Tue, 28 May 2024 01:36:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.145 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716860211; cv=none; b=ATgJ7I5XaAQrZtXzgYtANTFRWjuH6mFiiWIK33dxznu9gY0T/7L7z42u2WBln3uJC7tuTs6O58NLs/Nhu7ljQ5jSq3ooQRTBZ2huuJmE6LLlV08bliRJk9sigo2J/171yQ2kE3OlIyvCjxPdo/FuIXC08VG6d0ilgYWkCEJyn9E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716860211; c=relaxed/simple; bh=THpXRT3p/0C4OrepJEXq/5KRR9Y10IEXpmUMdwr0zeM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uMA/+1haE8vw5mGAa5M4LvoMxFvcfuG07OFozWH5Ce9tZMjaTv+ltnUYyLz7n04jLQC6/bQ2aLUx3moknNz25vpH5XM7sbsZVxpEcvRqNjFn8he9oQPUOLYq1wfPTtXzIlyxJiV3Z4dJaeCx2SPN0F3BTgDvKjgjamiWfLmVGUk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ljones.dev; spf=none smtp.mailfrom=ljones.dev; dkim=pass (2048-bit key) header.d=ljones.dev header.i=@ljones.dev header.b=BkS7uFZ1; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=WN12QDGw; arc=none smtp.client-ip=103.168.172.145 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=ljones.dev Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=ljones.dev Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailfout.nyi.internal (Postfix) with ESMTP id BC584138013D; Mon, 27 May 2024 21:36:48 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 27 May 2024 21:36:48 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ljones.dev; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1716860208; x= 1716946608; bh=1y5ZIKidcSJ1U8GECBO90D6MU52bNNu/qP9jzcpqqYw=; b=B kS7uFZ1iUJMkv1500v7ZpHoA2u9Fmw9+A9GDNL4+Nx5Wv31Od7rr5rnz/cdSIWtg kjdPfphYcdvWkPNFbnWVTVOVPfyn01GMJ8Mc9a4iTmmojXRjiMvG4H3WLlYnws0W 1CIjhs5O61wtNRbCskeMpdjYePr30cvDu0yNKXtigWDM2vjO2oNU+KwDYluRqNRJ SO0E4+ydCYkpeaDvBjnXZUMu3/8TuIFu7/REU7LfWoYfMvQwU72pOyDC1z1bk9h0 qVLlXkim6gCFddyVBRgTrul+iEY4Slzo/JybnvfgOBxL5lEEUCam+JfA2ir4c2ox XCyHuMc/lDgYpOHgavndA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1716860208; x= 1716946608; bh=1y5ZIKidcSJ1U8GECBO90D6MU52bNNu/qP9jzcpqqYw=; b=W N12QDGwO6IEtfEHx76CwupHnm5gqyReJxRohyc7e10bfFrczar71LPQlFTWJh8ca lmWTlIyV5JfZ24T8cR4Hkjmq0hBaCXbqDP46/EmYdraiGb4kgJs1oiOd4dFejpUs bN192GCO7Y+hOqQNhnSCOstqqWAUQgoy8pNbD/OfbWvNXcibXvp/j0PbceD7wJcJ M1smHbRTZJW17QUb1Bl2QT195pgGmfLDYKXZw6THKTRubmZjscSr0bpdKS+VQ7XG w53YInotILCH9fmIfufhe8w/wwpsPOYMhBfA3RewtbNJtEk4iO++0wkmq51RXLXI kAaD0A0WBbTwoz+aQWnmg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvdejhedggeekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpefhvfevufffkffojghfggfgsedtke ertdertddtnecuhfhrohhmpedfnfhukhgvucffrdculfhonhgvshdfuceolhhukhgvsehl jhhonhgvshdruggvvheqnecuggftrfgrthhtvghrnhepgfetfedugfetudeuheetjefhue fggfelleetvdevtefhueeujeefvdegleevhefgnecuvehluhhsthgvrhfuihiivgepuden ucfrrghrrghmpehmrghilhhfrhhomheplhhukhgvsehljhhonhgvshdruggvvh X-ME-Proxy: Feedback-ID: i5ec1447f:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 27 May 2024 21:36:45 -0400 (EDT) From: "Luke D. Jones" To: hdegoede@redhat.com Cc: ilpo.jarvinen@linux.intel.com, corentin.chary@gmail.com, platform-driver-x86@vger.kernel.org, linux-kernel@vger.kernel.org, "Luke D. Jones" Subject: [PATCH 4/9] platform/x86: asus-wmi: reduce code duplication with macros Date: Tue, 28 May 2024 13:36:21 +1200 Message-ID: <20240528013626.14066-5-luke@ljones.dev> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240528013626.14066-1-luke@ljones.dev> References: <20240528013626.14066-1-luke@ljones.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Over time many default patterns have emerged while adding functionality. This patch consolidates those patterns in to a few macros to remove a lot of copy/paste, and make it easier to add more of the same style of features in the future. Signed-off-by: Luke D. Jones --- drivers/platform/x86/asus-wmi.c | 215 ++++++-------------------------- 1 file changed, 38 insertions(+), 177 deletions(-) diff --git a/drivers/platform/x86/asus-wmi.c b/drivers/platform/x86/asus-wmi.c index d016acb23789..5c03e28ff252 100644 --- a/drivers/platform/x86/asus-wmi.c +++ b/drivers/platform/x86/asus-wmi.c @@ -676,7 +676,7 @@ static void asus_wmi_input_exit(struct asus_wmi *asus) static ssize_t rog_tunable_store(struct asus_wmi *asus, struct attribute *attr, const char *buf, size_t count, - u32 min, u32 max, u32 defaultv, + u32 min, u32 max, int defaultv, u32 *store_value, u32 wmi_dev) { int result, err, value; @@ -685,7 +685,7 @@ static ssize_t rog_tunable_store(struct asus_wmi *asus, if (result) return result; - if (value == -1 ) + if (value == -1 && defaultv != -1) value = defaultv; if (value < min || value > max) return -EINVAL; @@ -708,6 +708,36 @@ static ssize_t rog_tunable_store(struct asus_wmi *asus, return count; } +#define WMI_SIMPLE_STORE(_fname, _min, _max, _wmi) \ +static ssize_t _fname##_store(struct device *dev, \ + struct device_attribute *attr, const char *buf, size_t count) \ +{ \ + struct asus_wmi *asus = dev_get_drvdata(dev); \ + return rog_tunable_store(asus, &attr->attr, buf, count, \ + _min, _max, -1, NULL, _wmi); \ +} + +#define WMI_SIMPLE_SHOW(_fname, _fmt, _wmi) \ +static ssize_t _fname##_show(struct device *dev, \ + struct device_attribute *attr, char *buf) \ +{ \ + struct asus_wmi *asus = dev_get_drvdata(dev); \ + u32 result; \ + asus_wmi_get_devstate(asus, _wmi, &result); \ + if (result < 0) \ + return result; \ + return sysfs_emit(buf, _fmt, result & ~ASUS_WMI_DSTS_PRESENCE_BIT); \ +} + +#define WMI_ATTR_SIMPLE_RW(_fname, _minv, _maxv, _wmi) \ +WMI_SIMPLE_STORE(_fname, _minv, _maxv, _wmi); \ +WMI_SIMPLE_SHOW(_fname, "%d\n", _wmi); \ +static DEVICE_ATTR_RW(_fname) + +#define WMI_ATTR_SIMPLE_RO(_fname, _wmi) \ +WMI_SIMPLE_SHOW(_fname, "%d\n", _wmi); \ +static DEVICE_ATTR_RO(_fname) + #define ROG_TUNABLE_STORE(_fname, _min, _max, _default, _wmi) \ static ssize_t _fname##_store(struct device *dev, \ struct device_attribute *attr, const char *buf, size_t count) \ @@ -761,6 +791,12 @@ ROG_ATTR_RW(nv_dynamic_boost, NVIDIA_BOOST_MIN, nv_boost_max, nv_boost_default, ASUS_WMI_DEVID_NV_DYN_BOOST); ROG_ATTR_RW(nv_temp_target, NVIDIA_TEMP_MIN, nv_temp_max, nv_temp_default, ASUS_WMI_DEVID_NV_THERM_TARGET); +/* Ally MCU Powersave */ +WMI_ATTR_SIMPLE_RW(mcu_powersave, 0, 1, ASUS_WMI_DEVID_MCU_POWERSAVE); +WMI_ATTR_SIMPLE_RO(egpu_connected, ASUS_WMI_DEVID_EGPU_CONNECTED); +WMI_ATTR_SIMPLE_RW(panel_od, 0, 1, ASUS_WMI_DEVID_PANEL_OD); +WMI_ATTR_SIMPLE_RW(boot_sound, 0, 1, ASUS_WMI_DEVID_BOOT_SOUND); +WMI_ATTR_SIMPLE_RO(charge_mode, ASUS_WMI_DEVID_CHARGE_MODE); /* Tablet mode ****************************************************************/ @@ -776,22 +812,6 @@ static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus) asus_wmi_tablet_sw_report(asus, result); } -/* Charging mode, 1=Barrel, 2=USB ******************************************/ -static ssize_t charge_mode_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct asus_wmi *asus = dev_get_drvdata(dev); - int result, value; - - result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value); - if (result < 0) - return result; - - return sysfs_emit(buf, "%d\n", value & 0xff); -} - -static DEVICE_ATTR_RO(charge_mode); - /* dGPU ********************************************************************/ static ssize_t dgpu_disable_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -925,22 +945,6 @@ static ssize_t egpu_enable_store(struct device *dev, } static DEVICE_ATTR_RW(egpu_enable); -/* Is eGPU connected? *********************************************************/ -static ssize_t egpu_connected_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct asus_wmi *asus = dev_get_drvdata(dev); - int result; - - result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED); - if (result < 0) - return result; - - return sysfs_emit(buf, "%d\n", result); -} - -static DEVICE_ATTR_RO(egpu_connected); - /* gpu mux switch *************************************************************/ static ssize_t gpu_mux_mode_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -1128,53 +1132,6 @@ static const struct attribute_group *kbd_rgb_mode_groups[] = { NULL, }; -/* Ally MCU Powersave ********************************************************/ -static ssize_t mcu_powersave_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct asus_wmi *asus = dev_get_drvdata(dev); - int result; - - result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MCU_POWERSAVE); - if (result < 0) - return result; - - return sysfs_emit(buf, "%d\n", result); -} - -static ssize_t mcu_powersave_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - int result, err; - u32 enable; - - struct asus_wmi *asus = dev_get_drvdata(dev); - - result = kstrtou32(buf, 10, &enable); - if (result) - return result; - - if (enable > 1) - return -EINVAL; - - err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, enable, &result); - if (err) { - pr_warn("Failed to set MCU powersave: %d\n", err); - return err; - } - - if (result > 1) { - pr_warn("Failed to set MCU powersave (result): 0x%x\n", result); - return -EIO; - } - - sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mcu_powersave"); - - return count; -} -static DEVICE_ATTR_RW(mcu_powersave); - /* Battery ********************************************************************/ /* The battery maximum charging percentage */ @@ -2002,102 +1959,6 @@ static int asus_wmi_rfkill_init(struct asus_wmi *asus) return result; } -/* Panel Overdrive ************************************************************/ -static ssize_t panel_od_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct asus_wmi *asus = dev_get_drvdata(dev); - int result; - - result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD); - if (result < 0) - return result; - - return sysfs_emit(buf, "%d\n", result); -} - -static ssize_t panel_od_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - int result, err; - u32 overdrive; - - struct asus_wmi *asus = dev_get_drvdata(dev); - - result = kstrtou32(buf, 10, &overdrive); - if (result) - return result; - - if (overdrive > 1) - return -EINVAL; - - err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result); - - if (err) { - pr_warn("Failed to set panel overdrive: %d\n", err); - return err; - } - - if (result > 1) { - pr_warn("Failed to set panel overdrive (result): 0x%x\n", result); - return -EIO; - } - - sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od"); - - return count; -} -static DEVICE_ATTR_RW(panel_od); - -/* Bootup sound ***************************************************************/ - -static ssize_t boot_sound_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct asus_wmi *asus = dev_get_drvdata(dev); - int result; - - result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_BOOT_SOUND); - if (result < 0) - return result; - - return sysfs_emit(buf, "%d\n", result); -} - -static ssize_t boot_sound_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - int result, err; - u32 snd; - - struct asus_wmi *asus = dev_get_drvdata(dev); - - result = kstrtou32(buf, 10, &snd); - if (result) - return result; - - if (snd > 1) - return -EINVAL; - - err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BOOT_SOUND, snd, &result); - if (err) { - pr_warn("Failed to set boot sound: %d\n", err); - return err; - } - - if (result > 1) { - pr_warn("Failed to set panel boot sound (result): 0x%x\n", result); - return -EIO; - } - - sysfs_notify(&asus->platform_device->dev.kobj, NULL, "boot_sound"); - - return count; -} -static DEVICE_ATTR_RW(boot_sound); - /* Mini-LED mode **************************************************************/ static ssize_t mini_led_mode_show(struct device *dev, struct device_attribute *attr, char *buf) -- 2.45.1