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