xref: /netbsd-src/sys/dev/spkr_audio.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: spkr_audio.c,v 1.6 2017/06/11 21:54:22 pgoyette 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.6 2017/06/11 21:54:22 pgoyette 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 #include <dev/audio_if.h>
44 #include <dev/audiovar.h>
45 
46 #include <dev/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 	aprint_debug_dev(self, "%s: %u %d\n", __func__, xhz, ticks);
73 #endif /* SPKRDEBUG */
74 	audiobell(sc->sc_audiodev, xhz, ticks * (1000 / hz),
75 	    sc->sc_spkr.sc_vol, 0);
76 }
77 
78 static void
79 spkr_audio_rest(device_t self, int ticks)
80 {
81 	struct spkr_audio_softc *sc = device_private(self);
82 
83 #ifdef SPKRDEBUG
84 	aprint_debug_dev(self, "%s: %d\n", __func__, ticks);
85 #endif /* SPKRDEBUG */
86 	if (ticks > 0)
87 		audiobell(sc->sc_audiodev, 0, ticks * (1000 / hz),
88 		    sc->sc_spkr.sc_vol, 0);
89 }
90 
91 static int
92 spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
93 {
94 
95 	return 1;
96 }
97 
98 static void
99 spkr_audio_attach(device_t parent, device_t self, void *aux)
100 {
101 	struct spkr_audio_softc *sc = device_private(self);
102 
103 	aprint_naive("\n");
104 	aprint_normal(": PC Speaker (synthesized)\n");
105 
106 	sc->sc_audiodev = parent;
107 	sc->sc_spkr.sc_vol = 80;
108 
109 	if (!pmf_device_register(self, NULL, NULL))
110 		aprint_error_dev(self, "couldn't establish power handler\n");
111 
112 	spkr_attach(self, spkr_audio_tone, spkr_audio_rest);
113 }
114 
115 static int
116 spkr_audio_detach(device_t self, int flags)
117 {
118 	int error;
119 
120 	if ((error = spkr_detach(self, flags)) != 0)
121 		return error;
122 
123 	pmf_device_deregister(self);
124 
125 	return 0;
126 }
127 
128 static int
129 spkr_audio_rescan(device_t self, const char *iattr, const int *locators)
130 {
131 
132 	return spkr_rescan(self, iattr, locators);
133 }
134 
135 static void
136 spkr_audio_childdet(device_t self, device_t child)
137 {
138 
139 	spkr_childdet(self, child);
140 }
141