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