Deleted Added
sdiff udiff text old ( 170505 ) new ( 170815 )
full compact
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

--- 13 unchanged lines hidden (view full) ---

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 <dev/sound/pcm/sound.h>
28#include <sys/ctype.h>
29
30SND_DECLARE_FILE("$FreeBSD: head/sys/dev/sound/pcm/dsp.c 170815 2007-06-16 03:37:28Z ariff $");
31
32static int dsp_mmap_allow_prot_exec = 0;
33SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RW,
34 &dsp_mmap_allow_prot_exec, 0, "linux mmap compatibility");
35
36struct dsp_cdevinfo {
37 struct pcm_channel *rdch, *wrch;
38 int busy, simplex;
39 TAILQ_ENTRY(dsp_cdevinfo) link;
40};
41
42#define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
43#define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
44#define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
45
46#define DSP_CDEVINFO_CACHESIZE 8
47
48#define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
49 (y) != NULL && (y)->si_drv1 != NULL)
50
51#define OLDPCM_IOCTL
52
53static d_open_t dsp_open;
54static d_close_t dsp_close;
55static d_read_t dsp_read;
56static d_write_t dsp_write;
57static d_ioctl_t dsp_ioctl;
58static d_poll_t dsp_poll;
59static d_mmap_t dsp_mmap;
60
61struct cdevsw dsp_cdevsw = {
62 .d_version = D_VERSION,
63 .d_open = dsp_open,
64 .d_close = dsp_close,
65 .d_read = dsp_read,
66 .d_write = dsp_write,
67 .d_ioctl = dsp_ioctl,
68 .d_poll = dsp_poll,
69 .d_mmap = dsp_mmap,
70 .d_name = "dsp",

--- 20 unchanged lines hidden (view full) ---

91#endif
92
93static struct snddev_info *
94dsp_get_info(struct cdev *dev)
95{
96 return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
97}
98
99static uint32_t
100dsp_get_flags(struct cdev *dev)
101{
102 device_t bdev;
103
104 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
105
106 return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
107}
108
109static void
110dsp_set_flags(struct cdev *dev, uint32_t flags)
111{
112 device_t bdev;
113
114 bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
115
116 if (bdev != NULL)
117 pcm_setflags(bdev, flags);
118}
119
120/*
121 * return the channels associated with an open device instance.
122 * lock channels specified.
123 */
124static int
125getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
126 uint32_t prio)
127{
128 struct snddev_info *d;
129 struct pcm_channel *ch;
130 uint32_t flags;
131
132 if (PCM_SIMPLEX(dev) != 0) {
133 d = dsp_get_info(dev);
134 if (!PCM_REGISTERED(d))
135 return (ENXIO);
136 pcm_lock(d);
137 PCM_WAIT(d);
138 PCM_ACQUIRE(d);
139 /*
140 * Note: order is important -
141 * pcm flags -> prio query flags -> wild guess
142 */
143 ch = NULL;
144 flags = dsp_get_flags(dev);
145 if (flags & SD_F_PRIO_WR) {
146 ch = PCM_RDCH(dev);
147 PCM_RDCH(dev) = NULL;
148 } else if (flags & SD_F_PRIO_RD) {
149 ch = PCM_WRCH(dev);
150 PCM_WRCH(dev) = NULL;
151 } else if (prio & SD_F_PRIO_WR) {
152 ch = PCM_RDCH(dev);
153 PCM_RDCH(dev) = NULL;
154 flags |= SD_F_PRIO_WR;
155 } else if (prio & SD_F_PRIO_RD) {
156 ch = PCM_WRCH(dev);
157 PCM_WRCH(dev) = NULL;
158 flags |= SD_F_PRIO_RD;
159 } else if (PCM_WRCH(dev) != NULL) {
160 ch = PCM_RDCH(dev);
161 PCM_RDCH(dev) = NULL;
162 flags |= SD_F_PRIO_WR;
163 } else if (PCM_RDCH(dev) != NULL) {
164 ch = PCM_WRCH(dev);
165 PCM_WRCH(dev) = NULL;
166 flags |= SD_F_PRIO_RD;
167 }
168 PCM_SIMPLEX(dev) = 0;
169 dsp_set_flags(dev, flags);
170 if (ch != NULL) {
171 CHN_LOCK(ch);
172 pcm_chnref(ch, -1);
173 pcm_chnrelease(ch);
174 }
175 PCM_RELEASE(d);
176 pcm_unlock(d);
177 }
178
179 *rdch = PCM_RDCH(dev);
180 *wrch = PCM_WRCH(dev);
181
182 if (*rdch != NULL && (prio & SD_F_PRIO_RD))
183 CHN_LOCK(*rdch);
184 if (*wrch != NULL && (prio & SD_F_PRIO_WR))
185 CHN_LOCK(*wrch);
186
187 return (0);
188}
189
190/* unlock specified channels */
191static void
192relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
193 uint32_t prio)
194{
195 if (wrch != NULL && (prio & SD_F_PRIO_WR))
196 CHN_UNLOCK(wrch);
197 if (rdch != NULL && (prio & SD_F_PRIO_RD))
198 CHN_UNLOCK(rdch);
199}
200
201static void
202dsp_cdevinfo_alloc(struct cdev *dev,
203 struct pcm_channel *rdch, struct pcm_channel *wrch)
204{
205 struct snddev_info *d;
206 struct dsp_cdevinfo *cdi;
207 int simplex;
208
209 d = dsp_get_info(dev);
210
211 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
212 rdch != wrch,
213 ("bogus %s(), what are you trying to accomplish here?", __func__));
214 PCM_BUSYASSERT(d);
215 mtx_assert(d->lock, MA_OWNED);
216
217 simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
218
219 /*
220 * Scan for free instance entry and put it into the end of list.
221 * Create new one if necessary.
222 */
223 TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
224 if (cdi->busy != 0)
225 break;
226 cdi->rdch = rdch;
227 cdi->wrch = wrch;
228 cdi->simplex = simplex;
229 cdi->busy = 1;
230 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
231 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
232 dev->si_drv1 = cdi;
233 return;
234 }
235 pcm_unlock(d);
236 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
237 pcm_lock(d);
238 cdi->rdch = rdch;
239 cdi->wrch = wrch;
240 cdi->simplex = simplex;
241 cdi->busy = 1;
242 TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
243 dev->si_drv1 = cdi;
244}
245
246static void
247dsp_cdevinfo_free(struct cdev *dev)
248{
249 struct snddev_info *d;
250 struct dsp_cdevinfo *cdi, *tmp;
251 uint32_t flags;
252 int i;
253
254 d = dsp_get_info(dev);
255
256 KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
257 PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL,
258 ("bogus %s(), what are you trying to accomplish here?", __func__));
259 PCM_BUSYASSERT(d);
260 mtx_assert(d->lock, MA_OWNED);
261
262 cdi = dev->si_drv1;
263 dev->si_drv1 = NULL;
264 cdi->rdch = NULL;
265 cdi->wrch = NULL;
266 cdi->simplex = 0;
267 cdi->busy = 0;
268
269 /*
270 * Once it is free, move it back to the beginning of list for
271 * faster new entry allocation.
272 */
273 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
274 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
275
276 /*
277 * Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
278 * Reset simplex flags.
279 */
280 flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
281 i = DSP_CDEVINFO_CACHESIZE;
282 TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
283 if (cdi->busy != 0) {
284 if (cdi->simplex == 0) {
285 if (cdi->rdch != NULL)
286 flags |= SD_F_PRIO_RD;
287 if (cdi->wrch != NULL)
288 flags |= SD_F_PRIO_WR;
289 }
290 } else {
291 if (i == 0) {
292 TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
293 free(cdi, M_DEVBUF);
294 } else
295 i--;
296 }
297 }
298 dsp_set_flags(dev, flags);
299}
300
301void
302dsp_cdevinfo_init(struct snddev_info *d)
303{
304 struct dsp_cdevinfo *cdi;
305 int i;
306
307 KASSERT(d != NULL, ("NULL snddev_info"));
308 PCM_BUSYASSERT(d);
309 mtx_assert(d->lock, MA_NOTOWNED);
310
311 TAILQ_INIT(&d->dsp_cdevinfo_pool);
312 for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
313 cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
314 TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
315 }
316}
317
318void
319dsp_cdevinfo_flush(struct snddev_info *d)
320{
321 struct dsp_cdevinfo *cdi, *tmp;
322
323 KASSERT(d != NULL, ("NULL snddev_info"));
324 PCM_BUSYASSERT(d);
325 mtx_assert(d->lock, MA_NOTOWNED);
326
327 cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
328 while (cdi != NULL) {
329 tmp = TAILQ_NEXT(cdi, link);
330 free(cdi, M_DEVBUF);
331 cdi = tmp;
332 }
333 TAILQ_INIT(&d->dsp_cdevinfo_pool);
334}
335
336/* duplex / simplex cdev type */
337enum {
338 DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
339 DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
340 DSP_CDEV_TYPE_RDWR, /* duplex read, write, or both */
341};
342
343#define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))

