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