hda_auto_parser.c 21 KB

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