Home | History | Annotate | Line # | Download | only in wscons
wsbell.c revision 1.1
      1  1.1  nat /* $NetBSD: wsbell.c,v 1.1 2017/06/11 03:55:56 nat Exp $ */
      2  1.1  nat 
      3  1.1  nat /*-
      4  1.1  nat  * Copyright (c) 2017 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
      5  1.1  nat  * All rights reserved.
      6  1.1  nat  *
      7  1.1  nat  * Copyright (c) 2006 The NetBSD Foundation, Inc.
      8  1.1  nat  * All rights reserved.
      9  1.1  nat  *
     10  1.1  nat  * This code is derived from software contributed to The NetBSD Foundation
     11  1.1  nat  * by Julio M. Merino Vidal.
     12  1.1  nat  *
     13  1.1  nat  * Redistribution and use in source and binary forms, with or without
     14  1.1  nat  * modification, are permitted provided that the following conditions
     15  1.1  nat  * are met:
     16  1.1  nat  * 1. Redistributions of source code must retain the above copyright
     17  1.1  nat  *    notice, this list of conditions and the following disclaimer.
     18  1.1  nat  * 2. Redistributions in binary form must reproduce the above copyright
     19  1.1  nat  *    notice, this list of conditions and the following disclaimer in the
     20  1.1  nat  *    documentation and/or other materials provided with the distribution.
     21  1.1  nat  *
     22  1.1  nat  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     23  1.1  nat  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24  1.1  nat  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25  1.1  nat  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     26  1.1  nat  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     27  1.1  nat  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     28  1.1  nat  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     29  1.1  nat  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     30  1.1  nat  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     31  1.1  nat  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  1.1  nat  * POSSIBILITY OF SUCH DAMAGE.
     33  1.1  nat  */
     34  1.1  nat 
     35  1.1  nat /*
     36  1.1  nat  * Copyright (c) 1996, 1997 Christopher G. Demetriou.  All rights reserved.
     37  1.1  nat  *
     38  1.1  nat  * Redistribution and use in source and binary forms, with or without
     39  1.1  nat  * modification, are permitted provided that the following conditions
     40  1.1  nat  * are met:
     41  1.1  nat  * 1. Redistributions of source code must retain the above copyright
     42  1.1  nat  *    notice, this list of conditions and the following disclaimer.
     43  1.1  nat  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.1  nat  *    notice, this list of conditions and the following disclaimer in the
     45  1.1  nat  *    documentation and/or other materials provided with the distribution.
     46  1.1  nat  * 3. All advertising materials mentioning features or use of this software
     47  1.1  nat  *    must display the following acknowledgement:
     48  1.1  nat  *      This product includes software developed by Christopher G. Demetriou
     49  1.1  nat  *	for the NetBSD Project.
     50  1.1  nat  * 4. The name of the author may not be used to endorse or promote products
     51  1.1  nat  *    derived from this software without specific prior written permission
     52  1.1  nat  *
     53  1.1  nat  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     54  1.1  nat  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     55  1.1  nat  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     56  1.1  nat  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     57  1.1  nat  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     58  1.1  nat  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     59  1.1  nat  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     60  1.1  nat  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     61  1.1  nat  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     62  1.1  nat  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     63  1.1  nat  */
     64  1.1  nat 
     65  1.1  nat /*
     66  1.1  nat  * Copyright (c) 1992, 1993
     67  1.1  nat  *	The Regents of the University of California.  All rights reserved.
     68  1.1  nat  *
     69  1.1  nat  * This software was developed by the Computer Systems Engineering group
     70  1.1  nat  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     71  1.1  nat  * contributed to Berkeley.
     72  1.1  nat  *
     73  1.1  nat  * All advertising materials mentioning features or use of this software
     74  1.1  nat  * must display the following acknowledgement:
     75  1.1  nat  *	This product includes software developed by the University of
     76  1.1  nat  *	California, Lawrence Berkeley Laboratory.
     77  1.1  nat  *
     78  1.1  nat  * Redistribution and use in source and binary forms, with or without
     79  1.1  nat  * modification, are permitted provided that the following conditions
     80  1.1  nat  * are met:
     81  1.1  nat  * 1. Redistributions of source code must retain the above copyright
     82  1.1  nat  *    notice, this list of conditions and the following disclaimer.
     83  1.1  nat  * 2. Redistributions in binary form must reproduce the above copyright
     84  1.1  nat  *    notice, this list of conditions and the following disclaimer in the
     85  1.1  nat  *    documentation and/or other materials provided with the distribution.
     86  1.1  nat  * 3. Neither the name of the University nor the names of its contributors
     87  1.1  nat  *    may be used to endorse or promote products derived from this software
     88  1.1  nat  *    without specific prior written permission.
     89  1.1  nat  *
     90  1.1  nat  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     91  1.1  nat  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     92  1.1  nat  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     93  1.1  nat  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     94  1.1  nat  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     95  1.1  nat  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     96  1.1  nat  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     97  1.1  nat  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     98  1.1  nat  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     99  1.1  nat  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    100  1.1  nat  * SUCH DAMAGE.
    101  1.1  nat  *
    102  1.1  nat  *	@(#)ms.c	8.1 (Berkeley) 6/11/93
    103  1.1  nat  */
    104  1.1  nat 
    105  1.1  nat /*
    106  1.1  nat  * Keyboard Bell driver.
    107  1.1  nat  */
    108  1.1  nat 
    109  1.1  nat #include <sys/cdefs.h>
    110  1.1  nat __KERNEL_RCSID(0, "$NetBSD: wsbell.c,v 1.1 2017/06/11 03:55:56 nat Exp $");
    111  1.1  nat 
    112  1.1  nat #include "wsdisplay.h"
    113  1.1  nat #include "wsmux.h"
    114  1.1  nat 
    115  1.1  nat #include <sys/param.h>
    116  1.1  nat #include <sys/conf.h>
    117  1.1  nat #include <sys/ioctl.h>
    118  1.1  nat #include <sys/poll.h>
    119  1.1  nat #include <sys/fcntl.h>
    120  1.1  nat #include <sys/kernel.h>
    121  1.1  nat #include <sys/condvar.h>
    122  1.1  nat #include <sys/mutex.h>
    123  1.1  nat #include <sys/kthread.h>
    124  1.1  nat #include <sys/proc.h>
    125  1.1  nat #include <sys/syslog.h>
    126  1.1  nat #include <sys/systm.h>
    127  1.1  nat #include <sys/tty.h>
    128  1.1  nat #include <sys/signalvar.h>
    129  1.1  nat #include <sys/device.h>
    130  1.1  nat #include <sys/vnode.h>
    131  1.1  nat #include <sys/callout.h>
    132  1.1  nat #include <sys/malloc.h>
    133  1.1  nat 
    134  1.1  nat #include <dev/wscons/wsconsio.h>
    135  1.1  nat #include <dev/wscons/wsbellvar.h>
    136  1.1  nat #include <dev/wscons/wsbellmuxvar.h>
    137  1.1  nat #include <dev/wscons/wsbelldata.h>
    138  1.1  nat 
    139  1.1  nat #include <dev/spkrio.h>
    140  1.1  nat 
    141  1.1  nat #if defined(WSMUX_DEBUG) && NWSMUX > 0
    142  1.1  nat #define DPRINTF(x)	if (wsmuxdebug) printf x
    143  1.1  nat #define DPRINTFN(n,x)	if (wsmuxdebug > (n)) printf x
    144  1.1  nat extern int wsmuxdebug;
    145  1.1  nat #else
    146  1.1  nat #define DPRINTF(x)
    147  1.1  nat #define DPRINTFN(n,x)
    148  1.1  nat #endif
    149  1.1  nat 
    150  1.1  nat static void bell_thread(void *);
    151  1.1  nat static inline void spkr_audio_play(struct wsbell_softc *, u_int, u_int, u_int);
    152  1.1  nat 
    153  1.1  nat static int  wsbell_match(device_t, cfdata_t, void *);
    154  1.1  nat static void wsbell_attach(device_t, device_t, void *);
    155  1.1  nat static int  wsbell_detach(device_t, int);
    156  1.1  nat static int  wsbell_activate(device_t, enum devact);
    157  1.1  nat 
    158  1.1  nat #if NWSMUX > 0
    159  1.1  nat static int  wsbell_mux_open(struct wsevsrc *, struct wseventvar *);
    160  1.1  nat static int  wsbell_mux_close(struct wsevsrc *);
    161  1.1  nat 
    162  1.1  nat static int  wsbelldoopen(struct wsbell_softc *, struct wseventvar *);
    163  1.1  nat static int  wsbelldoioctl(device_t, u_long, void *, int, struct lwp *);
    164  1.1  nat 
    165  1.1  nat static int  wsbell_do_ioctl(struct wsbell_softc *, u_long, void *,
    166  1.1  nat 			     int, struct lwp *);
    167  1.1  nat 
    168  1.1  nat #endif
    169  1.1  nat 
    170  1.1  nat CFATTACH_DECL_NEW(wsbell, sizeof (struct wsbell_softc),
    171  1.1  nat     wsbell_match, wsbell_attach, wsbell_detach, wsbell_activate);
    172  1.1  nat 
    173  1.1  nat extern struct cfdriver wsbell_cd;
    174  1.1  nat 
    175  1.1  nat extern dev_type_open(spkropen);
    176  1.1  nat extern dev_type_close(spkrclose);
    177  1.1  nat extern dev_type_ioctl(spkrioctl);
    178  1.1  nat 
    179  1.1  nat const struct cdevsw wsbell_cdevsw = {
    180  1.1  nat 	.d_open = noopen,
    181  1.1  nat 	.d_close = noclose,
    182  1.1  nat 	.d_read = noread,
    183  1.1  nat 	.d_write = nowrite,
    184  1.1  nat 	.d_ioctl = noioctl,
    185  1.1  nat 	.d_stop = nostop,
    186  1.1  nat 	.d_tty = notty,
    187  1.1  nat 	.d_poll = nopoll,
    188  1.1  nat 	.d_mmap = nommap,
    189  1.1  nat 	.d_kqfilter = nokqfilter,
    190  1.1  nat 	.d_discard = nodiscard,
    191  1.1  nat 	.d_flag = D_OTHER
    192  1.1  nat };
    193  1.1  nat 
    194  1.1  nat #if NWSMUX > 0
    195  1.1  nat struct wssrcops wsbell_srcops = {
    196  1.1  nat 	WSMUX_BELL,
    197  1.1  nat 	wsbell_mux_open, wsbell_mux_close, wsbelldoioctl, wsbelldoioctl, NULL
    198  1.1  nat };
    199  1.1  nat #endif
    200  1.1  nat 
    201  1.1  nat int
    202  1.1  nat wsbell_match(device_t parent, cfdata_t match, void *aux)
    203  1.1  nat {
    204  1.1  nat 	return (1);
    205  1.1  nat }
    206  1.1  nat 
    207  1.1  nat void
    208  1.1  nat wsbell_attach(device_t parent, device_t self, void *aux)
    209  1.1  nat {
    210  1.1  nat         struct wsbell_softc *sc = device_private(self);
    211  1.1  nat 	struct wsbelldev_attach_args *ap = aux;
    212  1.1  nat #if NWSMUX > 0
    213  1.1  nat 	int mux, error;
    214  1.1  nat #endif
    215  1.1  nat 
    216  1.1  nat 	sc->sc_base.me_dv = self;
    217  1.1  nat 	sc->sc_accesscookie = ap->accesscookie;
    218  1.1  nat 
    219  1.1  nat 	sc->sc_spkr = device_unit(parent);
    220  1.1  nat 	sc->sc_bell_data = wskbd_default_bell_data;
    221  1.1  nat #if NWSMUX > 0
    222  1.1  nat 	sc->sc_base.me_ops = &wsbell_srcops;
    223  1.1  nat 	mux = device_cfdata(self)->wsbelldevcf_mux;
    224  1.1  nat 	if (mux >= 0) {
    225  1.1  nat 		error = wsmux_attach_sc(wsmux_getmux(mux), &sc->sc_base);
    226  1.1  nat 		if (error)
    227  1.1  nat 			aprint_error(" attach error=%d", error);
    228  1.1  nat 		else
    229  1.1  nat 			aprint_normal(" mux %d", mux);
    230  1.1  nat 	}
    231  1.1  nat #else
    232  1.1  nat 	if (device_cfdata(self)->wsbelldevcf_mux >= 0)
    233  1.1  nat 		aprint_normal(" (mux ignored)");
    234  1.1  nat #endif
    235  1.1  nat 
    236  1.1  nat 	aprint_naive("\n");
    237  1.1  nat 	aprint_normal("\n");
    238  1.1  nat 
    239  1.1  nat 	if (!pmf_device_register(self, NULL, NULL))
    240  1.1  nat 		aprint_error_dev(self, "couldn't establish power handler\n");
    241  1.1  nat 
    242  1.1  nat 	mutex_init(&sc->sc_bellock, MUTEX_DEFAULT, IPL_SCHED);
    243  1.1  nat 	cv_init(&sc->sc_bellcv, "bellcv");
    244  1.1  nat 
    245  1.1  nat 	kthread_create(PRI_BIO, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
    246  1.1  nat 	    bell_thread, sc, &sc->sc_bellthread, "%s", device_xname(self));
    247  1.1  nat }
    248  1.1  nat 
    249  1.1  nat int
    250  1.1  nat wsbell_activate(device_t self, enum devact act)
    251  1.1  nat {
    252  1.1  nat 	struct wsbell_softc *sc = device_private(self);
    253  1.1  nat 
    254  1.1  nat 	if (act == DVACT_DEACTIVATE)
    255  1.1  nat 		sc->sc_dying = 1;
    256  1.1  nat 	return (0);
    257  1.1  nat }
    258  1.1  nat 
    259  1.1  nat int
    260  1.1  nat wsbell_detach(device_t self, int flags)
    261  1.1  nat {
    262  1.1  nat 	struct wsbell_softc *sc = device_private(self);
    263  1.1  nat 	struct wseventvar *evar;
    264  1.1  nat 	int maj, mn;
    265  1.1  nat 	int s;
    266  1.1  nat 
    267  1.1  nat #if NWSMUX > 0
    268  1.1  nat 	/* Tell parent mux we're leaving. */
    269  1.1  nat 	if (sc->sc_base.me_parent != NULL) {
    270  1.1  nat 		DPRINTF(("wsbell_detach:\n"));
    271  1.1  nat 		wsmux_detach_sc(&sc->sc_base);
    272  1.1  nat 	}
    273  1.1  nat #endif
    274  1.1  nat 
    275  1.1  nat 	/* If we're open ... */
    276  1.1  nat 	evar = sc->sc_base.me_evp;
    277  1.1  nat 	if (evar != NULL && evar->io != NULL) {
    278  1.1  nat 		s = spltty();
    279  1.1  nat 		if (--sc->sc_refcnt >= 0) {
    280  1.1  nat 			struct wscons_event event;
    281  1.1  nat 
    282  1.1  nat 			/* Wake everyone by generating a dummy event. */
    283  1.1  nat 			event.type = 0;
    284  1.1  nat 			event.value = 0;
    285  1.1  nat 			if (wsevent_inject(evar, &event, 1) != 0)
    286  1.1  nat 				wsevent_wakeup(evar);
    287  1.1  nat 
    288  1.1  nat 			/* Wait for processes to go away. */
    289  1.1  nat 			if (tsleep(sc, PZERO, "wsmdet", hz * 60))
    290  1.1  nat 				printf("wsbell_detach: %s didn't detach\n",
    291  1.1  nat 				       device_xname(self));
    292  1.1  nat 		}
    293  1.1  nat 		splx(s);
    294  1.1  nat 	}
    295  1.1  nat 
    296  1.1  nat 	/* locate the major number */
    297  1.1  nat 	maj = cdevsw_lookup_major(&wsbell_cdevsw);
    298  1.1  nat 
    299  1.1  nat 	/* Nuke the vnodes for any open instances (calls close). */
    300  1.1  nat 	mn = device_unit(self);
    301  1.1  nat 	vdevgone(maj, mn, mn, VCHR);
    302  1.1  nat 
    303  1.1  nat 	mutex_enter(&sc->sc_bellock);
    304  1.1  nat 	sc->sc_bell_args.dying = true;
    305  1.1  nat 
    306  1.1  nat 	cv_broadcast(&sc->sc_bellcv);
    307  1.1  nat 	mutex_exit(&sc->sc_bellock);
    308  1.1  nat 
    309  1.1  nat 	kthread_join(sc->sc_bellthread);
    310  1.1  nat 	cv_destroy(&sc->sc_bellcv);
    311  1.1  nat 	mutex_destroy(&sc->sc_bellock);
    312  1.1  nat 
    313  1.1  nat 	return (0);
    314  1.1  nat }
    315  1.1  nat 
    316  1.1  nat #if NWSMUX > 0
    317  1.1  nat int
    318  1.1  nat wsbelldoopen(struct wsbell_softc *sc, struct wseventvar *evp)
    319  1.1  nat {
    320  1.1  nat 	return (0);
    321  1.1  nat }
    322  1.1  nat 
    323  1.1  nat /* A wrapper around the ioctl() workhorse to make reference counting easy. */
    324  1.1  nat int
    325  1.1  nat wsbelldoioctl(device_t dv, u_long cmd, void *data, int flag,
    326  1.1  nat 	       struct lwp *l)
    327  1.1  nat {
    328  1.1  nat 	struct wsbell_softc *sc = device_private(dv);
    329  1.1  nat 	int error;
    330  1.1  nat 
    331  1.1  nat 	sc->sc_refcnt++;
    332  1.1  nat 	error = wsbell_do_ioctl(sc, cmd, data, flag, l);
    333  1.1  nat 	if (--sc->sc_refcnt < 0)
    334  1.1  nat 		wakeup(sc);
    335  1.1  nat 	return (error);
    336  1.1  nat }
    337  1.1  nat 
    338  1.1  nat int
    339  1.1  nat wsbell_do_ioctl(struct wsbell_softc *sc, u_long cmd, void *data,
    340  1.1  nat 		 int flag, struct lwp *l)
    341  1.1  nat {
    342  1.1  nat 	struct wskbd_bell_data *ubdp, *kbdp;
    343  1.1  nat 	if (sc->sc_dying)
    344  1.1  nat 		return (EIO);
    345  1.1  nat 
    346  1.1  nat 	/*
    347  1.1  nat 	 * Try the wsbell specific ioctls.
    348  1.1  nat 	 */
    349  1.1  nat 	switch (cmd) {
    350  1.1  nat #define	SETBELL(dstp, srcp, dfltp)					\
    351  1.1  nat     do {								\
    352  1.1  nat 	(dstp)->pitch = ((srcp)->which & WSKBD_BELL_DOPITCH) ?		\
    353  1.1  nat 	    (srcp)->pitch : (dfltp)->pitch;				\
    354  1.1  nat 	(dstp)->period = ((srcp)->which & WSKBD_BELL_DOPERIOD) ?	\
    355  1.1  nat 	    (srcp)->period : (dfltp)->period;				\
    356  1.1  nat 	(dstp)->volume = ((srcp)->which & WSKBD_BELL_DOVOLUME) ?	\
    357  1.1  nat 	    (srcp)->volume : (dfltp)->volume;				\
    358  1.1  nat 	(dstp)->which = WSKBD_BELL_DOALL;				\
    359  1.1  nat     } while (0)
    360  1.1  nat 
    361  1.1  nat 	case WSKBDIO_SETBELL:
    362  1.1  nat 		if ((flag & FWRITE) == 0)
    363  1.1  nat 			return (EACCES);
    364  1.1  nat 		kbdp = &sc->sc_bell_data;
    365  1.1  nat 		ubdp = (struct wskbd_bell_data *)data;
    366  1.1  nat 		SETBELL(kbdp, ubdp, kbdp);
    367  1.1  nat 		return (0);
    368  1.1  nat 
    369  1.1  nat 	case WSKBDIO_GETBELL:
    370  1.1  nat 		kbdp = &sc->sc_bell_data;
    371  1.1  nat 		ubdp = (struct wskbd_bell_data *)data;
    372  1.1  nat 		SETBELL(ubdp, kbdp, kbdp);
    373  1.1  nat 		return (0);
    374  1.1  nat 
    375  1.1  nat 	case WSKBDIO_BELL:
    376  1.1  nat 		if ((flag & FWRITE) == 0)
    377  1.1  nat 			return (EACCES);
    378  1.1  nat 		spkr_audio_play(sc, sc->sc_bell_data.pitch,
    379  1.1  nat 		    sc->sc_bell_data.period, sc->sc_bell_data.volume);
    380  1.1  nat 
    381  1.1  nat 		return 0;
    382  1.1  nat 
    383  1.1  nat 	case WSKBDIO_COMPLEXBELL:
    384  1.1  nat 		if ((flag & FWRITE) == 0)
    385  1.1  nat 			return (EACCES);
    386  1.1  nat 		if (data == NULL)
    387  1.1  nat 			return 0;
    388  1.1  nat #define d ((struct wskbd_bell_data *)data)
    389  1.1  nat 		spkr_audio_play(sc, d->pitch, d->period, d->volume);
    390  1.1  nat #undef d
    391  1.1  nat 		return 0;
    392  1.1  nat 	}
    393  1.1  nat 
    394  1.1  nat 	return (EPASSTHROUGH);
    395  1.1  nat }
    396  1.1  nat #endif
    397  1.1  nat 
    398  1.1  nat static void
    399  1.1  nat bell_thread(void *arg)
    400  1.1  nat {
    401  1.1  nat 	struct wsbell_softc *sc = arg;
    402  1.1  nat 	struct vbell_args *vb = &sc->sc_bell_args;
    403  1.1  nat 	tone_t tone;
    404  1.1  nat 	u_int vol;
    405  1.1  nat 
    406  1.1  nat 	for (;;) {
    407  1.1  nat 		mutex_enter(&sc->sc_bellock);
    408  1.1  nat 		cv_wait_sig(&sc->sc_bellcv, &sc->sc_bellock);
    409  1.1  nat 
    410  1.1  nat 		if (vb->dying == true) {
    411  1.1  nat 			mutex_exit(&sc->sc_bellock);
    412  1.1  nat 			kthread_exit(0);
    413  1.1  nat 		}
    414  1.1  nat 
    415  1.1  nat 		tone.frequency = vb->pitch;
    416  1.1  nat 		tone.duration = vb->period;
    417  1.1  nat 		vol = vb->volume;
    418  1.1  nat 		mutex_exit(&sc->sc_bellock);
    419  1.1  nat 
    420  1.1  nat 		if (spkropen(sc->sc_spkr, FWRITE, 0, NULL) != 0)
    421  1.1  nat 			continue;
    422  1.1  nat 		spkrioctl(sc->sc_spkr, SPKRSETVOL, &vol, 0, curlwp);
    423  1.1  nat 		spkrioctl(sc->sc_spkr, SPKRTONE, &tone, 0, curlwp);
    424  1.1  nat 		spkrclose(sc->sc_spkr, FWRITE, 0, curlwp);
    425  1.1  nat 	}
    426  1.1  nat }
    427  1.1  nat 
    428  1.1  nat static inline void
    429  1.1  nat spkr_audio_play(struct wsbell_softc *sc, u_int pitch, u_int period, u_int volume)
    430  1.1  nat {
    431  1.1  nat 
    432  1.1  nat 	mutex_enter(&sc->sc_bellock);
    433  1.1  nat 	sc->sc_bell_args.dying = false;
    434  1.1  nat 	sc->sc_bell_args.pitch = pitch;
    435  1.1  nat 	sc->sc_bell_args.period = period / 5;
    436  1.1  nat 	sc->sc_bell_args.volume = volume;
    437  1.1  nat 
    438  1.1  nat 	cv_broadcast(&sc->sc_bellcv);
    439  1.1  nat 	mutex_exit(&sc->sc_bellock);
    440  1.1  nat }
    441  1.1  nat 
    442  1.1  nat #if NWSMUX > 0
    443  1.1  nat int
    444  1.1  nat wsbell_mux_open(struct wsevsrc *me, struct wseventvar *evp)
    445  1.1  nat {
    446  1.1  nat 	struct wsbell_softc *sc = (struct wsbell_softc *)me;
    447  1.1  nat 
    448  1.1  nat 	if (sc->sc_base.me_evp != NULL)
    449  1.1  nat 		return (EBUSY);
    450  1.1  nat 
    451  1.1  nat 	return wsbelldoopen(sc, evp);
    452  1.1  nat }
    453  1.1  nat 
    454  1.1  nat int
    455  1.1  nat wsbell_mux_close(struct wsevsrc *me)
    456  1.1  nat {
    457  1.1  nat 	struct wsbell_softc *sc = (struct wsbell_softc *)me;
    458  1.1  nat 
    459  1.1  nat 	sc->sc_base.me_evp = NULL;
    460  1.1  nat 
    461  1.1  nat 	return (0);
    462  1.1  nat }
    463  1.1  nat #endif /* NWSMUX > 0 */
    464