FreeBSD kernel sound device code
dsp.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
6 * Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#ifdef HAVE_KERNEL_OPTION_HEADERS
32#include "opt_snd.h"
33#endif
34
35#include <dev/sound/pcm/sound.h>
36#include <sys/ctype.h>
37#include <sys/lock.h>
38#include <sys/rwlock.h>
39#include <sys/sysent.h>
40
41#include <vm/vm.h>
42#include <vm/vm_object.h>
43#include <vm/vm_page.h>
44#include <vm/vm_pager.h>
45
46SND_DECLARE_FILE("$FreeBSD$");
47
49SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RWTUN,
51 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
52
53static int dsp_basename_clone = 1;
54SYSCTL_INT(_hw_snd, OID_AUTO, basename_clone, CTLFLAG_RWTUN,
56 "DSP basename cloning (0: Disable; 1: Enabled)");
57
62 TAILQ_ENTRY(dsp_cdevinfo) link;
63};
64
65#define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
66#define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
67#define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
68#define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
69
70#define DSP_CDEVINFO_CACHESIZE 8
71
72#define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
73 (y) != NULL && (y)->si_drv1 != NULL)
74
75#define OLDPCM_IOCTL
76
77static d_open_t dsp_open;
78static d_close_t dsp_close;
79static d_read_t dsp_read;
80static d_write_t dsp_write;
81static d_ioctl_t dsp_ioctl;
82static d_poll_t dsp_poll;
83static d_mmap_t dsp_mmap;
84static d_mmap_single_t dsp_mmap_single;
85
86struct cdevsw dsp_cdevsw = {
87 .d_version = D_VERSION,
88 .d_open = dsp_open,
89 .d_close = dsp_close,
90 .d_read = dsp_read,
91 .d_write = dsp_write,
92 .d_ioctl = dsp_ioctl,
93 .d_poll = dsp_poll,
94 .d_mmap = dsp_mmap,
95 .d_mmap_single = dsp_mmap_single,
96 .d_name = "dsp",
97};
98
99static eventhandler_tag dsp_ehtag = NULL;
100static int dsp_umax = -1;
101static int dsp_cmax = -1;
102
103static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
104static int dsp_oss_syncstart(int sg_id);
105static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
106static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
107static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
108static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
109static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
110#ifdef OSSV4_EXPERIMENT
111static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
112static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
113static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
114static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
115static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
116#endif
117
118static struct snddev_info *
119dsp_get_info(struct cdev *dev)
120{
121 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
122}
123
124static uint32_t
125dsp_get_flags(struct cdev *dev)
126{
127 device_t bdev;
128
129 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
130
131 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
132}
133
134static void
135dsp_set_flags(struct cdev *dev, uint32_t flags)
136{
137 device_t bdev;
138
139 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
140
141 if (bdev != NULL)
142 pcm_setflags(bdev, flags);
143}
144
145/*
146 * return the channels associated with an open device instance.
147 * lock channels specified.
148 */
149static int
150getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
151 uint32_t prio)
152{
153 struct snddev_info *d;
154 struct pcm_channel *ch;
155 uint32_t flags;
156
157 if (PCM_SIMPLEX(dev) != 0) {
158 d = dsp_get_info(dev);
159 if (!PCM_REGISTERED(d))
160 return (ENXIO);
161 PCM_LOCK(d);
162 PCM_WAIT(d);
163 PCM_ACQUIRE(d);
164 /*
165 * Note: order is important -
166 * pcm flags -> prio query flags -> wild guess
167 */
168 ch = NULL;
170 if (flags & SD_F_PRIO_WR) {
171 ch = PCM_RDCH(dev);
172 PCM_RDCH(dev) = NULL;
173 } else if (flags & SD_F_PRIO_RD) {
174 ch = PCM_WRCH(dev);
175 PCM_WRCH(dev) = NULL;
176 } else if (prio & SD_F_PRIO_WR) {
177 ch = PCM_RDCH(dev);
178 PCM_RDCH(dev) = NULL;
180 } else if (prio & SD_F_PRIO_RD) {
181 ch = PCM_WRCH(dev);
182 PCM_WRCH(dev) = NULL;
184 } else if (PCM_WRCH(dev) != NULL) {
185 ch = PCM_RDCH(dev);
186 PCM_RDCH(dev) = NULL;
188 } else if (PCM_RDCH(dev) != NULL) {
189 ch = PCM_WRCH(dev);
190 PCM_WRCH(dev) = NULL;
192 }
193 PCM_SIMPLEX(dev) = 0;
195 if (ch != NULL) {
196 CHN_LOCK(ch);
197 pcm_chnref(ch, -1);
198 pcm_chnrelease(ch);
199 }
200 PCM_RELEASE(d);
201 PCM_UNLOCK(d);
202 }
203
204 *rdch = PCM_RDCH(dev);
205 *wrch = PCM_WRCH(dev);
206
207 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
208 CHN_LOCK(*rdch);
209 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
210 CHN_LOCK(*wrch);
211
212 return (0);
213}
214
215/* unlock specified channels */
216static void
217relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
218 uint32_t prio)
219{
220 if (wrch != NULL && (prio & SD_F_PRIO_WR))
221 CHN_UNLOCK(wrch);
222 if (rdch != NULL && (prio & SD_F_PRIO_RD))
223 CHN_UNLOCK(rdch);
224}
225
226static void
228 struct pcm_channel *rdch, struct pcm_channel *wrch,
229 struct pcm_channel *volch)
230{
231 struct snddev_info *d;
232 struct dsp_cdevinfo *cdi;
233 int simplex;
234
235 d = dsp_get_info(dev);
236
237 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
238 ((rdch == NULL && wrch == NULL) || rdch != wrch),
239 ("bogus %s(), what are you trying to accomplish here?", __func__));
242
243 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
244
245 /*
246 * Scan for free instance entry and put it into the end of list.
247 * Create new one if necessary.
248 */
249 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
250 if (cdi->busy != 0)
251 break;
252 cdi->rdch = rdch;
253 cdi->wrch = wrch;
254 cdi->volch = volch;
255 cdi->simplex = simplex;
256 cdi->busy = 1;
257 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
258 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
259 dev->si_drv1 = cdi;
260 return;
261 }
262 PCM_UNLOCK(d);
263 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
264 PCM_LOCK(d);
265 cdi->rdch = rdch;
266 cdi->wrch = wrch;
267 cdi->volch = volch;
268 cdi->simplex = simplex;
269 cdi->busy = 1;
270 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
271 dev->si_drv1 = cdi;
272}
273
274static void
276{
277 struct snddev_info *d;
278 struct dsp_cdevinfo *cdi, *tmp;
279 uint32_t flags;
280 int i;
281
282 d = dsp_get_info(dev);
283
284 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
285 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
286 PCM_VOLCH(dev) == NULL,
287 ("bogus %s(), what are you trying to accomplish here?", __func__));
290
291 cdi = dev->si_drv1;
292 dev->si_drv1 = NULL;
293 cdi->rdch = NULL;
294 cdi->wrch = NULL;
295 cdi->volch = NULL;
296 cdi->simplex = 0;
297 cdi->busy = 0;
298
299 /*
300 * Once it is free, move it back to the beginning of list for
301 * faster new entry allocation.
302 */
303 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
304 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
305
306 /*
307 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
308 * Reset simplex flags.
309 */
310 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
312 TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
313 if (cdi->busy != 0) {
314 if (cdi->simplex == 0) {
315 if (cdi->rdch != NULL)
316 flags |= SD_F_PRIO_RD;
317 if (cdi->wrch != NULL)
318 flags |= SD_F_PRIO_WR;
319 }
320 } else {
321 if (i == 0) {
322 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
323 free(cdi, M_DEVBUF);
324 } else
325 i--;
326 }
327 }
328 dsp_set_flags(dev, flags);
329}
330
331void
333{
334 struct dsp_cdevinfo *cdi;
335 int i;
336
337 KASSERT(d != NULL, ("NULL snddev_info"));
340
341 TAILQ_INIT(&d->dsp_cdevinfo_pool);
342 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
343 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
344 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
345 }
346}
347
348void
350{
351 struct dsp_cdevinfo *cdi, *tmp;
352
353 KASSERT(d != NULL, ("NULL snddev_info"));
356
357 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
358 while (cdi != NULL) {
359 tmp = TAILQ_NEXT(cdi, link);
360 free(cdi, M_DEVBUF);
361 cdi = tmp;
362 }
363 TAILQ_INIT(&d->dsp_cdevinfo_pool);
364}
365
366/* duplex / simplex cdev type */
367enum {
368 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
369 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
370 DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
372
373enum {
378
379#define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
380#define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
381#define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
382#define DSP_F_READ(x) ((x) & FREAD)
383#define DSP_F_WRITE(x) ((x) & FWRITE)
384
385static const struct {
386 int type;
387 char *name;
388 char *sep;
389 char *alias;
391 int hw;
392 int max;
394 uint32_t fmt, spd;
395 int query;
396} dsp_cdevs[] = {
397 { SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
398 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
400 { SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
401 SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
403 { SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
404 SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
406 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
407 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
408 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
409 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
410 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
411 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
412 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
413 SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
414 { SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
415 SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
416 /* Low priority, OSSv4 aliases. */
417 { SND_DEV_DSP, "dsp_ac3", ".", "dsp", 0, 0, 0, 0,
418 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
420 { SND_DEV_DSP, "dsp_mmap", ".", "dsp", 0, 0, 0, 0,
421 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
423 { SND_DEV_DSP, "dsp_multich", ".", "dsp", 0, 0, 0, 0,
424 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
426 { SND_DEV_DSP, "dsp_spdifout", ".", "dsp", 0, 0, 0, 0,
427 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
429 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
430 SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
433
434#define DSP_FIXUP_ERROR() do { \
435 prio = dsp_get_flags(i_dev); \
436 if (!DSP_F_VALID(flags)) \
437 error = EINVAL; \
438 if (!DSP_F_DUPLEX(flags) && \
439 ((DSP_F_READ(flags) && d->reccount == 0) || \
440 (DSP_F_WRITE(flags) && d->playcount == 0))) \
441 error = ENOTSUP; \
442 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
443 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
444 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
445 error = EBUSY; \
446 else if (DSP_REGISTERED(d, i_dev)) \
447 error = EBUSY; \
448} while (0)
449
450static int
451dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
452{
453 struct pcm_channel *rdch, *wrch;
454 struct snddev_info *d;
455 uint32_t fmt, spd, prio, volctl;
456 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
457
458 /* Kind of impossible.. */
459 if (i_dev == NULL || td == NULL)
460 return (ENODEV);
461
462 d = dsp_get_info(i_dev);
463 if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
464 return (EBADF);
465
467
468 /* Lock snddev so nobody else can monkey with it. */
469 PCM_LOCK(d);
470 PCM_WAIT(d);
471
472 /*
473 * Try to acquire cloned device before someone else pick it.
474 * ENODEV means this is not a cloned droids.
475 */
476 error = snd_clone_acquire(i_dev);
477 if (!(error == 0 || error == ENODEV)) {
479 PCM_UNLOCK(d);
481 return (error);
482 }
483
484 error = 0;
486
487 if (error != 0) {
488 (void)snd_clone_release(i_dev);
489 PCM_UNLOCK(d);
491 return (error);
492 }
493
494 /*
495 * That is just enough. Acquire and unlock pcm lock so
496 * the other will just have to wait until we finish doing
497 * everything.
498 */
499 PCM_ACQUIRE(d);
500 PCM_UNLOCK(d);
501
502 devtype = PCMDEV(i_dev);
503 wdevunit = -1;
504 rdevunit = -1;
505 fmt = 0;
506 spd = 0;
508
509 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
510 if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
511 continue;
512 /*
513 * Volume control only valid for DSPHW devices,
514 * and it must be opened in opposite direction be it
515 * simplex or duplex. Anything else will be handled
516 * as usual.
517 */
519 if (dsp_cdevs[i].volctl != 0 &&
520 DSP_F_READ(flags)) {
522 flags &= ~FREAD;
523 flags |= FWRITE;
524 }
525 if (DSP_F_READ(flags)) {
526 (void)snd_clone_release(i_dev);
529 return (ENOTSUP);
530 }
531 wdevunit = dev2unit(i_dev);
532 } else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
533 if (dsp_cdevs[i].volctl != 0 &&
536 flags &= ~FWRITE;
537 flags |= FREAD;
538 }
539 if (DSP_F_WRITE(flags)) {
540 (void)snd_clone_release(i_dev);
543 return (ENOTSUP);
544 }
545 rdevunit = dev2unit(i_dev);
546 }
547 fmt = dsp_cdevs[i].fmt;
548 spd = dsp_cdevs[i].spd;
549 break;
550 }
551
552 /* No matching devtype? */
553 if (fmt == 0 || spd == 0)
554 panic("impossible devtype %d", devtype);
555
556 rdch = NULL;
557 wrch = NULL;
558 rderror = 0;
559 wrerror = 0;
560
561 /*
562 * if we get here, the open request is valid- either:
563 * * we were previously not open
564 * * we were open for play xor record and the opener wants
565 * the non-open direction
566 */
567 if (DSP_F_READ(flags)) {
568 /* open for read */
569 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
570 td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
571
572 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
573 rderror = ENXIO;
574
576 rderror = 0;
577
578 if (rderror != 0) {
579 if (rdch != NULL)
580 pcm_chnrelease(rdch);
581 if (!DSP_F_DUPLEX(flags)) {
582 (void)snd_clone_release(i_dev);
585 return (rderror);
586 }
587 rdch = NULL;
588 } else if (volctl == DSP_CDEV_VOLCTL_READ) {
589 if (rdch != NULL) {
590 pcm_chnref(rdch, 1);
591 pcm_chnrelease(rdch);
592 }
593 } else {
594 if (flags & O_NONBLOCK)
595 rdch->flags |= CHN_F_NBIO;
596 if (flags & O_EXCL)
597 rdch->flags |= CHN_F_EXCLUSIVE;
598 pcm_chnref(rdch, 1);
601 CHN_UNLOCK(rdch);
602 }
603 }
604
605 if (DSP_F_WRITE(flags)) {
606 /* open for write */
607 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
608 td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
609
610 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
611 wrerror = ENXIO;
612
614 wrerror = 0;
615
616 if (wrerror != 0) {
617 if (wrch != NULL)
618 pcm_chnrelease(wrch);
619 if (!DSP_F_DUPLEX(flags)) {
620 if (rdch != NULL) {
621 /*
622 * Lock, deref and release previously
623 * created record channel
624 */
625 CHN_LOCK(rdch);
626 pcm_chnref(rdch, -1);
627 pcm_chnrelease(rdch);
628 }
629 (void)snd_clone_release(i_dev);
632 return (wrerror);
633 }
634 wrch = NULL;
635 } else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
636 if (wrch != NULL) {
637 pcm_chnref(wrch, 1);
638 pcm_chnrelease(wrch);
639 }
640 } else {
641 if (flags & O_NONBLOCK)
642 wrch->flags |= CHN_F_NBIO;
643 if (flags & O_EXCL)
644 wrch->flags |= CHN_F_EXCLUSIVE;
645 pcm_chnref(wrch, 1);
648 CHN_UNLOCK(wrch);
649 }
650 }
651
652 PCM_LOCK(d);
653
654 /*
655 * We're done. Allocate channels information for this cdev.
656 */
657 switch (volctl) {
659 KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
660 dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
661 break;
663 KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
664 dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
665 break;
667 default:
668 if (wrch == NULL && rdch == NULL) {
669 (void)snd_clone_release(i_dev);
670 PCM_RELEASE(d);
671 PCM_UNLOCK(d);
673 if (wrerror != 0)
674 return (wrerror);
675 if (rderror != 0)
676 return (rderror);
677 return (EINVAL);
678 }
679 dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
680 if (rdch != NULL)
681 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
682 if (wrch != NULL)
683 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
684 break;
685 }
686
687 /*
688 * Increase clone refcount for its automatic garbage collector.
689 */
690 (void)snd_clone_ref(i_dev);
691
692 PCM_RELEASE(d);
693 PCM_UNLOCK(d);
694
696
697 return (0);
698}
699
700static int
701dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
702{
703 struct pcm_channel *rdch, *wrch, *volch;
704 struct snddev_info *d;
705 int sg_ids, rdref, wdref;
706
707 d = dsp_get_info(i_dev);
708 if (!DSP_REGISTERED(d, i_dev))
709 return (EBADF);
710
712
713 PCM_LOCK(d);
714 PCM_WAIT(d);
715 PCM_ACQUIRE(d);
716
717 rdch = PCM_RDCH(i_dev);
718 wrch = PCM_WRCH(i_dev);
719 volch = PCM_VOLCH(i_dev);
720
721 PCM_RDCH(i_dev) = NULL;
722 PCM_WRCH(i_dev) = NULL;
723 PCM_VOLCH(i_dev) = NULL;
724
725 rdref = -1;
726 wdref = -1;
727
728 if (volch != NULL) {
729 if (volch == rdch)
730 rdref--;
731 else if (volch == wrch)
732 wdref--;
733 else {
734 CHN_LOCK(volch);
735 pcm_chnref(volch, -1);
736 CHN_UNLOCK(volch);
737 }
738 }
739
740 if (rdch != NULL)
741 CHN_REMOVE(d, rdch, channels.pcm.opened);
742 if (wrch != NULL)
743 CHN_REMOVE(d, wrch, channels.pcm.opened);
744
745 if (rdch != NULL || wrch != NULL) {
746 PCM_UNLOCK(d);
747 if (rdch != NULL) {
748 /*
749 * The channel itself need not be locked because:
750 * a) Adding a channel to a syncgroup happens only
751 * in dsp_ioctl(), which cannot run concurrently
752 * to dsp_close().
753 * b) The syncmember pointer (sm) is protected by
754 * the global syncgroup list lock.
755 * c) A channel can't just disappear, invalidating
756 * pointers, unless it's closed/dereferenced
757 * first.
758 */
759 PCM_SG_LOCK();
760 sg_ids = chn_syncdestroy(rdch);
762 if (sg_ids != 0)
763 free_unr(pcmsg_unrhdr, sg_ids);
764
765 CHN_LOCK(rdch);
766 pcm_chnref(rdch, rdref);
767 chn_abort(rdch); /* won't sleep */
768 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
770 chn_reset(rdch, 0, 0);
771 pcm_chnrelease(rdch);
772 }
773 if (wrch != NULL) {
774 /*
775 * Please see block above.
776 */
777 PCM_SG_LOCK();
778 sg_ids = chn_syncdestroy(wrch);
780 if (sg_ids != 0)
781 free_unr(pcmsg_unrhdr, sg_ids);
782
783 CHN_LOCK(wrch);
784 pcm_chnref(wrch, wdref);
785 chn_flush(wrch); /* may sleep */
786 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
788 chn_reset(wrch, 0, 0);
789 pcm_chnrelease(wrch);
790 }
791 PCM_LOCK(d);
792 }
793
794 dsp_cdevinfo_free(i_dev);
795 /*
796 * Release clone busy state and unref it so the automatic
797 * garbage collector will get the hint and do the remaining
798 * cleanup process.
799 */
800 (void)snd_clone_release(i_dev);
801
802 /*
803 * destroy_dev() might sleep, so release pcm lock
804 * here and rely on pcm cv serialization.
805 */
806 PCM_UNLOCK(d);
807 (void)snd_clone_unref(i_dev);
808 PCM_LOCK(d);
809
810 PCM_RELEASE(d);
811 PCM_UNLOCK(d);
812
814
815 return (0);
816}
817
818static __inline int
819dsp_io_ops(struct cdev *i_dev, struct uio *buf)
820{
821 struct snddev_info *d;
822 struct pcm_channel **ch, *rdch, *wrch;
823 int (*chn_io)(struct pcm_channel *, struct uio *);
824 int prio, ret;
825 pid_t runpid;
826
827 KASSERT(i_dev != NULL && buf != NULL &&
828 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
829 ("%s(): io train wreck!", __func__));
830
831 d = dsp_get_info(i_dev);
832 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
833 return (EBADF);
834
836
837 switch (buf->uio_rw) {
838 case UIO_READ:
839 prio = SD_F_PRIO_RD;
840 ch = &rdch;
841 chn_io = chn_read;
842 break;
843 case UIO_WRITE:
844 prio = SD_F_PRIO_WR;
845 ch = &wrch;
846 chn_io = chn_write;
847 break;
848 default:
849 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
850 break;
851 }
852
853 rdch = NULL;
854 wrch = NULL;
855 runpid = buf->uio_td->td_proc->p_pid;
856
857 getchns(i_dev, &rdch, &wrch, prio);
858
859 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
860 if (rdch != NULL || wrch != NULL)
861 relchns(i_dev, rdch, wrch, prio);
863 return (EBADF);
864 }
865
866 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
867 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
868 relchns(i_dev, rdch, wrch, prio);
870 return (EINVAL);
871 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
872 (*ch)->flags |= CHN_F_RUNNING;
873 (*ch)->pid = runpid;
874 }
875
876 /*
877 * chn_read/write must give up channel lock in order to copy bytes
878 * from/to userland, so up the "in progress" counter to make sure
879 * someone else doesn't come along and muss up the buffer.
880 */
881 ++(*ch)->inprog;
882 ret = chn_io(*ch, buf);
883 --(*ch)->inprog;
884
885 CHN_BROADCAST(&(*ch)->cv);
886
887 relchns(i_dev, rdch, wrch, prio);
888
890
891 return (ret);
892}
893
894static int
895dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
896{
897 return (dsp_io_ops(i_dev, buf));
898}
899
900static int
901dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
902{
903 return (dsp_io_ops(i_dev, buf));
904}
905
906static int
907dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
908{
909 struct snddev_info *d;
910 struct pcm_channel *c;
911 int unit;
912
913 KASSERT(dev != NULL && volch != NULL,
914 ("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
915
916 d = dsp_get_info(dev);
917 if (!PCM_REGISTERED(d)) {
918 *volch = NULL;
919 return (EINVAL);
920 }
921
923
924 *volch = NULL;
925
926 c = PCM_VOLCH(dev);
927 if (c != NULL) {
928 if (!(c->feederflags & (1 << FEEDER_VOLUME)))
929 return (-1);
930 *volch = c;
931 return (0);
932 }
933
934 PCM_LOCK(d);
935 PCM_WAIT(d);
936 PCM_ACQUIRE(d);
937
938 unit = dev2unit(dev);
939
940 CHN_FOREACH(c, d, channels.pcm) {
941 CHN_LOCK(c);
942 if (c->unit != unit) {
943 CHN_UNLOCK(c);
944 continue;
945 }
946 *volch = c;
947 pcm_chnref(c, 1);
948 PCM_VOLCH(dev) = c;
949 CHN_UNLOCK(c);
950 PCM_RELEASE(d);
951 PCM_UNLOCK(d);
952 return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
953 }
954
955 PCM_RELEASE(d);
956 PCM_UNLOCK(d);
957
958 return (EINVAL);
959}
960
961static int
962dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
963 caddr_t arg)
964{
965 struct snddev_info *d;
966 struct pcm_channel *rdch, *wrch;
967 int j, devtype, ret;
968 int left, right, center, mute;
969
970 d = dsp_get_info(dev);
971 if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
972 return (-1);
973
975
976 j = cmd & 0xff;
977
978 rdch = PCM_RDCH(dev);
979 wrch = PCM_WRCH(dev);
980
981 /* No specific channel, look into cache */
982 if (volch == NULL)
983 volch = PCM_VOLCH(dev);
984
985 /* Look harder */
986 if (volch == NULL) {
987 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
988 volch = rdch;
989 else if (j == SOUND_MIXER_PCM && wrch != NULL)
990 volch = wrch;
991 }
992
993 devtype = PCMDEV(dev);
994
995 /* Look super harder */
996 if (volch == NULL &&
997 (devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
998 devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
999 ret = dsp_get_volume_channel(dev, &volch);
1000 if (ret != 0)
1001 return (ret);
1002 if (volch == NULL)
1003 return (EINVAL);
1004 }
1005
1006 /* Final validation */
1007 if (volch == NULL)
1008 return (EINVAL);
1009
1010 CHN_LOCK(volch);
1011 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
1012 CHN_UNLOCK(volch);
1013 return (EINVAL);
1014 }
1015
1016 switch (cmd & ~0xff) {
1017 case MIXER_WRITE(0):
1018 switch (j) {
1019 case SOUND_MIXER_MUTE:
1020 if (volch->direction == PCMDIR_REC) {
1021 chn_setmute_multi(volch, SND_VOL_C_PCM, (*(int *)arg & SOUND_MASK_RECLEV) != 0);
1022 } else {
1023 chn_setmute_multi(volch, SND_VOL_C_PCM, (*(int *)arg & SOUND_MASK_PCM) != 0);
1024 }
1025 break;
1026 case SOUND_MIXER_PCM:
1027 if (volch->direction != PCMDIR_PLAY)
1028 break;
1029 left = *(int *)arg & 0x7f;
1030 right = ((*(int *)arg) >> 8) & 0x7f;
1031 center = (left + right) >> 1;
1033 left, right, center);
1034 break;
1035 case SOUND_MIXER_RECLEV:
1036 if (volch->direction != PCMDIR_REC)
1037 break;
1038 left = *(int *)arg & 0x7f;
1039 right = ((*(int *)arg) >> 8) & 0x7f;
1040 center = (left + right) >> 1;
1042 left, right, center);
1043 break;
1044 default:
1045 /* ignore all other mixer writes */
1046 break;
1047 }
1048 break;
1049
1050 case MIXER_READ(0):
1051 switch (j) {
1052 case SOUND_MIXER_MUTE:
1053 mute = CHN_GETMUTE(volch, SND_VOL_C_PCM, SND_CHN_T_FL) ||
1055 if (volch->direction == PCMDIR_REC) {
1056 *(int *)arg = mute << SOUND_MIXER_RECLEV;
1057 } else {
1058 *(int *)arg = mute << SOUND_MIXER_PCM;
1059 }
1060 break;
1061 case SOUND_MIXER_PCM:
1062 if (volch->direction != PCMDIR_PLAY)
1063 break;
1064 *(int *)arg = CHN_GETVOLUME(volch,
1066 *(int *)arg |= CHN_GETVOLUME(volch,
1068 break;
1069 case SOUND_MIXER_RECLEV:
1070 if (volch->direction != PCMDIR_REC)
1071 break;
1072 *(int *)arg = CHN_GETVOLUME(volch,
1074 *(int *)arg |= CHN_GETVOLUME(volch,
1076 break;
1077 case SOUND_MIXER_DEVMASK:
1078 case SOUND_MIXER_CAPS:
1079 case SOUND_MIXER_STEREODEVS:
1080 if (volch->direction == PCMDIR_REC)
1081 *(int *)arg = SOUND_MASK_RECLEV;
1082 else
1083 *(int *)arg = SOUND_MASK_PCM;
1084 break;
1085 default:
1086 *(int *)arg = 0;
1087 break;
1088 }
1089 break;
1090
1091 default:
1092 break;
1093 }
1094 CHN_UNLOCK(volch);
1095 return (0);
1096}
1097
1098static int
1099dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
1100 struct thread *td)
1101{
1102 struct pcm_channel *chn, *rdch, *wrch;
1103 struct snddev_info *d;
1104 u_long xcmd;
1105 int *arg_i, ret, tmp;
1106
1107 d = dsp_get_info(i_dev);
1108 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
1109 return (EBADF);
1110
1111 PCM_GIANT_ENTER(d);
1112
1113 arg_i = (int *)arg;
1114 ret = 0;
1115 xcmd = 0;
1116 chn = NULL;
1117
1118 if (IOCGROUP(cmd) == 'M') {
1119 if (cmd == OSS_GETVERSION) {
1120 *arg_i = SOUND_VERSION;
1121 PCM_GIANT_EXIT(d);
1122 return (0);
1123 }
1124 ret = dsp_ioctl_channel(i_dev, PCM_VOLCH(i_dev), cmd, arg);
1125 if (ret != -1) {
1126 PCM_GIANT_EXIT(d);
1127 return (ret);
1128 }
1129
1130 if (d->mixer_dev != NULL) {
1132 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1135 } else
1136 ret = EBADF;
1137
1138 PCM_GIANT_EXIT(d);
1139
1140 return (ret);
1141 }
1142
1143 /*
1144 * Certain ioctls may be made on any type of device (audio, mixer,
1145 * and MIDI). Handle those special cases here.
1146 */
1147 if (IOCGROUP(cmd) == 'X') {
1149 switch(cmd) {
1150 case SNDCTL_SYSINFO:
1151 sound_oss_sysinfo((oss_sysinfo *)arg);
1152 break;
1153 case SNDCTL_CARDINFO:
1154 ret = sound_oss_card_info((oss_card_info *)arg);
1155 break;
1156 case SNDCTL_AUDIOINFO:
1157 case SNDCTL_AUDIOINFO_EX:
1158 case SNDCTL_ENGINEINFO:
1159 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
1160 break;
1161 case SNDCTL_MIXERINFO:
1162 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
1163 break;
1164 default:
1165 ret = EINVAL;
1166 }
1168 PCM_GIANT_EXIT(d);
1169 return (ret);
1170 }
1171
1172 getchns(i_dev, &rdch, &wrch, 0);
1173
1174 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
1175 wrch = NULL;
1176 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
1177 rdch = NULL;
1178
1179 if (wrch == NULL && rdch == NULL) {
1180 PCM_GIANT_EXIT(d);
1181 return (EINVAL);
1182 }
1183
1184 switch(cmd) {
1185#ifdef OLDPCM_IOCTL
1186 /*
1187 * we start with the new ioctl interface.
1188 */
1189 case AIONWRITE: /* how many bytes can write ? */
1190 if (wrch) {
1191 CHN_LOCK(wrch);
1192/*
1193 if (wrch && wrch->bufhard.dl)
1194 while (chn_wrfeed(wrch) == 0);
1195*/
1196 *arg_i = sndbuf_getfree(wrch->bufsoft);
1197 CHN_UNLOCK(wrch);
1198 } else {
1199 *arg_i = 0;
1200 ret = EINVAL;
1201 }
1202 break;
1203
1204 case AIOSSIZE: /* set the current blocksize */
1205 {
1206 struct snd_size *p = (struct snd_size *)arg;
1207
1208 p->play_size = 0;
1209 p->rec_size = 0;
1211 if (wrch) {
1212 CHN_LOCK(wrch);
1213 chn_setblocksize(wrch, 2, p->play_size);
1214 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1215 CHN_UNLOCK(wrch);
1216 }
1217 if (rdch) {
1218 CHN_LOCK(rdch);
1219 chn_setblocksize(rdch, 2, p->rec_size);
1220 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1221 CHN_UNLOCK(rdch);
1222 }
1224 }
1225 break;
1226 case AIOGSIZE: /* get the current blocksize */
1227 {
1228 struct snd_size *p = (struct snd_size *)arg;
1229
1230 if (wrch) {
1231 CHN_LOCK(wrch);
1232 p->play_size = sndbuf_getblksz(wrch->bufsoft);
1233 CHN_UNLOCK(wrch);
1234 }
1235 if (rdch) {
1236 CHN_LOCK(rdch);
1237 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
1238 CHN_UNLOCK(rdch);
1239 }
1240 }
1241 break;
1242
1243 case AIOSFMT:
1244 case AIOGFMT:
1245 {
1246 snd_chan_param *p = (snd_chan_param *)arg;
1247
1248 if (cmd == AIOSFMT &&
1249 ((p->play_format != 0 && p->play_rate == 0) ||
1250 (p->rec_format != 0 && p->rec_rate == 0))) {
1251 ret = EINVAL;
1252 break;
1253 }
1255 if (wrch) {
1256 CHN_LOCK(wrch);
1257 if (cmd == AIOSFMT && p->play_format != 0) {
1258 chn_setformat(wrch,
1259 SND_FORMAT(p->play_format,
1260 AFMT_CHANNEL(wrch->format),
1261 AFMT_EXTCHANNEL(wrch->format)));
1262 chn_setspeed(wrch, p->play_rate);
1263 }
1264 p->play_rate = wrch->speed;
1265 p->play_format = AFMT_ENCODING(wrch->format);
1266 CHN_UNLOCK(wrch);
1267 } else {
1268 p->play_rate = 0;
1269 p->play_format = 0;
1270 }
1271 if (rdch) {
1272 CHN_LOCK(rdch);
1273 if (cmd == AIOSFMT && p->rec_format != 0) {
1274 chn_setformat(rdch,
1275 SND_FORMAT(p->rec_format,
1276 AFMT_CHANNEL(rdch->format),
1277 AFMT_EXTCHANNEL(rdch->format)));
1278 chn_setspeed(rdch, p->rec_rate);
1279 }
1280 p->rec_rate = rdch->speed;
1281 p->rec_format = AFMT_ENCODING(rdch->format);
1282 CHN_UNLOCK(rdch);
1283 } else {
1284 p->rec_rate = 0;
1285 p->rec_format = 0;
1286 }
1288 }
1289 break;
1290
1291 case AIOGCAP: /* get capabilities */
1292 {
1293 snd_capabilities *p = (snd_capabilities *)arg;
1294 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
1295 struct cdev *pdev;
1296
1297 PCM_LOCK(d);
1298 if (rdch) {
1299 CHN_LOCK(rdch);
1300 rcaps = chn_getcaps(rdch);
1301 }
1302 if (wrch) {
1303 CHN_LOCK(wrch);
1304 pcaps = chn_getcaps(wrch);
1305 }
1306 p->rate_min = max(rcaps? rcaps->minspeed : 0,
1307 pcaps? pcaps->minspeed : 0);
1308 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
1309 pcaps? pcaps->maxspeed : 1000000);
1310 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
1311 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
1312 /* XXX bad on sb16 */
1313 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
1314 (wrch? chn_getformats(wrch) : 0xffffffff);
1315 if (rdch && wrch)
1316 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
1317 pdev = d->mixer_dev;
1318 p->mixers = 1; /* default: one mixer */
1319 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
1320 p->left = p->right = 100;
1321 if (wrch)
1322 CHN_UNLOCK(wrch);
1323 if (rdch)
1324 CHN_UNLOCK(rdch);
1325 PCM_UNLOCK(d);
1326 }
1327 break;
1328
1329 case AIOSTOP:
1330 if (*arg_i == AIOSYNC_PLAY && wrch) {
1331 CHN_LOCK(wrch);
1332 *arg_i = chn_abort(wrch);
1333 CHN_UNLOCK(wrch);
1334 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
1335 CHN_LOCK(rdch);
1336 *arg_i = chn_abort(rdch);
1337 CHN_UNLOCK(rdch);
1338 } else {
1339 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
1340 *arg_i = 0;
1341 }
1342 break;
1343
1344 case AIOSYNC:
1345 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
1346 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
1347 break;
1348#endif
1349 /*
1350 * here follow the standard ioctls (filio.h etc.)
1351 */
1352 case FIONREAD: /* get # bytes to read */
1353 if (rdch) {
1354 CHN_LOCK(rdch);
1355/* if (rdch && rdch->bufhard.dl)
1356 while (chn_rdfeed(rdch) == 0);
1357*/
1358 *arg_i = sndbuf_getready(rdch->bufsoft);
1359 CHN_UNLOCK(rdch);
1360 } else {
1361 *arg_i = 0;
1362 ret = EINVAL;
1363 }
1364 break;
1365
1366 case FIOASYNC: /*set/clear async i/o */
1367 DEB( printf("FIOASYNC\n") ; )
1368 break;
1369
1370 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1371 case FIONBIO: /* set/clear non-blocking i/o */
1372 if (rdch) {
1373 CHN_LOCK(rdch);
1374 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1375 rdch->flags |= CHN_F_NBIO;
1376 else
1377 rdch->flags &= ~CHN_F_NBIO;
1378 CHN_UNLOCK(rdch);
1379 }
1380 if (wrch) {
1381 CHN_LOCK(wrch);
1382 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1383 wrch->flags |= CHN_F_NBIO;
1384 else
1385 wrch->flags &= ~CHN_F_NBIO;
1386 CHN_UNLOCK(wrch);
1387 }
1388 break;
1389
1390 /*
1391 * Finally, here is the linux-compatible ioctl interface
1392 */
1393#define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1395 case SNDCTL_DSP_GETBLKSIZE:
1396 chn = wrch ? wrch : rdch;
1397 if (chn) {
1398 CHN_LOCK(chn);
1399 *arg_i = sndbuf_getblksz(chn->bufsoft);
1400 CHN_UNLOCK(chn);
1401 } else {
1402 *arg_i = 0;
1403 ret = EINVAL;
1404 }
1405 break;
1406
1407 case SNDCTL_DSP_SETBLKSIZE:
1408 RANGE(*arg_i, 16, 65536);
1410 if (wrch) {
1411 CHN_LOCK(wrch);
1412 chn_setblocksize(wrch, 2, *arg_i);
1413 CHN_UNLOCK(wrch);
1414 }
1415 if (rdch) {
1416 CHN_LOCK(rdch);
1417 chn_setblocksize(rdch, 2, *arg_i);
1418 CHN_UNLOCK(rdch);
1419 }
1421 break;
1422
1423 case SNDCTL_DSP_RESET:
1424 DEB(printf("dsp reset\n"));
1425 if (wrch) {
1426 CHN_LOCK(wrch);
1427 chn_abort(wrch);
1428 chn_resetbuf(wrch);
1429 CHN_UNLOCK(wrch);
1430 }
1431 if (rdch) {
1432 CHN_LOCK(rdch);
1433 chn_abort(rdch);
1434 chn_resetbuf(rdch);
1435 CHN_UNLOCK(rdch);
1436 }
1437 break;
1438
1439 case SNDCTL_DSP_SYNC:
1440 DEB(printf("dsp sync\n"));
1441 /* chn_sync may sleep */
1442 if (wrch) {
1443 CHN_LOCK(wrch);
1444 chn_sync(wrch, 0);
1445 CHN_UNLOCK(wrch);
1446 }
1447 break;
1448
1449 case SNDCTL_DSP_SPEED:
1450 /* chn_setspeed may sleep */
1451 tmp = 0;
1453 if (wrch) {
1454 CHN_LOCK(wrch);
1455 ret = chn_setspeed(wrch, *arg_i);
1456 tmp = wrch->speed;
1457 CHN_UNLOCK(wrch);
1458 }
1459 if (rdch && ret == 0) {
1460 CHN_LOCK(rdch);
1461 ret = chn_setspeed(rdch, *arg_i);
1462 if (tmp == 0)
1463 tmp = rdch->speed;
1464 CHN_UNLOCK(rdch);
1465 }
1467 *arg_i = tmp;
1468 break;
1469
1470 case SOUND_PCM_READ_RATE:
1471 chn = wrch ? wrch : rdch;
1472 if (chn) {
1473 CHN_LOCK(chn);
1474 *arg_i = chn->speed;
1475 CHN_UNLOCK(chn);
1476 } else {
1477 *arg_i = 0;
1478 ret = EINVAL;
1479 }
1480 break;
1481
1482 case SNDCTL_DSP_STEREO:
1483 tmp = -1;
1484 *arg_i = (*arg_i)? 2 : 1;
1486 if (wrch) {
1487 CHN_LOCK(wrch);
1488 ret = chn_setformat(wrch,
1489 SND_FORMAT(wrch->format, *arg_i, 0));
1490 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1491 CHN_UNLOCK(wrch);
1492 }
1493 if (rdch && ret == 0) {
1494 CHN_LOCK(rdch);
1495 ret = chn_setformat(rdch,
1496 SND_FORMAT(rdch->format, *arg_i, 0));
1497 if (tmp == -1)
1498 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1499 CHN_UNLOCK(rdch);
1500 }
1502 *arg_i = tmp;
1503 break;
1504
1505 case SOUND_PCM_WRITE_CHANNELS:
1506/* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1507 if (*arg_i < 0) {
1508 *arg_i = 0;
1509 ret = EINVAL;
1510 break;
1511 }
1512 if (*arg_i != 0) {
1513 struct pcmchan_matrix *m;
1514 uint32_t ext;
1515
1516 tmp = 0;
1517 if (*arg_i > SND_CHN_MAX)
1518 *arg_i = SND_CHN_MAX;
1519
1521 if (m != NULL)
1522 ext = m->ext;
1523 else
1524 ext = 0;
1525
1527 if (wrch) {
1528 CHN_LOCK(wrch);
1529 ret = chn_setformat(wrch,
1530 SND_FORMAT(wrch->format, *arg_i, ext));
1531 tmp = AFMT_CHANNEL(wrch->format);
1532 CHN_UNLOCK(wrch);
1533 }
1534 if (rdch && ret == 0) {
1535 CHN_LOCK(rdch);
1536 ret = chn_setformat(rdch,
1537 SND_FORMAT(rdch->format, *arg_i, ext));
1538 if (tmp == 0)
1539 tmp = AFMT_CHANNEL(rdch->format);
1540 CHN_UNLOCK(rdch);
1541 }
1543 *arg_i = tmp;
1544 } else {
1545 chn = wrch ? wrch : rdch;
1546 CHN_LOCK(chn);
1547 *arg_i = AFMT_CHANNEL(chn->format);
1548 CHN_UNLOCK(chn);
1549 }
1550 break;
1551
1552 case SOUND_PCM_READ_CHANNELS:
1553 chn = wrch ? wrch : rdch;
1554 if (chn) {
1555 CHN_LOCK(chn);
1556 *arg_i = AFMT_CHANNEL(chn->format);
1557 CHN_UNLOCK(chn);
1558 } else {
1559 *arg_i = 0;
1560 ret = EINVAL;
1561 }
1562 break;
1563
1564 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1565 chn = wrch ? wrch : rdch;
1566 if (chn) {
1567 CHN_LOCK(chn);
1568 *arg_i = chn_getformats(chn);
1569 CHN_UNLOCK(chn);
1570 } else {
1571 *arg_i = 0;
1572 ret = EINVAL;
1573 }
1574 break;
1575
1576 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1577 if (*arg_i != AFMT_QUERY) {
1578 tmp = 0;
1580 if (wrch) {
1581 CHN_LOCK(wrch);
1582 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1583 AFMT_CHANNEL(wrch->format),
1584 AFMT_EXTCHANNEL(wrch->format)));
1585 tmp = wrch->format;
1586 CHN_UNLOCK(wrch);
1587 }
1588 if (rdch && ret == 0) {
1589 CHN_LOCK(rdch);
1590 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1591 AFMT_CHANNEL(rdch->format),
1592 AFMT_EXTCHANNEL(rdch->format)));
1593 if (tmp == 0)
1594 tmp = rdch->format;
1595 CHN_UNLOCK(rdch);
1596 }
1598 *arg_i = AFMT_ENCODING(tmp);
1599 } else {
1600 chn = wrch ? wrch : rdch;
1601 CHN_LOCK(chn);
1602 *arg_i = AFMT_ENCODING(chn->format);
1603 CHN_UNLOCK(chn);
1604 }
1605 break;
1606
1607 case SNDCTL_DSP_SETFRAGMENT:
1608 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1609 {
1610 uint32_t fragln = (*arg_i) & 0x0000ffff;
1611 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1612 uint32_t fragsz;
1613 uint32_t r_maxfrags, r_fragsz;
1614
1615 RANGE(fragln, 4, 16);
1616 fragsz = 1 << fragln;
1617
1618 if (maxfrags == 0)
1619 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1620 if (maxfrags < 2)
1621 maxfrags = 2;
1622 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1623 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1624
1625 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1627 if (rdch) {
1628 CHN_LOCK(rdch);
1629 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1630 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1631 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1632 CHN_UNLOCK(rdch);
1633 } else {
1634 r_maxfrags = maxfrags;
1635 r_fragsz = fragsz;
1636 }
1637 if (wrch && ret == 0) {
1638 CHN_LOCK(wrch);
1639 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1640 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1641 fragsz = sndbuf_getblksz(wrch->bufsoft);
1642 CHN_UNLOCK(wrch);
1643 } else { /* use whatever came from the read channel */
1644 maxfrags = r_maxfrags;
1645 fragsz = r_fragsz;
1646 }
1648
1649 fragln = 0;
1650 while (fragsz > 1) {
1651 fragln++;
1652 fragsz >>= 1;
1653 }
1654 *arg_i = (maxfrags << 16) | fragln;
1655 }
1656 break;
1657
1658 case SNDCTL_DSP_GETISPACE:
1659 /* return the size of data available in the input queue */
1660 {
1661 audio_buf_info *a = (audio_buf_info *)arg;
1662 if (rdch) {
1663 struct snd_dbuf *bs = rdch->bufsoft;
1664
1665 CHN_LOCK(rdch);
1666 a->bytes = sndbuf_getready(bs);
1667 a->fragments = a->bytes / sndbuf_getblksz(bs);
1668 a->fragstotal = sndbuf_getblkcnt(bs);
1669 a->fragsize = sndbuf_getblksz(bs);
1670 CHN_UNLOCK(rdch);
1671 } else
1672 ret = EINVAL;
1673 }
1674 break;
1675
1676 case SNDCTL_DSP_GETOSPACE:
1677 /* return space available in the output queue */
1678 {
1679 audio_buf_info *a = (audio_buf_info *)arg;
1680 if (wrch) {
1681 struct snd_dbuf *bs = wrch->bufsoft;
1682
1683 CHN_LOCK(wrch);
1684 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1685 a->bytes = sndbuf_getfree(bs);
1686 a->fragments = a->bytes / sndbuf_getblksz(bs);
1687 a->fragstotal = sndbuf_getblkcnt(bs);
1688 a->fragsize = sndbuf_getblksz(bs);
1689 CHN_UNLOCK(wrch);
1690 } else
1691 ret = EINVAL;
1692 }
1693 break;
1694
1695 case SNDCTL_DSP_GETIPTR:
1696 {
1697 count_info *a = (count_info *)arg;
1698 if (rdch) {
1699 struct snd_dbuf *bs = rdch->bufsoft;
1700
1701 CHN_LOCK(rdch);
1702 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1703 a->bytes = sndbuf_gettotal(bs);
1704 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1705 a->ptr = sndbuf_getfreeptr(bs);
1706 rdch->blocks = sndbuf_getblocks(bs);
1707 CHN_UNLOCK(rdch);
1708 } else
1709 ret = EINVAL;
1710 }
1711 break;
1712
1713 case SNDCTL_DSP_GETOPTR:
1714 {
1715 count_info *a = (count_info *)arg;
1716 if (wrch) {
1717 struct snd_dbuf *bs = wrch->bufsoft;
1718
1719 CHN_LOCK(wrch);
1720 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1721 a->bytes = sndbuf_gettotal(bs);
1722 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1723 a->ptr = sndbuf_getreadyptr(bs);
1724 wrch->blocks = sndbuf_getblocks(bs);
1725 CHN_UNLOCK(wrch);
1726 } else
1727 ret = EINVAL;
1728 }
1729 break;
1730
1731 case SNDCTL_DSP_GETCAPS:
1732 PCM_LOCK(d);
1733 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1734 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1735 *arg_i |= PCM_CAP_DUPLEX;
1736 if (rdch && (rdch->flags & CHN_F_VIRTUAL) != 0)
1737 *arg_i |= PCM_CAP_VIRTUAL;
1738 if (wrch && (wrch->flags & CHN_F_VIRTUAL) != 0)
1739 *arg_i |= PCM_CAP_VIRTUAL;
1740 PCM_UNLOCK(d);
1741 break;
1742
1743 case SOUND_PCM_READ_BITS:
1744 chn = wrch ? wrch : rdch;
1745 if (chn) {
1746 CHN_LOCK(chn);
1747 if (chn->format & AFMT_8BIT)
1748 *arg_i = 8;
1749 else if (chn->format & AFMT_16BIT)
1750 *arg_i = 16;
1751 else if (chn->format & AFMT_24BIT)
1752 *arg_i = 24;
1753 else if (chn->format & AFMT_32BIT)
1754 *arg_i = 32;
1755 else
1756 ret = EINVAL;
1757 CHN_UNLOCK(chn);
1758 } else {
1759 *arg_i = 0;
1760 ret = EINVAL;
1761 }
1762 break;
1763
1764 case SNDCTL_DSP_SETTRIGGER:
1765 if (rdch) {
1766 CHN_LOCK(rdch);
1767 rdch->flags &= ~CHN_F_NOTRIGGER;
1768 if (*arg_i & PCM_ENABLE_INPUT)
1769 chn_start(rdch, 1);
1770 else {
1771 chn_abort(rdch);
1772 chn_resetbuf(rdch);
1773 rdch->flags |= CHN_F_NOTRIGGER;
1774 }
1775 CHN_UNLOCK(rdch);
1776 }
1777 if (wrch) {
1778 CHN_LOCK(wrch);
1779 wrch->flags &= ~CHN_F_NOTRIGGER;
1780 if (*arg_i & PCM_ENABLE_OUTPUT)
1781 chn_start(wrch, 1);
1782 else {
1783 chn_abort(wrch);
1784 chn_resetbuf(wrch);
1785 wrch->flags |= CHN_F_NOTRIGGER;
1786 }
1787 CHN_UNLOCK(wrch);
1788 }
1789 break;
1790
1791 case SNDCTL_DSP_GETTRIGGER:
1792 *arg_i = 0;
1793 if (wrch) {
1794 CHN_LOCK(wrch);
1795 if (wrch->flags & CHN_F_TRIGGERED)
1796 *arg_i |= PCM_ENABLE_OUTPUT;
1797 CHN_UNLOCK(wrch);
1798 }
1799 if (rdch) {
1800 CHN_LOCK(rdch);
1801 if (rdch->flags & CHN_F_TRIGGERED)
1802 *arg_i |= PCM_ENABLE_INPUT;
1803 CHN_UNLOCK(rdch);
1804 }
1805 break;
1806
1807 case SNDCTL_DSP_GETODELAY:
1808 if (wrch) {
1809 struct snd_dbuf *bs = wrch->bufsoft;
1810
1811 CHN_LOCK(wrch);
1812 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1813 *arg_i = sndbuf_getready(bs);
1814 CHN_UNLOCK(wrch);
1815 } else
1816 ret = EINVAL;
1817 break;
1818
1819 case SNDCTL_DSP_POST:
1820 if (wrch) {
1821 CHN_LOCK(wrch);
1822 wrch->flags &= ~CHN_F_NOTRIGGER;
1823 chn_start(wrch, 1);
1824 CHN_UNLOCK(wrch);
1825 }
1826 break;
1827
1828 case SNDCTL_DSP_SETDUPLEX:
1829 /*
1830 * switch to full-duplex mode if card is in half-duplex
1831 * mode and is able to work in full-duplex mode
1832 */
1833 PCM_LOCK(d);
1834 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1836 PCM_UNLOCK(d);
1837 break;
1838
1839 /*
1840 * The following four ioctls are simple wrappers around mixer_ioctl
1841 * with no further processing. xcmd is short for "translated
1842 * command".
1843 */
1844 case SNDCTL_DSP_GETRECVOL:
1845 if (xcmd == 0) {
1846 xcmd = SOUND_MIXER_READ_RECLEV;
1847 chn = rdch;
1848 }
1849 /* FALLTHROUGH */
1850 case SNDCTL_DSP_SETRECVOL:
1851 if (xcmd == 0) {
1852 xcmd = SOUND_MIXER_WRITE_RECLEV;
1853 chn = rdch;
1854 }
1855 /* FALLTHROUGH */
1856 case SNDCTL_DSP_GETPLAYVOL:
1857 if (xcmd == 0) {
1858 xcmd = SOUND_MIXER_READ_PCM;
1859 chn = wrch;
1860 }
1861 /* FALLTHROUGH */
1862 case SNDCTL_DSP_SETPLAYVOL:
1863 if (xcmd == 0) {
1864 xcmd = SOUND_MIXER_WRITE_PCM;
1865 chn = wrch;
1866 }
1867
1868 ret = dsp_ioctl_channel(i_dev, chn, xcmd, arg);
1869 if (ret != -1) {
1870 PCM_GIANT_EXIT(d);
1871 return (ret);
1872 }
1873
1874 if (d->mixer_dev != NULL) {
1876 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1879 } else
1880 ret = ENOTSUP;
1881
1882 break;
1883
1884 case SNDCTL_DSP_GET_RECSRC_NAMES:
1885 case SNDCTL_DSP_GET_RECSRC:
1886 case SNDCTL_DSP_SET_RECSRC:
1887 if (d->mixer_dev != NULL) {
1889 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1892 } else
1893 ret = ENOTSUP;
1894 break;
1895
1896 /*
1897 * The following 3 ioctls aren't very useful at the moment. For
1898 * now, only a single channel is associated with a cdev (/dev/dspN
1899 * instance), so there's only a single output routing to use (i.e.,
1900 * the wrch bound to this cdev).
1901 */
1902 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1903 {
1904 oss_mixer_enuminfo *ei;
1905 ei = (oss_mixer_enuminfo *)arg;
1906 ei->dev = 0;
1907 ei->ctrl = 0;
1908 ei->version = 0; /* static for now */
1909 ei->strindex[0] = 0;
1910
1911 if (wrch != NULL) {
1912 ei->nvalues = 1;
1913 strlcpy(ei->strings, wrch->name,
1914 sizeof(ei->strings));
1915 } else {
1916 ei->nvalues = 0;
1917 ei->strings[0] = '\0';
1918 }
1919 }
1920 break;
1921 case SNDCTL_DSP_GET_PLAYTGT:
1922 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1923 /*
1924 * Re: SET_PLAYTGT
1925 * OSSv4: "The value that was accepted by the device will
1926 * be returned back in the variable pointed by the
1927 * argument."
1928 */
1929 if (wrch != NULL)
1930 *arg_i = 0;
1931 else
1932 ret = EINVAL;
1933 break;
1934
1935 case SNDCTL_DSP_SILENCE:
1936 /*
1937 * Flush the software (pre-feed) buffer, but try to minimize playback
1938 * interruption. (I.e., record unplayed samples with intent to
1939 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1940 * functionality.
1941 */
1942 if (wrch == NULL)
1943 ret = EINVAL;
1944 else {
1945 struct snd_dbuf *bs;
1946 CHN_LOCK(wrch);
1947 while (wrch->inprog != 0)
1948 cv_wait(&wrch->cv, wrch->lock);
1949 bs = wrch->bufsoft;
1950 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1951 bs->sl = sndbuf_getready(bs);
1954 chn_start(wrch, 0);
1955 }
1956 CHN_UNLOCK(wrch);
1957 }
1958 break;
1959
1960 case SNDCTL_DSP_SKIP:
1961 /*
1962 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1963 * playback buffer by moving the current write position immediately
1964 * before the point where the device is currently reading the samples."
1965 */
1966 if (wrch == NULL)
1967 ret = EINVAL;
1968 else {
1969 struct snd_dbuf *bs;
1970 CHN_LOCK(wrch);
1971 while (wrch->inprog != 0)
1972 cv_wait(&wrch->cv, wrch->lock);
1973 bs = wrch->bufsoft;
1974 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1975 sndbuf_softreset(bs);
1976 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1977 bs->sl = 0;
1978 chn_start(wrch, 0);
1979 }
1980 CHN_UNLOCK(wrch);
1981 }
1982 break;
1983
1984 case SNDCTL_DSP_CURRENT_OPTR:
1985 case SNDCTL_DSP_CURRENT_IPTR:
1998 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1999 if (chn == NULL)
2000 ret = EINVAL;
2001 else {
2002 struct snd_dbuf *bs;
2003 /* int tmp; */
2004
2005 oss_count_t *oc = (oss_count_t *)arg;
2006
2007 CHN_LOCK(chn);
2008 bs = chn->bufsoft;
2009#if 0
2011 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
2012 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
2013#else
2014 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
2015 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
2016#endif
2017 CHN_UNLOCK(chn);
2018 }
2019 break;
2020
2021 case SNDCTL_DSP_HALT_OUTPUT:
2022 case SNDCTL_DSP_HALT_INPUT:
2023 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
2024 if (chn == NULL)
2025 ret = EINVAL;
2026 else {
2027 CHN_LOCK(chn);
2028 chn_abort(chn);
2029 CHN_UNLOCK(chn);
2030 }
2031 break;
2032
2033 case SNDCTL_DSP_LOW_WATER:
2034 /*
2035 * Set the number of bytes required to attract attention by
2036 * select/poll.
2037 */
2038 if (wrch != NULL) {
2039 CHN_LOCK(wrch);
2040 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
2041 CHN_UNLOCK(wrch);
2042 }
2043 if (rdch != NULL) {
2044 CHN_LOCK(rdch);
2045 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
2046 CHN_UNLOCK(rdch);
2047 }
2048 break;
2049
2050 case SNDCTL_DSP_GETERROR:
2051 /*
2052 * OSSv4 docs: "All errors and counters will automatically be
2053 * cleared to zeroes after the call so each call will return only
2054 * the errors that occurred after the previous invocation. ... The
2055 * play_underruns and rec_overrun fields are the only useful fields
2056 * returned by OSS 4.0."
2057 */
2058 {
2059 audio_errinfo *ei = (audio_errinfo *)arg;
2060
2061 bzero((void *)ei, sizeof(*ei));
2062
2063 if (wrch != NULL) {
2064 CHN_LOCK(wrch);
2065 ei->play_underruns = wrch->xruns;
2066 wrch->xruns = 0;
2067 CHN_UNLOCK(wrch);
2068 }
2069 if (rdch != NULL) {
2070 CHN_LOCK(rdch);
2071 ei->rec_overruns = rdch->xruns;
2072 rdch->xruns = 0;
2073 CHN_UNLOCK(rdch);
2074 }
2075 }
2076 break;
2077
2078 case SNDCTL_DSP_SYNCGROUP:
2080 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
2082 break;
2083
2084 case SNDCTL_DSP_SYNCSTART:
2086 ret = dsp_oss_syncstart(*arg_i);
2088 break;
2089
2090 case SNDCTL_DSP_POLICY:
2092 ret = dsp_oss_policy(wrch, rdch, *arg_i);
2094 break;
2095
2096 case SNDCTL_DSP_COOKEDMODE:
2098 if (!(dsp_get_flags(i_dev) & SD_F_BITPERFECT))
2099 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
2101 break;
2102 case SNDCTL_DSP_GET_CHNORDER:
2104 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
2106 break;
2107 case SNDCTL_DSP_SET_CHNORDER:
2109 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
2111 break;
2112 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
2114 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
2116 break;
2117 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
2118 ret = EINVAL;
2119 break;
2120#ifdef OSSV4_EXPERIMENT
2121 /*
2122 * XXX The following ioctls are not yet supported and just return
2123 * EINVAL.
2124 */
2125 case SNDCTL_DSP_GETOPEAKS:
2126 case SNDCTL_DSP_GETIPEAKS:
2127 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
2128 if (chn == NULL)
2129 ret = EINVAL;
2130 else {
2131 oss_peaks_t *op = (oss_peaks_t *)arg;
2132 int lpeak, rpeak;
2133
2134 CHN_LOCK(chn);
2135 ret = chn_getpeaks(chn, &lpeak, &rpeak);
2136 if (ret == -1)
2137 ret = EINVAL;
2138 else {
2139 (*op)[0] = lpeak;
2140 (*op)[1] = rpeak;
2141 }
2142 CHN_UNLOCK(chn);
2143 }
2144 break;
2145
2146 /*
2147 * XXX Once implemented, revisit this for proper cv protection
2148 * (if necessary).
2149 */
2150 case SNDCTL_GETLABEL:
2151 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
2152 break;
2153 case SNDCTL_SETLABEL:
2154 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
2155 break;
2156 case SNDCTL_GETSONG:
2157 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
2158 break;
2159 case SNDCTL_SETSONG:
2160 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
2161 break;
2162 case SNDCTL_SETNAME:
2163 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
2164 break;
2165#if 0
2171 case SNDCTL_DSP_READCTL:
2172 case SNDCTL_DSP_WRITECTL:
2173 ret = EINVAL;
2174 break;
2175#endif /* !0 (explicitly omitted ioctls) */
2176
2177#endif /* !OSSV4_EXPERIMENT */
2178 case SNDCTL_DSP_MAPINBUF:
2179 case SNDCTL_DSP_MAPOUTBUF:
2180 case SNDCTL_DSP_SETSYNCRO:
2181 /* undocumented */
2182
2183 case SNDCTL_DSP_SUBDIVIDE:
2184 case SOUND_PCM_WRITE_FILTER:
2185 case SOUND_PCM_READ_FILTER:
2186 /* dunno what these do, don't sound important */
2187
2188 default:
2189 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
2190 ret = EINVAL;
2191 break;
2192 }
2193
2194 PCM_GIANT_LEAVE(d);
2195
2196 return (ret);
2197}
2198
2199static int
2200dsp_poll(struct cdev *i_dev, int events, struct thread *td)
2201{
2202 struct snddev_info *d;
2203 struct pcm_channel *wrch, *rdch;
2204 int ret, e;
2205
2206 d = dsp_get_info(i_dev);
2207 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev)) {
2208 /* XXX many clients don't understand POLLNVAL */
2209 return (events & (POLLHUP | POLLPRI | POLLIN |
2210 POLLRDNORM | POLLOUT | POLLWRNORM));
2211 }
2212 PCM_GIANT_ENTER(d);
2213
2214 wrch = NULL;
2215 rdch = NULL;
2216 ret = 0;
2217
2218 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2219
2220 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
2221 e = (events & (POLLOUT | POLLWRNORM));
2222 if (e)
2223 ret |= chn_poll(wrch, e, td);
2224 }
2225
2226 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
2227 e = (events & (POLLIN | POLLRDNORM));
2228 if (e)
2229 ret |= chn_poll(rdch, e, td);
2230 }
2231
2232 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2233
2234 PCM_GIANT_LEAVE(d);
2235
2236 return (ret);
2237}
2238
2239static int
2240dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
2241 int nprot, vm_memattr_t *memattr)
2242{
2243
2244 /*
2245 * offset is in range due to checks in dsp_mmap_single().
2246 * XXX memattr is not honored.
2247 */
2248 *paddr = vtophys(offset);
2249 return (0);
2250}
2251
2252static int
2253dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
2254 vm_size_t size, struct vm_object **object, int nprot)
2255{
2256 struct snddev_info *d;
2257 struct pcm_channel *wrch, *rdch, *c;
2258
2259 /*
2260 * Reject PROT_EXEC by default. It just doesn't makes sense.
2261 * Unfortunately, we have to give up this one due to linux_mmap
2262 * changes.
2263 *
2264 * https://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
2265 *
2266 */
2267#ifdef SV_ABI_LINUX
2268 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
2270 SV_CURPROC_ABI() != SV_ABI_LINUX)))
2271#else
2272 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
2273#endif
2274 return (EINVAL);
2275
2276 /*
2277 * PROT_READ (alone) selects the input buffer.
2278 * PROT_WRITE (alone) selects the output buffer.
2279 * PROT_WRITE|PROT_READ together select the output buffer.
2280 */
2281 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
2282 return (EINVAL);
2283
2284 d = dsp_get_info(i_dev);
2285 if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
2286 return (EINVAL);
2287
2288 PCM_GIANT_ENTER(d);
2289
2290 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2291
2292 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
2293 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
2294 (*offset + size) > sndbuf_getsize(c->bufsoft) ||
2295 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
2296 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
2297 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2298 PCM_GIANT_EXIT(d);
2299 return (EINVAL);
2300 }
2301
2302 if (wrch != NULL)
2303 wrch->flags |= CHN_F_MMAP;
2304 if (rdch != NULL)
2305 rdch->flags |= CHN_F_MMAP;
2306
2307 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
2308 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
2309 *object = vm_pager_allocate(OBJT_DEVICE, i_dev,
2310 size, nprot, *offset, curthread->td_ucred);
2311
2312 PCM_GIANT_LEAVE(d);
2313
2314 if (*object == NULL)
2315 return (EINVAL);
2316 return (0);
2317}
2318
2319/* So much for dev_stdclone() */
2320static int
2321dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
2322{
2323 size_t len;
2324
2325 len = strlen(namep);
2326 if (strncmp(name, namep, len) != 0)
2327 return (ENODEV);
2328
2329 name += len;
2330
2331 if (isdigit(*name) == 0)
2332 return (ENODEV);
2333
2334 len = strlen(sep);
2335
2336 if (*name == '0' && !(name[1] == '\0' || bcmp(name + 1, sep, len) == 0))
2337 return (ENODEV);
2338
2339 for (*u = 0; isdigit(*name) != 0; name++) {
2340 *u *= 10;
2341 *u += *name - '0';
2342 if (*u > dsp_umax)
2343 return (ENODEV);
2344 }
2345
2346 if (*name == '\0')
2347 return ((use_sep == 0) ? 0 : ENODEV);
2348
2349 if (bcmp(name, sep, len) != 0 || isdigit(name[len]) == 0)
2350 return (ENODEV);
2351
2352 name += len;
2353
2354 if (*name == '0' && name[1] != '\0')
2355 return (ENODEV);
2356
2357 for (*c = 0; isdigit(*name) != 0; name++) {
2358 *c *= 10;
2359 *c += *name - '0';
2360 if (*c > dsp_cmax)
2361 return (ENODEV);
2362 }
2363
2364 if (*name != '\0')
2365 return (ENODEV);
2366
2367 return (0);
2368}
2369
2370static void
2371dsp_clone(void *arg,
2372 struct ucred *cred,
2373 char *name, int namelen, struct cdev **dev)
2374{
2375 struct snddev_info *d;
2376 struct snd_clone_entry *ce;
2377 struct pcm_channel *c;
2378 int i, unit, udcmask, cunit, devtype, devhw, devcmax, tumax;
2379 char *devname, *devcmp, *devsep;
2380
2381 KASSERT(dsp_umax >= 0 && dsp_cmax >= 0, ("Uninitialized unit!"));
2382
2383 if (*dev != NULL)
2384 return;
2385
2386 unit = -1;
2387 cunit = -1;
2388 devtype = -1;
2389 devhw = 0;
2390 devcmax = -1;
2391 tumax = -1;
2392 devname = NULL;
2393 devsep = NULL;
2394
2395 for (i = 0; unit == -1 &&
2396 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2397 devtype = dsp_cdevs[i].type;
2398 devcmp = dsp_cdevs[i].name;
2399 devsep = dsp_cdevs[i].sep;
2400 devname = dsp_cdevs[i].alias;
2401 if (devname == NULL)
2402 devname = devcmp;
2403 devhw = dsp_cdevs[i].hw;
2404 devcmax = dsp_cdevs[i].max - 1;
2405 if (strcmp(name, devcmp) == 0) {
2406 if (dsp_basename_clone != 0)
2407 unit = snd_unit;
2408 } else if (dsp_stdclone(name, devcmp, devsep,
2409 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2410 unit = -1;
2411 cunit = -1;
2412 }
2413 }
2414
2415 d = devclass_get_softc(pcm_devclass, unit);
2416 if (!PCM_REGISTERED(d) || d->clones == NULL)
2417 return;
2418
2419 /* XXX Need Giant magic entry ??? */
2420
2421 PCM_LOCK(d);
2422 if (snd_clone_disabled(d->clones)) {
2423 PCM_UNLOCK(d);
2424 return;
2425 }
2426
2427 PCM_WAIT(d);
2428 PCM_ACQUIRE(d);
2429 PCM_UNLOCK(d);
2430
2431 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2432
2433 if (devhw != 0) {
2434 KASSERT(devcmax <= dsp_cmax,
2435 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2436 if (cunit > devcmax) {
2438 return;
2439 }
2440 udcmask |= snd_c2unit(cunit);
2441 CHN_FOREACH(c, d, channels.pcm) {
2442 CHN_LOCK(c);
2443 if (c->unit != udcmask) {
2444 CHN_UNLOCK(c);
2445 continue;
2446 }
2447 CHN_UNLOCK(c);
2448 udcmask &= ~snd_c2unit(cunit);
2449 /*
2450 * Temporarily increase clone maxunit to overcome
2451 * vchan flexibility.
2452 *
2453 * # sysctl dev.pcm.0.play.vchans=256
2454 * dev.pcm.0.play.vchans: 1 -> 256
2455 * # cat /dev/zero > /dev/dsp0.vp255 &
2456 * [1] 17296
2457 * # sysctl dev.pcm.0.play.vchans=0
2458 * dev.pcm.0.play.vchans: 256 -> 1
2459 * # fg
2460 * [1] + running cat /dev/zero > /dev/dsp0.vp255
2461 * ^C
2462 * # cat /dev/zero > /dev/dsp0.vp255
2463 * zsh: operation not supported: /dev/dsp0.vp255
2464 */
2465 tumax = snd_clone_getmaxunit(d->clones);
2466 if (cunit > tumax)
2467 snd_clone_setmaxunit(d->clones, cunit);
2468 else
2469 tumax = -1;
2470 goto dsp_clone_alloc;
2471 }
2472 /*
2473 * Ok, so we're requesting unallocated vchan, but still
2474 * within maximum vchan limit.
2475 */
2476 if (((devtype == SND_DEV_DSPHW_VPLAY && d->pvchancount > 0) ||
2477 (devtype == SND_DEV_DSPHW_VREC && d->rvchancount > 0)) &&
2478 cunit < snd_maxautovchans) {
2479 udcmask &= ~snd_c2unit(cunit);
2480 tumax = snd_clone_getmaxunit(d->clones);
2481 if (cunit > tumax)
2482 snd_clone_setmaxunit(d->clones, cunit);
2483 else
2484 tumax = -1;
2485 goto dsp_clone_alloc;
2486 }
2488 return;
2489 }
2490
2491dsp_clone_alloc:
2492 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2493 if (tumax != -1)
2494 snd_clone_setmaxunit(d->clones, tumax);
2495 if (ce != NULL) {
2496 udcmask |= snd_c2unit(cunit);
2497 *dev = make_dev(&dsp_cdevsw, PCMMINOR(udcmask),
2498 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2499 devname, unit, devsep, cunit);
2500 snd_clone_register(ce, *dev);
2501 }
2502
2504
2505 if (*dev != NULL)
2506 dev_ref(*dev);
2507}
2508
2509static void
2511{
2512 if (dsp_ehtag != NULL)
2513 return;
2514 /* initialize unit numbering */
2515 snd_unit_init();
2518 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2519}
2520
2521static void
2523{
2524 if (dsp_ehtag == NULL)
2525 return;
2526 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2527 dsp_ehtag = NULL;
2528}
2529
2530SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2531SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2532
2533char *
2534dsp_unit2name(char *buf, size_t len, int unit)
2535{
2536 int i, dtype;
2537
2538 KASSERT(buf != NULL && len != 0,
2539 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2540
2541 dtype = snd_unit2d(unit);
2542
2543 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
2544 if (dtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2545 continue;
2546 snprintf(buf, len, "%s%d%s%d", dsp_cdevs[i].name,
2548 return (buf);
2549 }
2550
2551 return (NULL);
2552}
2553
2586int
2587dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2588{
2589 struct pcmchan_caps *caps;
2590 struct pcm_channel *ch;
2591 struct snddev_info *d;
2592 uint32_t fmts;
2593 int i, nchan, *rates, minch, maxch;
2594 char *devname, buf[CHN_NAMELEN];
2595
2596 /*
2597 * If probing the device that received the ioctl, make sure it's a
2598 * DSP device. (Users may use this ioctl with /dev/mixer and
2599 * /dev/midi.)
2600 */
2601 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2602 return (EINVAL);
2603
2604 ch = NULL;
2605 devname = NULL;
2606 nchan = 0;
2607 bzero(buf, sizeof(buf));
2608
2609 /*
2610 * Search for the requested audio device (channel). Start by
2611 * iterating over pcm devices.
2612 */
2613 for (i = 0; pcm_devclass != NULL &&
2614 i < devclass_get_maxunit(pcm_devclass); i++) {
2615 d = devclass_get_softc(pcm_devclass, i);
2616 if (!PCM_REGISTERED(d))
2617 continue;
2618
2619 /* XXX Need Giant magic entry ??? */
2620
2621 /* See the note in function docblock */
2623 PCM_LOCK(d);
2624
2625 CHN_FOREACH(ch, d, channels.pcm) {
2626 CHN_UNLOCKASSERT(ch);
2627 CHN_LOCK(ch);
2628 if (ai->dev == -1) {
2629 if (DSP_REGISTERED(d, i_dev) &&
2630 (ch == PCM_RDCH(i_dev) || /* record ch */
2631 ch == PCM_WRCH(i_dev))) { /* playback ch */
2632 devname = dsp_unit2name(buf,
2633 sizeof(buf), ch->unit);
2634 }
2635 } else if (ai->dev == nchan) {
2636 devname = dsp_unit2name(buf, sizeof(buf),
2637 ch->unit);
2638 }
2639 if (devname != NULL)
2640 break;
2641 CHN_UNLOCK(ch);
2642 ++nchan;
2643 }
2644
2645 if (devname != NULL) {
2646 /*
2647 * At this point, the following synchronization stuff
2648 * has happened:
2649 * - a specific PCM device is locked.
2650 * - a specific audio channel has been locked, so be
2651 * sure to unlock when exiting;
2652 */
2653
2654 caps = chn_getcaps(ch);
2655
2656 /*
2657 * With all handles collected, zero out the user's
2658 * container and begin filling in its fields.
2659 */
2660 bzero((void *)ai, sizeof(oss_audioinfo));
2661
2662 ai->dev = nchan;
2663 strlcpy(ai->name, ch->name, sizeof(ai->name));
2664
2665 if ((ch->flags & CHN_F_BUSY) == 0)
2666 ai->busy = 0;
2667 else
2668 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2669
2677 ai->pid = ch->pid;
2678
2679 /*
2680 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2681 * Note, however, that a single channel operates in
2682 * only one direction, so PCM_CAP_DUPLEX is out.
2683 */
2688 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2689 ((ch->flags & CHN_F_VIRTUAL) ? PCM_CAP_VIRTUAL : 0) |
2690 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2691
2692 /*
2693 * Collect formats supported @b natively by the
2694 * device. Also determine min/max channels. (I.e.,
2695 * mono, stereo, or both?)
2696 *
2697 * If any channel is stereo, maxch = 2;
2698 * if all channels are stereo, minch = 2, too;
2699 * if any channel is mono, minch = 1;
2700 * and if all channels are mono, maxch = 1.
2701 */
2702 minch = 0;
2703 maxch = 0;
2704 fmts = 0;
2705 for (i = 0; caps->fmtlist[i]; i++) {
2706 fmts |= caps->fmtlist[i];
2707 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2708 minch = (minch == 0) ? 2 : minch;
2709 maxch = 2;
2710 } else {
2711 minch = 1;
2712 maxch = (maxch == 0) ? 1 : maxch;
2713 }
2714 }
2715
2716 if (ch->direction == PCMDIR_PLAY)
2717 ai->oformats = fmts;
2718 else
2719 ai->iformats = fmts;
2720
2733 ai->card_number = -1;
2740 ai->port_number = -1;
2741 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2746 ai->real_device = -1;
2747 strlcpy(ai->devnode, "/dev/", sizeof(ai->devnode));
2748 strlcat(ai->devnode, devname, sizeof(ai->devnode));
2749 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2760 ai->min_rate = caps->minspeed;
2761 ai->max_rate = caps->maxspeed;
2762
2763 ai->min_channels = minch;
2764 ai->max_channels = maxch;
2765
2766 ai->nrates = chn_getrates(ch, &rates);
2767 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2768 ai->nrates = OSS_MAX_SAMPLE_RATES;
2769
2770 for (i = 0; i < ai->nrates; i++)
2771 ai->rates[i] = rates[i];
2772
2773 ai->next_play_engine = 0;
2774 ai->next_rec_engine = 0;
2775
2776 CHN_UNLOCK(ch);
2777 }
2778
2779 PCM_UNLOCK(d);
2780
2781 if (devname != NULL)
2782 return (0);
2783 }
2784
2785 /* Exhausted the search -- nothing is locked, so return. */
2786 return (EINVAL);
2787}
2788
2813static int
2814dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2815{
2816 struct pcmchan_syncmember *smrd, *smwr;
2817 struct pcmchan_syncgroup *sg;
2818 int ret, sg_ids[3];
2819
2820 smrd = NULL;
2821 smwr = NULL;
2822 sg = NULL;
2823 ret = 0;
2824
2825 /*
2826 * Free_unr() may sleep, so store released syncgroup IDs until after
2827 * all locks are released.
2828 */
2829 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2830
2831 PCM_SG_LOCK();
2832
2833 /*
2834 * - Insert channel(s) into group's member list.
2835 * - Set CHN_F_NOTRIGGER on channel(s).
2836 * - Stop channel(s).
2837 */
2838
2839 /*
2840 * If device's channels are already mapped to a group, unmap them.
2841 */
2842 if (wrch) {
2843 CHN_LOCK(wrch);
2844 sg_ids[0] = chn_syncdestroy(wrch);
2845 }
2846
2847 if (rdch) {
2848 CHN_LOCK(rdch);
2849 sg_ids[1] = chn_syncdestroy(rdch);
2850 }
2851
2852 /*
2853 * Verify that mode matches character device properites.
2854 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2855 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2856 */
2857 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2858 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2859 ret = EINVAL;
2860 goto out;
2861 }
2862
2863 /*
2864 * An id of zero indicates the user wants to create a new
2865 * syncgroup.
2866 */
2867 if (group->id == 0) {
2868 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2869 if (sg != NULL) {
2870 SLIST_INIT(&sg->members);
2871 sg->id = alloc_unr(pcmsg_unrhdr);
2872
2873 group->id = sg->id;
2874 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2875 } else
2876 ret = ENOMEM;
2877 } else {
2878 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2879 if (sg->id == group->id)
2880 break;
2881 }
2882 if (sg == NULL)
2883 ret = EINVAL;
2884 }
2885
2886 /* Couldn't create or find a syncgroup. Fail. */
2887 if (sg == NULL)
2888 goto out;
2889
2890 /*
2891 * Allocate a syncmember, assign it and a channel together, and
2892 * insert into syncgroup.
2893 */
2894 if (group->mode & PCM_ENABLE_INPUT) {
2895 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2896 if (smrd == NULL) {
2897 ret = ENOMEM;
2898 goto out;
2899 }
2900
2901 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2902 smrd->parent = sg;
2903 smrd->ch = rdch;
2904
2905 chn_abort(rdch);
2906 rdch->flags |= CHN_F_NOTRIGGER;
2907 rdch->sm = smrd;
2908 }
2909
2910 if (group->mode & PCM_ENABLE_OUTPUT) {
2911 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2912 if (smwr == NULL) {
2913 ret = ENOMEM;
2914 goto out;
2915 }
2916
2917 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2918 smwr->parent = sg;
2919 smwr->ch = wrch;
2920
2921 chn_abort(wrch);
2922 wrch->flags |= CHN_F_NOTRIGGER;
2923 wrch->sm = smwr;
2924 }
2925
2926out:
2927 if (ret != 0) {
2928 if (smrd != NULL)
2929 free(smrd, M_DEVBUF);
2930 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2931 sg_ids[2] = sg->id;
2932 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2933 free(sg, M_DEVBUF);
2934 }
2935
2936 if (wrch)
2937 wrch->sm = NULL;
2938 if (rdch)
2939 rdch->sm = NULL;
2940 }
2941
2942 if (wrch)
2943 CHN_UNLOCK(wrch);
2944 if (rdch)
2945 CHN_UNLOCK(rdch);
2946
2947 PCM_SG_UNLOCK();
2948
2949 if (sg_ids[0])
2950 free_unr(pcmsg_unrhdr, sg_ids[0]);
2951 if (sg_ids[1])
2952 free_unr(pcmsg_unrhdr, sg_ids[1]);
2953 if (sg_ids[2])
2954 free_unr(pcmsg_unrhdr, sg_ids[2]);
2955
2956 return (ret);
2957}
2958
2972static int
2974{
2975 struct pcmchan_syncmember *sm, *sm_tmp;
2976 struct pcmchan_syncgroup *sg;
2977 struct pcm_channel *c;
2978 int ret, needlocks;
2979
2980 /* Get the synclists lock */
2981 PCM_SG_LOCK();
2982
2983 do {
2984 ret = 0;
2985 needlocks = 0;
2986
2987 /* Search for syncgroup by ID */
2988 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2989 if (sg->id == sg_id)
2990 break;
2991 }
2992
2993 /* Return EINVAL if not found */
2994 if (sg == NULL) {
2995 ret = EINVAL;
2996 break;
2997 }
2998
2999 /* Any removals resulting in an empty group should've handled this */
3000 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
3001
3002 /*
3003 * Attempt to lock all member channels - if any are already
3004 * locked, unlock those acquired, sleep for a bit, and try
3005 * again.
3006 */
3007 SLIST_FOREACH(sm, &sg->members, link) {
3008 if (CHN_TRYLOCK(sm->ch) == 0) {
3009 int timo = hz * 5/1000;
3010 if (timo < 1)
3011 timo = 1;
3012
3013 /* Release all locked channels so far, retry */
3014 SLIST_FOREACH(sm_tmp, &sg->members, link) {
3015 /* sm is the member already locked */
3016 if (sm == sm_tmp)
3017 break;
3018 CHN_UNLOCK(sm_tmp->ch);
3019 }
3020
3022 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
3023 PRIBIO | PCATCH, "pcmsg", timo);
3024 if (ret == EINTR || ret == ERESTART)
3025 break;
3026
3027 needlocks = 1;
3028 ret = 0; /* Assumes ret == EAGAIN... */
3029 }
3030 }
3031 } while (needlocks && ret == 0);
3032
3033 /* Proceed only if no errors encountered. */
3034 if (ret == 0) {
3035 /* Launch channels */
3036 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
3037 SLIST_REMOVE_HEAD(&sg->members, link);
3038
3039 c = sm->ch;
3040 c->sm = NULL;
3041 chn_start(c, 1);
3042 c->flags &= ~CHN_F_NOTRIGGER;
3043 CHN_UNLOCK(c);
3044
3045 free(sm, M_DEVBUF);
3046 }
3047
3048 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
3049 free(sg, M_DEVBUF);
3050 }
3051
3052 PCM_SG_UNLOCK();
3053
3054 /*
3055 * Free_unr() may sleep, so be sure to give up the syncgroup lock
3056 * first.
3057 */
3058 if (ret == 0)
3059 free_unr(pcmsg_unrhdr, sg_id);
3060
3061 return (ret);
3062}
3063
3094static int
3095dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
3096{
3097 int ret;
3098
3099 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
3100 return (EIO);
3101
3102 /* Default: success */
3103 ret = 0;
3104
3105 if (rdch) {
3106 CHN_LOCK(rdch);
3107 ret = chn_setlatency(rdch, policy);
3108 CHN_UNLOCK(rdch);
3109 }
3110
3111 if (wrch && ret == 0) {
3112 CHN_LOCK(wrch);
3113 ret = chn_setlatency(wrch, policy);
3114 CHN_UNLOCK(wrch);
3115 }
3116
3117 if (ret)
3118 ret = EIO;
3119
3120 return (ret);
3121}
3122
3145static int
3146dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
3147{
3148
3149 /*
3150 * XXX I just don't get it. Why don't they call it
3151 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
3152 * This is just plain so confusing, incoherent,
3153 * <insert any non-printable characters here>.
3154 */
3155 if (!(enabled == 1 || enabled == 0))
3156 return (EINVAL);
3157
3158 /*
3159 * I won't give in. I'm inverting its logic here and now.
3160 * Brag all you want, but "BITPERFECT" should be the better
3161 * term here.
3162 */
3163 enabled ^= 0x00000001;
3164
3165 if (wrch != NULL) {
3166 CHN_LOCK(wrch);
3167 wrch->flags &= ~CHN_F_BITPERFECT;
3168 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3169 CHN_UNLOCK(wrch);
3170 }
3171
3172 if (rdch != NULL) {
3173 CHN_LOCK(rdch);
3174 rdch->flags &= ~CHN_F_BITPERFECT;
3175 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
3176 CHN_UNLOCK(rdch);
3177 }
3178
3179 return (0);
3180}
3181
3199static int
3200dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3201{
3202 struct pcm_channel *ch;
3203 int ret;
3204
3205 ch = (wrch != NULL) ? wrch : rdch;
3206 if (ch != NULL) {
3207 CHN_LOCK(ch);
3208 ret = chn_oss_getorder(ch, map);
3209 CHN_UNLOCK(ch);
3210 } else
3211 ret = EINVAL;
3212
3213 return (ret);
3214}
3215
3230static int
3231dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
3232{
3233 int ret;
3234
3235 ret = 0;
3236
3237 if (wrch != NULL) {
3238 CHN_LOCK(wrch);
3239 ret = chn_oss_setorder(wrch, map);
3240 CHN_UNLOCK(wrch);
3241 }
3242
3243 if (ret == 0 && rdch != NULL) {
3244 CHN_LOCK(rdch);
3245 ret = chn_oss_setorder(rdch, map);
3246 CHN_UNLOCK(rdch);
3247 }
3248
3249 return (ret);
3250}
3251
3252static int
3254 int *mask)
3255{
3256 struct pcm_channel *ch;
3257 uint32_t chnmask;
3258 int ret;
3259
3260 chnmask = 0;
3261 ch = (wrch != NULL) ? wrch : rdch;
3262
3263 if (ch != NULL) {
3264 CHN_LOCK(ch);
3265 ret = chn_oss_getmask(ch, &chnmask);
3266 CHN_UNLOCK(ch);
3267 } else
3268 ret = EINVAL;
3269
3270 if (ret == 0)
3271 *mask = chnmask;
3272
3273 return (ret);
3274}
3275
3276#ifdef OSSV4_EXPERIMENT
3299static int
3300dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3301{
3302 return (EINVAL);
3303}
3304
3323static int
3324dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
3325{
3326 return (EINVAL);
3327}
3328
3348static int
3349dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3350{
3351 return (EINVAL);
3352}
3353
3373static int
3374dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
3375{
3376 return (EINVAL);
3377}
3378
3402static int
3403dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
3404{
3405 return (EINVAL);
3406}
3407#endif /* !OSSV4_EXPERIMENT */
#define DEB(x)
Definition: als4000.c:56
u_int64_t sndbuf_gettotal(struct snd_dbuf *b)
Definition: buffer.c:557
int sndbuf_acquire(struct snd_dbuf *b, u_int8_t *from, unsigned int count)
Acquire buffer space to extend ready area.
Definition: buffer.c:618
unsigned int sndbuf_getblkcnt(struct snd_dbuf *b)
Definition: buffer.c:391
unsigned int sndbuf_getalign(struct snd_dbuf *b)
Definition: buffer.c:385
unsigned int sndbuf_getblksz(struct snd_dbuf *b)
Definition: buffer.c:403
unsigned int sndbuf_getfreeptr(struct snd_dbuf *b)
Definition: buffer.c:531
void sndbuf_fillsilence(struct snd_dbuf *b)
Zap buffer contents, resetting "ready area" fields.
Definition: buffer.c:299
unsigned int sndbuf_getfree(struct snd_dbuf *b)
Definition: buffer.c:522
unsigned int sndbuf_getready(struct snd_dbuf *b)
Definition: buffer.c:504
unsigned int sndbuf_getsize(struct snd_dbuf *b)
Definition: buffer.c:435
void sndbuf_softreset(struct snd_dbuf *b)
Reset buffer w/o flushing statistics.
Definition: buffer.c:326
unsigned int sndbuf_gethwptr(struct snd_dbuf *b)
Definition: buffer.c:488
void * sndbuf_getbufofs(struct snd_dbuf *b, unsigned int ofs)
Definition: buffer.c:427
u_int64_t sndbuf_getblocks(struct snd_dbuf *b)
Definition: buffer.c:541
int sndbuf_dispose(struct snd_dbuf *b, u_int8_t *to, unsigned int count)
Dispose samples from channel buffer, increasing size of ready area.
Definition: buffer.c:655
unsigned int sndbuf_getreadyptr(struct snd_dbuf *b)
Definition: buffer.c:513
int chn_reset(struct pcm_channel *c, uint32_t fmt, uint32_t spd)
Definition: channel.c:1129
void chn_vpc_reset(struct pcm_channel *c, int vc, int force)
Definition: channel.c:1584
int chn_syncdestroy(struct pcm_channel *c)
Remove channel from a sync group, if there is one.
Definition: channel.c:2554
int chn_flush(struct pcm_channel *c)
Definition: channel.c:962
int chn_read(struct pcm_channel *c, struct uio *buf)
Definition: channel.c:589
int chn_setspeed(struct pcm_channel *c, uint32_t speed)
Definition: channel.c:2032
int chn_setvolume_multi(struct pcm_channel *c, int vc, int left, int right, int center)
Definition: channel.c:1322
int chn_oss_setorder(struct pcm_channel *c, unsigned long long *map)
Definition: channel.c:1516
int chn_poll(struct pcm_channel *c, int ev, struct thread *td)
Definition: channel.c:897
int chn_setlatency(struct pcm_channel *c, int latency)
Definition: channel.c:1975
struct pcmchan_caps * chn_getcaps(struct pcm_channel *c)
Definition: channel.c:2277
int chn_abort(struct pcm_channel *c)
Definition: channel.c:927
int chn_setformat(struct pcm_channel *c, uint32_t format)
Definition: channel.c:2061
int chn_oss_getorder(struct pcm_channel *c, unsigned long long *map)
Definition: channel.c:1502
int chn_setblocksize(struct pcm_channel *c, int blkcnt, int blksz)
Definition: channel.c:1983
int chn_getrates(struct pcm_channel *c, int **rates)
Fetch array of supported discrete sample rates.
Definition: channel.c:2529
int chn_oss_getmask(struct pcm_channel *c, uint32_t *retmask)
Definition: channel.c:1542
void chn_resetbuf(struct pcm_channel *c)
Definition: channel.c:750
int chn_sync(struct pcm_channel *c, int threshold)
Definition: channel.c:766
struct pcm_synclist snd_pcm_syncgroups
syncgroups' master list
Definition: channel.c:222
int chn_write(struct pcm_channel *c, struct uio *buf)
Definition: channel.c:459
u_int32_t chn_getformats(struct pcm_channel *c)
Definition: channel.c:2284
u_int32_t chn_start(struct pcm_channel *c, int force)
Definition: channel.c:674
int chn_getptr(struct pcm_channel *c)
Queries sound driver for sample-aligned hardware buffer pointer index.
Definition: channel.c:2267
struct mtx snd_pcm_syncgroups_mtx
Channel sync group lock.
Definition: channel.c:212
int chn_setmute_multi(struct pcm_channel *c, int vc, int mute)
Definition: channel.c:1400
#define CHN_UNLOCK(c)
Definition: channel.h:322
#define CHN_F_MMAP
Definition: channel.h:362
#define CHN_UNLOCKASSERT(c)
Definition: channel.h:325
#define CHN_F_TRIGGERED
Definition: channel.h:357
#define CHN_LOCK(c)
Definition: channel.h:321
#define CHN_NAMELEN
Definition: channel.h:81
#define PCM_SG_UNLOCK()
Definition: channel.h:60
#define CHN_GETVOLUME(x, y, z)
Definition: channel.h:311
#define PCMDIR_PLAY
Definition: channel.h:339
#define CHN_POLICY_MAX
Definition: channel.h:422
#define CHN_F_RUNNING
Definition: channel.h:356
#define CHN_F_NBIO
Definition: channel.h:361
#define CHN_F_DEAD
Definition: channel.h:366
#define CHN_F_MMAP_INVALID
Definition: channel.h:408
#define CHN_GETMUTE(x, y, z)
Definition: channel.h:314
#define CHN_INSERT_HEAD(x, y, z)
Definition: channel.h:187
#define CHN_FOREACH(x, y, z)
Definition: channel.h:181
#define CHN_F_BUSY
Definition: channel.h:364
#define CHN_BROADCAST(x)
Definition: channel.h:252
#define CHN_REMOVE(x, y, z)
Definition: channel.h:193
#define CHN_F_NOTRIGGER
Definition: channel.h:358
#define PCM_SG_LOCK()
Definition: channel.h:58
#define CHN_F_BITPERFECT
Definition: channel.h:377
#define CHN_2NDBUFMAXSIZE
Definition: channel.h:449
#define CHN_TRYLOCK(c)
Definition: channel.h:323
#define CHN_F_EXCLUSIVE
Definition: channel.h:379
#define PCMDIR_REC
Definition: channel.h:341
#define CHN_F_VIRTUAL
Definition: channel.h:376
struct pcm_channel * c
Definition: channel_if.m:106
METHOD int free
Definition: channel_if.m:110
int ** rates
Definition: channel_if.m:220
int * lpeak
Definition: channel_if.m:195
int * rpeak
Definition: channel_if.m:196
struct pcmchan_matrix * m
Definition: channel_if.m:232
struct snd_dbuf * b
Definition: channel_if.m:105
struct snd_clone_entry * snd_clone_alloc(struct snd_clone *c, struct cdev **dev, int *unit, int tmask)
Definition: clone.c:519
int snd_clone_setmaxunit(struct snd_clone *c, int maxunit)
Definition: clone.c:202
int snd_clone_acquire(struct cdev *dev)
Definition: clone.c:385
void snd_clone_register(struct snd_clone_entry *ce, struct cdev *dev)
Definition: clone.c:498
int snd_clone_release(struct cdev *dev)
Definition: clone.c:412
int snd_clone_getmaxunit(struct snd_clone *c)
Definition: clone.c:194
int snd_clone_unref(struct cdev *dev)
Definition: clone.c:469
int snd_clone_ref(struct cdev *dev)
Definition: clone.c:450
#define snd_clone_disabled(x)
Definition: clone.h:127
static d_write_t dsp_write
Definition: dsp.c:80
static int dsp_basename_clone
Definition: dsp.c:53
void dsp_cdevinfo_init(struct snddev_info *d)
Definition: dsp.c:332
static int dsp_mmap_allow_prot_exec
Definition: dsp.c:48
struct cdevsw dsp_cdevsw
Definition: dsp.c:86
static void dsp_cdevinfo_alloc(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, struct pcm_channel *volch)
Definition: dsp.c:227
#define PCM_RDCH(x)
Definition: dsp.c:65
static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
Enable or disable "cooked" mode.
Definition: dsp.c:3146
static void dsp_clone(void *arg, struct ucred *cred, char *name, int namelen, struct cdev **dev)
Definition: dsp.c:2371
static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
Retrieve channel interleaving order.
Definition: dsp.c:3200
static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
Assigns a PCM channel to a sync group.
Definition: dsp.c:2814
static uint32_t dsp_get_flags(struct cdev *dev)
Definition: dsp.c:125
static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask)
Definition: dsp.c:3253
static void dsp_sysuninit(void *p)
Definition: dsp.c:2522
static d_mmap_t dsp_mmap
Definition: dsp.c:83
static int dsp_cmax
Definition: dsp.c:101
@ DSP_CDEV_TYPE_WRONLY
Definition: dsp.c:369
@ DSP_CDEV_TYPE_RDONLY
Definition: dsp.c:368
@ DSP_CDEV_TYPE_RDWR
Definition: dsp.c:370
SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL)
static struct snddev_info * dsp_get_info(struct cdev *dev)
Definition: dsp.c:119
SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RWTUN, &dsp_mmap_allow_prot_exec, 0, "linux mmap compatibility (-1=force disable 0=auto 1=force enable)")
static eventhandler_tag dsp_ehtag
Definition: dsp.c:99
char * name
Definition: dsp.c:387
static int dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
Definition: dsp.c:2321
void dsp_cdevinfo_flush(struct snddev_info *d)
Definition: dsp.c:349
static int getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, uint32_t prio)
Definition: dsp.c:150
#define DSP_F_READ(x)
Definition: dsp.c:382
char * sep
Definition: dsp.c:388
static void relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, uint32_t prio)
Definition: dsp.c:217
static d_close_t dsp_close
Definition: dsp.c:78
static int dsp_oss_syncstart(int sg_id)
Launch a sync group into action.
Definition: dsp.c:2973
SND_DECLARE_FILE("$FreeBSD$")
static d_open_t dsp_open
Definition: dsp.c:77
uint32_t spd
Definition: dsp.c:394
static void dsp_set_flags(struct cdev *dev, uint32_t flags)
Definition: dsp.c:135
int volctl
Definition: dsp.c:393
static int dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd, caddr_t arg)
Definition: dsp.c:962
int query
Definition: dsp.c:395
SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL)
int hw
Definition: dsp.c:391
char * alias
Definition: dsp.c:389
#define PCM_VOLCH(x)
Definition: dsp.c:67
@ DSP_CDEV_VOLCTL_NONE
Definition: dsp.c:374
@ DSP_CDEV_VOLCTL_READ
Definition: dsp.c:375
@ DSP_CDEV_VOLCTL_WRITE
Definition: dsp.c:376
static d_mmap_single_t dsp_mmap_single
Definition: dsp.c:84
static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
Handler for SNDCTL_DSP_POLICY.
Definition: dsp.c:3095
#define DSP_REGISTERED(x, y)
Definition: dsp.c:72
int use_sep
Definition: dsp.c:390
#define THE_REAL_SNDCTL_DSP_GETBLKSIZE
static int dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
Definition: dsp.c:907
static d_ioctl_t dsp_ioctl
Definition: dsp.c:81
static __inline int dsp_io_ops(struct cdev *i_dev, struct uio *buf)
Definition: dsp.c:819
static d_read_t dsp_read
Definition: dsp.c:79
int type
Definition: dsp.c:386
#define DSP_CDEVINFO_CACHESIZE
Definition: dsp.c:70
static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
Specify channel interleaving order.
Definition: dsp.c:3231
static const struct @35 dsp_cdevs[]
char * dsp_unit2name(char *buf, size_t len, int unit)
Definition: dsp.c:2534
static d_poll_t dsp_poll
Definition: dsp.c:82
static void dsp_cdevinfo_free(struct cdev *dev)
Definition: dsp.c:275
uint32_t fmt
Definition: dsp.c:394
int max
Definition: dsp.c:392
#define DSP_FIXUP_ERROR()
Definition: dsp.c:434
#define PCM_SIMPLEX(x)
Definition: dsp.c:68
int dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
Handler for SNDCTL_AUDIOINFO.
Definition: dsp.c:2587
#define DSP_F_WRITE(x)
Definition: dsp.c:383
#define PCM_WRCH(x)
Definition: dsp.c:66
#define DSP_F_DUPLEX(x)
Definition: dsp.c:380
static void dsp_sysinit(void *p)
Definition: dsp.c:2510
static int dsp_umax
Definition: dsp.c:100
uint16_t len
unsigned right
Definition: es137x.c:261
unsigned left
Definition: es137x.c:260
@ FEEDER_VOLUME
Definition: feeder.h:87
struct pcmchan_matrix * feeder_matrix_default_channel_map(uint32_t)
static u_int32_t fmts[]
Definition: fm801.c:112
uint8_t mask
Definition: hdac.c:212
bus_addr_t buf
Definition: hdac_if.m:63
uint8_t size
struct @109 error
uint8_t chan
#define SND_CHN_T_FL
Definition: matrix.h:42
#define SND_CHN_T_FR
Definition: matrix.h:43
#define SND_CHN_MAX
Definition: matrix.h:183
#define SND_VOL_C_PCM
Definition: matrix.h:195
int mixer_ioctl_cmd(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td, int from)
Definition: mixer.c:1278
u_int32_t mix_getdevs(struct snd_mixer *m)
Definition: mixer.c:627
int mixer_oss_mixerinfo(struct cdev *i_dev, oss_mixerinfo *mi)
Handler for SNDCTL_MIXERINFO.
Definition: mixer.c:1439
#define MIXER_CMD_DIRECT
Definition: mixer.h:76
unsigned dev
Definition: mixer_if.m:59
#define RANGE(var, low, high)
Definition: sequencer.h:45
int snd_maxautovchans
Definition: sound.c:59
void pcm_setflags(device_t dev, uint32_t val)
Definition: sound.c:824
int sound_oss_card_info(oss_card_info *si)
Definition: sound.c:1389
uint32_t pcm_getflags(device_t dev)
Definition: sound.c:816
struct unrhdr * pcmsg_unrhdr
Unit number allocator for syncgroup IDs.
Definition: sound.c:78
devclass_t pcm_devclass
Definition: sound.c:49
void sound_oss_sysinfo(oss_sysinfo *si)
Handle OSSv4 SNDCTL_SYSINFO ioctl.
Definition: sound.c:1296
int snd_unit
Definition: sound.c:53
int pcm_chnalloc(struct snddev_info *d, struct pcm_channel **ch, int direction, pid_t pid, char *comm, int devunit)
Definition: sound.c:273
int pcm_chnref(struct pcm_channel *c, int ref)
Definition: sound.c:387
int pcm_chnrelease(struct pcm_channel *c)
Definition: sound.c:373
#define AFMT_ENCODING(v)
Definition: sound.h:222
#define SND_DEV_DSP
Definition: sound.h:264
#define PCM_GIANT_LEAVE(x)
Definition: sound.h:612
#define PCMMAXUNIT
Definition: sound.h:113
#define SND_FORMAT(f, c, e)
Definition: sound.h:238
#define PCM_RELEASE_QUICK(x)
Definition: sound.h:573
#define SND_MAXHWCHAN
Definition: sound.h:129
#define SD_F_PRIO_RD
Definition: sound.h:151
#define SD_F_PRIO_WR
Definition: sound.h:152
#define SND_DEV_DSP16
Definition: sound.h:266
#define PCMMAXCHAN
Definition: sound.h:115
#define PCMDEV(x)
Definition: sound.h:120
#define PCMMINOR(x)
Definition: sound.h:124
#define PCM_RELEASE(x)
Definition: sound.h:554
#define PCM_LOCKASSERT(d)
Definition: sound.h:423
#define SND_DEV_DSPHW_CD
Definition: sound.h:279
#define AFMT_24BIT
Definition: sound.h:190
#define PCM_DETACHING(x)
Definition: sound.h:181
#define SND_DEV_DSPHW_VREC
Definition: sound.h:277
#define PCM_ACQUIRE(x)
Definition: sound.h:546
#define PCMUNIT(x)
Definition: sound.h:119
#define SND_DEV_AUDIO
Definition: sound.h:265
#define DSP_DEFAULT_SPEED
Definition: sound.h:295
#define PCM_BUSYASSERT(x)
Definition: sound.h:580
#define PCM_WAIT(x)
Definition: sound.h:540
#define SND_MAXVCHANS
Definition: sound.h:130
#define SD_F_BITPERFECT
Definition: sound.h:140
#define PCM_UNLOCK(d)
Definition: sound.h:421
#define PCM_GIANT_EXIT(x)
Definition: sound.h:595
#define AFMT_CHANNEL(v)
Definition: sound.h:227
#define SND_DEV_DSPHW_REC
Definition: sound.h:276
#define SD_F_SIMPLEX
Definition: sound.h:132
#define AFMT_8BIT
Definition: sound.h:193
#define PCM_LOCK(d)
Definition: sound.h:420
#define SND_DEV_DSPHW_VPLAY
Definition: sound.h:275
#define PCM_ACQUIRE_QUICK(x)
Definition: sound.h:565
#define SND_DEV_DSPHW_PLAY
Definition: sound.h:274
#define AFMT_EXTCHANNEL(v)
Definition: sound.h:224
#define PCM_UNLOCKASSERT(d)
Definition: sound.h:424
#define AFMT_32BIT
Definition: sound.h:189
#define PCM_GIANT_ENTER(x)
Definition: sound.h:586
#define PCM_REGISTERED(x)
Definition: sound.h:178
#define SD_F_VPC
Definition: sound.h:141
#define AFMT_16BIT
Definition: sound.h:191
int simplex
Definition: dsp.c:61
struct pcm_channel * volch
Definition: dsp.c:60
struct pcm_channel * rdch
Definition: dsp.c:59
int busy
Definition: dsp.c:61
struct pcm_channel * wrch
Definition: dsp.c:59
struct pcmchan_syncmember * sm
Definition: channel.h:140
char name[CHN_NAMELEN]
Definition: channel.h:109
u_int32_t flags
Definition: channel.h:96
struct pcm_channel::@28 channels
u_int64_t blocks
Definition: channel.h:98
u_int32_t feederflags
Definition: channel.h:97
u_int32_t speed
Definition: channel.h:94
unsigned int lw
Definition: channel.h:135
pid_t pid
Definition: channel.h:88
int direction
Definition: channel.h:100
unsigned int xruns
Definition: channel.h:101
u_int32_t format
Definition: channel.h:95
struct mtx * lock
Definition: channel.h:111
unsigned int inprog
Definition: channel.h:121
struct snd_dbuf * bufsoft
Definition: channel.h:103
int unit
Definition: channel.h:108
struct cv cv
Definition: channel.h:127
u_int32_t maxspeed
Definition: channel.h:34
u_int32_t * fmtlist
Definition: channel.h:35
u_int32_t caps
Definition: channel.h:36
u_int32_t minspeed
Definition: channel.h:34
uint8_t ext
Definition: channel.h:41
Specifies an audio device sync group.
Definition: channel.h:66
Specifies a container for members of a sync group.
Definition: channel.h:75
struct pcm_channel * ch
Definition: channel.h:78
struct pcmchan_syncgroup * parent
Definition: channel.h:77
Definition: clone.c:71
u_int8_t * shadbuf
Definition: buffer.h:44
volatile int sl
Definition: buffer.h:45
unsigned rvchancount
Definition: sound.h:395
struct cdev * mixer_dev
Definition: sound.h:403
struct snd_clone * clones
Definition: sound.h:394
unsigned flags
Definition: sound.h:396
unsigned pvchancount
Definition: sound.h:395
struct snddev_info::@49 channels
device_t dev
Definition: sound.h:400
int snd_u2unit(int u)
Definition: unit.c:132
int snd_unit2d(int unit)
Definition: unit.c:116
void snd_unit_init(void)
Definition: unit.c:168
int snd_d2unit(int d)
Definition: unit.c:140
int snd_unit2u(int unit)
Definition: unit.c:108
int snd_c2unit(int c)
Definition: unit.c:148
int snd_unit2c(int unit)
Definition: unit.c:124
uint16_t offset