Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * BIOS auto-parser helper functions for HD-audio |
| 3 | * |
| 4 | * Copyright (c) 2012 Takashi Iwai <tiwai@suse.de> |
| 5 | * |
| 6 | * This driver is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/export.h> |
| 14 | #include <sound/core.h> |
| 15 | #include "hda_codec.h" |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 16 | #include "hda_local.h" |
Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 17 | #include "hda_auto_parser.h" |
| 18 | |
| 19 | #define SFX "hda_codec: " |
| 20 | |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 21 | /* |
| 22 | * Helper for automatic pin configuration |
| 23 | */ |
| 24 | |
| 25 | static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list) |
| 26 | { |
| 27 | for (; *list; list++) |
| 28 | if (*list == nid) |
| 29 | return 1; |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /* |
| 35 | * Sort an associated group of pins according to their sequence numbers. |
| 36 | */ |
| 37 | static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences, |
| 38 | int num_pins) |
| 39 | { |
| 40 | int i, j; |
| 41 | short seq; |
| 42 | hda_nid_t nid; |
| 43 | |
| 44 | for (i = 0; i < num_pins; i++) { |
| 45 | for (j = i + 1; j < num_pins; j++) { |
| 46 | if (sequences[i] > sequences[j]) { |
| 47 | seq = sequences[i]; |
| 48 | sequences[i] = sequences[j]; |
| 49 | sequences[j] = seq; |
| 50 | nid = pins[i]; |
| 51 | pins[i] = pins[j]; |
| 52 | pins[j] = nid; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | /* add the found input-pin to the cfg->inputs[] table */ |
| 60 | static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid, |
| 61 | int type) |
| 62 | { |
| 63 | if (cfg->num_inputs < AUTO_CFG_MAX_INS) { |
| 64 | cfg->inputs[cfg->num_inputs].pin = nid; |
| 65 | cfg->inputs[cfg->num_inputs].type = type; |
| 66 | cfg->num_inputs++; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /* sort inputs in the order of AUTO_PIN_* type */ |
| 71 | static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg) |
| 72 | { |
| 73 | int i, j; |
| 74 | |
| 75 | for (i = 0; i < cfg->num_inputs; i++) { |
| 76 | for (j = i + 1; j < cfg->num_inputs; j++) { |
| 77 | if (cfg->inputs[i].type > cfg->inputs[j].type) { |
| 78 | struct auto_pin_cfg_item tmp; |
| 79 | tmp = cfg->inputs[i]; |
| 80 | cfg->inputs[i] = cfg->inputs[j]; |
| 81 | cfg->inputs[j] = tmp; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* Reorder the surround channels |
| 88 | * ALSA sequence is front/surr/clfe/side |
| 89 | * HDA sequence is: |
| 90 | * 4-ch: front/surr => OK as it is |
| 91 | * 6-ch: front/clfe/surr |
| 92 | * 8-ch: front/clfe/rear/side|fc |
| 93 | */ |
| 94 | static void reorder_outputs(unsigned int nums, hda_nid_t *pins) |
| 95 | { |
| 96 | hda_nid_t nid; |
| 97 | |
| 98 | switch (nums) { |
| 99 | case 3: |
| 100 | case 4: |
| 101 | nid = pins[1]; |
| 102 | pins[1] = pins[2]; |
| 103 | pins[2] = nid; |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Parse all pin widgets and store the useful pin nids to cfg |
| 110 | * |
| 111 | * The number of line-outs or any primary output is stored in line_outs, |
| 112 | * and the corresponding output pins are assigned to line_out_pins[], |
| 113 | * in the order of front, rear, CLFE, side, ... |
| 114 | * |
| 115 | * If more extra outputs (speaker and headphone) are found, the pins are |
| 116 | * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack |
| 117 | * is detected, one of speaker of HP pins is assigned as the primary |
| 118 | * output, i.e. to line_out_pins[0]. So, line_outs is always positive |
| 119 | * if any analog output exists. |
| 120 | * |
| 121 | * The analog input pins are assigned to inputs array. |
| 122 | * The digital input/output pins are assigned to dig_in_pin and dig_out_pin, |
| 123 | * respectively. |
| 124 | */ |
| 125 | int snd_hda_parse_pin_defcfg(struct hda_codec *codec, |
| 126 | struct auto_pin_cfg *cfg, |
| 127 | const hda_nid_t *ignore_nids, |
| 128 | unsigned int cond_flags) |
| 129 | { |
| 130 | hda_nid_t nid, end_nid; |
| 131 | short seq, assoc_line_out; |
| 132 | short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)]; |
| 133 | short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)]; |
| 134 | short sequences_hp[ARRAY_SIZE(cfg->hp_pins)]; |
| 135 | int i; |
| 136 | |
| 137 | memset(cfg, 0, sizeof(*cfg)); |
| 138 | |
| 139 | memset(sequences_line_out, 0, sizeof(sequences_line_out)); |
| 140 | memset(sequences_speaker, 0, sizeof(sequences_speaker)); |
| 141 | memset(sequences_hp, 0, sizeof(sequences_hp)); |
| 142 | assoc_line_out = 0; |
| 143 | |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 144 | end_nid = codec->start_nid + codec->num_nodes; |
| 145 | for (nid = codec->start_nid; nid < end_nid; nid++) { |
| 146 | unsigned int wid_caps = get_wcaps(codec, nid); |
| 147 | unsigned int wid_type = get_wcaps_type(wid_caps); |
| 148 | unsigned int def_conf; |
| 149 | short assoc, loc, conn, dev; |
| 150 | |
| 151 | /* read all default configuration for pin complex */ |
| 152 | if (wid_type != AC_WID_PIN) |
| 153 | continue; |
| 154 | /* ignore the given nids (e.g. pc-beep returns error) */ |
| 155 | if (ignore_nids && is_in_nid_list(nid, ignore_nids)) |
| 156 | continue; |
| 157 | |
| 158 | def_conf = snd_hda_codec_get_pincfg(codec, nid); |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 159 | conn = get_defcfg_connect(def_conf); |
| 160 | if (conn == AC_JACK_PORT_NONE) |
| 161 | continue; |
| 162 | loc = get_defcfg_location(def_conf); |
| 163 | dev = get_defcfg_device(def_conf); |
| 164 | |
| 165 | /* workaround for buggy BIOS setups */ |
| 166 | if (dev == AC_JACK_LINE_OUT) { |
| 167 | if (conn == AC_JACK_PORT_FIXED) |
| 168 | dev = AC_JACK_SPEAKER; |
| 169 | } |
| 170 | |
| 171 | switch (dev) { |
| 172 | case AC_JACK_LINE_OUT: |
| 173 | seq = get_defcfg_sequence(def_conf); |
| 174 | assoc = get_defcfg_association(def_conf); |
| 175 | |
| 176 | if (!(wid_caps & AC_WCAP_STEREO)) |
| 177 | if (!cfg->mono_out_pin) |
| 178 | cfg->mono_out_pin = nid; |
| 179 | if (!assoc) |
| 180 | continue; |
| 181 | if (!assoc_line_out) |
| 182 | assoc_line_out = assoc; |
| 183 | else if (assoc_line_out != assoc) |
| 184 | continue; |
| 185 | if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) |
| 186 | continue; |
| 187 | cfg->line_out_pins[cfg->line_outs] = nid; |
| 188 | sequences_line_out[cfg->line_outs] = seq; |
| 189 | cfg->line_outs++; |
| 190 | break; |
| 191 | case AC_JACK_SPEAKER: |
| 192 | seq = get_defcfg_sequence(def_conf); |
| 193 | assoc = get_defcfg_association(def_conf); |
| 194 | if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) |
| 195 | continue; |
| 196 | cfg->speaker_pins[cfg->speaker_outs] = nid; |
| 197 | sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq; |
| 198 | cfg->speaker_outs++; |
| 199 | break; |
| 200 | case AC_JACK_HP_OUT: |
| 201 | seq = get_defcfg_sequence(def_conf); |
| 202 | assoc = get_defcfg_association(def_conf); |
| 203 | if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) |
| 204 | continue; |
| 205 | cfg->hp_pins[cfg->hp_outs] = nid; |
| 206 | sequences_hp[cfg->hp_outs] = (assoc << 4) | seq; |
| 207 | cfg->hp_outs++; |
| 208 | break; |
| 209 | case AC_JACK_MIC_IN: |
| 210 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC); |
| 211 | break; |
| 212 | case AC_JACK_LINE_IN: |
| 213 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN); |
| 214 | break; |
| 215 | case AC_JACK_CD: |
| 216 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD); |
| 217 | break; |
| 218 | case AC_JACK_AUX: |
| 219 | add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX); |
| 220 | break; |
| 221 | case AC_JACK_SPDIF_OUT: |
| 222 | case AC_JACK_DIG_OTHER_OUT: |
| 223 | if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) |
| 224 | continue; |
| 225 | cfg->dig_out_pins[cfg->dig_outs] = nid; |
| 226 | cfg->dig_out_type[cfg->dig_outs] = |
| 227 | (loc == AC_JACK_LOC_HDMI) ? |
| 228 | HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF; |
| 229 | cfg->dig_outs++; |
| 230 | break; |
| 231 | case AC_JACK_SPDIF_IN: |
| 232 | case AC_JACK_DIG_OTHER_IN: |
| 233 | cfg->dig_in_pin = nid; |
| 234 | if (loc == AC_JACK_LOC_HDMI) |
| 235 | cfg->dig_in_type = HDA_PCM_TYPE_HDMI; |
| 236 | else |
| 237 | cfg->dig_in_type = HDA_PCM_TYPE_SPDIF; |
| 238 | break; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* FIX-UP: |
| 243 | * If no line-out is defined but multiple HPs are found, |
| 244 | * some of them might be the real line-outs. |
| 245 | */ |
| 246 | if (!cfg->line_outs && cfg->hp_outs > 1 && |
| 247 | !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) { |
| 248 | int i = 0; |
| 249 | while (i < cfg->hp_outs) { |
| 250 | /* The real HPs should have the sequence 0x0f */ |
| 251 | if ((sequences_hp[i] & 0x0f) == 0x0f) { |
| 252 | i++; |
| 253 | continue; |
| 254 | } |
| 255 | /* Move it to the line-out table */ |
| 256 | cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i]; |
| 257 | sequences_line_out[cfg->line_outs] = sequences_hp[i]; |
| 258 | cfg->line_outs++; |
| 259 | cfg->hp_outs--; |
| 260 | memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1, |
| 261 | sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i)); |
| 262 | memmove(sequences_hp + i, sequences_hp + i + 1, |
| 263 | sizeof(sequences_hp[0]) * (cfg->hp_outs - i)); |
| 264 | } |
| 265 | memset(cfg->hp_pins + cfg->hp_outs, 0, |
| 266 | sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs)); |
| 267 | if (!cfg->hp_outs) |
| 268 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 269 | |
| 270 | } |
| 271 | |
| 272 | /* sort by sequence */ |
| 273 | sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out, |
| 274 | cfg->line_outs); |
| 275 | sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker, |
| 276 | cfg->speaker_outs); |
| 277 | sort_pins_by_sequence(cfg->hp_pins, sequences_hp, |
| 278 | cfg->hp_outs); |
| 279 | |
| 280 | /* |
| 281 | * FIX-UP: if no line-outs are detected, try to use speaker or HP pin |
| 282 | * as a primary output |
| 283 | */ |
| 284 | if (!cfg->line_outs && |
| 285 | !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) { |
| 286 | if (cfg->speaker_outs) { |
| 287 | cfg->line_outs = cfg->speaker_outs; |
| 288 | memcpy(cfg->line_out_pins, cfg->speaker_pins, |
| 289 | sizeof(cfg->speaker_pins)); |
| 290 | cfg->speaker_outs = 0; |
| 291 | memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins)); |
| 292 | cfg->line_out_type = AUTO_PIN_SPEAKER_OUT; |
| 293 | } else if (cfg->hp_outs) { |
| 294 | cfg->line_outs = cfg->hp_outs; |
| 295 | memcpy(cfg->line_out_pins, cfg->hp_pins, |
| 296 | sizeof(cfg->hp_pins)); |
| 297 | cfg->hp_outs = 0; |
| 298 | memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins)); |
| 299 | cfg->line_out_type = AUTO_PIN_HP_OUT; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | reorder_outputs(cfg->line_outs, cfg->line_out_pins); |
| 304 | reorder_outputs(cfg->hp_outs, cfg->hp_pins); |
| 305 | reorder_outputs(cfg->speaker_outs, cfg->speaker_pins); |
| 306 | |
| 307 | sort_autocfg_input_pins(cfg); |
| 308 | |
| 309 | /* |
| 310 | * debug prints of the parsed results |
| 311 | */ |
| 312 | snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n", |
| 313 | cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1], |
| 314 | cfg->line_out_pins[2], cfg->line_out_pins[3], |
| 315 | cfg->line_out_pins[4], |
| 316 | cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" : |
| 317 | (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ? |
| 318 | "speaker" : "line")); |
| 319 | snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 320 | cfg->speaker_outs, cfg->speaker_pins[0], |
| 321 | cfg->speaker_pins[1], cfg->speaker_pins[2], |
| 322 | cfg->speaker_pins[3], cfg->speaker_pins[4]); |
| 323 | snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n", |
| 324 | cfg->hp_outs, cfg->hp_pins[0], |
| 325 | cfg->hp_pins[1], cfg->hp_pins[2], |
| 326 | cfg->hp_pins[3], cfg->hp_pins[4]); |
| 327 | snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin); |
| 328 | if (cfg->dig_outs) |
| 329 | snd_printd(" dig-out=0x%x/0x%x\n", |
| 330 | cfg->dig_out_pins[0], cfg->dig_out_pins[1]); |
Takashi Iwai | 709aea6 | 2012-08-07 18:09:23 +0200 | [diff] [blame] | 331 | snd_printd(" inputs:\n"); |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 332 | for (i = 0; i < cfg->num_inputs; i++) { |
Takashi Iwai | 709aea6 | 2012-08-07 18:09:23 +0200 | [diff] [blame] | 333 | snd_printd(" %s=0x%x\n", |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 334 | hda_get_autocfg_input_label(codec, cfg, i), |
| 335 | cfg->inputs[i].pin); |
| 336 | } |
Takashi Iwai | 128bc4b | 2012-05-07 17:42:31 +0200 | [diff] [blame] | 337 | if (cfg->dig_in_pin) |
| 338 | snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg); |
| 343 | |
| 344 | int snd_hda_get_input_pin_attr(unsigned int def_conf) |
| 345 | { |
| 346 | unsigned int loc = get_defcfg_location(def_conf); |
| 347 | unsigned int conn = get_defcfg_connect(def_conf); |
| 348 | if (conn == AC_JACK_PORT_NONE) |
| 349 | return INPUT_PIN_ATTR_UNUSED; |
| 350 | /* Windows may claim the internal mic to be BOTH, too */ |
| 351 | if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH) |
| 352 | return INPUT_PIN_ATTR_INT; |
| 353 | if ((loc & 0x30) == AC_JACK_LOC_INTERNAL) |
| 354 | return INPUT_PIN_ATTR_INT; |
| 355 | if ((loc & 0x30) == AC_JACK_LOC_SEPARATE) |
| 356 | return INPUT_PIN_ATTR_DOCK; |
| 357 | if (loc == AC_JACK_LOC_REAR) |
| 358 | return INPUT_PIN_ATTR_REAR; |
| 359 | if (loc == AC_JACK_LOC_FRONT) |
| 360 | return INPUT_PIN_ATTR_FRONT; |
| 361 | return INPUT_PIN_ATTR_NORMAL; |
| 362 | } |
| 363 | EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr); |
| 364 | |
| 365 | /** |
| 366 | * hda_get_input_pin_label - Give a label for the given input pin |
| 367 | * |
| 368 | * When check_location is true, the function checks the pin location |
| 369 | * for mic and line-in pins, and set an appropriate prefix like "Front", |
| 370 | * "Rear", "Internal". |
| 371 | */ |
| 372 | |
| 373 | static const char *hda_get_input_pin_label(struct hda_codec *codec, |
| 374 | hda_nid_t pin, bool check_location) |
| 375 | { |
| 376 | unsigned int def_conf; |
| 377 | static const char * const mic_names[] = { |
| 378 | "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic", |
| 379 | }; |
| 380 | int attr; |
| 381 | |
| 382 | def_conf = snd_hda_codec_get_pincfg(codec, pin); |
| 383 | |
| 384 | switch (get_defcfg_device(def_conf)) { |
| 385 | case AC_JACK_MIC_IN: |
| 386 | if (!check_location) |
| 387 | return "Mic"; |
| 388 | attr = snd_hda_get_input_pin_attr(def_conf); |
| 389 | if (!attr) |
| 390 | return "None"; |
| 391 | return mic_names[attr - 1]; |
| 392 | case AC_JACK_LINE_IN: |
| 393 | if (!check_location) |
| 394 | return "Line"; |
| 395 | attr = snd_hda_get_input_pin_attr(def_conf); |
| 396 | if (!attr) |
| 397 | return "None"; |
| 398 | if (attr == INPUT_PIN_ATTR_DOCK) |
| 399 | return "Dock Line"; |
| 400 | return "Line"; |
| 401 | case AC_JACK_AUX: |
| 402 | return "Aux"; |
| 403 | case AC_JACK_CD: |
| 404 | return "CD"; |
| 405 | case AC_JACK_SPDIF_IN: |
| 406 | return "SPDIF In"; |
| 407 | case AC_JACK_DIG_OTHER_IN: |
| 408 | return "Digital In"; |
| 409 | default: |
| 410 | return "Misc"; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* Check whether the location prefix needs to be added to the label. |
| 415 | * If all mic-jacks are in the same location (e.g. rear panel), we don't |
| 416 | * have to put "Front" prefix to each label. In such a case, returns false. |
| 417 | */ |
| 418 | static int check_mic_location_need(struct hda_codec *codec, |
| 419 | const struct auto_pin_cfg *cfg, |
| 420 | int input) |
| 421 | { |
| 422 | unsigned int defc; |
| 423 | int i, attr, attr2; |
| 424 | |
| 425 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin); |
| 426 | attr = snd_hda_get_input_pin_attr(defc); |
| 427 | /* for internal or docking mics, we need locations */ |
| 428 | if (attr <= INPUT_PIN_ATTR_NORMAL) |
| 429 | return 1; |
| 430 | |
| 431 | attr = 0; |
| 432 | for (i = 0; i < cfg->num_inputs; i++) { |
| 433 | defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin); |
| 434 | attr2 = snd_hda_get_input_pin_attr(defc); |
| 435 | if (attr2 >= INPUT_PIN_ATTR_NORMAL) { |
| 436 | if (attr && attr != attr2) |
| 437 | return 1; /* different locations found */ |
| 438 | attr = attr2; |
| 439 | } |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * hda_get_autocfg_input_label - Get a label for the given input |
| 446 | * |
| 447 | * Get a label for the given input pin defined by the autocfg item. |
| 448 | * Unlike hda_get_input_pin_label(), this function checks all inputs |
| 449 | * defined in autocfg and avoids the redundant mic/line prefix as much as |
| 450 | * possible. |
| 451 | */ |
| 452 | const char *hda_get_autocfg_input_label(struct hda_codec *codec, |
| 453 | const struct auto_pin_cfg *cfg, |
| 454 | int input) |
| 455 | { |
| 456 | int type = cfg->inputs[input].type; |
| 457 | int has_multiple_pins = 0; |
| 458 | |
| 459 | if ((input > 0 && cfg->inputs[input - 1].type == type) || |
| 460 | (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type)) |
| 461 | has_multiple_pins = 1; |
| 462 | if (has_multiple_pins && type == AUTO_PIN_MIC) |
| 463 | has_multiple_pins &= check_mic_location_need(codec, cfg, input); |
| 464 | return hda_get_input_pin_label(codec, cfg->inputs[input].pin, |
| 465 | has_multiple_pins); |
| 466 | } |
| 467 | EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label); |
| 468 | |
| 469 | /* return the position of NID in the list, or -1 if not found */ |
| 470 | static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums) |
| 471 | { |
| 472 | int i; |
| 473 | for (i = 0; i < nums; i++) |
| 474 | if (list[i] == nid) |
| 475 | return i; |
| 476 | return -1; |
| 477 | } |
| 478 | |
| 479 | /* get a unique suffix or an index number */ |
| 480 | static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins, |
| 481 | int num_pins, int *indexp) |
| 482 | { |
| 483 | static const char * const channel_sfx[] = { |
| 484 | " Front", " Surround", " CLFE", " Side" |
| 485 | }; |
| 486 | int i; |
| 487 | |
| 488 | i = find_idx_in_nid_list(nid, pins, num_pins); |
| 489 | if (i < 0) |
| 490 | return NULL; |
| 491 | if (num_pins == 1) |
| 492 | return ""; |
| 493 | if (num_pins > ARRAY_SIZE(channel_sfx)) { |
| 494 | if (indexp) |
| 495 | *indexp = i; |
| 496 | return ""; |
| 497 | } |
| 498 | return channel_sfx[i]; |
| 499 | } |
| 500 | |
| 501 | static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid, |
| 502 | const struct auto_pin_cfg *cfg, |
| 503 | const char *name, char *label, int maxlen, |
| 504 | int *indexp) |
| 505 | { |
| 506 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 507 | int attr = snd_hda_get_input_pin_attr(def_conf); |
| 508 | const char *pfx = "", *sfx = ""; |
| 509 | |
| 510 | /* handle as a speaker if it's a fixed line-out */ |
| 511 | if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT) |
| 512 | name = "Speaker"; |
| 513 | /* check the location */ |
| 514 | switch (attr) { |
| 515 | case INPUT_PIN_ATTR_DOCK: |
| 516 | pfx = "Dock "; |
| 517 | break; |
| 518 | case INPUT_PIN_ATTR_FRONT: |
| 519 | pfx = "Front "; |
| 520 | break; |
| 521 | } |
| 522 | if (cfg) { |
| 523 | /* try to give a unique suffix if needed */ |
| 524 | sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs, |
| 525 | indexp); |
| 526 | if (!sfx) |
| 527 | sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs, |
| 528 | indexp); |
| 529 | if (!sfx) { |
| 530 | /* don't add channel suffix for Headphone controls */ |
| 531 | int idx = find_idx_in_nid_list(nid, cfg->hp_pins, |
| 532 | cfg->hp_outs); |
| 533 | if (idx >= 0) |
| 534 | *indexp = idx; |
| 535 | sfx = ""; |
| 536 | } |
| 537 | } |
| 538 | snprintf(label, maxlen, "%s%s%s", pfx, name, sfx); |
| 539 | return 1; |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * snd_hda_get_pin_label - Get a label for the given I/O pin |
| 544 | * |
| 545 | * Get a label for the given pin. This function works for both input and |
| 546 | * output pins. When @cfg is given as non-NULL, the function tries to get |
| 547 | * an optimized label using hda_get_autocfg_input_label(). |
| 548 | * |
| 549 | * This function tries to give a unique label string for the pin as much as |
| 550 | * possible. For example, when the multiple line-outs are present, it adds |
| 551 | * the channel suffix like "Front", "Surround", etc (only when @cfg is given). |
| 552 | * If no unique name with a suffix is available and @indexp is non-NULL, the |
| 553 | * index number is stored in the pointer. |
| 554 | */ |
| 555 | int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid, |
| 556 | const struct auto_pin_cfg *cfg, |
| 557 | char *label, int maxlen, int *indexp) |
| 558 | { |
| 559 | unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid); |
| 560 | const char *name = NULL; |
| 561 | int i; |
| 562 | |
| 563 | if (indexp) |
| 564 | *indexp = 0; |
| 565 | if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE) |
| 566 | return 0; |
| 567 | |
| 568 | switch (get_defcfg_device(def_conf)) { |
| 569 | case AC_JACK_LINE_OUT: |
| 570 | return fill_audio_out_name(codec, nid, cfg, "Line Out", |
| 571 | label, maxlen, indexp); |
| 572 | case AC_JACK_SPEAKER: |
| 573 | return fill_audio_out_name(codec, nid, cfg, "Speaker", |
| 574 | label, maxlen, indexp); |
| 575 | case AC_JACK_HP_OUT: |
| 576 | return fill_audio_out_name(codec, nid, cfg, "Headphone", |
| 577 | label, maxlen, indexp); |
| 578 | case AC_JACK_SPDIF_OUT: |
| 579 | case AC_JACK_DIG_OTHER_OUT: |
| 580 | if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI) |
| 581 | name = "HDMI"; |
| 582 | else |
| 583 | name = "SPDIF"; |
| 584 | if (cfg && indexp) { |
| 585 | i = find_idx_in_nid_list(nid, cfg->dig_out_pins, |
| 586 | cfg->dig_outs); |
| 587 | if (i >= 0) |
| 588 | *indexp = i; |
| 589 | } |
| 590 | break; |
| 591 | default: |
| 592 | if (cfg) { |
| 593 | for (i = 0; i < cfg->num_inputs; i++) { |
| 594 | if (cfg->inputs[i].pin != nid) |
| 595 | continue; |
| 596 | name = hda_get_autocfg_input_label(codec, cfg, i); |
| 597 | if (name) |
| 598 | break; |
| 599 | } |
| 600 | } |
| 601 | if (!name) |
| 602 | name = hda_get_input_pin_label(codec, nid, true); |
| 603 | break; |
| 604 | } |
| 605 | if (!name) |
| 606 | return 0; |
| 607 | strlcpy(label, name, maxlen); |
| 608 | return 1; |
| 609 | } |
| 610 | EXPORT_SYMBOL_HDA(snd_hda_get_pin_label); |
| 611 | |
Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 612 | int snd_hda_gen_add_verbs(struct hda_gen_spec *spec, |
| 613 | const struct hda_verb *list) |
| 614 | { |
| 615 | const struct hda_verb **v; |
Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 616 | v = snd_array_new(&spec->verbs); |
| 617 | if (!v) |
| 618 | return -ENOMEM; |
| 619 | *v = list; |
| 620 | return 0; |
| 621 | } |
| 622 | EXPORT_SYMBOL_HDA(snd_hda_gen_add_verbs); |
| 623 | |
| 624 | void snd_hda_gen_apply_verbs(struct hda_codec *codec) |
| 625 | { |
| 626 | struct hda_gen_spec *spec = codec->spec; |
| 627 | int i; |
| 628 | for (i = 0; i < spec->verbs.used; i++) { |
| 629 | struct hda_verb **v = snd_array_elem(&spec->verbs, i); |
| 630 | snd_hda_sequence_write(codec, *v); |
| 631 | } |
| 632 | } |
| 633 | EXPORT_SYMBOL_HDA(snd_hda_gen_apply_verbs); |
| 634 | |
| 635 | void snd_hda_apply_pincfgs(struct hda_codec *codec, |
| 636 | const struct hda_pintbl *cfg) |
| 637 | { |
| 638 | for (; cfg->nid; cfg++) |
| 639 | snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val); |
| 640 | } |
| 641 | EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs); |
| 642 | |
| 643 | void snd_hda_apply_fixup(struct hda_codec *codec, int action) |
| 644 | { |
| 645 | struct hda_gen_spec *spec = codec->spec; |
| 646 | int id = spec->fixup_id; |
| 647 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
| 648 | const char *modelname = spec->fixup_name; |
| 649 | #endif |
| 650 | int depth = 0; |
| 651 | |
| 652 | if (!spec->fixup_list) |
| 653 | return; |
| 654 | |
| 655 | while (id >= 0) { |
| 656 | const struct hda_fixup *fix = spec->fixup_list + id; |
| 657 | |
| 658 | switch (fix->type) { |
| 659 | case HDA_FIXUP_PINS: |
| 660 | if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins) |
| 661 | break; |
| 662 | snd_printdd(KERN_INFO SFX |
| 663 | "%s: Apply pincfg for %s\n", |
| 664 | codec->chip_name, modelname); |
| 665 | snd_hda_apply_pincfgs(codec, fix->v.pins); |
| 666 | break; |
| 667 | case HDA_FIXUP_VERBS: |
| 668 | if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs) |
| 669 | break; |
| 670 | snd_printdd(KERN_INFO SFX |
| 671 | "%s: Apply fix-verbs for %s\n", |
| 672 | codec->chip_name, modelname); |
| 673 | snd_hda_gen_add_verbs(codec->spec, fix->v.verbs); |
| 674 | break; |
| 675 | case HDA_FIXUP_FUNC: |
| 676 | if (!fix->v.func) |
| 677 | break; |
| 678 | snd_printdd(KERN_INFO SFX |
| 679 | "%s: Apply fix-func for %s\n", |
| 680 | codec->chip_name, modelname); |
| 681 | fix->v.func(codec, fix, action); |
| 682 | break; |
| 683 | default: |
| 684 | snd_printk(KERN_ERR SFX |
| 685 | "%s: Invalid fixup type %d\n", |
| 686 | codec->chip_name, fix->type); |
| 687 | break; |
| 688 | } |
| 689 | if (!fix->chained) |
| 690 | break; |
| 691 | if (++depth > 10) |
| 692 | break; |
| 693 | id = fix->chain_id; |
| 694 | } |
| 695 | } |
| 696 | EXPORT_SYMBOL_HDA(snd_hda_apply_fixup); |
| 697 | |
| 698 | void snd_hda_pick_fixup(struct hda_codec *codec, |
| 699 | const struct hda_model_fixup *models, |
| 700 | const struct snd_pci_quirk *quirk, |
| 701 | const struct hda_fixup *fixlist) |
| 702 | { |
| 703 | struct hda_gen_spec *spec = codec->spec; |
| 704 | const struct snd_pci_quirk *q; |
| 705 | int id = -1; |
| 706 | const char *name = NULL; |
| 707 | |
| 708 | /* when model=nofixup is given, don't pick up any fixups */ |
| 709 | if (codec->modelname && !strcmp(codec->modelname, "nofixup")) { |
| 710 | spec->fixup_list = NULL; |
| 711 | spec->fixup_id = -1; |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | if (codec->modelname && models) { |
| 716 | while (models->name) { |
| 717 | if (!strcmp(codec->modelname, models->name)) { |
| 718 | id = models->id; |
| 719 | name = models->name; |
| 720 | break; |
| 721 | } |
| 722 | models++; |
| 723 | } |
| 724 | } |
David Henningsson | 639aa4b | 2012-07-18 18:02:53 +0200 | [diff] [blame] | 725 | if (id < 0 && quirk) { |
Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 726 | q = snd_pci_quirk_lookup(codec->bus->pci, quirk); |
| 727 | if (q) { |
| 728 | id = q->value; |
| 729 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
| 730 | name = q->name; |
| 731 | #endif |
| 732 | } |
| 733 | } |
David Henningsson | 639aa4b | 2012-07-18 18:02:53 +0200 | [diff] [blame] | 734 | if (id < 0 && quirk) { |
Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 735 | for (q = quirk; q->subvendor; q++) { |
| 736 | unsigned int vendorid = |
| 737 | q->subdevice | (q->subvendor << 16); |
Takashi Iwai | a33b7b0 | 2012-09-11 16:42:18 +0200 | [diff] [blame^] | 738 | unsigned int mask = 0xffff0000 | q->subdevice_mask; |
| 739 | if ((codec->subsystem_id & mask) == (vendorid & mask)) { |
Takashi Iwai | 23d30f2 | 2012-05-07 17:17:32 +0200 | [diff] [blame] | 740 | id = q->value; |
| 741 | #ifdef CONFIG_SND_DEBUG_VERBOSE |
| 742 | name = q->name; |
| 743 | #endif |
| 744 | break; |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | spec->fixup_id = id; |
| 750 | if (id >= 0) { |
| 751 | spec->fixup_list = fixlist; |
| 752 | spec->fixup_name = name; |
| 753 | } |
| 754 | } |
| 755 | EXPORT_SYMBOL_HDA(snd_hda_pick_fixup); |