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