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