Home | History | Annotate | Line # | Download | only in dev
spkr_audio.c revision 1.10
      1  1.10     isaki /*	$NetBSD: spkr_audio.c,v 1.10 2021/04/03 03:21:53 isaki 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.10     isaki __KERNEL_RCSID(0, "$NetBSD: spkr_audio.c,v 1.10 2021/04/03 03:21:53 isaki Exp $");
     31   1.1  christos 
     32   1.1  christos #include <sys/param.h>
     33   1.1  christos #include <sys/systm.h>
     34   1.9     isaki #include <sys/audioio.h>
     35   1.1  christos #include <sys/kernel.h>
     36   1.1  christos #include <sys/errno.h>
     37   1.1  christos #include <sys/device.h>
     38   1.1  christos #include <sys/malloc.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/sysctl.h>
     44   1.1  christos 
     45   1.9     isaki #include <dev/audio/audiovar.h>
     46   1.7     isaki #include <dev/audio/audiobellvar.h>
     47   1.1  christos 
     48   1.1  christos #include <dev/spkrvar.h>
     49   1.1  christos #include <dev/spkrio.h>
     50   1.1  christos 
     51   1.1  christos static int spkr_audio_probe(device_t, cfdata_t, void *);
     52   1.1  christos static void spkr_audio_attach(device_t, device_t, void *);
     53   1.1  christos static int spkr_audio_detach(device_t, int);
     54   1.6  pgoyette static int spkr_audio_rescan(device_t, const char *, const int *);
     55   1.6  pgoyette static void spkr_audio_childdet(device_t, device_t);
     56   1.1  christos 
     57   1.1  christos struct spkr_audio_softc {
     58   1.1  christos 	struct spkr_softc sc_spkr;
     59   1.1  christos 	device_t		sc_audiodev;
     60   1.1  christos };
     61   1.1  christos 
     62   1.6  pgoyette CFATTACH_DECL3_NEW(spkr_audio, sizeof(struct spkr_audio_softc),
     63   1.6  pgoyette     spkr_audio_probe, spkr_audio_attach, spkr_audio_detach, NULL,
     64   1.6  pgoyette     spkr_audio_rescan, spkr_audio_childdet, 0);
     65   1.1  christos 
     66   1.1  christos static void
     67   1.1  christos spkr_audio_tone(device_t self, u_int xhz, u_int ticks)
     68   1.1  christos {
     69   1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
     70   1.1  christos 
     71   1.1  christos #ifdef SPKRDEBUG
     72  1.10     isaki 	device_printf(self, "%s: %u %u\n", __func__, xhz, ticks);
     73   1.1  christos #endif /* SPKRDEBUG */
     74   1.8     isaki 	audiobell(sc->sc_audiodev, xhz, hztoms(ticks),
     75   1.5       nat 	    sc->sc_spkr.sc_vol, 0);
     76   1.1  christos }
     77   1.1  christos 
     78   1.1  christos static void
     79   1.1  christos spkr_audio_rest(device_t self, int ticks)
     80   1.1  christos {
     81   1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
     82   1.1  christos 
     83   1.1  christos #ifdef SPKRDEBUG
     84   1.1  christos 	aprint_debug_dev(self, "%s: %d\n", __func__, ticks);
     85   1.1  christos #endif /* SPKRDEBUG */
     86   1.1  christos 	if (ticks > 0)
     87   1.8     isaki 		audiobell(sc->sc_audiodev, 0, hztoms(ticks),
     88   1.5       nat 		    sc->sc_spkr.sc_vol, 0);
     89   1.1  christos }
     90   1.1  christos 
     91   1.1  christos static int
     92   1.1  christos spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
     93   1.1  christos {
     94   1.9     isaki 	struct audio_softc *asc = device_private(parent);
     95   1.1  christos 
     96   1.9     isaki 	if ((asc->sc_props & AUDIO_PROP_PLAYBACK))
     97   1.9     isaki 		return 1;
     98   1.9     isaki 
     99   1.9     isaki 	return 0;
    100   1.1  christos }
    101   1.1  christos 
    102   1.1  christos static void
    103   1.1  christos spkr_audio_attach(device_t parent, device_t self, void *aux)
    104   1.1  christos {
    105   1.1  christos 	struct spkr_audio_softc *sc = device_private(self);
    106   1.1  christos 
    107   1.1  christos 	aprint_naive("\n");
    108   1.1  christos 	aprint_normal(": PC Speaker (synthesized)\n");
    109   1.1  christos 
    110   1.1  christos 	sc->sc_audiodev = parent;
    111   1.5       nat 	sc->sc_spkr.sc_vol = 80;
    112   1.1  christos 
    113   1.1  christos 	if (!pmf_device_register(self, NULL, NULL))
    114   1.1  christos 		aprint_error_dev(self, "couldn't establish power handler\n");
    115   1.1  christos 
    116   1.1  christos 	spkr_attach(self, spkr_audio_tone, spkr_audio_rest);
    117   1.1  christos }
    118   1.1  christos 
    119   1.1  christos static int
    120   1.1  christos spkr_audio_detach(device_t self, int flags)
    121   1.1  christos {
    122   1.3  pgoyette 	int error;
    123   1.3  pgoyette 
    124   1.3  pgoyette 	if ((error = spkr_detach(self, flags)) != 0)
    125   1.3  pgoyette 		return error;
    126   1.1  christos 
    127   1.1  christos 	pmf_device_deregister(self);
    128   1.1  christos 
    129   1.1  christos 	return 0;
    130   1.1  christos }
    131   1.6  pgoyette 
    132   1.6  pgoyette static int
    133   1.6  pgoyette spkr_audio_rescan(device_t self, const char *iattr, const int *locators)
    134   1.6  pgoyette {
    135   1.6  pgoyette 
    136   1.6  pgoyette 	return spkr_rescan(self, iattr, locators);
    137   1.6  pgoyette }
    138   1.6  pgoyette 
    139   1.6  pgoyette static void
    140   1.6  pgoyette spkr_audio_childdet(device_t self, device_t child)
    141   1.6  pgoyette {
    142   1.6  pgoyette 
    143   1.6  pgoyette 	spkr_childdet(self, child);
    144   1.6  pgoyette }
    145