blob: 9e596f750cbd1825c8ebd47ed94aff597daf9604 [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>
Matthias Gehre910638a2006-03-28 01:56:48 -080073#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <sound/core.h>
75#include <sound/pcm.h>
76#include <sound/rawmidi.h>
77#include <sound/mpu401.h>
78#include <sound/opl3.h>
79#include <sound/sb.h>
80#include <sound/initval.h>
81
82MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
83MODULE_DESCRIPTION("Avance Logic ALS4000");
84MODULE_LICENSE("GPL");
85MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
86
87#if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
88#define SUPPORT_JOYSTICK 1
89#endif
90
91static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
92static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
93static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
94#ifdef SUPPORT_JOYSTICK
95static int joystick_port[SNDRV_CARDS];
96#endif
97
98module_param_array(index, int, NULL, 0444);
99MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
100module_param_array(id, charp, NULL, 0444);
101MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
102module_param_array(enable, bool, NULL, 0444);
103MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
104#ifdef SUPPORT_JOYSTICK
105module_param_array(joystick_port, int, NULL, 0444);
106MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
107#endif
108
Takashi Iwai17c39d92005-11-17 15:02:01 +0100109struct snd_card_als4000 {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100110 /* most frequent access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned long gcr;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100112 struct pci_dev *pci;
Takashi Iwai70352912005-11-17 16:16:36 +0100113 struct snd_sb *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#ifdef SUPPORT_JOYSTICK
115 struct gameport *gameport;
116#endif
Takashi Iwai17c39d92005-11-17 15:02:01 +0100117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Takashi Iwaif40b6892006-07-05 16:51:05 +0200119static struct pci_device_id snd_als4000_ids[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
121 { 0, }
122};
123
124MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
125
126static inline void snd_als4000_gcr_write_addr(unsigned long port, u32 reg, u32 val)
127{
128 outb(reg, port+0x0c);
129 outl(val, port+0x08);
130}
131
Takashi Iwai17c39d92005-11-17 15:02:01 +0100132static inline void snd_als4000_gcr_write(struct snd_sb *sb, u32 reg, u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
135}
136
137static inline u32 snd_als4000_gcr_read_addr(unsigned long port, u32 reg)
138{
139 outb(reg, port+0x0c);
140 return inl(port+0x08);
141}
142
Takashi Iwai17c39d92005-11-17 15:02:01 +0100143static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, u32 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 return snd_als4000_gcr_read_addr(sb->alt_port, reg);
146}
147
Takashi Iwai17c39d92005-11-17 15:02:01 +0100148static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 if (!(chip->mode & SB_RATE_LOCK)) {
151 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
152 snd_sbdsp_command(chip, rate>>8);
153 snd_sbdsp_command(chip, rate);
154 }
155}
156
Takashi Iwai17c39d92005-11-17 15:02:01 +0100157static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
158 dma_addr_t addr, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 snd_als4000_gcr_write(chip, 0xa2, addr);
161 snd_als4000_gcr_write(chip, 0xa3, (size-1));
162}
163
Takashi Iwai17c39d92005-11-17 15:02:01 +0100164static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
165 dma_addr_t addr, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 snd_als4000_gcr_write(chip, 0x91, addr);
168 snd_als4000_gcr_write(chip, 0x92, (size-1)|0x180000);
169}
170
171#define ALS4000_FORMAT_SIGNED (1<<0)
172#define ALS4000_FORMAT_16BIT (1<<1)
173#define ALS4000_FORMAT_STEREO (1<<2)
174
Takashi Iwai17c39d92005-11-17 15:02:01 +0100175static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int result;
178
179 result = 0;
180 if (snd_pcm_format_signed(runtime->format))
181 result |= ALS4000_FORMAT_SIGNED;
182 if (snd_pcm_format_physical_width(runtime->format) == 16)
183 result |= ALS4000_FORMAT_16BIT;
184 if (runtime->channels > 1)
185 result |= ALS4000_FORMAT_STEREO;
186 return result;
187}
188
189/* structure for setting up playback */
Andreas Mohrba7301c2005-11-17 11:03:31 +0100190static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 unsigned char dsp_cmd, dma_on, dma_off, format;
192} playback_cmd_vals[]={
193/* ALS4000_FORMAT_U8_MONO */
194{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
195/* ALS4000_FORMAT_S8_MONO */
196{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
197/* ALS4000_FORMAT_U16L_MONO */
198{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
199/* ALS4000_FORMAT_S16L_MONO */
200{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
201/* ALS4000_FORMAT_U8_STEREO */
202{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
203/* ALS4000_FORMAT_S8_STEREO */
204{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
205/* ALS4000_FORMAT_U16L_STEREO */
206{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
207/* ALS4000_FORMAT_S16L_STEREO */
208{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
209};
210#define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
211
212/* structure for setting up capture */
213enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
Andreas Mohrba7301c2005-11-17 11:03:31 +0100214static const unsigned char capture_cmd_vals[]=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
217CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
218CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
219CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
220CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
221CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
222CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
223CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
224};
225#define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
226
Takashi Iwai17c39d92005-11-17 15:02:01 +0100227static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
228 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
231}
232
Takashi Iwai17c39d92005-11-17 15:02:01 +0100233static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 snd_pcm_lib_free_pages(substream);
236 return 0;
237}
238
Takashi Iwai17c39d92005-11-17 15:02:01 +0100239static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100241 struct snd_sb *chip = snd_pcm_substream_chip(substream);
242 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 unsigned long size;
244 unsigned count;
245
246 chip->capture_format = snd_als4000_get_format(runtime);
247
248 size = snd_pcm_lib_buffer_bytes(substream);
249 count = snd_pcm_lib_period_bytes(substream);
250
251 if (chip->capture_format & ALS4000_FORMAT_16BIT)
252 count >>=1;
253 count--;
254
Takashi Iwai17c39d92005-11-17 15:02:01 +0100255 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 snd_als4000_set_rate(chip, runtime->rate);
257 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100258 spin_unlock_irq(&chip->reg_lock);
259 spin_lock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 snd_sbmixer_write(chip, 0xdc, count);
261 snd_sbmixer_write(chip, 0xdd, count>>8);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100262 spin_unlock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
Takashi Iwai17c39d92005-11-17 15:02:01 +0100266static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100268 struct snd_sb *chip = snd_pcm_substream_chip(substream);
269 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 unsigned long size;
271 unsigned count;
272
273 chip->playback_format = snd_als4000_get_format(runtime);
274
275 size = snd_pcm_lib_buffer_bytes(substream);
276 count = snd_pcm_lib_period_bytes(substream);
277
278 if (chip->playback_format & ALS4000_FORMAT_16BIT)
279 count >>=1;
280 count--;
281
282 /* FIXME: from second playback on, there's a lot more clicks and pops
283 * involved here than on first playback. Fiddling with
284 * tons of different settings didn't help (DMA, speaker on/off,
285 * reordering, ...). Something seems to get enabled on playback
286 * that I haven't found out how to disable again, which then causes
287 * the switching pops to reach the speakers the next time here. */
Takashi Iwai17c39d92005-11-17 15:02:01 +0100288 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 snd_als4000_set_rate(chip, runtime->rate);
290 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
291
292 /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
293 /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
294 snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
295 snd_sbdsp_command(chip, playback_cmd(chip).format);
296 snd_sbdsp_command(chip, count);
297 snd_sbdsp_command(chip, count>>8);
298 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100299 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return 0;
302}
303
Takashi Iwai17c39d92005-11-17 15:02:01 +0100304static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100306 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 int result = 0;
308
309 spin_lock(&chip->mixer_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100310 switch (cmd) {
311 case SNDRV_PCM_TRIGGER_START:
312 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 chip->mode |= SB_RATE_LOCK_CAPTURE;
314 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
Takashi Iwai70352912005-11-17 16:16:36 +0100315 break;
316 case SNDRV_PCM_TRIGGER_STOP:
317 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
319 snd_sbmixer_write(chip, 0xde, 0);
Takashi Iwai70352912005-11-17 16:16:36 +0100320 break;
321 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 spin_unlock(&chip->mixer_lock);
326 return result;
327}
328
Takashi Iwai17c39d92005-11-17 15:02:01 +0100329static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100331 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int result = 0;
333
334 spin_lock(&chip->reg_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100335 switch (cmd) {
336 case SNDRV_PCM_TRIGGER_START:
337 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 chip->mode |= SB_RATE_LOCK_PLAYBACK;
339 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
Takashi Iwai70352912005-11-17 16:16:36 +0100340 break;
341 case SNDRV_PCM_TRIGGER_STOP:
342 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
344 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
Takashi Iwai70352912005-11-17 16:16:36 +0100345 break;
346 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100348 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 spin_unlock(&chip->reg_lock);
351 return result;
352}
353
Takashi Iwai17c39d92005-11-17 15:02:01 +0100354static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100356 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned int result;
358
359 spin_lock(&chip->reg_lock);
360 result = snd_als4000_gcr_read(chip, 0xa4) & 0xffff;
361 spin_unlock(&chip->reg_lock);
362 return bytes_to_frames( substream->runtime, result );
363}
364
Takashi Iwai17c39d92005-11-17 15:02:01 +0100365static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100367 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unsigned result;
369
370 spin_lock(&chip->reg_lock);
371 result = snd_als4000_gcr_read(chip, 0xa0) & 0xffff;
372 spin_unlock(&chip->reg_lock);
373 return bytes_to_frames( substream->runtime, result );
374}
375
Andreas Mohrba7301c2005-11-17 11:03:31 +0100376/* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
377 * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
378 * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
379 * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
380 * register (alt_port + 0x0e). Probably something could be optimized here to
381 * query/write one register only...
382 * And even if both registers need to be queried, then there's still the
383 * question of whether it's actually correct to ACK PCI IRQ before reading
384 * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
385 * SB IRQ status.
386 * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
387 * */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
389{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100390 struct snd_sb *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 unsigned gcr_status;
392 unsigned sb_status;
393
394 /* find out which bit of the ALS4000 produced the interrupt */
395 gcr_status = inb(chip->alt_port + 0xe);
396
397 if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
398 snd_pcm_period_elapsed(chip->playback_substream);
399 if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
400 snd_pcm_period_elapsed(chip->capture_substream);
401 if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
Takashi Iwai11ca9af2005-05-27 11:34:34 +0200402 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /* release the gcr */
404 outb(gcr_status, chip->alt_port + 0xe);
405
406 spin_lock(&chip->mixer_lock);
407 sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
408 spin_unlock(&chip->mixer_lock);
409
410 if (sb_status & SB_IRQTYPE_8BIT)
411 snd_sb_ack_8bit(chip);
412 if (sb_status & SB_IRQTYPE_16BIT)
413 snd_sb_ack_16bit(chip);
414 if (sb_status & SB_IRQTYPE_MPUIN)
415 inb(chip->mpu_port);
416 if (sb_status & 0x20)
417 inb(SBP(chip, RESET));
418 return IRQ_HANDLED;
419}
420
421/*****************************************************************/
422
Takashi Iwai17c39d92005-11-17 15:02:01 +0100423static struct snd_pcm_hardware snd_als4000_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
426 SNDRV_PCM_INFO_MMAP_VALID),
427 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
428 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
429 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
430 .rate_min = 4000,
431 .rate_max = 48000,
432 .channels_min = 1,
433 .channels_max = 2,
434 .buffer_bytes_max = 65536,
435 .period_bytes_min = 64,
436 .period_bytes_max = 65536,
437 .periods_min = 1,
438 .periods_max = 1024,
439 .fifo_size = 0
440};
441
Takashi Iwai17c39d92005-11-17 15:02:01 +0100442static struct snd_pcm_hardware snd_als4000_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
445 SNDRV_PCM_INFO_MMAP_VALID),
446 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
447 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
448 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
449 .rate_min = 4000,
450 .rate_max = 48000,
451 .channels_min = 1,
452 .channels_max = 2,
453 .buffer_bytes_max = 65536,
454 .period_bytes_min = 64,
455 .period_bytes_max = 65536,
456 .periods_min = 1,
457 .periods_max = 1024,
458 .fifo_size = 0
459};
460
461/*****************************************************************/
462
Takashi Iwai17c39d92005-11-17 15:02:01 +0100463static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100465 struct snd_sb *chip = snd_pcm_substream_chip(substream);
466 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 chip->playback_substream = substream;
469 runtime->hw = snd_als4000_playback;
470 return 0;
471}
472
Takashi Iwai17c39d92005-11-17 15:02:01 +0100473static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100475 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 chip->playback_substream = NULL;
478 snd_pcm_lib_free_pages(substream);
479 return 0;
480}
481
Takashi Iwai17c39d92005-11-17 15:02:01 +0100482static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100484 struct snd_sb *chip = snd_pcm_substream_chip(substream);
485 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 chip->capture_substream = substream;
488 runtime->hw = snd_als4000_capture;
489 return 0;
490}
491
Takashi Iwai17c39d92005-11-17 15:02:01 +0100492static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100494 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 chip->capture_substream = NULL;
497 snd_pcm_lib_free_pages(substream);
498 return 0;
499}
500
501/******************************************************************/
502
Takashi Iwai17c39d92005-11-17 15:02:01 +0100503static struct snd_pcm_ops snd_als4000_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 .open = snd_als4000_playback_open,
505 .close = snd_als4000_playback_close,
506 .ioctl = snd_pcm_lib_ioctl,
507 .hw_params = snd_als4000_hw_params,
508 .hw_free = snd_als4000_hw_free,
509 .prepare = snd_als4000_playback_prepare,
510 .trigger = snd_als4000_playback_trigger,
511 .pointer = snd_als4000_playback_pointer
512};
513
Takashi Iwai17c39d92005-11-17 15:02:01 +0100514static struct snd_pcm_ops snd_als4000_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 .open = snd_als4000_capture_open,
516 .close = snd_als4000_capture_close,
517 .ioctl = snd_pcm_lib_ioctl,
518 .hw_params = snd_als4000_hw_params,
519 .hw_free = snd_als4000_hw_free,
520 .prepare = snd_als4000_capture_prepare,
521 .trigger = snd_als4000_capture_trigger,
522 .pointer = snd_als4000_capture_pointer
523};
524
Takashi Iwai17c39d92005-11-17 15:02:01 +0100525static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100527 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 int err;
529
530 if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
531 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 pcm->private_data = chip;
533 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
534 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
535 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
536
537 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
538 64*1024, 64*1024);
539
540 chip->pcm = pcm;
541
542 return 0;
543}
544
545/******************************************************************/
546
547static void snd_als4000_set_addr(unsigned long gcr,
548 unsigned int sb,
549 unsigned int mpu,
550 unsigned int opl,
551 unsigned int game)
552{
553 u32 confA = 0;
554 u32 confB = 0;
555
556 if (mpu > 0)
557 confB |= (mpu | 1) << 16;
558 if (sb > 0)
559 confB |= (sb | 1);
560 if (game > 0)
561 confA |= (game | 1) << 16;
562 if (opl > 0)
563 confA |= (opl | 1);
564 snd_als4000_gcr_write_addr(gcr, 0xa8, confA);
565 snd_als4000_gcr_write_addr(gcr, 0xa9, confB);
566}
567
Takashi Iwai70352912005-11-17 16:16:36 +0100568static void snd_als4000_configure(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 unsigned tmp;
571 int i;
572
573 /* do some more configuration */
574 spin_lock_irq(&chip->mixer_lock);
575 tmp = snd_sbmixer_read(chip, 0xc0);
576 snd_sbmixer_write(chip, 0xc0, tmp|0x80);
577 /* always select DMA channel 0, since we do not actually use DMA */
578 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
579 snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
580 spin_unlock_irq(&chip->mixer_lock);
581
582 spin_lock_irq(&chip->reg_lock);
583 /* magic number. Enables interrupts(?) */
584 snd_als4000_gcr_write(chip, 0x8c, 0x28000);
585 for(i = 0x91; i <= 0x96; ++i)
586 snd_als4000_gcr_write(chip, i, 0);
587
588 snd_als4000_gcr_write(chip, 0x99, snd_als4000_gcr_read(chip, 0x99));
589 spin_unlock_irq(&chip->reg_lock);
590}
591
592#ifdef SUPPORT_JOYSTICK
Takashi Iwai17c39d92005-11-17 15:02:01 +0100593static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 struct gameport *gp;
596 struct resource *r;
597 int io_port;
598
599 if (joystick_port[dev] == 0)
600 return -ENODEV;
601
602 if (joystick_port[dev] == 1) { /* auto-detect */
603 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
604 r = request_region(io_port, 8, "ALS4000 gameport");
605 if (r)
606 break;
607 }
608 } else {
609 io_port = joystick_port[dev];
610 r = request_region(io_port, 8, "ALS4000 gameport");
611 }
612
613 if (!r) {
614 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
615 return -EBUSY;
616 }
617
618 acard->gameport = gp = gameport_allocate_port();
619 if (!gp) {
620 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +0200621 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -ENOMEM;
623 }
624
625 gameport_set_name(gp, "ALS4000 Gameport");
626 gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
627 gameport_set_dev_parent(gp, &acard->pci->dev);
628 gp->io = io_port;
629 gameport_set_port_data(gp, r);
630
631 /* Enable legacy joystick port */
632 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
633
634 gameport_register_port(acard->gameport);
635
636 return 0;
637}
638
Takashi Iwai17c39d92005-11-17 15:02:01 +0100639static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 if (acard->gameport) {
642 struct resource *r = gameport_get_port_data(acard->gameport);
643
644 gameport_unregister_port(acard->gameport);
645 acard->gameport = NULL;
646
647 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0); /* disable joystick */
Takashi Iwaib1d57762005-10-10 11:56:31 +0200648 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
650}
651#else
Takashi Iwai17c39d92005-11-17 15:02:01 +0100652static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
653static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#endif
655
Takashi Iwai17c39d92005-11-17 15:02:01 +0100656static void snd_card_als4000_free( struct snd_card *card )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100658 struct snd_card_als4000 * acard = (struct snd_card_als4000 *)card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /* make sure that interrupts are disabled */
661 snd_als4000_gcr_write_addr( acard->gcr, 0x8c, 0);
662 /* free resources */
663 snd_als4000_free_gameport(acard);
664 pci_release_regions(acard->pci);
665 pci_disable_device(acard->pci);
666}
667
668static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
669 const struct pci_device_id *pci_id)
670{
671 static int dev;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100672 struct snd_card *card;
673 struct snd_card_als4000 *acard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned long gcr;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100675 struct snd_sb *chip;
676 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned short word;
678 int err;
679
680 if (dev >= SNDRV_CARDS)
681 return -ENODEV;
682 if (!enable[dev]) {
683 dev++;
684 return -ENOENT;
685 }
686
687 /* enable PCI device */
688 if ((err = pci_enable_device(pci)) < 0) {
689 return err;
690 }
691 /* check, if we can restrict PCI DMA transfers to 24 bits */
Matthias Gehre910638a2006-03-28 01:56:48 -0800692 if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
693 pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200694 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 pci_disable_device(pci);
696 return -ENXIO;
697 }
698
699 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
700 pci_disable_device(pci);
701 return err;
702 }
703 gcr = pci_resource_start(pci, 0);
704
705 pci_read_config_word(pci, PCI_COMMAND, &word);
706 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
707 pci_set_master(pci);
708
709 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai17c39d92005-11-17 15:02:01 +0100710 sizeof( struct snd_card_als4000 ) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 if (card == NULL) {
712 pci_release_regions(pci);
713 pci_disable_device(pci);
714 return -ENOMEM;
715 }
716
Takashi Iwai17c39d92005-11-17 15:02:01 +0100717 acard = (struct snd_card_als4000 *)card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 acard->pci = pci;
719 acard->gcr = gcr;
720 card->private_free = snd_card_als4000_free;
721
722 /* disable all legacy ISA stuff */
723 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 0);
724
725 if ((err = snd_sbdsp_create(card,
726 gcr + 0x10,
727 pci->irq,
728 snd_als4000_interrupt,
729 -1,
730 -1,
731 SB_HW_ALS4000,
732 &chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100733 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Takashi Iwai70352912005-11-17 16:16:36 +0100735 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 chip->pci = pci;
738 chip->alt_port = gcr;
739 snd_card_set_dev(card, &pci->dev);
740
741 snd_als4000_configure(chip);
742
743 strcpy(card->driver, "ALS4000");
744 strcpy(card->shortname, "Avance Logic ALS4000");
745 sprintf(card->longname, "%s at 0x%lx, irq %i",
746 card->shortname, chip->alt_port, chip->irq);
747
748 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
Takashi Iwai302e4c22006-05-23 13:24:30 +0200749 gcr+0x30, MPU401_INFO_INTEGRATED,
750 pci->irq, 0, &chip->rmidi)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100751 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n", gcr+0x30);
752 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
754
755 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100756 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 if ((err = snd_sbmixer_new(chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100759 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 if (snd_opl3_create(card, gcr+0x10, gcr+0x12,
763 OPL3_HW_AUTO, 1, &opl3) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100764 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 gcr+0x10, gcr+0x12 );
766 } else {
767 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100768 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
770 }
771
772 snd_als4000_create_gameport(acard, dev);
773
774 if ((err = snd_card_register(card)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100775 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 pci_set_drvdata(pci, card);
778 dev++;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100779 err = 0;
780 goto out;
781
782out_err:
783 snd_card_free(card);
784
785out:
786 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
788
789static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
790{
791 snd_card_free(pci_get_drvdata(pci));
792 pci_set_drvdata(pci, NULL);
793}
794
Takashi Iwai70352912005-11-17 16:16:36 +0100795#ifdef CONFIG_PM
796static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
797{
798 struct snd_card *card = pci_get_drvdata(pci);
799 struct snd_card_als4000 *acard = card->private_data;
800 struct snd_sb *chip = acard->chip;
801
802 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
803
804 snd_pcm_suspend_all(chip->pcm);
805 snd_sbmixer_suspend(chip);
806
807 pci_set_power_state(pci, PCI_D3hot);
808 pci_disable_device(pci);
809 pci_save_state(pci);
810 return 0;
811}
812
813static int snd_als4000_resume(struct pci_dev *pci)
814{
815 struct snd_card *card = pci_get_drvdata(pci);
816 struct snd_card_als4000 *acard = card->private_data;
817 struct snd_sb *chip = acard->chip;
818
819 pci_restore_state(pci);
820 pci_enable_device(pci);
821 pci_set_power_state(pci, PCI_D0);
822 pci_set_master(pci);
823
824 snd_als4000_configure(chip);
825 snd_sbdsp_reset(chip);
826 snd_sbmixer_resume(chip);
827
828#ifdef SUPPORT_JOYSTICK
829 if (acard->gameport)
830 snd_als4000_set_addr(acard->gcr, 0, 0, 0, 1);
831#endif
832
833 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
834 return 0;
835}
836#endif
837
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static struct pci_driver driver = {
840 .name = "ALS4000",
841 .id_table = snd_als4000_ids,
842 .probe = snd_card_als4000_probe,
843 .remove = __devexit_p(snd_card_als4000_remove),
Takashi Iwai70352912005-11-17 16:16:36 +0100844#ifdef CONFIG_PM
845 .suspend = snd_als4000_suspend,
846 .resume = snd_als4000_resume,
847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848};
849
850static int __init alsa_card_als4000_init(void)
851{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200852 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
855static void __exit alsa_card_als4000_exit(void)
856{
857 pci_unregister_driver(&driver);
858}
859
860module_init(alsa_card_als4000_init)
861module_exit(alsa_card_als4000_exit)