hda_generic.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 (node->pin_caps &
  441. (AC_PINCAP_VREF_80 << AC_PINCAP_VREF_SHIFT))
  442. *pinctl |= AC_PINCTL_VREF_80;
  443. if ((location & 0x0f) == AC_JACK_LOC_FRONT)
  444. return "Front Mic";
  445. return "Mic";
  446. case AC_JACK_SPDIF_IN:
  447. return "SPDIF";
  448. case AC_JACK_DIG_OTHER_IN:
  449. return "Digital";
  450. }
  451. return NULL;
  452. }
  453. /*
  454. * parse the nodes recursively until reach to the input PIN
  455. *
  456. * returns 0 if not found, 1 if found, or a negative error code.
  457. */
  458. static int parse_adc_sub_nodes(struct hda_codec *codec, struct hda_gspec *spec,
  459. struct hda_gnode *node)
  460. {
  461. int i, err;
  462. unsigned int pinctl;
  463. char *label;
  464. const char *type;
  465. if (node->checked)
  466. return 0;
  467. node->checked = 1;
  468. if (node->type != AC_WID_PIN) {
  469. for (i = 0; i < node->nconns; i++) {
  470. struct hda_gnode *child;
  471. child = hda_get_node(spec, node->conn_list[i]);
  472. if (! child)
  473. continue;
  474. err = parse_adc_sub_nodes(codec, spec, child);
  475. if (err < 0)
  476. return err;
  477. if (err > 0) {
  478. /* found one,
  479. * select the path, unmute both input and output
  480. */
  481. if (node->nconns > 1)
  482. select_input_connection(codec, node, i);
  483. unmute_input(codec, node, i);
  484. unmute_output(codec, node);
  485. return err;
  486. }
  487. }
  488. return 0;
  489. }
  490. /* input capable? */
  491. if (! (node->pin_caps & AC_PINCAP_IN))
  492. return 0;
  493. if (defcfg_port_conn(node) == AC_JACK_PORT_NONE)
  494. return 0; /* unconnected */
  495. if (node->wid_caps & AC_WCAP_DIGITAL)
  496. return 0; /* skip SPDIF */
  497. if (spec->input_mux.num_items >= HDA_MAX_NUM_INPUTS) {
  498. snd_printk(KERN_ERR "hda_generic: Too many items for capture\n");
  499. return -EINVAL;
  500. }
  501. pinctl = AC_PINCTL_IN_EN;
  502. /* create a proper capture source label */
  503. type = get_input_type(node, &pinctl);
  504. if (! type) {
  505. /* input as default? */
  506. if (! (node->pin_ctl & AC_PINCTL_IN_EN))
  507. return 0;
  508. type = "Input";
  509. }
  510. label = spec->cap_labels[spec->input_mux.num_items];
  511. strcpy(label, type);
  512. spec->input_mux.items[spec->input_mux.num_items].label = label;
  513. /* unmute the PIN external input */
  514. unmute_input(codec, node, 0); /* index = 0? */
  515. /* set PIN-In enable */
  516. snd_hda_codec_write(codec, node->nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pinctl);
  517. return 1; /* found */
  518. }
  519. /* add a capture source element */
  520. static void add_cap_src(struct hda_gspec *spec, int idx)
  521. {
  522. struct hda_input_mux_item *csrc;
  523. char *buf;
  524. int num, ocap;
  525. num = spec->input_mux.num_items;
  526. csrc = &spec->input_mux.items[num];
  527. buf = spec->cap_labels[num];
  528. for (ocap = 0; ocap < num; ocap++) {
  529. if (! strcmp(buf, spec->cap_labels[ocap])) {
  530. /* same label already exists,
  531. * put the index number to be unique
  532. */
  533. sprintf(buf, "%s %d", spec->cap_labels[ocap], num);
  534. break;
  535. }
  536. }
  537. csrc->index = idx;
  538. spec->input_mux.num_items++;
  539. }
  540. /*
  541. * parse input
  542. */
  543. static int parse_input_path(struct hda_codec *codec, struct hda_gnode *adc_node)
  544. {
  545. struct hda_gspec *spec = codec->spec;
  546. struct hda_gnode *node;
  547. int i, err;
  548. snd_printdd("AUD_IN = %x\n", adc_node->nid);
  549. clear_check_flags(spec);
  550. // awk added - fixed no recording due to muted widget
  551. unmute_input(codec, adc_node, 0);
  552. /*
  553. * check each connection of the ADC
  554. * if it reaches to a proper input PIN, add the path as the
  555. * input path.
  556. */
  557. /* first, check the direct connections to PIN widgets */
  558. for (i = 0; i < adc_node->nconns; i++) {
  559. node = hda_get_node(spec, adc_node->conn_list[i]);
  560. if (node && node->type == AC_WID_PIN) {
  561. err = parse_adc_sub_nodes(codec, spec, node);
  562. if (err < 0)
  563. return err;
  564. else if (err > 0)
  565. add_cap_src(spec, i);
  566. }
  567. }
  568. /* ... then check the rests, more complicated connections */
  569. for (i = 0; i < adc_node->nconns; i++) {
  570. node = hda_get_node(spec, adc_node->conn_list[i]);
  571. if (node && node->type != AC_WID_PIN) {
  572. err = parse_adc_sub_nodes(codec, spec, node);
  573. if (err < 0)
  574. return err;
  575. else if (err > 0)
  576. add_cap_src(spec, i);
  577. }
  578. }
  579. if (! spec->input_mux.num_items)
  580. return 0; /* no input path found... */
  581. snd_printdd("[Capture Source] NID=0x%x, #SRC=%d\n", adc_node->nid, spec->input_mux.num_items);
  582. for (i = 0; i < spec->input_mux.num_items; i++)
  583. snd_printdd(" [%s] IDX=0x%x\n", spec->input_mux.items[i].label,
  584. spec->input_mux.items[i].index);
  585. spec->adc_node = adc_node;
  586. return 1;
  587. }
  588. /*
  589. * parse input
  590. */
  591. static int parse_input(struct hda_codec *codec)
  592. {
  593. struct hda_gspec *spec = codec->spec;
  594. struct list_head *p;
  595. struct hda_gnode *node;
  596. int err;
  597. /*
  598. * At first we look for an audio input widget.
  599. * If it reaches to certain input PINs, we take it as the
  600. * input path.
  601. */
  602. list_for_each(p, &spec->nid_list) {
  603. node = list_entry(p, struct hda_gnode, list);
  604. if (node->wid_caps & AC_WCAP_DIGITAL)
  605. continue; /* skip SPDIF */
  606. if (node->type == AC_WID_AUD_IN) {
  607. err = parse_input_path(codec, node);
  608. if (err < 0)
  609. return err;
  610. else if (err > 0)
  611. return 0;
  612. }
  613. }
  614. snd_printd("hda_generic: no proper input path found\n");
  615. return 0;
  616. }
  617. /*
  618. * create mixer controls if possible
  619. */
  620. static int create_mixer(struct hda_codec *codec, struct hda_gnode *node,
  621. unsigned int index, const char *type, const char *dir_sfx)
  622. {
  623. char name[32];
  624. int err;
  625. int created = 0;
  626. struct snd_kcontrol_new knew;
  627. if (type)
  628. sprintf(name, "%s %s Switch", type, dir_sfx);
  629. else
  630. sprintf(name, "%s Switch", dir_sfx);
  631. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  632. (node->amp_in_caps & AC_AMPCAP_MUTE)) {
  633. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, index, HDA_INPUT);
  634. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  635. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  636. return err;
  637. created = 1;
  638. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  639. (node->amp_out_caps & AC_AMPCAP_MUTE)) {
  640. knew = (struct snd_kcontrol_new)HDA_CODEC_MUTE(name, node->nid, 0, HDA_OUTPUT);
  641. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  642. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  643. return err;
  644. created = 1;
  645. }
  646. if (type)
  647. sprintf(name, "%s %s Volume", type, dir_sfx);
  648. else
  649. sprintf(name, "%s Volume", dir_sfx);
  650. if ((node->wid_caps & AC_WCAP_IN_AMP) &&
  651. (node->amp_in_caps & AC_AMPCAP_NUM_STEPS)) {
  652. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, index, HDA_INPUT);
  653. snd_printdd("[%s] NID=0x%x, DIR=IN, IDX=0x%x\n", name, node->nid, index);
  654. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  655. return err;
  656. created = 1;
  657. } else if ((node->wid_caps & AC_WCAP_OUT_AMP) &&
  658. (node->amp_out_caps & AC_AMPCAP_NUM_STEPS)) {
  659. knew = (struct snd_kcontrol_new)HDA_CODEC_VOLUME(name, node->nid, 0, HDA_OUTPUT);
  660. snd_printdd("[%s] NID=0x%x, DIR=OUT\n", name, node->nid);
  661. if ((err = snd_ctl_add(codec->bus->card, snd_ctl_new1(&knew, codec))) < 0)
  662. return err;
  663. created = 1;
  664. }
  665. return created;
  666. }
  667. /*
  668. * check whether the controls with the given name and direction suffix already exist
  669. */
  670. static int check_existing_control(struct hda_codec *codec, const char *type, const char *dir)
  671. {
  672. struct snd_ctl_elem_id id;
  673. memset(&id, 0, sizeof(id));
  674. sprintf(id.name, "%s %s Volume", type, dir);
  675. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  676. if (snd_ctl_find_id(codec->bus->card, &id))
  677. return 1;
  678. sprintf(id.name, "%s %s Switch", type, dir);
  679. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  680. if (snd_ctl_find_id(codec->bus->card, &id))
  681. return 1;
  682. return 0;
  683. }
  684. /*
  685. * build output mixer controls
  686. */
  687. static int create_output_mixers(struct hda_codec *codec, const char **names)
  688. {
  689. struct hda_gspec *spec = codec->spec;
  690. int i, err;
  691. for (i = 0; i < spec->pcm_vol_nodes; i++) {
  692. err = create_mixer(codec, spec->pcm_vol[i].node,
  693. spec->pcm_vol[i].index,
  694. names[i], "Playback");
  695. if (err < 0)
  696. return err;
  697. }
  698. return 0;
  699. }
  700. static int build_output_controls(struct hda_codec *codec)
  701. {
  702. struct hda_gspec *spec = codec->spec;
  703. static const char *types_speaker[] = { "Speaker", "Headphone" };
  704. static const char *types_line[] = { "Front", "Headphone" };
  705. switch (spec->pcm_vol_nodes) {
  706. case 1:
  707. return create_mixer(codec, spec->pcm_vol[0].node,
  708. spec->pcm_vol[0].index,
  709. "Master", "Playback");
  710. case 2:
  711. if (defcfg_type(spec->out_pin_node[0]) == AC_JACK_SPEAKER)
  712. return create_output_mixers(codec, types_speaker);
  713. else
  714. return create_output_mixers(codec, types_line);
  715. }
  716. return 0;
  717. }
  718. /* create capture volume/switch */
  719. static int build_input_controls(struct hda_codec *codec)
  720. {
  721. struct hda_gspec *spec = codec->spec;
  722. struct hda_gnode *adc_node = spec->adc_node;
  723. int i, err;
  724. static struct snd_kcontrol_new cap_sel = {
  725. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  726. .name = "Capture Source",
  727. .info = capture_source_info,
  728. .get = capture_source_get,
  729. .put = capture_source_put,
  730. };
  731. if (! adc_node || ! spec->input_mux.num_items)
  732. return 0; /* not found */
  733. spec->cur_cap_src = 0;
  734. select_input_connection(codec, adc_node,
  735. spec->input_mux.items[0].index);
  736. /* create capture volume and switch controls if the ADC has an amp */
  737. /* do we have only a single item? */
  738. if (spec->input_mux.num_items == 1) {
  739. err = create_mixer(codec, adc_node,
  740. spec->input_mux.items[0].index,
  741. NULL, "Capture");
  742. if (err < 0)
  743. return err;
  744. return 0;
  745. }
  746. /* create input MUX if multiple sources are available */
  747. if ((err = snd_ctl_add(codec->bus->card,
  748. snd_ctl_new1(&cap_sel, codec))) < 0)
  749. return err;
  750. /* no volume control? */
  751. if (! (adc_node->wid_caps & AC_WCAP_IN_AMP) ||
  752. ! (adc_node->amp_in_caps & AC_AMPCAP_NUM_STEPS))
  753. return 0;
  754. for (i = 0; i < spec->input_mux.num_items; i++) {
  755. struct snd_kcontrol_new knew;
  756. char name[32];
  757. sprintf(name, "%s Capture Volume",
  758. spec->input_mux.items[i].label);
  759. knew = (struct snd_kcontrol_new)
  760. HDA_CODEC_VOLUME(name, adc_node->nid,
  761. spec->input_mux.items[i].index,
  762. HDA_INPUT);
  763. if ((err = snd_ctl_add(codec->bus->card,
  764. snd_ctl_new1(&knew, codec))) < 0)
  765. return err;
  766. }
  767. return 0;
  768. }
  769. /*
  770. * parse the nodes recursively until reach to the output PIN.
  771. *
  772. * returns 0 - if not found,
  773. * 1 - if found, but no mixer is created
  774. * 2 - if found and mixer was already created, (just skip)
  775. * a negative error code
  776. */
  777. static int parse_loopback_path(struct hda_codec *codec, struct hda_gspec *spec,
  778. struct hda_gnode *node, struct hda_gnode *dest_node,
  779. const char *type)
  780. {
  781. int i, err;
  782. if (node->checked)
  783. return 0;
  784. node->checked = 1;
  785. if (node == dest_node) {
  786. /* loopback connection found */
  787. return 1;
  788. }
  789. for (i = 0; i < node->nconns; i++) {
  790. struct hda_gnode *child = hda_get_node(spec, node->conn_list[i]);
  791. if (! child)
  792. continue;
  793. err = parse_loopback_path(codec, spec, child, dest_node, type);
  794. if (err < 0)
  795. return err;
  796. else if (err >= 1) {
  797. if (err == 1) {
  798. err = create_mixer(codec, node, i, type, "Playback");
  799. if (err < 0)
  800. return err;
  801. if (err > 0)
  802. return 2; /* ok, created */
  803. /* not created, maybe in the lower path */
  804. err = 1;
  805. }
  806. /* connect and unmute */
  807. if (node->nconns > 1)
  808. select_input_connection(codec, node, i);
  809. unmute_input(codec, node, i);
  810. unmute_output(codec, node);
  811. return err;
  812. }
  813. }
  814. return 0;
  815. }
  816. /*
  817. * parse the tree and build the loopback controls
  818. */
  819. static int build_loopback_controls(struct hda_codec *codec)
  820. {
  821. struct hda_gspec *spec = codec->spec;
  822. struct list_head *p;
  823. struct hda_gnode *node;
  824. int err;
  825. const char *type;
  826. if (! spec->out_pin_node[0])
  827. return 0;
  828. list_for_each(p, &spec->nid_list) {
  829. node = list_entry(p, struct hda_gnode, list);
  830. if (node->type != AC_WID_PIN)
  831. continue;
  832. /* input capable? */
  833. if (! (node->pin_caps & AC_PINCAP_IN))
  834. return 0;
  835. type = get_input_type(node, NULL);
  836. if (type) {
  837. if (check_existing_control(codec, type, "Playback"))
  838. continue;
  839. clear_check_flags(spec);
  840. err = parse_loopback_path(codec, spec,
  841. spec->out_pin_node[0],
  842. node, type);
  843. if (err < 0)
  844. return err;
  845. if (! err)
  846. continue;
  847. }
  848. }
  849. return 0;
  850. }
  851. /*
  852. * build mixer controls
  853. */
  854. static int build_generic_controls(struct hda_codec *codec)
  855. {
  856. int err;
  857. if ((err = build_input_controls(codec)) < 0 ||
  858. (err = build_output_controls(codec)) < 0 ||
  859. (err = build_loopback_controls(codec)) < 0)
  860. return err;
  861. return 0;
  862. }
  863. /*
  864. * PCM
  865. */
  866. static struct hda_pcm_stream generic_pcm_playback = {
  867. .substreams = 1,
  868. .channels_min = 2,
  869. .channels_max = 2,
  870. };
  871. static int generic_pcm2_prepare(struct hda_pcm_stream *hinfo,
  872. struct hda_codec *codec,
  873. unsigned int stream_tag,
  874. unsigned int format,
  875. struct snd_pcm_substream *substream)
  876. {
  877. struct hda_gspec *spec = codec->spec;
  878. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  879. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid,
  880. stream_tag, 0, format);
  881. return 0;
  882. }
  883. static int generic_pcm2_cleanup(struct hda_pcm_stream *hinfo,
  884. struct hda_codec *codec,
  885. struct snd_pcm_substream *substream)
  886. {
  887. struct hda_gspec *spec = codec->spec;
  888. snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
  889. snd_hda_codec_setup_stream(codec, spec->dac_node[1]->nid, 0, 0, 0);
  890. return 0;
  891. }
  892. static int build_generic_pcms(struct hda_codec *codec)
  893. {
  894. struct hda_gspec *spec = codec->spec;
  895. struct hda_pcm *info = &spec->pcm_rec;
  896. if (! spec->dac_node[0] && ! spec->adc_node) {
  897. snd_printd("hda_generic: no PCM found\n");
  898. return 0;
  899. }
  900. codec->num_pcms = 1;
  901. codec->pcm_info = info;
  902. info->name = "HDA Generic";
  903. if (spec->dac_node[0]) {
  904. info->stream[0] = generic_pcm_playback;
  905. info->stream[0].nid = spec->dac_node[0]->nid;
  906. if (spec->dac_node[1]) {
  907. info->stream[0].ops.prepare = generic_pcm2_prepare;
  908. info->stream[0].ops.cleanup = generic_pcm2_cleanup;
  909. }
  910. }
  911. if (spec->adc_node) {
  912. info->stream[1] = generic_pcm_playback;
  913. info->stream[1].nid = spec->adc_node->nid;
  914. }
  915. return 0;
  916. }
  917. /*
  918. */
  919. static struct hda_codec_ops generic_patch_ops = {
  920. .build_controls = build_generic_controls,
  921. .build_pcms = build_generic_pcms,
  922. .free = snd_hda_generic_free,
  923. };
  924. /*
  925. * the generic parser
  926. */
  927. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  928. {
  929. struct hda_gspec *spec;
  930. int err;
  931. if(!codec->afg)
  932. return 0;
  933. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  934. if (spec == NULL) {
  935. printk(KERN_ERR "hda_generic: can't allocate spec\n");
  936. return -ENOMEM;
  937. }
  938. codec->spec = spec;
  939. INIT_LIST_HEAD(&spec->nid_list);
  940. if ((err = build_afg_tree(codec)) < 0)
  941. goto error;
  942. if ((err = parse_input(codec)) < 0 ||
  943. (err = parse_output(codec)) < 0)
  944. goto error;
  945. codec->patch_ops = generic_patch_ops;
  946. return 0;
  947. error:
  948. snd_hda_generic_free(codec);
  949. return err;
  950. }