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