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@perex.cz>
5 * Copyright (C) 2002 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
7 * Framework borrowed from Massimo Piccioni's card-als100.c.
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.
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.
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
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.
30 * Note: this is no longer true. Pretty verbose chip docu (ALS4000a.PDF)
31 * can be found on the ALSA web site.
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.
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.
43 * The ALS4000 can do real full duplex playback/capture.
49 * Enable/disable 3D sound:
51 * - change bit 6 (0x40) of port 0x15
56 * 0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
59 * - value -> some port 0x0c0d
62 * - Proper shared IRQ handling?
63 * - by default, don't enable legacy game and use PCI game I/O
64 * - power management? (card can do voice wakeup according to datasheet!!)
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 <linux/dma-mapping.h>
74 #include <sound/core.h>
75 #include <sound/pcm.h>
76 #include <sound/rawmidi.h>
77 #include <sound/mpu401.h>
78 #include <sound/opl3.h>
80 #include <sound/initval.h>
82 MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>");
83 MODULE_DESCRIPTION("Avance Logic ALS4000");
84 MODULE_LICENSE("GPL");
85 MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
87 #if defined(CONFIG_GAMEPORT) || (defined(MODULE) && defined(CONFIG_GAMEPORT_MODULE))
88 #define SUPPORT_JOYSTICK 1
91 static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
92 static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
93 static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
94 #ifdef SUPPORT_JOYSTICK
95 static int joystick_port[SNDRV_CARDS];
98 module_param_array(index, int, NULL, 0444);
99 MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
100 module_param_array(id, charp, NULL, 0444);
101 MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
102 module_param_array(enable, bool, NULL, 0444);
103 MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
104 #ifdef SUPPORT_JOYSTICK
105 module_param_array(joystick_port, int, NULL, 0444);
106 MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
109 struct snd_card_als4000 {
110 /* most frequent access first */
111 unsigned long iobase;
114 #ifdef SUPPORT_JOYSTICK
115 struct gameport *gameport;
119 static struct pci_device_id snd_als4000_ids[] = {
120 { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
124 MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
126 enum 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 */
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,
181 enum als4k_gcr_8c_t {
182 ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
183 ALS4K_GCR_8C_CHIP_REV_MASK = 0xf0000
186 static inline void snd_als4000_gcr_write_addr(unsigned long iobase,
187 enum als4k_gcr_t reg,
190 outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
191 outl(val, iobase + ALS4K_IOD_08_GCR_DATA);
194 static inline void snd_als4000_gcr_write(struct snd_sb *sb,
195 enum als4k_gcr_t reg,
198 snd_als4000_gcr_write_addr(sb->alt_port, reg, val);
201 static inline u32 snd_als4000_gcr_read_addr(unsigned long iobase,
202 enum als4k_gcr_t reg)
204 outb(reg, iobase + ALS4K_IOB_0C_GCR_INDEX);
205 return inl(iobase + ALS4K_IOD_08_GCR_DATA);
208 static inline u32 snd_als4000_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
210 return snd_als4000_gcr_read_addr(sb->alt_port, reg);
213 static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
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);
222 static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
223 dma_addr_t addr, unsigned size)
225 snd_als4000_gcr_write(chip, ALS4K_GCR_A2_FIFO2_PCIADDR, addr);
226 snd_als4000_gcr_write(chip, ALS4K_GCR_A3_FIFO2_COUNT, (size-1));
229 static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
233 snd_als4000_gcr_write(chip, ALS4K_GCR_91_DMA0_ADDR, addr);
234 snd_als4000_gcr_write(chip, ALS4K_GCR_92_DMA0_MODE_COUNT,
238 #define ALS4000_FORMAT_SIGNED (1<<0)
239 #define ALS4000_FORMAT_16BIT (1<<1)
240 #define ALS4000_FORMAT_STEREO (1<<2)
242 static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
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;
256 /* structure for setting up playback */
257 static const struct {
258 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 },
277 #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
279 /* structure for setting up capture */
280 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
281 static const unsigned char capture_cmd_vals[]=
283 CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
284 CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
285 CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
286 CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
287 CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
288 CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
289 CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
290 CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
292 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
294 static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
295 struct snd_pcm_hw_params *hw_params)
297 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
300 static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
302 snd_pcm_lib_free_pages(substream);
306 static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
308 struct snd_sb *chip = snd_pcm_substream_chip(substream);
309 struct snd_pcm_runtime *runtime = substream->runtime;
313 chip->capture_format = snd_als4000_get_format(runtime);
315 size = snd_pcm_lib_buffer_bytes(substream);
316 count = snd_pcm_lib_period_bytes(substream);
318 if (chip->capture_format & ALS4000_FORMAT_16BIT)
322 spin_lock_irq(&chip->reg_lock);
323 snd_als4000_set_rate(chip, runtime->rate);
324 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
325 spin_unlock_irq(&chip->reg_lock);
326 spin_lock_irq(&chip->mixer_lock);
327 snd_sbmixer_write(chip, 0xdc, count);
328 snd_sbmixer_write(chip, 0xdd, count>>8);
329 spin_unlock_irq(&chip->mixer_lock);
333 static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
335 struct snd_sb *chip = snd_pcm_substream_chip(substream);
336 struct snd_pcm_runtime *runtime = substream->runtime;
340 chip->playback_format = snd_als4000_get_format(runtime);
342 size = snd_pcm_lib_buffer_bytes(substream);
343 count = snd_pcm_lib_period_bytes(substream);
345 if (chip->playback_format & ALS4000_FORMAT_16BIT)
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. */
355 spin_lock_irq(&chip->reg_lock);
356 snd_als4000_set_rate(chip, runtime->rate);
357 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
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);
366 spin_unlock_irq(&chip->reg_lock);
371 static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
373 struct snd_sb *chip = snd_pcm_substream_chip(substream);
376 /* 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?)
382 spin_lock(&chip->mixer_lock);
384 case SNDRV_PCM_TRIGGER_START:
385 case SNDRV_PCM_TRIGGER_RESUME:
386 chip->mode |= SB_RATE_LOCK_CAPTURE;
387 snd_sbmixer_write(chip, 0xde, capture_cmd(chip));
389 case SNDRV_PCM_TRIGGER_STOP:
390 case SNDRV_PCM_TRIGGER_SUSPEND:
391 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
392 snd_sbmixer_write(chip, 0xde, 0);
398 spin_unlock(&chip->mixer_lock);
402 static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
404 struct snd_sb *chip = snd_pcm_substream_chip(substream);
407 spin_lock(&chip->reg_lock);
409 case SNDRV_PCM_TRIGGER_START:
410 case SNDRV_PCM_TRIGGER_RESUME:
411 chip->mode |= SB_RATE_LOCK_PLAYBACK;
412 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
414 case SNDRV_PCM_TRIGGER_STOP:
415 case SNDRV_PCM_TRIGGER_SUSPEND:
416 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
417 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
423 spin_unlock(&chip->reg_lock);
427 static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
429 struct snd_sb *chip = snd_pcm_substream_chip(substream);
432 spin_lock(&chip->reg_lock);
433 result = snd_als4000_gcr_read(chip, ALS4K_GCR_A4_FIFO2_CURRENT_ADDR);
435 spin_unlock(&chip->reg_lock);
436 return bytes_to_frames( substream->runtime, result );
439 static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
441 struct snd_sb *chip = snd_pcm_substream_chip(substream);
444 spin_lock(&chip->reg_lock);
445 result = snd_als4000_gcr_read(chip, ALS4K_GCR_A0_FIFO1_CURRENT_ADDR);
447 spin_unlock(&chip->reg_lock);
448 return bytes_to_frames( substream->runtime, result );
451 /* 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
455 * register (alt_port + ALS4K_IOB_0E_SB_MPU_IRQ). Probably something
456 * could be optimized here to query/write one register only...
457 * 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*
461 * (hmm, page 38 mentions it the other way around!)
462 * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
464 static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
466 struct snd_sb *chip = dev_id;
470 /* find out which bit of the ALS4000 produced the interrupt */
471 gcr_status = inb(chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
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 */
478 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
479 /* release the gcr */
480 outb(gcr_status, chip->alt_port + ALS4K_IOB_0E_SB_MPU_IRQ);
482 spin_lock(&chip->mixer_lock);
483 sb_status = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
484 spin_unlock(&chip->mixer_lock);
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)
492 if (sb_status & 0x20)
493 inb(SBP(chip, RESET));
497 /*****************************************************************/
499 static struct snd_pcm_hardware snd_als4000_playback =
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,
510 .buffer_bytes_max = 65536,
511 .period_bytes_min = 64,
512 .period_bytes_max = 65536,
518 static struct snd_pcm_hardware snd_als4000_capture =
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,
529 .buffer_bytes_max = 65536,
530 .period_bytes_min = 64,
531 .period_bytes_max = 65536,
537 /*****************************************************************/
539 static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
541 struct snd_sb *chip = snd_pcm_substream_chip(substream);
542 struct snd_pcm_runtime *runtime = substream->runtime;
544 chip->playback_substream = substream;
545 runtime->hw = snd_als4000_playback;
549 static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
551 struct snd_sb *chip = snd_pcm_substream_chip(substream);
553 chip->playback_substream = NULL;
554 snd_pcm_lib_free_pages(substream);
558 static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
560 struct snd_sb *chip = snd_pcm_substream_chip(substream);
561 struct snd_pcm_runtime *runtime = substream->runtime;
563 chip->capture_substream = substream;
564 runtime->hw = snd_als4000_capture;
568 static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
570 struct snd_sb *chip = snd_pcm_substream_chip(substream);
572 chip->capture_substream = NULL;
573 snd_pcm_lib_free_pages(substream);
577 /******************************************************************/
579 static struct snd_pcm_ops snd_als4000_playback_ops = {
580 .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
590 static struct snd_pcm_ops snd_als4000_capture_ops = {
591 .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
601 static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
606 if ((err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm)) < 0)
608 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);
613 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
621 /******************************************************************/
623 static void snd_als4000_set_addr(unsigned long iobase,
627 unsigned int game_io)
633 cfg2 |= (mpu_io | 1) << 16;
637 cfg1 |= (game_io | 1) << 16;
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);
644 static void snd_als4000_configure(struct snd_sb *chip)
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);
658 spin_lock_irq(&chip->reg_lock);
659 /* enable interrupts */
660 snd_als4000_gcr_write(chip, ALS4K_GCR_8C_MISC_CTRL,
661 ALS4K_GCR_8C_IRQ_MASK_CTRL_ENABLE);
663 for (i = ALS4K_GCR_91_DMA0_ADDR; i <= ALS4K_GCR_96_DMA3_MODE_COUNT; ++i)
664 snd_als4000_gcr_write(chip, i, 0);
666 snd_als4000_gcr_write(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL,
667 snd_als4000_gcr_read(chip, ALS4K_GCR_99_DMA_EMULATION_CTRL));
668 spin_unlock_irq(&chip->reg_lock);
671 #ifdef SUPPORT_JOYSTICK
672 static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
678 if (joystick_port[dev] == 0)
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");
688 io_port = joystick_port[dev];
689 r = request_region(io_port, 8, "ALS4000 gameport");
693 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
697 acard->gameport = gp = gameport_allocate_port();
699 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
700 release_and_free_resource(r);
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);
708 gameport_set_port_data(gp, r);
710 /* Enable legacy joystick port */
711 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
713 gameport_register_port(acard->gameport);
718 static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
720 if (acard->gameport) {
721 struct resource *r = gameport_get_port_data(acard->gameport);
723 gameport_unregister_port(acard->gameport);
724 acard->gameport = NULL;
726 /* disable joystick */
727 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
729 release_and_free_resource(r);
733 static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
734 static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
737 static void snd_card_als4000_free( struct snd_card *card )
739 struct snd_card_als4000 *acard = card->private_data;
741 /* make sure that interrupts are disabled */
742 snd_als4000_gcr_write_addr(acard->iobase, ALS4K_GCR_8C_MISC_CTRL, 0);
744 snd_als4000_free_gameport(acard);
745 pci_release_regions(acard->pci);
746 pci_disable_device(acard->pci);
749 static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
750 const struct pci_device_id *pci_id)
753 struct snd_card *card;
754 struct snd_card_als4000 *acard;
755 unsigned long iobase;
757 struct snd_opl3 *opl3;
761 if (dev >= SNDRV_CARDS)
768 /* enable PCI device */
769 if ((err = pci_enable_device(pci)) < 0) {
772 /* check, if we can restrict PCI DMA transfers to 24 bits */
773 if (pci_set_dma_mask(pci, DMA_24BIT_MASK) < 0 ||
774 pci_set_consistent_dma_mask(pci, DMA_24BIT_MASK) < 0) {
775 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
776 pci_disable_device(pci);
780 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
781 pci_disable_device(pci);
784 iobase = pci_resource_start(pci, 0);
786 pci_read_config_word(pci, PCI_COMMAND, &word);
787 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
790 card = snd_card_new(index[dev], id[dev], THIS_MODULE,
791 sizeof( struct snd_card_als4000 ) );
793 pci_release_regions(pci);
794 pci_disable_device(pci);
798 acard = card->private_data;
800 acard->iobase = iobase;
801 card->private_free = snd_card_als4000_free;
803 /* disable all legacy ISA stuff */
804 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
806 if ((err = snd_sbdsp_create(card,
807 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
809 snd_als4000_interrupt,
819 chip->alt_port = iobase;
820 snd_card_set_dev(card, &pci->dev);
822 snd_als4000_configure(chip);
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);
829 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
830 iobase + ALS4K_IOB_30_MIDI_DATA,
831 MPU401_INFO_INTEGRATED,
832 pci->irq, 0, &chip->rmidi)) < 0) {
833 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n",
834 iobase + ALS4K_IOB_30_MIDI_DATA);
837 /* 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... */
841 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
844 if ((err = snd_sbmixer_new(chip)) < 0) {
848 if (snd_opl3_create(card,
849 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
850 iobase + ALS4K_IOB_12_ADLIB_ADDR2,
851 OPL3_HW_AUTO, 1, &opl3) < 0) {
852 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
853 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
854 iobase + ALS4K_IOB_12_ADLIB_ADDR2);
856 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
861 snd_als4000_create_gameport(acard, dev);
863 if ((err = snd_card_register(card)) < 0) {
866 pci_set_drvdata(pci, card);
878 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
880 snd_card_free(pci_get_drvdata(pci));
881 pci_set_drvdata(pci, NULL);
885 static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
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;
891 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
893 snd_pcm_suspend_all(chip->pcm);
894 snd_sbmixer_suspend(chip);
896 pci_disable_device(pci);
898 pci_set_power_state(pci, pci_choose_state(pci, state));
902 static int snd_als4000_resume(struct pci_dev *pci)
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;
908 pci_set_power_state(pci, PCI_D0);
909 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);
918 snd_als4000_configure(chip);
919 snd_sbdsp_reset(chip);
920 snd_sbmixer_resume(chip);
922 #ifdef SUPPORT_JOYSTICK
924 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
927 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
930 #endif /* CONFIG_PM */
933 static struct pci_driver driver = {
935 .id_table = snd_als4000_ids,
936 .probe = snd_card_als4000_probe,
937 .remove = __devexit_p(snd_card_als4000_remove),
939 .suspend = snd_als4000_suspend,
940 .resume = snd_als4000_resume,
944 static int __init alsa_card_als4000_init(void)
946 return pci_register_driver(&driver);
949 static void __exit alsa_card_als4000_exit(void)
951 pci_unregister_driver(&driver);
954 module_init(alsa_card_als4000_init)
955 module_exit(alsa_card_als4000_exit)