2005-01-06 22:18:23 +00:00
|
|
|
/*-
|
1993-11-09 02:32:30 +00:00
|
|
|
* spkr.c -- device driver for console speaker
|
1993-06-12 14:58:17 +00:00
|
|
|
*
|
1993-11-09 02:32:30 +00:00
|
|
|
* v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
|
|
|
|
* modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
|
2002-11-02 04:18:10 +00:00
|
|
|
* modified for PC98 by Kakefuda
|
1993-06-12 14:58:17 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-02 16:32:55 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1994-08-13 03:50:34 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
2000-06-23 07:44:33 +00:00
|
|
|
#include <sys/module.h>
|
1994-08-13 03:50:34 +00:00
|
|
|
#include <sys/uio.h>
|
1995-12-08 11:19:42 +00:00
|
|
|
#include <sys/conf.h>
|
1999-11-03 17:54:26 +00:00
|
|
|
#include <sys/ctype.h>
|
2000-05-05 09:05:39 +00:00
|
|
|
#include <sys/malloc.h>
|
1995-03-16 18:17:34 +00:00
|
|
|
#include <machine/clock.h>
|
2005-11-11 09:57:32 +00:00
|
|
|
#include <dev/speaker/speaker.h>
|
1993-06-12 14:58:17 +00:00
|
|
|
|
1995-12-08 11:19:42 +00:00
|
|
|
static d_open_t spkropen;
|
|
|
|
static d_close_t spkrclose;
|
|
|
|
static d_write_t spkrwrite;
|
|
|
|
static d_ioctl_t spkrioctl;
|
|
|
|
|
1999-05-30 16:53:49 +00:00
|
|
|
static struct cdevsw spkr_cdevsw = {
|
2004-02-21 21:10:55 +00:00
|
|
|
.d_version = D_VERSION,
|
|
|
|
.d_flags = D_NEEDGIANT,
|
2003-03-03 12:15:54 +00:00
|
|
|
.d_open = spkropen,
|
|
|
|
.d_close = spkrclose,
|
|
|
|
.d_write = spkrwrite,
|
|
|
|
.d_ioctl = spkrioctl,
|
|
|
|
.d_name = "spkr",
|
1999-05-30 16:53:49 +00:00
|
|
|
};
|
1995-09-03 05:43:50 +00:00
|
|
|
|
2000-12-08 20:09:00 +00:00
|
|
|
static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
|
2000-05-05 09:05:39 +00:00
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
**************** MACHINE DEPENDENT PART STARTS HERE *************************
|
1993-06-12 14:58:17 +00:00
|
|
|
* This section defines a function tone() which causes a tone of given
|
1996-10-25 14:00:47 +00:00
|
|
|
* frequency and duration from the ISA console speaker.
|
1993-06-12 14:58:17 +00:00
|
|
|
* 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
|
1996-10-25 14:00:47 +00:00
|
|
|
* Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
|
1993-06-12 14:58:17 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1993-11-15 01:33:11 +00:00
|
|
|
#define SPKRPRI PSOCK
|
|
|
|
static char endtone, endrest;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2007-06-04 08:33:18 +00:00
|
|
|
static void tone(unsigned int thz, unsigned int centisecs);
|
|
|
|
static void rest(int centisecs);
|
2002-03-20 07:51:46 +00:00
|
|
|
static void playinit(void);
|
|
|
|
static void playtone(int pitch, int value, int sustain);
|
|
|
|
static void playstring(char *cp, size_t slen);
|
1995-12-15 00:54:32 +00:00
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
* Emit tone of frequency thz for given number of centisecs
|
|
|
|
*/
|
1996-07-20 18:48:54 +00:00
|
|
|
static void
|
2008-05-15 01:22:48 +00:00
|
|
|
tone(unsigned int thz, unsigned int centisecs)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2008-05-15 01:22:48 +00:00
|
|
|
int sps, timo;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
if (thz <= 0)
|
|
|
|
return;
|
1995-05-05 06:15:11 +00:00
|
|
|
|
1993-06-12 14:58:17 +00:00
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("tone: thz=%d centisecs=%d\n", thz, centisecs);
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/* set timer to generate clicks at given frequency in Hertz */
|
|
|
|
sps = splclock();
|
1994-04-21 14:21:50 +00:00
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
if (timer_spkr_acquire()) {
|
|
|
|
/* enter list of waiting procs ??? */
|
|
|
|
splx(sps);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
splx(sps);
|
|
|
|
disable_intr();
|
|
|
|
timer_spkr_setfreq(thz);
|
|
|
|
enable_intr();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set timeout to endtone function, then give up the timeslice.
|
|
|
|
* This is so other processes can execute while the tone is being
|
|
|
|
* emitted.
|
|
|
|
*/
|
|
|
|
timo = centisecs * hz / 100;
|
|
|
|
if (timo > 0)
|
|
|
|
tsleep(&endtone, SPKRPRI | PCATCH, "spkrtn", timo);
|
|
|
|
sps = splclock();
|
|
|
|
timer_spkr_release();
|
1996-07-20 18:48:54 +00:00
|
|
|
splx(sps);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
* Rest for given number of centisecs
|
|
|
|
*/
|
1996-07-20 18:48:54 +00:00
|
|
|
static void
|
2008-05-15 01:22:48 +00:00
|
|
|
rest(int centisecs)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2008-05-15 01:22:48 +00:00
|
|
|
int timo;
|
2007-06-04 08:33:18 +00:00
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
* Set timeout to endrest function, then give up the timeslice.
|
|
|
|
* This is so other processes can execute while the rest is being
|
|
|
|
* waited out.
|
|
|
|
*/
|
1993-06-12 14:58:17 +00:00
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("rest: %d\n", centisecs);
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
2008-05-15 01:22:48 +00:00
|
|
|
timo = centisecs * hz / 100;
|
|
|
|
if (timo > 0)
|
|
|
|
tsleep(&endrest, SPKRPRI | PCATCH, "spkrrs", timo);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
**************** PLAY STRING INTERPRETER BEGINS HERE **********************
|
1993-06-12 14:58:17 +00:00
|
|
|
* Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
|
1993-11-09 02:32:30 +00:00
|
|
|
* M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
|
|
|
|
* tracking facility are added.
|
1993-06-12 14:58:17 +00:00
|
|
|
* 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,
|
|
|
|
};
|
|
|
|
|
1996-07-20 18:48:54 +00:00
|
|
|
static void
|
|
|
|
playinit()
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
octave = DFLT_OCTAVE;
|
2007-06-04 09:27:13 +00:00
|
|
|
whole = (100 * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
|
1993-06-12 14:58:17 +00:00
|
|
|
fill = NORMAL;
|
|
|
|
value = DFLT_VALUE;
|
|
|
|
octtrack = FALSE;
|
|
|
|
octprefix = TRUE; /* act as though there was an initial O(n) */
|
|
|
|
}
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
* Play tone of proper duration for current rhythm signature
|
|
|
|
*/
|
1996-07-20 18:48:54 +00:00
|
|
|
static void
|
2008-05-15 01:22:48 +00:00
|
|
|
playtone(int pitch, int value, int sustain)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2008-05-15 01:22:48 +00:00
|
|
|
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);
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
|
1993-06-12 14:58:17 +00:00
|
|
|
pitch, sound, silence);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
tone(pitchtab[pitch], sound);
|
|
|
|
if (fill != LEGATO)
|
|
|
|
rest(silence);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
* Interpret and play an item from a notation string
|
|
|
|
*/
|
1996-07-20 18:48:54 +00:00
|
|
|
static void
|
2008-05-15 01:22:48 +00:00
|
|
|
playstring(char *cp, size_t slen)
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
2008-05-15 01:22:48 +00:00
|
|
|
int pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
#define GETNUM(cp, v) for(v=0; isdigit(cp[1]) && slen > 0; ) \
|
|
|
|
{v = v * 10 + (*++cp - '0'); slen--;}
|
2008-05-15 01:22:48 +00:00
|
|
|
for (; slen--; cp++) {
|
|
|
|
int sustain, timeval, tempo;
|
|
|
|
register char c = toupper(*cp);
|
1993-06-12 14:58:17 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("playstring: %c (%x)\n", c, c);
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
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 = (100 * 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;
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-15 01:22:48 +00:00
|
|
|
/*
|
|
|
|
* ****************** UNIX DRIVER HOOKS BEGIN HERE **************************
|
1993-06-12 14:58:17 +00:00
|
|
|
* This section implements driver hooks to run playstring() and the tone(),
|
|
|
|
* endtone(), and rest() functions defined above.
|
|
|
|
*/
|
|
|
|
|
1993-11-09 02:32:30 +00:00
|
|
|
static int spkr_active = FALSE; /* exclusion flag */
|
2000-05-05 09:05:39 +00:00
|
|
|
static char *spkr_inbuf; /* incoming buf */
|
1993-06-12 14:58:17 +00:00
|
|
|
|
2002-10-16 10:16:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
spkropen(dev, flags, fmt, td)
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2008-05-15 01:22:48 +00:00
|
|
|
int flags;
|
|
|
|
int fmt;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td;
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2009-01-25 09:20:59 +00:00
|
|
|
if (spkr_active)
|
2008-05-15 01:22:48 +00:00
|
|
|
return(EBUSY);
|
|
|
|
else {
|
1993-11-09 02:32:30 +00:00
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("spkropen: about to perform play initialization\n");
|
1993-11-09 02:32:30 +00:00
|
|
|
#endif /* DEBUG */
|
2008-05-15 01:22:48 +00:00
|
|
|
playinit();
|
|
|
|
spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
|
|
|
|
spkr_active = TRUE;
|
|
|
|
return(0);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 10:16:17 +00:00
|
|
|
static int
|
1996-07-20 18:48:54 +00:00
|
|
|
spkrwrite(dev, uio, ioflag)
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2008-05-15 01:22:48 +00:00
|
|
|
struct uio *uio;
|
|
|
|
int ioflag;
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2009-06-25 18:46:30 +00:00
|
|
|
printf("spkrwrite: entering with dev = %s, count = %zd\n",
|
1999-08-17 20:25:50 +00:00
|
|
|
devtoname(dev), uio->uio_resid);
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
2009-01-25 09:20:59 +00:00
|
|
|
|
|
|
|
if (uio->uio_resid > (DEV_BSIZE - 1)) /* prevent system crashes */
|
2008-05-15 01:22:48 +00:00
|
|
|
return(E2BIG);
|
|
|
|
else {
|
|
|
|
unsigned n;
|
|
|
|
char *cp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
n = uio->uio_resid;
|
|
|
|
cp = spkr_inbuf;
|
|
|
|
error = uiomove(cp, n, uio);
|
|
|
|
if (!error) {
|
|
|
|
cp[n] = '\0';
|
|
|
|
playstring(cp, n);
|
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
return(error);
|
2008-05-15 01:22:48 +00:00
|
|
|
}
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 10:16:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
spkrclose(dev, flags, fmt, td)
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2008-05-15 01:22:48 +00:00
|
|
|
int flags;
|
|
|
|
int fmt;
|
|
|
|
struct thread *td;
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2009-01-25 09:20:59 +00:00
|
|
|
wakeup(&endtone);
|
|
|
|
wakeup(&endrest);
|
|
|
|
free(spkr_inbuf, M_SPKR);
|
|
|
|
spkr_active = FALSE;
|
|
|
|
return(0);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 10:16:17 +00:00
|
|
|
static int
|
2001-09-12 08:38:13 +00:00
|
|
|
spkrioctl(dev, cmd, cmdarg, flags, td)
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev;
|
2008-05-15 01:22:48 +00:00
|
|
|
unsigned long cmd;
|
|
|
|
caddr_t cmdarg;
|
|
|
|
int flags;
|
|
|
|
struct thread *td;
|
1993-06-12 14:58:17 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2008-05-15 01:22:48 +00:00
|
|
|
(void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
|
|
|
|
devtoname(dev), cmd);
|
1993-06-12 14:58:17 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
2009-01-25 09:20:59 +00:00
|
|
|
if (cmd == SPKRTONE) {
|
2008-05-15 01:22:48 +00:00
|
|
|
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);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
2008-05-15 01:22:48 +00:00
|
|
|
return(EINVAL);
|
1993-06-12 14:58:17 +00:00
|
|
|
}
|
|
|
|
|
2004-06-16 09:47:26 +00:00
|
|
|
static struct cdev *speaker_dev;
|
2002-01-23 01:11:52 +00:00
|
|
|
|
2008-03-26 21:33:41 +00:00
|
|
|
/*
|
|
|
|
* Module handling
|
|
|
|
*/
|
2000-06-23 07:44:33 +00:00
|
|
|
static int
|
2008-03-26 21:33:41 +00:00
|
|
|
speaker_modevent(module_t mod, int type, void *data)
|
2000-06-23 07:44:33 +00:00
|
|
|
{
|
2008-03-26 21:33:41 +00:00
|
|
|
int error = 0;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case MOD_LOAD:
|
|
|
|
speaker_dev = make_dev(&spkr_cdevsw, 0,
|
|
|
|
UID_ROOT, GID_WHEEL, 0600, "speaker");
|
|
|
|
break;
|
|
|
|
case MOD_SHUTDOWN:
|
|
|
|
case MOD_UNLOAD:
|
|
|
|
destroy_dev(speaker_dev);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EOPNOTSUPP;
|
2002-10-28 02:00:09 +00:00
|
|
|
}
|
2008-03-26 21:33:41 +00:00
|
|
|
return (error);
|
2000-06-23 07:44:33 +00:00
|
|
|
}
|
|
|
|
|
2008-03-26 21:33:41 +00:00
|
|
|
DEV_MODULE(speaker, speaker_modevent, NULL);
|