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