hda_auto_parser.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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:\n");
  307. for (i = 0; i < cfg->num_inputs; i++) {
  308. snd_printd(" %s=0x%x\n",
  309. hda_get_autocfg_input_label(codec, cfg, i),
  310. cfg->inputs[i].pin);
  311. }
  312. if (cfg->dig_in_pin)
  313. snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
  314. return 0;
  315. }
  316. EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
  317. int snd_hda_get_input_pin_attr(unsigned int def_conf)
  318. {
  319. unsigned int loc = get_defcfg_location(def_conf);
  320. unsigned int conn = get_defcfg_connect(def_conf);
  321. if (conn == AC_JACK_PORT_NONE)
  322. return INPUT_PIN_ATTR_UNUSED;
  323. /* Windows may claim the internal mic to be BOTH, too */
  324. if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
  325. return INPUT_PIN_ATTR_INT;
  326. if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
  327. return INPUT_PIN_ATTR_INT;
  328. if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
  329. return INPUT_PIN_ATTR_DOCK;
  330. if (loc == AC_JACK_LOC_REAR)
  331. return INPUT_PIN_ATTR_REAR;
  332. if (loc == AC_JACK_LOC_FRONT)
  333. return INPUT_PIN_ATTR_FRONT;
  334. return INPUT_PIN_ATTR_NORMAL;
  335. }
  336. EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
  337. /**
  338. * hda_get_input_pin_label - Give a label for the given input pin
  339. *
  340. * When check_location is true, the function checks the pin location
  341. * for mic and line-in pins, and set an appropriate prefix like "Front",
  342. * "Rear", "Internal".
  343. */
  344. static const char *hda_get_input_pin_label(struct hda_codec *codec,
  345. hda_nid_t pin, bool check_location)
  346. {
  347. unsigned int def_conf;
  348. static const char * const mic_names[] = {
  349. "Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
  350. };
  351. int attr;
  352. def_conf = snd_hda_codec_get_pincfg(codec, pin);
  353. switch (get_defcfg_device(def_conf)) {
  354. case AC_JACK_MIC_IN:
  355. if (!check_location)
  356. return "Mic";
  357. attr = snd_hda_get_input_pin_attr(def_conf);
  358. if (!attr)
  359. return "None";
  360. return mic_names[attr - 1];
  361. case AC_JACK_LINE_IN:
  362. if (!check_location)
  363. return "Line";
  364. attr = snd_hda_get_input_pin_attr(def_conf);
  365. if (!attr)
  366. return "None";
  367. if (attr == INPUT_PIN_ATTR_DOCK)
  368. return "Dock Line";
  369. return "Line";
  370. case AC_JACK_AUX:
  371. return "Aux";
  372. case AC_JACK_CD:
  373. return "CD";
  374. case AC_JACK_SPDIF_IN:
  375. return "SPDIF In";
  376. case AC_JACK_DIG_OTHER_IN:
  377. return "Digital In";
  378. default:
  379. return "Misc";
  380. }
  381. }
  382. /* Check whether the location prefix needs to be added to the label.
  383. * If all mic-jacks are in the same location (e.g. rear panel), we don't
  384. * have to put "Front" prefix to each label. In such a case, returns false.
  385. */
  386. static int check_mic_location_need(struct hda_codec *codec,
  387. const struct auto_pin_cfg *cfg,
  388. int input)
  389. {
  390. unsigned int defc;
  391. int i, attr, attr2;
  392. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
  393. attr = snd_hda_get_input_pin_attr(defc);
  394. /* for internal or docking mics, we need locations */
  395. if (attr <= INPUT_PIN_ATTR_NORMAL)
  396. return 1;
  397. attr = 0;
  398. for (i = 0; i < cfg->num_inputs; i++) {
  399. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
  400. attr2 = snd_hda_get_input_pin_attr(defc);
  401. if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
  402. if (attr && attr != attr2)
  403. return 1; /* different locations found */
  404. attr = attr2;
  405. }
  406. }
  407. return 0;
  408. }
  409. /**
  410. * hda_get_autocfg_input_label - Get a label for the given input
  411. *
  412. * Get a label for the given input pin defined by the autocfg item.
  413. * Unlike hda_get_input_pin_label(), this function checks all inputs
  414. * defined in autocfg and avoids the redundant mic/line prefix as much as
  415. * possible.
  416. */
  417. const char *hda_get_autocfg_input_label(struct hda_codec *codec,
  418. const struct auto_pin_cfg *cfg,
  419. int input)
  420. {
  421. int type = cfg->inputs[input].type;
  422. int has_multiple_pins = 0;
  423. if ((input > 0 && cfg->inputs[input - 1].type == type) ||
  424. (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
  425. has_multiple_pins = 1;
  426. if (has_multiple_pins && type == AUTO_PIN_MIC)
  427. has_multiple_pins &= check_mic_location_need(codec, cfg, input);
  428. return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
  429. has_multiple_pins);
  430. }
  431. EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
  432. /* return the position of NID in the list, or -1 if not found */
  433. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  434. {
  435. int i;
  436. for (i = 0; i < nums; i++)
  437. if (list[i] == nid)
  438. return i;
  439. return -1;
  440. }
  441. /* get a unique suffix or an index number */
  442. static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
  443. int num_pins, int *indexp)
  444. {
  445. static const char * const channel_sfx[] = {
  446. " Front", " Surround", " CLFE", " Side"
  447. };
  448. int i;
  449. i = find_idx_in_nid_list(nid, pins, num_pins);
  450. if (i < 0)
  451. return NULL;
  452. if (num_pins == 1)
  453. return "";
  454. if (num_pins > ARRAY_SIZE(channel_sfx)) {
  455. if (indexp)
  456. *indexp = i;
  457. return "";
  458. }
  459. return channel_sfx[i];
  460. }
  461. static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
  462. const struct auto_pin_cfg *cfg,
  463. const char *name, char *label, int maxlen,
  464. int *indexp)
  465. {
  466. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  467. int attr = snd_hda_get_input_pin_attr(def_conf);
  468. const char *pfx = "", *sfx = "";
  469. /* handle as a speaker if it's a fixed line-out */
  470. if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
  471. name = "Speaker";
  472. /* check the location */
  473. switch (attr) {
  474. case INPUT_PIN_ATTR_DOCK:
  475. pfx = "Dock ";
  476. break;
  477. case INPUT_PIN_ATTR_FRONT:
  478. pfx = "Front ";
  479. break;
  480. }
  481. if (cfg) {
  482. /* try to give a unique suffix if needed */
  483. sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
  484. indexp);
  485. if (!sfx)
  486. sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
  487. indexp);
  488. if (!sfx) {
  489. /* don't add channel suffix for Headphone controls */
  490. int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
  491. cfg->hp_outs);
  492. if (idx >= 0)
  493. *indexp = idx;
  494. sfx = "";
  495. }
  496. }
  497. snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
  498. return 1;
  499. }
  500. /**
  501. * snd_hda_get_pin_label - Get a label for the given I/O pin
  502. *
  503. * Get a label for the given pin. This function works for both input and
  504. * output pins. When @cfg is given as non-NULL, the function tries to get
  505. * an optimized label using hda_get_autocfg_input_label().
  506. *
  507. * This function tries to give a unique label string for the pin as much as
  508. * possible. For example, when the multiple line-outs are present, it adds
  509. * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
  510. * If no unique name with a suffix is available and @indexp is non-NULL, the
  511. * index number is stored in the pointer.
  512. */
  513. int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
  514. const struct auto_pin_cfg *cfg,
  515. char *label, int maxlen, int *indexp)
  516. {
  517. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  518. const char *name = NULL;
  519. int i;
  520. if (indexp)
  521. *indexp = 0;
  522. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  523. return 0;
  524. switch (get_defcfg_device(def_conf)) {
  525. case AC_JACK_LINE_OUT:
  526. return fill_audio_out_name(codec, nid, cfg, "Line Out",
  527. label, maxlen, indexp);
  528. case AC_JACK_SPEAKER:
  529. return fill_audio_out_name(codec, nid, cfg, "Speaker",
  530. label, maxlen, indexp);
  531. case AC_JACK_HP_OUT:
  532. return fill_audio_out_name(codec, nid, cfg, "Headphone",
  533. label, maxlen, indexp);
  534. case AC_JACK_SPDIF_OUT:
  535. case AC_JACK_DIG_OTHER_OUT:
  536. if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
  537. name = "HDMI";
  538. else
  539. name = "SPDIF";
  540. if (cfg && indexp) {
  541. i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
  542. cfg->dig_outs);
  543. if (i >= 0)
  544. *indexp = i;
  545. }
  546. break;
  547. default:
  548. if (cfg) {
  549. for (i = 0; i < cfg->num_inputs; i++) {
  550. if (cfg->inputs[i].pin != nid)
  551. continue;
  552. name = hda_get_autocfg_input_label(codec, cfg, i);
  553. if (name)
  554. break;
  555. }
  556. }
  557. if (!name)
  558. name = hda_get_input_pin_label(codec, nid, true);
  559. break;
  560. }
  561. if (!name)
  562. return 0;
  563. strlcpy(label, name, maxlen);
  564. return 1;
  565. }
  566. EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
  567. int snd_hda_gen_add_verbs(struct hda_gen_spec *spec,
  568. const struct hda_verb *list)
  569. {
  570. const struct hda_verb **v;
  571. v = snd_array_new(&spec->verbs);
  572. if (!v)
  573. return -ENOMEM;
  574. *v = list;
  575. return 0;
  576. }
  577. EXPORT_SYMBOL_HDA(snd_hda_gen_add_verbs);
  578. void snd_hda_gen_apply_verbs(struct hda_codec *codec)
  579. {
  580. struct hda_gen_spec *spec = codec->spec;
  581. int i;
  582. for (i = 0; i < spec->verbs.used; i++) {
  583. struct hda_verb **v = snd_array_elem(&spec->verbs, i);
  584. snd_hda_sequence_write(codec, *v);
  585. }
  586. }
  587. EXPORT_SYMBOL_HDA(snd_hda_gen_apply_verbs);
  588. void snd_hda_apply_pincfgs(struct hda_codec *codec,
  589. const struct hda_pintbl *cfg)
  590. {
  591. for (; cfg->nid; cfg++)
  592. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  593. }
  594. EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
  595. void snd_hda_apply_fixup(struct hda_codec *codec, int action)
  596. {
  597. struct hda_gen_spec *spec = codec->spec;
  598. int id = spec->fixup_id;
  599. #ifdef CONFIG_SND_DEBUG_VERBOSE
  600. const char *modelname = spec->fixup_name;
  601. #endif
  602. int depth = 0;
  603. if (!spec->fixup_list)
  604. return;
  605. while (id >= 0) {
  606. const struct hda_fixup *fix = spec->fixup_list + id;
  607. switch (fix->type) {
  608. case HDA_FIXUP_PINS:
  609. if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
  610. break;
  611. snd_printdd(KERN_INFO SFX
  612. "%s: Apply pincfg for %s\n",
  613. codec->chip_name, modelname);
  614. snd_hda_apply_pincfgs(codec, fix->v.pins);
  615. break;
  616. case HDA_FIXUP_VERBS:
  617. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
  618. break;
  619. snd_printdd(KERN_INFO SFX
  620. "%s: Apply fix-verbs for %s\n",
  621. codec->chip_name, modelname);
  622. snd_hda_gen_add_verbs(codec->spec, fix->v.verbs);
  623. break;
  624. case HDA_FIXUP_FUNC:
  625. if (!fix->v.func)
  626. break;
  627. snd_printdd(KERN_INFO SFX
  628. "%s: Apply fix-func for %s\n",
  629. codec->chip_name, modelname);
  630. fix->v.func(codec, fix, action);
  631. break;
  632. default:
  633. snd_printk(KERN_ERR SFX
  634. "%s: Invalid fixup type %d\n",
  635. codec->chip_name, fix->type);
  636. break;
  637. }
  638. if (!fix->chained)
  639. break;
  640. if (++depth > 10)
  641. break;
  642. id = fix->chain_id;
  643. }
  644. }
  645. EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
  646. void snd_hda_pick_fixup(struct hda_codec *codec,
  647. const struct hda_model_fixup *models,
  648. const struct snd_pci_quirk *quirk,
  649. const struct hda_fixup *fixlist)
  650. {
  651. struct hda_gen_spec *spec = codec->spec;
  652. const struct snd_pci_quirk *q;
  653. int id = -1;
  654. const char *name = NULL;
  655. /* when model=nofixup is given, don't pick up any fixups */
  656. if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
  657. spec->fixup_list = NULL;
  658. spec->fixup_id = -1;
  659. return;
  660. }
  661. if (codec->modelname && models) {
  662. while (models->name) {
  663. if (!strcmp(codec->modelname, models->name)) {
  664. id = models->id;
  665. name = models->name;
  666. break;
  667. }
  668. models++;
  669. }
  670. }
  671. if (id < 0 && quirk) {
  672. q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
  673. if (q) {
  674. id = q->value;
  675. #ifdef CONFIG_SND_DEBUG_VERBOSE
  676. name = q->name;
  677. #endif
  678. }
  679. }
  680. if (id < 0 && quirk) {
  681. for (q = quirk; q->subvendor; q++) {
  682. unsigned int vendorid =
  683. q->subdevice | (q->subvendor << 16);
  684. if (vendorid == codec->subsystem_id) {
  685. id = q->value;
  686. #ifdef CONFIG_SND_DEBUG_VERBOSE
  687. name = q->name;
  688. #endif
  689. break;
  690. }
  691. }
  692. }
  693. spec->fixup_id = id;
  694. if (id >= 0) {
  695. spec->fixup_list = fixlist;
  696. spec->fixup_name = name;
  697. }
  698. }
  699. EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);