blob: 000287f7da43018461026f796d8a22d475eaef0a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <sound/core.h>
27#include "hda_codec.h"
28#include "hda_local.h"
29
30/* widget node for parsing */
31struct hda_gnode {
32 hda_nid_t nid; /* NID of this widget */
33 unsigned short nconns; /* number of input connections */
Takashi Iwaid2569502005-11-21 16:33:51 +010034 hda_nid_t *conn_list;
35 hda_nid_t slist[2]; /* temporay list */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 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
Takashi Iwaic3132922005-04-20 13:43:00 +020047/* patch-specific record */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +020048
49#define MAX_PCM_VOLS 2
50struct pcm_vol {
51 struct hda_gnode *node; /* Node for PCM volume */
52 unsigned int index; /* connection of PCM volume */
53};
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct hda_gspec {
Takashi Iwai97ec5582006-03-21 11:29:07 +010056 struct hda_gnode *dac_node[2]; /* DAC node */
57 struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +020058 struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
59 unsigned int pcm_vol_nodes; /* number of PCM volumes */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
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
75/*
76 * retrieve the default device type from the default config value
77 */
Takashi Iwai97ec5582006-03-21 11:29:07 +010078#define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
79 AC_DEFCFG_DEVICE_SHIFT)
80#define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
81 AC_DEFCFG_LOCATION_SHIFT)
82#define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
83 AC_DEFCFG_PORT_CONN_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * destructor
87 */
88static void snd_hda_generic_free(struct hda_codec *codec)
89{
90 struct hda_gspec *spec = codec->spec;
91 struct list_head *p, *n;
92
93 if (! spec)
94 return;
95 /* free all widgets */
96 list_for_each_safe(p, n, &spec->nid_list) {
97 struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
Takashi Iwaid2569502005-11-21 16:33:51 +010098 if (node->conn_list != node->slist)
99 kfree(node->conn_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree(node);
101 }
102 kfree(spec);
103}
104
105
106/*
107 * add a new widget node and read its attributes
108 */
109static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
110{
111 struct hda_gnode *node;
112 int nconns;
Takashi Iwaid2569502005-11-21 16:33:51 +0100113 hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200115 node = kzalloc(sizeof(*node), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (node == NULL)
117 return -ENOMEM;
118 node->nid = nid;
Takashi Iwaid2569502005-11-21 16:33:51 +0100119 nconns = snd_hda_get_connections(codec, nid, conn_list,
120 HDA_MAX_CONNECTIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (nconns < 0) {
122 kfree(node);
123 return nconns;
124 }
Takashi Iwaid2569502005-11-21 16:33:51 +0100125 if (nconns <= ARRAY_SIZE(node->slist))
126 node->conn_list = node->slist;
127 else {
128 node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
129 GFP_KERNEL);
130 if (! node->conn_list) {
131 snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
132 kfree(node);
133 return -ENOMEM;
134 }
135 }
Takashi Iwai0bbed752007-05-08 15:17:15 +0200136 memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 node->nconns = nconns;
Takashi Iwaid2569502005-11-21 16:33:51 +0100138 node->wid_caps = get_wcaps(codec, nid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
140
141 if (node->type == AC_WID_PIN) {
142 node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
143 node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
144 node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
145 }
146
147 if (node->wid_caps & AC_WCAP_OUT_AMP) {
148 if (node->wid_caps & AC_WCAP_AMP_OVRD)
149 node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
150 if (! node->amp_out_caps)
151 node->amp_out_caps = spec->def_amp_out_caps;
152 }
153 if (node->wid_caps & AC_WCAP_IN_AMP) {
154 if (node->wid_caps & AC_WCAP_AMP_OVRD)
155 node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
156 if (! node->amp_in_caps)
157 node->amp_in_caps = spec->def_amp_in_caps;
158 }
159 list_add_tail(&node->list, &spec->nid_list);
160 return 0;
161}
162
163/*
164 * build the AFG subtree
165 */
166static int build_afg_tree(struct hda_codec *codec)
167{
168 struct hda_gspec *spec = codec->spec;
169 int i, nodes, err;
170 hda_nid_t nid;
171
172 snd_assert(spec, return -EINVAL);
173
174 spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
175 spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
176
177 nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
178 if (! nid || nodes < 0) {
179 printk(KERN_ERR "Invalid AFG subtree\n");
180 return -EINVAL;
181 }
182
183 /* parse all nodes belonging to the AFG */
184 for (i = 0; i < nodes; i++, nid++) {
185 if ((err = add_new_node(codec, spec, nid)) < 0)
186 return err;
187 }
188
189 return 0;
190}
191
192
193/*
194 * look for the node record for the given NID
195 */
196/* FIXME: should avoid the braindead linear search */
197static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
198{
199 struct list_head *p;
200 struct hda_gnode *node;
201
202 list_for_each(p, &spec->nid_list) {
203 node = list_entry(p, struct hda_gnode, list);
204 if (node->nid == nid)
205 return node;
206 }
207 return NULL;
208}
209
210/*
211 * unmute (and set max vol) the output amplifier
212 */
213static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
214{
215 unsigned int val, ofs;
216 snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
217 val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
218 ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
219 if (val >= ofs)
220 val -= ofs;
221 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
222 val |= AC_AMP_SET_OUTPUT;
223 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
224}
225
226/*
227 * unmute (and set max vol) the input amplifier
228 */
229static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
230{
231 unsigned int val, ofs;
232 snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
233 val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
234 ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
235 if (val >= ofs)
236 val -= ofs;
237 val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
238 val |= AC_AMP_SET_INPUT;
239 // awk added - fixed to allow unmuting of indexed amps
240 val |= index << AC_AMP_SET_INDEX_SHIFT;
241 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
242}
243
244/*
245 * select the input connection of the given node.
246 */
247static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
248 unsigned int index)
249{
250 snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
251 return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_CONNECT_SEL, index);
252}
253
254/*
255 * clear checked flag of each node in the node list
256 */
257static void clear_check_flags(struct hda_gspec *spec)
258{
259 struct list_head *p;
260 struct hda_gnode *node;
261
262 list_for_each(p, &spec->nid_list) {
263 node = list_entry(p, struct hda_gnode, list);
264 node->checked = 0;
265 }
266}
267
268/*
269 * parse the output path recursively until reach to an audio output widget
270 *
271 * returns 0 if not found, 1 if found, or a negative error code.
272 */
273static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
Takashi Iwai97ec5582006-03-21 11:29:07 +0100274 struct hda_gnode *node, int dac_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int i, err;
277 struct hda_gnode *child;
278
279 if (node->checked)
280 return 0;
281
282 node->checked = 1;
283 if (node->type == AC_WID_AUD_OUT) {
284 if (node->wid_caps & AC_WCAP_DIGITAL) {
285 snd_printdd("Skip Digital OUT node %x\n", node->nid);
286 return 0;
287 }
288 snd_printdd("AUD_OUT found %x\n", node->nid);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100289 if (spec->dac_node[dac_idx]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 /* already DAC node is assigned, just unmute & connect */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100291 return node == spec->dac_node[dac_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
Takashi Iwai97ec5582006-03-21 11:29:07 +0100293 spec->dac_node[dac_idx] = node;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200294 if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
295 spec->pcm_vol_nodes < MAX_PCM_VOLS) {
296 spec->pcm_vol[spec->pcm_vol_nodes].node = node;
297 spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
298 spec->pcm_vol_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 return 1; /* found */
301 }
302
303 for (i = 0; i < node->nconns; i++) {
304 child = hda_get_node(spec, node->conn_list[i]);
305 if (! child)
306 continue;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100307 err = parse_output_path(codec, spec, child, dac_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (err < 0)
309 return err;
310 else if (err > 0) {
311 /* found one,
312 * select the path, unmute both input and output
313 */
314 if (node->nconns > 1)
315 select_input_connection(codec, node, i);
316 unmute_input(codec, node, i);
317 unmute_output(codec, node);
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200318 if (spec->dac_node[dac_idx] &&
319 spec->pcm_vol_nodes < MAX_PCM_VOLS &&
320 !(spec->dac_node[dac_idx]->wid_caps &
321 AC_WCAP_OUT_AMP)) {
322 if ((node->wid_caps & AC_WCAP_IN_AMP) ||
323 (node->wid_caps & AC_WCAP_OUT_AMP)) {
324 int n = spec->pcm_vol_nodes;
325 spec->pcm_vol[n].node = node;
326 spec->pcm_vol[n].index = i;
327 spec->pcm_vol_nodes++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 }
330 return 1;
331 }
332 }
333 return 0;
334}
335
336/*
337 * Look for the output PIN widget with the given jack type
338 * and parse the output path to that PIN.
339 *
340 * Returns the PIN node when the path to DAC is established.
341 */
342static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
343 struct hda_gspec *spec,
344 int jack_type)
345{
346 struct list_head *p;
347 struct hda_gnode *node;
348 int err;
349
350 list_for_each(p, &spec->nid_list) {
351 node = list_entry(p, struct hda_gnode, list);
352 if (node->type != AC_WID_PIN)
353 continue;
354 /* output capable? */
355 if (! (node->pin_caps & AC_PINCAP_OUT))
356 continue;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100357 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
358 continue; /* unconnected */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (jack_type >= 0) {
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200360 if (jack_type != defcfg_type(node))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 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);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100370 err = parse_output_path(codec, spec, node, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (err < 0)
372 return NULL;
Takashi Iwai97ec5582006-03-21 11:29:07 +0100373 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* unmute the PIN output */
380 unmute_output(codec, node);
381 /* set PIN-Out enable */
382 snd_hda_codec_write(codec, node->nid, 0,
383 AC_VERB_SET_PIN_WIDGET_CONTROL,
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200384 AC_PINCTL_OUT_EN |
385 ((node->pin_caps & AC_PINCAP_HP_DRV) ?
386 AC_PINCTL_HP_EN : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 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 */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100408 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* look for the HP-out pin */
416 node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
417 if (node) {
Takashi Iwai97ec5582006-03-21 11:29:07 +0100418 if (! spec->out_pin_node[0])
419 spec->out_pin_node[0] = node;
420 else
421 spec->out_pin_node[1] = node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Takashi Iwai97ec5582006-03-21 11:29:07 +0100424 if (! spec->out_pin_node[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* no line-out or HP pins found,
426 * then choose for the first output pin
427 */
Takashi Iwai97ec5582006-03-21 11:29:07 +0100428 spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
429 if (! spec->out_pin_node[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 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 */
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100441static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
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
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100448static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
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
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100457static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
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{
Takashi Iwaie9edcee2005-06-13 14:16:38 +0200470 unsigned int location = defcfg_location(node);
471 switch (defcfg_type(node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 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:
Takashi Iwai071c73a2006-08-23 18:34:06 +0200477#if 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (pinctl)
Matt1a12de1e2005-04-13 14:37:50 +0200479 *pinctl |= AC_PINCTL_VREF_GRD;
Takashi Iwai071c73a2006-08-23 18:34:06 +0200480#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 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:
Takashi Iwai6afeb112006-12-18 16:16:04 +0100487 if (pinctl &&
488 (node->pin_caps &
489 (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
Takashi Iwai071c73a2006-08-23 18:34:06 +0200490 *pinctl |= AC_PINCTL_VREF_80;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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,
508 struct hda_gnode *node)
509{
510 int i, err;
511 unsigned int pinctl;
512 char *label;
513 const char *type;
514
515 if (node->checked)
516 return 0;
517
518 node->checked = 1;
519 if (node->type != AC_WID_PIN) {
520 for (i = 0; i < node->nconns; i++) {
521 struct hda_gnode *child;
522 child = hda_get_node(spec, node->conn_list[i]);
523 if (! child)
524 continue;
525 err = parse_adc_sub_nodes(codec, spec, child);
526 if (err < 0)
527 return err;
528 if (err > 0) {
529 /* found one,
530 * select the path, unmute both input and output
531 */
532 if (node->nconns > 1)
533 select_input_connection(codec, node, i);
534 unmute_input(codec, node, i);
535 unmute_output(codec, node);
536 return err;
537 }
538 }
539 return 0;
540 }
541
542 /* input capable? */
543 if (! (node->pin_caps & AC_PINCAP_IN))
544 return 0;
545
Takashi Iwai97ec5582006-03-21 11:29:07 +0100546 if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
547 return 0; /* unconnected */
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (node->wid_caps & AC_WCAP_DIGITAL)
550 return 0; /* skip SPDIF */
551
552 if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
553 snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
554 return -EINVAL;
555 }
556
557 pinctl = AC_PINCTL_IN_EN;
558 /* create a proper capture source label */
559 type = get_input_type(node, &pinctl);
560 if (! type) {
561 /* input as default? */
562 if (! (node->pin_ctl & AC_PINCTL_IN_EN))
563 return 0;
564 type = "Input";
565 }
566 label = spec->cap_labels[spec->input_mux.num_items];
567 strcpy(label, type);
568 spec->input_mux.items[spec->input_mux.num_items].label = label;
569
570 /* unmute the PIN external input */
571 unmute_input(codec, node, 0); /* index = 0? */
572 /* set PIN-In enable */
573 snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
574
575 return 1; /* found */
576}
577
Takashi Iwai071c73a2006-08-23 18:34:06 +0200578/* add a capture source element */
579static void add_cap_src(struct hda_gspec *spec, int idx)
580{
581 struct hda_input_mux_item *csrc;
582 char *buf;
583 int num, ocap;
584
585 num = spec->input_mux.num_items;
586 csrc = &spec->input_mux.items[num];
587 buf = spec->cap_labels[num];
588 for (ocap = 0; ocap < num; ocap++) {
589 if (! strcmp(buf, spec->cap_labels[ocap])) {
590 /* same label already exists,
591 * put the index number to be unique
592 */
593 sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
594 break;
595 }
596 }
597 csrc->index = idx;
598 spec->input_mux.num_items++;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/*
602 * parse input
603 */
604static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
605{
606 struct hda_gspec *spec = codec->spec;
607 struct hda_gnode *node;
608 int i, err;
609
610 snd_printdd("AUD_IN = %x\n", adc_node->nid);
611 clear_check_flags(spec);
612
613 // awk added - fixed no recording due to muted widget
614 unmute_input(codec, adc_node, 0);
615
616 /*
617 * check each connection of the ADC
618 * if it reaches to a proper input PIN, add the path as the
619 * input path.
620 */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200621 /* first, check the direct connections to PIN widgets */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 for (i = 0; i < adc_node->nconns; i++) {
623 node = hda_get_node(spec, adc_node->conn_list[i]);
Takashi Iwai071c73a2006-08-23 18:34:06 +0200624 if (node && node->type == AC_WID_PIN) {
625 err = parse_adc_sub_nodes(codec, spec, node);
626 if (err < 0)
627 return err;
628 else if (err > 0)
629 add_cap_src(spec, i);
630 }
631 }
632 /* ... then check the rests, more complicated connections */
633 for (i = 0; i < adc_node->nconns; i++) {
634 node = hda_get_node(spec, adc_node->conn_list[i]);
635 if (node && node->type != AC_WID_PIN) {
636 err = parse_adc_sub_nodes(codec, spec, node);
637 if (err < 0)
638 return err;
639 else if (err > 0)
640 add_cap_src(spec, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642 }
643
644 if (! spec->input_mux.num_items)
645 return 0; /* no input path found... */
646
647 snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
648 for (i = 0; i < spec->input_mux.num_items; i++)
649 snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
650 spec->input_mux.items[i].index);
651
652 spec->adc_node = adc_node;
653 return 1;
654}
655
656/*
657 * parse input
658 */
659static int parse_input(struct hda_codec *codec)
660{
661 struct hda_gspec *spec = codec->spec;
662 struct list_head *p;
663 struct hda_gnode *node;
664 int err;
665
666 /*
667 * At first we look for an audio input widget.
668 * If it reaches to certain input PINs, we take it as the
669 * input path.
670 */
671 list_for_each(p, &spec->nid_list) {
672 node = list_entry(p, struct hda_gnode, list);
673 if (node->wid_caps & AC_WCAP_DIGITAL)
674 continue; /* skip SPDIF */
675 if (node->type == AC_WID_AUD_IN) {
676 err = parse_input_path(codec, node);
677 if (err < 0)
678 return err;
679 else if (err > 0)
680 return 0;
681 }
682 }
683 snd_printd("hda_generic: no proper input path found\n");
684 return 0;
685}
686
687/*
688 * create mixer controls if possible
689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
691 unsigned int index, const char *type, const char *dir_sfx)
692{
693 char name[32];
694 int err;
695 int created = 0;
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100696 struct snd_kcontrol_new knew;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 if (type)
699 sprintf(name, "%s %s Switch", type, dir_sfx);
700 else
701 sprintf(name, "%s Switch", dir_sfx);
702 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
703 (node->amp_in_caps & AC_AMPCAP_MUTE)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100704 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
706 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
707 return err;
708 created = 1;
709 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
710 (node->amp_out_caps & AC_AMPCAP_MUTE)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100711 knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
713 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
714 return err;
715 created = 1;
716 }
717
718 if (type)
719 sprintf(name, "%s %s Volume", type, dir_sfx);
720 else
721 sprintf(name, "%s Volume", dir_sfx);
722 if ((node->wid_caps & AC_WCAP_IN_AMP) &&
723 (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100724 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
726 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
727 return err;
728 created = 1;
729 } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
730 (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100731 knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
733 if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
734 return err;
735 created = 1;
736 }
737
738 return created;
739}
740
741/*
742 * check whether the controls with the given name and direction suffix already exist
743 */
744static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
745{
Takashi Iwaic8b6bf9b2005-11-17 14:57:47 +0100746 struct snd_ctl_elem_id id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 memset(&id, 0, sizeof(id));
748 sprintf(id.name, "%s %s Volume", type, dir);
749 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
750 if (snd_ctl_find_id(codec->bus->card, &id))
751 return 1;
752 sprintf(id.name, "%s %s Switch", type, dir);
753 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
754 if (snd_ctl_find_id(codec->bus->card, &id))
755 return 1;
756 return 0;
757}
758
759/*
760 * build output mixer controls
761 */
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200762static int create_output_mixers(struct hda_codec *codec, const char **names)
763{
764 struct hda_gspec *spec = codec->spec;
765 int i, err;
766
767 for (i = 0; i < spec->pcm_vol_nodes; i++) {
768 err = create_mixer(codec, spec->pcm_vol[i].node,
769 spec->pcm_vol[i].index,
770 names[i], "Playback");
771 if (err < 0)
772 return err;
773 }
774 return 0;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static int build_output_controls(struct hda_codec *codec)
778{
779 struct hda_gspec *spec = codec->spec;
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200780 static const char *types_speaker[] = { "Speaker", "Headphone" };
781 static const char *types_line[] = { "Front", "Headphone" };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Takashi Iwaia7da6ce2006-09-06 14:03:14 +0200783 switch (spec->pcm_vol_nodes) {
784 case 1:
785 return create_mixer(codec, spec->pcm_vol[0].node,
786 spec->pcm_vol[0].index,
787 "Master", "Playback");
788 case 2:
789 if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
790 return create_output_mixers(codec, types_speaker);
791 else
792 return create_output_mixers(codec, types_line);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
795}
796
797/* create capture volume/switch */
798static int build_input_controls(struct hda_codec *codec)
799{
800 struct hda_gspec *spec = codec->spec;
801 struct hda_gnode *adc_node = spec->adc_node;
Takashi Iwai071c73a2006-08-23 18:34:06 +0200802 int i, err;
803 static struct snd_kcontrol_new cap_sel = {
804 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
805 .name = "Capture Source",
806 .info = capture_source_info,
807 .get = capture_source_get,
808 .put = capture_source_put,
809 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Takashi Iwai071c73a2006-08-23 18:34:06 +0200811 if (! adc_node || ! spec->input_mux.num_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return 0; /* not found */
813
Takashi Iwai071c73a2006-08-23 18:34:06 +0200814 spec->cur_cap_src = 0;
815 select_input_connection(codec, adc_node,
816 spec->input_mux.items[0].index);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* create capture volume and switch controls if the ADC has an amp */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200819 /* do we have only a single item? */
820 if (spec->input_mux.num_items == 1) {
821 err = create_mixer(codec, adc_node,
822 spec->input_mux.items[0].index,
823 NULL, "Capture");
824 if (err < 0)
825 return err;
826 return 0;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 /* create input MUX if multiple sources are available */
Takashi Iwai071c73a2006-08-23 18:34:06 +0200830 if ((err = snd_ctl_add(codec->bus->card,
831 snd_ctl_new1(&cap_sel, codec))) < 0)
832 return err;
833
834 /* no volume control? */
835 if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
836 ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
837 return 0;
838
839 for (i = 0; i < spec->input_mux.num_items; i++) {
840 struct snd_kcontrol_new knew;
841 char name[32];
842 sprintf(name, "%s Capture Volume",
843 spec->input_mux.items[i].label);
844 knew = (struct snd_kcontrol_new)
845 HDA_CODEC_VOLUME(name, adc_node->nid,
846 spec->input_mux.items[i].index,
847 HDA_INPUT);
848 if ((err = snd_ctl_add(codec->bus->card,
849 snd_ctl_new1(&knew, codec))) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 }
Takashi Iwai071c73a2006-08-23 18:34:06 +0200852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return 0;
854}
855
856
857/*
858 * parse the nodes recursively until reach to the output PIN.
859 *
860 * returns 0 - if not found,
861 * 1 - if found, but no mixer is created
862 * 2 - if found and mixer was already created, (just skip)
863 * a negative error code
864 */
865static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
866 struct hda_gnode *node, struct hda_gnode *dest_node,
867 const char *type)
868{
869 int i, err;
870
871 if (node->checked)
872 return 0;
873
874 node->checked = 1;
875 if (node == dest_node) {
876 /* loopback connection found */
877 return 1;
878 }
879
880 for (i = 0; i < node->nconns; i++) {
881 struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
882 if (! child)
883 continue;
884 err = parse_loopback_path(codec, spec, child, dest_node, type);
885 if (err < 0)
886 return err;
887 else if (err >= 1) {
888 if (err == 1) {
889 err = create_mixer(codec, node, i, type, "Playback");
890 if (err < 0)
891 return err;
892 if (err > 0)
893 return 2; /* ok, created */
894 /* not created, maybe in the lower path */
895 err = 1;
896 }
897 /* connect and unmute */
898 if (node->nconns > 1)
899 select_input_connection(codec, node, i);
900 unmute_input(codec, node, i);
901 unmute_output(codec, node);
902 return err;
903 }
904 }
905 return 0;
906}
907
908/*
909 * parse the tree and build the loopback controls
910 */
911static int build_loopback_controls(struct hda_codec *codec)
912{
913 struct hda_gspec *spec = codec->spec;
914 struct list_head *p;
915 struct hda_gnode *node;
916 int err;
917 const char *type;
918
Takashi Iwai97ec5582006-03-21 11:29:07 +0100919 if (! spec->out_pin_node[0])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921
922 list_for_each(p, &spec->nid_list) {
923 node = list_entry(p, struct hda_gnode, list);
924 if (node->type != AC_WID_PIN)
925 continue;
926 /* input capable? */
927 if (! (node->pin_caps & AC_PINCAP_IN))
928 return 0;
929 type = get_input_type(node, NULL);
930 if (type) {
931 if (check_existing_control(codec, type, "Playback"))
932 continue;
933 clear_check_flags(spec);
Takashi Iwai97ec5582006-03-21 11:29:07 +0100934 err = parse_loopback_path(codec, spec,
935 spec->out_pin_node[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 node, type);
937 if (err < 0)
938 return err;
939 if (! err)
940 continue;
941 }
942 }
943 return 0;
944}
945
946/*
947 * build mixer controls
948 */
949static int build_generic_controls(struct hda_codec *codec)
950{
951 int err;
952
953 if ((err = build_input_controls(codec)) < 0 ||
954 (err = build_output_controls(codec)) < 0 ||
955 (err = build_loopback_controls(codec)) < 0)
956 return err;
957
958 return 0;
959}
960
961/*
962 * PCM
963 */
964static struct hda_pcm_stream generic_pcm_playback = {
965 .substreams = 1,
966 .channels_min = 2,
967 .channels_max = 2,
968};
969
Takashi Iwai97ec5582006-03-21 11:29:07 +0100970static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
971 struct hda_codec *codec,
972 unsigned int stream_tag,
973 unsigned int format,
974 struct snd_pcm_substream *substream)
975{
976 struct hda_gspec *spec = codec->spec;
977
978 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
979 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
980 stream_tag, 0, format);
981 return 0;
982}
983
984static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
985 struct hda_codec *codec,
986 struct snd_pcm_substream *substream)
987{
988 struct hda_gspec *spec = codec->spec;
989
990 snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
991 snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
992 return 0;
993}
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995static int build_generic_pcms(struct hda_codec *codec)
996{
997 struct hda_gspec *spec = codec->spec;
998 struct hda_pcm *info = &spec->pcm_rec;
999
Takashi Iwai97ec5582006-03-21 11:29:07 +01001000 if (! spec->dac_node[0] && ! spec->adc_node) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 snd_printd("hda_generic: no PCM found\n");
1002 return 0;
1003 }
1004
1005 codec->num_pcms = 1;
1006 codec->pcm_info = info;
1007
1008 info->name = "HDA Generic";
Takashi Iwai97ec5582006-03-21 11:29:07 +01001009 if (spec->dac_node[0]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 info->stream[0] = generic_pcm_playback;
Takashi Iwai97ec5582006-03-21 11:29:07 +01001011 info->stream[0].nid = spec->dac_node[0]->nid;
1012 if (spec->dac_node[1]) {
1013 info->stream[0].ops.prepare = generic_pcm2_prepare;
1014 info->stream[0].ops.cleanup = generic_pcm2_cleanup;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017 if (spec->adc_node) {
1018 info->stream[1] = generic_pcm_playback;
1019 info->stream[1].nid = spec->adc_node->nid;
1020 }
1021
1022 return 0;
1023}
1024
1025
1026/*
1027 */
1028static struct hda_codec_ops generic_patch_ops = {
1029 .build_controls = build_generic_controls,
1030 .build_pcms = build_generic_pcms,
1031 .free = snd_hda_generic_free,
1032};
1033
1034/*
1035 * the generic parser
1036 */
1037int snd_hda_parse_generic_codec(struct hda_codec *codec)
1038{
1039 struct hda_gspec *spec;
1040 int err;
1041
Sasha Khapyorsky84802f02005-09-13 11:25:54 +02001042 if(!codec->afg)
1043 return 0;
Sasha Khapyorsky673b6832005-08-11 11:00:16 +02001044
Takashi Iwaie560d8d2005-09-09 14:21:46 +02001045 spec = kzalloc(sizeof(*spec), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (spec == NULL) {
1047 printk(KERN_ERR "hda_generic: can't allocate spec\n");
1048 return -ENOMEM;
1049 }
1050 codec->spec = spec;
1051 INIT_LIST_HEAD(&spec->nid_list);
1052
1053 if ((err = build_afg_tree(codec)) < 0)
1054 goto error;
1055
1056 if ((err = parse_input(codec)) < 0 ||
1057 (err = parse_output(codec)) < 0)
1058 goto error;
1059
1060 codec->patch_ops = generic_patch_ops;
1061
1062 return 0;
1063
1064 error:
1065 snd_hda_generic_free(codec);
1066 return err;
1067}