hda_auto_parser.c 21 KB

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