Remove unmatched crit_exit() from dsp_ioctl(). A critical section was never
[dragonfly.git] / sys / dev / sound / pcm / dsp.c
1 /*-
2  * Copyright (c) 1999 Cameron Grant <cg@freebsd.org>
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.80.2.6 2006/04/04 17:43:48 ariff Exp $
27  * $DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.17 2008/02/28 17:19:11 tgen Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/queue.h>
32
33 #include <dev/sound/pcm/dsp.h>
34 #include <dev/sound/pcm/sound.h>
35
36 SND_DECLARE_FILE("$DragonFly: src/sys/dev/sound/pcm/dsp.c,v 1.17 2008/02/28 17:19:11 tgen Exp $");
37
38 #define OLDPCM_IOCTL
39
40 static d_open_t dsp_open;
41 static d_close_t dsp_close;
42 static d_read_t dsp_read;
43 static d_write_t dsp_write;
44 static d_ioctl_t dsp_ioctl;
45 static d_poll_t dsp_poll;
46 static d_mmap_t dsp_mmap;
47
48 struct dev_ops dsp_cdevsw = {
49         { "dsp", SND_CDEV_MAJOR, 0},
50         /*.d_flags =    D_NEEDGIANT,*/
51         .d_open =       dsp_open,
52         .d_close =      dsp_close,
53         .d_read =       dsp_read,
54         .d_write =      dsp_write,
55         .d_ioctl =      dsp_ioctl,
56         .d_poll =       dsp_poll,
57         .d_mmap =       dsp_mmap,
58 };
59
60 #ifdef USING_DEVFS
61 static eventhandler_tag dsp_ehtag;
62 #endif
63
64 struct snddev_info *
65 dsp_get_info(struct cdev *dev)
66 {
67         struct snddev_info *d;
68         int unit;
69
70         unit = PCMUNIT(dev);
71         if (unit >= devclass_get_maxunit(pcm_devclass))
72                 return NULL;
73         d = devclass_get_softc(pcm_devclass, unit);
74
75         return d;
76 }
77
78 static u_int32_t
79 dsp_get_flags(struct cdev *dev)
80 {
81         device_t bdev;
82         int unit;
83
84         unit = PCMUNIT(dev);
85         if (unit >= devclass_get_maxunit(pcm_devclass))
86                 return 0xffffffff;
87         bdev = devclass_get_device(pcm_devclass, unit);
88
89         return pcm_getflags(bdev);
90 }
91
92 static void
93 dsp_set_flags(struct cdev *dev, u_int32_t flags)
94 {
95         device_t bdev;
96         int unit;
97
98         unit = PCMUNIT(dev);
99         if (unit >= devclass_get_maxunit(pcm_devclass))
100                 return;
101         bdev = devclass_get_device(pcm_devclass, unit);
102
103         pcm_setflags(bdev, flags);
104 }
105
106 /*
107  * return the channels associated with an open device instance.
108  * set the priority if the device is simplex and one direction (only) is
109  * specified.
110  * lock channels specified.
111  */
112 static int
113 getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch, u_int32_t prio)
114 {
115         struct snddev_info *d;
116         u_int32_t flags;
117
118         flags = dsp_get_flags(dev);
119         d = dsp_get_info(dev);
120         pcm_inprog(d, 1);
121         pcm_lock(d);
122         KASSERT((flags & SD_F_PRIO_SET) != SD_F_PRIO_SET, \
123                 ("getchns: read and write both prioritised"));
124
125         if ((flags & SD_F_PRIO_SET) == 0 && (prio != (SD_F_PRIO_RD | SD_F_PRIO_WR))) {
126                 flags |= prio & (SD_F_PRIO_RD | SD_F_PRIO_WR);
127                 dsp_set_flags(dev, flags);
128         }
129
130         *rdch = dev->si_drv1;
131         *wrch = dev->si_drv2;
132         if ((flags & SD_F_SIMPLEX) && (flags & SD_F_PRIO_SET)) {
133                 if (prio) {
134                         if (*rdch && flags & SD_F_PRIO_WR) {
135                                 dev->si_drv1 = NULL;
136                                 *rdch = pcm_getfakechan(d);
137                         } else if (*wrch && flags & SD_F_PRIO_RD) {
138                                 dev->si_drv2 = NULL;
139                                 *wrch = pcm_getfakechan(d);
140                         }
141                 }
142
143                 pcm_getfakechan(d)->flags |= CHN_F_BUSY;
144         }
145         pcm_unlock(d);
146
147         if (*rdch && *rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
148                 CHN_LOCK(*rdch);
149         if (*wrch && *wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
150                 CHN_LOCK(*wrch);
151
152         return 0;
153 }
154
155 /* unlock specified channels */
156 static void
157 relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch, u_int32_t prio)
158 {
159         struct snddev_info *d;
160
161         d = dsp_get_info(dev);
162         if (wrch && wrch != pcm_getfakechan(d) && (prio & SD_F_PRIO_WR))
163                 CHN_UNLOCK(wrch);
164         if (rdch && rdch != pcm_getfakechan(d) && (prio & SD_F_PRIO_RD))
165                 CHN_UNLOCK(rdch);
166         pcm_inprog(d, -1);
167 }
168
169 static int
170 dsp_open(struct dev_open_args *ap)
171 {
172         struct cdev *i_dev = ap->a_head.a_dev;
173         struct thread *td = curthread;
174         int flags = ap->a_oflags;
175         struct pcm_channel *rdch, *wrch;
176         struct snddev_info *d;
177         u_int32_t fmt;
178         int devtype;
179         int error;
180         int chnum;
181
182         if (i_dev == NULL || td == NULL)
183                 return ENODEV;
184
185         if ((flags & (FREAD | FWRITE)) == 0)
186                 return EINVAL;
187
188         d = dsp_get_info(i_dev);
189         devtype = PCMDEV(i_dev);
190         chnum = -1;
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 (flags & FWRITE)
213                         return EINVAL;
214                 chnum = PCMCHAN(i_dev);
215                 break;
216
217         default:
218                 panic("impossible devtype %d", devtype);
219         }
220
221         /* lock snddev so nobody else can monkey with it */
222         pcm_lock(d);
223
224         rdch = i_dev->si_drv1;
225         wrch = i_dev->si_drv2;
226
227         if (rdch || wrch || ((dsp_get_flags(i_dev) & SD_F_SIMPLEX) &&
228                     (flags & (FREAD | FWRITE)) == (FREAD | FWRITE))) {
229                 /* simplex or not, better safe than sorry. */
230                 pcm_unlock(d);
231                 return EBUSY;
232         }
233
234         /*
235          * if we get here, the open request is valid- either:
236          *   * we were previously not open
237          *   * we were open for play xor record and the opener wants
238          *     the non-open direction
239          */
240         if (flags & FREAD) {
241                 /* open for read */
242                 pcm_unlock(d);
243                 error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, chnum);
244                 if (error != 0 && error != EBUSY && chnum != -1 && (flags & FWRITE))
245                         error = pcm_chnalloc(d, &rdch, PCMDIR_REC, td->td_proc->p_pid, -1);
246
247                 if (error == 0 && (chn_reset(rdch, fmt) ||
248                                 (fmt && chn_setspeed(rdch, DSP_DEFAULT_SPEED))))
249                         error = ENODEV;
250
251                 if (error != 0) {
252                         if (rdch)
253                                 pcm_chnrelease(rdch);
254                         return error;
255                 }
256
257                 pcm_chnref(rdch, 1);
258                 CHN_UNLOCK(rdch);
259                 pcm_lock(d);
260         }
261
262         if (flags & FWRITE) {
263             /* open for write */
264             pcm_unlock(d);
265             error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, chnum);
266             if (error != 0 && error != EBUSY && chnum != -1 && (flags & FREAD))
267                 error = pcm_chnalloc(d, &wrch, PCMDIR_PLAY, td->td_proc->p_pid, -1);
268
269             if (error == 0 && (chn_reset(wrch, fmt) ||
270                         (fmt && chn_setspeed(wrch, DSP_DEFAULT_SPEED))))
271                 error = ENODEV;
272
273             if (error != 0) {
274                 if (wrch)
275                     pcm_chnrelease(wrch);
276                 if (rdch) {
277                     /*
278                      * Lock, deref and release previously created record channel
279                      */
280                     CHN_LOCK(rdch);
281                     pcm_chnref(rdch, -1);
282                     pcm_chnrelease(rdch);
283                 }
284
285                 return error;
286             }
287
288             pcm_chnref(wrch, 1);
289             CHN_UNLOCK(wrch);
290             pcm_lock(d);
291         }
292
293         i_dev->si_drv1 = rdch;
294         i_dev->si_drv2 = wrch;
295
296         pcm_unlock(d);
297         return 0;
298 }
299
300 static int
301 dsp_close(struct dev_close_args *ap)
302 {
303         struct cdev *i_dev = ap->a_head.a_dev;
304         struct pcm_channel *rdch, *wrch;
305         struct snddev_info *d;
306         int refs;
307
308         d = dsp_get_info(i_dev);
309         pcm_lock(d);
310         rdch = i_dev->si_drv1;
311         wrch = i_dev->si_drv2;
312         pcm_unlock(d);
313
314         if (rdch || wrch) {
315                 refs = 0;
316                 if (rdch) {
317                         CHN_LOCK(rdch);
318                         refs += pcm_chnref(rdch, -1);
319                         chn_abort(rdch); /* won't sleep */
320                         rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
321                         chn_reset(rdch, 0);
322                         pcm_chnrelease(rdch);
323                 }
324                 if (wrch) {
325                         CHN_LOCK(wrch);
326                         refs += pcm_chnref(wrch, -1);
327                         /*
328                          * XXX: Maybe the right behaviour is to abort on non_block.
329                          * It seems that mplayer flushes the audio queue by quickly
330                          * closing and re-opening.  In FBSD, there's a long pause
331                          * while the audio queue flushes that I presume isn't there in
332                          * linux.
333                          */
334                         chn_flush(wrch); /* may sleep */
335                         wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
336                         chn_reset(wrch, 0);
337                         pcm_chnrelease(wrch);
338                 }
339
340                 pcm_lock(d);
341                 if (rdch)
342                         i_dev->si_drv1 = NULL;
343                 if (wrch)
344                         i_dev->si_drv2 = NULL;
345                 /*
346                  * If there are no more references, release the channels.
347                  */
348                 if (refs == 0 && i_dev->si_drv1 == NULL &&
349                             i_dev->si_drv2 == NULL) {
350                         if (pcm_getfakechan(d))
351                                 pcm_getfakechan(d)->flags = 0;
352                         /* What is this?!? */
353                         dsp_set_flags(i_dev, dsp_get_flags(i_dev) & ~SD_F_TRANSIENT);
354                 }
355                 pcm_unlock(d);
356         }
357         return 0;
358 }
359
360 static int
361 dsp_read(struct dev_read_args *ap)
362 {
363         struct cdev *i_dev = ap->a_head.a_dev;
364         struct uio *buf = ap->a_uio;
365         int flag = ap->a_ioflag;
366         struct pcm_channel *rdch, *wrch;
367         int ret;
368
369         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD);
370
371         KASSERT(rdch, ("dsp_read: nonexistant channel"));
372         KASSERT(rdch->flags & CHN_F_BUSY, ("dsp_read: nonbusy channel"));
373
374         if (rdch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
375                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
376                 return EINVAL;
377         }
378         if (!(rdch->flags & CHN_F_RUNNING))
379                 rdch->flags |= CHN_F_RUNNING;
380         ret = chn_read(rdch, buf, flag);
381         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD);
382
383         return ret;
384 }
385
386 static int
387 dsp_write(struct dev_write_args *ap)
388 {
389         struct cdev *i_dev = ap->a_head.a_dev;
390         struct uio *buf = ap->a_uio;
391         int flag = ap->a_ioflag;
392         struct pcm_channel *rdch, *wrch;
393         int ret;
394
395         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_WR);
396
397         KASSERT(wrch, ("dsp_write: nonexistant channel"));
398         KASSERT(wrch->flags & CHN_F_BUSY, ("dsp_write: nonbusy channel"));
399
400         if (wrch->flags & (CHN_F_MAPPED | CHN_F_DEAD)) {
401                 relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
402                 return EINVAL;
403         }
404         if (!(wrch->flags & CHN_F_RUNNING))
405                 wrch->flags |= CHN_F_RUNNING;
406         ret = chn_write(wrch, buf, flag);
407         relchns(i_dev, rdch, wrch, SD_F_PRIO_WR);
408
409         return ret;
410 }
411
412 static int
413 dsp_ioctl(struct dev_ioctl_args *ap)
414 {
415         struct cdev *i_dev = ap->a_head.a_dev;
416         u_long cmd = ap->a_cmd;
417         caddr_t arg = ap->a_data;
418         struct pcm_channel *chn, *rdch, *wrch;
419         struct snddev_info *d;
420         int kill;
421         int ret = 0, *arg_i = (int *)arg, tmp;
422
423         d = dsp_get_info(i_dev);
424         getchns(i_dev, &rdch, &wrch, 0);
425
426         kill = 0;
427         if (wrch && (wrch->flags & CHN_F_DEAD))
428                 kill |= 1;
429         if (rdch && (rdch->flags & CHN_F_DEAD))
430                 kill |= 2;
431         if (kill == 3) {
432                 relchns(i_dev, rdch, wrch, 0);
433                 return EINVAL;
434         }
435         if (kill & 1)
436                 wrch = NULL;
437         if (kill & 2)
438                 rdch = NULL;
439
440         /*
441          * 4Front OSS specifies that dsp devices allow mixer controls to
442          * control PCM == their volume.
443          */
444         if (IOCGROUP(cmd) == 'M') {
445                 /*
446                  * For now only set the channel volume for vchans, pass
447                  * all others to the mixer.
448                  */
449                 if (wrch != NULL && wrch->flags & CHN_F_VIRTUAL &&
450                     (cmd & 0xff) == SOUND_MIXER_PCM) {
451                         if ((cmd & MIXER_WRITE(0)) == MIXER_WRITE(0)) {
452                                 int vol_raw = *(int *)arg;
453                                 int vol_left, vol_right;
454
455                                 vol_left = min(vol_raw & 0x00ff, 100);
456                                 vol_right = min((vol_raw & 0xff00) >> 8, 100);
457                                 ret = chn_setvolume(wrch, vol_left, vol_right);
458                         } else {
459                                 *(int *)arg = wrch->volume;
460                         }
461                 } else {
462                         ap->a_head.a_dev = d->mixer_dev;
463                         ret = mixer_ioctl(ap);
464                 }
465
466                 relchns(i_dev, rdch, wrch, 0);
467                 return ret;
468         }
469         
470         switch(cmd) {
471 #ifdef OLDPCM_IOCTL
472         /*
473          * we start with the new ioctl interface.
474          */
475         case AIONWRITE: /* how many bytes can write ? */
476                 if (wrch) {
477                         CHN_LOCK(wrch);
478 /*
479                 if (wrch && wrch->bufhard.dl)
480                         while (chn_wrfeed(wrch) == 0);
481 */
482                         *arg_i = sndbuf_getfree(wrch->bufsoft);
483                         CHN_UNLOCK(wrch);
484                 } else {
485                         *arg_i = 0;
486                         ret = EINVAL;
487                 }
488                 break;
489
490         case AIOSSIZE:     /* set the current blocksize */
491                 {
492                         struct snd_size *p = (struct snd_size *)arg;
493
494                         p->play_size = 0;
495                         p->rec_size = 0;
496                         if (wrch) {
497                                 CHN_LOCK(wrch);
498                                 chn_setblocksize(wrch, 2, p->play_size);
499                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
500                                 CHN_UNLOCK(wrch);
501                         }
502                         if (rdch) {
503                                 CHN_LOCK(rdch);
504                                 chn_setblocksize(rdch, 2, p->rec_size);
505                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
506                                 CHN_UNLOCK(rdch);
507                         }
508                 }
509                 break;
510         case AIOGSIZE:  /* get the current blocksize */
511                 {
512                         struct snd_size *p = (struct snd_size *)arg;
513
514                         if (wrch) {
515                                 CHN_LOCK(wrch);
516                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
517                                 CHN_UNLOCK(wrch);
518                         }
519                         if (rdch) {
520                                 CHN_LOCK(rdch);
521                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
522                                 CHN_UNLOCK(rdch);
523                         }
524                 }
525                 break;
526
527         case AIOSFMT:
528         case AIOGFMT:
529                 {
530                         snd_chan_param *p = (snd_chan_param *)arg;
531
532                         if (cmd == AIOSFMT &&
533                             ((p->play_format != 0 && p->play_rate == 0) ||
534                             (p->rec_format != 0 && p->rec_rate == 0))) {
535                                 ret = EINVAL;
536                                 break;
537                         }
538                         if (wrch) {
539                                 CHN_LOCK(wrch);
540                                 if (cmd == AIOSFMT && p->play_format != 0) {
541                                         chn_setformat(wrch, p->play_format);
542                                         chn_setspeed(wrch, p->play_rate);
543                                 }
544                                 p->play_rate = wrch->speed;
545                                 p->play_format = wrch->format;
546                                 CHN_UNLOCK(wrch);
547                         } else {
548                                 p->play_rate = 0;
549                                 p->play_format = 0;
550                         }
551                         if (rdch) {
552                                 CHN_LOCK(rdch);
553                                 if (cmd == AIOSFMT && p->rec_format != 0) {
554                                         chn_setformat(rdch, p->rec_format);
555                                         chn_setspeed(rdch, p->rec_rate);
556                                 }
557                                 p->rec_rate = rdch->speed;
558                                 p->rec_format = rdch->format;
559                                 CHN_UNLOCK(rdch);
560                         } else {
561                                 p->rec_rate = 0;
562                                 p->rec_format = 0;
563                         }
564                 }
565                 break;
566
567         case AIOGCAP:     /* get capabilities */
568                 {
569                         snd_capabilities *p = (snd_capabilities *)arg;
570                         struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
571                         struct cdev *pdev;
572
573                         if (rdch) {
574                                 CHN_LOCK(rdch);
575                                 rcaps = chn_getcaps(rdch);
576                         }
577                         if (wrch) {
578                                 CHN_LOCK(wrch);
579                                 pcaps = chn_getcaps(wrch);
580                         }
581                         p->rate_min = max(rcaps? rcaps->minspeed : 0,
582                                           pcaps? pcaps->minspeed : 0);
583                         p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
584                                           pcaps? pcaps->maxspeed : 1000000);
585                         p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
586                                          wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
587                         /* XXX bad on sb16 */
588                         p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
589                                      (wrch? chn_getformats(wrch) : 0xffffffff);
590                         if (rdch && wrch)
591                                 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
592                         pdev = d->mixer_dev;
593                         p->mixers = 1; /* default: one mixer */
594                         p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
595                         p->left = p->right = 100;
596                         if (rdch)
597                                 CHN_UNLOCK(rdch);
598                         if (wrch)
599                                 CHN_UNLOCK(wrch);
600                 }
601                 break;
602
603         case AIOSTOP:
604                 if (*arg_i == AIOSYNC_PLAY && wrch) {
605                         CHN_LOCK(wrch);
606                         *arg_i = chn_abort(wrch);
607                         CHN_UNLOCK(wrch);
608                 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
609                         CHN_LOCK(rdch);
610                         *arg_i = chn_abort(rdch);
611                         CHN_UNLOCK(rdch);
612                 } else {
613                         kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
614                         *arg_i = 0;
615                 }
616                 break;
617
618         case AIOSYNC:
619                 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
620                         ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
621                 break;
622 #endif
623         /*
624          * here follow the standard ioctls (filio.h etc.)
625          */
626         case FIONREAD: /* get # bytes to read */
627                 if (rdch) {
628                         CHN_LOCK(rdch);
629 /*                      if (rdch && rdch->bufhard.dl)
630                                 while (chn_rdfeed(rdch) == 0);
631 */
632                         *arg_i = sndbuf_getready(rdch->bufsoft);
633                         CHN_UNLOCK(rdch);
634                 } else {
635                         *arg_i = 0;
636                         ret = EINVAL;
637                 }
638                 break;
639
640         case FIOASYNC: /*set/clear async i/o */
641                 DEB( kprintf("FIOASYNC\n") ; )
642                 break;
643
644         case SNDCTL_DSP_NONBLOCK:
645         case FIONBIO: /* set/clear non-blocking i/o */
646                 if (rdch) {
647                         CHN_LOCK(rdch);
648                         if (*arg_i)
649                                 rdch->flags |= CHN_F_NBIO;
650                         else
651                                 rdch->flags &= ~CHN_F_NBIO;
652                         CHN_UNLOCK(rdch);
653                 }
654                 if (wrch) {
655                         CHN_LOCK(wrch);
656                         if (*arg_i)
657                                 wrch->flags |= CHN_F_NBIO;
658                         else
659                                 wrch->flags &= ~CHN_F_NBIO;
660                         CHN_UNLOCK(wrch);
661                 }
662                 break;
663
664         /*
665          * Finally, here is the linux-compatible ioctl interface
666          */
667 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
668         case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
669         case SNDCTL_DSP_GETBLKSIZE:
670                 chn = wrch ? wrch : rdch;
671                 if (chn) {
672                         CHN_LOCK(chn);
673                         *arg_i = sndbuf_getblksz(chn->bufsoft);
674                         CHN_UNLOCK(chn);
675                 } else {
676                         *arg_i = 0;
677                         ret = EINVAL;
678                 }
679                 break ;
680
681         case SNDCTL_DSP_SETBLKSIZE:
682                 RANGE(*arg_i, 16, 65536);
683                 if (wrch) {
684                         CHN_LOCK(wrch);
685                         chn_setblocksize(wrch, 2, *arg_i);
686                         CHN_UNLOCK(wrch);
687                 }
688                 if (rdch) {
689                         CHN_LOCK(rdch);
690                         chn_setblocksize(rdch, 2, *arg_i);
691                         CHN_UNLOCK(rdch);
692                 }
693                 break;
694
695         case SNDCTL_DSP_RESET:
696                 DEB(kprintf("dsp reset\n"));
697                 if (wrch) {
698                         CHN_LOCK(wrch);
699                         chn_abort(wrch);
700                         chn_resetbuf(wrch);
701                         CHN_UNLOCK(wrch);
702                 }
703                 if (rdch) {
704                         CHN_LOCK(rdch);
705                         chn_abort(rdch);
706                         chn_resetbuf(rdch);
707                         CHN_UNLOCK(rdch);
708                 }
709                 break;
710
711         case SNDCTL_DSP_SYNC:
712                 DEB(kprintf("dsp sync\n"));
713                 /* chn_sync may sleep */
714                 if (wrch) {
715                         CHN_LOCK(wrch);
716                         chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
717                         CHN_UNLOCK(wrch);
718                 }
719                 break;
720
721         case SNDCTL_DSP_SPEED:
722                 /* chn_setspeed may sleep */
723                 tmp = 0;
724                 if (wrch) {
725                         CHN_LOCK(wrch);
726                         ret = chn_setspeed(wrch, *arg_i);
727                         tmp = wrch->speed;
728                         CHN_UNLOCK(wrch);
729                 }
730                 if (rdch && ret == 0) {
731                         CHN_LOCK(rdch);
732                         ret = chn_setspeed(rdch, *arg_i);
733                         if (tmp == 0)
734                                 tmp = rdch->speed;
735                         CHN_UNLOCK(rdch);
736                 }
737                 *arg_i = tmp;
738                 break;
739
740         case SOUND_PCM_READ_RATE:
741                 chn = wrch ? wrch : rdch;
742                 if (chn) {
743                         CHN_LOCK(chn);
744                         *arg_i = chn->speed;
745                         CHN_UNLOCK(chn);
746                 } else {
747                         *arg_i = 0;
748                         ret = EINVAL;
749                 }
750                 break;
751
752         case SNDCTL_DSP_STEREO:
753                 tmp = -1;
754                 *arg_i = (*arg_i)? AFMT_STEREO : 0;
755                 if (wrch) {
756                         CHN_LOCK(wrch);
757                         ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
758                         tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
759                         CHN_UNLOCK(wrch);
760                 }
761                 if (rdch && ret == 0) {
762                         CHN_LOCK(rdch);
763                         ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
764                         if (tmp == -1)
765                                 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
766                         CHN_UNLOCK(rdch);
767                 }
768                 *arg_i = tmp;
769                 break;
770
771         case SOUND_PCM_WRITE_CHANNELS:
772 /*      case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
773                 if (*arg_i != 0) {
774                         tmp = 0;
775                         *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
776                         if (wrch) {
777                                 CHN_LOCK(wrch);
778                                 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
779                                 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
780                                 CHN_UNLOCK(wrch);
781                         }
782                         if (rdch && ret == 0) {
783                                 CHN_LOCK(rdch);
784                                 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
785                                 if (tmp == 0)
786                                         tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
787                                 CHN_UNLOCK(rdch);
788                         }
789                         *arg_i = tmp;
790                 } else {
791                         chn = wrch ? wrch : rdch;
792                         CHN_LOCK(chn);
793                         *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
794                         CHN_UNLOCK(chn);
795                 }
796                 break;
797
798         case SOUND_PCM_READ_CHANNELS:
799                 chn = wrch ? wrch : rdch;
800                 if (chn) {
801                         CHN_LOCK(chn);
802                         *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
803                         CHN_UNLOCK(chn);
804                 } else {
805                         *arg_i = 0;
806                         ret = EINVAL;
807                 }
808                 break;
809
810         case SNDCTL_DSP_GETFMTS:        /* returns a mask of supported fmts */
811                 chn = wrch ? wrch : rdch;
812                 if (chn) {
813                         CHN_LOCK(chn);
814                         *arg_i = chn_getformats(chn);
815                         CHN_UNLOCK(chn);
816                 } else {
817                         *arg_i = 0;
818                         ret = EINVAL;
819                 }
820                 break ;
821
822         case SNDCTL_DSP_SETFMT: /* sets _one_ format */
823                 if ((*arg_i != AFMT_QUERY)) {
824                         tmp = 0;
825                         if (wrch) {
826                                 CHN_LOCK(wrch);
827                                 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
828                                 tmp = wrch->format & ~AFMT_STEREO;
829                                 CHN_UNLOCK(wrch);
830                         }
831                         if (rdch && ret == 0) {
832                                 CHN_LOCK(rdch);
833                                 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
834                                 if (tmp == 0)
835                                         tmp = rdch->format & ~AFMT_STEREO;
836                                 CHN_UNLOCK(rdch);
837                         }
838                         *arg_i = tmp;
839                 } else {
840                         chn = wrch ? wrch : rdch;
841                         CHN_LOCK(chn);
842                         *arg_i = chn->format & ~AFMT_STEREO;
843                         CHN_UNLOCK(chn);
844                 }
845                 break;
846
847         case SNDCTL_DSP_SETFRAGMENT:
848                 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
849                 {
850                         u_int32_t fragln = (*arg_i) & 0x0000ffff;
851                         u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
852                         u_int32_t fragsz;
853                         u_int32_t r_maxfrags, r_fragsz;
854
855                         RANGE(fragln, 4, 16);
856                         fragsz = 1 << fragln;
857
858                         if (maxfrags == 0)
859                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
860                         if (maxfrags < 2)
861                                 maxfrags = 2;
862                         if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
863                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
864
865                         DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
866                         if (rdch) {
867                                 CHN_LOCK(rdch);
868                                 ret = chn_setblocksize(rdch, maxfrags, fragsz);
869                                 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
870                                 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
871                                 CHN_UNLOCK(rdch);
872                         } else {
873                                 r_maxfrags = maxfrags;
874                                 r_fragsz = fragsz;
875                         }
876                         if (wrch && ret == 0) {
877                                 CHN_LOCK(wrch);
878                                 ret = chn_setblocksize(wrch, maxfrags, fragsz);
879                                 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
880                                 fragsz = sndbuf_getblksz(wrch->bufsoft);
881                                 CHN_UNLOCK(wrch);
882                         } else { /* use whatever came from the read channel */
883                                 maxfrags = r_maxfrags;
884                                 fragsz = r_fragsz;
885                         }
886
887                         fragln = 0;
888                         while (fragsz > 1) {
889                                 fragln++;
890                                 fragsz >>= 1;
891                         }
892                         *arg_i = (maxfrags << 16) | fragln;
893                 }
894                 break;
895
896         case SNDCTL_DSP_GETISPACE:
897                 /* return the size of data available in the input queue */
898                 {
899                         audio_buf_info *a = (audio_buf_info *)arg;
900                         if (rdch) {
901                                 struct snd_dbuf *bs = rdch->bufsoft;
902
903                                 CHN_LOCK(rdch);
904                                 a->bytes = sndbuf_getready(bs);
905                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
906                                 a->fragstotal = sndbuf_getblkcnt(bs);
907                                 a->fragsize = sndbuf_getblksz(bs);
908                                 CHN_UNLOCK(rdch);
909                         }
910                 }
911                 break;
912
913         case SNDCTL_DSP_GETOSPACE:
914                 /* return space available in the output queue */
915                 {
916                         audio_buf_info *a = (audio_buf_info *)arg;
917                         if (wrch) {
918                                 struct snd_dbuf *bs = wrch->bufsoft;
919
920                                 CHN_LOCK(wrch);
921                                 /* XXX abusive DMA update: chn_wrupdate(wrch); */
922                                 a->bytes = sndbuf_getfree(bs);
923                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
924                                 a->fragstotal = sndbuf_getblkcnt(bs);
925                                 a->fragsize = sndbuf_getblksz(bs);
926                                 CHN_UNLOCK(wrch);
927                         }
928                 }
929                 break;
930
931         case SNDCTL_DSP_GETIPTR:
932                 {
933                         count_info *a = (count_info *)arg;
934                         if (rdch) {
935                                 struct snd_dbuf *bs = rdch->bufsoft;
936
937                                 CHN_LOCK(rdch);
938                                 /* XXX abusive DMA update: chn_rdupdate(rdch); */
939                                 a->bytes = sndbuf_gettotal(bs);
940                                 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
941                                 a->ptr = sndbuf_getreadyptr(bs);
942                                 rdch->blocks = sndbuf_getblocks(bs);
943                                 CHN_UNLOCK(rdch);
944                         } else
945                                 ret = EINVAL;
946                 }
947                 break;
948
949         case SNDCTL_DSP_GETOPTR:
950                 {
951                         count_info *a = (count_info *)arg;
952                         if (wrch) {
953                                 struct snd_dbuf *bs = wrch->bufsoft;
954
955                                 CHN_LOCK(wrch);
956                                 /* XXX abusive DMA update: chn_wrupdate(wrch); */
957                                 a->bytes = sndbuf_gettotal(bs);
958                                 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
959                                 a->ptr = sndbuf_getreadyptr(bs);
960                                 wrch->blocks = sndbuf_getblocks(bs);
961                                 CHN_UNLOCK(wrch);
962                         } else
963                                 ret = EINVAL;
964                 }
965                 break;
966
967         case SNDCTL_DSP_GETCAPS:
968                 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
969                 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
970                         *arg_i |= DSP_CAP_DUPLEX;
971                 break;
972
973         case SOUND_PCM_READ_BITS:
974                 chn = wrch ? wrch : rdch;
975                 if (chn) {
976                         CHN_LOCK(chn);
977                         if (chn->format & AFMT_8BIT)
978                                 *arg_i = 8;
979                         else if (chn->format & AFMT_16BIT)
980                                 *arg_i = 16;
981                         else if (chn->format & AFMT_24BIT)
982                                 *arg_i = 24;
983                         else if (chn->format & AFMT_32BIT)
984                                 *arg_i = 32;
985                         else
986                                 ret = EINVAL;
987                         CHN_UNLOCK(chn);
988                 } else {
989                         *arg_i = 0;
990                         ret = EINVAL;
991                 }
992                 break;
993
994         case SNDCTL_DSP_SETTRIGGER:
995                 if (rdch) {
996                         CHN_LOCK(rdch);
997                         rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
998                         if (*arg_i & PCM_ENABLE_INPUT)
999                                 chn_start(rdch, 1);
1000                         else
1001                                 rdch->flags |= CHN_F_NOTRIGGER;
1002                         CHN_UNLOCK(rdch);
1003                 }
1004                 if (wrch) {
1005                         CHN_LOCK(wrch);
1006                         wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1007                         if (*arg_i & PCM_ENABLE_OUTPUT)
1008                                 chn_start(wrch, 1);
1009                         else
1010                                 wrch->flags |= CHN_F_NOTRIGGER;
1011                         CHN_UNLOCK(wrch);
1012                 }
1013                 break;
1014
1015         case SNDCTL_DSP_GETTRIGGER:
1016                 *arg_i = 0;
1017                 if (wrch) {
1018                         CHN_LOCK(wrch);
1019                         if (wrch->flags & CHN_F_TRIGGERED)
1020                                 *arg_i |= PCM_ENABLE_OUTPUT;
1021                         CHN_UNLOCK(wrch);
1022                 }
1023                 if (rdch) {
1024                         CHN_LOCK(rdch);
1025                         if (rdch->flags & CHN_F_TRIGGERED)
1026                                 *arg_i |= PCM_ENABLE_INPUT;
1027                         CHN_UNLOCK(rdch);
1028                 }
1029                 break;
1030
1031         case SNDCTL_DSP_GETODELAY:
1032                 if (wrch) {
1033                         struct snd_dbuf *b = wrch->bufhard;
1034                         struct snd_dbuf *bs = wrch->bufsoft;
1035
1036                         CHN_LOCK(wrch);
1037                         /* XXX abusive DMA update: chn_wrupdate(wrch); */
1038                         *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1039                         CHN_UNLOCK(wrch);
1040                 } else
1041                         ret = EINVAL;
1042                 break;
1043
1044         case SNDCTL_DSP_POST:
1045                 if (wrch) {
1046                         CHN_LOCK(wrch);
1047                         wrch->flags &= ~CHN_F_NOTRIGGER;
1048                         chn_start(wrch, 1);
1049                         CHN_UNLOCK(wrch);
1050                 }
1051                 break;
1052
1053         case SNDCTL_DSP_SETDUPLEX:
1054                 /*
1055                  * switch to full-duplex mode if card is in half-duplex
1056                  * mode and is able to work in full-duplex mode
1057                  */
1058                 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1059                         dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1060                 break;
1061
1062         case SNDCTL_DSP_MAPINBUF:
1063         case SNDCTL_DSP_MAPOUTBUF:
1064         case SNDCTL_DSP_SETSYNCRO:
1065                 /* undocumented */
1066
1067         case SNDCTL_DSP_SUBDIVIDE:
1068         case SOUND_PCM_WRITE_FILTER:
1069         case SOUND_PCM_READ_FILTER:
1070                 /* dunno what these do, don't sound important */
1071
1072         default:
1073                 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
1074                 ret = EINVAL;
1075                 break;
1076         }
1077         relchns(i_dev, rdch, wrch, 0);
1078         return ret;
1079 }
1080
1081 static int
1082 dsp_poll(struct dev_poll_args *ap)
1083 {
1084         struct cdev *i_dev = ap->a_head.a_dev;
1085         int events = ap->a_events;
1086         struct thread *td = curthread;
1087         struct pcm_channel *wrch = NULL, *rdch = NULL;
1088         int ret, e;
1089
1090         ret = 0;
1091         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1092
1093         if (wrch) {
1094                 e = (events & (POLLOUT | POLLWRNORM));
1095                 if (e)
1096                         ret |= chn_poll(wrch, e, td);
1097         }
1098         if (rdch) {
1099                 e = (events & (POLLIN | POLLRDNORM));
1100                 if (e)
1101                         ret |= chn_poll(rdch, e, td);
1102         }
1103         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1104
1105         ap->a_events = ret;
1106         return (0);
1107 }
1108
1109 static int
1110 dsp_mmap(struct dev_mmap_args *ap)
1111 {
1112         struct cdev *i_dev = ap->a_head.a_dev;
1113         vm_offset_t offset = ap->a_offset;
1114         int nprot = ap->a_nprot;
1115         struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1116
1117         if (nprot & PROT_EXEC)
1118                 return -1;
1119
1120         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1121 #if 0
1122         /*
1123          * XXX the linux api uses the nprot to select read/write buffer
1124          * our vm system doesn't allow this, so force write buffer
1125          */
1126
1127         if (wrch && (nprot & PROT_WRITE)) {
1128                 c = wrch;
1129         } else if (rdch && (nprot & PROT_READ)) {
1130                 c = rdch;
1131         } else {
1132                 return -1;
1133         }
1134 #else
1135         c = wrch;
1136 #endif
1137
1138         if (c == NULL) {
1139                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1140                 return -1;
1141         }
1142
1143         if (offset >= sndbuf_getsize(c->bufsoft)) {
1144                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1145                 return -1;
1146         }
1147
1148         if (!(c->flags & CHN_F_MAPPED))
1149                 c->flags |= CHN_F_MAPPED;
1150
1151         ap->a_result = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1152         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1153
1154         return (0);
1155 }
1156
1157 #ifdef USING_DEVFS
1158
1159 /*
1160  * Clone logic is this:
1161  * x E X = {dsp, dspW, audio}
1162  * x -> x${sysctl("hw.snd.unit")}
1163  * xN->
1164  *    for i N = 1 to channels of device N
1165  *      if xN.i isn't busy, return its dev_t
1166  */
1167 static void
1168 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1169     struct cdev **dev)
1170 {
1171         struct cdev *pdev;
1172         struct snddev_info *pcm_dev;
1173         struct snddev_channel *pcm_chan;
1174         int i, unit, devtype;
1175         static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1176         static char *devnames[3] = {"dsp", "dspW", "audio"};
1177
1178         if (*dev != NULL)
1179                 return;
1180         if (pcm_devclass == NULL)
1181                 return;
1182
1183         devtype = 0;
1184         unit = -1;
1185         for (i = 0; (i < 3) && (unit == -1); i++) {
1186                 devtype = devtypes[i];
1187                 if (strcmp(name, devnames[i]) == 0) {
1188                         unit = snd_unit;
1189                 } else {
1190                         if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1191                                 unit = -1;
1192                 }
1193         }
1194         if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1195                 return;
1196
1197         pcm_dev = devclass_get_softc(pcm_devclass, unit);
1198
1199         if (pcm_dev == NULL)
1200                 return;
1201
1202         SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1203
1204                 switch(devtype) {
1205                         case SND_DEV_DSP:
1206                                 pdev = pcm_chan->dsp_devt;
1207                                 break;
1208                         case SND_DEV_DSP16:
1209                                 pdev = pcm_chan->dspW_devt;
1210                                 break;
1211                         case SND_DEV_AUDIO:
1212                                 pdev = pcm_chan->audio_devt;
1213                                 break;
1214                         default:
1215                                 panic("Unknown devtype %d", devtype);
1216                 }
1217
1218                 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1219                         *dev = pdev;
1220                         dev_ref(*dev);
1221                         return;
1222                 }
1223         }
1224 }
1225
1226 static void
1227 dsp_sysinit(void *p)
1228 {
1229         dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1230 }
1231
1232 static void
1233 dsp_sysuninit(void *p)
1234 {
1235         if (dsp_ehtag != NULL)
1236                 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1237 }
1238
1239 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1240 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1241 #endif
1242
1243