hda_auto_parser.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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, end_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. end_nid = codec->start_nid + codec->num_nodes;
  168. for (nid = codec->start_nid; nid < end_nid; nid++) {
  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. int 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. sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
  367. compare_input_type, NULL);
  368. /*
  369. * debug prints of the parsed results
  370. */
  371. codec_info(codec, "autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
  372. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  373. cfg->line_out_pins[2], cfg->line_out_pins[3],
  374. cfg->line_out_pins[4],
  375. cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
  376. (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
  377. "speaker" : "line"));
  378. codec_info(codec, " speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  379. cfg->speaker_outs, cfg->speaker_pins[0],
  380. cfg->speaker_pins[1], cfg->speaker_pins[2],
  381. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  382. codec_info(codec, " hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  383. cfg->hp_outs, cfg->hp_pins[0],
  384. cfg->hp_pins[1], cfg->hp_pins[2],
  385. cfg->hp_pins[3], cfg->hp_pins[4]);
  386. codec_info(codec, " mono: mono_out=0x%x\n", cfg->mono_out_pin);
  387. if (cfg->dig_outs)
  388. codec_info(codec, " dig-out=0x%x/0x%x\n",
  389. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  390. codec_info(codec, " inputs:\n");
  391. for (i = 0; i < cfg->num_inputs; i++) {
  392. codec_info(codec, " %s=0x%x\n",
  393. hda_get_autocfg_input_label(codec, cfg, i),
  394. cfg->inputs[i].pin);
  395. }
  396. if (cfg->dig_in_pin)
  397. codec_info(codec, " dig-in=0x%x\n", cfg->dig_in_pin);
  398. return 0;
  399. }
  400. EXPORT_SYMBOL_GPL(snd_hda_parse_pin_defcfg);
  401. int snd_hda_get_input_pin_attr(unsigned int def_conf)
  402. {
  403. unsigned int loc = get_defcfg_location(def_conf);
  404. unsigned int conn = get_defcfg_connect(def_conf);
  405. if (conn == AC_JACK_PORT_NONE)
  406. return INPUT_PIN_ATTR_UNUSED;
  407. /* Windows may claim the internal mic to be BOTH, too */
  408. if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
  409. return INPUT_PIN_ATTR_INT;
  410. if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
  411. return INPUT_PIN_ATTR_INT;
  412. if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
  413. return INPUT_PIN_ATTR_DOCK;
  414. if (loc == AC_JACK_LOC_REAR)
  415. return INPUT_PIN_ATTR_REAR;
  416. if (loc == AC_JACK_LOC_FRONT)
  417. return INPUT_PIN_ATTR_FRONT;
  418. return INPUT_PIN_ATTR_NORMAL;
  419. }
  420. EXPORT_SYMBOL_GPL(snd_hda_get_input_pin_attr);
  421. /**
  422. * hda_get_input_pin_label - Give a label for the given input pin
  423. *
  424. * When check_location is true, the function checks the pin location
  425. * for mic and line-in pins, and set an appropriate prefix like "Front",
  426. * "Rear", "Internal".
  427. */
  428. static const char *hda_get_input_pin_label(struct hda_codec *codec,
  429. const struct auto_pin_cfg_item *item,
  430. hda_nid_t pin, bool check_location)
  431. {
  432. unsigned int def_conf;
  433. static const char * const mic_names[] = {
  434. "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
  435. };
  436. int attr;
  437. def_conf = snd_hda_codec_get_pincfg(codec, pin);
  438. switch (get_defcfg_device(def_conf)) {
  439. case AC_JACK_MIC_IN:
  440. if (item && item->is_headset_mic)
  441. return "Headset Mic";
  442. if (item && item->is_headphone_mic)
  443. return "Headphone Mic";
  444. if (!check_location)
  445. return "Mic";
  446. attr = snd_hda_get_input_pin_attr(def_conf);
  447. if (!attr)
  448. return "None";
  449. return mic_names[attr - 1];
  450. case AC_JACK_LINE_IN:
  451. if (!check_location)
  452. return "Line";
  453. attr = snd_hda_get_input_pin_attr(def_conf);
  454. if (!attr)
  455. return "None";
  456. if (attr == INPUT_PIN_ATTR_DOCK)
  457. return "Dock Line";
  458. return "Line";
  459. case AC_JACK_AUX:
  460. return "Aux";
  461. case AC_JACK_CD:
  462. return "CD";
  463. case AC_JACK_SPDIF_IN:
  464. return "SPDIF In";
  465. case AC_JACK_DIG_OTHER_IN:
  466. return "Digital In";
  467. case AC_JACK_HP_OUT:
  468. return "Headphone Mic";
  469. default:
  470. return "Misc";
  471. }
  472. }
  473. /* Check whether the location prefix needs to be added to the label.
  474. * If all mic-jacks are in the same location (e.g. rear panel), we don't
  475. * have to put "Front" prefix to each label. In such a case, returns false.
  476. */
  477. static int check_mic_location_need(struct hda_codec *codec,
  478. const struct auto_pin_cfg *cfg,
  479. int input)
  480. {
  481. unsigned int defc;
  482. int i, attr, attr2;
  483. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
  484. attr = snd_hda_get_input_pin_attr(defc);
  485. /* for internal or docking mics, we need locations */
  486. if (attr <= INPUT_PIN_ATTR_NORMAL)
  487. return 1;
  488. attr = 0;
  489. for (i = 0; i < cfg->num_inputs; i++) {
  490. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
  491. attr2 = snd_hda_get_input_pin_attr(defc);
  492. if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
  493. if (attr && attr != attr2)
  494. return 1; /* different locations found */
  495. attr = attr2;
  496. }
  497. }
  498. return 0;
  499. }
  500. /**
  501. * hda_get_autocfg_input_label - Get a label for the given input
  502. *
  503. * Get a label for the given input pin defined by the autocfg item.
  504. * Unlike hda_get_input_pin_label(), this function checks all inputs
  505. * defined in autocfg and avoids the redundant mic/line prefix as much as
  506. * possible.
  507. */
  508. const char *hda_get_autocfg_input_label(struct hda_codec *codec,
  509. const struct auto_pin_cfg *cfg,
  510. int input)
  511. {
  512. int type = cfg->inputs[input].type;
  513. int has_multiple_pins = 0;
  514. if ((input > 0 && cfg->inputs[input - 1].type == type) ||
  515. (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
  516. has_multiple_pins = 1;
  517. if (has_multiple_pins && type == AUTO_PIN_MIC)
  518. has_multiple_pins &= check_mic_location_need(codec, cfg, input);
  519. return hda_get_input_pin_label(codec, &cfg->inputs[input],
  520. cfg->inputs[input].pin,
  521. has_multiple_pins);
  522. }
  523. EXPORT_SYMBOL_GPL(hda_get_autocfg_input_label);
  524. /* return the position of NID in the list, or -1 if not found */
  525. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  526. {
  527. int i;
  528. for (i = 0; i < nums; i++)
  529. if (list[i] == nid)
  530. return i;
  531. return -1;
  532. }
  533. /* get a unique suffix or an index number */
  534. static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
  535. int num_pins, int *indexp)
  536. {
  537. static const char * const channel_sfx[] = {
  538. " Front", " Surround", " CLFE", " Side"
  539. };
  540. int i;
  541. i = find_idx_in_nid_list(nid, pins, num_pins);
  542. if (i < 0)
  543. return NULL;
  544. if (num_pins == 1)
  545. return "";
  546. if (num_pins > ARRAY_SIZE(channel_sfx)) {
  547. if (indexp)
  548. *indexp = i;
  549. return "";
  550. }
  551. return channel_sfx[i];
  552. }
  553. static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
  554. {
  555. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  556. int attr = snd_hda_get_input_pin_attr(def_conf);
  557. /* check the location */
  558. switch (attr) {
  559. case INPUT_PIN_ATTR_DOCK:
  560. return "Dock ";
  561. case INPUT_PIN_ATTR_FRONT:
  562. return "Front ";
  563. }
  564. return "";
  565. }
  566. static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
  567. const hda_nid_t *pins, int num_pins)
  568. {
  569. int i, j, idx = 0;
  570. const char *pfx = check_output_pfx(codec, nid);
  571. i = find_idx_in_nid_list(nid, pins, num_pins);
  572. if (i < 0)
  573. return -1;
  574. for (j = 0; j < i; j++)
  575. if (pfx == check_output_pfx(codec, pins[j]))
  576. idx++;
  577. return idx;
  578. }
  579. static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
  580. const struct auto_pin_cfg *cfg,
  581. const char *name, char *label, int maxlen,
  582. int *indexp)
  583. {
  584. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  585. int attr = snd_hda_get_input_pin_attr(def_conf);
  586. const char *pfx, *sfx = "";
  587. /* handle as a speaker if it's a fixed line-out */
  588. if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
  589. name = "Speaker";
  590. pfx = check_output_pfx(codec, nid);
  591. if (cfg) {
  592. /* try to give a unique suffix if needed */
  593. sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
  594. indexp);
  595. if (!sfx)
  596. sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
  597. indexp);
  598. if (!sfx) {
  599. /* don't add channel suffix for Headphone controls */
  600. int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
  601. cfg->hp_outs);
  602. if (idx >= 0 && indexp)
  603. *indexp = idx;
  604. sfx = "";
  605. }
  606. }
  607. snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
  608. return 1;
  609. }
  610. #define is_hdmi_cfg(conf) \
  611. (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
  612. /**
  613. * snd_hda_get_pin_label - Get a label for the given I/O pin
  614. *
  615. * Get a label for the given pin. This function works for both input and
  616. * output pins. When @cfg is given as non-NULL, the function tries to get
  617. * an optimized label using hda_get_autocfg_input_label().
  618. *
  619. * This function tries to give a unique label string for the pin as much as
  620. * possible. For example, when the multiple line-outs are present, it adds
  621. * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
  622. * If no unique name with a suffix is available and @indexp is non-NULL, the
  623. * index number is stored in the pointer.
  624. */
  625. int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
  626. const struct auto_pin_cfg *cfg,
  627. char *label, int maxlen, int *indexp)
  628. {
  629. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  630. const char *name = NULL;
  631. int i;
  632. bool hdmi;
  633. if (indexp)
  634. *indexp = 0;
  635. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  636. return 0;
  637. switch (get_defcfg_device(def_conf)) {
  638. case AC_JACK_LINE_OUT:
  639. return fill_audio_out_name(codec, nid, cfg, "Line Out",
  640. label, maxlen, indexp);
  641. case AC_JACK_SPEAKER:
  642. return fill_audio_out_name(codec, nid, cfg, "Speaker",
  643. label, maxlen, indexp);
  644. case AC_JACK_HP_OUT:
  645. return fill_audio_out_name(codec, nid, cfg, "Headphone",
  646. label, maxlen, indexp);
  647. case AC_JACK_SPDIF_OUT:
  648. case AC_JACK_DIG_OTHER_OUT:
  649. hdmi = is_hdmi_cfg(def_conf);
  650. name = hdmi ? "HDMI" : "SPDIF";
  651. if (cfg && indexp)
  652. for (i = 0; i < cfg->dig_outs; i++) {
  653. hda_nid_t pin = cfg->dig_out_pins[i];
  654. unsigned int c;
  655. if (pin == nid)
  656. break;
  657. c = snd_hda_codec_get_pincfg(codec, pin);
  658. if (hdmi == is_hdmi_cfg(c))
  659. (*indexp)++;
  660. }
  661. break;
  662. default:
  663. if (cfg) {
  664. for (i = 0; i < cfg->num_inputs; i++) {
  665. if (cfg->inputs[i].pin != nid)
  666. continue;
  667. name = hda_get_autocfg_input_label(codec, cfg, i);
  668. if (name)
  669. break;
  670. }
  671. }
  672. if (!name)
  673. name = hda_get_input_pin_label(codec, NULL, nid, true);
  674. break;
  675. }
  676. if (!name)
  677. return 0;
  678. strlcpy(label, name, maxlen);
  679. return 1;
  680. }
  681. EXPORT_SYMBOL_GPL(snd_hda_get_pin_label);
  682. int snd_hda_add_verbs(struct hda_codec *codec,
  683. const struct hda_verb *list)
  684. {
  685. const struct hda_verb **v;
  686. v = snd_array_new(&codec->verbs);
  687. if (!v)
  688. return -ENOMEM;
  689. *v = list;
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(snd_hda_add_verbs);
  693. void snd_hda_apply_verbs(struct hda_codec *codec)
  694. {
  695. int i;
  696. for (i = 0; i < codec->verbs.used; i++) {
  697. struct hda_verb **v = snd_array_elem(&codec->verbs, i);
  698. snd_hda_sequence_write(codec, *v);
  699. }
  700. }
  701. EXPORT_SYMBOL_GPL(snd_hda_apply_verbs);
  702. void snd_hda_apply_pincfgs(struct hda_codec *codec,
  703. const struct hda_pintbl *cfg)
  704. {
  705. for (; cfg->nid; cfg++)
  706. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  707. }
  708. EXPORT_SYMBOL_GPL(snd_hda_apply_pincfgs);
  709. static void set_pin_targets(struct hda_codec *codec,
  710. const struct hda_pintbl *cfg)
  711. {
  712. for (; cfg->nid; cfg++)
  713. snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
  714. }
  715. static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
  716. {
  717. const char *modelname = codec->fixup_name;
  718. while (id >= 0) {
  719. const struct hda_fixup *fix = codec->fixup_list + id;
  720. if (fix->chained_before)
  721. apply_fixup(codec, fix->chain_id, action, depth + 1);
  722. switch (fix->type) {
  723. case HDA_FIXUP_PINS:
  724. if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
  725. break;
  726. codec_dbg(codec, "%s: Apply pincfg for %s\n",
  727. codec->chip_name, modelname);
  728. snd_hda_apply_pincfgs(codec, fix->v.pins);
  729. break;
  730. case HDA_FIXUP_VERBS:
  731. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
  732. break;
  733. codec_dbg(codec, "%s: Apply fix-verbs for %s\n",
  734. codec->chip_name, modelname);
  735. snd_hda_add_verbs(codec, fix->v.verbs);
  736. break;
  737. case HDA_FIXUP_FUNC:
  738. if (!fix->v.func)
  739. break;
  740. codec_dbg(codec, "%s: Apply fix-func for %s\n",
  741. codec->chip_name, modelname);
  742. fix->v.func(codec, fix, action);
  743. break;
  744. case HDA_FIXUP_PINCTLS:
  745. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
  746. break;
  747. codec_dbg(codec, "%s: Apply pinctl for %s\n",
  748. codec->chip_name, modelname);
  749. set_pin_targets(codec, fix->v.pins);
  750. break;
  751. default:
  752. codec_err(codec, "%s: Invalid fixup type %d\n",
  753. codec->chip_name, fix->type);
  754. break;
  755. }
  756. if (!fix->chained || fix->chained_before)
  757. break;
  758. if (++depth > 10)
  759. break;
  760. id = fix->chain_id;
  761. }
  762. }
  763. void snd_hda_apply_fixup(struct hda_codec *codec, int action)
  764. {
  765. if (codec->fixup_list)
  766. apply_fixup(codec, codec->fixup_id, action, 0);
  767. }
  768. EXPORT_SYMBOL_GPL(snd_hda_apply_fixup);
  769. static bool pin_config_match(struct hda_codec *codec,
  770. const struct hda_pintbl *pins)
  771. {
  772. for (; pins->nid; pins++) {
  773. u32 def_conf = snd_hda_codec_get_pincfg(codec, pins->nid);
  774. if (pins->val != def_conf)
  775. return false;
  776. }
  777. return true;
  778. }
  779. void snd_hda_pick_pin_fixup(struct hda_codec *codec,
  780. const struct snd_hda_pin_quirk *pin_quirk,
  781. const struct hda_fixup *fixlist)
  782. {
  783. const struct snd_hda_pin_quirk *pq;
  784. if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
  785. return;
  786. for (pq = pin_quirk; pq->subvendor; pq++) {
  787. if ((codec->subsystem_id & 0xffff0000) != (pq->subvendor << 16))
  788. continue;
  789. if (codec->vendor_id != pq->codec)
  790. continue;
  791. if (pin_config_match(codec, pq->pins)) {
  792. codec->fixup_id = pq->value;
  793. #ifdef CONFIG_SND_DEBUG_VERBOSE
  794. codec->fixup_name = pq->name;
  795. #endif
  796. codec->fixup_list = fixlist;
  797. return;
  798. }
  799. }
  800. }
  801. EXPORT_SYMBOL_GPL(snd_hda_pick_pin_fixup);
  802. void snd_hda_pick_fixup(struct hda_codec *codec,
  803. const struct hda_model_fixup *models,
  804. const struct snd_pci_quirk *quirk,
  805. const struct hda_fixup *fixlist)
  806. {
  807. const struct snd_pci_quirk *q;
  808. int id = HDA_FIXUP_ID_NOT_SET;
  809. const char *name = NULL;
  810. if (codec->fixup_id != HDA_FIXUP_ID_NOT_SET)
  811. return;
  812. /* when model=nofixup is given, don't pick up any fixups */
  813. if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
  814. codec->fixup_list = NULL;
  815. codec->fixup_name = NULL;
  816. codec->fixup_id = HDA_FIXUP_ID_NO_FIXUP;
  817. return;
  818. }
  819. if (codec->modelname && models) {
  820. while (models->name) {
  821. if (!strcmp(codec->modelname, models->name)) {
  822. codec->fixup_id = models->id;
  823. codec->fixup_name = models->name;
  824. codec->fixup_list = fixlist;
  825. return;
  826. }
  827. models++;
  828. }
  829. }
  830. if (quirk) {
  831. q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
  832. if (q) {
  833. id = q->value;
  834. #ifdef CONFIG_SND_DEBUG_VERBOSE
  835. name = q->name;
  836. #endif
  837. }
  838. }
  839. if (id < 0 && quirk) {
  840. for (q = quirk; q->subvendor || q->subdevice; q++) {
  841. unsigned int vendorid =
  842. q->subdevice | (q->subvendor << 16);
  843. unsigned int mask = 0xffff0000 | q->subdevice_mask;
  844. if ((codec->subsystem_id & mask) == (vendorid & mask)) {
  845. id = q->value;
  846. #ifdef CONFIG_SND_DEBUG_VERBOSE
  847. name = q->name;
  848. #endif
  849. break;
  850. }
  851. }
  852. }
  853. codec->fixup_id = id;
  854. if (id >= 0) {
  855. codec->fixup_list = fixlist;
  856. codec->fixup_name = name;
  857. }
  858. }
  859. EXPORT_SYMBOL_GPL(snd_hda_pick_fixup);