Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / pci / hda / hda_generic.c
blobf9de7c467c2552ad56b73568fa07caae203f7e85
1 /*
2 * Universal Interface for Intel High Definition Audio Codec
4 * Generic widget tree parser
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
8 * This driver is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This driver is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <sound/core.h>
26 #include "hda_codec.h"
27 #include "hda_local.h"
29 /* widget node for parsing */
30 struct hda_gnode {
31 hda_nid_t nid; /* NID of this widget */
32 unsigned short nconns; /* number of input connections */
33 hda_nid_t *conn_list;
34 hda_nid_t slist[2]; /* temporay list */
35 unsigned int wid_caps; /* widget capabilities */
36 unsigned char type; /* widget type */
37 unsigned char pin_ctl; /* pin controls */
38 unsigned char checked; /* the flag indicates that the node is already parsed */
39 unsigned int pin_caps; /* pin widget capabilities */
40 unsigned int def_cfg; /* default configuration */
41 unsigned int amp_out_caps; /* AMP out capabilities */
42 unsigned int amp_in_caps; /* AMP in capabilities */
43 struct list_head list;
46 /* patch-specific record */
48 #define MAX_PCM_VOLS 2
49 struct pcm_vol {
50 struct hda_gnode *node; /* Node for PCM volume */
51 unsigned int index; /* connection of PCM volume */
54 struct hda_gspec {
55 struct hda_gnode *dac_node[2]; /* DAC node */
56 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
57 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
58 unsigned int pcm_vol_nodes; /* number of PCM volumes */
60 struct hda_gnode *adc_node; /* ADC node */
61 struct hda_gnode *cap_vol_node; /* Node for capture volume */
62 unsigned int cur_cap_src; /* current capture source */
63 struct hda_input_mux input_mux;
64 char cap_labels[HDA_MAX_NUM_INPUTS][16];
66 unsigned int def_amp_in_caps;
67 unsigned int def_amp_out_caps;
69 struct hda_pcm pcm_rec; /* PCM information */
71 struct list_head nid_list; /* list of widgets */
73 #ifdef CONFIG_SND_HDA_POWER_SAVE
74 #define MAX_LOOPBACK_AMPS 7
75 struct hda_loopback_check loopback;
76 int num_loopbacks;
77 struct hda_amp_list loopback_list[MAX_LOOPBACK_AMPS + 1];
78 #endif
82 * retrieve the default device type from the default config value
84 #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
85 AC_DEFCFG_DEVICE_SHIFT)
86 #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
87 AC_DEFCFG_LOCATION_SHIFT)
88 #define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
89 AC_DEFCFG_PORT_CONN_SHIFT)
92 * destructor
94 static void snd_hda_generic_free(struct hda_codec *codec)
96 struct hda_gspec *spec = codec->spec;
97 struct hda_gnode *node, *n;
99 if (! spec)
100 return;
101 /* free all widgets */
102 list_for_each_entry_safe(node, n, &spec->nid_list, list) {
103 if (node->conn_list != node->slist)
104 kfree(node->conn_list);
105 kfree(node);
107 kfree(spec);
112 * add a new widget node and read its attributes
114 static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
116 struct hda_gnode *node;
117 int nconns;
118 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
120 node = kzalloc(sizeof(*node), GFP_KERNEL);
121 if (node == NULL)
122 return -ENOMEM;
123 node->nid = nid;
124 nconns = snd_hda_get_connections(codec, nid, conn_list,
125 HDA_MAX_CONNECTIONS);
126 if (nconns < 0) {
127 kfree(node);
128 return nconns;
130 if (nconns <= ARRAY_SIZE(node->slist))
131 node->conn_list = node->slist;
132 else {
133 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
134 GFP_KERNEL);
135 if (! node->conn_list) {
136 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
137 kfree(node);
138 return -ENOMEM;
141 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
142 node->nconns = nconns;
143 node->wid_caps = get_wcaps(codec, nid);
144 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
146 if (node->type == AC_WID_PIN) {
147 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
148 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
149 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
152 if (node->wid_caps & AC_WCAP_OUT_AMP) {
153 if (node->wid_caps & AC_WCAP_AMP_OVRD)
154 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
155 if (! node->amp_out_caps)
156 node->amp_out_caps = spec->def_amp_out_caps;
158 if (node->wid_caps & AC_WCAP_IN_AMP) {
159 if (node->wid_caps & AC_WCAP_AMP_OVRD)
160 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
161 if (! node->amp_in_caps)
162 node->amp_in_caps = spec->def_amp_in_caps;
164 list_add_tail(&node->list, &spec->nid_list);
165 return 0;
169 * build the AFG subtree
171 static int build_afg_tree(struct hda_codec *codec)
173 struct hda_gspec *spec = codec->spec;
174 int i, nodes, err;
175 hda_nid_t nid;
177 snd_assert(spec, return -EINVAL);
179 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
180 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
182 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
183 if (! nid || nodes < 0) {
184 printk(KERN_ERR "Invalid AFG subtree\n");
185 return -EINVAL;
188 /* parse all nodes belonging to the AFG */
189 for (i = 0; i < nodes; i++, nid++) {
190 if ((err = add_new_node(codec, spec, nid)) < 0)
191 return err;
194 return 0;
199 * look for the node record for the given NID
201 /* FIXME: should avoid the braindead linear search */
202 static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
204 struct hda_gnode *node;
206 list_for_each_entry(node, &spec->nid_list, list) {
207 if (node->nid == nid)
208 return node;
210 return NULL;
214 * unmute (and set max vol) the output amplifier
216 static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
218 unsigned int val, ofs;
219 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
220 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
221 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
222 if (val >= ofs)
223 val -= ofs;
224 snd_hda_codec_amp_stereo(codec, node->nid, HDA_OUTPUT, 0, 0xff, val);
225 return 0;
229 * unmute (and set max vol) the input amplifier
231 static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
233 unsigned int val, ofs;
234 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
235 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
236 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
237 if (val >= ofs)
238 val -= ofs;
239 snd_hda_codec_amp_stereo(codec, node->nid, HDA_INPUT, index, 0xff, val);
240 return 0;
244 * select the input connection of the given node.
246 static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
247 unsigned int index)
249 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
250 return snd_hda_codec_write_cache(codec, node->nid, 0,
251 AC_VERB_SET_CONNECT_SEL, index);
255 * clear checked flag of each node in the node list
257 static void clear_check_flags(struct hda_gspec *spec)
259 struct hda_gnode *node;
261 list_for_each_entry(node, &spec->nid_list, list) {
262 node->checked = 0;
267 * parse the output path recursively until reach to an audio output widget
269 * returns 0 if not found, 1 if found, or a negative error code.
271 static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
272 struct hda_gnode *node, int dac_idx)
274 int i, err;
275 struct hda_gnode *child;
277 if (node->checked)
278 return 0;
280 node->checked = 1;
281 if (node->type == AC_WID_AUD_OUT) {
282 if (node->wid_caps & AC_WCAP_DIGITAL) {
283 snd_printdd("Skip Digital OUT node %x\n", node->nid);
284 return 0;
286 snd_printdd("AUD_OUT found %x\n", node->nid);
287 if (spec->dac_node[dac_idx]) {
288 /* already DAC node is assigned, just unmute & connect */
289 return node == spec->dac_node[dac_idx];
291 spec->dac_node[dac_idx] = node;
292 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
293 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
294 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
295 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
296 spec->pcm_vol_nodes++;
298 return 1; /* found */
301 for (i = 0; i < node->nconns; i++) {
302 child = hda_get_node(spec, node->conn_list[i]);
303 if (! child)
304 continue;
305 err = parse_output_path(codec, spec, child, dac_idx);
306 if (err < 0)
307 return err;
308 else if (err > 0) {
309 /* found one,
310 * select the path, unmute both input and output
312 if (node->nconns > 1)
313 select_input_connection(codec, node, i);
314 unmute_input(codec, node, i);
315 unmute_output(codec, node);
316 if (spec->dac_node[dac_idx] &&
317 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
318 !(spec->dac_node[dac_idx]->wid_caps &
319 AC_WCAP_OUT_AMP)) {
320 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
321 (node->wid_caps & AC_WCAP_OUT_AMP)) {
322 int n = spec->pcm_vol_nodes;
323 spec->pcm_vol[n].node = node;
324 spec->pcm_vol[n].index = i;
325 spec->pcm_vol_nodes++;
328 return 1;
331 return 0;
335 * Look for the output PIN widget with the given jack type
336 * and parse the output path to that PIN.
338 * Returns the PIN node when the path to DAC is established.
340 static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
341 struct hda_gspec *spec,
342 int jack_type)
344 struct hda_gnode *node;
345 int err;
347 list_for_each_entry(node, &spec->nid_list, list) {
348 if (node->type != AC_WID_PIN)
349 continue;
350 /* output capable? */
351 if (! (node->pin_caps & AC_PINCAP_OUT))
352 continue;
353 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
354 continue; /* unconnected */
355 if (jack_type >= 0) {
356 if (jack_type != defcfg_type(node))
357 continue;
358 if (node->wid_caps & AC_WCAP_DIGITAL)
359 continue; /* skip SPDIF */
360 } else {
361 /* output as default? */
362 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
363 continue;
365 clear_check_flags(spec);
366 err = parse_output_path(codec, spec, node, 0);
367 if (err < 0)
368 return NULL;
369 if (! err && spec->out_pin_node[0]) {
370 err = parse_output_path(codec, spec, node, 1);
371 if (err < 0)
372 return NULL;
374 if (err > 0) {
375 /* unmute the PIN output */
376 unmute_output(codec, node);
377 /* set PIN-Out enable */
378 snd_hda_codec_write_cache(codec, node->nid, 0,
379 AC_VERB_SET_PIN_WIDGET_CONTROL,
380 AC_PINCTL_OUT_EN |
381 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
382 AC_PINCTL_HP_EN : 0));
383 return node;
386 return NULL;
391 * parse outputs
393 static int parse_output(struct hda_codec *codec)
395 struct hda_gspec *spec = codec->spec;
396 struct hda_gnode *node;
399 * Look for the output PIN widget
401 /* first, look for the line-out pin */
402 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
403 if (node) /* found, remember the PIN node */
404 spec->out_pin_node[0] = node;
405 else {
406 /* if no line-out is found, try speaker out */
407 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
408 if (node)
409 spec->out_pin_node[0] = node;
411 /* look for the HP-out pin */
412 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
413 if (node) {
414 if (! spec->out_pin_node[0])
415 spec->out_pin_node[0] = node;
416 else
417 spec->out_pin_node[1] = node;
420 if (! spec->out_pin_node[0]) {
421 /* no line-out or HP pins found,
422 * then choose for the first output pin
424 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
425 if (! spec->out_pin_node[0])
426 snd_printd("hda_generic: no proper output path found\n");
429 return 0;
433 * input MUX
436 /* control callbacks */
437 static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
439 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
440 struct hda_gspec *spec = codec->spec;
441 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
444 static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
446 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
447 struct hda_gspec *spec = codec->spec;
449 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
450 return 0;
453 static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
455 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
456 struct hda_gspec *spec = codec->spec;
457 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
458 spec->adc_node->nid, &spec->cur_cap_src);
462 * return the string name of the given input PIN widget
464 static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
466 unsigned int location = defcfg_location(node);
467 switch (defcfg_type(node)) {
468 case AC_JACK_LINE_IN:
469 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
470 return "Front Line";
471 return "Line";
472 case AC_JACK_CD:
473 #if 0
474 if (pinctl)
475 *pinctl |= AC_PINCTL_VREF_GRD;
476 #endif
477 return "CD";
478 case AC_JACK_AUX:
479 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
480 return "Front Aux";
481 return "Aux";
482 case AC_JACK_MIC_IN:
483 if (pinctl &&
484 (node->pin_caps &
485 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
486 *pinctl |= AC_PINCTL_VREF_80;
487 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
488 return "Front Mic";
489 return "Mic";
490 case AC_JACK_SPDIF_IN:
491 return "SPDIF";
492 case AC_JACK_DIG_OTHER_IN:
493 return "Digital";
495 return NULL;
499 * parse the nodes recursively until reach to the input PIN
501 * returns 0 if not found, 1 if found, or a negative error code.
503 static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
504 struct hda_gnode *node)
506 int i, err;
507 unsigned int pinctl;
508 char *label;
509 const char *type;
511 if (node->checked)
512 return 0;
514 node->checked = 1;
515 if (node->type != AC_WID_PIN) {
516 for (i = 0; i < node->nconns; i++) {
517 struct hda_gnode *child;
518 child = hda_get_node(spec, node->conn_list[i]);
519 if (! child)
520 continue;
521 err = parse_adc_sub_nodes(codec, spec, child);
522 if (err < 0)
523 return err;
524 if (err > 0) {
525 /* found one,
526 * select the path, unmute both input and output
528 if (node->nconns > 1)
529 select_input_connection(codec, node, i);
530 unmute_input(codec, node, i);
531 unmute_output(codec, node);
532 return err;
535 return 0;
538 /* input capable? */
539 if (! (node->pin_caps & AC_PINCAP_IN))
540 return 0;
542 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
543 return 0; /* unconnected */
545 if (node->wid_caps & AC_WCAP_DIGITAL)
546 return 0; /* skip SPDIF */
548 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
549 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
550 return -EINVAL;
553 pinctl = AC_PINCTL_IN_EN;
554 /* create a proper capture source label */
555 type = get_input_type(node, &pinctl);
556 if (! type) {
557 /* input as default? */
558 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
559 return 0;
560 type = "Input";
562 label = spec->cap_labels[spec->input_mux.num_items];
563 strcpy(label, type);
564 spec->input_mux.items[spec->input_mux.num_items].label = label;
566 /* unmute the PIN external input */
567 unmute_input(codec, node, 0); /* index = 0? */
568 /* set PIN-In enable */
569 snd_hda_codec_write_cache(codec, node->nid, 0,
570 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
572 return 1; /* found */
575 /* add a capture source element */
576 static void add_cap_src(struct hda_gspec *spec, int idx)
578 struct hda_input_mux_item *csrc;
579 char *buf;
580 int num, ocap;
582 num = spec->input_mux.num_items;
583 csrc = &spec->input_mux.items[num];
584 buf = spec->cap_labels[num];
585 for (ocap = 0; ocap < num; ocap++) {
586 if (! strcmp(buf, spec->cap_labels[ocap])) {
587 /* same label already exists,
588 * put the index number to be unique
590 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
591 break;
594 csrc->index = idx;
595 spec->input_mux.num_items++;
599 * parse input
601 static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
603 struct hda_gspec *spec = codec->spec;
604 struct hda_gnode *node;
605 int i, err;
607 snd_printdd("AUD_IN = %x\n", adc_node->nid);
608 clear_check_flags(spec);
610 // awk added - fixed no recording due to muted widget
611 unmute_input(codec, adc_node, 0);
614 * check each connection of the ADC
615 * if it reaches to a proper input PIN, add the path as the
616 * input path.
618 /* first, check the direct connections to PIN widgets */
619 for (i = 0; i < adc_node->nconns; i++) {
620 node = hda_get_node(spec, adc_node->conn_list[i]);
621 if (node && node->type == AC_WID_PIN) {
622 err = parse_adc_sub_nodes(codec, spec, node);
623 if (err < 0)
624 return err;
625 else if (err > 0)
626 add_cap_src(spec, i);
629 /* ... then check the rests, more complicated connections */
630 for (i = 0; i < adc_node->nconns; i++) {
631 node = hda_get_node(spec, adc_node->conn_list[i]);
632 if (node && node->type != AC_WID_PIN) {
633 err = parse_adc_sub_nodes(codec, spec, node);
634 if (err < 0)
635 return err;
636 else if (err > 0)
637 add_cap_src(spec, i);
641 if (! spec->input_mux.num_items)
642 return 0; /* no input path found... */
644 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
645 for (i = 0; i < spec->input_mux.num_items; i++)
646 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
647 spec->input_mux.items[i].index);
649 spec->adc_node = adc_node;
650 return 1;
654 * parse input
656 static int parse_input(struct hda_codec *codec)
658 struct hda_gspec *spec = codec->spec;
659 struct hda_gnode *node;
660 int err;
663 * At first we look for an audio input widget.
664 * If it reaches to certain input PINs, we take it as the
665 * input path.
667 list_for_each_entry(node, &spec->nid_list, list) {
668 if (node->wid_caps & AC_WCAP_DIGITAL)
669 continue; /* skip SPDIF */
670 if (node->type == AC_WID_AUD_IN) {
671 err = parse_input_path(codec, node);
672 if (err < 0)
673 return err;
674 else if (err > 0)
675 return 0;
678 snd_printd("hda_generic: no proper input path found\n");
679 return 0;
682 #ifdef CONFIG_SND_HDA_POWER_SAVE
683 static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
684 int dir, int idx)
686 struct hda_gspec *spec = codec->spec;
687 struct hda_amp_list *p;
689 if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
690 snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
691 return;
693 p = &spec->loopback_list[spec->num_loopbacks++];
694 p->nid = nid;
695 p->dir = dir;
696 p->idx = idx;
697 spec->loopback.amplist = spec->loopback_list;
699 #else
700 #define add_input_loopback(codec,nid,dir,idx)
701 #endif
704 * create mixer controls if possible
706 static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
707 unsigned int index, const char *type,
708 const char *dir_sfx, int is_loopback)
710 char name[32];
711 int err;
712 int created = 0;
713 struct snd_kcontrol_new knew;
715 if (type)
716 sprintf(name, "%s %s Switch", type, dir_sfx);
717 else
718 sprintf(name, "%s Switch", dir_sfx);
719 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
720 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
721 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
722 if (is_loopback)
723 add_input_loopback(codec, node->nid, HDA_INPUT, index);
724 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
725 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
726 return err;
727 created = 1;
728 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
729 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
730 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
731 if (is_loopback)
732 add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
733 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
734 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
735 return err;
736 created = 1;
739 if (type)
740 sprintf(name, "%s %s Volume", type, dir_sfx);
741 else
742 sprintf(name, "%s Volume", dir_sfx);
743 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
744 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
745 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
746 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
747 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
748 return err;
749 created = 1;
750 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
751 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
752 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
753 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
754 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
755 return err;
756 created = 1;
759 return created;
763 * check whether the controls with the given name and direction suffix already exist
765 static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
767 struct snd_ctl_elem_id id;
768 memset(&id, 0, sizeof(id));
769 sprintf(id.name, "%s %s Volume", type, dir);
770 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
771 if (snd_ctl_find_id(codec->bus->card, &id))
772 return 1;
773 sprintf(id.name, "%s %s Switch", type, dir);
774 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
775 if (snd_ctl_find_id(codec->bus->card, &id))
776 return 1;
777 return 0;
781 * build output mixer controls
783 static int create_output_mixers(struct hda_codec *codec, const char **names)
785 struct hda_gspec *spec = codec->spec;
786 int i, err;
788 for (i = 0; i < spec->pcm_vol_nodes; i++) {
789 err = create_mixer(codec, spec->pcm_vol[i].node,
790 spec->pcm_vol[i].index,
791 names[i], "Playback", 0);
792 if (err < 0)
793 return err;
795 return 0;
798 static int build_output_controls(struct hda_codec *codec)
800 struct hda_gspec *spec = codec->spec;
801 static const char *types_speaker[] = { "Speaker", "Headphone" };
802 static const char *types_line[] = { "Front", "Headphone" };
804 switch (spec->pcm_vol_nodes) {
805 case 1:
806 return create_mixer(codec, spec->pcm_vol[0].node,
807 spec->pcm_vol[0].index,
808 "Master", "Playback", 0);
809 case 2:
810 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
811 return create_output_mixers(codec, types_speaker);
812 else
813 return create_output_mixers(codec, types_line);
815 return 0;
818 /* create capture volume/switch */
819 static int build_input_controls(struct hda_codec *codec)
821 struct hda_gspec *spec = codec->spec;
822 struct hda_gnode *adc_node = spec->adc_node;
823 int i, err;
824 static struct snd_kcontrol_new cap_sel = {
825 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
826 .name = "Capture Source",
827 .info = capture_source_info,
828 .get = capture_source_get,
829 .put = capture_source_put,
832 if (! adc_node || ! spec->input_mux.num_items)
833 return 0; /* not found */
835 spec->cur_cap_src = 0;
836 select_input_connection(codec, adc_node,
837 spec->input_mux.items[0].index);
839 /* create capture volume and switch controls if the ADC has an amp */
840 /* do we have only a single item? */
841 if (spec->input_mux.num_items == 1) {
842 err = create_mixer(codec, adc_node,
843 spec->input_mux.items[0].index,
844 NULL, "Capture", 0);
845 if (err < 0)
846 return err;
847 return 0;
850 /* create input MUX if multiple sources are available */
851 if ((err = snd_ctl_add(codec->bus->card,
852 snd_ctl_new1(&cap_sel, codec))) < 0)
853 return err;
855 /* no volume control? */
856 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
857 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
858 return 0;
860 for (i = 0; i < spec->input_mux.num_items; i++) {
861 struct snd_kcontrol_new knew;
862 char name[32];
863 sprintf(name, "%s Capture Volume",
864 spec->input_mux.items[i].label);
865 knew = (struct snd_kcontrol_new)
866 HDA_CODEC_VOLUME(name, adc_node->nid,
867 spec->input_mux.items[i].index,
868 HDA_INPUT);
869 if ((err = snd_ctl_add(codec->bus->card,
870 snd_ctl_new1(&knew, codec))) < 0)
871 return err;
874 return 0;
879 * parse the nodes recursively until reach to the output PIN.
881 * returns 0 - if not found,
882 * 1 - if found, but no mixer is created
883 * 2 - if found and mixer was already created, (just skip)
884 * a negative error code
886 static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
887 struct hda_gnode *node, struct hda_gnode *dest_node,
888 const char *type)
890 int i, err;
892 if (node->checked)
893 return 0;
895 node->checked = 1;
896 if (node == dest_node) {
897 /* loopback connection found */
898 return 1;
901 for (i = 0; i < node->nconns; i++) {
902 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
903 if (! child)
904 continue;
905 err = parse_loopback_path(codec, spec, child, dest_node, type);
906 if (err < 0)
907 return err;
908 else if (err >= 1) {
909 if (err == 1) {
910 err = create_mixer(codec, node, i, type,
911 "Playback", 1);
912 if (err < 0)
913 return err;
914 if (err > 0)
915 return 2; /* ok, created */
916 /* not created, maybe in the lower path */
917 err = 1;
919 /* connect and unmute */
920 if (node->nconns > 1)
921 select_input_connection(codec, node, i);
922 unmute_input(codec, node, i);
923 unmute_output(codec, node);
924 return err;
927 return 0;
931 * parse the tree and build the loopback controls
933 static int build_loopback_controls(struct hda_codec *codec)
935 struct hda_gspec *spec = codec->spec;
936 struct hda_gnode *node;
937 int err;
938 const char *type;
940 if (! spec->out_pin_node[0])
941 return 0;
943 list_for_each_entry(node, &spec->nid_list, list) {
944 if (node->type != AC_WID_PIN)
945 continue;
946 /* input capable? */
947 if (! (node->pin_caps & AC_PINCAP_IN))
948 return 0;
949 type = get_input_type(node, NULL);
950 if (type) {
951 if (check_existing_control(codec, type, "Playback"))
952 continue;
953 clear_check_flags(spec);
954 err = parse_loopback_path(codec, spec,
955 spec->out_pin_node[0],
956 node, type);
957 if (err < 0)
958 return err;
959 if (! err)
960 continue;
963 return 0;
967 * build mixer controls
969 static int build_generic_controls(struct hda_codec *codec)
971 int err;
973 if ((err = build_input_controls(codec)) < 0 ||
974 (err = build_output_controls(codec)) < 0 ||
975 (err = build_loopback_controls(codec)) < 0)
976 return err;
978 return 0;
982 * PCM
984 static struct hda_pcm_stream generic_pcm_playback = {
985 .substreams = 1,
986 .channels_min = 2,
987 .channels_max = 2,
990 static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
991 struct hda_codec *codec,
992 unsigned int stream_tag,
993 unsigned int format,
994 struct snd_pcm_substream *substream)
996 struct hda_gspec *spec = codec->spec;
998 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
999 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
1000 stream_tag, 0, format);
1001 return 0;
1004 static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
1005 struct hda_codec *codec,
1006 struct snd_pcm_substream *substream)
1008 struct hda_gspec *spec = codec->spec;
1010 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1011 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
1012 return 0;
1015 static int build_generic_pcms(struct hda_codec *codec)
1017 struct hda_gspec *spec = codec->spec;
1018 struct hda_pcm *info = &spec->pcm_rec;
1020 if (! spec->dac_node[0] && ! spec->adc_node) {
1021 snd_printd("hda_generic: no PCM found\n");
1022 return 0;
1025 codec->num_pcms = 1;
1026 codec->pcm_info = info;
1028 info->name = "HDA Generic";
1029 if (spec->dac_node[0]) {
1030 info->stream[0] = generic_pcm_playback;
1031 info->stream[0].nid = spec->dac_node[0]->nid;
1032 if (spec->dac_node[1]) {
1033 info->stream[0].ops.prepare = generic_pcm2_prepare;
1034 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1037 if (spec->adc_node) {
1038 info->stream[1] = generic_pcm_playback;
1039 info->stream[1].nid = spec->adc_node->nid;
1042 return 0;
1045 #ifdef CONFIG_SND_HDA_POWER_SAVE
1046 static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1048 struct hda_gspec *spec = codec->spec;
1049 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1051 #endif
1056 static struct hda_codec_ops generic_patch_ops = {
1057 .build_controls = build_generic_controls,
1058 .build_pcms = build_generic_pcms,
1059 .free = snd_hda_generic_free,
1060 #ifdef CONFIG_SND_HDA_POWER_SAVE
1061 .check_power_status = generic_check_power_status,
1062 #endif
1066 * the generic parser
1068 int snd_hda_parse_generic_codec(struct hda_codec *codec)
1070 struct hda_gspec *spec;
1071 int err;
1073 if(!codec->afg)
1074 return 0;
1076 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1077 if (spec == NULL) {
1078 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1079 return -ENOMEM;
1081 codec->spec = spec;
1082 INIT_LIST_HEAD(&spec->nid_list);
1084 if ((err = build_afg_tree(codec)) < 0)
1085 goto error;
1087 if ((err = parse_input(codec)) < 0 ||
1088 (err = parse_output(codec)) < 0)
1089 goto error;
1091 codec->patch_ops = generic_patch_ops;
1093 return 0;
1095 error:
1096 snd_hda_generic_free(codec);
1097 return err;