Linux Sound subsystem development
 help / color / mirror / Atom feed
From: Wade Wang <wade.wang@hp.com>
To: jikos@kernel.org, tiwai@suse.com, bentiss@kernel.org,
	perex@perex.cz, linuxhid@cosmicgizmosystems.com
Cc: wade.wang@hp.com, linux-input@vger.kernel.org,
	linux-sound@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH v2 2/2] ALSA: usb-audio: Add quirk for Plantronics headsets to fix control names
Date: Tue, 24 Dec 2024 14:56:36 +0800	[thread overview]
Message-ID: <20241224065636.1870713-2-wade.wang@hp.com> (raw)
In-Reply-To: <20241224065636.1870713-1-wade.wang@hp.com>

From: Terry Junge <linuxhid@cosmicgizmosystems.com>

Many Poly/Plantronics headset families name the feature, input,
and/or output units in a such a way to produce control names
that are not recognized by user space. As such, the volume and
mute events do not get routed to the headset's audio controls.

As an example from a product family:

The microphone mute control is named
Headset Microphone Capture Switch
and the headset volume control is named
Headset Earphone Playback Volume

The quirk fixes these to become
Headset Capture Switch
Headset Playback Volume

Signed-off-by: Terry Junge <linuxhid@cosmicgizmosystems.com>
Signed-off-by: Wade Wang <wade.wang@hp.com>
Cc: stable@vger.kernel.org
---
V1 -> V2: Add comments, usb_audio_dbg() calls, fix leading space case

 sound/usb/mixer_quirks.c | 66 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/sound/usb/mixer_quirks.c b/sound/usb/mixer_quirks.c
index 23fcd680167d..5728c03dc49f 100644
--- a/sound/usb/mixer_quirks.c
+++ b/sound/usb/mixer_quirks.c
@@ -4216,6 +4216,67 @@ static void snd_dragonfly_quirk_db_scale(struct usb_mixer_interface *mixer,
 	}
 }
 
+/*
+ * Some Plantronics headsets have control names that don't meet ALSA naming
+ * standards. This function removes nonstandard source names. By the time
+ * this function is called the control name will look like one of these:
+ * "source names Playback Volume"
+ * "source names Playback Switch"
+ * "source names Capture Volume"
+ * "source names Capture Switch"
+ * First it scans through the list and removes any found name(s) by moving the
+ * remaining string and its null terminator over the found name and its leading
+ * space, if it has one.
+ * Second, if all source names were removed, it puts back "Headset"
+ * otherwise removes a leading space, if there is one.
+ */
+static void snd_fix_plt_control_name(struct usb_mixer_interface *mixer,
+				     struct snd_kcontrol *kctl)
+{
+	/* no variant of "Sidetone" should be added to this list */
+	static const char * const names_to_remove[] = {
+		"Earphone",
+		"Microphone",
+		"Receive",
+		"Transmit",
+		NULL
+	};
+	const char * const *n2r;
+	char *dst, *src, *last = NULL;
+	size_t len = 0;
+
+	for (n2r = names_to_remove; *n2r; ++n2r) {
+		dst = strstr(kctl->id.name, *n2r);
+		if (dst) {
+			usb_audio_dbg(mixer->chip, "found %s in %s\n",
+					*n2r, kctl->id.name);
+			src = dst + strlen(*n2r);
+			len = strlen(src) + 1;
+			if ((char *)kctl->id.name != dst && *(dst - 1) == ' ')
+				--dst;
+			last = memmove(dst, src, len);
+			usb_audio_dbg(mixer->chip, "now %s\n", kctl->id.name);
+		}
+	}
+	if (!len) {
+		usb_audio_dbg(mixer->chip, "no change in %s\n", kctl->id.name);
+		return;
+	}
+	if (len <= sizeof " Playback Volume" && (char *)kctl->id.name == last) {
+		char rcat[sizeof(kctl->id.name)] = { "Headset" };
+
+		strlcat(rcat, kctl->id.name, sizeof(rcat));
+		strscpy(kctl->id.name, rcat, sizeof(kctl->id.name));
+		usb_audio_dbg(mixer->chip, "now %s\n", kctl->id.name);
+	} else if (kctl->id.name[0] == ' ') {
+		dst = kctl->id.name;
+		src = dst + 1;
+		len = strlen(src) + 1;
+		memmove(dst, src, len);
+		usb_audio_dbg(mixer->chip, "now %s\n", kctl->id.name);
+	}
+}
+
 void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
 				  struct usb_mixer_elem_info *cval, int unitid,
 				  struct snd_kcontrol *kctl)
@@ -4233,5 +4294,10 @@ void snd_usb_mixer_fu_apply_quirk(struct usb_mixer_interface *mixer,
 			cval->min_mute = 1;
 		break;
 	}
+
+	/* ALSA-ify some Plantronics headset control names */
+	if (USB_ID_VENDOR(mixer->chip->usb_id) == 0x047f &&
+	    (cval->control == UAC_FU_MUTE || cval->control == UAC_FU_VOLUME))
+		snd_fix_plt_control_name(mixer, kctl);
 }
 
-- 
2.43.0


  reply	other threads:[~2024-12-24  7:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-24  6:56 [PATCH v2 1/2] HID: hid-plantronics: Add mic mute mapping and generalize quirks Wade Wang
2024-12-24  6:56 ` Wade Wang [this message]
2025-01-03 15:51   ` [PATCH v2 2/2] ALSA: usb-audio: Add quirk for Plantronics headsets to fix control names Takashi Iwai
2025-01-03 23:57     ` Terry Junge
2024-12-25  4:12 ` [PATCH v2 1/2] HID: hid-plantronics: Add mic mute mapping and generalize quirks Terry Junge
2024-12-25  4:35 ` Terry Junge
  -- strict thread matches above, loose matches on Subject: below --
2024-12-24  6:53 Wade Wang
2024-12-24  6:53 ` [PATCH v2 2/2] ALSA: usb-audio: Add quirk for Plantronics headsets to fix control names Wade Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241224065636.1870713-2-wade.wang@hp.com \
    --to=wade.wang@hp.com \
    --cc=bentiss@kernel.org \
    --cc=jikos@kernel.org \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=linuxhid@cosmicgizmosystems.com \
    --cc=perex@perex.cz \
    --cc=stable@vger.kernel.org \
    --cc=tiwai@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox