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