GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / pci / als4000.c
blobf39b38d6c48ba8744b80f742d636d51b0b1d511a
1 /*
2 * card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
3 * Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
4 * Jaroslav Kysela <perex@perex.cz>
5 * Copyright (C) 2002, 2008 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
24 * NOTES
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 (thank you!):
31 * pretty verbose chip docu (ALS4000a.PDF) can be found on the ALSA web site.
32 * Page numbers stated anywhere below with the "SPECS_PAGE:" tag
33 * refer to: ALS4000a.PDF specs Ver 1.0, May 28th, 1998.
35 * The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
36 * ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport
37 * interface. These subsystems can be mapped into ISA io-port space,
38 * using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ
39 * services to the subsystems.
41 * While ALS4000 is very similar to a SoundBlaster, the differences in
42 * DMA and capturing require more changes to the SoundBlaster than
43 * desirable, so I made this separate driver.
45 * The ALS4000 can do real full duplex playback/capture.
47 * FMDAC:
48 * - 0x4f -> port 0x14
49 * - port 0x15 |= 1
51 * Enable/disable 3D sound:
52 * - 0x50 -> port 0x14
53 * - change bit 6 (0x40) of port 0x15
55 * Set QSound:
56 * - 0xdb -> port 0x14
57 * - set port 0x15:
58 * 0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
60 * Set KSound:
61 * - value -> some port 0x0c0d
63 * ToDo:
64 * - by default, don't enable legacy game and use PCI game I/O
65 * - power management? (card can do voice wakeup according to datasheet!!)
68 #include <asm/io.h>
69 #include <linux/init.h>
70 #include <linux/pci.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>
79 #include <sound/sb.h>
80 #include <sound/initval.h>
82 MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>, Andreas Mohr");
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
89 #endif
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];
96 #endif
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)");
107 #endif
109 struct snd_card_als4000 {
110 /* most frequent access first */
111 unsigned long iobase;
112 struct pci_dev *pci;
113 struct snd_sb *chip;
114 #ifdef SUPPORT_JOYSTICK
115 struct gameport *gameport;
116 #endif
119 static DEFINE_PCI_DEVICE_TABLE(snd_als4000_ids) = {
120 { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
121 { 0, }
124 MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
126 enum als4k_iobase_t {
127 /* IOx: B == Byte, W = Word, D = DWord; SPECS_PAGE: 37 */
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_IRQTYPE_SB_CR1E_MPU = 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_RESET = 0x16,
142 ALS4K_IOB_16_ACK_FOR_CR1E = 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 */
157 enum als4k_iobase_0e_t {
158 ALS4K_IOB_0E_MPU_IRQ = 0x10,
159 ALS4K_IOB_0E_CR1E_IRQ = 0x40,
160 ALS4K_IOB_0E_SB_DMA_IRQ = 0x80,
163 enum als4k_gcr_t { /* all registers 32bit wide; SPECS_PAGE: 38 to 42 */
164 ALS4K_GCR8C_MISC_CTRL = 0x8c,
165 ALS4K_GCR90_TEST_MODE_REG = 0x90,
166 ALS4K_GCR91_DMA0_ADDR = 0x91,
167 ALS4K_GCR92_DMA0_MODE_COUNT = 0x92,
168 ALS4K_GCR93_DMA1_ADDR = 0x93,
169 ALS4K_GCR94_DMA1_MODE_COUNT = 0x94,
170 ALS4K_GCR95_DMA3_ADDR = 0x95,
171 ALS4K_GCR96_DMA3_MODE_COUNT = 0x96,
172 ALS4K_GCR99_DMA_EMULATION_CTRL = 0x99,
173 ALS4K_GCRA0_FIFO1_CURRENT_ADDR = 0xa0,
174 ALS4K_GCRA1_FIFO1_STATUS_BYTECOUNT = 0xa1,
175 ALS4K_GCRA2_FIFO2_PCIADDR = 0xa2,
176 ALS4K_GCRA3_FIFO2_COUNT = 0xa3,
177 ALS4K_GCRA4_FIFO2_CURRENT_ADDR = 0xa4,
178 ALS4K_GCRA5_FIFO1_STATUS_BYTECOUNT = 0xa5,
179 ALS4K_GCRA6_PM_CTRL = 0xa6,
180 ALS4K_GCRA7_PCI_ACCESS_STORAGE = 0xa7,
181 ALS4K_GCRA8_LEGACY_CFG1 = 0xa8,
182 ALS4K_GCRA9_LEGACY_CFG2 = 0xa9,
183 ALS4K_GCRFF_DUMMY_SCRATCH = 0xff,
186 enum als4k_gcr8c_t {
187 ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
188 ALS4K_GCR8C_CHIP_REV_MASK = 0xf0000
191 static inline void snd_als4k_iobase_writeb(unsigned long iobase,
192 enum als4k_iobase_t reg,
193 u8 val)
195 outb(val, iobase + reg);
198 static inline void snd_als4k_iobase_writel(unsigned long iobase,
199 enum als4k_iobase_t reg,
200 u32 val)
202 outl(val, iobase + reg);
205 static inline u8 snd_als4k_iobase_readb(unsigned long iobase,
206 enum als4k_iobase_t reg)
208 return inb(iobase + reg);
211 static inline u32 snd_als4k_iobase_readl(unsigned long iobase,
212 enum als4k_iobase_t reg)
214 return inl(iobase + reg);
217 static inline void snd_als4k_gcr_write_addr(unsigned long iobase,
218 enum als4k_gcr_t reg,
219 u32 val)
221 snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
222 snd_als4k_iobase_writel(iobase, ALS4K_IOD_08_GCR_DATA, val);
225 static inline void snd_als4k_gcr_write(struct snd_sb *sb,
226 enum als4k_gcr_t reg,
227 u32 val)
229 snd_als4k_gcr_write_addr(sb->alt_port, reg, val);
232 static inline u32 snd_als4k_gcr_read_addr(unsigned long iobase,
233 enum als4k_gcr_t reg)
235 /* SPECS_PAGE: 37/38 */
236 snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
237 return snd_als4k_iobase_readl(iobase, ALS4K_IOD_08_GCR_DATA);
240 static inline u32 snd_als4k_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
242 return snd_als4k_gcr_read_addr(sb->alt_port, reg);
245 enum als4k_cr_t { /* all registers 8bit wide; SPECS_PAGE: 20 to 23 */
246 ALS4K_CR0_SB_CONFIG = 0x00,
247 ALS4K_CR2_MISC_CONTROL = 0x02,
248 ALS4K_CR3_CONFIGURATION = 0x03,
249 ALS4K_CR17_FIFO_STATUS = 0x17,
250 ALS4K_CR18_ESP_MAJOR_VERSION = 0x18,
251 ALS4K_CR19_ESP_MINOR_VERSION = 0x19,
252 ALS4K_CR1A_MPU401_UART_MODE_CONTROL = 0x1a,
253 ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO = 0x1c,
254 ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI = 0x1d,
255 ALS4K_CR1E_FIFO2_CONTROL = 0x1e, /* secondary PCM FIFO (recording) */
256 ALS4K_CR3A_MISC_CONTROL = 0x3a,
257 ALS4K_CR3B_CRC32_BYTE0 = 0x3b, /* for testing, activate via CR3A */
258 ALS4K_CR3C_CRC32_BYTE1 = 0x3c,
259 ALS4K_CR3D_CRC32_BYTE2 = 0x3d,
260 ALS4K_CR3E_CRC32_BYTE3 = 0x3e,
263 enum als4k_cr0_t {
264 ALS4K_CR0_DMA_CONTIN_MODE_CTRL = 0x02, /* IRQ/FIFO controlled for 0/1 */
265 ALS4K_CR0_DMA_90H_MODE_CTRL = 0x04, /* IRQ/FIFO controlled for 0/1 */
266 ALS4K_CR0_MX80_81_REG_WRITE_ENABLE = 0x80,
269 static inline void snd_als4_cr_write(struct snd_sb *chip,
270 enum als4k_cr_t reg,
271 u8 data)
273 /* Control Register is reg | 0xc0 (bit 7, 6 set) on sbmixer_index
274 * NOTE: assumes chip->mixer_lock to be locked externally already!
275 * SPECS_PAGE: 6 */
276 snd_sbmixer_write(chip, reg | 0xc0, data);
279 static inline u8 snd_als4_cr_read(struct snd_sb *chip,
280 enum als4k_cr_t reg)
282 /* NOTE: assumes chip->mixer_lock to be locked externally already! */
283 return snd_sbmixer_read(chip, reg | 0xc0);
288 static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
290 if (!(chip->mode & SB_RATE_LOCK)) {
291 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
292 snd_sbdsp_command(chip, rate>>8);
293 snd_sbdsp_command(chip, rate);
297 static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
298 dma_addr_t addr, unsigned size)
300 /* SPECS_PAGE: 40 */
301 snd_als4k_gcr_write(chip, ALS4K_GCRA2_FIFO2_PCIADDR, addr);
302 snd_als4k_gcr_write(chip, ALS4K_GCRA3_FIFO2_COUNT, (size-1));
305 static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
306 dma_addr_t addr,
307 unsigned size)
309 /* SPECS_PAGE: 38 */
310 snd_als4k_gcr_write(chip, ALS4K_GCR91_DMA0_ADDR, addr);
311 snd_als4k_gcr_write(chip, ALS4K_GCR92_DMA0_MODE_COUNT,
312 (size-1)|0x180000);
315 #define ALS4000_FORMAT_SIGNED (1<<0)
316 #define ALS4000_FORMAT_16BIT (1<<1)
317 #define ALS4000_FORMAT_STEREO (1<<2)
319 static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
321 int result;
323 result = 0;
324 if (snd_pcm_format_signed(runtime->format))
325 result |= ALS4000_FORMAT_SIGNED;
326 if (snd_pcm_format_physical_width(runtime->format) == 16)
327 result |= ALS4000_FORMAT_16BIT;
328 if (runtime->channels > 1)
329 result |= ALS4000_FORMAT_STEREO;
330 return result;
333 /* structure for setting up playback */
334 static const struct {
335 unsigned char dsp_cmd, dma_on, dma_off, format;
336 } playback_cmd_vals[]={
337 /* ALS4000_FORMAT_U8_MONO */
338 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
339 /* ALS4000_FORMAT_S8_MONO */
340 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
341 /* ALS4000_FORMAT_U16L_MONO */
342 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
343 /* ALS4000_FORMAT_S16L_MONO */
344 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
345 /* ALS4000_FORMAT_U8_STEREO */
346 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
347 /* ALS4000_FORMAT_S8_STEREO */
348 { SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
349 /* ALS4000_FORMAT_U16L_STEREO */
350 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
351 /* ALS4000_FORMAT_S16L_STEREO */
352 { SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
354 #define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
356 /* structure for setting up capture */
357 enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
358 static const unsigned char capture_cmd_vals[]=
360 CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
361 CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
362 CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
363 CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
364 CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
365 CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
366 CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
367 CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
369 #define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
371 static int snd_als4000_hw_params(struct snd_pcm_substream *substream,
372 struct snd_pcm_hw_params *hw_params)
374 return snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(hw_params));
377 static int snd_als4000_hw_free(struct snd_pcm_substream *substream)
379 snd_pcm_lib_free_pages(substream);
380 return 0;
383 static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
385 struct snd_sb *chip = snd_pcm_substream_chip(substream);
386 struct snd_pcm_runtime *runtime = substream->runtime;
387 unsigned long size;
388 unsigned count;
390 chip->capture_format = snd_als4000_get_format(runtime);
392 size = snd_pcm_lib_buffer_bytes(substream);
393 count = snd_pcm_lib_period_bytes(substream);
395 if (chip->capture_format & ALS4000_FORMAT_16BIT)
396 count >>= 1;
397 count--;
399 spin_lock_irq(&chip->reg_lock);
400 snd_als4000_set_rate(chip, runtime->rate);
401 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
402 spin_unlock_irq(&chip->reg_lock);
403 spin_lock_irq(&chip->mixer_lock);
404 snd_als4_cr_write(chip, ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO, count & 0xff);
405 snd_als4_cr_write(chip, ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI, count >> 8);
406 spin_unlock_irq(&chip->mixer_lock);
407 return 0;
410 static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
412 struct snd_sb *chip = snd_pcm_substream_chip(substream);
413 struct snd_pcm_runtime *runtime = substream->runtime;
414 unsigned long size;
415 unsigned count;
417 chip->playback_format = snd_als4000_get_format(runtime);
419 size = snd_pcm_lib_buffer_bytes(substream);
420 count = snd_pcm_lib_period_bytes(substream);
422 if (chip->playback_format & ALS4000_FORMAT_16BIT)
423 count >>= 1;
424 count--;
426 spin_lock_irq(&chip->reg_lock);
427 snd_als4000_set_rate(chip, runtime->rate);
428 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
430 /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
431 /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
432 snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
433 snd_sbdsp_command(chip, playback_cmd(chip).format);
434 snd_sbdsp_command(chip, count & 0xff);
435 snd_sbdsp_command(chip, count >> 8);
436 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
437 spin_unlock_irq(&chip->reg_lock);
439 return 0;
442 static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
444 struct snd_sb *chip = snd_pcm_substream_chip(substream);
445 int result = 0;
447 spin_lock(&chip->mixer_lock);
448 switch (cmd) {
449 case SNDRV_PCM_TRIGGER_START:
450 case SNDRV_PCM_TRIGGER_RESUME:
451 chip->mode |= SB_RATE_LOCK_CAPTURE;
452 snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
453 capture_cmd(chip));
454 break;
455 case SNDRV_PCM_TRIGGER_STOP:
456 case SNDRV_PCM_TRIGGER_SUSPEND:
457 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
458 snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
459 capture_cmd(chip));
460 break;
461 default:
462 result = -EINVAL;
463 break;
465 spin_unlock(&chip->mixer_lock);
466 return result;
469 static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
471 struct snd_sb *chip = snd_pcm_substream_chip(substream);
472 int result = 0;
474 spin_lock(&chip->reg_lock);
475 switch (cmd) {
476 case SNDRV_PCM_TRIGGER_START:
477 case SNDRV_PCM_TRIGGER_RESUME:
478 chip->mode |= SB_RATE_LOCK_PLAYBACK;
479 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
480 break;
481 case SNDRV_PCM_TRIGGER_STOP:
482 case SNDRV_PCM_TRIGGER_SUSPEND:
483 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
484 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
485 break;
486 default:
487 result = -EINVAL;
488 break;
490 spin_unlock(&chip->reg_lock);
491 return result;
494 static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
496 struct snd_sb *chip = snd_pcm_substream_chip(substream);
497 unsigned int result;
499 spin_lock(&chip->reg_lock);
500 result = snd_als4k_gcr_read(chip, ALS4K_GCRA4_FIFO2_CURRENT_ADDR);
501 spin_unlock(&chip->reg_lock);
502 result &= 0xffff;
503 return bytes_to_frames( substream->runtime, result );
506 static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
508 struct snd_sb *chip = snd_pcm_substream_chip(substream);
509 unsigned result;
511 spin_lock(&chip->reg_lock);
512 result = snd_als4k_gcr_read(chip, ALS4K_GCRA0_FIFO1_CURRENT_ADDR);
513 spin_unlock(&chip->reg_lock);
514 result &= 0xffff;
515 return bytes_to_frames( substream->runtime, result );
518 static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
520 struct snd_sb *chip = dev_id;
521 unsigned pci_irqstatus;
522 unsigned sb_irqstatus;
524 /* find out which bit of the ALS4000 PCI block produced the interrupt,
525 SPECS_PAGE: 38, 5 */
526 pci_irqstatus = snd_als4k_iobase_readb(chip->alt_port,
527 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU);
528 if ((pci_irqstatus & ALS4K_IOB_0E_SB_DMA_IRQ)
529 && (chip->playback_substream)) /* playback */
530 snd_pcm_period_elapsed(chip->playback_substream);
531 if ((pci_irqstatus & ALS4K_IOB_0E_CR1E_IRQ)
532 && (chip->capture_substream)) /* capturing */
533 snd_pcm_period_elapsed(chip->capture_substream);
534 if ((pci_irqstatus & ALS4K_IOB_0E_MPU_IRQ)
535 && (chip->rmidi)) /* MPU401 interrupt */
536 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
537 /* ACK the PCI block IRQ */
538 snd_als4k_iobase_writeb(chip->alt_port,
539 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU, pci_irqstatus);
541 spin_lock(&chip->mixer_lock);
542 /* SPECS_PAGE: 20 */
543 sb_irqstatus = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
544 spin_unlock(&chip->mixer_lock);
546 if (sb_irqstatus & SB_IRQTYPE_8BIT)
547 snd_sb_ack_8bit(chip);
548 if (sb_irqstatus & SB_IRQTYPE_16BIT)
549 snd_sb_ack_16bit(chip);
550 if (sb_irqstatus & SB_IRQTYPE_MPUIN)
551 inb(chip->mpu_port);
552 if (sb_irqstatus & ALS4K_IRQTYPE_CR1E_DMA)
553 snd_als4k_iobase_readb(chip->alt_port,
554 ALS4K_IOB_16_ACK_FOR_CR1E);
556 /* printk(KERN_INFO "als4000: irq 0x%04x 0x%04x\n",
557 pci_irqstatus, sb_irqstatus); */
559 /* only ack the things we actually handled above */
560 return IRQ_RETVAL(
561 (pci_irqstatus & (ALS4K_IOB_0E_SB_DMA_IRQ|ALS4K_IOB_0E_CR1E_IRQ|
562 ALS4K_IOB_0E_MPU_IRQ))
563 || (sb_irqstatus & (SB_IRQTYPE_8BIT|SB_IRQTYPE_16BIT|
564 SB_IRQTYPE_MPUIN|ALS4K_IRQTYPE_CR1E_DMA))
568 /*****************************************************************/
570 static struct snd_pcm_hardware snd_als4000_playback =
572 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
573 SNDRV_PCM_INFO_MMAP_VALID),
574 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
575 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
576 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
577 .rate_min = 4000,
578 .rate_max = 48000,
579 .channels_min = 1,
580 .channels_max = 2,
581 .buffer_bytes_max = 65536,
582 .period_bytes_min = 64,
583 .period_bytes_max = 65536,
584 .periods_min = 1,
585 .periods_max = 1024,
586 .fifo_size = 0
589 static struct snd_pcm_hardware snd_als4000_capture =
591 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
592 SNDRV_PCM_INFO_MMAP_VALID),
593 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
594 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
595 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
596 .rate_min = 4000,
597 .rate_max = 48000,
598 .channels_min = 1,
599 .channels_max = 2,
600 .buffer_bytes_max = 65536,
601 .period_bytes_min = 64,
602 .period_bytes_max = 65536,
603 .periods_min = 1,
604 .periods_max = 1024,
605 .fifo_size = 0
608 /*****************************************************************/
610 static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
612 struct snd_sb *chip = snd_pcm_substream_chip(substream);
613 struct snd_pcm_runtime *runtime = substream->runtime;
615 chip->playback_substream = substream;
616 runtime->hw = snd_als4000_playback;
617 return 0;
620 static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
622 struct snd_sb *chip = snd_pcm_substream_chip(substream);
624 chip->playback_substream = NULL;
625 snd_pcm_lib_free_pages(substream);
626 return 0;
629 static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
631 struct snd_sb *chip = snd_pcm_substream_chip(substream);
632 struct snd_pcm_runtime *runtime = substream->runtime;
634 chip->capture_substream = substream;
635 runtime->hw = snd_als4000_capture;
636 return 0;
639 static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
641 struct snd_sb *chip = snd_pcm_substream_chip(substream);
643 chip->capture_substream = NULL;
644 snd_pcm_lib_free_pages(substream);
645 return 0;
648 /******************************************************************/
650 static struct snd_pcm_ops snd_als4000_playback_ops = {
651 .open = snd_als4000_playback_open,
652 .close = snd_als4000_playback_close,
653 .ioctl = snd_pcm_lib_ioctl,
654 .hw_params = snd_als4000_hw_params,
655 .hw_free = snd_als4000_hw_free,
656 .prepare = snd_als4000_playback_prepare,
657 .trigger = snd_als4000_playback_trigger,
658 .pointer = snd_als4000_playback_pointer
661 static struct snd_pcm_ops snd_als4000_capture_ops = {
662 .open = snd_als4000_capture_open,
663 .close = snd_als4000_capture_close,
664 .ioctl = snd_pcm_lib_ioctl,
665 .hw_params = snd_als4000_hw_params,
666 .hw_free = snd_als4000_hw_free,
667 .prepare = snd_als4000_capture_prepare,
668 .trigger = snd_als4000_capture_trigger,
669 .pointer = snd_als4000_capture_pointer
672 static int __devinit snd_als4000_pcm(struct snd_sb *chip, int device)
674 struct snd_pcm *pcm;
675 int err;
677 err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm);
678 if (err < 0)
679 return err;
680 pcm->private_data = chip;
681 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
682 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
683 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
685 snd_pcm_lib_preallocate_pages_for_all(pcm, SNDRV_DMA_TYPE_DEV, snd_dma_pci_data(chip->pci),
686 64*1024, 64*1024);
688 chip->pcm = pcm;
690 return 0;
693 /******************************************************************/
695 static void snd_als4000_set_addr(unsigned long iobase,
696 unsigned int sb_io,
697 unsigned int mpu_io,
698 unsigned int opl_io,
699 unsigned int game_io)
701 u32 cfg1 = 0;
702 u32 cfg2 = 0;
704 if (mpu_io > 0)
705 cfg2 |= (mpu_io | 1) << 16;
706 if (sb_io > 0)
707 cfg2 |= (sb_io | 1);
708 if (game_io > 0)
709 cfg1 |= (game_io | 1) << 16;
710 if (opl_io > 0)
711 cfg1 |= (opl_io | 1);
712 snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA8_LEGACY_CFG1, cfg1);
713 snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA9_LEGACY_CFG2, cfg2);
716 static void snd_als4000_configure(struct snd_sb *chip)
718 u8 tmp;
719 int i;
721 /* do some more configuration */
722 spin_lock_irq(&chip->mixer_lock);
723 tmp = snd_als4_cr_read(chip, ALS4K_CR0_SB_CONFIG);
724 snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
725 tmp|ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
726 /* always select DMA channel 0, since we do not actually use DMA
727 * SPECS_PAGE: 19/20 */
728 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
729 snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
730 tmp & ~ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
731 spin_unlock_irq(&chip->mixer_lock);
733 spin_lock_irq(&chip->reg_lock);
734 /* enable interrupts */
735 snd_als4k_gcr_write(chip, ALS4K_GCR8C_MISC_CTRL,
736 ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE);
738 /* SPECS_PAGE: 39 */
739 for (i = ALS4K_GCR91_DMA0_ADDR; i <= ALS4K_GCR96_DMA3_MODE_COUNT; ++i)
740 snd_als4k_gcr_write(chip, i, 0);
741 /* enable burst mode to prevent dropouts during high PCI bus usage */
742 snd_als4k_gcr_write(chip, ALS4K_GCR99_DMA_EMULATION_CTRL,
743 (snd_als4k_gcr_read(chip, ALS4K_GCR99_DMA_EMULATION_CTRL) & ~0x07) | 0x04);
744 spin_unlock_irq(&chip->reg_lock);
747 #ifdef SUPPORT_JOYSTICK
748 static int __devinit snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
750 struct gameport *gp;
751 struct resource *r;
752 int io_port;
754 if (joystick_port[dev] == 0)
755 return -ENODEV;
757 if (joystick_port[dev] == 1) { /* auto-detect */
758 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
759 r = request_region(io_port, 8, "ALS4000 gameport");
760 if (r)
761 break;
763 } else {
764 io_port = joystick_port[dev];
765 r = request_region(io_port, 8, "ALS4000 gameport");
768 if (!r) {
769 printk(KERN_WARNING "als4000: cannot reserve joystick ports\n");
770 return -EBUSY;
773 acard->gameport = gp = gameport_allocate_port();
774 if (!gp) {
775 printk(KERN_ERR "als4000: cannot allocate memory for gameport\n");
776 release_and_free_resource(r);
777 return -ENOMEM;
780 gameport_set_name(gp, "ALS4000 Gameport");
781 gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
782 gameport_set_dev_parent(gp, &acard->pci->dev);
783 gp->io = io_port;
784 gameport_set_port_data(gp, r);
786 /* Enable legacy joystick port */
787 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
789 gameport_register_port(acard->gameport);
791 return 0;
794 static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
796 if (acard->gameport) {
797 struct resource *r = gameport_get_port_data(acard->gameport);
799 gameport_unregister_port(acard->gameport);
800 acard->gameport = NULL;
802 /* disable joystick */
803 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
805 release_and_free_resource(r);
808 #else
809 static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
810 static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
811 #endif
813 static void snd_card_als4000_free( struct snd_card *card )
815 struct snd_card_als4000 *acard = card->private_data;
817 /* make sure that interrupts are disabled */
818 snd_als4k_gcr_write_addr(acard->iobase, ALS4K_GCR8C_MISC_CTRL, 0);
819 /* free resources */
820 snd_als4000_free_gameport(acard);
821 pci_release_regions(acard->pci);
822 pci_disable_device(acard->pci);
825 static int __devinit snd_card_als4000_probe(struct pci_dev *pci,
826 const struct pci_device_id *pci_id)
828 static int dev;
829 struct snd_card *card;
830 struct snd_card_als4000 *acard;
831 unsigned long iobase;
832 struct snd_sb *chip;
833 struct snd_opl3 *opl3;
834 unsigned short word;
835 int err;
837 if (dev >= SNDRV_CARDS)
838 return -ENODEV;
839 if (!enable[dev]) {
840 dev++;
841 return -ENOENT;
844 /* enable PCI device */
845 if ((err = pci_enable_device(pci)) < 0) {
846 return err;
848 /* check, if we can restrict PCI DMA transfers to 24 bits */
849 if (pci_set_dma_mask(pci, DMA_BIT_MASK(24)) < 0 ||
850 pci_set_consistent_dma_mask(pci, DMA_BIT_MASK(24)) < 0) {
851 snd_printk(KERN_ERR "architecture does not support 24bit PCI busmaster DMA\n");
852 pci_disable_device(pci);
853 return -ENXIO;
856 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
857 pci_disable_device(pci);
858 return err;
860 iobase = pci_resource_start(pci, 0);
862 pci_read_config_word(pci, PCI_COMMAND, &word);
863 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
864 pci_set_master(pci);
866 err = snd_card_create(index[dev], id[dev], THIS_MODULE,
867 sizeof(*acard) /* private_data: acard */,
868 &card);
869 if (err < 0) {
870 pci_release_regions(pci);
871 pci_disable_device(pci);
872 return err;
875 acard = card->private_data;
876 acard->pci = pci;
877 acard->iobase = iobase;
878 card->private_free = snd_card_als4000_free;
880 /* disable all legacy ISA stuff */
881 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
883 if ((err = snd_sbdsp_create(card,
884 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
885 pci->irq,
886 /* internally registered as IRQF_SHARED in case of ALS4000 SB */
887 snd_als4000_interrupt,
890 SB_HW_ALS4000,
891 &chip)) < 0) {
892 goto out_err;
894 acard->chip = chip;
896 chip->pci = pci;
897 chip->alt_port = iobase;
898 snd_card_set_dev(card, &pci->dev);
900 snd_als4000_configure(chip);
902 strcpy(card->driver, "ALS4000");
903 strcpy(card->shortname, "Avance Logic ALS4000");
904 sprintf(card->longname, "%s at 0x%lx, irq %i",
905 card->shortname, chip->alt_port, chip->irq);
907 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
908 iobase + ALS4K_IOB_30_MIDI_DATA,
909 MPU401_INFO_INTEGRATED,
910 pci->irq, 0, &chip->rmidi)) < 0) {
911 printk(KERN_ERR "als4000: no MPU-401 device at 0x%lx?\n",
912 iobase + ALS4K_IOB_30_MIDI_DATA);
913 goto out_err;
916 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
917 goto out_err;
919 if ((err = snd_sbmixer_new(chip)) < 0) {
920 goto out_err;
923 if (snd_opl3_create(card,
924 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
925 iobase + ALS4K_IOB_12_ADLIB_ADDR2,
926 OPL3_HW_AUTO, 1, &opl3) < 0) {
927 printk(KERN_ERR "als4000: no OPL device at 0x%lx-0x%lx?\n",
928 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
929 iobase + ALS4K_IOB_12_ADLIB_ADDR2);
930 } else {
931 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
932 goto out_err;
936 snd_als4000_create_gameport(acard, dev);
938 if ((err = snd_card_register(card)) < 0) {
939 goto out_err;
941 pci_set_drvdata(pci, card);
942 dev++;
943 err = 0;
944 goto out;
946 out_err:
947 snd_card_free(card);
949 out:
950 return err;
953 static void __devexit snd_card_als4000_remove(struct pci_dev *pci)
955 snd_card_free(pci_get_drvdata(pci));
956 pci_set_drvdata(pci, NULL);
959 #ifdef CONFIG_PM
960 static int snd_als4000_suspend(struct pci_dev *pci, pm_message_t state)
962 struct snd_card *card = pci_get_drvdata(pci);
963 struct snd_card_als4000 *acard = card->private_data;
964 struct snd_sb *chip = acard->chip;
966 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
968 snd_pcm_suspend_all(chip->pcm);
969 snd_sbmixer_suspend(chip);
971 pci_disable_device(pci);
972 pci_save_state(pci);
973 pci_set_power_state(pci, pci_choose_state(pci, state));
974 return 0;
977 static int snd_als4000_resume(struct pci_dev *pci)
979 struct snd_card *card = pci_get_drvdata(pci);
980 struct snd_card_als4000 *acard = card->private_data;
981 struct snd_sb *chip = acard->chip;
983 pci_set_power_state(pci, PCI_D0);
984 pci_restore_state(pci);
985 if (pci_enable_device(pci) < 0) {
986 printk(KERN_ERR "als4000: pci_enable_device failed, "
987 "disabling device\n");
988 snd_card_disconnect(card);
989 return -EIO;
991 pci_set_master(pci);
993 snd_als4000_configure(chip);
994 snd_sbdsp_reset(chip);
995 snd_sbmixer_resume(chip);
997 #ifdef SUPPORT_JOYSTICK
998 if (acard->gameport)
999 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
1000 #endif
1002 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
1003 return 0;
1005 #endif /* CONFIG_PM */
1008 static struct pci_driver driver = {
1009 .name = "ALS4000",
1010 .id_table = snd_als4000_ids,
1011 .probe = snd_card_als4000_probe,
1012 .remove = __devexit_p(snd_card_als4000_remove),
1013 #ifdef CONFIG_PM
1014 .suspend = snd_als4000_suspend,
1015 .resume = snd_als4000_resume,
1016 #endif
1019 static int __init alsa_card_als4000_init(void)
1021 return pci_register_driver(&driver);
1024 static void __exit alsa_card_als4000_exit(void)
1026 pci_unregister_driver(&driver);
1029 module_init(alsa_card_als4000_init)
1030 module_exit(alsa_card_als4000_exit)