blob: bde9860327fef9310b0e07167ee496cbc4b37b60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Driver for generic ESS AudioDrive ES18xx soundcards
3 * Copyright (c) by Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>
4 * Copyright (c) by Abramo Bagnara <abramo@alsa-project.org>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22/* GENERAL NOTES:
23 *
24 * BUGS:
25 * - There are pops (we can't delay in trigger function, cause midlevel
26 * often need to trigger down and then up very quickly).
27 * Any ideas?
28 * - Support for 16 bit DMA seems to be broken. I've no hardware to tune it.
29 */
30
31/*
32 * ES1868 NOTES:
33 * - The chip has one half duplex pcm (with very limited full duplex support).
34 *
35 * - Duplex stereophonic sound is impossible.
36 * - Record and playback must share the same frequency rate.
37 *
38 * - The driver use dma2 for playback and dma1 for capture.
39 */
40
41/*
42 * ES1869 NOTES:
43 *
44 * - there are a first full duplex pcm and a second playback only pcm
45 * (incompatible with first pcm capture)
46 *
47 * - there is support for the capture volume and ESS Spatializer 3D effect.
48 *
49 * - contrarily to some pages in DS_1869.PDF the rates can be set
50 * independently.
51 *
52 * BUGS:
53 *
54 * - There is a major trouble I noted:
55 *
56 * using both channel for playback stereo 16 bit samples at 44100 Hz
57 * the second pcm (Audio1) DMA slows down irregularly and sound is garbled.
58 *
59 * The same happens using Audio1 for captureing.
60 *
61 * The Windows driver does not suffer of this (although it use Audio1
62 * only for captureing). I'm unable to discover why.
63 *
64 */
65
66
67#include <sound/driver.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/init.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010069#include <linux/err.h>
70#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/pnp.h>
73#include <linux/isapnp.h>
74#include <linux/moduleparam.h>
Takashi Iwaif7e0ba32005-11-17 17:12:07 +010075#include <asm/io.h>
76#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <sound/core.h>
78#include <sound/control.h>
79#include <sound/pcm.h>
80#include <sound/pcm_params.h>
81#include <sound/mpu401.h>
82#include <sound/opl3.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#define SNDRV_LEGACY_FIND_FREE_IRQ
84#define SNDRV_LEGACY_FIND_FREE_DMA
85#include <sound/initval.h>
86
87#define PFX "es18xx: "
88
Takashi Iwai8047c912005-11-17 14:41:22 +010089struct snd_es18xx {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 unsigned long port; /* port of ESS chip */
91 unsigned long mpu_port; /* MPU-401 port of ESS chip */
92 unsigned long fm_port; /* FM port */
93 unsigned long ctrl_port; /* Control port of ESS chip */
94 struct resource *res_port;
95 struct resource *res_mpu_port;
96 struct resource *res_ctrl_port;
97 int irq; /* IRQ number of ESS chip */
98 int dma1; /* DMA1 */
99 int dma2; /* DMA2 */
100 unsigned short version; /* version of ESS chip */
101 int caps; /* Chip capabilities */
102 unsigned short audio2_vol; /* volume level of audio2 */
103
104 unsigned short active; /* active channel mask */
105 unsigned int dma1_size;
106 unsigned int dma2_size;
107 unsigned int dma1_shift;
108 unsigned int dma2_shift;
109
Takashi Iwai8047c912005-11-17 14:41:22 +0100110 struct snd_card *card;
111 struct snd_pcm *pcm;
112 struct snd_pcm_substream *playback_a_substream;
113 struct snd_pcm_substream *capture_a_substream;
114 struct snd_pcm_substream *playback_b_substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Takashi Iwai8047c912005-11-17 14:41:22 +0100116 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Takashi Iwai8047c912005-11-17 14:41:22 +0100118 struct snd_kcontrol *hw_volume;
119 struct snd_kcontrol *hw_switch;
120 struct snd_kcontrol *master_volume;
121 struct snd_kcontrol *master_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 spinlock_t reg_lock;
124 spinlock_t mixer_lock;
125 spinlock_t ctrl_lock;
126#ifdef CONFIG_PM
127 unsigned char pm_reg;
128#endif
129};
130
Takashi Iwaif7e0ba32005-11-17 17:12:07 +0100131struct snd_audiodrive {
132 struct snd_es18xx *chip;
133#ifdef CONFIG_PNP
134 struct pnp_dev *dev;
135 struct pnp_dev *devc;
136#endif
137};
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#define AUDIO1_IRQ 0x01
140#define AUDIO2_IRQ 0x02
141#define HWV_IRQ 0x04
142#define MPU_IRQ 0x08
143
144#define ES18XX_PCM2 0x0001 /* Has two useable PCM */
145#define ES18XX_SPATIALIZER 0x0002 /* Has 3D Spatializer */
146#define ES18XX_RECMIX 0x0004 /* Has record mixer */
147#define ES18XX_DUPLEX_MONO 0x0008 /* Has mono duplex only */
148#define ES18XX_DUPLEX_SAME 0x0010 /* Playback and record must share the same rate */
149#define ES18XX_NEW_RATE 0x0020 /* More precise rate setting */
150#define ES18XX_AUXB 0x0040 /* AuxB mixer control */
151#define ES18XX_HWV 0x0080 /* Has hardware volume */
152#define ES18XX_MONO 0x0100 /* Mono_in mixer control */
153#define ES18XX_I2S 0x0200 /* I2S mixer control */
154#define ES18XX_MUTEREC 0x0400 /* Record source can be muted */
155#define ES18XX_CONTROL 0x0800 /* Has control ports */
156
157/* Power Management */
158#define ES18XX_PM 0x07
159#define ES18XX_PM_GPO0 0x01
160#define ES18XX_PM_GPO1 0x02
161#define ES18XX_PM_PDR 0x04
162#define ES18XX_PM_ANA 0x08
163#define ES18XX_PM_FM 0x020
164#define ES18XX_PM_SUS 0x080
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* Lowlevel */
167
168#define DAC1 0x01
169#define ADC1 0x02
170#define DAC2 0x04
171#define MILLISECOND 10000
172
Takashi Iwai8047c912005-11-17 14:41:22 +0100173static int snd_es18xx_dsp_command(struct snd_es18xx *chip, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 int i;
176
177 for(i = MILLISECOND; i; i--)
178 if ((inb(chip->port + 0x0C) & 0x80) == 0) {
179 outb(val, chip->port + 0x0C);
180 return 0;
181 }
Takashi Iwai99b359b2005-10-20 18:26:44 +0200182 snd_printk(KERN_ERR "dsp_command: timeout (0x%x)\n", val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return -EINVAL;
184}
185
Takashi Iwai8047c912005-11-17 14:41:22 +0100186static int snd_es18xx_dsp_get_byte(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int i;
189
190 for(i = MILLISECOND/10; i; i--)
191 if (inb(chip->port + 0x0C) & 0x40)
192 return inb(chip->port + 0x0A);
Takashi Iwai99b359b2005-10-20 18:26:44 +0200193 snd_printk(KERN_ERR "dsp_get_byte failed: 0x%lx = 0x%x!!!\n",
194 chip->port + 0x0A, inb(chip->port + 0x0A));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return -ENODEV;
196}
197
198#undef REG_DEBUG
199
Takashi Iwai8047c912005-11-17 14:41:22 +0100200static int snd_es18xx_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned char reg, unsigned char data)
202{
203 unsigned long flags;
204 int ret;
205
206 spin_lock_irqsave(&chip->reg_lock, flags);
207 ret = snd_es18xx_dsp_command(chip, reg);
208 if (ret < 0)
209 goto end;
210 ret = snd_es18xx_dsp_command(chip, data);
211 end:
212 spin_unlock_irqrestore(&chip->reg_lock, flags);
213#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200214 snd_printk(KERN_DEBUG "Reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
216 return ret;
217}
218
Takashi Iwai8047c912005-11-17 14:41:22 +0100219static int snd_es18xx_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 unsigned long flags;
222 int ret, data;
223 spin_lock_irqsave(&chip->reg_lock, flags);
224 ret = snd_es18xx_dsp_command(chip, 0xC0);
225 if (ret < 0)
226 goto end;
227 ret = snd_es18xx_dsp_command(chip, reg);
228 if (ret < 0)
229 goto end;
230 data = snd_es18xx_dsp_get_byte(chip);
231 ret = data;
232#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200233 snd_printk(KERN_DEBUG "Reg %02x now is %02x (%d)\n", reg, data, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif
235 end:
236 spin_unlock_irqrestore(&chip->reg_lock, flags);
237 return ret;
238}
239
240/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100241static int snd_es18xx_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned char mask, unsigned char val)
243{
244 int ret;
245 unsigned char old, new, oval;
246 unsigned long flags;
247 spin_lock_irqsave(&chip->reg_lock, flags);
248 ret = snd_es18xx_dsp_command(chip, 0xC0);
249 if (ret < 0)
250 goto end;
251 ret = snd_es18xx_dsp_command(chip, reg);
252 if (ret < 0)
253 goto end;
254 ret = snd_es18xx_dsp_get_byte(chip);
255 if (ret < 0) {
256 goto end;
257 }
258 old = ret;
259 oval = old & mask;
260 if (val != oval) {
261 ret = snd_es18xx_dsp_command(chip, reg);
262 if (ret < 0)
263 goto end;
264 new = (old & ~mask) | (val & mask);
265 ret = snd_es18xx_dsp_command(chip, new);
266 if (ret < 0)
267 goto end;
268#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200269 snd_printk(KERN_DEBUG "Reg %02x was %02x, set to %02x (%d)\n",
270 reg, old, new, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#endif
272 }
273 ret = oval;
274 end:
275 spin_unlock_irqrestore(&chip->reg_lock, flags);
276 return ret;
277}
278
Takashi Iwai8047c912005-11-17 14:41:22 +0100279static inline void snd_es18xx_mixer_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned char reg, unsigned char data)
281{
282 unsigned long flags;
283 spin_lock_irqsave(&chip->mixer_lock, flags);
284 outb(reg, chip->port + 0x04);
285 outb(data, chip->port + 0x05);
286 spin_unlock_irqrestore(&chip->mixer_lock, flags);
287#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200288 snd_printk(KERN_DEBUG "Mixer reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289#endif
290}
291
Takashi Iwai8047c912005-11-17 14:41:22 +0100292static inline int snd_es18xx_mixer_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 unsigned long flags;
295 int data;
296 spin_lock_irqsave(&chip->mixer_lock, flags);
297 outb(reg, chip->port + 0x04);
298 data = inb(chip->port + 0x05);
299 spin_unlock_irqrestore(&chip->mixer_lock, flags);
300#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200301 snd_printk(KERN_DEBUG "Mixer reg %02x now is %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302#endif
303 return data;
304}
305
306/* Return old value */
Takashi Iwai8047c912005-11-17 14:41:22 +0100307static inline int snd_es18xx_mixer_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned char mask, unsigned char val)
309{
310 unsigned char old, new, oval;
311 unsigned long flags;
312 spin_lock_irqsave(&chip->mixer_lock, flags);
313 outb(reg, chip->port + 0x04);
314 old = inb(chip->port + 0x05);
315 oval = old & mask;
316 if (val != oval) {
317 new = (old & ~mask) | (val & mask);
318 outb(new, chip->port + 0x05);
319#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200320 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x\n",
321 reg, old, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322#endif
323 }
324 spin_unlock_irqrestore(&chip->mixer_lock, flags);
325 return oval;
326}
327
Takashi Iwai8047c912005-11-17 14:41:22 +0100328static inline int snd_es18xx_mixer_writable(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned char mask)
330{
331 int old, expected, new;
332 unsigned long flags;
333 spin_lock_irqsave(&chip->mixer_lock, flags);
334 outb(reg, chip->port + 0x04);
335 old = inb(chip->port + 0x05);
336 expected = old ^ mask;
337 outb(expected, chip->port + 0x05);
338 new = inb(chip->port + 0x05);
339 spin_unlock_irqrestore(&chip->mixer_lock, flags);
340#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +0200341 snd_printk(KERN_DEBUG "Mixer reg %02x was %02x, set to %02x, now is %02x\n",
342 reg, old, expected, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343#endif
344 return expected == new;
345}
346
347
Takashi Iwai8047c912005-11-17 14:41:22 +0100348static int snd_es18xx_reset(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 int i;
351 outb(0x03, chip->port + 0x06);
352 inb(chip->port + 0x06);
353 outb(0x00, chip->port + 0x06);
354 for(i = 0; i < MILLISECOND && !(inb(chip->port + 0x0E) & 0x80); i++);
355 if (inb(chip->port + 0x0A) != 0xAA)
356 return -1;
357 return 0;
358}
359
Takashi Iwai8047c912005-11-17 14:41:22 +0100360static int snd_es18xx_reset_fifo(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 outb(0x02, chip->port + 0x06);
363 inb(chip->port + 0x06);
364 outb(0x00, chip->port + 0x06);
365 return 0;
366}
367
Takashi Iwai8047c912005-11-17 14:41:22 +0100368static struct snd_ratnum new_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 {
370 .num = 793800,
371 .den_min = 1,
372 .den_max = 128,
373 .den_step = 1,
374 },
375 {
376 .num = 768000,
377 .den_min = 1,
378 .den_max = 128,
379 .den_step = 1,
380 }
381};
382
Takashi Iwai8047c912005-11-17 14:41:22 +0100383static struct snd_pcm_hw_constraint_ratnums new_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .nrats = 2,
385 .rats = new_clocks,
386};
387
Takashi Iwai8047c912005-11-17 14:41:22 +0100388static struct snd_ratnum old_clocks[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 {
390 .num = 795444,
391 .den_min = 1,
392 .den_max = 128,
393 .den_step = 1,
394 },
395 {
396 .num = 397722,
397 .den_min = 1,
398 .den_max = 128,
399 .den_step = 1,
400 }
401};
402
Takashi Iwai8047c912005-11-17 14:41:22 +0100403static struct snd_pcm_hw_constraint_ratnums old_hw_constraints_clocks = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .nrats = 2,
405 .rats = old_clocks,
406};
407
408
Takashi Iwai8047c912005-11-17 14:41:22 +0100409static void snd_es18xx_rate_set(struct snd_es18xx *chip,
410 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int mode)
412{
413 unsigned int bits, div0;
Takashi Iwai8047c912005-11-17 14:41:22 +0100414 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (chip->caps & ES18XX_NEW_RATE) {
416 if (runtime->rate_num == new_clocks[0].num)
417 bits = 128 - runtime->rate_den;
418 else
419 bits = 256 - runtime->rate_den;
420 } else {
421 if (runtime->rate_num == old_clocks[0].num)
422 bits = 256 - runtime->rate_den;
423 else
424 bits = 128 - runtime->rate_den;
425 }
426
427 /* set filter register */
428 div0 = 256 - 7160000*20/(8*82*runtime->rate);
429
430 if ((chip->caps & ES18XX_PCM2) && mode == DAC2) {
431 snd_es18xx_mixer_write(chip, 0x70, bits);
432 /*
433 * Comment from kernel oss driver:
434 * FKS: fascinating: 0x72 doesn't seem to work.
435 */
436 snd_es18xx_write(chip, 0xA2, div0);
437 snd_es18xx_mixer_write(chip, 0x72, div0);
438 } else {
439 snd_es18xx_write(chip, 0xA1, bits);
440 snd_es18xx_write(chip, 0xA2, div0);
441 }
442}
443
Takashi Iwai8047c912005-11-17 14:41:22 +0100444static int snd_es18xx_playback_hw_params(struct snd_pcm_substream *substream,
445 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Takashi Iwai8047c912005-11-17 14:41:22 +0100447 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 int shift, err;
449
450 shift = 0;
451 if (params_channels(hw_params) == 2)
452 shift++;
453 if (snd_pcm_format_width(params_format(hw_params)) == 16)
454 shift++;
455
456 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
457 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
458 (chip->capture_a_substream) &&
459 params_channels(hw_params) != 1) {
460 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
461 return -EBUSY;
462 }
463 chip->dma2_shift = shift;
464 } else {
465 chip->dma1_shift = shift;
466 }
467 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
468 return err;
469 return 0;
470}
471
Takashi Iwai8047c912005-11-17 14:41:22 +0100472static int snd_es18xx_pcm_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 return snd_pcm_lib_free_pages(substream);
475}
476
Takashi Iwai8047c912005-11-17 14:41:22 +0100477static int snd_es18xx_playback1_prepare(struct snd_es18xx *chip,
478 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Takashi Iwai8047c912005-11-17 14:41:22 +0100480 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
482 unsigned int count = snd_pcm_lib_period_bytes(substream);
483
484 chip->dma2_size = size;
485
486 snd_es18xx_rate_set(chip, substream, DAC2);
487
488 /* Transfer Count Reload */
489 count = 0x10000 - count;
490 snd_es18xx_mixer_write(chip, 0x74, count & 0xff);
491 snd_es18xx_mixer_write(chip, 0x76, count >> 8);
492
493 /* Set format */
494 snd_es18xx_mixer_bits(chip, 0x7A, 0x07,
495 ((runtime->channels == 1) ? 0x00 : 0x02) |
496 (snd_pcm_format_width(runtime->format) == 16 ? 0x01 : 0x00) |
497 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x04));
498
499 /* Set DMA controller */
500 snd_dma_program(chip->dma2, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
501
502 return 0;
503}
504
Takashi Iwai8047c912005-11-17 14:41:22 +0100505static int snd_es18xx_playback1_trigger(struct snd_es18xx *chip,
506 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int cmd)
508{
509 switch (cmd) {
510 case SNDRV_PCM_TRIGGER_START:
511 case SNDRV_PCM_TRIGGER_RESUME:
512 if (chip->active & DAC2)
513 return 0;
514 chip->active |= DAC2;
515 /* Start DMA */
516 if (chip->dma2 >= 4)
517 snd_es18xx_mixer_write(chip, 0x78, 0xb3);
518 else
519 snd_es18xx_mixer_write(chip, 0x78, 0x93);
520#ifdef AVOID_POPS
521 /* Avoid pops */
522 udelay(100000);
523 if (chip->caps & ES18XX_PCM2)
524 /* Restore Audio 2 volume */
525 snd_es18xx_mixer_write(chip, 0x7C, chip->audio2_vol);
526 else
527 /* Enable PCM output */
528 snd_es18xx_dsp_command(chip, 0xD1);
529#endif
530 break;
531 case SNDRV_PCM_TRIGGER_STOP:
532 case SNDRV_PCM_TRIGGER_SUSPEND:
533 if (!(chip->active & DAC2))
534 return 0;
535 chip->active &= ~DAC2;
536 /* Stop DMA */
537 snd_es18xx_mixer_write(chip, 0x78, 0x00);
538#ifdef AVOID_POPS
539 udelay(25000);
540 if (chip->caps & ES18XX_PCM2)
541 /* Set Audio 2 volume to 0 */
542 snd_es18xx_mixer_write(chip, 0x7C, 0);
543 else
544 /* Disable PCM output */
545 snd_es18xx_dsp_command(chip, 0xD3);
546#endif
547 break;
548 default:
549 return -EINVAL;
550 }
551
552 return 0;
553}
554
Takashi Iwai8047c912005-11-17 14:41:22 +0100555static int snd_es18xx_capture_hw_params(struct snd_pcm_substream *substream,
556 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Takashi Iwai8047c912005-11-17 14:41:22 +0100558 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int shift, err;
560
561 shift = 0;
562 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
563 chip->playback_a_substream &&
564 params_channels(hw_params) != 1) {
565 _snd_pcm_hw_param_setempty(hw_params, SNDRV_PCM_HW_PARAM_CHANNELS);
566 return -EBUSY;
567 }
568 if (params_channels(hw_params) == 2)
569 shift++;
570 if (snd_pcm_format_width(params_format(hw_params)) == 16)
571 shift++;
572 chip->dma1_shift = shift;
573 if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
574 return err;
575 return 0;
576}
577
Takashi Iwai8047c912005-11-17 14:41:22 +0100578static int snd_es18xx_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Takashi Iwai8047c912005-11-17 14:41:22 +0100580 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
581 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
583 unsigned int count = snd_pcm_lib_period_bytes(substream);
584
585 chip->dma1_size = size;
586
587 snd_es18xx_reset_fifo(chip);
588
589 /* Set stereo/mono */
590 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
591
592 snd_es18xx_rate_set(chip, substream, ADC1);
593
594 /* Transfer Count Reload */
595 count = 0x10000 - count;
596 snd_es18xx_write(chip, 0xA4, count & 0xff);
597 snd_es18xx_write(chip, 0xA5, count >> 8);
598
599#ifdef AVOID_POPS
600 udelay(100000);
601#endif
602
603 /* Set format */
604 snd_es18xx_write(chip, 0xB7,
605 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
606 snd_es18xx_write(chip, 0xB7, 0x90 |
607 ((runtime->channels == 1) ? 0x40 : 0x08) |
608 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
609 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
610
611 /* Set DMA controler */
612 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_READ | DMA_AUTOINIT);
613
614 return 0;
615}
616
Takashi Iwai8047c912005-11-17 14:41:22 +0100617static int snd_es18xx_capture_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 int cmd)
619{
Takashi Iwai8047c912005-11-17 14:41:22 +0100620 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 switch (cmd) {
623 case SNDRV_PCM_TRIGGER_START:
624 case SNDRV_PCM_TRIGGER_RESUME:
625 if (chip->active & ADC1)
626 return 0;
627 chip->active |= ADC1;
628 /* Start DMA */
629 snd_es18xx_write(chip, 0xB8, 0x0f);
630 break;
631 case SNDRV_PCM_TRIGGER_STOP:
632 case SNDRV_PCM_TRIGGER_SUSPEND:
633 if (!(chip->active & ADC1))
634 return 0;
635 chip->active &= ~ADC1;
636 /* Stop DMA */
637 snd_es18xx_write(chip, 0xB8, 0x00);
638 break;
639 default:
640 return -EINVAL;
641 }
642
643 return 0;
644}
645
Takashi Iwai8047c912005-11-17 14:41:22 +0100646static int snd_es18xx_playback2_prepare(struct snd_es18xx *chip,
647 struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Takashi Iwai8047c912005-11-17 14:41:22 +0100649 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 unsigned int size = snd_pcm_lib_buffer_bytes(substream);
651 unsigned int count = snd_pcm_lib_period_bytes(substream);
652
653 chip->dma1_size = size;
654
655 snd_es18xx_reset_fifo(chip);
656
657 /* Set stereo/mono */
658 snd_es18xx_bits(chip, 0xA8, 0x03, runtime->channels == 1 ? 0x02 : 0x01);
659
660 snd_es18xx_rate_set(chip, substream, DAC1);
661
662 /* Transfer Count Reload */
663 count = 0x10000 - count;
664 snd_es18xx_write(chip, 0xA4, count & 0xff);
665 snd_es18xx_write(chip, 0xA5, count >> 8);
666
667 /* Set format */
668 snd_es18xx_write(chip, 0xB6,
669 snd_pcm_format_unsigned(runtime->format) ? 0x80 : 0x00);
670 snd_es18xx_write(chip, 0xB7,
671 snd_pcm_format_unsigned(runtime->format) ? 0x51 : 0x71);
672 snd_es18xx_write(chip, 0xB7, 0x90 |
673 (runtime->channels == 1 ? 0x40 : 0x08) |
674 (snd_pcm_format_width(runtime->format) == 16 ? 0x04 : 0x00) |
675 (snd_pcm_format_unsigned(runtime->format) ? 0x00 : 0x20));
676
677 /* Set DMA controler */
678 snd_dma_program(chip->dma1, runtime->dma_addr, size, DMA_MODE_WRITE | DMA_AUTOINIT);
679
680 return 0;
681}
682
Takashi Iwai8047c912005-11-17 14:41:22 +0100683static int snd_es18xx_playback2_trigger(struct snd_es18xx *chip,
684 struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int cmd)
686{
687 switch (cmd) {
688 case SNDRV_PCM_TRIGGER_START:
689 case SNDRV_PCM_TRIGGER_RESUME:
690 if (chip->active & DAC1)
691 return 0;
692 chip->active |= DAC1;
693 /* Start DMA */
694 snd_es18xx_write(chip, 0xB8, 0x05);
695#ifdef AVOID_POPS
696 /* Avoid pops */
697 udelay(100000);
698 /* Enable Audio 1 */
699 snd_es18xx_dsp_command(chip, 0xD1);
700#endif
701 break;
702 case SNDRV_PCM_TRIGGER_STOP:
703 case SNDRV_PCM_TRIGGER_SUSPEND:
704 if (!(chip->active & DAC1))
705 return 0;
706 chip->active &= ~DAC1;
707 /* Stop DMA */
708 snd_es18xx_write(chip, 0xB8, 0x00);
709#ifdef AVOID_POPS
710 /* Avoid pops */
711 udelay(25000);
712 /* Disable Audio 1 */
713 snd_es18xx_dsp_command(chip, 0xD3);
714#endif
715 break;
716 default:
717 return -EINVAL;
718 }
719
720 return 0;
721}
722
Takashi Iwai8047c912005-11-17 14:41:22 +0100723static int snd_es18xx_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Takashi Iwai8047c912005-11-17 14:41:22 +0100725 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
727 return snd_es18xx_playback1_prepare(chip, substream);
728 else
729 return snd_es18xx_playback2_prepare(chip, substream);
730}
731
Takashi Iwai8047c912005-11-17 14:41:22 +0100732static int snd_es18xx_playback_trigger(struct snd_pcm_substream *substream,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int cmd)
734{
Takashi Iwai8047c912005-11-17 14:41:22 +0100735 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
737 return snd_es18xx_playback1_trigger(chip, substream, cmd);
738 else
739 return snd_es18xx_playback2_trigger(chip, substream, cmd);
740}
741
742static irqreturn_t snd_es18xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
743{
Takashi Iwai8047c912005-11-17 14:41:22 +0100744 struct snd_es18xx *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned char status;
746
747 if (chip->caps & ES18XX_CONTROL) {
748 /* Read Interrupt status */
749 status = inb(chip->ctrl_port + 6);
750 } else {
751 /* Read Interrupt status */
752 status = snd_es18xx_mixer_read(chip, 0x7f) >> 4;
753 }
754#if 0
755 else {
756 status = 0;
757 if (inb(chip->port + 0x0C) & 0x01)
758 status |= AUDIO1_IRQ;
759 if (snd_es18xx_mixer_read(chip, 0x7A) & 0x80)
760 status |= AUDIO2_IRQ;
761 if ((chip->caps & ES18XX_HWV) &&
762 snd_es18xx_mixer_read(chip, 0x64) & 0x10)
763 status |= HWV_IRQ;
764 }
765#endif
766
767 /* Audio 1 & Audio 2 */
768 if (status & AUDIO2_IRQ) {
769 if (chip->active & DAC2)
770 snd_pcm_period_elapsed(chip->playback_a_substream);
771 /* ack interrupt */
772 snd_es18xx_mixer_bits(chip, 0x7A, 0x80, 0x00);
773 }
774 if (status & AUDIO1_IRQ) {
775 /* ok.. capture is active */
776 if (chip->active & ADC1)
777 snd_pcm_period_elapsed(chip->capture_a_substream);
778 /* ok.. playback2 is active */
779 else if (chip->active & DAC1)
780 snd_pcm_period_elapsed(chip->playback_b_substream);
781 /* ack interrupt */
782 inb(chip->port + 0x0E);
783 }
784
785 /* MPU */
786 if ((status & MPU_IRQ) && chip->rmidi)
787 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
788
789 /* Hardware volume */
790 if (status & HWV_IRQ) {
791 int split = snd_es18xx_mixer_read(chip, 0x64) & 0x80;
792 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
793 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
794 if (!split) {
795 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
796 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
797 }
798 /* ack interrupt */
799 snd_es18xx_mixer_write(chip, 0x66, 0x00);
800 }
801 return IRQ_HANDLED;
802}
803
Takashi Iwai8047c912005-11-17 14:41:22 +0100804static snd_pcm_uframes_t snd_es18xx_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
Takashi Iwai8047c912005-11-17 14:41:22 +0100806 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int pos;
808
809 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
810 if (!(chip->active & DAC2))
811 return 0;
812 pos = snd_dma_pointer(chip->dma2, chip->dma2_size);
813 return pos >> chip->dma2_shift;
814 } else {
815 if (!(chip->active & DAC1))
816 return 0;
817 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
818 return pos >> chip->dma1_shift;
819 }
820}
821
Takashi Iwai8047c912005-11-17 14:41:22 +0100822static snd_pcm_uframes_t snd_es18xx_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Takashi Iwai8047c912005-11-17 14:41:22 +0100824 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 int pos;
826
827 if (!(chip->active & ADC1))
828 return 0;
829 pos = snd_dma_pointer(chip->dma1, chip->dma1_size);
830 return pos >> chip->dma1_shift;
831}
832
Takashi Iwai8047c912005-11-17 14:41:22 +0100833static struct snd_pcm_hardware snd_es18xx_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
835 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
836 SNDRV_PCM_INFO_RESUME |
837 SNDRV_PCM_INFO_MMAP_VALID),
838 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
839 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
840 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
841 .rate_min = 4000,
842 .rate_max = 48000,
843 .channels_min = 1,
844 .channels_max = 2,
845 .buffer_bytes_max = 65536,
846 .period_bytes_min = 64,
847 .period_bytes_max = 65536,
848 .periods_min = 1,
849 .periods_max = 1024,
850 .fifo_size = 0,
851};
852
Takashi Iwai8047c912005-11-17 14:41:22 +0100853static struct snd_pcm_hardware snd_es18xx_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
856 SNDRV_PCM_INFO_RESUME |
857 SNDRV_PCM_INFO_MMAP_VALID),
858 .formats = (SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S8 |
859 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE),
860 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
861 .rate_min = 4000,
862 .rate_max = 48000,
863 .channels_min = 1,
864 .channels_max = 2,
865 .buffer_bytes_max = 65536,
866 .period_bytes_min = 64,
867 .period_bytes_max = 65536,
868 .periods_min = 1,
869 .periods_max = 1024,
870 .fifo_size = 0,
871};
872
Takashi Iwai8047c912005-11-17 14:41:22 +0100873static int snd_es18xx_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Takashi Iwai8047c912005-11-17 14:41:22 +0100875 struct snd_pcm_runtime *runtime = substream->runtime;
876 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (substream->number == 0 && (chip->caps & ES18XX_PCM2)) {
879 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
880 chip->capture_a_substream &&
881 chip->capture_a_substream->runtime->channels != 1)
882 return -EAGAIN;
883 chip->playback_a_substream = substream;
884 } else if (substream->number <= 1) {
885 if (chip->capture_a_substream)
886 return -EAGAIN;
887 chip->playback_b_substream = substream;
888 } else {
889 snd_BUG();
890 return -EINVAL;
891 }
892 substream->runtime->hw = snd_es18xx_playback;
893 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
894 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
895 return 0;
896}
897
Takashi Iwai8047c912005-11-17 14:41:22 +0100898static int snd_es18xx_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Takashi Iwai8047c912005-11-17 14:41:22 +0100900 struct snd_pcm_runtime *runtime = substream->runtime;
901 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (chip->playback_b_substream)
904 return -EAGAIN;
905 if ((chip->caps & ES18XX_DUPLEX_MONO) &&
906 chip->playback_a_substream &&
907 chip->playback_a_substream->runtime->channels != 1)
908 return -EAGAIN;
909 chip->capture_a_substream = substream;
910 substream->runtime->hw = snd_es18xx_capture;
911 snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
912 (chip->caps & ES18XX_NEW_RATE) ? &new_hw_constraints_clocks : &old_hw_constraints_clocks);
913 return 0;
914}
915
Takashi Iwai8047c912005-11-17 14:41:22 +0100916static int snd_es18xx_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Takashi Iwai8047c912005-11-17 14:41:22 +0100918 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (substream->number == 0 && (chip->caps & ES18XX_PCM2))
921 chip->playback_a_substream = NULL;
922 else
923 chip->playback_b_substream = NULL;
924
925 snd_pcm_lib_free_pages(substream);
926 return 0;
927}
928
Takashi Iwai8047c912005-11-17 14:41:22 +0100929static int snd_es18xx_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
Takashi Iwai8047c912005-11-17 14:41:22 +0100931 struct snd_es18xx *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 chip->capture_a_substream = NULL;
934 snd_pcm_lib_free_pages(substream);
935 return 0;
936}
937
938/*
939 * MIXER part
940 */
941
Takashi Iwai8047c912005-11-17 14:41:22 +0100942static int snd_es18xx_info_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 static char *texts[8] = {
945 "Mic", "Mic Master", "CD", "AOUT",
946 "Mic1", "Mix", "Line", "Master"
947 };
948
949 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
950 uinfo->count = 1;
951 uinfo->value.enumerated.items = 8;
952 if (uinfo->value.enumerated.item > 7)
953 uinfo->value.enumerated.item = 7;
954 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
955 return 0;
956}
957
Takashi Iwai8047c912005-11-17 14:41:22 +0100958static int snd_es18xx_get_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Takashi Iwai8047c912005-11-17 14:41:22 +0100960 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 ucontrol->value.enumerated.item[0] = snd_es18xx_mixer_read(chip, 0x1c) & 0x07;
962 return 0;
963}
964
Takashi Iwai8047c912005-11-17 14:41:22 +0100965static int snd_es18xx_put_mux(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Takashi Iwai8047c912005-11-17 14:41:22 +0100967 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 unsigned char val = ucontrol->value.enumerated.item[0];
969
970 if (val > 7)
971 return -EINVAL;
972 return snd_es18xx_mixer_bits(chip, 0x1c, 0x07, val) != val;
973}
974
Takashi Iwai8047c912005-11-17 14:41:22 +0100975static int snd_es18xx_info_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
978 uinfo->count = 1;
979 uinfo->value.integer.min = 0;
980 uinfo->value.integer.max = 1;
981 return 0;
982}
983
Takashi Iwai8047c912005-11-17 14:41:22 +0100984static int snd_es18xx_get_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Takashi Iwai8047c912005-11-17 14:41:22 +0100986 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 unsigned char val = snd_es18xx_mixer_read(chip, 0x50);
988 ucontrol->value.integer.value[0] = !!(val & 8);
989 return 0;
990}
991
Takashi Iwai8047c912005-11-17 14:41:22 +0100992static int snd_es18xx_put_spatializer_enable(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
Takashi Iwai8047c912005-11-17 14:41:22 +0100994 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 unsigned char oval, nval;
996 int change;
997 nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
998 oval = snd_es18xx_mixer_read(chip, 0x50) & 0x0c;
999 change = nval != oval;
1000 if (change) {
1001 snd_es18xx_mixer_write(chip, 0x50, nval & ~0x04);
1002 snd_es18xx_mixer_write(chip, 0x50, nval);
1003 }
1004 return change;
1005}
1006
Takashi Iwai8047c912005-11-17 14:41:22 +01001007static int snd_es18xx_info_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1010 uinfo->count = 2;
1011 uinfo->value.integer.min = 0;
1012 uinfo->value.integer.max = 63;
1013 return 0;
1014}
1015
Takashi Iwai8047c912005-11-17 14:41:22 +01001016static int snd_es18xx_get_hw_volume(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Takashi Iwai8047c912005-11-17 14:41:22 +01001018 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 ucontrol->value.integer.value[0] = snd_es18xx_mixer_read(chip, 0x61) & 0x3f;
1020 ucontrol->value.integer.value[1] = snd_es18xx_mixer_read(chip, 0x63) & 0x3f;
1021 return 0;
1022}
1023
Takashi Iwai8047c912005-11-17 14:41:22 +01001024static int snd_es18xx_info_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1027 uinfo->count = 2;
1028 uinfo->value.integer.min = 0;
1029 uinfo->value.integer.max = 1;
1030 return 0;
1031}
1032
Takashi Iwai8047c912005-11-17 14:41:22 +01001033static int snd_es18xx_get_hw_switch(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Takashi Iwai8047c912005-11-17 14:41:22 +01001035 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 ucontrol->value.integer.value[0] = !(snd_es18xx_mixer_read(chip, 0x61) & 0x40);
1037 ucontrol->value.integer.value[1] = !(snd_es18xx_mixer_read(chip, 0x63) & 0x40);
1038 return 0;
1039}
1040
Takashi Iwai8047c912005-11-17 14:41:22 +01001041static void snd_es18xx_hwv_free(struct snd_kcontrol *kcontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Takashi Iwai8047c912005-11-17 14:41:22 +01001043 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 chip->master_volume = NULL;
1045 chip->master_switch = NULL;
1046 chip->hw_volume = NULL;
1047 chip->hw_switch = NULL;
1048}
1049
Takashi Iwai8047c912005-11-17 14:41:22 +01001050static int snd_es18xx_reg_bits(struct snd_es18xx *chip, unsigned char reg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 unsigned char mask, unsigned char val)
1052{
1053 if (reg < 0xa0)
1054 return snd_es18xx_mixer_bits(chip, reg, mask, val);
1055 else
1056 return snd_es18xx_bits(chip, reg, mask, val);
1057}
1058
Takashi Iwai8047c912005-11-17 14:41:22 +01001059static int snd_es18xx_reg_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 if (reg < 0xa0)
1062 return snd_es18xx_mixer_read(chip, reg);
1063 else
1064 return snd_es18xx_read(chip, reg);
1065}
1066
1067#define ES18XX_SINGLE(xname, xindex, reg, shift, mask, invert) \
1068{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1069 .info = snd_es18xx_info_single, \
1070 .get = snd_es18xx_get_single, .put = snd_es18xx_put_single, \
1071 .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1072
Takashi Iwai8047c912005-11-17 14:41:22 +01001073static int snd_es18xx_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 int mask = (kcontrol->private_value >> 16) & 0xff;
1076
1077 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1078 uinfo->count = 1;
1079 uinfo->value.integer.min = 0;
1080 uinfo->value.integer.max = mask;
1081 return 0;
1082}
1083
Takashi Iwai8047c912005-11-17 14:41:22 +01001084static int snd_es18xx_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Takashi Iwai8047c912005-11-17 14:41:22 +01001086 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int reg = kcontrol->private_value & 0xff;
1088 int shift = (kcontrol->private_value >> 8) & 0xff;
1089 int mask = (kcontrol->private_value >> 16) & 0xff;
1090 int invert = (kcontrol->private_value >> 24) & 0xff;
1091 int val;
1092
1093 val = snd_es18xx_reg_read(chip, reg);
1094 ucontrol->value.integer.value[0] = (val >> shift) & mask;
1095 if (invert)
1096 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1097 return 0;
1098}
1099
Takashi Iwai8047c912005-11-17 14:41:22 +01001100static int snd_es18xx_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
Takashi Iwai8047c912005-11-17 14:41:22 +01001102 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 int reg = kcontrol->private_value & 0xff;
1104 int shift = (kcontrol->private_value >> 8) & 0xff;
1105 int mask = (kcontrol->private_value >> 16) & 0xff;
1106 int invert = (kcontrol->private_value >> 24) & 0xff;
1107 unsigned char val;
1108
1109 val = (ucontrol->value.integer.value[0] & mask);
1110 if (invert)
1111 val = mask - val;
1112 mask <<= shift;
1113 val <<= shift;
1114 return snd_es18xx_reg_bits(chip, reg, mask, val) != val;
1115}
1116
1117#define ES18XX_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1118{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1119 .info = snd_es18xx_info_double, \
1120 .get = snd_es18xx_get_double, .put = snd_es18xx_put_double, \
1121 .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1122
Takashi Iwai8047c912005-11-17 14:41:22 +01001123static int snd_es18xx_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
1125 int mask = (kcontrol->private_value >> 24) & 0xff;
1126
1127 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1128 uinfo->count = 2;
1129 uinfo->value.integer.min = 0;
1130 uinfo->value.integer.max = mask;
1131 return 0;
1132}
1133
Takashi Iwai8047c912005-11-17 14:41:22 +01001134static int snd_es18xx_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Takashi Iwai8047c912005-11-17 14:41:22 +01001136 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int left_reg = kcontrol->private_value & 0xff;
1138 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1139 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1140 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1141 int mask = (kcontrol->private_value >> 24) & 0xff;
1142 int invert = (kcontrol->private_value >> 22) & 1;
1143 unsigned char left, right;
1144
1145 left = snd_es18xx_reg_read(chip, left_reg);
1146 if (left_reg != right_reg)
1147 right = snd_es18xx_reg_read(chip, right_reg);
1148 else
1149 right = left;
1150 ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1151 ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1152 if (invert) {
1153 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1154 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1155 }
1156 return 0;
1157}
1158
Takashi Iwai8047c912005-11-17 14:41:22 +01001159static int snd_es18xx_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Takashi Iwai8047c912005-11-17 14:41:22 +01001161 struct snd_es18xx *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 int left_reg = kcontrol->private_value & 0xff;
1163 int right_reg = (kcontrol->private_value >> 8) & 0xff;
1164 int shift_left = (kcontrol->private_value >> 16) & 0x07;
1165 int shift_right = (kcontrol->private_value >> 19) & 0x07;
1166 int mask = (kcontrol->private_value >> 24) & 0xff;
1167 int invert = (kcontrol->private_value >> 22) & 1;
1168 int change;
1169 unsigned char val1, val2, mask1, mask2;
1170
1171 val1 = ucontrol->value.integer.value[0] & mask;
1172 val2 = ucontrol->value.integer.value[1] & mask;
1173 if (invert) {
1174 val1 = mask - val1;
1175 val2 = mask - val2;
1176 }
1177 val1 <<= shift_left;
1178 val2 <<= shift_right;
1179 mask1 = mask << shift_left;
1180 mask2 = mask << shift_right;
1181 if (left_reg != right_reg) {
1182 change = 0;
1183 if (snd_es18xx_reg_bits(chip, left_reg, mask1, val1) != val1)
1184 change = 1;
1185 if (snd_es18xx_reg_bits(chip, right_reg, mask2, val2) != val2)
1186 change = 1;
1187 } else {
1188 change = (snd_es18xx_reg_bits(chip, left_reg, mask1 | mask2,
1189 val1 | val2) != (val1 | val2));
1190 }
1191 return change;
1192}
1193
Mark Salazarc1f6d412006-01-16 11:29:09 +01001194/* Mixer controls
1195 * These arrays contain setup data for mixer controls.
1196 *
1197 * The controls that are universal to all chipsets are fully initialized
1198 * here.
1199 */
Takashi Iwai8047c912005-11-17 14:41:22 +01001200static struct snd_kcontrol_new snd_es18xx_base_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201ES18XX_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1202ES18XX_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1203ES18XX_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1204ES18XX_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1205ES18XX_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206ES18XX_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1207ES18XX_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208ES18XX_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1209ES18XX_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1212 .name = "Capture Source",
1213 .info = snd_es18xx_info_mux,
1214 .get = snd_es18xx_get_mux,
1215 .put = snd_es18xx_put_mux,
1216}
1217};
1218
Takashi Iwai8047c912005-11-17 14:41:22 +01001219static struct snd_kcontrol_new snd_es18xx_recmix_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220ES18XX_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1221ES18XX_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1222ES18XX_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1223ES18XX_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224ES18XX_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1225ES18XX_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0)
1226};
1227
Mark Salazarc1f6d412006-01-16 11:29:09 +01001228/*
1229 * The chipset specific mixer controls
1230 */
1231static struct snd_kcontrol_new snd_es18xx_opt_speaker =
1232 ES18XX_SINGLE("PC Speaker Playback Volume", 0, 0x3c, 0, 7, 0);
1233
1234static struct snd_kcontrol_new snd_es18xx_opt_1869[] = {
1235ES18XX_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1236ES18XX_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1237ES18XX_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0)
1238};
1239
Takashi Iwai8047c912005-11-17 14:41:22 +01001240static struct snd_kcontrol_new snd_es18xx_pcm1_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241ES18XX_DOUBLE("PCM Playback Volume", 0, 0x14, 0x14, 4, 0, 15, 0),
1242};
1243
Takashi Iwai8047c912005-11-17 14:41:22 +01001244static struct snd_kcontrol_new snd_es18xx_pcm2_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245ES18XX_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1246ES18XX_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0)
1247};
1248
Takashi Iwai8047c912005-11-17 14:41:22 +01001249static struct snd_kcontrol_new snd_es18xx_spatializer_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250ES18XX_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1251{
1252 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1253 .name = "3D Control - Switch",
1254 .info = snd_es18xx_info_spatializer_enable,
1255 .get = snd_es18xx_get_spatializer_enable,
1256 .put = snd_es18xx_put_spatializer_enable,
1257}
1258};
1259
Takashi Iwai8047c912005-11-17 14:41:22 +01001260static struct snd_kcontrol_new snd_es18xx_micpre1_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0xa9, 2, 1, 0);
1262
Takashi Iwai8047c912005-11-17 14:41:22 +01001263static struct snd_kcontrol_new snd_es18xx_micpre2_control =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264ES18XX_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0);
1265
Takashi Iwai8047c912005-11-17 14:41:22 +01001266static struct snd_kcontrol_new snd_es18xx_hw_volume_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1269 .name = "Hardware Master Playback Volume",
1270 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1271 .info = snd_es18xx_info_hw_volume,
1272 .get = snd_es18xx_get_hw_volume,
1273},
1274{
1275 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1276 .name = "Hardware Master Playback Switch",
1277 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1278 .info = snd_es18xx_info_hw_switch,
1279 .get = snd_es18xx_get_hw_switch,
1280},
1281ES18XX_SINGLE("Hardware Master Volume Split", 0, 0x64, 7, 1, 0),
1282};
1283
1284#if 0
Takashi Iwai8047c912005-11-17 14:41:22 +01001285static int __devinit snd_es18xx_config_read(struct snd_es18xx *chip, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 int data;
1288 unsigned long flags;
1289 spin_lock_irqsave(&chip->ctrl_lock, flags);
1290 outb(reg, chip->ctrl_port);
1291 data = inb(chip->ctrl_port + 1);
1292 spin_unlock_irqrestore(&chip->ctrl_lock, flags);
1293 return data;
1294}
1295#endif
1296
Takashi Iwai8047c912005-11-17 14:41:22 +01001297static void __devinit snd_es18xx_config_write(struct snd_es18xx *chip,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 unsigned char reg, unsigned char data)
1299{
1300 /* No need for spinlocks, this function is used only in
1301 otherwise protected init code */
1302 outb(reg, chip->ctrl_port);
1303 outb(data, chip->ctrl_port + 1);
1304#ifdef REG_DEBUG
Takashi Iwai99b359b2005-10-20 18:26:44 +02001305 snd_printk(KERN_DEBUG "Config reg %02x set to %02x\n", reg, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306#endif
1307}
1308
Takashi Iwai8047c912005-11-17 14:41:22 +01001309static int __devinit snd_es18xx_initialize(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 int mask = 0;
1312
1313 /* enable extended mode */
1314 snd_es18xx_dsp_command(chip, 0xC6);
1315 /* Reset mixer registers */
1316 snd_es18xx_mixer_write(chip, 0x00, 0x00);
1317
1318 /* Audio 1 DMA demand mode (4 bytes/request) */
1319 snd_es18xx_write(chip, 0xB9, 2);
1320 if (chip->caps & ES18XX_CONTROL) {
1321 /* Hardware volume IRQ */
1322 snd_es18xx_config_write(chip, 0x27, chip->irq);
1323 if (chip->fm_port > 0 && chip->fm_port != SNDRV_AUTO_PORT) {
1324 /* FM I/O */
1325 snd_es18xx_config_write(chip, 0x62, chip->fm_port >> 8);
1326 snd_es18xx_config_write(chip, 0x63, chip->fm_port & 0xff);
1327 }
1328 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1329 /* MPU-401 I/O */
1330 snd_es18xx_config_write(chip, 0x64, chip->mpu_port >> 8);
1331 snd_es18xx_config_write(chip, 0x65, chip->mpu_port & 0xff);
1332 /* MPU-401 IRQ */
1333 snd_es18xx_config_write(chip, 0x28, chip->irq);
1334 }
1335 /* Audio1 IRQ */
1336 snd_es18xx_config_write(chip, 0x70, chip->irq);
1337 /* Audio2 IRQ */
1338 snd_es18xx_config_write(chip, 0x72, chip->irq);
1339 /* Audio1 DMA */
1340 snd_es18xx_config_write(chip, 0x74, chip->dma1);
1341 /* Audio2 DMA */
1342 snd_es18xx_config_write(chip, 0x75, chip->dma2);
1343
1344 /* Enable Audio 1 IRQ */
1345 snd_es18xx_write(chip, 0xB1, 0x50);
1346 /* Enable Audio 2 IRQ */
1347 snd_es18xx_mixer_write(chip, 0x7A, 0x40);
1348 /* Enable Audio 1 DMA */
1349 snd_es18xx_write(chip, 0xB2, 0x50);
1350 /* Enable MPU and hardware volume interrupt */
1351 snd_es18xx_mixer_write(chip, 0x64, 0x42);
1352 }
1353 else {
1354 int irqmask, dma1mask, dma2mask;
1355 switch (chip->irq) {
1356 case 2:
1357 case 9:
1358 irqmask = 0;
1359 break;
1360 case 5:
1361 irqmask = 1;
1362 break;
1363 case 7:
1364 irqmask = 2;
1365 break;
1366 case 10:
1367 irqmask = 3;
1368 break;
1369 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001370 snd_printk(KERN_ERR "invalid irq %d\n", chip->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return -ENODEV;
1372 }
1373 switch (chip->dma1) {
1374 case 0:
1375 dma1mask = 1;
1376 break;
1377 case 1:
1378 dma1mask = 2;
1379 break;
1380 case 3:
1381 dma1mask = 3;
1382 break;
1383 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001384 snd_printk(KERN_ERR "invalid dma1 %d\n", chip->dma1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return -ENODEV;
1386 }
1387 switch (chip->dma2) {
1388 case 0:
1389 dma2mask = 0;
1390 break;
1391 case 1:
1392 dma2mask = 1;
1393 break;
1394 case 3:
1395 dma2mask = 2;
1396 break;
1397 case 5:
1398 dma2mask = 3;
1399 break;
1400 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001401 snd_printk(KERN_ERR "invalid dma2 %d\n", chip->dma2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return -ENODEV;
1403 }
1404
1405 /* Enable and set Audio 1 IRQ */
1406 snd_es18xx_write(chip, 0xB1, 0x50 | (irqmask << 2));
1407 /* Enable and set Audio 1 DMA */
1408 snd_es18xx_write(chip, 0xB2, 0x50 | (dma1mask << 2));
1409 /* Set Audio 2 DMA */
1410 snd_es18xx_mixer_bits(chip, 0x7d, 0x07, 0x04 | dma2mask);
1411 /* Enable Audio 2 IRQ and DMA
1412 Set capture mixer input */
1413 snd_es18xx_mixer_write(chip, 0x7A, 0x68);
1414 /* Enable and set hardware volume interrupt */
1415 snd_es18xx_mixer_write(chip, 0x64, 0x06);
1416 if (chip->mpu_port > 0 && chip->mpu_port != SNDRV_AUTO_PORT) {
1417 /* MPU401 share irq with audio
1418 Joystick enabled
1419 FM enabled */
1420 snd_es18xx_mixer_write(chip, 0x40, 0x43 | (chip->mpu_port & 0xf0) >> 1);
1421 }
1422 snd_es18xx_mixer_write(chip, 0x7f, ((irqmask + 1) << 1) | 0x01);
1423 }
1424 if (chip->caps & ES18XX_NEW_RATE) {
1425 /* Change behaviour of register A1
1426 4x oversampling
1427 2nd channel DAC asynchronous */
1428 snd_es18xx_mixer_write(chip, 0x71, 0x32);
1429 }
1430 if (!(chip->caps & ES18XX_PCM2)) {
1431 /* Enable DMA FIFO */
1432 snd_es18xx_write(chip, 0xB7, 0x80);
1433 }
1434 if (chip->caps & ES18XX_SPATIALIZER) {
1435 /* Set spatializer parameters to recommended values */
1436 snd_es18xx_mixer_write(chip, 0x54, 0x8f);
1437 snd_es18xx_mixer_write(chip, 0x56, 0x95);
1438 snd_es18xx_mixer_write(chip, 0x58, 0x94);
1439 snd_es18xx_mixer_write(chip, 0x5a, 0x80);
1440 }
1441 /* Mute input source */
1442 if (chip->caps & ES18XX_MUTEREC)
1443 mask = 0x10;
1444 if (chip->caps & ES18XX_RECMIX)
1445 snd_es18xx_mixer_write(chip, 0x1c, 0x05 | mask);
1446 else {
1447 snd_es18xx_mixer_write(chip, 0x1c, 0x00 | mask);
1448 snd_es18xx_write(chip, 0xb4, 0x00);
1449 }
1450#ifndef AVOID_POPS
1451 /* Enable PCM output */
1452 snd_es18xx_dsp_command(chip, 0xD1);
1453#endif
1454
1455 return 0;
1456}
1457
Takashi Iwai8047c912005-11-17 14:41:22 +01001458static int __devinit snd_es18xx_identify(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459{
1460 int hi,lo;
1461
1462 /* reset */
1463 if (snd_es18xx_reset(chip) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +02001464 snd_printk(KERN_ERR "reset at 0x%lx failed!!!\n", chip->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 return -ENODEV;
1466 }
1467
1468 snd_es18xx_dsp_command(chip, 0xe7);
1469 hi = snd_es18xx_dsp_get_byte(chip);
1470 if (hi < 0) {
1471 return hi;
1472 }
1473 lo = snd_es18xx_dsp_get_byte(chip);
1474 if ((lo & 0xf0) != 0x80) {
1475 return -ENODEV;
1476 }
1477 if (hi == 0x48) {
1478 chip->version = 0x488;
1479 return 0;
1480 }
1481 if (hi != 0x68) {
1482 return -ENODEV;
1483 }
1484 if ((lo & 0x0f) < 8) {
1485 chip->version = 0x688;
1486 return 0;
1487 }
1488
1489 outb(0x40, chip->port + 0x04);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001490 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 hi = inb(chip->port + 0x05);
Mark Salazarc1f6d412006-01-16 11:29:09 +01001492 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 lo = inb(chip->port + 0x05);
1494 if (hi != lo) {
1495 chip->version = hi << 8 | lo;
1496 chip->ctrl_port = inb(chip->port + 0x05) << 8;
Mark Salazarc1f6d412006-01-16 11:29:09 +01001497 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 chip->ctrl_port += inb(chip->port + 0x05);
1499
1500 if ((chip->res_ctrl_port = request_region(chip->ctrl_port, 8, "ES18xx - CTRL")) == NULL) {
1501 snd_printk(KERN_ERR PFX "unable go grab port 0x%lx\n", chip->ctrl_port);
1502 return -EBUSY;
1503 }
1504
1505 return 0;
1506 }
1507
1508 /* If has Hardware volume */
1509 if (snd_es18xx_mixer_writable(chip, 0x64, 0x04)) {
1510 /* If has Audio2 */
1511 if (snd_es18xx_mixer_writable(chip, 0x70, 0x7f)) {
1512 /* If has volume count */
1513 if (snd_es18xx_mixer_writable(chip, 0x64, 0x20)) {
1514 chip->version = 0x1887;
1515 } else {
1516 chip->version = 0x1888;
1517 }
1518 } else {
1519 chip->version = 0x1788;
1520 }
1521 }
1522 else
1523 chip->version = 0x1688;
1524 return 0;
1525}
1526
Takashi Iwai8047c912005-11-17 14:41:22 +01001527static int __devinit snd_es18xx_probe(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 if (snd_es18xx_identify(chip) < 0) {
1530 snd_printk(KERN_ERR PFX "[0x%lx] ESS chip not found\n", chip->port);
1531 return -ENODEV;
1532 }
1533
1534 switch (chip->version) {
1535 case 0x1868:
1536 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_CONTROL | ES18XX_HWV;
1537 break;
1538 case 0x1869:
1539 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_MONO | ES18XX_MUTEREC | ES18XX_CONTROL | ES18XX_HWV;
1540 break;
1541 case 0x1878:
1542 chip->caps = ES18XX_DUPLEX_MONO | ES18XX_DUPLEX_SAME | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1543 break;
1544 case 0x1879:
1545 chip->caps = ES18XX_PCM2 | ES18XX_SPATIALIZER | ES18XX_RECMIX | ES18XX_NEW_RATE | ES18XX_AUXB | ES18XX_I2S | ES18XX_CONTROL | ES18XX_HWV;
1546 break;
1547 case 0x1887:
1548 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1549 break;
1550 case 0x1888:
1551 chip->caps = ES18XX_PCM2 | ES18XX_RECMIX | ES18XX_AUXB | ES18XX_DUPLEX_SAME | ES18XX_HWV;
1552 break;
1553 default:
Takashi Iwai99b359b2005-10-20 18:26:44 +02001554 snd_printk(KERN_ERR "[0x%lx] unsupported chip ES%x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 chip->port, chip->version);
1556 return -ENODEV;
1557 }
1558
1559 snd_printd("[0x%lx] ESS%x chip found\n", chip->port, chip->version);
1560
1561 if (chip->dma1 == chip->dma2)
1562 chip->caps &= ~(ES18XX_PCM2 | ES18XX_DUPLEX_SAME);
1563
1564 return snd_es18xx_initialize(chip);
1565}
1566
Takashi Iwai8047c912005-11-17 14:41:22 +01001567static struct snd_pcm_ops snd_es18xx_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 .open = snd_es18xx_playback_open,
1569 .close = snd_es18xx_playback_close,
1570 .ioctl = snd_pcm_lib_ioctl,
1571 .hw_params = snd_es18xx_playback_hw_params,
1572 .hw_free = snd_es18xx_pcm_hw_free,
1573 .prepare = snd_es18xx_playback_prepare,
1574 .trigger = snd_es18xx_playback_trigger,
1575 .pointer = snd_es18xx_playback_pointer,
1576};
1577
Takashi Iwai8047c912005-11-17 14:41:22 +01001578static struct snd_pcm_ops snd_es18xx_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 .open = snd_es18xx_capture_open,
1580 .close = snd_es18xx_capture_close,
1581 .ioctl = snd_pcm_lib_ioctl,
1582 .hw_params = snd_es18xx_capture_hw_params,
1583 .hw_free = snd_es18xx_pcm_hw_free,
1584 .prepare = snd_es18xx_capture_prepare,
1585 .trigger = snd_es18xx_capture_trigger,
1586 .pointer = snd_es18xx_capture_pointer,
1587};
1588
Takashi Iwai8047c912005-11-17 14:41:22 +01001589static int __devinit snd_es18xx_pcm(struct snd_es18xx *chip, int device, struct snd_pcm ** rpcm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Takashi Iwai8047c912005-11-17 14:41:22 +01001591 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 char str[16];
1593 int err;
1594
1595 if (rpcm)
1596 *rpcm = NULL;
1597 sprintf(str, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001598 if (chip->caps & ES18XX_PCM2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 err = snd_pcm_new(chip->card, str, device, 2, 1, &pcm);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001600 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 err = snd_pcm_new(chip->card, str, device, 1, 1, &pcm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (err < 0)
1603 return err;
1604
1605 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es18xx_playback_ops);
1606 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es18xx_capture_ops);
1607
1608 /* global setup */
1609 pcm->private_data = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 pcm->info_flags = 0;
1611 if (chip->caps & ES18XX_DUPLEX_SAME)
1612 pcm->info_flags |= SNDRV_PCM_INFO_JOINT_DUPLEX;
1613 if (! (chip->caps & ES18XX_PCM2))
1614 pcm->info_flags |= SNDRV_PCM_INFO_HALF_DUPLEX;
1615 sprintf(pcm->name, "ESS AudioDrive ES%x", chip->version);
1616 chip->pcm = pcm;
1617
1618 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1619 snd_dma_isa_data(),
1620 64*1024,
1621 chip->dma1 > 3 || chip->dma2 > 3 ? 128*1024 : 64*1024);
1622
1623 if (rpcm)
1624 *rpcm = pcm;
1625 return 0;
1626}
1627
1628/* Power Management support functions */
1629#ifdef CONFIG_PM
Takashi Iwai8047c912005-11-17 14:41:22 +01001630static int snd_es18xx_suspend(struct snd_card *card, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001632 struct snd_audiodrive *acard = card->private_data;
1633 struct snd_es18xx *chip = acard->chip;
1634
1635 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D3hot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 snd_pcm_suspend_all(chip->pcm);
1638
1639 /* power down */
1640 chip->pm_reg = (unsigned char)snd_es18xx_read(chip, ES18XX_PM);
1641 chip->pm_reg |= (ES18XX_PM_FM | ES18XX_PM_SUS);
1642 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg);
1643 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_SUS);
1644
1645 return 0;
1646}
1647
Takashi Iwai8047c912005-11-17 14:41:22 +01001648static int snd_es18xx_resume(struct snd_card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001650 struct snd_audiodrive *acard = card->private_data;
1651 struct snd_es18xx *chip = acard->chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /* restore PM register, we won't wake till (not 0x07) i/o activity though */
1654 snd_es18xx_write(chip, ES18XX_PM, chip->pm_reg ^= ES18XX_PM_FM);
1655
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01001656 snd_power_change_state(chip->card, SNDRV_CTL_POWER_D0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return 0;
1658}
1659#endif /* CONFIG_PM */
1660
Takashi Iwai8047c912005-11-17 14:41:22 +01001661static int snd_es18xx_free(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Takashi Iwaib1d57762005-10-10 11:56:31 +02001663 release_and_free_resource(chip->res_port);
1664 release_and_free_resource(chip->res_ctrl_port);
1665 release_and_free_resource(chip->res_mpu_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (chip->irq >= 0)
1667 free_irq(chip->irq, (void *) chip);
1668 if (chip->dma1 >= 0) {
1669 disable_dma(chip->dma1);
1670 free_dma(chip->dma1);
1671 }
1672 if (chip->dma2 >= 0 && chip->dma1 != chip->dma2) {
1673 disable_dma(chip->dma2);
1674 free_dma(chip->dma2);
1675 }
1676 kfree(chip);
1677 return 0;
1678}
1679
Takashi Iwai8047c912005-11-17 14:41:22 +01001680static int snd_es18xx_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Takashi Iwai8047c912005-11-17 14:41:22 +01001682 struct snd_es18xx *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return snd_es18xx_free(chip);
1684}
1685
Takashi Iwai8047c912005-11-17 14:41:22 +01001686static int __devinit snd_es18xx_new_device(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 unsigned long port,
1688 unsigned long mpu_port,
1689 unsigned long fm_port,
1690 int irq, int dma1, int dma2,
Takashi Iwai8047c912005-11-17 14:41:22 +01001691 struct snd_es18xx ** rchip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Takashi Iwai8047c912005-11-17 14:41:22 +01001693 struct snd_es18xx *chip;
1694 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 .dev_free = snd_es18xx_dev_free,
1696 };
1697 int err;
1698
1699 *rchip = NULL;
Takashi Iwai9e76a762005-09-09 14:21:17 +02001700 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (chip == NULL)
1702 return -ENOMEM;
1703 spin_lock_init(&chip->reg_lock);
1704 spin_lock_init(&chip->mixer_lock);
1705 spin_lock_init(&chip->ctrl_lock);
1706 chip->card = card;
1707 chip->port = port;
1708 chip->mpu_port = mpu_port;
1709 chip->fm_port = fm_port;
1710 chip->irq = -1;
1711 chip->dma1 = -1;
1712 chip->dma2 = -1;
1713 chip->audio2_vol = 0x00;
1714 chip->active = 0;
1715
1716 if ((chip->res_port = request_region(port, 16, "ES18xx")) == NULL) {
1717 snd_es18xx_free(chip);
1718 snd_printk(KERN_ERR PFX "unable to grap ports 0x%lx-0x%lx\n", port, port + 16 - 1);
1719 return -EBUSY;
1720 }
1721
1722 if (request_irq(irq, snd_es18xx_interrupt, SA_INTERRUPT, "ES18xx", (void *) chip)) {
1723 snd_es18xx_free(chip);
1724 snd_printk(KERN_ERR PFX "unable to grap IRQ %d\n", irq);
1725 return -EBUSY;
1726 }
1727 chip->irq = irq;
1728
1729 if (request_dma(dma1, "ES18xx DMA 1")) {
1730 snd_es18xx_free(chip);
1731 snd_printk(KERN_ERR PFX "unable to grap DMA1 %d\n", dma1);
1732 return -EBUSY;
1733 }
1734 chip->dma1 = dma1;
1735
1736 if (dma2 != dma1 && request_dma(dma2, "ES18xx DMA 2")) {
1737 snd_es18xx_free(chip);
1738 snd_printk(KERN_ERR PFX "unable to grap DMA2 %d\n", dma2);
1739 return -EBUSY;
1740 }
1741 chip->dma2 = dma2;
1742
1743 if (snd_es18xx_probe(chip) < 0) {
1744 snd_es18xx_free(chip);
1745 return -ENODEV;
1746 }
1747 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1748 snd_es18xx_free(chip);
1749 return err;
1750 }
1751 *rchip = chip;
1752 return 0;
1753}
1754
Takashi Iwai8047c912005-11-17 14:41:22 +01001755static int __devinit snd_es18xx_mixer(struct snd_es18xx *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Takashi Iwai8047c912005-11-17 14:41:22 +01001757 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 int err;
1759 unsigned int idx;
1760
1761 card = chip->card;
1762
1763 strcpy(card->mixername, chip->pcm->name);
1764
1765 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_base_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001766 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 kctl = snd_ctl_new1(&snd_es18xx_base_controls[idx], chip);
1768 if (chip->caps & ES18XX_HWV) {
1769 switch (idx) {
1770 case 0:
1771 chip->master_volume = kctl;
1772 kctl->private_free = snd_es18xx_hwv_free;
1773 break;
1774 case 1:
1775 chip->master_switch = kctl;
1776 kctl->private_free = snd_es18xx_hwv_free;
1777 break;
1778 }
1779 }
1780 if ((err = snd_ctl_add(card, kctl)) < 0)
1781 return err;
1782 }
1783 if (chip->caps & ES18XX_PCM2) {
1784 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm2_controls); idx++) {
1785 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm2_controls[idx], chip))) < 0)
1786 return err;
1787 }
1788 } else {
1789 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_pcm1_controls); idx++) {
1790 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_pcm1_controls[idx], chip))) < 0)
1791 return err;
1792 }
1793 }
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (chip->caps & ES18XX_RECMIX) {
1796 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_recmix_controls); idx++) {
1797 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_recmix_controls[idx], chip))) < 0)
1798 return err;
1799 }
1800 }
1801 switch (chip->version) {
1802 default:
1803 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre1_control, chip))) < 0)
1804 return err;
1805 break;
1806 case 0x1869:
1807 case 0x1879:
1808 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_micpre2_control, chip))) < 0)
1809 return err;
1810 break;
1811 }
1812 if (chip->caps & ES18XX_SPATIALIZER) {
1813 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_spatializer_controls); idx++) {
1814 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_spatializer_controls[idx], chip))) < 0)
1815 return err;
1816 }
1817 }
1818 if (chip->caps & ES18XX_HWV) {
1819 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_hw_volume_controls); idx++) {
Takashi Iwai8047c912005-11-17 14:41:22 +01001820 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 kctl = snd_ctl_new1(&snd_es18xx_hw_volume_controls[idx], chip);
1822 if (idx == 0)
1823 chip->hw_volume = kctl;
1824 else
1825 chip->hw_switch = kctl;
1826 kctl->private_free = snd_es18xx_hwv_free;
1827 if ((err = snd_ctl_add(card, kctl)) < 0)
1828 return err;
1829
1830 }
1831 }
Mark Salazarc1f6d412006-01-16 11:29:09 +01001832 /* finish initializing other chipset specific controls
1833 */
1834 if (chip->version != 0x1868) {
1835 err = snd_ctl_add(card, snd_ctl_new1(&snd_es18xx_opt_speaker,
1836 chip));
1837 if (err < 0)
1838 return err;
1839 }
1840 if (chip->version == 0x1869) {
1841 for (idx = 0; idx < ARRAY_SIZE(snd_es18xx_opt_1869); idx++) {
1842 err = snd_ctl_add(card,
1843 snd_ctl_new1(&snd_es18xx_opt_1869[idx],
1844 chip));
1845 if (err < 0)
1846 return err;
1847 }
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return 0;
1850}
1851
1852
1853/* Card level */
1854
1855MODULE_AUTHOR("Christian Fischbach <fishbach@pool.informatik.rwth-aachen.de>, Abramo Bagnara <abramo@alsa-project.org>");
1856MODULE_DESCRIPTION("ESS ES18xx AudioDrive");
1857MODULE_LICENSE("GPL");
1858MODULE_SUPPORTED_DEVICE("{{ESS,ES1868 PnP AudioDrive},"
1859 "{ESS,ES1869 PnP AudioDrive},"
1860 "{ESS,ES1878 PnP AudioDrive},"
1861 "{ESS,ES1879 PnP AudioDrive},"
1862 "{ESS,ES1887 PnP AudioDrive},"
1863 "{ESS,ES1888 PnP AudioDrive},"
1864 "{ESS,ES1887 AudioDrive},"
1865 "{ESS,ES1888 AudioDrive}}");
1866
1867static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
1868static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
1869static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_ISAPNP; /* Enable this card */
1870#ifdef CONFIG_PNP
1871static int isapnp[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 1};
1872#endif
1873static long port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT; /* 0x220,0x240,0x260,0x280 */
1874#ifndef CONFIG_PNP
1875static long mpu_port[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = -1};
1876#else
1877static long mpu_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1878#endif
1879static long fm_port[SNDRV_CARDS] = SNDRV_DEFAULT_PORT;
1880static int irq[SNDRV_CARDS] = SNDRV_DEFAULT_IRQ; /* 5,7,9,10 */
1881static int dma1[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1882static int dma2[SNDRV_CARDS] = SNDRV_DEFAULT_DMA; /* 0,1,3 */
1883
1884module_param_array(index, int, NULL, 0444);
1885MODULE_PARM_DESC(index, "Index value for ES18xx soundcard.");
1886module_param_array(id, charp, NULL, 0444);
1887MODULE_PARM_DESC(id, "ID string for ES18xx soundcard.");
1888module_param_array(enable, bool, NULL, 0444);
1889MODULE_PARM_DESC(enable, "Enable ES18xx soundcard.");
1890#ifdef CONFIG_PNP
1891module_param_array(isapnp, bool, NULL, 0444);
1892MODULE_PARM_DESC(isapnp, "PnP detection for specified soundcard.");
1893#endif
1894module_param_array(port, long, NULL, 0444);
1895MODULE_PARM_DESC(port, "Port # for ES18xx driver.");
1896module_param_array(mpu_port, long, NULL, 0444);
1897MODULE_PARM_DESC(mpu_port, "MPU-401 port # for ES18xx driver.");
1898module_param_array(fm_port, long, NULL, 0444);
1899MODULE_PARM_DESC(fm_port, "FM port # for ES18xx driver.");
1900module_param_array(irq, int, NULL, 0444);
1901MODULE_PARM_DESC(irq, "IRQ # for ES18xx driver.");
1902module_param_array(dma1, int, NULL, 0444);
1903MODULE_PARM_DESC(dma1, "DMA 1 # for ES18xx driver.");
1904module_param_array(dma2, int, NULL, 0444);
1905MODULE_PARM_DESC(dma2, "DMA 2 # for ES18xx driver.");
1906
Clemens Ladischf7a92752005-12-07 09:13:42 +01001907static struct platform_device *platform_devices[SNDRV_CARDS];
Clemens Ladischf7a92752005-12-07 09:13:42 +01001908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909#ifdef CONFIG_PNP
Takashi Iwai59b1b342006-01-04 15:06:44 +01001910static int pnp_registered;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912static struct pnp_card_device_id snd_audiodrive_pnpids[] = {
1913 /* ESS 1868 (integrated on Compaq dual P-Pro motherboard and Genius 18PnP 3D) */
1914 { .id = "ESS1868", .devs = { { "ESS1868" }, { "ESS0000" } } },
1915 /* ESS 1868 (integrated on Maxisound Cards) */
1916 { .id = "ESS1868", .devs = { { "ESS8601" }, { "ESS8600" } } },
1917 /* ESS 1868 (integrated on Maxisound Cards) */
1918 { .id = "ESS1868", .devs = { { "ESS8611" }, { "ESS8610" } } },
1919 /* ESS ES1869 Plug and Play AudioDrive */
1920 { .id = "ESS0003", .devs = { { "ESS1869" }, { "ESS0006" } } },
1921 /* ESS 1869 */
1922 { .id = "ESS1869", .devs = { { "ESS1869" }, { "ESS0006" } } },
1923 /* ESS 1878 */
1924 { .id = "ESS1878", .devs = { { "ESS1878" }, { "ESS0004" } } },
1925 /* ESS 1879 */
1926 { .id = "ESS1879", .devs = { { "ESS1879" }, { "ESS0009" } } },
1927 /* --- */
1928 { .id = "" } /* end */
1929};
1930
1931MODULE_DEVICE_TABLE(pnp_card, snd_audiodrive_pnpids);
1932
1933static int __devinit snd_audiodrive_pnp(int dev, struct snd_audiodrive *acard,
1934 struct pnp_card_link *card,
1935 const struct pnp_card_device_id *id)
1936{
1937 struct pnp_dev *pdev;
1938 struct pnp_resource_table * cfg = kmalloc(sizeof(struct pnp_resource_table), GFP_KERNEL);
1939 int err;
1940
1941 if (!cfg)
1942 return -ENOMEM;
1943 acard->dev = pnp_request_card_device(card, id->devs[0].id, NULL);
1944 if (acard->dev == NULL) {
1945 kfree(cfg);
1946 return -EBUSY;
1947 }
1948 acard->devc = pnp_request_card_device(card, id->devs[1].id, NULL);
1949 if (acard->devc == NULL) {
1950 kfree(cfg);
1951 return -EBUSY;
1952 }
1953 /* Control port initialization */
1954 err = pnp_activate_dev(acard->devc);
1955 if (err < 0) {
1956 snd_printk(KERN_ERR PFX "PnP control configure failure (out of resources?)\n");
1957 return -EAGAIN;
1958 }
1959 snd_printdd("pnp: port=0x%lx\n", pnp_port_start(acard->devc, 0));
1960 /* PnP initialization */
1961 pdev = acard->dev;
1962 pnp_init_resource_table(cfg);
1963 if (port[dev] != SNDRV_AUTO_PORT)
1964 pnp_resource_change(&cfg->port_resource[0], port[dev], 16);
1965 if (fm_port[dev] != SNDRV_AUTO_PORT)
1966 pnp_resource_change(&cfg->port_resource[1], fm_port[dev], 4);
1967 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1968 pnp_resource_change(&cfg->port_resource[2], mpu_port[dev], 2);
1969 if (dma1[dev] != SNDRV_AUTO_DMA)
1970 pnp_resource_change(&cfg->dma_resource[0], dma1[dev], 1);
1971 if (dma2[dev] != SNDRV_AUTO_DMA)
1972 pnp_resource_change(&cfg->dma_resource[1], dma2[dev], 1);
1973 if (irq[dev] != SNDRV_AUTO_IRQ)
1974 pnp_resource_change(&cfg->irq_resource[0], irq[dev], 1);
1975 err = pnp_manual_config_dev(pdev, cfg, 0);
1976 if (err < 0)
1977 snd_printk(KERN_ERR PFX "PnP manual resources are invalid, using auto config\n");
1978 err = pnp_activate_dev(pdev);
1979 if (err < 0) {
1980 snd_printk(KERN_ERR PFX "PnP configure failure (out of resources?)\n");
1981 kfree(cfg);
1982 return -EBUSY;
1983 }
1984 /* ok. hack using Vendor-Defined Card-Level registers */
1985 /* skip csn and logdev initialization - already done in isapnp_configure */
1986 if (pnp_device_is_isapnp(pdev)) {
1987 isapnp_cfg_begin(isapnp_card_number(pdev), isapnp_csn_number(pdev));
1988 isapnp_write_byte(0x27, pnp_irq(pdev, 0)); /* Hardware Volume IRQ Number */
1989 if (mpu_port[dev] != SNDRV_AUTO_PORT)
1990 isapnp_write_byte(0x28, pnp_irq(pdev, 0)); /* MPU-401 IRQ Number */
1991 isapnp_write_byte(0x72, pnp_irq(pdev, 0)); /* second IRQ */
1992 isapnp_cfg_end();
1993 } else {
1994 snd_printk(KERN_ERR PFX "unable to install ISA PnP hack, expect malfunction\n");
1995 }
1996 port[dev] = pnp_port_start(pdev, 0);
1997 fm_port[dev] = pnp_port_start(pdev, 1);
1998 mpu_port[dev] = pnp_port_start(pdev, 2);
1999 dma1[dev] = pnp_dma(pdev, 0);
2000 dma2[dev] = pnp_dma(pdev, 1);
2001 irq[dev] = pnp_irq(pdev, 0);
2002 snd_printdd("PnP ES18xx: port=0x%lx, fm port=0x%lx, mpu port=0x%lx\n", port[dev], fm_port[dev], mpu_port[dev]);
2003 snd_printdd("PnP ES18xx: dma1=%i, dma2=%i, irq=%i\n", dma1[dev], dma2[dev], irq[dev]);
2004 kfree(cfg);
2005 return 0;
2006}
2007#endif /* CONFIG_PNP */
2008
Takashi Iwai43bcd972005-09-05 17:19:20 +02002009#ifdef CONFIG_PNP
2010#define is_isapnp_selected(dev) isapnp[dev]
2011#else
2012#define is_isapnp_selected(dev) 0
2013#endif
2014
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002015static struct snd_card *snd_es18xx_card_new(int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002017 return snd_card_new(index[dev], id[dev], THIS_MODULE,
2018 sizeof(struct snd_audiodrive));
2019}
2020
2021static int __devinit snd_audiodrive_probe(struct snd_card *card, int dev)
2022{
2023 struct snd_audiodrive *acard = card->private_data;
Takashi Iwai8047c912005-11-17 14:41:22 +01002024 struct snd_es18xx *chip;
2025 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int err;
2027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if ((err = snd_es18xx_new_device(card,
2029 port[dev],
2030 mpu_port[dev],
2031 fm_port[dev],
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002032 irq[dev], dma1[dev], dma2[dev],
Takashi Iwai43bcd972005-09-05 17:19:20 +02002033 &chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002034 return err;
2035 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 sprintf(card->driver, "ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 sprintf(card->shortname, "ESS AudioDrive ES%x", chip->version);
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002040 if (dma1[dev] != dma2[dev])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 sprintf(card->longname, "%s at 0x%lx, irq %d, dma1 %d, dma2 %d",
2042 card->shortname,
2043 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002044 irq[dev], dma1[dev], dma2[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 else
2046 sprintf(card->longname, "%s at 0x%lx, irq %d, dma %d",
2047 card->shortname,
2048 chip->port,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002049 irq[dev], dma1[dev]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Takashi Iwai43bcd972005-09-05 17:19:20 +02002051 if ((err = snd_es18xx_pcm(chip, 0, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002052 return err;
Takashi Iwai43bcd972005-09-05 17:19:20 +02002053
2054 if ((err = snd_es18xx_mixer(chip)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002055 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 if (fm_port[dev] > 0 && fm_port[dev] != SNDRV_AUTO_PORT) {
2058 if (snd_opl3_create(card, chip->fm_port, chip->fm_port + 2, OPL3_HW_OPL3, 0, &opl3) < 0) {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002059 snd_printk(KERN_WARNING PFX "opl3 not detected at 0x%lx\n", chip->fm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } else {
Takashi Iwai43bcd972005-09-05 17:19:20 +02002061 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 }
2065
2066 if (mpu_port[dev] > 0 && mpu_port[dev] != SNDRV_AUTO_PORT) {
2067 if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_ES18XX,
2068 chip->mpu_port, 0,
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002069 irq[dev], 0,
Takashi Iwai43bcd972005-09-05 17:19:20 +02002070 &chip->rmidi)) < 0)
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002071 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002074 return snd_card_register(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002077static int __init snd_es18xx_nonpnp_probe1(int dev, struct platform_device *devptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002079 struct snd_card *card;
2080 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002082 card = snd_es18xx_card_new(dev);
2083 if (! card)
2084 return -ENOMEM;
2085 snd_card_set_dev(card, &devptr->dev);
2086 if ((err = snd_audiodrive_probe(card, dev)) < 0) {
2087 snd_card_free(card);
2088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002090 platform_set_drvdata(devptr, card);
2091 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002094static int __init snd_es18xx_nonpnp_probe(struct platform_device *pdev)
2095{
2096 int dev = pdev->id;
2097 int err;
2098 static int possible_irqs[] = {5, 9, 10, 7, 11, 12, -1};
2099 static int possible_dmas[] = {1, 0, 3, 5, -1};
2100
2101 if (irq[dev] == SNDRV_AUTO_IRQ) {
2102 if ((irq[dev] = snd_legacy_find_free_irq(possible_irqs)) < 0) {
2103 snd_printk(KERN_ERR PFX "unable to find a free IRQ\n");
2104 return -EBUSY;
2105 }
2106 }
2107 if (dma1[dev] == SNDRV_AUTO_DMA) {
2108 if ((dma1[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2109 snd_printk(KERN_ERR PFX "unable to find a free DMA1\n");
2110 return -EBUSY;
2111 }
2112 }
2113 if (dma2[dev] == SNDRV_AUTO_DMA) {
2114 if ((dma2[dev] = snd_legacy_find_free_dma(possible_dmas)) < 0) {
2115 snd_printk(KERN_ERR PFX "unable to find a free DMA2\n");
2116 return -EBUSY;
2117 }
2118 }
2119
2120 if (port[dev] != SNDRV_AUTO_PORT) {
2121 return snd_es18xx_nonpnp_probe1(dev, pdev);
2122 } else {
2123 static unsigned long possible_ports[] = {0x220, 0x240, 0x260, 0x280};
2124 int i;
2125 for (i = 0; i < ARRAY_SIZE(possible_ports); i++) {
2126 port[dev] = possible_ports[i];
2127 err = snd_es18xx_nonpnp_probe1(dev, pdev);
2128 if (! err)
2129 return 0;
2130 }
2131 return err;
2132 }
2133}
2134
2135static int __devexit snd_es18xx_nonpnp_remove(struct platform_device *devptr)
2136{
2137 snd_card_free(platform_get_drvdata(devptr));
2138 platform_set_drvdata(devptr, NULL);
2139 return 0;
2140}
2141
2142#ifdef CONFIG_PM
2143static int snd_es18xx_nonpnp_suspend(struct platform_device *dev, pm_message_t state)
2144{
2145 return snd_es18xx_suspend(platform_get_drvdata(dev), state);
2146}
2147
2148static int snd_es18xx_nonpnp_resume(struct platform_device *dev)
2149{
2150 return snd_es18xx_resume(platform_get_drvdata(dev));
2151}
2152#endif
2153
2154#define ES18XX_DRIVER "snd_es18xx"
2155
2156static struct platform_driver snd_es18xx_nonpnp_driver = {
2157 .probe = snd_es18xx_nonpnp_probe,
2158 .remove = __devexit_p(snd_es18xx_nonpnp_remove),
2159#ifdef CONFIG_PM
2160 .suspend = snd_es18xx_nonpnp_suspend,
2161 .resume = snd_es18xx_nonpnp_resume,
2162#endif
2163 .driver = {
2164 .name = ES18XX_DRIVER
2165 },
2166};
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169#ifdef CONFIG_PNP
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002170static int __devinit snd_audiodrive_pnp_detect(struct pnp_card_link *pcard,
2171 const struct pnp_card_device_id *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 static int dev;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002174 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 int res;
2176
2177 for ( ; dev < SNDRV_CARDS; dev++) {
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002178 if (enable[dev] && isapnp[dev])
2179 break;
2180 }
2181 if (dev >= SNDRV_CARDS)
2182 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002184 card = snd_es18xx_card_new(dev);
2185 if (! card)
2186 return -ENOMEM;
2187
2188 if ((res = snd_audiodrive_pnp(dev, card->private_data, pcard, pid)) < 0) {
2189 snd_card_free(card);
2190 return res;
2191 }
2192 snd_card_set_dev(card, &pcard->card->dev);
2193 if ((res = snd_audiodrive_probe(card, dev)) < 0) {
2194 snd_card_free(card);
2195 return res;
2196 }
2197
2198 pnp_set_card_drvdata(pcard, card);
2199 dev++;
2200 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203static void __devexit snd_audiodrive_pnp_remove(struct pnp_card_link * pcard)
2204{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002205 snd_card_free(pnp_get_card_drvdata(pcard));
2206 pnp_set_card_drvdata(pcard, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
2208
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002209#ifdef CONFIG_PM
2210static int snd_audiodrive_pnp_suspend(struct pnp_card_link *pcard, pm_message_t state)
2211{
2212 return snd_es18xx_suspend(pnp_get_card_drvdata(pcard), state);
2213}
2214
2215static int snd_audiodrive_pnp_resume(struct pnp_card_link *pcard)
2216{
2217 return snd_es18xx_resume(pnp_get_card_drvdata(pcard));
2218}
2219
2220#endif
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static struct pnp_card_driver es18xx_pnpc_driver = {
2223 .flags = PNP_DRIVER_RES_DISABLE,
2224 .name = "es18xx",
2225 .id_table = snd_audiodrive_pnpids,
2226 .probe = snd_audiodrive_pnp_detect,
2227 .remove = __devexit_p(snd_audiodrive_pnp_remove),
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002228#ifdef CONFIG_PM
2229 .suspend = snd_audiodrive_pnp_suspend,
2230 .resume = snd_audiodrive_pnp_resume,
2231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232};
2233#endif /* CONFIG_PNP */
2234
Clemens Ladischf7a92752005-12-07 09:13:42 +01002235static void __init_or_module snd_es18xx_unregister_all(void)
2236{
2237 int i;
2238
Takashi Iwai59b1b342006-01-04 15:06:44 +01002239#ifdef CONFIG_PNP
Clemens Ladischf7a92752005-12-07 09:13:42 +01002240 if (pnp_registered)
2241 pnp_unregister_card_driver(&es18xx_pnpc_driver);
Takashi Iwai59b1b342006-01-04 15:06:44 +01002242#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +01002243 for (i = 0; i < ARRAY_SIZE(platform_devices); ++i)
2244 platform_device_unregister(platform_devices[i]);
2245 platform_driver_unregister(&snd_es18xx_nonpnp_driver);
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248static int __init alsa_card_es18xx_init(void)
2249{
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002250 int i, err, cards = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002252 if ((err = platform_driver_register(&snd_es18xx_nonpnp_driver)) < 0)
2253 return err;
2254
2255 for (i = 0; i < SNDRV_CARDS && enable[i]; i++) {
2256 struct platform_device *device;
2257 if (is_isapnp_selected(i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 continue;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002259 device = platform_device_register_simple(ES18XX_DRIVER,
2260 i, NULL, 0);
2261 if (IS_ERR(device)) {
2262 err = PTR_ERR(device);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002263 goto errout;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002264 }
Clemens Ladischf7a92752005-12-07 09:13:42 +01002265 platform_devices[i] = device;
Takashi Iwaif7e0ba32005-11-17 17:12:07 +01002266 cards++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Takashi Iwai59b1b342006-01-04 15:06:44 +01002269#ifdef CONFIG_PNP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 i = pnp_register_card_driver(&es18xx_pnpc_driver);
Clemens Ladischf7a92752005-12-07 09:13:42 +01002271 if (i >= 0) {
2272 pnp_registered = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 cards += i;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002274 }
Takashi Iwai59b1b342006-01-04 15:06:44 +01002275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if(!cards) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278#ifdef MODULE
2279 snd_printk(KERN_ERR "ESS AudioDrive ES18xx soundcard not found or device busy\n");
2280#endif
Clemens Ladischf7a92752005-12-07 09:13:42 +01002281 err = -ENODEV;
2282 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284 return 0;
Clemens Ladischf7a92752005-12-07 09:13:42 +01002285
2286 errout:
2287 snd_es18xx_unregister_all();
2288 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
2291static void __exit alsa_card_es18xx_exit(void)
2292{
Clemens Ladischf7a92752005-12-07 09:13:42 +01002293 snd_es18xx_unregister_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296module_init(alsa_card_es18xx_init)
2297module_exit(alsa_card_es18xx_exit)