File:  [DragonFly] / src / sys / dev / sound / isa / i386 / spkr / Attic / spkr.c
Revision 1.9: download - view: text, annotated - select for diffs
Fri Jan 30 05:42:16 2004 UTC (10 years, 5 months ago) by dillon
Branches: MAIN
CVS tags: HEAD
This commit represents a major revamping of the clock interrupt and timebase
infrastructure in DragonFly.

* Rip out the existing 8254 timer 0 code, and also disable the use of
  Timer 2 (which means that the PC speaker will no longer go beep).  Timer 0
  used to represent a periodic interrupt and a great deal of code was in
  place to attempt to obtain a timebase off of that periodic interrupt.

  Timer 0 is now used in software retriggerable one-shot mode to produce
  variable-delay interrupts.  A new hardware interrupt clock abstraction
  called SYSTIMERS has been introduced which allows threads to register
  periodic or one-shot interrupt/IPI callbacks at approximately 1uS
  granularity.

  Timer 2 is now set in continuous periodic mode with a period of 65536
  and provides the timebase for the system, abstracted to 32 bits.

  All the old platform-integrated hardclock() and statclock() code has
  been rewritten.  The old IPI forwarding code has been #if 0'd out and
  will soon be entirely removed (the systimer abstraction takes care of
  multi-cpu registrations now).  The architecture-specific clkintr() now
  simply calls an entry point into the systimer and provides a Timer 0
  reload and Timer 2 timebase function API.

* On both UP and SMP systems, cpus register systimer interrupts for the Hz
  interrupt, the stat interrupt, and the scheduler round-robin interrupt.
  The abstraction is carefully designed to allow multiple interrupts occuring
  at the same time to be processed in a single hardware interrupt.  While
  we currently use IPI's to distribute requested interrupts from other cpu's,
  the intent is to use the abstraction to take advantage of per-cpu timers
  when available (e.g. on the LAPIC) in the future.

  systimer interrupts run OUTSIDE THE MP LOCK.  Entry points may be called
  from the hard interrupt or via an IPI message (IPI messages have always
  run outside the MP lock).

* Rip out timecounters and disable alternative timecounter code for other
  time sources.  This is temporary.  Eventually other time sources, such as
  the TSC, will be reintegrated as independant, parallel-running entities.
  There will be no 'time switching' per-say, subsystems will be able to
  select which timebase they wish to use.  It is desireable to reintegrate
  at least the TSC to improve [get]{micro,nano}[up]time() performance.

  WARNING: PPS events may not work properly.  They were not removed, but
  they have not been retested with the new code either.

* Remove spl protection around [get]{micro,nano}[up]time() calls, they are
  now internally protected.

* Use uptime instead of realtime in certain CAM timeout tests

* Remove struct clockframe.  Use struct intrframe everywhere where clockframe
  used to be used.

* Replace most splstatclock() protections with crit_*() protections, because
  such protections must now also protect against IPI messaging interrupts.

* Add fields to the per-cpu globaldata structure to access timebase related
  information using only a critical section rather then a mutex.  However,
  the 8254 Timer 2 access code still uses spin locks.  More work needs to
  be done here, the 'realtime' correction is still done in a single global
  'struct timespec basetime' structure.

* Remove the CLKINTR_PENDING icu and apic interrupt hacks.

* Augment the IPI Messaging code to make an intrframe available to callbacks.

* Document 8254 timing modes in i386/sai/timerreg.h.  Note that at the
  moment we assume an 8254 instead of an 8253 as we are using TIMER_SWSTROBE
  mode.  This may or may not have to be changed to an 8253 mode.

* Integrate the NTP correction code into the new timebase subsystem.

* Separate boottime from basettime.  Once boottime is believed to be stable
  it is no longer effected by NTP or other time corrections.

