1 /* 2 * Universal Interface for Intel High Definition Audio Codec 3 * 4 * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de> 5 * 6 * 7 * This driver is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This driver is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 #include <linux/mm.h> 23 #include <linux/init.h> 24 #include <linux/delay.h> 25 #include <linux/slab.h> 26 #include <linux/mutex.h> 27 #include <linux/module.h> 28 #include <linux/async.h> 29 #include <sound/core.h> 30 #include "hda_codec.h" 31 #include <sound/asoundef.h> 32 #include <sound/tlv.h> 33 #include <sound/initval.h> 34 #include <sound/jack.h> 35 #include "hda_local.h" 36 #include "hda_beep.h" 37 #include "hda_jack.h" 38 #include <sound/hda_hwdep.h> 39 40 #define CREATE_TRACE_POINTS 41 #include "hda_trace.h" 42 43 /* 44 * vendor / preset table 45 */ 46 47 struct hda_vendor_id { 48 unsigned int id; 49 const char *name; 50 }; 51 52 /* codec vendor labels */ 53 static struct hda_vendor_id hda_vendor_ids[] = { 54 { 0x1002, "ATI" }, 55 { 0x1013, "Cirrus Logic" }, 56 { 0x1057, "Motorola" }, 57 { 0x1095, "Silicon Image" }, 58 { 0x10de, "Nvidia" }, 59 { 0x10ec, "Realtek" }, 60 { 0x1102, "Creative" }, 61 { 0x1106, "VIA" }, 62 { 0x111d, "IDT" }, 63 { 0x11c1, "LSI" }, 64 { 0x11d4, "Analog Devices" }, 65 { 0x13f6, "C-Media" }, 66 { 0x14f1, "Conexant" }, 67 { 0x17e8, "Chrontel" }, 68 { 0x1854, "LG" }, 69 { 0x1aec, "Wolfson Microelectronics" }, 70 { 0x1af4, "QEMU" }, 71 { 0x434d, "C-Media" }, 72 { 0x8086, "Intel" }, 73 { 0x8384, "SigmaTel" }, 74 {} /* terminator */ 75 }; 76 77 static DEFINE_MUTEX(preset_mutex); 78 static LIST_HEAD(hda_preset_tables); 79 80 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset) 81 { 82 mutex_lock(&preset_mutex); 83 list_add_tail(&preset->list, &hda_preset_tables); 84 mutex_unlock(&preset_mutex); 85 return 0; 86 } 87 EXPORT_SYMBOL_GPL(snd_hda_add_codec_preset); 88 89 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset) 90 { 91 mutex_lock(&preset_mutex); 92 list_del(&preset->list); 93 mutex_unlock(&preset_mutex); 94 return 0; 95 } 96 EXPORT_SYMBOL_GPL(snd_hda_delete_codec_preset); 97 98 #ifdef CONFIG_PM 99 #define codec_in_pm(codec) ((codec)->in_pm) 100 static void hda_power_work(struct work_struct *work); 101 static void hda_keep_power_on(struct hda_codec *codec); 102 #define hda_codec_is_power_on(codec) ((codec)->power_on) 103 104 static void hda_call_pm_notify(struct hda_codec *codec, bool power_up) 105 { 106 struct hda_bus *bus = codec->bus; 107 108 if ((power_up && codec->pm_up_notified) || 109 (!power_up && !codec->pm_up_notified)) 110 return; 111 if (bus->ops.pm_notify) 112 bus->ops.pm_notify(bus, power_up); 113 codec->pm_up_notified = power_up; 114 } 115 116 #else 117 #define codec_in_pm(codec) 0 118 static inline void hda_keep_power_on(struct hda_codec *codec) {} 119 #define hda_codec_is_power_on(codec) 1 120 #define hda_call_pm_notify(codec, state) {} 121 #endif 122 123 /** 124 * snd_hda_get_jack_location - Give a location string of the jack 125 * @cfg: pin default config value 126 * 127 * Parse the pin default config value and returns the string of the 128 * jack location, e.g. "Rear", "Front", etc. 129 */ 130 const char *snd_hda_get_jack_location(u32 cfg) 131 { 132 static char *bases[7] = { 133 "N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom", 134 }; 135 static unsigned char specials_idx[] = { 136 0x07, 0x08, 137 0x17, 0x18, 0x19, 138 0x37, 0x38 139 }; 140 static char *specials[] = { 141 "Rear Panel", "Drive Bar", 142 "Riser", "HDMI", "ATAPI", 143 "Mobile-In", "Mobile-Out" 144 }; 145 int i; 146 cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT; 147 if ((cfg & 0x0f) < 7) 148 return bases[cfg & 0x0f]; 149 for (i = 0; i < ARRAY_SIZE(specials_idx); i++) { 150 if (cfg == specials_idx[i]) 151 return specials[i]; 152 } 153 return "UNKNOWN"; 154 } 155 EXPORT_SYMBOL_GPL(snd_hda_get_jack_location); 156 157 /** 158 * snd_hda_get_jack_connectivity - Give a connectivity string of the jack 159 * @cfg: pin default config value 160 * 161 * Parse the pin default config value and returns the string of the 162 * jack connectivity, i.e. external or internal connection. 163 */ 164 const char *snd_hda_get_jack_connectivity(u32 cfg) 165 { 166 static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" }; 167 168 return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3]; 169 } 170 EXPORT_SYMBOL_GPL(snd_hda_get_jack_connectivity); 171 172 /** 173 * snd_hda_get_jack_type - Give a type string of the jack 174 * @cfg: pin default config value 175 * 176 * Parse the pin default config value and returns the string of the 177 * jack type, i.e. the purpose of the jack, such as Line-Out or CD. 178 */ 179 const char *snd_hda_get_jack_type(u32 cfg) 180 { 181 static char *jack_types[16] = { 182 "Line Out", "Speaker", "HP Out", "CD", 183 "SPDIF Out", "Digital Out", "Modem Line", "Modem Hand", 184 "Line In", "Aux", "Mic", "Telephony", 185 "SPDIF In", "Digital In", "Reserved", "Other" 186 }; 187 188 return jack_types[(cfg & AC_DEFCFG_DEVICE) 189 >> AC_DEFCFG_DEVICE_SHIFT]; 190 } 191 EXPORT_SYMBOL_GPL(snd_hda_get_jack_type); 192 193 /* 194 * Compose a 32bit command word to be sent to the HD-audio controller 195 */ 196 static inline unsigned int 197 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int flags, 198 unsigned int verb, unsigned int parm) 199 { 200 u32 val; 201 202 if ((codec->addr & ~0xf) || (nid & ~0x7f) || 203 (verb & ~0xfff) || (parm & ~0xffff)) { 204 codec_err(codec, "hda-codec: out of range cmd %x:%x:%x:%x\n", 205 codec->addr, nid, verb, parm); 206 return ~0; 207 } 208 209 val = (u32)codec->addr << 28; 210 val |= (u32)nid << 20; 211 val |= verb << 8; 212 val |= parm; 213 return val; 214 } 215 216 /* 217 * Send and receive a verb 218 */ 219 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd, 220 int flags, unsigned int *res) 221 { 222 struct hda_bus *bus = codec->bus; 223 int err; 224 225 if (cmd == ~0) 226 return -1; 227 228 if (res) 229 *res = -1; 230 again: 231 snd_hda_power_up(codec); 232 mutex_lock(&bus->cmd_mutex); 233 if (flags & HDA_RW_NO_RESPONSE_FALLBACK) 234 bus->no_response_fallback = 1; 235 for (;;) { 236 trace_hda_send_cmd(codec, cmd); 237 err = bus->ops.command(bus, cmd); 238 if (err != -EAGAIN) 239 break; 240 /* process pending verbs */ 241 bus->ops.get_response(bus, codec->addr); 242 } 243 if (!err && res) { 244 *res = bus->ops.get_response(bus, codec->addr); 245 trace_hda_get_response(codec, *res); 246 } 247 bus->no_response_fallback = 0; 248 mutex_unlock(&bus->cmd_mutex); 249 snd_hda_power_down(codec); 250 if (!codec_in_pm(codec) && res && *res == -1 && bus->rirb_error) { 251 if (bus->response_reset) { 252 codec_dbg(codec, 253 "resetting BUS due to fatal communication error\n"); 254 trace_hda_bus_reset(bus); 255 bus->ops.bus_reset(bus); 256 } 257 goto again; 258 } 259 /* clear reset-flag when the communication gets recovered */ 260 if (!err || codec_in_pm(codec)) 261 bus->response_reset = 0; 262 return err; 263 } 264 265 /** 266 * snd_hda_codec_read - send a command and get the response 267 * @codec: the HDA codec 268 * @nid: NID to send the command 269 * @flags: optional bit flags 270 * @verb: the verb to send 271 * @parm: the parameter for the verb 272 * 273 * Send a single command and read the corresponding response. 274 * 275 * Returns the obtained response value, or -1 for an error. 276 */ 277 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, 278 int flags, 279 unsigned int verb, unsigned int parm) 280 { 281 unsigned cmd = make_codec_cmd(codec, nid, flags, verb, parm); 282 unsigned int res; 283 if (codec_exec_verb(codec, cmd, flags, &res)) 284 return -1; 285 return res; 286 } 287 EXPORT_SYMBOL_GPL(snd_hda_codec_read); 288 289 /** 290 * snd_hda_codec_write - send a single command without waiting for response 291 * @codec: the HDA codec 292 * @nid: NID to send the command 293 * @flags: optional bit flags 294 * @verb: the verb to send 295 * @parm: the parameter for the verb 296 * 297 * Send a single command without waiting for response. 298 * 299 * Returns 0 if successful, or a negative error code. 300 */ 301 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int flags, 302 unsigned int verb, unsigned int parm) 303 { 304 unsigned int cmd = make_codec_cmd(codec, nid, flags, verb, parm); 305 unsigned int res; 306 return codec_exec_verb(codec, cmd, flags, 307 codec->bus->sync_write ? &res : NULL); 308 } 309 EXPORT_SYMBOL_GPL(snd_hda_codec_write); 310 311 /** 312 * snd_hda_sequence_write - sequence writes 313 * @codec: the HDA codec 314 * @seq: VERB array to send 315 * 316 * Send the commands sequentially from the given array. 317 * The array must be terminated with NID=0. 318 */ 319 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq) 320 { 321 for (; seq->nid; seq++) 322 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param); 323 } 324 EXPORT_SYMBOL_GPL(snd_hda_sequence_write); 325 326 /** 327 * snd_hda_get_sub_nodes - get the range of sub nodes 328 * @codec: the HDA codec 329 * @nid: NID to parse 330 * @start_id: the pointer to store the start NID 331 * 332 * Parse the NID and store the start NID of its sub-nodes. 333 * Returns the number of sub-nodes. 334 */ 335 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, 336 hda_nid_t *start_id) 337 { 338 unsigned int parm; 339 340 parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT); 341 if (parm == -1) 342 return 0; 343 *start_id = (parm >> 16) & 0x7fff; 344 return (int)(parm & 0x7fff); 345 } 346 EXPORT_SYMBOL_GPL(snd_hda_get_sub_nodes); 347 348 /* connection list element */ 349 struct hda_conn_list { 350 struct list_head list; 351 int len; 352 hda_nid_t nid; 353 hda_nid_t conns[0]; 354 }; 355 356 /* look up the cached results */ 357 static struct hda_conn_list * 358 lookup_conn_list(struct hda_codec *codec, hda_nid_t nid) 359 { 360 struct hda_conn_list *p; 361 list_for_each_entry(p, &codec->conn_list, list) { 362 if (p->nid == nid) 363 return p; 364 } 365 return NULL; 366 } 367 368 static int add_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, 369 const hda_nid_t *list) 370 { 371 struct hda_conn_list *p; 372 373 p = kmalloc(sizeof(*p) + len * sizeof(hda_nid_t), GFP_KERNEL); 374 if (!p) 375 return -ENOMEM; 376 p->len = len; 377 p->nid = nid; 378 memcpy(p->conns, list, len * sizeof(hda_nid_t)); 379 list_add(&p->list, &codec->conn_list); 380 return 0; 381 } 382 383 static void remove_conn_list(struct hda_codec *codec) 384 { 385 while (!list_empty(&codec->conn_list)) { 386 struct hda_conn_list *p; 387 p = list_first_entry(&codec->conn_list, typeof(*p), list); 388 list_del(&p->list); 389 kfree(p); 390 } 391 } 392 393 /* read the connection and add to the cache */ 394 static int read_and_add_raw_conns(struct hda_codec *codec, hda_nid_t nid) 395 { 396 hda_nid_t list[32]; 397 hda_nid_t *result = list; 398 int len; 399 400 len = snd_hda_get_raw_connections(codec, nid, list, ARRAY_SIZE(list)); 401 if (len == -ENOSPC) { 402 len = snd_hda_get_num_raw_conns(codec, nid); 403 result = kmalloc(sizeof(hda_nid_t) * len, GFP_KERNEL); 404 if (!result) 405 return -ENOMEM; 406 len = snd_hda_get_raw_connections(codec, nid, result, len); 407 } 408 if (len >= 0) 409 len = snd_hda_override_conn_list(codec, nid, len, result); 410 if (result != list) 411 kfree(result); 412 return len; 413 } 414 415 /** 416 * snd_hda_get_conn_list - get connection list 417 * @codec: the HDA codec 418 * @nid: NID to parse 419 * @len: number of connection list entries 420 * @listp: the pointer to store NID list 421 * 422 * Parses the connection list of the given widget and stores the pointer 423 * to the list of NIDs. 424 * 425 * Returns the number of connections, or a negative error code. 426 * 427 * Note that the returned pointer isn't protected against the list 428 * modification. If snd_hda_override_conn_list() might be called 429 * concurrently, protect with a mutex appropriately. 430 */ 431 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid, 432 const hda_nid_t **listp) 433 { 434 bool added = false; 435 436 for (;;) { 437 int err; 438 const struct hda_conn_list *p; 439 440 /* if the connection-list is already cached, read it */ 441 p = lookup_conn_list(codec, nid); 442 if (p) { 443 if (listp) 444 *listp = p->conns; 445 return p->len; 446 } 447 if (snd_BUG_ON(added)) 448 return -EINVAL; 449 450 err = read_and_add_raw_conns(codec, nid); 451 if (err < 0) 452 return err; 453 added = true; 454 } 455 } 456 EXPORT_SYMBOL_GPL(snd_hda_get_conn_list); 457 458 /** 459 * snd_hda_get_connections - copy connection list 460 * @codec: the HDA codec 461 * @nid: NID to parse 462 * @conn_list: connection list array; when NULL, checks only the size 463 * @max_conns: max. number of connections to store 464 * 465 * Parses the connection list of the given widget and stores the list 466 * of NIDs. 467 * 468 * Returns the number of connections, or a negative error code. 469 */ 470 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid, 471 hda_nid_t *conn_list, int max_conns) 472 { 473 const hda_nid_t *list; 474 int len = snd_hda_get_conn_list(codec, nid, &list); 475 476 if (len > 0 && conn_list) { 477 if (len > max_conns) { 478 codec_err(codec, "Too many connections %d for NID 0x%x\n", 479 len, nid); 480 return -EINVAL; 481 } 482 memcpy(conn_list, list, len * sizeof(hda_nid_t)); 483 } 484 485 return len; 486 } 487 EXPORT_SYMBOL_GPL(snd_hda_get_connections); 488 489 /* return CONNLIST_LEN parameter of the given widget */ 490 static unsigned int get_num_conns(struct hda_codec *codec, hda_nid_t nid) 491 { 492 unsigned int wcaps = get_wcaps(codec, nid); 493 unsigned int parm; 494 495 if (!(wcaps & AC_WCAP_CONN_LIST) && 496 get_wcaps_type(wcaps) != AC_WID_VOL_KNB) 497 return 0; 498 499 parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN); 500 if (parm == -1) 501 parm = 0; 502 return parm; 503 } 504 505 int snd_hda_get_num_raw_conns(struct hda_codec *codec, hda_nid_t nid) 506 { 507 return snd_hda_get_raw_connections(codec, nid, NULL, 0); 508 } 509 510 /** 511 * snd_hda_get_raw_connections - copy connection list without cache 512 * @codec: the HDA codec 513 * @nid: NID to parse 514 * @conn_list: connection list array 515 * @max_conns: max. number of connections to store 516 * 517 * Like snd_hda_get_connections(), copy the connection list but without 518 * checking through the connection-list cache. 519 * Currently called only from hda_proc.c, so not exported. 520 */ 521 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid, 522 hda_nid_t *conn_list, int max_conns) 523 { 524 unsigned int parm; 525 int i, conn_len, conns; 526 unsigned int shift, num_elems, mask; 527 hda_nid_t prev_nid; 528 int null_count = 0; 529 530 parm = get_num_conns(codec, nid); 531 if (!parm) 532 return 0; 533 534 if (parm & AC_CLIST_LONG) { 535 /* long form */ 536 shift = 16; 537 num_elems = 2; 538 } else { 539 /* short form */ 540 shift = 8; 541 num_elems = 4; 542 } 543 conn_len = parm & AC_CLIST_LENGTH; 544 mask = (1 << (shift-1)) - 1; 545 546 if (!conn_len) 547 return 0; /* no connection */ 548 549 if (conn_len == 1) { 550 /* single connection */ 551 parm = snd_hda_codec_read(codec, nid, 0, 552 AC_VERB_GET_CONNECT_LIST, 0); 553 if (parm == -1 && codec->bus->rirb_error) 554 return -EIO; 555 if (conn_list) 556 conn_list[0] = parm & mask; 557 return 1; 558 } 559 560 /* multi connection */ 561 conns = 0; 562 prev_nid = 0; 563 for (i = 0; i < conn_len; i++) { 564 int range_val; 565 hda_nid_t val, n; 566 567 if (i % num_elems == 0) { 568 parm = snd_hda_codec_read(codec, nid, 0, 569 AC_VERB_GET_CONNECT_LIST, i); 570 if (parm == -1 && codec->bus->rirb_error) 571 return -EIO; 572 } 573 range_val = !!(parm & (1 << (shift-1))); /* ranges */ 574 val = parm & mask; 575 if (val == 0 && null_count++) { /* no second chance */ 576 codec_dbg(codec, 577 "invalid CONNECT_LIST verb %x[%i]:%x\n", 578 nid, i, parm); 579 return 0; 580 } 581 parm >>= shift; 582 if (range_val) { 583 /* ranges between the previous and this one */ 584 if (!prev_nid || prev_nid >= val) { 585 codec_warn(codec, 586 "invalid dep_range_val %x:%x\n", 587 prev_nid, val); 588 continue; 589 } 590 for (n = prev_nid + 1; n <= val; n++) { 591 if (conn_list) { 592 if (conns >= max_conns) 593 return -ENOSPC; 594 conn_list[conns] = n; 595 } 596 conns++; 597 } 598 } else { 599 if (conn_list) { 600 if (conns >= max_conns) 601 return -ENOSPC; 602 conn_list[conns] = val; 603 } 604 conns++; 605 } 606 prev_nid = val; 607 } 608 return conns; 609 } 610 611 /** 612 * snd_hda_override_conn_list - add/modify the connection-list to cache 613 * @codec: the HDA codec 614 * @nid: NID to parse 615 * @len: number of connection list entries 616 * @list: the list of connection entries 617 * 618 * Add or modify the given connection-list to the cache. If the corresponding 619 * cache already exists, invalidate it and append a new one. 620 * 621 * Returns zero or a negative error code. 622 */ 623 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len, 624 const hda_nid_t *list) 625 { 626 struct hda_conn_list *p; 627 628 p = lookup_conn_list(codec, nid); 629 if (p) { 630 list_del(&p->list); 631 kfree(p); 632 } 633 634 return add_conn_list(codec, nid, len, list); 635 } 636 EXPORT_SYMBOL_GPL(snd_hda_override_conn_list); 637 638 /** 639 * snd_hda_get_conn_index - get the connection index of the given NID 640 * @codec: the HDA codec 641 * @mux: NID containing the list 642 * @nid: NID to select 643 * @recursive: 1 when searching NID recursively, otherwise 0 644 * 645 * Parses the connection list of the widget @mux and checks whether the 646 * widget @nid is present. If it is, return the connection index. 647 * Otherwise it returns -1. 648 */ 649 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux, 650 hda_nid_t nid, int recursive) 651 { 652 const hda_nid_t *conn; 653 int i, nums; 654 655 nums = snd_hda_get_conn_list(codec, mux, &conn); 656 for (i = 0; i < nums; i++) 657 if (conn[i] == nid) 658 return i; 659 if (!recursive) 660 return -1; 661 if (recursive > 10) { 662 codec_dbg(codec, "too deep connection for 0x%x\n", nid); 663 return -1; 664 } 665 recursive++; 666 for (i = 0; i < nums; i++) { 667 unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i])); 668 if (type == AC_WID_PIN || type == AC_WID_AUD_OUT) 669 continue; 670 if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0) 671 return i; 672 } 673 return -1; 674 } 675 EXPORT_SYMBOL_GPL(snd_hda_get_conn_index); 676 677 678 /* return DEVLIST_LEN parameter of the given widget */ 679 static unsigned int get_num_devices(struct hda_codec *codec, hda_nid_t nid) 680 { 681 unsigned int wcaps = get_wcaps(codec, nid); 682 unsigned int parm; 683 684 if (!codec->dp_mst || !(wcaps & AC_WCAP_DIGITAL) || 685 get_wcaps_type(wcaps) != AC_WID_PIN) 686 return 0; 687 688 parm = snd_hda_param_read(codec, nid, AC_PAR_DEVLIST_LEN); 689 if (parm == -1 && codec->bus->rirb_error) 690 parm = 0; 691 return parm & AC_DEV_LIST_LEN_MASK; 692 } 693 694 /** 695 * snd_hda_get_devices - copy device list without cache 696 * @codec: the HDA codec 697 * @nid: NID of the pin to parse 698 * @dev_list: device list array 699 * @max_devices: max. number of devices to store 700 * 701 * Copy the device list. This info is dynamic and so not cached. 702 * Currently called only from hda_proc.c, so not exported. 703 */ 704 int snd_hda_get_devices(struct hda_codec *codec, hda_nid_t nid, 705 u8 *dev_list, int max_devices) 706 { 707 unsigned int parm; 708 int i, dev_len, devices; 709 710 parm = get_num_devices(codec, nid); 711 if (!parm) /* not multi-stream capable */ 712 return 0; 713 714 dev_len = parm + 1; 715 dev_len = dev_len < max_devices ? dev_len : max_devices; 716 717 devices = 0; 718 while (devices < dev_len) { 719 parm = snd_hda_codec_read(codec, nid, 0, 720 AC_VERB_GET_DEVICE_LIST, devices); 721 if (parm == -1 && codec->bus->rirb_error) 722 break; 723 724 for (i = 0; i < 8; i++) { 725 dev_list[devices] = (u8)parm; 726 parm >>= 4; 727 devices++; 728 if (devices >= dev_len) 729 break; 730 } 731 } 732 return devices; 733 } 734 735 /** 736 * snd_hda_queue_unsol_event - add an unsolicited event to queue 737 * @bus: the BUS 738 * @res: unsolicited event (lower 32bit of RIRB entry) 739 * @res_ex: codec addr and flags (upper 32bit or RIRB entry) 740 * 741 * Adds the given event to the queue. The events are processed in 742 * the workqueue asynchronously. Call this function in the interrupt 743 * hanlder when RIRB receives an unsolicited event. 744 * 745 * Returns 0 if successful, or a negative error code. 746 */ 747 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex) 748 { 749 struct hda_bus_unsolicited *unsol; 750 unsigned int wp; 751 752 if (!bus || !bus->workq) 753 return 0; 754 755 trace_hda_unsol_event(bus, res, res_ex); 756 unsol = bus->unsol; 757 if (!unsol) 758 return 0; 759 760 wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE; 761 unsol->wp = wp; 762 763 wp <<= 1; 764 unsol->queue[wp] = res; 765 unsol->queue[wp + 1] = res_ex; 766 767 queue_work(bus->workq, &unsol->work); 768 769 return 0; 770 } 771 EXPORT_SYMBOL_GPL(snd_hda_queue_unsol_event); 772 773 /* 774 * process queued unsolicited events 775 */ 776 static void process_unsol_events(struct work_struct *work) 777 { 778 struct hda_bus_unsolicited *unsol = 779 container_of(work, struct hda_bus_unsolicited, work); 780 struct hda_bus *bus = unsol->bus; 781 struct hda_codec *codec; 782 unsigned int rp, caddr, res; 783 784 while (unsol->rp != unsol->wp) { 785 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE; 786 unsol->rp = rp; 787 rp <<= 1; 788 res = unsol->queue[rp]; 789 caddr = unsol->queue[rp + 1]; 790 if (!(caddr & (1 << 4))) /* no unsolicited event? */ 791 continue; 792 codec = bus->caddr_tbl[caddr & 0x0f]; 793 if (codec && codec->patch_ops.unsol_event) 794 codec->patch_ops.unsol_event(codec, res); 795 } 796 } 797 798 /* 799 * initialize unsolicited queue 800 */ 801 static int init_unsol_queue(struct hda_bus *bus) 802 { 803 struct hda_bus_unsolicited *unsol; 804 805 if (bus->unsol) /* already initialized */ 806 return 0; 807 808 unsol = kzalloc(sizeof(*unsol), GFP_KERNEL); 809 if (!unsol) { 810 dev_err(bus->card->dev, "can't allocate unsolicited queue\n"); 811 return -ENOMEM; 812 } 813 INIT_WORK(&unsol->work, process_unsol_events); 814 unsol->bus = bus; 815 bus->unsol = unsol; 816 return 0; 817 } 818 819 /* 820 * destructor 821 */ 822 static void snd_hda_bus_free(struct hda_bus *bus) 823 { 824 if (!bus) 825 return; 826 827 WARN_ON(!list_empty(&bus->codec_list)); 828 if (bus->workq) 829 flush_workqueue(bus->workq); 830 if (bus->unsol) 831 kfree(bus->unsol); 832 if (bus->ops.private_free) 833 bus->ops.private_free(bus); 834 if (bus->workq) 835 destroy_workqueue(bus->workq); 836 837 kfree(bus); 838 } 839 840 static int snd_hda_bus_dev_free(struct snd_device *device) 841 { 842 snd_hda_bus_free(device->device_data); 843 return 0; 844 } 845 846 static int snd_hda_bus_dev_disconnect(struct snd_device *device) 847 { 848 struct hda_bus *bus = device->device_data; 849 bus->shutdown = 1; 850 return 0; 851 } 852 853 /** 854 * snd_hda_bus_new - create a HDA bus 855 * @card: the card entry 856 * @temp: the template for hda_bus information 857 * @busp: the pointer to store the created bus instance 858 * 859 * Returns 0 if successful, or a negative error code. 860 */ 861 int snd_hda_bus_new(struct snd_card *card, 862 const struct hda_bus_template *temp, 863 struct hda_bus **busp) 864 { 865 struct hda_bus *bus; 866 int err; 867 static struct snd_device_ops dev_ops = { 868 .dev_disconnect = snd_hda_bus_dev_disconnect, 869 .dev_free = snd_hda_bus_dev_free, 870 }; 871 872 if (snd_BUG_ON(!temp)) 873 return -EINVAL; 874 if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response)) 875 return -EINVAL; 876 877 if (busp) 878 *busp = NULL; 879 880 bus = kzalloc(sizeof(*bus), GFP_KERNEL); 881 if (bus == NULL) { 882 dev_err(card->dev, "can't allocate struct hda_bus\n"); 883 return -ENOMEM; 884 } 885 886 bus->card = card; 887 bus->private_data = temp->private_data; 888 bus->pci = temp->pci; 889 bus->modelname = temp->modelname; 890 bus->power_save = temp->power_save; 891 bus->ops = temp->ops; 892 893 mutex_init(&bus->cmd_mutex); 894 mutex_init(&bus->prepare_mutex); 895 INIT_LIST_HEAD(&bus->codec_list); 896 897 snprintf(bus->workq_name, sizeof(bus->workq_name), 898 "hd-audio%d", card->number); 899 bus->workq = create_singlethread_workqueue(bus->workq_name); 900 if (!bus->workq) { 901 dev_err(card->dev, "cannot create workqueue %s\n", 902 bus->workq_name); 903 kfree(bus); 904 return -ENOMEM; 905 } 906 907 err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops); 908 if (err < 0) { 909 snd_hda_bus_free(bus); 910 return err; 911 } 912 if (busp) 913 *busp = bus; 914 return 0; 915 } 916 EXPORT_SYMBOL_GPL(snd_hda_bus_new); 917 918 #if IS_ENABLED(CONFIG_SND_HDA_GENERIC) 919 #define is_generic_config(codec) \ 920 (codec->modelname && !strcmp(codec->modelname, "generic")) 921 #else 922 #define is_generic_config(codec) 0 923 #endif 924 925 #ifdef MODULE 926 #define HDA_MODREQ_MAX_COUNT 2 /* two request_modules()'s */ 927 #else 928 #define HDA_MODREQ_MAX_COUNT 0 /* all presets are statically linked */ 929 #endif 930 931 /* 932 * find a matching codec preset 933 */ 934 static const struct hda_codec_preset * 935 find_codec_preset(struct hda_codec *codec) 936 { 937 struct hda_codec_preset_list *tbl; 938 const struct hda_codec_preset *preset; 939 unsigned int mod_requested = 0; 940 941 again: 942 mutex_lock(&preset_mutex); 943 list_for_each_entry(tbl, &hda_preset_tables, list) { 944 if (!try_module_get(tbl->owner)) { 945 codec_err(codec, "cannot module_get\n"); 946 continue; 947 } 948 for (preset = tbl->preset; preset->id; preset++) { 949 u32 mask = preset->mask; 950 if (preset->afg && preset->afg != codec->afg) 951 continue; 952 if (preset->mfg && preset->mfg != codec->mfg) 953 continue; 954 if (!mask) 955 mask = ~0; 956 if (preset->id == (codec->vendor_id & mask) && 957 (!preset->rev || 958 preset->rev == codec->revision_id)) { 959 mutex_unlock(&preset_mutex); 960 codec->owner = tbl->owner; 961 return preset; 962 } 963 } 964 module_put(tbl->owner); 965 } 966 mutex_unlock(&preset_mutex); 967 968 if (mod_requested < HDA_MODREQ_MAX_COUNT) { 969 char name[32]; 970 if (!mod_requested) 971 snprintf(name, sizeof(name), "snd-hda-codec-id:%08x", 972 codec->vendor_id); 973 else 974 snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*", 975 (codec->vendor_id >> 16) & 0xffff); 976 request_module(name); 977 mod_requested++; 978 goto again; 979 } 980 return NULL; 981 } 982 983 /* 984 * get_codec_name - store the codec name 985 */ 986 static int get_codec_name(struct hda_codec *codec) 987 { 988 const struct hda_vendor_id *c; 989 const char *vendor = NULL; 990 u16 vendor_id = codec->vendor_id >> 16; 991 char tmp[16]; 992 993 if (codec->vendor_name) 994 goto get_chip_name; 995 996 for (c = hda_vendor_ids; c->id; c++) { 997 if (c->id == vendor_id) { 998 vendor = c->name; 999 break; 1000 } 1001 } 1002 if (!vendor) { 1003 sprintf(tmp, "Generic %04x", vendor_id); 1004 vendor = tmp; 1005 } 1006 codec->vendor_name = kstrdup(vendor, GFP_KERNEL); 1007 if (!codec->vendor_name) 1008 return -ENOMEM; 1009 1010 get_chip_name: 1011 if (codec->chip_name) 1012 return 0; 1013 1014 if (codec->preset && codec->preset->name) 1015 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL); 1016 else { 1017 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff); 1018 codec->chip_name = kstrdup(tmp, GFP_KERNEL); 1019 } 1020 if (!codec->chip_name) 1021 return -ENOMEM; 1022 return 0; 1023 } 1024 1025 /* 1026 * look for an AFG and MFG nodes 1027 */ 1028 static void setup_fg_nodes(struct hda_codec *codec) 1029 { 1030 int i, total_nodes, function_id; 1031 hda_nid_t nid; 1032 1033 total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid); 1034 for (i = 0; i < total_nodes; i++, nid++) { 1035 function_id = snd_hda_param_read(codec, nid, 1036 AC_PAR_FUNCTION_TYPE); 1037 switch (function_id & 0xff) { 1038 case AC_GRP_AUDIO_FUNCTION: 1039 codec->afg = nid; 1040 codec->afg_function_id = function_id & 0xff; 1041 codec->afg_unsol = (function_id >> 8) & 1; 1042 break; 1043 case AC_GRP_MODEM_FUNCTION: 1044 codec->mfg = nid; 1045 codec->mfg_function_id = function_id & 0xff; 1046 codec->mfg_unsol = (function_id >> 8) & 1; 1047 break; 1048 default: 1049 break; 1050 } 1051 } 1052 } 1053 1054 /* 1055 * read widget caps for each widget and store in cache 1056 */ 1057 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node) 1058 { 1059 int i; 1060 hda_nid_t nid; 1061 1062 codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node, 1063 &codec->start_nid); 1064 codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL); 1065 if (!codec->wcaps) 1066 return -ENOMEM; 1067 nid = codec->start_nid; 1068 for (i = 0; i < codec->num_nodes; i++, nid++) 1069 codec->wcaps[i] = snd_hda_param_read(codec, nid, 1070 AC_PAR_AUDIO_WIDGET_CAP); 1071 return 0; 1072 } 1073 1074 /* read all pin default configurations and save codec->init_pins */ 1075 static int read_pin_defaults(struct hda_codec *codec) 1076 { 1077 int i; 1078 hda_nid_t nid = codec->start_nid; 1079 1080 for (i = 0; i < codec->num_nodes; i++, nid++) { 1081 struct hda_pincfg *pin; 1082 unsigned int wcaps = get_wcaps(codec, nid); 1083 unsigned int wid_type = get_wcaps_type(wcaps); 1084 if (wid_type != AC_WID_PIN) 1085 continue; 1086 pin = snd_array_new(&codec->init_pins); 1087 if (!pin) 1088 return -ENOMEM; 1089 pin->nid = nid; 1090 pin->cfg = snd_hda_codec_read(codec, nid, 0, 1091 AC_VERB_GET_CONFIG_DEFAULT, 0); 1092 pin->ctrl = snd_hda_codec_read(codec, nid, 0, 1093 AC_VERB_GET_PIN_WIDGET_CONTROL, 1094 0); 1095 } 1096 return 0; 1097 } 1098 1099 /* look up the given pin config list and return the item matching with NID */ 1100 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec, 1101 struct snd_array *array, 1102 hda_nid_t nid) 1103 { 1104 int i; 1105 for (i = 0; i < array->used; i++) { 1106 struct hda_pincfg *pin = snd_array_elem(array, i); 1107 if (pin->nid == nid) 1108 return pin; 1109 } 1110 return NULL; 1111 } 1112 1113 /* set the current pin config value for the given NID. 1114 * the value is cached, and read via snd_hda_codec_get_pincfg() 1115 */ 1116 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list, 1117 hda_nid_t nid, unsigned int cfg) 1118 { 1119 struct hda_pincfg *pin; 1120 1121 /* the check below may be invalid when pins are added by a fixup 1122 * dynamically (e.g. via snd_hda_codec_update_widgets()), so disabled 1123 * for now 1124 */ 1125 /* 1126 if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN) 1127 return -EINVAL; 1128 */ 1129 1130 pin = look_up_pincfg(codec, list, nid); 1131 if (!pin) { 1132 pin = snd_array_new(list); 1133 if (!pin) 1134 return -ENOMEM; 1135 pin->nid = nid; 1136 } 1137 pin->cfg = cfg; 1138 return 0; 1139 } 1140 1141 /** 1142 * snd_hda_codec_set_pincfg - Override a pin default configuration 1143 * @codec: the HDA codec 1144 * @nid: NID to set the pin config 1145 * @cfg: the pin default config value 1146 * 1147 * Override a pin default configuration value in the cache. 1148 * This value can be read by snd_hda_codec_get_pincfg() in a higher 1149 * priority than the real hardware value. 1150 */ 1151 int snd_hda_codec_set_pincfg(struct hda_codec *codec, 1152 hda_nid_t nid, unsigned int cfg) 1153 { 1154 return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg); 1155 } 1156 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pincfg); 1157 1158 /** 1159 * snd_hda_codec_get_pincfg - Obtain a pin-default configuration 1160 * @codec: the HDA codec 1161 * @nid: NID to get the pin config 1162 * 1163 * Get the current pin config value of the given pin NID. 1164 * If the pincfg value is cached or overridden via sysfs or driver, 1165 * returns the cached value. 1166 */ 1167 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid) 1168 { 1169 struct hda_pincfg *pin; 1170 1171 #ifdef CONFIG_SND_HDA_RECONFIG 1172 { 1173 unsigned int cfg = 0; 1174 mutex_lock(&codec->user_mutex); 1175 pin = look_up_pincfg(codec, &codec->user_pins, nid); 1176 if (pin) 1177 cfg = pin->cfg; 1178 mutex_unlock(&codec->user_mutex); 1179 if (cfg) 1180 return cfg; 1181 } 1182 #endif 1183 pin = look_up_pincfg(codec, &codec->driver_pins, nid); 1184 if (pin) 1185 return pin->cfg; 1186 pin = look_up_pincfg(codec, &codec->init_pins, nid); 1187 if (pin) 1188 return pin->cfg; 1189 return 0; 1190 } 1191 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pincfg); 1192 1193 /* remember the current pinctl target value */ 1194 int snd_hda_codec_set_pin_target(struct hda_codec *codec, hda_nid_t nid, 1195 unsigned int val) 1196 { 1197 struct hda_pincfg *pin; 1198 1199 pin = look_up_pincfg(codec, &codec->init_pins, nid); 1200 if (!pin) 1201 return -EINVAL; 1202 pin->target = val; 1203 return 0; 1204 } 1205 EXPORT_SYMBOL_GPL(snd_hda_codec_set_pin_target); 1206 1207 /* return the current pinctl target value */ 1208 int snd_hda_codec_get_pin_target(struct hda_codec *codec, hda_nid_t nid) 1209 { 1210 struct hda_pincfg *pin; 1211 1212 pin = look_up_pincfg(codec, &codec->init_pins, nid); 1213 if (!pin) 1214 return 0; 1215 return pin->target; 1216 } 1217 EXPORT_SYMBOL_GPL(snd_hda_codec_get_pin_target); 1218 1219 /** 1220 * snd_hda_shutup_pins - Shut up all pins 1221 * @codec: the HDA codec 1222 * 1223 * Clear all pin controls to shup up before suspend for avoiding click noise. 1224 * The controls aren't cached so that they can be resumed properly. 1225 */ 1226 void snd_hda_shutup_pins(struct hda_codec *codec) 1227 { 1228 int i; 1229 /* don't shut up pins when unloading the driver; otherwise it breaks 1230 * the default pin setup at the next load of the driver 1231 */ 1232 if (codec->bus->shutdown) 1233 return; 1234 for (i = 0; i < codec->init_pins.used; i++) { 1235 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); 1236 /* use read here for syncing after issuing each verb */ 1237 snd_hda_codec_read(codec, pin->nid, 0, 1238 AC_VERB_SET_PIN_WIDGET_CONTROL, 0); 1239 } 1240 codec->pins_shutup = 1; 1241 } 1242 EXPORT_SYMBOL_GPL(snd_hda_shutup_pins); 1243 1244 #ifdef CONFIG_PM 1245 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */ 1246 static void restore_shutup_pins(struct hda_codec *codec) 1247 { 1248 int i; 1249 if (!codec->pins_shutup) 1250 return; 1251 if (codec->bus->shutdown) 1252 return; 1253 for (i = 0; i < codec->init_pins.used; i++) { 1254 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i); 1255 snd_hda_codec_write(codec, pin->nid, 0, 1256 AC_VERB_SET_PIN_WIDGET_CONTROL, 1257 pin->ctrl); 1258 } 1259 codec->pins_shutup = 0; 1260 } 1261 #endif 1262 1263 static void hda_jackpoll_work(struct work_struct *work) 1264 { 1265 struct hda_codec *codec = 1266 container_of(work, struct hda_codec, jackpoll_work.work); 1267 1268 snd_hda_jack_set_dirty_all(codec); 1269 snd_hda_jack_poll_all(codec); 1270 1271 if (!codec->jackpoll_interval) 1272 return; 1273 1274 queue_delayed_work(codec->bus->workq, &codec->jackpoll_work, 1275 codec->jackpoll_interval); 1276 } 1277 1278 static void init_hda_cache(struct hda_cache_rec *cache, 1279 unsigned int record_size); 1280 static void free_hda_cache(struct hda_cache_rec *cache); 1281 1282 /* release all pincfg lists */ 1283 static void free_init_pincfgs(struct hda_codec *codec) 1284 { 1285 snd_array_free(&codec->driver_pins); 1286 #ifdef CONFIG_SND_HDA_RECONFIG 1287 snd_array_free(&codec->user_pins); 1288 #endif 1289 snd_array_free(&codec->init_pins); 1290 } 1291 1292 /* 1293 * audio-converter setup caches 1294 */ 1295 struct hda_cvt_setup { 1296 hda_nid_t nid; 1297 u8 stream_tag; 1298 u8 channel_id; 1299 u16 format_id; 1300 unsigned char active; /* cvt is currently used */ 1301 unsigned char dirty; /* setups should be cleared */ 1302 }; 1303 1304 /* get or create a cache entry for the given audio converter NID */ 1305 static struct hda_cvt_setup * 1306 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid) 1307 { 1308 struct hda_cvt_setup *p; 1309 int i; 1310 1311 for (i = 0; i < codec->cvt_setups.used; i++) { 1312 p = snd_array_elem(&codec->cvt_setups, i); 1313 if (p->nid == nid) 1314 return p; 1315 } 1316 p = snd_array_new(&codec->cvt_setups); 1317 if (p) 1318 p->nid = nid; 1319 return p; 1320 } 1321 1322 /* 1323 * Dynamic symbol binding for the codec parsers 1324 */ 1325 1326 #define load_parser(codec, sym) \ 1327 ((codec)->parser = (int (*)(struct hda_codec *))symbol_request(sym)) 1328 1329 static void unload_parser(struct hda_codec *codec) 1330 { 1331 if (codec->parser) 1332 symbol_put_addr(codec->parser); 1333 codec->parser = NULL; 1334 } 1335 1336 /* 1337 * codec destructor 1338 */ 1339 static void snd_hda_codec_free(struct hda_codec *codec) 1340 { 1341 if (!codec) 1342 return; 1343 cancel_delayed_work_sync(&codec->jackpoll_work); 1344 snd_hda_jack_tbl_clear(codec); 1345 free_init_pincfgs(codec); 1346 #ifdef CONFIG_PM 1347 cancel_delayed_work(&codec->power_work); 1348 flush_workqueue(codec->bus->workq); 1349 #endif 1350 list_del(&codec->list); 1351 snd_array_free(&codec->mixers); 1352 snd_array_free(&codec->nids); 1353 snd_array_free(&codec->cvt_setups); 1354 snd_array_free(&codec->spdif_out); 1355 remove_conn_list(codec); 1356 codec->bus->caddr_tbl[codec->addr] = NULL; 1357 if (codec->patch_ops.free) 1358 codec->patch_ops.free(codec); 1359 hda_call_pm_notify(codec, false); /* cancel leftover refcounts */ 1360 snd_hda_sysfs_clear(codec); 1361 unload_parser(codec); 1362 module_put(codec->owner); 1363 free_hda_cache(&codec->amp_cache); 1364 free_hda_cache(&codec->cmd_cache); 1365 kfree(codec->vendor_name); 1366 kfree(codec->chip_name); 1367 kfree(codec->modelname); 1368 kfree(codec->wcaps); 1369 codec->bus->num_codecs--; 1370 put_device(&codec->dev); 1371 } 1372 1373 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, 1374 hda_nid_t fg, unsigned int power_state); 1375 1376 static unsigned int hda_set_power_state(struct hda_codec *codec, 1377 unsigned int power_state); 1378 1379 static int snd_hda_codec_dev_register(struct snd_device *device) 1380 { 1381 struct hda_codec *codec = device->device_data; 1382 int err = device_add(&codec->dev); 1383 1384 if (err < 0) 1385 return err; 1386 snd_hda_register_beep_device(codec); 1387 return 0; 1388 } 1389 1390 static int snd_hda_codec_dev_disconnect(struct snd_device *device) 1391 { 1392 struct hda_codec *codec = device->device_data; 1393 1394 snd_hda_detach_beep_device(codec); 1395 device_del(&codec->dev); 1396 return 0; 1397 } 1398 1399 static int snd_hda_codec_dev_free(struct snd_device *device) 1400 { 1401 snd_hda_codec_free(device->device_data); 1402 return 0; 1403 } 1404 1405 /* just free the container */ 1406 static void snd_hda_codec_dev_release(struct device *dev) 1407 { 1408 kfree(container_of(dev, struct hda_codec, dev)); 1409 } 1410 1411 /** 1412 * snd_hda_codec_new - create a HDA codec 1413 * @bus: the bus to assign 1414 * @codec_addr: the codec address 1415 * @codecp: the pointer to store the generated codec 1416 * 1417 * Returns 0 if successful, or a negative error code. 1418 */ 1419 int snd_hda_codec_new(struct hda_bus *bus, 1420 unsigned int codec_addr, 1421 struct hda_codec **codecp) 1422 { 1423 struct hda_codec *codec; 1424 char component[31]; 1425 hda_nid_t fg; 1426 int err; 1427 static struct snd_device_ops dev_ops = { 1428 .dev_register = snd_hda_codec_dev_register, 1429 .dev_disconnect = snd_hda_codec_dev_disconnect, 1430 .dev_free = snd_hda_codec_dev_free, 1431 }; 1432 1433 if (snd_BUG_ON(!bus)) 1434 return -EINVAL; 1435 if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS)) 1436 return -EINVAL; 1437 1438 if (bus->caddr_tbl[codec_addr]) { 1439 dev_err(bus->card->dev, 1440 "address 0x%x is already occupied\n", 1441 codec_addr); 1442 return -EBUSY; 1443 } 1444 1445 codec = kzalloc(sizeof(*codec), GFP_KERNEL); 1446 if (codec == NULL) { 1447 dev_err(bus->card->dev, "can't allocate struct hda_codec\n"); 1448 return -ENOMEM; 1449 } 1450 1451 device_initialize(&codec->dev); 1452 codec->dev.parent = &bus->card->card_dev; 1453 codec->dev.class = sound_class; 1454 codec->dev.release = snd_hda_codec_dev_release; 1455 codec->dev.groups = snd_hda_dev_attr_groups; 1456 dev_set_name(&codec->dev, "hdaudioC%dD%d", bus->card->number, 1457 codec_addr); 1458 dev_set_drvdata(&codec->dev, codec); /* for sysfs */ 1459 1460 codec->bus = bus; 1461 codec->addr = codec_addr; 1462 mutex_init(&codec->spdif_mutex); 1463 mutex_init(&codec->control_mutex); 1464 mutex_init(&codec->hash_mutex); 1465 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); 1466 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); 1467 snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32); 1468 snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32); 1469 snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16); 1470 snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16); 1471 snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8); 1472 snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16); 1473 snd_array_init(&codec->jacktbl, sizeof(struct hda_jack_tbl), 16); 1474 snd_array_init(&codec->verbs, sizeof(struct hda_verb *), 8); 1475 INIT_LIST_HEAD(&codec->conn_list); 1476 1477 INIT_DELAYED_WORK(&codec->jackpoll_work, hda_jackpoll_work); 1478 codec->depop_delay = -1; 1479 codec->fixup_id = HDA_FIXUP_ID_NOT_SET; 1480 1481 #ifdef CONFIG_PM 1482 spin_lock_init(&codec->power_lock); 1483 INIT_DELAYED_WORK(&codec->power_work, hda_power_work); 1484 /* snd_hda_codec_new() marks the codec as power-up, and leave it as is. 1485 * the caller has to power down appropriatley after initialization 1486 * phase. 1487 */ 1488 hda_keep_power_on(codec); 1489 #endif 1490 1491 snd_hda_sysfs_init(codec); 1492 1493 if (codec->bus->modelname) { 1494 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL); 1495 if (!codec->modelname) { 1496 err = -ENODEV; 1497 goto error; 1498 } 1499 } 1500 1501 list_add_tail(&codec->list, &bus->codec_list); 1502 bus->num_codecs++; 1503 1504 bus->caddr_tbl[codec_addr] = codec; 1505 1506 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, 1507 AC_PAR_VENDOR_ID); 1508 if (codec->vendor_id == -1) 1509 /* read again, hopefully the access method was corrected 1510 * in the last read... 1511 */ 1512 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, 1513 AC_PAR_VENDOR_ID); 1514 codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, 1515 AC_PAR_SUBSYSTEM_ID); 1516 codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, 1517 AC_PAR_REV_ID); 1518 1519 setup_fg_nodes(codec); 1520 if (!codec->afg && !codec->mfg) { 1521 dev_err(bus->card->dev, "no AFG or MFG node found\n"); 1522 err = -ENODEV; 1523 goto error; 1524 } 1525 1526 fg = codec->afg ? codec->afg : codec->mfg; 1527 err = read_widget_caps(codec, fg); 1528 if (err < 0) { 1529 dev_err(bus->card->dev, "cannot malloc\n"); 1530 goto error; 1531 } 1532 err = read_pin_defaults(codec); 1533 if (err < 0) 1534 goto error; 1535 1536 if (!codec->subsystem_id) { 1537 codec->subsystem_id = 1538 snd_hda_codec_read(codec, fg, 0, 1539 AC_VERB_GET_SUBSYSTEM_ID, 0); 1540 } 1541 1542 #ifdef CONFIG_PM 1543 codec->d3_stop_clk = snd_hda_codec_get_supported_ps(codec, fg, 1544 AC_PWRST_CLKSTOP); 1545 #endif 1546 codec->epss = snd_hda_codec_get_supported_ps(codec, fg, 1547 AC_PWRST_EPSS); 1548 #ifdef CONFIG_PM 1549 if (!codec->d3_stop_clk || !codec->epss) 1550 bus->power_keep_link_on = 1; 1551 #endif 1552 1553 1554 /* power-up all before initialization */ 1555 hda_set_power_state(codec, AC_PWRST_D0); 1556 1557 snd_hda_codec_proc_new(codec); 1558 1559 snd_hda_create_hwdep(codec); 1560 1561 sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, 1562 codec->subsystem_id, codec->revision_id); 1563 snd_component_add(codec->bus->card, component); 1564 1565 err = snd_device_new(bus->card, SNDRV_DEV_CODEC, codec, &dev_ops); 1566 if (err < 0) 1567 goto error; 1568 1569 if (codecp) 1570 *codecp = codec; 1571 return 0; 1572 1573 error: 1574 snd_hda_codec_free(codec); 1575 return err; 1576 } 1577 EXPORT_SYMBOL_GPL(snd_hda_codec_new); 1578 1579 int snd_hda_codec_update_widgets(struct hda_codec *codec) 1580 { 1581 hda_nid_t fg; 1582 int err; 1583 1584 /* Assume the function group node does not change, 1585 * only the widget nodes may change. 1586 */ 1587 kfree(codec->wcaps); 1588 fg = codec->afg ? codec->afg : codec->mfg; 1589 err = read_widget_caps(codec, fg); 1590 if (err < 0) { 1591 codec_err(codec, "cannot malloc\n"); 1592 return err; 1593 } 1594 1595 snd_array_free(&codec->init_pins); 1596 err = read_pin_defaults(codec); 1597 1598 return err; 1599 } 1600 EXPORT_SYMBOL_GPL(snd_hda_codec_update_widgets); 1601 1602 1603 #if IS_ENABLED(CONFIG_SND_HDA_CODEC_HDMI) 1604 /* if all audio out widgets are digital, let's assume the codec as a HDMI/DP */ 1605 static bool is_likely_hdmi_codec(struct hda_codec *codec) 1606 { 1607 hda_nid_t nid = codec->start_nid; 1608 int i; 1609 1610 for (i = 0; i < codec->num_nodes; i++, nid++) { 1611 unsigned int wcaps = get_wcaps(codec, nid); 1612 switch (get_wcaps_type(wcaps)) { 1613 case AC_WID_AUD_IN: 1614 return false; /* HDMI parser supports only HDMI out */ 1615 case AC_WID_AUD_OUT: 1616 if (!(wcaps & AC_WCAP_DIGITAL)) 1617 return false; 1618 break; 1619 } 1620 } 1621 return true; 1622 } 1623 #else 1624 /* no HDMI codec parser support */ 1625 #define is_likely_hdmi_codec(codec) false 1626 #endif /* CONFIG_SND_HDA_CODEC_HDMI */ 1627 1628 /** 1629 * snd_hda_codec_configure - (Re-)configure the HD-audio codec 1630 * @codec: the HDA codec 1631 * 1632 * Start parsing of the given codec tree and (re-)initialize the whole 1633 * patch instance. 1634 * 1635 * Returns 0 if successful or a negative error code. 1636 */ 1637 int snd_hda_codec_configure(struct hda_codec *codec) 1638 { 1639 int (*patch)(struct hda_codec *) = NULL; 1640 int err; 1641 1642 codec->preset = find_codec_preset(codec); 1643 if (!codec->vendor_name || !codec->chip_name) { 1644 err = get_codec_name(codec); 1645 if (err < 0) 1646 return err; 1647 } 1648 1649 if (!is_generic_config(codec) && codec->preset) 1650 patch = codec->preset->patch; 1651 if (!patch) { 1652 unload_parser(codec); /* to be sure */ 1653 if (is_likely_hdmi_codec(codec)) { 1654 #if IS_MODULE(CONFIG_SND_HDA_CODEC_HDMI) 1655 patch = load_parser(codec, snd_hda_parse_hdmi_codec); 1656 #elif IS_BUILTIN(CONFIG_SND_HDA_CODEC_HDMI) 1657 patch = snd_hda_parse_hdmi_codec; 1658 #endif 1659 } 1660 if (!patch) { 1661 #if IS_MODULE(CONFIG_SND_HDA_GENERIC) 1662 patch = load_parser(codec, snd_hda_parse_generic_codec); 1663 #elif IS_BUILTIN(CONFIG_SND_HDA_GENERIC) 1664 patch = snd_hda_parse_generic_codec; 1665 #endif 1666 } 1667 if (!patch) { 1668 codec_err(codec, "No codec parser is available\n"); 1669 return -ENODEV; 1670 } 1671 } 1672 1673 err = patch(codec); 1674 if (err < 0) { 1675 unload_parser(codec); 1676 return err; 1677 } 1678 1679 if (codec->patch_ops.unsol_event) { 1680 err = init_unsol_queue(codec->bus); 1681 if (err < 0) 1682 return err; 1683 } 1684 1685 /* audio codec should override the mixer name */ 1686 if (codec->afg || !*codec->bus->card->mixername) 1687 snprintf(codec->bus->card->mixername, 1688 sizeof(codec->bus->card->mixername), 1689 "%s %s", codec->vendor_name, codec->chip_name); 1690 return 0; 1691 } 1692 EXPORT_SYMBOL_GPL(snd_hda_codec_configure); 1693 1694 /* update the stream-id if changed */ 1695 static void update_pcm_stream_id(struct hda_codec *codec, 1696 struct hda_cvt_setup *p, hda_nid_t nid, 1697 u32 stream_tag, int channel_id) 1698 { 1699 unsigned int oldval, newval; 1700 1701 if (p->stream_tag != stream_tag || p->channel_id != channel_id) { 1702 oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0); 1703 newval = (stream_tag << 4) | channel_id; 1704 if (oldval != newval) 1705 snd_hda_codec_write(codec, nid, 0, 1706 AC_VERB_SET_CHANNEL_STREAMID, 1707 newval); 1708 p->stream_tag = stream_tag; 1709 p->channel_id = channel_id; 1710 } 1711 } 1712 1713 /* update the format-id if changed */ 1714 static void update_pcm_format(struct hda_codec *codec, struct hda_cvt_setup *p, 1715 hda_nid_t nid, int format) 1716 { 1717 unsigned int oldval; 1718 1719 if (p->format_id != format) { 1720 oldval = snd_hda_codec_read(codec, nid, 0, 1721 AC_VERB_GET_STREAM_FORMAT, 0); 1722 if (oldval != format) { 1723 msleep(1); 1724 snd_hda_codec_write(codec, nid, 0, 1725 AC_VERB_SET_STREAM_FORMAT, 1726 format); 1727 } 1728 p->format_id = format; 1729 } 1730 } 1731 1732 /** 1733 * snd_hda_codec_setup_stream - set up the codec for streaming 1734 * @codec: the CODEC to set up 1735 * @nid: the NID to set up 1736 * @stream_tag: stream tag to pass, it's between 0x1 and 0xf. 1737 * @channel_id: channel id to pass, zero based. 1738 * @format: stream format. 1739 */ 1740 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, 1741 u32 stream_tag, 1742 int channel_id, int format) 1743 { 1744 struct hda_codec *c; 1745 struct hda_cvt_setup *p; 1746 int type; 1747 int i; 1748 1749 if (!nid) 1750 return; 1751 1752 codec_dbg(codec, 1753 "hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n", 1754 nid, stream_tag, channel_id, format); 1755 p = get_hda_cvt_setup(codec, nid); 1756 if (!p) 1757 return; 1758 1759 if (codec->pcm_format_first) 1760 update_pcm_format(codec, p, nid, format); 1761 update_pcm_stream_id(codec, p, nid, stream_tag, channel_id); 1762 if (!codec->pcm_format_first) 1763 update_pcm_format(codec, p, nid, format); 1764 1765 p->active = 1; 1766 p->dirty = 0; 1767 1768 /* make other inactive cvts with the same stream-tag dirty */ 1769 type = get_wcaps_type(get_wcaps(codec, nid)); 1770 list_for_each_entry(c, &codec->bus->codec_list, list) { 1771 for (i = 0; i < c->cvt_setups.used; i++) { 1772 p = snd_array_elem(&c->cvt_setups, i); 1773 if (!p->active && p->stream_tag == stream_tag && 1774 get_wcaps_type(get_wcaps(c, p->nid)) == type) 1775 p->dirty = 1; 1776 } 1777 } 1778 } 1779 EXPORT_SYMBOL_GPL(snd_hda_codec_setup_stream); 1780 1781 static void really_cleanup_stream(struct hda_codec *codec, 1782 struct hda_cvt_setup *q); 1783 1784 /** 1785 * __snd_hda_codec_cleanup_stream - clean up the codec for closing 1786 * @codec: the CODEC to clean up 1787 * @nid: the NID to clean up 1788 * @do_now: really clean up the stream instead of clearing the active flag 1789 */ 1790 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid, 1791 int do_now) 1792 { 1793 struct hda_cvt_setup *p; 1794 1795 if (!nid) 1796 return; 1797 1798 if (codec->no_sticky_stream) 1799 do_now = 1; 1800 1801 codec_dbg(codec, "hda_codec_cleanup_stream: NID=0x%x\n", nid); 1802 p = get_hda_cvt_setup(codec, nid); 1803 if (p) { 1804 /* here we just clear the active flag when do_now isn't set; 1805 * actual clean-ups will be done later in 1806 * purify_inactive_streams() called from snd_hda_codec_prpapre() 1807 */ 1808 if (do_now) 1809 really_cleanup_stream(codec, p); 1810 else 1811 p->active = 0; 1812 } 1813 } 1814 EXPORT_SYMBOL_GPL(__snd_hda_codec_cleanup_stream); 1815 1816 static void really_cleanup_stream(struct hda_codec *codec, 1817 struct hda_cvt_setup *q) 1818 { 1819 hda_nid_t nid = q->nid; 1820 if (q->stream_tag || q->channel_id) 1821 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0); 1822 if (q->format_id) 1823 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0 1824 ); 1825 memset(q, 0, sizeof(*q)); 1826 q->nid = nid; 1827 } 1828 1829 /* clean up the all conflicting obsolete streams */ 1830 static void purify_inactive_streams(struct hda_codec *codec) 1831 { 1832 struct hda_codec *c; 1833 int i; 1834 1835 list_for_each_entry(c, &codec->bus->codec_list, list) { 1836 for (i = 0; i < c->cvt_setups.used; i++) { 1837 struct hda_cvt_setup *p; 1838 p = snd_array_elem(&c->cvt_setups, i); 1839 if (p->dirty) 1840 really_cleanup_stream(c, p); 1841 } 1842 } 1843 } 1844 1845 #ifdef CONFIG_PM 1846 /* clean up all streams; called from suspend */ 1847 static void hda_cleanup_all_streams(struct hda_codec *codec) 1848 { 1849 int i; 1850 1851 for (i = 0; i < codec->cvt_setups.used; i++) { 1852 struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i); 1853 if (p->stream_tag) 1854 really_cleanup_stream(codec, p); 1855 } 1856 } 1857 #endif 1858 1859 /* 1860 * amp access functions 1861 */ 1862 1863 /* FIXME: more better hash key? */ 1864 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24)) 1865 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24)) 1866 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24)) 1867 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24)) 1868 #define INFO_AMP_CAPS (1<<0) 1869 #define INFO_AMP_VOL(ch) (1 << (1 + (ch))) 1870 1871 /* initialize the hash table */ 1872 static void init_hda_cache(struct hda_cache_rec *cache, 1873 unsigned int record_size) 1874 { 1875 memset(cache, 0, sizeof(*cache)); 1876 memset(cache->hash, 0xff, sizeof(cache->hash)); 1877 snd_array_init(&cache->buf, record_size, 64); 1878 } 1879 1880 static void free_hda_cache(struct hda_cache_rec *cache) 1881 { 1882 snd_array_free(&cache->buf); 1883 } 1884 1885 /* query the hash. allocate an entry if not found. */ 1886 static struct hda_cache_head *get_hash(struct hda_cache_rec *cache, u32 key) 1887 { 1888 u16 idx = key % (u16)ARRAY_SIZE(cache->hash); 1889 u16 cur = cache->hash[idx]; 1890 struct hda_cache_head *info; 1891 1892 while (cur != 0xffff) { 1893 info = snd_array_elem(&cache->buf, cur); 1894 if (info->key == key) 1895 return info; 1896 cur = info->next; 1897 } 1898 return NULL; 1899 } 1900 1901 /* query the hash. allocate an entry if not found. */ 1902 static struct hda_cache_head *get_alloc_hash(struct hda_cache_rec *cache, 1903 u32 key) 1904 { 1905 struct hda_cache_head *info = get_hash(cache, key); 1906 if (!info) { 1907 u16 idx, cur; 1908 /* add a new hash entry */ 1909 info = snd_array_new(&cache->buf); 1910 if (!info) 1911 return NULL; 1912 cur = snd_array_index(&cache->buf, info); 1913 info->key = key; 1914 info->val = 0; 1915 info->dirty = 0; 1916 idx = key % (u16)ARRAY_SIZE(cache->hash); 1917 info->next = cache->hash[idx]; 1918 cache->hash[idx] = cur; 1919 } 1920 return info; 1921 } 1922 1923 /* query and allocate an amp hash entry */ 1924 static inline struct hda_amp_info * 1925 get_alloc_amp_hash(struct hda_codec *codec, u32 key) 1926 { 1927 return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key); 1928 } 1929 1930 /* overwrite the value with the key in the caps hash */ 1931 static int write_caps_hash(struct hda_codec *codec, u32 key, unsigned int val) 1932 { 1933 struct hda_amp_info *info; 1934 1935 mutex_lock(&codec->hash_mutex); 1936 info = get_alloc_amp_hash(codec, key); 1937 if (!info) { 1938 mutex_unlock(&codec->hash_mutex); 1939 return -EINVAL; 1940 } 1941 info->amp_caps = val; 1942 info->head.val |= INFO_AMP_CAPS; 1943 mutex_unlock(&codec->hash_mutex); 1944 return 0; 1945 } 1946 1947 /* query the value from the caps hash; if not found, fetch the current 1948 * value from the given function and store in the hash 1949 */ 1950 static unsigned int 1951 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, int dir, u32 key, 1952 unsigned int (*func)(struct hda_codec *, hda_nid_t, int)) 1953 { 1954 struct hda_amp_info *info; 1955 unsigned int val; 1956 1957 mutex_lock(&codec->hash_mutex); 1958 info = get_alloc_amp_hash(codec, key); 1959 if (!info) { 1960 mutex_unlock(&codec->hash_mutex); 1961 return 0; 1962 } 1963 if (!(info->head.val & INFO_AMP_CAPS)) { 1964 mutex_unlock(&codec->hash_mutex); /* for reentrance */ 1965 val = func(codec, nid, dir); 1966 write_caps_hash(codec, key, val); 1967 } else { 1968 val = info->amp_caps; 1969 mutex_unlock(&codec->hash_mutex); 1970 } 1971 return val; 1972 } 1973 1974 static unsigned int read_amp_cap(struct hda_codec *codec, hda_nid_t nid, 1975 int direction) 1976 { 1977 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD)) 1978 nid = codec->afg; 1979 return snd_hda_param_read(codec, nid, 1980 direction == HDA_OUTPUT ? 1981 AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP); 1982 } 1983 1984 /** 1985 * query_amp_caps - query AMP capabilities 1986 * @codec: the HD-auio codec 1987 * @nid: the NID to query 1988 * @direction: either #HDA_INPUT or #HDA_OUTPUT 1989 * 1990 * Query AMP capabilities for the given widget and direction. 1991 * Returns the obtained capability bits. 1992 * 1993 * When cap bits have been already read, this doesn't read again but 1994 * returns the cached value. 1995 */ 1996 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction) 1997 { 1998 return query_caps_hash(codec, nid, direction, 1999 HDA_HASH_KEY(nid, direction, 0), 2000 read_amp_cap); 2001 } 2002 EXPORT_SYMBOL_GPL(query_amp_caps); 2003 2004 /** 2005 * snd_hda_check_amp_caps - query AMP capabilities 2006 * @codec: the HD-audio codec 2007 * @nid: the NID to query 2008 * @dir: either #HDA_INPUT or #HDA_OUTPUT 2009 * 2010 * Check whether the widget has the given amp capability for the direction. 2011 */ 2012 bool snd_hda_check_amp_caps(struct hda_codec *codec, hda_nid_t nid, 2013 int dir, unsigned int bits) 2014 { 2015 if (!nid) 2016 return false; 2017 if (get_wcaps(codec, nid) & (1 << (dir + 1))) 2018 if (query_amp_caps(codec, nid, dir) & bits) 2019 return true; 2020 return false; 2021 } 2022 EXPORT_SYMBOL_GPL(snd_hda_check_amp_caps); 2023 2024 /** 2025 * snd_hda_override_amp_caps - Override the AMP capabilities 2026 * @codec: the CODEC to clean up 2027 * @nid: the NID to clean up 2028 * @direction: either #HDA_INPUT or #HDA_OUTPUT 2029 * @caps: the capability bits to set 2030 * 2031 * Override the cached AMP caps bits value by the given one. 2032 * This function is useful if the driver needs to adjust the AMP ranges, 2033 * e.g. limit to 0dB, etc. 2034 * 2035 * Returns zero if successful or a negative error code. 2036 */ 2037 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir, 2038 unsigned int caps) 2039 { 2040 return write_caps_hash(codec, HDA_HASH_KEY(nid, dir, 0), caps); 2041 } 2042 EXPORT_SYMBOL_GPL(snd_hda_override_amp_caps); 2043 2044 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid, 2045 int dir) 2046 { 2047 return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP); 2048 } 2049 2050 /** 2051 * snd_hda_query_pin_caps - Query PIN capabilities 2052 * @codec: the HD-auio codec 2053 * @nid: the NID to query 2054 * 2055 * Query PIN capabilities for the given widget. 2056 * Returns the obtained capability bits. 2057 * 2058 * When cap bits have been already read, this doesn't read again but 2059 * returns the cached value. 2060 */ 2061 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid) 2062 { 2063 return query_caps_hash(codec, nid, 0, HDA_HASH_PINCAP_KEY(nid), 2064 read_pin_cap); 2065 } 2066 EXPORT_SYMBOL_GPL(snd_hda_query_pin_caps); 2067 2068 /** 2069 * snd_hda_override_pin_caps - Override the pin capabilities 2070 * @codec: the CODEC 2071 * @nid: the NID to override 2072 * @caps: the capability bits to set 2073 * 2074 * Override the cached PIN capabilitiy bits value by the given one. 2075 * 2076 * Returns zero if successful or a negative error code. 2077 */ 2078 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid, 2079 unsigned int caps) 2080 { 2081 return write_caps_hash(codec, HDA_HASH_PINCAP_KEY(nid), caps); 2082 } 2083 EXPORT_SYMBOL_GPL(snd_hda_override_pin_caps); 2084 2085 /* read or sync the hash value with the current value; 2086 * call within hash_mutex 2087 */ 2088 static struct hda_amp_info * 2089 update_amp_hash(struct hda_codec *codec, hda_nid_t nid, int ch, 2090 int direction, int index, bool init_only) 2091 { 2092 struct hda_amp_info *info; 2093 unsigned int parm, val = 0; 2094 bool val_read = false; 2095 2096 retry: 2097 info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index)); 2098 if (!info) 2099 return NULL; 2100 if (!(info->head.val & INFO_AMP_VOL(ch))) { 2101 if (!val_read) { 2102 mutex_unlock(&codec->hash_mutex); 2103 parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT; 2104 parm |= direction == HDA_OUTPUT ? 2105 AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT; 2106 parm |= index; 2107 val = snd_hda_codec_read(codec, nid, 0, 2108 AC_VERB_GET_AMP_GAIN_MUTE, parm); 2109 val &= 0xff; 2110 val_read = true; 2111 mutex_lock(&codec->hash_mutex); 2112 goto retry; 2113 } 2114 info->vol[ch] = val; 2115 info->head.val |= INFO_AMP_VOL(ch); 2116 } else if (init_only) 2117 return NULL; 2118 return info; 2119 } 2120 2121 /* 2122 * write the current volume in info to the h/w 2123 */ 2124 static void put_vol_mute(struct hda_codec *codec, unsigned int amp_caps, 2125 hda_nid_t nid, int ch, int direction, int index, 2126 int val) 2127 { 2128 u32 parm; 2129 2130 parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT; 2131 parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT; 2132 parm |= index << AC_AMP_SET_INDEX_SHIFT; 2133 if ((val & HDA_AMP_MUTE) && !(amp_caps & AC_AMPCAP_MUTE) && 2134 (amp_caps & AC_AMPCAP_MIN_MUTE)) 2135 ; /* set the zero value as a fake mute */ 2136 else 2137 parm |= val; 2138 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm); 2139 } 2140 2141 /** 2142 * snd_hda_codec_amp_read - Read AMP value 2143 * @codec: HD-audio codec 2144 * @nid: NID to read the AMP value 2145 * @ch: channel (left=0 or right=1) 2146 * @direction: #HDA_INPUT or #HDA_OUTPUT 2147 * @index: the index value (only for input direction) 2148 * 2149 * Read AMP value. The volume is between 0 to 0x7f, 0x80 = mute bit. 2150 */ 2151 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, 2152 int direction, int index) 2153 { 2154 struct hda_amp_info *info; 2155 unsigned int val = 0; 2156 2157 mutex_lock(&codec->hash_mutex); 2158 info = update_amp_hash(codec, nid, ch, direction, index, false); 2159 if (info) 2160 val = info->vol[ch]; 2161 mutex_unlock(&codec->hash_mutex); 2162 return val; 2163 } 2164 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_read); 2165 2166 static int codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, 2167 int direction, int idx, int mask, int val, 2168 bool init_only) 2169 { 2170 struct hda_amp_info *info; 2171 unsigned int caps; 2172 unsigned int cache_only; 2173 2174 if (snd_BUG_ON(mask & ~0xff)) 2175 mask &= 0xff; 2176 val &= mask; 2177 2178 mutex_lock(&codec->hash_mutex); 2179 info = update_amp_hash(codec, nid, ch, direction, idx, init_only); 2180 if (!info) { 2181 mutex_unlock(&codec->hash_mutex); 2182 return 0; 2183 } 2184 val |= info->vol[ch] & ~mask; 2185 if (info->vol[ch] == val) { 2186 mutex_unlock(&codec->hash_mutex); 2187 return 0; 2188 } 2189 info->vol[ch] = val; 2190 cache_only = info->head.dirty = codec->cached_write; 2191 caps = info->amp_caps; 2192 mutex_unlock(&codec->hash_mutex); 2193 if (!cache_only) 2194 put_vol_mute(codec, caps, nid, ch, direction, idx, val); 2195 return 1; 2196 } 2197 2198 /** 2199 * snd_hda_codec_amp_update - update the AMP value 2200 * @codec: HD-audio codec 2201 * @nid: NID to read the AMP value 2202 * @ch: channel (left=0 or right=1) 2203 * @direction: #HDA_INPUT or #HDA_OUTPUT 2204 * @idx: the index value (only for input direction) 2205 * @mask: bit mask to set 2206 * @val: the bits value to set 2207 * 2208 * Update the AMP value with a bit mask. 2209 * Returns 0 if the value is unchanged, 1 if changed. 2210 */ 2211 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, 2212 int direction, int idx, int mask, int val) 2213 { 2214 return codec_amp_update(codec, nid, ch, direction, idx, mask, val, false); 2215 } 2216 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_update); 2217 2218 /** 2219 * snd_hda_codec_amp_stereo - update the AMP stereo values 2220 * @codec: HD-audio codec 2221 * @nid: NID to read the AMP value 2222 * @direction: #HDA_INPUT or #HDA_OUTPUT 2223 * @idx: the index value (only for input direction) 2224 * @mask: bit mask to set 2225 * @val: the bits value to set 2226 * 2227 * Update the AMP values like snd_hda_codec_amp_update(), but for a 2228 * stereo widget with the same mask and value. 2229 */ 2230 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid, 2231 int direction, int idx, int mask, int val) 2232 { 2233 int ch, ret = 0; 2234 2235 if (snd_BUG_ON(mask & ~0xff)) 2236 mask &= 0xff; 2237 for (ch = 0; ch < 2; ch++) 2238 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction, 2239 idx, mask, val); 2240 return ret; 2241 } 2242 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_stereo); 2243 2244 /* Works like snd_hda_codec_amp_update() but it writes the value only at 2245 * the first access. If the amp was already initialized / updated beforehand, 2246 * this does nothing. 2247 */ 2248 int snd_hda_codec_amp_init(struct hda_codec *codec, hda_nid_t nid, int ch, 2249 int dir, int idx, int mask, int val) 2250 { 2251 return codec_amp_update(codec, nid, ch, dir, idx, mask, val, true); 2252 } 2253 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init); 2254 2255 int snd_hda_codec_amp_init_stereo(struct hda_codec *codec, hda_nid_t nid, 2256 int dir, int idx, int mask, int val) 2257 { 2258 int ch, ret = 0; 2259 2260 if (snd_BUG_ON(mask & ~0xff)) 2261 mask &= 0xff; 2262 for (ch = 0; ch < 2; ch++) 2263 ret |= snd_hda_codec_amp_init(codec, nid, ch, dir, 2264 idx, mask, val); 2265 return ret; 2266 } 2267 EXPORT_SYMBOL_GPL(snd_hda_codec_amp_init_stereo); 2268 2269 /** 2270 * snd_hda_codec_resume_amp - Resume all AMP commands from the cache 2271 * @codec: HD-audio codec 2272 * 2273 * Resume the all amp commands from the cache. 2274 */ 2275 void snd_hda_codec_resume_amp(struct hda_codec *codec) 2276 { 2277 int i; 2278 2279 mutex_lock(&codec->hash_mutex); 2280 codec->cached_write = 0; 2281 for (i = 0; i < codec->amp_cache.buf.used; i++) { 2282 struct hda_amp_info *buffer; 2283 u32 key; 2284 hda_nid_t nid; 2285 unsigned int idx, dir, ch; 2286 struct hda_amp_info info; 2287 2288 buffer = snd_array_elem(&codec->amp_cache.buf, i); 2289 if (!buffer->head.dirty) 2290 continue; 2291 buffer->head.dirty = 0; 2292 info = *buffer; 2293 key = info.head.key; 2294 if (!key) 2295 continue; 2296 nid = key & 0xff; 2297 idx = (key >> 16) & 0xff; 2298 dir = (key >> 24) & 0xff; 2299 for (ch = 0; ch < 2; ch++) { 2300 if (!(info.head.val & INFO_AMP_VOL(ch))) 2301 continue; 2302 mutex_unlock(&codec->hash_mutex); 2303 put_vol_mute(codec, info.amp_caps, nid, ch, dir, idx, 2304 info.vol[ch]); 2305 mutex_lock(&codec->hash_mutex); 2306 } 2307 } 2308 mutex_unlock(&codec->hash_mutex); 2309 } 2310 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_amp); 2311 2312 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir, 2313 unsigned int ofs) 2314 { 2315 u32 caps = query_amp_caps(codec, nid, dir); 2316 /* get num steps */ 2317 caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; 2318 if (ofs < caps) 2319 caps -= ofs; 2320 return caps; 2321 } 2322 2323 /** 2324 * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer 2325 * 2326 * The control element is supposed to have the private_value field 2327 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2328 */ 2329 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol, 2330 struct snd_ctl_elem_info *uinfo) 2331 { 2332 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2333 u16 nid = get_amp_nid(kcontrol); 2334 u8 chs = get_amp_channels(kcontrol); 2335 int dir = get_amp_direction(kcontrol); 2336 unsigned int ofs = get_amp_offset(kcontrol); 2337 2338 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 2339 uinfo->count = chs == 3 ? 2 : 1; 2340 uinfo->value.integer.min = 0; 2341 uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs); 2342 if (!uinfo->value.integer.max) { 2343 codec_warn(codec, 2344 "num_steps = 0 for NID=0x%x (ctl = %s)\n", 2345 nid, kcontrol->id.name); 2346 return -EINVAL; 2347 } 2348 return 0; 2349 } 2350 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_info); 2351 2352 2353 static inline unsigned int 2354 read_amp_value(struct hda_codec *codec, hda_nid_t nid, 2355 int ch, int dir, int idx, unsigned int ofs) 2356 { 2357 unsigned int val; 2358 val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx); 2359 val &= HDA_AMP_VOLMASK; 2360 if (val >= ofs) 2361 val -= ofs; 2362 else 2363 val = 0; 2364 return val; 2365 } 2366 2367 static inline int 2368 update_amp_value(struct hda_codec *codec, hda_nid_t nid, 2369 int ch, int dir, int idx, unsigned int ofs, 2370 unsigned int val) 2371 { 2372 unsigned int maxval; 2373 2374 if (val > 0) 2375 val += ofs; 2376 /* ofs = 0: raw max value */ 2377 maxval = get_amp_max_value(codec, nid, dir, 0); 2378 if (val > maxval) 2379 val = maxval; 2380 return snd_hda_codec_amp_update(codec, nid, ch, dir, idx, 2381 HDA_AMP_VOLMASK, val); 2382 } 2383 2384 /** 2385 * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume 2386 * 2387 * The control element is supposed to have the private_value field 2388 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2389 */ 2390 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol, 2391 struct snd_ctl_elem_value *ucontrol) 2392 { 2393 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2394 hda_nid_t nid = get_amp_nid(kcontrol); 2395 int chs = get_amp_channels(kcontrol); 2396 int dir = get_amp_direction(kcontrol); 2397 int idx = get_amp_index(kcontrol); 2398 unsigned int ofs = get_amp_offset(kcontrol); 2399 long *valp = ucontrol->value.integer.value; 2400 2401 if (chs & 1) 2402 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs); 2403 if (chs & 2) 2404 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs); 2405 return 0; 2406 } 2407 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_get); 2408 2409 /** 2410 * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume 2411 * 2412 * The control element is supposed to have the private_value field 2413 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2414 */ 2415 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol, 2416 struct snd_ctl_elem_value *ucontrol) 2417 { 2418 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2419 hda_nid_t nid = get_amp_nid(kcontrol); 2420 int chs = get_amp_channels(kcontrol); 2421 int dir = get_amp_direction(kcontrol); 2422 int idx = get_amp_index(kcontrol); 2423 unsigned int ofs = get_amp_offset(kcontrol); 2424 long *valp = ucontrol->value.integer.value; 2425 int change = 0; 2426 2427 snd_hda_power_up(codec); 2428 if (chs & 1) { 2429 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp); 2430 valp++; 2431 } 2432 if (chs & 2) 2433 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp); 2434 snd_hda_power_down(codec); 2435 return change; 2436 } 2437 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_volume_put); 2438 2439 /** 2440 * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume 2441 * 2442 * The control element is supposed to have the private_value field 2443 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 2444 */ 2445 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag, 2446 unsigned int size, unsigned int __user *_tlv) 2447 { 2448 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 2449 hda_nid_t nid = get_amp_nid(kcontrol); 2450 int dir = get_amp_direction(kcontrol); 2451 unsigned int ofs = get_amp_offset(kcontrol); 2452 bool min_mute = get_amp_min_mute(kcontrol); 2453 u32 caps, val1, val2; 2454 2455 if (size < 4 * sizeof(unsigned int)) 2456 return -ENOMEM; 2457 caps = query_amp_caps(codec, nid, dir); 2458 val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; 2459 val2 = (val2 + 1) * 25; 2460 val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT); 2461 val1 += ofs; 2462 val1 = ((int)val1) * ((int)val2); 2463 if (min_mute || (caps & AC_AMPCAP_MIN_MUTE)) 2464 val2 |= TLV_DB_SCALE_MUTE; 2465 if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv)) 2466 return -EFAULT; 2467 if (put_user(2 * sizeof(unsigned int), _tlv + 1)) 2468 return -EFAULT; 2469 if (put_user(val1, _tlv + 2)) 2470 return -EFAULT; 2471 if (put_user(val2, _tlv + 3)) 2472 return -EFAULT; 2473 return 0; 2474 } 2475 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_tlv); 2476 2477 /** 2478 * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control 2479 * @codec: HD-audio codec 2480 * @nid: NID of a reference widget 2481 * @dir: #HDA_INPUT or #HDA_OUTPUT 2482 * @tlv: TLV data to be stored, at least 4 elements 2483 * 2484 * Set (static) TLV data for a virtual master volume using the AMP caps 2485 * obtained from the reference NID. 2486 * The volume range is recalculated as if the max volume is 0dB. 2487 */ 2488 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir, 2489 unsigned int *tlv) 2490 { 2491 u32 caps; 2492 int nums, step; 2493 2494 caps = query_amp_caps(codec, nid, dir); 2495 nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; 2496 step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT; 2497 step = (step + 1) * 25; 2498 tlv[0] = SNDRV_CTL_TLVT_DB_SCALE; 2499 tlv[1] = 2 * sizeof(unsigned int); 2500 tlv[2] = -nums * step; 2501 tlv[3] = step; 2502 } 2503 EXPORT_SYMBOL_GPL(snd_hda_set_vmaster_tlv); 2504 2505 /* find a mixer control element with the given name */ 2506 static struct snd_kcontrol * 2507 find_mixer_ctl(struct hda_codec *codec, const char *name, int dev, int idx) 2508 { 2509 struct snd_ctl_elem_id id; 2510 memset(&id, 0, sizeof(id)); 2511 id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; 2512 id.device = dev; 2513 id.index = idx; 2514 if (snd_BUG_ON(strlen(name) >= sizeof(id.name))) 2515 return NULL; 2516 strcpy(id.name, name); 2517 return snd_ctl_find_id(codec->bus->card, &id); 2518 } 2519 2520 /** 2521 * snd_hda_find_mixer_ctl - Find a mixer control element with the given name 2522 * @codec: HD-audio codec 2523 * @name: ctl id name string 2524 * 2525 * Get the control element with the given id string and IFACE_MIXER. 2526 */ 2527 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec, 2528 const char *name) 2529 { 2530 return find_mixer_ctl(codec, name, 0, 0); 2531 } 2532 EXPORT_SYMBOL_GPL(snd_hda_find_mixer_ctl); 2533 2534 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name, 2535 int start_idx) 2536 { 2537 int i, idx; 2538 /* 16 ctlrs should be large enough */ 2539 for (i = 0, idx = start_idx; i < 16; i++, idx++) { 2540 if (!find_mixer_ctl(codec, name, 0, idx)) 2541 return idx; 2542 } 2543 return -EBUSY; 2544 } 2545 2546 /** 2547 * snd_hda_ctl_add - Add a control element and assign to the codec 2548 * @codec: HD-audio codec 2549 * @nid: corresponding NID (optional) 2550 * @kctl: the control element to assign 2551 * 2552 * Add the given control element to an array inside the codec instance. 2553 * All control elements belonging to a codec are supposed to be added 2554 * by this function so that a proper clean-up works at the free or 2555 * reconfiguration time. 2556 * 2557 * If non-zero @nid is passed, the NID is assigned to the control element. 2558 * The assignment is shown in the codec proc file. 2559 * 2560 * snd_hda_ctl_add() checks the control subdev id field whether 2561 * #HDA_SUBDEV_NID_FLAG bit is set. If set (and @nid is zero), the lower 2562 * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit 2563 * specifies if kctl->private_value is a HDA amplifier value. 2564 */ 2565 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid, 2566 struct snd_kcontrol *kctl) 2567 { 2568 int err; 2569 unsigned short flags = 0; 2570 struct hda_nid_item *item; 2571 2572 if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) { 2573 flags |= HDA_NID_ITEM_AMP; 2574 if (nid == 0) 2575 nid = get_amp_nid_(kctl->private_value); 2576 } 2577 if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0) 2578 nid = kctl->id.subdevice & 0xffff; 2579 if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG)) 2580 kctl->id.subdevice = 0; 2581 err = snd_ctl_add(codec->bus->card, kctl); 2582 if (err < 0) 2583 return err; 2584 item = snd_array_new(&codec->mixers); 2585 if (!item) 2586 return -ENOMEM; 2587 item->kctl = kctl; 2588 item->nid = nid; 2589 item->flags = flags; 2590 return 0; 2591 } 2592 EXPORT_SYMBOL_GPL(snd_hda_ctl_add); 2593 2594 /** 2595 * snd_hda_add_nid - Assign a NID to a control element 2596 * @codec: HD-audio codec 2597 * @nid: corresponding NID (optional) 2598 * @kctl: the control element to assign 2599 * @index: index to kctl 2600 * 2601 * Add the given control element to an array inside the codec instance. 2602 * This function is used when #snd_hda_ctl_add cannot be used for 1:1 2603 * NID:KCTL mapping - for example "Capture Source" selector. 2604 */ 2605 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl, 2606 unsigned int index, hda_nid_t nid) 2607 { 2608 struct hda_nid_item *item; 2609 2610 if (nid > 0) { 2611 item = snd_array_new(&codec->nids); 2612 if (!item) 2613 return -ENOMEM; 2614 item->kctl = kctl; 2615 item->index = index; 2616 item->nid = nid; 2617 return 0; 2618 } 2619 codec_err(codec, "no NID for mapping control %s:%d:%d\n", 2620 kctl->id.name, kctl->id.index, index); 2621 return -EINVAL; 2622 } 2623 EXPORT_SYMBOL_GPL(snd_hda_add_nid); 2624 2625 /** 2626 * snd_hda_ctls_clear - Clear all controls assigned to the given codec 2627 * @codec: HD-audio codec 2628 */ 2629 void snd_hda_ctls_clear(struct hda_codec *codec) 2630 { 2631 int i; 2632 struct hda_nid_item *items = codec->mixers.list; 2633 for (i = 0; i < codec->mixers.used; i++) 2634 snd_ctl_remove(codec->bus->card, items[i].kctl); 2635 snd_array_free(&codec->mixers); 2636 snd_array_free(&codec->nids); 2637 } 2638 2639 /* pseudo device locking 2640 * toggle card->shutdown to allow/disallow the device access (as a hack) 2641 */ 2642 int snd_hda_lock_devices(struct hda_bus *bus) 2643 { 2644 struct snd_card *card = bus->card; 2645 struct hda_codec *codec; 2646 2647 spin_lock(&card->files_lock); 2648 if (card->shutdown) 2649 goto err_unlock; 2650 card->shutdown = 1; 2651 if (!list_empty(&card->ctl_files)) 2652 goto err_clear; 2653 2654 list_for_each_entry(codec, &bus->codec_list, list) { 2655 int pcm; 2656 for (pcm = 0; pcm < codec->num_pcms; pcm++) { 2657 struct hda_pcm *cpcm = &codec->pcm_info[pcm]; 2658 if (!cpcm->pcm) 2659 continue; 2660 if (cpcm->pcm->streams[0].substream_opened || 2661 cpcm->pcm->streams[1].substream_opened) 2662 goto err_clear; 2663 } 2664 } 2665 spin_unlock(&card->files_lock); 2666 return 0; 2667 2668 err_clear: 2669 card->shutdown = 0; 2670 err_unlock: 2671 spin_unlock(&card->files_lock); 2672 return -EINVAL; 2673 } 2674 EXPORT_SYMBOL_GPL(snd_hda_lock_devices); 2675 2676 void snd_hda_unlock_devices(struct hda_bus *bus) 2677 { 2678 struct snd_card *card = bus->card; 2679 2680 card = bus->card; 2681 spin_lock(&card->files_lock); 2682 card->shutdown = 0; 2683 spin_unlock(&card->files_lock); 2684 } 2685 EXPORT_SYMBOL_GPL(snd_hda_unlock_devices); 2686 2687 /** 2688 * snd_hda_codec_reset - Clear all objects assigned to the codec 2689 * @codec: HD-audio codec 2690 * 2691 * This frees the all PCM and control elements assigned to the codec, and 2692 * clears the caches and restores the pin default configurations. 2693 * 2694 * When a device is being used, it returns -EBSY. If successfully freed, 2695 * returns zero. 2696 */ 2697 int snd_hda_codec_reset(struct hda_codec *codec) 2698 { 2699 struct hda_bus *bus = codec->bus; 2700 struct snd_card *card = bus->card; 2701 int i; 2702 2703 if (snd_hda_lock_devices(bus) < 0) 2704 return -EBUSY; 2705 2706 /* OK, let it free */ 2707 cancel_delayed_work_sync(&codec->jackpoll_work); 2708 #ifdef CONFIG_PM 2709 cancel_delayed_work_sync(&codec->power_work); 2710 flush_workqueue(bus->workq); 2711 #endif 2712 snd_hda_ctls_clear(codec); 2713 /* release PCMs */ 2714 for (i = 0; i < codec->num_pcms; i++) { 2715 if (codec->pcm_info[i].pcm) { 2716 snd_device_free(card, codec->pcm_info[i].pcm); 2717 clear_bit(codec->pcm_info[i].device, 2718 bus->pcm_dev_bits); 2719 } 2720 } 2721 snd_hda_detach_beep_device(codec); 2722 if (codec->patch_ops.free) 2723 codec->patch_ops.free(codec); 2724 memset(&codec->patch_ops, 0, sizeof(codec->patch_ops)); 2725 snd_hda_jack_tbl_clear(codec); 2726 codec->proc_widget_hook = NULL; 2727 codec->spec = NULL; 2728 free_hda_cache(&codec->amp_cache); 2729 free_hda_cache(&codec->cmd_cache); 2730 init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info)); 2731 init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head)); 2732 /* free only driver_pins so that init_pins + user_pins are restored */ 2733 snd_array_free(&codec->driver_pins); 2734 snd_array_free(&codec->cvt_setups); 2735 snd_array_free(&codec->spdif_out); 2736 snd_array_free(&codec->verbs); 2737 codec->num_pcms = 0; 2738 codec->pcm_info = NULL; 2739 codec->preset = NULL; 2740 codec->slave_dig_outs = NULL; 2741 codec->spdif_status_reset = 0; 2742 unload_parser(codec); 2743 module_put(codec->owner); 2744 codec->owner = NULL; 2745 2746 /* allow device access again */ 2747 snd_hda_unlock_devices(bus); 2748 return 0; 2749 } 2750 2751 typedef int (*map_slave_func_t)(struct hda_codec *, void *, struct snd_kcontrol *); 2752 2753 /* apply the function to all matching slave ctls in the mixer list */ 2754 static int map_slaves(struct hda_codec *codec, const char * const *slaves, 2755 const char *suffix, map_slave_func_t func, void *data) 2756 { 2757 struct hda_nid_item *items; 2758 const char * const *s; 2759 int i, err; 2760 2761 items = codec->mixers.list; 2762 for (i = 0; i < codec->mixers.used; i++) { 2763 struct snd_kcontrol *sctl = items[i].kctl; 2764 if (!sctl || sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER) 2765 continue; 2766 for (s = slaves; *s; s++) { 2767 char tmpname[sizeof(sctl->id.name)]; 2768 const char *name = *s; 2769 if (suffix) { 2770 snprintf(tmpname, sizeof(tmpname), "%s %s", 2771 name, suffix); 2772 name = tmpname; 2773 } 2774 if (!strcmp(sctl->id.name, name)) { 2775 err = func(codec, data, sctl); 2776 if (err) 2777 return err; 2778 break; 2779 } 2780 } 2781 } 2782 return 0; 2783 } 2784 2785 static int check_slave_present(struct hda_codec *codec, 2786 void *data, struct snd_kcontrol *sctl) 2787 { 2788 return 1; 2789 } 2790 2791 /* guess the value corresponding to 0dB */ 2792 static int get_kctl_0dB_offset(struct hda_codec *codec, 2793 struct snd_kcontrol *kctl, int *step_to_check) 2794 { 2795 int _tlv[4]; 2796 const int *tlv = NULL; 2797 int val = -1; 2798 2799 if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) { 2800 /* FIXME: set_fs() hack for obtaining user-space TLV data */ 2801 mm_segment_t fs = get_fs(); 2802 set_fs(get_ds()); 2803 if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv)) 2804 tlv = _tlv; 2805 set_fs(fs); 2806 } else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ) 2807 tlv = kctl->tlv.p; 2808 if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE) { 2809 int step = tlv[3]; 2810 step &= ~TLV_DB_SCALE_MUTE; 2811 if (!step) 2812 return -1; 2813 if (*step_to_check && *step_to_check != step) { 2814 codec_err(codec, "Mismatching dB step for vmaster slave (%d!=%d)\n", 2815 - *step_to_check, step); 2816 return -1; 2817 } 2818 *step_to_check = step; 2819 val = -tlv[2] / step; 2820 } 2821 return val; 2822 } 2823 2824 /* call kctl->put with the given value(s) */ 2825 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val) 2826 { 2827 struct snd_ctl_elem_value *ucontrol; 2828 ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL); 2829 if (!ucontrol) 2830 return -ENOMEM; 2831 ucontrol->value.integer.value[0] = val; 2832 ucontrol->value.integer.value[1] = val; 2833 kctl->put(kctl, ucontrol); 2834 kfree(ucontrol); 2835 return 0; 2836 } 2837 2838 /* initialize the slave volume with 0dB */ 2839 static int init_slave_0dB(struct hda_codec *codec, 2840 void *data, struct snd_kcontrol *slave) 2841 { 2842 int offset = get_kctl_0dB_offset(codec, slave, data); 2843 if (offset > 0) 2844 put_kctl_with_value(slave, offset); 2845 return 0; 2846 } 2847 2848 /* unmute the slave */ 2849 static int init_slave_unmute(struct hda_codec *codec, 2850 void *data, struct snd_kcontrol *slave) 2851 { 2852 return put_kctl_with_value(slave, 1); 2853 } 2854 2855 static int add_slave(struct hda_codec *codec, 2856 void *data, struct snd_kcontrol *slave) 2857 { 2858 return snd_ctl_add_slave(data, slave); 2859 } 2860 2861 /** 2862 * snd_hda_add_vmaster - create a virtual master control and add slaves 2863 * @codec: HD-audio codec 2864 * @name: vmaster control name 2865 * @tlv: TLV data (optional) 2866 * @slaves: slave control names (optional) 2867 * @suffix: suffix string to each slave name (optional) 2868 * @init_slave_vol: initialize slaves to unmute/0dB 2869 * @ctl_ret: store the vmaster kcontrol in return 2870 * 2871 * Create a virtual master control with the given name. The TLV data 2872 * must be either NULL or a valid data. 2873 * 2874 * @slaves is a NULL-terminated array of strings, each of which is a 2875 * slave control name. All controls with these names are assigned to 2876 * the new virtual master control. 2877 * 2878 * This function returns zero if successful or a negative error code. 2879 */ 2880 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name, 2881 unsigned int *tlv, const char * const *slaves, 2882 const char *suffix, bool init_slave_vol, 2883 struct snd_kcontrol **ctl_ret) 2884 { 2885 struct snd_kcontrol *kctl; 2886 int err; 2887 2888 if (ctl_ret) 2889 *ctl_ret = NULL; 2890 2891 err = map_slaves(codec, slaves, suffix, check_slave_present, NULL); 2892 if (err != 1) { 2893 codec_dbg(codec, "No slave found for %s\n", name); 2894 return 0; 2895 } 2896 kctl = snd_ctl_make_virtual_master(name, tlv); 2897 if (!kctl) 2898 return -ENOMEM; 2899 err = snd_hda_ctl_add(codec, 0, kctl); 2900 if (err < 0) 2901 return err; 2902 2903 err = map_slaves(codec, slaves, suffix, add_slave, kctl); 2904 if (err < 0) 2905 return err; 2906 2907 /* init with master mute & zero volume */ 2908 put_kctl_with_value(kctl, 0); 2909 if (init_slave_vol) { 2910 int step = 0; 2911 map_slaves(codec, slaves, suffix, 2912 tlv ? init_slave_0dB : init_slave_unmute, &step); 2913 } 2914 2915 if (ctl_ret) 2916 *ctl_ret = kctl; 2917 return 0; 2918 } 2919 EXPORT_SYMBOL_GPL(__snd_hda_add_vmaster); 2920 2921 /* 2922 * mute-LED control using vmaster 2923 */ 2924 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol, 2925 struct snd_ctl_elem_info *uinfo) 2926 { 2927 static const char * const texts[] = { 2928 "On", "Off", "Follow Master" 2929 }; 2930 unsigned int index; 2931 2932 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 2933 uinfo->count = 1; 2934 uinfo->value.enumerated.items = 3; 2935 index = uinfo->value.enumerated.item; 2936 if (index >= 3) 2937 index = 2; 2938 strcpy(uinfo->value.enumerated.name, texts[index]); 2939 return 0; 2940 } 2941 2942 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol, 2943 struct snd_ctl_elem_value *ucontrol) 2944 { 2945 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); 2946 ucontrol->value.enumerated.item[0] = hook->mute_mode; 2947 return 0; 2948 } 2949 2950 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol, 2951 struct snd_ctl_elem_value *ucontrol) 2952 { 2953 struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol); 2954 unsigned int old_mode = hook->mute_mode; 2955 2956 hook->mute_mode = ucontrol->value.enumerated.item[0]; 2957 if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER) 2958 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; 2959 if (old_mode == hook->mute_mode) 2960 return 0; 2961 snd_hda_sync_vmaster_hook(hook); 2962 return 1; 2963 } 2964 2965 static struct snd_kcontrol_new vmaster_mute_mode = { 2966 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 2967 .name = "Mute-LED Mode", 2968 .info = vmaster_mute_mode_info, 2969 .get = vmaster_mute_mode_get, 2970 .put = vmaster_mute_mode_put, 2971 }; 2972 2973 /* 2974 * Add a mute-LED hook with the given vmaster switch kctl 2975 * "Mute-LED Mode" control is automatically created and associated with 2976 * the given hook. 2977 */ 2978 int snd_hda_add_vmaster_hook(struct hda_codec *codec, 2979 struct hda_vmaster_mute_hook *hook, 2980 bool expose_enum_ctl) 2981 { 2982 struct snd_kcontrol *kctl; 2983 2984 if (!hook->hook || !hook->sw_kctl) 2985 return 0; 2986 snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec); 2987 hook->codec = codec; 2988 hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER; 2989 if (!expose_enum_ctl) 2990 return 0; 2991 kctl = snd_ctl_new1(&vmaster_mute_mode, hook); 2992 if (!kctl) 2993 return -ENOMEM; 2994 return snd_hda_ctl_add(codec, 0, kctl); 2995 } 2996 EXPORT_SYMBOL_GPL(snd_hda_add_vmaster_hook); 2997 2998 /* 2999 * Call the hook with the current value for synchronization 3000 * Should be called in init callback 3001 */ 3002 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook) 3003 { 3004 if (!hook->hook || !hook->codec) 3005 return; 3006 /* don't call vmaster hook in the destructor since it might have 3007 * been already destroyed 3008 */ 3009 if (hook->codec->bus->shutdown) 3010 return; 3011 switch (hook->mute_mode) { 3012 case HDA_VMUTE_FOLLOW_MASTER: 3013 snd_ctl_sync_vmaster_hook(hook->sw_kctl); 3014 break; 3015 default: 3016 hook->hook(hook->codec, hook->mute_mode); 3017 break; 3018 } 3019 } 3020 EXPORT_SYMBOL_GPL(snd_hda_sync_vmaster_hook); 3021 3022 3023 /** 3024 * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch 3025 * 3026 * The control element is supposed to have the private_value field 3027 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 3028 */ 3029 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol, 3030 struct snd_ctl_elem_info *uinfo) 3031 { 3032 int chs = get_amp_channels(kcontrol); 3033 3034 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; 3035 uinfo->count = chs == 3 ? 2 : 1; 3036 uinfo->value.integer.min = 0; 3037 uinfo->value.integer.max = 1; 3038 return 0; 3039 } 3040 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_info); 3041 3042 /** 3043 * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch 3044 * 3045 * The control element is supposed to have the private_value field 3046 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 3047 */ 3048 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol, 3049 struct snd_ctl_elem_value *ucontrol) 3050 { 3051 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3052 hda_nid_t nid = get_amp_nid(kcontrol); 3053 int chs = get_amp_channels(kcontrol); 3054 int dir = get_amp_direction(kcontrol); 3055 int idx = get_amp_index(kcontrol); 3056 long *valp = ucontrol->value.integer.value; 3057 3058 if (chs & 1) 3059 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 3060 HDA_AMP_MUTE) ? 0 : 1; 3061 if (chs & 2) 3062 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 3063 HDA_AMP_MUTE) ? 0 : 1; 3064 return 0; 3065 } 3066 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_get); 3067 3068 /** 3069 * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch 3070 * 3071 * The control element is supposed to have the private_value field 3072 * set up via HDA_COMPOSE_AMP_VAL*() or related macros. 3073 */ 3074 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol, 3075 struct snd_ctl_elem_value *ucontrol) 3076 { 3077 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3078 hda_nid_t nid = get_amp_nid(kcontrol); 3079 int chs = get_amp_channels(kcontrol); 3080 int dir = get_amp_direction(kcontrol); 3081 int idx = get_amp_index(kcontrol); 3082 long *valp = ucontrol->value.integer.value; 3083 int change = 0; 3084 3085 snd_hda_power_up(codec); 3086 if (chs & 1) { 3087 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx, 3088 HDA_AMP_MUTE, 3089 *valp ? 0 : HDA_AMP_MUTE); 3090 valp++; 3091 } 3092 if (chs & 2) 3093 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx, 3094 HDA_AMP_MUTE, 3095 *valp ? 0 : HDA_AMP_MUTE); 3096 hda_call_check_power_status(codec, nid); 3097 snd_hda_power_down(codec); 3098 return change; 3099 } 3100 EXPORT_SYMBOL_GPL(snd_hda_mixer_amp_switch_put); 3101 3102 /* 3103 * bound volume controls 3104 * 3105 * bind multiple volumes (# indices, from 0) 3106 */ 3107 3108 #define AMP_VAL_IDX_SHIFT 19 3109 #define AMP_VAL_IDX_MASK (0x0f<<19) 3110 3111 /** 3112 * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control 3113 * 3114 * The control element is supposed to have the private_value field 3115 * set up via HDA_BIND_MUTE*() macros. 3116 */ 3117 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol, 3118 struct snd_ctl_elem_value *ucontrol) 3119 { 3120 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3121 unsigned long pval; 3122 int err; 3123 3124 mutex_lock(&codec->control_mutex); 3125 pval = kcontrol->private_value; 3126 kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */ 3127 err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol); 3128 kcontrol->private_value = pval; 3129 mutex_unlock(&codec->control_mutex); 3130 return err; 3131 } 3132 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_get); 3133 3134 /** 3135 * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control 3136 * 3137 * The control element is supposed to have the private_value field 3138 * set up via HDA_BIND_MUTE*() macros. 3139 */ 3140 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol, 3141 struct snd_ctl_elem_value *ucontrol) 3142 { 3143 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3144 unsigned long pval; 3145 int i, indices, err = 0, change = 0; 3146 3147 mutex_lock(&codec->control_mutex); 3148 pval = kcontrol->private_value; 3149 indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT; 3150 for (i = 0; i < indices; i++) { 3151 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | 3152 (i << AMP_VAL_IDX_SHIFT); 3153 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol); 3154 if (err < 0) 3155 break; 3156 change |= err; 3157 } 3158 kcontrol->private_value = pval; 3159 mutex_unlock(&codec->control_mutex); 3160 return err < 0 ? err : change; 3161 } 3162 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_switch_put); 3163 3164 /** 3165 * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control 3166 * 3167 * The control element is supposed to have the private_value field 3168 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. 3169 */ 3170 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol, 3171 struct snd_ctl_elem_info *uinfo) 3172 { 3173 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3174 struct hda_bind_ctls *c; 3175 int err; 3176 3177 mutex_lock(&codec->control_mutex); 3178 c = (struct hda_bind_ctls *)kcontrol->private_value; 3179 kcontrol->private_value = *c->values; 3180 err = c->ops->info(kcontrol, uinfo); 3181 kcontrol->private_value = (long)c; 3182 mutex_unlock(&codec->control_mutex); 3183 return err; 3184 } 3185 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_info); 3186 3187 /** 3188 * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control 3189 * 3190 * The control element is supposed to have the private_value field 3191 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. 3192 */ 3193 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol, 3194 struct snd_ctl_elem_value *ucontrol) 3195 { 3196 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3197 struct hda_bind_ctls *c; 3198 int err; 3199 3200 mutex_lock(&codec->control_mutex); 3201 c = (struct hda_bind_ctls *)kcontrol->private_value; 3202 kcontrol->private_value = *c->values; 3203 err = c->ops->get(kcontrol, ucontrol); 3204 kcontrol->private_value = (long)c; 3205 mutex_unlock(&codec->control_mutex); 3206 return err; 3207 } 3208 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_get); 3209 3210 /** 3211 * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control 3212 * 3213 * The control element is supposed to have the private_value field 3214 * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros. 3215 */ 3216 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol, 3217 struct snd_ctl_elem_value *ucontrol) 3218 { 3219 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3220 struct hda_bind_ctls *c; 3221 unsigned long *vals; 3222 int err = 0, change = 0; 3223 3224 mutex_lock(&codec->control_mutex); 3225 c = (struct hda_bind_ctls *)kcontrol->private_value; 3226 for (vals = c->values; *vals; vals++) { 3227 kcontrol->private_value = *vals; 3228 err = c->ops->put(kcontrol, ucontrol); 3229 if (err < 0) 3230 break; 3231 change |= err; 3232 } 3233 kcontrol->private_value = (long)c; 3234 mutex_unlock(&codec->control_mutex); 3235 return err < 0 ? err : change; 3236 } 3237 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_ctls_put); 3238 3239 /** 3240 * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control 3241 * 3242 * The control element is supposed to have the private_value field 3243 * set up via HDA_BIND_VOL() macro. 3244 */ 3245 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag, 3246 unsigned int size, unsigned int __user *tlv) 3247 { 3248 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3249 struct hda_bind_ctls *c; 3250 int err; 3251 3252 mutex_lock(&codec->control_mutex); 3253 c = (struct hda_bind_ctls *)kcontrol->private_value; 3254 kcontrol->private_value = *c->values; 3255 err = c->ops->tlv(kcontrol, op_flag, size, tlv); 3256 kcontrol->private_value = (long)c; 3257 mutex_unlock(&codec->control_mutex); 3258 return err; 3259 } 3260 EXPORT_SYMBOL_GPL(snd_hda_mixer_bind_tlv); 3261 3262 struct hda_ctl_ops snd_hda_bind_vol = { 3263 .info = snd_hda_mixer_amp_volume_info, 3264 .get = snd_hda_mixer_amp_volume_get, 3265 .put = snd_hda_mixer_amp_volume_put, 3266 .tlv = snd_hda_mixer_amp_tlv 3267 }; 3268 EXPORT_SYMBOL_GPL(snd_hda_bind_vol); 3269 3270 struct hda_ctl_ops snd_hda_bind_sw = { 3271 .info = snd_hda_mixer_amp_switch_info, 3272 .get = snd_hda_mixer_amp_switch_get, 3273 .put = snd_hda_mixer_amp_switch_put, 3274 .tlv = snd_hda_mixer_amp_tlv 3275 }; 3276 EXPORT_SYMBOL_GPL(snd_hda_bind_sw); 3277 3278 /* 3279 * SPDIF out controls 3280 */ 3281 3282 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol, 3283 struct snd_ctl_elem_info *uinfo) 3284 { 3285 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 3286 uinfo->count = 1; 3287 return 0; 3288 } 3289 3290 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol, 3291 struct snd_ctl_elem_value *ucontrol) 3292 { 3293 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | 3294 IEC958_AES0_NONAUDIO | 3295 IEC958_AES0_CON_EMPHASIS_5015 | 3296 IEC958_AES0_CON_NOT_COPYRIGHT; 3297 ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY | 3298 IEC958_AES1_CON_ORIGINAL; 3299 return 0; 3300 } 3301 3302 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol, 3303 struct snd_ctl_elem_value *ucontrol) 3304 { 3305 ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL | 3306 IEC958_AES0_NONAUDIO | 3307 IEC958_AES0_PRO_EMPHASIS_5015; 3308 return 0; 3309 } 3310 3311 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol, 3312 struct snd_ctl_elem_value *ucontrol) 3313 { 3314 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3315 int idx = kcontrol->private_value; 3316 struct hda_spdif_out *spdif; 3317 3318 mutex_lock(&codec->spdif_mutex); 3319 spdif = snd_array_elem(&codec->spdif_out, idx); 3320 ucontrol->value.iec958.status[0] = spdif->status & 0xff; 3321 ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff; 3322 ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff; 3323 ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff; 3324 mutex_unlock(&codec->spdif_mutex); 3325 3326 return 0; 3327 } 3328 3329 /* convert from SPDIF status bits to HDA SPDIF bits 3330 * bit 0 (DigEn) is always set zero (to be filled later) 3331 */ 3332 static unsigned short convert_from_spdif_status(unsigned int sbits) 3333 { 3334 unsigned short val = 0; 3335 3336 if (sbits & IEC958_AES0_PROFESSIONAL) 3337 val |= AC_DIG1_PROFESSIONAL; 3338 if (sbits & IEC958_AES0_NONAUDIO) 3339 val |= AC_DIG1_NONAUDIO; 3340 if (sbits & IEC958_AES0_PROFESSIONAL) { 3341 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == 3342 IEC958_AES0_PRO_EMPHASIS_5015) 3343 val |= AC_DIG1_EMPHASIS; 3344 } else { 3345 if ((sbits & IEC958_AES0_CON_EMPHASIS) == 3346 IEC958_AES0_CON_EMPHASIS_5015) 3347 val |= AC_DIG1_EMPHASIS; 3348 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT)) 3349 val |= AC_DIG1_COPYRIGHT; 3350 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8)) 3351 val |= AC_DIG1_LEVEL; 3352 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8); 3353 } 3354 return val; 3355 } 3356 3357 /* convert to SPDIF status bits from HDA SPDIF bits 3358 */ 3359 static unsigned int convert_to_spdif_status(unsigned short val) 3360 { 3361 unsigned int sbits = 0; 3362 3363 if (val & AC_DIG1_NONAUDIO) 3364 sbits |= IEC958_AES0_NONAUDIO; 3365 if (val & AC_DIG1_PROFESSIONAL) 3366 sbits |= IEC958_AES0_PROFESSIONAL; 3367 if (sbits & IEC958_AES0_PROFESSIONAL) { 3368 if (val & AC_DIG1_EMPHASIS) 3369 sbits |= IEC958_AES0_PRO_EMPHASIS_5015; 3370 } else { 3371 if (val & AC_DIG1_EMPHASIS) 3372 sbits |= IEC958_AES0_CON_EMPHASIS_5015; 3373 if (!(val & AC_DIG1_COPYRIGHT)) 3374 sbits |= IEC958_AES0_CON_NOT_COPYRIGHT; 3375 if (val & AC_DIG1_LEVEL) 3376 sbits |= (IEC958_AES1_CON_ORIGINAL << 8); 3377 sbits |= val & (0x7f << 8); 3378 } 3379 return sbits; 3380 } 3381 3382 /* set digital convert verbs both for the given NID and its slaves */ 3383 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid, 3384 int verb, int val) 3385 { 3386 const hda_nid_t *d; 3387 3388 snd_hda_codec_write_cache(codec, nid, 0, verb, val); 3389 d = codec->slave_dig_outs; 3390 if (!d) 3391 return; 3392 for (; *d; d++) 3393 snd_hda_codec_write_cache(codec, *d, 0, verb, val); 3394 } 3395 3396 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid, 3397 int dig1, int dig2) 3398 { 3399 if (dig1 != -1) 3400 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1); 3401 if (dig2 != -1) 3402 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2); 3403 } 3404 3405 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol, 3406 struct snd_ctl_elem_value *ucontrol) 3407 { 3408 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3409 int idx = kcontrol->private_value; 3410 struct hda_spdif_out *spdif; 3411 hda_nid_t nid; 3412 unsigned short val; 3413 int change; 3414 3415 mutex_lock(&codec->spdif_mutex); 3416 spdif = snd_array_elem(&codec->spdif_out, idx); 3417 nid = spdif->nid; 3418 spdif->status = ucontrol->value.iec958.status[0] | 3419 ((unsigned int)ucontrol->value.iec958.status[1] << 8) | 3420 ((unsigned int)ucontrol->value.iec958.status[2] << 16) | 3421 ((unsigned int)ucontrol->value.iec958.status[3] << 24); 3422 val = convert_from_spdif_status(spdif->status); 3423 val |= spdif->ctls & 1; 3424 change = spdif->ctls != val; 3425 spdif->ctls = val; 3426 if (change && nid != (u16)-1) 3427 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff); 3428 mutex_unlock(&codec->spdif_mutex); 3429 return change; 3430 } 3431 3432 #define snd_hda_spdif_out_switch_info snd_ctl_boolean_mono_info 3433 3434 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol, 3435 struct snd_ctl_elem_value *ucontrol) 3436 { 3437 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3438 int idx = kcontrol->private_value; 3439 struct hda_spdif_out *spdif; 3440 3441 mutex_lock(&codec->spdif_mutex); 3442 spdif = snd_array_elem(&codec->spdif_out, idx); 3443 ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE; 3444 mutex_unlock(&codec->spdif_mutex); 3445 return 0; 3446 } 3447 3448 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid, 3449 int dig1, int dig2) 3450 { 3451 set_dig_out_convert(codec, nid, dig1, dig2); 3452 /* unmute amp switch (if any) */ 3453 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) && 3454 (dig1 & AC_DIG1_ENABLE)) 3455 snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0, 3456 HDA_AMP_MUTE, 0); 3457 } 3458 3459 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol, 3460 struct snd_ctl_elem_value *ucontrol) 3461 { 3462 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3463 int idx = kcontrol->private_value; 3464 struct hda_spdif_out *spdif; 3465 hda_nid_t nid; 3466 unsigned short val; 3467 int change; 3468 3469 mutex_lock(&codec->spdif_mutex); 3470 spdif = snd_array_elem(&codec->spdif_out, idx); 3471 nid = spdif->nid; 3472 val = spdif->ctls & ~AC_DIG1_ENABLE; 3473 if (ucontrol->value.integer.value[0]) 3474 val |= AC_DIG1_ENABLE; 3475 change = spdif->ctls != val; 3476 spdif->ctls = val; 3477 if (change && nid != (u16)-1) 3478 set_spdif_ctls(codec, nid, val & 0xff, -1); 3479 mutex_unlock(&codec->spdif_mutex); 3480 return change; 3481 } 3482 3483 static struct snd_kcontrol_new dig_mixes[] = { 3484 { 3485 .access = SNDRV_CTL_ELEM_ACCESS_READ, 3486 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3487 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK), 3488 .info = snd_hda_spdif_mask_info, 3489 .get = snd_hda_spdif_cmask_get, 3490 }, 3491 { 3492 .access = SNDRV_CTL_ELEM_ACCESS_READ, 3493 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3494 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK), 3495 .info = snd_hda_spdif_mask_info, 3496 .get = snd_hda_spdif_pmask_get, 3497 }, 3498 { 3499 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3500 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 3501 .info = snd_hda_spdif_mask_info, 3502 .get = snd_hda_spdif_default_get, 3503 .put = snd_hda_spdif_default_put, 3504 }, 3505 { 3506 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3507 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH), 3508 .info = snd_hda_spdif_out_switch_info, 3509 .get = snd_hda_spdif_out_switch_get, 3510 .put = snd_hda_spdif_out_switch_put, 3511 }, 3512 { } /* end */ 3513 }; 3514 3515 /** 3516 * snd_hda_create_dig_out_ctls - create Output SPDIF-related controls 3517 * @codec: the HDA codec 3518 * @associated_nid: NID that new ctls associated with 3519 * @cvt_nid: converter NID 3520 * @type: HDA_PCM_TYPE_* 3521 * Creates controls related with the digital output. 3522 * Called from each patch supporting the digital out. 3523 * 3524 * Returns 0 if successful, or a negative error code. 3525 */ 3526 int snd_hda_create_dig_out_ctls(struct hda_codec *codec, 3527 hda_nid_t associated_nid, 3528 hda_nid_t cvt_nid, 3529 int type) 3530 { 3531 int err; 3532 struct snd_kcontrol *kctl; 3533 struct snd_kcontrol_new *dig_mix; 3534 int idx = 0; 3535 const int spdif_index = 16; 3536 struct hda_spdif_out *spdif; 3537 struct hda_bus *bus = codec->bus; 3538 3539 if (bus->primary_dig_out_type == HDA_PCM_TYPE_HDMI && 3540 type == HDA_PCM_TYPE_SPDIF) { 3541 idx = spdif_index; 3542 } else if (bus->primary_dig_out_type == HDA_PCM_TYPE_SPDIF && 3543 type == HDA_PCM_TYPE_HDMI) { 3544 /* suppose a single SPDIF device */ 3545 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { 3546 kctl = find_mixer_ctl(codec, dig_mix->name, 0, 0); 3547 if (!kctl) 3548 break; 3549 kctl->id.index = spdif_index; 3550 } 3551 bus->primary_dig_out_type = HDA_PCM_TYPE_HDMI; 3552 } 3553 if (!bus->primary_dig_out_type) 3554 bus->primary_dig_out_type = type; 3555 3556 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch", idx); 3557 if (idx < 0) { 3558 codec_err(codec, "too many IEC958 outputs\n"); 3559 return -EBUSY; 3560 } 3561 spdif = snd_array_new(&codec->spdif_out); 3562 if (!spdif) 3563 return -ENOMEM; 3564 for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) { 3565 kctl = snd_ctl_new1(dig_mix, codec); 3566 if (!kctl) 3567 return -ENOMEM; 3568 kctl->id.index = idx; 3569 kctl->private_value = codec->spdif_out.used - 1; 3570 err = snd_hda_ctl_add(codec, associated_nid, kctl); 3571 if (err < 0) 3572 return err; 3573 } 3574 spdif->nid = cvt_nid; 3575 spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0, 3576 AC_VERB_GET_DIGI_CONVERT_1, 0); 3577 spdif->status = convert_to_spdif_status(spdif->ctls); 3578 return 0; 3579 } 3580 EXPORT_SYMBOL_GPL(snd_hda_create_dig_out_ctls); 3581 3582 /* get the hda_spdif_out entry from the given NID 3583 * call within spdif_mutex lock 3584 */ 3585 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec, 3586 hda_nid_t nid) 3587 { 3588 int i; 3589 for (i = 0; i < codec->spdif_out.used; i++) { 3590 struct hda_spdif_out *spdif = 3591 snd_array_elem(&codec->spdif_out, i); 3592 if (spdif->nid == nid) 3593 return spdif; 3594 } 3595 return NULL; 3596 } 3597 EXPORT_SYMBOL_GPL(snd_hda_spdif_out_of_nid); 3598 3599 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx) 3600 { 3601 struct hda_spdif_out *spdif; 3602 3603 mutex_lock(&codec->spdif_mutex); 3604 spdif = snd_array_elem(&codec->spdif_out, idx); 3605 spdif->nid = (u16)-1; 3606 mutex_unlock(&codec->spdif_mutex); 3607 } 3608 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_unassign); 3609 3610 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid) 3611 { 3612 struct hda_spdif_out *spdif; 3613 unsigned short val; 3614 3615 mutex_lock(&codec->spdif_mutex); 3616 spdif = snd_array_elem(&codec->spdif_out, idx); 3617 if (spdif->nid != nid) { 3618 spdif->nid = nid; 3619 val = spdif->ctls; 3620 set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff); 3621 } 3622 mutex_unlock(&codec->spdif_mutex); 3623 } 3624 EXPORT_SYMBOL_GPL(snd_hda_spdif_ctls_assign); 3625 3626 /* 3627 * SPDIF sharing with analog output 3628 */ 3629 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol, 3630 struct snd_ctl_elem_value *ucontrol) 3631 { 3632 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); 3633 ucontrol->value.integer.value[0] = mout->share_spdif; 3634 return 0; 3635 } 3636 3637 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol, 3638 struct snd_ctl_elem_value *ucontrol) 3639 { 3640 struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol); 3641 mout->share_spdif = !!ucontrol->value.integer.value[0]; 3642 return 0; 3643 } 3644 3645 static struct snd_kcontrol_new spdif_share_sw = { 3646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3647 .name = "IEC958 Default PCM Playback Switch", 3648 .info = snd_ctl_boolean_mono_info, 3649 .get = spdif_share_sw_get, 3650 .put = spdif_share_sw_put, 3651 }; 3652 3653 /** 3654 * snd_hda_create_spdif_share_sw - create Default PCM switch 3655 * @codec: the HDA codec 3656 * @mout: multi-out instance 3657 */ 3658 int snd_hda_create_spdif_share_sw(struct hda_codec *codec, 3659 struct hda_multi_out *mout) 3660 { 3661 struct snd_kcontrol *kctl; 3662 3663 if (!mout->dig_out_nid) 3664 return 0; 3665 3666 kctl = snd_ctl_new1(&spdif_share_sw, mout); 3667 if (!kctl) 3668 return -ENOMEM; 3669 /* ATTENTION: here mout is passed as private_data, instead of codec */ 3670 return snd_hda_ctl_add(codec, mout->dig_out_nid, kctl); 3671 } 3672 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_share_sw); 3673 3674 /* 3675 * SPDIF input 3676 */ 3677 3678 #define snd_hda_spdif_in_switch_info snd_hda_spdif_out_switch_info 3679 3680 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol, 3681 struct snd_ctl_elem_value *ucontrol) 3682 { 3683 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3684 3685 ucontrol->value.integer.value[0] = codec->spdif_in_enable; 3686 return 0; 3687 } 3688 3689 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol, 3690 struct snd_ctl_elem_value *ucontrol) 3691 { 3692 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3693 hda_nid_t nid = kcontrol->private_value; 3694 unsigned int val = !!ucontrol->value.integer.value[0]; 3695 int change; 3696 3697 mutex_lock(&codec->spdif_mutex); 3698 change = codec->spdif_in_enable != val; 3699 if (change) { 3700 codec->spdif_in_enable = val; 3701 snd_hda_codec_write_cache(codec, nid, 0, 3702 AC_VERB_SET_DIGI_CONVERT_1, val); 3703 } 3704 mutex_unlock(&codec->spdif_mutex); 3705 return change; 3706 } 3707 3708 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol, 3709 struct snd_ctl_elem_value *ucontrol) 3710 { 3711 struct hda_codec *codec = snd_kcontrol_chip(kcontrol); 3712 hda_nid_t nid = kcontrol->private_value; 3713 unsigned short val; 3714 unsigned int sbits; 3715 3716 val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0); 3717 sbits = convert_to_spdif_status(val); 3718 ucontrol->value.iec958.status[0] = sbits; 3719 ucontrol->value.iec958.status[1] = sbits >> 8; 3720 ucontrol->value.iec958.status[2] = sbits >> 16; 3721 ucontrol->value.iec958.status[3] = sbits >> 24; 3722 return 0; 3723 } 3724 3725 static struct snd_kcontrol_new dig_in_ctls[] = { 3726 { 3727 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3728 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH), 3729 .info = snd_hda_spdif_in_switch_info, 3730 .get = snd_hda_spdif_in_switch_get, 3731 .put = snd_hda_spdif_in_switch_put, 3732 }, 3733 { 3734 .access = SNDRV_CTL_ELEM_ACCESS_READ, 3735 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 3736 .name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT), 3737 .info = snd_hda_spdif_mask_info, 3738 .get = snd_hda_spdif_in_status_get, 3739 }, 3740 { } /* end */ 3741 }; 3742 3743 /** 3744 * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls 3745 * @codec: the HDA codec 3746 * @nid: audio in widget NID 3747 * 3748 * Creates controls related with the SPDIF input. 3749 * Called from each patch supporting the SPDIF in. 3750 * 3751 * Returns 0 if successful, or a negative error code. 3752 */ 3753 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid) 3754 { 3755 int err; 3756 struct snd_kcontrol *kctl; 3757 struct snd_kcontrol_new *dig_mix; 3758 int idx; 3759 3760 idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch", 0); 3761 if (idx < 0) { 3762 codec_err(codec, "too many IEC958 inputs\n"); 3763 return -EBUSY; 3764 } 3765 for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) { 3766 kctl = snd_ctl_new1(dig_mix, codec); 3767 if (!kctl) 3768 return -ENOMEM; 3769 kctl->private_value = nid; 3770 err = snd_hda_ctl_add(codec, nid, kctl); 3771 if (err < 0) 3772 return err; 3773 } 3774 codec->spdif_in_enable = 3775 snd_hda_codec_read(codec, nid, 0, 3776 AC_VERB_GET_DIGI_CONVERT_1, 0) & 3777 AC_DIG1_ENABLE; 3778 return 0; 3779 } 3780 EXPORT_SYMBOL_GPL(snd_hda_create_spdif_in_ctls); 3781 3782 /* 3783 * command cache 3784 */ 3785 3786 /* build a 31bit cache key with the widget id and the command parameter */ 3787 #define build_cmd_cache_key(nid, verb) ((verb << 8) | nid) 3788 #define get_cmd_cache_nid(key) ((key) & 0xff) 3789 #define get_cmd_cache_cmd(key) (((key) >> 8) & 0xffff) 3790 3791 /** 3792 * snd_hda_codec_write_cache - send a single command with caching 3793 * @codec: the HDA codec 3794 * @nid: NID to send the command 3795 * @flags: optional bit flags 3796 * @verb: the verb to send 3797 * @parm: the parameter for the verb 3798 * 3799 * Send a single command without waiting for response. 3800 * 3801 * Returns 0 if successful, or a negative error code. 3802 */ 3803 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid, 3804 int flags, unsigned int verb, unsigned int parm) 3805 { 3806 int err; 3807 struct hda_cache_head *c; 3808 u32 key; 3809 unsigned int cache_only; 3810 3811 cache_only = codec->cached_write; 3812 if (!cache_only) { 3813 err = snd_hda_codec_write(codec, nid, flags, verb, parm); 3814 if (err < 0) 3815 return err; 3816 } 3817 3818 /* parm may contain the verb stuff for get/set amp */ 3819 verb = verb | (parm >> 8); 3820 parm &= 0xff; 3821 key = build_cmd_cache_key(nid, verb); 3822 mutex_lock(&codec->bus->cmd_mutex); 3823 c = get_alloc_hash(&codec->cmd_cache, key); 3824 if (c) { 3825 c->val = parm; 3826 c->dirty = cache_only; 3827 } 3828 mutex_unlock(&codec->bus->cmd_mutex); 3829 return 0; 3830 } 3831 EXPORT_SYMBOL_GPL(snd_hda_codec_write_cache); 3832 3833 /** 3834 * snd_hda_codec_update_cache - check cache and write the cmd only when needed 3835 * @codec: the HDA codec 3836 * @nid: NID to send the command 3837 * @flags: optional bit flags 3838 * @verb: the verb to send 3839 * @parm: the parameter for the verb 3840 * 3841 * This function works like snd_hda_codec_write_cache(), but it doesn't send 3842 * command if the parameter is already identical with the cached value. 3843 * If not, it sends the command and refreshes the cache. 3844 * 3845 * Returns 0 if successful, or a negative error code. 3846 */ 3847 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid, 3848 int flags, unsigned int verb, unsigned int parm) 3849 { 3850 struct hda_cache_head *c; 3851 u32 key; 3852 3853 /* parm may contain the verb stuff for get/set amp */ 3854 verb = verb | (parm >> 8); 3855 parm &= 0xff; 3856 key = build_cmd_cache_key(nid, verb); 3857 mutex_lock(&codec->bus->cmd_mutex); 3858 c = get_hash(&codec->cmd_cache, key); 3859 if (c && c->val == parm) { 3860 mutex_unlock(&codec->bus->cmd_mutex); 3861 return 0; 3862 } 3863 mutex_unlock(&codec->bus->cmd_mutex); 3864 return snd_hda_codec_write_cache(codec, nid, flags, verb, parm); 3865 } 3866 EXPORT_SYMBOL_GPL(snd_hda_codec_update_cache); 3867 3868 /** 3869 * snd_hda_codec_resume_cache - Resume the all commands from the cache 3870 * @codec: HD-audio codec 3871 * 3872 * Execute all verbs recorded in the command caches to resume. 3873 */ 3874 void snd_hda_codec_resume_cache(struct hda_codec *codec) 3875 { 3876 int i; 3877 3878 mutex_lock(&codec->hash_mutex); 3879 codec->cached_write = 0; 3880 for (i = 0; i < codec->cmd_cache.buf.used; i++) { 3881 struct hda_cache_head *buffer; 3882 u32 key; 3883 3884 buffer = snd_array_elem(&codec->cmd_cache.buf, i); 3885 key = buffer->key; 3886 if (!key) 3887 continue; 3888 if (!buffer->dirty) 3889 continue; 3890 buffer->dirty = 0; 3891 mutex_unlock(&codec->hash_mutex); 3892 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0, 3893 get_cmd_cache_cmd(key), buffer->val); 3894 mutex_lock(&codec->hash_mutex); 3895 } 3896 mutex_unlock(&codec->hash_mutex); 3897 } 3898 EXPORT_SYMBOL_GPL(snd_hda_codec_resume_cache); 3899 3900 /** 3901 * snd_hda_sequence_write_cache - sequence writes with caching 3902 * @codec: the HDA codec 3903 * @seq: VERB array to send 3904 * 3905 * Send the commands sequentially from the given array. 3906 * Thte commands are recorded on cache for power-save and resume. 3907 * The array must be terminated with NID=0. 3908 */ 3909 void snd_hda_sequence_write_cache(struct hda_codec *codec, 3910 const struct hda_verb *seq) 3911 { 3912 for (; seq->nid; seq++) 3913 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb, 3914 seq->param); 3915 } 3916 EXPORT_SYMBOL_GPL(snd_hda_sequence_write_cache); 3917 3918 /** 3919 * snd_hda_codec_flush_cache - Execute all pending (cached) amps / verbs 3920 * @codec: HD-audio codec 3921 */ 3922 void snd_hda_codec_flush_cache(struct hda_codec *codec) 3923 { 3924 snd_hda_codec_resume_amp(codec); 3925 snd_hda_codec_resume_cache(codec); 3926 } 3927 EXPORT_SYMBOL_GPL(snd_hda_codec_flush_cache); 3928 3929 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg, 3930 unsigned int power_state) 3931 { 3932 hda_nid_t nid = codec->start_nid; 3933 int i; 3934 3935 for (i = 0; i < codec->num_nodes; i++, nid++) { 3936 unsigned int wcaps = get_wcaps(codec, nid); 3937 unsigned int state = power_state; 3938 if (!(wcaps & AC_WCAP_POWER)) 3939 continue; 3940 if (codec->power_filter) { 3941 state = codec->power_filter(codec, nid, power_state); 3942 if (state != power_state && power_state == AC_PWRST_D3) 3943 continue; 3944 } 3945 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, 3946 state); 3947 } 3948 } 3949 EXPORT_SYMBOL_GPL(snd_hda_codec_set_power_to_all); 3950 3951 /* 3952 * supported power states check 3953 */ 3954 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec, hda_nid_t fg, 3955 unsigned int power_state) 3956 { 3957 int sup = snd_hda_param_read(codec, fg, AC_PAR_POWER_STATE); 3958 3959 if (sup == -1) 3960 return false; 3961 if (sup & power_state) 3962 return true; 3963 else 3964 return false; 3965 } 3966 3967 /* 3968 * wait until the state is reached, returns the current state 3969 */ 3970 static unsigned int hda_sync_power_state(struct hda_codec *codec, 3971 hda_nid_t fg, 3972 unsigned int power_state) 3973 { 3974 unsigned long end_time = jiffies + msecs_to_jiffies(500); 3975 unsigned int state, actual_state; 3976 3977 for (;;) { 3978 state = snd_hda_codec_read(codec, fg, 0, 3979 AC_VERB_GET_POWER_STATE, 0); 3980 if (state & AC_PWRST_ERROR) 3981 break; 3982 actual_state = (state >> 4) & 0x0f; 3983 if (actual_state == power_state) 3984 break; 3985 if (time_after_eq(jiffies, end_time)) 3986 break; 3987 /* wait until the codec reachs to the target state */ 3988 msleep(1); 3989 } 3990 return state; 3991 } 3992 3993 /* don't power down the widget if it controls eapd and EAPD_BTLENABLE is set */ 3994 unsigned int snd_hda_codec_eapd_power_filter(struct hda_codec *codec, 3995 hda_nid_t nid, 3996 unsigned int power_state) 3997 { 3998 if (nid == codec->afg || nid == codec->mfg) 3999 return power_state; 4000 if (power_state == AC_PWRST_D3 && 4001 get_wcaps_type(get_wcaps(codec, nid)) == AC_WID_PIN && 4002 (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) { 4003 int eapd = snd_hda_codec_read(codec, nid, 0, 4004 AC_VERB_GET_EAPD_BTLENABLE, 0); 4005 if (eapd & 0x02) 4006 return AC_PWRST_D0; 4007 } 4008 return power_state; 4009 } 4010 EXPORT_SYMBOL_GPL(snd_hda_codec_eapd_power_filter); 4011 4012 /* 4013 * set power state of the codec, and return the power state 4014 */ 4015 static unsigned int hda_set_power_state(struct hda_codec *codec, 4016 unsigned int power_state) 4017 { 4018 hda_nid_t fg = codec->afg ? codec->afg : codec->mfg; 4019 int count; 4020 unsigned int state; 4021 int flags = 0; 4022 4023 /* this delay seems necessary to avoid click noise at power-down */ 4024 if (power_state == AC_PWRST_D3) { 4025 if (codec->depop_delay < 0) 4026 msleep(codec->epss ? 10 : 100); 4027 else if (codec->depop_delay > 0) 4028 msleep(codec->depop_delay); 4029 flags = HDA_RW_NO_RESPONSE_FALLBACK; 4030 } 4031 4032 /* repeat power states setting at most 10 times*/ 4033 for (count = 0; count < 10; count++) { 4034 if (codec->patch_ops.set_power_state) 4035 codec->patch_ops.set_power_state(codec, fg, 4036 power_state); 4037 else { 4038 state = power_state; 4039 if (codec->power_filter) 4040 state = codec->power_filter(codec, fg, state); 4041 if (state == power_state || power_state != AC_PWRST_D3) 4042 snd_hda_codec_read(codec, fg, flags, 4043 AC_VERB_SET_POWER_STATE, 4044 state); 4045 snd_hda_codec_set_power_to_all(codec, fg, power_state); 4046 } 4047 state = hda_sync_power_state(codec, fg, power_state); 4048 if (!(state & AC_PWRST_ERROR)) 4049 break; 4050 } 4051 4052 return state; 4053 } 4054 4055 /* sync power states of all widgets; 4056 * this is called at the end of codec parsing 4057 */ 4058 static void sync_power_up_states(struct hda_codec *codec) 4059 { 4060 hda_nid_t nid = codec->start_nid; 4061 int i; 4062 4063 /* don't care if no filter is used */ 4064 if (!codec->power_filter) 4065 return; 4066 4067 for (i = 0; i < codec->num_nodes; i++, nid++) { 4068 unsigned int wcaps = get_wcaps(codec, nid); 4069 unsigned int target; 4070 if (!(wcaps & AC_WCAP_POWER)) 4071 continue; 4072 target = codec->power_filter(codec, nid, AC_PWRST_D0); 4073 if (target == AC_PWRST_D0) 4074 continue; 4075 if (!snd_hda_check_power_state(codec, nid, target)) 4076 snd_hda_codec_write(codec, nid, 0, 4077 AC_VERB_SET_POWER_STATE, target); 4078 } 4079 } 4080 4081 #ifdef CONFIG_SND_HDA_RECONFIG 4082 /* execute additional init verbs */ 4083 static void hda_exec_init_verbs(struct hda_codec *codec) 4084 { 4085 if (codec->init_verbs.list) 4086 snd_hda_sequence_write(codec, codec->init_verbs.list); 4087 } 4088 #else 4089 static inline void hda_exec_init_verbs(struct hda_codec *codec) {} 4090 #endif 4091 4092 #ifdef CONFIG_PM 4093 /* 4094 * call suspend and power-down; used both from PM and power-save 4095 * this function returns the power state in the end 4096 */ 4097 static unsigned int hda_call_codec_suspend(struct hda_codec *codec, bool in_wq) 4098 { 4099 unsigned int state; 4100 4101 codec->in_pm = 1; 4102 4103 if (codec->patch_ops.suspend) 4104 codec->patch_ops.suspend(codec); 4105 hda_cleanup_all_streams(codec); 4106 state = hda_set_power_state(codec, AC_PWRST_D3); 4107 /* Cancel delayed work if we aren't currently running from it. */ 4108 if (!in_wq) 4109 cancel_delayed_work_sync(&codec->power_work); 4110 spin_lock(&codec->power_lock); 4111 snd_hda_update_power_acct(codec); 4112 trace_hda_power_down(codec); 4113 codec->power_on = 0; 4114 codec->power_transition = 0; 4115 codec->power_jiffies = jiffies; 4116 spin_unlock(&codec->power_lock); 4117 codec->in_pm = 0; 4118 return state; 4119 } 4120 4121 /* mark all entries of cmd and amp caches dirty */ 4122 static void hda_mark_cmd_cache_dirty(struct hda_codec *codec) 4123 { 4124 int i; 4125 for (i = 0; i < codec->cmd_cache.buf.used; i++) { 4126 struct hda_cache_head *cmd; 4127 cmd = snd_array_elem(&codec->cmd_cache.buf, i); 4128 cmd->dirty = 1; 4129 } 4130 for (i = 0; i < codec->amp_cache.buf.used; i++) { 4131 struct hda_amp_info *amp; 4132 amp = snd_array_elem(&codec->amp_cache.buf, i); 4133 amp->head.dirty = 1; 4134 } 4135 } 4136 4137 /* 4138 * kick up codec; used both from PM and power-save 4139 */ 4140 static void hda_call_codec_resume(struct hda_codec *codec) 4141 { 4142 codec->in_pm = 1; 4143 4144 hda_mark_cmd_cache_dirty(codec); 4145 4146 /* set as if powered on for avoiding re-entering the resume 4147 * in the resume / power-save sequence 4148 */ 4149 hda_keep_power_on(codec); 4150 hda_set_power_state(codec, AC_PWRST_D0); 4151 restore_shutup_pins(codec); 4152 hda_exec_init_verbs(codec); 4153 snd_hda_jack_set_dirty_all(codec); 4154 if (codec->patch_ops.resume) 4155 codec->patch_ops.resume(codec); 4156 else { 4157 if (codec->patch_ops.init) 4158 codec->patch_ops.init(codec); 4159 snd_hda_codec_resume_amp(codec); 4160 snd_hda_codec_resume_cache(codec); 4161 } 4162 4163 if (codec->jackpoll_interval) 4164 hda_jackpoll_work(&codec->jackpoll_work.work); 4165 else 4166 snd_hda_jack_report_sync(codec); 4167 4168 codec->in_pm = 0; 4169 snd_hda_power_down(codec); /* flag down before returning */ 4170 } 4171 #endif /* CONFIG_PM */ 4172 4173 4174 /** 4175 * snd_hda_build_controls - build mixer controls 4176 * @bus: the BUS 4177 * 4178 * Creates mixer controls for each codec included in the bus. 4179 * 4180 * Returns 0 if successful, otherwise a negative error code. 4181 */ 4182 int snd_hda_build_controls(struct hda_bus *bus) 4183 { 4184 struct hda_codec *codec; 4185 4186 list_for_each_entry(codec, &bus->codec_list, list) { 4187 int err = snd_hda_codec_build_controls(codec); 4188 if (err < 0) { 4189 codec_err(codec, 4190 "cannot build controls for #%d (error %d)\n", 4191 codec->addr, err); 4192 err = snd_hda_codec_reset(codec); 4193 if (err < 0) { 4194 codec_err(codec, 4195 "cannot revert codec\n"); 4196 return err; 4197 } 4198 } 4199 } 4200 return 0; 4201 } 4202 EXPORT_SYMBOL_GPL(snd_hda_build_controls); 4203 4204 /* 4205 * add standard channel maps if not specified 4206 */ 4207 static int add_std_chmaps(struct hda_codec *codec) 4208 { 4209 int i, str, err; 4210 4211 for (i = 0; i < codec->num_pcms; i++) { 4212 for (str = 0; str < 2; str++) { 4213 struct snd_pcm *pcm = codec->pcm_info[i].pcm; 4214 struct hda_pcm_stream *hinfo = 4215 &codec->pcm_info[i].stream[str]; 4216 struct snd_pcm_chmap *chmap; 4217 const struct snd_pcm_chmap_elem *elem; 4218 4219 if (codec->pcm_info[i].own_chmap) 4220 continue; 4221 if (!pcm || !hinfo->substreams) 4222 continue; 4223 elem = hinfo->chmap ? hinfo->chmap : snd_pcm_std_chmaps; 4224 err = snd_pcm_add_chmap_ctls(pcm, str, elem, 4225 hinfo->channels_max, 4226 0, &chmap); 4227 if (err < 0) 4228 return err; 4229 chmap->channel_mask = SND_PCM_CHMAP_MASK_2468; 4230 } 4231 } 4232 return 0; 4233 } 4234 4235 /* default channel maps for 2.1 speakers; 4236 * since HD-audio supports only stereo, odd number channels are omitted 4237 */ 4238 const struct snd_pcm_chmap_elem snd_pcm_2_1_chmaps[] = { 4239 { .channels = 2, 4240 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR } }, 4241 { .channels = 4, 4242 .map = { SNDRV_CHMAP_FL, SNDRV_CHMAP_FR, 4243 SNDRV_CHMAP_LFE, SNDRV_CHMAP_LFE } }, 4244 { } 4245 }; 4246 EXPORT_SYMBOL_GPL(snd_pcm_2_1_chmaps); 4247 4248 int snd_hda_codec_build_controls(struct hda_codec *codec) 4249 { 4250 int err = 0; 4251 hda_exec_init_verbs(codec); 4252 /* continue to initialize... */ 4253 if (codec->patch_ops.init) 4254 err = codec->patch_ops.init(codec); 4255 if (!err && codec->patch_ops.build_controls) 4256 err = codec->patch_ops.build_controls(codec); 4257 if (err < 0) 4258 return err; 4259 4260 /* we create chmaps here instead of build_pcms */ 4261 err = add_std_chmaps(codec); 4262 if (err < 0) 4263 return err; 4264 4265 if (codec->jackpoll_interval) 4266 hda_jackpoll_work(&codec->jackpoll_work.work); 4267 else 4268 snd_hda_jack_report_sync(codec); /* call at the last init point */ 4269 sync_power_up_states(codec); 4270 return 0; 4271 } 4272 4273 /* 4274 * stream formats 4275 */ 4276 struct hda_rate_tbl { 4277 unsigned int hz; 4278 unsigned int alsa_bits; 4279 unsigned int hda_fmt; 4280 }; 4281 4282 /* rate = base * mult / div */ 4283 #define HDA_RATE(base, mult, div) \ 4284 (AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \ 4285 (((div) - 1) << AC_FMT_DIV_SHIFT)) 4286 4287 static struct hda_rate_tbl rate_bits[] = { 4288 /* rate in Hz, ALSA rate bitmask, HDA format value */ 4289 4290 /* autodetected value used in snd_hda_query_supported_pcm */ 4291 { 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) }, 4292 { 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) }, 4293 { 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) }, 4294 { 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) }, 4295 { 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) }, 4296 { 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) }, 4297 { 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) }, 4298 { 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) }, 4299 { 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) }, 4300 { 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) }, 4301 { 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) }, 4302 #define AC_PAR_PCM_RATE_BITS 11 4303 /* up to bits 10, 384kHZ isn't supported properly */ 4304 4305 /* not autodetected value */ 4306 { 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) }, 4307 4308 { 0 } /* terminator */ 4309 }; 4310 4311 /** 4312 * snd_hda_calc_stream_format - calculate format bitset 4313 * @codec: HD-audio codec 4314 * @rate: the sample rate 4315 * @channels: the number of channels 4316 * @format: the PCM format (SNDRV_PCM_FORMAT_XXX) 4317 * @maxbps: the max. bps 4318 * 4319 * Calculate the format bitset from the given rate, channels and th PCM format. 4320 * 4321 * Return zero if invalid. 4322 */ 4323 unsigned int snd_hda_calc_stream_format(struct hda_codec *codec, 4324 unsigned int rate, 4325 unsigned int channels, 4326 unsigned int format, 4327 unsigned int maxbps, 4328 unsigned short spdif_ctls) 4329 { 4330 int i; 4331 unsigned int val = 0; 4332 4333 for (i = 0; rate_bits[i].hz; i++) 4334 if (rate_bits[i].hz == rate) { 4335 val = rate_bits[i].hda_fmt; 4336 break; 4337 } 4338 if (!rate_bits[i].hz) { 4339 codec_dbg(codec, "invalid rate %d\n", rate); 4340 return 0; 4341 } 4342 4343 if (channels == 0 || channels > 8) { 4344 codec_dbg(codec, "invalid channels %d\n", channels); 4345 return 0; 4346 } 4347 val |= channels - 1; 4348 4349 switch (snd_pcm_format_width(format)) { 4350 case 8: 4351 val |= AC_FMT_BITS_8; 4352 break; 4353 case 16: 4354 val |= AC_FMT_BITS_16; 4355 break; 4356 case 20: 4357 case 24: 4358 case 32: 4359 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE) 4360 val |= AC_FMT_BITS_32; 4361 else if (maxbps >= 24) 4362 val |= AC_FMT_BITS_24; 4363 else 4364 val |= AC_FMT_BITS_20; 4365 break; 4366 default: 4367 codec_dbg(codec, "invalid format width %d\n", 4368 snd_pcm_format_width(format)); 4369 return 0; 4370 } 4371 4372 if (spdif_ctls & AC_DIG1_NONAUDIO) 4373 val |= AC_FMT_TYPE_NON_PCM; 4374 4375 return val; 4376 } 4377 EXPORT_SYMBOL_GPL(snd_hda_calc_stream_format); 4378 4379 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid, 4380 int dir) 4381 { 4382 unsigned int val = 0; 4383 if (nid != codec->afg && 4384 (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) 4385 val = snd_hda_param_read(codec, nid, AC_PAR_PCM); 4386 if (!val || val == -1) 4387 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM); 4388 if (!val || val == -1) 4389 return 0; 4390 return val; 4391 } 4392 4393 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid) 4394 { 4395 return query_caps_hash(codec, nid, 0, HDA_HASH_PARPCM_KEY(nid), 4396 get_pcm_param); 4397 } 4398 4399 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid, 4400 int dir) 4401 { 4402 unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM); 4403 if (!streams || streams == -1) 4404 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM); 4405 if (!streams || streams == -1) 4406 return 0; 4407 return streams; 4408 } 4409 4410 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid) 4411 { 4412 return query_caps_hash(codec, nid, 0, HDA_HASH_PARSTR_KEY(nid), 4413 get_stream_param); 4414 } 4415 4416 /** 4417 * snd_hda_query_supported_pcm - query the supported PCM rates and formats 4418 * @codec: the HDA codec 4419 * @nid: NID to query 4420 * @ratesp: the pointer to store the detected rate bitflags 4421 * @formatsp: the pointer to store the detected formats 4422 * @bpsp: the pointer to store the detected format widths 4423 * 4424 * Queries the supported PCM rates and formats. The NULL @ratesp, @formatsp 4425 * or @bsps argument is ignored. 4426 * 4427 * Returns 0 if successful, otherwise a negative error code. 4428 */ 4429 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid, 4430 u32 *ratesp, u64 *formatsp, unsigned int *bpsp) 4431 { 4432 unsigned int i, val, wcaps; 4433 4434 wcaps = get_wcaps(codec, nid); 4435 val = query_pcm_param(codec, nid); 4436 4437 if (ratesp) { 4438 u32 rates = 0; 4439 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) { 4440 if (val & (1 << i)) 4441 rates |= rate_bits[i].alsa_bits; 4442 } 4443 if (rates == 0) { 4444 codec_err(codec, 4445 "rates == 0 (nid=0x%x, val=0x%x, ovrd=%i)\n", 4446 nid, val, 4447 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0); 4448 return -EIO; 4449 } 4450 *ratesp = rates; 4451 } 4452 4453 if (formatsp || bpsp) { 4454 u64 formats = 0; 4455 unsigned int streams, bps; 4456 4457 streams = query_stream_param(codec, nid); 4458 if (!streams) 4459 return -EIO; 4460 4461 bps = 0; 4462 if (streams & AC_SUPFMT_PCM) { 4463 if (val & AC_SUPPCM_BITS_8) { 4464 formats |= SNDRV_PCM_FMTBIT_U8; 4465 bps = 8; 4466 } 4467 if (val & AC_SUPPCM_BITS_16) { 4468 formats |= SNDRV_PCM_FMTBIT_S16_LE; 4469 bps = 16; 4470 } 4471 if (wcaps & AC_WCAP_DIGITAL) { 4472 if (val & AC_SUPPCM_BITS_32) 4473 formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE; 4474 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24)) 4475 formats |= SNDRV_PCM_FMTBIT_S32_LE; 4476 if (val & AC_SUPPCM_BITS_24) 4477 bps = 24; 4478 else if (val & AC_SUPPCM_BITS_20) 4479 bps = 20; 4480 } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24| 4481 AC_SUPPCM_BITS_32)) { 4482 formats |= SNDRV_PCM_FMTBIT_S32_LE; 4483 if (val & AC_SUPPCM_BITS_32) 4484 bps = 32; 4485 else if (val & AC_SUPPCM_BITS_24) 4486 bps = 24; 4487 else if (val & AC_SUPPCM_BITS_20) 4488 bps = 20; 4489 } 4490 } 4491 #if 0 /* FIXME: CS4206 doesn't work, which is the only codec supporting float */ 4492 if (streams & AC_SUPFMT_FLOAT32) { 4493 formats |= SNDRV_PCM_FMTBIT_FLOAT_LE; 4494 if (!bps) 4495 bps = 32; 4496 } 4497 #endif 4498 if (streams == AC_SUPFMT_AC3) { 4499 /* should be exclusive */ 4500 /* temporary hack: we have still no proper support 4501 * for the direct AC3 stream... 4502 */ 4503 formats |= SNDRV_PCM_FMTBIT_U8; 4504 bps = 8; 4505 } 4506 if (formats == 0) { 4507 codec_err(codec, 4508 "formats == 0 (nid=0x%x, val=0x%x, ovrd=%i, streams=0x%x)\n", 4509 nid, val, 4510 (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0, 4511 streams); 4512 return -EIO; 4513 } 4514 if (formatsp) 4515 *formatsp = formats; 4516 if (bpsp) 4517 *bpsp = bps; 4518 } 4519 4520 return 0; 4521 } 4522 EXPORT_SYMBOL_GPL(snd_hda_query_supported_pcm); 4523 4524 /** 4525 * snd_hda_is_supported_format - Check the validity of the format 4526 * @codec: HD-audio codec 4527 * @nid: NID to check 4528 * @format: the HD-audio format value to check 4529 * 4530 * Check whether the given node supports the format value. 4531 * 4532 * Returns 1 if supported, 0 if not. 4533 */ 4534 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid, 4535 unsigned int format) 4536 { 4537 int i; 4538 unsigned int val = 0, rate, stream; 4539 4540 val = query_pcm_param(codec, nid); 4541 if (!val) 4542 return 0; 4543 4544 rate = format & 0xff00; 4545 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) 4546 if (rate_bits[i].hda_fmt == rate) { 4547 if (val & (1 << i)) 4548 break; 4549 return 0; 4550 } 4551 if (i >= AC_PAR_PCM_RATE_BITS) 4552 return 0; 4553 4554 stream = query_stream_param(codec, nid); 4555 if (!stream) 4556 return 0; 4557 4558 if (stream & AC_SUPFMT_PCM) { 4559 switch (format & 0xf0) { 4560 case 0x00: 4561 if (!(val & AC_SUPPCM_BITS_8)) 4562 return 0; 4563 break; 4564 case 0x10: 4565 if (!(val & AC_SUPPCM_BITS_16)) 4566 return 0; 4567 break; 4568 case 0x20: 4569 if (!(val & AC_SUPPCM_BITS_20)) 4570 return 0; 4571 break; 4572 case 0x30: 4573 if (!(val & AC_SUPPCM_BITS_24)) 4574 return 0; 4575 break; 4576 case 0x40: 4577 if (!(val & AC_SUPPCM_BITS_32)) 4578 return 0; 4579 break; 4580 default: 4581 return 0; 4582 } 4583 } else { 4584 /* FIXME: check for float32 and AC3? */ 4585 } 4586 4587 return 1; 4588 } 4589 EXPORT_SYMBOL_GPL(snd_hda_is_supported_format); 4590 4591 /* 4592 * PCM stuff 4593 */ 4594 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo, 4595 struct hda_codec *codec, 4596 struct snd_pcm_substream *substream) 4597 { 4598 return 0; 4599 } 4600 4601 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo, 4602 struct hda_codec *codec, 4603 unsigned int stream_tag, 4604 unsigned int format, 4605 struct snd_pcm_substream *substream) 4606 { 4607 snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format); 4608 return 0; 4609 } 4610 4611 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo, 4612 struct hda_codec *codec, 4613 struct snd_pcm_substream *substream) 4614 { 4615 snd_hda_codec_cleanup_stream(codec, hinfo->nid); 4616 return 0; 4617 } 4618 4619 static int set_pcm_default_values(struct hda_codec *codec, 4620 struct hda_pcm_stream *info) 4621 { 4622 int err; 4623 4624 /* query support PCM information from the given NID */ 4625 if (info->nid && (!info->rates || !info->formats)) { 4626 err = snd_hda_query_supported_pcm(codec, info->nid, 4627 info->rates ? NULL : &info->rates, 4628 info->formats ? NULL : &info->formats, 4629 info->maxbps ? NULL : &info->maxbps); 4630 if (err < 0) 4631 return err; 4632 } 4633 if (info->ops.open == NULL) 4634 info->ops.open = hda_pcm_default_open_close; 4635 if (info->ops.close == NULL) 4636 info->ops.close = hda_pcm_default_open_close; 4637 if (info->ops.prepare == NULL) { 4638 if (snd_BUG_ON(!info->nid)) 4639 return -EINVAL; 4640 info->ops.prepare = hda_pcm_default_prepare; 4641 } 4642 if (info->ops.cleanup == NULL) { 4643 if (snd_BUG_ON(!info->nid)) 4644 return -EINVAL; 4645 info->ops.cleanup = hda_pcm_default_cleanup; 4646 } 4647 return 0; 4648 } 4649 4650 /* 4651 * codec prepare/cleanup entries 4652 */ 4653 int snd_hda_codec_prepare(struct hda_codec *codec, 4654 struct hda_pcm_stream *hinfo, 4655 unsigned int stream, 4656 unsigned int format, 4657 struct snd_pcm_substream *substream) 4658 { 4659 int ret; 4660 mutex_lock(&codec->bus->prepare_mutex); 4661 ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream); 4662 if (ret >= 0) 4663 purify_inactive_streams(codec); 4664 mutex_unlock(&codec->bus->prepare_mutex); 4665 return ret; 4666 } 4667 EXPORT_SYMBOL_GPL(snd_hda_codec_prepare); 4668 4669 void snd_hda_codec_cleanup(struct hda_codec *codec, 4670 struct hda_pcm_stream *hinfo, 4671 struct snd_pcm_substream *substream) 4672 { 4673 mutex_lock(&codec->bus->prepare_mutex); 4674 hinfo->ops.cleanup(hinfo, codec, substream); 4675 mutex_unlock(&codec->bus->prepare_mutex); 4676 } 4677 EXPORT_SYMBOL_GPL(snd_hda_codec_cleanup); 4678 4679 /* global */ 4680 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = { 4681 "Audio", "SPDIF", "HDMI", "Modem" 4682 }; 4683 4684 /* 4685 * get the empty PCM device number to assign 4686 */ 4687 static int get_empty_pcm_device(struct hda_bus *bus, unsigned int type) 4688 { 4689 /* audio device indices; not linear to keep compatibility */ 4690 /* assigned to static slots up to dev#10; if more needed, assign 4691 * the later slot dynamically (when CONFIG_SND_DYNAMIC_MINORS=y) 4692 */ 4693 static int audio_idx[HDA_PCM_NTYPES][5] = { 4694 [HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 }, 4695 [HDA_PCM_TYPE_SPDIF] = { 1, -1 }, 4696 [HDA_PCM_TYPE_HDMI] = { 3, 7, 8, 9, -1 }, 4697 [HDA_PCM_TYPE_MODEM] = { 6, -1 }, 4698 }; 4699 int i; 4700 4701 if (type >= HDA_PCM_NTYPES) { 4702 dev_err(bus->card->dev, "Invalid PCM type %d\n", type); 4703 return -EINVAL; 4704 } 4705 4706 for (i = 0; audio_idx[type][i] >= 0; i++) { 4707 #ifndef CONFIG_SND_DYNAMIC_MINORS 4708 if (audio_idx[type][i] >= 8) 4709 break; 4710 #endif 4711 if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits)) 4712 return audio_idx[type][i]; 4713 } 4714 4715 #ifdef CONFIG_SND_DYNAMIC_MINORS 4716 /* non-fixed slots starting from 10 */ 4717 for (i = 10; i < 32; i++) { 4718 if (!test_and_set_bit(i, bus->pcm_dev_bits)) 4719 return i; 4720 } 4721 #endif 4722 4723 dev_warn(bus->card->dev, "Too many %s devices\n", 4724 snd_hda_pcm_type_name[type]); 4725 #ifndef CONFIG_SND_DYNAMIC_MINORS 4726 dev_warn(bus->card->dev, 4727 "Consider building the kernel with CONFIG_SND_DYNAMIC_MINORS=y\n"); 4728 #endif 4729 return -EAGAIN; 4730 } 4731 4732 /* 4733 * attach a new PCM stream 4734 */ 4735 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm) 4736 { 4737 struct hda_bus *bus = codec->bus; 4738 struct hda_pcm_stream *info; 4739 int stream, err; 4740 4741 if (snd_BUG_ON(!pcm->name)) 4742 return -EINVAL; 4743 for (stream = 0; stream < 2; stream++) { 4744 info = &pcm->stream[stream]; 4745 if (info->substreams) { 4746 err = set_pcm_default_values(codec, info); 4747 if (err < 0) 4748 return err; 4749 } 4750 } 4751 return bus->ops.attach_pcm(bus, codec, pcm); 4752 } 4753 4754 /* assign all PCMs of the given codec */ 4755 int snd_hda_codec_build_pcms(struct hda_codec *codec) 4756 { 4757 unsigned int pcm; 4758 int err; 4759 4760 if (!codec->num_pcms) { 4761 if (!codec->patch_ops.build_pcms) 4762 return 0; 4763 err = codec->patch_ops.build_pcms(codec); 4764 if (err < 0) { 4765 codec_err(codec, 4766 "cannot build PCMs for #%d (error %d)\n", 4767 codec->addr, err); 4768 err = snd_hda_codec_reset(codec); 4769 if (err < 0) { 4770 codec_err(codec, 4771 "cannot revert codec\n"); 4772 return err; 4773 } 4774 } 4775 } 4776 for (pcm = 0; pcm < codec->num_pcms; pcm++) { 4777 struct hda_pcm *cpcm = &codec->pcm_info[pcm]; 4778 int dev; 4779 4780 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams) 4781 continue; /* no substreams assigned */ 4782 4783 if (!cpcm->pcm) { 4784 dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type); 4785 if (dev < 0) 4786 continue; /* no fatal error */ 4787 cpcm->device = dev; 4788 err = snd_hda_attach_pcm(codec, cpcm); 4789 if (err < 0) { 4790 codec_err(codec, 4791 "cannot attach PCM stream %d for codec #%d\n", 4792 dev, codec->addr); 4793 continue; /* no fatal error */ 4794 } 4795 } 4796 } 4797 return 0; 4798 } 4799 4800 /** 4801 * snd_hda_build_pcms - build PCM information 4802 * @bus: the BUS 4803 * 4804 * Create PCM information for each codec included in the bus. 4805 * 4806 * The build_pcms codec patch is requested to set up codec->num_pcms and 4807 * codec->pcm_info properly. The array is referred by the top-level driver 4808 * to create its PCM instances. 4809 * The allocated codec->pcm_info should be released in codec->patch_ops.free 4810 * callback. 4811 * 4812 * At least, substreams, channels_min and channels_max must be filled for 4813 * each stream. substreams = 0 indicates that the stream doesn't exist. 4814 * When rates and/or formats are zero, the supported values are queried 4815 * from the given nid. The nid is used also by the default ops.prepare 4816 * and ops.cleanup callbacks. 4817 * 4818 * The driver needs to call ops.open in its open callback. Similarly, 4819 * ops.close is supposed to be called in the close callback. 4820 * ops.prepare should be called in the prepare or hw_params callback 4821 * with the proper parameters for set up. 4822 * ops.cleanup should be called in hw_free for clean up of streams. 4823 * 4824 * This function returns 0 if successful, or a negative error code. 4825 */ 4826 int snd_hda_build_pcms(struct hda_bus *bus) 4827 { 4828 struct hda_codec *codec; 4829 4830 list_for_each_entry(codec, &bus->codec_list, list) { 4831 int err = snd_hda_codec_build_pcms(codec); 4832 if (err < 0) 4833 return err; 4834 } 4835 return 0; 4836 } 4837 EXPORT_SYMBOL_GPL(snd_hda_build_pcms); 4838 4839 /** 4840 * snd_hda_add_new_ctls - create controls from the array 4841 * @codec: the HDA codec 4842 * @knew: the array of struct snd_kcontrol_new 4843 * 4844 * This helper function creates and add new controls in the given array. 4845 * The array must be terminated with an empty entry as terminator. 4846 * 4847 * Returns 0 if successful, or a negative error code. 4848 */ 4849 int snd_hda_add_new_ctls(struct hda_codec *codec, 4850 const struct snd_kcontrol_new *knew) 4851 { 4852 int err; 4853 4854 for (; knew->name; knew++) { 4855 struct snd_kcontrol *kctl; 4856 int addr = 0, idx = 0; 4857 if (knew->iface == -1) /* skip this codec private value */ 4858 continue; 4859 for (;;) { 4860 kctl = snd_ctl_new1(knew, codec); 4861 if (!kctl) 4862 return -ENOMEM; 4863 if (addr > 0) 4864 kctl->id.device = addr; 4865 if (idx > 0) 4866 kctl->id.index = idx; 4867 err = snd_hda_ctl_add(codec, 0, kctl); 4868 if (!err) 4869 break; 4870 /* try first with another device index corresponding to 4871 * the codec addr; if it still fails (or it's the 4872 * primary codec), then try another control index 4873 */ 4874 if (!addr && codec->addr) 4875 addr = codec->addr; 4876 else if (!idx && !knew->index) { 4877 idx = find_empty_mixer_ctl_idx(codec, 4878 knew->name, 0); 4879 if (idx <= 0) 4880 return err; 4881 } else 4882 return err; 4883 } 4884 } 4885 return 0; 4886 } 4887 EXPORT_SYMBOL_GPL(snd_hda_add_new_ctls); 4888 4889 #ifdef CONFIG_PM 4890 static void hda_power_work(struct work_struct *work) 4891 { 4892 struct hda_codec *codec = 4893 container_of(work, struct hda_codec, power_work.work); 4894 struct hda_bus *bus = codec->bus; 4895 unsigned int state; 4896 4897 spin_lock(&codec->power_lock); 4898 if (codec->power_transition > 0) { /* during power-up sequence? */ 4899 spin_unlock(&codec->power_lock); 4900 return; 4901 } 4902 if (!codec->power_on || codec->power_count) { 4903 codec->power_transition = 0; 4904 spin_unlock(&codec->power_lock); 4905 return; 4906 } 4907 spin_unlock(&codec->power_lock); 4908 4909 state = hda_call_codec_suspend(codec, true); 4910 if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) 4911 hda_call_pm_notify(codec, false); 4912 } 4913 4914 static void hda_keep_power_on(struct hda_codec *codec) 4915 { 4916 spin_lock(&codec->power_lock); 4917 codec->power_count++; 4918 codec->power_on = 1; 4919 codec->power_jiffies = jiffies; 4920 spin_unlock(&codec->power_lock); 4921 hda_call_pm_notify(codec, true); 4922 } 4923 4924 /* update the power on/off account with the current jiffies */ 4925 void snd_hda_update_power_acct(struct hda_codec *codec) 4926 { 4927 unsigned long delta = jiffies - codec->power_jiffies; 4928 if (codec->power_on) 4929 codec->power_on_acct += delta; 4930 else 4931 codec->power_off_acct += delta; 4932 codec->power_jiffies += delta; 4933 } 4934 4935 /* Transition to powered up, if wait_power_down then wait for a pending 4936 * transition to D3 to complete. A pending D3 transition is indicated 4937 * with power_transition == -1. */ 4938 /* call this with codec->power_lock held! */ 4939 static void __snd_hda_power_up(struct hda_codec *codec, bool wait_power_down) 4940 { 4941 /* Return if power_on or transitioning to power_on, unless currently 4942 * powering down. */ 4943 if ((codec->power_on || codec->power_transition > 0) && 4944 !(wait_power_down && codec->power_transition < 0)) 4945 return; 4946 spin_unlock(&codec->power_lock); 4947 4948 cancel_delayed_work_sync(&codec->power_work); 4949 4950 spin_lock(&codec->power_lock); 4951 /* If the power down delayed work was cancelled above before starting, 4952 * then there is no need to go through power up here. 4953 */ 4954 if (codec->power_on) { 4955 if (codec->power_transition < 0) 4956 codec->power_transition = 0; 4957 return; 4958 } 4959 4960 trace_hda_power_up(codec); 4961 snd_hda_update_power_acct(codec); 4962 codec->power_on = 1; 4963 codec->power_jiffies = jiffies; 4964 codec->power_transition = 1; /* avoid reentrance */ 4965 spin_unlock(&codec->power_lock); 4966 4967 hda_call_codec_resume(codec); 4968 4969 spin_lock(&codec->power_lock); 4970 codec->power_transition = 0; 4971 } 4972 4973 #define power_save(codec) \ 4974 ((codec)->bus->power_save ? *(codec)->bus->power_save : 0) 4975 4976 /* Transition to powered down */ 4977 static void __snd_hda_power_down(struct hda_codec *codec) 4978 { 4979 if (!codec->power_on || codec->power_count || codec->power_transition) 4980 return; 4981 4982 if (power_save(codec)) { 4983 codec->power_transition = -1; /* avoid reentrance */ 4984 queue_delayed_work(codec->bus->workq, &codec->power_work, 4985 msecs_to_jiffies(power_save(codec) * 1000)); 4986 } 4987 } 4988 4989 /** 4990 * snd_hda_power_save - Power-up/down/sync the codec 4991 * @codec: HD-audio codec 4992 * @delta: the counter delta to change 4993 * 4994 * Change the power-up counter via @delta, and power up or down the hardware 4995 * appropriately. For the power-down, queue to the delayed action. 4996 * Passing zero to @delta means to synchronize the power state. 4997 */ 4998 void snd_hda_power_save(struct hda_codec *codec, int delta, bool d3wait) 4999 { 5000 spin_lock(&codec->power_lock); 5001 codec->power_count += delta; 5002 trace_hda_power_count(codec); 5003 if (delta > 0) 5004 __snd_hda_power_up(codec, d3wait); 5005 else 5006 __snd_hda_power_down(codec); 5007 spin_unlock(&codec->power_lock); 5008 } 5009 EXPORT_SYMBOL_GPL(snd_hda_power_save); 5010 5011 /** 5012 * snd_hda_check_amp_list_power - Check the amp list and update the power 5013 * @codec: HD-audio codec 5014 * @check: the object containing an AMP list and the status 5015 * @nid: NID to check / update 5016 * 5017 * Check whether the given NID is in the amp list. If it's in the list, 5018 * check the current AMP status, and update the the power-status according 5019 * to the mute status. 5020 * 5021 * This function is supposed to be set or called from the check_power_status 5022 * patch ops. 5023 */ 5024 int snd_hda_check_amp_list_power(struct hda_codec *codec, 5025 struct hda_loopback_check *check, 5026 hda_nid_t nid) 5027 { 5028 const struct hda_amp_list *p; 5029 int ch, v; 5030 5031 if (!check->amplist) 5032 return 0; 5033 for (p = check->amplist; p->nid; p++) { 5034 if (p->nid == nid) 5035 break; 5036 } 5037 if (!p->nid) 5038 return 0; /* nothing changed */ 5039 5040 for (p = check->amplist; p->nid; p++) { 5041 for (ch = 0; ch < 2; ch++) { 5042 v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir, 5043 p->idx); 5044 if (!(v & HDA_AMP_MUTE) && v > 0) { 5045 if (!check->power_on) { 5046 check->power_on = 1; 5047 snd_hda_power_up(codec); 5048 } 5049 return 1; 5050 } 5051 } 5052 } 5053 if (check->power_on) { 5054 check->power_on = 0; 5055 snd_hda_power_down(codec); 5056 } 5057 return 0; 5058 } 5059 EXPORT_SYMBOL_GPL(snd_hda_check_amp_list_power); 5060 #endif 5061 5062 /* 5063 * Channel mode helper 5064 */ 5065 5066 /** 5067 * snd_hda_ch_mode_info - Info callback helper for the channel mode enum 5068 */ 5069 int snd_hda_ch_mode_info(struct hda_codec *codec, 5070 struct snd_ctl_elem_info *uinfo, 5071 const struct hda_channel_mode *chmode, 5072 int num_chmodes) 5073 { 5074 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 5075 uinfo->count = 1; 5076 uinfo->value.enumerated.items = num_chmodes; 5077 if (uinfo->value.enumerated.item >= num_chmodes) 5078 uinfo->value.enumerated.item = num_chmodes - 1; 5079 sprintf(uinfo->value.enumerated.name, "%dch", 5080 chmode[uinfo->value.enumerated.item].channels); 5081 return 0; 5082 } 5083 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_info); 5084 5085 /** 5086 * snd_hda_ch_mode_get - Get callback helper for the channel mode enum 5087 */ 5088 int snd_hda_ch_mode_get(struct hda_codec *codec, 5089 struct snd_ctl_elem_value *ucontrol, 5090 const struct hda_channel_mode *chmode, 5091 int num_chmodes, 5092 int max_channels) 5093 { 5094 int i; 5095 5096 for (i = 0; i < num_chmodes; i++) { 5097 if (max_channels == chmode[i].channels) { 5098 ucontrol->value.enumerated.item[0] = i; 5099 break; 5100 } 5101 } 5102 return 0; 5103 } 5104 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_get); 5105 5106 /** 5107 * snd_hda_ch_mode_put - Put callback helper for the channel mode enum 5108 */ 5109 int snd_hda_ch_mode_put(struct hda_codec *codec, 5110 struct snd_ctl_elem_value *ucontrol, 5111 const struct hda_channel_mode *chmode, 5112 int num_chmodes, 5113 int *max_channelsp) 5114 { 5115 unsigned int mode; 5116 5117 mode = ucontrol->value.enumerated.item[0]; 5118 if (mode >= num_chmodes) 5119 return -EINVAL; 5120 if (*max_channelsp == chmode[mode].channels) 5121 return 0; 5122 /* change the current channel setting */ 5123 *max_channelsp = chmode[mode].channels; 5124 if (chmode[mode].sequence) 5125 snd_hda_sequence_write_cache(codec, chmode[mode].sequence); 5126 return 1; 5127 } 5128 EXPORT_SYMBOL_GPL(snd_hda_ch_mode_put); 5129 5130 /* 5131 * input MUX helper 5132 */ 5133 5134 /** 5135 * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum 5136 */ 5137 int snd_hda_input_mux_info(const struct hda_input_mux *imux, 5138 struct snd_ctl_elem_info *uinfo) 5139 { 5140 unsigned int index; 5141 5142 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 5143 uinfo->count = 1; 5144 uinfo->value.enumerated.items = imux->num_items; 5145 if (!imux->num_items) 5146 return 0; 5147 index = uinfo->value.enumerated.item; 5148 if (index >= imux->num_items) 5149 index = imux->num_items - 1; 5150 strcpy(uinfo->value.enumerated.name, imux->items[index].label); 5151 return 0; 5152 } 5153 EXPORT_SYMBOL_GPL(snd_hda_input_mux_info); 5154 5155 /** 5156 * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum 5157 */ 5158 int snd_hda_input_mux_put(struct hda_codec *codec, 5159 const struct hda_input_mux *imux, 5160 struct snd_ctl_elem_value *ucontrol, 5161 hda_nid_t nid, 5162 unsigned int *cur_val) 5163 { 5164 unsigned int idx; 5165 5166 if (!imux->num_items) 5167 return 0; 5168 idx = ucontrol->value.enumerated.item[0]; 5169 if (idx >= imux->num_items) 5170 idx = imux->num_items - 1; 5171 if (*cur_val == idx) 5172 return 0; 5173 snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL, 5174 imux->items[idx].index); 5175 *cur_val = idx; 5176 return 1; 5177 } 5178 EXPORT_SYMBOL_GPL(snd_hda_input_mux_put); 5179 5180 5181 /* 5182 * process kcontrol info callback of a simple string enum array 5183 * when @num_items is 0 or @texts is NULL, assume a boolean enum array 5184 */ 5185 int snd_hda_enum_helper_info(struct snd_kcontrol *kcontrol, 5186 struct snd_ctl_elem_info *uinfo, 5187 int num_items, const char * const *texts) 5188 { 5189 static const char * const texts_default[] = { 5190 "Disabled", "Enabled" 5191 }; 5192 5193 if (!texts || !num_items) { 5194 num_items = 2; 5195 texts = texts_default; 5196 } 5197 5198 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 5199 uinfo->count = 1; 5200 uinfo->value.enumerated.items = num_items; 5201 if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items) 5202 uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1; 5203 strcpy(uinfo->value.enumerated.name, 5204 texts[uinfo->value.enumerated.item]); 5205 return 0; 5206 } 5207 EXPORT_SYMBOL_GPL(snd_hda_enum_helper_info); 5208 5209 /* 5210 * Multi-channel / digital-out PCM helper functions 5211 */ 5212 5213 /* setup SPDIF output stream */ 5214 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid, 5215 unsigned int stream_tag, unsigned int format) 5216 { 5217 struct hda_spdif_out *spdif; 5218 unsigned int curr_fmt; 5219 bool reset; 5220 5221 spdif = snd_hda_spdif_out_of_nid(codec, nid); 5222 curr_fmt = snd_hda_codec_read(codec, nid, 0, 5223 AC_VERB_GET_STREAM_FORMAT, 0); 5224 reset = codec->spdif_status_reset && 5225 (spdif->ctls & AC_DIG1_ENABLE) && 5226 curr_fmt != format; 5227 5228 /* turn off SPDIF if needed; otherwise the IEC958 bits won't be 5229 updated */ 5230 if (reset) 5231 set_dig_out_convert(codec, nid, 5232 spdif->ctls & ~AC_DIG1_ENABLE & 0xff, 5233 -1); 5234 snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format); 5235 if (codec->slave_dig_outs) { 5236 const hda_nid_t *d; 5237 for (d = codec->slave_dig_outs; *d; d++) 5238 snd_hda_codec_setup_stream(codec, *d, stream_tag, 0, 5239 format); 5240 } 5241 /* turn on again (if needed) */ 5242 if (reset) 5243 set_dig_out_convert(codec, nid, 5244 spdif->ctls & 0xff, -1); 5245 } 5246 5247 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid) 5248 { 5249 snd_hda_codec_cleanup_stream(codec, nid); 5250 if (codec->slave_dig_outs) { 5251 const hda_nid_t *d; 5252 for (d = codec->slave_dig_outs; *d; d++) 5253 snd_hda_codec_cleanup_stream(codec, *d); 5254 } 5255 } 5256 5257 /** 5258 * snd_hda_bus_reboot_notify - call the reboot notifier of each codec 5259 * @bus: HD-audio bus 5260 */ 5261 void snd_hda_bus_reboot_notify(struct hda_bus *bus) 5262 { 5263 struct hda_codec *codec; 5264 5265 if (!bus) 5266 return; 5267 list_for_each_entry(codec, &bus->codec_list, list) { 5268 if (hda_codec_is_power_on(codec) && 5269 codec->patch_ops.reboot_notify) 5270 codec->patch_ops.reboot_notify(codec); 5271 } 5272 } 5273 EXPORT_SYMBOL_GPL(snd_hda_bus_reboot_notify); 5274 5275 /** 5276 * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode 5277 */ 5278 int snd_hda_multi_out_dig_open(struct hda_codec *codec, 5279 struct hda_multi_out *mout) 5280 { 5281 mutex_lock(&codec->spdif_mutex); 5282 if (mout->dig_out_used == HDA_DIG_ANALOG_DUP) 5283 /* already opened as analog dup; reset it once */ 5284 cleanup_dig_out_stream(codec, mout->dig_out_nid); 5285 mout->dig_out_used = HDA_DIG_EXCLUSIVE; 5286 mutex_unlock(&codec->spdif_mutex); 5287 return 0; 5288 } 5289 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_open); 5290 5291 /** 5292 * snd_hda_multi_out_dig_prepare - prepare the digital out stream 5293 */ 5294 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec, 5295 struct hda_multi_out *mout, 5296 unsigned int stream_tag, 5297 unsigned int format, 5298 struct snd_pcm_substream *substream) 5299 { 5300 mutex_lock(&codec->spdif_mutex); 5301 setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format); 5302 mutex_unlock(&codec->spdif_mutex); 5303 return 0; 5304 } 5305 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_prepare); 5306 5307 /** 5308 * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream 5309 */ 5310 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec, 5311 struct hda_multi_out *mout) 5312 { 5313 mutex_lock(&codec->spdif_mutex); 5314 cleanup_dig_out_stream(codec, mout->dig_out_nid); 5315 mutex_unlock(&codec->spdif_mutex); 5316 return 0; 5317 } 5318 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_cleanup); 5319 5320 /** 5321 * snd_hda_multi_out_dig_close - release the digital out stream 5322 */ 5323 int snd_hda_multi_out_dig_close(struct hda_codec *codec, 5324 struct hda_multi_out *mout) 5325 { 5326 mutex_lock(&codec->spdif_mutex); 5327 mout->dig_out_used = 0; 5328 mutex_unlock(&codec->spdif_mutex); 5329 return 0; 5330 } 5331 EXPORT_SYMBOL_GPL(snd_hda_multi_out_dig_close); 5332 5333 /** 5334 * snd_hda_multi_out_analog_open - open analog outputs 5335 * 5336 * Open analog outputs and set up the hw-constraints. 5337 * If the digital outputs can be opened as slave, open the digital 5338 * outputs, too. 5339 */ 5340 int snd_hda_multi_out_analog_open(struct hda_codec *codec, 5341 struct hda_multi_out *mout, 5342 struct snd_pcm_substream *substream, 5343 struct hda_pcm_stream *hinfo) 5344 { 5345 struct snd_pcm_runtime *runtime = substream->runtime; 5346 runtime->hw.channels_max = mout->max_channels; 5347 if (mout->dig_out_nid) { 5348 if (!mout->analog_rates) { 5349 mout->analog_rates = hinfo->rates; 5350 mout->analog_formats = hinfo->formats; 5351 mout->analog_maxbps = hinfo->maxbps; 5352 } else { 5353 runtime->hw.rates = mout->analog_rates; 5354 runtime->hw.formats = mout->analog_formats; 5355 hinfo->maxbps = mout->analog_maxbps; 5356 } 5357 if (!mout->spdif_rates) { 5358 snd_hda_query_supported_pcm(codec, mout->dig_out_nid, 5359 &mout->spdif_rates, 5360 &mout->spdif_formats, 5361 &mout->spdif_maxbps); 5362 } 5363 mutex_lock(&codec->spdif_mutex); 5364 if (mout->share_spdif) { 5365 if ((runtime->hw.rates & mout->spdif_rates) && 5366 (runtime->hw.formats & mout->spdif_formats)) { 5367 runtime->hw.rates &= mout->spdif_rates; 5368 runtime->hw.formats &= mout->spdif_formats; 5369 if (mout->spdif_maxbps < hinfo->maxbps) 5370 hinfo->maxbps = mout->spdif_maxbps; 5371 } else { 5372 mout->share_spdif = 0; 5373 /* FIXME: need notify? */ 5374 } 5375 } 5376 mutex_unlock(&codec->spdif_mutex); 5377 } 5378 return snd_pcm_hw_constraint_step(substream->runtime, 0, 5379 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 5380 } 5381 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_open); 5382 5383 /** 5384 * snd_hda_multi_out_analog_prepare - Preapre the analog outputs. 5385 * 5386 * Set up the i/o for analog out. 5387 * When the digital out is available, copy the front out to digital out, too. 5388 */ 5389 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, 5390 struct hda_multi_out *mout, 5391 unsigned int stream_tag, 5392 unsigned int format, 5393 struct snd_pcm_substream *substream) 5394 { 5395 const hda_nid_t *nids = mout->dac_nids; 5396 int chs = substream->runtime->channels; 5397 struct hda_spdif_out *spdif; 5398 int i; 5399 5400 mutex_lock(&codec->spdif_mutex); 5401 spdif = snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid); 5402 if (mout->dig_out_nid && mout->share_spdif && 5403 mout->dig_out_used != HDA_DIG_EXCLUSIVE) { 5404 if (chs == 2 && 5405 snd_hda_is_supported_format(codec, mout->dig_out_nid, 5406 format) && 5407 !(spdif->status & IEC958_AES0_NONAUDIO)) { 5408 mout->dig_out_used = HDA_DIG_ANALOG_DUP; 5409 setup_dig_out_stream(codec, mout->dig_out_nid, 5410 stream_tag, format); 5411 } else { 5412 mout->dig_out_used = 0; 5413 cleanup_dig_out_stream(codec, mout->dig_out_nid); 5414 } 5415 } 5416 mutex_unlock(&codec->spdif_mutex); 5417 5418 /* front */ 5419 snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 5420 0, format); 5421 if (!mout->no_share_stream && 5422 mout->hp_nid && mout->hp_nid != nids[HDA_FRONT]) 5423 /* headphone out will just decode front left/right (stereo) */ 5424 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 5425 0, format); 5426 /* extra outputs copied from front */ 5427 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) 5428 if (!mout->no_share_stream && mout->hp_out_nid[i]) 5429 snd_hda_codec_setup_stream(codec, 5430 mout->hp_out_nid[i], 5431 stream_tag, 0, format); 5432 5433 /* surrounds */ 5434 for (i = 1; i < mout->num_dacs; i++) { 5435 if (chs >= (i + 1) * 2) /* independent out */ 5436 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 5437 i * 2, format); 5438 else if (!mout->no_share_stream) /* copy front */ 5439 snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 5440 0, format); 5441 } 5442 5443 /* extra surrounds */ 5444 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) { 5445 int ch = 0; 5446 if (!mout->extra_out_nid[i]) 5447 break; 5448 if (chs >= (i + 1) * 2) 5449 ch = i * 2; 5450 else if (!mout->no_share_stream) 5451 break; 5452 snd_hda_codec_setup_stream(codec, mout->extra_out_nid[i], 5453 stream_tag, ch, format); 5454 } 5455 5456 return 0; 5457 } 5458 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_prepare); 5459 5460 /** 5461 * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out 5462 */ 5463 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, 5464 struct hda_multi_out *mout) 5465 { 5466 const hda_nid_t *nids = mout->dac_nids; 5467 int i; 5468 5469 for (i = 0; i < mout->num_dacs; i++) 5470 snd_hda_codec_cleanup_stream(codec, nids[i]); 5471 if (mout->hp_nid) 5472 snd_hda_codec_cleanup_stream(codec, mout->hp_nid); 5473 for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++) 5474 if (mout->hp_out_nid[i]) 5475 snd_hda_codec_cleanup_stream(codec, 5476 mout->hp_out_nid[i]); 5477 for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++) 5478 if (mout->extra_out_nid[i]) 5479 snd_hda_codec_cleanup_stream(codec, 5480 mout->extra_out_nid[i]); 5481 mutex_lock(&codec->spdif_mutex); 5482 if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) { 5483 cleanup_dig_out_stream(codec, mout->dig_out_nid); 5484 mout->dig_out_used = 0; 5485 } 5486 mutex_unlock(&codec->spdif_mutex); 5487 return 0; 5488 } 5489 EXPORT_SYMBOL_GPL(snd_hda_multi_out_analog_cleanup); 5490 5491 /** 5492 * snd_hda_get_default_vref - Get the default (mic) VREF pin bits 5493 * 5494 * Guess the suitable VREF pin bits to be set as the pin-control value. 5495 * Note: the function doesn't set the AC_PINCTL_IN_EN bit. 5496 */ 5497 unsigned int snd_hda_get_default_vref(struct hda_codec *codec, hda_nid_t pin) 5498 { 5499 unsigned int pincap; 5500 unsigned int oldval; 5501 oldval = snd_hda_codec_read(codec, pin, 0, 5502 AC_VERB_GET_PIN_WIDGET_CONTROL, 0); 5503 pincap = snd_hda_query_pin_caps(codec, pin); 5504 pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 5505 /* Exception: if the default pin setup is vref50, we give it priority */ 5506 if ((pincap & AC_PINCAP_VREF_80) && oldval != PIN_VREF50) 5507 return AC_PINCTL_VREF_80; 5508 else if (pincap & AC_PINCAP_VREF_50) 5509 return AC_PINCTL_VREF_50; 5510 else if (pincap & AC_PINCAP_VREF_100) 5511 return AC_PINCTL_VREF_100; 5512 else if (pincap & AC_PINCAP_VREF_GRD) 5513 return AC_PINCTL_VREF_GRD; 5514 return AC_PINCTL_VREF_HIZ; 5515 } 5516 EXPORT_SYMBOL_GPL(snd_hda_get_default_vref); 5517 5518 /* correct the pin ctl value for matching with the pin cap */ 5519 unsigned int snd_hda_correct_pin_ctl(struct hda_codec *codec, 5520 hda_nid_t pin, unsigned int val) 5521 { 5522 static unsigned int cap_lists[][2] = { 5523 { AC_PINCTL_VREF_100, AC_PINCAP_VREF_100 }, 5524 { AC_PINCTL_VREF_80, AC_PINCAP_VREF_80 }, 5525 { AC_PINCTL_VREF_50, AC_PINCAP_VREF_50 }, 5526 { AC_PINCTL_VREF_GRD, AC_PINCAP_VREF_GRD }, 5527 }; 5528 unsigned int cap; 5529 5530 if (!val) 5531 return 0; 5532 cap = snd_hda_query_pin_caps(codec, pin); 5533 if (!cap) 5534 return val; /* don't know what to do... */ 5535 5536 if (val & AC_PINCTL_OUT_EN) { 5537 if (!(cap & AC_PINCAP_OUT)) 5538 val &= ~(AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN); 5539 else if ((val & AC_PINCTL_HP_EN) && !(cap & AC_PINCAP_HP_DRV)) 5540 val &= ~AC_PINCTL_HP_EN; 5541 } 5542 5543 if (val & AC_PINCTL_IN_EN) { 5544 if (!(cap & AC_PINCAP_IN)) 5545 val &= ~(AC_PINCTL_IN_EN | AC_PINCTL_VREFEN); 5546 else { 5547 unsigned int vcap, vref; 5548 int i; 5549 vcap = (cap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT; 5550 vref = val & AC_PINCTL_VREFEN; 5551 for (i = 0; i < ARRAY_SIZE(cap_lists); i++) { 5552 if (vref == cap_lists[i][0] && 5553 !(vcap & cap_lists[i][1])) { 5554 if (i == ARRAY_SIZE(cap_lists) - 1) 5555 vref = AC_PINCTL_VREF_HIZ; 5556 else 5557 vref = cap_lists[i + 1][0]; 5558 } 5559 } 5560 val &= ~AC_PINCTL_VREFEN; 5561 val |= vref; 5562 } 5563 } 5564 5565 return val; 5566 } 5567 EXPORT_SYMBOL_GPL(snd_hda_correct_pin_ctl); 5568 5569 int _snd_hda_set_pin_ctl(struct hda_codec *codec, hda_nid_t pin, 5570 unsigned int val, bool cached) 5571 { 5572 val = snd_hda_correct_pin_ctl(codec, pin, val); 5573 snd_hda_codec_set_pin_target(codec, pin, val); 5574 if (cached) 5575 return snd_hda_codec_update_cache(codec, pin, 0, 5576 AC_VERB_SET_PIN_WIDGET_CONTROL, val); 5577 else 5578 return snd_hda_codec_write(codec, pin, 0, 5579 AC_VERB_SET_PIN_WIDGET_CONTROL, val); 5580 } 5581 EXPORT_SYMBOL_GPL(_snd_hda_set_pin_ctl); 5582 5583 /** 5584 * snd_hda_add_imux_item - Add an item to input_mux 5585 * 5586 * When the same label is used already in the existing items, the number 5587 * suffix is appended to the label. This label index number is stored 5588 * to type_idx when non-NULL pointer is given. 5589 */ 5590 int snd_hda_add_imux_item(struct hda_codec *codec, 5591 struct hda_input_mux *imux, const char *label, 5592 int index, int *type_idx) 5593 { 5594 int i, label_idx = 0; 5595 if (imux->num_items >= HDA_MAX_NUM_INPUTS) { 5596 codec_err(codec, "hda_codec: Too many imux items!\n"); 5597 return -EINVAL; 5598 } 5599 for (i = 0; i < imux->num_items; i++) { 5600 if (!strncmp(label, imux->items[i].label, strlen(label))) 5601 label_idx++; 5602 } 5603 if (type_idx) 5604 *type_idx = label_idx; 5605 if (label_idx > 0) 5606 snprintf(imux->items[imux->num_items].label, 5607 sizeof(imux->items[imux->num_items].label), 5608 "%s %d", label, label_idx); 5609 else 5610 strlcpy(imux->items[imux->num_items].label, label, 5611 sizeof(imux->items[imux->num_items].label)); 5612 imux->items[imux->num_items].index = index; 5613 imux->num_items++; 5614 return 0; 5615 } 5616 EXPORT_SYMBOL_GPL(snd_hda_add_imux_item); 5617 5618 5619 #ifdef CONFIG_PM 5620 /* 5621 * power management 5622 */ 5623 5624 5625 static void hda_async_suspend(void *data, async_cookie_t cookie) 5626 { 5627 hda_call_codec_suspend(data, false); 5628 } 5629 5630 static void hda_async_resume(void *data, async_cookie_t cookie) 5631 { 5632 hda_call_codec_resume(data); 5633 } 5634 5635 /** 5636 * snd_hda_suspend - suspend the codecs 5637 * @bus: the HDA bus 5638 * 5639 * Returns 0 if successful. 5640 */ 5641 int snd_hda_suspend(struct hda_bus *bus) 5642 { 5643 struct hda_codec *codec; 5644 ASYNC_DOMAIN_EXCLUSIVE(domain); 5645 5646 list_for_each_entry(codec, &bus->codec_list, list) { 5647 cancel_delayed_work_sync(&codec->jackpoll_work); 5648 if (hda_codec_is_power_on(codec)) { 5649 if (bus->num_codecs > 1) 5650 async_schedule_domain(hda_async_suspend, codec, 5651 &domain); 5652 else 5653 hda_call_codec_suspend(codec, false); 5654 } 5655 } 5656 5657 if (bus->num_codecs > 1) 5658 async_synchronize_full_domain(&domain); 5659 5660 return 0; 5661 } 5662 EXPORT_SYMBOL_GPL(snd_hda_suspend); 5663 5664 /** 5665 * snd_hda_resume - resume the codecs 5666 * @bus: the HDA bus 5667 * 5668 * Returns 0 if successful. 5669 */ 5670 int snd_hda_resume(struct hda_bus *bus) 5671 { 5672 struct hda_codec *codec; 5673 ASYNC_DOMAIN_EXCLUSIVE(domain); 5674 5675 list_for_each_entry(codec, &bus->codec_list, list) { 5676 if (bus->num_codecs > 1) 5677 async_schedule_domain(hda_async_resume, codec, &domain); 5678 else 5679 hda_call_codec_resume(codec); 5680 } 5681 5682 if (bus->num_codecs > 1) 5683 async_synchronize_full_domain(&domain); 5684 5685 return 0; 5686 } 5687 EXPORT_SYMBOL_GPL(snd_hda_resume); 5688 #endif /* CONFIG_PM */ 5689 5690 /* 5691 * generic arrays 5692 */ 5693 5694 /** 5695 * snd_array_new - get a new element from the given array 5696 * @array: the array object 5697 * 5698 * Get a new element from the given array. If it exceeds the 5699 * pre-allocated array size, re-allocate the array. 5700 * 5701 * Returns NULL if allocation failed. 5702 */ 5703 void *snd_array_new(struct snd_array *array) 5704 { 5705 if (snd_BUG_ON(!array->elem_size)) 5706 return NULL; 5707 if (array->used >= array->alloced) { 5708 int num = array->alloced + array->alloc_align; 5709 int size = (num + 1) * array->elem_size; 5710 void *nlist; 5711 if (snd_BUG_ON(num >= 4096)) 5712 return NULL; 5713 nlist = krealloc(array->list, size, GFP_KERNEL | __GFP_ZERO); 5714 if (!nlist) 5715 return NULL; 5716 array->list = nlist; 5717 array->alloced = num; 5718 } 5719 return snd_array_elem(array, array->used++); 5720 } 5721 EXPORT_SYMBOL_GPL(snd_array_new); 5722 5723 /** 5724 * snd_array_free - free the given array elements 5725 * @array: the array object 5726 */ 5727 void snd_array_free(struct snd_array *array) 5728 { 5729 kfree(array->list); 5730 array->used = 0; 5731 array->alloced = 0; 5732 array->list = NULL; 5733 } 5734 EXPORT_SYMBOL_GPL(snd_array_free); 5735 5736 /** 5737 * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer 5738 * @pcm: PCM caps bits 5739 * @buf: the string buffer to write 5740 * @buflen: the max buffer length 5741 * 5742 * used by hda_proc.c and hda_eld.c 5743 */ 5744 void snd_print_pcm_bits(int pcm, char *buf, int buflen) 5745 { 5746 static unsigned int bits[] = { 8, 16, 20, 24, 32 }; 5747 int i, j; 5748 5749 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++) 5750 if (pcm & (AC_SUPPCM_BITS_8 << i)) 5751 j += snprintf(buf + j, buflen - j, " %d", bits[i]); 5752 5753 buf[j] = '\0'; /* necessary when j == 0 */ 5754 } 5755 EXPORT_SYMBOL_GPL(snd_print_pcm_bits); 5756 5757 MODULE_DESCRIPTION("HDA codec core"); 5758 MODULE_LICENSE("GPL"); 5759