1f6c2bfd43fdef56faa85973b24e7b70f431d40d
[safe/jmp/linux-2.6] / sound / pci / sonicvibes.c
1 /*
2  *  Driver for S3 SonicVibes soundcard
3  *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
4  *
5  *  BUGS:
6  *    It looks like 86c617 rev 3 doesn't supports DDMA buffers above 16MB?
7  *    Driver sometimes hangs... Nobody knows why at this moment...
8  *
9  *   This program is free software; you can redistribute it and/or modify
10  *   it under the terms of the GNU General Public License as published by
11  *   the Free Software Foundation; either version 2 of the License, or
12  *   (at your option) any later version.
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22  *
23  */
24
25 #include <sound/driver.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/pci.h>
30 #include <linux/slab.h>
31 #include <linux/gameport.h>
32 #include <linux/moduleparam.h>
33
34 #include <sound/core.h>
35 #include <sound/pcm.h>
36 #include <sound/info.h>
37 #include <sound/control.h>
38 #include <sound/mpu401.h>
39 #include <sound/opl3.h>
40 #include <sound/initval.h>
41
42 #include <asm/io.h>
43
44 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
45 MODULE_DESCRIPTION("S3 SonicVibes PCI");
46 MODULE_LICENSE("GPL");
47 MODULE_SUPPORTED_DEVICE("{{S3,SonicVibes PCI}}");
48
49 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
50 #define SUPPORT_JOYSTICK 1
51 #endif
52
53 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
54 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
55 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
56 static int reverb[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
57 static int mge[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS - 1)] = 0};
58 static unsigned int dmaio = 0x7a00;     /* DDMA i/o address */
59
60 module_param_array(index, int, NULL, 0444);
61 MODULE_PARM_DESC(index, "Index value for S3 SonicVibes soundcard.");
62 module_param_array(id, charp, NULL, 0444);
63 MODULE_PARM_DESC(id, "ID string for S3 SonicVibes soundcard.");
64 module_param_array(enable, bool, NULL, 0444);
65 MODULE_PARM_DESC(enable, "Enable S3 SonicVibes soundcard.");
66 module_param_array(reverb, bool, NULL, 0444);
67 MODULE_PARM_DESC(reverb, "Enable reverb (SRAM is present) for S3 SonicVibes soundcard.");
68 module_param_array(mge, bool, NULL, 0444);
69 MODULE_PARM_DESC(mge, "MIC Gain Enable for S3 SonicVibes soundcard.");
70 module_param(dmaio, uint, 0444);
71 MODULE_PARM_DESC(dmaio, "DDMA i/o base address for S3 SonicVibes soundcard.");
72
73 /*
74  * Enhanced port direct registers
75  */
76
77 #define SV_REG(sonic, x) ((sonic)->enh_port + SV_REG_##x)
78
79 #define SV_REG_CONTROL  0x00    /* R/W: CODEC/Mixer control register */
80 #define   SV_ENHANCED     0x01  /* audio mode select - enhanced mode */
81 #define   SV_TEST         0x02  /* test bit */
82 #define   SV_REVERB       0x04  /* reverb enable */
83 #define   SV_WAVETABLE    0x08  /* wavetable active / FM active if not set */
84 #define   SV_INTA         0x20  /* INTA driving - should be always 1 */
85 #define   SV_RESET        0x80  /* reset chip */
86 #define SV_REG_IRQMASK  0x01    /* R/W: CODEC/Mixer interrupt mask register */
87 #define   SV_DMAA_MASK    0x01  /* mask DMA-A interrupt */
88 #define   SV_DMAC_MASK    0x04  /* mask DMA-C interrupt */
89 #define   SV_SPEC_MASK    0x08  /* special interrupt mask - should be always masked */
90 #define   SV_UD_MASK      0x40  /* Up/Down button interrupt mask */
91 #define   SV_MIDI_MASK    0x80  /* mask MIDI interrupt */
92 #define SV_REG_STATUS   0x02    /* R/O: CODEC/Mixer status register */
93 #define   SV_DMAA_IRQ     0x01  /* DMA-A interrupt */
94 #define   SV_DMAC_IRQ     0x04  /* DMA-C interrupt */
95 #define   SV_SPEC_IRQ     0x08  /* special interrupt */
96 #define   SV_UD_IRQ       0x40  /* Up/Down interrupt */
97 #define   SV_MIDI_IRQ     0x80  /* MIDI interrupt */
98 #define SV_REG_INDEX    0x04    /* R/W: CODEC/Mixer index address register */
99 #define   SV_MCE          0x40  /* mode change enable */
100 #define   SV_TRD          0x80  /* DMA transfer request disabled */
101 #define SV_REG_DATA     0x05    /* R/W: CODEC/Mixer index data register */
102
103 /*
104  * Enhanced port indirect registers
105  */
106
107 #define SV_IREG_LEFT_ADC        0x00    /* Left ADC Input Control */
108 #define SV_IREG_RIGHT_ADC       0x01    /* Right ADC Input Control */
109 #define SV_IREG_LEFT_AUX1       0x02    /* Left AUX1 Input Control */
110 #define SV_IREG_RIGHT_AUX1      0x03    /* Right AUX1 Input Control */
111 #define SV_IREG_LEFT_CD         0x04    /* Left CD Input Control */
112 #define SV_IREG_RIGHT_CD        0x05    /* Right CD Input Control */
113 #define SV_IREG_LEFT_LINE       0x06    /* Left Line Input Control */
114 #define SV_IREG_RIGHT_LINE      0x07    /* Right Line Input Control */
115 #define SV_IREG_MIC             0x08    /* MIC Input Control */
116 #define SV_IREG_GAME_PORT       0x09    /* Game Port Control */
117 #define SV_IREG_LEFT_SYNTH      0x0a    /* Left Synth Input Control */
118 #define SV_IREG_RIGHT_SYNTH     0x0b    /* Right Synth Input Control */
119 #define SV_IREG_LEFT_AUX2       0x0c    /* Left AUX2 Input Control */
120 #define SV_IREG_RIGHT_AUX2      0x0d    /* Right AUX2 Input Control */
121 #define SV_IREG_LEFT_ANALOG     0x0e    /* Left Analog Mixer Output Control */
122 #define SV_IREG_RIGHT_ANALOG    0x0f    /* Right Analog Mixer Output Control */
123 #define SV_IREG_LEFT_PCM        0x10    /* Left PCM Input Control */
124 #define SV_IREG_RIGHT_PCM       0x11    /* Right PCM Input Control */
125 #define SV_IREG_DMA_DATA_FMT    0x12    /* DMA Data Format */
126 #define SV_IREG_PC_ENABLE       0x13    /* Playback/Capture Enable Register */
127 #define SV_IREG_UD_BUTTON       0x14    /* Up/Down Button Register */
128 #define SV_IREG_REVISION        0x15    /* Revision */
129 #define SV_IREG_ADC_OUTPUT_CTRL 0x16    /* ADC Output Control */
130 #define SV_IREG_DMA_A_UPPER     0x18    /* DMA A Upper Base Count */
131 #define SV_IREG_DMA_A_LOWER     0x19    /* DMA A Lower Base Count */
132 #define SV_IREG_DMA_C_UPPER     0x1c    /* DMA C Upper Base Count */
133 #define SV_IREG_DMA_C_LOWER     0x1d    /* DMA C Lower Base Count */
134 #define SV_IREG_PCM_RATE_LOW    0x1e    /* PCM Sampling Rate Low Byte */
135 #define SV_IREG_PCM_RATE_HIGH   0x1f    /* PCM Sampling Rate High Byte */
136 #define SV_IREG_SYNTH_RATE_LOW  0x20    /* Synthesizer Sampling Rate Low Byte */
137 #define SV_IREG_SYNTH_RATE_HIGH 0x21    /* Synthesizer Sampling Rate High Byte */
138 #define SV_IREG_ADC_CLOCK       0x22    /* ADC Clock Source Selection */
139 #define SV_IREG_ADC_ALT_RATE    0x23    /* ADC Alternative Sampling Rate Selection */
140 #define SV_IREG_ADC_PLL_M       0x24    /* ADC PLL M Register */
141 #define SV_IREG_ADC_PLL_N       0x25    /* ADC PLL N Register */
142 #define SV_IREG_SYNTH_PLL_M     0x26    /* Synthesizer PLL M Register */
143 #define SV_IREG_SYNTH_PLL_N     0x27    /* Synthesizer PLL N Register */
144 #define SV_IREG_MPU401          0x2a    /* MPU-401 UART Operation */
145 #define SV_IREG_DRIVE_CTRL      0x2b    /* Drive Control */
146 #define SV_IREG_SRS_SPACE       0x2c    /* SRS Space Control */
147 #define SV_IREG_SRS_CENTER      0x2d    /* SRS Center Control */
148 #define SV_IREG_WAVE_SOURCE     0x2e    /* Wavetable Sample Source Select */
149 #define SV_IREG_ANALOG_POWER    0x30    /* Analog Power Down Control */
150 #define SV_IREG_DIGITAL_POWER   0x31    /* Digital Power Down Control */
151
152 #define SV_IREG_ADC_PLL         SV_IREG_ADC_PLL_M
153 #define SV_IREG_SYNTH_PLL       SV_IREG_SYNTH_PLL_M
154
155 /*
156  *  DMA registers
157  */
158
159 #define SV_DMA_ADDR0            0x00
160 #define SV_DMA_ADDR1            0x01
161 #define SV_DMA_ADDR2            0x02
162 #define SV_DMA_ADDR3            0x03
163 #define SV_DMA_COUNT0           0x04
164 #define SV_DMA_COUNT1           0x05
165 #define SV_DMA_COUNT2           0x06
166 #define SV_DMA_MODE             0x0b
167 #define SV_DMA_RESET            0x0d
168 #define SV_DMA_MASK             0x0f
169
170 /*
171  *  Record sources
172  */
173
174 #define SV_RECSRC_RESERVED      (0x00<<5)
175 #define SV_RECSRC_CD            (0x01<<5)
176 #define SV_RECSRC_DAC           (0x02<<5)
177 #define SV_RECSRC_AUX2          (0x03<<5)
178 #define SV_RECSRC_LINE          (0x04<<5)
179 #define SV_RECSRC_AUX1          (0x05<<5)
180 #define SV_RECSRC_MIC           (0x06<<5)
181 #define SV_RECSRC_OUT           (0x07<<5)
182
183 /*
184  *  constants
185  */
186
187 #define SV_FULLRATE             48000
188 #define SV_REFFREQUENCY         24576000
189 #define SV_ADCMULT              512
190
191 #define SV_MODE_PLAY            1
192 #define SV_MODE_CAPTURE         2
193
194 /*
195
196  */
197
198 typedef struct _snd_sonicvibes sonicvibes_t;
199
200 struct _snd_sonicvibes {
201         unsigned long dma1size;
202         unsigned long dma2size;
203         int irq;
204
205         unsigned long sb_port;
206         unsigned long enh_port;
207         unsigned long synth_port;
208         unsigned long midi_port;
209         unsigned long game_port;
210         unsigned int dmaa_port;
211         struct resource *res_dmaa;
212         unsigned int dmac_port;
213         struct resource *res_dmac;
214
215         unsigned char enable;
216         unsigned char irqmask;
217         unsigned char revision;
218         unsigned char format;
219         unsigned char srs_space;
220         unsigned char srs_center;
221         unsigned char mpu_switch;
222         unsigned char wave_source;
223
224         unsigned int mode;
225
226         struct pci_dev *pci;
227         snd_card_t *card;
228         snd_pcm_t *pcm;
229         snd_pcm_substream_t *playback_substream;
230         snd_pcm_substream_t *capture_substream;
231         snd_rawmidi_t *rmidi;
232         snd_hwdep_t *fmsynth;   /* S3FM */
233
234         spinlock_t reg_lock;
235
236         unsigned int p_dma_size;
237         unsigned int c_dma_size;
238
239         snd_kcontrol_t *master_mute;
240         snd_kcontrol_t *master_volume;
241
242 #ifdef SUPPORT_JOYSTICK
243         struct gameport *gameport;
244 #endif
245 };
246
247 static struct pci_device_id snd_sonic_ids[] = {
248         { 0x5333, 0xca00, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },
249         { 0, }
250 };
251
252 MODULE_DEVICE_TABLE(pci, snd_sonic_ids);
253
254 static ratden_t sonicvibes_adc_clock = {
255         .num_min = 4000 * 65536,
256         .num_max = 48000UL * 65536,
257         .num_step = 1,
258         .den = 65536,
259 };
260 static snd_pcm_hw_constraint_ratdens_t snd_sonicvibes_hw_constraints_adc_clock = {
261         .nrats = 1,
262         .rats = &sonicvibes_adc_clock,
263 };
264
265 /*
266  *  common I/O routines
267  */
268
269 static inline void snd_sonicvibes_setdmaa(sonicvibes_t * sonic,
270                                           unsigned int addr,
271                                           unsigned int count)
272 {
273         count--;
274         outl(addr, sonic->dmaa_port + SV_DMA_ADDR0);
275         outl(count, sonic->dmaa_port + SV_DMA_COUNT0);
276         outb(0x18, sonic->dmaa_port + SV_DMA_MODE);
277 #if 0
278         printk("program dmaa: addr = 0x%x, paddr = 0x%x\n", addr, inl(sonic->dmaa_port + SV_DMA_ADDR0));
279 #endif
280 }
281
282 static inline void snd_sonicvibes_setdmac(sonicvibes_t * sonic,
283                                           unsigned int addr,
284                                           unsigned int count)
285 {
286         /* note: dmac is working in word mode!!! */
287         count >>= 1;
288         count--;
289         outl(addr, sonic->dmac_port + SV_DMA_ADDR0);
290         outl(count, sonic->dmac_port + SV_DMA_COUNT0);
291         outb(0x14, sonic->dmac_port + SV_DMA_MODE);
292 #if 0
293         printk("program dmac: addr = 0x%x, paddr = 0x%x\n", addr, inl(sonic->dmac_port + SV_DMA_ADDR0));
294 #endif
295 }
296
297 static inline unsigned int snd_sonicvibes_getdmaa(sonicvibes_t * sonic)
298 {
299         return (inl(sonic->dmaa_port + SV_DMA_COUNT0) & 0xffffff) + 1;
300 }
301
302 static inline unsigned int snd_sonicvibes_getdmac(sonicvibes_t * sonic)
303 {
304         /* note: dmac is working in word mode!!! */
305         return ((inl(sonic->dmac_port + SV_DMA_COUNT0) & 0xffffff) + 1) << 1;
306 }
307
308 static void snd_sonicvibes_out1(sonicvibes_t * sonic,
309                                 unsigned char reg,
310                                 unsigned char value)
311 {
312         outb(reg, SV_REG(sonic, INDEX));
313         udelay(10);
314         outb(value, SV_REG(sonic, DATA));
315         udelay(10);
316 }
317
318 static void snd_sonicvibes_out(sonicvibes_t * sonic,
319                                unsigned char reg,
320                                unsigned char value)
321 {
322         unsigned long flags;
323
324         spin_lock_irqsave(&sonic->reg_lock, flags);
325         outb(reg, SV_REG(sonic, INDEX));
326         udelay(10);
327         outb(value, SV_REG(sonic, DATA));
328         udelay(10);
329         spin_unlock_irqrestore(&sonic->reg_lock, flags);
330 }
331
332 static unsigned char snd_sonicvibes_in1(sonicvibes_t * sonic, unsigned char reg)
333 {
334         unsigned char value;
335
336         outb(reg, SV_REG(sonic, INDEX));
337         udelay(10);
338         value = inb(SV_REG(sonic, DATA));
339         udelay(10);
340         return value;
341 }
342
343 static unsigned char snd_sonicvibes_in(sonicvibes_t * sonic, unsigned char reg)
344 {
345         unsigned long flags;
346         unsigned char value;
347
348         spin_lock_irqsave(&sonic->reg_lock, flags);
349         outb(reg, SV_REG(sonic, INDEX));
350         udelay(10);
351         value = inb(SV_REG(sonic, DATA));
352         udelay(10);
353         spin_unlock_irqrestore(&sonic->reg_lock, flags);
354         return value;
355 }
356
357 #if 0
358 static void snd_sonicvibes_debug(sonicvibes_t * sonic)
359 {
360         printk("SV REGS:          INDEX = 0x%02x  ", inb(SV_REG(sonic, INDEX)));
361         printk("                 STATUS = 0x%02x\n", inb(SV_REG(sonic, STATUS)));
362         printk("  0x00: left input      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x00));
363         printk("  0x20: synth rate low  = 0x%02x\n", snd_sonicvibes_in(sonic, 0x20));
364         printk("  0x01: right input     = 0x%02x  ", snd_sonicvibes_in(sonic, 0x01));
365         printk("  0x21: synth rate high = 0x%02x\n", snd_sonicvibes_in(sonic, 0x21));
366         printk("  0x02: left AUX1       = 0x%02x  ", snd_sonicvibes_in(sonic, 0x02));
367         printk("  0x22: ADC clock       = 0x%02x\n", snd_sonicvibes_in(sonic, 0x22));
368         printk("  0x03: right AUX1      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x03));
369         printk("  0x23: ADC alt rate    = 0x%02x\n", snd_sonicvibes_in(sonic, 0x23));
370         printk("  0x04: left CD         = 0x%02x  ", snd_sonicvibes_in(sonic, 0x04));
371         printk("  0x24: ADC pll M       = 0x%02x\n", snd_sonicvibes_in(sonic, 0x24));
372         printk("  0x05: right CD        = 0x%02x  ", snd_sonicvibes_in(sonic, 0x05));
373         printk("  0x25: ADC pll N       = 0x%02x\n", snd_sonicvibes_in(sonic, 0x25));
374         printk("  0x06: left line       = 0x%02x  ", snd_sonicvibes_in(sonic, 0x06));
375         printk("  0x26: Synth pll M     = 0x%02x\n", snd_sonicvibes_in(sonic, 0x26));
376         printk("  0x07: right line      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x07));
377         printk("  0x27: Synth pll N     = 0x%02x\n", snd_sonicvibes_in(sonic, 0x27));
378         printk("  0x08: MIC             = 0x%02x  ", snd_sonicvibes_in(sonic, 0x08));
379         printk("  0x28: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x28));
380         printk("  0x09: Game port       = 0x%02x  ", snd_sonicvibes_in(sonic, 0x09));
381         printk("  0x29: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x29));
382         printk("  0x0a: left synth      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x0a));
383         printk("  0x2a: MPU401          = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2a));
384         printk("  0x0b: right synth     = 0x%02x  ", snd_sonicvibes_in(sonic, 0x0b));
385         printk("  0x2b: drive ctrl      = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2b));
386         printk("  0x0c: left AUX2       = 0x%02x  ", snd_sonicvibes_in(sonic, 0x0c));
387         printk("  0x2c: SRS space       = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2c));
388         printk("  0x0d: right AUX2      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x0d));
389         printk("  0x2d: SRS center      = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2d));
390         printk("  0x0e: left analog     = 0x%02x  ", snd_sonicvibes_in(sonic, 0x0e));
391         printk("  0x2e: wave source     = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2e));
392         printk("  0x0f: right analog    = 0x%02x  ", snd_sonicvibes_in(sonic, 0x0f));
393         printk("  0x2f: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x2f));
394         printk("  0x10: left PCM        = 0x%02x  ", snd_sonicvibes_in(sonic, 0x10));
395         printk("  0x30: analog power    = 0x%02x\n", snd_sonicvibes_in(sonic, 0x30));
396         printk("  0x11: right PCM       = 0x%02x  ", snd_sonicvibes_in(sonic, 0x11));
397         printk("  0x31: analog power    = 0x%02x\n", snd_sonicvibes_in(sonic, 0x31));
398         printk("  0x12: DMA data format = 0x%02x  ", snd_sonicvibes_in(sonic, 0x12));
399         printk("  0x32: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x32));
400         printk("  0x13: P/C enable      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x13));
401         printk("  0x33: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x33));
402         printk("  0x14: U/D button      = 0x%02x  ", snd_sonicvibes_in(sonic, 0x14));
403         printk("  0x34: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x34));
404         printk("  0x15: revision        = 0x%02x  ", snd_sonicvibes_in(sonic, 0x15));
405         printk("  0x35: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x35));
406         printk("  0x16: ADC output ctrl = 0x%02x  ", snd_sonicvibes_in(sonic, 0x16));
407         printk("  0x36: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x36));
408         printk("  0x17: ---             = 0x%02x  ", snd_sonicvibes_in(sonic, 0x17));
409         printk("  0x37: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x37));
410         printk("  0x18: DMA A upper cnt = 0x%02x  ", snd_sonicvibes_in(sonic, 0x18));
411         printk("  0x38: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x38));
412         printk("  0x19: DMA A lower cnt = 0x%02x  ", snd_sonicvibes_in(sonic, 0x19));
413         printk("  0x39: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x39));
414         printk("  0x1a: ---             = 0x%02x  ", snd_sonicvibes_in(sonic, 0x1a));
415         printk("  0x3a: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3a));
416         printk("  0x1b: ---             = 0x%02x  ", snd_sonicvibes_in(sonic, 0x1b));
417         printk("  0x3b: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3b));
418         printk("  0x1c: DMA C upper cnt = 0x%02x  ", snd_sonicvibes_in(sonic, 0x1c));
419         printk("  0x3c: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3c));
420         printk("  0x1d: DMA C upper cnt = 0x%02x  ", snd_sonicvibes_in(sonic, 0x1d));
421         printk("  0x3d: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3d));
422         printk("  0x1e: PCM rate low    = 0x%02x  ", snd_sonicvibes_in(sonic, 0x1e));
423         printk("  0x3e: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3e));
424         printk("  0x1f: PCM rate high   = 0x%02x  ", snd_sonicvibes_in(sonic, 0x1f));
425         printk("  0x3f: ---             = 0x%02x\n", snd_sonicvibes_in(sonic, 0x3f));
426 }
427
428 #endif
429
430 static void snd_sonicvibes_setfmt(sonicvibes_t * sonic,
431                                   unsigned char mask,
432                                   unsigned char value)
433 {
434         unsigned long flags;
435
436         spin_lock_irqsave(&sonic->reg_lock, flags);
437         outb(SV_MCE | SV_IREG_DMA_DATA_FMT, SV_REG(sonic, INDEX));
438         if (mask) {
439                 sonic->format = inb(SV_REG(sonic, DATA));
440                 udelay(10);
441         }
442         sonic->format = (sonic->format & mask) | value;
443         outb(sonic->format, SV_REG(sonic, DATA));
444         udelay(10);
445         outb(0, SV_REG(sonic, INDEX));
446         udelay(10);
447         spin_unlock_irqrestore(&sonic->reg_lock, flags);
448 }
449
450 static void snd_sonicvibes_pll(unsigned int rate,
451                                unsigned int *res_r,
452                                unsigned int *res_m,
453                                unsigned int *res_n)
454 {
455         unsigned int r, m = 0, n = 0;
456         unsigned int xm, xn, xr, xd, metric = ~0U;
457
458         if (rate < 625000 / SV_ADCMULT)
459                 rate = 625000 / SV_ADCMULT;
460         if (rate > 150000000 / SV_ADCMULT)
461                 rate = 150000000 / SV_ADCMULT;
462         /* slight violation of specs, needed for continuous sampling rates */
463         for (r = 0; rate < 75000000 / SV_ADCMULT; r += 0x20, rate <<= 1);
464         for (xn = 3; xn < 33; xn++)     /* 35 */
465                 for (xm = 3; xm < 257; xm++) {
466                         xr = ((SV_REFFREQUENCY / SV_ADCMULT) * xm) / xn;
467                         if (xr >= rate)
468                                 xd = xr - rate;
469                         else
470                                 xd = rate - xr;
471                         if (xd < metric) {
472                                 metric = xd;
473                                 m = xm - 2;
474                                 n = xn - 2;
475                         }
476                 }
477         *res_r = r;
478         *res_m = m;
479         *res_n = n;
480 #if 0
481         printk("metric = %i, xm = %i, xn = %i\n", metric, xm, xn);
482         printk("pll: m = 0x%x, r = 0x%x, n = 0x%x\n", reg, m, r, n);
483 #endif
484 }
485
486 static void snd_sonicvibes_setpll(sonicvibes_t * sonic,
487                                   unsigned char reg,
488                                   unsigned int rate)
489 {
490         unsigned long flags;
491         unsigned int r, m, n;
492
493         snd_sonicvibes_pll(rate, &r, &m, &n);
494         if (sonic != NULL) {
495                 spin_lock_irqsave(&sonic->reg_lock, flags);
496                 snd_sonicvibes_out1(sonic, reg, m);
497                 snd_sonicvibes_out1(sonic, reg + 1, r | n);
498                 spin_unlock_irqrestore(&sonic->reg_lock, flags);
499         }
500 }
501
502 static void snd_sonicvibes_set_adc_rate(sonicvibes_t * sonic, unsigned int rate)
503 {
504         unsigned long flags;
505         unsigned int div;
506         unsigned char clock;
507
508         div = 48000 / rate;
509         if (div > 8)
510                 div = 8;
511         if ((48000 / div) == rate) {    /* use the alternate clock */
512                 clock = 0x10;
513         } else {                        /* use the PLL source */
514                 clock = 0x00;
515                 snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, rate);
516         }
517         spin_lock_irqsave(&sonic->reg_lock, flags);
518         snd_sonicvibes_out1(sonic, SV_IREG_ADC_ALT_RATE, (div - 1) << 4);
519         snd_sonicvibes_out1(sonic, SV_IREG_ADC_CLOCK, clock);
520         spin_unlock_irqrestore(&sonic->reg_lock, flags);
521 }
522
523 static int snd_sonicvibes_hw_constraint_dac_rate(snd_pcm_hw_params_t *params,
524                                                  snd_pcm_hw_rule_t *rule)
525 {
526         unsigned int rate, div, r, m, n;
527
528         if (hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min == 
529             hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->max) {
530                 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE)->min;
531                 div = 48000 / rate;
532                 if (div > 8)
533                         div = 8;
534                 if ((48000 / div) == rate) {
535                         params->rate_num = rate;
536                         params->rate_den = 1;
537                 } else {
538                         snd_sonicvibes_pll(rate, &r, &m, &n);
539                         snd_assert((SV_REFFREQUENCY % 16) == 0, return -EINVAL);
540                         snd_assert((SV_ADCMULT % 512) == 0, return -EINVAL);
541                         params->rate_num = (SV_REFFREQUENCY/16) * (n+2) * r;
542                         params->rate_den = (SV_ADCMULT/512) * (m+2);
543                 }
544         }
545         return 0;
546 }
547
548 static void snd_sonicvibes_set_dac_rate(sonicvibes_t * sonic, unsigned int rate)
549 {
550         unsigned int div;
551         unsigned long flags;
552
553         div = (rate * 65536 + SV_FULLRATE / 2) / SV_FULLRATE;
554         if (div > 65535)
555                 div = 65535;
556         spin_lock_irqsave(&sonic->reg_lock, flags);
557         snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_HIGH, div >> 8);
558         snd_sonicvibes_out1(sonic, SV_IREG_PCM_RATE_LOW, div);
559         spin_unlock_irqrestore(&sonic->reg_lock, flags);
560 }
561
562 static int snd_sonicvibes_trigger(sonicvibes_t * sonic, int what, int cmd)
563 {
564         int result = 0;
565
566         spin_lock(&sonic->reg_lock);
567         if (cmd == SNDRV_PCM_TRIGGER_START) {
568                 if (!(sonic->enable & what)) {
569                         sonic->enable |= what;
570                         snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
571                 }
572         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
573                 if (sonic->enable & what) {
574                         sonic->enable &= ~what;
575                         snd_sonicvibes_out1(sonic, SV_IREG_PC_ENABLE, sonic->enable);
576                 }
577         } else {
578                 result = -EINVAL;
579         }
580         spin_unlock(&sonic->reg_lock);
581         return result;
582 }
583
584 static irqreturn_t snd_sonicvibes_interrupt(int irq, void *dev_id, struct pt_regs *regs)
585 {
586         sonicvibes_t *sonic = dev_id;
587         unsigned char status;
588
589         status = inb(SV_REG(sonic, STATUS));
590         if (!(status & (SV_DMAA_IRQ | SV_DMAC_IRQ | SV_MIDI_IRQ)))
591                 return IRQ_NONE;
592         if (status == 0xff) {   /* failure */
593                 outb(sonic->irqmask = ~0, SV_REG(sonic, IRQMASK));
594                 snd_printk("IRQ failure - interrupts disabled!!\n");
595                 return IRQ_HANDLED;
596         }
597         if (sonic->pcm) {
598                 if (status & SV_DMAA_IRQ)
599                         snd_pcm_period_elapsed(sonic->playback_substream);
600                 if (status & SV_DMAC_IRQ)
601                         snd_pcm_period_elapsed(sonic->capture_substream);
602         }
603         if (sonic->rmidi) {
604                 if (status & SV_MIDI_IRQ)
605                         snd_mpu401_uart_interrupt(irq, sonic->rmidi->private_data, regs);
606         }
607         if (status & SV_UD_IRQ) {
608                 unsigned char udreg;
609                 int vol, oleft, oright, mleft, mright;
610
611                 spin_lock(&sonic->reg_lock);
612                 udreg = snd_sonicvibes_in1(sonic, SV_IREG_UD_BUTTON);
613                 vol = udreg & 0x3f;
614                 if (!(udreg & 0x40))
615                         vol = -vol;
616                 oleft = mleft = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ANALOG);
617                 oright = mright = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ANALOG);
618                 oleft &= 0x1f;
619                 oright &= 0x1f;
620                 oleft += vol;
621                 if (oleft < 0)
622                         oleft = 0;
623                 if (oleft > 0x1f)
624                         oleft = 0x1f;
625                 oright += vol;
626                 if (oright < 0)
627                         oright = 0;
628                 if (oright > 0x1f)
629                         oright = 0x1f;
630                 if (udreg & 0x80) {
631                         mleft ^= 0x80;
632                         mright ^= 0x80;
633                 }
634                 oleft |= mleft & 0x80;
635                 oright |= mright & 0x80;
636                 snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ANALOG, oleft);
637                 snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ANALOG, oright);
638                 spin_unlock(&sonic->reg_lock);
639                 snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_mute->id);
640                 snd_ctl_notify(sonic->card, SNDRV_CTL_EVENT_MASK_VALUE, &sonic->master_volume->id);
641         }
642         return IRQ_HANDLED;
643 }
644
645 /*
646  *  PCM part
647  */
648
649 static int snd_sonicvibes_playback_trigger(snd_pcm_substream_t * substream,
650                                            int cmd)
651 {
652         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
653         return snd_sonicvibes_trigger(sonic, 1, cmd);
654 }
655
656 static int snd_sonicvibes_capture_trigger(snd_pcm_substream_t * substream,
657                                           int cmd)
658 {
659         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
660         return snd_sonicvibes_trigger(sonic, 2, cmd);
661 }
662
663 static int snd_sonicvibes_hw_params(snd_pcm_substream_t * substream,
664                                     snd_pcm_hw_params_t * hw_params)
665 {
666         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
667 }
668
669 static int snd_sonicvibes_hw_free(snd_pcm_substream_t * substream)
670 {
671         return snd_pcm_lib_free_pages(substream);
672 }
673
674 static int snd_sonicvibes_playback_prepare(snd_pcm_substream_t * substream)
675 {
676         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
677         snd_pcm_runtime_t *runtime = substream->runtime;
678         unsigned char fmt = 0;
679         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
680         unsigned int count = snd_pcm_lib_period_bytes(substream);
681
682         sonic->p_dma_size = size;
683         count--;
684         if (runtime->channels > 1)
685                 fmt |= 1;
686         if (snd_pcm_format_width(runtime->format) == 16)
687                 fmt |= 2;
688         snd_sonicvibes_setfmt(sonic, ~3, fmt);
689         snd_sonicvibes_set_dac_rate(sonic, runtime->rate);
690         spin_lock_irq(&sonic->reg_lock);
691         snd_sonicvibes_setdmaa(sonic, runtime->dma_addr, size);
692         snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_UPPER, count >> 8);
693         snd_sonicvibes_out1(sonic, SV_IREG_DMA_A_LOWER, count);
694         spin_unlock_irq(&sonic->reg_lock);
695         return 0;
696 }
697
698 static int snd_sonicvibes_capture_prepare(snd_pcm_substream_t * substream)
699 {
700         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
701         snd_pcm_runtime_t *runtime = substream->runtime;
702         unsigned char fmt = 0;
703         unsigned int size = snd_pcm_lib_buffer_bytes(substream);
704         unsigned int count = snd_pcm_lib_period_bytes(substream);
705
706         sonic->c_dma_size = size;
707         count >>= 1;
708         count--;
709         if (runtime->channels > 1)
710                 fmt |= 0x10;
711         if (snd_pcm_format_width(runtime->format) == 16)
712                 fmt |= 0x20;
713         snd_sonicvibes_setfmt(sonic, ~0x30, fmt);
714         snd_sonicvibes_set_adc_rate(sonic, runtime->rate);
715         spin_lock_irq(&sonic->reg_lock);
716         snd_sonicvibes_setdmac(sonic, runtime->dma_addr, size);
717         snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_UPPER, count >> 8);
718         snd_sonicvibes_out1(sonic, SV_IREG_DMA_C_LOWER, count);
719         spin_unlock_irq(&sonic->reg_lock);
720         return 0;
721 }
722
723 static snd_pcm_uframes_t snd_sonicvibes_playback_pointer(snd_pcm_substream_t * substream)
724 {
725         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
726         size_t ptr;
727
728         if (!(sonic->enable & 1))
729                 return 0;
730         ptr = sonic->p_dma_size - snd_sonicvibes_getdmaa(sonic);
731         return bytes_to_frames(substream->runtime, ptr);
732 }
733
734 static snd_pcm_uframes_t snd_sonicvibes_capture_pointer(snd_pcm_substream_t * substream)
735 {
736         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
737         size_t ptr;
738         if (!(sonic->enable & 2))
739                 return 0;
740         ptr = sonic->c_dma_size - snd_sonicvibes_getdmac(sonic);
741         return bytes_to_frames(substream->runtime, ptr);
742 }
743
744 static snd_pcm_hardware_t snd_sonicvibes_playback =
745 {
746         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
747                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
748                                  SNDRV_PCM_INFO_MMAP_VALID),
749         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
750         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
751         .rate_min =             4000,
752         .rate_max =             48000,
753         .channels_min =         1,
754         .channels_max =         2,
755         .buffer_bytes_max =     (128*1024),
756         .period_bytes_min =     32,
757         .period_bytes_max =     (128*1024),
758         .periods_min =          1,
759         .periods_max =          1024,
760         .fifo_size =            0,
761 };
762
763 static snd_pcm_hardware_t snd_sonicvibes_capture =
764 {
765         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
766                                  SNDRV_PCM_INFO_BLOCK_TRANSFER |
767                                  SNDRV_PCM_INFO_MMAP_VALID),
768         .formats =              SNDRV_PCM_FMTBIT_U8 | SNDRV_PCM_FMTBIT_S16_LE,
769         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
770         .rate_min =             4000,
771         .rate_max =             48000,
772         .channels_min =         1,
773         .channels_max =         2,
774         .buffer_bytes_max =     (128*1024),
775         .period_bytes_min =     32,
776         .period_bytes_max =     (128*1024),
777         .periods_min =          1,
778         .periods_max =          1024,
779         .fifo_size =            0,
780 };
781
782 static int snd_sonicvibes_playback_open(snd_pcm_substream_t * substream)
783 {
784         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
785         snd_pcm_runtime_t *runtime = substream->runtime;
786
787         sonic->mode |= SV_MODE_PLAY;
788         sonic->playback_substream = substream;
789         runtime->hw = snd_sonicvibes_playback;
790         snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE, snd_sonicvibes_hw_constraint_dac_rate, NULL, SNDRV_PCM_HW_PARAM_RATE, -1);
791         return 0;
792 }
793
794 static int snd_sonicvibes_capture_open(snd_pcm_substream_t * substream)
795 {
796         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
797         snd_pcm_runtime_t *runtime = substream->runtime;
798
799         sonic->mode |= SV_MODE_CAPTURE;
800         sonic->capture_substream = substream;
801         runtime->hw = snd_sonicvibes_capture;
802         snd_pcm_hw_constraint_ratdens(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
803                                       &snd_sonicvibes_hw_constraints_adc_clock);
804         return 0;
805 }
806
807 static int snd_sonicvibes_playback_close(snd_pcm_substream_t * substream)
808 {
809         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
810
811         sonic->playback_substream = NULL;
812         sonic->mode &= ~SV_MODE_PLAY;
813         return 0;
814 }
815
816 static int snd_sonicvibes_capture_close(snd_pcm_substream_t * substream)
817 {
818         sonicvibes_t *sonic = snd_pcm_substream_chip(substream);
819
820         sonic->capture_substream = NULL;
821         sonic->mode &= ~SV_MODE_CAPTURE;
822         return 0;
823 }
824
825 static snd_pcm_ops_t snd_sonicvibes_playback_ops = {
826         .open =         snd_sonicvibes_playback_open,
827         .close =        snd_sonicvibes_playback_close,
828         .ioctl =        snd_pcm_lib_ioctl,
829         .hw_params =    snd_sonicvibes_hw_params,
830         .hw_free =      snd_sonicvibes_hw_free,
831         .prepare =      snd_sonicvibes_playback_prepare,
832         .trigger =      snd_sonicvibes_playback_trigger,
833         .pointer =      snd_sonicvibes_playback_pointer,
834 };
835
836 static snd_pcm_ops_t snd_sonicvibes_capture_ops = {
837         .open =         snd_sonicvibes_capture_open,
838         .close =        snd_sonicvibes_capture_close,
839         .ioctl =        snd_pcm_lib_ioctl,
840         .hw_params =    snd_sonicvibes_hw_params,
841         .hw_free =      snd_sonicvibes_hw_free,
842         .prepare =      snd_sonicvibes_capture_prepare,
843         .trigger =      snd_sonicvibes_capture_trigger,
844         .pointer =      snd_sonicvibes_capture_pointer,
845 };
846
847 static void snd_sonicvibes_pcm_free(snd_pcm_t *pcm)
848 {
849         sonicvibes_t *sonic = pcm->private_data;
850         sonic->pcm = NULL;
851         snd_pcm_lib_preallocate_free_for_all(pcm);
852 }
853
854 static int __devinit snd_sonicvibes_pcm(sonicvibes_t * sonic, int device, snd_pcm_t ** rpcm)
855 {
856         snd_pcm_t *pcm;
857         int err;
858
859         if ((err = snd_pcm_new(sonic->card, "s3_86c617", device, 1, 1, &pcm)) < 0)
860                 return err;
861         snd_assert(pcm != NULL, return -EINVAL);
862
863         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_sonicvibes_playback_ops);
864         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_sonicvibes_capture_ops);
865
866         pcm->private_data = sonic;
867         pcm->private_free = snd_sonicvibes_pcm_free;
868         pcm->info_flags = 0;
869         strcpy(pcm->name, "S3 SonicVibes");
870         sonic->pcm = pcm;
871
872         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV,
873                                               snd_dma_pci_data(sonic->pci), 64*1024, 128*1024);
874
875         if (rpcm)
876                 *rpcm = pcm;
877         return 0;
878 }
879
880 /*
881  *  Mixer part
882  */
883
884 #define SONICVIBES_MUX(xname, xindex) \
885 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
886   .info = snd_sonicvibes_info_mux, \
887   .get = snd_sonicvibes_get_mux, .put = snd_sonicvibes_put_mux }
888
889 static int snd_sonicvibes_info_mux(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
890 {
891         static char *texts[7] = {
892                 "CD", "PCM", "Aux1", "Line", "Aux0", "Mic", "Mix"
893         };
894
895         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
896         uinfo->count = 2;
897         uinfo->value.enumerated.items = 7;
898         if (uinfo->value.enumerated.item >= 7)
899                 uinfo->value.enumerated.item = 6;
900         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
901         return 0;
902 }
903
904 static int snd_sonicvibes_get_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
905 {
906         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
907         
908         spin_lock_irq(&sonic->reg_lock);
909         ucontrol->value.enumerated.item[0] = ((snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
910         ucontrol->value.enumerated.item[1] = ((snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC) & SV_RECSRC_OUT) >> 5) - 1;
911         spin_unlock_irq(&sonic->reg_lock);
912         return 0;
913 }
914
915 static int snd_sonicvibes_put_mux(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
916 {
917         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
918         unsigned short left, right, oval1, oval2;
919         int change;
920         
921         if (ucontrol->value.enumerated.item[0] >= 7 ||
922             ucontrol->value.enumerated.item[1] >= 7)
923                 return -EINVAL;
924         left = (ucontrol->value.enumerated.item[0] + 1) << 5;
925         right = (ucontrol->value.enumerated.item[1] + 1) << 5;
926         spin_lock_irq(&sonic->reg_lock);
927         oval1 = snd_sonicvibes_in1(sonic, SV_IREG_LEFT_ADC);
928         oval2 = snd_sonicvibes_in1(sonic, SV_IREG_RIGHT_ADC);
929         left = (oval1 & ~SV_RECSRC_OUT) | left;
930         right = (oval2 & ~SV_RECSRC_OUT) | right;
931         change = left != oval1 || right != oval2;
932         snd_sonicvibes_out1(sonic, SV_IREG_LEFT_ADC, left);
933         snd_sonicvibes_out1(sonic, SV_IREG_RIGHT_ADC, right);
934         spin_unlock_irq(&sonic->reg_lock);
935         return change;
936 }
937
938 #define SONICVIBES_SINGLE(xname, xindex, reg, shift, mask, invert) \
939 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
940   .info = snd_sonicvibes_info_single, \
941   .get = snd_sonicvibes_get_single, .put = snd_sonicvibes_put_single, \
942   .private_value = reg | (shift << 8) | (mask << 16) | (invert << 24) }
943
944 static int snd_sonicvibes_info_single(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
945 {
946         int mask = (kcontrol->private_value >> 16) & 0xff;
947
948         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
949         uinfo->count = 1;
950         uinfo->value.integer.min = 0;
951         uinfo->value.integer.max = mask;
952         return 0;
953 }
954
955 static int snd_sonicvibes_get_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
956 {
957         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
958         int reg = kcontrol->private_value & 0xff;
959         int shift = (kcontrol->private_value >> 8) & 0xff;
960         int mask = (kcontrol->private_value >> 16) & 0xff;
961         int invert = (kcontrol->private_value >> 24) & 0xff;
962         
963         spin_lock_irq(&sonic->reg_lock);
964         ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, reg)>> shift) & mask;
965         spin_unlock_irq(&sonic->reg_lock);
966         if (invert)
967                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
968         return 0;
969 }
970
971 static int snd_sonicvibes_put_single(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
972 {
973         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
974         int reg = kcontrol->private_value & 0xff;
975         int shift = (kcontrol->private_value >> 8) & 0xff;
976         int mask = (kcontrol->private_value >> 16) & 0xff;
977         int invert = (kcontrol->private_value >> 24) & 0xff;
978         int change;
979         unsigned short val, oval;
980         
981         val = (ucontrol->value.integer.value[0] & mask);
982         if (invert)
983                 val = mask - val;
984         val <<= shift;
985         spin_lock_irq(&sonic->reg_lock);
986         oval = snd_sonicvibes_in1(sonic, reg);
987         val = (oval & ~(mask << shift)) | val;
988         change = val != oval;
989         snd_sonicvibes_out1(sonic, reg, val);
990         spin_unlock_irq(&sonic->reg_lock);
991         return change;
992 }
993
994 #define SONICVIBES_DOUBLE(xname, xindex, left_reg, right_reg, shift_left, shift_right, mask, invert) \
995 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
996   .info = snd_sonicvibes_info_double, \
997   .get = snd_sonicvibes_get_double, .put = snd_sonicvibes_put_double, \
998   .private_value = left_reg | (right_reg << 8) | (shift_left << 16) | (shift_right << 19) | (mask << 24) | (invert << 22) }
999
1000 static int snd_sonicvibes_info_double(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
1001 {
1002         int mask = (kcontrol->private_value >> 24) & 0xff;
1003
1004         uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
1005         uinfo->count = 2;
1006         uinfo->value.integer.min = 0;
1007         uinfo->value.integer.max = mask;
1008         return 0;
1009 }
1010
1011 static int snd_sonicvibes_get_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1012 {
1013         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
1014         int left_reg = kcontrol->private_value & 0xff;
1015         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1016         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1017         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1018         int mask = (kcontrol->private_value >> 24) & 0xff;
1019         int invert = (kcontrol->private_value >> 22) & 1;
1020         
1021         spin_lock_irq(&sonic->reg_lock);
1022         ucontrol->value.integer.value[0] = (snd_sonicvibes_in1(sonic, left_reg) >> shift_left) & mask;
1023         ucontrol->value.integer.value[1] = (snd_sonicvibes_in1(sonic, right_reg) >> shift_right) & mask;
1024         spin_unlock_irq(&sonic->reg_lock);
1025         if (invert) {
1026                 ucontrol->value.integer.value[0] = mask - ucontrol->value.integer.value[0];
1027                 ucontrol->value.integer.value[1] = mask - ucontrol->value.integer.value[1];
1028         }
1029         return 0;
1030 }
1031
1032 static int snd_sonicvibes_put_double(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t * ucontrol)
1033 {
1034         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
1035         int left_reg = kcontrol->private_value & 0xff;
1036         int right_reg = (kcontrol->private_value >> 8) & 0xff;
1037         int shift_left = (kcontrol->private_value >> 16) & 0x07;
1038         int shift_right = (kcontrol->private_value >> 19) & 0x07;
1039         int mask = (kcontrol->private_value >> 24) & 0xff;
1040         int invert = (kcontrol->private_value >> 22) & 1;
1041         int change;
1042         unsigned short val1, val2, oval1, oval2;
1043         
1044         val1 = ucontrol->value.integer.value[0] & mask;
1045         val2 = ucontrol->value.integer.value[1] & mask;
1046         if (invert) {
1047                 val1 = mask - val1;
1048                 val2 = mask - val2;
1049         }
1050         val1 <<= shift_left;
1051         val2 <<= shift_right;
1052         spin_lock_irq(&sonic->reg_lock);
1053         oval1 = snd_sonicvibes_in1(sonic, left_reg);
1054         oval2 = snd_sonicvibes_in1(sonic, right_reg);
1055         val1 = (oval1 & ~(mask << shift_left)) | val1;
1056         val2 = (oval2 & ~(mask << shift_right)) | val2;
1057         change = val1 != oval1 || val2 != oval2;
1058         snd_sonicvibes_out1(sonic, left_reg, val1);
1059         snd_sonicvibes_out1(sonic, right_reg, val2);
1060         spin_unlock_irq(&sonic->reg_lock);
1061         return change;
1062 }
1063
1064 static snd_kcontrol_new_t snd_sonicvibes_controls[] __devinitdata = {
1065 SONICVIBES_DOUBLE("Capture Volume", 0, SV_IREG_LEFT_ADC, SV_IREG_RIGHT_ADC, 0, 0, 15, 0),
1066 SONICVIBES_DOUBLE("Aux Playback Switch", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 7, 7, 1, 1),
1067 SONICVIBES_DOUBLE("Aux Playback Volume", 0, SV_IREG_LEFT_AUX1, SV_IREG_RIGHT_AUX1, 0, 0, 31, 1),
1068 SONICVIBES_DOUBLE("CD Playback Switch", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 7, 7, 1, 1),
1069 SONICVIBES_DOUBLE("CD Playback Volume", 0, SV_IREG_LEFT_CD, SV_IREG_RIGHT_CD, 0, 0, 31, 1),
1070 SONICVIBES_DOUBLE("Line Playback Switch", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 7, 7, 1, 1),
1071 SONICVIBES_DOUBLE("Line Playback Volume", 0, SV_IREG_LEFT_LINE, SV_IREG_RIGHT_LINE, 0, 0, 31, 1),
1072 SONICVIBES_SINGLE("Mic Playback Switch", 0, SV_IREG_MIC, 7, 1, 1),
1073 SONICVIBES_SINGLE("Mic Playback Volume", 0, SV_IREG_MIC, 0, 15, 1),
1074 SONICVIBES_SINGLE("Mic Boost", 0, SV_IREG_LEFT_ADC, 4, 1, 0),
1075 SONICVIBES_DOUBLE("Synth Playback Switch", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 7, 7, 1, 1),
1076 SONICVIBES_DOUBLE("Synth Playback Volume", 0, SV_IREG_LEFT_SYNTH, SV_IREG_RIGHT_SYNTH, 0, 0, 31, 1),
1077 SONICVIBES_DOUBLE("Aux Playback Switch", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 7, 7, 1, 1),
1078 SONICVIBES_DOUBLE("Aux Playback Volume", 1, SV_IREG_LEFT_AUX2, SV_IREG_RIGHT_AUX2, 0, 0, 31, 1),
1079 SONICVIBES_DOUBLE("Master Playback Switch", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 7, 7, 1, 1),
1080 SONICVIBES_DOUBLE("Master Playback Volume", 0, SV_IREG_LEFT_ANALOG, SV_IREG_RIGHT_ANALOG, 0, 0, 31, 1),
1081 SONICVIBES_DOUBLE("PCM Playback Switch", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 7, 7, 1, 1),
1082 SONICVIBES_DOUBLE("PCM Playback Volume", 0, SV_IREG_LEFT_PCM, SV_IREG_RIGHT_PCM, 0, 0, 63, 1),
1083 SONICVIBES_SINGLE("Loopback Capture Switch", 0, SV_IREG_ADC_OUTPUT_CTRL, 0, 1, 0),
1084 SONICVIBES_SINGLE("Loopback Capture Volume", 0, SV_IREG_ADC_OUTPUT_CTRL, 2, 63, 1),
1085 SONICVIBES_MUX("Capture Source", 0)
1086 };
1087
1088 static void snd_sonicvibes_master_free(snd_kcontrol_t *kcontrol)
1089 {
1090         sonicvibes_t *sonic = snd_kcontrol_chip(kcontrol);
1091         sonic->master_mute = NULL;
1092         sonic->master_volume = NULL;
1093 }
1094
1095 static int __devinit snd_sonicvibes_mixer(sonicvibes_t * sonic)
1096 {
1097         snd_card_t *card;
1098         snd_kcontrol_t *kctl;
1099         unsigned int idx;
1100         int err;
1101
1102         snd_assert(sonic != NULL && sonic->card != NULL, return -EINVAL);
1103         card = sonic->card;
1104         strcpy(card->mixername, "S3 SonicVibes");
1105
1106         for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_controls); idx++) {
1107                 if ((err = snd_ctl_add(card, kctl = snd_ctl_new1(&snd_sonicvibes_controls[idx], sonic))) < 0)
1108                         return err;
1109                 switch (idx) {
1110                 case 0:
1111                 case 1: kctl->private_free = snd_sonicvibes_master_free; break;
1112                 }
1113         }
1114         return 0;
1115 }
1116
1117 /*
1118
1119  */
1120
1121 static void snd_sonicvibes_proc_read(snd_info_entry_t *entry, 
1122                                      snd_info_buffer_t * buffer)
1123 {
1124         sonicvibes_t *sonic = entry->private_data;
1125         unsigned char tmp;
1126
1127         tmp = sonic->srs_space & 0x0f;
1128         snd_iprintf(buffer, "SRS 3D           : %s\n",
1129                     sonic->srs_space & 0x80 ? "off" : "on");
1130         snd_iprintf(buffer, "SRS Space        : %s\n",
1131                     tmp == 0x00 ? "100%" :
1132                     tmp == 0x01 ? "75%" :
1133                     tmp == 0x02 ? "50%" :
1134                     tmp == 0x03 ? "25%" : "0%");
1135         tmp = sonic->srs_center & 0x0f;
1136         snd_iprintf(buffer, "SRS Center       : %s\n",
1137                     tmp == 0x00 ? "100%" :
1138                     tmp == 0x01 ? "75%" :
1139                     tmp == 0x02 ? "50%" :
1140                     tmp == 0x03 ? "25%" : "0%");
1141         tmp = sonic->wave_source & 0x03;
1142         snd_iprintf(buffer, "WaveTable Source : %s\n",
1143                     tmp == 0x00 ? "on-board ROM" :
1144                     tmp == 0x01 ? "PCI bus" : "on-board ROM + PCI bus");
1145         tmp = sonic->mpu_switch;
1146         snd_iprintf(buffer, "Onboard synth    : %s\n", tmp & 0x01 ? "on" : "off");
1147         snd_iprintf(buffer, "Ext. Rx to synth : %s\n", tmp & 0x02 ? "on" : "off");
1148         snd_iprintf(buffer, "MIDI to ext. Tx  : %s\n", tmp & 0x04 ? "on" : "off");
1149 }
1150
1151 static void __devinit snd_sonicvibes_proc_init(sonicvibes_t * sonic)
1152 {
1153         snd_info_entry_t *entry;
1154
1155         if (! snd_card_proc_new(sonic->card, "sonicvibes", &entry))
1156                 snd_info_set_text_ops(entry, sonic, 1024, snd_sonicvibes_proc_read);
1157 }
1158
1159 /*
1160
1161  */
1162
1163 #ifdef SUPPORT_JOYSTICK
1164 static snd_kcontrol_new_t snd_sonicvibes_game_control __devinitdata =
1165 SONICVIBES_SINGLE("Joystick Speed", 0, SV_IREG_GAME_PORT, 1, 15, 0);
1166
1167 static int __devinit snd_sonicvibes_create_gameport(sonicvibes_t *sonic)
1168 {
1169         struct gameport *gp;
1170
1171         sonic->gameport = gp = gameport_allocate_port();
1172         if (!gp) {
1173                 printk(KERN_ERR "sonicvibes: cannot allocate memory for gameport\n");
1174                 return -ENOMEM;
1175         }
1176
1177         gameport_set_name(gp, "SonicVibes Gameport");
1178         gameport_set_phys(gp, "pci%s/gameport0", pci_name(sonic->pci));
1179         gameport_set_dev_parent(gp, &sonic->pci->dev);
1180         gp->io = sonic->game_port;
1181
1182         gameport_register_port(gp);
1183
1184         snd_ctl_add(sonic->card, snd_ctl_new1(&snd_sonicvibes_game_control, sonic));
1185
1186         return 0;
1187 }
1188
1189 static void snd_sonicvibes_free_gameport(sonicvibes_t *sonic)
1190 {
1191         if (sonic->gameport) {
1192                 gameport_unregister_port(sonic->gameport);
1193                 sonic->gameport = NULL;
1194         }
1195 }
1196 #else
1197 static inline int snd_sonicvibes_create_gameport(sonicvibes_t *sonic) { return -ENOSYS; }
1198 static inline void snd_sonicvibes_free_gameport(sonicvibes_t *sonic) { }
1199 #endif
1200
1201 static int snd_sonicvibes_free(sonicvibes_t *sonic)
1202 {
1203         snd_sonicvibes_free_gameport(sonic);
1204         pci_write_config_dword(sonic->pci, 0x40, sonic->dmaa_port);
1205         pci_write_config_dword(sonic->pci, 0x48, sonic->dmac_port);
1206         if (sonic->irq >= 0)
1207                 free_irq(sonic->irq, (void *)sonic);
1208         if (sonic->res_dmaa) {
1209                 release_resource(sonic->res_dmaa);
1210                 kfree_nocheck(sonic->res_dmaa);
1211         }
1212         if (sonic->res_dmac) {
1213                 release_resource(sonic->res_dmac);
1214                 kfree_nocheck(sonic->res_dmac);
1215         }
1216         pci_release_regions(sonic->pci);
1217         pci_disable_device(sonic->pci);
1218         kfree(sonic);
1219         return 0;
1220 }
1221
1222 static int snd_sonicvibes_dev_free(snd_device_t *device)
1223 {
1224         sonicvibes_t *sonic = device->device_data;
1225         return snd_sonicvibes_free(sonic);
1226 }
1227
1228 static int __devinit snd_sonicvibes_create(snd_card_t * card,
1229                                         struct pci_dev *pci,
1230                                         int reverb,
1231                                         int mge,
1232                                         sonicvibes_t ** rsonic)
1233 {
1234         sonicvibes_t *sonic;
1235         unsigned int dmaa, dmac;
1236         int err;
1237         static snd_device_ops_t ops = {
1238                 .dev_free =     snd_sonicvibes_dev_free,
1239         };
1240
1241         *rsonic = NULL;
1242         /* enable PCI device */
1243         if ((err = pci_enable_device(pci)) < 0)
1244                 return err;
1245         /* check, if we can restrict PCI DMA transfers to 24 bits */
1246         if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
1247             pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
1248                 snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
1249                 pci_disable_device(pci);
1250                 return -ENXIO;
1251         }
1252
1253         sonic = kzalloc(sizeof(*sonic), GFP_KERNEL);
1254         if (sonic == NULL) {
1255                 pci_disable_device(pci);
1256                 return -ENOMEM;
1257         }
1258         spin_lock_init(&sonic->reg_lock);
1259         sonic->card = card;
1260         sonic->pci = pci;
1261         sonic->irq = -1;
1262
1263         if ((err = pci_request_regions(pci, "S3 SonicVibes")) < 0) {
1264                 kfree(sonic);
1265                 pci_disable_device(pci);
1266                 return err;
1267         }
1268
1269         sonic->sb_port = pci_resource_start(pci, 0);
1270         sonic->enh_port = pci_resource_start(pci, 1);
1271         sonic->synth_port = pci_resource_start(pci, 2);
1272         sonic->midi_port = pci_resource_start(pci, 3);
1273         sonic->game_port = pci_resource_start(pci, 4);
1274
1275         if (request_irq(pci->irq, snd_sonicvibes_interrupt, SA_INTERRUPT|SA_SHIRQ, "S3 SonicVibes", (void *)sonic)) {
1276                 snd_printk("unable to grab IRQ %d\n", pci->irq);
1277                 snd_sonicvibes_free(sonic);
1278                 return -EBUSY;
1279         }
1280         sonic->irq = pci->irq;
1281
1282         pci_read_config_dword(pci, 0x40, &dmaa);
1283         pci_read_config_dword(pci, 0x48, &dmac);
1284         dmaio &= ~0x0f;
1285         dmaa &= ~0x0f;
1286         dmac &= ~0x0f;
1287         if (!dmaa) {
1288                 dmaa = dmaio;
1289                 dmaio += 0x10;
1290                 snd_printk("BIOS did not allocate DDMA channel A i/o, allocated at 0x%x\n", dmaa);
1291         }
1292         if (!dmac) {
1293                 dmac = dmaio;
1294                 dmaio += 0x10;
1295                 snd_printk("BIOS did not allocate DDMA channel C i/o, allocated at 0x%x\n", dmac);
1296         }
1297         pci_write_config_dword(pci, 0x40, dmaa);
1298         pci_write_config_dword(pci, 0x48, dmac);
1299
1300         if ((sonic->res_dmaa = request_region(dmaa, 0x10, "S3 SonicVibes DDMA-A")) == NULL) {
1301                 snd_sonicvibes_free(sonic);
1302                 snd_printk("unable to grab DDMA-A port at 0x%x-0x%x\n", dmaa, dmaa + 0x10 - 1);
1303                 return -EBUSY;
1304         }
1305         if ((sonic->res_dmac = request_region(dmac, 0x10, "S3 SonicVibes DDMA-C")) == NULL) {
1306                 snd_sonicvibes_free(sonic);
1307                 snd_printk("unable to grab DDMA-C port at 0x%x-0x%x\n", dmac, dmac + 0x10 - 1);
1308                 return -EBUSY;
1309         }
1310
1311         pci_read_config_dword(pci, 0x40, &sonic->dmaa_port);
1312         pci_read_config_dword(pci, 0x48, &sonic->dmac_port);
1313         sonic->dmaa_port &= ~0x0f;
1314         sonic->dmac_port &= ~0x0f;
1315         pci_write_config_dword(pci, 0x40, sonic->dmaa_port | 9);        /* enable + enhanced */
1316         pci_write_config_dword(pci, 0x48, sonic->dmac_port | 9);        /* enable */
1317         /* ok.. initialize S3 SonicVibes chip */
1318         outb(SV_RESET, SV_REG(sonic, CONTROL));         /* reset chip */
1319         udelay(100);
1320         outb(0, SV_REG(sonic, CONTROL));        /* release reset */
1321         udelay(100);
1322         outb(SV_ENHANCED | SV_INTA | (reverb ? SV_REVERB : 0), SV_REG(sonic, CONTROL));
1323         inb(SV_REG(sonic, STATUS));     /* clear IRQs */
1324 #if 1
1325         snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0);       /* drive current 16mA */
1326 #else
1327         snd_sonicvibes_out(sonic, SV_IREG_DRIVE_CTRL, 0x40);    /* drive current 8mA */
1328 #endif
1329         snd_sonicvibes_out(sonic, SV_IREG_PC_ENABLE, sonic->enable = 0);        /* disable playback & capture */
1330         outb(sonic->irqmask = ~(SV_DMAA_MASK | SV_DMAC_MASK | SV_UD_MASK), SV_REG(sonic, IRQMASK));
1331         inb(SV_REG(sonic, STATUS));     /* clear IRQs */
1332         snd_sonicvibes_out(sonic, SV_IREG_ADC_CLOCK, 0);        /* use PLL as clock source */
1333         snd_sonicvibes_out(sonic, SV_IREG_ANALOG_POWER, 0);     /* power up analog parts */
1334         snd_sonicvibes_out(sonic, SV_IREG_DIGITAL_POWER, 0);    /* power up digital parts */
1335         snd_sonicvibes_setpll(sonic, SV_IREG_ADC_PLL, 8000);
1336         snd_sonicvibes_out(sonic, SV_IREG_SRS_SPACE, sonic->srs_space = 0x80);  /* SRS space off */
1337         snd_sonicvibes_out(sonic, SV_IREG_SRS_CENTER, sonic->srs_center = 0x00);/* SRS center off */
1338         snd_sonicvibes_out(sonic, SV_IREG_MPU401, sonic->mpu_switch = 0x05);    /* MPU-401 switch */
1339         snd_sonicvibes_out(sonic, SV_IREG_WAVE_SOURCE, sonic->wave_source = 0x00);      /* onboard ROM */
1340         snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_LOW, (8000 * 65536 / SV_FULLRATE) & 0xff);
1341         snd_sonicvibes_out(sonic, SV_IREG_PCM_RATE_HIGH, ((8000 * 65536 / SV_FULLRATE) >> 8) & 0xff);
1342         snd_sonicvibes_out(sonic, SV_IREG_LEFT_ADC, mge ? 0xd0 : 0xc0);
1343         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ADC, 0xc0);
1344         snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX1, 0x9f);
1345         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX1, 0x9f);
1346         snd_sonicvibes_out(sonic, SV_IREG_LEFT_CD, 0x9f);
1347         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_CD, 0x9f);
1348         snd_sonicvibes_out(sonic, SV_IREG_LEFT_LINE, 0x9f);
1349         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_LINE, 0x9f);
1350         snd_sonicvibes_out(sonic, SV_IREG_MIC, 0x8f);
1351         snd_sonicvibes_out(sonic, SV_IREG_LEFT_SYNTH, 0x9f);
1352         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_SYNTH, 0x9f);
1353         snd_sonicvibes_out(sonic, SV_IREG_LEFT_AUX2, 0x9f);
1354         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_AUX2, 0x9f);
1355         snd_sonicvibes_out(sonic, SV_IREG_LEFT_ANALOG, 0x9f);
1356         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_ANALOG, 0x9f);
1357         snd_sonicvibes_out(sonic, SV_IREG_LEFT_PCM, 0xbf);
1358         snd_sonicvibes_out(sonic, SV_IREG_RIGHT_PCM, 0xbf);
1359         snd_sonicvibes_out(sonic, SV_IREG_ADC_OUTPUT_CTRL, 0xfc);
1360 #if 0
1361         snd_sonicvibes_debug(sonic);
1362 #endif
1363         sonic->revision = snd_sonicvibes_in(sonic, SV_IREG_REVISION);
1364
1365         if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, sonic, &ops)) < 0) {
1366                 snd_sonicvibes_free(sonic);
1367                 return err;
1368         }
1369
1370         snd_sonicvibes_proc_init(sonic);
1371
1372         snd_card_set_dev(card, &pci->dev);
1373
1374         *rsonic = sonic;
1375         return 0;
1376 }
1377
1378 /*
1379  *  MIDI section
1380  */
1381
1382 static snd_kcontrol_new_t snd_sonicvibes_midi_controls[] __devinitdata = {
1383 SONICVIBES_SINGLE("SonicVibes Wave Source RAM", 0, SV_IREG_WAVE_SOURCE, 0, 1, 0),
1384 SONICVIBES_SINGLE("SonicVibes Wave Source RAM+ROM", 0, SV_IREG_WAVE_SOURCE, 1, 1, 0),
1385 SONICVIBES_SINGLE("SonicVibes Onboard Synth", 0, SV_IREG_MPU401, 0, 1, 0),
1386 SONICVIBES_SINGLE("SonicVibes External Rx to Synth", 0, SV_IREG_MPU401, 1, 1, 0),
1387 SONICVIBES_SINGLE("SonicVibes External Tx", 0, SV_IREG_MPU401, 2, 1, 0)
1388 };
1389
1390 static int snd_sonicvibes_midi_input_open(mpu401_t * mpu)
1391 {
1392         sonicvibes_t *sonic = mpu->private_data;
1393         outb(sonic->irqmask &= ~SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
1394         return 0;
1395 }
1396
1397 static void snd_sonicvibes_midi_input_close(mpu401_t * mpu)
1398 {
1399         sonicvibes_t *sonic = mpu->private_data;
1400         outb(sonic->irqmask |= SV_MIDI_MASK, SV_REG(sonic, IRQMASK));
1401 }
1402
1403 static int __devinit snd_sonicvibes_midi(sonicvibes_t * sonic, snd_rawmidi_t * rmidi)
1404 {
1405         mpu401_t * mpu = rmidi->private_data;
1406         snd_card_t *card = sonic->card;
1407         snd_rawmidi_str_t *dir;
1408         unsigned int idx;
1409         int err;
1410
1411         mpu->private_data = sonic;
1412         mpu->open_input = snd_sonicvibes_midi_input_open;
1413         mpu->close_input = snd_sonicvibes_midi_input_close;
1414         dir = &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
1415         for (idx = 0; idx < ARRAY_SIZE(snd_sonicvibes_midi_controls); idx++)
1416                 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_sonicvibes_midi_controls[idx], sonic))) < 0)
1417                         return err;
1418         return 0;
1419 }
1420
1421 static int __devinit snd_sonic_probe(struct pci_dev *pci,
1422                                      const struct pci_device_id *pci_id)
1423 {
1424         static int dev;
1425         snd_card_t *card;
1426         sonicvibes_t *sonic;
1427         snd_rawmidi_t *midi_uart;
1428         opl3_t *opl3;
1429         int idx, err;
1430
1431         if (dev >= SNDRV_CARDS)
1432                 return -ENODEV;
1433         if (!enable[dev]) {
1434                 dev++;
1435                 return -ENOENT;
1436         }
1437  
1438         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 0);
1439         if (card == NULL)
1440                 return -ENOMEM;
1441         for (idx = 0; idx < 5; idx++) {
1442                 if (pci_resource_start(pci, idx) == 0 ||
1443                     !(pci_resource_flags(pci, idx) & IORESOURCE_IO)) {
1444                         snd_card_free(card);
1445                         return -ENODEV;
1446                 }
1447         }
1448         if ((err = snd_sonicvibes_create(card, pci,
1449                                          reverb[dev] ? 1 : 0,
1450                                          mge[dev] ? 1 : 0,
1451                                          &sonic)) < 0) {
1452                 snd_card_free(card);
1453                 return err;
1454         }
1455
1456         strcpy(card->driver, "SonicVibes");
1457         strcpy(card->shortname, "S3 SonicVibes");
1458         sprintf(card->longname, "%s rev %i at 0x%lx, irq %i",
1459                 card->shortname,
1460                 sonic->revision,
1461                 pci_resource_start(pci, 1),
1462                 sonic->irq);
1463
1464         if ((err = snd_sonicvibes_pcm(sonic, 0, NULL)) < 0) {
1465                 snd_card_free(card);
1466                 return err;
1467         }
1468         if ((err = snd_sonicvibes_mixer(sonic)) < 0) {
1469                 snd_card_free(card);
1470                 return err;
1471         }
1472         if ((err = snd_mpu401_uart_new(card, 0, MPU401_HW_SONICVIBES,
1473                                        sonic->midi_port, 1,
1474                                        sonic->irq, 0,
1475                                        &midi_uart)) < 0) {
1476                 snd_card_free(card);
1477                 return err;
1478         }
1479         snd_sonicvibes_midi(sonic, midi_uart);
1480         if ((err = snd_opl3_create(card, sonic->synth_port,
1481                                    sonic->synth_port + 2,
1482                                    OPL3_HW_OPL3_SV, 1, &opl3)) < 0) {
1483                 snd_card_free(card);
1484                 return err;
1485         }
1486         if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
1487                 snd_card_free(card);
1488                 return err;
1489         }
1490
1491         snd_sonicvibes_create_gameport(sonic);
1492
1493         if ((err = snd_card_register(card)) < 0) {
1494                 snd_card_free(card);
1495                 return err;
1496         }
1497         
1498         pci_set_drvdata(pci, card);
1499         dev++;
1500         return 0;
1501 }
1502
1503 static void __devexit snd_sonic_remove(struct pci_dev *pci)
1504 {
1505         snd_card_free(pci_get_drvdata(pci));
1506         pci_set_drvdata(pci, NULL);
1507 }
1508
1509 static struct pci_driver driver = {
1510         .name = "S3 SonicVibes",
1511         .owner = THIS_MODULE,
1512         .id_table = snd_sonic_ids,
1513         .probe = snd_sonic_probe,
1514         .remove = __devexit_p(snd_sonic_remove),
1515 };
1516
1517 static int __init alsa_card_sonicvibes_init(void)
1518 {
1519         return pci_register_driver(&driver);
1520 }
1521
1522 static void __exit alsa_card_sonicvibes_exit(void)
1523 {
1524         pci_unregister_driver(&driver);
1525 }
1526
1527 module_init(alsa_card_sonicvibes_init)
1528 module_exit(alsa_card_sonicvibes_exit)