blob: 7b2ff5f4672e27c721430badb24394a888e8548f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Andreas Mohrba7301c2005-11-17 11:03:31 +01009 *
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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 *
Andreas Mohrba7301c2005-11-17 11:03:31 +010061 * ToDo:
62 * - Proper shared IRQ handling?
63 * - power management? (card can do voice wakeup according to datasheet!!)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
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
81MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
82MODULE_DESCRIPTION("Avance Logic ALS4000");
83MODULE_LICENSE("GPL");
84MODULE_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
90static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
91static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
92static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
93#ifdef SUPPORT_JOYSTICK
94static int joystick_port[SNDRV_CARDS];
95#endif
96
97module_param_array(index, int, NULL, 0444);
98MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
99module_param_array(id, charp, NULL, 0444);
100MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
101module_param_array(enable, bool, NULL, 0444);
102MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
103#ifdef SUPPORT_JOYSTICK
104module_param_array(joystick_port, int, NULL, 0444);
105MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
106#endif
107
Takashi Iwai17c39d92005-11-17 15:02:01 +0100108struct snd_card_als4000 {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100109 /* most frequent access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned long gcr;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100111 struct pci_dev *pci;
Takashi Iwai70352912005-11-17 16:16:36 +0100112 struct snd_sb *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#ifdef SUPPORT_JOYSTICK
114 struct gameport *gameport;
115#endif
Takashi Iwai17c39d92005-11-17 15:02:01 +0100116};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118static struct pci_device_id snd_als4000_ids[] = {
119 { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
120 { 0, }
121};
122
123MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
124
125static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
126{
127 outb(reg, port+0x0c);
128 outl(val, port+0x08);
129}
130
Takashi Iwai17c39d92005-11-17 15:02:01 +0100131static inline void snd_als4000_gcr_write(struct snd_sb *sb, u32 reg, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
134}
135
136static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
137{
138 outb(reg, port+0x0c);
139 return inl(port+0x08);
140}
141
Takashi Iwai17c39d92005-11-17 15:02:01 +0100142static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, u32 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 return snd_als4000_gcr_read_addr(sb->alt_port, reg);
145}
146
Takashi Iwai17c39d92005-11-17 15:02:01 +0100147static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 if (!(chip->mode & SB_RATE_LOCK)) {
150 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
151 snd_sbdsp_command(chip, rate>>8);
152 snd_sbdsp_command(chip, rate);
153 }
154}
155
Takashi Iwai17c39d92005-11-17 15:02:01 +0100156static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
157 dma_addr_t addr, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 snd_als4000_gcr_write(chip, 0xa2, addr);
160 snd_als4000_gcr_write(chip, 0xa3, (size-1));
161}
162
Takashi Iwai17c39d92005-11-17 15:02:01 +0100163static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
164 dma_addr_t addr, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 snd_als4000_gcr_write(chip, 0x91, addr);
167 snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
168}
169
170#define ALS4000_FORMAT_SIGNED (1<<0)
171#define ALS4000_FORMAT_16BIT (1<<1)
172#define ALS4000_FORMAT_STEREO (1<<2)
173
Takashi Iwai17c39d92005-11-17 15:02:01 +0100174static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int result;
177
178 result = 0;
179 if (snd_pcm_format_signed(runtime->format))
180 result |= ALS4000_FORMAT_SIGNED;
181 if (snd_pcm_format_physical_width(runtime->format) == 16)
182 result |= ALS4000_FORMAT_16BIT;
183 if (runtime->channels > 1)
184 result |= ALS4000_FORMAT_STEREO;
185 return result;
186}
187
188/* structure for setting up playback */
Andreas Mohrba7301c2005-11-17 11:03:31 +0100189static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char dsp_cmd, dma_on, dma_off, format;
191} playback_cmd_vals[]={
192/* ALS4000_FORMAT_U8_MONO */
193{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
194/* ALS4000_FORMAT_S8_MONO */
195{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
196/* ALS4000_FORMAT_U16L_MONO */
197{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
198/* ALS4000_FORMAT_S16L_MONO */
199{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
200/* ALS4000_FORMAT_U8_STEREO */
201{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
202/* ALS4000_FORMAT_S8_STEREO */
203{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
204/* ALS4000_FORMAT_U16L_STEREO */
205{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
206/* ALS4000_FORMAT_S16L_STEREO */
207{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
208};
209#define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
210
211/* structure for setting up capture */
212enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
Andreas Mohrba7301c2005-11-17 11:03:31 +0100213static const unsigned char capture_cmd_vals[]=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
216CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
217CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
218CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
219CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
220CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
221CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
222CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
223};
224#define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
225
Takashi Iwai17c39d92005-11-17 15:02:01 +0100226static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
227 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
230}
231
Takashi Iwai17c39d92005-11-17 15:02:01 +0100232static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 snd_pcm_lib_free_pages(substream);
235 return 0;
236}
237
Takashi Iwai17c39d92005-11-17 15:02:01 +0100238static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100240 struct snd_sb *chip = snd_pcm_substream_chip(substream);
241 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned long size;
243 unsigned count;
244
245 chip->capture_format = snd_als4000_get_format(runtime);
246
247 size = snd_pcm_lib_buffer_bytes(substream);
248 count = snd_pcm_lib_period_bytes(substream);
249
250 if (chip->capture_format & ALS4000_FORMAT_16BIT)
251 count >>=1;
252 count--;
253
Takashi Iwai17c39d92005-11-17 15:02:01 +0100254 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 snd_als4000_set_rate(chip, runtime->rate);
256 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100257 spin_unlock_irq(&chip->reg_lock);
258 spin_lock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 snd_sbmixer_write(chip, 0xdc, count);
260 snd_sbmixer_write(chip, 0xdd, count>>8);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100261 spin_unlock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}
264
Takashi Iwai17c39d92005-11-17 15:02:01 +0100265static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100267 struct snd_sb *chip = snd_pcm_substream_chip(substream);
268 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 unsigned long size;
270 unsigned count;
271
272 chip->playback_format = snd_als4000_get_format(runtime);
273
274 size = snd_pcm_lib_buffer_bytes(substream);
275 count = snd_pcm_lib_period_bytes(substream);
276
277 if (chip->playback_format & ALS4000_FORMAT_16BIT)
278 count >>=1;
279 count--;
280
281 /* FIXME: from second playback on, there's a lot more clicks and pops
282 * involved here than on first playback. Fiddling with
283 * tons of different settings didn't help (DMA, speaker on/off,
284 * reordering, ...). Something seems to get enabled on playback
285 * that I haven't found out how to disable again, which then causes
286 * the switching pops to reach the speakers the next time here. */
Takashi Iwai17c39d92005-11-17 15:02:01 +0100287 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 snd_als4000_set_rate(chip, runtime->rate);
289 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
290
291 /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
292 /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
293 snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
294 snd_sbdsp_command(chip, playback_cmd(chip).format);
295 snd_sbdsp_command(chip, count);
296 snd_sbdsp_command(chip, count>>8);
297 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100298 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return 0;
301}
302
Takashi Iwai17c39d92005-11-17 15:02:01 +0100303static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100305 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int result = 0;
307
308 spin_lock(&chip->mixer_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100309 switch (cmd) {
310 case SNDRV_PCM_TRIGGER_START:
311 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 chip->mode |= SB_RATE_LOCK_CAPTURE;
313 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
Takashi Iwai70352912005-11-17 16:16:36 +0100314 break;
315 case SNDRV_PCM_TRIGGER_STOP:
316 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
318 snd_sbmixer_write(chip, 0xde, 0);
Takashi Iwai70352912005-11-17 16:16:36 +0100319 break;
320 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100322 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 spin_unlock(&chip->mixer_lock);
325 return result;
326}
327
Takashi Iwai17c39d92005-11-17 15:02:01 +0100328static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100330 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int result = 0;
332
333 spin_lock(&chip->reg_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100334 switch (cmd) {
335 case SNDRV_PCM_TRIGGER_START:
336 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 chip->mode |= SB_RATE_LOCK_PLAYBACK;
338 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
Takashi Iwai70352912005-11-17 16:16:36 +0100339 break;
340 case SNDRV_PCM_TRIGGER_STOP:
341 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
343 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
Takashi Iwai70352912005-11-17 16:16:36 +0100344 break;
345 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100347 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349 spin_unlock(&chip->reg_lock);
350 return result;
351}
352
Takashi Iwai17c39d92005-11-17 15:02:01 +0100353static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100355 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned int result;
357
358 spin_lock(&chip->reg_lock);
359 result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
360 spin_unlock(&chip->reg_lock);
361 return bytes_to_frames( substream->runtime, result );
362}
363
Takashi Iwai17c39d92005-11-17 15:02:01 +0100364static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100366 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 unsigned result;
368
369 spin_lock(&chip->reg_lock);
370 result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
371 spin_unlock(&chip->reg_lock);
372 return bytes_to_frames( substream->runtime, result );
373}
374
Andreas Mohrba7301c2005-11-17 11:03:31 +0100375/* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
376 * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
377 * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
378 * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
379 * register (alt_port + 0x0e). Probably something could be optimized here to
380 * query/write one register only...
381 * And even if both registers need to be queried, then there's still the
382 * question of whether it's actually correct to ACK PCI IRQ before reading
383 * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
384 * SB IRQ status.
385 * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
386 * */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
388{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100389 struct snd_sb *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned gcr_status;
391 unsigned sb_status;
392
393 /* find out which bit of the ALS4000 produced the interrupt */
394 gcr_status = inb(chip->alt_port + 0xe);
395
396 if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
397 snd_pcm_period_elapsed(chip->playback_substream);
398 if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
399 snd_pcm_period_elapsed(chip->capture_substream);
400 if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
Takashi Iwai11ca9af2005-05-27 11:34:34 +0200401 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* release the gcr */
403 outb(gcr_status, chip->alt_port + 0xe);
404
405 spin_lock(&chip->mixer_lock);
406 sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
407 spin_unlock(&chip->mixer_lock);
408
409 if (sb_status & SB_IRQTYPE_8BIT)
410 snd_sb_ack_8bit(chip);
411 if (sb_status & SB_IRQTYPE_16BIT)
412 snd_sb_ack_16bit(chip);
413 if (sb_status & SB_IRQTYPE_MPUIN)
414 inb(chip->mpu_port);
415 if (sb_status & 0x20)
416 inb(SBP(chip, RESET));
417 return IRQ_HANDLED;
418}
419
420/*****************************************************************/
421
Takashi Iwai17c39d92005-11-17 15:02:01 +0100422static struct snd_pcm_hardware snd_als4000_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
425 SNDRV_PCM_INFO_MMAP_VALID),
426 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
427 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
428 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
429 .rate_min = 4000,
430 .rate_max = 48000,
431 .channels_min = 1,
432 .channels_max = 2,
433 .buffer_bytes_max = 65536,
434 .period_bytes_min = 64,
435 .period_bytes_max = 65536,
436 .periods_min = 1,
437 .periods_max = 1024,
438 .fifo_size = 0
439};
440
Takashi Iwai17c39d92005-11-17 15:02:01 +0100441static struct snd_pcm_hardware snd_als4000_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
444 SNDRV_PCM_INFO_MMAP_VALID),
445 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
446 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
447 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
448 .rate_min = 4000,
449 .rate_max = 48000,
450 .channels_min = 1,
451 .channels_max = 2,
452 .buffer_bytes_max = 65536,
453 .period_bytes_min = 64,
454 .period_bytes_max = 65536,
455 .periods_min = 1,
456 .periods_max = 1024,
457 .fifo_size = 0
458};
459
460/*****************************************************************/
461
Takashi Iwai17c39d92005-11-17 15:02:01 +0100462static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100464 struct snd_sb *chip = snd_pcm_substream_chip(substream);
465 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 chip->playback_substream = substream;
468 runtime->hw = snd_als4000_playback;
469 return 0;
470}
471
Takashi Iwai17c39d92005-11-17 15:02:01 +0100472static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100474 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 chip->playback_substream = NULL;
477 snd_pcm_lib_free_pages(substream);
478 return 0;
479}
480
Takashi Iwai17c39d92005-11-17 15:02:01 +0100481static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100483 struct snd_sb *chip = snd_pcm_substream_chip(substream);
484 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 chip->capture_substream = substream;
487 runtime->hw = snd_als4000_capture;
488 return 0;
489}
490
Takashi Iwai17c39d92005-11-17 15:02:01 +0100491static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100493 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 chip->capture_substream = NULL;
496 snd_pcm_lib_free_pages(substream);
497 return 0;
498}
499
500/******************************************************************/
501
Takashi Iwai17c39d92005-11-17 15:02:01 +0100502static struct snd_pcm_ops snd_als4000_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 .open = snd_als4000_playback_open,
504 .close = snd_als4000_playback_close,
505 .ioctl = snd_pcm_lib_ioctl,
506 .hw_params = snd_als4000_hw_params,
507 .hw_free = snd_als4000_hw_free,
508 .prepare = snd_als4000_playback_prepare,
509 .trigger = snd_als4000_playback_trigger,
510 .pointer = snd_als4000_playback_pointer
511};
512
Takashi Iwai17c39d92005-11-17 15:02:01 +0100513static struct snd_pcm_ops snd_als4000_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 .open = snd_als4000_capture_open,
515 .close = snd_als4000_capture_close,
516 .ioctl = snd_pcm_lib_ioctl,
517 .hw_params = snd_als4000_hw_params,
518 .hw_free = snd_als4000_hw_free,
519 .prepare = snd_als4000_capture_prepare,
520 .trigger = snd_als4000_capture_trigger,
521 .pointer = snd_als4000_capture_pointer
522};
523
Takashi Iwai17c39d92005-11-17 15:02:01 +0100524static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100526 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int err;
528
529 if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
530 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 pcm->private_data = chip;
532 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
533 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
534 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
535
536 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
537 64*1024, 64*1024);
538
539 chip->pcm = pcm;
540
541 return 0;
542}
543
544/******************************************************************/
545
546static void snd_als4000_set_addr(unsigned long gcr,
547 unsigned int sb,
548 unsigned int mpu,
549 unsigned int opl,
550 unsigned int game)
551{
552 u32 confA = 0;
553 u32 confB = 0;
554
555 if (mpu > 0)
556 confB |= (mpu | 1) << 16;
557 if (sb > 0)
558 confB |= (sb | 1);
559 if (game > 0)
560 confA |= (game | 1) << 16;
561 if (opl > 0)
562 confA |= (opl | 1);
563 snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
564 snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
565}
566
Takashi Iwai70352912005-11-17 16:16:36 +0100567static void snd_als4000_configure(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
569 unsigned tmp;
570 int i;
571
572 /* do some more configuration */
573 spin_lock_irq(&chip->mixer_lock);
574 tmp = snd_sbmixer_read(chip, 0xc0);
575 snd_sbmixer_write(chip, 0xc0, tmp|0x80);
576 /* always select DMA channel 0, since we do not actually use DMA */
577 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
578 snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
579 spin_unlock_irq(&chip->mixer_lock);
580
581 spin_lock_irq(&chip->reg_lock);
582 /* magic number. Enables interrupts(?) */
583 snd_als4000_gcr_write(chip, 0x8c, 0x28000);
584 for(i = 0x91; i <= 0x96; ++i)
585 snd_als4000_gcr_write(chip, i, 0);
586
587 snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
588 spin_unlock_irq(&chip->reg_lock);
589}
590
591#ifdef SUPPORT_JOYSTICK
Takashi Iwai17c39d92005-11-17 15:02:01 +0100592static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 struct gameport *gp;
595 struct resource *r;
596 int io_port;
597
598 if (joystick_port[dev] == 0)
599 return -ENODEV;
600
601 if (joystick_port[dev] == 1) { /* auto-detect */
602 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
603 r = request_region(io_port, 8, "ALS4000 gameport");
604 if (r)
605 break;
606 }
607 } else {
608 io_port = joystick_port[dev];
609 r = request_region(io_port, 8, "ALS4000 gameport");
610 }
611
612 if (!r) {
613 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
614 return -EBUSY;
615 }
616
617 acard->gameport = gp = gameport_allocate_port();
618 if (!gp) {
619 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +0200620 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return -ENOMEM;
622 }
623
624 gameport_set_name(gp, "ALS4000 Gameport");
625 gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
626 gameport_set_dev_parent(gp, &acard->pci->dev);
627 gp->io = io_port;
628 gameport_set_port_data(gp, r);
629
630 /* Enable legacy joystick port */
631 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
632
633 gameport_register_port(acard->gameport);
634
635 return 0;
636}
637
Takashi Iwai17c39d92005-11-17 15:02:01 +0100638static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 if (acard->gameport) {
641 struct resource *r = gameport_get_port_data(acard->gameport);
642
643 gameport_unregister_port(acard->gameport);
644 acard->gameport = NULL;
645
646 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
Takashi Iwaib1d57762005-10-10 11:56:31 +0200647 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649}
650#else
Takashi Iwai17c39d92005-11-17 15:02:01 +0100651static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
652static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653#endif
654
Takashi Iwai17c39d92005-11-17 15:02:01 +0100655static void snd_card_als4000_free( struct snd_card *card )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100657 struct snd_card_als4000 * acard = (struct snd_card_als4000 *)card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* make sure that interrupts are disabled */
660 snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
661 /* free resources */
662 snd_als4000_free_gameport(acard);
663 pci_release_regions(acard->pci);
664 pci_disable_device(acard->pci);
665}
666
667static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
668 const struct pci_device_id *pci_id)
669{
670 static int dev;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100671 struct snd_card *card;
672 struct snd_card_als4000 *acard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 unsigned long gcr;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100674 struct snd_sb *chip;
675 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 unsigned short word;
677 int err;
678
679 if (dev >= SNDRV_CARDS)
680 return -ENODEV;
681 if (!enable[dev]) {
682 dev++;
683 return -ENOENT;
684 }
685
686 /* enable PCI device */
687 if ((err = pci_enable_device(pci)) < 0) {
688 return err;
689 }
690 /* check, if we can restrict PCI DMA transfers to 24 bits */
691 if (pci_set_dma_mask(pci, 0x00ffffff) < 0 ||
692 pci_set_consistent_dma_mask(pci, 0x00ffffff) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200693 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 pci_disable_device(pci);
695 return -ENXIO;
696 }
697
698 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
699 pci_disable_device(pci);
700 return err;
701 }
702 gcr = pci_resource_start(pci, 0);
703
704 pci_read_config_word(pci, PCI_COMMAND, &word);
705 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
706 pci_set_master(pci);
707
708 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai17c39d92005-11-17 15:02:01 +0100709 sizeof( struct snd_card_als4000 ) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (card == NULL) {
711 pci_release_regions(pci);
712 pci_disable_device(pci);
713 return -ENOMEM;
714 }
715
Takashi Iwai17c39d92005-11-17 15:02:01 +0100716 acard = (struct snd_card_als4000 *)card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 acard->pci = pci;
718 acard->gcr = gcr;
719 card->private_free = snd_card_als4000_free;
720
721 /* disable all legacy ISA stuff */
722 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
723
724 if ((err = snd_sbdsp_create(card,
725 gcr + 0x10,
726 pci->irq,
727 snd_als4000_interrupt,
728 -1,
729 -1,
730 SB_HW_ALS4000,
731 &chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100732 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Takashi Iwai70352912005-11-17 16:16:36 +0100734 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 chip->pci = pci;
737 chip->alt_port = gcr;
738 snd_card_set_dev(card, &pci->dev);
739
740 snd_als4000_configure(chip);
741
742 strcpy(card->driver, "ALS4000");
743 strcpy(card->shortname, "Avance Logic ALS4000");
744 sprintf(card->longname, "%s at 0x%lx, irq %i",
745 card->shortname, chip->alt_port, chip->irq);
746
747 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
748 gcr+0x30, 1, pci->irq, 0,
749 &chip->rmidi)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100750 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n", gcr+0x30);
751 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
754 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100755 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 if ((err = snd_sbmixer_new(chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100758 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
762 OPL3_HW_AUTO, 1, &opl3) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100763 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 gcr+0x10, gcr+0x12 );
765 } else {
766 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100767 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 }
770
771 snd_als4000_create_gameport(acard, dev);
772
773 if ((err = snd_card_register(card)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100774 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 pci_set_drvdata(pci, card);
777 dev++;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100778 err = 0;
779 goto out;
780
781out_err:
782 snd_card_free(card);
783
784out:
785 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
787
788static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
789{
790 snd_card_free(pci_get_drvdata(pci));
791 pci_set_drvdata(pci, NULL);
792}
793
Takashi Iwai70352912005-11-17 16:16:36 +0100794#ifdef CONFIG_PM
795static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
796{
797 struct snd_card *card = pci_get_drvdata(pci);
798 struct snd_card_als4000 *acard = card->private_data;
799 struct snd_sb *chip = acard->chip;
800
801 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
802
803 snd_pcm_suspend_all(chip->pcm);
804 snd_sbmixer_suspend(chip);
805
806 pci_set_power_state(pci, PCI_D3hot);
807 pci_disable_device(pci);
808 pci_save_state(pci);
809 return 0;
810}
811
812static int snd_als4000_resume(struct pci_dev *pci)
813{
814 struct snd_card *card = pci_get_drvdata(pci);
815 struct snd_card_als4000 *acard = card->private_data;
816 struct snd_sb *chip = acard->chip;
817
818 pci_restore_state(pci);
819 pci_enable_device(pci);
820 pci_set_power_state(pci, PCI_D0);
821 pci_set_master(pci);
822
823 snd_als4000_configure(chip);
824 snd_sbdsp_reset(chip);
825 snd_sbmixer_resume(chip);
826
827#ifdef SUPPORT_JOYSTICK
828 if (acard->gameport)
829 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
830#endif
831
832 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
833 return 0;
834}
835#endif
836
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838static struct pci_driver driver = {
839 .name = "ALS4000",
840 .id_table = snd_als4000_ids,
841 .probe = snd_card_als4000_probe,
842 .remove = __devexit_p(snd_card_als4000_remove),
Takashi Iwai70352912005-11-17 16:16:36 +0100843#ifdef CONFIG_PM
844 .suspend = snd_als4000_suspend,
845 .resume = snd_als4000_resume,
846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847};
848
849static int __init alsa_card_als4000_init(void)
850{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200851 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854static void __exit alsa_card_als4000_exit(void)
855{
856 pci_unregister_driver(&driver);
857}
858
859module_init(alsa_card_als4000_init)
860module_exit(alsa_card_als4000_exit)