hda_auto_parser.c 22 KB

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