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