[PATCH] Cleanup: Add zone pointer to get_page_from_freelist
[linux-2.6/linux-mips.git] / sound / pci / hda / hda_generic.c
blob97e9af130b7109fcee9ac0933d360d82a667ea0f
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 <sound/driver.h>
24 #include <linux/init.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include "hda_local.h"
31 /* widget node for parsing */
32 struct hda_gnode {
33 hda_nid_t nid; /* NID of this widget */
34 unsigned short nconns; /* number of input connections */
35 hda_nid_t *conn_list;
36 hda_nid_t slist[2]; /* temporay list */
37 unsigned int wid_caps; /* widget capabilities */
38 unsigned char type; /* widget type */
39 unsigned char pin_ctl; /* pin controls */
40 unsigned char checked; /* the flag indicates that the node is already parsed */
41 unsigned int pin_caps; /* pin widget capabilities */
42 unsigned int def_cfg; /* default configuration */
43 unsigned int amp_out_caps; /* AMP out capabilities */
44 unsigned int amp_in_caps; /* AMP in capabilities */
45 struct list_head list;
48 /* patch-specific record */
50 #define MAX_PCM_VOLS 2
51 struct pcm_vol {
52 struct hda_gnode *node; /* Node for PCM volume */
53 unsigned int index; /* connection of PCM volume */
56 struct hda_gspec {
57 struct hda_gnode *dac_node[2]; /* DAC node */
58 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
59 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
60 unsigned int pcm_vol_nodes; /* number of PCM volumes */
62 struct hda_gnode *adc_node; /* ADC node */
63 struct hda_gnode *cap_vol_node; /* Node for capture volume */
64 unsigned int cur_cap_src; /* current capture source */
65 struct hda_input_mux input_mux;
66 char cap_labels[HDA_MAX_NUM_INPUTS][16];
68 unsigned int def_amp_in_caps;
69 unsigned int def_amp_out_caps;
71 struct hda_pcm pcm_rec; /* PCM information */
73 struct list_head nid_list; /* list of widgets */
77 * retrieve the default device type from the default config value
79 #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
80 AC_DEFCFG_DEVICE_SHIFT)
81 #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
82 AC_DEFCFG_LOCATION_SHIFT)
83 #define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
84 AC_DEFCFG_PORT_CONN_SHIFT)
87 * destructor
89 static void snd_hda_generic_free(struct hda_codec *codec)
91 struct hda_gspec *spec = codec->spec;
92 struct list_head *p, *n;
94 if (! spec)
95 return;
96 /* free all widgets */
97 list_for_each_safe(p, n, &spec->nid_list) {
98 struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
99 if (node->conn_list != node->slist)
100 kfree(node->conn_list);
101 kfree(node);
103 kfree(spec);
108 * add a new widget node and read its attributes
110 static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
112 struct hda_gnode *node;
113 int nconns;
114 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
116 node = kzalloc(sizeof(*node), GFP_KERNEL);
117 if (node == NULL)
118 return -ENOMEM;
119 node->nid = nid;
120 nconns = snd_hda_get_connections(codec, nid, conn_list,
121 HDA_MAX_CONNECTIONS);
122 if (nconns < 0) {
123 kfree(node);
124 return nconns;
126 if (nconns <= ARRAY_SIZE(node->slist))
127 node->conn_list = node->slist;
128 else {
129 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
130 GFP_KERNEL);
131 if (! node->conn_list) {
132 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
133 kfree(node);
134 return -ENOMEM;
137 memcpy(node->conn_list, conn_list, nconns);
138 node->nconns = nconns;
139 node->wid_caps = get_wcaps(codec, nid);
140 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
142 if (node->type == AC_WID_PIN) {
143 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
144 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
145 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
148 if (node->wid_caps & AC_WCAP_OUT_AMP) {
149 if (node->wid_caps & AC_WCAP_AMP_OVRD)
150 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
151 if (! node->amp_out_caps)
152 node->amp_out_caps = spec->def_amp_out_caps;
154 if (node->wid_caps & AC_WCAP_IN_AMP) {
155 if (node->wid_caps & AC_WCAP_AMP_OVRD)
156 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
157 if (! node->amp_in_caps)
158 node->amp_in_caps = spec->def_amp_in_caps;
160 list_add_tail(&node->list, &spec->nid_list);
161 return 0;
165 * build the AFG subtree
167 static int build_afg_tree(struct hda_codec *codec)
169 struct hda_gspec *spec = codec->spec;
170 int i, nodes, err;
171 hda_nid_t nid;
173 snd_assert(spec, return -EINVAL);
175 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
176 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
178 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
179 if (! nid || nodes < 0) {
180 printk(KERN_ERR "Invalid AFG subtree\n");
181 return -EINVAL;
184 /* parse all nodes belonging to the AFG */
185 for (i = 0; i < nodes; i++, nid++) {
186 if ((err = add_new_node(codec, spec, nid)) < 0)
187 return err;
190 return 0;
195 * look for the node record for the given NID
197 /* FIXME: should avoid the braindead linear search */
198 static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
200 struct list_head *p;
201 struct hda_gnode *node;
203 list_for_each(p, &spec->nid_list) {
204 node = list_entry(p, struct hda_gnode, list);
205 if (node->nid == nid)
206 return node;
208 return NULL;
212 * unmute (and set max vol) the output amplifier
214 static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
216 unsigned int val, ofs;
217 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
218 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
219 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
220 if (val >= ofs)
221 val -= ofs;
222 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
223 val |= AC_AMP_SET_OUTPUT;
224 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
228 * unmute (and set max vol) the input amplifier
230 static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
232 unsigned int val, ofs;
233 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
234 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
235 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
236 if (val >= ofs)
237 val -= ofs;
238 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
239 val |= AC_AMP_SET_INPUT;
240 // awk added - fixed to allow unmuting of indexed amps
241 val |= index << AC_AMP_SET_INDEX_SHIFT;
242 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
246 * select the input connection of the given node.
248 static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
249 unsigned int index)
251 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
252 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_CONNECT_SEL, index);
256 * clear checked flag of each node in the node list
258 static void clear_check_flags(struct hda_gspec *spec)
260 struct list_head *p;
261 struct hda_gnode *node;
263 list_for_each(p, &spec->nid_list) {
264 node = list_entry(p, struct hda_gnode, list);
265 node->checked = 0;
270 * parse the output path recursively until reach to an audio output widget
272 * returns 0 if not found, 1 if found, or a negative error code.
274 static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
275 struct hda_gnode *node, int dac_idx)
277 int i, err;
278 struct hda_gnode *child;
280 if (node->checked)
281 return 0;
283 node->checked = 1;
284 if (node->type == AC_WID_AUD_OUT) {
285 if (node->wid_caps & AC_WCAP_DIGITAL) {
286 snd_printdd("Skip Digital OUT node %x\n", node->nid);
287 return 0;
289 snd_printdd("AUD_OUT found %x\n", node->nid);
290 if (spec->dac_node[dac_idx]) {
291 /* already DAC node is assigned, just unmute & connect */
292 return node == spec->dac_node[dac_idx];
294 spec->dac_node[dac_idx] = node;
295 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
296 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
297 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
298 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
299 spec->pcm_vol_nodes++;
301 return 1; /* found */
304 for (i = 0; i < node->nconns; i++) {
305 child = hda_get_node(spec, node->conn_list[i]);
306 if (! child)
307 continue;
308 err = parse_output_path(codec, spec, child, dac_idx);
309 if (err < 0)
310 return err;
311 else if (err > 0) {
312 /* found one,
313 * select the path, unmute both input and output
315 if (node->nconns > 1)
316 select_input_connection(codec, node, i);
317 unmute_input(codec, node, i);
318 unmute_output(codec, node);
319 if (spec->dac_node[dac_idx] &&
320 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
321 !(spec->dac_node[dac_idx]->wid_caps &
322 AC_WCAP_OUT_AMP)) {
323 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
324 (node->wid_caps & AC_WCAP_OUT_AMP)) {
325 int n = spec->pcm_vol_nodes;
326 spec->pcm_vol[n].node = node;
327 spec->pcm_vol[n].index = i;
328 spec->pcm_vol_nodes++;
331 return 1;
334 return 0;
338 * Look for the output PIN widget with the given jack type
339 * and parse the output path to that PIN.
341 * Returns the PIN node when the path to DAC is established.
343 static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
344 struct hda_gspec *spec,
345 int jack_type)
347 struct list_head *p;
348 struct hda_gnode *node;
349 int err;
351 list_for_each(p, &spec->nid_list) {
352 node = list_entry(p, struct hda_gnode, list);
353 if (node->type != AC_WID_PIN)
354 continue;
355 /* output capable? */
356 if (! (node->pin_caps & AC_PINCAP_OUT))
357 continue;
358 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
359 continue; /* unconnected */
360 if (jack_type >= 0) {
361 if (jack_type != defcfg_type(node))
362 continue;
363 if (node->wid_caps & AC_WCAP_DIGITAL)
364 continue; /* skip SPDIF */
365 } else {
366 /* output as default? */
367 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
368 continue;
370 clear_check_flags(spec);
371 err = parse_output_path(codec, spec, node, 0);
372 if (err < 0)
373 return NULL;
374 if (! err && spec->out_pin_node[0]) {
375 err = parse_output_path(codec, spec, node, 1);
376 if (err < 0)
377 return NULL;
379 if (err > 0) {
380 /* unmute the PIN output */
381 unmute_output(codec, node);
382 /* set PIN-Out enable */
383 snd_hda_codec_write(codec, node->nid, 0,
384 AC_VERB_SET_PIN_WIDGET_CONTROL,
385 AC_PINCTL_OUT_EN |
386 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
387 AC_PINCTL_HP_EN : 0));
388 return node;
391 return NULL;
396 * parse outputs
398 static int parse_output(struct hda_codec *codec)
400 struct hda_gspec *spec = codec->spec;
401 struct hda_gnode *node;
404 * Look for the output PIN widget
406 /* first, look for the line-out pin */
407 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
408 if (node) /* found, remember the PIN node */
409 spec->out_pin_node[0] = node;
410 else {
411 /* if no line-out is found, try speaker out */
412 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
413 if (node)
414 spec->out_pin_node[0] = node;
416 /* look for the HP-out pin */
417 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
418 if (node) {
419 if (! spec->out_pin_node[0])
420 spec->out_pin_node[0] = node;
421 else
422 spec->out_pin_node[1] = node;
425 if (! spec->out_pin_node[0]) {
426 /* no line-out or HP pins found,
427 * then choose for the first output pin
429 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
430 if (! spec->out_pin_node[0])
431 snd_printd("hda_generic: no proper output path found\n");
434 return 0;
438 * input MUX
441 /* control callbacks */
442 static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
444 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
445 struct hda_gspec *spec = codec->spec;
446 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
449 static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
451 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
452 struct hda_gspec *spec = codec->spec;
454 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
455 return 0;
458 static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
460 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
461 struct hda_gspec *spec = codec->spec;
462 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
463 spec->adc_node->nid, &spec->cur_cap_src);
467 * return the string name of the given input PIN widget
469 static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
471 unsigned int location = defcfg_location(node);
472 switch (defcfg_type(node)) {
473 case AC_JACK_LINE_IN:
474 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
475 return "Front Line";
476 return "Line";
477 case AC_JACK_CD:
478 #if 0
479 if (pinctl)
480 *pinctl |= AC_PINCTL_VREF_GRD;
481 #endif
482 return "CD";
483 case AC_JACK_AUX:
484 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
485 return "Front Aux";
486 return "Aux";
487 case AC_JACK_MIC_IN:
488 if (node->pin_caps &
489 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT))
490 *pinctl |= AC_PINCTL_VREF_80;
491 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
492 return "Front Mic";
493 return "Mic";
494 case AC_JACK_SPDIF_IN:
495 return "SPDIF";
496 case AC_JACK_DIG_OTHER_IN:
497 return "Digital";
499 return NULL;
503 * parse the nodes recursively until reach to the input PIN
505 * returns 0 if not found, 1 if found, or a negative error code.
507 static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
508 struct hda_gnode *node)
510 int i, err;
511 unsigned int pinctl;
512 char *label;
513 const char *type;
515 if (node->checked)
516 return 0;
518 node->checked = 1;
519 if (node->type != AC_WID_PIN) {
520 for (i = 0; i < node->nconns; i++) {
521 struct hda_gnode *child;
522 child = hda_get_node(spec, node->conn_list[i]);
523 if (! child)
524 continue;
525 err = parse_adc_sub_nodes(codec, spec, child);
526 if (err < 0)
527 return err;
528 if (err > 0) {
529 /* found one,
530 * select the path, unmute both input and output
532 if (node->nconns > 1)
533 select_input_connection(codec, node, i);
534 unmute_input(codec, node, i);
535 unmute_output(codec, node);
536 return err;
539 return 0;
542 /* input capable? */
543 if (! (node->pin_caps & AC_PINCAP_IN))
544 return 0;
546 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
547 return 0; /* unconnected */
549 if (node->wid_caps & AC_WCAP_DIGITAL)
550 return 0; /* skip SPDIF */
552 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
553 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
554 return -EINVAL;
557 pinctl = AC_PINCTL_IN_EN;
558 /* create a proper capture source label */
559 type = get_input_type(node, &pinctl);
560 if (! type) {
561 /* input as default? */
562 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
563 return 0;
564 type = "Input";
566 label = spec->cap_labels[spec->input_mux.num_items];
567 strcpy(label, type);
568 spec->input_mux.items[spec->input_mux.num_items].label = label;
570 /* unmute the PIN external input */
571 unmute_input(codec, node, 0); /* index = 0? */
572 /* set PIN-In enable */
573 snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
575 return 1; /* found */
578 /* add a capture source element */
579 static void add_cap_src(struct hda_gspec *spec, int idx)
581 struct hda_input_mux_item *csrc;
582 char *buf;
583 int num, ocap;
585 num = spec->input_mux.num_items;
586 csrc = &spec->input_mux.items[num];
587 buf = spec->cap_labels[num];
588 for (ocap = 0; ocap < num; ocap++) {
589 if (! strcmp(buf, spec->cap_labels[ocap])) {
590 /* same label already exists,
591 * put the index number to be unique
593 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
594 break;
597 csrc->index = idx;
598 spec->input_mux.num_items++;
602 * parse input
604 static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
606 struct hda_gspec *spec = codec->spec;
607 struct hda_gnode *node;
608 int i, err;
610 snd_printdd("AUD_IN = %x\n", adc_node->nid);
611 clear_check_flags(spec);
613 // awk added - fixed no recording due to muted widget
614 unmute_input(codec, adc_node, 0);
617 * check each connection of the ADC
618 * if it reaches to a proper input PIN, add the path as the
619 * input path.
621 /* first, check the direct connections to PIN widgets */
622 for (i = 0; i < adc_node->nconns; i++) {
623 node = hda_get_node(spec, adc_node->conn_list[i]);
624 if (node && node->type == AC_WID_PIN) {
625 err = parse_adc_sub_nodes(codec, spec, node);
626 if (err < 0)
627 return err;
628 else if (err > 0)
629 add_cap_src(spec, i);
632 /* ... then check the rests, more complicated connections */
633 for (i = 0; i < adc_node->nconns; i++) {
634 node = hda_get_node(spec, adc_node->conn_list[i]);
635 if (node && node->type != AC_WID_PIN) {
636 err = parse_adc_sub_nodes(codec, spec, node);
637 if (err < 0)
638 return err;
639 else if (err > 0)
640 add_cap_src(spec, i);
644 if (! spec->input_mux.num_items)
645 return 0; /* no input path found... */
647 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
648 for (i = 0; i < spec->input_mux.num_items; i++)
649 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
650 spec->input_mux.items[i].index);
652 spec->adc_node = adc_node;
653 return 1;
657 * parse input
659 static int parse_input(struct hda_codec *codec)
661 struct hda_gspec *spec = codec->spec;
662 struct list_head *p;
663 struct hda_gnode *node;
664 int err;
667 * At first we look for an audio input widget.
668 * If it reaches to certain input PINs, we take it as the
669 * input path.
671 list_for_each(p, &spec->nid_list) {
672 node = list_entry(p, struct hda_gnode, list);
673 if (node->wid_caps & AC_WCAP_DIGITAL)
674 continue; /* skip SPDIF */
675 if (node->type == AC_WID_AUD_IN) {
676 err = parse_input_path(codec, node);
677 if (err < 0)
678 return err;
679 else if (err > 0)
680 return 0;
683 snd_printd("hda_generic: no proper input path found\n");
684 return 0;
688 * create mixer controls if possible
690 static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
691 unsigned int index, const char *type, const char *dir_sfx)
693 char name[32];
694 int err;
695 int created = 0;
696 struct snd_kcontrol_new knew;
698 if (type)
699 sprintf(name, "%s %s Switch", type, dir_sfx);
700 else
701 sprintf(name, "%s Switch", dir_sfx);
702 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
703 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
704 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
705 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
706 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
707 return err;
708 created = 1;
709 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
710 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
711 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
712 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
713 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
714 return err;
715 created = 1;
718 if (type)
719 sprintf(name, "%s %s Volume", type, dir_sfx);
720 else
721 sprintf(name, "%s Volume", dir_sfx);
722 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
723 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
724 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
725 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
726 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
727 return err;
728 created = 1;
729 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
730 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
731 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
732 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
733 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
734 return err;
735 created = 1;
738 return created;
742 * check whether the controls with the given name and direction suffix already exist
744 static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
746 struct snd_ctl_elem_id id;
747 memset(&id, 0, sizeof(id));
748 sprintf(id.name, "%s %s Volume", type, dir);
749 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
750 if (snd_ctl_find_id(codec->bus->card, &id))
751 return 1;
752 sprintf(id.name, "%s %s Switch", type, dir);
753 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
754 if (snd_ctl_find_id(codec->bus->card, &id))
755 return 1;
756 return 0;
760 * build output mixer controls
762 static int create_output_mixers(struct hda_codec *codec, const char **names)
764 struct hda_gspec *spec = codec->spec;
765 int i, err;
767 for (i = 0; i < spec->pcm_vol_nodes; i++) {
768 err = create_mixer(codec, spec->pcm_vol[i].node,
769 spec->pcm_vol[i].index,
770 names[i], "Playback");
771 if (err < 0)
772 return err;
774 return 0;
777 static int build_output_controls(struct hda_codec *codec)
779 struct hda_gspec *spec = codec->spec;
780 static const char *types_speaker[] = { "Speaker", "Headphone" };
781 static const char *types_line[] = { "Front", "Headphone" };
783 switch (spec->pcm_vol_nodes) {
784 case 1:
785 return create_mixer(codec, spec->pcm_vol[0].node,
786 spec->pcm_vol[0].index,
787 "Master", "Playback");
788 case 2:
789 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
790 return create_output_mixers(codec, types_speaker);
791 else
792 return create_output_mixers(codec, types_line);
794 return 0;
797 /* create capture volume/switch */
798 static int build_input_controls(struct hda_codec *codec)
800 struct hda_gspec *spec = codec->spec;
801 struct hda_gnode *adc_node = spec->adc_node;
802 int i, err;
803 static struct snd_kcontrol_new cap_sel = {
804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
805 .name = "Capture Source",
806 .info = capture_source_info,
807 .get = capture_source_get,
808 .put = capture_source_put,
811 if (! adc_node || ! spec->input_mux.num_items)
812 return 0; /* not found */
814 spec->cur_cap_src = 0;
815 select_input_connection(codec, adc_node,
816 spec->input_mux.items[0].index);
818 /* create capture volume and switch controls if the ADC has an amp */
819 /* do we have only a single item? */
820 if (spec->input_mux.num_items == 1) {
821 err = create_mixer(codec, adc_node,
822 spec->input_mux.items[0].index,
823 NULL, "Capture");
824 if (err < 0)
825 return err;
826 return 0;
829 /* create input MUX if multiple sources are available */
830 if ((err = snd_ctl_add(codec->bus->card,
831 snd_ctl_new1(&cap_sel, codec))) < 0)
832 return err;
834 /* no volume control? */
835 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
836 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
837 return 0;
839 for (i = 0; i < spec->input_mux.num_items; i++) {
840 struct snd_kcontrol_new knew;
841 char name[32];
842 sprintf(name, "%s Capture Volume",
843 spec->input_mux.items[i].label);
844 knew = (struct snd_kcontrol_new)
845 HDA_CODEC_VOLUME(name, adc_node->nid,
846 spec->input_mux.items[i].index,
847 HDA_INPUT);
848 if ((err = snd_ctl_add(codec->bus->card,
849 snd_ctl_new1(&knew, codec))) < 0)
850 return err;
853 return 0;
858 * parse the nodes recursively until reach to the output PIN.
860 * returns 0 - if not found,
861 * 1 - if found, but no mixer is created
862 * 2 - if found and mixer was already created, (just skip)
863 * a negative error code
865 static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
866 struct hda_gnode *node, struct hda_gnode *dest_node,
867 const char *type)
869 int i, err;
871 if (node->checked)
872 return 0;
874 node->checked = 1;
875 if (node == dest_node) {
876 /* loopback connection found */
877 return 1;
880 for (i = 0; i < node->nconns; i++) {
881 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
882 if (! child)
883 continue;
884 err = parse_loopback_path(codec, spec, child, dest_node, type);
885 if (err < 0)
886 return err;
887 else if (err >= 1) {
888 if (err == 1) {
889 err = create_mixer(codec, node, i, type, "Playback");
890 if (err < 0)
891 return err;
892 if (err > 0)
893 return 2; /* ok, created */
894 /* not created, maybe in the lower path */
895 err = 1;
897 /* connect and unmute */
898 if (node->nconns > 1)
899 select_input_connection(codec, node, i);
900 unmute_input(codec, node, i);
901 unmute_output(codec, node);
902 return err;
905 return 0;
909 * parse the tree and build the loopback controls
911 static int build_loopback_controls(struct hda_codec *codec)
913 struct hda_gspec *spec = codec->spec;
914 struct list_head *p;
915 struct hda_gnode *node;
916 int err;
917 const char *type;
919 if (! spec->out_pin_node[0])
920 return 0;
922 list_for_each(p, &spec->nid_list) {
923 node = list_entry(p, struct hda_gnode, list);
924 if (node->type != AC_WID_PIN)
925 continue;
926 /* input capable? */
927 if (! (node->pin_caps & AC_PINCAP_IN))
928 return 0;
929 type = get_input_type(node, NULL);
930 if (type) {
931 if (check_existing_control(codec, type, "Playback"))
932 continue;
933 clear_check_flags(spec);
934 err = parse_loopback_path(codec, spec,
935 spec->out_pin_node[0],
936 node, type);
937 if (err < 0)
938 return err;
939 if (! err)
940 continue;
943 return 0;
947 * build mixer controls
949 static int build_generic_controls(struct hda_codec *codec)
951 int err;
953 if ((err = build_input_controls(codec)) < 0 ||
954 (err = build_output_controls(codec)) < 0 ||
955 (err = build_loopback_controls(codec)) < 0)
956 return err;
958 return 0;
962 * PCM
964 static struct hda_pcm_stream generic_pcm_playback = {
965 .substreams = 1,
966 .channels_min = 2,
967 .channels_max = 2,
970 static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
971 struct hda_codec *codec,
972 unsigned int stream_tag,
973 unsigned int format,
974 struct snd_pcm_substream *substream)
976 struct hda_gspec *spec = codec->spec;
978 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
979 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
980 stream_tag, 0, format);
981 return 0;
984 static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
985 struct hda_codec *codec,
986 struct snd_pcm_substream *substream)
988 struct hda_gspec *spec = codec->spec;
990 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
991 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
992 return 0;
995 static int build_generic_pcms(struct hda_codec *codec)
997 struct hda_gspec *spec = codec->spec;
998 struct hda_pcm *info = &spec->pcm_rec;
1000 if (! spec->dac_node[0] && ! spec->adc_node) {
1001 snd_printd("hda_generic: no PCM found\n");
1002 return 0;
1005 codec->num_pcms = 1;
1006 codec->pcm_info = info;
1008 info->name = "HDA Generic";
1009 if (spec->dac_node[0]) {
1010 info->stream[0] = generic_pcm_playback;
1011 info->stream[0].nid = spec->dac_node[0]->nid;
1012 if (spec->dac_node[1]) {
1013 info->stream[0].ops.prepare = generic_pcm2_prepare;
1014 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1017 if (spec->adc_node) {
1018 info->stream[1] = generic_pcm_playback;
1019 info->stream[1].nid = spec->adc_node->nid;
1022 return 0;
1028 static struct hda_codec_ops generic_patch_ops = {
1029 .build_controls = build_generic_controls,
1030 .build_pcms = build_generic_pcms,
1031 .free = snd_hda_generic_free,
1035 * the generic parser
1037 int snd_hda_parse_generic_codec(struct hda_codec *codec)
1039 struct hda_gspec *spec;
1040 int err;
1042 if(!codec->afg)
1043 return 0;
1045 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1046 if (spec == NULL) {
1047 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1048 return -ENOMEM;
1050 codec->spec = spec;
1051 INIT_LIST_HEAD(&spec->nid_list);
1053 if ((err = build_afg_tree(codec)) < 0)
1054 goto error;
1056 if ((err = parse_input(codec)) < 0 ||
1057 (err = parse_output(codec)) < 0)
1058 goto error;
1060 codec->patch_ops = generic_patch_ops;
1062 return 0;
1064 error:
1065 snd_hda_generic_free(codec);
1066 return err;