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