hda_generic.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Generic widget tree parser
  5. *
  6. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This driver is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This driver is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <sound/core.h>
  26. #include "hda_codec.h"
  27. #include "hda_local.h"
  28. /* widget node for parsing */
  29. struct hda_gnode {
  30. hda_nid_t nid; /* NID of this widget */
  31. unsigned short nconns; /* number of input connections */
  32. hda_nid_t *conn_list;
  33. hda_nid_t slist[2]; /* temporay list */
  34. unsigned int wid_caps; /* widget capabilities */
  35. unsigned char type; /* widget type */
  36. unsigned char pin_ctl; /* pin controls */
  37. unsigned char checked; /* the flag indicates that the node is already parsed */
  38. unsigned int pin_caps; /* pin widget capabilities */
  39. unsigned int def_cfg; /* default configuration */
  40. unsigned int amp_out_caps; /* AMP out capabilities */
  41. unsigned int amp_in_caps; /* AMP in capabilities */
  42. struct list_head list;
  43. };
  44. /* patch-specific record */
  45. #define MAX_PCM_VOLS 2
  46. struct pcm_vol {
  47. struct hda_gnode *node; /* Node for PCM volume */
  48. unsigned int index; /* connection of PCM volume */
  49. };
  50. struct hda_gspec {
  51. struct hda_gnode *dac_node[2]; /* DAC node */
  52. struct hda_gnode *out_pin_node[2]; /* Output pin (Line-Out) node */
  53. struct pcm_vol pcm_vol[MAX_PCM_VOLS]; /* PCM volumes */
  54. unsigned int pcm_vol_nodes; /* number of PCM volumes */
  55. struct hda_gnode *adc_node; /* ADC node */
  56. struct hda_gnode *cap_vol_node; /* Node for capture volume */
  57. unsigned int cur_cap_src; /* current capture source */
  58. struct hda_input_mux input_mux;
  59. char cap_labels[HDA_MAX_NUM_INPUTS][16];
  60. unsigned int def_amp_in_caps;
  61. unsigned int def_amp_out_caps;
  62. struct hda_pcm pcm_rec; /* PCM information */
  63. struct list_head nid_list; /* list of widgets */
  64. };
  65. /*
  66. * retrieve the default device type from the default config value
  67. */
  68. #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> \
  69. AC_DEFCFG_DEVICE_SHIFT)
  70. #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> \
  71. AC_DEFCFG_LOCATION_SHIFT)
  72. #define defcfg_port_conn(node) (((node)->def_cfg & AC_DEFCFG_PORT_CONN) >> \
  73. AC_DEFCFG_PORT_CONN_SHIFT)
  74. /*
  75. * destructor
  76. */
  77. static void snd_hda_generic_free(struct hda_codec *codec)
  78. {
  79. struct hda_gspec *spec = codec->spec;
  80. struct list_head *p, *n;
  81. if (! spec)
  82. return;
  83. /* free all widgets */
  84. list_for_each_safe(p, n, &spec->nid_list) {
  85. struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
  86. if (node->conn_list != node->slist)
  87. kfree(node->conn_list);
  88. kfree(node);
  89. }
  90. kfree(spec);
  91. }
  92. /*
  93. * add a new widget node and read its attributes
  94. */
  95. static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
  96. {
  97. struct hda_gnode *node;
  98. int nconns;
  99. hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
  100. node = kzalloc(sizeof(*node), GFP_KERNEL);
  101. if (node == NULL)
  102. return -ENOMEM;
  103. node->nid = nid;
  104. nconns = snd_hda_get_connections(codec, nid, conn_list,
  105. HDA_MAX_CONNECTIONS);
  106. if (nconns < 0) {
  107. kfree(node);
  108. return nconns;
  109. }
  110. if (nconns <= ARRAY_SIZE(node->slist))
  111. node->conn_list = node->slist;
  112. else {
  113. node->conn_list = kmalloc(sizeof(hda_nid_t) * nconns,
  114. GFP_KERNEL);
  115. if (! node->conn_list) {
  116. snd_printk(KERN_ERR "hda-generic: cannot malloc\n");
  117. kfree(node);
  118. return -ENOMEM;
  119. }
  120. }
  121. memcpy(node->conn_list, conn_list, nconns * sizeof(hda_nid_t));
  122. node->nconns = nconns;
  123. node->wid_caps = get_wcaps(codec, nid);
  124. node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  125. if (node->type == AC_WID_PIN) {
  126. node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
  127. node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  128. node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  129. }
  130. if (node->wid_caps & AC_WCAP_OUT_AMP) {
  131. if (node->wid_caps & AC_WCAP_AMP_OVRD)
  132. node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
  133. if (! node->amp_out_caps)
  134. node->amp_out_caps = spec->def_amp_out_caps;
  135. }
  136. if (node->wid_caps & AC_WCAP_IN_AMP) {
  137. if (node->wid_caps & AC_WCAP_AMP_OVRD)
  138. node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
  139. if (! node->amp_in_caps)
  140. node->amp_in_caps = spec->def_amp_in_caps;
  141. }
  142. list_add_tail(&node->list, &spec->nid_list);
  143. return 0;
  144. }
  145. /*
  146. * build the AFG subtree
  147. */
  148. static int build_afg_tree(struct hda_codec *codec)
  149. {
  150. struct hda_gspec *spec = codec->spec;
  151. int i, nodes, err;
  152. hda_nid_t nid;
  153. snd_assert(spec, return -EINVAL);
  154. spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
  155. spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
  156. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  157. if (! nid || nodes < 0) {
  158. printk(KERN_ERR "Invalid AFG subtree\n");
  159. return -EINVAL;
  160. }
  161. /* parse all nodes belonging to the AFG */
  162. for (i = 0; i < nodes; i++, nid++) {
  163. if ((err = add_new_node(codec, spec, nid)) < 0)
  164. return err;
  165. }
  166. return 0;
  167. }
  168. /*
  169. * look for the node record for the given NID
  170. */
  171. /* FIXME: should avoid the braindead linear search */
  172. static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
  173. {
  174. struct list_head *p;
  175. struct hda_gnode *node;
  176. list_for_each(p, &spec->nid_list) {
  177. node = list_entry(p, struct hda_gnode, list);
  178. if (node->nid == nid)
  179. return node;
  180. }
  181. return NULL;
  182. }
  183. /*
  184. * unmute (and set max vol) the output amplifier
  185. */
  186. static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
  187. {
  188. unsigned int val, ofs;
  189. snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
  190. val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  191. ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  192. if (val >= ofs)
  193. val -= ofs;
  194. snd_hda_codec_amp_update(codec, node->nid, 0, HDA_OUTPUT, 0, 0xff, val);
  195. snd_hda_codec_amp_update(codec, node->nid, 0, HDA_OUTPUT, 1, 0xff, val);
  196. return 0;
  197. }
  198. /*
  199. * unmute (and set max vol) the input amplifier
  200. */
  201. static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
  202. {
  203. unsigned int val, ofs;
  204. snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
  205. val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  206. ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  207. if (val >= ofs)
  208. val -= ofs;
  209. snd_hda_codec_amp_update(codec, node->nid, 0, HDA_INPUT, index,
  210. 0xff, val);
  211. snd_hda_codec_amp_update(codec, node->nid, 1, HDA_INPUT, index,
  212. 0xff, val);
  213. return 0;
  214. }
  215. /*
  216. * select the input connection of the given node.
  217. */
  218. static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
  219. unsigned int index)
  220. {
  221. snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
  222. return snd_hda_codec_write_cache(codec, node->nid, 0,
  223. AC_VERB_SET_CONNECT_SEL, index);
  224. }
  225. /*
  226. * clear checked flag of each node in the node list
  227. */
  228. static void clear_check_flags(struct hda_gspec *spec)
  229. {
  230. struct list_head *p;
  231. struct hda_gnode *node;
  232. list_for_each(p, &spec->nid_list) {
  233. node = list_entry(p, struct hda_gnode, list);
  234. node->checked = 0;
  235. }
  236. }
  237. /*
  238. * parse the output path recursively until reach to an audio output widget
  239. *
  240. * returns 0 if not found, 1 if found, or a negative error code.
  241. */
  242. static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
  243. struct hda_gnode *node, int dac_idx)
  244. {
  245. int i, err;
  246. struct hda_gnode *child;
  247. if (node->checked)
  248. return 0;
  249. node->checked = 1;
  250. if (node->type == AC_WID_AUD_OUT) {
  251. if (node->wid_caps & AC_WCAP_DIGITAL) {
  252. snd_printdd("Skip Digital OUT node %x\n", node->nid);
  253. return 0;
  254. }
  255. snd_printdd("AUD_OUT found %x\n", node->nid);
  256. if (spec->dac_node[dac_idx]) {
  257. /* already DAC node is assigned, just unmute & connect */
  258. return node == spec->dac_node[dac_idx];
  259. }
  260. spec->dac_node[dac_idx] = node;
  261. if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  262. spec->pcm_vol_nodes < MAX_PCM_VOLS) {
  263. spec->pcm_vol[spec->pcm_vol_nodes].node = node;
  264. spec->pcm_vol[spec->pcm_vol_nodes].index = 0;
  265. spec->pcm_vol_nodes++;
  266. }
  267. return 1; /* found */
  268. }
  269. for (i = 0; i < node->nconns; i++) {
  270. child = hda_get_node(spec, node->conn_list[i]);
  271. if (! child)
  272. continue;
  273. err = parse_output_path(codec, spec, child, dac_idx);
  274. if (err < 0)
  275. return err;
  276. else if (err > 0) {
  277. /* found one,
  278. * select the path, unmute both input and output
  279. */
  280. if (node->nconns > 1)
  281. select_input_connection(codec, node, i);
  282. unmute_input(codec, node, i);
  283. unmute_output(codec, node);
  284. if (spec->dac_node[dac_idx] &&
  285. spec->pcm_vol_nodes < MAX_PCM_VOLS &&
  286. !(spec->dac_node[dac_idx]->wid_caps &
  287. AC_WCAP_OUT_AMP)) {
  288. if ((node->wid_caps & AC_WCAP_IN_AMP) ||
  289. (node->wid_caps & AC_WCAP_OUT_AMP)) {
  290. int n = spec->pcm_vol_nodes;
  291. spec->pcm_vol[n].node = node;
  292. spec->pcm_vol[n].index = i;
  293. spec->pcm_vol_nodes++;
  294. }
  295. }
  296. return 1;
  297. }
  298. }
  299. return 0;
  300. }
  301. /*
  302. * Look for the output PIN widget with the given jack type
  303. * and parse the output path to that PIN.
  304. *
  305. * Returns the PIN node when the path to DAC is established.
  306. */
  307. static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
  308. struct hda_gspec *spec,
  309. int jack_type)
  310. {
  311. struct list_head *p;
  312. struct hda_gnode *node;
  313. int err;
  314. list_for_each(p, &spec->nid_list) {
  315. node = list_entry(p, struct hda_gnode, list);
  316. if (node->type != AC_WID_PIN)
  317. continue;
  318. /* output capable? */
  319. if (! (node->pin_caps & AC_PINCAP_OUT))
  320. continue;
  321. if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
  322. continue; /* unconnected */
  323. if (jack_type >= 0) {
  324. if (jack_type != defcfg_type(node))
  325. continue;
  326. if (node->wid_caps & AC_WCAP_DIGITAL)
  327. continue; /* skip SPDIF */
  328. } else {
  329. /* output as default? */
  330. if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
  331. continue;
  332. }
  333. clear_check_flags(spec);
  334. err = parse_output_path(codec, spec, node, 0);
  335. if (err < 0)
  336. return NULL;
  337. if (! err && spec->out_pin_node[0]) {
  338. err = parse_output_path(codec, spec, node, 1);
  339. if (err < 0)
  340. return NULL;
  341. }
  342. if (err > 0) {
  343. /* unmute the PIN output */
  344. unmute_output(codec, node);
  345. /* set PIN-Out enable */
  346. snd_hda_codec_write_cache(codec, node->nid, 0,
  347. AC_VERB_SET_PIN_WIDGET_CONTROL,
  348. AC_PINCTL_OUT_EN |
  349. ((node->pin_caps & AC_PINCAP_HP_DRV) ?
  350. AC_PINCTL_HP_EN : 0));
  351. return node;
  352. }
  353. }
  354. return NULL;
  355. }
  356. /*
  357. * parse outputs
  358. */
  359. static int parse_output(struct hda_codec *codec)
  360. {
  361. struct hda_gspec *spec = codec->spec;
  362. struct hda_gnode *node;
  363. /*
  364. * Look for the output PIN widget
  365. */
  366. /* first, look for the line-out pin */
  367. node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
  368. if (node) /* found, remember the PIN node */
  369. spec->out_pin_node[0] = node;
  370. else {
  371. /* if no line-out is found, try speaker out */
  372. node = parse_output_jack(codec, spec, AC_JACK_SPEAKER);
  373. if (node)
  374. spec->out_pin_node[0] = node;
  375. }
  376. /* look for the HP-out pin */
  377. node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
  378. if (node) {
  379. if (! spec->out_pin_node[0])
  380. spec->out_pin_node[0] = node;
  381. else
  382. spec->out_pin_node[1] = node;
  383. }
  384. if (! spec->out_pin_node[0]) {
  385. /* no line-out or HP pins found,
  386. * then choose for the first output pin
  387. */
  388. spec->out_pin_node[0] = parse_output_jack(codec, spec, -1);
  389. if (! spec->out_pin_node[0])
  390. snd_printd("hda_generic: no proper output path found\n");
  391. }
  392. return 0;
  393. }
  394. /*
  395. * input MUX
  396. */
  397. /* control callbacks */
  398. static int capture_source_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  399. {
  400. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  401. struct hda_gspec *spec = codec->spec;
  402. return snd_hda_input_mux_info(&spec->input_mux, uinfo);
  403. }
  404. static int capture_source_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  405. {
  406. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  407. struct hda_gspec *spec = codec->spec;
  408. ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
  409. return 0;
  410. }
  411. static int capture_source_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  412. {
  413. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  414. struct hda_gspec *spec = codec->spec;
  415. return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
  416. spec->adc_node->nid, &spec->cur_cap_src);
  417. }
  418. /*
  419. * return the string name of the given input PIN widget
  420. */
  421. static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
  422. {
  423. unsigned int location = defcfg_location(node);
  424. switch (defcfg_type(node)) {
  425. case AC_JACK_LINE_IN:
  426. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  427. return "Front Line";
  428. return "Line";
  429. case AC_JACK_CD:
  430. #if 0
  431. if (pinctl)
  432. *pinctl |= AC_PINCTL_VREF_GRD;
  433. #endif
  434. return "CD";
  435. case AC_JACK_AUX:
  436. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  437. return "Front Aux";
  438. return "Aux";
  439. case AC_JACK_MIC_IN:
  440. if (pinctl &&
  441. (node->pin_caps &
  442. (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT)))
  443. *pinctl |= AC_PINCTL_VREF_80;
  444. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  445. return "Front Mic";
  446. return "Mic";
  447. case AC_JACK_SPDIF_IN:
  448. return "SPDIF";
  449. case AC_JACK_DIG_OTHER_IN:
  450. return "Digital";
  451. }
  452. return NULL;
  453. }
  454. /*
  455. * parse the nodes recursively until reach to the input PIN
  456. *
  457. * returns 0 if not found, 1 if found, or a negative error code.
  458. */
  459. static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
  460. struct hda_gnode *node)
  461. {
  462. int i, err;
  463. unsigned int pinctl;
  464. char *label;
  465. const char *type;
  466. if (node->checked)
  467. return 0;
  468. node->checked = 1;
  469. if (node->type != AC_WID_PIN) {
  470. for (i = 0; i < node->nconns; i++) {
  471. struct hda_gnode *child;
  472. child = hda_get_node(spec, node->conn_list[i]);
  473. if (! child)
  474. continue;
  475. err = parse_adc_sub_nodes(codec, spec, child);
  476. if (err < 0)
  477. return err;
  478. if (err > 0) {
  479. /* found one,
  480. * select the path, unmute both input and output
  481. */
  482. if (node->nconns > 1)
  483. select_input_connection(codec, node, i);
  484. unmute_input(codec, node, i);
  485. unmute_output(codec, node);
  486. return err;
  487. }
  488. }
  489. return 0;
  490. }
  491. /* input capable? */
  492. if (! (node->pin_caps & AC_PINCAP_IN))
  493. return 0;
  494. if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
  495. return 0; /* unconnected */
  496. if (node->wid_caps & AC_WCAP_DIGITAL)
  497. return 0; /* skip SPDIF */
  498. if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
  499. snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
  500. return -EINVAL;
  501. }
  502. pinctl = AC_PINCTL_IN_EN;
  503. /* create a proper capture source label */
  504. type = get_input_type(node, &pinctl);
  505. if (! type) {
  506. /* input as default? */
  507. if (! (node->pin_ctl & AC_PINCTL_IN_EN))
  508. return 0;
  509. type = "Input";
  510. }
  511. label = spec->cap_labels[spec->input_mux.num_items];
  512. strcpy(label, type);
  513. spec->input_mux.items[spec->input_mux.num_items].label = label;
  514. /* unmute the PIN external input */
  515. unmute_input(codec, node, 0); /* index = 0? */
  516. /* set PIN-In enable */
  517. snd_hda_codec_write_cache(codec, node->nid, 0,
  518. AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
  519. return 1; /* found */
  520. }
  521. /* add a capture source element */
  522. static void add_cap_src(struct hda_gspec *spec, int idx)
  523. {
  524. struct hda_input_mux_item *csrc;
  525. char *buf;
  526. int num, ocap;
  527. num = spec->input_mux.num_items;
  528. csrc = &spec->input_mux.items[num];
  529. buf = spec->cap_labels[num];
  530. for (ocap = 0; ocap < num; ocap++) {
  531. if (! strcmp(buf, spec->cap_labels[ocap])) {
  532. /* same label already exists,
  533. * put the index number to be unique
  534. */
  535. sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
  536. break;
  537. }
  538. }
  539. csrc->index = idx;
  540. spec->input_mux.num_items++;
  541. }
  542. /*
  543. * parse input
  544. */
  545. static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
  546. {
  547. struct hda_gspec *spec = codec->spec;
  548. struct hda_gnode *node;
  549. int i, err;
  550. snd_printdd("AUD_IN = %x\n", adc_node->nid);
  551. clear_check_flags(spec);
  552. // awk added - fixed no recording due to muted widget
  553. unmute_input(codec, adc_node, 0);
  554. /*
  555. * check each connection of the ADC
  556. * if it reaches to a proper input PIN, add the path as the
  557. * input path.
  558. */
  559. /* first, check the direct connections to PIN widgets */
  560. for (i = 0; i < adc_node->nconns; i++) {
  561. node = hda_get_node(spec, adc_node->conn_list[i]);
  562. if (node && node->type == AC_WID_PIN) {
  563. err = parse_adc_sub_nodes(codec, spec, node);
  564. if (err < 0)
  565. return err;
  566. else if (err > 0)
  567. add_cap_src(spec, i);
  568. }
  569. }
  570. /* ... then check the rests, more complicated connections */
  571. for (i = 0; i < adc_node->nconns; i++) {
  572. node = hda_get_node(spec, adc_node->conn_list[i]);
  573. if (node && node->type != AC_WID_PIN) {
  574. err = parse_adc_sub_nodes(codec, spec, node);
  575. if (err < 0)
  576. return err;
  577. else if (err > 0)
  578. add_cap_src(spec, i);
  579. }
  580. }
  581. if (! spec->input_mux.num_items)
  582. return 0; /* no input path found... */
  583. snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
  584. for (i = 0; i < spec->input_mux.num_items; i++)
  585. snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
  586. spec->input_mux.items[i].index);
  587. spec->adc_node = adc_node;
  588. return 1;
  589. }
  590. /*
  591. * parse input
  592. */
  593. static int parse_input(struct hda_codec *codec)
  594. {
  595. struct hda_gspec *spec = codec->spec;
  596. struct list_head *p;
  597. struct hda_gnode *node;
  598. int err;
  599. /*
  600. * At first we look for an audio input widget.
  601. * If it reaches to certain input PINs, we take it as the
  602. * input path.
  603. */
  604. list_for_each(p, &spec->nid_list) {
  605. node = list_entry(p, struct hda_gnode, list);
  606. if (node->wid_caps & AC_WCAP_DIGITAL)
  607. continue; /* skip SPDIF */
  608. if (node->type == AC_WID_AUD_IN) {
  609. err = parse_input_path(codec, node);
  610. if (err < 0)
  611. return err;
  612. else if (err > 0)
  613. return 0;
  614. }
  615. }
  616. snd_printd("hda_generic: no proper input path found\n");
  617. return 0;
  618. }
  619. /*
  620. * create mixer controls if possible
  621. */
  622. static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
  623. unsigned int index, const char *type, const char *dir_sfx)
  624. {
  625. char name[32];
  626. int err;
  627. int created = 0;
  628. struct snd_kcontrol_new knew;
  629. if (type)
  630. sprintf(name, "%s %s Switch", type, dir_sfx);
  631. else
  632. sprintf(name, "%s Switch", dir_sfx);
  633. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  634. (node->amp_in_caps & AC_AMPCAP_MUTE)) {
  635. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
  636. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  637. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  638. return err;
  639. created = 1;
  640. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  641. (node->amp_out_caps & AC_AMPCAP_MUTE)) {
  642. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
  643. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  644. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  645. return err;
  646. created = 1;
  647. }
  648. if (type)
  649. sprintf(name, "%s %s Volume", type, dir_sfx);
  650. else
  651. sprintf(name, "%s Volume", dir_sfx);
  652. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  653. (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
  654. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
  655. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  656. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  657. return err;
  658. created = 1;
  659. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  660. (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
  661. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
  662. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  663. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  664. return err;
  665. created = 1;
  666. }
  667. return created;
  668. }
  669. /*
  670. * check whether the controls with the given name and direction suffix already exist
  671. */
  672. static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
  673. {
  674. struct snd_ctl_elem_id id;
  675. memset(&id, 0, sizeof(id));
  676. sprintf(id.name, "%s %s Volume", type, dir);
  677. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  678. if (snd_ctl_find_id(codec->bus->card, &id))
  679. return 1;
  680. sprintf(id.name, "%s %s Switch", type, dir);
  681. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  682. if (snd_ctl_find_id(codec->bus->card, &id))
  683. return 1;
  684. return 0;
  685. }
  686. /*
  687. * build output mixer controls
  688. */
  689. static int create_output_mixers(struct hda_codec *codec, const char **names)
  690. {
  691. struct hda_gspec *spec = codec->spec;
  692. int i, err;
  693. for (i = 0; i < spec->pcm_vol_nodes; i++) {
  694. err = create_mixer(codec, spec->pcm_vol[i].node,
  695. spec->pcm_vol[i].index,
  696. names[i], "Playback");
  697. if (err < 0)
  698. return err;
  699. }
  700. return 0;
  701. }
  702. static int build_output_controls(struct hda_codec *codec)
  703. {
  704. struct hda_gspec *spec = codec->spec;
  705. static const char *types_speaker[] = { "Speaker", "Headphone" };
  706. static const char *types_line[] = { "Front", "Headphone" };
  707. switch (spec->pcm_vol_nodes) {
  708. case 1:
  709. return create_mixer(codec, spec->pcm_vol[0].node,
  710. spec->pcm_vol[0].index,
  711. "Master", "Playback");
  712. case 2:
  713. if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
  714. return create_output_mixers(codec, types_speaker);
  715. else
  716. return create_output_mixers(codec, types_line);
  717. }
  718. return 0;
  719. }
  720. /* create capture volume/switch */
  721. static int build_input_controls(struct hda_codec *codec)
  722. {
  723. struct hda_gspec *spec = codec->spec;
  724. struct hda_gnode *adc_node = spec->adc_node;
  725. int i, err;
  726. static struct snd_kcontrol_new cap_sel = {
  727. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  728. .name = "Capture Source",
  729. .info = capture_source_info,
  730. .get = capture_source_get,
  731. .put = capture_source_put,
  732. };
  733. if (! adc_node || ! spec->input_mux.num_items)
  734. return 0; /* not found */
  735. spec->cur_cap_src = 0;
  736. select_input_connection(codec, adc_node,
  737. spec->input_mux.items[0].index);
  738. /* create capture volume and switch controls if the ADC has an amp */
  739. /* do we have only a single item? */
  740. if (spec->input_mux.num_items == 1) {
  741. err = create_mixer(codec, adc_node,
  742. spec->input_mux.items[0].index,
  743. NULL, "Capture");
  744. if (err < 0)
  745. return err;
  746. return 0;
  747. }
  748. /* create input MUX if multiple sources are available */
  749. if ((err = snd_ctl_add(codec->bus->card,
  750. snd_ctl_new1(&cap_sel, codec))) < 0)
  751. return err;
  752. /* no volume control? */
  753. if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
  754. ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
  755. return 0;
  756. for (i = 0; i < spec->input_mux.num_items; i++) {
  757. struct snd_kcontrol_new knew;
  758. char name[32];
  759. sprintf(name, "%s Capture Volume",
  760. spec->input_mux.items[i].label);
  761. knew = (struct snd_kcontrol_new)
  762. HDA_CODEC_VOLUME(name, adc_node->nid,
  763. spec->input_mux.items[i].index,
  764. HDA_INPUT);
  765. if ((err = snd_ctl_add(codec->bus->card,
  766. snd_ctl_new1(&knew, codec))) < 0)
  767. return err;
  768. }
  769. return 0;
  770. }
  771. /*
  772. * parse the nodes recursively until reach to the output PIN.
  773. *
  774. * returns 0 - if not found,
  775. * 1 - if found, but no mixer is created
  776. * 2 - if found and mixer was already created, (just skip)
  777. * a negative error code
  778. */
  779. static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
  780. struct hda_gnode *node, struct hda_gnode *dest_node,
  781. const char *type)
  782. {
  783. int i, err;
  784. if (node->checked)
  785. return 0;
  786. node->checked = 1;
  787. if (node == dest_node) {
  788. /* loopback connection found */
  789. return 1;
  790. }
  791. for (i = 0; i < node->nconns; i++) {
  792. struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
  793. if (! child)
  794. continue;
  795. err = parse_loopback_path(codec, spec, child, dest_node, type);
  796. if (err < 0)
  797. return err;
  798. else if (err >= 1) {
  799. if (err == 1) {
  800. err = create_mixer(codec, node, i, type, "Playback");
  801. if (err < 0)
  802. return err;
  803. if (err > 0)
  804. return 2; /* ok, created */
  805. /* not created, maybe in the lower path */
  806. err = 1;
  807. }
  808. /* connect and unmute */
  809. if (node->nconns > 1)
  810. select_input_connection(codec, node, i);
  811. unmute_input(codec, node, i);
  812. unmute_output(codec, node);
  813. return err;
  814. }
  815. }
  816. return 0;
  817. }
  818. /*
  819. * parse the tree and build the loopback controls
  820. */
  821. static int build_loopback_controls(struct hda_codec *codec)
  822. {
  823. struct hda_gspec *spec = codec->spec;
  824. struct list_head *p;
  825. struct hda_gnode *node;
  826. int err;
  827. const char *type;
  828. if (! spec->out_pin_node[0])
  829. return 0;
  830. list_for_each(p, &spec->nid_list) {
  831. node = list_entry(p, struct hda_gnode, list);
  832. if (node->type != AC_WID_PIN)
  833. continue;
  834. /* input capable? */
  835. if (! (node->pin_caps & AC_PINCAP_IN))
  836. return 0;
  837. type = get_input_type(node, NULL);
  838. if (type) {
  839. if (check_existing_control(codec, type, "Playback"))
  840. continue;
  841. clear_check_flags(spec);
  842. err = parse_loopback_path(codec, spec,
  843. spec->out_pin_node[0],
  844. node, type);
  845. if (err < 0)
  846. return err;
  847. if (! err)
  848. continue;
  849. }
  850. }
  851. return 0;
  852. }
  853. /*
  854. * build mixer controls
  855. */
  856. static int build_generic_controls(struct hda_codec *codec)
  857. {
  858. int err;
  859. if ((err = build_input_controls(codec)) < 0 ||
  860. (err = build_output_controls(codec)) < 0 ||
  861. (err = build_loopback_controls(codec)) < 0)
  862. return err;
  863. return 0;
  864. }
  865. /*
  866. * PCM
  867. */
  868. static struct hda_pcm_stream generic_pcm_playback = {
  869. .substreams = 1,
  870. .channels_min = 2,
  871. .channels_max = 2,
  872. };
  873. static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
  874. struct hda_codec *codec,
  875. unsigned int stream_tag,
  876. unsigned int format,
  877. struct snd_pcm_substream *substream)
  878. {
  879. struct hda_gspec *spec = codec->spec;
  880. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  881. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
  882. stream_tag, 0, format);
  883. return 0;
  884. }
  885. static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
  886. struct hda_codec *codec,
  887. struct snd_pcm_substream *substream)
  888. {
  889. struct hda_gspec *spec = codec->spec;
  890. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  891. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
  892. return 0;
  893. }
  894. static int build_generic_pcms(struct hda_codec *codec)
  895. {
  896. struct hda_gspec *spec = codec->spec;
  897. struct hda_pcm *info = &spec->pcm_rec;
  898. if (! spec->dac_node[0] && ! spec->adc_node) {
  899. snd_printd("hda_generic: no PCM found\n");
  900. return 0;
  901. }
  902. codec->num_pcms = 1;
  903. codec->pcm_info = info;
  904. info->name = "HDA Generic";
  905. if (spec->dac_node[0]) {
  906. info->stream[0] = generic_pcm_playback;
  907. info->stream[0].nid = spec->dac_node[0]->nid;
  908. if (spec->dac_node[1]) {
  909. info->stream[0].ops.prepare = generic_pcm2_prepare;
  910. info->stream[0].ops.cleanup = generic_pcm2_cleanup;
  911. }
  912. }
  913. if (spec->adc_node) {
  914. info->stream[1] = generic_pcm_playback;
  915. info->stream[1].nid = spec->adc_node->nid;
  916. }
  917. return 0;
  918. }
  919. /*
  920. */
  921. static struct hda_codec_ops generic_patch_ops = {
  922. .build_controls = build_generic_controls,
  923. .build_pcms = build_generic_pcms,
  924. .free = snd_hda_generic_free,
  925. };
  926. /*
  927. * the generic parser
  928. */
  929. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  930. {
  931. struct hda_gspec *spec;
  932. int err;
  933. if(!codec->afg)
  934. return 0;
  935. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  936. if (spec == NULL) {
  937. printk(KERN_ERR "hda_generic: can't allocate spec\n");
  938. return -ENOMEM;
  939. }
  940. codec->spec = spec;
  941. INIT_LIST_HEAD(&spec->nid_list);
  942. if ((err = build_afg_tree(codec)) < 0)
  943. goto error;
  944. if ((err = parse_input(codec)) < 0 ||
  945. (err = parse_output(codec)) < 0)
  946. goto error;
  947. codec->patch_ops = generic_patch_ops;
  948. return 0;
  949. error:
  950. snd_hda_generic_free(codec);
  951. return err;
  952. }