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