Blame view

sound/isa/es18xx.c 69.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  /*
   *  Driver for generic ESS AudioDrive ES18xx soundcards
   *  Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
   *  Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
   *
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */
  /* GENERAL NOTES:
   *
   * BUGS:
   * - There are pops (we can't delay in trigger function, cause midlevel 
   *   often need to trigger down and then up very quickly).
   *   Any ideas?
   * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
   */
  
  /*
   * ES1868  NOTES:
   * - The chip has one half duplex pcm (with very limited full duplex support).
   *
   * - Duplex stereophonic sound is impossible.
   * - Record and playback must share the same frequency rate.
   *
   * - The driver use dma2 for playback and dma1 for capture.
   */
  
  /*
   * ES1869 NOTES:
   *
   * - there are a first full duplex pcm and a second playback only pcm
   *   (incompatible with first pcm capture)
   * 
   * - there is support for the capture volume and ESS Spatializer 3D effect.
   *
   * - contrarily to some pages in DS_1869.PDF the rates can be set
   *   independently.
   *
95b712965   Mark Salazar   [ALSA] #4/4 for Z...
52
53
54
55
   * - Zoom Video is implemented by sharing the FM DAC, thus the user can
   *   have either FM playback or Video playback but not both simultaneously.
   *   The Video Playback Switch mixer control toggles this choice.
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
61
62
63
64
65
66
67
68
   * BUGS:
   *
   * - There is a major trouble I noted:
   *
   *   using both channel for playback stereo 16 bit samples at 44100 Hz
   *   the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
   *   
   *   The same happens using Audio1 for captureing.
   *
   *   The Windows driver does not suffer of this (although it use Audio1
   *   only for captureing). I'm unable to discover why.
   *
   */
95b712965   Mark Salazar   [ALSA] #4/4 for Z...
69
70
71
72
73
74
75
76
77
78
  /*
   * ES1879 NOTES:
   * - When Zoom Video is enabled (reg 0x71 bit 6 toggled on) the PCM playback
   *   seems to be effected (speaker_test plays a lower frequency). Can't find
   *   anything in the datasheet to account for this, so a Video Playback Switch
   *   control has been included to allow ZV to be enabled only when necessary.
   *   Then again on at least one test system the 0x71 bit 6 enable bit is not 
   *   needed for ZV, so maybe the datasheet is entirely wrong here.
   */
   
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
79
  #include <linux/init.h>
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
80
  #include <linux/err.h>
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
81
  #include <linux/isa.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
  #include <linux/pnp.h>
  #include <linux/isapnp.h>
65a772172   Paul Gortmaker   sound: fix driver...
84
  #include <linux/module.h>
1caef6aa9   Andrew Morton   [PATCH] es18xx bu...
85
  #include <linux/delay.h>
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
86
87
  #include <asm/io.h>
  #include <asm/dma.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
89
90
91
92
93
  #include <sound/core.h>
  #include <sound/control.h>
  #include <sound/pcm.h>
  #include <sound/pcm_params.h>
  #include <sound/mpu401.h>
  #include <sound/opl3.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
