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