From 358b6e62b86f6313d114e0f6b7d8f8adaf85ed9c Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Mon, 21 Nov 2011 14:34:20 +0100
Subject: [PATCH] ALSA: hda - Don't add channel suffix for headphone pin labels
Git-commit: 358b6e62b86f6313d114e0f6b7d8f8adaf85ed9c
Patch-mainline: 3.3-rc1
References: FATE#313695

The multiple headphone pins are usually handled as copied from the same
source, not as individual channels like front and surround.  Thus it'd
be more correct to avoid the channel suffix for "Headphone" pin labels
in snd_hda_get_pin_label() but give an index number instead.

Signed-off-by: Takashi Iwai <tiwai@suse.de>

---
 sound/pci/hda/hda_codec.c |   52 +++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 21 deletions(-)

--- a/sound/pci/hda/hda_codec.c
+++ b/sound/pci/hda/hda_codec.c
@@ -5075,6 +5075,16 @@ const char *hda_get_autocfg_input_label(
 }
 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
 
+/* return the position of NID in the list, or -1 if not found */
+static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
+{
+	int i;
+	for (i = 0; i < nums; i++)
+		if (list[i] == nid)
+			return i;
+	return -1;
+}
+
 /* get a unique suffix or an index number */
 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
 				    int num_pins, int *indexp)
@@ -5084,19 +5094,17 @@ static const char *check_output_sfx(hda_
 	};
 	int i;
 
-	for (i = 0; i < num_pins; i++) {
-		if (pins[i] == nid) {
-			if (num_pins == 1)
-				return "";
-			if (num_pins > ARRAY_SIZE(channel_sfx)) {
-				if (indexp)
-					*indexp = i;
-				return "";
-			}
-			return channel_sfx[i];
-		}
+	i = find_idx_in_nid_list(nid, pins, num_pins);
+	if (i < 0)
+		return NULL;
+	if (num_pins == 1)
+		return "";
+	if (num_pins > ARRAY_SIZE(channel_sfx)) {
+		if (indexp)
+			*indexp = i;
+		return "";
 	}
-	return NULL;
+	return channel_sfx[i];
 }
 
 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
@@ -5125,13 +5133,16 @@ static int fill_audio_out_name(struct hd
 		sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
 				       indexp);
 		if (!sfx)
-			sfx = check_output_sfx(nid, cfg->hp_pins, cfg->hp_outs,
-					       indexp);
-		if (!sfx)
 			sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
 					       indexp);
-		if (!sfx)
+		if (!sfx) {
+			/* don't add channel suffix for Headphone controls */
+			int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
+						       cfg->hp_outs);
+			if (idx >= 0)
+				*indexp = idx;
 			sfx = "";
+		}
 	}
 	snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
 	return 1;
@@ -5180,11 +5191,10 @@ int snd_hda_get_pin_label(struct hda_cod
 		else
 			name = "SPDIF";
 		if (cfg && indexp) {
-			for (i = 0; i < cfg->dig_outs; i++)
-				if (cfg->dig_out_pins[i] == nid) {
-					*indexp = i;
-					break;
-				}
+			i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
+						 cfg->dig_outs);
+			if (i >= 0)
+				*indexp = i;
 		}
 		break;
 	default:
