hda_auto_parser.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. /*
  19. * Helper for automatic pin configuration
  20. */
  21. static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
  22. {
  23. for (; *list; list++)
  24. if (*list == nid)
  25. return 1;
  26. return 0;
  27. }
  28. /* a pair of input pin and its sequence */
  29. struct auto_out_pin {
  30. hda_nid_t pin;
  31. short seq;
  32. };
  33. static int compare_seq(const void *ap, const void *bp)
  34. {
  35. const struct auto_out_pin *a = ap;
  36. const struct auto_out_pin *b = bp;
  37. return (int)(a->seq - b->seq);
  38. }
  39. /*
  40. * Sort an associated group of pins according to their sequence numbers.
  41. * then store it to a pin array.
  42. */
  43. static void sort_pins_by_sequence(hda_nid_t *pins, struct auto_out_pin *list,
  44. int num_pins)
  45. {
  46. int i;
  47. sort(list, num_pins, sizeof(list[0]), compare_seq, NULL);
  48. for (i = 0; i < num_pins; i++)
  49. pins[i] = list[i].pin;
  50. }
  51. /* add the found input-pin to the cfg->inputs[] table */
  52. static void add_auto_cfg_input_pin(struct hda_codec *codec, struct auto_pin_cfg *cfg,
  53. hda_nid_t nid, int type)
  54. {
  55. if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
  56. cfg->inputs[cfg->num_inputs].pin = nid;
  57. cfg->inputs[cfg->num_inputs].type = type;
  58. cfg->inputs[cfg->num_inputs].has_boost_on_pin =
  59. nid_has_volume(codec, nid, HDA_INPUT);
  60. cfg->num_inputs++;
  61. }
  62. }
  63. static int compare_input_type(const void *ap, const void *bp)
  64. {
  65. const struct auto_pin_cfg_item *a = ap;
  66. const struct auto_pin_cfg_item *b = bp;
  67. if (a->type != b->type)
  68. return (int)(a->type - b->type);
  69. /* In case one has boost and the other one has not,
  70. pick the one with boost first. */
  71. return (int)(b->has_boost_on_pin - a->has_boost_on_pin);
  72. }
  73. /* Reorder the surround channels
  74. * ALSA sequence is front/surr/clfe/side
  75. * HDA sequence is:
  76. * 4-ch: front/surr => OK as it is
  77. * 6-ch: front/clfe/surr
  78. * 8-ch: front/clfe/rear/side|fc
  79. */
  80. static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
  81. {
  82. hda_nid_t nid;
  83. switch (nums) {
  84. case 3:
  85. case 4:
  86. nid = pins[1];
  87. pins[1] = pins[2];
  88. pins[2] = nid;
  89. break;
  90. }
  91. }
  92. /* check whether the given pin has a proper pin I/O capability bit */
  93. static bool check_pincap_validity(struct hda_codec *codec, hda_nid_t pin,
  94. unsigned int dev)
  95. {
  96. unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
  97. /* some old hardware don't return the proper pincaps */
  98. if (!pincap)
  99. return true;
  100. switch (dev) {
  101. case AC_JACK_LINE_OUT:
  102. case AC_JACK_SPEAKER:
  103. case AC_JACK_HP_OUT:
  104. case AC_JACK_SPDIF_OUT:
  105. case AC_JACK_DIG_OTHER_OUT:
  106. return !!(pincap & AC_PINCAP_OUT);
  107. default:
  108. return !!(pincap & AC_PINCAP_IN);
  109. }
  110. }
  111. static bool can_be_headset_mic(struct hda_codec *codec,
  112. struct auto_pin_cfg_item *item,
  113. int seq_number)
  114. {
  115. int attr;
  116. unsigned int def_conf;
  117. if (item->type != AUTO_PIN_MIC)
  118. return false;
  119. if (item->is_headset_mic || item->is_headphone_mic)
  120. return false; /* Already assigned */
  121. def_conf = snd_hda_codec_get_pincfg(codec, item->pin);
  122. attr = snd_hda_get_input_pin_attr(def_conf);
  123. if (attr <= INPUT_PIN_ATTR_DOCK)
  124. return false;
  125. if (seq_number >= 0) {
  126. int seq = get_defcfg_sequence(def_conf);
  127. if (seq != seq_number)
  128. return false;
  129. }
  130. return true;
  131. }
  132. /*
  133. * Parse all pin widgets and store the useful pin nids to cfg
  134. *
  135. * The number of line-outs or any primary output is stored in line_outs,
  136. * and the corresponding output pins are assigned to line_out_pins[],
  137. * in the order of front, rear, CLFE, side, ...
  138. *
  139. * If more extra outputs (speaker and headphone) are found, the pins are
  140. * assisnged to hp_pins[] and speaker_pins[], respectively. If no line-out jack
  141. * is detected, one of speaker of HP pins is assigned as the primary
  142. * output, i.e. to line_out_pins[0]. So, line_outs is always positive
  143. * if any analog output exists.
  144. *
  145. * The analog input pins are assigned to inputs array.
  146. * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
  147. * respectively.
  148. */
  149. int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
  150. struct auto_pin_cfg *cfg,
  151. const hda_nid_t *ignore_nids,
  152. unsigned int cond_flags)
  153. {
  154. hda_nid_t nid;
  155. short seq, assoc_line_out;
  156. struct auto_out_pin line_out[ARRAY_SIZE(cfg->line_out_pins)];
  157. struct auto_out_pin speaker_out[ARRAY_SIZE(cfg->speaker_pins)];
  158. struct auto_out_pin hp_out[ARRAY_SIZE(cfg->hp_pins)];
  159. int i;
  160. if (!snd_hda_get_int_hint(codec, "parser_flags", &i))
  161. cond_flags = i;
  162. memset(cfg, 0, sizeof(*cfg));
  163. memset(line_out, 0, sizeof(line_out));
  164. memset(speaker_out, 0, sizeof(speaker_out));
  165. memset(hp_out, 0, sizeof(hp_out));
  166. assoc_line_out = 0;
  167. for_each_hda_codec_node(nid, codec) {
  168. unsigned int wid_caps = get_wcaps(codec, nid);
  169. unsigned int wid_type = get_wcaps_type(wid_caps);
  170. unsigned int def_conf;
  171. short assoc, loc, conn, dev;
  172. /* read all default configuration for pin complex */
  173. if (wid_type != AC_WID_PIN)
  174. continue;
  175. /* ignore the given nids (e.g. pc-beep returns error) */
  176. if (ignore_nids && is_in_nid_list(nid, ignore_nids))
  177. continue;
  178. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  179. conn = get_defcfg_connect(def_conf);
  180. if (conn == AC_JACK_PORT_NONE)
  181. continue;
  182. loc = get_defcfg_location(def_conf);
  183. dev = get_defcfg_device(def_conf);
  184. /* workaround for buggy BIOS setups */
  185. if (dev == AC_JACK_LINE_OUT) {
  186. if (conn == AC_JACK_PORT_FIXED ||
  187. conn == AC_JACK_PORT_BOTH)
  188. dev = AC_JACK_SPEAKER;
  189. }
  190. if (!check_pincap_validity(codec, nid, dev))
  191. continue;
  192. switch (dev) {
  193. case AC_JACK_LINE_OUT:
  194. seq = get_defcfg_sequence(def_conf);
  195. assoc = get_defcfg_association(def_conf);
  196. if (!(wid_caps & AC_WCAP_STEREO))
  197. if (!cfg->mono_out_pin)
  198. cfg->mono_out_pin = nid;
  199. if (!assoc)
  200. continue;
  201. if (!assoc_line_out)
  202. assoc_line_out = assoc;
  203. else if (assoc_line_out != assoc) {
  204. codec_info(codec,
  205. "ignore pin 0x%x with mismatching assoc# 0x%x vs 0x%x\n",
  206. nid, assoc, assoc_line_out);
  207. continue;
  208. }
  209. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins)) {
  210. codec_info(codec,
  211. "ignore pin 0x%x, too many assigned pins\n",
  212. nid);
  213. continue;
  214. }
  215. line_out[cfg->line_outs].pin = nid;
  216. line_out[cfg->line_outs].seq = seq;
  217. cfg->line_outs++;
  218. break;
  219. case AC_JACK_SPEAKER:
  220. seq = get_defcfg_sequence(def_conf);
  221. assoc = get_defcfg_association(def_conf);
  222. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins)) {
  223. codec_info(codec,
  224. "ignore pin 0x%x, too many assigned pins\n",
  225. nid);
  226. continue;
  227. }
  228. speaker_out[cfg->speaker_outs].pin = nid;
  229. speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
  230. cfg->speaker_outs++;
  231. break;
  232. case AC_JACK_HP_OUT:
  233. seq = get_defcfg_sequence(def_conf);
  234. assoc = get_defcfg_association(def_conf);
  235. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins)) {
  236. codec_info(codec,
  237. "ignore pin 0x%x, too many assigned pins\n",
  238. nid);
  239. continue;
  240. }
  241. hp_out[cfg->hp_outs].pin = nid;
  242. hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
  243. cfg->hp_outs++;
  244. break;
  245. case AC_JACK_MIC_IN:
  246. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_MIC);
  247. break;
  248. case AC_JACK_LINE_IN:
  249. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_LINE_IN);
  250. break;
  251. case AC_JACK_CD:
  252. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_CD);
  253. break;
  254. case AC_JACK_AUX:
  255. add_auto_cfg_input_pin(codec, cfg, nid, AUTO_PIN_AUX);
  256. break;
  257. case AC_JACK_SPDIF_OUT:
  258. case AC_JACK_DIG_OTHER_OUT:
  259. if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins)) {
  260. codec_info(codec,
  261. "ignore pin 0x%x, too many assigned pins\n",
  262. nid);
  263. continue;
  264. }
  265. cfg->dig_out_pins[cfg->dig_outs] = nid;
  266. cfg->dig_out_type[cfg->dig_outs] =
  267. (loc == AC_JACK_LOC_HDMI) ?
  268. HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
  269. cfg->dig_outs++;
  270. break;
  271. case AC_JACK_SPDIF_IN:
  272. case AC_JACK_DIG_OTHER_IN:
  273. cfg->dig_in_pin = nid;
  274. if (loc == AC_JACK_LOC_HDMI)
  275. cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
  276. else
  277. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  278. break;
  279. }
  280. }
  281. /* Find a pin that could be a headset or headphone mic */
  282. if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
  283. bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
  284. bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
  285. for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
  286. if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
  287. cfg->inputs[i].is_headset_mic = 1;
  288. hsmic = false;
  289. } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
  290. cfg->inputs[i].is_headphone_mic = 1;
  291. hpmic = false;
  292. }
  293. /* If we didn't find our sequence number mark, fall back to any sequence number */
  294. for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
  295. if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
  296. continue;
  297. if (hsmic) {
  298. cfg->inputs[i].is_headset_mic = 1;
  299. hsmic = false;
  300. } else if (hpmic) {
  301. cfg->inputs[i].is_headphone_mic = 1;
  302. hpmic = false;
  303. }
  304. }
  305. if (hsmic)
  306. codec_dbg(codec, "Told to look for a headset mic, but didn't find any.\n");
  307. if (hpmic)
  308. codec_dbg(codec, "Told to look for a headphone mic, but didn't find any.\n");
  309. }
  310. /* FIX-UP:
  311. * If no line-out is defined but multiple HPs are found,
  312. * some of them might be the real line-outs.
  313. */
  314. if (!cfg->line_outs && cfg->hp_outs > 1 &&
  315. !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
  316. int i = 0;
  317. while (i < cfg->hp_outs) {
  318. /* The real HPs should have the sequence 0x0f */
  319. if ((hp_out[i].seq & 0x0f) == 0x0f) {
  320. i++;
  321. continue;
  322. }
  323. /* Move it to the line-out table */
  324. line_out[cfg->line_outs++] = hp_out[i];
  325. cfg->hp_outs--;
  326. memmove(hp_out + i, hp_out + i + 1,
  327. sizeof(hp_out[0]) * (cfg->hp_outs - i));
  328. }
  329. memset(hp_out + cfg->hp_outs, 0,
  330. sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
  331. if (!cfg->hp_outs)
  332. cfg->line_out_type = AUTO_PIN_HP_OUT;
  333. }
  334. /* sort by sequence */
  335. sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
  336. sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
  337. cfg->speaker_outs);
  338. sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
  339. /*
  340. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  341. * as a primary output
  342. */
  343. if (!cfg->line_outs &&
  344. !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
  345. if (cfg->speaker_outs) {
  346. cfg->line_outs = cfg->speaker_outs;
  347. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  348. sizeof(cfg->speaker_pins));
  349. cfg->speaker_outs = 0;
  350. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  351. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  352. } else if (cfg->hp_outs) {
  353. cfg->line_outs = cfg->hp_outs;
  354. memcpy(cfg->line_out_pins, cfg->hp_pins,
  355. sizeof(cfg->hp_pins));
  356. cfg->hp_outs = 0;
  357. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  358. cfg->line_out_type = AUTO_PIN_HP_OUT;
  359. }
  360. }
  361. reorder_outputs(cfg->line_outs, cfg->line_out_pins);
  362. reorder_outputs(cfg->hp_outs, cfg->hp_pins);
  363. reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
  364. /* sort inputs in the order of AUTO_PIN_* type */
  365. sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
  366. compare_input_type, NULL);
  367. /*
  368. * debug prints of the parsed results
  369. */
  370. codec_info(codec, "autoconfig for %s: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
  371. codec->core.chip_name, cfg->line_outs, cfg->line_out_pins[0],
  372. cfg->line_out_pins[1], cfg->line_out_pins[2],
  373. cfg->line_out_pins[3], cfg->line_out_pins[4],
  374. cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
  375. (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
  376. "speaker" : "line"));
  377. codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  378. cfg->speaker_outs, cfg->speaker_pins[0],
  379. cfg->speaker_pins[1], cfg->speaker_pins[2],
  380. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  381. codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  382. cfg->hp_outs, cfg->hp_pins[0],
  383. cfg->hp_pins[1], cfg->hp_pins[2],
  384. cfg->hp_pins[3], cfg->hp_pins[4]);
  385. codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
  386. if (cfg->dig_outs)
  387. codec_info(codec, " dig-out=0x%x/0x%x\n",
  388. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  389. codec_info(codec, " inputs:\n");
  390. for (i = 0; i < cfg->num_inputs; i++) {
  391. codec_info(codec, " %s=0x%x\n",
  392. hda_get_autocfg_input_label(codec, cfg, i),
  393. cfg->inputs[i].pin);
  394. }
  395. if (cfg->dig_in_pin)
  396. codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
  397. return 0;
  398. }
  399. EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
  400. /**
  401. * snd_hda_get_input_pin_attr - Get the input pin attribute from pin config
  402. * @def_conf: pin configuration value
  403. *
  404. * Guess the input pin attribute (INPUT_PIN_ATTR_XXX) from the given
  405. * default pin configuration value.
  406. */
  407. int snd_hda_get_input_pin_attr(unsigned int def_conf)
  408. {
  409. unsigned int loc = get_defcfg_location(def_conf);
  410. unsigned int conn = get_defcfg_connect(def_conf);
  411. if (conn == AC_JACK_PORT_NONE)
  412. return INPUT_PIN_ATTR_UNUSED;
  413. /* Windows may claim the internal mic to be BOTH, too */
  414. if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
  415. return INPUT_PIN_ATTR_INT;
  416. if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
  417. return INPUT_PIN_ATTR_INT;
  418. if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
  419. return INPUT_PIN_ATTR_DOCK;
  420. if (loc == AC_JACK_LOC_REAR)
  421. return INPUT_PIN_ATTR_REAR;
  422. if (loc == AC_JACK_LOC_FRONT)
  423. return INPUT_PIN_ATTR_FRONT;
  424. return INPUT_PIN_ATTR_NORMAL;
  425. }
  426. EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
  427. /**
  428. * hda_get_input_pin_label - Give a label for the given input pin
  429. * @codec: the HDA codec
  430. * @item: ping config item to refer
  431. * @pin: the pin NID
  432. * @check_location: flag to add the jack location prefix
  433. *
  434. * When @check_location is true, the function checks the pin location
  435. * for mic and line-in pins, and set an appropriate prefix like "Front",
  436. * "Rear", "Internal".
  437. */
  438. static const char *hda_get_input_pin_label(struct hda_codec *codec,
  439. const struct auto_pin_cfg_item *item,
  440. hda_nid_t pin, bool check_location)
  441. {
  442. unsigned int def_conf;
  443. static const char * const mic_names[] = {
  444. "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
  445. };
  446. int attr;
  447. def_conf = snd_hda_codec_get_pincfg(codec, pin);
  448. switch (get_defcfg_device(def_conf)) {
  449. case AC_JACK_MIC_IN:
  450. if (item && item->is_headset_mic)
  451. return "Headset Mic";
  452. if (item && item->is_headphone_mic)
  453. return "Headphone Mic";
  454. if (!check_location)
  455. return "Mic";
  456. attr = snd_hda_get_input_pin_attr(def_conf);
  457. if (!attr)
  458. return "None";
  459. return mic_names[attr - 1];
  460. case AC_JACK_LINE_IN:
  461. if (!check_location)
  462. return "Line";
  463. attr = snd_hda_get_input_pin_attr(def_conf);
  464. if (!attr)
  465. return "None";
  466. if (attr == INPUT_PIN_ATTR_DOCK)
  467. return "Dock Line";
  468. return "Line";
  469. case AC_JACK_AUX:
  470. return "Aux";
  471. case AC_JACK_CD:
  472. return "CD";
  473. case AC_JACK_SPDIF_IN:
  474. return "SPDIF In";
  475. case AC_JACK_DIG_OTHER_IN:
  476. return "Digital In";
  477. case AC_JACK_HP_OUT:
  478. return "Headphone Mic";
  479. default:
  480. return "Misc";
  481. }
  482. }
  483. /* Check whether the location prefix needs to be added to the label.
  484. * If all mic-jacks are in the same location (e.g. rear panel), we don't
  485. * have to put "Front" prefix to each label. In such a case, returns false.
  486. */
  487. static int check_mic_location_need(struct hda_codec *codec,
  488. const struct auto_pin_cfg *cfg,
  489. int input)
  490. {
  491. unsigned int defc;
  492. int i, attr, attr2;
  493. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
  494. attr = snd_hda_get_input_pin_attr(defc);
  495. /* for internal or docking mics, we need locations */
  496. if (attr <= INPUT_PIN_ATTR_NORMAL)
  497. return 1;
  498. attr = 0;
  499. for (i = 0; i < cfg->num_inputs; i++) {
  500. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
  501. attr2 = snd_hda_get_input_pin_attr(defc);
  502. if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
  503. if (attr && attr != attr2)
  504. return 1; /* different locations found */
  505. attr = attr2;
  506. }
  507. }
  508. return 0;
  509. }
  510. /**
  511. * hda_get_autocfg_input_label - Get a label for the given input
  512. * @codec: the HDA codec
  513. * @cfg: the parsed pin configuration
  514. * @input: the input index number
  515. *
  516. * Get a label for the given input pin defined by the autocfg item.
  517. * Unlike hda_get_input_pin_label(), this function checks all inputs
  518. * defined in autocfg and avoids the redundant mic/line prefix as much as
  519. * possible.
  520. */
  521. const char *hda_get_autocfg_input_label(struct hda_codec *codec,
  522. const struct auto_pin_cfg *cfg,
  523. int input)
  524. {
  525. int type = cfg->inputs[input].type;
  526. int has_multiple_pins = 0;
  527. if ((input > 0 && cfg->inputs[input - 1].type == type) ||
  528. (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
  529. has_multiple_pins = 1;
  530. if (has_multiple_pins && type == AUTO_PIN_MIC)
  531. has_multiple_pins &= check_mic_location_need(codec, cfg, input);
  532. return hda_get_input_pin_label(codec, &cfg->inputs[input],
  533. cfg->inputs[input].pin,
  534. has_multiple_pins);
  535. }
  536. EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
  537. /* return the position of NID in the list, or -1 if not found */
  538. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  539. {
  540. int i;
  541. for (i = 0; i < nums; i++)
  542. if (list[i] == nid)
  543. return i;
  544. return -1;
  545. }
  546. /* get a unique suffix or an index number */
  547. static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
  548. int num_pins, int *indexp)
  549. {
  550. static const char * const channel_sfx[] = {
  551. " Front", " Surround", " CLFE", " Side"
  552. };
  553. int i;
  554. i = find_idx_in_nid_list(nid, pins, num_pins);
  555. if (i < 0)
  556. return NULL;
  557. if (num_pins == 1)
  558. return "";
  559. if (num_pins > ARRAY_SIZE(channel_sfx)) {
  560. if (indexp)
  561. *indexp = i;
  562. return "";
  563. }
  564. return channel_sfx[i];
  565. }
  566. static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
  567. {
  568. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  569. int attr = snd_hda_get_input_pin_attr(def_conf);
  570. /* check the location */
  571. switch (attr) {
  572. case INPUT_PIN_ATTR_DOCK:
  573. return "Dock ";
  574. case INPUT_PIN_ATTR_FRONT:
  575. return "Front ";
  576. }
  577. return "";
  578. }
  579. static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
  580. const hda_nid_t *pins, int num_pins)
  581. {
  582. int i, j, idx = 0;
  583. const char *pfx = check_output_pfx(codec, nid);
  584. i = find_idx_in_nid_list(nid, pins, num_pins);
  585. if (i < 0)
  586. return -1;
  587. for (j = 0; j < i; j++)
  588. if (pfx == check_output_pfx(codec, pins[j]))
  589. idx++;
  590. return idx;
  591. }
  592. static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
  593. const struct auto_pin_cfg *cfg,
  594. const char *name, char *label, int maxlen,
  595. int *indexp)
  596. {
  597. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  598. int attr = snd_hda_get_input_pin_attr(def_conf);
  599. const char *pfx, *sfx = "";
  600. /* handle as a speaker if it's a fixed line-out */
  601. if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
  602. name = "Speaker";
  603. pfx = check_output_pfx(codec, nid);
  604. if (cfg) {
  605. /* try to give a unique suffix if needed */
  606. sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
  607. indexp);
  608. if (!sfx)
  609. sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
  610. indexp);
  611. if (!sfx) {
  612. /* don't add channel suffix for Headphone controls */
  613. int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
  614. cfg->hp_outs);
  615. if (idx >= 0 && indexp)
  616. *indexp = idx;
  617. sfx = "";
  618. }
  619. }
  620. snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
  621. return 1;
  622. }
  623. #define is_hdmi_cfg(conf) \
  624. (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
  625. /**
  626. * snd_hda_get_pin_label - Get a label for the given I/O pin
  627. * @codec: the HDA codec
  628. * @nid: pin NID
  629. * @cfg: the parsed pin configuration
  630. * @label: the string buffer to store
  631. * @maxlen: the max length of string buffer (including termination)
  632. * @indexp: the pointer to return the index number (for multiple ctls)
  633. *
  634. * Get a label for the given pin. This function works for both input and
  635. * output pins. When @cfg is given as non-NULL, the function tries to get
  636. * an optimized label using hda_get_autocfg_input_label().
  637. *
  638. * This function tries to give a unique label string for the pin as much as
  639. * possible. For example, when the multiple line-outs are present, it adds
  640. * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
  641. * If no unique name with a suffix is available and @indexp is non-NULL, the
  642. * index number is stored in the pointer.
  643. */
  644. int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
  645. const struct auto_pin_cfg *cfg,
  646. char *label, int maxlen, int *indexp)
  647. {
  648. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  649. const char *name = NULL;
  650. int i;
  651. bool hdmi;
  652. if (indexp)
  653. *indexp = 0;
  654. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  655. return 0;
  656. switch (get_defcfg_device(def_conf)) {
  657. case AC_JACK_LINE_OUT:
  658. return fill_audio_out_name(codec, nid, cfg, "Line Out",
  659. label, maxlen, indexp);
  660. case AC_JACK_SPEAKER:
  661. return fill_audio_out_name(codec, nid, cfg, "Speaker",
  662. label, maxlen, indexp);
  663. case AC_JACK_HP_OUT:
  664. return fill_audio_out_name(codec, nid, cfg, "Headphone",
  665. label, maxlen, indexp);
  666. case AC_JACK_SPDIF_OUT:
  667. case AC_JACK_DIG_OTHER_OUT:
  668. hdmi = is_hdmi_cfg(def_conf);
  669. name = hdmi ? "HDMI" : "SPDIF";
  670. if (cfg && indexp)
  671. for (i = 0; i < cfg->dig_outs; i++) {
  672. hda_nid_t pin = cfg->dig_out_pins[i];
  673. unsigned int c;
  674. if (pin == nid)
  675. break;
  676. c = snd_hda_codec_get_pincfg(codec, pin);
  677. if (hdmi == is_hdmi_cfg(c))
  678. (*indexp)++;
  679. }
  680. break;
  681. default:
  682. if (cfg) {
  683. for (i = 0; i < cfg->num_inputs; i++) {
  684. if (cfg->inputs[i].pin != nid)
  685. continue;
  686. name = hda_get_autocfg_input_label(codec, cfg, i);
  687. if (name)
  688. break;
  689. }
  690. }
  691. if (!name)
  692. name = hda_get_input_pin_label(codec, NULL, nid, true);
  693. break;
  694. }
  695. if (!name)
  696. return 0;
  697. strlcpy(label, name, maxlen);
  698. return 1;
  699. }
  700. EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
  701. /**
  702. * snd_hda_add_verbs - Add verbs to the init list
  703. * @codec: the HDA codec
  704. * @list: zero-terminated verb list to add
  705. *
  706. * Append the given verb list to the execution list. The verbs will be
  707. * performed at init and resume time via snd_hda_apply_verbs().
  708. */
  709. int snd_hda_add_verbs(struct hda_codec *codec,
  710. const struct hda_verb *list)
  711. {
  712. const struct hda_verb **v;
  713. v = snd_array_new(&codec->verbs);
  714. if (!v)
  715. return -ENOMEM;
  716. *v = list;
  717. return 0;
  718. }
  719. EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
  720. /**
  721. * snd_hda_apply_verbs - Execute the init verb lists
  722. * @codec: the HDA codec
  723. */
  724. void snd_hda_apply_verbs(struct hda_codec *codec)
  725. {
  726. int i;
  727. for (i = 0; i < codec->verbs.used; i++) {
  728. struct hda_verb **v = snd_array_elem(&codec->verbs, i);
  729. snd_hda_sequence_write(codec, *v);
  730. }
  731. }
  732. EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
  733. /**
  734. * snd_hda_apply_pincfgs - Set each pin config in the given list
  735. * @codec: the HDA codec
  736. * @cfg: NULL-terminated pin config table
  737. */
  738. void snd_hda_apply_pincfgs(struct hda_codec *codec,
  739. const struct hda_pintbl *cfg)
  740. {
  741. for (; cfg->nid; cfg++)
  742. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  743. }
  744. EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
  745. static void set_pin_targets(struct hda_codec *codec,
  746. const struct hda_pintbl *cfg)
  747. {
  748. for (; cfg->nid; cfg++)
  749. snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
  750. }
  751. static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
  752. {
  753. const char *modelname = codec->fixup_name;
  754. while (id >= 0) {
  755. const struct hda_fixup *fix = codec->fixup_list + id;
  756. if (fix->chained_before)
  757. apply_fixup(codec, fix->chain_id, action, depth + 1);
  758. switch (fix->type) {
  759. case HDA_FIXUP_PINS:
  760. if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
  761. break;
  762. codec_dbg(codec, "%s: Apply pincfg for %s\n",
  763. codec->core.chip_name, modelname);
  764. snd_hda_apply_pincfgs(codec, fix->v.pins);
  765. break;
  766. case HDA_FIXUP_VERBS:
  767. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
  768. break;
  769. codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
  770. codec->core.chip_name, modelname);
  771. snd_hda_add_verbs(codec, fix->v.verbs);
  772. break;
  773. case HDA_FIXUP_FUNC:
  774. if (!fix->v.func)
  775. break;
  776. codec_dbg(codec, "%s: Apply fix-func for %s\n",
  777. codec->core.chip_name, modelname);
  778. fix->v.func(codec, fix, action);
  779. break;
  780. case HDA_FIXUP_PINCTLS:
  781. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
  782. break;
  783. codec_dbg(codec, "%s: Apply pinctl for %s\n",
  784. codec->core.chip_name, modelname);
  785. set_pin_targets(codec, fix->v.pins);
  786. break;
  787. default:
  788. codec_err(codec, "%s: Invalid fixup type %d\n",
  789. codec->core.chip_name, fix->type);
  790. break;
  791. }
  792. if (!fix->chained || fix->chained_before)
  793. break;
  794. if (++depth > 10)
  795. break;
  796. id = fix->chain_id;
  797. }
  798. }
  799. /**
  800. * snd_hda_apply_fixup - Apply the fixup chain with the given action
  801. * @codec: the HDA codec
  802. * @action: fixup action (HDA_FIXUP_ACT_XXX)
  803. */
  804. void snd_hda_apply_fixup(struct hda_codec *codec, int action)
  805. {
  806. if (codec->fixup_list)
  807. apply_fixup(codec, codec->fixup_id, action, 0);
  808. }
  809. EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
  810. static bool pin_config_match(struct hda_codec *codec,
  811. const struct hda_pintbl *pins)
  812. {
  813. for (; pins->nid; pins++) {
  814. u32 def_conf = snd_hda_codec_get_pincfg(codec, pins->nid);
  815. if (pins->val != def_conf)
  816. return false;
  817. }
  818. return true;
  819. }
  820. /**
  821. * snd_hda_pick_pin_fixup - Pick up a fixup matching with the pin quirk list
  822. * @codec: the HDA codec
  823. * @pin_quirk: zero-terminated pin quirk list
  824. * @fixlist: the fixup list
  825. */
  826. void snd_hda_pick_pin_fixup(struct hda_codec *codec,
  827. const struct snd_hda_pin_quirk *pin_quirk,
  828. const struct hda_fixup *fixlist)
  829. {
  830. const struct snd_hda_pin_quirk *pq;
  831. if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
  832. return;
  833. for (pq = pin_quirk; pq->subvendor; pq++) {
  834. if ((codec->core.subsystem_id & 0xffff0000) != (pq->subvendor << 16))
  835. continue;
  836. if (codec->core.vendor_id != pq->codec)
  837. continue;
  838. if (pin_config_match(codec, pq->pins)) {
  839. codec->fixup_id = pq->value;
  840. #ifdef CONFIG_SND_DEBUG_VERBOSE
  841. codec->fixup_name = pq->name;
  842. codec_dbg(codec, "%s: picked fixup %s (pin match)\n",
  843. codec->core.chip_name, codec->fixup_name);
  844. #endif
  845. codec->fixup_list = fixlist;
  846. return;
  847. }
  848. }
  849. }
  850. EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
  851. /**
  852. * snd_hda_pick_fixup - Pick up a fixup matching with PCI/codec SSID or model string
  853. * @codec: the HDA codec
  854. * @models: NULL-terminated model string list
  855. * @quirk: zero-terminated PCI/codec SSID quirk list
  856. * @fixlist: the fixup list
  857. *
  858. * Pick up a fixup entry matching with the given model string or SSID.
  859. * If a fixup was already set beforehand, the function doesn't do anything.
  860. * When a special model string "nofixup" is given, also no fixup is applied.
  861. *
  862. * The function tries to find the matching model name at first, if given.
  863. * If nothing matched, try to look up the PCI SSID.
  864. * If still nothing matched, try to look up the codec SSID.
  865. */
  866. void snd_hda_pick_fixup(struct hda_codec *codec,
  867. const struct hda_model_fixup *models,
  868. const struct snd_pci_quirk *quirk,
  869. const struct hda_fixup *fixlist)
  870. {
  871. const struct snd_pci_quirk *q;
  872. int id = HDA_FIXUP_ID_NOT_SET;
  873. const char *name = NULL;
  874. if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
  875. return;
  876. /* when model=nofixup is given, don't pick up any fixups */
  877. if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
  878. codec->fixup_list = NULL;
  879. codec->fixup_name = NULL;
  880. codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
  881. codec_dbg(codec, "%s: picked no fixup (nofixup specified)\n",
  882. codec->core.chip_name);
  883. return;
  884. }
  885. if (codec->modelname && models) {
  886. while (models->name) {
  887. if (!strcmp(codec->modelname, models->name)) {
  888. codec->fixup_id = models->id;
  889. codec->fixup_name = models->name;
  890. codec->fixup_list = fixlist;
  891. codec_dbg(codec, "%s: picked fixup %s (model specified)\n",
  892. codec->core.chip_name, codec->fixup_name);
  893. return;
  894. }
  895. models++;
  896. }
  897. }
  898. if (quirk) {
  899. q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
  900. if (q) {
  901. id = q->value;
  902. #ifdef CONFIG_SND_DEBUG_VERBOSE
  903. name = q->name;
  904. codec_dbg(codec, "%s: picked fixup %s (PCI SSID%s)\n",
  905. codec->core.chip_name, name, q->subdevice_mask ? "" : " - vendor generic");
  906. #endif
  907. }
  908. }
  909. if (id < 0 && quirk) {
  910. for (q = quirk; q->subvendor || q->subdevice; q++) {
  911. unsigned int vendorid =
  912. q->subdevice | (q->subvendor << 16);
  913. unsigned int mask = 0xffff0000 | q->subdevice_mask;
  914. if ((codec->core.subsystem_id & mask) == (vendorid & mask)) {
  915. id = q->value;
  916. #ifdef CONFIG_SND_DEBUG_VERBOSE
  917. name = q->name;
  918. codec_dbg(codec, "%s: picked fixup %s (codec SSID)\n",
  919. codec->core.chip_name, name);
  920. #endif
  921. break;
  922. }
  923. }
  924. }
  925. codec->fixup_id = id;
  926. if (id >= 0) {
  927. codec->fixup_list = fixlist;
  928. codec->fixup_name = name;
  929. }
  930. }
  931. EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);