source: GPL/trunk/alsa-kernel/pci/als4000.c@ 679

Last change on this file since 679 was 679, checked in by David Azarewicz, 4 years ago

Merge changes from Paul's uniaud32next branch.

File size: 30.6 KB
Line 
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * card-als4000.c - driver for Avance Logic ALS4000 based soundcards.
4 * Copyright (C) 2000 by Bart Hartgers <bart@etpmod.phys.tue.nl>,
5 * Jaroslav Kysela <perex@perex.cz>
6 * Copyright (C) 2002, 2008 by Andreas Mohr <hw7oshyuv3001@sneakemail.com>
7 *
8 * Framework borrowed from Massimo Piccioni's card-als100.c.
9 *
10 * NOTES
11 *
12 * Since Avance does not provide any meaningful documentation, and I
13 * bought an ALS4000 based soundcard, I was forced to base this driver
14 * on reverse engineering.
15 *
16 * Note: this is no longer true (thank you!):
17 * pretty verbose chip docu (ALS4000a.PDF) can be found on the ALSA web site.
18 * Page numbers stated anywhere below with the "SPECS_PAGE:" tag
19 * refer to: ALS4000a.PDF specs Ver 1.0, May 28th, 1998.
20 *
21 * The ALS4000 seems to be the PCI-cousin of the ALS100. It contains an
22 * ALS100-like SB DSP/mixer, an OPL3 synth, a MPU401 and a gameport
23 * interface. These subsystems can be mapped into ISA io-port space,
24 * using the PCI-interface. In addition, the PCI-bit provides DMA and IRQ
25 * services to the subsystems.
26 *
27 * While ALS4000 is very similar to a SoundBlaster, the differences in
28 * DMA and capturing require more changes to the SoundBlaster than
29 * desirable, so I made this separate driver.
30 *
31 * The ALS4000 can do real full duplex playback/capture.
32 *
33 * FMDAC:
34 * - 0x4f -> port 0x14
35 * - port 0x15 |= 1
36 *
37 * Enable/disable 3D sound:
38 * - 0x50 -> port 0x14
39 * - change bit 6 (0x40) of port 0x15
40 *
41 * Set QSound:
42 * - 0xdb -> port 0x14
43 * - set port 0x15:
44 * 0x3e (mode 3), 0x3c (mode 2), 0x3a (mode 1), 0x38 (mode 0)
45 *
46 * Set KSound:
47 * - value -> some port 0x0c0d
48 *
49 * ToDo:
50 * - by default, don't enable legacy game and use PCI game I/O
51 * - power management? (card can do voice wakeup according to datasheet!!)
52 */
53
54#include <linux/io.h>
55#include <linux/init.h>
56#include <linux/pci.h>
57#include <linux/gameport.h>
58#include <linux/module.h>
59#include <linux/dma-mapping.h>
60#include <sound/core.h>
61#include <sound/pcm.h>
62#include <sound/rawmidi.h>
63#include <sound/mpu401.h>
64#include <sound/opl3.h>
65#include <sound/sb.h>
66#include <sound/initval.h>
67
68#ifdef TARGET_OS2
69#define KBUILD_MODNAME "als4000"
70#endif
71MODULE_AUTHOR("Bart Hartgers <bart@etpmod.phys.tue.nl>, Andreas Mohr");
72MODULE_DESCRIPTION("Avance Logic ALS4000");
73MODULE_LICENSE("GPL");
74MODULE_SUPPORTED_DEVICE("{{Avance Logic,ALS4000}}");
75
76#if IS_REACHABLE(CONFIG_GAMEPORT)
77#define SUPPORT_JOYSTICK 1
78#endif
79
80static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; /* Index 0-MAX */
81static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; /* ID for this card */
82static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; /* Enable this card */
83#ifdef SUPPORT_JOYSTICK
84static int joystick_port[SNDRV_CARDS];
85#endif
86
87module_param_array(index, int, NULL, 0444);
88MODULE_PARM_DESC(index, "Index value for ALS4000 soundcard.");
89module_param_array(id, charp, NULL, 0444);
90MODULE_PARM_DESC(id, "ID string for ALS4000 soundcard.");
91module_param_array(enable, bool, NULL, 0444);
92MODULE_PARM_DESC(enable, "Enable ALS4000 soundcard.");
93#ifdef SUPPORT_JOYSTICK
94module_param_hw_array(joystick_port, int, ioport, NULL, 0444);
95MODULE_PARM_DESC(joystick_port, "Joystick port address for ALS4000 soundcard. (0 = disabled)");
96#endif
97
98struct snd_card_als4000 {
99 /* most frequent access first */
100 unsigned long iobase;
101 struct pci_dev *pci;
102 struct snd_sb *chip;
103#ifdef SUPPORT_JOYSTICK
104 struct gameport *gameport;
105#endif
106};
107
108static const struct pci_device_id snd_als4000_ids[] = {
109 { 0x4005, 0x4000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0, }, /* ALS4000 */
110 { 0, }
111};
112
113MODULE_DEVICE_TABLE(pci, snd_als4000_ids);
114
115enum als4k_iobase_t {
116 /* IOx: B == Byte, W = Word, D = DWord; SPECS_PAGE: 37 */
117 ALS4K_IOD_00_AC97_ACCESS = 0x00,
118 ALS4K_IOW_04_AC97_READ = 0x04,
119 ALS4K_IOB_06_AC97_STATUS = 0x06,
120 ALS4K_IOB_07_IRQSTATUS = 0x07,
121 ALS4K_IOD_08_GCR_DATA = 0x08,
122 ALS4K_IOB_0C_GCR_INDEX = 0x0c,
123 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU = 0x0e,
124 ALS4K_IOB_10_ADLIB_ADDR0 = 0x10,
125 ALS4K_IOB_11_ADLIB_ADDR1 = 0x11,
126 ALS4K_IOB_12_ADLIB_ADDR2 = 0x12,
127 ALS4K_IOB_13_ADLIB_ADDR3 = 0x13,
128 ALS4K_IOB_14_MIXER_INDEX = 0x14,
129 ALS4K_IOB_15_MIXER_DATA = 0x15,
130 ALS4K_IOB_16_ESP_RESET = 0x16,
131 ALS4K_IOB_16_ACK_FOR_CR1E = 0x16, /* 2nd function */
132 ALS4K_IOB_18_OPL_ADDR0 = 0x18,
133 ALS4K_IOB_19_OPL_ADDR1 = 0x19,
134 ALS4K_IOB_1A_ESP_RD_DATA = 0x1a,
135 ALS4K_IOB_1C_ESP_CMD_DATA = 0x1c,
136 ALS4K_IOB_1C_ESP_WR_STATUS = 0x1c, /* 2nd function */
137 ALS4K_IOB_1E_ESP_RD_STATUS8 = 0x1e,
138 ALS4K_IOB_1F_ESP_RD_STATUS16 = 0x1f,
139 ALS4K_IOB_20_ESP_GAMEPORT_200 = 0x20,
140 ALS4K_IOB_21_ESP_GAMEPORT_201 = 0x21,
141 ALS4K_IOB_30_MIDI_DATA = 0x30,
142 ALS4K_IOB_31_MIDI_STATUS = 0x31,
143 ALS4K_IOB_31_MIDI_COMMAND = 0x31, /* 2nd function */
144};
145
146enum als4k_iobase_0e_t {
147 ALS4K_IOB_0E_MPU_IRQ = 0x10,
148 ALS4K_IOB_0E_CR1E_IRQ = 0x40,
149 ALS4K_IOB_0E_SB_DMA_IRQ = 0x80,
150};
151
152enum als4k_gcr_t { /* all registers 32bit wide; SPECS_PAGE: 38 to 42 */
153 ALS4K_GCR8C_MISC_CTRL = 0x8c,
154 ALS4K_GCR90_TEST_MODE_REG = 0x90,
155 ALS4K_GCR91_DMA0_ADDR = 0x91,
156 ALS4K_GCR92_DMA0_MODE_COUNT = 0x92,
157 ALS4K_GCR93_DMA1_ADDR = 0x93,
158 ALS4K_GCR94_DMA1_MODE_COUNT = 0x94,
159 ALS4K_GCR95_DMA3_ADDR = 0x95,
160 ALS4K_GCR96_DMA3_MODE_COUNT = 0x96,
161 ALS4K_GCR99_DMA_EMULATION_CTRL = 0x99,
162 ALS4K_GCRA0_FIFO1_CURRENT_ADDR = 0xa0,
163 ALS4K_GCRA1_FIFO1_STATUS_BYTECOUNT = 0xa1,
164 ALS4K_GCRA2_FIFO2_PCIADDR = 0xa2,
165 ALS4K_GCRA3_FIFO2_COUNT = 0xa3,
166 ALS4K_GCRA4_FIFO2_CURRENT_ADDR = 0xa4,
167 ALS4K_GCRA5_FIFO1_STATUS_BYTECOUNT = 0xa5,
168 ALS4K_GCRA6_PM_CTRL = 0xa6,
169 ALS4K_GCRA7_PCI_ACCESS_STORAGE = 0xa7,
170 ALS4K_GCRA8_LEGACY_CFG1 = 0xa8,
171 ALS4K_GCRA9_LEGACY_CFG2 = 0xa9,
172 ALS4K_GCRFF_DUMMY_SCRATCH = 0xff,
173};
174
175enum als4k_gcr8c_t {
176 ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE = 0x8000,
177 ALS4K_GCR8C_CHIP_REV_MASK = 0xf0000
178};
179
180static inline void snd_als4k_iobase_writeb(unsigned long iobase,
181 enum als4k_iobase_t reg,
182 u8 val)
183{
184 outb(val, iobase + reg);
185}
186
187static inline void snd_als4k_iobase_writel(unsigned long iobase,
188 enum als4k_iobase_t reg,
189 u32 val)
190{
191 outl(val, iobase + reg);
192}
193
194static inline u8 snd_als4k_iobase_readb(unsigned long iobase,
195 enum als4k_iobase_t reg)
196{
197 return inb(iobase + reg);
198}
199
200static inline u32 snd_als4k_iobase_readl(unsigned long iobase,
201 enum als4k_iobase_t reg)
202{
203 return inl(iobase + reg);
204}
205
206static inline void snd_als4k_gcr_write_addr(unsigned long iobase,
207 enum als4k_gcr_t reg,
208 u32 val)
209{
210 snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
211 snd_als4k_iobase_writel(iobase, ALS4K_IOD_08_GCR_DATA, val);
212}
213
214static inline void snd_als4k_gcr_write(struct snd_sb *sb,
215 enum als4k_gcr_t reg,
216 u32 val)
217{
218 snd_als4k_gcr_write_addr(sb->alt_port, reg, val);
219}
220
221static inline u32 snd_als4k_gcr_read_addr(unsigned long iobase,
222 enum als4k_gcr_t reg)
223{
224 /* SPECS_PAGE: 37/38 */
225 snd_als4k_iobase_writeb(iobase, ALS4K_IOB_0C_GCR_INDEX, reg);
226 return snd_als4k_iobase_readl(iobase, ALS4K_IOD_08_GCR_DATA);
227}
228
229static inline u32 snd_als4k_gcr_read(struct snd_sb *sb, enum als4k_gcr_t reg)
230{
231 return snd_als4k_gcr_read_addr(sb->alt_port, reg);
232}
233
234enum als4k_cr_t { /* all registers 8bit wide; SPECS_PAGE: 20 to 23 */
235 ALS4K_CR0_SB_CONFIG = 0x00,
236 ALS4K_CR2_MISC_CONTROL = 0x02,
237 ALS4K_CR3_CONFIGURATION = 0x03,
238 ALS4K_CR17_FIFO_STATUS = 0x17,
239 ALS4K_CR18_ESP_MAJOR_VERSION = 0x18,
240 ALS4K_CR19_ESP_MINOR_VERSION = 0x19,
241 ALS4K_CR1A_MPU401_UART_MODE_CONTROL = 0x1a,
242 ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO = 0x1c,
243 ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI = 0x1d,
244 ALS4K_CR1E_FIFO2_CONTROL = 0x1e, /* secondary PCM FIFO (recording) */
245 ALS4K_CR3A_MISC_CONTROL = 0x3a,
246 ALS4K_CR3B_CRC32_BYTE0 = 0x3b, /* for testing, activate via CR3A */
247 ALS4K_CR3C_CRC32_BYTE1 = 0x3c,
248 ALS4K_CR3D_CRC32_BYTE2 = 0x3d,
249 ALS4K_CR3E_CRC32_BYTE3 = 0x3e,
250};
251
252enum als4k_cr0_t {
253 ALS4K_CR0_DMA_CONTIN_MODE_CTRL = 0x02, /* IRQ/FIFO controlled for 0/1 */
254 ALS4K_CR0_DMA_90H_MODE_CTRL = 0x04, /* IRQ/FIFO controlled for 0/1 */
255 ALS4K_CR0_MX80_81_REG_WRITE_ENABLE = 0x80,
256};
257
258static inline void snd_als4_cr_write(struct snd_sb *chip,
259 enum als4k_cr_t reg,
260 u8 data)
261{
262 /* Control Register is reg | 0xc0 (bit 7, 6 set) on sbmixer_index
263 * NOTE: assumes chip->mixer_lock to be locked externally already!
264 * SPECS_PAGE: 6 */
265 snd_sbmixer_write(chip, reg | 0xc0, data);
266}
267
268static inline u8 snd_als4_cr_read(struct snd_sb *chip,
269 enum als4k_cr_t reg)
270{
271 /* NOTE: assumes chip->mixer_lock to be locked externally already! */
272 return snd_sbmixer_read(chip, reg | 0xc0);
273}
274
275
276
277static void snd_als4000_set_rate(struct snd_sb *chip, unsigned int rate)
278{
279 if (!(chip->mode & SB_RATE_LOCK)) {
280 snd_sbdsp_command(chip, SB_DSP_SAMPLE_RATE_OUT);
281 snd_sbdsp_command(chip, rate>>8);
282 snd_sbdsp_command(chip, rate);
283 }
284}
285
286static inline void snd_als4000_set_capture_dma(struct snd_sb *chip,
287 dma_addr_t addr, unsigned size)
288{
289 /* SPECS_PAGE: 40 */
290 snd_als4k_gcr_write(chip, ALS4K_GCRA2_FIFO2_PCIADDR, addr);
291 snd_als4k_gcr_write(chip, ALS4K_GCRA3_FIFO2_COUNT, (size-1));
292}
293
294static inline void snd_als4000_set_playback_dma(struct snd_sb *chip,
295 dma_addr_t addr,
296 unsigned size)
297{
298 /* SPECS_PAGE: 38 */
299 snd_als4k_gcr_write(chip, ALS4K_GCR91_DMA0_ADDR, addr);
300 snd_als4k_gcr_write(chip, ALS4K_GCR92_DMA0_MODE_COUNT,
301 (size-1)|0x180000);
302}
303
304#define ALS4000_FORMAT_SIGNED (1<<0)
305#define ALS4000_FORMAT_16BIT (1<<1)
306#define ALS4000_FORMAT_STEREO (1<<2)
307
308static int snd_als4000_get_format(struct snd_pcm_runtime *runtime)
309{
310 int result;
311
312 result = 0;
313 if (snd_pcm_format_signed(runtime->format))
314 result |= ALS4000_FORMAT_SIGNED;
315 if (snd_pcm_format_physical_width(runtime->format) == 16)
316 result |= ALS4000_FORMAT_16BIT;
317 if (runtime->channels > 1)
318 result |= ALS4000_FORMAT_STEREO;
319 return result;
320}
321
322/* structure for setting up playback */
323static const struct {
324 unsigned char dsp_cmd, dma_on, dma_off, format;
325} playback_cmd_vals[]={
326/* ALS4000_FORMAT_U8_MONO */
327{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_MONO },
328/* ALS4000_FORMAT_S8_MONO */
329{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_MONO },
330/* ALS4000_FORMAT_U16L_MONO */
331{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_MONO },
332/* ALS4000_FORMAT_S16L_MONO */
333{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_MONO },
334/* ALS4000_FORMAT_U8_STEREO */
335{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_UNS_STEREO },
336/* ALS4000_FORMAT_S8_STEREO */
337{ SB_DSP4_OUT8_AI, SB_DSP_DMA8_ON, SB_DSP_DMA8_OFF, SB_DSP4_MODE_SIGN_STEREO },
338/* ALS4000_FORMAT_U16L_STEREO */
339{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_UNS_STEREO },
340/* ALS4000_FORMAT_S16L_STEREO */
341{ SB_DSP4_OUT16_AI, SB_DSP_DMA16_ON, SB_DSP_DMA16_OFF, SB_DSP4_MODE_SIGN_STEREO },
342};
343#define playback_cmd(chip) (playback_cmd_vals[(chip)->playback_format])
344
345/* structure for setting up capture */
346enum { CMD_WIDTH8=0x04, CMD_SIGNED=0x10, CMD_MONO=0x80, CMD_STEREO=0xA0 };
347static const unsigned char capture_cmd_vals[]=
348{
349CMD_WIDTH8|CMD_MONO, /* ALS4000_FORMAT_U8_MONO */
350CMD_WIDTH8|CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S8_MONO */
351CMD_MONO, /* ALS4000_FORMAT_U16L_MONO */
352CMD_SIGNED|CMD_MONO, /* ALS4000_FORMAT_S16L_MONO */
353CMD_WIDTH8|CMD_STEREO, /* ALS4000_FORMAT_U8_STEREO */
354CMD_WIDTH8|CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S8_STEREO */
355CMD_STEREO, /* ALS4000_FORMAT_U16L_STEREO */
356CMD_SIGNED|CMD_STEREO, /* ALS4000_FORMAT_S16L_STEREO */
357};
358#define capture_cmd(chip) (capture_cmd_vals[(chip)->capture_format])
359
360static int snd_als4000_capture_prepare(struct snd_pcm_substream *substream)
361{
362 struct snd_sb *chip = snd_pcm_substream_chip(substream);
363 struct snd_pcm_runtime *runtime = substream->runtime;
364 unsigned long size;
365 unsigned count;
366
367 chip->capture_format = snd_als4000_get_format(runtime);
368
369 size = snd_pcm_lib_buffer_bytes(substream);
370 count = snd_pcm_lib_period_bytes(substream);
371
372 if (chip->capture_format & ALS4000_FORMAT_16BIT)
373 count >>= 1;
374 count--;
375
376 spin_lock_irq(&chip->reg_lock);
377 snd_als4000_set_rate(chip, runtime->rate);
378 snd_als4000_set_capture_dma(chip, runtime->dma_addr, size);
379 spin_unlock_irq(&chip->reg_lock);
380 spin_lock_irq(&chip->mixer_lock);
381 snd_als4_cr_write(chip, ALS4K_CR1C_FIFO2_BLOCK_LENGTH_LO, count & 0xff);
382 snd_als4_cr_write(chip, ALS4K_CR1D_FIFO2_BLOCK_LENGTH_HI, count >> 8);
383 spin_unlock_irq(&chip->mixer_lock);
384 return 0;
385}
386
387static int snd_als4000_playback_prepare(struct snd_pcm_substream *substream)
388{
389 struct snd_sb *chip = snd_pcm_substream_chip(substream);
390 struct snd_pcm_runtime *runtime = substream->runtime;
391 unsigned long size;
392 unsigned count;
393
394 chip->playback_format = snd_als4000_get_format(runtime);
395
396 size = snd_pcm_lib_buffer_bytes(substream);
397 count = snd_pcm_lib_period_bytes(substream);
398
399 if (chip->playback_format & ALS4000_FORMAT_16BIT)
400 count >>= 1;
401 count--;
402
403 /* FIXME: from second playback on, there's a lot more clicks and pops
404 * involved here than on first playback. Fiddling with
405 * tons of different settings didn't help (DMA, speaker on/off,
406 * reordering, ...). Something seems to get enabled on playback
407 * that I haven't found out how to disable again, which then causes
408 * the switching pops to reach the speakers the next time here. */
409 spin_lock_irq(&chip->reg_lock);
410 snd_als4000_set_rate(chip, runtime->rate);
411 snd_als4000_set_playback_dma(chip, runtime->dma_addr, size);
412
413 /* SPEAKER_ON not needed, since dma_on seems to also enable speaker */
414 /* snd_sbdsp_command(chip, SB_DSP_SPEAKER_ON); */
415 snd_sbdsp_command(chip, playback_cmd(chip).dsp_cmd);
416 snd_sbdsp_command(chip, playback_cmd(chip).format);
417 snd_sbdsp_command(chip, count & 0xff);
418 snd_sbdsp_command(chip, count >> 8);
419 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
420 spin_unlock_irq(&chip->reg_lock);
421
422 return 0;
423}
424
425static int snd_als4000_capture_trigger(struct snd_pcm_substream *substream, int cmd)
426{
427 struct snd_sb *chip = snd_pcm_substream_chip(substream);
428 int result = 0;
429
430 /* FIXME race condition in here!!!
431 chip->mode non-atomic update gets consistently protected
432 by reg_lock always, _except_ for this place!!
433 Probably need to take reg_lock as outer (or inner??) lock, too.
434 (or serialize both lock operations? probably not, though... - racy?)
435 */
436 spin_lock(&chip->mixer_lock);
437 switch (cmd) {
438 case SNDRV_PCM_TRIGGER_START:
439 case SNDRV_PCM_TRIGGER_RESUME:
440 chip->mode |= SB_RATE_LOCK_CAPTURE;
441 snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
442 capture_cmd(chip));
443 break;
444 case SNDRV_PCM_TRIGGER_STOP:
445 case SNDRV_PCM_TRIGGER_SUSPEND:
446 chip->mode &= ~SB_RATE_LOCK_CAPTURE;
447 snd_als4_cr_write(chip, ALS4K_CR1E_FIFO2_CONTROL,
448 capture_cmd(chip));
449 break;
450 default:
451 result = -EINVAL;
452 break;
453 }
454 spin_unlock(&chip->mixer_lock);
455 return result;
456}
457
458static int snd_als4000_playback_trigger(struct snd_pcm_substream *substream, int cmd)
459{
460 struct snd_sb *chip = snd_pcm_substream_chip(substream);
461 int result = 0;
462
463 spin_lock(&chip->reg_lock);
464 switch (cmd) {
465 case SNDRV_PCM_TRIGGER_START:
466 case SNDRV_PCM_TRIGGER_RESUME:
467 chip->mode |= SB_RATE_LOCK_PLAYBACK;
468 snd_sbdsp_command(chip, playback_cmd(chip).dma_on);
469 break;
470 case SNDRV_PCM_TRIGGER_STOP:
471 case SNDRV_PCM_TRIGGER_SUSPEND:
472 snd_sbdsp_command(chip, playback_cmd(chip).dma_off);
473 chip->mode &= ~SB_RATE_LOCK_PLAYBACK;
474 break;
475 default:
476 result = -EINVAL;
477 break;
478 }
479 spin_unlock(&chip->reg_lock);
480 return result;
481}
482
483static snd_pcm_uframes_t snd_als4000_capture_pointer(struct snd_pcm_substream *substream)
484{
485 struct snd_sb *chip = snd_pcm_substream_chip(substream);
486 unsigned int result;
487
488 spin_lock(&chip->reg_lock);
489 result = snd_als4k_gcr_read(chip, ALS4K_GCRA4_FIFO2_CURRENT_ADDR);
490 spin_unlock(&chip->reg_lock);
491 result &= 0xffff;
492 return bytes_to_frames( substream->runtime, result );
493}
494
495static snd_pcm_uframes_t snd_als4000_playback_pointer(struct snd_pcm_substream *substream)
496{
497 struct snd_sb *chip = snd_pcm_substream_chip(substream);
498 unsigned result;
499
500 spin_lock(&chip->reg_lock);
501 result = snd_als4k_gcr_read(chip, ALS4K_GCRA0_FIFO1_CURRENT_ADDR);
502 spin_unlock(&chip->reg_lock);
503 result &= 0xffff;
504 return bytes_to_frames( substream->runtime, result );
505}
506
507/* FIXME: this IRQ routine doesn't really support IRQ sharing (we always
508 * return IRQ_HANDLED no matter whether we actually had an IRQ flag or not).
509 * ALS4000a.PDF writes that while ACKing IRQ in PCI block will *not* ACK
510 * the IRQ in the SB core, ACKing IRQ in SB block *will* ACK the PCI IRQ
511 * register (alt_port + ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU). Probably something
512 * could be optimized here to query/write one register only...
513 * And even if both registers need to be queried, then there's still the
514 * question of whether it's actually correct to ACK PCI IRQ before reading
515 * SB IRQ like we do now, since ALS4000a.PDF mentions that PCI IRQ will *clear*
516 * SB IRQ status.
517 * (hmm, SPECS_PAGE: 38 mentions it the other way around!)
518 * And do we *really* need the lock here for *reading* SB_DSP4_IRQSTATUS??
519 * */
520static irqreturn_t snd_als4000_interrupt(int irq, void *dev_id)
521{
522 struct snd_sb *chip = dev_id;
523 unsigned pci_irqstatus;
524 unsigned sb_irqstatus;
525
526 /* find out which bit of the ALS4000 PCI block produced the interrupt,
527 SPECS_PAGE: 38, 5 */
528 pci_irqstatus = snd_als4k_iobase_readb(chip->alt_port,
529 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU);
530 if ((pci_irqstatus & ALS4K_IOB_0E_SB_DMA_IRQ)
531 && (chip->playback_substream)) /* playback */
532 snd_pcm_period_elapsed(chip->playback_substream);
533 if ((pci_irqstatus & ALS4K_IOB_0E_CR1E_IRQ)
534 && (chip->capture_substream)) /* capturing */
535 snd_pcm_period_elapsed(chip->capture_substream);
536 if ((pci_irqstatus & ALS4K_IOB_0E_MPU_IRQ)
537 && (chip->rmidi)) /* MPU401 interrupt */
538 snd_mpu401_uart_interrupt(irq, chip->rmidi->private_data);
539 /* ACK the PCI block IRQ */
540 snd_als4k_iobase_writeb(chip->alt_port,
541 ALS4K_IOB_0E_IRQTYPE_SB_CR1E_MPU, pci_irqstatus);
542
543 spin_lock(&chip->mixer_lock);
544 /* SPECS_PAGE: 20 */
545 sb_irqstatus = snd_sbmixer_read(chip, SB_DSP4_IRQSTATUS);
546 spin_unlock(&chip->mixer_lock);
547
548 if (sb_irqstatus & SB_IRQTYPE_8BIT)
549 snd_sb_ack_8bit(chip);
550 if (sb_irqstatus & SB_IRQTYPE_16BIT)
551 snd_sb_ack_16bit(chip);
552 if (sb_irqstatus & SB_IRQTYPE_MPUIN)
553 inb(chip->mpu_port);
554 if (sb_irqstatus & ALS4K_IRQTYPE_CR1E_DMA)
555 snd_als4k_iobase_readb(chip->alt_port,
556 ALS4K_IOB_16_ACK_FOR_CR1E);
557
558 /* dev_dbg(chip->card->dev, "als4000: irq 0x%04x 0x%04x\n",
559 pci_irqstatus, sb_irqstatus); */
560
561 /* only ack the things we actually handled above */
562 return IRQ_RETVAL(
563 (pci_irqstatus & (ALS4K_IOB_0E_SB_DMA_IRQ|ALS4K_IOB_0E_CR1E_IRQ|
564 ALS4K_IOB_0E_MPU_IRQ))
565 || (sb_irqstatus & (SB_IRQTYPE_8BIT|SB_IRQTYPE_16BIT|
566 SB_IRQTYPE_MPUIN|ALS4K_IRQTYPE_CR1E_DMA))
567 );
568}
569
570/*****************************************************************/
571
572static const struct snd_pcm_hardware snd_als4000_playback =
573{
574 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
575 SNDRV_PCM_INFO_MMAP_VALID),
576 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
577 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
578 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
579 .rate_min = 4000,
580 .rate_max = 48000,
581 .channels_min = 1,
582 .channels_max = 2,
583 .buffer_bytes_max = 65536,
584 .period_bytes_min = 64,
585 .period_bytes_max = 65536,
586 .periods_min = 1,
587 .periods_max = 1024,
588 .fifo_size = 0
589};
590
591static const struct snd_pcm_hardware snd_als4000_capture =
592{
593 .info = (SNDRV_PCM_INFO_MMAP | SNDRV_PCM_INFO_INTERLEAVED |
594 SNDRV_PCM_INFO_MMAP_VALID),
595 .formats = SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_U8 |
596 SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_U16_LE, /* formats */
597 .rates = SNDRV_PCM_RATE_CONTINUOUS | SNDRV_PCM_RATE_8000_48000,
598 .rate_min = 4000,
599 .rate_max = 48000,
600 .channels_min = 1,
601 .channels_max = 2,
602 .buffer_bytes_max = 65536,
603 .period_bytes_min = 64,
604 .period_bytes_max = 65536,
605 .periods_min = 1,
606 .periods_max = 1024,
607 .fifo_size = 0
608};
609
610/*****************************************************************/
611
612static int snd_als4000_playback_open(struct snd_pcm_substream *substream)
613{
614 struct snd_sb *chip = snd_pcm_substream_chip(substream);
615 struct snd_pcm_runtime *runtime = substream->runtime;
616
617 chip->playback_substream = substream;
618 runtime->hw = snd_als4000_playback;
619 return 0;
620}
621
622static int snd_als4000_playback_close(struct snd_pcm_substream *substream)
623{
624 struct snd_sb *chip = snd_pcm_substream_chip(substream);
625
626 chip->playback_substream = NULL;
627 return 0;
628}
629
630static int snd_als4000_capture_open(struct snd_pcm_substream *substream)
631{
632 struct snd_sb *chip = snd_pcm_substream_chip(substream);
633 struct snd_pcm_runtime *runtime = substream->runtime;
634
635 chip->capture_substream = substream;
636 runtime->hw = snd_als4000_capture;
637 return 0;
638}
639
640static int snd_als4000_capture_close(struct snd_pcm_substream *substream)
641{
642 struct snd_sb *chip = snd_pcm_substream_chip(substream);
643
644 chip->capture_substream = NULL;
645 return 0;
646}
647
648/******************************************************************/
649
650static const struct snd_pcm_ops snd_als4000_playback_ops = {
651 .open = snd_als4000_playback_open,
652 .close = snd_als4000_playback_close,
653 .prepare = snd_als4000_playback_prepare,
654 .trigger = snd_als4000_playback_trigger,
655 .pointer = snd_als4000_playback_pointer
656};
657
658static const struct snd_pcm_ops snd_als4000_capture_ops = {
659 .open = snd_als4000_capture_open,
660 .close = snd_als4000_capture_close,
661 .prepare = snd_als4000_capture_prepare,
662 .trigger = snd_als4000_capture_trigger,
663 .pointer = snd_als4000_capture_pointer
664};
665
666static int snd_als4000_pcm(struct snd_sb *chip, int device)
667{
668 struct snd_pcm *pcm;
669 int err;
670
671 err = snd_pcm_new(chip->card, "ALS4000 DSP", device, 1, 1, &pcm);
672 if (err < 0)
673 return err;
674 pcm->private_data = chip;
675 pcm->info_flags = SNDRV_PCM_INFO_JOINT_DUPLEX;
676 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &snd_als4000_playback_ops);
677 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_CAPTURE, &snd_als4000_capture_ops);
678
679 snd_pcm_set_managed_buffer_all(pcm, SNDRV_DMA_TYPE_DEV,
680 &chip->pci->dev, 64*1024, 64*1024);
681
682 chip->pcm = pcm;
683
684 return 0;
685}
686
687/******************************************************************/
688
689static void snd_als4000_set_addr(unsigned long iobase,
690 unsigned int sb_io,
691 unsigned int mpu_io,
692 unsigned int opl_io,
693 unsigned int game_io)
694{
695 u32 cfg1 = 0;
696 u32 cfg2 = 0;
697
698 if (mpu_io > 0)
699 cfg2 |= (mpu_io | 1) << 16;
700 if (sb_io > 0)
701 cfg2 |= (sb_io | 1);
702 if (game_io > 0)
703 cfg1 |= (game_io | 1) << 16;
704 if (opl_io > 0)
705 cfg1 |= (opl_io | 1);
706 snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA8_LEGACY_CFG1, cfg1);
707 snd_als4k_gcr_write_addr(iobase, ALS4K_GCRA9_LEGACY_CFG2, cfg2);
708}
709
710static void snd_als4000_configure(struct snd_sb *chip)
711{
712 u8 tmp;
713 int i;
714
715 /* do some more configuration */
716 spin_lock_irq(&chip->mixer_lock);
717 tmp = snd_als4_cr_read(chip, ALS4K_CR0_SB_CONFIG);
718 snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
719 tmp|ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
720 /* always select DMA channel 0, since we do not actually use DMA
721 * SPECS_PAGE: 19/20 */
722 snd_sbmixer_write(chip, SB_DSP4_DMASETUP, SB_DMASETUP_DMA0);
723 snd_als4_cr_write(chip, ALS4K_CR0_SB_CONFIG,
724 tmp & ~ALS4K_CR0_MX80_81_REG_WRITE_ENABLE);
725 spin_unlock_irq(&chip->mixer_lock);
726
727 spin_lock_irq(&chip->reg_lock);
728 /* enable interrupts */
729 snd_als4k_gcr_write(chip, ALS4K_GCR8C_MISC_CTRL,
730 ALS4K_GCR8C_IRQ_MASK_CTRL_ENABLE);
731
732 /* SPECS_PAGE: 39 */
733 for (i = ALS4K_GCR91_DMA0_ADDR; i <= ALS4K_GCR96_DMA3_MODE_COUNT; ++i)
734 snd_als4k_gcr_write(chip, i, 0);
735 /* enable burst mode to prevent dropouts during high PCI bus usage */
736 snd_als4k_gcr_write(chip, ALS4K_GCR99_DMA_EMULATION_CTRL,
737 (snd_als4k_gcr_read(chip, ALS4K_GCR99_DMA_EMULATION_CTRL) & ~0x07) | 0x04);
738 spin_unlock_irq(&chip->reg_lock);
739}
740
741#ifdef SUPPORT_JOYSTICK
742static int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev)
743{
744 struct gameport *gp;
745 struct resource *r;
746 int io_port;
747
748 if (joystick_port[dev] == 0)
749 return -ENODEV;
750
751 if (joystick_port[dev] == 1) { /* auto-detect */
752 for (io_port = 0x200; io_port <= 0x218; io_port += 8) {
753 r = request_region(io_port, 8, "ALS4000 gameport");
754 if (r)
755 break;
756 }
757 } else {
758 io_port = joystick_port[dev];
759 r = request_region(io_port, 8, "ALS4000 gameport");
760 }
761
762 if (!r) {
763 dev_warn(&acard->pci->dev, "cannot reserve joystick ports\n");
764 return -EBUSY;
765 }
766
767 acard->gameport = gp = gameport_allocate_port();
768 if (!gp) {
769 dev_err(&acard->pci->dev, "cannot allocate memory for gameport\n");
770 release_and_free_resource(r);
771 return -ENOMEM;
772 }
773
774 gameport_set_name(gp, "ALS4000 Gameport");
775 gameport_set_phys(gp, "pci%s/gameport0", pci_name(acard->pci));
776 gameport_set_dev_parent(gp, &acard->pci->dev);
777 gp->io = io_port;
778 gameport_set_port_data(gp, r);
779
780 /* Enable legacy joystick port */
781 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
782
783 gameport_register_port(acard->gameport);
784
785 return 0;
786}
787
788static void snd_als4000_free_gameport(struct snd_card_als4000 *acard)
789{
790 if (acard->gameport) {
791 struct resource *r = gameport_get_port_data(acard->gameport);
792
793 gameport_unregister_port(acard->gameport);
794 acard->gameport = NULL;
795
796 /* disable joystick */
797 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
798
799 release_and_free_resource(r);
800 }
801}
802#else
803static inline int snd_als4000_create_gameport(struct snd_card_als4000 *acard, int dev) { return -ENOSYS; }
804static inline void snd_als4000_free_gameport(struct snd_card_als4000 *acard) { }
805#endif
806
807static void snd_card_als4000_free( struct snd_card *card )
808{
809 struct snd_card_als4000 *acard = card->private_data;
810
811 /* make sure that interrupts are disabled */
812 snd_als4k_gcr_write_addr(acard->iobase, ALS4K_GCR8C_MISC_CTRL, 0);
813 /* free resources */
814 snd_als4000_free_gameport(acard);
815 pci_release_regions(acard->pci);
816 pci_disable_device(acard->pci);
817}
818
819static int snd_card_als4000_probe(struct pci_dev *pci,
820 const struct pci_device_id *pci_id)
821{
822 static int dev;
823 struct snd_card *card;
824 struct snd_card_als4000 *acard;
825 unsigned long iobase;
826 struct snd_sb *chip;
827 struct snd_opl3 *opl3;
828 unsigned short word;
829 int err;
830
831 if (dev >= SNDRV_CARDS)
832 return -ENODEV;
833 if (!enable[dev]) {
834 dev++;
835 return -ENOENT;
836 }
837
838 /* enable PCI device */
839 if ((err = pci_enable_device(pci)) < 0) {
840 return err;
841 }
842 /* check, if we can restrict PCI DMA transfers to 24 bits */
843 if (dma_set_mask(&pci->dev, DMA_BIT_MASK(24)) < 0 ||
844 dma_set_coherent_mask(&pci->dev, DMA_BIT_MASK(24)) < 0) {
845 dev_err(&pci->dev, "architecture does not support 24bit PCI busmaster DMA\n");
846 pci_disable_device(pci);
847 return -ENXIO;
848 }
849
850 if ((err = pci_request_regions(pci, "ALS4000")) < 0) {
851 pci_disable_device(pci);
852 return err;
853 }
854 iobase = pci_resource_start(pci, 0);
855
856 pci_read_config_word(pci, PCI_COMMAND, &word);
857 pci_write_config_word(pci, PCI_COMMAND, word | PCI_COMMAND_IO);
858 pci_set_master(pci);
859
860 err = snd_card_new(&pci->dev, index[dev], id[dev], THIS_MODULE,
861 sizeof(*acard) /* private_data: acard */,
862 &card);
863 if (err < 0) {
864 pci_release_regions(pci);
865 pci_disable_device(pci);
866 return err;
867 }
868
869 acard = card->private_data;
870 acard->pci = pci;
871 acard->iobase = iobase;
872 card->private_free = snd_card_als4000_free;
873
874 /* disable all legacy ISA stuff */
875 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 0);
876
877 if ((err = snd_sbdsp_create(card,
878 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
879 pci->irq,
880 /* internally registered as IRQF_SHARED in case of ALS4000 SB */
881 snd_als4000_interrupt,
882 -1,
883 -1,
884 SB_HW_ALS4000,
885 &chip)) < 0) {
886 goto out_err;
887 }
888 acard->chip = chip;
889
890 chip->pci = pci;
891 chip->alt_port = iobase;
892
893 snd_als4000_configure(chip);
894
895 strcpy(card->driver, "ALS4000");
896 strcpy(card->shortname, "Avance Logic ALS4000");
897 sprintf(card->longname, "%s at 0x%lx, irq %i",
898 card->shortname, chip->alt_port, chip->irq);
899
900 if ((err = snd_mpu401_uart_new( card, 0, MPU401_HW_ALS4000,
901 iobase + ALS4K_IOB_30_MIDI_DATA,
902 MPU401_INFO_INTEGRATED |
903 MPU401_INFO_IRQ_HOOK,
904 -1, &chip->rmidi)) < 0) {
905 dev_err(&pci->dev, "no MPU-401 device at 0x%lx?\n",
906 iobase + ALS4K_IOB_30_MIDI_DATA);
907 goto out_err;
908 }
909 /* FIXME: ALS4000 has interesting MPU401 configuration features
910 * at ALS4K_CR1A_MPU401_UART_MODE_CONTROL
911 * (pass-thru / UART switching, fast MIDI clock, etc.),
912 * however there doesn't seem to be an ALSA API for this...
913 * SPECS_PAGE: 21 */
914
915 if ((err = snd_als4000_pcm(chip, 0)) < 0) {
916 goto out_err;
917 }
918 if ((err = snd_sbmixer_new(chip)) < 0) {
919 goto out_err;
920 }
921
922 if (snd_opl3_create(card,
923 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
924 iobase + ALS4K_IOB_12_ADLIB_ADDR2,
925 OPL3_HW_AUTO, 1, &opl3) < 0) {
926 dev_err(&pci->dev, "no OPL device at 0x%lx-0x%lx?\n",
927 iobase + ALS4K_IOB_10_ADLIB_ADDR0,
928 iobase + ALS4K_IOB_12_ADLIB_ADDR2);
929 } else {
930 if ((err = snd_opl3_hwdep_new(opl3, 0, 1, NULL)) < 0) {
931 goto out_err;
932 }
933 }
934
935 snd_als4000_create_gameport(acard, dev);
936
937 if ((err = snd_card_register(card)) < 0) {
938 goto out_err;
939 }
940 pci_set_drvdata(pci, card);
941 dev++;
942 err = 0;
943 goto out;
944
945out_err:
946 snd_card_free(card);
947
948out:
949 return err;
950}
951
952static void snd_card_als4000_remove(struct pci_dev *pci)
953{
954 snd_card_free(pci_get_drvdata(pci));
955}
956
957#ifdef CONFIG_PM_SLEEP
958static int snd_als4000_suspend(struct device *dev)
959{
960 struct snd_card *card = dev_get_drvdata(dev);
961 struct snd_card_als4000 *acard = card->private_data;
962 struct snd_sb *chip = acard->chip;
963
964 snd_power_change_state(card, SNDRV_CTL_POWER_D3hot);
965
966 snd_sbmixer_suspend(chip);
967 return 0;
968}
969
970static int snd_als4000_resume(struct device *dev)
971{
972 struct snd_card *card = dev_get_drvdata(dev);
973 struct snd_card_als4000 *acard = card->private_data;
974 struct snd_sb *chip = acard->chip;
975
976 snd_als4000_configure(chip);
977 snd_sbdsp_reset(chip);
978 snd_sbmixer_resume(chip);
979
980#ifdef SUPPORT_JOYSTICK
981 if (acard->gameport)
982 snd_als4000_set_addr(acard->iobase, 0, 0, 0, 1);
983#endif
984
985 snd_power_change_state(card, SNDRV_CTL_POWER_D0);
986 return 0;
987}
988
989static SIMPLE_DEV_PM_OPS(snd_als4000_pm, snd_als4000_suspend, snd_als4000_resume);
990#define SND_ALS4000_PM_OPS &snd_als4000_pm
991#else
992#define SND_ALS4000_PM_OPS NULL
993#endif /* CONFIG_PM_SLEEP */
994
995static struct pci_driver als4000_driver = {
996 .name = KBUILD_MODNAME,
997 .id_table = snd_als4000_ids,
998 .probe = snd_card_als4000_probe,
999 .remove = snd_card_als4000_remove,
1000 .driver = {
1001 .pm = SND_ALS4000_PM_OPS,
1002 },
1003};
1004
1005module_pci_driver(als4000_driver);
Note: See TracBrowser for help on using the repository browser.