Module Name:    src
Committed By:   nat
Date:           Sun Jun 11 03:55:57 UTC 2017

Modified Files:
        src/sys/conf: files
        src/sys/dev: spkr.c spkrvar.h
        src/sys/dev/pckbport: pckbd.c
        src/sys/dev/wscons: files.wscons wsconsio.h wsmux.c wsmuxvar.h
Added Files:
        src/sys/dev/wscons: wsbell.c wsbellmux.c wsbellmuxvar.h wsbellvar.h

Log Message:
New device wsbell - allows for a console beep for non pckbds (usb etc).
Works for platforms without pcppi - can work with spkr at audio and spkr
at pcppi.

To use add the following to your kernel config:
wsbell* at spkr? console?

Ok pgoyette@.


To generate a diff of this commit:
cvs rdiff -u -r1.1175 -r1.1176 src/sys/conf/files
cvs rdiff -u -r1.8 -r1.9 src/sys/dev/spkr.c
cvs rdiff -u -r1.7 -r1.8 src/sys/dev/spkrvar.h
cvs rdiff -u -r1.32 -r1.33 src/sys/dev/pckbport/pckbd.c
cvs rdiff -u -r1.50 -r1.51 src/sys/dev/wscons/files.wscons
cvs rdiff -u -r0 -r1.1 src/sys/dev/wscons/wsbell.c \
    src/sys/dev/wscons/wsbellmux.c src/sys/dev/wscons/wsbellmuxvar.h \
    src/sys/dev/wscons/wsbellvar.h
cvs rdiff -u -r1.118 -r1.119 src/sys/dev/wscons/wsconsio.h
cvs rdiff -u -r1.61 -r1.62 src/sys/dev/wscons/wsmux.c
cvs rdiff -u -r1.15 -r1.16 src/sys/dev/wscons/wsmuxvar.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/conf/files
diff -u src/sys/conf/files:1.1175 src/sys/conf/files:1.1176
--- src/sys/conf/files:1.1175	Thu Jun  8 21:00:43 2017
+++ src/sys/conf/files	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: files,v 1.1175 2017/06/08 21:00:43 jmcneill Exp $
+#	$NetBSD: files,v 1.1176 2017/06/11 03:55:56 nat Exp $
 #	@(#)files.newconf	7.5 (Berkeley) 5/10/93
 
 version 	20150846
@@ -344,8 +344,8 @@ define	pckbport_machdep_cnattach
 define	firmload
 
 # speaker devices, attaches to audio or pcppi drivers
-device	spkr
-file	dev/spkr.c			spkr
+device	spkr: wsbelldev
+file	dev/spkr.c			spkr	needs-flag
 
 include "dev/files.audio"
 
@@ -1185,6 +1185,7 @@ define	wsdisplaydev		{[kbdmux = 1]}
 define	wsemuldisplaydev	{[console = -1], [kbdmux = 1]}
 define	wskbddev		{[console = -1], [mux = 1]}
 define	wsmousedev		{[mux = 0]}
+define	wsbelldev		{[console = -1], [mux = 1]}
 define	vcons
 # attribute to pull in raster support
 #

Index: src/sys/dev/spkr.c
diff -u src/sys/dev/spkr.c:1.8 src/sys/dev/spkr.c:1.9
--- src/sys/dev/spkr.c:1.8	Sun Jun 11 03:33:48 2017
+++ src/sys/dev/spkr.c	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: spkr.c,v 1.8 2017/06/11 03:33:48 nat Exp $	*/
+/*	$NetBSD: spkr.c,v 1.9 2017/06/11 03:55:56 nat Exp $	*/
 
 /*
  * Copyright (c) 1990 Eric S. Raymond (e...@snark.thyrsus.com)
@@ -43,7 +43,9 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.8 2017/06/11 03:33:48 nat Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.9 2017/06/11 03:55:56 nat Exp $");
+
+#include "wsmux.h"
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -61,6 +63,9 @@ __KERNEL_RCSID(0, "$NetBSD: spkr.c,v 1.8
 
 #include <dev/spkrio.h>
 #include <dev/spkrvar.h>
+#include <dev/wscons/wsconsio.h>
+#include <dev/wscons/wsbellvar.h>
+#include <dev/wscons/wsbellmuxvar.h>
 
 dev_type_open(spkropen);
 dev_type_close(spkrclose);
@@ -365,6 +370,13 @@ spkr_attach(device_t self, void (*tone)(
 	sc->sc_tone = tone;
 	sc->sc_rest = rest;
 	sc->sc_inbuf = NULL;
+
+#if (NWSMUX > 0)
+	struct wsbelldev_attach_args a;
+
+	a.accesscookie = sc;
+	sc->sc_wsbelldev = config_found(self, &a, wsbelldevprint);
+#endif
 }
 
 int

Index: src/sys/dev/spkrvar.h
diff -u src/sys/dev/spkrvar.h:1.7 src/sys/dev/spkrvar.h:1.8
--- src/sys/dev/spkrvar.h:1.7	Sun Jun 11 03:33:48 2017
+++ src/sys/dev/spkrvar.h	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: spkrvar.h,v 1.7 2017/06/11 03:33:48 nat Exp $ */
+/* $NetBSD: spkrvar.h,v 1.8 2017/06/11 03:55:56 nat Exp $ */
 
 #ifndef _SYS_DEV_SPKRVAR_H
 #define _SYS_DEV_SPKRVAR_H
