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