vserver 1.9.3
[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 <linux/moduleparam.h>
68 #include <sound/core.h>
69 #include <sound/pcm.h>
70 #include <sound/rawmidi.h>
71 #include <sound/mpu401.h>
72 #include <sound/opl3.h>
73 #include <sound/sb.h>
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_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
80
81 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
82 #define SUPPORT_JOYSTICK 1
83 #endif
84
85 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
86 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
87 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
88 #ifdef SUPPORT_JOYSTICK
89 static int joystick_port[SNDRV_CARDS];
90 #endif
91 static int boot_devs;
92
93 module_param_array(index, int, boot_devs, 0444);
94 MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
95 module_param_array(id, charp, boot_devs, 0444);
96 MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
97 module_param_array(enable, bool, boot_devs, 0444);
98 MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
99 #ifdef SUPPORT_JOYSTICK
100 module_param_array(joystick_port, int, boot_devs, 0444);
101 MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
102 #endif
103
104 typedef struct {
105         struct pci_dev *pci;
106         unsigned long gcr;
107 #ifdef SUPPORT_JOYSTICK
108         struct gameport gameport;
109         struct resource *res_joystick;
110 #endif
111 } snd_card_als4000_t;
112
113 static struct pci_device_id snd_als4000_ids[] = {
114         { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, },   /* ALS4000 */
115         { 0, }
116 };
117
118 MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
119
120 static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
121 {
122         outb(reg, port+0x0c);
123         outl(val, port+0x08);
124 }
125
126 static inline void snd_als4000_gcr_write(sb_t *sb, u32 reg, u32 val)
127 {
128         snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
129 }       
130
131 static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
132 {
133         outb(reg, port+0x0c);
134         return inl(port+0x08);
135 }
136
137 static inline u32 snd_als4000_gcr_read(sb_t *sb, u32 reg)
138 {
139         return snd_als4000_gcr_read_addr(sb->alt_port, reg);
140 }
141
142 static void snd_als4000_set_rate(sb_t *chip, unsigned int rate)
143 {
144         if (!(chip->mode & SB_RATE_LOCK)) {
145                 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
146                 snd_sbdsp_command(chip, rate>>8);
147                 snd_sbdsp_command(chip, rate);
148         }
149 }
150
151 static void snd_als4000_set_capture_dma(sb_t *chip, dma_addr_t addr, unsigned size)
152 {
153         snd_als4000_gcr_write(chip, 0xa2, addr);
154         snd_als4000_gcr_write(chip, 0xa3, (size-1));
155 }
156
157 static void snd_als4000_set_playback_dma(sb_t *chip, dma_addr_t addr, unsigned size)
158 {
159         snd_als4000_gcr_write(chip, 0x91, addr);
160         snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
161 }
162
163 #define ALS4000_FORMAT_SIGNED   (1<<0)
164 #define ALS4000_FORMAT_16BIT    (1<<1)
165 #define ALS4000_FORMAT_STEREO   (1<<2)
166
167 static int snd_als4000_get_format(snd_pcm_runtime_t *runtime)
168 {
169         int result;
170
171         result = 0;
172         if (snd_pcm_format_signed(runtime->format))
173                 result |= ALS4000_FORMAT_SIGNED;
174         if (snd_pcm_format_physical_width(runtime->format) == 16)
175                 result |= ALS4000_FORMAT_16BIT;
176         if (runtime->channels > 1)
177                 result |= ALS4000_FORMAT_STEREO;
178         return result;
179 }
180
181 /* structure for setting up playback */
182 static struct {
183         unsigned char dsp_cmd, dma_on, dma_off, format;
184 } playback_cmd_vals[]={
185 /* ALS4000_FORMAT_U8_MONO */
186 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
187 /* ALS4000_FORMAT_S8_MONO */    
188 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
189 /* ALS4000_FORMAT_U16L_MONO */
190 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
191 /* ALS4000_FORMAT_S16L_MONO */
192 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
193 /* ALS4000_FORMAT_U8_STEREO */
194 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
195 /* ALS4000_FORMAT_S8_STEREO */  
196 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
197 /* ALS4000_FORMAT_U16L_STEREO */
198 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
199 /* ALS4000_FORMAT_S16L_STEREO */
200 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
201 };
202 #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
203
204 /* structure for setting up capture */
205 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
206 static unsigned char capture_cmd_vals[]=
207 {
208 CMD_WIDTH8|CMD_MONO,                    /* ALS4000_FORMAT_U8_MONO */
209 CMD_WIDTH8|CMD_SIGNED|CMD_MONO,         /* ALS4000_FORMAT_S8_MONO */    
210 CMD_MONO,                               /* ALS4000_FORMAT_U16L_MONO */
211 CMD_SIGNED|CMD_MONO,                    /* ALS4000_FORMAT_S16L_MONO */
212 CMD_WIDTH8|CMD_STEREO,                  /* ALS4000_FORMAT_U8_STEREO */
213 CMD_WIDTH8|CMD_SIGNED|CMD_STEREO,       /* ALS4000_FORMAT_S8_STEREO */  
214 CMD_STEREO,                             /* ALS4000_FORMAT_U16L_STEREO */
215 CMD_SIGNED|CMD_STEREO,                  /* ALS4000_FORMAT_S16L_STEREO */
216 };      
217 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
218
219 static int snd_als4000_hw_params(snd_pcm_substream_t * substream,
220                                  snd_pcm_hw_params_t * hw_params)
221 {
222         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
223 }
224
225 static int snd_als4000_hw_free(snd_pcm_substream_t * substream)
226 {
227         snd_pcm_lib_free_pages(substream);
228         return 0;
229 }
230
231 static int snd_als4000_capture_prepare(snd_pcm_substream_t * substream)
232 {
233         unsigned long flags;
234         sb_t *chip = snd_pcm_substream_chip(substream);
235         snd_pcm_runtime_t *runtime = substream->runtime;
236         unsigned long size;
237         unsigned count;
238
239         chip->capture_format = snd_als4000_get_format(runtime);
240                 
241         size = snd_pcm_lib_buffer_bytes(substream);
242         count = snd_pcm_lib_period_bytes(substream);
243         
244         if (chip->capture_format & ALS4000_FORMAT_16BIT)
245                 count >>=1;
246         count--;
247
248         spin_lock_irqsave(&chip->reg_lock, flags);
249         snd_als4000_set_rate(chip, runtime->rate);
250         snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
251         spin_unlock_irqrestore(&chip->reg_lock, flags);
252         spin_lock_irqsave(&chip->mixer_lock, flags );
253         snd_sbmixer_write(chip, 0xdc, count);
254         snd_sbmixer_write(chip, 0xdd, count>>8);
255         spin_unlock_irqrestore(&chip->mixer_lock, flags );
256         return 0;
257 }
258
259 static int snd_als4000_playback_prepare(snd_pcm_substream_t *substream)
260 {
261         unsigned long flags;
262         sb_t *chip = snd_pcm_substream_chip(substream);
263         snd_pcm_runtime_t *runtime = substream->runtime;
264         unsigned long size;
265         unsigned count;
266
267         chip->playback_format = snd_als4000_get_format(runtime);
268         
269         size = snd_pcm_lib_buffer_bytes(substream);
270         count = snd_pcm_lib_period_bytes(substream);
271         
272         if (chip->playback_format & ALS4000_FORMAT_16BIT)
273                 count >>=1;
274         count--;
275         
276         /* FIXME: from second playback on, there's a lot more clicks and pops
277          * involved here than on first playback. Fiddling with
278          * tons of different settings didn't help (DMA, speaker on/off,
279          * reordering, ...). Something seems to get enabled on playback
280          * that I haven't found out how to disable again, which then causes
281          * the switching pops to reach the speakers the next time here. */
282         spin_lock_irqsave(&chip->reg_lock, flags);
283         snd_als4000_set_rate(chip, runtime->rate);
284         snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
285         
286         /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
287         /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
288         snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
289         snd_sbdsp_command(chip, playback_cmd(chip).format);
290         snd_sbdsp_command(chip, count);
291         snd_sbdsp_command(chip, count>>8);
292         snd_sbdsp_command(chip, playback_cmd(chip).dma_off);    
293         spin_unlock_irqrestore(&chip->reg_lock, flags);
294         
295         return 0;
296 }
297
298 static int snd_als4000_capture_trigger(snd_pcm_substream_t * substream, int cmd)
299 {
300         sb_t *chip = snd_pcm_substream_chip(substream);
301         int result = 0;
302         
303         spin_lock(&chip->mixer_lock);
304         if (cmd == SNDRV_PCM_TRIGGER_START) {
305                 chip->mode |= SB_RATE_LOCK_CAPTURE;
306                 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
307         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
308                 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
309                 snd_sbmixer_write(chip, 0xde, 0);
310         } else {
311                 result = -EINVAL;
312         }
313         spin_unlock(&chip->mixer_lock);
314         return result;
315 }
316
317 static int snd_als4000_playback_trigger(snd_pcm_substream_t * substream, int cmd)
318 {
319         sb_t *chip = snd_pcm_substream_chip(substream);
320         int result = 0;
321
322         spin_lock(&chip->reg_lock);
323         if (cmd == SNDRV_PCM_TRIGGER_START) {
324                 chip->mode |= SB_RATE_LOCK_PLAYBACK;
325                 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
326         } else if (cmd == SNDRV_PCM_TRIGGER_STOP) {
327                 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
328                 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
329         } else {
330                 result = -EINVAL;
331         }
332         spin_unlock(&chip->reg_lock);
333         return result;
334 }
335
336 static snd_pcm_uframes_t snd_als4000_capture_pointer(snd_pcm_substream_t * substream)
337 {
338         sb_t *chip = snd_pcm_substream_chip(substream);
339         unsigned int result;
340
341         spin_lock(&chip->reg_lock);     
342         result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
343         spin_unlock(&chip->reg_lock);
344         return bytes_to_frames( substream->runtime, result );
345 }
346
347 static snd_pcm_uframes_t snd_als4000_playback_pointer(snd_pcm_substream_t * substream)
348 {
349         sb_t *chip = snd_pcm_substream_chip(substream);
350         unsigned result;
351
352         spin_lock(&chip->reg_lock);     
353         result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
354         spin_unlock(&chip->reg_lock);
355         return bytes_to_frames( substream->runtime, result );
356 }
357
358 static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
359 {
360         sb_t *chip = dev_id;
361         unsigned gcr_status;
362         unsigned sb_status;
363
364         /* find out which bit of the ALS4000 produced the interrupt */
365         gcr_status = inb(chip->alt_port + 0xe);
366
367         if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
368                 snd_pcm_period_elapsed(chip->playback_substream);
369         if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
370                 snd_pcm_period_elapsed(chip->capture_substream);
371         if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
372                 snd_mpu401_uart_interrupt(irq, chip->rmidi, regs);
373         /* release the gcr */
374         outb(gcr_status, chip->alt_port + 0xe);
375         
376         spin_lock(&chip->mixer_lock);
377         sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
378         spin_unlock(&chip->mixer_lock);
379         
380         if (sb_status & SB_IRQTYPE_8BIT) 
381                 snd_sb_ack_8bit(chip);
382         if (sb_status & SB_IRQTYPE_16BIT) 
383                 snd_sb_ack_16bit(chip);
384         if (sb_status & SB_IRQTYPE_MPUIN)
385                 inb(chip->mpu_port);
386         if (sb_status & 0x20)
387                 inb(SBP(chip, RESET));
388         return IRQ_HANDLED;
389 }
390
391 /*****************************************************************/
392
393 static snd_pcm_hardware_t snd_als4000_playback =
394 {
395         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
396                                  SNDRV_PCM_INFO_MMAP_VALID),
397         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
398                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
399         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
400         .rate_min =             4000,
401         .rate_max =             48000,
402         .channels_min =         1,
403         .channels_max =         2,
404         .buffer_bytes_max =     65536,
405         .period_bytes_min =     64,
406         .period_bytes_max =     65536,
407         .periods_min =          1,
408         .periods_max =          1024,
409         .fifo_size =            0
410 };
411
412 static snd_pcm_hardware_t snd_als4000_capture =
413 {
414         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
415                                  SNDRV_PCM_INFO_MMAP_VALID),
416         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
417                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
418         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
419         .rate_min =             4000,
420         .rate_max =             48000,
421         .channels_min =         1,
422         .channels_max =         2,
423         .buffer_bytes_max =     65536,
424         .period_bytes_min =     64,
425         .period_bytes_max =     65536,
426         .periods_min =          1,
427         .periods_max =          1024,
428         .fifo_size =            0
429 };
430
431 /*****************************************************************/
432
433 static int snd_als4000_playback_open(snd_pcm_substream_t * substream)
434 {
435         sb_t *chip = snd_pcm_substream_chip(substream);
436         snd_pcm_runtime_t *runtime = substream->runtime;
437
438         chip->playback_substream = substream;
439         runtime->hw = snd_als4000_playback;
440         return 0;
441 }
442
443 static int snd_als4000_playback_close(snd_pcm_substream_t * substream)
444 {
445         sb_t *chip = snd_pcm_substream_chip(substream);
446
447         chip->playback_substream = NULL;
448         snd_pcm_lib_free_pages(substream);
449         return 0;
450 }
451
452 static int snd_als4000_capture_open(snd_pcm_substream_t * substream)
453 {
454         sb_t *chip = snd_pcm_substream_chip(substream);
455         snd_pcm_runtime_t *runtime = substream->runtime;
456
457         chip->capture_substream = substream;
458         runtime->hw = snd_als4000_capture;
459         return 0;
460 }
461
462 static int snd_als4000_capture_close(snd_pcm_substream_t * substream)
463 {
464         sb_t *chip = snd_pcm_substream_chip(substream);
465
466         chip->capture_substream = NULL;
467         snd_pcm_lib_free_pages(substream);
468         return 0;
469 }
470
471 /******************************************************************/
472
473 static snd_pcm_ops_t snd_als4000_playback_ops = {
474         .open =         snd_als4000_playback_open,
475         .close =        snd_als4000_playback_close,
476         .ioctl =        snd_pcm_lib_ioctl,
477         .hw_params =    snd_als4000_hw_params,
478         .hw_free =      snd_als4000_hw_free,
479         .prepare =      snd_als4000_playback_prepare,
480         .trigger =      snd_als4000_playback_trigger,
481         .pointer =      snd_als4000_playback_pointer
482 };
483
484 static snd_pcm_ops_t snd_als4000_capture_ops = {
485         .open =         snd_als4000_capture_open,
486         .close =        snd_als4000_capture_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_capture_prepare,
491         .trigger =      snd_als4000_capture_trigger,
492         .pointer =      snd_als4000_capture_pointer
493 };
494
495 static void snd_als4000_pcm_free(snd_pcm_t *pcm)
496 {
497         sb_t *chip = pcm->private_data;
498         chip->pcm = NULL;
499         snd_pcm_lib_preallocate_free_for_all(pcm);
500 }
501
502 static int __devinit snd_als4000_pcm(sb_t *chip, int device)
503 {
504         snd_pcm_t *pcm;
505         int err;
506
507         if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
508                 return err;
509         pcm->private_free = snd_als4000_pcm_free;
510         pcm->private_data = chip;
511         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
512         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
513         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
514
515         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
516                                               64*1024, 64*1024);
517
518         chip->pcm = pcm;
519
520         return 0;
521 }
522
523 /******************************************************************/
524
525 static void snd_als4000_set_addr(unsigned long gcr,
526                                         unsigned int sb,
527                                         unsigned int mpu,
528                                         unsigned int opl,
529                                         unsigned int game)
530 {
531         u32 confA = 0;
532         u32 confB = 0;
533
534         if (mpu > 0)
535                 confB |= (mpu | 1) << 16;
536         if (sb > 0)
537                 confB |= (sb | 1);
538         if (game > 0)
539                 confA |= (game | 1) << 16;
540         if (opl > 0)    
541                 confA |= (opl | 1);
542         snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
543         snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
544 }
545
546 static void __devinit snd_als4000_configure(sb_t *chip)
547 {
548         unsigned tmp;
549         int i;
550
551         /* do some more configuration */
552         spin_lock_irq(&chip->mixer_lock);
553         tmp = snd_sbmixer_read(chip, 0xc0);
554         snd_sbmixer_write(chip, 0xc0, tmp|0x80);
555         /* always select DMA channel 0, since we do not actually use DMA */
556         snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
557         snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
558         spin_unlock_irq(&chip->mixer_lock);
559         
560         spin_lock_irq(&chip->reg_lock);
561         /* magic number. Enables interrupts(?) */
562         snd_als4000_gcr_write(chip, 0x8c, 0x28000);
563         for(i = 0x91; i <= 0x96; ++i)
564                 snd_als4000_gcr_write(chip, i, 0);
565         
566         snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
567         spin_unlock_irq(&chip->reg_lock);
568 }
569
570 static void snd_card_als4000_free( snd_card_t *card )
571 {
572         snd_card_als4000_t * acard = (snd_card_als4000_t *)card->private_data;
573         /* make sure that interrupts are disabled */
574         snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
575         /* free resources */
576 #ifdef SUPPORT_JOYSTICK
577         if (acard->res_joystick) {
578                 if (acard->gameport.io)
579                         gameport_unregister_port(&acard->gameport);
580                 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
581                 release_resource(acard->res_joystick);
582                 kfree_nocheck(acard->res_joystick);
583         }
584 #endif
585         pci_release_regions(acard->pci);
586 }
587
588 static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
589                                           const struct pci_device_id *pci_id)
590 {
591         static int dev;
592         snd_card_t *card;
593         snd_card_als4000_t *acard;
594         unsigned long gcr;
595         sb_t *chip;
596         opl3_t *opl3;
597         unsigned short word;
598         int err;
599         int joystick = 0;
600
601         if (dev >= SNDRV_CARDS)
602                 return -ENODEV;
603         if (!enable[dev]) {
604                 dev++;
605                 return -ENOENT;
606         }
607
608         /* enable PCI device */
609         if ((err = pci_enable_device(pci)) < 0) {
610                 return err;
611         }
612         /* check, if we can restrict PCI DMA transfers to 24 bits */
613         if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
614             pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
615                 snd_printk("architecture does not support 24bit PCI busmaster DMA\n");
616                 return -ENXIO;
617         }
618
619         if ((err = pci_request_regions(pci, "ALS4000")) < 0)
620                 return err;
621         gcr = pci_resource_start(pci, 0);
622
623         pci_read_config_word(pci, PCI_COMMAND, &word);
624         pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
625         pci_set_master(pci);
626         
627         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 
628                             sizeof( snd_card_als4000_t ) );
629         if (card == NULL) {
630                 pci_release_regions(pci);
631                 return -ENOMEM;
632         }
633
634         acard = (snd_card_als4000_t *)card->private_data;
635         acard->pci = pci;
636         acard->gcr = gcr;
637         card->private_free = snd_card_als4000_free;
638
639         /* disable all legacy ISA stuff except for joystick */
640 #ifdef SUPPORT_JOYSTICK
641         if (joystick_port[dev] == 1) {
642                 /* auto-detect */
643                 long p;
644                 for (p = 0x200; p <= 0x218; p += 8) {
645                         if ((acard->res_joystick = request_region(p, 8, "ALS4000 gameport")) != NULL) {
646                                 joystick_port[dev] = p;
647                                 break;
648                         }
649                 }
650         } else if (joystick_port[dev] > 0)
651                 acard->res_joystick = request_region(joystick_port[dev], 8, "ALS4000 gameport");
652         if (acard->res_joystick)
653                 joystick = joystick_port[dev];
654         else
655                 joystick = 0;
656 #endif
657         snd_als4000_set_addr(gcr, 0, 0, 0, joystick);
658         
659         if ((err = snd_sbdsp_create(card,
660                                     gcr + 0x10,
661                                     pci->irq,
662                                     snd_als4000_interrupt,
663                                     -1,
664                                     -1,
665                                     SB_HW_ALS4000,
666                                     &chip)) < 0) {
667                 snd_card_free(card);
668                 return err;
669         }
670
671         chip->pci = pci;
672         chip->alt_port = gcr;
673         snd_card_set_dev(card, &pci->dev);
674
675         snd_als4000_configure(chip);
676
677         strcpy(card->driver, "ALS4000");
678         strcpy(card->shortname, "Avance Logic ALS4000");
679         sprintf(card->longname, "%s at 0x%lx, irq %i",
680                 card->shortname, chip->alt_port, chip->irq);
681
682         if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
683                                         gcr+0x30, 1, pci->irq, 0,
684                                         &chip->rmidi)) < 0) {
685                 snd_card_free(card);
686                 printk(KERN_ERR "als4000: no MPU-401device at 0x%lx ?\n", gcr+0x30);
687                 return err;
688         }
689
690         if ((err = snd_als4000_pcm(chip, 0)) < 0) {
691                 snd_card_free(card);
692                 return err;
693         }
694         if ((err = snd_sbmixer_new(chip)) < 0) {
695                 snd_card_free(card);
696                 return err;
697         }           
698
699         if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
700                             OPL3_HW_AUTO, 1, &opl3) < 0) {
701                 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx ?\n",
702                            gcr+0x10, gcr+0x12 );
703         } else {
704                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
705                         snd_card_free(card);
706                         return err;
707                 }
708         }
709
710 #ifdef SUPPORT_JOYSTICK
711         if (acard->res_joystick) {
712                 acard->gameport.io = joystick;
713                 gameport_register_port(&acard->gameport);
714         }
715 #endif
716
717         if ((err = snd_card_register(card)) < 0) {
718                 snd_card_free(card);
719                 return err;
720         }
721         pci_set_drvdata(pci, card);
722         dev++;
723         return 0;
724 }
725
726 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
727 {
728         snd_card_free(pci_get_drvdata(pci));
729         pci_set_drvdata(pci, NULL);
730 }
731
732 static struct pci_driver driver = {
733         .name = "ALS4000",
734         .id_table = snd_als4000_ids,
735         .probe = snd_card_als4000_probe,
736         .remove = __devexit_p(snd_card_als4000_remove),
737 };
738
739 static int __init alsa_card_als4000_init(void)
740 {
741         return pci_module_init(&driver);
742 }
743
744 static void __exit alsa_card_als4000_exit(void)
745 {
746         pci_unregister_driver(&driver);
747 }
748
749 module_init(alsa_card_als4000_init)
750 module_exit(alsa_card_als4000_exit)