ALSA: hda - Use generic array helpers
[safe/jmp/linux-2.6] / 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/init.h>
23 #include <linux/delay.h>
24 #include <linux/slab.h>
25 #include <linux/pci.h>
26 #include <linux/mutex.h>
27 #include <sound/core.h>
28 #include "hda_codec.h"
29 #include <sound/asoundef.h>
30 #include <sound/tlv.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
33 #include <sound/hda_hwdep.h>
34 #include "hda_patch.h"  /* codec presets */
35
36 #ifdef CONFIG_SND_HDA_POWER_SAVE
37 /* define this option here to hide as static */
38 static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
39 module_param(power_save, int, 0644);
40 MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
41                  "(in second, 0 = disable).");
42 #endif
43
44 /*
45  * vendor / preset table
46  */
47
48 struct hda_vendor_id {
49         unsigned int id;
50         const char *name;
51 };
52
53 /* codec vendor labels */
54 static struct hda_vendor_id hda_vendor_ids[] = {
55         { 0x1002, "ATI" },
56         { 0x1057, "Motorola" },
57         { 0x1095, "Silicon Image" },
58         { 0x10ec, "Realtek" },
59         { 0x1106, "VIA" },
60         { 0x111d, "IDT" },
61         { 0x11c1, "LSI" },
62         { 0x11d4, "Analog Devices" },
63         { 0x13f6, "C-Media" },
64         { 0x14f1, "Conexant" },
65         { 0x17e8, "Chrontel" },
66         { 0x1854, "LG" },
67         { 0x434d, "C-Media" },
68         { 0x8384, "SigmaTel" },
69         {} /* terminator */
70 };
71
72 static const struct hda_codec_preset *hda_preset_tables[] = {
73 #ifdef CONFIG_SND_HDA_CODEC_REALTEK
74         snd_hda_preset_realtek,
75 #endif
76 #ifdef CONFIG_SND_HDA_CODEC_CMEDIA
77         snd_hda_preset_cmedia,
78 #endif
79 #ifdef CONFIG_SND_HDA_CODEC_ANALOG
80         snd_hda_preset_analog,
81 #endif
82 #ifdef CONFIG_SND_HDA_CODEC_SIGMATEL
83         snd_hda_preset_sigmatel,
84 #endif
85 #ifdef CONFIG_SND_HDA_CODEC_SI3054
86         snd_hda_preset_si3054,
87 #endif
88 #ifdef CONFIG_SND_HDA_CODEC_ATIHDMI
89         snd_hda_preset_atihdmi,
90 #endif
91 #ifdef CONFIG_SND_HDA_CODEC_CONEXANT
92         snd_hda_preset_conexant,
93 #endif
94 #ifdef CONFIG_SND_HDA_CODEC_VIA
95         snd_hda_preset_via,
96 #endif
97 #ifdef CONFIG_SND_HDA_CODEC_NVHDMI
98         snd_hda_preset_nvhdmi,
99 #endif
100         NULL
101 };
102
103 #ifdef CONFIG_SND_HDA_POWER_SAVE
104 static void hda_power_work(struct work_struct *work);
105 static void hda_keep_power_on(struct hda_codec *codec);
106 #else
107 static inline void hda_keep_power_on(struct hda_codec *codec) {}
108 #endif
109
110 /**
111  * snd_hda_codec_read - send a command and get the response
112  * @codec: the HDA codec
113  * @nid: NID to send the command
114  * @direct: direct flag
115  * @verb: the verb to send
116  * @parm: the parameter for the verb
117  *
118  * Send a single command and read the corresponding response.
119  *
120  * Returns the obtained response value, or -1 for an error.
121  */
122 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
123                                 int direct,
124                                 unsigned int verb, unsigned int parm)
125 {
126         unsigned int res;
127         snd_hda_power_up(codec);
128         mutex_lock(&codec->bus->cmd_mutex);
129         if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
130                 res = codec->bus->ops.get_response(codec);
131         else
132                 res = (unsigned int)-1;
133         mutex_unlock(&codec->bus->cmd_mutex);
134         snd_hda_power_down(codec);
135         return res;
136 }
137
138 /**
139  * snd_hda_codec_write - send a single command without waiting for response
140  * @codec: the HDA codec
141  * @nid: NID to send the command
142  * @direct: direct flag
143  * @verb: the verb to send
144  * @parm: the parameter for the verb
145  *
146  * Send a single command without waiting for response.
147  *
148  * Returns 0 if successful, or a negative error code.
149  */
150 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
151                          unsigned int verb, unsigned int parm)
152 {
153         int err;
154         snd_hda_power_up(codec);
155         mutex_lock(&codec->bus->cmd_mutex);
156         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
157         mutex_unlock(&codec->bus->cmd_mutex);
158         snd_hda_power_down(codec);
159         return err;
160 }
161
162 /**
163  * snd_hda_sequence_write - sequence writes
164  * @codec: the HDA codec
165  * @seq: VERB array to send
166  *
167  * Send the commands sequentially from the given array.
168  * The array must be terminated with NID=0.
169  */
170 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
171 {
172         for (; seq->nid; seq++)
173                 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
174 }
175
176 /**
177  * snd_hda_get_sub_nodes - get the range of sub nodes
178  * @codec: the HDA codec
179  * @nid: NID to parse
180  * @start_id: the pointer to store the start NID
181  *
182  * Parse the NID and store the start NID of its sub-nodes.
183  * Returns the number of sub-nodes.
184  */
185 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
186                           hda_nid_t *start_id)
187 {
188         unsigned int parm;
189
190         parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
191         if (parm == -1)
192                 return 0;
193         *start_id = (parm >> 16) & 0x7fff;
194         return (int)(parm & 0x7fff);
195 }
196
197 /**
198  * snd_hda_get_connections - get connection list
199  * @codec: the HDA codec
200  * @nid: NID to parse
201  * @conn_list: connection list array
202  * @max_conns: max. number of connections to store
203  *
204  * Parses the connection list of the given widget and stores the list
205  * of NIDs.
206  *
207  * Returns the number of connections, or a negative error code.
208  */
209 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
210                             hda_nid_t *conn_list, int max_conns)
211 {
212         unsigned int parm;
213         int i, conn_len, conns;
214         unsigned int shift, num_elems, mask;
215         hda_nid_t prev_nid;
216
217         if (snd_BUG_ON(!conn_list || max_conns <= 0))
218                 return -EINVAL;
219
220         parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
221         if (parm & AC_CLIST_LONG) {
222                 /* long form */
223                 shift = 16;
224                 num_elems = 2;
225         } else {
226                 /* short form */
227                 shift = 8;
228                 num_elems = 4;
229         }
230         conn_len = parm & AC_CLIST_LENGTH;
231         mask = (1 << (shift-1)) - 1;
232
233         if (!conn_len)
234                 return 0; /* no connection */
235
236         if (conn_len == 1) {
237                 /* single connection */
238                 parm = snd_hda_codec_read(codec, nid, 0,
239                                           AC_VERB_GET_CONNECT_LIST, 0);
240                 conn_list[0] = parm & mask;
241                 return 1;
242         }
243
244         /* multi connection */
245         conns = 0;
246         prev_nid = 0;
247         for (i = 0; i < conn_len; i++) {
248                 int range_val;
249                 hda_nid_t val, n;
250
251                 if (i % num_elems == 0)
252                         parm = snd_hda_codec_read(codec, nid, 0,
253                                                   AC_VERB_GET_CONNECT_LIST, i);
254                 range_val = !!(parm & (1 << (shift-1))); /* ranges */
255                 val = parm & mask;
256                 parm >>= shift;
257                 if (range_val) {
258                         /* ranges between the previous and this one */
259                         if (!prev_nid || prev_nid >= val) {
260                                 snd_printk(KERN_WARNING "hda_codec: "
261                                            "invalid dep_range_val %x:%x\n",
262                                            prev_nid, val);
263                                 continue;
264                         }
265                         for (n = prev_nid + 1; n <= val; n++) {
266                                 if (conns >= max_conns) {
267                                         snd_printk(KERN_ERR
268                                                    "Too many connections\n");
269                                         return -EINVAL;
270                                 }
271                                 conn_list[conns++] = n;
272                         }
273                 } else {
274                         if (conns >= max_conns) {
275                                 snd_printk(KERN_ERR "Too many connections\n");
276                                 return -EINVAL;
277                         }
278                         conn_list[conns++] = val;
279                 }
280                 prev_nid = val;
281         }
282         return conns;
283 }
284
285
286 /**
287  * snd_hda_queue_unsol_event - add an unsolicited event to queue
288  * @bus: the BUS
289  * @res: unsolicited event (lower 32bit of RIRB entry)
290  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
291  *
292  * Adds the given event to the queue.  The events are processed in
293  * the workqueue asynchronously.  Call this function in the interrupt
294  * hanlder when RIRB receives an unsolicited event.
295  *
296  * Returns 0 if successful, or a negative error code.
297  */
298 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
299 {
300         struct hda_bus_unsolicited *unsol;
301         unsigned int wp;
302
303         unsol = bus->unsol;
304         if (!unsol)
305                 return 0;
306
307         wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
308         unsol->wp = wp;
309
310         wp <<= 1;
311         unsol->queue[wp] = res;
312         unsol->queue[wp + 1] = res_ex;
313
314         schedule_work(&unsol->work);
315
316         return 0;
317 }
318
319 /*
320  * process queued unsolicited events
321  */
322 static void process_unsol_events(struct work_struct *work)
323 {
324         struct hda_bus_unsolicited *unsol =
325                 container_of(work, struct hda_bus_unsolicited, work);
326         struct hda_bus *bus = unsol->bus;
327         struct hda_codec *codec;
328         unsigned int rp, caddr, res;
329
330         while (unsol->rp != unsol->wp) {
331                 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
332                 unsol->rp = rp;
333                 rp <<= 1;
334                 res = unsol->queue[rp];
335                 caddr = unsol->queue[rp + 1];
336                 if (!(caddr & (1 << 4))) /* no unsolicited event? */
337                         continue;
338                 codec = bus->caddr_tbl[caddr & 0x0f];
339                 if (codec && codec->patch_ops.unsol_event)
340                         codec->patch_ops.unsol_event(codec, res);
341         }
342 }
343
344 /*
345  * initialize unsolicited queue
346  */
347 static int __devinit init_unsol_queue(struct hda_bus *bus)
348 {
349         struct hda_bus_unsolicited *unsol;
350
351         if (bus->unsol) /* already initialized */
352                 return 0;
353
354         unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
355         if (!unsol) {
356                 snd_printk(KERN_ERR "hda_codec: "
357                            "can't allocate unsolicited queue\n");
358                 return -ENOMEM;
359         }
360         INIT_WORK(&unsol->work, process_unsol_events);
361         unsol->bus = bus;
362         bus->unsol = unsol;
363         return 0;
364 }
365
366 /*
367  * destructor
368  */
369 static void snd_hda_codec_free(struct hda_codec *codec);
370
371 static int snd_hda_bus_free(struct hda_bus *bus)
372 {
373         struct hda_codec *codec, *n;
374
375         if (!bus)
376                 return 0;
377         if (bus->unsol) {
378                 flush_scheduled_work();
379                 kfree(bus->unsol);
380         }
381         list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
382                 snd_hda_codec_free(codec);
383         }
384         if (bus->ops.private_free)
385                 bus->ops.private_free(bus);
386         kfree(bus);
387         return 0;
388 }
389
390 static int snd_hda_bus_dev_free(struct snd_device *device)
391 {
392         struct hda_bus *bus = device->device_data;
393         return snd_hda_bus_free(bus);
394 }
395
396 /**
397  * snd_hda_bus_new - create a HDA bus
398  * @card: the card entry
399  * @temp: the template for hda_bus information
400  * @busp: the pointer to store the created bus instance
401  *
402  * Returns 0 if successful, or a negative error code.
403  */
404 int __devinit snd_hda_bus_new(struct snd_card *card,
405                               const struct hda_bus_template *temp,
406                               struct hda_bus **busp)
407 {
408         struct hda_bus *bus;
409         int err;
410         static struct snd_device_ops dev_ops = {
411                 .dev_free = snd_hda_bus_dev_free,
412         };
413
414         if (snd_BUG_ON(!temp))
415                 return -EINVAL;
416         if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
417                 return -EINVAL;
418
419         if (busp)
420                 *busp = NULL;
421
422         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
423         if (bus == NULL) {
424                 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
425                 return -ENOMEM;
426         }
427
428         bus->card = card;
429         bus->private_data = temp->private_data;
430         bus->pci = temp->pci;
431         bus->modelname = temp->modelname;
432         bus->ops = temp->ops;
433
434         mutex_init(&bus->cmd_mutex);
435         INIT_LIST_HEAD(&bus->codec_list);
436
437         err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
438         if (err < 0) {
439                 snd_hda_bus_free(bus);
440                 return err;
441         }
442         if (busp)
443                 *busp = bus;
444         return 0;
445 }
446
447 #ifdef CONFIG_SND_HDA_GENERIC
448 #define is_generic_config(codec) \
449         (codec->bus->modelname && !strcmp(codec->bus->modelname, "generic"))
450 #else
451 #define is_generic_config(codec)        0
452 #endif
453
454 /*
455  * find a matching codec preset
456  */
457 static const struct hda_codec_preset __devinit *
458 find_codec_preset(struct hda_codec *codec)
459 {
460         const struct hda_codec_preset **tbl, *preset;
461
462         if (is_generic_config(codec))
463                 return NULL; /* use the generic parser */
464
465         for (tbl = hda_preset_tables; *tbl; tbl++) {
466                 for (preset = *tbl; preset->id; preset++) {
467                         u32 mask = preset->mask;
468                         if (preset->afg && preset->afg != codec->afg)
469                                 continue;
470                         if (preset->mfg && preset->mfg != codec->mfg)
471                                 continue;
472                         if (!mask)
473                                 mask = ~0;
474                         if (preset->id == (codec->vendor_id & mask) &&
475                             (!preset->rev ||
476                              preset->rev == codec->revision_id))
477                                 return preset;
478                 }
479         }
480         return NULL;
481 }
482
483 /*
484  * snd_hda_get_codec_name - store the codec name
485  */
486 void snd_hda_get_codec_name(struct hda_codec *codec,
487                             char *name, int namelen)
488 {
489         const struct hda_vendor_id *c;
490         const char *vendor = NULL;
491         u16 vendor_id = codec->vendor_id >> 16;
492         char tmp[16];
493
494         for (c = hda_vendor_ids; c->id; c++) {
495                 if (c->id == vendor_id) {
496                         vendor = c->name;
497                         break;
498                 }
499         }
500         if (!vendor) {
501                 sprintf(tmp, "Generic %04x", vendor_id);
502                 vendor = tmp;
503         }
504         if (codec->preset && codec->preset->name)
505                 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
506         else
507                 snprintf(name, namelen, "%s ID %x", vendor,
508                          codec->vendor_id & 0xffff);
509 }
510
511 /*
512  * look for an AFG and MFG nodes
513  */
514 static void __devinit setup_fg_nodes(struct hda_codec *codec)
515 {
516         int i, total_nodes;
517         hda_nid_t nid;
518
519         total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
520         for (i = 0; i < total_nodes; i++, nid++) {
521                 unsigned int func;
522                 func = snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE);
523                 switch (func & 0xff) {
524                 case AC_GRP_AUDIO_FUNCTION:
525                         codec->afg = nid;
526                         break;
527                 case AC_GRP_MODEM_FUNCTION:
528                         codec->mfg = nid;
529                         break;
530                 default:
531                         break;
532                 }
533         }
534 }
535
536 /*
537  * read widget caps for each widget and store in cache
538  */
539 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
540 {
541         int i;
542         hda_nid_t nid;
543
544         codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
545                                                  &codec->start_nid);
546         codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
547         if (!codec->wcaps)
548                 return -ENOMEM;
549         nid = codec->start_nid;
550         for (i = 0; i < codec->num_nodes; i++, nid++)
551                 codec->wcaps[i] = snd_hda_param_read(codec, nid,
552                                                      AC_PAR_AUDIO_WIDGET_CAP);
553         return 0;
554 }
555
556
557 static void init_hda_cache(struct hda_cache_rec *cache,
558                            unsigned int record_size);
559 static void free_hda_cache(struct hda_cache_rec *cache);
560
561 /*
562  * codec destructor
563  */
564 static void snd_hda_codec_free(struct hda_codec *codec)
565 {
566         if (!codec)
567                 return;
568 #ifdef CONFIG_SND_HDA_POWER_SAVE
569         cancel_delayed_work(&codec->power_work);
570         flush_scheduled_work();
571 #endif
572         list_del(&codec->list);
573         codec->bus->caddr_tbl[codec->addr] = NULL;
574         if (codec->patch_ops.free)
575                 codec->patch_ops.free(codec);
576         free_hda_cache(&codec->amp_cache);
577         free_hda_cache(&codec->cmd_cache);
578         kfree(codec->wcaps);
579         kfree(codec);
580 }
581
582 /**
583  * snd_hda_codec_new - create a HDA codec
584  * @bus: the bus to assign
585  * @codec_addr: the codec address
586  * @codecp: the pointer to store the generated codec
587  *
588  * Returns 0 if successful, or a negative error code.
589  */
590 int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
591                                 struct hda_codec **codecp)
592 {
593         struct hda_codec *codec;
594         char component[31];
595         int err;
596
597         if (snd_BUG_ON(!bus))
598                 return -EINVAL;
599         if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
600                 return -EINVAL;
601
602         if (bus->caddr_tbl[codec_addr]) {
603                 snd_printk(KERN_ERR "hda_codec: "
604                            "address 0x%x is already occupied\n", codec_addr);
605                 return -EBUSY;
606         }
607
608         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
609         if (codec == NULL) {
610                 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
611                 return -ENOMEM;
612         }
613
614         codec->bus = bus;
615         codec->addr = codec_addr;
616         mutex_init(&codec->spdif_mutex);
617         init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
618         init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
619
620 #ifdef CONFIG_SND_HDA_POWER_SAVE
621         INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
622         /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
623          * the caller has to power down appropriatley after initialization
624          * phase.
625          */
626         hda_keep_power_on(codec);
627 #endif
628
629         list_add_tail(&codec->list, &bus->codec_list);
630         bus->caddr_tbl[codec_addr] = codec;
631
632         codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
633                                               AC_PAR_VENDOR_ID);
634         if (codec->vendor_id == -1)
635                 /* read again, hopefully the access method was corrected
636                  * in the last read...
637                  */
638                 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
639                                                       AC_PAR_VENDOR_ID);
640         codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
641                                                  AC_PAR_SUBSYSTEM_ID);
642         codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
643                                                 AC_PAR_REV_ID);
644
645         setup_fg_nodes(codec);
646         if (!codec->afg && !codec->mfg) {
647                 snd_printdd("hda_codec: no AFG or MFG node found\n");
648                 snd_hda_codec_free(codec);
649                 return -ENODEV;
650         }
651
652         if (read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg) < 0) {
653                 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
654                 snd_hda_codec_free(codec);
655                 return -ENOMEM;
656         }
657
658         if (!codec->subsystem_id) {
659                 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
660                 codec->subsystem_id =
661                         snd_hda_codec_read(codec, nid, 0,
662                                            AC_VERB_GET_SUBSYSTEM_ID, 0);
663         }
664
665         codec->preset = find_codec_preset(codec);
666         /* audio codec should override the mixer name */
667         if (codec->afg || !*bus->card->mixername)
668                 snd_hda_get_codec_name(codec, bus->card->mixername,
669                                        sizeof(bus->card->mixername));
670
671         if (is_generic_config(codec)) {
672                 err = snd_hda_parse_generic_codec(codec);
673                 goto patched;
674         }
675         if (codec->preset && codec->preset->patch) {
676                 err = codec->preset->patch(codec);
677                 goto patched;
678         }
679
680         /* call the default parser */
681         err = snd_hda_parse_generic_codec(codec);
682         if (err < 0)
683                 printk(KERN_ERR "hda-codec: No codec parser is available\n");
684
685  patched:
686         if (err < 0) {
687                 snd_hda_codec_free(codec);
688                 return err;
689         }
690
691         if (codec->patch_ops.unsol_event)
692                 init_unsol_queue(bus);
693
694         snd_hda_codec_proc_new(codec);
695 #ifdef CONFIG_SND_HDA_HWDEP
696         snd_hda_create_hwdep(codec);
697 #endif
698
699         sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id, codec->subsystem_id, codec->revision_id);
700         snd_component_add(codec->bus->card, component);
701
702         if (codecp)
703                 *codecp = codec;
704         return 0;
705 }
706
707 /**
708  * snd_hda_codec_setup_stream - set up the codec for streaming
709  * @codec: the CODEC to set up
710  * @nid: the NID to set up
711  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
712  * @channel_id: channel id to pass, zero based.
713  * @format: stream format.
714  */
715 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
716                                 u32 stream_tag,
717                                 int channel_id, int format)
718 {
719         if (!nid)
720                 return;
721
722         snd_printdd("hda_codec_setup_stream: "
723                     "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
724                     nid, stream_tag, channel_id, format);
725         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
726                             (stream_tag << 4) | channel_id);
727         msleep(1);
728         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
729 }
730
731 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
732 {
733         if (!nid)
734                 return;
735
736         snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
737         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
738 #if 0 /* keep the format */
739         msleep(1);
740         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
741 #endif
742 }
743
744 /*
745  * amp access functions
746  */
747
748 /* FIXME: more better hash key? */
749 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
750 #define INFO_AMP_CAPS   (1<<0)
751 #define INFO_AMP_VOL(ch)        (1 << (1 + (ch)))
752
753 /* initialize the hash table */
754 static void __devinit init_hda_cache(struct hda_cache_rec *cache,
755                                      unsigned int record_size)
756 {
757         memset(cache, 0, sizeof(*cache));
758         memset(cache->hash, 0xff, sizeof(cache->hash));
759         snd_array_init(&cache->buf, record_size, 64);
760 }
761
762 static void free_hda_cache(struct hda_cache_rec *cache)
763 {
764         snd_array_free(&cache->buf);
765 }
766
767 /* query the hash.  allocate an entry if not found. */
768 static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache,
769                                               u32 key)
770 {
771         u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
772         u16 cur = cache->hash[idx];
773         struct hda_cache_head *info_head = cache->buf.list;
774         struct hda_cache_head *info;
775
776         while (cur != 0xffff) {
777                 info = &info_head[cur];
778                 if (info->key == key)
779                         return info;
780                 cur = info->next;
781         }
782
783         /* add a new hash entry */
784         info = snd_array_new(&cache->buf);
785         info->key = key;
786         info->val = 0;
787         info->next = cache->hash[idx];
788         cache->hash[idx] = cur;
789
790         return info;
791 }
792
793 /* query and allocate an amp hash entry */
794 static inline struct hda_amp_info *
795 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
796 {
797         return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
798 }
799
800 /*
801  * query AMP capabilities for the given widget and direction
802  */
803 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
804 {
805         struct hda_amp_info *info;
806
807         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
808         if (!info)
809                 return 0;
810         if (!(info->head.val & INFO_AMP_CAPS)) {
811                 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
812                         nid = codec->afg;
813                 info->amp_caps = snd_hda_param_read(codec, nid,
814                                                     direction == HDA_OUTPUT ?
815                                                     AC_PAR_AMP_OUT_CAP :
816                                                     AC_PAR_AMP_IN_CAP);
817                 if (info->amp_caps)
818                         info->head.val |= INFO_AMP_CAPS;
819         }
820         return info->amp_caps;
821 }
822
823 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
824                               unsigned int caps)
825 {
826         struct hda_amp_info *info;
827
828         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
829         if (!info)
830                 return -EINVAL;
831         info->amp_caps = caps;
832         info->head.val |= INFO_AMP_CAPS;
833         return 0;
834 }
835
836 /*
837  * read the current volume to info
838  * if the cache exists, read the cache value.
839  */
840 static unsigned int get_vol_mute(struct hda_codec *codec,
841                                  struct hda_amp_info *info, hda_nid_t nid,
842                                  int ch, int direction, int index)
843 {
844         u32 val, parm;
845
846         if (info->head.val & INFO_AMP_VOL(ch))
847                 return info->vol[ch];
848
849         parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
850         parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
851         parm |= index;
852         val = snd_hda_codec_read(codec, nid, 0,
853                                  AC_VERB_GET_AMP_GAIN_MUTE, parm);
854         info->vol[ch] = val & 0xff;
855         info->head.val |= INFO_AMP_VOL(ch);
856         return info->vol[ch];
857 }
858
859 /*
860  * write the current volume in info to the h/w and update the cache
861  */
862 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
863                          hda_nid_t nid, int ch, int direction, int index,
864                          int val)
865 {
866         u32 parm;
867
868         parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
869         parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
870         parm |= index << AC_AMP_SET_INDEX_SHIFT;
871         parm |= val;
872         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
873         info->vol[ch] = val;
874 }
875
876 /*
877  * read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
878  */
879 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
880                            int direction, int index)
881 {
882         struct hda_amp_info *info;
883         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
884         if (!info)
885                 return 0;
886         return get_vol_mute(codec, info, nid, ch, direction, index);
887 }
888
889 /*
890  * update the AMP value, mask = bit mask to set, val = the value
891  */
892 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
893                              int direction, int idx, int mask, int val)
894 {
895         struct hda_amp_info *info;
896
897         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
898         if (!info)
899                 return 0;
900         val &= mask;
901         val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
902         if (info->vol[ch] == val)
903                 return 0;
904         put_vol_mute(codec, info, nid, ch, direction, idx, val);
905         return 1;
906 }
907
908 /*
909  * update the AMP stereo with the same mask and value
910  */
911 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
912                              int direction, int idx, int mask, int val)
913 {
914         int ch, ret = 0;
915         for (ch = 0; ch < 2; ch++)
916                 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
917                                                 idx, mask, val);
918         return ret;
919 }
920
921 #ifdef SND_HDA_NEEDS_RESUME
922 /* resume the all amp commands from the cache */
923 void snd_hda_codec_resume_amp(struct hda_codec *codec)
924 {
925         struct hda_amp_info *buffer = codec->amp_cache.buf.list;
926         int i;
927
928         for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
929                 u32 key = buffer->head.key;
930                 hda_nid_t nid;
931                 unsigned int idx, dir, ch;
932                 if (!key)
933                         continue;
934                 nid = key & 0xff;
935                 idx = (key >> 16) & 0xff;
936                 dir = (key >> 24) & 0xff;
937                 for (ch = 0; ch < 2; ch++) {
938                         if (!(buffer->head.val & INFO_AMP_VOL(ch)))
939                                 continue;
940                         put_vol_mute(codec, buffer, nid, ch, dir, idx,
941                                      buffer->vol[ch]);
942                 }
943         }
944 }
945 #endif /* SND_HDA_NEEDS_RESUME */
946
947 /* volume */
948 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
949                                   struct snd_ctl_elem_info *uinfo)
950 {
951         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
952         u16 nid = get_amp_nid(kcontrol);
953         u8 chs = get_amp_channels(kcontrol);
954         int dir = get_amp_direction(kcontrol);
955         u32 caps;
956
957         caps = query_amp_caps(codec, nid, dir);
958         /* num steps */
959         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
960         if (!caps) {
961                 printk(KERN_WARNING "hda_codec: "
962                        "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
963                        kcontrol->id.name);
964                 return -EINVAL;
965         }
966         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
967         uinfo->count = chs == 3 ? 2 : 1;
968         uinfo->value.integer.min = 0;
969         uinfo->value.integer.max = caps;
970         return 0;
971 }
972
973 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
974                                  struct snd_ctl_elem_value *ucontrol)
975 {
976         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
977         hda_nid_t nid = get_amp_nid(kcontrol);
978         int chs = get_amp_channels(kcontrol);
979         int dir = get_amp_direction(kcontrol);
980         int idx = get_amp_index(kcontrol);
981         long *valp = ucontrol->value.integer.value;
982
983         if (chs & 1)
984                 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx)
985                         & HDA_AMP_VOLMASK;
986         if (chs & 2)
987                 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx)
988                         & HDA_AMP_VOLMASK;
989         return 0;
990 }
991
992 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
993                                  struct snd_ctl_elem_value *ucontrol)
994 {
995         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
996         hda_nid_t nid = get_amp_nid(kcontrol);
997         int chs = get_amp_channels(kcontrol);
998         int dir = get_amp_direction(kcontrol);
999         int idx = get_amp_index(kcontrol);
1000         long *valp = ucontrol->value.integer.value;
1001         int change = 0;
1002
1003         snd_hda_power_up(codec);
1004         if (chs & 1) {
1005                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1006                                                   0x7f, *valp);
1007                 valp++;
1008         }
1009         if (chs & 2)
1010                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1011                                                    0x7f, *valp);
1012         snd_hda_power_down(codec);
1013         return change;
1014 }
1015
1016 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1017                           unsigned int size, unsigned int __user *_tlv)
1018 {
1019         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1020         hda_nid_t nid = get_amp_nid(kcontrol);
1021         int dir = get_amp_direction(kcontrol);
1022         u32 caps, val1, val2;
1023
1024         if (size < 4 * sizeof(unsigned int))
1025                 return -ENOMEM;
1026         caps = query_amp_caps(codec, nid, dir);
1027         val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1028         val2 = (val2 + 1) * 25;
1029         val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1030         val1 = ((int)val1) * ((int)val2);
1031         if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1032                 return -EFAULT;
1033         if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1034                 return -EFAULT;
1035         if (put_user(val1, _tlv + 2))
1036                 return -EFAULT;
1037         if (put_user(val2, _tlv + 3))
1038                 return -EFAULT;
1039         return 0;
1040 }
1041
1042 /*
1043  * set (static) TLV for virtual master volume; recalculated as max 0dB
1044  */
1045 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1046                              unsigned int *tlv)
1047 {
1048         u32 caps;
1049         int nums, step;
1050
1051         caps = query_amp_caps(codec, nid, dir);
1052         nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1053         step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1054         step = (step + 1) * 25;
1055         tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1056         tlv[1] = 2 * sizeof(unsigned int);
1057         tlv[2] = -nums * step;
1058         tlv[3] = step;
1059 }
1060
1061 /* find a mixer control element with the given name */
1062 static struct snd_kcontrol *
1063 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1064                         const char *name, int idx)
1065 {
1066         struct snd_ctl_elem_id id;
1067         memset(&id, 0, sizeof(id));
1068         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1069         id.index = idx;
1070         strcpy(id.name, name);
1071         return snd_ctl_find_id(codec->bus->card, &id);
1072 }
1073
1074 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1075                                             const char *name)
1076 {
1077         return _snd_hda_find_mixer_ctl(codec, name, 0);
1078 }
1079
1080 /* create a virtual master control and add slaves */
1081 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1082                         unsigned int *tlv, const char **slaves)
1083 {
1084         struct snd_kcontrol *kctl;
1085         const char **s;
1086         int err;
1087
1088         for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1089                 ;
1090         if (!*s) {
1091                 snd_printdd("No slave found for %s\n", name);
1092                 return 0;
1093         }
1094         kctl = snd_ctl_make_virtual_master(name, tlv);
1095         if (!kctl)
1096                 return -ENOMEM;
1097         err = snd_ctl_add(codec->bus->card, kctl);
1098         if (err < 0)
1099                 return err;
1100         
1101         for (s = slaves; *s; s++) {
1102                 struct snd_kcontrol *sctl;
1103
1104                 sctl = snd_hda_find_mixer_ctl(codec, *s);
1105                 if (!sctl) {
1106                         snd_printdd("Cannot find slave %s, skipped\n", *s);
1107                         continue;
1108                 }
1109                 err = snd_ctl_add_slave(kctl, sctl);
1110                 if (err < 0)
1111                         return err;
1112         }
1113         return 0;
1114 }
1115
1116 /* switch */
1117 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1118                                   struct snd_ctl_elem_info *uinfo)
1119 {
1120         int chs = get_amp_channels(kcontrol);
1121
1122         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1123         uinfo->count = chs == 3 ? 2 : 1;
1124         uinfo->value.integer.min = 0;
1125         uinfo->value.integer.max = 1;
1126         return 0;
1127 }
1128
1129 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1130                                  struct snd_ctl_elem_value *ucontrol)
1131 {
1132         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1133         hda_nid_t nid = get_amp_nid(kcontrol);
1134         int chs = get_amp_channels(kcontrol);
1135         int dir = get_amp_direction(kcontrol);
1136         int idx = get_amp_index(kcontrol);
1137         long *valp = ucontrol->value.integer.value;
1138
1139         if (chs & 1)
1140                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1141                            HDA_AMP_MUTE) ? 0 : 1;
1142         if (chs & 2)
1143                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1144                          HDA_AMP_MUTE) ? 0 : 1;
1145         return 0;
1146 }
1147
1148 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1149                                  struct snd_ctl_elem_value *ucontrol)
1150 {
1151         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1152         hda_nid_t nid = get_amp_nid(kcontrol);
1153         int chs = get_amp_channels(kcontrol);
1154         int dir = get_amp_direction(kcontrol);
1155         int idx = get_amp_index(kcontrol);
1156         long *valp = ucontrol->value.integer.value;
1157         int change = 0;
1158
1159         snd_hda_power_up(codec);
1160         if (chs & 1) {
1161                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1162                                                   HDA_AMP_MUTE,
1163                                                   *valp ? 0 : HDA_AMP_MUTE);
1164                 valp++;
1165         }
1166         if (chs & 2)
1167                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1168                                                    HDA_AMP_MUTE,
1169                                                    *valp ? 0 : HDA_AMP_MUTE);
1170 #ifdef CONFIG_SND_HDA_POWER_SAVE
1171         if (codec->patch_ops.check_power_status)
1172                 codec->patch_ops.check_power_status(codec, nid);
1173 #endif
1174         snd_hda_power_down(codec);
1175         return change;
1176 }
1177
1178 /*
1179  * bound volume controls
1180  *
1181  * bind multiple volumes (# indices, from 0)
1182  */
1183
1184 #define AMP_VAL_IDX_SHIFT       19
1185 #define AMP_VAL_IDX_MASK        (0x0f<<19)
1186
1187 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1188                                   struct snd_ctl_elem_value *ucontrol)
1189 {
1190         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1191         unsigned long pval;
1192         int err;
1193
1194         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1195         pval = kcontrol->private_value;
1196         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1197         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1198         kcontrol->private_value = pval;
1199         mutex_unlock(&codec->spdif_mutex);
1200         return err;
1201 }
1202
1203 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1204                                   struct snd_ctl_elem_value *ucontrol)
1205 {
1206         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1207         unsigned long pval;
1208         int i, indices, err = 0, change = 0;
1209
1210         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1211         pval = kcontrol->private_value;
1212         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1213         for (i = 0; i < indices; i++) {
1214                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1215                         (i << AMP_VAL_IDX_SHIFT);
1216                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1217                 if (err < 0)
1218                         break;
1219                 change |= err;
1220         }
1221         kcontrol->private_value = pval;
1222         mutex_unlock(&codec->spdif_mutex);
1223         return err < 0 ? err : change;
1224 }
1225
1226 /*
1227  * generic bound volume/swtich controls
1228  */
1229 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1230                                  struct snd_ctl_elem_info *uinfo)
1231 {
1232         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1233         struct hda_bind_ctls *c;
1234         int err;
1235
1236         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1237         c = (struct hda_bind_ctls *)kcontrol->private_value;
1238         kcontrol->private_value = *c->values;
1239         err = c->ops->info(kcontrol, uinfo);
1240         kcontrol->private_value = (long)c;
1241         mutex_unlock(&codec->spdif_mutex);
1242         return err;
1243 }
1244
1245 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1246                                 struct snd_ctl_elem_value *ucontrol)
1247 {
1248         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1249         struct hda_bind_ctls *c;
1250         int err;
1251
1252         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1253         c = (struct hda_bind_ctls *)kcontrol->private_value;
1254         kcontrol->private_value = *c->values;
1255         err = c->ops->get(kcontrol, ucontrol);
1256         kcontrol->private_value = (long)c;
1257         mutex_unlock(&codec->spdif_mutex);
1258         return err;
1259 }
1260
1261 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1262                                 struct snd_ctl_elem_value *ucontrol)
1263 {
1264         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1265         struct hda_bind_ctls *c;
1266         unsigned long *vals;
1267         int err = 0, change = 0;
1268
1269         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1270         c = (struct hda_bind_ctls *)kcontrol->private_value;
1271         for (vals = c->values; *vals; vals++) {
1272                 kcontrol->private_value = *vals;
1273                 err = c->ops->put(kcontrol, ucontrol);
1274                 if (err < 0)
1275                         break;
1276                 change |= err;
1277         }
1278         kcontrol->private_value = (long)c;
1279         mutex_unlock(&codec->spdif_mutex);
1280         return err < 0 ? err : change;
1281 }
1282
1283 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1284                            unsigned int size, unsigned int __user *tlv)
1285 {
1286         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1287         struct hda_bind_ctls *c;
1288         int err;
1289
1290         mutex_lock(&codec->spdif_mutex); /* reuse spdif_mutex */
1291         c = (struct hda_bind_ctls *)kcontrol->private_value;
1292         kcontrol->private_value = *c->values;
1293         err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1294         kcontrol->private_value = (long)c;
1295         mutex_unlock(&codec->spdif_mutex);
1296         return err;
1297 }
1298
1299 struct hda_ctl_ops snd_hda_bind_vol = {
1300         .info = snd_hda_mixer_amp_volume_info,
1301         .get = snd_hda_mixer_amp_volume_get,
1302         .put = snd_hda_mixer_amp_volume_put,
1303         .tlv = snd_hda_mixer_amp_tlv
1304 };
1305
1306 struct hda_ctl_ops snd_hda_bind_sw = {
1307         .info = snd_hda_mixer_amp_switch_info,
1308         .get = snd_hda_mixer_amp_switch_get,
1309         .put = snd_hda_mixer_amp_switch_put,
1310         .tlv = snd_hda_mixer_amp_tlv
1311 };
1312
1313 /*
1314  * SPDIF out controls
1315  */
1316
1317 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1318                                    struct snd_ctl_elem_info *uinfo)
1319 {
1320         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1321         uinfo->count = 1;
1322         return 0;
1323 }
1324
1325 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1326                                    struct snd_ctl_elem_value *ucontrol)
1327 {
1328         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1329                                            IEC958_AES0_NONAUDIO |
1330                                            IEC958_AES0_CON_EMPHASIS_5015 |
1331                                            IEC958_AES0_CON_NOT_COPYRIGHT;
1332         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1333                                            IEC958_AES1_CON_ORIGINAL;
1334         return 0;
1335 }
1336
1337 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1338                                    struct snd_ctl_elem_value *ucontrol)
1339 {
1340         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1341                                            IEC958_AES0_NONAUDIO |
1342                                            IEC958_AES0_PRO_EMPHASIS_5015;
1343         return 0;
1344 }
1345
1346 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1347                                      struct snd_ctl_elem_value *ucontrol)
1348 {
1349         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1350
1351         ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1352         ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1353         ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1354         ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1355
1356         return 0;
1357 }
1358
1359 /* convert from SPDIF status bits to HDA SPDIF bits
1360  * bit 0 (DigEn) is always set zero (to be filled later)
1361  */
1362 static unsigned short convert_from_spdif_status(unsigned int sbits)
1363 {
1364         unsigned short val = 0;
1365
1366         if (sbits & IEC958_AES0_PROFESSIONAL)
1367                 val |= AC_DIG1_PROFESSIONAL;
1368         if (sbits & IEC958_AES0_NONAUDIO)
1369                 val |= AC_DIG1_NONAUDIO;
1370         if (sbits & IEC958_AES0_PROFESSIONAL) {
1371                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1372                     IEC958_AES0_PRO_EMPHASIS_5015)
1373                         val |= AC_DIG1_EMPHASIS;
1374         } else {
1375                 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1376                     IEC958_AES0_CON_EMPHASIS_5015)
1377                         val |= AC_DIG1_EMPHASIS;
1378                 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1379                         val |= AC_DIG1_COPYRIGHT;
1380                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1381                         val |= AC_DIG1_LEVEL;
1382                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1383         }
1384         return val;
1385 }
1386
1387 /* convert to SPDIF status bits from HDA SPDIF bits
1388  */
1389 static unsigned int convert_to_spdif_status(unsigned short val)
1390 {
1391         unsigned int sbits = 0;
1392
1393         if (val & AC_DIG1_NONAUDIO)
1394                 sbits |= IEC958_AES0_NONAUDIO;
1395         if (val & AC_DIG1_PROFESSIONAL)
1396                 sbits |= IEC958_AES0_PROFESSIONAL;
1397         if (sbits & IEC958_AES0_PROFESSIONAL) {
1398                 if (sbits & AC_DIG1_EMPHASIS)
1399                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1400         } else {
1401                 if (val & AC_DIG1_EMPHASIS)
1402                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1403                 if (!(val & AC_DIG1_COPYRIGHT))
1404                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1405                 if (val & AC_DIG1_LEVEL)
1406                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1407                 sbits |= val & (0x7f << 8);
1408         }
1409         return sbits;
1410 }
1411
1412 /* set digital convert verbs both for the given NID and its slaves */
1413 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1414                         int verb, int val)
1415 {
1416         hda_nid_t *d;
1417
1418         snd_hda_codec_write(codec, nid, 0, verb, val);
1419         d = codec->slave_dig_outs;
1420         if (!d)
1421                 return;
1422         for (; *d; d++)
1423                 snd_hda_codec_write(codec, *d, 0, verb, val);
1424 }
1425
1426 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1427                                        int dig1, int dig2)
1428 {
1429         if (dig1 != -1)
1430                 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1431         if (dig2 != -1)
1432                 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1433 }
1434
1435 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1436                                      struct snd_ctl_elem_value *ucontrol)
1437 {
1438         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1439         hda_nid_t nid = kcontrol->private_value;
1440         unsigned short val;
1441         int change;
1442
1443         mutex_lock(&codec->spdif_mutex);
1444         codec->spdif_status = ucontrol->value.iec958.status[0] |
1445                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1446                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1447                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1448         val = convert_from_spdif_status(codec->spdif_status);
1449         val |= codec->spdif_ctls & 1;
1450         change = codec->spdif_ctls != val;
1451         codec->spdif_ctls = val;
1452
1453         if (change)
1454                 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
1455
1456         mutex_unlock(&codec->spdif_mutex);
1457         return change;
1458 }
1459
1460 #define snd_hda_spdif_out_switch_info   snd_ctl_boolean_mono_info
1461
1462 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
1463                                         struct snd_ctl_elem_value *ucontrol)
1464 {
1465         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1466
1467         ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
1468         return 0;
1469 }
1470
1471 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
1472                                         struct snd_ctl_elem_value *ucontrol)
1473 {
1474         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1475         hda_nid_t nid = kcontrol->private_value;
1476         unsigned short val;
1477         int change;
1478
1479         mutex_lock(&codec->spdif_mutex);
1480         val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
1481         if (ucontrol->value.integer.value[0])
1482                 val |= AC_DIG1_ENABLE;
1483         change = codec->spdif_ctls != val;
1484         if (change) {
1485                 codec->spdif_ctls = val;
1486                 set_dig_out_convert(codec, nid, val & 0xff, -1);
1487                 /* unmute amp switch (if any) */
1488                 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
1489                     (val & AC_DIG1_ENABLE))
1490                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
1491                                                  HDA_AMP_MUTE, 0);
1492         }
1493         mutex_unlock(&codec->spdif_mutex);
1494         return change;
1495 }
1496
1497 static struct snd_kcontrol_new dig_mixes[] = {
1498         {
1499                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1500                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1501                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1502                 .info = snd_hda_spdif_mask_info,
1503                 .get = snd_hda_spdif_cmask_get,
1504         },
1505         {
1506                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1507                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1508                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1509                 .info = snd_hda_spdif_mask_info,
1510                 .get = snd_hda_spdif_pmask_get,
1511         },
1512         {
1513                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1514                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1515                 .info = snd_hda_spdif_mask_info,
1516                 .get = snd_hda_spdif_default_get,
1517                 .put = snd_hda_spdif_default_put,
1518         },
1519         {
1520                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1521                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1522                 .info = snd_hda_spdif_out_switch_info,
1523                 .get = snd_hda_spdif_out_switch_get,
1524                 .put = snd_hda_spdif_out_switch_put,
1525         },
1526         { } /* end */
1527 };
1528
1529 #define SPDIF_MAX_IDX   4       /* 4 instances should be enough to probe */
1530
1531 /**
1532  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1533  * @codec: the HDA codec
1534  * @nid: audio out widget NID
1535  *
1536  * Creates controls related with the SPDIF output.
1537  * Called from each patch supporting the SPDIF out.
1538  *
1539  * Returns 0 if successful, or a negative error code.
1540  */
1541 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1542 {
1543         int err;
1544         struct snd_kcontrol *kctl;
1545         struct snd_kcontrol_new *dig_mix;
1546         int idx;
1547
1548         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1549                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
1550                                              idx))
1551                         break;
1552         }
1553         if (idx >= SPDIF_MAX_IDX) {
1554                 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
1555                 return -EBUSY;
1556         }
1557         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1558                 kctl = snd_ctl_new1(dig_mix, codec);
1559                 kctl->id.index = idx;
1560                 kctl->private_value = nid;
1561                 err = snd_ctl_add(codec->bus->card, kctl);
1562                 if (err < 0)
1563                         return err;
1564         }
1565         codec->spdif_ctls =
1566                 snd_hda_codec_read(codec, nid, 0,
1567                                    AC_VERB_GET_DIGI_CONVERT_1, 0);
1568         codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1569         return 0;
1570 }
1571
1572 /*
1573  * SPDIF sharing with analog output
1574  */
1575 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
1576                               struct snd_ctl_elem_value *ucontrol)
1577 {
1578         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1579         ucontrol->value.integer.value[0] = mout->share_spdif;
1580         return 0;
1581 }
1582
1583 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
1584                               struct snd_ctl_elem_value *ucontrol)
1585 {
1586         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
1587         mout->share_spdif = !!ucontrol->value.integer.value[0];
1588         return 0;
1589 }
1590
1591 static struct snd_kcontrol_new spdif_share_sw = {
1592         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1593         .name = "IEC958 Default PCM Playback Switch",
1594         .info = snd_ctl_boolean_mono_info,
1595         .get = spdif_share_sw_get,
1596         .put = spdif_share_sw_put,
1597 };
1598
1599 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
1600                                   struct hda_multi_out *mout)
1601 {
1602         if (!mout->dig_out_nid)
1603                 return 0;
1604         /* ATTENTION: here mout is passed as private_data, instead of codec */
1605         return snd_ctl_add(codec->bus->card,
1606                            snd_ctl_new1(&spdif_share_sw, mout));
1607 }
1608
1609 /*
1610  * SPDIF input
1611  */
1612
1613 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
1614
1615 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
1616                                        struct snd_ctl_elem_value *ucontrol)
1617 {
1618         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1619
1620         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1621         return 0;
1622 }
1623
1624 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
1625                                        struct snd_ctl_elem_value *ucontrol)
1626 {
1627         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1628         hda_nid_t nid = kcontrol->private_value;
1629         unsigned int val = !!ucontrol->value.integer.value[0];
1630         int change;
1631
1632         mutex_lock(&codec->spdif_mutex);
1633         change = codec->spdif_in_enable != val;
1634         if (change) {
1635                 codec->spdif_in_enable = val;
1636                 snd_hda_codec_write_cache(codec, nid, 0,
1637                                           AC_VERB_SET_DIGI_CONVERT_1, val);
1638         }
1639         mutex_unlock(&codec->spdif_mutex);
1640         return change;
1641 }
1642
1643 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
1644                                        struct snd_ctl_elem_value *ucontrol)
1645 {
1646         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1647         hda_nid_t nid = kcontrol->private_value;
1648         unsigned short val;
1649         unsigned int sbits;
1650
1651         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
1652         sbits = convert_to_spdif_status(val);
1653         ucontrol->value.iec958.status[0] = sbits;
1654         ucontrol->value.iec958.status[1] = sbits >> 8;
1655         ucontrol->value.iec958.status[2] = sbits >> 16;
1656         ucontrol->value.iec958.status[3] = sbits >> 24;
1657         return 0;
1658 }
1659
1660 static struct snd_kcontrol_new dig_in_ctls[] = {
1661         {
1662                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1663                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1664                 .info = snd_hda_spdif_in_switch_info,
1665                 .get = snd_hda_spdif_in_switch_get,
1666                 .put = snd_hda_spdif_in_switch_put,
1667         },
1668         {
1669                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1670                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1671                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1672                 .info = snd_hda_spdif_mask_info,
1673                 .get = snd_hda_spdif_in_status_get,
1674         },
1675         { } /* end */
1676 };
1677
1678 /**
1679  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1680  * @codec: the HDA codec
1681  * @nid: audio in widget NID
1682  *
1683  * Creates controls related with the SPDIF input.
1684  * Called from each patch supporting the SPDIF in.
1685  *
1686  * Returns 0 if successful, or a negative error code.
1687  */
1688 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1689 {
1690         int err;
1691         struct snd_kcontrol *kctl;
1692         struct snd_kcontrol_new *dig_mix;
1693         int idx;
1694
1695         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
1696                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
1697                                              idx))
1698                         break;
1699         }
1700         if (idx >= SPDIF_MAX_IDX) {
1701                 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
1702                 return -EBUSY;
1703         }
1704         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1705                 kctl = snd_ctl_new1(dig_mix, codec);
1706                 kctl->private_value = nid;
1707                 err = snd_ctl_add(codec->bus->card, kctl);
1708                 if (err < 0)
1709                         return err;
1710         }
1711         codec->spdif_in_enable =
1712                 snd_hda_codec_read(codec, nid, 0,
1713                                    AC_VERB_GET_DIGI_CONVERT_1, 0) &
1714                 AC_DIG1_ENABLE;
1715         return 0;
1716 }
1717
1718 #ifdef SND_HDA_NEEDS_RESUME
1719 /*
1720  * command cache
1721  */
1722
1723 /* build a 32bit cache key with the widget id and the command parameter */
1724 #define build_cmd_cache_key(nid, verb)  ((verb << 8) | nid)
1725 #define get_cmd_cache_nid(key)          ((key) & 0xff)
1726 #define get_cmd_cache_cmd(key)          (((key) >> 8) & 0xffff)
1727
1728 /**
1729  * snd_hda_codec_write_cache - send a single command with caching
1730  * @codec: the HDA codec
1731  * @nid: NID to send the command
1732  * @direct: direct flag
1733  * @verb: the verb to send
1734  * @parm: the parameter for the verb
1735  *
1736  * Send a single command without waiting for response.
1737  *
1738  * Returns 0 if successful, or a negative error code.
1739  */
1740 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
1741                               int direct, unsigned int verb, unsigned int parm)
1742 {
1743         int err;
1744         snd_hda_power_up(codec);
1745         mutex_lock(&codec->bus->cmd_mutex);
1746         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
1747         if (!err) {
1748                 struct hda_cache_head *c;
1749                 u32 key = build_cmd_cache_key(nid, verb);
1750                 c = get_alloc_hash(&codec->cmd_cache, key);
1751                 if (c)
1752                         c->val = parm;
1753         }
1754         mutex_unlock(&codec->bus->cmd_mutex);
1755         snd_hda_power_down(codec);
1756         return err;
1757 }
1758
1759 /* resume the all commands from the cache */
1760 void snd_hda_codec_resume_cache(struct hda_codec *codec)
1761 {
1762         struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
1763         int i;
1764
1765         for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
1766                 u32 key = buffer->key;
1767                 if (!key)
1768                         continue;
1769                 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
1770                                     get_cmd_cache_cmd(key), buffer->val);
1771         }
1772 }
1773
1774 /**
1775  * snd_hda_sequence_write_cache - sequence writes with caching
1776  * @codec: the HDA codec
1777  * @seq: VERB array to send
1778  *
1779  * Send the commands sequentially from the given array.
1780  * Thte commands are recorded on cache for power-save and resume.
1781  * The array must be terminated with NID=0.
1782  */
1783 void snd_hda_sequence_write_cache(struct hda_codec *codec,
1784                                   const struct hda_verb *seq)
1785 {
1786         for (; seq->nid; seq++)
1787                 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
1788                                           seq->param);
1789 }
1790 #endif /* SND_HDA_NEEDS_RESUME */
1791
1792 /*
1793  * set power state of the codec
1794  */
1795 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
1796                                 unsigned int power_state)
1797 {
1798         hda_nid_t nid;
1799         int i;
1800
1801         snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
1802                             power_state);
1803         msleep(10); /* partial workaround for "azx_get_response timeout" */
1804
1805         nid = codec->start_nid;
1806         for (i = 0; i < codec->num_nodes; i++, nid++) {
1807                 unsigned int wcaps = get_wcaps(codec, nid);
1808                 if (wcaps & AC_WCAP_POWER) {
1809                         unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
1810                                 AC_WCAP_TYPE_SHIFT;
1811                         if (wid_type == AC_WID_PIN) {
1812                                 unsigned int pincap;
1813                                 /*
1814                                  * don't power down the widget if it controls
1815                                  * eapd and EAPD_BTLENABLE is set.
1816                                  */
1817                                 pincap = snd_hda_param_read(codec, nid,
1818                                                             AC_PAR_PIN_CAP);
1819                                 if (pincap & AC_PINCAP_EAPD) {
1820                                         int eapd = snd_hda_codec_read(codec,
1821                                                 nid, 0,
1822                                                 AC_VERB_GET_EAPD_BTLENABLE, 0);
1823                                         eapd &= 0x02;
1824                                         if (power_state == AC_PWRST_D3 && eapd)
1825                                                 continue;
1826                                 }
1827                         }
1828                         snd_hda_codec_write(codec, nid, 0,
1829                                             AC_VERB_SET_POWER_STATE,
1830                                             power_state);
1831                 }
1832         }
1833
1834         if (power_state == AC_PWRST_D0) {
1835                 unsigned long end_time;
1836                 int state;
1837                 msleep(10);
1838                 /* wait until the codec reachs to D0 */
1839                 end_time = jiffies + msecs_to_jiffies(500);
1840                 do {
1841                         state = snd_hda_codec_read(codec, fg, 0,
1842                                                    AC_VERB_GET_POWER_STATE, 0);
1843                         if (state == power_state)
1844                                 break;
1845                         msleep(1);
1846                 } while (time_after_eq(end_time, jiffies));
1847         }
1848 }
1849
1850 #ifdef SND_HDA_NEEDS_RESUME
1851 /*
1852  * call suspend and power-down; used both from PM and power-save
1853  */
1854 static void hda_call_codec_suspend(struct hda_codec *codec)
1855 {
1856         if (codec->patch_ops.suspend)
1857                 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
1858         hda_set_power_state(codec,
1859                             codec->afg ? codec->afg : codec->mfg,
1860                             AC_PWRST_D3);
1861 #ifdef CONFIG_SND_HDA_POWER_SAVE
1862         cancel_delayed_work(&codec->power_work);
1863         codec->power_on = 0;
1864         codec->power_transition = 0;
1865 #endif
1866 }
1867
1868 /*
1869  * kick up codec; used both from PM and power-save
1870  */
1871 static void hda_call_codec_resume(struct hda_codec *codec)
1872 {
1873         hda_set_power_state(codec,
1874                             codec->afg ? codec->afg : codec->mfg,
1875                             AC_PWRST_D0);
1876         if (codec->patch_ops.resume)
1877                 codec->patch_ops.resume(codec);
1878         else {
1879                 if (codec->patch_ops.init)
1880                         codec->patch_ops.init(codec);
1881                 snd_hda_codec_resume_amp(codec);
1882                 snd_hda_codec_resume_cache(codec);
1883         }
1884 }
1885 #endif /* SND_HDA_NEEDS_RESUME */
1886
1887
1888 /**
1889  * snd_hda_build_controls - build mixer controls
1890  * @bus: the BUS
1891  *
1892  * Creates mixer controls for each codec included in the bus.
1893  *
1894  * Returns 0 if successful, otherwise a negative error code.
1895  */
1896 int __devinit snd_hda_build_controls(struct hda_bus *bus)
1897 {
1898         struct hda_codec *codec;
1899
1900         list_for_each_entry(codec, &bus->codec_list, list) {
1901                 int err = 0;
1902                 /* fake as if already powered-on */
1903                 hda_keep_power_on(codec);
1904                 /* then fire up */
1905                 hda_set_power_state(codec,
1906                                     codec->afg ? codec->afg : codec->mfg,
1907                                     AC_PWRST_D0);
1908                 /* continue to initialize... */
1909                 if (codec->patch_ops.init)
1910                         err = codec->patch_ops.init(codec);
1911                 if (!err && codec->patch_ops.build_controls)
1912                         err = codec->patch_ops.build_controls(codec);
1913                 snd_hda_power_down(codec);
1914                 if (err < 0)
1915                         return err;
1916         }
1917
1918         return 0;
1919 }
1920
1921 /*
1922  * stream formats
1923  */
1924 struct hda_rate_tbl {
1925         unsigned int hz;
1926         unsigned int alsa_bits;
1927         unsigned int hda_fmt;
1928 };
1929
1930 static struct hda_rate_tbl rate_bits[] = {
1931         /* rate in Hz, ALSA rate bitmask, HDA format value */
1932
1933         /* autodetected value used in snd_hda_query_supported_pcm */
1934         { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1935         { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1936         { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1937         { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1938         { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1939         { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1940         { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1941         { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1942         { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1943         { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1944         { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1945 #define AC_PAR_PCM_RATE_BITS    11
1946         /* up to bits 10, 384kHZ isn't supported properly */
1947
1948         /* not autodetected value */
1949         { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1950
1951         { 0 } /* terminator */
1952 };
1953
1954 /**
1955  * snd_hda_calc_stream_format - calculate format bitset
1956  * @rate: the sample rate
1957  * @channels: the number of channels
1958  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1959  * @maxbps: the max. bps
1960  *
1961  * Calculate the format bitset from the given rate, channels and th PCM format.
1962  *
1963  * Return zero if invalid.
1964  */
1965 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1966                                         unsigned int channels,
1967                                         unsigned int format,
1968                                         unsigned int maxbps)
1969 {
1970         int i;
1971         unsigned int val = 0;
1972
1973         for (i = 0; rate_bits[i].hz; i++)
1974                 if (rate_bits[i].hz == rate) {
1975                         val = rate_bits[i].hda_fmt;
1976                         break;
1977                 }
1978         if (!rate_bits[i].hz) {
1979                 snd_printdd("invalid rate %d\n", rate);
1980                 return 0;
1981         }
1982
1983         if (channels == 0 || channels > 8) {
1984                 snd_printdd("invalid channels %d\n", channels);
1985                 return 0;
1986         }
1987         val |= channels - 1;
1988
1989         switch (snd_pcm_format_width(format)) {
1990         case 8:  val |= 0x00; break;
1991         case 16: val |= 0x10; break;
1992         case 20:
1993         case 24:
1994         case 32:
1995                 if (maxbps >= 32)
1996                         val |= 0x40;
1997                 else if (maxbps >= 24)
1998                         val |= 0x30;
1999                 else
2000                         val |= 0x20;
2001                 break;
2002         default:
2003                 snd_printdd("invalid format width %d\n",
2004                             snd_pcm_format_width(format));
2005                 return 0;
2006         }
2007
2008         return val;
2009 }
2010
2011 /**
2012  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2013  * @codec: the HDA codec
2014  * @nid: NID to query
2015  * @ratesp: the pointer to store the detected rate bitflags
2016  * @formatsp: the pointer to store the detected formats
2017  * @bpsp: the pointer to store the detected format widths
2018  *
2019  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
2020  * or @bsps argument is ignored.
2021  *
2022  * Returns 0 if successful, otherwise a negative error code.
2023  */
2024 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2025                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2026 {
2027         int i;
2028         unsigned int val, streams;
2029
2030         val = 0;
2031         if (nid != codec->afg &&
2032             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2033                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2034                 if (val == -1)
2035                         return -EIO;
2036         }
2037         if (!val)
2038                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2039
2040         if (ratesp) {
2041                 u32 rates = 0;
2042                 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2043                         if (val & (1 << i))
2044                                 rates |= rate_bits[i].alsa_bits;
2045                 }
2046                 *ratesp = rates;
2047         }
2048
2049         if (formatsp || bpsp) {
2050                 u64 formats = 0;
2051                 unsigned int bps;
2052                 unsigned int wcaps;
2053
2054                 wcaps = get_wcaps(codec, nid);
2055                 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2056                 if (streams == -1)
2057                         return -EIO;
2058                 if (!streams) {
2059                         streams = snd_hda_param_read(codec, codec->afg,
2060                                                      AC_PAR_STREAM);
2061                         if (streams == -1)
2062                                 return -EIO;
2063                 }
2064
2065                 bps = 0;
2066                 if (streams & AC_SUPFMT_PCM) {
2067                         if (val & AC_SUPPCM_BITS_8) {
2068                                 formats |= SNDRV_PCM_FMTBIT_U8;
2069                                 bps = 8;
2070                         }
2071                         if (val & AC_SUPPCM_BITS_16) {
2072                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2073                                 bps = 16;
2074                         }
2075                         if (wcaps & AC_WCAP_DIGITAL) {
2076                                 if (val & AC_SUPPCM_BITS_32)
2077                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2078                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2079                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
2080                                 if (val & AC_SUPPCM_BITS_24)
2081                                         bps = 24;
2082                                 else if (val & AC_SUPPCM_BITS_20)
2083                                         bps = 20;
2084                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2085                                           AC_SUPPCM_BITS_32)) {
2086                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2087                                 if (val & AC_SUPPCM_BITS_32)
2088                                         bps = 32;
2089                                 else if (val & AC_SUPPCM_BITS_24)
2090                                         bps = 24;
2091                                 else if (val & AC_SUPPCM_BITS_20)
2092                                         bps = 20;
2093                         }
2094                 }
2095                 else if (streams == AC_SUPFMT_FLOAT32) {
2096                         /* should be exclusive */
2097                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2098                         bps = 32;
2099                 } else if (streams == AC_SUPFMT_AC3) {
2100                         /* should be exclusive */
2101                         /* temporary hack: we have still no proper support
2102                          * for the direct AC3 stream...
2103                          */
2104                         formats |= SNDRV_PCM_FMTBIT_U8;
2105                         bps = 8;
2106                 }
2107                 if (formatsp)
2108                         *formatsp = formats;
2109                 if (bpsp)
2110                         *bpsp = bps;
2111         }
2112
2113         return 0;
2114 }
2115
2116 /**
2117  * snd_hda_is_supported_format - check whether the given node supports
2118  * the format val
2119  *
2120  * Returns 1 if supported, 0 if not.
2121  */
2122 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2123                                 unsigned int format)
2124 {
2125         int i;
2126         unsigned int val = 0, rate, stream;
2127
2128         if (nid != codec->afg &&
2129             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD)) {
2130                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2131                 if (val == -1)
2132                         return 0;
2133         }
2134         if (!val) {
2135                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2136                 if (val == -1)
2137                         return 0;
2138         }
2139
2140         rate = format & 0xff00;
2141         for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2142                 if (rate_bits[i].hda_fmt == rate) {
2143                         if (val & (1 << i))
2144                                 break;
2145                         return 0;
2146                 }
2147         if (i >= AC_PAR_PCM_RATE_BITS)
2148                 return 0;
2149
2150         stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2151         if (stream == -1)
2152                 return 0;
2153         if (!stream && nid != codec->afg)
2154                 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2155         if (!stream || stream == -1)
2156                 return 0;
2157
2158         if (stream & AC_SUPFMT_PCM) {
2159                 switch (format & 0xf0) {
2160                 case 0x00:
2161                         if (!(val & AC_SUPPCM_BITS_8))
2162                                 return 0;
2163                         break;
2164                 case 0x10:
2165                         if (!(val & AC_SUPPCM_BITS_16))
2166                                 return 0;
2167                         break;
2168                 case 0x20:
2169                         if (!(val & AC_SUPPCM_BITS_20))
2170                                 return 0;
2171                         break;
2172                 case 0x30:
2173                         if (!(val & AC_SUPPCM_BITS_24))
2174                                 return 0;
2175                         break;
2176                 case 0x40:
2177                         if (!(val & AC_SUPPCM_BITS_32))
2178                                 return 0;
2179                         break;
2180                 default:
2181                         return 0;
2182                 }
2183         } else {
2184                 /* FIXME: check for float32 and AC3? */
2185         }
2186
2187         return 1;
2188 }
2189
2190 /*
2191  * PCM stuff
2192  */
2193 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2194                                       struct hda_codec *codec,
2195                                       struct snd_pcm_substream *substream)
2196 {
2197         return 0;
2198 }
2199
2200 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2201                                    struct hda_codec *codec,
2202                                    unsigned int stream_tag,
2203                                    unsigned int format,
2204                                    struct snd_pcm_substream *substream)
2205 {
2206         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2207         return 0;
2208 }
2209
2210 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2211                                    struct hda_codec *codec,
2212                                    struct snd_pcm_substream *substream)
2213 {
2214         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2215         return 0;
2216 }
2217
2218 static int __devinit set_pcm_default_values(struct hda_codec *codec,
2219                                             struct hda_pcm_stream *info)
2220 {
2221         /* query support PCM information from the given NID */
2222         if (info->nid && (!info->rates || !info->formats)) {
2223                 snd_hda_query_supported_pcm(codec, info->nid,
2224                                 info->rates ? NULL : &info->rates,
2225                                 info->formats ? NULL : &info->formats,
2226                                 info->maxbps ? NULL : &info->maxbps);
2227         }
2228         if (info->ops.open == NULL)
2229                 info->ops.open = hda_pcm_default_open_close;
2230         if (info->ops.close == NULL)
2231                 info->ops.close = hda_pcm_default_open_close;
2232         if (info->ops.prepare == NULL) {
2233                 if (snd_BUG_ON(!info->nid))
2234                         return -EINVAL;
2235                 info->ops.prepare = hda_pcm_default_prepare;
2236         }
2237         if (info->ops.cleanup == NULL) {
2238                 if (snd_BUG_ON(!info->nid))
2239                         return -EINVAL;
2240                 info->ops.cleanup = hda_pcm_default_cleanup;
2241         }
2242         return 0;
2243 }
2244
2245 /*
2246  * attach a new PCM stream
2247  */
2248 static int __devinit
2249 snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2250 {
2251         struct hda_pcm_stream *info;
2252         int stream, err;
2253
2254         if (!pcm->name)
2255                 return -EINVAL;
2256         for (stream = 0; stream < 2; stream++) {
2257                 info = &pcm->stream[stream];
2258                 if (info->substreams) {
2259                         err = set_pcm_default_values(codec, info);
2260                         if (err < 0)
2261                                 return err;
2262                 }
2263         }
2264         return codec->bus->ops.attach_pcm(codec, pcm);
2265 }
2266
2267 /**
2268  * snd_hda_build_pcms - build PCM information
2269  * @bus: the BUS
2270  *
2271  * Create PCM information for each codec included in the bus.
2272  *
2273  * The build_pcms codec patch is requested to set up codec->num_pcms and
2274  * codec->pcm_info properly.  The array is referred by the top-level driver
2275  * to create its PCM instances.
2276  * The allocated codec->pcm_info should be released in codec->patch_ops.free
2277  * callback.
2278  *
2279  * At least, substreams, channels_min and channels_max must be filled for
2280  * each stream.  substreams = 0 indicates that the stream doesn't exist.
2281  * When rates and/or formats are zero, the supported values are queried
2282  * from the given nid.  The nid is used also by the default ops.prepare
2283  * and ops.cleanup callbacks.
2284  *
2285  * The driver needs to call ops.open in its open callback.  Similarly,
2286  * ops.close is supposed to be called in the close callback.
2287  * ops.prepare should be called in the prepare or hw_params callback
2288  * with the proper parameters for set up.
2289  * ops.cleanup should be called in hw_free for clean up of streams.
2290  *
2291  * This function returns 0 if successfull, or a negative error code.
2292  */
2293 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
2294 {
2295         static const char *dev_name[HDA_PCM_NTYPES] = {
2296                 "Audio", "SPDIF", "HDMI", "Modem"
2297         };
2298         /* starting device index for each PCM type */
2299         static int dev_idx[HDA_PCM_NTYPES] = {
2300                 [HDA_PCM_TYPE_AUDIO] = 0,
2301                 [HDA_PCM_TYPE_SPDIF] = 1,
2302                 [HDA_PCM_TYPE_HDMI] = 3,
2303                 [HDA_PCM_TYPE_MODEM] = 6
2304         };
2305         /* normal audio device indices; not linear to keep compatibility */
2306         static int audio_idx[4] = { 0, 2, 4, 5 };
2307         struct hda_codec *codec;
2308         int num_devs[HDA_PCM_NTYPES];
2309
2310         memset(num_devs, 0, sizeof(num_devs));
2311         list_for_each_entry(codec, &bus->codec_list, list) {
2312                 unsigned int pcm;
2313                 int err;
2314                 if (!codec->patch_ops.build_pcms)
2315                         continue;
2316                 err = codec->patch_ops.build_pcms(codec);
2317                 if (err < 0)
2318                         return err;
2319                 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2320                         struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2321                         int type = cpcm->pcm_type;
2322                         switch (type) {
2323                         case HDA_PCM_TYPE_AUDIO:
2324                                 if (num_devs[type] >= ARRAY_SIZE(audio_idx)) {
2325                                         snd_printk(KERN_WARNING
2326                                                    "Too many audio devices\n");
2327                                         continue;
2328                                 }
2329                                 cpcm->device = audio_idx[num_devs[type]];
2330                                 break;
2331                         case HDA_PCM_TYPE_SPDIF:
2332                         case HDA_PCM_TYPE_HDMI:
2333                         case HDA_PCM_TYPE_MODEM:
2334                                 if (num_devs[type]) {
2335                                         snd_printk(KERN_WARNING
2336                                                    "%s already defined\n",
2337                                                    dev_name[type]);
2338                                         continue;
2339                                 }
2340                                 cpcm->device = dev_idx[type];
2341                                 break;
2342                         default:
2343                                 snd_printk(KERN_WARNING
2344                                            "Invalid PCM type %d\n", type);
2345                                 continue;
2346                         }
2347                         num_devs[type]++;
2348                         err = snd_hda_attach_pcm(codec, cpcm);
2349                         if (err < 0)
2350                                 return err;
2351                 }
2352         }
2353         return 0;
2354 }
2355
2356 /**
2357  * snd_hda_check_board_config - compare the current codec with the config table
2358  * @codec: the HDA codec
2359  * @num_configs: number of config enums
2360  * @models: array of model name strings
2361  * @tbl: configuration table, terminated by null entries
2362  *
2363  * Compares the modelname or PCI subsystem id of the current codec with the
2364  * given configuration table.  If a matching entry is found, returns its
2365  * config value (supposed to be 0 or positive).
2366  *
2367  * If no entries are matching, the function returns a negative value.
2368  */
2369 int snd_hda_check_board_config(struct hda_codec *codec,
2370                                int num_configs, const char **models,
2371                                const struct snd_pci_quirk *tbl)
2372 {
2373         if (codec->bus->modelname && models) {
2374                 int i;
2375                 for (i = 0; i < num_configs; i++) {
2376                         if (models[i] &&
2377                             !strcmp(codec->bus->modelname, models[i])) {
2378                                 snd_printd(KERN_INFO "hda_codec: model '%s' is "
2379                                            "selected\n", models[i]);
2380                                 return i;
2381                         }
2382                 }
2383         }
2384
2385         if (!codec->bus->pci || !tbl)
2386                 return -1;
2387
2388         tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
2389         if (!tbl)
2390                 return -1;
2391         if (tbl->value >= 0 && tbl->value < num_configs) {
2392 #ifdef CONFIG_SND_DEBUG_VERBOSE
2393                 char tmp[10];
2394                 const char *model = NULL;
2395                 if (models)
2396                         model = models[tbl->value];
2397                 if (!model) {
2398                         sprintf(tmp, "#%d", tbl->value);
2399                         model = tmp;
2400                 }
2401                 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
2402                             "for config %x:%x (%s)\n",
2403                             model, tbl->subvendor, tbl->subdevice,
2404                             (tbl->name ? tbl->name : "Unknown device"));
2405 #endif
2406                 return tbl->value;
2407         }
2408         return -1;
2409 }
2410
2411 /**
2412  * snd_hda_add_new_ctls - create controls from the array
2413  * @codec: the HDA codec
2414  * @knew: the array of struct snd_kcontrol_new
2415  *
2416  * This helper function creates and add new controls in the given array.
2417  * The array must be terminated with an empty entry as terminator.
2418  *
2419  * Returns 0 if successful, or a negative error code.
2420  */
2421 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
2422 {
2423         int err;
2424
2425         for (; knew->name; knew++) {
2426                 struct snd_kcontrol *kctl;
2427                 kctl = snd_ctl_new1(knew, codec);
2428                 if (!kctl)
2429                         return -ENOMEM;
2430                 err = snd_ctl_add(codec->bus->card, kctl);
2431                 if (err < 0) {
2432                         if (!codec->addr)
2433                                 return err;
2434                         kctl = snd_ctl_new1(knew, codec);
2435                         if (!kctl)
2436                                 return -ENOMEM;
2437                         kctl->id.device = codec->addr;
2438                         err = snd_ctl_add(codec->bus->card, kctl);
2439                         if (err < 0)
2440                                 return err;
2441                 }
2442         }
2443         return 0;
2444 }
2445
2446 #ifdef CONFIG_SND_HDA_POWER_SAVE
2447 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2448                                 unsigned int power_state);
2449
2450 static void hda_power_work(struct work_struct *work)
2451 {
2452         struct hda_codec *codec =
2453                 container_of(work, struct hda_codec, power_work.work);
2454
2455         if (!codec->power_on || codec->power_count) {
2456                 codec->power_transition = 0;
2457                 return;
2458         }
2459
2460         hda_call_codec_suspend(codec);
2461         if (codec->bus->ops.pm_notify)
2462                 codec->bus->ops.pm_notify(codec);
2463 }
2464
2465 static void hda_keep_power_on(struct hda_codec *codec)
2466 {
2467         codec->power_count++;
2468         codec->power_on = 1;
2469 }
2470
2471 void snd_hda_power_up(struct hda_codec *codec)
2472 {
2473         codec->power_count++;
2474         if (codec->power_on || codec->power_transition)
2475                 return;
2476
2477         codec->power_on = 1;
2478         if (codec->bus->ops.pm_notify)
2479                 codec->bus->ops.pm_notify(codec);
2480         hda_call_codec_resume(codec);
2481         cancel_delayed_work(&codec->power_work);
2482         codec->power_transition = 0;
2483 }
2484
2485 void snd_hda_power_down(struct hda_codec *codec)
2486 {
2487         --codec->power_count;
2488         if (!codec->power_on || codec->power_count || codec->power_transition)
2489                 return;
2490         if (power_save) {
2491                 codec->power_transition = 1; /* avoid reentrance */
2492                 schedule_delayed_work(&codec->power_work,
2493                                       msecs_to_jiffies(power_save * 1000));
2494         }
2495 }
2496
2497 int snd_hda_check_amp_list_power(struct hda_codec *codec,
2498                                  struct hda_loopback_check *check,
2499                                  hda_nid_t nid)
2500 {
2501         struct hda_amp_list *p;
2502         int ch, v;
2503
2504         if (!check->amplist)
2505                 return 0;
2506         for (p = check->amplist; p->nid; p++) {
2507                 if (p->nid == nid)
2508                         break;
2509         }
2510         if (!p->nid)
2511                 return 0; /* nothing changed */
2512
2513         for (p = check->amplist; p->nid; p++) {
2514                 for (ch = 0; ch < 2; ch++) {
2515                         v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
2516                                                    p->idx);
2517                         if (!(v & HDA_AMP_MUTE) && v > 0) {
2518                                 if (!check->power_on) {
2519                                         check->power_on = 1;
2520                                         snd_hda_power_up(codec);
2521                                 }
2522                                 return 1;
2523                         }
2524                 }
2525         }
2526         if (check->power_on) {
2527                 check->power_on = 0;
2528                 snd_hda_power_down(codec);
2529         }
2530         return 0;
2531 }
2532 #endif
2533
2534 /*
2535  * Channel mode helper
2536  */
2537 int snd_hda_ch_mode_info(struct hda_codec *codec,
2538                          struct snd_ctl_elem_info *uinfo,
2539                          const struct hda_channel_mode *chmode,
2540                          int num_chmodes)
2541 {
2542         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2543         uinfo->count = 1;
2544         uinfo->value.enumerated.items = num_chmodes;
2545         if (uinfo->value.enumerated.item >= num_chmodes)
2546                 uinfo->value.enumerated.item = num_chmodes - 1;
2547         sprintf(uinfo->value.enumerated.name, "%dch",
2548                 chmode[uinfo->value.enumerated.item].channels);
2549         return 0;
2550 }
2551
2552 int snd_hda_ch_mode_get(struct hda_codec *codec,
2553                         struct snd_ctl_elem_value *ucontrol,
2554                         const struct hda_channel_mode *chmode,
2555                         int num_chmodes,
2556                         int max_channels)
2557 {
2558         int i;
2559
2560         for (i = 0; i < num_chmodes; i++) {
2561                 if (max_channels == chmode[i].channels) {
2562                         ucontrol->value.enumerated.item[0] = i;
2563                         break;
2564                 }
2565         }
2566         return 0;
2567 }
2568
2569 int snd_hda_ch_mode_put(struct hda_codec *codec,
2570                         struct snd_ctl_elem_value *ucontrol,
2571                         const struct hda_channel_mode *chmode,
2572                         int num_chmodes,
2573                         int *max_channelsp)
2574 {
2575         unsigned int mode;
2576
2577         mode = ucontrol->value.enumerated.item[0];
2578         if (mode >= num_chmodes)
2579                 return -EINVAL;
2580         if (*max_channelsp == chmode[mode].channels)
2581                 return 0;
2582         /* change the current channel setting */
2583         *max_channelsp = chmode[mode].channels;
2584         if (chmode[mode].sequence)
2585                 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
2586         return 1;
2587 }
2588
2589 /*
2590  * input MUX helper
2591  */
2592 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
2593                            struct snd_ctl_elem_info *uinfo)
2594 {
2595         unsigned int index;
2596
2597         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
2598         uinfo->count = 1;
2599         uinfo->value.enumerated.items = imux->num_items;
2600         if (!imux->num_items)
2601                 return 0;
2602         index = uinfo->value.enumerated.item;
2603         if (index >= imux->num_items)
2604                 index = imux->num_items - 1;
2605         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
2606         return 0;
2607 }
2608
2609 int snd_hda_input_mux_put(struct hda_codec *codec,
2610                           const struct hda_input_mux *imux,
2611                           struct snd_ctl_elem_value *ucontrol,
2612                           hda_nid_t nid,
2613                           unsigned int *cur_val)
2614 {
2615         unsigned int idx;
2616
2617         if (!imux->num_items)
2618                 return 0;
2619         idx = ucontrol->value.enumerated.item[0];
2620         if (idx >= imux->num_items)
2621                 idx = imux->num_items - 1;
2622         if (*cur_val == idx)
2623                 return 0;
2624         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
2625                                   imux->items[idx].index);
2626         *cur_val = idx;
2627         return 1;
2628 }
2629
2630
2631 /*
2632  * Multi-channel / digital-out PCM helper functions
2633  */
2634
2635 /* setup SPDIF output stream */
2636 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
2637                                  unsigned int stream_tag, unsigned int format)
2638 {
2639         /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
2640         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2641                 set_dig_out_convert(codec, nid, 
2642                                     codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
2643                                     -1);
2644         snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
2645         if (codec->slave_dig_outs) {
2646                 hda_nid_t *d;
2647                 for (d = codec->slave_dig_outs; *d; d++)
2648                         snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
2649                                                    format);
2650         }
2651         /* turn on again (if needed) */
2652         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
2653                 set_dig_out_convert(codec, nid,
2654                                     codec->spdif_ctls & 0xff, -1);
2655 }
2656
2657 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
2658 {
2659         snd_hda_codec_cleanup_stream(codec, nid);
2660         if (codec->slave_dig_outs) {
2661                 hda_nid_t *d;
2662                 for (d = codec->slave_dig_outs; *d; d++)
2663                         snd_hda_codec_cleanup_stream(codec, *d);
2664         }
2665 }
2666
2667 /*
2668  * open the digital out in the exclusive mode
2669  */
2670 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
2671                                struct hda_multi_out *mout)
2672 {
2673         mutex_lock(&codec->spdif_mutex);
2674         if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
2675                 /* already opened as analog dup; reset it once */
2676                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2677         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
2678         mutex_unlock(&codec->spdif_mutex);
2679         return 0;
2680 }
2681
2682 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
2683                                   struct hda_multi_out *mout,
2684                                   unsigned int stream_tag,
2685                                   unsigned int format,
2686                                   struct snd_pcm_substream *substream)
2687 {
2688         mutex_lock(&codec->spdif_mutex);
2689         setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
2690         mutex_unlock(&codec->spdif_mutex);
2691         return 0;
2692 }
2693
2694 /*
2695  * release the digital out
2696  */
2697 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
2698                                 struct hda_multi_out *mout)
2699 {
2700         mutex_lock(&codec->spdif_mutex);
2701         mout->dig_out_used = 0;
2702         mutex_unlock(&codec->spdif_mutex);
2703         return 0;
2704 }
2705
2706 /*
2707  * set up more restrictions for analog out
2708  */
2709 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
2710                                   struct hda_multi_out *mout,
2711                                   struct snd_pcm_substream *substream,
2712                                   struct hda_pcm_stream *hinfo)
2713 {
2714         struct snd_pcm_runtime *runtime = substream->runtime;
2715         runtime->hw.channels_max = mout->max_channels;
2716         if (mout->dig_out_nid) {
2717                 if (!mout->analog_rates) {
2718                         mout->analog_rates = hinfo->rates;
2719                         mout->analog_formats = hinfo->formats;
2720                         mout->analog_maxbps = hinfo->maxbps;
2721                 } else {
2722                         runtime->hw.rates = mout->analog_rates;
2723                         runtime->hw.formats = mout->analog_formats;
2724                         hinfo->maxbps = mout->analog_maxbps;
2725                 }
2726                 if (!mout->spdif_rates) {
2727                         snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
2728                                                     &mout->spdif_rates,
2729                                                     &mout->spdif_formats,
2730                                                     &mout->spdif_maxbps);
2731                 }
2732                 mutex_lock(&codec->spdif_mutex);
2733                 if (mout->share_spdif) {
2734                         runtime->hw.rates &= mout->spdif_rates;
2735                         runtime->hw.formats &= mout->spdif_formats;
2736                         if (mout->spdif_maxbps < hinfo->maxbps)
2737                                 hinfo->maxbps = mout->spdif_maxbps;
2738                 }
2739                 mutex_unlock(&codec->spdif_mutex);
2740         }
2741         return snd_pcm_hw_constraint_step(substream->runtime, 0,
2742                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
2743 }
2744
2745 /*
2746  * set up the i/o for analog out
2747  * when the digital out is available, copy the front out to digital out, too.
2748  */
2749 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
2750                                      struct hda_multi_out *mout,
2751                                      unsigned int stream_tag,
2752                                      unsigned int format,
2753                                      struct snd_pcm_substream *substream)
2754 {
2755         hda_nid_t *nids = mout->dac_nids;
2756         int chs = substream->runtime->channels;
2757         int i;
2758
2759         mutex_lock(&codec->spdif_mutex);
2760         if (mout->dig_out_nid && mout->share_spdif &&
2761             mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
2762                 if (chs == 2 &&
2763                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
2764                                                 format) &&
2765                     !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
2766                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
2767                         setup_dig_out_stream(codec, mout->dig_out_nid,
2768                                              stream_tag, format);
2769                 } else {
2770                         mout->dig_out_used = 0;
2771                         cleanup_dig_out_stream(codec, mout->dig_out_nid);
2772                 }
2773         }
2774         mutex_unlock(&codec->spdif_mutex);
2775
2776         /* front */
2777         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
2778                                    0, format);
2779         if (!mout->no_share_stream &&
2780             mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
2781                 /* headphone out will just decode front left/right (stereo) */
2782                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
2783                                            0, format);
2784         /* extra outputs copied from front */
2785         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2786                 if (!mout->no_share_stream && mout->extra_out_nid[i])
2787                         snd_hda_codec_setup_stream(codec,
2788                                                    mout->extra_out_nid[i],
2789                                                    stream_tag, 0, format);
2790
2791         /* surrounds */
2792         for (i = 1; i < mout->num_dacs; i++) {
2793                 if (chs >= (i + 1) * 2) /* independent out */
2794                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2795                                                    i * 2, format);
2796                 else if (!mout->no_share_stream) /* copy front */
2797                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
2798                                                    0, format);
2799         }
2800         return 0;
2801 }
2802
2803 /*
2804  * clean up the setting for analog out
2805  */
2806 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
2807                                      struct hda_multi_out *mout)
2808 {
2809         hda_nid_t *nids = mout->dac_nids;
2810         int i;
2811
2812         for (i = 0; i < mout->num_dacs; i++)
2813                 snd_hda_codec_cleanup_stream(codec, nids[i]);
2814         if (mout->hp_nid)
2815                 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
2816         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
2817                 if (mout->extra_out_nid[i])
2818                         snd_hda_codec_cleanup_stream(codec,
2819                                                      mout->extra_out_nid[i]);
2820         mutex_lock(&codec->spdif_mutex);
2821         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
2822                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
2823                 mout->dig_out_used = 0;
2824         }
2825         mutex_unlock(&codec->spdif_mutex);
2826         return 0;
2827 }
2828
2829 /*
2830  * Helper for automatic pin configuration
2831  */
2832
2833 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
2834 {
2835         for (; *list; list++)
2836                 if (*list == nid)
2837                         return 1;
2838         return 0;
2839 }
2840
2841
2842 /*
2843  * Sort an associated group of pins according to their sequence numbers.
2844  */
2845 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
2846                                   int num_pins)
2847 {
2848         int i, j;
2849         short seq;
2850         hda_nid_t nid;
2851         
2852         for (i = 0; i < num_pins; i++) {
2853                 for (j = i + 1; j < num_pins; j++) {
2854                         if (sequences[i] > sequences[j]) {
2855                                 seq = sequences[i];
2856                                 sequences[i] = sequences[j];
2857                                 sequences[j] = seq;
2858                                 nid = pins[i];
2859                                 pins[i] = pins[j];
2860                                 pins[j] = nid;
2861                         }
2862                 }
2863         }
2864 }
2865
2866
2867 /*
2868  * Parse all pin widgets and store the useful pin nids to cfg
2869  *
2870  * The number of line-outs or any primary output is stored in line_outs,
2871  * and the corresponding output pins are assigned to line_out_pins[],
2872  * in the order of front, rear, CLFE, side, ...
2873  *
2874  * If more extra outputs (speaker and headphone) are found, the pins are
2875  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
2876  * is detected, one of speaker of HP pins is assigned as the primary
2877  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
2878  * if any analog output exists.
2879  * 
2880  * The analog input pins are assigned to input_pins array.
2881  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
2882  * respectively.
2883  */
2884 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
2885                                  struct auto_pin_cfg *cfg,
2886                                  hda_nid_t *ignore_nids)
2887 {
2888         hda_nid_t nid, end_nid;
2889         short seq, assoc_line_out, assoc_speaker;
2890         short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
2891         short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
2892         short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
2893
2894         memset(cfg, 0, sizeof(*cfg));
2895
2896         memset(sequences_line_out, 0, sizeof(sequences_line_out));
2897         memset(sequences_speaker, 0, sizeof(sequences_speaker));
2898         memset(sequences_hp, 0, sizeof(sequences_hp));
2899         assoc_line_out = assoc_speaker = 0;
2900
2901         end_nid = codec->start_nid + codec->num_nodes;
2902         for (nid = codec->start_nid; nid < end_nid; nid++) {
2903                 unsigned int wid_caps = get_wcaps(codec, nid);
2904                 unsigned int wid_type =
2905                         (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
2906                 unsigned int def_conf;
2907                 short assoc, loc;
2908
2909                 /* read all default configuration for pin complex */
2910                 if (wid_type != AC_WID_PIN)
2911                         continue;
2912                 /* ignore the given nids (e.g. pc-beep returns error) */
2913                 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
2914                         continue;
2915
2916                 def_conf = snd_hda_codec_read(codec, nid, 0,
2917                                               AC_VERB_GET_CONFIG_DEFAULT, 0);
2918                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
2919                         continue;
2920                 loc = get_defcfg_location(def_conf);
2921                 switch (get_defcfg_device(def_conf)) {
2922                 case AC_JACK_LINE_OUT:
2923                         seq = get_defcfg_sequence(def_conf);
2924                         assoc = get_defcfg_association(def_conf);
2925
2926                         if (!(wid_caps & AC_WCAP_STEREO))
2927                                 if (!cfg->mono_out_pin)
2928                                         cfg->mono_out_pin = nid;
2929                         if (!assoc)
2930                                 continue;
2931                         if (!assoc_line_out)
2932                                 assoc_line_out = assoc;
2933                         else if (assoc_line_out != assoc)
2934                                 continue;
2935                         if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
2936                                 continue;
2937                         cfg->line_out_pins[cfg->line_outs] = nid;
2938                         sequences_line_out[cfg->line_outs] = seq;
2939                         cfg->line_outs++;
2940                         break;
2941                 case AC_JACK_SPEAKER:
2942                         seq = get_defcfg_sequence(def_conf);
2943                         assoc = get_defcfg_association(def_conf);
2944                         if (! assoc)
2945                                 continue;
2946                         if (! assoc_speaker)
2947                                 assoc_speaker = assoc;
2948                         else if (assoc_speaker != assoc)
2949                                 continue;
2950                         if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
2951                                 continue;
2952                         cfg->speaker_pins[cfg->speaker_outs] = nid;
2953                         sequences_speaker[cfg->speaker_outs] = seq;
2954                         cfg->speaker_outs++;
2955                         break;
2956                 case AC_JACK_HP_OUT:
2957                         seq = get_defcfg_sequence(def_conf);
2958                         assoc = get_defcfg_association(def_conf);
2959                         if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
2960                                 continue;
2961                         cfg->hp_pins[cfg->hp_outs] = nid;
2962                         sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
2963                         cfg->hp_outs++;
2964                         break;
2965                 case AC_JACK_MIC_IN: {
2966                         int preferred, alt;
2967                         if (loc == AC_JACK_LOC_FRONT) {
2968                                 preferred = AUTO_PIN_FRONT_MIC;
2969                                 alt = AUTO_PIN_MIC;
2970                         } else {
2971                                 preferred = AUTO_PIN_MIC;
2972                                 alt = AUTO_PIN_FRONT_MIC;
2973                         }
2974                         if (!cfg->input_pins[preferred])
2975                                 cfg->input_pins[preferred] = nid;
2976                         else if (!cfg->input_pins[alt])
2977                                 cfg->input_pins[alt] = nid;
2978                         break;
2979                 }
2980                 case AC_JACK_LINE_IN:
2981                         if (loc == AC_JACK_LOC_FRONT)
2982                                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
2983                         else
2984                                 cfg->input_pins[AUTO_PIN_LINE] = nid;
2985                         break;
2986                 case AC_JACK_CD:
2987                         cfg->input_pins[AUTO_PIN_CD] = nid;
2988                         break;
2989                 case AC_JACK_AUX:
2990                         cfg->input_pins[AUTO_PIN_AUX] = nid;
2991                         break;
2992                 case AC_JACK_SPDIF_OUT:
2993                         cfg->dig_out_pin = nid;
2994                         break;
2995                 case AC_JACK_SPDIF_IN:
2996                         cfg->dig_in_pin = nid;
2997                         break;
2998                 }
2999         }
3000
3001         /* FIX-UP:
3002          * If no line-out is defined but multiple HPs are found,
3003          * some of them might be the real line-outs.
3004          */
3005         if (!cfg->line_outs && cfg->hp_outs > 1) {
3006                 int i = 0;
3007                 while (i < cfg->hp_outs) {
3008                         /* The real HPs should have the sequence 0x0f */
3009                         if ((sequences_hp[i] & 0x0f) == 0x0f) {
3010                                 i++;
3011                                 continue;
3012                         }
3013                         /* Move it to the line-out table */
3014                         cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3015                         sequences_line_out[cfg->line_outs] = sequences_hp[i];
3016                         cfg->line_outs++;
3017                         cfg->hp_outs--;
3018                         memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3019                                 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3020                         memmove(sequences_hp + i - 1, sequences_hp + i,
3021                                 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3022                 }
3023         }
3024
3025         /* sort by sequence */
3026         sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3027                               cfg->line_outs);
3028         sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3029                               cfg->speaker_outs);
3030         sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3031                               cfg->hp_outs);
3032         
3033         /* if we have only one mic, make it AUTO_PIN_MIC */
3034         if (!cfg->input_pins[AUTO_PIN_MIC] &&
3035             cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3036                 cfg->input_pins[AUTO_PIN_MIC] =
3037                         cfg->input_pins[AUTO_PIN_FRONT_MIC];
3038                 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3039         }
3040         /* ditto for line-in */
3041         if (!cfg->input_pins[AUTO_PIN_LINE] &&
3042             cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3043                 cfg->input_pins[AUTO_PIN_LINE] =
3044                         cfg->input_pins[AUTO_PIN_FRONT_LINE];
3045                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3046         }
3047
3048         /*
3049          * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3050          * as a primary output
3051          */
3052         if (!cfg->line_outs) {
3053                 if (cfg->speaker_outs) {
3054                         cfg->line_outs = cfg->speaker_outs;
3055                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3056                                sizeof(cfg->speaker_pins));
3057                         cfg->speaker_outs = 0;
3058                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3059                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3060                 } else if (cfg->hp_outs) {
3061                         cfg->line_outs = cfg->hp_outs;
3062                         memcpy(cfg->line_out_pins, cfg->hp_pins,
3063                                sizeof(cfg->hp_pins));
3064                         cfg->hp_outs = 0;
3065                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3066                         cfg->line_out_type = AUTO_PIN_HP_OUT;
3067                 }
3068         }
3069
3070         /* Reorder the surround channels
3071          * ALSA sequence is front/surr/clfe/side
3072          * HDA sequence is:
3073          *    4-ch: front/surr  =>  OK as it is
3074          *    6-ch: front/clfe/surr
3075          *    8-ch: front/clfe/rear/side|fc
3076          */
3077         switch (cfg->line_outs) {
3078         case 3:
3079         case 4:
3080                 nid = cfg->line_out_pins[1];
3081                 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3082                 cfg->line_out_pins[2] = nid;
3083                 break;
3084         }
3085
3086         /*
3087          * debug prints of the parsed results
3088          */
3089         snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3090                    cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3091                    cfg->line_out_pins[2], cfg->line_out_pins[3],
3092                    cfg->line_out_pins[4]);
3093         snd_printd("   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3094                    cfg->speaker_outs, cfg->speaker_pins[0],
3095                    cfg->speaker_pins[1], cfg->speaker_pins[2],
3096                    cfg->speaker_pins[3], cfg->speaker_pins[4]);
3097         snd_printd("   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3098                    cfg->hp_outs, cfg->hp_pins[0],
3099                    cfg->hp_pins[1], cfg->hp_pins[2],
3100                    cfg->hp_pins[3], cfg->hp_pins[4]);
3101         snd_printd("   mono: mono_out=0x%x\n", cfg->mono_out_pin);
3102         snd_printd("   inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3103                    " cd=0x%x, aux=0x%x\n",
3104                    cfg->input_pins[AUTO_PIN_MIC],
3105                    cfg->input_pins[AUTO_PIN_FRONT_MIC],
3106                    cfg->input_pins[AUTO_PIN_LINE],
3107                    cfg->input_pins[AUTO_PIN_FRONT_LINE],
3108                    cfg->input_pins[AUTO_PIN_CD],
3109                    cfg->input_pins[AUTO_PIN_AUX]);
3110
3111         return 0;
3112 }
3113
3114 /* labels for input pins */
3115 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3116         "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3117 };
3118
3119
3120 #ifdef CONFIG_PM
3121 /*
3122  * power management
3123  */
3124
3125 /**
3126  * snd_hda_suspend - suspend the codecs
3127  * @bus: the HDA bus
3128  * @state: suspsend state
3129  *
3130  * Returns 0 if successful.
3131  */
3132 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
3133 {
3134         struct hda_codec *codec;
3135
3136         list_for_each_entry(codec, &bus->codec_list, list) {
3137 #ifdef CONFIG_SND_HDA_POWER_SAVE
3138                 if (!codec->power_on)
3139                         continue;
3140 #endif
3141                 hda_call_codec_suspend(codec);
3142         }
3143         return 0;
3144 }
3145
3146 /**
3147  * snd_hda_resume - resume the codecs
3148  * @bus: the HDA bus
3149  * @state: resume state
3150  *
3151  * Returns 0 if successful.
3152  *
3153  * This fucntion is defined only when POWER_SAVE isn't set.
3154  * In the power-save mode, the codec is resumed dynamically.
3155  */
3156 int snd_hda_resume(struct hda_bus *bus)
3157 {
3158         struct hda_codec *codec;
3159
3160         list_for_each_entry(codec, &bus->codec_list, list) {
3161                 if (snd_hda_codec_needs_resume(codec))
3162                         hda_call_codec_resume(codec);
3163         }
3164         return 0;
3165 }
3166 #ifdef CONFIG_SND_HDA_POWER_SAVE
3167 int snd_hda_codecs_inuse(struct hda_bus *bus)
3168 {
3169         struct hda_codec *codec;
3170
3171         list_for_each_entry(codec, &bus->codec_list, list) {
3172                 if (snd_hda_codec_needs_resume(codec))
3173                         return 1;
3174         }
3175         return 0;
3176 }
3177 #endif
3178 #endif
3179
3180 /*
3181  * generic arrays
3182  */
3183
3184 /* get a new element from the given array
3185  * if it exceeds the pre-allocated array size, re-allocate the array
3186  */
3187 void *snd_array_new(struct snd_array *array)
3188 {
3189         if (array->used >= array->alloced) {
3190                 int num = array->alloced + array->alloc_align;
3191                 void *nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3192                 if (!nlist)
3193                         return NULL;
3194                 if (array->list) {
3195                         memcpy(nlist, array->list,
3196                                array->elem_size * array->alloced);
3197                         kfree(array->list);
3198                 }
3199                 array->list = nlist;
3200                 array->alloced = num;
3201         }
3202         return array->list + (array->used++ * array->elem_size);
3203 }
3204
3205 /* free the given array elements */
3206 void snd_array_free(struct snd_array *array)
3207 {
3208         kfree(array->list);
3209         array->used = 0;
3210         array->alloced = 0;
3211         array->list = NULL;
3212 }