37f5c1931b7585f6a04814aff95666c6abf13dfe
[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.16 2007/10/08 17:55:00 hasso 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.16 2007/10/08 17:55:00 hasso 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                 crit_exit();
468                 return ret;
469         }
470         
471         switch(cmd) {
472 #ifdef OLDPCM_IOCTL
473         /*
474          * we start with the new ioctl interface.
475          */
476         case AIONWRITE: /* how many bytes can write ? */
477                 if (wrch) {
478                         CHN_LOCK(wrch);
479 /*
480                 if (wrch && wrch->bufhard.dl)
481                         while (chn_wrfeed(wrch) == 0);
482 */
483                         *arg_i = sndbuf_getfree(wrch->bufsoft);
484                         CHN_UNLOCK(wrch);
485                 } else {
486                         *arg_i = 0;
487                         ret = EINVAL;
488                 }
489                 break;
490
491         case AIOSSIZE:     /* set the current blocksize */
492                 {
493                         struct snd_size *p = (struct snd_size *)arg;
494
495                         p->play_size = 0;
496                         p->rec_size = 0;
497                         if (wrch) {
498                                 CHN_LOCK(wrch);
499                                 chn_setblocksize(wrch, 2, p->play_size);
500                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
501                                 CHN_UNLOCK(wrch);
502                         }
503                         if (rdch) {
504                                 CHN_LOCK(rdch);
505                                 chn_setblocksize(rdch, 2, p->rec_size);
506                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
507                                 CHN_UNLOCK(rdch);
508                         }
509                 }
510                 break;
511         case AIOGSIZE:  /* get the current blocksize */
512                 {
513                         struct snd_size *p = (struct snd_size *)arg;
514
515                         if (wrch) {
516                                 CHN_LOCK(wrch);
517                                 p->play_size = sndbuf_getblksz(wrch->bufsoft);
518                                 CHN_UNLOCK(wrch);
519                         }
520                         if (rdch) {
521                                 CHN_LOCK(rdch);
522                                 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
523                                 CHN_UNLOCK(rdch);
524                         }
525                 }
526                 break;
527
528         case AIOSFMT:
529         case AIOGFMT:
530                 {
531                         snd_chan_param *p = (snd_chan_param *)arg;
532
533                         if (cmd == AIOSFMT &&
534                             ((p->play_format != 0 && p->play_rate == 0) ||
535                             (p->rec_format != 0 && p->rec_rate == 0))) {
536                                 ret = EINVAL;
537                                 break;
538                         }
539                         if (wrch) {
540                                 CHN_LOCK(wrch);
541                                 if (cmd == AIOSFMT && p->play_format != 0) {
542                                         chn_setformat(wrch, p->play_format);
543                                         chn_setspeed(wrch, p->play_rate);
544                                 }
545                                 p->play_rate = wrch->speed;
546                                 p->play_format = wrch->format;
547                                 CHN_UNLOCK(wrch);
548                         } else {
549                                 p->play_rate = 0;
550                                 p->play_format = 0;
551                         }
552                         if (rdch) {
553                                 CHN_LOCK(rdch);
554                                 if (cmd == AIOSFMT && p->rec_format != 0) {
555                                         chn_setformat(rdch, p->rec_format);
556                                         chn_setspeed(rdch, p->rec_rate);
557                                 }
558                                 p->rec_rate = rdch->speed;
559                                 p->rec_format = rdch->format;
560                                 CHN_UNLOCK(rdch);
561                         } else {
562                                 p->rec_rate = 0;
563                                 p->rec_format = 0;
564                         }
565                 }
566                 break;
567
568         case AIOGCAP:     /* get capabilities */
569                 {
570                         snd_capabilities *p = (snd_capabilities *)arg;
571                         struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
572                         struct cdev *pdev;
573
574                         if (rdch) {
575                                 CHN_LOCK(rdch);
576                                 rcaps = chn_getcaps(rdch);
577                         }
578                         if (wrch) {
579                                 CHN_LOCK(wrch);
580                                 pcaps = chn_getcaps(wrch);
581                         }
582                         p->rate_min = max(rcaps? rcaps->minspeed : 0,
583                                           pcaps? pcaps->minspeed : 0);
584                         p->rate_max = min(rcaps? rcaps->maxspeed : 1000000,
585                                           pcaps? pcaps->maxspeed : 1000000);
586                         p->bufsize = min(rdch? sndbuf_getsize(rdch->bufsoft) : 1000000,
587                                          wrch? sndbuf_getsize(wrch->bufsoft) : 1000000);
588                         /* XXX bad on sb16 */
589                         p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
590                                      (wrch? chn_getformats(wrch) : 0xffffffff);
591                         if (rdch && wrch)
592                                 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
593                         pdev = d->mixer_dev;
594                         p->mixers = 1; /* default: one mixer */
595                         p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
596                         p->left = p->right = 100;
597                         if (rdch)
598                                 CHN_UNLOCK(rdch);
599                         if (wrch)
600                                 CHN_UNLOCK(wrch);
601                 }
602                 break;
603
604         case AIOSTOP:
605                 if (*arg_i == AIOSYNC_PLAY && wrch) {
606                         CHN_LOCK(wrch);
607                         *arg_i = chn_abort(wrch);
608                         CHN_UNLOCK(wrch);
609                 } else if (*arg_i == AIOSYNC_CAPTURE && rdch) {
610                         CHN_LOCK(rdch);
611                         *arg_i = chn_abort(rdch);
612                         CHN_UNLOCK(rdch);
613                 } else {
614                         kprintf("AIOSTOP: bad channel 0x%x\n", *arg_i);
615                         *arg_i = 0;
616                 }
617                 break;
618
619         case AIOSYNC:
620                 kprintf("AIOSYNC chan 0x%03lx pos %lu unimplemented\n",
621                         ((snd_sync_parm *)arg)->chan, ((snd_sync_parm *)arg)->pos);
622                 break;
623 #endif
624         /*
625          * here follow the standard ioctls (filio.h etc.)
626          */
627         case FIONREAD: /* get # bytes to read */
628                 if (rdch) {
629                         CHN_LOCK(rdch);
630 /*                      if (rdch && rdch->bufhard.dl)
631                                 while (chn_rdfeed(rdch) == 0);
632 */
633                         *arg_i = sndbuf_getready(rdch->bufsoft);
634                         CHN_UNLOCK(rdch);
635                 } else {
636                         *arg_i = 0;
637                         ret = EINVAL;
638                 }
639                 break;
640
641         case FIOASYNC: /*set/clear async i/o */
642                 DEB( kprintf("FIOASYNC\n") ; )
643                 break;
644
645         case SNDCTL_DSP_NONBLOCK:
646         case FIONBIO: /* set/clear non-blocking i/o */
647                 if (rdch) {
648                         CHN_LOCK(rdch);
649                         if (*arg_i)
650                                 rdch->flags |= CHN_F_NBIO;
651                         else
652                                 rdch->flags &= ~CHN_F_NBIO;
653                         CHN_UNLOCK(rdch);
654                 }
655                 if (wrch) {
656                         CHN_LOCK(wrch);
657                         if (*arg_i)
658                                 wrch->flags |= CHN_F_NBIO;
659                         else
660                                 wrch->flags &= ~CHN_F_NBIO;
661                         CHN_UNLOCK(wrch);
662                 }
663                 break;
664
665         /*
666          * Finally, here is the linux-compatible ioctl interface
667          */
668 #define THE_REAL_SNDCTL_DSP_GETBLKSIZE _IOWR('P', 4, int)
669         case THE_REAL_SNDCTL_DSP_GETBLKSIZE:
670         case SNDCTL_DSP_GETBLKSIZE:
671                 chn = wrch ? wrch : rdch;
672                 if (chn) {
673                         CHN_LOCK(chn);
674                         *arg_i = sndbuf_getblksz(chn->bufsoft);
675                         CHN_UNLOCK(chn);
676                 } else {
677                         *arg_i = 0;
678                         ret = EINVAL;
679                 }
680                 break ;
681
682         case SNDCTL_DSP_SETBLKSIZE:
683                 RANGE(*arg_i, 16, 65536);
684                 if (wrch) {
685                         CHN_LOCK(wrch);
686                         chn_setblocksize(wrch, 2, *arg_i);
687                         CHN_UNLOCK(wrch);
688                 }
689                 if (rdch) {
690                         CHN_LOCK(rdch);
691                         chn_setblocksize(rdch, 2, *arg_i);
692                         CHN_UNLOCK(rdch);
693                 }
694                 break;
695
696         case SNDCTL_DSP_RESET:
697                 DEB(kprintf("dsp reset\n"));
698                 if (wrch) {
699                         CHN_LOCK(wrch);
700                         chn_abort(wrch);
701                         chn_resetbuf(wrch);
702                         CHN_UNLOCK(wrch);
703                 }
704                 if (rdch) {
705                         CHN_LOCK(rdch);
706                         chn_abort(rdch);
707                         chn_resetbuf(rdch);
708                         CHN_UNLOCK(rdch);
709                 }
710                 break;
711
712         case SNDCTL_DSP_SYNC:
713                 DEB(kprintf("dsp sync\n"));
714                 /* chn_sync may sleep */
715                 if (wrch) {
716                         CHN_LOCK(wrch);
717                         chn_sync(wrch, sndbuf_getsize(wrch->bufsoft) - 4);
718                         CHN_UNLOCK(wrch);
719                 }
720                 break;
721
722         case SNDCTL_DSP_SPEED:
723                 /* chn_setspeed may sleep */
724                 tmp = 0;
725                 if (wrch) {
726                         CHN_LOCK(wrch);
727                         ret = chn_setspeed(wrch, *arg_i);
728                         tmp = wrch->speed;
729                         CHN_UNLOCK(wrch);
730                 }
731                 if (rdch && ret == 0) {
732                         CHN_LOCK(rdch);
733                         ret = chn_setspeed(rdch, *arg_i);
734                         if (tmp == 0)
735                                 tmp = rdch->speed;
736                         CHN_UNLOCK(rdch);
737                 }
738                 *arg_i = tmp;
739                 break;
740
741         case SOUND_PCM_READ_RATE:
742                 chn = wrch ? wrch : rdch;
743                 if (chn) {
744                         CHN_LOCK(chn);
745                         *arg_i = chn->speed;
746                         CHN_UNLOCK(chn);
747                 } else {
748                         *arg_i = 0;
749                         ret = EINVAL;
750                 }
751                 break;
752
753         case SNDCTL_DSP_STEREO:
754                 tmp = -1;
755                 *arg_i = (*arg_i)? AFMT_STEREO : 0;
756                 if (wrch) {
757                         CHN_LOCK(wrch);
758                         ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
759                         tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
760                         CHN_UNLOCK(wrch);
761                 }
762                 if (rdch && ret == 0) {
763                         CHN_LOCK(rdch);
764                         ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
765                         if (tmp == -1)
766                                 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
767                         CHN_UNLOCK(rdch);
768                 }
769                 *arg_i = tmp;
770                 break;
771
772         case SOUND_PCM_WRITE_CHANNELS:
773 /*      case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
774                 if (*arg_i != 0) {
775                         tmp = 0;
776                         *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
777                         if (wrch) {
778                                 CHN_LOCK(wrch);
779                                 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
780                                 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
781                                 CHN_UNLOCK(wrch);
782                         }
783                         if (rdch && ret == 0) {
784                                 CHN_LOCK(rdch);
785                                 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
786                                 if (tmp == 0)
787                                         tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
788                                 CHN_UNLOCK(rdch);
789                         }
790                         *arg_i = tmp;
791                 } else {
792                         chn = wrch ? wrch : rdch;
793                         CHN_LOCK(chn);
794                         *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
795                         CHN_UNLOCK(chn);
796                 }
797                 break;
798
799         case SOUND_PCM_READ_CHANNELS:
800                 chn = wrch ? wrch : rdch;
801                 if (chn) {
802                         CHN_LOCK(chn);
803                         *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
804                         CHN_UNLOCK(chn);
805                 } else {
806                         *arg_i = 0;
807                         ret = EINVAL;
808                 }
809                 break;
810
811         case SNDCTL_DSP_GETFMTS:        /* returns a mask of supported fmts */
812                 chn = wrch ? wrch : rdch;
813                 if (chn) {
814                         CHN_LOCK(chn);
815                         *arg_i = chn_getformats(chn);
816                         CHN_UNLOCK(chn);
817                 } else {
818                         *arg_i = 0;
819                         ret = EINVAL;
820                 }
821                 break ;
822
823         case SNDCTL_DSP_SETFMT: /* sets _one_ format */
824                 if ((*arg_i != AFMT_QUERY)) {
825                         tmp = 0;
826                         if (wrch) {
827                                 CHN_LOCK(wrch);
828                                 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
829                                 tmp = wrch->format & ~AFMT_STEREO;
830                                 CHN_UNLOCK(wrch);
831                         }
832                         if (rdch && ret == 0) {
833                                 CHN_LOCK(rdch);
834                                 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
835                                 if (tmp == 0)
836                                         tmp = rdch->format & ~AFMT_STEREO;
837                                 CHN_UNLOCK(rdch);
838                         }
839                         *arg_i = tmp;
840                 } else {
841                         chn = wrch ? wrch : rdch;
842                         CHN_LOCK(chn);
843                         *arg_i = chn->format & ~AFMT_STEREO;
844                         CHN_UNLOCK(chn);
845                 }
846                 break;
847
848         case SNDCTL_DSP_SETFRAGMENT:
849                 DEB(kprintf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
850                 {
851                         u_int32_t fragln = (*arg_i) & 0x0000ffff;
852                         u_int32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
853                         u_int32_t fragsz;
854                         u_int32_t r_maxfrags, r_fragsz;
855
856                         RANGE(fragln, 4, 16);
857                         fragsz = 1 << fragln;
858
859                         if (maxfrags == 0)
860                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
861                         if (maxfrags < 2)
862                                 maxfrags = 2;
863                         if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
864                                 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
865
866                         DEB(kprintf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
867                         if (rdch) {
868                                 CHN_LOCK(rdch);
869                                 ret = chn_setblocksize(rdch, maxfrags, fragsz);
870                                 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
871                                 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
872                                 CHN_UNLOCK(rdch);
873                         } else {
874                                 r_maxfrags = maxfrags;
875                                 r_fragsz = fragsz;
876                         }
877                         if (wrch && ret == 0) {
878                                 CHN_LOCK(wrch);
879                                 ret = chn_setblocksize(wrch, maxfrags, fragsz);
880                                 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
881                                 fragsz = sndbuf_getblksz(wrch->bufsoft);
882                                 CHN_UNLOCK(wrch);
883                         } else { /* use whatever came from the read channel */
884                                 maxfrags = r_maxfrags;
885                                 fragsz = r_fragsz;
886                         }
887
888                         fragln = 0;
889                         while (fragsz > 1) {
890                                 fragln++;
891                                 fragsz >>= 1;
892                         }
893                         *arg_i = (maxfrags << 16) | fragln;
894                 }
895                 break;
896
897         case SNDCTL_DSP_GETISPACE:
898                 /* return the size of data available in the input queue */
899                 {
900                         audio_buf_info *a = (audio_buf_info *)arg;
901                         if (rdch) {
902                                 struct snd_dbuf *bs = rdch->bufsoft;
903
904                                 CHN_LOCK(rdch);
905                                 a->bytes = sndbuf_getready(bs);
906                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
907                                 a->fragstotal = sndbuf_getblkcnt(bs);
908                                 a->fragsize = sndbuf_getblksz(bs);
909                                 CHN_UNLOCK(rdch);
910                         }
911                 }
912                 break;
913
914         case SNDCTL_DSP_GETOSPACE:
915                 /* return space available in the output queue */
916                 {
917                         audio_buf_info *a = (audio_buf_info *)arg;
918                         if (wrch) {
919                                 struct snd_dbuf *bs = wrch->bufsoft;
920
921                                 CHN_LOCK(wrch);
922                                 /* XXX abusive DMA update: chn_wrupdate(wrch); */
923                                 a->bytes = sndbuf_getfree(bs);
924                                 a->fragments = a->bytes / sndbuf_getblksz(bs);
925                                 a->fragstotal = sndbuf_getblkcnt(bs);
926                                 a->fragsize = sndbuf_getblksz(bs);
927                                 CHN_UNLOCK(wrch);
928                         }
929                 }
930                 break;
931
932         case SNDCTL_DSP_GETIPTR:
933                 {
934                         count_info *a = (count_info *)arg;
935                         if (rdch) {
936                                 struct snd_dbuf *bs = rdch->bufsoft;
937
938                                 CHN_LOCK(rdch);
939                                 /* XXX abusive DMA update: chn_rdupdate(rdch); */
940                                 a->bytes = sndbuf_gettotal(bs);
941                                 a->blocks = sndbuf_getblocks(bs) - rdch->blocks;
942                                 a->ptr = sndbuf_getreadyptr(bs);
943                                 rdch->blocks = sndbuf_getblocks(bs);
944                                 CHN_UNLOCK(rdch);
945                         } else
946                                 ret = EINVAL;
947                 }
948                 break;
949
950         case SNDCTL_DSP_GETOPTR:
951                 {
952                         count_info *a = (count_info *)arg;
953                         if (wrch) {
954                                 struct snd_dbuf *bs = wrch->bufsoft;
955
956                                 CHN_LOCK(wrch);
957                                 /* XXX abusive DMA update: chn_wrupdate(wrch); */
958                                 a->bytes = sndbuf_gettotal(bs);
959                                 a->blocks = sndbuf_getblocks(bs) - wrch->blocks;
960                                 a->ptr = sndbuf_getreadyptr(bs);
961                                 wrch->blocks = sndbuf_getblocks(bs);
962                                 CHN_UNLOCK(wrch);
963                         } else
964                                 ret = EINVAL;
965                 }
966                 break;
967
968         case SNDCTL_DSP_GETCAPS:
969                 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
970                 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
971                         *arg_i |= DSP_CAP_DUPLEX;
972                 break;
973
974         case SOUND_PCM_READ_BITS:
975                 chn = wrch ? wrch : rdch;
976                 if (chn) {
977                         CHN_LOCK(chn);
978                         if (chn->format & AFMT_8BIT)
979                                 *arg_i = 8;
980                         else if (chn->format & AFMT_16BIT)
981                                 *arg_i = 16;
982                         else if (chn->format & AFMT_24BIT)
983                                 *arg_i = 24;
984                         else if (chn->format & AFMT_32BIT)
985                                 *arg_i = 32;
986                         else
987                                 ret = EINVAL;
988                         CHN_UNLOCK(chn);
989                 } else {
990                         *arg_i = 0;
991                         ret = EINVAL;
992                 }
993                 break;
994
995         case SNDCTL_DSP_SETTRIGGER:
996                 if (rdch) {
997                         CHN_LOCK(rdch);
998                         rdch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
999                         if (*arg_i & PCM_ENABLE_INPUT)
1000                                 chn_start(rdch, 1);
1001                         else
1002                                 rdch->flags |= CHN_F_NOTRIGGER;
1003                         CHN_UNLOCK(rdch);
1004                 }
1005                 if (wrch) {
1006                         CHN_LOCK(wrch);
1007                         wrch->flags &= ~(CHN_F_TRIGGERED | CHN_F_NOTRIGGER);
1008                         if (*arg_i & PCM_ENABLE_OUTPUT)
1009                                 chn_start(wrch, 1);
1010                         else
1011                                 wrch->flags |= CHN_F_NOTRIGGER;
1012                         CHN_UNLOCK(wrch);
1013                 }
1014                 break;
1015
1016         case SNDCTL_DSP_GETTRIGGER:
1017                 *arg_i = 0;
1018                 if (wrch) {
1019                         CHN_LOCK(wrch);
1020                         if (wrch->flags & CHN_F_TRIGGERED)
1021                                 *arg_i |= PCM_ENABLE_OUTPUT;
1022                         CHN_UNLOCK(wrch);
1023                 }
1024                 if (rdch) {
1025                         CHN_LOCK(rdch);
1026                         if (rdch->flags & CHN_F_TRIGGERED)
1027                                 *arg_i |= PCM_ENABLE_INPUT;
1028                         CHN_UNLOCK(rdch);
1029                 }
1030                 break;
1031
1032         case SNDCTL_DSP_GETODELAY:
1033                 if (wrch) {
1034                         struct snd_dbuf *b = wrch->bufhard;
1035                         struct snd_dbuf *bs = wrch->bufsoft;
1036
1037                         CHN_LOCK(wrch);
1038                         /* XXX abusive DMA update: chn_wrupdate(wrch); */
1039                         *arg_i = sndbuf_getready(b) + sndbuf_getready(bs);
1040                         CHN_UNLOCK(wrch);
1041                 } else
1042                         ret = EINVAL;
1043                 break;
1044
1045         case SNDCTL_DSP_POST:
1046                 if (wrch) {
1047                         CHN_LOCK(wrch);
1048                         wrch->flags &= ~CHN_F_NOTRIGGER;
1049                         chn_start(wrch, 1);
1050                         CHN_UNLOCK(wrch);
1051                 }
1052                 break;
1053
1054         case SNDCTL_DSP_SETDUPLEX:
1055                 /*
1056                  * switch to full-duplex mode if card is in half-duplex
1057                  * mode and is able to work in full-duplex mode
1058                  */
1059                 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1060                         dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1061                 break;
1062
1063         case SNDCTL_DSP_MAPINBUF:
1064         case SNDCTL_DSP_MAPOUTBUF:
1065         case SNDCTL_DSP_SETSYNCRO:
1066                 /* undocumented */
1067
1068         case SNDCTL_DSP_SUBDIVIDE:
1069         case SOUND_PCM_WRITE_FILTER:
1070         case SOUND_PCM_READ_FILTER:
1071                 /* dunno what these do, don't sound important */
1072
1073         default:
1074                 DEB(kprintf("default ioctl fn 0x%08lx fail\n", cmd));
1075                 ret = EINVAL;
1076                 break;
1077         }
1078         relchns(i_dev, rdch, wrch, 0);
1079         return ret;
1080 }
1081
1082 static int
1083 dsp_poll(struct dev_poll_args *ap)
1084 {
1085         struct cdev *i_dev = ap->a_head.a_dev;
1086         int events = ap->a_events;
1087         struct thread *td = curthread;
1088         struct pcm_channel *wrch = NULL, *rdch = NULL;
1089         int ret, e;
1090
1091         ret = 0;
1092         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1093
1094         if (wrch) {
1095                 e = (events & (POLLOUT | POLLWRNORM));
1096                 if (e)
1097                         ret |= chn_poll(wrch, e, td);
1098         }
1099         if (rdch) {
1100                 e = (events & (POLLIN | POLLRDNORM));
1101                 if (e)
1102                         ret |= chn_poll(rdch, e, td);
1103         }
1104         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1105
1106         ap->a_events = ret;
1107         return (0);
1108 }
1109
1110 static int
1111 dsp_mmap(struct dev_mmap_args *ap)
1112 {
1113         struct cdev *i_dev = ap->a_head.a_dev;
1114         vm_offset_t offset = ap->a_offset;
1115         int nprot = ap->a_nprot;
1116         struct pcm_channel *wrch = NULL, *rdch = NULL, *c;
1117
1118         if (nprot & PROT_EXEC)
1119                 return -1;
1120
1121         getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1122 #if 0
1123         /*
1124          * XXX the linux api uses the nprot to select read/write buffer
1125          * our vm system doesn't allow this, so force write buffer
1126          */
1127
1128         if (wrch && (nprot & PROT_WRITE)) {
1129                 c = wrch;
1130         } else if (rdch && (nprot & PROT_READ)) {
1131                 c = rdch;
1132         } else {
1133                 return -1;
1134         }
1135 #else
1136         c = wrch;
1137 #endif
1138
1139         if (c == NULL) {
1140                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1141                 return -1;
1142         }
1143
1144         if (offset >= sndbuf_getsize(c->bufsoft)) {
1145                 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1146                 return -1;
1147         }
1148
1149         if (!(c->flags & CHN_F_MAPPED))
1150                 c->flags |= CHN_F_MAPPED;
1151
1152         ap->a_result = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1153         relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1154
1155         return (0);
1156 }
1157
1158 #ifdef USING_DEVFS
1159
1160 /*
1161  * Clone logic is this:
1162  * x E X = {dsp, dspW, audio}
1163  * x -> x${sysctl("hw.snd.unit")}
1164  * xN->
1165  *    for i N = 1 to channels of device N
1166  *      if xN.i isn't busy, return its dev_t
1167  */
1168 static void
1169 dsp_clone(void *arg, struct ucred *cred, char *name, int namelen,
1170     struct cdev **dev)
1171 {
1172         struct cdev *pdev;
1173         struct snddev_info *pcm_dev;
1174         struct snddev_channel *pcm_chan;
1175         int i, unit, devtype;
1176         static int devtypes[3] = {SND_DEV_DSP, SND_DEV_DSP16, SND_DEV_AUDIO};
1177         static char *devnames[3] = {"dsp", "dspW", "audio"};
1178
1179         if (*dev != NULL)
1180                 return;
1181         if (pcm_devclass == NULL)
1182                 return;
1183
1184         devtype = 0;
1185         unit = -1;
1186         for (i = 0; (i < 3) && (unit == -1); i++) {
1187                 devtype = devtypes[i];
1188                 if (strcmp(name, devnames[i]) == 0) {
1189                         unit = snd_unit;
1190                 } else {
1191                         if (dev_stdclone(name, NULL, devnames[i], &unit) != 1)
1192                                 unit = -1;
1193                 }
1194         }
1195         if (unit == -1 || unit >= devclass_get_maxunit(pcm_devclass))
1196                 return;
1197
1198         pcm_dev = devclass_get_softc(pcm_devclass, unit);
1199
1200         if (pcm_dev == NULL)
1201                 return;
1202
1203         SLIST_FOREACH(pcm_chan, &pcm_dev->channels, link) {
1204
1205                 switch(devtype) {
1206                         case SND_DEV_DSP:
1207                                 pdev = pcm_chan->dsp_devt;
1208                                 break;
1209                         case SND_DEV_DSP16:
1210                                 pdev = pcm_chan->dspW_devt;
1211                                 break;
1212                         case SND_DEV_AUDIO:
1213                                 pdev = pcm_chan->audio_devt;
1214                                 break;
1215                         default:
1216                                 panic("Unknown devtype %d", devtype);
1217                 }
1218
1219                 if ((pdev != NULL) && (pdev->si_drv1 == NULL) && (pdev->si_drv2 == NULL)) {
1220                         *dev = pdev;
1221                         dev_ref(*dev);
1222                         return;
1223                 }
1224         }
1225 }
1226
1227 static void
1228 dsp_sysinit(void *p)
1229 {
1230         dsp_ehtag = EVENTHANDLER_REGISTER(dev_clone, dsp_clone, 0, 1000);
1231 }
1232
1233 static void
1234 dsp_sysuninit(void *p)
1235 {
1236         if (dsp_ehtag != NULL)
1237                 EVENTHANDLER_DEREGISTER(dev_clone, dsp_ehtag);
1238 }
1239
1240 SYSINIT(dsp_sysinit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysinit, NULL);
1241 SYSUNINIT(dsp_sysuninit, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, dsp_sysuninit, NULL);
1242 #endif
1243
1244