linux/sound/isa/gus/gus_main.c

484 lines
13 KiB
C
Raw Normal View History

/*
* Routines for Gravis UltraSound soundcards
* Copyright (c) by Jaroslav Kysela <perex@perex.cz>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <sound/core.h>
#include <sound/gus.h>
#include <sound/control.h>
#include <asm/dma.h>
MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
MODULE_DESCRIPTION("Routines for Gravis UltraSound soundcards");
MODULE_LICENSE("GPL");
static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches);
int snd_gus_use_inc(struct snd_gus_card * gus)
{
if (!try_module_get(gus->card->module))
return 0;
return 1;
}
void snd_gus_use_dec(struct snd_gus_card * gus)
{
module_put(gus->card->module);
}
static int snd_gus_joystick_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = 31;
return 0;
}
static int snd_gus_joystick_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = gus->joystick_dac & 31;
return 0;
}
static int snd_gus_joystick_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
{
struct snd_gus_card *gus = snd_kcontrol_chip(kcontrol);
unsigned long flags;
int change;
unsigned char nval;
nval = ucontrol->value.integer.value[0] & 31;
spin_lock_irqsave(&gus->reg_lock, flags);
change = gus->joystick_dac != nval;
gus->joystick_dac = nval;
snd_gf1_write8(gus, SNDRV_GF1_GB_JOYSTICK_DAC_LEVEL, gus->joystick_dac);
spin_unlock_irqrestore(&gus->reg_lock, flags);
return change;
}
static struct snd_kcontrol_new snd_gus_joystick_control = {
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.name = "Joystick Speed",
.info = snd_gus_joystick_info,
.get = snd_gus_joystick_get,
.put = snd_gus_joystick_put
};
static void snd_gus_init_control(struct snd_gus_card *gus)
{
if (!gus->ace_flag)
snd_ctl_add(gus->card, snd_ctl_new1(&snd_gus_joystick_control, gus));
}
/*
*
*/
static int snd_gus_free(struct snd_gus_card *gus)
{
if (gus->gf1.res_port2 == NULL)
goto __hw_end;
snd_gf1_stop(gus);
snd_gus_init_dma_irq(gus, 0);
__hw_end:
release_and_free_resource(gus->gf1.res_port1);
release_and_free_resource(gus->gf1.res_port2);
if (gus->gf1.irq >= 0)
free_irq(gus->gf1.irq, (void *) gus);
if (gus->gf1.dma1 >= 0) {
disable_dma(gus->gf1.dma1);
free_dma(gus->gf1.dma1);
}
if (!gus->equal_dma && gus->gf1.dma2 >= 0) {
disable_dma(gus->gf1.dma2);
free_dma(gus->gf1.dma2);
}
kfree(gus);
return 0;
}
static int snd_gus_dev_free(struct snd_device *device)
{
struct snd_gus_card *gus = device->device_data;
return snd_gus_free(gus);
}
int snd_gus_create(struct snd_card *card,
unsigned long port,
int irq, int dma1, int dma2,
int timer_dev,
int voices,
int pcm_channels,
int effect,
struct snd_gus_card **rgus)
{
struct snd_gus_card *gus;
int err;
static struct snd_device_ops ops = {
.dev_free = snd_gus_dev_free,
};
*rgus = NULL;
gus = kzalloc(sizeof(*gus), GFP_KERNEL);
if (gus == NULL)
return -ENOMEM;
[ALSA] fix bootup crash in snd_gus_interrupt() when simulating a storm of fake GUS interrupts (without actually owning this venerable piece of ISA hardware) the driver falls over (crashes) in two ways: 1) spinlocks being initialized too late: INFO: trying to register non-static key. the code is fine but needs lockdep annotation. turning off the locking correctness validator. [<401058ca>] show_trace_log_lvl+0x1a/0x30 [<401064b2>] show_trace+0x12/0x20 [<401064d6>] dump_stack+0x16/0x20 [<4014a72b>] __lock_acquire+0xcfb/0x1030 [<4014aac0>] lock_acquire+0x60/0x80 [<40721a68>] _spin_lock_irqsave+0x38/0x50 [<4058fc12>] snd_gf1_i_look8+0x22/0x60 [<405906fe>] snd_gus_interrupt+0x13e/0x270 [<401548e8>] handle_IRQ_event+0x28/0x60 [<40155cc1>] handle_fasteoi_irq+0x71/0xe0 [<40107238>] do_IRQ+0x48/0xa0 [<401051fe>] common_interrupt+0x2e/0x40 [<40156822>] register_handler_proc+0x92/0xf0 [<401550c2>] setup_irq+0xe2/0x190 [<40155224>] request_irq+0xb4/0xd0 [<4058f524>] snd_gus_create+0x124/0x3c0 [<40aa4087>] snd_gusclassic_probe+0x2a7/0x4b0 [<403f5eff>] isa_bus_probe+0x1f/0x30 [<403f1944>] driver_probe_device+0x84/0x190 [<403f1a58>] __device_attach+0x8/0x10 [<403f0e63>] bus_for_each_drv+0x53/0x80 [<403f1b1b>] device_attach+0x8b/0x90 [<403f0dd8>] bus_attach_device+0x48/0x80 [<403efdbd>] device_add+0x45d/0x5a0 [<403eff12>] device_register+0x12/0x20 [<403f60c3>] isa_register_driver+0xb3/0x140 [<40aa3dd2>] alsa_card_gusclassic_init+0x12/0x20 [<40a665c3>] kernel_init+0x133/0x310 [<401054a7>] kernel_thread_helper+0x7/0x10 ======================= 2) callback functions not being filled in yet: BUG: unable to handle kernel NULL pointer dereference at virtual address 00000000 printing eip: 00000000 *pde = 00000000 Oops: 0000 [#1] SMP DEBUG_PAGEALLOC CPU: 0 EIP: 0060:[<00000000>] Not tainted VLI EFLAGS: 00010002 (2.6.23 #37) EIP is at 0x0 eax: 7fe94000 ebx: 7fe94000 ecx: 00000000 edx: 00000226 esi: 00000000 edi: 00000005 ebp: 7ff87c28 esp: 7ff87bf4 ds: 007b es: 007b fs: 00d8 gs: 0000 ss: 0068 Process swapper (pid: 1, ti=7ff86000 task=7ff84000 task.ti=7ff86000) Stack: 40590683 408424a9 408db87c 00000029 40787406 00000064 00000046 ff000000 000000ff 00000001 7faefaf0 00000000 00000005 7ff87c40 401548e8 00000000 40a52000 7faefaf0 00000005 7ff87c58 40155cc1 40a52030 00000005 00000000 Call Trace: [<401058ca>] show_trace_log_lvl+0x1a/0x30 [<4010598b>] show_stack_log_lvl+0xab/0xd0 [<40105b7c>] show_registers+0x1cc/0x2d0 [<40105d96>] die+0x116/0x240 [<4011d7bb>] do_page_fault+0x18b/0x670 [<40721d22>] error_code+0x72/0x80 [<401548e8>] handle_IRQ_event+0x28/0x60 [<40155cc1>] handle_fasteoi_irq+0x71/0xe0 [<40107238>] do_IRQ+0x48/0xa0 [<401051fe>] common_interrupt+0x2e/0x40 [<401a344e>] proc_create+0x3e/0x120 [<401a3733>] proc_mkdir_mode+0x23/0x50 [<401a376f>] proc_mkdir+0xf/0x20 [<40156864>] register_handler_proc+0xd4/0xf0 [<401550c2>] setup_irq+0xe2/0x190 [<40155224>] request_irq+0xb4/0xd0 [<4058f524>] snd_gus_create+0x124/0x3c0 [<40aa4087>] snd_gusclassic_probe+0x2a7/0x4b0 [<403f5eff>] isa_bus_probe+0x1f/0x30 [<403f1944>] driver_probe_device+0x84/0x190 [<403f1a58>] __device_attach+0x8/0x10 [<403f0e63>] bus_for_each_drv+0x53/0x80 [<403f1b1b>] device_attach+0x8b/0x90 [<403f0dd8>] bus_attach_device+0x48/0x80 [<403efdbd>] device_add+0x45d/0x5a0 [<403eff12>] device_register+0x12/0x20 [<403f60c3>] isa_register_driver+0xb3/0x140 [<40aa3dd2>] alsa_card_gusclassic_init+0x12/0x20 [<40a665c3>] kernel_init+0x133/0x310 [<401054a7>] kernel_thread_helper+0x7/0x10 ======================= Code: Bad EIP value. EIP: [<00000000>] 0x0 SS:ESP 0068:7ff87bf4 Kernel panic - not syncing: Fatal exception in interrupt with these things fixed, i get the expected 'no such hardware' result from the driver initialization: Calling initcall 0x40aa3dc0: alsa_card_gusclassic_init+0x0/0x20() ALSA sound/isa/gus/gusclassic.c:136: [0x220] check 1 failed - 0xff initcall 0x40aa3dc0: alsa_card_gusclassic_init+0x0/0x20() returned 0. initcall 0x40aa3dc0 ran for 133 msecs: alsa_card_gusclassic_init+0x0/0x20() Signed-off-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Takashi Iwai <tiwai@suse.de> Signed-off-by: Jaroslav Kysela <perex@perex.cz>
2007-10-16 12:42:30 +00:00
spin_lock_init(&gus->reg_lock);
spin_lock_init(&gus->voice_alloc);
spin_lock_init(&gus->active_voice_lock);
spin_lock_init(&gus->event_lock);
spin_lock_init(&gus->dma_lock);
spin_lock_init(&gus->pcm_volume_level_lock);
spin_lock_init(&gus->uart_cmd_lock);
mutex_init(&gus->dma_mutex);
gus->gf1.irq = -1;
gus->gf1.dma1 = -1;
gus->gf1.dma2 = -1;
gus->card = card;
gus->gf1.port = port;
/* fill register variables for speedup */
gus->gf1.reg_page = GUSP(gus, GF1PAGE);
gus->gf1.reg_regsel = GUSP(gus, GF1REGSEL);
gus->gf1.reg_data8 = GUSP(gus, GF1DATAHIGH);
gus->gf1.reg_data16 = GUSP(gus, GF1DATALOW);
gus->gf1.reg_irqstat = GUSP(gus, IRQSTAT);
gus->gf1.reg_dram = GUSP(gus, DRAM);
gus->gf1.reg_timerctrl = GUSP(gus, TIMERCNTRL);
gus->gf1.reg_timerdata = GUSP(gus, TIMERDATA);
/* allocate resources */
if ((gus->gf1.res_port1 = request_region(port, 16, "GUS GF1 (Adlib/SB)")) == NULL) {
snd_printk(KERN_ERR "gus: can't grab SB port 0x%lx\n", port);
snd_gus_free(gus);
return -EBUSY;
}
if ((gus->gf1.res_port2 = request_region(port + 0x100, 12, "GUS GF1 (Synth)")) == NULL) {
snd_printk(KERN_ERR "gus: can't grab synth port 0x%lx\n", port + 0x100);
snd_gus_free(gus);
return -EBUSY;
}
if (irq >= 0 && request_irq(irq, snd_gus_interrupt, 0, "GUS GF1", (void *) gus)) {
snd_printk(KERN_ERR "gus: can't grab irq %d\n", irq);
snd_gus_free(gus);
return -EBUSY;
}
gus->gf1.irq = irq;
if (request_dma(dma1, "GUS - 1")) {
snd_printk(KERN_ERR "gus: can't grab DMA1 %d\n", dma1);
snd_gus_free(gus);
return -EBUSY;
}
gus->gf1.dma1 = dma1;
if (dma2 >= 0 && dma1 != dma2) {
if (request_dma(dma2, "GUS - 2")) {
snd_printk(KERN_ERR "gus: can't grab DMA2 %d\n", dma2);
snd_gus_free(gus);
return -EBUSY;
}
gus->gf1.dma2 = dma2;
} else {
gus->gf1.dma2 = gus->gf1.dma1;
gus->equal_dma = 1;
}
gus->timer_dev = timer_dev;
if (voices < 14)
voices = 14;
if (voices > 32)
voices = 32;
if (pcm_channels < 0)
pcm_channels = 0;
if (pcm_channels > 8)
pcm_channels = 8;
pcm_channels++;
pcm_channels &= ~1;
gus->gf1.effect = effect ? 1 : 0;
gus->gf1.active_voices = voices;
gus->gf1.pcm_channels = pcm_channels;
gus->gf1.volume_ramp = 25;
gus->gf1.smooth_pan = 1;
if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, gus, &ops)) < 0) {
snd_gus_free(gus);
return err;
}
*rgus = gus;
return 0;
}
/*
* Memory detection routine for plain GF1 soundcards
*/
static int snd_gus_detect_memory(struct snd_gus_card * gus)
{
int l, idx, local;
unsigned char d;
snd_gf1_poke(gus, 0L, 0xaa);
snd_gf1_poke(gus, 1L, 0x55);
if (snd_gf1_peek(gus, 0L) != 0xaa || snd_gf1_peek(gus, 1L) != 0x55) {
snd_printk(KERN_ERR "plain GF1 card at 0x%lx without onboard DRAM?\n", gus->gf1.port);
return -ENOMEM;
}
for (idx = 1, d = 0xab; idx < 4; idx++, d++) {
local = idx << 18;
snd_gf1_poke(gus, local, d);
snd_gf1_poke(gus, local + 1, d + 1);
if (snd_gf1_peek(gus, local) != d ||
snd_gf1_peek(gus, local + 1) != d + 1 ||
snd_gf1_peek(gus, 0L) != 0xaa)
break;
}
#if 1
gus->gf1.memory = idx << 18;
#else
gus->gf1.memory = 256 * 1024;
#endif
for (l = 0, local = gus->gf1.memory; l < 4; l++, local -= 256 * 1024) {
gus->gf1.mem_alloc.banks_8[l].address =
gus->gf1.mem_alloc.banks_8[l].size = 0;
gus->gf1.mem_alloc.banks_16[l].address = l << 18;
gus->gf1.mem_alloc.banks_16[l].size = local > 0 ? 256 * 1024 : 0;
}
gus->gf1.mem_alloc.banks_8[0].size = gus->gf1.memory;
return 0; /* some memory were detected */
}
static int snd_gus_init_dma_irq(struct snd_gus_card * gus, int latches)
{
struct snd_card *card;
unsigned long flags;
int irq, dma1, dma2;
static unsigned char irqs[16] =
{0, 0, 1, 3, 0, 2, 0, 4, 0, 1, 0, 5, 6, 0, 0, 7};
static unsigned char dmas[8] =
{6, 1, 0, 2, 0, 3, 4, 5};
if (snd_BUG_ON(!gus))
return -EINVAL;
card = gus->card;
if (snd_BUG_ON(!card))
return -EINVAL;
gus->mix_cntrl_reg &= 0xf8;
gus->mix_cntrl_reg |= 0x01; /* disable MIC, LINE IN, enable LINE OUT */
if (gus->codec_flag || gus->ess_flag) {
gus->mix_cntrl_reg &= ~1; /* enable LINE IN */
gus->mix_cntrl_reg |= 4; /* enable MIC */
}
dma1 = gus->gf1.dma1;
dma1 = abs(dma1);
dma1 = dmas[dma1 & 7];
dma2 = gus->gf1.dma2;
dma2 = abs(dma2);
dma2 = dmas[dma2 & 7];
dma1 |= gus->equal_dma ? 0x40 : (dma2 << 3);
if ((dma1 & 7) == 0 || (dma2 & 7) == 0) {
snd_printk(KERN_ERR "Error! DMA isn't defined.\n");
return -EINVAL;
}
irq = gus->gf1.irq;
irq = abs(irq);
irq = irqs[irq & 0x0f];
if (irq == 0) {
snd_printk(KERN_ERR "Error! IRQ isn't defined.\n");
return -EINVAL;
}
irq |= 0x40;
#if 0
card->mixer.mix_ctrl_reg |= 0x10;
#endif
spin_lock_irqsave(&gus->reg_lock, flags);
outb(5, GUSP(gus, REGCNTRLS));
outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
outb(0x00, GUSP(gus, IRQDMACNTRLREG));
outb(0, GUSP(gus, REGCNTRLS));
spin_unlock_irqrestore(&gus->reg_lock, flags);
udelay(100);
spin_lock_irqsave(&gus->reg_lock, flags);
outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
outb(dma1, GUSP(gus, IRQDMACNTRLREG));
if (latches) {
outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
outb(irq, GUSP(gus, IRQDMACNTRLREG));
}
spin_unlock_irqrestore(&gus->reg_lock, flags);
udelay(100);
spin_lock_irqsave(&gus->reg_lock, flags);
outb(0x00 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
outb(dma1, GUSP(gus, IRQDMACNTRLREG));
if (latches) {
outb(0x40 | gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
outb(irq, GUSP(gus, IRQDMACNTRLREG));
}
spin_unlock_irqrestore(&gus->reg_lock, flags);
snd_gf1_delay(gus);
if (latches)
gus->mix_cntrl_reg |= 0x08; /* enable latches */
else
gus->mix_cntrl_reg &= ~0x08; /* disable latches */
spin_lock_irqsave(&gus->reg_lock, flags);
outb(gus->mix_cntrl_reg, GUSP(gus, MIXCNTRLREG));
outb(0, GUSP(gus, GF1PAGE));
spin_unlock_irqrestore(&gus->reg_lock, flags);
return 0;
}
static int snd_gus_check_version(struct snd_gus_card * gus)
{
unsigned long flags;
unsigned char val, rev;
struct snd_card *card;
card = gus->card;
spin_lock_irqsave(&gus->reg_lock, flags);
outb(0x20, GUSP(gus, REGCNTRLS));
val = inb(GUSP(gus, REGCNTRLS));
rev = inb(GUSP(gus, BOARDVERSION));
spin_unlock_irqrestore(&gus->reg_lock, flags);
snd_printdd("GF1 [0x%lx] init - val = 0x%x, rev = 0x%x\n", gus->gf1.port, val, rev);
strcpy(card->driver, "GUS");
strcpy(card->longname, "Gravis UltraSound Classic (2.4)");
if ((val != 255 && (val & 0x06)) || (rev >= 5 && rev != 255)) {
if (rev >= 5 && rev <= 9) {
gus->ics_flag = 1;
if (rev == 5)
gus->ics_flipped = 1;
card->longname[27] = '3';
card->longname[29] = rev == 5 ? '5' : '7';
}
if (rev >= 10 && rev != 255) {
if (rev >= 10 && rev <= 11) {
strcpy(card->driver, "GUS MAX");
strcpy(card->longname, "Gravis UltraSound MAX");
gus->max_flag = 1;
} else if (rev == 0x30) {
strcpy(card->driver, "GUS ACE");
strcpy(card->longname, "Gravis UltraSound Ace");
gus->ace_flag = 1;
} else if (rev == 0x50) {
strcpy(card->driver, "GUS Extreme");
strcpy(card->longname, "Gravis UltraSound Extreme");
gus->ess_flag = 1;
} else {
snd_printk(KERN_ERR "unknown GF1 revision number at 0x%lx - 0x%x (0x%x)\n", gus->gf1.port, rev, val);
snd_printk(KERN_ERR " please - report to <perex@perex.cz>\n");
}
}
}
strcpy(card->shortname, card->longname);
gus->uart_enable = 1; /* standard GUSes doesn't have midi uart trouble */
snd_gus_init_control(gus);
return 0;
}
int snd_gus_initialize(struct snd_gus_card *gus)
{
int err;
if (!gus->interwave) {
if ((err = snd_gus_check_version(gus)) < 0) {
snd_printk(KERN_ERR "version check failed\n");
return err;
}
if ((err = snd_gus_detect_memory(gus)) < 0)
return err;
}
if ((err = snd_gus_init_dma_irq(gus, 1)) < 0)
return err;
snd_gf1_start(gus);
gus->initialized = 1;
return 0;
}
/* gus_io.c */
EXPORT_SYMBOL(snd_gf1_delay);
EXPORT_SYMBOL(snd_gf1_write8);
EXPORT_SYMBOL(snd_gf1_look8);
EXPORT_SYMBOL(snd_gf1_write16);
EXPORT_SYMBOL(snd_gf1_look16);
EXPORT_SYMBOL(snd_gf1_i_write8);
EXPORT_SYMBOL(snd_gf1_i_look8);
EXPORT_SYMBOL(snd_gf1_i_look16);
EXPORT_SYMBOL(snd_gf1_dram_addr);
EXPORT_SYMBOL(snd_gf1_write_addr);
EXPORT_SYMBOL(snd_gf1_poke);
EXPORT_SYMBOL(snd_gf1_peek);
/* gus_reset.c */
EXPORT_SYMBOL(snd_gf1_alloc_voice);
EXPORT_SYMBOL(snd_gf1_free_voice);
EXPORT_SYMBOL(snd_gf1_ctrl_stop);
EXPORT_SYMBOL(snd_gf1_stop_voice);
/* gus_mixer.c */
EXPORT_SYMBOL(snd_gf1_new_mixer);
/* gus_pcm.c */
EXPORT_SYMBOL(snd_gf1_pcm_new);
/* gus.c */
EXPORT_SYMBOL(snd_gus_use_inc);
EXPORT_SYMBOL(snd_gus_use_dec);
EXPORT_SYMBOL(snd_gus_create);
EXPORT_SYMBOL(snd_gus_initialize);
/* gus_irq.c */
EXPORT_SYMBOL(snd_gus_interrupt);
/* gus_uart.c */
EXPORT_SYMBOL(snd_gf1_rawmidi_new);
/* gus_dram.c */
EXPORT_SYMBOL(snd_gus_dram_write);
EXPORT_SYMBOL(snd_gus_dram_read);
/* gus_volume.c */
EXPORT_SYMBOL(snd_gf1_lvol_to_gvol_raw);
EXPORT_SYMBOL(snd_gf1_translate_freq);
/* gus_mem.c */
EXPORT_SYMBOL(snd_gf1_mem_alloc);
EXPORT_SYMBOL(snd_gf1_mem_xfree);
EXPORT_SYMBOL(snd_gf1_mem_free);
EXPORT_SYMBOL(snd_gf1_mem_lock);
/*
* INIT part
*/
static int __init alsa_gus_init(void)
{
return 0;
}
static void __exit alsa_gus_exit(void)
{
}
module_init(alsa_gus_init)
module_exit(alsa_gus_exit)