Home | History | Annotate | Line # | Download | only in dev
spkr_audio.c revision 1.1
      1  1.1  christos /*	$NetBSD: spkr_audio.c,v 1.1 2016/12/15 04:36:07 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2016 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * Redistribution and use in source and binary forms, with or without
      8  1.1  christos  * modification, are permitted provided that the following conditions
      9  1.1  christos  * are met:
     10  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     11  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     12  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  christos  *    documentation and/or other materials provided with the distribution.
     15  1.1  christos  *
     16  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.1  christos #include <sys/cdefs.h>
     30  1.1  christos __KERNEL_RCSID(0, "$NetBSD: spkr_audio.c,v 1.1 2016/12/15 04:36:07 christos Exp $");
     31  1.1  christos 
     32  1.1  christos #include <sys/param.h>
     33  1.1  christos #include <sys/systm.h>
     34  1.1  christos #include <sys/kernel.h>
     35  1.1  christos #include <sys/errno.h>
     36  1.1  christos #include <sys/device.h>
     37  1.1  christos #include <sys/malloc.h>
     38  1.1  christos #include <sys/module.h>
     39  1.1  christos #include <sys/uio.h>
     40  1.1  christos #include <sys/proc.h>
     41  1.1  christos #include <sys/ioctl.h>
     42  1.1  christos #include <sys/conf.h>
     43  1.1  christos #include <sys/condvar.h>
     44  1.1  christos #include <sys/mutex.h>
     45  1.1  christos #include <sys/kthread.h>
     46  1.1  christos #include <sys/sysctl.h>
     47  1.1  christos #include <dev/audio_if.h>
     48  1.1  christos #include <dev/audiovar.h>
     49  1.1  christos 
     50  1.1  christos struct vbell_args {
     51  1.1  christos 	u_int pitch;
     52  1.1  christos 	u_int period;
     53  1.1  christos 	u_int volume;
     54  1.1  christos 	bool dying;
     55  1.1  christos };
     56  1.1  christos 
     57  1.1  christos static void bell_thread(void *) __dead;
     58  1.1  christos 
     59  1.1  christos #include <dev/audiobellvar.h>
     60  1.1  christos 
     61  1.1  christos #include <dev/spkrvar.h>
     62  1.1  christos #include <dev/spkrio.h>
     63  1.1  christos 
     64  1.1  christos static int spkr_audio_probe(device_t, cfdata_t, void *);
     65  1.1  christos static void spkr_audio_attach(device_t, device_t, void *);
     66  1.1  christos static int spkr_audio_detach(device_t, int);
     67  1.1  christos 
     68  1.1  christos struct spkr_audio_softc {
     69  1.1  christos 	struct spkr_softc sc_spkr;
     70  1.1  christos 	lwp_t		*sc_bellthread;
     71  1.1  christos 	kmutex_t	sc_bellock;
     72  1.1  christos 	kcondvar_t	sc_bellcv;
     73  1.1  christos 	device_t		sc_audiodev;
     74  1.1  christos 	struct vbell_args sc_bell_args;
     75  1.1  christos };
     76  1.1  christos 
     77  1.1  christos CFATTACH_DECL_NEW(spkr_audio, sizeof(struct spkr_audio_softc),
     78  1.1  christos     spkr_audio_probe, spkr_audio_attach, spkr_audio_detach, NULL);
     79  1.1  christos 
     80  1.1  christos MODULE(MODULE_CLASS_DRIVER, spkr, NULL /* "audio" */);
     81  1.1  christos 
     82  1.1  christos static int
     83  1.1  christos spkr_modcmd(modcmd_t cmd, void *arg)
     84  1.1  christos {
     85  1.1  christos 	return spkr__modcmd(cmd, arg);
     86  1.1  christos }
     87  1.1  christos 
     88  1.1  christos static void
     89  1.1  christos spkr_audio_tone(device_t self, u_int xhz, u_int ticks)
     90  1.1  christos {
     91  1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
     92  1.1  christos 
     93  1.1  christos #ifdef SPKRDEBUG
     94  1.1  christos 	aprint_debug_dev(self, "%s: %u %d\n", __func__, xhz, ticks);
     95  1.1  christos #endif /* SPKRDEBUG */
     96  1.1  christos 	audiobell(sc->sc_audiodev, xhz, ticks * (1000 / hz), 80, 0);
     97  1.1  christos }
     98  1.1  christos 
     99  1.1  christos static void
    100  1.1  christos spkr_audio_rest(device_t self, int ticks)
    101  1.1  christos {
    102  1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
    103  1.1  christos 
    104  1.1  christos #ifdef SPKRDEBUG
    105  1.1  christos 	aprint_debug_dev(self, "%s: %d\n", __func__, ticks);
    106  1.1  christos #endif /* SPKRDEBUG */
    107  1.1  christos 	if (ticks > 0)
    108  1.1  christos 		audiobell(sc->sc_audiodev, 0, ticks * (1000 / hz), 80, 0);
    109  1.1  christos }
    110  1.1  christos 
    111  1.1  christos #ifdef notyet
    112  1.1  christos static void
    113  1.1  christos spkr_audio_play(device_t self, u_int pitch, u_int period, u_int volume)
    114  1.1  christos {
    115  1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
    116  1.1  christos 
    117  1.1  christos 	mutex_enter(&sc->sc_bellock);
    118  1.1  christos 	sc->sc_bell_args.dying = false;
    119  1.1  christos 	sc->sc_bell_args.pitch = pitch;
    120  1.1  christos 	sc->sc_bell_args.period = period;
    121  1.1  christos 	sc->sc_bell_args.volume = volume;
    122  1.1  christos 
    123  1.1  christos 	cv_broadcast(&sc->sc_bellcv);
    124  1.1  christos 	mutex_exit(&sc->sc_bellock);
    125  1.1  christos }
    126  1.1  christos #endif
    127  1.1  christos 
    128  1.1  christos static int
    129  1.1  christos spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
    130  1.1  christos {
    131  1.1  christos 
    132  1.1  christos 	return 1;
    133  1.1  christos }
    134  1.1  christos 
    135  1.1  christos static void
    136  1.1  christos spkr_audio_attach(device_t parent, device_t self, void *aux)
    137  1.1  christos {
    138  1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
    139  1.1  christos 
    140  1.1  christos 	aprint_naive("\n");
    141  1.1  christos 	aprint_normal(": PC Speaker (synthesized)\n");
    142  1.1  christos 
    143  1.1  christos 	sc->sc_audiodev = parent;
    144  1.1  christos 
    145  1.1  christos 	if (!pmf_device_register(self, NULL, NULL))
    146  1.1  christos 		aprint_error_dev(self, "couldn't establish power handler\n");
    147  1.1  christos 	mutex_init(&sc->sc_bellock, MUTEX_DEFAULT, IPL_SCHED);
    148  1.1  christos 	cv_init(&sc->sc_bellcv, "bellcv");
    149  1.1  christos 
    150  1.1  christos 	kthread_create(PRI_BIO, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
    151  1.1  christos 	    bell_thread, sc, &sc->sc_bellthread, "%s", device_xname(self));
    152  1.1  christos 
    153  1.1  christos 	spkr_attach(self, spkr_audio_tone, spkr_audio_rest);
    154  1.1  christos }
    155  1.1  christos 
    156  1.1  christos static int
    157  1.1  christos spkr_audio_detach(device_t self, int flags)
    158  1.1  christos {
    159  1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
    160  1.1  christos 
    161  1.1  christos 	pmf_device_deregister(self);
    162  1.1  christos 
    163  1.1  christos 	mutex_enter(&sc->sc_bellock);
    164  1.1  christos 	sc->sc_bell_args.dying = true;
    165  1.1  christos 
    166  1.1  christos 	cv_broadcast(&sc->sc_bellcv);
    167  1.1  christos 	mutex_exit(&sc->sc_bellock);
    168  1.1  christos 
    169  1.1  christos 	kthread_join(sc->sc_bellthread);
    170  1.1  christos 	cv_destroy(&sc->sc_bellcv);
    171  1.1  christos 	mutex_destroy(&sc->sc_bellock);
    172  1.1  christos 
    173  1.1  christos 
    174  1.1  christos 	return 0;
    175  1.1  christos }
    176  1.1  christos 
    177  1.1  christos static void
    178  1.1  christos bell_thread(void *arg)
    179  1.1  christos {
    180  1.1  christos 	struct spkr_audio_softc *sc = arg;
    181  1.1  christos 	struct vbell_args *vb = &sc->sc_bell_args;
    182  1.1  christos 	u_int bpitch;
    183  1.1  christos 	u_int bperiod;
    184  1.1  christos 	u_int bvolume;
    185  1.1  christos 
    186  1.1  christos 	for (;;) {
    187  1.1  christos 		mutex_enter(&sc->sc_bellock);
    188  1.1  christos 		cv_wait_sig(&sc->sc_bellcv, &sc->sc_bellock);
    189  1.1  christos 
    190  1.1  christos 		if (vb->dying == true) {
    191  1.1  christos 			mutex_exit(&sc->sc_bellock);
    192  1.1  christos 			kthread_exit(0);
    193  1.1  christos 		}
    194  1.1  christos 
    195  1.1  christos 		bpitch = vb->pitch;
    196  1.1  christos 		bperiod = vb->period;
    197  1.1  christos 		bvolume = vb->volume;
    198  1.1  christos 		mutex_exit(&sc->sc_bellock);
    199  1.1  christos 		audiobell(sc->sc_audiodev, bpitch, bperiod, bvolume, 0);
    200  1.1  christos 	}
    201  1.1  christos }
    202