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