xref: /netbsd-src/sys/dev/spkr_audio.c (revision 2a698ff5a341b6ec6aca83e780ad320a752687c1)
1 /*	$NetBSD: spkr_audio.c,v 1.11 2021/04/03 04:10:30 isaki 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.11 2021/04/03 04:10:30 isaki 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/malloc.h>
39 #include <sys/uio.h>
40 #include <sys/proc.h>
41 #include <sys/ioctl.h>
42 #include <sys/conf.h>
43 #include <sys/sysctl.h>
44 
45 #include <dev/audio/audiovar.h>
46 #include <dev/audio/audiobellvar.h>
47 
48 #include <dev/spkrvar.h>
49 #include <dev/spkrio.h>
50 
51 static int spkr_audio_probe(device_t, cfdata_t, void *);
52 static void spkr_audio_attach(device_t, device_t, void *);
53 static int spkr_audio_detach(device_t, int);
54 static int spkr_audio_rescan(device_t, const char *, const int *);
55 static void spkr_audio_childdet(device_t, device_t);
56 
57 struct spkr_audio_softc {
58 	struct spkr_softc sc_spkr;
59 	device_t		sc_audiodev;
60 };
61 
62 CFATTACH_DECL3_NEW(spkr_audio, sizeof(struct spkr_audio_softc),
63     spkr_audio_probe, spkr_audio_attach, spkr_audio_detach, NULL,
64     spkr_audio_rescan, spkr_audio_childdet, 0);
65 
66 static void
67 spkr_audio_tone(device_t self, u_int xhz, u_int ticks)
68 {
69 	struct spkr_audio_softc *sc = device_private(self);
70 
71 #ifdef SPKRDEBUG
72 	device_printf(self, "%s: %u %u\n", __func__, xhz, ticks);
73 #endif /* SPKRDEBUG */
74 
75 	if (xhz == 0 || ticks == 0)
76 		return;
77 
78 	audiobell(sc->sc_audiodev, xhz, hztoms(ticks), sc->sc_spkr.sc_vol, 0);
79 }
80 
81 static int
82 spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
83 {
84 	struct audio_softc *asc = device_private(parent);
85 
86 	if ((asc->sc_props & AUDIO_PROP_PLAYBACK))
87 		return 1;
88 
89 	return 0;
90 }
91 
92 static void
93 spkr_audio_attach(device_t parent, device_t self, void *aux)
94 {
95 	struct spkr_audio_softc *sc = device_private(self);
96 
97 	aprint_naive("\n");
98 	aprint_normal(": PC Speaker (synthesized)\n");
99 
100 	sc->sc_audiodev = parent;
101 	sc->sc_spkr.sc_vol = 80;
102 
103 	if (!pmf_device_register(self, NULL, NULL))
104 		aprint_error_dev(self, "couldn't establish power handler\n");
105 
106 	spkr_attach(self, spkr_audio_tone);
107 }
108 
109 static int
110 spkr_audio_detach(device_t self, int flags)
111 {
112 	int error;
113 
114 	if ((error = spkr_detach(self, flags)) != 0)
115 		return error;
116 
117 	pmf_device_deregister(self);
118 
119 	return 0;
120 }
121 
122 static int
123 spkr_audio_rescan(device_t self, const char *iattr, const int *locators)
124 {
125 
126 	return spkr_rescan(self, iattr, locators);
127 }
128 
129 static void
130 spkr_audio_childdet(device_t self, device_t child)
131 {
132 
133 	spkr_childdet(self, child);
134 }
135