audio.c revision 1.120 1 /* $NetBSD: audio.c,v 1.120 2022/03/26 06:49:27 isaki Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1991-1993 Regents of the University of California.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the Computer Systems
47 * Engineering Group at Lawrence Berkeley Laboratory.
48 * 4. Neither the name of the University nor of the Laboratory may be used
49 * to endorse or promote products derived from this software without
50 * specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 */
64
65 /*
66 * Terminology: "sample", "channel", "frame", "block", "track":
67 *
68 * channel frame
69 * | ........
70 * v : : \
71 * +------:------:------:- -+------+ : +------+-.. |
72 * #0(L) |sample|sample|sample| .. |sample| : |sample| |
73 * +------:------:------:- -+------+ : +------+-.. |
74 * #1(R) |sample|sample|sample| .. |sample| : |sample| |
75 * +------:------:------:- -+------+ : +------+-.. | track
76 * : : : : |
77 * +------:------:------:- -+------+ : +------+-.. |
78 * |sample|sample|sample| .. |sample| : |sample| |
79 * +------:------:------:- -+------+ : +------+-.. |
80 * : : /
81 * ........
82 *
83 * \--------------------------------/ \--------..
84 * block
85 *
86 * - A "frame" is the minimum unit in the time axis direction, and consists
87 * of samples for the number of channels.
88 * - A "block" is basic length of processing. The audio layer basically
89 * handles audio data stream block by block, asks underlying hardware to
90 * process them block by block, and then the hardware raises interrupt by
91 * each block.
92 * - A "track" is single completed audio stream.
93 *
94 * For example, the hardware block is assumed to be 10 msec, and your audio
95 * track consists of 2.1(=3) channels 44.1kHz 16bit PCM,
96 *
97 * "channel" = 3
98 * "sample" = 2 [bytes]
99 * "frame" = 2 [bytes/sample] * 3 [channels] = 6 [bytes]
100 * "block" = 44100 [Hz] * (10/1000) [seconds] * 6 [bytes/frame] = 2646 [bytes]
101 *
102 * The terminologies shown here are only for this MI audio layer. Note that
103 * different terminologies may be used in each manufacturer's datasheet, and
104 * each MD driver may follow it. For example, what we call a "block" is
105 * called a "frame" in sys/dev/pci/yds.c.
106 */
107
108 /*
109 * Locking: there are three locks per device.
110 *
111 * - sc_lock, provided by the underlying driver. This is an adaptive lock,
112 * returned in the second parameter to hw_if->get_locks(). It is known
113 * as the "thread lock".
114 *
115 * It serializes access to state in all places except the
116 * driver's interrupt service routine. This lock is taken from process
117 * context (example: access to /dev/audio). It is also taken from soft
118 * interrupt handlers in this module, primarily to serialize delivery of
119 * wakeups. This lock may be used/provided by modules external to the
120 * audio subsystem, so take care not to introduce a lock order problem.
121 * LONG TERM SLEEPS MUST NOT OCCUR WITH THIS LOCK HELD.
122 *
123 * - sc_intr_lock, provided by the underlying driver. This may be either a
124 * spinlock (at IPL_SCHED or IPL_VM) or an adaptive lock (IPL_NONE or
125 * IPL_SOFT*), returned in the first parameter to hw_if->get_locks(). It
126 * is known as the "interrupt lock".
127 *
128 * It provides atomic access to the device's hardware state, and to audio
129 * channel data that may be accessed by the hardware driver's ISR.
130 * In all places outside the ISR, sc_lock must be held before taking
131 * sc_intr_lock. This is to ensure that groups of hardware operations are
132 * made atomically. SLEEPS CANNOT OCCUR WITH THIS LOCK HELD.
133 *
134 * - sc_exlock, private to this module. This is a variable protected by
135 * sc_lock. It is known as the "critical section".
136 * Some operations release sc_lock in order to allocate memory, to wait
137 * for in-flight I/O to complete, to copy to/from user context, etc.
138 * sc_exlock provides a critical section even under the circumstance.
139 * "+" in following list indicates the interfaces which necessary to be
140 * protected by sc_exlock.
141 *
142 * List of hardware interface methods, and which locks are held when each
143 * is called by this module:
144 *
145 * METHOD INTR THREAD NOTES
146 * ----------------------- ------- ------- -------------------------
147 * open x x +
148 * close x x +
149 * query_format - x
150 * set_format - x
151 * round_blocksize - x
152 * commit_settings - x
153 * init_output x x
154 * init_input x x
155 * start_output x x +
156 * start_input x x +
157 * halt_output x x +
158 * halt_input x x +
159 * speaker_ctl x x
160 * getdev - -
161 * set_port - x +
162 * get_port - x +
163 * query_devinfo - x
164 * allocm - - +
165 * freem - - +
166 * round_buffersize - x
167 * get_props - - Called at attach time
168 * trigger_output x x +
169 * trigger_input x x +
170 * dev_ioctl - x
171 * get_locks - - Called at attach time
172 *
173 * In addition, there is an additional lock.
174 *
175 * - track->lock. This is an atomic variable and is similar to the
176 * "interrupt lock". This is one for each track. If any thread context
177 * (and software interrupt context) and hardware interrupt context who
178 * want to access some variables on this track, they must acquire this
179 * lock before. It protects track's consistency between hardware
180 * interrupt context and others.
181 */
182
183 #include <sys/cdefs.h>
184 __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.120 2022/03/26 06:49:27 isaki Exp $");
185
186 #ifdef _KERNEL_OPT
187 #include "audio.h"
188 #include "midi.h"
189 #endif
190
191 #if NAUDIO > 0
192
193 #include <sys/types.h>
194 #include <sys/param.h>
195 #include <sys/atomic.h>
196 #include <sys/audioio.h>
197 #include <sys/conf.h>
198 #include <sys/cpu.h>
199 #include <sys/device.h>
200 #include <sys/fcntl.h>
201 #include <sys/file.h>
202 #include <sys/filedesc.h>
203 #include <sys/intr.h>
204 #include <sys/ioctl.h>
205 #include <sys/kauth.h>
206 #include <sys/kernel.h>
207 #include <sys/kmem.h>
208 #include <sys/lock.h>
209 #include <sys/malloc.h>
210 #include <sys/mman.h>
211 #include <sys/module.h>
212 #include <sys/poll.h>
213 #include <sys/proc.h>
214 #include <sys/queue.h>
215 #include <sys/select.h>
216 #include <sys/signalvar.h>
217 #include <sys/stat.h>
218 #include <sys/sysctl.h>
219 #include <sys/systm.h>
220 #include <sys/syslog.h>
221 #include <sys/vnode.h>
222
223 #include <dev/audio/audio_if.h>
224 #include <dev/audio/audiovar.h>
225 #include <dev/audio/audiodef.h>
226 #include <dev/audio/linear.h>
227 #include <dev/audio/mulaw.h>
228
229 #include <machine/endian.h>
230
231 #include <uvm/uvm_extern.h>
232
233 #include "ioconf.h"
234
235 /*
236 * 0: No debug logs
237 * 1: action changes like open/close/set_format...
238 * 2: + normal operations like read/write/ioctl...
239 * 3: + TRACEs except interrupt
240 * 4: + TRACEs including interrupt
241 */
242 //#define AUDIO_DEBUG 1
243
244 #if defined(AUDIO_DEBUG)
245
246 int audiodebug = AUDIO_DEBUG;
247 static void audio_vtrace(struct audio_softc *sc, const char *, const char *,
248 const char *, va_list);
249 static void audio_trace(struct audio_softc *sc, const char *, const char *, ...)
250 __printflike(3, 4);
251 static void audio_tracet(const char *, audio_track_t *, const char *, ...)
252 __printflike(3, 4);
253 static void audio_tracef(const char *, audio_file_t *, const char *, ...)
254 __printflike(3, 4);
255
256 /* XXX sloppy memory logger */
257 static void audio_mlog_init(void);
258 static void audio_mlog_free(void);
259 static void audio_mlog_softintr(void *);
260 extern void audio_mlog_flush(void);
261 extern void audio_mlog_printf(const char *, ...);
262
263 static int mlog_refs; /* reference counter */
264 static char *mlog_buf[2]; /* double buffer */
265 static int mlog_buflen; /* buffer length */
266 static int mlog_used; /* used length */
267 static int mlog_full; /* number of dropped lines by buffer full */
268 static int mlog_drop; /* number of dropped lines by busy */
269 static volatile uint32_t mlog_inuse; /* in-use */
270 static int mlog_wpage; /* active page */
271 static void *mlog_sih; /* softint handle */
272
273 static void
274 audio_mlog_init(void)
275 {
276 mlog_refs++;
277 if (mlog_refs > 1)
278 return;
279 mlog_buflen = 4096;
280 mlog_buf[0] = kmem_zalloc(mlog_buflen, KM_SLEEP);
281 mlog_buf[1] = kmem_zalloc(mlog_buflen, KM_SLEEP);
282 mlog_used = 0;
283 mlog_full = 0;
284 mlog_drop = 0;
285 mlog_inuse = 0;
286 mlog_wpage = 0;
287 mlog_sih = softint_establish(SOFTINT_SERIAL, audio_mlog_softintr, NULL);
288 if (mlog_sih == NULL)
289 printf("%s: softint_establish failed\n", __func__);
290 }
291
292 static void
293 audio_mlog_free(void)
294 {
295 mlog_refs--;
296 if (mlog_refs > 0)
297 return;
298
299 audio_mlog_flush();
300 if (mlog_sih)
301 softint_disestablish(mlog_sih);
302 kmem_free(mlog_buf[0], mlog_buflen);
303 kmem_free(mlog_buf[1], mlog_buflen);
304 }
305
306 /*
307 * Flush memory buffer.
308 * It must not be called from hardware interrupt context.
309 */
310 void
311 audio_mlog_flush(void)
312 {
313 if (mlog_refs == 0)
314 return;
315
316 /* Nothing to do if already in use ? */
317 if (atomic_swap_32(&mlog_inuse, 1) == 1)
318 return;
319 membar_enter();
320
321 int rpage = mlog_wpage;
322 mlog_wpage ^= 1;
323 mlog_buf[mlog_wpage][0] = '\0';
324 mlog_used = 0;
325
326 atomic_store_release(&mlog_inuse, 0);
327
328 if (mlog_buf[rpage][0] != '\0') {
329 printf("%s", mlog_buf[rpage]);
330 if (mlog_drop > 0)
331 printf("mlog_drop %d\n", mlog_drop);
332 if (mlog_full > 0)
333 printf("mlog_full %d\n", mlog_full);
334 }
335 mlog_full = 0;
336 mlog_drop = 0;
337 }
338
339 static void
340 audio_mlog_softintr(void *cookie)
341 {
342 audio_mlog_flush();
343 }
344
345 void
346 audio_mlog_printf(const char *fmt, ...)
347 {
348 int len;
349 va_list ap;
350
351 if (atomic_swap_32(&mlog_inuse, 1) == 1) {
352 /* already inuse */
353 mlog_drop++;
354 return;
355 }
356 membar_enter();
357
358 va_start(ap, fmt);
359 len = vsnprintf(
360 mlog_buf[mlog_wpage] + mlog_used,
361 mlog_buflen - mlog_used,
362 fmt, ap);
363 va_end(ap);
364
365 mlog_used += len;
366 if (mlog_buflen - mlog_used <= 1) {
367 mlog_full++;
368 }
369
370 atomic_store_release(&mlog_inuse, 0);
371
372 if (mlog_sih)
373 softint_schedule(mlog_sih);
374 }
375
376 /* trace functions */
377 static void
378 audio_vtrace(struct audio_softc *sc, const char *funcname, const char *header,
379 const char *fmt, va_list ap)
380 {
381 char buf[256];
382 int n;
383
384 n = 0;
385 buf[0] = '\0';
386 n += snprintf(buf + n, sizeof(buf) - n, "%s@%d %s",
387 funcname, device_unit(sc->sc_dev), header);
388 n += vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
389
390 if (cpu_intr_p()) {
391 audio_mlog_printf("%s\n", buf);
392 } else {
393 audio_mlog_flush();
394 printf("%s\n", buf);
395 }
396 }
397
398 static void
399 audio_trace(struct audio_softc *sc, const char *funcname, const char *fmt, ...)
400 {
401 va_list ap;
402
403 va_start(ap, fmt);
404 audio_vtrace(sc, funcname, "", fmt, ap);
405 va_end(ap);
406 }
407
408 static void
409 audio_tracet(const char *funcname, audio_track_t *track, const char *fmt, ...)
410 {
411 char hdr[16];
412 va_list ap;
413
414 snprintf(hdr, sizeof(hdr), "#%d ", track->id);
415 va_start(ap, fmt);
416 audio_vtrace(track->mixer->sc, funcname, hdr, fmt, ap);
417 va_end(ap);
418 }
419
420 static void
421 audio_tracef(const char *funcname, audio_file_t *file, const char *fmt, ...)
422 {
423 char hdr[32];
424 char phdr[16], rhdr[16];
425 va_list ap;
426
427 phdr[0] = '\0';
428 rhdr[0] = '\0';
429 if (file->ptrack)
430 snprintf(phdr, sizeof(phdr), "#%d", file->ptrack->id);
431 if (file->rtrack)
432 snprintf(rhdr, sizeof(rhdr), "#%d", file->rtrack->id);
433 snprintf(hdr, sizeof(hdr), "{%s,%s} ", phdr, rhdr);
434
435 va_start(ap, fmt);
436 audio_vtrace(file->sc, funcname, hdr, fmt, ap);
437 va_end(ap);
438 }
439
440 #define DPRINTF(n, fmt...) do { \
441 if (audiodebug >= (n)) { \
442 audio_mlog_flush(); \
443 printf(fmt); \
444 } \
445 } while (0)
446 #define TRACE(n, fmt...) do { \
447 if (audiodebug >= (n)) audio_trace(sc, __func__, fmt); \
448 } while (0)
449 #define TRACET(n, t, fmt...) do { \
450 if (audiodebug >= (n)) audio_tracet(__func__, t, fmt); \
451 } while (0)
452 #define TRACEF(n, f, fmt...) do { \
453 if (audiodebug >= (n)) audio_tracef(__func__, f, fmt); \
454 } while (0)
455
456 struct audio_track_debugbuf {
457 char usrbuf[32];
458 char codec[32];
459 char chvol[32];
460 char chmix[32];
461 char freq[32];
462 char outbuf[32];
463 };
464
465 static void
466 audio_track_bufstat(audio_track_t *track, struct audio_track_debugbuf *buf)
467 {
468
469 memset(buf, 0, sizeof(*buf));
470
471 snprintf(buf->outbuf, sizeof(buf->outbuf), " out=%d/%d/%d",
472 track->outbuf.head, track->outbuf.used, track->outbuf.capacity);
473 if (track->freq.filter)
474 snprintf(buf->freq, sizeof(buf->freq), " f=%d/%d/%d",
475 track->freq.srcbuf.head,
476 track->freq.srcbuf.used,
477 track->freq.srcbuf.capacity);
478 if (track->chmix.filter)
479 snprintf(buf->chmix, sizeof(buf->chmix), " m=%d",
480 track->chmix.srcbuf.used);
481 if (track->chvol.filter)
482 snprintf(buf->chvol, sizeof(buf->chvol), " v=%d",
483 track->chvol.srcbuf.used);
484 if (track->codec.filter)
485 snprintf(buf->codec, sizeof(buf->codec), " e=%d",
486 track->codec.srcbuf.used);
487 snprintf(buf->usrbuf, sizeof(buf->usrbuf), " usr=%d/%d/H%d",
488 track->usrbuf.head, track->usrbuf.used, track->usrbuf_usedhigh);
489 }
490 #else
491 #define DPRINTF(n, fmt...) do { } while (0)
492 #define TRACE(n, fmt, ...) do { } while (0)
493 #define TRACET(n, t, fmt, ...) do { } while (0)
494 #define TRACEF(n, f, fmt, ...) do { } while (0)
495 #endif
496
497 #define SPECIFIED(x) ((x) != ~0)
498 #define SPECIFIED_CH(x) ((x) != (u_char)~0)
499
500 /*
501 * Default hardware blocksize in msec.
502 *
503 * We use 10 msec for most modern platforms. This period is good enough to
504 * play audio and video synchronizely.
505 * In contrast, for very old platforms, this is usually too short and too
506 * severe. Also such platforms usually can not play video confortably, so
507 * it's not so important to make the blocksize shorter. If the platform
508 * defines its own value as __AUDIO_BLK_MS in its <machine/param.h>, it
509 * uses this instead.
510 *
511 * In either case, you can overwrite AUDIO_BLK_MS by your kernel
512 * configuration file if you wish.
513 */
514 #if !defined(AUDIO_BLK_MS)
515 # if defined(__AUDIO_BLK_MS)
516 # define AUDIO_BLK_MS __AUDIO_BLK_MS
517 # else
518 # define AUDIO_BLK_MS (10)
519 # endif
520 #endif
521
522 /* Device timeout in msec */
523 #define AUDIO_TIMEOUT (3000)
524
525 /* #define AUDIO_PM_IDLE */
526 #ifdef AUDIO_PM_IDLE
527 int audio_idle_timeout = 30;
528 #endif
529
530 /* Number of elements of async mixer's pid */
531 #define AM_CAPACITY (4)
532
533 struct portname {
534 const char *name;
535 int mask;
536 };
537
538 static int audiomatch(device_t, cfdata_t, void *);
539 static void audioattach(device_t, device_t, void *);
540 static int audiodetach(device_t, int);
541 static int audioactivate(device_t, enum devact);
542 static void audiochilddet(device_t, device_t);
543 static int audiorescan(device_t, const char *, const int *);
544
545 static int audio_modcmd(modcmd_t, void *);
546
547 #ifdef AUDIO_PM_IDLE
548 static void audio_idle(void *);
549 static void audio_activity(device_t, devactive_t);
550 #endif
551
552 static bool audio_suspend(device_t dv, const pmf_qual_t *);
553 static bool audio_resume(device_t dv, const pmf_qual_t *);
554 static void audio_volume_down(device_t);
555 static void audio_volume_up(device_t);
556 static void audio_volume_toggle(device_t);
557
558 static void audio_mixer_capture(struct audio_softc *);
559 static void audio_mixer_restore(struct audio_softc *);
560
561 static void audio_softintr_rd(void *);
562 static void audio_softintr_wr(void *);
563
564 static void audio_printf(struct audio_softc *, const char *, ...)
565 __printflike(2, 3);
566 static int audio_exlock_mutex_enter(struct audio_softc *);
567 static void audio_exlock_mutex_exit(struct audio_softc *);
568 static int audio_exlock_enter(struct audio_softc *);
569 static void audio_exlock_exit(struct audio_softc *);
570 static void audio_sc_acquire_foropen(struct audio_softc *, struct psref *);
571 static struct audio_softc *audio_sc_acquire_fromfile(audio_file_t *,
572 struct psref *);
573 static void audio_sc_release(struct audio_softc *, struct psref *);
574 static int audio_track_waitio(struct audio_softc *, audio_track_t *);
575
576 static int audioclose(struct file *);
577 static int audioread(struct file *, off_t *, struct uio *, kauth_cred_t, int);
578 static int audiowrite(struct file *, off_t *, struct uio *, kauth_cred_t, int);
579 static int audioioctl(struct file *, u_long, void *);
580 static int audiopoll(struct file *, int);
581 static int audiokqfilter(struct file *, struct knote *);
582 static int audiommap(struct file *, off_t *, size_t, int, int *, int *,
583 struct uvm_object **, int *);
584 static int audiostat(struct file *, struct stat *);
585
586 static void filt_audiowrite_detach(struct knote *);
587 static int filt_audiowrite_event(struct knote *, long);
588 static void filt_audioread_detach(struct knote *);
589 static int filt_audioread_event(struct knote *, long);
590
591 static int audio_open(dev_t, struct audio_softc *, int, int, struct lwp *,
592 audio_file_t **);
593 static int audio_close(struct audio_softc *, audio_file_t *);
594 static void audio_unlink(struct audio_softc *, audio_file_t *);
595 static int audio_read(struct audio_softc *, struct uio *, int, audio_file_t *);
596 static int audio_write(struct audio_softc *, struct uio *, int, audio_file_t *);
597 static void audio_file_clear(struct audio_softc *, audio_file_t *);
598 static int audio_ioctl(dev_t, struct audio_softc *, u_long, void *, int,
599 struct lwp *, audio_file_t *);
600 static int audio_poll(struct audio_softc *, int, struct lwp *, audio_file_t *);
601 static int audio_kqfilter(struct audio_softc *, audio_file_t *, struct knote *);
602 static int audio_mmap(struct audio_softc *, off_t *, size_t, int, int *, int *,
603 struct uvm_object **, int *, audio_file_t *);
604
605 static int audioctl_open(dev_t, struct audio_softc *, int, int, struct lwp *);
606
607 static void audio_pintr(void *);
608 static void audio_rintr(void *);
609
610 static int audio_query_devinfo(struct audio_softc *, mixer_devinfo_t *);
611
612 static __inline int audio_track_readablebytes(const audio_track_t *);
613 static int audio_file_setinfo(struct audio_softc *, audio_file_t *,
614 const struct audio_info *);
615 static int audio_track_setinfo_check(audio_track_t *,
616 audio_format2_t *, const struct audio_prinfo *);
617 static void audio_track_setinfo_water(audio_track_t *,
618 const struct audio_info *);
619 static int audio_hw_setinfo(struct audio_softc *, const struct audio_info *,
620 struct audio_info *);
621 static int audio_hw_set_format(struct audio_softc *, int,
622 const audio_format2_t *, const audio_format2_t *,
623 audio_filter_reg_t *, audio_filter_reg_t *);
624 static int audiogetinfo(struct audio_softc *, struct audio_info *, int,
625 audio_file_t *);
626 static bool audio_can_playback(struct audio_softc *);
627 static bool audio_can_capture(struct audio_softc *);
628 static int audio_check_params(audio_format2_t *);
629 static int audio_mixers_init(struct audio_softc *sc, int,
630 const audio_format2_t *, const audio_format2_t *,
631 const audio_filter_reg_t *, const audio_filter_reg_t *);
632 static int audio_select_freq(const struct audio_format *);
633 static int audio_hw_probe(struct audio_softc *, audio_format2_t *, int);
634 static int audio_hw_validate_format(struct audio_softc *, int,
635 const audio_format2_t *);
636 static int audio_mixers_set_format(struct audio_softc *,
637 const struct audio_info *);
638 static void audio_mixers_get_format(struct audio_softc *, struct audio_info *);
639 static int audio_sysctl_blk_ms(SYSCTLFN_PROTO);
640 static int audio_sysctl_multiuser(SYSCTLFN_PROTO);
641 #if defined(AUDIO_DEBUG)
642 static int audio_sysctl_debug(SYSCTLFN_PROTO);
643 static void audio_format2_tostr(char *, size_t, const audio_format2_t *);
644 static void audio_print_format2(const char *, const audio_format2_t *) __unused;
645 #endif
646
647 static void *audio_realloc(void *, size_t);
648 static int audio_realloc_usrbuf(audio_track_t *, int);
649 static void audio_free_usrbuf(audio_track_t *);
650
651 static audio_track_t *audio_track_create(struct audio_softc *,
652 audio_trackmixer_t *);
653 static void audio_track_destroy(audio_track_t *);
654 static audio_filter_t audio_track_get_codec(audio_track_t *,
655 const audio_format2_t *, const audio_format2_t *);
656 static int audio_track_set_format(audio_track_t *, audio_format2_t *);
657 static void audio_track_play(audio_track_t *);
658 static int audio_track_drain(struct audio_softc *, audio_track_t *);
659 static void audio_track_record(audio_track_t *);
660 static void audio_track_clear(struct audio_softc *, audio_track_t *);
661
662 static int audio_mixer_init(struct audio_softc *, int,
663 const audio_format2_t *, const audio_filter_reg_t *);
664 static void audio_mixer_destroy(struct audio_softc *, audio_trackmixer_t *);
665 static void audio_pmixer_start(struct audio_softc *, bool);
666 static void audio_pmixer_process(struct audio_softc *);
667 static void audio_pmixer_agc(audio_trackmixer_t *, int);
668 static int audio_pmixer_mix_track(audio_trackmixer_t *, audio_track_t *, int);
669 static void audio_pmixer_output(struct audio_softc *);
670 static int audio_pmixer_halt(struct audio_softc *);
671 static void audio_rmixer_start(struct audio_softc *);
672 static void audio_rmixer_process(struct audio_softc *);
673 static void audio_rmixer_input(struct audio_softc *);
674 static int audio_rmixer_halt(struct audio_softc *);
675
676 static void mixer_init(struct audio_softc *);
677 static int mixer_open(dev_t, struct audio_softc *, int, int, struct lwp *);
678 static int mixer_close(struct audio_softc *, audio_file_t *);
679 static int mixer_ioctl(struct audio_softc *, u_long, void *, int, struct lwp *);
680 static void mixer_async_add(struct audio_softc *, pid_t);
681 static void mixer_async_remove(struct audio_softc *, pid_t);
682 static void mixer_signal(struct audio_softc *);
683
684 static int au_portof(struct audio_softc *, char *, int);
685
686 static void au_setup_ports(struct audio_softc *, struct au_mixer_ports *,
687 mixer_devinfo_t *, const struct portname *);
688 static int au_set_lr_value(struct audio_softc *, mixer_ctrl_t *, int, int);
689 static int au_get_lr_value(struct audio_softc *, mixer_ctrl_t *, int *, int *);
690 static int au_set_gain(struct audio_softc *, struct au_mixer_ports *, int, int);
691 static void au_get_gain(struct audio_softc *, struct au_mixer_ports *,
692 u_int *, u_char *);
693 static int au_set_port(struct audio_softc *, struct au_mixer_ports *, u_int);
694 static int au_get_port(struct audio_softc *, struct au_mixer_ports *);
695 static int au_set_monitor_gain(struct audio_softc *, int);
696 static int au_get_monitor_gain(struct audio_softc *);
697 static int audio_get_port(struct audio_softc *, mixer_ctrl_t *);
698 static int audio_set_port(struct audio_softc *, mixer_ctrl_t *);
699
700 static __inline struct audio_params
701 format2_to_params(const audio_format2_t *f2)
702 {
703 audio_params_t p;
704
705 /* validbits/precision <-> precision/stride */
706 p.sample_rate = f2->sample_rate;
707 p.channels = f2->channels;
708 p.encoding = f2->encoding;
709 p.validbits = f2->precision;
710 p.precision = f2->stride;
711 return p;
712 }
713
714 static __inline audio_format2_t
715 params_to_format2(const struct audio_params *p)
716 {
717 audio_format2_t f2;
718
719 /* precision/stride <-> validbits/precision */
720 f2.sample_rate = p->sample_rate;
721 f2.channels = p->channels;
722 f2.encoding = p->encoding;
723 f2.precision = p->validbits;
724 f2.stride = p->precision;
725 return f2;
726 }
727
728 /* Return true if this track is a playback track. */
729 static __inline bool
730 audio_track_is_playback(const audio_track_t *track)
731 {
732
733 return ((track->mode & AUMODE_PLAY) != 0);
734 }
735
736 /* Return true if this track is a recording track. */
737 static __inline bool
738 audio_track_is_record(const audio_track_t *track)
739 {
740
741 return ((track->mode & AUMODE_RECORD) != 0);
742 }
743
744 #if 0 /* XXX Not used yet */
745 /*
746 * Convert 0..255 volume used in userland to internal presentation 0..256.
747 */
748 static __inline u_int
749 audio_volume_to_inner(u_int v)
750 {
751
752 return v < 127 ? v : v + 1;
753 }
754
755 /*
756 * Convert 0..256 internal presentation to 0..255 volume used in userland.
757 */
758 static __inline u_int
759 audio_volume_to_outer(u_int v)
760 {
761
762 return v < 127 ? v : v - 1;
763 }
764 #endif /* 0 */
765
766 static dev_type_open(audioopen);
767 /* XXXMRG use more dev_type_xxx */
768
769 const struct cdevsw audio_cdevsw = {
770 .d_open = audioopen,
771 .d_close = noclose,
772 .d_read = noread,
773 .d_write = nowrite,
774 .d_ioctl = noioctl,
775 .d_stop = nostop,
776 .d_tty = notty,
777 .d_poll = nopoll,
778 .d_mmap = nommap,
779 .d_kqfilter = nokqfilter,
780 .d_discard = nodiscard,
781 .d_flag = D_OTHER | D_MPSAFE
782 };
783
784 const struct fileops audio_fileops = {
785 .fo_name = "audio",
786 .fo_read = audioread,
787 .fo_write = audiowrite,
788 .fo_ioctl = audioioctl,
789 .fo_fcntl = fnullop_fcntl,
790 .fo_stat = audiostat,
791 .fo_poll = audiopoll,
792 .fo_close = audioclose,
793 .fo_mmap = audiommap,
794 .fo_kqfilter = audiokqfilter,
795 .fo_restart = fnullop_restart
796 };
797
798 /* The default audio mode: 8 kHz mono mu-law */
799 static const struct audio_params audio_default = {
800 .sample_rate = 8000,
801 .encoding = AUDIO_ENCODING_ULAW,
802 .precision = 8,
803 .validbits = 8,
804 .channels = 1,
805 };
806
807 static const char *encoding_names[] = {
808 "none",
809 AudioEmulaw,
810 AudioEalaw,
811 "pcm16",
812 "pcm8",
813 AudioEadpcm,
814 AudioEslinear_le,
815 AudioEslinear_be,
816 AudioEulinear_le,
817 AudioEulinear_be,
818 AudioEslinear,
819 AudioEulinear,
820 AudioEmpeg_l1_stream,
821 AudioEmpeg_l1_packets,
822 AudioEmpeg_l1_system,
823 AudioEmpeg_l2_stream,
824 AudioEmpeg_l2_packets,
825 AudioEmpeg_l2_system,
826 AudioEac3,
827 };
828
829 /*
830 * Returns encoding name corresponding to AUDIO_ENCODING_*.
831 * Note that it may return a local buffer because it is mainly for debugging.
832 */
833 const char *
834 audio_encoding_name(int encoding)
835 {
836 static char buf[16];
837
838 if (0 <= encoding && encoding < __arraycount(encoding_names)) {
839 return encoding_names[encoding];
840 } else {
841 snprintf(buf, sizeof(buf), "enc=%d", encoding);
842 return buf;
843 }
844 }
845
846 /*
847 * Supported encodings used by AUDIO_GETENC.
848 * index and flags are set by code.
849 * XXX is there any needs for SLINEAR_OE:>=16/ULINEAR_OE:>=16 ?
850 */
851 static const audio_encoding_t audio_encodings[] = {
852 { 0, AudioEmulaw, AUDIO_ENCODING_ULAW, 8, 0 },
853 { 0, AudioEalaw, AUDIO_ENCODING_ALAW, 8, 0 },
854 { 0, AudioEslinear, AUDIO_ENCODING_SLINEAR, 8, 0 },
855 { 0, AudioEulinear, AUDIO_ENCODING_ULINEAR, 8, 0 },
856 { 0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 16, 0 },
857 { 0, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 16, 0 },
858 { 0, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 16, 0 },
859 { 0, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 16, 0 },
860 #if defined(AUDIO_SUPPORT_LINEAR24)
861 { 0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 24, 0 },
862 { 0, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 24, 0 },
863 { 0, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 24, 0 },
864 { 0, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 24, 0 },
865 #endif
866 { 0, AudioEslinear_le, AUDIO_ENCODING_SLINEAR_LE, 32, 0 },
867 { 0, AudioEulinear_le, AUDIO_ENCODING_ULINEAR_LE, 32, 0 },
868 { 0, AudioEslinear_be, AUDIO_ENCODING_SLINEAR_BE, 32, 0 },
869 { 0, AudioEulinear_be, AUDIO_ENCODING_ULINEAR_BE, 32, 0 },
870 };
871
872 static const struct portname itable[] = {
873 { AudioNmicrophone, AUDIO_MICROPHONE },
874 { AudioNline, AUDIO_LINE_IN },
875 { AudioNcd, AUDIO_CD },
876 { 0, 0 }
877 };
878 static const struct portname otable[] = {
879 { AudioNspeaker, AUDIO_SPEAKER },
880 { AudioNheadphone, AUDIO_HEADPHONE },
881 { AudioNline, AUDIO_LINE_OUT },
882 { 0, 0 }
883 };
884
885 static struct psref_class *audio_psref_class __read_mostly;
886
887 CFATTACH_DECL3_NEW(audio, sizeof(struct audio_softc),
888 audiomatch, audioattach, audiodetach, audioactivate, audiorescan,
889 audiochilddet, DVF_DETACH_SHUTDOWN);
890
891 static int
892 audiomatch(device_t parent, cfdata_t match, void *aux)
893 {
894 struct audio_attach_args *sa;
895
896 sa = aux;
897 DPRINTF(1, "%s: type=%d sa=%p hw=%p\n",
898 __func__, sa->type, sa, sa->hwif);
899 return (sa->type == AUDIODEV_TYPE_AUDIO) ? 1 : 0;
900 }
901
902 static void
903 audioattach(device_t parent, device_t self, void *aux)
904 {
905 struct audio_softc *sc;
906 struct audio_attach_args *sa;
907 const struct audio_hw_if *hw_if;
908 audio_format2_t phwfmt;
909 audio_format2_t rhwfmt;
910 audio_filter_reg_t pfil;
911 audio_filter_reg_t rfil;
912 const struct sysctlnode *node;
913 void *hdlp;
914 bool has_playback;
915 bool has_capture;
916 bool has_indep;
917 bool has_fulldup;
918 int mode;
919 int error;
920
921 sc = device_private(self);
922 sc->sc_dev = self;
923 sa = (struct audio_attach_args *)aux;
924 hw_if = sa->hwif;
925 hdlp = sa->hdl;
926
927 if (hw_if == NULL) {
928 panic("audioattach: missing hw_if method");
929 }
930 if (hw_if->get_locks == NULL || hw_if->get_props == NULL) {
931 aprint_error(": missing mandatory method\n");
932 return;
933 }
934
935 hw_if->get_locks(hdlp, &sc->sc_intr_lock, &sc->sc_lock);
936 sc->sc_props = hw_if->get_props(hdlp);
937
938 has_playback = (sc->sc_props & AUDIO_PROP_PLAYBACK);
939 has_capture = (sc->sc_props & AUDIO_PROP_CAPTURE);
940 has_indep = (sc->sc_props & AUDIO_PROP_INDEPENDENT);
941 has_fulldup = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
942
943 #ifdef DIAGNOSTIC
944 if (hw_if->query_format == NULL ||
945 hw_if->set_format == NULL ||
946 hw_if->getdev == NULL ||
947 hw_if->set_port == NULL ||
948 hw_if->get_port == NULL ||
949 hw_if->query_devinfo == NULL) {
950 aprint_error(": missing mandatory method\n");
951 return;
952 }
953 if (has_playback) {
954 if ((hw_if->start_output == NULL &&
955 hw_if->trigger_output == NULL) ||
956 hw_if->halt_output == NULL) {
957 aprint_error(": missing playback method\n");
958 }
959 }
960 if (has_capture) {
961 if ((hw_if->start_input == NULL &&
962 hw_if->trigger_input == NULL) ||
963 hw_if->halt_input == NULL) {
964 aprint_error(": missing capture method\n");
965 }
966 }
967 #endif
968
969 sc->hw_if = hw_if;
970 sc->hw_hdl = hdlp;
971 sc->hw_dev = parent;
972
973 sc->sc_exlock = 1;
974 sc->sc_blk_ms = AUDIO_BLK_MS;
975 SLIST_INIT(&sc->sc_files);
976 cv_init(&sc->sc_exlockcv, "audiolk");
977 sc->sc_am_capacity = 0;
978 sc->sc_am_used = 0;
979 sc->sc_am = NULL;
980
981 /* MMAP is now supported by upper layer. */
982 sc->sc_props |= AUDIO_PROP_MMAP;
983
984 KASSERT(has_playback || has_capture);
985 /* Unidirectional device must have neither FULLDUP nor INDEPENDENT. */
986 if (!has_playback || !has_capture) {
987 KASSERT(!has_indep);
988 KASSERT(!has_fulldup);
989 }
990
991 mode = 0;
992 if (has_playback) {
993 aprint_normal(": playback");
994 mode |= AUMODE_PLAY;
995 }
996 if (has_capture) {
997 aprint_normal("%c capture", has_playback ? ',' : ':');
998 mode |= AUMODE_RECORD;
999 }
1000 if (has_playback && has_capture) {
1001 if (has_fulldup)
1002 aprint_normal(", full duplex");
1003 else
1004 aprint_normal(", half duplex");
1005
1006 if (has_indep)
1007 aprint_normal(", independent");
1008 }
1009
1010 aprint_naive("\n");
1011 aprint_normal("\n");
1012
1013 /* probe hw params */
1014 memset(&phwfmt, 0, sizeof(phwfmt));
1015 memset(&rhwfmt, 0, sizeof(rhwfmt));
1016 memset(&pfil, 0, sizeof(pfil));
1017 memset(&rfil, 0, sizeof(rfil));
1018 if (has_indep) {
1019 int perror, rerror;
1020
1021 /* On independent devices, probe separately. */
1022 perror = audio_hw_probe(sc, &phwfmt, AUMODE_PLAY);
1023 rerror = audio_hw_probe(sc, &rhwfmt, AUMODE_RECORD);
1024 if (perror && rerror) {
1025 aprint_error_dev(self,
1026 "audio_hw_probe failed: perror=%d, rerror=%d\n",
1027 perror, rerror);
1028 goto bad;
1029 }
1030 if (perror) {
1031 mode &= ~AUMODE_PLAY;
1032 aprint_error_dev(self, "audio_hw_probe failed: "
1033 "errno=%d, playback disabled\n", perror);
1034 }
1035 if (rerror) {
1036 mode &= ~AUMODE_RECORD;
1037 aprint_error_dev(self, "audio_hw_probe failed: "
1038 "errno=%d, capture disabled\n", rerror);
1039 }
1040 } else {
1041 /*
1042 * On non independent devices or uni-directional devices,
1043 * probe once (simultaneously).
1044 */
1045 audio_format2_t *fmt = has_playback ? &phwfmt : &rhwfmt;
1046 error = audio_hw_probe(sc, fmt, mode);
1047 if (error) {
1048 aprint_error_dev(self,
1049 "audio_hw_probe failed: errno=%d\n", error);
1050 goto bad;
1051 }
1052 if (has_playback && has_capture)
1053 rhwfmt = phwfmt;
1054 }
1055
1056 /* Init hardware. */
1057 /* hw_probe() also validates [pr]hwfmt. */
1058 error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
1059 if (error) {
1060 aprint_error_dev(self,
1061 "audio_hw_set_format failed: errno=%d\n", error);
1062 goto bad;
1063 }
1064
1065 /*
1066 * Init track mixers. If at least one direction is available on
1067 * attach time, we assume a success.
1068 */
1069 error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
1070 if (sc->sc_pmixer == NULL && sc->sc_rmixer == NULL) {
1071 aprint_error_dev(self,
1072 "audio_mixers_init failed: errno=%d\n", error);
1073 goto bad;
1074 }
1075
1076 sc->sc_psz = pserialize_create();
1077 psref_target_init(&sc->sc_psref, audio_psref_class);
1078
1079 selinit(&sc->sc_wsel);
1080 selinit(&sc->sc_rsel);
1081
1082 /* Initial parameter of /dev/sound */
1083 sc->sc_sound_pparams = params_to_format2(&audio_default);
1084 sc->sc_sound_rparams = params_to_format2(&audio_default);
1085 sc->sc_sound_ppause = false;
1086 sc->sc_sound_rpause = false;
1087
1088 /* XXX TODO: consider about sc_ai */
1089
1090 mixer_init(sc);
1091 TRACE(2, "inputs ports=0x%x, input master=%d, "
1092 "output ports=0x%x, output master=%d",
1093 sc->sc_inports.allports, sc->sc_inports.master,
1094 sc->sc_outports.allports, sc->sc_outports.master);
1095
1096 sysctl_createv(&sc->sc_log, 0, NULL, &node,
1097 0,
1098 CTLTYPE_NODE, device_xname(sc->sc_dev),
1099 SYSCTL_DESCR("audio test"),
1100 NULL, 0,
1101 NULL, 0,
1102 CTL_HW,
1103 CTL_CREATE, CTL_EOL);
1104
1105 if (node != NULL) {
1106 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
1107 CTLFLAG_READWRITE,
1108 CTLTYPE_INT, "blk_ms",
1109 SYSCTL_DESCR("blocksize in msec"),
1110 audio_sysctl_blk_ms, 0, (void *)sc, 0,
1111 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1112
1113 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
1114 CTLFLAG_READWRITE,
1115 CTLTYPE_BOOL, "multiuser",
1116 SYSCTL_DESCR("allow multiple user access"),
1117 audio_sysctl_multiuser, 0, (void *)sc, 0,
1118 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1119
1120 #if defined(AUDIO_DEBUG)
1121 sysctl_createv(&sc->sc_log, 0, NULL, NULL,
1122 CTLFLAG_READWRITE,
1123 CTLTYPE_INT, "debug",
1124 SYSCTL_DESCR("debug level (0..4)"),
1125 audio_sysctl_debug, 0, (void *)sc, 0,
1126 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL);
1127 #endif
1128 }
1129
1130 #ifdef AUDIO_PM_IDLE
1131 callout_init(&sc->sc_idle_counter, 0);
1132 callout_setfunc(&sc->sc_idle_counter, audio_idle, self);
1133 #endif
1134
1135 if (!pmf_device_register(self, audio_suspend, audio_resume))
1136 aprint_error_dev(self, "couldn't establish power handler\n");
1137 #ifdef AUDIO_PM_IDLE
1138 if (!device_active_register(self, audio_activity))
1139 aprint_error_dev(self, "couldn't register activity handler\n");
1140 #endif
1141
1142 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_DOWN,
1143 audio_volume_down, true))
1144 aprint_error_dev(self, "couldn't add volume down handler\n");
1145 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_UP,
1146 audio_volume_up, true))
1147 aprint_error_dev(self, "couldn't add volume up handler\n");
1148 if (!pmf_event_register(self, PMFE_AUDIO_VOLUME_TOGGLE,
1149 audio_volume_toggle, true))
1150 aprint_error_dev(self, "couldn't add volume toggle handler\n");
1151
1152 #ifdef AUDIO_PM_IDLE
1153 callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
1154 #endif
1155
1156 #if defined(AUDIO_DEBUG)
1157 audio_mlog_init();
1158 #endif
1159
1160 audiorescan(self, NULL, NULL);
1161 sc->sc_exlock = 0;
1162 return;
1163
1164 bad:
1165 /* Clearing hw_if means that device is attached but disabled. */
1166 sc->hw_if = NULL;
1167 sc->sc_exlock = 0;
1168 aprint_error_dev(sc->sc_dev, "disabled\n");
1169 return;
1170 }
1171
1172 /*
1173 * Initialize hardware mixer.
1174 * This function is called from audioattach().
1175 */
1176 static void
1177 mixer_init(struct audio_softc *sc)
1178 {
1179 mixer_devinfo_t mi;
1180 int iclass, mclass, oclass, rclass;
1181 int record_master_found, record_source_found;
1182
1183 iclass = mclass = oclass = rclass = -1;
1184 sc->sc_inports.index = -1;
1185 sc->sc_inports.master = -1;
1186 sc->sc_inports.nports = 0;
1187 sc->sc_inports.isenum = false;
1188 sc->sc_inports.allports = 0;
1189 sc->sc_inports.isdual = false;
1190 sc->sc_inports.mixerout = -1;
1191 sc->sc_inports.cur_port = -1;
1192 sc->sc_outports.index = -1;
1193 sc->sc_outports.master = -1;
1194 sc->sc_outports.nports = 0;
1195 sc->sc_outports.isenum = false;
1196 sc->sc_outports.allports = 0;
1197 sc->sc_outports.isdual = false;
1198 sc->sc_outports.mixerout = -1;
1199 sc->sc_outports.cur_port = -1;
1200 sc->sc_monitor_port = -1;
1201 /*
1202 * Read through the underlying driver's list, picking out the class
1203 * names from the mixer descriptions. We'll need them to decode the
1204 * mixer descriptions on the next pass through the loop.
1205 */
1206 mutex_enter(sc->sc_lock);
1207 for(mi.index = 0; ; mi.index++) {
1208 if (audio_query_devinfo(sc, &mi) != 0)
1209 break;
1210 /*
1211 * The type of AUDIO_MIXER_CLASS merely introduces a class.
1212 * All the other types describe an actual mixer.
1213 */
1214 if (mi.type == AUDIO_MIXER_CLASS) {
1215 if (strcmp(mi.label.name, AudioCinputs) == 0)
1216 iclass = mi.mixer_class;
1217 if (strcmp(mi.label.name, AudioCmonitor) == 0)
1218 mclass = mi.mixer_class;
1219 if (strcmp(mi.label.name, AudioCoutputs) == 0)
1220 oclass = mi.mixer_class;
1221 if (strcmp(mi.label.name, AudioCrecord) == 0)
1222 rclass = mi.mixer_class;
1223 }
1224 }
1225 mutex_exit(sc->sc_lock);
1226
1227 /* Allocate save area. Ensure non-zero allocation. */
1228 sc->sc_nmixer_states = mi.index;
1229 sc->sc_mixer_state = kmem_zalloc(sizeof(sc->sc_mixer_state[0]) *
1230 (sc->sc_nmixer_states + 1), KM_SLEEP);
1231
1232 /*
1233 * This is where we assign each control in the "audio" model, to the
1234 * underlying "mixer" control. We walk through the whole list once,
1235 * assigning likely candidates as we come across them.
1236 */
1237 record_master_found = 0;
1238 record_source_found = 0;
1239 mutex_enter(sc->sc_lock);
1240 for(mi.index = 0; ; mi.index++) {
1241 if (audio_query_devinfo(sc, &mi) != 0)
1242 break;
1243 KASSERT(mi.index < sc->sc_nmixer_states);
1244 if (mi.type == AUDIO_MIXER_CLASS)
1245 continue;
1246 if (mi.mixer_class == iclass) {
1247 /*
1248 * AudioCinputs is only a fallback, when we don't
1249 * find what we're looking for in AudioCrecord, so
1250 * check the flags before accepting one of these.
1251 */
1252 if (strcmp(mi.label.name, AudioNmaster) == 0
1253 && record_master_found == 0)
1254 sc->sc_inports.master = mi.index;
1255 if (strcmp(mi.label.name, AudioNsource) == 0
1256 && record_source_found == 0) {
1257 if (mi.type == AUDIO_MIXER_ENUM) {
1258 int i;
1259 for(i = 0; i < mi.un.e.num_mem; i++)
1260 if (strcmp(mi.un.e.member[i].label.name,
1261 AudioNmixerout) == 0)
1262 sc->sc_inports.mixerout =
1263 mi.un.e.member[i].ord;
1264 }
1265 au_setup_ports(sc, &sc->sc_inports, &mi,
1266 itable);
1267 }
1268 if (strcmp(mi.label.name, AudioNdac) == 0 &&
1269 sc->sc_outports.master == -1)
1270 sc->sc_outports.master = mi.index;
1271 } else if (mi.mixer_class == mclass) {
1272 if (strcmp(mi.label.name, AudioNmonitor) == 0)
1273 sc->sc_monitor_port = mi.index;
1274 } else if (mi.mixer_class == oclass) {
1275 if (strcmp(mi.label.name, AudioNmaster) == 0)
1276 sc->sc_outports.master = mi.index;
1277 if (strcmp(mi.label.name, AudioNselect) == 0)
1278 au_setup_ports(sc, &sc->sc_outports, &mi,
1279 otable);
1280 } else if (mi.mixer_class == rclass) {
1281 /*
1282 * These are the preferred mixers for the audio record
1283 * controls, so set the flags here, but don't check.
1284 */
1285 if (strcmp(mi.label.name, AudioNmaster) == 0) {
1286 sc->sc_inports.master = mi.index;
1287 record_master_found = 1;
1288 }
1289 #if 1 /* Deprecated. Use AudioNmaster. */
1290 if (strcmp(mi.label.name, AudioNrecord) == 0) {
1291 sc->sc_inports.master = mi.index;
1292 record_master_found = 1;
1293 }
1294 if (strcmp(mi.label.name, AudioNvolume) == 0) {
1295 sc->sc_inports.master = mi.index;
1296 record_master_found = 1;
1297 }
1298 #endif
1299 if (strcmp(mi.label.name, AudioNsource) == 0) {
1300 if (mi.type == AUDIO_MIXER_ENUM) {
1301 int i;
1302 for(i = 0; i < mi.un.e.num_mem; i++)
1303 if (strcmp(mi.un.e.member[i].label.name,
1304 AudioNmixerout) == 0)
1305 sc->sc_inports.mixerout =
1306 mi.un.e.member[i].ord;
1307 }
1308 au_setup_ports(sc, &sc->sc_inports, &mi,
1309 itable);
1310 record_source_found = 1;
1311 }
1312 }
1313 }
1314 mutex_exit(sc->sc_lock);
1315 }
1316
1317 static int
1318 audioactivate(device_t self, enum devact act)
1319 {
1320 struct audio_softc *sc = device_private(self);
1321
1322 switch (act) {
1323 case DVACT_DEACTIVATE:
1324 mutex_enter(sc->sc_lock);
1325 sc->sc_dying = true;
1326 cv_broadcast(&sc->sc_exlockcv);
1327 mutex_exit(sc->sc_lock);
1328 return 0;
1329 default:
1330 return EOPNOTSUPP;
1331 }
1332 }
1333
1334 static int
1335 audiodetach(device_t self, int flags)
1336 {
1337 struct audio_softc *sc;
1338 struct audio_file *file;
1339 int error;
1340
1341 sc = device_private(self);
1342 TRACE(2, "flags=%d", flags);
1343
1344 /* device is not initialized */
1345 if (sc->hw_if == NULL)
1346 return 0;
1347
1348 /* Start draining existing accessors of the device. */
1349 error = config_detach_children(self, flags);
1350 if (error)
1351 return error;
1352
1353 /*
1354 * This waits currently running sysctls to finish if exists.
1355 * After this, no more new sysctls will come.
1356 */
1357 sysctl_teardown(&sc->sc_log);
1358
1359 mutex_enter(sc->sc_lock);
1360 sc->sc_dying = true;
1361 cv_broadcast(&sc->sc_exlockcv);
1362 if (sc->sc_pmixer)
1363 cv_broadcast(&sc->sc_pmixer->outcv);
1364 if (sc->sc_rmixer)
1365 cv_broadcast(&sc->sc_rmixer->outcv);
1366
1367 /* Prevent new users */
1368 SLIST_FOREACH(file, &sc->sc_files, entry) {
1369 atomic_store_relaxed(&file->dying, true);
1370 }
1371 mutex_exit(sc->sc_lock);
1372
1373 /*
1374 * Wait for existing users to drain.
1375 * - pserialize_perform waits for all pserialize_read sections on
1376 * all CPUs; after this, no more new psref_acquire can happen.
1377 * - psref_target_destroy waits for all extant acquired psrefs to
1378 * be psref_released.
1379 */
1380 pserialize_perform(sc->sc_psz);
1381 psref_target_destroy(&sc->sc_psref, audio_psref_class);
1382
1383 /*
1384 * We are now guaranteed that there are no calls to audio fileops
1385 * that hold sc, and any new calls with files that were for sc will
1386 * fail. Thus, we now have exclusive access to the softc.
1387 */
1388 sc->sc_exlock = 1;
1389
1390 /*
1391 * Clean up all open instances.
1392 */
1393 mutex_enter(sc->sc_lock);
1394 while ((file = SLIST_FIRST(&sc->sc_files)) != NULL) {
1395 mutex_enter(sc->sc_intr_lock);
1396 SLIST_REMOVE_HEAD(&sc->sc_files, entry);
1397 mutex_exit(sc->sc_intr_lock);
1398 if (file->ptrack || file->rtrack) {
1399 mutex_exit(sc->sc_lock);
1400 audio_unlink(sc, file);
1401 mutex_enter(sc->sc_lock);
1402 }
1403 }
1404 mutex_exit(sc->sc_lock);
1405
1406 pmf_event_deregister(self, PMFE_AUDIO_VOLUME_DOWN,
1407 audio_volume_down, true);
1408 pmf_event_deregister(self, PMFE_AUDIO_VOLUME_UP,
1409 audio_volume_up, true);
1410 pmf_event_deregister(self, PMFE_AUDIO_VOLUME_TOGGLE,
1411 audio_volume_toggle, true);
1412
1413 #ifdef AUDIO_PM_IDLE
1414 callout_halt(&sc->sc_idle_counter, sc->sc_lock);
1415
1416 device_active_deregister(self, audio_activity);
1417 #endif
1418
1419 pmf_device_deregister(self);
1420
1421 /* Free resources */
1422 if (sc->sc_pmixer) {
1423 audio_mixer_destroy(sc, sc->sc_pmixer);
1424 kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
1425 }
1426 if (sc->sc_rmixer) {
1427 audio_mixer_destroy(sc, sc->sc_rmixer);
1428 kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
1429 }
1430 if (sc->sc_am)
1431 kern_free(sc->sc_am);
1432
1433 seldestroy(&sc->sc_wsel);
1434 seldestroy(&sc->sc_rsel);
1435
1436 #ifdef AUDIO_PM_IDLE
1437 callout_destroy(&sc->sc_idle_counter);
1438 #endif
1439
1440 cv_destroy(&sc->sc_exlockcv);
1441
1442 #if defined(AUDIO_DEBUG)
1443 audio_mlog_free();
1444 #endif
1445
1446 return 0;
1447 }
1448
1449 static void
1450 audiochilddet(device_t self, device_t child)
1451 {
1452
1453 /* we hold no child references, so do nothing */
1454 }
1455
1456 static int
1457 audiosearch(device_t parent, cfdata_t cf, const int *locs, void *aux)
1458 {
1459
1460 if (config_probe(parent, cf, aux))
1461 config_attach(parent, cf, aux, NULL,
1462 CFARGS_NONE);
1463
1464 return 0;
1465 }
1466
1467 static int
1468 audiorescan(device_t self, const char *ifattr, const int *locators)
1469 {
1470 struct audio_softc *sc = device_private(self);
1471
1472 config_search(sc->sc_dev, NULL,
1473 CFARGS(.search = audiosearch));
1474
1475 return 0;
1476 }
1477
1478 /*
1479 * Called from hardware driver. This is where the MI audio driver gets
1480 * probed/attached to the hardware driver.
1481 */
1482 device_t
1483 audio_attach_mi(const struct audio_hw_if *ahwp, void *hdlp, device_t dev)
1484 {
1485 struct audio_attach_args arg;
1486
1487 #ifdef DIAGNOSTIC
1488 if (ahwp == NULL) {
1489 aprint_error("audio_attach_mi: NULL\n");
1490 return 0;
1491 }
1492 #endif
1493 arg.type = AUDIODEV_TYPE_AUDIO;
1494 arg.hwif = ahwp;
1495 arg.hdl = hdlp;
1496 return config_found(dev, &arg, audioprint,
1497 CFARGS(.iattr = "audiobus"));
1498 }
1499
1500 /*
1501 * audio_printf() outputs fmt... with the audio device name and MD device
1502 * name prefixed. If the message is considered to be related to the MD
1503 * driver, use this one instead of device_printf().
1504 */
1505 static void
1506 audio_printf(struct audio_softc *sc, const char *fmt, ...)
1507 {
1508 va_list ap;
1509
1510 printf("%s(%s): ", device_xname(sc->sc_dev), device_xname(sc->hw_dev));
1511 va_start(ap, fmt);
1512 vprintf(fmt, ap);
1513 va_end(ap);
1514 }
1515
1516 /*
1517 * Enter critical section and also keep sc_lock.
1518 * If successful, returns 0 with sc_lock held. Otherwise returns errno.
1519 * Must be called without sc_lock held.
1520 */
1521 static int
1522 audio_exlock_mutex_enter(struct audio_softc *sc)
1523 {
1524 int error;
1525
1526 mutex_enter(sc->sc_lock);
1527 if (sc->sc_dying) {
1528 mutex_exit(sc->sc_lock);
1529 return EIO;
1530 }
1531
1532 while (__predict_false(sc->sc_exlock != 0)) {
1533 error = cv_wait_sig(&sc->sc_exlockcv, sc->sc_lock);
1534 if (sc->sc_dying)
1535 error = EIO;
1536 if (error) {
1537 mutex_exit(sc->sc_lock);
1538 return error;
1539 }
1540 }
1541
1542 /* Acquire */
1543 sc->sc_exlock = 1;
1544 return 0;
1545 }
1546
1547 /*
1548 * Exit critical section and exit sc_lock.
1549 * Must be called with sc_lock held.
1550 */
1551 static void
1552 audio_exlock_mutex_exit(struct audio_softc *sc)
1553 {
1554
1555 KASSERT(mutex_owned(sc->sc_lock));
1556
1557 sc->sc_exlock = 0;
1558 cv_broadcast(&sc->sc_exlockcv);
1559 mutex_exit(sc->sc_lock);
1560 }
1561
1562 /*
1563 * Enter critical section.
1564 * If successful, it returns 0. Otherwise returns errno.
1565 * Must be called without sc_lock held.
1566 * This function returns without sc_lock held.
1567 */
1568 static int
1569 audio_exlock_enter(struct audio_softc *sc)
1570 {
1571 int error;
1572
1573 error = audio_exlock_mutex_enter(sc);
1574 if (error)
1575 return error;
1576 mutex_exit(sc->sc_lock);
1577 return 0;
1578 }
1579
1580 /*
1581 * Exit critical section.
1582 * Must be called without sc_lock held.
1583 */
1584 static void
1585 audio_exlock_exit(struct audio_softc *sc)
1586 {
1587
1588 mutex_enter(sc->sc_lock);
1589 audio_exlock_mutex_exit(sc);
1590 }
1591
1592 /*
1593 * Increment reference counter for this sc.
1594 * This is intended to be used for open.
1595 */
1596 void
1597 audio_sc_acquire_foropen(struct audio_softc *sc, struct psref *refp)
1598 {
1599 int s;
1600
1601 /* Block audiodetach while we acquire a reference */
1602 s = pserialize_read_enter();
1603
1604 /*
1605 * We don't examine sc_dying here. However, all open methods
1606 * call audio_exlock_enter() right after this, so we can examine
1607 * sc_dying in it.
1608 */
1609
1610 /* Acquire a reference */
1611 psref_acquire(refp, &sc->sc_psref, audio_psref_class);
1612
1613 /* Now sc won't go away until we drop the reference count */
1614 pserialize_read_exit(s);
1615 }
1616
1617 /*
1618 * Get sc from file, and increment reference counter for this sc.
1619 * This is intended to be used for methods other than open.
1620 * If successful, returns sc. Otherwise returns NULL.
1621 */
1622 struct audio_softc *
1623 audio_sc_acquire_fromfile(audio_file_t *file, struct psref *refp)
1624 {
1625 int s;
1626 bool dying;
1627
1628 /* Block audiodetach while we acquire a reference */
1629 s = pserialize_read_enter();
1630
1631 /* If close or audiodetach already ran, tough -- no more audio */
1632 dying = atomic_load_relaxed(&file->dying);
1633 if (dying) {
1634 pserialize_read_exit(s);
1635 return NULL;
1636 }
1637
1638 /* Acquire a reference */
1639 psref_acquire(refp, &file->sc->sc_psref, audio_psref_class);
1640
1641 /* Now sc won't go away until we drop the reference count */
1642 pserialize_read_exit(s);
1643
1644 return file->sc;
1645 }
1646
1647 /*
1648 * Decrement reference counter for this sc.
1649 */
1650 void
1651 audio_sc_release(struct audio_softc *sc, struct psref *refp)
1652 {
1653
1654 psref_release(refp, &sc->sc_psref, audio_psref_class);
1655 }
1656
1657 /*
1658 * Wait for I/O to complete, releasing sc_lock.
1659 * Must be called with sc_lock held.
1660 */
1661 static int
1662 audio_track_waitio(struct audio_softc *sc, audio_track_t *track)
1663 {
1664 int error;
1665
1666 KASSERT(track);
1667 KASSERT(mutex_owned(sc->sc_lock));
1668
1669 /* Wait for pending I/O to complete. */
1670 error = cv_timedwait_sig(&track->mixer->outcv, sc->sc_lock,
1671 mstohz(AUDIO_TIMEOUT));
1672 if (sc->sc_suspending) {
1673 /* If it's about to suspend, ignore timeout error. */
1674 if (error == EWOULDBLOCK) {
1675 TRACET(2, track, "timeout (suspending)");
1676 return 0;
1677 }
1678 }
1679 if (sc->sc_dying) {
1680 error = EIO;
1681 }
1682 if (error) {
1683 TRACET(2, track, "cv_timedwait_sig failed %d", error);
1684 if (error == EWOULDBLOCK)
1685 audio_printf(sc, "device timeout\n");
1686 } else {
1687 TRACET(3, track, "wakeup");
1688 }
1689 return error;
1690 }
1691
1692 /*
1693 * Try to acquire track lock.
1694 * It doesn't block if the track lock is already acquired.
1695 * Returns true if the track lock was acquired, or false if the track
1696 * lock was already acquired.
1697 */
1698 static __inline bool
1699 audio_track_lock_tryenter(audio_track_t *track)
1700 {
1701
1702 if (atomic_swap_uint(&track->lock, 1) != 0)
1703 return false;
1704 membar_enter();
1705 return true;
1706 }
1707
1708 /*
1709 * Acquire track lock.
1710 */
1711 static __inline void
1712 audio_track_lock_enter(audio_track_t *track)
1713 {
1714
1715 /* Don't sleep here. */
1716 while (audio_track_lock_tryenter(track) == false)
1717 SPINLOCK_BACKOFF_HOOK;
1718 }
1719
1720 /*
1721 * Release track lock.
1722 */
1723 static __inline void
1724 audio_track_lock_exit(audio_track_t *track)
1725 {
1726
1727 atomic_store_release(&track->lock, 0);
1728 }
1729
1730
1731 static int
1732 audioopen(dev_t dev, int flags, int ifmt, struct lwp *l)
1733 {
1734 struct audio_softc *sc;
1735 struct psref sc_ref;
1736 int bound;
1737 int error;
1738
1739 /* Find the device */
1740 sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
1741 if (sc == NULL || sc->hw_if == NULL)
1742 return ENXIO;
1743
1744 bound = curlwp_bind();
1745 audio_sc_acquire_foropen(sc, &sc_ref);
1746
1747 error = audio_exlock_enter(sc);
1748 if (error)
1749 goto done;
1750
1751 device_active(sc->sc_dev, DVA_SYSTEM);
1752 switch (AUDIODEV(dev)) {
1753 case SOUND_DEVICE:
1754 case AUDIO_DEVICE:
1755 error = audio_open(dev, sc, flags, ifmt, l, NULL);
1756 break;
1757 case AUDIOCTL_DEVICE:
1758 error = audioctl_open(dev, sc, flags, ifmt, l);
1759 break;
1760 case MIXER_DEVICE:
1761 error = mixer_open(dev, sc, flags, ifmt, l);
1762 break;
1763 default:
1764 error = ENXIO;
1765 break;
1766 }
1767 audio_exlock_exit(sc);
1768
1769 done:
1770 audio_sc_release(sc, &sc_ref);
1771 curlwp_bindx(bound);
1772 return error;
1773 }
1774
1775 static int
1776 audioclose(struct file *fp)
1777 {
1778 struct audio_softc *sc;
1779 struct psref sc_ref;
1780 audio_file_t *file;
1781 int bound;
1782 int error;
1783 dev_t dev;
1784
1785 KASSERT(fp->f_audioctx);
1786 file = fp->f_audioctx;
1787 dev = file->dev;
1788 error = 0;
1789
1790 /*
1791 * audioclose() must
1792 * - unplug track from the trackmixer (and unplug anything from softc),
1793 * if sc exists.
1794 * - free all memory objects, regardless of sc.
1795 */
1796
1797 bound = curlwp_bind();
1798 sc = audio_sc_acquire_fromfile(file, &sc_ref);
1799 if (sc) {
1800 switch (AUDIODEV(dev)) {
1801 case SOUND_DEVICE:
1802 case AUDIO_DEVICE:
1803 error = audio_close(sc, file);
1804 break;
1805 case AUDIOCTL_DEVICE:
1806 mutex_enter(sc->sc_lock);
1807 mutex_enter(sc->sc_intr_lock);
1808 SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
1809 mutex_exit(sc->sc_intr_lock);
1810 mutex_exit(sc->sc_lock);
1811 error = 0;
1812 break;
1813 case MIXER_DEVICE:
1814 mutex_enter(sc->sc_lock);
1815 mutex_enter(sc->sc_intr_lock);
1816 SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
1817 mutex_exit(sc->sc_intr_lock);
1818 mutex_exit(sc->sc_lock);
1819 error = mixer_close(sc, file);
1820 break;
1821 default:
1822 error = ENXIO;
1823 break;
1824 }
1825
1826 audio_sc_release(sc, &sc_ref);
1827 }
1828 curlwp_bindx(bound);
1829
1830 /* Free memory objects anyway */
1831 TRACEF(2, file, "free memory");
1832 if (file->ptrack)
1833 audio_track_destroy(file->ptrack);
1834 if (file->rtrack)
1835 audio_track_destroy(file->rtrack);
1836 kmem_free(file, sizeof(*file));
1837 fp->f_audioctx = NULL;
1838
1839 return error;
1840 }
1841
1842 static int
1843 audioread(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
1844 int ioflag)
1845 {
1846 struct audio_softc *sc;
1847 struct psref sc_ref;
1848 audio_file_t *file;
1849 int bound;
1850 int error;
1851 dev_t dev;
1852
1853 KASSERT(fp->f_audioctx);
1854 file = fp->f_audioctx;
1855 dev = file->dev;
1856
1857 bound = curlwp_bind();
1858 sc = audio_sc_acquire_fromfile(file, &sc_ref);
1859 if (sc == NULL) {
1860 error = EIO;
1861 goto done;
1862 }
1863
1864 if (fp->f_flag & O_NONBLOCK)
1865 ioflag |= IO_NDELAY;
1866
1867 switch (AUDIODEV(dev)) {
1868 case SOUND_DEVICE:
1869 case AUDIO_DEVICE:
1870 error = audio_read(sc, uio, ioflag, file);
1871 break;
1872 case AUDIOCTL_DEVICE:
1873 case MIXER_DEVICE:
1874 error = ENODEV;
1875 break;
1876 default:
1877 error = ENXIO;
1878 break;
1879 }
1880
1881 audio_sc_release(sc, &sc_ref);
1882 done:
1883 curlwp_bindx(bound);
1884 return error;
1885 }
1886
1887 static int
1888 audiowrite(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
1889 int ioflag)
1890 {
1891 struct audio_softc *sc;
1892 struct psref sc_ref;
1893 audio_file_t *file;
1894 int bound;
1895 int error;
1896 dev_t dev;
1897
1898 KASSERT(fp->f_audioctx);
1899 file = fp->f_audioctx;
1900 dev = file->dev;
1901
1902 bound = curlwp_bind();
1903 sc = audio_sc_acquire_fromfile(file, &sc_ref);
1904 if (sc == NULL) {
1905 error = EIO;
1906 goto done;
1907 }
1908
1909 if (fp->f_flag & O_NONBLOCK)
1910 ioflag |= IO_NDELAY;
1911
1912 switch (AUDIODEV(dev)) {
1913 case SOUND_DEVICE:
1914 case AUDIO_DEVICE:
1915 error = audio_write(sc, uio, ioflag, file);
1916 break;
1917 case AUDIOCTL_DEVICE:
1918 case MIXER_DEVICE:
1919 error = ENODEV;
1920 break;
1921 default:
1922 error = ENXIO;
1923 break;
1924 }
1925
1926 audio_sc_release(sc, &sc_ref);
1927 done:
1928 curlwp_bindx(bound);
1929 return error;
1930 }
1931
1932 static int
1933 audioioctl(struct file *fp, u_long cmd, void *addr)
1934 {
1935 struct audio_softc *sc;
1936 struct psref sc_ref;
1937 audio_file_t *file;
1938 struct lwp *l = curlwp;
1939 int bound;
1940 int error;
1941 dev_t dev;
1942
1943 KASSERT(fp->f_audioctx);
1944 file = fp->f_audioctx;
1945 dev = file->dev;
1946
1947 bound = curlwp_bind();
1948 sc = audio_sc_acquire_fromfile(file, &sc_ref);
1949 if (sc == NULL) {
1950 error = EIO;
1951 goto done;
1952 }
1953
1954 switch (AUDIODEV(dev)) {
1955 case SOUND_DEVICE:
1956 case AUDIO_DEVICE:
1957 case AUDIOCTL_DEVICE:
1958 mutex_enter(sc->sc_lock);
1959 device_active(sc->sc_dev, DVA_SYSTEM);
1960 mutex_exit(sc->sc_lock);
1961 if (IOCGROUP(cmd) == IOCGROUP(AUDIO_MIXER_READ))
1962 error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
1963 else
1964 error = audio_ioctl(dev, sc, cmd, addr, fp->f_flag, l,
1965 file);
1966 break;
1967 case MIXER_DEVICE:
1968 error = mixer_ioctl(sc, cmd, addr, fp->f_flag, l);
1969 break;
1970 default:
1971 error = ENXIO;
1972 break;
1973 }
1974
1975 audio_sc_release(sc, &sc_ref);
1976 done:
1977 curlwp_bindx(bound);
1978 return error;
1979 }
1980
1981 static int
1982 audiostat(struct file *fp, struct stat *st)
1983 {
1984 struct audio_softc *sc;
1985 struct psref sc_ref;
1986 audio_file_t *file;
1987 int bound;
1988 int error;
1989
1990 KASSERT(fp->f_audioctx);
1991 file = fp->f_audioctx;
1992
1993 bound = curlwp_bind();
1994 sc = audio_sc_acquire_fromfile(file, &sc_ref);
1995 if (sc == NULL) {
1996 error = EIO;
1997 goto done;
1998 }
1999
2000 error = 0;
2001 memset(st, 0, sizeof(*st));
2002
2003 st->st_dev = file->dev;
2004 st->st_uid = kauth_cred_geteuid(fp->f_cred);
2005 st->st_gid = kauth_cred_getegid(fp->f_cred);
2006 st->st_mode = S_IFCHR;
2007
2008 audio_sc_release(sc, &sc_ref);
2009 done:
2010 curlwp_bindx(bound);
2011 return error;
2012 }
2013
2014 static int
2015 audiopoll(struct file *fp, int events)
2016 {
2017 struct audio_softc *sc;
2018 struct psref sc_ref;
2019 audio_file_t *file;
2020 struct lwp *l = curlwp;
2021 int bound;
2022 int revents;
2023 dev_t dev;
2024
2025 KASSERT(fp->f_audioctx);
2026 file = fp->f_audioctx;
2027 dev = file->dev;
2028
2029 bound = curlwp_bind();
2030 sc = audio_sc_acquire_fromfile(file, &sc_ref);
2031 if (sc == NULL) {
2032 revents = POLLERR;
2033 goto done;
2034 }
2035
2036 switch (AUDIODEV(dev)) {
2037 case SOUND_DEVICE:
2038 case AUDIO_DEVICE:
2039 revents = audio_poll(sc, events, l, file);
2040 break;
2041 case AUDIOCTL_DEVICE:
2042 case MIXER_DEVICE:
2043 revents = 0;
2044 break;
2045 default:
2046 revents = POLLERR;
2047 break;
2048 }
2049
2050 audio_sc_release(sc, &sc_ref);
2051 done:
2052 curlwp_bindx(bound);
2053 return revents;
2054 }
2055
2056 static int
2057 audiokqfilter(struct file *fp, struct knote *kn)
2058 {
2059 struct audio_softc *sc;
2060 struct psref sc_ref;
2061 audio_file_t *file;
2062 dev_t dev;
2063 int bound;
2064 int error;
2065
2066 KASSERT(fp->f_audioctx);
2067 file = fp->f_audioctx;
2068 dev = file->dev;
2069
2070 bound = curlwp_bind();
2071 sc = audio_sc_acquire_fromfile(file, &sc_ref);
2072 if (sc == NULL) {
2073 error = EIO;
2074 goto done;
2075 }
2076
2077 switch (AUDIODEV(dev)) {
2078 case SOUND_DEVICE:
2079 case AUDIO_DEVICE:
2080 error = audio_kqfilter(sc, file, kn);
2081 break;
2082 case AUDIOCTL_DEVICE:
2083 case MIXER_DEVICE:
2084 error = ENODEV;
2085 break;
2086 default:
2087 error = ENXIO;
2088 break;
2089 }
2090
2091 audio_sc_release(sc, &sc_ref);
2092 done:
2093 curlwp_bindx(bound);
2094 return error;
2095 }
2096
2097 static int
2098 audiommap(struct file *fp, off_t *offp, size_t len, int prot, int *flagsp,
2099 int *advicep, struct uvm_object **uobjp, int *maxprotp)
2100 {
2101 struct audio_softc *sc;
2102 struct psref sc_ref;
2103 audio_file_t *file;
2104 dev_t dev;
2105 int bound;
2106 int error;
2107
2108 KASSERT(fp->f_audioctx);
2109 file = fp->f_audioctx;
2110 dev = file->dev;
2111
2112 bound = curlwp_bind();
2113 sc = audio_sc_acquire_fromfile(file, &sc_ref);
2114 if (sc == NULL) {
2115 error = EIO;
2116 goto done;
2117 }
2118
2119 mutex_enter(sc->sc_lock);
2120 device_active(sc->sc_dev, DVA_SYSTEM); /* XXXJDM */
2121 mutex_exit(sc->sc_lock);
2122
2123 switch (AUDIODEV(dev)) {
2124 case SOUND_DEVICE:
2125 case AUDIO_DEVICE:
2126 error = audio_mmap(sc, offp, len, prot, flagsp, advicep,
2127 uobjp, maxprotp, file);
2128 break;
2129 case AUDIOCTL_DEVICE:
2130 case MIXER_DEVICE:
2131 default:
2132 error = ENOTSUP;
2133 break;
2134 }
2135
2136 audio_sc_release(sc, &sc_ref);
2137 done:
2138 curlwp_bindx(bound);
2139 return error;
2140 }
2141
2142
2143 /* Exported interfaces for audiobell. */
2144
2145 /*
2146 * Open for audiobell.
2147 * It stores allocated file to *filep.
2148 * If successful returns 0, otherwise errno.
2149 */
2150 int
2151 audiobellopen(dev_t dev, audio_file_t **filep)
2152 {
2153 struct audio_softc *sc;
2154 struct psref sc_ref;
2155 int bound;
2156 int error;
2157
2158 /* Find the device */
2159 sc = device_lookup_private(&audio_cd, AUDIOUNIT(dev));
2160 if (sc == NULL || sc->hw_if == NULL)
2161 return ENXIO;
2162
2163 bound = curlwp_bind();
2164 audio_sc_acquire_foropen(sc, &sc_ref);
2165
2166 error = audio_exlock_enter(sc);
2167 if (error)
2168 goto done;
2169
2170 device_active(sc->sc_dev, DVA_SYSTEM);
2171 error = audio_open(dev, sc, FWRITE, 0, curlwp, filep);
2172
2173 audio_exlock_exit(sc);
2174 done:
2175 audio_sc_release(sc, &sc_ref);
2176 curlwp_bindx(bound);
2177 return error;
2178 }
2179
2180 /* Close for audiobell */
2181 int
2182 audiobellclose(audio_file_t *file)
2183 {
2184 struct audio_softc *sc;
2185 struct psref sc_ref;
2186 int bound;
2187 int error;
2188
2189 error = 0;
2190 /*
2191 * audiobellclose() must
2192 * - unplug track from the trackmixer if sc exist.
2193 * - free all memory objects, regardless of sc.
2194 */
2195 bound = curlwp_bind();
2196 sc = audio_sc_acquire_fromfile(file, &sc_ref);
2197 if (sc) {
2198 error = audio_close(sc, file);
2199 audio_sc_release(sc, &sc_ref);
2200 }
2201 curlwp_bindx(bound);
2202
2203 /* Free memory objects anyway */
2204 KASSERT(file->ptrack);
2205 audio_track_destroy(file->ptrack);
2206 KASSERT(file->rtrack == NULL);
2207 kmem_free(file, sizeof(*file));
2208 return error;
2209 }
2210
2211 /* Set sample rate for audiobell */
2212 int
2213 audiobellsetrate(audio_file_t *file, u_int sample_rate)
2214 {
2215 struct audio_softc *sc;
2216 struct psref sc_ref;
2217 struct audio_info ai;
2218 int bound;
2219 int error;
2220
2221 bound = curlwp_bind();
2222 sc = audio_sc_acquire_fromfile(file, &sc_ref);
2223 if (sc == NULL) {
2224 error = EIO;
2225 goto done1;
2226 }
2227
2228 AUDIO_INITINFO(&ai);
2229 ai.play.sample_rate = sample_rate;
2230
2231 error = audio_exlock_enter(sc);
2232 if (error)
2233 goto done2;
2234 error = audio_file_setinfo(sc, file, &ai);
2235 audio_exlock_exit(sc);
2236
2237 done2:
2238 audio_sc_release(sc, &sc_ref);
2239 done1:
2240 curlwp_bindx(bound);
2241 return error;
2242 }
2243
2244 /* Playback for audiobell */
2245 int
2246 audiobellwrite(audio_file_t *file, struct uio *uio)
2247 {
2248 struct audio_softc *sc;
2249 struct psref sc_ref;
2250 int bound;
2251 int error;
2252
2253 bound = curlwp_bind();
2254 sc = audio_sc_acquire_fromfile(file, &sc_ref);
2255 if (sc == NULL) {
2256 error = EIO;
2257 goto done;
2258 }
2259
2260 error = audio_write(sc, uio, 0, file);
2261
2262 audio_sc_release(sc, &sc_ref);
2263 done:
2264 curlwp_bindx(bound);
2265 return error;
2266 }
2267
2268
2269 /*
2270 * Audio driver
2271 */
2272
2273 /*
2274 * Must be called with sc_exlock held and without sc_lock held.
2275 */
2276 int
2277 audio_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
2278 struct lwp *l, audio_file_t **bellfile)
2279 {
2280 struct audio_info ai;
2281 struct file *fp;
2282 audio_file_t *af;
2283 audio_ring_t *hwbuf;
2284 bool fullduplex;
2285 bool cred_held;
2286 bool hw_opened;
2287 bool rmixer_started;
2288 bool inserted;
2289 int fd;
2290 int error;
2291
2292 KASSERT(sc->sc_exlock);
2293
2294 TRACE(1, "%sdev=%s flags=0x%x po=%d ro=%d",
2295 (audiodebug >= 3) ? "start " : "",
2296 ISDEVSOUND(dev) ? "sound" : "audio",
2297 flags, sc->sc_popens, sc->sc_ropens);
2298
2299 fp = NULL;
2300 cred_held = false;
2301 hw_opened = false;
2302 rmixer_started = false;
2303 inserted = false;
2304
2305 af = kmem_zalloc(sizeof(*af), KM_SLEEP);
2306 af->sc = sc;
2307 af->dev = dev;
2308 if ((flags & FWRITE) != 0 && audio_can_playback(sc))
2309 af->mode |= AUMODE_PLAY | AUMODE_PLAY_ALL;
2310 if ((flags & FREAD) != 0 && audio_can_capture(sc))
2311 af->mode |= AUMODE_RECORD;
2312 if (af->mode == 0) {
2313 error = ENXIO;
2314 goto bad;
2315 }
2316
2317 fullduplex = (sc->sc_props & AUDIO_PROP_FULLDUPLEX);
2318
2319 /*
2320 * On half duplex hardware,
2321 * 1. if mode is (PLAY | REC), let mode PLAY.
2322 * 2. if mode is PLAY, let mode PLAY if no rec tracks, otherwise error.
2323 * 3. if mode is REC, let mode REC if no play tracks, otherwise error.
2324 */
2325 if (fullduplex == false) {
2326 if ((af->mode & AUMODE_PLAY)) {
2327 if (sc->sc_ropens != 0) {
2328 TRACE(1, "record track already exists");
2329 error = ENODEV;
2330 goto bad;
2331 }
2332 /* Play takes precedence */
2333 af->mode &= ~AUMODE_RECORD;
2334 }
2335 if ((af->mode & AUMODE_RECORD)) {
2336 if (sc->sc_popens != 0) {
2337 TRACE(1, "play track already exists");
2338 error = ENODEV;
2339 goto bad;
2340 }
2341 }
2342 }
2343
2344 /* Create tracks */
2345 if ((af->mode & AUMODE_PLAY))
2346 af->ptrack = audio_track_create(sc, sc->sc_pmixer);
2347 if ((af->mode & AUMODE_RECORD))
2348 af->rtrack = audio_track_create(sc, sc->sc_rmixer);
2349
2350 /* Set parameters */
2351 AUDIO_INITINFO(&ai);
2352 if (bellfile) {
2353 /* If audiobell, only sample_rate will be set later. */
2354 ai.play.sample_rate = audio_default.sample_rate;
2355 ai.play.encoding = AUDIO_ENCODING_SLINEAR_NE;
2356 ai.play.channels = 1;
2357 ai.play.precision = 16;
2358 ai.play.pause = 0;
2359 } else if (ISDEVAUDIO(dev)) {
2360 /* If /dev/audio, initialize everytime. */
2361 ai.play.sample_rate = audio_default.sample_rate;
2362 ai.play.encoding = audio_default.encoding;
2363 ai.play.channels = audio_default.channels;
2364 ai.play.precision = audio_default.precision;
2365 ai.play.pause = 0;
2366 ai.record.sample_rate = audio_default.sample_rate;
2367 ai.record.encoding = audio_default.encoding;
2368 ai.record.channels = audio_default.channels;
2369 ai.record.precision = audio_default.precision;
2370 ai.record.pause = 0;
2371 } else {
2372 /* If /dev/sound, take over the previous parameters. */
2373 ai.play.sample_rate = sc->sc_sound_pparams.sample_rate;
2374 ai.play.encoding = sc->sc_sound_pparams.encoding;
2375 ai.play.channels = sc->sc_sound_pparams.channels;
2376 ai.play.precision = sc->sc_sound_pparams.precision;
2377 ai.play.pause = sc->sc_sound_ppause;
2378 ai.record.sample_rate = sc->sc_sound_rparams.sample_rate;
2379 ai.record.encoding = sc->sc_sound_rparams.encoding;
2380 ai.record.channels = sc->sc_sound_rparams.channels;
2381 ai.record.precision = sc->sc_sound_rparams.precision;
2382 ai.record.pause = sc->sc_sound_rpause;
2383 }
2384 error = audio_file_setinfo(sc, af, &ai);
2385 if (error)
2386 goto bad;
2387
2388 if (sc->sc_popens + sc->sc_ropens == 0) {
2389 /* First open */
2390
2391 sc->sc_cred = kauth_cred_get();
2392 kauth_cred_hold(sc->sc_cred);
2393 cred_held = true;
2394
2395 if (sc->hw_if->open) {
2396 int hwflags;
2397
2398 /*
2399 * Call hw_if->open() only at first open of
2400 * combination of playback and recording.
2401 * On full duplex hardware, the flags passed to
2402 * hw_if->open() is always (FREAD | FWRITE)
2403 * regardless of this open()'s flags.
2404 * see also dev/isa/aria.c
2405 * On half duplex hardware, the flags passed to
2406 * hw_if->open() is either FREAD or FWRITE.
2407 * see also arch/evbarm/mini2440/audio_mini2440.c
2408 */
2409 if (fullduplex) {
2410 hwflags = FREAD | FWRITE;
2411 } else {
2412 /* Construct hwflags from af->mode. */
2413 hwflags = 0;
2414 if ((af->mode & AUMODE_PLAY) != 0)
2415 hwflags |= FWRITE;
2416 if ((af->mode & AUMODE_RECORD) != 0)
2417 hwflags |= FREAD;
2418 }
2419
2420 mutex_enter(sc->sc_lock);
2421 mutex_enter(sc->sc_intr_lock);
2422 error = sc->hw_if->open(sc->hw_hdl, hwflags);
2423 mutex_exit(sc->sc_intr_lock);
2424 mutex_exit(sc->sc_lock);
2425 if (error)
2426 goto bad;
2427 }
2428 /*
2429 * Regardless of whether we called hw_if->open (whether
2430 * hw_if->open exists) or not, we move to the Opened phase
2431 * here. Therefore from this point, we have to call
2432 * hw_if->close (if exists) whenever abort.
2433 * Note that both of hw_if->{open,close} are optional.
2434 */
2435 hw_opened = true;
2436
2437 /*
2438 * Set speaker mode when a half duplex.
2439 * XXX I'm not sure this is correct.
2440 */
2441 if (1/*XXX*/) {
2442 if (sc->hw_if->speaker_ctl) {
2443 int on;
2444 if (af->ptrack) {
2445 on = 1;
2446 } else {
2447 on = 0;
2448 }
2449 mutex_enter(sc->sc_lock);
2450 mutex_enter(sc->sc_intr_lock);
2451 error = sc->hw_if->speaker_ctl(sc->hw_hdl, on);
2452 mutex_exit(sc->sc_intr_lock);
2453 mutex_exit(sc->sc_lock);
2454 if (error)
2455 goto bad;
2456 }
2457 }
2458 } else if (sc->sc_multiuser == false) {
2459 uid_t euid = kauth_cred_geteuid(kauth_cred_get());
2460 if (euid != 0 && euid != kauth_cred_geteuid(sc->sc_cred)) {
2461 error = EPERM;
2462 goto bad;
2463 }
2464 }
2465
2466 /* Call init_output if this is the first playback open. */
2467 if (af->ptrack && sc->sc_popens == 0) {
2468 if (sc->hw_if->init_output) {
2469 hwbuf = &sc->sc_pmixer->hwbuf;
2470 mutex_enter(sc->sc_lock);
2471 mutex_enter(sc->sc_intr_lock);
2472 error = sc->hw_if->init_output(sc->hw_hdl,
2473 hwbuf->mem,
2474 hwbuf->capacity *
2475 hwbuf->fmt.channels * hwbuf->fmt.stride / NBBY);
2476 mutex_exit(sc->sc_intr_lock);
2477 mutex_exit(sc->sc_lock);
2478 if (error)
2479 goto bad;
2480 }
2481 }
2482 /*
2483 * Call init_input and start rmixer, if this is the first recording
2484 * open. See pause consideration notes.
2485 */
2486 if (af->rtrack && sc->sc_ropens == 0) {
2487 if (sc->hw_if->init_input) {
2488 hwbuf = &sc->sc_rmixer->hwbuf;
2489 mutex_enter(sc->sc_lock);
2490 mutex_enter(sc->sc_intr_lock);
2491 error = sc->hw_if->init_input(sc->hw_hdl,
2492 hwbuf->mem,
2493 hwbuf->capacity *
2494 hwbuf->fmt.channels * hwbuf->fmt.stride / NBBY);
2495 mutex_exit(sc->sc_intr_lock);
2496 mutex_exit(sc->sc_lock);
2497 if (error)
2498 goto bad;
2499 }
2500
2501 mutex_enter(sc->sc_lock);
2502 audio_rmixer_start(sc);
2503 mutex_exit(sc->sc_lock);
2504 rmixer_started = true;
2505 }
2506
2507 /*
2508 * This is the last sc_lock section in the function, so we have to
2509 * examine sc_dying again before starting the rest tasks. Because
2510 * audiodeatch() may have been invoked (and it would set sc_dying)
2511 * from the time audioopen() was executed until now. If it happens,
2512 * audiodetach() may already have set file->dying for all sc_files
2513 * that exist at that point, so that audioopen() must abort without
2514 * inserting af to sc_files, in order to keep consistency.
2515 */
2516 mutex_enter(sc->sc_lock);
2517 if (sc->sc_dying) {
2518 mutex_exit(sc->sc_lock);
2519 error = ENXIO;
2520 goto bad;
2521 }
2522
2523 /* Count up finally */
2524 if (af->ptrack)
2525 sc->sc_popens++;
2526 if (af->rtrack)
2527 sc->sc_ropens++;
2528 mutex_enter(sc->sc_intr_lock);
2529 SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
2530 mutex_exit(sc->sc_intr_lock);
2531 mutex_exit(sc->sc_lock);
2532 inserted = true;
2533
2534 if (bellfile) {
2535 *bellfile = af;
2536 } else {
2537 error = fd_allocfile(&fp, &fd);
2538 if (error)
2539 goto bad;
2540
2541 error = fd_clone(fp, fd, flags, &audio_fileops, af);
2542 KASSERTMSG(error == EMOVEFD, "error=%d", error);
2543 }
2544
2545 /* Be nothing else after fd_clone */
2546
2547 TRACEF(3, af, "done");
2548 return error;
2549
2550 bad:
2551 if (inserted) {
2552 mutex_enter(sc->sc_lock);
2553 mutex_enter(sc->sc_intr_lock);
2554 SLIST_REMOVE(&sc->sc_files, af, audio_file, entry);
2555 mutex_exit(sc->sc_intr_lock);
2556 if (af->ptrack)
2557 sc->sc_popens--;
2558 if (af->rtrack)
2559 sc->sc_ropens--;
2560 mutex_exit(sc->sc_lock);
2561 }
2562
2563 if (rmixer_started) {
2564 mutex_enter(sc->sc_lock);
2565 audio_rmixer_halt(sc);
2566 mutex_exit(sc->sc_lock);
2567 }
2568
2569 if (hw_opened) {
2570 if (sc->hw_if->close) {
2571 mutex_enter(sc->sc_lock);
2572 mutex_enter(sc->sc_intr_lock);
2573 sc->hw_if->close(sc->hw_hdl);
2574 mutex_exit(sc->sc_intr_lock);
2575 mutex_exit(sc->sc_lock);
2576 }
2577 }
2578 if (cred_held) {
2579 kauth_cred_free(sc->sc_cred);
2580 }
2581
2582 /*
2583 * Since track here is not yet linked to sc_files,
2584 * you can call track_destroy() without sc_intr_lock.
2585 */
2586 if (af->rtrack) {
2587 audio_track_destroy(af->rtrack);
2588 af->rtrack = NULL;
2589 }
2590 if (af->ptrack) {
2591 audio_track_destroy(af->ptrack);
2592 af->ptrack = NULL;
2593 }
2594
2595 kmem_free(af, sizeof(*af));
2596 return error;
2597 }
2598
2599 /*
2600 * Must be called without sc_lock nor sc_exlock held.
2601 */
2602 int
2603 audio_close(struct audio_softc *sc, audio_file_t *file)
2604 {
2605 int error;
2606
2607 /*
2608 * Drain first.
2609 * It must be done before unlinking(acquiring exlock).
2610 */
2611 if (file->ptrack) {
2612 mutex_enter(sc->sc_lock);
2613 audio_track_drain(sc, file->ptrack);
2614 mutex_exit(sc->sc_lock);
2615 }
2616
2617 mutex_enter(sc->sc_lock);
2618 mutex_enter(sc->sc_intr_lock);
2619 SLIST_REMOVE(&sc->sc_files, file, audio_file, entry);
2620 mutex_exit(sc->sc_intr_lock);
2621 mutex_exit(sc->sc_lock);
2622
2623 error = audio_exlock_enter(sc);
2624 if (error) {
2625 /*
2626 * If EIO, this sc is about to detach. In this case, even if
2627 * we don't do subsequent _unlink(), audiodetach() will do it.
2628 */
2629 if (error == EIO)
2630 return error;
2631
2632 /* XXX This should not happen but what should I do ? */
2633 panic("%s: can't acquire exlock: errno=%d", __func__, error);
2634 }
2635 audio_unlink(sc, file);
2636 audio_exlock_exit(sc);
2637
2638 return 0;
2639 }
2640
2641 /*
2642 * Unlink this file, but not freeing memory here.
2643 * Must be called with sc_exlock held and without sc_lock held.
2644 */
2645 static void
2646 audio_unlink(struct audio_softc *sc, audio_file_t *file)
2647 {
2648 kauth_cred_t cred = NULL;
2649 int error;
2650
2651 mutex_enter(sc->sc_lock);
2652
2653 TRACEF(1, file, "%spid=%d.%d po=%d ro=%d",
2654 (audiodebug >= 3) ? "start " : "",
2655 (int)curproc->p_pid, (int)curlwp->l_lid,
2656 sc->sc_popens, sc->sc_ropens);
2657 KASSERTMSG(sc->sc_popens + sc->sc_ropens > 0,
2658 "sc->sc_popens=%d, sc->sc_ropens=%d",
2659 sc->sc_popens, sc->sc_ropens);
2660
2661 device_active(sc->sc_dev, DVA_SYSTEM);
2662
2663 if (file->ptrack) {
2664 TRACET(3, file->ptrack, "dropframes=%" PRIu64,
2665 file->ptrack->dropframes);
2666
2667 KASSERT(sc->sc_popens > 0);
2668 sc->sc_popens--;
2669
2670 /* Call hw halt_output if this is the last playback track. */
2671 if (sc->sc_popens == 0 && sc->sc_pbusy) {
2672 error = audio_pmixer_halt(sc);
2673 if (error) {
2674 audio_printf(sc,
2675 "halt_output failed: errno=%d (ignored)\n",
2676 error);
2677 }
2678 }
2679
2680 /* Restore mixing volume if all tracks are gone. */
2681 if (sc->sc_popens == 0) {
2682 /* intr_lock is not necessary, but just manners. */
2683 mutex_enter(sc->sc_intr_lock);
2684 sc->sc_pmixer->volume = 256;
2685 sc->sc_pmixer->voltimer = 0;
2686 mutex_exit(sc->sc_intr_lock);
2687 }
2688 }
2689 if (file->rtrack) {
2690 TRACET(3, file->rtrack, "dropframes=%" PRIu64,
2691 file->rtrack->dropframes);
2692
2693 KASSERT(sc->sc_ropens > 0);
2694 sc->sc_ropens--;
2695
2696 /* Call hw halt_input if this is the last recording track. */
2697 if (sc->sc_ropens == 0 && sc->sc_rbusy) {
2698 error = audio_rmixer_halt(sc);
2699 if (error) {
2700 audio_printf(sc,
2701 "halt_input failed: errno=%d (ignored)\n",
2702 error);
2703 }
2704 }
2705
2706 }
2707
2708 /* Call hw close if this is the last track. */
2709 if (sc->sc_popens + sc->sc_ropens == 0) {
2710 if (sc->hw_if->close) {
2711 TRACE(2, "hw_if close");
2712 mutex_enter(sc->sc_intr_lock);
2713 sc->hw_if->close(sc->hw_hdl);
2714 mutex_exit(sc->sc_intr_lock);
2715 }
2716 cred = sc->sc_cred;
2717 sc->sc_cred = NULL;
2718 }
2719
2720 mutex_exit(sc->sc_lock);
2721 if (cred)
2722 kauth_cred_free(cred);
2723
2724 TRACE(3, "done");
2725 }
2726
2727 /*
2728 * Must be called without sc_lock nor sc_exlock held.
2729 */
2730 int
2731 audio_read(struct audio_softc *sc, struct uio *uio, int ioflag,
2732 audio_file_t *file)
2733 {
2734 audio_track_t *track;
2735 audio_ring_t *usrbuf;
2736 audio_ring_t *input;
2737 int error;
2738
2739 /*
2740 * On half-duplex hardware, O_RDWR is treated as O_WRONLY.
2741 * However read() system call itself can be called because it's
2742 * opened with O_RDWR. So in this case, deny this read().
2743 */
2744 track = file->rtrack;
2745 if (track == NULL) {
2746 return EBADF;
2747 }
2748
2749 /* I think it's better than EINVAL. */
2750 if (track->mmapped)
2751 return EPERM;
2752
2753 TRACET(2, track, "resid=%zd ioflag=0x%x", uio->uio_resid, ioflag);
2754
2755 #ifdef AUDIO_PM_IDLE
2756 error = audio_exlock_mutex_enter(sc);
2757 if (error)
2758 return error;
2759
2760 if (device_is_active(&sc->sc_dev) || sc->sc_idle)
2761 device_active(&sc->sc_dev, DVA_SYSTEM);
2762
2763 /* In recording, unlike playback, read() never operates rmixer. */
2764
2765 audio_exlock_mutex_exit(sc);
2766 #endif
2767
2768 usrbuf = &track->usrbuf;
2769 input = track->input;
2770 error = 0;
2771
2772 while (uio->uio_resid > 0 && error == 0) {
2773 int bytes;
2774
2775 TRACET(3, track,
2776 "while resid=%zd input=%d/%d/%d usrbuf=%d/%d/H%d",
2777 uio->uio_resid,
2778 input->head, input->used, input->capacity,
2779 usrbuf->head, usrbuf->used, track->usrbuf_usedhigh);
2780
2781 /* Wait when buffers are empty. */
2782 mutex_enter(sc->sc_lock);
2783 for (;;) {
2784 bool empty;
2785 audio_track_lock_enter(track);
2786 empty = (input->used == 0 && usrbuf->used == 0);
2787 audio_track_lock_exit(track);
2788 if (!empty)
2789 break;
2790
2791 if ((ioflag & IO_NDELAY)) {
2792 mutex_exit(sc->sc_lock);
2793 return EWOULDBLOCK;
2794 }
2795
2796 TRACET(3, track, "sleep");
2797 error = audio_track_waitio(sc, track);
2798 if (error) {
2799 mutex_exit(sc->sc_lock);
2800 return error;
2801 }
2802 }
2803 mutex_exit(sc->sc_lock);
2804
2805 audio_track_lock_enter(track);
2806 /* Convert as many blocks as possible. */
2807 while (usrbuf->used <=
2808 track->usrbuf_usedhigh - track->usrbuf_blksize &&
2809 input->used > 0) {
2810 audio_track_record(track);
2811 }
2812
2813 /* uiomove from usrbuf as many bytes as possible. */
2814 bytes = uimin(usrbuf->used, uio->uio_resid);
2815 while (bytes > 0) {
2816 int head = usrbuf->head;
2817 int len = uimin(bytes, usrbuf->capacity - head);
2818 error = uiomove((uint8_t *)usrbuf->mem + head, len,
2819 uio);
2820 if (error) {
2821 audio_track_lock_exit(track);
2822 device_printf(sc->sc_dev,
2823 "%s: uiomove(%d) failed: errno=%d\n",
2824 __func__, len, error);
2825 goto abort;
2826 }
2827 auring_take(usrbuf, len);
2828 track->useriobytes += len;
2829 TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
2830 len,
2831 usrbuf->head, usrbuf->used, usrbuf->capacity);
2832 bytes -= len;
2833 }
2834
2835 audio_track_lock_exit(track);
2836 }
2837
2838 abort:
2839 return error;
2840 }
2841
2842
2843 /*
2844 * Clear file's playback and/or record track buffer immediately.
2845 */
2846 static void
2847 audio_file_clear(struct audio_softc *sc, audio_file_t *file)
2848 {
2849
2850 if (file->ptrack)
2851 audio_track_clear(sc, file->ptrack);
2852 if (file->rtrack)
2853 audio_track_clear(sc, file->rtrack);
2854 }
2855
2856 /*
2857 * Must be called without sc_lock nor sc_exlock held.
2858 */
2859 int
2860 audio_write(struct audio_softc *sc, struct uio *uio, int ioflag,
2861 audio_file_t *file)
2862 {
2863 audio_track_t *track;
2864 audio_ring_t *usrbuf;
2865 audio_ring_t *outbuf;
2866 int error;
2867
2868 track = file->ptrack;
2869 if (track == NULL)
2870 return EPERM;
2871
2872 /* I think it's better than EINVAL. */
2873 if (track->mmapped)
2874 return EPERM;
2875
2876 TRACET(2, track, "%sresid=%zd pid=%d.%d ioflag=0x%x",
2877 audiodebug >= 3 ? "begin " : "",
2878 uio->uio_resid, (int)curproc->p_pid, (int)curlwp->l_lid, ioflag);
2879
2880 if (uio->uio_resid == 0) {
2881 track->eofcounter++;
2882 return 0;
2883 }
2884
2885 error = audio_exlock_mutex_enter(sc);
2886 if (error)
2887 return error;
2888
2889 #ifdef AUDIO_PM_IDLE
2890 if (device_is_active(&sc->sc_dev) || sc->sc_idle)
2891 device_active(&sc->sc_dev, DVA_SYSTEM);
2892 #endif
2893
2894 /*
2895 * The first write starts pmixer.
2896 */
2897 if (sc->sc_pbusy == false)
2898 audio_pmixer_start(sc, false);
2899 audio_exlock_mutex_exit(sc);
2900
2901 usrbuf = &track->usrbuf;
2902 outbuf = &track->outbuf;
2903 track->pstate = AUDIO_STATE_RUNNING;
2904 error = 0;
2905
2906 while (uio->uio_resid > 0 && error == 0) {
2907 int bytes;
2908
2909 TRACET(3, track, "while resid=%zd usrbuf=%d/%d/H%d",
2910 uio->uio_resid,
2911 usrbuf->head, usrbuf->used, track->usrbuf_usedhigh);
2912
2913 /* Wait when buffers are full. */
2914 mutex_enter(sc->sc_lock);
2915 for (;;) {
2916 bool full;
2917 audio_track_lock_enter(track);
2918 full = (usrbuf->used >= track->usrbuf_usedhigh &&
2919 outbuf->used >= outbuf->capacity);
2920 audio_track_lock_exit(track);
2921 if (!full)
2922 break;
2923
2924 if ((ioflag & IO_NDELAY)) {
2925 error = EWOULDBLOCK;
2926 mutex_exit(sc->sc_lock);
2927 goto abort;
2928 }
2929
2930 TRACET(3, track, "sleep usrbuf=%d/H%d",
2931 usrbuf->used, track->usrbuf_usedhigh);
2932 error = audio_track_waitio(sc, track);
2933 if (error) {
2934 mutex_exit(sc->sc_lock);
2935 goto abort;
2936 }
2937 }
2938 mutex_exit(sc->sc_lock);
2939
2940 audio_track_lock_enter(track);
2941
2942 /* uiomove to usrbuf as many bytes as possible. */
2943 bytes = uimin(track->usrbuf_usedhigh - usrbuf->used,
2944 uio->uio_resid);
2945 while (bytes > 0) {
2946 int tail = auring_tail(usrbuf);
2947 int len = uimin(bytes, usrbuf->capacity - tail);
2948 error = uiomove((uint8_t *)usrbuf->mem + tail, len,
2949 uio);
2950 if (error) {
2951 audio_track_lock_exit(track);
2952 device_printf(sc->sc_dev,
2953 "%s: uiomove(%d) failed: errno=%d\n",
2954 __func__, len, error);
2955 goto abort;
2956 }
2957 auring_push(usrbuf, len);
2958 track->useriobytes += len;
2959 TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
2960 len,
2961 usrbuf->head, usrbuf->used, usrbuf->capacity);
2962 bytes -= len;
2963 }
2964
2965 /* Convert them as many blocks as possible. */
2966 while (usrbuf->used >= track->usrbuf_blksize &&
2967 outbuf->used < outbuf->capacity) {
2968 audio_track_play(track);
2969 }
2970
2971 audio_track_lock_exit(track);
2972 }
2973
2974 abort:
2975 TRACET(3, track, "done error=%d", error);
2976 return error;
2977 }
2978
2979 /*
2980 * Must be called without sc_lock nor sc_exlock held.
2981 */
2982 int
2983 audio_ioctl(dev_t dev, struct audio_softc *sc, u_long cmd, void *addr, int flag,
2984 struct lwp *l, audio_file_t *file)
2985 {
2986 struct audio_offset *ao;
2987 struct audio_info ai;
2988 audio_track_t *track;
2989 audio_encoding_t *ae;
2990 audio_format_query_t *query;
2991 u_int stamp;
2992 u_int offs;
2993 int fd;
2994 int index;
2995 int error;
2996
2997 #if defined(AUDIO_DEBUG)
2998 const char *ioctlnames[] = {
2999 " AUDIO_GETINFO", /* 21 */
3000 " AUDIO_SETINFO", /* 22 */
3001 " AUDIO_DRAIN", /* 23 */
3002 " AUDIO_FLUSH", /* 24 */
3003 " AUDIO_WSEEK", /* 25 */
3004 " AUDIO_RERROR", /* 26 */
3005 " AUDIO_GETDEV", /* 27 */
3006 " AUDIO_GETENC", /* 28 */
3007 " AUDIO_GETFD", /* 29 */
3008 " AUDIO_SETFD", /* 30 */
3009 " AUDIO_PERROR", /* 31 */
3010 " AUDIO_GETIOFFS", /* 32 */
3011 " AUDIO_GETOOFFS", /* 33 */
3012 " AUDIO_GETPROPS", /* 34 */
3013 " AUDIO_GETBUFINFO", /* 35 */
3014 " AUDIO_SETCHAN", /* 36 */
3015 " AUDIO_GETCHAN", /* 37 */
3016 " AUDIO_QUERYFORMAT", /* 38 */
3017 " AUDIO_GETFORMAT", /* 39 */
3018 " AUDIO_SETFORMAT", /* 40 */
3019 };
3020 int nameidx = (cmd & 0xff);
3021 const char *ioctlname = "";
3022 if (21 <= nameidx && nameidx <= 21 + __arraycount(ioctlnames))
3023 ioctlname = ioctlnames[nameidx - 21];
3024 TRACEF(2, file, "(%lu,'%c',%lu)%s pid=%d.%d",
3025 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, ioctlname,
3026 (int)curproc->p_pid, (int)l->l_lid);
3027 #endif
3028
3029 error = 0;
3030 switch (cmd) {
3031 case FIONBIO:
3032 /* All handled in the upper FS layer. */
3033 break;
3034
3035 case FIONREAD:
3036 /* Get the number of bytes that can be read. */
3037 if (file->rtrack) {
3038 *(int *)addr = audio_track_readablebytes(file->rtrack);
3039 } else {
3040 *(int *)addr = 0;
3041 }
3042 break;
3043
3044 case FIOASYNC:
3045 /* Set/Clear ASYNC I/O. */
3046 if (*(int *)addr) {
3047 file->async_audio = curproc->p_pid;
3048 TRACEF(2, file, "FIOASYNC pid %d", file->async_audio);
3049 } else {
3050 file->async_audio = 0;
3051 TRACEF(2, file, "FIOASYNC off");
3052 }
3053 break;
3054
3055 case AUDIO_FLUSH:
3056 /* XXX TODO: clear errors and restart? */
3057 audio_file_clear(sc, file);
3058 break;
3059
3060 case AUDIO_RERROR:
3061 /*
3062 * Number of read bytes dropped. We don't know where
3063 * or when they were dropped (including conversion stage).
3064 * Therefore, the number of accurate bytes or samples is
3065 * also unknown.
3066 */
3067 track = file->rtrack;
3068 if (track) {
3069 *(int *)addr = frametobyte(&track->usrbuf.fmt,
3070 track->dropframes);
3071 }
3072 break;
3073
3074 case AUDIO_PERROR:
3075 /*
3076 * Number of write bytes dropped. We don't know where
3077 * or when they were dropped (including conversion stage).
3078 * Therefore, the number of accurate bytes or samples is
3079 * also unknown.
3080 */
3081 track = file->ptrack;
3082 if (track) {
3083 *(int *)addr = frametobyte(&track->usrbuf.fmt,
3084 track->dropframes);
3085 }
3086 break;
3087
3088 case AUDIO_GETIOFFS:
3089 /* XXX TODO */
3090 ao = (struct audio_offset *)addr;
3091 ao->samples = 0;
3092 ao->deltablks = 0;
3093 ao->offset = 0;
3094 break;
3095
3096 case AUDIO_GETOOFFS:
3097 ao = (struct audio_offset *)addr;
3098 track = file->ptrack;
3099 if (track == NULL) {
3100 ao->samples = 0;
3101 ao->deltablks = 0;
3102 ao->offset = 0;
3103 break;
3104 }
3105 mutex_enter(sc->sc_lock);
3106 mutex_enter(sc->sc_intr_lock);
3107 /* figure out where next DMA will start */
3108 stamp = track->usrbuf_stamp;
3109 offs = track->usrbuf.head;
3110 mutex_exit(sc->sc_intr_lock);
3111 mutex_exit(sc->sc_lock);
3112
3113 ao->samples = stamp;
3114 ao->deltablks = (stamp / track->usrbuf_blksize) -
3115 (track->usrbuf_stamp_last / track->usrbuf_blksize);
3116 track->usrbuf_stamp_last = stamp;
3117 offs = rounddown(offs, track->usrbuf_blksize)
3118 + track->usrbuf_blksize;
3119 if (offs >= track->usrbuf.capacity)
3120 offs -= track->usrbuf.capacity;
3121 ao->offset = offs;
3122
3123 TRACET(3, track, "GETOOFFS: samples=%u deltablks=%u offset=%u",
3124 ao->samples, ao->deltablks, ao->offset);
3125 break;
3126
3127 case AUDIO_WSEEK:
3128 /* XXX return value does not include outbuf one. */
3129 if (file->ptrack)
3130 *(u_long *)addr = file->ptrack->usrbuf.used;
3131 break;
3132
3133 case AUDIO_SETINFO:
3134 error = audio_exlock_enter(sc);
3135 if (error)
3136 break;
3137 error = audio_file_setinfo(sc, file, (struct audio_info *)addr);
3138 if (error) {
3139 audio_exlock_exit(sc);
3140 break;
3141 }
3142 /* XXX TODO: update last_ai if /dev/sound ? */
3143 if (ISDEVSOUND(dev))
3144 error = audiogetinfo(sc, &sc->sc_ai, 0, file);
3145 audio_exlock_exit(sc);
3146 break;
3147
3148 case AUDIO_GETINFO:
3149 error = audio_exlock_enter(sc);
3150 if (error)
3151 break;
3152 error = audiogetinfo(sc, (struct audio_info *)addr, 1, file);
3153 audio_exlock_exit(sc);
3154 break;
3155
3156 case AUDIO_GETBUFINFO:
3157 error = audio_exlock_enter(sc);
3158 if (error)
3159 break;
3160 error = audiogetinfo(sc, (struct audio_info *)addr, 0, file);
3161 audio_exlock_exit(sc);
3162 break;
3163
3164 case AUDIO_DRAIN:
3165 if (file->ptrack) {
3166 mutex_enter(sc->sc_lock);
3167 error = audio_track_drain(sc, file->ptrack);
3168 mutex_exit(sc->sc_lock);
3169 }
3170 break;
3171
3172 case AUDIO_GETDEV:
3173 error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
3174 break;
3175
3176 case AUDIO_GETENC:
3177 ae = (audio_encoding_t *)addr;
3178 index = ae->index;
3179 if (index < 0 || index >= __arraycount(audio_encodings)) {
3180 error = EINVAL;
3181 break;
3182 }
3183 *ae = audio_encodings[index];
3184 ae->index = index;
3185 /*
3186 * EMULATED always.
3187 * EMULATED flag at that time used to mean that it could
3188 * not be passed directly to the hardware as-is. But
3189 * currently, all formats including hardware native is not
3190 * passed directly to the hardware. So I set EMULATED
3191 * flag for all formats.
3192 */
3193 ae->flags = AUDIO_ENCODINGFLAG_EMULATED;
3194 break;
3195
3196 case AUDIO_GETFD:
3197 /*
3198 * Returns the current setting of full duplex mode.
3199 * If HW has full duplex mode and there are two mixers,
3200 * it is full duplex. Otherwise half duplex.
3201 */
3202 error = audio_exlock_enter(sc);
3203 if (error)
3204 break;
3205 fd = (sc->sc_props & AUDIO_PROP_FULLDUPLEX)
3206 && (sc->sc_pmixer && sc->sc_rmixer);
3207 audio_exlock_exit(sc);
3208 *(int *)addr = fd;
3209 break;
3210
3211 case AUDIO_GETPROPS:
3212 *(int *)addr = sc->sc_props;
3213 break;
3214
3215 case AUDIO_QUERYFORMAT:
3216 query = (audio_format_query_t *)addr;
3217 mutex_enter(sc->sc_lock);
3218 error = sc->hw_if->query_format(sc->hw_hdl, query);
3219 mutex_exit(sc->sc_lock);
3220 /* Hide internal information */
3221 query->fmt.driver_data = NULL;
3222 break;
3223
3224 case AUDIO_GETFORMAT:
3225 error = audio_exlock_enter(sc);
3226 if (error)
3227 break;
3228 audio_mixers_get_format(sc, (struct audio_info *)addr);
3229 audio_exlock_exit(sc);
3230 break;
3231
3232 case AUDIO_SETFORMAT:
3233 error = audio_exlock_enter(sc);
3234 audio_mixers_get_format(sc, &ai);
3235 error = audio_mixers_set_format(sc, (struct audio_info *)addr);
3236 if (error) {
3237 /* Rollback */
3238 audio_mixers_set_format(sc, &ai);
3239 }
3240 audio_exlock_exit(sc);
3241 break;
3242
3243 case AUDIO_SETFD:
3244 case AUDIO_SETCHAN:
3245 case AUDIO_GETCHAN:
3246 /* Obsoleted */
3247 break;
3248
3249 default:
3250 if (sc->hw_if->dev_ioctl) {
3251 mutex_enter(sc->sc_lock);
3252 error = sc->hw_if->dev_ioctl(sc->hw_hdl,
3253 cmd, addr, flag, l);
3254 mutex_exit(sc->sc_lock);
3255 } else {
3256 TRACEF(2, file, "unknown ioctl");
3257 error = EINVAL;
3258 }
3259 break;
3260 }
3261 TRACEF(2, file, "(%lu,'%c',%lu)%s result %d",
3262 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, ioctlname,
3263 error);
3264 return error;
3265 }
3266
3267 /*
3268 * Returns the number of bytes that can be read on recording buffer.
3269 */
3270 static __inline int
3271 audio_track_readablebytes(const audio_track_t *track)
3272 {
3273 int bytes;
3274
3275 KASSERT(track);
3276 KASSERT(track->mode == AUMODE_RECORD);
3277
3278 /*
3279 * Although usrbuf is primarily readable data, recorded data
3280 * also stays in track->input until reading. So it is necessary
3281 * to add it. track->input is in frame, usrbuf is in byte.
3282 */
3283 bytes = track->usrbuf.used +
3284 track->input->used * frametobyte(&track->usrbuf.fmt, 1);
3285 return bytes;
3286 }
3287
3288 /*
3289 * Must be called without sc_lock nor sc_exlock held.
3290 */
3291 int
3292 audio_poll(struct audio_softc *sc, int events, struct lwp *l,
3293 audio_file_t *file)
3294 {
3295 audio_track_t *track;
3296 int revents;
3297 bool in_is_valid;
3298 bool out_is_valid;
3299
3300 #if defined(AUDIO_DEBUG)
3301 #define POLLEV_BITMAP "\177\020" \
3302 "b\10WRBAND\0" \
3303 "b\7RDBAND\0" "b\6RDNORM\0" "b\5NVAL\0" "b\4HUP\0" \
3304 "b\3ERR\0" "b\2OUT\0" "b\1PRI\0" "b\0IN\0"
3305 char evbuf[64];
3306 snprintb(evbuf, sizeof(evbuf), POLLEV_BITMAP, events);
3307 TRACEF(2, file, "pid=%d.%d events=%s",
3308 (int)curproc->p_pid, (int)l->l_lid, evbuf);
3309 #endif
3310
3311 revents = 0;
3312 in_is_valid = false;
3313 out_is_valid = false;
3314 if (events & (POLLIN | POLLRDNORM)) {
3315 track = file->rtrack;
3316 if (track) {
3317 int used;
3318 in_is_valid = true;
3319 used = audio_track_readablebytes(track);
3320 if (used > 0)
3321 revents |= events & (POLLIN | POLLRDNORM);
3322 }
3323 }
3324 if (events & (POLLOUT | POLLWRNORM)) {
3325 track = file->ptrack;
3326 if (track) {
3327 out_is_valid = true;
3328 if (track->usrbuf.used <= track->usrbuf_usedlow)
3329 revents |= events & (POLLOUT | POLLWRNORM);
3330 }
3331 }
3332
3333 if (revents == 0) {
3334 mutex_enter(sc->sc_lock);
3335 if (in_is_valid) {
3336 TRACEF(3, file, "selrecord rsel");
3337 selrecord(l, &sc->sc_rsel);
3338 }
3339 if (out_is_valid) {
3340 TRACEF(3, file, "selrecord wsel");
3341 selrecord(l, &sc->sc_wsel);
3342 }
3343 mutex_exit(sc->sc_lock);
3344 }
3345
3346 #if defined(AUDIO_DEBUG)
3347 snprintb(evbuf, sizeof(evbuf), POLLEV_BITMAP, revents);
3348 TRACEF(2, file, "revents=%s", evbuf);
3349 #endif
3350 return revents;
3351 }
3352
3353 static const struct filterops audioread_filtops = {
3354 .f_flags = FILTEROP_ISFD,
3355 .f_attach = NULL,
3356 .f_detach = filt_audioread_detach,
3357 .f_event = filt_audioread_event,
3358 };
3359
3360 static void
3361 filt_audioread_detach(struct knote *kn)
3362 {
3363 struct audio_softc *sc;
3364 audio_file_t *file;
3365
3366 file = kn->kn_hook;
3367 sc = file->sc;
3368 TRACEF(3, file, "called");
3369
3370 mutex_enter(sc->sc_lock);
3371 selremove_knote(&sc->sc_rsel, kn);
3372 mutex_exit(sc->sc_lock);
3373 }
3374
3375 static int
3376 filt_audioread_event(struct knote *kn, long hint)
3377 {
3378 audio_file_t *file;
3379 audio_track_t *track;
3380
3381 file = kn->kn_hook;
3382 track = file->rtrack;
3383
3384 /*
3385 * kn_data must contain the number of bytes can be read.
3386 * The return value indicates whether the event occurs or not.
3387 */
3388
3389 if (track == NULL) {
3390 /* can not read with this descriptor. */
3391 kn->kn_data = 0;
3392 return 0;
3393 }
3394
3395 kn->kn_data = audio_track_readablebytes(track);
3396 TRACEF(3, file, "data=%" PRId64, kn->kn_data);
3397 return kn->kn_data > 0;
3398 }
3399
3400 static const struct filterops audiowrite_filtops = {
3401 .f_flags = FILTEROP_ISFD,
3402 .f_attach = NULL,
3403 .f_detach = filt_audiowrite_detach,
3404 .f_event = filt_audiowrite_event,
3405 };
3406
3407 static void
3408 filt_audiowrite_detach(struct knote *kn)
3409 {
3410 struct audio_softc *sc;
3411 audio_file_t *file;
3412
3413 file = kn->kn_hook;
3414 sc = file->sc;
3415 TRACEF(3, file, "called");
3416
3417 mutex_enter(sc->sc_lock);
3418 selremove_knote(&sc->sc_wsel, kn);
3419 mutex_exit(sc->sc_lock);
3420 }
3421
3422 static int
3423 filt_audiowrite_event(struct knote *kn, long hint)
3424 {
3425 audio_file_t *file;
3426 audio_track_t *track;
3427
3428 file = kn->kn_hook;
3429 track = file->ptrack;
3430
3431 /*
3432 * kn_data must contain the number of bytes can be write.
3433 * The return value indicates whether the event occurs or not.
3434 */
3435
3436 if (track == NULL) {
3437 /* can not write with this descriptor. */
3438 kn->kn_data = 0;
3439 return 0;
3440 }
3441
3442 kn->kn_data = track->usrbuf_usedhigh - track->usrbuf.used;
3443 TRACEF(3, file, "data=%" PRId64, kn->kn_data);
3444 return (track->usrbuf.used < track->usrbuf_usedlow);
3445 }
3446
3447 /*
3448 * Must be called without sc_lock nor sc_exlock held.
3449 */
3450 int
3451 audio_kqfilter(struct audio_softc *sc, audio_file_t *file, struct knote *kn)
3452 {
3453 struct selinfo *sip;
3454
3455 TRACEF(3, file, "kn=%p kn_filter=%x", kn, (int)kn->kn_filter);
3456
3457 switch (kn->kn_filter) {
3458 case EVFILT_READ:
3459 sip = &sc->sc_rsel;
3460 kn->kn_fop = &audioread_filtops;
3461 break;
3462
3463 case EVFILT_WRITE:
3464 sip = &sc->sc_wsel;
3465 kn->kn_fop = &audiowrite_filtops;
3466 break;
3467
3468 default:
3469 return EINVAL;
3470 }
3471
3472 kn->kn_hook = file;
3473
3474 mutex_enter(sc->sc_lock);
3475 selrecord_knote(sip, kn);
3476 mutex_exit(sc->sc_lock);
3477
3478 return 0;
3479 }
3480
3481 /*
3482 * Must be called without sc_lock nor sc_exlock held.
3483 */
3484 int
3485 audio_mmap(struct audio_softc *sc, off_t *offp, size_t len, int prot,
3486 int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp,
3487 audio_file_t *file)
3488 {
3489 audio_track_t *track;
3490 vsize_t vsize;
3491 int error;
3492
3493 TRACEF(2, file, "off=%lld, prot=%d", (long long)(*offp), prot);
3494
3495 if (*offp < 0)
3496 return EINVAL;
3497
3498 #if 0
3499 /* XXX
3500 * The idea here was to use the protection to determine if
3501 * we are mapping the read or write buffer, but it fails.
3502 * The VM system is broken in (at least) two ways.
3503 * 1) If you map memory VM_PROT_WRITE you SIGSEGV
3504 * when writing to it, so VM_PROT_READ|VM_PROT_WRITE
3505 * has to be used for mmapping the play buffer.
3506 * 2) Even if calling mmap() with VM_PROT_READ|VM_PROT_WRITE
3507 * audio_mmap will get called at some point with VM_PROT_READ
3508 * only.
3509 * So, alas, we always map the play buffer for now.
3510 */
3511 if (prot == (VM_PROT_READ|VM_PROT_WRITE) ||
3512 prot == VM_PROT_WRITE)
3513 track = file->ptrack;
3514 else if (prot == VM_PROT_READ)
3515 track = file->rtrack;
3516 else
3517 return EINVAL;
3518 #else
3519 track = file->ptrack;
3520 #endif
3521 if (track == NULL)
3522 return EACCES;
3523
3524 vsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE), PAGE_SIZE);
3525 if (len > vsize)
3526 return EOVERFLOW;
3527 if (*offp > (uint)(vsize - len))
3528 return EOVERFLOW;
3529
3530 /* XXX TODO: what happens when mmap twice. */
3531 if (!track->mmapped) {
3532 track->mmapped = true;
3533
3534 if (!track->is_pause) {
3535 error = audio_exlock_mutex_enter(sc);
3536 if (error)
3537 return error;
3538 if (sc->sc_pbusy == false)
3539 audio_pmixer_start(sc, true);
3540 audio_exlock_mutex_exit(sc);
3541 }
3542 /* XXX mmapping record buffer is not supported */
3543 }
3544
3545 /* get ringbuffer */
3546 *uobjp = track->uobj;
3547
3548 /* Acquire a reference for the mmap. munmap will release. */
3549 uao_reference(*uobjp);
3550 *maxprotp = prot;
3551 *advicep = UVM_ADV_RANDOM;
3552 *flagsp = MAP_SHARED;
3553 return 0;
3554 }
3555
3556 /*
3557 * /dev/audioctl has to be able to open at any time without interference
3558 * with any /dev/audio or /dev/sound.
3559 * Must be called with sc_exlock held and without sc_lock held.
3560 */
3561 static int
3562 audioctl_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
3563 struct lwp *l)
3564 {
3565 struct file *fp;
3566 audio_file_t *af;
3567 int fd;
3568 int error;
3569
3570 KASSERT(sc->sc_exlock);
3571
3572 TRACE(1, "called");
3573
3574 error = fd_allocfile(&fp, &fd);
3575 if (error)
3576 return error;
3577
3578 af = kmem_zalloc(sizeof(*af), KM_SLEEP);
3579 af->sc = sc;
3580 af->dev = dev;
3581
3582 mutex_enter(sc->sc_lock);
3583 if (sc->sc_dying) {
3584 mutex_exit(sc->sc_lock);
3585 kmem_free(af, sizeof(*af));
3586 fd_abort(curproc, fp, fd);
3587 return ENXIO;
3588 }
3589 mutex_enter(sc->sc_intr_lock);
3590 SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
3591 mutex_exit(sc->sc_intr_lock);
3592 mutex_exit(sc->sc_lock);
3593
3594 error = fd_clone(fp, fd, flags, &audio_fileops, af);
3595 KASSERTMSG(error == EMOVEFD, "error=%d", error);
3596
3597 return error;
3598 }
3599
3600 /*
3601 * Free 'mem' if available, and initialize the pointer.
3602 * For this reason, this is implemented as macro.
3603 */
3604 #define audio_free(mem) do { \
3605 if (mem != NULL) { \
3606 kern_free(mem); \
3607 mem = NULL; \
3608 } \
3609 } while (0)
3610
3611 /*
3612 * (Re)allocate 'memblock' with specified 'bytes'.
3613 * bytes must not be 0.
3614 * This function never returns NULL.
3615 */
3616 static void *
3617 audio_realloc(void *memblock, size_t bytes)
3618 {
3619
3620 KASSERT(bytes != 0);
3621 audio_free(memblock);
3622 return kern_malloc(bytes, M_WAITOK);
3623 }
3624
3625 /*
3626 * (Re)allocate usrbuf with 'newbufsize' bytes.
3627 * Use this function for usrbuf because only usrbuf can be mmapped.
3628 * If successful, it updates track->usrbuf.mem, track->usrbuf.capacity and
3629 * returns 0. Otherwise, it clears track->usrbuf.mem, track->usrbuf.capacity
3630 * and returns errno.
3631 * It must be called before updating usrbuf.capacity.
3632 */
3633 static int
3634 audio_realloc_usrbuf(audio_track_t *track, int newbufsize)
3635 {
3636 struct audio_softc *sc;
3637 vaddr_t vstart;
3638 vsize_t oldvsize;
3639 vsize_t newvsize;
3640 int error;
3641
3642 KASSERT(newbufsize > 0);
3643 sc = track->mixer->sc;
3644
3645 /* Get a nonzero multiple of PAGE_SIZE */
3646 newvsize = roundup2(MAX(newbufsize, PAGE_SIZE), PAGE_SIZE);
3647
3648 if (track->usrbuf.mem != NULL) {
3649 oldvsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE),
3650 PAGE_SIZE);
3651 if (oldvsize == newvsize) {
3652 track->usrbuf.capacity = newbufsize;
3653 return 0;
3654 }
3655 vstart = (vaddr_t)track->usrbuf.mem;
3656 uvm_unmap(kernel_map, vstart, vstart + oldvsize);
3657 /* uvm_unmap also detach uobj */
3658 track->uobj = NULL; /* paranoia */
3659 track->usrbuf.mem = NULL;
3660 }
3661
3662 /* Create a uvm anonymous object */
3663 track->uobj = uao_create(newvsize, 0);
3664
3665 /* Map it into the kernel virtual address space */
3666 vstart = 0;
3667 error = uvm_map(kernel_map, &vstart, newvsize, track->uobj, 0, 0,
3668 UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
3669 UVM_ADV_RANDOM, 0));
3670 if (error) {
3671 device_printf(sc->sc_dev, "uvm_map failed: errno=%d\n", error);
3672 uao_detach(track->uobj); /* release reference */
3673 goto abort;
3674 }
3675
3676 error = uvm_map_pageable(kernel_map, vstart, vstart + newvsize,
3677 false, 0);
3678 if (error) {
3679 device_printf(sc->sc_dev, "uvm_map_pageable failed: errno=%d\n",
3680 error);
3681 uvm_unmap(kernel_map, vstart, vstart + newvsize);
3682 /* uvm_unmap also detach uobj */
3683 goto abort;
3684 }
3685
3686 track->usrbuf.mem = (void *)vstart;
3687 track->usrbuf.capacity = newbufsize;
3688 memset(track->usrbuf.mem, 0, newvsize);
3689 return 0;
3690
3691 /* failure */
3692 abort:
3693 track->uobj = NULL; /* paranoia */
3694 track->usrbuf.mem = NULL;
3695 track->usrbuf.capacity = 0;
3696 return error;
3697 }
3698
3699 /*
3700 * Free usrbuf (if available).
3701 */
3702 static void
3703 audio_free_usrbuf(audio_track_t *track)
3704 {
3705 vaddr_t vstart;
3706 vsize_t vsize;
3707
3708 vstart = (vaddr_t)track->usrbuf.mem;
3709 vsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE), PAGE_SIZE);
3710 if (track->usrbuf.mem != NULL) {
3711 /*
3712 * Unmap the kernel mapping. uvm_unmap releases the
3713 * reference to the uvm object, and this should be the
3714 * last virtual mapping of the uvm object, so no need
3715 * to explicitly release (`detach') the object.
3716 */
3717 uvm_unmap(kernel_map, vstart, vstart + vsize);
3718
3719 track->uobj = NULL;
3720 track->usrbuf.mem = NULL;
3721 track->usrbuf.capacity = 0;
3722 }
3723 }
3724
3725 /*
3726 * This filter changes the volume for each channel.
3727 * arg->context points track->ch_volume[].
3728 */
3729 static void
3730 audio_track_chvol(audio_filter_arg_t *arg)
3731 {
3732 int16_t *ch_volume;
3733 const aint_t *s;
3734 aint_t *d;
3735 u_int i;
3736 u_int ch;
3737 u_int channels;
3738
3739 DIAGNOSTIC_filter_arg(arg);
3740 KASSERTMSG(arg->srcfmt->channels == arg->dstfmt->channels,
3741 "arg->srcfmt->channels=%d, arg->dstfmt->channels=%d",
3742 arg->srcfmt->channels, arg->dstfmt->channels);
3743 KASSERT(arg->context != NULL);
3744 KASSERTMSG(arg->srcfmt->channels <= AUDIO_MAX_CHANNELS,
3745 "arg->srcfmt->channels=%d", arg->srcfmt->channels);
3746
3747 s = arg->src;
3748 d = arg->dst;
3749 ch_volume = arg->context;
3750
3751 channels = arg->srcfmt->channels;
3752 for (i = 0; i < arg->count; i++) {
3753 for (ch = 0; ch < channels; ch++) {
3754 aint2_t val;
3755 val = *s++;
3756 val = AUDIO_SCALEDOWN(val * ch_volume[ch], 8);
3757 *d++ = (aint_t)val;
3758 }
3759 }
3760 }
3761
3762 /*
3763 * This filter performs conversion from stereo (or more channels) to mono.
3764 */
3765 static void
3766 audio_track_chmix_mixLR(audio_filter_arg_t *arg)
3767 {
3768 const aint_t *s;
3769 aint_t *d;
3770 u_int i;
3771
3772 DIAGNOSTIC_filter_arg(arg);
3773
3774 s = arg->src;
3775 d = arg->dst;
3776
3777 for (i = 0; i < arg->count; i++) {
3778 *d++ = AUDIO_SCALEDOWN(s[0], 1) + AUDIO_SCALEDOWN(s[1], 1);
3779 s += arg->srcfmt->channels;
3780 }
3781 }
3782
3783 /*
3784 * This filter performs conversion from mono to stereo (or more channels).
3785 */
3786 static void
3787 audio_track_chmix_dupLR(audio_filter_arg_t *arg)
3788 {
3789 const aint_t *s;
3790 aint_t *d;
3791 u_int i;
3792 u_int ch;
3793 u_int dstchannels;
3794
3795 DIAGNOSTIC_filter_arg(arg);
3796
3797 s = arg->src;
3798 d = arg->dst;
3799 dstchannels = arg->dstfmt->channels;
3800
3801 for (i = 0; i < arg->count; i++) {
3802 d[0] = s[0];
3803 d[1] = s[0];
3804 s++;
3805 d += dstchannels;
3806 }
3807 if (dstchannels > 2) {
3808 d = arg->dst;
3809 for (i = 0; i < arg->count; i++) {
3810 for (ch = 2; ch < dstchannels; ch++) {
3811 d[ch] = 0;
3812 }
3813 d += dstchannels;
3814 }
3815 }
3816 }
3817
3818 /*
3819 * This filter shrinks M channels into N channels.
3820 * Extra channels are discarded.
3821 */
3822 static void
3823 audio_track_chmix_shrink(audio_filter_arg_t *arg)
3824 {
3825 const aint_t *s;
3826 aint_t *d;
3827 u_int i;
3828 u_int ch;
3829
3830 DIAGNOSTIC_filter_arg(arg);
3831
3832 s = arg->src;
3833 d = arg->dst;
3834
3835 for (i = 0; i < arg->count; i++) {
3836 for (ch = 0; ch < arg->dstfmt->channels; ch++) {
3837 *d++ = s[ch];
3838 }
3839 s += arg->srcfmt->channels;
3840 }
3841 }
3842
3843 /*
3844 * This filter expands M channels into N channels.
3845 * Silence is inserted for missing channels.
3846 */
3847 static void
3848 audio_track_chmix_expand(audio_filter_arg_t *arg)
3849 {
3850 const aint_t *s;
3851 aint_t *d;
3852 u_int i;
3853 u_int ch;
3854 u_int srcchannels;
3855 u_int dstchannels;
3856
3857 DIAGNOSTIC_filter_arg(arg);
3858
3859 s = arg->src;
3860 d = arg->dst;
3861
3862 srcchannels = arg->srcfmt->channels;
3863 dstchannels = arg->dstfmt->channels;
3864 for (i = 0; i < arg->count; i++) {
3865 for (ch = 0; ch < srcchannels; ch++) {
3866 *d++ = *s++;
3867 }
3868 for (; ch < dstchannels; ch++) {
3869 *d++ = 0;
3870 }
3871 }
3872 }
3873
3874 /*
3875 * This filter performs frequency conversion (up sampling).
3876 * It uses linear interpolation.
3877 */
3878 static void
3879 audio_track_freq_up(audio_filter_arg_t *arg)
3880 {
3881 audio_track_t *track;
3882 audio_ring_t *src;
3883 audio_ring_t *dst;
3884 const aint_t *s;
3885 aint_t *d;
3886 aint_t prev[AUDIO_MAX_CHANNELS];
3887 aint_t curr[AUDIO_MAX_CHANNELS];
3888 aint_t grad[AUDIO_MAX_CHANNELS];
3889 u_int i;
3890 u_int t;
3891 u_int step;
3892 u_int channels;
3893 u_int ch;
3894 int srcused;
3895
3896 track = arg->context;
3897 KASSERT(track);
3898 src = &track->freq.srcbuf;
3899 dst = track->freq.dst;
3900 DIAGNOSTIC_ring(dst);
3901 DIAGNOSTIC_ring(src);
3902 KASSERT(src->used > 0);
3903 KASSERTMSG(src->fmt.channels == dst->fmt.channels,
3904 "src->fmt.channels=%d dst->fmt.channels=%d",
3905 src->fmt.channels, dst->fmt.channels);
3906 KASSERTMSG(src->head % track->mixer->frames_per_block == 0,
3907 "src->head=%d track->mixer->frames_per_block=%d",
3908 src->head, track->mixer->frames_per_block);
3909
3910 s = arg->src;
3911 d = arg->dst;
3912
3913 /*
3914 * In order to facilitate interpolation for each block, slide (delay)
3915 * input by one sample. As a result, strictly speaking, the output
3916 * phase is delayed by 1/dstfreq. However, I believe there is no
3917 * observable impact.
3918 *
3919 * Example)
3920 * srcfreq:dstfreq = 1:3
3921 *
3922 * A - -
3923 * |
3924 * |
3925 * | B - -
3926 * +-----+-----> input timeframe
3927 * 0 1
3928 *
3929 * 0 1
3930 * +-----+-----> input timeframe
3931 * | A
3932 * | x x
3933 * | x x
3934 * x (B)
3935 * +-+-+-+-+-+-> output timeframe
3936 * 0 1 2 3 4 5
3937 */
3938
3939 /* Last samples in previous block */
3940 channels = src->fmt.channels;
3941 for (ch = 0; ch < channels; ch++) {
3942 prev[ch] = track->freq_prev[ch];
3943 curr[ch] = track->freq_curr[ch];
3944 grad[ch] = curr[ch] - prev[ch];
3945 }
3946
3947 step = track->freq_step;
3948 t = track->freq_current;
3949 //#define FREQ_DEBUG
3950 #if defined(FREQ_DEBUG)
3951 #define PRINTF(fmt...) printf(fmt)
3952 #else
3953 #define PRINTF(fmt...) do { } while (0)
3954 #endif
3955 srcused = src->used;
3956 PRINTF("upstart step=%d leap=%d", step, track->freq_leap);
3957 PRINTF(" srcused=%d arg->count=%u", src->used, arg->count);
3958 PRINTF(" prev=%d curr=%d grad=%d", prev[0], curr[0], grad[0]);
3959 PRINTF(" t=%d\n", t);
3960
3961 for (i = 0; i < arg->count; i++) {
3962 PRINTF("i=%d t=%5d", i, t);
3963 if (t >= 65536) {
3964 for (ch = 0; ch < channels; ch++) {
3965 prev[ch] = curr[ch];
3966 curr[ch] = *s++;
3967 grad[ch] = curr[ch] - prev[ch];
3968 }
3969 PRINTF(" prev=%d s[%d]=%d",
3970 prev[0], src->used - srcused, curr[0]);
3971
3972 /* Update */
3973 t -= 65536;
3974 srcused--;
3975 if (srcused < 0) {
3976 PRINTF(" break\n");
3977 break;
3978 }
3979 }
3980
3981 for (ch = 0; ch < channels; ch++) {
3982 *d++ = prev[ch] + (aint2_t)grad[ch] * t / 65536;
3983 #if defined(FREQ_DEBUG)
3984 if (ch == 0)
3985 printf(" t=%5d *d=%d", t, d[-1]);
3986 #endif
3987 }
3988 t += step;
3989
3990 PRINTF("\n");
3991 }
3992 PRINTF("end prev=%d curr=%d\n", prev[0], curr[0]);
3993
3994 auring_take(src, src->used);
3995 auring_push(dst, i);
3996
3997 /* Adjust */
3998 t += track->freq_leap;
3999
4000 track->freq_current = t;
4001 for (ch = 0; ch < channels; ch++) {
4002 track->freq_prev[ch] = prev[ch];
4003 track->freq_curr[ch] = curr[ch];
4004 }
4005 }
4006
4007 /*
4008 * This filter performs frequency conversion (down sampling).
4009 * It uses simple thinning.
4010 */
4011 static void
4012 audio_track_freq_down(audio_filter_arg_t *arg)
4013 {
4014 audio_track_t *track;
4015 audio_ring_t *src;
4016 audio_ring_t *dst;
4017 const aint_t *s0;
4018 aint_t *d;
4019 u_int i;
4020 u_int t;
4021 u_int step;
4022 u_int ch;
4023 u_int channels;
4024
4025 track = arg->context;
4026 KASSERT(track);
4027 src = &track->freq.srcbuf;
4028 dst = track->freq.dst;
4029
4030 DIAGNOSTIC_ring(dst);
4031 DIAGNOSTIC_ring(src);
4032 KASSERT(src->used > 0);
4033 KASSERTMSG(src->fmt.channels == dst->fmt.channels,
4034 "src->fmt.channels=%d dst->fmt.channels=%d",
4035 src->fmt.channels, dst->fmt.channels);
4036 KASSERTMSG(src->head % track->mixer->frames_per_block == 0,
4037 "src->head=%d track->mixer->frames_per_block=%d",
4038 src->head, track->mixer->frames_per_block);
4039
4040 s0 = arg->src;
4041 d = arg->dst;
4042 t = track->freq_current;
4043 step = track->freq_step;
4044 channels = dst->fmt.channels;
4045 PRINTF("downstart step=%d leap=%d", step, track->freq_leap);
4046 PRINTF(" srcused=%d arg->count=%u", src->used, arg->count);
4047 PRINTF(" t=%d\n", t);
4048
4049 for (i = 0; i < arg->count && t / 65536 < src->used; i++) {
4050 const aint_t *s;
4051 PRINTF("i=%4d t=%10d", i, t);
4052 s = s0 + (t / 65536) * channels;
4053 PRINTF(" s=%5ld", (s - s0) / channels);
4054 for (ch = 0; ch < channels; ch++) {
4055 if (ch == 0) PRINTF(" *s=%d", s[ch]);
4056 *d++ = s[ch];
4057 }
4058 PRINTF("\n");
4059 t += step;
4060 }
4061 t += track->freq_leap;
4062 PRINTF("end t=%d\n", t);
4063 auring_take(src, src->used);
4064 auring_push(dst, i);
4065 track->freq_current = t % 65536;
4066 }
4067
4068 /*
4069 * Creates track and returns it.
4070 * Must be called without sc_lock held.
4071 */
4072 audio_track_t *
4073 audio_track_create(struct audio_softc *sc, audio_trackmixer_t *mixer)
4074 {
4075 audio_track_t *track;
4076 static int newid = 0;
4077
4078 track = kmem_zalloc(sizeof(*track), KM_SLEEP);
4079
4080 track->id = newid++;
4081 track->mixer = mixer;
4082 track->mode = mixer->mode;
4083
4084 /* Do TRACE after id is assigned. */
4085 TRACET(3, track, "for %s",
4086 mixer->mode == AUMODE_PLAY ? "playback" : "recording");
4087
4088 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
4089 track->volume = 256;
4090 #endif
4091 for (int i = 0; i < AUDIO_MAX_CHANNELS; i++) {
4092 track->ch_volume[i] = 256;
4093 }
4094
4095 return track;
4096 }
4097
4098 /*
4099 * Release all resources of the track and track itself.
4100 * track must not be NULL. Don't specify the track within the file
4101 * structure linked from sc->sc_files.
4102 */
4103 static void
4104 audio_track_destroy(audio_track_t *track)
4105 {
4106
4107 KASSERT(track);
4108
4109 audio_free_usrbuf(track);
4110 audio_free(track->codec.srcbuf.mem);
4111 audio_free(track->chvol.srcbuf.mem);
4112 audio_free(track->chmix.srcbuf.mem);
4113 audio_free(track->freq.srcbuf.mem);
4114 audio_free(track->outbuf.mem);
4115
4116 kmem_free(track, sizeof(*track));
4117 }
4118
4119 /*
4120 * It returns encoding conversion filter according to src and dst format.
4121 * If it is not a convertible pair, it returns NULL. Either src or dst
4122 * must be internal format.
4123 */
4124 static audio_filter_t
4125 audio_track_get_codec(audio_track_t *track, const audio_format2_t *src,
4126 const audio_format2_t *dst)
4127 {
4128
4129 if (audio_format2_is_internal(src)) {
4130 if (dst->encoding == AUDIO_ENCODING_ULAW) {
4131 return audio_internal_to_mulaw;
4132 } else if (dst->encoding == AUDIO_ENCODING_ALAW) {
4133 return audio_internal_to_alaw;
4134 } else if (audio_format2_is_linear(dst)) {
4135 switch (dst->stride) {
4136 case 8:
4137 return audio_internal_to_linear8;
4138 case 16:
4139 return audio_internal_to_linear16;
4140 #if defined(AUDIO_SUPPORT_LINEAR24)
4141 case 24:
4142 return audio_internal_to_linear24;
4143 #endif
4144 case 32:
4145 return audio_internal_to_linear32;
4146 default:
4147 TRACET(1, track, "unsupported %s stride %d",
4148 "dst", dst->stride);
4149 goto abort;
4150 }
4151 }
4152 } else if (audio_format2_is_internal(dst)) {
4153 if (src->encoding == AUDIO_ENCODING_ULAW) {
4154 return audio_mulaw_to_internal;
4155 } else if (src->encoding == AUDIO_ENCODING_ALAW) {
4156 return audio_alaw_to_internal;
4157 } else if (audio_format2_is_linear(src)) {
4158 switch (src->stride) {
4159 case 8:
4160 return audio_linear8_to_internal;
4161 case 16:
4162 return audio_linear16_to_internal;
4163 #if defined(AUDIO_SUPPORT_LINEAR24)
4164 case 24:
4165 return audio_linear24_to_internal;
4166 #endif
4167 case 32:
4168 return audio_linear32_to_internal;
4169 default:
4170 TRACET(1, track, "unsupported %s stride %d",
4171 "src", src->stride);
4172 goto abort;
4173 }
4174 }
4175 }
4176
4177 TRACET(1, track, "unsupported encoding");
4178 abort:
4179 #if defined(AUDIO_DEBUG)
4180 if (audiodebug >= 2) {
4181 char buf[100];
4182 audio_format2_tostr(buf, sizeof(buf), src);
4183 TRACET(2, track, "src %s", buf);
4184 audio_format2_tostr(buf, sizeof(buf), dst);
4185 TRACET(2, track, "dst %s", buf);
4186 }
4187 #endif
4188 return NULL;
4189 }
4190
4191 /*
4192 * Initialize the codec stage of this track as necessary.
4193 * If successful, it initializes the codec stage as necessary, stores updated
4194 * last_dst in *last_dstp in any case, and returns 0.
4195 * Otherwise, it returns errno without modifying *last_dstp.
4196 */
4197 static int
4198 audio_track_init_codec(audio_track_t *track, audio_ring_t **last_dstp)
4199 {
4200 audio_ring_t *last_dst;
4201 audio_ring_t *srcbuf;
4202 audio_format2_t *srcfmt;
4203 audio_format2_t *dstfmt;
4204 audio_filter_arg_t *arg;
4205 u_int len;
4206 int error;
4207
4208 KASSERT(track);
4209
4210 last_dst = *last_dstp;
4211 dstfmt = &last_dst->fmt;
4212 srcfmt = &track->inputfmt;
4213 srcbuf = &track->codec.srcbuf;
4214 error = 0;
4215
4216 if (srcfmt->encoding != dstfmt->encoding
4217 || srcfmt->precision != dstfmt->precision
4218 || srcfmt->stride != dstfmt->stride) {
4219 track->codec.dst = last_dst;
4220
4221 srcbuf->fmt = *dstfmt;
4222 srcbuf->fmt.encoding = srcfmt->encoding;
4223 srcbuf->fmt.precision = srcfmt->precision;
4224 srcbuf->fmt.stride = srcfmt->stride;
4225
4226 track->codec.filter = audio_track_get_codec(track,
4227 &srcbuf->fmt, dstfmt);
4228 if (track->codec.filter == NULL) {
4229 error = EINVAL;
4230 goto abort;
4231 }
4232
4233 srcbuf->head = 0;
4234 srcbuf->used = 0;
4235 srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
4236 len = auring_bytelen(srcbuf);
4237 srcbuf->mem = audio_realloc(srcbuf->mem, len);
4238
4239 arg = &track->codec.arg;
4240 arg->srcfmt = &srcbuf->fmt;
4241 arg->dstfmt = dstfmt;
4242 arg->context = NULL;
4243
4244 *last_dstp = srcbuf;
4245 return 0;
4246 }
4247
4248 abort:
4249 track->codec.filter = NULL;
4250 audio_free(srcbuf->mem);
4251 return error;
4252 }
4253
4254 /*
4255 * Initialize the chvol stage of this track as necessary.
4256 * If successful, it initializes the chvol stage as necessary, stores updated
4257 * last_dst in *last_dstp in any case, and returns 0.
4258 * Otherwise, it returns errno without modifying *last_dstp.
4259 */
4260 static int
4261 audio_track_init_chvol(audio_track_t *track, audio_ring_t **last_dstp)
4262 {
4263 audio_ring_t *last_dst;
4264 audio_ring_t *srcbuf;
4265 audio_format2_t *srcfmt;
4266 audio_format2_t *dstfmt;
4267 audio_filter_arg_t *arg;
4268 u_int len;
4269 int error;
4270
4271 KASSERT(track);
4272
4273 last_dst = *last_dstp;
4274 dstfmt = &last_dst->fmt;
4275 srcfmt = &track->inputfmt;
4276 srcbuf = &track->chvol.srcbuf;
4277 error = 0;
4278
4279 /* Check whether channel volume conversion is necessary. */
4280 bool use_chvol = false;
4281 for (int ch = 0; ch < srcfmt->channels; ch++) {
4282 if (track->ch_volume[ch] != 256) {
4283 use_chvol = true;
4284 break;
4285 }
4286 }
4287
4288 if (use_chvol == true) {
4289 track->chvol.dst = last_dst;
4290 track->chvol.filter = audio_track_chvol;
4291
4292 srcbuf->fmt = *dstfmt;
4293 /* no format conversion occurs */
4294
4295 srcbuf->head = 0;
4296 srcbuf->used = 0;
4297 srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
4298 len = auring_bytelen(srcbuf);
4299 srcbuf->mem = audio_realloc(srcbuf->mem, len);
4300
4301 arg = &track->chvol.arg;
4302 arg->srcfmt = &srcbuf->fmt;
4303 arg->dstfmt = dstfmt;
4304 arg->context = track->ch_volume;
4305
4306 *last_dstp = srcbuf;
4307 return 0;
4308 }
4309
4310 track->chvol.filter = NULL;
4311 audio_free(srcbuf->mem);
4312 return error;
4313 }
4314
4315 /*
4316 * Initialize the chmix stage of this track as necessary.
4317 * If successful, it initializes the chmix stage as necessary, stores updated
4318 * last_dst in *last_dstp in any case, and returns 0.
4319 * Otherwise, it returns errno without modifying *last_dstp.
4320 */
4321 static int
4322 audio_track_init_chmix(audio_track_t *track, audio_ring_t **last_dstp)
4323 {
4324 audio_ring_t *last_dst;
4325 audio_ring_t *srcbuf;
4326 audio_format2_t *srcfmt;
4327 audio_format2_t *dstfmt;
4328 audio_filter_arg_t *arg;
4329 u_int srcch;
4330 u_int dstch;
4331 u_int len;
4332 int error;
4333
4334 KASSERT(track);
4335
4336 last_dst = *last_dstp;
4337 dstfmt = &last_dst->fmt;
4338 srcfmt = &track->inputfmt;
4339 srcbuf = &track->chmix.srcbuf;
4340 error = 0;
4341
4342 srcch = srcfmt->channels;
4343 dstch = dstfmt->channels;
4344 if (srcch != dstch) {
4345 track->chmix.dst = last_dst;
4346
4347 if (srcch >= 2 && dstch == 1) {
4348 track->chmix.filter = audio_track_chmix_mixLR;
4349 } else if (srcch == 1 && dstch >= 2) {
4350 track->chmix.filter = audio_track_chmix_dupLR;
4351 } else if (srcch > dstch) {
4352 track->chmix.filter = audio_track_chmix_shrink;
4353 } else {
4354 track->chmix.filter = audio_track_chmix_expand;
4355 }
4356
4357 srcbuf->fmt = *dstfmt;
4358 srcbuf->fmt.channels = srcch;
4359
4360 srcbuf->head = 0;
4361 srcbuf->used = 0;
4362 /* XXX The buffer size should be able to calculate. */
4363 srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
4364 len = auring_bytelen(srcbuf);
4365 srcbuf->mem = audio_realloc(srcbuf->mem, len);
4366
4367 arg = &track->chmix.arg;
4368 arg->srcfmt = &srcbuf->fmt;
4369 arg->dstfmt = dstfmt;
4370 arg->context = NULL;
4371
4372 *last_dstp = srcbuf;
4373 return 0;
4374 }
4375
4376 track->chmix.filter = NULL;
4377 audio_free(srcbuf->mem);
4378 return error;
4379 }
4380
4381 /*
4382 * Initialize the freq stage of this track as necessary.
4383 * If successful, it initializes the freq stage as necessary, stores updated
4384 * last_dst in *last_dstp in any case, and returns 0.
4385 * Otherwise, it returns errno without modifying *last_dstp.
4386 */
4387 static int
4388 audio_track_init_freq(audio_track_t *track, audio_ring_t **last_dstp)
4389 {
4390 audio_ring_t *last_dst;
4391 audio_ring_t *srcbuf;
4392 audio_format2_t *srcfmt;
4393 audio_format2_t *dstfmt;
4394 audio_filter_arg_t *arg;
4395 uint32_t srcfreq;
4396 uint32_t dstfreq;
4397 u_int dst_capacity;
4398 u_int mod;
4399 u_int len;
4400 int error;
4401
4402 KASSERT(track);
4403
4404 last_dst = *last_dstp;
4405 dstfmt = &last_dst->fmt;
4406 srcfmt = &track->inputfmt;
4407 srcbuf = &track->freq.srcbuf;
4408 error = 0;
4409
4410 srcfreq = srcfmt->sample_rate;
4411 dstfreq = dstfmt->sample_rate;
4412 if (srcfreq != dstfreq) {
4413 track->freq.dst = last_dst;
4414
4415 memset(track->freq_prev, 0, sizeof(track->freq_prev));
4416 memset(track->freq_curr, 0, sizeof(track->freq_curr));
4417
4418 /* freq_step is the ratio of src/dst when let dst 65536. */
4419 track->freq_step = (uint64_t)srcfreq * 65536 / dstfreq;
4420
4421 dst_capacity = frame_per_block(track->mixer, dstfmt);
4422 mod = (uint64_t)srcfreq * 65536 % dstfreq;
4423 track->freq_leap = (mod * dst_capacity + dstfreq / 2) / dstfreq;
4424
4425 if (track->freq_step < 65536) {
4426 track->freq.filter = audio_track_freq_up;
4427 /* In order to carry at the first time. */
4428 track->freq_current = 65536;
4429 } else {
4430 track->freq.filter = audio_track_freq_down;
4431 track->freq_current = 0;
4432 }
4433
4434 srcbuf->fmt = *dstfmt;
4435 srcbuf->fmt.sample_rate = srcfreq;
4436
4437 srcbuf->head = 0;
4438 srcbuf->used = 0;
4439 srcbuf->capacity = frame_per_block(track->mixer, &srcbuf->fmt);
4440 len = auring_bytelen(srcbuf);
4441 srcbuf->mem = audio_realloc(srcbuf->mem, len);
4442
4443 arg = &track->freq.arg;
4444 arg->srcfmt = &srcbuf->fmt;
4445 arg->dstfmt = dstfmt;/*&last_dst->fmt;*/
4446 arg->context = track;
4447
4448 *last_dstp = srcbuf;
4449 return 0;
4450 }
4451
4452 track->freq.filter = NULL;
4453 audio_free(srcbuf->mem);
4454 return error;
4455 }
4456
4457 /*
4458 * When playing back: (e.g. if codec and freq stage are valid)
4459 *
4460 * write
4461 * | uiomove
4462 * v
4463 * usrbuf [...............] byte ring buffer (mmap-able)
4464 * | memcpy
4465 * v
4466 * codec.srcbuf[....] 1 block (ring) buffer <-- stage input
4467 * .dst ----+
4468 * | convert
4469 * v
4470 * freq.srcbuf [....] 1 block (ring) buffer
4471 * .dst ----+
4472 * | convert
4473 * v
4474 * outbuf [...............] NBLKOUT blocks ring buffer
4475 *
4476 *
4477 * When recording:
4478 *
4479 * freq.srcbuf [...............] NBLKOUT blocks ring buffer <-- stage input
4480 * .dst ----+
4481 * | convert
4482 * v
4483 * codec.srcbuf[.....] 1 block (ring) buffer
4484 * .dst ----+
4485 * | convert
4486 * v
4487 * outbuf [.....] 1 block (ring) buffer
4488 * | memcpy
4489 * v
4490 * usrbuf [...............] byte ring buffer (mmap-able *)
4491 * | uiomove
4492 * v
4493 * read
4494 *
4495 * *: usrbuf for recording is also mmap-able due to symmetry with
4496 * playback buffer, but for now mmap will never happen for recording.
4497 */
4498
4499 /*
4500 * Set the userland format of this track.
4501 * usrfmt argument should have been previously verified by
4502 * audio_track_setinfo_check().
4503 * This function may release and reallocate all internal conversion buffers.
4504 * It returns 0 if successful. Otherwise it returns errno with clearing all
4505 * internal buffers.
4506 * It must be called without sc_intr_lock since uvm_* routines require non
4507 * intr_lock state.
4508 * It must be called with track lock held since it may release and reallocate
4509 * outbuf.
4510 */
4511 static int
4512 audio_track_set_format(audio_track_t *track, audio_format2_t *usrfmt)
4513 {
4514 struct audio_softc *sc;
4515 u_int newbufsize;
4516 u_int oldblksize;
4517 u_int len;
4518 int error;
4519
4520 KASSERT(track);
4521 sc = track->mixer->sc;
4522
4523 /* usrbuf is the closest buffer to the userland. */
4524 track->usrbuf.fmt = *usrfmt;
4525
4526 /*
4527 * For references, one block size (in 40msec) is:
4528 * 320 bytes = 204 blocks/64KB for mulaw/8kHz/1ch
4529 * 7680 bytes = 8 blocks/64KB for s16/48kHz/2ch
4530 * 30720 bytes = 90 KB/3blocks for s16/48kHz/8ch
4531 * 61440 bytes = 180 KB/3blocks for s16/96kHz/8ch
4532 * 245760 bytes = 720 KB/3blocks for s32/192kHz/8ch
4533 *
4534 * For example,
4535 * 1) If usrbuf_blksize = 7056 (s16/44.1k/2ch) and PAGE_SIZE = 8192,
4536 * newbufsize = rounddown(65536 / 7056) = 63504
4537 * newvsize = roundup2(63504, PAGE_SIZE) = 65536
4538 * Therefore it maps 8 * 8K pages and usrbuf->capacity = 63504.
4539 *
4540 * 2) If usrbuf_blksize = 7680 (s16/48k/2ch) and PAGE_SIZE = 4096,
4541 * newbufsize = rounddown(65536 / 7680) = 61440
4542 * newvsize = roundup2(61440, PAGE_SIZE) = 61440 (= 15 pages)
4543 * Therefore it maps 15 * 4K pages and usrbuf->capacity = 61440.
4544 */
4545 oldblksize = track->usrbuf_blksize;
4546 track->usrbuf_blksize = frametobyte(&track->usrbuf.fmt,
4547 frame_per_block(track->mixer, &track->usrbuf.fmt));
4548 track->usrbuf.head = 0;
4549 track->usrbuf.used = 0;
4550 newbufsize = MAX(track->usrbuf_blksize * AUMINNOBLK, 65536);
4551 newbufsize = rounddown(newbufsize, track->usrbuf_blksize);
4552 error = audio_realloc_usrbuf(track, newbufsize);
4553 if (error) {
4554 device_printf(sc->sc_dev, "malloc usrbuf(%d) failed\n",
4555 newbufsize);
4556 goto error;
4557 }
4558
4559 /* Recalc water mark. */
4560 if (track->usrbuf_blksize != oldblksize) {
4561 if (audio_track_is_playback(track)) {
4562 /* Set high at 100%, low at 75%. */
4563 track->usrbuf_usedhigh = track->usrbuf.capacity;
4564 track->usrbuf_usedlow = track->usrbuf.capacity * 3 / 4;
4565 } else {
4566 /* Set high at 100% minus 1block(?), low at 0% */
4567 track->usrbuf_usedhigh = track->usrbuf.capacity -
4568 track->usrbuf_blksize;
4569 track->usrbuf_usedlow = 0;
4570 }
4571 }
4572
4573 /* Stage buffer */
4574 audio_ring_t *last_dst = &track->outbuf;
4575 if (audio_track_is_playback(track)) {
4576 /* On playback, initialize from the mixer side in order. */
4577 track->inputfmt = *usrfmt;
4578 track->outbuf.fmt = track->mixer->track_fmt;
4579
4580 if ((error = audio_track_init_freq(track, &last_dst)) != 0)
4581 goto error;
4582 if ((error = audio_track_init_chmix(track, &last_dst)) != 0)
4583 goto error;
4584 if ((error = audio_track_init_chvol(track, &last_dst)) != 0)
4585 goto error;
4586 if ((error = audio_track_init_codec(track, &last_dst)) != 0)
4587 goto error;
4588 } else {
4589 /* On recording, initialize from userland side in order. */
4590 track->inputfmt = track->mixer->track_fmt;
4591 track->outbuf.fmt = *usrfmt;
4592
4593 if ((error = audio_track_init_codec(track, &last_dst)) != 0)
4594 goto error;
4595 if ((error = audio_track_init_chvol(track, &last_dst)) != 0)
4596 goto error;
4597 if ((error = audio_track_init_chmix(track, &last_dst)) != 0)
4598 goto error;
4599 if ((error = audio_track_init_freq(track, &last_dst)) != 0)
4600 goto error;
4601 }
4602 #if 0
4603 /* debug */
4604 if (track->freq.filter) {
4605 audio_print_format2("freq src", &track->freq.srcbuf.fmt);
4606 audio_print_format2("freq dst", &track->freq.dst->fmt);
4607 }
4608 if (track->chmix.filter) {
4609 audio_print_format2("chmix src", &track->chmix.srcbuf.fmt);
4610 audio_print_format2("chmix dst", &track->chmix.dst->fmt);
4611 }
4612 if (track->chvol.filter) {
4613 audio_print_format2("chvol src", &track->chvol.srcbuf.fmt);
4614 audio_print_format2("chvol dst", &track->chvol.dst->fmt);
4615 }
4616 if (track->codec.filter) {
4617 audio_print_format2("codec src", &track->codec.srcbuf.fmt);
4618 audio_print_format2("codec dst", &track->codec.dst->fmt);
4619 }
4620 #endif
4621
4622 /* Stage input buffer */
4623 track->input = last_dst;
4624
4625 /*
4626 * On the recording track, make the first stage a ring buffer.
4627 * XXX is there a better way?
4628 */
4629 if (audio_track_is_record(track)) {
4630 track->input->capacity = NBLKOUT *
4631 frame_per_block(track->mixer, &track->input->fmt);
4632 len = auring_bytelen(track->input);
4633 track->input->mem = audio_realloc(track->input->mem, len);
4634 }
4635
4636 /*
4637 * Output buffer.
4638 * On the playback track, its capacity is NBLKOUT blocks.
4639 * On the recording track, its capacity is 1 block.
4640 */
4641 track->outbuf.head = 0;
4642 track->outbuf.used = 0;
4643 track->outbuf.capacity = frame_per_block(track->mixer,
4644 &track->outbuf.fmt);
4645 if (audio_track_is_playback(track))
4646 track->outbuf.capacity *= NBLKOUT;
4647 len = auring_bytelen(&track->outbuf);
4648 track->outbuf.mem = audio_realloc(track->outbuf.mem, len);
4649 if (track->outbuf.mem == NULL) {
4650 device_printf(sc->sc_dev, "malloc outbuf(%d) failed\n", len);
4651 error = ENOMEM;
4652 goto error;
4653 }
4654
4655 #if defined(AUDIO_DEBUG)
4656 if (audiodebug >= 3) {
4657 struct audio_track_debugbuf m;
4658
4659 memset(&m, 0, sizeof(m));
4660 snprintf(m.outbuf, sizeof(m.outbuf), " out=%d",
4661 track->outbuf.capacity * frametobyte(&track->outbuf.fmt,1));
4662 if (track->freq.filter)
4663 snprintf(m.freq, sizeof(m.freq), " freq=%d",
4664 track->freq.srcbuf.capacity *
4665 frametobyte(&track->freq.srcbuf.fmt, 1));
4666 if (track->chmix.filter)
4667 snprintf(m.chmix, sizeof(m.chmix), " chmix=%d",
4668 track->chmix.srcbuf.capacity *
4669 frametobyte(&track->chmix.srcbuf.fmt, 1));
4670 if (track->chvol.filter)
4671 snprintf(m.chvol, sizeof(m.chvol), " chvol=%d",
4672 track->chvol.srcbuf.capacity *
4673 frametobyte(&track->chvol.srcbuf.fmt, 1));
4674 if (track->codec.filter)
4675 snprintf(m.codec, sizeof(m.codec), " codec=%d",
4676 track->codec.srcbuf.capacity *
4677 frametobyte(&track->codec.srcbuf.fmt, 1));
4678 snprintf(m.usrbuf, sizeof(m.usrbuf),
4679 " usr=%d", track->usrbuf.capacity);
4680
4681 if (audio_track_is_playback(track)) {
4682 TRACET(0, track, "bufsize%s%s%s%s%s%s",
4683 m.outbuf, m.freq, m.chmix,
4684 m.chvol, m.codec, m.usrbuf);
4685 } else {
4686 TRACET(0, track, "bufsize%s%s%s%s%s%s",
4687 m.freq, m.chmix, m.chvol,
4688 m.codec, m.outbuf, m.usrbuf);
4689 }
4690 }
4691 #endif
4692 return 0;
4693
4694 error:
4695 audio_free_usrbuf(track);
4696 audio_free(track->codec.srcbuf.mem);
4697 audio_free(track->chvol.srcbuf.mem);
4698 audio_free(track->chmix.srcbuf.mem);
4699 audio_free(track->freq.srcbuf.mem);
4700 audio_free(track->outbuf.mem);
4701 return error;
4702 }
4703
4704 /*
4705 * Fill silence frames (as the internal format) up to 1 block
4706 * if the ring is not empty and less than 1 block.
4707 * It returns the number of appended frames.
4708 */
4709 static int
4710 audio_append_silence(audio_track_t *track, audio_ring_t *ring)
4711 {
4712 int fpb;
4713 int n;
4714
4715 KASSERT(track);
4716 KASSERT(audio_format2_is_internal(&ring->fmt));
4717
4718 /* XXX is n correct? */
4719 /* XXX memset uses frametobyte()? */
4720
4721 if (ring->used == 0)
4722 return 0;
4723
4724 fpb = frame_per_block(track->mixer, &ring->fmt);
4725 if (ring->used >= fpb)
4726 return 0;
4727
4728 n = (ring->capacity - ring->used) % fpb;
4729
4730 KASSERTMSG(auring_get_contig_free(ring) >= n,
4731 "auring_get_contig_free(ring)=%d n=%d",
4732 auring_get_contig_free(ring), n);
4733
4734 memset(auring_tailptr_aint(ring), 0,
4735 n * ring->fmt.channels * sizeof(aint_t));
4736 auring_push(ring, n);
4737 return n;
4738 }
4739
4740 /*
4741 * Execute the conversion stage.
4742 * It prepares arg from this stage and executes stage->filter.
4743 * It must be called only if stage->filter is not NULL.
4744 *
4745 * For stages other than frequency conversion, the function increments
4746 * src and dst counters here. For frequency conversion stage, on the
4747 * other hand, the function does not touch src and dst counters and
4748 * filter side has to increment them.
4749 */
4750 static void
4751 audio_apply_stage(audio_track_t *track, audio_stage_t *stage, bool isfreq)
4752 {
4753 audio_filter_arg_t *arg;
4754 int srccount;
4755 int dstcount;
4756 int count;
4757
4758 KASSERT(track);
4759 KASSERT(stage->filter);
4760
4761 srccount = auring_get_contig_used(&stage->srcbuf);
4762 dstcount = auring_get_contig_free(stage->dst);
4763
4764 if (isfreq) {
4765 KASSERTMSG(srccount > 0, "freq but srccount=%d", srccount);
4766 count = uimin(dstcount, track->mixer->frames_per_block);
4767 } else {
4768 count = uimin(srccount, dstcount);
4769 }
4770
4771 if (count > 0) {
4772 arg = &stage->arg;
4773 arg->src = auring_headptr(&stage->srcbuf);
4774 arg->dst = auring_tailptr(stage->dst);
4775 arg->count = count;
4776
4777 stage->filter(arg);
4778
4779 if (!isfreq) {
4780 auring_take(&stage->srcbuf, count);
4781 auring_push(stage->dst, count);
4782 }
4783 }
4784 }
4785
4786 /*
4787 * Produce output buffer for playback from user input buffer.
4788 * It must be called only if usrbuf is not empty and outbuf is
4789 * available at least one free block.
4790 */
4791 static void
4792 audio_track_play(audio_track_t *track)
4793 {
4794 audio_ring_t *usrbuf;
4795 audio_ring_t *input;
4796 int count;
4797 int framesize;
4798 int bytes;
4799
4800 KASSERT(track);
4801 KASSERT(track->lock);
4802 TRACET(4, track, "start pstate=%d", track->pstate);
4803
4804 /* At this point usrbuf must not be empty. */
4805 KASSERT(track->usrbuf.used > 0);
4806 /* Also, outbuf must be available at least one block. */
4807 count = auring_get_contig_free(&track->outbuf);
4808 KASSERTMSG(count >= frame_per_block(track->mixer, &track->outbuf.fmt),
4809 "count=%d fpb=%d",
4810 count, frame_per_block(track->mixer, &track->outbuf.fmt));
4811
4812 /* XXX TODO: is this necessary for now? */
4813 int track_count_0 = track->outbuf.used;
4814
4815 usrbuf = &track->usrbuf;
4816 input = track->input;
4817
4818 /*
4819 * framesize is always 1 byte or more since all formats supported as
4820 * usrfmt(=input) have 8bit or more stride.
4821 */
4822 framesize = frametobyte(&input->fmt, 1);
4823 KASSERT(framesize >= 1);
4824
4825 /* The next stage of usrbuf (=input) must be available. */
4826 KASSERT(auring_get_contig_free(input) > 0);
4827
4828 /*
4829 * Copy usrbuf up to 1block to input buffer.
4830 * count is the number of frames to copy from usrbuf.
4831 * bytes is the number of bytes to copy from usrbuf. However it is
4832 * not copied less than one frame.
4833 */
4834 count = uimin(usrbuf->used, track->usrbuf_blksize) / framesize;
4835 bytes = count * framesize;
4836
4837 track->usrbuf_stamp += bytes;
4838
4839 if (usrbuf->head + bytes < usrbuf->capacity) {
4840 memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
4841 (uint8_t *)usrbuf->mem + usrbuf->head,
4842 bytes);
4843 auring_push(input, count);
4844 auring_take(usrbuf, bytes);
4845 } else {
4846 int bytes1;
4847 int bytes2;
4848
4849 bytes1 = auring_get_contig_used(usrbuf);
4850 KASSERTMSG(bytes1 % framesize == 0,
4851 "bytes1=%d framesize=%d", bytes1, framesize);
4852 memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
4853 (uint8_t *)usrbuf->mem + usrbuf->head,
4854 bytes1);
4855 auring_push(input, bytes1 / framesize);
4856 auring_take(usrbuf, bytes1);
4857
4858 bytes2 = bytes - bytes1;
4859 memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
4860 (uint8_t *)usrbuf->mem + usrbuf->head,
4861 bytes2);
4862 auring_push(input, bytes2 / framesize);
4863 auring_take(usrbuf, bytes2);
4864 }
4865
4866 /* Encoding conversion */
4867 if (track->codec.filter)
4868 audio_apply_stage(track, &track->codec, false);
4869
4870 /* Channel volume */
4871 if (track->chvol.filter)
4872 audio_apply_stage(track, &track->chvol, false);
4873
4874 /* Channel mix */
4875 if (track->chmix.filter)
4876 audio_apply_stage(track, &track->chmix, false);
4877
4878 /* Frequency conversion */
4879 /*
4880 * Since the frequency conversion needs correction for each block,
4881 * it rounds up to 1 block.
4882 */
4883 if (track->freq.filter) {
4884 int n;
4885 n = audio_append_silence(track, &track->freq.srcbuf);
4886 if (n > 0) {
4887 TRACET(4, track,
4888 "freq.srcbuf add silence %d -> %d/%d/%d",
4889 n,
4890 track->freq.srcbuf.head,
4891 track->freq.srcbuf.used,
4892 track->freq.srcbuf.capacity);
4893 }
4894 if (track->freq.srcbuf.used > 0) {
4895 audio_apply_stage(track, &track->freq, true);
4896 }
4897 }
4898
4899 if (bytes < track->usrbuf_blksize) {
4900 /*
4901 * Clear all conversion buffer pointer if the conversion was
4902 * not exactly one block. These conversion stage buffers are
4903 * certainly circular buffers because of symmetry with the
4904 * previous and next stage buffer. However, since they are
4905 * treated as simple contiguous buffers in operation, so head
4906 * always should point 0. This may happen during drain-age.
4907 */
4908 TRACET(4, track, "reset stage");
4909 if (track->codec.filter) {
4910 KASSERT(track->codec.srcbuf.used == 0);
4911 track->codec.srcbuf.head = 0;
4912 }
4913 if (track->chvol.filter) {
4914 KASSERT(track->chvol.srcbuf.used == 0);
4915 track->chvol.srcbuf.head = 0;
4916 }
4917 if (track->chmix.filter) {
4918 KASSERT(track->chmix.srcbuf.used == 0);
4919 track->chmix.srcbuf.head = 0;
4920 }
4921 if (track->freq.filter) {
4922 KASSERT(track->freq.srcbuf.used == 0);
4923 track->freq.srcbuf.head = 0;
4924 }
4925 }
4926
4927 if (track->input == &track->outbuf) {
4928 track->outputcounter = track->inputcounter;
4929 } else {
4930 track->outputcounter += track->outbuf.used - track_count_0;
4931 }
4932
4933 #if defined(AUDIO_DEBUG)
4934 if (audiodebug >= 3) {
4935 struct audio_track_debugbuf m;
4936 audio_track_bufstat(track, &m);
4937 TRACET(0, track, "end%s%s%s%s%s%s",
4938 m.outbuf, m.freq, m.chvol, m.chmix, m.codec, m.usrbuf);
4939 }
4940 #endif
4941 }
4942
4943 /*
4944 * Produce user output buffer for recording from input buffer.
4945 */
4946 static void
4947 audio_track_record(audio_track_t *track)
4948 {
4949 audio_ring_t *outbuf;
4950 audio_ring_t *usrbuf;
4951 int count;
4952 int bytes;
4953 int framesize;
4954
4955 KASSERT(track);
4956 KASSERT(track->lock);
4957
4958 if (auring_get_contig_used(track->input) == 0) {
4959 TRACET(4, track, "input->used == 0");
4960 return;
4961 }
4962
4963 /* Frequency conversion */
4964 if (track->freq.filter) {
4965 if (track->freq.srcbuf.used > 0) {
4966 audio_apply_stage(track, &track->freq, true);
4967 /* XXX should input of freq be from beginning of buf? */
4968 }
4969 }
4970
4971 /* Channel mix */
4972 if (track->chmix.filter)
4973 audio_apply_stage(track, &track->chmix, false);
4974
4975 /* Channel volume */
4976 if (track->chvol.filter)
4977 audio_apply_stage(track, &track->chvol, false);
4978
4979 /* Encoding conversion */
4980 if (track->codec.filter)
4981 audio_apply_stage(track, &track->codec, false);
4982
4983 /* Copy outbuf to usrbuf */
4984 outbuf = &track->outbuf;
4985 usrbuf = &track->usrbuf;
4986 /* usrbuf must have at least one free block. */
4987 KASSERT(usrbuf->used <= track->usrbuf_usedhigh - track->usrbuf_blksize);
4988 /*
4989 * framesize is always 1 byte or more since all formats supported
4990 * as usrfmt(=output) have 8bit or more stride.
4991 */
4992 framesize = frametobyte(&outbuf->fmt, 1);
4993 KASSERT(framesize >= 1);
4994 /*
4995 * count is the number of frames to copy to usrbuf.
4996 * bytes is the number of bytes to copy to usrbuf.
4997 */
4998 count = outbuf->used;
4999 count = uimin(count, track->usrbuf_blksize / framesize);
5000 bytes = count * framesize;
5001 if (auring_tail(usrbuf) + bytes < usrbuf->capacity) {
5002 memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
5003 (uint8_t *)outbuf->mem + outbuf->head * framesize,
5004 bytes);
5005 auring_push(usrbuf, bytes);
5006 auring_take(outbuf, count);
5007 } else {
5008 int bytes1;
5009 int bytes2;
5010
5011 bytes1 = auring_get_contig_free(usrbuf);
5012 KASSERTMSG(bytes1 % framesize == 0,
5013 "bytes1=%d framesize=%d", bytes1, framesize);
5014 memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
5015 (uint8_t *)outbuf->mem + outbuf->head * framesize,
5016 bytes1);
5017 auring_push(usrbuf, bytes1);
5018 auring_take(outbuf, bytes1 / framesize);
5019
5020 bytes2 = bytes - bytes1;
5021 memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
5022 (uint8_t *)outbuf->mem + outbuf->head * framesize,
5023 bytes2);
5024 auring_push(usrbuf, bytes2);
5025 auring_take(outbuf, bytes2 / framesize);
5026 }
5027
5028 /* XXX TODO: any counters here? */
5029
5030 #if defined(AUDIO_DEBUG)
5031 if (audiodebug >= 3) {
5032 struct audio_track_debugbuf m;
5033 audio_track_bufstat(track, &m);
5034 TRACET(0, track, "end%s%s%s%s%s%s",
5035 m.freq, m.chvol, m.chmix, m.codec, m.outbuf, m.usrbuf);
5036 }
5037 #endif
5038 }
5039
5040 /*
5041 * Calculate blktime [msec] from mixer(.hwbuf.fmt).
5042 * Must be called with sc_exlock held.
5043 */
5044 static u_int
5045 audio_mixer_calc_blktime(struct audio_softc *sc, audio_trackmixer_t *mixer)
5046 {
5047 audio_format2_t *fmt;
5048 u_int blktime;
5049 u_int frames_per_block;
5050
5051 KASSERT(sc->sc_exlock);
5052
5053 fmt = &mixer->hwbuf.fmt;
5054 blktime = sc->sc_blk_ms;
5055
5056 /*
5057 * If stride is not multiples of 8, special treatment is necessary.
5058 * For now, it is only x68k's vs(4), 4 bit/sample ADPCM.
5059 */
5060 if (fmt->stride == 4) {
5061 frames_per_block = fmt->sample_rate * blktime / 1000;
5062 if ((frames_per_block & 1) != 0)
5063 blktime *= 2;
5064 }
5065 #ifdef DIAGNOSTIC
5066 else if (fmt->stride % NBBY != 0) {
5067 panic("unsupported HW stride %d", fmt->stride);
5068 }
5069 #endif
5070
5071 return blktime;
5072 }
5073
5074 /*
5075 * Initialize the mixer corresponding to the mode.
5076 * Set AUMODE_PLAY to the 'mode' for playback or AUMODE_RECORD for recording.
5077 * sc->sc_[pr]mixer (corresponding to the 'mode') must be zero-filled.
5078 * This function returns 0 on successful. Otherwise returns errno.
5079 * Must be called with sc_exlock held and without sc_lock held.
5080 */
5081 static int
5082 audio_mixer_init(struct audio_softc *sc, int mode,
5083 const audio_format2_t *hwfmt, const audio_filter_reg_t *reg)
5084 {
5085 char codecbuf[64];
5086 char blkdmsbuf[8];
5087 audio_trackmixer_t *mixer;
5088 void (*softint_handler)(void *);
5089 int len;
5090 int blksize;
5091 int capacity;
5092 size_t bufsize;
5093 int hwblks;
5094 int blkms;
5095 int blkdms;
5096 int error;
5097
5098 KASSERT(hwfmt != NULL);
5099 KASSERT(reg != NULL);
5100 KASSERT(sc->sc_exlock);
5101
5102 error = 0;
5103 if (mode == AUMODE_PLAY)
5104 mixer = sc->sc_pmixer;
5105 else
5106 mixer = sc->sc_rmixer;
5107
5108 mixer->sc = sc;
5109 mixer->mode = mode;
5110
5111 mixer->hwbuf.fmt = *hwfmt;
5112 mixer->volume = 256;
5113 mixer->blktime_d = 1000;
5114 mixer->blktime_n = audio_mixer_calc_blktime(sc, mixer);
5115 sc->sc_blk_ms = mixer->blktime_n;
5116 hwblks = NBLKHW;
5117
5118 mixer->frames_per_block = frame_per_block(mixer, &mixer->hwbuf.fmt);
5119 blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
5120 if (sc->hw_if->round_blocksize) {
5121 int rounded;
5122 audio_params_t p = format2_to_params(&mixer->hwbuf.fmt);
5123 mutex_enter(sc->sc_lock);
5124 rounded = sc->hw_if->round_blocksize(sc->hw_hdl, blksize,
5125 mode, &p);
5126 mutex_exit(sc->sc_lock);
5127 TRACE(1, "round_blocksize %d -> %d", blksize, rounded);
5128 if (rounded != blksize) {
5129 if ((rounded * NBBY) % (mixer->hwbuf.fmt.stride *
5130 mixer->hwbuf.fmt.channels) != 0) {
5131 audio_printf(sc,
5132 "round_blocksize returned blocksize "
5133 "indivisible by framesize: "
5134 "blksize=%d rounded=%d "
5135 "stride=%ubit channels=%u\n",
5136 blksize, rounded,
5137 mixer->hwbuf.fmt.stride,
5138 mixer->hwbuf.fmt.channels);
5139 return EINVAL;
5140 }
5141 /* Recalculation */
5142 blksize = rounded;
5143 mixer->frames_per_block = blksize * NBBY /
5144 (mixer->hwbuf.fmt.stride *
5145 mixer->hwbuf.fmt.channels);
5146 }
5147 }
5148 mixer->blktime_n = mixer->frames_per_block;
5149 mixer->blktime_d = mixer->hwbuf.fmt.sample_rate;
5150
5151 capacity = mixer->frames_per_block * hwblks;
5152 bufsize = frametobyte(&mixer->hwbuf.fmt, capacity);
5153 if (sc->hw_if->round_buffersize) {
5154 size_t rounded;
5155 mutex_enter(sc->sc_lock);
5156 rounded = sc->hw_if->round_buffersize(sc->hw_hdl, mode,
5157 bufsize);
5158 mutex_exit(sc->sc_lock);
5159 TRACE(1, "round_buffersize %zd -> %zd", bufsize, rounded);
5160 if (rounded < bufsize) {
5161 /* buffersize needs NBLKHW blocks at least. */
5162 audio_printf(sc,
5163 "round_buffersize returned too small buffersize: "
5164 "buffersize=%zd blksize=%d\n",
5165 rounded, blksize);
5166 return EINVAL;
5167 }
5168 if (rounded % blksize != 0) {
5169 /* buffersize/blksize constraint mismatch? */
5170 audio_printf(sc,
5171 "round_buffersize returned buffersize indivisible "
5172 "by blksize: buffersize=%zu blksize=%d\n",
5173 rounded, blksize);
5174 return EINVAL;
5175 }
5176 if (rounded != bufsize) {
5177 /* Recalculation */
5178 bufsize = rounded;
5179 hwblks = bufsize / blksize;
5180 capacity = mixer->frames_per_block * hwblks;
5181 }
5182 }
5183 TRACE(1, "buffersize for %s = %zu",
5184 (mode == AUMODE_PLAY) ? "playback" : "recording",
5185 bufsize);
5186 mixer->hwbuf.capacity = capacity;
5187
5188 if (sc->hw_if->allocm) {
5189 /* sc_lock is not necessary for allocm */
5190 mixer->hwbuf.mem = sc->hw_if->allocm(sc->hw_hdl, mode, bufsize);
5191 if (mixer->hwbuf.mem == NULL) {
5192 audio_printf(sc, "allocm(%zu) failed\n", bufsize);
5193 return ENOMEM;
5194 }
5195 } else {
5196 mixer->hwbuf.mem = kmem_alloc(bufsize, KM_SLEEP);
5197 }
5198
5199 /* From here, audio_mixer_destroy is necessary to exit. */
5200 if (mode == AUMODE_PLAY) {
5201 cv_init(&mixer->outcv, "audiowr");
5202 } else {
5203 cv_init(&mixer->outcv, "audiord");
5204 }
5205
5206 if (mode == AUMODE_PLAY) {
5207 softint_handler = audio_softintr_wr;
5208 } else {
5209 softint_handler = audio_softintr_rd;
5210 }
5211 mixer->sih = softint_establish(SOFTINT_SERIAL | SOFTINT_MPSAFE,
5212 softint_handler, sc);
5213 if (mixer->sih == NULL) {
5214 device_printf(sc->sc_dev, "softint_establish failed\n");
5215 goto abort;
5216 }
5217
5218 mixer->track_fmt.encoding = AUDIO_ENCODING_SLINEAR_NE;
5219 mixer->track_fmt.precision = AUDIO_INTERNAL_BITS;
5220 mixer->track_fmt.stride = AUDIO_INTERNAL_BITS;
5221 mixer->track_fmt.channels = mixer->hwbuf.fmt.channels;
5222 mixer->track_fmt.sample_rate = mixer->hwbuf.fmt.sample_rate;
5223
5224 if (mixer->hwbuf.fmt.encoding == AUDIO_ENCODING_SLINEAR_OE &&
5225 mixer->hwbuf.fmt.precision == AUDIO_INTERNAL_BITS) {
5226 mixer->swap_endian = true;
5227 TRACE(1, "swap_endian");
5228 }
5229
5230 if (mode == AUMODE_PLAY) {
5231 /* Mixing buffer */
5232 mixer->mixfmt = mixer->track_fmt;
5233 mixer->mixfmt.precision *= 2;
5234 mixer->mixfmt.stride *= 2;
5235 /* XXX TODO: use some macros? */
5236 len = mixer->frames_per_block * mixer->mixfmt.channels *
5237 mixer->mixfmt.stride / NBBY;
5238 mixer->mixsample = audio_realloc(mixer->mixsample, len);
5239 } else {
5240 /* No mixing buffer for recording */
5241 }
5242
5243 if (reg->codec) {
5244 mixer->codec = reg->codec;
5245 mixer->codecarg.context = reg->context;
5246 if (mode == AUMODE_PLAY) {
5247 mixer->codecarg.srcfmt = &mixer->track_fmt;
5248 mixer->codecarg.dstfmt = &mixer->hwbuf.fmt;
5249 } else {
5250 mixer->codecarg.srcfmt = &mixer->hwbuf.fmt;
5251 mixer->codecarg.dstfmt = &mixer->track_fmt;
5252 }
5253 mixer->codecbuf.fmt = mixer->track_fmt;
5254 mixer->codecbuf.capacity = mixer->frames_per_block;
5255 len = auring_bytelen(&mixer->codecbuf);
5256 mixer->codecbuf.mem = audio_realloc(mixer->codecbuf.mem, len);
5257 if (mixer->codecbuf.mem == NULL) {
5258 device_printf(sc->sc_dev,
5259 "malloc codecbuf(%d) failed\n", len);
5260 error = ENOMEM;
5261 goto abort;
5262 }
5263 }
5264
5265 /* Succeeded so display it. */
5266 codecbuf[0] = '\0';
5267 if (mixer->codec || mixer->swap_endian) {
5268 snprintf(codecbuf, sizeof(codecbuf), " %s %s:%d",
5269 (mode == AUMODE_PLAY) ? "->" : "<-",
5270 audio_encoding_name(mixer->hwbuf.fmt.encoding),
5271 mixer->hwbuf.fmt.precision);
5272 }
5273 blkms = mixer->blktime_n * 1000 / mixer->blktime_d;
5274 blkdms = (mixer->blktime_n * 10000 / mixer->blktime_d) % 10;
5275 blkdmsbuf[0] = '\0';
5276 if (blkdms != 0) {
5277 snprintf(blkdmsbuf, sizeof(blkdmsbuf), ".%1d", blkdms);
5278 }
5279 aprint_normal_dev(sc->sc_dev,
5280 "%s:%d%s %dch %dHz, blk %d bytes (%d%sms) for %s\n",
5281 audio_encoding_name(mixer->track_fmt.encoding),
5282 mixer->track_fmt.precision,
5283 codecbuf,
5284 mixer->track_fmt.channels,
5285 mixer->track_fmt.sample_rate,
5286 blksize,
5287 blkms, blkdmsbuf,
5288 (mode == AUMODE_PLAY) ? "playback" : "recording");
5289
5290 return 0;
5291
5292 abort:
5293 audio_mixer_destroy(sc, mixer);
5294 return error;
5295 }
5296
5297 /*
5298 * Releases all resources of 'mixer'.
5299 * Note that it does not release the memory area of 'mixer' itself.
5300 * Must be called with sc_exlock held and without sc_lock held.
5301 */
5302 static void
5303 audio_mixer_destroy(struct audio_softc *sc, audio_trackmixer_t *mixer)
5304 {
5305 int bufsize;
5306
5307 KASSERT(sc->sc_exlock == 1);
5308
5309 bufsize = frametobyte(&mixer->hwbuf.fmt, mixer->hwbuf.capacity);
5310
5311 if (mixer->hwbuf.mem != NULL) {
5312 if (sc->hw_if->freem) {
5313 /* sc_lock is not necessary for freem */
5314 sc->hw_if->freem(sc->hw_hdl, mixer->hwbuf.mem, bufsize);
5315 } else {
5316 kmem_free(mixer->hwbuf.mem, bufsize);
5317 }
5318 mixer->hwbuf.mem = NULL;
5319 }
5320
5321 audio_free(mixer->codecbuf.mem);
5322 audio_free(mixer->mixsample);
5323
5324 cv_destroy(&mixer->outcv);
5325
5326 if (mixer->sih) {
5327 softint_disestablish(mixer->sih);
5328 mixer->sih = NULL;
5329 }
5330 }
5331
5332 /*
5333 * Starts playback mixer.
5334 * Must be called only if sc_pbusy is false.
5335 * Must be called with sc_lock && sc_exlock held.
5336 * Must not be called from the interrupt context.
5337 */
5338 static void
5339 audio_pmixer_start(struct audio_softc *sc, bool force)
5340 {
5341 audio_trackmixer_t *mixer;
5342 int minimum;
5343
5344 KASSERT(mutex_owned(sc->sc_lock));
5345 KASSERT(sc->sc_exlock);
5346 KASSERT(sc->sc_pbusy == false);
5347
5348 mutex_enter(sc->sc_intr_lock);
5349
5350 mixer = sc->sc_pmixer;
5351 TRACE(2, "%smixseq=%d hwseq=%d hwbuf=%d/%d/%d%s",
5352 (audiodebug >= 3) ? "begin " : "",
5353 (int)mixer->mixseq, (int)mixer->hwseq,
5354 mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity,
5355 force ? " force" : "");
5356
5357 /* Need two blocks to start normally. */
5358 minimum = (force) ? 1 : 2;
5359 while (mixer->hwbuf.used < mixer->frames_per_block * minimum) {
5360 audio_pmixer_process(sc);
5361 }
5362
5363 /* Start output */
5364 audio_pmixer_output(sc);
5365 sc->sc_pbusy = true;
5366
5367 TRACE(3, "end mixseq=%d hwseq=%d hwbuf=%d/%d/%d",
5368 (int)mixer->mixseq, (int)mixer->hwseq,
5369 mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
5370
5371 mutex_exit(sc->sc_intr_lock);
5372 }
5373
5374 /*
5375 * When playing back with MD filter:
5376 *
5377 * track track ...
5378 * v v
5379 * + mix (with aint2_t)
5380 * | master volume (with aint2_t)
5381 * v
5382 * mixsample [::::] wide-int 1 block (ring) buffer
5383 * |
5384 * | convert aint2_t -> aint_t
5385 * v
5386 * codecbuf [....] 1 block (ring) buffer
5387 * |
5388 * | convert to hw format
5389 * v
5390 * hwbuf [............] NBLKHW blocks ring buffer
5391 *
5392 * When playing back without MD filter:
5393 *
5394 * mixsample [::::] wide-int 1 block (ring) buffer
5395 * |
5396 * | convert aint2_t -> aint_t
5397 * | (with byte swap if necessary)
5398 * v
5399 * hwbuf [............] NBLKHW blocks ring buffer
5400 *
5401 * mixsample: slinear_NE, wide internal precision, HW ch, HW freq.
5402 * codecbuf: slinear_NE, internal precision, HW ch, HW freq.
5403 * hwbuf: HW encoding, HW precision, HW ch, HW freq.
5404 */
5405
5406 /*
5407 * Performs track mixing and converts it to hwbuf.
5408 * Note that this function doesn't transfer hwbuf to hardware.
5409 * Must be called with sc_intr_lock held.
5410 */
5411 static void
5412 audio_pmixer_process(struct audio_softc *sc)
5413 {
5414 audio_trackmixer_t *mixer;
5415 audio_file_t *f;
5416 int frame_count;
5417 int sample_count;
5418 int mixed;
5419 int i;
5420 aint2_t *m;
5421 aint_t *h;
5422
5423 mixer = sc->sc_pmixer;
5424
5425 frame_count = mixer->frames_per_block;
5426 KASSERTMSG(auring_get_contig_free(&mixer->hwbuf) >= frame_count,
5427 "auring_get_contig_free()=%d frame_count=%d",
5428 auring_get_contig_free(&mixer->hwbuf), frame_count);
5429 sample_count = frame_count * mixer->mixfmt.channels;
5430
5431 mixer->mixseq++;
5432
5433 /* Mix all tracks */
5434 mixed = 0;
5435 SLIST_FOREACH(f, &sc->sc_files, entry) {
5436 audio_track_t *track = f->ptrack;
5437
5438 if (track == NULL)
5439 continue;
5440
5441 if (track->is_pause) {
5442 TRACET(4, track, "skip; paused");
5443 continue;
5444 }
5445
5446 /* Skip if the track is used by process context. */
5447 if (audio_track_lock_tryenter(track) == false) {
5448 TRACET(4, track, "skip; in use");
5449 continue;
5450 }
5451
5452 /* Emulate mmap'ped track */
5453 if (track->mmapped) {
5454 auring_push(&track->usrbuf, track->usrbuf_blksize);
5455 TRACET(4, track, "mmap; usr=%d/%d/C%d",
5456 track->usrbuf.head,
5457 track->usrbuf.used,
5458 track->usrbuf.capacity);
5459 }
5460
5461 if (track->outbuf.used < mixer->frames_per_block &&
5462 track->usrbuf.used > 0) {
5463 TRACET(4, track, "process");
5464 audio_track_play(track);
5465 }
5466
5467 if (track->outbuf.used > 0) {
5468 mixed = audio_pmixer_mix_track(mixer, track, mixed);
5469 } else {
5470 TRACET(4, track, "skip; empty");
5471 }
5472
5473 audio_track_lock_exit(track);
5474 }
5475
5476 if (mixed == 0) {
5477 /* Silence */
5478 memset(mixer->mixsample, 0,
5479 frametobyte(&mixer->mixfmt, frame_count));
5480 } else {
5481 if (mixed > 1) {
5482 /* If there are multiple tracks, do auto gain control */
5483 audio_pmixer_agc(mixer, sample_count);
5484 }
5485
5486 /* Apply master volume */
5487 if (mixer->volume < 256) {
5488 m = mixer->mixsample;
5489 for (i = 0; i < sample_count; i++) {
5490 *m = AUDIO_SCALEDOWN(*m * mixer->volume, 8);
5491 m++;
5492 }
5493
5494 /*
5495 * Recover the volume gradually at the pace of
5496 * several times per second. If it's too fast, you
5497 * can recognize that the volume changes up and down
5498 * quickly and it's not so comfortable.
5499 */
5500 mixer->voltimer += mixer->blktime_n;
5501 if (mixer->voltimer * 4 >= mixer->blktime_d) {
5502 mixer->volume++;
5503 mixer->voltimer = 0;
5504 #if defined(AUDIO_DEBUG_AGC)
5505 TRACE(1, "volume recover: %d", mixer->volume);
5506 #endif
5507 }
5508 }
5509 }
5510
5511 /*
5512 * The rest is the hardware part.
5513 */
5514
5515 if (mixer->codec) {
5516 h = auring_tailptr_aint(&mixer->codecbuf);
5517 } else {
5518 h = auring_tailptr_aint(&mixer->hwbuf);
5519 }
5520
5521 m = mixer->mixsample;
5522 if (mixer->swap_endian) {
5523 for (i = 0; i < sample_count; i++) {
5524 *h++ = bswap16(*m++);
5525 }
5526 } else {
5527 for (i = 0; i < sample_count; i++) {
5528 *h++ = *m++;
5529 }
5530 }
5531
5532 /* Hardware driver's codec */
5533 if (mixer->codec) {
5534 auring_push(&mixer->codecbuf, frame_count);
5535 mixer->codecarg.src = auring_headptr(&mixer->codecbuf);
5536 mixer->codecarg.dst = auring_tailptr(&mixer->hwbuf);
5537 mixer->codecarg.count = frame_count;
5538 mixer->codec(&mixer->codecarg);
5539 auring_take(&mixer->codecbuf, mixer->codecarg.count);
5540 }
5541
5542 auring_push(&mixer->hwbuf, frame_count);
5543
5544 TRACE(4, "done mixseq=%d hwbuf=%d/%d/%d%s",
5545 (int)mixer->mixseq,
5546 mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity,
5547 (mixed == 0) ? " silent" : "");
5548 }
5549
5550 /*
5551 * Do auto gain control.
5552 * Must be called sc_intr_lock held.
5553 */
5554 static void
5555 audio_pmixer_agc(audio_trackmixer_t *mixer, int sample_count)
5556 {
5557 struct audio_softc *sc __unused;
5558 aint2_t val;
5559 aint2_t maxval;
5560 aint2_t minval;
5561 aint2_t over_plus;
5562 aint2_t over_minus;
5563 aint2_t *m;
5564 int newvol;
5565 int i;
5566
5567 sc = mixer->sc;
5568
5569 /* Overflow detection */
5570 maxval = AINT_T_MAX;
5571 minval = AINT_T_MIN;
5572 m = mixer->mixsample;
5573 for (i = 0; i < sample_count; i++) {
5574 val = *m++;
5575 if (val > maxval)
5576 maxval = val;
5577 else if (val < minval)
5578 minval = val;
5579 }
5580
5581 /* Absolute value of overflowed amount */
5582 over_plus = maxval - AINT_T_MAX;
5583 over_minus = AINT_T_MIN - minval;
5584
5585 if (over_plus > 0 || over_minus > 0) {
5586 if (over_plus > over_minus) {
5587 newvol = (int)((aint2_t)AINT_T_MAX * 256 / maxval);
5588 } else {
5589 newvol = (int)((aint2_t)AINT_T_MIN * 256 / minval);
5590 }
5591
5592 /*
5593 * Change the volume only if new one is smaller.
5594 * Reset the timer even if the volume isn't changed.
5595 */
5596 if (newvol <= mixer->volume) {
5597 mixer->volume = newvol;
5598 mixer->voltimer = 0;
5599 #if defined(AUDIO_DEBUG_AGC)
5600 TRACE(1, "auto volume adjust: %d", mixer->volume);
5601 #endif
5602 }
5603 }
5604 }
5605
5606 /*
5607 * Mix one track.
5608 * 'mixed' specifies the number of tracks mixed so far.
5609 * It returns the number of tracks mixed. In other words, it returns
5610 * mixed + 1 if this track is mixed.
5611 */
5612 static int
5613 audio_pmixer_mix_track(audio_trackmixer_t *mixer, audio_track_t *track,
5614 int mixed)
5615 {
5616 int count;
5617 int sample_count;
5618 int remain;
5619 int i;
5620 const aint_t *s;
5621 aint2_t *d;
5622
5623 /* XXX TODO: Is this necessary for now? */
5624 if (mixer->mixseq < track->seq)
5625 return mixed;
5626
5627 count = auring_get_contig_used(&track->outbuf);
5628 count = uimin(count, mixer->frames_per_block);
5629
5630 s = auring_headptr_aint(&track->outbuf);
5631 d = mixer->mixsample;
5632
5633 /*
5634 * Apply track volume with double-sized integer and perform
5635 * additive synthesis.
5636 *
5637 * XXX If you limit the track volume to 1.0 or less (<= 256),
5638 * it would be better to do this in the track conversion stage
5639 * rather than here. However, if you accept the volume to
5640 * be greater than 1.0 (> 256), it's better to do it here.
5641 * Because the operation here is done by double-sized integer.
5642 */
5643 sample_count = count * mixer->mixfmt.channels;
5644 if (mixed == 0) {
5645 /* If this is the first track, assignment can be used. */
5646 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
5647 if (track->volume != 256) {
5648 for (i = 0; i < sample_count; i++) {
5649 aint2_t v;
5650 v = *s++;
5651 *d++ = AUDIO_SCALEDOWN(v * track->volume, 8)
5652 }
5653 } else
5654 #endif
5655 {
5656 for (i = 0; i < sample_count; i++) {
5657 *d++ = ((aint2_t)*s++);
5658 }
5659 }
5660 /* Fill silence if the first track is not filled. */
5661 for (; i < mixer->frames_per_block * mixer->mixfmt.channels; i++)
5662 *d++ = 0;
5663 } else {
5664 /* If this is the second or later, add it. */
5665 #if defined(AUDIO_SUPPORT_TRACK_VOLUME)
5666 if (track->volume != 256) {
5667 for (i = 0; i < sample_count; i++) {
5668 aint2_t v;
5669 v = *s++;
5670 *d++ += AUDIO_SCALEDOWN(v * track->volume, 8);
5671 }
5672 } else
5673 #endif
5674 {
5675 for (i = 0; i < sample_count; i++) {
5676 *d++ += ((aint2_t)*s++);
5677 }
5678 }
5679 }
5680
5681 auring_take(&track->outbuf, count);
5682 /*
5683 * The counters have to align block even if outbuf is less than
5684 * one block. XXX Is this still necessary?
5685 */
5686 remain = mixer->frames_per_block - count;
5687 if (__predict_false(remain != 0)) {
5688 auring_push(&track->outbuf, remain);
5689 auring_take(&track->outbuf, remain);
5690 }
5691
5692 /*
5693 * Update track sequence.
5694 * mixseq has previous value yet at this point.
5695 */
5696 track->seq = mixer->mixseq + 1;
5697
5698 return mixed + 1;
5699 }
5700
5701 /*
5702 * Output one block from hwbuf to HW.
5703 * Must be called with sc_intr_lock held.
5704 */
5705 static void
5706 audio_pmixer_output(struct audio_softc *sc)
5707 {
5708 audio_trackmixer_t *mixer;
5709 audio_params_t params;
5710 void *start;
5711 void *end;
5712 int blksize;
5713 int error;
5714
5715 mixer = sc->sc_pmixer;
5716 TRACE(4, "pbusy=%d hwbuf=%d/%d/%d",
5717 sc->sc_pbusy,
5718 mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
5719 KASSERTMSG(mixer->hwbuf.used >= mixer->frames_per_block,
5720 "mixer->hwbuf.used=%d mixer->frames_per_block=%d",
5721 mixer->hwbuf.used, mixer->frames_per_block);
5722
5723 blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
5724
5725 if (sc->hw_if->trigger_output) {
5726 /* trigger (at once) */
5727 if (!sc->sc_pbusy) {
5728 start = mixer->hwbuf.mem;
5729 end = (uint8_t *)start + auring_bytelen(&mixer->hwbuf);
5730 params = format2_to_params(&mixer->hwbuf.fmt);
5731
5732 error = sc->hw_if->trigger_output(sc->hw_hdl,
5733 start, end, blksize, audio_pintr, sc, ¶ms);
5734 if (error) {
5735 audio_printf(sc,
5736 "trigger_output failed: errno=%d\n",
5737 error);
5738 return;
5739 }
5740 }
5741 } else {
5742 /* start (everytime) */
5743 start = auring_headptr(&mixer->hwbuf);
5744
5745 error = sc->hw_if->start_output(sc->hw_hdl,
5746 start, blksize, audio_pintr, sc);
5747 if (error) {
5748 audio_printf(sc,
5749 "start_output failed: errno=%d\n", error);
5750 return;
5751 }
5752 }
5753 }
5754
5755 /*
5756 * This is an interrupt handler for playback.
5757 * It is called with sc_intr_lock held.
5758 *
5759 * It is usually called from hardware interrupt. However, note that
5760 * for some drivers (e.g. uaudio) it is called from software interrupt.
5761 */
5762 static void
5763 audio_pintr(void *arg)
5764 {
5765 struct audio_softc *sc;
5766 audio_trackmixer_t *mixer;
5767
5768 sc = arg;
5769 KASSERT(mutex_owned(sc->sc_intr_lock));
5770
5771 if (sc->sc_dying)
5772 return;
5773 if (sc->sc_pbusy == false) {
5774 #if defined(DIAGNOSTIC)
5775 audio_printf(sc, "DIAGNOSTIC: %s raised stray interrupt\n",
5776 device_xname(sc->hw_dev));
5777 #endif
5778 return;
5779 }
5780
5781 mixer = sc->sc_pmixer;
5782 mixer->hw_complete_counter += mixer->frames_per_block;
5783 mixer->hwseq++;
5784
5785 auring_take(&mixer->hwbuf, mixer->frames_per_block);
5786
5787 TRACE(4,
5788 "HW_INT ++hwseq=%" PRIu64 " cmplcnt=%" PRIu64 " hwbuf=%d/%d/%d",
5789 mixer->hwseq, mixer->hw_complete_counter,
5790 mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
5791
5792 #if defined(AUDIO_HW_SINGLE_BUFFER)
5793 /*
5794 * Create a new block here and output it immediately.
5795 * It makes a latency lower but needs machine power.
5796 */
5797 audio_pmixer_process(sc);
5798 audio_pmixer_output(sc);
5799 #else
5800 /*
5801 * It is called when block N output is done.
5802 * Output immediately block N+1 created by the last interrupt.
5803 * And then create block N+2 for the next interrupt.
5804 * This method makes playback robust even on slower machines.
5805 * Instead the latency is increased by one block.
5806 */
5807
5808 /* At first, output ready block. */
5809 if (mixer->hwbuf.used >= mixer->frames_per_block) {
5810 audio_pmixer_output(sc);
5811 }
5812
5813 bool later = false;
5814
5815 if (mixer->hwbuf.used < mixer->frames_per_block) {
5816 later = true;
5817 }
5818
5819 /* Then, process next block. */
5820 audio_pmixer_process(sc);
5821
5822 if (later) {
5823 audio_pmixer_output(sc);
5824 }
5825 #endif
5826
5827 /*
5828 * When this interrupt is the real hardware interrupt, disabling
5829 * preemption here is not necessary. But some drivers (e.g. uaudio)
5830 * emulate it by software interrupt, so kpreempt_disable is necessary.
5831 */
5832 kpreempt_disable();
5833 softint_schedule(mixer->sih);
5834 kpreempt_enable();
5835 }
5836
5837 /*
5838 * Starts record mixer.
5839 * Must be called only if sc_rbusy is false.
5840 * Must be called with sc_lock && sc_exlock held.
5841 * Must not be called from the interrupt context.
5842 */
5843 static void
5844 audio_rmixer_start(struct audio_softc *sc)
5845 {
5846
5847 KASSERT(mutex_owned(sc->sc_lock));
5848 KASSERT(sc->sc_exlock);
5849 KASSERT(sc->sc_rbusy == false);
5850
5851 mutex_enter(sc->sc_intr_lock);
5852
5853 TRACE(2, "%s", (audiodebug >= 3) ? "begin" : "");
5854 audio_rmixer_input(sc);
5855 sc->sc_rbusy = true;
5856 TRACE(3, "end");
5857
5858 mutex_exit(sc->sc_intr_lock);
5859 }
5860
5861 /*
5862 * When recording with MD filter:
5863 *
5864 * hwbuf [............] NBLKHW blocks ring buffer
5865 * |
5866 * | convert from hw format
5867 * v
5868 * codecbuf [....] 1 block (ring) buffer
5869 * | |
5870 * v v
5871 * track track ...
5872 *
5873 * When recording without MD filter:
5874 *
5875 * hwbuf [............] NBLKHW blocks ring buffer
5876 * | |
5877 * v v
5878 * track track ...
5879 *
5880 * hwbuf: HW encoding, HW precision, HW ch, HW freq.
5881 * codecbuf: slinear_NE, internal precision, HW ch, HW freq.
5882 */
5883
5884 /*
5885 * Distribute a recorded block to all recording tracks.
5886 */
5887 static void
5888 audio_rmixer_process(struct audio_softc *sc)
5889 {
5890 audio_trackmixer_t *mixer;
5891 audio_ring_t *mixersrc;
5892 audio_file_t *f;
5893 aint_t *p;
5894 int count;
5895 int bytes;
5896 int i;
5897
5898 mixer = sc->sc_rmixer;
5899
5900 /*
5901 * count is the number of frames to be retrieved this time.
5902 * count should be one block.
5903 */
5904 count = auring_get_contig_used(&mixer->hwbuf);
5905 count = uimin(count, mixer->frames_per_block);
5906 if (count <= 0) {
5907 TRACE(4, "count %d: too short", count);
5908 return;
5909 }
5910 bytes = frametobyte(&mixer->track_fmt, count);
5911
5912 /* Hardware driver's codec */
5913 if (mixer->codec) {
5914 mixer->codecarg.src = auring_headptr(&mixer->hwbuf);
5915 mixer->codecarg.dst = auring_tailptr(&mixer->codecbuf);
5916 mixer->codecarg.count = count;
5917 mixer->codec(&mixer->codecarg);
5918 auring_take(&mixer->hwbuf, mixer->codecarg.count);
5919 auring_push(&mixer->codecbuf, mixer->codecarg.count);
5920 mixersrc = &mixer->codecbuf;
5921 } else {
5922 mixersrc = &mixer->hwbuf;
5923 }
5924
5925 if (mixer->swap_endian) {
5926 /* inplace conversion */
5927 p = auring_headptr_aint(mixersrc);
5928 for (i = 0; i < count * mixer->track_fmt.channels; i++, p++) {
5929 *p = bswap16(*p);
5930 }
5931 }
5932
5933 /* Distribute to all tracks. */
5934 SLIST_FOREACH(f, &sc->sc_files, entry) {
5935 audio_track_t *track = f->rtrack;
5936 audio_ring_t *input;
5937
5938 if (track == NULL)
5939 continue;
5940
5941 if (track->is_pause) {
5942 TRACET(4, track, "skip; paused");
5943 continue;
5944 }
5945
5946 if (audio_track_lock_tryenter(track) == false) {
5947 TRACET(4, track, "skip; in use");
5948 continue;
5949 }
5950
5951 /*
5952 * If the track buffer has less than one block of free space,
5953 * make one block free.
5954 */
5955 input = track->input;
5956 if (input->capacity - input->used < mixer->frames_per_block) {
5957 int drops = mixer->frames_per_block -
5958 (input->capacity - input->used);
5959 track->dropframes += drops;
5960 TRACET(4, track, "drop %d frames: inp=%d/%d/%d",
5961 drops,
5962 input->head, input->used, input->capacity);
5963 auring_take(input, drops);
5964 }
5965
5966 KASSERTMSG(auring_tail(input) % mixer->frames_per_block == 0,
5967 "inputtail=%d mixer->frames_per_block=%d",
5968 auring_tail(input), mixer->frames_per_block);
5969 memcpy(auring_tailptr_aint(input),
5970 auring_headptr_aint(mixersrc),
5971 bytes);
5972 auring_push(input, count);
5973
5974 /* XXX sequence counter? */
5975
5976 audio_track_lock_exit(track);
5977 }
5978
5979 auring_take(mixersrc, count);
5980 }
5981
5982 /*
5983 * Input one block from HW to hwbuf.
5984 * Must be called with sc_intr_lock held.
5985 */
5986 static void
5987 audio_rmixer_input(struct audio_softc *sc)
5988 {
5989 audio_trackmixer_t *mixer;
5990 audio_params_t params;
5991 void *start;
5992 void *end;
5993 int blksize;
5994 int error;
5995
5996 mixer = sc->sc_rmixer;
5997 blksize = frametobyte(&mixer->hwbuf.fmt, mixer->frames_per_block);
5998
5999 if (sc->hw_if->trigger_input) {
6000 /* trigger (at once) */
6001 if (!sc->sc_rbusy) {
6002 start = mixer->hwbuf.mem;
6003 end = (uint8_t *)start + auring_bytelen(&mixer->hwbuf);
6004 params = format2_to_params(&mixer->hwbuf.fmt);
6005
6006 error = sc->hw_if->trigger_input(sc->hw_hdl,
6007 start, end, blksize, audio_rintr, sc, ¶ms);
6008 if (error) {
6009 audio_printf(sc,
6010 "trigger_input failed: errno=%d\n",
6011 error);
6012 return;
6013 }
6014 }
6015 } else {
6016 /* start (everytime) */
6017 start = auring_tailptr(&mixer->hwbuf);
6018
6019 error = sc->hw_if->start_input(sc->hw_hdl,
6020 start, blksize, audio_rintr, sc);
6021 if (error) {
6022 audio_printf(sc,
6023 "start_input failed: errno=%d\n", error);
6024 return;
6025 }
6026 }
6027 }
6028
6029 /*
6030 * This is an interrupt handler for recording.
6031 * It is called with sc_intr_lock.
6032 *
6033 * It is usually called from hardware interrupt. However, note that
6034 * for some drivers (e.g. uaudio) it is called from software interrupt.
6035 */
6036 static void
6037 audio_rintr(void *arg)
6038 {
6039 struct audio_softc *sc;
6040 audio_trackmixer_t *mixer;
6041
6042 sc = arg;
6043 KASSERT(mutex_owned(sc->sc_intr_lock));
6044
6045 if (sc->sc_dying)
6046 return;
6047 if (sc->sc_rbusy == false) {
6048 #if defined(DIAGNOSTIC)
6049 audio_printf(sc, "DIAGNOSTIC: %s raised stray interrupt\n",
6050 device_xname(sc->hw_dev));
6051 #endif
6052 return;
6053 }
6054
6055 mixer = sc->sc_rmixer;
6056 mixer->hw_complete_counter += mixer->frames_per_block;
6057 mixer->hwseq++;
6058
6059 auring_push(&mixer->hwbuf, mixer->frames_per_block);
6060
6061 TRACE(4,
6062 "HW_INT ++hwseq=%" PRIu64 " cmplcnt=%" PRIu64 " hwbuf=%d/%d/%d",
6063 mixer->hwseq, mixer->hw_complete_counter,
6064 mixer->hwbuf.head, mixer->hwbuf.used, mixer->hwbuf.capacity);
6065
6066 /* Distrubute recorded block */
6067 audio_rmixer_process(sc);
6068
6069 /* Request next block */
6070 audio_rmixer_input(sc);
6071
6072 /*
6073 * When this interrupt is the real hardware interrupt, disabling
6074 * preemption here is not necessary. But some drivers (e.g. uaudio)
6075 * emulate it by software interrupt, so kpreempt_disable is necessary.
6076 */
6077 kpreempt_disable();
6078 softint_schedule(mixer->sih);
6079 kpreempt_enable();
6080 }
6081
6082 /*
6083 * Halts playback mixer.
6084 * This function also clears related parameters, so call this function
6085 * instead of calling halt_output directly.
6086 * Must be called only if sc_pbusy is true.
6087 * Must be called with sc_lock && sc_exlock held.
6088 */
6089 static int
6090 audio_pmixer_halt(struct audio_softc *sc)
6091 {
6092 int error;
6093
6094 TRACE(2, "called");
6095 KASSERT(mutex_owned(sc->sc_lock));
6096 KASSERT(sc->sc_exlock);
6097
6098 mutex_enter(sc->sc_intr_lock);
6099 error = sc->hw_if->halt_output(sc->hw_hdl);
6100
6101 /* Halts anyway even if some error has occurred. */
6102 sc->sc_pbusy = false;
6103 sc->sc_pmixer->hwbuf.head = 0;
6104 sc->sc_pmixer->hwbuf.used = 0;
6105 sc->sc_pmixer->mixseq = 0;
6106 sc->sc_pmixer->hwseq = 0;
6107 mutex_exit(sc->sc_intr_lock);
6108
6109 return error;
6110 }
6111
6112 /*
6113 * Halts recording mixer.
6114 * This function also clears related parameters, so call this function
6115 * instead of calling halt_input directly.
6116 * Must be called only if sc_rbusy is true.
6117 * Must be called with sc_lock && sc_exlock held.
6118 */
6119 static int
6120 audio_rmixer_halt(struct audio_softc *sc)
6121 {
6122 int error;
6123
6124 TRACE(2, "called");
6125 KASSERT(mutex_owned(sc->sc_lock));
6126 KASSERT(sc->sc_exlock);
6127
6128 mutex_enter(sc->sc_intr_lock);
6129 error = sc->hw_if->halt_input(sc->hw_hdl);
6130
6131 /* Halts anyway even if some error has occurred. */
6132 sc->sc_rbusy = false;
6133 sc->sc_rmixer->hwbuf.head = 0;
6134 sc->sc_rmixer->hwbuf.used = 0;
6135 sc->sc_rmixer->mixseq = 0;
6136 sc->sc_rmixer->hwseq = 0;
6137 mutex_exit(sc->sc_intr_lock);
6138
6139 return error;
6140 }
6141
6142 /*
6143 * Flush this track.
6144 * Halts all operations, clears all buffers, reset error counters.
6145 * XXX I'm not sure...
6146 */
6147 static void
6148 audio_track_clear(struct audio_softc *sc, audio_track_t *track)
6149 {
6150
6151 KASSERT(track);
6152 TRACET(3, track, "clear");
6153
6154 audio_track_lock_enter(track);
6155
6156 track->usrbuf.used = 0;
6157 /* Clear all internal parameters. */
6158 if (track->codec.filter) {
6159 track->codec.srcbuf.used = 0;
6160 track->codec.srcbuf.head = 0;
6161 }
6162 if (track->chvol.filter) {
6163 track->chvol.srcbuf.used = 0;
6164 track->chvol.srcbuf.head = 0;
6165 }
6166 if (track->chmix.filter) {
6167 track->chmix.srcbuf.used = 0;
6168 track->chmix.srcbuf.head = 0;
6169 }
6170 if (track->freq.filter) {
6171 track->freq.srcbuf.used = 0;
6172 track->freq.srcbuf.head = 0;
6173 if (track->freq_step < 65536)
6174 track->freq_current = 65536;
6175 else
6176 track->freq_current = 0;
6177 memset(track->freq_prev, 0, sizeof(track->freq_prev));
6178 memset(track->freq_curr, 0, sizeof(track->freq_curr));
6179 }
6180 /* Clear buffer, then operation halts naturally. */
6181 track->outbuf.used = 0;
6182
6183 /* Clear counters. */
6184 track->dropframes = 0;
6185
6186 audio_track_lock_exit(track);
6187 }
6188
6189 /*
6190 * Drain the track.
6191 * track must be present and for playback.
6192 * If successful, it returns 0. Otherwise returns errno.
6193 * Must be called with sc_lock held.
6194 */
6195 static int
6196 audio_track_drain(struct audio_softc *sc, audio_track_t *track)
6197 {
6198 audio_trackmixer_t *mixer;
6199 int done;
6200 int error;
6201
6202 KASSERT(track);
6203 TRACET(3, track, "start");
6204 mixer = track->mixer;
6205 KASSERT(mutex_owned(sc->sc_lock));
6206
6207 /* Ignore them if pause. */
6208 if (track->is_pause) {
6209 TRACET(3, track, "pause -> clear");
6210 track->pstate = AUDIO_STATE_CLEAR;
6211 }
6212 /* Terminate early here if there is no data in the track. */
6213 if (track->pstate == AUDIO_STATE_CLEAR) {
6214 TRACET(3, track, "no need to drain");
6215 return 0;
6216 }
6217 track->pstate = AUDIO_STATE_DRAINING;
6218
6219 for (;;) {
6220 /* I want to display it before condition evaluation. */
6221 TRACET(3, track, "pid=%d.%d trkseq=%d hwseq=%d out=%d/%d/%d",
6222 (int)curproc->p_pid, (int)curlwp->l_lid,
6223 (int)track->seq, (int)mixer->hwseq,
6224 track->outbuf.head, track->outbuf.used,
6225 track->outbuf.capacity);
6226
6227 /* Condition to terminate */
6228 audio_track_lock_enter(track);
6229 done = (track->usrbuf.used < frametobyte(&track->inputfmt, 1) &&
6230 track->outbuf.used == 0 &&
6231 track->seq <= mixer->hwseq);
6232 audio_track_lock_exit(track);
6233 if (done)
6234 break;
6235
6236 TRACET(3, track, "sleep");
6237 error = audio_track_waitio(sc, track);
6238 if (error)
6239 return error;
6240
6241 /* XXX call audio_track_play here ? */
6242 }
6243
6244 track->pstate = AUDIO_STATE_CLEAR;
6245 TRACET(3, track, "done trk_inp=%d trk_out=%d",
6246 (int)track->inputcounter, (int)track->outputcounter);
6247 return 0;
6248 }
6249
6250 /*
6251 * Send signal to process.
6252 * This is intended to be called only from audio_softintr_{rd,wr}.
6253 * Must be called without sc_intr_lock held.
6254 */
6255 static inline void
6256 audio_psignal(struct audio_softc *sc, pid_t pid, int signum)
6257 {
6258 proc_t *p;
6259
6260 KASSERT(pid != 0);
6261
6262 /*
6263 * psignal() must be called without spin lock held.
6264 */
6265
6266 mutex_enter(&proc_lock);
6267 p = proc_find(pid);
6268 if (p)
6269 psignal(p, signum);
6270 mutex_exit(&proc_lock);
6271 }
6272
6273 /*
6274 * This is software interrupt handler for record.
6275 * It is called from recording hardware interrupt everytime.
6276 * It does:
6277 * - Deliver SIGIO for all async processes.
6278 * - Notify to audio_read() that data has arrived.
6279 * - selnotify() for select/poll-ing processes.
6280 */
6281 /*
6282 * XXX If a process issues FIOASYNC between hardware interrupt and
6283 * software interrupt, (stray) SIGIO will be sent to the process
6284 * despite the fact that it has not receive recorded data yet.
6285 */
6286 static void
6287 audio_softintr_rd(void *cookie)
6288 {
6289 struct audio_softc *sc = cookie;
6290 audio_file_t *f;
6291 pid_t pid;
6292
6293 mutex_enter(sc->sc_lock);
6294
6295 SLIST_FOREACH(f, &sc->sc_files, entry) {
6296 audio_track_t *track = f->rtrack;
6297
6298 if (track == NULL)
6299 continue;
6300
6301 TRACET(4, track, "broadcast; inp=%d/%d/%d",
6302 track->input->head,
6303 track->input->used,
6304 track->input->capacity);
6305
6306 pid = f->async_audio;
6307 if (pid != 0) {
6308 TRACEF(4, f, "sending SIGIO %d", pid);
6309 audio_psignal(sc, pid, SIGIO);
6310 }
6311 }
6312
6313 /* Notify that data has arrived. */
6314 selnotify(&sc->sc_rsel, 0, NOTE_SUBMIT);
6315 cv_broadcast(&sc->sc_rmixer->outcv);
6316
6317 mutex_exit(sc->sc_lock);
6318 }
6319
6320 /*
6321 * This is software interrupt handler for playback.
6322 * It is called from playback hardware interrupt everytime.
6323 * It does:
6324 * - Deliver SIGIO for all async and writable (used < lowat) processes.
6325 * - Notify to audio_write() that outbuf block available.
6326 * - selnotify() for select/poll-ing processes if there are any writable
6327 * (used < lowat) processes. Checking each descriptor will be done by
6328 * filt_audiowrite_event().
6329 */
6330 static void
6331 audio_softintr_wr(void *cookie)
6332 {
6333 struct audio_softc *sc = cookie;
6334 audio_file_t *f;
6335 bool found;
6336 pid_t pid;
6337
6338 TRACE(4, "called");
6339 found = false;
6340
6341 mutex_enter(sc->sc_lock);
6342
6343 SLIST_FOREACH(f, &sc->sc_files, entry) {
6344 audio_track_t *track = f->ptrack;
6345
6346 if (track == NULL)
6347 continue;
6348
6349 TRACET(4, track, "broadcast; trkseq=%d out=%d/%d/%d",
6350 (int)track->seq,
6351 track->outbuf.head,
6352 track->outbuf.used,
6353 track->outbuf.capacity);
6354
6355 /*
6356 * Send a signal if the process is async mode and
6357 * used is lower than lowat.
6358 */
6359 if (track->usrbuf.used <= track->usrbuf_usedlow &&
6360 !track->is_pause) {
6361 /* For selnotify */
6362 found = true;
6363 /* For SIGIO */
6364 pid = f->async_audio;
6365 if (pid != 0) {
6366 TRACEF(4, f, "sending SIGIO %d", pid);
6367 audio_psignal(sc, pid, SIGIO);
6368 }
6369 }
6370 }
6371
6372 /*
6373 * Notify for select/poll when someone become writable.
6374 * It needs sc_lock (and not sc_intr_lock).
6375 */
6376 if (found) {
6377 TRACE(4, "selnotify");
6378 selnotify(&sc->sc_wsel, 0, NOTE_SUBMIT);
6379 }
6380
6381 /* Notify to audio_write() that outbuf available. */
6382 cv_broadcast(&sc->sc_pmixer->outcv);
6383
6384 mutex_exit(sc->sc_lock);
6385 }
6386
6387 /*
6388 * Check (and convert) the format *p came from userland.
6389 * If successful, it writes back the converted format to *p if necessary and
6390 * returns 0. Otherwise returns errno (*p may be changed even in this case).
6391 */
6392 static int
6393 audio_check_params(audio_format2_t *p)
6394 {
6395
6396 /*
6397 * Convert obsolete AUDIO_ENCODING_PCM encodings.
6398 *
6399 * AUDIO_ENCODING_PCM16 == AUDIO_ENCODING_LINEAR
6400 * So, it's always signed, as in SunOS.
6401 *
6402 * AUDIO_ENCODING_PCM8 == AUDIO_ENCODING_LINEAR8
6403 * So, it's always unsigned, as in SunOS.
6404 */
6405 if (p->encoding == AUDIO_ENCODING_PCM16) {
6406 p->encoding = AUDIO_ENCODING_SLINEAR;
6407 } else if (p->encoding == AUDIO_ENCODING_PCM8) {
6408 if (p->precision == 8)
6409 p->encoding = AUDIO_ENCODING_ULINEAR;
6410 else
6411 return EINVAL;
6412 }
6413
6414 /*
6415 * Convert obsoleted AUDIO_ENCODING_[SU]LINEAR without endianness
6416 * suffix.
6417 */
6418 if (p->encoding == AUDIO_ENCODING_SLINEAR)
6419 p->encoding = AUDIO_ENCODING_SLINEAR_NE;
6420 if (p->encoding == AUDIO_ENCODING_ULINEAR)
6421 p->encoding = AUDIO_ENCODING_ULINEAR_NE;
6422
6423 switch (p->encoding) {
6424 case AUDIO_ENCODING_ULAW:
6425 case AUDIO_ENCODING_ALAW:
6426 if (p->precision != 8)
6427 return EINVAL;
6428 break;
6429 case AUDIO_ENCODING_ADPCM:
6430 if (p->precision != 4 && p->precision != 8)
6431 return EINVAL;
6432 break;
6433 case AUDIO_ENCODING_SLINEAR_LE:
6434 case AUDIO_ENCODING_SLINEAR_BE:
6435 case AUDIO_ENCODING_ULINEAR_LE:
6436 case AUDIO_ENCODING_ULINEAR_BE:
6437 if (p->precision != 8 && p->precision != 16 &&
6438 p->precision != 24 && p->precision != 32)
6439 return EINVAL;
6440
6441 /* 8bit format does not have endianness. */
6442 if (p->precision == 8) {
6443 if (p->encoding == AUDIO_ENCODING_SLINEAR_OE)
6444 p->encoding = AUDIO_ENCODING_SLINEAR_NE;
6445 if (p->encoding == AUDIO_ENCODING_ULINEAR_OE)
6446 p->encoding = AUDIO_ENCODING_ULINEAR_NE;
6447 }
6448
6449 if (p->precision > p->stride)
6450 return EINVAL;
6451 break;
6452 case AUDIO_ENCODING_MPEG_L1_STREAM:
6453 case AUDIO_ENCODING_MPEG_L1_PACKETS:
6454 case AUDIO_ENCODING_MPEG_L1_SYSTEM:
6455 case AUDIO_ENCODING_MPEG_L2_STREAM:
6456 case AUDIO_ENCODING_MPEG_L2_PACKETS:
6457 case AUDIO_ENCODING_MPEG_L2_SYSTEM:
6458 case AUDIO_ENCODING_AC3:
6459 break;
6460 default:
6461 return EINVAL;
6462 }
6463
6464 /* sanity check # of channels*/
6465 if (p->channels < 1 || p->channels > AUDIO_MAX_CHANNELS)
6466 return EINVAL;
6467
6468 return 0;
6469 }
6470
6471 /*
6472 * Initialize playback and record mixers.
6473 * mode (AUMODE_{PLAY,RECORD}) indicates the mixer to be initialized.
6474 * phwfmt and rhwfmt indicate the hardware format. pfil and rfil indicate
6475 * the filter registration information. These four must not be NULL.
6476 * If successful returns 0. Otherwise returns errno.
6477 * Must be called with sc_exlock held and without sc_lock held.
6478 * Must not be called if there are any tracks.
6479 * Caller should check that the initialization succeed by whether
6480 * sc_[pr]mixer is not NULL.
6481 */
6482 static int
6483 audio_mixers_init(struct audio_softc *sc, int mode,
6484 const audio_format2_t *phwfmt, const audio_format2_t *rhwfmt,
6485 const audio_filter_reg_t *pfil, const audio_filter_reg_t *rfil)
6486 {
6487 int error;
6488
6489 KASSERT(phwfmt != NULL);
6490 KASSERT(rhwfmt != NULL);
6491 KASSERT(pfil != NULL);
6492 KASSERT(rfil != NULL);
6493 KASSERT(sc->sc_exlock);
6494
6495 if ((mode & AUMODE_PLAY)) {
6496 if (sc->sc_pmixer == NULL) {
6497 sc->sc_pmixer = kmem_zalloc(sizeof(*sc->sc_pmixer),
6498 KM_SLEEP);
6499 } else {
6500 /* destroy() doesn't free memory. */
6501 audio_mixer_destroy(sc, sc->sc_pmixer);
6502 memset(sc->sc_pmixer, 0, sizeof(*sc->sc_pmixer));
6503 }
6504 error = audio_mixer_init(sc, AUMODE_PLAY, phwfmt, pfil);
6505 if (error) {
6506 /* audio_mixer_init already displayed error code */
6507 audio_printf(sc, "configuring playback mode failed\n");
6508 kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
6509 sc->sc_pmixer = NULL;
6510 return error;
6511 }
6512 }
6513 if ((mode & AUMODE_RECORD)) {
6514 if (sc->sc_rmixer == NULL) {
6515 sc->sc_rmixer = kmem_zalloc(sizeof(*sc->sc_rmixer),
6516 KM_SLEEP);
6517 } else {
6518 /* destroy() doesn't free memory. */
6519 audio_mixer_destroy(sc, sc->sc_rmixer);
6520 memset(sc->sc_rmixer, 0, sizeof(*sc->sc_rmixer));
6521 }
6522 error = audio_mixer_init(sc, AUMODE_RECORD, rhwfmt, rfil);
6523 if (error) {
6524 /* audio_mixer_init already displayed error code */
6525 audio_printf(sc, "configuring record mode failed\n");
6526 kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
6527 sc->sc_rmixer = NULL;
6528 return error;
6529 }
6530 }
6531
6532 return 0;
6533 }
6534
6535 /*
6536 * Select a frequency.
6537 * Prioritize 48kHz and 44.1kHz. Otherwise choose the highest one.
6538 * XXX Better algorithm?
6539 */
6540 static int
6541 audio_select_freq(const struct audio_format *fmt)
6542 {
6543 int freq;
6544 int high;
6545 int low;
6546 int j;
6547
6548 if (fmt->frequency_type == 0) {
6549 low = fmt->frequency[0];
6550 high = fmt->frequency[1];
6551 freq = 48000;
6552 if (low <= freq && freq <= high) {
6553 return freq;
6554 }
6555 freq = 44100;
6556 if (low <= freq && freq <= high) {
6557 return freq;
6558 }
6559 return high;
6560 } else {
6561 for (j = 0; j < fmt->frequency_type; j++) {
6562 if (fmt->frequency[j] == 48000) {
6563 return fmt->frequency[j];
6564 }
6565 }
6566 high = 0;
6567 for (j = 0; j < fmt->frequency_type; j++) {
6568 if (fmt->frequency[j] == 44100) {
6569 return fmt->frequency[j];
6570 }
6571 if (fmt->frequency[j] > high) {
6572 high = fmt->frequency[j];
6573 }
6574 }
6575 return high;
6576 }
6577 }
6578
6579 /*
6580 * Choose the most preferred hardware format.
6581 * If successful, it will store the chosen format into *cand and return 0.
6582 * Otherwise, return errno.
6583 * Must be called without sc_lock held.
6584 */
6585 static int
6586 audio_hw_probe(struct audio_softc *sc, audio_format2_t *cand, int mode)
6587 {
6588 audio_format_query_t query;
6589 int cand_score;
6590 int score;
6591 int i;
6592 int error;
6593
6594 /*
6595 * Score each formats and choose the highest one.
6596 *
6597 * +---- priority(0-3)
6598 * |+--- encoding/precision
6599 * ||+-- channels
6600 * score = 0x000000PEC
6601 */
6602
6603 cand_score = 0;
6604 for (i = 0; ; i++) {
6605 memset(&query, 0, sizeof(query));
6606 query.index = i;
6607
6608 mutex_enter(sc->sc_lock);
6609 error = sc->hw_if->query_format(sc->hw_hdl, &query);
6610 mutex_exit(sc->sc_lock);
6611 if (error == EINVAL)
6612 break;
6613 if (error)
6614 return error;
6615
6616 #if defined(AUDIO_DEBUG)
6617 DPRINTF(1, "fmt[%d] %c%c pri=%d %s,%d/%dbit,%dch,", i,
6618 (query.fmt.mode & AUMODE_PLAY) ? 'P' : '-',
6619 (query.fmt.mode & AUMODE_RECORD) ? 'R' : '-',
6620 query.fmt.priority,
6621 audio_encoding_name(query.fmt.encoding),
6622 query.fmt.validbits,
6623 query.fmt.precision,
6624 query.fmt.channels);
6625 if (query.fmt.frequency_type == 0) {
6626 DPRINTF(1, "{%d-%d",
6627 query.fmt.frequency[0], query.fmt.frequency[1]);
6628 } else {
6629 int j;
6630 for (j = 0; j < query.fmt.frequency_type; j++) {
6631 DPRINTF(1, "%c%d",
6632 (j == 0) ? '{' : ',',
6633 query.fmt.frequency[j]);
6634 }
6635 }
6636 DPRINTF(1, "}\n");
6637 #endif
6638
6639 if ((query.fmt.mode & mode) == 0) {
6640 DPRINTF(1, "fmt[%d] skip; mode not match %d\n", i,
6641 mode);
6642 continue;
6643 }
6644
6645 if (query.fmt.priority < 0) {
6646 DPRINTF(1, "fmt[%d] skip; unsupported encoding\n", i);
6647 continue;
6648 }
6649
6650 /* Score */
6651 score = (query.fmt.priority & 3) * 0x100;
6652 if (query.fmt.encoding == AUDIO_ENCODING_SLINEAR_NE &&
6653 query.fmt.validbits == AUDIO_INTERNAL_BITS &&
6654 query.fmt.precision == AUDIO_INTERNAL_BITS) {
6655 score += 0x20;
6656 } else if (query.fmt.encoding == AUDIO_ENCODING_SLINEAR_OE &&
6657 query.fmt.validbits == AUDIO_INTERNAL_BITS &&
6658 query.fmt.precision == AUDIO_INTERNAL_BITS) {
6659 score += 0x10;
6660 }
6661
6662 /* Do not prefer surround formats */
6663 if (query.fmt.channels <= 2)
6664 score += query.fmt.channels;
6665
6666 if (score < cand_score) {
6667 DPRINTF(1, "fmt[%d] skip; score 0x%x < 0x%x\n", i,
6668 score, cand_score);
6669 continue;
6670 }
6671
6672 /* Update candidate */
6673 cand_score = score;
6674 cand->encoding = query.fmt.encoding;
6675 cand->precision = query.fmt.validbits;
6676 cand->stride = query.fmt.precision;
6677 cand->channels = query.fmt.channels;
6678 cand->sample_rate = audio_select_freq(&query.fmt);
6679 DPRINTF(1, "fmt[%d] candidate (score=0x%x)"
6680 " pri=%d %s,%d/%d,%dch,%dHz\n", i,
6681 cand_score, query.fmt.priority,
6682 audio_encoding_name(query.fmt.encoding),
6683 cand->precision, cand->stride,
6684 cand->channels, cand->sample_rate);
6685 }
6686
6687 if (cand_score == 0) {
6688 DPRINTF(1, "%s no fmt\n", __func__);
6689 return ENXIO;
6690 }
6691 DPRINTF(1, "%s selected: %s,%d/%d,%dch,%dHz\n", __func__,
6692 audio_encoding_name(cand->encoding),
6693 cand->precision, cand->stride, cand->channels, cand->sample_rate);
6694 return 0;
6695 }
6696
6697 /*
6698 * Validate fmt with query_format.
6699 * If fmt is included in the result of query_format, returns 0.
6700 * Otherwise returns EINVAL.
6701 * Must be called without sc_lock held.
6702 */
6703 static int
6704 audio_hw_validate_format(struct audio_softc *sc, int mode,
6705 const audio_format2_t *fmt)
6706 {
6707 audio_format_query_t query;
6708 struct audio_format *q;
6709 int index;
6710 int error;
6711 int j;
6712
6713 for (index = 0; ; index++) {
6714 query.index = index;
6715 mutex_enter(sc->sc_lock);
6716 error = sc->hw_if->query_format(sc->hw_hdl, &query);
6717 mutex_exit(sc->sc_lock);
6718 if (error == EINVAL)
6719 break;
6720 if (error)
6721 return error;
6722
6723 q = &query.fmt;
6724 /*
6725 * Note that fmt is audio_format2_t (precision/stride) but
6726 * q is audio_format_t (validbits/precision).
6727 */
6728 if ((q->mode & mode) == 0) {
6729 continue;
6730 }
6731 if (fmt->encoding != q->encoding) {
6732 continue;
6733 }
6734 if (fmt->precision != q->validbits) {
6735 continue;
6736 }
6737 if (fmt->stride != q->precision) {
6738 continue;
6739 }
6740 if (fmt->channels != q->channels) {
6741 continue;
6742 }
6743 if (q->frequency_type == 0) {
6744 if (fmt->sample_rate < q->frequency[0] ||
6745 fmt->sample_rate > q->frequency[1]) {
6746 continue;
6747 }
6748 } else {
6749 for (j = 0; j < q->frequency_type; j++) {
6750 if (fmt->sample_rate == q->frequency[j])
6751 break;
6752 }
6753 if (j == query.fmt.frequency_type) {
6754 continue;
6755 }
6756 }
6757
6758 /* Matched. */
6759 return 0;
6760 }
6761
6762 return EINVAL;
6763 }
6764
6765 /*
6766 * Set track mixer's format depending on ai->mode.
6767 * If AUMODE_PLAY is set in ai->mode, it set up the playback mixer
6768 * with ai.play.*.
6769 * If AUMODE_RECORD is set in ai->mode, it set up the recording mixer
6770 * with ai.record.*.
6771 * All other fields in ai are ignored.
6772 * If successful returns 0. Otherwise returns errno.
6773 * This function does not roll back even if it fails.
6774 * Must be called with sc_exlock held and without sc_lock held.
6775 */
6776 static int
6777 audio_mixers_set_format(struct audio_softc *sc, const struct audio_info *ai)
6778 {
6779 audio_format2_t phwfmt;
6780 audio_format2_t rhwfmt;
6781 audio_filter_reg_t pfil;
6782 audio_filter_reg_t rfil;
6783 int mode;
6784 int error;
6785
6786 KASSERT(sc->sc_exlock);
6787
6788 /*
6789 * Even when setting either one of playback and recording,
6790 * both must be halted.
6791 */
6792 if (sc->sc_popens + sc->sc_ropens > 0)
6793 return EBUSY;
6794
6795 if (!SPECIFIED(ai->mode) || ai->mode == 0)
6796 return ENOTTY;
6797
6798 mode = ai->mode;
6799 if ((mode & AUMODE_PLAY)) {
6800 phwfmt.encoding = ai->play.encoding;
6801 phwfmt.precision = ai->play.precision;
6802 phwfmt.stride = ai->play.precision;
6803 phwfmt.channels = ai->play.channels;
6804 phwfmt.sample_rate = ai->play.sample_rate;
6805 }
6806 if ((mode & AUMODE_RECORD)) {
6807 rhwfmt.encoding = ai->record.encoding;
6808 rhwfmt.precision = ai->record.precision;
6809 rhwfmt.stride = ai->record.precision;
6810 rhwfmt.channels = ai->record.channels;
6811 rhwfmt.sample_rate = ai->record.sample_rate;
6812 }
6813
6814 /* On non-independent devices, use the same format for both. */
6815 if ((sc->sc_props & AUDIO_PROP_INDEPENDENT) == 0) {
6816 if (mode == AUMODE_RECORD) {
6817 phwfmt = rhwfmt;
6818 } else {
6819 rhwfmt = phwfmt;
6820 }
6821 mode = AUMODE_PLAY | AUMODE_RECORD;
6822 }
6823
6824 /* Then, unset the direction not exist on the hardware. */
6825 if ((sc->sc_props & AUDIO_PROP_PLAYBACK) == 0)
6826 mode &= ~AUMODE_PLAY;
6827 if ((sc->sc_props & AUDIO_PROP_CAPTURE) == 0)
6828 mode &= ~AUMODE_RECORD;
6829
6830 /* debug */
6831 if ((mode & AUMODE_PLAY)) {
6832 TRACE(1, "play=%s/%d/%d/%dch/%dHz",
6833 audio_encoding_name(phwfmt.encoding),
6834 phwfmt.precision,
6835 phwfmt.stride,
6836 phwfmt.channels,
6837 phwfmt.sample_rate);
6838 }
6839 if ((mode & AUMODE_RECORD)) {
6840 TRACE(1, "rec =%s/%d/%d/%dch/%dHz",
6841 audio_encoding_name(rhwfmt.encoding),
6842 rhwfmt.precision,
6843 rhwfmt.stride,
6844 rhwfmt.channels,
6845 rhwfmt.sample_rate);
6846 }
6847
6848 /* Check the format */
6849 if ((mode & AUMODE_PLAY)) {
6850 if (audio_hw_validate_format(sc, AUMODE_PLAY, &phwfmt)) {
6851 TRACE(1, "invalid format");
6852 return EINVAL;
6853 }
6854 }
6855 if ((mode & AUMODE_RECORD)) {
6856 if (audio_hw_validate_format(sc, AUMODE_RECORD, &rhwfmt)) {
6857 TRACE(1, "invalid format");
6858 return EINVAL;
6859 }
6860 }
6861
6862 /* Configure the mixers. */
6863 memset(&pfil, 0, sizeof(pfil));
6864 memset(&rfil, 0, sizeof(rfil));
6865 error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
6866 if (error)
6867 return error;
6868
6869 error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
6870 if (error)
6871 return error;
6872
6873 /*
6874 * Reinitialize the sticky parameters for /dev/sound.
6875 * If the number of the hardware channels becomes less than the number
6876 * of channels that sticky parameters remember, subsequent /dev/sound
6877 * open will fail. To prevent this, reinitialize the sticky
6878 * parameters whenever the hardware format is changed.
6879 */
6880 sc->sc_sound_pparams = params_to_format2(&audio_default);
6881 sc->sc_sound_rparams = params_to_format2(&audio_default);
6882 sc->sc_sound_ppause = false;
6883 sc->sc_sound_rpause = false;
6884
6885 return 0;
6886 }
6887
6888 /*
6889 * Store current mixers format into *ai.
6890 * Must be called with sc_exlock held.
6891 */
6892 static void
6893 audio_mixers_get_format(struct audio_softc *sc, struct audio_info *ai)
6894 {
6895
6896 KASSERT(sc->sc_exlock);
6897
6898 /*
6899 * There is no stride information in audio_info but it doesn't matter.
6900 * trackmixer always treats stride and precision as the same.
6901 */
6902 AUDIO_INITINFO(ai);
6903 ai->mode = 0;
6904 if (sc->sc_pmixer) {
6905 audio_format2_t *fmt = &sc->sc_pmixer->track_fmt;
6906 ai->play.encoding = fmt->encoding;
6907 ai->play.precision = fmt->precision;
6908 ai->play.channels = fmt->channels;
6909 ai->play.sample_rate = fmt->sample_rate;
6910 ai->mode |= AUMODE_PLAY;
6911 }
6912 if (sc->sc_rmixer) {
6913 audio_format2_t *fmt = &sc->sc_rmixer->track_fmt;
6914 ai->record.encoding = fmt->encoding;
6915 ai->record.precision = fmt->precision;
6916 ai->record.channels = fmt->channels;
6917 ai->record.sample_rate = fmt->sample_rate;
6918 ai->mode |= AUMODE_RECORD;
6919 }
6920 }
6921
6922 /*
6923 * audio_info details:
6924 *
6925 * ai.{play,record}.sample_rate (R/W)
6926 * ai.{play,record}.encoding (R/W)
6927 * ai.{play,record}.precision (R/W)
6928 * ai.{play,record}.channels (R/W)
6929 * These specify the playback or recording format.
6930 * Ignore members within an inactive track.
6931 *
6932 * ai.mode (R/W)
6933 * It specifies the playback or recording mode, AUMODE_*.
6934 * Currently, a mode change operation by ai.mode after opening is
6935 * prohibited. In addition, AUMODE_PLAY_ALL no longer makes sense.
6936 * However, it's possible to get or to set for backward compatibility.
6937 *
6938 * ai.{hiwat,lowat} (R/W)
6939 * These specify the high water mark and low water mark for playback
6940 * track. The unit is block.
6941 *
6942 * ai.{play,record}.gain (R/W)
6943 * It specifies the HW mixer volume in 0-255.
6944 * It is historical reason that the gain is connected to HW mixer.
6945 *
6946 * ai.{play,record}.balance (R/W)
6947 * It specifies the left-right balance of HW mixer in 0-64.
6948 * 32 means the center.
6949 * It is historical reason that the balance is connected to HW mixer.
6950 *
6951 * ai.{play,record}.port (R/W)
6952 * It specifies the input/output port of HW mixer.
6953 *
6954 * ai.monitor_gain (R/W)
6955 * It specifies the recording monitor gain(?) of HW mixer.
6956 *
6957 * ai.{play,record}.pause (R/W)
6958 * Non-zero means the track is paused.
6959 *
6960 * ai.play.seek (R/-)
6961 * It indicates the number of bytes written but not processed.
6962 * ai.record.seek (R/-)
6963 * It indicates the number of bytes to be able to read.
6964 *
6965 * ai.{play,record}.avail_ports (R/-)
6966 * Mixer info.
6967 *
6968 * ai.{play,record}.buffer_size (R/-)
6969 * It indicates the buffer size in bytes. Internally it means usrbuf.
6970 *
6971 * ai.{play,record}.samples (R/-)
6972 * It indicates the total number of bytes played or recorded.
6973 *
6974 * ai.{play,record}.eof (R/-)
6975 * It indicates the number of times reached EOF(?).
6976 *
6977 * ai.{play,record}.error (R/-)
6978 * Non-zero indicates overflow/underflow has occurred.
6979 *
6980 * ai.{play,record}.waiting (R/-)
6981 * Non-zero indicates that other process waits to open.
6982 * It will never happen anymore.
6983 *
6984 * ai.{play,record}.open (R/-)
6985 * Non-zero indicates the direction is opened by this process(?).
6986 * XXX Is this better to indicate that "the device is opened by
6987 * at least one process"?
6988 *
6989 * ai.{play,record}.active (R/-)
6990 * Non-zero indicates that I/O is currently active.
6991 *
6992 * ai.blocksize (R/-)
6993 * It indicates the block size in bytes.
6994 * XXX The blocksize of playback and recording may be different.
6995 */
6996
6997 /*
6998 * Pause consideration:
6999 *
7000 * Pausing/unpausing never affect [pr]mixer. This single rule makes
7001 * operation simple. Note that playback and recording are asymmetric.
7002 *
7003 * For playback,
7004 * 1. Any playback open doesn't start pmixer regardless of initial pause
7005 * state of this track.
7006 * 2. The first write access among playback tracks only starts pmixer
7007 * regardless of this track's pause state.
7008 * 3. Even a pause of the last playback track doesn't stop pmixer.
7009 * 4. The last close of all playback tracks only stops pmixer.
7010 *
7011 * For recording,
7012 * 1. The first recording open only starts rmixer regardless of initial
7013 * pause state of this track.
7014 * 2. Even a pause of the last track doesn't stop rmixer.
7015 * 3. The last close of all recording tracks only stops rmixer.
7016 */
7017
7018 /*
7019 * Set both track's parameters within a file depending on ai.
7020 * Update sc_sound_[pr]* if set.
7021 * Must be called with sc_exlock held and without sc_lock held.
7022 */
7023 static int
7024 audio_file_setinfo(struct audio_softc *sc, audio_file_t *file,
7025 const struct audio_info *ai)
7026 {
7027 const struct audio_prinfo *pi;
7028 const struct audio_prinfo *ri;
7029 audio_track_t *ptrack;
7030 audio_track_t *rtrack;
7031 audio_format2_t pfmt;
7032 audio_format2_t rfmt;
7033 int pchanges;
7034 int rchanges;
7035 int mode;
7036 struct audio_info saved_ai;
7037 audio_format2_t saved_pfmt;
7038 audio_format2_t saved_rfmt;
7039 int error;
7040
7041 KASSERT(sc->sc_exlock);
7042
7043 pi = &ai->play;
7044 ri = &ai->record;
7045 pchanges = 0;
7046 rchanges = 0;
7047
7048 ptrack = file->ptrack;
7049 rtrack = file->rtrack;
7050
7051 #if defined(AUDIO_DEBUG)
7052 if (audiodebug >= 2) {
7053 char buf[256];
7054 char p[64];
7055 int buflen;
7056 int plen;
7057 #define SPRINTF(var, fmt...) do { \
7058 var##len += snprintf(var + var##len, sizeof(var) - var##len, fmt); \
7059 } while (0)
7060
7061 buflen = 0;
7062 plen = 0;
7063 if (SPECIFIED(pi->encoding))
7064 SPRINTF(p, "/%s", audio_encoding_name(pi->encoding));
7065 if (SPECIFIED(pi->precision))
7066 SPRINTF(p, "/%dbit", pi->precision);
7067 if (SPECIFIED(pi->channels))
7068 SPRINTF(p, "/%dch", pi->channels);
7069 if (SPECIFIED(pi->sample_rate))
7070 SPRINTF(p, "/%dHz", pi->sample_rate);
7071 if (plen > 0)
7072 SPRINTF(buf, ",play.param=%s", p + 1);
7073
7074 plen = 0;
7075 if (SPECIFIED(ri->encoding))
7076 SPRINTF(p, "/%s", audio_encoding_name(ri->encoding));
7077 if (SPECIFIED(ri->precision))
7078 SPRINTF(p, "/%dbit", ri->precision);
7079 if (SPECIFIED(ri->channels))
7080 SPRINTF(p, "/%dch", ri->channels);
7081 if (SPECIFIED(ri->sample_rate))
7082 SPRINTF(p, "/%dHz", ri->sample_rate);
7083 if (plen > 0)
7084 SPRINTF(buf, ",record.param=%s", p + 1);
7085
7086 if (SPECIFIED(ai->mode))
7087 SPRINTF(buf, ",mode=%d", ai->mode);
7088 if (SPECIFIED(ai->hiwat))
7089 SPRINTF(buf, ",hiwat=%d", ai->hiwat);
7090 if (SPECIFIED(ai->lowat))
7091 SPRINTF(buf, ",lowat=%d", ai->lowat);
7092 if (SPECIFIED(ai->play.gain))
7093 SPRINTF(buf, ",play.gain=%d", ai->play.gain);
7094 if (SPECIFIED(ai->record.gain))
7095 SPRINTF(buf, ",record.gain=%d", ai->record.gain);
7096 if (SPECIFIED_CH(ai->play.balance))
7097 SPRINTF(buf, ",play.balance=%d", ai->play.balance);
7098 if (SPECIFIED_CH(ai->record.balance))
7099 SPRINTF(buf, ",record.balance=%d", ai->record.balance);
7100 if (SPECIFIED(ai->play.port))
7101 SPRINTF(buf, ",play.port=%d", ai->play.port);
7102 if (SPECIFIED(ai->record.port))
7103 SPRINTF(buf, ",record.port=%d", ai->record.port);
7104 if (SPECIFIED(ai->monitor_gain))
7105 SPRINTF(buf, ",monitor_gain=%d", ai->monitor_gain);
7106 if (SPECIFIED_CH(ai->play.pause))
7107 SPRINTF(buf, ",play.pause=%d", ai->play.pause);
7108 if (SPECIFIED_CH(ai->record.pause))
7109 SPRINTF(buf, ",record.pause=%d", ai->record.pause);
7110
7111 if (buflen > 0)
7112 TRACE(2, "specified %s", buf + 1);
7113 }
7114 #endif
7115
7116 AUDIO_INITINFO(&saved_ai);
7117 /* XXX shut up gcc */
7118 memset(&saved_pfmt, 0, sizeof(saved_pfmt));
7119 memset(&saved_rfmt, 0, sizeof(saved_rfmt));
7120
7121 /*
7122 * Set default value and save current parameters.
7123 * For backward compatibility, use sticky parameters for nonexistent
7124 * track.
7125 */
7126 if (ptrack) {
7127 pfmt = ptrack->usrbuf.fmt;
7128 saved_pfmt = ptrack->usrbuf.fmt;
7129 saved_ai.play.pause = ptrack->is_pause;
7130 } else {
7131 pfmt = sc->sc_sound_pparams;
7132 }
7133 if (rtrack) {
7134 rfmt = rtrack->usrbuf.fmt;
7135 saved_rfmt = rtrack->usrbuf.fmt;
7136 saved_ai.record.pause = rtrack->is_pause;
7137 } else {
7138 rfmt = sc->sc_sound_rparams;
7139 }
7140 saved_ai.mode = file->mode;
7141
7142 /*
7143 * Overwrite if specified.
7144 */
7145 mode = file->mode;
7146 if (SPECIFIED(ai->mode)) {
7147 /*
7148 * Setting ai->mode no longer does anything because it's
7149 * prohibited to change playback/recording mode after open
7150 * and AUMODE_PLAY_ALL is obsoleted. However, it still
7151 * keeps the state of AUMODE_PLAY_ALL itself for backward
7152 * compatibility.
7153 * In the internal, only file->mode has the state of
7154 * AUMODE_PLAY_ALL flag and track->mode in both track does
7155 * not have.
7156 */
7157 if ((file->mode & AUMODE_PLAY)) {
7158 mode = (file->mode & (AUMODE_PLAY | AUMODE_RECORD))
7159 | (ai->mode & AUMODE_PLAY_ALL);
7160 }
7161 }
7162
7163 pchanges = audio_track_setinfo_check(ptrack, &pfmt, pi);
7164 if (pchanges == -1) {
7165 #if defined(AUDIO_DEBUG)
7166 TRACEF(1, file, "check play.params failed: "
7167 "%s %ubit %uch %uHz",
7168 audio_encoding_name(pi->encoding),
7169 pi->precision,
7170 pi->channels,
7171 pi->sample_rate);
7172 #endif
7173 return EINVAL;
7174 }
7175
7176 rchanges = audio_track_setinfo_check(rtrack, &rfmt, ri);
7177 if (rchanges == -1) {
7178 #if defined(AUDIO_DEBUG)
7179 TRACEF(1, file, "check record.params failed: "
7180 "%s %ubit %uch %uHz",
7181 audio_encoding_name(ri->encoding),
7182 ri->precision,
7183 ri->channels,
7184 ri->sample_rate);
7185 #endif
7186 return EINVAL;
7187 }
7188
7189 if (SPECIFIED(ai->mode)) {
7190 pchanges = 1;
7191 rchanges = 1;
7192 }
7193
7194 /*
7195 * Even when setting either one of playback and recording,
7196 * both track must be halted.
7197 */
7198 if (pchanges || rchanges) {
7199 audio_file_clear(sc, file);
7200 #if defined(AUDIO_DEBUG)
7201 char nbuf[16];
7202 char fmtbuf[64];
7203 if (pchanges) {
7204 if (ptrack) {
7205 snprintf(nbuf, sizeof(nbuf), "%d", ptrack->id);
7206 } else {
7207 snprintf(nbuf, sizeof(nbuf), "-");
7208 }
7209 audio_format2_tostr(fmtbuf, sizeof(fmtbuf), &pfmt);
7210 DPRINTF(1, "audio track#%s play mode: %s\n",
7211 nbuf, fmtbuf);
7212 }
7213 if (rchanges) {
7214 if (rtrack) {
7215 snprintf(nbuf, sizeof(nbuf), "%d", rtrack->id);
7216 } else {
7217 snprintf(nbuf, sizeof(nbuf), "-");
7218 }
7219 audio_format2_tostr(fmtbuf, sizeof(fmtbuf), &rfmt);
7220 DPRINTF(1, "audio track#%s rec mode: %s\n",
7221 nbuf, fmtbuf);
7222 }
7223 #endif
7224 }
7225
7226 /* Set mixer parameters */
7227 mutex_enter(sc->sc_lock);
7228 error = audio_hw_setinfo(sc, ai, &saved_ai);
7229 mutex_exit(sc->sc_lock);
7230 if (error)
7231 goto abort1;
7232
7233 /*
7234 * Set to track and update sticky parameters.
7235 */
7236 error = 0;
7237 file->mode = mode;
7238
7239 if (SPECIFIED_CH(pi->pause)) {
7240 if (ptrack)
7241 ptrack->is_pause = pi->pause;
7242 sc->sc_sound_ppause = pi->pause;
7243 }
7244 if (pchanges) {
7245 if (ptrack) {
7246 audio_track_lock_enter(ptrack);
7247 error = audio_track_set_format(ptrack, &pfmt);
7248 audio_track_lock_exit(ptrack);
7249 if (error) {
7250 TRACET(1, ptrack, "set play.params failed");
7251 goto abort2;
7252 }
7253 }
7254 sc->sc_sound_pparams = pfmt;
7255 }
7256 /* Change water marks after initializing the buffers. */
7257 if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
7258 if (ptrack)
7259 audio_track_setinfo_water(ptrack, ai);
7260 }
7261
7262 if (SPECIFIED_CH(ri->pause)) {
7263 if (rtrack)
7264 rtrack->is_pause = ri->pause;
7265 sc->sc_sound_rpause = ri->pause;
7266 }
7267 if (rchanges) {
7268 if (rtrack) {
7269 audio_track_lock_enter(rtrack);
7270 error = audio_track_set_format(rtrack, &rfmt);
7271 audio_track_lock_exit(rtrack);
7272 if (error) {
7273 TRACET(1, rtrack, "set record.params failed");
7274 goto abort3;
7275 }
7276 }
7277 sc->sc_sound_rparams = rfmt;
7278 }
7279
7280 return 0;
7281
7282 /* Rollback */
7283 abort3:
7284 if (error != ENOMEM) {
7285 rtrack->is_pause = saved_ai.record.pause;
7286 audio_track_lock_enter(rtrack);
7287 audio_track_set_format(rtrack, &saved_rfmt);
7288 audio_track_lock_exit(rtrack);
7289 }
7290 sc->sc_sound_rpause = saved_ai.record.pause;
7291 sc->sc_sound_rparams = saved_rfmt;
7292 abort2:
7293 if (ptrack && error != ENOMEM) {
7294 ptrack->is_pause = saved_ai.play.pause;
7295 audio_track_lock_enter(ptrack);
7296 audio_track_set_format(ptrack, &saved_pfmt);
7297 audio_track_lock_exit(ptrack);
7298 }
7299 sc->sc_sound_ppause = saved_ai.play.pause;
7300 sc->sc_sound_pparams = saved_pfmt;
7301 file->mode = saved_ai.mode;
7302 abort1:
7303 mutex_enter(sc->sc_lock);
7304 audio_hw_setinfo(sc, &saved_ai, NULL);
7305 mutex_exit(sc->sc_lock);
7306
7307 return error;
7308 }
7309
7310 /*
7311 * Write SPECIFIED() parameters within info back to fmt.
7312 * Note that track can be NULL here.
7313 * Return value of 1 indicates that fmt is modified.
7314 * Return value of 0 indicates that fmt is not modified.
7315 * Return value of -1 indicates that error EINVAL has occurred.
7316 */
7317 static int
7318 audio_track_setinfo_check(audio_track_t *track,
7319 audio_format2_t *fmt, const struct audio_prinfo *info)
7320 {
7321 const audio_format2_t *hwfmt;
7322 int changes;
7323
7324 changes = 0;
7325 if (SPECIFIED(info->sample_rate)) {
7326 if (info->sample_rate < AUDIO_MIN_FREQUENCY)
7327 return -1;
7328 if (info->sample_rate > AUDIO_MAX_FREQUENCY)
7329 return -1;
7330 fmt->sample_rate = info->sample_rate;
7331 changes = 1;
7332 }
7333 if (SPECIFIED(info->encoding)) {
7334 fmt->encoding = info->encoding;
7335 changes = 1;
7336 }
7337 if (SPECIFIED(info->precision)) {
7338 fmt->precision = info->precision;
7339 /* we don't have API to specify stride */
7340 fmt->stride = info->precision;
7341 changes = 1;
7342 }
7343 if (SPECIFIED(info->channels)) {
7344 /*
7345 * We can convert between monaural and stereo each other.
7346 * We can reduce than the number of channels that the hardware
7347 * supports.
7348 */
7349 if (info->channels > 2) {
7350 if (track) {
7351 hwfmt = &track->mixer->hwbuf.fmt;
7352 if (info->channels > hwfmt->channels)
7353 return -1;
7354 } else {
7355 /*
7356 * This should never happen.
7357 * If track == NULL, channels should be <= 2.
7358 */
7359 return -1;
7360 }
7361 }
7362 fmt->channels = info->channels;
7363 changes = 1;
7364 }
7365
7366 if (changes) {
7367 if (audio_check_params(fmt) != 0)
7368 return -1;
7369 }
7370
7371 return changes;
7372 }
7373
7374 /*
7375 * Change water marks for playback track if specified.
7376 */
7377 static void
7378 audio_track_setinfo_water(audio_track_t *track, const struct audio_info *ai)
7379 {
7380 u_int blks;
7381 u_int maxblks;
7382 u_int blksize;
7383
7384 KASSERT(audio_track_is_playback(track));
7385
7386 blksize = track->usrbuf_blksize;
7387 maxblks = track->usrbuf.capacity / blksize;
7388
7389 if (SPECIFIED(ai->hiwat)) {
7390 blks = ai->hiwat;
7391 if (blks > maxblks)
7392 blks = maxblks;
7393 if (blks < 2)
7394 blks = 2;
7395 track->usrbuf_usedhigh = blks * blksize;
7396 }
7397 if (SPECIFIED(ai->lowat)) {
7398 blks = ai->lowat;
7399 if (blks > maxblks - 1)
7400 blks = maxblks - 1;
7401 track->usrbuf_usedlow = blks * blksize;
7402 }
7403 if (SPECIFIED(ai->hiwat) || SPECIFIED(ai->lowat)) {
7404 if (track->usrbuf_usedlow > track->usrbuf_usedhigh - blksize) {
7405 track->usrbuf_usedlow = track->usrbuf_usedhigh -
7406 blksize;
7407 }
7408 }
7409 }
7410
7411 /*
7412 * Set hardware part of *newai.
7413 * The parameters handled here are *.port, *.gain, *.balance and monitor_gain.
7414 * If oldai is specified, previous parameters are stored.
7415 * This function itself does not roll back if error occurred.
7416 * Must be called with sc_lock && sc_exlock held.
7417 */
7418 static int
7419 audio_hw_setinfo(struct audio_softc *sc, const struct audio_info *newai,
7420 struct audio_info *oldai)
7421 {
7422 const struct audio_prinfo *newpi;
7423 const struct audio_prinfo *newri;
7424 struct audio_prinfo *oldpi;
7425 struct audio_prinfo *oldri;
7426 u_int pgain;
7427 u_int rgain;
7428 u_char pbalance;
7429 u_char rbalance;
7430 int error;
7431
7432 KASSERT(mutex_owned(sc->sc_lock));
7433 KASSERT(sc->sc_exlock);
7434
7435 /* XXX shut up gcc */
7436 oldpi = NULL;
7437 oldri = NULL;
7438
7439 newpi = &newai->play;
7440 newri = &newai->record;
7441 if (oldai) {
7442 oldpi = &oldai->play;
7443 oldri = &oldai->record;
7444 }
7445 error = 0;
7446
7447 /*
7448 * It looks like unnecessary to halt HW mixers to set HW mixers.
7449 * mixer_ioctl(MIXER_WRITE) also doesn't halt.
7450 */
7451
7452 if (SPECIFIED(newpi->port)) {
7453 if (oldai)
7454 oldpi->port = au_get_port(sc, &sc->sc_outports);
7455 error = au_set_port(sc, &sc->sc_outports, newpi->port);
7456 if (error) {
7457 audio_printf(sc,
7458 "setting play.port=%d failed: errno=%d\n",
7459 newpi->port, error);
7460 goto abort;
7461 }
7462 }
7463 if (SPECIFIED(newri->port)) {
7464 if (oldai)
7465 oldri->port = au_get_port(sc, &sc->sc_inports);
7466 error = au_set_port(sc, &sc->sc_inports, newri->port);
7467 if (error) {
7468 audio_printf(sc,
7469 "setting record.port=%d failed: errno=%d\n",
7470 newri->port, error);
7471 goto abort;
7472 }
7473 }
7474
7475 /* play.{gain,balance} */
7476 if (SPECIFIED(newpi->gain) || SPECIFIED_CH(newpi->balance)) {
7477 au_get_gain(sc, &sc->sc_outports, &pgain, &pbalance);
7478 if (oldai) {
7479 oldpi->gain = pgain;
7480 oldpi->balance = pbalance;
7481 }
7482
7483 if (SPECIFIED(newpi->gain))
7484 pgain = newpi->gain;
7485 if (SPECIFIED_CH(newpi->balance))
7486 pbalance = newpi->balance;
7487 error = au_set_gain(sc, &sc->sc_outports, pgain, pbalance);
7488 if (error) {
7489 audio_printf(sc,
7490 "setting play.gain=%d/balance=%d failed: "
7491 "errno=%d\n",
7492 pgain, pbalance, error);
7493 goto abort;
7494 }
7495 }
7496
7497 /* record.{gain,balance} */
7498 if (SPECIFIED(newri->gain) || SPECIFIED_CH(newri->balance)) {
7499 au_get_gain(sc, &sc->sc_inports, &rgain, &rbalance);
7500 if (oldai) {
7501 oldri->gain = rgain;
7502 oldri->balance = rbalance;
7503 }
7504
7505 if (SPECIFIED(newri->gain))
7506 rgain = newri->gain;
7507 if (SPECIFIED_CH(newri->balance))
7508 rbalance = newri->balance;
7509 error = au_set_gain(sc, &sc->sc_inports, rgain, rbalance);
7510 if (error) {
7511 audio_printf(sc,
7512 "setting record.gain=%d/balance=%d failed: "
7513 "errno=%d\n",
7514 rgain, rbalance, error);
7515 goto abort;
7516 }
7517 }
7518
7519 if (SPECIFIED(newai->monitor_gain) && sc->sc_monitor_port != -1) {
7520 if (oldai)
7521 oldai->monitor_gain = au_get_monitor_gain(sc);
7522 error = au_set_monitor_gain(sc, newai->monitor_gain);
7523 if (error) {
7524 audio_printf(sc,
7525 "setting monitor_gain=%d failed: errno=%d\n",
7526 newai->monitor_gain, error);
7527 goto abort;
7528 }
7529 }
7530
7531 /* XXX TODO */
7532 /* sc->sc_ai = *ai; */
7533
7534 error = 0;
7535 abort:
7536 return error;
7537 }
7538
7539 /*
7540 * Setup the hardware with mixer format phwfmt, rhwfmt.
7541 * The arguments have following restrictions:
7542 * - setmode is the direction you want to set, AUMODE_PLAY or AUMODE_RECORD,
7543 * or both.
7544 * - phwfmt and rhwfmt must not be NULL regardless of setmode.
7545 * - On non-independent devices, phwfmt and rhwfmt must have the same
7546 * parameters.
7547 * - pfil and rfil must be zero-filled.
7548 * If successful,
7549 * - pfil, rfil will be filled with filter information specified by the
7550 * hardware driver if necessary.
7551 * and then returns 0. Otherwise returns errno.
7552 * Must be called without sc_lock held.
7553 */
7554 static int
7555 audio_hw_set_format(struct audio_softc *sc, int setmode,
7556 const audio_format2_t *phwfmt, const audio_format2_t *rhwfmt,
7557 audio_filter_reg_t *pfil, audio_filter_reg_t *rfil)
7558 {
7559 audio_params_t pp, rp;
7560 int error;
7561
7562 KASSERT(phwfmt != NULL);
7563 KASSERT(rhwfmt != NULL);
7564
7565 pp = format2_to_params(phwfmt);
7566 rp = format2_to_params(rhwfmt);
7567
7568 mutex_enter(sc->sc_lock);
7569 error = sc->hw_if->set_format(sc->hw_hdl, setmode,
7570 &pp, &rp, pfil, rfil);
7571 if (error) {
7572 mutex_exit(sc->sc_lock);
7573 audio_printf(sc, "set_format failed: errno=%d\n", error);
7574 return error;
7575 }
7576
7577 if (sc->hw_if->commit_settings) {
7578 error = sc->hw_if->commit_settings(sc->hw_hdl);
7579 if (error) {
7580 mutex_exit(sc->sc_lock);
7581 audio_printf(sc,
7582 "commit_settings failed: errno=%d\n", error);
7583 return error;
7584 }
7585 }
7586 mutex_exit(sc->sc_lock);
7587
7588 return 0;
7589 }
7590
7591 /*
7592 * Fill audio_info structure. If need_mixerinfo is true, it will also
7593 * fill the hardware mixer information.
7594 * Must be called with sc_exlock held and without sc_lock held.
7595 */
7596 static int
7597 audiogetinfo(struct audio_softc *sc, struct audio_info *ai, int need_mixerinfo,
7598 audio_file_t *file)
7599 {
7600 struct audio_prinfo *ri, *pi;
7601 audio_track_t *track;
7602 audio_track_t *ptrack;
7603 audio_track_t *rtrack;
7604 int gain;
7605
7606 KASSERT(sc->sc_exlock);
7607
7608 ri = &ai->record;
7609 pi = &ai->play;
7610 ptrack = file->ptrack;
7611 rtrack = file->rtrack;
7612
7613 memset(ai, 0, sizeof(*ai));
7614
7615 if (ptrack) {
7616 pi->sample_rate = ptrack->usrbuf.fmt.sample_rate;
7617 pi->channels = ptrack->usrbuf.fmt.channels;
7618 pi->precision = ptrack->usrbuf.fmt.precision;
7619 pi->encoding = ptrack->usrbuf.fmt.encoding;
7620 pi->pause = ptrack->is_pause;
7621 } else {
7622 /* Use sticky parameters if the track is not available. */
7623 pi->sample_rate = sc->sc_sound_pparams.sample_rate;
7624 pi->channels = sc->sc_sound_pparams.channels;
7625 pi->precision = sc->sc_sound_pparams.precision;
7626 pi->encoding = sc->sc_sound_pparams.encoding;
7627 pi->pause = sc->sc_sound_ppause;
7628 }
7629 if (rtrack) {
7630 ri->sample_rate = rtrack->usrbuf.fmt.sample_rate;
7631 ri->channels = rtrack->usrbuf.fmt.channels;
7632 ri->precision = rtrack->usrbuf.fmt.precision;
7633 ri->encoding = rtrack->usrbuf.fmt.encoding;
7634 ri->pause = rtrack->is_pause;
7635 } else {
7636 /* Use sticky parameters if the track is not available. */
7637 ri->sample_rate = sc->sc_sound_rparams.sample_rate;
7638 ri->channels = sc->sc_sound_rparams.channels;
7639 ri->precision = sc->sc_sound_rparams.precision;
7640 ri->encoding = sc->sc_sound_rparams.encoding;
7641 ri->pause = sc->sc_sound_rpause;
7642 }
7643
7644 if (ptrack) {
7645 pi->seek = ptrack->usrbuf.used;
7646 pi->samples = ptrack->usrbuf_stamp;
7647 pi->eof = ptrack->eofcounter;
7648 pi->error = (ptrack->dropframes != 0) ? 1 : 0;
7649 pi->open = 1;
7650 pi->buffer_size = ptrack->usrbuf.capacity;
7651 }
7652 pi->waiting = 0; /* open never hangs */
7653 pi->active = sc->sc_pbusy;
7654
7655 if (rtrack) {
7656 ri->seek = rtrack->usrbuf.used;
7657 ri->samples = rtrack->usrbuf_stamp;
7658 ri->eof = 0;
7659 ri->error = (rtrack->dropframes != 0) ? 1 : 0;
7660 ri->open = 1;
7661 ri->buffer_size = rtrack->usrbuf.capacity;
7662 }
7663 ri->waiting = 0; /* open never hangs */
7664 ri->active = sc->sc_rbusy;
7665
7666 /*
7667 * XXX There may be different number of channels between playback
7668 * and recording, so that blocksize also may be different.
7669 * But struct audio_info has an united blocksize...
7670 * Here, I use play info precedencely if ptrack is available,
7671 * otherwise record info.
7672 *
7673 * XXX hiwat/lowat is a playback-only parameter. What should I
7674 * return for a record-only descriptor?
7675 */
7676 track = ptrack ? ptrack : rtrack;
7677 if (track) {
7678 ai->blocksize = track->usrbuf_blksize;
7679 ai->hiwat = track->usrbuf_usedhigh / track->usrbuf_blksize;
7680 ai->lowat = track->usrbuf_usedlow / track->usrbuf_blksize;
7681 }
7682 ai->mode = file->mode;
7683
7684 /*
7685 * For backward compatibility, we have to pad these five fields
7686 * a fake non-zero value even if there are no tracks.
7687 */
7688 if (ptrack == NULL)
7689 pi->buffer_size = 65536;
7690 if (rtrack == NULL)
7691 ri->buffer_size = 65536;
7692 if (ptrack == NULL && rtrack == NULL) {
7693 ai->blocksize = 2048;
7694 ai->hiwat = ai->play.buffer_size / ai->blocksize;
7695 ai->lowat = ai->hiwat * 3 / 4;
7696 }
7697
7698 if (need_mixerinfo) {
7699 mutex_enter(sc->sc_lock);
7700
7701 pi->port = au_get_port(sc, &sc->sc_outports);
7702 ri->port = au_get_port(sc, &sc->sc_inports);
7703
7704 pi->avail_ports = sc->sc_outports.allports;
7705 ri->avail_ports = sc->sc_inports.allports;
7706
7707 au_get_gain(sc, &sc->sc_outports, &pi->gain, &pi->balance);
7708 au_get_gain(sc, &sc->sc_inports, &ri->gain, &ri->balance);
7709
7710 if (sc->sc_monitor_port != -1) {
7711 gain = au_get_monitor_gain(sc);
7712 if (gain != -1)
7713 ai->monitor_gain = gain;
7714 }
7715 mutex_exit(sc->sc_lock);
7716 }
7717
7718 return 0;
7719 }
7720
7721 /*
7722 * Return true if playback is configured.
7723 * This function can be used after audioattach.
7724 */
7725 static bool
7726 audio_can_playback(struct audio_softc *sc)
7727 {
7728
7729 return (sc->sc_pmixer != NULL);
7730 }
7731
7732 /*
7733 * Return true if recording is configured.
7734 * This function can be used after audioattach.
7735 */
7736 static bool
7737 audio_can_capture(struct audio_softc *sc)
7738 {
7739
7740 return (sc->sc_rmixer != NULL);
7741 }
7742
7743 /*
7744 * Get the afp->index'th item from the valid one of format[].
7745 * If found, stores it to afp->fmt and returns 0. Otherwise return EINVAL.
7746 *
7747 * This is common routines for query_format.
7748 * If your hardware driver has struct audio_format[], the simplest case
7749 * you can write your query_format interface as follows:
7750 *
7751 * struct audio_format foo_format[] = { ... };
7752 *
7753 * int
7754 * foo_query_format(void *hdl, audio_format_query_t *afp)
7755 * {
7756 * return audio_query_format(foo_format, __arraycount(foo_format), afp);
7757 * }
7758 */
7759 int
7760 audio_query_format(const struct audio_format *format, int nformats,
7761 audio_format_query_t *afp)
7762 {
7763 const struct audio_format *f;
7764 int idx;
7765 int i;
7766
7767 idx = 0;
7768 for (i = 0; i < nformats; i++) {
7769 f = &format[i];
7770 if (!AUFMT_IS_VALID(f))
7771 continue;
7772 if (afp->index == idx) {
7773 afp->fmt = *f;
7774 return 0;
7775 }
7776 idx++;
7777 }
7778 return EINVAL;
7779 }
7780
7781 /*
7782 * This function is provided for the hardware driver's set_format() to
7783 * find index matches with 'param' from array of audio_format_t 'formats'.
7784 * 'mode' is either of AUMODE_PLAY or AUMODE_RECORD.
7785 * It returns the matched index and never fails. Because param passed to
7786 * set_format() is selected from query_format().
7787 * This function will be an alternative to auconv_set_converter() to
7788 * find index.
7789 */
7790 int
7791 audio_indexof_format(const struct audio_format *formats, int nformats,
7792 int mode, const audio_params_t *param)
7793 {
7794 const struct audio_format *f;
7795 int index;
7796 int j;
7797
7798 for (index = 0; index < nformats; index++) {
7799 f = &formats[index];
7800
7801 if (!AUFMT_IS_VALID(f))
7802 continue;
7803 if ((f->mode & mode) == 0)
7804 continue;
7805 if (f->encoding != param->encoding)
7806 continue;
7807 if (f->validbits != param->precision)
7808 continue;
7809 if (f->channels != param->channels)
7810 continue;
7811
7812 if (f->frequency_type == 0) {
7813 if (param->sample_rate < f->frequency[0] ||
7814 param->sample_rate > f->frequency[1])
7815 continue;
7816 } else {
7817 for (j = 0; j < f->frequency_type; j++) {
7818 if (param->sample_rate == f->frequency[j])
7819 break;
7820 }
7821 if (j == f->frequency_type)
7822 continue;
7823 }
7824
7825 /* Then, matched */
7826 return index;
7827 }
7828
7829 /* Not matched. This should not be happened. */
7830 panic("%s: cannot find matched format\n", __func__);
7831 }
7832
7833 /*
7834 * Get or set hardware blocksize in msec.
7835 * XXX It's for debug.
7836 */
7837 static int
7838 audio_sysctl_blk_ms(SYSCTLFN_ARGS)
7839 {
7840 struct sysctlnode node;
7841 struct audio_softc *sc;
7842 audio_format2_t phwfmt;
7843 audio_format2_t rhwfmt;
7844 audio_filter_reg_t pfil;
7845 audio_filter_reg_t rfil;
7846 int t;
7847 int old_blk_ms;
7848 int mode;
7849 int error;
7850
7851 node = *rnode;
7852 sc = node.sysctl_data;
7853
7854 error = audio_exlock_enter(sc);
7855 if (error)
7856 return error;
7857
7858 old_blk_ms = sc->sc_blk_ms;
7859 t = old_blk_ms;
7860 node.sysctl_data = &t;
7861 error = sysctl_lookup(SYSCTLFN_CALL(&node));
7862 if (error || newp == NULL)
7863 goto abort;
7864
7865 if (t < 0) {
7866 error = EINVAL;
7867 goto abort;
7868 }
7869
7870 if (sc->sc_popens + sc->sc_ropens > 0) {
7871 error = EBUSY;
7872 goto abort;
7873 }
7874 sc->sc_blk_ms = t;
7875 mode = 0;
7876 if (sc->sc_pmixer) {
7877 mode |= AUMODE_PLAY;
7878 phwfmt = sc->sc_pmixer->hwbuf.fmt;
7879 }
7880 if (sc->sc_rmixer) {
7881 mode |= AUMODE_RECORD;
7882 rhwfmt = sc->sc_rmixer->hwbuf.fmt;
7883 }
7884
7885 /* re-init hardware */
7886 memset(&pfil, 0, sizeof(pfil));
7887 memset(&rfil, 0, sizeof(rfil));
7888 error = audio_hw_set_format(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
7889 if (error) {
7890 goto abort;
7891 }
7892
7893 /* re-init track mixer */
7894 error = audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
7895 if (error) {
7896 /* Rollback */
7897 sc->sc_blk_ms = old_blk_ms;
7898 audio_mixers_init(sc, mode, &phwfmt, &rhwfmt, &pfil, &rfil);
7899 goto abort;
7900 }
7901 error = 0;
7902 abort:
7903 audio_exlock_exit(sc);
7904 return error;
7905 }
7906
7907 /*
7908 * Get or set multiuser mode.
7909 */
7910 static int
7911 audio_sysctl_multiuser(SYSCTLFN_ARGS)
7912 {
7913 struct sysctlnode node;
7914 struct audio_softc *sc;
7915 bool t;
7916 int error;
7917
7918 node = *rnode;
7919 sc = node.sysctl_data;
7920
7921 error = audio_exlock_enter(sc);
7922 if (error)
7923 return error;
7924
7925 t = sc->sc_multiuser;
7926 node.sysctl_data = &t;
7927 error = sysctl_lookup(SYSCTLFN_CALL(&node));
7928 if (error || newp == NULL)
7929 goto abort;
7930
7931 sc->sc_multiuser = t;
7932 error = 0;
7933 abort:
7934 audio_exlock_exit(sc);
7935 return error;
7936 }
7937
7938 #if defined(AUDIO_DEBUG)
7939 /*
7940 * Get or set debug verbose level. (0..4)
7941 * XXX It's for debug.
7942 * XXX It is not separated per device.
7943 */
7944 static int
7945 audio_sysctl_debug(SYSCTLFN_ARGS)
7946 {
7947 struct sysctlnode node;
7948 int t;
7949 int error;
7950
7951 node = *rnode;
7952 t = audiodebug;
7953 node.sysctl_data = &t;
7954 error = sysctl_lookup(SYSCTLFN_CALL(&node));
7955 if (error || newp == NULL)
7956 return error;
7957
7958 if (t < 0 || t > 4)
7959 return EINVAL;
7960 audiodebug = t;
7961 printf("audio: audiodebug = %d\n", audiodebug);
7962 return 0;
7963 }
7964 #endif /* AUDIO_DEBUG */
7965
7966 #ifdef AUDIO_PM_IDLE
7967 static void
7968 audio_idle(void *arg)
7969 {
7970 device_t dv = arg;
7971 struct audio_softc *sc = device_private(dv);
7972
7973 #ifdef PNP_DEBUG
7974 extern int pnp_debug_idle;
7975 if (pnp_debug_idle)
7976 printf("%s: idle handler called\n", device_xname(dv));
7977 #endif
7978
7979 sc->sc_idle = true;
7980
7981 /* XXX joerg Make pmf_device_suspend handle children? */
7982 if (!pmf_device_suspend(dv, PMF_Q_SELF))
7983 return;
7984
7985 if (!pmf_device_suspend(sc->hw_dev, PMF_Q_SELF))
7986 pmf_device_resume(dv, PMF_Q_SELF);
7987 }
7988
7989 static void
7990 audio_activity(device_t dv, devactive_t type)
7991 {
7992 struct audio_softc *sc = device_private(dv);
7993
7994 if (type != DVA_SYSTEM)
7995 return;
7996
7997 callout_schedule(&sc->sc_idle_counter, audio_idle_timeout * hz);
7998
7999 sc->sc_idle = false;
8000 if (!device_is_active(dv)) {
8001 /* XXX joerg How to deal with a failing resume... */
8002 pmf_device_resume(sc->hw_dev, PMF_Q_SELF);
8003 pmf_device_resume(dv, PMF_Q_SELF);
8004 }
8005 }
8006 #endif
8007
8008 static bool
8009 audio_suspend(device_t dv, const pmf_qual_t *qual)
8010 {
8011 struct audio_softc *sc = device_private(dv);
8012 int error;
8013
8014 error = audio_exlock_mutex_enter(sc);
8015 if (error)
8016 return error;
8017 sc->sc_suspending = true;
8018 audio_mixer_capture(sc);
8019
8020 if (sc->sc_pbusy) {
8021 audio_pmixer_halt(sc);
8022 /* Reuse this as need-to-restart flag while suspending */
8023 sc->sc_pbusy = true;
8024 }
8025 if (sc->sc_rbusy) {
8026 audio_rmixer_halt(sc);
8027 /* Reuse this as need-to-restart flag while suspending */
8028 sc->sc_rbusy = true;
8029 }
8030
8031 #ifdef AUDIO_PM_IDLE
8032 callout_halt(&sc->sc_idle_counter, sc->sc_lock);
8033 #endif
8034 audio_exlock_mutex_exit(sc);
8035
8036 return true;
8037 }
8038
8039 static bool
8040 audio_resume(device_t dv, const pmf_qual_t *qual)
8041 {
8042 struct audio_softc *sc = device_private(dv);
8043 struct audio_info ai;
8044 int error;
8045
8046 error = audio_exlock_mutex_enter(sc);
8047 if (error)
8048 return error;
8049
8050 sc->sc_suspending = false;
8051 audio_mixer_restore(sc);
8052 /* XXX ? */
8053 AUDIO_INITINFO(&ai);
8054 audio_hw_setinfo(sc, &ai, NULL);
8055
8056 /*
8057 * During from suspend to resume here, sc_[pr]busy is used as
8058 * need-to-restart flag temporarily. After this point,
8059 * sc_[pr]busy is returned to its original usage (busy flag).
8060 * And note that sc_[pr]busy must be false to call [pr]mixer_start().
8061 */
8062 if (sc->sc_pbusy) {
8063 /* pmixer_start() requires pbusy is false */
8064 sc->sc_pbusy = false;
8065 audio_pmixer_start(sc, true);
8066 }
8067 if (sc->sc_rbusy) {
8068 /* rmixer_start() requires rbusy is false */
8069 sc->sc_rbusy = false;
8070 audio_rmixer_start(sc);
8071 }
8072
8073 audio_exlock_mutex_exit(sc);
8074
8075 return true;
8076 }
8077
8078 #if defined(AUDIO_DEBUG)
8079 static void
8080 audio_format2_tostr(char *buf, size_t bufsize, const audio_format2_t *fmt)
8081 {
8082 int n;
8083
8084 n = 0;
8085 n += snprintf(buf + n, bufsize - n, "%s",
8086 audio_encoding_name(fmt->encoding));
8087 if (fmt->precision == fmt->stride) {
8088 n += snprintf(buf + n, bufsize - n, " %dbit", fmt->precision);
8089 } else {
8090 n += snprintf(buf + n, bufsize - n, " %d/%dbit",
8091 fmt->precision, fmt->stride);
8092 }
8093
8094 snprintf(buf + n, bufsize - n, " %uch %uHz",
8095 fmt->channels, fmt->sample_rate);
8096 }
8097 #endif
8098
8099 #if defined(AUDIO_DEBUG)
8100 static void
8101 audio_print_format2(const char *s, const audio_format2_t *fmt)
8102 {
8103 char fmtstr[64];
8104
8105 audio_format2_tostr(fmtstr, sizeof(fmtstr), fmt);
8106 printf("%s %s\n", s, fmtstr);
8107 }
8108 #endif
8109
8110 #ifdef DIAGNOSTIC
8111 void
8112 audio_diagnostic_format2(const char *where, const audio_format2_t *fmt)
8113 {
8114
8115 KASSERTMSG(fmt, "called from %s", where);
8116
8117 /* XXX MSM6258 vs(4) only has 4bit stride format. */
8118 if (fmt->encoding == AUDIO_ENCODING_ADPCM) {
8119 KASSERTMSG(fmt->stride == 4 || fmt->stride == 8,
8120 "called from %s: fmt->stride=%d", where, fmt->stride);
8121 } else {
8122 KASSERTMSG(fmt->stride % NBBY == 0,
8123 "called from %s: fmt->stride=%d", where, fmt->stride);
8124 }
8125 KASSERTMSG(fmt->precision <= fmt->stride,
8126 "called from %s: fmt->precision=%d fmt->stride=%d",
8127 where, fmt->precision, fmt->stride);
8128 KASSERTMSG(1 <= fmt->channels && fmt->channels <= AUDIO_MAX_CHANNELS,
8129 "called from %s: fmt->channels=%d", where, fmt->channels);
8130
8131 /* XXX No check for encodings? */
8132 }
8133
8134 void
8135 audio_diagnostic_filter_arg(const char *where, const audio_filter_arg_t *arg)
8136 {
8137
8138 KASSERT(arg != NULL);
8139 KASSERT(arg->src != NULL);
8140 KASSERT(arg->dst != NULL);
8141 audio_diagnostic_format2(where, arg->srcfmt);
8142 audio_diagnostic_format2(where, arg->dstfmt);
8143 KASSERT(arg->count > 0);
8144 }
8145
8146 void
8147 audio_diagnostic_ring(const char *where, const audio_ring_t *ring)
8148 {
8149
8150 KASSERTMSG(ring, "called from %s", where);
8151 audio_diagnostic_format2(where, &ring->fmt);
8152 KASSERTMSG(0 <= ring->capacity && ring->capacity < INT_MAX / 2,
8153 "called from %s: ring->capacity=%d", where, ring->capacity);
8154 KASSERTMSG(0 <= ring->used && ring->used <= ring->capacity,
8155 "called from %s: ring->used=%d ring->capacity=%d",
8156 where, ring->used, ring->capacity);
8157 if (ring->capacity == 0) {
8158 KASSERTMSG(ring->mem == NULL,
8159 "called from %s: capacity == 0 but mem != NULL", where);
8160 } else {
8161 KASSERTMSG(ring->mem != NULL,
8162 "called from %s: capacity != 0 but mem == NULL", where);
8163 KASSERTMSG(0 <= ring->head && ring->head < ring->capacity,
8164 "called from %s: ring->head=%d ring->capacity=%d",
8165 where, ring->head, ring->capacity);
8166 }
8167 }
8168 #endif /* DIAGNOSTIC */
8169
8170
8171 /*
8172 * Mixer driver
8173 */
8174
8175 /*
8176 * Must be called without sc_lock held.
8177 */
8178 int
8179 mixer_open(dev_t dev, struct audio_softc *sc, int flags, int ifmt,
8180 struct lwp *l)
8181 {
8182 struct file *fp;
8183 audio_file_t *af;
8184 int error, fd;
8185
8186 TRACE(1, "flags=0x%x", flags);
8187
8188 error = fd_allocfile(&fp, &fd);
8189 if (error)
8190 return error;
8191
8192 af = kmem_zalloc(sizeof(*af), KM_SLEEP);
8193 af->sc = sc;
8194 af->dev = dev;
8195
8196 mutex_enter(sc->sc_lock);
8197 if (sc->sc_dying) {
8198 mutex_exit(sc->sc_lock);
8199 kmem_free(af, sizeof(*af));
8200 fd_abort(curproc, fp, fd);
8201 return ENXIO;
8202 }
8203 mutex_enter(sc->sc_intr_lock);
8204 SLIST_INSERT_HEAD(&sc->sc_files, af, entry);
8205 mutex_exit(sc->sc_intr_lock);
8206 mutex_exit(sc->sc_lock);
8207
8208 error = fd_clone(fp, fd, flags, &audio_fileops, af);
8209 KASSERT(error == EMOVEFD);
8210
8211 return error;
8212 }
8213
8214 /*
8215 * Add a process to those to be signalled on mixer activity.
8216 * If the process has already been added, do nothing.
8217 * Must be called with sc_exlock held and without sc_lock held.
8218 */
8219 static void
8220 mixer_async_add(struct audio_softc *sc, pid_t pid)
8221 {
8222 int i;
8223
8224 KASSERT(sc->sc_exlock);
8225
8226 /* If already exists, returns without doing anything. */
8227 for (i = 0; i < sc->sc_am_used; i++) {
8228 if (sc->sc_am[i] == pid)
8229 return;
8230 }
8231
8232 /* Extend array if necessary. */
8233 if (sc->sc_am_used >= sc->sc_am_capacity) {
8234 sc->sc_am_capacity += AM_CAPACITY;
8235 sc->sc_am = kern_realloc(sc->sc_am,
8236 sc->sc_am_capacity * sizeof(pid_t), M_WAITOK);
8237 TRACE(2, "realloc am_capacity=%d", sc->sc_am_capacity);
8238 }
8239
8240 TRACE(2, "am[%d]=%d", sc->sc_am_used, (int)pid);
8241 sc->sc_am[sc->sc_am_used++] = pid;
8242 }
8243
8244 /*
8245 * Remove a process from those to be signalled on mixer activity.
8246 * If the process has not been added, do nothing.
8247 * Must be called with sc_exlock held and without sc_lock held.
8248 */
8249 static void
8250 mixer_async_remove(struct audio_softc *sc, pid_t pid)
8251 {
8252 int i;
8253
8254 KASSERT(sc->sc_exlock);
8255
8256 for (i = 0; i < sc->sc_am_used; i++) {
8257 if (sc->sc_am[i] == pid) {
8258 sc->sc_am[i] = sc->sc_am[--sc->sc_am_used];
8259 TRACE(2, "am[%d](%d) removed, used=%d",
8260 i, (int)pid, sc->sc_am_used);
8261
8262 /* Empty array if no longer necessary. */
8263 if (sc->sc_am_used == 0) {
8264 kern_free(sc->sc_am);
8265 sc->sc_am = NULL;
8266 sc->sc_am_capacity = 0;
8267 TRACE(2, "released");
8268 }
8269 return;
8270 }
8271 }
8272 }
8273
8274 /*
8275 * Signal all processes waiting for the mixer.
8276 * Must be called with sc_exlock held.
8277 */
8278 static void
8279 mixer_signal(struct audio_softc *sc)
8280 {
8281 proc_t *p;
8282 int i;
8283
8284 KASSERT(sc->sc_exlock);
8285
8286 for (i = 0; i < sc->sc_am_used; i++) {
8287 mutex_enter(&proc_lock);
8288 p = proc_find(sc->sc_am[i]);
8289 if (p)
8290 psignal(p, SIGIO);
8291 mutex_exit(&proc_lock);
8292 }
8293 }
8294
8295 /*
8296 * Close a mixer device
8297 */
8298 int
8299 mixer_close(struct audio_softc *sc, audio_file_t *file)
8300 {
8301 int error;
8302
8303 error = audio_exlock_enter(sc);
8304 if (error)
8305 return error;
8306 TRACE(1, "called");
8307 mixer_async_remove(sc, curproc->p_pid);
8308 audio_exlock_exit(sc);
8309
8310 return 0;
8311 }
8312
8313 /*
8314 * Must be called without sc_lock nor sc_exlock held.
8315 */
8316 int
8317 mixer_ioctl(struct audio_softc *sc, u_long cmd, void *addr, int flag,
8318 struct lwp *l)
8319 {
8320 mixer_devinfo_t *mi;
8321 mixer_ctrl_t *mc;
8322 int error;
8323
8324 TRACE(2, "(%lu,'%c',%lu)",
8325 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff);
8326 error = EINVAL;
8327
8328 /* we can return cached values if we are sleeping */
8329 if (cmd != AUDIO_MIXER_READ) {
8330 mutex_enter(sc->sc_lock);
8331 device_active(sc->sc_dev, DVA_SYSTEM);
8332 mutex_exit(sc->sc_lock);
8333 }
8334
8335 switch (cmd) {
8336 case FIOASYNC:
8337 error = audio_exlock_enter(sc);
8338 if (error)
8339 break;
8340 if (*(int *)addr) {
8341 mixer_async_add(sc, curproc->p_pid);
8342 } else {
8343 mixer_async_remove(sc, curproc->p_pid);
8344 }
8345 audio_exlock_exit(sc);
8346 break;
8347
8348 case AUDIO_GETDEV:
8349 TRACE(2, "AUDIO_GETDEV");
8350 error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
8351 break;
8352
8353 case AUDIO_MIXER_DEVINFO:
8354 TRACE(2, "AUDIO_MIXER_DEVINFO");
8355 mi = (mixer_devinfo_t *)addr;
8356
8357 mi->un.v.delta = 0; /* default */
8358 mutex_enter(sc->sc_lock);
8359 error = audio_query_devinfo(sc, mi);
8360 mutex_exit(sc->sc_lock);
8361 break;
8362
8363 case AUDIO_MIXER_READ:
8364 TRACE(2, "AUDIO_MIXER_READ");
8365 mc = (mixer_ctrl_t *)addr;
8366
8367 error = audio_exlock_mutex_enter(sc);
8368 if (error)
8369 break;
8370 if (device_is_active(sc->hw_dev))
8371 error = audio_get_port(sc, mc);
8372 else if (mc->dev < 0 || mc->dev >= sc->sc_nmixer_states)
8373 error = ENXIO;
8374 else {
8375 int dev = mc->dev;
8376 memcpy(mc, &sc->sc_mixer_state[dev],
8377 sizeof(mixer_ctrl_t));
8378 error = 0;
8379 }
8380 audio_exlock_mutex_exit(sc);
8381 break;
8382
8383 case AUDIO_MIXER_WRITE:
8384 TRACE(2, "AUDIO_MIXER_WRITE");
8385 error = audio_exlock_mutex_enter(sc);
8386 if (error)
8387 break;
8388 error = audio_set_port(sc, (mixer_ctrl_t *)addr);
8389 if (error) {
8390 audio_exlock_mutex_exit(sc);
8391 break;
8392 }
8393
8394 if (sc->hw_if->commit_settings) {
8395 error = sc->hw_if->commit_settings(sc->hw_hdl);
8396 if (error) {
8397 audio_exlock_mutex_exit(sc);
8398 break;
8399 }
8400 }
8401 mutex_exit(sc->sc_lock);
8402 mixer_signal(sc);
8403 audio_exlock_exit(sc);
8404 break;
8405
8406 default:
8407 if (sc->hw_if->dev_ioctl) {
8408 mutex_enter(sc->sc_lock);
8409 error = sc->hw_if->dev_ioctl(sc->hw_hdl,
8410 cmd, addr, flag, l);
8411 mutex_exit(sc->sc_lock);
8412 } else
8413 error = EINVAL;
8414 break;
8415 }
8416 TRACE(2, "(%lu,'%c',%lu) result %d",
8417 IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd & 0xff, error);
8418 return error;
8419 }
8420
8421 /*
8422 * Must be called with sc_lock held.
8423 */
8424 int
8425 au_portof(struct audio_softc *sc, char *name, int class)
8426 {
8427 mixer_devinfo_t mi;
8428
8429 KASSERT(mutex_owned(sc->sc_lock));
8430
8431 for (mi.index = 0; audio_query_devinfo(sc, &mi) == 0; mi.index++) {
8432 if (mi.mixer_class == class && strcmp(mi.label.name, name) == 0)
8433 return mi.index;
8434 }
8435 return -1;
8436 }
8437
8438 /*
8439 * Must be called with sc_lock held.
8440 */
8441 void
8442 au_setup_ports(struct audio_softc *sc, struct au_mixer_ports *ports,
8443 mixer_devinfo_t *mi, const struct portname *tbl)
8444 {
8445 int i, j;
8446
8447 KASSERT(mutex_owned(sc->sc_lock));
8448
8449 ports->index = mi->index;
8450 if (mi->type == AUDIO_MIXER_ENUM) {
8451 ports->isenum = true;
8452 for(i = 0; tbl[i].name; i++)
8453 for(j = 0; j < mi->un.e.num_mem; j++)
8454 if (strcmp(mi->un.e.member[j].label.name,
8455 tbl[i].name) == 0) {
8456 ports->allports |= tbl[i].mask;
8457 ports->aumask[ports->nports] = tbl[i].mask;
8458 ports->misel[ports->nports] =
8459 mi->un.e.member[j].ord;
8460 ports->miport[ports->nports] =
8461 au_portof(sc, mi->un.e.member[j].label.name,
8462 mi->mixer_class);
8463 if (ports->mixerout != -1 &&
8464 ports->miport[ports->nports] != -1)
8465 ports->isdual = true;
8466 ++ports->nports;
8467 }
8468 } else if (mi->type == AUDIO_MIXER_SET) {
8469 for(i = 0; tbl[i].name; i++)
8470 for(j = 0; j < mi->un.s.num_mem; j++)
8471 if (strcmp(mi->un.s.member[j].label.name,
8472 tbl[i].name) == 0) {
8473 ports->allports |= tbl[i].mask;
8474 ports->aumask[ports->nports] = tbl[i].mask;
8475 ports->misel[ports->nports] =
8476 mi->un.s.member[j].mask;
8477 ports->miport[ports->nports] =
8478 au_portof(sc, mi->un.s.member[j].label.name,
8479 mi->mixer_class);
8480 ++ports->nports;
8481 }
8482 }
8483 }
8484
8485 /*
8486 * Must be called with sc_lock && sc_exlock held.
8487 */
8488 int
8489 au_set_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int l, int r)
8490 {
8491
8492 KASSERT(mutex_owned(sc->sc_lock));
8493 KASSERT(sc->sc_exlock);
8494
8495 ct->type = AUDIO_MIXER_VALUE;
8496 ct->un.value.num_channels = 2;
8497 ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = l;
8498 ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = r;
8499 if (audio_set_port(sc, ct) == 0)
8500 return 0;
8501 ct->un.value.num_channels = 1;
8502 ct->un.value.level[AUDIO_MIXER_LEVEL_MONO] = (l+r)/2;
8503 return audio_set_port(sc, ct);
8504 }
8505
8506 /*
8507 * Must be called with sc_lock && sc_exlock held.
8508 */
8509 int
8510 au_get_lr_value(struct audio_softc *sc, mixer_ctrl_t *ct, int *l, int *r)
8511 {
8512 int error;
8513
8514 KASSERT(mutex_owned(sc->sc_lock));
8515 KASSERT(sc->sc_exlock);
8516
8517 ct->un.value.num_channels = 2;
8518 if (audio_get_port(sc, ct) == 0) {
8519 *l = ct->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
8520 *r = ct->un.value.level[AUDIO_MIXER_LEVEL_RIGHT];
8521 } else {
8522 ct->un.value.num_channels = 1;
8523 error = audio_get_port(sc, ct);
8524 if (error)
8525 return error;
8526 *r = *l = ct->un.value.level[AUDIO_MIXER_LEVEL_MONO];
8527 }
8528 return 0;
8529 }
8530
8531 /*
8532 * Must be called with sc_lock && sc_exlock held.
8533 */
8534 int
8535 au_set_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
8536 int gain, int balance)
8537 {
8538 mixer_ctrl_t ct;
8539 int i, error;
8540 int l, r;
8541 u_int mask;
8542 int nset;
8543
8544 KASSERT(mutex_owned(sc->sc_lock));
8545 KASSERT(sc->sc_exlock);
8546
8547 if (balance == AUDIO_MID_BALANCE) {
8548 l = r = gain;
8549 } else if (balance < AUDIO_MID_BALANCE) {
8550 l = gain;
8551 r = (balance * gain) / AUDIO_MID_BALANCE;
8552 } else {
8553 r = gain;
8554 l = ((AUDIO_RIGHT_BALANCE - balance) * gain)
8555 / AUDIO_MID_BALANCE;
8556 }
8557 TRACE(2, "gain=%d balance=%d, l=%d r=%d", gain, balance, l, r);
8558
8559 if (ports->index == -1) {
8560 usemaster:
8561 if (ports->master == -1)
8562 return 0; /* just ignore it silently */
8563 ct.dev = ports->master;
8564 error = au_set_lr_value(sc, &ct, l, r);
8565 } else {
8566 ct.dev = ports->index;
8567 if (ports->isenum) {
8568 ct.type = AUDIO_MIXER_ENUM;
8569 error = audio_get_port(sc, &ct);
8570 if (error)
8571 return error;
8572 if (ports->isdual) {
8573 if (ports->cur_port == -1)
8574 ct.dev = ports->master;
8575 else
8576 ct.dev = ports->miport[ports->cur_port];
8577 error = au_set_lr_value(sc, &ct, l, r);
8578 } else {
8579 for(i = 0; i < ports->nports; i++)
8580 if (ports->misel[i] == ct.un.ord) {
8581 ct.dev = ports->miport[i];
8582 if (ct.dev == -1 ||
8583 au_set_lr_value(sc, &ct, l, r))
8584 goto usemaster;
8585 else
8586 break;
8587 }
8588 }
8589 } else {
8590 ct.type = AUDIO_MIXER_SET;
8591 error = audio_get_port(sc, &ct);
8592 if (error)
8593 return error;
8594 mask = ct.un.mask;
8595 nset = 0;
8596 for(i = 0; i < ports->nports; i++) {
8597 if (ports->misel[i] & mask) {
8598 ct.dev = ports->miport[i];
8599 if (ct.dev != -1 &&
8600 au_set_lr_value(sc, &ct, l, r) == 0)
8601 nset++;
8602 }
8603 }
8604 if (nset == 0)
8605 goto usemaster;
8606 }
8607 }
8608 if (!error)
8609 mixer_signal(sc);
8610 return error;
8611 }
8612
8613 /*
8614 * Must be called with sc_lock && sc_exlock held.
8615 */
8616 void
8617 au_get_gain(struct audio_softc *sc, struct au_mixer_ports *ports,
8618 u_int *pgain, u_char *pbalance)
8619 {
8620 mixer_ctrl_t ct;
8621 int i, l, r, n;
8622 int lgain, rgain;
8623
8624 KASSERT(mutex_owned(sc->sc_lock));
8625 KASSERT(sc->sc_exlock);
8626
8627 lgain = AUDIO_MAX_GAIN / 2;
8628 rgain = AUDIO_MAX_GAIN / 2;
8629 if (ports->index == -1) {
8630 usemaster:
8631 if (ports->master == -1)
8632 goto bad;
8633 ct.dev = ports->master;
8634 ct.type = AUDIO_MIXER_VALUE;
8635 if (au_get_lr_value(sc, &ct, &lgain, &rgain))
8636 goto bad;
8637 } else {
8638 ct.dev = ports->index;
8639 if (ports->isenum) {
8640 ct.type = AUDIO_MIXER_ENUM;
8641 if (audio_get_port(sc, &ct))
8642 goto bad;
8643 ct.type = AUDIO_MIXER_VALUE;
8644 if (ports->isdual) {
8645 if (ports->cur_port == -1)
8646 ct.dev = ports->master;
8647 else
8648 ct.dev = ports->miport[ports->cur_port];
8649 au_get_lr_value(sc, &ct, &lgain, &rgain);
8650 } else {
8651 for(i = 0; i < ports->nports; i++)
8652 if (ports->misel[i] == ct.un.ord) {
8653 ct.dev = ports->miport[i];
8654 if (ct.dev == -1 ||
8655 au_get_lr_value(sc, &ct,
8656 &lgain, &rgain))
8657 goto usemaster;
8658 else
8659 break;
8660 }
8661 }
8662 } else {
8663 ct.type = AUDIO_MIXER_SET;
8664 if (audio_get_port(sc, &ct))
8665 goto bad;
8666 ct.type = AUDIO_MIXER_VALUE;
8667 lgain = rgain = n = 0;
8668 for(i = 0; i < ports->nports; i++) {
8669 if (ports->misel[i] & ct.un.mask) {
8670 ct.dev = ports->miport[i];
8671 if (ct.dev == -1 ||
8672 au_get_lr_value(sc, &ct, &l, &r))
8673 goto usemaster;
8674 else {
8675 lgain += l;
8676 rgain += r;
8677 n++;
8678 }
8679 }
8680 }
8681 if (n != 0) {
8682 lgain /= n;
8683 rgain /= n;
8684 }
8685 }
8686 }
8687 bad:
8688 if (lgain == rgain) { /* handles lgain==rgain==0 */
8689 *pgain = lgain;
8690 *pbalance = AUDIO_MID_BALANCE;
8691 } else if (lgain < rgain) {
8692 *pgain = rgain;
8693 /* balance should be > AUDIO_MID_BALANCE */
8694 *pbalance = AUDIO_RIGHT_BALANCE -
8695 (AUDIO_MID_BALANCE * lgain) / rgain;
8696 } else /* lgain > rgain */ {
8697 *pgain = lgain;
8698 /* balance should be < AUDIO_MID_BALANCE */
8699 *pbalance = (AUDIO_MID_BALANCE * rgain) / lgain;
8700 }
8701 }
8702
8703 /*
8704 * Must be called with sc_lock && sc_exlock held.
8705 */
8706 int
8707 au_set_port(struct audio_softc *sc, struct au_mixer_ports *ports, u_int port)
8708 {
8709 mixer_ctrl_t ct;
8710 int i, error, use_mixerout;
8711
8712 KASSERT(mutex_owned(sc->sc_lock));
8713 KASSERT(sc->sc_exlock);
8714
8715 use_mixerout = 1;
8716 if (port == 0) {
8717 if (ports->allports == 0)
8718 return 0; /* Allow this special case. */
8719 else if (ports->isdual) {
8720 if (ports->cur_port == -1) {
8721 return 0;
8722 } else {
8723 port = ports->aumask[ports->cur_port];
8724 ports->cur_port = -1;
8725 use_mixerout = 0;
8726 }
8727 }
8728 }
8729 if (ports->index == -1)
8730 return EINVAL;
8731 ct.dev = ports->index;
8732 if (ports->isenum) {
8733 if (port & (port-1))
8734 return EINVAL; /* Only one port allowed */
8735 ct.type = AUDIO_MIXER_ENUM;
8736 error = EINVAL;
8737 for(i = 0; i < ports->nports; i++)
8738 if (ports->aumask[i] == port) {
8739 if (ports->isdual && use_mixerout) {
8740 ct.un.ord = ports->mixerout;
8741 ports->cur_port = i;
8742 } else {
8743 ct.un.ord = ports->misel[i];
8744 }
8745 error = audio_set_port(sc, &ct);
8746 break;
8747 }
8748 } else {
8749 ct.type = AUDIO_MIXER_SET;
8750 ct.un.mask = 0;
8751 for(i = 0; i < ports->nports; i++)
8752 if (ports->aumask[i] & port)
8753 ct.un.mask |= ports->misel[i];
8754 if (port != 0 && ct.un.mask == 0)
8755 error = EINVAL;
8756 else
8757 error = audio_set_port(sc, &ct);
8758 }
8759 if (!error)
8760 mixer_signal(sc);
8761 return error;
8762 }
8763
8764 /*
8765 * Must be called with sc_lock && sc_exlock held.
8766 */
8767 int
8768 au_get_port(struct audio_softc *sc, struct au_mixer_ports *ports)
8769 {
8770 mixer_ctrl_t ct;
8771 int i, aumask;
8772
8773 KASSERT(mutex_owned(sc->sc_lock));
8774 KASSERT(sc->sc_exlock);
8775
8776 if (ports->index == -1)
8777 return 0;
8778 ct.dev = ports->index;
8779 ct.type = ports->isenum ? AUDIO_MIXER_ENUM : AUDIO_MIXER_SET;
8780 if (audio_get_port(sc, &ct))
8781 return 0;
8782 aumask = 0;
8783 if (ports->isenum) {
8784 if (ports->isdual && ports->cur_port != -1) {
8785 if (ports->mixerout == ct.un.ord)
8786 aumask = ports->aumask[ports->cur_port];
8787 else
8788 ports->cur_port = -1;
8789 }
8790 if (aumask == 0)
8791 for(i = 0; i < ports->nports; i++)
8792 if (ports->misel[i] == ct.un.ord)
8793 aumask = ports->aumask[i];
8794 } else {
8795 for(i = 0; i < ports->nports; i++)
8796 if (ct.un.mask & ports->misel[i])
8797 aumask |= ports->aumask[i];
8798 }
8799 return aumask;
8800 }
8801
8802 /*
8803 * It returns 0 if success, otherwise errno.
8804 * Must be called only if sc->sc_monitor_port != -1.
8805 * Must be called with sc_lock && sc_exlock held.
8806 */
8807 static int
8808 au_set_monitor_gain(struct audio_softc *sc, int monitor_gain)
8809 {
8810 mixer_ctrl_t ct;
8811
8812 KASSERT(mutex_owned(sc->sc_lock));
8813 KASSERT(sc->sc_exlock);
8814
8815 ct.dev = sc->sc_monitor_port;
8816 ct.type = AUDIO_MIXER_VALUE;
8817 ct.un.value.num_channels = 1;
8818 ct.un.value.level[AUDIO_MIXER_LEVEL_MONO] = monitor_gain;
8819 return audio_set_port(sc, &ct);
8820 }
8821
8822 /*
8823 * It returns monitor gain if success, otherwise -1.
8824 * Must be called only if sc->sc_monitor_port != -1.
8825 * Must be called with sc_lock && sc_exlock held.
8826 */
8827 static int
8828 au_get_monitor_gain(struct audio_softc *sc)
8829 {
8830 mixer_ctrl_t ct;
8831
8832 KASSERT(mutex_owned(sc->sc_lock));
8833 KASSERT(sc->sc_exlock);
8834
8835 ct.dev = sc->sc_monitor_port;
8836 ct.type = AUDIO_MIXER_VALUE;
8837 ct.un.value.num_channels = 1;
8838 if (audio_get_port(sc, &ct))
8839 return -1;
8840 return ct.un.value.level[AUDIO_MIXER_LEVEL_MONO];
8841 }
8842
8843 /*
8844 * Must be called with sc_lock && sc_exlock held.
8845 */
8846 static int
8847 audio_set_port(struct audio_softc *sc, mixer_ctrl_t *mc)
8848 {
8849
8850 KASSERT(mutex_owned(sc->sc_lock));
8851 KASSERT(sc->sc_exlock);
8852
8853 return sc->hw_if->set_port(sc->hw_hdl, mc);
8854 }
8855
8856 /*
8857 * Must be called with sc_lock && sc_exlock held.
8858 */
8859 static int
8860 audio_get_port(struct audio_softc *sc, mixer_ctrl_t *mc)
8861 {
8862
8863 KASSERT(mutex_owned(sc->sc_lock));
8864 KASSERT(sc->sc_exlock);
8865
8866 return sc->hw_if->get_port(sc->hw_hdl, mc);
8867 }
8868
8869 /*
8870 * Must be called with sc_lock && sc_exlock held.
8871 */
8872 static void
8873 audio_mixer_capture(struct audio_softc *sc)
8874 {
8875 mixer_devinfo_t mi;
8876 mixer_ctrl_t *mc;
8877
8878 KASSERT(mutex_owned(sc->sc_lock));
8879 KASSERT(sc->sc_exlock);
8880
8881 for (mi.index = 0;; mi.index++) {
8882 if (audio_query_devinfo(sc, &mi) != 0)
8883 break;
8884 KASSERT(mi.index < sc->sc_nmixer_states);
8885 if (mi.type == AUDIO_MIXER_CLASS)
8886 continue;
8887 mc = &sc->sc_mixer_state[mi.index];
8888 mc->dev = mi.index;
8889 mc->type = mi.type;
8890 mc->un.value.num_channels = mi.un.v.num_channels;
8891 (void)audio_get_port(sc, mc);
8892 }
8893
8894 return;
8895 }
8896
8897 /*
8898 * Must be called with sc_lock && sc_exlock held.
8899 */
8900 static void
8901 audio_mixer_restore(struct audio_softc *sc)
8902 {
8903 mixer_devinfo_t mi;
8904 mixer_ctrl_t *mc;
8905
8906 KASSERT(mutex_owned(sc->sc_lock));
8907 KASSERT(sc->sc_exlock);
8908
8909 for (mi.index = 0; ; mi.index++) {
8910 if (audio_query_devinfo(sc, &mi) != 0)
8911 break;
8912 if (mi.type == AUDIO_MIXER_CLASS)
8913 continue;
8914 mc = &sc->sc_mixer_state[mi.index];
8915 (void)audio_set_port(sc, mc);
8916 }
8917 if (sc->hw_if->commit_settings)
8918 sc->hw_if->commit_settings(sc->hw_hdl);
8919
8920 return;
8921 }
8922
8923 static void
8924 audio_volume_down(device_t dv)
8925 {
8926 struct audio_softc *sc = device_private(dv);
8927 mixer_devinfo_t mi;
8928 int newgain;
8929 u_int gain;
8930 u_char balance;
8931
8932 if (audio_exlock_mutex_enter(sc) != 0)
8933 return;
8934 if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
8935 mi.index = sc->sc_outports.master;
8936 mi.un.v.delta = 0;
8937 if (audio_query_devinfo(sc, &mi) == 0) {
8938 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
8939 newgain = gain - mi.un.v.delta;
8940 if (newgain < AUDIO_MIN_GAIN)
8941 newgain = AUDIO_MIN_GAIN;
8942 au_set_gain(sc, &sc->sc_outports, newgain, balance);
8943 }
8944 }
8945 audio_exlock_mutex_exit(sc);
8946 }
8947
8948 static void
8949 audio_volume_up(device_t dv)
8950 {
8951 struct audio_softc *sc = device_private(dv);
8952 mixer_devinfo_t mi;
8953 u_int gain, newgain;
8954 u_char balance;
8955
8956 if (audio_exlock_mutex_enter(sc) != 0)
8957 return;
8958 if (sc->sc_outports.index == -1 && sc->sc_outports.master != -1) {
8959 mi.index = sc->sc_outports.master;
8960 mi.un.v.delta = 0;
8961 if (audio_query_devinfo(sc, &mi) == 0) {
8962 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
8963 newgain = gain + mi.un.v.delta;
8964 if (newgain > AUDIO_MAX_GAIN)
8965 newgain = AUDIO_MAX_GAIN;
8966 au_set_gain(sc, &sc->sc_outports, newgain, balance);
8967 }
8968 }
8969 audio_exlock_mutex_exit(sc);
8970 }
8971
8972 static void
8973 audio_volume_toggle(device_t dv)
8974 {
8975 struct audio_softc *sc = device_private(dv);
8976 u_int gain, newgain;
8977 u_char balance;
8978
8979 if (audio_exlock_mutex_enter(sc) != 0)
8980 return;
8981 au_get_gain(sc, &sc->sc_outports, &gain, &balance);
8982 if (gain != 0) {
8983 sc->sc_lastgain = gain;
8984 newgain = 0;
8985 } else
8986 newgain = sc->sc_lastgain;
8987 au_set_gain(sc, &sc->sc_outports, newgain, balance);
8988 audio_exlock_mutex_exit(sc);
8989 }
8990
8991 /*
8992 * Must be called with sc_lock held.
8993 */
8994 static int
8995 audio_query_devinfo(struct audio_softc *sc, mixer_devinfo_t *di)
8996 {
8997
8998 KASSERT(mutex_owned(sc->sc_lock));
8999
9000 return sc->hw_if->query_devinfo(sc->hw_hdl, di);
9001 }
9002
9003 #endif /* NAUDIO > 0 */
9004
9005 #if NAUDIO == 0 && (NMIDI > 0 || NMIDIBUS > 0)
9006 #include <sys/param.h>
9007 #include <sys/systm.h>
9008 #include <sys/device.h>
9009 #include <sys/audioio.h>
9010 #include <dev/audio/audio_if.h>
9011 #endif
9012
9013 #if NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0)
9014 int
9015 audioprint(void *aux, const char *pnp)
9016 {
9017 struct audio_attach_args *arg;
9018 const char *type;
9019
9020 if (pnp != NULL) {
9021 arg = aux;
9022 switch (arg->type) {
9023 case AUDIODEV_TYPE_AUDIO:
9024 type = "audio";
9025 break;
9026 case AUDIODEV_TYPE_MIDI:
9027 type = "midi";
9028 break;
9029 case AUDIODEV_TYPE_OPL:
9030 type = "opl";
9031 break;
9032 case AUDIODEV_TYPE_MPU:
9033 type = "mpu";
9034 break;
9035 case AUDIODEV_TYPE_AUX:
9036 type = "aux";
9037 break;
9038 default:
9039 panic("audioprint: unknown type %d", arg->type);
9040 }
9041 aprint_normal("%s at %s", type, pnp);
9042 }
9043 return UNCONF;
9044 }
9045
9046 #endif /* NAUDIO > 0 || (NMIDI > 0 || NMIDIBUS > 0) */
9047
9048 #ifdef _MODULE
9049
9050 devmajor_t audio_bmajor = -1, audio_cmajor = -1;
9051
9052 #include "ioconf.c"
9053
9054 #endif
9055
9056 MODULE(MODULE_CLASS_DRIVER, audio, NULL);
9057
9058 static int
9059 audio_modcmd(modcmd_t cmd, void *arg)
9060 {
9061 int error = 0;
9062
9063 switch (cmd) {
9064 case MODULE_CMD_INIT:
9065 /* XXX interrupt level? */
9066 audio_psref_class = psref_class_create("audio", IPL_SOFTSERIAL);
9067 #ifdef _MODULE
9068 error = devsw_attach(audio_cd.cd_name, NULL, &audio_bmajor,
9069 &audio_cdevsw, &audio_cmajor);
9070 if (error)
9071 break;
9072
9073 error = config_init_component(cfdriver_ioconf_audio,
9074 cfattach_ioconf_audio, cfdata_ioconf_audio);
9075 if (error) {
9076 devsw_detach(NULL, &audio_cdevsw);
9077 }
9078 #endif
9079 break;
9080 case MODULE_CMD_FINI:
9081 #ifdef _MODULE
9082 devsw_detach(NULL, &audio_cdevsw);
9083 error = config_fini_component(cfdriver_ioconf_audio,
9084 cfattach_ioconf_audio, cfdata_ioconf_audio);
9085 if (error)
9086 devsw_attach(audio_cd.cd_name, NULL, &audio_bmajor,
9087 &audio_cdevsw, &audio_cmajor);
9088 #endif
9089 psref_class_destroy(audio_psref_class);
9090 break;
9091 default:
9092 error = ENOTTY;
9093 break;
9094 }
9095
9096 return error;
9097 }
9098