Module Name:    src
Committed By:   christos
Date:           Fri Dec  9 04:32:39 UTC 2016

Modified Files:
        src/sys/dev: files.audio spkr_synth.c
        src/sys/dev/isa: files.isa spkr_pcppi.c
Added Files:
        src/sys/dev: spkr.c spkrio.h
Removed Files:
        src/sys/dev/isa: spkr.c spkrio.h

Log Message:
more spkr rework.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/files.audio
cvs rdiff -u -r0 -r1.1 src/sys/dev/spkr.c src/sys/dev/spkrio.h
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/spkr_synth.c
cvs rdiff -u -r1.166 -r1.167 src/sys/dev/isa/files.isa
cvs rdiff -u -r1.39 -r0 src/sys/dev/isa/spkr.c
cvs rdiff -u -r1.1 -r1.2 src/sys/dev/isa/spkr_pcppi.c
cvs rdiff -u -r1.3 -r0 src/sys/dev/isa/spkrio.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/files.audio
diff -u src/sys/dev/files.audio:1.5 src/sys/dev/files.audio:1.6
--- src/sys/dev/files.audio:1.5	Thu Dec  8 06:31:08 2016
+++ src/sys/dev/files.audio	Thu Dec  8 23:32:39 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: files.audio,v 1.5 2016/12/08 11:31:08 nat Exp $
+#	$NetBSD: files.audio,v 1.6 2016/12/09 04:32:39 christos Exp $
 
 define	audiobus	{ }
 define	midibus		{ }
@@ -33,4 +33,5 @@ file	dev/midi.c			midi			needs-flag
 file	dev/midictl.c			midisyn
 file	dev/midisyn.c			midisyn
 file	dev/mulaw.c			mulaw			needs-flag
-file	dev/spkr_synth.c		spkr & vaudiospeaker	needs-flag
+file	dev/spkr.c			spkr			needs-flag
+file	dev/spkr_synth.c		spkr_synth		needs-flag

Index: src/sys/dev/spkr_synth.c
diff -u src/sys/dev/spkr_synth.c:1.1 src/sys/dev/spkr_synth.c:1.2
--- src/sys/dev/spkr_synth.c:1.1	Thu Dec  8 06:31:08 2016
+++ src/sys/dev/spkr_synth.c	Thu Dec  8 23:32:39 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: spkr_synth.c,v 1.1 2016/12/08 11:31:08 nat Exp $	*/
+/*	$NetBSD: spkr_synth.c,v 1.2 2016/12/09 04:32:39 christos Exp $	*/
 
 /*-
  * Copyright (c) 2016 Nathanial Sloss <nathanialsl...@yahoo.com.au>
@@ -26,9 +26,8 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifdef VAUDIOSPEAKER
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spkr_synth.c,v 1.1 2016/12/08 11:31:08 nat Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spkr_synth.c,v 1.2 2016/12/09 04:32:39 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -62,26 +61,29 @@ static int beep_sysctl_device(SYSCTLFN_P
 #include <dev/audiobellvar.h>
 
 #include <dev/spkrvar.h>
-#include <dev/isa/spkrio.h>
+#include <dev/spkrio.h>
 
-#include "isa/spkr.c"
-
-int spkrprobe(device_t, cfdata_t, void *);
-void spkrattach(device_t, device_t, void *);
-int spkrdetach(device_t, int);
+static void spkrattach(device_t, device_t, void *);
+static int spkrdetach(device_t, int);
 device_t speakerattach_mi(device_t);
 
 #include "ioconf.h"
 
 MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "audio" */);
 
