hda_auto_parser.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. continue;
  201. if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
  202. continue;
  203. line_out[cfg->line_outs].pin = nid;
  204. line_out[cfg->line_outs].seq = seq;
  205. cfg->line_outs++;
  206. break;
  207. case AC_JACK_SPEAKER:
  208. seq = get_defcfg_sequence(def_conf);
  209. assoc = get_defcfg_association(def_conf);
  210. if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
  211. continue;
  212. speaker_out[cfg->speaker_outs].pin = nid;
  213. speaker_out[cfg->speaker_outs].seq = (assoc << 4) | seq;
  214. cfg->speaker_outs++;
  215. break;
  216. case AC_JACK_HP_OUT:
  217. seq = get_defcfg_sequence(def_conf);
  218. assoc = get_defcfg_association(def_conf);
  219. if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
  220. continue;
  221. hp_out[cfg->hp_outs].pin = nid;
  222. hp_out[cfg->hp_outs].seq = (assoc << 4) | seq;
  223. cfg->hp_outs++;
  224. break;
  225. case AC_JACK_MIC_IN:
  226. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
  227. break;
  228. case AC_JACK_LINE_IN:
  229. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
  230. break;
  231. case AC_JACK_CD:
  232. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
  233. break;
  234. case AC_JACK_AUX:
  235. add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
  236. break;
  237. case AC_JACK_SPDIF_OUT:
  238. case AC_JACK_DIG_OTHER_OUT:
  239. if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
  240. continue;
  241. cfg->dig_out_pins[cfg->dig_outs] = nid;
  242. cfg->dig_out_type[cfg->dig_outs] =
  243. (loc == AC_JACK_LOC_HDMI) ?
  244. HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
  245. cfg->dig_outs++;
  246. break;
  247. case AC_JACK_SPDIF_IN:
  248. case AC_JACK_DIG_OTHER_IN:
  249. cfg->dig_in_pin = nid;
  250. if (loc == AC_JACK_LOC_HDMI)
  251. cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
  252. else
  253. cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
  254. break;
  255. }
  256. }
  257. /* Find a pin that could be a headset or headphone mic */
  258. if (cond_flags & HDA_PINCFG_HEADSET_MIC || cond_flags & HDA_PINCFG_HEADPHONE_MIC) {
  259. bool hsmic = !!(cond_flags & HDA_PINCFG_HEADSET_MIC);
  260. bool hpmic = !!(cond_flags & HDA_PINCFG_HEADPHONE_MIC);
  261. for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++)
  262. if (hsmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xc)) {
  263. cfg->inputs[i].is_headset_mic = 1;
  264. hsmic = false;
  265. } else if (hpmic && can_be_headset_mic(codec, &cfg->inputs[i], 0xd)) {
  266. cfg->inputs[i].is_headphone_mic = 1;
  267. hpmic = false;
  268. }
  269. /* If we didn't find our sequence number mark, fall back to any sequence number */
  270. for (i = 0; (hsmic || hpmic) && (i < cfg->num_inputs); i++) {
  271. if (!can_be_headset_mic(codec, &cfg->inputs[i], -1))
  272. continue;
  273. if (hsmic) {
  274. cfg->inputs[i].is_headset_mic = 1;
  275. hsmic = false;
  276. } else if (hpmic) {
  277. cfg->inputs[i].is_headphone_mic = 1;
  278. hpmic = false;
  279. }
  280. }
  281. if (hsmic)
  282. snd_printdd("Told to look for a headset mic, but didn't find any.\n");
  283. if (hpmic)
  284. snd_printdd("Told to look for a headphone mic, but didn't find any.\n");
  285. }
  286. /* FIX-UP:
  287. * If no line-out is defined but multiple HPs are found,
  288. * some of them might be the real line-outs.
  289. */
  290. if (!cfg->line_outs && cfg->hp_outs > 1 &&
  291. !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
  292. int i = 0;
  293. while (i < cfg->hp_outs) {
  294. /* The real HPs should have the sequence 0x0f */
  295. if ((hp_out[i].seq & 0x0f) == 0x0f) {
  296. i++;
  297. continue;
  298. }
  299. /* Move it to the line-out table */
  300. line_out[cfg->line_outs++] = hp_out[i];
  301. cfg->hp_outs--;
  302. memmove(hp_out + i, hp_out + i + 1,
  303. sizeof(hp_out[0]) * (cfg->hp_outs - i));
  304. }
  305. memset(hp_out + cfg->hp_outs, 0,
  306. sizeof(hp_out[0]) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
  307. if (!cfg->hp_outs)
  308. cfg->line_out_type = AUTO_PIN_HP_OUT;
  309. }
  310. /* sort by sequence */
  311. sort_pins_by_sequence(cfg->line_out_pins, line_out, cfg->line_outs);
  312. sort_pins_by_sequence(cfg->speaker_pins, speaker_out,
  313. cfg->speaker_outs);
  314. sort_pins_by_sequence(cfg->hp_pins, hp_out, cfg->hp_outs);
  315. /*
  316. * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
  317. * as a primary output
  318. */
  319. if (!cfg->line_outs &&
  320. !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
  321. if (cfg->speaker_outs) {
  322. cfg->line_outs = cfg->speaker_outs;
  323. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  324. sizeof(cfg->speaker_pins));
  325. cfg->speaker_outs = 0;
  326. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  327. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  328. } else if (cfg->hp_outs) {
  329. cfg->line_outs = cfg->hp_outs;
  330. memcpy(cfg->line_out_pins, cfg->hp_pins,
  331. sizeof(cfg->hp_pins));
  332. cfg->hp_outs = 0;
  333. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  334. cfg->line_out_type = AUTO_PIN_HP_OUT;
  335. }
  336. }
  337. reorder_outputs(cfg->line_outs, cfg->line_out_pins);
  338. reorder_outputs(cfg->hp_outs, cfg->hp_pins);
  339. reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
  340. /* sort inputs in the order of AUTO_PIN_* type */
  341. sort(cfg->inputs, cfg->num_inputs, sizeof(cfg->inputs[0]),
  342. compare_input_type, NULL);
  343. /*
  344. * debug prints of the parsed results
  345. */
  346. snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
  347. cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
  348. cfg->line_out_pins[2], cfg->line_out_pins[3],
  349. cfg->line_out_pins[4],
  350. cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
  351. (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
  352. "speaker" : "line"));
  353. snd_printd(" speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  354. cfg->speaker_outs, cfg->speaker_pins[0],
  355. cfg->speaker_pins[1], cfg->speaker_pins[2],
  356. cfg->speaker_pins[3], cfg->speaker_pins[4]);
  357. snd_printd(" hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
  358. cfg->hp_outs, cfg->hp_pins[0],
  359. cfg->hp_pins[1], cfg->hp_pins[2],
  360. cfg->hp_pins[3], cfg->hp_pins[4]);
  361. snd_printd(" mono: mono_out=0x%x\n", cfg->mono_out_pin);
  362. if (cfg->dig_outs)
  363. snd_printd(" dig-out=0x%x/0x%x\n",
  364. cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
  365. snd_printd(" inputs:\n");
  366. for (i = 0; i < cfg->num_inputs; i++) {
  367. snd_printd(" %s=0x%x\n",
  368. hda_get_autocfg_input_label(codec, cfg, i),
  369. cfg->inputs[i].pin);
  370. }
  371. if (cfg->dig_in_pin)
  372. snd_printd(" dig-in=0x%x\n", cfg->dig_in_pin);
  373. return 0;
  374. }
  375. EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
  376. int snd_hda_get_input_pin_attr(unsigned int def_conf)
  377. {
  378. unsigned int loc = get_defcfg_location(def_conf);
  379. unsigned int conn = get_defcfg_connect(def_conf);
  380. if (conn == AC_JACK_PORT_NONE)
  381. return INPUT_PIN_ATTR_UNUSED;
  382. /* Windows may claim the internal mic to be BOTH, too */
  383. if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
  384. return INPUT_PIN_ATTR_INT;
  385. if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
  386. return INPUT_PIN_ATTR_INT;
  387. if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
  388. return INPUT_PIN_ATTR_DOCK;
  389. if (loc == AC_JACK_LOC_REAR)
  390. return INPUT_PIN_ATTR_REAR;
  391. if (loc == AC_JACK_LOC_FRONT)
  392. return INPUT_PIN_ATTR_FRONT;
  393. return INPUT_PIN_ATTR_NORMAL;
  394. }
  395. EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
  396. /**
  397. * hda_get_input_pin_label - Give a label for the given input pin
  398. *
  399. * When check_location is true, the function checks the pin location
  400. * for mic and line-in pins, and set an appropriate prefix like "Front",
  401. * "Rear", "Internal".
  402. */
  403. static const char *hda_get_input_pin_label(struct hda_codec *codec,
  404. const struct auto_pin_cfg_item *item,
  405. hda_nid_t pin, bool check_location)
  406. {
  407. unsigned int def_conf;
  408. static const char * const mic_names[] = {
  409. "Internal Mic", "Dock Mic", "Mic", "Rear Mic", "Front Mic"
  410. };
  411. int attr;
  412. def_conf = snd_hda_codec_get_pincfg(codec, pin);
  413. switch (get_defcfg_device(def_conf)) {
  414. case AC_JACK_MIC_IN:
  415. if (item && item->is_headset_mic)
  416. return "Headset Mic";
  417. if (item && item->is_headphone_mic)
  418. return "Headphone Mic";
  419. if (!check_location)
  420. return "Mic";
  421. attr = snd_hda_get_input_pin_attr(def_conf);
  422. if (!attr)
  423. return "None";
  424. return mic_names[attr - 1];
  425. case AC_JACK_LINE_IN:
  426. if (!check_location)
  427. return "Line";
  428. attr = snd_hda_get_input_pin_attr(def_conf);
  429. if (!attr)
  430. return "None";
  431. if (attr == INPUT_PIN_ATTR_DOCK)
  432. return "Dock Line";
  433. return "Line";
  434. case AC_JACK_AUX:
  435. return "Aux";
  436. case AC_JACK_CD:
  437. return "CD";
  438. case AC_JACK_SPDIF_IN:
  439. return "SPDIF In";
  440. case AC_JACK_DIG_OTHER_IN:
  441. return "Digital In";
  442. case AC_JACK_HP_OUT:
  443. return "Headphone Mic";
  444. default:
  445. return "Misc";
  446. }
  447. }
  448. /* Check whether the location prefix needs to be added to the label.
  449. * If all mic-jacks are in the same location (e.g. rear panel), we don't
  450. * have to put "Front" prefix to each label. In such a case, returns false.
  451. */
  452. static int check_mic_location_need(struct hda_codec *codec,
  453. const struct auto_pin_cfg *cfg,
  454. int input)
  455. {
  456. unsigned int defc;
  457. int i, attr, attr2;
  458. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
  459. attr = snd_hda_get_input_pin_attr(defc);
  460. /* for internal or docking mics, we need locations */
  461. if (attr <= INPUT_PIN_ATTR_NORMAL)
  462. return 1;
  463. attr = 0;
  464. for (i = 0; i < cfg->num_inputs; i++) {
  465. defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
  466. attr2 = snd_hda_get_input_pin_attr(defc);
  467. if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
  468. if (attr && attr != attr2)
  469. return 1; /* different locations found */
  470. attr = attr2;
  471. }
  472. }
  473. return 0;
  474. }
  475. /**
  476. * hda_get_autocfg_input_label - Get a label for the given input
  477. *
  478. * Get a label for the given input pin defined by the autocfg item.
  479. * Unlike hda_get_input_pin_label(), this function checks all inputs
  480. * defined in autocfg and avoids the redundant mic/line prefix as much as
  481. * possible.
  482. */
  483. const char *hda_get_autocfg_input_label(struct hda_codec *codec,
  484. const struct auto_pin_cfg *cfg,
  485. int input)
  486. {
  487. int type = cfg->inputs[input].type;
  488. int has_multiple_pins = 0;
  489. if ((input > 0 && cfg->inputs[input - 1].type == type) ||
  490. (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
  491. has_multiple_pins = 1;
  492. if (has_multiple_pins && type == AUTO_PIN_MIC)
  493. has_multiple_pins &= check_mic_location_need(codec, cfg, input);
  494. return hda_get_input_pin_label(codec, &cfg->inputs[input],
  495. cfg->inputs[input].pin,
  496. has_multiple_pins);
  497. }
  498. EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
  499. /* return the position of NID in the list, or -1 if not found */
  500. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  501. {
  502. int i;
  503. for (i = 0; i < nums; i++)
  504. if (list[i] == nid)
  505. return i;
  506. return -1;
  507. }
  508. /* get a unique suffix or an index number */
  509. static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
  510. int num_pins, int *indexp)
  511. {
  512. static const char * const channel_sfx[] = {
  513. " Front", " Surround", " CLFE", " Side"
  514. };
  515. int i;
  516. i = find_idx_in_nid_list(nid, pins, num_pins);
  517. if (i < 0)
  518. return NULL;
  519. if (num_pins == 1)
  520. return "";
  521. if (num_pins > ARRAY_SIZE(channel_sfx)) {
  522. if (indexp)
  523. *indexp = i;
  524. return "";
  525. }
  526. return channel_sfx[i];
  527. }
  528. static const char *check_output_pfx(struct hda_codec *codec, hda_nid_t nid)
  529. {
  530. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  531. int attr = snd_hda_get_input_pin_attr(def_conf);
  532. /* check the location */
  533. switch (attr) {
  534. case INPUT_PIN_ATTR_DOCK:
  535. return "Dock ";
  536. case INPUT_PIN_ATTR_FRONT:
  537. return "Front ";
  538. }
  539. return "";
  540. }
  541. static int get_hp_label_index(struct hda_codec *codec, hda_nid_t nid,
  542. const hda_nid_t *pins, int num_pins)
  543. {
  544. int i, j, idx = 0;
  545. const char *pfx = check_output_pfx(codec, nid);
  546. i = find_idx_in_nid_list(nid, pins, num_pins);
  547. if (i < 0)
  548. return -1;
  549. for (j = 0; j < i; j++)
  550. if (pfx == check_output_pfx(codec, pins[j]))
  551. idx++;
  552. return idx;
  553. }
  554. static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
  555. const struct auto_pin_cfg *cfg,
  556. const char *name, char *label, int maxlen,
  557. int *indexp)
  558. {
  559. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  560. int attr = snd_hda_get_input_pin_attr(def_conf);
  561. const char *pfx, *sfx = "";
  562. /* handle as a speaker if it's a fixed line-out */
  563. if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
  564. name = "Speaker";
  565. pfx = check_output_pfx(codec, nid);
  566. if (cfg) {
  567. /* try to give a unique suffix if needed */
  568. sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
  569. indexp);
  570. if (!sfx)
  571. sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
  572. indexp);
  573. if (!sfx) {
  574. /* don't add channel suffix for Headphone controls */
  575. int idx = get_hp_label_index(codec, nid, cfg->hp_pins,
  576. cfg->hp_outs);
  577. if (idx >= 0)
  578. *indexp = idx;
  579. sfx = "";
  580. }
  581. }
  582. snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
  583. return 1;
  584. }
  585. #define is_hdmi_cfg(conf) \
  586. (get_defcfg_location(conf) == AC_JACK_LOC_HDMI)
  587. /**
  588. * snd_hda_get_pin_label - Get a label for the given I/O pin
  589. *
  590. * Get a label for the given pin. This function works for both input and
  591. * output pins. When @cfg is given as non-NULL, the function tries to get
  592. * an optimized label using hda_get_autocfg_input_label().
  593. *
  594. * This function tries to give a unique label string for the pin as much as
  595. * possible. For example, when the multiple line-outs are present, it adds
  596. * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
  597. * If no unique name with a suffix is available and @indexp is non-NULL, the
  598. * index number is stored in the pointer.
  599. */
  600. int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
  601. const struct auto_pin_cfg *cfg,
  602. char *label, int maxlen, int *indexp)
  603. {
  604. unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
  605. const char *name = NULL;
  606. int i;
  607. bool hdmi;
  608. if (indexp)
  609. *indexp = 0;
  610. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
  611. return 0;
  612. switch (get_defcfg_device(def_conf)) {
  613. case AC_JACK_LINE_OUT:
  614. return fill_audio_out_name(codec, nid, cfg, "Line Out",
  615. label, maxlen, indexp);
  616. case AC_JACK_SPEAKER:
  617. return fill_audio_out_name(codec, nid, cfg, "Speaker",
  618. label, maxlen, indexp);
  619. case AC_JACK_HP_OUT:
  620. return fill_audio_out_name(codec, nid, cfg, "Headphone",
  621. label, maxlen, indexp);
  622. case AC_JACK_SPDIF_OUT:
  623. case AC_JACK_DIG_OTHER_OUT:
  624. hdmi = is_hdmi_cfg(def_conf);
  625. name = hdmi ? "HDMI" : "SPDIF";
  626. if (cfg && indexp)
  627. for (i = 0; i < cfg->dig_outs; i++) {
  628. hda_nid_t pin = cfg->dig_out_pins[i];
  629. unsigned int c;
  630. if (pin == nid)
  631. break;
  632. c = snd_hda_codec_get_pincfg(codec, pin);
  633. if (hdmi == is_hdmi_cfg(c))
  634. (*indexp)++;
  635. }
  636. break;
  637. default:
  638. if (cfg) {
  639. for (i = 0; i < cfg->num_inputs; i++) {
  640. if (cfg->inputs[i].pin != nid)
  641. continue;
  642. name = hda_get_autocfg_input_label(codec, cfg, i);
  643. if (name)
  644. break;
  645. }
  646. }
  647. if (!name)
  648. name = hda_get_input_pin_label(codec, NULL, nid, true);
  649. break;
  650. }
  651. if (!name)
  652. return 0;
  653. strlcpy(label, name, maxlen);
  654. return 1;
  655. }
  656. EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
  657. int snd_hda_add_verbs(struct hda_codec *codec,
  658. const struct hda_verb *list)
  659. {
  660. const struct hda_verb **v;
  661. v = snd_array_new(&codec->verbs);
  662. if (!v)
  663. return -ENOMEM;
  664. *v = list;
  665. return 0;
  666. }
  667. EXPORT_SYMBOL_HDA(snd_hda_add_verbs);
  668. void snd_hda_apply_verbs(struct hda_codec *codec)
  669. {
  670. int i;
  671. for (i = 0; i < codec->verbs.used; i++) {
  672. struct hda_verb **v = snd_array_elem(&codec->verbs, i);
  673. snd_hda_sequence_write(codec, *v);
  674. }
  675. }
  676. EXPORT_SYMBOL_HDA(snd_hda_apply_verbs);
  677. void snd_hda_apply_pincfgs(struct hda_codec *codec,
  678. const struct hda_pintbl *cfg)
  679. {
  680. for (; cfg->nid; cfg++)
  681. snd_hda_codec_set_pincfg(codec, cfg->nid, cfg->val);
  682. }
  683. EXPORT_SYMBOL_HDA(snd_hda_apply_pincfgs);
  684. static void set_pin_targets(struct hda_codec *codec,
  685. const struct hda_pintbl *cfg)
  686. {
  687. for (; cfg->nid; cfg++)
  688. snd_hda_set_pin_ctl_cache(codec, cfg->nid, cfg->val);
  689. }
  690. static void apply_fixup(struct hda_codec *codec, int id, int action, int depth)
  691. {
  692. const char *modelname = codec->fixup_name;
  693. while (id >= 0) {
  694. const struct hda_fixup *fix = codec->fixup_list + id;
  695. if (fix->chained_before)
  696. apply_fixup(codec, fix->chain_id, action, depth + 1);
  697. switch (fix->type) {
  698. case HDA_FIXUP_PINS:
  699. if (action != HDA_FIXUP_ACT_PRE_PROBE || !fix->v.pins)
  700. break;
  701. snd_printdd(KERN_INFO SFX
  702. "%s: Apply pincfg for %s\n",
  703. codec->chip_name, modelname);
  704. snd_hda_apply_pincfgs(codec, fix->v.pins);
  705. break;
  706. case HDA_FIXUP_VERBS:
  707. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.verbs)
  708. break;
  709. snd_printdd(KERN_INFO SFX
  710. "%s: Apply fix-verbs for %s\n",
  711. codec->chip_name, modelname);
  712. snd_hda_add_verbs(codec, fix->v.verbs);
  713. break;
  714. case HDA_FIXUP_FUNC:
  715. if (!fix->v.func)
  716. break;
  717. snd_printdd(KERN_INFO SFX
  718. "%s: Apply fix-func for %s\n",
  719. codec->chip_name, modelname);
  720. fix->v.func(codec, fix, action);
  721. break;
  722. case HDA_FIXUP_PINCTLS:
  723. if (action != HDA_FIXUP_ACT_PROBE || !fix->v.pins)
  724. break;
  725. snd_printdd(KERN_INFO SFX
  726. "%s: Apply pinctl for %s\n",
  727. codec->chip_name, modelname);
  728. set_pin_targets(codec, fix->v.pins);
  729. break;
  730. default:
  731. snd_printk(KERN_ERR SFX
  732. "%s: Invalid fixup type %d\n",
  733. codec->chip_name, fix->type);
  734. break;
  735. }
  736. if (!fix->chained || fix->chained_before)
  737. break;
  738. if (++depth > 10)
  739. break;
  740. id = fix->chain_id;
  741. }
  742. }
  743. void snd_hda_apply_fixup(struct hda_codec *codec, int action)
  744. {
  745. if (codec->fixup_list)
  746. apply_fixup(codec, codec->fixup_id, action, 0);
  747. }
  748. EXPORT_SYMBOL_HDA(snd_hda_apply_fixup);
  749. void snd_hda_pick_fixup(struct hda_codec *codec,
  750. const struct hda_model_fixup *models,
  751. const struct snd_pci_quirk *quirk,
  752. const struct hda_fixup *fixlist)
  753. {
  754. const struct snd_pci_quirk *q;
  755. int id = -1;
  756. const char *name = NULL;
  757. /* when model=nofixup is given, don't pick up any fixups */
  758. if (codec->modelname && !strcmp(codec->modelname, "nofixup")) {
  759. codec->fixup_list = NULL;
  760. codec->fixup_id = -1;
  761. return;
  762. }
  763. if (codec->modelname && models) {
  764. while (models->name) {
  765. if (!strcmp(codec->modelname, models->name)) {
  766. id = models->id;
  767. name = models->name;
  768. break;
  769. }
  770. models++;
  771. }
  772. }
  773. if (id < 0 && quirk) {
  774. q = snd_pci_quirk_lookup(codec->bus->pci, quirk);
  775. if (q) {
  776. id = q->value;
  777. #ifdef CONFIG_SND_DEBUG_VERBOSE
  778. name = q->name;
  779. #endif
  780. }
  781. }
  782. if (id < 0 && quirk) {
  783. for (q = quirk; q->subvendor || q->subdevice; q++) {
  784. unsigned int vendorid =
  785. q->subdevice | (q->subvendor << 16);
  786. unsigned int mask = 0xffff0000 | q->subdevice_mask;
  787. if ((codec->subsystem_id & mask) == (vendorid & mask)) {
  788. id = q->value;
  789. #ifdef CONFIG_SND_DEBUG_VERBOSE
  790. name = q->name;
  791. #endif
  792. break;
  793. }
  794. }
  795. }
  796. codec->fixup_id = id;
  797. if (id >= 0) {
  798. codec->fixup_list = fixlist;
  799. codec->fixup_name = name;
  800. }
  801. }
  802. EXPORT_SYMBOL_HDA(snd_hda_pick_fixup);