freebsd-skq/sys/dev/sound/pci/emu10kx-midi.c
Ariff Abdullah 90da2b2859 Sound Mega-commit. Expect further cleanup until code freeze.
For a slightly thorough explaination, please refer to
	[1] http://people.freebsd.org/~ariff/SOUND_4.TXT.html .

Summary of changes includes:

1 Volume Per-Channel (vpc).  Provides private / standalone volume control
  unique per-stream pcm channel without touching master volume / pcm.
  Applications can directly use SNDCTL_DSP_[GET|SET][PLAY|REC]VOL, or for
  backwards compatibility, SOUND_MIXER_PCM through the opened dsp device
  instead of /dev/mixer.  Special "bypass" mode is enabled through
  /dev/mixer which will automatically detect if the adjustment is made
  through /dev/mixer and forward its request to this private volume
  controller.  Changes to this volume object will not interfere with
  other channels.

  Requirements:
    - SNDCTL_DSP_[GET|SET][PLAY|REC]_VOL are newer ioctls (OSSv4) which
      require specific application modifications (preferred).
    - No modifications required for using bypass mode, so applications
      like mplayer or xmms should work out of the box.

  Kernel hints:
    - hint.pcm.%d.vpc (0 = disable vpc).

  Kernel sysctls:
    - hw.snd.vpc_mixer_bypass (default: 1).  Enable or disable /dev/mixer
      bypass mode.
    - hw.snd.vpc_autoreset (default: 1).  By default, closing/opening
      /dev/dsp will reset the volume back to 0 db gain/attenuation.
      Setting this to 0 will preserve its settings across device
      closing/opening.
    - hw.snd.vpc_reset (default: 0).  Panic/reset button to reset all
      volume settings back to 0 db.
    - hw.snd.vpc_0db (default: 45).  0 db relative to linear mixer value.

