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