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