CAVETS:

	* PC speaker no longer works

	* Profiling interrupt rate not increased (it needs work to be
	  made operational on a per-cpu basis rather then system-wide).

	* The native timebase API is function-based, but currently hardwired.

	* There might or might not be issues with 486 systems due to the
	  timer mode I am using.

/*
 * spkr.c -- device driver for console speaker
 *
 * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
 * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
 *
 * $FreeBSD: src/sys/i386/isa/spkr.c,v 1.45 2000/01/29 16:00:32 peter Exp $
 * $DragonFly: src/sys/dev/sound/isa/i386/spkr/spkr.c,v 1.9 2004/01/30 05:42:16 dillon Exp $
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/buf.h>
#include <sys/uio.h>
#include <sys/conf.h>
#include <sys/ctype.h>
#include <sys/systimer.h>
#include <bus/isa/i386/isa.h>
#include <i386/isa/timerreg.h>
#include <machine/clock.h>
#include <machine/speaker.h>

static	d_open_t	spkropen;
static	d_close_t	spkrclose;
static	d_write_t	spkrwrite;
static	d_ioctl_t	spkrioctl;

#define CDEV_MAJOR 26
static struct cdevsw spkr_cdevsw = {
	/* name */	"spkr",
	/* maj */	CDEV_MAJOR,
	/* flags */	0,
	/* port */	NULL,
	/* autoq */	0,

	/* open */	spkropen,
	/* close */	spkrclose,
	/* read */	noread,
	/* write */	spkrwrite,
	/* ioctl */	spkrioctl,
	/* poll */	nopoll,
	/* mmap */	nommap,
	/* strategy */	nostrategy,
	/* dump */	nodump,
	/* psize */	nopsize
};

/**************** MACHINE DEPENDENT PART STARTS HERE *************************
 *
 * This section defines a function tone() which causes a tone of given
 * frequency and duration from the ISA console speaker.
 * Another function endtone() is defined to force sound off, and there is
 * also a rest() entry point to do pauses.
 *
 * Audible sound is generated using the Programmable Interval Timer (PIT) and
 * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
 * PPI controls whether sound is passed through at all; the PIT's channel 2 is
 * used to generate clicks (a square wave) of whatever frequency is desired.
 */

/*
 * PPI control values.
 * XXX should be in a header and used in clock.c.
 */
#define PPI_SPKR	0x03	/* turn these PPI bits on to pass sound */

static char endtone, endrest;

static void tone (unsigned int thz, unsigned int ticks);
static void rest (int ticks);
static void playinit (void);
static void playtone (int pitch, int value, int sustain);
static int abs (int n);
static void playstring (char *cp, size_t slen);

/* emit tone of frequency thz for given number of ticks */
static void
tone(thz, ticks)
	unsigned int thz, ticks;
{
    unsigned int divisor;

    if (thz <= 0)
	return;

    divisor = cputimer_freq / thz;

#ifdef DEBUG
    (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
#endif /* DEBUG */

    /* set timer to generate clicks at given frequency in Hertz */

    if (acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT)) {
	/* enter list of waiting procs ??? */
	return;
    }
    clock_lock();
    outb(TIMER_CNTR2, (divisor & 0xff));	/* send lo byte */
    outb(TIMER_CNTR2, (divisor >> 8));	/* send hi byte */
    clock_unlock();

    /* turn the speaker on */
    outb(IO_PPI, inb(IO_PPI) | PPI_SPKR);

    /*
     * Set timeout to endtone function, then give up the timeslice.
     * This is so other processes can execute while the tone is being
     * emitted.
     */
    if (ticks > 0)
	tsleep((caddr_t)&endtone, PCATCH, "spkrtn", ticks);
    outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR);
    release_timer2();
}

/* rest for given number of ticks */
static void
rest(ticks)
	int	ticks;
{
    /*
     * Set timeout to endrest function, then give up the timeslice.
     * This is so other processes can execute while the rest is being
     * waited out.
     */
#ifdef DEBUG
    (void) printf("rest: %d\n", ticks);
#endif /* DEBUG */
    if (ticks > 0)
	tsleep((caddr_t)&endrest, PCATCH, "spkrrs", ticks);
}

