ALSA: als4000 - Code clean up
[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  * - by default, don't enable legacy game and use PCI game I/O
64  * - power management? (card can do voice wakeup according to datasheet!!)
65  */
66
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 iobase;
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 enum als4k_iobase_t {
127         /* IOx: B == Byte, W = Word, D = DWord */
128         ALS4K_IOD_00_AC97_ACCESS = 0x00,
129         ALS4K_IOW_04_AC97_READ = 0x04,
130         ALS4K_IOB_06_AC97_STATUS = 0x06,
131         ALS4K_IOB_07_IRQSTATUS = 0x07,
132         ALS4K_IOD_08_GCR_DATA = 0x08,
133         ALS4K_IOB_0C_GCR_INDEX = 0x0c,
134         ALS4K_IOB_0E_SB_MPU_IRQ = 0x0e,
135         ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
136         ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
137         ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
138         ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
139         ALS4K_IOB_14_MIXER_INDEX = 0x14,
140         ALS4K_IOB_15_MIXER_DATA = 0x15,
141         ALS4K_IOB_16_ESP_RST_PORT = 0x16,
142         ALS4K_IOB_16_CR1E_ACK_PORT = 0x16, /* 2nd function */
143         ALS4K_IOB_18_OPL_ADDR0 = 0x18,
144         ALS4K_IOB_19_OPL_ADDR1 = 0x19,
145         ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
146         ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
147         ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
148         ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
149         ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
150         ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
151         ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
152         ALS4K_IOB_30_MIDI_DATA = 0x30,
153         ALS4K_IOB_31_MIDI_STATUS = 0x31,
154         ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
155 };
156
157 enum als4k_gcr_t {
158         /* all registers 32bit wide */
159         ALS4K_GCR_8C_MISC_CTRL = 0x8c,
160         ALS4K_GCR_90_TEST_MODE_REG = 0x90,
161         ALS4K_GCR_91_DMA0_ADDR = 0x91,
162         ALS4K_GCR_92_DMA0_MODE_COUNT = 0x92,
163         ALS4K_GCR_93_DMA1_ADDR = 0x93,
164         ALS4K_GCR_94_DMA1_MODE_COUNT = 0x94,
165         ALS4K_GCR_95_DMA3_ADDR = 0x95,
166         ALS4K_GCR_96_DMA3_MODE_COUNT = 0x96,
167         ALS4K_GCR_99_DMA_EMULATION_CTRL = 0x99,
168         ALS4K_GCR_A0_FIFO1_CURRENT_ADDR = 0xa0,
169         ALS4K_GCR_A1_FIFO1_STATUS_BYTECOUNT = 0xa1,
170         ALS4K_GCR_A2_FIFO2_PCIADDR = 0xa2,
171         ALS4K_GCR_A3_FIFO2_COUNT = 0xa3,
172         ALS4K_GCR_A4_FIFO2_CURRENT_ADDR = 0xa4,
173         ALS4K_GCR_A5_FIFO1_STATUS_BYTECOUNT = 0xa5,
174         ALS4K_GCR_A6_PM_CTRL = 0xa6,
175         ALS4K_GCR_A7_PCI_ACCESS_STORAGE = 0xa7,
176         ALS4K_GCR_A8_LEGACY_CFG1 = 0xa8,
177         ALS4K_GCR_A9_LEGACY_CFG2 = 0xa9,
178         ALS4K_GCR_FF_DUMMY_SCRATCH = 0xff,
179 };
180
181 enum als4k_gcr_8c_t {
182         ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
183         ALS4K_GCR_8C_CHIP_REV_MASK = 0xf0000
184 };
185
186 static inline void snd_als4000_gcr_write_addr(unsigned long iobase,
187                                                  enum als4k_gcr_t reg,
188                                                  u32 val)
189 {
190         outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
191         outl(val, iobase + ALS4K_IOD_08_GCR_DATA);
192 }
193
194 static inline void snd_als4000_gcr_write(struct snd_sb *sb,
195                                                  enum als4k_gcr_t reg,
196                                                  u32 val)
197 {
198         snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
199 }       
200
201 static inline u32 snd_als4000_gcr_read_addr(unsigned long iobase,
202                                                  enum als4k_gcr_t reg)
203 {
204         outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
205         return inl(iobase + ALS4K_IOD_08_GCR_DATA);
206 }
207
208 static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
209 {
210         return snd_als4000_gcr_read_addr(sb->alt_port, reg);
211 }
212
213 static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
214 {
215         if (!(chip->mode & SB_RATE_LOCK)) {
216                 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
217                 snd_sbdsp_command(chip, rate>>8);
218                 snd_sbdsp_command(chip, rate);
219         }
220 }
221
222 static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
223                                                dma_addr_t addr, unsigned size)
224 {
225         snd_als4000_gcr_write(chip, ALS4K_GCR_A2_FIFO2_PCIADDR, addr);
226         snd_als4000_gcr_write(chip, ALS4K_GCR_A3_FIFO2_COUNT, (size-1));
227 }
228
229 static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
230                                                 dma_addr_t addr,
231                                                 unsigned size)
232 {
233         snd_als4000_gcr_write(chip, ALS4K_GCR_91_DMA0_ADDR, addr);
234         snd_als4000_gcr_write(chip, ALS4K_GCR_92_DMA0_MODE_COUNT,
235                                                         (size-1)|0x180000);
236 }
237
238 #define ALS4000_FORMAT_SIGNED   (1<<0)
239 #define ALS4000_FORMAT_16BIT    (1<<1)
240 #define ALS4000_FORMAT_STEREO   (1<<2)
241
242 static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
243 {
244         int result;
245
246         result = 0;
247         if (snd_pcm_format_signed(runtime->format))
248                 result |= ALS4000_FORMAT_SIGNED;
249         if (snd_pcm_format_physical_width(runtime->format) == 16)
250                 result |= ALS4000_FORMAT_16BIT;
251         if (runtime->channels > 1)
252                 result |= ALS4000_FORMAT_STEREO;
253         return result;
254 }
255
256 /* structure for setting up playback */
257 static const struct {
258         unsigned char dsp_cmd, dma_on, dma_off, format;
259 } playback_cmd_vals[]={
260 /* ALS4000_FORMAT_U8_MONO */
261 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
262 /* ALS4000_FORMAT_S8_MONO */    
263 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
264 /* ALS4000_FORMAT_U16L_MONO */
265 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
266 /* ALS4000_FORMAT_S16L_MONO */
267 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
268 /* ALS4000_FORMAT_U8_STEREO */
269 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
270 /* ALS4000_FORMAT_S8_STEREO */  
271 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
272 /* ALS4000_FORMAT_U16L_STEREO */
273 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
274 /* ALS4000_FORMAT_S16L_STEREO */
275 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
276 };
277 #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
278
279 /* structure for setting up capture */
280 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
281 static const unsigned char capture_cmd_vals[]=
282 {
283 CMD_WIDTH8|CMD_MONO,                    /* ALS4000_FORMAT_U8_MONO */
284 CMD_WIDTH8|CMD_SIGNED|CMD_MONO,         /* ALS4000_FORMAT_S8_MONO */    
285 CMD_MONO,                               /* ALS4000_FORMAT_U16L_MONO */
286 CMD_SIGNED|CMD_MONO,                    /* ALS4000_FORMAT_S16L_MONO */
287 CMD_WIDTH8|CMD_STEREO,                  /* ALS4000_FORMAT_U8_STEREO */
288 CMD_WIDTH8|CMD_SIGNED|CMD_STEREO,       /* ALS4000_FORMAT_S8_STEREO */  
289 CMD_STEREO,                             /* ALS4000_FORMAT_U16L_STEREO */
290 CMD_SIGNED|CMD_STEREO,                  /* ALS4000_FORMAT_S16L_STEREO */
291 };      
292 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
293
294 static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
295                                  struct snd_pcm_hw_params *hw_params)
296 {
297         return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
298 }
299
300 static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
301 {
302         snd_pcm_lib_free_pages(substream);
303         return 0;
304 }
305
306 static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
307 {
308         struct snd_sb *chip = snd_pcm_substream_chip(substream);
309         struct snd_pcm_runtime *runtime = substream->runtime;
310         unsigned long size;
311         unsigned count;
312
313         chip->capture_format = snd_als4000_get_format(runtime);
314                 
315         size = snd_pcm_lib_buffer_bytes(substream);
316         count = snd_pcm_lib_period_bytes(substream);
317         
318         if (chip->capture_format & ALS4000_FORMAT_16BIT)
319                 count >>=1;
320         count--;
321
322         spin_lock_irq(&chip->reg_lock);
323         snd_als4000_set_rate(chip, runtime->rate);
324         snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
325         spin_unlock_irq(&chip->reg_lock);
326         spin_lock_irq(&chip->mixer_lock);
327         snd_sbmixer_write(chip, 0xdc, count);
328         snd_sbmixer_write(chip, 0xdd, count>>8);
329         spin_unlock_irq(&chip->mixer_lock);
330         return 0;
331 }
332
333 static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
334 {
335         struct snd_sb *chip = snd_pcm_substream_chip(substream);
336         struct snd_pcm_runtime *runtime = substream->runtime;
337         unsigned long size;
338         unsigned count;
339
340         chip->playback_format = snd_als4000_get_format(runtime);
341         
342         size = snd_pcm_lib_buffer_bytes(substream);
343         count = snd_pcm_lib_period_bytes(substream);
344         
345         if (chip->playback_format & ALS4000_FORMAT_16BIT)
346                 count >>=1;
347         count--;
348         
349         /* FIXME: from second playback on, there's a lot more clicks and pops
350          * involved here than on first playback. Fiddling with
351          * tons of different settings didn't help (DMA, speaker on/off,
352          * reordering, ...). Something seems to get enabled on playback
353          * that I haven't found out how to disable again, which then causes
354          * the switching pops to reach the speakers the next time here. */
355         spin_lock_irq(&chip->reg_lock);
356         snd_als4000_set_rate(chip, runtime->rate);
357         snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
358         
359         /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
360         /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
361         snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
362         snd_sbdsp_command(chip, playback_cmd(chip).format);
363         snd_sbdsp_command(chip, count);
364         snd_sbdsp_command(chip, count>>8);
365         snd_sbdsp_command(chip, playback_cmd(chip).dma_off);    
366         spin_unlock_irq(&chip->reg_lock);
367         
368         return 0;
369 }
370
371 static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
372 {
373         struct snd_sb *chip = snd_pcm_substream_chip(substream);
374         int result = 0;
375         
376         /* FIXME race condition in here!!!
377            chip->mode non-atomic update gets consistently protected
378            by reg_lock always, _except_ for this place!!
379            Probably need to take reg_lock as outer (or inner??) lock, too.
380            (or serialize both lock operations? probably not, though... - racy?)
381         */
382         spin_lock(&chip->mixer_lock);
383         switch (cmd) {
384         case SNDRV_PCM_TRIGGER_START:
385         case SNDRV_PCM_TRIGGER_RESUME:
386                 chip->mode |= SB_RATE_LOCK_CAPTURE;
387                 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
388                 break;
389         case SNDRV_PCM_TRIGGER_STOP:
390         case SNDRV_PCM_TRIGGER_SUSPEND:
391                 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
392                 snd_sbmixer_write(chip, 0xde, 0);
393                 break;
394         default:
395                 result = -EINVAL;
396                 break;
397         }
398         spin_unlock(&chip->mixer_lock);
399         return result;
400 }
401
402 static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
403 {
404         struct snd_sb *chip = snd_pcm_substream_chip(substream);
405         int result = 0;
406
407         spin_lock(&chip->reg_lock);
408         switch (cmd) {
409         case SNDRV_PCM_TRIGGER_START:
410         case SNDRV_PCM_TRIGGER_RESUME:
411                 chip->mode |= SB_RATE_LOCK_PLAYBACK;
412                 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
413                 break;
414         case SNDRV_PCM_TRIGGER_STOP:
415         case SNDRV_PCM_TRIGGER_SUSPEND:
416                 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
417                 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
418                 break;
419         default:
420                 result = -EINVAL;
421                 break;
422         }
423         spin_unlock(&chip->reg_lock);
424         return result;
425 }
426
427 static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
428 {
429         struct snd_sb *chip = snd_pcm_substream_chip(substream);
430         unsigned int result;
431
432         spin_lock(&chip->reg_lock);     
433         result = snd_als4000_gcr_read(chip, ALS4K_GCR_A4_FIFO2_CURRENT_ADDR);
434         result &= 0xffff;
435         spin_unlock(&chip->reg_lock);
436         return bytes_to_frames( substream->runtime, result );
437 }
438
439 static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
440 {
441         struct snd_sb *chip = snd_pcm_substream_chip(substream);
442         unsigned result;
443
444         spin_lock(&chip->reg_lock);     
445         result = snd_als4000_gcr_read(chip, ALS4K_GCR_A0_FIFO1_CURRENT_ADDR);
446         result &= 0xffff;
447         spin_unlock(&chip->reg_lock);
448         return bytes_to_frames( substream->runtime, result );
449 }
450
451 /* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
452  * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
453  * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
454  * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
455  * register (alt_port + ALS4K_IOB_0E_SB_MPU_IRQ). Probably something
456  * could be optimized here to query/write one register only...
457  * And even if both registers need to be queried, then there's still the
458  * question of whether it's actually correct to ACK PCI IRQ before reading
459  * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
460  * SB IRQ status.
461  * (hmm, page 38 mentions it the other way around!)
462  * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
463  * */
464 static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
465 {
466         struct snd_sb *chip = dev_id;
467         unsigned gcr_status;
468         unsigned sb_status;
469
470         /* find out which bit of the ALS4000 produced the interrupt */
471         gcr_status = inb(chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
472
473         if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
474                 snd_pcm_period_elapsed(chip->playback_substream);
475         if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
476                 snd_pcm_period_elapsed(chip->capture_substream);
477         if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
478                 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
479         /* release the gcr */
480         outb(gcr_status, chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
481         
482         spin_lock(&chip->mixer_lock);
483         sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
484         spin_unlock(&chip->mixer_lock);
485         
486         if (sb_status & SB_IRQTYPE_8BIT) 
487                 snd_sb_ack_8bit(chip);
488         if (sb_status & SB_IRQTYPE_16BIT) 
489                 snd_sb_ack_16bit(chip);
490         if (sb_status & SB_IRQTYPE_MPUIN)
491                 inb(chip->mpu_port);
492         if (sb_status & 0x20)
493                 inb(SBP(chip, RESET));
494         return IRQ_HANDLED;
495 }
496
497 /*****************************************************************/
498
499 static struct snd_pcm_hardware snd_als4000_playback =
500 {
501         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
502                                  SNDRV_PCM_INFO_MMAP_VALID),
503         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
504                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
505         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
506         .rate_min =             4000,
507         .rate_max =             48000,
508         .channels_min =         1,
509         .channels_max =         2,
510         .buffer_bytes_max =     65536,
511         .period_bytes_min =     64,
512         .period_bytes_max =     65536,
513         .periods_min =          1,
514         .periods_max =          1024,
515         .fifo_size =            0
516 };
517
518 static struct snd_pcm_hardware snd_als4000_capture =
519 {
520         .info =                 (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
521                                  SNDRV_PCM_INFO_MMAP_VALID),
522         .formats =              SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
523                                 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE,      /* formats */
524         .rates =                SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
525         .rate_min =             4000,
526         .rate_max =             48000,
527         .channels_min =         1,
528         .channels_max =         2,
529         .buffer_bytes_max =     65536,
530         .period_bytes_min =     64,
531         .period_bytes_max =     65536,
532         .periods_min =          1,
533         .periods_max =          1024,
534         .fifo_size =            0
535 };
536
537 /*****************************************************************/
538
539 static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
540 {
541         struct snd_sb *chip = snd_pcm_substream_chip(substream);
542         struct snd_pcm_runtime *runtime = substream->runtime;
543
544         chip->playback_substream = substream;
545         runtime->hw = snd_als4000_playback;
546         return 0;
547 }
548
549 static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
550 {
551         struct snd_sb *chip = snd_pcm_substream_chip(substream);
552
553         chip->playback_substream = NULL;
554         snd_pcm_lib_free_pages(substream);
555         return 0;
556 }
557
558 static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
559 {
560         struct snd_sb *chip = snd_pcm_substream_chip(substream);
561         struct snd_pcm_runtime *runtime = substream->runtime;
562
563         chip->capture_substream = substream;
564         runtime->hw = snd_als4000_capture;
565         return 0;
566 }
567
568 static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
569 {
570         struct snd_sb *chip = snd_pcm_substream_chip(substream);
571
572         chip->capture_substream = NULL;
573         snd_pcm_lib_free_pages(substream);
574         return 0;
575 }
576
577 /******************************************************************/
578
579 static struct snd_pcm_ops snd_als4000_playback_ops = {
580         .open =         snd_als4000_playback_open,
581         .close =        snd_als4000_playback_close,
582         .ioctl =        snd_pcm_lib_ioctl,
583         .hw_params =    snd_als4000_hw_params,
584         .hw_free =      snd_als4000_hw_free,
585         .prepare =      snd_als4000_playback_prepare,
586         .trigger =      snd_als4000_playback_trigger,
587         .pointer =      snd_als4000_playback_pointer
588 };
589
590 static struct snd_pcm_ops snd_als4000_capture_ops = {
591         .open =         snd_als4000_capture_open,
592         .close =        snd_als4000_capture_close,
593         .ioctl =        snd_pcm_lib_ioctl,
594         .hw_params =    snd_als4000_hw_params,
595         .hw_free =      snd_als4000_hw_free,
596         .prepare =      snd_als4000_capture_prepare,
597         .trigger =      snd_als4000_capture_trigger,
598         .pointer =      snd_als4000_capture_pointer
599 };
600
601 static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
602 {
603         struct snd_pcm *pcm;
604         int err;
605
606         if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
607                 return err;
608         pcm->private_data = chip;
609         pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
610         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
611         snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
612
613         snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
614                                               64*1024, 64*1024);
615
616         chip->pcm = pcm;
617
618         return 0;
619 }
620
621 /******************************************************************/
622
623 static void snd_als4000_set_addr(unsigned long iobase,
624                                         unsigned int sb_io,
625                                         unsigned int mpu_io,
626                                         unsigned int opl_io,
627                                         unsigned int game_io)
628 {
629         u32 cfg1 = 0;
630         u32 cfg2 = 0;
631
632         if (mpu_io > 0)
633                 cfg2 |= (mpu_io | 1) << 16;
634         if (sb_io > 0)
635                 cfg2 |= (sb_io | 1);
636         if (game_io > 0)
637                 cfg1 |= (game_io | 1) << 16;
638         if (opl_io > 0)
639                 cfg1 |= (opl_io | 1);
640         snd_als4000_gcr_write_addr(iobase, ALS4K_GCR_A8_LEGACY_CFG1, cfg1);
641         snd_als4000_gcr_write_addr(iobase, ALS4K_GCR_A9_LEGACY_CFG2, cfg2);
642 }
643
644 static void snd_als4000_configure(struct snd_sb *chip)
645 {
646         unsigned tmp;
647         int i;
648
649         /* do some more configuration */
650         spin_lock_irq(&chip->mixer_lock);
651         tmp = snd_sbmixer_read(chip, 0xc0);
652         snd_sbmixer_write(chip, 0xc0, tmp|0x80);
653         /* always select DMA channel 0, since we do not actually use DMA */
654         snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
655         snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
656         spin_unlock_irq(&chip->mixer_lock);
657         
658         spin_lock_irq(&chip->reg_lock);
659         /* enable interrupts */
660         snd_als4000_gcr_write(chip, ALS4K_GCR_8C_MISC_CTRL,
661                                          ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE);
662
663         for (i = ALS4K_GCR_91_DMA0_ADDR; i <= ALS4K_GCR_96_DMA3_MODE_COUNT; ++i)
664                 snd_als4000_gcr_write(chip, i, 0);
665         
666         snd_als4000_gcr_write(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL,
667                 snd_als4000_gcr_read(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL));
668         spin_unlock_irq(&chip->reg_lock);
669 }
670
671 #ifdef SUPPORT_JOYSTICK
672 static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
673 {
674         struct gameport *gp;
675         struct resource *r;
676         int io_port;
677
678         if (joystick_port[dev] == 0)
679                 return -ENODEV;
680
681         if (joystick_port[dev] == 1) { /* auto-detect */
682                 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
683                         r = request_region(io_port, 8, "ALS4000 gameport");
684                         if (r)
685                                 break;
686                 }
687         } else {
688                 io_port = joystick_port[dev];
689                 r = request_region(io_port, 8, "ALS4000 gameport");
690         }
691
692         if (!r) {
693                 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
694                 return -EBUSY;
695         }
696
697         acard->gameport = gp = gameport_allocate_port();
698         if (!gp) {
699                 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
700                 release_and_free_resource(r);
701                 return -ENOMEM;
702         }
703
704         gameport_set_name(gp, "ALS4000 Gameport");
705         gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
706         gameport_set_dev_parent(gp, &acard->pci->dev);
707         gp->io = io_port;
708         gameport_set_port_data(gp, r);
709
710         /* Enable legacy joystick port */
711         snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
712
713         gameport_register_port(acard->gameport);
714
715         return 0;
716 }
717
718 static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
719 {
720         if (acard->gameport) {
721                 struct resource *r = gameport_get_port_data(acard->gameport);
722
723                 gameport_unregister_port(acard->gameport);
724                 acard->gameport = NULL;
725
726                 /* disable joystick */
727                 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
728
729                 release_and_free_resource(r);
730         }
731 }
732 #else
733 static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
734 static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
735 #endif
736
737 static void snd_card_als4000_free( struct snd_card *card )
738 {
739         struct snd_card_als4000 *acard = card->private_data;
740
741         /* make sure that interrupts are disabled */
742         snd_als4000_gcr_write_addr(acard->iobase, ALS4K_GCR_8C_MISC_CTRL, 0);
743         /* free resources */
744         snd_als4000_free_gameport(acard);
745         pci_release_regions(acard->pci);
746         pci_disable_device(acard->pci);
747 }
748
749 static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
750                                           const struct pci_device_id *pci_id)
751 {
752         static int dev;
753         struct snd_card *card;
754         struct snd_card_als4000 *acard;
755         unsigned long iobase;
756         struct snd_sb *chip;
757         struct snd_opl3 *opl3;
758         unsigned short word;
759         int err;
760
761         if (dev >= SNDRV_CARDS)
762                 return -ENODEV;
763         if (!enable[dev]) {
764                 dev++;
765                 return -ENOENT;
766         }
767
768         /* enable PCI device */
769         if ((err = pci_enable_device(pci)) < 0) {
770                 return err;
771         }
772         /* check, if we can restrict PCI DMA transfers to 24 bits */
773         if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
774             pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
775                 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
776                 pci_disable_device(pci);
777                 return -ENXIO;
778         }
779
780         if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
781                 pci_disable_device(pci);
782                 return err;
783         }
784         iobase = pci_resource_start(pci, 0);
785
786         pci_read_config_word(pci, PCI_COMMAND, &word);
787         pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
788         pci_set_master(pci);
789         
790         card = snd_card_new(index[dev], id[dev], THIS_MODULE, 
791                             sizeof( struct snd_card_als4000 ) );
792         if (card == NULL) {
793                 pci_release_regions(pci);
794                 pci_disable_device(pci);
795                 return -ENOMEM;
796         }
797
798         acard = card->private_data;
799         acard->pci = pci;
800         acard->iobase = iobase;
801         card->private_free = snd_card_als4000_free;
802
803         /* disable all legacy ISA stuff */
804         snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
805
806         if ((err = snd_sbdsp_create(card,
807                                     iobase + ALS4K_IOB_10_ADLIB_ADDR0,
808                                     pci->irq,
809                                     snd_als4000_interrupt,
810                                     -1,
811                                     -1,
812                                     SB_HW_ALS4000,
813                                     &chip)) < 0) {
814                 goto out_err;
815         }
816         acard->chip = chip;
817
818         chip->pci = pci;
819         chip->alt_port = iobase;
820         snd_card_set_dev(card, &pci->dev);
821
822         snd_als4000_configure(chip);
823
824         strcpy(card->driver, "ALS4000");
825         strcpy(card->shortname, "Avance Logic ALS4000");
826         sprintf(card->longname, "%s at 0x%lx, irq %i",
827                 card->shortname, chip->alt_port, chip->irq);
828
829         if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
830                                         iobase + ALS4K_IOB_30_MIDI_DATA,
831                                         MPU401_INFO_INTEGRATED,
832                                         pci->irq, 0, &chip->rmidi)) < 0) {
833                 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n",
834                                 iobase + ALS4K_IOB_30_MIDI_DATA);
835                 goto out_err;
836         }
837         /* FIXME: ALS4000 has interesting MPU401 configuration features
838          * at CR 0x1A (pass-thru / UART switching, fast MIDI clock, etc.),
839          * however there doesn't seem to be an ALSA API for this... */
840
841         if ((err = snd_als4000_pcm(chip, 0)) < 0) {
842                 goto out_err;
843         }
844         if ((err = snd_sbmixer_new(chip)) < 0) {
845                 goto out_err;
846         }           
847
848         if (snd_opl3_create(card,
849                                 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
850                                 iobase + ALS4K_IOB_12_ADLIB_ADDR2,
851                             OPL3_HW_AUTO, 1, &opl3) < 0) {
852                 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
853                            iobase + ALS4K_IOB_10_ADLIB_ADDR0,
854                            iobase + ALS4K_IOB_12_ADLIB_ADDR2);
855         } else {
856                 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
857                         goto out_err;
858                 }
859         }
860
861         snd_als4000_create_gameport(acard, dev);
862
863         if ((err = snd_card_register(card)) < 0) {
864                 goto out_err;
865         }
866         pci_set_drvdata(pci, card);
867         dev++;
868         err = 0;
869         goto out;
870
871 out_err:
872         snd_card_free(card);
873         
874 out:
875         return err;
876 }
877
878 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
879 {
880         snd_card_free(pci_get_drvdata(pci));
881         pci_set_drvdata(pci, NULL);
882 }
883
884 #ifdef CONFIG_PM
885 static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
886 {
887         struct snd_card *card = pci_get_drvdata(pci);
888         struct snd_card_als4000 *acard = card->private_data;
889         struct snd_sb *chip = acard->chip;
890
891         snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
892         
893         snd_pcm_suspend_all(chip->pcm);
894         snd_sbmixer_suspend(chip);
895
896         pci_disable_device(pci);
897         pci_save_state(pci);
898         pci_set_power_state(pci, pci_choose_state(pci, state));
899         return 0;
900 }
901
902 static int snd_als4000_resume(struct pci_dev *pci)
903 {
904         struct snd_card *card = pci_get_drvdata(pci);
905         struct snd_card_als4000 *acard = card->private_data;
906         struct snd_sb *chip = acard->chip;
907
908         pci_set_power_state(pci, PCI_D0);
909         pci_restore_state(pci);
910         if (pci_enable_device(pci) < 0) {
911                 printk(KERN_ERR "als4000: pci_enable_device failed, "
912                        "disabling device\n");
913                 snd_card_disconnect(card);
914                 return -EIO;
915         }
916         pci_set_master(pci);
917
918         snd_als4000_configure(chip);
919         snd_sbdsp_reset(chip);
920         snd_sbmixer_resume(chip);
921
922 #ifdef SUPPORT_JOYSTICK
923         if (acard->gameport)
924                 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
925 #endif
926
927         snd_power_change_state(card, SNDRV_CTL_POWER_D0);
928         return 0;
929 }
930 #endif /* CONFIG_PM */
931
932
933 static struct pci_driver driver = {
934         .name = "ALS4000",
935         .id_table = snd_als4000_ids,
936         .probe = snd_card_als4000_probe,
937         .remove = __devexit_p(snd_card_als4000_remove),
938 #ifdef CONFIG_PM
939         .suspend = snd_als4000_suspend,
940         .resume = snd_als4000_resume,
941 #endif
942 };
943
944 static int __init alsa_card_als4000_init(void)
945 {
946         return pci_register_driver(&driver);
947 }
948
949 static void __exit alsa_card_als4000_exit(void)
950 {
951         pci_unregister_driver(&driver);
952 }
953
954 module_init(alsa_card_als4000_init)
955 module_exit(alsa_card_als4000_exit)