+static int
+spkr_modcmd(modcmd_t cmd, void *arg)
+{
+	return spkr__modcmd(cmd, arg);
+}
+
 #ifdef _MODULE
 #include "ioconf.c"
 #endif
 
 
 CFATTACH_DECL3_NEW(spkr_synth, 0,
-    spkrprobe, spkrattach, spkrdetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
+    spkr_probe, spkrattach, spkrdetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
 
 extern struct cfdriver audio_cd;
 
@@ -97,17 +99,17 @@ struct spkr_attach_args {
 	device_t dev;
 };
 
-static void
-tone(u_int xhz, u_int ticks)
+void
+spkr_tone(u_int xhz, u_int ticks)
 {
 	audiobell(beep_index, xhz, ticks * (1000 / hz), 80, 0);
 }
 
-static void
-rest(int ticks)
+void
+spkr_rest(int ticks)
 {
 #ifdef SPKRDEBUG
-    printf("rest: %d\n", ticks);
+    printf("%s: %d\n", __func__, ticks);
 #endif /* SPKRDEBUG */
     if (ticks > 0)
 	audiobell(beep_index, 0, ticks * (1000 / hz), 80, 0);
@@ -121,7 +123,7 @@ speakerattach_mi(device_t dev)
 	return config_found(dev, &sa, NULL);
 }
 
-void
+static void
 spkrattach(device_t parent, device_t self, void *aux)
 {
 	const struct sysctlnode *node;
@@ -160,7 +162,7 @@ spkrattach(device_t parent, device_t sel
 	    bell_thread, &sc_bell_args, &sc_bellthread, "vbell");
 }
 
-int
+static int
 spkrdetach(device_t self, int flags)
 {
 
@@ -250,6 +252,3 @@ beep_sysctl_device(SYSCTLFN_ARGS)
 
 	return error;
 }
-
-#endif /* VAUDIOSPEAKER */
-/* spkr.c ends here */

Index: src/sys/dev/isa/files.isa
diff -u src/sys/dev/isa/files.isa:1.166 src/sys/dev/isa/files.isa:1.167
--- src/sys/dev/isa/files.isa:1.166	Thu Dec  8 21:22:34 2016
+++ src/sys/dev/isa/files.isa	Thu Dec  8 23:32:39 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: files.isa,v 1.166 2016/12/09 02:22:34 christos Exp $
+#	$NetBSD: files.isa,v 1.167 2016/12/09 04:32:39 christos Exp $
 #
 # Config file and device description for machine-independent ISA code.
 # Included by ports that need it.  Requires that the SCSI files be
@@ -435,7 +435,6 @@ attach	pcppi at isa
 file	dev/isa/pcppi.c			pcppi			needs-flag
 
 attach	spkr at pcppi with spkr_pcppi
-file	dev/isa/spkr.c			spkr			needs-flag
 file	dev/isa/spkr_pcppi.c		spkr_pcppi
 
 attach	midi at pcppi with midi_pcppi: midisyn

Index: src/sys/dev/isa/spkr_pcppi.c
diff -u src/sys/dev/isa/spkr_pcppi.c:1.1 src/sys/dev/isa/spkr_pcppi.c:1.2
--- src/sys/dev/isa/spkr_pcppi.c:1.1	Thu Dec  8 21:22:34 2016
+++ src/sys/dev/isa/spkr_pcppi.c	Thu Dec  8 23:32:39 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: spkr_pcppi.c,v 1.1 2016/12/09 02:22:34 christos Exp $	*/
+/*	$NetBSD: spkr_pcppi.c,v 1.2 2016/12/09 04:32:39 christos Exp $	*/
 
 /*
  * Copyright (c) 1990 Eric S. Raymond (e...@snark.thyrsus.com)
@@ -43,7 +43,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spkr_pcppi.c,v 1.1 2016/12/09 02:22:34 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spkr_pcppi.c,v 1.2 2016/12/09 04:32:39 christos Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -61,21 +61,27 @@ __KERNEL_RCSID(0, "$NetBSD: spkr_pcppi.c
 
 #include <dev/isa/pcppivar.h>
 
-#include <dev/isa/spkrio.h>
+#include <dev/spkrio.h>
 
-void spkrattach(device_t, device_t, void *);
-int spkrdetach(device_t, int);
-int spkrprobe(device_t, cfdata_t, void *);
+extern int spkr_attached;
+static void spkrattach(device_t, device_t, void *);
+static int spkrdetach(device_t, int);
 
 #include "ioconf.h"
 
 MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "pcppi" */);
 
+static int
+spkr_modcmd(modcmd_t cmd, void *arg)
+{
+	return spkr__modcmd(cmd, arg);
+}
+
 #ifdef _MODULE
 #include "ioconf.c"
 #endif
 
-CFATTACH_DECL_NEW(spkr_pcppi, 0, spkrprobe, spkrattach, spkrdetach, NULL);
+CFATTACH_DECL_NEW(spkr_pcppi, 0, spkr_probe, spkrattach, spkrdetach, NULL);
 
 static pcppi_tag_t ppicookie;
 
@@ -104,18 +110,7 @@ spkr_rest(int ticks)
 	    tsleep(spkr_rest, SPKRPRI | PCATCH, "rest", ticks);
 }
 
