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