ALSA: hda - Fix potential endless loop at applying quirks
[linux-2.6-block.git] / sound / pci / hda / hda_auto_parser.c
CommitLineData
62810dbd 1// SPDX-License-Identifier: GPL-2.0-or-later
23d30f28
TI
2/*
3 * BIOS auto-parser helper functions for HD-audio
4 *
5 * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de>
23d30f28
TI
6 */
7
8#include <linux/slab.h>
9#include <linux/export.h>
b9030a00 10#include <linux/sort.h>
23d30f28 11#include <sound/core.h>
be57bfff 12#include <sound/hda_codec.h>
128bc4ba 13#include "hda_local.h"
23d30f28
TI
14#include "hda_auto_parser.h"
15
128bc4ba
TI
16/*
17 * Helper for automatic pin configuration
18 */
19
20static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
21{
22 for (; *list; list++)
23 if (*list == nid)
24 return 1;
25 return 0;
26}
27
b9030a00
TI
28/* a pair of input pin and its sequence */
29struct auto_out_pin {
30 hda_nid_t pin;
31 short seq;
32};
33
34static int compare_seq(const void *ap, const void *bp)
35{
36 const struct auto_out_pin *a = ap;
37 const struct auto_out_pin *b = bp;
38 return (int)(a->seq - b->seq);
39}
128bc4ba
TI
40
41/*
42 * Sort an associated group of pins according to their sequence numbers.
b9030a00 43 * then store it to a pin array.
128bc4ba 44 */
b9030a00 45static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
128bc4ba
TI
46 int num_pins)
47{
b9030a00
TI
48 int i;
49 sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
50 for (i = 0; i < num_pins; i++)
51 pins[i] = list[i].pin;
128bc4ba
TI
52}
53
54
55/* add the found input-pin to the cfg->inputs[] table */
95f72cf2
DH
56static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
57 hda_nid_t nid, int type)
128bc4ba
TI
58{
59 if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
60 cfg->inputs[cfg->num_inputs].pin = nid;
61 cfg->inputs[cfg->num_inputs].type = type;
95f72cf2
DH
62 cfg->inputs[cfg->num_inputs].has_boost_on_pin =
63 nid_has_volume(codec, nid, HDA_INPUT);
128bc4ba
TI
64 cfg->num_inputs++;
65 }
66}
67
b9030a00 68static int compare_input_type(const void *ap, const void *bp)
128bc4ba 69{
b9030a00
TI
70 const struct auto_pin_cfg_item *a = ap;
71 const struct auto_pin_cfg_item *b = bp;
95f72cf2
DH
72 if (a->type != b->type)
73 return (int)(a->type - b->type);
74
75 /* In case one has boost and the other one has not,
76 pick the one with boost first. */
77 return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
128bc4ba
TI
78}
79
80/* Reorder the surround channels
81 * ALSA sequence is front/surr/clfe/side
82 * HDA sequence is:
83 * 4-ch: front/surr => OK as it is
84 * 6-ch: front/clfe/surr
85 * 8-ch: front/clfe/rear/side|fc
86 */
87static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
88{
89 hda_nid_t nid;
90
91 switch (nums) {
92 case 3:
93 case 4:
94 nid = pins[1];
95 pins[1] = pins[2];
96 pins[2] = nid;
97 break;
98 }
99}
100
52fd5cbc
TI
101/* check whether the given pin has a proper pin I/O capability bit */
102static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
103 unsigned int dev)
104{
105 unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
106
107 /* some old hardware don't return the proper pincaps */
108 if (!pincap)
109 return true;
110
111 switch (dev) {
112 case AC_JACK_LINE_OUT:
113 case AC_JACK_SPEAKER:
114 case AC_JACK_HP_OUT:
115 case AC_JACK_SPDIF_OUT:
116 case AC_JACK_DIG_OTHER_OUT:
117 return !!(pincap & AC_PINCAP_OUT);
118 default:
119 return !!(pincap & AC_PINCAP_IN);
120 }
121}
122
cb420b11
DH
123static bool can_be_headset_mic(struct hda_codec *codec,
124 struct auto_pin_cfg_item *item,
125 int seq_number)
126{
127 int attr;
128 unsigned int def_conf;
129 if (item->type != AUTO_PIN_MIC)
130 return false;
131
132 if (item->is_headset_mic || item->is_headphone_mic)
133 return false; /* Already assigned */
134
135 def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
136 attr = snd_hda_get_input_pin_attr(def_conf);
137 if (attr <= INPUT_PIN_ATTR_DOCK)
138 return false;
139
140 if (seq_number >= 0) {
141 int seq = get_defcfg_sequence(def_conf);
142 if (seq != seq_number)
143 return false;
144 }
145
146 return true;
147}
148
128bc4ba
TI
149/*
150 * Parse all pin widgets and store the useful pin nids to cfg
151 *
152 * The number of line-outs or any primary output is stored in line_outs,
153 * and the corresponding output pins are assigned to line_out_pins[],
154 * in the order of front, rear, CLFE, side, ...
155 *
156 * If more extra outputs (speaker and headphone) are found, the pins are
157 * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
158 * is detected, one of speaker of HP pins is assigned as the primary
159 * output, i.e. to line_out_pins[0]. So, line_outs is always positive
160 * if any analog output exists.
161 *
162 * The analog input pins are assigned to inputs array.
163 * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
164 * respectively.
165 */
166int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
167 struct auto_pin_cfg *cfg,
168 const hda_nid_t *ignore_nids,
169 unsigned int cond_flags)
170{
7639a06c 171 hda_nid_t nid;
128bc4ba 172 short seq, assoc_line_out;
b9030a00
TI
173 struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
174 struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
175 struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
128bc4ba
TI
176 int i;
177
1c70a583
TI
178 if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
179 cond_flags = i;
180
128bc4ba
TI
181 memset(cfg, 0, sizeof(*cfg));
182
b9030a00
TI
183 memset(line_out, 0, sizeof(line_out));
184 memset(speaker_out, 0, sizeof(speaker_out));
185 memset(hp_out, 0, sizeof(hp_out));
128bc4ba
TI
186 assoc_line_out = 0;
187
7639a06c 188 for_each_hda_codec_node(nid, codec) {
128bc4ba
TI
189 unsigned int wid_caps = get_wcaps(codec, nid);
190 unsigned int wid_type = get_wcaps_type(wid_caps);
191 unsigned int def_conf;
192 short assoc, loc, conn, dev;
193
194 /* read all default configuration for pin complex */
195 if (wid_type != AC_WID_PIN)
196 continue;
197 /* ignore the given nids (e.g. pc-beep returns error) */
198 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
199 continue;
200
201 def_conf = snd_hda_codec_get_pincfg(codec, nid);
128bc4ba
TI
202 conn = get_defcfg_connect(def_conf);
203 if (conn == AC_JACK_PORT_NONE)
204 continue;
205 loc = get_defcfg_location(def_conf);
206 dev = get_defcfg_device(def_conf);
207
208 /* workaround for buggy BIOS setups */
209 if (dev == AC_JACK_LINE_OUT) {
fb690cf5
TI
210 if (conn == AC_JACK_PORT_FIXED ||
211 conn == AC_JACK_PORT_BOTH)
128bc4ba
TI
212 dev = AC_JACK_SPEAKER;
213 }
214
52fd5cbc
TI
215 if (!check_pincap_validity(codec, nid, dev))
216 continue;
217
128bc4ba
TI
218 switch (dev) {
219 case AC_JACK_LINE_OUT:
220 seq = get_defcfg_sequence(def_conf);
221 assoc = get_defcfg_association(def_conf);
222
223 if (!(wid_caps & AC_WCAP_STEREO))
224 if (!cfg->mono_out_pin)
225 cfg->mono_out_pin = nid;
226 if (!assoc)
227 continue;
228 if (!assoc_line_out)
229 assoc_line_out = assoc;
9b7564a6
TI
230 else if (assoc_line_out != assoc) {
231 codec_info(codec,
232 "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
233 nid, assoc, assoc_line_out);
128bc4ba 234 continue;
9b7564a6
TI
235 }
236 if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
237 codec_info(codec,
238 "ignore pin 0x%x, too many assigned pins\n",
239 nid);
128bc4ba 240 continue;
9b7564a6 241 }
b9030a00
TI
242 line_out[cfg->line_outs].pin = nid;
243 line_out[cfg->line_outs].seq = seq;
128bc4ba
TI
244 cfg->line_outs++;
245 break;
246 case AC_JACK_SPEAKER:
247 seq = get_defcfg_sequence(def_conf);
248 assoc = get_defcfg_association(def_conf);
9b7564a6
TI
249 if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
250 codec_info(codec,
251 "ignore pin 0x%x, too many assigned pins\n",
252 nid);
128bc4ba 253 continue;
9b7564a6 254 }
b9030a00
TI
255 speaker_out[cfg->speaker_outs].pin = nid;
256 speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
128bc4ba
TI
257 cfg->speaker_outs++;
258 break;
259 case AC_JACK_HP_OUT:
260 seq = get_defcfg_sequence(def_conf);
261 assoc = get_defcfg_association(def_conf);
9b7564a6
TI
262 if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
263 codec_info(codec,
264 "ignore pin 0x%x, too many assigned pins\n",
265 nid);
128bc4ba 266 continue;
9b7564a6 267 }
b9030a00
TI
268 hp_out[cfg->hp_outs].pin = nid;
269 hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
128bc4ba
TI
270 cfg->hp_outs++;
271 break;
272 case AC_JACK_MIC_IN:
95f72cf2 273 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
128bc4ba
TI
274 break;
275 case AC_JACK_LINE_IN:
95f72cf2 276 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
128bc4ba
TI
277 break;
278 case AC_JACK_CD:
95f72cf2 279 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
128bc4ba
TI
280 break;
281 case AC_JACK_AUX:
95f72cf2 282 add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
128bc4ba
TI
283 break;
284 case AC_JACK_SPDIF_OUT:
285 case AC_JACK_DIG_OTHER_OUT:
9b7564a6
TI
286 if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
287 codec_info(codec,
288 "ignore pin 0x%x, too many assigned pins\n",
289 nid);
128bc4ba 290 continue;
9b7564a6 291 }
128bc4ba
TI
292 cfg->dig_out_pins[cfg->dig_outs] = nid;
293 cfg->dig_out_type[cfg->dig_outs] =
294 (loc == AC_JACK_LOC_HDMI) ?
295 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
296 cfg->dig_outs++;
297 break;
298 case AC_JACK_SPDIF_IN:
299 case AC_JACK_DIG_OTHER_IN:
300 cfg->dig_in_pin = nid;
301 if (loc == AC_JACK_LOC_HDMI)
302 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
303 else
304 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
305 break;
306 }
307 }
308
cb420b11
DH
309 /* Find a pin that could be a headset or headphone mic */
310 if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
311 bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
312 bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
313 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
314 if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
315 cfg->inputs[i].is_headset_mic = 1;
316 hsmic = false;
317 } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
318 cfg->inputs[i].is_headphone_mic = 1;
319 hpmic = false;
320 }
321
322 /* If we didn't find our sequence number mark, fall back to any sequence number */
323 for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
324 if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
a385d97b 325 continue;
cb420b11
DH
326 if (hsmic) {
327 cfg->inputs[i].is_headset_mic = 1;
328 hsmic = false;
329 } else if (hpmic) {
330 cfg->inputs[i].is_headphone_mic = 1;
331 hpmic = false;
332 }
a385d97b 333 }
cb420b11
DH
334
335 if (hsmic)
4e76a883 336 codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
cb420b11 337 if (hpmic)
4e76a883 338 codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
a385d97b
DH
339 }
340
128bc4ba
TI
341 /* FIX-UP:
342 * If no line-out is defined but multiple HPs are found,
343 * some of them might be the real line-outs.
344 */
345 if (!cfg->line_outs && cfg->hp_outs > 1 &&
346 !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
347 int i = 0;
348 while (i < cfg->hp_outs) {
349 /* The real HPs should have the sequence 0x0f */
b9030a00 350 if ((hp_out[i].seq & 0x0f) == 0x0f) {
128bc4ba
TI
351 i++;
352 continue;
353 }
354 /* Move it to the line-out table */
b9030a00 355 line_out[cfg->line_outs++] = hp_out[i];
128bc4ba 356 cfg->hp_outs--;
b9030a00
TI
357 memmove(hp_out + i, hp_out + i + 1,
358 sizeof(hp_out[0]) * (cfg->hp_outs - i));
128bc4ba 359 }
b9030a00
TI
360 memset(hp_out + cfg->hp_outs, 0,
361 sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
128bc4ba
TI
362 if (!cfg->hp_outs)
363 cfg->line_out_type = AUTO_PIN_HP_OUT;
364
365 }
366
367 /* sort by sequence */
b9030a00
TI
368 sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
369 sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
128bc4ba 370 cfg->speaker_outs);
b9030a00 371 sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
128bc4ba
TI
372
373 /*
374 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
375 * as a primary output
376 */
377 if (!cfg->line_outs &&
378 !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
379 if (cfg->speaker_outs) {
380 cfg->line_outs = cfg->speaker_outs;
381 memcpy(cfg->line_out_pins, cfg->speaker_pins,
382 sizeof(cfg->speaker_pins));
383 cfg->speaker_outs = 0;
384 memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
385 cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
386 } else if (cfg->hp_outs) {
387 cfg->line_outs = cfg->hp_outs;
388 memcpy(cfg->line_out_pins, cfg->hp_pins,
389 sizeof(cfg->hp_pins));
390 cfg->hp_outs = 0;
391 memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
392 cfg->line_out_type = AUTO_PIN_HP_OUT;
393 }
394 }
395
396 reorder_outputs(cfg->line_outs, cfg->line_out_pins);
397 reorder_outputs(cfg->hp_outs, cfg->hp_pins);
398 reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
399
b9030a00
TI
400 /* sort inputs in the order of AUTO_PIN_* type */
401 sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
402 compare_input_type, NULL);
128bc4ba
TI
403
404 /*
405 * debug prints of the parsed results
406 */
322b8af1 407 codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
7639a06c 408 codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
322b8af1
DH
409 cfg->line_out_pins[1], cfg->line_out_pins[2],
410 cfg->line_out_pins[3], cfg->line_out_pins[4],
128bc4ba
TI
411 cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
412 (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
413 "speaker" : "line"));
4e76a883 414 codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
128bc4ba
TI
415 cfg->speaker_outs, cfg->speaker_pins[0],
416 cfg->speaker_pins[1], cfg->speaker_pins[2],
417 cfg->speaker_pins[3], cfg->speaker_pins[4]);
4e76a883 418 codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
128bc4ba
TI
419 cfg->hp_outs, cfg->hp_pins[0],
420 cfg->hp_pins[1], cfg->hp_pins[2],
421 cfg->hp_pins[3], cfg->hp_pins[4]);
4e76a883 422 codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
128bc4ba 423 if (cfg->dig_outs)
4e76a883 424 codec_info(codec, " dig-out=0x%x/0x%x\n",
128bc4ba 425 cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
4e76a883 426 codec_info(codec, " inputs:\n");
128bc4ba 427 for (i = 0; i < cfg->num_inputs; i++) {
4e76a883 428 codec_info(codec, " %s=0x%x\n",
128bc4ba
TI
429 hda_get_autocfg_input_label(codec, cfg, i),
430 cfg->inputs[i].pin);
431 }
128bc4ba 432 if (cfg->dig_in_pin)
4e76a883 433 codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
128bc4ba
TI
434
435 return 0;
436}
2698ea98 437EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
128bc4ba 438
95a962c3
TI
439/**
440 * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
441 * @def_conf: pin configuration value
442 *
443 * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
444 * default pin configuration value.
445 */
128bc4ba
TI
446int snd_hda_get_input_pin_attr(unsigned int def_conf)
447{
448 unsigned int loc = get_defcfg_location(def_conf);
449 unsigned int conn = get_defcfg_connect(def_conf);
450 if (conn == AC_JACK_PORT_NONE)
451 return INPUT_PIN_ATTR_UNUSED;
452 /* Windows may claim the internal mic to be BOTH, too */
453 if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
454 return INPUT_PIN_ATTR_INT;
455 if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
456 return INPUT_PIN_ATTR_INT;
457 if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
458 return INPUT_PIN_ATTR_DOCK;
459 if (loc == AC_JACK_LOC_REAR)
460 return INPUT_PIN_ATTR_REAR;
461 if (loc == AC_JACK_LOC_FRONT)
462 return INPUT_PIN_ATTR_FRONT;
463 return INPUT_PIN_ATTR_NORMAL;
464}
2698ea98 465EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
128bc4ba
TI
466
467/**
468 * hda_get_input_pin_label - Give a label for the given input pin
a11e9b16
TI
469 * @codec: the HDA codec
470 * @item: ping config item to refer
471 * @pin: the pin NID
472 * @check_location: flag to add the jack location prefix
128bc4ba 473 *
a11e9b16 474 * When @check_location is true, the function checks the pin location
128bc4ba
TI
475 * for mic and line-in pins, and set an appropriate prefix like "Front",
476 * "Rear", "Internal".
477 */
128bc4ba 478static const char *hda_get_input_pin_label(struct hda_codec *codec,
a385d97b 479 const struct auto_pin_cfg_item *item,
128bc4ba
TI
480 hda_nid_t pin, bool check_location)
481{
482 unsigned int def_conf;
483 static const char * const mic_names[] = {
5ec16d12 484 "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
128bc4ba
TI
485 };
486 int attr;
487
488 def_conf = snd_hda_codec_get_pincfg(codec, pin);
489
490 switch (get_defcfg_device(def_conf)) {
491 case AC_JACK_MIC_IN:
a385d97b
DH
492 if (item && item->is_headset_mic)
493 return "Headset Mic";
cb420b11
DH
494 if (item && item->is_headphone_mic)
495 return "Headphone Mic";
128bc4ba
TI
496 if (!check_location)
497 return "Mic";
498 attr = snd_hda_get_input_pin_attr(def_conf);
499 if (!attr)
500 return "None";
501 return mic_names[attr - 1];
502 case AC_JACK_LINE_IN:
503 if (!check_location)
504 return "Line";
505 attr = snd_hda_get_input_pin_attr(def_conf);
506 if (!attr)
507 return "None";
508 if (attr == INPUT_PIN_ATTR_DOCK)
509 return "Dock Line";
510 return "Line";
511 case AC_JACK_AUX:
512 return "Aux";
513 case AC_JACK_CD:
514 return "CD";
515 case AC_JACK_SPDIF_IN:
516 return "SPDIF In";
517 case AC_JACK_DIG_OTHER_IN:
518 return "Digital In";
54d778b3
TI
519 case AC_JACK_HP_OUT:
520 return "Headphone Mic";
128bc4ba
TI
521 default:
522 return "Misc";
523 }
524}
525
526/* Check whether the location prefix needs to be added to the label.
527 * If all mic-jacks are in the same location (e.g. rear panel), we don't
528 * have to put "Front" prefix to each label. In such a case, returns false.
529 */
530static int check_mic_location_need(struct hda_codec *codec,
531 const struct auto_pin_cfg *cfg,
532 int input)
533{
534 unsigned int defc;
535 int i, attr, attr2;
536
537 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
538 attr = snd_hda_get_input_pin_attr(defc);
539 /* for internal or docking mics, we need locations */
540 if (attr <= INPUT_PIN_ATTR_NORMAL)
541 return 1;
542
543 attr = 0;
544 for (i = 0; i < cfg->num_inputs; i++) {
545 defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
546 attr2 = snd_hda_get_input_pin_attr(defc);
547 if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
548 if (attr && attr != attr2)
549 return 1; /* different locations found */
550 attr = attr2;
551 }
552 }
553 return 0;
554}
555
556/**
557 * hda_get_autocfg_input_label - Get a label for the given input
a11e9b16
TI
558 * @codec: the HDA codec
559 * @cfg: the parsed pin configuration
560 * @input: the input index number
128bc4ba
TI
561 *
562 * Get a label for the given input pin defined by the autocfg item.
563 * Unlike hda_get_input_pin_label(), this function checks all inputs
564 * defined in autocfg and avoids the redundant mic/line prefix as much as
565 * possible.
566 */
567const char *hda_get_autocfg_input_label(struct hda_codec *codec,
568 const struct auto_pin_cfg *cfg,
569 int input)
570{
571 int type = cfg->inputs[input].type;
572 int has_multiple_pins = 0;
573
574 if ((input > 0 && cfg->inputs[input - 1].type == type) ||
575 (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
576 has_multiple_pins = 1;
577 if (has_multiple_pins && type == AUTO_PIN_MIC)
578 has_multiple_pins &= check_mic_location_need(codec, cfg, input);
9f3dadb1 579 has_multiple_pins |= codec->force_pin_prefix;
a385d97b
DH
580 return hda_get_input_pin_label(codec, &cfg->inputs[input],
581 cfg->inputs[input].pin,
128bc4ba
TI
582 has_multiple_pins);
583}
2698ea98 584EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
128bc4ba
TI
585
586/* return the position of NID in the list, or -1 if not found */
587static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
588{
589 int i;
590 for (i = 0; i < nums; i++)
591 if (list[i] == nid)
592 return i;
593 return -1;
594}
595
596/* get a unique suffix or an index number */
597static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
598 int num_pins, int *indexp)
599{
600 static const char * const channel_sfx[] = {
601 " Front", " Surround", " CLFE", " Side"
602 };
603 int i;
604
605 i = find_idx_in_nid_list(nid, pins, num_pins);
606 if (i < 0)
607 return NULL;
608 if (num_pins == 1)
609 return "";
610 if (num_pins > ARRAY_SIZE(channel_sfx)) {
611 if (indexp)
612 *indexp = i;
613 return "";
614 }
615 return channel_sfx[i];
616}
617
eee3ed43
DH
618static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
619{
620 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
621 int attr = snd_hda_get_input_pin_attr(def_conf);
622
623 /* check the location */
624 switch (attr) {
625 case INPUT_PIN_ATTR_DOCK:
626 return "Dock ";
627 case INPUT_PIN_ATTR_FRONT:
628 return "Front ";
629 }
630 return "";
631}
632
633static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
634 const hda_nid_t *pins, int num_pins)
635{
636 int i, j, idx = 0;
637
638 const char *pfx = check_output_pfx(codec, nid);
639
640 i = find_idx_in_nid_list(nid, pins, num_pins);
641 if (i < 0)
642 return -1;
643 for (j = 0; j < i; j++)
644 if (pfx == check_output_pfx(codec, pins[j]))
645 idx++;
646
647 return idx;
648}
649
128bc4ba
TI
650static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
651 const struct auto_pin_cfg *cfg,
652 const char *name, char *label, int maxlen,
653 int *indexp)
654{
655 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
656 int attr = snd_hda_get_input_pin_attr(def_conf);
eee3ed43 657 const char *pfx, *sfx = "";
128bc4ba
TI
658
659 /* handle as a speaker if it's a fixed line-out */
660 if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
661 name = "Speaker";
eee3ed43
DH
662 pfx = check_output_pfx(codec, nid);
663
128bc4ba
TI
664 if (cfg) {
665 /* try to give a unique suffix if needed */
666 sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
667 indexp);
668 if (!sfx)
669 sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
670 indexp);
671 if (!sfx) {
672 /* don't add channel suffix for Headphone controls */
eee3ed43
DH
673 int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
674 cfg->hp_outs);
728deecd 675 if (idx >= 0 && indexp)
128bc4ba
TI
676 *indexp = idx;
677 sfx = "";
678 }
679 }
680 snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
681 return 1;
682}
683
3f25dcf6
DH
684#define is_hdmi_cfg(conf) \
685 (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
686
128bc4ba
TI
687/**
688 * snd_hda_get_pin_label - Get a label for the given I/O pin
a11e9b16
TI
689 * @codec: the HDA codec
690 * @nid: pin NID
691 * @cfg: the parsed pin configuration
692 * @label: the string buffer to store
693 * @maxlen: the max length of string buffer (including termination)
694 * @indexp: the pointer to return the index number (for multiple ctls)
128bc4ba
TI
695 *
696 * Get a label for the given pin. This function works for both input and
697 * output pins. When @cfg is given as non-NULL, the function tries to get
698 * an optimized label using hda_get_autocfg_input_label().
699 *
700 * This function tries to give a unique label string for the pin as much as
701 * possible. For example, when the multiple line-outs are present, it adds
702 * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
703 * If no unique name with a suffix is available and @indexp is non-NULL, the
704 * index number is stored in the pointer.
705 */
706int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
707 const struct auto_pin_cfg *cfg,
708 char *label, int maxlen, int *indexp)
709{
710 unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
711 const char *name = NULL;
712 int i;
3f25dcf6 713 bool hdmi;
128bc4ba
TI
714
715 if (indexp)
716 *indexp = 0;
717 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
718 return 0;
719
720 switch (get_defcfg_device(def_conf)) {
721 case AC_JACK_LINE_OUT:
722 return fill_audio_out_name(codec, nid, cfg, "Line Out",
723 label, maxlen, indexp);
724 case AC_JACK_SPEAKER:
725 return fill_audio_out_name(codec, nid, cfg, "Speaker",
726 label, maxlen, indexp);
727 case AC_JACK_HP_OUT:
728 return fill_audio_out_name(codec, nid, cfg, "Headphone",
729 label, maxlen, indexp);
730 case AC_JACK_SPDIF_OUT:
731 case AC_JACK_DIG_OTHER_OUT:
3f25dcf6
DH
732 hdmi = is_hdmi_cfg(def_conf);
733 name = hdmi ? "HDMI" : "SPDIF";
734 if (cfg && indexp)
735 for (i = 0; i < cfg->dig_outs; i++) {
736 hda_nid_t pin = cfg->dig_out_pins[i];
737 unsigned int c;
738 if (pin == nid)
739 break;
740 c = snd_hda_codec_get_pincfg(codec, pin);
741 if (hdmi == is_hdmi_cfg(c))
742 (*indexp)++;
743 }
128bc4ba
TI
744 break;
745 default:
746 if (cfg) {
747 for (i = 0; i < cfg->num_inputs; i++) {
748 if (cfg->inputs[i].pin != nid)
749 continue;
750 name = hda_get_autocfg_input_label(codec, cfg, i);
751 if (name)
752 break;
753 }
754 }
755 if (!name)
a385d97b 756 name = hda_get_input_pin_label(codec, NULL, nid, true);
128bc4ba
TI
757 break;
758 }
759 if (!name)
760 return 0;
761 strlcpy(label, name, maxlen);
762 return 1;
763}
2698ea98 764EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
128bc4ba 765
95a962c3
TI
766/**
767 * snd_hda_add_verbs - Add verbs to the init list
768 * @codec: the HDA codec
769 * @list: zero-terminated verb list to add
770 *
771 * Append the given verb list to the execution list. The verbs will be
772 * performed at init and resume time via snd_hda_apply_verbs().
773 */
c9ce6b26
TI
774int snd_hda_add_verbs(struct hda_codec *codec,
775 const struct hda_verb *list)
23d30f28
TI
776{
777 const struct hda_verb **v;
c9ce6b26 778 v = snd_array_new(&codec->verbs);
23d30f28
TI
779 if (!v)
780 return -ENOMEM;
781 *v = list;
782 return 0;
783}
2698ea98 784EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
23d30f28 785
95a962c3
TI
786/**
787 * snd_hda_apply_verbs - Execute the init verb lists
788 * @codec: the HDA codec
789 */
c9ce6b26 790void snd_hda_apply_verbs(struct hda_codec *codec)
23d30f28 791{
a9c2dfc8 792 const struct hda_verb **v;
23d30f28 793 int i;
a9c2dfc8
TI
794
795 snd_array_for_each(&codec->verbs, i, v)
23d30f28 796 snd_hda_sequence_write(codec, *v);
23d30f28 797}
2698ea98 798EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
23d30f28 799
95a962c3
TI
800/**
801 * snd_hda_apply_pincfgs - Set each pin config in the given list
802 * @codec: the HDA codec
803 * @cfg: NULL-terminated pin config table
804 */
23d30f28
TI
805void snd_hda_apply_pincfgs(struct hda_codec *codec,
806 const struct hda_pintbl *cfg)
807{
808 for (; cfg->nid; cfg++)
809 snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
810}
2698ea98 811EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
23d30f28 812
fd108215
TI
813static void set_pin_targets(struct hda_codec *codec,
814 const struct hda_pintbl *cfg)
815{
816 for (; cfg->nid; cfg++)
817 snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
818}
819
1f578250 820static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
23d30f28 821{
c9ce6b26 822 const char *modelname = codec->fixup_name;
23d30f28
TI
823
824 while (id >= 0) {
c9ce6b26 825 const struct hda_fixup *fix = codec->fixup_list + id;
23d30f28 826
333f3143
TI
827 if (++depth > 10)
828 break;
1f578250
TI
829 if (fix->chained_before)
830 apply_fixup(codec, fix->chain_id, action, depth + 1);
831
23d30f28
TI
832 switch (fix->type) {
833 case HDA_FIXUP_PINS:
834 if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
835 break;
4e76a883 836 codec_dbg(codec, "%s: Apply pincfg for %s\n",
7639a06c 837 codec->core.chip_name, modelname);
23d30f28
TI
838 snd_hda_apply_pincfgs(codec, fix->v.pins);
839 break;
840 case HDA_FIXUP_VERBS:
841 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
842 break;
4e76a883 843 codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
7639a06c 844 codec->core.chip_name, modelname);
c9ce6b26 845 snd_hda_add_verbs(codec, fix->v.verbs);
23d30f28
TI
846 break;
847 case HDA_FIXUP_FUNC:
848 if (!fix->v.func)
849 break;
4e76a883 850 codec_dbg(codec, "%s: Apply fix-func for %s\n",
7639a06c 851 codec->core.chip_name, modelname);
23d30f28
TI
852 fix->v.func(codec, fix, action);
853 break;
fd108215
TI
854 case HDA_FIXUP_PINCTLS:
855 if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
856 break;
4e76a883 857 codec_dbg(codec, "%s: Apply pinctl for %s\n",
7639a06c 858 codec->core.chip_name, modelname);
fd108215
TI
859 set_pin_targets(codec, fix->v.pins);
860 break;
23d30f28 861 default:
4e76a883 862 codec_err(codec, "%s: Invalid fixup type %d\n",
7639a06c 863 codec->core.chip_name, fix->type);
23d30f28
TI
864 break;
865 }
1f578250 866 if (!fix->chained || fix->chained_before)
23d30f28 867 break;
23d30f28
TI
868 id = fix->chain_id;
869 }
870}
1f578250 871
95a962c3
TI
872/**
873 * snd_hda_apply_fixup - Apply the fixup chain with the given action
874 * @codec: the HDA codec
875 * @action: fixup action (HDA_FIXUP_ACT_XXX)
876 */
1f578250
TI
877void snd_hda_apply_fixup(struct hda_codec *codec, int action)
878{
879 if (codec->fixup_list)
880 apply_fixup(codec, codec->fixup_id, action, 0);
881}
2698ea98 882EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
23d30f28 883
5e0ad0d8
KHF
884#define IGNORE_SEQ_ASSOC (~(AC_DEFCFG_SEQUENCE | AC_DEFCFG_DEF_ASSOC))
885
20531415
DH
886static bool pin_config_match(struct hda_codec *codec,
887 const struct hda_pintbl *pins)
888{
a9c2dfc8 889 const struct hda_pincfg *pin;
11580297
HW
890 int i;
891
a9c2dfc8 892 snd_array_for_each(&codec->init_pins, i, pin) {
11580297
HW
893 hda_nid_t nid = pin->nid;
894 u32 cfg = pin->cfg;
895 const struct hda_pintbl *t_pins;
896 int found;
897
898 t_pins = pins;
899 found = 0;
900 for (; t_pins->nid; t_pins++) {
901 if (t_pins->nid == nid) {
902 found = 1;
5e0ad0d8 903 if ((t_pins->val & IGNORE_SEQ_ASSOC) == (cfg & IGNORE_SEQ_ASSOC))
11580297
HW
904 break;
905 else if ((cfg & 0xf0000000) == 0x40000000 && (t_pins->val & 0xf0000000) == 0x40000000)
906 break;
907 else
908 return false;
909 }
910 }
911 if (!found && (cfg & 0xf0000000) != 0x40000000)
20531415
DH
912 return false;
913 }
11580297 914
20531415
DH
915 return true;
916}
917
95a962c3
TI
918/**
919 * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
920 * @codec: the HDA codec
921 * @pin_quirk: zero-terminated pin quirk list
922 * @fixlist: the fixup list
923 */
20531415
DH
924void snd_hda_pick_pin_fixup(struct hda_codec *codec,
925 const struct snd_hda_pin_quirk *pin_quirk,
926 const struct hda_fixup *fixlist)
927{
928 const struct snd_hda_pin_quirk *pq;
929
f5662e1c 930 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
20531415
DH
931 return;
932
933 for (pq = pin_quirk; pq->subvendor; pq++) {
7639a06c 934 if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
20531415 935 continue;
7639a06c 936 if (codec->core.vendor_id != pq->codec)
20531415
DH
937 continue;
938 if (pin_config_match(codec, pq->pins)) {
939 codec->fixup_id = pq->value;
940#ifdef CONFIG_SND_DEBUG_VERBOSE
941 codec->fixup_name = pq->name;
4f7946ec 942 codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
7639a06c 943 codec->core.chip_name, codec->fixup_name);
20531415
DH
944#endif
945 codec->fixup_list = fixlist;
946 return;
947 }
948 }
949}
950EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
951
95a962c3
TI
952/**
953 * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
954 * @codec: the HDA codec
955 * @models: NULL-terminated model string list
956 * @quirk: zero-terminated PCI/codec SSID quirk list
957 * @fixlist: the fixup list
958 *
959 * Pick up a fixup entry matching with the given model string or SSID.
960 * If a fixup was already set beforehand, the function doesn't do anything.
961 * When a special model string "nofixup" is given, also no fixup is applied.
962 *
963 * The function tries to find the matching model name at first, if given.
964 * If nothing matched, try to look up the PCI SSID.
965 * If still nothing matched, try to look up the codec SSID.
966 */
23d30f28
TI
967void snd_hda_pick_fixup(struct hda_codec *codec,
968 const struct hda_model_fixup *models,
969 const struct snd_pci_quirk *quirk,
970 const struct hda_fixup *fixlist)
971{
23d30f28 972 const struct snd_pci_quirk *q;
f5662e1c 973 int id = HDA_FIXUP_ID_NOT_SET;
23d30f28
TI
974 const char *name = NULL;
975
f5662e1c
DH
976 if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
977 return;
978
23d30f28
TI
979 /* when model=nofixup is given, don't pick up any fixups */
980 if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
c9ce6b26 981 codec->fixup_list = NULL;
f5662e1c
DH
982 codec->fixup_name = NULL;
983 codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
4f7946ec 984 codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
7639a06c 985 codec->core.chip_name);
23d30f28
TI
986 return;
987 }
988
989 if (codec->modelname && models) {
990 while (models->name) {
991 if (!strcmp(codec->modelname, models->name)) {
c21c8cf7
DH
992 codec->fixup_id = models->id;
993 codec->fixup_name = models->name;
8fffe7d1 994 codec->fixup_list = fixlist;
4f7946ec 995 codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
7639a06c 996 codec->core.chip_name, codec->fixup_name);
c21c8cf7 997 return;
23d30f28
TI
998 }
999 models++;
1000 }
1001 }
f5662e1c 1002 if (quirk) {
23d30f28
TI
1003 q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
1004 if (q) {
1005 id = q->value;
1006#ifdef CONFIG_SND_DEBUG_VERBOSE
1007 name = q->name;
4f7946ec 1008 codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
7639a06c 1009 codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
23d30f28
TI
1010#endif
1011 }
1012 }
639aa4bd 1013 if (id < 0 && quirk) {
697aebab 1014 for (q = quirk; q->subvendor || q->subdevice; q++) {
23d30f28
TI
1015 unsigned int vendorid =
1016 q->subdevice | (q->subvendor << 16);
a33b7b0a 1017 unsigned int mask = 0xffff0000 | q->subdevice_mask;
7639a06c 1018 if ((codec->core.subsystem_id & mask) == (vendorid & mask)) {
23d30f28
TI
1019 id = q->value;
1020#ifdef CONFIG_SND_DEBUG_VERBOSE
1021 name = q->name;
4f7946ec 1022 codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
7639a06c 1023 codec->core.chip_name, name);
23d30f28
TI
1024#endif
1025 break;
1026 }
1027 }
1028 }
1029
c9ce6b26 1030 codec->fixup_id = id;
23d30f28 1031 if (id >= 0) {
c9ce6b26
TI
1032 codec->fixup_list = fixlist;
1033 codec->fixup_name = name;
23d30f28
TI
1034 }
1035}
2698ea98 1036EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);