1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
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 * Copyright (c) 2024 The FreeBSD Foundation
9 *
10 * Portions of this software were developed by Christos Margiolis
11 * <christos@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #ifdef HAVE_KERNEL_OPTION_HEADERS
36 #include "opt_snd.h"
37 #endif
38
39 #include <dev/sound/pcm/sound.h>
40 #include <sys/ctype.h>
41 #include <sys/lock.h>
42 #include <sys/rwlock.h>
43 #include <sys/sysent.h>
44
45 #include <vm/vm.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_page.h>
48 #include <vm/vm_pager.h>
49
50 struct dsp_cdevpriv {
51 struct snddev_info *sc;
52 struct pcm_channel *rdch;
53 struct pcm_channel *wrch;
54 struct pcm_channel *volch;
55 int simplex;
56 };
57
58 static int dsp_mmap_allow_prot_exec = 0;
59 SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RWTUN,
60 &dsp_mmap_allow_prot_exec, 0,
61 "linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
62
63 static int dsp_basename_clone = 1;
64 SYSCTL_INT(_hw_snd, OID_AUTO, basename_clone, CTLFLAG_RWTUN,
65 &dsp_basename_clone, 0,
66 "DSP basename cloning (0: Disable; 1: Enabled)");
67
68 #define DSP_REGISTERED(x) (PCM_REGISTERED(x) && (x)->dsp_dev != NULL)
69
70 #define OLDPCM_IOCTL
71
72 static d_open_t dsp_open;
73 static d_read_t dsp_read;
74 static d_write_t dsp_write;
75 static d_ioctl_t dsp_ioctl;
76 static d_poll_t dsp_poll;
77 static d_mmap_t dsp_mmap;
78 static d_mmap_single_t dsp_mmap_single;
79
80 struct cdevsw dsp_cdevsw = {
81 .d_version = D_VERSION,
82 .d_open = dsp_open,
83 .d_read = dsp_read,
84 .d_write = dsp_write,
85 .d_ioctl = dsp_ioctl,
86 .d_poll = dsp_poll,
87 .d_mmap = dsp_mmap,
88 .d_mmap_single = dsp_mmap_single,
89 .d_name = "dsp",
90 };
91
92 static eventhandler_tag dsp_ehtag = NULL;
93
94 static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
95 static int dsp_oss_syncstart(int sg_id);
96 static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
97 static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
98 static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
99 static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
100 static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
101 #ifdef OSSV4_EXPERIMENT
102 static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
103 static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
104 static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
105 static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
106 static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
107 #endif
108
109 int
dsp_make_dev(device_t dev)110 dsp_make_dev(device_t dev)
111 {
112 struct make_dev_args devargs;
113 struct snddev_info *sc;
114 int err, unit;
115
116 sc = device_get_softc(dev);
117 unit = device_get_unit(dev);
118
119 make_dev_args_init(&devargs);
120 devargs.mda_devsw = &dsp_cdevsw;
121 devargs.mda_uid = UID_ROOT;
122 devargs.mda_gid = GID_WHEEL;
123 devargs.mda_mode = 0666;
124 devargs.mda_si_drv1 = sc;
125 err = make_dev_s(&devargs, &sc->dsp_dev, "dsp%d", unit);
126 if (err != 0) {
127 device_printf(dev, "failed to create dsp%d: error %d",
128 unit, err);
129 return (ENXIO);
130 }
131
132 return (0);
133 }
134
135 void
dsp_destroy_dev(device_t dev)136 dsp_destroy_dev(device_t dev)
137 {
138 struct snddev_info *d;
139
140 d = device_get_softc(dev);
141 destroy_dev_sched(d->dsp_dev);
142 }
143
144 static void
getchns(struct dsp_cdevpriv * priv,uint32_t prio)145 getchns(struct dsp_cdevpriv *priv, uint32_t prio)
146 {
147 struct snddev_info *d;
148 struct pcm_channel *ch;
149 uint32_t flags;
150
151 if (priv->simplex) {
152 d = priv->sc;
153 if (!PCM_REGISTERED(d))
154 return;
155 PCM_LOCK(d);
156 PCM_WAIT(d);
157 PCM_ACQUIRE(d);
158 /*
159 * Note: order is important -
160 * pcm flags -> prio query flags -> wild guess
161 */
162 ch = NULL;
163 flags = pcm_getflags(d->dev);
164 if (flags & SD_F_PRIO_WR) {
165 ch = priv->rdch;
166 } else if (flags & SD_F_PRIO_RD) {
167 ch = priv->wrch;
168 } else if (prio & SD_F_PRIO_WR) {
169 ch = priv->rdch;
170 flags |= SD_F_PRIO_WR;
171 } else if (prio & SD_F_PRIO_RD) {
172 ch = priv->wrch;
173 flags |= SD_F_PRIO_RD;
174 } else if (priv->wrch != NULL) {
175 ch = priv->rdch;
176 flags |= SD_F_PRIO_WR;
177 } else if (priv->rdch != NULL) {
178 ch = priv->wrch;
179 flags |= SD_F_PRIO_RD;
180 }
181 pcm_setflags(d->dev, flags);
182 if (ch != NULL) {
183 CHN_LOCK(ch);
184 chn_ref(ch, -1);
185 chn_release(ch);
186 }
187 PCM_RELEASE(d);
188 PCM_UNLOCK(d);
189 }
190
191 if (priv->rdch != NULL && (prio & SD_F_PRIO_RD))
192 CHN_LOCK(priv->rdch);
193 if (priv->wrch != NULL && (prio & SD_F_PRIO_WR))
194 CHN_LOCK(priv->wrch);
195 }
196
197 static void
relchns(struct dsp_cdevpriv * priv,uint32_t prio)198 relchns(struct dsp_cdevpriv *priv, uint32_t prio)
199 {
200 if (priv->rdch != NULL && (prio & SD_F_PRIO_RD))
201 CHN_UNLOCK(priv->rdch);
202 if (priv->wrch != NULL && (prio & SD_F_PRIO_WR))
203 CHN_UNLOCK(priv->wrch);
204 }
205
206 #define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
207 #define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
208 #define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
209 #define DSP_F_READ(x) ((x) & FREAD)
210 #define DSP_F_WRITE(x) ((x) & FWRITE)
211
212 static const struct {
213 int type;
214 char *name;
215 char *sep;
216 char *alias;
217 } dsp_cdevs[] = {
218 { SND_DEV_DSP, "dsp", ".", NULL },
219 { SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL },
220 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL },
221 { SND_DEV_DSPHW_REC, "dsp", ".r", NULL },
222 { SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL },
223 /* Low priority, OSSv4 aliases. */
224 { SND_DEV_DSP, "dsp_ac3", ".", "dsp" },
225 { SND_DEV_DSP, "dsp_mmap", ".", "dsp" },
226 { SND_DEV_DSP, "dsp_multich", ".", "dsp" },
227 { SND_DEV_DSP, "dsp_spdifout", ".", "dsp" },
228 { SND_DEV_DSP, "dsp_spdifin", ".", "dsp" },
229 };
230
231 static void
dsp_close(void * data)232 dsp_close(void *data)
233 {
234 struct dsp_cdevpriv *priv = data;
235 struct pcm_channel *rdch, *wrch, *volch;
236 struct snddev_info *d;
237 int sg_ids, rdref, wdref;
238
239 if (priv == NULL)
240 return;
241
242 d = priv->sc;
243 /* At this point pcm_unregister() will destroy all channels anyway. */
244 if (PCM_DETACHING(d))
245 goto skip;
246
247 PCM_GIANT_ENTER(d);
248
249 PCM_LOCK(d);
250 PCM_WAIT(d);
251 PCM_ACQUIRE(d);
252
253 rdch = priv->rdch;
254 wrch = priv->wrch;
255 volch = priv->volch;
256
257 rdref = -1;
258 wdref = -1;
259
260 if (volch != NULL) {
261 if (volch == rdch)
262 rdref--;
263 else if (volch == wrch)
264 wdref--;
265 else {
266 CHN_LOCK(volch);
267 chn_ref(volch, -1);
268 CHN_UNLOCK(volch);
269 }
270 }
271
272 if (rdch != NULL)
273 CHN_REMOVE(d, rdch, channels.pcm.opened);
274 if (wrch != NULL)
275 CHN_REMOVE(d, wrch, channels.pcm.opened);
276
277 if (rdch != NULL || wrch != NULL) {
278 PCM_UNLOCK(d);
279 if (rdch != NULL) {
280 /*
281 * The channel itself need not be locked because:
282 * a) Adding a channel to a syncgroup happens only
283 * in dsp_ioctl(), which cannot run concurrently
284 * to dsp_close().
285 * b) The syncmember pointer (sm) is protected by
286 * the global syncgroup list lock.
287 * c) A channel can't just disappear, invalidating
288 * pointers, unless it's closed/dereferenced
289 * first.
290 */
291 PCM_SG_LOCK();
292 sg_ids = chn_syncdestroy(rdch);
293 PCM_SG_UNLOCK();
294 if (sg_ids != 0)
295 free_unr(pcmsg_unrhdr, sg_ids);
296
297 CHN_LOCK(rdch);
298 chn_ref(rdch, rdref);
299 chn_abort(rdch); /* won't sleep */
300 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
301 CHN_F_DEAD | CHN_F_EXCLUSIVE);
302 chn_reset(rdch, 0, 0);
303 chn_release(rdch);
304 }
305 if (wrch != NULL) {
306 /*
307 * Please see block above.
308 */
309 PCM_SG_LOCK();
310 sg_ids = chn_syncdestroy(wrch);
311 PCM_SG_UNLOCK();
312 if (sg_ids != 0)
313 free_unr(pcmsg_unrhdr, sg_ids);
314
315 CHN_LOCK(wrch);
316 chn_ref(wrch, wdref);
317 chn_flush(wrch); /* may sleep */
318 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
319 CHN_F_DEAD | CHN_F_EXCLUSIVE);
320 chn_reset(wrch, 0, 0);
321 chn_release(wrch);
322 }
323 PCM_LOCK(d);
324 }
325
326 PCM_RELEASE(d);
327 PCM_UNLOCK(d);
328
329 PCM_GIANT_LEAVE(d);
330 skip:
331 free(priv, M_DEVBUF);
332 priv = NULL;
333 }
334
335 #define DSP_FIXUP_ERROR() do { \
336 prio = pcm_getflags(d->dev); \
337 if (!DSP_F_VALID(flags)) \
338 error = EINVAL; \
339 if (!DSP_F_DUPLEX(flags) && \
340 ((DSP_F_READ(flags) && d->reccount == 0) || \
341 (DSP_F_WRITE(flags) && d->playcount == 0))) \
342 error = ENOTSUP; \
343 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
344 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
345 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
346 error = EBUSY; \
347 } while (0)
348
349 static int
dsp_open(struct cdev * i_dev,int flags,int mode,struct thread * td)350 dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
351 {
352 struct dsp_cdevpriv *priv;
353 struct pcm_channel *rdch, *wrch;
354 struct snddev_info *d;
355 uint32_t fmt, spd, prio;
356 int error, rderror, wrerror;
357
358 /* Kind of impossible.. */
359 if (i_dev == NULL || td == NULL)
360 return (ENODEV);
361
362 d = i_dev->si_drv1;
363 if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
364 return (EBADF);
365
366 priv = malloc(sizeof(*priv), M_DEVBUF, M_WAITOK | M_ZERO);
367 priv->sc = d;
368 priv->rdch = NULL;
369 priv->wrch = NULL;
370 priv->volch = NULL;
371 priv->simplex = (pcm_getflags(d->dev) & SD_F_SIMPLEX) ? 1 : 0;
372
373 error = devfs_set_cdevpriv(priv, dsp_close);
374 if (error != 0)
375 return (error);
376
377 PCM_GIANT_ENTER(d);
378
379 /* Lock snddev so nobody else can monkey with it. */
380 PCM_LOCK(d);
381 PCM_WAIT(d);
382
383 error = 0;
384 DSP_FIXUP_ERROR();
385 if (error != 0) {
386 PCM_UNLOCK(d);
387 PCM_GIANT_EXIT(d);
388 return (error);
389 }
390
391 /*
392 * That is just enough. Acquire and unlock pcm lock so
393 * the other will just have to wait until we finish doing
394 * everything.
395 */
396 PCM_ACQUIRE(d);
397 PCM_UNLOCK(d);
398
399 fmt = SND_FORMAT(AFMT_U8, 1, 0);
400 spd = DSP_DEFAULT_SPEED;
401
402 rdch = NULL;
403 wrch = NULL;
404 rderror = 0;
405 wrerror = 0;
406
407 if (DSP_F_READ(flags)) {
408 /* open for read */
409 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
410 td->td_proc->p_pid, td->td_proc->p_comm);
411
412 if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
413 rderror = ENXIO;
414
415 if (rderror != 0) {
416 if (rdch != NULL)
417 chn_release(rdch);
418 if (!DSP_F_DUPLEX(flags)) {
419 PCM_RELEASE_QUICK(d);
420 PCM_GIANT_EXIT(d);
421 return (rderror);
422 }
423 rdch = NULL;
424 } else {
425 if (flags & O_NONBLOCK)
426 rdch->flags |= CHN_F_NBIO;
427 if (flags & O_EXCL)
428 rdch->flags |= CHN_F_EXCLUSIVE;
429 chn_ref(rdch, 1);
430 chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
431 CHN_UNLOCK(rdch);
432 }
433 }
434
435 if (DSP_F_WRITE(flags)) {
436 /* open for write */
437 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
438 td->td_proc->p_pid, td->td_proc->p_comm);
439
440 if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
441 wrerror = ENXIO;
442
443 if (wrerror != 0) {
444 if (wrch != NULL)
445 chn_release(wrch);
446 if (!DSP_F_DUPLEX(flags)) {
447 if (rdch != NULL) {
448 /*
449 * Lock, deref and release previously
450 * created record channel
451 */
452 CHN_LOCK(rdch);
453 chn_ref(rdch, -1);
454 chn_release(rdch);
455 }
456 PCM_RELEASE_QUICK(d);
457 PCM_GIANT_EXIT(d);
458 return (wrerror);
459 }
460 wrch = NULL;
461 } else {
462 if (flags & O_NONBLOCK)
463 wrch->flags |= CHN_F_NBIO;
464 if (flags & O_EXCL)
465 wrch->flags |= CHN_F_EXCLUSIVE;
466 chn_ref(wrch, 1);
467 chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
468 CHN_UNLOCK(wrch);
469 }
470 }
471
472 PCM_LOCK(d);
473
474 if (wrch == NULL && rdch == NULL) {
475 PCM_RELEASE(d);
476 PCM_UNLOCK(d);
477 PCM_GIANT_EXIT(d);
478 if (wrerror != 0)
479 return (wrerror);
480 if (rderror != 0)
481 return (rderror);
482 return (EINVAL);
483 }
484 if (rdch != NULL)
485 CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
486 if (wrch != NULL)
487 CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
488 priv->rdch = rdch;
489 priv->wrch = wrch;
490
491 PCM_RELEASE(d);
492 PCM_UNLOCK(d);
493
494 PCM_GIANT_LEAVE(d);
495
496 return (0);
497 }
498
499 static __inline int
dsp_io_ops(struct dsp_cdevpriv * priv,struct uio * buf)500 dsp_io_ops(struct dsp_cdevpriv *priv, struct uio *buf)
501 {
502 struct snddev_info *d;
503 struct pcm_channel **ch;
504 int (*chn_io)(struct pcm_channel *, struct uio *);
505 int prio, ret;
506 pid_t runpid;
507
508 KASSERT(buf != NULL &&
509 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
510 ("%s(): io train wreck!", __func__));
511
512 d = priv->sc;
513 if (PCM_DETACHING(d) || !DSP_REGISTERED(d))
514 return (EBADF);
515
516 PCM_GIANT_ENTER(d);
517
518 switch (buf->uio_rw) {
519 case UIO_READ:
520 prio = SD_F_PRIO_RD;
521 ch = &priv->rdch;
522 chn_io = chn_read;
523 break;
524 case UIO_WRITE:
525 prio = SD_F_PRIO_WR;
526 ch = &priv->wrch;
527 chn_io = chn_write;
528 break;
529 default:
530 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
531 break;
532 }
533
534 runpid = buf->uio_td->td_proc->p_pid;
535
536 getchns(priv, prio);
537
538 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
539 if (priv->rdch != NULL || priv->wrch != NULL)
540 relchns(priv, prio);
541 PCM_GIANT_EXIT(d);
542 return (EBADF);
543 }
544
545 if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
546 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
547 relchns(priv, prio);
548 PCM_GIANT_EXIT(d);
549 return (EINVAL);
550 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
551 (*ch)->flags |= CHN_F_RUNNING;
552 (*ch)->pid = runpid;
553 }
554
555 /*
556 * chn_read/write must give up channel lock in order to copy bytes
557 * from/to userland, so up the "in progress" counter to make sure
558 * someone else doesn't come along and muss up the buffer.
559 */
560 ++(*ch)->inprog;
561 ret = chn_io(*ch, buf);
562 --(*ch)->inprog;
563
564 CHN_BROADCAST(&(*ch)->cv);
565
566 relchns(priv, prio);
567
568 PCM_GIANT_LEAVE(d);
569
570 return (ret);
571 }
572
573 static int
dsp_read(struct cdev * i_dev,struct uio * buf,int flag)574 dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
575 {
576 struct dsp_cdevpriv *priv;
577 int err;
578
579 if ((err = devfs_get_cdevpriv((void **)&priv)) != 0)
580 return (err);
581 return (dsp_io_ops(priv, buf));
582 }
583
584 static int
dsp_write(struct cdev * i_dev,struct uio * buf,int flag)585 dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
586 {
587 struct dsp_cdevpriv *priv;
588 int err;
589
590 if ((err = devfs_get_cdevpriv((void **)&priv)) != 0)
591 return (err);
592 return (dsp_io_ops(priv, buf));
593 }
594
595 static int
dsp_ioctl_channel(struct dsp_cdevpriv * priv,struct pcm_channel * volch,u_long cmd,caddr_t arg)596 dsp_ioctl_channel(struct dsp_cdevpriv *priv, struct pcm_channel *volch,
597 u_long cmd, caddr_t arg)
598 {
599 struct snddev_info *d;
600 struct pcm_channel *rdch, *wrch;
601 int j, left, right, center, mute;
602
603 d = priv->sc;
604 if (!PCM_REGISTERED(d) || !(pcm_getflags(d->dev) & SD_F_VPC))
605 return (-1);
606
607 PCM_UNLOCKASSERT(d);
608
609 j = cmd & 0xff;
610
611 rdch = priv->rdch;
612 wrch = priv->wrch;
613
614 /* No specific channel, look into cache */
615 if (volch == NULL)
616 volch = priv->volch;
617
618 /* Look harder */
619 if (volch == NULL) {
620 if (j == SOUND_MIXER_RECLEV && rdch != NULL)
621 volch = rdch;
622 else if (j == SOUND_MIXER_PCM && wrch != NULL)
623 volch = wrch;
624 }
625
626 /* Final validation */
627 if (volch == NULL)
628 return (EINVAL);
629
630 CHN_LOCK(volch);
631 if (!(volch->feederflags & (1 << FEEDER_VOLUME))) {
632 CHN_UNLOCK(volch);
633 return (EINVAL);
634 }
635
636 switch (cmd & ~0xff) {
637 case MIXER_WRITE(0):
638 switch (j) {
639 case SOUND_MIXER_MUTE:
640 if (volch->direction == PCMDIR_REC) {
641 chn_setmute_multi(volch, SND_VOL_C_PCM, (*(int *)arg & SOUND_MASK_RECLEV) != 0);
642 } else {
643 chn_setmute_multi(volch, SND_VOL_C_PCM, (*(int *)arg & SOUND_MASK_PCM) != 0);
644 }
645 break;
646 case SOUND_MIXER_PCM:
647 if (volch->direction != PCMDIR_PLAY)
648 break;
649 left = *(int *)arg & 0x7f;
650 right = ((*(int *)arg) >> 8) & 0x7f;
651 center = (left + right) >> 1;
652 chn_setvolume_multi(volch, SND_VOL_C_PCM,
653 left, right, center);
654 break;
655 case SOUND_MIXER_RECLEV:
656 if (volch->direction != PCMDIR_REC)
657 break;
658 left = *(int *)arg & 0x7f;
659 right = ((*(int *)arg) >> 8) & 0x7f;
660 center = (left + right) >> 1;
661 chn_setvolume_multi(volch, SND_VOL_C_PCM,
662 left, right, center);
663 break;
664 default:
665 /* ignore all other mixer writes */
666 break;
667 }
668 break;
669
670 case MIXER_READ(0):
671 switch (j) {
672 case SOUND_MIXER_MUTE:
673 mute = CHN_GETMUTE(volch, SND_VOL_C_PCM, SND_CHN_T_FL) ||
674 CHN_GETMUTE(volch, SND_VOL_C_PCM, SND_CHN_T_FR);
675 if (volch->direction == PCMDIR_REC) {
676 *(int *)arg = mute << SOUND_MIXER_RECLEV;
677 } else {
678 *(int *)arg = mute << SOUND_MIXER_PCM;
679 }
680 break;
681 case SOUND_MIXER_PCM:
682 if (volch->direction != PCMDIR_PLAY)
683 break;
684 *(int *)arg = CHN_GETVOLUME(volch,
685 SND_VOL_C_PCM, SND_CHN_T_FL);
686 *(int *)arg |= CHN_GETVOLUME(volch,
687 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
688 break;
689 case SOUND_MIXER_RECLEV:
690 if (volch->direction != PCMDIR_REC)
691 break;
692 *(int *)arg = CHN_GETVOLUME(volch,
693 SND_VOL_C_PCM, SND_CHN_T_FL);
694 *(int *)arg |= CHN_GETVOLUME(volch,
695 SND_VOL_C_PCM, SND_CHN_T_FR) << 8;
696 break;
697 case SOUND_MIXER_DEVMASK:
698 case SOUND_MIXER_CAPS:
699 case SOUND_MIXER_STEREODEVS:
700 if (volch->direction == PCMDIR_REC)
701 *(int *)arg = SOUND_MASK_RECLEV;
702 else
703 *(int *)arg = SOUND_MASK_PCM;
704 break;
705 default:
706 *(int *)arg = 0;
707 break;
708 }
709 break;
710
711 default:
712 break;
713 }
714 CHN_UNLOCK(volch);
715 return (0);
716 }
717
718 static int
dsp_ioctl(struct cdev * i_dev,u_long cmd,caddr_t arg,int mode,struct thread * td)719 dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode,
720 struct thread *td)
721 {
722 struct dsp_cdevpriv *priv;
723 struct pcm_channel *chn, *rdch, *wrch;
724 struct snddev_info *d;
725 u_long xcmd;
726 int *arg_i, ret, tmp, err;
727
728 if ((err = devfs_get_cdevpriv((void **)&priv)) != 0)
729 return (err);
730
731 d = priv->sc;
732 if (PCM_DETACHING(d) || !DSP_REGISTERED(d))
733 return (EBADF);
734
735 PCM_GIANT_ENTER(d);
736
737 arg_i = (int *)arg;
738 ret = 0;
739 xcmd = 0;
740 chn = NULL;
741
742 if (IOCGROUP(cmd) == 'M') {
743 if (cmd == OSS_GETVERSION) {
744 *arg_i = SOUND_VERSION;
745 PCM_GIANT_EXIT(d);
746 return (0);
747 }
748 ret = dsp_ioctl_channel(priv, priv->volch, cmd, arg);
749 if (ret != -1) {
750 PCM_GIANT_EXIT(d);
751 return (ret);
752 }
753
754 if (d->mixer_dev != NULL) {
755 PCM_ACQUIRE_QUICK(d);
756 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
757 MIXER_CMD_DIRECT);
758 PCM_RELEASE_QUICK(d);
759 } else
760 ret = EBADF;
761
762 PCM_GIANT_EXIT(d);
763
764 return (ret);
765 }
766
767 /*
768 * Certain ioctls may be made on any type of device (audio, mixer,
769 * and MIDI). Handle those special cases here.
770 */
771 if (IOCGROUP(cmd) == 'X') {
772 PCM_ACQUIRE_QUICK(d);
773 switch(cmd) {
774 case SNDCTL_SYSINFO:
775 sound_oss_sysinfo((oss_sysinfo *)arg);
776 break;
777 case SNDCTL_CARDINFO:
778 ret = sound_oss_card_info((oss_card_info *)arg);
779 break;
780 case SNDCTL_AUDIOINFO:
781 case SNDCTL_AUDIOINFO_EX:
782 case SNDCTL_ENGINEINFO:
783 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
784 break;
785 case SNDCTL_MIXERINFO:
786 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
787 break;
788 default:
789 ret = EINVAL;
790 }
791 PCM_RELEASE_QUICK(d);
792 PCM_GIANT_EXIT(d);
793 return (ret);
794 }
795
796 getchns(priv, 0);
797 rdch = priv->rdch;
798 wrch = priv->wrch;
799
800 if (wrch != NULL && (wrch->flags & CHN_F_DEAD))
801 wrch = NULL;
802 if (rdch != NULL && (rdch->flags & CHN_F_DEAD))
803 rdch = NULL;
804
805 if (wrch == NULL && rdch == NULL) {
806 PCM_GIANT_EXIT(d);
807 return (EINVAL);
808 }
809
810 switch(cmd) {
811 #ifdef OLDPCM_IOCTL
812 /*
813 * we start with the new ioctl interface.
814 */
815 case AIONWRITE: /* how many bytes can write ? */
816 if (wrch) {
817 CHN_LOCK(wrch);
818 /*
819 if (wrch && wrch->bufhard.dl)
820 while (chn_wrfeed(wrch) == 0);
821 */
822 *arg_i = sndbuf_getfree(wrch->bufsoft);
823 CHN_UNLOCK(wrch);
824 } else {
825 *arg_i = 0;
826 ret = EINVAL;
827 }
828 break;
829
830 case AIOSSIZE: /* set the current blocksize */
831 {
832 struct snd_size *p = (struct snd_size *)arg;
833
834 p->play_size = 0;
835 p->rec_size = 0;
836 PCM_ACQUIRE_QUICK(d);
837 if (wrch) {
838 CHN_LOCK(wrch);
839 chn_setblocksize(wrch, 2, p->play_size);
840 p->play_size = sndbuf_getblksz(wrch->bufsoft);
841 CHN_UNLOCK(wrch);
842 }
843 if (rdch) {
844 CHN_LOCK(rdch);
845 chn_setblocksize(rdch, 2, p->rec_size);
846 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
847 CHN_UNLOCK(rdch);
848 }
849 PCM_RELEASE_QUICK(d);
850 }
851 break;
852 case AIOGSIZE: /* get the current blocksize */
853 {
854 struct snd_size *p = (struct snd_size *)arg;
855
856 if (wrch) {
857 CHN_LOCK(wrch);
858 p->play_size = sndbuf_getblksz(wrch->bufsoft);
859 CHN_UNLOCK(wrch);
860 }
861 if (rdch) {
862 CHN_LOCK(rdch);
863 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
864 CHN_UNLOCK(rdch);
865 }
866 }
867 break;
868
869 case AIOSFMT:
870 case AIOGFMT:
871 {
872 snd_chan_param *p = (snd_chan_param *)arg;
873
874 if (cmd == AIOSFMT &&
875 ((p->play_format != 0 && p->play_rate == 0) ||
876 (p->rec_format != 0 && p->rec_rate == 0))) {
877 ret = EINVAL;
878 break;
879 }
880 PCM_ACQUIRE_QUICK(d);
881 if (wrch) {
882 CHN_LOCK(wrch);
883 if (cmd == AIOSFMT && p->play_format != 0) {
884 chn_setformat(wrch,
885 SND_FORMAT(p->play_format,
886 AFMT_CHANNEL(wrch->format),
887 AFMT_EXTCHANNEL(wrch->format)));
888 chn_setspeed(wrch, p->play_rate);
889 }
890 p->play_rate = wrch->speed;
891 p->play_format = AFMT_ENCODING(wrch->format);
892 CHN_UNLOCK(wrch);
893 } else {
894 p->play_rate = 0;
895 p->play_format = 0;
896 }
897 if (rdch) {
898 CHN_LOCK(rdch);
899 if (cmd == AIOSFMT && p->rec_format != 0) {
900 chn_setformat(rdch,
901 SND_FORMAT(p->rec_format,
902 AFMT_CHANNEL(rdch->format),
903 AFMT_EXTCHANNEL(rdch->format)));
904 chn_setspeed(rdch, p->rec_rate);
905 }
906 p->rec_rate = rdch->speed;
907 p->rec_format = AFMT_ENCODING(rdch->format);
908 CHN_UNLOCK(rdch);
909 } else {
910 p->rec_rate = 0;
911 p->rec_format = 0;
912 }
913 PCM_RELEASE_QUICK(d);
914 }
915 break;
916
917 case AIOGCAP: /* get capabilities */
918 {
919 snd_capabilities *p = (snd_capabilities *)arg;
920 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
921 struct cdev *pdev;
922
923 PCM_LOCK(d);
924 if (rdch) {
925 CHN_LOCK(rdch);
926 rcaps = chn_getcaps(rdch);
927 }
928 if (wrch) {
929 CHN_LOCK(wrch);
930 pcaps = chn_getcaps(wrch);
931 }
932 p->rate_min = max(rcaps? rcaps->minspeed : 0,
933 pcaps? pcaps->minspeed : 0);
934 p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
935 pcaps? pcaps->maxspeed : 1000000);
936 p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
937 wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
938 /* XXX bad on sb16 */
939 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
940 (wrch? chn_getformats(wrch) : 0xffffffff);
941 if (rdch && wrch) {
942 p->formats |=
943 (pcm_getflags(d->dev) & SD_F_SIMPLEX) ? 0 :
944 AFMT_FULLDUPLEX;
945 }
946 pdev = d->mixer_dev;
947 p->mixers = 1; /* default: one mixer */
948 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
949 p->left = p->right = 100;
950 if (wrch)
951 CHN_UNLOCK(wrch);
952 if (rdch)
953 CHN_UNLOCK(rdch);
954 PCM_UNLOCK(d);
955 }
956 break;
957
958 case AIOSTOP:
959 if (*arg_i == AIOSYNC_PLAY && wrch) {
960 CHN_LOCK(wrch);
961 *arg_i = chn_abort(wrch);
962 CHN_UNLOCK(wrch);
963 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
964 CHN_LOCK(rdch);
965 *arg_i = chn_abort(rdch);
966 CHN_UNLOCK(rdch);
967 } else {
968 printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
969 *arg_i = 0;
970 }
971 break;
972
973 case AIOSYNC:
974 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
975 ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
976 break;
977 #endif
978 /*
979 * here follow the standard ioctls (filio.h etc.)
980 */
981 case FIONREAD: /* get # bytes to read */
982 if (rdch) {
983 CHN_LOCK(rdch);
984 /* if (rdch && rdch->bufhard.dl)
985 while (chn_rdfeed(rdch) == 0);
986 */
987 *arg_i = sndbuf_getready(rdch->bufsoft);
988 CHN_UNLOCK(rdch);
989 } else {
990 *arg_i = 0;
991 ret = EINVAL;
992 }
993 break;
994
995 case FIOASYNC: /*set/clear async i/o */
996 DEB( printf("FIOASYNC\n") ; )
997 break;
998
999 case SNDCTL_DSP_NONBLOCK: /* set non-blocking i/o */
1000 case FIONBIO: /* set/clear non-blocking i/o */
1001 if (rdch) {
1002 CHN_LOCK(rdch);
1003 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1004 rdch->flags |= CHN_F_NBIO;
1005 else
1006 rdch->flags &= ~CHN_F_NBIO;
1007 CHN_UNLOCK(rdch);
1008 }
1009 if (wrch) {
1010 CHN_LOCK(wrch);
1011 if (cmd == SNDCTL_DSP_NONBLOCK || *arg_i)
1012 wrch->flags |= CHN_F_NBIO;
1013 else
1014 wrch->flags &= ~CHN_F_NBIO;
1015 CHN_UNLOCK(wrch);
1016 }
1017 break;
1018
1019 /*
1020 * Finally, here is the linux-compatible ioctl interface
1021 */
1022 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
1023 case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
1024 case SNDCTL_DSP_GETBLKSIZE:
1025 chn = wrch ? wrch : rdch;
1026 if (chn) {
1027 CHN_LOCK(chn);
1028 *arg_i = sndbuf_getblksz(chn->bufsoft);
1029 CHN_UNLOCK(chn);
1030 } else {
1031 *arg_i = 0;
1032 ret = EINVAL;
1033 }
1034 break;
1035
1036 case SNDCTL_DSP_SETBLKSIZE:
1037 RANGE(*arg_i, 16, 65536);
1038 PCM_ACQUIRE_QUICK(d);
1039 if (wrch) {
1040 CHN_LOCK(wrch);
1041 chn_setblocksize(wrch, 2, *arg_i);
1042 CHN_UNLOCK(wrch);
1043 }
1044 if (rdch) {
1045 CHN_LOCK(rdch);
1046 chn_setblocksize(rdch, 2, *arg_i);
1047 CHN_UNLOCK(rdch);
1048 }
1049 PCM_RELEASE_QUICK(d);
1050 break;
1051
1052 case SNDCTL_DSP_RESET:
1053 DEB(printf("dsp reset\n"));
1054 if (wrch) {
1055 CHN_LOCK(wrch);
1056 chn_abort(wrch);
1057 chn_resetbuf(wrch);
1058 CHN_UNLOCK(wrch);
1059 }
1060 if (rdch) {
1061 CHN_LOCK(rdch);
1062 chn_abort(rdch);
1063 chn_resetbuf(rdch);
1064 CHN_UNLOCK(rdch);
1065 }
1066 break;
1067
1068 case SNDCTL_DSP_SYNC:
1069 DEB(printf("dsp sync\n"));
1070 /* chn_sync may sleep */
1071 if (wrch) {
1072 CHN_LOCK(wrch);
1073 chn_sync(wrch, 0);
1074 CHN_UNLOCK(wrch);
1075 }
1076 break;
1077
1078 case SNDCTL_DSP_SPEED:
1079 /* chn_setspeed may sleep */
1080 tmp = 0;
1081 PCM_ACQUIRE_QUICK(d);
1082 if (wrch) {
1083 CHN_LOCK(wrch);
1084 ret = chn_setspeed(wrch, *arg_i);
1085 tmp = wrch->speed;
1086 CHN_UNLOCK(wrch);
1087 }
1088 if (rdch && ret == 0) {
1089 CHN_LOCK(rdch);
1090 ret = chn_setspeed(rdch, *arg_i);
1091 if (tmp == 0)
1092 tmp = rdch->speed;
1093 CHN_UNLOCK(rdch);
1094 }
1095 PCM_RELEASE_QUICK(d);
1096 *arg_i = tmp;
1097 break;
1098
1099 case SOUND_PCM_READ_RATE:
1100 chn = wrch ? wrch : rdch;
1101 if (chn) {
1102 CHN_LOCK(chn);
1103 *arg_i = chn->speed;
1104 CHN_UNLOCK(chn);
1105 } else {
1106 *arg_i = 0;
1107 ret = EINVAL;
1108 }
1109 break;
1110
1111 case SNDCTL_DSP_STEREO:
1112 tmp = -1;
1113 *arg_i = (*arg_i)? 2 : 1;
1114 PCM_ACQUIRE_QUICK(d);
1115 if (wrch) {
1116 CHN_LOCK(wrch);
1117 ret = chn_setformat(wrch,
1118 SND_FORMAT(wrch->format, *arg_i, 0));
1119 tmp = (AFMT_CHANNEL(wrch->format) > 1)? 1 : 0;
1120 CHN_UNLOCK(wrch);
1121 }
1122 if (rdch && ret == 0) {
1123 CHN_LOCK(rdch);
1124 ret = chn_setformat(rdch,
1125 SND_FORMAT(rdch->format, *arg_i, 0));
1126 if (tmp == -1)
1127 tmp = (AFMT_CHANNEL(rdch->format) > 1)? 1 : 0;
1128 CHN_UNLOCK(rdch);
1129 }
1130 PCM_RELEASE_QUICK(d);
1131 *arg_i = tmp;
1132 break;
1133
1134 case SOUND_PCM_WRITE_CHANNELS:
1135 /* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1136 if (*arg_i < 0 || *arg_i > AFMT_CHANNEL_MAX) {
1137 *arg_i = 0;
1138 ret = EINVAL;
1139 break;
1140 }
1141 if (*arg_i != 0) {
1142 uint32_t ext = 0;
1143
1144 tmp = 0;
1145 /*
1146 * Map channel number to surround sound formats.
1147 * Devices that need bitperfect mode to operate
1148 * (e.g. more than SND_CHN_MAX channels) are not
1149 * subject to any mapping.
1150 */
1151 if (!(pcm_getflags(d->dev) & SD_F_BITPERFECT)) {
1152 struct pcmchan_matrix *m;
1153
1154 if (*arg_i > SND_CHN_MAX)
1155 *arg_i = SND_CHN_MAX;
1156
1157 m = feeder_matrix_default_channel_map(*arg_i);
1158 if (m != NULL)
1159 ext = m->ext;
1160 }
1161
1162 PCM_ACQUIRE_QUICK(d);
1163 if (wrch) {
1164 CHN_LOCK(wrch);
1165 ret = chn_setformat(wrch,
1166 SND_FORMAT(wrch->format, *arg_i, ext));
1167 tmp = AFMT_CHANNEL(wrch->format);
1168 CHN_UNLOCK(wrch);
1169 }
1170 if (rdch && ret == 0) {
1171 CHN_LOCK(rdch);
1172 ret = chn_setformat(rdch,
1173 SND_FORMAT(rdch->format, *arg_i, ext));
1174 if (tmp == 0)
1175 tmp = AFMT_CHANNEL(rdch->format);
1176 CHN_UNLOCK(rdch);
1177 }
1178 PCM_RELEASE_QUICK(d);
1179 *arg_i = tmp;
1180 } else {
1181 chn = wrch ? wrch : rdch;
1182 CHN_LOCK(chn);
1183 *arg_i = AFMT_CHANNEL(chn->format);
1184 CHN_UNLOCK(chn);
1185 }
1186 break;
1187
1188 case SOUND_PCM_READ_CHANNELS:
1189 chn = wrch ? wrch : rdch;
1190 if (chn) {
1191 CHN_LOCK(chn);
1192 *arg_i = AFMT_CHANNEL(chn->format);
1193 CHN_UNLOCK(chn);
1194 } else {
1195 *arg_i = 0;
1196 ret = EINVAL;
1197 }
1198 break;
1199
1200 case SNDCTL_DSP_GETFMTS: /* returns a mask of supported fmts */
1201 chn = wrch ? wrch : rdch;
1202 if (chn) {
1203 CHN_LOCK(chn);
1204 *arg_i = chn_getformats(chn);
1205 CHN_UNLOCK(chn);
1206 } else {
1207 *arg_i = 0;
1208 ret = EINVAL;
1209 }
1210 break;
1211
1212 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1213 if (*arg_i != AFMT_QUERY) {
1214 tmp = 0;
1215 PCM_ACQUIRE_QUICK(d);
1216 if (wrch) {
1217 CHN_LOCK(wrch);
1218 ret = chn_setformat(wrch, SND_FORMAT(*arg_i,
1219 AFMT_CHANNEL(wrch->format),
1220 AFMT_EXTCHANNEL(wrch->format)));
1221 tmp = wrch->format;
1222 CHN_UNLOCK(wrch);
1223 }
1224 if (rdch && ret == 0) {
1225 CHN_LOCK(rdch);
1226 ret = chn_setformat(rdch, SND_FORMAT(*arg_i,
1227 AFMT_CHANNEL(rdch->format),
1228 AFMT_EXTCHANNEL(rdch->format)));
1229 if (tmp == 0)
1230 tmp = rdch->format;
1231 CHN_UNLOCK(rdch);
1232 }
1233 PCM_RELEASE_QUICK(d);
1234 *arg_i = AFMT_ENCODING(tmp);
1235 } else {
1236 chn = wrch ? wrch : rdch;
1237 CHN_LOCK(chn);
1238 *arg_i = AFMT_ENCODING(chn->format);
1239 CHN_UNLOCK(chn);
1240 }
1241 break;
1242
1243 case SNDCTL_DSP_SETFRAGMENT:
1244 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1245 {
1246 uint32_t fragln = (*arg_i) & 0x0000ffff;
1247 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1248 uint32_t fragsz;
1249 uint32_t r_maxfrags, r_fragsz;
1250
1251 RANGE(fragln, 4, 16);
1252 fragsz = 1 << fragln;
1253
1254 if (maxfrags == 0)
1255 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1256 if (maxfrags < 2)
1257 maxfrags = 2;
1258 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1259 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1260
1261 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1262 PCM_ACQUIRE_QUICK(d);
1263 if (rdch) {
1264 CHN_LOCK(rdch);
1265 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1266 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1267 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1268 CHN_UNLOCK(rdch);
1269 } else {
1270 r_maxfrags = maxfrags;
1271 r_fragsz = fragsz;
1272 }
1273 if (wrch && ret == 0) {
1274 CHN_LOCK(wrch);
1275 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1276 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1277 fragsz = sndbuf_getblksz(wrch->bufsoft);
1278 CHN_UNLOCK(wrch);
1279 } else { /* use whatever came from the read channel */
1280 maxfrags = r_maxfrags;
1281 fragsz = r_fragsz;
1282 }
1283 PCM_RELEASE_QUICK(d);
1284
1285 fragln = 0;
1286 while (fragsz > 1) {
1287 fragln++;
1288 fragsz >>= 1;
1289 }
1290 *arg_i = (maxfrags << 16) | fragln;
1291 }
1292 break;
1293
1294 case SNDCTL_DSP_GETISPACE:
1295 /* return the size of data available in the input queue */
1296 {
1297 audio_buf_info *a = (audio_buf_info *)arg;
1298 if (rdch) {
1299 struct snd_dbuf *bs = rdch->bufsoft;
1300
1301 CHN_LOCK(rdch);
1302 a->bytes = sndbuf_getready(bs);
1303 a->fragments = a->bytes / sndbuf_getblksz(bs);
1304 a->fragstotal = sndbuf_getblkcnt(bs);
1305 a->fragsize = sndbuf_getblksz(bs);
1306 CHN_UNLOCK(rdch);
1307 } else
1308 ret = EINVAL;
1309 }
1310 break;
1311
1312 case SNDCTL_DSP_GETOSPACE:
1313 /* return space available in the output queue */
1314 {
1315 audio_buf_info *a = (audio_buf_info *)arg;
1316 if (wrch) {
1317 struct snd_dbuf *bs = wrch->bufsoft;
1318
1319 CHN_LOCK(wrch);
1320 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1321 a->bytes = sndbuf_getfree(bs);
1322 a->fragments = a->bytes / sndbuf_getblksz(bs);
1323 a->fragstotal = sndbuf_getblkcnt(bs);
1324 a->fragsize = sndbuf_getblksz(bs);
1325 CHN_UNLOCK(wrch);
1326 } else
1327 ret = EINVAL;
1328 }
1329 break;
1330
1331 case SNDCTL_DSP_GETIPTR:
1332 {
1333 count_info *a = (count_info *)arg;
1334 if (rdch) {
1335 struct snd_dbuf *bs = rdch->bufsoft;
1336
1337 CHN_LOCK(rdch);
1338 /* XXX abusive DMA update: chn_rdupdate(rdch); */
1339 a->bytes = sndbuf_gettotal(bs);
1340 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
1341 a->ptr = sndbuf_getfreeptr(bs);
1342 rdch->blocks = sndbuf_getblocks(bs);
1343 CHN_UNLOCK(rdch);
1344 } else
1345 ret = EINVAL;
1346 }
1347 break;
1348
1349 case SNDCTL_DSP_GETOPTR:
1350 {
1351 count_info *a = (count_info *)arg;
1352 if (wrch) {
1353 struct snd_dbuf *bs = wrch->bufsoft;
1354
1355 CHN_LOCK(wrch);
1356 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1357 a->bytes = sndbuf_gettotal(bs);
1358 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
1359 a->ptr = sndbuf_getreadyptr(bs);
1360 wrch->blocks = sndbuf_getblocks(bs);
1361 CHN_UNLOCK(wrch);
1362 } else
1363 ret = EINVAL;
1364 }
1365 break;
1366
1367 case SNDCTL_DSP_GETCAPS:
1368 PCM_LOCK(d);
1369 *arg_i = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER;
1370 if (rdch && wrch && !(pcm_getflags(d->dev) & SD_F_SIMPLEX))
1371 *arg_i |= PCM_CAP_DUPLEX;
1372 if (rdch && (rdch->flags & CHN_F_VIRTUAL) != 0)
1373 *arg_i |= PCM_CAP_VIRTUAL;
1374 if (wrch && (wrch->flags & CHN_F_VIRTUAL) != 0)
1375 *arg_i |= PCM_CAP_VIRTUAL;
1376 PCM_UNLOCK(d);
1377 break;
1378
1379 case SOUND_PCM_READ_BITS:
1380 chn = wrch ? wrch : rdch;
1381 if (chn) {
1382 CHN_LOCK(chn);
1383 if (chn->format & AFMT_8BIT)
1384 *arg_i = 8;
1385 else if (chn->format & AFMT_16BIT)
1386 *arg_i = 16;
1387 else if (chn->format & AFMT_24BIT)
1388 *arg_i = 24;
1389 else if (chn->format & AFMT_32BIT)
1390 *arg_i = 32;
1391 else
1392 ret = EINVAL;
1393 CHN_UNLOCK(chn);
1394 } else {
1395 *arg_i = 0;
1396 ret = EINVAL;
1397 }
1398 break;
1399
1400 case SNDCTL_DSP_SETTRIGGER:
1401 if (rdch) {
1402 CHN_LOCK(rdch);
1403 rdch->flags &= ~CHN_F_NOTRIGGER;
1404 if (*arg_i & PCM_ENABLE_INPUT)
1405 chn_start(rdch, 1);
1406 else {
1407 chn_abort(rdch);
1408 chn_resetbuf(rdch);
1409 rdch->flags |= CHN_F_NOTRIGGER;
1410 }
1411 CHN_UNLOCK(rdch);
1412 }
1413 if (wrch) {
1414 CHN_LOCK(wrch);
1415 wrch->flags &= ~CHN_F_NOTRIGGER;
1416 if (*arg_i & PCM_ENABLE_OUTPUT)
1417 chn_start(wrch, 1);
1418 else {
1419 chn_abort(wrch);
1420 chn_resetbuf(wrch);
1421 wrch->flags |= CHN_F_NOTRIGGER;
1422 }
1423 CHN_UNLOCK(wrch);
1424 }
1425 break;
1426
1427 case SNDCTL_DSP_GETTRIGGER:
1428 *arg_i = 0;
1429 if (wrch) {
1430 CHN_LOCK(wrch);
1431 if (wrch->flags & CHN_F_TRIGGERED)
1432 *arg_i |= PCM_ENABLE_OUTPUT;
1433 CHN_UNLOCK(wrch);
1434 }
1435 if (rdch) {
1436 CHN_LOCK(rdch);
1437 if (rdch->flags & CHN_F_TRIGGERED)
1438 *arg_i |= PCM_ENABLE_INPUT;
1439 CHN_UNLOCK(rdch);
1440 }
1441 break;
1442
1443 case SNDCTL_DSP_GETODELAY:
1444 if (wrch) {
1445 struct snd_dbuf *bs = wrch->bufsoft;
1446
1447 CHN_LOCK(wrch);
1448 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1449 *arg_i = sndbuf_getready(bs);
1450 CHN_UNLOCK(wrch);
1451 } else
1452 ret = EINVAL;
1453 break;
1454
1455 case SNDCTL_DSP_POST:
1456 if (wrch) {
1457 CHN_LOCK(wrch);
1458 wrch->flags &= ~CHN_F_NOTRIGGER;
1459 chn_start(wrch, 1);
1460 CHN_UNLOCK(wrch);
1461 }
1462 break;
1463
1464 case SNDCTL_DSP_SETDUPLEX:
1465 /*
1466 * switch to full-duplex mode if card is in half-duplex
1467 * mode and is able to work in full-duplex mode
1468 */
1469 PCM_LOCK(d);
1470 if (rdch && wrch && (pcm_getflags(d->dev) & SD_F_SIMPLEX))
1471 pcm_setflags(d->dev, pcm_getflags(d->dev)^SD_F_SIMPLEX);
1472 PCM_UNLOCK(d);
1473 break;
1474
1475 /*
1476 * The following four ioctls are simple wrappers around mixer_ioctl
1477 * with no further processing. xcmd is short for "translated
1478 * command".
1479 */
1480 case SNDCTL_DSP_GETRECVOL:
1481 if (xcmd == 0) {
1482 xcmd = SOUND_MIXER_READ_RECLEV;
1483 chn = rdch;
1484 }
1485 /* FALLTHROUGH */
1486 case SNDCTL_DSP_SETRECVOL:
1487 if (xcmd == 0) {
1488 xcmd = SOUND_MIXER_WRITE_RECLEV;
1489 chn = rdch;
1490 }
1491 /* FALLTHROUGH */
1492 case SNDCTL_DSP_GETPLAYVOL:
1493 if (xcmd == 0) {
1494 xcmd = SOUND_MIXER_READ_PCM;
1495 chn = wrch;
1496 }
1497 /* FALLTHROUGH */
1498 case SNDCTL_DSP_SETPLAYVOL:
1499 if (xcmd == 0) {
1500 xcmd = SOUND_MIXER_WRITE_PCM;
1501 chn = wrch;
1502 }
1503
1504 ret = dsp_ioctl_channel(priv, chn, xcmd, arg);
1505 if (ret != -1) {
1506 PCM_GIANT_EXIT(d);
1507 return (ret);
1508 }
1509
1510 if (d->mixer_dev != NULL) {
1511 PCM_ACQUIRE_QUICK(d);
1512 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1513 MIXER_CMD_DIRECT);
1514 PCM_RELEASE_QUICK(d);
1515 } else
1516 ret = ENOTSUP;
1517
1518 break;
1519
1520 case SNDCTL_DSP_GET_RECSRC_NAMES:
1521 case SNDCTL_DSP_GET_RECSRC:
1522 case SNDCTL_DSP_SET_RECSRC:
1523 if (d->mixer_dev != NULL) {
1524 PCM_ACQUIRE_QUICK(d);
1525 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1526 MIXER_CMD_DIRECT);
1527 PCM_RELEASE_QUICK(d);
1528 } else
1529 ret = ENOTSUP;
1530 break;
1531
1532 /*
1533 * The following 3 ioctls aren't very useful at the moment. For
1534 * now, only a single channel is associated with a cdev (/dev/dspN
1535 * instance), so there's only a single output routing to use (i.e.,
1536 * the wrch bound to this cdev).
1537 */
1538 case SNDCTL_DSP_GET_PLAYTGT_NAMES:
1539 {
1540 oss_mixer_enuminfo *ei;
1541 ei = (oss_mixer_enuminfo *)arg;
1542 ei->dev = 0;
1543 ei->ctrl = 0;
1544 ei->version = 0; /* static for now */
1545 ei->strindex[0] = 0;
1546
1547 if (wrch != NULL) {
1548 ei->nvalues = 1;
1549 strlcpy(ei->strings, wrch->name,
1550 sizeof(ei->strings));
1551 } else {
1552 ei->nvalues = 0;
1553 ei->strings[0] = '\0';
1554 }
1555 }
1556 break;
1557 case SNDCTL_DSP_GET_PLAYTGT:
1558 case SNDCTL_DSP_SET_PLAYTGT: /* yes, they are the same for now */
1559 /*
1560 * Re: SET_PLAYTGT
1561 * OSSv4: "The value that was accepted by the device will
1562 * be returned back in the variable pointed by the
1563 * argument."
1564 */
1565 if (wrch != NULL)
1566 *arg_i = 0;
1567 else
1568 ret = EINVAL;
1569 break;
1570
1571 case SNDCTL_DSP_SILENCE:
1572 /*
1573 * Flush the software (pre-feed) buffer, but try to minimize playback
1574 * interruption. (I.e., record unplayed samples with intent to
1575 * restore by SNDCTL_DSP_SKIP.) Intended for application "pause"
1576 * functionality.
1577 */
1578 if (wrch == NULL)
1579 ret = EINVAL;
1580 else {
1581 struct snd_dbuf *bs;
1582 CHN_LOCK(wrch);
1583 while (wrch->inprog != 0)
1584 cv_wait(&wrch->cv, wrch->lock);
1585 bs = wrch->bufsoft;
1586 if ((bs->shadbuf != NULL) && (sndbuf_getready(bs) > 0)) {
1587 bs->sl = sndbuf_getready(bs);
1588 sndbuf_dispose(bs, bs->shadbuf, sndbuf_getready(bs));
1589 sndbuf_fillsilence(bs);
1590 chn_start(wrch, 0);
1591 }
1592 CHN_UNLOCK(wrch);
1593 }
1594 break;
1595
1596 case SNDCTL_DSP_SKIP:
1597 /*
1598 * OSSv4 docs: "This ioctl call discards all unplayed samples in the
1599 * playback buffer by moving the current write position immediately
1600 * before the point where the device is currently reading the samples."
1601 */
1602 if (wrch == NULL)
1603 ret = EINVAL;
1604 else {
1605 struct snd_dbuf *bs;
1606 CHN_LOCK(wrch);
1607 while (wrch->inprog != 0)
1608 cv_wait(&wrch->cv, wrch->lock);
1609 bs = wrch->bufsoft;
1610 if ((bs->shadbuf != NULL) && (bs->sl > 0)) {
1611 sndbuf_softreset(bs);
1612 sndbuf_acquire(bs, bs->shadbuf, bs->sl);
1613 bs->sl = 0;
1614 chn_start(wrch, 0);
1615 }
1616 CHN_UNLOCK(wrch);
1617 }
1618 break;
1619
1620 case SNDCTL_DSP_CURRENT_OPTR:
1621 case SNDCTL_DSP_CURRENT_IPTR:
1622 /**
1623 * @note Changing formats resets the buffer counters, which differs
1624 * from the 4Front drivers. However, I don't expect this to be
1625 * much of a problem.
1626 *
1627 * @note In a test where @c CURRENT_OPTR is called immediately after write
1628 * returns, this driver is about 32K samples behind whereas
1629 * 4Front's is about 8K samples behind. Should determine source
1630 * of discrepancy, even if only out of curiosity.
1631 *
1632 * @todo Actually test SNDCTL_DSP_CURRENT_IPTR.
1633 */
1634 chn = (cmd == SNDCTL_DSP_CURRENT_OPTR) ? wrch : rdch;
1635 if (chn == NULL)
1636 ret = EINVAL;
1637 else {
1638 struct snd_dbuf *bs;
1639 /* int tmp; */
1640
1641 oss_count_t *oc = (oss_count_t *)arg;
1642
1643 CHN_LOCK(chn);
1644 bs = chn->bufsoft;
1645 #if 0
1646 tmp = (sndbuf_getsize(b) + chn_getptr(chn) - sndbuf_gethwptr(b)) % sndbuf_getsize(b);
1647 oc->samples = (sndbuf_gettotal(b) + tmp) / sndbuf_getalign(b);
1648 oc->fifo_samples = (sndbuf_getready(b) - tmp) / sndbuf_getalign(b);
1649 #else
1650 oc->samples = sndbuf_gettotal(bs) / sndbuf_getalign(bs);
1651 oc->fifo_samples = sndbuf_getready(bs) / sndbuf_getalign(bs);
1652 #endif
1653 CHN_UNLOCK(chn);
1654 }
1655 break;
1656
1657 case SNDCTL_DSP_HALT_OUTPUT:
1658 case SNDCTL_DSP_HALT_INPUT:
1659 chn = (cmd == SNDCTL_DSP_HALT_OUTPUT) ? wrch : rdch;
1660 if (chn == NULL)
1661 ret = EINVAL;
1662 else {
1663 CHN_LOCK(chn);
1664 chn_abort(chn);
1665 CHN_UNLOCK(chn);
1666 }
1667 break;
1668
1669 case SNDCTL_DSP_LOW_WATER:
1670 /*
1671 * Set the number of bytes required to attract attention by
1672 * select/poll.
1673 */
1674 if (wrch != NULL) {
1675 CHN_LOCK(wrch);
1676 wrch->lw = (*arg_i > 1) ? *arg_i : 1;
1677 CHN_UNLOCK(wrch);
1678 }
1679 if (rdch != NULL) {
1680 CHN_LOCK(rdch);
1681 rdch->lw = (*arg_i > 1) ? *arg_i : 1;
1682 CHN_UNLOCK(rdch);
1683 }
1684 break;
1685
1686 case SNDCTL_DSP_GETERROR:
1687 /*
1688 * OSSv4 docs: "All errors and counters will automatically be
1689 * cleared to zeroes after the call so each call will return only
1690 * the errors that occurred after the previous invocation. ... The
1691 * play_underruns and rec_overrun fields are the only useful fields
1692 * returned by OSS 4.0."
1693 */
1694 {
1695 audio_errinfo *ei = (audio_errinfo *)arg;
1696
1697 bzero((void *)ei, sizeof(*ei));
1698
1699 if (wrch != NULL) {
1700 CHN_LOCK(wrch);
1701 ei->play_underruns = wrch->xruns;
1702 wrch->xruns = 0;
1703 CHN_UNLOCK(wrch);
1704 }
1705 if (rdch != NULL) {
1706 CHN_LOCK(rdch);
1707 ei->rec_overruns = rdch->xruns;
1708 rdch->xruns = 0;
1709 CHN_UNLOCK(rdch);
1710 }
1711 }
1712 break;
1713
1714 case SNDCTL_DSP_SYNCGROUP:
1715 PCM_ACQUIRE_QUICK(d);
1716 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
1717 PCM_RELEASE_QUICK(d);
1718 break;
1719
1720 case SNDCTL_DSP_SYNCSTART:
1721 PCM_ACQUIRE_QUICK(d);
1722 ret = dsp_oss_syncstart(*arg_i);
1723 PCM_RELEASE_QUICK(d);
1724 break;
1725
1726 case SNDCTL_DSP_POLICY:
1727 PCM_ACQUIRE_QUICK(d);
1728 ret = dsp_oss_policy(wrch, rdch, *arg_i);
1729 PCM_RELEASE_QUICK(d);
1730 break;
1731
1732 case SNDCTL_DSP_COOKEDMODE:
1733 PCM_ACQUIRE_QUICK(d);
1734 if (!(pcm_getflags(d->dev) & SD_F_BITPERFECT))
1735 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
1736 PCM_RELEASE_QUICK(d);
1737 break;
1738 case SNDCTL_DSP_GET_CHNORDER:
1739 PCM_ACQUIRE_QUICK(d);
1740 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
1741 PCM_RELEASE_QUICK(d);
1742 break;
1743 case SNDCTL_DSP_SET_CHNORDER:
1744 PCM_ACQUIRE_QUICK(d);
1745 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);
1746 PCM_RELEASE_QUICK(d);
1747 break;
1748 case SNDCTL_DSP_GETCHANNELMASK: /* XXX vlc */
1749 PCM_ACQUIRE_QUICK(d);
1750 ret = dsp_oss_getchannelmask(wrch, rdch, (int *)arg);
1751 PCM_RELEASE_QUICK(d);
1752 break;
1753 case SNDCTL_DSP_BIND_CHANNEL: /* XXX what?!? */
1754 ret = EINVAL;
1755 break;
1756 #ifdef OSSV4_EXPERIMENT
1757 /*
1758 * XXX The following ioctls are not yet supported and just return
1759 * EINVAL.
1760 */
1761 case SNDCTL_DSP_GETOPEAKS:
1762 case SNDCTL_DSP_GETIPEAKS:
1763 chn = (cmd == SNDCTL_DSP_GETOPEAKS) ? wrch : rdch;
1764 if (chn == NULL)
1765 ret = EINVAL;
1766 else {
1767 oss_peaks_t *op = (oss_peaks_t *)arg;
1768 int lpeak, rpeak;
1769
1770 CHN_LOCK(chn);
1771 ret = chn_getpeaks(chn, &lpeak, &rpeak);
1772 if (ret == -1)
1773 ret = EINVAL;
1774 else {
1775 (*op)[0] = lpeak;
1776 (*op)[1] = rpeak;
1777 }
1778 CHN_UNLOCK(chn);
1779 }
1780 break;
1781
1782 /*
1783 * XXX Once implemented, revisit this for proper cv protection
1784 * (if necessary).
1785 */
1786 case SNDCTL_GETLABEL:
1787 ret = dsp_oss_getlabel(wrch, rdch, (oss_label_t *)arg);
1788 break;
1789 case SNDCTL_SETLABEL:
1790 ret = dsp_oss_setlabel(wrch, rdch, (oss_label_t *)arg);
1791 break;
1792 case SNDCTL_GETSONG:
1793 ret = dsp_oss_getsong(wrch, rdch, (oss_longname_t *)arg);
1794 break;
1795 case SNDCTL_SETSONG:
1796 ret = dsp_oss_setsong(wrch, rdch, (oss_longname_t *)arg);
1797 break;
1798 case SNDCTL_SETNAME:
1799 ret = dsp_oss_setname(wrch, rdch, (oss_longname_t *)arg);
1800 break;
1801 #if 0
1802 /**
1803 * @note The S/PDIF interface ioctls, @c SNDCTL_DSP_READCTL and
1804 * @c SNDCTL_DSP_WRITECTL have been omitted at the suggestion of
1805 * 4Front Technologies.
1806 */
1807 case SNDCTL_DSP_READCTL:
1808 case SNDCTL_DSP_WRITECTL:
1809 ret = EINVAL;
1810 break;
1811 #endif /* !0 (explicitly omitted ioctls) */
1812
1813 #endif /* !OSSV4_EXPERIMENT */
1814 case SNDCTL_DSP_MAPINBUF:
1815 case SNDCTL_DSP_MAPOUTBUF:
1816 case SNDCTL_DSP_SETSYNCRO:
1817 /* undocumented */
1818
1819 case SNDCTL_DSP_SUBDIVIDE:
1820 case SOUND_PCM_WRITE_FILTER:
1821 case SOUND_PCM_READ_FILTER:
1822 /* dunno what these do, don't sound important */
1823
1824 default:
1825 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1826 ret = EINVAL;
1827 break;
1828 }
1829
1830 PCM_GIANT_LEAVE(d);
1831
1832 return (ret);
1833 }
1834
1835 static int
dsp_poll(struct cdev * i_dev,int events,struct thread * td)1836 dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1837 {
1838 struct dsp_cdevpriv *priv;
1839 struct snddev_info *d;
1840 struct pcm_channel *wrch, *rdch;
1841 int ret, e, err;
1842
1843 if ((err = devfs_get_cdevpriv((void **)&priv)) != 0)
1844 return (err);
1845 d = priv->sc;
1846 if (PCM_DETACHING(d) || !DSP_REGISTERED(d)) {
1847 /* XXX many clients don't understand POLLNVAL */
1848 return (events & (POLLHUP | POLLPRI | POLLIN |
1849 POLLRDNORM | POLLOUT | POLLWRNORM));
1850 }
1851 PCM_GIANT_ENTER(d);
1852
1853 ret = 0;
1854
1855 getchns(priv, SD_F_PRIO_RD | SD_F_PRIO_WR);
1856 wrch = priv->wrch;
1857 rdch = priv->rdch;
1858
1859 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
1860 e = (events & (POLLOUT | POLLWRNORM));
1861 if (e)
1862 ret |= chn_poll(wrch, e, td);
1863 }
1864
1865 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
1866 e = (events & (POLLIN | POLLRDNORM));
1867 if (e)
1868 ret |= chn_poll(rdch, e, td);
1869 }
1870
1871 relchns(priv, SD_F_PRIO_RD | SD_F_PRIO_WR);
1872
1873 PCM_GIANT_LEAVE(d);
1874
1875 return (ret);
1876 }
1877
1878 static int
dsp_mmap(struct cdev * i_dev,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)1879 dsp_mmap(struct cdev *i_dev, vm_ooffset_t offset, vm_paddr_t *paddr,
1880 int nprot, vm_memattr_t *memattr)
1881 {
1882
1883 /*
1884 * offset is in range due to checks in dsp_mmap_single().
1885 * XXX memattr is not honored.
1886 */
1887 *paddr = vtophys(offset);
1888 return (0);
1889 }
1890
1891 static int
dsp_mmap_single(struct cdev * i_dev,vm_ooffset_t * offset,vm_size_t size,struct vm_object ** object,int nprot)1892 dsp_mmap_single(struct cdev *i_dev, vm_ooffset_t *offset,
1893 vm_size_t size, struct vm_object **object, int nprot)
1894 {
1895 struct dsp_cdevpriv *priv;
1896 struct snddev_info *d;
1897 struct pcm_channel *wrch, *rdch, *c;
1898 int err;
1899
1900 /*
1901 * Reject PROT_EXEC by default. It just doesn't makes sense.
1902 * Unfortunately, we have to give up this one due to linux_mmap
1903 * changes.
1904 *
1905 * https://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
1906 *
1907 */
1908 #ifdef SV_ABI_LINUX
1909 if ((nprot & PROT_EXEC) && (dsp_mmap_allow_prot_exec < 0 ||
1910 (dsp_mmap_allow_prot_exec == 0 &&
1911 SV_CURPROC_ABI() != SV_ABI_LINUX)))
1912 #else
1913 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec < 1)
1914 #endif
1915 return (EINVAL);
1916
1917 /*
1918 * PROT_READ (alone) selects the input buffer.
1919 * PROT_WRITE (alone) selects the output buffer.
1920 * PROT_WRITE|PROT_READ together select the output buffer.
1921 */
1922 if ((nprot & (PROT_READ | PROT_WRITE)) == 0)
1923 return (EINVAL);
1924
1925 if ((err = devfs_get_cdevpriv((void **)&priv)) != 0)
1926 return (err);
1927 d = priv->sc;
1928 if (PCM_DETACHING(d) || !DSP_REGISTERED(d))
1929 return (EINVAL);
1930
1931 PCM_GIANT_ENTER(d);
1932
1933 getchns(priv, SD_F_PRIO_RD | SD_F_PRIO_WR);
1934 wrch = priv->wrch;
1935 rdch = priv->rdch;
1936
1937 c = ((nprot & PROT_WRITE) != 0) ? wrch : rdch;
1938 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
1939 (*offset + size) > sndbuf_getallocsize(c->bufsoft) ||
1940 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
1941 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
1942 relchns(priv, SD_F_PRIO_RD | SD_F_PRIO_WR);
1943 PCM_GIANT_EXIT(d);
1944 return (EINVAL);
1945 }
1946
1947 if (wrch != NULL)
1948 wrch->flags |= CHN_F_MMAP;
1949 if (rdch != NULL)
1950 rdch->flags |= CHN_F_MMAP;
1951
1952 *offset = (uintptr_t)sndbuf_getbufofs(c->bufsoft, *offset);
1953 relchns(priv, SD_F_PRIO_RD | SD_F_PRIO_WR);
1954 *object = vm_pager_allocate(OBJT_DEVICE, i_dev,
1955 size, nprot, *offset, curthread->td_ucred);
1956
1957 PCM_GIANT_LEAVE(d);
1958
1959 if (*object == NULL)
1960 return (EINVAL);
1961 return (0);
1962 }
1963
1964 static void
dsp_clone(void * arg,struct ucred * cred,char * name,int namelen,struct cdev ** dev)1965 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1966 struct cdev **dev)
1967 {
1968 struct snddev_info *d;
1969 size_t i;
1970
1971 if (*dev != NULL)
1972 return;
1973 if (strcmp(name, "dsp") == 0 && dsp_basename_clone)
1974 goto found;
1975 for (i = 0; i < nitems(dsp_cdevs); i++) {
1976 if (dsp_cdevs[i].alias != NULL &&
1977 strcmp(name, dsp_cdevs[i].name) == 0)
1978 goto found;
1979 }
1980 return;
1981 found:
1982 bus_topo_lock();
1983 d = devclass_get_softc(pcm_devclass, snd_unit);
1984 /*
1985 * If we only have a single soundcard attached and we detach it right
1986 * before entering dsp_clone(), there is a chance pcm_unregister() will
1987 * have returned already, meaning it will have set snd_unit to -1, and
1988 * thus devclass_get_softc() will return NULL here.
1989 */
1990 if (d != NULL && PCM_REGISTERED(d) && d->dsp_dev != NULL) {
1991 *dev = d->dsp_dev;
1992 dev_ref(*dev);
1993 }
1994 bus_topo_unlock();
1995 }
1996
1997 static void
dsp_sysinit(void * p)1998 dsp_sysinit(void *p)
1999 {
2000 if (dsp_ehtag != NULL)
2001 return;
2002 dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
2003 }
2004
2005 static void
dsp_sysuninit(void * p)2006 dsp_sysuninit(void *p)
2007 {
2008 if (dsp_ehtag == NULL)
2009 return;
2010 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
2011 dsp_ehtag = NULL;
2012 }
2013
2014 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
2015 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
2016
2017 char *
dsp_unit2name(char * buf,size_t len,struct pcm_channel * ch)2018 dsp_unit2name(char *buf, size_t len, struct pcm_channel *ch)
2019 {
2020 size_t i;
2021
2022 KASSERT(buf != NULL && len != 0,
2023 ("bogus buf=%p len=%ju", buf, (uintmax_t)len));
2024
2025 for (i = 0; i < nitems(dsp_cdevs); i++) {
2026 if (ch->type != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
2027 continue;
2028 snprintf(buf, len, "%s%d%s%d",
2029 dsp_cdevs[i].name, device_get_unit(ch->dev),
2030 dsp_cdevs[i].sep, ch->unit);
2031 return (buf);
2032 }
2033
2034 return (NULL);
2035 }
2036
2037 static int
dsp_oss_audioinfo_cb(void * data,void * arg)2038 dsp_oss_audioinfo_cb(void *data, void *arg)
2039 {
2040 struct dsp_cdevpriv *priv = data;
2041 struct pcm_channel *ch = arg;
2042
2043 if (DSP_REGISTERED(priv->sc) && (ch == priv->rdch || ch == priv->wrch))
2044 return (1);
2045
2046 return (0);
2047 }
2048
2049 /**
2050 * @brief Handler for SNDCTL_AUDIOINFO.
2051 *
2052 * Gathers information about the audio device specified in ai->dev. If
2053 * ai->dev == -1, then this function gathers information about the current
2054 * device. If the call comes in on a non-audio device and ai->dev == -1,
2055 * return EINVAL.
2056 *
2057 * This routine is supposed to go practically straight to the hardware,
2058 * getting capabilities directly from the sound card driver, side-stepping
2059 * the intermediate channel interface.
2060 *
2061 * @note
2062 * Calling threads must not hold any snddev_info or pcm_channel locks.
2063 *
2064 * @param dev device on which the ioctl was issued
2065 * @param ai ioctl request data container
2066 *
2067 * @retval 0 success
2068 * @retval EINVAL ai->dev specifies an invalid device
2069 *
2070 * @todo Verify correctness of Doxygen tags. ;)
2071 */
2072 int
dsp_oss_audioinfo(struct cdev * i_dev,oss_audioinfo * ai)2073 dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2074 {
2075 struct pcmchan_caps *caps;
2076 struct pcm_channel *ch;
2077 struct snddev_info *d;
2078 uint32_t fmts;
2079 int i, nchan, *rates, minch, maxch, unit;
2080 char *devname, buf[CHN_NAMELEN];
2081
2082 /*
2083 * If probing the device that received the ioctl, make sure it's a
2084 * DSP device. (Users may use this ioctl with /dev/mixer and
2085 * /dev/midi.)
2086 */
2087 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2088 return (EINVAL);
2089
2090 ch = NULL;
2091 devname = NULL;
2092 nchan = 0;
2093 bzero(buf, sizeof(buf));
2094
2095 /*
2096 * Search for the requested audio device (channel). Start by
2097 * iterating over pcm devices.
2098 */
2099 for (unit = 0; pcm_devclass != NULL &&
2100 unit < devclass_get_maxunit(pcm_devclass); unit++) {
2101 d = devclass_get_softc(pcm_devclass, unit);
2102 if (!PCM_REGISTERED(d))
2103 continue;
2104
2105 /* XXX Need Giant magic entry ??? */
2106
2107 /* See the note in function docblock */
2108 PCM_UNLOCKASSERT(d);
2109 PCM_LOCK(d);
2110
2111 CHN_FOREACH(ch, d, channels.pcm) {
2112 CHN_UNLOCKASSERT(ch);
2113 CHN_LOCK(ch);
2114 if (ai->dev == -1) {
2115 if (devfs_foreach_cdevpriv(i_dev,
2116 dsp_oss_audioinfo_cb, ch) != 0) {
2117 devname = dsp_unit2name(buf,
2118 sizeof(buf), ch);
2119 }
2120 } else if (ai->dev == nchan)
2121 devname = dsp_unit2name(buf, sizeof(buf), ch);
2122 if (devname != NULL)
2123 break;
2124 CHN_UNLOCK(ch);
2125 ++nchan;
2126 }
2127
2128 if (devname != NULL) {
2129 /*
2130 * At this point, the following synchronization stuff
2131 * has happened:
2132 * - a specific PCM device is locked.
2133 * - a specific audio channel has been locked, so be
2134 * sure to unlock when exiting;
2135 */
2136
2137 caps = chn_getcaps(ch);
2138
2139 /*
2140 * With all handles collected, zero out the user's
2141 * container and begin filling in its fields.
2142 */
2143 bzero((void *)ai, sizeof(oss_audioinfo));
2144
2145 ai->dev = nchan;
2146 strlcpy(ai->name, ch->name, sizeof(ai->name));
2147
2148 if ((ch->flags & CHN_F_BUSY) == 0)
2149 ai->busy = 0;
2150 else
2151 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2152
2153 /**
2154 * @note
2155 * @c cmd - OSSv4 docs: "Only supported under Linux at
2156 * this moment." Cop-out, I know, but I'll save
2157 * running around in the process table for later.
2158 * Is there a risk of leaking information?
2159 */
2160 ai->pid = ch->pid;
2161
2162 /*
2163 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2164 * Note, however, that a single channel operates in
2165 * only one direction, so PCM_CAP_DUPLEX is out.
2166 */
2167 /**
2168 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2169 * these in pcmchan::caps?
2170 */
2171 ai->caps = PCM_CAP_REALTIME | PCM_CAP_MMAP | PCM_CAP_TRIGGER |
2172 ((ch->flags & CHN_F_VIRTUAL) ? PCM_CAP_VIRTUAL : 0) |
2173 ((ch->direction == PCMDIR_PLAY) ? PCM_CAP_OUTPUT : PCM_CAP_INPUT);
2174
2175 /*
2176 * Collect formats supported @b natively by the
2177 * device. Also determine min/max channels. (I.e.,
2178 * mono, stereo, or both?)
2179 *
2180 * If any channel is stereo, maxch = 2;
2181 * if all channels are stereo, minch = 2, too;
2182 * if any channel is mono, minch = 1;
2183 * and if all channels are mono, maxch = 1.
2184 */
2185 minch = 0;
2186 maxch = 0;
2187 fmts = 0;
2188 for (i = 0; caps->fmtlist[i]; i++) {
2189 fmts |= caps->fmtlist[i];
2190 if (AFMT_CHANNEL(caps->fmtlist[i]) > 1) {
2191 minch = (minch == 0) ? 2 : minch;
2192 maxch = 2;
2193 } else {
2194 minch = 1;
2195 maxch = (maxch == 0) ? 1 : maxch;
2196 }
2197 }
2198
2199 if (ch->direction == PCMDIR_PLAY)
2200 ai->oformats = fmts;
2201 else
2202 ai->iformats = fmts;
2203
2204 /**
2205 * @note
2206 * @c magic - OSSv4 docs: "Reserved for internal use
2207 * by OSS."
2208 *
2209 * @par
2210 * @c card_number - OSSv4 docs: "Number of the sound
2211 * card where this device belongs or -1 if this
2212 * information is not available. Applications
2213 * should normally not use this field for any
2214 * purpose."
2215 */
2216 ai->card_number = -1;
2217 /**
2218 * @todo @c song_name - depends first on
2219 * SNDCTL_[GS]ETSONG @todo @c label - depends
2220 * on SNDCTL_[GS]ETLABEL
2221 * @todo @c port_number - routing information?
2222 */
2223 ai->port_number = -1;
2224 ai->mixer_dev = (d->mixer_dev != NULL) ? unit : -1;
2225 /**
2226 * @note
2227 * @c legacy_device - OSSv4 docs: "Obsolete."
2228 */
2229 ai->legacy_device = -1;
2230 snprintf(ai->devnode, sizeof(ai->devnode), "/dev/dsp%d", unit);
2231 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2232 /**
2233 * @note
2234 * @c flags - OSSv4 docs: "Reserved for future use."
2235 *
2236 * @note
2237 * @c binding - OSSv4 docs: "Reserved for future use."
2238 *
2239 * @todo @c handle - haven't decided how to generate
2240 * this yet; bus, vendor, device IDs?
2241 */
2242 ai->min_rate = caps->minspeed;
2243 ai->max_rate = caps->maxspeed;
2244
2245 ai->min_channels = minch;
2246 ai->max_channels = maxch;
2247
2248 ai->nrates = chn_getrates(ch, &rates);
2249 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2250 ai->nrates = OSS_MAX_SAMPLE_RATES;
2251
2252 for (i = 0; i < ai->nrates; i++)
2253 ai->rates[i] = rates[i];
2254
2255 ai->next_play_engine = 0;
2256 ai->next_rec_engine = 0;
2257
2258 CHN_UNLOCK(ch);
2259 }
2260
2261 PCM_UNLOCK(d);
2262
2263 if (devname != NULL)
2264 return (0);
2265 }
2266
2267 /* Exhausted the search -- nothing is locked, so return. */
2268 return (EINVAL);
2269 }
2270
2271 /**
2272 * @brief Assigns a PCM channel to a sync group.
2273 *
2274 * Sync groups are used to enable audio operations on multiple devices
2275 * simultaneously. They may be used with any number of devices and may
2276 * span across applications. Devices are added to groups with
2277 * the SNDCTL_DSP_SYNCGROUP ioctl, and operations are triggered with the
2278 * SNDCTL_DSP_SYNCSTART ioctl.
2279 *
2280 * If the @c id field of the @c group parameter is set to zero, then a new
2281 * sync group is created. Otherwise, wrch and rdch (if set) are added to
2282 * the group specified.
2283 *
2284 * @todo As far as memory allocation, should we assume that things are
2285 * okay and allocate with M_WAITOK before acquiring channel locks,
2286 * freeing later if not?
2287 *
2288 * @param wrch output channel associated w/ device (if any)
2289 * @param rdch input channel associated w/ device (if any)
2290 * @param group Sync group parameters
2291 *
2292 * @retval 0 success
2293 * @retval non-zero error to be propagated upstream
2294 */
2295 static int
dsp_oss_syncgroup(struct pcm_channel * wrch,struct pcm_channel * rdch,oss_syncgroup * group)2296 dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group)
2297 {
2298 struct pcmchan_syncmember *smrd, *smwr;
2299 struct pcmchan_syncgroup *sg;
2300 int ret, sg_ids[3];
2301
2302 smrd = NULL;
2303 smwr = NULL;
2304 sg = NULL;
2305 ret = 0;
2306
2307 /*
2308 * Free_unr() may sleep, so store released syncgroup IDs until after
2309 * all locks are released.
2310 */
2311 sg_ids[0] = sg_ids[1] = sg_ids[2] = 0;
2312
2313 PCM_SG_LOCK();
2314
2315 /*
2316 * - Insert channel(s) into group's member list.
2317 * - Set CHN_F_NOTRIGGER on channel(s).
2318 * - Stop channel(s).
2319 */
2320
2321 /*
2322 * If device's channels are already mapped to a group, unmap them.
2323 */
2324 if (wrch) {
2325 CHN_LOCK(wrch);
2326 sg_ids[0] = chn_syncdestroy(wrch);
2327 }
2328
2329 if (rdch) {
2330 CHN_LOCK(rdch);
2331 sg_ids[1] = chn_syncdestroy(rdch);
2332 }
2333
2334 /*
2335 * Verify that mode matches character device properites.
2336 * - Bail if PCM_ENABLE_OUTPUT && wrch == NULL.
2337 * - Bail if PCM_ENABLE_INPUT && rdch == NULL.
2338 */
2339 if (((wrch == NULL) && (group->mode & PCM_ENABLE_OUTPUT)) ||
2340 ((rdch == NULL) && (group->mode & PCM_ENABLE_INPUT))) {
2341 ret = EINVAL;
2342 goto out;
2343 }
2344
2345 /*
2346 * An id of zero indicates the user wants to create a new
2347 * syncgroup.
2348 */
2349 if (group->id == 0) {
2350 sg = (struct pcmchan_syncgroup *)malloc(sizeof(*sg), M_DEVBUF, M_NOWAIT);
2351 if (sg != NULL) {
2352 SLIST_INIT(&sg->members);
2353 sg->id = alloc_unr(pcmsg_unrhdr);
2354
2355 group->id = sg->id;
2356 SLIST_INSERT_HEAD(&snd_pcm_syncgroups, sg, link);
2357 } else
2358 ret = ENOMEM;
2359 } else {
2360 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2361 if (sg->id == group->id)
2362 break;
2363 }
2364 if (sg == NULL)
2365 ret = EINVAL;
2366 }
2367
2368 /* Couldn't create or find a syncgroup. Fail. */
2369 if (sg == NULL)
2370 goto out;
2371
2372 /*
2373 * Allocate a syncmember, assign it and a channel together, and
2374 * insert into syncgroup.
2375 */
2376 if (group->mode & PCM_ENABLE_INPUT) {
2377 smrd = (struct pcmchan_syncmember *)malloc(sizeof(*smrd), M_DEVBUF, M_NOWAIT);
2378 if (smrd == NULL) {
2379 ret = ENOMEM;
2380 goto out;
2381 }
2382
2383 SLIST_INSERT_HEAD(&sg->members, smrd, link);
2384 smrd->parent = sg;
2385 smrd->ch = rdch;
2386
2387 chn_abort(rdch);
2388 rdch->flags |= CHN_F_NOTRIGGER;
2389 rdch->sm = smrd;
2390 }
2391
2392 if (group->mode & PCM_ENABLE_OUTPUT) {
2393 smwr = (struct pcmchan_syncmember *)malloc(sizeof(*smwr), M_DEVBUF, M_NOWAIT);
2394 if (smwr == NULL) {
2395 ret = ENOMEM;
2396 goto out;
2397 }
2398
2399 SLIST_INSERT_HEAD(&sg->members, smwr, link);
2400 smwr->parent = sg;
2401 smwr->ch = wrch;
2402
2403 chn_abort(wrch);
2404 wrch->flags |= CHN_F_NOTRIGGER;
2405 wrch->sm = smwr;
2406 }
2407
2408 out:
2409 if (ret != 0) {
2410 if (smrd != NULL)
2411 free(smrd, M_DEVBUF);
2412 if ((sg != NULL) && SLIST_EMPTY(&sg->members)) {
2413 sg_ids[2] = sg->id;
2414 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2415 free(sg, M_DEVBUF);
2416 }
2417
2418 if (wrch)
2419 wrch->sm = NULL;
2420 if (rdch)
2421 rdch->sm = NULL;
2422 }
2423
2424 if (wrch)
2425 CHN_UNLOCK(wrch);
2426 if (rdch)
2427 CHN_UNLOCK(rdch);
2428
2429 PCM_SG_UNLOCK();
2430
2431 if (sg_ids[0])
2432 free_unr(pcmsg_unrhdr, sg_ids[0]);
2433 if (sg_ids[1])
2434 free_unr(pcmsg_unrhdr, sg_ids[1]);
2435 if (sg_ids[2])
2436 free_unr(pcmsg_unrhdr, sg_ids[2]);
2437
2438 return (ret);
2439 }
2440
2441 /**
2442 * @brief Launch a sync group into action
2443 *
2444 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2445 * iterates over all members, triggering them along the way.
2446 *
2447 * @note Caller must not hold any channel locks.
2448 *
2449 * @param sg_id sync group identifier
2450 *
2451 * @retval 0 success
2452 * @retval non-zero error worthy of propagating upstream to user
2453 */
2454 static int
dsp_oss_syncstart(int sg_id)2455 dsp_oss_syncstart(int sg_id)
2456 {
2457 struct pcmchan_syncmember *sm, *sm_tmp;
2458 struct pcmchan_syncgroup *sg;
2459 struct pcm_channel *c;
2460 int ret, needlocks;
2461
2462 /* Get the synclists lock */
2463 PCM_SG_LOCK();
2464
2465 do {
2466 ret = 0;
2467 needlocks = 0;
2468
2469 /* Search for syncgroup by ID */
2470 SLIST_FOREACH(sg, &snd_pcm_syncgroups, link) {
2471 if (sg->id == sg_id)
2472 break;
2473 }
2474
2475 /* Return EINVAL if not found */
2476 if (sg == NULL) {
2477 ret = EINVAL;
2478 break;
2479 }
2480
2481 /* Any removals resulting in an empty group should've handled this */
2482 KASSERT(!SLIST_EMPTY(&sg->members), ("found empty syncgroup"));
2483
2484 /*
2485 * Attempt to lock all member channels - if any are already
2486 * locked, unlock those acquired, sleep for a bit, and try
2487 * again.
2488 */
2489 SLIST_FOREACH(sm, &sg->members, link) {
2490 if (CHN_TRYLOCK(sm->ch) == 0) {
2491 int timo = hz * 5/1000;
2492 if (timo < 1)
2493 timo = 1;
2494
2495 /* Release all locked channels so far, retry */
2496 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2497 /* sm is the member already locked */
2498 if (sm == sm_tmp)
2499 break;
2500 CHN_UNLOCK(sm_tmp->ch);
2501 }
2502
2503 /** @todo Is PRIBIO correct/ */
2504 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
2505 PRIBIO | PCATCH, "pcmsg", timo);
2506 if (ret == EINTR || ret == ERESTART)
2507 break;
2508
2509 needlocks = 1;
2510 ret = 0; /* Assumes ret == EAGAIN... */
2511 }
2512 }
2513 } while (needlocks && ret == 0);
2514
2515 /* Proceed only if no errors encountered. */
2516 if (ret == 0) {
2517 /* Launch channels */
2518 while ((sm = SLIST_FIRST(&sg->members)) != NULL) {
2519 SLIST_REMOVE_HEAD(&sg->members, link);
2520
2521 c = sm->ch;
2522 c->sm = NULL;
2523 chn_start(c, 1);
2524 c->flags &= ~CHN_F_NOTRIGGER;
2525 CHN_UNLOCK(c);
2526
2527 free(sm, M_DEVBUF);
2528 }
2529
2530 SLIST_REMOVE(&snd_pcm_syncgroups, sg, pcmchan_syncgroup, link);
2531 free(sg, M_DEVBUF);
2532 }
2533
2534 PCM_SG_UNLOCK();
2535
2536 /*
2537 * Free_unr() may sleep, so be sure to give up the syncgroup lock
2538 * first.
2539 */
2540 if (ret == 0)
2541 free_unr(pcmsg_unrhdr, sg_id);
2542
2543 return (ret);
2544 }
2545
2546 /**
2547 * @brief Handler for SNDCTL_DSP_POLICY
2548 *
2549 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
2550 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
2551 * specifying those two parameters, s/he simply selects a number from 0..10
2552 * which corresponds to a buffer size. Smaller numbers request smaller
2553 * buffers with lower latencies (at greater overhead from more frequent
2554 * interrupts), while greater numbers behave in the opposite manner.
2555 *
2556 * The 4Front spec states that a value of 5 should be the default. However,
2557 * this implementation deviates slightly by using a linear scale without
2558 * consulting drivers. I.e., even though drivers may have different default
2559 * buffer sizes, a policy argument of 5 will have the same result across
2560 * all drivers.
2561 *
2562 * See http://manuals.opensound.com/developer/SNDCTL_DSP_POLICY.html for
2563 * more information.
2564 *
2565 * @todo When SNDCTL_DSP_COOKEDMODE is supported, it'll be necessary to
2566 * work with hardware drivers directly.
2567 *
2568 * @note PCM channel arguments must not be locked by caller.
2569 *
2570 * @param wrch Pointer to opened playback channel (optional; may be NULL)
2571 * @param rdch " recording channel (optional; may be NULL)
2572 * @param policy Integer from [0:10]
2573 *
2574 * @retval 0 constant (for now)
2575 */
2576 static int
dsp_oss_policy(struct pcm_channel * wrch,struct pcm_channel * rdch,int policy)2577 dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
2578 {
2579 int ret;
2580
2581 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
2582 return (EIO);
2583
2584 /* Default: success */
2585 ret = 0;
2586
2587 if (rdch) {
2588 CHN_LOCK(rdch);
2589 ret = chn_setlatency(rdch, policy);
2590 CHN_UNLOCK(rdch);
2591 }
2592
2593 if (wrch && ret == 0) {
2594 CHN_LOCK(wrch);
2595 ret = chn_setlatency(wrch, policy);
2596 CHN_UNLOCK(wrch);
2597 }
2598
2599 if (ret)
2600 ret = EIO;
2601
2602 return (ret);
2603 }
2604
2605 /**
2606 * @brief Enable or disable "cooked" mode
2607 *
2608 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
2609 * is the default, the sound system handles rate and format conversions
2610 * automatically (ex: user writing 11025Hz/8 bit/unsigned but card only
2611 * operates with 44100Hz/16bit/signed samples).
2612 *
2613 * Disabling cooked mode is intended for applications wanting to mmap()
2614 * a sound card's buffer space directly, bypassing the FreeBSD 2-stage
2615 * feeder architecture, presumably to gain as much control over audio
2616 * hardware as possible.
2617 *
2618 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_COOKEDMODE.html
2619 * for more details.
2620 *
2621 * @param wrch playback channel (optional; may be NULL)
2622 * @param rdch recording channel (optional; may be NULL)
2623 * @param enabled 0 = raw mode, 1 = cooked mode
2624 *
2625 * @retval EINVAL Operation not yet supported.
2626 */
2627 static int
dsp_oss_cookedmode(struct pcm_channel * wrch,struct pcm_channel * rdch,int enabled)2628 dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
2629 {
2630
2631 /*
2632 * XXX I just don't get it. Why don't they call it
2633 * "BITPERFECT" ~ SNDCTL_DSP_BITPERFECT !?!?.
2634 * This is just plain so confusing, incoherent,
2635 * <insert any non-printable characters here>.
2636 */
2637 if (!(enabled == 1 || enabled == 0))
2638 return (EINVAL);
2639
2640 /*
2641 * I won't give in. I'm inverting its logic here and now.
2642 * Brag all you want, but "BITPERFECT" should be the better
2643 * term here.
2644 */
2645 enabled ^= 0x00000001;
2646
2647 if (wrch != NULL) {
2648 CHN_LOCK(wrch);
2649 wrch->flags &= ~CHN_F_BITPERFECT;
2650 wrch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
2651 CHN_UNLOCK(wrch);
2652 }
2653
2654 if (rdch != NULL) {
2655 CHN_LOCK(rdch);
2656 rdch->flags &= ~CHN_F_BITPERFECT;
2657 rdch->flags |= (enabled != 0) ? CHN_F_BITPERFECT : 0x00000000;
2658 CHN_UNLOCK(rdch);
2659 }
2660
2661 return (0);
2662 }
2663
2664 /**
2665 * @brief Retrieve channel interleaving order
2666 *
2667 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
2668 *
2669 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html
2670 * for more details.
2671 *
2672 * @note As the ioctl definition is still under construction, FreeBSD
2673 * does not currently support SNDCTL_DSP_GET_CHNORDER.
2674 *
2675 * @param wrch playback channel (optional; may be NULL)
2676 * @param rdch recording channel (optional; may be NULL)
2677 * @param map channel map (result will be stored there)
2678 *
2679 * @retval EINVAL Operation not yet supported.
2680 */
2681 static int
dsp_oss_getchnorder(struct pcm_channel * wrch,struct pcm_channel * rdch,unsigned long long * map)2682 dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2683 {
2684 struct pcm_channel *ch;
2685 int ret;
2686
2687 ch = (wrch != NULL) ? wrch : rdch;
2688 if (ch != NULL) {
2689 CHN_LOCK(ch);
2690 ret = chn_oss_getorder(ch, map);
2691 CHN_UNLOCK(ch);
2692 } else
2693 ret = EINVAL;
2694
2695 return (ret);
2696 }
2697
2698 /**
2699 * @brief Specify channel interleaving order
2700 *
2701 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
2702 *
2703 * @note As the ioctl definition is still under construction, FreeBSD
2704 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
2705 *
2706 * @param wrch playback channel (optional; may be NULL)
2707 * @param rdch recording channel (optional; may be NULL)
2708 * @param map channel map
2709 *
2710 * @retval EINVAL Operation not yet supported.
2711 */
2712 static int
dsp_oss_setchnorder(struct pcm_channel * wrch,struct pcm_channel * rdch,unsigned long long * map)2713 dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2714 {
2715 int ret;
2716
2717 ret = 0;
2718
2719 if (wrch != NULL) {
2720 CHN_LOCK(wrch);
2721 ret = chn_oss_setorder(wrch, map);
2722 CHN_UNLOCK(wrch);
2723 }
2724
2725 if (ret == 0 && rdch != NULL) {
2726 CHN_LOCK(rdch);
2727 ret = chn_oss_setorder(rdch, map);
2728 CHN_UNLOCK(rdch);
2729 }
2730
2731 return (ret);
2732 }
2733
2734 static int
dsp_oss_getchannelmask(struct pcm_channel * wrch,struct pcm_channel * rdch,int * mask)2735 dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch,
2736 int *mask)
2737 {
2738 struct pcm_channel *ch;
2739 uint32_t chnmask;
2740 int ret;
2741
2742 chnmask = 0;
2743 ch = (wrch != NULL) ? wrch : rdch;
2744
2745 if (ch != NULL) {
2746 CHN_LOCK(ch);
2747 ret = chn_oss_getmask(ch, &chnmask);
2748 CHN_UNLOCK(ch);
2749 } else
2750 ret = EINVAL;
2751
2752 if (ret == 0)
2753 *mask = chnmask;
2754
2755 return (ret);
2756 }
2757
2758 #ifdef OSSV4_EXPERIMENT
2759 /**
2760 * @brief Retrieve an audio device's label
2761 *
2762 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
2763 *
2764 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2765 * for more details.
2766 *
2767 * From Hannu@4Front: "For example ossxmix (just like some HW mixer
2768 * consoles) can show variable "labels" for certain controls. By default
2769 * the application name (say quake) is shown as the label but
2770 * applications may change the labels themselves."
2771 *
2772 * @note As the ioctl definition is still under construction, FreeBSD
2773 * does not currently support @c SNDCTL_GETLABEL.
2774 *
2775 * @param wrch playback channel (optional; may be NULL)
2776 * @param rdch recording channel (optional; may be NULL)
2777 * @param label label gets copied here
2778 *
2779 * @retval EINVAL Operation not yet supported.
2780 */
2781 static int
dsp_oss_getlabel(struct pcm_channel * wrch,struct pcm_channel * rdch,oss_label_t * label)2782 dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2783 {
2784 return (EINVAL);
2785 }
2786
2787 /**
2788 * @brief Specify an audio device's label
2789 *
2790 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
2791 * comments for @c dsp_oss_getlabel immediately above.
2792 *
2793 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html
2794 * for more details.
2795 *
2796 * @note As the ioctl definition is still under construction, FreeBSD
2797 * does not currently support SNDCTL_SETLABEL.
2798 *
2799 * @param wrch playback channel (optional; may be NULL)
2800 * @param rdch recording channel (optional; may be NULL)
2801 * @param label label gets copied from here
2802 *
2803 * @retval EINVAL Operation not yet supported.
2804 */
2805 static int
dsp_oss_setlabel(struct pcm_channel * wrch,struct pcm_channel * rdch,oss_label_t * label)2806 dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2807 {
2808 return (EINVAL);
2809 }
2810
2811 /**
2812 * @brief Retrieve name of currently played song
2813 *
2814 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
2815 * tell the system the name of the currently playing song, which would be
2816 * visible in @c /dev/sndstat.
2817 *
2818 * See @c http://manuals.opensound.com/developer/SNDCTL_GETSONG.html
2819 * for more details.
2820 *
2821 * @note As the ioctl definition is still under construction, FreeBSD
2822 * does not currently support SNDCTL_GETSONG.
2823 *
2824 * @param wrch playback channel (optional; may be NULL)
2825 * @param rdch recording channel (optional; may be NULL)
2826 * @param song song name gets copied here
2827 *
2828 * @retval EINVAL Operation not yet supported.
2829 */
2830 static int
dsp_oss_getsong(struct pcm_channel * wrch,struct pcm_channel * rdch,oss_longname_t * song)2831 dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2832 {
2833 return (EINVAL);
2834 }
2835
2836 /**
2837 * @brief Retrieve name of currently played song
2838 *
2839 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
2840 * tell the system the name of the currently playing song, which would be
2841 * visible in @c /dev/sndstat.
2842 *
2843 * See @c http://manuals.opensound.com/developer/SNDCTL_SETSONG.html
2844 * for more details.
2845 *
2846 * @note As the ioctl definition is still under construction, FreeBSD
2847 * does not currently support SNDCTL_SETSONG.
2848 *
2849 * @param wrch playback channel (optional; may be NULL)
2850 * @param rdch recording channel (optional; may be NULL)
2851 * @param song song name gets copied from here
2852 *
2853 * @retval EINVAL Operation not yet supported.
2854 */
2855 static int
dsp_oss_setsong(struct pcm_channel * wrch,struct pcm_channel * rdch,oss_longname_t * song)2856 dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2857 {
2858 return (EINVAL);
2859 }
2860
2861 /**
2862 * @brief Rename a device
2863 *
2864 * This is a handler for the @c SNDCTL_SETNAME ioctl.
2865 *
2866 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for
2867 * more details.
2868 *
2869 * From Hannu@4Front: "This call is used to change the device name
2870 * reported in /dev/sndstat and ossinfo. So instead of using some generic
2871 * 'OSS loopback audio (MIDI) driver' the device may be given a meaningfull
2872 * name depending on the current context (for example 'OSS virtual wave table
2873 * synth' or 'VoIP link to London')."
2874 *
2875 * @note As the ioctl definition is still under construction, FreeBSD
2876 * does not currently support SNDCTL_SETNAME.
2877 *
2878 * @param wrch playback channel (optional; may be NULL)
2879 * @param rdch recording channel (optional; may be NULL)
2880 * @param name new device name gets copied from here
2881 *
2882 * @retval EINVAL Operation not yet supported.
2883 */
2884 static int
dsp_oss_setname(struct pcm_channel * wrch,struct pcm_channel * rdch,oss_longname_t * name)2885 dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
2886 {
2887 return (EINVAL);
2888 }
2889 #endif /* !OSSV4_EXPERIMENT */
2890