/**************** PLAY STRING INTERPRETER BEGINS HERE **********************
 *
 * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
 * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
 * tracking facility are added.
 * Requires tone(), rest(), and endtone(). String play is not interruptible
 * except possibly at physical block boundaries.
 */

typedef int	bool;
#define TRUE	1
#define FALSE	0

#define dtoi(c)		((c) - '0')

static int octave;	/* currently selected octave */
static int whole;	/* whole-note time at current tempo, in ticks */
static int value;	/* whole divisor for note time, quarter note = 1 */
static int fill;	/* controls spacing of notes */
static bool octtrack;	/* octave-tracking on? */
static bool octprefix;	/* override current octave-tracking state? */

/*
 * Magic number avoidance...
 */
#define SECS_PER_MIN	60	/* seconds per minute */
#define WHOLE_NOTE	4	/* quarter notes per whole note */
#define MIN_VALUE	64	/* the most we can divide a note by */
#define DFLT_VALUE	4	/* default value (quarter-note) */
#define FILLTIME	8	/* for articulation, break note in parts */
#define STACCATO	6	/* 6/8 = 3/4 of note is filled */
#define NORMAL		7	/* 7/8ths of note interval is filled */
#define LEGATO		8	/* all of note interval is filled */
#define DFLT_OCTAVE	4	/* default octave */
#define MIN_TEMPO	32	/* minimum tempo */
#define DFLT_TEMPO	120	/* default tempo */
#define MAX_TEMPO	255	/* max tempo */
#define NUM_MULT	3	/* numerator of dot multiplier */
#define DENOM_MULT	2	/* denominator of dot multiplier */

/* letter to half-tone:  A   B  C  D  E  F  G */
static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};

/*
 * This is the American Standard A440 Equal-Tempered scale with frequencies
 * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
 * our octave 0 is standard octave 2.
 */
#define OCTAVE_NOTES	12	/* semitones per octave */
static int pitchtab[] =
{
/*        C     C#    D     D#    E     F     F#    G     G#    A     A#    B*/
/* 0 */   65,   69,   73,   78,   82,   87,   93,   98,  103,  110,  117,  123,
/* 1 */  131,  139,  147,  156,  165,  175,  185,  196,  208,  220,  233,  247,
/* 2 */  262,  277,  294,  311,  330,  349,  370,  392,  415,  440,  466,  494,
/* 3 */  523,  554,  587,  622,  659,  698,  740,  784,  831,  880,  932,  988,
/* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
/* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
/* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
};

static void
playinit()
{
    octave = DFLT_OCTAVE;
    whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
    fill = NORMAL;
    value = DFLT_VALUE;
    octtrack = FALSE;
    octprefix = TRUE;	/* act as though there was an initial O(n) */
}

/* play tone of proper duration for current rhythm signature */
static void
playtone(pitch, value, sustain)
	int	pitch, value, sustain;
{
    register int	sound, silence, snum = 1, sdenom = 1;

    /* this weirdness avoids floating-point arithmetic */
    for (; sustain; sustain--)
    {
	/* See the BUGS section in the man page for discussion */
	snum *= NUM_MULT;
	sdenom *= DENOM_MULT;
    }

    if (value == 0 || sdenom == 0)
	return;

    if (pitch == -1)
	rest(whole * snum / (value * sdenom));
    else
    {
	sound = (whole * snum) / (value * sdenom)
		- (whole * (FILLTIME - fill)) / (value * FILLTIME);
	silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);

#ifdef DEBUG
	(void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
			pitch, sound, silence);
#endif /* DEBUG */

	tone(pitchtab[pitch], sound);
	if (fill != LEGATO)
	    rest(silence);
    }
}

