xref: /linux/sound/isa/es18xx.c (revision 24bce201d79807b668bf9d9e0aca801c5c0d5f78)
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 	int data;
1348 
1349 	outb(reg, chip->ctrl_port);
1350 	data = inb(chip->ctrl_port + 1);
1351 	return data;
1352 }
1353 
1354 static void snd_es18xx_config_write(struct snd_es18xx *chip,
1355 				    unsigned char reg, unsigned char data)
1356 {
1357 	/* No need for spinlocks, this function is used only in
1358 	   otherwise protected init code */
1359 	outb(reg, chip->ctrl_port);
1360 	outb(data, chip->ctrl_port + 1);
1361 #ifdef REG_DEBUG
1362 	snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
1363 #endif
1364 }
1365 
1366 static int snd_es18xx_initialize(struct snd_es18xx *chip,
1367 				 unsigned long mpu_port,
1368 				 unsigned long fm_port)
1369 {
1370 	int mask = 0;
1371 
1372         /* enable extended mode */
1373         snd_es18xx_dsp_command(chip, 0xC6);
1374 	/* Reset mixer registers */
1375 	snd_es18xx_mixer_write(chip, 0x00, 0x00);
1376 
1377         /* Audio 1 DMA demand mode (4 bytes/request) */
1378         snd_es18xx_write(chip, 0xB9, 2);
1379 	if (chip->caps & ES18XX_CONTROL) {
1380 		/* Hardware volume IRQ */
1381 		snd_es18xx_config_write(chip, 0x27, chip->irq);
1382 		if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1383 			/* FM I/O */
1384 			snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
1385 			snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
1386 		}
1387 		if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1388 			/* MPU-401 I/O */
1389 			snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
1390 			snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
1391 			/* MPU-401 IRQ */
1392 			snd_es18xx_config_write(chip, 0x28, chip->irq);
1393 		}
1394 		/* Audio1 IRQ */
1395 		snd_es18xx_config_write(chip, 0x70, chip->irq);
1396 		/* Audio2 IRQ */
1397 		snd_es18xx_config_write(chip, 0x72, chip->irq);
1398 		/* Audio1 DMA */
1399 		snd_es18xx_config_write(chip, 0x74, chip->dma1);
1400 		/* Audio2 DMA */
1401 		snd_es18xx_config_write(chip, 0x75, chip->dma2);
1402 
1403 		/* Enable Audio 1 IRQ */
1404 		snd_es18xx_write(chip, 0xB1, 0x50);
1405 		/* Enable Audio 2 IRQ */
1406 		snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1407 		/* Enable Audio 1 DMA */
1408 		snd_es18xx_write(chip, 0xB2, 0x50);
1409 		/* Enable MPU and hardware volume interrupt */
1410 		snd_es18xx_mixer_write(chip, 0x64, 0x42);
1411 		/* Enable ESS wavetable input */
1412 		snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
1413 	}
1414 	else {
1415 		int irqmask, dma1mask, dma2mask;
1416 		switch (chip->irq) {
1417 		case 2:
1418 		case 9:
1419 			irqmask = 0;
1420 			break;
1421 		case 5:
1422 			irqmask = 1;
1423 			break;
1424 		case 7:
1425 			irqmask = 2;
1426 			break;
1427 		case 10:
1428 			irqmask = 3;
1429 			break;
1430 		default:
1431 			snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
1432 			return -ENODEV;
1433 		}
1434 		switch (chip->dma1) {
1435 		case 0:
1436 			dma1mask = 1;
1437 			break;
1438 		case 1:
1439 			dma1mask = 2;
1440 			break;
1441 		case 3:
1442 			dma1mask = 3;
1443 			break;
1444 		default:
1445 			snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
1446 			return -ENODEV;
1447 		}
1448 		switch (chip->dma2) {
1449 		case 0:
1450 			dma2mask = 0;
1451 			break;
1452 		case 1:
1453 			dma2mask = 1;
1454 			break;
1455 		case 3:
1456 			dma2mask = 2;
1457 			break;
1458 		case 5:
1459 			dma2mask = 3;
1460 			break;
1461 		default:
1462 			snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
1463 			return -ENODEV;
1464 		}
1465 
1466 		/* Enable and set Audio 1 IRQ */
1467 		snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1468 		/* Enable and set Audio 1 DMA */
1469 		snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1470 		/* Set Audio 2 DMA */
1471 		snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1472 		/* Enable Audio 2 IRQ and DMA
1473 		   Set capture mixer input */
1474 		snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1475 		/* Enable and set hardware volume interrupt */
1476 		snd_es18xx_mixer_write(chip, 0x64, 0x06);
1477 		if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1478 			/* MPU401 share irq with audio
1479 			   Joystick enabled
1480 			   FM enabled */
1481 			snd_es18xx_mixer_write(chip, 0x40,
1482 					       0x43 | (mpu_port & 0xf0) >> 1);
1483 		}
1484 		snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1485 	}
1486 	if (chip->caps & ES18XX_NEW_RATE) {
1487 		/* Change behaviour of register A1
1488 		   4x oversampling
1489 		   2nd channel DAC asynchronous */
1490 		snd_es18xx_mixer_write(chip, 0x71, 0x32);
1491 	}
1492 	if (!(chip->caps & ES18XX_PCM2)) {
1493 		/* Enable DMA FIFO */
1494 		snd_es18xx_write(chip, 0xB7, 0x80);
1495 	}
1496 	if (chip->caps & ES18XX_SPATIALIZER) {
1497 		/* Set spatializer parameters to recommended values */
1498 		snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1499 		snd_es18xx_mixer_write(chip, 0x56, 0x95);
1500 		snd_es18xx_mixer_write(chip, 0x58, 0x94);
1501 		snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1502 	}
1503 	/* Flip the "enable I2S" bits for those chipsets that need it */
1504 	switch (chip->version) {
1505 	case 0x1879:
1506 		//Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
1507 		//so a Switch control has been added to toggle this 0x71 bit on/off:
1508 		//snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
1509 		/* Note: we fall through on purpose here. */
1510 	case 0x1878:
1511 		snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
1512 		break;
1513 	}
1514 	/* Mute input source */
1515 	if (chip->caps & ES18XX_MUTEREC)
1516 		mask = 0x10;
1517 	if (chip->caps & ES18XX_RECMIX)
1518 		snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1519 	else {
1520 		snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1521 		snd_es18xx_write(chip, 0xb4, 0x00);
1522 	}
1523 #ifndef AVOID_POPS
1524 	/* Enable PCM output */
1525 	snd_es18xx_dsp_command(chip, 0xD1);
1526 #endif
1527 
1528         return 0;
1529 }
1530 
1531 static int snd_es18xx_identify(struct snd_card *card, struct snd_es18xx *chip)
1532 {
1533 	int hi,lo;
1534 
1535 	/* reset */
1536 	if (snd_es18xx_reset(chip) < 0) {
1537 		snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
1538 		return -ENODEV;
1539 	}
1540 
1541 	snd_es18xx_dsp_command(chip, 0xe7);
1542 	hi = snd_es18xx_dsp_get_byte(chip);
1543 	if (hi < 0) {
1544 		return hi;
1545 	}
1546 	lo = snd_es18xx_dsp_get_byte(chip);
1547 	if ((lo & 0xf0) != 0x80) {
1548 		return -ENODEV;
1549 	}
1550 	if (hi == 0x48) {
1551 		chip->version = 0x488;
1552 		return 0;
1553 	}
1554 	if (hi != 0x68) {
1555 		return -ENODEV;
1556 	}
1557 	if ((lo & 0x0f) < 8) {
1558 		chip->version = 0x688;
1559 		return 0;
1560 	}
1561 
1562         outb(0x40, chip->port + 0x04);
1563 	udelay(10);
1564 	hi = inb(chip->port + 0x05);
1565 	udelay(10);
1566 	lo = inb(chip->port + 0x05);
1567 	if (hi != lo) {
1568 		chip->version = hi << 8 | lo;
1569 		chip->ctrl_port = inb(chip->port + 0x05) << 8;
1570 		udelay(10);
1571 		chip->ctrl_port += inb(chip->port + 0x05);
1572 
1573 		if (!devm_request_region(card->dev, chip->ctrl_port, 8,
1574 					 "ES18xx - CTRL")) {
1575 			snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1576 			return -EBUSY;
1577 		}
1578 
1579 		return 0;
1580 	}
1581 
1582 	/* If has Hardware volume */
1583 	if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1584 		/* If has Audio2 */
1585 		if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1586 			/* If has volume count */
1587 			if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1588 				chip->version = 0x1887;
1589 			} else {
1590 				chip->version = 0x1888;
1591 			}
1592 		} else {
1593 			chip->version = 0x1788;
1594 		}
1595 	}
1596 	else
1597 		chip->version = 0x1688;
1598 	return 0;
1599 }
1600 
1601 static int snd_es18xx_probe(struct snd_card *card,
1602 			    struct snd_es18xx *chip,
1603 			    unsigned long mpu_port,
1604 			    unsigned long fm_port)
1605 {
1606 	if (snd_es18xx_identify(card, chip) < 0) {
1607 		snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1608                 return -ENODEV;
1609 	}
1610 
1611 	switch (chip->version) {
1612 	case 0x1868:
1613 		chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
1614 		break;
1615 	case 0x1869:
1616 		chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV | ES18XX_GPO_2BIT;
1617 		break;
1618 	case 0x1878:
1619 		chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
1620 		break;
1621 	case 0x1879:
1622 		chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1623 		break;
1624 	case 0x1887:
1625 	case 0x1888:
1626 		chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
1627 		break;
1628 	default:
1629 		snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
1630                            chip->port, chip->version);
1631                 return -ENODEV;
1632         }
1633 
1634         snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1635 
1636 	if (chip->dma1 == chip->dma2)
1637 		chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1638 
1639 	return snd_es18xx_initialize(chip, mpu_port, fm_port);
1640 }
1641 
1642 static const struct snd_pcm_ops snd_es18xx_playback_ops = {
1643 	.open =		snd_es18xx_playback_open,
1644 	.close =	snd_es18xx_playback_close,
1645 	.hw_params =	snd_es18xx_playback_hw_params,
1646 	.prepare =	snd_es18xx_playback_prepare,
1647 	.trigger =	snd_es18xx_playback_trigger,
1648 	.pointer =	snd_es18xx_playback_pointer,
1649 };
1650 
1651 static const struct snd_pcm_ops snd_es18xx_capture_ops = {
1652 	.open =		snd_es18xx_capture_open,
1653 	.close =	snd_es18xx_capture_close,
1654 	.hw_params =	snd_es18xx_capture_hw_params,
1655 	.prepare =	snd_es18xx_capture_prepare,
1656 	.trigger =	snd_es18xx_capture_trigger,
1657 	.pointer =	snd_es18xx_capture_pointer,
1658 };
1659 
1660 static int snd_es18xx_pcm(struct snd_card *card, int device)
1661 {
1662 	struct snd_es18xx *chip = card->private_data;
1663         struct snd_pcm *pcm;
1664 	char str[16];
1665 	int err;
1666 
1667 	sprintf(str, "ES%x", chip->version);
1668 	if (chip->caps & ES18XX_PCM2)
1669 		err = snd_pcm_new(card, str, device, 2, 1, &pcm);
1670 	else
1671 		err = snd_pcm_new(card, str, device, 1, 1, &pcm);
1672         if (err < 0)
1673                 return err;
1674 
1675 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1676 	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1677 
1678 	/* global setup */
1679         pcm->private_data = chip;
1680         pcm->info_flags = 0;
1681 	if (chip->caps & ES18XX_DUPLEX_SAME)
1682 		pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1683 	if (! (chip->caps & ES18XX_PCM2))
1684 		pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1685 	sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1686         chip->pcm = pcm;
1687 
1688 	snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV, card->dev,
1689 				       64*1024,
1690 				       chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1691 	return 0;
1692 }
1693 
1694 /* Power Management support functions */
1695 #ifdef CONFIG_PM
1696 static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1697 {
1698 	struct snd_es18xx *chip = card->private_data;
1699 
1700 	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1701 
1702 	/* power down */
1703 	chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1704 	chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1705 	snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1706 	snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1707 
1708 	return 0;
1709 }
1710 
1711 static int snd_es18xx_resume(struct snd_card *card)
1712 {
1713 	struct snd_es18xx *chip = card->private_data;
1714 
1715 	/* restore PM register, we won't wake till (not 0x07) i/o activity though */
1716 	snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1717 
1718 	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1719 	return 0;
1720 }
1721 #endif /* CONFIG_PM */
1722 
1723 static int snd_es18xx_new_device(struct snd_card *card,
1724 				 unsigned long port,
1725 				 unsigned long mpu_port,
1726 				 unsigned long fm_port,
1727 				 int irq, int dma1, int dma2)
1728 {
1729 	struct snd_es18xx *chip = card->private_data;
1730 
1731 	spin_lock_init(&chip->reg_lock);
1732  	spin_lock_init(&chip->mixer_lock);
1733         chip->port = port;
1734         chip->irq = -1;
1735         chip->dma1 = -1;
1736         chip->dma2 = -1;
1737         chip->audio2_vol = 0x00;
1738 	chip->active = 0;
1739 
1740 	if (!devm_request_region(card->dev, port, 16, "ES18xx")) {
1741 		snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1742 		return -EBUSY;
1743 	}
1744 
1745 	if (devm_request_irq(card->dev, irq, snd_es18xx_interrupt, 0, "ES18xx",
1746 			     (void *) card)) {
1747 		snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1748 		return -EBUSY;
1749 	}
1750 	chip->irq = irq;
1751 	card->sync_irq = chip->irq;
1752 
1753 	if (snd_devm_request_dma(card->dev, dma1, "ES18xx DMA 1")) {
1754 		snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1755 		return -EBUSY;
1756 	}
1757 	chip->dma1 = dma1;
1758 
1759 	if (dma2 != dma1 &&
1760 	    snd_devm_request_dma(card->dev, dma2, "ES18xx DMA 2")) {
1761 		snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1762 		return -EBUSY;
1763 	}
1764 	chip->dma2 = dma2;
1765 
1766 	if (snd_es18xx_probe(card, chip, mpu_port, fm_port) < 0)
1767 		return -ENODEV;
1768         return 0;
1769 }
1770 
1771 static int snd_es18xx_mixer(struct snd_card *card)
1772 {
1773 	struct snd_es18xx *chip = card->private_data;
1774 	int err;
1775 	unsigned int idx;
1776 
1777 	strcpy(card->mixername, chip->pcm->name);
1778 
1779 	for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
1780 		struct snd_kcontrol *kctl;
1781 		kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1782 		if (chip->caps & ES18XX_HWV) {
1783 			switch (idx) {
1784 			case 0:
1785 				chip->master_volume = kctl;
1786 				kctl->private_free = snd_es18xx_hwv_free;
1787 				break;
1788 			case 1:
1789 				chip->master_switch = kctl;
1790 				kctl->private_free = snd_es18xx_hwv_free;
1791 				break;
1792 			}
1793 		}
1794 		err = snd_ctl_add(card, kctl);
1795 		if (err < 0)
1796 			return err;
1797 	}
1798 	if (chip->caps & ES18XX_PCM2) {
1799 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1800 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip));
1801 			if (err < 0)
1802 				return err;
1803 		}
1804 	} else {
1805 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1806 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip));
1807 			if (err < 0)
1808 				return err;
1809 		}
1810 	}
1811 
1812 	if (chip->caps & ES18XX_RECMIX) {
1813 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1814 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip));
1815 			if (err < 0)
1816 				return err;
1817 		}
1818 	}
1819 	switch (chip->version) {
1820 	default:
1821 		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip));
1822 		if (err < 0)
1823 			return err;
1824 		break;
1825 	case 0x1869:
1826 	case 0x1879:
1827 		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip));
1828 		if (err < 0)
1829 			return err;
1830 		break;
1831 	}
1832 	if (chip->caps & ES18XX_SPATIALIZER) {
1833 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1834 			err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip));
1835 			if (err < 0)
1836 				return err;
1837 		}
1838 	}
1839 	if (chip->caps & ES18XX_HWV) {
1840 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
1841 			struct snd_kcontrol *kctl;
1842 			kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1843 			if (idx == 0)
1844 				chip->hw_volume = kctl;
1845 			else
1846 				chip->hw_switch = kctl;
1847 			kctl->private_free = snd_es18xx_hwv_free;
1848 			err = snd_ctl_add(card, kctl);
1849 			if (err < 0)
1850 				return err;
1851 
1852 		}
1853 	}
1854 	/* finish initializing other chipset specific controls
1855 	 */
1856 	if (chip->version != 0x1868) {
1857 		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1858 						     chip));
1859 		if (err < 0)
1860 			return err;
1861 	}
1862 	if (chip->version == 0x1869) {
1863 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1864 			err = snd_ctl_add(card,
1865 					  snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1866 						       chip));
1867 			if (err < 0)
1868 				return err;
1869 		}
1870 	} else if (chip->version == 0x1878) {
1871 		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
1872 						     chip));
1873 		if (err < 0)
1874 			return err;
1875 	} else if (chip->version == 0x1879) {
1876 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
1877 			err = snd_ctl_add(card,
1878 					  snd_ctl_new1(&snd_es18xx_opt_1879[idx],
1879 						       chip));
1880 			if (err < 0)
1881 				return err;
1882 		}
1883 	}
1884 	if (chip->caps & ES18XX_GPO_2BIT) {
1885 		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) {
1886 			err = snd_ctl_add(card,
1887 					  snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx],
1888 						       chip));
1889 			if (err < 0)
1890 				return err;
1891 		}
1892 	}
1893 	return 0;
1894 }
1895 
1896 
1897 /* Card level */
1898 
1899 MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1900 MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1901 MODULE_LICENSE("GPL");
1902 
1903 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
1904 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
1905 static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1906 #ifdef CONFIG_PNP
1907 static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
1908 #endif
1909 static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x240,0x260,0x280 */
1910 #ifndef CONFIG_PNP
1911 static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1912 #else
1913 static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1914 #endif
1915 static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1916 static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
1917 static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
1918 static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
1919 
1920 module_param_array(index, int, NULL, 0444);
1921 MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1922 module_param_array(id, charp, NULL, 0444);
1923 MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1924 module_param_array(enable, bool, NULL, 0444);
1925 MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1926 #ifdef CONFIG_PNP
1927 module_param_array(isapnp, bool, NULL, 0444);
1928 MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1929 #endif
1930 module_param_hw_array(port, long, ioport, NULL, 0444);
1931 MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1932 module_param_hw_array(mpu_port, long, ioport, NULL, 0444);
1933 MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1934 module_param_hw_array(fm_port, long, ioport, NULL, 0444);
1935 MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1936 module_param_hw_array(irq, int, irq, NULL, 0444);
1937 MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1938 module_param_hw_array(dma1, int, dma, NULL, 0444);
1939 MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1940 module_param_hw_array(dma2, int, dma, NULL, 0444);
1941 MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1942 
1943 #ifdef CONFIG_PNP
1944 static int isa_registered;
1945 static int pnp_registered;
1946 static int pnpc_registered;
1947 
1948 static const struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
1949 	{ .id = "ESS1869" },
1950 	{ .id = "ESS1879" },
1951 	{ .id = "" }		/* end */
1952 };
1953 
1954 MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
1955 
1956 /* PnP main device initialization */
1957 static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
1958 {
1959 	if (pnp_activate_dev(pdev) < 0) {
1960 		snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
1961 		return -EBUSY;
1962 	}
1963 	/* ok. hack using Vendor-Defined Card-Level registers */
1964 	/* skip csn and logdev initialization - already done in isapnp_configure */
1965 	if (pnp_device_is_isapnp(pdev)) {
1966 		isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1967 		isapnp_write_byte(0x27, pnp_irq(pdev, 0));	/* Hardware Volume IRQ Number */
1968 		if (mpu_port[dev] != SNDRV_AUTO_PORT)
1969 			isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1970 		isapnp_write_byte(0x72, pnp_irq(pdev, 0));	/* second IRQ */
1971 		isapnp_cfg_end();
1972 	}
1973 	port[dev] = pnp_port_start(pdev, 0);
1974 	fm_port[dev] = pnp_port_start(pdev, 1);
1975 	mpu_port[dev] = pnp_port_start(pdev, 2);
1976 	dma1[dev] = pnp_dma(pdev, 0);
1977 	dma2[dev] = pnp_dma(pdev, 1);
1978 	irq[dev] = pnp_irq(pdev, 0);
1979 	snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
1980 	snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
1981 	return 0;
1982 }
1983 
1984 static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
1985 			      struct pnp_dev *pdev)
1986 {
1987 	chip->dev = pdev;
1988 	if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
1989 		return -EBUSY;
1990 	return 0;
1991 }
1992 
1993 static const struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1994 	/* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1995 	{ .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1996 	/* ESS 1868 (integrated on Maxisound Cards) */
1997 	{ .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1998 	/* ESS 1868 (integrated on Maxisound Cards) */
1999 	{ .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
2000 	/* ESS ES1869 Plug and Play AudioDrive */
2001 	{ .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
2002 	/* ESS 1869 */
2003 	{ .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
2004 	/* ESS 1878 */
2005 	{ .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
2006 	/* ESS 1879 */
2007 	{ .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
2008 	/* --- */
2009 	{ .id = "" } /* end */
2010 };
2011 
2012 MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
2013 
2014 static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
2015 			       struct pnp_card_link *card,
2016 			       const struct pnp_card_device_id *id)
2017 {
2018 	chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
2019 	if (chip->dev == NULL)
2020 		return -EBUSY;
2021 
2022 	chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
2023 	if (chip->devc == NULL)
2024 		return -EBUSY;
2025 
2026 	/* Control port initialization */
2027 	if (pnp_activate_dev(chip->devc) < 0) {
2028 		snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
2029 		return -EAGAIN;
2030 	}
2031 	snd_printdd("pnp: port=0x%llx\n",
2032 			(unsigned long long)pnp_port_start(chip->devc, 0));
2033 	if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
2034 		return -EBUSY;
2035 
2036 	return 0;
2037 }
2038 #endif /* CONFIG_PNP */
2039 
2040 #ifdef CONFIG_PNP
2041 #define is_isapnp_selected(dev)		isapnp[dev]
2042 #else
2043 #define is_isapnp_selected(dev)		0
2044 #endif
2045 
2046 static int snd_es18xx_card_new(struct device *pdev, int dev,
2047 			       struct snd_card **cardp)
2048 {
2049 	return snd_devm_card_new(pdev, index[dev], id[dev], THIS_MODULE,
2050 				 sizeof(struct snd_es18xx), cardp);
2051 }
2052 
2053 static int snd_audiodrive_probe(struct snd_card *card, int dev)
2054 {
2055 	struct snd_es18xx *chip = card->private_data;
2056 	struct snd_opl3 *opl3;
2057 	int err;
2058 
2059 	err = snd_es18xx_new_device(card,
2060 				    port[dev], mpu_port[dev], fm_port[dev],
2061 				    irq[dev], dma1[dev], dma2[dev]);
2062 	if (err < 0)
2063 		return err;
2064 
2065 	sprintf(card->driver, "ES%x", chip->version);
2066 
2067 	sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
2068 	if (dma1[dev] != dma2[dev])
2069 		sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2070 			card->shortname,
2071 			chip->port,
2072 			irq[dev], dma1[dev], dma2[dev]);
2073 	else
2074 		sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2075 			card->shortname,
2076 			chip->port,
2077 			irq[dev], dma1[dev]);
2078 
2079 	err = snd_es18xx_pcm(card, 0);
2080 	if (err < 0)
2081 		return err;
2082 
2083 	err = snd_es18xx_mixer(card);
2084 	if (err < 0)
2085 		return err;
2086 
2087 	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2088 		if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
2089 				    OPL3_HW_OPL3, 0, &opl3) < 0) {
2090 			snd_printk(KERN_WARNING PFX
2091 				   "opl3 not detected at 0x%lx\n",
2092 				   fm_port[dev]);
2093 		} else {
2094 			err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
2095 			if (err < 0)
2096 				return err;
2097 		}
2098 	}
2099 
2100 	if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2101 		err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2102 					  mpu_port[dev], MPU401_INFO_IRQ_HOOK,
2103 					  -1, &chip->rmidi);
2104 		if (err < 0)
2105 			return err;
2106 	}
2107 
2108 	return snd_card_register(card);
2109 }
2110 
2111 static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
2112 {
2113 	return enable[dev] && !is_isapnp_selected(dev);
2114 }
2115 
2116 static int snd_es18xx_isa_probe1(int dev, struct device *devptr)
2117 {
2118 	struct snd_card *card;
2119 	int err;
2120 
2121 	err = snd_es18xx_card_new(devptr, dev, &card);
2122 	if (err < 0)
2123 		return err;
2124 	err = snd_audiodrive_probe(card, dev);
2125 	if (err < 0)
2126 		return err;
2127 	dev_set_drvdata(devptr, card);
2128 	return 0;
2129 }
2130 
2131 static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
2132 {
2133 	int err;
2134 	static const int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2135 	static const int possible_dmas[] = {1, 0, 3, 5, -1};
2136 
2137 	if (irq[dev] == SNDRV_AUTO_IRQ) {
2138 		irq[dev] = snd_legacy_find_free_irq(possible_irqs);
2139 		if (irq[dev] < 0) {
2140 			snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2141 			return -EBUSY;
2142 		}
2143 	}
2144 	if (dma1[dev] == SNDRV_AUTO_DMA) {
2145 		dma1[dev] = snd_legacy_find_free_dma(possible_dmas);
2146 		if (dma1[dev] < 0) {
2147 			snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2148 			return -EBUSY;
2149 		}
2150 	}
2151 	if (dma2[dev] == SNDRV_AUTO_DMA) {
2152 		dma2[dev] = snd_legacy_find_free_dma(possible_dmas);
2153 		if (dma2[dev] < 0) {
2154 			snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2155 			return -EBUSY;
2156 		}
2157 	}
2158 
2159 	if (port[dev] != SNDRV_AUTO_PORT) {
2160 		return snd_es18xx_isa_probe1(dev, pdev);
2161 	} else {
2162 		static const unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2163 		int i;
2164 		for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2165 			port[dev] = possible_ports[i];
2166 			err = snd_es18xx_isa_probe1(dev, pdev);
2167 			if (! err)
2168 				return 0;
2169 		}
2170 		return err;
2171 	}
2172 }
2173 
2174 #ifdef CONFIG_PM
2175 static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
2176 				  pm_message_t state)
2177 {
2178 	return snd_es18xx_suspend(dev_get_drvdata(dev), state);
2179 }
2180 
2181 static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
2182 {
2183 	return snd_es18xx_resume(dev_get_drvdata(dev));
2184 }
2185 #endif
2186 
2187 #define DEV_NAME "es18xx"
2188 
2189 static struct isa_driver snd_es18xx_isa_driver = {
2190 	.match		= snd_es18xx_isa_match,
2191 	.probe		= snd_es18xx_isa_probe,
2192 #ifdef CONFIG_PM
2193 	.suspend	= snd_es18xx_isa_suspend,
2194 	.resume		= snd_es18xx_isa_resume,
2195 #endif
2196 	.driver		= {
2197 		.name	= DEV_NAME
2198 	},
2199 };
2200 
2201 
2202 #ifdef CONFIG_PNP
2203 static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
2204 				     const struct pnp_device_id *id)
2205 {
2206 	static int dev;
2207 	int err;
2208 	struct snd_card *card;
2209 
2210 	if (pnp_device_is_isapnp(pdev))
2211 		return -ENOENT;	/* we have another procedure - card */
2212 	for (; dev < SNDRV_CARDS; dev++) {
2213 		if (enable[dev] && isapnp[dev])
2214 			break;
2215 	}
2216 	if (dev >= SNDRV_CARDS)
2217 		return -ENODEV;
2218 
2219 	err = snd_es18xx_card_new(&pdev->dev, dev, &card);
2220 	if (err < 0)
2221 		return err;
2222 	err = snd_audiodrive_pnp(dev, card->private_data, pdev);
2223 	if (err < 0)
2224 		return err;
2225 	err = snd_audiodrive_probe(card, dev);
2226 	if (err < 0)
2227 		return err;
2228 	pnp_set_drvdata(pdev, card);
2229 	dev++;
2230 	return 0;
2231 }
2232 
2233 #ifdef CONFIG_PM
2234 static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
2235 {
2236 	return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
2237 }
2238 static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
2239 {
2240 	return snd_es18xx_resume(pnp_get_drvdata(pdev));
2241 }
2242 #endif
2243 
2244 static struct pnp_driver es18xx_pnp_driver = {
2245 	.name = "es18xx-pnpbios",
2246 	.id_table = snd_audiodrive_pnpbiosids,
2247 	.probe = snd_audiodrive_pnp_detect,
2248 #ifdef CONFIG_PM
2249 	.suspend = snd_audiodrive_pnp_suspend,
2250 	.resume = snd_audiodrive_pnp_resume,
2251 #endif
2252 };
2253 
2254 static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
2255 				      const struct pnp_card_device_id *pid)
2256 {
2257 	static int dev;
2258 	struct snd_card *card;
2259 	int res;
2260 
2261 	for ( ; dev < SNDRV_CARDS; dev++) {
2262 		if (enable[dev] && isapnp[dev])
2263 			break;
2264 	}
2265 	if (dev >= SNDRV_CARDS)
2266 		return -ENODEV;
2267 
2268 	res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
2269 	if (res < 0)
2270 		return res;
2271 
2272 	res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid);
2273 	if (res < 0)
2274 		return res;
2275 	res = snd_audiodrive_probe(card, dev);
2276 	if (res < 0)
2277 		return res;
2278 
2279 	pnp_set_card_drvdata(pcard, card);
2280 	dev++;
2281 	return 0;
2282 }
2283 
2284 #ifdef CONFIG_PM
2285 static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
2286 {
2287 	return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2288 }
2289 
2290 static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
2291 {
2292 	return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2293 }
2294 
2295 #endif
2296 
2297 static struct pnp_card_driver es18xx_pnpc_driver = {
2298 	.flags = PNP_DRIVER_RES_DISABLE,
2299 	.name = "es18xx",
2300 	.id_table = snd_audiodrive_pnpids,
2301 	.probe = snd_audiodrive_pnpc_detect,
2302 #ifdef CONFIG_PM
2303 	.suspend	= snd_audiodrive_pnpc_suspend,
2304 	.resume		= snd_audiodrive_pnpc_resume,
2305 #endif
2306 };
2307 #endif /* CONFIG_PNP */
2308 
2309 static int __init alsa_card_es18xx_init(void)
2310 {
2311 	int err;
2312 
2313 	err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
2314 #ifdef CONFIG_PNP
2315 	if (!err)
2316 		isa_registered = 1;
2317 
2318 	err = pnp_register_driver(&es18xx_pnp_driver);
2319 	if (!err)
2320 		pnp_registered = 1;
2321 
2322 	err = pnp_register_card_driver(&es18xx_pnpc_driver);
2323 	if (!err)
2324 		pnpc_registered = 1;
2325 
2326 	if (isa_registered || pnp_registered)
2327 		err = 0;
2328 #endif
2329 	return err;
2330 }
2331 
2332 static void __exit alsa_card_es18xx_exit(void)
2333 {
2334 #ifdef CONFIG_PNP
2335 	if (pnpc_registered)
2336 		pnp_unregister_card_driver(&es18xx_pnpc_driver);
2337 	if (pnp_registered)
2338 		pnp_unregister_driver(&es18xx_pnp_driver);
2339 	if (isa_registered)
2340 #endif
2341 		isa_unregister_driver(&snd_es18xx_isa_driver);
2342 }
2343 
2344 module_init(alsa_card_es18xx_init)
2345 module_exit(alsa_card_es18xx_exit)
2346