From 8a80f70c23ca5f93b1db7e3956cc7ee28c1fa767 Mon Sep 17 00:00:00 2001
From: Jinwang Li <jinwang.li@oss.qualcomm.com>
Date: Mon, 9 Mar 2026 19:18:26 +0800
Subject: [PATCH] transport: Fix set volume failure with invalid device volume

When AVRCP is connected before AVDTP, an invalid device volume causes
the target to reject registration for the EVENT_VOLUME_CHANGED
notification, which breaks subsequent volume updates.

Fix this by initializing the volume to the maximum value in the AVRCP
target init path when it is invalid, allowing the controller to
subscribe to AVRCP_EVENT_VOLUME_CHANGED.

Fixes: fa7828bddd21 ("transport: Fix not being able to initialize volume properly")
Suggested-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Signed-off-by: Jinwang Li <jinwang.li@oss.qualcomm.com>
Upstream-Status: Backport [https://github.com/bluez/bluez/commit/121e5ca79be559c5374f8a54c6fb6e233e383379]
---
 profiles/audio/avrcp.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/profiles/audio/avrcp.c b/profiles/audio/avrcp.c
index 724b46c59..326dfdab7 100644
--- a/profiles/audio/avrcp.c
+++ b/profiles/audio/avrcp.c
@@ -4296,9 +4296,16 @@ static void target_init(struct avrcp *session)
 	if (target->version < 0x0104)
 		return;
 
-	if (avrcp_volume_supported(target))
+	if (avrcp_volume_supported(target)) {
 		session->supported_events |=
 				(1 << AVRCP_EVENT_VOLUME_CHANGED);
+		/* Check if transport volume hasn't been initialized then set it
+		 * to max so it works properly if the controller attempts to
+		 * subscribe to AVRCP_EVENT_VOLUME_CHANGED.
+		 */
+		if (media_transport_get_a2dp_volume(session->dev) < 0)
+			media_transport_set_a2dp_volume(session->dev, 127);
+	}
 
 	session->supported_events |=
 				(1 << AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED) |
-- 
2.34.1

