ALSA: hda - Fix the previous sanity check in make_codec_cmd()
[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         if ((codec->addr & ~0xf) || (direct & ~1) || (nid & ~0x7f) ||
154             (verb & ~0xfff) || (parm & ~0xffff)) {
155                 printk(KERN_ERR "hda-codec: out of range cmd %x:%x:%x:%x:%x\n",
156                        codec->addr, direct, nid, verb, parm);
157                 return ~0;
158         }
159
160         val = (u32)codec->addr << 28;
161         val |= (u32)direct << 27;
162         val |= (u32)nid << 20;
163         val |= verb << 8;
164         val |= parm;
165         return val;
166 }
167
168 /*
169  * Send and receive a verb
170  */
171 static int codec_exec_verb(struct hda_codec *codec, unsigned int cmd,
172                            unsigned int *res)
173 {
174         struct hda_bus *bus = codec->bus;
175         int err;
176
177         if (cmd == ~0)
178                 return -1;
179
180         if (res)
181                 *res = -1;
182  again:
183         snd_hda_power_up(codec);
184         mutex_lock(&bus->cmd_mutex);
185         err = bus->ops.command(bus, cmd);
186         if (!err && res)
187                 *res = bus->ops.get_response(bus);
188         mutex_unlock(&bus->cmd_mutex);
189         snd_hda_power_down(codec);
190         if (res && *res == -1 && bus->rirb_error) {
191                 if (bus->response_reset) {
192                         snd_printd("hda_codec: resetting BUS due to "
193                                    "fatal communication error\n");
194                         bus->ops.bus_reset(bus);
195                 }
196                 goto again;
197         }
198         /* clear reset-flag when the communication gets recovered */
199         if (!err)
200                 bus->response_reset = 0;
201         return err;
202 }
203
204 /**
205  * snd_hda_codec_read - send a command and get the response
206  * @codec: the HDA codec
207  * @nid: NID to send the command
208  * @direct: direct flag
209  * @verb: the verb to send
210  * @parm: the parameter for the verb
211  *
212  * Send a single command and read the corresponding response.
213  *
214  * Returns the obtained response value, or -1 for an error.
215  */
216 unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
217                                 int direct,
218                                 unsigned int verb, unsigned int parm)
219 {
220         unsigned cmd = make_codec_cmd(codec, nid, direct, verb, parm);
221         unsigned int res;
222         codec_exec_verb(codec, cmd, &res);
223         return res;
224 }
225 EXPORT_SYMBOL_HDA(snd_hda_codec_read);
226
227 /**
228  * snd_hda_codec_write - send a single command without waiting for response
229  * @codec: the HDA codec
230  * @nid: NID to send the command
231  * @direct: direct flag
232  * @verb: the verb to send
233  * @parm: the parameter for the verb
234  *
235  * Send a single command without waiting for response.
236  *
237  * Returns 0 if successful, or a negative error code.
238  */
239 int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
240                          unsigned int verb, unsigned int parm)
241 {
242         unsigned int cmd = make_codec_cmd(codec, nid, direct, verb, parm);
243         unsigned int res;
244         return codec_exec_verb(codec, cmd,
245                                codec->bus->sync_write ? &res : NULL);
246 }
247 EXPORT_SYMBOL_HDA(snd_hda_codec_write);
248
249 /**
250  * snd_hda_sequence_write - sequence writes
251  * @codec: the HDA codec
252  * @seq: VERB array to send
253  *
254  * Send the commands sequentially from the given array.
255  * The array must be terminated with NID=0.
256  */
257 void snd_hda_sequence_write(struct hda_codec *codec, const struct hda_verb *seq)
258 {
259         for (; seq->nid; seq++)
260                 snd_hda_codec_write(codec, seq->nid, 0, seq->verb, seq->param);
261 }
262 EXPORT_SYMBOL_HDA(snd_hda_sequence_write);
263
264 /**
265  * snd_hda_get_sub_nodes - get the range of sub nodes
266  * @codec: the HDA codec
267  * @nid: NID to parse
268  * @start_id: the pointer to store the start NID
269  *
270  * Parse the NID and store the start NID of its sub-nodes.
271  * Returns the number of sub-nodes.
272  */
273 int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
274                           hda_nid_t *start_id)
275 {
276         unsigned int parm;
277
278         parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
279         if (parm == -1)
280                 return 0;
281         *start_id = (parm >> 16) & 0x7fff;
282         return (int)(parm & 0x7fff);
283 }
284 EXPORT_SYMBOL_HDA(snd_hda_get_sub_nodes);
285
286 /**
287  * snd_hda_get_connections - get connection list
288  * @codec: the HDA codec
289  * @nid: NID to parse
290  * @conn_list: connection list array
291  * @max_conns: max. number of connections to store
292  *
293  * Parses the connection list of the given widget and stores the list
294  * of NIDs.
295  *
296  * Returns the number of connections, or a negative error code.
297  */
298 int snd_hda_get_connections(struct hda_codec *codec, hda_nid_t nid,
299                             hda_nid_t *conn_list, int max_conns)
300 {
301         unsigned int parm;
302         int i, conn_len, conns;
303         unsigned int shift, num_elems, mask;
304         hda_nid_t prev_nid;
305
306         if (snd_BUG_ON(!conn_list || max_conns <= 0))
307                 return -EINVAL;
308
309         parm = snd_hda_param_read(codec, nid, AC_PAR_CONNLIST_LEN);
310         if (parm & AC_CLIST_LONG) {
311                 /* long form */
312                 shift = 16;
313                 num_elems = 2;
314         } else {
315                 /* short form */
316                 shift = 8;
317                 num_elems = 4;
318         }
319         conn_len = parm & AC_CLIST_LENGTH;
320         mask = (1 << (shift-1)) - 1;
321
322         if (!conn_len)
323                 return 0; /* no connection */
324
325         if (conn_len == 1) {
326                 /* single connection */
327                 parm = snd_hda_codec_read(codec, nid, 0,
328                                           AC_VERB_GET_CONNECT_LIST, 0);
329                 if (parm == -1 && codec->bus->rirb_error)
330                         return -EIO;
331                 conn_list[0] = parm & mask;
332                 return 1;
333         }
334
335         /* multi connection */
336         conns = 0;
337         prev_nid = 0;
338         for (i = 0; i < conn_len; i++) {
339                 int range_val;
340                 hda_nid_t val, n;
341
342                 if (i % num_elems == 0) {
343                         parm = snd_hda_codec_read(codec, nid, 0,
344                                                   AC_VERB_GET_CONNECT_LIST, i);
345                         if (parm == -1 && codec->bus->rirb_error)
346                                 return -EIO;
347                 }
348                 range_val = !!(parm & (1 << (shift-1))); /* ranges */
349                 val = parm & mask;
350                 parm >>= shift;
351                 if (range_val) {
352                         /* ranges between the previous and this one */
353                         if (!prev_nid || prev_nid >= val) {
354                                 snd_printk(KERN_WARNING "hda_codec: "
355                                            "invalid dep_range_val %x:%x\n",
356                                            prev_nid, val);
357                                 continue;
358                         }
359                         for (n = prev_nid + 1; n <= val; n++) {
360                                 if (conns >= max_conns) {
361                                         snd_printk(KERN_ERR
362                                                    "Too many connections\n");
363                                         return -EINVAL;
364                                 }
365                                 conn_list[conns++] = n;
366                         }
367                 } else {
368                         if (conns >= max_conns) {
369                                 snd_printk(KERN_ERR "Too many connections\n");
370                                 return -EINVAL;
371                         }
372                         conn_list[conns++] = val;
373                 }
374                 prev_nid = val;
375         }
376         return conns;
377 }
378 EXPORT_SYMBOL_HDA(snd_hda_get_connections);
379
380
381 /**
382  * snd_hda_queue_unsol_event - add an unsolicited event to queue
383  * @bus: the BUS
384  * @res: unsolicited event (lower 32bit of RIRB entry)
385  * @res_ex: codec addr and flags (upper 32bit or RIRB entry)
386  *
387  * Adds the given event to the queue.  The events are processed in
388  * the workqueue asynchronously.  Call this function in the interrupt
389  * hanlder when RIRB receives an unsolicited event.
390  *
391  * Returns 0 if successful, or a negative error code.
392  */
393 int snd_hda_queue_unsol_event(struct hda_bus *bus, u32 res, u32 res_ex)
394 {
395         struct hda_bus_unsolicited *unsol;
396         unsigned int wp;
397
398         unsol = bus->unsol;
399         if (!unsol)
400                 return 0;
401
402         wp = (unsol->wp + 1) % HDA_UNSOL_QUEUE_SIZE;
403         unsol->wp = wp;
404
405         wp <<= 1;
406         unsol->queue[wp] = res;
407         unsol->queue[wp + 1] = res_ex;
408
409         queue_work(bus->workq, &unsol->work);
410
411         return 0;
412 }
413 EXPORT_SYMBOL_HDA(snd_hda_queue_unsol_event);
414
415 /*
416  * process queued unsolicited events
417  */
418 static void process_unsol_events(struct work_struct *work)
419 {
420         struct hda_bus_unsolicited *unsol =
421                 container_of(work, struct hda_bus_unsolicited, work);
422         struct hda_bus *bus = unsol->bus;
423         struct hda_codec *codec;
424         unsigned int rp, caddr, res;
425
426         while (unsol->rp != unsol->wp) {
427                 rp = (unsol->rp + 1) % HDA_UNSOL_QUEUE_SIZE;
428                 unsol->rp = rp;
429                 rp <<= 1;
430                 res = unsol->queue[rp];
431                 caddr = unsol->queue[rp + 1];
432                 if (!(caddr & (1 << 4))) /* no unsolicited event? */
433                         continue;
434                 codec = bus->caddr_tbl[caddr & 0x0f];
435                 if (codec && codec->patch_ops.unsol_event)
436                         codec->patch_ops.unsol_event(codec, res);
437         }
438 }
439
440 /*
441  * initialize unsolicited queue
442  */
443 static int init_unsol_queue(struct hda_bus *bus)
444 {
445         struct hda_bus_unsolicited *unsol;
446
447         if (bus->unsol) /* already initialized */
448                 return 0;
449
450         unsol = kzalloc(sizeof(*unsol), GFP_KERNEL);
451         if (!unsol) {
452                 snd_printk(KERN_ERR "hda_codec: "
453                            "can't allocate unsolicited queue\n");
454                 return -ENOMEM;
455         }
456         INIT_WORK(&unsol->work, process_unsol_events);
457         unsol->bus = bus;
458         bus->unsol = unsol;
459         return 0;
460 }
461
462 /*
463  * destructor
464  */
465 static void snd_hda_codec_free(struct hda_codec *codec);
466
467 static int snd_hda_bus_free(struct hda_bus *bus)
468 {
469         struct hda_codec *codec, *n;
470
471         if (!bus)
472                 return 0;
473         if (bus->workq)
474                 flush_workqueue(bus->workq);
475         if (bus->unsol)
476                 kfree(bus->unsol);
477         list_for_each_entry_safe(codec, n, &bus->codec_list, list) {
478                 snd_hda_codec_free(codec);
479         }
480         if (bus->ops.private_free)
481                 bus->ops.private_free(bus);
482         if (bus->workq)
483                 destroy_workqueue(bus->workq);
484         kfree(bus);
485         return 0;
486 }
487
488 static int snd_hda_bus_dev_free(struct snd_device *device)
489 {
490         struct hda_bus *bus = device->device_data;
491         bus->shutdown = 1;
492         return snd_hda_bus_free(bus);
493 }
494
495 #ifdef CONFIG_SND_HDA_HWDEP
496 static int snd_hda_bus_dev_register(struct snd_device *device)
497 {
498         struct hda_bus *bus = device->device_data;
499         struct hda_codec *codec;
500         list_for_each_entry(codec, &bus->codec_list, list) {
501                 snd_hda_hwdep_add_sysfs(codec);
502         }
503         return 0;
504 }
505 #else
506 #define snd_hda_bus_dev_register        NULL
507 #endif
508
509 /**
510  * snd_hda_bus_new - create a HDA bus
511  * @card: the card entry
512  * @temp: the template for hda_bus information
513  * @busp: the pointer to store the created bus instance
514  *
515  * Returns 0 if successful, or a negative error code.
516  */
517 int /*__devinit*/ snd_hda_bus_new(struct snd_card *card,
518                               const struct hda_bus_template *temp,
519                               struct hda_bus **busp)
520 {
521         struct hda_bus *bus;
522         int err;
523         static struct snd_device_ops dev_ops = {
524                 .dev_register = snd_hda_bus_dev_register,
525                 .dev_free = snd_hda_bus_dev_free,
526         };
527
528         if (snd_BUG_ON(!temp))
529                 return -EINVAL;
530         if (snd_BUG_ON(!temp->ops.command || !temp->ops.get_response))
531                 return -EINVAL;
532
533         if (busp)
534                 *busp = NULL;
535
536         bus = kzalloc(sizeof(*bus), GFP_KERNEL);
537         if (bus == NULL) {
538                 snd_printk(KERN_ERR "can't allocate struct hda_bus\n");
539                 return -ENOMEM;
540         }
541
542         bus->card = card;
543         bus->private_data = temp->private_data;
544         bus->pci = temp->pci;
545         bus->modelname = temp->modelname;
546         bus->power_save = temp->power_save;
547         bus->ops = temp->ops;
548
549         mutex_init(&bus->cmd_mutex);
550         INIT_LIST_HEAD(&bus->codec_list);
551
552         snprintf(bus->workq_name, sizeof(bus->workq_name),
553                  "hd-audio%d", card->number);
554         bus->workq = create_singlethread_workqueue(bus->workq_name);
555         if (!bus->workq) {
556                 snd_printk(KERN_ERR "cannot create workqueue %s\n",
557                            bus->workq_name);
558                 kfree(bus);
559                 return -ENOMEM;
560         }
561
562         err = snd_device_new(card, SNDRV_DEV_BUS, bus, &dev_ops);
563         if (err < 0) {
564                 snd_hda_bus_free(bus);
565                 return err;
566         }
567         if (busp)
568                 *busp = bus;
569         return 0;
570 }
571 EXPORT_SYMBOL_HDA(snd_hda_bus_new);
572
573 #ifdef CONFIG_SND_HDA_GENERIC
574 #define is_generic_config(codec) \
575         (codec->modelname && !strcmp(codec->modelname, "generic"))
576 #else
577 #define is_generic_config(codec)        0
578 #endif
579
580 #ifdef MODULE
581 #define HDA_MODREQ_MAX_COUNT    2       /* two request_modules()'s */
582 #else
583 #define HDA_MODREQ_MAX_COUNT    0       /* all presets are statically linked */
584 #endif
585
586 /*
587  * find a matching codec preset
588  */
589 static const struct hda_codec_preset *
590 find_codec_preset(struct hda_codec *codec)
591 {
592         struct hda_codec_preset_list *tbl;
593         const struct hda_codec_preset *preset;
594         int mod_requested = 0;
595
596         if (is_generic_config(codec))
597                 return NULL; /* use the generic parser */
598
599  again:
600         mutex_lock(&preset_mutex);
601         list_for_each_entry(tbl, &hda_preset_tables, list) {
602                 if (!try_module_get(tbl->owner)) {
603                         snd_printk(KERN_ERR "hda_codec: cannot module_get\n");
604                         continue;
605                 }
606                 for (preset = tbl->preset; preset->id; preset++) {
607                         u32 mask = preset->mask;
608                         if (preset->afg && preset->afg != codec->afg)
609                                 continue;
610                         if (preset->mfg && preset->mfg != codec->mfg)
611                                 continue;
612                         if (!mask)
613                                 mask = ~0;
614                         if (preset->id == (codec->vendor_id & mask) &&
615                             (!preset->rev ||
616                              preset->rev == codec->revision_id)) {
617                                 mutex_unlock(&preset_mutex);
618                                 codec->owner = tbl->owner;
619                                 return preset;
620                         }
621                 }
622                 module_put(tbl->owner);
623         }
624         mutex_unlock(&preset_mutex);
625
626         if (mod_requested < HDA_MODREQ_MAX_COUNT) {
627                 char name[32];
628                 if (!mod_requested)
629                         snprintf(name, sizeof(name), "snd-hda-codec-id:%08x",
630                                  codec->vendor_id);
631                 else
632                         snprintf(name, sizeof(name), "snd-hda-codec-id:%04x*",
633                                  (codec->vendor_id >> 16) & 0xffff);
634                 request_module(name);
635                 mod_requested++;
636                 goto again;
637         }
638         return NULL;
639 }
640
641 /*
642  * get_codec_name - store the codec name
643  */
644 static int get_codec_name(struct hda_codec *codec)
645 {
646         const struct hda_vendor_id *c;
647         const char *vendor = NULL;
648         u16 vendor_id = codec->vendor_id >> 16;
649         char tmp[16];
650
651         if (codec->vendor_name)
652                 goto get_chip_name;
653
654         for (c = hda_vendor_ids; c->id; c++) {
655                 if (c->id == vendor_id) {
656                         vendor = c->name;
657                         break;
658                 }
659         }
660         if (!vendor) {
661                 sprintf(tmp, "Generic %04x", vendor_id);
662                 vendor = tmp;
663         }
664         codec->vendor_name = kstrdup(vendor, GFP_KERNEL);
665         if (!codec->vendor_name)
666                 return -ENOMEM;
667
668  get_chip_name:
669         if (codec->chip_name)
670                 return 0;
671
672         if (codec->preset && codec->preset->name)
673                 codec->chip_name = kstrdup(codec->preset->name, GFP_KERNEL);
674         else {
675                 sprintf(tmp, "ID %x", codec->vendor_id & 0xffff);
676                 codec->chip_name = kstrdup(tmp, GFP_KERNEL);
677         }
678         if (!codec->chip_name)
679                 return -ENOMEM;
680         return 0;
681 }
682
683 /*
684  * look for an AFG and MFG nodes
685  */
686 static void /*__devinit*/ setup_fg_nodes(struct hda_codec *codec)
687 {
688         int i, total_nodes, function_id;
689         hda_nid_t nid;
690
691         total_nodes = snd_hda_get_sub_nodes(codec, AC_NODE_ROOT, &nid);
692         for (i = 0; i < total_nodes; i++, nid++) {
693                 function_id = snd_hda_param_read(codec, nid,
694                                                 AC_PAR_FUNCTION_TYPE) & 0xff;
695                 switch (function_id) {
696                 case AC_GRP_AUDIO_FUNCTION:
697                         codec->afg = nid;
698                         codec->function_id = function_id;
699                         break;
700                 case AC_GRP_MODEM_FUNCTION:
701                         codec->mfg = nid;
702                         codec->function_id = function_id;
703                         break;
704                 default:
705                         break;
706                 }
707         }
708 }
709
710 /*
711  * read widget caps for each widget and store in cache
712  */
713 static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
714 {
715         int i;
716         hda_nid_t nid;
717
718         codec->num_nodes = snd_hda_get_sub_nodes(codec, fg_node,
719                                                  &codec->start_nid);
720         codec->wcaps = kmalloc(codec->num_nodes * 4, GFP_KERNEL);
721         if (!codec->wcaps)
722                 return -ENOMEM;
723         nid = codec->start_nid;
724         for (i = 0; i < codec->num_nodes; i++, nid++)
725                 codec->wcaps[i] = snd_hda_param_read(codec, nid,
726                                                      AC_PAR_AUDIO_WIDGET_CAP);
727         return 0;
728 }
729
730 /* read all pin default configurations and save codec->init_pins */
731 static int read_pin_defaults(struct hda_codec *codec)
732 {
733         int i;
734         hda_nid_t nid = codec->start_nid;
735
736         for (i = 0; i < codec->num_nodes; i++, nid++) {
737                 struct hda_pincfg *pin;
738                 unsigned int wcaps = get_wcaps(codec, nid);
739                 unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
740                                 AC_WCAP_TYPE_SHIFT;
741                 if (wid_type != AC_WID_PIN)
742                         continue;
743                 pin = snd_array_new(&codec->init_pins);
744                 if (!pin)
745                         return -ENOMEM;
746                 pin->nid = nid;
747                 pin->cfg = snd_hda_codec_read(codec, nid, 0,
748                                               AC_VERB_GET_CONFIG_DEFAULT, 0);
749         }
750         return 0;
751 }
752
753 /* look up the given pin config list and return the item matching with NID */
754 static struct hda_pincfg *look_up_pincfg(struct hda_codec *codec,
755                                          struct snd_array *array,
756                                          hda_nid_t nid)
757 {
758         int i;
759         for (i = 0; i < array->used; i++) {
760                 struct hda_pincfg *pin = snd_array_elem(array, i);
761                 if (pin->nid == nid)
762                         return pin;
763         }
764         return NULL;
765 }
766
767 /* write a config value for the given NID */
768 static void set_pincfg(struct hda_codec *codec, hda_nid_t nid,
769                        unsigned int cfg)
770 {
771         int i;
772         for (i = 0; i < 4; i++) {
773                 snd_hda_codec_write(codec, nid, 0,
774                                     AC_VERB_SET_CONFIG_DEFAULT_BYTES_0 + i,
775                                     cfg & 0xff);
776                 cfg >>= 8;
777         }
778 }
779
780 /* set the current pin config value for the given NID.
781  * the value is cached, and read via snd_hda_codec_get_pincfg()
782  */
783 int snd_hda_add_pincfg(struct hda_codec *codec, struct snd_array *list,
784                        hda_nid_t nid, unsigned int cfg)
785 {
786         struct hda_pincfg *pin;
787         unsigned int oldcfg;
788
789         oldcfg = snd_hda_codec_get_pincfg(codec, nid);
790         pin = look_up_pincfg(codec, list, nid);
791         if (!pin) {
792                 pin = snd_array_new(list);
793                 if (!pin)
794                         return -ENOMEM;
795                 pin->nid = nid;
796         }
797         pin->cfg = cfg;
798
799         /* change only when needed; e.g. if the pincfg is already present
800          * in user_pins[], don't write it
801          */
802         cfg = snd_hda_codec_get_pincfg(codec, nid);
803         if (oldcfg != cfg)
804                 set_pincfg(codec, nid, cfg);
805         return 0;
806 }
807
808 int snd_hda_codec_set_pincfg(struct hda_codec *codec,
809                              hda_nid_t nid, unsigned int cfg)
810 {
811         return snd_hda_add_pincfg(codec, &codec->driver_pins, nid, cfg);
812 }
813 EXPORT_SYMBOL_HDA(snd_hda_codec_set_pincfg);
814
815 /* get the current pin config value of the given pin NID */
816 unsigned int snd_hda_codec_get_pincfg(struct hda_codec *codec, hda_nid_t nid)
817 {
818         struct hda_pincfg *pin;
819
820 #ifdef CONFIG_SND_HDA_HWDEP
821         pin = look_up_pincfg(codec, &codec->user_pins, nid);
822         if (pin)
823                 return pin->cfg;
824 #endif
825         pin = look_up_pincfg(codec, &codec->driver_pins, nid);
826         if (pin)
827                 return pin->cfg;
828         pin = look_up_pincfg(codec, &codec->init_pins, nid);
829         if (pin)
830                 return pin->cfg;
831         return 0;
832 }
833 EXPORT_SYMBOL_HDA(snd_hda_codec_get_pincfg);
834
835 /* restore all current pin configs */
836 static void restore_pincfgs(struct hda_codec *codec)
837 {
838         int i;
839         for (i = 0; i < codec->init_pins.used; i++) {
840                 struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
841                 set_pincfg(codec, pin->nid,
842                            snd_hda_codec_get_pincfg(codec, pin->nid));
843         }
844 }
845
846 static void init_hda_cache(struct hda_cache_rec *cache,
847                            unsigned int record_size);
848 static void free_hda_cache(struct hda_cache_rec *cache);
849
850 /* restore the initial pin cfgs and release all pincfg lists */
851 static void restore_init_pincfgs(struct hda_codec *codec)
852 {
853         /* first free driver_pins and user_pins, then call restore_pincfg
854          * so that only the values in init_pins are restored
855          */
856         snd_array_free(&codec->driver_pins);
857 #ifdef CONFIG_SND_HDA_HWDEP
858         snd_array_free(&codec->user_pins);
859 #endif
860         restore_pincfgs(codec);
861         snd_array_free(&codec->init_pins);
862 }
863
864 /*
865  * codec destructor
866  */
867 static void snd_hda_codec_free(struct hda_codec *codec)
868 {
869         if (!codec)
870                 return;
871         restore_init_pincfgs(codec);
872 #ifdef CONFIG_SND_HDA_POWER_SAVE
873         cancel_delayed_work(&codec->power_work);
874         flush_workqueue(codec->bus->workq);
875 #endif
876         list_del(&codec->list);
877         snd_array_free(&codec->mixers);
878         codec->bus->caddr_tbl[codec->addr] = NULL;
879         if (codec->patch_ops.free)
880                 codec->patch_ops.free(codec);
881         module_put(codec->owner);
882         free_hda_cache(&codec->amp_cache);
883         free_hda_cache(&codec->cmd_cache);
884         kfree(codec->vendor_name);
885         kfree(codec->chip_name);
886         kfree(codec->modelname);
887         kfree(codec->wcaps);
888         kfree(codec);
889 }
890
891 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
892                                 unsigned int power_state);
893
894 /**
895  * snd_hda_codec_new - create a HDA codec
896  * @bus: the bus to assign
897  * @codec_addr: the codec address
898  * @codecp: the pointer to store the generated codec
899  *
900  * Returns 0 if successful, or a negative error code.
901  */
902 int /*__devinit*/ snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
903                                     struct hda_codec **codecp)
904 {
905         struct hda_codec *codec;
906         char component[31];
907         int err;
908
909         if (snd_BUG_ON(!bus))
910                 return -EINVAL;
911         if (snd_BUG_ON(codec_addr > HDA_MAX_CODEC_ADDRESS))
912                 return -EINVAL;
913
914         if (bus->caddr_tbl[codec_addr]) {
915                 snd_printk(KERN_ERR "hda_codec: "
916                            "address 0x%x is already occupied\n", codec_addr);
917                 return -EBUSY;
918         }
919
920         codec = kzalloc(sizeof(*codec), GFP_KERNEL);
921         if (codec == NULL) {
922                 snd_printk(KERN_ERR "can't allocate struct hda_codec\n");
923                 return -ENOMEM;
924         }
925
926         codec->bus = bus;
927         codec->addr = codec_addr;
928         mutex_init(&codec->spdif_mutex);
929         mutex_init(&codec->control_mutex);
930         init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
931         init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
932         snd_array_init(&codec->mixers, sizeof(struct snd_kcontrol *), 32);
933         snd_array_init(&codec->init_pins, sizeof(struct hda_pincfg), 16);
934         snd_array_init(&codec->driver_pins, sizeof(struct hda_pincfg), 16);
935         if (codec->bus->modelname) {
936                 codec->modelname = kstrdup(codec->bus->modelname, GFP_KERNEL);
937                 if (!codec->modelname) {
938                         snd_hda_codec_free(codec);
939                         return -ENODEV;
940                 }
941         }
942
943 #ifdef CONFIG_SND_HDA_POWER_SAVE
944         INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
945         /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
946          * the caller has to power down appropriatley after initialization
947          * phase.
948          */
949         hda_keep_power_on(codec);
950 #endif
951
952         list_add_tail(&codec->list, &bus->codec_list);
953         bus->caddr_tbl[codec_addr] = codec;
954
955         codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
956                                               AC_PAR_VENDOR_ID);
957         if (codec->vendor_id == -1)
958                 /* read again, hopefully the access method was corrected
959                  * in the last read...
960                  */
961                 codec->vendor_id = snd_hda_param_read(codec, AC_NODE_ROOT,
962                                                       AC_PAR_VENDOR_ID);
963         codec->subsystem_id = snd_hda_param_read(codec, AC_NODE_ROOT,
964                                                  AC_PAR_SUBSYSTEM_ID);
965         codec->revision_id = snd_hda_param_read(codec, AC_NODE_ROOT,
966                                                 AC_PAR_REV_ID);
967
968         setup_fg_nodes(codec);
969         if (!codec->afg && !codec->mfg) {
970                 snd_printdd("hda_codec: no AFG or MFG node found\n");
971                 err = -ENODEV;
972                 goto error;
973         }
974
975         err = read_widget_caps(codec, codec->afg ? codec->afg : codec->mfg);
976         if (err < 0) {
977                 snd_printk(KERN_ERR "hda_codec: cannot malloc\n");
978                 goto error;
979         }
980         err = read_pin_defaults(codec);
981         if (err < 0)
982                 goto error;
983
984         if (!codec->subsystem_id) {
985                 hda_nid_t nid = codec->afg ? codec->afg : codec->mfg;
986                 codec->subsystem_id =
987                         snd_hda_codec_read(codec, nid, 0,
988                                            AC_VERB_GET_SUBSYSTEM_ID, 0);
989         }
990
991         /* power-up all before initialization */
992         hda_set_power_state(codec,
993                             codec->afg ? codec->afg : codec->mfg,
994                             AC_PWRST_D0);
995
996         snd_hda_codec_proc_new(codec);
997
998         snd_hda_create_hwdep(codec);
999
1000         sprintf(component, "HDA:%08x,%08x,%08x", codec->vendor_id,
1001                 codec->subsystem_id, codec->revision_id);
1002         snd_component_add(codec->bus->card, component);
1003
1004         if (codecp)
1005                 *codecp = codec;
1006         return 0;
1007
1008  error:
1009         snd_hda_codec_free(codec);
1010         return err;
1011 }
1012 EXPORT_SYMBOL_HDA(snd_hda_codec_new);
1013
1014 int snd_hda_codec_configure(struct hda_codec *codec)
1015 {
1016         int err;
1017
1018         codec->preset = find_codec_preset(codec);
1019         if (!codec->vendor_name || !codec->chip_name) {
1020                 err = get_codec_name(codec);
1021                 if (err < 0)
1022                         return err;
1023         }
1024         /* audio codec should override the mixer name */
1025         if (codec->afg || !*codec->bus->card->mixername)
1026                 snprintf(codec->bus->card->mixername,
1027                          sizeof(codec->bus->card->mixername),
1028                          "%s %s", codec->vendor_name, codec->chip_name);
1029
1030         if (is_generic_config(codec)) {
1031                 err = snd_hda_parse_generic_codec(codec);
1032                 goto patched;
1033         }
1034         if (codec->preset && codec->preset->patch) {
1035                 err = codec->preset->patch(codec);
1036                 goto patched;
1037         }
1038
1039         /* call the default parser */
1040         err = snd_hda_parse_generic_codec(codec);
1041         if (err < 0)
1042                 printk(KERN_ERR "hda-codec: No codec parser is available\n");
1043
1044  patched:
1045         if (!err && codec->patch_ops.unsol_event)
1046                 err = init_unsol_queue(codec->bus);
1047         return err;
1048 }
1049 EXPORT_SYMBOL_HDA(snd_hda_codec_configure);
1050
1051 /**
1052  * snd_hda_codec_setup_stream - set up the codec for streaming
1053  * @codec: the CODEC to set up
1054  * @nid: the NID to set up
1055  * @stream_tag: stream tag to pass, it's between 0x1 and 0xf.
1056  * @channel_id: channel id to pass, zero based.
1057  * @format: stream format.
1058  */
1059 void snd_hda_codec_setup_stream(struct hda_codec *codec, hda_nid_t nid,
1060                                 u32 stream_tag,
1061                                 int channel_id, int format)
1062 {
1063         if (!nid)
1064                 return;
1065
1066         snd_printdd("hda_codec_setup_stream: "
1067                     "NID=0x%x, stream=0x%x, channel=%d, format=0x%x\n",
1068                     nid, stream_tag, channel_id, format);
1069         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID,
1070                             (stream_tag << 4) | channel_id);
1071         msleep(1);
1072         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, format);
1073 }
1074 EXPORT_SYMBOL_HDA(snd_hda_codec_setup_stream);
1075
1076 void snd_hda_codec_cleanup_stream(struct hda_codec *codec, hda_nid_t nid)
1077 {
1078         if (!nid)
1079                 return;
1080
1081         snd_printdd("hda_codec_cleanup_stream: NID=0x%x\n", nid);
1082         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
1083 #if 0 /* keep the format */
1084         msleep(1);
1085         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_STREAM_FORMAT, 0);
1086 #endif
1087 }
1088 EXPORT_SYMBOL_HDA(snd_hda_codec_cleanup_stream);
1089
1090 /*
1091  * amp access functions
1092  */
1093
1094 /* FIXME: more better hash key? */
1095 #define HDA_HASH_KEY(nid,dir,idx) (u32)((nid) + ((idx) << 16) + ((dir) << 24))
1096 #define HDA_HASH_PINCAP_KEY(nid) (u32)((nid) + (0x02 << 24))
1097 #define HDA_HASH_PARPCM_KEY(nid) (u32)((nid) + (0x03 << 24))
1098 #define HDA_HASH_PARSTR_KEY(nid) (u32)((nid) + (0x04 << 24))
1099 #define INFO_AMP_CAPS   (1<<0)
1100 #define INFO_AMP_VOL(ch)        (1 << (1 + (ch)))
1101
1102 /* initialize the hash table */
1103 static void /*__devinit*/ init_hda_cache(struct hda_cache_rec *cache,
1104                                      unsigned int record_size)
1105 {
1106         memset(cache, 0, sizeof(*cache));
1107         memset(cache->hash, 0xff, sizeof(cache->hash));
1108         snd_array_init(&cache->buf, record_size, 64);
1109 }
1110
1111 static void free_hda_cache(struct hda_cache_rec *cache)
1112 {
1113         snd_array_free(&cache->buf);
1114 }
1115
1116 /* query the hash.  allocate an entry if not found. */
1117 static struct hda_cache_head  *get_alloc_hash(struct hda_cache_rec *cache,
1118                                               u32 key)
1119 {
1120         u16 idx = key % (u16)ARRAY_SIZE(cache->hash);
1121         u16 cur = cache->hash[idx];
1122         struct hda_cache_head *info;
1123
1124         while (cur != 0xffff) {
1125                 info = snd_array_elem(&cache->buf, cur);
1126                 if (info->key == key)
1127                         return info;
1128                 cur = info->next;
1129         }
1130
1131         /* add a new hash entry */
1132         info = snd_array_new(&cache->buf);
1133         if (!info)
1134                 return NULL;
1135         cur = snd_array_index(&cache->buf, info);
1136         info->key = key;
1137         info->val = 0;
1138         info->next = cache->hash[idx];
1139         cache->hash[idx] = cur;
1140
1141         return info;
1142 }
1143
1144 /* query and allocate an amp hash entry */
1145 static inline struct hda_amp_info *
1146 get_alloc_amp_hash(struct hda_codec *codec, u32 key)
1147 {
1148         return (struct hda_amp_info *)get_alloc_hash(&codec->amp_cache, key);
1149 }
1150
1151 /*
1152  * query AMP capabilities for the given widget and direction
1153  */
1154 u32 query_amp_caps(struct hda_codec *codec, hda_nid_t nid, int direction)
1155 {
1156         struct hda_amp_info *info;
1157
1158         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, 0));
1159         if (!info)
1160                 return 0;
1161         if (!(info->head.val & INFO_AMP_CAPS)) {
1162                 if (!(get_wcaps(codec, nid) & AC_WCAP_AMP_OVRD))
1163                         nid = codec->afg;
1164                 info->amp_caps = snd_hda_param_read(codec, nid,
1165                                                     direction == HDA_OUTPUT ?
1166                                                     AC_PAR_AMP_OUT_CAP :
1167                                                     AC_PAR_AMP_IN_CAP);
1168                 if (info->amp_caps)
1169                         info->head.val |= INFO_AMP_CAPS;
1170         }
1171         return info->amp_caps;
1172 }
1173 EXPORT_SYMBOL_HDA(query_amp_caps);
1174
1175 int snd_hda_override_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
1176                               unsigned int caps)
1177 {
1178         struct hda_amp_info *info;
1179
1180         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, dir, 0));
1181         if (!info)
1182                 return -EINVAL;
1183         info->amp_caps = caps;
1184         info->head.val |= INFO_AMP_CAPS;
1185         return 0;
1186 }
1187 EXPORT_SYMBOL_HDA(snd_hda_override_amp_caps);
1188
1189 static unsigned int
1190 query_caps_hash(struct hda_codec *codec, hda_nid_t nid, u32 key,
1191                 unsigned int (*func)(struct hda_codec *, hda_nid_t))
1192 {
1193         struct hda_amp_info *info;
1194
1195         info = get_alloc_amp_hash(codec, key);
1196         if (!info)
1197                 return 0;
1198         if (!info->head.val) {
1199                 info->head.val |= INFO_AMP_CAPS;
1200                 info->amp_caps = func(codec, nid);
1201         }
1202         return info->amp_caps;
1203 }
1204
1205 static unsigned int read_pin_cap(struct hda_codec *codec, hda_nid_t nid)
1206 {
1207         return snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
1208 }
1209
1210 u32 snd_hda_query_pin_caps(struct hda_codec *codec, hda_nid_t nid)
1211 {
1212         return query_caps_hash(codec, nid, HDA_HASH_PINCAP_KEY(nid),
1213                                read_pin_cap);
1214 }
1215 EXPORT_SYMBOL_HDA(snd_hda_query_pin_caps);
1216
1217 /*
1218  * read the current volume to info
1219  * if the cache exists, read the cache value.
1220  */
1221 static unsigned int get_vol_mute(struct hda_codec *codec,
1222                                  struct hda_amp_info *info, hda_nid_t nid,
1223                                  int ch, int direction, int index)
1224 {
1225         u32 val, parm;
1226
1227         if (info->head.val & INFO_AMP_VOL(ch))
1228                 return info->vol[ch];
1229
1230         parm = ch ? AC_AMP_GET_RIGHT : AC_AMP_GET_LEFT;
1231         parm |= direction == HDA_OUTPUT ? AC_AMP_GET_OUTPUT : AC_AMP_GET_INPUT;
1232         parm |= index;
1233         val = snd_hda_codec_read(codec, nid, 0,
1234                                  AC_VERB_GET_AMP_GAIN_MUTE, parm);
1235         info->vol[ch] = val & 0xff;
1236         info->head.val |= INFO_AMP_VOL(ch);
1237         return info->vol[ch];
1238 }
1239
1240 /*
1241  * write the current volume in info to the h/w and update the cache
1242  */
1243 static void put_vol_mute(struct hda_codec *codec, struct hda_amp_info *info,
1244                          hda_nid_t nid, int ch, int direction, int index,
1245                          int val)
1246 {
1247         u32 parm;
1248
1249         parm = ch ? AC_AMP_SET_RIGHT : AC_AMP_SET_LEFT;
1250         parm |= direction == HDA_OUTPUT ? AC_AMP_SET_OUTPUT : AC_AMP_SET_INPUT;
1251         parm |= index << AC_AMP_SET_INDEX_SHIFT;
1252         parm |= val;
1253         snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE, parm);
1254         info->vol[ch] = val;
1255 }
1256
1257 /*
1258  * read AMP value.  The volume is between 0 to 0x7f, 0x80 = mute bit.
1259  */
1260 int snd_hda_codec_amp_read(struct hda_codec *codec, hda_nid_t nid, int ch,
1261                            int direction, int index)
1262 {
1263         struct hda_amp_info *info;
1264         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, index));
1265         if (!info)
1266                 return 0;
1267         return get_vol_mute(codec, info, nid, ch, direction, index);
1268 }
1269 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_read);
1270
1271 /*
1272  * update the AMP value, mask = bit mask to set, val = the value
1273  */
1274 int snd_hda_codec_amp_update(struct hda_codec *codec, hda_nid_t nid, int ch,
1275                              int direction, int idx, int mask, int val)
1276 {
1277         struct hda_amp_info *info;
1278
1279         info = get_alloc_amp_hash(codec, HDA_HASH_KEY(nid, direction, idx));
1280         if (!info)
1281                 return 0;
1282         val &= mask;
1283         val |= get_vol_mute(codec, info, nid, ch, direction, idx) & ~mask;
1284         if (info->vol[ch] == val)
1285                 return 0;
1286         put_vol_mute(codec, info, nid, ch, direction, idx, val);
1287         return 1;
1288 }
1289 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_update);
1290
1291 /*
1292  * update the AMP stereo with the same mask and value
1293  */
1294 int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
1295                              int direction, int idx, int mask, int val)
1296 {
1297         int ch, ret = 0;
1298         for (ch = 0; ch < 2; ch++)
1299                 ret |= snd_hda_codec_amp_update(codec, nid, ch, direction,
1300                                                 idx, mask, val);
1301         return ret;
1302 }
1303 EXPORT_SYMBOL_HDA(snd_hda_codec_amp_stereo);
1304
1305 #ifdef SND_HDA_NEEDS_RESUME
1306 /* resume the all amp commands from the cache */
1307 void snd_hda_codec_resume_amp(struct hda_codec *codec)
1308 {
1309         struct hda_amp_info *buffer = codec->amp_cache.buf.list;
1310         int i;
1311
1312         for (i = 0; i < codec->amp_cache.buf.used; i++, buffer++) {
1313                 u32 key = buffer->head.key;
1314                 hda_nid_t nid;
1315                 unsigned int idx, dir, ch;
1316                 if (!key)
1317                         continue;
1318                 nid = key & 0xff;
1319                 idx = (key >> 16) & 0xff;
1320                 dir = (key >> 24) & 0xff;
1321                 for (ch = 0; ch < 2; ch++) {
1322                         if (!(buffer->head.val & INFO_AMP_VOL(ch)))
1323                                 continue;
1324                         put_vol_mute(codec, buffer, nid, ch, dir, idx,
1325                                      buffer->vol[ch]);
1326                 }
1327         }
1328 }
1329 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_amp);
1330 #endif /* SND_HDA_NEEDS_RESUME */
1331
1332 /* volume */
1333 int snd_hda_mixer_amp_volume_info(struct snd_kcontrol *kcontrol,
1334                                   struct snd_ctl_elem_info *uinfo)
1335 {
1336         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1337         u16 nid = get_amp_nid(kcontrol);
1338         u8 chs = get_amp_channels(kcontrol);
1339         int dir = get_amp_direction(kcontrol);
1340         unsigned int ofs = get_amp_offset(kcontrol);
1341         u32 caps;
1342
1343         caps = query_amp_caps(codec, nid, dir);
1344         /* num steps */
1345         caps = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1346         if (!caps) {
1347                 printk(KERN_WARNING "hda_codec: "
1348                        "num_steps = 0 for NID=0x%x (ctl = %s)\n", nid,
1349                        kcontrol->id.name);
1350                 return -EINVAL;
1351         }
1352         if (ofs < caps)
1353                 caps -= ofs;
1354         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1355         uinfo->count = chs == 3 ? 2 : 1;
1356         uinfo->value.integer.min = 0;
1357         uinfo->value.integer.max = caps;
1358         return 0;
1359 }
1360 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_info);
1361
1362
1363 static inline unsigned int
1364 read_amp_value(struct hda_codec *codec, hda_nid_t nid,
1365                int ch, int dir, int idx, unsigned int ofs)
1366 {
1367         unsigned int val;
1368         val = snd_hda_codec_amp_read(codec, nid, ch, dir, idx);
1369         val &= HDA_AMP_VOLMASK;
1370         if (val >= ofs)
1371                 val -= ofs;
1372         else
1373                 val = 0;
1374         return val;
1375 }
1376
1377 static inline int
1378 update_amp_value(struct hda_codec *codec, hda_nid_t nid,
1379                  int ch, int dir, int idx, unsigned int ofs,
1380                  unsigned int val)
1381 {
1382         if (val > 0)
1383                 val += ofs;
1384         return snd_hda_codec_amp_update(codec, nid, ch, dir, idx,
1385                                         HDA_AMP_VOLMASK, val);
1386 }
1387
1388 int snd_hda_mixer_amp_volume_get(struct snd_kcontrol *kcontrol,
1389                                  struct snd_ctl_elem_value *ucontrol)
1390 {
1391         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1392         hda_nid_t nid = get_amp_nid(kcontrol);
1393         int chs = get_amp_channels(kcontrol);
1394         int dir = get_amp_direction(kcontrol);
1395         int idx = get_amp_index(kcontrol);
1396         unsigned int ofs = get_amp_offset(kcontrol);
1397         long *valp = ucontrol->value.integer.value;
1398
1399         if (chs & 1)
1400                 *valp++ = read_amp_value(codec, nid, 0, dir, idx, ofs);
1401         if (chs & 2)
1402                 *valp = read_amp_value(codec, nid, 1, dir, idx, ofs);
1403         return 0;
1404 }
1405 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_get);
1406
1407 int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
1408                                  struct snd_ctl_elem_value *ucontrol)
1409 {
1410         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1411         hda_nid_t nid = get_amp_nid(kcontrol);
1412         int chs = get_amp_channels(kcontrol);
1413         int dir = get_amp_direction(kcontrol);
1414         int idx = get_amp_index(kcontrol);
1415         unsigned int ofs = get_amp_offset(kcontrol);
1416         long *valp = ucontrol->value.integer.value;
1417         int change = 0;
1418
1419         snd_hda_power_up(codec);
1420         if (chs & 1) {
1421                 change = update_amp_value(codec, nid, 0, dir, idx, ofs, *valp);
1422                 valp++;
1423         }
1424         if (chs & 2)
1425                 change |= update_amp_value(codec, nid, 1, dir, idx, ofs, *valp);
1426         snd_hda_power_down(codec);
1427         return change;
1428 }
1429 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_volume_put);
1430
1431 int snd_hda_mixer_amp_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1432                           unsigned int size, unsigned int __user *_tlv)
1433 {
1434         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1435         hda_nid_t nid = get_amp_nid(kcontrol);
1436         int dir = get_amp_direction(kcontrol);
1437         unsigned int ofs = get_amp_offset(kcontrol);
1438         u32 caps, val1, val2;
1439
1440         if (size < 4 * sizeof(unsigned int))
1441                 return -ENOMEM;
1442         caps = query_amp_caps(codec, nid, dir);
1443         val2 = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1444         val2 = (val2 + 1) * 25;
1445         val1 = -((caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT);
1446         val1 += ofs;
1447         val1 = ((int)val1) * ((int)val2);
1448         if (put_user(SNDRV_CTL_TLVT_DB_SCALE, _tlv))
1449                 return -EFAULT;
1450         if (put_user(2 * sizeof(unsigned int), _tlv + 1))
1451                 return -EFAULT;
1452         if (put_user(val1, _tlv + 2))
1453                 return -EFAULT;
1454         if (put_user(val2, _tlv + 3))
1455                 return -EFAULT;
1456         return 0;
1457 }
1458 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_tlv);
1459
1460 /*
1461  * set (static) TLV for virtual master volume; recalculated as max 0dB
1462  */
1463 void snd_hda_set_vmaster_tlv(struct hda_codec *codec, hda_nid_t nid, int dir,
1464                              unsigned int *tlv)
1465 {
1466         u32 caps;
1467         int nums, step;
1468
1469         caps = query_amp_caps(codec, nid, dir);
1470         nums = (caps & AC_AMPCAP_NUM_STEPS) >> AC_AMPCAP_NUM_STEPS_SHIFT;
1471         step = (caps & AC_AMPCAP_STEP_SIZE) >> AC_AMPCAP_STEP_SIZE_SHIFT;
1472         step = (step + 1) * 25;
1473         tlv[0] = SNDRV_CTL_TLVT_DB_SCALE;
1474         tlv[1] = 2 * sizeof(unsigned int);
1475         tlv[2] = -nums * step;
1476         tlv[3] = step;
1477 }
1478 EXPORT_SYMBOL_HDA(snd_hda_set_vmaster_tlv);
1479
1480 /* find a mixer control element with the given name */
1481 static struct snd_kcontrol *
1482 _snd_hda_find_mixer_ctl(struct hda_codec *codec,
1483                         const char *name, int idx)
1484 {
1485         struct snd_ctl_elem_id id;
1486         memset(&id, 0, sizeof(id));
1487         id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1488         id.index = idx;
1489         if (snd_BUG_ON(strlen(name) >= sizeof(id.name)))
1490                 return NULL;
1491         strcpy(id.name, name);
1492         return snd_ctl_find_id(codec->bus->card, &id);
1493 }
1494
1495 struct snd_kcontrol *snd_hda_find_mixer_ctl(struct hda_codec *codec,
1496                                             const char *name)
1497 {
1498         return _snd_hda_find_mixer_ctl(codec, name, 0);
1499 }
1500 EXPORT_SYMBOL_HDA(snd_hda_find_mixer_ctl);
1501
1502 /* Add a control element and assign to the codec */
1503 int snd_hda_ctl_add(struct hda_codec *codec, struct snd_kcontrol *kctl)
1504 {
1505         int err;
1506         struct snd_kcontrol **knewp;
1507
1508         err = snd_ctl_add(codec->bus->card, kctl);
1509         if (err < 0)
1510                 return err;
1511         knewp = snd_array_new(&codec->mixers);
1512         if (!knewp)
1513                 return -ENOMEM;
1514         *knewp = kctl;
1515         return 0;
1516 }
1517 EXPORT_SYMBOL_HDA(snd_hda_ctl_add);
1518
1519 /* Clear all controls assigned to the given codec */
1520 void snd_hda_ctls_clear(struct hda_codec *codec)
1521 {
1522         int i;
1523         struct snd_kcontrol **kctls = codec->mixers.list;
1524         for (i = 0; i < codec->mixers.used; i++)
1525                 snd_ctl_remove(codec->bus->card, kctls[i]);
1526         snd_array_free(&codec->mixers);
1527 }
1528
1529 /* pseudo device locking
1530  * toggle card->shutdown to allow/disallow the device access (as a hack)
1531  */
1532 static int hda_lock_devices(struct snd_card *card)
1533 {
1534         spin_lock(&card->files_lock);
1535         if (card->shutdown) {
1536                 spin_unlock(&card->files_lock);
1537                 return -EINVAL;
1538         }
1539         card->shutdown = 1;
1540         spin_unlock(&card->files_lock);
1541         return 0;
1542 }
1543
1544 static void hda_unlock_devices(struct snd_card *card)
1545 {
1546         spin_lock(&card->files_lock);
1547         card->shutdown = 0;
1548         spin_unlock(&card->files_lock);
1549 }
1550
1551 int snd_hda_codec_reset(struct hda_codec *codec)
1552 {
1553         struct snd_card *card = codec->bus->card;
1554         int i, pcm;
1555
1556         if (hda_lock_devices(card) < 0)
1557                 return -EBUSY;
1558         /* check whether the codec isn't used by any mixer or PCM streams */
1559         if (!list_empty(&card->ctl_files)) {
1560                 hda_unlock_devices(card);
1561                 return -EBUSY;
1562         }
1563         for (pcm = 0; pcm < codec->num_pcms; pcm++) {
1564                 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
1565                 if (!cpcm->pcm)
1566                         continue;
1567                 if (cpcm->pcm->streams[0].substream_opened ||
1568                     cpcm->pcm->streams[1].substream_opened) {
1569                         hda_unlock_devices(card);
1570                         return -EBUSY;
1571                 }
1572         }
1573
1574         /* OK, let it free */
1575
1576 #ifdef CONFIG_SND_HDA_POWER_SAVE
1577         cancel_delayed_work(&codec->power_work);
1578         flush_workqueue(codec->bus->workq);
1579 #endif
1580         snd_hda_ctls_clear(codec);
1581         /* relase PCMs */
1582         for (i = 0; i < codec->num_pcms; i++) {
1583                 if (codec->pcm_info[i].pcm) {
1584                         snd_device_free(card, codec->pcm_info[i].pcm);
1585                         clear_bit(codec->pcm_info[i].device,
1586                                   codec->bus->pcm_dev_bits);
1587                 }
1588         }
1589         if (codec->patch_ops.free)
1590                 codec->patch_ops.free(codec);
1591         codec->proc_widget_hook = NULL;
1592         codec->spec = NULL;
1593         free_hda_cache(&codec->amp_cache);
1594         free_hda_cache(&codec->cmd_cache);
1595         init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
1596         init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
1597         /* free only driver_pins so that init_pins + user_pins are restored */
1598         snd_array_free(&codec->driver_pins);
1599         restore_pincfgs(codec);
1600         codec->num_pcms = 0;
1601         codec->pcm_info = NULL;
1602         codec->preset = NULL;
1603         memset(&codec->patch_ops, 0, sizeof(codec->patch_ops));
1604         codec->slave_dig_outs = NULL;
1605         codec->spdif_status_reset = 0;
1606         module_put(codec->owner);
1607         codec->owner = NULL;
1608
1609         /* allow device access again */
1610         hda_unlock_devices(card);
1611         return 0;
1612 }
1613
1614 /* create a virtual master control and add slaves */
1615 int snd_hda_add_vmaster(struct hda_codec *codec, char *name,
1616                         unsigned int *tlv, const char **slaves)
1617 {
1618         struct snd_kcontrol *kctl;
1619         const char **s;
1620         int err;
1621
1622         for (s = slaves; *s && !snd_hda_find_mixer_ctl(codec, *s); s++)
1623                 ;
1624         if (!*s) {
1625                 snd_printdd("No slave found for %s\n", name);
1626                 return 0;
1627         }
1628         kctl = snd_ctl_make_virtual_master(name, tlv);
1629         if (!kctl)
1630                 return -ENOMEM;
1631         err = snd_hda_ctl_add(codec, kctl);
1632         if (err < 0)
1633                 return err;
1634         
1635         for (s = slaves; *s; s++) {
1636                 struct snd_kcontrol *sctl;
1637                 int i = 0;
1638                 for (;;) {
1639                         sctl = _snd_hda_find_mixer_ctl(codec, *s, i);
1640                         if (!sctl) {
1641                                 if (!i)
1642                                         snd_printdd("Cannot find slave %s, "
1643                                                     "skipped\n", *s);
1644                                 break;
1645                         }
1646                         err = snd_ctl_add_slave(kctl, sctl);
1647                         if (err < 0)
1648                                 return err;
1649                         i++;
1650                 }
1651         }
1652         return 0;
1653 }
1654 EXPORT_SYMBOL_HDA(snd_hda_add_vmaster);
1655
1656 /* switch */
1657 int snd_hda_mixer_amp_switch_info(struct snd_kcontrol *kcontrol,
1658                                   struct snd_ctl_elem_info *uinfo)
1659 {
1660         int chs = get_amp_channels(kcontrol);
1661
1662         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1663         uinfo->count = chs == 3 ? 2 : 1;
1664         uinfo->value.integer.min = 0;
1665         uinfo->value.integer.max = 1;
1666         return 0;
1667 }
1668 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_info);
1669
1670 int snd_hda_mixer_amp_switch_get(struct snd_kcontrol *kcontrol,
1671                                  struct snd_ctl_elem_value *ucontrol)
1672 {
1673         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1674         hda_nid_t nid = get_amp_nid(kcontrol);
1675         int chs = get_amp_channels(kcontrol);
1676         int dir = get_amp_direction(kcontrol);
1677         int idx = get_amp_index(kcontrol);
1678         long *valp = ucontrol->value.integer.value;
1679
1680         if (chs & 1)
1681                 *valp++ = (snd_hda_codec_amp_read(codec, nid, 0, dir, idx) &
1682                            HDA_AMP_MUTE) ? 0 : 1;
1683         if (chs & 2)
1684                 *valp = (snd_hda_codec_amp_read(codec, nid, 1, dir, idx) &
1685                          HDA_AMP_MUTE) ? 0 : 1;
1686         return 0;
1687 }
1688 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_get);
1689
1690 int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
1691                                  struct snd_ctl_elem_value *ucontrol)
1692 {
1693         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1694         hda_nid_t nid = get_amp_nid(kcontrol);
1695         int chs = get_amp_channels(kcontrol);
1696         int dir = get_amp_direction(kcontrol);
1697         int idx = get_amp_index(kcontrol);
1698         long *valp = ucontrol->value.integer.value;
1699         int change = 0;
1700
1701         snd_hda_power_up(codec);
1702         if (chs & 1) {
1703                 change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
1704                                                   HDA_AMP_MUTE,
1705                                                   *valp ? 0 : HDA_AMP_MUTE);
1706                 valp++;
1707         }
1708         if (chs & 2)
1709                 change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
1710                                                    HDA_AMP_MUTE,
1711                                                    *valp ? 0 : HDA_AMP_MUTE);
1712 #ifdef CONFIG_SND_HDA_POWER_SAVE
1713         if (codec->patch_ops.check_power_status)
1714                 codec->patch_ops.check_power_status(codec, nid);
1715 #endif
1716         snd_hda_power_down(codec);
1717         return change;
1718 }
1719 EXPORT_SYMBOL_HDA(snd_hda_mixer_amp_switch_put);
1720
1721 /*
1722  * bound volume controls
1723  *
1724  * bind multiple volumes (# indices, from 0)
1725  */
1726
1727 #define AMP_VAL_IDX_SHIFT       19
1728 #define AMP_VAL_IDX_MASK        (0x0f<<19)
1729
1730 int snd_hda_mixer_bind_switch_get(struct snd_kcontrol *kcontrol,
1731                                   struct snd_ctl_elem_value *ucontrol)
1732 {
1733         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1734         unsigned long pval;
1735         int err;
1736
1737         mutex_lock(&codec->control_mutex);
1738         pval = kcontrol->private_value;
1739         kcontrol->private_value = pval & ~AMP_VAL_IDX_MASK; /* index 0 */
1740         err = snd_hda_mixer_amp_switch_get(kcontrol, ucontrol);
1741         kcontrol->private_value = pval;
1742         mutex_unlock(&codec->control_mutex);
1743         return err;
1744 }
1745 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_get);
1746
1747 int snd_hda_mixer_bind_switch_put(struct snd_kcontrol *kcontrol,
1748                                   struct snd_ctl_elem_value *ucontrol)
1749 {
1750         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1751         unsigned long pval;
1752         int i, indices, err = 0, change = 0;
1753
1754         mutex_lock(&codec->control_mutex);
1755         pval = kcontrol->private_value;
1756         indices = (pval & AMP_VAL_IDX_MASK) >> AMP_VAL_IDX_SHIFT;
1757         for (i = 0; i < indices; i++) {
1758                 kcontrol->private_value = (pval & ~AMP_VAL_IDX_MASK) |
1759                         (i << AMP_VAL_IDX_SHIFT);
1760                 err = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
1761                 if (err < 0)
1762                         break;
1763                 change |= err;
1764         }
1765         kcontrol->private_value = pval;
1766         mutex_unlock(&codec->control_mutex);
1767         return err < 0 ? err : change;
1768 }
1769 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_switch_put);
1770
1771 /*
1772  * generic bound volume/swtich controls
1773  */
1774 int snd_hda_mixer_bind_ctls_info(struct snd_kcontrol *kcontrol,
1775                                  struct snd_ctl_elem_info *uinfo)
1776 {
1777         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1778         struct hda_bind_ctls *c;
1779         int err;
1780
1781         mutex_lock(&codec->control_mutex);
1782         c = (struct hda_bind_ctls *)kcontrol->private_value;
1783         kcontrol->private_value = *c->values;
1784         err = c->ops->info(kcontrol, uinfo);
1785         kcontrol->private_value = (long)c;
1786         mutex_unlock(&codec->control_mutex);
1787         return err;
1788 }
1789 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_info);
1790
1791 int snd_hda_mixer_bind_ctls_get(struct snd_kcontrol *kcontrol,
1792                                 struct snd_ctl_elem_value *ucontrol)
1793 {
1794         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1795         struct hda_bind_ctls *c;
1796         int err;
1797
1798         mutex_lock(&codec->control_mutex);
1799         c = (struct hda_bind_ctls *)kcontrol->private_value;
1800         kcontrol->private_value = *c->values;
1801         err = c->ops->get(kcontrol, ucontrol);
1802         kcontrol->private_value = (long)c;
1803         mutex_unlock(&codec->control_mutex);
1804         return err;
1805 }
1806 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_get);
1807
1808 int snd_hda_mixer_bind_ctls_put(struct snd_kcontrol *kcontrol,
1809                                 struct snd_ctl_elem_value *ucontrol)
1810 {
1811         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1812         struct hda_bind_ctls *c;
1813         unsigned long *vals;
1814         int err = 0, change = 0;
1815
1816         mutex_lock(&codec->control_mutex);
1817         c = (struct hda_bind_ctls *)kcontrol->private_value;
1818         for (vals = c->values; *vals; vals++) {
1819                 kcontrol->private_value = *vals;
1820                 err = c->ops->put(kcontrol, ucontrol);
1821                 if (err < 0)
1822                         break;
1823                 change |= err;
1824         }
1825         kcontrol->private_value = (long)c;
1826         mutex_unlock(&codec->control_mutex);
1827         return err < 0 ? err : change;
1828 }
1829 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_ctls_put);
1830
1831 int snd_hda_mixer_bind_tlv(struct snd_kcontrol *kcontrol, int op_flag,
1832                            unsigned int size, unsigned int __user *tlv)
1833 {
1834         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1835         struct hda_bind_ctls *c;
1836         int err;
1837
1838         mutex_lock(&codec->control_mutex);
1839         c = (struct hda_bind_ctls *)kcontrol->private_value;
1840         kcontrol->private_value = *c->values;
1841         err = c->ops->tlv(kcontrol, op_flag, size, tlv);
1842         kcontrol->private_value = (long)c;
1843         mutex_unlock(&codec->control_mutex);
1844         return err;
1845 }
1846 EXPORT_SYMBOL_HDA(snd_hda_mixer_bind_tlv);
1847
1848 struct hda_ctl_ops snd_hda_bind_vol = {
1849         .info = snd_hda_mixer_amp_volume_info,
1850         .get = snd_hda_mixer_amp_volume_get,
1851         .put = snd_hda_mixer_amp_volume_put,
1852         .tlv = snd_hda_mixer_amp_tlv
1853 };
1854 EXPORT_SYMBOL_HDA(snd_hda_bind_vol);
1855
1856 struct hda_ctl_ops snd_hda_bind_sw = {
1857         .info = snd_hda_mixer_amp_switch_info,
1858         .get = snd_hda_mixer_amp_switch_get,
1859         .put = snd_hda_mixer_amp_switch_put,
1860         .tlv = snd_hda_mixer_amp_tlv
1861 };
1862 EXPORT_SYMBOL_HDA(snd_hda_bind_sw);
1863
1864 /*
1865  * SPDIF out controls
1866  */
1867
1868 static int snd_hda_spdif_mask_info(struct snd_kcontrol *kcontrol,
1869                                    struct snd_ctl_elem_info *uinfo)
1870 {
1871         uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1872         uinfo->count = 1;
1873         return 0;
1874 }
1875
1876 static int snd_hda_spdif_cmask_get(struct snd_kcontrol *kcontrol,
1877                                    struct snd_ctl_elem_value *ucontrol)
1878 {
1879         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1880                                            IEC958_AES0_NONAUDIO |
1881                                            IEC958_AES0_CON_EMPHASIS_5015 |
1882                                            IEC958_AES0_CON_NOT_COPYRIGHT;
1883         ucontrol->value.iec958.status[1] = IEC958_AES1_CON_CATEGORY |
1884                                            IEC958_AES1_CON_ORIGINAL;
1885         return 0;
1886 }
1887
1888 static int snd_hda_spdif_pmask_get(struct snd_kcontrol *kcontrol,
1889                                    struct snd_ctl_elem_value *ucontrol)
1890 {
1891         ucontrol->value.iec958.status[0] = IEC958_AES0_PROFESSIONAL |
1892                                            IEC958_AES0_NONAUDIO |
1893                                            IEC958_AES0_PRO_EMPHASIS_5015;
1894         return 0;
1895 }
1896
1897 static int snd_hda_spdif_default_get(struct snd_kcontrol *kcontrol,
1898                                      struct snd_ctl_elem_value *ucontrol)
1899 {
1900         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1901
1902         ucontrol->value.iec958.status[0] = codec->spdif_status & 0xff;
1903         ucontrol->value.iec958.status[1] = (codec->spdif_status >> 8) & 0xff;
1904         ucontrol->value.iec958.status[2] = (codec->spdif_status >> 16) & 0xff;
1905         ucontrol->value.iec958.status[3] = (codec->spdif_status >> 24) & 0xff;
1906
1907         return 0;
1908 }
1909
1910 /* convert from SPDIF status bits to HDA SPDIF bits
1911  * bit 0 (DigEn) is always set zero (to be filled later)
1912  */
1913 static unsigned short convert_from_spdif_status(unsigned int sbits)
1914 {
1915         unsigned short val = 0;
1916
1917         if (sbits & IEC958_AES0_PROFESSIONAL)
1918                 val |= AC_DIG1_PROFESSIONAL;
1919         if (sbits & IEC958_AES0_NONAUDIO)
1920                 val |= AC_DIG1_NONAUDIO;
1921         if (sbits & IEC958_AES0_PROFESSIONAL) {
1922                 if ((sbits & IEC958_AES0_PRO_EMPHASIS) ==
1923                     IEC958_AES0_PRO_EMPHASIS_5015)
1924                         val |= AC_DIG1_EMPHASIS;
1925         } else {
1926                 if ((sbits & IEC958_AES0_CON_EMPHASIS) ==
1927                     IEC958_AES0_CON_EMPHASIS_5015)
1928                         val |= AC_DIG1_EMPHASIS;
1929                 if (!(sbits & IEC958_AES0_CON_NOT_COPYRIGHT))
1930                         val |= AC_DIG1_COPYRIGHT;
1931                 if (sbits & (IEC958_AES1_CON_ORIGINAL << 8))
1932                         val |= AC_DIG1_LEVEL;
1933                 val |= sbits & (IEC958_AES1_CON_CATEGORY << 8);
1934         }
1935         return val;
1936 }
1937
1938 /* convert to SPDIF status bits from HDA SPDIF bits
1939  */
1940 static unsigned int convert_to_spdif_status(unsigned short val)
1941 {
1942         unsigned int sbits = 0;
1943
1944         if (val & AC_DIG1_NONAUDIO)
1945                 sbits |= IEC958_AES0_NONAUDIO;
1946         if (val & AC_DIG1_PROFESSIONAL)
1947                 sbits |= IEC958_AES0_PROFESSIONAL;
1948         if (sbits & IEC958_AES0_PROFESSIONAL) {
1949                 if (sbits & AC_DIG1_EMPHASIS)
1950                         sbits |= IEC958_AES0_PRO_EMPHASIS_5015;
1951         } else {
1952                 if (val & AC_DIG1_EMPHASIS)
1953                         sbits |= IEC958_AES0_CON_EMPHASIS_5015;
1954                 if (!(val & AC_DIG1_COPYRIGHT))
1955                         sbits |= IEC958_AES0_CON_NOT_COPYRIGHT;
1956                 if (val & AC_DIG1_LEVEL)
1957                         sbits |= (IEC958_AES1_CON_ORIGINAL << 8);
1958                 sbits |= val & (0x7f << 8);
1959         }
1960         return sbits;
1961 }
1962
1963 /* set digital convert verbs both for the given NID and its slaves */
1964 static void set_dig_out(struct hda_codec *codec, hda_nid_t nid,
1965                         int verb, int val)
1966 {
1967         hda_nid_t *d;
1968
1969         snd_hda_codec_write_cache(codec, nid, 0, verb, val);
1970         d = codec->slave_dig_outs;
1971         if (!d)
1972                 return;
1973         for (; *d; d++)
1974                 snd_hda_codec_write_cache(codec, *d, 0, verb, val);
1975 }
1976
1977 static inline void set_dig_out_convert(struct hda_codec *codec, hda_nid_t nid,
1978                                        int dig1, int dig2)
1979 {
1980         if (dig1 != -1)
1981                 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_1, dig1);
1982         if (dig2 != -1)
1983                 set_dig_out(codec, nid, AC_VERB_SET_DIGI_CONVERT_2, dig2);
1984 }
1985
1986 static int snd_hda_spdif_default_put(struct snd_kcontrol *kcontrol,
1987                                      struct snd_ctl_elem_value *ucontrol)
1988 {
1989         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
1990         hda_nid_t nid = kcontrol->private_value;
1991         unsigned short val;
1992         int change;
1993
1994         mutex_lock(&codec->spdif_mutex);
1995         codec->spdif_status = ucontrol->value.iec958.status[0] |
1996                 ((unsigned int)ucontrol->value.iec958.status[1] << 8) |
1997                 ((unsigned int)ucontrol->value.iec958.status[2] << 16) |
1998                 ((unsigned int)ucontrol->value.iec958.status[3] << 24);
1999         val = convert_from_spdif_status(codec->spdif_status);
2000         val |= codec->spdif_ctls & 1;
2001         change = codec->spdif_ctls != val;
2002         codec->spdif_ctls = val;
2003
2004         if (change)
2005                 set_dig_out_convert(codec, nid, val & 0xff, (val >> 8) & 0xff);
2006
2007         mutex_unlock(&codec->spdif_mutex);
2008         return change;
2009 }
2010
2011 #define snd_hda_spdif_out_switch_info   snd_ctl_boolean_mono_info
2012
2013 static int snd_hda_spdif_out_switch_get(struct snd_kcontrol *kcontrol,
2014                                         struct snd_ctl_elem_value *ucontrol)
2015 {
2016         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2017
2018         ucontrol->value.integer.value[0] = codec->spdif_ctls & AC_DIG1_ENABLE;
2019         return 0;
2020 }
2021
2022 static int snd_hda_spdif_out_switch_put(struct snd_kcontrol *kcontrol,
2023                                         struct snd_ctl_elem_value *ucontrol)
2024 {
2025         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2026         hda_nid_t nid = kcontrol->private_value;
2027         unsigned short val;
2028         int change;
2029
2030         mutex_lock(&codec->spdif_mutex);
2031         val = codec->spdif_ctls & ~AC_DIG1_ENABLE;
2032         if (ucontrol->value.integer.value[0])
2033                 val |= AC_DIG1_ENABLE;
2034         change = codec->spdif_ctls != val;
2035         if (change) {
2036                 codec->spdif_ctls = val;
2037                 set_dig_out_convert(codec, nid, val & 0xff, -1);
2038                 /* unmute amp switch (if any) */
2039                 if ((get_wcaps(codec, nid) & AC_WCAP_OUT_AMP) &&
2040                     (val & AC_DIG1_ENABLE))
2041                         snd_hda_codec_amp_stereo(codec, nid, HDA_OUTPUT, 0,
2042                                                  HDA_AMP_MUTE, 0);
2043         }
2044         mutex_unlock(&codec->spdif_mutex);
2045         return change;
2046 }
2047
2048 static struct snd_kcontrol_new dig_mixes[] = {
2049         {
2050                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2051                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2052                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
2053                 .info = snd_hda_spdif_mask_info,
2054                 .get = snd_hda_spdif_cmask_get,
2055         },
2056         {
2057                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2058                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2059                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,PRO_MASK),
2060                 .info = snd_hda_spdif_mask_info,
2061                 .get = snd_hda_spdif_pmask_get,
2062         },
2063         {
2064                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2065                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
2066                 .info = snd_hda_spdif_mask_info,
2067                 .get = snd_hda_spdif_default_get,
2068                 .put = snd_hda_spdif_default_put,
2069         },
2070         {
2071                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2072                 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,SWITCH),
2073                 .info = snd_hda_spdif_out_switch_info,
2074                 .get = snd_hda_spdif_out_switch_get,
2075                 .put = snd_hda_spdif_out_switch_put,
2076         },
2077         { } /* end */
2078 };
2079
2080 #define SPDIF_MAX_IDX   4       /* 4 instances should be enough to probe */
2081
2082 /**
2083  * snd_hda_create_spdif_out_ctls - create Output SPDIF-related controls
2084  * @codec: the HDA codec
2085  * @nid: audio out widget NID
2086  *
2087  * Creates controls related with the SPDIF output.
2088  * Called from each patch supporting the SPDIF out.
2089  *
2090  * Returns 0 if successful, or a negative error code.
2091  */
2092 int snd_hda_create_spdif_out_ctls(struct hda_codec *codec, hda_nid_t nid)
2093 {
2094         int err;
2095         struct snd_kcontrol *kctl;
2096         struct snd_kcontrol_new *dig_mix;
2097         int idx;
2098
2099         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2100                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Playback Switch",
2101                                              idx))
2102                         break;
2103         }
2104         if (idx >= SPDIF_MAX_IDX) {
2105                 printk(KERN_ERR "hda_codec: too many IEC958 outputs\n");
2106                 return -EBUSY;
2107         }
2108         for (dig_mix = dig_mixes; dig_mix->name; dig_mix++) {
2109                 kctl = snd_ctl_new1(dig_mix, codec);
2110                 if (!kctl)
2111                         return -ENOMEM;
2112                 kctl->id.index = idx;
2113                 kctl->private_value = nid;
2114                 err = snd_hda_ctl_add(codec, kctl);
2115                 if (err < 0)
2116                         return err;
2117         }
2118         codec->spdif_ctls =
2119                 snd_hda_codec_read(codec, nid, 0,
2120                                    AC_VERB_GET_DIGI_CONVERT_1, 0);
2121         codec->spdif_status = convert_to_spdif_status(codec->spdif_ctls);
2122         return 0;
2123 }
2124 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_out_ctls);
2125
2126 /*
2127  * SPDIF sharing with analog output
2128  */
2129 static int spdif_share_sw_get(struct snd_kcontrol *kcontrol,
2130                               struct snd_ctl_elem_value *ucontrol)
2131 {
2132         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2133         ucontrol->value.integer.value[0] = mout->share_spdif;
2134         return 0;
2135 }
2136
2137 static int spdif_share_sw_put(struct snd_kcontrol *kcontrol,
2138                               struct snd_ctl_elem_value *ucontrol)
2139 {
2140         struct hda_multi_out *mout = snd_kcontrol_chip(kcontrol);
2141         mout->share_spdif = !!ucontrol->value.integer.value[0];
2142         return 0;
2143 }
2144
2145 static struct snd_kcontrol_new spdif_share_sw = {
2146         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2147         .name = "IEC958 Default PCM Playback Switch",
2148         .info = snd_ctl_boolean_mono_info,
2149         .get = spdif_share_sw_get,
2150         .put = spdif_share_sw_put,
2151 };
2152
2153 int snd_hda_create_spdif_share_sw(struct hda_codec *codec,
2154                                   struct hda_multi_out *mout)
2155 {
2156         if (!mout->dig_out_nid)
2157                 return 0;
2158         /* ATTENTION: here mout is passed as private_data, instead of codec */
2159         return snd_hda_ctl_add(codec,
2160                            snd_ctl_new1(&spdif_share_sw, mout));
2161 }
2162 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_share_sw);
2163
2164 /*
2165  * SPDIF input
2166  */
2167
2168 #define snd_hda_spdif_in_switch_info    snd_hda_spdif_out_switch_info
2169
2170 static int snd_hda_spdif_in_switch_get(struct snd_kcontrol *kcontrol,
2171                                        struct snd_ctl_elem_value *ucontrol)
2172 {
2173         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2174
2175         ucontrol->value.integer.value[0] = codec->spdif_in_enable;
2176         return 0;
2177 }
2178
2179 static int snd_hda_spdif_in_switch_put(struct snd_kcontrol *kcontrol,
2180                                        struct snd_ctl_elem_value *ucontrol)
2181 {
2182         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2183         hda_nid_t nid = kcontrol->private_value;
2184         unsigned int val = !!ucontrol->value.integer.value[0];
2185         int change;
2186
2187         mutex_lock(&codec->spdif_mutex);
2188         change = codec->spdif_in_enable != val;
2189         if (change) {
2190                 codec->spdif_in_enable = val;
2191                 snd_hda_codec_write_cache(codec, nid, 0,
2192                                           AC_VERB_SET_DIGI_CONVERT_1, val);
2193         }
2194         mutex_unlock(&codec->spdif_mutex);
2195         return change;
2196 }
2197
2198 static int snd_hda_spdif_in_status_get(struct snd_kcontrol *kcontrol,
2199                                        struct snd_ctl_elem_value *ucontrol)
2200 {
2201         struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
2202         hda_nid_t nid = kcontrol->private_value;
2203         unsigned short val;
2204         unsigned int sbits;
2205
2206         val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_DIGI_CONVERT_1, 0);
2207         sbits = convert_to_spdif_status(val);
2208         ucontrol->value.iec958.status[0] = sbits;
2209         ucontrol->value.iec958.status[1] = sbits >> 8;
2210         ucontrol->value.iec958.status[2] = sbits >> 16;
2211         ucontrol->value.iec958.status[3] = sbits >> 24;
2212         return 0;
2213 }
2214
2215 static struct snd_kcontrol_new dig_in_ctls[] = {
2216         {
2217                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2218                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,SWITCH),
2219                 .info = snd_hda_spdif_in_switch_info,
2220                 .get = snd_hda_spdif_in_switch_get,
2221                 .put = snd_hda_spdif_in_switch_put,
2222         },
2223         {
2224                 .access = SNDRV_CTL_ELEM_ACCESS_READ,
2225                 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
2226                 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
2227                 .info = snd_hda_spdif_mask_info,
2228                 .get = snd_hda_spdif_in_status_get,
2229         },
2230         { } /* end */
2231 };
2232
2233 /**
2234  * snd_hda_create_spdif_in_ctls - create Input SPDIF-related controls
2235  * @codec: the HDA codec
2236  * @nid: audio in widget NID
2237  *
2238  * Creates controls related with the SPDIF input.
2239  * Called from each patch supporting the SPDIF in.
2240  *
2241  * Returns 0 if successful, or a negative error code.
2242  */
2243 int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
2244 {
2245         int err;
2246         struct snd_kcontrol *kctl;
2247         struct snd_kcontrol_new *dig_mix;
2248         int idx;
2249
2250         for (idx = 0; idx < SPDIF_MAX_IDX; idx++) {
2251                 if (!_snd_hda_find_mixer_ctl(codec, "IEC958 Capture Switch",
2252                                              idx))
2253                         break;
2254         }
2255         if (idx >= SPDIF_MAX_IDX) {
2256                 printk(KERN_ERR "hda_codec: too many IEC958 inputs\n");
2257                 return -EBUSY;
2258         }
2259         for (dig_mix = dig_in_ctls; dig_mix->name; dig_mix++) {
2260                 kctl = snd_ctl_new1(dig_mix, codec);
2261                 if (!kctl)
2262                         return -ENOMEM;
2263                 kctl->private_value = nid;
2264                 err = snd_hda_ctl_add(codec, kctl);
2265                 if (err < 0)
2266                         return err;
2267         }
2268         codec->spdif_in_enable =
2269                 snd_hda_codec_read(codec, nid, 0,
2270                                    AC_VERB_GET_DIGI_CONVERT_1, 0) &
2271                 AC_DIG1_ENABLE;
2272         return 0;
2273 }
2274 EXPORT_SYMBOL_HDA(snd_hda_create_spdif_in_ctls);
2275
2276 #ifdef SND_HDA_NEEDS_RESUME
2277 /*
2278  * command cache
2279  */
2280
2281 /* build a 32bit cache key with the widget id and the command parameter */
2282 #define build_cmd_cache_key(nid, verb)  ((verb << 8) | nid)
2283 #define get_cmd_cache_nid(key)          ((key) & 0xff)
2284 #define get_cmd_cache_cmd(key)          (((key) >> 8) & 0xffff)
2285
2286 /**
2287  * snd_hda_codec_write_cache - send a single command with caching
2288  * @codec: the HDA codec
2289  * @nid: NID to send the command
2290  * @direct: direct flag
2291  * @verb: the verb to send
2292  * @parm: the parameter for the verb
2293  *
2294  * Send a single command without waiting for response.
2295  *
2296  * Returns 0 if successful, or a negative error code.
2297  */
2298 int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
2299                               int direct, unsigned int verb, unsigned int parm)
2300 {
2301         int err = snd_hda_codec_write(codec, nid, direct, verb, parm);
2302         struct hda_cache_head *c;
2303         u32 key;
2304
2305         if (err < 0)
2306                 return err;
2307         /* parm may contain the verb stuff for get/set amp */
2308         verb = verb | (parm >> 8);
2309         parm &= 0xff;
2310         key = build_cmd_cache_key(nid, verb);
2311         mutex_lock(&codec->bus->cmd_mutex);
2312         c = get_alloc_hash(&codec->cmd_cache, key);
2313         if (c)
2314                 c->val = parm;
2315         mutex_unlock(&codec->bus->cmd_mutex);
2316         return 0;
2317 }
2318 EXPORT_SYMBOL_HDA(snd_hda_codec_write_cache);
2319
2320 /* resume the all commands from the cache */
2321 void snd_hda_codec_resume_cache(struct hda_codec *codec)
2322 {
2323         struct hda_cache_head *buffer = codec->cmd_cache.buf.list;
2324         int i;
2325
2326         for (i = 0; i < codec->cmd_cache.buf.used; i++, buffer++) {
2327                 u32 key = buffer->key;
2328                 if (!key)
2329                         continue;
2330                 snd_hda_codec_write(codec, get_cmd_cache_nid(key), 0,
2331                                     get_cmd_cache_cmd(key), buffer->val);
2332         }
2333 }
2334 EXPORT_SYMBOL_HDA(snd_hda_codec_resume_cache);
2335
2336 /**
2337  * snd_hda_sequence_write_cache - sequence writes with caching
2338  * @codec: the HDA codec
2339  * @seq: VERB array to send
2340  *
2341  * Send the commands sequentially from the given array.
2342  * Thte commands are recorded on cache for power-save and resume.
2343  * The array must be terminated with NID=0.
2344  */
2345 void snd_hda_sequence_write_cache(struct hda_codec *codec,
2346                                   const struct hda_verb *seq)
2347 {
2348         for (; seq->nid; seq++)
2349                 snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
2350                                           seq->param);
2351 }
2352 EXPORT_SYMBOL_HDA(snd_hda_sequence_write_cache);
2353 #endif /* SND_HDA_NEEDS_RESUME */
2354
2355 /*
2356  * set power state of the codec
2357  */
2358 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
2359                                 unsigned int power_state)
2360 {
2361         hda_nid_t nid;
2362         int i;
2363
2364         snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
2365                             power_state);
2366         msleep(10); /* partial workaround for "azx_get_response timeout" */
2367
2368         nid = codec->start_nid;
2369         for (i = 0; i < codec->num_nodes; i++, nid++) {
2370                 unsigned int wcaps = get_wcaps(codec, nid);
2371                 if (wcaps & AC_WCAP_POWER) {
2372                         unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
2373                                 AC_WCAP_TYPE_SHIFT;
2374                         if (power_state == AC_PWRST_D3 &&
2375                             wid_type == AC_WID_PIN) {
2376                                 unsigned int pincap;
2377                                 /*
2378                                  * don't power down the widget if it controls
2379                                  * eapd and EAPD_BTLENABLE is set.
2380                                  */
2381                                 pincap = snd_hda_query_pin_caps(codec, nid);
2382                                 if (pincap & AC_PINCAP_EAPD) {
2383                                         int eapd = snd_hda_codec_read(codec,
2384                                                 nid, 0,
2385                                                 AC_VERB_GET_EAPD_BTLENABLE, 0);
2386                                         eapd &= 0x02;
2387                                         if (eapd)
2388                                                 continue;
2389                                 }
2390                         }
2391                         snd_hda_codec_write(codec, nid, 0,
2392                                             AC_VERB_SET_POWER_STATE,
2393                                             power_state);
2394                 }
2395         }
2396
2397         if (power_state == AC_PWRST_D0) {
2398                 unsigned long end_time;
2399                 int state;
2400                 msleep(10);
2401                 /* wait until the codec reachs to D0 */
2402                 end_time = jiffies + msecs_to_jiffies(500);
2403                 do {
2404                         state = snd_hda_codec_read(codec, fg, 0,
2405                                                    AC_VERB_GET_POWER_STATE, 0);
2406                         if (state == power_state)
2407                                 break;
2408                         msleep(1);
2409                 } while (time_after_eq(end_time, jiffies));
2410         }
2411 }
2412
2413 #ifdef CONFIG_SND_HDA_HWDEP
2414 /* execute additional init verbs */
2415 static void hda_exec_init_verbs(struct hda_codec *codec)
2416 {
2417         if (codec->init_verbs.list)
2418                 snd_hda_sequence_write(codec, codec->init_verbs.list);
2419 }
2420 #else
2421 static inline void hda_exec_init_verbs(struct hda_codec *codec) {}
2422 #endif
2423
2424 #ifdef SND_HDA_NEEDS_RESUME
2425 /*
2426  * call suspend and power-down; used both from PM and power-save
2427  */
2428 static void hda_call_codec_suspend(struct hda_codec *codec)
2429 {
2430         if (codec->patch_ops.suspend)
2431                 codec->patch_ops.suspend(codec, PMSG_SUSPEND);
2432         hda_set_power_state(codec,
2433                             codec->afg ? codec->afg : codec->mfg,
2434                             AC_PWRST_D3);
2435 #ifdef CONFIG_SND_HDA_POWER_SAVE
2436         cancel_delayed_work(&codec->power_work);
2437         codec->power_on = 0;
2438         codec->power_transition = 0;
2439 #endif
2440 }
2441
2442 /*
2443  * kick up codec; used both from PM and power-save
2444  */
2445 static void hda_call_codec_resume(struct hda_codec *codec)
2446 {
2447         hda_set_power_state(codec,
2448                             codec->afg ? codec->afg : codec->mfg,
2449                             AC_PWRST_D0);
2450         restore_pincfgs(codec); /* restore all current pin configs */
2451         hda_exec_init_verbs(codec);
2452         if (codec->patch_ops.resume)
2453                 codec->patch_ops.resume(codec);
2454         else {
2455                 if (codec->patch_ops.init)
2456                         codec->patch_ops.init(codec);
2457                 snd_hda_codec_resume_amp(codec);
2458                 snd_hda_codec_resume_cache(codec);
2459         }
2460 }
2461 #endif /* SND_HDA_NEEDS_RESUME */
2462
2463
2464 /**
2465  * snd_hda_build_controls - build mixer controls
2466  * @bus: the BUS
2467  *
2468  * Creates mixer controls for each codec included in the bus.
2469  *
2470  * Returns 0 if successful, otherwise a negative error code.
2471  */
2472 int /*__devinit*/ snd_hda_build_controls(struct hda_bus *bus)
2473 {
2474         struct hda_codec *codec;
2475
2476         list_for_each_entry(codec, &bus->codec_list, list) {
2477                 int err = snd_hda_codec_build_controls(codec);
2478                 if (err < 0) {
2479                         printk(KERN_ERR "hda_codec: cannot build controls"
2480                                "for #%d (error %d)\n", codec->addr, err); 
2481                         err = snd_hda_codec_reset(codec);
2482                         if (err < 0) {
2483                                 printk(KERN_ERR
2484                                        "hda_codec: cannot revert codec\n");
2485                                 return err;
2486                         }
2487                 }
2488         }
2489         return 0;
2490 }
2491 EXPORT_SYMBOL_HDA(snd_hda_build_controls);
2492
2493 int snd_hda_codec_build_controls(struct hda_codec *codec)
2494 {
2495         int err = 0;
2496         hda_exec_init_verbs(codec);
2497         /* continue to initialize... */
2498         if (codec->patch_ops.init)
2499                 err = codec->patch_ops.init(codec);
2500         if (!err && codec->patch_ops.build_controls)
2501                 err = codec->patch_ops.build_controls(codec);
2502         if (err < 0)
2503                 return err;
2504         return 0;
2505 }
2506
2507 /*
2508  * stream formats
2509  */
2510 struct hda_rate_tbl {
2511         unsigned int hz;
2512         unsigned int alsa_bits;
2513         unsigned int hda_fmt;
2514 };
2515
2516 static struct hda_rate_tbl rate_bits[] = {
2517         /* rate in Hz, ALSA rate bitmask, HDA format value */
2518
2519         /* autodetected value used in snd_hda_query_supported_pcm */
2520         { 8000, SNDRV_PCM_RATE_8000, 0x0500 }, /* 1/6 x 48 */
2521         { 11025, SNDRV_PCM_RATE_11025, 0x4300 }, /* 1/4 x 44 */
2522         { 16000, SNDRV_PCM_RATE_16000, 0x0200 }, /* 1/3 x 48 */
2523         { 22050, SNDRV_PCM_RATE_22050, 0x4100 }, /* 1/2 x 44 */
2524         { 32000, SNDRV_PCM_RATE_32000, 0x0a00 }, /* 2/3 x 48 */
2525         { 44100, SNDRV_PCM_RATE_44100, 0x4000 }, /* 44 */
2526         { 48000, SNDRV_PCM_RATE_48000, 0x0000 }, /* 48 */
2527         { 88200, SNDRV_PCM_RATE_88200, 0x4800 }, /* 2 x 44 */
2528         { 96000, SNDRV_PCM_RATE_96000, 0x0800 }, /* 2 x 48 */
2529         { 176400, SNDRV_PCM_RATE_176400, 0x5800 },/* 4 x 44 */
2530         { 192000, SNDRV_PCM_RATE_192000, 0x1800 }, /* 4 x 48 */
2531 #define AC_PAR_PCM_RATE_BITS    11
2532         /* up to bits 10, 384kHZ isn't supported properly */
2533
2534         /* not autodetected value */
2535         { 9600, SNDRV_PCM_RATE_KNOT, 0x0400 }, /* 1/5 x 48 */
2536
2537         { 0 } /* terminator */
2538 };
2539
2540 /**
2541  * snd_hda_calc_stream_format - calculate format bitset
2542  * @rate: the sample rate
2543  * @channels: the number of channels
2544  * @format: the PCM format (SNDRV_PCM_FORMAT_XXX)
2545  * @maxbps: the max. bps
2546  *
2547  * Calculate the format bitset from the given rate, channels and th PCM format.
2548  *
2549  * Return zero if invalid.
2550  */
2551 unsigned int snd_hda_calc_stream_format(unsigned int rate,
2552                                         unsigned int channels,
2553                                         unsigned int format,
2554                                         unsigned int maxbps)
2555 {
2556         int i;
2557         unsigned int val = 0;
2558
2559         for (i = 0; rate_bits[i].hz; i++)
2560                 if (rate_bits[i].hz == rate) {
2561                         val = rate_bits[i].hda_fmt;
2562                         break;
2563                 }
2564         if (!rate_bits[i].hz) {
2565                 snd_printdd("invalid rate %d\n", rate);
2566                 return 0;
2567         }
2568
2569         if (channels == 0 || channels > 8) {
2570                 snd_printdd("invalid channels %d\n", channels);
2571                 return 0;
2572         }
2573         val |= channels - 1;
2574
2575         switch (snd_pcm_format_width(format)) {
2576         case 8:  val |= 0x00; break;
2577         case 16: val |= 0x10; break;
2578         case 20:
2579         case 24:
2580         case 32:
2581                 if (maxbps >= 32 || format == SNDRV_PCM_FORMAT_FLOAT_LE)
2582                         val |= 0x40;
2583                 else if (maxbps >= 24)
2584                         val |= 0x30;
2585                 else
2586                         val |= 0x20;
2587                 break;
2588         default:
2589                 snd_printdd("invalid format width %d\n",
2590                             snd_pcm_format_width(format));
2591                 return 0;
2592         }
2593
2594         return val;
2595 }
2596 EXPORT_SYMBOL_HDA(snd_hda_calc_stream_format);
2597
2598 static unsigned int get_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2599 {
2600         unsigned int val = 0;
2601         if (nid != codec->afg &&
2602             (get_wcaps(codec, nid) & AC_WCAP_FORMAT_OVRD))
2603                 val = snd_hda_param_read(codec, nid, AC_PAR_PCM);
2604         if (!val || val == -1)
2605                 val = snd_hda_param_read(codec, codec->afg, AC_PAR_PCM);
2606         if (!val || val == -1)
2607                 return 0;
2608         return val;
2609 }
2610
2611 static unsigned int query_pcm_param(struct hda_codec *codec, hda_nid_t nid)
2612 {
2613         return query_caps_hash(codec, nid, HDA_HASH_PARPCM_KEY(nid),
2614                                get_pcm_param);
2615 }
2616
2617 static unsigned int get_stream_param(struct hda_codec *codec, hda_nid_t nid)
2618 {
2619         unsigned int streams = snd_hda_param_read(codec, nid, AC_PAR_STREAM);
2620         if (!streams || streams == -1)
2621                 streams = snd_hda_param_read(codec, codec->afg, AC_PAR_STREAM);
2622         if (!streams || streams == -1)
2623                 return 0;
2624         return streams;
2625 }
2626
2627 static unsigned int query_stream_param(struct hda_codec *codec, hda_nid_t nid)
2628 {
2629         return query_caps_hash(codec, nid, HDA_HASH_PARSTR_KEY(nid),
2630                                get_stream_param);
2631 }
2632
2633 /**
2634  * snd_hda_query_supported_pcm - query the supported PCM rates and formats
2635  * @codec: the HDA codec
2636  * @nid: NID to query
2637  * @ratesp: the pointer to store the detected rate bitflags
2638  * @formatsp: the pointer to store the detected formats
2639  * @bpsp: the pointer to store the detected format widths
2640  *
2641  * Queries the supported PCM rates and formats.  The NULL @ratesp, @formatsp
2642  * or @bsps argument is ignored.
2643  *
2644  * Returns 0 if successful, otherwise a negative error code.
2645  */
2646 static int snd_hda_query_supported_pcm(struct hda_codec *codec, hda_nid_t nid,
2647                                 u32 *ratesp, u64 *formatsp, unsigned int *bpsp)
2648 {
2649         unsigned int i, val, wcaps;
2650
2651         wcaps = get_wcaps(codec, nid);
2652         val = query_pcm_param(codec, nid);
2653
2654         if (ratesp) {
2655                 u32 rates = 0;
2656                 for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++) {
2657                         if (val & (1 << i))
2658                                 rates |= rate_bits[i].alsa_bits;
2659                 }
2660                 if (rates == 0) {
2661                         snd_printk(KERN_ERR "hda_codec: rates == 0 "
2662                                    "(nid=0x%x, val=0x%x, ovrd=%i)\n",
2663                                         nid, val,
2664                                         (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0);
2665                         return -EIO;
2666                 }
2667                 *ratesp = rates;
2668         }
2669
2670         if (formatsp || bpsp) {
2671                 u64 formats = 0;
2672                 unsigned int streams, bps;
2673
2674                 streams = query_stream_param(codec, nid);
2675                 if (!streams)
2676                         return -EIO;
2677
2678                 bps = 0;
2679                 if (streams & AC_SUPFMT_PCM) {
2680                         if (val & AC_SUPPCM_BITS_8) {
2681                                 formats |= SNDRV_PCM_FMTBIT_U8;
2682                                 bps = 8;
2683                         }
2684                         if (val & AC_SUPPCM_BITS_16) {
2685                                 formats |= SNDRV_PCM_FMTBIT_S16_LE;
2686                                 bps = 16;
2687                         }
2688                         if (wcaps & AC_WCAP_DIGITAL) {
2689                                 if (val & AC_SUPPCM_BITS_32)
2690                                         formats |= SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE;
2691                                 if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24))
2692                                         formats |= SNDRV_PCM_FMTBIT_S32_LE;
2693                                 if (val & AC_SUPPCM_BITS_24)
2694                                         bps = 24;
2695                                 else if (val & AC_SUPPCM_BITS_20)
2696                                         bps = 20;
2697                         } else if (val & (AC_SUPPCM_BITS_20|AC_SUPPCM_BITS_24|
2698                                           AC_SUPPCM_BITS_32)) {
2699                                 formats |= SNDRV_PCM_FMTBIT_S32_LE;
2700                                 if (val & AC_SUPPCM_BITS_32)
2701                                         bps = 32;
2702                                 else if (val & AC_SUPPCM_BITS_24)
2703                                         bps = 24;
2704                                 else if (val & AC_SUPPCM_BITS_20)
2705                                         bps = 20;
2706                         }
2707                 }
2708                 if (streams & AC_SUPFMT_FLOAT32) {
2709                         formats |= SNDRV_PCM_FMTBIT_FLOAT_LE;
2710                         if (!bps)
2711                                 bps = 32;
2712                 }
2713                 if (streams == AC_SUPFMT_AC3) {
2714                         /* should be exclusive */
2715                         /* temporary hack: we have still no proper support
2716                          * for the direct AC3 stream...
2717                          */
2718                         formats |= SNDRV_PCM_FMTBIT_U8;
2719                         bps = 8;
2720                 }
2721                 if (formats == 0) {
2722                         snd_printk(KERN_ERR "hda_codec: formats == 0 "
2723                                    "(nid=0x%x, val=0x%x, ovrd=%i, "
2724                                    "streams=0x%x)\n",
2725                                         nid, val,
2726                                         (wcaps & AC_WCAP_FORMAT_OVRD) ? 1 : 0,
2727                                         streams);
2728                         return -EIO;
2729                 }
2730                 if (formatsp)
2731                         *formatsp = formats;
2732                 if (bpsp)
2733                         *bpsp = bps;
2734         }
2735
2736         return 0;
2737 }
2738
2739 /**
2740  * snd_hda_is_supported_format - check whether the given node supports
2741  * the format val
2742  *
2743  * Returns 1 if supported, 0 if not.
2744  */
2745 int snd_hda_is_supported_format(struct hda_codec *codec, hda_nid_t nid,
2746                                 unsigned int format)
2747 {
2748         int i;
2749         unsigned int val = 0, rate, stream;
2750
2751         val = query_pcm_param(codec, nid);
2752         if (!val)
2753                 return 0;
2754
2755         rate = format & 0xff00;
2756         for (i = 0; i < AC_PAR_PCM_RATE_BITS; i++)
2757                 if (rate_bits[i].hda_fmt == rate) {
2758                         if (val & (1 << i))
2759                                 break;
2760                         return 0;
2761                 }
2762         if (i >= AC_PAR_PCM_RATE_BITS)
2763                 return 0;
2764
2765         stream = query_stream_param(codec, nid);
2766         if (!stream)
2767                 return 0;
2768
2769         if (stream & AC_SUPFMT_PCM) {
2770                 switch (format & 0xf0) {
2771                 case 0x00:
2772                         if (!(val & AC_SUPPCM_BITS_8))
2773                                 return 0;
2774                         break;
2775                 case 0x10:
2776                         if (!(val & AC_SUPPCM_BITS_16))
2777                                 return 0;
2778                         break;
2779                 case 0x20:
2780                         if (!(val & AC_SUPPCM_BITS_20))
2781                                 return 0;
2782                         break;
2783                 case 0x30:
2784                         if (!(val & AC_SUPPCM_BITS_24))
2785                                 return 0;
2786                         break;
2787                 case 0x40:
2788                         if (!(val & AC_SUPPCM_BITS_32))
2789                                 return 0;
2790                         break;
2791                 default:
2792                         return 0;
2793                 }
2794         } else {
2795                 /* FIXME: check for float32 and AC3? */
2796         }
2797
2798         return 1;
2799 }
2800 EXPORT_SYMBOL_HDA(snd_hda_is_supported_format);
2801
2802 /*
2803  * PCM stuff
2804  */
2805 static int hda_pcm_default_open_close(struct hda_pcm_stream *hinfo,
2806                                       struct hda_codec *codec,
2807                                       struct snd_pcm_substream *substream)
2808 {
2809         return 0;
2810 }
2811
2812 static int hda_pcm_default_prepare(struct hda_pcm_stream *hinfo,
2813                                    struct hda_codec *codec,
2814                                    unsigned int stream_tag,
2815                                    unsigned int format,
2816                                    struct snd_pcm_substream *substream)
2817 {
2818         snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
2819         return 0;
2820 }
2821
2822 static int hda_pcm_default_cleanup(struct hda_pcm_stream *hinfo,
2823                                    struct hda_codec *codec,
2824                                    struct snd_pcm_substream *substream)
2825 {
2826         snd_hda_codec_cleanup_stream(codec, hinfo->nid);
2827         return 0;
2828 }
2829
2830 static int set_pcm_default_values(struct hda_codec *codec,
2831                                   struct hda_pcm_stream *info)
2832 {
2833         int err;
2834
2835         /* query support PCM information from the given NID */
2836         if (info->nid && (!info->rates || !info->formats)) {
2837                 err = snd_hda_query_supported_pcm(codec, info->nid,
2838                                 info->rates ? NULL : &info->rates,
2839                                 info->formats ? NULL : &info->formats,
2840                                 info->maxbps ? NULL : &info->maxbps);
2841                 if (err < 0)
2842                         return err;
2843         }
2844         if (info->ops.open == NULL)
2845                 info->ops.open = hda_pcm_default_open_close;
2846         if (info->ops.close == NULL)
2847                 info->ops.close = hda_pcm_default_open_close;
2848         if (info->ops.prepare == NULL) {
2849                 if (snd_BUG_ON(!info->nid))
2850                         return -EINVAL;
2851                 info->ops.prepare = hda_pcm_default_prepare;
2852         }
2853         if (info->ops.cleanup == NULL) {
2854                 if (snd_BUG_ON(!info->nid))
2855                         return -EINVAL;
2856                 info->ops.cleanup = hda_pcm_default_cleanup;
2857         }
2858         return 0;
2859 }
2860
2861 /*
2862  * get the empty PCM device number to assign
2863  */
2864 static int get_empty_pcm_device(struct hda_bus *bus, int type)
2865 {
2866         static const char *dev_name[HDA_PCM_NTYPES] = {
2867                 "Audio", "SPDIF", "HDMI", "Modem"
2868         };
2869         /* starting device index for each PCM type */
2870         static int dev_idx[HDA_PCM_NTYPES] = {
2871                 [HDA_PCM_TYPE_AUDIO] = 0,
2872                 [HDA_PCM_TYPE_SPDIF] = 1,
2873                 [HDA_PCM_TYPE_HDMI] = 3,
2874                 [HDA_PCM_TYPE_MODEM] = 6
2875         };
2876         /* normal audio device indices; not linear to keep compatibility */
2877         static int audio_idx[4] = { 0, 2, 4, 5 };
2878         int i, dev;
2879
2880         switch (type) {
2881         case HDA_PCM_TYPE_AUDIO:
2882                 for (i = 0; i < ARRAY_SIZE(audio_idx); i++) {
2883                         dev = audio_idx[i];
2884                         if (!test_bit(dev, bus->pcm_dev_bits))
2885                                 goto ok;
2886                 }
2887                 snd_printk(KERN_WARNING "Too many audio devices\n");
2888                 return -EAGAIN;
2889         case HDA_PCM_TYPE_SPDIF:
2890         case HDA_PCM_TYPE_HDMI:
2891         case HDA_PCM_TYPE_MODEM:
2892                 dev = dev_idx[type];
2893                 if (test_bit(dev, bus->pcm_dev_bits)) {
2894                         snd_printk(KERN_WARNING "%s already defined\n",
2895                                    dev_name[type]);
2896                         return -EAGAIN;
2897                 }
2898                 break;
2899         default:
2900                 snd_printk(KERN_WARNING "Invalid PCM type %d\n", type);
2901                 return -EINVAL;
2902         }
2903  ok:
2904         set_bit(dev, bus->pcm_dev_bits);
2905         return dev;
2906 }
2907
2908 /*
2909  * attach a new PCM stream
2910  */
2911 static int snd_hda_attach_pcm(struct hda_codec *codec, struct hda_pcm *pcm)
2912 {
2913         struct hda_bus *bus = codec->bus;
2914         struct hda_pcm_stream *info;
2915         int stream, err;
2916
2917         if (snd_BUG_ON(!pcm->name))
2918                 return -EINVAL;
2919         for (stream = 0; stream < 2; stream++) {
2920                 info = &pcm->stream[stream];
2921                 if (info->substreams) {
2922                         err = set_pcm_default_values(codec, info);
2923                         if (err < 0)
2924                                 return err;
2925                 }
2926         }
2927         return bus->ops.attach_pcm(bus, codec, pcm);
2928 }
2929
2930 /* assign all PCMs of the given codec */
2931 int snd_hda_codec_build_pcms(struct hda_codec *codec)
2932 {
2933         unsigned int pcm;
2934         int err;
2935
2936         if (!codec->num_pcms) {
2937                 if (!codec->patch_ops.build_pcms)
2938                         return 0;
2939                 err = codec->patch_ops.build_pcms(codec);
2940                 if (err < 0) {
2941                         printk(KERN_ERR "hda_codec: cannot build PCMs"
2942                                "for #%d (error %d)\n", codec->addr, err); 
2943                         err = snd_hda_codec_reset(codec);
2944                         if (err < 0) {
2945                                 printk(KERN_ERR
2946                                        "hda_codec: cannot revert codec\n");
2947                                 return err;
2948                         }
2949                 }
2950         }
2951         for (pcm = 0; pcm < codec->num_pcms; pcm++) {
2952                 struct hda_pcm *cpcm = &codec->pcm_info[pcm];
2953                 int dev;
2954
2955                 if (!cpcm->stream[0].substreams && !cpcm->stream[1].substreams)
2956                         continue; /* no substreams assigned */
2957
2958                 if (!cpcm->pcm) {
2959                         dev = get_empty_pcm_device(codec->bus, cpcm->pcm_type);
2960                         if (dev < 0)
2961                                 continue; /* no fatal error */
2962                         cpcm->device = dev;
2963                         err = snd_hda_attach_pcm(codec, cpcm);
2964                         if (err < 0) {
2965                                 printk(KERN_ERR "hda_codec: cannot attach "
2966                                        "PCM stream %d for codec #%d\n",
2967                                        dev, codec->addr);
2968                                 continue; /* no fatal error */
2969                         }
2970                 }
2971         }
2972         return 0;
2973 }
2974
2975 /**
2976  * snd_hda_build_pcms - build PCM information
2977  * @bus: the BUS
2978  *
2979  * Create PCM information for each codec included in the bus.
2980  *
2981  * The build_pcms codec patch is requested to set up codec->num_pcms and
2982  * codec->pcm_info properly.  The array is referred by the top-level driver
2983  * to create its PCM instances.
2984  * The allocated codec->pcm_info should be released in codec->patch_ops.free
2985  * callback.
2986  *
2987  * At least, substreams, channels_min and channels_max must be filled for
2988  * each stream.  substreams = 0 indicates that the stream doesn't exist.
2989  * When rates and/or formats are zero, the supported values are queried
2990  * from the given nid.  The nid is used also by the default ops.prepare
2991  * and ops.cleanup callbacks.
2992  *
2993  * The driver needs to call ops.open in its open callback.  Similarly,
2994  * ops.close is supposed to be called in the close callback.
2995  * ops.prepare should be called in the prepare or hw_params callback
2996  * with the proper parameters for set up.
2997  * ops.cleanup should be called in hw_free for clean up of streams.
2998  *
2999  * This function returns 0 if successfull, or a negative error code.
3000  */
3001 int __devinit snd_hda_build_pcms(struct hda_bus *bus)
3002 {
3003         struct hda_codec *codec;
3004
3005         list_for_each_entry(codec, &bus->codec_list, list) {
3006                 int err = snd_hda_codec_build_pcms(codec);
3007                 if (err < 0)
3008                         return err;
3009         }
3010         return 0;
3011 }
3012 EXPORT_SYMBOL_HDA(snd_hda_build_pcms);
3013
3014 /**
3015  * snd_hda_check_board_config - compare the current codec with the config table
3016  * @codec: the HDA codec
3017  * @num_configs: number of config enums
3018  * @models: array of model name strings
3019  * @tbl: configuration table, terminated by null entries
3020  *
3021  * Compares the modelname or PCI subsystem id of the current codec with the
3022  * given configuration table.  If a matching entry is found, returns its
3023  * config value (supposed to be 0 or positive).
3024  *
3025  * If no entries are matching, the function returns a negative value.
3026  */
3027 int snd_hda_check_board_config(struct hda_codec *codec,
3028                                int num_configs, const char **models,
3029                                const struct snd_pci_quirk *tbl)
3030 {
3031         if (codec->modelname && models) {
3032                 int i;
3033                 for (i = 0; i < num_configs; i++) {
3034                         if (models[i] &&
3035                             !strcmp(codec->modelname, models[i])) {
3036                                 snd_printd(KERN_INFO "hda_codec: model '%s' is "
3037                                            "selected\n", models[i]);
3038                                 return i;
3039                         }
3040                 }
3041         }
3042
3043         if (!codec->bus->pci || !tbl)
3044                 return -1;
3045
3046         tbl = snd_pci_quirk_lookup(codec->bus->pci, tbl);
3047         if (!tbl)
3048                 return -1;
3049         if (tbl->value >= 0 && tbl->value < num_configs) {
3050 #ifdef CONFIG_SND_DEBUG_VERBOSE
3051                 char tmp[10];
3052                 const char *model = NULL;
3053                 if (models)
3054                         model = models[tbl->value];
3055                 if (!model) {
3056                         sprintf(tmp, "#%d", tbl->value);
3057                         model = tmp;
3058                 }
3059                 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3060                             "for config %x:%x (%s)\n",
3061                             model, tbl->subvendor, tbl->subdevice,
3062                             (tbl->name ? tbl->name : "Unknown device"));
3063 #endif
3064                 return tbl->value;
3065         }
3066         return -1;
3067 }
3068 EXPORT_SYMBOL_HDA(snd_hda_check_board_config);
3069
3070 /**
3071  * snd_hda_check_board_codec_sid_config - compare the current codec
3072                                           subsystem ID with the
3073                                           config table
3074
3075            This is important for Gateway notebooks with SB450 HDA Audio
3076            where the vendor ID of the PCI device is:
3077                 ATI Technologies Inc SB450 HDA Audio [1002:437b]
3078            and the vendor/subvendor are found only at the codec.
3079
3080  * @codec: the HDA codec
3081  * @num_configs: number of config enums
3082  * @models: array of model name strings
3083  * @tbl: configuration table, terminated by null entries
3084  *
3085  * Compares the modelname or PCI subsystem id of the current codec with the
3086  * given configuration table.  If a matching entry is found, returns its
3087  * config value (supposed to be 0 or positive).
3088  *
3089  * If no entries are matching, the function returns a negative value.
3090  */
3091 int snd_hda_check_board_codec_sid_config(struct hda_codec *codec,
3092                                int num_configs, const char **models,
3093                                const struct snd_pci_quirk *tbl)
3094 {
3095         const struct snd_pci_quirk *q;
3096
3097         /* Search for codec ID */
3098         for (q = tbl; q->subvendor; q++) {
3099                 unsigned long vendorid = (q->subdevice) | (q->subvendor << 16);
3100
3101                 if (vendorid == codec->subsystem_id)
3102                         break;
3103         }
3104
3105         if (!q->subvendor)
3106                 return -1;
3107
3108         tbl = q;
3109
3110         if (tbl->value >= 0 && tbl->value < num_configs) {
3111 #ifdef CONFIG_SND_DEBUG_DETECT
3112                 char tmp[10];
3113                 const char *model = NULL;
3114                 if (models)
3115                         model = models[tbl->value];
3116                 if (!model) {
3117                         sprintf(tmp, "#%d", tbl->value);
3118                         model = tmp;
3119                 }
3120                 snd_printdd(KERN_INFO "hda_codec: model '%s' is selected "
3121                             "for config %x:%x (%s)\n",
3122                             model, tbl->subvendor, tbl->subdevice,
3123                             (tbl->name ? tbl->name : "Unknown device"));
3124 #endif
3125                 return tbl->value;
3126         }
3127         return -1;
3128 }
3129 EXPORT_SYMBOL_HDA(snd_hda_check_board_codec_sid_config);
3130
3131 /**
3132  * snd_hda_add_new_ctls - create controls from the array
3133  * @codec: the HDA codec
3134  * @knew: the array of struct snd_kcontrol_new
3135  *
3136  * This helper function creates and add new controls in the given array.
3137  * The array must be terminated with an empty entry as terminator.
3138  *
3139  * Returns 0 if successful, or a negative error code.
3140  */
3141 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
3142 {
3143         int err;
3144
3145         for (; knew->name; knew++) {
3146                 struct snd_kcontrol *kctl;
3147                 kctl = snd_ctl_new1(knew, codec);
3148                 if (!kctl)
3149                         return -ENOMEM;
3150                 err = snd_hda_ctl_add(codec, kctl);
3151                 if (err < 0) {
3152                         if (!codec->addr)
3153                                 return err;
3154                         kctl = snd_ctl_new1(knew, codec);
3155                         if (!kctl)
3156                                 return -ENOMEM;
3157                         kctl->id.device = codec->addr;
3158                         err = snd_hda_ctl_add(codec, kctl);
3159                         if (err < 0)
3160                                 return err;
3161                 }
3162         }
3163         return 0;
3164 }
3165 EXPORT_SYMBOL_HDA(snd_hda_add_new_ctls);
3166
3167 #ifdef CONFIG_SND_HDA_POWER_SAVE
3168 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
3169                                 unsigned int power_state);
3170
3171 static void hda_power_work(struct work_struct *work)
3172 {
3173         struct hda_codec *codec =
3174                 container_of(work, struct hda_codec, power_work.work);
3175         struct hda_bus *bus = codec->bus;
3176
3177         if (!codec->power_on || codec->power_count) {
3178                 codec->power_transition = 0;
3179                 return;
3180         }
3181
3182         hda_call_codec_suspend(codec);
3183         if (bus->ops.pm_notify)
3184                 bus->ops.pm_notify(bus);
3185 }
3186
3187 static void hda_keep_power_on(struct hda_codec *codec)
3188 {
3189         codec->power_count++;
3190         codec->power_on = 1;
3191 }
3192
3193 void snd_hda_power_up(struct hda_codec *codec)
3194 {
3195         struct hda_bus *bus = codec->bus;
3196
3197         codec->power_count++;
3198         if (codec->power_on || codec->power_transition)
3199                 return;
3200
3201         codec->power_on = 1;
3202         if (bus->ops.pm_notify)
3203                 bus->ops.pm_notify(bus);
3204         hda_call_codec_resume(codec);
3205         cancel_delayed_work(&codec->power_work);
3206         codec->power_transition = 0;
3207 }
3208 EXPORT_SYMBOL_HDA(snd_hda_power_up);
3209
3210 #define power_save(codec)       \
3211         ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3212
3213 #define power_save(codec)       \
3214         ((codec)->bus->power_save ? *(codec)->bus->power_save : 0)
3215
3216 void snd_hda_power_down(struct hda_codec *codec)
3217 {
3218         --codec->power_count;
3219         if (!codec->power_on || codec->power_count || codec->power_transition)
3220                 return;
3221         if (power_save(codec)) {
3222                 codec->power_transition = 1; /* avoid reentrance */
3223                 queue_delayed_work(codec->bus->workq, &codec->power_work,
3224                                 msecs_to_jiffies(power_save(codec) * 1000));
3225         }
3226 }
3227 EXPORT_SYMBOL_HDA(snd_hda_power_down);
3228
3229 int snd_hda_check_amp_list_power(struct hda_codec *codec,
3230                                  struct hda_loopback_check *check,
3231                                  hda_nid_t nid)
3232 {
3233         struct hda_amp_list *p;
3234         int ch, v;
3235
3236         if (!check->amplist)
3237                 return 0;
3238         for (p = check->amplist; p->nid; p++) {
3239                 if (p->nid == nid)
3240                         break;
3241         }
3242         if (!p->nid)
3243                 return 0; /* nothing changed */
3244
3245         for (p = check->amplist; p->nid; p++) {
3246                 for (ch = 0; ch < 2; ch++) {
3247                         v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
3248                                                    p->idx);
3249                         if (!(v & HDA_AMP_MUTE) && v > 0) {
3250                                 if (!check->power_on) {
3251                                         check->power_on = 1;
3252                                         snd_hda_power_up(codec);
3253                                 }
3254                                 return 1;
3255                         }
3256                 }
3257         }
3258         if (check->power_on) {
3259                 check->power_on = 0;
3260                 snd_hda_power_down(codec);
3261         }
3262         return 0;
3263 }
3264 EXPORT_SYMBOL_HDA(snd_hda_check_amp_list_power);
3265 #endif
3266
3267 /*
3268  * Channel mode helper
3269  */
3270 int snd_hda_ch_mode_info(struct hda_codec *codec,
3271                          struct snd_ctl_elem_info *uinfo,
3272                          const struct hda_channel_mode *chmode,
3273                          int num_chmodes)
3274 {
3275         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3276         uinfo->count = 1;
3277         uinfo->value.enumerated.items = num_chmodes;
3278         if (uinfo->value.enumerated.item >= num_chmodes)
3279                 uinfo->value.enumerated.item = num_chmodes - 1;
3280         sprintf(uinfo->value.enumerated.name, "%dch",
3281                 chmode[uinfo->value.enumerated.item].channels);
3282         return 0;
3283 }
3284 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_info);
3285
3286 int snd_hda_ch_mode_get(struct hda_codec *codec,
3287                         struct snd_ctl_elem_value *ucontrol,
3288                         const struct hda_channel_mode *chmode,
3289                         int num_chmodes,
3290                         int max_channels)
3291 {
3292         int i;
3293
3294         for (i = 0; i < num_chmodes; i++) {
3295                 if (max_channels == chmode[i].channels) {
3296                         ucontrol->value.enumerated.item[0] = i;
3297                         break;
3298                 }
3299         }
3300         return 0;
3301 }
3302 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_get);
3303
3304 int snd_hda_ch_mode_put(struct hda_codec *codec,
3305                         struct snd_ctl_elem_value *ucontrol,
3306                         const struct hda_channel_mode *chmode,
3307                         int num_chmodes,
3308                         int *max_channelsp)
3309 {
3310         unsigned int mode;
3311
3312         mode = ucontrol->value.enumerated.item[0];
3313         if (mode >= num_chmodes)
3314                 return -EINVAL;
3315         if (*max_channelsp == chmode[mode].channels)
3316                 return 0;
3317         /* change the current channel setting */
3318         *max_channelsp = chmode[mode].channels;
3319         if (chmode[mode].sequence)
3320                 snd_hda_sequence_write_cache(codec, chmode[mode].sequence);
3321         return 1;
3322 }
3323 EXPORT_SYMBOL_HDA(snd_hda_ch_mode_put);
3324
3325 /*
3326  * input MUX helper
3327  */
3328 int snd_hda_input_mux_info(const struct hda_input_mux *imux,
3329                            struct snd_ctl_elem_info *uinfo)
3330 {
3331         unsigned int index;
3332
3333         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
3334         uinfo->count = 1;
3335         uinfo->value.enumerated.items = imux->num_items;
3336         if (!imux->num_items)
3337                 return 0;
3338         index = uinfo->value.enumerated.item;
3339         if (index >= imux->num_items)
3340                 index = imux->num_items - 1;
3341         strcpy(uinfo->value.enumerated.name, imux->items[index].label);
3342         return 0;
3343 }
3344 EXPORT_SYMBOL_HDA(snd_hda_input_mux_info);
3345
3346 int snd_hda_input_mux_put(struct hda_codec *codec,
3347                           const struct hda_input_mux *imux,
3348                           struct snd_ctl_elem_value *ucontrol,
3349                           hda_nid_t nid,
3350                           unsigned int *cur_val)
3351 {
3352         unsigned int idx;
3353
3354         if (!imux->num_items)
3355                 return 0;
3356         idx = ucontrol->value.enumerated.item[0];
3357         if (idx >= imux->num_items)
3358                 idx = imux->num_items - 1;
3359         if (*cur_val == idx)
3360                 return 0;
3361         snd_hda_codec_write_cache(codec, nid, 0, AC_VERB_SET_CONNECT_SEL,
3362                                   imux->items[idx].index);
3363         *cur_val = idx;
3364         return 1;
3365 }
3366 EXPORT_SYMBOL_HDA(snd_hda_input_mux_put);
3367
3368
3369 /*
3370  * Multi-channel / digital-out PCM helper functions
3371  */
3372
3373 /* setup SPDIF output stream */
3374 static void setup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid,
3375                                  unsigned int stream_tag, unsigned int format)
3376 {
3377         /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
3378         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3379                 set_dig_out_convert(codec, nid, 
3380                                     codec->spdif_ctls & ~AC_DIG1_ENABLE & 0xff,
3381                                     -1);
3382         snd_hda_codec_setup_stream(codec, nid, stream_tag, 0, format);
3383         if (codec->slave_dig_outs) {
3384                 hda_nid_t *d;
3385                 for (d = codec->slave_dig_outs; *d; d++)
3386                         snd_hda_codec_setup_stream(codec, *d, stream_tag, 0,
3387                                                    format);
3388         }
3389         /* turn on again (if needed) */
3390         if (codec->spdif_status_reset && (codec->spdif_ctls & AC_DIG1_ENABLE))
3391                 set_dig_out_convert(codec, nid,
3392                                     codec->spdif_ctls & 0xff, -1);
3393 }
3394
3395 static void cleanup_dig_out_stream(struct hda_codec *codec, hda_nid_t nid)
3396 {
3397         snd_hda_codec_cleanup_stream(codec, nid);
3398         if (codec->slave_dig_outs) {
3399                 hda_nid_t *d;
3400                 for (d = codec->slave_dig_outs; *d; d++)
3401                         snd_hda_codec_cleanup_stream(codec, *d);
3402         }
3403 }
3404
3405 /*
3406  * open the digital out in the exclusive mode
3407  */
3408 int snd_hda_multi_out_dig_open(struct hda_codec *codec,
3409                                struct hda_multi_out *mout)
3410 {
3411         mutex_lock(&codec->spdif_mutex);
3412         if (mout->dig_out_used == HDA_DIG_ANALOG_DUP)
3413                 /* already opened as analog dup; reset it once */
3414                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3415         mout->dig_out_used = HDA_DIG_EXCLUSIVE;
3416         mutex_unlock(&codec->spdif_mutex);
3417         return 0;
3418 }
3419 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_open);
3420
3421 int snd_hda_multi_out_dig_prepare(struct hda_codec *codec,
3422                                   struct hda_multi_out *mout,
3423                                   unsigned int stream_tag,
3424                                   unsigned int format,
3425                                   struct snd_pcm_substream *substream)
3426 {
3427         mutex_lock(&codec->spdif_mutex);
3428         setup_dig_out_stream(codec, mout->dig_out_nid, stream_tag, format);
3429         mutex_unlock(&codec->spdif_mutex);
3430         return 0;
3431 }
3432 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_prepare);
3433
3434 int snd_hda_multi_out_dig_cleanup(struct hda_codec *codec,
3435                                   struct hda_multi_out *mout)
3436 {
3437         mutex_lock(&codec->spdif_mutex);
3438         cleanup_dig_out_stream(codec, mout->dig_out_nid);
3439         mutex_unlock(&codec->spdif_mutex);
3440         return 0;
3441 }
3442 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_cleanup);
3443
3444 /*
3445  * release the digital out
3446  */
3447 int snd_hda_multi_out_dig_close(struct hda_codec *codec,
3448                                 struct hda_multi_out *mout)
3449 {
3450         mutex_lock(&codec->spdif_mutex);
3451         mout->dig_out_used = 0;
3452         mutex_unlock(&codec->spdif_mutex);
3453         return 0;
3454 }
3455 EXPORT_SYMBOL_HDA(snd_hda_multi_out_dig_close);
3456
3457 /*
3458  * set up more restrictions for analog out
3459  */
3460 int snd_hda_multi_out_analog_open(struct hda_codec *codec,
3461                                   struct hda_multi_out *mout,
3462                                   struct snd_pcm_substream *substream,
3463                                   struct hda_pcm_stream *hinfo)
3464 {
3465         struct snd_pcm_runtime *runtime = substream->runtime;
3466         runtime->hw.channels_max = mout->max_channels;
3467         if (mout->dig_out_nid) {
3468                 if (!mout->analog_rates) {
3469                         mout->analog_rates = hinfo->rates;
3470                         mout->analog_formats = hinfo->formats;
3471                         mout->analog_maxbps = hinfo->maxbps;
3472                 } else {
3473                         runtime->hw.rates = mout->analog_rates;
3474                         runtime->hw.formats = mout->analog_formats;
3475                         hinfo->maxbps = mout->analog_maxbps;
3476                 }
3477                 if (!mout->spdif_rates) {
3478                         snd_hda_query_supported_pcm(codec, mout->dig_out_nid,
3479                                                     &mout->spdif_rates,
3480                                                     &mout->spdif_formats,
3481                                                     &mout->spdif_maxbps);
3482                 }
3483                 mutex_lock(&codec->spdif_mutex);
3484                 if (mout->share_spdif) {
3485                         if ((runtime->hw.rates & mout->spdif_rates) &&
3486                             (runtime->hw.formats & mout->spdif_formats)) {
3487                                 runtime->hw.rates &= mout->spdif_rates;
3488                                 runtime->hw.formats &= mout->spdif_formats;
3489                                 if (mout->spdif_maxbps < hinfo->maxbps)
3490                                         hinfo->maxbps = mout->spdif_maxbps;
3491                         } else {
3492                                 mout->share_spdif = 0;
3493                                 /* FIXME: need notify? */
3494                         }
3495                 }
3496                 mutex_unlock(&codec->spdif_mutex);
3497         }
3498         return snd_pcm_hw_constraint_step(substream->runtime, 0,
3499                                           SNDRV_PCM_HW_PARAM_CHANNELS, 2);
3500 }
3501 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_open);
3502
3503 /*
3504  * set up the i/o for analog out
3505  * when the digital out is available, copy the front out to digital out, too.
3506  */
3507 int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
3508                                      struct hda_multi_out *mout,
3509                                      unsigned int stream_tag,
3510                                      unsigned int format,
3511                                      struct snd_pcm_substream *substream)
3512 {
3513         hda_nid_t *nids = mout->dac_nids;
3514         int chs = substream->runtime->channels;
3515         int i;
3516
3517         mutex_lock(&codec->spdif_mutex);
3518         if (mout->dig_out_nid && mout->share_spdif &&
3519             mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
3520                 if (chs == 2 &&
3521                     snd_hda_is_supported_format(codec, mout->dig_out_nid,
3522                                                 format) &&
3523                     !(codec->spdif_status & IEC958_AES0_NONAUDIO)) {
3524                         mout->dig_out_used = HDA_DIG_ANALOG_DUP;
3525                         setup_dig_out_stream(codec, mout->dig_out_nid,
3526                                              stream_tag, format);
3527                 } else {
3528                         mout->dig_out_used = 0;
3529                         cleanup_dig_out_stream(codec, mout->dig_out_nid);
3530                 }
3531         }
3532         mutex_unlock(&codec->spdif_mutex);
3533
3534         /* front */
3535         snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
3536                                    0, format);
3537         if (!mout->no_share_stream &&
3538             mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
3539                 /* headphone out will just decode front left/right (stereo) */
3540                 snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
3541                                            0, format);
3542         /* extra outputs copied from front */
3543         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3544                 if (!mout->no_share_stream && mout->extra_out_nid[i])
3545                         snd_hda_codec_setup_stream(codec,
3546                                                    mout->extra_out_nid[i],
3547                                                    stream_tag, 0, format);
3548
3549         /* surrounds */
3550         for (i = 1; i < mout->num_dacs; i++) {
3551                 if (chs >= (i + 1) * 2) /* independent out */
3552                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3553                                                    i * 2, format);
3554                 else if (!mout->no_share_stream) /* copy front */
3555                         snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
3556                                                    0, format);
3557         }
3558         return 0;
3559 }
3560 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_prepare);
3561
3562 /*
3563  * clean up the setting for analog out
3564  */
3565 int snd_hda_multi_out_analog_cleanup(struct hda_codec *codec,
3566                                      struct hda_multi_out *mout)
3567 {
3568         hda_nid_t *nids = mout->dac_nids;
3569         int i;
3570
3571         for (i = 0; i < mout->num_dacs; i++)
3572                 snd_hda_codec_cleanup_stream(codec, nids[i]);
3573         if (mout->hp_nid)
3574                 snd_hda_codec_cleanup_stream(codec, mout->hp_nid);
3575         for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
3576                 if (mout->extra_out_nid[i])
3577                         snd_hda_codec_cleanup_stream(codec,
3578                                                      mout->extra_out_nid[i]);
3579         mutex_lock(&codec->spdif_mutex);
3580         if (mout->dig_out_nid && mout->dig_out_used == HDA_DIG_ANALOG_DUP) {
3581                 cleanup_dig_out_stream(codec, mout->dig_out_nid);
3582                 mout->dig_out_used = 0;
3583         }
3584         mutex_unlock(&codec->spdif_mutex);
3585         return 0;
3586 }
3587 EXPORT_SYMBOL_HDA(snd_hda_multi_out_analog_cleanup);
3588
3589 /*
3590  * Helper for automatic pin configuration
3591  */
3592
3593 static int is_in_nid_list(hda_nid_t nid, hda_nid_t *list)
3594 {
3595         for (; *list; list++)
3596                 if (*list == nid)
3597                         return 1;
3598         return 0;
3599 }
3600
3601
3602 /*
3603  * Sort an associated group of pins according to their sequence numbers.
3604  */
3605 static void sort_pins_by_sequence(hda_nid_t * pins, short * sequences,
3606                                   int num_pins)
3607 {
3608         int i, j;
3609         short seq;
3610         hda_nid_t nid;
3611         
3612         for (i = 0; i < num_pins; i++) {
3613                 for (j = i + 1; j < num_pins; j++) {
3614                         if (sequences[i] > sequences[j]) {
3615                                 seq = sequences[i];
3616                                 sequences[i] = sequences[j];
3617                                 sequences[j] = seq;
3618                                 nid = pins[i];
3619                                 pins[i] = pins[j];
3620                                 pins[j] = nid;
3621                         }
3622                 }
3623         }
3624 }
3625
3626
3627 /*
3628  * Parse all pin widgets and store the useful pin nids to cfg
3629  *
3630  * The number of line-outs or any primary output is stored in line_outs,
3631  * and the corresponding output pins are assigned to line_out_pins[],
3632  * in the order of front, rear, CLFE, side, ...
3633  *
3634  * If more extra outputs (speaker and headphone) are found, the pins are
3635  * assisnged to hp_pins[] and speaker_pins[], respectively.  If no line-out jack
3636  * is detected, one of speaker of HP pins is assigned as the primary
3637  * output, i.e. to line_out_pins[0].  So, line_outs is always positive
3638  * if any analog output exists.
3639  * 
3640  * The analog input pins are assigned to input_pins array.
3641  * The digital input/output pins are assigned to dig_in_pin and dig_out_pin,
3642  * respectively.
3643  */
3644 int snd_hda_parse_pin_def_config(struct hda_codec *codec,
3645                                  struct auto_pin_cfg *cfg,
3646                                  hda_nid_t *ignore_nids)
3647 {
3648         hda_nid_t nid, end_nid;
3649         short seq, assoc_line_out, assoc_speaker;
3650         short sequences_line_out[ARRAY_SIZE(cfg->line_out_pins)];
3651         short sequences_speaker[ARRAY_SIZE(cfg->speaker_pins)];
3652         short sequences_hp[ARRAY_SIZE(cfg->hp_pins)];
3653
3654         memset(cfg, 0, sizeof(*cfg));
3655
3656         memset(sequences_line_out, 0, sizeof(sequences_line_out));
3657         memset(sequences_speaker, 0, sizeof(sequences_speaker));
3658         memset(sequences_hp, 0, sizeof(sequences_hp));
3659         assoc_line_out = assoc_speaker = 0;
3660
3661         end_nid = codec->start_nid + codec->num_nodes;
3662         for (nid = codec->start_nid; nid < end_nid; nid++) {
3663                 unsigned int wid_caps = get_wcaps(codec, nid);
3664                 unsigned int wid_type =
3665                         (wid_caps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
3666                 unsigned int def_conf;
3667                 short assoc, loc;
3668
3669                 /* read all default configuration for pin complex */
3670                 if (wid_type != AC_WID_PIN)
3671                         continue;
3672                 /* ignore the given nids (e.g. pc-beep returns error) */
3673                 if (ignore_nids && is_in_nid_list(nid, ignore_nids))
3674                         continue;
3675
3676                 def_conf = snd_hda_codec_get_pincfg(codec, nid);
3677                 if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE)
3678                         continue;
3679                 loc = get_defcfg_location(def_conf);
3680                 switch (get_defcfg_device(def_conf)) {
3681                 case AC_JACK_LINE_OUT:
3682                         seq = get_defcfg_sequence(def_conf);
3683                         assoc = get_defcfg_association(def_conf);
3684
3685                         if (!(wid_caps & AC_WCAP_STEREO))
3686                                 if (!cfg->mono_out_pin)
3687                                         cfg->mono_out_pin = nid;
3688                         if (!assoc)
3689                                 continue;
3690                         if (!assoc_line_out)
3691                                 assoc_line_out = assoc;
3692                         else if (assoc_line_out != assoc)
3693                                 continue;
3694                         if (cfg->line_outs >= ARRAY_SIZE(cfg->line_out_pins))
3695                                 continue;
3696                         cfg->line_out_pins[cfg->line_outs] = nid;
3697                         sequences_line_out[cfg->line_outs] = seq;
3698                         cfg->line_outs++;
3699                         break;
3700                 case AC_JACK_SPEAKER:
3701                         seq = get_defcfg_sequence(def_conf);
3702                         assoc = get_defcfg_association(def_conf);
3703                         if (! assoc)
3704                                 continue;
3705                         if (! assoc_speaker)
3706                                 assoc_speaker = assoc;
3707                         else if (assoc_speaker != assoc)
3708                                 continue;
3709                         if (cfg->speaker_outs >= ARRAY_SIZE(cfg->speaker_pins))
3710                                 continue;
3711                         cfg->speaker_pins[cfg->speaker_outs] = nid;
3712                         sequences_speaker[cfg->speaker_outs] = seq;
3713                         cfg->speaker_outs++;
3714                         break;
3715                 case AC_JACK_HP_OUT:
3716                         seq = get_defcfg_sequence(def_conf);
3717                         assoc = get_defcfg_association(def_conf);
3718                         if (cfg->hp_outs >= ARRAY_SIZE(cfg->hp_pins))
3719                                 continue;
3720                         cfg->hp_pins[cfg->hp_outs] = nid;
3721                         sequences_hp[cfg->hp_outs] = (assoc << 4) | seq;
3722                         cfg->hp_outs++;
3723                         break;
3724                 case AC_JACK_MIC_IN: {
3725                         int preferred, alt;
3726                         if (loc == AC_JACK_LOC_FRONT) {
3727                                 preferred = AUTO_PIN_FRONT_MIC;
3728                                 alt = AUTO_PIN_MIC;
3729                         } else {
3730                                 preferred = AUTO_PIN_MIC;
3731                                 alt = AUTO_PIN_FRONT_MIC;
3732                         }
3733                         if (!cfg->input_pins[preferred])
3734                                 cfg->input_pins[preferred] = nid;
3735                         else if (!cfg->input_pins[alt])
3736                                 cfg->input_pins[alt] = nid;
3737                         break;
3738                 }
3739                 case AC_JACK_LINE_IN:
3740                         if (loc == AC_JACK_LOC_FRONT)
3741                                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = nid;
3742                         else
3743                                 cfg->input_pins[AUTO_PIN_LINE] = nid;
3744                         break;
3745                 case AC_JACK_CD:
3746                         cfg->input_pins[AUTO_PIN_CD] = nid;
3747                         break;
3748                 case AC_JACK_AUX:
3749                         cfg->input_pins[AUTO_PIN_AUX] = nid;
3750                         break;
3751                 case AC_JACK_SPDIF_OUT:
3752                 case AC_JACK_DIG_OTHER_OUT:
3753                         if (cfg->dig_outs >= ARRAY_SIZE(cfg->dig_out_pins))
3754                                 continue;
3755                         cfg->dig_out_pins[cfg->dig_outs] = nid;
3756                         cfg->dig_out_type[cfg->dig_outs] =
3757                                 (loc == AC_JACK_LOC_HDMI) ?
3758                                 HDA_PCM_TYPE_HDMI : HDA_PCM_TYPE_SPDIF;
3759                         cfg->dig_outs++;
3760                         break;
3761                 case AC_JACK_SPDIF_IN:
3762                 case AC_JACK_DIG_OTHER_IN:
3763                         cfg->dig_in_pin = nid;
3764                         if (loc == AC_JACK_LOC_HDMI)
3765                                 cfg->dig_in_type = HDA_PCM_TYPE_HDMI;
3766                         else
3767                                 cfg->dig_in_type = HDA_PCM_TYPE_SPDIF;
3768                         break;
3769                 }
3770         }
3771
3772         /* FIX-UP:
3773          * If no line-out is defined but multiple HPs are found,
3774          * some of them might be the real line-outs.
3775          */
3776         if (!cfg->line_outs && cfg->hp_outs > 1) {
3777                 int i = 0;
3778                 while (i < cfg->hp_outs) {
3779                         /* The real HPs should have the sequence 0x0f */
3780                         if ((sequences_hp[i] & 0x0f) == 0x0f) {
3781                                 i++;
3782                                 continue;
3783                         }
3784                         /* Move it to the line-out table */
3785                         cfg->line_out_pins[cfg->line_outs] = cfg->hp_pins[i];
3786                         sequences_line_out[cfg->line_outs] = sequences_hp[i];
3787                         cfg->line_outs++;
3788                         cfg->hp_outs--;
3789                         memmove(cfg->hp_pins + i, cfg->hp_pins + i + 1,
3790                                 sizeof(cfg->hp_pins[0]) * (cfg->hp_outs - i));
3791                         memmove(sequences_hp + i - 1, sequences_hp + i,
3792                                 sizeof(sequences_hp[0]) * (cfg->hp_outs - i));
3793                 }
3794         }
3795
3796         /* sort by sequence */
3797         sort_pins_by_sequence(cfg->line_out_pins, sequences_line_out,
3798                               cfg->line_outs);
3799         sort_pins_by_sequence(cfg->speaker_pins, sequences_speaker,
3800                               cfg->speaker_outs);
3801         sort_pins_by_sequence(cfg->hp_pins, sequences_hp,
3802                               cfg->hp_outs);
3803         
3804         /* if we have only one mic, make it AUTO_PIN_MIC */
3805         if (!cfg->input_pins[AUTO_PIN_MIC] &&
3806             cfg->input_pins[AUTO_PIN_FRONT_MIC]) {
3807                 cfg->input_pins[AUTO_PIN_MIC] =
3808                         cfg->input_pins[AUTO_PIN_FRONT_MIC];
3809                 cfg->input_pins[AUTO_PIN_FRONT_MIC] = 0;
3810         }
3811         /* ditto for line-in */
3812         if (!cfg->input_pins[AUTO_PIN_LINE] &&
3813             cfg->input_pins[AUTO_PIN_FRONT_LINE]) {
3814                 cfg->input_pins[AUTO_PIN_LINE] =
3815                         cfg->input_pins[AUTO_PIN_FRONT_LINE];
3816                 cfg->input_pins[AUTO_PIN_FRONT_LINE] = 0;
3817         }
3818
3819         /*
3820          * FIX-UP: if no line-outs are detected, try to use speaker or HP pin
3821          * as a primary output
3822          */
3823         if (!cfg->line_outs) {
3824                 if (cfg->speaker_outs) {
3825                         cfg->line_outs = cfg->speaker_outs;
3826                         memcpy(cfg->line_out_pins, cfg->speaker_pins,
3827                                sizeof(cfg->speaker_pins));
3828                         cfg->speaker_outs = 0;
3829                         memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
3830                         cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
3831                 } else if (cfg->hp_outs) {
3832                         cfg->line_outs = cfg->hp_outs;
3833                         memcpy(cfg->line_out_pins, cfg->hp_pins,
3834                                sizeof(cfg->hp_pins));
3835                         cfg->hp_outs = 0;
3836                         memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
3837                         cfg->line_out_type = AUTO_PIN_HP_OUT;
3838                 }
3839         }
3840
3841         /* Reorder the surround channels
3842          * ALSA sequence is front/surr/clfe/side
3843          * HDA sequence is:
3844          *    4-ch: front/surr  =>  OK as it is
3845          *    6-ch: front/clfe/surr
3846          *    8-ch: front/clfe/rear/side|fc
3847          */
3848         switch (cfg->line_outs) {
3849         case 3:
3850         case 4:
3851                 nid = cfg->line_out_pins[1];
3852                 cfg->line_out_pins[1] = cfg->line_out_pins[2];
3853                 cfg->line_out_pins[2] = nid;
3854                 break;
3855         }
3856
3857         /*
3858          * debug prints of the parsed results
3859          */
3860         snd_printd("autoconfig: line_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3861                    cfg->line_outs, cfg->line_out_pins[0], cfg->line_out_pins[1],
3862                    cfg->line_out_pins[2], cfg->line_out_pins[3],
3863                    cfg->line_out_pins[4]);
3864         snd_printd("   speaker_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3865                    cfg->speaker_outs, cfg->speaker_pins[0],
3866                    cfg->speaker_pins[1], cfg->speaker_pins[2],
3867                    cfg->speaker_pins[3], cfg->speaker_pins[4]);
3868         snd_printd("   hp_outs=%d (0x%x/0x%x/0x%x/0x%x/0x%x)\n",
3869                    cfg->hp_outs, cfg->hp_pins[0],
3870                    cfg->hp_pins[1], cfg->hp_pins[2],
3871                    cfg->hp_pins[3], cfg->hp_pins[4]);
3872         snd_printd("   mono: mono_out=0x%x\n", cfg->mono_out_pin);
3873         if (cfg->dig_outs)
3874                 snd_printd("   dig-out=0x%x/0x%x\n",
3875                            cfg->dig_out_pins[0], cfg->dig_out_pins[1]);
3876         snd_printd("   inputs: mic=0x%x, fmic=0x%x, line=0x%x, fline=0x%x,"
3877                    " cd=0x%x, aux=0x%x\n",
3878                    cfg->input_pins[AUTO_PIN_MIC],
3879                    cfg->input_pins[AUTO_PIN_FRONT_MIC],
3880                    cfg->input_pins[AUTO_PIN_LINE],
3881                    cfg->input_pins[AUTO_PIN_FRONT_LINE],
3882                    cfg->input_pins[AUTO_PIN_CD],
3883                    cfg->input_pins[AUTO_PIN_AUX]);
3884         if (cfg->dig_in_pin)
3885                 snd_printd("   dig-in=0x%x\n", cfg->dig_in_pin);
3886
3887         return 0;
3888 }
3889 EXPORT_SYMBOL_HDA(snd_hda_parse_pin_def_config);
3890
3891 /* labels for input pins */
3892 const char *auto_pin_cfg_labels[AUTO_PIN_LAST] = {
3893         "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
3894 };
3895 EXPORT_SYMBOL_HDA(auto_pin_cfg_labels);
3896
3897
3898 #ifdef CONFIG_PM
3899 /*
3900  * power management
3901  */
3902
3903 /**
3904  * snd_hda_suspend - suspend the codecs
3905  * @bus: the HDA bus
3906  *
3907  * Returns 0 if successful.
3908  */
3909 int snd_hda_suspend(struct hda_bus *bus)
3910 {
3911         struct hda_codec *codec;
3912
3913         list_for_each_entry(codec, &bus->codec_list, list) {
3914 #ifdef CONFIG_SND_HDA_POWER_SAVE
3915                 if (!codec->power_on)
3916                         continue;
3917 #endif
3918                 hda_call_codec_suspend(codec);
3919         }
3920         return 0;
3921 }
3922 EXPORT_SYMBOL_HDA(snd_hda_suspend);
3923
3924 /**
3925  * snd_hda_resume - resume the codecs
3926  * @bus: the HDA bus
3927  *
3928  * Returns 0 if successful.
3929  *
3930  * This fucntion is defined only when POWER_SAVE isn't set.
3931  * In the power-save mode, the codec is resumed dynamically.
3932  */
3933 int snd_hda_resume(struct hda_bus *bus)
3934 {
3935         struct hda_codec *codec;
3936
3937         list_for_each_entry(codec, &bus->codec_list, list) {
3938                 if (snd_hda_codec_needs_resume(codec))
3939                         hda_call_codec_resume(codec);
3940         }
3941         return 0;
3942 }
3943 EXPORT_SYMBOL_HDA(snd_hda_resume);
3944 #endif /* CONFIG_PM */
3945
3946 /*
3947  * generic arrays
3948  */
3949
3950 /* get a new element from the given array
3951  * if it exceeds the pre-allocated array size, re-allocate the array
3952  */
3953 void *snd_array_new(struct snd_array *array)
3954 {
3955         if (array->used >= array->alloced) {
3956                 int num = array->alloced + array->alloc_align;
3957                 void *nlist;
3958                 if (snd_BUG_ON(num >= 4096))
3959                         return NULL;
3960                 nlist = kcalloc(num + 1, array->elem_size, GFP_KERNEL);
3961                 if (!nlist)
3962                         return NULL;
3963                 if (array->list) {
3964                         memcpy(nlist, array->list,
3965                                array->elem_size * array->alloced);
3966                         kfree(array->list);
3967                 }
3968                 array->list = nlist;
3969                 array->alloced = num;
3970         }
3971         return snd_array_elem(array, array->used++);
3972 }
3973 EXPORT_SYMBOL_HDA(snd_array_new);
3974
3975 /* free the given array elements */
3976 void snd_array_free(struct snd_array *array)
3977 {
3978         kfree(array->list);
3979         array->used = 0;
3980         array->alloced = 0;
3981         array->list = NULL;
3982 }
3983 EXPORT_SYMBOL_HDA(snd_array_free);
3984
3985 /*
3986  * used by hda_proc.c and hda_eld.c
3987  */
3988 void snd_print_pcm_rates(int pcm, char *buf, int buflen)
3989 {
3990         static unsigned int rates[] = {
3991                 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
3992                 96000, 176400, 192000, 384000
3993         };
3994         int i, j;
3995
3996         for (i = 0, j = 0; i < ARRAY_SIZE(rates); i++)
3997                 if (pcm & (1 << i))
3998                         j += snprintf(buf + j, buflen - j,  " %d", rates[i]);
3999
4000         buf[j] = '\0'; /* necessary when j == 0 */
4001 }
4002 EXPORT_SYMBOL_HDA(snd_print_pcm_rates);
4003
4004 void snd_print_pcm_bits(int pcm, char *buf, int buflen)
4005 {
4006         static unsigned int bits[] = { 8, 16, 20, 24, 32 };
4007         int i, j;
4008
4009         for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
4010                 if (pcm & (AC_SUPPCM_BITS_8 << i))
4011                         j += snprintf(buf + j, buflen - j,  " %d", bits[i]);
4012
4013         buf[j] = '\0'; /* necessary when j == 0 */
4014 }
4015 EXPORT_SYMBOL_HDA(snd_print_pcm_bits);
4016
4017 MODULE_DESCRIPTION("HDA codec core");
4018 MODULE_LICENSE("GPL");