Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / sys / dev / sound / pcm / dsp.c
1 /*
2  * Copyright (c) 1999 Cameron Grant <gandalf@vilnya.demon.co.uk>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/sound/pcm/dsp.c,v 1.15.2.13 2002/08/30 13:53:03 orion Exp $
27  * $DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.2 2003/06/17 04:28:31 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/queue.h>
32
33 #include <dev/sound/pcm/sound.h>
34
35 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.2 2003/06/17 04:28:31 dillon Exp $");
36
37 #define OLDPCM_IOCTL
38
39 static d_open_t dsp_open;
40 static d_close_t dsp_close;
41 static d_read_t dsp_read;
42 static d_write_t dsp_write;
43 static d_ioctl_t dsp_ioctl;
44 static d_poll_t dsp_poll;
45 static d_mmap_t dsp_mmap;
46
47 static struct cdevsw dsp_cdevsw = {
48         /* open */      dsp_open,
49         /* close */     dsp_close,
50         /* read */      dsp_read,
51         /* write */     dsp_write,
52         /* ioctl */     dsp_ioctl,
53         /* poll */      dsp_poll,
54         /* mmap */      dsp_mmap,
55         /* strategy */  nostrategy,
56         /* name */      "dsp",
57         /* maj */       SND_CDEV_MAJOR,
58         /* dump */      nodump,
59         /* psize */     nopsize,
60         /* flags */     0,
61 };
62
63 #ifdef USING_DEVFS
64 static eventhandler_tag dsp_ehtag;
65 #endif
66
67 static struct snddev_info *
68 dsp_get_info(dev_t dev)
69 {
70         struct snddev_info *d;
71         int unit;
72
73         unit = PCMUNIT(dev);
74         if (unit >= devclass_get_maxunit(pcm_devclass))
75                 return NULL;
76         d = devclass_get_softc(pcm_devclass, unit);
77
78         return d;
79 }
80
81 static u_int32_t
82 dsp_get_flags(dev_t dev)
83 {
84         device_t bdev;
85         int unit;
86
87         unit = PCMUNIT(dev);
88         if (unit >= devclass_get_maxunit(pcm_devclass))
89                 return 0xffffffff;
90         bdev = devclass_get_device(pcm_devclass, unit);
91
92         return pcm_getflags(bdev);
93 }
94
95 static void
96 dsp_set_flags(dev_t dev, u_int32_t flags)
97 {
98         device_t bdev;
99         int unit;
100
101         unit = PCMUNIT(dev);
102         if (unit >= devclass_get_maxunit(pcm_devclass))
103                 return;
104         bdev = devclass_get_device(pcm_devclass, unit);
105
106         pcm_setflags(bdev, flags);
107 }
108
109 /*
110  * return the channels channels associated with an open device instance.
111  * set the priority if the device is simplex and one direction (only) is
112  * specified.
113  * lock channels specified.
114  */
115 static int
116 getchns(dev_t dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
117 {
118         struct snddev_info *d;
119         u_int32_t flags;
120
121         flags = dsp_get_flags(dev);
122         d = dsp_get_info(dev);
123         pcm_lock(d);
124         pcm_inprog(d, 1);
125         KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
126                 ("getchns: read and write both prioritised"));
127
128         if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
129                 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
130                 dsp_set_flags(dev, flags);
131         }
132
133         *rdch = dev->si_drv1;
134         *wrch = dev->si_drv2;
135         if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
136                 if (prio) {
137                         if (*rdch && flags & SD_F_PRIO_WR) {
138                                 dev->si_drv1 = NULL;
139                                 *rdch = pcm_getfakechan(d);
140                         } else if (*wrch && flags & SD_F_PRIO_RD) {
141                                 dev->si_drv2 = NULL;
142                                 *wrch = pcm_getfakechan(d);
143                         }
144                 }
145
146                 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
147         }
148         pcm_unlock(d);
149
150         if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
151                 CHN_LOCK(*rdch);
152         if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
153                 CHN_LOCK(*wrch);
154
155         return 0;
156 }
157
158 /* unlock specified channels */
159 static void
160 relchns(dev_t dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
161 {
162         struct snddev_info *d;
163
164         d = dsp_get_info(dev);
165         if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
166                 CHN_UNLOCK(wrch);
167         if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
168                 CHN_UNLOCK(rdch);
169         pcm_lock(d);
170         pcm_inprog(d, -1);
171         pcm_unlock(d);
172 }
173
174 static int
175 dsp_open(dev_t i_dev, int flags, int mode, struct proc *p)
176 {
177         struct pcm_channel *rdch, *wrch;
178         struct snddev_info *d;
179         intrmask_t s;
180         u_int32_t fmt;
181         int devtype;
182
183         s = spltty();
184         d = dsp_get_info(i_dev);
185         devtype = PCMDEV(i_dev);
186
187         /* decide default format */
188         switch (devtype) {
189         case SND_DEV_DSP16:
190                 fmt = AFMT_S16_LE;
191                 break;
192
193         case SND_DEV_DSP:
194                 fmt = AFMT_U8;
195                 break;
196
197         case SND_DEV_AUDIO:
198                 fmt = AFMT_MU_LAW;
199                 break;
200
201         case SND_DEV_NORESET:
202                 fmt = 0;
203                 break;
204
205         case SND_DEV_DSPREC:
206                 fmt = AFMT_U8;
207                 if (mode & FWRITE) {
208                         splx(s);
209                         return EINVAL;
210                 }
211                 break;
212
213         default:
214                 panic("impossible devtype %d", devtype);
215         }
216
217         /* lock snddev so nobody else can monkey with it */
218         pcm_lock(d);
219
220         rdch = i_dev->si_drv1;
221         wrch = i_dev->si_drv2;
222
223         if ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) && (rdch || wrch)) {
224                 /* simplex device, already open, exit */
225                 pcm_unlock(d);
226                 splx(s);
227                 return EBUSY;
228         }
229
230         if (((flags & FREAD) && rdch) || ((flags & FWRITE) && wrch)) {
231                 /* device already open in one or both directions */
232                 pcm_unlock(d);
233                 splx(s);
234                 return EBUSY;
235         }
236
237         /*  if we get here, the open request is valid */
238         if (flags & FREAD) {
239                 /* open for read */
240                 if (devtype == SND_DEV_DSPREC)
241                         rdch = pcm_chnalloc(d, PCMDIR_REC, p->p_pid, PCMCHAN(i_dev));
242                 else
243                         rdch = pcm_chnalloc(d, PCMDIR_REC, p->p_pid, -1);
244                 if (!rdch) {
245                         /* no channel available, exit */
246                         pcm_unlock(d);
247                         splx(s);
248                         return EBUSY;
249                 }
250                 /* got a channel, already locked for us */
251         }
252
253         if (flags & FWRITE) {
254                 /* open for write */
255                 wrch = pcm_chnalloc(d, PCMDIR_PLAY, p->p_pid, -1);
256                 if (!wrch) {
257                         /* no channel available */
258                         if (rdch && (flags & FREAD)) {
259                                 /* just opened a read channel, release it */
260                                 pcm_chnrelease(rdch);
261                         }
262                                 /* exit */
263                         pcm_unlock(d);
264                         splx(s);
265                         return EBUSY;
266                 }
267                 /* got a channel, already locked for us */
268         }
269
270         i_dev->si_drv1 = rdch;
271         i_dev->si_drv2 = wrch;
272         pcm_unlock(d);
273         /* finished with snddev, new channels still locked */
274
275         /* bump refcounts, reset and unlock any channels that we just opened */
276         if (flags & FREAD) {
277                 if (chn_reset(rdch, fmt)) {
278                         pcm_lock(d);
279                         pcm_chnrelease(rdch);
280                         if (wrch && (flags & FWRITE))
281                                 pcm_chnrelease(wrch);
282                         pcm_unlock(d);
283                         splx(s);
284                         return ENODEV;
285                 }
286                 if (flags & O_NONBLOCK)
287                         rdch->flags |= CHN_F_NBIO;
288                 pcm_chnref(rdch, 1);
289                 CHN_UNLOCK(rdch);
290         }
291         if (flags & FWRITE) {
292                 if (chn_reset(wrch, fmt)) {
293                         pcm_lock(d);
294                         pcm_chnrelease(wrch);
295                         if (flags & FREAD) {
296                                 CHN_LOCK(rdch);
297                                 pcm_chnref(rdch, -1);
298                                 pcm_chnrelease(rdch);
299                                 CHN_UNLOCK(rdch);
300                         }
301                         pcm_unlock(d);
302                         splx(s);
303                         return ENODEV;
304                 }
305                 if (flags & O_NONBLOCK)
306                         wrch->flags |= CHN_F_NBIO;
307                 pcm_chnref(wrch, 1);
308                 CHN_UNLOCK(wrch);
309         }
310         splx(s);
311         return 0;
312 }
313
314 static int
315 dsp_close(dev_t i_dev, int flags, int mode, struct proc *p)
316 {
317         struct pcm_channel *rdch, *wrch;
318         struct snddev_info *d;
319         intrmask_t s;
320         int exit;
321
322         s = spltty();
323         d = dsp_get_info(i_dev);
324         pcm_lock(d);
325         rdch = i_dev->si_drv1;
326         wrch = i_dev->si_drv2;
327
328         exit = 0;
329
330         /* decrement refcount for each channel, exit if nonzero */
331         if (rdch) {
332                 CHN_LOCK(rdch);
333                 if (pcm_chnref(rdch, -1) > 0) {
334                         CHN_UNLOCK(rdch);
335                         exit = 1;
336                 }
337         }
338         if (wrch) {
339                 CHN_LOCK(wrch);
340                 if (pcm_chnref(wrch, -1) > 0) {
341                         CHN_UNLOCK(wrch);
342                         exit = 1;
343                 }
344         }
345         if (exit) {
346                 pcm_unlock(d);
347                 splx(s);
348                 return 0;
349         }
350
351         /* both refcounts are zero, abort and release */
352
353         if (pcm_getfakechan(d))
354                 pcm_getfakechan(d)->flags = 0;
355
356         i_dev->si_drv1 = NULL;
357         i_dev->si_drv2 = NULL;
358
359         dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
360         pcm_unlock(d);
361
362         if (rdch) {
363                 chn_abort(rdch); /* won't sleep */
364                 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
365                 chn_reset(rdch, 0);
366                 pcm_chnrelease(rdch);
367         }
368         if (wrch) {
369                 chn_flush(wrch); /* may sleep */
370                 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
371                 chn_reset(wrch, 0);
372                 pcm_chnrelease(wrch);
373         }
374
375         splx(s);
376         return 0;
377 }
378
379 static int
380 dsp_read(dev_t i_dev, struct uio *buf, int flag)
381 {
382         struct pcm_channel *rdch, *wrch;
383         intrmask_t s;
384         int ret;
385
386         s = spltty();
387         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
388
389         KASSERT(rdch, ("dsp_read: nonexistant channel"));
390         KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
391
392         if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
393                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
394                 splx(s);
395                 return EINVAL;
396         }
397         if (!(rdch->flags & CHN_F_RUNNING))
398                 rdch->flags |= CHN_F_RUNNING;
399         ret = chn_read(rdch, buf);
400         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
401
402         splx(s);
403         return ret;
404 }
405
406 static int
407 dsp_write(dev_t i_dev, struct uio *buf, int flag)
408 {
409         struct pcm_channel *rdch, *wrch;
410         intrmask_t s;
411         int ret;
412
413         s = spltty();
414         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
415
416         KASSERT(wrch, ("dsp_write: nonexistant channel"));
417         KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
418
419         if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
420                 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
421                 splx(s);
422                 return EINVAL;
423         }
424         if (!(wrch->flags & CHN_F_RUNNING))
425                 wrch->flags |= CHN_F_RUNNING;
426         ret = chn_write(wrch, buf);
427         relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
428
429         splx(s);
430         return ret;
431 }
432
433 static int
434 dsp_ioctl(dev_t i_dev, u_long cmd, caddr_t arg, int mode, struct proc *p)
435 {
436         struct pcm_channel *wrch, *rdch;
437         struct snddev_info *d;
438         intrmask_t s;
439         int kill;
440         int ret = 0, *arg_i = (int *)arg, tmp;
441
442         /*
443          * this is an evil hack to allow broken apps to perform mixer ioctls
444          * on dsp devices.
445          */
446
447         if (IOCGROUP(cmd) == 'M') {
448                 dev_t pdev;
449
450                 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(PCMUNIT(i_dev), SND_DEV_CTL, 0));
451                 return mixer_ioctl(pdev, cmd, arg, mode, p);
452         }
453
454         s = spltty();
455         d = dsp_get_info(i_dev);
456         getchns(i_dev, &rdch, &wrch, 0);
457
458         kill = 0;
459         if (wrch && (wrch->flags & CHN_F_DEAD))
460                 kill |= 1;
461         if (rdch && (rdch->flags & CHN_F_DEAD))
462                 kill |= 2;
463         if (kill == 3) {
464                 relchns(i_dev, rdch, wrch, 0);
465                 splx(s);
466                 return EINVAL;
467         }
468         if (kill & 1)
469                 wrch = NULL;
470         if (kill & 2)
471                 rdch = NULL;
472
473         switch(cmd) {
474 #ifdef OLDPCM_IOCTL
475         /*
476          * we start with the new ioctl interface.
477          */
478         case AIONWRITE: /* how many bytes can write ? */
479 /*
480                 if (wrch && wrch->bufhard.dl)
481                         while (chn_wrfeed(wrch) == 0);
482 */
483                 *arg_i = wrch? sndbuf_getfree(wrch->bufsoft) : 0;
484                 break;
485
486         case AIOSSIZE:     /* set the current blocksize */
487                 {
488                         struct snd_size *p = (struct snd_size *)arg;
489
490                         p->play_size = 0;
491                         p->rec_size = 0;
492                         if (wrch) {
493                                 CHN_LOCK(wrch);
494                                 chn_setblocksize(wrch, 2, p->play_size);
495                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
496                                 CHN_UNLOCK(wrch);
497                         }
498                         if (rdch) {
499                                 CHN_LOCK(rdch);
500                                 chn_setblocksize(rdch, 2, p->rec_size);
501                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
502                                 CHN_UNLOCK(rdch);
503                         }
504                 }
505                 break;
506         case AIOGSIZE:  /* get the current blocksize */
507                 {
508                         struct snd_size *p = (struct snd_size *)arg;
509
510                         if (wrch)
511                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
512                         if (rdch)
513                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
514                 }
515                 break;
516
517         case AIOSFMT:
518                 {
519                         snd_chan_param *p = (snd_chan_param *)arg;
520
521                         if (wrch) {
522                                 CHN_LOCK(wrch);
523                                 chn_setformat(wrch, p->play_format);
524                                 chn_setspeed(wrch, p->play_rate);
525                                 CHN_UNLOCK(wrch);
526                         }
527                         if (rdch) {
528                                 CHN_LOCK(rdch);
529                                 chn_setformat(rdch, p->rec_format);
530                                 chn_setspeed(rdch, p->rec_rate);
531                                 CHN_UNLOCK(rdch);
532                         }
533                 }
534                 /* FALLTHROUGH */
535
536         case AIOGFMT:
537                 {
538                         snd_chan_param *p = (snd_chan_param *)arg;
539
540                         p->play_rate = wrch? wrch->speed : 0;
541                         p->rec_rate = rdch? rdch->speed : 0;
542                         p->play_format = wrch? wrch->format : 0;
543                         p->rec_format = rdch? rdch->format : 0;
544                 }
545                 break;
546
547         case AIOGCAP:     /* get capabilities */
548                 {
549                         snd_capabilities *p = (snd_capabilities *)arg;
550                         struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
551                         dev_t pdev;
552
553                         if (rdch) {
554                                 CHN_LOCK(rdch);
555                                 rcaps = chn_getcaps(rdch);
556                         }
557                         if (wrch) {
558                                 CHN_LOCK(wrch);
559                                 pcaps = chn_getcaps(wrch);
560                         }
561                         p->rate_min = max(rcaps? rcaps->minspeed : 0,
562                                           pcaps? pcaps->minspeed : 0);
563                         p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
564                                           pcaps? pcaps->maxspeed : 1000000);
565                         p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
566                                          wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
567                         /* XXX bad on sb16 */
568                         p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
569                                      (wrch? chn_getformats(wrch) : 0xffffffff);
570                         if (rdch && wrch)
571                                 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
572                         pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(PCMUNIT(i_dev), SND_DEV_CTL, 0));
573                         p->mixers = 1; /* default: one mixer */
574                         p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
575                         p->left = p->right = 100;
576                         if (wrch)
577                                 CHN_UNLOCK(wrch);
578                         if (rdch)
579                                 CHN_UNLOCK(rdch);
580                 }
581                 break;
582
583         case AIOSTOP:
584                 if (*arg_i == AIOSYNC_PLAY && wrch)
585                         *arg_i = chn_abort(wrch);
586                 else if (*arg_i == AIOSYNC_CAPTURE && rdch)
587                         *arg_i = chn_abort(rdch);
588                 else {
589                         printf("AIOSTOP: bad channel 0x%x\n", *arg_i);
590                         *arg_i = 0;
591                 }
592                 break;
593
594         case AIOSYNC:
595                 printf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
596                         ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
597                 break;
598 #endif
599         /*
600          * here follow the standard ioctls (filio.h etc.)
601          */
602         case FIONREAD: /* get # bytes to read */
603 /*              if (rdch && rdch->bufhard.dl)
604                         while (chn_rdfeed(rdch) == 0);
605 */              *arg_i = rdch? sndbuf_getready(rdch->bufsoft) : 0;
606                 break;
607
608         case FIOASYNC: /*set/clear async i/o */
609                 DEB( printf("FIOASYNC\n") ; )
610                 break;
611
612         case SNDCTL_DSP_NONBLOCK:
613         case FIONBIO: /* set/clear non-blocking i/o */
614                 if (rdch)
615                         rdch->flags &= ~CHN_F_NBIO;
616                 if (wrch)
617                         wrch->flags &= ~CHN_F_NBIO;
618                 if (*arg_i) {
619                         if (rdch)
620                                 rdch->flags |= CHN_F_NBIO;
621                         if (wrch)
622                                 wrch->flags |= CHN_F_NBIO;
623                 }
624                 break;
625
626         /*
627          * Finally, here is the linux-compatible ioctl interface
628          */
629 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
630         case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
631         case SNDCTL_DSP_GETBLKSIZE:
632                 if (wrch)
633                         *arg_i = sndbuf_getblksz(wrch->bufsoft);
634                 else if (rdch)
635                         *arg_i = sndbuf_getblksz(rdch->bufsoft);
636                 else
637                         *arg_i = 0;
638                 break ;
639
640         case SNDCTL_DSP_SETBLKSIZE:
641                 RANGE(*arg_i, 16, 65536);
642                 if (wrch) {
643                         CHN_LOCK(wrch);
644                         chn_setblocksize(wrch, 2, *arg_i);
645                         CHN_UNLOCK(wrch);
646                 }
647                 if (rdch) {
648                         CHN_LOCK(rdch);
649                         chn_setblocksize(rdch, 2, *arg_i);
650                         CHN_UNLOCK(rdch);
651                 }
652                 break;
653
654         case SNDCTL_DSP_RESET:
655                 DEB(printf("dsp reset\n"));
656                 if (wrch)
657                         chn_abort(wrch);
658                 if (rdch)
659                         chn_abort(rdch);
660                 break;
661
662         case SNDCTL_DSP_SYNC:
663                 DEB(printf("dsp sync\n"));
664                 /* chn_sync may sleep */
665                 if (wrch) {
666                         CHN_LOCK(wrch);
667                         chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
668                         CHN_UNLOCK(wrch);
669                 }
670                 break;
671
672         case SNDCTL_DSP_SPEED:
673                 /* chn_setspeed may sleep */
674                 tmp = 0;
675                 if (wrch) {
676                         CHN_LOCK(wrch);
677                         ret = chn_setspeed(wrch, *arg_i);
678                         tmp = wrch->speed;
679                         CHN_UNLOCK(wrch);
680                 }
681                 if (rdch && ret == 0) {
682                         CHN_LOCK(rdch);
683                         ret = chn_setspeed(rdch, *arg_i);
684                         if (tmp == 0)
685                                 tmp = rdch->speed;
686                         CHN_UNLOCK(rdch);
687                 }
688                 *arg_i = tmp;
689                 break;
690
691         case SOUND_PCM_READ_RATE:
692                 *arg_i = wrch? wrch->speed : rdch->speed;
693                 break;
694
695         case SNDCTL_DSP_STEREO:
696                 tmp = -1;
697                 *arg_i = (*arg_i)? AFMT_STEREO : 0;
698                 if (wrch) {
699                         CHN_LOCK(wrch);
700                         ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
701                         tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
702                         CHN_UNLOCK(wrch);
703                 }
704                 if (rdch && ret == 0) {
705                         CHN_LOCK(rdch);
706                         ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
707                         if (tmp == -1)
708                                 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
709                         CHN_UNLOCK(rdch);
710                 }
711                 *arg_i = tmp;
712                 break;
713
714         case SOUND_PCM_WRITE_CHANNELS:
715 /*      case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
716                 if (*arg_i != 0) {
717                         tmp = 0;
718                         *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
719                         if (wrch) {
720                                 CHN_LOCK(wrch);
721                                 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
722                                 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
723                                 CHN_UNLOCK(wrch);
724                         }
725                         if (rdch && ret == 0) {
726                                 CHN_LOCK(rdch);
727                                 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
728                                 if (tmp == 0)
729                                         tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
730                                 CHN_UNLOCK(rdch);
731                         }
732                         *arg_i = tmp;
733                 } else {
734                         *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
735                 }
736                 break;
737
738         case SOUND_PCM_READ_CHANNELS:
739                 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_STEREO)? 2 : 1;
740                 break;
741
742         case SNDCTL_DSP_GETFMTS:        /* returns a mask of supported fmts */
743                 *arg_i = wrch? chn_getformats(wrch) : chn_getformats(rdch);
744                 break ;
745
746         case SNDCTL_DSP_SETFMT: /* sets _one_ format */
747                 /* XXX locking */
748                 if ((*arg_i != AFMT_QUERY)) {
749                         tmp = 0;
750                         if (wrch) {
751                                 CHN_LOCK(wrch);
752                                 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
753                                 tmp = wrch->format & ~AFMT_STEREO;
754                                 CHN_UNLOCK(wrch);
755                         }
756                         if (rdch && ret == 0) {
757                                 CHN_LOCK(rdch);
758                                 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
759                                 if (tmp == 0)
760                                         tmp = rdch->format & ~AFMT_STEREO;
761                                 CHN_UNLOCK(rdch);
762                         }
763                         *arg_i = tmp;
764                 } else
765                         *arg_i = (wrch? wrch->format : rdch->format) & ~AFMT_STEREO;
766                 break;
767
768         case SNDCTL_DSP_SETFRAGMENT:
769                 /* XXX locking */
770                 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
771                 {
772                         u_int32_t fragln = (*arg_i) & 0x0000ffff;
773                         u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
774                         u_int32_t fragsz;
775
776                         RANGE(fragln, 4, 16);
777                         fragsz = 1 << fragln;
778
779                         if (maxfrags == 0)
780                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
781                         if (maxfrags < 2) {
782                                 ret = EINVAL;
783                                 break;
784                         }
785                         if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
786                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
787
788                         DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
789                         if (rdch) {
790                                 CHN_LOCK(rdch);
791                                 ret = chn_setblocksize(rdch, maxfrags, fragsz);
792                                 maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
793                                 fragsz = sndbuf_getblksz(rdch->bufsoft);
794                                 CHN_UNLOCK(rdch);
795                         }
796                         if (wrch && ret == 0) {
797                                 CHN_LOCK(wrch);
798                                 ret = chn_setblocksize(wrch, maxfrags, fragsz);
799                                 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
800                                 fragsz = sndbuf_getblksz(wrch->bufsoft);
801                                 CHN_UNLOCK(wrch);
802                         }
803
804                         fragln = 0;
805                         while (fragsz > 1) {
806                                 fragln++;
807                                 fragsz >>= 1;
808                         }
809                         *arg_i = (maxfrags << 16) | fragln;
810                 }
811                 break;
812
813         case SNDCTL_DSP_GETISPACE:
814                 /* return the size of data available in the input queue */
815                 {
816                         audio_buf_info *a = (audio_buf_info *)arg;
817                         if (rdch) {
818                                 struct snd_dbuf *bs = rdch->bufsoft;
819
820                                 CHN_LOCK(rdch);
821                                 a->bytes = sndbuf_getready(bs);
822                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
823                                 a->fragstotal = sndbuf_getblkcnt(bs);
824                                 a->fragsize = sndbuf_getblksz(bs);
825                                 CHN_UNLOCK(rdch);
826                         }
827                 }
828                 break;
829
830         case SNDCTL_DSP_GETOSPACE:
831                 /* return space available in the output queue */
832                 {
833                         audio_buf_info *a = (audio_buf_info *)arg;
834                         if (wrch) {
835                                 struct snd_dbuf *bs = wrch->bufsoft;
836
837                                 CHN_LOCK(wrch);
838                                 chn_wrupdate(wrch);
839                                 a->bytes = sndbuf_getfree(bs);
840                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
841                                 a->fragstotal = sndbuf_getblkcnt(bs);
842                                 a->fragsize = sndbuf_getblksz(bs);
843                                 CHN_UNLOCK(wrch);
844                         }
845                 }
846                 break;
847
848         case SNDCTL_DSP_GETIPTR:
849                 {
850                         count_info *a = (count_info *)arg;
851                         if (rdch) {
852                                 struct snd_dbuf *bs = rdch->bufsoft;
853
854                                 CHN_LOCK(rdch);
855                                 chn_rdupdate(rdch);
856                                 a->bytes = sndbuf_gettotal(bs);
857                                 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
858                                 a->ptr = sndbuf_getreadyptr(bs);
859                                 rdch->blocks = sndbuf_getblocks(bs);
860                                 CHN_UNLOCK(rdch);
861                         } else
862                                 ret = EINVAL;
863                 }
864                 break;
865
866         case SNDCTL_DSP_GETOPTR:
867                 {
868                         count_info *a = (count_info *)arg;
869                         if (wrch) {
870                                 struct snd_dbuf *bs = wrch->bufsoft;
871
872                                 CHN_LOCK(wrch);
873                                 chn_wrupdate(wrch);
874                                 a->bytes = sndbuf_gettotal(bs);
875                                 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
876                                 a->ptr = sndbuf_getreadyptr(bs);
877                                 wrch->blocks = sndbuf_getblocks(bs);
878                                 CHN_UNLOCK(wrch);
879                         } else
880                                 ret = EINVAL;
881                 }
882                 break;
883
884         case SNDCTL_DSP_GETCAPS:
885                 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
886                 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
887                         *arg_i |= DSP_CAP_DUPLEX;
888                 break;
889
890         case SOUND_PCM_READ_BITS:
891                 *arg_i = ((wrch? wrch->format : rdch->format) & AFMT_16BIT)? 16 : 8;
892                 break;
893
894         case SNDCTL_DSP_SETTRIGGER:
895                 if (rdch) {
896                         CHN_LOCK(rdch);
897                         rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
898                         if (*arg_i & PCM_ENABLE_INPUT)
899                                 chn_start(rdch, 1);
900                         else
901                                 rdch->flags |= CHN_F_NOTRIGGER;
902                         CHN_UNLOCK(rdch);
903                 }
904                 if (wrch) {
905                         CHN_LOCK(wrch);
906                         wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
907                         if (*arg_i & PCM_ENABLE_OUTPUT)
908                                 chn_start(wrch, 1);
909                         else
910                                 wrch->flags |= CHN_F_NOTRIGGER;
911                         CHN_UNLOCK(wrch);
912                 }
913                 break;
914
915         case SNDCTL_DSP_GETTRIGGER:
916                 *arg_i = 0;
917                 if (wrch && wrch->flags & CHN_F_TRIGGERED)
918                         *arg_i |= PCM_ENABLE_OUTPUT;
919                 if (rdch && rdch->flags & CHN_F_TRIGGERED)
920                         *arg_i |= PCM_ENABLE_INPUT;
921                 break;
922
923         case SNDCTL_DSP_GETODELAY:
924                 if (wrch) {
925                         struct snd_dbuf *b = wrch->bufhard;
926                         struct snd_dbuf *bs = wrch->bufsoft;
927
928                         CHN_LOCK(wrch);
929                         chn_wrupdate(wrch);
930                         *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
931                         CHN_UNLOCK(wrch);
932                 } else
933                         ret = EINVAL;
934                 break;
935
936         case SNDCTL_DSP_POST:
937                 if (wrch) {
938                         CHN_LOCK(wrch);
939                         wrch->flags &= ~CHN_F_NOTRIGGER;
940                         chn_start(wrch, 1);
941                         CHN_UNLOCK(wrch);
942                 }
943                 break;
944
945         case SNDCTL_DSP_MAPINBUF:
946         case SNDCTL_DSP_MAPOUTBUF:
947         case SNDCTL_DSP_SETSYNCRO:
948                 /* undocumented */
949
950         case SNDCTL_DSP_SUBDIVIDE:
951         case SOUND_PCM_WRITE_FILTER:
952         case SOUND_PCM_READ_FILTER:
953                 /* dunno what these do, don't sound important */
954         default:
955                 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
956                 ret = EINVAL;
957                 break;
958         }
959         relchns(i_dev, rdch, wrch, 0);
960         splx(s);
961         return ret;
962 }
963
964 static int
965 dsp_poll(dev_t i_dev, int events, struct proc *p)
966 {
967         struct pcm_channel *wrch = NULL, *rdch = NULL;
968         intrmask_t s;
969         int ret, e;
970
971         s = spltty();
972         ret = 0;
973         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
974
975         if (wrch) {
976                 e = (events & (POLLOUT | POLLWRNORM));
977                 if (e)
978                         ret |= chn_poll(wrch, e, p);
979         }
980         if (rdch) {
981                 e = (events & (POLLIN | POLLRDNORM));
982                 if (e)
983                         ret |= chn_poll(rdch, e, p);
984         }
985         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
986
987         splx(s);
988         return ret;
989 }
990
991 static int
992 dsp_mmap(dev_t i_dev, vm_offset_t offset, int nprot)
993 {
994         struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
995         intrmask_t s;
996         int ret;
997
998         if (nprot & PROT_EXEC)
999                 return -1;
1000
1001         s = spltty();
1002         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1003 #if 0
1004         /*
1005          * XXX the linux api uses the nprot to select read/write buffer
1006          * our vm system doesn't allow this, so force write buffer
1007          */
1008
1009         if (wrch && (nprot & PROT_WRITE)) {
1010                 c = wrch;
1011         } else if (rdch && (nprot & PROT_READ)) {
1012                 c = rdch;
1013         } else {
1014                 splx(s);
1015                 return -1;
1016         }
1017 #else
1018         c = wrch;
1019 #endif
1020
1021         if (c == NULL) {
1022                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1023                 splx(s);
1024                 return -1;
1025         }
1026
1027         if (offset >= sndbuf_getsize(c->bufsoft)) {
1028                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1029                 splx(s);
1030                 return -1;
1031         }
1032
1033         if (!(c->flags & CHN_F_MAPPED))
1034                 c->flags |= CHN_F_MAPPED;
1035
1036         ret = atop(vtophys(sndbuf_getbufofs(c->bufsoft, offset)));
1037         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1038
1039         splx(s);
1040         return ret;
1041 }
1042
1043 int
1044 dsp_register(int unit, int channel)
1045 {
1046         make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP, channel),
1047                  UID_ROOT, GID_WHEEL, 0666, "dsp%d.%d", unit, channel);
1048         make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_DSP16, channel),
1049                  UID_ROOT, GID_WHEEL, 0666, "dspW%d.%d", unit, channel);
1050         make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_AUDIO, channel),
1051                  UID_ROOT, GID_WHEEL, 0666, "audio%d.%d", unit, channel);
1052
1053         return 0;
1054 }
1055
1056 int
1057 dsp_registerrec(int unit, int channel)
1058 {
1059         make_dev(&dsp_cdevsw, PCMMKMINOR(unit, SND_DEV_DSPREC, channel),
1060                  UID_ROOT, GID_WHEEL, 0666, "dspr%d.%d", unit, channel);
1061
1062         return 0;
1063 }
1064
1065 int
1066 dsp_unregister(int unit, int channel)
1067 {
1068         dev_t pdev;
1069
1070         pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_DSP, channel));
1071         destroy_dev(pdev);
1072         pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_DSP16, channel));
1073         destroy_dev(pdev);
1074         pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_AUDIO, channel));
1075         destroy_dev(pdev);
1076
1077         return 0;
1078 }
1079
1080 int
1081 dsp_unregisterrec(int unit, int channel)
1082 {
1083         dev_t pdev;
1084
1085         pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, SND_DEV_DSPREC, channel));
1086         destroy_dev(pdev);
1087
1088         return 0;
1089 }
1090
1091 #ifdef USING_DEVFS
1092 static void
1093 dsp_clone(void *arg, char *name, int namelen, dev_t *dev)
1094 {
1095         dev_t pdev;
1096         int i, cont, unit, devtype;
1097         int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1098         char *devnames[3] = {"dsp", "dspW", "audio"};
1099
1100         if (*dev != NODEV)
1101                 return;
1102         if (pcm_devclass == NULL)
1103                 return;
1104
1105         devtype = 0;
1106         unit = -1;
1107         for (i = 0; (i < 3) && (unit == -1); i++) {
1108                 devtype = devtypes[i];
1109                 if (strcmp(name, devnames[i]) == 0) {
1110                         unit = snd_unit;
1111                 } else {
1112                         if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1113                                 unit = -1;
1114                 }
1115         }
1116         if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1117                 return;
1118
1119         cont = 1;
1120         for (i = 0; cont; i++) {
1121                 pdev = makedev(SND_CDEV_MAJOR, PCMMKMINOR(unit, devtype, i));
1122                 if (pdev->si_flags & SI_NAMED) {
1123                         if ((pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1124                                 *dev = pdev;
1125                                 return;
1126                         }
1127                 } else {
1128                         cont = 0;
1129                 }
1130         }
1131 }
1132
1133 static void
1134 dsp_sysinit(void *p)
1135 {
1136         dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1137 }
1138
1139 static void
1140 dsp_sysuninit(void *p)
1141 {
1142         if (dsp_ehtag != NULL)
1143                 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1144 }
1145
1146 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1147 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1148 #endif
1149
1150