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