From nobody Sat Feb 7 06:21:08 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 3B75C31AAA5 for ; Fri, 26 Dec 2025 11:10:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766747436; cv=none; b=Kc54OQIp4v++twy+G7TDo2HxWXtN75JXJ0ZtAEcFeNkHqhRYH+icLJpNKu+1hWgzU0aYegehu+OheprNH3NK49NASbWugvnt1XDyk3dG/VV4inTKcMZ3dA1VbkgA7IYn7zN1IF5jFAWvIVtjsS0sEjo8kYycRDyNjpqqtn/WBGY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766747436; c=relaxed/simple; bh=TZBm7xraSyzbd3zPTXLuxxnIVTOSK7rzIf/fTMWuvKM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qKmX+syqb6PFcu1ujD61wtwF6Q96yjkRAs9thl546ZNt/oX57mn4YLR0ZzAkX3uw2KiW9PDgdXy2pFmHIFkQDacbtyu0AYimdKi0jYEp/BeO8HoCXZ/DasxNz9giDZqpgHSLh0utGt6zCaWYGWK+lc3xzu15dM/50GOhTtFN0VM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NrkUFV31; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=sYVczA8o; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NrkUFV31"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="sYVczA8o" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766747432; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hxxe+0EjF9NUofDY+U4LnGwysYHjaMOQnhMUAVNbt4s=; b=NrkUFV31gftMMa2a+qUSZ/FYPNyC2owZPstnHJH8bB2O/QyON/H6GAx9kNo1a8MQBkrtqc pr7ViWj+pltT2ApRz04Zyu6bsAA4nWQyP8xGkUgwuVBjGjcG9EoFUQO5Aa1d3NFRyHNYF3 3M6X1wH72jSO45FomcLBsux8BHSZTIc= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-541-C_vveLTAPE-z6kSS0fTQ3A-1; Fri, 26 Dec 2025 06:10:31 -0500 X-MC-Unique: C_vveLTAPE-z6kSS0fTQ3A-1 X-Mimecast-MFC-AGG-ID: C_vveLTAPE-z6kSS0fTQ3A_1766747430 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-4779b3749a8so50832315e9.1 for ; Fri, 26 Dec 2025 03:10:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766747430; x=1767352230; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hxxe+0EjF9NUofDY+U4LnGwysYHjaMOQnhMUAVNbt4s=; b=sYVczA8o+/n2PwpwgGQn25GhKGkWcEac58F11gPn+wSh5NaTH7wr40RjvrBwixICCW atZ6zz832SBwvAVPBHqkJ6A+lnnyEjCBSoIS1IS52UfUOEVmf8E/5KX/Oop3XfkUWA97 pxhrf51sQKaY+0tc8Lm4b4R3iuUG9eYNIObuubD/RNwBOpa8SCNNUkHRZ7ZseAaPSWn8 cT/IrGRjepsTRxmIeJ2V/SXPD6jXzQxFkFT3d5eRK7z1uHVuvbZpOS61h4tr2hAHaxmb +5FqHZ85mi8GrHsn7SEefab+P6x2mHFb55w0j85+fizu3VTFdnMNbgKastFzcUn6kRGL 5QVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766747430; x=1767352230; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=hxxe+0EjF9NUofDY+U4LnGwysYHjaMOQnhMUAVNbt4s=; b=AfAvp0CNcjX8AnFsh+2jNX7YuYJ+7fepOhGSFnQVBYE+vfZwUW1HZvvPlPBTxZEFOz I1WDHajxz7sSmzOwVGlpPyQMWEaUPEQHnx6436LwSHtOeFiwLrpy3fGqUPEeU559jBs1 ulz5Vr09ZTOsSnZwexCIkyGdp6Q1QJThn6t09/9E6s8KTrj3g4Et+Sx9uv0V6jl9H2Kk JVmC7Ugsla61r0Hlci633HXMQtCtBscdmvPvH1d1skoBI9OF57vuo29ei19YGyuh6O5s 4Zr2vwf1vweyJxI7H93LssvBqoXRQy56xUiSckJS8c251EKl9Djlh9y1GRXWlyr5+Mm2 XsXg== X-Forwarded-Encrypted: i=1; AJvYcCVpN5iPOoQ5sva7t/bBuZIPqYP7YFZ5y4IZF1Z9noRi+Tjwqy7rfxSrgxpopM3deH0pjjVfGDiPrMPZK18=@vger.kernel.org X-Gm-Message-State: AOJu0YwBI7kmO8FAUclY2m/iHi8ud0YyvwLOaMyqOyMLLxFgz+2rO4Ip h2UfosyyjlD4IoYOgrxpckh1UfmRiY8WvymZyC3GptGnrWEchJ4sVr969ZHAlhy28Thz9Qof1ln qCPuODO/t8UPTlYegxkEBZ/B8eroYjLCs20/zMZSNpD+QASsLlytc0+iokkXJZhZx/Q== X-Gm-Gg: AY/fxX7PyAGiCfQHFMCuNoHipfDMwuKtKrnQ2xdA+EvH1K3ah1F1kptXm+Tkjy2N6wM 6pI33StnNKOF5KT9XIkfWS4zF1WNRzYUD6ivRg5lXVTzSWyNJovcbsOhcG2Bu0M/CvpwM6sMDb3 OdhWEPFTAorWyr6Nddenlc/GjVRqFgyswFWDJpFHYDSNmaYDPVxUdbgbqijYOsU0N5jHC8qpCwX sHqjJ+NJOZ5LUxc6Pxlsy3X0VRo1q0AZgTR9ObeN/W1Di20MENju7waZ9JGLNCCzA0R1F4rkzfD FL/BexkrHqHA45oZnOIAV+pgRqILH+eGqWwTi2XrZIHmq8KY7f4PP/GCgM7hweEAoQ+EZ2QX28w yOBVZTq5WU9C2B9hkTuVGWiUsW0gpcX2LQWcSBzbbFRuiG0xo2i9Zw6IBsTCyemx0VinwuxTwzD vPh7O34VJHXg== X-Received: by 2002:a05:600c:310c:b0:46e:32dd:1b1a with SMTP id 5b1f17b1804b1-47d1953b941mr192007375e9.7.1766747426188; Fri, 26 Dec 2025 03:10:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHrAJY07FPV6QeiBb5VL/M5EhPut9Jv/p09K9isCNRbjpx9PlU/IWIEkXo0YNR2MTtFi2aaLQ== X-Received: by 2002:a05:600c:310c:b0:46e:32dd:1b1a with SMTP id 5b1f17b1804b1-47d1953b941mr192007135e9.7.1766747425712; Fri, 26 Dec 2025 03:10:25 -0800 (PST) Received: from sissix.lzampier.com ([2a06:5900:814a:ab00:3725:2991:6cf3:b3aa]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d193d4e91sm375653855e9.13.2025.12.26.03.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Dec 2025 03:10:25 -0800 (PST) From: Lucas Zampieri To: linux-input@vger.kernel.org Cc: Lucas Zampieri , linux-kernel@vger.kernel.org, Jiri Kosina , Benjamin Tissoires , Sebastian Reichel , Bastien Nocera , linux-pm@vger.kernel.org, lcasmz54@gmail.com Subject: [PATCH v6 1/3] HID: input: Convert battery code to devm_* Date: Fri, 26 Dec 2025 11:10:14 +0000 Message-ID: <20251226111019.31243-2-lzampier@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251226111019.31243-1-lzampier@redhat.com> References: <20251226111019.31243-1-lzampier@redhat.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" Convert the HID battery code to use devm_* managed resource APIs for the power_supply_desc allocation, battery name string, and power supply registration. The error path uses devm_kfree() to clean up allocated memory if devm_power_supply_register() fails, preventing memory waste on repeated setup attempts. The hidinput_cleanup_battery() function is removed as devm handles cleanup automatically. Signed-off-by: Lucas Zampieri --- drivers/hid/hid-input.c | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index e56e7de53279..71862beede9e 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -530,13 +530,13 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, if (quirks & HID_BATTERY_QUIRK_IGNORE) return 0; =20 - psy_desc =3D kzalloc(sizeof(*psy_desc), GFP_KERNEL); + psy_desc =3D devm_kzalloc(&dev->dev, sizeof(*psy_desc), GFP_KERNEL); if (!psy_desc) return -ENOMEM; =20 - psy_desc->name =3D kasprintf(GFP_KERNEL, "hid-%s-battery", - strlen(dev->uniq) ? - dev->uniq : dev_name(&dev->dev)); + psy_desc->name =3D devm_kasprintf(&dev->dev, GFP_KERNEL, "hid-%s-battery", + strlen(dev->uniq) ? + dev->uniq : dev_name(&dev->dev)); if (!psy_desc->name) { error =3D -ENOMEM; goto err_free_mem; @@ -576,7 +576,7 @@ static int hidinput_setup_battery(struct hid_device *de= v, unsigned report_type, if (quirks & HID_BATTERY_QUIRK_AVOID_QUERY) dev->battery_avoid_query =3D true; =20 - dev->battery =3D power_supply_register(&dev->dev, psy_desc, &psy_cfg); + dev->battery =3D devm_power_supply_register(&dev->dev, psy_desc, &psy_cfg= ); if (IS_ERR(dev->battery)) { error =3D PTR_ERR(dev->battery); hid_warn(dev, "can't register power supply: %d\n", error); @@ -587,27 +587,13 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, return 0; =20 err_free_name: - kfree(psy_desc->name); + devm_kfree(&dev->dev, psy_desc->name); err_free_mem: - kfree(psy_desc); + devm_kfree(&dev->dev, psy_desc); dev->battery =3D NULL; return error; } =20 -static void hidinput_cleanup_battery(struct hid_device *dev) -{ - const struct power_supply_desc *psy_desc; - - if (!dev->battery) - return; - - psy_desc =3D dev->battery->desc; - power_supply_unregister(dev->battery); - kfree(psy_desc->name); - kfree(psy_desc); - dev->battery =3D NULL; -} - static bool hidinput_update_battery_charge_status(struct hid_device *dev, unsigned int usage, int value) { @@ -660,10 +646,6 @@ static int hidinput_setup_battery(struct hid_device *d= ev, unsigned report_type, return 0; } =20 -static void hidinput_cleanup_battery(struct hid_device *dev) -{ -} - static void hidinput_update_battery(struct hid_device *dev, unsigned int u= sage, int value) { @@ -2379,8 +2361,6 @@ void hidinput_disconnect(struct hid_device *hid) { struct hid_input *hidinput, *next; =20 - hidinput_cleanup_battery(hid); - list_for_each_entry_safe(hidinput, next, &hid->inputs, list) { list_del(&hidinput->list); if (hidinput->registered) --=20 2.52.0 From nobody Sat Feb 7 06:21:08 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 7D1F931A053 for ; Fri, 26 Dec 2025 11:10:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766747433; cv=none; b=U+IbVeDcwnHyVhLapUKGOUWjP2dR+Vx3pgilhrXlJ/0HdgZdkkclBJaxYc8GkfUpTylkiEP9obfnboc5exmWMsmUVdf2cc6ZlV1OncsRxX8ylnvyaRidk7TonEYmu2qtHjjlgmsiYKCIaLbSSkBmT4+qPYwGUBkOn5AYD8h0Hxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766747433; c=relaxed/simple; bh=OPqAPXSNfScp/WjOLLIGKwDhHxQ0u7LBqf+H7bfoa0c=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QdW2xtxOjgBMp5kLiev9UAnW5fMQ5tc7EMbPBRhlaoE0j/uy828z2ToioW8Vnedbf6KSjIKnjrMpmj/5bZ7dKgvAYZ9XjaDuP+nd9eTEnNke5ANo55DlYJnGiKfJlseDQ3MTkUlVl/w7tTQWYNGXxPTA5IQdiv8v2GqJK9zyyd4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=XMcDEObg; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=LzL2/ULo; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="XMcDEObg"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="LzL2/ULo" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766747430; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5lzUQU6gKnw7KrSrIVAgtcMoXzj3A3pQeBpgfzCf+yc=; b=XMcDEObgBrZR7C6uPPEGqdOW0HTMs1VLY4uuY2MDATqQOIfUYfGc8Ffqil8phgAdMbw8Iy 6yT0lrVzk916l3NF7pIkdwggAF4yJaQLTcsxlkpbQrJjGpmMvi6/GPA55CaRfJelSUnISV yaqWa63AGYnDPtU//lkQjzCTqK2AlJQ= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-421-kmXRj8kjMgu3lDwf7jZcnA-1; Fri, 26 Dec 2025 06:10:28 -0500 X-MC-Unique: kmXRj8kjMgu3lDwf7jZcnA-1 X-Mimecast-MFC-AGG-ID: kmXRj8kjMgu3lDwf7jZcnA_1766747427 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-4327778efa9so912984f8f.2 for ; Fri, 26 Dec 2025 03:10:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766747427; x=1767352227; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5lzUQU6gKnw7KrSrIVAgtcMoXzj3A3pQeBpgfzCf+yc=; b=LzL2/ULozcJbW5/i+QD8TPMAnn/haWMoDwI1vISb/Y1gEai0gtFoizgwkm46OMgBVi RUgyDxwj53ZO1u/RPkbougaS39wa1hpizXYxVYVXz0ytkkTvT4X0Ejb+it1pSn7r6XHV IIvxQHR2Y0HlTktydUp1SemvxY4VLMMNlbxGRHXUWGG1qbQf2TmPY3n5vSQ2iQo8aA9e ObFlOQgRJN6NxHcd12qnr1eE6mDFYp2pteXx9O3CfVosCvop9iNKwEAb2vmRLiFCYCUK Sx7z14NXeW4NLmhEZ8yZCp9/cwaKZBRYtgWJ5RpaE/Eqby8uLnMaHim2uPxQc4xG/U1M RU0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766747427; x=1767352227; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5lzUQU6gKnw7KrSrIVAgtcMoXzj3A3pQeBpgfzCf+yc=; b=hGrDUrk2VM25PeZ1eshoKkjH9h1PuvDj+n86OZcn2aRd7u42etHjDUMqJajRpmG6xY RrvUAa90okJdoU3hL5BlN16kB7maOpI/5e/9iyiqfZvSWbrB2udUOLkzKlGjihsTVdDD loDN+2YlCSAKl/YjwhmdtFMkwj3BZMdWo1CQBrBMpvuxnsrOZCJTeVaIj7AafbYrVF+w 4lk6GwGeVyIvpZEBAijHNo3+VyNXu5nCws4oJevgesOLTnfFCxP6dhm5LoxYHw/mjorD tKOxIsRfRLyVExxZMxYY/UwzU4JxiEfSwX3kWIfA3zRi+cO/1zOQ+qxxSh1wFW/NuXMP mh0g== X-Forwarded-Encrypted: i=1; AJvYcCUHL3h3/PQonjlRwyaIBs46HkA0D5sgsfrdc0GAfVKrnB9rhBNA7SpYSzYKikQCswT/QpTHgp1leWzbdZc=@vger.kernel.org X-Gm-Message-State: AOJu0YyPWJ/8b8OOoO/djaKJoWhCWTL582nA8fvENuAfAb3F3rosECg5 QVVnGuRcPvhshhuouFe1cnLyLosrOba7tHAxS4EOhtoCRw4SYYJZ1VI+giPgM0h129qTXsLBi6i K14RdTAOB3DZBKge14qHQbF7IaIndN+2q1WVNdJCiV8Qz5DHmJgpn+a1s5dl3Vv6xPGF2CIlJhu Nu X-Gm-Gg: AY/fxX7z+NiyII0SdYtenaghtsnRMmVcISuTSyRC/geWg9aRorVYJiF8QiFBscvKds3 9IBshQcMk7TzLYVi7DwAYKJe6oOp6yp6zCTP/ZGsc73AULLjNPlBhNYMa6cjHZOITzsQfwaGpkH bWTgl2aHEQADGmFIvi8+/5MBam9W4/FtfO8E4j1Ao2gaenxnpFBocGWDkchC2ny4earMMgWPJAL 5O3eBJ9JZBDb2fzVymO5aVvCgyMJ2yPFSZru77uVUvV2+YFF16L+l7YVjV8hzQcvOUwa4XfBaa6 Vk1N1DiUrZ73mH6Y8LmN8GLGNrSOmx5iHhBXkXkBBG4owBDerAe5zmtd1DOJZ6NJYppU4tv6nwY lnVqPErTdGOmVdNYWmf4+W6yLCDc9npCtdWJJ32bUdiuUPUIhk3lwmS2EeSVLo9p20zQPhWO9Dt 2w+9j5jRq/2g== X-Received: by 2002:a05:600c:45cf:b0:477:582e:7a81 with SMTP id 5b1f17b1804b1-47d1954986fmr222389745e9.4.1766747426994; Fri, 26 Dec 2025 03:10:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IHyHY3l79eahMPdTGyMoL8hWD3V6bSrWPn5iCpySXOAIfLf8X++0alz/pvmfAWdglqioPYYPw== X-Received: by 2002:a05:600c:45cf:b0:477:582e:7a81 with SMTP id 5b1f17b1804b1-47d1954986fmr222389445e9.4.1766747426560; Fri, 26 Dec 2025 03:10:26 -0800 (PST) Received: from sissix.lzampier.com ([2a06:5900:814a:ab00:3725:2991:6cf3:b3aa]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d193d4e91sm375653855e9.13.2025.12.26.03.10.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Dec 2025 03:10:26 -0800 (PST) From: Lucas Zampieri To: linux-input@vger.kernel.org Cc: Lucas Zampieri , linux-kernel@vger.kernel.org, Jiri Kosina , Benjamin Tissoires , Sebastian Reichel , Bastien Nocera , linux-pm@vger.kernel.org, lcasmz54@gmail.com Subject: [PATCH v6 2/3] HID: input: Introduce struct hid_battery and refactor battery code Date: Fri, 26 Dec 2025 11:10:15 +0000 Message-ID: <20251226111019.31243-3-lzampier@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251226111019.31243-1-lzampier@redhat.com> References: <20251226111019.31243-1-lzampier@redhat.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" Introduce struct hid_battery to encapsulate individual battery state, preparing for future multi-battery support. The new structure contains all battery-related fields previously stored directly in hid_device (capacity, min, max, report_type, report_id, charge_status, etc.). The hid_device->battery pointer type changes from struct power_supply* to struct hid_battery*, and all battery functions are refactored accordingly. A hid_get_battery() helper is added for external drivers, with hid-apple.c and hid-magicmouse.c updated to use the new API. The hid-input-test.c KUnit tests are also updated for the new structure. No functional changes for single-battery devices. Signed-off-by: Lucas Zampieri --- drivers/hid/hid-apple.c | 10 +-- drivers/hid/hid-input-test.c | 39 ++++++----- drivers/hid/hid-input.c | 123 ++++++++++++++++++++--------------- drivers/hid/hid-magicmouse.c | 10 +-- include/linux/hid.h | 49 ++++++++++---- 5 files changed, 140 insertions(+), 91 deletions(-) diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c index 61404d7a43ee..4889160beefe 100644 --- a/drivers/hid/hid-apple.c +++ b/drivers/hid/hid-apple.c @@ -618,17 +618,19 @@ static int apple_fetch_battery(struct hid_device *hde= v) struct apple_sc *asc =3D hid_get_drvdata(hdev); struct hid_report_enum *report_enum; struct hid_report *report; + struct hid_battery *bat; =20 - if (!(asc->quirks & APPLE_RDESC_BATTERY) || !hdev->battery) + bat =3D hid_get_battery(hdev); + if (!(asc->quirks & APPLE_RDESC_BATTERY) || !bat) return -1; =20 - report_enum =3D &hdev->report_enum[hdev->battery_report_type]; - report =3D report_enum->report_id_hash[hdev->battery_report_id]; + report_enum =3D &hdev->report_enum[bat->report_type]; + report =3D report_enum->report_id_hash[bat->report_id]; =20 if (!report || report->maxfield < 1) return -1; =20 - if (hdev->battery_capacity =3D=3D hdev->battery_max) + if (bat->capacity =3D=3D bat->max) return -1; =20 hid_hw_request(hdev, report, HID_REQ_GET_REPORT); diff --git a/drivers/hid/hid-input-test.c b/drivers/hid/hid-input-test.c index 6f5c71660d82..c92008dafddf 100644 --- a/drivers/hid/hid-input-test.c +++ b/drivers/hid/hid-input-test.c @@ -9,54 +9,59 @@ =20 static void hid_test_input_update_battery_charge_status(struct kunit *test) { - struct hid_device *dev; + struct hid_battery *bat; bool handled; =20 - dev =3D kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL); - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); + bat =3D kunit_kzalloc(test, sizeof(*bat), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bat); =20 - handled =3D hidinput_update_battery_charge_status(dev, HID_DG_HEIGHT, 0); + handled =3D hidinput_update_battery_charge_status(bat, HID_DG_HEIGHT, 0); KUNIT_EXPECT_FALSE(test, handled); - KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_UNK= NOWN); + KUNIT_EXPECT_EQ(test, bat->charge_status, POWER_SUPPLY_STATUS_UNKNOWN); =20 - handled =3D hidinput_update_battery_charge_status(dev, HID_BAT_CHARGING, = 0); + handled =3D hidinput_update_battery_charge_status(bat, HID_BAT_CHARGING, = 0); KUNIT_EXPECT_TRUE(test, handled); - KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_DIS= CHARGING); + KUNIT_EXPECT_EQ(test, bat->charge_status, POWER_SUPPLY_STATUS_DISCHARGING= ); =20 - handled =3D hidinput_update_battery_charge_status(dev, HID_BAT_CHARGING, = 1); + handled =3D hidinput_update_battery_charge_status(bat, HID_BAT_CHARGING, = 1); KUNIT_EXPECT_TRUE(test, handled); - KUNIT_EXPECT_EQ(test, dev->battery_charge_status, POWER_SUPPLY_STATUS_CHA= RGING); + KUNIT_EXPECT_EQ(test, bat->charge_status, POWER_SUPPLY_STATUS_CHARGING); } =20 static void hid_test_input_get_battery_property(struct kunit *test) { struct power_supply *psy; + struct hid_battery *bat; struct hid_device *dev; union power_supply_propval val; int ret; =20 dev =3D kunit_kzalloc(test, sizeof(*dev), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev); - dev->battery_avoid_query =3D true; + + bat =3D kunit_kzalloc(test, sizeof(*bat), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, bat); + bat->dev =3D dev; + bat->avoid_query =3D true; =20 psy =3D kunit_kzalloc(test, sizeof(*psy), GFP_KERNEL); KUNIT_ASSERT_NOT_ERR_OR_NULL(test, psy); - psy->drv_data =3D dev; + psy->drv_data =3D bat; =20 - dev->battery_status =3D HID_BATTERY_UNKNOWN; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_CHARGING; + bat->status =3D HID_BATTERY_UNKNOWN; + bat->charge_status =3D POWER_SUPPLY_STATUS_CHARGING; ret =3D hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val= ); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_UNKNOWN); =20 - dev->battery_status =3D HID_BATTERY_REPORTED; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_CHARGING; + bat->status =3D HID_BATTERY_REPORTED; + bat->charge_status =3D POWER_SUPPLY_STATUS_CHARGING; ret =3D hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val= ); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_CHARGING); =20 - dev->battery_status =3D HID_BATTERY_REPORTED; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; + bat->status =3D HID_BATTERY_REPORTED; + bat->charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; ret =3D hidinput_get_battery_property(psy, POWER_SUPPLY_PROP_STATUS, &val= ); KUNIT_EXPECT_EQ(test, ret, 0); KUNIT_EXPECT_EQ(test, val.intval, POWER_SUPPLY_STATUS_DISCHARGING); diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index 71862beede9e..c2d16236c638 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -418,18 +418,18 @@ static unsigned find_battery_quirk(struct hid_device = *hdev) return quirks; } =20 -static int hidinput_scale_battery_capacity(struct hid_device *dev, +static int hidinput_scale_battery_capacity(struct hid_battery *bat, int value) { - if (dev->battery_min < dev->battery_max && - value >=3D dev->battery_min && value <=3D dev->battery_max) - value =3D ((value - dev->battery_min) * 100) / - (dev->battery_max - dev->battery_min); + if (bat->min < bat->max && + value >=3D bat->min && value <=3D bat->max) + value =3D ((value - bat->min) * 100) / + (bat->max - bat->min); =20 return value; } =20 -static int hidinput_query_battery_capacity(struct hid_device *dev) +static int hidinput_query_battery_capacity(struct hid_battery *bat) { u8 *buf; int ret; @@ -438,14 +438,14 @@ static int hidinput_query_battery_capacity(struct hid= _device *dev) if (!buf) return -ENOMEM; =20 - ret =3D hid_hw_raw_request(dev, dev->battery_report_id, buf, 4, - dev->battery_report_type, HID_REQ_GET_REPORT); + ret =3D hid_hw_raw_request(bat->dev, bat->report_id, buf, 4, + bat->report_type, HID_REQ_GET_REPORT); if (ret < 2) { kfree(buf); return -ENODATA; } =20 - ret =3D hidinput_scale_battery_capacity(dev, buf[1]); + ret =3D hidinput_scale_battery_capacity(bat, buf[1]); kfree(buf); return ret; } @@ -454,7 +454,8 @@ static int hidinput_get_battery_property(struct power_s= upply *psy, enum power_supply_property prop, union power_supply_propval *val) { - struct hid_device *dev =3D power_supply_get_drvdata(psy); + struct hid_battery *bat =3D power_supply_get_drvdata(psy); + struct hid_device *dev =3D bat->dev; int value; int ret =3D 0; =20 @@ -465,13 +466,13 @@ static int hidinput_get_battery_property(struct power= _supply *psy, break; =20 case POWER_SUPPLY_PROP_CAPACITY: - if (dev->battery_status !=3D HID_BATTERY_REPORTED && - !dev->battery_avoid_query) { - value =3D hidinput_query_battery_capacity(dev); + if (bat->status !=3D HID_BATTERY_REPORTED && + !bat->avoid_query) { + value =3D hidinput_query_battery_capacity(bat); if (value < 0) return value; } else { - value =3D dev->battery_capacity; + value =3D bat->capacity; } =20 val->intval =3D value; @@ -482,20 +483,20 @@ static int hidinput_get_battery_property(struct power= _supply *psy, break; =20 case POWER_SUPPLY_PROP_STATUS: - if (dev->battery_status !=3D HID_BATTERY_REPORTED && - !dev->battery_avoid_query) { - value =3D hidinput_query_battery_capacity(dev); + if (bat->status !=3D HID_BATTERY_REPORTED && + !bat->avoid_query) { + value =3D hidinput_query_battery_capacity(bat); if (value < 0) return value; =20 - dev->battery_capacity =3D value; - dev->battery_status =3D HID_BATTERY_QUERIED; + bat->capacity =3D value; + bat->status =3D HID_BATTERY_QUERIED; } =20 - if (dev->battery_status =3D=3D HID_BATTERY_UNKNOWN) + if (bat->status =3D=3D HID_BATTERY_UNKNOWN) val->intval =3D POWER_SUPPLY_STATUS_UNKNOWN; else - val->intval =3D dev->battery_charge_status; + val->intval =3D bat->charge_status; break; =20 case POWER_SUPPLY_PROP_SCOPE: @@ -513,8 +514,9 @@ static int hidinput_get_battery_property(struct power_s= upply *psy, static int hidinput_setup_battery(struct hid_device *dev, unsigned report_= type, struct hid_field *field, bool is_percentage) { + struct hid_battery *bat; struct power_supply_desc *psy_desc; - struct power_supply_config psy_cfg =3D { .drv_data =3D dev, }; + struct power_supply_config psy_cfg =3D { 0 }; unsigned quirks; s32 min, max; int error; @@ -530,16 +532,22 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, if (quirks & HID_BATTERY_QUIRK_IGNORE) return 0; =20 - psy_desc =3D devm_kzalloc(&dev->dev, sizeof(*psy_desc), GFP_KERNEL); - if (!psy_desc) + bat =3D devm_kzalloc(&dev->dev, sizeof(*bat), GFP_KERNEL); + if (!bat) return -ENOMEM; =20 + psy_desc =3D devm_kzalloc(&dev->dev, sizeof(*psy_desc), GFP_KERNEL); + if (!psy_desc) { + error =3D -ENOMEM; + goto err_free_bat; + } + psy_desc->name =3D devm_kasprintf(&dev->dev, GFP_KERNEL, "hid-%s-battery", strlen(dev->uniq) ? dev->uniq : dev_name(&dev->dev)); if (!psy_desc->name) { error =3D -ENOMEM; - goto err_free_mem; + goto err_free_desc; } =20 psy_desc->type =3D POWER_SUPPLY_TYPE_BATTERY; @@ -559,49 +567,55 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, if (quirks & HID_BATTERY_QUIRK_FEATURE) report_type =3D HID_FEATURE_REPORT; =20 - dev->battery_min =3D min; - dev->battery_max =3D max; - dev->battery_report_type =3D report_type; - dev->battery_report_id =3D field->report->id; - dev->battery_charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; + bat->dev =3D dev; + bat->min =3D min; + bat->max =3D max; + bat->report_type =3D report_type; + bat->report_id =3D field->report->id; + bat->charge_status =3D POWER_SUPPLY_STATUS_DISCHARGING; + bat->status =3D HID_BATTERY_UNKNOWN; =20 /* * Stylus is normally not connected to the device and thus we * can't query the device and get meaningful battery strength. * We have to wait for the device to report it on its own. */ - dev->battery_avoid_query =3D report_type =3D=3D HID_INPUT_REPORT && - field->physical =3D=3D HID_DG_STYLUS; + bat->avoid_query =3D report_type =3D=3D HID_INPUT_REPORT && + field->physical =3D=3D HID_DG_STYLUS; =20 if (quirks & HID_BATTERY_QUIRK_AVOID_QUERY) - dev->battery_avoid_query =3D true; + bat->avoid_query =3D true; =20 - dev->battery =3D devm_power_supply_register(&dev->dev, psy_desc, &psy_cfg= ); - if (IS_ERR(dev->battery)) { - error =3D PTR_ERR(dev->battery); + psy_cfg.drv_data =3D bat; + bat->ps =3D devm_power_supply_register(&dev->dev, psy_desc, &psy_cfg); + if (IS_ERR(bat->ps)) { + error =3D PTR_ERR(bat->ps); hid_warn(dev, "can't register power supply: %d\n", error); goto err_free_name; } =20 - power_supply_powers(dev->battery, &dev->dev); + power_supply_powers(bat->ps, &dev->dev); + dev->battery =3D bat; return 0; =20 err_free_name: devm_kfree(&dev->dev, psy_desc->name); -err_free_mem: +err_free_desc: devm_kfree(&dev->dev, psy_desc); +err_free_bat: + devm_kfree(&dev->dev, bat); dev->battery =3D NULL; return error; } =20 -static bool hidinput_update_battery_charge_status(struct hid_device *dev, +static bool hidinput_update_battery_charge_status(struct hid_battery *bat, unsigned int usage, int value) { switch (usage) { case HID_BAT_CHARGING: - dev->battery_charge_status =3D value ? - POWER_SUPPLY_STATUS_CHARGING : - POWER_SUPPLY_STATUS_DISCHARGING; + bat->charge_status =3D value ? + POWER_SUPPLY_STATUS_CHARGING : + POWER_SUPPLY_STATUS_DISCHARGING; return true; } =20 @@ -611,32 +625,33 @@ static bool hidinput_update_battery_charge_status(str= uct hid_device *dev, static void hidinput_update_battery(struct hid_device *dev, unsigned int u= sage, int value) { + struct hid_battery *bat =3D dev->battery; int capacity; =20 - if (!dev->battery) + if (!bat) return; =20 - if (hidinput_update_battery_charge_status(dev, usage, value)) { - power_supply_changed(dev->battery); + if (hidinput_update_battery_charge_status(bat, usage, value)) { + power_supply_changed(bat->ps); return; } =20 if ((usage & HID_USAGE_PAGE) =3D=3D HID_UP_DIGITIZER && value =3D=3D 0) return; =20 - if (value < dev->battery_min || value > dev->battery_max) + if (value < bat->min || value > bat->max) return; =20 - capacity =3D hidinput_scale_battery_capacity(dev, value); + capacity =3D hidinput_scale_battery_capacity(bat, value); =20 - if (dev->battery_status !=3D HID_BATTERY_REPORTED || - capacity !=3D dev->battery_capacity || - ktime_after(ktime_get_coarse(), dev->battery_ratelimit_time)) { - dev->battery_capacity =3D capacity; - dev->battery_status =3D HID_BATTERY_REPORTED; - dev->battery_ratelimit_time =3D + if (bat->status !=3D HID_BATTERY_REPORTED || + capacity !=3D bat->capacity || + ktime_after(ktime_get_coarse(), bat->ratelimit_time)) { + bat->capacity =3D capacity; + bat->status =3D HID_BATTERY_REPORTED; + bat->ratelimit_time =3D ktime_add_ms(ktime_get_coarse(), 30 * 1000); - power_supply_changed(dev->battery); + power_supply_changed(bat->ps); } } #else /* !CONFIG_HID_BATTERY_STRENGTH */ diff --git a/drivers/hid/hid-magicmouse.c b/drivers/hid/hid-magicmouse.c index 7d4a25c6de0e..b3c20af0c010 100644 --- a/drivers/hid/hid-magicmouse.c +++ b/drivers/hid/hid-magicmouse.c @@ -812,19 +812,21 @@ static int magicmouse_fetch_battery(struct hid_device= *hdev) #ifdef CONFIG_HID_BATTERY_STRENGTH struct hid_report_enum *report_enum; struct hid_report *report; + struct hid_battery *bat; =20 - if (!hdev->battery || + bat =3D hid_get_battery(hdev); + if (!bat || (!is_usb_magicmouse2(hdev->vendor, hdev->product) && !is_usb_magictrackpad2(hdev->vendor, hdev->product))) return -1; =20 - report_enum =3D &hdev->report_enum[hdev->battery_report_type]; - report =3D report_enum->report_id_hash[hdev->battery_report_id]; + report_enum =3D &hdev->report_enum[bat->report_type]; + report =3D report_enum->report_id_hash[bat->report_id]; =20 if (!report || report->maxfield < 1) return -1; =20 - if (hdev->battery_capacity =3D=3D hdev->battery_max) + if (bat->capacity =3D=3D bat->max) return -1; =20 hid_hw_request(hdev, report, HID_REQ_GET_REPORT); diff --git a/include/linux/hid.h b/include/linux/hid.h index a4ddb94e3ee5..d14b867299c8 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -634,6 +634,34 @@ enum hid_battery_status { HID_BATTERY_REPORTED, /* Device sent unsolicited battery strength report= */ }; =20 +/** + * struct hid_battery - represents a single battery power supply + * @dev: pointer to the parent hid_device + * @ps: the power supply instance + * @min: minimum battery value from HID descriptor + * @max: maximum battery value from HID descriptor + * @report_type: HID report type (input/feature) + * @report_id: HID report ID for this battery + * @charge_status: current charging status + * @status: battery reporting status + * @capacity: current battery capacity (0-100) + * @avoid_query: if true, avoid querying battery (e.g., for stylus) + * @ratelimit_time: rate limiting for battery reports + */ +struct hid_battery { + struct hid_device *dev; + struct power_supply *ps; + __s32 min; + __s32 max; + __s32 report_type; + __s32 report_id; + __s32 charge_status; + enum hid_battery_status status; + __s32 capacity; + bool avoid_query; + ktime_t ratelimit_time; +}; + struct hid_driver; struct hid_ll_driver; =20 @@ -670,19 +698,9 @@ struct hid_device { #ifdef CONFIG_HID_BATTERY_STRENGTH /* * Power supply information for HID devices which report - * battery strength. power_supply was successfully registered if - * battery is non-NULL. + * battery strength. battery is non-NULL if successfully registered. */ - struct power_supply *battery; - __s32 battery_capacity; - __s32 battery_min; - __s32 battery_max; - __s32 battery_report_type; - __s32 battery_report_id; - __s32 battery_charge_status; - enum hid_battery_status battery_status; - bool battery_avoid_query; - ktime_t battery_ratelimit_time; + struct hid_battery *battery; #endif =20 unsigned long status; /* see STAT flags above */ @@ -743,6 +761,13 @@ static inline void hid_set_drvdata(struct hid_device *= hdev, void *data) dev_set_drvdata(&hdev->dev, data); } =20 +#ifdef CONFIG_HID_BATTERY_STRENGTH +static inline struct hid_battery *hid_get_battery(struct hid_device *hdev) +{ + return hdev->battery; +} +#endif + #define HID_GLOBAL_STACK_SIZE 4 #define HID_COLLECTION_STACK_SIZE 4 =20 --=20 2.52.0 From nobody Sat Feb 7 06:21:08 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 033F0319615 for ; Fri, 26 Dec 2025 11:10:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766747437; cv=none; b=ZZF8nCZSwvYX0+Leo1UTXKIn258/Yo2bewqjsqcjWz7ZNANhlyEuaTvdrvZLrHywq9XgqbRLmMUAfj3FItj9Ft7fjrNwiIFQBco+Y53MTfhGzVS9N7SKLBwM//hTqXVatr09YllmEWq08BzxpOuHHy6dqMBlqpF2iVDj2fM7lNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766747437; c=relaxed/simple; bh=Rn1+K+oDKCqxE1iwfPRSHgU9Q3fMkg2TcTCaOkKZL0U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Mi+jnrjkr2PgHo59DF3Tr6/zygKvBc/vr9qmo6IQwikZOqvVnlfT5f6BTcXSzWdeduVSTYLZbxh3/ELQMeNLBwHk3MtSV7OeYMuylHUY55y+I+0hBZ4vrIaXR/gRxEBFLGXHPUTAuJWLvkdRIHCnoMkAy34cC6KRNox2oXiGGo0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=NJ3NyBpq; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=pqZ6lj/O; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="NJ3NyBpq"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="pqZ6lj/O" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1766747434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QoSNpVz84k6O+emje+qv97k+nCpKT4dVeE4/DACkgMQ=; b=NJ3NyBpqNfPxEfxccRStz4x3qV1nNXxpXNIs3yI5w4bRUf76LpF5TfqpdaPNwGpCAKM/PK UG4OUhTcXbVI2JAhjcATQA3hE0OBaakac7xQPB2A+Htidh8oaaxXjfHcTzEeTv7iRVt8hd CAEp7D0kO1xL5JhI5KO1rnttg25+HMM= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-553-cOEB67ofOEiUAkQfuC0ZEA-1; Fri, 26 Dec 2025 06:10:33 -0500 X-MC-Unique: cOEB67ofOEiUAkQfuC0ZEA-1 X-Mimecast-MFC-AGG-ID: cOEB67ofOEiUAkQfuC0ZEA_1766747432 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-477563e531cso47912935e9.1 for ; Fri, 26 Dec 2025 03:10:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1766747432; x=1767352232; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QoSNpVz84k6O+emje+qv97k+nCpKT4dVeE4/DACkgMQ=; b=pqZ6lj/OLoFocslTEH3kAF4ZhfNDyN00yUtJx10C9HMQCHsupBs5S4t0yEzfW4rhf8 RFtTU8pxptMlZcp5PuvbbWHLm7KBLCmUYEGm3DdwAKGdTJGjTNztiXUlP3b+8jGSpZq5 4jQ1xSW5Sev/tntqLdmPh1OwM7va0eYgTgvDpG5LO2tWUJlrXaYffw7rZ1FvWH56u8N0 cv4CVqXmv27uIpvaz9QFPNq7pu0jXP6YtzhM15b9vCWkWSWcV2u2tBXT9dnRl/KaSu6Y /6HavkpePZbx5/87Y+MdWlF5Xl51DFULNl0KoMBQlvGGJ4LRqPuoIlMWMfRct2v76diP 14Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766747432; x=1767352232; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QoSNpVz84k6O+emje+qv97k+nCpKT4dVeE4/DACkgMQ=; b=sqxZkT99HMpd8mQ+YyzFZCDlhF3kzDxwwjmB1OLs/jK30Npcb0JggoTI0sEqRU5kzS uWUsQnjRiix65ZEMwh+ilWiCmyn/WU6M1TzJuNYue3wcGntneYle00pNa+6Q4RMdzQLZ qZQgz7A/DQOHnVj+k0UNP2xB5gKpQgTm5WN5gPVHF63YtwiV1+XdjX2s9VrllkCLz1G4 NhiMA95JNKKVy8/LqqrB8kY4XsHwdw0bFA/VPHzey8FPsnBlZAWH5rXZQpaQeQKigtLC FiCD3DsbxUAQhPx34ZUusQFBQeBN3zNJ46VGIXKJmlPCZRXycg2COnmOrvb2iaf6GwXu NgpA== X-Forwarded-Encrypted: i=1; AJvYcCWsI2EarkvSTI82JbmGnrCWcrMHEh2Mfp9m4lZepebIJqfJSefkTPII5kkrUi4lSVijw8ICHMj7mvLAiVU=@vger.kernel.org X-Gm-Message-State: AOJu0YzRv0cgpS6FKu3wBpVcIKRtRqOLEeReVt0XHpEAOmYE+naL7fxc 96UMPIY+CprB8lZDXEOfD/0w2Bm98gFBcPCibw22a1arlHa7nX5EKZ+zvb5MmkHKhhLnEMl6Hla tGE9KIaZ9fC7+zLzx1clfWmykKpeHTETk38SOf6bOWI14l5x5/gola5BHk1ausyjE7w== X-Gm-Gg: AY/fxX5FhYF3CElIznvhIkYTcFGQk7M9ty6bJsnSGLKsFMjYYypI8gQG49eqPFHqiDW psORVrVUxOCBqQK0uM1cwqR09F/t6MG/cLHYvu6d1gvKGNV5NA3uNPwKn+raU6iqILwHuftaWpy dnkbUTzhB/fip1sme6orJI0DxwciQhpi+ieXtibmXDgNB/oFRLZcmkAeflT0hVVZO1P9yklDj8/ dY9jLw8qH3KT1n7wD3VA/Ufxipakze9N+DHn4mPKiNOhXMM+9FY7grMBQ+XJx6SN+fUZqaF4iJ+ VjCivxP/nCS5eB5umnLdlZGwL+ufSxMXxRwwl+hIwsoDJ4/M54YeMRrIK0sX7ZCAmicIz8vxjLK 0/eGkoqVeXKycuZ9OCzYYKV+Fnb9dKfkhoYAy5I+3426GnRujH7NqS9RvyGfzUJSJ/fTSN0UAdc +qaTqn9ZTmdQ== X-Received: by 2002:a05:600c:4fd4:b0:46e:4a13:e6c6 with SMTP id 5b1f17b1804b1-47d1958a43fmr220086635e9.19.1766747432468; Fri, 26 Dec 2025 03:10:32 -0800 (PST) X-Google-Smtp-Source: AGHT+IEU4MGJHqxBEWn5UWS0CJv1A2i60wxgLqqXa5EqtmhmRyIjWHos23C9iDTSKsLczNxqr/tUVQ== X-Received: by 2002:a05:600c:4fd4:b0:46e:4a13:e6c6 with SMTP id 5b1f17b1804b1-47d1958a43fmr220083225e9.19.1766747427304; Fri, 26 Dec 2025 03:10:27 -0800 (PST) Received: from sissix.lzampier.com ([2a06:5900:814a:ab00:3725:2991:6cf3:b3aa]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d193d4e91sm375653855e9.13.2025.12.26.03.10.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Dec 2025 03:10:26 -0800 (PST) From: Lucas Zampieri To: linux-input@vger.kernel.org Cc: Lucas Zampieri , linux-kernel@vger.kernel.org, Jiri Kosina , Benjamin Tissoires , Sebastian Reichel , Bastien Nocera , linux-pm@vger.kernel.org, lcasmz54@gmail.com Subject: [PATCH v6 3/3] HID: input: Add support for multiple batteries per device Date: Fri, 26 Dec 2025 11:10:16 +0000 Message-ID: <20251226111019.31243-4-lzampier@redhat.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251226111019.31243-1-lzampier@redhat.com> References: <20251226111019.31243-1-lzampier@redhat.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" Add support for HID devices that report multiple batteries, each identified by its report ID. The hid_device->battery pointer is replaced with a batteries list. Batteries are named using the pattern hid-{uniq}-battery-{report_id}. The hid_get_battery() helper returns the first battery in the list for backwards compatibility with single-battery drivers. Signed-off-by: Lucas Zampieri --- drivers/hid/hid-core.c | 4 ++++ drivers/hid/hid-input.c | 44 ++++++++++++++++++++++++++++------------- include/linux/hid.h | 11 ++++++++--- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c index a5b3a8ca2fcb..76d628547e9a 100644 --- a/drivers/hid/hid-core.c +++ b/drivers/hid/hid-core.c @@ -2990,6 +2990,10 @@ struct hid_device *hid_allocate_device(void) mutex_init(&hdev->ll_open_lock); kref_init(&hdev->ref); =20 +#ifdef CONFIG_HID_BATTERY_STRENGTH + INIT_LIST_HEAD(&hdev->batteries); +#endif + ret =3D hid_bpf_device_init(hdev); if (ret) goto out_err; diff --git a/drivers/hid/hid-input.c b/drivers/hid/hid-input.c index c2d16236c638..445d6f160be8 100644 --- a/drivers/hid/hid-input.c +++ b/drivers/hid/hid-input.c @@ -511,6 +511,18 @@ static int hidinput_get_battery_property(struct power_= supply *psy, return ret; } =20 +static struct hid_battery *hidinput_find_battery(struct hid_device *dev, + int report_id) +{ + struct hid_battery *bat; + + list_for_each_entry(bat, &dev->batteries, list) { + if (bat->report_id =3D=3D report_id) + return bat; + } + return NULL; +} + static int hidinput_setup_battery(struct hid_device *dev, unsigned report_= type, struct hid_field *field, bool is_percentage) { @@ -521,13 +533,15 @@ static int hidinput_setup_battery(struct hid_device *= dev, unsigned report_type, s32 min, max; int error; =20 - if (dev->battery) - return 0; /* already initialized? */ + /* Check if battery for this report ID already exists */ + if (hidinput_find_battery(dev, field->report->id)) + return 0; =20 quirks =3D find_battery_quirk(dev); =20 - hid_dbg(dev, "device %x:%x:%x %d quirks %d\n", - dev->bus, dev->vendor, dev->product, dev->version, quirks); + hid_dbg(dev, "device %x:%x:%x %d quirks %d report_id %d\n", + dev->bus, dev->vendor, dev->product, dev->version, quirks, + field->report->id); =20 if (quirks & HID_BATTERY_QUIRK_IGNORE) return 0; @@ -542,9 +556,11 @@ static int hidinput_setup_battery(struct hid_device *d= ev, unsigned report_type, goto err_free_bat; } =20 - psy_desc->name =3D devm_kasprintf(&dev->dev, GFP_KERNEL, "hid-%s-battery", + psy_desc->name =3D devm_kasprintf(&dev->dev, GFP_KERNEL, + "hid-%s-battery-%d", strlen(dev->uniq) ? - dev->uniq : dev_name(&dev->dev)); + dev->uniq : dev_name(&dev->dev), + field->report->id); if (!psy_desc->name) { error =3D -ENOMEM; goto err_free_desc; @@ -595,7 +611,7 @@ static int hidinput_setup_battery(struct hid_device *de= v, unsigned report_type, } =20 power_supply_powers(bat->ps, &dev->dev); - dev->battery =3D bat; + list_add_tail(&bat->list, &dev->batteries); return 0; =20 err_free_name: @@ -604,7 +620,6 @@ static int hidinput_setup_battery(struct hid_device *de= v, unsigned report_type, devm_kfree(&dev->dev, psy_desc); err_free_bat: devm_kfree(&dev->dev, bat); - dev->battery =3D NULL; return error; } =20 @@ -622,12 +637,13 @@ static bool hidinput_update_battery_charge_status(str= uct hid_battery *bat, return false; } =20 -static void hidinput_update_battery(struct hid_device *dev, unsigned int u= sage, - int value) +static void hidinput_update_battery(struct hid_device *dev, int report_id, + unsigned int usage, int value) { - struct hid_battery *bat =3D dev->battery; + struct hid_battery *bat; int capacity; =20 + bat =3D hidinput_find_battery(dev, report_id); if (!bat) return; =20 @@ -661,8 +677,8 @@ static int hidinput_setup_battery(struct hid_device *de= v, unsigned report_type, return 0; } =20 -static void hidinput_update_battery(struct hid_device *dev, unsigned int u= sage, - int value) +static void hidinput_update_battery(struct hid_device *dev, int report_id, + unsigned int usage, int value) { } #endif /* CONFIG_HID_BATTERY_STRENGTH */ @@ -1530,7 +1546,7 @@ void hidinput_hid_event(struct hid_device *hid, struc= t hid_field *field, struct return; =20 if (usage->type =3D=3D EV_PWR) { - hidinput_update_battery(hid, usage->hid, value); + hidinput_update_battery(hid, report->id, usage->hid, value); return; } =20 diff --git a/include/linux/hid.h b/include/linux/hid.h index d14b867299c8..5069ef90cf7b 100644 --- a/include/linux/hid.h +++ b/include/linux/hid.h @@ -647,6 +647,7 @@ enum hid_battery_status { * @capacity: current battery capacity (0-100) * @avoid_query: if true, avoid querying battery (e.g., for stylus) * @ratelimit_time: rate limiting for battery reports + * @list: list node for linking into hid_device's battery list */ struct hid_battery { struct hid_device *dev; @@ -660,6 +661,7 @@ struct hid_battery { __s32 capacity; bool avoid_query; ktime_t ratelimit_time; + struct list_head list; }; =20 struct hid_driver; @@ -698,9 +700,10 @@ struct hid_device { #ifdef CONFIG_HID_BATTERY_STRENGTH /* * Power supply information for HID devices which report - * battery strength. battery is non-NULL if successfully registered. + * battery strength. Each battery is tracked separately in the + * batteries list. */ - struct hid_battery *battery; + struct list_head batteries; #endif =20 unsigned long status; /* see STAT flags above */ @@ -764,7 +767,9 @@ static inline void hid_set_drvdata(struct hid_device *h= dev, void *data) #ifdef CONFIG_HID_BATTERY_STRENGTH static inline struct hid_battery *hid_get_battery(struct hid_device *hdev) { - return hdev->battery; + if (list_empty(&hdev->batteries)) + return NULL; + return list_first_entry(&hdev->batteries, struct hid_battery, list); } #endif =20 --=20 2.52.0