vserver 1.9.5.x5
[linux-2.6.git] / sound / pci / es1938.c
1 /*
2  *  Driver for ESS Solo-1 (ES1938, ES1946, ES1969) soundcard
3  *  Copyright (c) by Jaromir Koutek <miri@punknet.cz>,
4  *                   Jaroslav Kysela <perex@suse.cz>,
5  *                   Thomas Sailer <sailer@ife.ee.ethz.ch>,
6  *                   Abramo Bagnara <abramo@alsa-project.org>,
7  *                   Markus Gruber <gruber@eikon.tum.de>
8  * 
9  * Rewritten from sonicvibes.c source.
10  *
11  *  TODO:
12  *    Rewrite better spinlocks
13  *
14  *
15  *   This program is free software; you can redistribute it and/or modify
16  *   it under the terms of the GNU General Public License as published by
17  *   the Free Software Foundation; either version 2 of the License, or
18  *   (at your option) any later version.
19  *
20  *   This program is distributed in the hope that it will be useful,
21  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
22  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   GNU General Public License for more details.
24  *
25  *   You should have received a copy of the GNU General Public License
26  *   along with this program; if not, write to the Free Software
27  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
28  *
29  */
30
31 /*
32   NOTES:
33   - Capture data is written unaligned starting from dma_base + 1 so I need to
34     disable mmap and to add a copy callback.
35   - After several cycle of the following:
36     while : ; do arecord -d1 -f cd -t raw | aplay -f cd ; done
37     a "playback write error (DMA or IRQ trouble?)" may happen.
38     This is due to playback interrupts not generated.
39     I suspect a timing issue.
40   - Sometimes the interrupt handler is invoked wrongly during playback.
41     This generates some harmless "Unexpected hw_pointer: wrong interrupt
42     acknowledge".
43     I've seen that using small period sizes.
44     Reproducible with:
45     mpg123 test.mp3 &
46     hdparm -t -T /dev/hda
47 */
48
49
50 #include <sound/driver.h>
51 #include <linux/init.h>
52 #include <linux/interrupt.h>
53 #include <linux/pci.h>
54 #include <linux/slab.h>
55 #include <linux/gameport.h>
56 #include <linux/moduleparam.h>
57 #include <linux/delay.h>
58 #include <sound/core.h>
59 #include <sound/control.h>
60 #include <sound/pcm.h>
61 #include <sound/opl3.h>
62 #include <sound/mpu401.h>
63 #include <sound/initval.h>
64
65 #include <asm/io.h>
66
67 MODULE_AUTHOR("Jaromir Koutek <miri@punknet.cz>");
68 MODULE_DESCRIPTION("ESS Solo-1");
69 MODULE_LICENSE("GPL");
70 MODULE_SUPPORTED_DEVICE("{{ESS,ES1938},"
71                 "{ESS,ES1946},"
72                 "{ESS,ES1969},"
73                 "{TerraTec,128i PCI}}");
74
75 #ifndef PCI_VENDOR_ID_ESS
76 #define PCI_VENDOR_ID_ESS               0x125d
77 #endif
78 #ifndef PCI_DEVICE_ID_ESS_ES1938
79 #define PCI_DEVICE_ID_ESS_ES1938        0x1969
80 #endif
81
82 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
83 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
84 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
85
86 module_param_array(index, int, NULL, 0444);
87 MODULE_PARM_DESC(index, "Index value for ESS Solo-1 soundcard.");
88 module_param_array(id, charp, NULL, 0444);
89 MODULE_PARM_DESC(id, "ID string for ESS Solo-1 soundcard.");
90 module_param_array(enable, bool, NULL, 0444);
91 MODULE_PARM_DESC(enable, "Enable ESS Solo-1 soundcard.");
92
93 #define SLIO_REG(chip, x) ((chip)->io_port + ESSIO_REG_##x)
94
95 #define SLDM_REG(chip, x) ((chip)->ddma_port + ESSDM_REG_##x)
96
97 #define SLSB_REG(chip, x) ((chip)->sb_port + ESSSB_REG_##x)
98
99 #define SL_PCI_LEGACYCONTROL            0x40
100 #define SL_PCI_CONFIG                   0x50
101 #define SL_PCI_DDMACONTROL              0x60
102
103 #define ESSIO_REG_AUDIO2DMAADDR         0
104 #define ESSIO_REG_AUDIO2DMACOUNT        4
105 #define ESSIO_REG_AUDIO2MODE            6
106 #define ESSIO_REG_IRQCONTROL            7
107
108 #define ESSDM_REG_DMAADDR               0x00
109 #define ESSDM_REG_DMACOUNT              0x04
110 #define ESSDM_REG_DMACOMMAND            0x08
111 #define ESSDM_REG_DMASTATUS             0x08
112 #define ESSDM_REG_DMAMODE               0x0b
113 #define ESSDM_REG_DMACLEAR              0x0d
114 #define ESSDM_REG_DMAMASK               0x0f
115
116 #define ESSSB_REG_FMLOWADDR             0x00
117 #define ESSSB_REG_FMHIGHADDR            0x02
118 #define ESSSB_REG_MIXERADDR             0x04
119 #define ESSSB_REG_MIXERDATA             0x05
120
121 #define ESSSB_IREG_AUDIO1               0x14
122 #define ESSSB_IREG_MICMIX               0x1a
123 #define ESSSB_IREG_RECSRC               0x1c
124 #define ESSSB_IREG_MASTER               0x32
125 #define ESSSB_IREG_FM                   0x36
126 #define ESSSB_IREG_AUXACD               0x38
127 #define ESSSB_IREG_AUXB                 0x3a
128 #define ESSSB_IREG_PCSPEAKER            0x3c
129 #define ESSSB_IREG_LINE                 0x3e
130 #define ESSSB_IREG_SPATCONTROL          0x50
131 #define ESSSB_IREG_SPATLEVEL            0x52
132 #define ESSSB_IREG_MASTER_LEFT          0x60
133 #define ESSSB_IREG_MASTER_RIGHT         0x62
134 #define ESSSB_IREG_MPU401CONTROL        0x64
135 #define ESSSB_IREG_MICMIXRECORD         0x68
136 #define ESSSB_IREG_AUDIO2RECORD         0x69
137 #define ESSSB_IREG_AUXACDRECORD         0x6a
138 #define ESSSB_IREG_FMRECORD             0x6b
139 #define ESSSB_IREG_AUXBRECORD           0x6c
140 #define ESSSB_IREG_MONO                 0x6d
141 #define ESSSB_IREG_LINERECORD           0x6e
142 #define ESSSB_IREG_MONORECORD           0x6f
143 #define ESSSB_IREG_AUDIO2SAMPLE         0x70
144 #define ESSSB_IREG_AUDIO2MODE           0x71
145 #define ESSSB_IREG_AUDIO2FILTER         0x72
146 #define ESSSB_IREG_AUDIO2TCOUNTL        0x74
147 #define ESSSB_IREG_AUDIO2TCOUNTH        0x76
148 #define ESSSB_IREG_AUDIO2CONTROL1       0x78
149 #define ESSSB_IREG_AUDIO2CONTROL2       0x7a
150 #define ESSSB_IREG_AUDIO2               0x7c
151
152 #define ESSSB_REG_RESET                 0x06
153
154 #define ESSSB_REG_READDATA              0x0a
155 #define ESSSB_REG_WRITEDATA             0x0c
156 #define ESSSB_REG_READSTATUS            0x0c
157
158 #define ESSSB_REG_STATUS                0x0e
159
160 #define ESS_CMD_EXTSAMPLERATE           0xa1
161 #define ESS_CMD_FILTERDIV               0xa2
162 #define ESS_CMD_DMACNTRELOADL           0xa4
163 #define ESS_CMD_DMACNTRELOADH           0xa5
164 #define ESS_CMD_ANALOGCONTROL           0xa8
165 #define ESS_CMD_IRQCONTROL              0xb1
166 #define ESS_CMD_DRQCONTROL              0xb2
167 #define ESS_CMD_RECLEVEL                0xb4
168 #define ESS_CMD_SETFORMAT               0xb6
169 #define ESS_CMD_SETFORMAT2              0xb7
170 #define ESS_CMD_DMACONTROL              0xb8
171 #define ESS_CMD_DMATYPE                 0xb9
172 #define ESS_CMD_OFFSETLEFT              0xba    
173 #define ESS_CMD_OFFSETRIGHT             0xbb
174 #define ESS_CMD_READREG                 0xc0
175 #define ESS_CMD_ENABLEEXT               0xc6
176 #define ESS_CMD_PAUSEDMA                0xd0
177 #define ESS_CMD_ENABLEAUDIO1            0xd1
178 #define ESS_CMD_STOPAUDIO1              0xd3
179 #define ESS_CMD_AUDIO1STATUS            0xd8
180 #define ESS_CMD_CONTDMA                 0xd4
181 #define ESS_CMD_TESTIRQ                 0xf2
182
183 #define ESS_RECSRC_MIC          0
184 #define ESS_RECSRC_AUXACD       2
185 #define ESS_RECSRC_AUXB         5
186 #define ESS_RECSRC_LINE         6
187 #define ESS_RECSRC_NONE         7
188
189 #define DAC1 0x01
190 #define ADC1 0x02
191 #define DAC2 0x04
192
193 /*
194
195  */
196
197 typedef struct _snd_es1938 es1938_t;
198
199 #define SAVED_REG_SIZE  32 /* max. number of registers to save */
200
201 struct _snd_es1938 {
202         int irq;
203
204         unsigned long io_port;
205         unsigned long sb_port;
206         unsigned long vc_port;
207         unsigned long mpu_port;
208         unsigned long game_port;
209         unsigned long ddma_port;
210
211         unsigned char irqmask;
212         unsigned char revision;
213
214         snd_kcontrol_t *hw_volume;
215         snd_kcontrol_t *hw_switch;
216         snd_kcontrol_t *master_volume;
217         snd_kcontrol_t *master_switch;
218
219         struct pci_dev *pci;
220         snd_card_t *card;
221         snd_pcm_t *pcm;
222         snd_pcm_substream_t *capture_substream;
223         snd_pcm_substream_t *playback1_substream;
224         snd_pcm_substream_t *playback2_substream;
225         snd_kmixer_t *mixer;
226         snd_rawmidi_t *rmidi;
227
228         unsigned int dma1_size;
229         unsigned int dma2_size;
230         unsigned int dma1_start;
231         unsigned int dma2_start;
232         unsigned int dma1_shift;
233         unsigned int dma2_shift;
234         unsigned int active;
235
236         spinlock_t reg_lock;
237         spinlock_t mixer_lock;
238         snd_info_entry_t *proc_entry;
239
240 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
241         struct gameport gameport;
242 #endif
243 #ifdef CONFIG_PM
244         unsigned char saved_regs[SAVED_REG_SIZE];
245 #endif
246 };
247
248 static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs);
249
250 static struct pci_device_id snd_es1938_ids[] = {
251         { 0x125d, 0x1969, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* Solo-1 */
252         { 0, }
253 };
254
255 MODULE_DEVICE_TABLE(pci, snd_es1938_ids);
256
257 #define RESET_LOOP_TIMEOUT      0x10000
258 #define WRITE_LOOP_TIMEOUT      0x10000
259 #define GET_LOOP_TIMEOUT        0x01000
260
261 #undef REG_DEBUG
262 /* -----------------------------------------------------------------
263  * Write to a mixer register
264  * -----------------------------------------------------------------*/
265 static void snd_es1938_mixer_write(es1938_t *chip, unsigned char reg, unsigned char val)
266 {
267         unsigned long flags;
268         spin_lock_irqsave(&chip->mixer_lock, flags);
269         outb(reg, SLSB_REG(chip, MIXERADDR));
270         outb(val, SLSB_REG(chip, MIXERDATA));
271         spin_unlock_irqrestore(&chip->mixer_lock, flags);
272 #ifdef REG_DEBUG
273         snd_printk("Mixer reg %02x set to %02x\n", reg, val);
274 #endif
275 }
276
277 /* -----------------------------------------------------------------
278  * Read from a mixer register
279  * -----------------------------------------------------------------*/
280 static int snd_es1938_mixer_read(es1938_t *chip, unsigned char reg)
281 {
282         int data;
283         unsigned long flags;
284         spin_lock_irqsave(&chip->mixer_lock, flags);
285         outb(reg, SLSB_REG(chip, MIXERADDR));
286         data = inb(SLSB_REG(chip, MIXERDATA));
287         spin_unlock_irqrestore(&chip->mixer_lock, flags);
288 #ifdef REG_DEBUG
289         snd_printk("Mixer reg %02x now is %02x\n", reg, data);
290 #endif
291         return data;
292 }
293
294 /* -----------------------------------------------------------------
295  * Write to some bits of a mixer register (return old value)
296  * -----------------------------------------------------------------*/
297 static int snd_es1938_mixer_bits(es1938_t *chip, unsigned char reg, unsigned char mask, unsigned char val)
298 {
299         unsigned long flags;
300         unsigned char old, new, oval;
301         spin_lock_irqsave(&chip->mixer_lock, flags);
302         outb(reg, SLSB_REG(chip, MIXERADDR));
303         old = inb(SLSB_REG(chip, MIXERDATA));
304         oval = old & mask;
305         if (val != oval) {
306                 new = (old & ~mask) | (val & mask);
307                 outb(new, SLSB_REG(chip, MIXERDATA));
308 #ifdef REG_DEBUG
309                 snd_printk("Mixer reg %02x was %02x, set to %02x\n", reg, old, new);
310 #endif
311         }
312         spin_unlock_irqrestore(&chip->mixer_lock, flags);
313         return oval;
314 }
315
316 /* -----------------------------------------------------------------
317  * Write command to Controller Registers
318  * -----------------------------------------------------------------*/
319 static void snd_es1938_write_cmd(es1938_t *chip, unsigned char cmd)
320 {
321         int i;
322         unsigned char v;
323         for (i = 0; i < WRITE_LOOP_TIMEOUT; i++) {
324                 if (!(v = inb(SLSB_REG(chip, READSTATUS)) & 0x80)) {
325                         outb(cmd, SLSB_REG(chip, WRITEDATA));
326                         return;
327                 }
328         }
329         printk("snd_es1938_write_cmd timeout (0x02%x/0x02%x)\n", cmd, v);
330 }
331
332 /* -----------------------------------------------------------------
333  * Read the Read Data Buffer
334  * -----------------------------------------------------------------*/
335 static int snd_es1938_get_byte(es1938_t *chip)
336 {
337         int i;
338         unsigned char v;
339         for (i = GET_LOOP_TIMEOUT; i; i--)
340                 if ((v = inb(SLSB_REG(chip, STATUS))) & 0x80)
341                         return inb(SLSB_REG(chip, READDATA));
342         snd_printk("get_byte timeout: status 0x02%x\n", v);
343         return -ENODEV;
344 }
345
346 /* -----------------------------------------------------------------
347  * Write value cmd register
348  * -----------------------------------------------------------------*/
349 static void snd_es1938_write(es1938_t *chip, unsigned char reg, unsigned char val)
350 {
351         unsigned long flags;
352         spin_lock_irqsave(&chip->reg_lock, flags);
353         snd_es1938_write_cmd(chip, reg);
354         snd_es1938_write_cmd(chip, val);
355         spin_unlock_irqrestore(&chip->reg_lock, flags);
356 #ifdef REG_DEBUG
357         snd_printk("Reg %02x set to %02x\n", reg, val);
358 #endif
359 }
360
361 /* -----------------------------------------------------------------
362  * Read data from cmd register and return it
363  * -----------------------------------------------------------------*/
364 static unsigned char snd_es1938_read(es1938_t *chip, unsigned char reg)
365 {
366         unsigned char val;
367         unsigned long flags;
368         spin_lock_irqsave(&chip->reg_lock, flags);
369         snd_es1938_write_cmd(chip, ESS_CMD_READREG);
370         snd_es1938_write_cmd(chip, reg);
371         val = snd_es1938_get_byte(chip);
372         spin_unlock_irqrestore(&chip->reg_lock, flags);
373 #ifdef REG_DEBUG
374         snd_printk("Reg %02x now is %02x\n", reg, val);
375 #endif
376         return val;
377 }
378
379 /* -----------------------------------------------------------------
380  * Write data to cmd register and return old value
381  * -----------------------------------------------------------------*/
382 static int snd_es1938_bits(es1938_t *chip, unsigned char reg, unsigned char mask, unsigned char val)
383 {
384         unsigned long flags;
385         unsigned char old, new, oval;
386         spin_lock_irqsave(&chip->reg_lock, flags);
387         snd_es1938_write_cmd(chip, ESS_CMD_READREG);
388         snd_es1938_write_cmd(chip, reg);
389         old = snd_es1938_get_byte(chip);
390         oval = old & mask;
391         if (val != oval) {
392                 snd_es1938_write_cmd(chip, reg);
393                 new = (old & ~mask) | (val & mask);
394                 snd_es1938_write_cmd(chip, new);
395 #ifdef REG_DEBUG
396                 snd_printk("Reg %02x was %02x, set to %02x\n", reg, old, new);
397 #endif
398         }
399         spin_unlock_irqrestore(&chip->reg_lock, flags);
400         return oval;
401 }
402
403 /* --------------------------------------------------------------------
404  * Reset the chip
405  * --------------------------------------------------------------------*/
406 static void snd_es1938_reset(es1938_t *chip)
407 {
408         int i;
409
410         outb(3, SLSB_REG(chip, RESET));
411         inb(SLSB_REG(chip, RESET));
412         outb(0, SLSB_REG(chip, RESET));
413         for (i = 0; i < RESET_LOOP_TIMEOUT; i++) {
414                 if (inb(SLSB_REG(chip, STATUS)) & 0x80) {
415                         if (inb(SLSB_REG(chip, READDATA)) == 0xaa)
416                                 goto __next;
417                 }
418         }
419         snd_printk("ESS Solo-1 reset failed\n");
420
421      __next:
422         snd_es1938_write_cmd(chip, ESS_CMD_ENABLEEXT);
423
424         /* Demand transfer DMA: 4 bytes per DMA request */
425         snd_es1938_write(chip, ESS_CMD_DMATYPE, 2);
426
427         /* Change behaviour of register A1
428            4x oversampling
429            2nd channel DAC asynchronous */                                                      
430         snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2MODE, 0x32);
431         /* enable/select DMA channel and IRQ channel */
432         snd_es1938_bits(chip, ESS_CMD_IRQCONTROL, 0xf0, 0x50);
433         snd_es1938_bits(chip, ESS_CMD_DRQCONTROL, 0xf0, 0x50);
434         snd_es1938_write_cmd(chip, ESS_CMD_ENABLEAUDIO1);
435         /* Set spatializer parameters to recommended values */
436         snd_es1938_mixer_write(chip, 0x54, 0x8f);
437         snd_es1938_mixer_write(chip, 0x56, 0x95);
438         snd_es1938_mixer_write(chip, 0x58, 0x94);
439         snd_es1938_mixer_write(chip, 0x5a, 0x80);
440 }
441
442 /* --------------------------------------------------------------------
443  * Reset the FIFOs
444  * --------------------------------------------------------------------*/
445 static void snd_es1938_reset_fifo(es1938_t *chip)
446 {
447         outb(2, SLSB_REG(chip, RESET));
448         outb(0, SLSB_REG(chip, RESET));
449 }
450
451 static ratnum_t clocks[2] = {
452         {
453                 .num = 793800,
454                 .den_min = 1,
455                 .den_max = 128,
456                 .den_step = 1,
457         },
458         {
459                 .num = 768000,
460                 .den_min = 1,
461                 .den_max = 128,
462                 .den_step = 1,
463         }
464 };
465
466 static snd_pcm_hw_constraint_ratnums_t hw_constraints_clocks = {
467         .nrats = 2,
468         .rats = clocks,
469 };
470
471
472 static void snd_es1938_rate_set(es1938_t *chip, 
473                                 snd_pcm_substream_t *substream,
474                                 int mode)
475 {
476         unsigned int bits, div0;
477         snd_pcm_runtime_t *runtime = substream->runtime;
478         if (runtime->rate_num == clocks[0].num)
479                 bits = 128 - runtime->rate_den;
480         else
481                 bits = 256 - runtime->rate_den;
482
483         /* set filter register */
484         div0 = 256 - 7160000*20/(8*82*runtime->rate);
485                 
486         if (mode == DAC2) {
487                 snd_es1938_mixer_write(chip, 0x70, bits);
488                 snd_es1938_mixer_write(chip, 0x72, div0);
489         } else {
490                 snd_es1938_write(chip, 0xA1, bits);
491                 snd_es1938_write(chip, 0xA2, div0);
492         }
493 }
494
495 /* --------------------------------------------------------------------
496  * Configure Solo1 builtin DMA Controller
497  * --------------------------------------------------------------------*/
498
499 static void snd_es1938_playback1_setdma(es1938_t *chip)
500 {
501         outb(0x00, SLIO_REG(chip, AUDIO2MODE));
502         outl(chip->dma2_start, SLIO_REG(chip, AUDIO2DMAADDR));
503         outw(0, SLIO_REG(chip, AUDIO2DMACOUNT));
504         outw(chip->dma2_size, SLIO_REG(chip, AUDIO2DMACOUNT));
505 }
506
507 static void snd_es1938_playback2_setdma(es1938_t *chip)
508 {
509         /* Enable DMA controller */
510         outb(0xc4, SLDM_REG(chip, DMACOMMAND));
511         /* 1. Master reset */
512         outb(0, SLDM_REG(chip, DMACLEAR));
513         /* 2. Mask DMA */
514         outb(1, SLDM_REG(chip, DMAMASK));
515         outb(0x18, SLDM_REG(chip, DMAMODE));
516         outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
517         outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
518         /* 3. Unmask DMA */
519         outb(0, SLDM_REG(chip, DMAMASK));
520 }
521
522 static void snd_es1938_capture_setdma(es1938_t *chip)
523 {
524         /* Enable DMA controller */
525         outb(0xc4, SLDM_REG(chip, DMACOMMAND));
526         /* 1. Master reset */
527         outb(0, SLDM_REG(chip, DMACLEAR));
528         /* 2. Mask DMA */
529         outb(1, SLDM_REG(chip, DMAMASK));
530         outb(0x14, SLDM_REG(chip, DMAMODE));
531         outl(chip->dma1_start, SLDM_REG(chip, DMAADDR));
532         outw(chip->dma1_size - 1, SLDM_REG(chip, DMACOUNT));
533         /* 3. Unmask DMA */
534         outb(0, SLDM_REG(chip, DMAMASK));
535 }
536
537 /* ----------------------------------------------------------------------
538  *
539  *                           *** PCM part ***
540  */
541
542 static int snd_es1938_capture_trigger(snd_pcm_substream_t * substream,
543                                       int cmd)
544 {
545         es1938_t *chip = snd_pcm_substream_chip(substream);
546         int val;
547         switch (cmd) {
548         case SNDRV_PCM_TRIGGER_START:
549                 val = 0x0f;
550                 chip->active |= ADC1;
551                 break;
552         case SNDRV_PCM_TRIGGER_STOP:
553                 val = 0x00;
554                 chip->active &= ~ADC1;
555                 break;
556         default:
557                 return -EINVAL;
558         }
559         snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
560         return 0;
561 }
562
563 static int snd_es1938_playback1_trigger(snd_pcm_substream_t * substream,
564                                         int cmd)
565 {
566         es1938_t *chip = snd_pcm_substream_chip(substream);
567         switch (cmd) {
568         case SNDRV_PCM_TRIGGER_START:
569                 /* According to the documentation this should be:
570                    0x13 but that value may randomly swap stereo channels */
571                 snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x92);
572                 udelay(10);
573                 snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0x93);
574                 /* This two stage init gives the FIFO -> DAC connection time to
575                  * settle before first data from DMA flows in.  This should ensure
576                  * no swapping of stereo channels.  Report a bug if otherwise :-) */
577                 outb(0x0a, SLIO_REG(chip, AUDIO2MODE));
578                 chip->active |= DAC2;
579                 break;
580         case SNDRV_PCM_TRIGGER_STOP:
581                 outb(0, SLIO_REG(chip, AUDIO2MODE));
582                 snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL1, 0);
583                 chip->active &= ~DAC2;
584                 break;
585         default:
586                 return -EINVAL;
587         }
588         return 0;
589 }
590
591 static int snd_es1938_playback2_trigger(snd_pcm_substream_t * substream,
592                                         int cmd)
593 {
594         es1938_t *chip = snd_pcm_substream_chip(substream);
595         int val;
596         switch (cmd) {
597         case SNDRV_PCM_TRIGGER_START:
598                 val = 5;
599                 chip->active |= DAC1;
600                 break;
601         case SNDRV_PCM_TRIGGER_STOP:
602                 val = 0;
603                 chip->active &= ~DAC1;
604                 break;
605         default:
606                 return -EINVAL;
607         }
608         snd_es1938_write(chip, ESS_CMD_DMACONTROL, val);
609         return 0;
610 }
611
612 static int snd_es1938_playback_trigger(snd_pcm_substream_t *substream,
613                                        int cmd)
614 {
615         switch (substream->number) {
616         case 0:
617                 return snd_es1938_playback1_trigger(substream, cmd);
618         case 1:
619                 return snd_es1938_playback2_trigger(substream, cmd);
620         }
621         snd_BUG();
622         return -EINVAL;
623 }
624
625 /* --------------------------------------------------------------------
626  * First channel for Extended Mode Audio 1 ADC Operation
627  * --------------------------------------------------------------------*/
628 static int snd_es1938_capture_prepare(snd_pcm_substream_t * substream)
629 {
630         es1938_t *chip = snd_pcm_substream_chip(substream);
631         snd_pcm_runtime_t *runtime = substream->runtime;
632         int u, is8, mono;
633         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
634         unsigned int count = snd_pcm_lib_period_bytes(substream);
635
636         chip->dma1_size = size;
637         chip->dma1_start = runtime->dma_addr;
638
639         mono = (runtime->channels > 1) ? 0 : 1;
640         is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
641         u = snd_pcm_format_unsigned(runtime->format);
642
643         chip->dma1_shift = 2 - mono - is8;
644
645         snd_es1938_reset_fifo(chip);
646         
647         /* program type */
648         snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
649
650         /* set clock and counters */
651         snd_es1938_rate_set(chip, substream, ADC1);
652
653         count = 0x10000 - count;
654         snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
655         snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
656
657         /* initialize and configure ADC */
658         snd_es1938_write(chip, ESS_CMD_SETFORMAT2, u ? 0x51 : 0x71);
659         snd_es1938_write(chip, ESS_CMD_SETFORMAT2, 0x90 | 
660                        (u ? 0x00 : 0x20) | 
661                        (is8 ? 0x00 : 0x04) | 
662                        (mono ? 0x40 : 0x08));
663
664         //      snd_es1938_reset_fifo(chip);    
665
666         /* 11. configure system interrupt controller and DMA controller */
667         snd_es1938_capture_setdma(chip);
668
669         return 0;
670 }
671
672
673 /* ------------------------------------------------------------------------------
674  * Second Audio channel DAC Operation
675  * ------------------------------------------------------------------------------*/
676 static int snd_es1938_playback1_prepare(snd_pcm_substream_t * substream)
677 {
678         es1938_t *chip = snd_pcm_substream_chip(substream);
679         snd_pcm_runtime_t *runtime = substream->runtime;
680         int u, is8, mono;
681         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
682         unsigned int count = snd_pcm_lib_period_bytes(substream);
683
684         chip->dma2_size = size;
685         chip->dma2_start = runtime->dma_addr;
686
687         mono = (runtime->channels > 1) ? 0 : 1;
688         is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
689         u = snd_pcm_format_unsigned(runtime->format);
690
691         chip->dma2_shift = 2 - mono - is8;
692
693         snd_es1938_reset_fifo(chip);
694
695         /* set clock and counters */
696         snd_es1938_rate_set(chip, substream, DAC2);
697
698         count >>= 1;
699         count = 0x10000 - count;
700         snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTL, count & 0xff);
701         snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2TCOUNTH, count >> 8);
702
703         /* initialize and configure Audio 2 DAC */
704         snd_es1938_mixer_write(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x40 | (u ? 0 : 4) | (mono ? 0 : 2) | (is8 ? 0 : 1));
705
706         /* program DMA */
707         snd_es1938_playback1_setdma(chip);
708         
709         return 0;
710 }
711
712 static int snd_es1938_playback2_prepare(snd_pcm_substream_t * substream)
713 {
714         es1938_t *chip = snd_pcm_substream_chip(substream);
715         snd_pcm_runtime_t *runtime = substream->runtime;
716         int u, is8, mono;
717         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
718         unsigned int count = snd_pcm_lib_period_bytes(substream);
719
720         chip->dma1_size = size;
721         chip->dma1_start = runtime->dma_addr;
722
723         mono = (runtime->channels > 1) ? 0 : 1;
724         is8 = snd_pcm_format_width(runtime->format) == 16 ? 0 : 1;
725         u = snd_pcm_format_unsigned(runtime->format);
726
727         chip->dma1_shift = 2 - mono - is8;
728
729         count = 0x10000 - count;
730  
731         /* reset */
732         snd_es1938_reset_fifo(chip);
733         
734         snd_es1938_bits(chip, ESS_CMD_ANALOGCONTROL, 0x03, (mono ? 2 : 1));
735
736         /* set clock and counters */
737         snd_es1938_rate_set(chip, substream, DAC1);
738         snd_es1938_write(chip, ESS_CMD_DMACNTRELOADL, count & 0xff);
739         snd_es1938_write(chip, ESS_CMD_DMACNTRELOADH, count >> 8);
740
741         /* initialized and configure DAC */
742         snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x80 : 0x00);
743         snd_es1938_write(chip, ESS_CMD_SETFORMAT, u ? 0x51 : 0x71);
744         snd_es1938_write(chip, ESS_CMD_SETFORMAT2, 
745                          0x90 | (mono ? 0x40 : 0x08) |
746                          (is8 ? 0x00 : 0x04) | (u ? 0x00 : 0x20));
747
748         /* program DMA */
749         snd_es1938_playback2_setdma(chip);
750         
751         return 0;
752 }
753
754 static int snd_es1938_playback_prepare(snd_pcm_substream_t *substream)
755 {
756         switch (substream->number) {
757         case 0:
758                 return snd_es1938_playback1_prepare(substream);
759         case 1:
760                 return snd_es1938_playback2_prepare(substream);
761         }
762         snd_BUG();
763         return -EINVAL;
764 }
765
766 static snd_pcm_uframes_t snd_es1938_capture_pointer(snd_pcm_substream_t * substream)
767 {
768         es1938_t *chip = snd_pcm_substream_chip(substream);
769         size_t ptr;
770         size_t old, new;
771 #if 1
772         /* This stuff is *needed*, don't ask why - AB */
773         old = inw(SLDM_REG(chip, DMACOUNT));
774         while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
775                 old = new;
776         ptr = chip->dma1_size - 1 - new;
777 #else
778         ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
779 #endif
780         return ptr >> chip->dma1_shift;
781 }
782
783 static snd_pcm_uframes_t snd_es1938_playback1_pointer(snd_pcm_substream_t * substream)
784 {
785         es1938_t *chip = snd_pcm_substream_chip(substream);
786         size_t ptr;
787 #if 1
788         ptr = chip->dma2_size - inw(SLIO_REG(chip, AUDIO2DMACOUNT));
789 #else
790         ptr = inl(SLIO_REG(chip, AUDIO2DMAADDR)) - chip->dma2_start;
791 #endif
792         return ptr >> chip->dma2_shift;
793 }
794
795 static snd_pcm_uframes_t snd_es1938_playback2_pointer(snd_pcm_substream_t * substream)
796 {
797         es1938_t *chip = snd_pcm_substream_chip(substream);
798         size_t ptr;
799         size_t old, new;
800 #if 1
801         /* This stuff is *needed*, don't ask why - AB */
802         old = inw(SLDM_REG(chip, DMACOUNT));
803         while ((new = inw(SLDM_REG(chip, DMACOUNT))) != old)
804                 old = new;
805         ptr = chip->dma1_size - 1 - new;
806 #else
807         ptr = inl(SLDM_REG(chip, DMAADDR)) - chip->dma1_start;
808 #endif
809         return ptr >> chip->dma1_shift;
810 }
811
812 static snd_pcm_uframes_t snd_es1938_playback_pointer(snd_pcm_substream_t *substream)
813 {
814         switch (substream->number) {
815         case 0:
816                 return snd_es1938_playback1_pointer(substream);
817         case 1:
818                 return snd_es1938_playback2_pointer(substream);
819         }
820         snd_BUG();
821         return -EINVAL;
822 }
823
824 static int snd_es1938_capture_copy(snd_pcm_substream_t *substream,
825                                    int channel,
826                                    snd_pcm_uframes_t pos,
827                                    void __user *dst,
828                                    snd_pcm_uframes_t count)
829 {
830         snd_pcm_runtime_t *runtime = substream->runtime;
831         es1938_t *chip = snd_pcm_substream_chip(substream);
832         pos <<= chip->dma1_shift;
833         count <<= chip->dma1_shift;
834         snd_assert(pos + count <= chip->dma1_size, return -EINVAL);
835         if (pos + count < chip->dma1_size) {
836                 if (copy_to_user(dst, runtime->dma_area + pos + 1, count))
837                         return -EFAULT;
838         } else {
839                 if (copy_to_user(dst, runtime->dma_area + pos + 1, count - 1))
840                         return -EFAULT;
841                 if (put_user(runtime->dma_area[0], ((unsigned char __user *)dst) + count - 1))
842                         return -EFAULT;
843         }
844         return 0;
845 }
846
847 /*
848  * buffer management
849  */
850 static int snd_es1938_pcm_hw_params(snd_pcm_substream_t *substream,
851                                     snd_pcm_hw_params_t * hw_params)
852
853 {
854         int err;
855
856         if ((err = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params))) < 0)
857                 return err;
858         return 0;
859 }
860
861 static int snd_es1938_pcm_hw_free(snd_pcm_substream_t *substream)
862 {
863         return snd_pcm_lib_free_pages(substream);
864 }
865
866 /* ----------------------------------------------------------------------
867  * Audio1 Capture (ADC)
868  * ----------------------------------------------------------------------*/
869 static snd_pcm_hardware_t snd_es1938_capture =
870 {
871         .info =                 (SNDRV_PCM_INFO_INTERLEAVED |
872                                 SNDRV_PCM_INFO_BLOCK_TRANSFER),
873         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE,
874         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
875         .rate_min =             6000,
876         .rate_max =             48000,
877         .channels_min =         1,
878         .channels_max =         2,
879         .buffer_bytes_max =     0x8000,       /* DMA controller screws on higher values */
880         .period_bytes_min =     64,
881         .period_bytes_max =     0x8000,
882         .periods_min =          1,
883         .periods_max =          1024,
884         .fifo_size =            256,
885 };
886
887 /* -----------------------------------------------------------------------
888  * Audio2 Playback (DAC)
889  * -----------------------------------------------------------------------*/
890 static snd_pcm_hardware_t snd_es1938_playback =
891 {
892         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
893                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
894                                  SNDRV_PCM_INFO_MMAP_VALID),
895         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U16_LE,
896         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
897         .rate_min =             6000,
898         .rate_max =             48000,
899         .channels_min =         1,
900         .channels_max =         2,
901         .buffer_bytes_max =     0x8000,       /* DMA controller screws on higher values */
902         .period_bytes_min =     64,
903         .period_bytes_max =     0x8000,
904         .periods_min =          1,
905         .periods_max =          1024,
906         .fifo_size =            256,
907 };
908
909 static int snd_es1938_capture_open(snd_pcm_substream_t * substream)
910 {
911         es1938_t *chip = snd_pcm_substream_chip(substream);
912         snd_pcm_runtime_t *runtime = substream->runtime;
913
914         if (chip->playback2_substream)
915                 return -EAGAIN;
916         chip->capture_substream = substream;
917         runtime->hw = snd_es1938_capture;
918         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
919                                       &hw_constraints_clocks);
920         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
921         return 0;
922 }
923
924 static int snd_es1938_playback_open(snd_pcm_substream_t * substream)
925 {
926         es1938_t *chip = snd_pcm_substream_chip(substream);
927         snd_pcm_runtime_t *runtime = substream->runtime;
928
929         switch (substream->number) {
930         case 0:
931                 chip->playback1_substream = substream;
932                 break;
933         case 1:
934                 if (chip->capture_substream)
935                         return -EAGAIN;
936                 chip->playback2_substream = substream;
937                 break;
938         default:
939                 snd_BUG();
940                 return -EINVAL;
941         }
942         runtime->hw = snd_es1938_playback;
943         snd_pcm_hw_constraint_ratnums(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
944                                       &hw_constraints_clocks);
945         snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 0, 0xff00);
946         return 0;
947 }
948
949 static int snd_es1938_capture_close(snd_pcm_substream_t * substream)
950 {
951         es1938_t *chip = snd_pcm_substream_chip(substream);
952
953         chip->capture_substream = NULL;
954         return 0;
955 }
956
957 static int snd_es1938_playback_close(snd_pcm_substream_t * substream)
958 {
959         es1938_t *chip = snd_pcm_substream_chip(substream);
960
961         switch (substream->number) {
962         case 0:
963                 chip->playback1_substream = NULL;
964                 break;
965         case 1:
966                 chip->playback2_substream = NULL;
967                 break;
968         default:
969                 snd_BUG();
970                 return -EINVAL;
971         }
972         return 0;
973 }
974
975 static snd_pcm_ops_t snd_es1938_playback_ops = {
976         .open =         snd_es1938_playback_open,
977         .close =        snd_es1938_playback_close,
978         .ioctl =        snd_pcm_lib_ioctl,
979         .hw_params =    snd_es1938_pcm_hw_params,
980         .hw_free =      snd_es1938_pcm_hw_free,
981         .prepare =      snd_es1938_playback_prepare,
982         .trigger =      snd_es1938_playback_trigger,
983         .pointer =      snd_es1938_playback_pointer,
984 };
985
986 static snd_pcm_ops_t snd_es1938_capture_ops = {
987         .open =         snd_es1938_capture_open,
988         .close =        snd_es1938_capture_close,
989         .ioctl =        snd_pcm_lib_ioctl,
990         .hw_params =    snd_es1938_pcm_hw_params,
991         .hw_free =      snd_es1938_pcm_hw_free,
992         .prepare =      snd_es1938_capture_prepare,
993         .trigger =      snd_es1938_capture_trigger,
994         .pointer =      snd_es1938_capture_pointer,
995         .copy =         snd_es1938_capture_copy,
996 };
997
998 static void snd_es1938_free_pcm(snd_pcm_t *pcm)
999 {
1000         snd_pcm_lib_preallocate_free_for_all(pcm);
1001 }
1002
1003 static int __devinit snd_es1938_new_pcm(es1938_t *chip, int device)
1004 {
1005         snd_pcm_t *pcm;
1006         int err;
1007
1008         if ((err = snd_pcm_new(chip->card, "es-1938-1946", device, 2, 1, &pcm)) < 0)
1009                 return err;
1010         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_es1938_playback_ops);
1011         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_es1938_capture_ops);
1012         
1013         pcm->private_data = chip;
1014         pcm->private_free = snd_es1938_free_pcm;
1015         pcm->info_flags = 0;
1016         strcpy(pcm->name, "ESS Solo-1");
1017
1018         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
1019                                               snd_dma_pci_data(chip->pci), 64*1024, 64*1024);
1020
1021         chip->pcm = pcm;
1022         return 0;
1023 }
1024
1025 /* -------------------------------------------------------------------
1026  * 
1027  *                       *** Mixer part ***
1028  */
1029
1030 static int snd_es1938_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1031 {
1032         static char *texts[8] = {
1033                 "Mic", "Mic Master", "CD", "AOUT",
1034                 "Mic1", "Mix", "Line", "Master"
1035         };
1036
1037         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1038         uinfo->count = 1;
1039         uinfo->value.enumerated.items = 8;
1040         if (uinfo->value.enumerated.item > 7)
1041                 uinfo->value.enumerated.item = 7;
1042         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
1043         return 0;
1044 }
1045
1046 static int snd_es1938_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1047 {
1048         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1049         ucontrol->value.enumerated.item[0] = snd_es1938_mixer_read(chip, 0x1c) & 0x07;
1050         return 0;
1051 }
1052
1053 static int snd_es1938_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1054 {
1055         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1056         unsigned char val = ucontrol->value.enumerated.item[0];
1057         
1058         if (val > 7)
1059                 return -EINVAL;
1060         return snd_es1938_mixer_bits(chip, 0x1c, 0x07, val) != val;
1061 }
1062
1063 static int snd_es1938_info_spatializer_enable(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1064 {
1065         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1066         uinfo->count = 1;
1067         uinfo->value.integer.min = 0;
1068         uinfo->value.integer.max = 1;
1069         return 0;
1070 }
1071
1072 static int snd_es1938_get_spatializer_enable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1073 {
1074         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1075         unsigned char val = snd_es1938_mixer_read(chip, 0x50);
1076         ucontrol->value.integer.value[0] = !!(val & 8);
1077         return 0;
1078 }
1079
1080 static int snd_es1938_put_spatializer_enable(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1081 {
1082         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1083         unsigned char oval, nval;
1084         int change;
1085         nval = ucontrol->value.integer.value[0] ? 0x0c : 0x04;
1086         oval = snd_es1938_mixer_read(chip, 0x50) & 0x0c;
1087         change = nval != oval;
1088         if (change) {
1089                 snd_es1938_mixer_write(chip, 0x50, nval & ~0x04);
1090                 snd_es1938_mixer_write(chip, 0x50, nval);
1091         }
1092         return change;
1093 }
1094
1095 static int snd_es1938_info_hw_volume(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1096 {
1097         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1098         uinfo->count = 2;
1099         uinfo->value.integer.min = 0;
1100         uinfo->value.integer.max = 63;
1101         return 0;
1102 }
1103
1104 static int snd_es1938_get_hw_volume(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1105 {
1106         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1107         ucontrol->value.integer.value[0] = snd_es1938_mixer_read(chip, 0x61) & 0x3f;
1108         ucontrol->value.integer.value[1] = snd_es1938_mixer_read(chip, 0x63) & 0x3f;
1109         return 0;
1110 }
1111
1112 static int snd_es1938_info_hw_switch(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1113 {
1114         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
1115         uinfo->count = 2;
1116         uinfo->value.integer.min = 0;
1117         uinfo->value.integer.max = 1;
1118         return 0;
1119 }
1120
1121 static int snd_es1938_get_hw_switch(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1122 {
1123         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1124         ucontrol->value.integer.value[0] = !(snd_es1938_mixer_read(chip, 0x61) & 0x40);
1125         ucontrol->value.integer.value[1] = !(snd_es1938_mixer_read(chip, 0x63) & 0x40);
1126         return 0;
1127 }
1128
1129 static void snd_es1938_hwv_free(snd_kcontrol_t *kcontrol)
1130 {
1131         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1132         chip->master_volume = NULL;
1133         chip->master_switch = NULL;
1134         chip->hw_volume = NULL;
1135         chip->hw_switch = NULL;
1136 }
1137
1138 static int snd_es1938_reg_bits(es1938_t *chip, unsigned char reg,
1139                                unsigned char mask, unsigned char val)
1140 {
1141         if (reg < 0xa0)
1142                 return snd_es1938_mixer_bits(chip, reg, mask, val);
1143         else
1144                 return snd_es1938_bits(chip, reg, mask, val);
1145 }
1146
1147 static int snd_es1938_reg_read(es1938_t *chip, unsigned char reg)
1148 {
1149         if (reg < 0xa0)
1150                 return snd_es1938_mixer_read(chip, reg);
1151         else
1152                 return snd_es1938_read(chip, reg);
1153 }
1154
1155 #define ES1938_SINGLE(xname, xindex, reg, shift, mask, invert) \
1156 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1157   .info = snd_es1938_info_single, \
1158   .get = snd_es1938_get_single, .put = snd_es1938_put_single, \
1159   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
1160
1161 static int snd_es1938_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1162 {
1163         int mask = (kcontrol->private_value >> 16) & 0xff;
1164
1165         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1166         uinfo->count = 1;
1167         uinfo->value.integer.min = 0;
1168         uinfo->value.integer.max = mask;
1169         return 0;
1170 }
1171
1172 static int snd_es1938_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1173 {
1174         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1175         int reg = kcontrol->private_value & 0xff;
1176         int shift = (kcontrol->private_value >> 8) & 0xff;
1177         int mask = (kcontrol->private_value >> 16) & 0xff;
1178         int invert = (kcontrol->private_value >> 24) & 0xff;
1179         int val;
1180         
1181         val = snd_es1938_reg_read(chip, reg);
1182         ucontrol->value.integer.value[0] = (val >> shift) & mask;
1183         if (invert)
1184                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1185         return 0;
1186 }
1187
1188 static int snd_es1938_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1189 {
1190         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1191         int reg = kcontrol->private_value & 0xff;
1192         int shift = (kcontrol->private_value >> 8) & 0xff;
1193         int mask = (kcontrol->private_value >> 16) & 0xff;
1194         int invert = (kcontrol->private_value >> 24) & 0xff;
1195         unsigned char val;
1196         
1197         val = (ucontrol->value.integer.value[0] & mask);
1198         if (invert)
1199                 val = mask - val;
1200         mask <<= shift;
1201         val <<= shift;
1202         return snd_es1938_reg_bits(chip, reg, mask, val) != val;
1203 }
1204
1205 #define ES1938_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
1206 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
1207   .info = snd_es1938_info_double, \
1208   .get = snd_es1938_get_double, .put = snd_es1938_put_double, \
1209   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
1210
1211 static int snd_es1938_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1212 {
1213         int mask = (kcontrol->private_value >> 24) & 0xff;
1214
1215         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1216         uinfo->count = 2;
1217         uinfo->value.integer.min = 0;
1218         uinfo->value.integer.max = mask;
1219         return 0;
1220 }
1221
1222 static int snd_es1938_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1223 {
1224         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1225         int left_reg = kcontrol->private_value & 0xff;
1226         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1227         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1228         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1229         int mask = (kcontrol->private_value >> 24) & 0xff;
1230         int invert = (kcontrol->private_value >> 22) & 1;
1231         unsigned char left, right;
1232         
1233         left = snd_es1938_reg_read(chip, left_reg);
1234         if (left_reg != right_reg)
1235                 right = snd_es1938_reg_read(chip, right_reg);
1236         else
1237                 right = left;
1238         ucontrol->value.integer.value[0] = (left >> shift_left) & mask;
1239         ucontrol->value.integer.value[1] = (right >> shift_right) & mask;
1240         if (invert) {
1241                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1242                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1243         }
1244         return 0;
1245 }
1246
1247 static int snd_es1938_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1248 {
1249         es1938_t *chip = snd_kcontrol_chip(kcontrol);
1250         int left_reg = kcontrol->private_value & 0xff;
1251         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1252         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1253         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1254         int mask = (kcontrol->private_value >> 24) & 0xff;
1255         int invert = (kcontrol->private_value >> 22) & 1;
1256         int change;
1257         unsigned char val1, val2, mask1, mask2;
1258         
1259         val1 = ucontrol->value.integer.value[0] & mask;
1260         val2 = ucontrol->value.integer.value[1] & mask;
1261         if (invert) {
1262                 val1 = mask - val1;
1263                 val2 = mask - val2;
1264         }
1265         val1 <<= shift_left;
1266         val2 <<= shift_right;
1267         mask1 = mask << shift_left;
1268         mask2 = mask << shift_right;
1269         if (left_reg != right_reg) {
1270                 change = 0;
1271                 if (snd_es1938_reg_bits(chip, left_reg, mask1, val1) != val1)
1272                         change = 1;
1273                 if (snd_es1938_reg_bits(chip, right_reg, mask2, val2) != val2)
1274                         change = 1;
1275         } else {
1276                 change = (snd_es1938_reg_bits(chip, left_reg, mask1 | mask2, 
1277                                               val1 | val2) != (val1 | val2));
1278         }
1279         return change;
1280 }
1281
1282 static snd_kcontrol_new_t snd_es1938_controls[] = {
1283 ES1938_DOUBLE("Master Playback Volume", 0, 0x60, 0x62, 0, 0, 63, 0),
1284 ES1938_DOUBLE("Master Playback Switch", 0, 0x60, 0x62, 6, 6, 1, 1),
1285 {
1286         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1287         .name = "Hardware Master Playback Volume",
1288         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1289         .info = snd_es1938_info_hw_volume,
1290         .get = snd_es1938_get_hw_volume,
1291 },
1292 {
1293         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1294         .name = "Hardware Master Playback Switch",
1295         .access = SNDRV_CTL_ELEM_ACCESS_READ,
1296         .info = snd_es1938_info_hw_switch,
1297         .get = snd_es1938_get_hw_switch,
1298 },
1299 ES1938_SINGLE("Hardware Volume Split", 0, 0x64, 7, 1, 0),
1300 ES1938_DOUBLE("Line Playback Volume", 0, 0x3e, 0x3e, 4, 0, 15, 0),
1301 ES1938_DOUBLE("CD Playback Volume", 0, 0x38, 0x38, 4, 0, 15, 0),
1302 ES1938_DOUBLE("FM Playback Volume", 0, 0x36, 0x36, 4, 0, 15, 0),
1303 ES1938_DOUBLE("Mono Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1304 ES1938_DOUBLE("Mic Playback Volume", 0, 0x1a, 0x1a, 4, 0, 15, 0),
1305 ES1938_DOUBLE("Aux Playback Volume", 0, 0x3a, 0x3a, 4, 0, 15, 0),
1306 ES1938_DOUBLE("Capture Volume", 0, 0xb4, 0xb4, 4, 0, 15, 0),
1307 ES1938_SINGLE("PC Speaker Volume", 0, 0x3c, 0, 7, 0),
1308 ES1938_SINGLE("Record Monitor", 0, 0xa8, 3, 1, 0),
1309 ES1938_SINGLE("Capture Switch", 0, 0x1c, 4, 1, 1),
1310 {
1311         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1312         .name = "Capture Source",
1313         .info = snd_es1938_info_mux,
1314         .get = snd_es1938_get_mux,
1315         .put = snd_es1938_put_mux,
1316 },
1317 ES1938_DOUBLE("Mono Input Playback Volume", 0, 0x6d, 0x6d, 4, 0, 15, 0),
1318 ES1938_DOUBLE("PCM Capture Volume", 0, 0x69, 0x69, 4, 0, 15, 0),
1319 ES1938_DOUBLE("Mic Capture Volume", 0, 0x68, 0x68, 4, 0, 15, 0),
1320 ES1938_DOUBLE("Line Capture Volume", 0, 0x6e, 0x6e, 4, 0, 15, 0),
1321 ES1938_DOUBLE("FM Capture Volume", 0, 0x6b, 0x6b, 4, 0, 15, 0),
1322 ES1938_DOUBLE("Mono Capture Volume", 0, 0x6f, 0x6f, 4, 0, 15, 0),
1323 ES1938_DOUBLE("CD Capture Volume", 0, 0x6a, 0x6a, 4, 0, 15, 0),
1324 ES1938_DOUBLE("Aux Capture Volume", 0, 0x6c, 0x6c, 4, 0, 15, 0),
1325 ES1938_DOUBLE("PCM Playback Volume", 0, 0x7c, 0x7c, 4, 0, 15, 0),
1326 ES1938_DOUBLE("PCM Playback Volume", 1, 0x14, 0x14, 4, 0, 15, 0),
1327 ES1938_SINGLE("3D Control - Level", 0, 0x52, 0, 63, 0),
1328 {
1329         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1330         .name = "3D Control - Switch",
1331         .info = snd_es1938_info_spatializer_enable,
1332         .get = snd_es1938_get_spatializer_enable,
1333         .put = snd_es1938_put_spatializer_enable,
1334 },
1335 ES1938_SINGLE("Mic Boost (+26dB)", 0, 0x7d, 3, 1, 0)
1336 };
1337
1338
1339 /* ---------------------------------------------------------------------------- */
1340 /* ---------------------------------------------------------------------------- */
1341
1342 /*
1343  * initialize the chip - used by resume callback, too
1344  */
1345 static void snd_es1938_chip_init(es1938_t *chip)
1346 {
1347         /* reset chip */
1348         snd_es1938_reset(chip);
1349
1350         /* configure native mode */
1351
1352         /* enable bus master */
1353         pci_set_master(chip->pci);
1354
1355         /* disable legacy audio */
1356         pci_write_config_word(chip->pci, SL_PCI_LEGACYCONTROL, 0x805f);
1357
1358         /* set DDMA base */
1359         pci_write_config_word(chip->pci, SL_PCI_DDMACONTROL, chip->ddma_port | 1);
1360
1361         /* set DMA/IRQ policy */
1362         pci_write_config_dword(chip->pci, SL_PCI_CONFIG, 0);
1363
1364         /* enable Audio 1, Audio 2, MPU401 IRQ and HW volume IRQ*/
1365         outb(0xf0, SLIO_REG(chip, IRQCONTROL));
1366
1367         /* reset DMA */
1368         outb(0, SLDM_REG(chip, DMACLEAR));
1369 }
1370
1371 #ifdef CONFIG_PM
1372 /*
1373  * PM support
1374  */
1375
1376 static unsigned char saved_regs[SAVED_REG_SIZE+1] = {
1377         0x14, 0x1a, 0x1c, 0x3a, 0x3c, 0x3e, 0x36, 0x38,
1378         0x50, 0x52, 0x60, 0x61, 0x62, 0x63, 0x64, 0x68,
1379         0x69, 0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x7c, 0x7d,
1380         0xa8, 0xb4,
1381 };
1382
1383
1384 static int es1938_suspend(snd_card_t *card, unsigned int state)
1385 {
1386         es1938_t *chip = card->pm_private_data;
1387         unsigned char *s, *d;
1388
1389         snd_pcm_suspend_all(chip->pcm);
1390
1391         /* save mixer-related registers */
1392         for (s = saved_regs, d = chip->saved_regs; *s; s++, d++)
1393                 *d = snd_es1938_reg_read(chip, *s);
1394
1395         outb(0x00, SLIO_REG(chip, IRQCONTROL)); /* disable irqs */
1396
1397         pci_disable_device(chip->pci);
1398         return 0;
1399 }
1400
1401 static int es1938_resume(snd_card_t *card, unsigned int state)
1402 {
1403         es1938_t *chip = card->pm_private_data;
1404         unsigned char *s, *d;
1405
1406         pci_enable_device(chip->pci);
1407         snd_es1938_chip_init(chip);
1408
1409         /* restore mixer-related registers */
1410         for (s = saved_regs, d = chip->saved_regs; *s; s++, d++) {
1411                 if (*s < 0xa0)
1412                         snd_es1938_mixer_write(chip, *s, *d);
1413                 else
1414                         snd_es1938_write(chip, *s, *d);
1415         }
1416
1417         return 0;
1418 }
1419 #endif /* CONFIG_PM */
1420
1421 static int snd_es1938_free(es1938_t *chip)
1422 {
1423         /* disable irqs */
1424         outb(0x00, SLIO_REG(chip, IRQCONTROL));
1425         if (chip->rmidi)
1426                 snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0);
1427
1428 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
1429         if (chip->gameport.io)
1430                 gameport_unregister_port(&chip->gameport);
1431 #endif
1432         if (chip->irq >= 0)
1433                 free_irq(chip->irq, (void *)chip);
1434         pci_release_regions(chip->pci);
1435         pci_disable_device(chip->pci);
1436         kfree(chip);
1437         return 0;
1438 }
1439
1440 static int snd_es1938_dev_free(snd_device_t *device)
1441 {
1442         es1938_t *chip = device->device_data;
1443         return snd_es1938_free(chip);
1444 }
1445
1446 static int __devinit snd_es1938_create(snd_card_t * card,
1447                                     struct pci_dev * pci,
1448                                     es1938_t ** rchip)
1449 {
1450         es1938_t *chip;
1451         int err;
1452         static snd_device_ops_t ops = {
1453                 .dev_free =     snd_es1938_dev_free,
1454         };
1455
1456         *rchip = NULL;
1457
1458         /* enable PCI device */
1459         if ((err = pci_enable_device(pci)) < 0)
1460                 return err;
1461         /* check, if we can restrict PCI DMA transfers to 24 bits */
1462         if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
1463             pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
1464                 snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
1465                 pci_disable_device(pci);
1466                 return -ENXIO;
1467         }
1468
1469         chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
1470         if (chip == NULL) {
1471                 pci_disable_device(pci);
1472                 return -ENOMEM;
1473         }
1474         spin_lock_init(&chip->reg_lock);
1475         spin_lock_init(&chip->mixer_lock);
1476         chip->card = card;
1477         chip->pci = pci;
1478         if ((err = pci_request_regions(pci, "ESS Solo-1")) < 0) {
1479                 kfree(chip);
1480                 pci_disable_device(pci);
1481                 return err;
1482         }
1483         chip->io_port = pci_resource_start(pci, 0);
1484         chip->sb_port = pci_resource_start(pci, 1);
1485         chip->vc_port = pci_resource_start(pci, 2);
1486         chip->mpu_port = pci_resource_start(pci, 3);
1487         chip->game_port = pci_resource_start(pci, 4);
1488         if (request_irq(pci->irq, snd_es1938_interrupt, SA_INTERRUPT|SA_SHIRQ, "ES1938", (void *)chip)) {
1489                 snd_printk("unable to grab IRQ %d\n", pci->irq);
1490                 snd_es1938_free(chip);
1491                 return -EBUSY;
1492         }
1493         chip->irq = pci->irq;
1494 #ifdef ES1938_DDEBUG
1495         snd_printk("create: io: 0x%lx, sb: 0x%lx, vc: 0x%lx, mpu: 0x%lx, game: 0x%lx\n",
1496                    chip->io_port, chip->sb_port, chip->vc_port, chip->mpu_port, chip->game_port);
1497 #endif
1498
1499         chip->ddma_port = chip->vc_port + 0x00;         /* fix from Thomas Sailer */
1500
1501         snd_es1938_chip_init(chip);
1502
1503         snd_card_set_pm_callback(card, es1938_suspend, es1938_resume, chip);
1504
1505         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) {
1506                 snd_es1938_free(chip);
1507                 return err;
1508         }
1509
1510         snd_card_set_dev(card, &pci->dev);
1511
1512         *rchip = chip;
1513         return 0;
1514 }
1515
1516 /* --------------------------------------------------------------------
1517  * Interrupt handler
1518  * -------------------------------------------------------------------- */
1519 static irqreturn_t snd_es1938_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1520 {
1521         es1938_t *chip = dev_id;
1522         unsigned char status, audiostatus;
1523         int handled = 0;
1524
1525         status = inb(SLIO_REG(chip, IRQCONTROL));
1526 #if 0
1527         printk("Es1938debug - interrupt status: =0x%x\n", status);
1528 #endif
1529         
1530         /* AUDIO 1 */
1531         if (status & 0x10) {
1532 #if 0
1533                 printk("Es1938debug - AUDIO channel 1 interrupt\n");
1534                 printk("Es1938debug - AUDIO channel 1 DMAC DMA count: %u\n", inw(SLDM_REG(chip, DMACOUNT)));
1535                 printk("Es1938debug - AUDIO channel 1 DMAC DMA base: %u\n", inl(SLDM_REG(chip, DMAADDR)));
1536                 printk("Es1938debug - AUDIO channel 1 DMAC DMA status: 0x%x\n", inl(SLDM_REG(chip, DMASTATUS)));
1537 #endif
1538                 /* clear irq */
1539                 handled = 1;
1540                 audiostatus = inb(SLSB_REG(chip, STATUS));
1541                 if (chip->active & ADC1)
1542                         snd_pcm_period_elapsed(chip->capture_substream);
1543                 else if (chip->active & DAC1)
1544                         snd_pcm_period_elapsed(chip->playback2_substream);
1545         }
1546         
1547         /* AUDIO 2 */
1548         if (status & 0x20) {
1549 #if 0
1550                 printk("Es1938debug - AUDIO channel 2 interrupt\n");
1551                 printk("Es1938debug - AUDIO channel 2 DMAC DMA count: %u\n", inw(SLIO_REG(chip, AUDIO2DMACOUNT)));
1552                 printk("Es1938debug - AUDIO channel 2 DMAC DMA base: %u\n", inl(SLIO_REG(chip, AUDIO2DMAADDR)));
1553
1554 #endif
1555                 /* clear irq */
1556                 handled = 1;
1557                 snd_es1938_mixer_bits(chip, ESSSB_IREG_AUDIO2CONTROL2, 0x80, 0);
1558                 if (chip->active & DAC2)
1559                         snd_pcm_period_elapsed(chip->playback1_substream);
1560         }
1561
1562         /* Hardware volume */
1563         if (status & 0x40) {
1564                 int split = snd_es1938_mixer_read(chip, 0x64) & 0x80;
1565                 handled = 1;
1566                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_switch->id);
1567                 snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->hw_volume->id);
1568                 if (!split) {
1569                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_switch->id);
1570                         snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE, &chip->master_volume->id);
1571                 }
1572                 /* ack interrupt */
1573                 snd_es1938_mixer_write(chip, 0x66, 0x00);
1574         }
1575
1576         /* MPU401 */
1577         if (status & 0x80) {
1578                 // the following line is evil! It switches off MIDI interrupt handling after the first interrupt received.
1579                 // replacing the last 0 by 0x40 works for ESS-Solo1, but just doing nothing works as well!
1580                 // andreas@flying-snail.de
1581                 // snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0); /* ack? */
1582                 if (chip->rmidi) {
1583                         handled = 1;
1584                         snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
1585                 }
1586         }
1587         return IRQ_RETVAL(handled);
1588 }
1589
1590 #define ES1938_DMA_SIZE 64
1591
1592 static int __devinit snd_es1938_mixer(es1938_t *chip)
1593 {
1594         snd_card_t *card;
1595         unsigned int idx;
1596         int err;
1597
1598         card = chip->card;
1599
1600         strcpy(card->mixername, "ESS Solo-1");
1601
1602         for (idx = 0; idx < ARRAY_SIZE(snd_es1938_controls); idx++) {
1603                 snd_kcontrol_t *kctl;
1604                 kctl = snd_ctl_new1(&snd_es1938_controls[idx], chip);
1605                 switch (idx) {
1606                         case 0:
1607                                 chip->master_volume = kctl;
1608                                 kctl->private_free = snd_es1938_hwv_free;
1609                                 break;
1610                         case 1:
1611                                 chip->master_switch = kctl;
1612                                 kctl->private_free = snd_es1938_hwv_free;
1613                                 break;
1614                         case 2:
1615                                 chip->hw_volume = kctl;
1616                                 kctl->private_free = snd_es1938_hwv_free;
1617                                 break;
1618                         case 3:
1619                                 chip->hw_switch = kctl;
1620                                 kctl->private_free = snd_es1938_hwv_free;
1621                                 break;
1622                         }
1623                 if ((err = snd_ctl_add(card, kctl)) < 0)
1624                         return err;
1625         }
1626         return 0;
1627 }
1628        
1629
1630 static int __devinit snd_es1938_probe(struct pci_dev *pci,
1631                                       const struct pci_device_id *pci_id)
1632 {
1633         static int dev;
1634         snd_card_t *card;
1635         es1938_t *chip;
1636         opl3_t *opl3;
1637         int idx, err;
1638
1639         if (dev >= SNDRV_CARDS)
1640                 return -ENODEV;
1641         if (!enable[dev]) {
1642                 dev++;
1643                 return -ENOENT;
1644         }
1645
1646         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1647         if (card == NULL)
1648                 return -ENOMEM;
1649         for (idx = 0; idx < 5; idx++) {
1650                 if (pci_resource_start(pci, idx) == 0 ||
1651                     !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
1652                         snd_card_free(card);
1653                         return -ENODEV;
1654                 }
1655         }
1656         if ((err = snd_es1938_create(card, pci, &chip)) < 0) {
1657                 snd_card_free(card);
1658                 return err;
1659         }
1660
1661         strcpy(card->driver, "ES1938");
1662         strcpy(card->shortname, "ESS ES1938 (Solo-1)");
1663         sprintf(card->longname, "%s rev %i, irq %i",
1664                 card->shortname,
1665                 chip->revision,
1666                 chip->irq);
1667
1668         if ((err = snd_es1938_new_pcm(chip, 0)) < 0) {
1669                 snd_card_free(card);
1670                 return err;
1671         }
1672         if ((err = snd_es1938_mixer(chip)) < 0) {
1673                 snd_card_free(card);
1674                 return err;
1675         }
1676         if (snd_opl3_create(card,
1677                             SLSB_REG(chip, FMLOWADDR),
1678                             SLSB_REG(chip, FMHIGHADDR),
1679                             OPL3_HW_OPL3, 1, &opl3) < 0) {
1680                 printk(KERN_ERR "es1938: OPL3 not detected at 0x%lx\n",
1681                            SLSB_REG(chip, FMLOWADDR));
1682         } else {
1683                 if ((err = snd_opl3_timer_new(opl3, 0, 1)) < 0) {
1684                         snd_card_free(card);
1685                         return err;
1686                 }
1687                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1688                         snd_card_free(card);
1689                         return err;
1690                 }
1691         }
1692         if (snd_mpu401_uart_new(card, 0, MPU401_HW_MPU401,
1693                                 chip->mpu_port, 1, chip->irq, 0, &chip->rmidi) < 0) {
1694                 printk(KERN_ERR "es1938: unable to initialize MPU-401\n");
1695         } else {
1696                 // this line is vital for MIDI interrupt handling on ess-solo1
1697                 // andreas@flying-snail.de
1698                 snd_es1938_mixer_bits(chip, ESSSB_IREG_MPU401CONTROL, 0x40, 0x40);
1699         }
1700
1701 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
1702         chip->gameport.io = chip->game_port;
1703         gameport_register_port(&chip->gameport);
1704 #endif
1705
1706         if ((err = snd_card_register(card)) < 0) {
1707                 snd_card_free(card);
1708                 return err;
1709         }
1710
1711         pci_set_drvdata(pci, card);
1712         dev++;
1713         return 0;
1714 }
1715
1716 static void __devexit snd_es1938_remove(struct pci_dev *pci)
1717 {
1718         snd_card_free(pci_get_drvdata(pci));
1719         pci_set_drvdata(pci, NULL);
1720 }
1721
1722 static struct pci_driver driver = {
1723         .name = "ESS ES1938 (Solo-1)",
1724         .id_table = snd_es1938_ids,
1725         .probe = snd_es1938_probe,
1726         .remove = __devexit_p(snd_es1938_remove),
1727         SND_PCI_PM_CALLBACKS
1728 };
1729
1730 static int __init alsa_card_es1938_init(void)
1731 {
1732         return pci_module_init(&driver);
1733 }
1734
1735 static void __exit alsa_card_es1938_exit(void)
1736 {
1737         pci_unregister_driver(&driver);
1738 }
1739
1740 module_init(alsa_card_es1938_init)
1741 module_exit(alsa_card_es1938_exit)