| @@ -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 { |
64 | enum { | | 64 | enum { |
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 | |
72 | static int pad_match(device_t, cfdata_t, void *); | | 72 | static int pad_match(device_t, cfdata_t, void *); |
73 | static void pad_attach(device_t, device_t, void *); | | 73 | static void pad_attach(device_t, device_t, void *); |
74 | static int pad_detach(device_t, int); | | 74 | static int pad_detach(device_t, int); |
75 | static void pad_childdet(device_t, device_t); | | 75 | static void pad_childdet(device_t, device_t); |
76 | | | 76 | |
| | | 77 | static int pad_audio_open(void *, int); |
77 | static int pad_query_encoding(void *, struct audio_encoding *); | | 78 | static int pad_query_encoding(void *, struct audio_encoding *); |
78 | static int pad_set_params(void *, int, int, | | 79 | static 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 *); |
81 | static int pad_start_output(void *, void *, int, | | 82 | static int pad_start_output(void *, void *, int, |
82 | void (*)(void *), void *); | | 83 | void (*)(void *), void *); |
83 | static int pad_start_input(void *, void *, int, | | 84 | static int pad_start_input(void *, void *, int, |
84 | void (*)(void *), void *); | | 85 | void (*)(void *), void *); |
85 | static int pad_halt_output(void *); | | 86 | static int pad_halt_output(void *); |
86 | static int pad_halt_input(void *); | | 87 | static int pad_halt_input(void *); |
87 | static int pad_getdev(void *, struct audio_device *); | | 88 | static int pad_getdev(void *, struct audio_device *); |
88 | static int pad_set_port(void *, mixer_ctrl_t *); | | 89 | static int pad_set_port(void *, mixer_ctrl_t *); |
89 | static int pad_get_port(void *, mixer_ctrl_t *); | | 90 | static int pad_get_port(void *, mixer_ctrl_t *); |
90 | static int pad_query_devinfo(void *, mixer_devinfo_t *); | | 91 | static int pad_query_devinfo(void *, mixer_devinfo_t *); |
91 | static int pad_get_props(void *); | | 92 | static int pad_get_props(void *); |
92 | static int pad_round_blocksize(void *, int, int, const audio_params_t *); | | 93 | static int pad_round_blocksize(void *, int, int, const audio_params_t *); |
93 | static void pad_get_locks(void *, kmutex_t **, kmutex_t **); | | 94 | static void pad_get_locks(void *, kmutex_t **, kmutex_t **); |
94 | | | 95 | |
95 | static stream_filter_t *pad_swvol_filter_le(struct audio_softc *, | | 96 | static 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 *); |
97 | static stream_filter_t *pad_swvol_filter_be(struct audio_softc *, | | 98 | static 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 *); |
99 | static void pad_swvol_dtor(stream_filter_t *); | | 100 | static void pad_swvol_dtor(stream_filter_t *); |
100 | | | 101 | |
101 | static const struct audio_hw_if pad_hw_if = { | | 102 | static 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 | |
346 | int | | 348 | int |
347 | pad_read(dev_t dev, struct uio *uio, int flags) | | 349 | pad_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 | |
422 | static int | | 424 | static int |
| | | 425 | pad_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 | |
| | | 437 | static int |
423 | pad_query_encoding(void *opaque, struct audio_encoding *ae) | | 438 | pad_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 | |
434 | static int | | 449 | static int |
435 | pad_set_params(void *opaque, int setmode, int usemode, | | 450 | pad_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 | |
470 | static int | | 485 | static int |
471 | pad_start_output(void *opaque, void *block, int blksize, | | 486 | pad_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 | |
492 | static int | | 509 | static int |