--- 21 unchanged lines hidden (view full) ---

365 { SND_DEV_DSPHW_PLAY, "dsp", ".p", 1, 1, SND_MAXHWCHAN,
366 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_WRONLY },
367 { SND_DEV_DSPHW_VPLAY, "dsp", ".vp", 1, 1, SND_MAXVCHANS,
368 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_WRONLY },
369 { SND_DEV_DSPHW_REC, "dsp", ".r", 1, 1, SND_MAXHWCHAN,
370 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDONLY },
371 { SND_DEV_DSPHW_VREC, "dsp", ".vr", 1, 1, SND_MAXVCHANS,
372 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDONLY },
373 { SND_DEV_DSPHW_CD, "dspcd", ".", 0, 0, 0,
374 AFMT_S16_LE | AFMT_STEREO, 44100, DSP_CDEV_TYPE_RDWR },
375 { SND_DEV_DSP_MMAP, "dsp_mmap", ".", 0, 0, 0,
376 AFMT_S16_LE | AFMT_STEREO, 48000, DSP_CDEV_TYPE_RDWR },
377};
378
379#define DSP_FIXUP_ERROR() do { \
380 prio = dsp_get_flags(i_dev); \
381 if (!DSP_F_VALID(flags)) \
382 error = EINVAL; \
383 if (!DSP_F_DUPLEX(flags) && \
384 ((DSP_F_READ(flags) && d->reccount == 0) || \
385 (DSP_F_WRITE(flags) && d->playcount == 0))) \
386 error = ENOTSUP; \
387 else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
388 ((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
389 (DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
390 error = EBUSY; \
391 else if (DSP_REGISTERED(d, i_dev)) \
392 error = EBUSY; \
393} while(0)
394
395static int
396dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
397{
398 struct pcm_channel *rdch, *wrch;
399 struct snddev_info *d;
400 uint32_t fmt, spd, prio;
401 int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
402
403 /* Kind of impossible.. */
404 if (i_dev == NULL || td == NULL)
405 return (ENODEV);
406
407 d = dsp_get_info(i_dev);
408 if (!PCM_REGISTERED(d))
409 return (EBADF);
410
411 PCM_GIANT_ENTER(d);
412
413 /* Lock snddev so nobody else can monkey with it. */
414 pcm_lock(d);
415 PCM_WAIT(d);
416
417 /*
418 * Try to acquire cloned device before someone else pick it.
419 * ENODEV means this is not a cloned droids.
420 */
421 error = snd_clone_acquire(i_dev);
422 if (!(error == 0 || error == ENODEV)) {
423 DSP_FIXUP_ERROR();
424 pcm_unlock(d);
425 PCM_GIANT_EXIT(d);
426 return (error);
427 }
428
429 error = 0;
430 DSP_FIXUP_ERROR();
431
432 if (error != 0) {
433 (void)snd_clone_release(i_dev);
434 pcm_unlock(d);
435 PCM_GIANT_EXIT(d);
436 return (error);
437 }
438
439 /*
440 * That is just enough. Acquire and unlock pcm lock so
441 * the other will just have to wait until we finish doing
442 * everything.
443 */
444 PCM_ACQUIRE(d);
445 pcm_unlock(d);
446
447 devtype = PCMDEV(i_dev);
448 wdevunit = -1;
449 rdevunit = -1;
450 fmt = 0;
451 spd = 0;
452
453 for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
454 if (devtype != dsp_cdevs[i].type)
455 continue;
456 if (DSP_F_SIMPLEX(flags) &&
457 ((dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY &&
458 DSP_F_READ(flags)) ||
459 (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY &&
460 DSP_F_WRITE(flags)))) {
461 /*
462 * simplex, opposite direction? Please be gone..
463 */
464 (void)snd_clone_release(i_dev);
465 PCM_RELEASE_QUICK(d);
466 PCM_GIANT_EXIT(d);
467 return (ENOTSUP);
468 }
469 if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY)
470 wdevunit = dev2unit(i_dev);
471 else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY)
472 rdevunit = dev2unit(i_dev);
473 fmt = dsp_cdevs[i].fmt;
474 spd = dsp_cdevs[i].spd;
475 break;
476 }
477
478 /* No matching devtype? */
479 if (fmt == 0 || spd == 0)
480 panic("impossible devtype %d", devtype);
481
482 rdch = NULL;
483 wrch = NULL;
484 rderror = 0;
485 wrerror = 0;
486
487 /*
488 * if we get here, the open request is valid- either:
489 * * we were previously not open
490 * * we were open for play xor record and the opener wants
491 * the non-open direction
492 */
493 if (DSP_F_READ(flags)) {
494 /* open for read */
495 rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
496 td->td_proc->p_pid, rdevunit);
497
498 if (rderror == 0 && (chn_reset(rdch, fmt) != 0 ||
499 (chn_setspeed(rdch, spd) != 0)))
500 rderror = ENXIO;
501
502 if (rderror != 0) {
503 if (rdch != NULL)
504 pcm_chnrelease(rdch);
505 if (!DSP_F_DUPLEX(flags)) {
506 (void)snd_clone_release(i_dev);
507 PCM_RELEASE_QUICK(d);
508 PCM_GIANT_EXIT(d);
509 return (rderror);
510 }
511 rdch = NULL;
512 } else {
513 if (flags & O_NONBLOCK)
514 rdch->flags |= CHN_F_NBIO;
515 pcm_chnref(rdch, 1);
516 CHN_UNLOCK(rdch);
517 }
518 }
519
520 if (DSP_F_WRITE(flags)) {
521 /* open for write */
522 wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
523 td->td_proc->p_pid, wdevunit);
524
525 if (wrerror == 0 && (chn_reset(wrch, fmt) != 0 ||
526 (chn_setspeed(wrch, spd) != 0)))
527 wrerror = ENXIO;
528
529 if (wrerror != 0) {
530 if (wrch != NULL)
531 pcm_chnrelease(wrch);
532 if (!DSP_F_DUPLEX(flags)) {
533 if (rdch != NULL) {
534 /*
535 * Lock, deref and release previously
536 * created record channel
537 */
538 CHN_LOCK(rdch);
539 pcm_chnref(rdch, -1);
540 pcm_chnrelease(rdch);
541 }
542 (void)snd_clone_release(i_dev);
543 PCM_RELEASE_QUICK(d);
544 PCM_GIANT_EXIT(d);
545 return (wrerror);
546 }
547 wrch = NULL;
548 } else {
549 if (flags & O_NONBLOCK)
550 wrch->flags |= CHN_F_NBIO;
551 pcm_chnref(wrch, 1);
552 CHN_UNLOCK(wrch);
553 }
554 }
555
556 if (rdch == NULL && wrch == NULL) {
557 (void)snd_clone_release(i_dev);
558 PCM_RELEASE_QUICK(d);
559 PCM_GIANT_EXIT(d);
560 return ((wrerror != 0) ? wrerror : rderror);
561 }
562
563 pcm_lock(d);
564
565 /*
566 * We're done. Allocate channels information for this cdev.
567 */
568 dsp_cdevinfo_alloc(i_dev, rdch, wrch);
569
570 /*
571 * Increase clone refcount for its automatic garbage collector.
572 */
573 (void)snd_clone_ref(i_dev);
574
575 PCM_RELEASE(d);
576 pcm_unlock(d);
577
578 PCM_GIANT_LEAVE(d);
579
580 return (0);
581}
582
583static int
584dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
585{
586 struct pcm_channel *rdch, *wrch;
587 struct snddev_info *d;
588 int sg_ids, refs;
589
590 d = dsp_get_info(i_dev);
591 if (!DSP_REGISTERED(d, i_dev))
592 return (EBADF);
593
594 PCM_GIANT_ENTER(d);
595
596 pcm_lock(d);
597 PCM_WAIT(d);
598
599 rdch = PCM_RDCH(i_dev);
600 wrch = PCM_WRCH(i_dev);
601
602 if (rdch || wrch) {
603 PCM_ACQUIRE(d);
604 pcm_unlock(d);
605
606 refs = 0;
607 if (rdch) {
608 /*
609 * The channel itself need not be locked because:
610 * a) Adding a channel to a syncgroup happens only in dsp_ioctl(),
611 * which cannot run concurrently to dsp_close().
612 * b) The syncmember pointer (sm) is protected by the global
613 * syncgroup list lock.
614 * c) A channel can't just disappear, invalidating pointers,
615 * unless it's closed/dereferenced first.
616 */
617 PCM_SG_LOCK();
618 sg_ids = chn_syncdestroy(rdch);
619 PCM_SG_UNLOCK();
620 if (sg_ids != 0)
621 free_unr(pcmsg_unrhdr, sg_ids);
622
623 CHN_LOCK(rdch);
624 refs += pcm_chnref(rdch, -1);
625 chn_abort(rdch); /* won't sleep */
626 rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
627 chn_reset(rdch, 0);
628 pcm_chnrelease(rdch);
629 PCM_RDCH(i_dev) = NULL;
630 }
631 if (wrch) {
632 /*
633 * Please see block above.
634 */
635 PCM_SG_LOCK();
636 sg_ids = chn_syncdestroy(wrch);
637 PCM_SG_UNLOCK();
638 if (sg_ids != 0)
639 free_unr(pcmsg_unrhdr, sg_ids);
640
641 CHN_LOCK(wrch);
642 refs += pcm_chnref(wrch, -1);
643 chn_flush(wrch); /* may sleep */
644 wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MAPPED | CHN_F_DEAD);
645 chn_reset(wrch, 0);
646 pcm_chnrelease(wrch);
647 PCM_WRCH(i_dev) = NULL;
648 }
649
650 pcm_lock(d);
651 /*
652 * If there are no more references, release the channels.
653 */
654 if (refs == 0 && PCM_RDCH(i_dev) == NULL &&
655 PCM_WRCH(i_dev) == NULL) {
656 dsp_cdevinfo_free(i_dev);
657 /*
658 * Release clone busy state and unref it
659 * so the automatic garbage collector will
660 * get the hint and do the remaining cleanup
661 * process.
662 */
663 (void)snd_clone_release(i_dev);
664 (void)snd_clone_unref(i_dev);
665 }
666 PCM_RELEASE(d);
667 }
668
669 pcm_unlock(d);
670
671 PCM_GIANT_LEAVE(d);
672
673 return (0);
674}
675
676static __inline int
677dsp_io_ops(struct cdev *i_dev, struct uio *buf)
678{
679 struct snddev_info *d;
680 struct pcm_channel **ch, *rdch, *wrch;
681 int (*chn_io)(struct pcm_channel *, struct uio *);
682 int prio, ret;
683 pid_t runpid;
684
685 KASSERT(i_dev != NULL && buf != NULL &&
686 (buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
687 ("%s(): io train wreck!", __func__));
688
689 d = dsp_get_info(i_dev);
690 if (!DSP_REGISTERED(d, i_dev))
691 return (EBADF);
692
693 PCM_GIANT_ENTER(d);
694
695 switch (buf->uio_rw) {
696 case UIO_READ:
697 prio = SD_F_PRIO_RD;
698 ch = &rdch;
699 chn_io = chn_read;
700 break;
701 case UIO_WRITE:
702 prio = SD_F_PRIO_WR;
703 ch = &wrch;
704 chn_io = chn_write;
705 break;
706 default:
707 panic("invalid/corrupted uio direction: %d", buf->uio_rw);
708 break;
709 }
710
711 rdch = NULL;
712 wrch = NULL;
713 runpid = buf->uio_td->td_proc->p_pid;
714
715 getchns(i_dev, &rdch, &wrch, prio);
716
717 if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
718 PCM_GIANT_EXIT(d);
719 return (EBADF);
720 }
721
722 if (((*ch)->flags & (CHN_F_MAPPED | CHN_F_DEAD)) ||
723 (((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
724 relchns(i_dev, rdch, wrch, prio);
725 PCM_GIANT_EXIT(d);
726 return (EINVAL);
727 } else if (!((*ch)->flags & CHN_F_RUNNING)) {
728 (*ch)->flags |= CHN_F_RUNNING;
729 (*ch)->pid = runpid;
730 }
731
732 /*
733 * chn_read/write must give up channel lock in order to copy bytes
734 * from/to userland, so up the "in progress" counter to make sure
735 * someone else doesn't come along and muss up the buffer.
736 */
737 ++(*ch)->inprog;
738 ret = chn_io(*ch, buf);
739 --(*ch)->inprog;
740
741 CHN_BROADCAST(&(*ch)->cv);
742
743 relchns(i_dev, rdch, wrch, prio);
744
745 PCM_GIANT_LEAVE(d);
746
747 return (ret);
748}
749
750static int
751dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
752{
753 return (dsp_io_ops(i_dev, buf));
754}
755
756static int
757dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
758{
759 return (dsp_io_ops(i_dev, buf));
760}
761
762static int
763dsp_ioctl(struct cdev *i_dev, u_long cmd, caddr_t arg, int mode, struct thread *td)
764{
765 struct pcm_channel *chn, *rdch, *wrch;
766 struct snddev_info *d;
767 int *arg_i, ret, kill, tmp, xcmd;
768
769 d = dsp_get_info(i_dev);
770 if (!DSP_REGISTERED(d, i_dev))
771 return (EBADF);
772
773 PCM_GIANT_ENTER(d);
774
775 arg_i = (int *)arg;
776 ret = 0;
777 xcmd = 0;
778
779 /*
780 * this is an evil hack to allow broken apps to perform mixer ioctls
781 * on dsp devices.
782 */
783 if (IOCGROUP(cmd) == 'M') {
784 /*
785 * This is at least, a bug to bug compatible with OSS.
786 */
787 if (d->mixer_dev != NULL) {
788 PCM_ACQUIRE_QUICK(d);
789 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
790 MIXER_CMD_DIRECT);
791 PCM_RELEASE_QUICK(d);
792 } else
793 ret = EBADF;
794
795 PCM_GIANT_EXIT(d);
796
797 return (ret);
798 }
799
800 /*
801 * Certain ioctls may be made on any type of device (audio, mixer,
802 * and MIDI). Handle those special cases here.
803 */
804 if (IOCGROUP(cmd) == 'X') {
805 PCM_ACQUIRE_QUICK(d);
806 switch(cmd) {
807 case SNDCTL_SYSINFO:
808 sound_oss_sysinfo((oss_sysinfo *)arg);
809 break;
810 case SNDCTL_AUDIOINFO:
811 ret = dsp_oss_audioinfo(i_dev, (oss_audioinfo *)arg);
812 break;
813 case SNDCTL_MIXERINFO:
814 ret = mixer_oss_mixerinfo(i_dev, (oss_mixerinfo *)arg);
815 break;
816 default:
817 ret = EINVAL;
818 }
819 PCM_RELEASE_QUICK(d);
820 PCM_GIANT_EXIT(d);
821 return (ret);
822 }
823
824 getchns(i_dev, &rdch, &wrch, 0);
825
826 kill = 0;
827 if (wrch && (wrch->flags & CHN_F_DEAD))
828 kill |= 1;
829 if (rdch && (rdch->flags & CHN_F_DEAD))
830 kill |= 2;
831 if (kill == 3) {
832 relchns(i_dev, rdch, wrch, 0);
833 PCM_GIANT_EXIT(d);
834 return (EINVAL);
835 }
836 if (kill & 1)
837 wrch = NULL;
838 if (kill & 2)
839 rdch = NULL;
840
841 if (wrch == NULL && rdch == NULL) {
842 relchns(i_dev, rdch, wrch, 0);
843 PCM_GIANT_EXIT(d);
844 return (EINVAL);
845 }
846
847 switch(cmd) {
848#ifdef OLDPCM_IOCTL
849 /*
850 * we start with the new ioctl interface.
851 */
852 case AIONWRITE: /* how many bytes can write ? */
853 if (wrch) {
854 CHN_LOCK(wrch);

--- 10 unchanged lines hidden (view full) ---

865 break;
866
867 case AIOSSIZE: /* set the current blocksize */
868 {
869 struct snd_size *p = (struct snd_size *)arg;
870
871 p->play_size = 0;
872 p->rec_size = 0;
873 PCM_ACQUIRE_QUICK(d);
874 if (wrch) {
875 CHN_LOCK(wrch);
876 chn_setblocksize(wrch, 2, p->play_size);
877 p->play_size = sndbuf_getblksz(wrch->bufsoft);
878 CHN_UNLOCK(wrch);
879 }
880 if (rdch) {
881 CHN_LOCK(rdch);
882 chn_setblocksize(rdch, 2, p->rec_size);
883 p->rec_size = sndbuf_getblksz(rdch->bufsoft);
884 CHN_UNLOCK(rdch);
885 }
886 PCM_RELEASE_QUICK(d);
887 }
888 break;
889 case AIOGSIZE: /* get the current blocksize */
890 {
891 struct snd_size *p = (struct snd_size *)arg;
892
893 if (wrch) {
894 CHN_LOCK(wrch);

--- 14 unchanged lines hidden (view full) ---

909 snd_chan_param *p = (snd_chan_param *)arg;
910
911 if (cmd == AIOSFMT &&
912 ((p->play_format != 0 && p->play_rate == 0) ||
913 (p->rec_format != 0 && p->rec_rate == 0))) {
914 ret = EINVAL;
915 break;
916 }
917 PCM_ACQUIRE_QUICK(d);
918 if (wrch) {
919 CHN_LOCK(wrch);
920 if (cmd == AIOSFMT && p->play_format != 0) {
921 chn_setformat(wrch, p->play_format);
922 chn_setspeed(wrch, p->play_rate);
923 }
924 p->play_rate = wrch->speed;
925 p->play_format = wrch->format;

--- 10 unchanged lines hidden (view full) ---

936 }
937 p->rec_rate = rdch->speed;
938 p->rec_format = rdch->format;
939 CHN_UNLOCK(rdch);
940 } else {
941 p->rec_rate = 0;
942 p->rec_format = 0;
943 }
944 PCM_RELEASE_QUICK(d);
945 }
946 break;
947
948 case AIOGCAP: /* get capabilities */
949 {
950 snd_capabilities *p = (snd_capabilities *)arg;
951 struct pcmchan_caps *pcaps = NULL, *rcaps = NULL;
952 struct cdev *pdev;
953
954 pcm_lock(d);
955 if (rdch) {
956 CHN_LOCK(rdch);
957 rcaps = chn_getcaps(rdch);
958 }
959 if (wrch) {
960 CHN_LOCK(wrch);
961 pcaps = chn_getcaps(wrch);
962 }

--- 7 unchanged lines hidden (view full) ---

970 p->formats = (rdch? chn_getformats(rdch) : 0xffffffff) &
971 (wrch? chn_getformats(wrch) : 0xffffffff);
972 if (rdch && wrch)
973 p->formats |= (dsp_get_flags(i_dev) & SD_F_SIMPLEX)? 0 : AFMT_FULLDUPLEX;
974 pdev = d->mixer_dev;
975 p->mixers = 1; /* default: one mixer */
976 p->inputs = pdev->si_drv1? mix_getdevs(pdev->si_drv1) : 0;
977 p->left = p->right = 100;
978 if (wrch)
979 CHN_UNLOCK(wrch);
980 if (rdch)
981 CHN_UNLOCK(rdch);
982 pcm_unlock(d);
983 }
984 break;
985
986 case AIOSTOP:
987 if (*arg_i == AIOSYNC_PLAY && wrch) {
988 CHN_LOCK(wrch);
989 *arg_i = chn_abort(wrch);
990 CHN_UNLOCK(wrch);

--- 63 unchanged lines hidden (view full) ---

1054 if (chn) {
1055 CHN_LOCK(chn);
1056 *arg_i = sndbuf_getblksz(chn->bufsoft);
1057 CHN_UNLOCK(chn);
1058 } else {
1059 *arg_i = 0;
1060 ret = EINVAL;
1061 }
1062 break;
1063
1064 case SNDCTL_DSP_SETBLKSIZE:
1065 RANGE(*arg_i, 16, 65536);
1066 PCM_ACQUIRE_QUICK(d);
1067 if (wrch) {
1068 CHN_LOCK(wrch);
1069 chn_setblocksize(wrch, 2, *arg_i);
1070 CHN_UNLOCK(wrch);
1071 }
1072 if (rdch) {
1073 CHN_LOCK(rdch);
1074 chn_setblocksize(rdch, 2, *arg_i);
1075 CHN_UNLOCK(rdch);
1076 }
1077 PCM_RELEASE_QUICK(d);
1078 break;
1079
1080 case SNDCTL_DSP_RESET:
1081 DEB(printf("dsp reset\n"));
1082 if (wrch) {
1083 CHN_LOCK(wrch);
1084 chn_abort(wrch);
1085 chn_resetbuf(wrch);

--- 9 unchanged lines hidden (view full) ---

1095
1096 case SNDCTL_DSP_SYNC:
1097 DEB(printf("dsp sync\n"));
1098 /* chn_sync may sleep */
1099 if (wrch) {
1100 CHN_LOCK(wrch);
1101 chn_sync(wrch, 0);
1102 CHN_UNLOCK(wrch);
1103 } else
1104 ret = EINVAL;
1105 break;
1106
1107 case SNDCTL_DSP_SPEED:
1108 /* chn_setspeed may sleep */
1109 tmp = 0;
1110 PCM_ACQUIRE_QUICK(d);
1111 if (wrch) {
1112 CHN_LOCK(wrch);
1113 ret = chn_setspeed(wrch, *arg_i);
1114 tmp = wrch->speed;
1115 CHN_UNLOCK(wrch);
1116 }
1117 if (rdch && ret == 0) {
1118 CHN_LOCK(rdch);
1119 ret = chn_setspeed(rdch, *arg_i);
1120 if (tmp == 0)
1121 tmp = rdch->speed;
1122 CHN_UNLOCK(rdch);
1123 }
1124 PCM_RELEASE_QUICK(d);
1125 *arg_i = tmp;
1126 break;
1127
1128 case SOUND_PCM_READ_RATE:
1129 chn = wrch ? wrch : rdch;
1130 if (chn) {
1131 CHN_LOCK(chn);
1132 *arg_i = chn->speed;
1133 CHN_UNLOCK(chn);
1134 } else {
1135 *arg_i = 0;
1136 ret = EINVAL;
1137 }
1138 break;
1139
1140 case SNDCTL_DSP_STEREO:
1141 tmp = -1;
1142 *arg_i = (*arg_i)? AFMT_STEREO : 0;
1143 PCM_ACQUIRE_QUICK(d);
1144 if (wrch) {
1145 CHN_LOCK(wrch);
1146 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
1147 tmp = (wrch->format & AFMT_STEREO)? 1 : 0;
1148 CHN_UNLOCK(wrch);
1149 }
1150 if (rdch && ret == 0) {
1151 CHN_LOCK(rdch);
1152 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
1153 if (tmp == -1)
1154 tmp = (rdch->format & AFMT_STEREO)? 1 : 0;
1155 CHN_UNLOCK(rdch);
1156 }
1157 PCM_RELEASE_QUICK(d);
1158 *arg_i = tmp;
1159 break;
1160
1161 case SOUND_PCM_WRITE_CHANNELS:
1162/* case SNDCTL_DSP_CHANNELS: ( == SOUND_PCM_WRITE_CHANNELS) */
1163 if (*arg_i != 0) {
1164 tmp = 0;
1165 *arg_i = (*arg_i != 1)? AFMT_STEREO : 0;
1166 PCM_ACQUIRE_QUICK(d);
1167 if (wrch) {
1168 CHN_LOCK(wrch);
1169 ret = chn_setformat(wrch, (wrch->format & ~AFMT_STEREO) | *arg_i);
1170 tmp = (wrch->format & AFMT_STEREO)? 2 : 1;
1171 CHN_UNLOCK(wrch);
1172 }
1173 if (rdch && ret == 0) {
1174 CHN_LOCK(rdch);
1175 ret = chn_setformat(rdch, (rdch->format & ~AFMT_STEREO) | *arg_i);
1176 if (tmp == 0)
1177 tmp = (rdch->format & AFMT_STEREO)? 2 : 1;
1178 CHN_UNLOCK(rdch);
1179 }
1180 PCM_RELEASE_QUICK(d);
1181 *arg_i = tmp;
1182 } else {
1183 chn = wrch ? wrch : rdch;
1184 CHN_LOCK(chn);
1185 *arg_i = (chn->format & AFMT_STEREO) ? 2 : 1;
1186 CHN_UNLOCK(chn);
1187 }
1188 break;

--- 15 unchanged lines hidden (view full) ---

1204 if (chn) {
1205 CHN_LOCK(chn);
1206 *arg_i = chn_getformats(chn);
1207 CHN_UNLOCK(chn);
1208 } else {
1209 *arg_i = 0;
1210 ret = EINVAL;
1211 }
1212 break;
1213
1214 case SNDCTL_DSP_SETFMT: /* sets _one_ format */
1215 if ((*arg_i != AFMT_QUERY)) {
1216 tmp = 0;
1217 PCM_ACQUIRE_QUICK(d);
1218 if (wrch) {
1219 CHN_LOCK(wrch);
1220 ret = chn_setformat(wrch, (*arg_i) | (wrch->format & AFMT_STEREO));
1221 tmp = wrch->format & ~AFMT_STEREO;
1222 CHN_UNLOCK(wrch);
1223 }
1224 if (rdch && ret == 0) {
1225 CHN_LOCK(rdch);
1226 ret = chn_setformat(rdch, (*arg_i) | (rdch->format & AFMT_STEREO));
1227 if (tmp == 0)
1228 tmp = rdch->format & ~AFMT_STEREO;
1229 CHN_UNLOCK(rdch);
1230 }
1231 PCM_RELEASE_QUICK(d);
1232 *arg_i = tmp;
1233 } else {
1234 chn = wrch ? wrch : rdch;
1235 CHN_LOCK(chn);
1236 *arg_i = chn->format & ~AFMT_STEREO;
1237 CHN_UNLOCK(chn);
1238 }
1239 break;
1240
1241 case SNDCTL_DSP_SETFRAGMENT:
1242 DEB(printf("SNDCTL_DSP_SETFRAGMENT 0x%08x\n", *(int *)arg));
1243 {
1244 uint32_t fragln = (*arg_i) & 0x0000ffff;
1245 uint32_t maxfrags = ((*arg_i) & 0xffff0000) >> 16;
1246 uint32_t fragsz;
1247 uint32_t r_maxfrags, r_fragsz;
1248
1249 RANGE(fragln, 4, 16);
1250 fragsz = 1 << fragln;
1251
1252 if (maxfrags == 0)
1253 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1254 if (maxfrags < 2)
1255 maxfrags = 2;
1256 if (maxfrags * fragsz > CHN_2NDBUFMAXSIZE)
1257 maxfrags = CHN_2NDBUFMAXSIZE / fragsz;
1258
1259 DEB(printf("SNDCTL_DSP_SETFRAGMENT %d frags, %d sz\n", maxfrags, fragsz));
1260 PCM_ACQUIRE_QUICK(d);
1261 if (rdch) {
1262 CHN_LOCK(rdch);
1263 ret = chn_setblocksize(rdch, maxfrags, fragsz);
1264 r_maxfrags = sndbuf_getblkcnt(rdch->bufsoft);
1265 r_fragsz = sndbuf_getblksz(rdch->bufsoft);
1266 CHN_UNLOCK(rdch);
1267 } else {
1268 r_maxfrags = maxfrags;

--- 4 unchanged lines hidden (view full) ---

1273 ret = chn_setblocksize(wrch, maxfrags, fragsz);
1274 maxfrags = sndbuf_getblkcnt(wrch->bufsoft);
1275 fragsz = sndbuf_getblksz(wrch->bufsoft);
1276 CHN_UNLOCK(wrch);
1277 } else { /* use whatever came from the read channel */
1278 maxfrags = r_maxfrags;
1279 fragsz = r_fragsz;
1280 }
1281 PCM_RELEASE_QUICK(d);
1282
1283 fragln = 0;
1284 while (fragsz > 1) {
1285 fragln++;
1286 fragsz >>= 1;
1287 }
1288 *arg_i = (maxfrags << 16) | fragln;
1289 }

--- 7 unchanged lines hidden (view full) ---

1297 struct snd_dbuf *bs = rdch->bufsoft;
1298
1299 CHN_LOCK(rdch);
1300 a->bytes = sndbuf_getready(bs);
1301 a->fragments = a->bytes / sndbuf_getblksz(bs);
1302 a->fragstotal = sndbuf_getblkcnt(bs);
1303 a->fragsize = sndbuf_getblksz(bs);
1304 CHN_UNLOCK(rdch);
1305 } else
1306 ret = EINVAL;
1307 }
1308 break;
1309
1310 case SNDCTL_DSP_GETOSPACE:
1311 /* return space available in the output queue */
1312 {
1313 audio_buf_info *a = (audio_buf_info *)arg;
1314 if (wrch) {
1315 struct snd_dbuf *bs = wrch->bufsoft;
1316
1317 CHN_LOCK(wrch);
1318 /* XXX abusive DMA update: chn_wrupdate(wrch); */
1319 a->bytes = sndbuf_getfree(bs);
1320 a->fragments = a->bytes / sndbuf_getblksz(bs);
1321 a->fragstotal = sndbuf_getblkcnt(bs);
1322 a->fragsize = sndbuf_getblksz(bs);
1323 CHN_UNLOCK(wrch);
1324 } else
1325 ret = EINVAL;
1326 }
1327 break;
1328
1329 case SNDCTL_DSP_GETIPTR:
1330 {
1331 count_info *a = (count_info *)arg;
1332 if (rdch) {
1333 struct snd_dbuf *bs = rdch->bufsoft;

--- 24 unchanged lines hidden (view full) ---

1358 wrch->blocks = sndbuf_getblocks(bs);
1359 CHN_UNLOCK(wrch);
1360 } else
1361 ret = EINVAL;
1362 }
1363 break;
1364
1365 case SNDCTL_DSP_GETCAPS:
1366 pcm_lock(d);
1367 *arg_i = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
1368 if (rdch && wrch && !(dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1369 *arg_i |= DSP_CAP_DUPLEX;
1370 pcm_unlock(d);
1371 break;
1372
1373 case SOUND_PCM_READ_BITS:
1374 chn = wrch ? wrch : rdch;
1375 if (chn) {
1376 CHN_LOCK(chn);
1377 if (chn->format & AFMT_8BIT)
1378 *arg_i = 8;

--- 62 unchanged lines hidden (view full) ---

1441 break;
1442
1443 case SNDCTL_DSP_POST:
1444 if (wrch) {
1445 CHN_LOCK(wrch);
1446 wrch->flags &= ~CHN_F_NOTRIGGER;
1447 chn_start(wrch, 1);
1448 CHN_UNLOCK(wrch);
1449 } else
1450 ret = EINVAL;
1451 break;
1452
1453 case SNDCTL_DSP_SETDUPLEX:
1454 /*
1455 * switch to full-duplex mode if card is in half-duplex
1456 * mode and is able to work in full-duplex mode
1457 */
1458 pcm_lock(d);
1459 if (rdch && wrch && (dsp_get_flags(i_dev) & SD_F_SIMPLEX))
1460 dsp_set_flags(i_dev, dsp_get_flags(i_dev)^SD_F_SIMPLEX);
1461 pcm_unlock(d);
1462 break;
1463
1464 /*
1465 * The following four ioctls are simple wrappers around mixer_ioctl
1466 * with no further processing. xcmd is short for "translated
1467 * command".
1468 */
1469 case SNDCTL_DSP_GETRECVOL:

--- 7 unchanged lines hidden (view full) ---

1477 case SNDCTL_DSP_GETPLAYVOL:
1478 if (xcmd == 0)
1479 xcmd = SOUND_MIXER_READ_PCM;
1480 /* FALLTHROUGH */
1481 case SNDCTL_DSP_SETPLAYVOL:
1482 if (xcmd == 0)
1483 xcmd = SOUND_MIXER_WRITE_PCM;
1484
1485 if (d->mixer_dev != NULL) {
1486 PCM_ACQUIRE_QUICK(d);
1487 ret = mixer_ioctl_cmd(d->mixer_dev, xcmd, arg, -1, td,
1488 MIXER_CMD_DIRECT);
1489 PCM_RELEASE_QUICK(d);
1490 } else
1491 ret = ENOTSUP;
1492 break;
1493
1494 case SNDCTL_DSP_GET_RECSRC_NAMES:
1495 case SNDCTL_DSP_GET_RECSRC:
1496 case SNDCTL_DSP_SET_RECSRC:
1497 if (d->mixer_dev != NULL) {
1498 PCM_ACQUIRE_QUICK(d);
1499 ret = mixer_ioctl_cmd(d->mixer_dev, cmd, arg, -1, td,
1500 MIXER_CMD_DIRECT);
1501 PCM_RELEASE_QUICK(d);
1502 } else
1503 ret = ENOTSUP;
1504 break;
1505
1506 /*
1507 * The following 3 ioctls aren't very useful at the moment. For
1508 * now, only a single channel is associated with a cdev (/dev/dspN
1509 * instance), so there's only a single output routing to use (i.e.,
1510 * the wrch bound to this cdev).

--- 170 unchanged lines hidden (view full) ---

1681 ei->rec_overruns = rdch->xruns;
1682 rdch->xruns = 0;
1683 CHN_UNLOCK(rdch);
1684 }
1685 }
1686 break;
1687
1688 case SNDCTL_DSP_SYNCGROUP:
1689 PCM_ACQUIRE_QUICK(d);
1690 ret = dsp_oss_syncgroup(wrch, rdch, (oss_syncgroup *)arg);
1691 PCM_RELEASE_QUICK(d);
1692 break;
1693
1694 case SNDCTL_DSP_SYNCSTART:
1695 PCM_ACQUIRE_QUICK(d);
1696 ret = dsp_oss_syncstart(*arg_i);
1697 PCM_RELEASE_QUICK(d);
1698 break;
1699
1700 case SNDCTL_DSP_POLICY:
1701 PCM_ACQUIRE_QUICK(d);
1702 ret = dsp_oss_policy(wrch, rdch, *arg_i);
1703 PCM_RELEASE_QUICK(d);
1704 break;
1705
1706#ifdef OSSV4_EXPERIMENT
1707 /*
1708 * XXX The following ioctls are not yet supported and just return
1709 * EINVAL.
1710 */
1711 case SNDCTL_DSP_GETOPEAKS:

--- 12 unchanged lines hidden (view full) ---

1724 else {
1725 (*op)[0] = lpeak;
1726 (*op)[1] = rpeak;
1727 }
1728 CHN_UNLOCK(chn);
1729 }
1730 break;
1731
1732 /*
1733 * XXX Once implemented, revisit this for proper cv protection
1734 * (if necessary).
1735 */
1736 case SNDCTL_DSP_COOKEDMODE:
1737 ret = dsp_oss_cookedmode(wrch, rdch, *arg_i);
1738 break;
1739 case SNDCTL_DSP_GET_CHNORDER:
1740 ret = dsp_oss_getchnorder(wrch, rdch, (unsigned long long *)arg);
1741 break;
1742 case SNDCTL_DSP_SET_CHNORDER:
1743 ret = dsp_oss_setchnorder(wrch, rdch, (unsigned long long *)arg);

--- 48 unchanged lines hidden (view full) ---

1792 case SOUND_PCM_READ_FILTER:
1793 /* dunno what these do, don't sound important */
1794
1795 default:
1796 DEB(printf("default ioctl fn 0x%08lx fail\n", cmd));
1797 ret = EINVAL;
1798 break;
1799 }
1800
1801 relchns(i_dev, rdch, wrch, 0);
1802
1803 PCM_GIANT_LEAVE(d);
1804
1805 return (ret);
1806}
1807
1808static int
1809dsp_poll(struct cdev *i_dev, int events, struct thread *td)
1810{
1811 struct snddev_info *d;
1812 struct pcm_channel *wrch, *rdch;
1813 int ret, e;
1814
1815 d = dsp_get_info(i_dev);
1816 if (!DSP_REGISTERED(d, i_dev))
1817 return (EBADF);
1818
1819 PCM_GIANT_ENTER(d);
1820
1821 wrch = NULL;
1822 rdch = NULL;
1823 ret = 0;
1824
1825 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1826
1827 if (wrch != NULL && !(wrch->flags & CHN_F_DEAD)) {
1828 e = (events & (POLLOUT | POLLWRNORM));
1829 if (e)
1830 ret |= chn_poll(wrch, e, td);
1831 }
1832
1833 if (rdch != NULL && !(rdch->flags & CHN_F_DEAD)) {
1834 e = (events & (POLLIN | POLLRDNORM));
1835 if (e)
1836 ret |= chn_poll(rdch, e, td);
1837 }
1838
1839 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1840
1841 PCM_GIANT_LEAVE(d);
1842
1843 return (ret);
1844}
1845
1846static int
1847dsp_mmap(struct cdev *i_dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
1848{
1849 struct snddev_info *d;
1850 struct pcm_channel *wrch, *rdch, *c;
1851
1852 /*
1853 * Reject PROT_EXEC by default. It just doesn't makes sense.
1854 * Unfortunately, we have to give up this one due to linux_mmap
1855 * changes.
1856 *
1857 * http://lists.freebsd.org/pipermail/freebsd-emulation/2007-June/003698.html
1858 *
1859 */
1860 if ((nprot & PROT_EXEC) && dsp_mmap_allow_prot_exec == 0)
1861 return (-1);
1862
1863 d = dsp_get_info(i_dev);
1864 if (!DSP_REGISTERED(d, i_dev))
1865 return (-1);
1866
1867 PCM_GIANT_ENTER(d);
1868
1869 getchns(i_dev, &rdch, &wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1870
1871 /*
1872 * XXX The linux api uses the nprot to select read/write buffer
1873 * our vm system doesn't allow this, so force write buffer.
1874 *
1875 * This is just a quack to fool full-duplex mmap, so that at
1876 * least playback _or_ recording works. If you really got the
1877 * urge to make _both_ work at the same time, avoid O_RDWR.
1878 * Just open each direction separately and mmap() it.
1879 *
1880 * Failure is not an option due to INVARIANTS check within
1881 * device_pager.c, which means, we have to give up one over
1882 * another.
1883 */
1884 c = (wrch != NULL) ? wrch : rdch;
1885
1886 if (c == NULL || (c->flags & CHN_F_MMAP_INVALID) ||
1887 offset >= sndbuf_getsize(c->bufsoft) ||
1888 (wrch != NULL && (wrch->flags & CHN_F_MMAP_INVALID)) ||
1889 (rdch != NULL && (rdch->flags & CHN_F_MMAP_INVALID))) {
1890 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1891 PCM_GIANT_EXIT(d);
1892 return (-1);
1893 }
1894
1895 /* XXX full-duplex quack. */
1896 if (wrch != NULL)
1897 wrch->flags |= CHN_F_MAPPED;
1898 if (rdch != NULL)
1899 rdch->flags |= CHN_F_MAPPED;
1900
1901 *paddr = vtophys(sndbuf_getbufofs(c->bufsoft, offset));
1902 relchns(i_dev, rdch, wrch, SD_F_PRIO_RD | SD_F_PRIO_WR);
1903
1904 PCM_GIANT_LEAVE(d);
1905
1906 return (0);
1907}
1908
1909#ifdef USING_DEVFS
1910
1911/* So much for dev_stdclone() */
1912static int
1913dsp_stdclone(char *name, char *namep, char *sep, int use_sep, int *u, int *c)
1914{

--- 67 unchanged lines hidden (view full) ---

1982 cunit = -1;
1983 devtype = -1;
1984 devhw = 0;
1985 devcmax = -1;
1986 tumax = -1;
1987 devname = NULL;
1988 devsep = NULL;
1989
1990 for (i = 0; unit == -1 &&
1991 i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
1992 devtype = dsp_cdevs[i].type;
1993 devname = dsp_cdevs[i].name;
1994 devsep = dsp_cdevs[i].sep;
1995 devhw = dsp_cdevs[i].hw;
1996 devcmax = dsp_cdevs[i].max - 1;
1997 if (strcmp(name, devname) == 0)
1998 unit = snd_unit;
1999 else if (dsp_stdclone(name, devname, devsep,
2000 dsp_cdevs[i].use_sep, &unit, &cunit) != 0) {
2001 unit = -1;
2002 cunit = -1;
2003 }
2004 }
2005
2006 d = devclass_get_softc(pcm_devclass, unit);
2007 if (!PCM_REGISTERED(d) || d->clones == NULL)
2008 return;
2009
2010 /* XXX Need Giant magic entry ??? */
2011
2012 pcm_lock(d);
2013 if (snd_clone_disabled(d->clones)) {
2014 pcm_unlock(d);
2015 return;
2016 }
2017
2018 PCM_WAIT(d);
2019 PCM_ACQUIRE(d);
2020 pcm_unlock(d);
2021
2022 udcmask = snd_u2unit(unit) | snd_d2unit(devtype);
2023
2024 if (devhw != 0) {
2025 KASSERT(devcmax <= dsp_cmax,
2026 ("overflow: devcmax=%d, dsp_cmax=%d", devcmax, dsp_cmax));
2027 if (cunit > devcmax) {
2028 PCM_RELEASE_QUICK(d);
2029 return;
2030 }
2031 udcmask |= snd_c2unit(cunit);
2032 CHN_FOREACH(c, d, channels.pcm) {
2033 CHN_LOCK(c);
2034 if (c->unit != udcmask) {
2035 CHN_UNLOCK(c);
2036 continue;

--- 33 unchanged lines hidden (view full) ---

2070 udcmask &= ~snd_c2unit(cunit);
2071 tumax = snd_clone_getmaxunit(d->clones);
2072 if (cunit > tumax)
2073 snd_clone_setmaxunit(d->clones, cunit);
2074 else
2075 tumax = -1;
2076 goto dsp_clone_alloc;
2077 }
2078 PCM_RELEASE_QUICK(d);
2079 return;
2080 }
2081
2082dsp_clone_alloc:
2083 ce = snd_clone_alloc(d->clones, dev, &cunit, udcmask);
2084 if (tumax != -1)
2085 snd_clone_setmaxunit(d->clones, tumax);
2086 if (ce != NULL) {
2087 udcmask |= snd_c2unit(cunit);
2088 *dev = make_dev(&dsp_cdevsw, unit2minor(udcmask),
2089 UID_ROOT, GID_WHEEL, 0666, "%s%d%s%d",
2090 devname, unit, devsep, cunit);
2091 snd_clone_register(ce, *dev);
2092 }
2093
2094 PCM_RELEASE_QUICK(d);
2095
2096 if (*dev != NULL)
2097 dev_ref(*dev);
2098}
2099
2100static void
2101dsp_sysinit(void *p)
2102{
2103 if (dsp_ehtag != NULL)

--- 73 unchanged lines hidden (view full) ---

2177 */
2178int
2179dsp_oss_audioinfo(struct cdev *i_dev, oss_audioinfo *ai)
2180{
2181 struct pcmchan_caps *caps;
2182 struct pcm_channel *ch;
2183 struct snddev_info *d;
2184 uint32_t fmts;
2185 int i, nchan, *rates, minch, maxch;
2186 char *devname, buf[CHN_NAMELEN];
2187
2188 /*
2189 * If probing the device that received the ioctl, make sure it's a
2190 * DSP device. (Users may use this ioctl with /dev/mixer and
2191 * /dev/midi.)
2192 */
2193 if (ai->dev == -1 && i_dev->si_devsw != &dsp_cdevsw)
2194 return (EINVAL);
2195
2196 ch = NULL;
2197 devname = NULL;
2198 nchan = 0;
2199 bzero(buf, sizeof(buf));
2200
2201 /*
2202 * Search for the requested audio device (channel). Start by
2203 * iterating over pcm devices.
2204 */
2205 for (i = 0; pcm_devclass != NULL &&
2206 i < devclass_get_maxunit(pcm_devclass); i++) {
2207 d = devclass_get_softc(pcm_devclass, i);
2208 if (!PCM_REGISTERED(d))
2209 continue;
2210
2211 /* XXX Need Giant magic entry ??? */
2212
2213 /* See the note in function docblock */
2214 mtx_assert(d->lock, MA_NOTOWNED);
2215 pcm_lock(d);
2216
2217 CHN_FOREACH(ch, d, channels.pcm) {
2218 mtx_assert(ch->lock, MA_NOTOWNED);
2219 CHN_LOCK(ch);
2220 if (ai->dev == -1) {
2221 if (DSP_REGISTERED(d, i_dev) &&
2222 (ch == PCM_RDCH(i_dev) || /* record ch */
2223 ch == PCM_WRCH(i_dev))) { /* playback ch */
2224 devname = dsp_unit2name(buf,
2225 sizeof(buf), ch->unit);
2226 }
2227 } else if (ai->dev == nchan) {
2228 devname = dsp_unit2name(buf, sizeof(buf),
2229 ch->unit);
2230 }
2231 if (devname != NULL)
2232 break;
2233 CHN_UNLOCK(ch);
2234 ++nchan;
2235 }
2236
2237 if (devname != NULL) {
2238 /*
2239 * At this point, the following synchronization stuff
2240 * has happened:
2241 * - a specific PCM device is locked.
2242 * - a specific audio channel has been locked, so be
2243 * sure to unlock when exiting;
2244 */
2245
2246 caps = chn_getcaps(ch);
2247
2248 /*
2249 * With all handles collected, zero out the user's
2250 * container and begin filling in its fields.
2251 */
2252 bzero((void *)ai, sizeof(oss_audioinfo));
2253
2254 ai->dev = nchan;
2255 strlcpy(ai->name, ch->name, sizeof(ai->name));
2256
2257 if ((ch->flags & CHN_F_BUSY) == 0)
2258 ai->busy = 0;
2259 else
2260 ai->busy = (ch->direction == PCMDIR_PLAY) ? OPEN_WRITE : OPEN_READ;
2261
2262 /**
2263 * @note
2264 * @c cmd - OSSv4 docs: "Only supported under Linux at
2265 * this moment." Cop-out, I know, but I'll save
2266 * running around in the process table for later.
2267 * Is there a risk of leaking information?
2268 */
2269 ai->pid = ch->pid;
2270
2271 /*
2272 * These flags stolen from SNDCTL_DSP_GETCAPS handler.
2273 * Note, however, that a single channel operates in
2274 * only one direction, so DSP_CAP_DUPLEX is out.
2275 */
2276 /**
2277 * @todo @c SNDCTL_AUDIOINFO::caps - Make drivers keep
2278 * these in pcmchan::caps?
2279 */
2280 ai->caps = DSP_CAP_REALTIME | DSP_CAP_MMAP | DSP_CAP_TRIGGER;
2281
2282 /*
2283 * Collect formats supported @b natively by the
2284 * device. Also determine min/max channels. (I.e.,
2285 * mono, stereo, or both?)
2286 *
2287 * If any channel is stereo, maxch = 2;
2288 * if all channels are stereo, minch = 2, too;
2289 * if any channel is mono, minch = 1;
2290 * and if all channels are mono, maxch = 1.
2291 */
2292 minch = 0;
2293 maxch = 0;
2294 fmts = 0;
2295 for (i = 0; caps->fmtlist[i]; i++) {
2296 fmts |= caps->fmtlist[i];
2297 if (caps->fmtlist[i] & AFMT_STEREO) {
2298 minch = (minch == 0) ? 2 : minch;
2299 maxch = 2;
2300 } else {
2301 minch = 1;
2302 maxch = (maxch == 0) ? 1 : maxch;
2303 }
2304 }
2305
2306 if (ch->direction == PCMDIR_PLAY)
2307 ai->oformats = fmts;
2308 else
2309 ai->iformats = fmts;
2310
2311 /**
2312 * @note
2313 * @c magic - OSSv4 docs: "Reserved for internal use
2314 * by OSS."
2315 *
2316 * @par
2317 * @c card_number - OSSv4 docs: "Number of the sound
2318 * card where this device belongs or -1 if this
2319 * information is not available. Applications
2320 * should normally not use this field for any
2321 * purpose."
2322 */
2323 ai->card_number = -1;
2324 /**
2325 * @todo @c song_name - depends first on
2326 * SNDCTL_[GS]ETSONG @todo @c label - depends
2327 * on SNDCTL_[GS]ETLABEL
2328 * @todo @c port_number - routing information?
2329 */
2330 ai->port_number = -1;
2331 ai->mixer_dev = (d->mixer_dev != NULL) ? PCMUNIT(d->mixer_dev) : -1;
2332 /**
2333 * @note
2334 * @c real_device - OSSv4 docs: "Obsolete."
2335 */
2336 ai->real_device = -1;
2337 strlcpy(ai->devnode, devname, sizeof(ai->devnode));
2338 ai->enabled = device_is_attached(d->dev) ? 1 : 0;
2339 /**
2340 * @note
2341 * @c flags - OSSv4 docs: "Reserved for future use."
2342 *
2343 * @note
2344 * @c binding - OSSv4 docs: "Reserved for future use."
2345 *
2346 * @todo @c handle - haven't decided how to generate
2347 * this yet; bus, vendor, device IDs?
2348 */
2349 ai->min_rate = caps->minspeed;
2350 ai->max_rate = caps->maxspeed;
2351
2352 ai->min_channels = minch;
2353 ai->max_channels = maxch;
2354
2355 ai->nrates = chn_getrates(ch, &rates);
2356 if (ai->nrates > OSS_MAX_SAMPLE_RATES)
2357 ai->nrates = OSS_MAX_SAMPLE_RATES;
2358
2359 for (i = 0; i < ai->nrates; i++)
2360 ai->rates[i] = rates[i];
2361
2362 CHN_UNLOCK(ch);
2363 }
2364
2365 pcm_unlock(d);
2366
2367 if (devname != NULL)
2368 return (0);
2369 }
2370
2371 /* Exhausted the search -- nothing is locked, so return. */
2372 return (EINVAL);
2373}
2374
2375/**
2376 * @brief Assigns a PCM channel to a sync group.
2377 *
2378 * Sync groups are used to enable audio operations on multiple devices
2379 * simultaneously. They may be used with any number of devices and may
2380 * span across applications. Devices are added to groups with

--- 154 unchanged lines hidden (view full) ---

2535
2536 if (sg_ids[0])
2537 free_unr(pcmsg_unrhdr, sg_ids[0]);
2538 if (sg_ids[1])
2539 free_unr(pcmsg_unrhdr, sg_ids[1]);
2540 if (sg_ids[2])
2541 free_unr(pcmsg_unrhdr, sg_ids[2]);
2542
2543 return (ret);
2544}
2545
2546/**
2547 * @brief Launch a sync group into action
2548 *
2549 * Sync groups are established via SNDCTL_DSP_SYNCGROUP. This function
2550 * iterates over all members, triggering them along the way.
2551 *

--- 49 unchanged lines hidden (view full) ---

2601 SLIST_FOREACH(sm_tmp, &sg->members, link) {
2602 /* sm is the member already locked */
2603 if (sm == sm_tmp)
2604 break;
2605 CHN_UNLOCK(sm_tmp->ch);
2606 }
2607
2608 /** @todo Is PRIBIO correct/ */
2609 ret = msleep(sm, &snd_pcm_syncgroups_mtx,
2610 PRIBIO | PCATCH, "pcmsg", timo);
2611 if (ret == EINTR || ret == ERESTART)
2612 break;
2613
2614 needlocks = 1;
2615 ret = 0; /* Assumes ret == EAGAIN... */
2616 }
2617 }
2618 } while (needlocks && ret == 0);

--- 21 unchanged lines hidden (view full) ---

2640
2641 /*
2642 * Free_unr() may sleep, so be sure to give up the syncgroup lock
2643 * first.
2644 */
2645 if (ret == 0)
2646 free_unr(pcmsg_unrhdr, sg_id);
2647
2648 return (ret);
2649}
2650
2651/**
2652 * @brief Handler for SNDCTL_DSP_POLICY
2653 *
2654 * The SNDCTL_DSP_POLICY ioctl is a simpler interface to control fragment
2655 * size and count like with SNDCTL_DSP_SETFRAGMENT. Instead of the user
2656 * specifying those two parameters, s/he simply selects a number from 0..10

--- 22 unchanged lines hidden (view full) ---

2679 * @retval 0 constant (for now)
2680 */
2681static int
2682dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy)
2683{
2684 int ret;
2685
2686 if (policy < CHN_POLICY_MIN || policy > CHN_POLICY_MAX)
2687 return (EIO);
2688
2689 /* Default: success */
2690 ret = 0;
2691
2692 if (rdch) {
2693 CHN_LOCK(rdch);
2694 ret = chn_setlatency(rdch, policy);
2695 CHN_UNLOCK(rdch);
2696 }
2697
2698 if (wrch && ret == 0) {
2699 CHN_LOCK(wrch);
2700 ret = chn_setlatency(wrch, policy);
2701 CHN_UNLOCK(wrch);
2702 }
2703
2704 if (ret)
2705 ret = EIO;
2706
2707 return (ret);
2708}
2709
2710#ifdef OSSV4_EXPERIMENT
2711/**
2712 * @brief Enable or disable "cooked" mode
2713 *
2714 * This is a handler for @c SNDCTL_DSP_COOKEDMODE. When in cooked mode, which
2715 * is the default, the sound system handles rate and format conversions

--- 16 unchanged lines hidden (view full) ---

2732 * @param rdch recording channel (optional; may be NULL)
2733 * @param enabled 0 = raw mode, 1 = cooked mode
2734 *
2735 * @retval EINVAL Operation not yet supported.
2736 */
2737static int
2738dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled)
2739{
2740 return (EINVAL);
2741}
2742
2743/**
2744 * @brief Retrieve channel interleaving order
2745 *
2746 * This is the handler for @c SNDCTL_DSP_GET_CHNORDER.
2747 *
2748 * See @c http://manuals.opensound.com/developer/SNDCTL_DSP_GET_CHNORDER.html

--- 6 unchanged lines hidden (view full) ---

2755 * @param rdch recording channel (optional; may be NULL)
2756 * @param map channel map (result will be stored there)
2757 *
2758 * @retval EINVAL Operation not yet supported.
2759 */
2760static int
2761dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2762{
2763 return (EINVAL);
2764}
2765
2766/**
2767 * @brief Specify channel interleaving order
2768 *
2769 * This is the handler for @c SNDCTL_DSP_SET_CHNORDER.
2770 *
2771 * @note As the ioctl definition is still under construction, FreeBSD
2772 * does not currently support @c SNDCTL_DSP_SET_CHNORDER.
2773 *
2774 * @param wrch playback channel (optional; may be NULL)
2775 * @param rdch recording channel (optional; may be NULL)
2776 * @param map channel map
2777 *
2778 * @retval EINVAL Operation not yet supported.
2779 */
2780static int
2781dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map)
2782{
2783 return (EINVAL);
2784}
2785
2786/**
2787 * @brief Retrieve an audio device's label
2788 *
2789 * This is a handler for the @c SNDCTL_GETLABEL ioctl.
2790 *
2791 * See @c http://manuals.opensound.com/developer/SNDCTL_GETLABEL.html

--- 11 unchanged lines hidden (view full) ---

2803 * @param rdch recording channel (optional; may be NULL)
2804 * @param label label gets copied here
2805 *
2806 * @retval EINVAL Operation not yet supported.
2807 */
2808static int
2809dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2810{
2811 return (EINVAL);
2812}
2813
2814/**
2815 * @brief Specify an audio device's label
2816 *
2817 * This is a handler for the @c SNDCTL_SETLABEL ioctl. Please see the
2818 * comments for @c dsp_oss_getlabel immediately above.
2819 *

--- 7 unchanged lines hidden (view full) ---

2827 * @param rdch recording channel (optional; may be NULL)
2828 * @param label label gets copied from here
2829 *
2830 * @retval EINVAL Operation not yet supported.
2831 */
2832static int
2833dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label)
2834{
2835 return (EINVAL);
2836}
2837
2838/**
2839 * @brief Retrieve name of currently played song
2840 *
2841 * This is a handler for the @c SNDCTL_GETSONG ioctl. Audio players could
2842 * tell the system the name of the currently playing song, which would be
2843 * visible in @c /dev/sndstat.

--- 8 unchanged lines hidden (view full) ---

2852 * @param rdch recording channel (optional; may be NULL)
2853 * @param song song name gets copied here
2854 *
2855 * @retval EINVAL Operation not yet supported.
2856 */
2857static int
2858dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2859{
2860 return (EINVAL);
2861}
2862
2863/**
2864 * @brief Retrieve name of currently played song
2865 *
2866 * This is a handler for the @c SNDCTL_SETSONG ioctl. Audio players could
2867 * tell the system the name of the currently playing song, which would be
2868 * visible in @c /dev/sndstat.

--- 8 unchanged lines hidden (view full) ---

2877 * @param rdch recording channel (optional; may be NULL)
2878 * @param song song name gets copied from here
2879 *
2880 * @retval EINVAL Operation not yet supported.
2881 */
2882static int
2883dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song)
2884{
2885 return (EINVAL);
2886}
2887
2888/**
2889 * @brief Rename a device
2890 *
2891 * This is a handler for the @c SNDCTL_SETNAME ioctl.
2892 *
2893 * See @c http://manuals.opensound.com/developer/SNDCTL_SETNAME.html for

--- 12 unchanged lines hidden (view full) ---

2906 * @param rdch recording channel (optional; may be NULL)
2907 * @param name new device name gets copied from here
2908 *
2909 * @retval EINVAL Operation not yet supported.
2910 */
2911static int
2912dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name)
2913{
2914 return (EINVAL);
2915}
2916#endif /* !OSSV4_EXPERIMENT */