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 (expand / 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,14 +1,14 @@ @@ -1,14 +1,14 @@
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.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
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>
@@ -64,51 +64,53 @@ typedef struct pad_block { @@ -64,51 +64,53 @@ typedef struct pad_block {
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,
@@ -331,27 +333,27 @@ pad_close(dev_t dev, int flags, int fmt, @@ -331,27 +333,27 @@ pad_close(dev_t dev, int flags, int fmt,
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));
@@ -410,26 +412,39 @@ pad_read(dev_t dev, struct uio *uio, int @@ -410,26 +412,39 @@ pad_read(dev_t dev, struct uio *uio, int
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,
@@ -467,26 +482,28 @@ pad_set_params(void *opaque, int setmode @@ -467,26 +482,28 @@ pad_set_params(void *opaque, int setmode
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

cvs diff -r1.1 -r1.2 src/tests/dev/audio/h_pad.c (expand / 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,14 +1,14 @@ @@ -1,14 +1,14 @@
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 *
@@ -46,31 +46,31 @@ @@ -46,31 +46,31 @@
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}