-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
dsp.c
3379 lines (3001 loc) · 81.9 KB
/
dsp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
* Portions Copyright (c) Ryan Beasley <ryan.beasley@gmail.com> - GSoC 2006
* Copyright (c) 1999 Cameron Grant <cg@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef HAVE_KERNEL_OPTION_HEADERS
#include "opt_snd.h"
#endif
#include <dev/sound/pcm/sound.h>
#include <sys/ctype.h>
#include <sys/lock.h>
#include <sys/rwlock.h>
#include <sys/sysent.h>
#include <vm/vm.h>
#include <vm/vm_object.h>
#include <vm/vm_page.h>
#include <vm/vm_pager.h>
SND_DECLARE_FILE("$FreeBSD$");
static int dsp_mmap_allow_prot_exec = 0;
SYSCTL_INT(_hw_snd, OID_AUTO, compat_linux_mmap, CTLFLAG_RWTUN,
&dsp_mmap_allow_prot_exec, 0,
"linux mmap compatibility (-1=force disable 0=auto 1=force enable)");
static int dsp_basename_clone = 1;
SYSCTL_INT(_hw_snd, OID_AUTO, basename_clone, CTLFLAG_RWTUN,
&dsp_basename_clone, 0,
"DSP basename cloning (0: Disable; 1: Enabled)");
struct dsp_cdevinfo {
struct pcm_channel *rdch, *wrch;
struct pcm_channel *volch;
int busy, simplex;
TAILQ_ENTRY(dsp_cdevinfo) link;
};
#define PCM_RDCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->rdch)
#define PCM_WRCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->wrch)
#define PCM_VOLCH(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->volch)
#define PCM_SIMPLEX(x) (((struct dsp_cdevinfo *)(x)->si_drv1)->simplex)
#define DSP_CDEVINFO_CACHESIZE 8
#define DSP_REGISTERED(x, y) (PCM_REGISTERED(x) && \
(y) != NULL && (y)->si_drv1 != NULL)
#define OLDPCM_IOCTL
static d_open_t dsp_open;
static d_close_t dsp_close;
static d_read_t dsp_read;
static d_write_t dsp_write;
static d_ioctl_t dsp_ioctl;
static d_poll_t dsp_poll;
static d_mmap_t dsp_mmap;
static d_mmap_single_t dsp_mmap_single;
struct cdevsw dsp_cdevsw = {
.d_version = D_VERSION,
.d_open = dsp_open,
.d_close = dsp_close,
.d_read = dsp_read,
.d_write = dsp_write,
.d_ioctl = dsp_ioctl,
.d_poll = dsp_poll,
.d_mmap = dsp_mmap,
.d_mmap_single = dsp_mmap_single,
.d_name = "dsp",
};
static eventhandler_tag dsp_ehtag = NULL;
static int dsp_umax = -1;
static int dsp_cmax = -1;
static int dsp_oss_syncgroup(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_syncgroup *group);
static int dsp_oss_syncstart(int sg_id);
static int dsp_oss_policy(struct pcm_channel *wrch, struct pcm_channel *rdch, int policy);
static int dsp_oss_cookedmode(struct pcm_channel *wrch, struct pcm_channel *rdch, int enabled);
static int dsp_oss_getchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
static int dsp_oss_setchnorder(struct pcm_channel *wrch, struct pcm_channel *rdch, unsigned long long *map);
static int dsp_oss_getchannelmask(struct pcm_channel *wrch, struct pcm_channel *rdch, int *mask);
#ifdef OSSV4_EXPERIMENT
static int dsp_oss_getlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
static int dsp_oss_setlabel(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_label_t *label);
static int dsp_oss_getsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
static int dsp_oss_setsong(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *song);
static int dsp_oss_setname(struct pcm_channel *wrch, struct pcm_channel *rdch, oss_longname_t *name);
#endif
static struct snddev_info *
dsp_get_info(struct cdev *dev)
{
return (devclass_get_softc(pcm_devclass, PCMUNIT(dev)));
}
static uint32_t
dsp_get_flags(struct cdev *dev)
{
device_t bdev;
bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
return ((bdev != NULL) ? pcm_getflags(bdev) : 0xffffffff);
}
static void
dsp_set_flags(struct cdev *dev, uint32_t flags)
{
device_t bdev;
bdev = devclass_get_device(pcm_devclass, PCMUNIT(dev));
if (bdev != NULL)
pcm_setflags(bdev, flags);
}
/*
* return the channels associated with an open device instance.
* lock channels specified.
*/
static int
getchns(struct cdev *dev, struct pcm_channel **rdch, struct pcm_channel **wrch,
uint32_t prio)
{
struct snddev_info *d;
struct pcm_channel *ch;
uint32_t flags;
if (PCM_SIMPLEX(dev) != 0) {
d = dsp_get_info(dev);
if (!PCM_REGISTERED(d))
return (ENXIO);
PCM_LOCK(d);
PCM_WAIT(d);
PCM_ACQUIRE(d);
/*
* Note: order is important -
* pcm flags -> prio query flags -> wild guess
*/
ch = NULL;
flags = dsp_get_flags(dev);
if (flags & SD_F_PRIO_WR) {
ch = PCM_RDCH(dev);
PCM_RDCH(dev) = NULL;
} else if (flags & SD_F_PRIO_RD) {
ch = PCM_WRCH(dev);
PCM_WRCH(dev) = NULL;
} else if (prio & SD_F_PRIO_WR) {
ch = PCM_RDCH(dev);
PCM_RDCH(dev) = NULL;
flags |= SD_F_PRIO_WR;
} else if (prio & SD_F_PRIO_RD) {
ch = PCM_WRCH(dev);
PCM_WRCH(dev) = NULL;
flags |= SD_F_PRIO_RD;
} else if (PCM_WRCH(dev) != NULL) {
ch = PCM_RDCH(dev);
PCM_RDCH(dev) = NULL;
flags |= SD_F_PRIO_WR;
} else if (PCM_RDCH(dev) != NULL) {
ch = PCM_WRCH(dev);
PCM_WRCH(dev) = NULL;
flags |= SD_F_PRIO_RD;
}
PCM_SIMPLEX(dev) = 0;
dsp_set_flags(dev, flags);
if (ch != NULL) {
CHN_LOCK(ch);
pcm_chnref(ch, -1);
pcm_chnrelease(ch);
}
PCM_RELEASE(d);
PCM_UNLOCK(d);
}
*rdch = PCM_RDCH(dev);
*wrch = PCM_WRCH(dev);
if (*rdch != NULL && (prio & SD_F_PRIO_RD))
CHN_LOCK(*rdch);
if (*wrch != NULL && (prio & SD_F_PRIO_WR))
CHN_LOCK(*wrch);
return (0);
}
/* unlock specified channels */
static void
relchns(struct cdev *dev, struct pcm_channel *rdch, struct pcm_channel *wrch,
uint32_t prio)
{
if (wrch != NULL && (prio & SD_F_PRIO_WR))
CHN_UNLOCK(wrch);
if (rdch != NULL && (prio & SD_F_PRIO_RD))
CHN_UNLOCK(rdch);
}
static void
dsp_cdevinfo_alloc(struct cdev *dev,
struct pcm_channel *rdch, struct pcm_channel *wrch,
struct pcm_channel *volch)
{
struct snddev_info *d;
struct dsp_cdevinfo *cdi;
int simplex;
d = dsp_get_info(dev);
KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 == NULL &&
((rdch == NULL && wrch == NULL) || rdch != wrch),
("bogus %s(), what are you trying to accomplish here?", __func__));
PCM_BUSYASSERT(d);
PCM_LOCKASSERT(d);
simplex = (dsp_get_flags(dev) & SD_F_SIMPLEX) ? 1 : 0;
/*
* Scan for free instance entry and put it into the end of list.
* Create new one if necessary.
*/
TAILQ_FOREACH(cdi, &d->dsp_cdevinfo_pool, link) {
if (cdi->busy != 0)
break;
cdi->rdch = rdch;
cdi->wrch = wrch;
cdi->volch = volch;
cdi->simplex = simplex;
cdi->busy = 1;
TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
dev->si_drv1 = cdi;
return;
}
PCM_UNLOCK(d);
cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
PCM_LOCK(d);
cdi->rdch = rdch;
cdi->wrch = wrch;
cdi->volch = volch;
cdi->simplex = simplex;
cdi->busy = 1;
TAILQ_INSERT_TAIL(&d->dsp_cdevinfo_pool, cdi, link);
dev->si_drv1 = cdi;
}
static void
dsp_cdevinfo_free(struct cdev *dev)
{
struct snddev_info *d;
struct dsp_cdevinfo *cdi, *tmp;
uint32_t flags;
int i;
d = dsp_get_info(dev);
KASSERT(PCM_REGISTERED(d) && dev != NULL && dev->si_drv1 != NULL &&
PCM_RDCH(dev) == NULL && PCM_WRCH(dev) == NULL &&
PCM_VOLCH(dev) == NULL,
("bogus %s(), what are you trying to accomplish here?", __func__));
PCM_BUSYASSERT(d);
PCM_LOCKASSERT(d);
cdi = dev->si_drv1;
dev->si_drv1 = NULL;
cdi->rdch = NULL;
cdi->wrch = NULL;
cdi->volch = NULL;
cdi->simplex = 0;
cdi->busy = 0;
/*
* Once it is free, move it back to the beginning of list for
* faster new entry allocation.
*/
TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
/*
* Scan the list, cache free entries up to DSP_CDEVINFO_CACHESIZE.
* Reset simplex flags.
*/
flags = dsp_get_flags(dev) & ~SD_F_PRIO_SET;
i = DSP_CDEVINFO_CACHESIZE;
TAILQ_FOREACH_SAFE(cdi, &d->dsp_cdevinfo_pool, link, tmp) {
if (cdi->busy != 0) {
if (cdi->simplex == 0) {
if (cdi->rdch != NULL)
flags |= SD_F_PRIO_RD;
if (cdi->wrch != NULL)
flags |= SD_F_PRIO_WR;
}
} else {
if (i == 0) {
TAILQ_REMOVE(&d->dsp_cdevinfo_pool, cdi, link);
free(cdi, M_DEVBUF);
} else
i--;
}
}
dsp_set_flags(dev, flags);
}
void
dsp_cdevinfo_init(struct snddev_info *d)
{
struct dsp_cdevinfo *cdi;
int i;
KASSERT(d != NULL, ("NULL snddev_info"));
PCM_BUSYASSERT(d);
PCM_UNLOCKASSERT(d);
TAILQ_INIT(&d->dsp_cdevinfo_pool);
for (i = 0; i < DSP_CDEVINFO_CACHESIZE; i++) {
cdi = malloc(sizeof(*cdi), M_DEVBUF, M_WAITOK | M_ZERO);
TAILQ_INSERT_HEAD(&d->dsp_cdevinfo_pool, cdi, link);
}
}
void
dsp_cdevinfo_flush(struct snddev_info *d)
{
struct dsp_cdevinfo *cdi, *tmp;
KASSERT(d != NULL, ("NULL snddev_info"));
PCM_BUSYASSERT(d);
PCM_UNLOCKASSERT(d);
cdi = TAILQ_FIRST(&d->dsp_cdevinfo_pool);
while (cdi != NULL) {
tmp = TAILQ_NEXT(cdi, link);
free(cdi, M_DEVBUF);
cdi = tmp;
}
TAILQ_INIT(&d->dsp_cdevinfo_pool);
}
/* duplex / simplex cdev type */
enum {
DSP_CDEV_TYPE_RDONLY, /* simplex read-only (record) */
DSP_CDEV_TYPE_WRONLY, /* simplex write-only (play) */
DSP_CDEV_TYPE_RDWR /* duplex read, write, or both */
};
enum {
DSP_CDEV_VOLCTL_NONE,
DSP_CDEV_VOLCTL_READ,
DSP_CDEV_VOLCTL_WRITE
};
#define DSP_F_VALID(x) ((x) & (FREAD | FWRITE))
#define DSP_F_DUPLEX(x) (((x) & (FREAD | FWRITE)) == (FREAD | FWRITE))
#define DSP_F_SIMPLEX(x) (!DSP_F_DUPLEX(x))
#define DSP_F_READ(x) ((x) & FREAD)
#define DSP_F_WRITE(x) ((x) & FWRITE)
static const struct {
int type;
char *name;
char *sep;
char *alias;
int use_sep;
int hw;
int max;
int volctl;
uint32_t fmt, spd;
int query;
} dsp_cdevs[] = {
{ SND_DEV_DSP, "dsp", ".", NULL, 0, 0, 0, 0,
SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_AUDIO, "audio", ".", NULL, 0, 0, 0, 0,
SND_FORMAT(AFMT_MU_LAW, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_DSP16, "dspW", ".", NULL, 0, 0, 0, 0,
SND_FORMAT(AFMT_S16_LE, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_DSPHW_PLAY, "dsp", ".p", NULL, 1, 1, SND_MAXHWCHAN, 1,
SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
{ SND_DEV_DSPHW_VPLAY, "dsp", ".vp", NULL, 1, 1, SND_MAXVCHANS, 1,
SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_WRONLY },
{ SND_DEV_DSPHW_REC, "dsp", ".r", NULL, 1, 1, SND_MAXHWCHAN, 1,
SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
{ SND_DEV_DSPHW_VREC, "dsp", ".vr", NULL, 1, 1, SND_MAXVCHANS, 1,
SND_FORMAT(AFMT_S16_LE, 2, 0), 48000, DSP_CDEV_TYPE_RDONLY },
{ SND_DEV_DSPHW_CD, "dspcd", ".", NULL, 0, 0, 0, 0,
SND_FORMAT(AFMT_S16_LE, 2, 0), 44100, DSP_CDEV_TYPE_RDWR },
/* Low priority, OSSv4 aliases. */
{ SND_DEV_DSP, "dsp_ac3", ".", "dsp", 0, 0, 0, 0,
SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_DSP, "dsp_mmap", ".", "dsp", 0, 0, 0, 0,
SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_DSP, "dsp_multich", ".", "dsp", 0, 0, 0, 0,
SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_DSP, "dsp_spdifout", ".", "dsp", 0, 0, 0, 0,
SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
{ SND_DEV_DSP, "dsp_spdifin", ".", "dsp", 0, 0, 0, 0,
SND_FORMAT(AFMT_U8, 1, 0), DSP_DEFAULT_SPEED,
DSP_CDEV_TYPE_RDWR },
};
#define DSP_FIXUP_ERROR() do { \
prio = dsp_get_flags(i_dev); \
if (!DSP_F_VALID(flags)) \
error = EINVAL; \
if (!DSP_F_DUPLEX(flags) && \
((DSP_F_READ(flags) && d->reccount == 0) || \
(DSP_F_WRITE(flags) && d->playcount == 0))) \
error = ENOTSUP; \
else if (!DSP_F_DUPLEX(flags) && (prio & SD_F_SIMPLEX) && \
((DSP_F_READ(flags) && (prio & SD_F_PRIO_WR)) || \
(DSP_F_WRITE(flags) && (prio & SD_F_PRIO_RD)))) \
error = EBUSY; \
else if (DSP_REGISTERED(d, i_dev)) \
error = EBUSY; \
} while (0)
static int
dsp_open(struct cdev *i_dev, int flags, int mode, struct thread *td)
{
struct pcm_channel *rdch, *wrch;
struct snddev_info *d;
uint32_t fmt, spd, prio, volctl;
int i, error, rderror, wrerror, devtype, wdevunit, rdevunit;
/* Kind of impossible.. */
if (i_dev == NULL || td == NULL)
return (ENODEV);
d = dsp_get_info(i_dev);
if (PCM_DETACHING(d) || !PCM_REGISTERED(d))
return (EBADF);
PCM_GIANT_ENTER(d);
/* Lock snddev so nobody else can monkey with it. */
PCM_LOCK(d);
PCM_WAIT(d);
/*
* Try to acquire cloned device before someone else pick it.
* ENODEV means this is not a cloned droids.
*/
error = snd_clone_acquire(i_dev);
if (!(error == 0 || error == ENODEV)) {
DSP_FIXUP_ERROR();
PCM_UNLOCK(d);
PCM_GIANT_EXIT(d);
return (error);
}
error = 0;
DSP_FIXUP_ERROR();
if (error != 0) {
(void)snd_clone_release(i_dev);
PCM_UNLOCK(d);
PCM_GIANT_EXIT(d);
return (error);
}
/*
* That is just enough. Acquire and unlock pcm lock so
* the other will just have to wait until we finish doing
* everything.
*/
PCM_ACQUIRE(d);
PCM_UNLOCK(d);
devtype = PCMDEV(i_dev);
wdevunit = -1;
rdevunit = -1;
fmt = 0;
spd = 0;
volctl = DSP_CDEV_VOLCTL_NONE;
for (i = 0; i < (sizeof(dsp_cdevs) / sizeof(dsp_cdevs[0])); i++) {
if (devtype != dsp_cdevs[i].type || dsp_cdevs[i].alias != NULL)
continue;
/*
* Volume control only valid for DSPHW devices,
* and it must be opened in opposite direction be it
* simplex or duplex. Anything else will be handled
* as usual.
*/
if (dsp_cdevs[i].query == DSP_CDEV_TYPE_WRONLY) {
if (dsp_cdevs[i].volctl != 0 &&
DSP_F_READ(flags)) {
volctl = DSP_CDEV_VOLCTL_WRITE;
flags &= ~FREAD;
flags |= FWRITE;
}
if (DSP_F_READ(flags)) {
(void)snd_clone_release(i_dev);
PCM_RELEASE_QUICK(d);
PCM_GIANT_EXIT(d);
return (ENOTSUP);
}
wdevunit = dev2unit(i_dev);
} else if (dsp_cdevs[i].query == DSP_CDEV_TYPE_RDONLY) {
if (dsp_cdevs[i].volctl != 0 &&
DSP_F_WRITE(flags)) {
volctl = DSP_CDEV_VOLCTL_READ;
flags &= ~FWRITE;
flags |= FREAD;
}
if (DSP_F_WRITE(flags)) {
(void)snd_clone_release(i_dev);
PCM_RELEASE_QUICK(d);
PCM_GIANT_EXIT(d);
return (ENOTSUP);
}
rdevunit = dev2unit(i_dev);
}
fmt = dsp_cdevs[i].fmt;
spd = dsp_cdevs[i].spd;
break;
}
/* No matching devtype? */
if (fmt == 0 || spd == 0)
panic("impossible devtype %d", devtype);
rdch = NULL;
wrch = NULL;
rderror = 0;
wrerror = 0;
/*
* if we get here, the open request is valid- either:
* * we were previously not open
* * we were open for play xor record and the opener wants
* the non-open direction
*/
if (DSP_F_READ(flags)) {
/* open for read */
rderror = pcm_chnalloc(d, &rdch, PCMDIR_REC,
td->td_proc->p_pid, td->td_proc->p_comm, rdevunit);
if (rderror == 0 && chn_reset(rdch, fmt, spd) != 0)
rderror = ENXIO;
if (volctl == DSP_CDEV_VOLCTL_READ)
rderror = 0;
if (rderror != 0) {
if (rdch != NULL)
pcm_chnrelease(rdch);
if (!DSP_F_DUPLEX(flags)) {
(void)snd_clone_release(i_dev);
PCM_RELEASE_QUICK(d);
PCM_GIANT_EXIT(d);
return (rderror);
}
rdch = NULL;
} else if (volctl == DSP_CDEV_VOLCTL_READ) {
if (rdch != NULL) {
pcm_chnref(rdch, 1);
pcm_chnrelease(rdch);
}
} else {
if (flags & O_NONBLOCK)
rdch->flags |= CHN_F_NBIO;
if (flags & O_EXCL)
rdch->flags |= CHN_F_EXCLUSIVE;
pcm_chnref(rdch, 1);
if (volctl == DSP_CDEV_VOLCTL_NONE)
chn_vpc_reset(rdch, SND_VOL_C_PCM, 0);
CHN_UNLOCK(rdch);
}
}
if (DSP_F_WRITE(flags)) {
/* open for write */
wrerror = pcm_chnalloc(d, &wrch, PCMDIR_PLAY,
td->td_proc->p_pid, td->td_proc->p_comm, wdevunit);
if (wrerror == 0 && chn_reset(wrch, fmt, spd) != 0)
wrerror = ENXIO;
if (volctl == DSP_CDEV_VOLCTL_WRITE)
wrerror = 0;
if (wrerror != 0) {
if (wrch != NULL)
pcm_chnrelease(wrch);
if (!DSP_F_DUPLEX(flags)) {
if (rdch != NULL) {
/*
* Lock, deref and release previously
* created record channel
*/
CHN_LOCK(rdch);
pcm_chnref(rdch, -1);
pcm_chnrelease(rdch);
}
(void)snd_clone_release(i_dev);
PCM_RELEASE_QUICK(d);
PCM_GIANT_EXIT(d);
return (wrerror);
}
wrch = NULL;
} else if (volctl == DSP_CDEV_VOLCTL_WRITE) {
if (wrch != NULL) {
pcm_chnref(wrch, 1);
pcm_chnrelease(wrch);
}
} else {
if (flags & O_NONBLOCK)
wrch->flags |= CHN_F_NBIO;
if (flags & O_EXCL)
wrch->flags |= CHN_F_EXCLUSIVE;
pcm_chnref(wrch, 1);
if (volctl == DSP_CDEV_VOLCTL_NONE)
chn_vpc_reset(wrch, SND_VOL_C_PCM, 0);
CHN_UNLOCK(wrch);
}
}
PCM_LOCK(d);
/*
* We're done. Allocate channels information for this cdev.
*/
switch (volctl) {
case DSP_CDEV_VOLCTL_READ:
KASSERT(wrch == NULL, ("wrch=%p not null!", wrch));
dsp_cdevinfo_alloc(i_dev, NULL, NULL, rdch);
break;
case DSP_CDEV_VOLCTL_WRITE:
KASSERT(rdch == NULL, ("rdch=%p not null!", rdch));
dsp_cdevinfo_alloc(i_dev, NULL, NULL, wrch);
break;
case DSP_CDEV_VOLCTL_NONE:
default:
if (wrch == NULL && rdch == NULL) {
(void)snd_clone_release(i_dev);
PCM_RELEASE(d);
PCM_UNLOCK(d);
PCM_GIANT_EXIT(d);
if (wrerror != 0)
return (wrerror);
if (rderror != 0)
return (rderror);
return (EINVAL);
}
dsp_cdevinfo_alloc(i_dev, rdch, wrch, NULL);
if (rdch != NULL)
CHN_INSERT_HEAD(d, rdch, channels.pcm.opened);
if (wrch != NULL)
CHN_INSERT_HEAD(d, wrch, channels.pcm.opened);
break;
}
/*
* Increase clone refcount for its automatic garbage collector.
*/
(void)snd_clone_ref(i_dev);
PCM_RELEASE(d);
PCM_UNLOCK(d);
PCM_GIANT_LEAVE(d);
return (0);
}
static int
dsp_close(struct cdev *i_dev, int flags, int mode, struct thread *td)
{
struct pcm_channel *rdch, *wrch, *volch;
struct snddev_info *d;
int sg_ids, rdref, wdref;
d = dsp_get_info(i_dev);
if (!DSP_REGISTERED(d, i_dev))
return (EBADF);
PCM_GIANT_ENTER(d);
PCM_LOCK(d);
PCM_WAIT(d);
PCM_ACQUIRE(d);
rdch = PCM_RDCH(i_dev);
wrch = PCM_WRCH(i_dev);
volch = PCM_VOLCH(i_dev);
PCM_RDCH(i_dev) = NULL;
PCM_WRCH(i_dev) = NULL;
PCM_VOLCH(i_dev) = NULL;
rdref = -1;
wdref = -1;
if (volch != NULL) {
if (volch == rdch)
rdref--;
else if (volch == wrch)
wdref--;
else {
CHN_LOCK(volch);
pcm_chnref(volch, -1);
CHN_UNLOCK(volch);
}
}
if (rdch != NULL)
CHN_REMOVE(d, rdch, channels.pcm.opened);
if (wrch != NULL)
CHN_REMOVE(d, wrch, channels.pcm.opened);
if (rdch != NULL || wrch != NULL) {
PCM_UNLOCK(d);
if (rdch != NULL) {
/*
* The channel itself need not be locked because:
* a) Adding a channel to a syncgroup happens only
* in dsp_ioctl(), which cannot run concurrently
* to dsp_close().
* b) The syncmember pointer (sm) is protected by
* the global syncgroup list lock.
* c) A channel can't just disappear, invalidating
* pointers, unless it's closed/dereferenced
* first.
*/
PCM_SG_LOCK();
sg_ids = chn_syncdestroy(rdch);
PCM_SG_UNLOCK();
if (sg_ids != 0)
free_unr(pcmsg_unrhdr, sg_ids);
CHN_LOCK(rdch);
pcm_chnref(rdch, rdref);
chn_abort(rdch); /* won't sleep */
rdch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
CHN_F_DEAD | CHN_F_EXCLUSIVE);
chn_reset(rdch, 0, 0);
pcm_chnrelease(rdch);
}
if (wrch != NULL) {
/*
* Please see block above.
*/
PCM_SG_LOCK();
sg_ids = chn_syncdestroy(wrch);
PCM_SG_UNLOCK();
if (sg_ids != 0)
free_unr(pcmsg_unrhdr, sg_ids);
CHN_LOCK(wrch);
pcm_chnref(wrch, wdref);
chn_flush(wrch); /* may sleep */
wrch->flags &= ~(CHN_F_RUNNING | CHN_F_MMAP |
CHN_F_DEAD | CHN_F_EXCLUSIVE);
chn_reset(wrch, 0, 0);
pcm_chnrelease(wrch);
}
PCM_LOCK(d);
}
dsp_cdevinfo_free(i_dev);
/*
* Release clone busy state and unref it so the automatic
* garbage collector will get the hint and do the remaining
* cleanup process.
*/
(void)snd_clone_release(i_dev);
/*
* destroy_dev() might sleep, so release pcm lock
* here and rely on pcm cv serialization.
*/
PCM_UNLOCK(d);
(void)snd_clone_unref(i_dev);
PCM_LOCK(d);
PCM_RELEASE(d);
PCM_UNLOCK(d);
PCM_GIANT_LEAVE(d);
return (0);
}
static __inline int
dsp_io_ops(struct cdev *i_dev, struct uio *buf)
{
struct snddev_info *d;
struct pcm_channel **ch, *rdch, *wrch;
int (*chn_io)(struct pcm_channel *, struct uio *);
int prio, ret;
pid_t runpid;
KASSERT(i_dev != NULL && buf != NULL &&
(buf->uio_rw == UIO_READ || buf->uio_rw == UIO_WRITE),
("%s(): io train wreck!", __func__));
d = dsp_get_info(i_dev);
if (PCM_DETACHING(d) || !DSP_REGISTERED(d, i_dev))
return (EBADF);
PCM_GIANT_ENTER(d);
switch (buf->uio_rw) {
case UIO_READ:
prio = SD_F_PRIO_RD;
ch = &rdch;
chn_io = chn_read;
break;
case UIO_WRITE:
prio = SD_F_PRIO_WR;
ch = &wrch;
chn_io = chn_write;
break;
default:
panic("invalid/corrupted uio direction: %d", buf->uio_rw);
break;
}
rdch = NULL;
wrch = NULL;
runpid = buf->uio_td->td_proc->p_pid;
getchns(i_dev, &rdch, &wrch, prio);
if (*ch == NULL || !((*ch)->flags & CHN_F_BUSY)) {
if (rdch != NULL || wrch != NULL)
relchns(i_dev, rdch, wrch, prio);
PCM_GIANT_EXIT(d);
return (EBADF);
}
if (((*ch)->flags & (CHN_F_MMAP | CHN_F_DEAD)) ||
(((*ch)->flags & CHN_F_RUNNING) && (*ch)->pid != runpid)) {
relchns(i_dev, rdch, wrch, prio);
PCM_GIANT_EXIT(d);
return (EINVAL);
} else if (!((*ch)->flags & CHN_F_RUNNING)) {
(*ch)->flags |= CHN_F_RUNNING;
(*ch)->pid = runpid;
}
/*
* chn_read/write must give up channel lock in order to copy bytes
* from/to userland, so up the "in progress" counter to make sure
* someone else doesn't come along and muss up the buffer.
*/
++(*ch)->inprog;
ret = chn_io(*ch, buf);
--(*ch)->inprog;
CHN_BROADCAST(&(*ch)->cv);
relchns(i_dev, rdch, wrch, prio);
PCM_GIANT_LEAVE(d);
return (ret);
}
static int
dsp_read(struct cdev *i_dev, struct uio *buf, int flag)
{
return (dsp_io_ops(i_dev, buf));
}
static int
dsp_write(struct cdev *i_dev, struct uio *buf, int flag)
{
return (dsp_io_ops(i_dev, buf));
}
static int
dsp_get_volume_channel(struct cdev *dev, struct pcm_channel **volch)
{
struct snddev_info *d;
struct pcm_channel *c;
int unit;
KASSERT(dev != NULL && volch != NULL,
("%s(): NULL query dev=%p volch=%p", __func__, dev, volch));
d = dsp_get_info(dev);
if (!PCM_REGISTERED(d)) {
*volch = NULL;
return (EINVAL);
}
PCM_UNLOCKASSERT(d);
*volch = NULL;
c = PCM_VOLCH(dev);
if (c != NULL) {
if (!(c->feederflags & (1 << FEEDER_VOLUME)))
return (-1);
*volch = c;
return (0);
}
PCM_LOCK(d);
PCM_WAIT(d);
PCM_ACQUIRE(d);
unit = dev2unit(dev);
CHN_FOREACH(c, d, channels.pcm) {
CHN_LOCK(c);
if (c->unit != unit) {
CHN_UNLOCK(c);
continue;
}
*volch = c;
pcm_chnref(c, 1);
PCM_VOLCH(dev) = c;
CHN_UNLOCK(c);
PCM_RELEASE(d);
PCM_UNLOCK(d);
return ((c->feederflags & (1 << FEEDER_VOLUME)) ? 0 : -1);
}
PCM_RELEASE(d);
PCM_UNLOCK(d);
return (EINVAL);
}
static int
dsp_ioctl_channel(struct cdev *dev, struct pcm_channel *volch, u_long cmd,
caddr_t arg)
{
struct snddev_info *d;
struct pcm_channel *rdch, *wrch;
int j, devtype, ret;
d = dsp_get_info(dev);
if (!PCM_REGISTERED(d) || !(dsp_get_flags(dev) & SD_F_VPC))
return (-1);
PCM_UNLOCKASSERT(d);
j = cmd & 0xff;
rdch = PCM_RDCH(dev);
wrch = PCM_WRCH(dev);
/* No specific channel, look into cache */
if (volch == NULL)
volch = PCM_VOLCH(dev);
/* Look harder */
if (volch == NULL) {
if (j == SOUND_MIXER_RECLEV && rdch != NULL)
volch = rdch;
else if (j == SOUND_MIXER_PCM && wrch != NULL)
volch = wrch;
}
devtype = PCMDEV(dev);
/* Look super harder */
if (volch == NULL &&
(devtype == SND_DEV_DSPHW_PLAY || devtype == SND_DEV_DSPHW_VPLAY ||
devtype == SND_DEV_DSPHW_REC || devtype == SND_DEV_DSPHW_VREC)) {
ret = dsp_get_volume_channel(dev, &volch);
if (ret != 0)
return (ret);