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