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