hda_auto_parser.c 21 KB

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