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