xref: /openbmc/linux/sound/pci/hda/hda_auto_parser.c (revision 8fdff1dc)
1 /*
2  * BIOS auto-parser helper functions for HD-audio
3  *
4  * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
5  *
6  * This driver is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 
12 #include <linux/slab.h>
13 #include <linux/export.h>
14 #include <linux/sort.h>
15 #include <sound/core.h>
16 #include "hda_codec.h"
17 #include "hda_local.h"
18 #include "hda_auto_parser.h"
19 
20 #define SFX	"hda_codec: "
21 
22 /*
23  * Helper for automatic pin configuration
24  */
25 
26 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
27 {
28 	for (; *list; list++)
29 		if (*list == nid)
30 			return 1;
31 	return 0;
32 }
33 
34 /* a pair of input pin and its sequence */
35 struct auto_out_pin {
36 	hda_nid_t pin;
37 	short seq;
38 };
39 
40 static int compare_seq(const void *ap, const void *bp)
41 {
42 	const struct auto_out_pin *a = ap;
43 	const struct auto_out_pin *b = bp;
44 	return (int)(a->seq - b->seq);
45 }
46 
47 /*
48  * Sort an associated group of pins according to their sequence numbers.
49  * then store it to a pin array.
50  */
51 static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
52 				  int num_pins)
53 {
54 	int i;
55 	sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
56 	for (i = 0; i < num_pins; i++)
57 		pins[i] = list[i].pin;
58 }
59 
60 
61 /* add the found input-pin to the cfg->inputs[] table */
62 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
63 				   int type)
64 {
65 	if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
66 		cfg->inputs[cfg->num_inputs].pin = nid;
67 		cfg->inputs[cfg->num_inputs].type = type;
68 		cfg->num_inputs++;
69 	}
70 }
71 
72 static int compare_input_type(const void *ap, const void *bp)
73 {
74 	const struct auto_pin_cfg_item *a = ap;
75 	const struct auto_pin_cfg_item *b = bp;
76 	return (int)(a->type - b->type);
77 }
78 
79 /* Reorder the surround channels
80  * ALSA sequence is front/surr/clfe/side
81  * HDA sequence is:
82  *    4-ch: front/surr  =>  OK as it is
83  *    6-ch: front/clfe/surr
84  *    8-ch: front/clfe/rear/side|fc
85  */
86 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
87 {
88 	hda_nid_t nid;
89 
90 	switch (nums) {
91 	case 3:
92 	case 4:
93 		nid = pins[1];
94 		pins[1] = pins[2];
95 		pins[2] = nid;
96 		break;
97 	}
98 }
99 
100 /*
101  * Parse all pin widgets and store the useful pin nids to cfg
102  *
103  * The number of line-outs or any primary output is stored in line_outs,
104  * and the corresponding output pins are assigned to line_out_pins[],
105  * in the order of front, rear, CLFE, side, ...
106  *
107  * If more extra outputs (speaker and headphone) are found, the pins are
108  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
109  * is detected, one of speaker of HP pins is assigned as the primary
110  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
111  * if any analog output exists.
112  *
113  * The analog input pins are assigned to inputs array.
114  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
115  * respectively.
116  */
117 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
118 			     struct auto_pin_cfg *cfg,
119 			     const hda_nid_t *ignore_nids,
120 			     unsigned int cond_flags)
121 {
122 	hda_nid_t nid, end_nid;
123 	short seq, assoc_line_out;
124 	struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
125 	struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
126 	struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
127 	int i;
128 
129 	memset(cfg, 0, sizeof(*cfg));
130 
131 	memset(line_out, 0, sizeof(line_out));
132 	memset(speaker_out, 0, sizeof(speaker_out));
133 	memset(hp_out, 0, sizeof(hp_out));
134 	assoc_line_out = 0;
135 
136 	end_nid = codec->start_nid + codec->num_nodes;
137 	for (nid = codec->start_nid; nid < end_nid; nid++) {
138 		unsigned int wid_caps = get_wcaps(codec, nid);
139 		unsigned int wid_type = get_wcaps_type(wid_caps);
140 		unsigned int def_conf;
141 		short assoc, loc, conn, dev;
142 
143 		/* read all default configuration for pin complex */
144 		if (wid_type != AC_WID_PIN)
145 			continue;
146 		/* ignore the given nids (e.g. pc-beep returns error) */
147 		if (ignore_nids && is_in_nid_list(nid, ignore_nids))
148 			continue;
149 
150 		def_conf = snd_hda_codec_get_pincfg(codec, nid);
151 		conn = get_defcfg_connect(def_conf);
152 		if (conn == AC_JACK_PORT_NONE)
153 			continue;
154 		loc = get_defcfg_location(def_conf);
155 		dev = get_defcfg_device(def_conf);
156 
157 		/* workaround for buggy BIOS setups */
158 		if (dev == AC_JACK_LINE_OUT) {
159 			if (conn == AC_JACK_PORT_FIXED)
160 				dev = AC_JACK_SPEAKER;
161 		}
162 
163 		switch (dev) {
164 		case AC_JACK_LINE_OUT:
165 			seq = get_defcfg_sequence(def_conf);
166 			assoc = get_defcfg_association(def_conf);
167 
168 			if (!(wid_caps & AC_WCAP_STEREO))
169 				if (!cfg->mono_out_pin)
170 					cfg->mono_out_pin = nid;
171 			if (!assoc)
172 				continue;
173 			if (!assoc_line_out)
174 				assoc_line_out = assoc;
175 			else if (assoc_line_out != assoc)
176 				continue;
177 			if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
178 				continue;
179 			line_out[cfg->line_outs].pin = nid;
180 			line_out[cfg->line_outs].seq = seq;
181 			cfg->line_outs++;
182 			break;
183 		case AC_JACK_SPEAKER:
184 			seq = get_defcfg_sequence(def_conf);
185 			assoc = get_defcfg_association(def_conf);
186 			if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
187 				continue;
188 			speaker_out[cfg->speaker_outs].pin = nid;
189 			speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
190 			cfg->speaker_outs++;
191 			break;
192 		case AC_JACK_HP_OUT:
193 			seq = get_defcfg_sequence(def_conf);
194 			assoc = get_defcfg_association(def_conf);
195 			if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
196 				continue;
197 			hp_out[cfg->hp_outs].pin = nid;
198 			hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
199 			cfg->hp_outs++;
200 			break;
201 		case AC_JACK_MIC_IN:
202 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
203 			break;
204 		case AC_JACK_LINE_IN:
205 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
206 			break;
207 		case AC_JACK_CD:
208 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
209 			break;
210 		case AC_JACK_AUX:
211 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
212 			break;
213 		case AC_JACK_SPDIF_OUT:
214 		case AC_JACK_DIG_OTHER_OUT:
215 			if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
216 				continue;
217 			cfg->dig_out_pins[cfg->dig_outs] = nid;
218 			cfg->dig_out_type[cfg->dig_outs] =
219 				(loc == AC_JACK_LOC_HDMI) ?
220 				HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
221 			cfg->dig_outs++;
222 			break;
223 		case AC_JACK_SPDIF_IN:
224 		case AC_JACK_DIG_OTHER_IN:
225 			cfg->dig_in_pin = nid;
226 			if (loc == AC_JACK_LOC_HDMI)
227 				cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
228 			else
229 				cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
230 			break;
231 		}
232 	}
233 
234 	/* FIX-UP:
235 	 * If no line-out is defined but multiple HPs are found,
236 	 * some of them might be the real line-outs.
237 	 */
238 	if (!cfg->line_outs && cfg->hp_outs > 1 &&
239 	    !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
240 		int i = 0;
241 		while (i < cfg->hp_outs) {
242 			/* The real HPs should have the sequence 0x0f */
243 			if ((hp_out[i].seq & 0x0f) == 0x0f) {
244 				i++;
245 				continue;
246 			}
247 			/* Move it to the line-out table */
248 			line_out[cfg->line_outs++] = hp_out[i];
249 			cfg->hp_outs--;
250 			memmove(hp_out + i, hp_out + i + 1,
251 				sizeof(hp_out[0]) * (cfg->hp_outs - i));
252 		}
253 		memset(hp_out + cfg->hp_outs, 0,
254 		       sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
255 		if (!cfg->hp_outs)
256 			cfg->line_out_type = AUTO_PIN_HP_OUT;
257 
258 	}
259 
260 	/* sort by sequence */
261 	sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
262 	sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
263 			      cfg->speaker_outs);
264 	sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
265 
266 	/*
267 	 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
268 	 * as a primary output
269 	 */
270 	if (!cfg->line_outs &&
271 	    !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
272 		if (cfg->speaker_outs) {
273 			cfg->line_outs = cfg->speaker_outs;
274 			memcpy(cfg->line_out_pins, cfg->speaker_pins,
275 			       sizeof(cfg->speaker_pins));
276 			cfg->speaker_outs = 0;
277 			memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
278 			cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
279 		} else if (cfg->hp_outs) {
280 			cfg->line_outs = cfg->hp_outs;
281 			memcpy(cfg->line_out_pins, cfg->hp_pins,
282 			       sizeof(cfg->hp_pins));
283 			cfg->hp_outs = 0;
284 			memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
285 			cfg->line_out_type = AUTO_PIN_HP_OUT;
286 		}
287 	}
288 
289 	reorder_outputs(cfg->line_outs, cfg->line_out_pins);
290 	reorder_outputs(cfg->hp_outs, cfg->hp_pins);
291 	reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
292 
293 	/* sort inputs in the order of AUTO_PIN_* type */
294 	sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
295 	     compare_input_type, NULL);
296 
297 	/*
298 	 * debug prints of the parsed results
299 	 */
300 	snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
301 		   cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
302 		   cfg->line_out_pins[2], cfg->line_out_pins[3],
303 		   cfg->line_out_pins[4],
304 		   cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
305 		   (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
306 		    "speaker" : "line"));
307 	snd_printd("   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
308 		   cfg->speaker_outs, cfg->speaker_pins[0],
309 		   cfg->speaker_pins[1], cfg->speaker_pins[2],
310 		   cfg->speaker_pins[3], cfg->speaker_pins[4]);
311 	snd_printd("   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
312 		   cfg->hp_outs, cfg->hp_pins[0],
313 		   cfg->hp_pins[1], cfg->hp_pins[2],
314 		   cfg->hp_pins[3], cfg->hp_pins[4]);
315 	snd_printd("   mono: mono_out=0x%x\n", cfg->mono_out_pin);
316 	if (cfg->dig_outs)
317 		snd_printd("   dig-out=0x%x/0x%x\n",
318 			   cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
319 	snd_printd("   inputs:\n");
320 	for (i = 0; i < cfg->num_inputs; i++) {
321 		snd_printd("     %s=0x%x\n",
322 			    hda_get_autocfg_input_label(codec, cfg, i),
323 			    cfg->inputs[i].pin);
324 	}
325 	if (cfg->dig_in_pin)
326 		snd_printd("   dig-in=0x%x\n", cfg->dig_in_pin);
327 
328 	return 0;
329 }
330 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
331 
332 int snd_hda_get_input_pin_attr(unsigned int def_conf)
333 {
334 	unsigned int loc = get_defcfg_location(def_conf);
335 	unsigned int conn = get_defcfg_connect(def_conf);
336 	if (conn == AC_JACK_PORT_NONE)
337 		return INPUT_PIN_ATTR_UNUSED;
338 	/* Windows may claim the internal mic to be BOTH, too */
339 	if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
340 		return INPUT_PIN_ATTR_INT;
341 	if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
342 		return INPUT_PIN_ATTR_INT;
343 	if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
344 		return INPUT_PIN_ATTR_DOCK;
345 	if (loc == AC_JACK_LOC_REAR)
346 		return INPUT_PIN_ATTR_REAR;
347 	if (loc == AC_JACK_LOC_FRONT)
348 		return INPUT_PIN_ATTR_FRONT;
349 	return INPUT_PIN_ATTR_NORMAL;
350 }
351 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
352 
353 /**
354  * hda_get_input_pin_label - Give a label for the given input pin
355  *
356  * When check_location is true, the function checks the pin location
357  * for mic and line-in pins, and set an appropriate prefix like "Front",
358  * "Rear", "Internal".
359  */
360 
361 static const char *hda_get_input_pin_label(struct hda_codec *codec,
362 					   hda_nid_t pin, bool check_location)
363 {
364 	unsigned int def_conf;
365 	static const char * const mic_names[] = {
366 		"Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
367 	};
368 	int attr;
369 
370 	def_conf = snd_hda_codec_get_pincfg(codec, pin);
371 
372 	switch (get_defcfg_device(def_conf)) {
373 	case AC_JACK_MIC_IN:
374 		if (!check_location)
375 			return "Mic";
376 		attr = snd_hda_get_input_pin_attr(def_conf);
377 		if (!attr)
378 			return "None";
379 		return mic_names[attr - 1];
380 	case AC_JACK_LINE_IN:
381 		if (!check_location)
382 			return "Line";
383 		attr = snd_hda_get_input_pin_attr(def_conf);
384 		if (!attr)
385 			return "None";
386 		if (attr == INPUT_PIN_ATTR_DOCK)
387 			return "Dock Line";
388 		return "Line";
389 	case AC_JACK_AUX:
390 		return "Aux";
391 	case AC_JACK_CD:
392 		return "CD";
393 	case AC_JACK_SPDIF_IN:
394 		return "SPDIF In";
395 	case AC_JACK_DIG_OTHER_IN:
396 		return "Digital In";
397 	default:
398 		return "Misc";
399 	}
400 }
401 
402 /* Check whether the location prefix needs to be added to the label.
403  * If all mic-jacks are in the same location (e.g. rear panel), we don't
404  * have to put "Front" prefix to each label.  In such a case, returns false.
405  */
406 static int check_mic_location_need(struct hda_codec *codec,
407 				   const struct auto_pin_cfg *cfg,
408 				   int input)
409 {
410 	unsigned int defc;
411 	int i, attr, attr2;
412 
413 	defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
414 	attr = snd_hda_get_input_pin_attr(defc);
415 	/* for internal or docking mics, we need locations */
416 	if (attr <= INPUT_PIN_ATTR_NORMAL)
417 		return 1;
418 
419 	attr = 0;
420 	for (i = 0; i < cfg->num_inputs; i++) {
421 		defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
422 		attr2 = snd_hda_get_input_pin_attr(defc);
423 		if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
424 			if (attr && attr != attr2)
425 				return 1; /* different locations found */
426 			attr = attr2;
427 		}
428 	}
429 	return 0;
430 }
431 
432 /**
433  * hda_get_autocfg_input_label - Get a label for the given input
434  *
435  * Get a label for the given input pin defined by the autocfg item.
436  * Unlike hda_get_input_pin_label(), this function checks all inputs
437  * defined in autocfg and avoids the redundant mic/line prefix as much as
438  * possible.
439  */
440 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
441 					const struct auto_pin_cfg *cfg,
442 					int input)
443 {
444 	int type = cfg->inputs[input].type;
445 	int has_multiple_pins = 0;
446 
447 	if ((input > 0 && cfg->inputs[input - 1].type == type) ||
448 	    (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
449 		has_multiple_pins = 1;
450 	if (has_multiple_pins && type == AUTO_PIN_MIC)
451 		has_multiple_pins &= check_mic_location_need(codec, cfg, input);
452 	return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
453 				       has_multiple_pins);
454 }
455 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
456 
457 /* return the position of NID in the list, or -1 if not found */
458 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
459 {
460 	int i;
461 	for (i = 0; i < nums; i++)
462 		if (list[i] == nid)
463 			return i;
464 	return -1;
465 }
466 
467 /* get a unique suffix or an index number */
468 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
469 				    int num_pins, int *indexp)
470 {
471 	static const char * const channel_sfx[] = {
472 		" Front", " Surround", " CLFE", " Side"
473 	};
474 	int i;
475 
476 	i = find_idx_in_nid_list(nid, pins, num_pins);
477 	if (i < 0)
478 		return NULL;
479 	if (num_pins == 1)
480 		return "";
481 	if (num_pins > ARRAY_SIZE(channel_sfx)) {
482 		if (indexp)
483 			*indexp = i;
484 		return "";
485 	}
486 	return channel_sfx[i];
487 }
488 
489 static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
490 {
491 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
492 	int attr = snd_hda_get_input_pin_attr(def_conf);
493 
494 	/* check the location */
495 	switch (attr) {
496 	case INPUT_PIN_ATTR_DOCK:
497 		return "Dock ";
498 	case INPUT_PIN_ATTR_FRONT:
499 		return "Front ";
500 	}
501 	return "";
502 }
503 
504 static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
505 			      const hda_nid_t *pins, int num_pins)
506 {
507 	int i, j, idx = 0;
508 
509 	const char *pfx = check_output_pfx(codec, nid);
510 
511 	i = find_idx_in_nid_list(nid, pins, num_pins);
512 	if (i < 0)
513 		return -1;
514 	for (j = 0; j < i; j++)
515 		if (pfx == check_output_pfx(codec, pins[j]))
516 			idx++;
517 
518 	return idx;
519 }
520 
521 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
522 			       const struct auto_pin_cfg *cfg,
523 			       const char *name, char *label, int maxlen,
524 			       int *indexp)
525 {
526 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
527 	int attr = snd_hda_get_input_pin_attr(def_conf);
528 	const char *pfx, *sfx = "";
529 
530 	/* handle as a speaker if it's a fixed line-out */
531 	if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
532 		name = "Speaker";
533 	pfx = check_output_pfx(codec, nid);
534 
535 	if (cfg) {
536 		/* try to give a unique suffix if needed */
537 		sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
538 				       indexp);
539 		if (!sfx)
540 			sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
541 					       indexp);
542 		if (!sfx) {
543 			/* don't add channel suffix for Headphone controls */
544 			int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
545 						     cfg->hp_outs);
546 			if (idx >= 0)
547 				*indexp = idx;
548 			sfx = "";
549 		}
550 	}
551 	snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
552 	return 1;
553 }
554 
555 /**
556  * snd_hda_get_pin_label - Get a label for the given I/O pin
557  *
558  * Get a label for the given pin.  This function works for both input and
559  * output pins.  When @cfg is given as non-NULL, the function tries to get
560  * an optimized label using hda_get_autocfg_input_label().
561  *
562  * This function tries to give a unique label string for the pin as much as
563  * possible.  For example, when the multiple line-outs are present, it adds
564  * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
565  * If no unique name with a suffix is available and @indexp is non-NULL, the
566  * index number is stored in the pointer.
567  */
568 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
569 			  const struct auto_pin_cfg *cfg,
570 			  char *label, int maxlen, int *indexp)
571 {
572 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
573 	const char *name = NULL;
574 	int i;
575 
576 	if (indexp)
577 		*indexp = 0;
578 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
579 		return 0;
580 
581 	switch (get_defcfg_device(def_conf)) {
582 	case AC_JACK_LINE_OUT:
583 		return fill_audio_out_name(codec, nid, cfg, "Line Out",
584 					   label, maxlen, indexp);
585 	case AC_JACK_SPEAKER:
586 		return fill_audio_out_name(codec, nid, cfg, "Speaker",
587 					   label, maxlen, indexp);
588 	case AC_JACK_HP_OUT:
589 		return fill_audio_out_name(codec, nid, cfg, "Headphone",
590 					   label, maxlen, indexp);
591 	case AC_JACK_SPDIF_OUT:
592 	case AC_JACK_DIG_OTHER_OUT:
593 		if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
594 			name = "HDMI";
595 		else
596 			name = "SPDIF";
597 		if (cfg && indexp) {
598 			i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
599 						 cfg->dig_outs);
600 			if (i >= 0)
601 				*indexp = i;
602 		}
603 		break;
604 	default:
605 		if (cfg) {
606 			for (i = 0; i < cfg->num_inputs; i++) {
607 				if (cfg->inputs[i].pin != nid)
608 					continue;
609 				name = hda_get_autocfg_input_label(codec, cfg, i);
610 				if (name)
611 					break;
612 			}
613 		}
614 		if (!name)
615 			name = hda_get_input_pin_label(codec, nid, true);
616 		break;
617 	}
618 	if (!name)
619 		return 0;
620 	strlcpy(label, name, maxlen);
621 	return 1;
622 }
623 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
624 
625 int snd_hda_gen_add_verbs(struct hda_gen_spec *spec,
626 			  const struct hda_verb *list)
627 {
628 	const struct hda_verb **v;
629 	v = snd_array_new(&spec->verbs);
630 	if (!v)
631 		return -ENOMEM;
632 	*v = list;
633 	return 0;
634 }
635 EXPORT_SYMBOL_HDA(snd_hda_gen_add_verbs);
636 
637 void snd_hda_gen_apply_verbs(struct hda_codec *codec)
638 {
639 	struct hda_gen_spec *spec = codec->spec;
640 	int i;
641 	for (i = 0; i < spec->verbs.used; i++) {
642 		struct hda_verb **v = snd_array_elem(&spec->verbs, i);
643 		snd_hda_sequence_write(codec, *v);
644 	}
645 }
646 EXPORT_SYMBOL_HDA(snd_hda_gen_apply_verbs);
647 
648 void snd_hda_apply_pincfgs(struct hda_codec *codec,
649 			   const struct hda_pintbl *cfg)
650 {
651 	for (; cfg->nid; cfg++)
652 		snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
653 }
654 EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
655 
656 void snd_hda_apply_fixup(struct hda_codec *codec, int action)
657 {
658 	struct hda_gen_spec *spec = codec->spec;
659 	int id = spec->fixup_id;
660 #ifdef CONFIG_SND_DEBUG_VERBOSE
661 	const char *modelname = spec->fixup_name;
662 #endif
663 	int depth = 0;
664 
665 	if (!spec->fixup_list)
666 		return;
667 
668 	while (id >= 0) {
669 		const struct hda_fixup *fix = spec->fixup_list + id;
670 
671 		switch (fix->type) {
672 		case HDA_FIXUP_PINS:
673 			if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
674 				break;
675 			snd_printdd(KERN_INFO SFX
676 				    "%s: Apply pincfg for %s\n",
677 				    codec->chip_name, modelname);
678 			snd_hda_apply_pincfgs(codec, fix->v.pins);
679 			break;
680 		case HDA_FIXUP_VERBS:
681 			if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
682 				break;
683 			snd_printdd(KERN_INFO SFX
684 				    "%s: Apply fix-verbs for %s\n",
685 				    codec->chip_name, modelname);
686 			snd_hda_gen_add_verbs(codec->spec, fix->v.verbs);
687 			break;
688 		case HDA_FIXUP_FUNC:
689 			if (!fix->v.func)
690 				break;
691 			snd_printdd(KERN_INFO SFX
692 				    "%s: Apply fix-func for %s\n",
693 				    codec->chip_name, modelname);
694 			fix->v.func(codec, fix, action);
695 			break;
696 		default:
697 			snd_printk(KERN_ERR SFX
698 				   "%s: Invalid fixup type %d\n",
699 				   codec->chip_name, fix->type);
700 			break;
701 		}
702 		if (!fix->chained)
703 			break;
704 		if (++depth > 10)
705 			break;
706 		id = fix->chain_id;
707 	}
708 }
709 EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
710 
711 void snd_hda_pick_fixup(struct hda_codec *codec,
712 			const struct hda_model_fixup *models,
713 			const struct snd_pci_quirk *quirk,
714 			const struct hda_fixup *fixlist)
715 {
716 	struct hda_gen_spec *spec = codec->spec;
717 	const struct snd_pci_quirk *q;
718 	int id = -1;
719 	const char *name = NULL;
720 
721 	/* when model=nofixup is given, don't pick up any fixups */
722 	if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
723 		spec->fixup_list = NULL;
724 		spec->fixup_id = -1;
725 		return;
726 	}
727 
728 	if (codec->modelname && models) {
729 		while (models->name) {
730 			if (!strcmp(codec->modelname, models->name)) {
731 				id = models->id;
732 				name = models->name;
733 				break;
734 			}
735 			models++;
736 		}
737 	}
738 	if (id < 0 && quirk) {
739 		q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
740 		if (q) {
741 			id = q->value;
742 #ifdef CONFIG_SND_DEBUG_VERBOSE
743 			name = q->name;
744 #endif
745 		}
746 	}
747 	if (id < 0 && quirk) {
748 		for (q = quirk; q->subvendor; q++) {
749 			unsigned int vendorid =
750 				q->subdevice | (q->subvendor << 16);
751 			unsigned int mask = 0xffff0000 | q->subdevice_mask;
752 			if ((codec->subsystem_id & mask) == (vendorid & mask)) {
753 				id = q->value;
754 #ifdef CONFIG_SND_DEBUG_VERBOSE
755 				name = q->name;
756 #endif
757 				break;
758 			}
759 		}
760 	}
761 
762 	spec->fixup_id = id;
763 	if (id >= 0) {
764 		spec->fixup_list = fixlist;
765 		spec->fixup_name = name;
766 	}
767 }
768 EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);
769