ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / pci / als4000.c
1 /*
2  *  card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
3  *  Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
4  *                        Jaroslav Kysela <perex@suse.cz>
5  *  Copyright (C) 2002 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
6  *
7  *  Framework borrowed from Massimo Piccioni's card-als100.c.
8  *
9  * NOTES
10  *
11  *  Since Avance does not provide any meaningful documentation, and I
12  *  bought an ALS4000 based soundcard, I was forced to base this driver
13  *  on reverse engineering.
14  *
15  *  Note: this is no longer true. Pretty verbose chip docu (ALS4000a.PDF)
16  *  can be found on the ALSA web site.
17  *
18  *  The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
19  *  ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport 
20  *  interface. These subsystems can be mapped into ISA io-port space, 
21  *  using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ 
22  *  services to the subsystems.
23  * 
24  * While ALS4000 is very similar to a SoundBlaster, the differences in
25  * DMA and capturing require more changes to the SoundBlaster than
26  * desirable, so I made this separate driver.
27  * 
28  * The ALS4000 can do real full duplex playback/capture.
29  *
30  * FMDAC:
31  * - 0x4f -> port 0x14
32  * - port 0x15 |= 1
33  *
34  * Enable/disable 3D sound:
35  * - 0x50 -> port 0x14
36  * - change bit 6 (0x40) of port 0x15
37  *
38  * Set QSound:
39  * - 0xdb -> port 0x14
40  * - set port 0x15:
41  *   0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
42  *
43  * Set KSound:
44  * - value -> some port 0x0c0d
45  *
46  *  This program is free software; you can redistribute it and/or modify
47  *  it under the terms of the GNU General Public License as published by
48  *  the Free Software Foundation; either version 2 of the License, or
49  *  (at your option) any later version.
50  *
51  *  This program is distributed in the hope that it will be useful,
52  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
53  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
54  *  GNU General Public License for more details.
55
56  *  You should have received a copy of the GNU General Public License
57  *  along with this program; if not, write to the Free Software
58  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
59  */
60
61 #include <sound/driver.h>
62 #include <asm/io.h>
63 #include <linux/init.h>
64 #include <linux/pci.h>
65 #include <linux/slab.h>
66 #include <linux/gameport.h>
67 #include <sound/core.h>
68 #include <sound/pcm.h>
69 #include <sound/rawmidi.h>
70 #include <sound/mpu401.h>
71 #include <sound/opl3.h>
72 #include <sound/sb.h>
73 #define SNDRV_GET_ID
74 #include <sound/initval.h>
75
76 MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
77 MODULE_DESCRIPTION("Avance Logic ALS4000");
78 MODULE_LICENSE("GPL");
79 MODULE_CLASSES("{sound}");
80 MODULE_DEVICES("{{Avance Logic,ALS4000}}");
81
82 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
83 #define SUPPORT_JOYSTICK 1
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 #ifdef SUPPORT_JOYSTICK
90 static int joystick_port[SNDRV_CARDS];
91 #endif
92
93 MODULE_PARM(index, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
94 MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
95 MODULE_PARM_SYNTAX(index, SNDRV_INDEX_DESC);
96 MODULE_PARM(id, "1-" __MODULE_STRING(SNDRV_CARDS) "s");
97 MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
98 MODULE_PARM_SYNTAX(id, SNDRV_ID_DESC);
99 MODULE_PARM(enable, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
100 MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
101 MODULE_PARM_SYNTAX(enable, SNDRV_INDEX_DESC);
102 #ifdef SUPPORT_JOYSTICK
103 MODULE_PARM(joystick_port, "1-" __MODULE_STRING(SNDRV_CARDS) "i");
104 MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
105 MODULE_PARM_SYNTAX(joystick_port, SNDRV_ENABLED);
106 #endif
107
108 #define chip_t sb_t
109
110 typedef struct {
111         unsigned long gcr;
112         struct resource *res_gcr;
113 #ifdef SUPPORT_JOYSTICK
114         struct gameport gameport;
115         struct resource *res_joystick;
116 #endif
117 } snd_card_als4000_t;
118
119 static struct pci_device_id snd_als4000_ids[] = {
120         { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* ALS4000 */
121         { 0, }
122 };
123
124 MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
125
126 static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
127 {
128         outb(reg, port+0x0c);
129         outl(val, port+0x08);
130 }
131
132 static inline void snd_als4000_gcr_write(sb_t *sb, u32 reg, u32 val)
133 {
134         snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
135 }       
136
137 static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
138 {
139         outb(reg, port+0x0c);
140         return inl(port+0x08);
141 }
142
143 static inline u32 snd_als4000_gcr_read(sb_t *sb, u32 reg)
144 {
145         return snd_als4000_gcr_read_addr(sb->alt_port, reg);
146 }
147
148 static void snd_als4000_set_rate(sb_t *chip, unsigned int rate)
149 {
150         if (!(chip->mode & SB_RATE_LOCK)) {
151                 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
152                 snd_sbdsp_command(chip, rate>>8);
153                 snd_sbdsp_command(chip, rate);
154         }
155 }
156
157 static void snd_als4000_set_capture_dma(sb_t *chip, dma_addr_t addr, unsigned size)
158 {
159         snd_als4000_gcr_write(chip, 0xa2, addr);
160         snd_als4000_gcr_write(chip, 0xa3, (size-1));
161 }
162
163 static void snd_als4000_set_playback_dma(sb_t *chip, dma_addr_t addr, unsigned size)
164 {
165         snd_als4000_gcr_write(chip, 0x91, addr);
166         snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
167 }
168
169 #define ALS4000_FORMAT_SIGNED   (1<<0)
170 #define ALS4000_FORMAT_16BIT    (1<<1)
171 #define ALS4000_FORMAT_STEREO   (1<<2)
172
173 static int snd_als4000_get_format(snd_pcm_runtime_t *runtime)
174 {
175         int result;
176
177         result = 0;
178         if (snd_pcm_format_signed(runtime->format))
179                 result |= ALS4000_FORMAT_SIGNED;
180         if (snd_pcm_format_physical_width(runtime->format) == 16)
181                 result |= ALS4000_FORMAT_16BIT;
182         if (runtime->channels > 1)
183                 result |= ALS4000_FORMAT_STEREO;
184         return result;
185 }
186
187 /* structure for setting up playback */
188 static struct {
189         unsigned char dsp_cmd, dma_on, dma_off, format;
190 } playback_cmd_vals[]={
191 /* ALS4000_FORMAT_U8_MONO */
192 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
193 /* ALS4000_FORMAT_S8_MONO */    
194 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
195 /* ALS4000_FORMAT_U16L_MONO */
196 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
197 /* ALS4000_FORMAT_S16L_MONO */
198 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
199 /* ALS4000_FORMAT_U8_STEREO */
200 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
201 /* ALS4000_FORMAT_S8_STEREO */  
202 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
203 /* ALS4000_FORMAT_U16L_STEREO */
204 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
205 /* ALS4000_FORMAT_S16L_STEREO */
206 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
207 };
208 #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
209
210 /* structure for setting up capture */
211 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
212 static unsigned char capture_cmd_vals[]=
213 {
214 CMD_WIDTH8|CMD_MONO,                    /* ALS4000_FORMAT_U8_MONO */
215 CMD_WIDTH8|CMD_SIGNED|CMD_MONO,         /* ALS4000_FORMAT_S8_MONO */    
216 CMD_MONO,                               /* ALS4000_FORMAT_U16L_MONO */
217 CMD_SIGNED|CMD_MONO,                    /* ALS4000_FORMAT_S16L_MONO */
218 CMD_WIDTH8|CMD_STEREO,                  /* ALS4000_FORMAT_U8_STEREO */
219 CMD_WIDTH8|CMD_SIGNED|CMD_STEREO,       /* ALS4000_FORMAT_S8_STEREO */  
220 CMD_STEREO,                             /* ALS4000_FORMAT_U16L_STEREO */
221 CMD_SIGNED|CMD_STEREO,                  /* ALS4000_FORMAT_S16L_STEREO */
222 };      
223 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
224
225 static int snd_als4000_hw_params(snd_pcm_substream_t * substream,
226                                  snd_pcm_hw_params_t * hw_params)
227 {
228         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
229 }
230
231 static int snd_als4000_hw_free(snd_pcm_substream_t * substream)
232 {
233         snd_pcm_lib_free_pages(substream);
234         return 0;
235 }
236
237 static int snd_als4000_capture_prepare(snd_pcm_substream_t * substream)
238 {
239         unsigned long flags;
240         sb_t *chip = snd_pcm_substream_chip(substream);
241         snd_pcm_runtime_t *runtime = substream->runtime;
242         unsigned long size;
243         unsigned count;
244
245         chip->capture_format = snd_als4000_get_format(runtime);
246                 
247         size = snd_pcm_lib_buffer_bytes(substream);
248         count = snd_pcm_lib_period_bytes(substream);
249         
250         if (chip->capture_format & ALS4000_FORMAT_16BIT)
251                 count >>=1;
252         count--;
253
254         spin_lock_irqsave(&chip->reg_lock, flags);
255         snd_als4000_set_rate(chip, runtime->rate);
256         snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
257         spin_unlock_irqrestore(&chip->reg_lock, flags);
258         spin_lock_irqsave(&chip->mixer_lock, flags );
259         snd_sbmixer_write(chip, 0xdc, count);
260         snd_sbmixer_write(chip, 0xdd, count>>8);
261         spin_unlock_irqrestore(&chip->mixer_lock, flags );
262         return 0;
263 }
264
265 static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
266 {
267         unsigned long flags;
268         sb_t *chip = snd_pcm_substream_chip(substream);
269         snd_pcm_runtime_t *runtime = substream->runtime;
270         unsigned long size;
271         unsigned count;
272
273         chip->playback_format = snd_als4000_get_format(runtime);
274         
275         size = snd_pcm_lib_buffer_bytes(substream);
276         count = snd_pcm_lib_period_bytes(substream);
277         
278         if (chip->playback_format & ALS4000_FORMAT_16BIT)
279                 count >>=1;
280         count--;
281         
282         /* FIXME: from second playback on, there's a lot more clicks and pops
283          * involved here than on first playback. Fiddling with
284          * tons of different settings didn't help (DMA, speaker on/off,
285          * reordering, ...). Something seems to get enabled on playback
286          * that I haven't found out how to disable again, which then causes
287          * the switching pops to reach the speakers the next time here. */
288         spin_lock_irqsave(&chip->reg_lock, flags);
289         snd_als4000_set_rate(chip, runtime->rate);
290         snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
291         
292         /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
293         /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
294         snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
295         snd_sbdsp_command(chip, playback_cmd(chip).format);
296         snd_sbdsp_command(chip, count);
297         snd_sbdsp_command(chip, count>>8);
298         snd_sbdsp_command(chip, playback_cmd(chip).dma_off);    
299         spin_unlock_irqrestore(&chip->reg_lock, flags);
300         
301         return 0;
302 }
303
304 static int snd_als4000_capture_trigger(snd_pcm_substream_t * substream, int cmd)
305 {
306         unsigned long flags;
307         sb_t *chip = snd_pcm_substream_chip(substream);
308         int result = 0;
309         
310         spin_lock_irqsave(&chip->mixer_lock, flags);
311         if (cmd == SNDRV_PCM_TRIGGER_START) {
312                 chip->mode |= SB_RATE_LOCK_CAPTURE;
313                 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
314         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
315                 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
316                 snd_sbmixer_write(chip, 0xde, 0);
317         } else {
318                 result = -EINVAL;
319         }
320         spin_unlock_irqrestore(&chip->mixer_lock, flags);
321         return result;
322 }
323
324 static int snd_als4000_playback_trigger(snd_pcm_substream_t * substream, int cmd)
325 {
326         unsigned long flags;
327         sb_t *chip = snd_pcm_substream_chip(substream);
328         int result = 0;
329
330         spin_lock_irqsave(&chip->reg_lock, flags);
331         if (cmd == SNDRV_PCM_TRIGGER_START) {
332                 chip->mode |= SB_RATE_LOCK_PLAYBACK;
333                 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
334         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
335                 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
336                 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
337         } else {
338                 result = -EINVAL;
339         }
340         spin_unlock_irqrestore(&chip->reg_lock, flags);
341         return result;
342 }
343
344 static snd_pcm_uframes_t snd_als4000_capture_pointer(snd_pcm_substream_t * substream)
345 {
346         unsigned long flags;
347         sb_t *chip = snd_pcm_substream_chip(substream);
348         unsigned int result;
349
350         spin_lock_irqsave(&chip->reg_lock, flags);      
351         result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
352         spin_unlock_irqrestore(&chip->reg_lock, flags);
353         return bytes_to_frames( substream->runtime, result );
354 }
355
356 static snd_pcm_uframes_t snd_als4000_playback_pointer(snd_pcm_substream_t * substream)
357 {
358         unsigned long flags;
359         sb_t *chip = snd_pcm_substream_chip(substream);
360         unsigned result;
361
362         spin_lock_irqsave(&chip->reg_lock, flags);      
363         result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
364         spin_unlock_irqrestore(&chip->reg_lock, flags);
365         return bytes_to_frames( substream->runtime, result );
366 }
367
368 static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
369 {
370         sb_t *chip = snd_magic_cast(sb_t, dev_id, return IRQ_NONE);
371         unsigned long flags;
372         unsigned gcr_status;
373         unsigned sb_status;
374
375         /* find out which bit of the ALS4000 produced the interrupt */
376         gcr_status = inb(chip->alt_port + 0xe);
377
378         if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
379                 snd_pcm_period_elapsed(chip->playback_substream);
380         if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
381                 snd_pcm_period_elapsed(chip->capture_substream);
382         if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
383                 snd_mpu401_uart_interrupt(irq, chip->rmidi, regs);
384         /* release the gcr */
385         outb(gcr_status, chip->alt_port + 0xe);
386         
387         spin_lock_irqsave(&chip->mixer_lock, flags);
388         sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
389         spin_unlock_irqrestore(&chip->mixer_lock, flags);
390         
391         if (sb_status & SB_IRQTYPE_8BIT) 
392                 snd_sb_ack_8bit(chip);
393         if (sb_status & SB_IRQTYPE_16BIT) 
394                 snd_sb_ack_16bit(chip);
395         if (sb_status & SB_IRQTYPE_MPUIN)
396                 inb(chip->mpu_port);
397         if (sb_status & 0x20)
398                 inb(SBP(chip, RESET));
399         return IRQ_HANDLED;
400 }
401
402 /*****************************************************************/
403
404 static snd_pcm_hardware_t snd_als4000_playback =
405 {
406         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
407                                  SNDRV_PCM_INFO_MMAP_VALID),
408         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
409                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
410         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
411         .rate_min =             4000,
412         .rate_max =             48000,
413         .channels_min =         1,
414         .channels_max =         2,
415         .buffer_bytes_max =     65536,
416         .period_bytes_min =     64,
417         .period_bytes_max =     65536,
418         .periods_min =          1,
419         .periods_max =          1024,
420         .fifo_size =            0
421 };
422
423 static snd_pcm_hardware_t snd_als4000_capture =
424 {
425         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
426                                  SNDRV_PCM_INFO_MMAP_VALID),
427         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
428                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
429         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
430         .rate_min =             4000,
431         .rate_max =             48000,
432         .channels_min =         1,
433         .channels_max =         2,
434         .buffer_bytes_max =     65536,
435         .period_bytes_min =     64,
436         .period_bytes_max =     65536,
437         .periods_min =          1,
438         .periods_max =          1024,
439         .fifo_size =            0
440 };
441
442 /*****************************************************************/
443
444 static int snd_als4000_playback_open(snd_pcm_substream_t * substream)
445 {
446         sb_t *chip = snd_pcm_substream_chip(substream);
447         snd_pcm_runtime_t *runtime = substream->runtime;
448
449         chip->playback_substream = substream;
450         runtime->hw = snd_als4000_playback;
451         return 0;
452 }
453
454 static int snd_als4000_playback_close(snd_pcm_substream_t * substream)
455 {
456         sb_t *chip = snd_pcm_substream_chip(substream);
457
458         chip->playback_substream = NULL;
459         snd_pcm_lib_free_pages(substream);
460         return 0;
461 }
462
463 static int snd_als4000_capture_open(snd_pcm_substream_t * substream)
464 {
465         sb_t *chip = snd_pcm_substream_chip(substream);
466         snd_pcm_runtime_t *runtime = substream->runtime;
467
468         chip->capture_substream = substream;
469         runtime->hw = snd_als4000_capture;
470         return 0;
471 }
472
473 static int snd_als4000_capture_close(snd_pcm_substream_t * substream)
474 {
475         sb_t *chip = snd_pcm_substream_chip(substream);
476
477         chip->capture_substream = NULL;
478         snd_pcm_lib_free_pages(substream);
479         return 0;
480 }
481
482 /******************************************************************/
483
484 static snd_pcm_ops_t snd_als4000_playback_ops = {
485         .open =         snd_als4000_playback_open,
486         .close =        snd_als4000_playback_close,
487         .ioctl =        snd_pcm_lib_ioctl,
488         .hw_params =    snd_als4000_hw_params,
489         .hw_free =      snd_als4000_hw_free,
490         .prepare =      snd_als4000_playback_prepare,
491         .trigger =      snd_als4000_playback_trigger,
492         .pointer =      snd_als4000_playback_pointer
493 };
494
495 static snd_pcm_ops_t snd_als4000_capture_ops = {
496         .open =         snd_als4000_capture_open,
497         .close =        snd_als4000_capture_close,
498         .ioctl =        snd_pcm_lib_ioctl,
499         .hw_params =    snd_als4000_hw_params,
500         .hw_free =      snd_als4000_hw_free,
501         .prepare =      snd_als4000_capture_prepare,
502         .trigger =      snd_als4000_capture_trigger,
503         .pointer =      snd_als4000_capture_pointer
504 };
505
506 static void snd_als4000_pcm_free(snd_pcm_t *pcm)
507 {
508         sb_t *chip = snd_magic_cast(sb_t, pcm->private_data, return);
509         chip->pcm = NULL;
510         snd_pcm_lib_preallocate_free_for_all(pcm);
511 }
512
513 static int __devinit snd_als4000_pcm(sb_t *chip, int device)
514 {
515         snd_pcm_t *pcm;
516         int err;
517
518         if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
519                 return err;
520         pcm->private_free = snd_als4000_pcm_free;
521         pcm->private_data = chip;
522         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
523         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
524         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
525
526         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
527                                               64*1024, 64*1024);
528
529         chip->pcm = pcm;
530
531         return 0;
532 }
533
534 /******************************************************************/
535
536 static void snd_als4000_set_addr(unsigned long gcr,
537                                         unsigned int sb,
538                                         unsigned int mpu,
539                                         unsigned int opl,
540                                         unsigned int game)
541 {
542         u32 confA = 0;
543         u32 confB = 0;
544
545         if (mpu > 0)
546                 confB |= (mpu | 1) << 16;
547         if (sb > 0)
548                 confB |= (sb | 1);
549         if (game > 0)
550                 confA |= (game | 1) << 16;
551         if (opl > 0)    
552                 confA |= (opl | 1);
553         snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
554         snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
555 }
556
557 static void __devinit snd_als4000_configure(sb_t *chip)
558 {
559         unsigned long flags;
560         unsigned tmp;
561         int i;
562
563         /* do some more configuration */
564         spin_lock_irqsave(&chip->mixer_lock, flags);
565         tmp = snd_sbmixer_read(chip, 0xc0);
566         snd_sbmixer_write(chip, 0xc0, tmp|0x80);
567         /* always select DMA channel 0, since we do not actually use DMA */
568         snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
569         snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
570         spin_unlock_irqrestore(&chip->mixer_lock, flags);
571         
572         spin_lock_irqsave(&chip->reg_lock,flags);
573         /* magic number. Enables interrupts(?) */
574         snd_als4000_gcr_write(chip, 0x8c, 0x28000);
575         for(i = 0x91; i <= 0x96; ++i)
576                 snd_als4000_gcr_write(chip, i, 0);
577         
578         snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
579         spin_unlock_irqrestore(&chip->reg_lock,flags);
580 }
581
582 static void snd_card_als4000_free( snd_card_t *card )
583 {
584         snd_card_als4000_t * acard = (snd_card_als4000_t *)card->private_data;
585         /* make sure that interrupts are disabled */
586         snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
587         /* free resources */
588 #ifdef SUPPORT_JOYSTICK
589         if (acard->res_joystick) {
590                 if (acard->gameport.io)
591                         gameport_unregister_port(&acard->gameport);
592                 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
593                 release_resource(acard->res_joystick);
594                 kfree_nocheck(acard->res_joystick);
595         }
596 #endif
597         release_resource(acard->res_gcr);
598         kfree_nocheck(acard->res_gcr);
599 }
600
601 static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
602                                           const struct pci_device_id *pci_id)
603 {
604         static int dev;
605         snd_card_t *card;
606         snd_card_als4000_t *acard;
607         unsigned long gcr;
608         struct resource *res_gcr_port;
609         sb_t *chip;
610         opl3_t *opl3;
611         unsigned short word;
612         int err;
613         int joystick = 0;
614
615         if (dev >= SNDRV_CARDS)
616                 return -ENODEV;
617         if (!enable[dev]) {
618                 dev++;
619                 return -ENOENT;
620         }
621
622         /* enable PCI device */
623         if ((err = pci_enable_device(pci)) < 0) {
624                 return err;
625         }
626         /* check, if we can restrict PCI DMA transfers to 24 bits */
627         if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
628             pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
629                 snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
630                 return -ENXIO;
631         }
632
633         gcr = pci_resource_start(pci, 0);
634         if ((res_gcr_port = request_region(gcr, 0x40, "ALS4000")) == NULL) {
635                 snd_printk("unable to grab region 0x%lx-0x%lx\n", gcr, gcr + 0x40 - 1);
636                 return -EBUSY;
637         }
638
639         pci_read_config_word(pci, PCI_COMMAND, &word);
640         pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
641         pci_set_master(pci);
642         
643         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 
644                             sizeof( snd_card_als4000_t ) );
645         if (card == NULL) {
646                 release_resource(res_gcr_port);
647                 kfree_nocheck(res_gcr_port);
648                 return -ENOMEM;
649         }
650
651         acard = (snd_card_als4000_t *)card->private_data;
652         acard->gcr = gcr;
653         acard->res_gcr = res_gcr_port;
654         card->private_free = snd_card_als4000_free;
655
656         /* disable all legacy ISA stuff except for joystick */
657 #ifdef SUPPORT_JOYSTICK
658         if (joystick_port[dev] == 1) {
659                 /* auto-detect */
660                 long p;
661                 for (p = 0x200; p <= 0x218; p += 8) {
662                         if ((acard->res_joystick = request_region(p, 8, "ALS4000 gameport")) != NULL) {
663                                 joystick_port[dev] = p;
664                                 break;
665                         }
666                 }
667         } else if (joystick_port[dev] > 0)
668                 acard->res_joystick = request_region(joystick_port[dev], 8, "ALS4000 gameport");
669         if (acard->res_joystick)
670                 joystick = joystick_port[dev];
671         else
672                 joystick = 0;
673 #endif
674         snd_als4000_set_addr(gcr, 0, 0, 0, joystick);
675         
676         if ((err = snd_sbdsp_create(card,
677                                     gcr + 0x10,
678                                     pci->irq,
679                                     snd_als4000_interrupt,
680                                     -1,
681                                     -1,
682                                     SB_HW_ALS4000,
683                                     &chip)) < 0) {
684                 snd_card_free(card);
685                 return err;
686         }
687
688         chip->pci = pci;
689         chip->alt_port = gcr;
690         snd_card_set_dev(card, &pci->dev);
691
692         snd_als4000_configure(chip);
693
694         strcpy(card->driver, "ALS4000");
695         strcpy(card->shortname, "Avance Logic ALS4000");
696         sprintf(card->longname, "%s at 0x%lx, irq %i",
697                 card->shortname, chip->alt_port, chip->irq);
698
699         if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
700                                         gcr+0x30, 1, pci->irq, 0,
701                                         &chip->rmidi)) < 0) {
702                 snd_card_free(card);
703                 printk(KERN_ERR "als4000: no MPU-401device at 0x%lx ?\n", gcr+0x30);
704                 return err;
705         }
706
707         if ((err = snd_als4000_pcm(chip, 0)) < 0) {
708                 snd_card_free(card);
709                 return err;
710         }
711         if ((err = snd_sbmixer_new(chip)) < 0) {
712                 snd_card_free(card);
713                 return err;
714         }           
715
716         if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
717                             OPL3_HW_AUTO, 1, &opl3) < 0) {
718                 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx ?\n",
719                            gcr+0x10, gcr+0x12 );
720         } else {
721                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
722                         snd_card_free(card);
723                         return err;
724                 }
725         }
726
727 #ifdef SUPPORT_JOYSTICK
728         if (acard->res_joystick) {
729                 acard->gameport.io = joystick;
730                 gameport_register_port(&acard->gameport);
731         }
732 #endif
733
734         if ((err = snd_card_register(card)) < 0) {
735                 snd_card_free(card);
736                 return err;
737         }
738         pci_set_drvdata(pci, card);
739         dev++;
740         return 0;
741 }
742
743 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
744 {
745         snd_card_free(pci_get_drvdata(pci));
746         pci_set_drvdata(pci, NULL);
747 }
748
749 static struct pci_driver driver = {
750         .name = "ALS4000",
751         .id_table = snd_als4000_ids,
752         .probe = snd_card_als4000_probe,
753         .remove = __devexit_p(snd_card_als4000_remove),
754 };
755
756 static int __init alsa_card_als4000_init(void)
757 {
758         int err;
759         
760         if ((err = pci_module_init(&driver)) < 0) {
761 #ifdef MODULE
762                 printk(KERN_ERR "no ALS4000 based soundcards found or device busy\n");
763 #endif
764                 return err;
765         }
766         return 0;
767 }
768
769 static void __exit alsa_card_als4000_exit(void)
770 {
771         pci_unregister_driver(&driver);
772 }
773
774 module_init(alsa_card_als4000_init)
775 module_exit(alsa_card_als4000_exit)
776
777 #ifndef MODULE
778
779 /* format is: snd-als4000=enable,index,id,joystick_port */
780
781 static int __init alsa_card_als4000_setup(char *str)
782 {
783         static unsigned __initdata nr_dev = 0;
784
785         if (nr_dev >= SNDRV_CARDS)
786                 return 0;
787         (void)(get_option(&str,&enable[nr_dev]) == 2 &&
788                get_option(&str,&index[nr_dev]) == 2 &&
789                get_id(&str,&id[nr_dev]) == 2
790 #ifdef SUPPORT_JOYSTICK
791                && get_option(&str,&joystick_port[nr_dev]) == 2
792 #endif
793                );
794         nr_dev++;
795         return 1;
796 }
797
798 __setup("snd-als4000=", alsa_card_als4000_setup);
799
800 #endif /* ifndef MODULE */