1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21 
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/mutex.h>
28 #include <linux/module.h>
29 #include <sound/core.h>
30 #include "hda_codec.h"
31 #include <sound/asoundef.h>
32 #include <sound/tlv.h>
33 #include <sound/initval.h>
34 #include <sound/jack.h>
35 #include "hda_local.h"
36 #include "hda_beep.h"
37 #include "hda_jack.h"
38 #include <sound/hda_hwdep.h>
39 
40 #define CREATE_TRACE_POINTS
41 #include "hda_trace.h"
42 
43 /*
44  * vendor / preset table
45  */
46 
47 struct hda_vendor_id {
48 	unsigned int id;
49 	const char *name;
50 };
51 
52 /* codec vendor labels */
53 static struct hda_vendor_id hda_vendor_ids[] = {
54 	{ 0x1002, "ATI" },
55 	{ 0x1013, "Cirrus Logic" },
56 	{ 0x1057, "Motorola" },
57 	{ 0x1095, "Silicon Image" },
58 	{ 0x10de, "Nvidia" },
59 	{ 0x10ec, "Realtek" },
60 	{ 0x1102, "Creative" },
61 	{ 0x1106, "VIA" },
62 	{ 0x111d, "IDT" },
63 	{ 0x11c1, "LSI" },
64 	{ 0x11d4, "Analog Devices" },
65 	{ 0x13f6, "C-Media" },
66 	{ 0x14f1, "Conexant" },
67 	{ 0x17e8, "Chrontel" },
68 	{ 0x1854, "LG" },
69 	{ 0x1aec, "Wolfson Microelectronics" },
70 	{ 0x434d, "C-Media" },
71 	{ 0x8086, "Intel" },
72 	{ 0x8384, "SigmaTel" },
73 	{} /* terminator */
74 };
75 
76 static DEFINE_MUTEX(preset_mutex);
77 static LIST_HEAD(hda_preset_tables);
78 
snd_hda_add_codec_preset(struct hda_codec_preset_list * preset)79 int snd_hda_add_codec_preset(struct hda_codec_preset_list *preset)
80 {
81 	mutex_lock(&preset_mutex);
82 	list_add_tail(&preset->list, &hda_preset_tables);
83 	mutex_unlock(&preset_mutex);
84 	return 0;
85 }
86 EXPORT_SYMBOL_HDA(snd_hda_add_codec_preset);
87 
snd_hda_delete_codec_preset(struct hda_codec_preset_list * preset)88 int snd_hda_delete_codec_preset(struct hda_codec_preset_list *preset)
89 {
90 	mutex_lock(&preset_mutex);
91 	list_del(&preset->list);
92 	mutex_unlock(&preset_mutex);
93 	return 0;
94 }
95 EXPORT_SYMBOL_HDA(snd_hda_delete_codec_preset);
96 
97 #ifdef CONFIG_SND_HDA_POWER_SAVE
98 static void hda_power_work(struct work_struct *work);
99 static void hda_keep_power_on(struct hda_codec *codec);
100 #define hda_codec_is_power_on(codec)	((codec)->power_on)
101 #else
hda_keep_power_on(struct hda_codec * codec)102 static inline void hda_keep_power_on(struct hda_codec *codec) {}
103 #define hda_codec_is_power_on(codec)	1
104 #endif
105 
106 /**
107  * snd_hda_get_jack_location - Give a location string of the jack
108  * @cfg: pin default config value
109  *
110  * Parse the pin default config value and returns the string of the
111  * jack location, e.g. "Rear", "Front", etc.
112  */
snd_hda_get_jack_location(u32 cfg)113 const char *snd_hda_get_jack_location(u32 cfg)
114 {
115 	static char *bases[7] = {
116 		"N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
117 	};
118 	static unsigned char specials_idx[] = {
119 		0x07, 0x08,
120 		0x17, 0x18, 0x19,
121 		0x37, 0x38
122 	};
123 	static char *specials[] = {
124 		"Rear Panel", "Drive Bar",
125 		"Riser", "HDMI", "ATAPI",
126 		"Mobile-In", "Mobile-Out"
127 	};
128 	int i;
129 	cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
130 	if ((cfg & 0x0f) < 7)
131 		return bases[cfg & 0x0f];
132 	for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
133 		if (cfg == specials_idx[i])
134 			return specials[i];
135 	}
136 	return "UNKNOWN";
137 }
138 EXPORT_SYMBOL_HDA(snd_hda_get_jack_location);
139 
140 /**
141  * snd_hda_get_jack_connectivity - Give a connectivity string of the jack
142  * @cfg: pin default config value
143  *
144  * Parse the pin default config value and returns the string of the
145  * jack connectivity, i.e. external or internal connection.
146  */
snd_hda_get_jack_connectivity(u32 cfg)147 const char *snd_hda_get_jack_connectivity(u32 cfg)
148 {
149 	static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
150 
151 	return jack_locations[(cfg >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3];
152 }
153 EXPORT_SYMBOL_HDA(snd_hda_get_jack_connectivity);
154 
155 /**
156  * snd_hda_get_jack_type - Give a type string of the jack
157  * @cfg: pin default config value
158  *
159  * Parse the pin default config value and returns the string of the
160  * jack type, i.e. the purpose of the jack, such as Line-Out or CD.
161  */
snd_hda_get_jack_type(u32 cfg)162 const char *snd_hda_get_jack_type(u32 cfg)
163 {
164 	static char *jack_types[16] = {
165 		"Line Out", "Speaker", "HP Out", "CD",
166 		"SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
167 		"Line In", "Aux", "Mic", "Telephony",
168 		"SPDIF In", "Digital In", "Reserved", "Other"
169 	};
170 
171 	return jack_types[(cfg & AC_DEFCFG_DEVICE)
172 				>> AC_DEFCFG_DEVICE_SHIFT];
173 }
174 EXPORT_SYMBOL_HDA(snd_hda_get_jack_type);
175 
176 /*
177  * Compose a 32bit command word to be sent to the HD-audio controller
178  */
179 static inline unsigned int
make_codec_cmd(struct hda_codec * codec,hda_nid_t nid,int direct,unsigned int verb,unsigned int parm)180 make_codec_cmd(struct hda_codec *codec, hda_nid_t nid, int direct,
181 	       unsigned int verb, unsigned int parm)
182 {
183 	u32 val;
184 
185 	if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
186 	    (verb & ~0xfff) || (parm & ~0xffff)) {
187 		printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
188 		       codec->addr, direct, nid, verb, parm);
189 		return ~0;
190 	}
191 
192 	val = (u32)codec->addr << 28;
193 	val |= (u32)direct << 27;
194 	val |= (u32)nid << 20;
195 	val |= verb << 8;
196 	val |= parm;
197 	return val;
198 }
199 
200 /*
201  * Send and receive a verb
202  */
codec_exec_verb(struct hda_codec * codec,unsigned int cmd,unsigned int * res)203 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
204 			   unsigned int *res)
205 {
206 	struct hda_bus *bus = codec->bus;
207 	int err;
208 
209 	if (cmd == ~0)
210 		return -1;
211 
212 	if (res)
213 		*res = -1;
214  again:
215 	snd_hda_power_up(codec);
216 	mutex_lock(&bus->cmd_mutex);
217 	trace_hda_send_cmd(codec, cmd);
218 	err = bus->ops.command(bus, cmd);
219 	if (!err && res) {
220 		*res = bus->ops.get_response(bus, codec->addr);
221 		trace_hda_get_response(codec, *res);
222 	}
223 	mutex_unlock(&bus->cmd_mutex);
224 	snd_hda_power_down(codec);
225 	if (res && *res == -1 && bus->rirb_error) {
226 		if (bus->response_reset) {
227 			snd_printd("hda_codec: resetting BUS due to "
228 				   "fatal communication error\n");
229 			trace_hda_bus_reset(bus);
230 			bus->ops.bus_reset(bus);
231 		}
232 		goto again;
233 	}
234 	/* clear reset-flag when the communication gets recovered */
235 	if (!err)
236 		bus->response_reset = 0;
237 	return err;
238 }
239 
240 /**
241  * snd_hda_codec_read - send a command and get the response
242  * @codec: the HDA codec
243  * @nid: NID to send the command
244  * @direct: direct flag
245  * @verb: the verb to send
246  * @parm: the parameter for the verb
247  *
248  * Send a single command and read the corresponding response.
249  *
250  * Returns the obtained response value, or -1 for an error.
251  */
snd_hda_codec_read(struct hda_codec * codec,hda_nid_t nid,int direct,unsigned int verb,unsigned int parm)252 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
253 				int direct,
254 				unsigned int verb, unsigned int parm)
255 {
256 	unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
257 	unsigned int res;
258 	if (codec_exec_verb(codec, cmd, &res))
259 		return -1;
260 	return res;
261 }
262 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
263 
264 /**
265  * snd_hda_codec_write - send a single command without waiting for response
266  * @codec: the HDA codec
267  * @nid: NID to send the command
268  * @direct: direct flag
269  * @verb: the verb to send
270  * @parm: the parameter for the verb
271  *
272  * Send a single command without waiting for response.
273  *
274  * Returns 0 if successful, or a negative error code.
275  */
snd_hda_codec_write(struct hda_codec * codec,hda_nid_t nid,int direct,unsigned int verb,unsigned int parm)276 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
277 			 unsigned int verb, unsigned int parm)
278 {
279 	unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
280 	unsigned int res;
281 	return codec_exec_verb(codec, cmd,
282 			       codec->bus->sync_write ? &res : NULL);
283 }
284 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
285 
286 /**
287  * snd_hda_sequence_write - sequence writes
288  * @codec: the HDA codec
289  * @seq: VERB array to send
290  *
291  * Send the commands sequentially from the given array.
292  * The array must be terminated with NID=0.
293  */
snd_hda_sequence_write(struct hda_codec * codec,const struct hda_verb * seq)294 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
295 {
296 	for (; seq->nid; seq++)
297 		snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
298 }
299 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
300 
301 /**
302  * snd_hda_get_sub_nodes - get the range of sub nodes
303  * @codec: the HDA codec
304  * @nid: NID to parse
305  * @start_id: the pointer to store the start NID
306  *
307  * Parse the NID and store the start NID of its sub-nodes.
308  * Returns the number of sub-nodes.
309  */
snd_hda_get_sub_nodes(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * start_id)310 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
311 			  hda_nid_t *start_id)
312 {
313 	unsigned int parm;
314 
315 	parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
316 	if (parm == -1)
317 		return 0;
318 	*start_id = (parm >> 16) & 0x7fff;
319 	return (int)(parm & 0x7fff);
320 }
321 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
322 
323 /* look up the cached results */
lookup_conn_list(struct snd_array * array,hda_nid_t nid)324 static hda_nid_t *lookup_conn_list(struct snd_array *array, hda_nid_t nid)
325 {
326 	int i, len;
327 	for (i = 0; i < array->used; ) {
328 		hda_nid_t *p = snd_array_elem(array, i);
329 		if (nid == *p)
330 			return p;
331 		len = p[1];
332 		i += len + 2;
333 	}
334 	return NULL;
335 }
336 
337 /**
338  * snd_hda_get_conn_list - get connection list
339  * @codec: the HDA codec
340  * @nid: NID to parse
341  * @listp: the pointer to store NID list
342  *
343  * Parses the connection list of the given widget and stores the list
344  * of NIDs.
345  *
346  * Returns the number of connections, or a negative error code.
347  */
snd_hda_get_conn_list(struct hda_codec * codec,hda_nid_t nid,const hda_nid_t ** listp)348 int snd_hda_get_conn_list(struct hda_codec *codec, hda_nid_t nid,
349 			  const hda_nid_t **listp)
350 {
351 	struct snd_array *array = &codec->conn_lists;
352 	int len, err;
353 	hda_nid_t list[HDA_MAX_CONNECTIONS];
354 	hda_nid_t *p;
355 	bool added = false;
356 
357  again:
358 	/* if the connection-list is already cached, read it */
359 	p = lookup_conn_list(array, nid);
360 	if (p) {
361 		if (listp)
362 			*listp = p + 2;
363 		return p[1];
364 	}
365 	if (snd_BUG_ON(added))
366 		return -EINVAL;
367 
368 	/* read the connection and add to the cache */
369 	len = snd_hda_get_raw_connections(codec, nid, list, HDA_MAX_CONNECTIONS);
370 	if (len < 0)
371 		return len;
372 	err = snd_hda_override_conn_list(codec, nid, len, list);
373 	if (err < 0)
374 		return err;
375 	added = true;
376 	goto again;
377 }
378 EXPORT_SYMBOL_HDA(snd_hda_get_conn_list);
379 
380 /**
381  * snd_hda_get_connections - copy connection list
382  * @codec: the HDA codec
383  * @nid: NID to parse
384  * @conn_list: connection list array
385  * @max_conns: max. number of connections to store
386  *
387  * Parses the connection list of the given widget and stores the list
388  * of NIDs.
389  *
390  * Returns the number of connections, or a negative error code.
391  */
snd_hda_get_connections(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * conn_list,int max_conns)392 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
393 			     hda_nid_t *conn_list, int max_conns)
394 {
395 	const hda_nid_t *list;
396 	int len = snd_hda_get_conn_list(codec, nid, &list);
397 
398 	if (len <= 0)
399 		return len;
400 	if (len > max_conns) {
401 		snd_printk(KERN_ERR "hda_codec: "
402 			   "Too many connections %d for NID 0x%x\n",
403 			   len, nid);
404 		return -EINVAL;
405 	}
406 	memcpy(conn_list, list, len * sizeof(hda_nid_t));
407 	return len;
408 }
409 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
410 
411 /**
412  * snd_hda_get_raw_connections - copy connection list without cache
413  * @codec: the HDA codec
414  * @nid: NID to parse
415  * @conn_list: connection list array
416  * @max_conns: max. number of connections to store
417  *
418  * Like snd_hda_get_connections(), copy the connection list but without
419  * checking through the connection-list cache.
420  * Currently called only from hda_proc.c, so not exported.
421  */
snd_hda_get_raw_connections(struct hda_codec * codec,hda_nid_t nid,hda_nid_t * conn_list,int max_conns)422 int snd_hda_get_raw_connections(struct hda_codec *codec, hda_nid_t nid,
423 				hda_nid_t *conn_list, int max_conns)
424 {
425 	unsigned int parm;
426 	int i, conn_len, conns;
427 	unsigned int shift, num_elems, mask;
428 	unsigned int wcaps;
429 	hda_nid_t prev_nid;
430 
431 	if (snd_BUG_ON(!conn_list || max_conns <= 0))
432 		return -EINVAL;
433 
434 	wcaps = get_wcaps(codec, nid);
435 	if (!(wcaps & AC_WCAP_CONN_LIST) &&
436 	    get_wcaps_type(wcaps) != AC_WID_VOL_KNB)
437 		return 0;
438 
439 	parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
440 	if (parm & AC_CLIST_LONG) {
441 		/* long form */
442 		shift = 16;
443 		num_elems = 2;
444 	} else {
445 		/* short form */
446 		shift = 8;
447 		num_elems = 4;
448 	}
449 	conn_len = parm & AC_CLIST_LENGTH;
450 	mask = (1 << (shift-1)) - 1;
451 
452 	if (!conn_len)
453 		return 0; /* no connection */
454 
455 	if (conn_len == 1) {
456 		/* single connection */
457 		parm = snd_hda_codec_read(codec, nid, 0,
458 					  AC_VERB_GET_CONNECT_LIST, 0);
459 		if (parm == -1 && codec->bus->rirb_error)
460 			return -EIO;
461 		conn_list[0] = parm & mask;
462 		return 1;
463 	}
464 
465 	/* multi connection */
466 	conns = 0;
467 	prev_nid = 0;
468 	for (i = 0; i < conn_len; i++) {
469 		int range_val;
470 		hda_nid_t val, n;
471 
472 		if (i % num_elems == 0) {
473 			parm = snd_hda_codec_read(codec, nid, 0,
474 						  AC_VERB_GET_CONNECT_LIST, i);
475 			if (parm == -1 && codec->bus->rirb_error)
476 				return -EIO;
477 		}
478 		range_val = !!(parm & (1 << (shift-1))); /* ranges */
479 		val = parm & mask;
480 		if (val == 0) {
481 			snd_printk(KERN_WARNING "hda_codec: "
482 				   "invalid CONNECT_LIST verb %x[%i]:%x\n",
483 				    nid, i, parm);
484 			return 0;
485 		}
486 		parm >>= shift;
487 		if (range_val) {
488 			/* ranges between the previous and this one */
489 			if (!prev_nid || prev_nid >= val) {
490 				snd_printk(KERN_WARNING "hda_codec: "
491 					   "invalid dep_range_val %x:%x\n",
492 					   prev_nid, val);
493 				continue;
494 			}
495 			for (n = prev_nid + 1; n <= val; n++) {
496 				if (conns >= max_conns) {
497 					snd_printk(KERN_ERR "hda_codec: "
498 						   "Too many connections %d for NID 0x%x\n",
499 						   conns, nid);
500 					return -EINVAL;
501 				}
502 				conn_list[conns++] = n;
503 			}
504 		} else {
505 			if (conns >= max_conns) {
506 				snd_printk(KERN_ERR "hda_codec: "
507 					   "Too many connections %d for NID 0x%x\n",
508 					   conns, nid);
509 				return -EINVAL;
510 			}
511 			conn_list[conns++] = val;
512 		}
513 		prev_nid = val;
514 	}
515 	return conns;
516 }
517 
add_conn_list(struct snd_array * array,hda_nid_t nid)518 static bool add_conn_list(struct snd_array *array, hda_nid_t nid)
519 {
520 	hda_nid_t *p = snd_array_new(array);
521 	if (!p)
522 		return false;
523 	*p = nid;
524 	return true;
525 }
526 
527 /**
528  * snd_hda_override_conn_list - add/modify the connection-list to cache
529  * @codec: the HDA codec
530  * @nid: NID to parse
531  * @len: number of connection list entries
532  * @list: the list of connection entries
533  *
534  * Add or modify the given connection-list to the cache.  If the corresponding
535  * cache already exists, invalidate it and append a new one.
536  *
537  * Returns zero or a negative error code.
538  */
snd_hda_override_conn_list(struct hda_codec * codec,hda_nid_t nid,int len,const hda_nid_t * list)539 int snd_hda_override_conn_list(struct hda_codec *codec, hda_nid_t nid, int len,
540 			       const hda_nid_t *list)
541 {
542 	struct snd_array *array = &codec->conn_lists;
543 	hda_nid_t *p;
544 	int i, old_used;
545 
546 	p = lookup_conn_list(array, nid);
547 	if (p)
548 		*p = -1; /* invalidate the old entry */
549 
550 	old_used = array->used;
551 	if (!add_conn_list(array, nid) || !add_conn_list(array, len))
552 		goto error_add;
553 	for (i = 0; i < len; i++)
554 		if (!add_conn_list(array, list[i]))
555 			goto error_add;
556 	return 0;
557 
558  error_add:
559 	array->used = old_used;
560 	return -ENOMEM;
561 }
562 EXPORT_SYMBOL_HDA(snd_hda_override_conn_list);
563 
564 /**
565  * snd_hda_get_conn_index - get the connection index of the given NID
566  * @codec: the HDA codec
567  * @mux: NID containing the list
568  * @nid: NID to select
569  * @recursive: 1 when searching NID recursively, otherwise 0
570  *
571  * Parses the connection list of the widget @mux and checks whether the
572  * widget @nid is present.  If it is, return the connection index.
573  * Otherwise it returns -1.
574  */
snd_hda_get_conn_index(struct hda_codec * codec,hda_nid_t mux,hda_nid_t nid,int recursive)575 int snd_hda_get_conn_index(struct hda_codec *codec, hda_nid_t mux,
576 			   hda_nid_t nid, int recursive)
577 {
578 	hda_nid_t conn[HDA_MAX_NUM_INPUTS];
579 	int i, nums;
580 
581 	nums = snd_hda_get_connections(codec, mux, conn, ARRAY_SIZE(conn));
582 	for (i = 0; i < nums; i++)
583 		if (conn[i] == nid)
584 			return i;
585 	if (!recursive)
586 		return -1;
587 	if (recursive > 5) {
588 		snd_printd("hda_codec: too deep connection for 0x%x\n", nid);
589 		return -1;
590 	}
591 	recursive++;
592 	for (i = 0; i < nums; i++) {
593 		unsigned int type = get_wcaps_type(get_wcaps(codec, conn[i]));
594 		if (type == AC_WID_PIN || type == AC_WID_AUD_OUT)
595 			continue;
596 		if (snd_hda_get_conn_index(codec, conn[i], nid, recursive) >= 0)
597 			return i;
598 	}
599 	return -1;
600 }
601 EXPORT_SYMBOL_HDA(snd_hda_get_conn_index);
602 
603 /**
604  * snd_hda_queue_unsol_event - add an unsolicited event to queue
605  * @bus: the BUS
606  * @res: unsolicited event (lower 32bit of RIRB entry)
607  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
608  *
609  * Adds the given event to the queue.  The events are processed in
610  * the workqueue asynchronously.  Call this function in the interrupt
611  * hanlder when RIRB receives an unsolicited event.
612  *
613  * Returns 0 if successful, or a negative error code.
614  */
snd_hda_queue_unsol_event(struct hda_bus * bus,u32 res,u32 res_ex)615 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
616 {
617 	struct hda_bus_unsolicited *unsol;
618 	unsigned int wp;
619 
620 	if (!bus || !bus->workq)
621 		return 0;
622 
623 	trace_hda_unsol_event(bus, res, res_ex);
624 	unsol = bus->unsol;
625 	if (!unsol)
626 		return 0;
627 
628 	wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
629 	unsol->wp = wp;
630 
631 	wp <<= 1;
632 	unsol->queue[wp] = res;
633 	unsol->queue[wp + 1] = res_ex;
634 
635 	queue_work(bus->workq, &unsol->work);
636 
637 	return 0;
638 }
639 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
640 
641 /*
642  * process queued unsolicited events
643  */
process_unsol_events(struct work_struct * work)644 static void process_unsol_events(struct work_struct *work)
645 {
646 	struct hda_bus_unsolicited *unsol =
647 		container_of(work, struct hda_bus_unsolicited, work);
648 	struct hda_bus *bus = unsol->bus;
649 	struct hda_codec *codec;
650 	unsigned int rp, caddr, res;
651 
652 	while (unsol->rp != unsol->wp) {
653 		rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
654 		unsol->rp = rp;
655 		rp <<= 1;
656 		res = unsol->queue[rp];
657 		caddr = unsol->queue[rp + 1];
658 		if (!(caddr & (1 << 4))) /* no unsolicited event? */
659 			continue;
660 		codec = bus->caddr_tbl[caddr & 0x0f];
661 		if (codec && codec->patch_ops.unsol_event)
662 			codec->patch_ops.unsol_event(codec, res);
663 	}
664 }
665 
666 /*
667  * initialize unsolicited queue
668  */
init_unsol_queue(struct hda_bus * bus)669 static int init_unsol_queue(struct hda_bus *bus)
670 {
671 	struct hda_bus_unsolicited *unsol;
672 
673 	if (bus->unsol) /* already initialized */
674 		return 0;
675 
676 	unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
677 	if (!unsol) {
678 		snd_printk(KERN_ERR "hda_codec: "
679 			   "can't allocate unsolicited queue\n");
680 		return -ENOMEM;
681 	}
682 	INIT_WORK(&unsol->work, process_unsol_events);
683 	unsol->bus = bus;
684 	bus->unsol = unsol;
685 	return 0;
686 }
687 
688 /*
689  * destructor
690  */
691 static void snd_hda_codec_free(struct hda_codec *codec);
692 
snd_hda_bus_free(struct hda_bus * bus)693 static int snd_hda_bus_free(struct hda_bus *bus)
694 {
695 	struct hda_codec *codec, *n;
696 
697 	if (!bus)
698 		return 0;
699 	if (bus->workq)
700 		flush_workqueue(bus->workq);
701 	if (bus->unsol)
702 		kfree(bus->unsol);
703 	list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
704 		snd_hda_codec_free(codec);
705 	}
706 	if (bus->ops.private_free)
707 		bus->ops.private_free(bus);
708 	if (bus->workq)
709 		destroy_workqueue(bus->workq);
710 	kfree(bus);
711 	return 0;
712 }
713 
snd_hda_bus_dev_free(struct snd_device * device)714 static int snd_hda_bus_dev_free(struct snd_device *device)
715 {
716 	struct hda_bus *bus = device->device_data;
717 	bus->shutdown = 1;
718 	return snd_hda_bus_free(bus);
719 }
720 
721 #ifdef CONFIG_SND_HDA_HWDEP
snd_hda_bus_dev_register(struct snd_device * device)722 static int snd_hda_bus_dev_register(struct snd_device *device)
723 {
724 	struct hda_bus *bus = device->device_data;
725 	struct hda_codec *codec;
726 	list_for_each_entry(codec, &bus->codec_list, list) {
727 		snd_hda_hwdep_add_sysfs(codec);
728 		snd_hda_hwdep_add_power_sysfs(codec);
729 	}
730 	return 0;
731 }
732 #else
733 #define snd_hda_bus_dev_register	NULL
734 #endif
735 
736 /**
737  * snd_hda_bus_new - create a HDA bus
738  * @card: the card entry
739  * @temp: the template for hda_bus information
740  * @busp: the pointer to store the created bus instance
741  *
742  * Returns 0 if successful, or a negative error code.
743  */
snd_hda_bus_new(struct snd_card * card,const struct hda_bus_template * temp,struct hda_bus ** busp)744 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
745 			      const struct hda_bus_template *temp,
746 			      struct hda_bus **busp)
747 {
748 	struct hda_bus *bus;
749 	int err;
750 	static struct snd_device_ops dev_ops = {
751 		.dev_register = snd_hda_bus_dev_register,
752 		.dev_free = snd_hda_bus_dev_free,
753 	};
754 
755 	if (snd_BUG_ON(!temp))
756 		return -EINVAL;
757 	if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
758 		return -EINVAL;
759 
760 	if (busp)
761 		*busp = NULL;
762 
763 	bus = kzalloc(sizeof(*bus), GFP_KERNEL);
764 	if (bus == NULL) {
765 		snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
766 		return -ENOMEM;
767 	}
768 
769 	bus->card = card;
770 	bus->private_data = temp->private_data;
771 	bus->pci = temp->pci;
772 	bus->modelname = temp->modelname;
773 	bus->power_save = temp->power_save;
774 	bus->ops = temp->ops;
775 
776 	mutex_init(&bus->cmd_mutex);
777 	mutex_init(&bus->prepare_mutex);
778 	INIT_LIST_HEAD(&bus->codec_list);
779 
780 	snprintf(bus->workq_name, sizeof(bus->workq_name),
781 		 "hd-audio%d", card->number);
782 	bus->workq = create_singlethread_workqueue(bus->workq_name);
783 	if (!bus->workq) {
784 		snd_printk(KERN_ERR "cannot create workqueue %s\n",
785 			   bus->workq_name);
786 		kfree(bus);
787 		return -ENOMEM;
788 	}
789 
790 	err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
791 	if (err < 0) {
792 		snd_hda_bus_free(bus);
793 		return err;
794 	}
795 	if (busp)
796 		*busp = bus;
797 	return 0;
798 }
799 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
800 
801 #ifdef CONFIG_SND_HDA_GENERIC
802 #define is_generic_config(codec) \
803 	(codec->modelname && !strcmp(codec->modelname, "generic"))
804 #else
805 #define is_generic_config(codec)	0
806 #endif
807 
808 #ifdef MODULE
809 #define HDA_MODREQ_MAX_COUNT	2	/* two request_modules()'s */
810 #else
811 #define HDA_MODREQ_MAX_COUNT	0	/* all presets are statically linked */
812 #endif
813 
814 /*
815  * find a matching codec preset
816  */
817 static const struct hda_codec_preset *
find_codec_preset(struct hda_codec * codec)818 find_codec_preset(struct hda_codec *codec)
819 {
820 	struct hda_codec_preset_list *tbl;
821 	const struct hda_codec_preset *preset;
822 	int mod_requested = 0;
823 
824 	if (is_generic_config(codec))
825 		return NULL; /* use the generic parser */
826 
827  again:
828 	mutex_lock(&preset_mutex);
829 	list_for_each_entry(tbl, &hda_preset_tables, list) {
830 		if (!try_module_get(tbl->owner)) {
831 			snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
832 			continue;
833 		}
834 		for (preset = tbl->preset; preset->id; preset++) {
835 			u32 mask = preset->mask;
836 			if (preset->afg && preset->afg != codec->afg)
837 				continue;
838 			if (preset->mfg && preset->mfg != codec->mfg)
839 				continue;
840 			if (!mask)
841 				mask = ~0;
842 			if (preset->id == (codec->vendor_id & mask) &&
843 			    (!preset->rev ||
844 			     preset->rev == codec->revision_id)) {
845 				mutex_unlock(&preset_mutex);
846 				codec->owner = tbl->owner;
847 				return preset;
848 			}
849 		}
850 		module_put(tbl->owner);
851 	}
852 	mutex_unlock(&preset_mutex);
853 
854 	if (mod_requested < HDA_MODREQ_MAX_COUNT) {
855 		char name[32];
856 		if (!mod_requested)
857 			snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
858 				 codec->vendor_id);
859 		else
860 			snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
861 				 (codec->vendor_id >> 16) & 0xffff);
862 		request_module(name);
863 		mod_requested++;
864 		goto again;
865 	}
866 	return NULL;
867 }
868 
869 /*
870  * get_codec_name - store the codec name
871  */
get_codec_name(struct hda_codec * codec)872 static int get_codec_name(struct hda_codec *codec)
873 {
874 	const struct hda_vendor_id *c;
875 	const char *vendor = NULL;
876 	u16 vendor_id = codec->vendor_id >> 16;
877 	char tmp[16];
878 
879 	if (codec->vendor_name)
880 		goto get_chip_name;
881 
882 	for (c = hda_vendor_ids; c->id; c++) {
883 		if (c->id == vendor_id) {
884 			vendor = c->name;
885 			break;
886 		}
887 	}
888 	if (!vendor) {
889 		sprintf(tmp, "Generic %04x", vendor_id);
890 		vendor = tmp;
891 	}
892 	codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
893 	if (!codec->vendor_name)
894 		return -ENOMEM;
895 
896  get_chip_name:
897 	if (codec->chip_name)
898 		return 0;
899 
900 	if (codec->preset && codec->preset->name)
901 		codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
902 	else {
903 		sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
904 		codec->chip_name = kstrdup(tmp, GFP_KERNEL);
905 	}
906 	if (!codec->chip_name)
907 		return -ENOMEM;
908 	return 0;
909 }
910 
911 /*
912  * look for an AFG and MFG nodes
913  */
setup_fg_nodes(struct hda_codec * codec)914 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
915 {
916 	int i, total_nodes, function_id;
917 	hda_nid_t nid;
918 
919 	total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
920 	for (i = 0; i < total_nodes; i++, nid++) {
921 		function_id = snd_hda_param_read(codec, nid,
922 						AC_PAR_FUNCTION_TYPE);
923 		switch (function_id & 0xff) {
924 		case AC_GRP_AUDIO_FUNCTION:
925 			codec->afg = nid;
926 			codec->afg_function_id = function_id & 0xff;
927 			codec->afg_unsol = (function_id >> 8) & 1;
928 			break;
929 		case AC_GRP_MODEM_FUNCTION:
930 			codec->mfg = nid;
931 			codec->mfg_function_id = function_id & 0xff;
932 			codec->mfg_unsol = (function_id >> 8) & 1;
933 			break;
934 		default:
935 			break;
936 		}
937 	}
938 }
939 
940 /*
941  * read widget caps for each widget and store in cache
942  */
read_widget_caps(struct hda_codec * codec,hda_nid_t fg_node)943 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
944 {
945 	int i;
946 	hda_nid_t nid;
947 
948 	codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
949 						 &codec->start_nid);
950 	codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
951 	if (!codec->wcaps)
952 		return -ENOMEM;
953 	nid = codec->start_nid;
954 	for (i = 0; i < codec->num_nodes; i++, nid++)
955 		codec->wcaps[i] = snd_hda_param_read(codec, nid,
956 						     AC_PAR_AUDIO_WIDGET_CAP);
957 	return 0;
958 }
959 
960 /* read all pin default configurations and save codec->init_pins */
read_pin_defaults(struct hda_codec * codec)961 static int read_pin_defaults(struct hda_codec *codec)
962 {
963 	int i;
964 	hda_nid_t nid = codec->start_nid;
965 
966 	for (i = 0; i < codec->num_nodes; i++, nid++) {
967 		struct hda_pincfg *pin;
968 		unsigned int wcaps = get_wcaps(codec, nid);
969 		unsigned int wid_type = get_wcaps_type(wcaps);
970 		if (wid_type != AC_WID_PIN)
971 			continue;
972 		pin = snd_array_new(&codec->init_pins);
973 		if (!pin)
974 			return -ENOMEM;
975 		pin->nid = nid;
976 		pin->cfg = snd_hda_codec_read(codec, nid, 0,
977 					      AC_VERB_GET_CONFIG_DEFAULT, 0);
978 		pin->ctrl = snd_hda_codec_read(codec, nid, 0,
979 					       AC_VERB_GET_PIN_WIDGET_CONTROL,
980 					       0);
981 	}
982 	return 0;
983 }
984 
985 /* look up the given pin config list and return the item matching with NID */
look_up_pincfg(struct hda_codec * codec,struct snd_array * array,hda_nid_t nid)986 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
987 					 struct snd_array *array,
988 					 hda_nid_t nid)
989 {
990 	int i;
991 	for (i = 0; i < array->used; i++) {
992 		struct hda_pincfg *pin = snd_array_elem(array, i);
993 		if (pin->nid == nid)
994 			return pin;
995 	}
996 	return NULL;
997 }
998 
999 /* write a config value for the given NID */
set_pincfg(struct hda_codec * codec,hda_nid_t nid,unsigned int cfg)1000 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
1001 		       unsigned int cfg)
1002 {
1003 	int i;
1004 	for (i = 0; i < 4; i++) {
1005 		snd_hda_codec_write(codec, nid, 0,
1006 				    AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
1007 				    cfg & 0xff);
1008 		cfg >>= 8;
1009 	}
1010 }
1011 
1012 /* set the current pin config value for the given NID.
1013  * the value is cached, and read via snd_hda_codec_get_pincfg()
1014  */
snd_hda_add_pincfg(struct hda_codec * codec,struct snd_array * list,hda_nid_t nid,unsigned int cfg)1015 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
1016 		       hda_nid_t nid, unsigned int cfg)
1017 {
1018 	struct hda_pincfg *pin;
1019 	unsigned int oldcfg;
1020 
1021 	if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_PIN)
1022 		return -EINVAL;
1023 
1024 	oldcfg = snd_hda_codec_get_pincfg(codec, nid);
1025 	pin = look_up_pincfg(codec, list, nid);
1026 	if (!pin) {
1027 		pin = snd_array_new(list);
1028 		if (!pin)
1029 			return -ENOMEM;
1030 		pin->nid = nid;
1031 	}
1032 	pin->cfg = cfg;
1033 
1034 	/* change only when needed; e.g. if the pincfg is already present
1035 	 * in user_pins[], don't write it
1036 	 */
1037 	cfg = snd_hda_codec_get_pincfg(codec, nid);
1038 	if (oldcfg != cfg)
1039 		set_pincfg(codec, nid, cfg);
1040 	return 0;
1041 }
1042 
1043 /**
1044  * snd_hda_codec_set_pincfg - Override a pin default configuration
1045  * @codec: the HDA codec
1046  * @nid: NID to set the pin config
1047  * @cfg: the pin default config value
1048  *
1049  * Override a pin default configuration value in the cache.
1050  * This value can be read by snd_hda_codec_get_pincfg() in a higher
1051  * priority than the real hardware value.
1052  */
snd_hda_codec_set_pincfg(struct hda_codec * codec,hda_nid_t nid,unsigned int cfg)1053 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
1054 			     hda_nid_t nid, unsigned int cfg)
1055 {
1056 	return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
1057 }
1058 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
1059 
1060 /**
1061  * snd_hda_codec_get_pincfg - Obtain a pin-default configuration
1062  * @codec: the HDA codec
1063  * @nid: NID to get the pin config
1064  *
1065  * Get the current pin config value of the given pin NID.
1066  * If the pincfg value is cached or overridden via sysfs or driver,
1067  * returns the cached value.
1068  */
snd_hda_codec_get_pincfg(struct hda_codec * codec,hda_nid_t nid)1069 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
1070 {
1071 	struct hda_pincfg *pin;
1072 
1073 #ifdef CONFIG_SND_HDA_HWDEP
1074 	pin = look_up_pincfg(codec, &codec->user_pins, nid);
1075 	if (pin)
1076 		return pin->cfg;
1077 #endif
1078 	pin = look_up_pincfg(codec, &codec->driver_pins, nid);
1079 	if (pin)
1080 		return pin->cfg;
1081 	pin = look_up_pincfg(codec, &codec->init_pins, nid);
1082 	if (pin)
1083 		return pin->cfg;
1084 	return 0;
1085 }
1086 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
1087 
1088 /* restore all current pin configs */
restore_pincfgs(struct hda_codec * codec)1089 static void restore_pincfgs(struct hda_codec *codec)
1090 {
1091 	int i;
1092 	for (i = 0; i < codec->init_pins.used; i++) {
1093 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1094 		set_pincfg(codec, pin->nid,
1095 			   snd_hda_codec_get_pincfg(codec, pin->nid));
1096 	}
1097 }
1098 
1099 /**
1100  * snd_hda_shutup_pins - Shut up all pins
1101  * @codec: the HDA codec
1102  *
1103  * Clear all pin controls to shup up before suspend for avoiding click noise.
1104  * The controls aren't cached so that they can be resumed properly.
1105  */
snd_hda_shutup_pins(struct hda_codec * codec)1106 void snd_hda_shutup_pins(struct hda_codec *codec)
1107 {
1108 	int i;
1109 	/* don't shut up pins when unloading the driver; otherwise it breaks
1110 	 * the default pin setup at the next load of the driver
1111 	 */
1112 	if (codec->bus->shutdown)
1113 		return;
1114 	for (i = 0; i < codec->init_pins.used; i++) {
1115 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1116 		/* use read here for syncing after issuing each verb */
1117 		snd_hda_codec_read(codec, pin->nid, 0,
1118 				   AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
1119 	}
1120 	codec->pins_shutup = 1;
1121 }
1122 EXPORT_SYMBOL_HDA(snd_hda_shutup_pins);
1123 
1124 #ifdef CONFIG_PM
1125 /* Restore the pin controls cleared previously via snd_hda_shutup_pins() */
restore_shutup_pins(struct hda_codec * codec)1126 static void restore_shutup_pins(struct hda_codec *codec)
1127 {
1128 	int i;
1129 	if (!codec->pins_shutup)
1130 		return;
1131 	if (codec->bus->shutdown)
1132 		return;
1133 	for (i = 0; i < codec->init_pins.used; i++) {
1134 		struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
1135 		snd_hda_codec_write(codec, pin->nid, 0,
1136 				    AC_VERB_SET_PIN_WIDGET_CONTROL,
1137 				    pin->ctrl);
1138 	}
1139 	codec->pins_shutup = 0;
1140 }
1141 #endif
1142 
1143 static void init_hda_cache(struct hda_cache_rec *cache,
1144 			   unsigned int record_size);
1145 static void free_hda_cache(struct hda_cache_rec *cache);
1146 
1147 /* restore the initial pin cfgs and release all pincfg lists */
restore_init_pincfgs(struct hda_codec * codec)1148 static void restore_init_pincfgs(struct hda_codec *codec)
1149 {
1150 	/* first free driver_pins and user_pins, then call restore_pincfg
1151 	 * so that only the values in init_pins are restored
1152 	 */
1153 	snd_array_free(&codec->driver_pins);
1154 #ifdef CONFIG_SND_HDA_HWDEP
1155 	snd_array_free(&codec->user_pins);
1156 #endif
1157 	restore_pincfgs(codec);
1158 	snd_array_free(&codec->init_pins);
1159 }
1160 
1161 /*
1162  * audio-converter setup caches
1163  */
1164 struct hda_cvt_setup {
1165 	hda_nid_t nid;
1166 	u8 stream_tag;
1167 	u8 channel_id;
1168 	u16 format_id;
1169 	unsigned char active;	/* cvt is currently used */
1170 	unsigned char dirty;	/* setups should be cleared */
1171 };
1172 
1173 /* get or create a cache entry for the given audio converter NID */
1174 static struct hda_cvt_setup *
get_hda_cvt_setup(struct hda_codec * codec,hda_nid_t nid)1175 get_hda_cvt_setup(struct hda_codec *codec, hda_nid_t nid)
1176 {
1177 	struct hda_cvt_setup *p;
1178 	int i;
1179 
1180 	for (i = 0; i < codec->cvt_setups.used; i++) {
1181 		p = snd_array_elem(&codec->cvt_setups, i);
1182 		if (p->nid == nid)
1183 			return p;
1184 	}
1185 	p = snd_array_new(&codec->cvt_setups);
1186 	if (p)
1187 		p->nid = nid;
1188 	return p;
1189 }
1190 
1191 /*
1192  * codec destructor
1193  */
snd_hda_codec_free(struct hda_codec * codec)1194 static void snd_hda_codec_free(struct hda_codec *codec)
1195 {
1196 	if (!codec)
1197 		return;
1198 	snd_hda_jack_tbl_clear(codec);
1199 	restore_init_pincfgs(codec);
1200 #ifdef CONFIG_SND_HDA_POWER_SAVE
1201 	cancel_delayed_work(&codec->power_work);
1202 	flush_workqueue(codec->bus->workq);
1203 #endif
1204 	list_del(&codec->list);
1205 	snd_array_free(&codec->mixers);
1206 	snd_array_free(&codec->nids);
1207 	snd_array_free(&codec->cvt_setups);
1208 	snd_array_free(&codec->conn_lists);
1209 	snd_array_free(&codec->spdif_out);
1210 	codec->bus->caddr_tbl[codec->addr] = NULL;
1211 	if (codec->patch_ops.free)
1212 		codec->patch_ops.free(codec);
1213 	module_put(codec->owner);
1214 	free_hda_cache(&codec->amp_cache);
1215 	free_hda_cache(&codec->cmd_cache);
1216 	kfree(codec->vendor_name);
1217 	kfree(codec->chip_name);
1218 	kfree(codec->modelname);
1219 	kfree(codec->wcaps);
1220 	kfree(codec);
1221 }
1222 
1223 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1224 				unsigned int power_state);
1225 
1226 /**
1227  * snd_hda_codec_new - create a HDA codec
1228  * @bus: the bus to assign
1229  * @codec_addr: the codec address
1230  * @codecp: the pointer to store the generated codec
1231  *
1232  * Returns 0 if successful, or a negative error code.
1233  */
snd_hda_codec_new(struct hda_bus * bus,unsigned int codec_addr,struct hda_codec ** codecp)1234 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus,
1235 				unsigned int codec_addr,
1236 				struct hda_codec **codecp)
1237 {
1238 	struct hda_codec *codec;
1239 	char component[31];
1240 	int err;
1241 
1242 	if (snd_BUG_ON(!bus))
1243 		return -EINVAL;
1244 	if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
1245 		return -EINVAL;
1246 
1247 	if (bus->caddr_tbl[codec_addr]) {
1248 		snd_printk(KERN_ERR "hda_codec: "
1249 			   "address 0x%x is already occupied\n", codec_addr);
1250 		return -EBUSY;
1251 	}
1252 
1253 	codec = kzalloc(sizeof(*codec), GFP_KERNEL);
1254 	if (codec == NULL) {
1255 		snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
1256 		return -ENOMEM;
1257 	}
1258 
1259 	codec->bus = bus;
1260 	codec->addr = codec_addr;
1261 	mutex_init(&codec->spdif_mutex);
1262 	mutex_init(&codec->control_mutex);
1263 	init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1264 	init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1265 	snd_array_init(&codec->mixers, sizeof(struct hda_nid_item), 32);
1266 	snd_array_init(&codec->nids, sizeof(struct hda_nid_item), 32);
1267 	snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
1268 	snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
1269 	snd_array_init(&codec->cvt_setups, sizeof(struct hda_cvt_setup), 8);
1270 	snd_array_init(&codec->conn_lists, sizeof(hda_nid_t), 64);
1271 	snd_array_init(&codec->spdif_out, sizeof(struct hda_spdif_out), 16);
1272 	if (codec->bus->modelname) {
1273 		codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
1274 		if (!codec->modelname) {
1275 			snd_hda_codec_free(codec);
1276 			return -ENODEV;
1277 		}
1278 	}
1279 
1280 #ifdef CONFIG_SND_HDA_POWER_SAVE
1281 	INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
1282 	/* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
1283 	 * the caller has to power down appropriatley after initialization
1284 	 * phase.
1285 	 */
1286 	hda_keep_power_on(codec);
1287 #endif
1288 
1289 	list_add_tail(&codec->list, &bus->codec_list);
1290 	bus->caddr_tbl[codec_addr] = codec;
1291 
1292 	codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1293 					      AC_PAR_VENDOR_ID);
1294 	if (codec->vendor_id == -1)
1295 		/* read again, hopefully the access method was corrected
1296 		 * in the last read...
1297 		 */
1298 		codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1299 						      AC_PAR_VENDOR_ID);
1300 	codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1301 						 AC_PAR_SUBSYSTEM_ID);
1302 	codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
1303 						AC_PAR_REV_ID);
1304 
1305 	setup_fg_nodes(codec);
1306 	if (!codec->afg && !codec->mfg) {
1307 		snd_printdd("hda_codec: no AFG or MFG node found\n");
1308 		err = -ENODEV;
1309 		goto error;
1310 	}
1311 
1312 	err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
1313 	if (err < 0) {
1314 		snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
1315 		goto error;
1316 	}
1317 	err = read_pin_defaults(codec);
1318 	if (err < 0)
1319 		goto error;
1320 
1321 	if (!codec->subsystem_id) {
1322 		hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
1323 		codec->subsystem_id =
1324 			snd_hda_codec_read(codec, nid, 0,
1325 					   AC_VERB_GET_SUBSYSTEM_ID, 0);
1326 	}
1327 
1328 	/* power-up all before initialization */
1329 	hda_set_power_state(codec,
1330 			    codec->afg ? codec->afg : codec->mfg,
1331 			    AC_PWRST_D0);
1332 
1333 	snd_hda_codec_proc_new(codec);
1334 
1335 	snd_hda_create_hwdep(codec);
1336 
1337 	sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1338 		codec->subsystem_id, codec->revision_id);
1339 	snd_component_add(codec->bus->card, component);
1340 
1341 	if (codecp)
1342 		*codecp = codec;
1343 	return 0;
1344 
1345  error:
1346 	snd_hda_codec_free(codec);
1347 	return err;
1348 }
1349 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1350 
1351 /**
1352  * snd_hda_codec_configure - (Re-)configure the HD-audio codec
1353  * @codec: the HDA codec
1354  *
1355  * Start parsing of the given codec tree and (re-)initialize the whole
1356  * patch instance.
1357  *
1358  * Returns 0 if successful or a negative error code.
1359  */
snd_hda_codec_configure(struct hda_codec * codec)1360 int snd_hda_codec_configure(struct hda_codec *codec)
1361 {
1362 	int err;
1363 
1364 	codec->preset = find_codec_preset(codec);
1365 	if (!codec->vendor_name || !codec->chip_name) {
1366 		err = get_codec_name(codec);
1367 		if (err < 0)
1368 			return err;
1369 	}
1370 
1371 	if (is_generic_config(codec)) {
1372 		err = snd_hda_parse_generic_codec(codec);
1373 		goto patched;
1374 	}
1375 	if (codec->preset && codec->preset->patch) {
1376 		err = codec->preset->patch(codec);
1377 		goto patched;
1378 	}
1379 
1380 	/* call the default parser */
1381 	err = snd_hda_parse_generic_codec(codec);
1382 	if (err < 0)
1383 		printk(KERN_ERR "hda-codec: No codec parser is available\n");
1384 
1385  patched:
1386 	if (!err && codec->patch_ops.unsol_event)
1387 		err = init_unsol_queue(codec->bus);
1388 	/* audio codec should override the mixer name */
1389 	if (!err && (codec->afg || !*codec->bus->card->mixername))
1390 		snprintf(codec->bus->card->mixername,
1391 			 sizeof(codec->bus->card->mixername),
1392 			 "%s %s", codec->vendor_name, codec->chip_name);
1393 	return err;
1394 }
1395 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1396 
1397 /**
1398  * snd_hda_codec_setup_stream - set up the codec for streaming
1399  * @codec: the CODEC to set up
1400  * @nid: the NID to set up
1401  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1402  * @channel_id: channel id to pass, zero based.
1403  * @format: stream format.
1404  */
snd_hda_codec_setup_stream(struct hda_codec * codec,hda_nid_t nid,u32 stream_tag,int channel_id,int format)1405 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1406 				u32 stream_tag,
1407 				int channel_id, int format)
1408 {
1409 	struct hda_codec *c;
1410 	struct hda_cvt_setup *p;
1411 	unsigned int oldval, newval;
1412 	int type;
1413 	int i;
1414 
1415 	if (!nid)
1416 		return;
1417 
1418 	snd_printdd("hda_codec_setup_stream: "
1419 		    "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1420 		    nid, stream_tag, channel_id, format);
1421 	p = get_hda_cvt_setup(codec, nid);
1422 	if (!p)
1423 		return;
1424 	/* update the stream-id if changed */
1425 	if (p->stream_tag != stream_tag || p->channel_id != channel_id) {
1426 		oldval = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0);
1427 		newval = (stream_tag << 4) | channel_id;
1428 		if (oldval != newval)
1429 			snd_hda_codec_write(codec, nid, 0,
1430 					    AC_VERB_SET_CHANNEL_STREAMID,
1431 					    newval);
1432 		p->stream_tag = stream_tag;
1433 		p->channel_id = channel_id;
1434 	}
1435 	/* update the format-id if changed */
1436 	if (p->format_id != format) {
1437 		oldval = snd_hda_codec_read(codec, nid, 0,
1438 					    AC_VERB_GET_STREAM_FORMAT, 0);
1439 		if (oldval != format) {
1440 			msleep(1);
1441 			snd_hda_codec_write(codec, nid, 0,
1442 					    AC_VERB_SET_STREAM_FORMAT,
1443 					    format);
1444 		}
1445 		p->format_id = format;
1446 	}
1447 	p->active = 1;
1448 	p->dirty = 0;
1449 
1450 	/* make other inactive cvts with the same stream-tag dirty */
1451 	type = get_wcaps_type(get_wcaps(codec, nid));
1452 	list_for_each_entry(c, &codec->bus->codec_list, list) {
1453 		for (i = 0; i < c->cvt_setups.used; i++) {
1454 			p = snd_array_elem(&c->cvt_setups, i);
1455 			if (!p->active && p->stream_tag == stream_tag &&
1456 			    get_wcaps_type(get_wcaps(c, p->nid)) == type)
1457 				p->dirty = 1;
1458 		}
1459 	}
1460 }
1461 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1462 
1463 static void really_cleanup_stream(struct hda_codec *codec,
1464 				  struct hda_cvt_setup *q);
1465 
1466 /**
1467  * __snd_hda_codec_cleanup_stream - clean up the codec for closing
1468  * @codec: the CODEC to clean up
1469  * @nid: the NID to clean up
1470  * @do_now: really clean up the stream instead of clearing the active flag
1471  */
__snd_hda_codec_cleanup_stream(struct hda_codec * codec,hda_nid_t nid,int do_now)1472 void __snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid,
1473 				    int do_now)
1474 {
1475 	struct hda_cvt_setup *p;
1476 
1477 	if (!nid)
1478 		return;
1479 
1480 	if (codec->no_sticky_stream)
1481 		do_now = 1;
1482 
1483 	snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1484 	p = get_hda_cvt_setup(codec, nid);
1485 	if (p) {
1486 		/* here we just clear the active flag when do_now isn't set;
1487 		 * actual clean-ups will be done later in
1488 		 * purify_inactive_streams() called from snd_hda_codec_prpapre()
1489 		 */
1490 		if (do_now)
1491 			really_cleanup_stream(codec, p);
1492 		else
1493 			p->active = 0;
1494 	}
1495 }
1496 EXPORT_SYMBOL_HDA(__snd_hda_codec_cleanup_stream);
1497 
really_cleanup_stream(struct hda_codec * codec,struct hda_cvt_setup * q)1498 static void really_cleanup_stream(struct hda_codec *codec,
1499 				  struct hda_cvt_setup *q)
1500 {
1501 	hda_nid_t nid = q->nid;
1502 	if (q->stream_tag || q->channel_id)
1503 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1504 	if (q->format_id)
1505 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0
1506 );
1507 	memset(q, 0, sizeof(*q));
1508 	q->nid = nid;
1509 }
1510 
1511 /* clean up the all conflicting obsolete streams */
purify_inactive_streams(struct hda_codec * codec)1512 static void purify_inactive_streams(struct hda_codec *codec)
1513 {
1514 	struct hda_codec *c;
1515 	int i;
1516 
1517 	list_for_each_entry(c, &codec->bus->codec_list, list) {
1518 		for (i = 0; i < c->cvt_setups.used; i++) {
1519 			struct hda_cvt_setup *p;
1520 			p = snd_array_elem(&c->cvt_setups, i);
1521 			if (p->dirty)
1522 				really_cleanup_stream(c, p);
1523 		}
1524 	}
1525 }
1526 
1527 #ifdef CONFIG_PM
1528 /* clean up all streams; called from suspend */
hda_cleanup_all_streams(struct hda_codec * codec)1529 static void hda_cleanup_all_streams(struct hda_codec *codec)
1530 {
1531 	int i;
1532 
1533 	for (i = 0; i < codec->cvt_setups.used; i++) {
1534 		struct hda_cvt_setup *p = snd_array_elem(&codec->cvt_setups, i);
1535 		if (p->stream_tag)
1536 			really_cleanup_stream(codec, p);
1537 	}
1538 }
1539 #endif
1540 
1541 /*
1542  * amp access functions
1543  */
1544 
1545 /* FIXME: more better hash key? */
1546 #define HDA_HASH_KEY(nid, dir, idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1547 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1548 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1549 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1550 #define INFO_AMP_CAPS	(1<<0)
1551 #define INFO_AMP_VOL(ch)	(1 << (1 + (ch)))
1552 
1553 /* initialize the hash table */
init_hda_cache(struct hda_cache_rec * cache,unsigned int record_size)1554 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1555 				     unsigned int record_size)
1556 {
1557 	memset(cache, 0, sizeof(*cache));
1558 	memset(cache->hash, 0xff, sizeof(cache->hash));
1559 	snd_array_init(&cache->buf, record_size, 64);
1560 }
1561 
free_hda_cache(struct hda_cache_rec * cache)1562 static void free_hda_cache(struct hda_cache_rec *cache)
1563 {
1564 	snd_array_free(&cache->buf);
1565 }
1566 
1567 /* query the hash.  allocate an entry if not found. */
get_hash(struct hda_cache_rec * cache,u32 key)1568 static struct hda_cache_head  *get_hash(struct hda_cache_rec *cache, u32 key)
1569 {
1570 	u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1571 	u16 cur = cache->hash[idx];
1572 	struct hda_cache_head *info;
1573 
1574 	while (cur != 0xffff) {
1575 		info = snd_array_elem(&cache->buf, cur);
1576 		if (info->key == key)
1577 			return info;
1578 		cur = info->next;
1579 	}
1580 	return NULL;
1581 }
1582 
1583 /* query the hash.  allocate an entry if not found. */
get_alloc_hash(struct hda_cache_rec * cache,u32 key)1584 static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache,
1585 					      u32 key)
1586 {
1587 	struct hda_cache_head *info = get_hash(cache, key);
1588 	if (!info) {
1589 		u16 idx, cur;
1590 		/* add a new hash entry */
1591 		info = snd_array_new(&cache->buf);
1592 		if (!info)
1593 			return NULL;
1594 		cur = snd_array_index(&cache->buf, info);
1595 		info->key = key;
1596 		info->val = 0;
1597 		idx = key % (u16)ARRAY_SIZE(cache->hash);
1598 		info->next = cache->hash[idx];
1599 		cache->hash[idx] = cur;
1600 	}
1601 	return info;
1602 }
1603 
1604 /* query and allocate an amp hash entry */
1605 static inline struct hda_amp_info *
get_alloc_amp_hash(struct hda_codec * codec,u32 key)1606 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1607 {
1608 	return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1609 }
1610 
1611 /**
1612  * query_amp_caps - query AMP capabilities
1613  * @codec: the HD-auio codec
1614  * @nid: the NID to query
1615  * @direction: either #HDA_INPUT or #HDA_OUTPUT
1616  *
1617  * Query AMP capabilities for the given widget and direction.
1618  * Returns the obtained capability bits.
1619  *
1620  * When cap bits have been already read, this doesn't read again but
1621  * returns the cached value.
1622  */
query_amp_caps(struct hda_codec * codec,hda_nid_t nid,int direction)1623 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1624 {
1625 	struct hda_amp_info *info;
1626 
1627 	info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1628 	if (!info)
1629 		return 0;
1630 	if (!(info->head.val & INFO_AMP_CAPS)) {
1631 		if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1632 			nid = codec->afg;
1633 		info->amp_caps = snd_hda_param_read(codec, nid,
1634 						    direction == HDA_OUTPUT ?
1635 						    AC_PAR_AMP_OUT_CAP :
1636 						    AC_PAR_AMP_IN_CAP);
1637 		if (info->amp_caps)
1638 			info->head.val |= INFO_AMP_CAPS;
1639 	}
1640 	return info->amp_caps;
1641 }
1642 EXPORT_SYMBOL_HDA(query_amp_caps);
1643 
1644 /**
1645  * snd_hda_override_amp_caps - Override the AMP capabilities
1646  * @codec: the CODEC to clean up
1647  * @nid: the NID to clean up
1648  * @direction: either #HDA_INPUT or #HDA_OUTPUT
1649  * @caps: the capability bits to set
1650  *
1651  * Override the cached AMP caps bits value by the given one.
1652  * This function is useful if the driver needs to adjust the AMP ranges,
1653  * e.g. limit to 0dB, etc.
1654  *
1655  * Returns zero if successful or a negative error code.
1656  */
snd_hda_override_amp_caps(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int caps)1657 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1658 			      unsigned int caps)
1659 {
1660 	struct hda_amp_info *info;
1661 
1662 	info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1663 	if (!info)
1664 		return -EINVAL;
1665 	info->amp_caps = caps;
1666 	info->head.val |= INFO_AMP_CAPS;
1667 	return 0;
1668 }
1669 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1670 
1671 static unsigned int
query_caps_hash(struct hda_codec * codec,hda_nid_t nid,u32 key,unsigned int (* func)(struct hda_codec *,hda_nid_t))1672 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1673 		unsigned int (*func)(struct hda_codec *, hda_nid_t))
1674 {
1675 	struct hda_amp_info *info;
1676 
1677 	info = get_alloc_amp_hash(codec, key);
1678 	if (!info)
1679 		return 0;
1680 	if (!info->head.val) {
1681 		info->head.val |= INFO_AMP_CAPS;
1682 		info->amp_caps = func(codec, nid);
1683 	}
1684 	return info->amp_caps;
1685 }
1686 
read_pin_cap(struct hda_codec * codec,hda_nid_t nid)1687 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1688 {
1689 	return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1690 }
1691 
1692 /**
1693  * snd_hda_query_pin_caps - Query PIN capabilities
1694  * @codec: the HD-auio codec
1695  * @nid: the NID to query
1696  *
1697  * Query PIN capabilities for the given widget.
1698  * Returns the obtained capability bits.
1699  *
1700  * When cap bits have been already read, this doesn't read again but
1701  * returns the cached value.
1702  */
snd_hda_query_pin_caps(struct hda_codec * codec,hda_nid_t nid)1703 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1704 {
1705 	return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1706 			       read_pin_cap);
1707 }
1708 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1709 
1710 /**
1711  * snd_hda_override_pin_caps - Override the pin capabilities
1712  * @codec: the CODEC
1713  * @nid: the NID to override
1714  * @caps: the capability bits to set
1715  *
1716  * Override the cached PIN capabilitiy bits value by the given one.
1717  *
1718  * Returns zero if successful or a negative error code.
1719  */
snd_hda_override_pin_caps(struct hda_codec * codec,hda_nid_t nid,unsigned int caps)1720 int snd_hda_override_pin_caps(struct hda_codec *codec, hda_nid_t nid,
1721 			      unsigned int caps)
1722 {
1723 	struct hda_amp_info *info;
1724 	info = get_alloc_amp_hash(codec, HDA_HASH_PINCAP_KEY(nid));
1725 	if (!info)
1726 		return -ENOMEM;
1727 	info->amp_caps = caps;
1728 	info->head.val |= INFO_AMP_CAPS;
1729 	return 0;
1730 }
1731 EXPORT_SYMBOL_HDA(snd_hda_override_pin_caps);
1732 
1733 /*
1734  * read the current volume to info
1735  * if the cache exists, read the cache value.
1736  */
get_vol_mute(struct hda_codec * codec,struct hda_amp_info * info,hda_nid_t nid,int ch,int direction,int index)1737 static unsigned int get_vol_mute(struct hda_codec *codec,
1738 				 struct hda_amp_info *info, hda_nid_t nid,
1739 				 int ch, int direction, int index)
1740 {
1741 	u32 val, parm;
1742 
1743 	if (info->head.val & INFO_AMP_VOL(ch))
1744 		return info->vol[ch];
1745 
1746 	parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1747 	parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1748 	parm |= index;
1749 	val = snd_hda_codec_read(codec, nid, 0,
1750 				 AC_VERB_GET_AMP_GAIN_MUTE, parm);
1751 	info->vol[ch] = val & 0xff;
1752 	info->head.val |= INFO_AMP_VOL(ch);
1753 	return info->vol[ch];
1754 }
1755 
1756 /*
1757  * write the current volume in info to the h/w and update the cache
1758  */
put_vol_mute(struct hda_codec * codec,struct hda_amp_info * info,hda_nid_t nid,int ch,int direction,int index,int val)1759 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1760 			 hda_nid_t nid, int ch, int direction, int index,
1761 			 int val)
1762 {
1763 	u32 parm;
1764 
1765 	parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1766 	parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1767 	parm |= index << AC_AMP_SET_INDEX_SHIFT;
1768 	if ((val & HDA_AMP_MUTE) && !(info->amp_caps & AC_AMPCAP_MUTE) &&
1769 	    (info->amp_caps & AC_AMPCAP_MIN_MUTE))
1770 		; /* set the zero value as a fake mute */
1771 	else
1772 		parm |= val;
1773 	snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1774 	info->vol[ch] = val;
1775 }
1776 
1777 /**
1778  * snd_hda_codec_amp_read - Read AMP value
1779  * @codec: HD-audio codec
1780  * @nid: NID to read the AMP value
1781  * @ch: channel (left=0 or right=1)
1782  * @direction: #HDA_INPUT or #HDA_OUTPUT
1783  * @index: the index value (only for input direction)
1784  *
1785  * Read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
1786  */
snd_hda_codec_amp_read(struct hda_codec * codec,hda_nid_t nid,int ch,int direction,int index)1787 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1788 			   int direction, int index)
1789 {
1790 	struct hda_amp_info *info;
1791 	info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1792 	if (!info)
1793 		return 0;
1794 	return get_vol_mute(codec, info, nid, ch, direction, index);
1795 }
1796 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1797 
1798 /**
1799  * snd_hda_codec_amp_update - update the AMP value
1800  * @codec: HD-audio codec
1801  * @nid: NID to read the AMP value
1802  * @ch: channel (left=0 or right=1)
1803  * @direction: #HDA_INPUT or #HDA_OUTPUT
1804  * @idx: the index value (only for input direction)
1805  * @mask: bit mask to set
1806  * @val: the bits value to set
1807  *
1808  * Update the AMP value with a bit mask.
1809  * Returns 0 if the value is unchanged, 1 if changed.
1810  */
snd_hda_codec_amp_update(struct hda_codec * codec,hda_nid_t nid,int ch,int direction,int idx,int mask,int val)1811 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1812 			     int direction, int idx, int mask, int val)
1813 {
1814 	struct hda_amp_info *info;
1815 
1816 	info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1817 	if (!info)
1818 		return 0;
1819 	if (snd_BUG_ON(mask & ~0xff))
1820 		mask &= 0xff;
1821 	val &= mask;
1822 	val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1823 	if (info->vol[ch] == val)
1824 		return 0;
1825 	put_vol_mute(codec, info, nid, ch, direction, idx, val);
1826 	return 1;
1827 }
1828 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1829 
1830 /**
1831  * snd_hda_codec_amp_stereo - update the AMP stereo values
1832  * @codec: HD-audio codec
1833  * @nid: NID to read the AMP value
1834  * @direction: #HDA_INPUT or #HDA_OUTPUT
1835  * @idx: the index value (only for input direction)
1836  * @mask: bit mask to set
1837  * @val: the bits value to set
1838  *
1839  * Update the AMP values like snd_hda_codec_amp_update(), but for a
1840  * stereo widget with the same mask and value.
1841  */
snd_hda_codec_amp_stereo(struct hda_codec * codec,hda_nid_t nid,int direction,int idx,int mask,int val)1842 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1843 			     int direction, int idx, int mask, int val)
1844 {
1845 	int ch, ret = 0;
1846 
1847 	if (snd_BUG_ON(mask & ~0xff))
1848 		mask &= 0xff;
1849 	for (ch = 0; ch < 2; ch++)
1850 		ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1851 						idx, mask, val);
1852 	return ret;
1853 }
1854 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1855 
1856 #ifdef CONFIG_PM
1857 /**
1858  * snd_hda_codec_resume_amp - Resume all AMP commands from the cache
1859  * @codec: HD-audio codec
1860  *
1861  * Resume the all amp commands from the cache.
1862  */
snd_hda_codec_resume_amp(struct hda_codec * codec)1863 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1864 {
1865 	struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1866 	int i;
1867 
1868 	for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1869 		u32 key = buffer->head.key;
1870 		hda_nid_t nid;
1871 		unsigned int idx, dir, ch;
1872 		if (!key)
1873 			continue;
1874 		nid = key & 0xff;
1875 		idx = (key >> 16) & 0xff;
1876 		dir = (key >> 24) & 0xff;
1877 		for (ch = 0; ch < 2; ch++) {
1878 			if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1879 				continue;
1880 			put_vol_mute(codec, buffer, nid, ch, dir, idx,
1881 				     buffer->vol[ch]);
1882 		}
1883 	}
1884 }
1885 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1886 #endif /* CONFIG_PM */
1887 
get_amp_max_value(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int ofs)1888 static u32 get_amp_max_value(struct hda_codec *codec, hda_nid_t nid, int dir,
1889 			     unsigned int ofs)
1890 {
1891 	u32 caps = query_amp_caps(codec, nid, dir);
1892 	/* get num steps */
1893 	caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1894 	if (ofs < caps)
1895 		caps -= ofs;
1896 	return caps;
1897 }
1898 
1899 /**
1900  * snd_hda_mixer_amp_volume_info - Info callback for a standard AMP mixer
1901  *
1902  * The control element is supposed to have the private_value field
1903  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1904  */
snd_hda_mixer_amp_volume_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1905 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1906 				  struct snd_ctl_elem_info *uinfo)
1907 {
1908 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1909 	u16 nid = get_amp_nid(kcontrol);
1910 	u8 chs = get_amp_channels(kcontrol);
1911 	int dir = get_amp_direction(kcontrol);
1912 	unsigned int ofs = get_amp_offset(kcontrol);
1913 
1914 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1915 	uinfo->count = chs == 3 ? 2 : 1;
1916 	uinfo->value.integer.min = 0;
1917 	uinfo->value.integer.max = get_amp_max_value(codec, nid, dir, ofs);
1918 	if (!uinfo->value.integer.max) {
1919 		printk(KERN_WARNING "hda_codec: "
1920 		       "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1921 		       kcontrol->id.name);
1922 		return -EINVAL;
1923 	}
1924 	return 0;
1925 }
1926 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1927 
1928 
1929 static inline unsigned int
read_amp_value(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,unsigned int ofs)1930 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1931 	       int ch, int dir, int idx, unsigned int ofs)
1932 {
1933 	unsigned int val;
1934 	val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1935 	val &= HDA_AMP_VOLMASK;
1936 	if (val >= ofs)
1937 		val -= ofs;
1938 	else
1939 		val = 0;
1940 	return val;
1941 }
1942 
1943 static inline int
update_amp_value(struct hda_codec * codec,hda_nid_t nid,int ch,int dir,int idx,unsigned int ofs,unsigned int val)1944 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1945 		 int ch, int dir, int idx, unsigned int ofs,
1946 		 unsigned int val)
1947 {
1948 	unsigned int maxval;
1949 
1950 	if (val > 0)
1951 		val += ofs;
1952 	/* ofs = 0: raw max value */
1953 	maxval = get_amp_max_value(codec, nid, dir, 0);
1954 	if (val > maxval)
1955 		val = maxval;
1956 	return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1957 					HDA_AMP_VOLMASK, val);
1958 }
1959 
1960 /**
1961  * snd_hda_mixer_amp_volume_get - Get callback for a standard AMP mixer volume
1962  *
1963  * The control element is supposed to have the private_value field
1964  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1965  */
snd_hda_mixer_amp_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1966 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1967 				 struct snd_ctl_elem_value *ucontrol)
1968 {
1969 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1970 	hda_nid_t nid = get_amp_nid(kcontrol);
1971 	int chs = get_amp_channels(kcontrol);
1972 	int dir = get_amp_direction(kcontrol);
1973 	int idx = get_amp_index(kcontrol);
1974 	unsigned int ofs = get_amp_offset(kcontrol);
1975 	long *valp = ucontrol->value.integer.value;
1976 
1977 	if (chs & 1)
1978 		*valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1979 	if (chs & 2)
1980 		*valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1981 	return 0;
1982 }
1983 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1984 
1985 /**
1986  * snd_hda_mixer_amp_volume_put - Put callback for a standard AMP mixer volume
1987  *
1988  * The control element is supposed to have the private_value field
1989  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
1990  */
snd_hda_mixer_amp_volume_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1991 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1992 				 struct snd_ctl_elem_value *ucontrol)
1993 {
1994 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1995 	hda_nid_t nid = get_amp_nid(kcontrol);
1996 	int chs = get_amp_channels(kcontrol);
1997 	int dir = get_amp_direction(kcontrol);
1998 	int idx = get_amp_index(kcontrol);
1999 	unsigned int ofs = get_amp_offset(kcontrol);
2000 	long *valp = ucontrol->value.integer.value;
2001 	int change = 0;
2002 
2003 	snd_hda_power_up(codec);
2004 	if (chs & 1) {
2005 		change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
2006 		valp++;
2007 	}
2008 	if (chs & 2)
2009 		change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
2010 	snd_hda_power_down(codec);
2011 	return change;
2012 }
2013 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
2014 
2015 /**
2016  * snd_hda_mixer_amp_volume_put - TLV callback for a standard AMP mixer volume
2017  *
2018  * The control element is supposed to have the private_value field
2019  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2020  */
snd_hda_mixer_amp_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * _tlv)2021 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2022 			  unsigned int size, unsigned int __user *_tlv)
2023 {
2024 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2025 	hda_nid_t nid = get_amp_nid(kcontrol);
2026 	int dir = get_amp_direction(kcontrol);
2027 	unsigned int ofs = get_amp_offset(kcontrol);
2028 	bool min_mute = get_amp_min_mute(kcontrol);
2029 	u32 caps, val1, val2;
2030 
2031 	if (size < 4 * sizeof(unsigned int))
2032 		return -ENOMEM;
2033 	caps = query_amp_caps(codec, nid, dir);
2034 	val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2035 	val2 = (val2 + 1) * 25;
2036 	val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
2037 	val1 += ofs;
2038 	val1 = ((int)val1) * ((int)val2);
2039 	if (min_mute || (caps & AC_AMPCAP_MIN_MUTE))
2040 		val2 |= TLV_DB_SCALE_MUTE;
2041 	if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
2042 		return -EFAULT;
2043 	if (put_user(2 * sizeof(unsigned int), _tlv + 1))
2044 		return -EFAULT;
2045 	if (put_user(val1, _tlv + 2))
2046 		return -EFAULT;
2047 	if (put_user(val2, _tlv + 3))
2048 		return -EFAULT;
2049 	return 0;
2050 }
2051 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
2052 
2053 /**
2054  * snd_hda_set_vmaster_tlv - Set TLV for a virtual master control
2055  * @codec: HD-audio codec
2056  * @nid: NID of a reference widget
2057  * @dir: #HDA_INPUT or #HDA_OUTPUT
2058  * @tlv: TLV data to be stored, at least 4 elements
2059  *
2060  * Set (static) TLV data for a virtual master volume using the AMP caps
2061  * obtained from the reference NID.
2062  * The volume range is recalculated as if the max volume is 0dB.
2063  */
snd_hda_set_vmaster_tlv(struct hda_codec * codec,hda_nid_t nid,int dir,unsigned int * tlv)2064 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
2065 			     unsigned int *tlv)
2066 {
2067 	u32 caps;
2068 	int nums, step;
2069 
2070 	caps = query_amp_caps(codec, nid, dir);
2071 	nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
2072 	step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
2073 	step = (step + 1) * 25;
2074 	tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
2075 	tlv[1] = 2 * sizeof(unsigned int);
2076 	tlv[2] = -nums * step;
2077 	tlv[3] = step;
2078 }
2079 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
2080 
2081 /* find a mixer control element with the given name */
2082 static struct snd_kcontrol *
_snd_hda_find_mixer_ctl(struct hda_codec * codec,const char * name,int idx)2083 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
2084 			const char *name, int idx)
2085 {
2086 	struct snd_ctl_elem_id id;
2087 	memset(&id, 0, sizeof(id));
2088 	id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
2089 	id.index = idx;
2090 	if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
2091 		return NULL;
2092 	strcpy(id.name, name);
2093 	return snd_ctl_find_id(codec->bus->card, &id);
2094 }
2095 
2096 /**
2097  * snd_hda_find_mixer_ctl - Find a mixer control element with the given name
2098  * @codec: HD-audio codec
2099  * @name: ctl id name string
2100  *
2101  * Get the control element with the given id string and IFACE_MIXER.
2102  */
snd_hda_find_mixer_ctl(struct hda_codec * codec,const char * name)2103 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
2104 					    const char *name)
2105 {
2106 	return _snd_hda_find_mixer_ctl(codec, name, 0);
2107 }
2108 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
2109 
find_empty_mixer_ctl_idx(struct hda_codec * codec,const char * name)2110 static int find_empty_mixer_ctl_idx(struct hda_codec *codec, const char *name)
2111 {
2112 	int idx;
2113 	for (idx = 0; idx < 16; idx++) { /* 16 ctlrs should be large enough */
2114 		if (!_snd_hda_find_mixer_ctl(codec, name, idx))
2115 			return idx;
2116 	}
2117 	return -EBUSY;
2118 }
2119 
2120 /**
2121  * snd_hda_ctl_add - Add a control element and assign to the codec
2122  * @codec: HD-audio codec
2123  * @nid: corresponding NID (optional)
2124  * @kctl: the control element to assign
2125  *
2126  * Add the given control element to an array inside the codec instance.
2127  * All control elements belonging to a codec are supposed to be added
2128  * by this function so that a proper clean-up works at the free or
2129  * reconfiguration time.
2130  *
2131  * If non-zero @nid is passed, the NID is assigned to the control element.
2132  * The assignment is shown in the codec proc file.
2133  *
2134  * snd_hda_ctl_add() checks the control subdev id field whether
2135  * #HDA_SUBDEV_NID_FLAG bit is set.  If set (and @nid is zero), the lower
2136  * bits value is taken as the NID to assign. The #HDA_NID_ITEM_AMP bit
2137  * specifies if kctl->private_value is a HDA amplifier value.
2138  */
snd_hda_ctl_add(struct hda_codec * codec,hda_nid_t nid,struct snd_kcontrol * kctl)2139 int snd_hda_ctl_add(struct hda_codec *codec, hda_nid_t nid,
2140 		    struct snd_kcontrol *kctl)
2141 {
2142 	int err;
2143 	unsigned short flags = 0;
2144 	struct hda_nid_item *item;
2145 
2146 	if (kctl->id.subdevice & HDA_SUBDEV_AMP_FLAG) {
2147 		flags |= HDA_NID_ITEM_AMP;
2148 		if (nid == 0)
2149 			nid = get_amp_nid_(kctl->private_value);
2150 	}
2151 	if ((kctl->id.subdevice & HDA_SUBDEV_NID_FLAG) != 0 && nid == 0)
2152 		nid = kctl->id.subdevice & 0xffff;
2153 	if (kctl->id.subdevice & (HDA_SUBDEV_NID_FLAG|HDA_SUBDEV_AMP_FLAG))
2154 		kctl->id.subdevice = 0;
2155 	err = snd_ctl_add(codec->bus->card, kctl);
2156 	if (err < 0)
2157 		return err;
2158 	item = snd_array_new(&codec->mixers);
2159 	if (!item)
2160 		return -ENOMEM;
2161 	item->kctl = kctl;
2162 	item->nid = nid;
2163 	item->flags = flags;
2164 	return 0;
2165 }
2166 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
2167 
2168 /**
2169  * snd_hda_add_nid - Assign a NID to a control element
2170  * @codec: HD-audio codec
2171  * @nid: corresponding NID (optional)
2172  * @kctl: the control element to assign
2173  * @index: index to kctl
2174  *
2175  * Add the given control element to an array inside the codec instance.
2176  * This function is used when #snd_hda_ctl_add cannot be used for 1:1
2177  * NID:KCTL mapping - for example "Capture Source" selector.
2178  */
snd_hda_add_nid(struct hda_codec * codec,struct snd_kcontrol * kctl,unsigned int index,hda_nid_t nid)2179 int snd_hda_add_nid(struct hda_codec *codec, struct snd_kcontrol *kctl,
2180 		    unsigned int index, hda_nid_t nid)
2181 {
2182 	struct hda_nid_item *item;
2183 
2184 	if (nid > 0) {
2185 		item = snd_array_new(&codec->nids);
2186 		if (!item)
2187 			return -ENOMEM;
2188 		item->kctl = kctl;
2189 		item->index = index;
2190 		item->nid = nid;
2191 		return 0;
2192 	}
2193 	printk(KERN_ERR "hda-codec: no NID for mapping control %s:%d:%d\n",
2194 	       kctl->id.name, kctl->id.index, index);
2195 	return -EINVAL;
2196 }
2197 EXPORT_SYMBOL_HDA(snd_hda_add_nid);
2198 
2199 /**
2200  * snd_hda_ctls_clear - Clear all controls assigned to the given codec
2201  * @codec: HD-audio codec
2202  */
snd_hda_ctls_clear(struct hda_codec * codec)2203 void snd_hda_ctls_clear(struct hda_codec *codec)
2204 {
2205 	int i;
2206 	struct hda_nid_item *items = codec->mixers.list;
2207 	for (i = 0; i < codec->mixers.used; i++)
2208 		snd_ctl_remove(codec->bus->card, items[i].kctl);
2209 	snd_array_free(&codec->mixers);
2210 	snd_array_free(&codec->nids);
2211 }
2212 
2213 /* pseudo device locking
2214  * toggle card->shutdown to allow/disallow the device access (as a hack)
2215  */
hda_lock_devices(struct snd_card * card)2216 static int hda_lock_devices(struct snd_card *card)
2217 {
2218 	spin_lock(&card->files_lock);
2219 	if (card->shutdown) {
2220 		spin_unlock(&card->files_lock);
2221 		return -EINVAL;
2222 	}
2223 	card->shutdown = 1;
2224 	spin_unlock(&card->files_lock);
2225 	return 0;
2226 }
2227 
hda_unlock_devices(struct snd_card * card)2228 static void hda_unlock_devices(struct snd_card *card)
2229 {
2230 	spin_lock(&card->files_lock);
2231 	card->shutdown = 0;
2232 	spin_unlock(&card->files_lock);
2233 }
2234 
2235 /**
2236  * snd_hda_codec_reset - Clear all objects assigned to the codec
2237  * @codec: HD-audio codec
2238  *
2239  * This frees the all PCM and control elements assigned to the codec, and
2240  * clears the caches and restores the pin default configurations.
2241  *
2242  * When a device is being used, it returns -EBSY.  If successfully freed,
2243  * returns zero.
2244  */
snd_hda_codec_reset(struct hda_codec * codec)2245 int snd_hda_codec_reset(struct hda_codec *codec)
2246 {
2247 	struct snd_card *card = codec->bus->card;
2248 	int i, pcm;
2249 
2250 	if (hda_lock_devices(card) < 0)
2251 		return -EBUSY;
2252 	/* check whether the codec isn't used by any mixer or PCM streams */
2253 	if (!list_empty(&card->ctl_files)) {
2254 		hda_unlock_devices(card);
2255 		return -EBUSY;
2256 	}
2257 	for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2258 		struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2259 		if (!cpcm->pcm)
2260 			continue;
2261 		if (cpcm->pcm->streams[0].substream_opened ||
2262 		    cpcm->pcm->streams[1].substream_opened) {
2263 			hda_unlock_devices(card);
2264 			return -EBUSY;
2265 		}
2266 	}
2267 
2268 	/* OK, let it free */
2269 
2270 #ifdef CONFIG_SND_HDA_POWER_SAVE
2271 	cancel_delayed_work(&codec->power_work);
2272 	flush_workqueue(codec->bus->workq);
2273 #endif
2274 	snd_hda_ctls_clear(codec);
2275 	/* relase PCMs */
2276 	for (i = 0; i < codec->num_pcms; i++) {
2277 		if (codec->pcm_info[i].pcm) {
2278 			snd_device_free(card, codec->pcm_info[i].pcm);
2279 			clear_bit(codec->pcm_info[i].device,
2280 				  codec->bus->pcm_dev_bits);
2281 		}
2282 	}
2283 	if (codec->patch_ops.free)
2284 		codec->patch_ops.free(codec);
2285 	memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
2286 	snd_hda_jack_tbl_clear(codec);
2287 	codec->proc_widget_hook = NULL;
2288 	codec->spec = NULL;
2289 	free_hda_cache(&codec->amp_cache);
2290 	free_hda_cache(&codec->cmd_cache);
2291 	init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
2292 	init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
2293 	/* free only driver_pins so that init_pins + user_pins are restored */
2294 	snd_array_free(&codec->driver_pins);
2295 	restore_pincfgs(codec);
2296 	codec->num_pcms = 0;
2297 	codec->pcm_info = NULL;
2298 	codec->preset = NULL;
2299 	codec->slave_dig_outs = NULL;
2300 	codec->spdif_status_reset = 0;
2301 	module_put(codec->owner);
2302 	codec->owner = NULL;
2303 
2304 	/* allow device access again */
2305 	hda_unlock_devices(card);
2306 	return 0;
2307 }
2308 
2309 typedef int (*map_slave_func_t)(void *, struct snd_kcontrol *);
2310 
2311 /* apply the function to all matching slave ctls in the mixer list */
map_slaves(struct hda_codec * codec,const char * const * slaves,const char * suffix,map_slave_func_t func,void * data)2312 static int map_slaves(struct hda_codec *codec, const char * const *slaves,
2313 		      const char *suffix, map_slave_func_t func, void *data)
2314 {
2315 	struct hda_nid_item *items;
2316 	const char * const *s;
2317 	int i, err;
2318 
2319 	items = codec->mixers.list;
2320 	for (i = 0; i < codec->mixers.used; i++) {
2321 		struct snd_kcontrol *sctl = items[i].kctl;
2322 		if (!sctl || !sctl->id.name ||
2323 		    sctl->id.iface != SNDRV_CTL_ELEM_IFACE_MIXER)
2324 			continue;
2325 		for (s = slaves; *s; s++) {
2326 			char tmpname[sizeof(sctl->id.name)];
2327 			const char *name = *s;
2328 			if (suffix) {
2329 				snprintf(tmpname, sizeof(tmpname), "%s %s",
2330 					 name, suffix);
2331 				name = tmpname;
2332 			}
2333 			if (!strcmp(sctl->id.name, name)) {
2334 				err = func(data, sctl);
2335 				if (err)
2336 					return err;
2337 				break;
2338 			}
2339 		}
2340 	}
2341 	return 0;
2342 }
2343 
check_slave_present(void * data,struct snd_kcontrol * sctl)2344 static int check_slave_present(void *data, struct snd_kcontrol *sctl)
2345 {
2346 	return 1;
2347 }
2348 
2349 /* guess the value corresponding to 0dB */
get_kctl_0dB_offset(struct snd_kcontrol * kctl)2350 static int get_kctl_0dB_offset(struct snd_kcontrol *kctl)
2351 {
2352 	int _tlv[4];
2353 	const int *tlv = NULL;
2354 	int val = -1;
2355 
2356 	if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK) {
2357 		/* FIXME: set_fs() hack for obtaining user-space TLV data */
2358 		mm_segment_t fs = get_fs();
2359 		set_fs(get_ds());
2360 		if (!kctl->tlv.c(kctl, 0, sizeof(_tlv), _tlv))
2361 			tlv = _tlv;
2362 		set_fs(fs);
2363 	} else if (kctl->vd[0].access & SNDRV_CTL_ELEM_ACCESS_TLV_READ)
2364 		tlv = kctl->tlv.p;
2365 	if (tlv && tlv[0] == SNDRV_CTL_TLVT_DB_SCALE)
2366 		val = -tlv[2] / tlv[3];
2367 	return val;
2368 }
2369 
2370 /* call kctl->put with the given value(s) */
put_kctl_with_value(struct snd_kcontrol * kctl,int val)2371 static int put_kctl_with_value(struct snd_kcontrol *kctl, int val)
2372 {
2373 	struct snd_ctl_elem_value *ucontrol;
2374 	ucontrol = kzalloc(sizeof(*ucontrol), GFP_KERNEL);
2375 	if (!ucontrol)
2376 		return -ENOMEM;
2377 	ucontrol->value.integer.value[0] = val;
2378 	ucontrol->value.integer.value[1] = val;
2379 	kctl->put(kctl, ucontrol);
2380 	kfree(ucontrol);
2381 	return 0;
2382 }
2383 
2384 /* initialize the slave volume with 0dB */
init_slave_0dB(void * data,struct snd_kcontrol * slave)2385 static int init_slave_0dB(void *data, struct snd_kcontrol *slave)
2386 {
2387 	int offset = get_kctl_0dB_offset(slave);
2388 	if (offset > 0)
2389 		put_kctl_with_value(slave, offset);
2390 	return 0;
2391 }
2392 
2393 /* unmute the slave */
init_slave_unmute(void * data,struct snd_kcontrol * slave)2394 static int init_slave_unmute(void *data, struct snd_kcontrol *slave)
2395 {
2396 	return put_kctl_with_value(slave, 1);
2397 }
2398 
2399 /**
2400  * snd_hda_add_vmaster - create a virtual master control and add slaves
2401  * @codec: HD-audio codec
2402  * @name: vmaster control name
2403  * @tlv: TLV data (optional)
2404  * @slaves: slave control names (optional)
2405  * @suffix: suffix string to each slave name (optional)
2406  * @init_slave_vol: initialize slaves to unmute/0dB
2407  * @ctl_ret: store the vmaster kcontrol in return
2408  *
2409  * Create a virtual master control with the given name.  The TLV data
2410  * must be either NULL or a valid data.
2411  *
2412  * @slaves is a NULL-terminated array of strings, each of which is a
2413  * slave control name.  All controls with these names are assigned to
2414  * the new virtual master control.
2415  *
2416  * This function returns zero if successful or a negative error code.
2417  */
__snd_hda_add_vmaster(struct hda_codec * codec,char * name,unsigned int * tlv,const char * const * slaves,const char * suffix,bool init_slave_vol,struct snd_kcontrol ** ctl_ret)2418 int __snd_hda_add_vmaster(struct hda_codec *codec, char *name,
2419 			unsigned int *tlv, const char * const *slaves,
2420 			  const char *suffix, bool init_slave_vol,
2421 			  struct snd_kcontrol **ctl_ret)
2422 {
2423 	struct snd_kcontrol *kctl;
2424 	int err;
2425 
2426 	if (ctl_ret)
2427 		*ctl_ret = NULL;
2428 
2429 	err = map_slaves(codec, slaves, suffix, check_slave_present, NULL);
2430 	if (err != 1) {
2431 		snd_printdd("No slave found for %s\n", name);
2432 		return 0;
2433 	}
2434 	kctl = snd_ctl_make_virtual_master(name, tlv);
2435 	if (!kctl)
2436 		return -ENOMEM;
2437 	err = snd_hda_ctl_add(codec, 0, kctl);
2438 	if (err < 0)
2439 		return err;
2440 
2441 	err = map_slaves(codec, slaves, suffix,
2442 			 (map_slave_func_t)snd_ctl_add_slave, kctl);
2443 	if (err < 0)
2444 		return err;
2445 
2446 	/* init with master mute & zero volume */
2447 	put_kctl_with_value(kctl, 0);
2448 	if (init_slave_vol)
2449 		map_slaves(codec, slaves, suffix,
2450 			   tlv ? init_slave_0dB : init_slave_unmute, kctl);
2451 
2452 	if (ctl_ret)
2453 		*ctl_ret = kctl;
2454 	return 0;
2455 }
2456 EXPORT_SYMBOL_HDA(__snd_hda_add_vmaster);
2457 
2458 /*
2459  * mute-LED control using vmaster
2460  */
vmaster_mute_mode_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2461 static int vmaster_mute_mode_info(struct snd_kcontrol *kcontrol,
2462 				  struct snd_ctl_elem_info *uinfo)
2463 {
2464 	static const char * const texts[] = {
2465 		"Off", "On", "Follow Master"
2466 	};
2467 	unsigned int index;
2468 
2469 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2470 	uinfo->count = 1;
2471 	uinfo->value.enumerated.items = 3;
2472 	index = uinfo->value.enumerated.item;
2473 	if (index >= 3)
2474 		index = 2;
2475 	strcpy(uinfo->value.enumerated.name, texts[index]);
2476 	return 0;
2477 }
2478 
vmaster_mute_mode_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2479 static int vmaster_mute_mode_get(struct snd_kcontrol *kcontrol,
2480 				 struct snd_ctl_elem_value *ucontrol)
2481 {
2482 	struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2483 	ucontrol->value.enumerated.item[0] = hook->mute_mode;
2484 	return 0;
2485 }
2486 
vmaster_mute_mode_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2487 static int vmaster_mute_mode_put(struct snd_kcontrol *kcontrol,
2488 				 struct snd_ctl_elem_value *ucontrol)
2489 {
2490 	struct hda_vmaster_mute_hook *hook = snd_kcontrol_chip(kcontrol);
2491 	unsigned int old_mode = hook->mute_mode;
2492 
2493 	hook->mute_mode = ucontrol->value.enumerated.item[0];
2494 	if (hook->mute_mode > HDA_VMUTE_FOLLOW_MASTER)
2495 		hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2496 	if (old_mode == hook->mute_mode)
2497 		return 0;
2498 	snd_hda_sync_vmaster_hook(hook);
2499 	return 1;
2500 }
2501 
2502 static struct snd_kcontrol_new vmaster_mute_mode = {
2503 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2504 	.name = "Mute-LED Mode",
2505 	.info = vmaster_mute_mode_info,
2506 	.get = vmaster_mute_mode_get,
2507 	.put = vmaster_mute_mode_put,
2508 };
2509 
2510 /*
2511  * Add a mute-LED hook with the given vmaster switch kctl
2512  * "Mute-LED Mode" control is automatically created and associated with
2513  * the given hook.
2514  */
snd_hda_add_vmaster_hook(struct hda_codec * codec,struct hda_vmaster_mute_hook * hook,bool expose_enum_ctl)2515 int snd_hda_add_vmaster_hook(struct hda_codec *codec,
2516 			     struct hda_vmaster_mute_hook *hook,
2517 			     bool expose_enum_ctl)
2518 {
2519 	struct snd_kcontrol *kctl;
2520 
2521 	if (!hook->hook || !hook->sw_kctl)
2522 		return 0;
2523 	snd_ctl_add_vmaster_hook(hook->sw_kctl, hook->hook, codec);
2524 	hook->codec = codec;
2525 	hook->mute_mode = HDA_VMUTE_FOLLOW_MASTER;
2526 	if (!expose_enum_ctl)
2527 		return 0;
2528 	kctl = snd_ctl_new1(&vmaster_mute_mode, hook);
2529 	if (!kctl)
2530 		return -ENOMEM;
2531 	return snd_hda_ctl_add(codec, 0, kctl);
2532 }
2533 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster_hook);
2534 
2535 /*
2536  * Call the hook with the current value for synchronization
2537  * Should be called in init callback
2538  */
snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook * hook)2539 void snd_hda_sync_vmaster_hook(struct hda_vmaster_mute_hook *hook)
2540 {
2541 	if (!hook->hook || !hook->codec)
2542 		return;
2543 	switch (hook->mute_mode) {
2544 	case HDA_VMUTE_FOLLOW_MASTER:
2545 		snd_ctl_sync_vmaster_hook(hook->sw_kctl);
2546 		break;
2547 	default:
2548 		hook->hook(hook->codec, hook->mute_mode);
2549 		break;
2550 	}
2551 }
2552 EXPORT_SYMBOL_HDA(snd_hda_sync_vmaster_hook);
2553 
2554 
2555 /**
2556  * snd_hda_mixer_amp_switch_info - Info callback for a standard AMP mixer switch
2557  *
2558  * The control element is supposed to have the private_value field
2559  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2560  */
snd_hda_mixer_amp_switch_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2561 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
2562 				  struct snd_ctl_elem_info *uinfo)
2563 {
2564 	int chs = get_amp_channels(kcontrol);
2565 
2566 	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
2567 	uinfo->count = chs == 3 ? 2 : 1;
2568 	uinfo->value.integer.min = 0;
2569 	uinfo->value.integer.max = 1;
2570 	return 0;
2571 }
2572 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
2573 
2574 /**
2575  * snd_hda_mixer_amp_switch_get - Get callback for a standard AMP mixer switch
2576  *
2577  * The control element is supposed to have the private_value field
2578  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2579  */
snd_hda_mixer_amp_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2580 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
2581 				 struct snd_ctl_elem_value *ucontrol)
2582 {
2583 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2584 	hda_nid_t nid = get_amp_nid(kcontrol);
2585 	int chs = get_amp_channels(kcontrol);
2586 	int dir = get_amp_direction(kcontrol);
2587 	int idx = get_amp_index(kcontrol);
2588 	long *valp = ucontrol->value.integer.value;
2589 
2590 	if (chs & 1)
2591 		*valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
2592 			   HDA_AMP_MUTE) ? 0 : 1;
2593 	if (chs & 2)
2594 		*valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
2595 			 HDA_AMP_MUTE) ? 0 : 1;
2596 	return 0;
2597 }
2598 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
2599 
2600 /**
2601  * snd_hda_mixer_amp_switch_put - Put callback for a standard AMP mixer switch
2602  *
2603  * The control element is supposed to have the private_value field
2604  * set up via HDA_COMPOSE_AMP_VAL*() or related macros.
2605  */
snd_hda_mixer_amp_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2606 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
2607 				 struct snd_ctl_elem_value *ucontrol)
2608 {
2609 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2610 	hda_nid_t nid = get_amp_nid(kcontrol);
2611 	int chs = get_amp_channels(kcontrol);
2612 	int dir = get_amp_direction(kcontrol);
2613 	int idx = get_amp_index(kcontrol);
2614 	long *valp = ucontrol->value.integer.value;
2615 	int change = 0;
2616 
2617 	snd_hda_power_up(codec);
2618 	if (chs & 1) {
2619 		change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
2620 						  HDA_AMP_MUTE,
2621 						  *valp ? 0 : HDA_AMP_MUTE);
2622 		valp++;
2623 	}
2624 	if (chs & 2)
2625 		change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
2626 						   HDA_AMP_MUTE,
2627 						   *valp ? 0 : HDA_AMP_MUTE);
2628 	hda_call_check_power_status(codec, nid);
2629 	snd_hda_power_down(codec);
2630 	return change;
2631 }
2632 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
2633 
2634 #ifdef CONFIG_SND_HDA_INPUT_BEEP
2635 /**
2636  * snd_hda_mixer_amp_switch_put_beep - Put callback for a beep AMP switch
2637  *
2638  * This function calls snd_hda_enable_beep_device(), which behaves differently
2639  * depending on beep_mode option.
2640  */
snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2641 int snd_hda_mixer_amp_switch_put_beep(struct snd_kcontrol *kcontrol,
2642 				      struct snd_ctl_elem_value *ucontrol)
2643 {
2644 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2645 	long *valp = ucontrol->value.integer.value;
2646 
2647 	snd_hda_enable_beep_device(codec, *valp);
2648 	return snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2649 }
2650 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put_beep);
2651 #endif /* CONFIG_SND_HDA_INPUT_BEEP */
2652 
2653 /*
2654  * bound volume controls
2655  *
2656  * bind multiple volumes (# indices, from 0)
2657  */
2658 
2659 #define AMP_VAL_IDX_SHIFT	19
2660 #define AMP_VAL_IDX_MASK	(0x0f<<19)
2661 
2662 /**
2663  * snd_hda_mixer_bind_switch_get - Get callback for a bound volume control
2664  *
2665  * The control element is supposed to have the private_value field
2666  * set up via HDA_BIND_MUTE*() macros.
2667  */
snd_hda_mixer_bind_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2668 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
2669 				  struct snd_ctl_elem_value *ucontrol)
2670 {
2671 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2672 	unsigned long pval;
2673 	int err;
2674 
2675 	mutex_lock(&codec->control_mutex);
2676 	pval = kcontrol->private_value;
2677 	kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
2678 	err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
2679 	kcontrol->private_value = pval;
2680 	mutex_unlock(&codec->control_mutex);
2681 	return err;
2682 }
2683 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
2684 
2685 /**
2686  * snd_hda_mixer_bind_switch_put - Put callback for a bound volume control
2687  *
2688  * The control element is supposed to have the private_value field
2689  * set up via HDA_BIND_MUTE*() macros.
2690  */
snd_hda_mixer_bind_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2691 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
2692 				  struct snd_ctl_elem_value *ucontrol)
2693 {
2694 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2695 	unsigned long pval;
2696 	int i, indices, err = 0, change = 0;
2697 
2698 	mutex_lock(&codec->control_mutex);
2699 	pval = kcontrol->private_value;
2700 	indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
2701 	for (i = 0; i < indices; i++) {
2702 		kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
2703 			(i << AMP_VAL_IDX_SHIFT);
2704 		err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
2705 		if (err < 0)
2706 			break;
2707 		change |= err;
2708 	}
2709 	kcontrol->private_value = pval;
2710 	mutex_unlock(&codec->control_mutex);
2711 	return err < 0 ? err : change;
2712 }
2713 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
2714 
2715 /**
2716  * snd_hda_mixer_bind_ctls_info - Info callback for a generic bound control
2717  *
2718  * The control element is supposed to have the private_value field
2719  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2720  */
snd_hda_mixer_bind_ctls_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2721 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
2722 				 struct snd_ctl_elem_info *uinfo)
2723 {
2724 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2725 	struct hda_bind_ctls *c;
2726 	int err;
2727 
2728 	mutex_lock(&codec->control_mutex);
2729 	c = (struct hda_bind_ctls *)kcontrol->private_value;
2730 	kcontrol->private_value = *c->values;
2731 	err = c->ops->info(kcontrol, uinfo);
2732 	kcontrol->private_value = (long)c;
2733 	mutex_unlock(&codec->control_mutex);
2734 	return err;
2735 }
2736 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
2737 
2738 /**
2739  * snd_hda_mixer_bind_ctls_get - Get callback for a generic bound control
2740  *
2741  * The control element is supposed to have the private_value field
2742  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2743  */
snd_hda_mixer_bind_ctls_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2744 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
2745 				struct snd_ctl_elem_value *ucontrol)
2746 {
2747 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2748 	struct hda_bind_ctls *c;
2749 	int err;
2750 
2751 	mutex_lock(&codec->control_mutex);
2752 	c = (struct hda_bind_ctls *)kcontrol->private_value;
2753 	kcontrol->private_value = *c->values;
2754 	err = c->ops->get(kcontrol, ucontrol);
2755 	kcontrol->private_value = (long)c;
2756 	mutex_unlock(&codec->control_mutex);
2757 	return err;
2758 }
2759 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
2760 
2761 /**
2762  * snd_hda_mixer_bind_ctls_put - Put callback for a generic bound control
2763  *
2764  * The control element is supposed to have the private_value field
2765  * set up via HDA_BIND_VOL() or HDA_BIND_SW() macros.
2766  */
snd_hda_mixer_bind_ctls_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2767 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
2768 				struct snd_ctl_elem_value *ucontrol)
2769 {
2770 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2771 	struct hda_bind_ctls *c;
2772 	unsigned long *vals;
2773 	int err = 0, change = 0;
2774 
2775 	mutex_lock(&codec->control_mutex);
2776 	c = (struct hda_bind_ctls *)kcontrol->private_value;
2777 	for (vals = c->values; *vals; vals++) {
2778 		kcontrol->private_value = *vals;
2779 		err = c->ops->put(kcontrol, ucontrol);
2780 		if (err < 0)
2781 			break;
2782 		change |= err;
2783 	}
2784 	kcontrol->private_value = (long)c;
2785 	mutex_unlock(&codec->control_mutex);
2786 	return err < 0 ? err : change;
2787 }
2788 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
2789 
2790 /**
2791  * snd_hda_mixer_bind_tlv - TLV callback for a generic bound control
2792  *
2793  * The control element is supposed to have the private_value field
2794  * set up via HDA_BIND_VOL() macro.
2795  */
snd_hda_mixer_bind_tlv(struct snd_kcontrol * kcontrol,int op_flag,unsigned int size,unsigned int __user * tlv)2796 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
2797 			   unsigned int size, unsigned int __user *tlv)
2798 {
2799 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2800 	struct hda_bind_ctls *c;
2801 	int err;
2802 
2803 	mutex_lock(&codec->control_mutex);
2804 	c = (struct hda_bind_ctls *)kcontrol->private_value;
2805 	kcontrol->private_value = *c->values;
2806 	err = c->ops->tlv(kcontrol, op_flag, size, tlv);
2807 	kcontrol->private_value = (long)c;
2808 	mutex_unlock(&codec->control_mutex);
2809 	return err;
2810 }
2811 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
2812 
2813 struct hda_ctl_ops snd_hda_bind_vol = {
2814 	.info = snd_hda_mixer_amp_volume_info,
2815 	.get = snd_hda_mixer_amp_volume_get,
2816 	.put = snd_hda_mixer_amp_volume_put,
2817 	.tlv = snd_hda_mixer_amp_tlv
2818 };
2819 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
2820 
2821 struct hda_ctl_ops snd_hda_bind_sw = {
2822 	.info = snd_hda_mixer_amp_switch_info,
2823 	.get = snd_hda_mixer_amp_switch_get,
2824 	.put = snd_hda_mixer_amp_switch_put,
2825 	.tlv = snd_hda_mixer_amp_tlv
2826 };
2827 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
2828 
2829 /*
2830  * SPDIF out controls
2831  */
2832 
snd_hda_spdif_mask_info(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)2833 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
2834 				   struct snd_ctl_elem_info *uinfo)
2835 {
2836 	uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
2837 	uinfo->count = 1;
2838 	return 0;
2839 }
2840 
snd_hda_spdif_cmask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2841 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
2842 				   struct snd_ctl_elem_value *ucontrol)
2843 {
2844 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2845 					   IEC958_AES0_NONAUDIO |
2846 					   IEC958_AES0_CON_EMPHASIS_5015 |
2847 					   IEC958_AES0_CON_NOT_COPYRIGHT;
2848 	ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
2849 					   IEC958_AES1_CON_ORIGINAL;
2850 	return 0;
2851 }
2852 
snd_hda_spdif_pmask_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2853 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
2854 				   struct snd_ctl_elem_value *ucontrol)
2855 {
2856 	ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
2857 					   IEC958_AES0_NONAUDIO |
2858 					   IEC958_AES0_PRO_EMPHASIS_5015;
2859 	return 0;
2860 }
2861 
snd_hda_spdif_default_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2862 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
2863 				     struct snd_ctl_elem_value *ucontrol)
2864 {
2865 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2866 	int idx = kcontrol->private_value;
2867 	struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2868 
2869 	ucontrol->value.iec958.status[0] = spdif->status & 0xff;
2870 	ucontrol->value.iec958.status[1] = (spdif->status >> 8) & 0xff;
2871 	ucontrol->value.iec958.status[2] = (spdif->status >> 16) & 0xff;
2872 	ucontrol->value.iec958.status[3] = (spdif->status >> 24) & 0xff;
2873 
2874 	return 0;
2875 }
2876 
2877 /* convert from SPDIF status bits to HDA SPDIF bits
2878  * bit 0 (DigEn) is always set zero (to be filled later)
2879  */
convert_from_spdif_status(unsigned int sbits)2880 static unsigned short convert_from_spdif_status(unsigned int sbits)
2881 {
2882 	unsigned short val = 0;
2883 
2884 	if (sbits & IEC958_AES0_PROFESSIONAL)
2885 		val |= AC_DIG1_PROFESSIONAL;
2886 	if (sbits & IEC958_AES0_NONAUDIO)
2887 		val |= AC_DIG1_NONAUDIO;
2888 	if (sbits & IEC958_AES0_PROFESSIONAL) {
2889 		if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
2890 		    IEC958_AES0_PRO_EMPHASIS_5015)
2891 			val |= AC_DIG1_EMPHASIS;
2892 	} else {
2893 		if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
2894 		    IEC958_AES0_CON_EMPHASIS_5015)
2895 			val |= AC_DIG1_EMPHASIS;
2896 		if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
2897 			val |= AC_DIG1_COPYRIGHT;
2898 		if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
2899 			val |= AC_DIG1_LEVEL;
2900 		val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
2901 	}
2902 	return val;
2903 }
2904 
2905 /* convert to SPDIF status bits from HDA SPDIF bits
2906  */
convert_to_spdif_status(unsigned short val)2907 static unsigned int convert_to_spdif_status(unsigned short val)
2908 {
2909 	unsigned int sbits = 0;
2910 
2911 	if (val & AC_DIG1_NONAUDIO)
2912 		sbits |= IEC958_AES0_NONAUDIO;
2913 	if (val & AC_DIG1_PROFESSIONAL)
2914 		sbits |= IEC958_AES0_PROFESSIONAL;
2915 	if (sbits & IEC958_AES0_PROFESSIONAL) {
2916 		if (val & AC_DIG1_EMPHASIS)
2917 			sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
2918 	} else {
2919 		if (val & AC_DIG1_EMPHASIS)
2920 			sbits |= IEC958_AES0_CON_EMPHASIS_5015;
2921 		if (!(val & AC_DIG1_COPYRIGHT))
2922 			sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
2923 		if (val & AC_DIG1_LEVEL)
2924 			sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
2925 		sbits |= val & (0x7f << 8);
2926 	}
2927 	return sbits;
2928 }
2929 
2930 /* set digital convert verbs both for the given NID and its slaves */
set_dig_out(struct hda_codec * codec,hda_nid_t nid,int verb,int val)2931 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
2932 			int verb, int val)
2933 {
2934 	const hda_nid_t *d;
2935 
2936 	snd_hda_codec_write_cache(codec, nid, 0, verb, val);
2937 	d = codec->slave_dig_outs;
2938 	if (!d)
2939 		return;
2940 	for (; *d; d++)
2941 		snd_hda_codec_write_cache(codec, *d, 0, verb, val);
2942 }
2943 
set_dig_out_convert(struct hda_codec * codec,hda_nid_t nid,int dig1,int dig2)2944 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
2945 				       int dig1, int dig2)
2946 {
2947 	if (dig1 != -1)
2948 		set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
2949 	if (dig2 != -1)
2950 		set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
2951 }
2952 
snd_hda_spdif_default_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2953 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
2954 				     struct snd_ctl_elem_value *ucontrol)
2955 {
2956 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2957 	int idx = kcontrol->private_value;
2958 	struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2959 	hda_nid_t nid = spdif->nid;
2960 	unsigned short val;
2961 	int change;
2962 
2963 	mutex_lock(&codec->spdif_mutex);
2964 	spdif->status = ucontrol->value.iec958.status[0] |
2965 		((unsigned int)ucontrol->value.iec958.status[1] << 8) |
2966 		((unsigned int)ucontrol->value.iec958.status[2] << 16) |
2967 		((unsigned int)ucontrol->value.iec958.status[3] << 24);
2968 	val = convert_from_spdif_status(spdif->status);
2969 	val |= spdif->ctls & 1;
2970 	change = spdif->ctls != val;
2971 	spdif->ctls = val;
2972 	if (change && nid != (u16)-1)
2973 		set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2974 	mutex_unlock(&codec->spdif_mutex);
2975 	return change;
2976 }
2977 
2978 #define snd_hda_spdif_out_switch_info	snd_ctl_boolean_mono_info
2979 
snd_hda_spdif_out_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)2980 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2981 					struct snd_ctl_elem_value *ucontrol)
2982 {
2983 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2984 	int idx = kcontrol->private_value;
2985 	struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
2986 
2987 	ucontrol->value.integer.value[0] = spdif->ctls & AC_DIG1_ENABLE;
2988 	return 0;
2989 }
2990 
set_spdif_ctls(struct hda_codec * codec,hda_nid_t nid,int dig1,int dig2)2991 static inline void set_spdif_ctls(struct hda_codec *codec, hda_nid_t nid,
2992 				  int dig1, int dig2)
2993 {
2994 	set_dig_out_convert(codec, nid, dig1, dig2);
2995 	/* unmute amp switch (if any) */
2996 	if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2997 	    (dig1 & AC_DIG1_ENABLE))
2998 		snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2999 					    HDA_AMP_MUTE, 0);
3000 }
3001 
snd_hda_spdif_out_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3002 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
3003 					struct snd_ctl_elem_value *ucontrol)
3004 {
3005 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3006 	int idx = kcontrol->private_value;
3007 	struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3008 	hda_nid_t nid = spdif->nid;
3009 	unsigned short val;
3010 	int change;
3011 
3012 	mutex_lock(&codec->spdif_mutex);
3013 	val = spdif->ctls & ~AC_DIG1_ENABLE;
3014 	if (ucontrol->value.integer.value[0])
3015 		val |= AC_DIG1_ENABLE;
3016 	change = spdif->ctls != val;
3017 	spdif->ctls = val;
3018 	if (change && nid != (u16)-1)
3019 		set_spdif_ctls(codec, nid, val & 0xff, -1);
3020 	mutex_unlock(&codec->spdif_mutex);
3021 	return change;
3022 }
3023 
3024 static struct snd_kcontrol_new dig_mixes[] = {
3025 	{
3026 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
3027 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3028 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, CON_MASK),
3029 		.info = snd_hda_spdif_mask_info,
3030 		.get = snd_hda_spdif_cmask_get,
3031 	},
3032 	{
3033 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
3034 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3035 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, PRO_MASK),
3036 		.info = snd_hda_spdif_mask_info,
3037 		.get = snd_hda_spdif_pmask_get,
3038 	},
3039 	{
3040 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3041 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
3042 		.info = snd_hda_spdif_mask_info,
3043 		.get = snd_hda_spdif_default_get,
3044 		.put = snd_hda_spdif_default_put,
3045 	},
3046 	{
3047 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3048 		.name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
3049 		.info = snd_hda_spdif_out_switch_info,
3050 		.get = snd_hda_spdif_out_switch_get,
3051 		.put = snd_hda_spdif_out_switch_put,
3052 	},
3053 	{ } /* end */
3054 };
3055 
3056 /**
3057  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
3058  * @codec: the HDA codec
3059  * @nid: audio out widget NID
3060  *
3061  * Creates controls related with the SPDIF output.
3062  * Called from each patch supporting the SPDIF out.
3063  *
3064  * Returns 0 if successful, or a negative error code.
3065  */
snd_hda_create_spdif_out_ctls(struct hda_codec * codec,hda_nid_t associated_nid,hda_nid_t cvt_nid)3066 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec,
3067 				  hda_nid_t associated_nid,
3068 				  hda_nid_t cvt_nid)
3069 {
3070 	int err;
3071 	struct snd_kcontrol *kctl;
3072 	struct snd_kcontrol_new *dig_mix;
3073 	int idx;
3074 	struct hda_spdif_out *spdif;
3075 
3076 	idx = find_empty_mixer_ctl_idx(codec, "IEC958 Playback Switch");
3077 	if (idx < 0) {
3078 		printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
3079 		return -EBUSY;
3080 	}
3081 	spdif = snd_array_new(&codec->spdif_out);
3082 	for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
3083 		kctl = snd_ctl_new1(dig_mix, codec);
3084 		if (!kctl)
3085 			return -ENOMEM;
3086 		kctl->id.index = idx;
3087 		kctl->private_value = codec->spdif_out.used - 1;
3088 		err = snd_hda_ctl_add(codec, associated_nid, kctl);
3089 		if (err < 0)
3090 			return err;
3091 	}
3092 	spdif->nid = cvt_nid;
3093 	spdif->ctls = snd_hda_codec_read(codec, cvt_nid, 0,
3094 					 AC_VERB_GET_DIGI_CONVERT_1, 0);
3095 	spdif->status = convert_to_spdif_status(spdif->ctls);
3096 	return 0;
3097 }
3098 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
3099 
snd_hda_spdif_out_of_nid(struct hda_codec * codec,hda_nid_t nid)3100 struct hda_spdif_out *snd_hda_spdif_out_of_nid(struct hda_codec *codec,
3101 					       hda_nid_t nid)
3102 {
3103 	int i;
3104 	for (i = 0; i < codec->spdif_out.used; i++) {
3105 		struct hda_spdif_out *spdif =
3106 				snd_array_elem(&codec->spdif_out, i);
3107 		if (spdif->nid == nid)
3108 			return spdif;
3109 	}
3110 	return NULL;
3111 }
3112 EXPORT_SYMBOL_HDA(snd_hda_spdif_out_of_nid);
3113 
snd_hda_spdif_ctls_unassign(struct hda_codec * codec,int idx)3114 void snd_hda_spdif_ctls_unassign(struct hda_codec *codec, int idx)
3115 {
3116 	struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3117 
3118 	mutex_lock(&codec->spdif_mutex);
3119 	spdif->nid = (u16)-1;
3120 	mutex_unlock(&codec->spdif_mutex);
3121 }
3122 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_unassign);
3123 
snd_hda_spdif_ctls_assign(struct hda_codec * codec,int idx,hda_nid_t nid)3124 void snd_hda_spdif_ctls_assign(struct hda_codec *codec, int idx, hda_nid_t nid)
3125 {
3126 	struct hda_spdif_out *spdif = snd_array_elem(&codec->spdif_out, idx);
3127 	unsigned short val;
3128 
3129 	mutex_lock(&codec->spdif_mutex);
3130 	if (spdif->nid != nid) {
3131 		spdif->nid = nid;
3132 		val = spdif->ctls;
3133 		set_spdif_ctls(codec, nid, val & 0xff, (val >> 8) & 0xff);
3134 	}
3135 	mutex_unlock(&codec->spdif_mutex);
3136 }
3137 EXPORT_SYMBOL_HDA(snd_hda_spdif_ctls_assign);
3138 
3139 /*
3140  * SPDIF sharing with analog output
3141  */
spdif_share_sw_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3142 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
3143 			      struct snd_ctl_elem_value *ucontrol)
3144 {
3145 	struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3146 	ucontrol->value.integer.value[0] = mout->share_spdif;
3147 	return 0;
3148 }
3149 
spdif_share_sw_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3150 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
3151 			      struct snd_ctl_elem_value *ucontrol)
3152 {
3153 	struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
3154 	mout->share_spdif = !!ucontrol->value.integer.value[0];
3155 	return 0;
3156 }
3157 
3158 static struct snd_kcontrol_new spdif_share_sw = {
3159 	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3160 	.name = "IEC958 Default PCM Playback Switch",
3161 	.info = snd_ctl_boolean_mono_info,
3162 	.get = spdif_share_sw_get,
3163 	.put = spdif_share_sw_put,
3164 };
3165 
3166 /**
3167  * snd_hda_create_spdif_share_sw - create Default PCM switch
3168  * @codec: the HDA codec
3169  * @mout: multi-out instance
3170  */
snd_hda_create_spdif_share_sw(struct hda_codec * codec,struct hda_multi_out * mout)3171 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
3172 				  struct hda_multi_out *mout)
3173 {
3174 	if (!mout->dig_out_nid)
3175 		return 0;
3176 	/* ATTENTION: here mout is passed as private_data, instead of codec */
3177 	return snd_hda_ctl_add(codec, mout->dig_out_nid,
3178 			      snd_ctl_new1(&spdif_share_sw, mout));
3179 }
3180 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
3181 
3182 /*
3183  * SPDIF input
3184  */
3185 
3186 #define snd_hda_spdif_in_switch_info	snd_hda_spdif_out_switch_info
3187 
snd_hda_spdif_in_switch_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3188 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
3189 				       struct snd_ctl_elem_value *ucontrol)
3190 {
3191 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3192 
3193 	ucontrol->value.integer.value[0] = codec->spdif_in_enable;
3194 	return 0;
3195 }
3196 
snd_hda_spdif_in_switch_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3197 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
3198 				       struct snd_ctl_elem_value *ucontrol)
3199 {
3200 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3201 	hda_nid_t nid = kcontrol->private_value;
3202 	unsigned int val = !!ucontrol->value.integer.value[0];
3203 	int change;
3204 
3205 	mutex_lock(&codec->spdif_mutex);
3206 	change = codec->spdif_in_enable != val;
3207 	if (change) {
3208 		codec->spdif_in_enable = val;
3209 		snd_hda_codec_write_cache(codec, nid, 0,
3210 					  AC_VERB_SET_DIGI_CONVERT_1, val);
3211 	}
3212 	mutex_unlock(&codec->spdif_mutex);
3213 	return change;
3214 }
3215 
snd_hda_spdif_in_status_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)3216 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
3217 				       struct snd_ctl_elem_value *ucontrol)
3218 {
3219 	struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
3220 	hda_nid_t nid = kcontrol->private_value;
3221 	unsigned short val;
3222 	unsigned int sbits;
3223 
3224 	val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
3225 	sbits = convert_to_spdif_status(val);
3226 	ucontrol->value.iec958.status[0] = sbits;
3227 	ucontrol->value.iec958.status[1] = sbits >> 8;
3228 	ucontrol->value.iec958.status[2] = sbits >> 16;
3229 	ucontrol->value.iec958.status[3] = sbits >> 24;
3230 	return 0;
3231 }
3232 
3233 static struct snd_kcontrol_new dig_in_ctls[] = {
3234 	{
3235 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3236 		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, SWITCH),
3237 		.info = snd_hda_spdif_in_switch_info,
3238 		.get = snd_hda_spdif_in_switch_get,
3239 		.put = snd_hda_spdif_in_switch_put,
3240 	},
3241 	{
3242 		.access = SNDRV_CTL_ELEM_ACCESS_READ,
3243 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
3244 		.name = SNDRV_CTL_NAME_IEC958("", CAPTURE, DEFAULT),
3245 		.info = snd_hda_spdif_mask_info,
3246 		.get = snd_hda_spdif_in_status_get,
3247 	},
3248 	{ } /* end */
3249 };
3250 
3251 /**
3252  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
3253  * @codec: the HDA codec
3254  * @nid: audio in widget NID
3255  *
3256  * Creates controls related with the SPDIF input.
3257  * Called from each patch supporting the SPDIF in.
3258  *
3259  * Returns 0 if successful, or a negative error code.
3260  */
snd_hda_create_spdif_in_ctls(struct hda_codec * codec,hda_nid_t nid)3261 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
3262 {
3263 	int err;
3264 	struct snd_kcontrol *kctl;
3265 	struct snd_kcontrol_new *dig_mix;
3266 	int idx;
3267 
3268 	idx = find_empty_mixer_ctl_idx(codec, "IEC958 Capture Switch");
3269 	if (idx < 0) {
3270 		printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
3271 		return -EBUSY;
3272 	}
3273 	for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
3274 		kctl = snd_ctl_new1(dig_mix, codec);
3275 		if (!kctl)
3276 			return -ENOMEM;
3277 		kctl->private_value = nid;
3278 		err = snd_hda_ctl_add(codec, nid, kctl);
3279 		if (err < 0)
3280 			return err;
3281 	}
3282 	codec->spdif_in_enable =
3283 		snd_hda_codec_read(codec, nid, 0,
3284 				   AC_VERB_GET_DIGI_CONVERT_1, 0) &
3285 		AC_DIG1_ENABLE;
3286 	return 0;
3287 }
3288 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
3289 
3290 #ifdef CONFIG_PM
3291 /*
3292  * command cache
3293  */
3294 
3295 /* build a 32bit cache key with the widget id and the command parameter */
3296 #define build_cmd_cache_key(nid, verb)	((verb << 8) | nid)
3297 #define get_cmd_cache_nid(key)		((key) & 0xff)
3298 #define get_cmd_cache_cmd(key)		(((key) >> 8) & 0xffff)
3299 
3300 /**
3301  * snd_hda_codec_write_cache - send a single command with caching
3302  * @codec: the HDA codec
3303  * @nid: NID to send the command
3304  * @direct: direct flag
3305  * @verb: the verb to send
3306  * @parm: the parameter for the verb
3307  *
3308  * Send a single command without waiting for response.
3309  *
3310  * Returns 0 if successful, or a negative error code.
3311  */
snd_hda_codec_write_cache(struct hda_codec * codec,hda_nid_t nid,int direct,unsigned int verb,unsigned int parm)3312 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
3313 			      int direct, unsigned int verb, unsigned int parm)
3314 {
3315 	int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
3316 	struct hda_cache_head *c;
3317 	u32 key;
3318 
3319 	if (err < 0)
3320 		return err;
3321 	/* parm may contain the verb stuff for get/set amp */
3322 	verb = verb | (parm >> 8);
3323 	parm &= 0xff;
3324 	key = build_cmd_cache_key(nid, verb);
3325 	mutex_lock(&codec->bus->cmd_mutex);
3326 	c = get_alloc_hash(&codec->cmd_cache, key);
3327 	if (c)
3328 		c->val = parm;
3329 	mutex_unlock(&codec->bus->cmd_mutex);
3330 	return 0;
3331 }
3332 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
3333 
3334 /**
3335  * snd_hda_codec_update_cache - check cache and write the cmd only when needed
3336  * @codec: the HDA codec
3337  * @nid: NID to send the command
3338  * @direct: direct flag
3339  * @verb: the verb to send
3340  * @parm: the parameter for the verb
3341  *
3342  * This function works like snd_hda_codec_write_cache(), but it doesn't send
3343  * command if the parameter is already identical with the cached value.
3344  * If not, it sends the command and refreshes the cache.
3345  *
3346  * Returns 0 if successful, or a negative error code.
3347  */
snd_hda_codec_update_cache(struct hda_codec * codec,hda_nid_t nid,int direct,unsigned int verb,unsigned int parm)3348 int snd_hda_codec_update_cache(struct hda_codec *codec, hda_nid_t nid,
3349 			       int direct, unsigned int verb, unsigned int parm)
3350 {
3351 	struct hda_cache_head *c;
3352 	u32 key;
3353 
3354 	/* parm may contain the verb stuff for get/set amp */
3355 	verb = verb | (parm >> 8);
3356 	parm &= 0xff;
3357 	key = build_cmd_cache_key(nid, verb);
3358 	mutex_lock(&codec->bus->cmd_mutex);
3359 	c = get_hash(&codec->cmd_cache, key);
3360 	if (c && c->val == parm) {
3361 		mutex_unlock(&codec->bus->cmd_mutex);
3362 		return 0;
3363 	}
3364 	mutex_unlock(&codec->bus->cmd_mutex);
3365 	return snd_hda_codec_write_cache(codec, nid, direct, verb, parm);
3366 }
3367 EXPORT_SYMBOL_HDA(snd_hda_codec_update_cache);
3368 
3369 /**
3370  * snd_hda_codec_resume_cache - Resume the all commands from the cache
3371  * @codec: HD-audio codec
3372  *
3373  * Execute all verbs recorded in the command caches to resume.
3374  */
snd_hda_codec_resume_cache(struct hda_codec * codec)3375 void snd_hda_codec_resume_cache(struct hda_codec *codec)
3376 {
3377 	struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
3378 	int i;
3379 
3380 	for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
3381 		u32 key = buffer->key;
3382 		if (!key)
3383 			continue;
3384 		snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
3385 				    get_cmd_cache_cmd(key), buffer->val);
3386 	}
3387 }
3388 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
3389 
3390 /**
3391  * snd_hda_sequence_write_cache - sequence writes with caching
3392  * @codec: the HDA codec
3393  * @seq: VERB array to send
3394  *
3395  * Send the commands sequentially from the given array.
3396  * Thte commands are recorded on cache for power-save and resume.
3397  * The array must be terminated with NID=0.
3398  */
snd_hda_sequence_write_cache(struct hda_codec * codec,const struct hda_verb * seq)3399 void snd_hda_sequence_write_cache(struct hda_codec *codec,
3400 				  const struct hda_verb *seq)
3401 {
3402 	for (; seq->nid; seq++)
3403 		snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
3404 					  seq->param);
3405 }
3406 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
3407 #endif /* CONFIG_PM */
3408 
snd_hda_codec_set_power_to_all(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state,bool eapd_workaround)3409 void snd_hda_codec_set_power_to_all(struct hda_codec *codec, hda_nid_t fg,
3410 				    unsigned int power_state,
3411 				    bool eapd_workaround)
3412 {
3413 	hda_nid_t nid = codec->start_nid;
3414 	int i;
3415 
3416 	for (i = 0; i < codec->num_nodes; i++, nid++) {
3417 		unsigned int wcaps = get_wcaps(codec, nid);
3418 		if (!(wcaps & AC_WCAP_POWER))
3419 			continue;
3420 		/* don't power down the widget if it controls eapd and
3421 		 * EAPD_BTLENABLE is set.
3422 		 */
3423 		if (eapd_workaround && power_state == AC_PWRST_D3 &&
3424 		    get_wcaps_type(wcaps) == AC_WID_PIN &&
3425 		    (snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_EAPD)) {
3426 			int eapd = snd_hda_codec_read(codec, nid, 0,
3427 						AC_VERB_GET_EAPD_BTLENABLE, 0);
3428 			if (eapd & 0x02)
3429 				continue;
3430 		}
3431 		snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
3432 				    power_state);
3433 	}
3434 
3435 	if (power_state == AC_PWRST_D0) {
3436 		unsigned long end_time;
3437 		int state;
3438 		/* wait until the codec reachs to D0 */
3439 		end_time = jiffies + msecs_to_jiffies(500);
3440 		do {
3441 			state = snd_hda_codec_read(codec, fg, 0,
3442 						   AC_VERB_GET_POWER_STATE, 0);
3443 			if (state == power_state)
3444 				break;
3445 			msleep(1);
3446 		} while (time_after_eq(end_time, jiffies));
3447 	}
3448 }
3449 EXPORT_SYMBOL_HDA(snd_hda_codec_set_power_to_all);
3450 
3451 /*
3452  * set power state of the codec
3453  */
hda_set_power_state(struct hda_codec * codec,hda_nid_t fg,unsigned int power_state)3454 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3455 				unsigned int power_state)
3456 {
3457 	if (codec->patch_ops.set_power_state) {
3458 		codec->patch_ops.set_power_state(codec, fg, power_state);
3459 		return;
3460 	}
3461 
3462 	/* this delay seems necessary to avoid click noise at power-down */
3463 	if (power_state == AC_PWRST_D3)
3464 		msleep(100);
3465 	snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE,
3466 			    power_state);
3467 	snd_hda_codec_set_power_to_all(codec, fg, power_state, true);
3468 }
3469 
3470 #ifdef CONFIG_SND_HDA_HWDEP
3471 /* execute additional init verbs */
hda_exec_init_verbs(struct hda_codec * codec)3472 static void hda_exec_init_verbs(struct hda_codec *codec)
3473 {
3474 	if (codec->init_verbs.list)
3475 		snd_hda_sequence_write(codec, codec->init_verbs.list);
3476 }
3477 #else
hda_exec_init_verbs(struct hda_codec * codec)3478 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
3479 #endif
3480 
3481 #ifdef CONFIG_PM
3482 /*
3483  * call suspend and power-down; used both from PM and power-save
3484  */
hda_call_codec_suspend(struct hda_codec * codec)3485 static void hda_call_codec_suspend(struct hda_codec *codec)
3486 {
3487 	if (codec->patch_ops.suspend)
3488 		codec->patch_ops.suspend(codec, PMSG_SUSPEND);
3489 	hda_cleanup_all_streams(codec);
3490 	hda_set_power_state(codec,
3491 			    codec->afg ? codec->afg : codec->mfg,
3492 			    AC_PWRST_D3);
3493 #ifdef CONFIG_SND_HDA_POWER_SAVE
3494 	snd_hda_update_power_acct(codec);
3495 	cancel_delayed_work(&codec->power_work);
3496 	codec->power_on = 0;
3497 	codec->power_transition = 0;
3498 	codec->power_jiffies = jiffies;
3499 #endif
3500 }
3501 
3502 /*
3503  * kick up codec; used both from PM and power-save
3504  */
hda_call_codec_resume(struct hda_codec * codec)3505 static void hda_call_codec_resume(struct hda_codec *codec)
3506 {
3507 	hda_set_power_state(codec,
3508 			    codec->afg ? codec->afg : codec->mfg,
3509 			    AC_PWRST_D0);
3510 	restore_pincfgs(codec); /* restore all current pin configs */
3511 	restore_shutup_pins(codec);
3512 	hda_exec_init_verbs(codec);
3513 	snd_hda_jack_set_dirty_all(codec);
3514 	if (codec->patch_ops.resume)
3515 		codec->patch_ops.resume(codec);
3516 	else {
3517 		if (codec->patch_ops.init)
3518 			codec->patch_ops.init(codec);
3519 		snd_hda_codec_resume_amp(codec);
3520 		snd_hda_codec_resume_cache(codec);
3521 	}
3522 }
3523 #endif /* CONFIG_PM */
3524 
3525 
3526 /**
3527  * snd_hda_build_controls - build mixer controls
3528  * @bus: the BUS
3529  *
3530  * Creates mixer controls for each codec included in the bus.
3531  *
3532  * Returns 0 if successful, otherwise a negative error code.
3533  */
snd_hda_build_controls(struct hda_bus * bus)3534 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
3535 {
3536 	struct hda_codec *codec;
3537 
3538 	list_for_each_entry(codec, &bus->codec_list, list) {
3539 		int err = snd_hda_codec_build_controls(codec);
3540 		if (err < 0) {
3541 			printk(KERN_ERR "hda_codec: cannot build controls "
3542 			       "for #%d (error %d)\n", codec->addr, err);
3543 			err = snd_hda_codec_reset(codec);
3544 			if (err < 0) {
3545 				printk(KERN_ERR
3546 				       "hda_codec: cannot revert codec\n");
3547 				return err;
3548 			}
3549 		}
3550 	}
3551 	return 0;
3552 }
3553 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
3554 
snd_hda_codec_build_controls(struct hda_codec * codec)3555 int snd_hda_codec_build_controls(struct hda_codec *codec)
3556 {
3557 	int err = 0;
3558 	hda_exec_init_verbs(codec);
3559 	/* continue to initialize... */
3560 	if (codec->patch_ops.init)
3561 		err = codec->patch_ops.init(codec);
3562 	if (!err && codec->patch_ops.build_controls)
3563 		err = codec->patch_ops.build_controls(codec);
3564 	if (err < 0)
3565 		return err;
3566 	return 0;
3567 }
3568 
3569 /*
3570  * stream formats
3571  */
3572 struct hda_rate_tbl {
3573 	unsigned int hz;
3574 	unsigned int alsa_bits;
3575 	unsigned int hda_fmt;
3576 };
3577 
3578 /* rate = base * mult / div */
3579 #define HDA_RATE(base, mult, div) \
3580 	(AC_FMT_BASE_##base##K | (((mult) - 1) << AC_FMT_MULT_SHIFT) | \
3581 	 (((div) - 1) << AC_FMT_DIV_SHIFT))
3582 
3583 static struct hda_rate_tbl rate_bits[] = {
3584 	/* rate in Hz, ALSA rate bitmask, HDA format value */
3585 
3586 	/* autodetected value used in snd_hda_query_supported_pcm */
3587 	{ 8000, SNDRV_PCM_RATE_8000, HDA_RATE(48, 1, 6) },
3588 	{ 11025, SNDRV_PCM_RATE_11025, HDA_RATE(44, 1, 4) },
3589 	{ 16000, SNDRV_PCM_RATE_16000, HDA_RATE(48, 1, 3) },
3590 	{ 22050, SNDRV_PCM_RATE_22050, HDA_RATE(44, 1, 2) },
3591 	{ 32000, SNDRV_PCM_RATE_32000, HDA_RATE(48, 2, 3) },
3592 	{ 44100, SNDRV_PCM_RATE_44100, HDA_RATE(44, 1, 1) },
3593 	{ 48000, SNDRV_PCM_RATE_48000, HDA_RATE(48, 1, 1) },
3594 	{ 88200, SNDRV_PCM_RATE_88200, HDA_RATE(44, 2, 1) },
3595 	{ 96000, SNDRV_PCM_RATE_96000, HDA_RATE(48, 2, 1) },
3596 	{ 176400, SNDRV_PCM_RATE_176400, HDA_RATE(44, 4, 1) },
3597 	{ 192000, SNDRV_PCM_RATE_192000, HDA_RATE(48, 4, 1) },
3598 #define AC_PAR_PCM_RATE_BITS	11
3599 	/* up to bits 10, 384kHZ isn't supported properly */
3600 
3601 	/* not autodetected value */
3602 	{ 9600, SNDRV_PCM_RATE_KNOT, HDA_RATE(48, 1, 5) },
3603 
3604 	{ 0 } /* terminator */
3605 };
3606 
3607 /**
3608  * snd_hda_calc_stream_format - calculate format bitset
3609  * @rate: the sample rate
3610  * @channels: the number of channels
3611  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
3612  * @maxbps: the max. bps
3613  *
3614  * Calculate the format bitset from the given rate, channels and th PCM format.
3615  *
3616  * Return zero if invalid.
3617  */
snd_hda_calc_stream_format(unsigned int rate,unsigned int channels,unsigned int format,unsigned int maxbps,unsigned short spdif_ctls)3618 unsigned int snd_hda_calc_stream_format(unsigned int rate,
3619 					unsigned int channels,
3620 					unsigned int format,
3621 					unsigned int maxbps,
3622 					unsigned short spdif_ctls)
3623 {
3624 	int i;
3625 	unsigned int val = 0;
3626 
3627 	for (i = 0; rate_bits[i].hz; i++)
3628 		if (rate_bits[i].hz == rate) {
3629 			val = rate_bits[i].hda_fmt;
3630 			break;
3631 		}
3632 	if (!rate_bits[i].hz) {
3633 		snd_printdd("invalid rate %d\n", rate);
3634 		return 0;
3635 	}
3636 
3637 	if (channels == 0 || channels > 8) {
3638 		snd_printdd("invalid channels %d\n", channels);
3639 		return 0;
3640 	}
3641 	val |= channels - 1;
3642 
3643 	switch (snd_pcm_format_width(format)) {
3644 	case 8:
3645 		val |= AC_FMT_BITS_8;
3646 		break;
3647 	case 16:
3648 		val |= AC_FMT_BITS_16;
3649 		break;
3650 	case 20:
3651 	case 24:
3652 	case 32:
3653 		if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
3654 			val |= AC_FMT_BITS_32;
3655 		else if (maxbps >= 24)
3656 			val |= AC_FMT_BITS_24;
3657 		else
3658 			val |= AC_FMT_BITS_20;
3659 		break;
3660 	default:
3661 		snd_printdd("invalid format width %d\n",
3662 			    snd_pcm_format_width(format));
3663 		return 0;
3664 	}
3665 
3666 	if (spdif_ctls & AC_DIG1_NONAUDIO)
3667 		val |= AC_FMT_TYPE_NON_PCM;
3668 
3669 	return val;
3670 }
3671 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
3672 
get_pcm_param(struct hda_codec * codec,hda_nid_t nid)3673 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3674 {
3675 	unsigned int val = 0;
3676 	if (nid != codec->afg &&
3677 	    (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
3678 		val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
3679 	if (!val || val == -1)
3680 		val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
3681 	if (!val || val == -1)
3682 		return 0;
3683 	return val;
3684 }
3685 
query_pcm_param(struct hda_codec * codec,hda_nid_t nid)3686 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
3687 {
3688 	return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
3689 			       get_pcm_param);
3690 }
3691 
get_stream_param(struct hda_codec * codec,hda_nid_t nid)3692 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
3693 {
3694 	unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
3695 	if (!streams || streams == -1)
3696 		streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
3697 	if (!streams || streams == -1)
3698 		return 0;
3699 	return streams;
3700 }
3701 
query_stream_param(struct hda_codec * codec,hda_nid_t nid)3702 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
3703 {
3704 	return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
3705 			       get_stream_param);
3706 }
3707 
3708 /**
3709  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
3710  * @codec: the HDA codec
3711  * @nid: NID to query
3712  * @ratesp: the pointer to store the detected rate bitflags
3713  * @formatsp: the pointer to store the detected formats
3714  * @bpsp: the pointer to store the detected format widths
3715  *
3716  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
3717  * or @bsps argument is ignored.
3718  *
3719  * Returns 0 if successful, otherwise a negative error code.
3720  */
snd_hda_query_supported_pcm(struct hda_codec * codec,hda_nid_t nid,u32 * ratesp,u64 * formatsp,unsigned int * bpsp)3721 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
3722 				u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
3723 {
3724 	unsigned int i, val, wcaps;
3725 
3726 	wcaps = get_wcaps(codec, nid);
3727 	val = query_pcm_param(codec, nid);
3728 
3729 	if (ratesp) {
3730 		u32 rates = 0;
3731 		for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
3732 			if (val & (1 << i))
3733 				rates |= rate_bits[i].alsa_bits;
3734 		}
3735 		if (rates == 0) {
3736 			snd_printk(KERN_ERR "hda_codec: rates == 0 "
3737 				   "(nid=0x%x, val=0x%x, ovrd=%i)\n",
3738 					nid, val,
3739 					(wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
3740 			return -EIO;
3741 		}
3742 		*ratesp = rates;
3743 	}
3744 
3745 	if (formatsp || bpsp) {
3746 		u64 formats = 0;
3747 		unsigned int streams, bps;
3748 
3749 		streams = query_stream_param(codec, nid);
3750 		if (!streams)
3751 			return -EIO;
3752 
3753 		bps = 0;
3754 		if (streams & AC_SUPFMT_PCM) {
3755 			if (val & AC_SUPPCM_BITS_8) {
3756 				formats |= SNDRV_PCM_FMTBIT_U8;
3757 				bps = 8;
3758 			}
3759 			if (val & AC_SUPPCM_BITS_16) {
3760 				formats |= SNDRV_PCM_FMTBIT_S16_LE;
3761 				bps = 16;
3762 			}
3763 			if (wcaps & AC_WCAP_DIGITAL) {
3764 				if (val & AC_SUPPCM_BITS_32)
3765 					formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
3766 				if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
3767 					formats |= SNDRV_PCM_FMTBIT_S32_LE;
3768 				if (val & AC_SUPPCM_BITS_24)
3769 					bps = 24;
3770 				else if (val & AC_SUPPCM_BITS_20)
3771 					bps = 20;
3772 			} else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
3773 					  AC_SUPPCM_BITS_32)) {
3774 				formats |= SNDRV_PCM_FMTBIT_S32_LE;
3775 				if (val & AC_SUPPCM_BITS_32)
3776 					bps = 32;
3777 				else if (val & AC_SUPPCM_BITS_24)
3778 					bps = 24;
3779 				else if (val & AC_SUPPCM_BITS_20)
3780 					bps = 20;
3781 			}
3782 		}
3783 		if (streams & AC_SUPFMT_FLOAT32) {
3784 			formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
3785 			if (!bps)
3786 				bps = 32;
3787 		}
3788 		if (streams == AC_SUPFMT_AC3) {
3789 			/* should be exclusive */
3790 			/* temporary hack: we have still no proper support
3791 			 * for the direct AC3 stream...
3792 			 */
3793 			formats |= SNDRV_PCM_FMTBIT_U8;
3794 			bps = 8;
3795 		}
3796 		if (formats == 0) {
3797 			snd_printk(KERN_ERR "hda_codec: formats == 0 "
3798 				   "(nid=0x%x, val=0x%x, ovrd=%i, "
3799 				   "streams=0x%x)\n",
3800 					nid, val,
3801 					(wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
3802 					streams);
3803 			return -EIO;
3804 		}
3805 		if (formatsp)
3806 			*formatsp = formats;
3807 		if (bpsp)
3808 			*bpsp = bps;
3809 	}
3810 
3811 	return 0;
3812 }
3813 EXPORT_SYMBOL_HDA(snd_hda_query_supported_pcm);
3814 
3815 /**
3816  * snd_hda_is_supported_format - Check the validity of the format
3817  * @codec: HD-audio codec
3818  * @nid: NID to check
3819  * @format: the HD-audio format value to check
3820  *
3821  * Check whether the given node supports the format value.
3822  *
3823  * Returns 1 if supported, 0 if not.
3824  */
snd_hda_is_supported_format(struct hda_codec * codec,hda_nid_t nid,unsigned int format)3825 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
3826 				unsigned int format)
3827 {
3828 	int i;
3829 	unsigned int val = 0, rate, stream;
3830 
3831 	val = query_pcm_param(codec, nid);
3832 	if (!val)
3833 		return 0;
3834 
3835 	rate = format & 0xff00;
3836 	for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
3837 		if (rate_bits[i].hda_fmt == rate) {
3838 			if (val & (1 << i))
3839 				break;
3840 			return 0;
3841 		}
3842 	if (i >= AC_PAR_PCM_RATE_BITS)
3843 		return 0;
3844 
3845 	stream = query_stream_param(codec, nid);
3846 	if (!stream)
3847 		return 0;
3848 
3849 	if (stream & AC_SUPFMT_PCM) {
3850 		switch (format & 0xf0) {
3851 		case 0x00:
3852 			if (!(val & AC_SUPPCM_BITS_8))
3853 				return 0;
3854 			break;
3855 		case 0x10:
3856 			if (!(val & AC_SUPPCM_BITS_16))
3857 				return 0;
3858 			break;
3859 		case 0x20:
3860 			if (!(val & AC_SUPPCM_BITS_20))
3861 				return 0;
3862 			break;
3863 		case 0x30:
3864 			if (!(val & AC_SUPPCM_BITS_24))
3865 				return 0;
3866 			break;
3867 		case 0x40:
3868 			if (!(val & AC_SUPPCM_BITS_32))
3869 				return 0;
3870 			break;
3871 		default:
3872 			return 0;
3873 		}
3874 	} else {
3875 		/* FIXME: check for float32 and AC3? */
3876 	}
3877 
3878 	return 1;
3879 }
3880 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
3881 
3882 /*
3883  * PCM stuff
3884  */
hda_pcm_default_open_close(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3885 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
3886 				      struct hda_codec *codec,
3887 				      struct snd_pcm_substream *substream)
3888 {
3889 	return 0;
3890 }
3891 
hda_pcm_default_prepare(struct hda_pcm_stream * hinfo,struct hda_codec * codec,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)3892 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
3893 				   struct hda_codec *codec,
3894 				   unsigned int stream_tag,
3895 				   unsigned int format,
3896 				   struct snd_pcm_substream *substream)
3897 {
3898 	snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
3899 	return 0;
3900 }
3901 
hda_pcm_default_cleanup(struct hda_pcm_stream * hinfo,struct hda_codec * codec,struct snd_pcm_substream * substream)3902 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
3903 				   struct hda_codec *codec,
3904 				   struct snd_pcm_substream *substream)
3905 {
3906 	snd_hda_codec_cleanup_stream(codec, hinfo->nid);
3907 	return 0;
3908 }
3909 
set_pcm_default_values(struct hda_codec * codec,struct hda_pcm_stream * info)3910 static int set_pcm_default_values(struct hda_codec *codec,
3911 				  struct hda_pcm_stream *info)
3912 {
3913 	int err;
3914 
3915 	/* query support PCM information from the given NID */
3916 	if (info->nid && (!info->rates || !info->formats)) {
3917 		err = snd_hda_query_supported_pcm(codec, info->nid,
3918 				info->rates ? NULL : &info->rates,
3919 				info->formats ? NULL : &info->formats,
3920 				info->maxbps ? NULL : &info->maxbps);
3921 		if (err < 0)
3922 			return err;
3923 	}
3924 	if (info->ops.open == NULL)
3925 		info->ops.open = hda_pcm_default_open_close;
3926 	if (info->ops.close == NULL)
3927 		info->ops.close = hda_pcm_default_open_close;
3928 	if (info->ops.prepare == NULL) {
3929 		if (snd_BUG_ON(!info->nid))
3930 			return -EINVAL;
3931 		info->ops.prepare = hda_pcm_default_prepare;
3932 	}
3933 	if (info->ops.cleanup == NULL) {
3934 		if (snd_BUG_ON(!info->nid))
3935 			return -EINVAL;
3936 		info->ops.cleanup = hda_pcm_default_cleanup;
3937 	}
3938 	return 0;
3939 }
3940 
3941 /*
3942  * codec prepare/cleanup entries
3943  */
snd_hda_codec_prepare(struct hda_codec * codec,struct hda_pcm_stream * hinfo,unsigned int stream,unsigned int format,struct snd_pcm_substream * substream)3944 int snd_hda_codec_prepare(struct hda_codec *codec,
3945 			  struct hda_pcm_stream *hinfo,
3946 			  unsigned int stream,
3947 			  unsigned int format,
3948 			  struct snd_pcm_substream *substream)
3949 {
3950 	int ret;
3951 	mutex_lock(&codec->bus->prepare_mutex);
3952 	ret = hinfo->ops.prepare(hinfo, codec, stream, format, substream);
3953 	if (ret >= 0)
3954 		purify_inactive_streams(codec);
3955 	mutex_unlock(&codec->bus->prepare_mutex);
3956 	return ret;
3957 }
3958 EXPORT_SYMBOL_HDA(snd_hda_codec_prepare);
3959 
snd_hda_codec_cleanup(struct hda_codec * codec,struct hda_pcm_stream * hinfo,struct snd_pcm_substream * substream)3960 void snd_hda_codec_cleanup(struct hda_codec *codec,
3961 			   struct hda_pcm_stream *hinfo,
3962 			   struct snd_pcm_substream *substream)
3963 {
3964 	mutex_lock(&codec->bus->prepare_mutex);
3965 	hinfo->ops.cleanup(hinfo, codec, substream);
3966 	mutex_unlock(&codec->bus->prepare_mutex);
3967 }
3968 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup);
3969 
3970 /* global */
3971 const char *snd_hda_pcm_type_name[HDA_PCM_NTYPES] = {
3972 	"Audio", "SPDIF", "HDMI", "Modem"
3973 };
3974 
3975 /*
3976  * get the empty PCM device number to assign
3977  *
3978  * note the max device number is limited by HDA_MAX_PCMS, currently 10
3979  */
get_empty_pcm_device(struct hda_bus * bus,int type)3980 static int get_empty_pcm_device(struct hda_bus *bus, int type)
3981 {
3982 	/* audio device indices; not linear to keep compatibility */
3983 	static int audio_idx[HDA_PCM_NTYPES][5] = {
3984 		[HDA_PCM_TYPE_AUDIO] = { 0, 2, 4, 5, -1 },
3985 		[HDA_PCM_TYPE_SPDIF] = { 1, -1 },
3986 		[HDA_PCM_TYPE_HDMI]  = { 3, 7, 8, 9, -1 },
3987 		[HDA_PCM_TYPE_MODEM] = { 6, -1 },
3988 	};
3989 	int i;
3990 
3991 	if (type >= HDA_PCM_NTYPES) {
3992 		snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
3993 		return -EINVAL;
3994 	}
3995 
3996 	for (i = 0; audio_idx[type][i] >= 0 ; i++)
3997 		if (!test_and_set_bit(audio_idx[type][i], bus->pcm_dev_bits))
3998 			return audio_idx[type][i];
3999 
4000 	/* non-fixed slots starting from 10 */
4001 	for (i = 10; i < 32; i++) {
4002 		if (!test_and_set_bit(i, bus->pcm_dev_bits))
4003 			return i;
4004 	}
4005 
4006 	snd_printk(KERN_WARNING "Too many %s devices\n",
4007 		snd_hda_pcm_type_name[type]);
4008 	return -EAGAIN;
4009 }
4010 
4011 /*
4012  * attach a new PCM stream
4013  */
snd_hda_attach_pcm(struct hda_codec * codec,struct hda_pcm * pcm)4014 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
4015 {
4016 	struct hda_bus *bus = codec->bus;
4017 	struct hda_pcm_stream *info;
4018 	int stream, err;
4019 
4020 	if (snd_BUG_ON(!pcm->name))
4021 		return -EINVAL;
4022 	for (stream = 0; stream < 2; stream++) {
4023 		info = &pcm->stream[stream];
4024 		if (info->substreams) {
4025 			err = set_pcm_default_values(codec, info);
4026 			if (err < 0)
4027 				return err;
4028 		}
4029 	}
4030 	return bus->ops.attach_pcm(bus, codec, pcm);
4031 }
4032 
4033 /* assign all PCMs of the given codec */
snd_hda_codec_build_pcms(struct hda_codec * codec)4034 int snd_hda_codec_build_pcms(struct hda_codec *codec)
4035 {
4036 	unsigned int pcm;
4037 	int err;
4038 
4039 	if (!codec->num_pcms) {
4040 		if (!codec->patch_ops.build_pcms)
4041 			return 0;
4042 		err = codec->patch_ops.build_pcms(codec);
4043 		if (err < 0) {
4044 			printk(KERN_ERR "hda_codec: cannot build PCMs"
4045 			       "for #%d (error %d)\n", codec->addr, err);
4046 			err = snd_hda_codec_reset(codec);
4047 			if (err < 0) {
4048 				printk(KERN_ERR
4049 				       "hda_codec: cannot revert codec\n");
4050 				return err;
4051 			}
4052 		}
4053 	}
4054 	for (pcm = 0; pcm < codec->num_pcms; pcm++) {
4055 		struct hda_pcm *cpcm = &codec->pcm_info[pcm];
4056 		int dev;
4057 
4058 		if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
4059 			continue; /* no substreams assigned */
4060 
4061 		if (!cpcm->pcm) {
4062 			dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
4063 			if (dev < 0)
4064 				continue; /* no fatal error */
4065 			cpcm->device = dev;
4066 			err = snd_hda_attach_pcm(codec, cpcm);
4067 			if (err < 0) {
4068 				printk(KERN_ERR "hda_codec: cannot attach "
4069 				       "PCM stream %d for codec #%d\n",
4070 				       dev, codec->addr);
4071 				continue; /* no fatal error */
4072 			}
4073 		}
4074 	}
4075 	return 0;
4076 }
4077 
4078 /**
4079  * snd_hda_build_pcms - build PCM information
4080  * @bus: the BUS
4081  *
4082  * Create PCM information for each codec included in the bus.
4083  *
4084  * The build_pcms codec patch is requested to set up codec->num_pcms and
4085  * codec->pcm_info properly.  The array is referred by the top-level driver
4086  * to create its PCM instances.
4087  * The allocated codec->pcm_info should be released in codec->patch_ops.free
4088  * callback.
4089  *
4090  * At least, substreams, channels_min and channels_max must be filled for
4091  * each stream.  substreams = 0 indicates that the stream doesn't exist.
4092  * When rates and/or formats are zero, the supported values are queried
4093  * from the given nid.  The nid is used also by the default ops.prepare
4094  * and ops.cleanup callbacks.
4095  *
4096  * The driver needs to call ops.open in its open callback.  Similarly,
4097  * ops.close is supposed to be called in the close callback.
4098  * ops.prepare should be called in the prepare or hw_params callback
4099  * with the proper parameters for set up.
4100  * ops.cleanup should be called in hw_free for clean up of streams.
4101  *
4102  * This function returns 0 if successful, or a negative error code.
4103  */
snd_hda_build_pcms(struct hda_bus * bus)4104 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
4105 {
4106 	struct hda_codec *codec;
4107 
4108 	list_for_each_entry(codec, &bus->codec_list, list) {
4109 		int err = snd_hda_codec_build_pcms(codec);
4110 		if (err < 0)
4111 			return err;
4112 	}
4113 	return 0;
4114 }
4115 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
4116 
4117 /**
4118  * snd_hda_check_board_config - compare the current codec with the config table
4119  * @codec: the HDA codec
4120  * @num_configs: number of config enums
4121  * @models: array of model name strings
4122  * @tbl: configuration table, terminated by null entries
4123  *
4124  * Compares the modelname or PCI subsystem id of the current codec with the
4125  * given configuration table.  If a matching entry is found, returns its
4126  * config value (supposed to be 0 or positive).
4127  *
4128  * If no entries are matching, the function returns a negative value.
4129  */
snd_hda_check_board_config(struct hda_codec * codec,int num_configs,const char * const * models,const struct snd_pci_quirk * tbl)4130 int snd_hda_check_board_config(struct hda_codec *codec,
4131 			       int num_configs, const char * const *models,
4132 			       const struct snd_pci_quirk *tbl)
4133 {
4134 	if (codec->modelname && models) {
4135 		int i;
4136 		for (i = 0; i < num_configs; i++) {
4137 			if (models[i] &&
4138 			    !strcmp(codec->modelname, models[i])) {
4139 				snd_printd(KERN_INFO "hda_codec: model '%s' is "
4140 					   "selected\n", models[i]);
4141 				return i;
4142 			}
4143 		}
4144 	}
4145 
4146 	if (!codec->bus->pci || !tbl)
4147 		return -1;
4148 
4149 	tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
4150 	if (!tbl)
4151 		return -1;
4152 	if (tbl->value >= 0 && tbl->value < num_configs) {
4153 #ifdef CONFIG_SND_DEBUG_VERBOSE
4154 		char tmp[10];
4155 		const char *model = NULL;
4156 		if (models)
4157 			model = models[tbl->value];
4158 		if (!model) {
4159 			sprintf(tmp, "#%d", tbl->value);
4160 			model = tmp;
4161 		}
4162 		snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4163 			    "for config %x:%x (%s)\n",
4164 			    model, tbl->subvendor, tbl->subdevice,
4165 			    (tbl->name ? tbl->name : "Unknown device"));
4166 #endif
4167 		return tbl->value;
4168 	}
4169 	return -1;
4170 }
4171 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
4172 
4173 /**
4174  * snd_hda_check_board_codec_sid_config - compare the current codec
4175 					subsystem ID with the
4176 					config table
4177 
4178 	   This is important for Gateway notebooks with SB450 HDA Audio
4179 	   where the vendor ID of the PCI device is:
4180 		ATI Technologies Inc SB450 HDA Audio [1002:437b]
4181 	   and the vendor/subvendor are found only at the codec.
4182 
4183  * @codec: the HDA codec
4184  * @num_configs: number of config enums
4185  * @models: array of model name strings
4186  * @tbl: configuration table, terminated by null entries
4187  *
4188  * Compares the modelname or PCI subsystem id of the current codec with the
4189  * given configuration table.  If a matching entry is found, returns its
4190  * config value (supposed to be 0 or positive).
4191  *
4192  * If no entries are matching, the function returns a negative value.
4193  */
snd_hda_check_board_codec_sid_config(struct hda_codec * codec,int num_configs,const char * const * models,const struct snd_pci_quirk * tbl)4194 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
4195 			       int num_configs, const char * const *models,
4196 			       const struct snd_pci_quirk *tbl)
4197 {
4198 	const struct snd_pci_quirk *q;
4199 
4200 	/* Search for codec ID */
4201 	for (q = tbl; q->subvendor; q++) {
4202 		unsigned int mask = 0xffff0000 | q->subdevice_mask;
4203 		unsigned int id = (q->subdevice | (q->subvendor << 16)) & mask;
4204 		if ((codec->subsystem_id & mask) == id)
4205 			break;
4206 	}
4207 
4208 	if (!q->subvendor)
4209 		return -1;
4210 
4211 	tbl = q;
4212 
4213 	if (tbl->value >= 0 && tbl->value < num_configs) {
4214 #ifdef CONFIG_SND_DEBUG_VERBOSE
4215 		char tmp[10];
4216 		const char *model = NULL;
4217 		if (models)
4218 			model = models[tbl->value];
4219 		if (!model) {
4220 			sprintf(tmp, "#%d", tbl->value);
4221 			model = tmp;
4222 		}
4223 		snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
4224 			    "for config %x:%x (%s)\n",
4225 			    model, tbl->subvendor, tbl->subdevice,
4226 			    (tbl->name ? tbl->name : "Unknown device"));
4227 #endif
4228 		return tbl->value;
4229 	}
4230 	return -1;
4231 }
4232 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
4233 
4234 /**
4235  * snd_hda_add_new_ctls - create controls from the array
4236  * @codec: the HDA codec
4237  * @knew: the array of struct snd_kcontrol_new
4238  *
4239  * This helper function creates and add new controls in the given array.
4240  * The array must be terminated with an empty entry as terminator.
4241  *
4242  * Returns 0 if successful, or a negative error code.
4243  */
snd_hda_add_new_ctls(struct hda_codec * codec,const struct snd_kcontrol_new * knew)4244 int snd_hda_add_new_ctls(struct hda_codec *codec,
4245 			 const struct snd_kcontrol_new *knew)
4246 {
4247 	int err;
4248 
4249 	for (; knew->name; knew++) {
4250 		struct snd_kcontrol *kctl;
4251 		int addr = 0, idx = 0;
4252 		if (knew->iface == -1)	/* skip this codec private value */
4253 			continue;
4254 		for (;;) {
4255 			kctl = snd_ctl_new1(knew, codec);
4256 			if (!kctl)
4257 				return -ENOMEM;
4258 			if (addr > 0)
4259 				kctl->id.device = addr;
4260 			if (idx > 0)
4261 				kctl->id.index = idx;
4262 			err = snd_hda_ctl_add(codec, 0, kctl);
4263 			if (!err)
4264 				break;
4265 			/* try first with another device index corresponding to
4266 			 * the codec addr; if it still fails (or it's the
4267 			 * primary codec), then try another control index
4268 			 */
4269 			if (!addr && codec->addr)
4270 				addr = codec->addr;
4271 			else if (!idx && !knew->index) {
4272 				idx = find_empty_mixer_ctl_idx(codec,
4273 							       knew->name);
4274 				if (idx <= 0)
4275 					return err;
4276 			} else
4277 				return err;
4278 		}
4279 	}
4280 	return 0;
4281 }
4282 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
4283 
4284 #ifdef CONFIG_SND_HDA_POWER_SAVE
hda_power_work(struct work_struct * work)4285 static void hda_power_work(struct work_struct *work)
4286 {
4287 	struct hda_codec *codec =
4288 		container_of(work, struct hda_codec, power_work.work);
4289 	struct hda_bus *bus = codec->bus;
4290 
4291 	if (!codec->power_on || codec->power_count) {
4292 		codec->power_transition = 0;
4293 		return;
4294 	}
4295 
4296 	trace_hda_power_down(codec);
4297 	hda_call_codec_suspend(codec);
4298 	if (bus->ops.pm_notify)
4299 		bus->ops.pm_notify(bus);
4300 }
4301 
hda_keep_power_on(struct hda_codec * codec)4302 static void hda_keep_power_on(struct hda_codec *codec)
4303 {
4304 	codec->power_count++;
4305 	codec->power_on = 1;
4306 	codec->power_jiffies = jiffies;
4307 }
4308 
4309 /* update the power on/off account with the current jiffies */
snd_hda_update_power_acct(struct hda_codec * codec)4310 void snd_hda_update_power_acct(struct hda_codec *codec)
4311 {
4312 	unsigned long delta = jiffies - codec->power_jiffies;
4313 	if (codec->power_on)
4314 		codec->power_on_acct += delta;
4315 	else
4316 		codec->power_off_acct += delta;
4317 	codec->power_jiffies += delta;
4318 }
4319 
4320 /**
4321  * snd_hda_power_up - Power-up the codec
4322  * @codec: HD-audio codec
4323  *
4324  * Increment the power-up counter and power up the hardware really when
4325  * not turned on yet.
4326  */
snd_hda_power_up(struct hda_codec * codec)4327 void snd_hda_power_up(struct hda_codec *codec)
4328 {
4329 	struct hda_bus *bus = codec->bus;
4330 
4331 	codec->power_count++;
4332 	if (codec->power_on || codec->power_transition)
4333 		return;
4334 
4335 	trace_hda_power_up(codec);
4336 	snd_hda_update_power_acct(codec);
4337 	codec->power_on = 1;
4338 	codec->power_jiffies = jiffies;
4339 	if (bus->ops.pm_notify)
4340 		bus->ops.pm_notify(bus);
4341 	hda_call_codec_resume(codec);
4342 	cancel_delayed_work(&codec->power_work);
4343 	codec->power_transition = 0;
4344 }
4345 EXPORT_SYMBOL_HDA(snd_hda_power_up);
4346 
4347 #define power_save(codec)	\
4348 	((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
4349 
4350 /**
4351  * snd_hda_power_down - Power-down the codec
4352  * @codec: HD-audio codec
4353  *
4354  * Decrement the power-up counter and schedules the power-off work if
4355  * the counter rearches to zero.
4356  */
snd_hda_power_down(struct hda_codec * codec)4357 void snd_hda_power_down(struct hda_codec *codec)
4358 {
4359 	--codec->power_count;
4360 	if (!codec->power_on || codec->power_count || codec->power_transition)
4361 		return;
4362 	if (power_save(codec)) {
4363 		codec->power_transition = 1; /* avoid reentrance */
4364 		queue_delayed_work(codec->bus->workq, &codec->power_work,
4365 				msecs_to_jiffies(power_save(codec) * 1000));
4366 	}
4367 }
4368 EXPORT_SYMBOL_HDA(snd_hda_power_down);
4369 
4370 /**
4371  * snd_hda_check_amp_list_power - Check the amp list and update the power
4372  * @codec: HD-audio codec
4373  * @check: the object containing an AMP list and the status
4374  * @nid: NID to check / update
4375  *
4376  * Check whether the given NID is in the amp list.  If it's in the list,
4377  * check the current AMP status, and update the the power-status according
4378  * to the mute status.
4379  *
4380  * This function is supposed to be set or called from the check_power_status
4381  * patch ops.
4382  */
snd_hda_check_amp_list_power(struct hda_codec * codec,struct hda_loopback_check * check,hda_nid_t nid)4383 int snd_hda_check_amp_list_power(struct hda_codec *codec,
4384 				 struct hda_loopback_check *check,
4385 				 hda_nid_t nid)
4386 {
4387 	const struct hda_amp_list *p;
4388 	int ch, v;
4389 
4390 	if (!check->amplist)
4391 		return 0;
4392 	for (p = check->amplist; p->nid; p++) {
4393 		if (p->nid == nid)
4394 			break;
4395 	}
4396 	if (!p->nid)
4397 		return 0; /* nothing changed */
4398 
4399 	for (p = check->amplist; p->nid; p++) {
4400 		for (ch = 0; ch < 2; ch++) {
4401 			v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
4402 						   p->idx);
4403 			if (!(v & HDA_AMP_MUTE) && v > 0) {
4404 				if (!check->power_on) {
4405 					check->power_on = 1;
4406 					snd_hda_power_up(codec);
4407 				}
4408 				return 1;
4409 			}
4410 		}
4411 	}
4412 	if (check->power_on) {
4413 		check->power_on = 0;
4414 		snd_hda_power_down(codec);
4415 	}
4416 	return 0;
4417 }
4418 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
4419 #endif
4420 
4421 /*
4422  * Channel mode helper
4423  */
4424 
4425 /**
4426  * snd_hda_ch_mode_info - Info callback helper for the channel mode enum
4427  */
snd_hda_ch_mode_info(struct hda_codec * codec,struct snd_ctl_elem_info * uinfo,const struct hda_channel_mode * chmode,int num_chmodes)4428 int snd_hda_ch_mode_info(struct hda_codec *codec,
4429 			 struct snd_ctl_elem_info *uinfo,
4430 			 const struct hda_channel_mode *chmode,
4431 			 int num_chmodes)
4432 {
4433 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4434 	uinfo->count = 1;
4435 	uinfo->value.enumerated.items = num_chmodes;
4436 	if (uinfo->value.enumerated.item >= num_chmodes)
4437 		uinfo->value.enumerated.item = num_chmodes - 1;
4438 	sprintf(uinfo->value.enumerated.name, "%dch",
4439 		chmode[uinfo->value.enumerated.item].channels);
4440 	return 0;
4441 }
4442 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
4443 
4444 /**
4445  * snd_hda_ch_mode_get - Get callback helper for the channel mode enum
4446  */
snd_hda_ch_mode_get(struct hda_codec * codec,struct snd_ctl_elem_value * ucontrol,const struct hda_channel_mode * chmode,int num_chmodes,int max_channels)4447 int snd_hda_ch_mode_get(struct hda_codec *codec,
4448 			struct snd_ctl_elem_value *ucontrol,
4449 			const struct hda_channel_mode *chmode,
4450 			int num_chmodes,
4451 			int max_channels)
4452 {
4453 	int i;
4454 
4455 	for (i = 0; i < num_chmodes; i++) {
4456 		if (max_channels == chmode[i].channels) {
4457 			ucontrol->value.enumerated.item[0] = i;
4458 			break;
4459 		}
4460 	}
4461 	return 0;
4462 }
4463 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
4464 
4465 /**
4466  * snd_hda_ch_mode_put - Put callback helper for the channel mode enum
4467  */
snd_hda_ch_mode_put(struct hda_codec * codec,struct snd_ctl_elem_value * ucontrol,const struct hda_channel_mode * chmode,int num_chmodes,int * max_channelsp)4468 int snd_hda_ch_mode_put(struct hda_codec *codec,
4469 			struct snd_ctl_elem_value *ucontrol,
4470 			const struct hda_channel_mode *chmode,
4471 			int num_chmodes,
4472 			int *max_channelsp)
4473 {
4474 	unsigned int mode;
4475 
4476 	mode = ucontrol->value.enumerated.item[0];
4477 	if (mode >= num_chmodes)
4478 		return -EINVAL;
4479 	if (*max_channelsp == chmode[mode].channels)
4480 		return 0;
4481 	/* change the current channel setting */
4482 	*max_channelsp = chmode[mode].channels;
4483 	if (chmode[mode].sequence)
4484 		snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
4485 	return 1;
4486 }
4487 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
4488 
4489 /*
4490  * input MUX helper
4491  */
4492 
4493 /**
4494  * snd_hda_input_mux_info_info - Info callback helper for the input-mux enum
4495  */
snd_hda_input_mux_info(const struct hda_input_mux * imux,struct snd_ctl_elem_info * uinfo)4496 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
4497 			   struct snd_ctl_elem_info *uinfo)
4498 {
4499 	unsigned int index;
4500 
4501 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
4502 	uinfo->count = 1;
4503 	uinfo->value.enumerated.items = imux->num_items;
4504 	if (!imux->num_items)
4505 		return 0;
4506 	index = uinfo->value.enumerated.item;
4507 	if (index >= imux->num_items)
4508 		index = imux->num_items - 1;
4509 	strcpy(uinfo->value.enumerated.name, imux->items[index].label);
4510 	return 0;
4511 }
4512 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
4513 
4514 /**
4515  * snd_hda_input_mux_info_put - Put callback helper for the input-mux enum
4516  */
snd_hda_input_mux_put(struct hda_codec * codec,const struct hda_input_mux * imux,struct snd_ctl_elem_value * ucontrol,hda_nid_t nid,unsigned int * cur_val)4517 int snd_hda_input_mux_put(struct hda_codec *codec,
4518 			  const struct hda_input_mux *imux,
4519 			  struct snd_ctl_elem_value *ucontrol,
4520 			  hda_nid_t nid,
4521 			  unsigned int *cur_val)
4522 {
4523 	unsigned int idx;
4524 
4525 	if (!imux->num_items)
4526 		return 0;
4527 	idx = ucontrol->value.enumerated.item[0];
4528 	if (idx >= imux->num_items)
4529 		idx = imux->num_items - 1;
4530 	if (*cur_val == idx)
4531 		return 0;
4532 	snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
4533 				  imux->items[idx].index);
4534 	*cur_val = idx;
4535 	return 1;
4536 }
4537 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
4538 
4539 
4540 /*
4541  * Multi-channel / digital-out PCM helper functions
4542  */
4543 
4544 /* setup SPDIF output stream */
setup_dig_out_stream(struct hda_codec * codec,hda_nid_t nid,unsigned int stream_tag,unsigned int format)4545 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
4546 				 unsigned int stream_tag, unsigned int format)
4547 {
4548 	struct hda_spdif_out *spdif = snd_hda_spdif_out_of_nid(codec, nid);
4549 
4550 	/* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
4551 	if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
4552 		set_dig_out_convert(codec, nid,
4553 				    spdif->ctls & ~AC_DIG1_ENABLE & 0xff,
4554 				    -1);
4555 	snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
4556 	if (codec->slave_dig_outs) {
4557 		const hda_nid_t *d;
4558 		for (d = codec->slave_dig_outs; *d; d++)
4559 			snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
4560 						   format);
4561 	}
4562 	/* turn on again (if needed) */
4563 	if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
4564 		set_dig_out_convert(codec, nid,
4565 				    spdif->ctls & 0xff, -1);
4566 }
4567 
cleanup_dig_out_stream(struct hda_codec * codec,hda_nid_t nid)4568 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
4569 {
4570 	snd_hda_codec_cleanup_stream(codec, nid);
4571 	if (codec->slave_dig_outs) {
4572 		const hda_nid_t *d;
4573 		for (d = codec->slave_dig_outs; *d; d++)
4574 			snd_hda_codec_cleanup_stream(codec, *d);
4575 	}
4576 }
4577 
4578 /**
4579  * snd_hda_bus_reboot_notify - call the reboot notifier of each codec
4580  * @bus: HD-audio bus
4581  */
snd_hda_bus_reboot_notify(struct hda_bus * bus)4582 void snd_hda_bus_reboot_notify(struct hda_bus *bus)
4583 {
4584 	struct hda_codec *codec;
4585 
4586 	if (!bus)
4587 		return;
4588 	list_for_each_entry(codec, &bus->codec_list, list) {
4589 		if (hda_codec_is_power_on(codec) &&
4590 		    codec->patch_ops.reboot_notify)
4591 			codec->patch_ops.reboot_notify(codec);
4592 	}
4593 }
4594 EXPORT_SYMBOL_HDA(snd_hda_bus_reboot_notify);
4595 
4596 /**
4597  * snd_hda_multi_out_dig_open - open the digital out in the exclusive mode
4598  */
snd_hda_multi_out_dig_open(struct hda_codec * codec,struct hda_multi_out * mout)4599 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
4600 			       struct hda_multi_out *mout)
4601 {
4602 	mutex_lock(&codec->spdif_mutex);
4603 	if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
4604 		/* already opened as analog dup; reset it once */
4605 		cleanup_dig_out_stream(codec, mout->dig_out_nid);
4606 	mout->dig_out_used = HDA_DIG_EXCLUSIVE;
4607 	mutex_unlock(&codec->spdif_mutex);
4608 	return 0;
4609 }
4610 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
4611 
4612 /**
4613  * snd_hda_multi_out_dig_prepare - prepare the digital out stream
4614  */
snd_hda_multi_out_dig_prepare(struct hda_codec * codec,struct hda_multi_out * mout,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)4615 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
4616 				  struct hda_multi_out *mout,
4617 				  unsigned int stream_tag,
4618 				  unsigned int format,
4619 				  struct snd_pcm_substream *substream)
4620 {
4621 	mutex_lock(&codec->spdif_mutex);
4622 	setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
4623 	mutex_unlock(&codec->spdif_mutex);
4624 	return 0;
4625 }
4626 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
4627 
4628 /**
4629  * snd_hda_multi_out_dig_cleanup - clean-up the digital out stream
4630  */
snd_hda_multi_out_dig_cleanup(struct hda_codec * codec,struct hda_multi_out * mout)4631 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
4632 				  struct hda_multi_out *mout)
4633 {
4634 	mutex_lock(&codec->spdif_mutex);
4635 	cleanup_dig_out_stream(codec, mout->dig_out_nid);
4636 	mutex_unlock(&codec->spdif_mutex);
4637 	return 0;
4638 }
4639 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
4640 
4641 /**
4642  * snd_hda_multi_out_dig_close - release the digital out stream
4643  */
snd_hda_multi_out_dig_close(struct hda_codec * codec,struct hda_multi_out * mout)4644 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
4645 				struct hda_multi_out *mout)
4646 {
4647 	mutex_lock(&codec->spdif_mutex);
4648 	mout->dig_out_used = 0;
4649 	mutex_unlock(&codec->spdif_mutex);
4650 	return 0;
4651 }
4652 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
4653 
4654 /**
4655  * snd_hda_multi_out_analog_open - open analog outputs
4656  *
4657  * Open analog outputs and set up the hw-constraints.
4658  * If the digital outputs can be opened as slave, open the digital
4659  * outputs, too.
4660  */
snd_hda_multi_out_analog_open(struct hda_codec * codec,struct hda_multi_out * mout,struct snd_pcm_substream * substream,struct hda_pcm_stream * hinfo)4661 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
4662 				  struct hda_multi_out *mout,
4663 				  struct snd_pcm_substream *substream,
4664 				  struct hda_pcm_stream *hinfo)
4665 {
4666 	struct snd_pcm_runtime *runtime = substream->runtime;
4667 	runtime->hw.channels_max = mout->max_channels;
4668 	if (mout->dig_out_nid) {
4669 		if (!mout->analog_rates) {
4670 			mout->analog_rates = hinfo->rates;
4671 			mout->analog_formats = hinfo->formats;
4672 			mout->analog_maxbps = hinfo->maxbps;
4673 		} else {
4674 			runtime->hw.rates = mout->analog_rates;
4675 			runtime->hw.formats = mout->analog_formats;
4676 			hinfo->maxbps = mout->analog_maxbps;
4677 		}
4678 		if (!mout->spdif_rates) {
4679 			snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
4680 						    &mout->spdif_rates,
4681 						    &mout->spdif_formats,
4682 						    &mout->spdif_maxbps);
4683 		}
4684 		mutex_lock(&codec->spdif_mutex);
4685 		if (mout->share_spdif) {
4686 			if ((runtime->hw.rates & mout->spdif_rates) &&
4687 			    (runtime->hw.formats & mout->spdif_formats)) {
4688 				runtime->hw.rates &= mout->spdif_rates;
4689 				runtime->hw.formats &= mout->spdif_formats;
4690 				if (mout->spdif_maxbps < hinfo->maxbps)
4691 					hinfo->maxbps = mout->spdif_maxbps;
4692 			} else {
4693 				mout->share_spdif = 0;
4694 				/* FIXME: need notify? */
4695 			}
4696 		}
4697 		mutex_unlock(&codec->spdif_mutex);
4698 	}
4699 	return snd_pcm_hw_constraint_step(substream->runtime, 0,
4700 					  SNDRV_PCM_HW_PARAM_CHANNELS, 2);
4701 }
4702 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
4703 
4704 /**
4705  * snd_hda_multi_out_analog_prepare - Preapre the analog outputs.
4706  *
4707  * Set up the i/o for analog out.
4708  * When the digital out is available, copy the front out to digital out, too.
4709  */
snd_hda_multi_out_analog_prepare(struct hda_codec * codec,struct hda_multi_out * mout,unsigned int stream_tag,unsigned int format,struct snd_pcm_substream * substream)4710 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
4711 				     struct hda_multi_out *mout,
4712 				     unsigned int stream_tag,
4713 				     unsigned int format,
4714 				     struct snd_pcm_substream *substream)
4715 {
4716 	const hda_nid_t *nids = mout->dac_nids;
4717 	int chs = substream->runtime->channels;
4718 	struct hda_spdif_out *spdif =
4719 			snd_hda_spdif_out_of_nid(codec, mout->dig_out_nid);
4720 	int i;
4721 
4722 	mutex_lock(&codec->spdif_mutex);
4723 	if (mout->dig_out_nid && mout->share_spdif &&
4724 	    mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
4725 		if (chs == 2 &&
4726 		    snd_hda_is_supported_format(codec, mout->dig_out_nid,
4727 						format) &&
4728 		    !(spdif->status & IEC958_AES0_NONAUDIO)) {
4729 			mout->dig_out_used = HDA_DIG_ANALOG_DUP;
4730 			setup_dig_out_stream(codec, mout->dig_out_nid,
4731 					     stream_tag, format);
4732 		} else {
4733 			mout->dig_out_used = 0;
4734 			cleanup_dig_out_stream(codec, mout->dig_out_nid);
4735 		}
4736 	}
4737 	mutex_unlock(&codec->spdif_mutex);
4738 
4739 	/* front */
4740 	snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
4741 				   0, format);
4742 	if (!mout->no_share_stream &&
4743 	    mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
4744 		/* headphone out will just decode front left/right (stereo) */
4745 		snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
4746 					   0, format);
4747 	/* extra outputs copied from front */
4748 	for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4749 		if (!mout->no_share_stream && mout->hp_out_nid[i])
4750 			snd_hda_codec_setup_stream(codec,
4751 						   mout->hp_out_nid[i],
4752 						   stream_tag, 0, format);
4753 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4754 		if (!mout->no_share_stream && mout->extra_out_nid[i])
4755 			snd_hda_codec_setup_stream(codec,
4756 						   mout->extra_out_nid[i],
4757 						   stream_tag, 0, format);
4758 
4759 	/* surrounds */
4760 	for (i = 1; i < mout->num_dacs; i++) {
4761 		if (chs >= (i + 1) * 2) /* independent out */
4762 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4763 						   i * 2, format);
4764 		else if (!mout->no_share_stream) /* copy front */
4765 			snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
4766 						   0, format);
4767 	}
4768 	return 0;
4769 }
4770 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
4771 
4772 /**
4773  * snd_hda_multi_out_analog_cleanup - clean up the setting for analog out
4774  */
snd_hda_multi_out_analog_cleanup(struct hda_codec * codec,struct hda_multi_out * mout)4775 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
4776 				     struct hda_multi_out *mout)
4777 {
4778 	const hda_nid_t *nids = mout->dac_nids;
4779 	int i;
4780 
4781 	for (i = 0; i < mout->num_dacs; i++)
4782 		snd_hda_codec_cleanup_stream(codec, nids[i]);
4783 	if (mout->hp_nid)
4784 		snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
4785 	for (i = 0; i < ARRAY_SIZE(mout->hp_out_nid); i++)
4786 		if (mout->hp_out_nid[i])
4787 			snd_hda_codec_cleanup_stream(codec,
4788 						     mout->hp_out_nid[i]);
4789 	for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
4790 		if (mout->extra_out_nid[i])
4791 			snd_hda_codec_cleanup_stream(codec,
4792 						     mout->extra_out_nid[i]);
4793 	mutex_lock(&codec->spdif_mutex);
4794 	if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
4795 		cleanup_dig_out_stream(codec, mout->dig_out_nid);
4796 		mout->dig_out_used = 0;
4797 	}
4798 	mutex_unlock(&codec->spdif_mutex);
4799 	return 0;
4800 }
4801 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
4802 
4803 /*
4804  * Helper for automatic pin configuration
4805  */
4806 
is_in_nid_list(hda_nid_t nid,const hda_nid_t * list)4807 static int is_in_nid_list(hda_nid_t nid, const hda_nid_t *list)
4808 {
4809 	for (; *list; list++)
4810 		if (*list == nid)
4811 			return 1;
4812 	return 0;
4813 }
4814 
4815 
4816 /*
4817  * Sort an associated group of pins according to their sequence numbers.
4818  */
sort_pins_by_sequence(hda_nid_t * pins,short * sequences,int num_pins)4819 static void sort_pins_by_sequence(hda_nid_t *pins, short *sequences,
4820 				  int num_pins)
4821 {
4822 	int i, j;
4823 	short seq;
4824 	hda_nid_t nid;
4825 
4826 	for (i = 0; i < num_pins; i++) {
4827 		for (j = i + 1; j < num_pins; j++) {
4828 			if (sequences[i] > sequences[j]) {
4829 				seq = sequences[i];
4830 				sequences[i] = sequences[j];
4831 				sequences[j] = seq;
4832 				nid = pins[i];
4833 				pins[i] = pins[j];
4834 				pins[j] = nid;
4835 			}
4836 		}
4837 	}
4838 }
4839 
4840 
4841 /* add the found input-pin to the cfg->inputs[] table */
add_auto_cfg_input_pin(struct auto_pin_cfg * cfg,hda_nid_t nid,int type)4842 static void add_auto_cfg_input_pin(struct auto_pin_cfg *cfg, hda_nid_t nid,
4843 				   int type)
4844 {
4845 	if (cfg->num_inputs < AUTO_CFG_MAX_INS) {
4846 		cfg->inputs[cfg->num_inputs].pin = nid;
4847 		cfg->inputs[cfg->num_inputs].type = type;
4848 		cfg->num_inputs++;
4849 	}
4850 }
4851 
4852 /* sort inputs in the order of AUTO_PIN_* type */
sort_autocfg_input_pins(struct auto_pin_cfg * cfg)4853 static void sort_autocfg_input_pins(struct auto_pin_cfg *cfg)
4854 {
4855 	int i, j;
4856 
4857 	for (i = 0; i < cfg->num_inputs; i++) {
4858 		for (j = i + 1; j < cfg->num_inputs; j++) {
4859 			if (cfg->inputs[i].type > cfg->inputs[j].type) {
4860 				struct auto_pin_cfg_item tmp;
4861 				tmp = cfg->inputs[i];
4862 				cfg->inputs[i] = cfg->inputs[j];
4863 				cfg->inputs[j] = tmp;
4864 			}
4865 		}
4866 	}
4867 }
4868 
4869 /* Reorder the surround channels
4870  * ALSA sequence is front/surr/clfe/side
4871  * HDA sequence is:
4872  *    4-ch: front/surr  =>  OK as it is
4873  *    6-ch: front/clfe/surr
4874  *    8-ch: front/clfe/rear/side|fc
4875  */
reorder_outputs(unsigned int nums,hda_nid_t * pins)4876 static void reorder_outputs(unsigned int nums, hda_nid_t *pins)
4877 {
4878 	hda_nid_t nid;
4879 
4880 	switch (nums) {
4881 	case 3:
4882 	case 4:
4883 		nid = pins[1];
4884 		pins[1] = pins[2];
4885 		pins[2] = nid;
4886 		break;
4887 	}
4888 }
4889 
4890 /*
4891  * Parse all pin widgets and store the useful pin nids to cfg
4892  *
4893  * The number of line-outs or any primary output is stored in line_outs,
4894  * and the corresponding output pins are assigned to line_out_pins[],
4895  * in the order of front, rear, CLFE, side, ...
4896  *
4897  * If more extra outputs (speaker and headphone) are found, the pins are
4898  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
4899  * is detected, one of speaker of HP pins is assigned as the primary
4900  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
4901  * if any analog output exists.
4902  *
4903  * The analog input pins are assigned to inputs array.
4904  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
4905  * respectively.
4906  */
snd_hda_parse_pin_defcfg(struct hda_codec * codec,struct auto_pin_cfg * cfg,const hda_nid_t * ignore_nids,unsigned int cond_flags)4907 int snd_hda_parse_pin_defcfg(struct hda_codec *codec,
4908 			     struct auto_pin_cfg *cfg,
4909 			     const hda_nid_t *ignore_nids,
4910 			     unsigned int cond_flags)
4911 {
4912 	hda_nid_t nid, end_nid;
4913 	short seq, assoc_line_out;
4914 	short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
4915 	short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
4916 	short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
4917 	int i;
4918 
4919 	memset(cfg, 0, sizeof(*cfg));
4920 
4921 	memset(sequences_line_out, 0, sizeof(sequences_line_out));
4922 	memset(sequences_speaker, 0, sizeof(sequences_speaker));
4923 	memset(sequences_hp, 0, sizeof(sequences_hp));
4924 	assoc_line_out = 0;
4925 
4926 	codec->ignore_misc_bit = true;
4927 	end_nid = codec->start_nid + codec->num_nodes;
4928 	for (nid = codec->start_nid; nid < end_nid; nid++) {
4929 		unsigned int wid_caps = get_wcaps(codec, nid);
4930 		unsigned int wid_type = get_wcaps_type(wid_caps);
4931 		unsigned int def_conf;
4932 		short assoc, loc, conn, dev;
4933 
4934 		/* read all default configuration for pin complex */
4935 		if (wid_type != AC_WID_PIN)
4936 			continue;
4937 		/* ignore the given nids (e.g. pc-beep returns error) */
4938 		if (ignore_nids && is_in_nid_list(nid, ignore_nids))
4939 			continue;
4940 
4941 		def_conf = snd_hda_codec_get_pincfg(codec, nid);
4942 		if (!(get_defcfg_misc(snd_hda_codec_get_pincfg(codec, nid)) &
4943 		      AC_DEFCFG_MISC_NO_PRESENCE))
4944 			codec->ignore_misc_bit = false;
4945 		conn = get_defcfg_connect(def_conf);
4946 		if (conn == AC_JACK_PORT_NONE)
4947 			continue;
4948 		loc = get_defcfg_location(def_conf);
4949 		dev = get_defcfg_device(def_conf);
4950 
4951 		/* workaround for buggy BIOS setups */
4952 		if (dev == AC_JACK_LINE_OUT) {
4953 			if (conn == AC_JACK_PORT_FIXED)
4954 				dev = AC_JACK_SPEAKER;
4955 		}
4956 
4957 		switch (dev) {
4958 		case AC_JACK_LINE_OUT:
4959 			seq = get_defcfg_sequence(def_conf);
4960 			assoc = get_defcfg_association(def_conf);
4961 
4962 			if (!(wid_caps & AC_WCAP_STEREO))
4963 				if (!cfg->mono_out_pin)
4964 					cfg->mono_out_pin = nid;
4965 			if (!assoc)
4966 				continue;
4967 			if (!assoc_line_out)
4968 				assoc_line_out = assoc;
4969 			else if (assoc_line_out != assoc)
4970 				continue;
4971 			if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
4972 				continue;
4973 			cfg->line_out_pins[cfg->line_outs] = nid;
4974 			sequences_line_out[cfg->line_outs] = seq;
4975 			cfg->line_outs++;
4976 			break;
4977 		case AC_JACK_SPEAKER:
4978 			seq = get_defcfg_sequence(def_conf);
4979 			assoc = get_defcfg_association(def_conf);
4980 			if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
4981 				continue;
4982 			cfg->speaker_pins[cfg->speaker_outs] = nid;
4983 			sequences_speaker[cfg->speaker_outs] = (assoc << 4) | seq;
4984 			cfg->speaker_outs++;
4985 			break;
4986 		case AC_JACK_HP_OUT:
4987 			seq = get_defcfg_sequence(def_conf);
4988 			assoc = get_defcfg_association(def_conf);
4989 			if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
4990 				continue;
4991 			cfg->hp_pins[cfg->hp_outs] = nid;
4992 			sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
4993 			cfg->hp_outs++;
4994 			break;
4995 		case AC_JACK_MIC_IN:
4996 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_MIC);
4997 			break;
4998 		case AC_JACK_LINE_IN:
4999 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_LINE_IN);
5000 			break;
5001 		case AC_JACK_CD:
5002 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_CD);
5003 			break;
5004 		case AC_JACK_AUX:
5005 			add_auto_cfg_input_pin(cfg, nid, AUTO_PIN_AUX);
5006 			break;
5007 		case AC_JACK_SPDIF_OUT:
5008 		case AC_JACK_DIG_OTHER_OUT:
5009 			if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
5010 				continue;
5011 			cfg->dig_out_pins[cfg->dig_outs] = nid;
5012 			cfg->dig_out_type[cfg->dig_outs] =
5013 				(loc == AC_JACK_LOC_HDMI) ?
5014 				HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
5015 			cfg->dig_outs++;
5016 			break;
5017 		case AC_JACK_SPDIF_IN:
5018 		case AC_JACK_DIG_OTHER_IN:
5019 			cfg->dig_in_pin = nid;
5020 			if (loc == AC_JACK_LOC_HDMI)
5021 				cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
5022 			else
5023 				cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
5024 			break;
5025 		}
5026 	}
5027 
5028 	/* FIX-UP:
5029 	 * If no line-out is defined but multiple HPs are found,
5030 	 * some of them might be the real line-outs.
5031 	 */
5032 	if (!cfg->line_outs && cfg->hp_outs > 1 &&
5033 	    !(cond_flags & HDA_PINCFG_NO_HP_FIXUP)) {
5034 		int i = 0;
5035 		while (i < cfg->hp_outs) {
5036 			/* The real HPs should have the sequence 0x0f */
5037 			if ((sequences_hp[i] & 0x0f) == 0x0f) {
5038 				i++;
5039 				continue;
5040 			}
5041 			/* Move it to the line-out table */
5042 			cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
5043 			sequences_line_out[cfg->line_outs] = sequences_hp[i];
5044 			cfg->line_outs++;
5045 			cfg->hp_outs--;
5046 			memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
5047 				sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
5048 			memmove(sequences_hp + i, sequences_hp + i + 1,
5049 				sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
5050 		}
5051 		memset(cfg->hp_pins + cfg->hp_outs, 0,
5052 		       sizeof(hda_nid_t) * (AUTO_CFG_MAX_OUTS - cfg->hp_outs));
5053 		if (!cfg->hp_outs)
5054 			cfg->line_out_type = AUTO_PIN_HP_OUT;
5055 
5056 	}
5057 
5058 	/* sort by sequence */
5059 	sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
5060 			      cfg->line_outs);
5061 	sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
5062 			      cfg->speaker_outs);
5063 	sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
5064 			      cfg->hp_outs);
5065 
5066 	/*
5067 	 * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
5068 	 * as a primary output
5069 	 */
5070 	if (!cfg->line_outs &&
5071 	    !(cond_flags & HDA_PINCFG_NO_LO_FIXUP)) {
5072 		if (cfg->speaker_outs) {
5073 			cfg->line_outs = cfg->speaker_outs;
5074 			memcpy(cfg->line_out_pins, cfg->speaker_pins,
5075 			       sizeof(cfg->speaker_pins));
5076 			cfg->speaker_outs = 0;
5077 			memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
5078 			cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
5079 		} else if (cfg->hp_outs) {
5080 			cfg->line_outs = cfg->hp_outs;
5081 			memcpy(cfg->line_out_pins, cfg->hp_pins,
5082 			       sizeof(cfg->hp_pins));
5083 			cfg->hp_outs = 0;
5084 			memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
5085 			cfg->line_out_type = AUTO_PIN_HP_OUT;
5086 		}
5087 	}
5088 
5089 	reorder_outputs(cfg->line_outs, cfg->line_out_pins);
5090 	reorder_outputs(cfg->hp_outs, cfg->hp_pins);
5091 	reorder_outputs(cfg->speaker_outs, cfg->speaker_pins);
5092 
5093 	sort_autocfg_input_pins(cfg);
5094 
5095 	/*
5096 	 * debug prints of the parsed results
5097 	 */
5098 	snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x) type:%s\n",
5099 		   cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
5100 		   cfg->line_out_pins[2], cfg->line_out_pins[3],
5101 		   cfg->line_out_pins[4],
5102 		   cfg->line_out_type == AUTO_PIN_HP_OUT ? "hp" :
5103 		   (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT ?
5104 		    "speaker" : "line"));
5105 	snd_printd("   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5106 		   cfg->speaker_outs, cfg->speaker_pins[0],
5107 		   cfg->speaker_pins[1], cfg->speaker_pins[2],
5108 		   cfg->speaker_pins[3], cfg->speaker_pins[4]);
5109 	snd_printd("   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
5110 		   cfg->hp_outs, cfg->hp_pins[0],
5111 		   cfg->hp_pins[1], cfg->hp_pins[2],
5112 		   cfg->hp_pins[3], cfg->hp_pins[4]);
5113 	snd_printd("   mono: mono_out=0x%x\n", cfg->mono_out_pin);
5114 	if (cfg->dig_outs)
5115 		snd_printd("   dig-out=0x%x/0x%x\n",
5116 			   cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
5117 	snd_printd("   inputs:");
5118 	for (i = 0; i < cfg->num_inputs; i++) {
5119 		snd_printd(" %s=0x%x",
5120 			    hda_get_autocfg_input_label(codec, cfg, i),
5121 			    cfg->inputs[i].pin);
5122 	}
5123 	snd_printd("\n");
5124 	if (cfg->dig_in_pin)
5125 		snd_printd("   dig-in=0x%x\n", cfg->dig_in_pin);
5126 
5127 	return 0;
5128 }
5129 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_defcfg);
5130 
snd_hda_get_input_pin_attr(unsigned int def_conf)5131 int snd_hda_get_input_pin_attr(unsigned int def_conf)
5132 {
5133 	unsigned int loc = get_defcfg_location(def_conf);
5134 	unsigned int conn = get_defcfg_connect(def_conf);
5135 	if (conn == AC_JACK_PORT_NONE)
5136 		return INPUT_PIN_ATTR_UNUSED;
5137 	/* Windows may claim the internal mic to be BOTH, too */
5138 	if (conn == AC_JACK_PORT_FIXED || conn == AC_JACK_PORT_BOTH)
5139 		return INPUT_PIN_ATTR_INT;
5140 	if ((loc & 0x30) == AC_JACK_LOC_INTERNAL)
5141 		return INPUT_PIN_ATTR_INT;
5142 	if ((loc & 0x30) == AC_JACK_LOC_SEPARATE)
5143 		return INPUT_PIN_ATTR_DOCK;
5144 	if (loc == AC_JACK_LOC_REAR)
5145 		return INPUT_PIN_ATTR_REAR;
5146 	if (loc == AC_JACK_LOC_FRONT)
5147 		return INPUT_PIN_ATTR_FRONT;
5148 	return INPUT_PIN_ATTR_NORMAL;
5149 }
5150 EXPORT_SYMBOL_HDA(snd_hda_get_input_pin_attr);
5151 
5152 /**
5153  * hda_get_input_pin_label - Give a label for the given input pin
5154  *
5155  * When check_location is true, the function checks the pin location
5156  * for mic and line-in pins, and set an appropriate prefix like "Front",
5157  * "Rear", "Internal".
5158  */
5159 
hda_get_input_pin_label(struct hda_codec * codec,hda_nid_t pin,bool check_location)5160 static const char *hda_get_input_pin_label(struct hda_codec *codec,
5161 					   hda_nid_t pin, bool check_location)
5162 {
5163 	unsigned int def_conf;
5164 	static const char * const mic_names[] = {
5165 		"Internal Mic", "Dock Mic", "Mic", "Front Mic", "Rear Mic",
5166 	};
5167 	int attr;
5168 
5169 	def_conf = snd_hda_codec_get_pincfg(codec, pin);
5170 
5171 	switch (get_defcfg_device(def_conf)) {
5172 	case AC_JACK_MIC_IN:
5173 		if (!check_location)
5174 			return "Mic";
5175 		attr = snd_hda_get_input_pin_attr(def_conf);
5176 		if (!attr)
5177 			return "None";
5178 		return mic_names[attr - 1];
5179 	case AC_JACK_LINE_IN:
5180 		if (!check_location)
5181 			return "Line";
5182 		attr = snd_hda_get_input_pin_attr(def_conf);
5183 		if (!attr)
5184 			return "None";
5185 		if (attr == INPUT_PIN_ATTR_DOCK)
5186 			return "Dock Line";
5187 		return "Line";
5188 	case AC_JACK_AUX:
5189 		return "Aux";
5190 	case AC_JACK_CD:
5191 		return "CD";
5192 	case AC_JACK_SPDIF_IN:
5193 		return "SPDIF In";
5194 	case AC_JACK_DIG_OTHER_IN:
5195 		return "Digital In";
5196 	default:
5197 		return "Misc";
5198 	}
5199 }
5200 
5201 /* Check whether the location prefix needs to be added to the label.
5202  * If all mic-jacks are in the same location (e.g. rear panel), we don't
5203  * have to put "Front" prefix to each label.  In such a case, returns false.
5204  */
check_mic_location_need(struct hda_codec * codec,const struct auto_pin_cfg * cfg,int input)5205 static int check_mic_location_need(struct hda_codec *codec,
5206 				   const struct auto_pin_cfg *cfg,
5207 				   int input)
5208 {
5209 	unsigned int defc;
5210 	int i, attr, attr2;
5211 
5212 	defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[input].pin);
5213 	attr = snd_hda_get_input_pin_attr(defc);
5214 	/* for internal or docking mics, we need locations */
5215 	if (attr <= INPUT_PIN_ATTR_NORMAL)
5216 		return 1;
5217 
5218 	attr = 0;
5219 	for (i = 0; i < cfg->num_inputs; i++) {
5220 		defc = snd_hda_codec_get_pincfg(codec, cfg->inputs[i].pin);
5221 		attr2 = snd_hda_get_input_pin_attr(defc);
5222 		if (attr2 >= INPUT_PIN_ATTR_NORMAL) {
5223 			if (attr && attr != attr2)
5224 				return 1; /* different locations found */
5225 			attr = attr2;
5226 		}
5227 	}
5228 	return 0;
5229 }
5230 
5231 /**
5232  * hda_get_autocfg_input_label - Get a label for the given input
5233  *
5234  * Get a label for the given input pin defined by the autocfg item.
5235  * Unlike hda_get_input_pin_label(), this function checks all inputs
5236  * defined in autocfg and avoids the redundant mic/line prefix as much as
5237  * possible.
5238  */
hda_get_autocfg_input_label(struct hda_codec * codec,const struct auto_pin_cfg * cfg,int input)5239 const char *hda_get_autocfg_input_label(struct hda_codec *codec,
5240 					const struct auto_pin_cfg *cfg,
5241 					int input)
5242 {
5243 	int type = cfg->inputs[input].type;
5244 	int has_multiple_pins = 0;
5245 
5246 	if ((input > 0 && cfg->inputs[input - 1].type == type) ||
5247 	    (input < cfg->num_inputs - 1 && cfg->inputs[input + 1].type == type))
5248 		has_multiple_pins = 1;
5249 	if (has_multiple_pins && type == AUTO_PIN_MIC)
5250 		has_multiple_pins &= check_mic_location_need(codec, cfg, input);
5251 	return hda_get_input_pin_label(codec, cfg->inputs[input].pin,
5252 				       has_multiple_pins);
5253 }
5254 EXPORT_SYMBOL_HDA(hda_get_autocfg_input_label);
5255 
5256 /* return the position of NID in the list, or -1 if not found */
find_idx_in_nid_list(hda_nid_t nid,const hda_nid_t * list,int nums)5257 static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
5258 {
5259 	int i;
5260 	for (i = 0; i < nums; i++)
5261 		if (list[i] == nid)
5262 			return i;
5263 	return -1;
5264 }
5265 
5266 /* get a unique suffix or an index number */
check_output_sfx(hda_nid_t nid,const hda_nid_t * pins,int num_pins,int * indexp)5267 static const char *check_output_sfx(hda_nid_t nid, const hda_nid_t *pins,
5268 				    int num_pins, int *indexp)
5269 {
5270 	static const char * const channel_sfx[] = {
5271 		" Front", " Surround", " CLFE", " Side"
5272 	};
5273 	int i;
5274 
5275 	i = find_idx_in_nid_list(nid, pins, num_pins);
5276 	if (i < 0)
5277 		return NULL;
5278 	if (num_pins == 1)
5279 		return "";
5280 	if (num_pins > ARRAY_SIZE(channel_sfx)) {
5281 		if (indexp)
5282 			*indexp = i;
5283 		return "";
5284 	}
5285 	return channel_sfx[i];
5286 }
5287 
fill_audio_out_name(struct hda_codec * codec,hda_nid_t nid,const struct auto_pin_cfg * cfg,const char * name,char * label,int maxlen,int * indexp)5288 static int fill_audio_out_name(struct hda_codec *codec, hda_nid_t nid,
5289 			       const struct auto_pin_cfg *cfg,
5290 			       const char *name, char *label, int maxlen,
5291 			       int *indexp)
5292 {
5293 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5294 	int attr = snd_hda_get_input_pin_attr(def_conf);
5295 	const char *pfx = "", *sfx = "";
5296 
5297 	/* handle as a speaker if it's a fixed line-out */
5298 	if (!strcmp(name, "Line Out") && attr == INPUT_PIN_ATTR_INT)
5299 		name = "Speaker";
5300 	/* check the location */
5301 	switch (attr) {
5302 	case INPUT_PIN_ATTR_DOCK:
5303 		pfx = "Dock ";
5304 		break;
5305 	case INPUT_PIN_ATTR_FRONT:
5306 		pfx = "Front ";
5307 		break;
5308 	}
5309 	if (cfg) {
5310 		/* try to give a unique suffix if needed */
5311 		sfx = check_output_sfx(nid, cfg->line_out_pins, cfg->line_outs,
5312 				       indexp);
5313 		if (!sfx)
5314 			sfx = check_output_sfx(nid, cfg->speaker_pins, cfg->speaker_outs,
5315 					       indexp);
5316 		if (!sfx) {
5317 			/* don't add channel suffix for Headphone controls */
5318 			int idx = find_idx_in_nid_list(nid, cfg->hp_pins,
5319 						       cfg->hp_outs);
5320 			if (idx >= 0)
5321 				*indexp = idx;
5322 			sfx = "";
5323 		}
5324 	}
5325 	snprintf(label, maxlen, "%s%s%s", pfx, name, sfx);
5326 	return 1;
5327 }
5328 
5329 /**
5330  * snd_hda_get_pin_label - Get a label for the given I/O pin
5331  *
5332  * Get a label for the given pin.  This function works for both input and
5333  * output pins.  When @cfg is given as non-NULL, the function tries to get
5334  * an optimized label using hda_get_autocfg_input_label().
5335  *
5336  * This function tries to give a unique label string for the pin as much as
5337  * possible.  For example, when the multiple line-outs are present, it adds
5338  * the channel suffix like "Front", "Surround", etc (only when @cfg is given).
5339  * If no unique name with a suffix is available and @indexp is non-NULL, the
5340  * index number is stored in the pointer.
5341  */
snd_hda_get_pin_label(struct hda_codec * codec,hda_nid_t nid,const struct auto_pin_cfg * cfg,char * label,int maxlen,int * indexp)5342 int snd_hda_get_pin_label(struct hda_codec *codec, hda_nid_t nid,
5343 			  const struct auto_pin_cfg *cfg,
5344 			  char *label, int maxlen, int *indexp)
5345 {
5346 	unsigned int def_conf = snd_hda_codec_get_pincfg(codec, nid);
5347 	const char *name = NULL;
5348 	int i;
5349 
5350 	if (indexp)
5351 		*indexp = 0;
5352 	if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
5353 		return 0;
5354 
5355 	switch (get_defcfg_device(def_conf)) {
5356 	case AC_JACK_LINE_OUT:
5357 		return fill_audio_out_name(codec, nid, cfg, "Line Out",
5358 					   label, maxlen, indexp);
5359 	case AC_JACK_SPEAKER:
5360 		return fill_audio_out_name(codec, nid, cfg, "Speaker",
5361 					   label, maxlen, indexp);
5362 	case AC_JACK_HP_OUT:
5363 		return fill_audio_out_name(codec, nid, cfg, "Headphone",
5364 					   label, maxlen, indexp);
5365 	case AC_JACK_SPDIF_OUT:
5366 	case AC_JACK_DIG_OTHER_OUT:
5367 		if (get_defcfg_location(def_conf) == AC_JACK_LOC_HDMI)
5368 			name = "HDMI";
5369 		else
5370 			name = "SPDIF";
5371 		if (cfg && indexp) {
5372 			i = find_idx_in_nid_list(nid, cfg->dig_out_pins,
5373 						 cfg->dig_outs);
5374 			if (i >= 0)
5375 				*indexp = i;
5376 		}
5377 		break;
5378 	default:
5379 		if (cfg) {
5380 			for (i = 0; i < cfg->num_inputs; i++) {
5381 				if (cfg->inputs[i].pin != nid)
5382 					continue;
5383 				name = hda_get_autocfg_input_label(codec, cfg, i);
5384 				if (name)
5385 					break;
5386 			}
5387 		}
5388 		if (!name)
5389 			name = hda_get_input_pin_label(codec, nid, true);
5390 		break;
5391 	}
5392 	if (!name)
5393 		return 0;
5394 	strlcpy(label, name, maxlen);
5395 	return 1;
5396 }
5397 EXPORT_SYMBOL_HDA(snd_hda_get_pin_label);
5398 
5399 /**
5400  * snd_hda_add_imux_item - Add an item to input_mux
5401  *
5402  * When the same label is used already in the existing items, the number
5403  * suffix is appended to the label.  This label index number is stored
5404  * to type_idx when non-NULL pointer is given.
5405  */
snd_hda_add_imux_item(struct hda_input_mux * imux,const char * label,int index,int * type_idx)5406 int snd_hda_add_imux_item(struct hda_input_mux *imux, const char *label,
5407 			  int index, int *type_idx)
5408 {
5409 	int i, label_idx = 0;
5410 	if (imux->num_items >= HDA_MAX_NUM_INPUTS) {
5411 		snd_printd(KERN_ERR "hda_codec: Too many imux items!\n");
5412 		return -EINVAL;
5413 	}
5414 	for (i = 0; i < imux->num_items; i++) {
5415 		if (!strncmp(label, imux->items[i].label, strlen(label)))
5416 			label_idx++;
5417 	}
5418 	if (type_idx)
5419 		*type_idx = label_idx;
5420 	if (label_idx > 0)
5421 		snprintf(imux->items[imux->num_items].label,
5422 			 sizeof(imux->items[imux->num_items].label),
5423 			 "%s %d", label, label_idx);
5424 	else
5425 		strlcpy(imux->items[imux->num_items].label, label,
5426 			sizeof(imux->items[imux->num_items].label));
5427 	imux->items[imux->num_items].index = index;
5428 	imux->num_items++;
5429 	return 0;
5430 }
5431 EXPORT_SYMBOL_HDA(snd_hda_add_imux_item);
5432 
5433 
5434 #ifdef CONFIG_PM
5435 /*
5436  * power management
5437  */
5438 
5439 /**
5440  * snd_hda_suspend - suspend the codecs
5441  * @bus: the HDA bus
5442  *
5443  * Returns 0 if successful.
5444  */
snd_hda_suspend(struct hda_bus * bus)5445 int snd_hda_suspend(struct hda_bus *bus)
5446 {
5447 	struct hda_codec *codec;
5448 
5449 	list_for_each_entry(codec, &bus->codec_list, list) {
5450 		if (hda_codec_is_power_on(codec))
5451 			hda_call_codec_suspend(codec);
5452 		if (codec->patch_ops.post_suspend)
5453 			codec->patch_ops.post_suspend(codec);
5454 	}
5455 	return 0;
5456 }
5457 EXPORT_SYMBOL_HDA(snd_hda_suspend);
5458 
5459 /**
5460  * snd_hda_resume - resume the codecs
5461  * @bus: the HDA bus
5462  *
5463  * Returns 0 if successful.
5464  *
5465  * This function is defined only when POWER_SAVE isn't set.
5466  * In the power-save mode, the codec is resumed dynamically.
5467  */
snd_hda_resume(struct hda_bus * bus)5468 int snd_hda_resume(struct hda_bus *bus)
5469 {
5470 	struct hda_codec *codec;
5471 
5472 	list_for_each_entry(codec, &bus->codec_list, list) {
5473 		if (codec->patch_ops.pre_resume)
5474 			codec->patch_ops.pre_resume(codec);
5475 		if (snd_hda_codec_needs_resume(codec))
5476 			hda_call_codec_resume(codec);
5477 	}
5478 	return 0;
5479 }
5480 EXPORT_SYMBOL_HDA(snd_hda_resume);
5481 #endif /* CONFIG_PM */
5482 
5483 /*
5484  * generic arrays
5485  */
5486 
5487 /**
5488  * snd_array_new - get a new element from the given array
5489  * @array: the array object
5490  *
5491  * Get a new element from the given array.  If it exceeds the
5492  * pre-allocated array size, re-allocate the array.
5493  *
5494  * Returns NULL if allocation failed.
5495  */
snd_array_new(struct snd_array * array)5496 void *snd_array_new(struct snd_array *array)
5497 {
5498 	if (array->used >= array->alloced) {
5499 		int num = array->alloced + array->alloc_align;
5500 		int size = (num + 1) * array->elem_size;
5501 		int oldsize = array->alloced * array->elem_size;
5502 		void *nlist;
5503 		if (snd_BUG_ON(num >= 4096))
5504 			return NULL;
5505 		nlist = krealloc(array->list, size, GFP_KERNEL);
5506 		if (!nlist)
5507 			return NULL;
5508 		memset(nlist + oldsize, 0, size - oldsize);
5509 		array->list = nlist;
5510 		array->alloced = num;
5511 	}
5512 	return snd_array_elem(array, array->used++);
5513 }
5514 EXPORT_SYMBOL_HDA(snd_array_new);
5515 
5516 /**
5517  * snd_array_free - free the given array elements
5518  * @array: the array object
5519  */
snd_array_free(struct snd_array * array)5520 void snd_array_free(struct snd_array *array)
5521 {
5522 	kfree(array->list);
5523 	array->used = 0;
5524 	array->alloced = 0;
5525 	array->list = NULL;
5526 }
5527 EXPORT_SYMBOL_HDA(snd_array_free);
5528 
5529 /**
5530  * snd_print_pcm_bits - Print the supported PCM fmt bits to the string buffer
5531  * @pcm: PCM caps bits
5532  * @buf: the string buffer to write
5533  * @buflen: the max buffer length
5534  *
5535  * used by hda_proc.c and hda_eld.c
5536  */
snd_print_pcm_bits(int pcm,char * buf,int buflen)5537 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
5538 {
5539 	static unsigned int bits[] = { 8, 16, 20, 24, 32 };
5540 	int i, j;
5541 
5542 	for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
5543 		if (pcm & (AC_SUPPCM_BITS_8 << i))
5544 			j += snprintf(buf + j, buflen - j,  " %d", bits[i]);
5545 
5546 	buf[j] = '\0'; /* necessary when j == 0 */
5547 }
5548 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
5549 
5550 MODULE_DESCRIPTION("HDA codec core");
5551 MODULE_LICENSE("GPL");
5552