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