[ALSA] Changed Jaroslav Kysela's e-mail from perex@suse.cz to perex@perex.cz
[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@perex.cz>
5  *  Copyright (C) 2002 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
6  *
7  *  Framework borrowed from Massimo Piccioni's card-als100.c.
8  *
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  *
24  * NOTES
25  *
26  *  Since Avance does not provide any meaningful documentation, and I
27  *  bought an ALS4000 based soundcard, I was forced to base this driver
28  *  on reverse engineering.
29  *
30  *  Note: this is no longer true. Pretty verbose chip docu (ALS4000a.PDF)
31  *  can be found on the ALSA web site.
32  *
33  *  The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
34  *  ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport 
35  *  interface. These subsystems can be mapped into ISA io-port space, 
36  *  using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ 
37  *  services to the subsystems.
38  * 
39  * While ALS4000 is very similar to a SoundBlaster, the differences in
40  * DMA and capturing require more changes to the SoundBlaster than
41  * desirable, so I made this separate driver.
42  * 
43  * The ALS4000 can do real full duplex playback/capture.
44  *
45  * FMDAC:
46  * - 0x4f -> port 0x14
47  * - port 0x15 |= 1
48  *
49  * Enable/disable 3D sound:
50  * - 0x50 -> port 0x14
51  * - change bit 6 (0x40) of port 0x15
52  *
53  * Set QSound:
54  * - 0xdb -> port 0x14
55  * - set port 0x15:
56  *   0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
57  *
58  * Set KSound:
59  * - value -> some port 0x0c0d
60  *
61  * ToDo:
62  * - Proper shared IRQ handling?
63  * - power management? (card can do voice wakeup according to datasheet!!)
64  */
65
66 #include <sound/driver.h>
67 #include <asm/io.h>
68 #include <linux/init.h>
69 #include <linux/pci.h>
70 #include <linux/slab.h>
71 #include <linux/gameport.h>
72 #include <linux/moduleparam.h>
73 #include <linux/dma-mapping.h>
74 #include <sound/core.h>
75 #include <sound/pcm.h>
76 #include <sound/rawmidi.h>
77 #include <sound/mpu401.h>
78 #include <sound/opl3.h>
79 #include <sound/sb.h>
80 #include <sound/initval.h>
81
82 MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
83 MODULE_DESCRIPTION("Avance Logic ALS4000");
84 MODULE_LICENSE("GPL");
85 MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
86
87 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
88 #define SUPPORT_JOYSTICK 1
89 #endif
90
91 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;      /* Index 0-MAX */
92 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;       /* ID for this card */
93 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;      /* Enable this card */
94 #ifdef SUPPORT_JOYSTICK
95 static int joystick_port[SNDRV_CARDS];
96 #endif
97
98 module_param_array(index, int, NULL, 0444);
99 MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
100 module_param_array(id, charp, NULL, 0444);
101 MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
102 module_param_array(enable, bool, NULL, 0444);
103 MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
104 #ifdef SUPPORT_JOYSTICK
105 module_param_array(joystick_port, int, NULL, 0444);
106 MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
107 #endif
108
109 struct snd_card_als4000 {
110         /* most frequent access first */
111         unsigned long gcr;
112         struct pci_dev *pci;
113         struct snd_sb *chip;
114 #ifdef SUPPORT_JOYSTICK
115         struct gameport *gameport;
116 #endif
117 };
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(struct snd_sb *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(struct snd_sb *sb, u32 reg)
144 {
145         return snd_als4000_gcr_read_addr(sb->alt_port, reg);
146 }
147
148 static void snd_als4000_set_rate(struct snd_sb *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 inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
158                                                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 inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
165                                                 dma_addr_t addr, unsigned size)
166 {
167         snd_als4000_gcr_write(chip, 0x91, addr);
168         snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
169 }
170
171 #define ALS4000_FORMAT_SIGNED   (1<<0)
172 #define ALS4000_FORMAT_16BIT    (1<<1)
173 #define ALS4000_FORMAT_STEREO   (1<<2)
174
175 static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
176 {
177         int result;
178
179         result = 0;
180         if (snd_pcm_format_signed(runtime->format))
181                 result |= ALS4000_FORMAT_SIGNED;
182         if (snd_pcm_format_physical_width(runtime->format) == 16)
183                 result |= ALS4000_FORMAT_16BIT;
184         if (runtime->channels > 1)
185                 result |= ALS4000_FORMAT_STEREO;
186         return result;
187 }
188
189 /* structure for setting up playback */
190 static const struct {
191         unsigned char dsp_cmd, dma_on, dma_off, format;
192 } playback_cmd_vals[]={
193 /* ALS4000_FORMAT_U8_MONO */
194 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
195 /* ALS4000_FORMAT_S8_MONO */    
196 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
197 /* ALS4000_FORMAT_U16L_MONO */
198 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
199 /* ALS4000_FORMAT_S16L_MONO */
200 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
201 /* ALS4000_FORMAT_U8_STEREO */
202 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
203 /* ALS4000_FORMAT_S8_STEREO */  
204 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
205 /* ALS4000_FORMAT_U16L_STEREO */
206 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
207 /* ALS4000_FORMAT_S16L_STEREO */
208 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
209 };
210 #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
211
212 /* structure for setting up capture */
213 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
214 static const unsigned char capture_cmd_vals[]=
215 {
216 CMD_WIDTH8|CMD_MONO,                    /* ALS4000_FORMAT_U8_MONO */
217 CMD_WIDTH8|CMD_SIGNED|CMD_MONO,         /* ALS4000_FORMAT_S8_MONO */    
218 CMD_MONO,                               /* ALS4000_FORMAT_U16L_MONO */
219 CMD_SIGNED|CMD_MONO,                    /* ALS4000_FORMAT_S16L_MONO */
220 CMD_WIDTH8|CMD_STEREO,                  /* ALS4000_FORMAT_U8_STEREO */
221 CMD_WIDTH8|CMD_SIGNED|CMD_STEREO,       /* ALS4000_FORMAT_S8_STEREO */  
222 CMD_STEREO,                             /* ALS4000_FORMAT_U16L_STEREO */
223 CMD_SIGNED|CMD_STEREO,                  /* ALS4000_FORMAT_S16L_STEREO */
224 };      
225 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
226
227 static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
228                                  struct snd_pcm_hw_params *hw_params)
229 {
230         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
231 }
232
233 static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
234 {
235         snd_pcm_lib_free_pages(substream);
236         return 0;
237 }
238
239 static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
240 {
241         struct snd_sb *chip = snd_pcm_substream_chip(substream);
242         struct snd_pcm_runtime *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_irq(&chip->reg_lock);
256         snd_als4000_set_rate(chip, runtime->rate);
257         snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
258         spin_unlock_irq(&chip->reg_lock);
259         spin_lock_irq(&chip->mixer_lock);
260         snd_sbmixer_write(chip, 0xdc, count);
261         snd_sbmixer_write(chip, 0xdd, count>>8);
262         spin_unlock_irq(&chip->mixer_lock);
263         return 0;
264 }
265
266 static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
267 {
268         struct snd_sb *chip = snd_pcm_substream_chip(substream);
269         struct snd_pcm_runtime *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_irq(&chip->reg_lock);
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_irq(&chip->reg_lock);
300         
301         return 0;
302 }
303
304 static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
305 {
306         struct snd_sb *chip = snd_pcm_substream_chip(substream);
307         int result = 0;
308         
309         spin_lock(&chip->mixer_lock);
310         switch (cmd) {
311         case SNDRV_PCM_TRIGGER_START:
312         case SNDRV_PCM_TRIGGER_RESUME:
313                 chip->mode |= SB_RATE_LOCK_CAPTURE;
314                 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
315                 break;
316         case SNDRV_PCM_TRIGGER_STOP:
317         case SNDRV_PCM_TRIGGER_SUSPEND:
318                 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
319                 snd_sbmixer_write(chip, 0xde, 0);
320                 break;
321         default:
322                 result = -EINVAL;
323                 break;
324         }
325         spin_unlock(&chip->mixer_lock);
326         return result;
327 }
328
329 static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
330 {
331         struct snd_sb *chip = snd_pcm_substream_chip(substream);
332         int result = 0;
333
334         spin_lock(&chip->reg_lock);
335         switch (cmd) {
336         case SNDRV_PCM_TRIGGER_START:
337         case SNDRV_PCM_TRIGGER_RESUME:
338                 chip->mode |= SB_RATE_LOCK_PLAYBACK;
339                 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
340                 break;
341         case SNDRV_PCM_TRIGGER_STOP:
342         case SNDRV_PCM_TRIGGER_SUSPEND:
343                 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
344                 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
345                 break;
346         default:
347                 result = -EINVAL;
348                 break;
349         }
350         spin_unlock(&chip->reg_lock);
351         return result;
352 }
353
354 static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
355 {
356         struct snd_sb *chip = snd_pcm_substream_chip(substream);
357         unsigned int result;
358
359         spin_lock(&chip->reg_lock);     
360         result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
361         spin_unlock(&chip->reg_lock);
362         return bytes_to_frames( substream->runtime, result );
363 }
364
365 static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
366 {
367         struct snd_sb *chip = snd_pcm_substream_chip(substream);
368         unsigned result;
369
370         spin_lock(&chip->reg_lock);     
371         result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
372         spin_unlock(&chip->reg_lock);
373         return bytes_to_frames( substream->runtime, result );
374 }
375
376 /* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
377  * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
378  * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
379  * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
380  * register (alt_port + 0x0e). Probably something could be optimized here to
381  * query/write one register only...
382  * And even if both registers need to be queried, then there's still the
383  * question of whether it's actually correct to ACK PCI IRQ before reading
384  * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
385  * SB IRQ status.
386  * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
387  * */
388 static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
389 {
390         struct snd_sb *chip = dev_id;
391         unsigned gcr_status;
392         unsigned sb_status;
393
394         /* find out which bit of the ALS4000 produced the interrupt */
395         gcr_status = inb(chip->alt_port + 0xe);
396
397         if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
398                 snd_pcm_period_elapsed(chip->playback_substream);
399         if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
400                 snd_pcm_period_elapsed(chip->capture_substream);
401         if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
402                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
403         /* release the gcr */
404         outb(gcr_status, chip->alt_port + 0xe);
405         
406         spin_lock(&chip->mixer_lock);
407         sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
408         spin_unlock(&chip->mixer_lock);
409         
410         if (sb_status & SB_IRQTYPE_8BIT) 
411                 snd_sb_ack_8bit(chip);
412         if (sb_status & SB_IRQTYPE_16BIT) 
413                 snd_sb_ack_16bit(chip);
414         if (sb_status & SB_IRQTYPE_MPUIN)
415                 inb(chip->mpu_port);
416         if (sb_status & 0x20)
417                 inb(SBP(chip, RESET));
418         return IRQ_HANDLED;
419 }
420
421 /*****************************************************************/
422
423 static struct snd_pcm_hardware snd_als4000_playback =
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 static struct snd_pcm_hardware snd_als4000_capture =
443 {
444         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
445                                  SNDRV_PCM_INFO_MMAP_VALID),
446         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
447                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
448         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
449         .rate_min =             4000,
450         .rate_max =             48000,
451         .channels_min =         1,
452         .channels_max =         2,
453         .buffer_bytes_max =     65536,
454         .period_bytes_min =     64,
455         .period_bytes_max =     65536,
456         .periods_min =          1,
457         .periods_max =          1024,
458         .fifo_size =            0
459 };
460
461 /*****************************************************************/
462
463 static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
464 {
465         struct snd_sb *chip = snd_pcm_substream_chip(substream);
466         struct snd_pcm_runtime *runtime = substream->runtime;
467
468         chip->playback_substream = substream;
469         runtime->hw = snd_als4000_playback;
470         return 0;
471 }
472
473 static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
474 {
475         struct snd_sb *chip = snd_pcm_substream_chip(substream);
476
477         chip->playback_substream = NULL;
478         snd_pcm_lib_free_pages(substream);
479         return 0;
480 }
481
482 static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
483 {
484         struct snd_sb *chip = snd_pcm_substream_chip(substream);
485         struct snd_pcm_runtime *runtime = substream->runtime;
486
487         chip->capture_substream = substream;
488         runtime->hw = snd_als4000_capture;
489         return 0;
490 }
491
492 static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
493 {
494         struct snd_sb *chip = snd_pcm_substream_chip(substream);
495
496         chip->capture_substream = NULL;
497         snd_pcm_lib_free_pages(substream);
498         return 0;
499 }
500
501 /******************************************************************/
502
503 static struct snd_pcm_ops snd_als4000_playback_ops = {
504         .open =         snd_als4000_playback_open,
505         .close =        snd_als4000_playback_close,
506         .ioctl =        snd_pcm_lib_ioctl,
507         .hw_params =    snd_als4000_hw_params,
508         .hw_free =      snd_als4000_hw_free,
509         .prepare =      snd_als4000_playback_prepare,
510         .trigger =      snd_als4000_playback_trigger,
511         .pointer =      snd_als4000_playback_pointer
512 };
513
514 static struct snd_pcm_ops snd_als4000_capture_ops = {
515         .open =         snd_als4000_capture_open,
516         .close =        snd_als4000_capture_close,
517         .ioctl =        snd_pcm_lib_ioctl,
518         .hw_params =    snd_als4000_hw_params,
519         .hw_free =      snd_als4000_hw_free,
520         .prepare =      snd_als4000_capture_prepare,
521         .trigger =      snd_als4000_capture_trigger,
522         .pointer =      snd_als4000_capture_pointer
523 };
524
525 static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
526 {
527         struct snd_pcm *pcm;
528         int err;
529
530         if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
531                 return err;
532         pcm->private_data = chip;
533         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
534         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
535         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
536
537         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
538                                               64*1024, 64*1024);
539
540         chip->pcm = pcm;
541
542         return 0;
543 }
544
545 /******************************************************************/
546
547 static void snd_als4000_set_addr(unsigned long gcr,
548                                         unsigned int sb,
549                                         unsigned int mpu,
550                                         unsigned int opl,
551                                         unsigned int game)
552 {
553         u32 confA = 0;
554         u32 confB = 0;
555
556         if (mpu > 0)
557                 confB |= (mpu | 1) << 16;
558         if (sb > 0)
559                 confB |= (sb | 1);
560         if (game > 0)
561                 confA |= (game | 1) << 16;
562         if (opl > 0)    
563                 confA |= (opl | 1);
564         snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
565         snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
566 }
567
568 static void snd_als4000_configure(struct snd_sb *chip)
569 {
570         unsigned tmp;
571         int i;
572
573         /* do some more configuration */
574         spin_lock_irq(&chip->mixer_lock);
575         tmp = snd_sbmixer_read(chip, 0xc0);
576         snd_sbmixer_write(chip, 0xc0, tmp|0x80);
577         /* always select DMA channel 0, since we do not actually use DMA */
578         snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
579         snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
580         spin_unlock_irq(&chip->mixer_lock);
581         
582         spin_lock_irq(&chip->reg_lock);
583         /* magic number. Enables interrupts(?) */
584         snd_als4000_gcr_write(chip, 0x8c, 0x28000);
585         for(i = 0x91; i <= 0x96; ++i)
586                 snd_als4000_gcr_write(chip, i, 0);
587         
588         snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
589         spin_unlock_irq(&chip->reg_lock);
590 }
591
592 #ifdef SUPPORT_JOYSTICK
593 static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
594 {
595         struct gameport *gp;
596         struct resource *r;
597         int io_port;
598
599         if (joystick_port[dev] == 0)
600                 return -ENODEV;
601
602         if (joystick_port[dev] == 1) { /* auto-detect */
603                 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
604                         r = request_region(io_port, 8, "ALS4000 gameport");
605                         if (r)
606                                 break;
607                 }
608         } else {
609                 io_port = joystick_port[dev];
610                 r = request_region(io_port, 8, "ALS4000 gameport");
611         }
612
613         if (!r) {
614                 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
615                 return -EBUSY;
616         }
617
618         acard->gameport = gp = gameport_allocate_port();
619         if (!gp) {
620                 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
621                 release_and_free_resource(r);
622                 return -ENOMEM;
623         }
624
625         gameport_set_name(gp, "ALS4000 Gameport");
626         gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
627         gameport_set_dev_parent(gp, &acard->pci->dev);
628         gp->io = io_port;
629         gameport_set_port_data(gp, r);
630
631         /* Enable legacy joystick port */
632         snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
633
634         gameport_register_port(acard->gameport);
635
636         return 0;
637 }
638
639 static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
640 {
641         if (acard->gameport) {
642                 struct resource *r = gameport_get_port_data(acard->gameport);
643
644                 gameport_unregister_port(acard->gameport);
645                 acard->gameport = NULL;
646
647                 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
648                 release_and_free_resource(r);
649         }
650 }
651 #else
652 static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
653 static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
654 #endif
655
656 static void snd_card_als4000_free( struct snd_card *card )
657 {
658         struct snd_card_als4000 * acard = (struct snd_card_als4000 *)card->private_data;
659
660         /* make sure that interrupts are disabled */
661         snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
662         /* free resources */
663         snd_als4000_free_gameport(acard);
664         pci_release_regions(acard->pci);
665         pci_disable_device(acard->pci);
666 }
667
668 static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
669                                           const struct pci_device_id *pci_id)
670 {
671         static int dev;
672         struct snd_card *card;
673         struct snd_card_als4000 *acard;
674         unsigned long gcr;
675         struct snd_sb *chip;
676         struct snd_opl3 *opl3;
677         unsigned short word;
678         int err;
679
680         if (dev >= SNDRV_CARDS)
681                 return -ENODEV;
682         if (!enable[dev]) {
683                 dev++;
684                 return -ENOENT;
685         }
686
687         /* enable PCI device */
688         if ((err = pci_enable_device(pci)) < 0) {
689                 return err;
690         }
691         /* check, if we can restrict PCI DMA transfers to 24 bits */
692         if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
693             pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
694                 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
695                 pci_disable_device(pci);
696                 return -ENXIO;
697         }
698
699         if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
700                 pci_disable_device(pci);
701                 return err;
702         }
703         gcr = pci_resource_start(pci, 0);
704
705         pci_read_config_word(pci, PCI_COMMAND, &word);
706         pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
707         pci_set_master(pci);
708         
709         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 
710                             sizeof( struct snd_card_als4000 ) );
711         if (card == NULL) {
712                 pci_release_regions(pci);
713                 pci_disable_device(pci);
714                 return -ENOMEM;
715         }
716
717         acard = (struct snd_card_als4000 *)card->private_data;
718         acard->pci = pci;
719         acard->gcr = gcr;
720         card->private_free = snd_card_als4000_free;
721
722         /* disable all legacy ISA stuff */
723         snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
724
725         if ((err = snd_sbdsp_create(card,
726                                     gcr + 0x10,
727                                     pci->irq,
728                                     snd_als4000_interrupt,
729                                     -1,
730                                     -1,
731                                     SB_HW_ALS4000,
732                                     &chip)) < 0) {
733                 goto out_err;
734         }
735         acard->chip = chip;
736
737         chip->pci = pci;
738         chip->alt_port = gcr;
739         snd_card_set_dev(card, &pci->dev);
740
741         snd_als4000_configure(chip);
742
743         strcpy(card->driver, "ALS4000");
744         strcpy(card->shortname, "Avance Logic ALS4000");
745         sprintf(card->longname, "%s at 0x%lx, irq %i",
746                 card->shortname, chip->alt_port, chip->irq);
747
748         if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
749                                         gcr+0x30, MPU401_INFO_INTEGRATED,
750                                         pci->irq, 0, &chip->rmidi)) < 0) {
751                 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n", gcr+0x30);
752                 goto out_err;
753         }
754
755         if ((err = snd_als4000_pcm(chip, 0)) < 0) {
756                 goto out_err;
757         }
758         if ((err = snd_sbmixer_new(chip)) < 0) {
759                 goto out_err;
760         }           
761
762         if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
763                             OPL3_HW_AUTO, 1, &opl3) < 0) {
764                 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
765                            gcr+0x10, gcr+0x12 );
766         } else {
767                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
768                         goto out_err;
769                 }
770         }
771
772         snd_als4000_create_gameport(acard, dev);
773
774         if ((err = snd_card_register(card)) < 0) {
775                 goto out_err;
776         }
777         pci_set_drvdata(pci, card);
778         dev++;
779         err = 0;
780         goto out;
781
782 out_err:
783         snd_card_free(card);
784         
785 out:
786         return err;
787 }
788
789 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
790 {
791         snd_card_free(pci_get_drvdata(pci));
792         pci_set_drvdata(pci, NULL);
793 }
794
795 #ifdef CONFIG_PM
796 static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
797 {
798         struct snd_card *card = pci_get_drvdata(pci);
799         struct snd_card_als4000 *acard = card->private_data;
800         struct snd_sb *chip = acard->chip;
801
802         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
803         
804         snd_pcm_suspend_all(chip->pcm);
805         snd_sbmixer_suspend(chip);
806
807         pci_disable_device(pci);
808         pci_save_state(pci);
809         pci_set_power_state(pci, pci_choose_state(pci, state));
810         return 0;
811 }
812
813 static int snd_als4000_resume(struct pci_dev *pci)
814 {
815         struct snd_card *card = pci_get_drvdata(pci);
816         struct snd_card_als4000 *acard = card->private_data;
817         struct snd_sb *chip = acard->chip;
818
819         pci_set_power_state(pci, PCI_D0);
820         pci_restore_state(pci);
821         if (pci_enable_device(pci) < 0) {
822                 printk(KERN_ERR "als4000: pci_enable_device failed, "
823                        "disabling device\n");
824                 snd_card_disconnect(card);
825                 return -EIO;
826         }
827         pci_set_master(pci);
828
829         snd_als4000_configure(chip);
830         snd_sbdsp_reset(chip);
831         snd_sbmixer_resume(chip);
832
833 #ifdef SUPPORT_JOYSTICK
834         if (acard->gameport)
835                 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
836 #endif
837
838         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
839         return 0;
840 }
841 #endif
842
843
844 static struct pci_driver driver = {
845         .name = "ALS4000",
846         .id_table = snd_als4000_ids,
847         .probe = snd_card_als4000_probe,
848         .remove = __devexit_p(snd_card_als4000_remove),
849 #ifdef CONFIG_PM
850         .suspend = snd_als4000_suspend,
851         .resume = snd_als4000_resume,
852 #endif
853 };
854
855 static int __init alsa_card_als4000_init(void)
856 {
857         return pci_register_driver(&driver);
858 }
859
860 static void __exit alsa_card_als4000_exit(void)
861 {
862         pci_unregister_driver(&driver);
863 }
864
865 module_init(alsa_card_als4000_init)
866 module_exit(alsa_card_als4000_exit)