hda_generic.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 <linux/pci.h>
  26. #include <sound/core.h>
  27. #include "hda_codec.h"
  28. #include "hda_local.h"
  29. /* widget node for parsing */
  30. struct hda_gnode {
  31. hda_nid_t nid; /* NID of this widget */
  32. unsigned short nconns; /* number of input connections */
  33. hda_nid_t conn_list[HDA_MAX_CONNECTIONS]; /* input connections */
  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. struct hda_gspec {
  46. struct hda_gnode *dac_node; /* DAC node */
  47. struct hda_gnode *out_pin_node; /* Output pin (Line-Out) node */
  48. struct hda_gnode *pcm_vol_node; /* Node for PCM volume */
  49. unsigned int pcm_vol_index; /* connection of PCM volume */
  50. struct hda_gnode *adc_node; /* ADC node */
  51. struct hda_gnode *cap_vol_node; /* Node for capture volume */
  52. unsigned int cur_cap_src; /* current capture source */
  53. struct hda_input_mux input_mux;
  54. char cap_labels[HDA_MAX_NUM_INPUTS][16];
  55. unsigned int def_amp_in_caps;
  56. unsigned int def_amp_out_caps;
  57. struct hda_pcm pcm_rec; /* PCM information */
  58. struct list_head nid_list; /* list of widgets */
  59. };
  60. /*
  61. * retrieve the default device type from the default config value
  62. */
  63. #define defcfg_type(node) (((node)->def_cfg & AC_DEFCFG_DEVICE) >> AC_DEFCFG_DEVICE_SHIFT)
  64. #define defcfg_location(node) (((node)->def_cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT)
  65. /*
  66. * destructor
  67. */
  68. static void snd_hda_generic_free(struct hda_codec *codec)
  69. {
  70. struct hda_gspec *spec = codec->spec;
  71. struct list_head *p, *n;
  72. if (! spec)
  73. return;
  74. /* free all widgets */
  75. list_for_each_safe(p, n, &spec->nid_list) {
  76. struct hda_gnode *node = list_entry(p, struct hda_gnode, list);
  77. kfree(node);
  78. }
  79. kfree(spec);
  80. }
  81. /*
  82. * add a new widget node and read its attributes
  83. */
  84. static int add_new_node(struct hda_codec *codec, struct hda_gspec *spec, hda_nid_t nid)
  85. {
  86. struct hda_gnode *node;
  87. int nconns;
  88. node = kcalloc(1, sizeof(*node), GFP_KERNEL);
  89. if (node == NULL)
  90. return -ENOMEM;
  91. node->nid = nid;
  92. nconns = snd_hda_get_connections(codec, nid, node->conn_list, HDA_MAX_CONNECTIONS);
  93. if (nconns < 0) {
  94. kfree(node);
  95. return nconns;
  96. }
  97. node->nconns = nconns;
  98. node->wid_caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  99. node->type = (node->wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
  100. if (node->type == AC_WID_PIN) {
  101. node->pin_caps = snd_hda_param_read(codec, node->nid, AC_PAR_PIN_CAP);
  102. node->pin_ctl = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  103. node->def_cfg = snd_hda_codec_read(codec, node->nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  104. }
  105. if (node->wid_caps & AC_WCAP_OUT_AMP) {
  106. if (node->wid_caps & AC_WCAP_AMP_OVRD)
  107. node->amp_out_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_OUT_CAP);
  108. if (! node->amp_out_caps)
  109. node->amp_out_caps = spec->def_amp_out_caps;
  110. }
  111. if (node->wid_caps & AC_WCAP_IN_AMP) {
  112. if (node->wid_caps & AC_WCAP_AMP_OVRD)
  113. node->amp_in_caps = snd_hda_param_read(codec, node->nid, AC_PAR_AMP_IN_CAP);
  114. if (! node->amp_in_caps)
  115. node->amp_in_caps = spec->def_amp_in_caps;
  116. }
  117. list_add_tail(&node->list, &spec->nid_list);
  118. return 0;
  119. }
  120. /*
  121. * build the AFG subtree
  122. */
  123. static int build_afg_tree(struct hda_codec *codec)
  124. {
  125. struct hda_gspec *spec = codec->spec;
  126. int i, nodes, err;
  127. hda_nid_t nid;
  128. snd_assert(spec, return -EINVAL);
  129. spec->def_amp_out_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_OUT_CAP);
  130. spec->def_amp_in_caps = snd_hda_param_read(codec, codec->afg, AC_PAR_AMP_IN_CAP);
  131. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  132. if (! nid || nodes < 0) {
  133. printk(KERN_ERR "Invalid AFG subtree\n");
  134. return -EINVAL;
  135. }
  136. /* parse all nodes belonging to the AFG */
  137. for (i = 0; i < nodes; i++, nid++) {
  138. if ((err = add_new_node(codec, spec, nid)) < 0)
  139. return err;
  140. }
  141. return 0;
  142. }
  143. /*
  144. * look for the node record for the given NID
  145. */
  146. /* FIXME: should avoid the braindead linear search */
  147. static struct hda_gnode *hda_get_node(struct hda_gspec *spec, hda_nid_t nid)
  148. {
  149. struct list_head *p;
  150. struct hda_gnode *node;
  151. list_for_each(p, &spec->nid_list) {
  152. node = list_entry(p, struct hda_gnode, list);
  153. if (node->nid == nid)
  154. return node;
  155. }
  156. return NULL;
  157. }
  158. /*
  159. * unmute (and set max vol) the output amplifier
  160. */
  161. static int unmute_output(struct hda_codec *codec, struct hda_gnode *node)
  162. {
  163. unsigned int val, ofs;
  164. snd_printdd("UNMUTE OUT: NID=0x%x\n", node->nid);
  165. val = (node->amp_out_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  166. ofs = (node->amp_out_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  167. if (val >= ofs)
  168. val -= ofs;
  169. val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
  170. val |= AC_AMP_SET_OUTPUT;
  171. return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
  172. }
  173. /*
  174. * unmute (and set max vol) the input amplifier
  175. */
  176. static int unmute_input(struct hda_codec *codec, struct hda_gnode *node, unsigned int index)
  177. {
  178. unsigned int val, ofs;
  179. snd_printdd("UNMUTE IN: NID=0x%x IDX=0x%x\n", node->nid, index);
  180. val = (node->amp_in_caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
  181. ofs = (node->amp_in_caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  182. if (val >= ofs)
  183. val -= ofs;
  184. val |= AC_AMP_SET_LEFT | AC_AMP_SET_RIGHT;
  185. val |= AC_AMP_SET_INPUT;
  186. // awk added - fixed to allow unmuting of indexed amps
  187. val |= index << AC_AMP_SET_INDEX_SHIFT;
  188. return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, val);
  189. }
  190. /*
  191. * select the input connection of the given node.
  192. */
  193. static int select_input_connection(struct hda_codec *codec, struct hda_gnode *node,
  194. unsigned int index)
  195. {
  196. snd_printdd("CONNECT: NID=0x%x IDX=0x%x\n", node->nid, index);
  197. return snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_CONNECT_SEL, index);
  198. }
  199. /*
  200. * clear checked flag of each node in the node list
  201. */
  202. static void clear_check_flags(struct hda_gspec *spec)
  203. {
  204. struct list_head *p;
  205. struct hda_gnode *node;
  206. list_for_each(p, &spec->nid_list) {
  207. node = list_entry(p, struct hda_gnode, list);
  208. node->checked = 0;
  209. }
  210. }
  211. /*
  212. * parse the output path recursively until reach to an audio output widget
  213. *
  214. * returns 0 if not found, 1 if found, or a negative error code.
  215. */
  216. static int parse_output_path(struct hda_codec *codec, struct hda_gspec *spec,
  217. struct hda_gnode *node)
  218. {
  219. int i, err;
  220. struct hda_gnode *child;
  221. if (node->checked)
  222. return 0;
  223. node->checked = 1;
  224. if (node->type == AC_WID_AUD_OUT) {
  225. if (node->wid_caps & AC_WCAP_DIGITAL) {
  226. snd_printdd("Skip Digital OUT node %x\n", node->nid);
  227. return 0;
  228. }
  229. snd_printdd("AUD_OUT found %x\n", node->nid);
  230. if (spec->dac_node) {
  231. /* already DAC node is assigned, just unmute & connect */
  232. return node == spec->dac_node;
  233. }
  234. spec->dac_node = node;
  235. if (node->wid_caps & AC_WCAP_OUT_AMP) {
  236. spec->pcm_vol_node = node;
  237. spec->pcm_vol_index = 0;
  238. }
  239. return 1; /* found */
  240. }
  241. for (i = 0; i < node->nconns; i++) {
  242. child = hda_get_node(spec, node->conn_list[i]);
  243. if (! child)
  244. continue;
  245. err = parse_output_path(codec, spec, child);
  246. if (err < 0)
  247. return err;
  248. else if (err > 0) {
  249. /* found one,
  250. * select the path, unmute both input and output
  251. */
  252. if (node->nconns > 1)
  253. select_input_connection(codec, node, i);
  254. unmute_input(codec, node, i);
  255. unmute_output(codec, node);
  256. if (! spec->pcm_vol_node) {
  257. if (node->wid_caps & AC_WCAP_IN_AMP) {
  258. spec->pcm_vol_node = node;
  259. spec->pcm_vol_index = i;
  260. } else if (node->wid_caps & AC_WCAP_OUT_AMP) {
  261. spec->pcm_vol_node = node;
  262. spec->pcm_vol_index = 0;
  263. }
  264. }
  265. return 1;
  266. }
  267. }
  268. return 0;
  269. }
  270. /*
  271. * Look for the output PIN widget with the given jack type
  272. * and parse the output path to that PIN.
  273. *
  274. * Returns the PIN node when the path to DAC is established.
  275. */
  276. static struct hda_gnode *parse_output_jack(struct hda_codec *codec,
  277. struct hda_gspec *spec,
  278. int jack_type)
  279. {
  280. struct list_head *p;
  281. struct hda_gnode *node;
  282. int err;
  283. list_for_each(p, &spec->nid_list) {
  284. node = list_entry(p, struct hda_gnode, list);
  285. if (node->type != AC_WID_PIN)
  286. continue;
  287. /* output capable? */
  288. if (! (node->pin_caps & AC_PINCAP_OUT))
  289. continue;
  290. if (jack_type >= 0) {
  291. if (jack_type != defcfg_type(node))
  292. continue;
  293. if (node->wid_caps & AC_WCAP_DIGITAL)
  294. continue; /* skip SPDIF */
  295. } else {
  296. /* output as default? */
  297. if (! (node->pin_ctl & AC_PINCTL_OUT_EN))
  298. continue;
  299. }
  300. clear_check_flags(spec);
  301. err = parse_output_path(codec, spec, node);
  302. if (err < 0)
  303. return NULL;
  304. else if (err > 0) {
  305. /* unmute the PIN output */
  306. unmute_output(codec, node);
  307. /* set PIN-Out enable */
  308. snd_hda_codec_write(codec, node->nid, 0,
  309. AC_VERB_SET_PIN_WIDGET_CONTROL,
  310. AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  311. return node;
  312. }
  313. }
  314. return NULL;
  315. }
  316. /*
  317. * parse outputs
  318. */
  319. static int parse_output(struct hda_codec *codec)
  320. {
  321. struct hda_gspec *spec = codec->spec;
  322. struct hda_gnode *node;
  323. /*
  324. * Look for the output PIN widget
  325. */
  326. /* first, look for the line-out pin */
  327. node = parse_output_jack(codec, spec, AC_JACK_LINE_OUT);
  328. if (node) /* found, remember the PIN node */
  329. spec->out_pin_node = node;
  330. /* look for the HP-out pin */
  331. node = parse_output_jack(codec, spec, AC_JACK_HP_OUT);
  332. if (node) {
  333. if (! spec->out_pin_node)
  334. spec->out_pin_node = node;
  335. }
  336. if (! spec->out_pin_node) {
  337. /* no line-out or HP pins found,
  338. * then choose for the first output pin
  339. */
  340. spec->out_pin_node = parse_output_jack(codec, spec, -1);
  341. if (! spec->out_pin_node)
  342. snd_printd("hda_generic: no proper output path found\n");
  343. }
  344. return 0;
  345. }
  346. /*
  347. * input MUX
  348. */
  349. /* control callbacks */
  350. static int capture_source_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  351. {
  352. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  353. struct hda_gspec *spec = codec->spec;
  354. return snd_hda_input_mux_info(&spec->input_mux, uinfo);
  355. }
  356. static int capture_source_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  357. {
  358. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  359. struct hda_gspec *spec = codec->spec;
  360. ucontrol->value.enumerated.item[0] = spec->cur_cap_src;
  361. return 0;
  362. }
  363. static int capture_source_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  364. {
  365. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  366. struct hda_gspec *spec = codec->spec;
  367. return snd_hda_input_mux_put(codec, &spec->input_mux, ucontrol,
  368. spec->adc_node->nid, &spec->cur_cap_src);
  369. }
  370. /*
  371. * return the string name of the given input PIN widget
  372. */
  373. static const char *get_input_type(struct hda_gnode *node, unsigned int *pinctl)
  374. {
  375. unsigned int location = defcfg_location(node);
  376. switch (defcfg_type(node)) {
  377. case AC_JACK_LINE_IN:
  378. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  379. return "Front Line";
  380. return "Line";
  381. case AC_JACK_CD:
  382. if (pinctl)
  383. *pinctl |= AC_PINCTL_VREF_GRD;
  384. return "CD";
  385. case AC_JACK_AUX:
  386. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  387. return "Front Aux";
  388. return "Aux";
  389. case AC_JACK_MIC_IN:
  390. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  391. return "Front Mic";
  392. return "Mic";
  393. case AC_JACK_SPDIF_IN:
  394. return "SPDIF";
  395. case AC_JACK_DIG_OTHER_IN:
  396. return "Digital";
  397. }
  398. return NULL;
  399. }
  400. /*
  401. * parse the nodes recursively until reach to the input PIN
  402. *
  403. * returns 0 if not found, 1 if found, or a negative error code.
  404. */
  405. static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
  406. struct hda_gnode *node)
  407. {
  408. int i, err;
  409. unsigned int pinctl;
  410. char *label;
  411. const char *type;
  412. if (node->checked)
  413. return 0;
  414. node->checked = 1;
  415. if (node->type != AC_WID_PIN) {
  416. for (i = 0; i < node->nconns; i++) {
  417. struct hda_gnode *child;
  418. child = hda_get_node(spec, node->conn_list[i]);
  419. if (! child)
  420. continue;
  421. err = parse_adc_sub_nodes(codec, spec, child);
  422. if (err < 0)
  423. return err;
  424. if (err > 0) {
  425. /* found one,
  426. * select the path, unmute both input and output
  427. */
  428. if (node->nconns > 1)
  429. select_input_connection(codec, node, i);
  430. unmute_input(codec, node, i);
  431. unmute_output(codec, node);
  432. return err;
  433. }
  434. }
  435. return 0;
  436. }
  437. /* input capable? */
  438. if (! (node->pin_caps & AC_PINCAP_IN))
  439. return 0;
  440. if (node->wid_caps & AC_WCAP_DIGITAL)
  441. return 0; /* skip SPDIF */
  442. if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
  443. snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
  444. return -EINVAL;
  445. }
  446. pinctl = AC_PINCTL_IN_EN;
  447. /* create a proper capture source label */
  448. type = get_input_type(node, &pinctl);
  449. if (! type) {
  450. /* input as default? */
  451. if (! (node->pin_ctl & AC_PINCTL_IN_EN))
  452. return 0;
  453. type = "Input";
  454. }
  455. label = spec->cap_labels[spec->input_mux.num_items];
  456. strcpy(label, type);
  457. spec->input_mux.items[spec->input_mux.num_items].label = label;
  458. /* unmute the PIN external input */
  459. unmute_input(codec, node, 0); /* index = 0? */
  460. /* set PIN-In enable */
  461. snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
  462. return 1; /* found */
  463. }
  464. /*
  465. * parse input
  466. */
  467. static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
  468. {
  469. struct hda_gspec *spec = codec->spec;
  470. struct hda_gnode *node;
  471. int i, err;
  472. snd_printdd("AUD_IN = %x\n", adc_node->nid);
  473. clear_check_flags(spec);
  474. // awk added - fixed no recording due to muted widget
  475. unmute_input(codec, adc_node, 0);
  476. /*
  477. * check each connection of the ADC
  478. * if it reaches to a proper input PIN, add the path as the
  479. * input path.
  480. */
  481. for (i = 0; i < adc_node->nconns; i++) {
  482. node = hda_get_node(spec, adc_node->conn_list[i]);
  483. if (! node)
  484. continue;
  485. err = parse_adc_sub_nodes(codec, spec, node);
  486. if (err < 0)
  487. return err;
  488. else if (err > 0) {
  489. struct hda_input_mux_item *csrc = &spec->input_mux.items[spec->input_mux.num_items];
  490. char *buf = spec->cap_labels[spec->input_mux.num_items];
  491. int ocap;
  492. for (ocap = 0; ocap < spec->input_mux.num_items; ocap++) {
  493. if (! strcmp(buf, spec->cap_labels[ocap])) {
  494. /* same label already exists,
  495. * put the index number to be unique
  496. */
  497. sprintf(buf, "%s %d", spec->cap_labels[ocap],
  498. spec->input_mux.num_items);
  499. }
  500. }
  501. csrc->index = i;
  502. spec->input_mux.num_items++;
  503. }
  504. }
  505. if (! spec->input_mux.num_items)
  506. return 0; /* no input path found... */
  507. snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
  508. for (i = 0; i < spec->input_mux.num_items; i++)
  509. snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
  510. spec->input_mux.items[i].index);
  511. spec->adc_node = adc_node;
  512. return 1;
  513. }
  514. /*
  515. * parse input
  516. */
  517. static int parse_input(struct hda_codec *codec)
  518. {
  519. struct hda_gspec *spec = codec->spec;
  520. struct list_head *p;
  521. struct hda_gnode *node;
  522. int err;
  523. /*
  524. * At first we look for an audio input widget.
  525. * If it reaches to certain input PINs, we take it as the
  526. * input path.
  527. */
  528. list_for_each(p, &spec->nid_list) {
  529. node = list_entry(p, struct hda_gnode, list);
  530. if (node->wid_caps & AC_WCAP_DIGITAL)
  531. continue; /* skip SPDIF */
  532. if (node->type == AC_WID_AUD_IN) {
  533. err = parse_input_path(codec, node);
  534. if (err < 0)
  535. return err;
  536. else if (err > 0)
  537. return 0;
  538. }
  539. }
  540. snd_printd("hda_generic: no proper input path found\n");
  541. return 0;
  542. }
  543. /*
  544. * create mixer controls if possible
  545. */
  546. #define DIR_OUT 0x1
  547. #define DIR_IN 0x2
  548. static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
  549. unsigned int index, const char *type, const char *dir_sfx)
  550. {
  551. char name[32];
  552. int err;
  553. int created = 0;
  554. snd_kcontrol_new_t knew;
  555. if (type)
  556. sprintf(name, "%s %s Switch", type, dir_sfx);
  557. else
  558. sprintf(name, "%s Switch", dir_sfx);
  559. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  560. (node->amp_in_caps & AC_AMPCAP_MUTE)) {
  561. knew = (snd_kcontrol_new_t)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
  562. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  563. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  564. return err;
  565. created = 1;
  566. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  567. (node->amp_out_caps & AC_AMPCAP_MUTE)) {
  568. knew = (snd_kcontrol_new_t)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
  569. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  570. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  571. return err;
  572. created = 1;
  573. }
  574. if (type)
  575. sprintf(name, "%s %s Volume", type, dir_sfx);
  576. else
  577. sprintf(name, "%s Volume", dir_sfx);
  578. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  579. (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
  580. knew = (snd_kcontrol_new_t)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
  581. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  582. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  583. return err;
  584. created = 1;
  585. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  586. (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
  587. knew = (snd_kcontrol_new_t)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
  588. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  589. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  590. return err;
  591. created = 1;
  592. }
  593. return created;
  594. }
  595. /*
  596. * check whether the controls with the given name and direction suffix already exist
  597. */
  598. static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
  599. {
  600. snd_ctl_elem_id_t id;
  601. memset(&id, 0, sizeof(id));
  602. sprintf(id.name, "%s %s Volume", type, dir);
  603. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  604. if (snd_ctl_find_id(codec->bus->card, &id))
  605. return 1;
  606. sprintf(id.name, "%s %s Switch", type, dir);
  607. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  608. if (snd_ctl_find_id(codec->bus->card, &id))
  609. return 1;
  610. return 0;
  611. }
  612. /*
  613. * build output mixer controls
  614. */
  615. static int build_output_controls(struct hda_codec *codec)
  616. {
  617. struct hda_gspec *spec = codec->spec;
  618. int err;
  619. err = create_mixer(codec, spec->pcm_vol_node, spec->pcm_vol_index,
  620. "PCM", "Playback");
  621. if (err < 0)
  622. return err;
  623. return 0;
  624. }
  625. /* create capture volume/switch */
  626. static int build_input_controls(struct hda_codec *codec)
  627. {
  628. struct hda_gspec *spec = codec->spec;
  629. struct hda_gnode *adc_node = spec->adc_node;
  630. int err;
  631. if (! adc_node)
  632. return 0; /* not found */
  633. /* create capture volume and switch controls if the ADC has an amp */
  634. err = create_mixer(codec, adc_node, 0, NULL, "Capture");
  635. /* create input MUX if multiple sources are available */
  636. if (spec->input_mux.num_items > 1) {
  637. static snd_kcontrol_new_t cap_sel = {
  638. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  639. .name = "Capture Source",
  640. .info = capture_source_info,
  641. .get = capture_source_get,
  642. .put = capture_source_put,
  643. };
  644. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&cap_sel, codec))) < 0)
  645. return err;
  646. spec->cur_cap_src = 0;
  647. select_input_connection(codec, adc_node, spec->input_mux.items[0].index);
  648. }
  649. return 0;
  650. }
  651. /*
  652. * parse the nodes recursively until reach to the output PIN.
  653. *
  654. * returns 0 - if not found,
  655. * 1 - if found, but no mixer is created
  656. * 2 - if found and mixer was already created, (just skip)
  657. * a negative error code
  658. */
  659. static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
  660. struct hda_gnode *node, struct hda_gnode *dest_node,
  661. const char *type)
  662. {
  663. int i, err;
  664. if (node->checked)
  665. return 0;
  666. node->checked = 1;
  667. if (node == dest_node) {
  668. /* loopback connection found */
  669. return 1;
  670. }
  671. for (i = 0; i < node->nconns; i++) {
  672. struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
  673. if (! child)
  674. continue;
  675. err = parse_loopback_path(codec, spec, child, dest_node, type);
  676. if (err < 0)
  677. return err;
  678. else if (err >= 1) {
  679. if (err == 1) {
  680. err = create_mixer(codec, node, i, type, "Playback");
  681. if (err < 0)
  682. return err;
  683. if (err > 0)
  684. return 2; /* ok, created */
  685. /* not created, maybe in the lower path */
  686. err = 1;
  687. }
  688. /* connect and unmute */
  689. if (node->nconns > 1)
  690. select_input_connection(codec, node, i);
  691. unmute_input(codec, node, i);
  692. unmute_output(codec, node);
  693. return err;
  694. }
  695. }
  696. return 0;
  697. }
  698. /*
  699. * parse the tree and build the loopback controls
  700. */
  701. static int build_loopback_controls(struct hda_codec *codec)
  702. {
  703. struct hda_gspec *spec = codec->spec;
  704. struct list_head *p;
  705. struct hda_gnode *node;
  706. int err;
  707. const char *type;
  708. if (! spec->out_pin_node)
  709. return 0;
  710. list_for_each(p, &spec->nid_list) {
  711. node = list_entry(p, struct hda_gnode, list);
  712. if (node->type != AC_WID_PIN)
  713. continue;
  714. /* input capable? */
  715. if (! (node->pin_caps & AC_PINCAP_IN))
  716. return 0;
  717. type = get_input_type(node, NULL);
  718. if (type) {
  719. if (check_existing_control(codec, type, "Playback"))
  720. continue;
  721. clear_check_flags(spec);
  722. err = parse_loopback_path(codec, spec, spec->out_pin_node,
  723. node, type);
  724. if (err < 0)
  725. return err;
  726. if (! err)
  727. continue;
  728. }
  729. }
  730. return 0;
  731. }
  732. /*
  733. * build mixer controls
  734. */
  735. static int build_generic_controls(struct hda_codec *codec)
  736. {
  737. int err;
  738. if ((err = build_input_controls(codec)) < 0 ||
  739. (err = build_output_controls(codec)) < 0 ||
  740. (err = build_loopback_controls(codec)) < 0)
  741. return err;
  742. return 0;
  743. }
  744. /*
  745. * PCM
  746. */
  747. static struct hda_pcm_stream generic_pcm_playback = {
  748. .substreams = 1,
  749. .channels_min = 2,
  750. .channels_max = 2,
  751. };
  752. static int build_generic_pcms(struct hda_codec *codec)
  753. {
  754. struct hda_gspec *spec = codec->spec;
  755. struct hda_pcm *info = &spec->pcm_rec;
  756. if (! spec->dac_node && ! spec->adc_node) {
  757. snd_printd("hda_generic: no PCM found\n");
  758. return 0;
  759. }
  760. codec->num_pcms = 1;
  761. codec->pcm_info = info;
  762. info->name = "HDA Generic";
  763. if (spec->dac_node) {
  764. info->stream[0] = generic_pcm_playback;
  765. info->stream[0].nid = spec->dac_node->nid;
  766. }
  767. if (spec->adc_node) {
  768. info->stream[1] = generic_pcm_playback;
  769. info->stream[1].nid = spec->adc_node->nid;
  770. }
  771. return 0;
  772. }
  773. /*
  774. */
  775. static struct hda_codec_ops generic_patch_ops = {
  776. .build_controls = build_generic_controls,
  777. .build_pcms = build_generic_pcms,
  778. .free = snd_hda_generic_free,
  779. };
  780. /*
  781. * the generic parser
  782. */
  783. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  784. {
  785. struct hda_gspec *spec;
  786. int err;
  787. if(!codec->afg) {
  788. snd_printdd("hda_generic: no generic modem yet\n");
  789. return -ENODEV;
  790. }
  791. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  792. if (spec == NULL) {
  793. printk(KERN_ERR "hda_generic: can't allocate spec\n");
  794. return -ENOMEM;
  795. }
  796. codec->spec = spec;
  797. INIT_LIST_HEAD(&spec->nid_list);
  798. if ((err = build_afg_tree(codec)) < 0)
  799. goto error;
  800. if ((err = parse_input(codec)) < 0 ||
  801. (err = parse_output(codec)) < 0)
  802. goto error;
  803. codec->patch_ops = generic_patch_ops;
  804. return 0;
  805. error:
  806. snd_hda_generic_free(codec);
  807. return err;
  808. }