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