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