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