[Bluetooth] Fix reference count when connection lookup fails
[pandora-kernel.git] / sound / isa / es18xx.c
1 /*
2  *  Driver for generic ESS AudioDrive ES18xx soundcards
3  *  Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4  *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
5  *
6  *
7  *   This program 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 program 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 /* GENERAL NOTES:
23  *
24  * BUGS:
25  * - There are pops (we can't delay in trigger function, cause midlevel 
26  *   often need to trigger down and then up very quickly).
27  *   Any ideas?
28  * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
29  */
30
31 /*
32  * ES1868  NOTES:
33  * - The chip has one half duplex pcm (with very limited full duplex support).
34  *
35  * - Duplex stereophonic sound is impossible.
36  * - Record and playback must share the same frequency rate.
37  *
38  * - The driver use dma2 for playback and dma1 for capture.
39  */
40
41 /*
42  * ES1869 NOTES:
43  *
44  * - there are a first full duplex pcm and a second playback only pcm
45  *   (incompatible with first pcm capture)
46  * 
47  * - there is support for the capture volume and ESS Spatializer 3D effect.
48  *
49  * - contrarily to some pages in DS_1869.PDF the rates can be set
50  *   independently.
51  *
52  * - Zoom Video is implemented by sharing the FM DAC, thus the user can
53  *   have either FM playback or Video playback but not both simultaneously.
54  *   The Video Playback Switch mixer control toggles this choice.
55  *
56  * BUGS:
57  *
58  * - There is a major trouble I noted:
59  *
60  *   using both channel for playback stereo 16 bit samples at 44100 Hz
61  *   the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
62  *   
63  *   The same happens using Audio1 for captureing.
64  *
65  *   The Windows driver does not suffer of this (although it use Audio1
66  *   only for captureing). I'm unable to discover why.
67  *
68  */
69
70 /*
71  * ES1879 NOTES:
72  * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
73  *   seems to be effected (speaker_test plays a lower frequency). Can't find
74  *   anything in the datasheet to account for this, so a Video Playback Switch
75  *   control has been included to allow ZV to be enabled only when necessary.
76  *   Then again on at least one test system the 0x71 bit 6 enable bit is not 
77  *   needed for ZV, so maybe the datasheet is entirely wrong here.
78  */
79  
80 #include <sound/driver.h>
81 #include <linux/init.h>
82 #include <linux/err.h>
83 #include <linux/platform_device.h>
84 #include <linux/slab.h>
85 #include <linux/pnp.h>
86 #include <linux/isapnp.h>
87 #include <linux/moduleparam.h>
88 #include <linux/delay.h>
89
90 #include <asm/io.h>
91 #include <asm/dma.h>
92 #include <sound/core.h>
93 #include <sound/control.h>
94 #include <sound/pcm.h>
95 #include <sound/pcm_params.h>
96 #include <sound/mpu401.h>
97 #include <sound/opl3.h>
98 #define SNDRV_LEGACY_FIND_FREE_IRQ
99 #define SNDRV_LEGACY_FIND_FREE_DMA
100 #include <sound/initval.h>
101
102 #define PFX "es18xx: "
103
104 struct snd_es18xx {
105         unsigned long port;             /* port of ESS chip */
106         unsigned long mpu_port;         /* MPU-401 port of ESS chip */
107         unsigned long fm_port;          /* FM port */
108         unsigned long ctrl_port;        /* Control port of ESS chip */
109         struct resource *res_port;
110         struct resource *res_mpu_port;
111         struct resource *res_ctrl_port;
112         int irq;                        /* IRQ number of ESS chip */
113         int dma1;                       /* DMA1 */
114         int dma2;                       /* DMA2 */
115         unsigned short version;         /* version of ESS chip */
116         int caps;                       /* Chip capabilities */
117         unsigned short audio2_vol;      /* volume level of audio2 */
118
119         unsigned short active;          /* active channel mask */
120         unsigned int dma1_size;
121         unsigned int dma2_size;
122         unsigned int dma1_shift;
123         unsigned int dma2_shift;
124
125         struct snd_card *card;
126         struct snd_pcm *pcm;
127         struct snd_pcm_substream *playback_a_substream;
128         struct snd_pcm_substream *capture_a_substream;
129         struct snd_pcm_substream *playback_b_substream;
130
131         struct snd_rawmidi *rmidi;
132
133         struct snd_kcontrol *hw_volume;
134         struct snd_kcontrol *hw_switch;
135         struct snd_kcontrol *master_volume;
136         struct snd_kcontrol *master_switch;
137
138         spinlock_t reg_lock;
139         spinlock_t mixer_lock;
140         spinlock_t ctrl_lock;
141 #ifdef CONFIG_PM
142         unsigned char pm_reg;
143 #endif
144 };
145
146 struct snd_audiodrive {
147         struct snd_es18xx *chip;
148 #ifdef CONFIG_PNP
149         struct pnp_dev *dev;
150         struct pnp_dev *devc;
151 #endif
152 };
153
154 #define AUDIO1_IRQ      0x01
155 #define AUDIO2_IRQ      0x02
156 #define HWV_IRQ         0x04
157 #define MPU_IRQ         0x08
158
159 #define ES18XX_PCM2     0x0001  /* Has two useable PCM */
160 #define ES18XX_SPATIALIZER 0x0002       /* Has 3D Spatializer */
161 #define ES18XX_RECMIX   0x0004  /* Has record mixer */
162 #define ES18XX_DUPLEX_MONO 0x0008       /* Has mono duplex only */
163 #define ES18XX_DUPLEX_SAME 0x0010       /* Playback and record must share the same rate */
164 #define ES18XX_NEW_RATE 0x0020  /* More precise rate setting */
165 #define ES18XX_AUXB     0x0040  /* AuxB mixer control */
166 #define ES18XX_HWV      0x0080  /* Has seperate hardware volume mixer controls*/
167 #define ES18XX_MONO     0x0100  /* Mono_in mixer control */
168 #define ES18XX_I2S      0x0200  /* I2S mixer control */
169 #define ES18XX_MUTEREC  0x0400  /* Record source can be muted */
170 #define ES18XX_CONTROL  0x0800  /* Has control ports */
171
172 /* Power Management */
173 #define ES18XX_PM       0x07
174 #define ES18XX_PM_GPO0  0x01
175 #define ES18XX_PM_GPO1  0x02
176 #define ES18XX_PM_PDR   0x04
177 #define ES18XX_PM_ANA   0x08
178 #define ES18XX_PM_FM    0x020
179 #define ES18XX_PM_SUS   0x080
180
181 /* Lowlevel */
182
183 #define DAC1 0x01
184 #define ADC1 0x02
185 #define DAC2 0x04
186 #define MILLISECOND 10000
187
188 static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
189 {
190         int i;
191
192         for(i = MILLISECOND; i; i--)
193                 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
194                         outb(val, chip->port + 0x0C);
195                         return 0;
196                 }
197         snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
198         return -EINVAL;
199 }
200
201 static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
202 {
203         int i;
204
205         for(i = MILLISECOND/10; i; i--)
206                 if (inb(chip->port + 0x0C) & 0x40)
207                         return inb(chip->port + 0x0A);
208         snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
209                    chip->port + 0x0A, inb(chip->port + 0x0A));
210         return -ENODEV;
211 }
212
213 #undef REG_DEBUG
214
215 static int snd_es18xx_write(struct snd_es18xx *chip,
216                             unsigned char reg, unsigned char data)
217 {
218         unsigned long flags;
219         int ret;
220         
221         spin_lock_irqsave(&chip->reg_lock, flags);
222         ret = snd_es18xx_dsp_command(chip, reg);
223         if (ret < 0)
224                 goto end;
225         ret = snd_es18xx_dsp_command(chip, data);
226  end:
227         spin_unlock_irqrestore(&chip->reg_lock, flags);
228 #ifdef REG_DEBUG
229         snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
230 #endif
231         return ret;
232 }
233
234 static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
235 {
236         unsigned long flags;
237         int ret, data;
238         spin_lock_irqsave(&chip->reg_lock, flags);
239         ret = snd_es18xx_dsp_command(chip, 0xC0);
240         if (ret < 0)
241                 goto end;
242         ret = snd_es18xx_dsp_command(chip, reg);
243         if (ret < 0)
244                 goto end;
245         data = snd_es18xx_dsp_get_byte(chip);
246         ret = data;
247 #ifdef REG_DEBUG
248         snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
249 #endif
250  end:
251         spin_unlock_irqrestore(&chip->reg_lock, flags);
252         return ret;
253 }
254
255 /* Return old value */
256 static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
257                            unsigned char mask, unsigned char val)
258 {
259         int ret;
260         unsigned char old, new, oval;
261         unsigned long flags;
262         spin_lock_irqsave(&chip->reg_lock, flags);
263         ret = snd_es18xx_dsp_command(chip, 0xC0);
264         if (ret < 0)
265                 goto end;
266         ret = snd_es18xx_dsp_command(chip, reg);
267         if (ret < 0)
268                 goto end;
269         ret = snd_es18xx_dsp_get_byte(chip);
270         if (ret < 0) {
271                 goto end;
272         }
273         old = ret;
274         oval = old & mask;
275         if (val != oval) {
276                 ret = snd_es18xx_dsp_command(chip, reg);
277                 if (ret < 0)
278                         goto end;
279                 new = (old & ~mask) | (val & mask);
280                 ret = snd_es18xx_dsp_command(chip, new);
281                 if (ret < 0)
282                         goto end;
283 #ifdef REG_DEBUG
284                 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
285                            reg, old, new, ret);
286 #endif
287         }
288         ret = oval;
289  end:
290         spin_unlock_irqrestore(&chip->reg_lock, flags);
291         return ret;
292 }
293
294 static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
295                             unsigned char reg, unsigned char data)
296 {
297         unsigned long flags;
298         spin_lock_irqsave(&chip->mixer_lock, flags);
299         outb(reg, chip->port + 0x04);
300         outb(data, chip->port + 0x05);
301         spin_unlock_irqrestore(&chip->mixer_lock, flags);
302 #ifdef REG_DEBUG
303         snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
304 #endif
305 }
306
307 static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
308 {
309         unsigned long flags;
310         int data;
311         spin_lock_irqsave(&chip->mixer_lock, flags);
312         outb(reg, chip->port + 0x04);
313         data = inb(chip->port + 0x05);
314         spin_unlock_irqrestore(&chip->mixer_lock, flags);
315 #ifdef REG_DEBUG
316         snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
317 #endif
318         return data;
319 }
320
321 /* Return old value */
322 static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
323                                         unsigned char mask, unsigned char val)
324 {
325         unsigned char old, new, oval;
326         unsigned long flags;
327         spin_lock_irqsave(&chip->mixer_lock, flags);
328         outb(reg, chip->port + 0x04);
329         old = inb(chip->port + 0x05);
330         oval = old & mask;
331         if (val != oval) {
332                 new = (old & ~mask) | (val & mask);
333                 outb(new, chip->port + 0x05);
334 #ifdef REG_DEBUG
335                 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
336                            reg, old, new);
337 #endif
338         }
339         spin_unlock_irqrestore(&chip->mixer_lock, flags);
340         return oval;
341 }
342
343 static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
344                                             unsigned char mask)
345 {
346         int old, expected, new;
347         unsigned long flags;
348         spin_lock_irqsave(&chip->mixer_lock, flags);
349         outb(reg, chip->port + 0x04);
350         old = inb(chip->port + 0x05);
351         expected = old ^ mask;
352         outb(expected, chip->port + 0x05);
353         new = inb(chip->port + 0x05);
354         spin_unlock_irqrestore(&chip->mixer_lock, flags);
355 #ifdef REG_DEBUG
356         snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
357                    reg, old, expected, new);
358 #endif
359         return expected == new;
360 }
361
362
363 static int snd_es18xx_reset(struct snd_es18xx *chip)
364 {
365         int i;
366         outb(0x03, chip->port + 0x06);
367         inb(chip->port + 0x06);
368         outb(0x00, chip->port + 0x06);
369         for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
370         if (inb(chip->port + 0x0A) != 0xAA)
371                 return -1;
372         return 0;
373 }
374
375 static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
376 {
377         outb(0x02, chip->port + 0x06);
378         inb(chip->port + 0x06);
379         outb(0x00, chip->port + 0x06);
380         return 0;
381 }
382
383 static struct snd_ratnum new_clocks[2] = {
384         {
385                 .num = 793800,
386                 .den_min = 1,
387                 .den_max = 128,
388                 .den_step = 1,
389         },
390         {
391                 .num = 768000,
392                 .den_min = 1,
393                 .den_max = 128,
394                 .den_step = 1,
395         }
396 };
397
398 static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
399         .nrats = 2,
400         .rats = new_clocks,
401 };
402
403 static struct snd_ratnum old_clocks[2] = {
404         {
405                 .num = 795444,
406                 .den_min = 1,
407                 .den_max = 128,
408                 .den_step = 1,
409         },
410         {
411                 .num = 397722,
412                 .den_min = 1,
413                 .den_max = 128,
414                 .den_step = 1,
415         }
416 };
417
418 static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks  = {
419         .nrats = 2,
420         .rats = old_clocks,
421 };
422
423
424 static void snd_es18xx_rate_set(struct snd_es18xx *chip, 
425                                 struct snd_pcm_substream *substream,
426                                 int mode)
427 {
428         unsigned int bits, div0;
429         struct snd_pcm_runtime *runtime = substream->runtime;
430         if (chip->caps & ES18XX_NEW_RATE) {
431                 if (runtime->rate_num == new_clocks[0].num)
432                         bits = 128 - runtime->rate_den;
433                 else
434                         bits = 256 - runtime->rate_den;
435         } else {
436                 if (runtime->rate_num == old_clocks[0].num)
437                         bits = 256 - runtime->rate_den;
438                 else
439                         bits = 128 - runtime->rate_den;
440         }
441
442         /* set filter register */
443         div0 = 256 - 7160000*20/(8*82*runtime->rate);
444                 
445         if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
446                 snd_es18xx_mixer_write(chip, 0x70, bits);
447                 /*
448                  * Comment from kernel oss driver:
449                  * FKS: fascinating: 0x72 doesn't seem to work.
450                  */
451                 snd_es18xx_write(chip, 0xA2, div0);
452                 snd_es18xx_mixer_write(chip, 0x72, div0);
453         } else {
454                 snd_es18xx_write(chip, 0xA1, bits);
455                 snd_es18xx_write(chip, 0xA2, div0);
456         }
457 }
458
459 static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
460                                          struct snd_pcm_hw_params *hw_params)
461 {
462         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
463         int shift, err;
464
465         shift = 0;
466         if (params_channels(hw_params) == 2)
467                 shift++;
468         if (snd_pcm_format_width(params_format(hw_params)) == 16)
469                 shift++;
470
471         if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
472                 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
473                     (chip->capture_a_substream) &&
474                     params_channels(hw_params) != 1) {
475                         _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
476                         return -EBUSY;
477                 }
478                 chip->dma2_shift = shift;
479         } else {
480                 chip->dma1_shift = shift;
481         }
482         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
483                 return err;
484         return 0;
485 }
486
487 static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
488 {
489         return snd_pcm_lib_free_pages(substream);
490 }
491
492 static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
493                                         struct snd_pcm_substream *substream)
494 {
495         struct snd_pcm_runtime *runtime = substream->runtime;
496         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
497         unsigned int count = snd_pcm_lib_period_bytes(substream);
498
499         chip->dma2_size = size;
500
501         snd_es18xx_rate_set(chip, substream, DAC2);
502
503         /* Transfer Count Reload */
504         count = 0x10000 - count;
505         snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
506         snd_es18xx_mixer_write(chip, 0x76, count >> 8);
507
508         /* Set format */
509         snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
510                               ((runtime->channels == 1) ? 0x00 : 0x02) |
511                               (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
512                               (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
513
514         /* Set DMA controller */
515         snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
516
517         return 0;
518 }
519
520 static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
521                                         struct snd_pcm_substream *substream,
522                                         int cmd)
523 {
524         switch (cmd) {
525         case SNDRV_PCM_TRIGGER_START:
526         case SNDRV_PCM_TRIGGER_RESUME:
527                 if (chip->active & DAC2)
528                         return 0;
529                 chip->active |= DAC2;
530                 /* Start DMA */
531                 if (chip->dma2 >= 4)
532                         snd_es18xx_mixer_write(chip, 0x78, 0xb3);
533                 else
534                         snd_es18xx_mixer_write(chip, 0x78, 0x93);
535 #ifdef AVOID_POPS
536                 /* Avoid pops */
537                 udelay(100000);
538                 if (chip->caps & ES18XX_PCM2)
539                         /* Restore Audio 2 volume */
540                         snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
541                 else
542                         /* Enable PCM output */
543                         snd_es18xx_dsp_command(chip, 0xD1);
544 #endif
545                 break;
546         case SNDRV_PCM_TRIGGER_STOP:
547         case SNDRV_PCM_TRIGGER_SUSPEND:
548                 if (!(chip->active & DAC2))
549                         return 0;
550                 chip->active &= ~DAC2;
551                 /* Stop DMA */
552                 snd_es18xx_mixer_write(chip, 0x78, 0x00);
553 #ifdef AVOID_POPS
554                 udelay(25000);
555                 if (chip->caps & ES18XX_PCM2)
556                         /* Set Audio 2 volume to 0 */
557                         snd_es18xx_mixer_write(chip, 0x7C, 0);
558                 else
559                         /* Disable PCM output */
560                         snd_es18xx_dsp_command(chip, 0xD3);
561 #endif
562                 break;
563         default:
564                 return -EINVAL;
565         }
566
567         return 0;
568 }
569
570 static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
571                                         struct snd_pcm_hw_params *hw_params)
572 {
573         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
574         int shift, err;
575
576         shift = 0;
577         if ((chip->caps & ES18XX_DUPLEX_MONO) &&
578             chip->playback_a_substream &&
579             params_channels(hw_params) != 1) {
580                 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
581                 return -EBUSY;
582         }
583         if (params_channels(hw_params) == 2)
584                 shift++;
585         if (snd_pcm_format_width(params_format(hw_params)) == 16)
586                 shift++;
587         chip->dma1_shift = shift;
588         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
589                 return err;
590         return 0;
591 }
592
593 static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
594 {
595         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
596         struct snd_pcm_runtime *runtime = substream->runtime;
597         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
598         unsigned int count = snd_pcm_lib_period_bytes(substream);
599
600         chip->dma1_size = size;
601
602         snd_es18xx_reset_fifo(chip);
603
604         /* Set stereo/mono */
605         snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
606
607         snd_es18xx_rate_set(chip, substream, ADC1);
608
609         /* Transfer Count Reload */
610         count = 0x10000 - count;
611         snd_es18xx_write(chip, 0xA4, count & 0xff);
612         snd_es18xx_write(chip, 0xA5, count >> 8);
613
614 #ifdef AVOID_POPS
615         udelay(100000);
616 #endif
617
618         /* Set format */
619         snd_es18xx_write(chip, 0xB7, 
620                          snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
621         snd_es18xx_write(chip, 0xB7, 0x90 |
622                          ((runtime->channels == 1) ? 0x40 : 0x08) |
623                          (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
624                          (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
625
626         /* Set DMA controler */
627         snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
628
629         return 0;
630 }
631
632 static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
633                                       int cmd)
634 {
635         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
636
637         switch (cmd) {
638         case SNDRV_PCM_TRIGGER_START:
639         case SNDRV_PCM_TRIGGER_RESUME:
640                 if (chip->active & ADC1)
641                         return 0;
642                 chip->active |= ADC1;
643                 /* Start DMA */
644                 snd_es18xx_write(chip, 0xB8, 0x0f);
645                 break;
646         case SNDRV_PCM_TRIGGER_STOP:
647         case SNDRV_PCM_TRIGGER_SUSPEND:
648                 if (!(chip->active & ADC1))
649                         return 0;
650                 chip->active &= ~ADC1;
651                 /* Stop DMA */
652                 snd_es18xx_write(chip, 0xB8, 0x00);
653                 break;
654         default:
655                 return -EINVAL;
656         }
657
658         return 0;
659 }
660
661 static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
662                                         struct snd_pcm_substream *substream)
663 {
664         struct snd_pcm_runtime *runtime = substream->runtime;
665         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
666         unsigned int count = snd_pcm_lib_period_bytes(substream);
667
668         chip->dma1_size = size;
669
670         snd_es18xx_reset_fifo(chip);
671
672         /* Set stereo/mono */
673         snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
674
675         snd_es18xx_rate_set(chip, substream, DAC1);
676
677         /* Transfer Count Reload */
678         count = 0x10000 - count;
679         snd_es18xx_write(chip, 0xA4, count & 0xff);
680         snd_es18xx_write(chip, 0xA5, count >> 8);
681
682         /* Set format */
683         snd_es18xx_write(chip, 0xB6,
684                          snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
685         snd_es18xx_write(chip, 0xB7, 
686                          snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
687         snd_es18xx_write(chip, 0xB7, 0x90 |
688                          (runtime->channels == 1 ? 0x40 : 0x08) |
689                          (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
690                          (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
691
692         /* Set DMA controler */
693         snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
694
695         return 0;
696 }
697
698 static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
699                                         struct snd_pcm_substream *substream,
700                                         int cmd)
701 {
702         switch (cmd) {
703         case SNDRV_PCM_TRIGGER_START:
704         case SNDRV_PCM_TRIGGER_RESUME:
705                 if (chip->active & DAC1)
706                         return 0;
707                 chip->active |= DAC1;
708                 /* Start DMA */
709                 snd_es18xx_write(chip, 0xB8, 0x05);
710 #ifdef AVOID_POPS
711                 /* Avoid pops */
712                 udelay(100000);
713                 /* Enable Audio 1 */
714                 snd_es18xx_dsp_command(chip, 0xD1);
715 #endif
716                 break;
717         case SNDRV_PCM_TRIGGER_STOP:
718         case SNDRV_PCM_TRIGGER_SUSPEND:
719                 if (!(chip->active & DAC1))
720                         return 0;
721                 chip->active &= ~DAC1;
722                 /* Stop DMA */
723                 snd_es18xx_write(chip, 0xB8, 0x00);
724 #ifdef AVOID_POPS
725                 /* Avoid pops */
726                 udelay(25000);
727                 /* Disable Audio 1 */
728                 snd_es18xx_dsp_command(chip, 0xD3);
729 #endif
730                 break;
731         default:
732                 return -EINVAL;
733         }
734
735         return 0;
736 }
737
738 static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
739 {
740         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
741         if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
742                 return snd_es18xx_playback1_prepare(chip, substream);
743         else
744                 return snd_es18xx_playback2_prepare(chip, substream);
745 }
746
747 static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
748                                        int cmd)
749 {
750         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
751         if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
752                 return snd_es18xx_playback1_trigger(chip, substream, cmd);
753         else
754                 return snd_es18xx_playback2_trigger(chip, substream, cmd);
755 }
756
757 static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
758 {
759         struct snd_es18xx *chip = dev_id;
760         unsigned char status;
761
762         if (chip->caps & ES18XX_CONTROL) {
763                 /* Read Interrupt status */
764                 status = inb(chip->ctrl_port + 6);
765         } else {
766                 /* Read Interrupt status */
767                 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
768         }
769 #if 0
770         else {
771                 status = 0;
772                 if (inb(chip->port + 0x0C) & 0x01)
773                         status |= AUDIO1_IRQ;
774                 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
775                         status |= AUDIO2_IRQ;
776                 if ((chip->caps & ES18XX_HWV) &&
777                     snd_es18xx_mixer_read(chip, 0x64) & 0x10)
778                         status |= HWV_IRQ;
779         }
780 #endif
781
782         /* Audio 1 & Audio 2 */
783         if (status & AUDIO2_IRQ) {
784                 if (chip->active & DAC2)
785                         snd_pcm_period_elapsed(chip->playback_a_substream);
786                 /* ack interrupt */
787                 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
788         }
789         if (status & AUDIO1_IRQ) {
790                 /* ok.. capture is active */
791                 if (chip->active & ADC1)
792                         snd_pcm_period_elapsed(chip->capture_a_substream);
793                 /* ok.. playback2 is active */
794                 else if (chip->active & DAC1)
795                         snd_pcm_period_elapsed(chip->playback_b_substream);
796                 /* ack interrupt */
797                 inb(chip->port + 0x0E);
798         }
799
800         /* MPU */
801         if ((status & MPU_IRQ) && chip->rmidi)
802                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
803
804         /* Hardware volume */
805         if (status & HWV_IRQ) {
806                 int split = 0;
807                 if (chip->caps & ES18XX_HWV) {
808                         split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
809                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
810                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
811                 }
812                 if (!split) {
813                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
814                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
815                 }
816                 /* ack interrupt */
817                 snd_es18xx_mixer_write(chip, 0x66, 0x00);
818         }
819         return IRQ_HANDLED;
820 }
821
822 static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
823 {
824         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
825         int pos;
826
827         if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
828                 if (!(chip->active & DAC2))
829                         return 0;
830                 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
831                 return pos >> chip->dma2_shift;
832         } else {
833                 if (!(chip->active & DAC1))
834                         return 0;
835                 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
836                 return pos >> chip->dma1_shift;
837         }
838 }
839
840 static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
841 {
842         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
843         int pos;
844
845         if (!(chip->active & ADC1))
846                 return 0;
847         pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
848         return pos >> chip->dma1_shift;
849 }
850
851 static struct snd_pcm_hardware snd_es18xx_playback =
852 {
853         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
854                                  SNDRV_PCM_INFO_RESUME |
855                                  SNDRV_PCM_INFO_MMAP_VALID),
856         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | 
857                                  SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
858         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
859         .rate_min =             4000,
860         .rate_max =             48000,
861         .channels_min =         1,
862         .channels_max =         2,
863         .buffer_bytes_max =     65536,
864         .period_bytes_min =     64,
865         .period_bytes_max =     65536,
866         .periods_min =          1,
867         .periods_max =          1024,
868         .fifo_size =            0,
869 };
870
871 static struct snd_pcm_hardware snd_es18xx_capture =
872 {
873         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
874                                  SNDRV_PCM_INFO_RESUME |
875                                  SNDRV_PCM_INFO_MMAP_VALID),
876         .formats =              (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | 
877                                  SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
878         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
879         .rate_min =             4000,
880         .rate_max =             48000,
881         .channels_min =         1,
882         .channels_max =         2,
883         .buffer_bytes_max =     65536,
884         .period_bytes_min =     64,
885         .period_bytes_max =     65536,
886         .periods_min =          1,
887         .periods_max =          1024,
888         .fifo_size =            0,
889 };
890
891 static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
892 {
893         struct snd_pcm_runtime *runtime = substream->runtime;
894         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
895
896         if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
897                 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
898                     chip->capture_a_substream && 
899                     chip->capture_a_substream->runtime->channels != 1)
900                         return -EAGAIN;
901                 chip->playback_a_substream = substream;
902         } else if (substream->number <= 1) {
903                 if (chip->capture_a_substream)
904                         return -EAGAIN;
905                 chip->playback_b_substream = substream;
906         } else {
907                 snd_BUG();
908                 return -EINVAL;
909         }
910         substream->runtime->hw = snd_es18xx_playback;
911         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
912                                       (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
913         return 0;
914 }
915
916 static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
917 {
918         struct snd_pcm_runtime *runtime = substream->runtime;
919         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
920
921         if (chip->playback_b_substream)
922                 return -EAGAIN;
923         if ((chip->caps & ES18XX_DUPLEX_MONO) &&
924             chip->playback_a_substream &&
925             chip->playback_a_substream->runtime->channels != 1)
926                 return -EAGAIN;
927         chip->capture_a_substream = substream;
928         substream->runtime->hw = snd_es18xx_capture;
929         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
930                                       (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
931         return 0;
932 }
933
934 static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
935 {
936         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
937
938         if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
939                 chip->playback_a_substream = NULL;
940         else
941                 chip->playback_b_substream = NULL;
942         
943         snd_pcm_lib_free_pages(substream);
944         return 0;
945 }
946
947 static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
948 {
949         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
950
951         chip->capture_a_substream = NULL;
952         snd_pcm_lib_free_pages(substream);
953         return 0;
954 }
955
956 /*
957  *  MIXER part
958  */
959
960 /* Record source mux routines:
961  * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
962  * bit table for the 4/5 source mux:
963  * reg 1C:
964  *  b2 b1 b0   muxSource
965  *   x  0  x   microphone
966  *   0  1  x   CD
967  *   1  1  0   line
968  *   1  1  1   mixer
969  * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
970  * either the play mixer or the capture mixer.
971  *
972  * "map4Source" translates from source number to reg bit pattern
973  * "invMap4Source" translates from reg bit pattern to source number
974  */
975
976 static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
977 {
978         static char *texts4Source[4] = {
979                 "Mic", "CD", "Line", "Master"
980         };
981         static char *texts5Source[5] = {
982                 "Mic", "CD", "Line", "Master", "Mix"
983         };
984         static char *texts8Source[8] = {
985                 "Mic", "Mic Master", "CD", "AOUT",
986                 "Mic1", "Mix", "Line", "Master"
987         };
988         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
989
990         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
991         uinfo->count = 1;
992         switch (chip->version) {
993         case 0x1868:
994         case 0x1878:
995                 uinfo->value.enumerated.items = 4;
996                 if (uinfo->value.enumerated.item > 3)
997                         uinfo->value.enumerated.item = 3;
998                 strcpy(uinfo->value.enumerated.name, texts4Source[uinfo->value.enumerated.item]);
999                 break;
1000         case 0x1887:
1001         case 0x1888:
1002                 uinfo->value.enumerated.items = 5;
1003                 if (uinfo->value.enumerated.item > 4)
1004                         uinfo->value.enumerated.item = 4;
1005                 strcpy(uinfo->value.enumerated.name, texts5Source[uinfo->value.enumerated.item]);
1006                 break;
1007         case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
1008         case 0x1879:
1009                 uinfo->value.enumerated.items = 8;
1010                 if (uinfo->value.enumerated.item > 7)
1011                         uinfo->value.enumerated.item = 7;
1012                 strcpy(uinfo->value.enumerated.name, texts8Source[uinfo->value.enumerated.item]);
1013                 break;
1014         default:
1015                 return -EINVAL;
1016         }
1017         return 0;
1018 }
1019
1020 static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1021 {
1022         static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
1023         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1024         int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
1025         if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
1026                 muxSource = invMap4Source[muxSource];
1027                 if (muxSource==3 && 
1028                     (chip->version == 0x1887 || chip->version == 0x1888) &&
1029                     (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
1030                 ) 
1031                         muxSource = 4;
1032         }
1033         ucontrol->value.enumerated.item[0] = muxSource;
1034         return 0;
1035 }
1036
1037 static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1038 {
1039         static unsigned char map4Source[4] = {0, 2, 6, 7};
1040         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1041         unsigned char val = ucontrol->value.enumerated.item[0];
1042         unsigned char retVal = 0;
1043
1044         switch (chip->version) {
1045  /* 5 source chips */
1046         case 0x1887:
1047         case 0x1888:
1048                 if (val > 4)
1049                         return -EINVAL;
1050                 if (val == 4) {
1051                         retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
1052                         val = 3;
1053                 } else
1054                         retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
1055  /* 4 source chips */
1056         case 0x1868:
1057         case 0x1878:
1058                 if (val > 3)
1059                         return -EINVAL;
1060                 val = map4Source[val];
1061                 break;
1062  /* 8 source chips */
1063         case 0x1869:
1064         case 0x1879:
1065                 if (val > 7)
1066                         return -EINVAL;
1067                 break;
1068         default:
1069                 return -EINVAL;
1070         }
1071         return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
1072 }
1073
1074 static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1075 {
1076         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1077         uinfo->count = 1;
1078         uinfo->value.integer.min = 0;
1079         uinfo->value.integer.max = 1;
1080         return 0;
1081 }
1082
1083 static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1084 {
1085         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1086         unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
1087         ucontrol->value.integer.value[0] = !!(val & 8);
1088         return 0;
1089 }
1090
1091 static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1092 {
1093         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1094         unsigned char oval, nval;
1095         int change;
1096         nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1097         oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
1098         change = nval != oval;
1099         if (change) {
1100                 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1101                 snd_es18xx_mixer_write(chip, 0x50, nval);
1102         }
1103         return change;
1104 }
1105
1106 static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1107 {
1108         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1109         uinfo->count = 2;
1110         uinfo->value.integer.min = 0;
1111         uinfo->value.integer.max = 63;
1112         return 0;
1113 }
1114
1115 static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1116 {
1117         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1118         ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1119         ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1120         return 0;
1121 }
1122
1123 static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1124 {
1125         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1126         uinfo->count = 2;
1127         uinfo->value.integer.min = 0;
1128         uinfo->value.integer.max = 1;
1129         return 0;
1130 }
1131
1132 static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1133 {
1134         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1135         ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1136         ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1137         return 0;
1138 }
1139
1140 static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
1141 {
1142         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1143         chip->master_volume = NULL;
1144         chip->master_switch = NULL;
1145         chip->hw_volume = NULL;
1146         chip->hw_switch = NULL;
1147 }
1148
1149 static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
1150                                unsigned char mask, unsigned char val)
1151 {
1152         if (reg < 0xa0)
1153                 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1154         else
1155                 return snd_es18xx_bits(chip, reg, mask, val);
1156 }
1157
1158 static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
1159 {
1160         if (reg < 0xa0)
1161                 return snd_es18xx_mixer_read(chip, reg);
1162         else
1163                 return snd_es18xx_read(chip, reg);
1164 }
1165
1166 #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1167 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1168   .info = snd_es18xx_info_single, \
1169   .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1170   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1171
1172 static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1173 {
1174         int mask = (kcontrol->private_value >> 16) & 0xff;
1175
1176         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1177         uinfo->count = 1;
1178         uinfo->value.integer.min = 0;
1179         uinfo->value.integer.max = mask;
1180         return 0;
1181 }
1182
1183 static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1184 {
1185         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1186         int reg = kcontrol->private_value & 0xff;
1187         int shift = (kcontrol->private_value >> 8) & 0xff;
1188         int mask = (kcontrol->private_value >> 16) & 0xff;
1189         int invert = (kcontrol->private_value >> 24) & 0xff;
1190         int val;
1191         
1192         val = snd_es18xx_reg_read(chip, reg);
1193         ucontrol->value.integer.value[0] = (val >> shift) & mask;
1194         if (invert)
1195                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1196         return 0;
1197 }
1198
1199 static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1200 {
1201         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1202         int reg = kcontrol->private_value & 0xff;
1203         int shift = (kcontrol->private_value >> 8) & 0xff;
1204         int mask = (kcontrol->private_value >> 16) & 0xff;
1205         int invert = (kcontrol->private_value >> 24) & 0xff;
1206         unsigned char val;
1207         
1208         val = (ucontrol->value.integer.value[0] & mask);
1209         if (invert)
1210                 val = mask - val;
1211         mask <<= shift;
1212         val <<= shift;
1213         return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1214 }
1215
1216 #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1217 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1218   .info = snd_es18xx_info_double, \
1219   .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1220   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1221
1222 static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1223 {
1224         int mask = (kcontrol->private_value >> 24) & 0xff;
1225
1226         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1227         uinfo->count = 2;
1228         uinfo->value.integer.min = 0;
1229         uinfo->value.integer.max = mask;
1230         return 0;
1231 }
1232
1233 static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1234 {
1235         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1236         int left_reg = kcontrol->private_value & 0xff;
1237         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1238         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1239         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1240         int mask = (kcontrol->private_value >> 24) & 0xff;
1241         int invert = (kcontrol->private_value >> 22) & 1;
1242         unsigned char left, right;
1243         
1244         left = snd_es18xx_reg_read(chip, left_reg);
1245         if (left_reg != right_reg)
1246                 right = snd_es18xx_reg_read(chip, right_reg);
1247         else
1248                 right = left;
1249         ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1250         ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1251         if (invert) {
1252                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1253                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1254         }
1255         return 0;
1256 }
1257
1258 static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1259 {
1260         struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1261         int left_reg = kcontrol->private_value & 0xff;
1262         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1263         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1264         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1265         int mask = (kcontrol->private_value >> 24) & 0xff;
1266         int invert = (kcontrol->private_value >> 22) & 1;
1267         int change;
1268         unsigned char val1, val2, mask1, mask2;
1269         
1270         val1 = ucontrol->value.integer.value[0] & mask;
1271         val2 = ucontrol->value.integer.value[1] & mask;
1272         if (invert) {
1273                 val1 = mask - val1;
1274                 val2 = mask - val2;
1275         }
1276         val1 <<= shift_left;
1277         val2 <<= shift_right;
1278         mask1 = mask << shift_left;
1279         mask2 = mask << shift_right;
1280         if (left_reg != right_reg) {
1281                 change = 0;
1282                 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1283                         change = 1;
1284                 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1285                         change = 1;
1286         } else {
1287                 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2, 
1288                                               val1 | val2) != (val1 | val2));
1289         }
1290         return change;
1291 }
1292
1293 /* Mixer controls
1294  * These arrays contain setup data for mixer controls.
1295  * 
1296  * The controls that are universal to all chipsets are fully initialized
1297  * here.
1298  */
1299 static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
1300 ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1301 ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1302 ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1303 ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1304 ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1305 ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1306 ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1307 ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1308 ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1309 {
1310         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1311         .name = "Capture Source",
1312         .info = snd_es18xx_info_mux,
1313         .get = snd_es18xx_get_mux,
1314         .put = snd_es18xx_put_mux,
1315 }
1316 };
1317
1318 static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
1319 ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1320 ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1321 ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1322 ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1323 ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1324 ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1325 };
1326
1327 /*
1328  * The chipset specific mixer controls
1329  */
1330 static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1331         ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1332
1333 static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1334 ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1335 ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
1336 ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1337 ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1338 };
1339
1340 static struct snd_kcontrol_new snd_es18xx_opt_1878 =
1341         ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
1342
1343 static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
1344 ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
1345 ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1346 ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1347 };
1348
1349 static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
1350 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1351 };
1352
1353 static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
1354 ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1355 ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1356 };
1357
1358 static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
1359 ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1360 {
1361         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1362         .name = "3D Control - Switch",
1363         .info = snd_es18xx_info_spatializer_enable,
1364         .get = snd_es18xx_get_spatializer_enable,
1365         .put = snd_es18xx_put_spatializer_enable,
1366 }
1367 };
1368
1369 static struct snd_kcontrol_new snd_es18xx_micpre1_control = 
1370 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1371
1372 static struct snd_kcontrol_new snd_es18xx_micpre2_control =
1373 ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1374
1375 static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
1376 {
1377         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1378         .name = "Hardware Master Playback Volume",
1379         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1380         .info = snd_es18xx_info_hw_volume,
1381         .get = snd_es18xx_get_hw_volume,
1382 },
1383 {
1384         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1385         .name = "Hardware Master Playback Switch",
1386         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1387         .info = snd_es18xx_info_hw_switch,
1388         .get = snd_es18xx_get_hw_switch,
1389 },
1390 ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1391 };
1392
1393 static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
1394 {
1395         int data;
1396         unsigned long flags;
1397         spin_lock_irqsave(&chip->ctrl_lock, flags);
1398         outb(reg, chip->ctrl_port);
1399         data = inb(chip->ctrl_port + 1);
1400         spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1401         return data;
1402 }
1403
1404 static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip, 
1405                                               unsigned char reg, unsigned char data)
1406 {
1407         /* No need for spinlocks, this function is used only in
1408            otherwise protected init code */
1409         outb(reg, chip->ctrl_port);
1410         outb(data, chip->ctrl_port + 1);
1411 #ifdef REG_DEBUG
1412         snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
1413 #endif
1414 }
1415
1416 static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
1417 {
1418         int mask = 0;
1419
1420         /* enable extended mode */
1421         snd_es18xx_dsp_command(chip, 0xC6);
1422         /* Reset mixer registers */
1423         snd_es18xx_mixer_write(chip, 0x00, 0x00);
1424
1425         /* Audio 1 DMA demand mode (4 bytes/request) */
1426         snd_es18xx_write(chip, 0xB9, 2);
1427         if (chip->caps & ES18XX_CONTROL) {
1428                 /* Hardware volume IRQ */
1429                 snd_es18xx_config_write(chip, 0x27, chip->irq);
1430                 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1431                         /* FM I/O */
1432                         snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1433                         snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1434                 }
1435                 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1436                         /* MPU-401 I/O */
1437                         snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1438                         snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1439                         /* MPU-401 IRQ */
1440                         snd_es18xx_config_write(chip, 0x28, chip->irq);
1441                 }
1442                 /* Audio1 IRQ */
1443                 snd_es18xx_config_write(chip, 0x70, chip->irq);
1444                 /* Audio2 IRQ */
1445                 snd_es18xx_config_write(chip, 0x72, chip->irq);
1446                 /* Audio1 DMA */
1447                 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1448                 /* Audio2 DMA */
1449                 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1450
1451                 /* Enable Audio 1 IRQ */
1452                 snd_es18xx_write(chip, 0xB1, 0x50);
1453                 /* Enable Audio 2 IRQ */
1454                 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1455                 /* Enable Audio 1 DMA */
1456                 snd_es18xx_write(chip, 0xB2, 0x50);
1457                 /* Enable MPU and hardware volume interrupt */
1458                 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1459         }
1460         else {
1461                 int irqmask, dma1mask, dma2mask;
1462                 switch (chip->irq) {
1463                 case 2:
1464                 case 9:
1465                         irqmask = 0;
1466                         break;
1467                 case 5:
1468                         irqmask = 1;
1469                         break;
1470                 case 7:
1471                         irqmask = 2;
1472                         break;
1473                 case 10:
1474                         irqmask = 3;
1475                         break;
1476                 default:
1477                         snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
1478                         return -ENODEV;
1479                 }
1480                 switch (chip->dma1) {
1481                 case 0:
1482                         dma1mask = 1;
1483                         break;
1484                 case 1:
1485                         dma1mask = 2;
1486                         break;
1487                 case 3:
1488                         dma1mask = 3;
1489                         break;
1490                 default:
1491                         snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
1492                         return -ENODEV;
1493                 }
1494                 switch (chip->dma2) {
1495                 case 0:
1496                         dma2mask = 0;
1497                         break;
1498                 case 1:
1499                         dma2mask = 1;
1500                         break;
1501                 case 3:
1502                         dma2mask = 2;
1503                         break;
1504                 case 5:
1505                         dma2mask = 3;
1506                         break;
1507                 default:
1508                         snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
1509                         return -ENODEV;
1510                 }
1511
1512                 /* Enable and set Audio 1 IRQ */
1513                 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1514                 /* Enable and set Audio 1 DMA */
1515                 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1516                 /* Set Audio 2 DMA */
1517                 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1518                 /* Enable Audio 2 IRQ and DMA
1519                    Set capture mixer input */
1520                 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1521                 /* Enable and set hardware volume interrupt */
1522                 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1523                 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1524                         /* MPU401 share irq with audio
1525                            Joystick enabled
1526                            FM enabled */
1527                         snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1528                 }
1529                 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1530         }
1531         if (chip->caps & ES18XX_NEW_RATE) {
1532                 /* Change behaviour of register A1
1533                    4x oversampling
1534                    2nd channel DAC asynchronous */
1535                 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1536         }
1537         if (!(chip->caps & ES18XX_PCM2)) {
1538                 /* Enable DMA FIFO */
1539                 snd_es18xx_write(chip, 0xB7, 0x80);
1540         }
1541         if (chip->caps & ES18XX_SPATIALIZER) {
1542                 /* Set spatializer parameters to recommended values */
1543                 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1544                 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1545                 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1546                 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1547         }
1548         /* Flip the "enable I2S" bits for those chipsets that need it */
1549         switch (chip->version) {
1550         case 0x1879:
1551                 //Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1552                 //so a Switch control has been added to toggle this 0x71 bit on/off:
1553                 //snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1554                 /* Note: we fall through on purpose here. */
1555         case 0x1878:
1556                 snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1557                 break;
1558         }
1559         /* Mute input source */
1560         if (chip->caps & ES18XX_MUTEREC)
1561                 mask = 0x10;
1562         if (chip->caps & ES18XX_RECMIX)
1563                 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1564         else {
1565                 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1566                 snd_es18xx_write(chip, 0xb4, 0x00);
1567         }
1568 #ifndef AVOID_POPS
1569         /* Enable PCM output */
1570         snd_es18xx_dsp_command(chip, 0xD1);
1571 #endif
1572
1573         return 0;
1574 }
1575
1576 static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
1577 {
1578         int hi,lo;
1579
1580         /* reset */
1581         if (snd_es18xx_reset(chip) < 0) {
1582                 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
1583                 return -ENODEV;
1584         }
1585
1586         snd_es18xx_dsp_command(chip, 0xe7);
1587         hi = snd_es18xx_dsp_get_byte(chip);
1588         if (hi < 0) {
1589                 return hi;
1590         }
1591         lo = snd_es18xx_dsp_get_byte(chip);
1592         if ((lo & 0xf0) != 0x80) {
1593                 return -ENODEV;
1594         }
1595         if (hi == 0x48) {
1596                 chip->version = 0x488;
1597                 return 0;
1598         }
1599         if (hi != 0x68) {
1600                 return -ENODEV;
1601         }
1602         if ((lo & 0x0f) < 8) {
1603                 chip->version = 0x688;
1604                 return 0;
1605         }
1606                         
1607         outb(0x40, chip->port + 0x04);
1608         udelay(10);
1609         hi = inb(chip->port + 0x05);
1610         udelay(10);
1611         lo = inb(chip->port + 0x05);
1612         if (hi != lo) {
1613                 chip->version = hi << 8 | lo;
1614                 chip->ctrl_port = inb(chip->port + 0x05) << 8;
1615                 udelay(10);
1616                 chip->ctrl_port += inb(chip->port + 0x05);
1617
1618                 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1619                         snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1620                         return -EBUSY;
1621                 }
1622
1623                 return 0;
1624         }
1625
1626         /* If has Hardware volume */
1627         if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1628                 /* If has Audio2 */
1629                 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1630                         /* If has volume count */
1631                         if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1632                                 chip->version = 0x1887;
1633                         } else {
1634                                 chip->version = 0x1888;
1635                         }
1636                 } else {
1637                         chip->version = 0x1788;
1638                 }
1639         }
1640         else
1641                 chip->version = 0x1688;
1642         return 0;
1643 }
1644
1645 static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
1646 {
1647         if (snd_es18xx_identify(chip) < 0) {
1648                 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1649                 return -ENODEV;
1650         }
1651
1652         switch (chip->version) {
1653         case 0x1868:
1654                 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL;
1655                 break;
1656         case 0x1869:
1657                 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1658                 break;
1659         case 0x1878:
1660                 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
1661                 break;
1662         case 0x1879:
1663                 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1664                 break;
1665         case 0x1887:
1666                 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
1667                 break;
1668         case 0x1888:
1669                 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME;
1670                 break;
1671         default:
1672                 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
1673                            chip->port, chip->version);
1674                 return -ENODEV;
1675         }
1676
1677         snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1678
1679         if (chip->dma1 == chip->dma2)
1680                 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1681
1682         return snd_es18xx_initialize(chip);
1683 }
1684
1685 static struct snd_pcm_ops snd_es18xx_playback_ops = {
1686         .open =         snd_es18xx_playback_open,
1687         .close =        snd_es18xx_playback_close,
1688         .ioctl =        snd_pcm_lib_ioctl,
1689         .hw_params =    snd_es18xx_playback_hw_params,
1690         .hw_free =      snd_es18xx_pcm_hw_free,
1691         .prepare =      snd_es18xx_playback_prepare,
1692         .trigger =      snd_es18xx_playback_trigger,
1693         .pointer =      snd_es18xx_playback_pointer,
1694 };
1695
1696 static struct snd_pcm_ops snd_es18xx_capture_ops = {
1697         .open =         snd_es18xx_capture_open,
1698         .close =        snd_es18xx_capture_close,
1699         .ioctl =        snd_pcm_lib_ioctl,
1700         .hw_params =    snd_es18xx_capture_hw_params,
1701         .hw_free =      snd_es18xx_pcm_hw_free,
1702         .prepare =      snd_es18xx_capture_prepare,
1703         .trigger =      snd_es18xx_capture_trigger,
1704         .pointer =      snd_es18xx_capture_pointer,
1705 };
1706
1707 static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
1708 {
1709         struct snd_pcm *pcm;
1710         char str[16];
1711         int err;
1712
1713         if (rpcm)
1714                 *rpcm = NULL;
1715         sprintf(str, "ES%x", chip->version);
1716         if (chip->caps & ES18XX_PCM2)
1717                 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
1718         else
1719                 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
1720         if (err < 0)
1721                 return err;
1722
1723         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1724         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1725
1726         /* global setup */
1727         pcm->private_data = chip;
1728         pcm->info_flags = 0;
1729         if (chip->caps & ES18XX_DUPLEX_SAME)
1730                 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1731         if (! (chip->caps & ES18XX_PCM2))
1732                 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1733         sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1734         chip->pcm = pcm;
1735
1736         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1737                                               snd_dma_isa_data(),
1738                                               64*1024,
1739                                               chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1740
1741         if (rpcm)
1742                 *rpcm = pcm;
1743         return 0;
1744 }
1745
1746 /* Power Management support functions */
1747 #ifdef CONFIG_PM
1748 static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1749 {
1750         struct snd_audiodrive *acard = card->private_data;
1751         struct snd_es18xx *chip = acard->chip;
1752
1753         snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1754
1755         snd_pcm_suspend_all(chip->pcm);
1756
1757         /* power down */
1758         chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1759         chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1760         snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1761         snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1762
1763         return 0;
1764 }
1765
1766 static int snd_es18xx_resume(struct snd_card *card)
1767 {
1768         struct snd_audiodrive *acard = card->private_data;
1769         struct snd_es18xx *chip = acard->chip;
1770
1771         /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1772         snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1773
1774         snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
1775         return 0;
1776 }
1777 #endif /* CONFIG_PM */
1778
1779 static int snd_es18xx_free(struct snd_es18xx *chip)
1780 {
1781         release_and_free_resource(chip->res_port);
1782         release_and_free_resource(chip->res_ctrl_port);
1783         release_and_free_resource(chip->res_mpu_port);
1784         if (chip->irq >= 0)
1785                 free_irq(chip->irq, (void *) chip);
1786         if (chip->dma1 >= 0) {
1787                 disable_dma(chip->dma1);
1788                 free_dma(chip->dma1);
1789         }
1790         if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1791                 disable_dma(chip->dma2);
1792                 free_dma(chip->dma2);
1793         }
1794         kfree(chip);
1795         return 0;
1796 }
1797
1798 static int snd_es18xx_dev_free(struct snd_device *device)
1799 {
1800         struct snd_es18xx *chip = device->device_data;
1801         return snd_es18xx_free(chip);
1802 }
1803
1804 static int __devinit snd_es18xx_new_device(struct snd_card *card,
1805                                            unsigned long port,
1806                                            unsigned long mpu_port,
1807                                            unsigned long fm_port,
1808                                            int irq, int dma1, int dma2,
1809                                            struct snd_es18xx ** rchip)
1810 {
1811         struct snd_es18xx *chip;
1812         static struct snd_device_ops ops = {
1813                 .dev_free =     snd_es18xx_dev_free,
1814         };
1815         int err;
1816
1817         *rchip = NULL;
1818         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1819         if (chip == NULL)
1820                 return -ENOMEM;
1821         spin_lock_init(&chip->reg_lock);
1822         spin_lock_init(&chip->mixer_lock);
1823         spin_lock_init(&chip->ctrl_lock);
1824         chip->card = card;
1825         chip->port = port;
1826         chip->mpu_port = mpu_port;
1827         chip->fm_port = fm_port;
1828         chip->irq = -1;
1829         chip->dma1 = -1;
1830         chip->dma2 = -1;
1831         chip->audio2_vol = 0x00;
1832         chip->active = 0;
1833
1834         if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1835                 snd_es18xx_free(chip);
1836                 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1837                 return -EBUSY;
1838         }
1839
1840         if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx", (void *) chip)) {
1841                 snd_es18xx_free(chip);
1842                 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1843                 return -EBUSY;
1844         }
1845         chip->irq = irq;
1846
1847         if (request_dma(dma1, "ES18xx DMA 1")) {
1848                 snd_es18xx_free(chip);
1849                 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1850                 return -EBUSY;
1851         }
1852         chip->dma1 = dma1;
1853
1854         if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1855                 snd_es18xx_free(chip);
1856                 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1857                 return -EBUSY;
1858         }
1859         chip->dma2 = dma2;
1860
1861         if (snd_es18xx_probe(chip) < 0) {
1862                 snd_es18xx_free(chip);
1863                 return -ENODEV;
1864         }
1865         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1866                 snd_es18xx_free(chip);
1867                 return err;
1868         }
1869         *rchip = chip;
1870         return 0;
1871 }
1872
1873 static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
1874 {
1875         struct snd_card *card;
1876         int err;
1877         unsigned int idx;
1878
1879         card = chip->card;
1880
1881         strcpy(card->mixername, chip->pcm->name);
1882
1883         for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
1884                 struct snd_kcontrol *kctl;
1885                 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1886                 if (chip->caps & ES18XX_HWV) {
1887                         switch (idx) {
1888                         case 0:
1889                                 chip->master_volume = kctl;
1890                                 kctl->private_free = snd_es18xx_hwv_free;
1891                                 break;
1892                         case 1:
1893                                 chip->master_switch = kctl;
1894                                 kctl->private_free = snd_es18xx_hwv_free;
1895                                 break;
1896                         }
1897                 }
1898                 if ((err = snd_ctl_add(card, kctl)) < 0)
1899                         return err;
1900         }
1901         if (chip->caps & ES18XX_PCM2) {
1902                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1903                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1904                                 return err;
1905                 } 
1906         } else {
1907                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1908                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1909                                 return err;
1910                 }
1911         }
1912
1913         if (chip->caps & ES18XX_RECMIX) {
1914                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1915                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1916                                 return err;
1917                 }
1918         }
1919         switch (chip->version) {
1920         default:
1921                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1922                         return err;
1923                 break;
1924         case 0x1869:
1925         case 0x1879:
1926                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1927                         return err;
1928                 break;
1929         }
1930         if (chip->caps & ES18XX_SPATIALIZER) {
1931                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1932                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1933                                 return err;
1934                 }
1935         }
1936         if (chip->caps & ES18XX_HWV) {
1937                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
1938                         struct snd_kcontrol *kctl;
1939                         kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1940                         if (idx == 0)
1941                                 chip->hw_volume = kctl;
1942                         else
1943                                 chip->hw_switch = kctl;
1944                         kctl->private_free = snd_es18xx_hwv_free;
1945                         if ((err = snd_ctl_add(card, kctl)) < 0)
1946                                 return err;
1947                         
1948                 }
1949         }
1950         /* finish initializing other chipset specific controls
1951          */
1952         if (chip->version != 0x1868) {
1953                 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1954                                                      chip));
1955                 if (err < 0)
1956                         return err;
1957         }
1958         if (chip->version == 0x1869) {
1959                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1960                         err = snd_ctl_add(card,
1961                                           snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1962                                                        chip));
1963                         if (err < 0)
1964                                 return err;
1965                 }
1966         } else if (chip->version == 0x1878) {
1967                 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1968                                                      chip));
1969                 if (err < 0)
1970                         return err;
1971         } else if (chip->version == 0x1879) {
1972                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1973                         err = snd_ctl_add(card,
1974                                           snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1975                                                        chip));
1976                         if (err < 0)
1977                                 return err;
1978                 }
1979         }
1980         return 0;
1981 }
1982        
1983
1984 /* Card level */
1985
1986 MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");  
1987 MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1988 MODULE_LICENSE("GPL");
1989 MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1990                 "{ESS,ES1869 PnP AudioDrive},"
1991                 "{ESS,ES1878 PnP AudioDrive},"
1992                 "{ESS,ES1879 PnP AudioDrive},"
1993                 "{ESS,ES1887 PnP AudioDrive},"
1994                 "{ESS,ES1888 PnP AudioDrive},"
1995                 "{ESS,ES1887 AudioDrive},"
1996                 "{ESS,ES1888 AudioDrive}}");
1997
1998 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
1999 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
2000 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
2001 #ifdef CONFIG_PNP
2002 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
2003 #endif
2004 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x240,0x260,0x280 */
2005 #ifndef CONFIG_PNP
2006 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
2007 #else
2008 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
2009 #endif
2010 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
2011 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,10 */
2012 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
2013 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
2014
2015 module_param_array(index, int, NULL, 0444);
2016 MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
2017 module_param_array(id, charp, NULL, 0444);
2018 MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
2019 module_param_array(enable, bool, NULL, 0444);
2020 MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
2021 #ifdef CONFIG_PNP
2022 module_param_array(isapnp, bool, NULL, 0444);
2023 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
2024 #endif
2025 module_param_array(port, long, NULL, 0444);
2026 MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
2027 module_param_array(mpu_port, long, NULL, 0444);
2028 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
2029 module_param_array(fm_port, long, NULL, 0444);
2030 MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
2031 module_param_array(irq, int, NULL, 0444);
2032 MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2033 module_param_array(dma1, int, NULL, 0444);
2034 MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2035 module_param_array(dma2, int, NULL, 0444);
2036 MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2037
2038 static struct platform_device *platform_devices[SNDRV_CARDS];
2039
2040 #ifdef CONFIG_PNP
2041 static int pnp_registered, pnpc_registered;
2042
2043 static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2044         { .id = "ESS1869" },
2045         { .id = "" }            /* end */
2046 };
2047
2048 MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2049
2050 /* PnP main device initialization */
2051 static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev,
2052                                                   struct pnp_resource_table *cfg)
2053 {
2054         int err;
2055
2056         pnp_init_resource_table(cfg);
2057         if (port[dev] != SNDRV_AUTO_PORT)
2058                 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
2059         if (fm_port[dev] != SNDRV_AUTO_PORT)
2060                 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
2061         if (mpu_port[dev] != SNDRV_AUTO_PORT)
2062                 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
2063         if (dma1[dev] != SNDRV_AUTO_DMA)
2064                 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
2065         if (dma2[dev] != SNDRV_AUTO_DMA)
2066                 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
2067         if (irq[dev] != SNDRV_AUTO_IRQ)
2068                 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
2069         if (pnp_device_is_isapnp(pdev)) {
2070                 err = pnp_manual_config_dev(pdev, cfg, 0);
2071                 if (err < 0)
2072                         snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
2073         }
2074         err = pnp_activate_dev(pdev);
2075         if (err < 0) {
2076                 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2077                 return -EBUSY;
2078         }
2079         /* ok. hack using Vendor-Defined Card-Level registers */
2080         /* skip csn and logdev initialization - already done in isapnp_configure */
2081         if (pnp_device_is_isapnp(pdev)) {
2082                 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2083                 isapnp_write_byte(0x27, pnp_irq(pdev, 0));      /* Hardware Volume IRQ Number */
2084                 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2085                         isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2086                 isapnp_write_byte(0x72, pnp_irq(pdev, 0));      /* second IRQ */
2087                 isapnp_cfg_end();
2088         }
2089         port[dev] = pnp_port_start(pdev, 0);
2090         fm_port[dev] = pnp_port_start(pdev, 1);
2091         mpu_port[dev] = pnp_port_start(pdev, 2);
2092         dma1[dev] = pnp_dma(pdev, 0);
2093         dma2[dev] = pnp_dma(pdev, 1);
2094         irq[dev] = pnp_irq(pdev, 0);
2095         snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2096         snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2097         return 0;
2098 }
2099
2100 static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2101                                         struct pnp_dev *pdev)
2102 {
2103         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
2104
2105         if (!cfg)
2106                 return -ENOMEM;
2107         acard->dev = pdev;
2108         if (snd_audiodrive_pnp_init_main(dev, acard->dev, cfg) < 0) {
2109                 kfree(cfg);
2110                 return -EBUSY;
2111         }
2112         kfree(cfg);
2113         return 0;
2114 }
2115
2116 static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2117         /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2118         { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2119         /* ESS 1868 (integrated on Maxisound Cards) */
2120         { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2121         /* ESS 1868 (integrated on Maxisound Cards) */
2122         { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2123         /* ESS ES1869 Plug and Play AudioDrive */
2124         { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2125         /* ESS 1869 */
2126         { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2127         /* ESS 1878 */
2128         { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2129         /* ESS 1879 */
2130         { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2131         /* --- */
2132         { .id = "" } /* end */
2133 };
2134
2135 MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2136
2137 static int __devinit snd_audiodrive_pnpc(int dev, struct snd_audiodrive *acard,
2138                                         struct pnp_card_link *card,
2139                                         const struct pnp_card_device_id *id)
2140 {
2141         struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
2142
2143         if (!cfg)
2144                 return -ENOMEM;
2145         acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2146         if (acard->dev == NULL) {
2147                 kfree(cfg);
2148                 return -EBUSY;
2149         }
2150         acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2151         if (acard->devc == NULL) {
2152                 kfree(cfg);
2153                 return -EBUSY;
2154         }
2155         /* Control port initialization */
2156         if (pnp_activate_dev(acard->devc) < 0) {
2157                 kfree(cfg);
2158                 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2159                 return -EAGAIN;
2160         }
2161         snd_printdd("pnp: port=0x%llx\n",
2162                         (unsigned long long)pnp_port_start(acard->devc, 0));
2163         if (snd_audiodrive_pnp_init_main(dev, acard->dev, cfg) < 0) {
2164                 kfree(cfg);
2165                 return -EBUSY;
2166         }
2167         kfree(cfg);
2168         return 0;
2169 }
2170 #endif /* CONFIG_PNP */
2171
2172 #ifdef CONFIG_PNP
2173 #define is_isapnp_selected(dev)         isapnp[dev]
2174 #else
2175 #define is_isapnp_selected(dev)         0
2176 #endif
2177
2178 static struct snd_card *snd_es18xx_card_new(int dev)
2179 {
2180         return snd_card_new(index[dev], id[dev], THIS_MODULE,
2181                             sizeof(struct snd_audiodrive));
2182 }
2183
2184 static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2185 {
2186         struct snd_audiodrive *acard = card->private_data;
2187         struct snd_es18xx *chip;
2188         struct snd_opl3 *opl3;
2189         int err;
2190
2191         if ((err = snd_es18xx_new_device(card,
2192                                          port[dev],
2193                                          mpu_port[dev],
2194                                          fm_port[dev],
2195                                          irq[dev], dma1[dev], dma2[dev],
2196                                          &chip)) < 0)
2197                 return err;
2198         acard->chip = chip;
2199
2200         sprintf(card->driver, "ES%x", chip->version);
2201         
2202         sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2203         if (dma1[dev] != dma2[dev])
2204                 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2205                         card->shortname,
2206                         chip->port,
2207                         irq[dev], dma1[dev], dma2[dev]);
2208         else
2209                 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2210                         card->shortname,
2211                         chip->port,
2212                         irq[dev], dma1[dev]);
2213
2214         if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
2215                 return err;
2216
2217         if ((err = snd_es18xx_mixer(chip)) < 0)
2218                 return err;
2219
2220         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2221                 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
2222                         snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
2223                 } else {
2224                         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
2225                                 return err;
2226                 }
2227         }
2228
2229         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2230                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2231                                                chip->mpu_port, 0,
2232                                                irq[dev], 0,
2233                                                &chip->rmidi)) < 0)
2234                         return err;
2235         }
2236
2237         return snd_card_register(card);
2238 }
2239
2240 static int __devinit snd_es18xx_nonpnp_probe1(int dev, struct platform_device *devptr)
2241 {
2242         struct snd_card *card;
2243         int err;
2244
2245         card = snd_es18xx_card_new(dev);
2246         if (! card)
2247                 return -ENOMEM;
2248         snd_card_set_dev(card, &devptr->dev);
2249         if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2250                 snd_card_free(card);
2251                 return err;
2252         }
2253         platform_set_drvdata(devptr, card);
2254         return 0;
2255 }
2256
2257 static int __devinit snd_es18xx_nonpnp_probe(struct platform_device *pdev)
2258 {
2259         int dev = pdev->id;
2260         int err;
2261         static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2262         static int possible_dmas[] = {1, 0, 3, 5, -1};
2263
2264         if (irq[dev] == SNDRV_AUTO_IRQ) {
2265                 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2266                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2267                         return -EBUSY;
2268                 }
2269         }
2270         if (dma1[dev] == SNDRV_AUTO_DMA) {
2271                 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2272                         snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2273                         return -EBUSY;
2274                 }
2275         }
2276         if (dma2[dev] == SNDRV_AUTO_DMA) {
2277                 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2278                         snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2279                         return -EBUSY;
2280                 }
2281         }
2282
2283         if (port[dev] != SNDRV_AUTO_PORT) {
2284                 return snd_es18xx_nonpnp_probe1(dev, pdev);
2285         } else {
2286                 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2287                 int i;
2288                 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2289                         port[dev] = possible_ports[i];
2290                         err = snd_es18xx_nonpnp_probe1(dev, pdev);
2291                         if (! err)
2292                                 return 0;
2293                 }
2294                 return err;
2295         }
2296 }
2297
2298 static int __devexit snd_es18xx_nonpnp_remove(struct platform_device *devptr)
2299 {
2300         snd_card_free(platform_get_drvdata(devptr));
2301         platform_set_drvdata(devptr, NULL);
2302         return 0;
2303 }
2304
2305 #ifdef CONFIG_PM
2306 static int snd_es18xx_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
2307 {
2308         return snd_es18xx_suspend(platform_get_drvdata(dev), state);
2309 }
2310
2311 static int snd_es18xx_nonpnp_resume(struct platform_device *dev)
2312 {
2313         return snd_es18xx_resume(platform_get_drvdata(dev));
2314 }
2315 #endif
2316
2317 #define ES18XX_DRIVER   "snd_es18xx"
2318
2319 static struct platform_driver snd_es18xx_nonpnp_driver = {
2320         .probe          = snd_es18xx_nonpnp_probe,
2321         .remove         = __devexit_p(snd_es18xx_nonpnp_remove),
2322 #ifdef CONFIG_PM
2323         .suspend        = snd_es18xx_nonpnp_suspend,
2324         .resume         = snd_es18xx_nonpnp_resume,
2325 #endif
2326         .driver         = {
2327                 .name   = ES18XX_DRIVER
2328         },
2329 };
2330
2331
2332 #ifdef CONFIG_PNP
2333 static unsigned int __devinitdata es18xx_pnp_devices;
2334
2335 static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2336                                             const struct pnp_device_id *id)
2337 {
2338         static int dev;
2339         int err;
2340         struct snd_card *card;
2341
2342         if (pnp_device_is_isapnp(pdev))
2343                 return -ENOENT; /* we have another procedure - card */
2344         for (; dev < SNDRV_CARDS; dev++) {
2345                 if (enable[dev] && isapnp[dev])
2346                         break;
2347         }
2348         if (dev >= SNDRV_CARDS)
2349                 return -ENODEV;
2350
2351         card = snd_es18xx_card_new(dev);
2352         if (! card)
2353                 return -ENOMEM;
2354         if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2355                 snd_card_free(card);
2356                 return err;
2357         }
2358         snd_card_set_dev(card, &pdev->dev);
2359         if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2360                 snd_card_free(card);
2361                 return err;
2362         }
2363         pnp_set_drvdata(pdev, card);
2364         dev++;
2365         es18xx_pnp_devices++;
2366         return 0;
2367 }
2368
2369 static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
2370 {
2371         snd_card_free(pnp_get_drvdata(pdev));
2372         pnp_set_drvdata(pdev, NULL);
2373 }
2374
2375 #ifdef CONFIG_PM
2376 static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2377 {
2378         return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2379 }
2380 static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2381 {
2382         return snd_es18xx_resume(pnp_get_drvdata(pdev));
2383 }
2384 #endif
2385
2386 static struct pnp_driver es18xx_pnp_driver = {
2387         .name = "es18xx-pnpbios",
2388         .id_table = snd_audiodrive_pnpbiosids,
2389         .probe = snd_audiodrive_pnp_detect,
2390         .remove = __devexit_p(snd_audiodrive_pnp_remove),
2391 #ifdef CONFIG_PM
2392         .suspend = snd_audiodrive_pnp_suspend,
2393         .resume = snd_audiodrive_pnp_resume,
2394 #endif
2395 };
2396
2397 static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2398                                                const struct pnp_card_device_id *pid)
2399 {
2400         static int dev;
2401         struct snd_card *card;
2402         int res;
2403
2404         for ( ; dev < SNDRV_CARDS; dev++) {
2405                 if (enable[dev] && isapnp[dev])
2406                         break;
2407         }
2408         if (dev >= SNDRV_CARDS)
2409                 return -ENODEV;
2410
2411         card = snd_es18xx_card_new(dev);
2412         if (! card)
2413                 return -ENOMEM;
2414
2415         if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
2416                 snd_card_free(card);
2417                 return res;
2418         }
2419         snd_card_set_dev(card, &pcard->card->dev);
2420         if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2421                 snd_card_free(card);
2422                 return res;
2423         }
2424
2425         pnp_set_card_drvdata(pcard, card);
2426         dev++;
2427         es18xx_pnp_devices++;
2428         return 0;
2429 }
2430
2431 static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
2432 {
2433         snd_card_free(pnp_get_card_drvdata(pcard));
2434         pnp_set_card_drvdata(pcard, NULL);
2435 }
2436
2437 #ifdef CONFIG_PM
2438 static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
2439 {
2440         return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2441 }
2442
2443 static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
2444 {
2445         return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2446 }
2447
2448 #endif
2449
2450 static struct pnp_card_driver es18xx_pnpc_driver = {
2451         .flags = PNP_DRIVER_RES_DISABLE,
2452         .name = "es18xx",
2453         .id_table = snd_audiodrive_pnpids,
2454         .probe = snd_audiodrive_pnpc_detect,
2455         .remove = __devexit_p(snd_audiodrive_pnpc_remove),
2456 #ifdef CONFIG_PM
2457         .suspend        = snd_audiodrive_pnpc_suspend,
2458         .resume         = snd_audiodrive_pnpc_resume,
2459 #endif
2460 };
2461 #endif /* CONFIG_PNP */
2462
2463 static void __init_or_module snd_es18xx_unregister_all(void)
2464 {
2465         int i;
2466
2467 #ifdef CONFIG_PNP
2468         if (pnpc_registered)
2469                 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2470         if (pnp_registered)
2471                 pnp_unregister_driver(&es18xx_pnp_driver);
2472 #endif
2473         for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
2474                 platform_device_unregister(platform_devices[i]);
2475         platform_driver_unregister(&snd_es18xx_nonpnp_driver);
2476 }
2477
2478 static int __init alsa_card_es18xx_init(void)
2479 {
2480         int i, err, cards = 0;
2481
2482         if ((err = platform_driver_register(&snd_es18xx_nonpnp_driver)) < 0)
2483                 return err;
2484
2485         for (i = 0; i < SNDRV_CARDS; i++) {
2486                 struct platform_device *device;
2487                 if (! enable[i] || is_isapnp_selected(i))
2488                         continue;
2489                 device = platform_device_register_simple(ES18XX_DRIVER,
2490                                                          i, NULL, 0);
2491                 if (IS_ERR(device))
2492                         continue;
2493                 if (!platform_get_drvdata(device)) {
2494                         platform_device_unregister(device);
2495                         continue;
2496                 }
2497                 platform_devices[i] = device;
2498                 cards++;
2499         }
2500
2501 #ifdef CONFIG_PNP
2502         err = pnp_register_driver(&es18xx_pnp_driver);
2503         if (!err)
2504                 pnp_registered = 1;
2505         err = pnp_register_card_driver(&es18xx_pnpc_driver);
2506         if (!err)
2507                 pnpc_registered = 1;
2508         cards += es18xx_pnp_devices;
2509 #endif
2510
2511         if(!cards) {
2512 #ifdef MODULE
2513                 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2514 #endif
2515                 snd_es18xx_unregister_all();
2516                 return -ENODEV;
2517         }
2518         return 0;
2519 }
2520
2521 static void __exit alsa_card_es18xx_exit(void)
2522 {
2523         snd_es18xx_unregister_all();
2524 }
2525
2526 module_init(alsa_card_es18xx_init)
2527 module_exit(alsa_card_es18xx_exit)