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