static int
abs(n)
	int n;
{
    if (n < 0)
	return(-n);
    else
	return(n);
}

/* interpret and play an item from a notation string */
static void
playstring(cp, slen)
	char	*cp;
	size_t	slen;
{
    int		pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;

#define GETNUM(cp, v)	for(v=0; isdigit(cp[1]) && slen > 0; ) \
				{v = v * 10 + (*++cp - '0'); slen--;}
    for (; slen--; cp++)
    {
	int		sustain, timeval, tempo;
	register char	c = toupper(*cp);

#ifdef DEBUG
	(void) printf("playstring: %c (%x)\n", c, c);
#endif /* DEBUG */

	switch (c)
	{
	case 'A':  case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':

	    /* compute pitch */
	    pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;

	    /* this may be followed by an accidental sign */
	    if (cp[1] == '#' || cp[1] == '+')
	    {
		++pitch;
		++cp;
		slen--;
	    }
	    else if (cp[1] == '-')
	    {
		--pitch;
		++cp;
		slen--;
	    }

	    /*
	     * If octave-tracking mode is on, and there has been no octave-
	     * setting prefix, find the version of the current letter note
	     * closest to the last regardless of octave.
	     */
	    if (octtrack && !octprefix)
	    {
		if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
		{
		    ++octave;
		    pitch += OCTAVE_NOTES;
		}

		if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
		{
		    --octave;
		    pitch -= OCTAVE_NOTES;
		}
	    }
	    octprefix = FALSE;
	    lastpitch = pitch;

	    /* ...which may in turn be followed by an override time value */
	    GETNUM(cp, timeval);
	    if (timeval <= 0 || timeval > MIN_VALUE)
		timeval = value;

	    /* ...and/or sustain dots */
	    for (sustain = 0; cp[1] == '.'; cp++)
	    {
		slen--;
		sustain++;
	    }

	    /* ...and/or a slur mark */
	    oldfill = fill;
	    if (cp[1] == '_')
	    {
		fill = LEGATO;
		++cp;
		slen--;
	    }

	    /* time to emit the actual tone */
	    playtone(pitch, timeval, sustain);

	    fill = oldfill;
	    break;

	case 'O':
	    if (cp[1] == 'N' || cp[1] == 'n')
	    {
		octprefix = octtrack = FALSE;
		++cp;
		slen--;
	    }
	    else if (cp[1] == 'L' || cp[1] == 'l')
	    {
		octtrack = TRUE;
		++cp;
		slen--;
	    }
	    else
	    {
		GETNUM(cp, octave);
		if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
		    octave = DFLT_OCTAVE;
		octprefix = TRUE;
	    }
	    break;

	case '>':
	    if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
		octave++;
	    octprefix = TRUE;
	    break;

	case '<':
	    if (octave > 0)
		octave--;
	    octprefix = TRUE;
	    break;

	case 'N':
	    GETNUM(cp, pitch);
	    for (sustain = 0; cp[1] == '.'; cp++)
	    {
		slen--;
		sustain++;
	    }
	    oldfill = fill;
	    if (cp[1] == '_')
	    {
		fill = LEGATO;
		++cp;
		slen--;
	    }
	    playtone(pitch - 1, value, sustain);
	    fill = oldfill;
	    break;

	case 'L':
	    GETNUM(cp, value);
	    if (value <= 0 || value > MIN_VALUE)
		value = DFLT_VALUE;
	    break;

	case 'P':
	case '~':
	    /* this may be followed by an override time value */
	    GETNUM(cp, timeval);
	    if (timeval <= 0 || timeval > MIN_VALUE)
		timeval = value;
	    for (sustain = 0; cp[1] == '.'; cp++)
	    {
		slen--;
		sustain++;
	    }
	    playtone(-1, timeval, sustain);
	    break;

	case 'T':
	    GETNUM(cp, tempo);
	    if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
		tempo = DFLT_TEMPO;
	    whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
	    break;

	case 'M':
	    if (cp[1] == 'N' || cp[1] == 'n')
	    {
		fill = NORMAL;
		++cp;
		slen--;
	    }
	    else if (cp[1] == 'L' || cp[1] == 'l')
	    {
		fill = LEGATO;
		++cp;
		slen--;
	    }
	    else if (cp[1] == 'S' || cp[1] == 's')
	    {
		fill = STACCATO;
		++cp;
		slen--;
	    }
	    break;
	}
    }
}

