• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/sound/pci/hda/
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 <linux/init.h>
24#include <linux/slab.h>
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 */
33	hda_nid_t *conn_list;
34	hda_nid_t slist[2];	/* temporay list */
35	unsigned int wid_caps;	/* widget capabilities */
36	unsigned char type;	/* widget type */
37	unsigned char pin_ctl;	/* pin controls */
38	unsigned char checked;	/* the flag indicates that the node is already parsed */
39	unsigned int pin_caps;	/* pin widget capabilities */
40	unsigned int def_cfg;	/* default configuration */
41	unsigned int amp_out_caps;	/* AMP out capabilities */
42	unsigned int amp_in_caps;	/* AMP in capabilities */
43	struct list_head list;
44};
45
46/* patch-specific record */
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
54struct hda_gspec {
55	struct hda_gnode *dac_node[2];	/* DAC node */
56	struct hda_gnode *out_pin_node[2];	/* Output pin (Line-Out) node */
57	struct pcm_vol pcm_vol[MAX_PCM_VOLS];	/* PCM volumes */
58	unsigned int pcm_vol_nodes;	/* number of PCM volumes */
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;
64	char cap_labels[HDA_MAX_NUM_INPUTS][16];
65
66	unsigned int def_amp_in_caps;
67	unsigned int def_amp_out_caps;
68
69	struct hda_pcm pcm_rec;		/* PCM information */
70
71	struct list_head nid_list;	/* list of widgets */
72
73#ifdef CONFIG_SND_HDA_POWER_SAVE
74#define MAX_LOOPBACK_AMPS	7
75	struct hda_loopback_check loopback;
76	int num_loopbacks;
77	struct hda_amp_list loopback_list[MAX_LOOPBACK_AMPS + 1];
78#endif
79};
80
81/*
82 * retrieve the default device type from the default config value
83 */
84#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
85			   AC_DEFCFG_DEVICE_SHIFT)
86#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
87			       AC_DEFCFG_LOCATION_SHIFT)
88#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
89				AC_DEFCFG_PORT_CONN_SHIFT)
90
91/*
92 * destructor
93 */
94static void snd_hda_generic_free(struct hda_codec *codec)
95{
96	struct hda_gspec *spec = codec->spec;
97	struct hda_gnode *node, *n;
98
99	if (! spec)
100		return;
101	/* free all widgets */
102	list_for_each_entry_safe(node, n, &spec->nid_list, list) {
103		if (node->conn_list != node->slist)
104			kfree(node->conn_list);
105		kfree(node);
106	}
107	kfree(spec);
108}
109
110
111/*
112 * add a new widget node and read its attributes
113 */
114static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
115{
116	struct hda_gnode *node;
117	int nconns;
118	hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
119
120	node = kzalloc(sizeof(*node), GFP_KERNEL);
121	if (node == NULL)
122		return -ENOMEM;
123	node->nid = nid;
124	node->wid_caps = get_wcaps(codec, nid);
125	node->type = get_wcaps_type(node->wid_caps);
126	if (node->wid_caps & AC_WCAP_CONN_LIST) {
127		nconns = snd_hda_get_connections(codec, nid, conn_list,
128						 HDA_MAX_CONNECTIONS);
129		if (nconns < 0) {
130			kfree(node);
131			return nconns;
132		}
133	} else {
134		nconns = 0;
135	}
136	if (nconns <= ARRAY_SIZE(node->slist))
137		node->conn_list = node->slist;
138	else {
139		node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
140					  GFP_KERNEL);
141		if (! node->conn_list) {
142			snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
143			kfree(node);
144			return -ENOMEM;
145		}
146	}
147	memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
148	node->nconns = nconns;
149
150	if (node->type == AC_WID_PIN) {
151		node->pin_caps = snd_hda_query_pin_caps(codec, node->nid);
152		node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
153		node->def_cfg = snd_hda_codec_get_pincfg(codec, node->nid);
154	}
155
156	if (node->wid_caps & AC_WCAP_OUT_AMP) {
157		if (node->wid_caps & AC_WCAP_AMP_OVRD)
158			node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
159		if (! node->amp_out_caps)
160			node->amp_out_caps = spec->def_amp_out_caps;
161	}
162	if (node->wid_caps & AC_WCAP_IN_AMP) {
163		if (node->wid_caps & AC_WCAP_AMP_OVRD)
164			node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
165		if (! node->amp_in_caps)
166			node->amp_in_caps = spec->def_amp_in_caps;
167	}
168	list_add_tail(&node->list, &spec->nid_list);
169	return 0;
170}
171
172/*
173 * build the AFG subtree
174 */
175static int build_afg_tree(struct hda_codec *codec)
176{
177	struct hda_gspec *spec = codec->spec;
178	int i, nodes, err;
179	hda_nid_t nid;
180
181	if (snd_BUG_ON(!spec))
182		return -EINVAL;
183
184	spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
185	spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
186
187	nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
188	if (! nid || nodes < 0) {
189		printk(KERN_ERR "Invalid AFG subtree\n");
190		return -EINVAL;
191	}
192
193	/* parse all nodes belonging to the AFG */
194	for (i = 0; i < nodes; i++, nid++) {
195		if ((err = add_new_node(codec, spec, nid)) < 0)
196			return err;
197	}
198
199	return 0;
200}
201
202
203/*
204 * look for the node record for the given NID
205 */
206static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
207{
208	struct hda_gnode *node;
209
210	list_for_each_entry(node, &spec->nid_list, 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 */
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;
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 */
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;
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 */
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);
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 */
261static void clear_check_flags(struct hda_gspec *spec)
262{
263	struct hda_gnode *node;
264
265	list_for_each_entry(node, &spec->nid_list, list) {
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,
276			     struct hda_gnode *node, int dac_idx)
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);
291		if (spec->dac_node[dac_idx]) {
292			/* already DAC node is assigned, just unmute & connect */
293			return node == spec->dac_node[dac_idx];
294		}
295		spec->dac_node[dac_idx] = node;
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++;
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;
309		err = parse_output_path(codec, spec, child, dac_idx);
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);
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++;
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{
348	struct hda_gnode *node;
349	int err;
350
351	list_for_each_entry(node, &spec->nid_list, list) {
352		if (node->type != AC_WID_PIN)
353			continue;
354		/* output capable? */
355		if (! (node->pin_caps & AC_PINCAP_OUT))
356			continue;
357		if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
358			continue; /* unconnected */
359		if (jack_type >= 0) {
360			if (jack_type != defcfg_type(node))
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);
370		err = parse_output_path(codec, spec, node, 0);
371		if (err < 0)
372			return NULL;
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) {
379			/* unmute the PIN output */
380			unmute_output(codec, node);
381			/* set PIN-Out enable */
382			snd_hda_codec_write_cache(codec, node->nid, 0,
383					    AC_VERB_SET_PIN_WIDGET_CONTROL,
384					    AC_PINCTL_OUT_EN |
385					    ((node->pin_caps & AC_PINCAP_HP_DRV) ?
386					     AC_PINCTL_HP_EN : 0));
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 */
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	}
415	/* look for the HP-out pin */
416	node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
417	if (node) {
418		if (! spec->out_pin_node[0])
419			spec->out_pin_node[0] = node;
420		else
421			spec->out_pin_node[1] = node;
422	}
423
424	if (! spec->out_pin_node[0]) {
425		/* no line-out or HP pins found,
426		 * then choose for the first output pin
427		 */
428		spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
429		if (! spec->out_pin_node[0])
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 */
441static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
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
448static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
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
457static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
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{
470	unsigned int location = defcfg_location(node);
471	switch (defcfg_type(node)) {
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:
477		return "CD";
478	case AC_JACK_AUX:
479		if ((location & 0x0f) == AC_JACK_LOC_FRONT)
480			return "Front Aux";
481		return "Aux";
482	case AC_JACK_MIC_IN:
483		if (pinctl &&
484		    (node->pin_caps &
485		     (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
486			*pinctl |= AC_PINCTL_VREF_80;
487		if ((location & 0x0f) == AC_JACK_LOC_FRONT)
488			return "Front Mic";
489		return "Mic";
490	case AC_JACK_SPDIF_IN:
491		return "SPDIF";
492	case AC_JACK_DIG_OTHER_IN:
493		return "Digital";
494	}
495	return NULL;
496}
497
498/*
499 * parse the nodes recursively until reach to the input PIN
500 *
501 * returns 0 if not found, 1 if found, or a negative error code.
502 */
503static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
504			       struct hda_gnode *node)
505{
506	int i, err;
507	unsigned int pinctl;
508	char *label;
509	const char *type;
510
511	if (node->checked)
512		return 0;
513
514	node->checked = 1;
515	if (node->type != AC_WID_PIN) {
516		for (i = 0; i < node->nconns; i++) {
517			struct hda_gnode *child;
518			child = hda_get_node(spec, node->conn_list[i]);
519			if (! child)
520				continue;
521			err = parse_adc_sub_nodes(codec, spec, child);
522			if (err < 0)
523				return err;
524			if (err > 0) {
525				/* found one,
526				 * select the path, unmute both input and output
527				 */
528				if (node->nconns > 1)
529					select_input_connection(codec, node, i);
530				unmute_input(codec, node, i);
531				unmute_output(codec, node);
532				return err;
533			}
534		}
535		return 0;
536	}
537
538	/* input capable? */
539	if (! (node->pin_caps & AC_PINCAP_IN))
540		return 0;
541
542	if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
543		return 0; /* unconnected */
544
545	if (node->wid_caps & AC_WCAP_DIGITAL)
546		return 0; /* skip SPDIF */
547
548	if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
549		snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
550		return -EINVAL;
551	}
552
553	pinctl = AC_PINCTL_IN_EN;
554	/* create a proper capture source label */
555	type = get_input_type(node, &pinctl);
556	if (! type) {
557		/* input as default? */
558		if (! (node->pin_ctl & AC_PINCTL_IN_EN))
559			return 0;
560		type = "Input";
561	}
562	label = spec->cap_labels[spec->input_mux.num_items];
563	strcpy(label, type);
564	spec->input_mux.items[spec->input_mux.num_items].label = label;
565
566	/* unmute the PIN external input */
567	unmute_input(codec, node, 0); /* index = 0? */
568	/* set PIN-In enable */
569	snd_hda_codec_write_cache(codec, node->nid, 0,
570				  AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
571
572	return 1; /* found */
573}
574
575/* add a capture source element */
576static void add_cap_src(struct hda_gspec *spec, int idx)
577{
578	struct hda_input_mux_item *csrc;
579	char *buf;
580	int num, ocap;
581
582	num = spec->input_mux.num_items;
583	csrc = &spec->input_mux.items[num];
584	buf = spec->cap_labels[num];
585	for (ocap = 0; ocap < num; ocap++) {
586		if (! strcmp(buf, spec->cap_labels[ocap])) {
587			/* same label already exists,
588			 * put the index number to be unique
589			 */
590			sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
591			break;
592		}
593	}
594	csrc->index = idx;
595	spec->input_mux.num_items++;
596}
597
598/*
599 * parse input
600 */
601static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
602{
603	struct hda_gspec *spec = codec->spec;
604	struct hda_gnode *node;
605	int i, err;
606
607	snd_printdd("AUD_IN = %x\n", adc_node->nid);
608	clear_check_flags(spec);
609
610	// awk added - fixed no recording due to muted widget
611	unmute_input(codec, adc_node, 0);
612
613	/*
614	 * check each connection of the ADC
615	 * if it reaches to a proper input PIN, add the path as the
616	 * input path.
617	 */
618	/* first, check the direct connections to PIN widgets */
619	for (i = 0; i < adc_node->nconns; i++) {
620		node = hda_get_node(spec, adc_node->conn_list[i]);
621		if (node && node->type == AC_WID_PIN) {
622			err = parse_adc_sub_nodes(codec, spec, node);
623			if (err < 0)
624				return err;
625			else if (err > 0)
626				add_cap_src(spec, i);
627		}
628	}
629	/* ... then check the rests, more complicated connections */
630	for (i = 0; i < adc_node->nconns; i++) {
631		node = hda_get_node(spec, adc_node->conn_list[i]);
632		if (node && node->type != AC_WID_PIN) {
633			err = parse_adc_sub_nodes(codec, spec, node);
634			if (err < 0)
635				return err;
636			else if (err > 0)
637				add_cap_src(spec, i);
638		}
639	}
640
641	if (! spec->input_mux.num_items)
642		return 0; /* no input path found... */
643
644	snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
645	for (i = 0; i < spec->input_mux.num_items; i++)
646		snd_printdd("  [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
647			    spec->input_mux.items[i].index);
648
649	spec->adc_node = adc_node;
650	return 1;
651}
652
653/*
654 * parse input
655 */
656static int parse_input(struct hda_codec *codec)
657{
658	struct hda_gspec *spec = codec->spec;
659	struct hda_gnode *node;
660	int err;
661
662	/*
663	 * At first we look for an audio input widget.
664	 * If it reaches to certain input PINs, we take it as the
665	 * input path.
666	 */
667	list_for_each_entry(node, &spec->nid_list, list) {
668		if (node->wid_caps & AC_WCAP_DIGITAL)
669			continue; /* skip SPDIF */
670		if (node->type == AC_WID_AUD_IN) {
671			err = parse_input_path(codec, node);
672			if (err < 0)
673				return err;
674			else if (err > 0)
675				return 0;
676		}
677	}
678	snd_printd("hda_generic: no proper input path found\n");
679	return 0;
680}
681
682#ifdef CONFIG_SND_HDA_POWER_SAVE
683static void add_input_loopback(struct hda_codec *codec, hda_nid_t nid,
684			       int dir, int idx)
685{
686	struct hda_gspec *spec = codec->spec;
687	struct hda_amp_list *p;
688
689	if (spec->num_loopbacks >= MAX_LOOPBACK_AMPS) {
690		snd_printk(KERN_ERR "hda_generic: Too many loopback ctls\n");
691		return;
692	}
693	p = &spec->loopback_list[spec->num_loopbacks++];
694	p->nid = nid;
695	p->dir = dir;
696	p->idx = idx;
697	spec->loopback.amplist = spec->loopback_list;
698}
699#else
700#define add_input_loopback(codec,nid,dir,idx)
701#endif
702
703/*
704 * create mixer controls if possible
705 */
706static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
707			unsigned int index, const char *type,
708			const char *dir_sfx, int is_loopback)
709{
710	char name[32];
711	int err;
712	int created = 0;
713	struct snd_kcontrol_new knew;
714
715	if (type)
716		sprintf(name, "%s %s Switch", type, dir_sfx);
717	else
718		sprintf(name, "%s Switch", dir_sfx);
719	if ((node->wid_caps & AC_WCAP_IN_AMP) &&
720	    (node->amp_in_caps & AC_AMPCAP_MUTE)) {
721		knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
722		if (is_loopback)
723			add_input_loopback(codec, node->nid, HDA_INPUT, index);
724		snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
725		err = snd_hda_ctl_add(codec, node->nid,
726					snd_ctl_new1(&knew, codec));
727		if (err < 0)
728			return err;
729		created = 1;
730	} else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
731		   (node->amp_out_caps & AC_AMPCAP_MUTE)) {
732		knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
733		if (is_loopback)
734			add_input_loopback(codec, node->nid, HDA_OUTPUT, 0);
735		snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
736		err = snd_hda_ctl_add(codec, node->nid,
737					snd_ctl_new1(&knew, codec));
738		if (err < 0)
739			return err;
740		created = 1;
741	}
742
743	if (type)
744		sprintf(name, "%s %s Volume", type, dir_sfx);
745	else
746		sprintf(name, "%s Volume", dir_sfx);
747	if ((node->wid_caps & AC_WCAP_IN_AMP) &&
748	    (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
749		knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
750		snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
751		err = snd_hda_ctl_add(codec, node->nid,
752					snd_ctl_new1(&knew, codec));
753		if (err < 0)
754			return err;
755		created = 1;
756	} else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
757		   (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
758		knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
759		snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
760		err = snd_hda_ctl_add(codec, node->nid,
761					snd_ctl_new1(&knew, codec));
762		if (err < 0)
763			return err;
764		created = 1;
765	}
766
767	return created;
768}
769
770/*
771 * check whether the controls with the given name and direction suffix already exist
772 */
773static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
774{
775	struct snd_ctl_elem_id id;
776	memset(&id, 0, sizeof(id));
777	sprintf(id.name, "%s %s Volume", type, dir);
778	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
779	if (snd_ctl_find_id(codec->bus->card, &id))
780		return 1;
781	sprintf(id.name, "%s %s Switch", type, dir);
782	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
783	if (snd_ctl_find_id(codec->bus->card, &id))
784		return 1;
785	return 0;
786}
787
788/*
789 * build output mixer controls
790 */
791static int create_output_mixers(struct hda_codec *codec, const char **names)
792{
793	struct hda_gspec *spec = codec->spec;
794	int i, err;
795
796	for (i = 0; i < spec->pcm_vol_nodes; i++) {
797		err = create_mixer(codec, spec->pcm_vol[i].node,
798				   spec->pcm_vol[i].index,
799				   names[i], "Playback", 0);
800		if (err < 0)
801			return err;
802	}
803	return 0;
804}
805
806static int build_output_controls(struct hda_codec *codec)
807{
808	struct hda_gspec *spec = codec->spec;
809	static const char *types_speaker[] = { "Speaker", "Headphone" };
810	static const char *types_line[] = { "Front", "Headphone" };
811
812	switch (spec->pcm_vol_nodes) {
813	case 1:
814		return create_mixer(codec, spec->pcm_vol[0].node,
815				    spec->pcm_vol[0].index,
816				    "Master", "Playback", 0);
817	case 2:
818		if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
819			return create_output_mixers(codec, types_speaker);
820		else
821			return create_output_mixers(codec, types_line);
822	}
823	return 0;
824}
825
826/* create capture volume/switch */
827static int build_input_controls(struct hda_codec *codec)
828{
829	struct hda_gspec *spec = codec->spec;
830	struct hda_gnode *adc_node = spec->adc_node;
831	int i, err;
832	static struct snd_kcontrol_new cap_sel = {
833		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
834		.name = "Capture Source",
835		.info = capture_source_info,
836		.get = capture_source_get,
837		.put = capture_source_put,
838	};
839
840	if (! adc_node || ! spec->input_mux.num_items)
841		return 0; /* not found */
842
843	spec->cur_cap_src = 0;
844	select_input_connection(codec, adc_node,
845				spec->input_mux.items[0].index);
846
847	/* create capture volume and switch controls if the ADC has an amp */
848	/* do we have only a single item? */
849	if (spec->input_mux.num_items == 1) {
850		err = create_mixer(codec, adc_node,
851				   spec->input_mux.items[0].index,
852				   NULL, "Capture", 0);
853		if (err < 0)
854			return err;
855		return 0;
856	}
857
858	/* create input MUX if multiple sources are available */
859	err = snd_hda_ctl_add(codec, spec->adc_node->nid,
860			      snd_ctl_new1(&cap_sel, codec));
861	if (err < 0)
862		return err;
863
864	/* no volume control? */
865	if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
866	    ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
867		return 0;
868
869	for (i = 0; i < spec->input_mux.num_items; i++) {
870		struct snd_kcontrol_new knew;
871		char name[32];
872		sprintf(name, "%s Capture Volume",
873			spec->input_mux.items[i].label);
874		knew = (struct snd_kcontrol_new)
875			HDA_CODEC_VOLUME(name, adc_node->nid,
876					 spec->input_mux.items[i].index,
877					 HDA_INPUT);
878		err = snd_hda_ctl_add(codec, adc_node->nid,
879					snd_ctl_new1(&knew, codec));
880		if (err < 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 */
896static 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 */
943static int build_loopback_controls(struct hda_codec *codec)
944{
945	struct hda_gspec *spec = codec->spec;
946	struct hda_gnode *node;
947	int err;
948	const char *type;
949
950	if (! spec->out_pin_node[0])
951		return 0;
952
953	list_for_each_entry(node, &spec->nid_list, list) {
954		if (node->type != AC_WID_PIN)
955			continue;
956		/* input capable? */
957		if (! (node->pin_caps & AC_PINCAP_IN))
958			return 0;
959		type = get_input_type(node, NULL);
960		if (type) {
961			if (check_existing_control(codec, type, "Playback"))
962				continue;
963			clear_check_flags(spec);
964			err = parse_loopback_path(codec, spec,
965						  spec->out_pin_node[0],
966						  node, type);
967			if (err < 0)
968				return err;
969			if (! err)
970				continue;
971		}
972	}
973	return 0;
974}
975
976/*
977 * build mixer controls
978 */
979static int build_generic_controls(struct hda_codec *codec)
980{
981	int err;
982
983	if ((err = build_input_controls(codec)) < 0 ||
984	    (err = build_output_controls(codec)) < 0 ||
985	    (err = build_loopback_controls(codec)) < 0)
986		return err;
987
988	return 0;
989}
990
991/*
992 * PCM
993 */
994static struct hda_pcm_stream generic_pcm_playback = {
995	.substreams = 1,
996	.channels_min = 2,
997	.channels_max = 2,
998};
999
1000static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
1001				struct hda_codec *codec,
1002				unsigned int stream_tag,
1003				unsigned int format,
1004				struct snd_pcm_substream *substream)
1005{
1006	struct hda_gspec *spec = codec->spec;
1007
1008	snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1009	snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
1010				   stream_tag, 0, format);
1011	return 0;
1012}
1013
1014static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
1015				struct hda_codec *codec,
1016				struct snd_pcm_substream *substream)
1017{
1018	struct hda_gspec *spec = codec->spec;
1019
1020	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
1021	snd_hda_codec_cleanup_stream(codec, spec->dac_node[1]->nid);
1022	return 0;
1023}
1024
1025static int build_generic_pcms(struct hda_codec *codec)
1026{
1027	struct hda_gspec *spec = codec->spec;
1028	struct hda_pcm *info = &spec->pcm_rec;
1029
1030	if (! spec->dac_node[0] && ! spec->adc_node) {
1031		snd_printd("hda_generic: no PCM found\n");
1032		return 0;
1033	}
1034
1035	codec->num_pcms = 1;
1036	codec->pcm_info = info;
1037
1038	info->name = "HDA Generic";
1039	if (spec->dac_node[0]) {
1040		info->stream[0] = generic_pcm_playback;
1041		info->stream[0].nid = spec->dac_node[0]->nid;
1042		if (spec->dac_node[1]) {
1043			info->stream[0].ops.prepare = generic_pcm2_prepare;
1044			info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1045		}
1046	}
1047	if (spec->adc_node) {
1048		info->stream[1] = generic_pcm_playback;
1049		info->stream[1].nid = spec->adc_node->nid;
1050	}
1051
1052	return 0;
1053}
1054
1055#ifdef CONFIG_SND_HDA_POWER_SAVE
1056static int generic_check_power_status(struct hda_codec *codec, hda_nid_t nid)
1057{
1058	struct hda_gspec *spec = codec->spec;
1059	return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
1060}
1061#endif
1062
1063
1064/*
1065 */
1066static struct hda_codec_ops generic_patch_ops = {
1067	.build_controls = build_generic_controls,
1068	.build_pcms = build_generic_pcms,
1069	.free = snd_hda_generic_free,
1070#ifdef CONFIG_SND_HDA_POWER_SAVE
1071	.check_power_status = generic_check_power_status,
1072#endif
1073};
1074
1075/*
1076 * the generic parser
1077 */
1078int snd_hda_parse_generic_codec(struct hda_codec *codec)
1079{
1080	struct hda_gspec *spec;
1081	int err;
1082
1083	if(!codec->afg)
1084		return 0;
1085
1086	spec = kzalloc(sizeof(*spec), GFP_KERNEL);
1087	if (spec == NULL) {
1088		printk(KERN_ERR "hda_generic: can't allocate spec\n");
1089		return -ENOMEM;
1090	}
1091	codec->spec = spec;
1092	INIT_LIST_HEAD(&spec->nid_list);
1093
1094	if ((err = build_afg_tree(codec)) < 0)
1095		goto error;
1096
1097	if ((err = parse_input(codec)) < 0 ||
1098	    (err = parse_output(codec)) < 0)
1099		goto error;
1100
1101	codec->patch_ops = generic_patch_ops;
1102
1103	return 0;
1104
1105 error:
1106	snd_hda_generic_free(codec);
1107	return err;
1108}
1109EXPORT_SYMBOL(snd_hda_parse_generic_codec);
1110