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