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