auto_parser.c 31 KB

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