hda_generic.c 27 KB

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