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