/******************* UNIX DRIVER HOOKS BEGIN HERE **************************
 *
 * This section implements driver hooks to run playstring() and the tone(),
 * endtone(), and rest() functions defined above.
 */

static int spkr_active = FALSE; /* exclusion flag */
static struct buf *spkr_inbuf;  /* incoming buf */

int
spkropen(dev_t dev, int	flags, int fmt, struct thread *td)
{
#ifdef DEBUG
    (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
#endif /* DEBUG */

    if (minor(dev) != 0)
	return(ENXIO);
    else if (spkr_active)
	return(EBUSY);
    else
    {
#ifdef DEBUG
	(void) printf("spkropen: about to perform play initialization\n");
#endif /* DEBUG */
	playinit();
	spkr_inbuf = geteblk(DEV_BSIZE);
	spkr_active = TRUE;
	return(0);
    }
}

int
spkrwrite(dev, uio, ioflag)
	dev_t		dev;
	struct uio	*uio;
	int		ioflag;
{
#ifdef DEBUG
    printf("spkrwrite: entering with dev = %s, count = %d\n",
		devtoname(dev), uio->uio_resid);
#endif /* DEBUG */

    if (minor(dev) != 0)
	return(ENXIO);
    else if (uio->uio_resid > (DEV_BSIZE - 1))     /* prevent system crashes */
	return(E2BIG);
    else
    {
	unsigned n;
	char *cp;
	int error;

	n = uio->uio_resid;
	cp = spkr_inbuf->b_data;
	error = uiomove(cp, n, uio);
	if (!error) {
		cp[n] = '\0';
		playstring(cp, n);
	}
	return(error);
    }
}

int
spkrclose(dev_t	dev, int flags, int fmt, struct thread *td)
{
#ifdef DEBUG
    (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
#endif /* DEBUG */

    if (minor(dev) != 0)
	return(ENXIO);
    else
    {
	wakeup((caddr_t)&endtone);
	wakeup((caddr_t)&endrest);
	brelse(spkr_inbuf);
	spkr_active = FALSE;
	return(0);
    }
}

int
spkrioctl(dev_t	dev, unsigned long cmd, caddr_t	cmdarg, int flags, struct thread*td)
{
#ifdef DEBUG
    (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
    	devtoname(dev), cmd);
#endif /* DEBUG */

    if (minor(dev) != 0)
	return(ENXIO);
    else if (cmd == SPKRTONE)
    {
	tone_t	*tp = (tone_t *)cmdarg;

	if (tp->frequency == 0)
	    rest(tp->duration);
	else
	    tone(tp->frequency, tp->duration);
	return 0;
    }
    else if (cmd == SPKRTUNE)
    {
	tone_t  *tp = (tone_t *)(*(caddr_t *)cmdarg);
	tone_t ttp;
	int error;

	for (; ; tp++) {
	    error = copyin(tp, &ttp, sizeof(tone_t));
	    if (error)
		    return(error);
	    if (ttp.duration == 0)
		    break;
	    if (ttp.frequency == 0)
		 rest(ttp.duration);
	    else
		 tone(ttp.frequency, ttp.duration);
	}
	return(0);
    }
    return(EINVAL);
}

static void
spkr_drvinit(void *unused)
{
	make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, "speaker");
}

SYSINIT(spkrdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,spkr_drvinit,NULL)


/* spkr.c ends here */