@@ -7,6 +7,7 @@
 
 struct spkr_softc {
 	device_t sc_dev;
+	device_t sc_wsbelldev;
 	int sc_octave;	/* currently selected octave */
 	int sc_whole;	/* whole-note time at current tempo, in ticks */
 	int sc_value;	/* whole divisor for note time, quarter note = 1 */

Index: src/sys/dev/pckbport/pckbd.c
diff -u src/sys/dev/pckbport/pckbd.c:1.32 src/sys/dev/pckbport/pckbd.c:1.33
--- src/sys/dev/pckbport/pckbd.c:1.32	Thu Jul 16 15:01:04 2015
+++ src/sys/dev/pckbport/pckbd.c	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: pckbd.c,v 1.32 2015/07/16 15:01:04 prlw1 Exp $ */
+/* $NetBSD: pckbd.c,v 1.33 2017/06/11 03:55:56 nat Exp $ */
 
 /*-
  * Copyright (c) 1998, 2009 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pckbd.c,v 1.32 2015/07/16 15:01:04 prlw1 Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pckbd.c,v 1.33 2017/06/11 03:55:56 nat Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -1010,6 +1010,7 @@ pckbd_ioctl(void *v, u_long cmd, void *d
 	case WSKBDIO_GETLEDS:
 		*(int *)data = pckbd_led_decode(sc->sc_ledstate);
 		return 0;
+#if 0
 	case WSKBDIO_COMPLEXBELL:
 #define d ((struct wskbd_bell_data *)data)
 		/*
@@ -1019,6 +1020,7 @@ pckbd_ioctl(void *v, u_long cmd, void *d
 		pckbd_bell(d->pitch, d->period, d->volume, 0);
 #undef d
 		return 0;
+#endif
 #ifdef WSDISPLAY_COMPAT_RAWKBD
 	case WSKBDIO_SETMODE:
 		sc->rawkbd = (*(int *)data == WSKBD_RAW);

Index: src/sys/dev/wscons/files.wscons
diff -u src/sys/dev/wscons/files.wscons:1.50 src/sys/dev/wscons/files.wscons:1.51
--- src/sys/dev/wscons/files.wscons:1.50	Fri May 19 19:22:33 2017
+++ src/sys/dev/wscons/files.wscons	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-# $NetBSD: files.wscons,v 1.50 2017/05/19 19:22:33 macallan Exp $
+# $NetBSD: files.wscons,v 1.51 2017/06/11 03:55:56 nat Exp $
 
 #
 # "Workstation Console" glue; attaches frame buffer to emulator & keyboard,
@@ -43,6 +43,8 @@ device	wskbd
 attach	wskbd at wskbddev
 device	wsmouse
 attach	wsmouse at wsmousedev
+device	wsbell
+attach	wsbell at wsbelldev
 
 file	dev/wscons/wsdisplay.c		wsdisplay		needs-flag
 file	dev/wscons/wsdisplay_compat_usl.c wsdisplay & wsdisplay_compat_usl
@@ -58,6 +60,7 @@ file	dev/wscons/wsevent.c		wsdisplay | w
 file	dev/wscons/wskbd.c		wskbd			needs-flag
 file	dev/wscons/wskbdutil.c		wskbd			needs-flag
 file	dev/wscons/wsmouse.c		wsmouse			needs-flag
+file	dev/wscons/wsbell.c		wsbell			needs-flag
 
 # rcons bit-depth options
 include "dev/rcons/files.rcons"
@@ -67,6 +70,7 @@ file	dev/wscons/wscons_rops.c	wsrasterem
 
 defpseudo	wsmux
 file	dev/wscons/wsmux.c		wsmux			needs-flag
+file	dev/wscons/wsbellmux.c		wsmux			needs-flag
 
 define	tpcalib
 file	dev/wscons/tpcalib.c		tpcalib

Index: src/sys/dev/wscons/wsconsio.h
diff -u src/sys/dev/wscons/wsconsio.h:1.118 src/sys/dev/wscons/wsconsio.h:1.119
--- src/sys/dev/wscons/wsconsio.h:1.118	Sat Jun  3 14:49:42 2017
+++ src/sys/dev/wscons/wsconsio.h	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-/* $NetBSD: wsconsio.h,v 1.118 2017/06/03 14:49:42 jmcneill Exp $ */
+/* $NetBSD: wsconsio.h,v 1.119 2017/06/11 03:55:56 nat Exp $ */
 
 /*
  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
@@ -544,6 +544,7 @@ struct wsmux_device {
 #define	WSMUX_MOUSE	1
 #define	WSMUX_KBD	2
 #define	WSMUX_MUX	3
+#define	WSMUX_BELL	4
 	int idx;
 };
 #define	WSMUXIO_ADD_DEVICE	_IOW('W', 97, struct wsmux_device)

Index: src/sys/dev/wscons/wsmux.c
diff -u src/sys/dev/wscons/wsmux.c:1.61 src/sys/dev/wscons/wsmux.c:1.62
--- src/sys/dev/wscons/wsmux.c:1.61	Thu Jul  7 06:55:42 2016
+++ src/sys/dev/wscons/wsmux.c	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: wsmux.c,v 1.61 2016/07/07 06:55:42 msaitoh Exp $	*/
+/*	$NetBSD: wsmux.c,v 1.62 2017/06/11 03:55:56 nat Exp $	*/
 
 /*
  * Copyright (c) 1998, 2005 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: wsmux.c,v 1.61 2016/07/07 06:55:42 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: wsmux.c,v 1.62 2017/06/11 03:55:56 nat Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -48,6 +48,7 @@ __KERNEL_RCSID(0, "$NetBSD: wsmux.c,v 1.
 #include "wsmux.h"
 #include "wskbd.h"
 #include "wsmouse.h"
+#include "wsbell.h"
 
 #include <sys/param.h>
 #include <sys/conf.h>
@@ -456,6 +457,8 @@ wsmux_do_ioctl(device_t dv, u_long cmd, 
 #endif
 		case WSMUX_MUX:
 			return (wsmux_add_mux(d->idx, sc));
+		case WSMUX_BELL:
+			return (wsbell_add_mux(d->idx, sc));
 		default:
 			return (EINVAL);
 		}

Index: src/sys/dev/wscons/wsmuxvar.h
diff -u src/sys/dev/wscons/wsmuxvar.h:1.15 src/sys/dev/wscons/wsmuxvar.h:1.16
--- src/sys/dev/wscons/wsmuxvar.h:1.15	Sat Nov 23 20:56:41 2013
+++ src/sys/dev/wscons/wsmuxvar.h	Sun Jun 11 03:55:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: wsmuxvar.h,v 1.15 2013/11/23 20:56:41 christos Exp $	*/
+/*	$NetBSD: wsmuxvar.h,v 1.16 2017/06/11 03:55:56 nat Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -89,5 +89,6 @@ int	wsmux_set_display(struct wsmux_softc
 
 int	wskbd_add_mux(int, struct wsmux_softc *);
 int	wsmouse_add_mux(int, struct wsmux_softc *);
+int	wsbell_add_mux(int, struct wsmux_softc *);
 
 #endif /* NWSMUX > 0 */

Added files:

Index: src/sys/dev/wscons/wsbell.c
diff -u /dev/null src/sys/dev/wscons/wsbell.c:1.1
--- /dev/null	Sun Jun 11 03:55:57 2017
+++ src/sys/dev/wscons/wsbell.c	Sun Jun 11 03:55:56 2017
@@ -0,0 +1,463 @@
+/* $NetBSD: wsbell.c,v 1.1 2017/06/11 03:55:56 nat Exp $ */
+
+/*-
+ * Copyright (c) 2017 Nathanial Sloss <nathanialsl...@yahoo.com.au>
+ * All rights reserved.
+ *
+ * Copyright (c) 2006 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Julio M. Merino Vidal.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
+ * Copyright (c) 1996, 1997 Christopher G. Demetriou.  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 Christopher G. Demetriou
+ *	for the NetBSD Project.
+ * 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.
+ */
+
+/*
+ * Copyright (c) 1992, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * This software was developed by the Computer Systems Engineering group
+ * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
+ * contributed to Berkeley.
+ *
+ * All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Lawrence Berkeley Laboratory.
+ *
+ * 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. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *	@(#)ms.c	8.1 (Berkeley) 6/11/93
+ */
+
+/*
+ * Keyboard Bell driver.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: wsbell.c,v 1.1 2017/06/11 03:55:56 nat Exp $");
+
+#include "wsdisplay.h"
+#include "wsmux.h"
+
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/ioctl.h>
+#include <sys/poll.h>
+#include <sys/fcntl.h>
+#include <sys/kernel.h>
+#include <sys/condvar.h>
+#include <sys/mutex.h>
+#include <sys/kthread.h>
+#include <sys/proc.h>
+#include <sys/syslog.h>
+#include <sys/systm.h>
+#include <sys/tty.h>
+#include <sys/signalvar.h>
+#include <sys/device.h>
+#include <sys/vnode.h>
+#include <sys/callout.h>
+#include <sys/malloc.h>
+
+#include <dev/wscons/wsconsio.h>
+#include <dev/wscons/wsbellvar.h>
+#include <dev/wscons/wsbellmuxvar.h>
+#include <dev/wscons/wsbelldata.h>
+
+#include <dev/spkrio.h>
+
+#if defined(WSMUX_DEBUG) && NWSMUX > 0
+#define DPRINTF(x)	if (wsmuxdebug) printf x
+#define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
+extern int wsmuxdebug;
+#else
+#define DPRINTF(x)
+#define DPRINTFN(n,x)
+#endif
+
+static void bell_thread(void *);
+static inline void spkr_audio_play(struct wsbell_softc *, u_int, u_int, u_int);
+
+static int  wsbell_match(device_t, cfdata_t, void *);
+static void wsbell_attach(device_t, device_t, void *);
+static int  wsbell_detach(device_t, int);
+static int  wsbell_activate(device_t, enum devact);
+
+#if NWSMUX > 0
+static int  wsbell_mux_open(struct wsevsrc *, struct wseventvar *);
+static int  wsbell_mux_close(struct wsevsrc *);
+
+static int  wsbelldoopen(struct wsbell_softc *, struct wseventvar *);
+static int  wsbelldoioctl(device_t, u_long, void *, int, struct lwp *);
+
+static int  wsbell_do_ioctl(struct wsbell_softc *, u_long, void *,
+			     int, struct lwp *);
+
+#endif
+
+CFATTACH_DECL_NEW(wsbell, sizeof (struct wsbell_softc),
+    wsbell_match, wsbell_attach, wsbell_detach, wsbell_activate);
+
+extern struct cfdriver wsbell_cd;
+
+extern dev_type_open(spkropen);
+extern dev_type_close(spkrclose);
+extern dev_type_ioctl(spkrioctl);
+
+const struct cdevsw wsbell_cdevsw = {
+	.d_open = noopen,
+	.d_close = noclose,
+	.d_read = noread,
+	.d_write = nowrite,
+	.d_ioctl = noioctl,
+	.d_stop = nostop,
+	.d_tty = notty,
+	.d_poll = nopoll,
+	.d_mmap = nommap,
+	.d_kqfilter = nokqfilter,
+	.d_discard = nodiscard,
+	.d_flag = D_OTHER
+};
+
+#if NWSMUX > 0
+struct wssrcops wsbell_srcops = {
+	WSMUX_BELL,
+	wsbell_mux_open, wsbell_mux_close, wsbelldoioctl, wsbelldoioctl, NULL
+};
+#endif
+
+int
+wsbell_match(device_t parent, cfdata_t match, void *aux)
+{
+	return (1);
+}
+
+void
+wsbell_attach(device_t parent, device_t self, void *aux)
+{
+        struct wsbell_softc *sc = device_private(self);
+	struct wsbelldev_attach_args *ap = aux;
+#if NWSMUX > 0
+	int mux, error;
+#endif
+
+	sc->sc_base.me_dv = self;
+	sc->sc_accesscookie = ap->accesscookie;
+
+	sc->sc_spkr = device_unit(parent);
+	sc->sc_bell_data = wskbd_default_bell_data;
+#if NWSMUX > 0
+	sc->sc_base.me_ops = &wsbell_srcops;
+	mux = device_cfdata(self)->wsbelldevcf_mux;
+	if (mux >= 0) {
+		error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
+		if (error)
+			aprint_error(" attach error=%d", error);
+		else
+			aprint_normal(" mux %d", mux);
+	}
+#else
+	if (device_cfdata(self)->wsbelldevcf_mux >= 0)
+		aprint_normal(" (mux ignored)");
+#endif
+
+	aprint_naive("\n");
+	aprint_normal("\n");
+
+	if (!pmf_device_register(self, NULL, NULL))
+		aprint_error_dev(self, "couldn't establish power handler\n");
+
+	mutex_init(&sc->sc_bellock, MUTEX_DEFAULT, IPL_SCHED);
+	cv_init(&sc->sc_bellcv, "bellcv");
+
+	kthread_create(PRI_BIO, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
+	    bell_thread, sc, &sc->sc_bellthread, "%s", device_xname(self));
+}
+
+int
+wsbell_activate(device_t self, enum devact act)
+{
+	struct wsbell_softc *sc = device_private(self);
+
+	if (act == DVACT_DEACTIVATE)
+		sc->sc_dying = 1;
+	return (0);
+}
+
+int
+wsbell_detach(device_t self, int flags)
+{
+	struct wsbell_softc *sc = device_private(self);
+	struct wseventvar *evar;
+	int maj, mn;
+	int s;
+
+#if NWSMUX > 0
+	/* Tell parent mux we're leaving. */
+	if (sc->sc_base.me_parent != NULL) {
+		DPRINTF(("wsbell_detach:\n"));
+		wsmux_detach_sc(&sc->sc_base);
+	}
+#endif
+
+	/* If we're open ... */
+	evar = sc->sc_base.me_evp;
+	if (evar != NULL && evar->io != NULL) {
+		s = spltty();
+		if (--sc->sc_refcnt >= 0) {
+			struct wscons_event event;
+
+			/* Wake everyone by generating a dummy event. */
+			event.type = 0;
+			event.value = 0;
+			if (wsevent_inject(evar, &event, 1) != 0)
+				wsevent_wakeup(evar);
+
+			/* Wait for processes to go away. */
+			if (tsleep(sc, PZERO, "wsmdet", hz * 60))
+				printf("wsbell_detach: %s didn't detach\n",
+				       device_xname(self));
+		}
+		splx(s);
+	}
+
+	/* locate the major number */
+	maj = cdevsw_lookup_major(&wsbell_cdevsw);
+
+	/* Nuke the vnodes for any open instances (calls close). */
+	mn = device_unit(self);
+	vdevgone(maj, mn, mn, VCHR);
+
+	mutex_enter(&sc->sc_bellock);
+	sc->sc_bell_args.dying = true;
+
+	cv_broadcast(&sc->sc_bellcv);
+	mutex_exit(&sc->sc_bellock);
+
+	kthread_join(sc->sc_bellthread);
+	cv_destroy(&sc->sc_bellcv);
+	mutex_destroy(&sc->sc_bellock);
+
+	return (0);
+}
+
+#if NWSMUX > 0
+int
+wsbelldoopen(struct wsbell_softc *sc, struct wseventvar *evp)
+{
+	return (0);
+}
+
+/* A wrapper around the ioctl() workhorse to make reference counting easy. */
+int
+wsbelldoioctl(device_t dv, u_long cmd, void *data, int flag,
+	       struct lwp *l)
+{
+	struct wsbell_softc *sc = device_private(dv);
+	int error;
+
+	sc->sc_refcnt++;
+	error = wsbell_do_ioctl(sc, cmd, data, flag, l);
+	if (--sc->sc_refcnt < 0)
+		wakeup(sc);
+	return (error);
+}
+
+int
+wsbell_do_ioctl(struct wsbell_softc *sc, u_long cmd, void *data,
+		 int flag, struct lwp *l)
+{
+	struct wskbd_bell_data *ubdp, *kbdp;
+	if (sc->sc_dying)
+		return (EIO);
+
+	/*
+	 * Try the wsbell specific ioctls.
+	 */
+	switch (cmd) {
+#define	SETBELL(dstp, srcp, dfltp)					\
+    do {								\
+	(dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ?		\
+	    (srcp)->pitch : (dfltp)->pitch;				\
+	(dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ?	\
+	    (srcp)->period : (dfltp)->period;				\
+	(dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ?	\
+	    (srcp)->volume : (dfltp)->volume;				\
+	(dstp)->which = WSKBD_BELL_DOALL;				\
+    } while (0)
+
+	case WSKBDIO_SETBELL:
+		if ((flag & FWRITE) == 0)
+			return (EACCES);
+		kbdp = &sc->sc_bell_data;
+		ubdp = (struct wskbd_bell_data *)data;
+		SETBELL(kbdp, ubdp, kbdp);
+		return (0);
+
+	case WSKBDIO_GETBELL:
+		kbdp = &sc->sc_bell_data;
+		ubdp = (struct wskbd_bell_data *)data;
+		SETBELL(ubdp, kbdp, kbdp);
+		return (0);
+
+	case WSKBDIO_BELL:
+		if ((flag & FWRITE) == 0)
+			return (EACCES);
+		spkr_audio_play(sc, sc->sc_bell_data.pitch,
+		    sc->sc_bell_data.period, sc->sc_bell_data.volume);
+
+		return 0;
+
+	case WSKBDIO_COMPLEXBELL:
+		if ((flag & FWRITE) == 0)
+			return (EACCES);
+		if (data == NULL)
+			return 0;
+#define d ((struct wskbd_bell_data *)data)
+		spkr_audio_play(sc, d->pitch, d->period, d->volume);
+#undef d
+		return 0;
+	}	
+
+	return (EPASSTHROUGH);
+}
+#endif
+
+static void
+bell_thread(void *arg)
+{
+	struct wsbell_softc *sc = arg;
+	struct vbell_args *vb = &sc->sc_bell_args;
+	tone_t tone;
+	u_int vol;
+	
+	for (;;) {
+		mutex_enter(&sc->sc_bellock);
+		cv_wait_sig(&sc->sc_bellcv, &sc->sc_bellock);
+		
+		if (vb->dying == true) {
+			mutex_exit(&sc->sc_bellock);
+			kthread_exit(0);
+		}
+		
+		tone.frequency = vb->pitch;
+		tone.duration = vb->period;
+		vol = vb->volume;
+		mutex_exit(&sc->sc_bellock);
+
+		if (spkropen(sc->sc_spkr, FWRITE, 0, NULL) != 0)
+			continue;
+		spkrioctl(sc->sc_spkr, SPKRSETVOL, &vol, 0, curlwp);
+		spkrioctl(sc->sc_spkr, SPKRTONE, &tone, 0, curlwp);
+		spkrclose(sc->sc_spkr, FWRITE, 0, curlwp);
+	}
+}
+
+static inline void
+spkr_audio_play(struct wsbell_softc *sc, u_int pitch, u_int period, u_int volume)
+{
+
+	mutex_enter(&sc->sc_bellock);
+	sc->sc_bell_args.dying = false;
+	sc->sc_bell_args.pitch = pitch;
+	sc->sc_bell_args.period = period / 5;
+	sc->sc_bell_args.volume = volume;
+
+	cv_broadcast(&sc->sc_bellcv);
+	mutex_exit(&sc->sc_bellock);
+}
+
+#if NWSMUX > 0
+int
+wsbell_mux_open(struct wsevsrc *me, struct wseventvar *evp)
+{
+	struct wsbell_softc *sc = (struct wsbell_softc *)me;
+
+	if (sc->sc_base.me_evp != NULL)
+		return (EBUSY);
+
+	return wsbelldoopen(sc, evp);
+}
+
+int
+wsbell_mux_close(struct wsevsrc *me)
+{
+	struct wsbell_softc *sc = (struct wsbell_softc *)me;
+
+	sc->sc_base.me_evp = NULL;
+
+	return (0);
+}
+#endif /* NWSMUX > 0 */
Index: src/sys/dev/wscons/wsbellmux.c
diff -u /dev/null src/sys/dev/wscons/wsbellmux.c:1.1
--- /dev/null	Sun Jun 11 03:55:57 2017
+++ src/sys/dev/wscons/wsbellmux.c	Sun Jun 11 03:55:56 2017
@@ -0,0 +1,79 @@
+/* $NetBSD: wsbellmux.c,v 1.1 2017/06/11 03:55:56 nat Exp $ */
+/*-
+ * Copyright (c) 2017 Nathanial Sloss <nathanialsl...@yahoo.com.au>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: wsbellmux.c,v 1.1 2017/06/11 03:55:56 nat Exp $");
+
+#include <sys/param.h>
+#include <sys/conf.h>
+#include <sys/types.h>
+#include <sys/device.h>
+#include <sys/errno.h>
+#include <sys/selinfo.h>
+
+#include "wsmux.h"
+
+#include <dev/wscons/wsconsio.h>
+#include <dev/wscons/wsbellvar.h>
+
+/*
+ * Print function (for parent devices).
+ */
+int
+wsbelldevprint(void *aux, const char *pnp)
+{
+
+	if (pnp)
+		aprint_normal("wsbell at %s", pnp);
+	return (UNCONF);
+}
+
+#if NWSMUX > 0
+int
+wsbell_add_mux(int unit, struct wsmux_softc *muxsc)
+{
+	struct wsbell_softc *sc;
+	device_t wsbelldev;
+	cfdriver_t wsbellcd;
+
+	wsbelldev = device_find_by_driver_unit("wsbell", unit);
+	if (wsbelldev == NULL)
+		return ENXIO;
+	wsbellcd = device_cfdriver(wsbelldev);
+	if (wsbellcd == NULL)
+		return ENXIO;
+
+	sc = device_lookup_private(wsbellcd, unit);
+	if (sc == NULL)
+		return ENXIO;
+
+	if (sc->sc_base.me_parent != NULL || sc->sc_base.me_evp != NULL)
+		return (EBUSY);
+
+	return (wsmux_attach_sc(muxsc, &sc->sc_base));
+}
+#endif
Index: src/sys/dev/wscons/wsbellmuxvar.h
diff -u /dev/null src/sys/dev/wscons/wsbellmuxvar.h:1.1
--- /dev/null	Sun Jun 11 03:55:57 2017
+++ src/sys/dev/wscons/wsbellmuxvar.h	Sun Jun 11 03:55:56 2017
@@ -0,0 +1,31 @@
+/* $NetBSD: wsbellmuxvar.h,v 1.1 2017/06/11 03:55:56 nat Exp $ */
+/*-
+ * Copyright (c) 2017 Nathanial Sloss <nathanialsl...@yahoo.com.au>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+struct wsbelldev_attach_args {
+	void	*accesscookie;				/* access cookie */
+};
+
Index: src/sys/dev/wscons/wsbellvar.h
diff -u /dev/null src/sys/dev/wscons/wsbellvar.h:1.1
--- /dev/null	Sun Jun 11 03:55:57 2017
+++ src/sys/dev/wscons/wsbellvar.h	Sun Jun 11 03:55:56 2017
@@ -0,0 +1,61 @@
+/* $NetBSD: wsbellvar.h,v 1.1 2017/06/11 03:55:56 nat Exp $ */
+/*-
+ * Copyright (c) 2017 Nathanial Sloss <nathanialsl...@yahoo.com.au>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "locators.h"
+#include <dev/wscons/wseventvar.h>
+#include <dev/wscons/wsmuxvar.h>
+
+#define	wsbelldevcf_mux	cf_loc[WSBELLDEVCF_MUX]
+
+struct vbell_args {
+	u_int pitch;
+	u_int period;
+	u_int volume;
+	bool dying;
+};
+
+struct wsbell_softc {
+	struct wsevsrc	sc_base;
+	dev_t		sc_spkr;	/* our spkr device */
+	struct wskbd_bell_data sc_bell_data;
+
+	void		*sc_accesscookie;
+
+	int		sc_refcnt;
+	u_char		sc_dying;	/* device is being detached */
+
+	lwp_t		*sc_bellthread;
+	kmutex_t	sc_bellock;
+	kcondvar_t	sc_bellcv;
+
+	struct vbell_args sc_bell_args;
+};
+
+/*
+ * Autoconfiguration helper functions.
+ */
+int	wsbelldevprint(void *, const char *);

Reply via email to