1/*	$NetBSD: spkr_audio.c,v 1.12 2022/09/24 23:16:02 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2016 Nathanial Sloss <nathanialsloss@yahoo.com.au>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: spkr_audio.c,v 1.12 2022/09/24 23:16:02 thorpej Exp $");
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/audioio.h>
35#include <sys/kernel.h>
36#include <sys/errno.h>
37#include <sys/device.h>
38#include <sys/uio.h>
39#include <sys/proc.h>
40#include <sys/ioctl.h>
41#include <sys/conf.h>
42#include <sys/sysctl.h>
43
44#include <dev/audio/audiovar.h>
45#include <dev/audio/audiobellvar.h>
46
47#include <dev/spkrvar.h>
48#include <dev/spkrio.h>
49
50static int spkr_audio_probe(device_t, cfdata_t, void *);
51static void spkr_audio_attach(device_t, device_t, void *);
52static int spkr_audio_detach(device_t, int);
53static int spkr_audio_rescan(device_t, const char *, const int *);
54static void spkr_audio_childdet(device_t, device_t);
55
56struct spkr_audio_softc {
57	struct spkr_softc sc_spkr;
58	device_t		sc_audiodev;
59};
60
61CFATTACH_DECL3_NEW(spkr_audio, sizeof(struct spkr_audio_softc),
62    spkr_audio_probe, spkr_audio_attach, spkr_audio_detach, NULL,
63    spkr_audio_rescan, spkr_audio_childdet, 0);
64
65static void
66spkr_audio_tone(device_t self, u_int xhz, u_int ticks)
67{
68	struct spkr_audio_softc *sc = device_private(self);
69
70#ifdef SPKRDEBUG
71	device_printf(self, "%s: %u %u\n", __func__, xhz, ticks);
72#endif /* SPKRDEBUG */
73
74	if (xhz == 0 || ticks == 0)
75		return;
76
77	audiobell(sc->sc_audiodev, xhz, hztoms(ticks), sc->sc_spkr.sc_vol, 0);
78}
79
80static int
81spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
82{
83	struct audio_softc *asc = device_private(parent);
84
85	if ((asc->sc_props & AUDIO_PROP_PLAYBACK))
86		return 1;
87
88	return 0;
89}
90
91static void
92spkr_audio_attach(device_t parent, device_t self, void *aux)
93{
94	struct spkr_audio_softc *sc = device_private(self);
95
96	aprint_naive("\n");
97	aprint_normal(": PC Speaker (synthesized)\n");
98
99	sc->sc_audiodev = parent;
100	sc->sc_spkr.sc_vol = 80;
101
102	if (!pmf_device_register(self, NULL, NULL))
103		aprint_error_dev(self, "couldn't establish power handler\n");
104
105	spkr_attach(self, spkr_audio_tone);
106}
107
108static int
109spkr_audio_detach(device_t self, int flags)
110{
111	int error;
112
113	if ((error = spkr_detach(self, flags)) != 0)
114		return error;
115
116	pmf_device_deregister(self);
117
118	return 0;
119}
120
121static int
122spkr_audio_rescan(device_t self, const char *iattr, const int *locators)
123{
124
125	return spkr_rescan(self, iattr, locators);
126}
127
128static void
129spkr_audio_childdet(device_t self, device_t child)
130{
131
132	spkr_childdet(self, child);
133}
134