ieee80211_security: correct warning about width of auth_mode
[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_card *card;
125         struct snd_pcm *pcm;
126         struct snd_pcm_substream *playback_a_substream;
127         struct snd_pcm_substream *capture_a_substream;
128         struct snd_pcm_substream *playback_b_substream;
129
130         struct snd_rawmidi *rmidi;
131
132         struct snd_kcontrol *hw_volume;
133         struct snd_kcontrol *hw_switch;
134         struct snd_kcontrol *master_volume;
135         struct snd_kcontrol *master_switch;
136
137         spinlock_t reg_lock;
138         spinlock_t mixer_lock;
139         spinlock_t ctrl_lock;
140 #ifdef CONFIG_PM
141         unsigned char pm_reg;
142 #endif
143 };
144
145 struct snd_audiodrive {
146         struct snd_es18xx *chip;
147 #ifdef CONFIG_PNP
148         struct pnp_dev *dev;
149         struct pnp_dev *devc;
150 #endif
151 };
152
153 #define AUDIO1_IRQ      0x01
154 #define AUDIO2_IRQ      0x02
155 #define HWV_IRQ         0x04
156 #define MPU_IRQ         0x08
157
158 #define ES18XX_PCM2     0x0001  /* Has two useable PCM */
159 #define ES18XX_SPATIALIZER 0x0002       /* Has 3D Spatializer */
160 #define ES18XX_RECMIX   0x0004  /* Has record mixer */
161 #define ES18XX_DUPLEX_MONO 0x0008       /* Has mono duplex only */
162 #define ES18XX_DUPLEX_SAME 0x0010       /* Playback and record must share the same rate */
163 #define ES18XX_NEW_RATE 0x0020  /* More precise rate setting */
164 #define ES18XX_AUXB     0x0040  /* AuxB mixer control */
165 #define ES18XX_HWV      0x0080  /* Has separate hardware volume mixer controls*/
166 #define ES18XX_MONO     0x0100  /* Mono_in mixer control */
167 #define ES18XX_I2S      0x0200  /* I2S mixer control */
168 #define ES18XX_MUTEREC  0x0400  /* Record source can be muted */
169 #define ES18XX_CONTROL  0x0800  /* Has control ports */
170
171 /* Power Management */
172 #define ES18XX_PM       0x07
173 #define ES18XX_PM_GPO0  0x01
174 #define ES18XX_PM_GPO1  0x02
175 #define ES18XX_PM_PDR   0x04
176 #define ES18XX_PM_ANA   0x08
177 #define ES18XX_PM_FM    0x020
178 #define ES18XX_PM_SUS   0x080
179
180 /* Lowlevel */
181
182 #define DAC1 0x01
183 #define ADC1 0x02
184 #define DAC2 0x04
185 #define MILLISECOND 10000
186
187 static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
188 {
189         int i;
190
191         for(i = MILLISECOND; i; i--)
192                 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
193                         outb(val, chip->port + 0x0C);
194                         return 0;
195                 }
196         snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
197         return -EINVAL;
198 }
199
200 static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
201 {
202         int i;
203
204         for(i = MILLISECOND/10; i; i--)
205                 if (inb(chip->port + 0x0C) & 0x40)
206                         return inb(chip->port + 0x0A);
207         snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
208                    chip->port + 0x0A, inb(chip->port + 0x0A));
209         return -ENODEV;
210 }
211
212 #undef REG_DEBUG
213
214 static int snd_es18xx_write(struct snd_es18xx *chip,
215                             unsigned char reg, unsigned char data)
216 {
217         unsigned long flags;
218         int ret;
219         
220         spin_lock_irqsave(&chip->reg_lock, flags);
221         ret = snd_es18xx_dsp_command(chip, reg);
222         if (ret < 0)
223                 goto end;
224         ret = snd_es18xx_dsp_command(chip, data);
225  end:
226         spin_unlock_irqrestore(&chip->reg_lock, flags);
227 #ifdef REG_DEBUG
228         snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
229 #endif
230         return ret;
231 }
232
233 static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
234 {
235         unsigned long flags;
236         int ret, data;
237         spin_lock_irqsave(&chip->reg_lock, flags);
238         ret = snd_es18xx_dsp_command(chip, 0xC0);
239         if (ret < 0)
240                 goto end;
241         ret = snd_es18xx_dsp_command(chip, reg);
242         if (ret < 0)
243                 goto end;
244         data = snd_es18xx_dsp_get_byte(chip);
245         ret = data;
246 #ifdef REG_DEBUG
247         snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
248 #endif
249  end:
250         spin_unlock_irqrestore(&chip->reg_lock, flags);
251         return ret;
252 }
253
254 /* Return old value */
255 static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
256                            unsigned char mask, unsigned char val)
257 {
258         int ret;
259         unsigned char old, new, oval;
260         unsigned long flags;
261         spin_lock_irqsave(&chip->reg_lock, flags);
262         ret = snd_es18xx_dsp_command(chip, 0xC0);
263         if (ret < 0)
264                 goto end;
265         ret = snd_es18xx_dsp_command(chip, reg);
266         if (ret < 0)
267                 goto end;
268         ret = snd_es18xx_dsp_get_byte(chip);
269         if (ret < 0) {
270                 goto end;
271         }
272         old = ret;
273         oval = old & mask;
274         if (val != oval) {
275                 ret = snd_es18xx_dsp_command(chip, reg);
276                 if (ret < 0)
277                         goto end;
278                 new = (old & ~mask) | (val & mask);
279                 ret = snd_es18xx_dsp_command(chip, new);
280                 if (ret < 0)
281                         goto end;
282 #ifdef REG_DEBUG
283                 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
284                            reg, old, new, ret);
285 #endif
286         }
287         ret = oval;
288  end:
289         spin_unlock_irqrestore(&chip->reg_lock, flags);
290         return ret;
291 }
292
293 static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
294                             unsigned char reg, unsigned char data)
295 {
296         unsigned long flags;
297         spin_lock_irqsave(&chip->mixer_lock, flags);
298         outb(reg, chip->port + 0x04);
299         outb(data, chip->port + 0x05);
300         spin_unlock_irqrestore(&chip->mixer_lock, flags);
301 #ifdef REG_DEBUG
302         snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
303 #endif
304 }
305
306 static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
307 {
308         unsigned long flags;
309         int data;
310         spin_lock_irqsave(&chip->mixer_lock, flags);
311         outb(reg, chip->port + 0x04);
312         data = inb(chip->port + 0x05);
313         spin_unlock_irqrestore(&chip->mixer_lock, flags);
314 #ifdef REG_DEBUG
315         snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
316 #endif
317         return data;
318 }
319
320 /* Return old value */
321 static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
322                                         unsigned char mask, unsigned char val)
323 {
324         unsigned char old, new, oval;
325         unsigned long flags;
326         spin_lock_irqsave(&chip->mixer_lock, flags);
327         outb(reg, chip->port + 0x04);
328         old = inb(chip->port + 0x05);
329         oval = old & mask;
330         if (val != oval) {
331                 new = (old & ~mask) | (val & mask);
332                 outb(new, chip->port + 0x05);
333 #ifdef REG_DEBUG
334                 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
335                            reg, old, new);
336 #endif
337         }
338         spin_unlock_irqrestore(&chip->mixer_lock, flags);
339         return oval;
340 }
341
342 static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
343                                             unsigned char mask)
344 {
345         int old, expected, new;
346         unsigned long flags;
347         spin_lock_irqsave(&chip->mixer_lock, flags);
348         outb(reg, chip->port + 0x04);
349         old = inb(chip->port + 0x05);
350         expected = old ^ mask;
351         outb(expected, chip->port + 0x05);
352         new = inb(chip->port + 0x05);
353         spin_unlock_irqrestore(&chip->mixer_lock, flags);
354 #ifdef REG_DEBUG
355         snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
356                    reg, old, expected, new);
357 #endif
358         return expected == new;
359 }
360
361
362 static int snd_es18xx_reset(struct snd_es18xx *chip)
363 {
364         int i;
365         outb(0x03, chip->port + 0x06);
366         inb(chip->port + 0x06);
367         outb(0x00, chip->port + 0x06);
368         for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
369         if (inb(chip->port + 0x0A) != 0xAA)
370                 return -1;
371         return 0;
372 }
373
374 static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
375 {
376         outb(0x02, chip->port + 0x06);
377         inb(chip->port + 0x06);
378         outb(0x00, chip->port + 0x06);
379         return 0;
380 }
381
382 static struct snd_ratnum new_clocks[2] = {
383         {
384                 .num = 793800,
385                 .den_min = 1,
386                 .den_max = 128,
387                 .den_step = 1,
388         },
389         {
390                 .num = 768000,
391                 .den_min = 1,
392                 .den_max = 128,
393                 .den_step = 1,
394         }
395 };
396
397 static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
398         .nrats = 2,
399         .rats = new_clocks,
400 };
401
402 static struct snd_ratnum old_clocks[2] = {
403         {
404                 .num = 795444,
405                 .den_min = 1,
406                 .den_max = 128,
407                 .den_step = 1,
408         },
409         {
410                 .num = 397722,
411                 .den_min = 1,
412                 .den_max = 128,
413                 .den_step = 1,
414         }
415 };
416
417 static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks  = {
418         .nrats = 2,
419         .rats = old_clocks,
420 };
421
422
423 static void snd_es18xx_rate_set(struct snd_es18xx *chip, 
424                                 struct snd_pcm_substream *substream,
425                                 int mode)
426 {
427         unsigned int bits, div0;
428         struct snd_pcm_runtime *runtime = substream->runtime;
429         if (chip->caps & ES18XX_NEW_RATE) {
430                 if (runtime->rate_num == new_clocks[0].num)
431                         bits = 128 - runtime->rate_den;
432                 else
433                         bits = 256 - runtime->rate_den;
434         } else {
435                 if (runtime->rate_num == old_clocks[0].num)
436                         bits = 256 - runtime->rate_den;
437                 else
438                         bits = 128 - runtime->rate_den;
439         }
440
441         /* set filter register */
442         div0 = 256 - 7160000*20/(8*82*runtime->rate);
443                 
444         if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
445                 snd_es18xx_mixer_write(chip, 0x70, bits);
446                 /*
447                  * Comment from kernel oss driver:
448                  * FKS: fascinating: 0x72 doesn't seem to work.
449                  */
450                 snd_es18xx_write(chip, 0xA2, div0);
451                 snd_es18xx_mixer_write(chip, 0x72, div0);
452         } else {
453                 snd_es18xx_write(chip, 0xA1, bits);
454                 snd_es18xx_write(chip, 0xA2, div0);
455         }
456 }
457
458 static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
459                                          struct snd_pcm_hw_params *hw_params)
460 {
461         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
462         int shift, err;
463
464         shift = 0;
465         if (params_channels(hw_params) == 2)
466                 shift++;
467         if (snd_pcm_format_width(params_format(hw_params)) == 16)
468                 shift++;
469
470         if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
471                 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
472                     (chip->capture_a_substream) &&
473                     params_channels(hw_params) != 1) {
474                         _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
475                         return -EBUSY;
476                 }
477                 chip->dma2_shift = shift;
478         } else {
479                 chip->dma1_shift = shift;
480         }
481         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
482                 return err;
483         return 0;
484 }
485
486 static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
487 {
488         return snd_pcm_lib_free_pages(substream);
489 }
490
491 static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
492                                         struct snd_pcm_substream *substream)
493 {
494         struct snd_pcm_runtime *runtime = substream->runtime;
495         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
496         unsigned int count = snd_pcm_lib_period_bytes(substream);
497
498         chip->dma2_size = size;
499
500         snd_es18xx_rate_set(chip, substream, DAC2);
501
502         /* Transfer Count Reload */
503         count = 0x10000 - count;
504         snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
505         snd_es18xx_mixer_write(chip, 0x76, count >> 8);
506
507         /* Set format */
508         snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
509                               ((runtime->channels == 1) ? 0x00 : 0x02) |
510                               (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
511                               (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
512
513         /* Set DMA controller */
514         snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
515
516         return 0;
517 }
518
519 static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
520                                         struct snd_pcm_substream *substream,
521                                         int cmd)
522 {
523         switch (cmd) {
524         case SNDRV_PCM_TRIGGER_START:
525         case SNDRV_PCM_TRIGGER_RESUME:
526                 if (chip->active & DAC2)
527                         return 0;
528                 chip->active |= DAC2;
529                 /* Start DMA */
530                 if (chip->dma2 >= 4)
531                         snd_es18xx_mixer_write(chip, 0x78, 0xb3);
532                 else
533                         snd_es18xx_mixer_write(chip, 0x78, 0x93);
534 #ifdef AVOID_POPS
535                 /* Avoid pops */
536                 udelay(100000);
537                 if (chip->caps & ES18XX_PCM2)
538                         /* Restore Audio 2 volume */
539                         snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
540                 else
541                         /* Enable PCM output */
542                         snd_es18xx_dsp_command(chip, 0xD1);
543 #endif
544                 break;
545         case SNDRV_PCM_TRIGGER_STOP:
546         case SNDRV_PCM_TRIGGER_SUSPEND:
547                 if (!(chip->active & DAC2))
548                         return 0;
549                 chip->active &= ~DAC2;
550                 /* Stop DMA */
551                 snd_es18xx_mixer_write(chip, 0x78, 0x00);
552 #ifdef AVOID_POPS
553                 udelay(25000);
554                 if (chip->caps & ES18XX_PCM2)
555                         /* Set Audio 2 volume to 0 */
556                         snd_es18xx_mixer_write(chip, 0x7C, 0);
557                 else
558                         /* Disable PCM output */
559                         snd_es18xx_dsp_command(chip, 0xD3);
560 #endif
561                 break;
562         default:
563                 return -EINVAL;
564         }
565
566         return 0;
567 }
568
569 static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
570                                         struct snd_pcm_hw_params *hw_params)
571 {
572         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
573         int shift, err;
574
575         shift = 0;
576         if ((chip->caps & ES18XX_DUPLEX_MONO) &&
577             chip->playback_a_substream &&
578             params_channels(hw_params) != 1) {
579                 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
580                 return -EBUSY;
581         }
582         if (params_channels(hw_params) == 2)
583                 shift++;
584         if (snd_pcm_format_width(params_format(hw_params)) == 16)
585                 shift++;
586         chip->dma1_shift = shift;
587         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
588                 return err;
589         return 0;
590 }
591
592 static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
593 {
594         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
595         struct snd_pcm_runtime *runtime = substream->runtime;
596         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
597         unsigned int count = snd_pcm_lib_period_bytes(substream);
598
599         chip->dma1_size = size;
600
601         snd_es18xx_reset_fifo(chip);
602
603         /* Set stereo/mono */
604         snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
605
606         snd_es18xx_rate_set(chip, substream, ADC1);
607
608         /* Transfer Count Reload */
609         count = 0x10000 - count;
610         snd_es18xx_write(chip, 0xA4, count & 0xff);
611         snd_es18xx_write(chip, 0xA5, count >> 8);
612
613 #ifdef AVOID_POPS
614         udelay(100000);
615 #endif
616
617         /* Set format */
618         snd_es18xx_write(chip, 0xB7, 
619                          snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
620         snd_es18xx_write(chip, 0xB7, 0x90 |
621                          ((runtime->channels == 1) ? 0x40 : 0x08) |
622                          (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
623                          (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
624
625         /* Set DMA controller */
626         snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
627
628         return 0;
629 }
630
631 static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
632                                       int cmd)
633 {
634         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
635
636         switch (cmd) {
637         case SNDRV_PCM_TRIGGER_START:
638         case SNDRV_PCM_TRIGGER_RESUME:
639                 if (chip->active & ADC1)
640                         return 0;
641                 chip->active |= ADC1;
642                 /* Start DMA */
643                 snd_es18xx_write(chip, 0xB8, 0x0f);
644                 break;
645         case SNDRV_PCM_TRIGGER_STOP:
646         case SNDRV_PCM_TRIGGER_SUSPEND:
647                 if (!(chip->active & ADC1))
648                         return 0;
649                 chip->active &= ~ADC1;
650                 /* Stop DMA */
651                 snd_es18xx_write(chip, 0xB8, 0x00);
652                 break;
653         default:
654                 return -EINVAL;
655         }
656
657         return 0;
658 }
659
660 static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
661                                         struct snd_pcm_substream *substream)
662 {
663         struct snd_pcm_runtime *runtime = substream->runtime;
664         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
665         unsigned int count = snd_pcm_lib_period_bytes(substream);
666
667         chip->dma1_size = size;
668
669         snd_es18xx_reset_fifo(chip);
670
671         /* Set stereo/mono */
672         snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
673
674         snd_es18xx_rate_set(chip, substream, DAC1);
675
676         /* Transfer Count Reload */
677         count = 0x10000 - count;
678         snd_es18xx_write(chip, 0xA4, count & 0xff);
679         snd_es18xx_write(chip, 0xA5, count >> 8);
680
681         /* Set format */
682         snd_es18xx_write(chip, 0xB6,
683                          snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
684         snd_es18xx_write(chip, 0xB7, 
685                          snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
686         snd_es18xx_write(chip, 0xB7, 0x90 |
687                          (runtime->channels == 1 ? 0x40 : 0x08) |
688                          (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
689                          (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
690
691         /* Set DMA controller */
692         snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
693
694         return 0;
695 }
696
697 static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
698                                         struct snd_pcm_substream *substream,
699                                         int cmd)
700 {
701         switch (cmd) {
702         case SNDRV_PCM_TRIGGER_START:
703         case SNDRV_PCM_TRIGGER_RESUME:
704                 if (chip->active & DAC1)
705                         return 0;
706                 chip->active |= DAC1;
707                 /* Start DMA */
708                 snd_es18xx_write(chip, 0xB8, 0x05);
709 #ifdef AVOID_POPS
710                 /* Avoid pops */
711                 udelay(100000);
712                 /* Enable Audio 1 */
713                 snd_es18xx_dsp_command(chip, 0xD1);
714 #endif
715                 break;
716         case SNDRV_PCM_TRIGGER_STOP:
717         case SNDRV_PCM_TRIGGER_SUSPEND:
718                 if (!(chip->active & DAC1))
719                         return 0;
720                 chip->active &= ~DAC1;
721                 /* Stop DMA */
722                 snd_es18xx_write(chip, 0xB8, 0x00);
723 #ifdef AVOID_POPS
724                 /* Avoid pops */
725                 udelay(25000);
726                 /* Disable Audio 1 */
727                 snd_es18xx_dsp_command(chip, 0xD3);
728 #endif
729                 break;
730         default:
731                 return -EINVAL;
732         }
733
734         return 0;
735 }
736
737 static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
738 {
739         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
740         if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
741                 return snd_es18xx_playback1_prepare(chip, substream);
742         else
743                 return snd_es18xx_playback2_prepare(chip, substream);
744 }
745
746 static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
747                                        int cmd)
748 {
749         struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
750         if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
751                 return snd_es18xx_playback1_trigger(chip, substream, cmd);
752         else
753                 return snd_es18xx_playback2_trigger(chip, substream, cmd);
754 }
755
756 static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
757 {
758         struct snd_es18xx *chip = dev_id;
759         unsigned char status;
760
761         if (chip->caps & ES18XX_CONTROL) {
762                 /* Read Interrupt status */
763                 status = inb(chip->ctrl_port + 6);
764         } else {
765                 /* Read Interrupt status */
766                 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
767         }
768 #if 0
769         else {
770                 status = 0;
771                 if (inb(chip->port + 0x0C) & 0x01)
772                         status |= AUDIO1_IRQ;
773                 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
774                         status |= AUDIO2_IRQ;
775                 if ((chip->caps & ES18XX_HWV) &&
776                     snd_es18xx_mixer_read(chip, 0x64) & 0x10)
777                         status |= HWV_IRQ;
778         }
779 #endif
780
781         /* Audio 1 & Audio 2 */
782         if (status & AUDIO2_IRQ) {
783                 if (chip->active & DAC2)
784                         snd_pcm_period_elapsed(chip->playback_a_substream);
785                 /* ack interrupt */
786                 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
787         }
788         if (status & AUDIO1_IRQ) {
789                 /* ok.. capture is active */
790                 if (chip->active & ADC1)
791                         snd_pcm_period_elapsed(chip->capture_a_substream);
792                 /* ok.. playback2 is active */
793                 else if (chip->active & DAC1)
794                         snd_pcm_period_elapsed(chip->playback_b_substream);
795                 /* ack interrupt */
796                 inb(chip->port + 0x0E);
797         }
798
799         /* MPU */
800         if ((status & MPU_IRQ) && chip->rmidi)
801                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
802
803         /* Hardware volume */
804         if (status & HWV_IRQ) {
805                 int split = 0;
806                 if (chip->caps & ES18XX_HWV) {
807                         split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
808                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
809                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
810                 }
811                 if (!split) {
812                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
813                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &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("PC Speaker 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_es18xx *chip, int device, struct snd_pcm ** rpcm)
1695 {
1696         struct snd_pcm *pcm;
1697         char str[16];
1698         int err;
1699
1700         if (rpcm)
1701                 *rpcm = NULL;
1702         sprintf(str, "ES%x", chip->version);
1703         if (chip->caps & ES18XX_PCM2)
1704                 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
1705         else
1706                 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
1707         if (err < 0)
1708                 return err;
1709
1710         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1711         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1712
1713         /* global setup */
1714         pcm->private_data = chip;
1715         pcm->info_flags = 0;
1716         if (chip->caps & ES18XX_DUPLEX_SAME)
1717                 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1718         if (! (chip->caps & ES18XX_PCM2))
1719                 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1720         sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1721         chip->pcm = pcm;
1722
1723         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1724                                               snd_dma_isa_data(),
1725                                               64*1024,
1726                                               chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1727
1728         if (rpcm)
1729                 *rpcm = pcm;
1730         return 0;
1731 }
1732
1733 /* Power Management support functions */
1734 #ifdef CONFIG_PM
1735 static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1736 {
1737         struct snd_audiodrive *acard = card->private_data;
1738         struct snd_es18xx *chip = acard->chip;
1739
1740         snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
1741
1742         snd_pcm_suspend_all(chip->pcm);
1743
1744         /* power down */
1745         chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1746         chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1747         snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1748         snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1749
1750         return 0;
1751 }
1752
1753 static int snd_es18xx_resume(struct snd_card *card)
1754 {
1755         struct snd_audiodrive *acard = card->private_data;
1756         struct snd_es18xx *chip = acard->chip;
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(chip->card, SNDRV_CTL_POWER_D0);
1762         return 0;
1763 }
1764 #endif /* CONFIG_PM */
1765
1766 static int snd_es18xx_free(struct snd_es18xx *chip)
1767 {
1768         release_and_free_resource(chip->res_port);
1769         release_and_free_resource(chip->res_ctrl_port);
1770         release_and_free_resource(chip->res_mpu_port);
1771         if (chip->irq >= 0)
1772                 free_irq(chip->irq, (void *) chip);
1773         if (chip->dma1 >= 0) {
1774                 disable_dma(chip->dma1);
1775                 free_dma(chip->dma1);
1776         }
1777         if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1778                 disable_dma(chip->dma2);
1779                 free_dma(chip->dma2);
1780         }
1781         kfree(chip);
1782         return 0;
1783 }
1784
1785 static int snd_es18xx_dev_free(struct snd_device *device)
1786 {
1787         struct snd_es18xx *chip = device->device_data;
1788         return snd_es18xx_free(chip);
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                                            struct snd_es18xx ** rchip)
1797 {
1798         struct snd_es18xx *chip;
1799         static struct snd_device_ops ops = {
1800                 .dev_free =     snd_es18xx_dev_free,
1801         };
1802         int err;
1803
1804         *rchip = NULL;
1805         chip = kzalloc(sizeof(*chip), GFP_KERNEL);
1806         if (chip == NULL)
1807                 return -ENOMEM;
1808         spin_lock_init(&chip->reg_lock);
1809         spin_lock_init(&chip->mixer_lock);
1810         spin_lock_init(&chip->ctrl_lock);
1811         chip->card = card;
1812         chip->port = port;
1813         chip->mpu_port = mpu_port;
1814         chip->fm_port = fm_port;
1815         chip->irq = -1;
1816         chip->dma1 = -1;
1817         chip->dma2 = -1;
1818         chip->audio2_vol = 0x00;
1819         chip->active = 0;
1820
1821         if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1822                 snd_es18xx_free(chip);
1823                 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1824                 return -EBUSY;
1825         }
1826
1827         if (request_irq(irq, snd_es18xx_interrupt, IRQF_DISABLED, "ES18xx", (void *) chip)) {
1828                 snd_es18xx_free(chip);
1829                 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1830                 return -EBUSY;
1831         }
1832         chip->irq = irq;
1833
1834         if (request_dma(dma1, "ES18xx DMA 1")) {
1835                 snd_es18xx_free(chip);
1836                 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1837                 return -EBUSY;
1838         }
1839         chip->dma1 = dma1;
1840
1841         if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1842                 snd_es18xx_free(chip);
1843                 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1844                 return -EBUSY;
1845         }
1846         chip->dma2 = dma2;
1847
1848         if (snd_es18xx_probe(chip) < 0) {
1849                 snd_es18xx_free(chip);
1850                 return -ENODEV;
1851         }
1852         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1853                 snd_es18xx_free(chip);
1854                 return err;
1855         }
1856         *rchip = chip;
1857         return 0;
1858 }
1859
1860 static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
1861 {
1862         struct snd_card *card;
1863         int err;
1864         unsigned int idx;
1865
1866         card = chip->card;
1867
1868         strcpy(card->mixername, chip->pcm->name);
1869
1870         for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
1871                 struct snd_kcontrol *kctl;
1872                 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1873                 if (chip->caps & ES18XX_HWV) {
1874                         switch (idx) {
1875                         case 0:
1876                                 chip->master_volume = kctl;
1877                                 kctl->private_free = snd_es18xx_hwv_free;
1878                                 break;
1879                         case 1:
1880                                 chip->master_switch = kctl;
1881                                 kctl->private_free = snd_es18xx_hwv_free;
1882                                 break;
1883                         }
1884                 }
1885                 if ((err = snd_ctl_add(card, kctl)) < 0)
1886                         return err;
1887         }
1888         if (chip->caps & ES18XX_PCM2) {
1889                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1890                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1891                                 return err;
1892                 } 
1893         } else {
1894                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1895                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1896                                 return err;
1897                 }
1898         }
1899
1900         if (chip->caps & ES18XX_RECMIX) {
1901                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1902                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1903                                 return err;
1904                 }
1905         }
1906         switch (chip->version) {
1907         default:
1908                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1909                         return err;
1910                 break;
1911         case 0x1869:
1912         case 0x1879:
1913                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1914                         return err;
1915                 break;
1916         }
1917         if (chip->caps & ES18XX_SPATIALIZER) {
1918                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1919                         if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1920                                 return err;
1921                 }
1922         }
1923         if (chip->caps & ES18XX_HWV) {
1924                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
1925                         struct snd_kcontrol *kctl;
1926                         kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1927                         if (idx == 0)
1928                                 chip->hw_volume = kctl;
1929                         else
1930                                 chip->hw_switch = kctl;
1931                         kctl->private_free = snd_es18xx_hwv_free;
1932                         if ((err = snd_ctl_add(card, kctl)) < 0)
1933                                 return err;
1934                         
1935                 }
1936         }
1937         /* finish initializing other chipset specific controls
1938          */
1939         if (chip->version != 0x1868) {
1940                 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1941                                                      chip));
1942                 if (err < 0)
1943                         return err;
1944         }
1945         if (chip->version == 0x1869) {
1946                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1947                         err = snd_ctl_add(card,
1948                                           snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1949                                                        chip));
1950                         if (err < 0)
1951                                 return err;
1952                 }
1953         } else if (chip->version == 0x1878) {
1954                 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1955                                                      chip));
1956                 if (err < 0)
1957                         return err;
1958         } else if (chip->version == 0x1879) {
1959                 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1960                         err = snd_ctl_add(card,
1961                                           snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1962                                                        chip));
1963                         if (err < 0)
1964                                 return err;
1965                 }
1966         }
1967         return 0;
1968 }
1969        
1970
1971 /* Card level */
1972
1973 MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");  
1974 MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1975 MODULE_LICENSE("GPL");
1976 MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1977                 "{ESS,ES1869 PnP AudioDrive},"
1978                 "{ESS,ES1878 PnP AudioDrive},"
1979                 "{ESS,ES1879 PnP AudioDrive},"
1980                 "{ESS,ES1887 PnP AudioDrive},"
1981                 "{ESS,ES1888 PnP AudioDrive},"
1982                 "{ESS,ES1887 AudioDrive},"
1983                 "{ESS,ES1888 AudioDrive}}");
1984
1985 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
1986 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
1987 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1988 #ifdef CONFIG_PNP
1989 static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1990 #endif
1991 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;     /* 0x220,0x240,0x260,0x280 */
1992 #ifndef CONFIG_PNP
1993 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1994 #else
1995 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1996 #endif
1997 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1998 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;        /* 5,7,9,10 */
1999 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
2000 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;       /* 0,1,3 */
2001
2002 module_param_array(index, int, NULL, 0444);
2003 MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
2004 module_param_array(id, charp, NULL, 0444);
2005 MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
2006 module_param_array(enable, bool, NULL, 0444);
2007 MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
2008 #ifdef CONFIG_PNP
2009 module_param_array(isapnp, bool, NULL, 0444);
2010 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
2011 #endif
2012 module_param_array(port, long, NULL, 0444);
2013 MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
2014 module_param_array(mpu_port, long, NULL, 0444);
2015 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
2016 module_param_array(fm_port, long, NULL, 0444);
2017 MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
2018 module_param_array(irq, int, NULL, 0444);
2019 MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
2020 module_param_array(dma1, int, NULL, 0444);
2021 MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
2022 module_param_array(dma2, int, NULL, 0444);
2023 MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
2024
2025 #ifdef CONFIG_PNP
2026 static int isa_registered;
2027 static int pnp_registered;
2028 static int pnpc_registered;
2029
2030 static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
2031         { .id = "ESS1869" },
2032         { .id = "ESS1879" },
2033         { .id = "" }            /* end */
2034 };
2035
2036 MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
2037
2038 /* PnP main device initialization */
2039 static int __devinit snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
2040 {
2041         if (pnp_activate_dev(pdev) < 0) {
2042                 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
2043                 return -EBUSY;
2044         }
2045         /* ok. hack using Vendor-Defined Card-Level registers */
2046         /* skip csn and logdev initialization - already done in isapnp_configure */
2047         if (pnp_device_is_isapnp(pdev)) {
2048                 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
2049                 isapnp_write_byte(0x27, pnp_irq(pdev, 0));      /* Hardware Volume IRQ Number */
2050                 if (mpu_port[dev] != SNDRV_AUTO_PORT)
2051                         isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
2052                 isapnp_write_byte(0x72, pnp_irq(pdev, 0));      /* second IRQ */
2053                 isapnp_cfg_end();
2054         }
2055         port[dev] = pnp_port_start(pdev, 0);
2056         fm_port[dev] = pnp_port_start(pdev, 1);
2057         mpu_port[dev] = pnp_port_start(pdev, 2);
2058         dma1[dev] = pnp_dma(pdev, 0);
2059         dma2[dev] = pnp_dma(pdev, 1);
2060         irq[dev] = pnp_irq(pdev, 0);
2061         snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2062         snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2063         return 0;
2064 }
2065
2066 static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
2067                                         struct pnp_dev *pdev)
2068 {
2069         acard->dev = pdev;
2070         if (snd_audiodrive_pnp_init_main(dev, acard->dev) < 0)
2071                 return -EBUSY;
2072         return 0;
2073 }
2074
2075 static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
2076         /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
2077         { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
2078         /* ESS 1868 (integrated on Maxisound Cards) */
2079         { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
2080         /* ESS 1868 (integrated on Maxisound Cards) */
2081         { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2082         /* ESS ES1869 Plug and Play AudioDrive */
2083         { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2084         /* ESS 1869 */
2085         { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2086         /* ESS 1878 */
2087         { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2088         /* ESS 1879 */
2089         { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2090         /* --- */
2091         { .id = "" } /* end */
2092 };
2093
2094 MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2095
2096 static int __devinit snd_audiodrive_pnpc(int dev, struct snd_audiodrive *acard,
2097                                         struct pnp_card_link *card,
2098                                         const struct pnp_card_device_id *id)
2099 {
2100         acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2101         if (acard->dev == NULL)
2102                 return -EBUSY;
2103
2104         acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2105         if (acard->devc == NULL)
2106                 return -EBUSY;
2107
2108         /* Control port initialization */
2109         if (pnp_activate_dev(acard->devc) < 0) {
2110                 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2111                 return -EAGAIN;
2112         }
2113         snd_printdd("pnp: port=0x%llx\n",
2114                         (unsigned long long)pnp_port_start(acard->devc, 0));
2115         if (snd_audiodrive_pnp_init_main(dev, acard->dev) < 0)
2116                 return -EBUSY;
2117
2118         return 0;
2119 }
2120 #endif /* CONFIG_PNP */
2121
2122 #ifdef CONFIG_PNP
2123 #define is_isapnp_selected(dev)         isapnp[dev]
2124 #else
2125 #define is_isapnp_selected(dev)         0
2126 #endif
2127
2128 static struct snd_card *snd_es18xx_card_new(int dev)
2129 {
2130         return snd_card_new(index[dev], id[dev], THIS_MODULE,
2131                             sizeof(struct snd_audiodrive));
2132 }
2133
2134 static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2135 {
2136         struct snd_audiodrive *acard = card->private_data;
2137         struct snd_es18xx *chip;
2138         struct snd_opl3 *opl3;
2139         int err;
2140
2141         if ((err = snd_es18xx_new_device(card,
2142                                          port[dev],
2143                                          mpu_port[dev],
2144                                          fm_port[dev],
2145                                          irq[dev], dma1[dev], dma2[dev],
2146                                          &chip)) < 0)
2147                 return err;
2148         acard->chip = chip;
2149
2150         sprintf(card->driver, "ES%x", chip->version);
2151         
2152         sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2153         if (dma1[dev] != dma2[dev])
2154                 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2155                         card->shortname,
2156                         chip->port,
2157                         irq[dev], dma1[dev], dma2[dev]);
2158         else
2159                 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2160                         card->shortname,
2161                         chip->port,
2162                         irq[dev], dma1[dev]);
2163
2164         if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
2165                 return err;
2166
2167         if ((err = snd_es18xx_mixer(chip)) < 0)
2168                 return err;
2169
2170         if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2171                 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
2172                         snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
2173                 } else {
2174                         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
2175                                 return err;
2176                 }
2177         }
2178
2179         if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2180                 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2181                                                chip->mpu_port, 0,
2182                                                irq[dev], 0,
2183                                                &chip->rmidi)) < 0)
2184                         return err;
2185         }
2186
2187         return snd_card_register(card);
2188 }
2189
2190 static int __devinit snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2191 {
2192         return enable[dev] && !is_isapnp_selected(dev);
2193 }
2194
2195 static int __devinit snd_es18xx_isa_probe1(int dev, struct device *devptr)
2196 {
2197         struct snd_card *card;
2198         int err;
2199
2200         card = snd_es18xx_card_new(dev);
2201         if (! card)
2202                 return -ENOMEM;
2203         snd_card_set_dev(card, devptr);
2204         if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2205                 snd_card_free(card);
2206                 return err;
2207         }
2208         dev_set_drvdata(devptr, card);
2209         return 0;
2210 }
2211
2212 static int __devinit snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
2213 {
2214         int err;
2215         static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2216         static int possible_dmas[] = {1, 0, 3, 5, -1};
2217
2218         if (irq[dev] == SNDRV_AUTO_IRQ) {
2219                 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2220                         snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2221                         return -EBUSY;
2222                 }
2223         }
2224         if (dma1[dev] == SNDRV_AUTO_DMA) {
2225                 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2226                         snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2227                         return -EBUSY;
2228                 }
2229         }
2230         if (dma2[dev] == SNDRV_AUTO_DMA) {
2231                 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2232                         snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2233                         return -EBUSY;
2234                 }
2235         }
2236
2237         if (port[dev] != SNDRV_AUTO_PORT) {
2238                 return snd_es18xx_isa_probe1(dev, pdev);
2239         } else {
2240                 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2241                 int i;
2242                 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2243                         port[dev] = possible_ports[i];
2244                         err = snd_es18xx_isa_probe1(dev, pdev);
2245                         if (! err)
2246                                 return 0;
2247                 }
2248                 return err;
2249         }
2250 }
2251
2252 static int __devexit snd_es18xx_isa_remove(struct device *devptr,
2253                                            unsigned int dev)
2254 {
2255         snd_card_free(dev_get_drvdata(devptr));
2256         dev_set_drvdata(devptr, NULL);
2257         return 0;
2258 }
2259
2260 #ifdef CONFIG_PM
2261 static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2262                                   pm_message_t state)
2263 {
2264         return snd_es18xx_suspend(dev_get_drvdata(dev), state);
2265 }
2266
2267 static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
2268 {
2269         return snd_es18xx_resume(dev_get_drvdata(dev));
2270 }
2271 #endif
2272
2273 #define DEV_NAME "es18xx"
2274
2275 static struct isa_driver snd_es18xx_isa_driver = {
2276         .match          = snd_es18xx_isa_match,
2277         .probe          = snd_es18xx_isa_probe,
2278         .remove         = __devexit_p(snd_es18xx_isa_remove),
2279 #ifdef CONFIG_PM
2280         .suspend        = snd_es18xx_isa_suspend,
2281         .resume         = snd_es18xx_isa_resume,
2282 #endif
2283         .driver         = {
2284                 .name   = DEV_NAME
2285         },
2286 };
2287
2288
2289 #ifdef CONFIG_PNP
2290 static int __devinit snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2291                                             const struct pnp_device_id *id)
2292 {
2293         static int dev;
2294         int err;
2295         struct snd_card *card;
2296
2297         if (pnp_device_is_isapnp(pdev))
2298                 return -ENOENT; /* we have another procedure - card */
2299         for (; dev < SNDRV_CARDS; dev++) {
2300                 if (enable[dev] && isapnp[dev])
2301                         break;
2302         }
2303         if (dev >= SNDRV_CARDS)
2304                 return -ENODEV;
2305
2306         card = snd_es18xx_card_new(dev);
2307         if (! card)
2308                 return -ENOMEM;
2309         if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
2310                 snd_card_free(card);
2311                 return err;
2312         }
2313         snd_card_set_dev(card, &pdev->dev);
2314         if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2315                 snd_card_free(card);
2316                 return err;
2317         }
2318         pnp_set_drvdata(pdev, card);
2319         dev++;
2320         return 0;
2321 }
2322
2323 static void __devexit snd_audiodrive_pnp_remove(struct pnp_dev * pdev)
2324 {
2325         snd_card_free(pnp_get_drvdata(pdev));
2326         pnp_set_drvdata(pdev, NULL);
2327 }
2328
2329 #ifdef CONFIG_PM
2330 static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2331 {
2332         return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2333 }
2334 static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2335 {
2336         return snd_es18xx_resume(pnp_get_drvdata(pdev));
2337 }
2338 #endif
2339
2340 static struct pnp_driver es18xx_pnp_driver = {
2341         .name = "es18xx-pnpbios",
2342         .id_table = snd_audiodrive_pnpbiosids,
2343         .probe = snd_audiodrive_pnp_detect,
2344         .remove = __devexit_p(snd_audiodrive_pnp_remove),
2345 #ifdef CONFIG_PM
2346         .suspend = snd_audiodrive_pnp_suspend,
2347         .resume = snd_audiodrive_pnp_resume,
2348 #endif
2349 };
2350
2351 static int __devinit snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2352                                                const struct pnp_card_device_id *pid)
2353 {
2354         static int dev;
2355         struct snd_card *card;
2356         int res;
2357
2358         for ( ; dev < SNDRV_CARDS; dev++) {
2359                 if (enable[dev] && isapnp[dev])
2360                         break;
2361         }
2362         if (dev >= SNDRV_CARDS)
2363                 return -ENODEV;
2364
2365         card = snd_es18xx_card_new(dev);
2366         if (! card)
2367                 return -ENOMEM;
2368
2369         if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
2370                 snd_card_free(card);
2371                 return res;
2372         }
2373         snd_card_set_dev(card, &pcard->card->dev);
2374         if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2375                 snd_card_free(card);
2376                 return res;
2377         }
2378
2379         pnp_set_card_drvdata(pcard, card);
2380         dev++;
2381         return 0;
2382 }
2383
2384 static void __devexit snd_audiodrive_pnpc_remove(struct pnp_card_link * pcard)
2385 {
2386         snd_card_free(pnp_get_card_drvdata(pcard));
2387         pnp_set_card_drvdata(pcard, NULL);
2388 }
2389
2390 #ifdef CONFIG_PM
2391 static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
2392 {
2393         return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2394 }
2395
2396 static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
2397 {
2398         return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2399 }
2400
2401 #endif
2402
2403 static struct pnp_card_driver es18xx_pnpc_driver = {
2404         .flags = PNP_DRIVER_RES_DISABLE,
2405         .name = "es18xx",
2406         .id_table = snd_audiodrive_pnpids,
2407         .probe = snd_audiodrive_pnpc_detect,
2408         .remove = __devexit_p(snd_audiodrive_pnpc_remove),
2409 #ifdef CONFIG_PM
2410         .suspend        = snd_audiodrive_pnpc_suspend,
2411         .resume         = snd_audiodrive_pnpc_resume,
2412 #endif
2413 };
2414 #endif /* CONFIG_PNP */
2415
2416 static int __init alsa_card_es18xx_init(void)
2417 {
2418         int err;
2419
2420         err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
2421 #ifdef CONFIG_PNP
2422         if (!err)
2423                 isa_registered = 1;
2424
2425         err = pnp_register_driver(&es18xx_pnp_driver);
2426         if (!err)
2427                 pnp_registered = 1;
2428
2429         err = pnp_register_card_driver(&es18xx_pnpc_driver);
2430         if (!err)
2431                 pnpc_registered = 1;
2432
2433         if (isa_registered || pnp_registered)
2434                 err = 0;
2435 #endif
2436         return err;
2437 }
2438
2439 static void __exit alsa_card_es18xx_exit(void)
2440 {
2441 #ifdef CONFIG_PNP
2442         if (pnpc_registered)
2443                 pnp_unregister_card_driver(&es18xx_pnpc_driver);
2444         if (pnp_registered)
2445                 pnp_unregister_driver(&es18xx_pnp_driver);
2446         if (isa_registered)
2447 #endif
2448                 isa_unregister_driver(&snd_es18xx_isa_driver);
2449 }
2450
2451 module_init(alsa_card_es18xx_init)
2452 module_exit(alsa_card_es18xx_exit)