hda_auto_parser.c 21 KB

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