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