hda_generic.c 28 KB

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