xref: /netbsd-src/sys/dev/spkr_audio.c (revision cef8759bd76c1b621f8eab8faa6f208faabc2e15)
1 /*	$NetBSD: spkr_audio.c,v 1.8 2019/06/21 09:34: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.8 2019/06/21 09:34:30 isaki Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/errno.h>
36 #include <sys/device.h>
37 #include <sys/malloc.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/audio_if.h>
45 #include <dev/audio/audiobellvar.h>
46 
47 #include <dev/spkrvar.h>
48 #include <dev/spkrio.h>
49 
50 static int spkr_audio_probe(device_t, cfdata_t, void *);
51 static void spkr_audio_attach(device_t, device_t, void *);
52 static int spkr_audio_detach(device_t, int);
53 static int spkr_audio_rescan(device_t, const char *, const int *);
54 static void spkr_audio_childdet(device_t, device_t);
55 
56 struct spkr_audio_softc {
57 	struct spkr_softc sc_spkr;
58 	device_t		sc_audiodev;
59 };
60 
61 CFATTACH_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 
65 static void
66 spkr_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 	aprint_debug_dev(self, "%s: %u %d\n", __func__, xhz, ticks);
72 #endif /* SPKRDEBUG */
73 	audiobell(sc->sc_audiodev, xhz, hztoms(ticks),
74 	    sc->sc_spkr.sc_vol, 0);
75 }
76 
77 static void
78 spkr_audio_rest(device_t self, int ticks)
79 {
80 	struct spkr_audio_softc *sc = device_private(self);
81 
82 #ifdef SPKRDEBUG
83 	aprint_debug_dev(self, "%s: %d\n", __func__, ticks);
84 #endif /* SPKRDEBUG */
85 	if (ticks > 0)
86 		audiobell(sc->sc_audiodev, 0, hztoms(ticks),
87 		    sc->sc_spkr.sc_vol, 0);
88 }
89 
90 static int
91 spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
92 {
93 
94 	return 1;
95 }
96 
97 static void
98 spkr_audio_attach(device_t parent, device_t self, void *aux)
99 {
100 	struct spkr_audio_softc *sc = device_private(self);
101 
102 	aprint_naive("\n");
103 	aprint_normal(": PC Speaker (synthesized)\n");
104 
105 	sc->sc_audiodev = parent;
106 	sc->sc_spkr.sc_vol = 80;
107 
108 	if (!pmf_device_register(self, NULL, NULL))
109 		aprint_error_dev(self, "couldn't establish power handler\n");
110 
111 	spkr_attach(self, spkr_audio_tone, spkr_audio_rest);
112 }
113 
114 static int
115 spkr_audio_detach(device_t self, int flags)
116 {
117 	int error;
118 
119 	if ((error = spkr_detach(self, flags)) != 0)
120 		return error;
121 
122 	pmf_device_deregister(self);
123 
124 	return 0;
125 }
126 
127 static int
128 spkr_audio_rescan(device_t self, const char *iattr, const int *locators)
129 {
130 
131 	return spkr_rescan(self, iattr, locators);
132 }
133 
134 static void
135 spkr_audio_childdet(device_t self, device_t child)
136 {
137 
138 	spkr_childdet(self, child);
139 }
140