Sat Oct 15 07:08:06 2016 UTC ()
pad(4) must be open before corresponding audio device is opened.

OK christos@


(nat)
diff -r1.25 -r1.26 src/sys/dev/pad/pad.c
diff -r1.1 -r1.2 src/tests/dev/audio/h_pad.c

cvs diff -r1.25 -r1.26 src/sys/dev/pad/pad.c (switch to unified diff)

--- src/sys/dev/pad/pad.c 2016/07/07 06:55:41 1.25
+++ src/sys/dev/pad/pad.c 2016/10/15 07:08:06 1.26
@@ -1,797 +1,814 @@ @@ -1,797 +1,814 @@
1/* $NetBSD: pad.c,v 1.25 2016/07/07 06:55:41 msaitoh Exp $ */ 1/* $NetBSD: pad.c,v 1.26 2016/10/15 07:08:06 nat Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca> 4 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29#include <sys/cdefs.h> 29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.25 2016/07/07 06:55:41 msaitoh Exp $"); 30__KERNEL_RCSID(0, "$NetBSD: pad.c,v 1.26 2016/10/15 07:08:06 nat Exp $");
31 31
32#include <sys/types.h> 32#include <sys/types.h>
33#include <sys/param.h> 33#include <sys/param.h>
34#include <sys/conf.h> 34#include <sys/conf.h>
35#include <sys/buf.h> 35#include <sys/buf.h>
36#include <sys/kmem.h> 36#include <sys/kmem.h>
37#include <sys/kernel.h> 37#include <sys/kernel.h>
38#include <sys/device.h> 38#include <sys/device.h>
39#include <sys/proc.h> 39#include <sys/proc.h>
40#include <sys/condvar.h> 40#include <sys/condvar.h>
41#include <sys/select.h> 41#include <sys/select.h>
42#include <sys/audioio.h> 42#include <sys/audioio.h>
43#include <sys/vnode.h> 43#include <sys/vnode.h>
44#include <sys/module.h> 44#include <sys/module.h>
45#include <sys/atomic.h> 45#include <sys/atomic.h>
46#include <sys/time.h> 46#include <sys/time.h>
47 47
48#include <dev/audio_if.h> 48#include <dev/audio_if.h>
49#include <dev/audiovar.h> 49#include <dev/audiovar.h>
50#include <dev/auconv.h> 50#include <dev/auconv.h>
51#include <dev/auvolconv.h> 51#include <dev/auvolconv.h>
52 52
53#include <dev/pad/padvar.h> 53#include <dev/pad/padvar.h>
54 54
55#define PADUNIT(x) minor(x) 55#define PADUNIT(x) minor(x)
56 56
57extern struct cfdriver pad_cd; 57extern struct cfdriver pad_cd;
58 58
59typedef struct pad_block { 59typedef struct pad_block {
60 uint8_t *pb_ptr; 60 uint8_t *pb_ptr;
61 int pb_len; 61 int pb_len;
62} pad_block_t; 62} pad_block_t;
63 63
64enum { 64enum {
65 PAD_OUTPUT_CLASS, 65 PAD_OUTPUT_CLASS,
66 PAD_INPUT_CLASS, 66 PAD_INPUT_CLASS,
67 PAD_OUTPUT_MASTER_VOLUME, 67 PAD_OUTPUT_MASTER_VOLUME,
68 PAD_INPUT_DAC_VOLUME, 68 PAD_INPUT_DAC_VOLUME,
69 PAD_ENUM_LAST, 69 PAD_ENUM_LAST,
70}; 70};
71 71
72static int pad_match(device_t, cfdata_t, void *); 72static int pad_match(device_t, cfdata_t, void *);
73static void pad_attach(device_t, device_t, void *); 73static void pad_attach(device_t, device_t, void *);
74static int pad_detach(device_t, int); 74static int pad_detach(device_t, int);
75static void pad_childdet(device_t, device_t); 75static void pad_childdet(device_t, device_t);
76 76
 77static int pad_audio_open(void *, int);
77static int pad_query_encoding(void *, struct audio_encoding *); 78static int pad_query_encoding(void *, struct audio_encoding *);
78static int pad_set_params(void *, int, int, 79static int pad_set_params(void *, int, int,
79 audio_params_t *, audio_params_t *, 80 audio_params_t *, audio_params_t *,
80 stream_filter_list_t *, stream_filter_list_t *); 81 stream_filter_list_t *, stream_filter_list_t *);
81static int pad_start_output(void *, void *, int, 82static int pad_start_output(void *, void *, int,
82 void (*)(void *), void *); 83 void (*)(void *), void *);
83static int pad_start_input(void *, void *, int, 84static int pad_start_input(void *, void *, int,
84 void (*)(void *), void *); 85 void (*)(void *), void *);
85static int pad_halt_output(void *); 86static int pad_halt_output(void *);
86static int pad_halt_input(void *); 87static int pad_halt_input(void *);
87static int pad_getdev(void *, struct audio_device *); 88static int pad_getdev(void *, struct audio_device *);
88static int pad_set_port(void *, mixer_ctrl_t *); 89static int pad_set_port(void *, mixer_ctrl_t *);
89static int pad_get_port(void *, mixer_ctrl_t *); 90static int pad_get_port(void *, mixer_ctrl_t *);
90static int pad_query_devinfo(void *, mixer_devinfo_t *); 91static int pad_query_devinfo(void *, mixer_devinfo_t *);
91static int pad_get_props(void *); 92static int pad_get_props(void *);
92static int pad_round_blocksize(void *, int, int, const audio_params_t *); 93static int pad_round_blocksize(void *, int, int, const audio_params_t *);
93static void pad_get_locks(void *, kmutex_t **, kmutex_t **); 94static void pad_get_locks(void *, kmutex_t **, kmutex_t **);
94 95
95static stream_filter_t *pad_swvol_filter_le(struct audio_softc *, 96static stream_filter_t *pad_swvol_filter_le(struct audio_softc *,
96 const audio_params_t *, const audio_params_t *); 97 const audio_params_t *, const audio_params_t *);
97static stream_filter_t *pad_swvol_filter_be(struct audio_softc *, 98static stream_filter_t *pad_swvol_filter_be(struct audio_softc *,
98 const audio_params_t *, const audio_params_t *); 99 const audio_params_t *, const audio_params_t *);
99static void pad_swvol_dtor(stream_filter_t *); 100static void pad_swvol_dtor(stream_filter_t *);
100 101
101static const struct audio_hw_if pad_hw_if = { 102static const struct audio_hw_if pad_hw_if = {
 103 .open = pad_audio_open,
102 .query_encoding = pad_query_encoding, 104 .query_encoding = pad_query_encoding,
103 .set_params = pad_set_params, 105 .set_params = pad_set_params,
104 .start_output = pad_start_output, 106 .start_output = pad_start_output,
105 .start_input = pad_start_input, 107 .start_input = pad_start_input,
106 .halt_output = pad_halt_output, 108 .halt_output = pad_halt_output,
107 .halt_input = pad_halt_input, 109 .halt_input = pad_halt_input,
108 .getdev = pad_getdev, 110 .getdev = pad_getdev,
109 .set_port = pad_set_port, 111 .set_port = pad_set_port,
110 .get_port = pad_get_port, 112 .get_port = pad_get_port,
111 .query_devinfo = pad_query_devinfo, 113 .query_devinfo = pad_query_devinfo,
112 .get_props = pad_get_props, 114 .get_props = pad_get_props,
113 .round_blocksize = pad_round_blocksize, 115 .round_blocksize = pad_round_blocksize,
114 .get_locks = pad_get_locks, 116 .get_locks = pad_get_locks,
115}; 117};
116 118
117#define PAD_NFORMATS 1 119#define PAD_NFORMATS 1
118static const struct audio_format pad_formats[PAD_NFORMATS] = { 120static const struct audio_format pad_formats[PAD_NFORMATS] = {
119 { NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16, 121 { NULL, AUMODE_PLAY|AUMODE_RECORD, AUDIO_ENCODING_SLINEAR_LE, 16, 16,
120 2, AUFMT_STEREO, 1, { 44100 } }, 122 2, AUFMT_STEREO, 1, { 44100 } },
121}; 123};
122 124
123extern void padattach(int); 125extern void padattach(int);
124 126
125static int pad_add_block(pad_softc_t *, uint8_t *, int); 127static int pad_add_block(pad_softc_t *, uint8_t *, int);
126static int pad_get_block(pad_softc_t *, pad_block_t *, int); 128static int pad_get_block(pad_softc_t *, pad_block_t *, int);
127 129
128dev_type_open(pad_open); 130dev_type_open(pad_open);
129dev_type_close(pad_close); 131dev_type_close(pad_close);
130dev_type_read(pad_read); 132dev_type_read(pad_read);
131 133
132const struct cdevsw pad_cdevsw = { 134const struct cdevsw pad_cdevsw = {
133 .d_open = pad_open, 135 .d_open = pad_open,
134 .d_close = pad_close, 136 .d_close = pad_close,
135 .d_read = pad_read, 137 .d_read = pad_read,
136 .d_write = nowrite, 138 .d_write = nowrite,
137 .d_ioctl = noioctl, 139 .d_ioctl = noioctl,
138 .d_stop = nostop, 140 .d_stop = nostop,
139 .d_tty = notty, 141 .d_tty = notty,
140 .d_poll = nopoll, 142 .d_poll = nopoll,
141 .d_mmap = nommap, 143 .d_mmap = nommap,
142 .d_kqfilter = nokqfilter, 144 .d_kqfilter = nokqfilter,
143 .d_discard = nodiscard, 145 .d_discard = nodiscard,
144 .d_flag = D_OTHER | D_MPSAFE, 146 .d_flag = D_OTHER | D_MPSAFE,
145}; 147};
146 148
147CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach, 149CFATTACH_DECL2_NEW(pad, sizeof(pad_softc_t), pad_match, pad_attach, pad_detach,
148 NULL, NULL, pad_childdet); 150 NULL, NULL, pad_childdet);
149 151
150void 152void
151padattach(int n) 153padattach(int n)
152{ 154{
153 int i, err; 155 int i, err;
154 cfdata_t cf; 156 cfdata_t cf;
155 157
156 aprint_debug("pad: requested %d units\n", n); 158 aprint_debug("pad: requested %d units\n", n);
157 159
158 err = config_cfattach_attach(pad_cd.cd_name, &pad_ca); 160 err = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
159 if (err) { 161 if (err) {
160 aprint_error("%s: couldn't register cfattach: %d\n", 162 aprint_error("%s: couldn't register cfattach: %d\n",
161 pad_cd.cd_name, err); 163 pad_cd.cd_name, err);
162 config_cfdriver_detach(&pad_cd); 164 config_cfdriver_detach(&pad_cd);
163 return; 165 return;
164 } 166 }
165 167
166 for (i = 0; i < n; i++) { 168 for (i = 0; i < n; i++) {
167 cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP); 169 cf = kmem_alloc(sizeof(struct cfdata), KM_SLEEP);
168 if (cf == NULL) { 170 if (cf == NULL) {
169 aprint_error("%s: couldn't allocate cfdata\n", 171 aprint_error("%s: couldn't allocate cfdata\n",
170 pad_cd.cd_name); 172 pad_cd.cd_name);
171 continue; 173 continue;
172 } 174 }
173 cf->cf_name = pad_cd.cd_name; 175 cf->cf_name = pad_cd.cd_name;
174 cf->cf_atname = pad_cd.cd_name; 176 cf->cf_atname = pad_cd.cd_name;
175 cf->cf_unit = i; 177 cf->cf_unit = i;
176 cf->cf_fstate = FSTATE_STAR; 178 cf->cf_fstate = FSTATE_STAR;
177 179
178 (void)config_attach_pseudo(cf); 180 (void)config_attach_pseudo(cf);
179 } 181 }
180 182
181 return; 183 return;
182} 184}
183 185
184static int 186static int
185pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize) 187pad_add_block(pad_softc_t *sc, uint8_t *blk, int blksize)
186{ 188{
187 int l; 189 int l;
188 190
189 if (sc->sc_open == 0) 191 if (sc->sc_open == 0)
190 return EIO; 192 return EIO;
191 193
192 KASSERT(mutex_owned(&sc->sc_lock)); 194 KASSERT(mutex_owned(&sc->sc_lock));
193 195
194 if (sc->sc_buflen + blksize > PAD_BUFSIZE) 196 if (sc->sc_buflen + blksize > PAD_BUFSIZE)
195 return ENOBUFS; 197 return ENOBUFS;
196 198
197 if (sc->sc_wpos + blksize <= PAD_BUFSIZE) 199 if (sc->sc_wpos + blksize <= PAD_BUFSIZE)
198 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize); 200 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, blksize);
199 else { 201 else {
200 l = PAD_BUFSIZE - sc->sc_wpos; 202 l = PAD_BUFSIZE - sc->sc_wpos;
201 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l); 203 memcpy(sc->sc_audiobuf + sc->sc_wpos, blk, l);
202 memcpy(sc->sc_audiobuf, blk + l, blksize - l); 204 memcpy(sc->sc_audiobuf, blk + l, blksize - l);
203 } 205 }
204 206
205 sc->sc_wpos += blksize; 207 sc->sc_wpos += blksize;
206 if (sc->sc_wpos > PAD_BUFSIZE) 208 if (sc->sc_wpos > PAD_BUFSIZE)
207 sc->sc_wpos -= PAD_BUFSIZE; 209 sc->sc_wpos -= PAD_BUFSIZE;
208 210
209 sc->sc_buflen += blksize; 211 sc->sc_buflen += blksize;
210 212
211 return 0; 213 return 0;
212} 214}
213 215
214static int 216static int
215pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize) 217pad_get_block(pad_softc_t *sc, pad_block_t *pb, int blksize)
216{ 218{
217 int l; 219 int l;
218 220
219 KASSERT(mutex_owned(&sc->sc_lock)); 221 KASSERT(mutex_owned(&sc->sc_lock));
220 KASSERT(pb != NULL); 222 KASSERT(pb != NULL);
221 223
222 if (sc->sc_buflen < blksize) 224 if (sc->sc_buflen < blksize)
223 return ERESTART; 225 return ERESTART;
224 226
225 pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos); 227 pb->pb_ptr = (sc->sc_audiobuf + sc->sc_rpos);
226 if (sc->sc_rpos + blksize < PAD_BUFSIZE) { 228 if (sc->sc_rpos + blksize < PAD_BUFSIZE) {
227 pb->pb_len = blksize; 229 pb->pb_len = blksize;
228 sc->sc_rpos += blksize; 230 sc->sc_rpos += blksize;
229 } else { 231 } else {
230 l = PAD_BUFSIZE - sc->sc_rpos; 232 l = PAD_BUFSIZE - sc->sc_rpos;
231 pb->pb_len = l; 233 pb->pb_len = l;
232 sc->sc_rpos = 0; 234 sc->sc_rpos = 0;
233 } 235 }
234 sc->sc_buflen -= pb->pb_len; 236 sc->sc_buflen -= pb->pb_len;
235 237
236 return 0; 238 return 0;
237} 239}
238 240
239static int 241static int
240pad_match(device_t parent, cfdata_t data, void *opaque) 242pad_match(device_t parent, cfdata_t data, void *opaque)
241{ 243{
242 244
243 return 1; 245 return 1;
244} 246}
245 247
246static void 248static void
247pad_childdet(device_t self, device_t child) 249pad_childdet(device_t self, device_t child)
248{ 250{
249 pad_softc_t *sc = device_private(self); 251 pad_softc_t *sc = device_private(self);
250 252
251 sc->sc_audiodev = NULL; 253 sc->sc_audiodev = NULL;
252} 254}
253 255
254static void 256static void
255pad_attach(device_t parent, device_t self, void *opaque) 257pad_attach(device_t parent, device_t self, void *opaque)
256{ 258{
257 pad_softc_t *sc = device_private(self); 259 pad_softc_t *sc = device_private(self);
258 260
259 aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n"); 261 aprint_normal_dev(self, "outputs: 44100Hz, 16-bit, stereo\n");
260 262
261 sc->sc_dev = self; 263 sc->sc_dev = self;
262 sc->sc_open = 0; 264 sc->sc_open = 0;
263 if (auconv_create_encodings(pad_formats, PAD_NFORMATS, 265 if (auconv_create_encodings(pad_formats, PAD_NFORMATS,
264 &sc->sc_encodings) != 0) { 266 &sc->sc_encodings) != 0) {
265 aprint_error_dev(self, "couldn't create encodings\n"); 267 aprint_error_dev(self, "couldn't create encodings\n");
266 return; 268 return;
267 } 269 }
268 270
269 cv_init(&sc->sc_condvar, device_xname(self)); 271 cv_init(&sc->sc_condvar, device_xname(self));
270 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE); 272 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
271 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE); 273 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
272 274
273 sc->sc_swvol = 255; 275 sc->sc_swvol = 255;
274 sc->sc_buflen = 0; 276 sc->sc_buflen = 0;
275 sc->sc_rpos = sc->sc_wpos = 0; 277 sc->sc_rpos = sc->sc_wpos = 0;
276 sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, sc->sc_dev); 278 sc->sc_audiodev = (void *)audio_attach_mi(&pad_hw_if, sc, sc->sc_dev);
277 279
278 if (!pmf_device_register(self, NULL, NULL)) 280 if (!pmf_device_register(self, NULL, NULL))
279 aprint_error_dev(self, "couldn't establish power handler\n"); 281 aprint_error_dev(self, "couldn't establish power handler\n");
280 282
281 return; 283 return;
282} 284}
283 285
284static int 286static int
285pad_detach(device_t self, int flags) 287pad_detach(device_t self, int flags)
286{ 288{
287 pad_softc_t *sc = device_private(self); 289 pad_softc_t *sc = device_private(self);
288 int cmaj, mn, rc; 290 int cmaj, mn, rc;
289 291
290 cmaj = cdevsw_lookup_major(&pad_cdevsw); 292 cmaj = cdevsw_lookup_major(&pad_cdevsw);
291 mn = device_unit(self); 293 mn = device_unit(self);
292 vdevgone(cmaj, mn, mn, VCHR); 294 vdevgone(cmaj, mn, mn, VCHR);
293 295
294 if ((rc = config_detach_children(self, flags)) != 0) 296 if ((rc = config_detach_children(self, flags)) != 0)
295 return rc; 297 return rc;
296 298
297 pmf_device_deregister(self); 299 pmf_device_deregister(self);
298 300
299 mutex_destroy(&sc->sc_lock); 301 mutex_destroy(&sc->sc_lock);
300 mutex_destroy(&sc->sc_intr_lock); 302 mutex_destroy(&sc->sc_intr_lock);
301 cv_destroy(&sc->sc_condvar); 303 cv_destroy(&sc->sc_condvar);
302 304
303 auconv_delete_encodings(sc->sc_encodings); 305 auconv_delete_encodings(sc->sc_encodings);
304 306
305 return 0; 307 return 0;
306} 308}
307 309
308int 310int
309pad_open(dev_t dev, int flags, int fmt, struct lwp *l) 311pad_open(dev_t dev, int flags, int fmt, struct lwp *l)
310{ 312{
311 pad_softc_t *sc; 313 pad_softc_t *sc;
312 314
313 sc = device_lookup_private(&pad_cd, PADUNIT(dev)); 315 sc = device_lookup_private(&pad_cd, PADUNIT(dev));
314 if (sc == NULL) 316 if (sc == NULL)
315 return ENXIO; 317 return ENXIO;
316 318
317 if (atomic_swap_uint(&sc->sc_open, 1) != 0) { 319 if (atomic_swap_uint(&sc->sc_open, 1) != 0) {
318 return EBUSY; 320 return EBUSY;
319 } 321 }
320  322
321 getmicrotime(&sc->sc_last); 323 getmicrotime(&sc->sc_last);
322 sc->sc_bytes_count = 0; 324 sc->sc_bytes_count = 0;
323 325
324 return 0; 326 return 0;
325} 327}
326 328
327int 329int
328pad_close(dev_t dev, int flags, int fmt, struct lwp *l) 330pad_close(dev_t dev, int flags, int fmt, struct lwp *l)
329{ 331{
330 pad_softc_t *sc; 332 pad_softc_t *sc;
331 333
332 sc = device_lookup_private(&pad_cd, PADUNIT(dev)); 334 sc = device_lookup_private(&pad_cd, PADUNIT(dev));
333 if (sc == NULL) 335 if (sc == NULL)
334 return ENXIO; 336 return ENXIO;
335 337
336 KASSERT(sc->sc_open > 0); 338 KASSERT(sc->sc_open > 0);
337 sc->sc_open = 0; 339 sc->sc_open = 0;
338 340
339 return 0; 341 return 0;
340} 342}
341 343
342#define PAD_BYTES_PER_SEC (44100 * sizeof(int16_t) * 2) 344#define PAD_BYTES_PER_SEC (44100 * sizeof(int16_t) * 2)
343#define TIMENEXTREAD (20 * 1000) 345#define TIMENEXTREAD (20 * 1000)
344#define BYTESTOSLEEP (PAD_BYTES_PER_SEC / (1000000 / TIMENEXTREAD)) 346#define BYTESTOSLEEP ((PAD_BYTES_PER_SEC / (1000000 / TIMENEXTREAD)) + PAD_BLKSIZE)
345 347
346int 348int
347pad_read(dev_t dev, struct uio *uio, int flags) 349pad_read(dev_t dev, struct uio *uio, int flags)
348{ 350{
349 struct timeval now; 351 struct timeval now;
350 uint64_t nowusec, lastusec; 352 uint64_t nowusec, lastusec;
351 pad_softc_t *sc; 353 pad_softc_t *sc;
352 pad_block_t pb; 354 pad_block_t pb;
353 void (*intr)(void *); 355 void (*intr)(void *);
354 void *intrarg; 356 void *intrarg;
355 int err, wait_ticks; 357 int err, wait_ticks;
356 358
357 sc = device_lookup_private(&pad_cd, PADUNIT(dev)); 359 sc = device_lookup_private(&pad_cd, PADUNIT(dev));
358 if (sc == NULL) 360 if (sc == NULL)
359 return ENXIO; 361 return ENXIO;
360 362
361 err = 0; 363 err = 0;
362 364
363 mutex_enter(&sc->sc_lock); 365 mutex_enter(&sc->sc_lock);
364 intr = sc->sc_intr; 366 intr = sc->sc_intr;
365 intrarg = sc->sc_intrarg; 367 intrarg = sc->sc_intrarg;
366 368
367 while (uio->uio_resid > 0 && !err) { 369 while (uio->uio_resid > 0 && !err) {
368 getmicrotime(&now); 370 getmicrotime(&now);
369 nowusec = (now.tv_sec * 1000000) + now.tv_usec; 371 nowusec = (now.tv_sec * 1000000) + now.tv_usec;
370 lastusec = (sc->sc_last.tv_sec * 1000000) + 372 lastusec = (sc->sc_last.tv_sec * 1000000) +
371 sc->sc_last.tv_usec; 373 sc->sc_last.tv_usec;
372 if (lastusec + TIMENEXTREAD > nowusec && 374 if (lastusec + TIMENEXTREAD > nowusec &&
373 sc->sc_bytes_count >= BYTESTOSLEEP) { 375 sc->sc_bytes_count >= BYTESTOSLEEP) {
374 wait_ticks = (hz * ((lastusec + TIMENEXTREAD) - 376 wait_ticks = (hz * ((lastusec + TIMENEXTREAD) -
375 nowusec)) / 1000000; 377 nowusec)) / 1000000;
376 if (wait_ticks > 0) { 378 if (wait_ticks > 0) {
377 kpause("padwait", TRUE, wait_ticks, 379 kpause("padwait", TRUE, wait_ticks,
378 &sc->sc_lock); 380 &sc->sc_lock);
379 } 381 }
380 382
381 sc->sc_bytes_count -= BYTESTOSLEEP; 383 sc->sc_bytes_count -= BYTESTOSLEEP;
382 getmicrotime(&sc->sc_last); 384 getmicrotime(&sc->sc_last);
383 } else if (sc->sc_bytes_count >= BYTESTOSLEEP) { 385 } else if (sc->sc_bytes_count >= BYTESTOSLEEP) {
384 sc->sc_bytes_count -= BYTESTOSLEEP; 386 sc->sc_bytes_count -= BYTESTOSLEEP;
385 getmicrotime(&sc->sc_last); 387 getmicrotime(&sc->sc_last);
386 } else if (lastusec + TIMENEXTREAD <= nowusec) 388 } else if (lastusec + TIMENEXTREAD <= nowusec)
387 getmicrotime(&sc->sc_last); 389 getmicrotime(&sc->sc_last);
388 390
389 err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE)); 391 err = pad_get_block(sc, &pb, min(uio->uio_resid, PAD_BLKSIZE));
390 if (!err) { 392 if (!err) {
391 sc->sc_bytes_count += pb.pb_len; 393 sc->sc_bytes_count += pb.pb_len;
392 394
393 mutex_exit(&sc->sc_lock); 395 mutex_exit(&sc->sc_lock);
394 err = uiomove(pb.pb_ptr, pb.pb_len, uio); 396 err = uiomove(pb.pb_ptr, pb.pb_len, uio);
395 mutex_enter(&sc->sc_lock); 397 mutex_enter(&sc->sc_lock);
396 continue; 398 continue;
397 } 399 }
398 400
399 if (intr) { 401 if (intr) {
400 mutex_enter(&sc->sc_intr_lock); 402 mutex_enter(&sc->sc_intr_lock);
401 kpreempt_disable(); 403 kpreempt_disable();
402 (*intr)(intrarg); 404 (*intr)(intrarg);
403 kpreempt_enable(); 405 kpreempt_enable();
404 mutex_exit(&sc->sc_intr_lock); 406 mutex_exit(&sc->sc_intr_lock);
405 intr = sc->sc_intr; 407 intr = sc->sc_intr;
406 intrarg = sc->sc_intrarg; 408 intrarg = sc->sc_intrarg;
407 err = 0; 409 err = 0;
408 continue; 410 continue;
409 } 411 }
410 err = cv_wait_sig(&sc->sc_condvar, &sc->sc_lock); 412 err = cv_wait_sig(&sc->sc_condvar, &sc->sc_lock);
411 if (err != 0) 413 if (err != 0)
412 break; 414 break;
413 415
414 intr = sc->sc_intr; 416 intr = sc->sc_intr;
415 intrarg = sc->sc_intrarg; 417 intrarg = sc->sc_intrarg;
416 } 418 }
417 mutex_exit(&sc->sc_lock); 419 mutex_exit(&sc->sc_lock);
418 420
419 return err; 421 return err;
420} 422}
421 423
422static int 424static int
 425pad_audio_open(void *opaque, int flags)
 426{
 427 pad_softc_t *sc;
 428 sc = opaque;
 429
 430 if (sc->sc_open == 0)
 431 return EIO;
 432
 433 getmicrotime(&sc->sc_last);
 434 return 0;
 435}
 436
 437static int
423pad_query_encoding(void *opaque, struct audio_encoding *ae) 438pad_query_encoding(void *opaque, struct audio_encoding *ae)
424{ 439{
425 pad_softc_t *sc; 440 pad_softc_t *sc;
426 441
427 sc = (pad_softc_t *)opaque; 442 sc = (pad_softc_t *)opaque;
428 443
429 KASSERT(mutex_owned(&sc->sc_lock)); 444 KASSERT(mutex_owned(&sc->sc_lock));
430 445
431 return auconv_query_encoding(sc->sc_encodings, ae); 446 return auconv_query_encoding(sc->sc_encodings, ae);
432} 447}
433 448
434static int 449static int
435pad_set_params(void *opaque, int setmode, int usemode, 450pad_set_params(void *opaque, int setmode, int usemode,
436 audio_params_t *play, audio_params_t *rec, 451 audio_params_t *play, audio_params_t *rec,
437 stream_filter_list_t *pfil, stream_filter_list_t *rfil) 452 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
438{ 453{
439 pad_softc_t *sc __diagused; 454 pad_softc_t *sc __diagused;
440 455
441 sc = (pad_softc_t *)opaque; 456 sc = (pad_softc_t *)opaque;
442 457
443 KASSERT(mutex_owned(&sc->sc_lock)); 458 KASSERT(mutex_owned(&sc->sc_lock));
444 459
445 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY, 460 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_PLAY,
446 play, true, pfil) < 0) 461 play, true, pfil) < 0)
447 return EINVAL; 462 return EINVAL;
448 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD, 463 if (auconv_set_converter(pad_formats, PAD_NFORMATS, AUMODE_RECORD,
449 rec, true, rfil) < 0) 464 rec, true, rfil) < 0)
450 return EINVAL; 465 return EINVAL;
451 466
452 if (pfil->req_size > 0) 467 if (pfil->req_size > 0)
453 play = &pfil->filters[0].param; 468 play = &pfil->filters[0].param;
454 switch (play->encoding) { 469 switch (play->encoding) {
455 case AUDIO_ENCODING_SLINEAR_LE: 470 case AUDIO_ENCODING_SLINEAR_LE:
456 if (play->precision == 16 && play->validbits == 16) 471 if (play->precision == 16 && play->validbits == 16)
457 pfil->prepend(pfil, pad_swvol_filter_le, play); 472 pfil->prepend(pfil, pad_swvol_filter_le, play);
458 break; 473 break;
459 case AUDIO_ENCODING_SLINEAR_BE: 474 case AUDIO_ENCODING_SLINEAR_BE:
460 if (play->precision == 16 && play->validbits == 16) 475 if (play->precision == 16 && play->validbits == 16)
461 pfil->prepend(pfil, pad_swvol_filter_be, play); 476 pfil->prepend(pfil, pad_swvol_filter_be, play);
462 break; 477 break;
463 default: 478 default:
464 break; 479 break;
465 } 480 }
466 481
467 return 0; 482 return 0;
468} 483}
469 484
470static int 485static int
471pad_start_output(void *opaque, void *block, int blksize, 486pad_start_output(void *opaque, void *block, int blksize,
472 void (*intr)(void *), void *intrarg) 487 void (*intr)(void *), void *intrarg)
473{ 488{
474 pad_softc_t *sc; 489 pad_softc_t *sc;
475 int err; 490 int err;
476 491
477 sc = (pad_softc_t *)opaque; 492 sc = (pad_softc_t *)opaque;
478 493
479 KASSERT(mutex_owned(&sc->sc_lock)); 494 KASSERT(mutex_owned(&sc->sc_lock));
 495 if (!sc->sc_open)
 496 return EIO;
480 497
481 sc->sc_intr = intr; 498 sc->sc_intr = intr;
482 sc->sc_intrarg = intrarg; 499 sc->sc_intrarg = intrarg;
483 sc->sc_blksize = blksize; 500 sc->sc_blksize = blksize;
484 501
485 err = pad_add_block(sc, block, blksize); 502 err = pad_add_block(sc, block, blksize);
486 503
487 cv_broadcast(&sc->sc_condvar); 504 cv_broadcast(&sc->sc_condvar);
488 505
489 return err; 506 return err;
490} 507}
491 508
492static int 509static int
493pad_start_input(void *opaque, void *block, int blksize, 510pad_start_input(void *opaque, void *block, int blksize,
494 void (*intr)(void *), void *intrarg) 511 void (*intr)(void *), void *intrarg)
495{ 512{
496 pad_softc_t *sc __diagused; 513 pad_softc_t *sc __diagused;
497 514
498 sc = (pad_softc_t *)opaque; 515 sc = (pad_softc_t *)opaque;
499 516
500 KASSERT(mutex_owned(&sc->sc_lock)); 517 KASSERT(mutex_owned(&sc->sc_lock));
501 518
502 return EOPNOTSUPP; 519 return EOPNOTSUPP;
503} 520}
504 521
505static int 522static int
506pad_halt_output(void *opaque) 523pad_halt_output(void *opaque)
507{ 524{
508 pad_softc_t *sc; 525 pad_softc_t *sc;
509 526
510 sc = (pad_softc_t *)opaque; 527 sc = (pad_softc_t *)opaque;
511 528
512 KASSERT(mutex_owned(&sc->sc_lock)); 529 KASSERT(mutex_owned(&sc->sc_lock));
513 530
514 sc->sc_intr = NULL; 531 sc->sc_intr = NULL;
515 sc->sc_intrarg = NULL; 532 sc->sc_intrarg = NULL;
516 sc->sc_buflen = 0; 533 sc->sc_buflen = 0;
517 sc->sc_rpos = sc->sc_wpos = 0; 534 sc->sc_rpos = sc->sc_wpos = 0;
518 535
519 return 0; 536 return 0;
520} 537}
521 538
522static int 539static int
523pad_halt_input(void *opaque) 540pad_halt_input(void *opaque)
524{ 541{
525 pad_softc_t *sc __diagused; 542 pad_softc_t *sc __diagused;
526 543
527 sc = (pad_softc_t *)opaque; 544 sc = (pad_softc_t *)opaque;
528 545
529 KASSERT(mutex_owned(&sc->sc_lock)); 546 KASSERT(mutex_owned(&sc->sc_lock));
530 547
531 return 0; 548 return 0;
532} 549}
533 550
534static int 551static int
535pad_getdev(void *opaque, struct audio_device *ret) 552pad_getdev(void *opaque, struct audio_device *ret)
536{ 553{
537 strlcpy(ret->name, "Virtual Audio", sizeof(ret->name)); 554 strlcpy(ret->name, "Virtual Audio", sizeof(ret->name));
538 strlcpy(ret->version, osrelease, sizeof(ret->version)); 555 strlcpy(ret->version, osrelease, sizeof(ret->version));
539 strlcpy(ret->config, "pad", sizeof(ret->config)); 556 strlcpy(ret->config, "pad", sizeof(ret->config));
540 557
541 return 0; 558 return 0;
542} 559}
543 560
544static int 561static int
545pad_set_port(void *opaque, mixer_ctrl_t *mc) 562pad_set_port(void *opaque, mixer_ctrl_t *mc)
546{ 563{
547 pad_softc_t *sc; 564 pad_softc_t *sc;
548 565
549 sc = (pad_softc_t *)opaque; 566 sc = (pad_softc_t *)opaque;
550 567
551 KASSERT(mutex_owned(&sc->sc_lock)); 568 KASSERT(mutex_owned(&sc->sc_lock));
552 569
553 switch (mc->dev) { 570 switch (mc->dev) {
554 case PAD_OUTPUT_MASTER_VOLUME: 571 case PAD_OUTPUT_MASTER_VOLUME:
555 case PAD_INPUT_DAC_VOLUME: 572 case PAD_INPUT_DAC_VOLUME:
556 sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO]; 573 sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
557 return 0; 574 return 0;
558 } 575 }
559 576
560 return ENXIO; 577 return ENXIO;
561} 578}
562 579
563static int 580static int
564pad_get_port(void *opaque, mixer_ctrl_t *mc) 581pad_get_port(void *opaque, mixer_ctrl_t *mc)
565{ 582{
566 pad_softc_t *sc; 583 pad_softc_t *sc;
567 584
568 sc = (pad_softc_t *)opaque; 585 sc = (pad_softc_t *)opaque;
569 586
570 KASSERT(mutex_owned(&sc->sc_lock)); 587 KASSERT(mutex_owned(&sc->sc_lock));
571 588
572 switch (mc->dev) { 589 switch (mc->dev) {
573 case PAD_OUTPUT_MASTER_VOLUME: 590 case PAD_OUTPUT_MASTER_VOLUME:
574 case PAD_INPUT_DAC_VOLUME: 591 case PAD_INPUT_DAC_VOLUME:
575 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol; 592 mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_swvol;
576 return 0; 593 return 0;
577 } 594 }
578 595
579 return ENXIO; 596 return ENXIO;
580} 597}
581 598
582static int 599static int
583pad_query_devinfo(void *opaque, mixer_devinfo_t *di) 600pad_query_devinfo(void *opaque, mixer_devinfo_t *di)
584{ 601{
585 pad_softc_t *sc __diagused; 602 pad_softc_t *sc __diagused;
586 603
587 sc = (pad_softc_t *)opaque; 604 sc = (pad_softc_t *)opaque;
588 605
589 KASSERT(mutex_owned(&sc->sc_lock)); 606 KASSERT(mutex_owned(&sc->sc_lock));
590 607
591 switch (di->index) { 608 switch (di->index) {
592 case PAD_OUTPUT_CLASS: 609 case PAD_OUTPUT_CLASS:
593 di->mixer_class = PAD_OUTPUT_CLASS; 610 di->mixer_class = PAD_OUTPUT_CLASS;
594 strcpy(di->label.name, AudioCoutputs); 611 strcpy(di->label.name, AudioCoutputs);
595 di->type = AUDIO_MIXER_CLASS; 612 di->type = AUDIO_MIXER_CLASS;
596 di->next = di->prev = AUDIO_MIXER_LAST; 613 di->next = di->prev = AUDIO_MIXER_LAST;
597 return 0; 614 return 0;
598 case PAD_INPUT_CLASS: 615 case PAD_INPUT_CLASS:
599 di->mixer_class = PAD_INPUT_CLASS; 616 di->mixer_class = PAD_INPUT_CLASS;
600 strcpy(di->label.name, AudioCinputs); 617 strcpy(di->label.name, AudioCinputs);
601 di->type = AUDIO_MIXER_CLASS; 618 di->type = AUDIO_MIXER_CLASS;
602 di->next = di->prev = AUDIO_MIXER_LAST; 619 di->next = di->prev = AUDIO_MIXER_LAST;
603 return 0; 620 return 0;
604 case PAD_OUTPUT_MASTER_VOLUME: 621 case PAD_OUTPUT_MASTER_VOLUME:
605 di->mixer_class = PAD_OUTPUT_CLASS; 622 di->mixer_class = PAD_OUTPUT_CLASS;
606 strcpy(di->label.name, AudioNmaster); 623 strcpy(di->label.name, AudioNmaster);
607 di->type = AUDIO_MIXER_VALUE; 624 di->type = AUDIO_MIXER_VALUE;
608 di->next = di->prev = AUDIO_MIXER_LAST; 625 di->next = di->prev = AUDIO_MIXER_LAST;
609 di->un.v.num_channels = 1; 626 di->un.v.num_channels = 1;
610 strcpy(di->un.v.units.name, AudioNvolume); 627 strcpy(di->un.v.units.name, AudioNvolume);
611 return 0; 628 return 0;
612 case PAD_INPUT_DAC_VOLUME: 629 case PAD_INPUT_DAC_VOLUME:
613 di->mixer_class = PAD_INPUT_CLASS; 630 di->mixer_class = PAD_INPUT_CLASS;
614 strcpy(di->label.name, AudioNdac); 631 strcpy(di->label.name, AudioNdac);
615 di->type = AUDIO_MIXER_VALUE; 632 di->type = AUDIO_MIXER_VALUE;
616 di->next = di->prev = AUDIO_MIXER_LAST; 633 di->next = di->prev = AUDIO_MIXER_LAST;
617 di->un.v.num_channels = 1; 634 di->un.v.num_channels = 1;
618 strcpy(di->un.v.units.name, AudioNvolume); 635 strcpy(di->un.v.units.name, AudioNvolume);
619 return 0; 636 return 0;
620 } 637 }
621 638
622 return ENXIO; 639 return ENXIO;
623} 640}
624 641
625static int 642static int
626pad_get_props(void *opaque) 643pad_get_props(void *opaque)
627{ 644{
628 pad_softc_t *sc __diagused; 645 pad_softc_t *sc __diagused;
629 646
630 sc = (pad_softc_t *)opaque; 647 sc = (pad_softc_t *)opaque;
631 648
632 KASSERT(mutex_owned(&sc->sc_lock)); 649 KASSERT(mutex_owned(&sc->sc_lock));
633 650
634 return 0; 651 return 0;
635} 652}
636 653
637static int 654static int
638pad_round_blocksize(void *opaque, int blksize, int mode, 655pad_round_blocksize(void *opaque, int blksize, int mode,
639 const audio_params_t *p) 656 const audio_params_t *p)
640{ 657{
641 pad_softc_t *sc __diagused; 658 pad_softc_t *sc __diagused;
642 659
643 sc = (pad_softc_t *)opaque; 660 sc = (pad_softc_t *)opaque;
644 KASSERT(mutex_owned(&sc->sc_lock)); 661 KASSERT(mutex_owned(&sc->sc_lock));
645 662
646 return PAD_BLKSIZE; 663 return PAD_BLKSIZE;
647} 664}
648 665
649static void 666static void
650pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread) 667pad_get_locks(void *opaque, kmutex_t **intr, kmutex_t **thread)
651{ 668{
652 pad_softc_t *sc; 669 pad_softc_t *sc;
653 670
654 sc = (pad_softc_t *)opaque; 671 sc = (pad_softc_t *)opaque;
655 672
656 *intr = &sc->sc_intr_lock; 673 *intr = &sc->sc_intr_lock;
657 *thread = &sc->sc_lock; 674 *thread = &sc->sc_lock;
658} 675}
659 676
660static stream_filter_t * 677static stream_filter_t *
661pad_swvol_filter_le(struct audio_softc *asc, 678pad_swvol_filter_le(struct audio_softc *asc,
662 const audio_params_t *from, const audio_params_t *to) 679 const audio_params_t *from, const audio_params_t *to)
663{ 680{
664 auvolconv_filter_t *this; 681 auvolconv_filter_t *this;
665 device_t dev = audio_get_device(asc); 682 device_t dev = audio_get_device(asc);
666 struct pad_softc *sc = device_private(dev); 683 struct pad_softc *sc = device_private(dev);
667 684
668 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP); 685 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
669 this->base.base.fetch_to = auvolconv_slinear16_le_fetch_to; 686 this->base.base.fetch_to = auvolconv_slinear16_le_fetch_to;
670 this->base.dtor = pad_swvol_dtor; 687 this->base.dtor = pad_swvol_dtor;
671 this->base.set_fetcher = stream_filter_set_fetcher; 688 this->base.set_fetcher = stream_filter_set_fetcher;
672 this->base.set_inputbuffer = stream_filter_set_inputbuffer; 689 this->base.set_inputbuffer = stream_filter_set_inputbuffer;
673 this->vol = &sc->sc_swvol; 690 this->vol = &sc->sc_swvol;
674 691
675 return (stream_filter_t *)this; 692 return (stream_filter_t *)this;
676} 693}
677 694
678static stream_filter_t * 695static stream_filter_t *
679pad_swvol_filter_be(struct audio_softc *asc, 696pad_swvol_filter_be(struct audio_softc *asc,
680 const audio_params_t *from, const audio_params_t *to) 697 const audio_params_t *from, const audio_params_t *to)
681{ 698{
682 auvolconv_filter_t *this; 699 auvolconv_filter_t *this;
683 device_t dev = audio_get_device(asc); 700 device_t dev = audio_get_device(asc);
684 struct pad_softc *sc = device_private(dev); 701 struct pad_softc *sc = device_private(dev);
685 702
686 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP); 703 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
687 this->base.base.fetch_to = auvolconv_slinear16_be_fetch_to; 704 this->base.base.fetch_to = auvolconv_slinear16_be_fetch_to;
688 this->base.dtor = pad_swvol_dtor; 705 this->base.dtor = pad_swvol_dtor;
689 this->base.set_fetcher = stream_filter_set_fetcher; 706 this->base.set_fetcher = stream_filter_set_fetcher;
690 this->base.set_inputbuffer = stream_filter_set_inputbuffer; 707 this->base.set_inputbuffer = stream_filter_set_inputbuffer;
691 this->vol = &sc->sc_swvol; 708 this->vol = &sc->sc_swvol;
692 709
693 return (stream_filter_t *)this; 710 return (stream_filter_t *)this;
694} 711}
695 712
696static void 713static void
697pad_swvol_dtor(stream_filter_t *this) 714pad_swvol_dtor(stream_filter_t *this)
698{ 715{
699 if (this) 716 if (this)
700 kmem_free(this, sizeof(auvolconv_filter_t)); 717 kmem_free(this, sizeof(auvolconv_filter_t));
701} 718}
702 719
703#ifdef _MODULE 720#ifdef _MODULE
704 721
705MODULE(MODULE_CLASS_DRIVER, pad, NULL); 722MODULE(MODULE_CLASS_DRIVER, pad, NULL);
706 723
707static const struct cfiattrdata audiobuscf_iattrdata = { 724static const struct cfiattrdata audiobuscf_iattrdata = {
708 "audiobus", 0, { { NULL, NULL, 0 }, } 725 "audiobus", 0, { { NULL, NULL, 0 }, }
709}; 726};
710static const struct cfiattrdata * const pad_attrs[] = { 727static const struct cfiattrdata * const pad_attrs[] = {
711 &audiobuscf_iattrdata, NULL 728 &audiobuscf_iattrdata, NULL
712}; 729};
713 730
714CFDRIVER_DECL(pad, DV_DULL, pad_attrs); 731CFDRIVER_DECL(pad, DV_DULL, pad_attrs);
715extern struct cfattach pad_ca; 732extern struct cfattach pad_ca;
716static int padloc[] = { -1, -1 }; 733static int padloc[] = { -1, -1 };
717 734
718static struct cfdata pad_cfdata[] = { 735static struct cfdata pad_cfdata[] = {
719 { 736 {
720 .cf_name = "pad", 737 .cf_name = "pad",
721 .cf_atname = "pad", 738 .cf_atname = "pad",
722 .cf_unit = 0, 739 .cf_unit = 0,
723 .cf_fstate = FSTATE_STAR, 740 .cf_fstate = FSTATE_STAR,
724 .cf_loc = padloc, 741 .cf_loc = padloc,
725 .cf_flags = 0, 742 .cf_flags = 0,
726 .cf_pspec = NULL, 743 .cf_pspec = NULL,
727 }, 744 },
728 { NULL, NULL, 0, 0, NULL, 0, NULL } 745 { NULL, NULL, 0, 0, NULL, 0, NULL }
729}; 746};
730 747
731static int 748static int
732pad_modcmd(modcmd_t cmd, void *arg) 749pad_modcmd(modcmd_t cmd, void *arg)
733{ 750{
734 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR; 751 devmajor_t cmajor = NODEVMAJOR, bmajor = NODEVMAJOR;
735 int error; 752 int error;
736 753
737 switch (cmd) { 754 switch (cmd) {
738 case MODULE_CMD_INIT: 755 case MODULE_CMD_INIT:
739 error = config_cfdriver_attach(&pad_cd); 756 error = config_cfdriver_attach(&pad_cd);
740 if (error) { 757 if (error) {
741 return error; 758 return error;
742 } 759 }
743 760
744 error = config_cfattach_attach(pad_cd.cd_name, &pad_ca); 761 error = config_cfattach_attach(pad_cd.cd_name, &pad_ca);
745 if (error) { 762 if (error) {
746 config_cfdriver_detach(&pad_cd); 763 config_cfdriver_detach(&pad_cd);
747 aprint_error("%s: unable to register cfattach\n", 764 aprint_error("%s: unable to register cfattach\n",
748 pad_cd.cd_name); 765 pad_cd.cd_name);
749 766
750 return error; 767 return error;
751 } 768 }
752 769
753 error = config_cfdata_attach(pad_cfdata, 1); 770 error = config_cfdata_attach(pad_cfdata, 1);
754 if (error) { 771 if (error) {
755 config_cfattach_detach(pad_cd.cd_name, &pad_ca); 772 config_cfattach_detach(pad_cd.cd_name, &pad_ca);
756 config_cfdriver_detach(&pad_cd); 773 config_cfdriver_detach(&pad_cd);
757 aprint_error("%s: unable to register cfdata\n", 774 aprint_error("%s: unable to register cfdata\n",
758 pad_cd.cd_name); 775 pad_cd.cd_name);
759 776
760 return error; 777 return error;
761 } 778 }
762 779
763 error = devsw_attach(pad_cd.cd_name, NULL, &bmajor, 780 error = devsw_attach(pad_cd.cd_name, NULL, &bmajor,
764 &pad_cdevsw, &cmajor); 781 &pad_cdevsw, &cmajor);
765 if (error) { 782 if (error) {
766 error = config_cfdata_detach(pad_cfdata); 783 error = config_cfdata_detach(pad_cfdata);
767 if (error) { 784 if (error) {
768 return error; 785 return error;
769 } 786 }
770 config_cfattach_detach(pad_cd.cd_name, &pad_ca); 787 config_cfattach_detach(pad_cd.cd_name, &pad_ca);
771 config_cfdriver_detach(&pad_cd); 788 config_cfdriver_detach(&pad_cd);
772 aprint_error("%s: unable to register devsw\n", 789 aprint_error("%s: unable to register devsw\n",
773 pad_cd.cd_name); 790 pad_cd.cd_name);
774 791
775 return error; 792 return error;
776 } 793 }
777 794
778 (void)config_attach_pseudo(pad_cfdata); 795 (void)config_attach_pseudo(pad_cfdata);
779 796
780 return 0; 797 return 0;
781 case MODULE_CMD_FINI: 798 case MODULE_CMD_FINI:
782 error = config_cfdata_detach(pad_cfdata); 799 error = config_cfdata_detach(pad_cfdata);
783 if (error) { 800 if (error) {
784 return error; 801 return error;
785 } 802 }
786 803
787 config_cfattach_detach(pad_cd.cd_name, &pad_ca); 804 config_cfattach_detach(pad_cd.cd_name, &pad_ca);
788 config_cfdriver_detach(&pad_cd); 805 config_cfdriver_detach(&pad_cd);
789 devsw_detach(NULL, &pad_cdevsw); 806 devsw_detach(NULL, &pad_cdevsw);
790 807
791 return 0; 808 return 0;
792 default: 809 default:
793 return ENOTTY; 810 return ENOTTY;
794 } 811 }
795} 812}
796 813
797#endif 814#endif

cvs diff -r1.1 -r1.2 src/tests/dev/audio/h_pad.c (switch to unified diff)

--- src/tests/dev/audio/h_pad.c 2010/08/04 13:15:15 1.1
+++ src/tests/dev/audio/h_pad.c 2016/10/15 07:08:06 1.2
@@ -1,76 +1,76 @@ @@ -1,76 +1,76 @@
1/* $NetBSD: h_pad.c,v 1.1 2010/08/04 13:15:15 pooka Exp $ */ 1/* $NetBSD: h_pad.c,v 1.2 2016/10/15 07:08:06 nat Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved. 4 * Copyright (c) 2010 Antti Kantee. All Rights Reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE. 25 * SUCH DAMAGE.
26 */ 26 */
27 27
28#include <sys/types.h> 28#include <sys/types.h>
29 29
30#include <rump/rump.h> 30#include <rump/rump.h>
31#include <rump/rump_syscalls.h> 31#include <rump/rump_syscalls.h>
32 32
33#include <err.h> 33#include <err.h>
34#include <fcntl.h> 34#include <fcntl.h>
35#include <stdio.h> 35#include <stdio.h>
36#include <stdlib.h> 36#include <stdlib.h>
37#include <string.h> 37#include <string.h>
38#include <unistd.h> 38#include <unistd.h>
39 39
40#include "h_pad_musa.c" 40#include "h_pad_musa.c"
41 41
42/* 42/*
43 * Stuff some audio into /dev/audio, read it from /dev/pad. Use in 43 * Stuff some audio into /dev/audio, read it from /dev/pad. Use in
44 * conjunction with t_pad, which tests that we got sensible output 44 * conjunction with t_pad, which tests that we got sensible output
45 * by comparing against a previous audibly good result. 45 * by comparing against a previous audibly good result.
46 */ 46 */
47 47
48#define BUFSIZE 1024 48#define BUFSIZE 1024
49 49
50int 50int
51main(int argc, char *argv[]) 51main(int argc, char *argv[])
52{ 52{
53 char buf[BUFSIZE]; 53 char buf[BUFSIZE];
54 char zeros[BUFSIZE]; 54 char zeros[BUFSIZE];
55 int padfd, audiofd; 55 int padfd, audiofd;
56 ssize_t n; 56 ssize_t n;
57 57
58 rump_init(); 58 rump_init();
59 audiofd = rump_sys_open("/dev/audio0", O_RDWR); 
60 if (audiofd == -1) 
61 err(1, "open audio"); 
62 
63 padfd = rump_sys_open("/dev/pad0", O_RDONLY); 59 padfd = rump_sys_open("/dev/pad0", O_RDONLY);
64 if (padfd == -1) 60 if (padfd == -1)
65 err(1, "open pad"); 61 err(1, "open pad");
66 62
 63 audiofd = rump_sys_open("/dev/audio0", O_RDWR);
 64 if (audiofd == -1)
 65 err(1, "open audio");
 66
67 if ((n = rump_sys_write(audiofd, musa, sizeof(musa))) != sizeof(musa)) 67 if ((n = rump_sys_write(audiofd, musa, sizeof(musa))) != sizeof(musa))
68 err(1, "write"); 68 err(1, "write");
69 69
70 memset(zeros, 0, sizeof(zeros)); 70 memset(zeros, 0, sizeof(zeros));
71 while ((n = rump_sys_read(padfd, buf, sizeof(buf))) > 0) { 71 while ((n = rump_sys_read(padfd, buf, sizeof(buf))) > 0) {
72 if (memcmp(buf, zeros, sizeof(buf)) == 0) 72 if (memcmp(buf, zeros, sizeof(buf)) == 0)
73 break; 73 break;
74 write(STDOUT_FILENO, buf, n); 74 write(STDOUT_FILENO, buf, n);
75 } 75 }
76} 76}