-extern int spkr_active;	/* exclusion flag */
-extern const struct cdevsw spkr_cdevsw;
-
-int spkr_attached = 0;
-
-int
-spkrprobe(device_t parent, cfdata_t match, void *aux)
-{
-	return (!spkr_attached);
-}
-
-void
+static void
 spkrattach(device_t parent, device_t self, void *aux)
 {
 	aprint_naive("\n");
@@ -126,7 +121,7 @@ spkrattach(device_t parent, device_t sel
 		aprint_error_dev(self, "couldn't establish power handler\n");
 }
 
-int
+static int
 spkrdetach(device_t self, int flags)
 {
 
@@ -136,44 +131,3 @@ spkrdetach(device_t self, int flags)
 
 	return 0;
 }
-
-
-static int
-spkr_modcmd(modcmd_t cmd, void *arg)
-{
-#ifdef _MODULE
-	devmajor_t bmajor, cmajor;
-#endif
-	int error = 0;
-
-#ifdef _MODULE
-	switch(cmd) {
-	case MODULE_CMD_INIT:
-		bmajor = cmajor = -1;
-		error = devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
-		    &spkr_cdevsw, &cmajor);
-		if (error)
-			break;
-
-		error = config_init_component(cfdriver_ioconf_spkr,
-			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
-		if (error) {
-			devsw_detach(NULL, &spkr_cdevsw);
-		}
-		break;
-
-	case MODULE_CMD_FINI:
-		if (spkr_active)
-			return EBUSY;
-		error = config_fini_component(cfdriver_ioconf_spkr,
-			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
-		devsw_detach(NULL, &spkr_cdevsw);
-		break;
-	default:
-		error = ENOTTY;
-		break;
-	}
-#endif
-
-	return error;
-}

Added files:

Index: src/sys/dev/spkr.c
diff -u /dev/null src/sys/dev/spkr.c:1.1
--- /dev/null	Thu Dec  8 23:32:39 2016
+++ src/sys/dev/spkr.c	Thu Dec  8 23:32:39 2016
@@ -0,0 +1,518 @@
+/*	$NetBSD: spkr.c,v 1.1 2016/12/09 04:32:39 christos Exp $	*/
+
+/*
+ * Copyright (c) 1990 Eric S. Raymond (e...@snark.thyrsus.com)
+ * Copyright (c) 1990 Andrew A. Chernov (a...@astral.msk.su)
+ * Copyright (c) 1990 Lennart Augustsson (lenn...@augustsson.net)
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by Eric S. Raymond
+ * 4. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
+ */
+
+/*
+ * spkr.c -- device driver for console speaker on 80386
+ *
+ * v1.1 by Eric S. Raymond (e...@snark.thyrsus.com) Feb 1990
+ *      modified for 386bsd by Andrew A. Chernov <a...@astral.msk.su>
+ *      386bsd only clean version, all SYSV stuff removed
+ *      use hz value from param.c
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.1 2016/12/09 04:32:39 christos Exp $");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/errno.h>
+#include <sys/device.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/uio.h>
+#include <sys/proc.h>
+#include <sys/ioctl.h>
+#include <sys/conf.h>
+
+#include <sys/bus.h>
+
+#include <dev/spkrio.h>
+
+dev_type_open(spkropen);
+dev_type_close(spkrclose);
+dev_type_write(spkrwrite);
+dev_type_ioctl(spkrioctl);
+
+const struct cdevsw spkr_cdevsw = {
+	.d_open = spkropen,
+	.d_close = spkrclose,
+	.d_read = noread,
+	.d_write = spkrwrite,
+	.d_ioctl = spkrioctl,
+	.d_stop = nostop,
+	.d_tty = notty,
+	.d_poll = nopoll,
+	.d_mmap = nommap,
+	.d_kqfilter = nokqfilter,
+	.d_discard = nodiscard,
+	.d_flag = D_OTHER
+};
+
+static void playinit(void);
+static void playtone(int, int, int);
+static void playstring(char *, int);
+
+/**************** PLAY STRING INTERPRETER BEGINS HERE **********************
+ *
+ * Play string interpretation is modelled on IBM BASIC 2.0's PLAY statement;
+ * M[LNS] are missing and the ~ synonym and octave-tracking facility is added.
+ * Requires spkr_tone(), spkr_rest(). String play is not interruptible
+ * except possibly at physical block boundaries.
+ */
+
+#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 const 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 const 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,
+};
+#define NOCTAVES (int)(__arraycount(pitchtab) / OCTAVE_NOTES)
+
+static void
+playinit(void)
+{
+    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) */
+}
+
+static void
+playtone(int pitch, int val, int sustain)
+/* play tone of proper duration for current rhythm signature */
+{
+    int	sound, silence, snum = 1, sdenom = 1;
+
+    /* this weirdness avoids floating-point arithmetic */
+    for (; sustain; sustain--)
+    {
+	snum *= NUM_MULT;
+	sdenom *= DENOM_MULT;
+    }
+
+    if (pitch == -1)
+	spkr_rest(whole * snum / (val * sdenom));
+    else
+    {
+	sound = (whole * snum) / (val * sdenom)
+		- (whole * (FILLTIME - fill)) / (val * FILLTIME);
+	silence = whole * (FILLTIME-fill) * snum / (FILLTIME * val * sdenom);
+
+#ifdef SPKRDEBUG
+	printf("playtone: pitch %d for %d ticks, rest for %d ticks\n",
+	    pitch, sound, silence);
+#endif /* SPKRDEBUG */
+
+	spkr_tone(pitchtab[pitch], sound);
+	if (fill != LEGATO)
+	    spkr_rest(silence);
+    }
+}
+
+static void
+playstring(char *cp, int slen)
+/* interpret and play an item from a notation string */
+{
+    int		pitch, lastpitch = OCTAVE_NOTES * DFLT_OCTAVE;
+
+#define GETNUM(cp, v)	for(v=0; slen > 0 && isdigit(cp[1]); ) \
+				{v = v * 10 + (*++cp - '0'); slen--;}
+    for (; slen--; cp++)
+    {
+	int		sustain, timeval, tempo;
+	char	c = toupper(*cp);
+
+#ifdef SPKRDEBUG
+	printf("playstring: %c (%x)\n", c, c);
+#endif /* SPKRDEBUG */
+
+	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 (slen > 0 && (cp[1] == '#' || cp[1] == '+'))
+	    {
+		++pitch;
+		++cp;
+		slen--;
+	    }
+	    else if (slen > 0 && 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))
+		{
+		    if (octave < NOCTAVES - 1) {
+			++octave;
+			pitch += OCTAVE_NOTES;
+		    }
+		}
+
+		if (abs(pitch-lastpitch) > abs((pitch-OCTAVE_NOTES)-lastpitch))
+		{
+		    if (octave > 0) {
+			--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; slen > 0 && cp[1] == '.'; cp++)
+	    {
+		slen--;
+		sustain++;
+	    }
+
+	    /* time to emit the actual tone */
+	    playtone(pitch, timeval, sustain);
+	    break;
+
+	case 'O':
+	    if (slen > 0 && (cp[1] == 'N' || cp[1] == 'n'))
+	    {
+		octprefix = octtrack = false;
+		++cp;
+		slen--;
+	    }
+	    else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l'))
+	    {
+		octtrack = true;
+		++cp;
+		slen--;
+	    }
+	    else
+	    {
+		GETNUM(cp, octave);
+		if (octave >= NOCTAVES)
+		    octave = DFLT_OCTAVE;
+		octprefix = true;
+	    }
+	    break;
+
+	case '>':
+	    if (octave < NOCTAVES - 1)
+		octave++;
+	    octprefix = true;
+	    break;
+
+	case '<':
+	    if (octave > 0)
+		octave--;
+	    octprefix = true;
+	    break;
+
+	case 'N':
+	    GETNUM(cp, pitch);
+	    for (sustain = 0; slen > 0 && cp[1] == '.'; cp++)
+	    {
+		slen--;
+		sustain++;
+	    }
+	    playtone(pitch - 1, value, sustain);
+	    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; slen > 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 (slen > 0 && (cp[1] == 'N' || cp[1] == 'n'))
+	    {
+		fill = NORMAL;
+		++cp;
+		slen--;
+	    }
+	    else if (slen > 0 && (cp[1] == 'L' || cp[1] == 'l'))
+	    {
+		fill = LEGATO;
+		++cp;
+		slen--;
+	    }
+	    else if (slen > 0 && (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 spkr_tone()
+ * and spkr_rest() functions defined above.
+ */
+
+static int spkr_active;	/* exclusion flag */
+int spkr_attached;
+static void *spkr_inbuf;
+
+int
+spkr_probe(device_t parent, cfdata_t match, void *aux)
+{
+	return (!spkr_attached);
+}
+
+int
+spkropen(dev_t dev, int	flags, int mode, struct lwp *l)
+{
+#ifdef SPKRDEBUG
+    printf("spkropen: entering with dev = %"PRIx64"\n", dev);
+#endif /* SPKRDEBUG */
+
+    if (minor(dev) != 0 || !spkr_attached)
+	return(ENXIO);
+    else if (spkr_active)
+	return(EBUSY);
+    else
+    {
+	playinit();
+	spkr_inbuf = malloc(DEV_BSIZE, M_DEVBUF, M_WAITOK);
+	spkr_active = 1;
+    }
+    return(0);
+}
+
+int
+spkrwrite(dev_t dev, struct uio *uio, int flags)
+{
+    int n;
+    int error;
+#ifdef SPKRDEBUG
+    printf("spkrwrite: entering with dev = %"PRIx64", count = %zu\n",
+		dev, uio->uio_resid);
+#endif /* SPKRDEBUG */
+
+    if (minor(dev) != 0)
+	return(ENXIO);
+    else
+    {
+	n = min(DEV_BSIZE, uio->uio_resid);
+	error = uiomove(spkr_inbuf, n, uio);
+	if (!error)
+		playstring((char *)spkr_inbuf, n);
+	return(error);
+    }
+}
+
+int
+spkrclose(dev_t dev, int flags, int mode, struct lwp *l)
+{
+#ifdef SPKRDEBUG
+    printf("spkrclose: entering with dev = %"PRIx64"\n", dev);
+#endif /* SPKRDEBUG */
+
+    if (minor(dev) != 0)
+	return(ENXIO);
+    else
+    {
+	spkr_tone(0, 0);
+	free(spkr_inbuf, M_DEVBUF);
+	spkr_active = 0;
+    }
+    return(0);
+}
+
+int
+spkrioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
+{
+#ifdef SPKRDEBUG
+    printf("spkrioctl: entering with dev = %"PRIx64", cmd = %lx\n", dev, cmd);
+#endif /* SPKRDEBUG */
+
+    if (minor(dev) != 0)
+	return(ENXIO);
+    else if (cmd == SPKRTONE)
+    {
+	tone_t	*tp = (tone_t *)data;
+
+	if (tp->frequency == 0)
+	    spkr_rest(tp->duration);
+	else
+	    spkr_tone(tp->frequency, tp->duration);
+    }
+    else if (cmd == SPKRTUNE)
+    {
+	tone_t  *tp = (tone_t *)(*(void **)data);
+	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)
+		spkr_rest(ttp.duration);
+	    else
+		spkr_tone(ttp.frequency, ttp.duration);
+	}
+    }
+    else
+	return(EINVAL);
+    return(0);
+}
+
+int
+spkr__modcmd(modcmd_t cmd, void *arg)
+{
+#ifdef _MODULE
+	devmajor_t bmajor, cmajor;
+#endif
+	int error = 0;
+
+#ifdef _MODULE
+	switch(cmd) {
+	case MODULE_CMD_INIT:
+		bmajor = cmajor = -1;
+		error = devsw_attach(spkr_cd.cd_name, NULL, &bmajor,
+		    &spkr_cdevsw, &cmajor);
+		if (error)
+			break;
+
+		error = config_init_component(cfdriver_ioconf_spkr,
+			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
+		if (error) {
+			devsw_detach(NULL, &spkr_cdevsw);
+		}
+		break;
+
+	case MODULE_CMD_FINI:
+		if (spkr_active)
+			return EBUSY;
+		error = config_fini_component(cfdriver_ioconf_spkr,
+			cfattach_ioconf_spkr, cfdata_ioconf_spkr);
+		devsw_detach(NULL, &spkr_cdevsw);
+		break;
+	default:
+		error = ENOTTY;
+		break;
+	}
+#endif
+
+	return error;
+}
Index: src/sys/dev/spkrio.h
diff -u /dev/null src/sys/dev/spkrio.h:1.1
--- /dev/null	Thu Dec  8 23:32:39 2016
+++ src/sys/dev/spkrio.h	Thu Dec  8 23:32:39 2016
@@ -0,0 +1,26 @@
+/*	$NetBSD: spkrio.h,v 1.1 2016/12/09 04:32:39 christos Exp $	*/
+
+/*
+ * spkr.h -- interface definitions for speaker ioctl()
+ */
+
+#ifndef _DEV_ISA_SPKR_H_
+#define _DEV_ISA_SPKR_H_
+
+#include <sys/ioccom.h>
+
+#define SPKRTONE        _IOW('S', 1, tone_t)    /* emit tone */
+#define SPKRTUNE        _IO('S', 2)             /* emit tone sequence */
+
+typedef struct {
+	int	frequency;	/* in hertz */
+	int	duration;	/* in 1/100ths of a second */
+} tone_t;
+
+void spkr_tone(u_int, u_int);
+void spkr_rest(int);
+int spkr__modcmd(modcmd_t, void *);
+int spkr_probe(device_t, cfdata_t, void *);
+extern int spkr_attached;
+
+#endif

Reply via email to