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