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