spkr_audio.c revision 1.3.6.2 1 1.3.6.2 skrll /* $NetBSD: spkr_audio.c,v 1.3.6.2 2017/02/05 13:40:26 skrll Exp $ */
2 1.3.6.2 skrll
3 1.3.6.2 skrll /*-
4 1.3.6.2 skrll * Copyright (c) 2016 Nathanial Sloss <nathanialsloss (at) yahoo.com.au>
5 1.3.6.2 skrll * All rights reserved.
6 1.3.6.2 skrll *
7 1.3.6.2 skrll * Redistribution and use in source and binary forms, with or without
8 1.3.6.2 skrll * modification, are permitted provided that the following conditions
9 1.3.6.2 skrll * are met:
10 1.3.6.2 skrll * 1. Redistributions of source code must retain the above copyright
11 1.3.6.2 skrll * notice, this list of conditions and the following disclaimer.
12 1.3.6.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
13 1.3.6.2 skrll * notice, this list of conditions and the following disclaimer in the
14 1.3.6.2 skrll * documentation and/or other materials provided with the distribution.
15 1.3.6.2 skrll *
16 1.3.6.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.3.6.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.3.6.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.3.6.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.3.6.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.3.6.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.3.6.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.3.6.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.3.6.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.3.6.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.3.6.2 skrll * POSSIBILITY OF SUCH DAMAGE.
27 1.3.6.2 skrll */
28 1.3.6.2 skrll
29 1.3.6.2 skrll #include <sys/cdefs.h>
30 1.3.6.2 skrll __KERNEL_RCSID(0, "$NetBSD: spkr_audio.c,v 1.3.6.2 2017/02/05 13:40:26 skrll Exp $");
31 1.3.6.2 skrll
32 1.3.6.2 skrll #include <sys/param.h>
33 1.3.6.2 skrll #include <sys/systm.h>
34 1.3.6.2 skrll #include <sys/kernel.h>
35 1.3.6.2 skrll #include <sys/errno.h>
36 1.3.6.2 skrll #include <sys/device.h>
37 1.3.6.2 skrll #include <sys/malloc.h>
38 1.3.6.2 skrll #include <sys/uio.h>
39 1.3.6.2 skrll #include <sys/proc.h>
40 1.3.6.2 skrll #include <sys/ioctl.h>
41 1.3.6.2 skrll #include <sys/conf.h>
42 1.3.6.2 skrll #include <sys/condvar.h>
43 1.3.6.2 skrll #include <sys/mutex.h>
44 1.3.6.2 skrll #include <sys/kthread.h>
45 1.3.6.2 skrll #include <sys/sysctl.h>
46 1.3.6.2 skrll #include <dev/audio_if.h>
47 1.3.6.2 skrll #include <dev/audiovar.h>
48 1.3.6.2 skrll
49 1.3.6.2 skrll struct vbell_args {
50 1.3.6.2 skrll u_int pitch;
51 1.3.6.2 skrll u_int period;
52 1.3.6.2 skrll u_int volume;
53 1.3.6.2 skrll bool dying;
54 1.3.6.2 skrll };
55 1.3.6.2 skrll
56 1.3.6.2 skrll static void bell_thread(void *) __dead;
57 1.3.6.2 skrll
58 1.3.6.2 skrll #include <dev/audiobellvar.h>
59 1.3.6.2 skrll
60 1.3.6.2 skrll #include <dev/spkrvar.h>
61 1.3.6.2 skrll #include <dev/spkrio.h>
62 1.3.6.2 skrll
63 1.3.6.2 skrll static int spkr_audio_probe(device_t, cfdata_t, void *);
64 1.3.6.2 skrll static void spkr_audio_attach(device_t, device_t, void *);
65 1.3.6.2 skrll static int spkr_audio_detach(device_t, int);
66 1.3.6.2 skrll
67 1.3.6.2 skrll struct spkr_audio_softc {
68 1.3.6.2 skrll struct spkr_softc sc_spkr;
69 1.3.6.2 skrll lwp_t *sc_bellthread;
70 1.3.6.2 skrll kmutex_t sc_bellock;
71 1.3.6.2 skrll kcondvar_t sc_bellcv;
72 1.3.6.2 skrll device_t sc_audiodev;
73 1.3.6.2 skrll struct vbell_args sc_bell_args;
74 1.3.6.2 skrll };
75 1.3.6.2 skrll
76 1.3.6.2 skrll CFATTACH_DECL_NEW(spkr_audio, sizeof(struct spkr_audio_softc),
77 1.3.6.2 skrll spkr_audio_probe, spkr_audio_attach, spkr_audio_detach, NULL);
78 1.3.6.2 skrll
79 1.3.6.2 skrll static void
80 1.3.6.2 skrll spkr_audio_tone(device_t self, u_int xhz, u_int ticks)
81 1.3.6.2 skrll {
82 1.3.6.2 skrll struct spkr_audio_softc *sc = device_private(self);
83 1.3.6.2 skrll
84 1.3.6.2 skrll #ifdef SPKRDEBUG
85 1.3.6.2 skrll aprint_debug_dev(self, "%s: %u %d\n", __func__, xhz, ticks);
86 1.3.6.2 skrll #endif /* SPKRDEBUG */
87 1.3.6.2 skrll audiobell(sc->sc_audiodev, xhz, ticks * (1000 / hz), 80, 0);
88 1.3.6.2 skrll }
89 1.3.6.2 skrll
90 1.3.6.2 skrll static void
91 1.3.6.2 skrll spkr_audio_rest(device_t self, int ticks)
92 1.3.6.2 skrll {
93 1.3.6.2 skrll struct spkr_audio_softc *sc = device_private(self);
94 1.3.6.2 skrll
95 1.3.6.2 skrll #ifdef SPKRDEBUG
96 1.3.6.2 skrll aprint_debug_dev(self, "%s: %d\n", __func__, ticks);
97 1.3.6.2 skrll #endif /* SPKRDEBUG */
98 1.3.6.2 skrll if (ticks > 0)
99 1.3.6.2 skrll audiobell(sc->sc_audiodev, 0, ticks * (1000 / hz), 80, 0);
100 1.3.6.2 skrll }
101 1.3.6.2 skrll
102 1.3.6.2 skrll #ifdef notyet
103 1.3.6.2 skrll static void
104 1.3.6.2 skrll spkr_audio_play(device_t self, u_int pitch, u_int period, u_int volume)
105 1.3.6.2 skrll {
106 1.3.6.2 skrll struct spkr_audio_softc *sc = device_private(self);
107 1.3.6.2 skrll
108 1.3.6.2 skrll mutex_enter(&sc->sc_bellock);
109 1.3.6.2 skrll sc->sc_bell_args.dying = false;
110 1.3.6.2 skrll sc->sc_bell_args.pitch = pitch;
111 1.3.6.2 skrll sc->sc_bell_args.period = period;
112 1.3.6.2 skrll sc->sc_bell_args.volume = volume;
113 1.3.6.2 skrll
114 1.3.6.2 skrll cv_broadcast(&sc->sc_bellcv);
115 1.3.6.2 skrll mutex_exit(&sc->sc_bellock);
116 1.3.6.2 skrll }
117 1.3.6.2 skrll #endif
118 1.3.6.2 skrll
119 1.3.6.2 skrll static int
120 1.3.6.2 skrll spkr_audio_probe(device_t parent, cfdata_t cf, void *aux)
121 1.3.6.2 skrll {
122 1.3.6.2 skrll
123 1.3.6.2 skrll return 1;
124 1.3.6.2 skrll }
125 1.3.6.2 skrll
126 1.3.6.2 skrll static void
127 1.3.6.2 skrll spkr_audio_attach(device_t parent, device_t self, void *aux)
128 1.3.6.2 skrll {
129 1.3.6.2 skrll struct spkr_audio_softc *sc = device_private(self);
130 1.3.6.2 skrll
131 1.3.6.2 skrll aprint_naive("\n");
132 1.3.6.2 skrll aprint_normal(": PC Speaker (synthesized)\n");
133 1.3.6.2 skrll
134 1.3.6.2 skrll sc->sc_audiodev = parent;
135 1.3.6.2 skrll
136 1.3.6.2 skrll if (!pmf_device_register(self, NULL, NULL))
137 1.3.6.2 skrll aprint_error_dev(self, "couldn't establish power handler\n");
138 1.3.6.2 skrll mutex_init(&sc->sc_bellock, MUTEX_DEFAULT, IPL_SCHED);
139 1.3.6.2 skrll cv_init(&sc->sc_bellcv, "bellcv");
140 1.3.6.2 skrll
141 1.3.6.2 skrll kthread_create(PRI_BIO, KTHREAD_MPSAFE | KTHREAD_MUSTJOIN, NULL,
142 1.3.6.2 skrll bell_thread, sc, &sc->sc_bellthread, "%s", device_xname(self));
143 1.3.6.2 skrll
144 1.3.6.2 skrll spkr_attach(self, spkr_audio_tone, spkr_audio_rest);
145 1.3.6.2 skrll }
146 1.3.6.2 skrll
147 1.3.6.2 skrll static int
148 1.3.6.2 skrll spkr_audio_detach(device_t self, int flags)
149 1.3.6.2 skrll {
150 1.3.6.2 skrll struct spkr_audio_softc *sc = device_private(self);
151 1.3.6.2 skrll int error;
152 1.3.6.2 skrll
153 1.3.6.2 skrll if ((error = spkr_detach(self, flags)) != 0)
154 1.3.6.2 skrll return error;
155 1.3.6.2 skrll
156 1.3.6.2 skrll pmf_device_deregister(self);
157 1.3.6.2 skrll
158 1.3.6.2 skrll mutex_enter(&sc->sc_bellock);
159 1.3.6.2 skrll sc->sc_bell_args.dying = true;
160 1.3.6.2 skrll
161 1.3.6.2 skrll cv_broadcast(&sc->sc_bellcv);
162 1.3.6.2 skrll mutex_exit(&sc->sc_bellock);
163 1.3.6.2 skrll
164 1.3.6.2 skrll kthread_join(sc->sc_bellthread);
165 1.3.6.2 skrll cv_destroy(&sc->sc_bellcv);
166 1.3.6.2 skrll mutex_destroy(&sc->sc_bellock);
167 1.3.6.2 skrll
168 1.3.6.2 skrll return 0;
169 1.3.6.2 skrll }
170 1.3.6.2 skrll
171 1.3.6.2 skrll static void
172 1.3.6.2 skrll bell_thread(void *arg)
173 1.3.6.2 skrll {
174 1.3.6.2 skrll struct spkr_audio_softc *sc = arg;
175 1.3.6.2 skrll struct vbell_args *vb = &sc->sc_bell_args;
176 1.3.6.2 skrll u_int bpitch;
177 1.3.6.2 skrll u_int bperiod;
178 1.3.6.2 skrll u_int bvolume;
179 1.3.6.2 skrll
180 1.3.6.2 skrll for (;;) {
181 1.3.6.2 skrll mutex_enter(&sc->sc_bellock);
182 1.3.6.2 skrll cv_wait_sig(&sc->sc_bellcv, &sc->sc_bellock);
183 1.3.6.2 skrll
184 1.3.6.2 skrll if (vb->dying == true) {
185 1.3.6.2 skrll mutex_exit(&sc->sc_bellock);
186 1.3.6.2 skrll kthread_exit(0);
187 1.3.6.2 skrll }
188 1.3.6.2 skrll
189 1.3.6.2 skrll bpitch = vb->pitch;
190 1.3.6.2 skrll bperiod = vb->period;
191 1.3.6.2 skrll bvolume = vb->volume;
192 1.3.6.2 skrll mutex_exit(&sc->sc_bellock);
193 1.3.6.2 skrll audiobell(sc->sc_audiodev, bpitch, bperiod, bvolume, 0);
194 1.3.6.2 skrll }
195 1.3.6.2 skrll }
196