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