]> bbs.cooldavid.org Git - net-next-2.6.git/blame - sound/pci/hda/hda_generic.c
ALSA: hda - Improve the input source name labels
[net-next-2.6.git] / sound / pci / hda / hda_generic.c
CommitLineData
1da177e4
LT
1/*
2 * Universal Interface for Intel High Definition Audio Codec
3 *
4 * Generic widget tree parser
5 *
6 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
7 *
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.
12 *
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.
17 *
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
21 */
22
1da177e4
LT
23#include <linux/init.h>
24#include <linux/slab.h>
1da177e4
LT
25#include <sound/core.h>
26#include "hda_codec.h"
27#include "hda_local.h"
28
29/* widget node for parsing */
30struct hda_gnode {
31 hda_nid_t nid; /* NID of this widget */
32 unsigned short nconns; /* number of input connections */
d2569505
TI
33 hda_nid_t *conn_list;
34 hda_nid_t slist[2]; /* temporay list */
1da177e4
LT
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;
44};
45
c3132925 46/* patch-specific record */
a7da6ce5
TI
47
48#define MAX_PCM_VOLS 2
49struct pcm_vol {
50 struct hda_gnode *node; /* Node for PCM volume */
51 unsigned int index; /* connection of PCM volume */
52};
53
1da177e4 54struct hda_gspec {
97ec558a
TI
55 struct hda_gnode *dac_node[2]; /* DAC node */
56 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
a7da6ce5
TI
57 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
58 unsigned int pcm_vol_nodes; /* number of PCM volumes */
1da177e4
LT
59
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;
1da177e4
LT
64
65 unsigned int def_amp_in_caps;
66 unsigned int def_amp_out_caps;
67
68 struct hda_pcm pcm_rec; /* PCM information */
69
70 struct list_head nid_list; /* list of widgets */
cb53c626
TI
71
72#ifdef CONFIG_SND_HDA_POWER_SAVE
73#define MAX_LOOPBACK_AMPS 7
74 struct hda_loopback_check loopback;
75 int num_loopbacks;
76 struct hda_amp_list loopback_list[MAX_LOOPBACK_AMPS + 1];
77#endif
1da177e4
LT
78};
79
80/*
81 * retrieve the default device type from the default config value
82 */
97ec558a
TI
83#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
84 AC_DEFCFG_DEVICE_SHIFT)
85#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
86 AC_DEFCFG_LOCATION_SHIFT)
87#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
88 AC_DEFCFG_PORT_CONN_SHIFT)
1da177e4
LT
89
90/*
91 * destructor
92 */
93static void snd_hda_generic_free(struct hda_codec *codec)
94{
95 struct hda_gspec *spec = codec->spec;
33206e86 96 struct hda_gnode *node, *n;
1da177e4
LT
97
98 if (! spec)
99 return;
100 /* free all widgets */
33206e86 101 list_for_each_entry_safe(node, n, &spec->nid_list, list) {
d2569505
TI
102 if (node->conn_list != node->slist)
103 kfree(node->conn_list);
1da177e4
LT
104 kfree(node);
105 }
106 kfree(spec);
107}
108
109
110/*
111 * add a new widget node and read its attributes
112 */
113static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
114{
115 struct hda_gnode *node;
116 int nconns;
d2569505 117 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
1da177e4 118
e560d8d8 119 node = kzalloc(sizeof(*node), GFP_KERNEL);
1da177e4
LT
120 if (node == NULL)
121 return -ENOMEM;
122 node->nid = nid;
05296048 123 node->wid_caps = get_wcaps(codec, nid);
a22d543a 124 node->type = get_wcaps_type(node->wid_caps);
254da007 125 if (node->wid_caps & AC_WCAP_CONN_LIST) {
05296048
JK
126 nconns = snd_hda_get_connections(codec, nid, conn_list,
127 HDA_MAX_CONNECTIONS);
128 if (nconns < 0) {
129 kfree(node);
130 return nconns;
131 }
132 } else {
133 nconns = 0;
1da177e4 134 }
d2569505
TI
135 if (nconns <= ARRAY_SIZE(node->slist))
136 node->conn_list = node->slist;
137 else {
138 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
139 GFP_KERNEL);
140 if (! node->conn_list) {
141 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
142 kfree(node);
143 return -ENOMEM;
144 }
145 }
0bbed758 146 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
1da177e4 147 node->nconns = nconns;
1da177e4
LT
148
149 if (node->type == AC_WID_PIN) {
1327a32b 150 node->pin_caps = snd_hda_query_pin_caps(codec, node->nid);
1da177e4 151 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
c17a1aba 152 node->def_cfg = snd_hda_codec_get_pincfg(codec, node->nid);
1da177e4
LT
153 }
154
155 if (node->wid_caps & AC_WCAP_OUT_AMP) {
156 if (node->wid_caps & AC_WCAP_AMP_OVRD)
157 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
158 if (! node->amp_out_caps)
159 node->amp_out_caps = spec->def_amp_out_caps;
160 }
161 if (node->wid_caps & AC_WCAP_IN_AMP) {
162 if (node->wid_caps & AC_WCAP_AMP_OVRD)
163 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
164 if (! node->amp_in_caps)
165 node->amp_in_caps = spec->def_amp_in_caps;
166 }
167 list_add_tail(&node->list, &spec->nid_list);
168 return 0;
169}
170
171/*
172 * build the AFG subtree
173 */
174static int build_afg_tree(struct hda_codec *codec)
175{
176 struct hda_gspec *spec = codec->spec;
177 int i, nodes, err;
178 hda_nid_t nid;
179
da3cec35
TI
180 if (snd_BUG_ON(!spec))
181 return -EINVAL;
1da177e4
LT
182
183 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
184 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
185
186 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
187 if (! nid || nodes < 0) {
188 printk(KERN_ERR "Invalid AFG subtree\n");
189 return -EINVAL;
190 }
191
192 /* parse all nodes belonging to the AFG */
193 for (i = 0; i < nodes; i++, nid++) {
194 if ((err = add_new_node(codec, spec, nid)) < 0)
195 return err;
196 }
197
198 return 0;
199}
200
201
202/*
203 * look for the node record for the given NID
204 */
205/* FIXME: should avoid the braindead linear search */
206static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
207{
1da177e4
LT
208 struct hda_gnode *node;
209
33206e86 210 list_for_each_entry(node, &spec->nid_list, list) {
1da177e4
LT
211 if (node->nid == nid)
212 return node;
213 }
214 return NULL;
215}
216
217/*
218 * unmute (and set max vol) the output amplifier
219 */
220static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
221{
222 unsigned int val, ofs;
223 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
224 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
225 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
226 if (val >= ofs)
227 val -= ofs;
47fd830a 228 snd_hda_codec_amp_stereo(codec, node->nid, HDA_OUTPUT, 0, 0xff, val);
82beb8fd 229 return 0;
1da177e4
LT
230}
231
232/*
233 * unmute (and set max vol) the input amplifier
234 */
235static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
236{
237 unsigned int val, ofs;
238 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
239 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
240 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
241 if (val >= ofs)
242 val -= ofs;
47fd830a 243 snd_hda_codec_amp_stereo(codec, node->nid, HDA_INPUT, index, 0xff, val);
82beb8fd 244 return 0;
1da177e4
LT
245}
246
247/*
248 * select the input connection of the given node.
249 */
250static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
251 unsigned int index)
252{
253 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
82beb8fd
TI
254 return snd_hda_codec_write_cache(codec, node->nid, 0,
255 AC_VERB_SET_CONNECT_SEL, index);
1da177e4
LT
256}
257
258/*
259 * clear checked flag of each node in the node list
260 */
261static void clear_check_flags(struct hda_gspec *spec)
262{
1da177e4
LT
263 struct hda_gnode *node;
264
33206e86 265 list_for_each_entry(node, &spec->nid_list, list) {
1da177e4
LT
266 node->checked = 0;
267 }
268}
269
270/*
271 * parse the output path recursively until reach to an audio output widget
272 *
273 * returns 0 if not found, 1 if found, or a negative error code.
274 */
275static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
97ec558a 276 struct hda_gnode *node, int dac_idx)
1da177e4
LT
277{
278 int i, err;
279 struct hda_gnode *child;
280
281 if (node->checked)
282 return 0;
283
284 node->checked = 1;
285 if (node->type == AC_WID_AUD_OUT) {
286 if (node->wid_caps & AC_WCAP_DIGITAL) {
287 snd_printdd("Skip Digital OUT node %x\n", node->nid);
288 return 0;
289 }
290 snd_printdd("AUD_OUT found %x\n", node->nid);
97ec558a 291 if (spec->dac_node[dac_idx]) {
1da177e4 292 /* already DAC node is assigned, just unmute & connect */
97ec558a 293 return node == spec->dac_node[dac_idx];
1da177e4 294 }
97ec558a 295 spec->dac_node[dac_idx] = node;
a7da6ce5
TI
296 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
297 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
298 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
299 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
300 spec->pcm_vol_nodes++;
1da177e4
LT
301 }
302 return 1; /* found */
303 }
304
305 for (i = 0; i < node->nconns; i++) {
306 child = hda_get_node(spec, node->conn_list[i]);
307 if (! child)
308 continue;
97ec558a 309 err = parse_output_path(codec, spec, child, dac_idx);
1da177e4
LT
310 if (err < 0)
311 return err;
312 else if (err > 0) {
313 /* found one,
314 * select the path, unmute both input and output
315 */
316 if (node->nconns > 1)
317 select_input_connection(codec, node, i);
318 unmute_input(codec, node, i);
319 unmute_output(codec, node);
a7da6ce5
TI
320 if (spec->dac_node[dac_idx] &&
321 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
322 !(spec->dac_node[dac_idx]->wid_caps &
323 AC_WCAP_OUT_AMP)) {
324 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
325 (node->wid_caps & AC_WCAP_OUT_AMP)) {
326 int n = spec->pcm_vol_nodes;
327 spec->pcm_vol[n].node = node;
328 spec->pcm_vol[n].index = i;
329 spec->pcm_vol_nodes++;
1da177e4
LT
330 }
331 }
332 return 1;
333 }
334 }
335 return 0;
336}
337
338/*
339 * Look for the output PIN widget with the given jack type
340 * and parse the output path to that PIN.
341 *
342 * Returns the PIN node when the path to DAC is established.
343 */
344static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
345 struct hda_gspec *spec,
346 int jack_type)
347{
1da177e4
LT
348 struct hda_gnode *node;
349 int err;
350
33206e86 351 list_for_each_entry(node, &spec->nid_list, list) {
1da177e4
LT
352 if (node->type != AC_WID_PIN)
353 continue;
354 /* output capable? */
355 if (! (node->pin_caps & AC_PINCAP_OUT))
356 continue;
97ec558a
TI
357 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
358 continue; /* unconnected */
1da177e4 359 if (jack_type >= 0) {
e9edcee0 360 if (jack_type != defcfg_type(node))
1da177e4
LT
361 continue;
362 if (node->wid_caps & AC_WCAP_DIGITAL)
363 continue; /* skip SPDIF */
364 } else {
365 /* output as default? */
366 if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
367 continue;
368 }
369 clear_check_flags(spec);
97ec558a 370 err = parse_output_path(codec, spec, node, 0);
1da177e4
LT
371 if (err < 0)
372 return NULL;
97ec558a
TI
373 if (! err && spec->out_pin_node[0]) {
374 err = parse_output_path(codec, spec, node, 1);
375 if (err < 0)
376 return NULL;
377 }
378 if (err > 0) {
1da177e4
LT
379 /* unmute the PIN output */
380 unmute_output(codec, node);
381 /* set PIN-Out enable */
82beb8fd 382 snd_hda_codec_write_cache(codec, node->nid, 0,
1da177e4 383 AC_VERB_SET_PIN_WIDGET_CONTROL,
a7da6ce5
TI
384 AC_PINCTL_OUT_EN |
385 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
386 AC_PINCTL_HP_EN : 0));
1da177e4
LT
387 return node;
388 }
389 }
390 return NULL;
391}
392
393
394/*
395 * parse outputs
396 */
397static int parse_output(struct hda_codec *codec)
398{
399 struct hda_gspec *spec = codec->spec;
400 struct hda_gnode *node;
401
402 /*
403 * Look for the output PIN widget
404 */
405 /* first, look for the line-out pin */
406 node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
407 if (node) /* found, remember the PIN node */
97ec558a
TI
408 spec->out_pin_node[0] = node;
409 else {
410 /* if no line-out is found, try speaker out */
411 node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
412 if (node)
413 spec->out_pin_node[0] = node;
414 }
1da177e4
LT
415 /* look for the HP-out pin */
416 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
417 if (node) {
97ec558a
TI
418 if (! spec->out_pin_node[0])
419 spec->out_pin_node[0] = node;
420 else
421 spec->out_pin_node[1] = node;
1da177e4
LT
422 }
423
97ec558a 424 if (! spec->out_pin_node[0]) {
1da177e4
LT
425 /* no line-out or HP pins found,
426 * then choose for the first output pin
427 */
97ec558a
TI
428 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
429 if (! spec->out_pin_node[0])
1da177e4
LT
430 snd_printd("hda_generic: no proper output path found\n");
431 }
432
433 return 0;
434}
435
436/*
437 * input MUX
438 */
439
440/* control callbacks */
c8b6bf9b 441static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4
LT
442{
443 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
444 struct hda_gspec *spec = codec->spec;
445 return snd_hda_input_mux_info(&spec->input_mux, uinfo);
446}
447
c8b6bf9b 448static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
449{
450 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
451 struct hda_gspec *spec = codec->spec;
452
453 ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
454 return 0;
455}
456
c8b6bf9b 457static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4
LT
458{
459 struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
460 struct hda_gspec *spec = codec->spec;
461 return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
462 spec->adc_node->nid, &spec->cur_cap_src);
463}
464
465/*
466 * return the string name of the given input PIN widget
467 */
468static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
469{
e9edcee0
TI
470 unsigned int location = defcfg_location(node);
471 switch (defcfg_type(node)) {
1da177e4
LT
472 case AC_JACK_LINE_IN:
473 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
474 return "Front Line";
475 return "Line";
476 case AC_JACK_CD:
071c73ad 477#if 0
1da177e4 478 if (pinctl)
1a12de1e 479 *pinctl |= AC_PINCTL_VREF_GRD;
071c73ad 480#endif
1da177e4
LT
481 return "CD";
482 case AC_JACK_AUX:
483 if ((location & 0x0f) == AC_JACK_LOC_FRONT)
484 return "Front Aux";
485 return "Aux";
486 case AC_JACK_MIC_IN:
6afeb11d
TI
487 if (pinctl &&
488 (node->pin_caps &
489 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
071c73ad 490 *pinctl |= AC_PINCTL_VREF_80;
1da177e4
LT
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";
498 }
499 return NULL;
500}
501
502/*
503 * parse the nodes recursively until reach to the input PIN
504 *
505 * returns 0 if not found, 1 if found, or a negative error code.
506 */
507static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
10a20af7 508 struct hda_gnode *node, int idx)
1da177e4
LT
509{
510 int i, err;
511 unsigned int pinctl;
1da177e4
LT
512 const char *type;
513
514 if (node->checked)
515 return 0;
516
517 node->checked = 1;
518 if (node->type != AC_WID_PIN) {
519 for (i = 0; i < node->nconns; i++) {
520 struct hda_gnode *child;
521 child = hda_get_node(spec, node->conn_list[i]);
522 if (! child)
523 continue;
10a20af7 524 err = parse_adc_sub_nodes(codec, spec, child, idx);
1da177e4
LT
525 if (err < 0)
526 return err;
527 if (err > 0) {
528 /* found one,
529 * select the path, unmute both input and output
530 */
531 if (node->nconns > 1)
532 select_input_connection(codec, node, i);
533 unmute_input(codec, node, i);
534 unmute_output(codec, node);
535 return err;
536 }
537 }
538 return 0;
539 }
540
541 /* input capable? */
542 if (! (node->pin_caps & AC_PINCAP_IN))
543 return 0;
544
97ec558a
TI
545 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
546 return 0; /* unconnected */
547
1da177e4
LT
548 if (node->wid_caps & AC_WCAP_DIGITAL)
549 return 0; /* skip SPDIF */
550
551 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
552 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
553 return -EINVAL;
554 }
555
556 pinctl = AC_PINCTL_IN_EN;
557 /* create a proper capture source label */
558 type = get_input_type(node, &pinctl);
559 if (! type) {
560 /* input as default? */
561 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
562 return 0;
563 type = "Input";
564 }
10a20af7 565 snd_hda_add_imux_item(&spec->input_mux, type, idx, NULL);
1da177e4
LT
566
567 /* unmute the PIN external input */
568 unmute_input(codec, node, 0); /* index = 0? */
569 /* set PIN-In enable */
82beb8fd
TI
570 snd_hda_codec_write_cache(codec, node->nid, 0,
571 AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
1da177e4
LT
572
573 return 1; /* found */
574}
575
576/*
577 * parse input
578 */
579static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
580{
581 struct hda_gspec *spec = codec->spec;
582 struct hda_gnode *node;
583 int i, err;
584
585 snd_printdd("AUD_IN = %x\n", adc_node->nid);
586 clear_check_flags(spec);
587
588 // awk added - fixed no recording due to muted widget
589 unmute_input(codec, adc_node, 0);
590
591 /*
592 * check each connection of the ADC
593 * if it reaches to a proper input PIN, add the path as the
594 * input path.
595 */
071c73ad 596 /* first, check the direct connections to PIN widgets */
1da177e4
LT
597 for (i = 0; i < adc_node->nconns; i++) {
598 node = hda_get_node(spec, adc_node->conn_list[i]);
071c73ad 599 if (node && node->type == AC_WID_PIN) {
10a20af7 600 err = parse_adc_sub_nodes(codec, spec, node, i);
071c73ad
TI
601 if (err < 0)
602 return err;
071c73ad
TI
603 }
604 }
605 /* ... then check the rests, more complicated connections */
606 for (i = 0; i < adc_node->nconns; i++) {
607 node = hda_get_node(spec, adc_node->conn_list[i]);
608 if (node && node->type != AC_WID_PIN) {
10a20af7 609 err = parse_adc_sub_nodes(codec, spec, node, i);
071c73ad
TI
610 if (err < 0)
611 return err;
1da177e4
LT
612 }
613 }
614
615 if (! spec->input_mux.num_items)
616 return 0; /* no input path found... */
617
618 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
619 for (i = 0; i < spec->input_mux.num_items; i++)
620 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
621 spec->input_mux.items[i].index);
622
623 spec->adc_node = adc_node;
624 return 1;
625}
626
627/*
628 * parse input
629 */
630static int parse_input(struct hda_codec *codec)
631{
632 struct hda_gspec *spec = codec->spec;
1da177e4
LT
633 struct hda_gnode *node;
634 int err;
635
636 /*
637 * At first we look for an audio input widget.
638 * If it reaches to certain input PINs, we take it as the
639 * input path.
640 */
33206e86 641 list_for_each_entry(node, &spec->nid_list, list) {
1da177e4
LT
642 if (node->wid_caps & AC_WCAP_DIGITAL)
643 continue; /* skip SPDIF */
644 if (node->type == AC_WID_AUD_IN) {
645 err = parse_input_path(codec, node);
646 if (err < 0)
647 return err;
648 else if (err > 0)
649 return 0;
650 }
651 }
652 snd_printd("hda_generic: no proper input path found\n");
653 return 0;
654}
655
cb53c626
TI
656#ifdef CONFIG_SND_HDA_POWER_SAVE
657static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
658 int dir, int idx)
659{
660 struct hda_gspec *spec = codec->spec;
661 struct hda_amp_list *p;
662
663 if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
664 snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
665 return;
666 }
667 p = &spec->loopback_list[spec->num_loopbacks++];
668 p->nid = nid;
669 p->dir = dir;
670 p->idx = idx;
671 spec->loopback.amplist = spec->loopback_list;
672}
673#else
674#define add_input_loopback(codec,nid,dir,idx)
675#endif
676
1da177e4
LT
677/*
678 * create mixer controls if possible
679 */
1da177e4 680static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
cb53c626
TI
681 unsigned int index, const char *type,
682 const char *dir_sfx, int is_loopback)
1da177e4
LT
683{
684 char name[32];
685 int err;
686 int created = 0;
c8b6bf9b 687 struct snd_kcontrol_new knew;
1da177e4
LT
688
689 if (type)
690 sprintf(name, "%s %s Switch", type, dir_sfx);
691 else
692 sprintf(name, "%s Switch", dir_sfx);
693 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
694 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
c8b6bf9b 695 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
cb53c626
TI
696 if (is_loopback)
697 add_input_loopback(codec, node->nid, HDA_INPUT, index);
1da177e4 698 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
3911a4c1
JK
699 err = snd_hda_ctl_add(codec, node->nid,
700 snd_ctl_new1(&knew, codec));
d13bd412 701 if (err < 0)
1da177e4
LT
702 return err;
703 created = 1;
704 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
705 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
c8b6bf9b 706 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
cb53c626
TI
707 if (is_loopback)
708 add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
1da177e4 709 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
3911a4c1
JK
710 err = snd_hda_ctl_add(codec, node->nid,
711 snd_ctl_new1(&knew, codec));
d13bd412 712 if (err < 0)
1da177e4
LT
713 return err;
714 created = 1;
715 }
716
717 if (type)
718 sprintf(name, "%s %s Volume", type, dir_sfx);
719 else
720 sprintf(name, "%s Volume", dir_sfx);
721 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
722 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
c8b6bf9b 723 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
1da177e4 724 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
3911a4c1
JK
725 err = snd_hda_ctl_add(codec, node->nid,
726 snd_ctl_new1(&knew, codec));
d13bd412 727 if (err < 0)
1da177e4
LT
728 return err;
729 created = 1;
730 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
731 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
c8b6bf9b 732 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
1da177e4 733 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
3911a4c1
JK
734 err = snd_hda_ctl_add(codec, node->nid,
735 snd_ctl_new1(&knew, codec));
d13bd412 736 if (err < 0)
1da177e4
LT
737 return err;
738 created = 1;
739 }
740
741 return created;
742}
743
744/*
745 * check whether the controls with the given name and direction suffix already exist
746 */
747static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
748{
c8b6bf9b 749 struct snd_ctl_elem_id id;
1da177e4
LT
750 memset(&id, 0, sizeof(id));
751 sprintf(id.name, "%s %s Volume", type, dir);
752 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
753 if (snd_ctl_find_id(codec->bus->card, &id))
754 return 1;
755 sprintf(id.name, "%s %s Switch", type, dir);
756 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
757 if (snd_ctl_find_id(codec->bus->card, &id))
758 return 1;
759 return 0;
760}
761
762/*
763 * build output mixer controls
764 */
a7da6ce5 765static int create_output_mixers(struct hda_codec *codec, const char **names)
1da177e4
LT
766{
767 struct hda_gspec *spec = codec->spec;
97ec558a 768 int i, err;
1da177e4 769
a7da6ce5
TI
770 for (i = 0; i < spec->pcm_vol_nodes; i++) {
771 err = create_mixer(codec, spec->pcm_vol[i].node,
772 spec->pcm_vol[i].index,
cb53c626 773 names[i], "Playback", 0);
97ec558a
TI
774 if (err < 0)
775 return err;
776 }
1da177e4
LT
777 return 0;
778}
779
a7da6ce5
TI
780static int build_output_controls(struct hda_codec *codec)
781{
782 struct hda_gspec *spec = codec->spec;
783 static const char *types_speaker[] = { "Speaker", "Headphone" };
784 static const char *types_line[] = { "Front", "Headphone" };
785
786 switch (spec->pcm_vol_nodes) {
787 case 1:
788 return create_mixer(codec, spec->pcm_vol[0].node,
789 spec->pcm_vol[0].index,
cb53c626 790 "Master", "Playback", 0);
a7da6ce5
TI
791 case 2:
792 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
793 return create_output_mixers(codec, types_speaker);
794 else
795 return create_output_mixers(codec, types_line);
796 }
797 return 0;
798}
799
1da177e4
LT
800/* create capture volume/switch */
801static int build_input_controls(struct hda_codec *codec)
802{
803 struct hda_gspec *spec = codec->spec;
804 struct hda_gnode *adc_node = spec->adc_node;
071c73ad
TI
805 int i, err;
806 static struct snd_kcontrol_new cap_sel = {
807 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
808 .name = "Capture Source",
809 .info = capture_source_info,
810 .get = capture_source_get,
811 .put = capture_source_put,
812 };
813
814 if (! adc_node || ! spec->input_mux.num_items)
1da177e4
LT
815 return 0; /* not found */
816
071c73ad
TI
817 spec->cur_cap_src = 0;
818 select_input_connection(codec, adc_node,
819 spec->input_mux.items[0].index);
820
1da177e4 821 /* create capture volume and switch controls if the ADC has an amp */
071c73ad
TI
822 /* do we have only a single item? */
823 if (spec->input_mux.num_items == 1) {
824 err = create_mixer(codec, adc_node,
825 spec->input_mux.items[0].index,
cb53c626 826 NULL, "Capture", 0);
071c73ad
TI
827 if (err < 0)
828 return err;
829 return 0;
830 }
1da177e4
LT
831
832 /* create input MUX if multiple sources are available */
5b0cb1d8
JK
833 err = snd_hda_ctl_add(codec, spec->adc_node->nid,
834 snd_ctl_new1(&cap_sel, codec));
d13bd412 835 if (err < 0)
071c73ad
TI
836 return err;
837
838 /* no volume control? */
839 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
840 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
841 return 0;
842
843 for (i = 0; i < spec->input_mux.num_items; i++) {
844 struct snd_kcontrol_new knew;
845 char name[32];
846 sprintf(name, "%s Capture Volume",
847 spec->input_mux.items[i].label);
848 knew = (struct snd_kcontrol_new)
849 HDA_CODEC_VOLUME(name, adc_node->nid,
850 spec->input_mux.items[i].index,
851 HDA_INPUT);
3911a4c1
JK
852 err = snd_hda_ctl_add(codec, adc_node->nid,
853 snd_ctl_new1(&knew, codec));
d13bd412 854 if (err < 0)
1da177e4 855 return err;
1da177e4 856 }
071c73ad 857
1da177e4
LT
858 return 0;
859}
860
861
862/*
863 * parse the nodes recursively until reach to the output PIN.
864 *
865 * returns 0 - if not found,
866 * 1 - if found, but no mixer is created
867 * 2 - if found and mixer was already created, (just skip)
868 * a negative error code
869 */
870static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
871 struct hda_gnode *node, struct hda_gnode *dest_node,
872 const char *type)
873{
874 int i, err;
875
876 if (node->checked)
877 return 0;
878
879 node->checked = 1;
880 if (node == dest_node) {
881 /* loopback connection found */
882 return 1;
883 }
884
885 for (i = 0; i < node->nconns; i++) {
886 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
887 if (! child)
888 continue;
889 err = parse_loopback_path(codec, spec, child, dest_node, type);
890 if (err < 0)
891 return err;
892 else if (err >= 1) {
893 if (err == 1) {
cb53c626
TI
894 err = create_mixer(codec, node, i, type,
895 "Playback", 1);
1da177e4
LT
896 if (err < 0)
897 return err;
898 if (err > 0)
899 return 2; /* ok, created */
900 /* not created, maybe in the lower path */
901 err = 1;
902 }
903 /* connect and unmute */
904 if (node->nconns > 1)
905 select_input_connection(codec, node, i);
906 unmute_input(codec, node, i);
907 unmute_output(codec, node);
908 return err;
909 }
910 }
911 return 0;
912}
913
914/*
915 * parse the tree and build the loopback controls
916 */
917static int build_loopback_controls(struct hda_codec *codec)
918{
919 struct hda_gspec *spec = codec->spec;
1da177e4
LT
920 struct hda_gnode *node;
921 int err;
922 const char *type;
923
97ec558a 924 if (! spec->out_pin_node[0])
1da177e4
LT
925 return 0;
926
33206e86 927 list_for_each_entry(node, &spec->nid_list, list) {
1da177e4
LT
928 if (node->type != AC_WID_PIN)
929 continue;
930 /* input capable? */
931 if (! (node->pin_caps & AC_PINCAP_IN))
932 return 0;
933 type = get_input_type(node, NULL);
934 if (type) {
935 if (check_existing_control(codec, type, "Playback"))
936 continue;
937 clear_check_flags(spec);
97ec558a
TI
938 err = parse_loopback_path(codec, spec,
939 spec->out_pin_node[0],
1da177e4
LT
940 node, type);
941 if (err < 0)
942 return err;
943 if (! err)
944 continue;
945 }
946 }
947 return 0;
948}
949
950/*
951 * build mixer controls
952 */
953static int build_generic_controls(struct hda_codec *codec)
954{
955 int err;
956
957 if ((err = build_input_controls(codec)) < 0 ||
958 (err = build_output_controls(codec)) < 0 ||
959 (err = build_loopback_controls(codec)) < 0)
960 return err;
961
962 return 0;
963}
964
965/*
966 * PCM
967 */
968static struct hda_pcm_stream generic_pcm_playback = {
969 .substreams = 1,
970 .channels_min = 2,
971 .channels_max = 2,
972};
973
97ec558a
TI
974static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
975 struct hda_codec *codec,
976 unsigned int stream_tag,
977 unsigned int format,
978 struct snd_pcm_substream *substream)
979{
980 struct hda_gspec *spec = codec->spec;
981
982 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
983 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
984 stream_tag, 0, format);
985 return 0;
986}
987
988static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
989 struct hda_codec *codec,
990 struct snd_pcm_substream *substream)
991{
992 struct hda_gspec *spec = codec->spec;
993
888afa15
TI
994 snd_hda_codec_cleanup_stream(codec, hinfo->nid);
995 snd_hda_codec_cleanup_stream(codec, spec->dac_node[1]->nid);
97ec558a
TI
996 return 0;
997}
998
1da177e4
LT
999static int build_generic_pcms(struct hda_codec *codec)
1000{
1001 struct hda_gspec *spec = codec->spec;
1002 struct hda_pcm *info = &spec->pcm_rec;
1003
97ec558a 1004 if (! spec->dac_node[0] && ! spec->adc_node) {
1da177e4
LT
1005 snd_printd("hda_generic: no PCM found\n");
1006 return 0;
1007 }
1008
1009 codec->num_pcms = 1;
1010 codec->pcm_info = info;
1011
1012 info->name = "HDA Generic";
97ec558a 1013 if (spec->dac_node[0]) {
1da177e4 1014 info->stream[0] = generic_pcm_playback;
97ec558a
TI
1015 info->stream[0].nid = spec->dac_node[0]->nid;
1016 if (spec->dac_node[1]) {
1017 info->stream[0].ops.prepare = generic_pcm2_prepare;
1018 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1019 }
1da177e4
LT
1020 }
1021 if (spec->adc_node) {
1022 info->stream[1] = generic_pcm_playback;
1023 info->stream[1].nid = spec->adc_node->nid;
1024 }
1025
1026 return 0;
1027}
1028
cb53c626
TI
1029#ifdef CONFIG_SND_HDA_POWER_SAVE
1030static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1031{
1032 struct hda_gspec *spec = codec->spec;
1033 return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1034}
1035#endif
1036
1da177e4
LT
1037
1038/*
1039 */
1040static struct hda_codec_ops generic_patch_ops = {
1041 .build_controls = build_generic_controls,
1042 .build_pcms = build_generic_pcms,
1043 .free = snd_hda_generic_free,
cb53c626
TI
1044#ifdef CONFIG_SND_HDA_POWER_SAVE
1045 .check_power_status = generic_check_power_status,
1046#endif
1da177e4
LT
1047};
1048
1049/*
1050 * the generic parser
1051 */
1052int snd_hda_parse_generic_codec(struct hda_codec *codec)
1053{
1054 struct hda_gspec *spec;
1055 int err;
1056
84802f0d
SK
1057 if(!codec->afg)
1058 return 0;
673b683a 1059
e560d8d8 1060 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1da177e4
LT
1061 if (spec == NULL) {
1062 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1063 return -ENOMEM;
1064 }
1065 codec->spec = spec;
1066 INIT_LIST_HEAD(&spec->nid_list);
1067
1068 if ((err = build_afg_tree(codec)) < 0)
1069 goto error;
1070
1071 if ((err = parse_input(codec)) < 0 ||
1072 (err = parse_output(codec)) < 0)
1073 goto error;
1074
1075 codec->patch_ops = generic_patch_ops;
1076
1077 return 0;
1078
1079 error:
1080 snd_hda_generic_free(codec);
1081 return err;
1082}
1289e9e8 1083EXPORT_SYMBOL(snd_hda_parse_generic_codec);