/
audio.c
9087 lines (8027 loc) · 224 KB
/
audio.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
/* $NetBSD: audio.c,v 1.123 2022/04/09 23:35:58 riastradh Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Andrew Doran.
*
* 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* Copyright (c) 1991-1993 Regents of the University of California.
* 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.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*/
/*
* Terminology: "sample", "channel", "frame", "block", "track":
*
* channel frame
* | ........
* v : : \
* +------:------:------:- -+------+ : +------+-.. |
* #0(L) |sample|sample|sample| .. |sample| : |sample| |
* +------:------:------:- -+------+ : +------+-.. |
* #1(R) |sample|sample|sample| .. |sample| : |sample| |
* +------:------:------:- -+------+ : +------+-.. | track
* : : : : |
* +------:------:------:- -+------+ : +------+-.. |
* |sample|sample|sample| .. |sample| : |sample| |
* +------:------:------:- -+------+ : +------+-.. |
* : : /
* ........
*
* \--------------------------------/ \--------..
* block
*
* - A "frame" is the minimum unit in the time axis direction, and consists
* of samples for the number of channels.
* - A "block" is basic length of processing. The audio layer basically
* handles audio data stream block by block, asks underlying hardware to
* process them block by block, and then the hardware raises interrupt by
* each block.
* - A "track" is single completed audio stream.
*
* For example, the hardware block is assumed to be 10 msec, and your audio
* track consists of 2.1(=3) channels 44.1kHz 16bit PCM,
*
* "channel" = 3
* "sample" = 2 [bytes]
* "frame" = 2 [bytes/sample] * 3 [channels] = 6 [bytes]
* "block" = 44100 [Hz] * (10/1000) [seconds] * 6 [bytes/frame] = 2646 [bytes]
*
* The terminologies shown here are only for this MI audio layer. Note that
* different terminologies may be used in each manufacturer's datasheet, and
* each MD driver may follow it. For example, what we call a "block" is
* called a "frame" in sys/dev/pci/yds.c.
*/
/*
* Locking: there are three locks per device.
*
* - sc_lock, provided by the underlying driver. This is an adaptive lock,
* returned in the second parameter to hw_if->get_locks(). It is known
* as the "thread lock".
*
* It serializes access to state in all places except the
* driver's interrupt service routine. This lock is taken from process
* context (example: access to /dev/audio). It is also taken from soft
* interrupt handlers in this module, primarily to serialize delivery of
* wakeups. This lock may be used/provided by modules external to the
* audio subsystem, so take care not to introduce a lock order problem.
* LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD.
*
* - sc_intr_lock, provided by the underlying driver. This may be either a
* spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE or
* IPL_SOFT*), returned in the first parameter to hw_if->get_locks(). It
* is known as the "interrupt lock".
*
* It provides atomic access to the device's hardware state, and to audio
* channel data that may be accessed by the hardware driver's ISR.
* In all places outside the ISR, sc_lock must be held before taking
* sc_intr_lock. This is to ensure that groups of hardware operations are
* made atomically. SLEEPS CANNOT OCCUR WITH THIS LOCK HELD.
*
* - sc_exlock, private to this module. This is a variable protected by
* sc_lock. It is known as the "critical section".
* Some operations release sc_lock in order to allocate memory, to wait
* for in-flight I/O to complete, to copy to/from user context, etc.
* sc_exlock provides a critical section even under the circumstance.
* "+" in following list indicates the interfaces which necessary to be
* protected by sc_exlock.
*
* List of hardware interface methods, and which locks are held when each
* is called by this module:
*
* METHOD INTR THREAD NOTES
* ----------------------- ------- ------- -------------------------
* open x x +
* close x x +
* query_format - x
* set_format - x
* round_blocksize - x
* commit_settings - x
* init_output x x
* init_input x x
* start_output x x +
* start_input x x +
* halt_output x x +
* halt_input x x +
* speaker_ctl x x
* getdev - -
* set_port - x +
* get_port - x +
* query_devinfo - x
* allocm - - +
* freem - - +
* round_buffersize - x
* get_props - - Called at attach time
* trigger_output x x +
* trigger_input x x +
* dev_ioctl - x
* get_locks - - Called at attach time
*
* In addition, there is an additional lock.
*
* - track->lock. This is an atomic variable and is similar to the
* "interrupt lock". This is one for each track. If any thread context
* (and software interrupt context) and hardware interrupt context who
* want to access some variables on this track, they must acquire this
* lock before. It protects track's consistency between hardware
* interrupt context and others.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.123 2022/04/09 23:35:58 riastradh Exp $");
#ifdef _KERNEL_OPT
#include "audio.h"
#include "midi.h"
#endif
#if NAUDIO > 0
#include <sys/types.h>
#include <sys/param.h>
#include <sys/atomic.h>
#include <sys/audioio.h>
#include <sys/conf.h>
#include <sys/cpu.h>
#include <sys/device.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/intr.h>
#include <sys/ioctl.h>
#include <sys/kauth.h>
#include <sys/kernel.h>
#include <sys/kmem.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mman.h>
#include <sys/module.h>
#include <sys/poll.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/select.h>
#include <sys/signalvar.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/syslog.h>
#include <sys/vnode.h>
#include <dev/audio/audio_if.h>
#include <dev/audio/audiovar.h>
#include <dev/audio/audiodef.h>
#include <dev/audio/linear.h>
#include <dev/audio/mulaw.h>
#include <machine/endian.h>
#include <uvm/uvm_extern.h>
#include "ioconf.h"
/*
* 0: No debug logs
* 1: action changes like open/close/set_format...
* 2: + normal operations like read/write/ioctl...
* 3: + TRACEs except interrupt
* 4: + TRACEs including interrupt
*/
//#define AUDIO_DEBUG 1
#if defined(AUDIO_DEBUG)
int audiodebug = AUDIO_DEBUG;
static void audio_vtrace(struct audio_softc *sc, const char *, const char *,
const char *, va_list);
static void audio_trace(struct audio_softc *sc, const char *, const char *, ...)
__printflike(3, 4);
static void audio_tracet(const char *, audio_track_t *, const char *, ...)
__printflike(3, 4);
static void audio_tracef(const char *, audio_file_t *, const char *, ...)
__printflike(3, 4);
/* XXX sloppy memory logger */
static void audio_mlog_init(void);
static void audio_mlog_free(void);
static void audio_mlog_softintr(void *);
extern void audio_mlog_flush(void);
extern void audio_mlog_printf(const char *, ...);
static int mlog_refs; /* reference counter */
static char *mlog_buf[2]; /* double buffer */
static int mlog_buflen; /* buffer length */
static int mlog_used; /* used length */
static int mlog_full; /* number of dropped lines by buffer full */
static int mlog_drop; /* number of dropped lines by busy */
static volatile uint32_t mlog_inuse; /* in-use */
static int mlog_wpage; /* active page */
static void *mlog_sih; /* softint handle */
static void
audio_mlog_init(void)
{
mlog_refs++;
if (mlog_refs > 1)
return;
mlog_buflen = 4096;
mlog_buf[0] = kmem_zalloc(mlog_buflen, KM_SLEEP);
mlog_buf[1] = kmem_zalloc(mlog_buflen, KM_SLEEP);
mlog_used = 0;
mlog_full = 0;
mlog_drop = 0;
mlog_inuse = 0;
mlog_wpage = 0;
mlog_sih = softint_establish(SOFTINT_SERIAL, audio_mlog_softintr, NULL);
if (mlog_sih == NULL)
printf("%s: softint_establish failed\n", __func__);
}
static void
audio_mlog_free(void)
{
mlog_refs--;
if (mlog_refs > 0)
return;
audio_mlog_flush();
if (mlog_sih)
softint_disestablish(mlog_sih);
kmem_free(mlog_buf[0], mlog_buflen);
kmem_free(mlog_buf[1], mlog_buflen);
}
/*
* Flush memory buffer.
* It must not be called from hardware interrupt context.
*/
void
audio_mlog_flush(void)
{
if (mlog_refs == 0)
return;
/* Nothing to do if already in use ? */
if (atomic_swap_32(&mlog_inuse, 1) == 1)
return;
membar_acquire();
int rpage = mlog_wpage;
mlog_wpage ^= 1;
mlog_buf[mlog_wpage][0] = '\0';
mlog_used = 0;
atomic_store_release(&mlog_inuse, 0);
if (mlog_buf[rpage][0] != '\0') {
printf("%s", mlog_buf[rpage]);
if (mlog_drop > 0)
printf("mlog_drop %d\n", mlog_drop);
if (mlog_full > 0)
printf("mlog_full %d\n", mlog_full);
}
mlog_full = 0;
mlog_drop = 0;
}
static void
audio_mlog_softintr(void *cookie)
{
audio_mlog_flush();
}
void
audio_mlog_printf(const char *fmt, ...)
{
int len;
va_list ap;
if (atomic_swap_32(&mlog_inuse, 1) == 1) {
/* already inuse */
mlog_drop++;
return;
}
membar_acquire();
va_start(ap, fmt);
len = vsnprintf(
mlog_buf[mlog_wpage] + mlog_used,
mlog_buflen - mlog_used,
fmt, ap);
va_end(ap);
mlog_used += len;
if (mlog_buflen - mlog_used <= 1) {
mlog_full++;
}
atomic_store_release(&mlog_inuse, 0);
if (mlog_sih)
softint_schedule(mlog_sih);
}
/* trace functions */
static void
audio_vtrace(struct audio_softc *sc, const char *funcname, const char *header,
const char *fmt, va_list ap)
{
char buf[256];
int n;
n = 0;
buf[0] = '\0';
n += snprintf(buf + n, sizeof(buf) - n, "%s@%d %s",
funcname, device_unit(sc->sc_dev), header);
n += vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
if (cpu_intr_p()) {
audio_mlog_printf("%s\n", buf);
} else {
audio_mlog_flush();
printf("%s\n", buf);
}
}
static void
audio_trace(struct audio_softc *sc, const char *funcname, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
audio_vtrace(sc, funcname, "", fmt, ap);
va_end(ap);
}
static void
audio_tracet(const char *funcname, audio_track_t *track, const char *fmt, ...)
{
char hdr[16];
va_list ap;
snprintf(hdr, sizeof(hdr), "#%d ", track->id);
va_start(ap, fmt);
audio_vtrace(track->mixer->sc, funcname, hdr, fmt, ap);
va_end(ap);
}
static void
audio_tracef(const char *funcname, audio_file_t *file, const char *fmt, ...)
{
char hdr[32];
char phdr[16], rhdr[16];
va_list ap;
phdr[0] = '\0';
rhdr[0] = '\0';
if (file->ptrack)
snprintf(phdr, sizeof(phdr), "#%d", file->ptrack->id);
if (file->rtrack)
snprintf(rhdr, sizeof(rhdr), "#%d", file->rtrack->id);
snprintf(hdr, sizeof(hdr), "{%s,%s} ", phdr, rhdr);
va_start(ap, fmt);
audio_vtrace(file->sc, funcname, hdr, fmt, ap);
va_end(ap);
}
#define DPRINTF(n, fmt...) do { \
if (audiodebug >= (n)) { \
audio_mlog_flush(); \
printf(fmt); \
} \
} while (0)
#define TRACE(n, fmt...) do { \
if (audiodebug >= (n)) audio_trace(sc, __func__, fmt); \
} while (0)
#define TRACET(n, t, fmt...) do { \
if (audiodebug >= (n)) audio_tracet(__func__, t, fmt); \
} while (0)
#define TRACEF(n, f, fmt...) do { \
if (audiodebug >= (n)) audio_tracef(__func__, f, fmt); \
} while (0)
struct audio_track_debugbuf {
char usrbuf[32];
char codec[32];
char chvol[32];
char chmix[32];
char freq[32];
char outbuf[32];
};
static void
audio_track_bufstat(audio_track_t *track, struct audio_track_debugbuf *buf)
{
memset(buf, 0, sizeof(*buf));
snprintf(buf->outbuf, sizeof(buf->outbuf), " out=%d/%d/%d",
track->outbuf.head, track->outbuf.used, track->outbuf.capacity);
if (track->freq.filter)
snprintf(buf->freq, sizeof(buf->freq), " f=%d/%d/%d",
track->freq.srcbuf.head,
track->freq.srcbuf.used,
track->freq.srcbuf.capacity);
if (track->chmix.filter)
snprintf(buf->chmix, sizeof(buf->chmix), " m=%d",
track->chmix.srcbuf.used);
if (track->chvol.filter)
snprintf(buf->chvol, sizeof(buf->chvol), " v=%d",
track->chvol.srcbuf.used);
if (track->codec.filter)
snprintf(buf->codec, sizeof(buf->codec), " e=%d",
track->codec.srcbuf.used);
snprintf(buf->usrbuf, sizeof(buf->usrbuf), " usr=%d/%d/H%d",
track->usrbuf.head, track->usrbuf.used, track->usrbuf_usedhigh);
}
#else
#define DPRINTF(n, fmt...) do { } while (0)
#define TRACE(n, fmt, ...) do { } while (0)
#define TRACET(n, t, fmt, ...) do { } while (0)
#define TRACEF(n, f, fmt, ...) do { } while (0)
#endif
#define SPECIFIED(x) ((x) != ~0)
#define SPECIFIED_CH(x) ((x) != (u_char)~0)
/*
* Default hardware blocksize in msec.
*
* We use 10 msec for most modern platforms. This period is good enough to
* play audio and video synchronizely.
* In contrast, for very old platforms, this is usually too short and too
* severe. Also such platforms usually can not play video confortably, so
* it's not so important to make the blocksize shorter. If the platform
* defines its own value as __AUDIO_BLK_MS in its <machine/param.h>, it
* uses this instead.
*
* In either case, you can overwrite AUDIO_BLK_MS by your kernel
* configuration file if you wish.
*/
#if !defined(AUDIO_BLK_MS)
# if defined(__AUDIO_BLK_MS)
# define AUDIO_BLK_MS __AUDIO_BLK_MS
# else
# define AUDIO_BLK_MS (10)
# endif
#endif
/* Device timeout in msec */
#define AUDIO_TIMEOUT (3000)
/* #define AUDIO_PM_IDLE */
#ifdef AUDIO_PM_IDLE
int audio_idle_timeout = 30;
#endif
/* Number of elements of async mixer's pid */
#define AM_CAPACITY (4)
struct portname {
const char *name;
int mask;
};
static int audiomatch(device_t, cfdata_t, void *);
static void audioattach(device_t, device_t, void *);
static int audiodetach(device_t, int);
static int audioactivate(device_t, enum devact);
static void audiochilddet(device_t, device_t);
static int audiorescan(device_t, const char *, const int *);
static int audio_modcmd(modcmd_t, void *);
#ifdef AUDIO_PM_IDLE
static void audio_idle(void *);
static void audio_activity(device_t, devactive_t);
#endif
static bool audio_suspend(device_t dv, const pmf_qual_t *);
static bool audio_resume(device_t dv, const pmf_qual_t *);
static void audio_volume_down(device_t);
static void audio_volume_up(device_t);
static void audio_volume_toggle(device_t);
static void audio_mixer_capture(struct audio_softc *);
static void audio_mixer_restore(struct audio_softc *);
static void audio_softintr_rd(void *);
static void audio_softintr_wr(void *);
static void audio_printf(struct audio_softc *, const char *, ...)
__printflike(2, 3);
static int audio_exlock_mutex_enter(struct audio_softc *);
static void audio_exlock_mutex_exit(struct audio_softc *);
static int audio_exlock_enter(struct audio_softc *);
static void audio_exlock_exit(struct audio_softc *);
static struct audio_softc *audio_sc_acquire_fromfile(audio_file_t *,
struct psref *);
static void audio_sc_release(struct audio_softc *, struct psref *);
static int audio_track_waitio(struct audio_softc *, audio_track_t *);
static int audioclose(struct file *);
static int audioread(struct file *, off_t *, struct uio *, kauth_cred_t, int);
static int audiowrite(struct file *, off_t *, struct uio *, kauth_cred_t, int);
static int audioioctl(struct file *, u_long, void *);
static int audiopoll(struct file *, int);
static int audiokqfilter(struct file *, struct knote *);
static int audiommap(struct file *, off_t *, size_t, int, int *, int *,
struct uvm_object **, int *);
static int audiostat(struct file *, struct stat *);
static void filt_audiowrite_detach(struct knote *);
static int filt_audiowrite_event(struct knote *, long);
static void filt_audioread_detach(struct knote *);
static int filt_audioread_event(struct knote *, long);
static int audio_open(dev_t, struct audio_softc *, int, int, struct lwp *,
audio_file_t **);
static int audio_close(struct audio_softc *, audio_file_t *);
static void audio_unlink(struct audio_softc *, audio_file_t *);
static int audio_read(struct audio_softc *, struct uio *, int, audio_file_t *);
static int audio_write(struct audio_softc *, struct uio *, int, audio_file_t *);
static void audio_file_clear(struct audio_softc *, audio_file_t *);
static int audio_ioctl(dev_t, struct audio_softc *, u_long, void *, int,
struct lwp *, audio_file_t *);
static int audio_poll(struct audio_softc *, int, struct lwp *, audio_file_t *);
static int audio_kqfilter(struct audio_softc *, audio_file_t *, struct knote *);
static int audio_mmap(struct audio_softc *, off_t *, size_t, int, int *, int *,
struct uvm_object **, int *, audio_file_t *);
static int audioctl_open(dev_t, struct audio_softc *, int, int, struct lwp *);
static void audio_pintr(void *);
static void audio_rintr(void *);
static int audio_query_devinfo(struct audio_softc *, mixer_devinfo_t *);
static __inline int audio_track_readablebytes(const audio_track_t *);
static int audio_file_setinfo(struct audio_softc *, audio_file_t *,
const struct audio_info *);
static int audio_track_setinfo_check(audio_track_t *,
audio_format2_t *, const struct audio_prinfo *);
static void audio_track_setinfo_water(audio_track_t *,
const struct audio_info *);
static int audio_hw_setinfo(struct audio_softc *, const struct audio_info *,
struct audio_info *);
static int audio_hw_set_format(struct audio_softc *, int,
const audio_format2_t *, const audio_format2_t *,
audio_filter_reg_t *, audio_filter_reg_t *);
static int audiogetinfo(struct audio_softc *, struct audio_info *, int,
audio_file_t *);
static bool audio_can_playback(struct audio_softc *);
static bool audio_can_capture(struct audio_softc *);
static int audio_check_params(audio_format2_t *);
static int audio_mixers_init(struct audio_softc *sc, int,
const audio_format2_t *, const audio_format2_t *,
const audio_filter_reg_t *, const audio_filter_reg_t *);
static int audio_select_freq(const struct audio_format *);
static int audio_hw_probe(struct audio_softc *, audio_format2_t *, int);
static int audio_hw_validate_format(struct audio_softc *, int,
const audio_format2_t *);
static int audio_mixers_set_format(struct audio_softc *,
const struct audio_info *);
static void audio_mixers_get_format(struct audio_softc *, struct audio_info *);
static int audio_sysctl_blk_ms(SYSCTLFN_PROTO);
static int audio_sysctl_multiuser(SYSCTLFN_PROTO);
#if defined(AUDIO_DEBUG)
static int audio_sysctl_debug(SYSCTLFN_PROTO);
static void audio_format2_tostr(char *, size_t, const audio_format2_t *);
static void audio_print_format2(const char *, const audio_format2_t *) __unused;
#endif
static void *audio_realloc(void *, size_t);
static int audio_realloc_usrbuf(audio_track_t *, int);
static void audio_free_usrbuf(audio_track_t *);
static audio_track_t *audio_track_create(struct audio_softc *,
audio_trackmixer_t *);
static void audio_track_destroy(audio_track_t *);
static audio_filter_t audio_track_get_codec(audio_track_t *,
const audio_format2_t *, const audio_format2_t *);
static int audio_track_set_format(audio_track_t *, audio_format2_t *);
static void audio_track_play(audio_track_t *);
static int audio_track_drain(struct audio_softc *, audio_track_t *);
static void audio_track_record(audio_track_t *);
static void audio_track_clear(struct audio_softc *, audio_track_t *);
static int audio_mixer_init(struct audio_softc *, int,
const audio_format2_t *, const audio_filter_reg_t *);
static void audio_mixer_destroy(struct audio_softc *, audio_trackmixer_t *);
static void audio_pmixer_start(struct audio_softc *, bool);
static void audio_pmixer_process(struct audio_softc *);
static void audio_pmixer_agc(audio_trackmixer_t *, int);
static int audio_pmixer_mix_track(audio_trackmixer_t *, audio_track_t *, int);
static void audio_pmixer_output(struct audio_softc *);
static int audio_pmixer_halt(struct audio_softc *);
static void audio_rmixer_start(struct audio_softc *);
static void audio_rmixer_process(struct audio_softc *);
static void audio_rmixer_input(struct audio_softc *);
static int audio_rmixer_halt(struct audio_softc *);
static void mixer_init(struct audio_softc *);
static int mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *);
static int mixer_close(struct audio_softc *, audio_file_t *);
static int mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
static void mixer_async_add(struct audio_softc *, pid_t);
static void mixer_async_remove(struct audio_softc *, pid_t);
static void mixer_signal(struct audio_softc *);
static int au_portof(struct audio_softc *, char *, int);
static void au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
mixer_devinfo_t *, const struct portname *);
static int au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, int, int);
static int au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, int *, int *);
static int au_set_gain(struct audio_softc *, struct au_mixer_ports *, int, int);
static void au_get_gain(struct audio_softc *, struct au_mixer_ports *,
u_int *, u_char *);
static int au_set_port(struct audio_softc *, struct au_mixer_ports *, u_int);
static int au_get_port(struct audio_softc *, struct au_mixer_ports *);
static int au_set_monitor_gain(struct audio_softc *, int);
static int au_get_monitor_gain(struct audio_softc *);
static int audio_get_port(struct audio_softc *, mixer_ctrl_t *);
static int audio_set_port(struct audio_softc *, mixer_ctrl_t *);
static __inline struct audio_params
format2_to_params(const audio_format2_t *f2)
{
audio_params_t p;
/* validbits/precision <-> precision/stride */
p.sample_rate = f2->sample_rate;
p.channels = f2->channels;
p.encoding = f2->encoding;
p.validbits = f2->precision;
p.precision = f2->stride;
return p;
}
static __inline audio_format2_t
params_to_format2(const struct audio_params *p)
{
audio_format2_t f2;
/* precision/stride <-> validbits/precision */
f2.sample_rate = p->sample_rate;
f2.channels = p->channels;
f2.encoding = p->encoding;
f2.precision = p->validbits;
f2.stride = p->precision;
return f2;
}
/* Return true if this track is a playback track. */
static __inline bool
audio_track_is_playback(const audio_track_t *track)
{
return ((track->mode & AUMODE_PLAY) != 0);
}
/* Return true if this track is a recording track. */
static __inline bool
audio_track_is_record(const audio_track_t *track)
{
return ((track->mode & AUMODE_RECORD) != 0);
}
#if 0 /* XXX Not used yet */
/*
* Convert 0..255 volume used in userland to internal presentation 0..256.
*/
static __inline u_int
audio_volume_to_inner(u_int v)
{
return v < 127 ? v : v + 1;
}
/*
* Convert 0..256 internal presentation to 0..255 volume used in userland.
*/
static __inline u_int
audio_volume_to_outer(u_int v)
{
return v < 127 ? v : v - 1;
}
#endif /* 0 */
static dev_type_open(audioopen);
/* XXXMRG use more dev_type_xxx */
static int
audiounit(dev_t dev)
{
return AUDIOUNIT(dev);
}
const struct cdevsw audio_cdevsw = {
.d_open = audioopen,
.d_close = noclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_cfdriver = &audio_cd,
.d_devtounit = audiounit,
.d_flag = D_OTHER | D_MPSAFE
};
const struct fileops audio_fileops = {
.fo_name = "audio",
.fo_read = audioread,
.fo_write = audiowrite,
.fo_ioctl = audioioctl,
.fo_fcntl = fnullop_fcntl,
.fo_stat = audiostat,
.fo_poll = audiopoll,
.fo_close = audioclose,
.fo_mmap = audiommap,
.fo_kqfilter = audiokqfilter,
.fo_restart = fnullop_restart
};
/* The default audio mode: 8 kHz mono mu-law */
static const struct audio_params audio_default = {
.sample_rate = 8000,
.encoding = AUDIO_ENCODING_ULAW,
.precision = 8,
.validbits = 8,
.channels = 1,
};
static const char *encoding_names[] = {
"none",
AudioEmulaw,
AudioEalaw,
"pcm16",
"pcm8",
AudioEadpcm,
AudioEslinear_le,
AudioEslinear_be,
AudioEulinear_le,
AudioEulinear_be,
AudioEslinear,
AudioEulinear,
AudioEmpeg_l1_stream,
AudioEmpeg_l1_packets,
AudioEmpeg_l1_system,
AudioEmpeg_l2_stream,
AudioEmpeg_l2_packets,
AudioEmpeg_l2_system,
AudioEac3,
};
/*
* Returns encoding name corresponding to AUDIO_ENCODING_*.
* Note that it may return a local buffer because it is mainly for debugging.
*/
const char *
audio_encoding_name(int encoding)
{
static char buf[16];
if (0 <= encoding && encoding < __arraycount(encoding_names)) {
return encoding_names[encoding];
} else {
snprintf(buf, sizeof(buf), "enc=%d", encoding);
return buf;
}
}
/*
* Supported encodings used by AUDIO_GETENC.
* index and flags are set by code.
* XXX is there any needs for SLINEAR_OE:>=16/ULINEAR_OE:>=16 ?
*/
static const audio_encoding_t audio_encodings[] = {
{ 0, AudioEmulaw, AUDIO_ENCODING_ULAW, 8, 0 },
{ 0, AudioEalaw, AUDIO_ENCODING_ALAW, 8, 0 },
{ 0, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8, 0 },
{ 0, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0 },
{ 0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0 },
{ 0, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16, 0 },
{ 0, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16, 0 },
{ 0, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16, 0 },
#if defined(AUDIO_SUPPORT_LINEAR24)
{ 0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 24, 0 },
{ 0, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 24, 0 },
{ 0, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 24, 0 },
{ 0, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 24, 0 },
#endif
{ 0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 32, 0 },
{ 0, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 32, 0 },
{ 0, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 32, 0 },
{ 0, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 32, 0 },
};
static const struct portname itable[] = {
{ AudioNmicrophone, AUDIO_MICROPHONE },
{ AudioNline, AUDIO_LINE_IN },
{ AudioNcd, AUDIO_CD },
{ 0, 0 }
};
static const struct portname otable[] = {
{ AudioNspeaker, AUDIO_SPEAKER },
{ AudioNheadphone, AUDIO_HEADPHONE },
{ AudioNline, AUDIO_LINE_OUT },
{ 0, 0 }
};
static struct psref_class *audio_psref_class __read_mostly;
CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc),
audiomatch, audioattach, audiodetach, audioactivate, audiorescan,
audiochilddet, DVF_DETACH_SHUTDOWN);
static int
audiomatch(device_t parent, cfdata_t match, void *aux)
{
struct audio_attach_args *sa;
sa = aux;
DPRINTF(1, "%s: type=%d sa=%p hw=%p\n",
__func__, sa->type, sa, sa->hwif);
return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
}
static void
audioattach(device_t parent, device_t self, void *aux)
{
struct audio_softc *sc;
struct audio_attach_args *sa;
const struct audio_hw_if *hw_if;
audio_format2_t phwfmt;
audio_format2_t rhwfmt;
audio_filter_reg_t pfil;
audio_filter_reg_t rfil;
const struct sysctlnode *node;
void *hdlp;
bool has_playback;
bool has_capture;
bool has_indep;
bool has_fulldup;
int mode;
int error;
sc = device_private(self);
sc->sc_dev = self;
sa = (struct audio_attach_args *)aux;
hw_if = sa->hwif;
hdlp = sa->hdl;
if (hw_if == NULL) {
panic("audioattach: missing hw_if method");
}
if (hw_if->get_locks == NULL || hw_if->get_props == NULL) {
aprint_error(": missing mandatory method\n");
return;
}
hw_if->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock);
sc->sc_props = hw_if->get_props(hdlp);
has_playback = (sc->sc_props & AUDIO_PROP_PLAYBACK);
has_capture = (sc->sc_props & AUDIO_PROP_CAPTURE);
has_indep = (sc->sc_props & AUDIO_PROP_INDEPENDENT);
has_fulldup = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
#ifdef DIAGNOSTIC
if (hw_if->query_format == NULL ||
hw_if->set_format == NULL ||
hw_if->getdev == NULL ||
hw_if->set_port == NULL ||
hw_if->get_port == NULL ||
hw_if->query_devinfo == NULL) {
aprint_error(": missing mandatory method\n");
return;
}
if (has_playback) {
if ((hw_if->start_output == NULL &&
hw_if->trigger_output == NULL) ||
hw_if->halt_output == NULL) {
aprint_error(": missing playback method\n");
}
}
if (has_capture) {
if ((hw_if->start_input == NULL &&
hw_if->trigger_input == NULL) ||
hw_if->halt_input == NULL) {
aprint_error(": missing capture method\n");
}
}
#endif
sc->hw_if = hw_if;
sc->hw_hdl = hdlp;
sc->hw_dev = parent;
sc->sc_exlock = 1;
sc->sc_blk_ms = AUDIO_BLK_MS;
SLIST_INIT(&sc->sc_files);
cv_init(&sc->sc_exlockcv, "audiolk");
sc->sc_am_capacity = 0;
sc->sc_am_used = 0;
sc->sc_am = NULL;
/* MMAP is now supported by upper layer. */
sc->sc_props |= AUDIO_PROP_MMAP;
KASSERT(has_playback || has_capture);
/* Unidirectional device must have neither FULLDUP nor INDEPENDENT. */
if (!has_playback || !has_capture) {
KASSERT(!has_indep);
KASSERT(!has_fulldup);
}
mode = 0;
if (has_playback) {