2 High quality fixed-point Bandlimited SINC sampling rate converter,
  based on Julius O'Smith's Digital Audio Resampling -
  http://ccrma.stanford.edu/~jos/resample/.  It includes a filter design
  script written in awk (the clumsiest joke I've ever written)
    - 100% 32bit fixed-point, 64bit accumulator.
    - Possibly among the fastest (if not fastest) of its kind.
    - Resampling quality is tunable, either runtime or during kernel
      compilation (FEEDER_RATE_PRESETS).
    - Quality can be further customized during kernel compilation by
      defining FEEDER_RATE_PRESETS in /etc/make.conf.

  Kernel sysctls:
    - hw.snd.feeder_rate_quality.
      0 - Zero-order Hold (ZOH).  Fastest, bad quality.
      1 - Linear Interpolation (LINEAR).  Slightly slower than ZOH,
          better quality but still does not eliminate aliasing.
      2 - (and above) - Sinc Interpolation(SINC).  Best quality.  SINC
          quality always start from 2 and above.

  Rough quality comparisons:
    - http://people.freebsd.org/~ariff/z_comparison/

3 Bit-perfect mode.  Bypasses all feeder/dsp effects.  Pure sound will be
  directly fed into the hardware.

4 Parametric (compile time) Software Equalizer (Bass/Treble mixer). Can
  be customized by defining FEEDER_EQ_PRESETS in /etc/make.conf.

5 Transparent/Adaptive Virtual Channel. Now you don't have to disable
  vchans in order to make digital format pass through.  It also makes
  vchans more dynamic by choosing a better format/rate among all the
  concurrent streams, which means that dev.pcm.X.play.vchanformat/rate
  becomes sort of optional.

6 Exclusive Stream, with special open() mode O_EXCL.  This will "mute"
  other concurrent vchan streams and only allow a single channel with
  O_EXCL set to keep producing sound.

Other Changes:
    * most feeder_* stuffs are compilable in userland. Let's not
      speculate whether we should go all out for it (save that for
      FreeBSD 16.0-RELEASE).
    * kobj signature fixups, thanks to Andriy Gapon <avg@freebsd.org>
    * pull out channel mixing logic out of vchan.c and create its own
      feeder_mixer for world justice.
    * various refactoring here and there, for good or bad.
    * activation of few more OSSv4 ioctls() (see [1] above).
    * opt_snd.h for possible compile time configuration:
      (mostly for debugging purposes, don't try these at home)
        SND_DEBUG
        SND_DIAGNOSTIC
        SND_FEEDER_MULTIFORMAT
        SND_FEEDER_FULL_MULTIFORMAT
        SND_FEEDER_RATE_HP
        SND_PCM_64
        SND_OLDSTEREO

Manual page updates are on the way.

Tested by:	joel, Olivier SMEDTS <olivier at gid0 d org>, too many
          	unsung / unnamed heroes.
2009-06-07 19:12:08 +00:00

254 lines
6.8 KiB
C

/*-
* Copyright (c) 1999 Seigo Tanimura
* Copyright (c) 2003 Mathew Kanner
* Copyright (c) 2003-2006 Yuriy Tsibizov <yuriy.tsibizov@gfk.ru>
* All rights reserved
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/bus.h>
#include <machine/bus.h>
#include <sys/rman.h>
#include <sys/systm.h>
#include <sys/sbuf.h>
#include <sys/queue.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_snd.h"
#endif
#include <dev/sound/chip.h>
#include <dev/sound/pcm/sound.h>
#include <dev/sound/midi/midi.h>
#include <dev/sound/midi/mpu401.h>
#include "mpufoi_if.h"
#include <dev/sound/pci/emu10kx.h>
#include "emu10k1-alsa%diked.h"
struct emu_midi_softc {
struct mtx mtx;
device_t dev;
struct mpu401 *mpu;
mpu401_intr_t *mpu_intr;
struct emu_sc_info *card;
int port; /* I/O port or I/O ptr reg */
int is_emu10k1;
int fflags; /* File flags */
int ihandle; /* interrupt manager handle */
};
static uint32_t emu_midi_card_intr(void *p, uint32_t arg);
static devclass_t emu_midi_devclass;
static unsigned char
emu_mread(struct mpu401 *arg __unused, void *cookie, int reg)
{
struct emu_midi_softc *sc = cookie;
unsigned int d;
d = 0;
if (sc->is_emu10k1)
d = emu_rd(sc->card, 0x18 + reg, 1);
else
d = emu_rdptr(sc->card, 0, sc->port + reg);
return (d);
}
static void
emu_mwrite(struct mpu401 *arg __unused, void *cookie, int reg, unsigned char b)
{
struct emu_midi_softc *sc = cookie;
if (sc->is_emu10k1)
emu_wr(sc->card, 0x18 + reg, b, 1);
else
emu_wrptr(sc->card, 0, sc->port + reg, b);
}
static int
emu_muninit(struct mpu401 *arg __unused, void *cookie)
{
struct emu_midi_softc *sc = cookie;
mtx_lock(&sc->mtx);
sc->mpu_intr = NULL;
mtx_unlock(&sc->mtx);
return (0);
}
static kobj_method_t emu_mpu_methods[] = {
KOBJMETHOD(mpufoi_read, emu_mread),
KOBJMETHOD(mpufoi_write, emu_mwrite),
KOBJMETHOD(mpufoi_uninit, emu_muninit),
KOBJMETHOD_END
};
static DEFINE_CLASS(emu_mpu, emu_mpu_methods, 0);
static uint32_t
emu_midi_card_intr(void *p, uint32_t intr_status)
{
struct emu_midi_softc *sc = (struct emu_midi_softc *)p;
if (sc->mpu_intr)
(sc->mpu_intr) (sc->mpu);
if (sc->mpu_intr == NULL) {
/* We should read MIDI event to unlock card after
* interrupt. XXX - check, why this happens. */
if (bootverbose)
device_printf(sc->dev, "midi interrupt %08x without interrupt handler, force mread!\n", intr_status);
(void)emu_mread((void *)(NULL), sc, 0);
}
return (intr_status); /* Acknowledge everything */
}
static void
emu_midi_intr(void *p)
{
(void)emu_midi_card_intr(p, 0);
}
static int
emu_midi_probe(device_t dev)
{
struct emu_midi_softc *scp;
uintptr_t func, r, is_emu10k1;
r = BUS_READ_IVAR(device_get_parent(dev), dev, 0, &func);
if (func != SCF_MIDI)
return (ENXIO);
scp = device_get_softc(dev);
bzero(scp, sizeof(*scp));
r = BUS_READ_IVAR(device_get_parent(dev), dev, EMU_VAR_ISEMU10K1, &is_emu10k1);
scp->is_emu10k1 = is_emu10k1 ? 1 : 0;
device_set_desc(dev, "EMU10Kx MIDI Interface");
return (0);
}
static int
emu_midi_attach(device_t dev)
{
struct emu_midi_softc * scp;
struct sndcard_func *func;
struct emu_midiinfo *midiinfo;
uint32_t inte_val, ipr_val;
scp = device_get_softc(dev);
func = device_get_ivars(dev);
scp->dev = dev;
midiinfo = (struct emu_midiinfo *)func->varinfo;
scp->port = midiinfo->port;
scp->card = midiinfo->card;
mtx_init(&scp->mtx, device_get_nameunit(dev), "midi softc", MTX_DEF);
if (scp->is_emu10k1) {
/* SB Live! - only one MIDI device here */
inte_val = 0;
/* inte_val |= INTE_MIDITXENABLE;*/
inte_val |= INTE_MIDIRXENABLE;
ipr_val = IPR_MIDITRANSBUFEMPTY;
ipr_val |= IPR_MIDIRECVBUFEMPTY;
} else {
if (scp->port == A_MUDATA1) {
/* EXTERNAL MIDI (AudigyDrive) */
inte_val = 0;
/* inte_val |= A_INTE_MIDITXENABLE1;*/
inte_val |= INTE_MIDIRXENABLE;
ipr_val = IPR_MIDITRANSBUFEMPTY;
ipr_val |= IPR_MIDIRECVBUFEMPTY;
} else {
/* MIDI hw config port 2 */
inte_val = 0;
/* inte_val |= A_INTE_MIDITXENABLE2;*/
inte_val |= INTE_A_MIDIRXENABLE2;
ipr_val = IPR_A_MIDITRANSBUFEMPTY2;
ipr_val |= IPR_A_MIDIRECVBUFEMPTY2;
}
}
scp->ihandle = emu_intr_register(scp->card, inte_val, ipr_val, &emu_midi_card_intr, scp);
/* Init the interface. */
scp->mpu = mpu401_init(&emu_mpu_class, scp, emu_midi_intr, &scp->mpu_intr);
if (scp->mpu == NULL) {
emu_intr_unregister(scp->card, scp->ihandle);
mtx_destroy(&scp->mtx);
return (ENOMEM);
}
/*
* XXX I don't know how to check for Live!Drive / AudigyDrive
* presence. Let's hope that IR enabling code will not harm if
* it is not present.
*/
if (scp->is_emu10k1)
emu_enable_ir(scp->card);
else {
if (scp->port == A_MUDATA1)
emu_enable_ir(scp->card);
}
return (0);
}
static int
emu_midi_detach(device_t dev)
{
struct emu_midi_softc *scp;
scp = device_get_softc(dev);
mpu401_uninit(scp->mpu);
emu_intr_unregister(scp->card, scp->ihandle);
mtx_destroy(&scp->mtx);
return (0);
}
static device_method_t emu_midi_methods[] = {
DEVMETHOD(device_probe, emu_midi_probe),
DEVMETHOD(device_attach, emu_midi_attach),
DEVMETHOD(device_detach, emu_midi_detach),
{0, 0},
};
static driver_t emu_midi_driver = {
"midi",
emu_midi_methods,
sizeof(struct emu_midi_softc),
};
DRIVER_MODULE(snd_emu10kx_midi, emu10kx, emu_midi_driver, emu_midi_devclass, 0, 0);
MODULE_DEPEND(snd_emu10kx_midi, snd_emu10kx, SND_EMU10KX_MINVER, SND_EMU10KX_PREFVER, SND_EMU10KX_MAXVER);
MODULE_DEPEND(snd_emu10kx_midi, sound, SOUND_MINVER, SOUND_PREFVER, SOUND_MAXVER);
MODULE_VERSION(snd_emu10kx_midi, SND_EMU10KX_PREFVER);