hda_auto_parser.c 26 KB

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