summaryrefslogtreecommitdiff
path: root/sound/drivers/opl3
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 15:20:36 -0700
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 15:20:36 -0700
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /sound/drivers/opl3
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'sound/drivers/opl3')
-rw-r--r--sound/drivers/opl3/Makefile22
-rw-r--r--sound/drivers/opl3/opl3_drums.c223
-rw-r--r--sound/drivers/opl3/opl3_lib.c558
-rw-r--r--sound/drivers/opl3/opl3_midi.c873
-rw-r--r--sound/drivers/opl3/opl3_oss.c356
-rw-r--r--sound/drivers/opl3/opl3_seq.c314
-rw-r--r--sound/drivers/opl3/opl3_synth.c447
-rw-r--r--sound/drivers/opl3/opl3_voice.h52
8 files changed, 2845 insertions, 0 deletions
diff --git a/sound/drivers/opl3/Makefile b/sound/drivers/opl3/Makefile
new file mode 100644
index 000000000000..12059785b5cb
--- /dev/null
+++ b/sound/drivers/opl3/Makefile
@@ -0,0 +1,22 @@
+#
+# Makefile for ALSA
+# Copyright (c) 2001 by Jaroslav Kysela <perex@suse.cz>
+#
+
+snd-opl3-lib-objs := opl3_lib.o opl3_synth.o
+snd-opl3-synth-objs := opl3_seq.o opl3_midi.o opl3_drums.o
+ifeq ($(CONFIG_SND_SEQUENCER_OSS),y)
+snd-opl3-synth-objs += opl3_oss.o
+endif
+
+#
+# this function returns:
+# "m" - CONFIG_SND_SEQUENCER is m
+# <empty string> - CONFIG_SND_SEQUENCER is undefined
+# otherwise parameter #1 value
+#
+sequencer = $(if $(subst y,,$(CONFIG_SND_SEQUENCER)),$(if $(1),m),$(if $(CONFIG_SND_SEQUENCER),$(1)))
+
+obj-$(CONFIG_SND_OPL3_LIB) += snd-opl3-lib.o
+obj-$(CONFIG_SND_OPL4_LIB) += snd-opl3-lib.o
+obj-$(call sequencer,$(CONFIG_SND_OPL3_LIB)) += snd-opl3-synth.o
diff --git a/sound/drivers/opl3/opl3_drums.c b/sound/drivers/opl3/opl3_drums.c
new file mode 100644
index 000000000000..f26332680c19
--- /dev/null
+++ b/sound/drivers/opl3/opl3_drums.c
@@ -0,0 +1,223 @@
+/*
+ * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
+ *
+ * OPL2/OPL3/OPL4 FM routines for internal percussion channels
+ *
+ * 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 "opl3_voice.h"
+
+extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
+
+static char snd_opl3_drum_table[47] =
+{
+ OPL3_BASSDRUM_ON, OPL3_BASSDRUM_ON, OPL3_HIHAT_ON, /* 35 - 37 */
+ OPL3_SNAREDRUM_ON, OPL3_HIHAT_ON, OPL3_SNAREDRUM_ON, /* 38 - 40 */
+ OPL3_BASSDRUM_ON, OPL3_HIHAT_ON, OPL3_BASSDRUM_ON, /* 41 - 43 */
+ OPL3_HIHAT_ON, OPL3_TOMTOM_ON, OPL3_HIHAT_ON, /* 44 - 46 */
+ OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_CYMBAL_ON, /* 47 - 49 */
+
+ OPL3_TOMTOM_ON, OPL3_CYMBAL_ON, OPL3_CYMBAL_ON, /* 50 - 52 */
+ OPL3_CYMBAL_ON, OPL3_CYMBAL_ON, OPL3_CYMBAL_ON, /* 53 - 55 */
+ OPL3_HIHAT_ON, OPL3_CYMBAL_ON, OPL3_TOMTOM_ON, /* 56 - 58 */
+ OPL3_CYMBAL_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 59 - 61 */
+ OPL3_HIHAT_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 62 - 64 */
+
+ OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 65 - 67 */
+ OPL3_TOMTOM_ON, OPL3_HIHAT_ON, OPL3_HIHAT_ON, /* 68 - 70 */
+ OPL3_HIHAT_ON, OPL3_HIHAT_ON, OPL3_TOMTOM_ON, /* 71 - 73 */
+ OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 74 - 76 */
+ OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, OPL3_TOMTOM_ON, /* 77 - 79 */
+ OPL3_CYMBAL_ON, OPL3_CYMBAL_ON /* 80 - 81 */
+};
+
+typedef struct snd_opl3_drum_voice {
+ int voice;
+ int op;
+ unsigned char am_vib;
+ unsigned char ksl_level;
+ unsigned char attack_decay;
+ unsigned char sustain_release;
+ unsigned char feedback_connection;
+ unsigned char wave_select;
+} snd_opl3_drum_voice_t;
+
+typedef struct snd_opl3_drum_note {
+ int voice;
+ unsigned char fnum;
+ unsigned char octave_f;
+ unsigned char feedback_connection;
+} snd_opl3_drum_note_t;
+
+static snd_opl3_drum_voice_t bass_op0 = {6, 0, 0x00, 0x32, 0xf8, 0x66, 0x30, 0x00};
+static snd_opl3_drum_voice_t bass_op1 = {6, 1, 0x00, 0x03, 0xf6, 0x57, 0x30, 0x00};
+static snd_opl3_drum_note_t bass_note = {6, 0x90, 0x09};
+
+static snd_opl3_drum_voice_t hihat = {7, 0, 0x00, 0x03, 0xf0, 0x06, 0x20, 0x00};
+
+static snd_opl3_drum_voice_t snare = {7, 1, 0x00, 0x03, 0xf0, 0x07, 0x20, 0x02};
+static snd_opl3_drum_note_t snare_note = {7, 0xf4, 0x0d};
+
+static snd_opl3_drum_voice_t tomtom = {8, 0, 0x02, 0x03, 0xf0, 0x06, 0x10, 0x00};
+static snd_opl3_drum_note_t tomtom_note = {8, 0xf4, 0x09};
+
+static snd_opl3_drum_voice_t cymbal = {8, 1, 0x04, 0x03, 0xf0, 0x06, 0x10, 0x00};
+
+/*
+ * set drum voice characteristics
+ */
+static void snd_opl3_drum_voice_set(opl3_t *opl3, snd_opl3_drum_voice_t *data)
+{
+ unsigned char op_offset = snd_opl3_regmap[data->voice][data->op];
+ unsigned char voice_offset = data->voice;
+ unsigned short opl3_reg;
+
+ /* Set OPL3 AM_VIB register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_AM_VIB + op_offset);
+ opl3->command(opl3, opl3_reg, data->am_vib);
+
+ /* Set OPL3 KSL_LEVEL register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset);
+ opl3->command(opl3, opl3_reg, data->ksl_level);
+
+ /* Set OPL3 ATTACK_DECAY register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_ATTACK_DECAY + op_offset);
+ opl3->command(opl3, opl3_reg, data->attack_decay);
+
+ /* Set OPL3 SUSTAIN_RELEASE register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
+ opl3->command(opl3, opl3_reg, data->sustain_release);
+
+ /* Set OPL3 FEEDBACK_CONNECTION register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
+ opl3->command(opl3, opl3_reg, data->feedback_connection);
+
+ /* Select waveform */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_WAVE_SELECT + op_offset);
+ opl3->command(opl3, opl3_reg, data->wave_select);
+}
+
+/*
+ * Set drum voice pitch
+ */
+static void snd_opl3_drum_note_set(opl3_t *opl3, snd_opl3_drum_note_t *data)
+{
+ unsigned char voice_offset = data->voice;
+ unsigned short opl3_reg;
+
+ /* Set OPL3 FNUM_LOW register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_FNUM_LOW + voice_offset);
+ opl3->command(opl3, opl3_reg, data->fnum);
+
+ /* Set OPL3 KEYON_BLOCK register */
+ opl3_reg = OPL3_LEFT | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ opl3->command(opl3, opl3_reg, data->octave_f);
+}
+
+/*
+ * Set drum voice volume and position
+ */
+static void snd_opl3_drum_vol_set(opl3_t *opl3, snd_opl3_drum_voice_t *data,
+ int vel, snd_midi_channel_t *chan)
+{
+ unsigned char op_offset = snd_opl3_regmap[data->voice][data->op];
+ unsigned char voice_offset = data->voice;
+ unsigned char reg_val;
+ unsigned short opl3_reg;
+
+ /* Set OPL3 KSL_LEVEL register */
+ reg_val = data->ksl_level;
+ snd_opl3_calc_volume(&reg_val, vel, chan);
+ opl3_reg = OPL3_LEFT | (OPL3_REG_KSL_LEVEL + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set OPL3 FEEDBACK_CONNECTION register */
+ /* Set output voice connection */
+ reg_val = data->feedback_connection | OPL3_STEREO_BITS;
+ if (chan->gm_pan < 43)
+ reg_val &= ~OPL3_VOICE_TO_RIGHT;
+ if (chan->gm_pan > 85)
+ reg_val &= ~OPL3_VOICE_TO_LEFT;
+ opl3_reg = OPL3_LEFT | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+}
+
+/*
+ * Loads drum voices at init time
+ */
+void snd_opl3_load_drums(opl3_t *opl3)
+{
+ snd_opl3_drum_voice_set(opl3, &bass_op0);
+ snd_opl3_drum_voice_set(opl3, &bass_op1);
+ snd_opl3_drum_note_set(opl3, &bass_note);
+
+ snd_opl3_drum_voice_set(opl3, &hihat);
+
+ snd_opl3_drum_voice_set(opl3, &snare);
+ snd_opl3_drum_note_set(opl3, &snare_note);
+
+ snd_opl3_drum_voice_set(opl3, &tomtom);
+ snd_opl3_drum_note_set(opl3, &tomtom_note);
+
+ snd_opl3_drum_voice_set(opl3, &cymbal);
+}
+
+/*
+ * Switch drum voice on or off
+ */
+void snd_opl3_drum_switch(opl3_t *opl3, int note, int vel, int on_off,
+ snd_midi_channel_t *chan)
+{
+ unsigned char drum_mask;
+ snd_opl3_drum_voice_t *drum_voice;
+
+ if (!(opl3->drum_reg & OPL3_PERCUSSION_ENABLE))
+ return;
+
+ if ((note < 35) || (note > 81))
+ return;
+ drum_mask = snd_opl3_drum_table[note - 35];
+
+ if (on_off) {
+ switch (drum_mask) {
+ case OPL3_BASSDRUM_ON:
+ drum_voice = &bass_op1;
+ break;
+ case OPL3_HIHAT_ON:
+ drum_voice = &hihat;
+ break;
+ case OPL3_SNAREDRUM_ON:
+ drum_voice = &snare;
+ break;
+ case OPL3_TOMTOM_ON:
+ drum_voice = &tomtom;
+ break;
+ case OPL3_CYMBAL_ON:
+ drum_voice = &cymbal;
+ break;
+ default:
+ drum_voice = &tomtom;
+ }
+
+ snd_opl3_drum_vol_set(opl3, drum_voice, vel, chan);
+ opl3->drum_reg |= drum_mask;
+ } else {
+ opl3->drum_reg &= ~drum_mask;
+ }
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
+ opl3->drum_reg);
+}
diff --git a/sound/drivers/opl3/opl3_lib.c b/sound/drivers/opl3/opl3_lib.c
new file mode 100644
index 000000000000..c313e5205cb8
--- /dev/null
+++ b/sound/drivers/opl3/opl3_lib.c
@@ -0,0 +1,558 @@
+/*
+ * Copyright (c) by Jaroslav Kysela <perex@suse.cz>,
+ * Hannu Savolainen 1993-1996,
+ * Rob Hooft
+ *
+ * Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)
+ *
+ * Most if code is ported from OSS/Lite.
+ *
+ * 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 <sound/opl3.h>
+#include <asm/io.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/ioport.h>
+#include <sound/minors.h>
+
+MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Hannu Savolainen 1993-1996, Rob Hooft");
+MODULE_DESCRIPTION("Routines for control of AdLib FM cards (OPL2/OPL3/OPL4 chips)");
+MODULE_LICENSE("GPL");
+
+extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
+
+static void snd_opl2_command(opl3_t * opl3, unsigned short cmd, unsigned char val)
+{
+ unsigned long flags;
+ unsigned long port;
+
+ /*
+ * The original 2-OP synth requires a quite long delay
+ * after writing to a register.
+ */
+
+ port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
+
+ spin_lock_irqsave(&opl3->reg_lock, flags);
+
+ outb((unsigned char) cmd, port);
+ udelay(10);
+
+ outb((unsigned char) val, port + 1);
+ udelay(30);
+
+ spin_unlock_irqrestore(&opl3->reg_lock, flags);
+}
+
+static void snd_opl3_command(opl3_t * opl3, unsigned short cmd, unsigned char val)
+{
+ unsigned long flags;
+ unsigned long port;
+
+ /*
+ * The OPL-3 survives with just two INBs
+ * after writing to a register.
+ */
+
+ port = (cmd & OPL3_RIGHT) ? opl3->r_port : opl3->l_port;
+
+ spin_lock_irqsave(&opl3->reg_lock, flags);
+
+ outb((unsigned char) cmd, port);
+ inb(opl3->l_port);
+ inb(opl3->l_port);
+
+ outb((unsigned char) val, port + 1);
+ inb(opl3->l_port);
+ inb(opl3->l_port);
+
+ spin_unlock_irqrestore(&opl3->reg_lock, flags);
+}
+
+static int snd_opl3_detect(opl3_t * opl3)
+{
+ /*
+ * This function returns 1 if the FM chip is present at the given I/O port
+ * The detection algorithm plays with the timer built in the FM chip and
+ * looks for a change in the status register.
+ *
+ * Note! The timers of the FM chip are not connected to AdLib (and compatible)
+ * boards.
+ *
+ * Note2! The chip is initialized if detected.
+ */
+
+ unsigned char stat1, stat2, signature;
+
+ /* Reset timers 1 and 2 */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
+ /* Reset the IRQ of the FM chip */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
+ signature = stat1 = inb(opl3->l_port); /* Status register */
+ if ((stat1 & 0xe0) != 0x00) { /* Should be 0x00 */
+ snd_printd("OPL3: stat1 = 0x%x\n", stat1);
+ return -ENODEV;
+ }
+ /* Set timer1 to 0xff */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 0xff);
+ /* Unmask and start timer 1 */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER2_MASK | OPL3_TIMER1_START);
+ /* Now we have to delay at least 80us */
+ udelay(200);
+ /* Read status after timers have expired */
+ stat2 = inb(opl3->l_port);
+ /* Stop the timers */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_TIMER1_MASK | OPL3_TIMER2_MASK);
+ /* Reset the IRQ of the FM chip */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, OPL3_IRQ_RESET);
+ if ((stat2 & 0xe0) != 0xc0) { /* There is no YM3812 */
+ snd_printd("OPL3: stat2 = 0x%x\n", stat2);
+ return -ENODEV;
+ }
+
+ /* If the toplevel code knows exactly the type of chip, don't try
+ to detect it. */
+ if (opl3->hardware != OPL3_HW_AUTO)
+ return 0;
+
+ /* There is a FM chip on this address. Detect the type (OPL2 to OPL4) */
+ if (signature == 0x06) { /* OPL2 */
+ opl3->hardware = OPL3_HW_OPL2;
+ } else {
+ /*
+ * If we had an OPL4 chip, opl3->hardware would have been set
+ * by the OPL4 driver; so we can assume OPL3 here.
+ */
+ snd_assert(opl3->r_port != 0, return -ENODEV);
+ opl3->hardware = OPL3_HW_OPL3;
+ }
+ return 0;
+}
+
+/*
+ * AdLib timers
+ */
+
+/*
+ * Timer 1 - 80us
+ */
+
+static int snd_opl3_timer1_start(snd_timer_t * timer)
+{
+ unsigned long flags;
+ unsigned char tmp;
+ unsigned int ticks;
+ opl3_t *opl3;
+
+ opl3 = snd_timer_chip(timer);
+ spin_lock_irqsave(&opl3->timer_lock, flags);
+ ticks = timer->sticks;
+ tmp = (opl3->timer_enable | OPL3_TIMER1_START) & ~OPL3_TIMER1_MASK;
+ opl3->timer_enable = tmp;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER1, 256 - ticks); /* timer 1 count */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
+ spin_unlock_irqrestore(&opl3->timer_lock, flags);
+ return 0;
+}
+
+static int snd_opl3_timer1_stop(snd_timer_t * timer)
+{
+ unsigned long flags;
+ unsigned char tmp;
+ opl3_t *opl3;
+
+ opl3 = snd_timer_chip(timer);
+ spin_lock_irqsave(&opl3->timer_lock, flags);
+ tmp = (opl3->timer_enable | OPL3_TIMER1_MASK) & ~OPL3_TIMER1_START;
+ opl3->timer_enable = tmp;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
+ spin_unlock_irqrestore(&opl3->timer_lock, flags);
+ return 0;
+}
+
+/*
+ * Timer 2 - 320us
+ */
+
+static int snd_opl3_timer2_start(snd_timer_t * timer)
+{
+ unsigned long flags;
+ unsigned char tmp;
+ unsigned int ticks;
+ opl3_t *opl3;
+
+ opl3 = snd_timer_chip(timer);
+ spin_lock_irqsave(&opl3->timer_lock, flags);
+ ticks = timer->sticks;
+ tmp = (opl3->timer_enable | OPL3_TIMER2_START) & ~OPL3_TIMER2_MASK;
+ opl3->timer_enable = tmp;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER2, 256 - ticks); /* timer 1 count */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* enable timer 1 IRQ */
+ spin_unlock_irqrestore(&opl3->timer_lock, flags);
+ return 0;
+}
+
+static int snd_opl3_timer2_stop(snd_timer_t * timer)
+{
+ unsigned long flags;
+ unsigned char tmp;
+ opl3_t *opl3;
+
+ opl3 = snd_timer_chip(timer);
+ spin_lock_irqsave(&opl3->timer_lock, flags);
+ tmp = (opl3->timer_enable | OPL3_TIMER2_MASK) & ~OPL3_TIMER2_START;
+ opl3->timer_enable = tmp;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TIMER_CONTROL, tmp); /* disable timer #1 */
+ spin_unlock_irqrestore(&opl3->timer_lock, flags);
+ return 0;
+}
+
+/*
+
+ */
+
+static struct _snd_timer_hardware snd_opl3_timer1 =
+{
+ .flags = SNDRV_TIMER_HW_STOP,
+ .resolution = 80000,
+ .ticks = 256,
+ .start = snd_opl3_timer1_start,
+ .stop = snd_opl3_timer1_stop,
+};
+
+static struct _snd_timer_hardware snd_opl3_timer2 =
+{
+ .flags = SNDRV_TIMER_HW_STOP,
+ .resolution = 320000,
+ .ticks = 256,
+ .start = snd_opl3_timer2_start,
+ .stop = snd_opl3_timer2_stop,
+};
+
+static int snd_opl3_timer1_init(opl3_t * opl3, int timer_no)
+{
+ snd_timer_t *timer = NULL;
+ snd_timer_id_t tid;
+ int err;
+
+ tid.dev_class = SNDRV_TIMER_CLASS_CARD;
+ tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
+ tid.card = opl3->card->number;
+ tid.device = timer_no;
+ tid.subdevice = 0;
+ if ((err = snd_timer_new(opl3->card, "AdLib timer #1", &tid, &timer)) >= 0) {
+ strcpy(timer->name, "AdLib timer #1");
+ timer->private_data = opl3;
+ timer->hw = snd_opl3_timer1;
+ }
+ opl3->timer1 = timer;
+ return err;
+}
+
+static int snd_opl3_timer2_init(opl3_t * opl3, int timer_no)
+{
+ snd_timer_t *timer = NULL;
+ snd_timer_id_t tid;
+ int err;
+
+ tid.dev_class = SNDRV_TIMER_CLASS_CARD;
+ tid.dev_sclass = SNDRV_TIMER_SCLASS_NONE;
+ tid.card = opl3->card->number;
+ tid.device = timer_no;
+ tid.subdevice = 0;
+ if ((err = snd_timer_new(opl3->card, "AdLib timer #2", &tid, &timer)) >= 0) {
+ strcpy(timer->name, "AdLib timer #2");
+ timer->private_data = opl3;
+ timer->hw = snd_opl3_timer2;
+ }
+ opl3->timer2 = timer;
+ return err;
+}
+
+/*
+
+ */
+
+void snd_opl3_interrupt(snd_hwdep_t * hw)
+{
+ unsigned char status;
+ opl3_t *opl3;
+ snd_timer_t *timer;
+
+ if (hw == NULL)
+ return;
+
+ opl3 = hw->private_data;
+ status = inb(opl3->l_port);
+#if 0
+ snd_printk("AdLib IRQ status = 0x%x\n", status);
+#endif
+ if (!(status & 0x80))
+ return;
+
+ if (status & 0x40) {
+ timer = opl3->timer1;
+ snd_timer_interrupt(timer, timer->sticks);
+ }
+ if (status & 0x20) {
+ timer = opl3->timer2;
+ snd_timer_interrupt(timer, timer->sticks);
+ }
+}
+
+/*
+
+ */
+
+static int snd_opl3_free(opl3_t *opl3)
+{
+ snd_assert(opl3 != NULL, return -ENXIO);
+ if (opl3->private_free)
+ opl3->private_free(opl3);
+ if (opl3->res_l_port) {
+ release_resource(opl3->res_l_port);
+ kfree_nocheck(opl3->res_l_port);
+ }
+ if (opl3->res_r_port) {
+ release_resource(opl3->res_r_port);
+ kfree_nocheck(opl3->res_r_port);
+ }
+ kfree(opl3);
+ return 0;
+}
+
+static int snd_opl3_dev_free(snd_device_t *device)
+{
+ opl3_t *opl3 = device->device_data;
+ return snd_opl3_free(opl3);
+}
+
+int snd_opl3_new(snd_card_t *card,
+ unsigned short hardware,
+ opl3_t **ropl3)
+{
+ static snd_device_ops_t ops = {
+ .dev_free = snd_opl3_dev_free,
+ };
+ opl3_t *opl3;
+ int err;
+
+ *ropl3 = NULL;
+ opl3 = kcalloc(1, sizeof(*opl3), GFP_KERNEL);
+ if (opl3 == NULL)
+ return -ENOMEM;
+
+ opl3->card = card;
+ opl3->hardware = hardware;
+ spin_lock_init(&opl3->reg_lock);
+ spin_lock_init(&opl3->timer_lock);
+ init_MUTEX(&opl3->access_mutex);
+
+ if ((err = snd_device_new(card, SNDRV_DEV_CODEC, opl3, &ops)) < 0) {
+ snd_opl3_free(opl3);
+ return err;
+ }
+
+ *ropl3 = opl3;
+ return 0;
+}
+
+int snd_opl3_init(opl3_t *opl3)
+{
+ if (! opl3->command) {
+ printk(KERN_ERR "snd_opl3_init: command not defined!\n");
+ return -EINVAL;
+ }
+
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
+ /* Melodic mode */
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00);
+
+ switch (opl3->hardware & OPL3_HW_MASK) {
+ case OPL3_HW_OPL2:
+ opl3->max_voices = MAX_OPL2_VOICES;
+ break;
+ case OPL3_HW_OPL3:
+ case OPL3_HW_OPL4:
+ opl3->max_voices = MAX_OPL3_VOICES;
+ /* Enter OPL3 mode */
+ opl3->command(opl3, OPL3_RIGHT | OPL3_REG_MODE, OPL3_OPL3_ENABLE);
+ }
+ return 0;
+}
+
+int snd_opl3_create(snd_card_t * card,
+ unsigned long l_port,
+ unsigned long r_port,
+ unsigned short hardware,
+ int integrated,
+ opl3_t ** ropl3)
+{
+ opl3_t *opl3;
+ int err;
+
+ *ropl3 = NULL;
+ if ((err = snd_opl3_new(card, hardware, &opl3)) < 0)
+ return err;
+ if (! integrated) {
+ if ((opl3->res_l_port = request_region(l_port, 2, "OPL2/3 (left)")) == NULL) {
+ snd_printk(KERN_ERR "opl3: can't grab left port 0x%lx\n", l_port);
+ snd_opl3_free(opl3);
+ return -EBUSY;
+ }
+ if (r_port != 0 &&
+ (opl3->res_r_port = request_region(r_port, 2, "OPL2/3 (right)")) == NULL) {
+ snd_printk(KERN_ERR "opl3: can't grab right port 0x%lx\n", r_port);
+ snd_opl3_free(opl3);
+ return -EBUSY;
+ }
+ }
+ opl3->l_port = l_port;
+ opl3->r_port = r_port;
+
+ switch (opl3->hardware) {
+ /* some hardware doesn't support timers */
+ case OPL3_HW_OPL3_SV:
+ case OPL3_HW_OPL3_CS:
+ case OPL3_HW_OPL3_FM801:
+ opl3->command = &snd_opl3_command;
+ break;
+ default:
+ opl3->command = &snd_opl2_command;
+ if ((err = snd_opl3_detect(opl3)) < 0) {
+ snd_printd("OPL2/3 chip not detected at 0x%lx/0x%lx\n",
+ opl3->l_port, opl3->r_port);
+ snd_opl3_free(opl3);
+ return err;
+ }
+ /* detect routine returns correct hardware type */
+ switch (opl3->hardware & OPL3_HW_MASK) {
+ case OPL3_HW_OPL3:
+ case OPL3_HW_OPL4:
+ opl3->command = &snd_opl3_command;
+ }
+ }
+
+ snd_opl3_init(opl3);
+
+ *ropl3 = opl3;
+ return 0;
+}
+
+int snd_opl3_timer_new(opl3_t * opl3, int timer1_dev, int timer2_dev)
+{
+ int err;
+
+ if (timer1_dev >= 0)
+ if ((err = snd_opl3_timer1_init(opl3, timer1_dev)) < 0)
+ return err;
+ if (timer2_dev >= 0) {
+ if ((err = snd_opl3_timer2_init(opl3, timer2_dev)) < 0) {
+ snd_device_free(opl3->card, opl3->timer1);
+ opl3->timer1 = NULL;
+ return err;
+ }
+ }
+ return 0;
+}
+
+int snd_opl3_hwdep_new(opl3_t * opl3,
+ int device, int seq_device,
+ snd_hwdep_t ** rhwdep)
+{
+ snd_hwdep_t *hw;
+ snd_card_t *card = opl3->card;
+ int err;
+
+ if (rhwdep)
+ *rhwdep = NULL;
+
+ /* create hardware dependent device (direct FM) */
+
+ if ((err = snd_hwdep_new(card, "OPL2/OPL3", device, &hw)) < 0) {
+ snd_device_free(card, opl3);
+ return err;
+ }
+ hw->private_data = opl3;
+#ifdef CONFIG_SND_OSSEMUL
+ if (device == 0) {
+ hw->oss_type = SNDRV_OSS_DEVICE_TYPE_DMFM;
+ sprintf(hw->oss_dev, "dmfm%i", card->number);
+ }
+#endif
+ strcpy(hw->name, hw->id);
+ switch (opl3->hardware & OPL3_HW_MASK) {
+ case OPL3_HW_OPL2:
+ strcpy(hw->name, "OPL2 FM");
+ hw->iface = SNDRV_HWDEP_IFACE_OPL2;
+ break;
+ case OPL3_HW_OPL3:
+ strcpy(hw->name, "OPL3 FM");
+ hw->iface = SNDRV_HWDEP_IFACE_OPL3;
+ break;
+ case OPL3_HW_OPL4:
+ strcpy(hw->name, "OPL4 FM");
+ hw->iface = SNDRV_HWDEP_IFACE_OPL4;
+ break;
+ }
+
+ /* operators - only ioctl */
+ hw->ops.open = snd_opl3_open;
+ hw->ops.ioctl = snd_opl3_ioctl;
+ hw->ops.release = snd_opl3_release;
+
+ opl3->seq_dev_num = seq_device;
+#if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
+ if (snd_seq_device_new(card, seq_device, SNDRV_SEQ_DEV_ID_OPL3,
+ sizeof(opl3_t*), &opl3->seq_dev) >= 0) {
+ strcpy(opl3->seq_dev->name, hw->name);
+ *(opl3_t**)SNDRV_SEQ_DEVICE_ARGPTR(opl3->seq_dev) = opl3;
+ }
+#endif
+ if (rhwdep)
+ *rhwdep = hw;
+ return 0;
+}
+
+EXPORT_SYMBOL(snd_opl3_interrupt);
+EXPORT_SYMBOL(snd_opl3_new);
+EXPORT_SYMBOL(snd_opl3_init);
+EXPORT_SYMBOL(snd_opl3_create);
+EXPORT_SYMBOL(snd_opl3_timer_new);
+EXPORT_SYMBOL(snd_opl3_hwdep_new);
+
+/* opl3_synth.c */
+EXPORT_SYMBOL(snd_opl3_regmap);
+EXPORT_SYMBOL(snd_opl3_reset);
+
+/*
+ * INIT part
+ */
+
+static int __init alsa_opl3_init(void)
+{
+ return 0;
+}
+
+static void __exit alsa_opl3_exit(void)
+{
+}
+
+module_init(alsa_opl3_init)
+module_exit(alsa_opl3_exit)
diff --git a/sound/drivers/opl3/opl3_midi.c b/sound/drivers/opl3/opl3_midi.c
new file mode 100644
index 000000000000..93d674070b71
--- /dev/null
+++ b/sound/drivers/opl3/opl3_midi.c
@@ -0,0 +1,873 @@
+/*
+ * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
+ *
+ * Midi synth routines for OPL2/OPL3/OPL4 FM
+ *
+ * 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
+ *
+ */
+
+#undef DEBUG_ALLOC
+#undef DEBUG_MIDI
+
+#include "opl3_voice.h"
+#include <sound/asoundef.h>
+
+extern char snd_opl3_regmap[MAX_OPL2_VOICES][4];
+
+extern int use_internal_drums;
+
+/*
+ * The next table looks magical, but it certainly is not. Its values have
+ * been calculated as table[i]=8*log(i/64)/log(2) with an obvious exception
+ * for i=0. This log-table converts a linear volume-scaling (0..127) to a
+ * logarithmic scaling as present in the FM-synthesizer chips. so : Volume
+ * 64 = 0 db = relative volume 0 and: Volume 32 = -6 db = relative
+ * volume -8 it was implemented as a table because it is only 128 bytes and
+ * it saves a lot of log() calculations. (Rob Hooft <hooft@chem.ruu.nl>)
+ */
+
+static char opl3_volume_table[128] =
+{
+ -63, -48, -40, -35, -32, -29, -27, -26,
+ -24, -23, -21, -20, -19, -18, -18, -17,
+ -16, -15, -15, -14, -13, -13, -12, -12,
+ -11, -11, -10, -10, -10, -9, -9, -8,
+ -8, -8, -7, -7, -7, -6, -6, -6,
+ -5, -5, -5, -5, -4, -4, -4, -4,
+ -3, -3, -3, -3, -2, -2, -2, -2,
+ -2, -1, -1, -1, -1, 0, 0, 0,
+ 0, 0, 0, 1, 1, 1, 1, 1,
+ 1, 2, 2, 2, 2, 2, 2, 2,
+ 3, 3, 3, 3, 3, 3, 3, 4,
+ 4, 4, 4, 4, 4, 4, 4, 5,
+ 5, 5, 5, 5, 5, 5, 5, 5,
+ 6, 6, 6, 6, 6, 6, 6, 6,
+ 6, 7, 7, 7, 7, 7, 7, 7,
+ 7, 7, 7, 8, 8, 8, 8, 8
+};
+
+void snd_opl3_calc_volume(unsigned char *volbyte, int vel,
+ snd_midi_channel_t *chan)
+{
+ int oldvol, newvol, n;
+ int volume;
+
+ volume = (vel * chan->gm_volume * chan->gm_expression) / (127*127);
+ if (volume > 127)
+ volume = 127;
+
+ oldvol = OPL3_TOTAL_LEVEL_MASK - (*volbyte & OPL3_TOTAL_LEVEL_MASK);
+
+ newvol = opl3_volume_table[volume] + oldvol;
+ if (newvol > OPL3_TOTAL_LEVEL_MASK)
+ newvol = OPL3_TOTAL_LEVEL_MASK;
+ else if (newvol < 0)
+ newvol = 0;
+
+ n = OPL3_TOTAL_LEVEL_MASK - (newvol & OPL3_TOTAL_LEVEL_MASK);
+
+ *volbyte = (*volbyte & OPL3_KSL_MASK) | (n & OPL3_TOTAL_LEVEL_MASK);
+}
+
+/*
+ * Converts the note frequency to block and fnum values for the FM chip
+ */
+static short opl3_note_table[16] =
+{
+ 305, 323, /* for pitch bending, -2 semitones */
+ 343, 363, 385, 408, 432, 458, 485, 514, 544, 577, 611, 647,
+ 686, 726 /* for pitch bending, +2 semitones */
+};
+
+static void snd_opl3_calc_pitch(unsigned char *fnum, unsigned char *blocknum,
+ int note, snd_midi_channel_t *chan)
+{
+ int block = ((note / 12) & 0x07) - 1;
+ int idx = (note % 12) + 2;
+ int freq;
+
+ if (chan->midi_pitchbend) {
+ int pitchbend = chan->midi_pitchbend;
+ int segment;
+
+ if (pitchbend > 0x1FFF)
+ pitchbend = 0x1FFF;
+
+ segment = pitchbend / 0x1000;
+ freq = opl3_note_table[idx+segment];
+ freq += ((opl3_note_table[idx+segment+1] - freq) *
+ (pitchbend % 0x1000)) / 0x1000;
+ } else {
+ freq = opl3_note_table[idx];
+ }
+
+ *fnum = (unsigned char) freq;
+ *blocknum = ((freq >> 8) & OPL3_FNUM_HIGH_MASK) |
+ ((block << 2) & OPL3_BLOCKNUM_MASK);
+}
+
+
+#ifdef DEBUG_ALLOC
+static void debug_alloc(opl3_t *opl3, char *s, int voice) {
+ int i;
+ char *str = "x.24";
+
+ printk("time %.5i: %s [%.2i]: ", opl3->use_time, s, voice);
+ for (i = 0; i < opl3->max_voices; i++)
+ printk("%c", *(str + opl3->voices[i].state + 1));
+ printk("\n");
+}
+#endif
+
+/*
+ * Get a FM voice (channel) to play a note on.
+ */
+static int opl3_get_voice(opl3_t *opl3, int instr_4op,
+ snd_midi_channel_t *chan) {
+ int chan_4op_1; /* first voice for 4op instrument */
+ int chan_4op_2; /* second voice for 4op instrument */
+
+ snd_opl3_voice_t *vp, *vp2;
+ unsigned int voice_time;
+ int i;
+
+#ifdef DEBUG_ALLOC
+ char *alloc_type[3] = { "FREE ", "CHEAP ", "EXPENSIVE" };
+#endif
+
+ /* This is our "allocation cost" table */
+ enum {
+ FREE = 0, CHEAP, EXPENSIVE, END
+ };
+
+ /* Keeps track of what we are finding */
+ struct best {
+ unsigned int time;
+ int voice;
+ } best[END];
+ struct best *bp;
+
+ for (i = 0; i < END; i++) {
+ best[i].time = (unsigned int)(-1); /* XXX MAX_?INT really */;
+ best[i].voice = -1;
+ }
+
+ /* Look through all the channels for the most suitable. */
+ for (i = 0; i < opl3->max_voices; i++) {
+ vp = &opl3->voices[i];
+
+ if (vp->state == SNDRV_OPL3_ST_NOT_AVAIL)
+ /* skip unavailable channels, allocated by
+ drum voices or by bounded 4op voices) */
+ continue;
+
+ voice_time = vp->time;
+ bp = best;
+
+ chan_4op_1 = ((i < 3) || (i > 8 && i < 12));
+ chan_4op_2 = ((i > 2 && i < 6) || (i > 11 && i < 15));
+ if (instr_4op) {
+ /* allocate 4op voice */
+ /* skip channels unavailable to 4op instrument */
+ if (!chan_4op_1)
+ continue;
+
+ if (vp->state)
+ /* kill one voice, CHEAP */
+ bp++;
+ /* get state of bounded 2op channel
+ to be allocated for 4op instrument */
+ vp2 = &opl3->voices[i + 3];
+ if (vp2->state == SNDRV_OPL3_ST_ON_2OP) {
+ /* kill two voices, EXPENSIVE */
+ bp++;
+ voice_time = (voice_time > vp->time) ?
+ voice_time : vp->time;
+ }
+ } else {
+ /* allocate 2op voice */
+ if ((chan_4op_1) || (chan_4op_2))
+ /* use bounded channels for 2op, CHEAP */
+ bp++;
+ else if (vp->state)
+ /* kill one voice on 2op channel, CHEAP */
+ bp++;
+ /* raise kill cost to EXPENSIVE for all channels */
+ if (vp->state)
+ bp++;
+ }
+ if (voice_time < bp->time) {
+ bp->time = voice_time;
+ bp->voice = i;
+ }
+ }
+
+ for (i = 0; i < END; i++) {
+ if (best[i].voice >= 0) {
+#ifdef DEBUG_ALLOC
+ printk("%s %iop allocation on voice %i\n",
+ alloc_type[i], instr_4op ? 4 : 2,
+ best[i].voice);
+#endif
+ return best[i].voice;
+ }
+ }
+ /* not found */
+ return -1;
+}
+
+/* ------------------------------ */
+
+/*
+ * System timer interrupt function
+ */
+void snd_opl3_timer_func(unsigned long data)
+{
+
+ opl3_t *opl3 = (opl3_t *)data;
+ int again = 0;
+ int i;
+
+ spin_lock(&opl3->sys_timer_lock);
+ for (i = 0; i < opl3->max_voices; i++) {
+ snd_opl3_voice_t *vp = &opl3->voices[i];
+ if (vp->state > 0 && vp->note_off_check) {
+ if (vp->note_off == jiffies)
+ snd_opl3_note_off(opl3, vp->note, 0, vp->chan);
+ else
+ again++;
+ }
+ }
+ if (again) {
+ opl3->tlist.expires = jiffies + 1; /* invoke again */
+ add_timer(&opl3->tlist);
+ } else {
+ opl3->sys_timer_status = 0;
+ }
+ spin_unlock(&opl3->sys_timer_lock);
+}
+
+/*
+ * Start system timer
+ */
+static void snd_opl3_start_timer(opl3_t *opl3)
+{
+ unsigned long flags;
+ spin_lock_irqsave(&opl3->sys_timer_lock, flags);
+ if (! opl3->sys_timer_status) {
+ opl3->tlist.expires = jiffies + 1;
+ add_timer(&opl3->tlist);
+ opl3->sys_timer_status = 1;
+ }
+ spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
+}
+
+/* ------------------------------ */
+
+
+static int snd_opl3_oss_map[MAX_OPL3_VOICES] = {
+ 0, 1, 2, 9, 10, 11, 6, 7, 8, 15, 16, 17, 3, 4 ,5, 12, 13, 14
+};
+
+/*
+ * Start a note.
+ */
+void snd_opl3_note_on(void *p, int note, int vel, snd_midi_channel_t *chan)
+{
+ opl3_t *opl3;
+ snd_seq_instr_t wanted;
+ snd_seq_kinstr_t *kinstr;
+ int instr_4op;
+
+ int voice;
+ snd_opl3_voice_t *vp, *vp2;
+ unsigned short connect_mask;
+ unsigned char connection;
+ unsigned char vol_op[4];
+
+ int extra_prg = 0;
+
+ unsigned short reg_side;
+ unsigned char op_offset;
+ unsigned char voice_offset;
+ unsigned short opl3_reg;
+ unsigned char reg_val;
+
+ int key = note;
+ unsigned char fnum, blocknum;
+ int i;
+
+ fm_instrument_t *fm;
+ unsigned long flags;
+
+ opl3 = p;
+
+#ifdef DEBUG_MIDI
+ snd_printk("Note on, ch %i, inst %i, note %i, vel %i\n",
+ chan->number, chan->midi_program, note, vel);
+#endif
+ wanted.cluster = 0;
+ wanted.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
+
+ /* in SYNTH mode, application takes care of voices */
+ /* in SEQ mode, drum voice numbers are notes on drum channel */
+ if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
+ if (chan->drum_channel) {
+ /* percussion instruments are located in bank 128 */
+ wanted.bank = 128;
+ wanted.prg = note;
+ } else {
+ wanted.bank = chan->gm_bank_select;
+ wanted.prg = chan->midi_program;
+ }
+ } else {
+ /* Prepare for OSS mode */
+ if (chan->number >= MAX_OPL3_VOICES)
+ return;
+
+ /* OSS instruments are located in bank 127 */
+ wanted.bank = 127;
+ wanted.prg = chan->midi_program;
+ }
+
+ spin_lock_irqsave(&opl3->voice_lock, flags);
+
+ if (use_internal_drums) {
+ snd_opl3_drum_switch(opl3, note, vel, 1, chan);
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+ return;
+ }
+
+ __extra_prg:
+ kinstr = snd_seq_instr_find(opl3->ilist, &wanted, 1, 0);
+ if (kinstr == NULL) {
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+ return;
+ }
+
+ fm = KINSTR_DATA(kinstr);
+
+ switch (fm->type) {
+ case FM_PATCH_OPL2:
+ instr_4op = 0;
+ break;
+ case FM_PATCH_OPL3:
+ if (opl3->hardware >= OPL3_HW_OPL3) {
+ instr_4op = 1;
+ break;
+ }
+ default:
+ snd_seq_instr_free_use(opl3->ilist, kinstr);
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+ return;
+ }
+
+#ifdef DEBUG_MIDI
+ snd_printk(" --> OPL%i instrument: %s\n",
+ instr_4op ? 3 : 2, kinstr->name);
+#endif
+ /* in SYNTH mode, application takes care of voices */
+ /* in SEQ mode, allocate voice on free OPL3 channel */
+ if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
+ voice = opl3_get_voice(opl3, instr_4op, chan);
+ } else {
+ /* remap OSS voice */
+ voice = snd_opl3_oss_map[chan->number];
+ }
+
+ if (voice < MAX_OPL2_VOICES) {
+ /* Left register block for voices 0 .. 8 */
+ reg_side = OPL3_LEFT;
+ voice_offset = voice;
+ connect_mask = (OPL3_LEFT_4OP_0 << voice_offset) & 0x07;
+ } else {
+ /* Right register block for voices 9 .. 17 */
+ reg_side = OPL3_RIGHT;
+ voice_offset = voice - MAX_OPL2_VOICES;
+ connect_mask = (OPL3_RIGHT_4OP_0 << voice_offset) & 0x38;
+ }
+
+ /* kill voice on channel */
+ vp = &opl3->voices[voice];
+ if (vp->state > 0) {
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
+ opl3->command(opl3, opl3_reg, reg_val);
+ }
+ if (instr_4op) {
+ vp2 = &opl3->voices[voice + 3];
+ if (vp->state > 0) {
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK +
+ voice_offset + 3);
+ reg_val = vp->keyon_reg & ~OPL3_KEYON_BIT;
+ opl3->command(opl3, opl3_reg, reg_val);
+ }
+ }
+
+ /* set connection register */
+ if (instr_4op) {
+ if ((opl3->connection_reg ^ connect_mask) & connect_mask) {
+ opl3->connection_reg |= connect_mask;
+ /* set connection bit */
+ opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
+ opl3->command(opl3, opl3_reg, opl3->connection_reg);
+ }
+ } else {
+ if ((opl3->connection_reg ^ ~connect_mask) & connect_mask) {
+ opl3->connection_reg &= ~connect_mask;
+ /* clear connection bit */
+ opl3_reg = OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT;
+ opl3->command(opl3, opl3_reg, opl3->connection_reg);
+ }
+ }
+
+#ifdef DEBUG_MIDI
+ snd_printk(" --> setting OPL3 connection: 0x%x\n",
+ opl3->connection_reg);
+#endif
+ /*
+ * calculate volume depending on connection
+ * between FM operators (see include/opl3.h)
+ */
+ for (i = 0; i < (instr_4op ? 4 : 2); i++)
+ vol_op[i] = fm->op[i].ksl_level;
+
+ connection = fm->feedback_connection[0] & 0x01;
+ if (instr_4op) {
+ connection <<= 1;
+ connection |= fm->feedback_connection[1] & 0x01;
+
+ snd_opl3_calc_volume(&vol_op[3], vel, chan);
+ switch (connection) {
+ case 0x03:
+ snd_opl3_calc_volume(&vol_op[2], vel, chan);
+ /* fallthru */
+ case 0x02:
+ snd_opl3_calc_volume(&vol_op[0], vel, chan);
+ break;
+ case 0x01:
+ snd_opl3_calc_volume(&vol_op[1], vel, chan);
+ }
+ } else {
+ snd_opl3_calc_volume(&vol_op[1], vel, chan);
+ if (connection)
+ snd_opl3_calc_volume(&vol_op[0], vel, chan);
+ }
+
+ /* Program the FM voice characteristics */
+ for (i = 0; i < (instr_4op ? 4 : 2); i++) {
+#ifdef DEBUG_MIDI
+ snd_printk(" --> programming operator %i\n", i);
+#endif
+ op_offset = snd_opl3_regmap[voice_offset][i];
+
+ /* Set OPL3 AM_VIB register of requested voice/operator */
+ reg_val = fm->op[i].am_vib;
+ opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set OPL3 KSL_LEVEL register of requested voice/operator */
+ reg_val = vol_op[i];
+ opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
+ reg_val = fm->op[i].attack_decay;
+ opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
+ reg_val = fm->op[i].sustain_release;
+ opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Select waveform */
+ reg_val = fm->op[i].wave_select;
+ opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+ }
+
+ /* Set operator feedback and 2op inter-operator connection */
+ reg_val = fm->feedback_connection[0];
+ /* Set output voice connection */
+ reg_val |= OPL3_STEREO_BITS;
+ if (chan->gm_pan < 43)
+ reg_val &= ~OPL3_VOICE_TO_RIGHT;
+ if (chan->gm_pan > 85)
+ reg_val &= ~OPL3_VOICE_TO_LEFT;
+ opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ if (instr_4op) {
+ /* Set 4op inter-operator connection */
+ reg_val = fm->feedback_connection[1] & OPL3_CONNECTION_BIT;
+ /* Set output voice connection */
+ reg_val |= OPL3_STEREO_BITS;
+ if (chan->gm_pan < 43)
+ reg_val &= ~OPL3_VOICE_TO_RIGHT;
+ if (chan->gm_pan > 85)
+ reg_val &= ~OPL3_VOICE_TO_LEFT;
+ opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION +
+ voice_offset + 3);
+ opl3->command(opl3, opl3_reg, reg_val);
+ }
+
+ /*
+ * Special treatment of percussion notes for fm:
+ * Requested pitch is really program, and pitch for
+ * device is whatever was specified in the patch library.
+ */
+ if (fm->fix_key)
+ note = fm->fix_key;
+ /*
+ * use transpose if defined in patch library
+ */
+ if (fm->trnsps)
+ note += (fm->trnsps - 64);
+
+ snd_opl3_calc_pitch(&fnum, &blocknum, note, chan);
+
+ /* Set OPL3 FNUM_LOW register of requested voice */
+ opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
+ opl3->command(opl3, opl3_reg, fnum);
+
+ opl3->voices[voice].keyon_reg = blocknum;
+
+ /* Set output sound flag */
+ blocknum |= OPL3_KEYON_BIT;
+
+#ifdef DEBUG_MIDI
+ snd_printk(" --> trigger voice %i\n", voice);
+#endif
+ /* Set OPL3 KEYON_BLOCK register of requested voice */
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ opl3->command(opl3, opl3_reg, blocknum);
+
+ /* kill note after fixed duration (in centiseconds) */
+ if (fm->fix_dur) {
+ opl3->voices[voice].note_off = jiffies +
+ (fm->fix_dur * HZ) / 100;
+ snd_opl3_start_timer(opl3);
+ opl3->voices[voice].note_off_check = 1;
+ } else
+ opl3->voices[voice].note_off_check = 0;
+
+ /* get extra pgm, but avoid possible loops */
+ extra_prg = (extra_prg) ? 0 : fm->modes;
+
+ snd_seq_instr_free_use(opl3->ilist, kinstr);
+
+ /* do the bookkeeping */
+ vp->time = opl3->use_time++;
+ vp->note = key;
+ vp->chan = chan;
+
+ if (instr_4op) {
+ vp->state = SNDRV_OPL3_ST_ON_4OP;
+
+ vp2 = &opl3->voices[voice + 3];
+ vp2->time = opl3->use_time++;
+ vp2->note = key;
+ vp2->chan = chan;
+ vp2->state = SNDRV_OPL3_ST_NOT_AVAIL;
+ } else {
+ if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
+ /* 4op killed by 2op, release bounded voice */
+ vp2 = &opl3->voices[voice + 3];
+ vp2->time = opl3->use_time++;
+ vp2->state = SNDRV_OPL3_ST_OFF;
+ }
+ vp->state = SNDRV_OPL3_ST_ON_2OP;
+ }
+
+#ifdef DEBUG_ALLOC
+ debug_alloc(opl3, "note on ", voice);
+#endif
+
+ /* allocate extra program if specified in patch library */
+ if (extra_prg) {
+ if (extra_prg > 128) {
+ wanted.bank = 128;
+ /* percussions start at 35 */
+ wanted.prg = extra_prg - 128 + 35 - 1;
+ } else {
+ wanted.bank = 0;
+ wanted.prg = extra_prg - 1;
+ }
+#ifdef DEBUG_MIDI
+ snd_printk(" *** allocating extra program\n");
+#endif
+ goto __extra_prg;
+ }
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+}
+
+static void snd_opl3_kill_voice(opl3_t *opl3, int voice)
+{
+ unsigned short reg_side;
+ unsigned char voice_offset;
+ unsigned short opl3_reg;
+
+ snd_opl3_voice_t *vp, *vp2;
+
+ snd_assert(voice < MAX_OPL3_VOICES, return);
+
+ vp = &opl3->voices[voice];
+ if (voice < MAX_OPL2_VOICES) {
+ /* Left register block for voices 0 .. 8 */
+ reg_side = OPL3_LEFT;
+ voice_offset = voice;
+ } else {
+ /* Right register block for voices 9 .. 17 */
+ reg_side = OPL3_RIGHT;
+ voice_offset = voice - MAX_OPL2_VOICES;
+ }
+
+ /* kill voice */
+#ifdef DEBUG_MIDI
+ snd_printk(" --> kill voice %i\n", voice);
+#endif
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ /* clear Key ON bit */
+ opl3->command(opl3, opl3_reg, vp->keyon_reg);
+
+ /* do the bookkeeping */
+ vp->time = opl3->use_time++;
+
+ if (vp->state == SNDRV_OPL3_ST_ON_4OP) {
+ vp2 = &opl3->voices[voice + 3];
+
+ vp2->time = opl3->use_time++;
+ vp2->state = SNDRV_OPL3_ST_OFF;
+ }
+ vp->state = SNDRV_OPL3_ST_OFF;
+#ifdef DEBUG_ALLOC
+ debug_alloc(opl3, "note off", voice);
+#endif
+
+}
+
+/*
+ * Release a note in response to a midi note off.
+ */
+void snd_opl3_note_off(void *p, int note, int vel, snd_midi_channel_t *chan)
+{
+ opl3_t *opl3;
+
+ int voice;
+ snd_opl3_voice_t *vp;
+
+ unsigned long flags;
+
+ opl3 = p;
+
+#ifdef DEBUG_MIDI
+ snd_printk("Note off, ch %i, inst %i, note %i\n",
+ chan->number, chan->midi_program, note);
+#endif
+
+ spin_lock_irqsave(&opl3->voice_lock, flags);
+
+ if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
+ if (chan->drum_channel && use_internal_drums) {
+ snd_opl3_drum_switch(opl3, note, vel, 0, chan);
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+ return;
+ }
+ /* this loop will hopefully kill all extra voices, because
+ they are grouped by the same channel and note values */
+ for (voice = 0; voice < opl3->max_voices; voice++) {
+ vp = &opl3->voices[voice];
+ if (vp->state > 0 && vp->chan == chan && vp->note == note) {
+ snd_opl3_kill_voice(opl3, voice);
+ }
+ }
+ } else {
+ /* remap OSS voices */
+ if (chan->number < MAX_OPL3_VOICES) {
+ voice = snd_opl3_oss_map[chan->number];
+ snd_opl3_kill_voice(opl3, voice);
+ }
+ }
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+}
+
+/*
+ * key pressure change
+ */
+void snd_opl3_key_press(void *p, int note, int vel, snd_midi_channel_t *chan)
+{
+ opl3_t *opl3;
+
+ opl3 = p;
+#ifdef DEBUG_MIDI
+ snd_printk("Key pressure, ch#: %i, inst#: %i\n",
+ chan->number, chan->midi_program);
+#endif
+}
+
+/*
+ * terminate note
+ */
+void snd_opl3_terminate_note(void *p, int note, snd_midi_channel_t *chan)
+{
+ opl3_t *opl3;
+
+ opl3 = p;
+#ifdef DEBUG_MIDI
+ snd_printk("Terminate note, ch#: %i, inst#: %i\n",
+ chan->number, chan->midi_program);
+#endif
+}
+
+static void snd_opl3_update_pitch(opl3_t *opl3, int voice)
+{
+ unsigned short reg_side;
+ unsigned char voice_offset;
+ unsigned short opl3_reg;
+
+ unsigned char fnum, blocknum;
+
+ snd_opl3_voice_t *vp;
+
+ snd_assert(voice < MAX_OPL3_VOICES, return);
+
+ vp = &opl3->voices[voice];
+ if (vp->chan == NULL)
+ return; /* not allocated? */
+
+ if (voice < MAX_OPL2_VOICES) {
+ /* Left register block for voices 0 .. 8 */
+ reg_side = OPL3_LEFT;
+ voice_offset = voice;
+ } else {
+ /* Right register block for voices 9 .. 17 */
+ reg_side = OPL3_RIGHT;
+ voice_offset = voice - MAX_OPL2_VOICES;
+ }
+
+ snd_opl3_calc_pitch(&fnum, &blocknum, vp->note, vp->chan);
+
+ /* Set OPL3 FNUM_LOW register of requested voice */
+ opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
+ opl3->command(opl3, opl3_reg, fnum);
+
+ vp->keyon_reg = blocknum;
+
+ /* Set output sound flag */
+ blocknum |= OPL3_KEYON_BIT;
+
+ /* Set OPL3 KEYON_BLOCK register of requested voice */
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ opl3->command(opl3, opl3_reg, blocknum);
+
+ vp->time = opl3->use_time++;
+}
+
+/*
+ * Update voice pitch controller
+ */
+static void snd_opl3_pitch_ctrl(opl3_t *opl3, snd_midi_channel_t *chan)
+{
+ int voice;
+ snd_opl3_voice_t *vp;
+
+ unsigned long flags;
+
+ spin_lock_irqsave(&opl3->voice_lock, flags);
+
+ if (opl3->synth_mode == SNDRV_OPL3_MODE_SEQ) {
+ for (voice = 0; voice < opl3->max_voices; voice++) {
+ vp = &opl3->voices[voice];
+ if (vp->state > 0 && vp->chan == chan) {
+ snd_opl3_update_pitch(opl3, voice);
+ }
+ }
+ } else {
+ /* remap OSS voices */
+ if (chan->number < MAX_OPL3_VOICES) {
+ voice = snd_opl3_oss_map[chan->number];
+ snd_opl3_update_pitch(opl3, voice);
+ }
+ }
+ spin_unlock_irqrestore(&opl3->voice_lock, flags);
+}
+
+/*
+ * Deal with a controler type event. This includes all types of
+ * control events, not just the midi controllers
+ */
+void snd_opl3_control(void *p, int type, snd_midi_channel_t *chan)
+{
+ opl3_t *opl3;
+
+ opl3 = p;
+#ifdef DEBUG_MIDI
+ snd_printk("Controller, TYPE = %i, ch#: %i, inst#: %i\n",
+ type, chan->number, chan->midi_program);
+#endif
+
+ switch (type) {
+ case MIDI_CTL_MSB_MODWHEEL:
+ if (chan->control[MIDI_CTL_MSB_MODWHEEL] > 63)
+ opl3->drum_reg |= OPL3_VIBRATO_DEPTH;
+ else
+ opl3->drum_reg &= ~OPL3_VIBRATO_DEPTH;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
+ opl3->drum_reg);
+ break;
+ case MIDI_CTL_E2_TREMOLO_DEPTH:
+ if (chan->control[MIDI_CTL_E2_TREMOLO_DEPTH] > 63)
+ opl3->drum_reg |= OPL3_TREMOLO_DEPTH;
+ else
+ opl3->drum_reg &= ~OPL3_TREMOLO_DEPTH;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION,
+ opl3->drum_reg);
+ break;
+ case MIDI_CTL_PITCHBEND:
+ snd_opl3_pitch_ctrl(opl3, chan);
+ break;
+ }
+}
+
+/*
+ * NRPN events
+ */
+void snd_opl3_nrpn(void *p, snd_midi_channel_t *chan,
+ snd_midi_channel_set_t *chset)
+{
+ opl3_t *opl3;
+
+ opl3 = p;
+#ifdef DEBUG_MIDI
+ snd_printk("NRPN, ch#: %i, inst#: %i\n",
+ chan->number, chan->midi_program);
+#endif
+}
+
+/*
+ * receive sysex
+ */
+void snd_opl3_sysex(void *p, unsigned char *buf, int len,
+ int parsed, snd_midi_channel_set_t *chset)
+{
+ opl3_t *opl3;
+
+ opl3 = p;
+#ifdef DEBUG_MIDI
+ snd_printk("SYSEX\n");
+#endif
+}
diff --git a/sound/drivers/opl3/opl3_oss.c b/sound/drivers/opl3/opl3_oss.c
new file mode 100644
index 000000000000..33da334ae981
--- /dev/null
+++ b/sound/drivers/opl3/opl3_oss.c
@@ -0,0 +1,356 @@
+/*
+ * Interface for OSS sequencer emulation
+ *
+ * Copyright (C) 2000 Uros Bizjak <uros@kss-loka.si>
+ *
+ * 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 "opl3_voice.h"
+#include <linux/slab.h>
+
+static int snd_opl3_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure);
+static int snd_opl3_close_seq_oss(snd_seq_oss_arg_t *arg);
+static int snd_opl3_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd, unsigned long ioarg);
+static int snd_opl3_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format, const char __user *buf, int offs, int count);
+static int snd_opl3_reset_seq_oss(snd_seq_oss_arg_t *arg);
+
+/* */
+
+static inline mm_segment_t snd_enter_user(void)
+{
+ mm_segment_t fs = get_fs();
+ set_fs(get_ds());
+ return fs;
+}
+
+static inline void snd_leave_user(mm_segment_t fs)
+{
+ set_fs(fs);
+}
+
+/* operators */
+
+extern snd_midi_op_t opl3_ops;
+
+static snd_seq_oss_callback_t oss_callback = {
+ .owner = THIS_MODULE,
+ .open = snd_opl3_open_seq_oss,
+ .close = snd_opl3_close_seq_oss,
+ .ioctl = snd_opl3_ioctl_seq_oss,
+ .load_patch = snd_opl3_load_patch_seq_oss,
+ .reset = snd_opl3_reset_seq_oss,
+};
+
+static int snd_opl3_oss_event_input(snd_seq_event_t *ev, int direct,
+ void *private_data, int atomic, int hop)
+{
+ opl3_t *opl3 = private_data;
+
+ if (ev->type != SNDRV_SEQ_EVENT_OSS)
+ snd_midi_process_event(&opl3_ops, ev, opl3->oss_chset);
+ return 0;
+}
+
+/* ------------------------------ */
+
+static void snd_opl3_oss_free_port(void *private_data)
+{
+ opl3_t *opl3 = private_data;
+
+ snd_midi_channel_free_set(opl3->oss_chset);
+}
+
+static int snd_opl3_oss_create_port(opl3_t * opl3)
+{
+ snd_seq_port_callback_t callbacks;
+ char name[32];
+ int voices, opl_ver;
+
+ voices = (opl3->hardware < OPL3_HW_OPL3) ?
+ MAX_OPL2_VOICES : MAX_OPL3_VOICES;
+ opl3->oss_chset = snd_midi_channel_alloc_set(voices);
+ if (opl3->oss_chset == NULL)
+ return -ENOMEM;
+ opl3->oss_chset->private_data = opl3;
+
+ memset(&callbacks, 0, sizeof(callbacks));
+ callbacks.owner = THIS_MODULE;
+ callbacks.event_input = snd_opl3_oss_event_input;
+ callbacks.private_free = snd_opl3_oss_free_port;
+ callbacks.private_data = opl3;
+
+ opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
+ sprintf(name, "OPL%i OSS Port", opl_ver);
+
+ opl3->oss_chset->client = opl3->seq_client;
+ opl3->oss_chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
+ SNDRV_SEQ_PORT_CAP_WRITE,
+ SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
+ SNDRV_SEQ_PORT_TYPE_MIDI_GM |
+ SNDRV_SEQ_PORT_TYPE_SYNTH,
+ voices, voices,
+ name);
+ if (opl3->oss_chset->port < 0) {
+ snd_midi_channel_free_set(opl3->oss_chset);
+ return opl3->oss_chset->port;
+ }
+ return 0;
+}
+
+/* ------------------------------ */
+
+/* register OSS synth */
+void snd_opl3_init_seq_oss(opl3_t *opl3, char *name)
+{
+ snd_seq_oss_reg_t *arg;
+ snd_seq_device_t *dev;
+
+ if (snd_seq_device_new(opl3->card, 0, SNDRV_SEQ_DEV_ID_OSS,
+ sizeof(snd_seq_oss_reg_t), &dev) < 0)
+ return;
+
+ opl3->oss_seq_dev = dev;
+ strlcpy(dev->name, name, sizeof(dev->name));
+ arg = SNDRV_SEQ_DEVICE_ARGPTR(dev);
+ arg->type = SYNTH_TYPE_FM;
+ if (opl3->hardware < OPL3_HW_OPL3) {
+ arg->subtype = FM_TYPE_ADLIB;
+ arg->nvoices = MAX_OPL2_VOICES;
+ } else {
+ arg->subtype = FM_TYPE_OPL3;
+ arg->nvoices = MAX_OPL3_VOICES;
+ }
+ arg->oper = oss_callback;
+ arg->private_data = opl3;
+
+ snd_opl3_oss_create_port(opl3);
+
+ /* register to OSS synth table */
+ snd_device_register(opl3->card, dev);
+}
+
+/* unregister */
+void snd_opl3_free_seq_oss(opl3_t *opl3)
+{
+ if (opl3->oss_seq_dev) {
+ snd_device_free(opl3->card, opl3->oss_seq_dev);
+ opl3->oss_seq_dev = NULL;
+ }
+}
+
+/* ------------------------------ */
+
+/* open OSS sequencer */
+static int snd_opl3_open_seq_oss(snd_seq_oss_arg_t *arg, void *closure)
+{
+ opl3_t *opl3 = closure;
+ int err;
+
+ snd_assert(arg != NULL, return -ENXIO);
+
+ if ((err = snd_opl3_synth_setup(opl3)) < 0)
+ return err;
+
+ /* fill the argument data */
+ arg->private_data = opl3;
+ arg->addr.client = opl3->oss_chset->client;
+ arg->addr.port = opl3->oss_chset->port;
+
+ if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
+ return err;
+
+ opl3->synth_mode = SNDRV_OPL3_MODE_SYNTH;
+ return 0;
+}
+
+/* close OSS sequencer */
+static int snd_opl3_close_seq_oss(snd_seq_oss_arg_t *arg)
+{
+ opl3_t *opl3;
+
+ snd_assert(arg != NULL, return -ENXIO);
+ opl3 = arg->private_data;
+
+ snd_opl3_synth_cleanup(opl3);
+
+ snd_opl3_synth_use_dec(opl3);
+ return 0;
+}
+
+/* load patch */
+
+/* offsets for SBI params */
+#define AM_VIB 0
+#define KSL_LEVEL 2
+#define ATTACK_DECAY 4
+#define SUSTAIN_RELEASE 6
+#define WAVE_SELECT 8
+
+/* offset for SBI instrument */
+#define CONNECTION 10
+#define OFFSET_4OP 11
+
+/* from sound_config.h */
+#define SBFM_MAXINSTR 256
+
+static int snd_opl3_load_patch_seq_oss(snd_seq_oss_arg_t *arg, int format,
+ const char __user *buf, int offs, int count)
+{
+ opl3_t *opl3;
+ int err = -EINVAL;
+
+ snd_assert(arg != NULL, return -ENXIO);
+ opl3 = arg->private_data;
+
+ if ((format == FM_PATCH) || (format == OPL3_PATCH)) {
+ struct sbi_instrument sbi;
+
+ size_t size;
+ snd_seq_instr_header_t *put;
+ snd_seq_instr_data_t *data;
+ fm_xinstrument_t *xinstr;
+
+ snd_seq_event_t ev;
+ int i;
+
+ mm_segment_t fs;
+
+ if (count < (int)sizeof(sbi)) {
+ snd_printk("FM Error: Patch record too short\n");
+ return -EINVAL;
+ }
+ if (copy_from_user(&sbi, buf, sizeof(sbi)))
+ return -EFAULT;
+
+ if (sbi.channel < 0 || sbi.channel >= SBFM_MAXINSTR) {
+ snd_printk("FM Error: Invalid instrument number %d\n", sbi.channel);
+ return -EINVAL;
+ }
+
+ size = sizeof(*put) + sizeof(fm_xinstrument_t);
+ put = kcalloc(1, size, GFP_KERNEL);
+ if (put == NULL)
+ return -ENOMEM;
+ /* build header */
+ data = &put->data;
+ data->type = SNDRV_SEQ_INSTR_ATYPE_DATA;
+ strcpy(data->data.format, SNDRV_SEQ_INSTR_ID_OPL2_3);
+ /* build data section */
+ xinstr = (fm_xinstrument_t *)(data + 1);
+ xinstr->stype = FM_STRU_INSTR;
+
+ for (i = 0; i < 2; i++) {
+ xinstr->op[i].am_vib = sbi.operators[AM_VIB + i];
+ xinstr->op[i].ksl_level = sbi.operators[KSL_LEVEL + i];
+ xinstr->op[i].attack_decay = sbi.operators[ATTACK_DECAY + i];
+ xinstr->op[i].sustain_release = sbi.operators[SUSTAIN_RELEASE + i];
+ xinstr->op[i].wave_select = sbi.operators[WAVE_SELECT + i];
+ }
+ xinstr->feedback_connection[0] = sbi.operators[CONNECTION];
+
+ if (format == OPL3_PATCH) {
+ xinstr->type = FM_PATCH_OPL3;
+ for (i = 0; i < 2; i++) {
+ xinstr->op[i+2].am_vib = sbi.operators[OFFSET_4OP + AM_VIB + i];
+ xinstr->op[i+2].ksl_level = sbi.operators[OFFSET_4OP + KSL_LEVEL + i];
+ xinstr->op[i+2].attack_decay = sbi.operators[OFFSET_4OP + ATTACK_DECAY + i];
+ xinstr->op[i+2].sustain_release = sbi.operators[OFFSET_4OP + SUSTAIN_RELEASE + i];
+ xinstr->op[i+2].wave_select = sbi.operators[OFFSET_4OP + WAVE_SELECT + i];
+ }
+ xinstr->feedback_connection[1] = sbi.operators[OFFSET_4OP + CONNECTION];
+ } else {
+ xinstr->type = FM_PATCH_OPL2;
+ }
+
+ put->id.instr.std = SNDRV_SEQ_INSTR_TYPE2_OPL2_3;
+ put->id.instr.bank = 127;
+ put->id.instr.prg = sbi.channel;
+ put->cmd = SNDRV_SEQ_INSTR_PUT_CMD_CREATE;
+
+ memset (&ev, 0, sizeof(ev));
+ ev.source.client = SNDRV_SEQ_CLIENT_OSS;
+ ev.dest = arg->addr;
+
+ ev.flags = SNDRV_SEQ_EVENT_LENGTH_VARUSR;
+ ev.queue = SNDRV_SEQ_QUEUE_DIRECT;
+
+ fs = snd_enter_user();
+ __again:
+ ev.type = SNDRV_SEQ_EVENT_INSTR_PUT;
+ ev.data.ext.len = size;
+ ev.data.ext.ptr = put;
+
+ err = snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
+ opl3->seq_client, 0, 0);
+ if (err == -EBUSY) {
+ snd_seq_instr_header_t remove;
+
+ memset (&remove, 0, sizeof(remove));
+ remove.cmd = SNDRV_SEQ_INSTR_FREE_CMD_SINGLE;
+ remove.id.instr = put->id.instr;
+
+ /* remove instrument */
+ ev.type = SNDRV_SEQ_EVENT_INSTR_FREE;
+ ev.data.ext.len = sizeof(remove);
+ ev.data.ext.ptr = &remove;
+
+ snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, &ev,
+ opl3->seq_client, 0, 0);
+ goto __again;
+ }
+ snd_leave_user(fs);
+
+ kfree(put);
+ }
+ return err;
+}
+
+/* ioctl */
+static int snd_opl3_ioctl_seq_oss(snd_seq_oss_arg_t *arg, unsigned int cmd,
+ unsigned long ioarg)
+{
+ opl3_t *opl3;
+
+ snd_assert(arg != NULL, return -ENXIO);
+ opl3 = arg->private_data;
+ switch (cmd) {
+ case SNDCTL_FM_LOAD_INSTR:
+ snd_printk("OPL3: Obsolete ioctl(SNDCTL_FM_LOAD_INSTR) used. Fix the program.\n");
+ return -EINVAL;
+
+ case SNDCTL_SYNTH_MEMAVL:
+ return 0x7fffffff;
+
+ case SNDCTL_FM_4OP_ENABLE:
+ // handled automatically by OPL instrument type
+ return 0;
+
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+/* reset device */
+static int snd_opl3_reset_seq_oss(snd_seq_oss_arg_t *arg)
+{
+ opl3_t *opl3;
+
+ snd_assert(arg != NULL, return -ENXIO);
+ opl3 = arg->private_data;
+
+ return 0;
+}
diff --git a/sound/drivers/opl3/opl3_seq.c b/sound/drivers/opl3/opl3_seq.c
new file mode 100644
index 000000000000..136964b844de
--- /dev/null
+++ b/sound/drivers/opl3/opl3_seq.c
@@ -0,0 +1,314 @@
+/*
+ * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
+ *
+ * Midi Sequencer interface routines for OPL2/OPL3/OPL4 FM
+ *
+ * OPL2/3 FM instrument loader:
+ * alsa-tools/seq/sbiload/
+ *
+ * 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 "opl3_voice.h"
+#include <linux/init.h>
+#include <linux/moduleparam.h>
+#include <sound/initval.h>
+
+MODULE_AUTHOR("Uros Bizjak <uros@kss-loka.si>");
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("ALSA driver for OPL3 FM synth");
+
+int use_internal_drums = 0;
+module_param(use_internal_drums, bool, 0444);
+MODULE_PARM_DESC(use_internal_drums, "Enable internal OPL2/3 drums.");
+
+int snd_opl3_synth_use_inc(opl3_t * opl3)
+{
+ if (!try_module_get(opl3->card->module))
+ return -EFAULT;
+ return 0;
+
+}
+
+void snd_opl3_synth_use_dec(opl3_t * opl3)
+{
+ module_put(opl3->card->module);
+}
+
+int snd_opl3_synth_setup(opl3_t * opl3)
+{
+ int idx;
+
+ down(&opl3->access_mutex);
+ if (opl3->used) {
+ up(&opl3->access_mutex);
+ return -EBUSY;
+ }
+ opl3->used++;
+ up(&opl3->access_mutex);
+
+ snd_opl3_reset(opl3);
+
+ for (idx = 0; idx < MAX_OPL3_VOICES; idx++) {
+ opl3->voices[idx].state = SNDRV_OPL3_ST_OFF;
+ opl3->voices[idx].time = 0;
+ opl3->voices[idx].keyon_reg = 0x00;
+ }
+ opl3->use_time = 0;
+ opl3->connection_reg = 0x00;
+ if (opl3->hardware >= OPL3_HW_OPL3) {
+ /* Clear 4-op connections */
+ opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT,
+ opl3->connection_reg);
+ opl3->max_voices = MAX_OPL3_VOICES;
+ }
+ return 0;
+}
+
+void snd_opl3_synth_cleanup(opl3_t * opl3)
+{
+ unsigned long flags;
+
+ /* Stop system timer */
+ spin_lock_irqsave(&opl3->sys_timer_lock, flags);
+ if (opl3->sys_timer_status) {
+ del_timer(&opl3->tlist);
+ opl3->sys_timer_status = 0;
+ }
+ spin_unlock_irqrestore(&opl3->sys_timer_lock, flags);
+
+ snd_opl3_reset(opl3);
+ down(&opl3->access_mutex);
+ opl3->used--;
+ up(&opl3->access_mutex);
+}
+
+static int snd_opl3_synth_use(void *private_data, snd_seq_port_subscribe_t * info)
+{
+ opl3_t *opl3 = private_data;
+ int err;
+
+ if ((err = snd_opl3_synth_setup(opl3)) < 0)
+ return err;
+
+ if (use_internal_drums) {
+ /* Percussion mode */
+ opl3->voices[6].state = opl3->voices[7].state =
+ opl3->voices[8].state = SNDRV_OPL3_ST_NOT_AVAIL;
+ snd_opl3_load_drums(opl3);
+ opl3->drum_reg = OPL3_PERCUSSION_ENABLE;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, opl3->drum_reg);
+ } else {
+ opl3->drum_reg = 0x00;
+ }
+
+ if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM) {
+ if ((err = snd_opl3_synth_use_inc(opl3)) < 0)
+ return err;
+ }
+ opl3->synth_mode = SNDRV_OPL3_MODE_SEQ;
+ return 0;
+}
+
+static int snd_opl3_synth_unuse(void *private_data, snd_seq_port_subscribe_t * info)
+{
+ opl3_t *opl3 = private_data;
+
+ snd_opl3_synth_cleanup(opl3);
+
+ if (info->sender.client != SNDRV_SEQ_CLIENT_SYSTEM)
+ snd_opl3_synth_use_dec(opl3);
+ return 0;
+}
+
+/*
+ * MIDI emulation operators
+ */
+snd_midi_op_t opl3_ops = {
+ .note_on = snd_opl3_note_on,
+ .note_off = snd_opl3_note_off,
+ .key_press = snd_opl3_key_press,
+ .note_terminate = snd_opl3_terminate_note,
+ .control = snd_opl3_control,
+ .nrpn = snd_opl3_nrpn,
+ .sysex = snd_opl3_sysex,
+};
+
+static int snd_opl3_synth_event_input(snd_seq_event_t * ev, int direct,
+ void *private_data, int atomic, int hop)
+{
+ opl3_t *opl3 = private_data;
+
+ if (ev->type >= SNDRV_SEQ_EVENT_INSTR_BEGIN &&
+ ev->type <= SNDRV_SEQ_EVENT_INSTR_CHANGE) {
+ if (direct) {
+ snd_seq_instr_event(&opl3->fm_ops, opl3->ilist, ev,
+ opl3->seq_client, atomic, hop);
+ }
+ } else {
+ snd_midi_process_event(&opl3_ops, ev, opl3->chset);
+ }
+ return 0;
+}
+
+/* ------------------------------ */
+
+static void snd_opl3_synth_free_port(void *private_data)
+{
+ opl3_t *opl3 = private_data;
+
+ snd_midi_channel_free_set(opl3->chset);
+}
+
+static int snd_opl3_synth_create_port(opl3_t * opl3)
+{
+ snd_seq_port_callback_t callbacks;
+ char name[32];
+ int voices, opl_ver;
+
+ voices = (opl3->hardware < OPL3_HW_OPL3) ?
+ MAX_OPL2_VOICES : MAX_OPL3_VOICES;
+ opl3->chset = snd_midi_channel_alloc_set(16);
+ if (opl3->chset == NULL)
+ return -ENOMEM;
+ opl3->chset->private_data = opl3;
+
+ memset(&callbacks, 0, sizeof(callbacks));
+ callbacks.owner = THIS_MODULE;
+ callbacks.use = snd_opl3_synth_use;
+ callbacks.unuse = snd_opl3_synth_unuse;
+ callbacks.event_input = snd_opl3_synth_event_input;
+ callbacks.private_free = snd_opl3_synth_free_port;
+ callbacks.private_data = opl3;
+
+ opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
+ sprintf(name, "OPL%i FM Port", opl_ver);
+
+ opl3->chset->client = opl3->seq_client;
+ opl3->chset->port = snd_seq_event_port_attach(opl3->seq_client, &callbacks,
+ SNDRV_SEQ_PORT_CAP_WRITE |
+ SNDRV_SEQ_PORT_CAP_SUBS_WRITE,
+ SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
+ SNDRV_SEQ_PORT_TYPE_MIDI_GM |
+ SNDRV_SEQ_PORT_TYPE_SYNTH,
+ 16, voices,
+ name);
+ if (opl3->chset->port < 0) {
+ snd_midi_channel_free_set(opl3->chset);
+ return opl3->chset->port;
+ }
+ return 0;
+}
+
+/* ------------------------------ */
+
+static int snd_opl3_seq_new_device(snd_seq_device_t *dev)
+{
+ opl3_t *opl3;
+ int client;
+ snd_seq_client_callback_t callbacks;
+ snd_seq_client_info_t cinfo;
+ int opl_ver;
+
+ opl3 = *(opl3_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
+ if (opl3 == NULL)
+ return -EINVAL;
+
+ spin_lock_init(&opl3->voice_lock);
+
+ opl3->seq_client = -1;
+
+ /* allocate new client */
+ memset(&callbacks, 0, sizeof(callbacks));
+ callbacks.private_data = opl3;
+ callbacks.allow_output = callbacks.allow_input = 1;
+ client = opl3->seq_client =
+ snd_seq_create_kernel_client(opl3->card, opl3->seq_dev_num, &callbacks);
+ if (client < 0)
+ return client;
+
+ /* change name of client */
+ memset(&cinfo, 0, sizeof(cinfo));
+ cinfo.client = client;
+ cinfo.type = KERNEL_CLIENT;
+ opl_ver = (opl3->hardware & OPL3_HW_MASK) >> 8;
+ sprintf(cinfo.name, "OPL%i FM synth", opl_ver);
+ snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_SET_CLIENT_INFO, &cinfo);
+
+ snd_opl3_synth_create_port(opl3);
+
+ /* initialize instrument list */
+ opl3->ilist = snd_seq_instr_list_new();
+ if (opl3->ilist == NULL) {
+ snd_seq_delete_kernel_client(client);
+ opl3->seq_client = -1;
+ return -ENOMEM;
+ }
+ opl3->ilist->flags = SNDRV_SEQ_INSTR_FLG_DIRECT;
+ snd_seq_fm_init(&opl3->fm_ops, NULL);
+
+ /* setup system timer */
+ init_timer(&opl3->tlist);
+ opl3->tlist.function = snd_opl3_timer_func;
+ opl3->tlist.data = (unsigned long) opl3;
+ spin_lock_init(&opl3->sys_timer_lock);
+ opl3->sys_timer_status = 0;
+
+#ifdef CONFIG_SND_SEQUENCER_OSS
+ snd_opl3_init_seq_oss(opl3, cinfo.name);
+#endif
+ return 0;
+}
+
+static int snd_opl3_seq_delete_device(snd_seq_device_t *dev)
+{
+ opl3_t *opl3;
+
+ opl3 = *(opl3_t **)SNDRV_SEQ_DEVICE_ARGPTR(dev);
+ if (opl3 == NULL)
+ return -EINVAL;
+
+#ifdef CONFIG_SND_SEQUENCER_OSS
+ snd_opl3_free_seq_oss(opl3);
+#endif
+ if (opl3->seq_client >= 0) {
+ snd_seq_delete_kernel_client(opl3->seq_client);
+ opl3->seq_client = -1;
+ }
+ if (opl3->ilist)
+ snd_seq_instr_list_free(&opl3->ilist);
+ return 0;
+}
+
+static int __init alsa_opl3_seq_init(void)
+{
+ static snd_seq_dev_ops_t ops =
+ {
+ snd_opl3_seq_new_device,
+ snd_opl3_seq_delete_device
+ };
+
+ return snd_seq_device_register_driver(SNDRV_SEQ_DEV_ID_OPL3, &ops,
+ sizeof(opl3_t*));
+}
+
+static void __exit alsa_opl3_seq_exit(void)
+{
+ snd_seq_device_unregister_driver(SNDRV_SEQ_DEV_ID_OPL3);
+}
+
+module_init(alsa_opl3_seq_init)
+module_exit(alsa_opl3_seq_exit)
diff --git a/sound/drivers/opl3/opl3_synth.c b/sound/drivers/opl3/opl3_synth.c
new file mode 100644
index 000000000000..04f9f955e5b7
--- /dev/null
+++ b/sound/drivers/opl3/opl3_synth.c
@@ -0,0 +1,447 @@
+/*
+ * Copyright (c) by Uros Bizjak <uros@kss-loka.si>
+ *
+ * Routines for OPL2/OPL3/OPL4 control
+ *
+ * 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 <sound/opl3.h>
+#include <sound/asound_fm.h>
+
+/*
+ * There is 18 possible 2 OP voices
+ * (9 in the left and 9 in the right).
+ * The first OP is the modulator and 2nd is the carrier.
+ *
+ * The first three voices in the both sides may be connected
+ * with another voice to a 4 OP voice. For example voice 0
+ * can be connected with voice 3. The operators of voice 3 are
+ * used as operators 3 and 4 of the new 4 OP voice.
+ * In this case the 2 OP voice number 0 is the 'first half' and
+ * voice 3 is the second.
+ */
+
+
+/*
+ * Register offset table for OPL2/3 voices,
+ * OPL2 / one OPL3 register array side only
+ */
+
+char snd_opl3_regmap[MAX_OPL2_VOICES][4] =
+{
+/* OP1 OP2 OP3 OP4 */
+/* ------------------------ */
+ { 0x00, 0x03, 0x08, 0x0b },
+ { 0x01, 0x04, 0x09, 0x0c },
+ { 0x02, 0x05, 0x0a, 0x0d },
+
+ { 0x08, 0x0b, 0x00, 0x00 },
+ { 0x09, 0x0c, 0x00, 0x00 },
+ { 0x0a, 0x0d, 0x00, 0x00 },
+
+ { 0x10, 0x13, 0x00, 0x00 }, /* used by percussive voices */
+ { 0x11, 0x14, 0x00, 0x00 }, /* if the percussive mode */
+ { 0x12, 0x15, 0x00, 0x00 } /* is selected (only left reg block) */
+};
+
+/*
+ * prototypes
+ */
+static int snd_opl3_play_note(opl3_t * opl3, snd_dm_fm_note_t * note);
+static int snd_opl3_set_voice(opl3_t * opl3, snd_dm_fm_voice_t * voice);
+static int snd_opl3_set_params(opl3_t * opl3, snd_dm_fm_params_t * params);
+static int snd_opl3_set_mode(opl3_t * opl3, int mode);
+static int snd_opl3_set_connection(opl3_t * opl3, int connection);
+
+/* ------------------------------ */
+
+/*
+ * open the device exclusively
+ */
+int snd_opl3_open(snd_hwdep_t * hw, struct file *file)
+{
+ opl3_t *opl3 = hw->private_data;
+
+ down(&opl3->access_mutex);
+ if (opl3->used) {
+ up(&opl3->access_mutex);
+ return -EAGAIN;
+ }
+ opl3->used++;
+ up(&opl3->access_mutex);
+
+ return 0;
+}
+
+/*
+ * ioctl for hwdep device:
+ */
+int snd_opl3_ioctl(snd_hwdep_t * hw, struct file *file,
+ unsigned int cmd, unsigned long arg)
+{
+ opl3_t *opl3 = hw->private_data;
+ void __user *argp = (void __user *)arg;
+
+ snd_assert(opl3 != NULL, return -EINVAL);
+
+ switch (cmd) {
+ /* get information */
+ case SNDRV_DM_FM_IOCTL_INFO:
+ {
+ snd_dm_fm_info_t info;
+
+ info.fm_mode = opl3->fm_mode;
+ info.rhythm = opl3->rhythm;
+ if (copy_to_user(argp, &info, sizeof(snd_dm_fm_info_t)))
+ return -EFAULT;
+ return 0;
+ }
+
+ case SNDRV_DM_FM_IOCTL_RESET:
+#ifdef CONFIG_SND_OSSEMUL
+ case SNDRV_DM_FM_OSS_IOCTL_RESET:
+#endif
+ snd_opl3_reset(opl3);
+ return 0;
+
+ case SNDRV_DM_FM_IOCTL_PLAY_NOTE:
+#ifdef CONFIG_SND_OSSEMUL
+ case SNDRV_DM_FM_OSS_IOCTL_PLAY_NOTE:
+#endif
+ {
+ snd_dm_fm_note_t note;
+ if (copy_from_user(&note, argp, sizeof(snd_dm_fm_note_t)))
+ return -EFAULT;
+ return snd_opl3_play_note(opl3, &note);
+ }
+
+ case SNDRV_DM_FM_IOCTL_SET_VOICE:
+#ifdef CONFIG_SND_OSSEMUL
+ case SNDRV_DM_FM_OSS_IOCTL_SET_VOICE:
+#endif
+ {
+ snd_dm_fm_voice_t voice;
+ if (copy_from_user(&voice, argp, sizeof(snd_dm_fm_voice_t)))
+ return -EFAULT;
+ return snd_opl3_set_voice(opl3, &voice);
+ }
+
+ case SNDRV_DM_FM_IOCTL_SET_PARAMS:
+#ifdef CONFIG_SND_OSSEMUL
+ case SNDRV_DM_FM_OSS_IOCTL_SET_PARAMS:
+#endif
+ {
+ snd_dm_fm_params_t params;
+ if (copy_from_user(&params, argp, sizeof(snd_dm_fm_params_t)))
+ return -EFAULT;
+ return snd_opl3_set_params(opl3, &params);
+ }
+
+ case SNDRV_DM_FM_IOCTL_SET_MODE:
+#ifdef CONFIG_SND_OSSEMUL
+ case SNDRV_DM_FM_OSS_IOCTL_SET_MODE:
+#endif
+ return snd_opl3_set_mode(opl3, (int) arg);
+
+ case SNDRV_DM_FM_IOCTL_SET_CONNECTION:
+#ifdef CONFIG_SND_OSSEMUL
+ case SNDRV_DM_FM_OSS_IOCTL_SET_OPL:
+#endif
+ return snd_opl3_set_connection(opl3, (int) arg);
+
+#ifdef CONFIG_SND_DEBUG
+ default:
+ snd_printk("unknown IOCTL: 0x%x\n", cmd);
+#endif
+ }
+ return -ENOTTY;
+}
+
+/*
+ * close the device
+ */
+int snd_opl3_release(snd_hwdep_t * hw, struct file *file)
+{
+ opl3_t *opl3 = hw->private_data;
+
+ snd_opl3_reset(opl3);
+ down(&opl3->access_mutex);
+ opl3->used--;
+ up(&opl3->access_mutex);
+
+ return 0;
+}
+
+/* ------------------------------ */
+
+void snd_opl3_reset(opl3_t * opl3)
+{
+ unsigned short opl3_reg;
+
+ unsigned short reg_side;
+ unsigned char voice_offset;
+
+ int max_voices, i;
+
+ max_voices = (opl3->hardware < OPL3_HW_OPL3) ?
+ MAX_OPL2_VOICES : MAX_OPL3_VOICES;
+
+ for (i = 0; i < max_voices; i++) {
+ /* Get register array side and offset of voice */
+ if (i < MAX_OPL2_VOICES) {
+ /* Left register block for voices 0 .. 8 */
+ reg_side = OPL3_LEFT;
+ voice_offset = i;
+ } else {
+ /* Right register block for voices 9 .. 17 */
+ reg_side = OPL3_RIGHT;
+ voice_offset = i - MAX_OPL2_VOICES;
+ }
+ opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][0]);
+ opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 1 volume */
+ opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + snd_opl3_regmap[voice_offset][1]);
+ opl3->command(opl3, opl3_reg, OPL3_TOTAL_LEVEL_MASK); /* Operator 2 volume */
+
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ opl3->command(opl3, opl3_reg, 0x00); /* Note off */
+ }
+
+ opl3->max_voices = MAX_OPL2_VOICES;
+ opl3->fm_mode = SNDRV_DM_FM_MODE_OPL2;
+
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_TEST, OPL3_ENABLE_WAVE_SELECT);
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, 0x00); /* Melodic mode */
+ opl3->rhythm = 0;
+}
+
+
+static int snd_opl3_play_note(opl3_t * opl3, snd_dm_fm_note_t * note)
+{
+ unsigned short reg_side;
+ unsigned char voice_offset;
+
+ unsigned short opl3_reg;
+ unsigned char reg_val;
+
+ /* Voices 0 - 8 in OPL2 mode */
+ /* Voices 0 - 17 in OPL3 mode */
+ if (note->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
+ MAX_OPL3_VOICES : MAX_OPL2_VOICES))
+ return -EINVAL;
+
+ /* Get register array side and offset of voice */
+ if (note->voice < MAX_OPL2_VOICES) {
+ /* Left register block for voices 0 .. 8 */
+ reg_side = OPL3_LEFT;
+ voice_offset = note->voice;
+ } else {
+ /* Right register block for voices 9 .. 17 */
+ reg_side = OPL3_RIGHT;
+ voice_offset = note->voice - MAX_OPL2_VOICES;
+ }
+
+ /* Set lower 8 bits of note frequency */
+ reg_val = (unsigned char) note->fnum;
+ opl3_reg = reg_side | (OPL3_REG_FNUM_LOW + voice_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ reg_val = 0x00;
+ /* Set output sound flag */
+ if (note->key_on)
+ reg_val |= OPL3_KEYON_BIT;
+ /* Set octave */
+ reg_val |= (note->octave << 2) & OPL3_BLOCKNUM_MASK;
+ /* Set higher 2 bits of note frequency */
+ reg_val |= (unsigned char) (note->fnum >> 8) & OPL3_FNUM_HIGH_MASK;
+
+ /* Set OPL3 KEYON_BLOCK register of requested voice */
+ opl3_reg = reg_side | (OPL3_REG_KEYON_BLOCK + voice_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ return 0;
+}
+
+
+static int snd_opl3_set_voice(opl3_t * opl3, snd_dm_fm_voice_t * voice)
+{
+ unsigned short reg_side;
+ unsigned char op_offset;
+ unsigned char voice_offset;
+
+ unsigned short opl3_reg;
+ unsigned char reg_val;
+
+ /* Only operators 1 and 2 */
+ if (voice->op > 1)
+ return -EINVAL;
+ /* Voices 0 - 8 in OPL2 mode */
+ /* Voices 0 - 17 in OPL3 mode */
+ if (voice->voice >= ((opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) ?
+ MAX_OPL3_VOICES : MAX_OPL2_VOICES))
+ return -EINVAL;
+
+ /* Get register array side and offset of voice */
+ if (voice->voice < MAX_OPL2_VOICES) {
+ /* Left register block for voices 0 .. 8 */
+ reg_side = OPL3_LEFT;
+ voice_offset = voice->voice;
+ } else {
+ /* Right register block for voices 9 .. 17 */
+ reg_side = OPL3_RIGHT;
+ voice_offset = voice->voice - MAX_OPL2_VOICES;
+ }
+ /* Get register offset of operator */
+ op_offset = snd_opl3_regmap[voice_offset][voice->op];
+
+ reg_val = 0x00;
+ /* Set amplitude modulation (tremolo) effect */
+ if (voice->am)
+ reg_val |= OPL3_TREMOLO_ON;
+ /* Set vibrato effect */
+ if (voice->vibrato)
+ reg_val |= OPL3_VIBRATO_ON;
+ /* Set sustaining sound phase */
+ if (voice->do_sustain)
+ reg_val |= OPL3_SUSTAIN_ON;
+ /* Set keyboard scaling bit */
+ if (voice->kbd_scale)
+ reg_val |= OPL3_KSR;
+ /* Set harmonic or frequency multiplier */
+ reg_val |= voice->harmonic & OPL3_MULTIPLE_MASK;
+
+ /* Set OPL3 AM_VIB register of requested voice/operator */
+ opl3_reg = reg_side | (OPL3_REG_AM_VIB + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set decreasing volume of higher notes */
+ reg_val = (voice->scale_level << 6) & OPL3_KSL_MASK;
+ /* Set output volume */
+ reg_val |= ~voice->volume & OPL3_TOTAL_LEVEL_MASK;
+
+ /* Set OPL3 KSL_LEVEL register of requested voice/operator */
+ opl3_reg = reg_side | (OPL3_REG_KSL_LEVEL + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set attack phase level */
+ reg_val = (voice->attack << 4) & OPL3_ATTACK_MASK;
+ /* Set decay phase level */
+ reg_val |= voice->decay & OPL3_DECAY_MASK;
+
+ /* Set OPL3 ATTACK_DECAY register of requested voice/operator */
+ opl3_reg = reg_side | (OPL3_REG_ATTACK_DECAY + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set sustain phase level */
+ reg_val = (voice->sustain << 4) & OPL3_SUSTAIN_MASK;
+ /* Set release phase level */
+ reg_val |= voice->release & OPL3_RELEASE_MASK;
+
+ /* Set OPL3 SUSTAIN_RELEASE register of requested voice/operator */
+ opl3_reg = reg_side | (OPL3_REG_SUSTAIN_RELEASE + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Set inter-operator feedback */
+ reg_val = (voice->feedback << 1) & OPL3_FEEDBACK_MASK;
+ /* Set inter-operator connection */
+ if (voice->connection)
+ reg_val |= OPL3_CONNECTION_BIT;
+ /* OPL-3 only */
+ if (opl3->fm_mode == SNDRV_DM_FM_MODE_OPL3) {
+ if (voice->left)
+ reg_val |= OPL3_VOICE_TO_LEFT;
+ if (voice->right)
+ reg_val |= OPL3_VOICE_TO_RIGHT;
+ }
+ /* Feedback/connection bits are applicable to voice */
+ opl3_reg = reg_side | (OPL3_REG_FEEDBACK_CONNECTION + voice_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ /* Select waveform */
+ reg_val = voice->waveform & OPL3_WAVE_SELECT_MASK;
+ opl3_reg = reg_side | (OPL3_REG_WAVE_SELECT + op_offset);
+ opl3->command(opl3, opl3_reg, reg_val);
+
+ return 0;
+}
+
+static int snd_opl3_set_params(opl3_t * opl3, snd_dm_fm_params_t * params)
+{
+ unsigned char reg_val;
+
+ reg_val = 0x00;
+ /* Set keyboard split method */
+ if (params->kbd_split)
+ reg_val |= OPL3_KEYBOARD_SPLIT;
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_KBD_SPLIT, reg_val);
+
+ reg_val = 0x00;
+ /* Set amplitude modulation (tremolo) depth */
+ if (params->am_depth)
+ reg_val |= OPL3_TREMOLO_DEPTH;
+ /* Set vibrato depth */
+ if (params->vib_depth)
+ reg_val |= OPL3_VIBRATO_DEPTH;
+ /* Set percussion mode */
+ if (params->rhythm) {
+ reg_val |= OPL3_PERCUSSION_ENABLE;
+ opl3->rhythm = 1;
+ } else {
+ opl3->rhythm = 0;
+ }
+ /* Play percussion instruments */
+ if (params->bass)
+ reg_val |= OPL3_BASSDRUM_ON;
+ if (params->snare)
+ reg_val |= OPL3_SNAREDRUM_ON;
+ if (params->tomtom)
+ reg_val |= OPL3_TOMTOM_ON;
+ if (params->cymbal)
+ reg_val |= OPL3_CYMBAL_ON;
+ if (params->hihat)
+ reg_val |= OPL3_HIHAT_ON;
+
+ opl3->command(opl3, OPL3_LEFT | OPL3_REG_PERCUSSION, reg_val);
+ return 0;
+}
+
+static int snd_opl3_set_mode(opl3_t * opl3, int mode)
+{
+ if ((mode == SNDRV_DM_FM_MODE_OPL3) && (opl3->hardware < OPL3_HW_OPL3))
+ return -EINVAL;
+
+ opl3->fm_mode = mode;
+ if (opl3->hardware >= OPL3_HW_OPL3)
+ opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, 0x00); /* Clear 4-op connections */
+
+ return 0;
+}
+
+static int snd_opl3_set_connection(opl3_t * opl3, int connection)
+{
+ unsigned char reg_val;
+
+ /* OPL-3 only */
+ if (opl3->fm_mode != SNDRV_DM_FM_MODE_OPL3)
+ return -EINVAL;
+
+ reg_val = connection & (OPL3_RIGHT_4OP_0 | OPL3_RIGHT_4OP_1 | OPL3_RIGHT_4OP_2 |
+ OPL3_LEFT_4OP_0 | OPL3_LEFT_4OP_1 | OPL3_LEFT_4OP_2);
+ /* Set 4-op connections */
+ opl3->command(opl3, OPL3_RIGHT | OPL3_REG_CONNECTION_SELECT, reg_val);
+
+ return 0;
+}
diff --git a/sound/drivers/opl3/opl3_voice.h b/sound/drivers/opl3/opl3_voice.h
new file mode 100644
index 000000000000..63346a5c3498
--- /dev/null
+++ b/sound/drivers/opl3/opl3_voice.h
@@ -0,0 +1,52 @@
+#ifndef __OPL3_VOICE_H
+#define __OPL3_VOICE_H
+
+/*
+ * Copyright (c) 2000 Uros Bizjak <uros@kss-loka.si>
+ *
+ * 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 <sound/opl3.h>
+
+/* Prototypes for opl3_seq.c */
+int snd_opl3_synth_use_inc(opl3_t * opl3);
+void snd_opl3_synth_use_dec(opl3_t * opl3);
+int snd_opl3_synth_setup(opl3_t * opl3);
+void snd_opl3_synth_cleanup(opl3_t * opl3);
+
+/* Prototypes for opl3_midi.c */
+void snd_opl3_note_on(void *p, int note, int vel, struct snd_midi_channel *chan);
+void snd_opl3_note_off(void *p, int note, int vel, struct snd_midi_channel *chan);
+void snd_opl3_key_press(void *p, int note, int vel, struct snd_midi_channel *chan);
+void snd_opl3_terminate_note(void *p, int note, snd_midi_channel_t *chan);
+void snd_opl3_control(void *p, int type, struct snd_midi_channel *chan);
+void snd_opl3_nrpn(void *p, snd_midi_channel_t *chan, snd_midi_channel_set_t *chset);
+void snd_opl3_sysex(void *p, unsigned char *buf, int len, int parsed, snd_midi_channel_set_t *chset);
+
+void snd_opl3_calc_volume(unsigned char *reg, int vel, snd_midi_channel_t *chan);
+void snd_opl3_timer_func(unsigned long data);
+
+/* Prototypes for opl3_drums.c */
+void snd_opl3_load_drums(opl3_t *opl3);
+void snd_opl3_drum_switch(opl3_t *opl3, int note, int on_off, int vel, snd_midi_channel_t *chan);
+
+/* Prototypes for opl3_oss.c */
+#ifdef CONFIG_SND_SEQUENCER_OSS
+void snd_opl3_init_seq_oss(opl3_t *opl3, char *name);
+void snd_opl3_free_seq_oss(opl3_t *opl3);
+#endif
+
+#endif