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