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