95
96
97
98
  #define SNDRV_LEGACY_FIND_FREE_IRQ
  #define SNDRV_LEGACY_FIND_FREE_DMA
  #include <sound/initval.h>
  
  #define PFX "es18xx: "
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
99
  struct snd_es18xx {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  	unsigned long port;		/* port of ESS chip */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
103
104
105
106
107
108
109
110
111
112
  	unsigned long ctrl_port;	/* Control port of ESS chip */
  	struct resource *res_port;
  	struct resource *res_mpu_port;
  	struct resource *res_ctrl_port;
  	int irq;			/* IRQ number of ESS chip */
  	int dma1;			/* DMA1 */
  	int dma2;			/* DMA2 */
  	unsigned short version;		/* version of ESS chip */
  	int caps;			/* Chip capabilities */
  	unsigned short audio2_vol;	/* volume level of audio2 */
  
  	unsigned short active;		/* active channel mask */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
  	unsigned int dma1_shift;
  	unsigned int dma2_shift;
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
115
116
117
118
  	struct snd_pcm *pcm;
  	struct snd_pcm_substream *playback_a_substream;
  	struct snd_pcm_substream *capture_a_substream;
  	struct snd_pcm_substream *playback_b_substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119

8047c910f   Takashi Iwai   [ALSA] Remove xxx...
120
  	struct snd_rawmidi *rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
121

8047c910f   Takashi Iwai   [ALSA] Remove xxx...
122
123
124
125
  	struct snd_kcontrol *hw_volume;
  	struct snd_kcontrol *hw_switch;
  	struct snd_kcontrol *master_volume;
  	struct snd_kcontrol *master_switch;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
  
  	spinlock_t reg_lock;
  	spinlock_t mixer_lock;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
130
131
  #ifdef CONFIG_PM
  	unsigned char pm_reg;
  #endif
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
132
133
134
135
136
  #ifdef CONFIG_PNP
  	struct pnp_dev *dev;
  	struct pnp_dev *devc;
  #endif
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
139
140
141
142
143
144
145
146
147
148
  #define AUDIO1_IRQ	0x01
  #define AUDIO2_IRQ	0x02
  #define HWV_IRQ		0x04
  #define MPU_IRQ		0x08
  
  #define ES18XX_PCM2	0x0001	/* Has two useable PCM */
  #define ES18XX_SPATIALIZER 0x0002	/* Has 3D Spatializer */
  #define ES18XX_RECMIX	0x0004	/* Has record mixer */
  #define ES18XX_DUPLEX_MONO 0x0008	/* Has mono duplex only */
  #define ES18XX_DUPLEX_SAME 0x0010	/* Playback and record must share the same rate */
  #define ES18XX_NEW_RATE	0x0020	/* More precise rate setting */
  #define ES18XX_AUXB	0x0040	/* AuxB mixer control */
561de31a2   Joe Perches   [ALSA] sound/: Sp...
149
  #define ES18XX_HWV	0x0080	/* Has separate hardware volume mixer controls*/
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
150
151
152
153
  #define ES18XX_MONO	0x0100	/* Mono_in mixer control */
  #define ES18XX_I2S	0x0200	/* I2S mixer control */
  #define ES18XX_MUTEREC	0x0400	/* Record source can be muted */
  #define ES18XX_CONTROL	0x0800	/* Has control ports */
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
154
  #define ES18XX_GPO_2BIT	0x1000	/* GPO0,1 controlled by PM port */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
156
157
158
159
160
161
162
163
  
  /* Power Management */
  #define ES18XX_PM	0x07
  #define ES18XX_PM_GPO0	0x01
  #define ES18XX_PM_GPO1	0x02
  #define ES18XX_PM_PDR	0x04
  #define ES18XX_PM_ANA	0x08
  #define ES18XX_PM_FM	0x020
  #define ES18XX_PM_SUS	0x080
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
164
165
166
167
168
169
  /* Lowlevel */
  
  #define DAC1 0x01
  #define ADC1 0x02
  #define DAC2 0x04
  #define MILLISECOND 10000
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
170
  static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
171
172
173
174
175
176
177
178
  {
          int i;
  
          for(i = MILLISECOND; i; i--)
                  if ((inb(chip->port + 0x0C) & 0x80) == 0) {
                          outb(val, chip->port + 0x0C);
                          return 0;
                  }
99b359ba1   Takashi Iwai   [ALSA] Add missin...
179
180
  	snd_printk(KERN_ERR "dsp_command: timeout (0x%x)
  ", val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
182
          return -EINVAL;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
183
  static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
185
186
187
188
189
  {
          int i;
  
          for(i = MILLISECOND/10; i; i--)
                  if (inb(chip->port + 0x0C) & 0x40)
                          return inb(chip->port + 0x0A);
99b359ba1   Takashi Iwai   [ALSA] Add missin...
190
191
192
  	snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!
  ",
  		   chip->port + 0x0A, inb(chip->port + 0x0A));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
193
194
195
196
          return -ENODEV;
  }
  
  #undef REG_DEBUG
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
197
  static int snd_es18xx_write(struct snd_es18xx *chip,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
198
199
200
201
202
203
204
205
206
207
208
209
210
  			    unsigned char reg, unsigned char data)
  {
  	unsigned long flags;
  	int ret;
  	
          spin_lock_irqsave(&chip->reg_lock, flags);
  	ret = snd_es18xx_dsp_command(chip, reg);
  	if (ret < 0)
  		goto end;
          ret = snd_es18xx_dsp_command(chip, data);
   end:
          spin_unlock_irqrestore(&chip->reg_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
211
212
  	snd_printk(KERN_DEBUG "Reg %02x set to %02x
  ", reg, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
215
  #endif
  	return ret;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
216
  static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
217
218
219
220
221
222
223
224
225
226
227
228
229
  {
  	unsigned long flags;
  	int ret, data;
          spin_lock_irqsave(&chip->reg_lock, flags);
  	ret = snd_es18xx_dsp_command(chip, 0xC0);
  	if (ret < 0)
  		goto end;
          ret = snd_es18xx_dsp_command(chip, reg);
  	if (ret < 0)
  		goto end;
  	data = snd_es18xx_dsp_get_byte(chip);
  	ret = data;
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
230
231
  	snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)
  ", reg, data, ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
232
233
234
235
236
237
238
  #endif
   end:
          spin_unlock_irqrestore(&chip->reg_lock, flags);
  	return ret;
  }
  
  /* Return old value */
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
239
  static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
  			   unsigned char mask, unsigned char val)
  {
          int ret;
  	unsigned char old, new, oval;
  	unsigned long flags;
          spin_lock_irqsave(&chip->reg_lock, flags);
          ret = snd_es18xx_dsp_command(chip, 0xC0);
  	if (ret < 0)
  		goto end;
          ret = snd_es18xx_dsp_command(chip, reg);
  	if (ret < 0)
  		goto end;
  	ret = snd_es18xx_dsp_get_byte(chip);
  	if (ret < 0) {
  		goto end;
  	}
  	old = ret;
  	oval = old & mask;
  	if (val != oval) {
  		ret = snd_es18xx_dsp_command(chip, reg);
  		if (ret < 0)
  			goto end;
  		new = (old & ~mask) | (val & mask);
  		ret = snd_es18xx_dsp_command(chip, new);
  		if (ret < 0)
  			goto end;
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
267
268
269
  		snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)
  ",
  			   reg, old, new, ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
270
271
272
273
274
275
276
  #endif
  	}
  	ret = oval;
   end:
          spin_unlock_irqrestore(&chip->reg_lock, flags);
  	return ret;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
277
  static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
278
279
280
281
282
283
284
285
  			    unsigned char reg, unsigned char data)
  {
  	unsigned long flags;
          spin_lock_irqsave(&chip->mixer_lock, flags);
          outb(reg, chip->port + 0x04);
          outb(data, chip->port + 0x05);
          spin_unlock_irqrestore(&chip->mixer_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
286
287
  	snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x
  ", reg, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
  #endif
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
290
  static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
291
292
293
294
295
296
297
298
  {
  	unsigned long flags;
  	int data;
          spin_lock_irqsave(&chip->mixer_lock, flags);
          outb(reg, chip->port + 0x04);
  	data = inb(chip->port + 0x05);
          spin_unlock_irqrestore(&chip->mixer_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
299
300
  	snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x
  ", reg, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
303
304
305
  #endif
          return data;
  }
  
  /* Return old value */
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
306
  static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
313
314
315
316
317
318
  					unsigned char mask, unsigned char val)
  {
  	unsigned char old, new, oval;
  	unsigned long flags;
          spin_lock_irqsave(&chip->mixer_lock, flags);
          outb(reg, chip->port + 0x04);
  	old = inb(chip->port + 0x05);
  	oval = old & mask;
  	if (val != oval) {
  		new = (old & ~mask) | (val & mask);
  		outb(new, chip->port + 0x05);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
319
320
321
  		snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x
  ",
  			   reg, old, new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
322
323
324
325
326
  #endif
  	}
          spin_unlock_irqrestore(&chip->mixer_lock, flags);
  	return oval;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
327
  static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
333
334
335
336
337
338
339
  					    unsigned char mask)
  {
  	int old, expected, new;
  	unsigned long flags;
          spin_lock_irqsave(&chip->mixer_lock, flags);
          outb(reg, chip->port + 0x04);
  	old = inb(chip->port + 0x05);
  	expected = old ^ mask;
  	outb(expected, chip->port + 0x05);
  	new = inb(chip->port + 0x05);
          spin_unlock_irqrestore(&chip->mixer_lock, flags);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
340
341
342
  	snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x
  ",
  		   reg, old, expected, new);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
  #endif
  	return expected == new;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
346
  static int snd_es18xx_reset(struct snd_es18xx *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
347
348
349
350
351
352
353
354
355
356
  {
  	int i;
          outb(0x03, chip->port + 0x06);
          inb(chip->port + 0x06);
          outb(0x00, chip->port + 0x06);
          for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
          if (inb(chip->port + 0x0A) != 0xAA)
                  return -1;
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
357
  static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
361
362
363
  {
          outb(0x02, chip->port + 0x06);
          inb(chip->port + 0x06);
          outb(0x00, chip->port + 0x06);
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
364
  static struct snd_ratnum new_clocks[2] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
370
371
372
373
374
375
376
377
  	{
  		.num = 793800,
  		.den_min = 1,
  		.den_max = 128,
  		.den_step = 1,
  	},
  	{
  		.num = 768000,
  		.den_min = 1,
  		.den_max = 128,
  		.den_step = 1,
  	}
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
378
  static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
  	.nrats = 2,
  	.rats = new_clocks,
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
382
  static struct snd_ratnum old_clocks[2] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
383
384
385
386
387
388
389
390
391
392
393
394
395
  	{
  		.num = 795444,
  		.den_min = 1,
  		.den_max = 128,
  		.den_step = 1,
  	},
  	{
  		.num = 397722,
  		.den_min = 1,
  		.den_max = 128,
  		.den_step = 1,
  	}
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
396
  static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks  = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
397
398
399
  	.nrats = 2,
  	.rats = old_clocks,
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
400
401
  static void snd_es18xx_rate_set(struct snd_es18xx *chip, 
  				struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
402
403
404
  				int mode)
  {
  	unsigned int bits, div0;
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
405
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
  	if (chip->caps & ES18XX_NEW_RATE) {
  		if (runtime->rate_num == new_clocks[0].num)
  			bits = 128 - runtime->rate_den;
  		else
  			bits = 256 - runtime->rate_den;
  	} else {
  		if (runtime->rate_num == old_clocks[0].num)
  			bits = 256 - runtime->rate_den;
  		else
  			bits = 128 - runtime->rate_den;
  	}
  
  	/* set filter register */
  	div0 = 256 - 7160000*20/(8*82*runtime->rate);
  		
  	if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
  		snd_es18xx_mixer_write(chip, 0x70, bits);
  		/*
  		 * Comment from kernel oss driver:
  		 * FKS: fascinating: 0x72 doesn't seem to work.
  		 */
  		snd_es18xx_write(chip, 0xA2, div0);
  		snd_es18xx_mixer_write(chip, 0x72, div0);
  	} else {
  		snd_es18xx_write(chip, 0xA1, bits);
  		snd_es18xx_write(chip, 0xA2, div0);
  	}
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
434
435
  static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
  					 struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
436
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
437
  	struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
  	int shift, err;
  
  	shift = 0;
  	if (params_channels(hw_params) == 2)
  		shift++;
  	if (snd_pcm_format_width(params_format(hw_params)) == 16)
  		shift++;
  
  	if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
  		if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  		    (chip->capture_a_substream) &&
  		    params_channels(hw_params) != 1) {
  			_snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
  			return -EBUSY;
  		}
  		chip->dma2_shift = shift;
  	} else {
  		chip->dma1_shift = shift;
  	}
  	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  		return err;
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
461
  static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
462
463
464
  {
  	return snd_pcm_lib_free_pages(substream);
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
465
466
  static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
  					struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
468
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
470
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
          snd_es18xx_rate_set(chip, substream, DAC2);
  
          /* Transfer Count Reload */
          count = 0x10000 - count;
          snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
          snd_es18xx_mixer_write(chip, 0x76, count >> 8);
  
  	/* Set format */
          snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
  			      ((runtime->channels == 1) ? 0x00 : 0x02) |
  			      (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
  			      (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
  
          /* Set DMA controller */
          snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
489
490
  static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
  					struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
  					int cmd)
  {
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  	case SNDRV_PCM_TRIGGER_RESUME:
  		if (chip->active & DAC2)
  			return 0;
  		chip->active |= DAC2;
                  /* Start DMA */
  		if (chip->dma2 >= 4)
  			snd_es18xx_mixer_write(chip, 0x78, 0xb3);
  		else
  			snd_es18xx_mixer_write(chip, 0x78, 0x93);
  #ifdef AVOID_POPS
  		/* Avoid pops */
5e0c18799   Li, Zhen-Hua   ALSA: es18xx driv...
506
  		mdelay(100);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  		if (chip->caps & ES18XX_PCM2)
  			/* Restore Audio 2 volume */
  			snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
  		else
  			/* Enable PCM output */
  			snd_es18xx_dsp_command(chip, 0xD1);
  #endif
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  	case SNDRV_PCM_TRIGGER_SUSPEND:
  		if (!(chip->active & DAC2))
  			return 0;
  		chip->active &= ~DAC2;
                  /* Stop DMA */
                  snd_es18xx_mixer_write(chip, 0x78, 0x00);
  #ifdef AVOID_POPS
5e0c18799   Li, Zhen-Hua   ALSA: es18xx driv...
523
  		mdelay(25);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  		if (chip->caps & ES18XX_PCM2)
  			/* Set Audio 2 volume to 0 */
  			snd_es18xx_mixer_write(chip, 0x7C, 0);
  		else
  			/* Disable PCM output */
  			snd_es18xx_dsp_command(chip, 0xD3);
  #endif
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
538
539
  static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
  					struct snd_pcm_hw_params *hw_params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
540
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
541
  	struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
  	int shift, err;
  
  	shift = 0;
  	if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  	    chip->playback_a_substream &&
  	    params_channels(hw_params) != 1) {
  		_snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
  		return -EBUSY;
  	}
  	if (params_channels(hw_params) == 2)
  		shift++;
  	if (snd_pcm_format_width(params_format(hw_params)) == 16)
  		shift++;
  	chip->dma1_shift = shift;
  	if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
  		return err;
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
560
  static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
561
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
562
563
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
564
565
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
567
568
569
570
571
572
573
574
575
576
577
578
  	snd_es18xx_reset_fifo(chip);
  
          /* Set stereo/mono */
          snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
  
          snd_es18xx_rate_set(chip, substream, ADC1);
  
          /* Transfer Count Reload */
  	count = 0x10000 - count;
  	snd_es18xx_write(chip, 0xA4, count & 0xff);
  	snd_es18xx_write(chip, 0xA5, count >> 8);
  
  #ifdef AVOID_POPS
5e0c18799   Li, Zhen-Hua   ALSA: es18xx driv...
579
  	mdelay(100);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
580
581
582
583
584
585
586
587
588
  #endif
  
          /* Set format */
          snd_es18xx_write(chip, 0xB7, 
                           snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
          snd_es18xx_write(chip, 0xB7, 0x90 |
                           ((runtime->channels == 1) ? 0x40 : 0x08) |
                           (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
                           (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
3a4fa0a25   Robert P. J. Day   Fix misspellings ...
589
          /* Set DMA controller */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
590
591
592
593
          snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
594
  static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
595
596
  				      int cmd)
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
597
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
  
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  	case SNDRV_PCM_TRIGGER_RESUME:
  		if (chip->active & ADC1)
  			return 0;
  		chip->active |= ADC1;
                  /* Start DMA */
                  snd_es18xx_write(chip, 0xB8, 0x0f);
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  	case SNDRV_PCM_TRIGGER_SUSPEND:
  		if (!(chip->active & ADC1))
  			return 0;
  		chip->active &= ~ADC1;
                  /* Stop DMA */
                  snd_es18xx_write(chip, 0xB8, 0x00);
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
622
623
  static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
  					struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
624
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
625
  	struct snd_pcm_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
626
627
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
  	unsigned int count = snd_pcm_lib_period_bytes(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
  	snd_es18xx_reset_fifo(chip);
  
          /* Set stereo/mono */
          snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
  
          snd_es18xx_rate_set(chip, substream, DAC1);
  
          /* Transfer Count Reload */
  	count = 0x10000 - count;
  	snd_es18xx_write(chip, 0xA4, count & 0xff);
  	snd_es18xx_write(chip, 0xA5, count >> 8);
  
          /* Set format */
          snd_es18xx_write(chip, 0xB6,
                           snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
          snd_es18xx_write(chip, 0xB7, 
                           snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
          snd_es18xx_write(chip, 0xB7, 0x90 |
                           (runtime->channels == 1 ? 0x40 : 0x08) |
                           (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
                           (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
3a4fa0a25   Robert P. J. Day   Fix misspellings ...
649
          /* Set DMA controller */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
650
651
652
653
          snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
654
655
  static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
  					struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
657
658
659
660
661
662
663
664
665
666
667
  					int cmd)
  {
  	switch (cmd) {
  	case SNDRV_PCM_TRIGGER_START:
  	case SNDRV_PCM_TRIGGER_RESUME:
  		if (chip->active & DAC1)
  			return 0;
  		chip->active |= DAC1;
                  /* Start DMA */
                  snd_es18xx_write(chip, 0xB8, 0x05);
  #ifdef AVOID_POPS
  		/* Avoid pops */
5e0c18799   Li, Zhen-Hua   ALSA: es18xx driv...
668
  		mdelay(100);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
669
670
671
672
673
674
675
676
677
678
679
680
681
                  /* Enable Audio 1 */
                  snd_es18xx_dsp_command(chip, 0xD1);
  #endif
  		break;
  	case SNDRV_PCM_TRIGGER_STOP:
  	case SNDRV_PCM_TRIGGER_SUSPEND:
  		if (!(chip->active & DAC1))
  			return 0;
  		chip->active &= ~DAC1;
                  /* Stop DMA */
                  snd_es18xx_write(chip, 0xB8, 0x00);
  #ifdef AVOID_POPS
  		/* Avoid pops */
5e0c18799   Li, Zhen-Hua   ALSA: es18xx driv...
682
  		mdelay(25);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
684
685
686
687
688
689
690
691
692
                  /* Disable Audio 1 */
                  snd_es18xx_dsp_command(chip, 0xD3);
  #endif
  		break;
  	default:
  		return -EINVAL;
  	}
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
693
  static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
695
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696
697
698
699
700
  	if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
  		return snd_es18xx_playback1_prepare(chip, substream);
  	else
  		return snd_es18xx_playback2_prepare(chip, substream);
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
701
  static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
702
703
  				       int cmd)
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
704
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
705
706
707
708
709
  	if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
  		return snd_es18xx_playback1_trigger(chip, substream, cmd);
  	else
  		return snd_es18xx_playback2_trigger(chip, substream, cmd);
  }
7d12e780e   David Howells   IRQ: Maintain reg...
710
  static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
711
  {
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
712
  	struct snd_card *card = dev_id;
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
713
  	struct snd_es18xx *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
  	unsigned char status;
  
  	if (chip->caps & ES18XX_CONTROL) {
  		/* Read Interrupt status */
  		status = inb(chip->ctrl_port + 6);
  	} else {
  		/* Read Interrupt status */
  		status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
  	}
  #if 0
  	else {
  		status = 0;
  		if (inb(chip->port + 0x0C) & 0x01)
  			status |= AUDIO1_IRQ;
  		if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
  			status |= AUDIO2_IRQ;
  		if ((chip->caps & ES18XX_HWV) &&
  		    snd_es18xx_mixer_read(chip, 0x64) & 0x10)
  			status |= HWV_IRQ;
  	}
  #endif
  
  	/* Audio 1 & Audio 2 */
          if (status & AUDIO2_IRQ) {
                  if (chip->active & DAC2)
                  	snd_pcm_period_elapsed(chip->playback_a_substream);
  		/* ack interrupt */
                  snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
          }
          if (status & AUDIO1_IRQ) {
                  /* ok.. capture is active */
                  if (chip->active & ADC1)
                  	snd_pcm_period_elapsed(chip->capture_a_substream);
                  /* ok.. playback2 is active */
                  else if (chip->active & DAC1)
                  	snd_pcm_period_elapsed(chip->playback_b_substream);
  		/* ack interrupt */
  		inb(chip->port + 0x0E);
          }
  
  	/* MPU */
  	if ((status & MPU_IRQ) && chip->rmidi)
7d12e780e   David Howells   IRQ: Maintain reg...
756
  		snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
757
758
759
  
  	/* Hardware volume */
  	if (status & HWV_IRQ) {
14086771c   Mark Salazar   [ALSA] #3/4 for Z...
760
761
762
  		int split = 0;
  		if (chip->caps & ES18XX_HWV) {
  			split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
763
764
765
766
  			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  					&chip->hw_switch->id);
  			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  					&chip->hw_volume->id);
14086771c   Mark Salazar   [ALSA] #3/4 for Z...
767
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
  		if (!split) {
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
769
770
771
772
  			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  					&chip->master_switch->id);
  			snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_VALUE,
  					&chip->master_volume->id);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
773
774
775
776
777
778
  		}
  		/* ack interrupt */
  		snd_es18xx_mixer_write(chip, 0x66, 0x00);
  	}
  	return IRQ_HANDLED;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
779
  static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
781
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
782
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
784
785
786
787
  	int pos;
  
  	if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
  		if (!(chip->active & DAC2))
  			return 0;
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
788
  		pos = snd_dma_pointer(chip->dma2, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
789
790
791
792
  		return pos >> chip->dma2_shift;
  	} else {
  		if (!(chip->active & DAC1))
  			return 0;
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
793
  		pos = snd_dma_pointer(chip->dma1, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
795
796
  		return pos >> chip->dma1_shift;
  	}
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
797
  static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
799
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
800
  	unsigned int size = snd_pcm_lib_buffer_bytes(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
801
802
803
804
  	int pos;
  
          if (!(chip->active & ADC1))
                  return 0;
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
805
  	pos = snd_dma_pointer(chip->dma1, size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
806
807
  	return pos >> chip->dma1_shift;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
808
  static struct snd_pcm_hardware snd_es18xx_playback =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_RESUME |
  				 SNDRV_PCM_INFO_MMAP_VALID),
  	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | 
  				 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		4000,
  	.rate_max =		48000,
  	.channels_min =		1,
  	.channels_max =		2,
  	.buffer_bytes_max =	65536,
  	.period_bytes_min =	64,
  	.period_bytes_max =	65536,
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
827
  static struct snd_pcm_hardware snd_es18xx_capture =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
  {
  	.info =			(SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
  				 SNDRV_PCM_INFO_RESUME |
  				 SNDRV_PCM_INFO_MMAP_VALID),
  	.formats =		(SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 | 
  				 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
  	.rates =		SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
  	.rate_min =		4000,
  	.rate_max =		48000,
  	.channels_min =		1,
  	.channels_max =		2,
  	.buffer_bytes_max =	65536,
  	.period_bytes_min =	64,
  	.period_bytes_max =	65536,
  	.periods_min =		1,
  	.periods_max =		1024,
  	.fifo_size =		0,
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
846
  static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
847
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
848
849
  	struct snd_pcm_runtime *runtime = substream->runtime;
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
  
  	if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
  		if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  		    chip->capture_a_substream && 
  		    chip->capture_a_substream->runtime->channels != 1)
  			return -EAGAIN;
  		chip->playback_a_substream = substream;
  	} else if (substream->number <= 1) {
  		if (chip->capture_a_substream)
  			return -EAGAIN;
  		chip->playback_b_substream = substream;
  	} else {
  		snd_BUG();
  		return -EINVAL;
  	}
  	substream->runtime->hw = snd_es18xx_playback;
  	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  				      (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
          return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
870
  static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
871
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
872
873
  	struct snd_pcm_runtime *runtime = substream->runtime;
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
874
875
876
877
878
879
880
881
882
883
884
885
886
  
          if (chip->playback_b_substream)
                  return -EAGAIN;
  	if ((chip->caps & ES18XX_DUPLEX_MONO) &&
  	    chip->playback_a_substream &&
  	    chip->playback_a_substream->runtime->channels != 1)
  		return -EAGAIN;
          chip->capture_a_substream = substream;
  	substream->runtime->hw = snd_es18xx_capture;
  	snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
  				      (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
          return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
887
  static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
888
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
889
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
890
891
892
893
894
895
896
897
898
  
  	if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
  		chip->playback_a_substream = NULL;
  	else
  		chip->playback_b_substream = NULL;
  	
  	snd_pcm_lib_free_pages(substream);
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
899
  static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
900
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
901
          struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
902
903
904
905
906
907
908
909
910
  
          chip->capture_a_substream = NULL;
  	snd_pcm_lib_free_pages(substream);
          return 0;
  }
  
  /*
   *  MIXER part
   */
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
  /* Record source mux routines:
   * Depending on the chipset this mux switches between 4, 5, or 8 possible inputs.
   * bit table for the 4/5 source mux:
   * reg 1C:
   *  b2 b1 b0   muxSource
   *   x  0  x   microphone
   *   0  1  x   CD
   *   1  1  0   line
   *   1  1  1   mixer
   * if it's "mixer" and it's a 5 source mux chipset then reg 7A bit 3 determines
   * either the play mixer or the capture mixer.
   *
   * "map4Source" translates from source number to reg bit pattern
   * "invMap4Source" translates from reg bit pattern to source number
   */
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
926
  static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
927
  {
4e28350a8   Takashi Iwai   ALSA: es18xx: Use...
928
  	static const char * const texts5Source[5] = {
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
929
930
  		"Mic", "CD", "Line", "Master", "Mix"
  	};
4e28350a8   Takashi Iwai   ALSA: es18xx: Use...
931
  	static const char * const texts8Source[8] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
932
933
934
  		"Mic", "Mic Master", "CD", "AOUT",
  		"Mic1", "Mix", "Line", "Master"
  	};
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
935
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
936

f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
937
938
939
  	switch (chip->version) {
  	case 0x1868:
  	case 0x1878:
4e28350a8   Takashi Iwai   ALSA: es18xx: Use...
940
  		return snd_ctl_enum_info(uinfo, 1, 4, texts5Source);
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
941
942
  	case 0x1887:
  	case 0x1888:
4e28350a8   Takashi Iwai   ALSA: es18xx: Use...
943
  		return snd_ctl_enum_info(uinfo, 1, 5, texts5Source);
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
944
945
  	case 0x1869: /* DS somewhat contradictory for 1869: could be be 5 or 8 */
  	case 0x1879:
4e28350a8   Takashi Iwai   ALSA: es18xx: Use...
946
  		return snd_ctl_enum_info(uinfo, 1, 8, texts8Source);
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
947
948
949
  	default:
  		return -EINVAL;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
950
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
951
  static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
952
  {
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
953
  	static unsigned char invMap4Source[8] = {0, 0, 1, 1, 0, 0, 2, 3};
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
954
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
955
956
957
958
959
960
961
962
963
964
  	int muxSource = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
  	if (!(chip->version == 0x1869 || chip->version == 0x1879)) {
  		muxSource = invMap4Source[muxSource];
  		if (muxSource==3 && 
  		    (chip->version == 0x1887 || chip->version == 0x1888) &&
  		    (snd_es18xx_mixer_read(chip, 0x7a) & 0x08)
  		) 
  			muxSource = 4;
  	}
  	ucontrol->value.enumerated.item[0] = muxSource;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
965
966
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
967
  static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
968
  {
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
969
  	static unsigned char map4Source[4] = {0, 2, 6, 7};
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
970
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
971
  	unsigned char val = ucontrol->value.enumerated.item[0];
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
  	unsigned char retVal = 0;
  
  	switch (chip->version) {
   /* 5 source chips */
  	case 0x1887:
  	case 0x1888:
  		if (val > 4)
  			return -EINVAL;
  		if (val == 4) {
  			retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x08) != 0x08;
  			val = 3;
  		} else
  			retVal = snd_es18xx_mixer_bits(chip, 0x7a, 0x08, 0x00) != 0x00;
   /* 4 source chips */
  	case 0x1868:
  	case 0x1878:
  		if (val > 3)
  			return -EINVAL;
  		val = map4Source[val];
  		break;
   /* 8 source chips */
  	case 0x1869:
  	case 0x1879:
  		if (val > 7)
  			return -EINVAL;
  		break;
  	default:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
999
  		return -EINVAL;
f4df221f8   Mark Salazar   [ALSA] #2/4 for Z...
1000
1001
  	}
  	return (snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val) || retVal;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1002
  }
a5ce88909   Takashi Iwai   [ALSA] Clean up w...
1003
  #define snd_es18xx_info_spatializer_enable	snd_ctl_boolean_mono_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1004

8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1005
  static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1006
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1007
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1008
1009
1010
1011
  	unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
  	ucontrol->value.integer.value[0] = !!(val & 8);
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1012
  static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1013
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1014
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  	unsigned char oval, nval;
  	int change;
  	nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
  	oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
  	change = nval != oval;
  	if (change) {
  		snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
  		snd_es18xx_mixer_write(chip, 0x50, nval);
  	}
  	return change;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1026
  static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1027
1028
1029
1030
1031
1032
1033
  {
  	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 2;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = 63;
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1034
  static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1035
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1036
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1037
1038
1039
1040
  	ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
  	ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
  	return 0;
  }
a5ce88909   Takashi Iwai   [ALSA] Clean up w...
1041
  #define snd_es18xx_info_hw_switch	snd_ctl_boolean_stereo_info
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1042

8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1043
  static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1044
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1045
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1046
1047
1048
1049
  	ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
  	ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1050
  static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1051
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1052
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1053
1054
1055
1056
1057
  	chip->master_volume = NULL;
  	chip->master_switch = NULL;
  	chip->hw_volume = NULL;
  	chip->hw_switch = NULL;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1058
  static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1059
1060
1061
1062
1063
1064
1065
  			       unsigned char mask, unsigned char val)
  {
  	if (reg < 0xa0)
  		return snd_es18xx_mixer_bits(chip, reg, mask, val);
  	else
  		return snd_es18xx_bits(chip, reg, mask, val);
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1066
  static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1067
1068
1069
1070
1071
1072
  {
  	if (reg < 0xa0)
  		return snd_es18xx_mixer_read(chip, reg);
  	else
  		return snd_es18xx_read(chip, reg);
  }
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1073
  #define ES18XX_SINGLE(xname, xindex, reg, shift, mask, flags) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1074
1075
1076
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
    .info = snd_es18xx_info_single, \
    .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1077
1078
1079
1080
    .private_value = reg | (shift << 8) | (mask << 16) | (flags << 24) }
  
  #define ES18XX_FL_INVERT	(1 << 0)
  #define ES18XX_FL_PMPORT	(1 << 1)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1081

8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1082
  static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1083
1084
1085
1086
1087
1088
1089
1090
1091
  {
  	int mask = (kcontrol->private_value >> 16) & 0xff;
  
  	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 1;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = mask;
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1092
  static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1093
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1094
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1095
1096
1097
  	int reg = kcontrol->private_value & 0xff;
  	int shift = (kcontrol->private_value >> 8) & 0xff;
  	int mask = (kcontrol->private_value >> 16) & 0xff;
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1098
1099
  	int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
  	int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1100
  	int val;
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1101
1102
1103
1104
1105
  
  	if (pm_port)
  		val = inb(chip->port + ES18XX_PM);
  	else
  		val = snd_es18xx_reg_read(chip, reg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1106
1107
1108
1109
1110
  	ucontrol->value.integer.value[0] = (val >> shift) & mask;
  	if (invert)
  		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1111
  static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1112
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1113
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1114
1115
1116
  	int reg = kcontrol->private_value & 0xff;
  	int shift = (kcontrol->private_value >> 8) & 0xff;
  	int mask = (kcontrol->private_value >> 16) & 0xff;
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1117
1118
  	int invert = (kcontrol->private_value >> 24) & ES18XX_FL_INVERT;
  	int pm_port = (kcontrol->private_value >> 24) & ES18XX_FL_PMPORT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1119
1120
1121
1122
1123
1124
1125
  	unsigned char val;
  	
  	val = (ucontrol->value.integer.value[0] & mask);
  	if (invert)
  		val = mask - val;
  	mask <<= shift;
  	val <<= shift;
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1126
1127
1128
1129
1130
1131
1132
1133
  	if (pm_port) {
  		unsigned char cur = inb(chip->port + ES18XX_PM);
  
  		if ((cur & mask) == val)
  			return 0;
  		outb((cur & ~mask) | val, chip->port + ES18XX_PM);
  		return 1;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1134
1135
1136
1137
1138
1139
1140
1141
  	return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
  }
  
  #define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
  { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
    .info = snd_es18xx_info_double, \
    .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
    .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1142
  static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1143
1144
1145
1146
1147
1148
1149
1150
1151
  {
  	int mask = (kcontrol->private_value >> 24) & 0xff;
  
  	uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
  	uinfo->count = 2;
  	uinfo->value.integer.min = 0;
  	uinfo->value.integer.max = mask;
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1152
  static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1153
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1154
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
  	int left_reg = kcontrol->private_value & 0xff;
  	int right_reg = (kcontrol->private_value >> 8) & 0xff;
  	int shift_left = (kcontrol->private_value >> 16) & 0x07;
  	int shift_right = (kcontrol->private_value >> 19) & 0x07;
  	int mask = (kcontrol->private_value >> 24) & 0xff;
  	int invert = (kcontrol->private_value >> 22) & 1;
  	unsigned char left, right;
  	
  	left = snd_es18xx_reg_read(chip, left_reg);
  	if (left_reg != right_reg)
  		right = snd_es18xx_reg_read(chip, right_reg);
  	else
  		right = left;
  	ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
  	ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
  	if (invert) {
  		ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
  		ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
  	}
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1176
  static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1177
  {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1178
  	struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
  	int left_reg = kcontrol->private_value & 0xff;
  	int right_reg = (kcontrol->private_value >> 8) & 0xff;
  	int shift_left = (kcontrol->private_value >> 16) & 0x07;
  	int shift_right = (kcontrol->private_value >> 19) & 0x07;
  	int mask = (kcontrol->private_value >> 24) & 0xff;
  	int invert = (kcontrol->private_value >> 22) & 1;
  	int change;
  	unsigned char val1, val2, mask1, mask2;
  	
  	val1 = ucontrol->value.integer.value[0] & mask;
  	val2 = ucontrol->value.integer.value[1] & mask;
  	if (invert) {
  		val1 = mask - val1;
  		val2 = mask - val2;
  	}
  	val1 <<= shift_left;
  	val2 <<= shift_right;
  	mask1 = mask << shift_left;
  	mask2 = mask << shift_right;
  	if (left_reg != right_reg) {
  		change = 0;
  		if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
  			change = 1;
  		if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
  			change = 1;
  	} else {
  		change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2, 
  					      val1 | val2) != (val1 | val2));
  	}
  	return change;
  }
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1210
1211
1212
1213
1214
1215
  /* Mixer controls
   * These arrays contain setup data for mixer controls.
   * 
   * The controls that are universal to all chipsets are fully initialized
   * here.
   */
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1216
  static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1217
1218
1219
1220
1221
  ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
  ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
  ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
  ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
  ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1222
1223
  ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
  ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1224
1225
  ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
  ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1226
1227
1228
1229
1230
1231
1232
1233
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "Capture Source",
  	.info = snd_es18xx_info_mux,
  	.get = snd_es18xx_get_mux,
  	.put = snd_es18xx_put_mux,
  }
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1234
  static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1235
1236
1237
1238
  ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
  ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
  ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
  ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1239
1240
1241
  ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
  ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
  };
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1242
1243
1244
1245
  /*
   * The chipset specific mixer controls
   */
  static struct snd_kcontrol_new snd_es18xx_opt_speaker =
d355c82a0   Jaroslav Kysela   ALSA: rename "PC ...
1246
  	ES18XX_SINGLE("Beep Playback Volume", 0, 0x3c, 0, 7, 0);
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1247
1248
  
  static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1249
  ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, ES18XX_FL_INVERT),
95b712965   Mark Salazar   [ALSA] #4/4 for Z...
1250
  ES18XX_SINGLE("Video Playback Switch", 0, 0x7f, 0, 1, 0),
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1251
1252
1253
  ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
  };
95b712965   Mark Salazar   [ALSA] #4/4 for Z...
1254
1255
1256
1257
1258
1259
1260
1261
  static struct snd_kcontrol_new snd_es18xx_opt_1878 =
  	ES18XX_DOUBLE("Video Playback Volume", 0, 0x68, 0x68, 4, 0, 15, 0);
  
  static struct snd_kcontrol_new snd_es18xx_opt_1879[] = {
  ES18XX_SINGLE("Video Playback Switch", 0, 0x71, 6, 1, 0),
  ES18XX_DOUBLE("Video Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
  ES18XX_DOUBLE("Video Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1262
  static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1263
1264
  ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1265
  static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1266
1267
1268
  ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
  ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1269
  static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1270
1271
1272
1273
1274
1275
1276
1277
1278
  ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "3D Control - Switch",
  	.info = snd_es18xx_info_spatializer_enable,
  	.get = snd_es18xx_get_spatializer_enable,
  	.put = snd_es18xx_put_spatializer_enable,
  }
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1279
  static struct snd_kcontrol_new snd_es18xx_micpre1_control = 
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1280
  ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1281
  static struct snd_kcontrol_new snd_es18xx_micpre2_control =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1282
  ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1283
  static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "Hardware Master Playback Volume",
  	.access = SNDRV_CTL_ELEM_ACCESS_READ,
  	.info = snd_es18xx_info_hw_volume,
  	.get = snd_es18xx_get_hw_volume,
  },
  {
  	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  	.name = "Hardware Master Playback Switch",
  	.access = SNDRV_CTL_ELEM_ACCESS_READ,
  	.info = snd_es18xx_info_hw_switch,
  	.get = snd_es18xx_get_hw_switch,
  },
  ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
  };
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1300
1301
1302
1303
  static struct snd_kcontrol_new snd_es18xx_opt_gpo_2bit[] = {
  ES18XX_SINGLE("GPO0 Switch", 0, ES18XX_PM, 0, 1, ES18XX_FL_PMPORT),
  ES18XX_SINGLE("GPO1 Switch", 0, ES18XX_PM, 1, 1, ES18XX_FL_PMPORT),
  };
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1304
  static int snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1305
1306
  {
  	int data;
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1307

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1308
1309
  	outb(reg, chip->ctrl_port);
  	data = inb(chip->ctrl_port + 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1310
1311
  	return data;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1312

1bff292e9   Bill Pemberton   ALSA: isa: remove...
1313
1314
  static void snd_es18xx_config_write(struct snd_es18xx *chip,
  				    unsigned char reg, unsigned char data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1315
1316
1317
1318
1319
1320
  {
  	/* No need for spinlocks, this function is used only in
  	   otherwise protected init code */
  	outb(reg, chip->ctrl_port);
  	outb(data, chip->ctrl_port + 1);
  #ifdef REG_DEBUG
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1321
1322
  	snd_printk(KERN_DEBUG "Config reg %02x set to %02x
  ", reg, data);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1323
1324
  #endif
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1325
1326
1327
  static int snd_es18xx_initialize(struct snd_es18xx *chip,
  				 unsigned long mpu_port,
  				 unsigned long fm_port)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
  {
  	int mask = 0;
  
          /* enable extended mode */
          snd_es18xx_dsp_command(chip, 0xC6);
  	/* Reset mixer registers */
  	snd_es18xx_mixer_write(chip, 0x00, 0x00);
  
          /* Audio 1 DMA demand mode (4 bytes/request) */
          snd_es18xx_write(chip, 0xB9, 2);
  	if (chip->caps & ES18XX_CONTROL) {
  		/* Hardware volume IRQ */
  		snd_es18xx_config_write(chip, 0x27, chip->irq);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1341
  		if (fm_port > 0 && fm_port != SNDRV_AUTO_PORT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1342
  			/* FM I/O */
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1343
1344
  			snd_es18xx_config_write(chip, 0x62, fm_port >> 8);
  			snd_es18xx_config_write(chip, 0x63, fm_port & 0xff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1345
  		}
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1346
  		if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1347
  			/* MPU-401 I/O */
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1348
1349
  			snd_es18xx_config_write(chip, 0x64, mpu_port >> 8);
  			snd_es18xx_config_write(chip, 0x65, mpu_port & 0xff);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
  			/* MPU-401 IRQ */
  			snd_es18xx_config_write(chip, 0x28, chip->irq);
  		}
  		/* Audio1 IRQ */
  		snd_es18xx_config_write(chip, 0x70, chip->irq);
  		/* Audio2 IRQ */
  		snd_es18xx_config_write(chip, 0x72, chip->irq);
  		/* Audio1 DMA */
  		snd_es18xx_config_write(chip, 0x74, chip->dma1);
  		/* Audio2 DMA */
  		snd_es18xx_config_write(chip, 0x75, chip->dma2);
  
  		/* Enable Audio 1 IRQ */
  		snd_es18xx_write(chip, 0xB1, 0x50);
  		/* Enable Audio 2 IRQ */
  		snd_es18xx_mixer_write(chip, 0x7A, 0x40);
  		/* Enable Audio 1 DMA */
  		snd_es18xx_write(chip, 0xB2, 0x50);
  		/* Enable MPU and hardware volume interrupt */
  		snd_es18xx_mixer_write(chip, 0x64, 0x42);
1bc9eed37   Krzysztof Helt   [ALSA] es18xx: En...
1370
1371
  		/* Enable ESS wavetable input */
  		snd_es18xx_mixer_bits(chip, 0x48, 0x10, 0x10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
  	}
  	else {
  		int irqmask, dma1mask, dma2mask;
  		switch (chip->irq) {
  		case 2:
  		case 9:
  			irqmask = 0;
  			break;
  		case 5:
  			irqmask = 1;
  			break;
  		case 7:
  			irqmask = 2;
  			break;
  		case 10:
  			irqmask = 3;
  			break;
  		default:
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1390
1391
  			snd_printk(KERN_ERR "invalid irq %d
  ", chip->irq);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
  			return -ENODEV;
  		}
  		switch (chip->dma1) {
  		case 0:
  			dma1mask = 1;
  			break;
  		case 1:
  			dma1mask = 2;
  			break;
  		case 3:
  			dma1mask = 3;
  			break;
  		default:
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1405
1406
  			snd_printk(KERN_ERR "invalid dma1 %d
  ", chip->dma1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
  			return -ENODEV;
  		}
  		switch (chip->dma2) {
  		case 0:
  			dma2mask = 0;
  			break;
  		case 1:
  			dma2mask = 1;
  			break;
  		case 3:
  			dma2mask = 2;
  			break;
  		case 5:
  			dma2mask = 3;
  			break;
  		default:
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1423
1424
  			snd_printk(KERN_ERR "invalid dma2 %d
  ", chip->dma2);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
  			return -ENODEV;
  		}
  
  		/* Enable and set Audio 1 IRQ */
  		snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
  		/* Enable and set Audio 1 DMA */
  		snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
  		/* Set Audio 2 DMA */
  		snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
  		/* Enable Audio 2 IRQ and DMA
  		   Set capture mixer input */
  		snd_es18xx_mixer_write(chip, 0x7A, 0x68);
  		/* Enable and set hardware volume interrupt */
  		snd_es18xx_mixer_write(chip, 0x64, 0x06);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1439
  		if (mpu_port > 0 && mpu_port != SNDRV_AUTO_PORT) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1440
1441
1442
  			/* MPU401 share irq with audio
  			   Joystick enabled
  			   FM enabled */
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1443
1444
  			snd_es18xx_mixer_write(chip, 0x40,
  					       0x43 | (mpu_port & 0xf0) >> 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
  		}
  		snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
  	}
  	if (chip->caps & ES18XX_NEW_RATE) {
  		/* Change behaviour of register A1
  		   4x oversampling
  		   2nd channel DAC asynchronous */
  		snd_es18xx_mixer_write(chip, 0x71, 0x32);
  	}
  	if (!(chip->caps & ES18XX_PCM2)) {
  		/* Enable DMA FIFO */
  		snd_es18xx_write(chip, 0xB7, 0x80);
  	}
  	if (chip->caps & ES18XX_SPATIALIZER) {
  		/* Set spatializer parameters to recommended values */
  		snd_es18xx_mixer_write(chip, 0x54, 0x8f);
  		snd_es18xx_mixer_write(chip, 0x56, 0x95);
  		snd_es18xx_mixer_write(chip, 0x58, 0x94);
  		snd_es18xx_mixer_write(chip, 0x5a, 0x80);
  	}
95b712965   Mark Salazar   [ALSA] #4/4 for Z...
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
  	/* Flip the "enable I2S" bits for those chipsets that need it */
  	switch (chip->version) {
  	case 0x1879:
  		//Leaving I2S enabled on the 1879 screws up the PCM playback (rate effected somehow)
  		//so a Switch control has been added to toggle this 0x71 bit on/off:
  		//snd_es18xx_mixer_bits(chip, 0x71, 0x40, 0x40);
  		/* Note: we fall through on purpose here. */
  	case 0x1878:
  		snd_es18xx_config_write(chip, 0x29, snd_es18xx_config_read(chip, 0x29) | 0x40);
  		break;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
  	/* Mute input source */
  	if (chip->caps & ES18XX_MUTEREC)
  		mask = 0x10;
  	if (chip->caps & ES18XX_RECMIX)
  		snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
  	else {
  		snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
  		snd_es18xx_write(chip, 0xb4, 0x00);
  	}
  #ifndef AVOID_POPS
  	/* Enable PCM output */
  	snd_es18xx_dsp_command(chip, 0xD1);
  #endif
  
          return 0;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1492
  static int snd_es18xx_identify(struct snd_es18xx *chip)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1493
1494
1495
1496
1497
  {
  	int hi,lo;
  
  	/* reset */
  	if (snd_es18xx_reset(chip) < 0) {
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1498
1499
  		snd_printk(KERN_ERR "reset at 0x%lx failed!!!
  ", chip->port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
  		return -ENODEV;
  	}
  
  	snd_es18xx_dsp_command(chip, 0xe7);
  	hi = snd_es18xx_dsp_get_byte(chip);
  	if (hi < 0) {
  		return hi;
  	}
  	lo = snd_es18xx_dsp_get_byte(chip);
  	if ((lo & 0xf0) != 0x80) {
  		return -ENODEV;
  	}
  	if (hi == 0x48) {
  		chip->version = 0x488;
  		return 0;
  	}
  	if (hi != 0x68) {
  		return -ENODEV;
  	}
  	if ((lo & 0x0f) < 8) {
  		chip->version = 0x688;
  		return 0;
  	}
  			
          outb(0x40, chip->port + 0x04);
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1525
  	udelay(10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1526
  	hi = inb(chip->port + 0x05);
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1527
  	udelay(10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1528
1529
1530
1531
  	lo = inb(chip->port + 0x05);
  	if (hi != lo) {
  		chip->version = hi << 8 | lo;
  		chip->ctrl_port = inb(chip->port + 0x05) << 8;
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1532
  		udelay(10);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
  		chip->ctrl_port += inb(chip->port + 0x05);
  
  		if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
  			snd_printk(KERN_ERR PFX "unable go grab port 0x%lx
  ", chip->ctrl_port);
  			return -EBUSY;
  		}
  
  		return 0;
  	}
  
  	/* If has Hardware volume */
  	if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
  		/* If has Audio2 */
  		if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
  			/* If has volume count */
  			if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
  				chip->version = 0x1887;
  			} else {
  				chip->version = 0x1888;
  			}
  		} else {
  			chip->version = 0x1788;
  		}
  	}
  	else
  		chip->version = 0x1688;
  	return 0;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1562
1563
1564
  static int snd_es18xx_probe(struct snd_es18xx *chip,
  			    unsigned long mpu_port,
  			    unsigned long fm_port)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1565
1566
1567
1568
1569
1570
1571
1572
1573
  {
  	if (snd_es18xx_identify(chip) < 0) {
  		snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found
  ", chip->port);
                  return -ENODEV;
  	}
  
  	switch (chip->version) {
  	case 0x1868:
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1574
  		chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_GPO_2BIT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1575
1576
  		break;
  	case 0x1869:
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1577
  		chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV | ES18XX_GPO_2BIT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1578
1579
  		break;
  	case 0x1878:
14086771c   Mark Salazar   [ALSA] #3/4 for Z...
1580
  		chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1581
1582
1583
1584
1585
  		break;
  	case 0x1879:
  		chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
  		break;
  	case 0x1887:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1586
  	case 0x1888:
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1587
  		chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_GPO_2BIT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1588
1589
  		break;
  	default:
99b359ba1   Takashi Iwai   [ALSA] Add missin...
1590
1591
  		snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x
  ",
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1592
1593
1594
1595
1596
1597
1598
1599
1600
                             chip->port, chip->version);
                  return -ENODEV;
          }
  
          snd_printd("[0x%lx] ESS%x chip found
  ", chip->port, chip->version);
  
  	if (chip->dma1 == chip->dma2)
  		chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1601
  	return snd_es18xx_initialize(chip, mpu_port, fm_port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1602
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1603
  static struct snd_pcm_ops snd_es18xx_playback_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1604
1605
1606
1607
1608
1609
1610
1611
1612
  	.open =		snd_es18xx_playback_open,
  	.close =	snd_es18xx_playback_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_es18xx_playback_hw_params,
  	.hw_free =	snd_es18xx_pcm_hw_free,
  	.prepare =	snd_es18xx_playback_prepare,
  	.trigger =	snd_es18xx_playback_trigger,
  	.pointer =	snd_es18xx_playback_pointer,
  };
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1613
  static struct snd_pcm_ops snd_es18xx_capture_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1614
1615
1616
1617
1618
1619
1620
1621
1622
  	.open =		snd_es18xx_capture_open,
  	.close =	snd_es18xx_capture_close,
  	.ioctl =	snd_pcm_lib_ioctl,
  	.hw_params =	snd_es18xx_capture_hw_params,
  	.hw_free =	snd_es18xx_pcm_hw_free,
  	.prepare =	snd_es18xx_capture_prepare,
  	.trigger =	snd_es18xx_capture_trigger,
  	.pointer =	snd_es18xx_capture_pointer,
  };
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1623
1624
  static int snd_es18xx_pcm(struct snd_card *card, int device,
  			  struct snd_pcm **rpcm)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1625
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1626
  	struct snd_es18xx *chip = card->private_data;
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1627
          struct snd_pcm *pcm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1628
1629
1630
1631
1632
1633
  	char str[16];
  	int err;
  
  	if (rpcm)
  		*rpcm = NULL;
  	sprintf(str, "ES%x", chip->version);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
1634
  	if (chip->caps & ES18XX_PCM2)
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1635
  		err = snd_pcm_new(card, str, device, 2, 1, &pcm);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
1636
  	else
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1637
  		err = snd_pcm_new(card, str, device, 1, 1, &pcm);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1638
1639
1640
1641
1642
1643
1644
1645
          if (err < 0)
                  return err;
  
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
  	snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
  
  	/* global setup */
          pcm->private_data = chip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
          pcm->info_flags = 0;
  	if (chip->caps & ES18XX_DUPLEX_SAME)
  		pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
  	if (! (chip->caps & ES18XX_PCM2))
  		pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
  	sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
          chip->pcm = pcm;
  
  	snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
  					      snd_dma_isa_data(),
  					      64*1024,
  					      chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
  
          if (rpcm)
          	*rpcm = pcm;
  	return 0;
  }
  
  /* Power Management support functions */
  #ifdef CONFIG_PM
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1666
  static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1667
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1668
  	struct snd_es18xx *chip = card->private_data;
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
1669

3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1670
  	snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
  
  	snd_pcm_suspend_all(chip->pcm);
  
  	/* power down */
  	chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
  	chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
  	snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
  	snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
  
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1682
  static int snd_es18xx_resume(struct snd_card *card)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1683
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1684
  	struct snd_es18xx *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1685
1686
1687
  
  	/* restore PM register, we won't wake till (not 0x07) i/o activity though */
  	snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1688
  	snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1689
1690
1691
  	return 0;
  }
  #endif /* CONFIG_PM */
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1692
  static int snd_es18xx_free(struct snd_card *card)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1693
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1694
  	struct snd_es18xx *chip = card->private_data;
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1695

b1d5776d8   Takashi Iwai   [ALSA] Remove vma...
1696
1697
1698
  	release_and_free_resource(chip->res_port);
  	release_and_free_resource(chip->res_ctrl_port);
  	release_and_free_resource(chip->res_mpu_port);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1699
  	if (chip->irq >= 0)
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1700
  		free_irq(chip->irq, (void *) card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1701
1702
1703
1704
1705
1706
1707
1708
  	if (chip->dma1 >= 0) {
  		disable_dma(chip->dma1);
  		free_dma(chip->dma1);
  	}
  	if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
  		disable_dma(chip->dma2);
  		free_dma(chip->dma2);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1709
1710
  	return 0;
  }
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1711
  static int snd_es18xx_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1712
  {
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1713
  	return snd_es18xx_free(device->card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1714
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1715
1716
1717
1718
1719
  static int snd_es18xx_new_device(struct snd_card *card,
  				 unsigned long port,
  				 unsigned long mpu_port,
  				 unsigned long fm_port,
  				 int irq, int dma1, int dma2)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1720
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1721
  	struct snd_es18xx *chip = card->private_data;
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1722
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1723
1724
1725
  		.dev_free =	snd_es18xx_dev_free,
          };
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1726
1727
  	spin_lock_init(&chip->reg_lock);
   	spin_lock_init(&chip->mixer_lock);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1728
          chip->port = port;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1729
1730
1731
1732
1733
          chip->irq = -1;
          chip->dma1 = -1;
          chip->dma2 = -1;
          chip->audio2_vol = 0x00;
  	chip->active = 0;
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1734
1735
1736
  	chip->res_port = request_region(port, 16, "ES18xx");
  	if (chip->res_port == NULL) {
  		snd_es18xx_free(card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1737
1738
1739
1740
  		snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx
  ", port, port + 16 - 1);
  		return -EBUSY;
  	}
88e24c3a4   Yong Zhang   sound: irq: Remov...
1741
  	if (request_irq(irq, snd_es18xx_interrupt, 0, "ES18xx",
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1742
1743
  			(void *) card)) {
  		snd_es18xx_free(card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1744
1745
1746
1747
1748
1749
1750
  		snd_printk(KERN_ERR PFX "unable to grap IRQ %d
  ", irq);
  		return -EBUSY;
  	}
  	chip->irq = irq;
  
  	if (request_dma(dma1, "ES18xx DMA 1")) {
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1751
  		snd_es18xx_free(card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1752
1753
1754
1755
1756
1757
1758
  		snd_printk(KERN_ERR PFX "unable to grap DMA1 %d
  ", dma1);
  		return -EBUSY;
  	}
  	chip->dma1 = dma1;
  
  	if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1759
  		snd_es18xx_free(card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1760
1761
1762
1763
1764
  		snd_printk(KERN_ERR PFX "unable to grap DMA2 %d
  ", dma2);
  		return -EBUSY;
  	}
  	chip->dma2 = dma2;
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1765
  	if (snd_es18xx_probe(chip, mpu_port, fm_port) < 0) {
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1766
  		snd_es18xx_free(card);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
1767
1768
1769
  		return -ENODEV;
  	}
  	err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops);
3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
1770
1771
  	if (err < 0) {
  		snd_es18xx_free(card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1772
1773
  		return err;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1774
1775
          return 0;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1776
  static int snd_es18xx_mixer(struct snd_card *card)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1777
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1778
  	struct snd_es18xx *chip = card->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1779
1780
  	int err;
  	unsigned int idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1781
1782
1783
  	strcpy(card->mixername, chip->pcm->name);
  
  	for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1784
  		struct snd_kcontrol *kctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
  		kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
  		if (chip->caps & ES18XX_HWV) {
  			switch (idx) {
  			case 0:
  				chip->master_volume = kctl;
  				kctl->private_free = snd_es18xx_hwv_free;
  				break;
  			case 1:
  				chip->master_switch = kctl;
  				kctl->private_free = snd_es18xx_hwv_free;
  				break;
  			}
  		}
  		if ((err = snd_ctl_add(card, kctl)) < 0)
  			return err;
  	}
  	if (chip->caps & ES18XX_PCM2) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
  			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
  				return err;
  		} 
  	} else {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
  			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
  				return err;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
  	if (chip->caps & ES18XX_RECMIX) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
  			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
  				return err;
  		}
  	}
  	switch (chip->version) {
  	default:
  		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
  			return err;
  		break;
  	case 0x1869:
  	case 0x1879:
  		if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
  			return err;
  		break;
  	}
  	if (chip->caps & ES18XX_SPATIALIZER) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
  			if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
  				return err;
  		}
  	}
  	if (chip->caps & ES18XX_HWV) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
1837
  			struct snd_kcontrol *kctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
  			kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
  			if (idx == 0)
  				chip->hw_volume = kctl;
  			else
  				chip->hw_switch = kctl;
  			kctl->private_free = snd_es18xx_hwv_free;
  			if ((err = snd_ctl_add(card, kctl)) < 0)
  				return err;
  			
  		}
  	}
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
  	/* finish initializing other chipset specific controls
  	 */
  	if (chip->version != 0x1868) {
  		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
  						     chip));
  		if (err < 0)
  			return err;
  	}
  	if (chip->version == 0x1869) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
  			err = snd_ctl_add(card,
  					  snd_ctl_new1(&snd_es18xx_opt_1869[idx],
  						       chip));
  			if (err < 0)
  				return err;
  		}
95b712965   Mark Salazar   [ALSA] #4/4 for Z...
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
  	} else if (chip->version == 0x1878) {
  		err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_1878,
  						     chip));
  		if (err < 0)
  			return err;
  	} else if (chip->version == 0x1879) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1879); idx++) {
  			err = snd_ctl_add(card,
  					  snd_ctl_new1(&snd_es18xx_opt_1879[idx],
  						       chip));
  			if (err < 0)
  				return err;
  		}
c1f6d4159   Mark Salazar   [ALSA] #1/4 for Z...
1878
  	}
2603fe21b   Ondrej Zary   ALSA: es18xx: Add...
1879
1880
1881
1882
1883
1884
1885
1886
1887
  	if (chip->caps & ES18XX_GPO_2BIT) {
  		for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_gpo_2bit); idx++) {
  			err = snd_ctl_add(card,
  					  snd_ctl_new1(&snd_es18xx_opt_gpo_2bit[idx],
  						       chip));
  			if (err < 0)
  				return err;
  		}
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
  	return 0;
  }
         
  
  /* Card level */
  
  MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");  
  MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
  MODULE_LICENSE("GPL");
  MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
  		"{ESS,ES1869 PnP AudioDrive},"
  		"{ESS,ES1878 PnP AudioDrive},"
  		"{ESS,ES1879 PnP AudioDrive},"
  		"{ESS,ES1887 PnP AudioDrive},"
  		"{ESS,ES1888 PnP AudioDrive},"
  		"{ESS,ES1887 AudioDrive},"
  		"{ESS,ES1888 AudioDrive}}");
  
  static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;	/* Index 0-MAX */
  static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;	/* ID for this card */
a67ff6a54   Rusty Russell   ALSA: module_para...
1908
  static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1909
  #ifdef CONFIG_PNP
a67ff6a54   Rusty Russell   ALSA: module_para...
1910
  static bool isapnp[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
  #endif
  static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;	/* 0x220,0x240,0x260,0x280 */
  #ifndef CONFIG_PNP
  static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
  #else
  static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  #endif
  static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
  static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ;	/* 5,7,9,10 */
  static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
  static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA;	/* 0,1,3 */
  
  module_param_array(index, int, NULL, 0444);
  MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
  module_param_array(id, charp, NULL, 0444);
  MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
  module_param_array(enable, bool, NULL, 0444);
  MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
  #ifdef CONFIG_PNP
  module_param_array(isapnp, bool, NULL, 0444);
  MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
  #endif
  module_param_array(port, long, NULL, 0444);
  MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
  module_param_array(mpu_port, long, NULL, 0444);
  MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
  module_param_array(fm_port, long, NULL, 0444);
  MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
  module_param_array(irq, int, NULL, 0444);
  MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
  module_param_array(dma1, int, NULL, 0444);
  MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
  module_param_array(dma2, int, NULL, 0444);
  MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1945
  #ifdef CONFIG_PNP
609d76941   Rene Herman   [ALSA] Fix probe ...
1946
1947
1948
  static int isa_registered;
  static int pnp_registered;
  static int pnpc_registered;
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1949
1950
1951
  
  static struct pnp_device_id snd_audiodrive_pnpbiosids[] = {
  	{ .id = "ESS1869" },
4848ffe52   Rene Herman   [ALSA] add the ES...
1952
  	{ .id = "ESS1879" },
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1953
1954
1955
1956
1957
1958
  	{ .id = "" }		/* end */
  };
  
  MODULE_DEVICE_TABLE(pnp, snd_audiodrive_pnpbiosids);
  
  /* PnP main device initialization */
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1959
  static int snd_audiodrive_pnp_init_main(int dev, struct pnp_dev *pdev)
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1960
  {
109c53f84   Rene Herman   [ALSA] sound/isa:...
1961
  	if (pnp_activate_dev(pdev) < 0) {
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
  		snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)
  ");
  		return -EBUSY;
  	}
  	/* ok. hack using Vendor-Defined Card-Level registers */
  	/* skip csn and logdev initialization - already done in isapnp_configure */
  	if (pnp_device_is_isapnp(pdev)) {
  		isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
  		isapnp_write_byte(0x27, pnp_irq(pdev, 0));	/* Hardware Volume IRQ Number */
  		if (mpu_port[dev] != SNDRV_AUTO_PORT)
  			isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
  		isapnp_write_byte(0x72, pnp_irq(pdev, 0));	/* second IRQ */
  		isapnp_cfg_end();
  	}
  	port[dev] = pnp_port_start(pdev, 0);
  	fm_port[dev] = pnp_port_start(pdev, 1);
  	mpu_port[dev] = pnp_port_start(pdev, 2);
  	dma1[dev] = pnp_dma(pdev, 0);
  	dma2[dev] = pnp_dma(pdev, 1);
  	irq[dev] = pnp_irq(pdev, 0);
  	snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx
  ", port[dev], fm_port[dev], mpu_port[dev]);
  	snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i
  ", dma1[dev], dma2[dev], irq[dev]);
  	return 0;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
1988
1989
  static int snd_audiodrive_pnp(int dev, struct snd_es18xx *chip,
  			      struct pnp_dev *pdev)
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1990
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
1991
1992
  	chip->dev = pdev;
  	if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1993
  		return -EBUSY;
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
1994
1995
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
  
  static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
  	/* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
  	{ .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
  	/* ESS 1868 (integrated on Maxisound Cards) */
  	{ .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
  	/* ESS 1868 (integrated on Maxisound Cards) */
  	{ .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
  	/* ESS ES1869 Plug and Play AudioDrive */
  	{ .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
  	/* ESS 1869 */
  	{ .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
  	/* ESS 1878 */
  	{ .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
  	/* ESS 1879 */
  	{ .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
  	/* --- */
  	{ .id = "" } /* end */
  };
  
  MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2017
2018
2019
  static int snd_audiodrive_pnpc(int dev, struct snd_es18xx *chip,
  			       struct pnp_card_link *card,
  			       const struct pnp_card_device_id *id)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2020
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
2021
2022
  	chip->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
  	if (chip->dev == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2023
  		return -EBUSY;
109c53f84   Rene Herman   [ALSA] sound/isa:...
2024

b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
2025
2026
  	chip->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
  	if (chip->devc == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2027
  		return -EBUSY;
109c53f84   Rene Herman   [ALSA] sound/isa:...
2028

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2029
  	/* Control port initialization */
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
2030
  	if (pnp_activate_dev(chip->devc) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2031
2032
2033
2034
  		snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)
  ");
  		return -EAGAIN;
  	}
aa0a2ddc5   Greg Kroah-Hartman   [PATCH] 64bit res...
2035
2036
  	snd_printdd("pnp: port=0x%llx
  ",
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
2037
2038
  			(unsigned long long)pnp_port_start(chip->devc, 0));
  	if (snd_audiodrive_pnp_init_main(dev, chip->dev) < 0)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2039
  		return -EBUSY;
109c53f84   Rene Herman   [ALSA] sound/isa:...
2040

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2041
2042
2043
  	return 0;
  }
  #endif /* CONFIG_PNP */
43bcd973d   Takashi Iwai   [ALSA] Add snd_ca...
2044
2045
2046
2047
2048
  #ifdef CONFIG_PNP
  #define is_isapnp_selected(dev)		isapnp[dev]
  #else
  #define is_isapnp_selected(dev)		0
  #endif
4323cc4d5   Takashi Iwai   ALSA: isa: Conver...
2049
2050
  static int snd_es18xx_card_new(struct device *pdev, int dev,
  			       struct snd_card **cardp)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2051
  {
4323cc4d5   Takashi Iwai   ALSA: isa: Conver...
2052
2053
  	return snd_card_new(pdev, index[dev], id[dev], THIS_MODULE,
  			    sizeof(struct snd_es18xx), cardp);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2054
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2055
  static int snd_audiodrive_probe(struct snd_card *card, int dev)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2056
  {
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
2057
  	struct snd_es18xx *chip = card->private_data;
8047c910f   Takashi Iwai   [ALSA] Remove xxx...
2058
  	struct snd_opl3 *opl3;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2059
  	int err;
b14f5de73   Krzysztof Helt   ALSA: es18xx: rem...
2060
2061
2062
2063
  	err = snd_es18xx_new_device(card,
  				    port[dev], mpu_port[dev], fm_port[dev],
  				    irq[dev], dma1[dev], dma2[dev]);
  	if (err < 0)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2064
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2065
2066
  
  	sprintf(card->driver, "ES%x", chip->version);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2067
  	
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2068
  	sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2069
  	if (dma1[dev] != dma2[dev])
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2070
2071
2072
  		sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
  			card->shortname,
  			chip->port,
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2073
  			irq[dev], dma1[dev], dma2[dev]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2074
2075
2076
2077
  	else
  		sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
  			card->shortname,
  			chip->port,
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2078
  			irq[dev], dma1[dev]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2079

3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
2080
2081
  	err = snd_es18xx_pcm(card, 0, NULL);
  	if (err < 0)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2082
  		return err;
43bcd973d   Takashi Iwai   [ALSA] Add snd_ca...
2083

3c76b4d69   Krzysztof Helt   ALSA: es18xx: rem...
2084
2085
  	err = snd_es18xx_mixer(card);
  	if (err < 0)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2086
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2087
2088
  
  	if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
2089
2090
2091
2092
2093
2094
  		if (snd_opl3_create(card, fm_port[dev], fm_port[dev] + 2,
  				    OPL3_HW_OPL3, 0, &opl3) < 0) {
  			snd_printk(KERN_WARNING PFX
  				   "opl3 not detected at 0x%lx
  ",
  				   fm_port[dev]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2095
  		} else {
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
2096
2097
  			err = snd_opl3_hwdep_new(opl3, 0, 1, NULL);
  			if (err < 0)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2098
  				return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2099
2100
2101
2102
  		}
  	}
  
  	if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
2103
  		err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
dba8b4699   Clemens Ladisch   ALSA: mpu401: cle...
2104
2105
  					  mpu_port[dev], MPU401_INFO_IRQ_HOOK,
  					  -1, &chip->rmidi);
faa1242c5   Krzysztof Helt   ALSA: es18xx: cod...
2106
  		if (err < 0)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2107
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2108
  	}
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2109
2110
  	return snd_card_register(card);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2111

1bff292e9   Bill Pemberton   ALSA: isa: remove...
2112
  static int snd_es18xx_isa_match(struct device *pdev, unsigned int dev)
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2113
2114
2115
  {
  	return enable[dev] && !is_isapnp_selected(dev);
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2116
  static int snd_es18xx_isa_probe1(int dev, struct device *devptr)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2117
2118
2119
  {
  	struct snd_card *card;
  	int err;
43bcd973d   Takashi Iwai   [ALSA] Add snd_ca...
2120

4323cc4d5   Takashi Iwai   ALSA: isa: Conver...
2121
  	err = snd_es18xx_card_new(devptr, dev, &card);
3e7fb9f7e   Takashi Iwai   ALSA: Return prop...
2122
2123
  	if (err < 0)
  		return err;
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2124
2125
2126
2127
  	if ((err = snd_audiodrive_probe(card, dev)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2128
  	dev_set_drvdata(devptr, card);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2129
2130
  	return 0;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2131
  static int snd_es18xx_isa_probe(struct device *pdev, unsigned int dev)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2132
  {
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2133
2134
2135
  	int err;
  	static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
  	static int possible_dmas[] = {1, 0, 3, 5, -1};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2136

f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
  	if (irq[dev] == SNDRV_AUTO_IRQ) {
  		if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
  			snd_printk(KERN_ERR PFX "unable to find a free IRQ
  ");
  			return -EBUSY;
  		}
  	}
  	if (dma1[dev] == SNDRV_AUTO_DMA) {
  		if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  			snd_printk(KERN_ERR PFX "unable to find a free DMA1
  ");
  			return -EBUSY;
  		}
  	}
  	if (dma2[dev] == SNDRV_AUTO_DMA) {
  		if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
  			snd_printk(KERN_ERR PFX "unable to find a free DMA2
  ");
  			return -EBUSY;
  		}
  	}
  
  	if (port[dev] != SNDRV_AUTO_PORT) {
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2160
  		return snd_es18xx_isa_probe1(dev, pdev);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2161
2162
2163
2164
2165
  	} else {
  		static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
  		int i;
  		for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
  			port[dev] = possible_ports[i];
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2166
  			err = snd_es18xx_isa_probe1(dev, pdev);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2167
2168
2169
2170
  			if (! err)
  				return 0;
  		}
  		return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2171
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2172
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2173
2174
  static int snd_es18xx_isa_remove(struct device *devptr,
  				 unsigned int dev)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2175
  {
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2176
  	snd_card_free(dev_get_drvdata(devptr));
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2177
2178
2179
2180
  	return 0;
  }
  
  #ifdef CONFIG_PM
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2181
2182
  static int snd_es18xx_isa_suspend(struct device *dev, unsigned int n,
  				  pm_message_t state)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2183
  {
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2184
  	return snd_es18xx_suspend(dev_get_drvdata(dev), state);
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2185
  }
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2186
  static int snd_es18xx_isa_resume(struct device *dev, unsigned int n)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2187
  {
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2188
  	return snd_es18xx_resume(dev_get_drvdata(dev));
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2189
2190
  }
  #endif
83c51c0ab   Rene Herman   [ALSA] isa_bus de...
2191
  #define DEV_NAME "es18xx"
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2192

5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2193
2194
2195
  static struct isa_driver snd_es18xx_isa_driver = {
  	.match		= snd_es18xx_isa_match,
  	.probe		= snd_es18xx_isa_probe,
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2196
  	.remove		= snd_es18xx_isa_remove,
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2197
  #ifdef CONFIG_PM
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2198
2199
  	.suspend	= snd_es18xx_isa_suspend,
  	.resume		= snd_es18xx_isa_resume,
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2200
2201
  #endif
  	.driver		= {
83c51c0ab   Rene Herman   [ALSA] isa_bus de...
2202
  		.name	= DEV_NAME
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2203
2204
  	},
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2205
2206
  
  #ifdef CONFIG_PNP
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2207
2208
  static int snd_audiodrive_pnp_detect(struct pnp_dev *pdev,
  				     const struct pnp_device_id *id)
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
  {
  	static int dev;
  	int err;
  	struct snd_card *card;
  
  	if (pnp_device_is_isapnp(pdev))
  		return -ENOENT;	/* we have another procedure - card */
  	for (; dev < SNDRV_CARDS; dev++) {
  		if (enable[dev] && isapnp[dev])
  			break;
  	}
  	if (dev >= SNDRV_CARDS)
  		return -ENODEV;
4323cc4d5   Takashi Iwai   ALSA: isa: Conver...
2222
  	err = snd_es18xx_card_new(&pdev->dev, dev, &card);
3e7fb9f7e   Takashi Iwai   ALSA: Return prop...
2223
2224
  	if (err < 0)
  		return err;
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2225
2226
2227
2228
  	if ((err = snd_audiodrive_pnp(dev, card->private_data, pdev)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2229
2230
2231
2232
2233
2234
  	if ((err = snd_audiodrive_probe(card, dev)) < 0) {
  		snd_card_free(card);
  		return err;
  	}
  	pnp_set_drvdata(pdev, card);
  	dev++;
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2235
2236
  	return 0;
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2237
  static void snd_audiodrive_pnp_remove(struct pnp_dev *pdev)
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2238
2239
  {
  	snd_card_free(pnp_get_drvdata(pdev));
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
  }
  
  #ifdef CONFIG_PM
  static int snd_audiodrive_pnp_suspend(struct pnp_dev *pdev, pm_message_t state)
  {
  	return snd_es18xx_suspend(pnp_get_drvdata(pdev), state);
  }
  static int snd_audiodrive_pnp_resume(struct pnp_dev *pdev)
  {
  	return snd_es18xx_resume(pnp_get_drvdata(pdev));
  }
  #endif
  
  static struct pnp_driver es18xx_pnp_driver = {
  	.name = "es18xx-pnpbios",
  	.id_table = snd_audiodrive_pnpbiosids,
  	.probe = snd_audiodrive_pnp_detect,
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2257
  	.remove = snd_audiodrive_pnp_remove,
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2258
2259
2260
2261
2262
  #ifdef CONFIG_PM
  	.suspend = snd_audiodrive_pnp_suspend,
  	.resume = snd_audiodrive_pnp_resume,
  #endif
  };
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2263
2264
  static int snd_audiodrive_pnpc_detect(struct pnp_card_link *pcard,
  				      const struct pnp_card_device_id *pid)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2265
2266
  {
  	static int dev;
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2267
  	struct snd_card *card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2268
2269
2270
  	int res;
  
  	for ( ; dev < SNDRV_CARDS; dev++) {
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2271
2272
2273
2274
2275
  		if (enable[dev] && isapnp[dev])
  			break;
  	}
  	if (dev >= SNDRV_CARDS)
  		return -ENODEV;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2276

4323cc4d5   Takashi Iwai   ALSA: isa: Conver...
2277
  	res = snd_es18xx_card_new(&pcard->card->dev, dev, &card);
3e7fb9f7e   Takashi Iwai   ALSA: Return prop...
2278
2279
  	if (res < 0)
  		return res;
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2280

1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2281
  	if ((res = snd_audiodrive_pnpc(dev, card->private_data, pcard, pid)) < 0) {
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2282
2283
2284
  		snd_card_free(card);
  		return res;
  	}
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2285
2286
2287
2288
2289
2290
2291
2292
  	if ((res = snd_audiodrive_probe(card, dev)) < 0) {
  		snd_card_free(card);
  		return res;
  	}
  
  	pnp_set_card_drvdata(pcard, card);
  	dev++;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2293
  }
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2294
  static void snd_audiodrive_pnpc_remove(struct pnp_card_link *pcard)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2295
  {
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2296
2297
2298
  	snd_card_free(pnp_get_card_drvdata(pcard));
  	pnp_set_card_drvdata(pcard, NULL);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2299

f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2300
  #ifdef CONFIG_PM
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2301
  static int snd_audiodrive_pnpc_suspend(struct pnp_card_link *pcard, pm_message_t state)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2302
2303
2304
  {
  	return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
  }
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2305
  static int snd_audiodrive_pnpc_resume(struct pnp_card_link *pcard)
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2306
2307
  {
  	return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2308
  }
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2309
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2310
2311
2312
2313
  static struct pnp_card_driver es18xx_pnpc_driver = {
  	.flags = PNP_DRIVER_RES_DISABLE,
  	.name = "es18xx",
  	.id_table = snd_audiodrive_pnpids,
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2314
  	.probe = snd_audiodrive_pnpc_detect,
1bff292e9   Bill Pemberton   ALSA: isa: remove...
2315
  	.remove = snd_audiodrive_pnpc_remove,
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2316
  #ifdef CONFIG_PM
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2317
2318
  	.suspend	= snd_audiodrive_pnpc_suspend,
  	.resume		= snd_audiodrive_pnpc_resume,
f7e0ba3e4   Takashi Iwai   [ALSA] es18xx - U...
2319
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2320
2321
2322
2323
2324
  };
  #endif /* CONFIG_PNP */
  
  static int __init alsa_card_es18xx_init(void)
  {
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2325
  	int err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2326

5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2327
  	err = isa_register_driver(&snd_es18xx_isa_driver, SNDRV_CARDS);
59b1b34f4   Takashi Iwai   [ALSA] Fix compil...
2328
  #ifdef CONFIG_PNP
609d76941   Rene Herman   [ALSA] Fix probe ...
2329
2330
  	if (!err)
  		isa_registered = 1;
1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2331
2332
  	err = pnp_register_driver(&es18xx_pnp_driver);
  	if (!err)
f7a9275d9   Clemens Ladisch   [ALSA] unregister...
2333
  		pnp_registered = 1;
609d76941   Rene Herman   [ALSA] Fix probe ...
2334

1c3985580   Ondrej Zary   [ALSA] es18xx - A...
2335
2336
2337
  	err = pnp_register_card_driver(&es18xx_pnpc_driver);
  	if (!err)
  		pnpc_registered = 1;
609d76941   Rene Herman   [ALSA] Fix probe ...
2338
2339
2340
  
  	if (isa_registered || pnp_registered)
  		err = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2341
  #endif
609d76941   Rene Herman   [ALSA] Fix probe ...
2342
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2343
2344
2345
2346
  }
  
  static void __exit alsa_card_es18xx_exit(void)
  {
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2347
2348
2349
2350
2351
  #ifdef CONFIG_PNP
  	if (pnpc_registered)
  		pnp_unregister_card_driver(&es18xx_pnpc_driver);
  	if (pnp_registered)
  		pnp_unregister_driver(&es18xx_pnp_driver);
609d76941   Rene Herman   [ALSA] Fix probe ...
2352
  	if (isa_registered)
5e24c1c1c   Takashi Iwai   [ALSA] Port the r...
2353
  #endif
609d76941   Rene Herman   [ALSA] Fix probe ...
2354
  		isa_unregister_driver(&snd_es18xx_isa_driver);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2355
2356
2357
2358
  }
  
  module_init(alsa_card_es18xx_init)
  module_exit(alsa_card_es18xx_exit)