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