blob: 92d8c47cd3b2fe6fa7187eb56ab448d835ffc70e [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>,
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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?
Andreas Mohrc0874442008-08-20 10:04:56 +020063 * - by default, don't enable legacy game and use PCI game I/O
Andreas Mohrba7301c2005-11-17 11:03:31 +010064 * - power management? (card can do voice wakeup according to datasheet!!)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#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 */
Andreas Mohrc0874442008-08-20 10:04:56 +0200111 unsigned long iobase;
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
Andreas Mohrc0874442008-08-20 10:04:56 +0200126enum als4k_iobase_t {
127 /* IOx: B == Byte, W = Word, D = DWord */
128 ALS4K_IOD_00_AC97_ACCESS = 0x00,
129 ALS4K_IOW_04_AC97_READ = 0x04,
130 ALS4K_IOB_06_AC97_STATUS = 0x06,
131 ALS4K_IOB_07_IRQSTATUS = 0x07,
132 ALS4K_IOD_08_GCR_DATA = 0x08,
133 ALS4K_IOB_0C_GCR_INDEX = 0x0c,
134 ALS4K_IOB_0E_SB_MPU_IRQ = 0x0e,
135 ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
136 ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
137 ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
138 ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
139 ALS4K_IOB_14_MIXER_INDEX = 0x14,
140 ALS4K_IOB_15_MIXER_DATA = 0x15,
141 ALS4K_IOB_16_ESP_RST_PORT = 0x16,
142 ALS4K_IOB_16_CR1E_ACK_PORT = 0x16, /* 2nd function */
143 ALS4K_IOB_18_OPL_ADDR0 = 0x18,
144 ALS4K_IOB_19_OPL_ADDR1 = 0x19,
145 ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
146 ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
147 ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
148 ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
149 ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
150 ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
151 ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
152 ALS4K_IOB_30_MIDI_DATA = 0x30,
153 ALS4K_IOB_31_MIDI_STATUS = 0x31,
154 ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
155};
156
157enum als4k_gcr_t {
158 /* all registers 32bit wide */
159 ALS4K_GCR_8C_MISC_CTRL = 0x8c,
160 ALS4K_GCR_90_TEST_MODE_REG = 0x90,
161 ALS4K_GCR_91_DMA0_ADDR = 0x91,
162 ALS4K_GCR_92_DMA0_MODE_COUNT = 0x92,
163 ALS4K_GCR_93_DMA1_ADDR = 0x93,
164 ALS4K_GCR_94_DMA1_MODE_COUNT = 0x94,
165 ALS4K_GCR_95_DMA3_ADDR = 0x95,
166 ALS4K_GCR_96_DMA3_MODE_COUNT = 0x96,
167 ALS4K_GCR_99_DMA_EMULATION_CTRL = 0x99,
168 ALS4K_GCR_A0_FIFO1_CURRENT_ADDR = 0xa0,
169 ALS4K_GCR_A1_FIFO1_STATUS_BYTECOUNT = 0xa1,
170 ALS4K_GCR_A2_FIFO2_PCIADDR = 0xa2,
171 ALS4K_GCR_A3_FIFO2_COUNT = 0xa3,
172 ALS4K_GCR_A4_FIFO2_CURRENT_ADDR = 0xa4,
173 ALS4K_GCR_A5_FIFO1_STATUS_BYTECOUNT = 0xa5,
174 ALS4K_GCR_A6_PM_CTRL = 0xa6,
175 ALS4K_GCR_A7_PCI_ACCESS_STORAGE = 0xa7,
176 ALS4K_GCR_A8_LEGACY_CFG1 = 0xa8,
177 ALS4K_GCR_A9_LEGACY_CFG2 = 0xa9,
178 ALS4K_GCR_FF_DUMMY_SCRATCH = 0xff,
179};
180
181enum als4k_gcr_8c_t {
182 ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
183 ALS4K_GCR_8C_CHIP_REV_MASK = 0xf0000
184};
185
186static inline void snd_als4000_gcr_write_addr(unsigned long iobase,
187 enum als4k_gcr_t reg,
188 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Andreas Mohrc0874442008-08-20 10:04:56 +0200190 outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
191 outl(val, iobase + ALS4K_IOD_08_GCR_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Andreas Mohrc0874442008-08-20 10:04:56 +0200194static inline void snd_als4000_gcr_write(struct snd_sb *sb,
195 enum als4k_gcr_t reg,
196 u32 val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
198 snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
199}
200
Andreas Mohrc0874442008-08-20 10:04:56 +0200201static inline u32 snd_als4000_gcr_read_addr(unsigned long iobase,
202 enum als4k_gcr_t reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Andreas Mohrc0874442008-08-20 10:04:56 +0200204 outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
205 return inl(iobase + ALS4K_IOD_08_GCR_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Andreas Mohrc0874442008-08-20 10:04:56 +0200208static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 return snd_als4000_gcr_read_addr(sb->alt_port, reg);
211}
212
Takashi Iwai17c39d92005-11-17 15:02:01 +0100213static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 if (!(chip->mode & SB_RATE_LOCK)) {
216 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
217 snd_sbdsp_command(chip, rate>>8);
218 snd_sbdsp_command(chip, rate);
219 }
220}
221
Takashi Iwai17c39d92005-11-17 15:02:01 +0100222static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
223 dma_addr_t addr, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Andreas Mohrc0874442008-08-20 10:04:56 +0200225 snd_als4000_gcr_write(chip, ALS4K_GCR_A2_FIFO2_PCIADDR, addr);
226 snd_als4000_gcr_write(chip, ALS4K_GCR_A3_FIFO2_COUNT, (size-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Takashi Iwai17c39d92005-11-17 15:02:01 +0100229static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
Andreas Mohrc0874442008-08-20 10:04:56 +0200230 dma_addr_t addr,
231 unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Andreas Mohrc0874442008-08-20 10:04:56 +0200233 snd_als4000_gcr_write(chip, ALS4K_GCR_91_DMA0_ADDR, addr);
234 snd_als4000_gcr_write(chip, ALS4K_GCR_92_DMA0_MODE_COUNT,
235 (size-1)|0x180000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
238#define ALS4000_FORMAT_SIGNED (1<<0)
239#define ALS4000_FORMAT_16BIT (1<<1)
240#define ALS4000_FORMAT_STEREO (1<<2)
241
Takashi Iwai17c39d92005-11-17 15:02:01 +0100242static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int result;
245
246 result = 0;
247 if (snd_pcm_format_signed(runtime->format))
248 result |= ALS4000_FORMAT_SIGNED;
249 if (snd_pcm_format_physical_width(runtime->format) == 16)
250 result |= ALS4000_FORMAT_16BIT;
251 if (runtime->channels > 1)
252 result |= ALS4000_FORMAT_STEREO;
253 return result;
254}
255
256/* structure for setting up playback */
Andreas Mohrba7301c2005-11-17 11:03:31 +0100257static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 unsigned char dsp_cmd, dma_on, dma_off, format;
259} playback_cmd_vals[]={
260/* ALS4000_FORMAT_U8_MONO */
261{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
262/* ALS4000_FORMAT_S8_MONO */
263{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
264/* ALS4000_FORMAT_U16L_MONO */
265{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
266/* ALS4000_FORMAT_S16L_MONO */
267{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
268/* ALS4000_FORMAT_U8_STEREO */
269{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
270/* ALS4000_FORMAT_S8_STEREO */
271{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
272/* ALS4000_FORMAT_U16L_STEREO */
273{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
274/* ALS4000_FORMAT_S16L_STEREO */
275{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
276};
277#define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
278
279/* structure for setting up capture */
280enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
Andreas Mohrba7301c2005-11-17 11:03:31 +0100281static const unsigned char capture_cmd_vals[]=
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
284CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
285CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
286CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
287CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
288CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
289CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
290CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
291};
292#define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
293
Takashi Iwai17c39d92005-11-17 15:02:01 +0100294static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
295 struct snd_pcm_hw_params *hw_params)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
298}
299
Takashi Iwai17c39d92005-11-17 15:02:01 +0100300static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 snd_pcm_lib_free_pages(substream);
303 return 0;
304}
305
Takashi Iwai17c39d92005-11-17 15:02:01 +0100306static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100308 struct snd_sb *chip = snd_pcm_substream_chip(substream);
309 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned long size;
311 unsigned count;
312
313 chip->capture_format = snd_als4000_get_format(runtime);
314
315 size = snd_pcm_lib_buffer_bytes(substream);
316 count = snd_pcm_lib_period_bytes(substream);
317
318 if (chip->capture_format & ALS4000_FORMAT_16BIT)
319 count >>=1;
320 count--;
321
Takashi Iwai17c39d92005-11-17 15:02:01 +0100322 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 snd_als4000_set_rate(chip, runtime->rate);
324 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100325 spin_unlock_irq(&chip->reg_lock);
326 spin_lock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 snd_sbmixer_write(chip, 0xdc, count);
328 snd_sbmixer_write(chip, 0xdd, count>>8);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100329 spin_unlock_irq(&chip->mixer_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return 0;
331}
332
Takashi Iwai17c39d92005-11-17 15:02:01 +0100333static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100335 struct snd_sb *chip = snd_pcm_substream_chip(substream);
336 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 unsigned long size;
338 unsigned count;
339
340 chip->playback_format = snd_als4000_get_format(runtime);
341
342 size = snd_pcm_lib_buffer_bytes(substream);
343 count = snd_pcm_lib_period_bytes(substream);
344
345 if (chip->playback_format & ALS4000_FORMAT_16BIT)
346 count >>=1;
347 count--;
348
349 /* FIXME: from second playback on, there's a lot more clicks and pops
350 * involved here than on first playback. Fiddling with
351 * tons of different settings didn't help (DMA, speaker on/off,
352 * reordering, ...). Something seems to get enabled on playback
353 * that I haven't found out how to disable again, which then causes
354 * the switching pops to reach the speakers the next time here. */
Takashi Iwai17c39d92005-11-17 15:02:01 +0100355 spin_lock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 snd_als4000_set_rate(chip, runtime->rate);
357 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
358
359 /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
360 /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
361 snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
362 snd_sbdsp_command(chip, playback_cmd(chip).format);
363 snd_sbdsp_command(chip, count);
364 snd_sbdsp_command(chip, count>>8);
365 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
Takashi Iwai17c39d92005-11-17 15:02:01 +0100366 spin_unlock_irq(&chip->reg_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 return 0;
369}
370
Takashi Iwai17c39d92005-11-17 15:02:01 +0100371static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100373 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 int result = 0;
375
Andreas Mohrc0874442008-08-20 10:04:56 +0200376 /* FIXME race condition in here!!!
377 chip->mode non-atomic update gets consistently protected
378 by reg_lock always, _except_ for this place!!
379 Probably need to take reg_lock as outer (or inner??) lock, too.
380 (or serialize both lock operations? probably not, though... - racy?)
381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spin_lock(&chip->mixer_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100383 switch (cmd) {
384 case SNDRV_PCM_TRIGGER_START:
385 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 chip->mode |= SB_RATE_LOCK_CAPTURE;
387 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
Takashi Iwai70352912005-11-17 16:16:36 +0100388 break;
389 case SNDRV_PCM_TRIGGER_STOP:
390 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
392 snd_sbmixer_write(chip, 0xde, 0);
Takashi Iwai70352912005-11-17 16:16:36 +0100393 break;
394 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 spin_unlock(&chip->mixer_lock);
399 return result;
400}
401
Takashi Iwai17c39d92005-11-17 15:02:01 +0100402static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100404 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int result = 0;
406
407 spin_lock(&chip->reg_lock);
Takashi Iwai70352912005-11-17 16:16:36 +0100408 switch (cmd) {
409 case SNDRV_PCM_TRIGGER_START:
410 case SNDRV_PCM_TRIGGER_RESUME:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 chip->mode |= SB_RATE_LOCK_PLAYBACK;
412 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
Takashi Iwai70352912005-11-17 16:16:36 +0100413 break;
414 case SNDRV_PCM_TRIGGER_STOP:
415 case SNDRV_PCM_TRIGGER_SUSPEND:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
417 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
Takashi Iwai70352912005-11-17 16:16:36 +0100418 break;
419 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 result = -EINVAL;
Takashi Iwai70352912005-11-17 16:16:36 +0100421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423 spin_unlock(&chip->reg_lock);
424 return result;
425}
426
Takashi Iwai17c39d92005-11-17 15:02:01 +0100427static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100429 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 unsigned int result;
431
432 spin_lock(&chip->reg_lock);
Andreas Mohrc0874442008-08-20 10:04:56 +0200433 result = snd_als4000_gcr_read(chip, ALS4K_GCR_A4_FIFO2_CURRENT_ADDR);
434 result &= 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 spin_unlock(&chip->reg_lock);
436 return bytes_to_frames( substream->runtime, result );
437}
438
Takashi Iwai17c39d92005-11-17 15:02:01 +0100439static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100441 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unsigned result;
443
444 spin_lock(&chip->reg_lock);
Andreas Mohrc0874442008-08-20 10:04:56 +0200445 result = snd_als4000_gcr_read(chip, ALS4K_GCR_A0_FIFO1_CURRENT_ADDR);
446 result &= 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 spin_unlock(&chip->reg_lock);
448 return bytes_to_frames( substream->runtime, result );
449}
450
Andreas Mohrba7301c2005-11-17 11:03:31 +0100451/* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
452 * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
453 * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
454 * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
Andreas Mohrc0874442008-08-20 10:04:56 +0200455 * register (alt_port + ALS4K_IOB_0E_SB_MPU_IRQ). Probably something
456 * could be optimized here to query/write one register only...
Andreas Mohrba7301c2005-11-17 11:03:31 +0100457 * And even if both registers need to be queried, then there's still the
458 * question of whether it's actually correct to ACK PCI IRQ before reading
459 * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
460 * SB IRQ status.
Andreas Mohrc0874442008-08-20 10:04:56 +0200461 * (hmm, page 38 mentions it the other way around!)
Andreas Mohrba7301c2005-11-17 11:03:31 +0100462 * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
463 * */
David Howells7d12e782006-10-05 14:55:46 +0100464static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100466 struct snd_sb *chip = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 unsigned gcr_status;
468 unsigned sb_status;
469
470 /* find out which bit of the ALS4000 produced the interrupt */
Andreas Mohrc0874442008-08-20 10:04:56 +0200471 gcr_status = inb(chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 if ((gcr_status & 0x80) && (chip->playback_substream)) /* playback */
474 snd_pcm_period_elapsed(chip->playback_substream);
475 if ((gcr_status & 0x40) && (chip->capture_substream)) /* capturing */
476 snd_pcm_period_elapsed(chip->capture_substream);
477 if ((gcr_status & 0x10) && (chip->rmidi)) /* MPU401 interrupt */
David Howells7d12e782006-10-05 14:55:46 +0100478 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* release the gcr */
Andreas Mohrc0874442008-08-20 10:04:56 +0200480 outb(gcr_status, chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 spin_lock(&chip->mixer_lock);
483 sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
484 spin_unlock(&chip->mixer_lock);
485
486 if (sb_status & SB_IRQTYPE_8BIT)
487 snd_sb_ack_8bit(chip);
488 if (sb_status & SB_IRQTYPE_16BIT)
489 snd_sb_ack_16bit(chip);
490 if (sb_status & SB_IRQTYPE_MPUIN)
491 inb(chip->mpu_port);
492 if (sb_status & 0x20)
493 inb(SBP(chip, RESET));
494 return IRQ_HANDLED;
495}
496
497/*****************************************************************/
498
Takashi Iwai17c39d92005-11-17 15:02:01 +0100499static struct snd_pcm_hardware snd_als4000_playback =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
501 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
502 SNDRV_PCM_INFO_MMAP_VALID),
503 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
504 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
505 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
506 .rate_min = 4000,
507 .rate_max = 48000,
508 .channels_min = 1,
509 .channels_max = 2,
510 .buffer_bytes_max = 65536,
511 .period_bytes_min = 64,
512 .period_bytes_max = 65536,
513 .periods_min = 1,
514 .periods_max = 1024,
515 .fifo_size = 0
516};
517
Takashi Iwai17c39d92005-11-17 15:02:01 +0100518static struct snd_pcm_hardware snd_als4000_capture =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
521 SNDRV_PCM_INFO_MMAP_VALID),
522 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
523 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
524 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
525 .rate_min = 4000,
526 .rate_max = 48000,
527 .channels_min = 1,
528 .channels_max = 2,
529 .buffer_bytes_max = 65536,
530 .period_bytes_min = 64,
531 .period_bytes_max = 65536,
532 .periods_min = 1,
533 .periods_max = 1024,
534 .fifo_size = 0
535};
536
537/*****************************************************************/
538
Takashi Iwai17c39d92005-11-17 15:02:01 +0100539static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100541 struct snd_sb *chip = snd_pcm_substream_chip(substream);
542 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 chip->playback_substream = substream;
545 runtime->hw = snd_als4000_playback;
546 return 0;
547}
548
Takashi Iwai17c39d92005-11-17 15:02:01 +0100549static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100551 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 chip->playback_substream = NULL;
554 snd_pcm_lib_free_pages(substream);
555 return 0;
556}
557
Takashi Iwai17c39d92005-11-17 15:02:01 +0100558static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100560 struct snd_sb *chip = snd_pcm_substream_chip(substream);
561 struct snd_pcm_runtime *runtime = substream->runtime;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 chip->capture_substream = substream;
564 runtime->hw = snd_als4000_capture;
565 return 0;
566}
567
Takashi Iwai17c39d92005-11-17 15:02:01 +0100568static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100570 struct snd_sb *chip = snd_pcm_substream_chip(substream);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 chip->capture_substream = NULL;
573 snd_pcm_lib_free_pages(substream);
574 return 0;
575}
576
577/******************************************************************/
578
Takashi Iwai17c39d92005-11-17 15:02:01 +0100579static struct snd_pcm_ops snd_als4000_playback_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 .open = snd_als4000_playback_open,
581 .close = snd_als4000_playback_close,
582 .ioctl = snd_pcm_lib_ioctl,
583 .hw_params = snd_als4000_hw_params,
584 .hw_free = snd_als4000_hw_free,
585 .prepare = snd_als4000_playback_prepare,
586 .trigger = snd_als4000_playback_trigger,
587 .pointer = snd_als4000_playback_pointer
588};
589
Takashi Iwai17c39d92005-11-17 15:02:01 +0100590static struct snd_pcm_ops snd_als4000_capture_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .open = snd_als4000_capture_open,
592 .close = snd_als4000_capture_close,
593 .ioctl = snd_pcm_lib_ioctl,
594 .hw_params = snd_als4000_hw_params,
595 .hw_free = snd_als4000_hw_free,
596 .prepare = snd_als4000_capture_prepare,
597 .trigger = snd_als4000_capture_trigger,
598 .pointer = snd_als4000_capture_pointer
599};
600
Takashi Iwai17c39d92005-11-17 15:02:01 +0100601static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Takashi Iwai17c39d92005-11-17 15:02:01 +0100603 struct snd_pcm *pcm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 int err;
605
606 if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 pcm->private_data = chip;
609 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
610 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
611 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
612
613 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
614 64*1024, 64*1024);
615
616 chip->pcm = pcm;
617
618 return 0;
619}
620
621/******************************************************************/
622
Andreas Mohrc0874442008-08-20 10:04:56 +0200623static void snd_als4000_set_addr(unsigned long iobase,
624 unsigned int sb_io,
625 unsigned int mpu_io,
626 unsigned int opl_io,
627 unsigned int game_io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Andreas Mohrc0874442008-08-20 10:04:56 +0200629 u32 cfg1 = 0;
630 u32 cfg2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Andreas Mohrc0874442008-08-20 10:04:56 +0200632 if (mpu_io > 0)
633 cfg2 |= (mpu_io | 1) << 16;
634 if (sb_io > 0)
635 cfg2 |= (sb_io | 1);
636 if (game_io > 0)
637 cfg1 |= (game_io | 1) << 16;
638 if (opl_io > 0)
639 cfg1 |= (opl_io | 1);
640 snd_als4000_gcr_write_addr(iobase, ALS4K_GCR_A8_LEGACY_CFG1, cfg1);
641 snd_als4000_gcr_write_addr(iobase, ALS4K_GCR_A9_LEGACY_CFG2, cfg2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Takashi Iwai70352912005-11-17 16:16:36 +0100644static void snd_als4000_configure(struct snd_sb *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 unsigned tmp;
647 int i;
648
649 /* do some more configuration */
650 spin_lock_irq(&chip->mixer_lock);
651 tmp = snd_sbmixer_read(chip, 0xc0);
652 snd_sbmixer_write(chip, 0xc0, tmp|0x80);
653 /* always select DMA channel 0, since we do not actually use DMA */
654 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
655 snd_sbmixer_write(chip, 0xc0, tmp&0x7f);
656 spin_unlock_irq(&chip->mixer_lock);
657
658 spin_lock_irq(&chip->reg_lock);
Andreas Mohrc0874442008-08-20 10:04:56 +0200659 /* enable interrupts */
660 snd_als4000_gcr_write(chip, ALS4K_GCR_8C_MISC_CTRL,
661 ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE);
662
663 for (i = ALS4K_GCR_91_DMA0_ADDR; i <= ALS4K_GCR_96_DMA3_MODE_COUNT; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 snd_als4000_gcr_write(chip, i, 0);
665
Andreas Mohrc0874442008-08-20 10:04:56 +0200666 snd_als4000_gcr_write(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL,
667 snd_als4000_gcr_read(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 spin_unlock_irq(&chip->reg_lock);
669}
670
671#ifdef SUPPORT_JOYSTICK
Takashi Iwai17c39d92005-11-17 15:02:01 +0100672static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 struct gameport *gp;
675 struct resource *r;
676 int io_port;
677
678 if (joystick_port[dev] == 0)
679 return -ENODEV;
680
681 if (joystick_port[dev] == 1) { /* auto-detect */
682 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
683 r = request_region(io_port, 8, "ALS4000 gameport");
684 if (r)
685 break;
686 }
687 } else {
688 io_port = joystick_port[dev];
689 r = request_region(io_port, 8, "ALS4000 gameport");
690 }
691
692 if (!r) {
693 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
694 return -EBUSY;
695 }
696
697 acard->gameport = gp = gameport_allocate_port();
698 if (!gp) {
699 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
Takashi Iwaib1d57762005-10-10 11:56:31 +0200700 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -ENOMEM;
702 }
703
704 gameport_set_name(gp, "ALS4000 Gameport");
705 gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
706 gameport_set_dev_parent(gp, &acard->pci->dev);
707 gp->io = io_port;
708 gameport_set_port_data(gp, r);
709
710 /* Enable legacy joystick port */
Andreas Mohrc0874442008-08-20 10:04:56 +0200711 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 gameport_register_port(acard->gameport);
714
715 return 0;
716}
717
Takashi Iwai17c39d92005-11-17 15:02:01 +0100718static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 if (acard->gameport) {
721 struct resource *r = gameport_get_port_data(acard->gameport);
722
723 gameport_unregister_port(acard->gameport);
724 acard->gameport = NULL;
725
Andreas Mohrc0874442008-08-20 10:04:56 +0200726 /* disable joystick */
727 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
728
Takashi Iwaib1d57762005-10-10 11:56:31 +0200729 release_and_free_resource(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731}
732#else
Takashi Iwai17c39d92005-11-17 15:02:01 +0100733static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
734static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735#endif
736
Takashi Iwai17c39d92005-11-17 15:02:01 +0100737static void snd_card_als4000_free( struct snd_card *card )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Andreas Mohrc0874442008-08-20 10:04:56 +0200739 struct snd_card_als4000 *acard = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* make sure that interrupts are disabled */
Andreas Mohrc0874442008-08-20 10:04:56 +0200742 snd_als4000_gcr_write_addr(acard->iobase, ALS4K_GCR_8C_MISC_CTRL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* free resources */
744 snd_als4000_free_gameport(acard);
745 pci_release_regions(acard->pci);
746 pci_disable_device(acard->pci);
747}
748
749static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
750 const struct pci_device_id *pci_id)
751{
752 static int dev;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100753 struct snd_card *card;
754 struct snd_card_als4000 *acard;
Andreas Mohrc0874442008-08-20 10:04:56 +0200755 unsigned long iobase;
Takashi Iwai17c39d92005-11-17 15:02:01 +0100756 struct snd_sb *chip;
757 struct snd_opl3 *opl3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 unsigned short word;
759 int err;
760
761 if (dev >= SNDRV_CARDS)
762 return -ENODEV;
763 if (!enable[dev]) {
764 dev++;
765 return -ENOENT;
766 }
767
768 /* enable PCI device */
769 if ((err = pci_enable_device(pci)) < 0) {
770 return err;
771 }
772 /* check, if we can restrict PCI DMA transfers to 24 bits */
Matthias Gehre910638a2006-03-28 01:56:48 -0800773 if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
774 pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200775 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 pci_disable_device(pci);
777 return -ENXIO;
778 }
779
780 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
781 pci_disable_device(pci);
782 return err;
783 }
Andreas Mohrc0874442008-08-20 10:04:56 +0200784 iobase = pci_resource_start(pci, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 pci_read_config_word(pci, PCI_COMMAND, &word);
787 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
788 pci_set_master(pci);
789
790 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
Takashi Iwai17c39d92005-11-17 15:02:01 +0100791 sizeof( struct snd_card_als4000 ) );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (card == NULL) {
793 pci_release_regions(pci);
794 pci_disable_device(pci);
795 return -ENOMEM;
796 }
797
Andreas Mohrc0874442008-08-20 10:04:56 +0200798 acard = card->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 acard->pci = pci;
Andreas Mohrc0874442008-08-20 10:04:56 +0200800 acard->iobase = iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 card->private_free = snd_card_als4000_free;
802
803 /* disable all legacy ISA stuff */
Andreas Mohrc0874442008-08-20 10:04:56 +0200804 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if ((err = snd_sbdsp_create(card,
Andreas Mohrc0874442008-08-20 10:04:56 +0200807 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 pci->irq,
809 snd_als4000_interrupt,
810 -1,
811 -1,
812 SB_HW_ALS4000,
813 &chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100814 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Takashi Iwai70352912005-11-17 16:16:36 +0100816 acard->chip = chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 chip->pci = pci;
Andreas Mohrc0874442008-08-20 10:04:56 +0200819 chip->alt_port = iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 snd_card_set_dev(card, &pci->dev);
821
822 snd_als4000_configure(chip);
823
824 strcpy(card->driver, "ALS4000");
825 strcpy(card->shortname, "Avance Logic ALS4000");
826 sprintf(card->longname, "%s at 0x%lx, irq %i",
827 card->shortname, chip->alt_port, chip->irq);
828
829 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
Andreas Mohrc0874442008-08-20 10:04:56 +0200830 iobase + ALS4K_IOB_30_MIDI_DATA,
831 MPU401_INFO_INTEGRATED,
Takashi Iwai302e4c22006-05-23 13:24:30 +0200832 pci->irq, 0, &chip->rmidi)) < 0) {
Andreas Mohrc0874442008-08-20 10:04:56 +0200833 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n",
834 iobase + ALS4K_IOB_30_MIDI_DATA);
Andreas Mohrba7301c2005-11-17 11:03:31 +0100835 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
Andreas Mohrc0874442008-08-20 10:04:56 +0200837 /* FIXME: ALS4000 has interesting MPU401 configuration features
838 * at CR 0x1A (pass-thru / UART switching, fast MIDI clock, etc.),
839 * however there doesn't seem to be an ALSA API for this... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100842 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 if ((err = snd_sbmixer_new(chip)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100845 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
Andreas Mohrc0874442008-08-20 10:04:56 +0200848 if (snd_opl3_create(card,
849 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
850 iobase + ALS4K_IOB_12_ADLIB_ADDR2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 OPL3_HW_AUTO, 1, &opl3) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100852 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
Andreas Mohrc0874442008-08-20 10:04:56 +0200853 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
854 iobase + ALS4K_IOB_12_ADLIB_ADDR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 } else {
856 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100857 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859 }
860
861 snd_als4000_create_gameport(acard, dev);
862
863 if ((err = snd_card_register(card)) < 0) {
Andreas Mohrba7301c2005-11-17 11:03:31 +0100864 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 pci_set_drvdata(pci, card);
867 dev++;
Andreas Mohrba7301c2005-11-17 11:03:31 +0100868 err = 0;
869 goto out;
870
871out_err:
872 snd_card_free(card);
873
874out:
875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
879{
880 snd_card_free(pci_get_drvdata(pci));
881 pci_set_drvdata(pci, NULL);
882}
883
Takashi Iwai70352912005-11-17 16:16:36 +0100884#ifdef CONFIG_PM
885static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
886{
887 struct snd_card *card = pci_get_drvdata(pci);
888 struct snd_card_als4000 *acard = card->private_data;
889 struct snd_sb *chip = acard->chip;
890
891 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
892
893 snd_pcm_suspend_all(chip->pcm);
894 snd_sbmixer_suspend(chip);
895
Takashi Iwai70352912005-11-17 16:16:36 +0100896 pci_disable_device(pci);
897 pci_save_state(pci);
Takashi Iwai30b35392006-10-11 18:52:53 +0200898 pci_set_power_state(pci, pci_choose_state(pci, state));
Takashi Iwai70352912005-11-17 16:16:36 +0100899 return 0;
900}
901
902static int snd_als4000_resume(struct pci_dev *pci)
903{
904 struct snd_card *card = pci_get_drvdata(pci);
905 struct snd_card_als4000 *acard = card->private_data;
906 struct snd_sb *chip = acard->chip;
907
Takashi Iwai70352912005-11-17 16:16:36 +0100908 pci_set_power_state(pci, PCI_D0);
Takashi Iwai30b35392006-10-11 18:52:53 +0200909 pci_restore_state(pci);
910 if (pci_enable_device(pci) < 0) {
911 printk(KERN_ERR "als4000: pci_enable_device failed, "
912 "disabling device\n");
913 snd_card_disconnect(card);
914 return -EIO;
915 }
Takashi Iwai70352912005-11-17 16:16:36 +0100916 pci_set_master(pci);
917
918 snd_als4000_configure(chip);
919 snd_sbdsp_reset(chip);
920 snd_sbmixer_resume(chip);
921
922#ifdef SUPPORT_JOYSTICK
923 if (acard->gameport)
Andreas Mohrc0874442008-08-20 10:04:56 +0200924 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
Takashi Iwai70352912005-11-17 16:16:36 +0100925#endif
926
927 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
928 return 0;
929}
Andreas Mohrc0874442008-08-20 10:04:56 +0200930#endif /* CONFIG_PM */
Takashi Iwai70352912005-11-17 16:16:36 +0100931
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933static struct pci_driver driver = {
934 .name = "ALS4000",
935 .id_table = snd_als4000_ids,
936 .probe = snd_card_als4000_probe,
937 .remove = __devexit_p(snd_card_als4000_remove),
Takashi Iwai70352912005-11-17 16:16:36 +0100938#ifdef CONFIG_PM
939 .suspend = snd_als4000_suspend,
940 .resume = snd_als4000_resume,
941#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942};
943
944static int __init alsa_card_als4000_init(void)
945{
Takashi Iwai01d25d42005-04-11 16:58:24 +0200946 return pci_register_driver(&driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949static void __exit alsa_card_als4000_exit(void)
950{
951 pci_unregister_driver(&driver);
952}
953
954module_init(alsa_card_als4000_init)
955module_exit(alsa_card_als4000_exit)