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