[ALSA] hda-codec - Prepare unsol workqueue on demand
[linux-2.6.git] / sound / pci / hda / hda_codec.c
1 /*
2  * Universal Interface for Intel High Definition Audio Codec
3  *
4  * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
5  *
6  *
7  *  This driver is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This driver is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20  */
21
22 #include <sound/driver.h>
23 #include <linux/init.h>
24 #include <linux/delay.h>
25 #include <linux/slab.h>
26 #include <linux/pci.h>
27 #include <linux/moduleparam.h>
28 #include <sound/core.h>
29 #include "hda_codec.h"
30 #include <sound/asoundef.h>
31 #include <sound/initval.h>
32 #include "hda_local.h"
33
34
35 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
36 MODULE_DESCRIPTION("Universal interface for High Definition Audio Codec");
37 MODULE_LICENSE("GPL");
38
39
40 /*
41  * vendor / preset table
42  */
43
44 struct hda_vendor_id {
45         unsigned int id;
46         const char *name;
47 };
48
49 /* codec vendor labels */
50 static struct hda_vendor_id hda_vendor_ids[] = {
51         { 0x10ec, "Realtek" },
52         { 0x11d4, "Analog Devices" },
53         { 0x13f6, "C-Media" },
54         { 0x434d, "C-Media" },
55         { 0x8384, "SigmaTel" },
56         {} /* terminator */
57 };
58
59 /* codec presets */
60 #include "hda_patch.h"
61
62
63 /**
64  * snd_hda_codec_read - send a command and get the response
65  * @codec: the HDA codec
66  * @nid: NID to send the command
67  * @direct: direct flag
68  * @verb: the verb to send
69  * @parm: the parameter for the verb
70  *
71  * Send a single command and read the corresponding response.
72  *
73  * Returns the obtained response value, or -1 for an error.
74  */
75 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid, int direct,
76                                 unsigned int verb, unsigned int parm)
77 {
78         unsigned int res;
79         down(&codec->bus->cmd_mutex);
80         if (! codec->bus->ops.command(codec, nid, direct, verb, parm))
81                 res = codec->bus->ops.get_response(codec);
82         else
83                 res = (unsigned int)-1;
84         up(&codec->bus->cmd_mutex);
85         return res;
86 }
87
88 /**
89  * snd_hda_codec_write - send a single command without waiting for response
90  * @codec: the HDA codec
91  * @nid: NID to send the command
92  * @direct: direct flag
93  * @verb: the verb to send
94  * @parm: the parameter for the verb
95  *
96  * Send a single command without waiting for response.
97  *
98  * Returns 0 if successful, or a negative error code.
99  */
100 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
101                          unsigned int verb, unsigned int parm)
102 {
103         int err;
104         down(&codec->bus->cmd_mutex);
105         err = codec->bus->ops.command(codec, nid, direct, verb, parm);
106         up(&codec->bus->cmd_mutex);
107         return err;
108 }
109
110 /**
111  * snd_hda_sequence_write - sequence writes
112  * @codec: the HDA codec
113  * @seq: VERB array to send
114  *
115  * Send the commands sequentially from the given array.
116  * The array must be terminated with NID=0.
117  */
118 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
119 {
120         for (; seq->nid; seq++)
121                 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
122 }
123
124 /**
125  * snd_hda_get_sub_nodes - get the range of sub nodes
126  * @codec: the HDA codec
127  * @nid: NID to parse
128  * @start_id: the pointer to store the start NID
129  *
130  * Parse the NID and store the start NID of its sub-nodes.
131  * Returns the number of sub-nodes.
132  */
133 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid, hda_nid_t *start_id)
134 {
135         unsigned int parm;
136
137         parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
138         *start_id = (parm >> 16) & 0x7fff;
139         return (int)(parm & 0x7fff);
140 }
141
142 /**
143  * snd_hda_get_connections - get connection list
144  * @codec: the HDA codec
145  * @nid: NID to parse
146  * @conn_list: connection list array
147  * @max_conns: max. number of connections to store
148  *
149  * Parses the connection list of the given widget and stores the list
150  * of NIDs.
151  *
152  * Returns the number of connections, or a negative error code.
153  */
154 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
155                             hda_nid_t *conn_list, int max_conns)
156 {
157         unsigned int parm;
158         int i, j, conn_len, num_tupples, conns;
159         unsigned int shift, num_elems, mask;
160
161         snd_assert(conn_list && max_conns > 0, return -EINVAL);
162
163         parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
164         if (parm & AC_CLIST_LONG) {
165                 /* long form */
166                 shift = 16;
167                 num_elems = 2;
168         } else {
169                 /* short form */
170                 shift = 8;
171                 num_elems = 4;
172         }
173         conn_len = parm & AC_CLIST_LENGTH;
174         num_tupples = num_elems / 2;
175         mask = (1 << (shift-1)) - 1;
176
177         if (! conn_len)
178                 return 0; /* no connection */
179
180         if (conn_len == 1) {
181                 /* single connection */
182                 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, 0);
183                 conn_list[0] = parm & mask;
184                 return 1;
185         }
186
187         /* multi connection */
188         conns = 0;
189         for (i = 0; i < conn_len; i += num_elems) {
190                 parm = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONNECT_LIST, i);
191                 for (j = 0; j < num_tupples; j++) {
192                         int range_val;
193                         hda_nid_t val1, val2, n;
194                         range_val = parm & (1 << (shift-1)); /* ranges */
195                         val1 = parm & mask;
196                         parm >>= shift;
197                         val2 = parm & mask;
198                         parm >>= shift;
199                         if (range_val) {
200                                 /* ranges between val1 and val2 */
201                                 if (val1 > val2) {
202                                         snd_printk(KERN_WARNING "hda_codec: invalid dep_range_val %x:%x\n", val1, val2);
203                                         continue;
204                                 }
205                                 for (n = val1; n <= val2; n++) {
206                                         if (conns >= max_conns)
207                                                 return -EINVAL;
208                                         conn_list[conns++] = n;
209                                 }
210                         } else {
211                                 if (! val1)
212                                         break;
213                                 if (conns >= max_conns)
214                                         return -EINVAL;
215                                 conn_list[conns++] = val1;
216                                 if (! val2)
217                                         break;
218                                 if (conns >= max_conns)
219                                         return -EINVAL;
220                                 conn_list[conns++] = val2;
221                         }
222                 }
223         }
224         return conns;
225 }
226
227
228 /**
229  * snd_hda_queue_unsol_event - add an unsolicited event to queue
230  * @bus: the BUS
231  * @res: unsolicited event (lower 32bit of RIRB entry)
232  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
233  *
234  * Adds the given event to the queue.  The events are processed in
235  * the workqueue asynchronously.  Call this function in the interrupt
236  * hanlder when RIRB receives an unsolicited event.
237  *
238  * Returns 0 if successful, or a negative error code.
239  */
240 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
241 {
242         struct hda_bus_unsolicited *unsol;
243         unsigned int wp;
244
245         if ((unsol = bus->unsol) == NULL)
246                 return 0;
247
248         wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
249         unsol->wp = wp;
250
251         wp <<= 1;
252         unsol->queue[wp] = res;
253         unsol->queue[wp + 1] = res_ex;
254
255         queue_work(unsol->workq, &unsol->work);
256
257         return 0;
258 }
259
260 /*
261  * process queueud unsolicited events
262  */
263 static void process_unsol_events(void *data)
264 {
265         struct hda_bus *bus = data;
266         struct hda_bus_unsolicited *unsol = bus->unsol;
267         struct hda_codec *codec;
268         unsigned int rp, caddr, res;
269
270         while (unsol->rp != unsol->wp) {
271                 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
272                 unsol->rp = rp;
273                 rp <<= 1;
274                 res = unsol->queue[rp];
275                 caddr = unsol->queue[rp + 1];
276                 if (! (caddr & (1 << 4))) /* no unsolicited event? */
277                         continue;
278                 codec = bus->caddr_tbl[caddr & 0x0f];
279                 if (codec && codec->patch_ops.unsol_event)
280                         codec->patch_ops.unsol_event(codec, res);
281         }
282 }
283
284 /*
285  * initialize unsolicited queue
286  */
287 static int init_unsol_queue(struct hda_bus *bus)
288 {
289         struct hda_bus_unsolicited *unsol;
290
291         if (bus->unsol) /* already initialized */
292                 return 0;
293
294         unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
295         if (! unsol) {
296                 snd_printk(KERN_ERR "hda_codec: can't allocate unsolicited queue\n");
297                 return -ENOMEM;
298         }
299         unsol->workq = create_workqueue("hda_codec");
300         if (! unsol->workq) {
301                 snd_printk(KERN_ERR "hda_codec: can't create workqueue\n");
302                 kfree(unsol);
303                 return -ENOMEM;
304         }
305         INIT_WORK(&unsol->work, process_unsol_events, bus);
306         bus->unsol = unsol;
307         return 0;
308 }
309
310 /*
311  * destructor
312  */
313 static void snd_hda_codec_free(struct hda_codec *codec);
314
315 static int snd_hda_bus_free(struct hda_bus *bus)
316 {
317         struct list_head *p, *n;
318
319         if (! bus)
320                 return 0;
321         if (bus->unsol) {
322                 destroy_workqueue(bus->unsol->workq);
323                 kfree(bus->unsol);
324         }
325         list_for_each_safe(p, n, &bus->codec_list) {
326                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
327                 snd_hda_codec_free(codec);
328         }
329         if (bus->ops.private_free)
330                 bus->ops.private_free(bus);
331         kfree(bus);
332         return 0;
333 }
334
335 static int snd_hda_bus_dev_free(snd_device_t *device)
336 {
337         struct hda_bus *bus = device->device_data;
338         return snd_hda_bus_free(bus);
339 }
340
341 /**
342  * snd_hda_bus_new - create a HDA bus
343  * @card: the card entry
344  * @temp: the template for hda_bus information
345  * @busp: the pointer to store the created bus instance
346  *
347  * Returns 0 if successful, or a negative error code.
348  */
349 int snd_hda_bus_new(snd_card_t *card, const struct hda_bus_template *temp,
350                     struct hda_bus **busp)
351 {
352         struct hda_bus *bus;
353         int err;
354         static snd_device_ops_t dev_ops = {
355                 .dev_free = snd_hda_bus_dev_free,
356         };
357
358         snd_assert(temp, return -EINVAL);
359         snd_assert(temp->ops.command && temp->ops.get_response, return -EINVAL);
360
361         if (busp)
362                 *busp = NULL;
363
364         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
365         if (bus == NULL) {
366                 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
367                 return -ENOMEM;
368         }
369
370         bus->card = card;
371         bus->private_data = temp->private_data;
372         bus->pci = temp->pci;
373         bus->modelname = temp->modelname;
374         bus->ops = temp->ops;
375
376         init_MUTEX(&bus->cmd_mutex);
377         INIT_LIST_HEAD(&bus->codec_list);
378
379         if ((err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops)) < 0) {
380                 snd_hda_bus_free(bus);
381                 return err;
382         }
383         if (busp)
384                 *busp = bus;
385         return 0;
386 }
387
388
389 /*
390  * find a matching codec preset
391  */
392 static const struct hda_codec_preset *find_codec_preset(struct hda_codec *codec)
393 {
394         const struct hda_codec_preset **tbl, *preset;
395
396         for (tbl = hda_preset_tables; *tbl; tbl++) {
397                 for (preset = *tbl; preset->id; preset++) {
398                         u32 mask = preset->mask;
399                         if (! mask)
400                                 mask = ~0;
401                         if (preset->id == (codec->vendor_id & mask))
402                                 return preset;
403                 }
404         }
405         return NULL;
406 }
407
408 /*
409  * snd_hda_get_codec_name - store the codec name
410  */
411 void snd_hda_get_codec_name(struct hda_codec *codec,
412                             char *name, int namelen)
413 {
414         const struct hda_vendor_id *c;
415         const char *vendor = NULL;
416         u16 vendor_id = codec->vendor_id >> 16;
417         char tmp[16];
418
419         for (c = hda_vendor_ids; c->id; c++) {
420                 if (c->id == vendor_id) {
421                         vendor = c->name;
422                         break;
423                 }
424         }
425         if (! vendor) {
426                 sprintf(tmp, "Generic %04x", vendor_id);
427                 vendor = tmp;
428         }
429         if (codec->preset && codec->preset->name)
430                 snprintf(name, namelen, "%s %s", vendor, codec->preset->name);
431         else
432                 snprintf(name, namelen, "%s ID %x", vendor, codec->vendor_id & 0xffff);
433 }
434
435 /*
436  * look for an AFG and MFG nodes
437  */
438 static void setup_fg_nodes(struct hda_codec *codec)
439 {
440         int i, total_nodes;
441         hda_nid_t nid;
442
443         total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
444         for (i = 0; i < total_nodes; i++, nid++) {
445                 switch((snd_hda_param_read(codec, nid, AC_PAR_FUNCTION_TYPE) & 0xff)) {
446                 case AC_GRP_AUDIO_FUNCTION:
447                         codec->afg = nid;
448                         break;
449                 case AC_GRP_MODEM_FUNCTION:
450                         codec->mfg = nid;
451                         break;
452                 default:
453                         break;
454                 }
455         }
456 }
457
458 /*
459  * codec destructor
460  */
461 static void snd_hda_codec_free(struct hda_codec *codec)
462 {
463         if (! codec)
464                 return;
465         list_del(&codec->list);
466         codec->bus->caddr_tbl[codec->addr] = NULL;
467         if (codec->patch_ops.free)
468                 codec->patch_ops.free(codec);
469         kfree(codec->amp_info);
470         kfree(codec);
471 }
472
473 static void init_amp_hash(struct hda_codec *codec);
474
475 /**
476  * snd_hda_codec_new - create a HDA codec
477  * @bus: the bus to assign
478  * @codec_addr: the codec address
479  * @codecp: the pointer to store the generated codec
480  *
481  * Returns 0 if successful, or a negative error code.
482  */
483 int snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
484                       struct hda_codec **codecp)
485 {
486         struct hda_codec *codec;
487         char component[13];
488         int err;
489
490         snd_assert(bus, return -EINVAL);
491         snd_assert(codec_addr <= HDA_MAX_CODEC_ADDRESS, return -EINVAL);
492
493         if (bus->caddr_tbl[codec_addr]) {
494                 snd_printk(KERN_ERR "hda_codec: address 0x%x is already occupied\n", codec_addr);
495                 return -EBUSY;
496         }
497
498         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
499         if (codec == NULL) {
500                 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
501                 return -ENOMEM;
502         }
503
504         codec->bus = bus;
505         codec->addr = codec_addr;
506         init_MUTEX(&codec->spdif_mutex);
507         init_amp_hash(codec);
508
509         list_add_tail(&codec->list, &bus->codec_list);
510         bus->caddr_tbl[codec_addr] = codec;
511
512         codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_VENDOR_ID);
513         codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_SUBSYSTEM_ID);
514         codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT, AC_PAR_REV_ID);
515
516         setup_fg_nodes(codec);
517         if (! codec->afg && ! codec->mfg) {
518                 snd_printdd("hda_codec: no AFG or MFG node found\n");
519                 snd_hda_codec_free(codec);
520                 return -ENODEV;
521         }
522
523         if (! codec->subsystem_id) {
524                 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
525                 codec->subsystem_id = snd_hda_codec_read(codec, nid, 0,
526                                                          AC_VERB_GET_SUBSYSTEM_ID,
527                                                          0);
528         }
529
530         codec->preset = find_codec_preset(codec);
531         if (! *bus->card->mixername)
532                 snd_hda_get_codec_name(codec, bus->card->mixername,
533                                        sizeof(bus->card->mixername));
534
535         if (codec->preset && codec->preset->patch)
536                 err = codec->preset->patch(codec);
537         else
538                 err = snd_hda_parse_generic_codec(codec);
539         if (err < 0) {
540                 snd_hda_codec_free(codec);
541                 return err;
542         }
543
544         if (codec->patch_ops.unsol_event)
545                 init_unsol_queue(bus);
546
547         snd_hda_codec_proc_new(codec);
548
549         sprintf(component, "HDA:%08x", codec->vendor_id);
550         snd_component_add(codec->bus->card, component);
551
552         if (codecp)
553                 *codecp = codec;
554         return 0;
555 }
556
557 /**
558  * snd_hda_codec_setup_stream - set up the codec for streaming
559  * @codec: the CODEC to set up
560  * @nid: the NID to set up
561  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
562  * @channel_id: channel id to pass, zero based.
563  * @format: stream format.
564  */
565 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid, u32 stream_tag,
566                                 int channel_id, int format)
567 {
568         if (! nid)
569                 return;
570
571         snd_printdd("hda_codec_setup_stream: NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
572                     nid, stream_tag, channel_id, format);
573         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
574                             (stream_tag << 4) | channel_id);
575         msleep(1);
576         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
577 }
578
579
580 /*
581  * amp access functions
582  */
583
584 /* FIXME: more better hash key? */
585 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
586 #define INFO_AMP_CAPS   (1<<0)
587 #define INFO_AMP_VOL(ch)        (1 << (1 + (ch)))
588
589 /* initialize the hash table */
590 static void init_amp_hash(struct hda_codec *codec)
591 {
592         memset(codec->amp_hash, 0xff, sizeof(codec->amp_hash));
593         codec->num_amp_entries = 0;
594         codec->amp_info_size = 0;
595         codec->amp_info = NULL;
596 }
597
598 /* query the hash.  allocate an entry if not found. */
599 static struct hda_amp_info *get_alloc_amp_hash(struct hda_codec *codec, u32 key)
600 {
601         u16 idx = key % (u16)ARRAY_SIZE(codec->amp_hash);
602         u16 cur = codec->amp_hash[idx];
603         struct hda_amp_info *info;
604
605         while (cur != 0xffff) {
606                 info = &codec->amp_info[cur];
607                 if (info->key == key)
608                         return info;
609                 cur = info->next;
610         }
611
612         /* add a new hash entry */
613         if (codec->num_amp_entries >= codec->amp_info_size) {
614                 /* reallocate the array */
615                 int new_size = codec->amp_info_size + 64;
616                 struct hda_amp_info *new_info = kcalloc(new_size, sizeof(struct hda_amp_info),
617                                                         GFP_KERNEL);
618                 if (! new_info) {
619                         snd_printk(KERN_ERR "hda_codec: can't malloc amp_info\n");
620                         return NULL;
621                 }
622                 if (codec->amp_info) {
623                         memcpy(new_info, codec->amp_info,
624                                codec->amp_info_size * sizeof(struct hda_amp_info));
625                         kfree(codec->amp_info);
626                 }
627                 codec->amp_info_size = new_size;
628                 codec->amp_info = new_info;
629         }
630         cur = codec->num_amp_entries++;
631         info = &codec->amp_info[cur];
632         info->key = key;
633         info->status = 0; /* not initialized yet */
634         info->next = codec->amp_hash[idx];
635         codec->amp_hash[idx] = cur;
636
637         return info;
638 }
639
640 /*
641  * query AMP capabilities for the given widget and direction
642  */
643 static u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
644 {
645         struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
646
647         if (! info)
648                 return 0;
649         if (! (info->status & INFO_AMP_CAPS)) {
650                 if (!(snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_AMP_OVRD))
651                         nid = codec->afg;
652                 info->amp_caps = snd_hda_param_read(codec, nid, direction == HDA_OUTPUT ?
653                                                     AC_PAR_AMP_OUT_CAP : AC_PAR_AMP_IN_CAP);
654                 info->status |= INFO_AMP_CAPS;
655         }
656         return info->amp_caps;
657 }
658
659 /*
660  * read the current volume to info
661  * if the cache exists, read the cache value.
662  */
663 static unsigned int get_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
664                          hda_nid_t nid, int ch, int direction, int index)
665 {
666         u32 val, parm;
667
668         if (info->status & INFO_AMP_VOL(ch))
669                 return info->vol[ch];
670
671         parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
672         parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
673         parm |= index;
674         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_AMP_GAIN_MUTE, parm);
675         info->vol[ch] = val & 0xff;
676         info->status |= INFO_AMP_VOL(ch);
677         return info->vol[ch];
678 }
679
680 /*
681  * write the current volume in info to the h/w and update the cache
682  */
683 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
684                          hda_nid_t nid, int ch, int direction, int index, int val)
685 {
686         u32 parm;
687
688         parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
689         parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
690         parm |= index << AC_AMP_SET_INDEX_SHIFT;
691         parm |= val;
692         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
693         info->vol[ch] = val;
694 }
695
696 /*
697  * read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
698  */
699 static int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int index)
700 {
701         struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
702         if (! info)
703                 return 0;
704         return get_vol_mute(codec, info, nid, ch, direction, index);
705 }
706
707 /*
708  * update the AMP value, mask = bit mask to set, val = the value
709  */
710 static int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch, int direction, int idx, int mask, int val)
711 {
712         struct hda_amp_info *info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
713
714         if (! info)
715                 return 0;
716         val &= mask;
717         val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
718         if (info->vol[ch] == val && ! codec->in_resume)
719                 return 0;
720         put_vol_mute(codec, info, nid, ch, direction, idx, val);
721         return 1;
722 }
723
724
725 /*
726  * AMP control callbacks
727  */
728 /* retrieve parameters from private_value */
729 #define get_amp_nid(kc)         ((kc)->private_value & 0xffff)
730 #define get_amp_channels(kc)    (((kc)->private_value >> 16) & 0x3)
731 #define get_amp_direction(kc)   (((kc)->private_value >> 18) & 0x1)
732 #define get_amp_index(kc)       (((kc)->private_value >> 19) & 0xf)
733
734 /* volume */
735 int snd_hda_mixer_amp_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
736 {
737         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
738         u16 nid = get_amp_nid(kcontrol);
739         u8 chs = get_amp_channels(kcontrol);
740         int dir = get_amp_direction(kcontrol);
741         u32 caps;
742
743         caps = query_amp_caps(codec, nid, dir);
744         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT; /* num steps */
745         if (! caps) {
746                 printk(KERN_WARNING "hda_codec: num_steps = 0 for NID=0x%x\n", nid);
747                 return -EINVAL;
748         }
749         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
750         uinfo->count = chs == 3 ? 2 : 1;
751         uinfo->value.integer.min = 0;
752         uinfo->value.integer.max = caps;
753         return 0;
754 }
755
756 int snd_hda_mixer_amp_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
757 {
758         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
759         hda_nid_t nid = get_amp_nid(kcontrol);
760         int chs = get_amp_channels(kcontrol);
761         int dir = get_amp_direction(kcontrol);
762         int idx = get_amp_index(kcontrol);
763         long *valp = ucontrol->value.integer.value;
764
765         if (chs & 1)
766                 *valp++ = snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x7f;
767         if (chs & 2)
768                 *valp = snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x7f;
769         return 0;
770 }
771
772 int snd_hda_mixer_amp_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
773 {
774         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
775         hda_nid_t nid = get_amp_nid(kcontrol);
776         int chs = get_amp_channels(kcontrol);
777         int dir = get_amp_direction(kcontrol);
778         int idx = get_amp_index(kcontrol);
779         long *valp = ucontrol->value.integer.value;
780         int change = 0;
781
782         if (chs & 1) {
783                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
784                                                   0x7f, *valp);
785                 valp++;
786         }
787         if (chs & 2)
788                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
789                                                    0x7f, *valp);
790         return change;
791 }
792
793 /* switch */
794 int snd_hda_mixer_amp_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
795 {
796         int chs = get_amp_channels(kcontrol);
797
798         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
799         uinfo->count = chs == 3 ? 2 : 1;
800         uinfo->value.integer.min = 0;
801         uinfo->value.integer.max = 1;
802         return 0;
803 }
804
805 int snd_hda_mixer_amp_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
806 {
807         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
808         hda_nid_t nid = get_amp_nid(kcontrol);
809         int chs = get_amp_channels(kcontrol);
810         int dir = get_amp_direction(kcontrol);
811         int idx = get_amp_index(kcontrol);
812         long *valp = ucontrol->value.integer.value;
813
814         if (chs & 1)
815                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) & 0x80) ? 0 : 1;
816         if (chs & 2)
817                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) & 0x80) ? 0 : 1;
818         return 0;
819 }
820
821 int snd_hda_mixer_amp_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
822 {
823         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
824         hda_nid_t nid = get_amp_nid(kcontrol);
825         int chs = get_amp_channels(kcontrol);
826         int dir = get_amp_direction(kcontrol);
827         int idx = get_amp_index(kcontrol);
828         long *valp = ucontrol->value.integer.value;
829         int change = 0;
830
831         if (chs & 1) {
832                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
833                                                   0x80, *valp ? 0 : 0x80);
834                 valp++;
835         }
836         if (chs & 2)
837                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
838                                                    0x80, *valp ? 0 : 0x80);
839         
840         return change;
841 }
842
843 /*
844  * bound volume controls
845  *
846  * bind multiple volumes (# indices, from 0)
847  */
848
849 #define AMP_VAL_IDX_SHIFT       19
850 #define AMP_VAL_IDX_MASK        (0x0f<<19)
851
852 int snd_hda_mixer_bind_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
853 {
854         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
855         unsigned long pval;
856         int err;
857
858         down(&codec->spdif_mutex); /* reuse spdif_mutex */
859         pval = kcontrol->private_value;
860         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
861         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
862         kcontrol->private_value = pval;
863         up(&codec->spdif_mutex);
864         return err;
865 }
866
867 int snd_hda_mixer_bind_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
868 {
869         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
870         unsigned long pval;
871         int i, indices, err = 0, change = 0;
872
873         down(&codec->spdif_mutex); /* reuse spdif_mutex */
874         pval = kcontrol->private_value;
875         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
876         for (i = 0; i < indices; i++) {
877                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) | (i << AMP_VAL_IDX_SHIFT);
878                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
879                 if (err < 0)
880                         break;
881                 change |= err;
882         }
883         kcontrol->private_value = pval;
884         up(&codec->spdif_mutex);
885         return err < 0 ? err : change;
886 }
887
888 /*
889  * SPDIF out controls
890  */
891
892 static int snd_hda_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
893 {
894         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
895         uinfo->count = 1;
896         return 0;
897 }
898
899 static int snd_hda_spdif_cmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
900 {
901         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
902                                            IEC958_AES0_NONAUDIO |
903                                            IEC958_AES0_CON_EMPHASIS_5015 |
904                                            IEC958_AES0_CON_NOT_COPYRIGHT;
905         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
906                                            IEC958_AES1_CON_ORIGINAL;
907         return 0;
908 }
909
910 static int snd_hda_spdif_pmask_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
911 {
912         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
913                                            IEC958_AES0_NONAUDIO |
914                                            IEC958_AES0_PRO_EMPHASIS_5015;
915         return 0;
916 }
917
918 static int snd_hda_spdif_default_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
919 {
920         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
921
922         ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
923         ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
924         ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
925         ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
926
927         return 0;
928 }
929
930 /* convert from SPDIF status bits to HDA SPDIF bits
931  * bit 0 (DigEn) is always set zero (to be filled later)
932  */
933 static unsigned short convert_from_spdif_status(unsigned int sbits)
934 {
935         unsigned short val = 0;
936
937         if (sbits & IEC958_AES0_PROFESSIONAL)
938                 val |= 1 << 6;
939         if (sbits & IEC958_AES0_NONAUDIO)
940                 val |= 1 << 5;
941         if (sbits & IEC958_AES0_PROFESSIONAL) {
942                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) == IEC958_AES0_PRO_EMPHASIS_5015)
943                         val |= 1 << 3;
944         } else {
945                 if ((sbits & IEC958_AES0_CON_EMPHASIS) == IEC958_AES0_CON_EMPHASIS_5015)
946                         val |= 1 << 3;
947                 if (! (sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
948                         val |= 1 << 4;
949                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
950                         val |= 1 << 7;
951                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
952         }
953         return val;
954 }
955
956 /* convert to SPDIF status bits from HDA SPDIF bits
957  */
958 static unsigned int convert_to_spdif_status(unsigned short val)
959 {
960         unsigned int sbits = 0;
961
962         if (val & (1 << 5))
963                 sbits |= IEC958_AES0_NONAUDIO;
964         if (val & (1 << 6))
965                 sbits |= IEC958_AES0_PROFESSIONAL;
966         if (sbits & IEC958_AES0_PROFESSIONAL) {
967                 if (sbits & (1 << 3))
968                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
969         } else {
970                 if (val & (1 << 3))
971                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
972                 if (! (val & (1 << 4)))
973                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
974                 if (val & (1 << 7))
975                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
976                 sbits |= val & (0x7f << 8);
977         }
978         return sbits;
979 }
980
981 static int snd_hda_spdif_default_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
982 {
983         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
984         hda_nid_t nid = kcontrol->private_value;
985         unsigned short val;
986         int change;
987
988         down(&codec->spdif_mutex);
989         codec->spdif_status = ucontrol->value.iec958.status[0] |
990                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
991                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
992                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
993         val = convert_from_spdif_status(codec->spdif_status);
994         val |= codec->spdif_ctls & 1;
995         change = codec->spdif_ctls != val;
996         codec->spdif_ctls = val;
997
998         if (change || codec->in_resume) {
999                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1000                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_2, val >> 8);
1001         }
1002
1003         up(&codec->spdif_mutex);
1004         return change;
1005 }
1006
1007 static int snd_hda_spdif_out_switch_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
1008 {
1009         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1010         uinfo->count = 1;
1011         uinfo->value.integer.min = 0;
1012         uinfo->value.integer.max = 1;
1013         return 0;
1014 }
1015
1016 static int snd_hda_spdif_out_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1017 {
1018         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1019
1020         ucontrol->value.integer.value[0] = codec->spdif_ctls & 1;
1021         return 0;
1022 }
1023
1024 static int snd_hda_spdif_out_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1025 {
1026         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1027         hda_nid_t nid = kcontrol->private_value;
1028         unsigned short val;
1029         int change;
1030
1031         down(&codec->spdif_mutex);
1032         val = codec->spdif_ctls & ~1;
1033         if (ucontrol->value.integer.value[0])
1034                 val |= 1;
1035         change = codec->spdif_ctls != val;
1036         if (change || codec->in_resume) {
1037                 codec->spdif_ctls = val;
1038                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val & 0xff);
1039                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
1040                                     AC_AMP_SET_RIGHT | AC_AMP_SET_LEFT |
1041                                     AC_AMP_SET_OUTPUT | ((val & 1) ? 0 : 0x80));
1042         }
1043         up(&codec->spdif_mutex);
1044         return change;
1045 }
1046
1047 static snd_kcontrol_new_t dig_mixes[] = {
1048         {
1049                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1050                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1051                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
1052                 .info = snd_hda_spdif_mask_info,
1053                 .get = snd_hda_spdif_cmask_get,
1054         },
1055         {
1056                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1057                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1058                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
1059                 .info = snd_hda_spdif_mask_info,
1060                 .get = snd_hda_spdif_pmask_get,
1061         },
1062         {
1063                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1064                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
1065                 .info = snd_hda_spdif_mask_info,
1066                 .get = snd_hda_spdif_default_get,
1067                 .put = snd_hda_spdif_default_put,
1068         },
1069         {
1070                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1071                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
1072                 .info = snd_hda_spdif_out_switch_info,
1073                 .get = snd_hda_spdif_out_switch_get,
1074                 .put = snd_hda_spdif_out_switch_put,
1075         },
1076         { } /* end */
1077 };
1078
1079 /**
1080  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
1081  * @codec: the HDA codec
1082  * @nid: audio out widget NID
1083  *
1084  * Creates controls related with the SPDIF output.
1085  * Called from each patch supporting the SPDIF out.
1086  *
1087  * Returns 0 if successful, or a negative error code.
1088  */
1089 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
1090 {
1091         int err;
1092         snd_kcontrol_t *kctl;
1093         snd_kcontrol_new_t *dig_mix;
1094
1095         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
1096                 kctl = snd_ctl_new1(dig_mix, codec);
1097                 kctl->private_value = nid;
1098                 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1099                         return err;
1100         }
1101         codec->spdif_ctls = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1102         codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
1103         return 0;
1104 }
1105
1106 /*
1107  * SPDIF input
1108  */
1109
1110 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
1111
1112 static int snd_hda_spdif_in_switch_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1113 {
1114         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1115
1116         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
1117         return 0;
1118 }
1119
1120 static int snd_hda_spdif_in_switch_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1121 {
1122         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1123         hda_nid_t nid = kcontrol->private_value;
1124         unsigned int val = !!ucontrol->value.integer.value[0];
1125         int change;
1126
1127         down(&codec->spdif_mutex);
1128         change = codec->spdif_in_enable != val;
1129         if (change || codec->in_resume) {
1130                 codec->spdif_in_enable = val;
1131                 snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_DIGI_CONVERT_1, val);
1132         }
1133         up(&codec->spdif_mutex);
1134         return change;
1135 }
1136
1137 static int snd_hda_spdif_in_status_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
1138 {
1139         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1140         hda_nid_t nid = kcontrol->private_value;
1141         unsigned short val;
1142         unsigned int sbits;
1143
1144         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0);
1145         sbits = convert_to_spdif_status(val);
1146         ucontrol->value.iec958.status[0] = sbits;
1147         ucontrol->value.iec958.status[1] = sbits >> 8;
1148         ucontrol->value.iec958.status[2] = sbits >> 16;
1149         ucontrol->value.iec958.status[3] = sbits >> 24;
1150         return 0;
1151 }
1152
1153 static snd_kcontrol_new_t dig_in_ctls[] = {
1154         {
1155                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1156                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
1157                 .info = snd_hda_spdif_in_switch_info,
1158                 .get = snd_hda_spdif_in_switch_get,
1159                 .put = snd_hda_spdif_in_switch_put,
1160         },
1161         {
1162                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1163                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1164                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
1165                 .info = snd_hda_spdif_mask_info,
1166                 .get = snd_hda_spdif_in_status_get,
1167         },
1168         { } /* end */
1169 };
1170
1171 /**
1172  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
1173  * @codec: the HDA codec
1174  * @nid: audio in widget NID
1175  *
1176  * Creates controls related with the SPDIF input.
1177  * Called from each patch supporting the SPDIF in.
1178  *
1179  * Returns 0 if successful, or a negative error code.
1180  */
1181 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
1182 {
1183         int err;
1184         snd_kcontrol_t *kctl;
1185         snd_kcontrol_new_t *dig_mix;
1186
1187         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
1188                 kctl = snd_ctl_new1(dig_mix, codec);
1189                 kctl->private_value = nid;
1190                 if ((err = snd_ctl_add(codec->bus->card, kctl)) < 0)
1191                         return err;
1192         }
1193         codec->spdif_in_enable = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT, 0) & 1;
1194         return 0;
1195 }
1196
1197
1198 /**
1199  * snd_hda_build_controls - build mixer controls
1200  * @bus: the BUS
1201  *
1202  * Creates mixer controls for each codec included in the bus.
1203  *
1204  * Returns 0 if successful, otherwise a negative error code.
1205  */
1206 int snd_hda_build_controls(struct hda_bus *bus)
1207 {
1208         struct list_head *p;
1209
1210         /* build controls */
1211         list_for_each(p, &bus->codec_list) {
1212                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1213                 int err;
1214                 if (! codec->patch_ops.build_controls)
1215                         continue;
1216                 err = codec->patch_ops.build_controls(codec);
1217                 if (err < 0)
1218                         return err;
1219         }
1220
1221         /* initialize */
1222         list_for_each(p, &bus->codec_list) {
1223                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1224                 int err;
1225                 if (! codec->patch_ops.init)
1226                         continue;
1227                 err = codec->patch_ops.init(codec);
1228                 if (err < 0)
1229                         return err;
1230         }
1231         return 0;
1232 }
1233
1234
1235 /*
1236  * stream formats
1237  */
1238 struct hda_rate_tbl {
1239         unsigned int hz;
1240         unsigned int alsa_bits;
1241         unsigned int hda_fmt;
1242 };
1243
1244 static struct hda_rate_tbl rate_bits[] = {
1245         /* rate in Hz, ALSA rate bitmask, HDA format value */
1246
1247         /* autodetected value used in snd_hda_query_supported_pcm */
1248         { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
1249         { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
1250         { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
1251         { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
1252         { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
1253         { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
1254         { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
1255         { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
1256         { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
1257         { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
1258         { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
1259
1260         /* not autodetected value */
1261         { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
1262
1263         { 0 } /* terminator */
1264 };
1265
1266 /**
1267  * snd_hda_calc_stream_format - calculate format bitset
1268  * @rate: the sample rate
1269  * @channels: the number of channels
1270  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
1271  * @maxbps: the max. bps
1272  *
1273  * Calculate the format bitset from the given rate, channels and th PCM format.
1274  *
1275  * Return zero if invalid.
1276  */
1277 unsigned int snd_hda_calc_stream_format(unsigned int rate,
1278                                         unsigned int channels,
1279                                         unsigned int format,
1280                                         unsigned int maxbps)
1281 {
1282         int i;
1283         unsigned int val = 0;
1284
1285         for (i = 0; rate_bits[i].hz; i++)
1286                 if (rate_bits[i].hz == rate) {
1287                         val = rate_bits[i].hda_fmt;
1288                         break;
1289                 }
1290         if (! rate_bits[i].hz) {
1291                 snd_printdd("invalid rate %d\n", rate);
1292                 return 0;
1293         }
1294
1295         if (channels == 0 || channels > 8) {
1296                 snd_printdd("invalid channels %d\n", channels);
1297                 return 0;
1298         }
1299         val |= channels - 1;
1300
1301         switch (snd_pcm_format_width(format)) {
1302         case 8:  val |= 0x00; break;
1303         case 16: val |= 0x10; break;
1304         case 20:
1305         case 24:
1306         case 32:
1307                 if (maxbps >= 32)
1308                         val |= 0x40;
1309                 else if (maxbps >= 24)
1310                         val |= 0x30;
1311                 else
1312                         val |= 0x20;
1313                 break;
1314         default:
1315                 snd_printdd("invalid format width %d\n", snd_pcm_format_width(format));
1316                 return 0;
1317         }
1318
1319         return val;
1320 }
1321
1322 /**
1323  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
1324  * @codec: the HDA codec
1325  * @nid: NID to query
1326  * @ratesp: the pointer to store the detected rate bitflags
1327  * @formatsp: the pointer to store the detected formats
1328  * @bpsp: the pointer to store the detected format widths
1329  *
1330  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
1331  * or @bsps argument is ignored.
1332  *
1333  * Returns 0 if successful, otherwise a negative error code.
1334  */
1335 int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
1336                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
1337 {
1338         int i;
1339         unsigned int val, streams;
1340
1341         val = 0;
1342         if (nid != codec->afg &&
1343             snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1344                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1345                 if (val == -1)
1346                         return -EIO;
1347         }
1348         if (! val)
1349                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1350
1351         if (ratesp) {
1352                 u32 rates = 0;
1353                 for (i = 0; rate_bits[i].hz; i++) {
1354                         if (val & (1 << i))
1355                                 rates |= rate_bits[i].alsa_bits;
1356                 }
1357                 *ratesp = rates;
1358         }
1359
1360         if (formatsp || bpsp) {
1361                 u64 formats = 0;
1362                 unsigned int bps;
1363                 unsigned int wcaps;
1364
1365                 wcaps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
1366                 streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1367                 if (streams == -1)
1368                         return -EIO;
1369                 if (! streams) {
1370                         streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1371                         if (streams == -1)
1372                                 return -EIO;
1373                 }
1374
1375                 bps = 0;
1376                 if (streams & AC_SUPFMT_PCM) {
1377                         if (val & AC_SUPPCM_BITS_8) {
1378                                 formats |= SNDRV_PCM_FMTBIT_U8;
1379                                 bps = 8;
1380                         }
1381                         if (val & AC_SUPPCM_BITS_16) {
1382                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
1383                                 bps = 16;
1384                         }
1385                         if (wcaps & AC_WCAP_DIGITAL) {
1386                                 if (val & AC_SUPPCM_BITS_32)
1387                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
1388                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
1389                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
1390                                 if (val & AC_SUPPCM_BITS_24)
1391                                         bps = 24;
1392                                 else if (val & AC_SUPPCM_BITS_20)
1393                                         bps = 20;
1394                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|AC_SUPPCM_BITS_32)) {
1395                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
1396                                 if (val & AC_SUPPCM_BITS_32)
1397                                         bps = 32;
1398                                 else if (val & AC_SUPPCM_BITS_20)
1399                                         bps = 20;
1400                                 else if (val & AC_SUPPCM_BITS_24)
1401                                         bps = 24;
1402                         }
1403                 }
1404                 else if (streams == AC_SUPFMT_FLOAT32) { /* should be exclusive */
1405                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
1406                         bps = 32;
1407                 } else if (streams == AC_SUPFMT_AC3) { /* should be exclusive */
1408                         /* temporary hack: we have still no proper support
1409                          * for the direct AC3 stream...
1410                          */
1411                         formats |= SNDRV_PCM_FMTBIT_U8;
1412                         bps = 8;
1413                 }
1414                 if (formatsp)
1415                         *formatsp = formats;
1416                 if (bpsp)
1417                         *bpsp = bps;
1418         }
1419
1420         return 0;
1421 }
1422
1423 /**
1424  * snd_hda_is_supported_format - check whether the given node supports the format val
1425  *
1426  * Returns 1 if supported, 0 if not.
1427  */
1428 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
1429                                 unsigned int format)
1430 {
1431         int i;
1432         unsigned int val = 0, rate, stream;
1433
1434         if (nid != codec->afg &&
1435             snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP) & AC_WCAP_FORMAT_OVRD) {
1436                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
1437                 if (val == -1)
1438                         return 0;
1439         }
1440         if (! val) {
1441                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
1442                 if (val == -1)
1443                         return 0;
1444         }
1445
1446         rate = format & 0xff00;
1447         for (i = 0; rate_bits[i].hz; i++)
1448                 if (rate_bits[i].hda_fmt == rate) {
1449                         if (val & (1 << i))
1450                                 break;
1451                         return 0;
1452                 }
1453         if (! rate_bits[i].hz)
1454                 return 0;
1455
1456         stream = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
1457         if (stream == -1)
1458                 return 0;
1459         if (! stream && nid != codec->afg)
1460                 stream = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
1461         if (! stream || stream == -1)
1462                 return 0;
1463
1464         if (stream & AC_SUPFMT_PCM) {
1465                 switch (format & 0xf0) {
1466                 case 0x00:
1467                         if (! (val & AC_SUPPCM_BITS_8))
1468                                 return 0;
1469                         break;
1470                 case 0x10:
1471                         if (! (val & AC_SUPPCM_BITS_16))
1472                                 return 0;
1473                         break;
1474                 case 0x20:
1475                         if (! (val & AC_SUPPCM_BITS_20))
1476                                 return 0;
1477                         break;
1478                 case 0x30:
1479                         if (! (val & AC_SUPPCM_BITS_24))
1480                                 return 0;
1481                         break;
1482                 case 0x40:
1483                         if (! (val & AC_SUPPCM_BITS_32))
1484                                 return 0;
1485                         break;
1486                 default:
1487                         return 0;
1488                 }
1489         } else {
1490                 /* FIXME: check for float32 and AC3? */
1491         }
1492
1493         return 1;
1494 }
1495
1496 /*
1497  * PCM stuff
1498  */
1499 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
1500                                       struct hda_codec *codec,
1501                                       snd_pcm_substream_t *substream)
1502 {
1503         return 0;
1504 }
1505
1506 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
1507                                    struct hda_codec *codec,
1508                                    unsigned int stream_tag,
1509                                    unsigned int format,
1510                                    snd_pcm_substream_t *substream)
1511 {
1512         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
1513         return 0;
1514 }
1515
1516 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
1517                                    struct hda_codec *codec,
1518                                    snd_pcm_substream_t *substream)
1519 {
1520         snd_hda_codec_setup_stream(codec, hinfo->nid, 0, 0, 0);
1521         return 0;
1522 }
1523
1524 static int set_pcm_default_values(struct hda_codec *codec, struct hda_pcm_stream *info)
1525 {
1526         if (info->nid) {
1527                 /* query support PCM information from the given NID */
1528                 if (! info->rates || ! info->formats)
1529                         snd_hda_query_supported_pcm(codec, info->nid,
1530                                                     info->rates ? NULL : &info->rates,
1531                                                     info->formats ? NULL : &info->formats,
1532                                                     info->maxbps ? NULL : &info->maxbps);
1533         }
1534         if (info->ops.open == NULL)
1535                 info->ops.open = hda_pcm_default_open_close;
1536         if (info->ops.close == NULL)
1537                 info->ops.close = hda_pcm_default_open_close;
1538         if (info->ops.prepare == NULL) {
1539                 snd_assert(info->nid, return -EINVAL);
1540                 info->ops.prepare = hda_pcm_default_prepare;
1541         }
1542         if (info->ops.cleanup == NULL) {
1543                 snd_assert(info->nid, return -EINVAL);
1544                 info->ops.cleanup = hda_pcm_default_cleanup;
1545         }
1546         return 0;
1547 }
1548
1549 /**
1550  * snd_hda_build_pcms - build PCM information
1551  * @bus: the BUS
1552  *
1553  * Create PCM information for each codec included in the bus.
1554  *
1555  * The build_pcms codec patch is requested to set up codec->num_pcms and
1556  * codec->pcm_info properly.  The array is referred by the top-level driver
1557  * to create its PCM instances.
1558  * The allocated codec->pcm_info should be released in codec->patch_ops.free
1559  * callback.
1560  *
1561  * At least, substreams, channels_min and channels_max must be filled for
1562  * each stream.  substreams = 0 indicates that the stream doesn't exist.
1563  * When rates and/or formats are zero, the supported values are queried
1564  * from the given nid.  The nid is used also by the default ops.prepare
1565  * and ops.cleanup callbacks.
1566  *
1567  * The driver needs to call ops.open in its open callback.  Similarly,
1568  * ops.close is supposed to be called in the close callback.
1569  * ops.prepare should be called in the prepare or hw_params callback
1570  * with the proper parameters for set up.
1571  * ops.cleanup should be called in hw_free for clean up of streams.
1572  *
1573  * This function returns 0 if successfull, or a negative error code.
1574  */
1575 int snd_hda_build_pcms(struct hda_bus *bus)
1576 {
1577         struct list_head *p;
1578
1579         list_for_each(p, &bus->codec_list) {
1580                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1581                 unsigned int pcm, s;
1582                 int err;
1583                 if (! codec->patch_ops.build_pcms)
1584                         continue;
1585                 err = codec->patch_ops.build_pcms(codec);
1586                 if (err < 0)
1587                         return err;
1588                 for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1589                         for (s = 0; s < 2; s++) {
1590                                 struct hda_pcm_stream *info;
1591                                 info = &codec->pcm_info[pcm].stream[s];
1592                                 if (! info->substreams)
1593                                         continue;
1594                                 err = set_pcm_default_values(codec, info);
1595                                 if (err < 0)
1596                                         return err;
1597                         }
1598                 }
1599         }
1600         return 0;
1601 }
1602
1603
1604 /**
1605  * snd_hda_check_board_config - compare the current codec with the config table
1606  * @codec: the HDA codec
1607  * @tbl: configuration table, terminated by null entries
1608  *
1609  * Compares the modelname or PCI subsystem id of the current codec with the
1610  * given configuration table.  If a matching entry is found, returns its
1611  * config value (supposed to be 0 or positive).
1612  *
1613  * If no entries are matching, the function returns a negative value.
1614  */
1615 int snd_hda_check_board_config(struct hda_codec *codec, const struct hda_board_config *tbl)
1616 {
1617         const struct hda_board_config *c;
1618
1619         if (codec->bus->modelname) {
1620                 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1621                         if (c->modelname &&
1622                             ! strcmp(codec->bus->modelname, c->modelname)) {
1623                                 snd_printd(KERN_INFO "hda_codec: model '%s' is selected\n", c->modelname);
1624                                 return c->config;
1625                         }
1626                 }
1627         }
1628
1629         if (codec->bus->pci) {
1630                 u16 subsystem_vendor, subsystem_device;
1631                 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_VENDOR_ID, &subsystem_vendor);
1632                 pci_read_config_word(codec->bus->pci, PCI_SUBSYSTEM_ID, &subsystem_device);
1633                 for (c = tbl; c->modelname || c->pci_subvendor; c++) {
1634                         if (c->pci_subvendor == subsystem_vendor &&
1635                             (! c->pci_subdevice /* all match */||
1636                              (c->pci_subdevice == subsystem_device))) {
1637                                 snd_printdd(KERN_INFO "hda_codec: PCI %x:%x, codec config %d is selected\n",
1638                                             subsystem_vendor, subsystem_device, c->config);
1639                                 return c->config;
1640                         }
1641                 }
1642         }
1643         return -1;
1644 }
1645
1646 /**
1647  * snd_hda_add_new_ctls - create controls from the array
1648  * @codec: the HDA codec
1649  * @knew: the array of snd_kcontrol_new_t
1650  *
1651  * This helper function creates and add new controls in the given array.
1652  * The array must be terminated with an empty entry as terminator.
1653  *
1654  * Returns 0 if successful, or a negative error code.
1655  */
1656 int snd_hda_add_new_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
1657 {
1658         int err;
1659
1660         for (; knew->name; knew++) {
1661                 err = snd_ctl_add(codec->bus->card, snd_ctl_new1(knew, codec));
1662                 if (err < 0)
1663                         return err;
1664         }
1665         return 0;
1666 }
1667
1668
1669  /*
1670  * Channel mode helper
1671  */
1672 int snd_hda_ch_mode_info(struct hda_codec *codec, snd_ctl_elem_info_t *uinfo,
1673                          const struct hda_channel_mode *chmode, int num_chmodes)
1674 {
1675         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1676         uinfo->count = 1;
1677         uinfo->value.enumerated.items = num_chmodes;
1678         if (uinfo->value.enumerated.item >= num_chmodes)
1679                 uinfo->value.enumerated.item = num_chmodes - 1;
1680         sprintf(uinfo->value.enumerated.name, "%dch",
1681                 chmode[uinfo->value.enumerated.item].channels);
1682         return 0;
1683 }
1684
1685 int snd_hda_ch_mode_get(struct hda_codec *codec, snd_ctl_elem_value_t *ucontrol,
1686                         const struct hda_channel_mode *chmode, int num_chmodes,
1687                         int max_channels)
1688 {
1689         int i;
1690
1691         for (i = 0; i < num_chmodes; i++) {
1692                 if (max_channels == chmode[i].channels) {
1693                         ucontrol->value.enumerated.item[0] = i;
1694                         break;
1695                 }
1696         }
1697         return 0;
1698 }
1699
1700 int snd_hda_ch_mode_put(struct hda_codec *codec, snd_ctl_elem_value_t *ucontrol,
1701                         const struct hda_channel_mode *chmode, int num_chmodes,
1702                         int *max_channelsp)
1703 {
1704         unsigned int mode;
1705
1706         mode = ucontrol->value.enumerated.item[0];
1707         snd_assert(mode < num_chmodes, return -EINVAL);
1708         if (*max_channelsp && ! codec->in_resume)
1709                 return 0;
1710         /* change the current channel setting */
1711         *max_channelsp = chmode[mode].channels;
1712         if (chmode[mode].sequence)
1713                 snd_hda_sequence_write(codec, chmode[mode].sequence);
1714         return 1;
1715 }
1716
1717 /*
1718  * input MUX helper
1719  */
1720 int snd_hda_input_mux_info(const struct hda_input_mux *imux, snd_ctl_elem_info_t *uinfo)
1721 {
1722         unsigned int index;
1723
1724         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1725         uinfo->count = 1;
1726         uinfo->value.enumerated.items = imux->num_items;
1727         index = uinfo->value.enumerated.item;
1728         if (index >= imux->num_items)
1729                 index = imux->num_items - 1;
1730         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
1731         return 0;
1732 }
1733
1734 int snd_hda_input_mux_put(struct hda_codec *codec, const struct hda_input_mux *imux,
1735                           snd_ctl_elem_value_t *ucontrol, hda_nid_t nid,
1736                           unsigned int *cur_val)
1737 {
1738         unsigned int idx;
1739
1740         idx = ucontrol->value.enumerated.item[0];
1741         if (idx >= imux->num_items)
1742                 idx = imux->num_items - 1;
1743         if (*cur_val == idx && ! codec->in_resume)
1744                 return 0;
1745         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
1746                             imux->items[idx].index);
1747         *cur_val = idx;
1748         return 1;
1749 }
1750
1751
1752 /*
1753  * Multi-channel / digital-out PCM helper functions
1754  */
1755
1756 /*
1757  * open the digital out in the exclusive mode
1758  */
1759 int snd_hda_multi_out_dig_open(struct hda_codec *codec, struct hda_multi_out *mout)
1760 {
1761         down(&codec->spdif_mutex);
1762         if (mout->dig_out_used) {
1763                 up(&codec->spdif_mutex);
1764                 return -EBUSY; /* already being used */
1765         }
1766         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
1767         up(&codec->spdif_mutex);
1768         return 0;
1769 }
1770
1771 /*
1772  * release the digital out
1773  */
1774 int snd_hda_multi_out_dig_close(struct hda_codec *codec, struct hda_multi_out *mout)
1775 {
1776         down(&codec->spdif_mutex);
1777         mout->dig_out_used = 0;
1778         up(&codec->spdif_mutex);
1779         return 0;
1780 }
1781
1782 /*
1783  * set up more restrictions for analog out
1784  */
1785 int snd_hda_multi_out_analog_open(struct hda_codec *codec, struct hda_multi_out *mout,
1786                                   snd_pcm_substream_t *substream)
1787 {
1788         substream->runtime->hw.channels_max = mout->max_channels;
1789         return snd_pcm_hw_constraint_step(substream->runtime, 0,
1790                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
1791 }
1792
1793 /*
1794  * set up the i/o for analog out
1795  * when the digital out is available, copy the front out to digital out, too.
1796  */
1797 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
1798                                      unsigned int stream_tag,
1799                                      unsigned int format,
1800                                      snd_pcm_substream_t *substream)
1801 {
1802         hda_nid_t *nids = mout->dac_nids;
1803         int chs = substream->runtime->channels;
1804         int i;
1805
1806         down(&codec->spdif_mutex);
1807         if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
1808                 if (chs == 2 &&
1809                     snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
1810                     ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
1811                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
1812                         /* setup digital receiver */
1813                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
1814                                                    stream_tag, 0, format);
1815                 } else {
1816                         mout->dig_out_used = 0;
1817                         snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1818                 }
1819         }
1820         up(&codec->spdif_mutex);
1821
1822         /* front */
1823         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
1824         if (mout->hp_nid)
1825                 /* headphone out will just decode front left/right (stereo) */
1826                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
1827         /* surrounds */
1828         for (i = 1; i < mout->num_dacs; i++) {
1829                 if (chs >= (i + 1) * 2) /* independent out */
1830                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
1831                                                    format);
1832                 else /* copy front */
1833                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
1834                                                    format);
1835         }
1836         return 0;
1837 }
1838
1839 /*
1840  * clean up the setting for analog out
1841  */
1842 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec, struct hda_multi_out *mout)
1843 {
1844         hda_nid_t *nids = mout->dac_nids;
1845         int i;
1846
1847         for (i = 0; i < mout->num_dacs; i++)
1848                 snd_hda_codec_setup_stream(codec, nids[i], 0, 0, 0);
1849         if (mout->hp_nid)
1850                 snd_hda_codec_setup_stream(codec, mout->hp_nid, 0, 0, 0);
1851         down(&codec->spdif_mutex);
1852         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
1853                 snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
1854                 mout->dig_out_used = 0;
1855         }
1856         up(&codec->spdif_mutex);
1857         return 0;
1858 }
1859
1860 /*
1861  * Helper for automatic ping configuration
1862  */
1863 /* parse all pin widgets and store the useful pin nids to cfg */
1864 int snd_hda_parse_pin_def_config(struct hda_codec *codec, struct auto_pin_cfg *cfg)
1865 {
1866         hda_nid_t nid, nid_start;
1867         int i, j, nodes;
1868         short seq, sequences[4], assoc_line_out;
1869
1870         memset(cfg, 0, sizeof(*cfg));
1871
1872         memset(sequences, 0, sizeof(sequences));
1873         assoc_line_out = 0;
1874
1875         nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
1876         for (nid = nid_start; nid < nodes + nid_start; nid++) {
1877                 unsigned int wid_caps = snd_hda_param_read(codec, nid,
1878                                                            AC_PAR_AUDIO_WIDGET_CAP);
1879                 unsigned int wid_type = (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
1880                 unsigned int def_conf;
1881                 short assoc, loc;
1882
1883                 /* read all default configuration for pin complex */
1884                 if (wid_type != AC_WID_PIN)
1885                         continue;
1886                 def_conf = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
1887                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
1888                         continue;
1889                 loc = get_defcfg_location(def_conf);
1890                 switch (get_defcfg_device(def_conf)) {
1891                 case AC_JACK_LINE_OUT:
1892                 case AC_JACK_SPEAKER:
1893                         seq = get_defcfg_sequence(def_conf);
1894                         assoc = get_defcfg_association(def_conf);
1895                         if (! assoc)
1896                                 continue;
1897                         if (! assoc_line_out)
1898                                 assoc_line_out = assoc;
1899                         else if (assoc_line_out != assoc)
1900                                 continue;
1901                         if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
1902                                 continue;
1903                         cfg->line_out_pins[cfg->line_outs] = nid;
1904                         sequences[cfg->line_outs] = seq;
1905                         cfg->line_outs++;
1906                         break;
1907                 case AC_JACK_HP_OUT:
1908                         cfg->hp_pin = nid;
1909                         break;
1910                 case AC_JACK_MIC_IN:
1911                         if (loc == AC_JACK_LOC_FRONT)
1912                                 cfg->input_pins[AUTO_PIN_FRONT_MIC] = nid;
1913                         else
1914                                 cfg->input_pins[AUTO_PIN_MIC] = nid;
1915                         break;
1916                 case AC_JACK_LINE_IN:
1917                         if (loc == AC_JACK_LOC_FRONT)
1918                                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
1919                         else
1920                                 cfg->input_pins[AUTO_PIN_LINE] = nid;
1921                         break;
1922                 case AC_JACK_CD:
1923                         cfg->input_pins[AUTO_PIN_CD] = nid;
1924                         break;
1925                 case AC_JACK_AUX:
1926                         cfg->input_pins[AUTO_PIN_AUX] = nid;
1927                         break;
1928                 case AC_JACK_SPDIF_OUT:
1929                         cfg->dig_out_pin = nid;
1930                         break;
1931                 case AC_JACK_SPDIF_IN:
1932                         cfg->dig_in_pin = nid;
1933                         break;
1934                 }
1935         }
1936
1937         /* sort by sequence */
1938         for (i = 0; i < cfg->line_outs; i++)
1939                 for (j = i + 1; j < cfg->line_outs; j++)
1940                         if (sequences[i] > sequences[j]) {
1941                                 seq = sequences[i];
1942                                 sequences[i] = sequences[j];
1943                                 sequences[j] = seq;
1944                                 nid = cfg->line_out_pins[i];
1945                                 cfg->line_out_pins[i] = cfg->line_out_pins[j];
1946                                 cfg->line_out_pins[j] = nid;
1947                         }
1948
1949         /* Reorder the surround channels
1950          * ALSA sequence is front/surr/clfe/side
1951          * HDA sequence is:
1952          *    4-ch: front/surr  =>  OK as it is
1953          *    6-ch: front/clfe/surr
1954          *    8-ch: front/clfe/side/surr
1955          */
1956         switch (cfg->line_outs) {
1957         case 3:
1958                 nid = cfg->line_out_pins[1];
1959                 cfg->line_out_pins[1] = cfg->line_out_pins[2];
1960                 cfg->line_out_pins[2] = nid;
1961                 break;
1962         case 4:
1963                 nid = cfg->line_out_pins[1];
1964                 cfg->line_out_pins[1] = cfg->line_out_pins[3];
1965                 cfg->line_out_pins[3] = cfg->line_out_pins[2];
1966                 cfg->line_out_pins[2] = nid;
1967                 break;
1968         }
1969
1970         return 0;
1971 }
1972
1973 #ifdef CONFIG_PM
1974 /*
1975  * power management
1976  */
1977
1978 /**
1979  * snd_hda_suspend - suspend the codecs
1980  * @bus: the HDA bus
1981  * @state: suspsend state
1982  *
1983  * Returns 0 if successful.
1984  */
1985 int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
1986 {
1987         struct list_head *p;
1988
1989         /* FIXME: should handle power widget capabilities */
1990         list_for_each(p, &bus->codec_list) {
1991                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
1992                 if (codec->patch_ops.suspend)
1993                         codec->patch_ops.suspend(codec, state);
1994         }
1995         return 0;
1996 }
1997
1998 /**
1999  * snd_hda_resume - resume the codecs
2000  * @bus: the HDA bus
2001  * @state: resume state
2002  *
2003  * Returns 0 if successful.
2004  */
2005 int snd_hda_resume(struct hda_bus *bus)
2006 {
2007         struct list_head *p;
2008
2009         list_for_each(p, &bus->codec_list) {
2010                 struct hda_codec *codec = list_entry(p, struct hda_codec, list);
2011                 if (codec->patch_ops.resume)
2012                         codec->patch_ops.resume(codec);
2013         }
2014         return 0;
2015 }
2016
2017 /**
2018  * snd_hda_resume_ctls - resume controls in the new control list
2019  * @codec: the HDA codec
2020  * @knew: the array of snd_kcontrol_new_t
2021  *
2022  * This function resumes the mixer controls in the snd_kcontrol_new_t array,
2023  * originally for snd_hda_add_new_ctls().
2024  * The array must be terminated with an empty entry as terminator.
2025  */
2026 int snd_hda_resume_ctls(struct hda_codec *codec, snd_kcontrol_new_t *knew)
2027 {
2028         snd_ctl_elem_value_t *val;
2029
2030         val = kmalloc(sizeof(*val), GFP_KERNEL);
2031         if (! val)
2032                 return -ENOMEM;
2033         codec->in_resume = 1;
2034         for (; knew->name; knew++) {
2035                 int i, count;
2036                 count = knew->count ? knew->count : 1;
2037                 for (i = 0; i < count; i++) {
2038                         memset(val, 0, sizeof(*val));
2039                         val->id.iface = knew->iface;
2040                         val->id.device = knew->device;
2041                         val->id.subdevice = knew->subdevice;
2042                         strcpy(val->id.name, knew->name);
2043                         val->id.index = knew->index ? knew->index : i;
2044                         /* Assume that get callback reads only from cache,
2045                          * not accessing to the real hardware
2046                          */
2047                         if (snd_ctl_elem_read(codec->bus->card, val) < 0)
2048                                 continue;
2049                         snd_ctl_elem_write(codec->bus->card, NULL, val);
2050                 }
2051         }
2052         codec->in_resume = 0;
2053         kfree(val);
2054         return 0;
2055 }
2056
2057 /**
2058  * snd_hda_resume_spdif_out - resume the digital out
2059  * @codec: the HDA codec
2060  */
2061 int snd_hda_resume_spdif_out(struct hda_codec *codec)
2062 {
2063         return snd_hda_resume_ctls(codec, dig_mixes);
2064 }
2065
2066 /**
2067  * snd_hda_resume_spdif_in - resume the digital in
2068  * @codec: the HDA codec
2069  */
2070 int snd_hda_resume_spdif_in(struct hda_codec *codec)
2071 {
2072         return snd_hda_resume_ctls(codec, dig_in_ctls);
2073 }
2074 #endif
2075
2076 /*
2077  * symbols exported for controller modules
2078  */
2079 EXPORT_SYMBOL(snd_hda_codec_read);
2080 EXPORT_SYMBOL(snd_hda_codec_write);
2081 EXPORT_SYMBOL(snd_hda_sequence_write);
2082 EXPORT_SYMBOL(snd_hda_get_sub_nodes);
2083 EXPORT_SYMBOL(snd_hda_queue_unsol_event);
2084 EXPORT_SYMBOL(snd_hda_bus_new);
2085 EXPORT_SYMBOL(snd_hda_codec_new);
2086 EXPORT_SYMBOL(snd_hda_codec_setup_stream);
2087 EXPORT_SYMBOL(snd_hda_calc_stream_format);
2088 EXPORT_SYMBOL(snd_hda_build_pcms);
2089 EXPORT_SYMBOL(snd_hda_build_controls);
2090 #ifdef CONFIG_PM
2091 EXPORT_SYMBOL(snd_hda_suspend);
2092 EXPORT_SYMBOL(snd_hda_resume);
2093 #endif
2094
2095 /*
2096  *  INIT part
2097  */
2098
2099 static int __init alsa_hda_init(void)
2100 {
2101         return 0;
2102 }
2103
2104 static void __exit alsa_hda_exit(void)
2105 {
2106 }
2107
2108 module_init(alsa_hda_init)
2109 module_exit(alsa_hda_exit)