xref: /freebsd/sys/dev/speaker/spkr.c (revision ce6d929bb55d3b9d2b7e481590b92532e4d9c3b8)
15b81b6b3SRodney W. Grimes /*
2e614aa8aSAndrey A. Chernov  * spkr.c -- device driver for console speaker
35b81b6b3SRodney W. Grimes  *
4e614aa8aSAndrey A. Chernov  * v1.4 by Eric S. Raymond (esr@snark.thyrsus.com) Aug 1993
5e614aa8aSAndrey A. Chernov  * modified for FreeBSD by Andrew A. Chernov <ache@astral.msk.su>
66f78ca60SRodney W. Grimes  *
7c3aac50fSPeter Wemm  * $FreeBSD$
85b81b6b3SRodney W. Grimes  */
95b81b6b3SRodney W. Grimes 
10f540b106SGarrett Wollman #include <sys/param.h>
11f540b106SGarrett Wollman #include <sys/systm.h>
122a50a6d7SMike Smith #include <sys/bus.h>
13f540b106SGarrett Wollman #include <sys/kernel.h>
142a50a6d7SMike Smith #include <sys/module.h>
15f540b106SGarrett Wollman #include <sys/uio.h>
1687f6c662SJulian Elischer #include <sys/conf.h>
175b664c7cSPoul-Henning Kamp #include <sys/ctype.h>
18e4961fbfSPoul-Henning Kamp #include <sys/malloc.h>
192a50a6d7SMike Smith #include <isa/isavar.h>
20f540b106SGarrett Wollman #include <i386/isa/isa.h>
21f540b106SGarrett Wollman #include <i386/isa/timerreg.h>
22b5e8ce9fSBruce Evans #include <machine/clock.h>
23f540b106SGarrett Wollman #include <machine/speaker.h>
245b81b6b3SRodney W. Grimes 
2587f6c662SJulian Elischer static	d_open_t	spkropen;
2687f6c662SJulian Elischer static	d_close_t	spkrclose;
2787f6c662SJulian Elischer static	d_write_t	spkrwrite;
2887f6c662SJulian Elischer static	d_ioctl_t	spkrioctl;
2987f6c662SJulian Elischer 
3087f6c662SJulian Elischer #define CDEV_MAJOR 26
314e2f199eSPoul-Henning Kamp static struct cdevsw spkr_cdevsw = {
324e2f199eSPoul-Henning Kamp 	/* open */	spkropen,
334e2f199eSPoul-Henning Kamp 	/* close */	spkrclose,
344e2f199eSPoul-Henning Kamp 	/* read */	noread,
354e2f199eSPoul-Henning Kamp 	/* write */	spkrwrite,
364e2f199eSPoul-Henning Kamp 	/* ioctl */	spkrioctl,
374e2f199eSPoul-Henning Kamp 	/* poll */	nopoll,
384e2f199eSPoul-Henning Kamp 	/* mmap */	nommap,
394e2f199eSPoul-Henning Kamp 	/* strategy */	nostrategy,
404e2f199eSPoul-Henning Kamp 	/* name */	"spkr",
414e2f199eSPoul-Henning Kamp 	/* maj */	CDEV_MAJOR,
424e2f199eSPoul-Henning Kamp 	/* dump */	nodump,
434e2f199eSPoul-Henning Kamp 	/* psize */	nopsize,
444e2f199eSPoul-Henning Kamp 	/* flags */	0,
454e2f199eSPoul-Henning Kamp };
468af5d536SJulian Elischer 
47959b7375SPoul-Henning Kamp static MALLOC_DEFINE(M_SPKR, "spkr", "Speaker buffer");
48e4961fbfSPoul-Henning Kamp 
495b81b6b3SRodney W. Grimes /**************** MACHINE DEPENDENT PART STARTS HERE *************************
505b81b6b3SRodney W. Grimes  *
515b81b6b3SRodney W. Grimes  * This section defines a function tone() which causes a tone of given
529275c7ccSBruce Evans  * frequency and duration from the ISA console speaker.
535b81b6b3SRodney W. Grimes  * Another function endtone() is defined to force sound off, and there is
545b81b6b3SRodney W. Grimes  * also a rest() entry point to do pauses.
555b81b6b3SRodney W. Grimes  *
565b81b6b3SRodney W. Grimes  * Audible sound is generated using the Programmable Interval Timer (PIT) and
579275c7ccSBruce Evans  * Programmable Peripheral Interface (PPI) attached to the ISA speaker. The
585b81b6b3SRodney W. Grimes  * PPI controls whether sound is passed through at all; the PIT's channel 2 is
595b81b6b3SRodney W. Grimes  * used to generate clicks (a square wave) of whatever frequency is desired.
605b81b6b3SRodney W. Grimes  */
615b81b6b3SRodney W. Grimes 
625b81b6b3SRodney W. Grimes /*
639275c7ccSBruce Evans  * PPI control values.
649275c7ccSBruce Evans  * XXX should be in a header and used in clock.c.
655b81b6b3SRodney W. Grimes  */
665b81b6b3SRodney W. Grimes #define PPI_SPKR	0x03	/* turn these PPI bits on to pass sound */
675b81b6b3SRodney W. Grimes 
681e064bd4SAndrey A. Chernov #define SPKRPRI PSOCK
691e064bd4SAndrey A. Chernov static char endtone, endrest;
705b81b6b3SRodney W. Grimes 
710dfe10a6SBruce Evans static void tone __P((unsigned int thz, unsigned int ticks));
720dfe10a6SBruce Evans static void rest __P((int ticks));
730dfe10a6SBruce Evans static void playinit __P((void));
740dfe10a6SBruce Evans static void playtone __P((int pitch, int value, int sustain));
750dfe10a6SBruce Evans static int abs __P((int n));
760dfe10a6SBruce Evans static void playstring __P((char *cp, size_t slen));
770dfe10a6SBruce Evans 
781e064bd4SAndrey A. Chernov /* emit tone of frequency thz for given number of ticks */
79f4de22acSJoerg Wunsch static void
80f4de22acSJoerg Wunsch tone(thz, ticks)
811e064bd4SAndrey A. Chernov 	unsigned int thz, ticks;
825b81b6b3SRodney W. Grimes {
839714de6aSDavid Greenman     unsigned int divisor;
84424e2d04SAndrey A. Chernov     int sps;
855b81b6b3SRodney W. Grimes 
869714de6aSDavid Greenman     if (thz <= 0)
879714de6aSDavid Greenman 	return;
889714de6aSDavid Greenman 
899275c7ccSBruce Evans     divisor = timer_freq / thz;
909714de6aSDavid Greenman 
915b81b6b3SRodney W. Grimes #ifdef DEBUG
921e064bd4SAndrey A. Chernov     (void) printf("tone: thz=%d ticks=%d\n", thz, ticks);
935b81b6b3SRodney W. Grimes #endif /* DEBUG */
945b81b6b3SRodney W. Grimes 
955b81b6b3SRodney W. Grimes     /* set timer to generate clicks at given frequency in Hertz */
96f4de22acSJoerg Wunsch     sps = splclock();
97bc4ff6e3SSøren Schmidt 
989275c7ccSBruce Evans     if (acquire_timer2(TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT)) {
99bc4ff6e3SSøren Schmidt 	/* enter list of waiting procs ??? */
100f4de22acSJoerg Wunsch 	splx(sps);
101bc4ff6e3SSøren Schmidt 	return;
102bc4ff6e3SSøren Schmidt     }
103f4de22acSJoerg Wunsch     splx(sps);
104f4de22acSJoerg Wunsch     disable_intr();
105bc4ff6e3SSøren Schmidt     outb(TIMER_CNTR2, (divisor & 0xff));	/* send lo byte */
106bc4ff6e3SSøren Schmidt     outb(TIMER_CNTR2, (divisor >> 8));	/* send hi byte */
107f4de22acSJoerg Wunsch     enable_intr();
1085b81b6b3SRodney W. Grimes 
1095b81b6b3SRodney W. Grimes     /* turn the speaker on */
110bc4ff6e3SSøren Schmidt     outb(IO_PPI, inb(IO_PPI) | PPI_SPKR);
1115b81b6b3SRodney W. Grimes 
1125b81b6b3SRodney W. Grimes     /*
1135b81b6b3SRodney W. Grimes      * Set timeout to endtone function, then give up the timeslice.
1145b81b6b3SRodney W. Grimes      * This is so other processes can execute while the tone is being
1155b81b6b3SRodney W. Grimes      * emitted.
1165b81b6b3SRodney W. Grimes      */
1173bdc031eSAndrey A. Chernov     if (ticks > 0)
1183bdc031eSAndrey A. Chernov 	tsleep((caddr_t)&endtone, SPKRPRI | PCATCH, "spkrtn", ticks);
119bc4ff6e3SSøren Schmidt     outb(IO_PPI, inb(IO_PPI) & ~PPI_SPKR);
120f4de22acSJoerg Wunsch     sps = splclock();
121bc4ff6e3SSøren Schmidt     release_timer2();
122f4de22acSJoerg Wunsch     splx(sps);
1235b81b6b3SRodney W. Grimes }
1245b81b6b3SRodney W. Grimes 
1255b81b6b3SRodney W. Grimes /* rest for given number of ticks */
126f4de22acSJoerg Wunsch static void
127f4de22acSJoerg Wunsch rest(ticks)
1285b81b6b3SRodney W. Grimes 	int	ticks;
1295b81b6b3SRodney W. Grimes {
1305b81b6b3SRodney W. Grimes     /*
1315b81b6b3SRodney W. Grimes      * Set timeout to endrest function, then give up the timeslice.
1325b81b6b3SRodney W. Grimes      * This is so other processes can execute while the rest is being
1335b81b6b3SRodney W. Grimes      * waited out.
1345b81b6b3SRodney W. Grimes      */
1355b81b6b3SRodney W. Grimes #ifdef DEBUG
136e614aa8aSAndrey A. Chernov     (void) printf("rest: %d\n", ticks);
1375b81b6b3SRodney W. Grimes #endif /* DEBUG */
1383bdc031eSAndrey A. Chernov     if (ticks > 0)
1393bdc031eSAndrey A. Chernov 	tsleep((caddr_t)&endrest, SPKRPRI | PCATCH, "spkrrs", ticks);
1405b81b6b3SRodney W. Grimes }
1415b81b6b3SRodney W. Grimes 
1425b81b6b3SRodney W. Grimes /**************** PLAY STRING INTERPRETER BEGINS HERE **********************
1435b81b6b3SRodney W. Grimes  *
1445b81b6b3SRodney W. Grimes  * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
145e614aa8aSAndrey A. Chernov  * M[LNS] are missing; the ~ synonym and the _ slur mark and the octave-
146e614aa8aSAndrey A. Chernov  * tracking facility are added.
1475b81b6b3SRodney W. Grimes  * Requires tone(), rest(), and endtone(). String play is not interruptible
1485b81b6b3SRodney W. Grimes  * except possibly at physical block boundaries.
1495b81b6b3SRodney W. Grimes  */
1505b81b6b3SRodney W. Grimes 
1515b81b6b3SRodney W. Grimes typedef int	bool;
1525b81b6b3SRodney W. Grimes #define TRUE	1
1535b81b6b3SRodney W. Grimes #define FALSE	0
1545b81b6b3SRodney W. Grimes 
1555b81b6b3SRodney W. Grimes #define dtoi(c)		((c) - '0')
1565b81b6b3SRodney W. Grimes 
1575b81b6b3SRodney W. Grimes static int octave;	/* currently selected octave */
1585b81b6b3SRodney W. Grimes static int whole;	/* whole-note time at current tempo, in ticks */
1595b81b6b3SRodney W. Grimes static int value;	/* whole divisor for note time, quarter note = 1 */
1605b81b6b3SRodney W. Grimes static int fill;	/* controls spacing of notes */
1615b81b6b3SRodney W. Grimes static bool octtrack;	/* octave-tracking on? */
1625b81b6b3SRodney W. Grimes static bool octprefix;	/* override current octave-tracking state? */
1635b81b6b3SRodney W. Grimes 
1645b81b6b3SRodney W. Grimes /*
1655b81b6b3SRodney W. Grimes  * Magic number avoidance...
1665b81b6b3SRodney W. Grimes  */
1675b81b6b3SRodney W. Grimes #define SECS_PER_MIN	60	/* seconds per minute */
1685b81b6b3SRodney W. Grimes #define WHOLE_NOTE	4	/* quarter notes per whole note */
1695b81b6b3SRodney W. Grimes #define MIN_VALUE	64	/* the most we can divide a note by */
1705b81b6b3SRodney W. Grimes #define DFLT_VALUE	4	/* default value (quarter-note) */
1715b81b6b3SRodney W. Grimes #define FILLTIME	8	/* for articulation, break note in parts */
1725b81b6b3SRodney W. Grimes #define STACCATO	6	/* 6/8 = 3/4 of note is filled */
1735b81b6b3SRodney W. Grimes #define NORMAL		7	/* 7/8ths of note interval is filled */
1745b81b6b3SRodney W. Grimes #define LEGATO		8	/* all of note interval is filled */
1755b81b6b3SRodney W. Grimes #define DFLT_OCTAVE	4	/* default octave */
1765b81b6b3SRodney W. Grimes #define MIN_TEMPO	32	/* minimum tempo */
1775b81b6b3SRodney W. Grimes #define DFLT_TEMPO	120	/* default tempo */
1785b81b6b3SRodney W. Grimes #define MAX_TEMPO	255	/* max tempo */
1795b81b6b3SRodney W. Grimes #define NUM_MULT	3	/* numerator of dot multiplier */
1805b81b6b3SRodney W. Grimes #define DENOM_MULT	2	/* denominator of dot multiplier */
1815b81b6b3SRodney W. Grimes 
1825b81b6b3SRodney W. Grimes /* letter to half-tone:  A   B  C  D  E  F  G */
1835b81b6b3SRodney W. Grimes static int notetab[8] = {9, 11, 0, 2, 4, 5, 7};
1845b81b6b3SRodney W. Grimes 
1855b81b6b3SRodney W. Grimes /*
1865b81b6b3SRodney W. Grimes  * This is the American Standard A440 Equal-Tempered scale with frequencies
1875b81b6b3SRodney W. Grimes  * rounded to nearest integer. Thank Goddess for the good ol' CRC Handbook...
1885b81b6b3SRodney W. Grimes  * our octave 0 is standard octave 2.
1895b81b6b3SRodney W. Grimes  */
1905b81b6b3SRodney W. Grimes #define OCTAVE_NOTES	12	/* semitones per octave */
1915b81b6b3SRodney W. Grimes static int pitchtab[] =
1925b81b6b3SRodney W. Grimes {
1935b81b6b3SRodney W. Grimes /*        C     C#    D     D#    E     F     F#    G     G#    A     A#    B*/
1945b81b6b3SRodney W. Grimes /* 0 */   65,   69,   73,   78,   82,   87,   93,   98,  103,  110,  117,  123,
1955b81b6b3SRodney W. Grimes /* 1 */  131,  139,  147,  156,  165,  175,  185,  196,  208,  220,  233,  247,
1965b81b6b3SRodney W. Grimes /* 2 */  262,  277,  294,  311,  330,  349,  370,  392,  415,  440,  466,  494,
1975b81b6b3SRodney W. Grimes /* 3 */  523,  554,  587,  622,  659,  698,  740,  784,  831,  880,  932,  988,
1985b81b6b3SRodney W. Grimes /* 4 */ 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1975,
1995b81b6b3SRodney W. Grimes /* 5 */ 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951,
2005b81b6b3SRodney W. Grimes /* 6 */ 4186, 4435, 4698, 4978, 5274, 5588, 5920, 6272, 6644, 7040, 7459, 7902,
2015b81b6b3SRodney W. Grimes };
2025b81b6b3SRodney W. Grimes 
203f4de22acSJoerg Wunsch static void
204f4de22acSJoerg Wunsch playinit()
2055b81b6b3SRodney W. Grimes {
2065b81b6b3SRodney W. Grimes     octave = DFLT_OCTAVE;
2071e064bd4SAndrey A. Chernov     whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / DFLT_TEMPO;
2085b81b6b3SRodney W. Grimes     fill = NORMAL;
2095b81b6b3SRodney W. Grimes     value = DFLT_VALUE;
2105b81b6b3SRodney W. Grimes     octtrack = FALSE;
2115b81b6b3SRodney W. Grimes     octprefix = TRUE;	/* act as though there was an initial O(n) */
2125b81b6b3SRodney W. Grimes }
2135b81b6b3SRodney W. Grimes 
2145b81b6b3SRodney W. Grimes /* play tone of proper duration for current rhythm signature */
215f4de22acSJoerg Wunsch static void
216f4de22acSJoerg Wunsch playtone(pitch, value, sustain)
2175b81b6b3SRodney W. Grimes 	int	pitch, value, sustain;
2185b81b6b3SRodney W. Grimes {
2195b81b6b3SRodney W. Grimes     register int	sound, silence, snum = 1, sdenom = 1;
2205b81b6b3SRodney W. Grimes 
2215b81b6b3SRodney W. Grimes     /* this weirdness avoids floating-point arithmetic */
2225b81b6b3SRodney W. Grimes     for (; sustain; sustain--)
2235b81b6b3SRodney W. Grimes     {
224e614aa8aSAndrey A. Chernov 	/* See the BUGS section in the man page for discussion */
2255b81b6b3SRodney W. Grimes 	snum *= NUM_MULT;
2265b81b6b3SRodney W. Grimes 	sdenom *= DENOM_MULT;
2275b81b6b3SRodney W. Grimes     }
2285b81b6b3SRodney W. Grimes 
2299714de6aSDavid Greenman     if (value == 0 || sdenom == 0)
2309714de6aSDavid Greenman 	return;
2319714de6aSDavid Greenman 
2325b81b6b3SRodney W. Grimes     if (pitch == -1)
233424e2d04SAndrey A. Chernov 	rest(whole * snum / (value * sdenom));
2345b81b6b3SRodney W. Grimes     else
2355b81b6b3SRodney W. Grimes     {
2365b81b6b3SRodney W. Grimes 	sound = (whole * snum) / (value * sdenom)
2375b81b6b3SRodney W. Grimes 		- (whole * (FILLTIME - fill)) / (value * FILLTIME);
2385b81b6b3SRodney W. Grimes 	silence = whole * (FILLTIME-fill) * snum / (FILLTIME * value * sdenom);
2395b81b6b3SRodney W. Grimes 
2405b81b6b3SRodney W. Grimes #ifdef DEBUG
241e614aa8aSAndrey A. Chernov 	(void) printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
2425b81b6b3SRodney W. Grimes 			pitch, sound, silence);
2435b81b6b3SRodney W. Grimes #endif /* DEBUG */
2445b81b6b3SRodney W. Grimes 
245424e2d04SAndrey A. Chernov 	tone(pitchtab[pitch], sound);
2465b81b6b3SRodney W. Grimes 	if (fill != LEGATO)
247424e2d04SAndrey A. Chernov 	    rest(silence);
2485b81b6b3SRodney W. Grimes     }
2495b81b6b3SRodney W. Grimes }
2505b81b6b3SRodney W. Grimes 
251f4de22acSJoerg Wunsch static int
252f4de22acSJoerg Wunsch abs(n)
2535b81b6b3SRodney W. Grimes 	int n;
2545b81b6b3SRodney W. Grimes {
2555b81b6b3SRodney W. Grimes     if (n < 0)
2565b81b6b3SRodney W. Grimes 	return(-n);
2575b81b6b3SRodney W. Grimes     else
2585b81b6b3SRodney W. Grimes 	return(n);
2595b81b6b3SRodney W. Grimes }
2605b81b6b3SRodney W. Grimes 
2615b81b6b3SRodney W. Grimes /* interpret and play an item from a notation string */
262f4de22acSJoerg Wunsch static void
263f4de22acSJoerg Wunsch playstring(cp, slen)
2645b81b6b3SRodney W. Grimes 	char	*cp;
2655b81b6b3SRodney W. Grimes 	size_t	slen;
2665b81b6b3SRodney W. Grimes {
267e614aa8aSAndrey A. Chernov     int		pitch, oldfill, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
2685b81b6b3SRodney W. Grimes 
2695b81b6b3SRodney W. Grimes #define GETNUM(cp, v)	for(v=0; isdigit(cp[1]) && slen > 0; ) \
2705b81b6b3SRodney W. Grimes 				{v = v * 10 + (*++cp - '0'); slen--;}
2715b81b6b3SRodney W. Grimes     for (; slen--; cp++)
2725b81b6b3SRodney W. Grimes     {
2735b81b6b3SRodney W. Grimes 	int		sustain, timeval, tempo;
2745b81b6b3SRodney W. Grimes 	register char	c = toupper(*cp);
2755b81b6b3SRodney W. Grimes 
2765b81b6b3SRodney W. Grimes #ifdef DEBUG
277e614aa8aSAndrey A. Chernov 	(void) printf("playstring: %c (%x)\n", c, c);
2785b81b6b3SRodney W. Grimes #endif /* DEBUG */
2795b81b6b3SRodney W. Grimes 
2805b81b6b3SRodney W. Grimes 	switch (c)
2815b81b6b3SRodney W. Grimes 	{
2825b81b6b3SRodney W. Grimes 	case 'A':  case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
2835b81b6b3SRodney W. Grimes 
2845b81b6b3SRodney W. Grimes 	    /* compute pitch */
2855b81b6b3SRodney W. Grimes 	    pitch = notetab[c - 'A'] + octave * OCTAVE_NOTES;
2865b81b6b3SRodney W. Grimes 
2875b81b6b3SRodney W. Grimes 	    /* this may be followed by an accidental sign */
2885b81b6b3SRodney W. Grimes 	    if (cp[1] == '#' || cp[1] == '+')
2895b81b6b3SRodney W. Grimes 	    {
2905b81b6b3SRodney W. Grimes 		++pitch;
2915b81b6b3SRodney W. Grimes 		++cp;
2925b81b6b3SRodney W. Grimes 		slen--;
2935b81b6b3SRodney W. Grimes 	    }
2945b81b6b3SRodney W. Grimes 	    else if (cp[1] == '-')
2955b81b6b3SRodney W. Grimes 	    {
2965b81b6b3SRodney W. Grimes 		--pitch;
2975b81b6b3SRodney W. Grimes 		++cp;
2985b81b6b3SRodney W. Grimes 		slen--;
2995b81b6b3SRodney W. Grimes 	    }
3005b81b6b3SRodney W. Grimes 
3015b81b6b3SRodney W. Grimes 	    /*
3025b81b6b3SRodney W. Grimes 	     * If octave-tracking mode is on, and there has been no octave-
3035b81b6b3SRodney W. Grimes 	     * setting prefix, find the version of the current letter note
3045b81b6b3SRodney W. Grimes 	     * closest to the last regardless of octave.
3055b81b6b3SRodney W. Grimes 	     */
3065b81b6b3SRodney W. Grimes 	    if (octtrack && !octprefix)
3075b81b6b3SRodney W. Grimes 	    {
3085b81b6b3SRodney W. Grimes 		if (abs(pitch-lastpitch) > abs(pitch+OCTAVE_NOTES-lastpitch))
3095b81b6b3SRodney W. Grimes 		{
3105b81b6b3SRodney W. Grimes 		    ++octave;
3115b81b6b3SRodney W. Grimes 		    pitch += OCTAVE_NOTES;
3125b81b6b3SRodney W. Grimes 		}
3135b81b6b3SRodney W. Grimes 
3145b81b6b3SRodney W. Grimes 		if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
3155b81b6b3SRodney W. Grimes 		{
3165b81b6b3SRodney W. Grimes 		    --octave;
3175b81b6b3SRodney W. Grimes 		    pitch -= OCTAVE_NOTES;
3185b81b6b3SRodney W. Grimes 		}
3195b81b6b3SRodney W. Grimes 	    }
3205b81b6b3SRodney W. Grimes 	    octprefix = FALSE;
3215b81b6b3SRodney W. Grimes 	    lastpitch = pitch;
3225b81b6b3SRodney W. Grimes 
3235b81b6b3SRodney W. Grimes 	    /* ...which may in turn be followed by an override time value */
3245b81b6b3SRodney W. Grimes 	    GETNUM(cp, timeval);
3255b81b6b3SRodney W. Grimes 	    if (timeval <= 0 || timeval > MIN_VALUE)
3265b81b6b3SRodney W. Grimes 		timeval = value;
3275b81b6b3SRodney W. Grimes 
3285b81b6b3SRodney W. Grimes 	    /* ...and/or sustain dots */
3295b81b6b3SRodney W. Grimes 	    for (sustain = 0; cp[1] == '.'; cp++)
3305b81b6b3SRodney W. Grimes 	    {
3315b81b6b3SRodney W. Grimes 		slen--;
3325b81b6b3SRodney W. Grimes 		sustain++;
3335b81b6b3SRodney W. Grimes 	    }
3345b81b6b3SRodney W. Grimes 
335e614aa8aSAndrey A. Chernov 	    /* ...and/or a slur mark */
336e614aa8aSAndrey A. Chernov 	    oldfill = fill;
337e614aa8aSAndrey A. Chernov 	    if (cp[1] == '_')
338e614aa8aSAndrey A. Chernov 	    {
339e614aa8aSAndrey A. Chernov 		fill = LEGATO;
340e614aa8aSAndrey A. Chernov 		++cp;
341e614aa8aSAndrey A. Chernov 		slen--;
342e614aa8aSAndrey A. Chernov 	    }
343e614aa8aSAndrey A. Chernov 
3445b81b6b3SRodney W. Grimes 	    /* time to emit the actual tone */
345424e2d04SAndrey A. Chernov 	    playtone(pitch, timeval, sustain);
346e614aa8aSAndrey A. Chernov 
347e614aa8aSAndrey A. Chernov 	    fill = oldfill;
3485b81b6b3SRodney W. Grimes 	    break;
3495b81b6b3SRodney W. Grimes 
3505b81b6b3SRodney W. Grimes 	case 'O':
3515b81b6b3SRodney W. Grimes 	    if (cp[1] == 'N' || cp[1] == 'n')
3525b81b6b3SRodney W. Grimes 	    {
3535b81b6b3SRodney W. Grimes 		octprefix = octtrack = FALSE;
3545b81b6b3SRodney W. Grimes 		++cp;
3555b81b6b3SRodney W. Grimes 		slen--;
3565b81b6b3SRodney W. Grimes 	    }
3575b81b6b3SRodney W. Grimes 	    else if (cp[1] == 'L' || cp[1] == 'l')
3585b81b6b3SRodney W. Grimes 	    {
3595b81b6b3SRodney W. Grimes 		octtrack = TRUE;
3605b81b6b3SRodney W. Grimes 		++cp;
3615b81b6b3SRodney W. Grimes 		slen--;
3625b81b6b3SRodney W. Grimes 	    }
3635b81b6b3SRodney W. Grimes 	    else
3645b81b6b3SRodney W. Grimes 	    {
3655b81b6b3SRodney W. Grimes 		GETNUM(cp, octave);
3660a97fb59SAndrey A. Chernov 		if (octave >= sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES)
3675b81b6b3SRodney W. Grimes 		    octave = DFLT_OCTAVE;
3685b81b6b3SRodney W. Grimes 		octprefix = TRUE;
3695b81b6b3SRodney W. Grimes 	    }
3705b81b6b3SRodney W. Grimes 	    break;
3715b81b6b3SRodney W. Grimes 
3725b81b6b3SRodney W. Grimes 	case '>':
3730a97fb59SAndrey A. Chernov 	    if (octave < sizeof(pitchtab) / sizeof(pitchtab[0]) / OCTAVE_NOTES - 1)
3745b81b6b3SRodney W. Grimes 		octave++;
3755b81b6b3SRodney W. Grimes 	    octprefix = TRUE;
3765b81b6b3SRodney W. Grimes 	    break;
3775b81b6b3SRodney W. Grimes 
3785b81b6b3SRodney W. Grimes 	case '<':
3795b81b6b3SRodney W. Grimes 	    if (octave > 0)
3805b81b6b3SRodney W. Grimes 		octave--;
3815b81b6b3SRodney W. Grimes 	    octprefix = TRUE;
3825b81b6b3SRodney W. Grimes 	    break;
3835b81b6b3SRodney W. Grimes 
3845b81b6b3SRodney W. Grimes 	case 'N':
3855b81b6b3SRodney W. Grimes 	    GETNUM(cp, pitch);
3865b81b6b3SRodney W. Grimes 	    for (sustain = 0; cp[1] == '.'; cp++)
3875b81b6b3SRodney W. Grimes 	    {
3885b81b6b3SRodney W. Grimes 		slen--;
3895b81b6b3SRodney W. Grimes 		sustain++;
3905b81b6b3SRodney W. Grimes 	    }
391e614aa8aSAndrey A. Chernov 	    oldfill = fill;
392e614aa8aSAndrey A. Chernov 	    if (cp[1] == '_')
393e614aa8aSAndrey A. Chernov 	    {
394e614aa8aSAndrey A. Chernov 		fill = LEGATO;
395e614aa8aSAndrey A. Chernov 		++cp;
396e614aa8aSAndrey A. Chernov 		slen--;
397e614aa8aSAndrey A. Chernov 	    }
398424e2d04SAndrey A. Chernov 	    playtone(pitch - 1, value, sustain);
399e614aa8aSAndrey A. Chernov 	    fill = oldfill;
4005b81b6b3SRodney W. Grimes 	    break;
4015b81b6b3SRodney W. Grimes 
4025b81b6b3SRodney W. Grimes 	case 'L':
4035b81b6b3SRodney W. Grimes 	    GETNUM(cp, value);
4045b81b6b3SRodney W. Grimes 	    if (value <= 0 || value > MIN_VALUE)
4055b81b6b3SRodney W. Grimes 		value = DFLT_VALUE;
4065b81b6b3SRodney W. Grimes 	    break;
4075b81b6b3SRodney W. Grimes 
4085b81b6b3SRodney W. Grimes 	case 'P':
4095b81b6b3SRodney W. Grimes 	case '~':
4105b81b6b3SRodney W. Grimes 	    /* this may be followed by an override time value */
4115b81b6b3SRodney W. Grimes 	    GETNUM(cp, timeval);
4125b81b6b3SRodney W. Grimes 	    if (timeval <= 0 || timeval > MIN_VALUE)
4135b81b6b3SRodney W. Grimes 		timeval = value;
4145b81b6b3SRodney W. Grimes 	    for (sustain = 0; cp[1] == '.'; cp++)
4155b81b6b3SRodney W. Grimes 	    {
4165b81b6b3SRodney W. Grimes 		slen--;
4175b81b6b3SRodney W. Grimes 		sustain++;
4185b81b6b3SRodney W. Grimes 	    }
419424e2d04SAndrey A. Chernov 	    playtone(-1, timeval, sustain);
4205b81b6b3SRodney W. Grimes 	    break;
4215b81b6b3SRodney W. Grimes 
4225b81b6b3SRodney W. Grimes 	case 'T':
4235b81b6b3SRodney W. Grimes 	    GETNUM(cp, tempo);
4245b81b6b3SRodney W. Grimes 	    if (tempo < MIN_TEMPO || tempo > MAX_TEMPO)
4255b81b6b3SRodney W. Grimes 		tempo = DFLT_TEMPO;
4261e064bd4SAndrey A. Chernov 	    whole = (hz * SECS_PER_MIN * WHOLE_NOTE) / tempo;
4275b81b6b3SRodney W. Grimes 	    break;
4285b81b6b3SRodney W. Grimes 
4295b81b6b3SRodney W. Grimes 	case 'M':
4305b81b6b3SRodney W. Grimes 	    if (cp[1] == 'N' || cp[1] == 'n')
4315b81b6b3SRodney W. Grimes 	    {
4325b81b6b3SRodney W. Grimes 		fill = NORMAL;
4335b81b6b3SRodney W. Grimes 		++cp;
4345b81b6b3SRodney W. Grimes 		slen--;
4355b81b6b3SRodney W. Grimes 	    }
4365b81b6b3SRodney W. Grimes 	    else if (cp[1] == 'L' || cp[1] == 'l')
4375b81b6b3SRodney W. Grimes 	    {
4385b81b6b3SRodney W. Grimes 		fill = LEGATO;
4395b81b6b3SRodney W. Grimes 		++cp;
4405b81b6b3SRodney W. Grimes 		slen--;
4415b81b6b3SRodney W. Grimes 	    }
4425b81b6b3SRodney W. Grimes 	    else if (cp[1] == 'S' || cp[1] == 's')
4435b81b6b3SRodney W. Grimes 	    {
4445b81b6b3SRodney W. Grimes 		fill = STACCATO;
4455b81b6b3SRodney W. Grimes 		++cp;
4465b81b6b3SRodney W. Grimes 		slen--;
4475b81b6b3SRodney W. Grimes 	    }
4485b81b6b3SRodney W. Grimes 	    break;
4495b81b6b3SRodney W. Grimes 	}
4505b81b6b3SRodney W. Grimes     }
4515b81b6b3SRodney W. Grimes }
4525b81b6b3SRodney W. Grimes 
4535b81b6b3SRodney W. Grimes /******************* UNIX DRIVER HOOKS BEGIN HERE **************************
4545b81b6b3SRodney W. Grimes  *
4555b81b6b3SRodney W. Grimes  * This section implements driver hooks to run playstring() and the tone(),
4565b81b6b3SRodney W. Grimes  * endtone(), and rest() functions defined above.
4575b81b6b3SRodney W. Grimes  */
4585b81b6b3SRodney W. Grimes 
459e614aa8aSAndrey A. Chernov static int spkr_active = FALSE; /* exclusion flag */
460e4961fbfSPoul-Henning Kamp static char *spkr_inbuf;  /* incoming buf */
4615b81b6b3SRodney W. Grimes 
462f4de22acSJoerg Wunsch int
463b40ce416SJulian Elischer spkropen(dev, flags, fmt, td)
4645b81b6b3SRodney W. Grimes 	dev_t		dev;
46560039670SBruce Evans 	int		flags;
46660039670SBruce Evans 	int		fmt;
467b40ce416SJulian Elischer 	struct thread	*td;
4685b81b6b3SRodney W. Grimes {
4695b81b6b3SRodney W. Grimes #ifdef DEBUG
470b8e49f68SBill Fumerola     (void) printf("spkropen: entering with dev = %s\n", devtoname(dev));
4715b81b6b3SRodney W. Grimes #endif /* DEBUG */
4725b81b6b3SRodney W. Grimes 
4735b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
4745b81b6b3SRodney W. Grimes 	return(ENXIO);
4755b81b6b3SRodney W. Grimes     else if (spkr_active)
4765b81b6b3SRodney W. Grimes 	return(EBUSY);
4775b81b6b3SRodney W. Grimes     else
4785b81b6b3SRodney W. Grimes     {
479e614aa8aSAndrey A. Chernov #ifdef DEBUG
480e614aa8aSAndrey A. Chernov 	(void) printf("spkropen: about to perform play initialization\n");
481e614aa8aSAndrey A. Chernov #endif /* DEBUG */
4825b81b6b3SRodney W. Grimes 	playinit();
483e4961fbfSPoul-Henning Kamp 	spkr_inbuf = malloc(DEV_BSIZE, M_SPKR, M_WAITOK);
484e614aa8aSAndrey A. Chernov 	spkr_active = TRUE;
4855b81b6b3SRodney W. Grimes 	return(0);
4865b81b6b3SRodney W. Grimes     }
487e614aa8aSAndrey A. Chernov }
4885b81b6b3SRodney W. Grimes 
489f4de22acSJoerg Wunsch int
490f4de22acSJoerg Wunsch spkrwrite(dev, uio, ioflag)
4915b81b6b3SRodney W. Grimes 	dev_t		dev;
4925b81b6b3SRodney W. Grimes 	struct uio	*uio;
49360039670SBruce Evans 	int		ioflag;
4945b81b6b3SRodney W. Grimes {
4955b81b6b3SRodney W. Grimes #ifdef DEBUG
496b8e49f68SBill Fumerola     printf("spkrwrite: entering with dev = %s, count = %d\n",
497b8e49f68SBill Fumerola 		devtoname(dev), uio->uio_resid);
4985b81b6b3SRodney W. Grimes #endif /* DEBUG */
4995b81b6b3SRodney W. Grimes 
5005b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
5015b81b6b3SRodney W. Grimes 	return(ENXIO);
5023bd9f6dbSPeter Wemm     else if (uio->uio_resid > (DEV_BSIZE - 1))     /* prevent system crashes */
503e614aa8aSAndrey A. Chernov 	return(E2BIG);
5045b81b6b3SRodney W. Grimes     else
5055b81b6b3SRodney W. Grimes     {
506e614aa8aSAndrey A. Chernov 	unsigned n;
507e614aa8aSAndrey A. Chernov 	char *cp;
508e614aa8aSAndrey A. Chernov 	int error;
509e614aa8aSAndrey A. Chernov 
510e614aa8aSAndrey A. Chernov 	n = uio->uio_resid;
511e4961fbfSPoul-Henning Kamp 	cp = spkr_inbuf;
5123bd9f6dbSPeter Wemm 	error = uiomove(cp, n, uio);
5133bd9f6dbSPeter Wemm 	if (!error) {
5143bd9f6dbSPeter Wemm 		cp[n] = '\0';
515424e2d04SAndrey A. Chernov 		playstring(cp, n);
5163bd9f6dbSPeter Wemm 	}
5175b81b6b3SRodney W. Grimes 	return(error);
5185b81b6b3SRodney W. Grimes     }
5195b81b6b3SRodney W. Grimes }
5205b81b6b3SRodney W. Grimes 
521f4de22acSJoerg Wunsch int
522b40ce416SJulian Elischer spkrclose(dev, flags, fmt, td)
5235b81b6b3SRodney W. Grimes 	dev_t		dev;
52460039670SBruce Evans 	int		flags;
52560039670SBruce Evans 	int		fmt;
526b40ce416SJulian Elischer 	struct thread	*td;
5275b81b6b3SRodney W. Grimes {
5285b81b6b3SRodney W. Grimes #ifdef DEBUG
529b8e49f68SBill Fumerola     (void) printf("spkrclose: entering with dev = %s\n", devtoname(dev));
5305b81b6b3SRodney W. Grimes #endif /* DEBUG */
5315b81b6b3SRodney W. Grimes 
5325b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
5335b81b6b3SRodney W. Grimes 	return(ENXIO);
5345b81b6b3SRodney W. Grimes     else
5355b81b6b3SRodney W. Grimes     {
5361e064bd4SAndrey A. Chernov 	wakeup((caddr_t)&endtone);
5371e064bd4SAndrey A. Chernov 	wakeup((caddr_t)&endrest);
538e4961fbfSPoul-Henning Kamp 	free(spkr_inbuf, M_SPKR);
539e614aa8aSAndrey A. Chernov 	spkr_active = FALSE;
5405b81b6b3SRodney W. Grimes 	return(0);
5415b81b6b3SRodney W. Grimes     }
542e614aa8aSAndrey A. Chernov }
5435b81b6b3SRodney W. Grimes 
544f4de22acSJoerg Wunsch int
545b40ce416SJulian Elischer spkrioctl(dev, cmd, cmdarg, flags, td)
5465b81b6b3SRodney W. Grimes 	dev_t		dev;
54700671271SBruce Evans 	unsigned long	cmd;
5485b81b6b3SRodney W. Grimes 	caddr_t		cmdarg;
54960039670SBruce Evans 	int		flags;
550b40ce416SJulian Elischer 	struct thread	*td;
5515b81b6b3SRodney W. Grimes {
5525b81b6b3SRodney W. Grimes #ifdef DEBUG
553d9183205SBruce Evans     (void) printf("spkrioctl: entering with dev = %s, cmd = %lx\n",
554d9183205SBruce Evans     	devtoname(dev), cmd);
5555b81b6b3SRodney W. Grimes #endif /* DEBUG */
5565b81b6b3SRodney W. Grimes 
5575b81b6b3SRodney W. Grimes     if (minor(dev) != 0)
5585b81b6b3SRodney W. Grimes 	return(ENXIO);
5595b81b6b3SRodney W. Grimes     else if (cmd == SPKRTONE)
5605b81b6b3SRodney W. Grimes     {
5615b81b6b3SRodney W. Grimes 	tone_t	*tp = (tone_t *)cmdarg;
5625b81b6b3SRodney W. Grimes 
5635b81b6b3SRodney W. Grimes 	if (tp->frequency == 0)
564424e2d04SAndrey A. Chernov 	    rest(tp->duration);
5655b81b6b3SRodney W. Grimes 	else
566424e2d04SAndrey A. Chernov 	    tone(tp->frequency, tp->duration);
567424e2d04SAndrey A. Chernov 	return 0;
5685b81b6b3SRodney W. Grimes     }
5695b81b6b3SRodney W. Grimes     else if (cmd == SPKRTUNE)
5705b81b6b3SRodney W. Grimes     {
5715b81b6b3SRodney W. Grimes 	tone_t  *tp = (tone_t *)(*(caddr_t *)cmdarg);
5725b81b6b3SRodney W. Grimes 	tone_t ttp;
5735b81b6b3SRodney W. Grimes 	int error;
5745b81b6b3SRodney W. Grimes 
5755b81b6b3SRodney W. Grimes 	for (; ; tp++) {
5765b81b6b3SRodney W. Grimes 	    error = copyin(tp, &ttp, sizeof(tone_t));
5775b81b6b3SRodney W. Grimes 	    if (error)
5785b81b6b3SRodney W. Grimes 		    return(error);
5795b81b6b3SRodney W. Grimes 	    if (ttp.duration == 0)
5805b81b6b3SRodney W. Grimes 		    break;
5815b81b6b3SRodney W. Grimes 	    if (ttp.frequency == 0)
582424e2d04SAndrey A. Chernov 		 rest(ttp.duration);
5835b81b6b3SRodney W. Grimes 	    else
584424e2d04SAndrey A. Chernov 		 tone(ttp.frequency, ttp.duration);
5855b81b6b3SRodney W. Grimes 	}
5865b81b6b3SRodney W. Grimes 	return(0);
5875b81b6b3SRodney W. Grimes     }
588e614aa8aSAndrey A. Chernov     return(EINVAL);
589e614aa8aSAndrey A. Chernov }
5905b81b6b3SRodney W. Grimes 
5912a50a6d7SMike Smith /*
5922a50a6d7SMike Smith  * Install placeholder to claim the resources owned by the
5932a50a6d7SMike Smith  * AT tone generator.
5942a50a6d7SMike Smith  */
5952a50a6d7SMike Smith static struct isa_pnp_id atspeaker_ids[] = {
5962a50a6d7SMike Smith 	{ 0x0008d041 /* PNP0800 */, "AT speaker" },
5972a50a6d7SMike Smith 	{ 0 }
5982a50a6d7SMike Smith };
5992a50a6d7SMike Smith 
600ce6d929bSWes Peters static dev_t atspeaker_dev;
601ce6d929bSWes Peters 
6022a50a6d7SMike Smith static int
6032a50a6d7SMike Smith atspeaker_probe(device_t dev)
6042a50a6d7SMike Smith {
6052a50a6d7SMike Smith 	return(ISA_PNP_PROBE(device_get_parent(dev), dev, atspeaker_ids));
6062a50a6d7SMike Smith }
6072a50a6d7SMike Smith 
6082a50a6d7SMike Smith static int
6092a50a6d7SMike Smith atspeaker_attach(device_t dev)
6102a50a6d7SMike Smith {
611ce6d929bSWes Peters 	atspeaker_dev = make_dev(&spkr_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600,
612ce6d929bSWes Peters 	    "speaker");
613ce6d929bSWes Peters 	return (0);
614ce6d929bSWes Peters }
615ce6d929bSWes Peters 
616ce6d929bSWes Peters static int
617ce6d929bSWes Peters atspeaker_detach(device_t dev)
618ce6d929bSWes Peters {
619ce6d929bSWes Peters 	destroy_dev(atspeaker_dev);
6202a50a6d7SMike Smith 	return (0);
6212a50a6d7SMike Smith }
6222a50a6d7SMike Smith 
6232a50a6d7SMike Smith static device_method_t atspeaker_methods[] = {
6242a50a6d7SMike Smith 	/* Device interface */
6252a50a6d7SMike Smith 	DEVMETHOD(device_probe,		atspeaker_probe),
6262a50a6d7SMike Smith 	DEVMETHOD(device_attach,	atspeaker_attach),
627ce6d929bSWes Peters 	DEVMETHOD(device_detach,	atspeaker_detach),
6282a50a6d7SMike Smith 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
6292a50a6d7SMike Smith 	DEVMETHOD(device_suspend,	bus_generic_suspend),
6302a50a6d7SMike Smith 	DEVMETHOD(device_resume,	bus_generic_resume),
6312a50a6d7SMike Smith 	{ 0, 0 }
6322a50a6d7SMike Smith };
6332a50a6d7SMike Smith 
6342a50a6d7SMike Smith static driver_t atspeaker_driver = {
6352a50a6d7SMike Smith 	"atspeaker",
6362a50a6d7SMike Smith 	atspeaker_methods,
6372a50a6d7SMike Smith 	1,		/* no softc */
6382a50a6d7SMike Smith };
6392a50a6d7SMike Smith 
6402a50a6d7SMike Smith static devclass_t atspeaker_devclass;
6412a50a6d7SMike Smith 
6422a50a6d7SMike Smith DRIVER_MODULE(atspeaker, isa, atspeaker_driver, atspeaker_devclass, 0, 0);
6435f063c7bSMike Smith DRIVER_MODULE(atspeaker, acpi, atspeaker_driver, atspeaker_devclass, 0, 0);
64453ac6efbSJulian Elischer 
6455b81b6b3SRodney W. Grimes /* spkr.c ends here */
646