hda_generic.c 26 KB

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