hda_generic.c 24 KB

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