CVS commit: src/sys/dev/audio

2024-04-20 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 20 05:38:40 UTC 2024

Modified Files:
src/sys/dev/audio: linear.c

Log Message:
Fix typo in comment.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/audio/linear.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2024-04-20 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 20 05:38:40 UTC 2024

Modified Files:
src/sys/dev/audio: linear.c

Log Message:
Fix typo in comment.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/audio/linear.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/linear.c
diff -u src/sys/dev/audio/linear.c:1.4 src/sys/dev/audio/linear.c:1.5
--- src/sys/dev/audio/linear.c:1.4	Wed Jul 21 06:35:44 2021
+++ src/sys/dev/audio/linear.c	Sat Apr 20 05:38:40 2024
@@ -1,4 +1,4 @@
-/*	$NetBSD: linear.c,v 1.4 2021/07/21 06:35:44 skrll Exp $	*/
+/*	$NetBSD: linear.c,v 1.5 2024/04/20 05:38:40 isaki Exp $	*/
 
 /*
  * Copyright (C) 2017 Tetsuya Isaki. All rights reserved.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: linear.c,v 1.4 2021/07/21 06:35:44 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linear.c,v 1.5 2024/04/20 05:38:40 isaki Exp $");
 
 #include 
 #include 
@@ -222,7 +222,7 @@ audio_internal_to_linear16(audio_filter_
 /*
  * audio_linear24_to_internal:
  *	This filter performs conversion from [US]LINEAR24/24{LE,BE} to
- *	internal format.  Since it's rerely used, it's size optimized.
+ *	internal format.  Since it's rarely used, it's size optimized.
  */
 void
 audio_linear24_to_internal(audio_filter_arg_t *arg)



CVS commit: src/sys/dev/audio

2023-10-01 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Oct  1 09:34:29 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix output for big-endian hardware.
Also optimize the output scaling routine.


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.144 src/sys/dev/audio/audio.c:1.145
--- src/sys/dev/audio/audio.c:1.144	Mon Jun  5 16:26:05 2023
+++ src/sys/dev/audio/audio.c	Sun Oct  1 09:34:28 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.144 2023/06/05 16:26:05 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.145 2023/10/01 09:34:28 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.144 2023/06/05 16:26:05 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.145 2023/10/01 09:34:28 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -9222,7 +9222,6 @@ audio_mixsample_to_linear(audio_filter_a
 	const aint2_t *m;
 	uint8_t *p;
 	u_int sample_count;
-	bool swap;
 	aint2_t v, xor;
 	u_int i, bps;
 	bool little;
@@ -9235,48 +9234,150 @@ audio_mixsample_to_linear(audio_filter_a
 	m = arg->src;
 	p = arg->dst;
 	sample_count = arg->count * fmt->channels;
-	swap = arg->dstfmt->encoding == AUDIO_ENCODING_SLINEAR_OE;
-
-#if BYTE_ORDER == LITTLE_ENDIAN
-	little = !swap;
-#endif
-#if BYTE_ORDER == BIG_ENDIAN
-	little = swap;
-#endif
+	little = arg->dstfmt->encoding == AUDIO_ENCODING_SLINEAR_LE;
 
 	bps = fmt->stride / NBBY;
+	xor = audio_format2_is_signed(fmt) ? 0 : (aint2_t)1 << 31;
 
-	xor = audio_format2_is_signed(fmt)
-	   ? 0 : 1 << (fmt->stride - 1);
-
-	for (i=0; i>= (4 - bps) * NBBY;
-
-		/* signed -> unsigned */
-		v ^= xor;
-
-		if (little) {
-			switch (bps) {
-			case 4: *p++ = v; v >>= 8; /* FALLTHROUGH */
-			case 3: *p++ = v; v >>= 8; /* FALLTHROUGH */
-			case 2: *p++ = v; v >>= 8; /* FALLTHROUGH */
-			case 1: *p++ = v; /* FALLTHROUGH */
+#if AUDIO_INTERNAL_BITS == 16
+	if (little) {
+		switch (bps) {
+		case 4:
+			for (i=0; i> 8;
 			}
-		} else {
-			switch (bps) {
-			case 4: *p++ = v >> 24; v <<= 8; /* FALLTHROUGH */
-			case 3: *p++ = v >> 24; v <<= 8; /* FALLTHROUGH */
-			case 2: *p++ = v >> 24; v <<= 8; /* FALLTHROUGH */
-			case 1: *p++ = v >> 24; /* FALLTHROUGH */
+			break;
+		case 3:
+			for (i=0; i> 8;
+			}
+			break;
+		case 2:
+			for (i=0; i> 8;
+			}
+			break;
+		case 1:
+			for (i=0; i> 8;
+			}
+			break;
+		}
+	} else {
+		switch (bps) {
+		case 4:
+			for (i=0; i> 8;
+*p++ = v;
+*p++ = 0;
+*p++ = 0;
+			}
+			break;
+		case 3:
+			for (i=0; i> 8;
+*p++ = v;
+*p++ = 0;
+			}
+			break;
+		case 2:
+			for (i=0; i> 8;
+*p++ = v;
+			}
+			break;
+		case 1:
+			for (i=0; i> 8;
+			}
+			break;
+		}
+	}
+#elif AUDIO_INTERNAL_BITS == 32
+	if (little) {
+		switch (bps) {
+		case 4:
+			for (i=0; i> 8;
+*p++ = v >> 16;
+*p++ = v >> 24;
+			}
+			break;
+		case 3:
+			for (i=0; i> 8;
+*p++ = v >> 16;
+*p++ = v >> 24;
+			}
+			break;
+		case 2:
+			for (i=0; i> 16;
+*p++ = v >> 24;
+			}
+			break;
+		case 1:
+			for (i=0; i> 24;
+			}
+			break;
+		}
+	} else {
+		switch (bps) {
+		case 4:
+			for (i=0; i> 24;
+*p++ = v >> 16;
+*p++ = v >> 8;
+*p++ = v;
+			}
+			break;
+		case 3:
+			for (i=0; i> 24;
+*p++ = v >> 16;
+*p++ = v >> 8;
+			}
+			break;
+		case 2:
+			for (i=0; i> 24;
+*p++ = v >> 16;
 			}
+			break;
+		case 1:
+			for (i=0; i> 24;
+			}
+			break;
 		}
 	}
-}
+#endif /* AUDIO_INTERNAL_BITS */
 
+}
 
 #endif /* NAUDIO > 0 */
 



CVS commit: src/sys/dev/audio

2023-10-01 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Oct  1 09:34:29 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix output for big-endian hardware.
Also optimize the output scaling routine.


To generate a diff of this commit:
cvs rdiff -u -r1.144 -r1.145 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-06-05 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Mon Jun  5 16:26:05 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
We cannot handle input with precision != stride yet.
Drain input buffer for unhandled input.


To generate a diff of this commit:
cvs rdiff -u -r1.143 -r1.144 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-06-05 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Mon Jun  5 16:26:05 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
We cannot handle input with precision != stride yet.
Drain input buffer for unhandled input.


To generate a diff of this commit:
cvs rdiff -u -r1.143 -r1.144 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.143 src/sys/dev/audio/audio.c:1.144
--- src/sys/dev/audio/audio.c:1.143	Sun Apr 23 08:53:08 2023
+++ src/sys/dev/audio/audio.c	Mon Jun  5 16:26:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.143 2023/04/23 08:53:08 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.144 2023/06/05 16:26:05 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.143 2023/04/23 08:53:08 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.144 2023/06/05 16:26:05 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -6095,7 +6095,8 @@ audio_rmixer_process(struct audio_softc 
 		codecarg.srcfmt = >hwbuf.fmt;
 		codecarg.dstfmt = >mixfmt;
 		codec = NULL;
-		if (audio_format2_is_linear(codecarg.srcfmt)) {
+		if (audio_format2_is_linear(codecarg.srcfmt) &&
+		codecarg.srcfmt->stride == codecarg.srcfmt->precision) {
 			switch (codecarg.srcfmt->stride) {
 			case 8:
 codec = audio_linear8_to_internal;
@@ -6115,6 +6116,8 @@ audio_rmixer_process(struct audio_softc 
 		}
 		if (codec == NULL) {
 			TRACE(4, "unsupported hw format");
+			/* drain hwbuf */
+			auring_take(>hwbuf, count);
 			return;
 		}
 



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:53:08 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
print stage formats with AUDIO_DEBUG


To generate a diff of this commit:
cvs rdiff -u -r1.142 -r1.143 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.142 src/sys/dev/audio/audio.c:1.143
--- src/sys/dev/audio/audio.c:1.142	Sun Apr 23 08:38:53 2023
+++ src/sys/dev/audio/audio.c	Sun Apr 23 08:53:08 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.142 2023/04/23 08:38:53 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.143 2023/04/23 08:53:08 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.142 2023/04/23 08:38:53 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.143 2023/04/23 08:53:08 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -4764,25 +4764,35 @@ audio_track_set_format(audio_track_t *tr
 		if ((error = audio_track_init_freq(track, _dst)) != 0)
 			goto error;
 	}
-#if 0
-	/* debug */
-	if (track->freq.filter) {
-		audio_print_format2("freq src", >freq.srcbuf.fmt);
-		audio_print_format2("freq dst", >freq.dst->fmt);
-	}
-	if (track->chmix.filter) {
-		audio_print_format2("chmix src", >chmix.srcbuf.fmt);
-		audio_print_format2("chmix dst", >chmix.dst->fmt);
-	}
-	if (track->chvol.filter) {
-		audio_print_format2("chvol src", >chvol.srcbuf.fmt);
-		audio_print_format2("chvol dst", >chvol.dst->fmt);
-	}
-	if (track->codec.filter) {
-		audio_print_format2("codec src", >codec.srcbuf.fmt);
-		audio_print_format2("codec dst", >codec.dst->fmt);
+
+#if defined(AUDIO_DEBUG)
+	if (audiodebug >= 3) {
+		if (track->freq.filter) {
+			audio_print_format2("freq src",
+			>freq.srcbuf.fmt);
+			audio_print_format2("freq dst",
+			>freq.dst->fmt);
+		}
+		if (track->chmix.filter) {
+			audio_print_format2("chmix src",
+			>chmix.srcbuf.fmt);
+			audio_print_format2("chmix dst",
+			>chmix.dst->fmt);
+		}
+		if (track->chvol.filter) {
+			audio_print_format2("chvol src",
+			>chvol.srcbuf.fmt);
+			audio_print_format2("chvol dst",
+			>chvol.dst->fmt);
+		}
+		if (track->codec.filter) {
+			audio_print_format2("codec src",
+			>codec.srcbuf.fmt);
+			audio_print_format2("codec dst",
+			>codec.dst->fmt);
+		}
 	}
-#endif
+#endif /* AUDIO_DEBUG */
 
 	/* Stage input buffer */
 	track->input = last_dst;



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:53:08 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
print stage formats with AUDIO_DEBUG


To generate a diff of this commit:
cvs rdiff -u -r1.142 -r1.143 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:38:53 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Be a little bit more informative on device timeout.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.141 src/sys/dev/audio/audio.c:1.142
--- src/sys/dev/audio/audio.c:1.141	Sun Apr 23 08:26:05 2023
+++ src/sys/dev/audio/audio.c	Sun Apr 23 08:38:53 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.141 2023/04/23 08:26:05 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.142 2023/04/23 08:38:53 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.141 2023/04/23 08:26:05 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.142 2023/04/23 08:38:53 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -571,7 +571,8 @@ static void audio_exlock_exit(struct aud
 static struct audio_softc *audio_sc_acquire_fromfile(audio_file_t *,
 	struct psref *);
 static void audio_sc_release(struct audio_softc *, struct psref *);
-static int audio_track_waitio(struct audio_softc *, audio_track_t *);
+static int audio_track_waitio(struct audio_softc *, audio_track_t *,
+	const char *mess);
 
 static int audioclose(struct file *);
 static int audioread(struct file *, off_t *, struct uio *, kauth_cred_t, int);
@@ -1692,7 +1693,8 @@ audio_sc_release(struct audio_softc *sc,
  * Must be called with sc_lock held.
  */
 static int
-audio_track_waitio(struct audio_softc *sc, audio_track_t *track)
+audio_track_waitio(struct audio_softc *sc, audio_track_t *track,
+const char *mess)
 {
 	int error;
 
@@ -1714,8 +1716,15 @@ audio_track_waitio(struct audio_softc *s
 	}
 	if (error) {
 		TRACET(2, track, "cv_timedwait_sig failed %d", error);
-		if (error == EWOULDBLOCK)
-			audio_printf(sc, "device timeout\n");
+		if (error == EWOULDBLOCK) {
+			audio_ring_t *usrbuf = >usrbuf;
+			audio_ring_t *outbuf = >outbuf;
+			audio_printf(sc,
+			"%s: device timeout, seq=%d, usrbuf=%d/H%d, outbuf=%d/%d\n",
+			mess, (int)track->seq,
+			usrbuf->used, track->usrbuf_usedhigh,
+			outbuf->used, outbuf->capacity);
+		}
 	} else {
 		TRACET(3, track, "wakeup");
 	}
@@ -2837,7 +2846,7 @@ audio_read(struct audio_softc *sc, struc
 			}
 
 			TRACET(3, track, "sleep");
-			error = audio_track_waitio(sc, track);
+			error = audio_track_waitio(sc, track, "audio_read");
 			if (error) {
 mutex_exit(sc->sc_lock);
 return error;
@@ -2964,7 +2973,7 @@ audio_write(struct audio_softc *sc, stru
 
 			TRACET(3, track, "sleep usrbuf=%d/H%d",
 			usrbuf->used, track->usrbuf_usedhigh);
-			error = audio_track_waitio(sc, track);
+			error = audio_track_waitio(sc, track, "audio_write");
 			if (error) {
 mutex_exit(sc->sc_lock);
 goto abort;
@@ -6418,7 +6427,7 @@ audio_track_drain(struct audio_softc *sc
 			break;
 
 		TRACET(3, track, "sleep");
-		error = audio_track_waitio(sc, track);
+		error = audio_track_waitio(sc, track, "audio_drain");
 		if (error)
 			return error;
 



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:38:53 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Be a little bit more informative on device timeout.


To generate a diff of this commit:
cvs rdiff -u -r1.141 -r1.142 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:26:05 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.140 -r1.141 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.140 src/sys/dev/audio/audio.c:1.141
--- src/sys/dev/audio/audio.c:1.140	Sun Apr 23 08:05:36 2023
+++ src/sys/dev/audio/audio.c	Sun Apr 23 08:26:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.140 2023/04/23 08:05:36 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.141 2023/04/23 08:26:05 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.140 2023/04/23 08:05:36 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.141 2023/04/23 08:26:05 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -1186,7 +1186,7 @@ bad:
 	return;
 }
 
- /*
+/*
  * Identify audio backend device for drvctl.
  */
 static int
@@ -6071,7 +6071,7 @@ audio_rmixer_process(struct audio_softc 
 		tmpsrc.mem = mixer->mixsample;
 		tmpsrc.head = 0;
 		tmpsrc.used = 0;
- 
+
 		/* ad-hoc codec */
 		codecarg.srcfmt = >hwbuf.fmt;
 		codecarg.dstfmt = >mixfmt;
@@ -6098,7 +6098,7 @@ audio_rmixer_process(struct audio_softc 
 			TRACE(4, "unsupported hw format");
 			return;
 		}
- 
+
 		codecarg.src = auring_headptr(>hwbuf);
 		codecarg.dst = auring_tailptr();
 		codecarg.count = count;
@@ -6108,9 +6108,8 @@ audio_rmixer_process(struct audio_softc 
 
 	auring_take(>hwbuf, count);
 	auring_push(mixersrc, count);
- 
+
 	TRACE(4, "distribute");
- 
 
 	/* Distribute to all tracks. */
 	SLIST_FOREACH(f, >sc_files, entry) {



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:26:05 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.140 -r1.141 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:06:05 UTC 2023

Modified Files:
src/sys/dev/audio: audiovar.h

Log Message:
Enable 24bit support by default.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/audio/audiovar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audiovar.h
diff -u src/sys/dev/audio/audiovar.h:1.12 src/sys/dev/audio/audiovar.h:1.13
--- src/sys/dev/audio/audiovar.h:1.12	Fri May 29 03:09:14 2020
+++ src/sys/dev/audio/audiovar.h	Sun Apr 23 08:06:05 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audiovar.h,v 1.12 2020/05/29 03:09:14 isaki Exp $	*/
+/*	$NetBSD: audiovar.h,v 1.13 2023/04/23 08:06:05 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -79,7 +79,7 @@
 /*
  * Whether supports [US]LINEAR24/24 as userland format.
  */
-/* #define AUDIO_SUPPORT_LINEAR24 */
+#define AUDIO_SUPPORT_LINEAR24
 
 /*
  * Frequency range.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:06:05 UTC 2023

Modified Files:
src/sys/dev/audio: audiovar.h

Log Message:
Enable 24bit support by default.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/dev/audio/audiovar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:05:37 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Add 24bit/32bit hardware support.


To generate a diff of this commit:
cvs rdiff -u -r1.139 -r1.140 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.139 src/sys/dev/audio/audio.c:1.140
--- src/sys/dev/audio/audio.c:1.139	Sun Apr 23 06:30:58 2023
+++ src/sys/dev/audio/audio.c	Sun Apr 23 08:05:36 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.139 2023/04/23 06:30:58 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.140 2023/04/23 08:05:36 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.139 2023/04/23 06:30:58 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.140 2023/04/23 08:05:36 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -697,6 +697,8 @@ static int au_get_monitor_gain(struct au
 static int audio_get_port(struct audio_softc *, mixer_ctrl_t *);
 static int audio_set_port(struct audio_softc *, mixer_ctrl_t *);
 
+void audio_mixsample_to_linear(audio_filter_arg_t *);
+
 static __inline struct audio_params
 format2_to_params(const audio_format2_t *f2)
 {
@@ -5377,8 +5379,15 @@ audio_mixer_init(struct audio_softc *sc,
 		len = mixer->frames_per_block * mixer->mixfmt.channels *
 		mixer->mixfmt.stride / NBBY;
 		mixer->mixsample = audio_realloc(mixer->mixsample, len);
-	} else {
-		/* No mixing buffer for recording */
+	} else if (reg->codec == NULL) {
+		/*
+		 * Recording requires an input conversion buffer
+		 * unless the hardware provides a codec itself
+		 */
+		mixer->mixfmt = mixer->track_fmt;
+		len = mixer->frames_per_block * mixer->mixfmt.channels *
+		mixer->mixfmt.stride / NBBY;
+		mixer->mixsample = audio_realloc(mixer->mixsample, len);
 	}
 
 	if (reg->codec) {
@@ -5647,31 +5656,32 @@ audio_pmixer_process(struct audio_softc 
 	 * The rest is the hardware part.
 	 */
 
+	m = mixer->mixsample;
+
 	if (mixer->codec) {
-		h = auring_tailptr_aint(>codecbuf);
-	} else {
-		h = auring_tailptr_aint(>hwbuf);
-	}
+		TRACE(4, "codec count=%d", frame_count);
 
-	m = mixer->mixsample;
-	if (!mixer->codec && mixer->swap_endian) {
-		for (i = 0; i < sample_count; i++) {
-			*h++ = bswap16(*m++);
-		}
-	} else {
-		for (i = 0; i < sample_count; i++) {
+		h = auring_tailptr_aint(>codecbuf);
+		for (i=0; icodec) {
+		/* Hardware driver's codec */
 		auring_push(>codecbuf, frame_count);
 		mixer->codecarg.src = auring_headptr(>codecbuf);
 		mixer->codecarg.dst = auring_tailptr(>hwbuf);
 		mixer->codecarg.count = frame_count;
 		mixer->codec(>codecarg);
 		auring_take(>codecbuf, mixer->codecarg.count);
+	} else {
+		TRACE(4, "direct count=%d", frame_count);
+
+		/* Direct conversion to linear output */
+		mixer->codecarg.src = m;
+		mixer->codecarg.dst = auring_tailptr(>hwbuf);
+		mixer->codecarg.count = frame_count;
+		mixer->codecarg.srcfmt = >mixfmt;
+		mixer->codecarg.dstfmt = >hwbuf.fmt;
+		audio_mixsample_to_linear(>codecarg);
 	}
 
 	auring_push(>hwbuf, frame_count);
@@ -6024,11 +6034,12 @@ audio_rmixer_process(struct audio_softc 
 {
 	audio_trackmixer_t *mixer;
 	audio_ring_t *mixersrc;
+	audio_ring_t tmpsrc;
+	audio_filter_t codec;
+	audio_filter_arg_t codecarg;
 	audio_file_t *f;
-	aint_t *p;
 	int count;
 	int bytes;
-	int i;
 
 	mixer = sc->sc_rmixer;
 
@@ -6046,25 +6057,61 @@ audio_rmixer_process(struct audio_softc 
 
 	/* Hardware driver's codec */
 	if (mixer->codec) {
+		TRACE(4, "codec count=%d", count);
 		mixer->codecarg.src = auring_headptr(>hwbuf);
 		mixer->codecarg.dst = auring_tailptr(>codecbuf);
 		mixer->codecarg.count = count;
 		mixer->codec(>codecarg);
-		auring_take(>hwbuf, mixer->codecarg.count);
-		auring_push(>codecbuf, mixer->codecarg.count);
 		mixersrc = >codecbuf;
 	} else {
-		mixersrc = >hwbuf;
-	}
-
-	if (!mixer->codec && mixer->swap_endian) {
-		/* inplace conversion */
-		p = auring_headptr_aint(mixersrc);
-		for (i = 0; i < count * mixer->track_fmt.channels; i++, p++) {
-			*p = bswap16(*p);
+		TRACE(4, "direct count=%d", count);
+		/* temporary ring using mixsample buffer */
+		tmpsrc.fmt = mixer->mixfmt;
+		tmpsrc.capacity = mixer->frames_per_block;
+		tmpsrc.mem = mixer->mixsample;
+		tmpsrc.head = 0;
+		tmpsrc.used = 0;
+ 
+		/* ad-hoc codec */
+		codecarg.srcfmt = >hwbuf.fmt;
+		codecarg.dstfmt = >mixfmt;
+		codec = NULL;
+		if (audio_format2_is_linear(codecarg.srcfmt)) {
+			switch (codecarg.srcfmt->stride) {
+			case 8:
+codec = audio_linear8_to_internal;
+break;
+			case 16:
+codec = audio_linear16_to_internal;
+break;
+#if defined(AUDIO_SUPPORT_LINEAR24)
+			case 24:
+codec = audio_linear24_to_internal;
+break;
+#endif
+			case 32:
+codec = audio_linear32_to_internal;
+break;
+			

CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 08:05:37 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Add 24bit/32bit hardware support.


To generate a diff of this commit:
cvs rdiff -u -r1.139 -r1.140 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 06:30:58 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 06:30:58 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Whitespace


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.138 src/sys/dev/audio/audio.c:1.139
--- src/sys/dev/audio/audio.c:1.138	Sun Apr 23 06:28:34 2023
+++ src/sys/dev/audio/audio.c	Sun Apr 23 06:30:58 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.138 2023/04/23 06:28:34 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.139 2023/04/23 06:30:58 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.138 2023/04/23 06:28:34 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.139 2023/04/23 06:30:58 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -1190,19 +1190,19 @@ bad:
 static int
 audio_properties(struct audio_softc *sc)
 {
-   prop_dictionary_t dict = device_properties(sc->sc_dev);
-   audio_device_t adev;
-   int error;
-
-   error = sc->hw_if->getdev(sc->hw_hdl, );
-   if (error)
-	   return error;
-
-   prop_dictionary_set_string(dict, "name", adev.name);
-   prop_dictionary_set_string(dict, "version", adev.version);
-   prop_dictionary_set_string(dict, "config", adev.config);
+	prop_dictionary_t dict = device_properties(sc->sc_dev);
+	audio_device_t adev;
+	int error;
+
+	error = sc->hw_if->getdev(sc->hw_hdl, );
+	if (error)
+		return error;
+
+	prop_dictionary_set_string(dict, "name", adev.name);
+	prop_dictionary_set_string(dict, "version", adev.version);
+	prop_dictionary_set_string(dict, "config", adev.config);
 
-   return 0;
+	return 0;
 }
 
 /*



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 06:28:34 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Make audio_device information available to drvctl and devpubd.


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.137 src/sys/dev/audio/audio.c:1.138
--- src/sys/dev/audio/audio.c:1.137	Mon Apr 17 20:33:45 2023
+++ src/sys/dev/audio/audio.c	Sun Apr 23 06:28:34 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.137 2023/04/17 20:33:45 mlelstv Exp $	*/
+/*	$NetBSD: audio.c,v 1.138 2023/04/23 06:28:34 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.137 2023/04/17 20:33:45 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.138 2023/04/23 06:28:34 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -561,6 +561,7 @@ static void audio_mixer_restore(struct a
 static void audio_softintr_rd(void *);
 static void audio_softintr_wr(void *);
 
+static int audio_properties(struct audio_softc *);
 static void audio_printf(struct audio_softc *, const char *, ...)
 	__printflike(2, 3);
 static int audio_exlock_mutex_enter(struct audio_softc *);
@@ -1063,6 +1064,10 @@ audioattach(device_t parent, device_t se
 			rhwfmt = phwfmt;
 	}
 
+	/* Make device id available */
+	if (audio_properties(sc))
+		aprint_error_dev(self, "audio_properties failed\n");
+
 	/* Init hardware. */
 	/* hw_probe() also validates [pr]hwfmt.  */
 	error = audio_hw_set_format(sc, mode, , , , );
@@ -1179,6 +1184,27 @@ bad:
 	return;
 }
 
+ /*
+ * Identify audio backend device for drvctl.
+ */
+static int
+audio_properties(struct audio_softc *sc)
+{
+   prop_dictionary_t dict = device_properties(sc->sc_dev);
+   audio_device_t adev;
+   int error;
+
+   error = sc->hw_if->getdev(sc->hw_hdl, );
+   if (error)
+	   return error;
+
+   prop_dictionary_set_string(dict, "name", adev.name);
+   prop_dictionary_set_string(dict, "version", adev.version);
+   prop_dictionary_set_string(dict, "config", adev.config);
+
+   return 0;
+}
+
 /*
  * Initialize hardware mixer.
  * This function is called from audioattach().



CVS commit: src/sys/dev/audio

2023-04-23 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Sun Apr 23 06:28:34 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Make audio_device information available to drvctl and devpubd.


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2023-04-17 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Mon Apr 17 20:33:45 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Input and output codecs produce and consume internal audio data, so
don't byteswap it.


To generate a diff of this commit:
cvs rdiff -u -r1.136 -r1.137 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.136 src/sys/dev/audio/audio.c:1.137
--- src/sys/dev/audio/audio.c:1.136	Thu Aug 25 11:16:33 2022
+++ src/sys/dev/audio/audio.c	Mon Apr 17 20:33:45 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.136 2022/08/25 11:16:33 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.137 2023/04/17 20:33:45 mlelstv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.136 2022/08/25 11:16:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.137 2023/04/17 20:33:45 mlelstv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -5628,7 +5628,7 @@ audio_pmixer_process(struct audio_softc 
 	}
 
 	m = mixer->mixsample;
-	if (mixer->swap_endian) {
+	if (!mixer->codec && mixer->swap_endian) {
 		for (i = 0; i < sample_count; i++) {
 			*h++ = bswap16(*m++);
 		}
@@ -6031,7 +6031,7 @@ audio_rmixer_process(struct audio_softc 
 		mixersrc = >hwbuf;
 	}
 
-	if (mixer->swap_endian) {
+	if (!mixer->codec && mixer->swap_endian) {
 		/* inplace conversion */
 		p = auring_headptr_aint(mixersrc);
 		for (i = 0; i < count * mixer->track_fmt.channels; i++, p++) {



CVS commit: src/sys/dev/audio

2023-04-17 Thread Michael van Elst
Module Name:src
Committed By:   mlelstv
Date:   Mon Apr 17 20:33:45 UTC 2023

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Input and output codecs produce and consume internal audio data, so
don't byteswap it.


To generate a diff of this commit:
cvs rdiff -u -r1.136 -r1.137 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-08-25 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Aug 25 11:16:34 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix bug in detaching audio16 and beyond.

The minor numbers have only four bits for the unit number, so unit
numbers past 15 can't be represented as is.  Attempting to revoke
them was once harmless, when the system made no attempt to avoid
open/detach races; now it crashes because vdevgone assumes that the
minor number can be mapped back to an autoconf device, but it's the
wrong one.  With this change, we stop trying to revoke units beyond
15, because they can't be opened anyway (which may be a bug in its
own right, requiring expansion of the minor number encoding!).

Reported-by: syzbot+6634ffd48997ae9b1...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

Reported-by: syzbot+d2df39bb3f72975c0...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=cbdd598287529cff9a8c4230263f7414df88db4b

Reported-by: syzbot+1404969f68424f8f6...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=6e4782408d0351769215fe433986f1844a546774

Reported-by: syzbot+2a4174a65609b3a00...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=886bbee544c2337683e24c801f9b632630a24681

Reported-by: syzbot+c0d9e49f22e571650...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=7fb2e5576ebae731e859283f85c97747d2824f35

Reported-by: syzbot+583ba2cdb8aa6e59a...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=2af44f5245eba572ebfb222070b9fd1378854303


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.136 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-08-25 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Aug 25 11:16:34 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix bug in detaching audio16 and beyond.

The minor numbers have only four bits for the unit number, so unit
numbers past 15 can't be represented as is.  Attempting to revoke
them was once harmless, when the system made no attempt to avoid
open/detach races; now it crashes because vdevgone assumes that the
minor number can be mapped back to an autoconf device, but it's the
wrong one.  With this change, we stop trying to revoke units beyond
15, because they can't be opened anyway (which may be a bug in its
own right, requiring expansion of the minor number encoding!).

Reported-by: syzbot+6634ffd48997ae9b1...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=db40a795a0b078f9b3b9fa0d3b7a9addcd2534de

Reported-by: syzbot+d2df39bb3f72975c0...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=cbdd598287529cff9a8c4230263f7414df88db4b

Reported-by: syzbot+1404969f68424f8f6...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=6e4782408d0351769215fe433986f1844a546774

Reported-by: syzbot+2a4174a65609b3a00...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=886bbee544c2337683e24c801f9b632630a24681

Reported-by: syzbot+c0d9e49f22e571650...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=7fb2e5576ebae731e859283f85c97747d2824f35

Reported-by: syzbot+583ba2cdb8aa6e59a...@syzkaller.appspotmail.com
https://syzkaller.appspot.com/bug?id=2af44f5245eba572ebfb222070b9fd1378854303


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.136 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.135 src/sys/dev/audio/audio.c:1.136
--- src/sys/dev/audio/audio.c:1.135	Sat Aug 13 06:47:41 2022
+++ src/sys/dev/audio/audio.c	Thu Aug 25 11:16:33 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.135 2022/08/13 06:47:41 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.136 2022/08/25 11:16:33 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.135 2022/08/13 06:47:41 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.136 2022/08/25 11:16:33 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -1363,13 +1363,22 @@ audiodetach(device_t self, int flags)
 
 	/*
 	 * Prevent new opens and wait for existing opens to complete.
+	 *
+	 * At the moment there are only four bits in the minor for the
+	 * unit number, so we only revoke if the unit number could be
+	 * used in a device node.
+	 *
+	 * XXX If we want more audio units, we need to encode them
+	 * more elaborately in the minor space.
 	 */
 	maj = cdevsw_lookup_major(_cdevsw);
 	mn = device_unit(self);
-	vdevgone(maj, mn|SOUND_DEVICE, mn|SOUND_DEVICE, VCHR);
-	vdevgone(maj, mn|AUDIO_DEVICE, mn|AUDIO_DEVICE, VCHR);
-	vdevgone(maj, mn|AUDIOCTL_DEVICE, mn|AUDIOCTL_DEVICE, VCHR);
-	vdevgone(maj, mn|MIXER_DEVICE, mn|MIXER_DEVICE, VCHR);
+	if (mn <= 0xf) {
+		vdevgone(maj, mn|SOUND_DEVICE, mn|SOUND_DEVICE, VCHR);
+		vdevgone(maj, mn|AUDIO_DEVICE, mn|AUDIO_DEVICE, VCHR);
+		vdevgone(maj, mn|AUDIOCTL_DEVICE, mn|AUDIOCTL_DEVICE, VCHR);
+		vdevgone(maj, mn|MIXER_DEVICE, mn|MIXER_DEVICE, VCHR);
+	}
 
 	/*
 	 * This waits currently running sysctls to finish if exists.



CVS commit: src/sys/dev/audio

2022-08-13 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Aug 13 06:47:41 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio: Rework about usrbuf allocation.
- Allocate the usrbuf from kmem(9) instead of uvm(9).  The usrbuf has used
  uvm(9), in case mmap(2) might be issued later.  However, despite the most
  apps don't use mmap(2), allocating (and reallocating) uvm(9) every time
  would be expensive.  In addition, uvm(9) for recording usrbuf was totally
  pointless now.
- For playback, the usrbuf memory will be allocated in pages.  Because the
  usrbuf for playback is mostly near 64KB for backward compatibility.
  This will reduce reallocation especially from the initial ulaw to the most
  commonly used format like slinear16/2ch/48kHz.
- When mmap(2) is called, it will replace the playback usrbuf from kmem(9) to
  uvm(9).
- Prohibit changing playback format once mmap(2) is called.
  It follows netbsd-7.
- For recording, the usrbuf memory will be allocated in the requested size
  every time as before.  Because the usrbuf for recording is only one block
  and is enough small to the page in the most case.
Inspired by PR kern/56947.


To generate a diff of this commit:
cvs rdiff -u -r1.134 -r1.135 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.19 -r1.20 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.134 src/sys/dev/audio/audio.c:1.135
--- src/sys/dev/audio/audio.c:1.134	Wed Jul  6 01:12:45 2022
+++ src/sys/dev/audio/audio.c	Sat Aug 13 06:47:41 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.134 2022/07/06 01:12:45 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.135 2022/08/13 06:47:41 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.134 2022/07/06 01:12:45 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.135 2022/08/13 06:47:41 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -234,7 +234,7 @@ __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.
 
 /*
  * 0: No debug logs
- * 1: action changes like open/close/set_format...
+ * 1: action changes like open/close/set_format/mmap...
  * 2: + normal operations like read/write/ioctl...
  * 3: + TRACEs except interrupt
  * 4: + TRACEs including interrupt
@@ -645,7 +645,6 @@ static void audio_print_format2(const ch
 #endif
 
 static void *audio_realloc(void *, size_t);
-static int audio_realloc_usrbuf(audio_track_t *, int);
 static void audio_free_usrbuf(audio_track_t *);
 
 static audio_track_t *audio_track_create(struct audio_softc *,
@@ -3559,10 +3558,13 @@ audio_mmap(struct audio_softc *sc, off_t
 	audio_file_t *file)
 {
 	audio_track_t *track;
+	struct uvm_object *uobj;
+	vaddr_t vstart;
 	vsize_t vsize;
 	int error;
 
-	TRACEF(2, file, "off=%lld, prot=%d", (long long)(*offp), prot);
+	TRACEF(1, file, "off=%jd, len=%ju, prot=%d",
+	(intmax_t)(*offp), (uintmax_t)len, prot);
 
 	KASSERT(len > 0);
 
@@ -3595,36 +3597,69 @@ audio_mmap(struct audio_softc *sc, off_t
 	if (track == NULL)
 		return EACCES;
 
+	/* XXX TODO: what happens when mmap twice. */
+	if (track->mmapped)
+		return EIO;
+
+	/* Create a uvm anonymous object */
 	vsize = roundup2(MAX(track->usrbuf.capacity, PAGE_SIZE), PAGE_SIZE);
-	if (len > vsize)
-		return EOVERFLOW;
-	if (*offp > (uint)(vsize - len))
+	if (*offp + len > vsize)
 		return EOVERFLOW;
+	uobj = uao_create(vsize, 0);
 
-	/* XXX TODO: what happens when mmap twice. */
-	if (!track->mmapped) {
-		track->mmapped = true;
+	/* Map it into the kernel virtual address space */
+	vstart = 0;
+	error = uvm_map(kernel_map, , vsize, uobj, 0, 0,
+	UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW, UVM_INH_NONE,
+	UVM_ADV_RANDOM, 0));
+	if (error) {
+		device_printf(sc->sc_dev, "uvm_map failed: errno=%d\n", error);
+		uao_detach(uobj);	/* release reference */
+		return error;
+	}
 
-		if (!track->is_pause) {
-			error = audio_exlock_mutex_enter(sc);
-			if (error)
-return error;
-			if (sc->sc_pbusy == false)
-audio_pmixer_start(sc, true);
-			audio_exlock_mutex_exit(sc);
-		}
-		/* XXX mmapping record buffer is not supported */
+	error = uvm_map_pageable(kernel_map, vstart, vstart + vsize,
+	false, 0);
+	if (error) {
+		device_printf(sc->sc_dev, "uvm_map_pageable failed: errno=%d\n",
+		error);
+		goto abort;
 	}
 
-	/* get ringbuffer */
-	*uobjp = track->uobj;
+	error = audio_exlock_mutex_enter(sc);
+	if (error)
+		goto abort;
+
+	/*
+	 * mmap() will start playing immediately.  XXX Maybe we lack API...
+	 * If no one has played yet, start pmixer here.
+	 */
+	if (sc->sc_pbusy == false)
+		audio_pmixer_start(sc, true);
+	audio_exlock_mutex_exit(sc);
+
+	/* Finally, replace the usrbuf from kmem to uvm. */
+	audio_track_lock_enter(track);
+	kmem_free(track->usrbuf.mem, track->usrbuf_allocsize);
+	track->usrbuf.mem 

CVS commit: src/sys/dev/audio

2022-08-13 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Aug 13 06:47:41 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio: Rework about usrbuf allocation.
- Allocate the usrbuf from kmem(9) instead of uvm(9).  The usrbuf has used
  uvm(9), in case mmap(2) might be issued later.  However, despite the most
  apps don't use mmap(2), allocating (and reallocating) uvm(9) every time
  would be expensive.  In addition, uvm(9) for recording usrbuf was totally
  pointless now.
- For playback, the usrbuf memory will be allocated in pages.  Because the
  usrbuf for playback is mostly near 64KB for backward compatibility.
  This will reduce reallocation especially from the initial ulaw to the most
  commonly used format like slinear16/2ch/48kHz.
- When mmap(2) is called, it will replace the playback usrbuf from kmem(9) to
  uvm(9).
- Prohibit changing playback format once mmap(2) is called.
  It follows netbsd-7.
- For recording, the usrbuf memory will be allocated in the requested size
  every time as before.  Because the usrbuf for recording is only one block
  and is enough small to the page in the most case.
Inspired by PR kern/56947.


To generate a diff of this commit:
cvs rdiff -u -r1.134 -r1.135 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.19 -r1.20 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 11:44:01 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix a typo in comment.  Remove several old comments.


To generate a diff of this commit:
cvs rdiff -u -r1.132 -r1.133 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.132 src/sys/dev/audio/audio.c:1.133
--- src/sys/dev/audio/audio.c:1.132	Sat Apr 23 11:30:57 2022
+++ src/sys/dev/audio/audio.c	Sat Apr 23 11:44:01 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.132 2022/04/23 11:30:57 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.133 2022/04/23 11:44:01 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.132 2022/04/23 11:30:57 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.133 2022/04/23 11:44:01 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -3160,7 +3160,6 @@ audio_ioctl(dev_t dev, struct audio_soft
 			audio_exlock_exit(sc);
 			break;
 		}
-		/* XXX TODO: update last_ai if /dev/sound ? */
 		if (ISDEVSOUND(dev))
 			error = audiogetinfo(sc, >sc_ai, 0, file);
 		audio_exlock_exit(sc);
@@ -4514,7 +4513,7 @@ audio_track_init_freq(audio_track_t *tra
 
 		arg = >freq.arg;
 		arg->srcfmt = >fmt;
-		arg->dstfmt = dstfmt;/*_dst->fmt;*/
+		arg->dstfmt = dstfmt;
 		arg->context = track;
 
 		*last_dstp = srcbuf;
@@ -4780,7 +4779,7 @@ audio_track_set_format(audio_track_t *tr
 
 	/*
 	 * On the recording track, expand the input stage buffer, which is
-	 * the closest buffer to rmixer, to NBLKOUT blocks.
+	 * the closest buffer to rmixer, to NBLKIN blocks.
 	 * Note that input buffer may point to outbuf.
 	 */
 	if (!is_playback) {



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 11:44:01 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix a typo in comment.  Remove several old comments.


To generate a diff of this commit:
cvs rdiff -u -r1.132 -r1.133 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 11:30:57 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Clean up about audio_realloc().
- audio_realloc() never returns NULL, so there is no need to check it.
- audio_free() is no point in this case.


To generate a diff of this commit:
cvs rdiff -u -r1.131 -r1.132 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.131 src/sys/dev/audio/audio.c:1.132
--- src/sys/dev/audio/audio.c:1.131	Sat Apr 23 07:55:07 2022
+++ src/sys/dev/audio/audio.c	Sat Apr 23 11:30:57 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.131 2022/04/23 07:55:07 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.132 2022/04/23 11:30:57 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.131 2022/04/23 07:55:07 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.132 2022/04/23 11:30:57 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -3689,7 +3689,8 @@ audio_realloc(void *memblock, size_t byt
 {
 
 	KASSERT(bytes != 0);
-	audio_free(memblock);
+	if (memblock)
+		kern_free(memblock);
 	return kern_malloc(bytes, M_WAITOK);
 }
 
@@ -4776,11 +4777,6 @@ audio_track_set_format(audio_track_t *tr
 		track->outbuf.capacity *= NBLKOUT;
 	len = auring_bytelen(>outbuf);
 	track->outbuf.mem = audio_realloc(track->outbuf.mem, len);
-	if (track->outbuf.mem == NULL) {
-		device_printf(sc->sc_dev, "malloc outbuf(%d) failed\n", len);
-		error = ENOMEM;
-		goto error;
-	}
 
 	/*
 	 * On the recording track, expand the input stage buffer, which is
@@ -5387,12 +5383,6 @@ audio_mixer_init(struct audio_softc *sc,
 		mixer->codecbuf.capacity = mixer->frames_per_block;
 		len = auring_bytelen(>codecbuf);
 		mixer->codecbuf.mem = audio_realloc(mixer->codecbuf.mem, len);
-		if (mixer->codecbuf.mem == NULL) {
-			device_printf(sc->sc_dev,
-			"malloc codecbuf(%d) failed\n", len);
-			error = ENOMEM;
-			goto abort;
-		}
 	}
 
 	/* Succeeded so display it. */



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 11:30:57 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Clean up about audio_realloc().
- audio_realloc() never returns NULL, so there is no need to check it.
- audio_free() is no point in this case.


To generate a diff of this commit:
cvs rdiff -u -r1.131 -r1.132 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 07:55:07 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio(4): Remove no longer used counters.
These were used at very early phase of development.


To generate a diff of this commit:
cvs rdiff -u -r1.130 -r1.131 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.18 -r1.19 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.130 src/sys/dev/audio/audio.c:1.131
--- src/sys/dev/audio/audio.c:1.130	Sat Apr 23 07:43:16 2022
+++ src/sys/dev/audio/audio.c	Sat Apr 23 07:55:07 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.130 2022/04/23 07:43:16 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.131 2022/04/23 07:55:07 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.130 2022/04/23 07:43:16 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.131 2022/04/23 07:55:07 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2825,7 +2825,6 @@ audio_read(struct audio_softc *sc, struc
 			goto abort;
 		}
 		auring_take(usrbuf, bytes);
-		track->useriobytes += bytes;
 		TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
 		bytes,
 		usrbuf->head, usrbuf->used, usrbuf->capacity);
@@ -2953,7 +2952,6 @@ audio_write(struct audio_softc *sc, stru
 goto abort;
 			}
 			auring_push(usrbuf, len);
-			track->useriobytes += len;
 			TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
 			len,
 			usrbuf->head, usrbuf->used, usrbuf->capacity);
@@ -4955,9 +4953,6 @@ audio_track_play(audio_track_t *track)
 	"count=%d fpb=%d",
 	count, frame_per_block(track->mixer, >outbuf.fmt));
 
-	/* XXX TODO: is this necessary for now? */
-	int track_count_0 = track->outbuf.used;
-
 	usrbuf = >usrbuf;
 	input = track->input;
 
@@ -5068,12 +5063,6 @@ audio_track_play(audio_track_t *track)
 		}
 	}
 
-	if (track->input == >outbuf) {
-		track->outputcounter = track->inputcounter;
-	} else {
-		track->outputcounter += track->outbuf.used - track_count_0;
-	}
-
 	track->stamp++;
 
 #if defined(AUDIO_DEBUG)
@@ -6389,8 +6378,7 @@ audio_track_drain(struct audio_softc *sc
 	}
 
 	track->pstate = AUDIO_STATE_CLEAR;
-	TRACET(3, track, "done trk_inp=%d trk_out=%d",
-		(int)track->inputcounter, (int)track->outputcounter);
+	TRACET(3, track, "done");
 	return 0;
 }
 

Index: src/sys/dev/audio/audiodef.h
diff -u src/sys/dev/audio/audiodef.h:1.18 src/sys/dev/audio/audiodef.h:1.19
--- src/sys/dev/audio/audiodef.h:1.18	Wed Apr 20 07:11:13 2022
+++ src/sys/dev/audio/audiodef.h	Sat Apr 23 07:55:07 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audiodef.h,v 1.18 2022/04/20 07:11:13 isaki Exp $	*/
+/*	$NetBSD: audiodef.h,v 1.19 2022/04/23 07:55:07 isaki Exp $	*/
 
 /*
  * Copyright (C) 2017 Tetsuya Isaki. All rights reserved.
@@ -175,11 +175,7 @@ struct audio_track {
 	audio_state_t	pstate;		/* playback state */
 	bool		is_pause;
 
-	/* Statistic counters. */
-	uint64_t	inputcounter;	/* # of frames input to track */
-	uint64_t	outputcounter;	/* # of frames output from track */
-	uint64_t	useriobytes;	/* # of bytes xfer to/from userland */
-	uint64_t	dropframes;	/* # of frames dropped */
+	uint64_t	dropframes;	/* number of dropped frames */
 	int		eofcounter;	/* count of zero-sized write */
 
 	/*



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 07:55:07 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio(4): Remove no longer used counters.
These were used at very early phase of development.


To generate a diff of this commit:
cvs rdiff -u -r1.130 -r1.131 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.18 -r1.19 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 07:43:16 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Restore(implement) AUDIO_GETIOFFS ioctl.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.129 src/sys/dev/audio/audio.c:1.130
--- src/sys/dev/audio/audio.c:1.129	Sat Apr 23 06:17:59 2022
+++ src/sys/dev/audio/audio.c	Sat Apr 23 07:43:16 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.129 2022/04/23 06:17:59 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.130 2022/04/23 07:43:16 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.129 2022/04/23 06:17:59 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.130 2022/04/23 07:43:16 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -3086,12 +3086,31 @@ audio_ioctl(dev_t dev, struct audio_soft
 		break;
 
 	case AUDIO_GETIOFFS:
-		/* XXX TODO */
-		TRACEF(2, file, "%s", pre);
 		ao = (struct audio_offset *)addr;
-		ao->samples = 0;
-		ao->deltablks = 0;
-		ao->offset = 0;
+		track = file->rtrack;
+		if (track == NULL) {
+			ao->samples = 0;
+			ao->deltablks = 0;
+			ao->offset = 0;
+			TRACEF(2, file, "%s no rtrack", pre);
+			break;
+		}
+		mutex_enter(sc->sc_lock);
+		mutex_enter(sc->sc_intr_lock);
+		/* figure out where next transfer will start */
+		stamp = track->stamp;
+		offset = auring_tail(track->input);
+		mutex_exit(sc->sc_intr_lock);
+		mutex_exit(sc->sc_lock);
+
+		/* samples will overflow soon but is as per spec. */
+		ao->samples = stamp * track->usrbuf_blksize;
+		ao->deltablks = stamp - track->last_stamp;
+		ao->offset = audio_track_inputblk_as_usrbyte(track, offset);
+		TRACET(2, track, "%s samples=%u deltablks=%u offset=%u",
+		pre, ao->samples, ao->deltablks, ao->offset);
+
+		track->last_stamp = stamp;
 		break;
 
 	case AUDIO_GETOOFFS:
@@ -5152,8 +5171,6 @@ audio_track_record(audio_track_t *track)
 		auring_take(outbuf, bytes2 / framesize);
 	}
 
-	/* XXX TODO: any counters here? */
-
 #if defined(AUDIO_DEBUG)
 	if (audiodebug >= 3) {
 		struct audio_track_debugbuf m;
@@ -6098,7 +6115,7 @@ audio_rmixer_process(struct audio_softc 
 		bytes);
 		auring_push(input, count);
 
-		/* XXX sequence counter? */
+		track->stamp++;
 
 		audio_track_lock_exit(track);
 	}



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 07:43:16 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Restore(implement) AUDIO_GETIOFFS ioctl.


To generate a diff of this commit:
cvs rdiff -u -r1.129 -r1.130 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 06:17:59 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix an (unintended) minor behavior on AUDIO_FLUSH.
On NetBSD7, when AUDIO_FLUSH was issued, .offset of AUDIO_GETOOFFS was
reinitialized (to one block ahead from zero) or unchanged depend on
whether the user encoding is hardware native or not (probably).
I don't believe that it's intended or we need to maintain it.
Now, AUDIO_FLUSH always clears the offset to zero.


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-23 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Apr 23 06:17:59 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix an (unintended) minor behavior on AUDIO_FLUSH.
On NetBSD7, when AUDIO_FLUSH was issued, .offset of AUDIO_GETOOFFS was
reinitialized (to one block ahead from zero) or unchanged depend on
whether the user encoding is hardware native or not (probably).
I don't believe that it's intended or we need to maintain it.
Now, AUDIO_FLUSH always clears the offset to zero.


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.128 src/sys/dev/audio/audio.c:1.129
--- src/sys/dev/audio/audio.c:1.128	Thu Apr 21 01:15:24 2022
+++ src/sys/dev/audio/audio.c	Sat Apr 23 06:17:59 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.128 2022/04/21 01:15:24 macallan Exp $	*/
+/*	$NetBSD: audio.c,v 1.129 2022/04/23 06:17:59 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.128 2022/04/21 01:15:24 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.129 2022/04/23 06:17:59 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -6280,8 +6280,9 @@ audio_track_clear(struct audio_softc *sc
 
 	audio_track_lock_enter(track);
 
-	track->usrbuf.used = 0;
 	/* Clear all internal parameters. */
+	track->usrbuf.used = 0;
+	track->usrbuf.head = 0;
 	if (track->codec.filter) {
 		track->codec.srcbuf.used = 0;
 		track->codec.srcbuf.head = 0;



CVS commit: src/sys/dev/audio

2022-04-20 Thread Michael Lorenz
Module Name:src
Committed By:   macallan
Date:   Thu Apr 21 01:15:25 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
#if 0 now unused audio_track_is_record() to appease clang


To generate a diff of this commit:
cvs rdiff -u -r1.127 -r1.128 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.127 src/sys/dev/audio/audio.c:1.128
--- src/sys/dev/audio/audio.c:1.127	Wed Apr 20 07:11:13 2022
+++ src/sys/dev/audio/audio.c	Thu Apr 21 01:15:24 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.127 2022/04/20 07:11:13 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.128 2022/04/21 01:15:24 macallan Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.127 2022/04/20 07:11:13 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.128 2022/04/21 01:15:24 macallan Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -733,6 +733,7 @@ audio_track_is_playback(const audio_trac
 	return ((track->mode & AUMODE_PLAY) != 0);
 }
 
+#if 0
 /* Return true if this track is a recording track. */
 static __inline bool
 audio_track_is_record(const audio_track_t *track)
@@ -740,6 +741,7 @@ audio_track_is_record(const audio_track_
 
 	return ((track->mode & AUMODE_RECORD) != 0);
 }
+#endif
 
 #if 0 /* XXX Not used yet */
 /*



CVS commit: src/sys/dev/audio

2022-04-20 Thread Michael Lorenz
Module Name:src
Committed By:   macallan
Date:   Thu Apr 21 01:15:25 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
#if 0 now unused audio_track_is_record() to appease clang


To generate a diff of this commit:
cvs rdiff -u -r1.127 -r1.128 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-20 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Apr 20 07:11:14 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio(4): Rework AUDIO_GETOOFFS.
- Count .samples/.deltablks in blocks.  It makes .deltablks integer wrap
  around safe.
- Remove suspicious one block offset from .offset.  I added the offset
  because it was observed so on NetBSD7.  But according to manpage, it
  should not be.  And it looks fine without the offset.
- Related to that, remove a comment in AUDIO_WSEEK.
  Limit the user-visible buffer to usrbuf only.


To generate a diff of this commit:
cvs rdiff -u -r1.126 -r1.127 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.17 -r1.18 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.126 src/sys/dev/audio/audio.c:1.127
--- src/sys/dev/audio/audio.c:1.126	Wed Apr 20 06:05:22 2022
+++ src/sys/dev/audio/audio.c	Wed Apr 20 07:11:13 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.126 2022/04/20 06:05:22 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.127 2022/04/20 07:11:13 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.126 2022/04/20 06:05:22 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.127 2022/04/20 07:11:13 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2985,7 +2985,7 @@ audio_ioctl(dev_t dev, struct audio_soft
 	audio_encoding_t *ae;
 	audio_format_query_t *query;
 	u_int stamp;
-	u_int offs;
+	u_int offset;
 	int val;
 	int index;
 	int error;
@@ -3104,28 +3104,23 @@ audio_ioctl(dev_t dev, struct audio_soft
 		}
 		mutex_enter(sc->sc_lock);
 		mutex_enter(sc->sc_intr_lock);
-		/* figure out where next DMA will start */
-		stamp = track->usrbuf_stamp;
-		offs = track->usrbuf.head;
+		/* figure out where next transfer will start */
+		stamp = track->stamp;
+		offset = track->usrbuf.head;
 		mutex_exit(sc->sc_intr_lock);
 		mutex_exit(sc->sc_lock);
 
-		ao->samples = stamp;
-		ao->deltablks = (stamp / track->usrbuf_blksize) -
-		(track->usrbuf_stamp_last / track->usrbuf_blksize);
-		track->usrbuf_stamp_last = stamp;
-		offs = rounddown(offs, track->usrbuf_blksize)
-		+ track->usrbuf_blksize;
-		if (offs >= track->usrbuf.capacity)
-			offs -= track->usrbuf.capacity;
-		ao->offset = offs;
-
+		/* samples will overflow soon but is as per spec. */
+		ao->samples = stamp * track->usrbuf_blksize;
+		ao->deltablks = stamp - track->last_stamp;
+		ao->offset = offset;
 		TRACET(2, track, "%s samples=%u deltablks=%u offset=%u",
 		pre, ao->samples, ao->deltablks, ao->offset);
+
+		track->last_stamp = stamp;
 		break;
 
 	case AUDIO_WSEEK:
-		/* XXX return value does not include outbuf one. */
 		track = file->ptrack;
 		if (track) {
 			val = track->usrbuf.used;
@@ -4964,8 +4959,6 @@ audio_track_play(audio_track_t *track)
 	count = uimin(usrbuf->used, track->usrbuf_blksize) / framesize;
 	bytes = count * framesize;
 
-	track->usrbuf_stamp += bytes;
-
 	if (usrbuf->head + bytes < usrbuf->capacity) {
 		memcpy((uint8_t *)input->mem + auring_tail(input) * framesize,
 		(uint8_t *)usrbuf->mem + usrbuf->head,
@@ -5060,6 +5053,8 @@ audio_track_play(audio_track_t *track)
 		track->outputcounter += track->outbuf.used - track_count_0;
 	}
 
+	track->stamp++;
+
 #if defined(AUDIO_DEBUG)
 	if (audiodebug >= 3) {
 		struct audio_track_debugbuf m;
@@ -6311,6 +6306,8 @@ audio_track_clear(struct audio_softc *sc
 	track->outbuf.used = 0;
 
 	/* Clear counters. */
+	track->stamp = 0;
+	track->last_stamp = 0;
 	track->dropframes = 0;
 
 	audio_track_lock_exit(track);
@@ -7773,7 +7770,7 @@ audiogetinfo(struct audio_softc *sc, str
 
 	if (ptrack) {
 		pi->seek = ptrack->usrbuf.used;
-		pi->samples = ptrack->usrbuf_stamp;
+		pi->samples = ptrack->stamp * ptrack->usrbuf_blksize;
 		pi->eof = ptrack->eofcounter;
 		pi->error = (ptrack->dropframes != 0) ? 1 : 0;
 		pi->open = 1;
@@ -7784,7 +7781,7 @@ audiogetinfo(struct audio_softc *sc, str
 
 	if (rtrack) {
 		ri->seek = audio_track_readablebytes(rtrack);
-		ri->samples = rtrack->usrbuf_stamp;
+		ri->samples = rtrack->stamp * rtrack->usrbuf_blksize;
 		ri->eof = 0;
 		ri->error = (rtrack->dropframes != 0) ? 1 : 0;
 		ri->open = 1;

Index: src/sys/dev/audio/audiodef.h
diff -u src/sys/dev/audio/audiodef.h:1.17 src/sys/dev/audio/audiodef.h:1.18
--- src/sys/dev/audio/audiodef.h:1.17	Wed Apr 20 06:05:22 2022
+++ src/sys/dev/audio/audiodef.h	Wed Apr 20 07:11:13 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audiodef.h,v 1.17 2022/04/20 06:05:22 isaki Exp $	*/
+/*	$NetBSD: audiodef.h,v 1.18 2022/04/20 07:11:13 isaki Exp $	*/
 
 /*
  * Copyright (C) 2017 Tetsuya Isaki. All rights reserved.
@@ -121,8 +121,6 @@ struct audio_track {
 	u_int		usrbuf_blksize;	/* usrbuf block size in bytes */
 	struct uvm_object *uobj;
 	bool		mmapped;	/* device is 

CVS commit: src/sys/dev/audio

2022-04-20 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Apr 20 07:11:14 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio(4): Rework AUDIO_GETOOFFS.
- Count .samples/.deltablks in blocks.  It makes .deltablks integer wrap
  around safe.
- Remove suspicious one block offset from .offset.  I added the offset
  because it was observed so on NetBSD7.  But according to manpage, it
  should not be.  And it looks fine without the offset.
- Related to that, remove a comment in AUDIO_WSEEK.
  Limit the user-visible buffer to usrbuf only.


To generate a diff of this commit:
cvs rdiff -u -r1.126 -r1.127 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.17 -r1.18 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-20 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Apr 20 06:05:22 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio(4): Make recording buffer more robust.
Previously, main buffer in recording track was usrbuf, which is the closest
buffer to the userland.  Because, this buffer arrangement was symmetrical
with the playback track, and had affinity with the past implementation.
However, in the current implementation, read(2) (from user application)
takes recorded block out from inputbuf, which is the closest buffer to
rmixer, to usrbuf.  So it was not good way to use the usrbuf as main buffer.
Now, usrbuf in recording track holds only fragment bytes in order to
transfer to the userland, and main buffer in recording track is the inputbuf,
the closest to rmixer.

Buffer size of the inputbuf is also modified.  Previously, it was less than
64KB or at least 4 blocks.  This had affinity with playback track and the
past implementation.  But this was not appropriate for both formats with
too large frames or too small frames.  In large frames (for example,
192kHz/12ch), 184KB buffer would be allocated but it corresponds to only
40msec.  In opposite, in small frames (for example, 8000Hz/1ch), 64KB
buffer would be allocated and it corresponds to 4.1 seconds.  But for such
machines that have 8000Hz/1ch device, in-kernel 64KB memory would probably
be expensive.
Now, inputbuf will always be allocated 16(NBLKIN) blocks, regardless of its
hardware format.  It corresponds to 160msec on modern archs (if blk_ms=10),
or 640msec on antique archs (if blk_ms=40).


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.126 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.16 -r1.17 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-20 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Apr 20 06:05:22 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
audio(4): Make recording buffer more robust.
Previously, main buffer in recording track was usrbuf, which is the closest
buffer to the userland.  Because, this buffer arrangement was symmetrical
with the playback track, and had affinity with the past implementation.
However, in the current implementation, read(2) (from user application)
takes recorded block out from inputbuf, which is the closest buffer to
rmixer, to usrbuf.  So it was not good way to use the usrbuf as main buffer.
Now, usrbuf in recording track holds only fragment bytes in order to
transfer to the userland, and main buffer in recording track is the inputbuf,
the closest to rmixer.

Buffer size of the inputbuf is also modified.  Previously, it was less than
64KB or at least 4 blocks.  This had affinity with playback track and the
past implementation.  But this was not appropriate for both formats with
too large frames or too small frames.  In large frames (for example,
192kHz/12ch), 184KB buffer would be allocated but it corresponds to only
40msec.  In opposite, in small frames (for example, 8000Hz/1ch), 64KB
buffer would be allocated and it corresponds to 4.1 seconds.  But for such
machines that have 8000Hz/1ch device, in-kernel 64KB memory would probably
be expensive.
Now, inputbuf will always be allocated 16(NBLKIN) blocks, regardless of its
hardware format.  It corresponds to 160msec on modern archs (if blk_ms=10),
or 640msec on antique archs (if blk_ms=40).


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.126 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.16 -r1.17 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.125 src/sys/dev/audio/audio.c:1.126
--- src/sys/dev/audio/audio.c:1.125	Wed Apr 20 04:41:29 2022
+++ src/sys/dev/audio/audio.c	Wed Apr 20 06:05:22 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.125 2022/04/20 04:41:29 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.126 2022/04/20 06:05:22 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.125 2022/04/20 04:41:29 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.126 2022/04/20 06:05:22 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -608,7 +608,8 @@ static void audio_rintr(void *);
 
 static int audio_query_devinfo(struct audio_softc *, mixer_devinfo_t *);
 
-static __inline int audio_track_readablebytes(const audio_track_t *);
+static int audio_track_inputblk_as_usrbyte(const audio_track_t *, int);
+static int audio_track_readablebytes(const audio_track_t *);
 static int audio_file_setinfo(struct audio_softc *, audio_file_t *,
 	const struct audio_info *);
 static int audio_track_setinfo_check(audio_track_t *,
@@ -2775,10 +2776,10 @@ audio_read(struct audio_softc *sc, struc
 		int bytes;
 
 		TRACET(3, track,
-		"while resid=%zd input=%d/%d/%d usrbuf=%d/%d/H%d",
+		"while resid=%zd input=%d/%d/%d usrbuf=%d/%d/C%d",
 		uio->uio_resid,
 		input->head, input->used, input->capacity,
-		usrbuf->head, usrbuf->used, track->usrbuf_usedhigh);
+		usrbuf->head, usrbuf->used, usrbuf->capacity);
 
 		/* Wait when buffers are empty. */
 		mutex_enter(sc->sc_lock);
@@ -2805,34 +2806,27 @@ audio_read(struct audio_softc *sc, struc
 		mutex_exit(sc->sc_lock);
 
 		audio_track_lock_enter(track);
-		/* Convert as many blocks as possible. */
-		while (usrbuf->used <=
-		track->usrbuf_usedhigh - track->usrbuf_blksize &&
-		input->used > 0) {
+		/* Convert one block if possible. */
+		if (usrbuf->used == 0 && input->used > 0) {
 			audio_track_record(track);
 		}
 
 		/* uiomove from usrbuf as many bytes as possible. */
 		bytes = uimin(usrbuf->used, uio->uio_resid);
-		while (bytes > 0) {
-			int head = usrbuf->head;
-			int len = uimin(bytes, usrbuf->capacity - head);
-			error = uiomove((uint8_t *)usrbuf->mem + head, len,
-			uio);
-			if (error) {
-audio_track_lock_exit(track);
-device_printf(sc->sc_dev,
-"%s: uiomove(%d) failed: errno=%d\n",
-__func__, len, error);
-goto abort;
-			}
-			auring_take(usrbuf, len);
-			track->useriobytes += len;
-			TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
-			len,
-			usrbuf->head, usrbuf->used, usrbuf->capacity);
-			bytes -= len;
+		error = uiomove((uint8_t *)usrbuf->mem + usrbuf->head, bytes,
+		uio);
+		if (error) {
+			audio_track_lock_exit(track);
+			device_printf(sc->sc_dev,
+			"%s: uiomove(%d) failed: errno=%d\n",
+			__func__, bytes, error);
+			goto abort;
 		}
+		auring_take(usrbuf, bytes);
+		track->useriobytes += bytes;
+		TRACET(3, track, "uiomove(len=%d) usrbuf=%d/%d/C%d",
+		bytes,
+	

CVS commit: src/sys/dev/audio

2022-04-19 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Apr 20 04:41:29 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Make debug messages better in audio_ioctl() and mixier_ioctl().
Divide by case.  Reduce to one line if possible.


To generate a diff of this commit:
cvs rdiff -u -r1.124 -r1.125 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.124 src/sys/dev/audio/audio.c:1.125
--- src/sys/dev/audio/audio.c:1.124	Tue Apr 19 09:19:53 2022
+++ src/sys/dev/audio/audio.c	Wed Apr 20 04:41:29 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.124 2022/04/19 09:19:53 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.125 2022/04/20 04:41:29 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.124 2022/04/19 09:19:53 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.125 2022/04/20 04:41:29 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2992,40 +2992,44 @@ audio_ioctl(dev_t dev, struct audio_soft
 	audio_format_query_t *query;
 	u_int stamp;
 	u_int offs;
-	int fd;
+	int val;
 	int index;
 	int error;
 
 #if defined(AUDIO_DEBUG)
 	const char *ioctlnames[] = {
-		" AUDIO_GETINFO",	/* 21 */
-		" AUDIO_SETINFO",	/* 22 */
-		" AUDIO_DRAIN",		/* 23 */
-		" AUDIO_FLUSH",		/* 24 */
-		" AUDIO_WSEEK",		/* 25 */
-		" AUDIO_RERROR",	/* 26 */
-		" AUDIO_GETDEV",	/* 27 */
-		" AUDIO_GETENC",	/* 28 */
-		" AUDIO_GETFD",		/* 29 */
-		" AUDIO_SETFD",		/* 30 */
-		" AUDIO_PERROR",	/* 31 */
-		" AUDIO_GETIOFFS",	/* 32 */
-		" AUDIO_GETOOFFS",	/* 33 */
-		" AUDIO_GETPROPS",	/* 34 */
-		" AUDIO_GETBUFINFO",	/* 35 */
-		" AUDIO_SETCHAN",	/* 36 */
-		" AUDIO_GETCHAN",	/* 37 */
-		" AUDIO_QUERYFORMAT",	/* 38 */
-		" AUDIO_GETFORMAT",	/* 39 */
-		" AUDIO_SETFORMAT",	/* 40 */
+		"AUDIO_GETINFO",	/* 21 */
+		"AUDIO_SETINFO",	/* 22 */
+		"AUDIO_DRAIN",		/* 23 */
+		"AUDIO_FLUSH",		/* 24 */
+		"AUDIO_WSEEK",		/* 25 */
+		"AUDIO_RERROR",		/* 26 */
+		"AUDIO_GETDEV",		/* 27 */
+		"AUDIO_GETENC",		/* 28 */
+		"AUDIO_GETFD",		/* 29 */
+		"AUDIO_SETFD",		/* 30 */
+		"AUDIO_PERROR",		/* 31 */
+		"AUDIO_GETIOFFS",	/* 32 */
+		"AUDIO_GETOOFFS",	/* 33 */
+		"AUDIO_GETPROPS",	/* 34 */
+		"AUDIO_GETBUFINFO",	/* 35 */
+		"AUDIO_SETCHAN",	/* 36 */
+		"AUDIO_GETCHAN",	/* 37 */
+		"AUDIO_QUERYFORMAT",	/* 38 */
+		"AUDIO_GETFORMAT",	/* 39 */
+		"AUDIO_SETFORMAT",	/* 40 */
 	};
+	char pre[64];
 	int nameidx = (cmd & 0xff);
-	const char *ioctlname = "";
-	if (21 <= nameidx && nameidx <= 21 + __arraycount(ioctlnames))
-		ioctlname = ioctlnames[nameidx - 21];
-	TRACEF(2, file, "(%lu,'%c',%lu)%s pid=%d.%d",
-	IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), cmd&0xff, ioctlname,
-	(int)curproc->p_pid, (int)l->l_lid);
+	if (21 <= nameidx && nameidx <= 21 + __arraycount(ioctlnames)) {
+		snprintf(pre, sizeof(pre), "pid=%d.%d %s",
+		(int)curproc->p_pid, (int)l->l_lid,
+		ioctlnames[nameidx - 21]);
+	} else {
+		snprintf(pre, sizeof(pre), "pid=%d.%d (%lu,'%c',%u)",
+		(int)curproc->p_pid, (int)l->l_lid,
+		IOCPARM_LEN(cmd), (char)IOCGROUP(cmd), nameidx);
+	}
 #endif
 
 	error = 0;
@@ -3036,10 +3040,15 @@ audio_ioctl(dev_t dev, struct audio_soft
 
 	case FIONREAD:
 		/* Get the number of bytes that can be read. */
-		if (file->rtrack) {
-			*(int *)addr = audio_track_readablebytes(file->rtrack);
+		track = file->rtrack;
+		if (track) {
+			val = audio_track_readablebytes(track);
+			*(int *)addr = val;
+			TRACET(2, track, "pid=%d.%d FIONREAD bytes=%d",
+			(int)curproc->p_pid, (int)l->l_lid, val);
 		} else {
-			*(int *)addr = 0;
+			TRACEF(2, file, "pid=%d.%d FIONREAD no track",
+			(int)curproc->p_pid, (int)l->l_lid);
 		}
 		break;
 
@@ -3047,48 +3056,42 @@ audio_ioctl(dev_t dev, struct audio_soft
 		/* Set/Clear ASYNC I/O. */
 		if (*(int *)addr) {
 			file->async_audio = curproc->p_pid;
-			TRACEF(2, file, "FIOASYNC pid %d", file->async_audio);
 		} else {
 			file->async_audio = 0;
-			TRACEF(2, file, "FIOASYNC off");
 		}
+		TRACEF(2, file, "pid=%d.%d FIOASYNC %s",
+		(int)curproc->p_pid, (int)l->l_lid,
+		file->async_audio ? "on" : "off");
 		break;
 
 	case AUDIO_FLUSH:
 		/* XXX TODO: clear errors and restart? */
+		TRACEF(2, file, "%s", pre);
 		audio_file_clear(sc, file);
 		break;
 
-	case AUDIO_RERROR:
-		/*
-		 * Number of read bytes dropped.  We don't know where
-		 * or when they were dropped (including conversion stage).
-		 * Therefore, the number of accurate bytes or samples is
-		 * also unknown.
-		 */
-		track = file->rtrack;
-		if (track) {
-			*(int *)addr = frametobyte(>usrbuf.fmt,
-			track->dropframes);
-		}
-		break;
-
 	case AUDIO_PERROR:
+	case AUDIO_RERROR:
 		/*
-		 * Number of write bytes dropped.  We don't know where
-		 * or when they were dropped (including conversion 

CVS commit: src/sys/dev/audio

2022-04-19 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Apr 20 04:41:29 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Make debug messages better in audio_ioctl() and mixier_ioctl().
Divide by case.  Reduce to one line if possible.


To generate a diff of this commit:
cvs rdiff -u -r1.124 -r1.125 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-19 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Apr 19 09:19:53 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Wait for opens to drain in detach.

Otherwise detach may barge ahead and start freeing things before open
has finished and is about to use them after free.

Reported-by: syzbot+31d2619e72c2c8436...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.123 -r1.124 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.123 src/sys/dev/audio/audio.c:1.124
--- src/sys/dev/audio/audio.c:1.123	Sat Apr  9 23:35:58 2022
+++ src/sys/dev/audio/audio.c	Tue Apr 19 09:19:53 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.123 2022/04/09 23:35:58 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.124 2022/04/19 09:19:53 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.123 2022/04/09 23:35:58 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.124 2022/04/19 09:19:53 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -1344,6 +1344,7 @@ audiodetach(device_t self, int flags)
 {
 	struct audio_softc *sc;
 	struct audio_file *file;
+	int maj, mn;
 	int error;
 
 	sc = device_private(self);
@@ -1359,6 +1360,16 @@ audiodetach(device_t self, int flags)
 		return error;
 
 	/*
+	 * Prevent new opens and wait for existing opens to complete.
+	 */
+	maj = cdevsw_lookup_major(_cdevsw);
+	mn = device_unit(self);
+	vdevgone(maj, mn|SOUND_DEVICE, mn|SOUND_DEVICE, VCHR);
+	vdevgone(maj, mn|AUDIO_DEVICE, mn|AUDIO_DEVICE, VCHR);
+	vdevgone(maj, mn|AUDIOCTL_DEVICE, mn|AUDIOCTL_DEVICE, VCHR);
+	vdevgone(maj, mn|MIXER_DEVICE, mn|MIXER_DEVICE, VCHR);
+
+	/*
 	 * This waits currently running sysctls to finish if exists.
 	 * After this, no more new sysctls will come.
 	 */



CVS commit: src/sys/dev/audio

2022-04-19 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Tue Apr 19 09:19:53 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Wait for opens to drain in detach.

Otherwise detach may barge ahead and start freeing things before open
has finished and is about to use them after free.

Reported-by: syzbot+31d2619e72c2c8436...@syzkaller.appspotmail.com


To generate a diff of this commit:
cvs rdiff -u -r1.123 -r1.124 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-04-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr  9 23:35:58 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Use membar_acquire, not membar_enter.

Cheaper and adequate to make an atomic_swap into a load-acquire.


To generate a diff of this commit:
cvs rdiff -u -r1.122 -r1.123 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.122 src/sys/dev/audio/audio.c:1.123
--- src/sys/dev/audio/audio.c:1.122	Thu Mar 31 19:30:15 2022
+++ src/sys/dev/audio/audio.c	Sat Apr  9 23:35:58 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.122 2022/03/31 19:30:15 pgoyette Exp $	*/
+/*	$NetBSD: audio.c,v 1.123 2022/04/09 23:35:58 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.122 2022/03/31 19:30:15 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.123 2022/04/09 23:35:58 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -316,7 +316,7 @@ audio_mlog_flush(void)
 	/* Nothing to do if already in use ? */
 	if (atomic_swap_32(_inuse, 1) == 1)
 		return;
-	membar_enter();
+	membar_acquire();
 
 	int rpage = mlog_wpage;
 	mlog_wpage ^= 1;
@@ -353,7 +353,7 @@ audio_mlog_printf(const char *fmt, ...)
 		mlog_drop++;
 		return;
 	}
-	membar_enter();
+	membar_acquire();
 
 	va_start(ap, fmt);
 	len = vsnprintf(
@@ -1684,7 +1684,7 @@ audio_track_lock_tryenter(audio_track_t 
 
 	if (atomic_swap_uint(>lock, 1) != 0)
 		return false;
-	membar_enter();
+	membar_acquire();
 	return true;
 }
 



CVS commit: src/sys/dev/audio

2022-04-09 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sat Apr  9 23:35:58 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Use membar_acquire, not membar_enter.

Cheaper and adequate to make an atomic_swap into a load-acquire.


To generate a diff of this commit:
cvs rdiff -u -r1.122 -r1.123 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Mar 28 12:39:57 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Use d_cfdriver/devtounit to avoid open/detach races.


To generate a diff of this commit:
cvs rdiff -u -r1.120 -r1.121 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.120 src/sys/dev/audio/audio.c:1.121
--- src/sys/dev/audio/audio.c:1.120	Sat Mar 26 06:49:27 2022
+++ src/sys/dev/audio/audio.c	Mon Mar 28 12:39:57 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.120 2022/03/26 06:49:27 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.121 2022/03/28 12:39:57 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -181,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.120 2022/03/26 06:49:27 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.121 2022/03/28 12:39:57 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -567,7 +567,6 @@ static int audio_exlock_mutex_enter(stru
 static void audio_exlock_mutex_exit(struct audio_softc *);
 static int audio_exlock_enter(struct audio_softc *);
 static void audio_exlock_exit(struct audio_softc *);
-static void audio_sc_acquire_foropen(struct audio_softc *, struct psref *);
 static struct audio_softc *audio_sc_acquire_fromfile(audio_file_t *,
 	struct psref *);
 static void audio_sc_release(struct audio_softc *, struct psref *);
@@ -766,6 +765,13 @@ audio_volume_to_outer(u_int v)
 static dev_type_open(audioopen);
 /* XXXMRG use more dev_type_xxx */
 
+static int
+audiounit(dev_t dev)
+{
+
+	return AUDIOUNIT(dev);
+}
+
 const struct cdevsw audio_cdevsw = {
 	.d_open = audioopen,
 	.d_close = noclose,
@@ -778,6 +784,8 @@ const struct cdevsw audio_cdevsw = {
 	.d_mmap = nommap,
 	.d_kqfilter = nokqfilter,
 	.d_discard = nodiscard,
+	.d_cfdriver = _cd,
+	.d_devtounit = audiounit,
 	.d_flag = D_OTHER | D_MPSAFE
 };
 
@@ -1590,31 +1598,6 @@ audio_exlock_exit(struct audio_softc *sc
 }
 
 /*
- * Increment reference counter for this sc.
- * This is intended to be used for open.
- */
-void
-audio_sc_acquire_foropen(struct audio_softc *sc, struct psref *refp)
-{
-	int s;
-
-	/* Block audiodetach while we acquire a reference */
-	s = pserialize_read_enter();
-
-	/*
-	 * We don't examine sc_dying here.  However, all open methods
-	 * call audio_exlock_enter() right after this, so we can examine
-	 * sc_dying in it.
-	 */
-
-	/* Acquire a reference */
-	psref_acquire(refp, >sc_psref, audio_psref_class);
-
-	/* Now sc won't go away until we drop the reference count */
-	pserialize_read_exit(s);
-}
-
-/*
  * Get sc from file, and increment reference counter for this sc.
  * This is intended to be used for methods other than open.
  * If successful, returns sc.  Otherwise returns NULL.
@@ -1732,21 +1715,20 @@ static int
 audioopen(dev_t dev, int flags, int ifmt, struct lwp *l)
 {
 	struct audio_softc *sc;
-	struct psref sc_ref;
-	int bound;
 	int error;
 
-	/* Find the device */
+	/*
+	 * Find the device.  Because we wired the cdevsw to the audio
+	 * autoconf instance, the system ensures it will not go away
+	 * until after we return.
+	 */
 	sc = device_lookup_private(_cd, AUDIOUNIT(dev));
 	if (sc == NULL || sc->hw_if == NULL)
 		return ENXIO;
 
-	bound = curlwp_bind();
-	audio_sc_acquire_foropen(sc, _ref);
-
 	error = audio_exlock_enter(sc);
 	if (error)
-		goto done;
+		return error;
 
 	device_active(sc->sc_dev, DVA_SYSTEM);
 	switch (AUDIODEV(dev)) {
@@ -1766,9 +1748,6 @@ audioopen(dev_t dev, int flags, int ifmt
 	}
 	audio_exlock_exit(sc);
 
-done:
-	audio_sc_release(sc, _ref);
-	curlwp_bindx(bound);
 	return error;
 }
 
@@ -2150,30 +2129,42 @@ done:
 int
 audiobellopen(dev_t dev, audio_file_t **filep)
 {
+	device_t audiodev = NULL;
 	struct audio_softc *sc;
-	struct psref sc_ref;
-	int bound;
+	bool exlock = false;
 	int error;
 
-	/* Find the device */
-	sc = device_lookup_private(_cd, AUDIOUNIT(dev));
-	if (sc == NULL || sc->hw_if == NULL)
-		return ENXIO;
+	/*
+	 * Find the autoconf instance and make sure it doesn't go away
+	 * while we are opening it.
+	 */
+	audiodev = device_lookup_acquire(_cd, AUDIOUNIT(dev));
+	if (audiodev == NULL) {
+		error = ENXIO;
+		goto out;
+	}
 
-	bound = curlwp_bind();
-	audio_sc_acquire_foropen(sc, _ref);
+	/* If attach failed, it's hopeless -- give up.  */
+	sc = device_private(audiodev);
+	if (sc->hw_if == NULL) {
+		error = ENXIO;
+		goto out;
+	}
 
+	/* Take the exclusive configuration lock.  */
 	error = audio_exlock_enter(sc);
 	if (error)
-		goto done;
+		goto out;
+	exlock = true;
 
+	/* Open the audio device.  */
 	device_active(sc->sc_dev, DVA_SYSTEM);
 	error = audio_open(dev, sc, FWRITE, 0, curlwp, filep);
 
-	audio_exlock_exit(sc);
-done:
-	audio_sc_release(sc, _ref);
-	curlwp_bindx(bound);
+out:	if (exlock)
+		audio_exlock_exit(sc);
+	if 

CVS commit: src/sys/dev/audio

2022-03-28 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Mar 28 12:39:57 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Use d_cfdriver/devtounit to avoid open/detach races.


To generate a diff of this commit:
cvs rdiff -u -r1.120 -r1.121 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:49:27 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Add terminology comments.


To generate a diff of this commit:
cvs rdiff -u -r1.119 -r1.120 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.119 src/sys/dev/audio/audio.c:1.120
--- src/sys/dev/audio/audio.c:1.119	Sat Mar 26 06:43:36 2022
+++ src/sys/dev/audio/audio.c	Sat Mar 26 06:49:27 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.119 2022/03/26 06:43:36 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.120 2022/03/26 06:49:27 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -63,6 +63,49 @@
  */
 
 /*
+ * Terminology: "sample", "channel", "frame", "block", "track":
+ *
+ *  channel   frame
+ *   |   
+ *   v   :  :\
+ *+--:--:--:-  -+--+ : +--+-..   |
+ *  #0(L) |sample|sample|sample| .. |sample| : |sample|  |
+ *+--:--:--:-  -+--+ : +--+-..   |
+ *  #1(R) |sample|sample|sample| .. |sample| : |sample|  |
+ *+--:--:--:-  -+--+ : +--+-..   | track
+ *   :   :  ::   |
+ *+--:--:--:-  -+--+ : +--+-..   |
+ *|sample|sample|sample| .. |sample| : |sample|  |
+ *+--:--:--:-  -+--+ : +--+-..   |
+ *   :  :/
+ *   
+ *
+ *\/   \..
+ * block
+ *
+ * - A "frame" is the minimum unit in the time axis direction, and consists
+ *   of samples for the number of channels.
+ * - A "block" is basic length of processing.  The audio layer basically
+ *   handles audio data stream block by block, asks underlying hardware to
+ *   process them block by block, and then the hardware raises interrupt by
+ *   each block.
+ * - A "track" is single completed audio stream.
+ *
+ * For example, the hardware block is assumed to be 10 msec, and your audio
+ * track consists of 2.1(=3) channels 44.1kHz 16bit PCM,
+ *
+ * "channel" = 3
+ * "sample" = 2 [bytes]
+ * "frame" = 2 [bytes/sample] * 3 [channels] = 6 [bytes]
+ * "block" = 44100 [Hz] * (10/1000) [seconds] * 6 [bytes/frame] = 2646 [bytes]
+ *
+ * The terminologies shown here are only for this MI audio layer.  Note that
+ * different terminologies may be used in each manufacturer's datasheet, and
+ * each MD driver may follow it.  For example, what we call a "block" is
+ * called a "frame" in sys/dev/pci/yds.c.
+ */
+
+/*
  * Locking: there are three locks per device.
  *
  * - sc_lock, provided by the underlying driver.  This is an adaptive lock,
@@ -138,7 +181,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.119 2022/03/26 06:43:36 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.120 2022/03/26 06:49:27 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:49:27 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Add terminology comments.


To generate a diff of this commit:
cvs rdiff -u -r1.119 -r1.120 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:43:36 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Improve comments.


To generate a diff of this commit:
cvs rdiff -u -r1.118 -r1.119 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.118 src/sys/dev/audio/audio.c:1.119
--- src/sys/dev/audio/audio.c:1.118	Sat Mar 26 06:41:12 2022
+++ src/sys/dev/audio/audio.c	Sat Mar 26 06:43:36 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.118 2022/03/26 06:41:12 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.119 2022/03/26 06:43:36 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.118 2022/03/26 06:41:12 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.119 2022/03/26 06:43:36 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2767,7 +2767,7 @@ audio_read(struct audio_softc *sc, struc
 			audio_track_record(track);
 		}
 
-		/* uiomove from usrbuf as much as possible. */
+		/* uiomove from usrbuf as many bytes as possible. */
 		bytes = uimin(usrbuf->used, uio->uio_resid);
 		while (bytes > 0) {
 			int head = usrbuf->head;
@@ -2896,7 +2896,7 @@ audio_write(struct audio_softc *sc, stru
 
 		audio_track_lock_enter(track);
 
-		/* uiomove to usrbuf as much as possible. */
+		/* uiomove to usrbuf as many bytes as possible. */
 		bytes = uimin(track->usrbuf_usedhigh - usrbuf->used,
 		uio->uio_resid);
 		while (bytes > 0) {
@@ -2919,7 +2919,7 @@ audio_write(struct audio_softc *sc, stru
 			bytes -= len;
 		}
 
-		/* Convert them as much as possible. */
+		/* Convert them as many blocks as possible. */
 		while (usrbuf->used >= track->usrbuf_blksize &&
 		outbuf->used < outbuf->capacity) {
 			audio_track_play(track);
@@ -5905,7 +5905,10 @@ audio_rmixer_process(struct audio_softc 
 			continue;
 		}
 
-		/* If the track buffer is full, discard the oldest one? */
+		/*
+		 * If the track buffer has less than one block of free space,
+		 * make one block free.
+		 */
 		input = track->input;
 		if (input->capacity - input->used < mixer->frames_per_block) {
 			int drops = mixer->frames_per_block -



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:43:36 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Improve comments.


To generate a diff of this commit:
cvs rdiff -u -r1.118 -r1.119 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:41:12 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Remove a dead code in audio_track_record().


To generate a diff of this commit:
cvs rdiff -u -r1.117 -r1.118 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.117 src/sys/dev/audio/audio.c:1.118
--- src/sys/dev/audio/audio.c:1.117	Sat Mar 26 06:36:06 2022
+++ src/sys/dev/audio/audio.c	Sat Mar 26 06:41:12 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.117 2022/03/26 06:36:06 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.118 2022/03/26 06:41:12 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.117 2022/03/26 06:36:06 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.118 2022/03/26 06:41:12 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -4912,11 +4912,8 @@ audio_track_record(audio_track_t *track)
 	KASSERT(track);
 	KASSERT(track->lock);
 
-	/* Number of frames to process */
-	count = auring_get_contig_used(track->input);
-	count = uimin(count, track->mixer->frames_per_block);
-	if (count == 0) {
-		TRACET(4, track, "count == 0");
+	if (auring_get_contig_used(track->input) == 0) {
+		TRACET(4, track, "input->used == 0");
 		return;
 	}
 



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:41:12 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Remove a dead code in audio_track_record().


To generate a diff of this commit:
cvs rdiff -u -r1.117 -r1.118 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:36:06 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Clarify the assertion in audio_rmixer_process().
By previous commit (r1.116), the assersion no longer fires even without
this modification.  But the condition was a bit inaccurate.
There is no need to check the data length must be aligned to blocks here
(though it also should be aligned now).  What we should check here is that
the tail must be aligned.


To generate a diff of this commit:
cvs rdiff -u -r1.116 -r1.117 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.116 src/sys/dev/audio/audio.c:1.117
--- src/sys/dev/audio/audio.c:1.116	Sat Mar 26 06:27:32 2022
+++ src/sys/dev/audio/audio.c	Sat Mar 26 06:36:06 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.116 2022/03/26 06:27:32 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.117 2022/03/26 06:36:06 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.116 2022/03/26 06:27:32 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.117 2022/03/26 06:36:06 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -5919,10 +5919,10 @@ audio_rmixer_process(struct audio_softc 
 			input->head, input->used, input->capacity);
 			auring_take(input, drops);
 		}
-		KASSERTMSG(input->used % mixer->frames_per_block == 0,
-		"input->used=%d mixer->frames_per_block=%d",
-		input->used, mixer->frames_per_block);
 
+		KASSERTMSG(auring_tail(input) % mixer->frames_per_block == 0,
+		"inputtail=%d mixer->frames_per_block=%d",
+		auring_tail(input), mixer->frames_per_block);
 		memcpy(auring_tailptr_aint(input),
 		auring_headptr_aint(mixersrc),
 		bytes);



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:36:06 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Clarify the assertion in audio_rmixer_process().
By previous commit (r1.116), the assersion no longer fires even without
this modification.  But the condition was a bit inaccurate.
There is no need to check the data length must be aligned to blocks here
(though it also should be aligned now).  What we should check here is that
the tail must be aligned.


To generate a diff of this commit:
cvs rdiff -u -r1.116 -r1.117 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:27:32 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix conditions that audio_read() calls audio_track_record().
audio_track_record() must be called when usrbuf has at least one free block.

I hope that this will fix the panic reported in PR kern/56644.
When an user process specifies the hardware format as its recording format
(i.e., there is no track conversions), if the user process read(2) a small
amount of data and the rmixer_process then runs, depending on the conditions,
the panic may happen.  I have never reproduced it because it's difficult to
do intentionally.

Thanks Y.Sugahara and riastradh@ for help and comments.


To generate a diff of this commit:
cvs rdiff -u -r1.115 -r1.116 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.115 src/sys/dev/audio/audio.c:1.116
--- src/sys/dev/audio/audio.c:1.115	Mon Mar 14 21:38:04 2022
+++ src/sys/dev/audio/audio.c	Sat Mar 26 06:27:32 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.115 2022/03/14 21:38:04 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.116 2022/03/26 06:27:32 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.115 2022/03/14 21:38:04 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.116 2022/03/26 06:27:32 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2760,7 +2760,12 @@ audio_read(struct audio_softc *sc, struc
 		mutex_exit(sc->sc_lock);
 
 		audio_track_lock_enter(track);
-		audio_track_record(track);
+		/* Convert as many blocks as possible. */
+		while (usrbuf->used <=
+		track->usrbuf_usedhigh - track->usrbuf_blksize &&
+		input->used > 0) {
+			audio_track_record(track);
+		}
 
 		/* uiomove from usrbuf as much as possible. */
 		bytes = uimin(usrbuf->used, uio->uio_resid);
@@ -4938,6 +4943,8 @@ audio_track_record(audio_track_t *track)
 	/* Copy outbuf to usrbuf */
 	outbuf = >outbuf;
 	usrbuf = >usrbuf;
+	/* usrbuf must have at least one free block. */
+	KASSERT(usrbuf->used <= track->usrbuf_usedhigh - track->usrbuf_blksize);
 	/*
 	 * framesize is always 1 byte or more since all formats supported
 	 * as usrfmt(=output) have 8bit or more stride.
@@ -4949,8 +4956,7 @@ audio_track_record(audio_track_t *track)
 	 * bytes is the number of bytes to copy to usrbuf.
 	 */
 	count = outbuf->used;
-	count = uimin(count,
-	(track->usrbuf_usedhigh - usrbuf->used) / framesize);
+	count = uimin(count, track->usrbuf_blksize / framesize);
 	bytes = count * framesize;
 	if (auring_tail(usrbuf) + bytes < usrbuf->capacity) {
 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),



CVS commit: src/sys/dev/audio

2022-03-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Mar 26 06:27:32 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix conditions that audio_read() calls audio_track_record().
audio_track_record() must be called when usrbuf has at least one free block.

I hope that this will fix the panic reported in PR kern/56644.
When an user process specifies the hardware format as its recording format
(i.e., there is no track conversions), if the user process read(2) a small
amount of data and the rmixer_process then runs, depending on the conditions,
the panic may happen.  I have never reproduced it because it's difficult to
do intentionally.

Thanks Y.Sugahara and riastradh@ for help and comments.


To generate a diff of this commit:
cvs rdiff -u -r1.115 -r1.116 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Mar 14 21:38:04 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix typo in previous -- atomic_store_release, not reease.

Built the wrong kernel to compile-test AUDIO_DEBUG, oops.


To generate a diff of this commit:
cvs rdiff -u -r1.114 -r1.115 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.114 src/sys/dev/audio/audio.c:1.115
--- src/sys/dev/audio/audio.c:1.114	Mon Mar 14 11:47:33 2022
+++ src/sys/dev/audio/audio.c	Mon Mar 14 21:38:04 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.114 2022/03/14 11:47:33 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.115 2022/03/14 21:38:04 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.114 2022/03/14 11:47:33 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.115 2022/03/14 21:38:04 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -280,7 +280,7 @@ audio_mlog_flush(void)
 	mlog_buf[mlog_wpage][0] = '\0';
 	mlog_used = 0;
 
-	atomic_store_reease(_inuse, 0);
+	atomic_store_release(_inuse, 0);
 
 	if (mlog_buf[rpage][0] != '\0') {
 		printf("%s", mlog_buf[rpage]);



CVS commit: src/sys/dev/audio

2022-03-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Mar 14 21:38:04 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Fix typo in previous -- atomic_store_release, not reease.

Built the wrong kernel to compile-test AUDIO_DEBUG, oops.


To generate a diff of this commit:
cvs rdiff -u -r1.114 -r1.115 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Mar 14 11:47:34 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Membar audit.

Won't affect anything on x86 because atomic r/m/w operations are
always full sequential consistency barriers, but might potentially
fix problems on, e.g., arm.

Note 1: I'm not clear on why the track lock is a bespoke mutex made
out of an atomic -- why not just mutex(9)?

Note 2: I'm not convinced the audio_mlog_flush synchronization is
correct; what happens if the softint runs on two CPUs at the same
time and swaps mlog_wpage simultaneously?

Note 3: Should maybe use atomic_load/store_relaxed for mlog_full and
mlog_drop, and atomic_inc/dec for mlog_refs.


To generate a diff of this commit:
cvs rdiff -u -r1.113 -r1.114 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2022-03-14 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Mon Mar 14 11:47:34 UTC 2022

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(4): Membar audit.

Won't affect anything on x86 because atomic r/m/w operations are
always full sequential consistency barriers, but might potentially
fix problems on, e.g., arm.

Note 1: I'm not clear on why the track lock is a bespoke mutex made
out of an atomic -- why not just mutex(9)?

Note 2: I'm not convinced the audio_mlog_flush synchronization is
correct; what happens if the softint runs on two CPUs at the same
time and swaps mlog_wpage simultaneously?

Note 3: Should maybe use atomic_load/store_relaxed for mlog_full and
mlog_drop, and atomic_inc/dec for mlog_refs.


To generate a diff of this commit:
cvs rdiff -u -r1.113 -r1.114 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.113 src/sys/dev/audio/audio.c:1.114
--- src/sys/dev/audio/audio.c:1.113	Sun Dec 12 13:05:13 2021
+++ src/sys/dev/audio/audio.c	Mon Mar 14 11:47:33 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.113 2021/12/12 13:05:13 andvar Exp $	*/
+/*	$NetBSD: audio.c,v 1.114 2022/03/14 11:47:33 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.113 2021/12/12 13:05:13 andvar Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.114 2022/03/14 11:47:33 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -162,6 +162,7 @@ __KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -272,13 +273,14 @@ audio_mlog_flush(void)
 	/* Nothing to do if already in use ? */
 	if (atomic_swap_32(_inuse, 1) == 1)
 		return;
+	membar_enter();
 
 	int rpage = mlog_wpage;
 	mlog_wpage ^= 1;
 	mlog_buf[mlog_wpage][0] = '\0';
 	mlog_used = 0;
 
-	atomic_swap_32(_inuse, 0);
+	atomic_store_reease(_inuse, 0);
 
 	if (mlog_buf[rpage][0] != '\0') {
 		printf("%s", mlog_buf[rpage]);
@@ -308,6 +310,7 @@ audio_mlog_printf(const char *fmt, ...)
 		mlog_drop++;
 		return;
 	}
+	membar_enter();
 
 	va_start(ap, fmt);
 	len = vsnprintf(
@@ -321,7 +324,7 @@ audio_mlog_printf(const char *fmt, ...)
 		mlog_full++;
 	}
 
-	atomic_swap_32(_inuse, 0);
+	atomic_store_release(_inuse, 0);
 
 	if (mlog_sih)
 		softint_schedule(mlog_sih);
@@ -1652,7 +1655,11 @@ audio_track_waitio(struct audio_softc *s
 static __inline bool
 audio_track_lock_tryenter(audio_track_t *track)
 {
-	return (atomic_cas_uint(>lock, 0, 1) == 0);
+
+	if (atomic_swap_uint(>lock, 1) != 0)
+		return false;
+	membar_enter();
+	return true;
 }
 
 /*
@@ -1661,9 +1668,10 @@ audio_track_lock_tryenter(audio_track_t 
 static __inline void
 audio_track_lock_enter(audio_track_t *track)
 {
+
 	/* Don't sleep here. */
 	while (audio_track_lock_tryenter(track) == false)
-		;
+		SPINLOCK_BACKOFF_HOOK;
 }
 
 /*
@@ -1672,7 +1680,8 @@ audio_track_lock_enter(audio_track_t *tr
 static __inline void
 audio_track_lock_exit(audio_track_t *track)
 {
-	atomic_swap_uint(>lock, 0);
+
+	atomic_store_release(>lock, 0);
 }
 
 



CVS commit: src/sys/dev/audio

2021-12-04 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Sun Dec  5 02:56:55 UTC 2021

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
s/faciliate/facilitate/ in comment.


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.110 src/sys/dev/audio/audio.c:1.111
--- src/sys/dev/audio/audio.c:1.110	Sun Oct 10 11:21:05 2021
+++ src/sys/dev/audio/audio.c	Sun Dec  5 02:56:55 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.110 2021/10/10 11:21:05 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.111 2021/12/05 02:56:55 msaitoh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.110 2021/10/10 11:21:05 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.111 2021/12/05 02:56:55 msaitoh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -3854,7 +3854,7 @@ audio_track_freq_up(audio_filter_arg_t *
 	d = arg->dst;
 
 	/*
-	 * In order to faciliate interpolation for each block, slide (delay)
+	 * In order to facilitate interpolation for each block, slide (delay)
 	 * input by one sample.  As a result, strictly speaking, the output
 	 * phase is delayed by 1/dstfreq.  However, I believe there is no
 	 * observable impact.



CVS commit: src/sys/dev/audio

2021-12-04 Thread SAITOH Masanobu
Module Name:src
Committed By:   msaitoh
Date:   Sun Dec  5 02:56:55 UTC 2021

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
s/faciliate/facilitate/ in comment.


To generate a diff of this commit:
cvs rdiff -u -r1.110 -r1.111 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2021-10-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Oct 10 11:21:05 UTC 2021

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(9): Issue pserialize_perform outside sc_lock in audiodetach.

Breaks another deadlock between sc_lock and high-priority xcalls at
softint serial.  With any luck, this should be the last such softint
deadlock in audio!


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.109 src/sys/dev/audio/audio.c:1.110
--- src/sys/dev/audio/audio.c:1.109	Sun Oct 10 11:20:29 2021
+++ src/sys/dev/audio/audio.c	Sun Oct 10 11:21:05 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.109 2021/10/10 11:20:29 riastradh Exp $	*/
+/*	$NetBSD: audio.c,v 1.110 2021/10/10 11:21:05 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.109 2021/10/10 11:20:29 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.110 2021/10/10 11:21:05 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -1322,6 +1322,7 @@ audiodetach(device_t self, int flags)
 	SLIST_FOREACH(file, >sc_files, entry) {
 		atomic_store_relaxed(>dying, true);
 	}
+	mutex_exit(sc->sc_lock);
 
 	/*
 	 * Wait for existing users to drain.
@@ -1331,7 +1332,6 @@ audiodetach(device_t self, int flags)
 	 *   be psref_released.
 	 */
 	pserialize_perform(sc->sc_psz);
-	mutex_exit(sc->sc_lock);
 	psref_target_destroy(>sc_psref, audio_psref_class);
 
 	/*



CVS commit: src/sys/dev/audio

2021-10-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Oct 10 11:21:05 UTC 2021

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(9): Issue pserialize_perform outside sc_lock in audiodetach.

Breaks another deadlock between sc_lock and high-priority xcalls at
softint serial.  With any luck, this should be the last such softint
deadlock in audio!


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2021-10-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Oct 10 11:20:29 UTC 2021

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(9): Call hw_if->getdev without sc_lock.

Holding sc_lock is not necessary -- I reviewed all ~70 cases in-tree,
and none of them rely on state protected by sc_lock.  Essentially
everything just copies from static data or data initialized at attach
time.

(Exceptions: tms320av110.c issues a bus_space_read_1, but I don't see
any reason why that needs to be serialized; and uaudio.c reads from
sc_dying, but that's not necessary and also not protected by sc_lock
anyway.)

Holding sc_lock is harmful because at least hdafg(4) can trigger module
autoload that leads to pserialize_perform, which waits for logic to run
at softint serial on all CPUs; at the same time, audio_softintr_rd/wr
run at softint serial and take sc_lock, so this leads to deadlock.


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.109 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.108 src/sys/dev/audio/audio.c:1.109
--- src/sys/dev/audio/audio.c:1.108	Sun Sep 26 01:16:08 2021
+++ src/sys/dev/audio/audio.c	Sun Oct 10 11:20:29 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.108 2021/09/26 01:16:08 thorpej Exp $	*/
+/*	$NetBSD: audio.c,v 1.109 2021/10/10 11:20:29 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -114,7 +114,7 @@
  *	halt_output 		x	x +
  *	halt_input 		x	x +
  *	speaker_ctl 		x	x
- *	getdev 			-	x
+ *	getdev 			-	-
  *	set_port 		-	x +
  *	get_port 		-	x +
  *	query_devinfo 		-	x
@@ -138,7 +138,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.108 2021/09/26 01:16:08 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.109 2021/10/10 11:20:29 riastradh Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -3113,9 +3113,7 @@ audio_ioctl(dev_t dev, struct audio_soft
 		break;
 
 	case AUDIO_GETDEV:
-		mutex_enter(sc->sc_lock);
 		error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
-		mutex_exit(sc->sc_lock);
 		break;
 
 	case AUDIO_GETENC:
@@ -8291,9 +8289,7 @@ mixer_ioctl(struct audio_softc *sc, u_lo
 
 	case AUDIO_GETDEV:
 		TRACE(2, "AUDIO_GETDEV");
-		mutex_enter(sc->sc_lock);
 		error = sc->hw_if->getdev(sc->hw_hdl, (audio_device_t *)addr);
-		mutex_exit(sc->sc_lock);
 		break;
 
 	case AUDIO_MIXER_DEVINFO:



CVS commit: src/sys/dev/audio

2021-10-10 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Sun Oct 10 11:20:29 UTC 2021

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
audio(9): Call hw_if->getdev without sc_lock.

Holding sc_lock is not necessary -- I reviewed all ~70 cases in-tree,
and none of them rely on state protected by sc_lock.  Essentially
everything just copies from static data or data initialized at attach
time.

(Exceptions: tms320av110.c issues a bus_space_read_1, but I don't see
any reason why that needs to be serialized; and uaudio.c reads from
sc_dying, but that's not necessary and also not protected by sc_lock
anyway.)

Holding sc_lock is harmful because at least hdafg(4) can trigger module
autoload that leads to pserialize_perform, which waits for logic to run
at softint serial on all CPUs; at the same time, audio_softintr_rd/wr
run at softint serial and take sc_lock, so this leads to deadlock.


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.109 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



Re: CVS commit: src/sys/dev/audio

2021-06-08 Thread Rin Okuyama

On 2021/06/08 16:09, nia wrote:

On Tue, Jun 01, 2021 at 09:12:24PM +, Taylor R Campbell wrote:

audio(4): Set AUMODE_PLAY/RECORD only if asked _and_ supported.

If one is requested and _not_ supported, fail; otherwise we might
enter audio_write with a null play track and crash on KASSERT.


It looks like this is an incompatible change.

Sun says:

- Attempts to open a device with FREAD set fail if the device is not
   capable of recording.  (Likewise for FWRITE and playback.)
https://github.com/illumos/illumos-gate/blob/9ecd05bdc59e4a1091c51ce68cce2028d5ba6fd1/usr/src/uts/common/io/audio/impl/audio_sun.c#L70

But in NetBSD 7...

audio_open() does not return a clear failure:
https://github.com/NetBSD/src/blob/netbsd-7/sys/dev/audio.c#L1652

EINVAL is returned if !audio_can_playback in audiostartp()
https://github.com/NetBSD/src/blob/netbsd-7/sys/dev/audio.c#L2801
... which is called from audio_write()

So it looks to me like open() should succeed but write() should fail.

This is important if you want to open an audio device just to test
a few properties (i.e. AUDIO_GETPROPS, AUDIO_GETDEV...).



Also, FYI, some tests for audio(4) have started to fail somewhen
between audio.c revs 1.96 (this commit) to 1.102:

https://releng.netbsd.org/b5reports/i386/commits-2021.06.html#2021.06.02.08.46.16

Thanks,
rin


Re: CVS commit: src/sys/dev/audio

2021-06-08 Thread nia
On Tue, Jun 01, 2021 at 09:12:24PM +, Taylor R Campbell wrote:
> audio(4): Set AUMODE_PLAY/RECORD only if asked _and_ supported.
> 
> If one is requested and _not_ supported, fail; otherwise we might
> enter audio_write with a null play track and crash on KASSERT.

It looks like this is an incompatible change.

Sun says:

- Attempts to open a device with FREAD set fail if the device is not
  capable of recording.  (Likewise for FWRITE and playback.)
https://github.com/illumos/illumos-gate/blob/9ecd05bdc59e4a1091c51ce68cce2028d5ba6fd1/usr/src/uts/common/io/audio/impl/audio_sun.c#L70

But in NetBSD 7...

audio_open() does not return a clear failure:
https://github.com/NetBSD/src/blob/netbsd-7/sys/dev/audio.c#L1652

EINVAL is returned if !audio_can_playback in audiostartp()
https://github.com/NetBSD/src/blob/netbsd-7/sys/dev/audio.c#L2801
... which is called from audio_write()

So it looks to me like open() should succeed but write() should fail.

This is important if you want to open an audio device just to test
a few properties (i.e. AUDIO_GETPROPS, AUDIO_GETDEV...).


Re: CVS commit: src/sys/dev/audio

2020-05-30 Thread nia
On Sat, May 30, 2020 at 09:48:36PM +0900, Tetsuya Isaki wrote:
> I will do it on next weekend.
> 
> Thanks,
> ---
> Tetsuya Isaki 

Thank you.


Re: CVS commit: src/sys/dev/audio

2020-05-30 Thread Tetsuya Isaki
At Fri, 29 May 2020 12:32:39 +,
nia wrote:
> OK... Can you request a pullup to ensure resuming with a stream
> playing doesn't panic on 9.1?

I will do it on next weekend.

Thanks,
---
Tetsuya Isaki 


Re: CVS commit: src/sys/dev/audio

2020-05-29 Thread nia
OK... Can you request a pullup to ensure resuming with a stream
playing doesn't panic on 9.1?

Playing audio is very distorted on resume, but that can be resolved
by killing the streams...


Re: CVS commit: src/sys/dev/audio

2020-05-28 Thread Tetsuya Isaki
At Wed, 27 May 2020 13:19:22 +,
nia wrote:
> I think this is because audio_rmixer_start is used unguarded
> in audio_open (it doesn't check for the sc_rbusy flag).
> This isn't the case for pmixer. 
> 
> So, if the audio device is opened for recording for the 
> first time after system resumption, a panic will occur
> due to an assertion failure (the recording mixer would
> already be busy).

It's because your change didn't restore [pr]mixer's running
state correctly.  I have fixed it.

Thanks,
---
Tetsuya Isaki 


Re: CVS commit: src/sys/dev/audio

2020-05-27 Thread nia
On Wed, May 27, 2020 at 09:46:04PM +0900, Tetsuya Isaki wrote:
> Why are playback and recording asymmetric?
> 
> Thanks,

I think this is because audio_rmixer_start is used unguarded
in audio_open (it doesn't check for the sc_rbusy flag).
This isn't the case for pmixer. 

So, if the audio device is opened for recording for the 
first time after system resumption, a panic will occur
due to an assertion failure (the recording mixer would
already be busy).

If there's an advantage to not starting the playback
mixer on resume if no devices were previously opened we
can do that too?


Re: CVS commit: src/sys/dev/audio

2020-05-27 Thread Tetsuya Isaki
nia,

At Tue, 26 May 2020 15:20:16 +,
Nia Alarie wrote:
> Module Name:  src
> Committed By: nia
> Date: Tue May 26 15:20:16 UTC 2020
> 
> Modified Files:
>   src/sys/dev/audio: audio.c
> 
> Log Message:
> audio: Only restart recording mixer on resume if it's already been started
> 
> 
> To generate a diff of this commit:
> cvs rdiff -u -r1.73 -r1.74 src/sys/dev/audio/audio.c

Why are playback and recording asymmetric?

Thanks,
---
Tetsuya Isaki 


CVS commit: src/sys/dev/audio

2019-11-06 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Nov  6 13:37:28 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix a wrong calculation of recording ring buffer.  Reported on
http://mail-index.netbsd.org/current-users/2019/11/04/msg036976.html


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-11-06 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Nov  6 13:37:28 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix a wrong calculation of recording ring buffer.  Reported on
http://mail-index.netbsd.org/current-users/2019/11/04/msg036976.html


To generate a diff of this commit:
cvs rdiff -u -r1.32 -r1.33 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.32 src/sys/dev/audio/audio.c:1.33
--- src/sys/dev/audio/audio.c:1.32	Fri Oct 18 04:09:02 2019
+++ src/sys/dev/audio/audio.c	Wed Nov  6 13:37:27 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.32 2019/10/18 04:09:02 msaitoh Exp $	*/
+/*	$NetBSD: audio.c,v 1.33 2019/11/06 13:37:27 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.32 2019/10/18 04:09:02 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.33 2019/11/06 13:37:27 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -4510,7 +4510,7 @@ audio_track_record(audio_track_t *track)
 		int bytes1;
 		int bytes2;
 
-		bytes1 = auring_get_contig_used(usrbuf);
+		bytes1 = auring_get_contig_free(usrbuf);
 		KASSERT(bytes1 % framesize == 0);
 		memcpy((uint8_t *)usrbuf->mem + auring_tail(usrbuf),
 		(uint8_t *)outbuf->mem + outbuf->head * framesize,



CVS commit: src/sys/dev/audio

2019-09-06 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Fri Sep  6 06:44:45 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Tune some debug message level.
These messages are important for debugging hardware driver.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-09-06 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Fri Sep  6 06:44:45 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Tune some debug message level.
These messages are important for debugging hardware driver.


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.30 src/sys/dev/audio/audio.c:1.31
--- src/sys/dev/audio/audio.c:1.30	Thu Aug 29 13:01:07 2019
+++ src/sys/dev/audio/audio.c	Fri Sep  6 06:44:45 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.30 2019/08/29 13:01:07 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.31 2019/09/06 06:44:45 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.30 2019/08/29 13:01:07 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.31 2019/09/06 06:44:45 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -4621,7 +4621,7 @@ audio_mixer_init(struct audio_softc *sc,
 		audio_params_t p = format2_to_params(>hwbuf.fmt);
 		rounded = sc->hw_if->round_blocksize(sc->hw_hdl, blksize,
 		mode, );
-		TRACE(2, "round_blocksize %d -> %d", blksize, rounded);
+		TRACE(1, "round_blocksize %d -> %d", blksize, rounded);
 		if (rounded != blksize) {
 			if ((rounded * NBBY) % (mixer->hwbuf.fmt.stride *
 			mixer->hwbuf.fmt.channels) != 0) {
@@ -4646,7 +4646,7 @@ audio_mixer_init(struct audio_softc *sc,
 		size_t rounded;
 		rounded = sc->hw_if->round_buffersize(sc->hw_hdl, mode,
 		bufsize);
-		TRACE(2, "round_buffersize %zd -> %zd", bufsize, rounded);
+		TRACE(1, "round_buffersize %zd -> %zd", bufsize, rounded);
 		if (rounded < bufsize) {
 			/* buffersize needs NBLKHW blocks at least. */
 			device_printf(sc->sc_dev,
@@ -4669,7 +4669,7 @@ audio_mixer_init(struct audio_softc *sc,
 			capacity = mixer->frames_per_block * hwblks;
 		}
 	}
-	TRACE(2, "buffersize for %s = %zu",
+	TRACE(1, "buffersize for %s = %zu",
 	(mode == AUMODE_PLAY) ? "playback" : "recording",
 	bufsize);
 	mixer->hwbuf.capacity = capacity;



CVS commit: src/sys/dev/audio

2019-08-29 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Thu Aug 29 13:01:07 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c audiovar.h

Log Message:
Fix lock assertion on async I/O mode.
psignal() must be called without any spin locks.
Thanks maxv@!


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/audio/audiovar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.29 src/sys/dev/audio/audio.c:1.30
--- src/sys/dev/audio/audio.c:1.29	Fri Aug 23 09:41:26 2019
+++ src/sys/dev/audio/audio.c	Thu Aug 29 13:01:07 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.29 2019/08/23 09:41:26 maxv Exp $	*/
+/*	$NetBSD: audio.c,v 1.30 2019/08/29 13:01:07 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.29 2019/08/23 09:41:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.30 2019/08/29 13:01:07 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -5730,6 +5730,36 @@ audio_track_drain(struct audio_softc *sc
 }
 
 /*
+ * Send signal to process.
+ * This is intended to be called only from audio_softintr_{rd,wr}.
+ * Must be called with sc_lock && sc_intr_lock held.
+ */
+static inline void
+audio_psignal(struct audio_softc *sc, pid_t pid, int signum)
+{
+	proc_t *p;
+
+	KASSERT(mutex_owned(sc->sc_lock));
+	KASSERT(mutex_owned(sc->sc_intr_lock));
+	KASSERT(pid != 0);
+
+	/*
+	 * psignal() must be called without spin lock held.
+	 * So leave intr_lock temporarily here.
+	 */
+	mutex_exit(sc->sc_intr_lock);
+
+	mutex_enter(proc_lock);
+	p = proc_find(pid);
+	if (p)
+		psignal(p, signum);
+	mutex_exit(proc_lock);
+
+	/* Enter intr_lock again */
+	mutex_enter(sc->sc_intr_lock);
+}
+
+/*
  * This is software interrupt handler for record.
  * It is called from recording hardware interrupt everytime.
  * It does:
@@ -5747,7 +5777,6 @@ audio_softintr_rd(void *cookie)
 {
 	struct audio_softc *sc = cookie;
 	audio_file_t *f;
-	proc_t *p;
 	pid_t pid;
 
 	mutex_enter(sc->sc_lock);
@@ -5767,10 +5796,7 @@ audio_softintr_rd(void *cookie)
 		pid = f->async_audio;
 		if (pid != 0) {
 			TRACEF(4, f, "sending SIGIO %d", pid);
-			mutex_enter(proc_lock);
-			if ((p = proc_find(pid)) != NULL)
-psignal(p, SIGIO);
-			mutex_exit(proc_lock);
+			audio_psignal(sc, pid, SIGIO);
 		}
 	}
 	mutex_exit(sc->sc_intr_lock);
@@ -5799,7 +5825,6 @@ audio_softintr_wr(void *cookie)
 	struct audio_softc *sc = cookie;
 	audio_file_t *f;
 	bool found;
-	proc_t *p;
 	pid_t pid;
 
 	TRACE(4, "called");
@@ -5826,14 +5851,13 @@ audio_softintr_wr(void *cookie)
 		 */
 		if (track->usrbuf.used <= track->usrbuf_usedlow &&
 		!track->is_pause) {
+			/* For selnotify */
 			found = true;
+			/* For SIGIO */
 			pid = f->async_audio;
 			if (pid != 0) {
 TRACEF(4, f, "sending SIGIO %d", pid);
-mutex_enter(proc_lock);
-if ((p = proc_find(pid)) != NULL)
-	psignal(p, SIGIO);
-mutex_exit(proc_lock);
+audio_psignal(sc, pid, SIGIO);
 			}
 		}
 	}

Index: src/sys/dev/audio/audiovar.h
diff -u src/sys/dev/audio/audiovar.h:1.4 src/sys/dev/audio/audiovar.h:1.5
--- src/sys/dev/audio/audiovar.h:1.4	Wed Jun 26 06:57:45 2019
+++ src/sys/dev/audio/audiovar.h	Thu Aug 29 13:01:07 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audiovar.h,v 1.4 2019/06/26 06:57:45 isaki Exp $	*/
+/*	$NetBSD: audiovar.h,v 1.5 2019/08/29 13:01:07 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -149,7 +149,8 @@ struct audio_softc {
 
 	/*
 	 * List of opened descriptors.
-	 * Must be protected by sc_intr_lock.
+	 * Must be protected by sc_lock || sc_intr_lock for traversal(FOREACH).
+	 * Must be protected by sc_lock && sc_intr_lock for insertion/removal.
 	 */
 	SLIST_HEAD(, audio_file) sc_files;
 



CVS commit: src/sys/dev/audio

2019-08-29 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Thu Aug 29 13:01:07 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c audiovar.h

Log Message:
Fix lock assertion on async I/O mode.
psignal() must be called without any spin locks.
Thanks maxv@!


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.30 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.4 -r1.5 src/sys/dev/audio/audiovar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-08-23 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Aug 23 09:41:26 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Add missing mutex, we were hitting a KASSERT.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-08-23 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Aug 23 09:41:26 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Add missing mutex, we were hitting a KASSERT.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.28 src/sys/dev/audio/audio.c:1.29
--- src/sys/dev/audio/audio.c:1.28	Wed Jul 10 13:26:47 2019
+++ src/sys/dev/audio/audio.c	Fri Aug 23 09:41:26 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.28 2019/07/10 13:26:47 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.29 2019/08/23 09:41:26 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.28 2019/07/10 13:26:47 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.29 2019/08/23 09:41:26 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -7726,7 +7726,9 @@ mixer_ioctl(struct audio_softc *sc, u_lo
 		} else {
 			ma = NULL;
 		}
+		mutex_enter(sc->sc_lock);
 		mixer_remove(sc);	/* remove old entry */
+		mutex_exit(sc->sc_lock);
 		if (ma != NULL) {
 			ma->next = sc->sc_async_mixer;
 			ma->pid = curproc->p_pid;



CVS commit: src/sys/dev/audio

2019-07-10 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Jul 10 13:26:47 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Use kmem_alloc/free() instead of old kern_malloc/free().


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.27 src/sys/dev/audio/audio.c:1.28
--- src/sys/dev/audio/audio.c:1.27	Wed Jul 10 13:17:57 2019
+++ src/sys/dev/audio/audio.c	Wed Jul 10 13:26:47 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.27 2019/07/10 13:17:57 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.28 2019/07/10 13:26:47 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.27 2019/07/10 13:17:57 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.28 2019/07/10 13:26:47 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -4685,13 +4685,7 @@ audio_mixer_init(struct audio_softc *sc,
 			return ENOMEM;
 		}
 	} else {
-		mixer->hwbuf.mem = kern_malloc(bufsize, M_NOWAIT);
-		if (mixer->hwbuf.mem == NULL) {
-			device_printf(sc->sc_dev,
-			"%s: malloc hwbuf(%zu) failed\n",
-			__func__, bufsize);
-			return ENOMEM;
-		}
+		mixer->hwbuf.mem = kmem_alloc(bufsize, KM_SLEEP);
 	}
 
 	/* From here, audio_mixer_destroy is necessary to exit. */
@@ -4811,7 +4805,7 @@ audio_mixer_destroy(struct audio_softc *
 		if (sc->hw_if->freem) {
 			sc->hw_if->freem(sc->hw_hdl, mixer->hwbuf.mem, bufsize);
 		} else {
-			kern_free(mixer->hwbuf.mem);
+			kmem_free(mixer->hwbuf.mem, bufsize);
 		}
 		mixer->hwbuf.mem = NULL;
 	}



CVS commit: src/sys/dev/audio

2019-07-10 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Jul 10 13:26:47 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Use kmem_alloc/free() instead of old kern_malloc/free().


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-07-10 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Jul 10 13:17:57 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix freem() argument.  The 3rd argument is bufsize, not direction...


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-07-10 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Jul 10 13:17:57 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix freem() argument.  The 3rd argument is bufsize, not direction...


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.26 src/sys/dev/audio/audio.c:1.27
--- src/sys/dev/audio/audio.c:1.26	Sun Jul  7 06:29:14 2019
+++ src/sys/dev/audio/audio.c	Wed Jul 10 13:17:57 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.26 2019/07/07 06:29:14 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.27 2019/07/10 13:17:57 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.26 2019/07/07 06:29:14 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.27 2019/07/10 13:17:57 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -4801,16 +4801,15 @@ abort:
 static void
 audio_mixer_destroy(struct audio_softc *sc, audio_trackmixer_t *mixer)
 {
-	int mode;
+	int bufsize;
 
 	KASSERT(mutex_owned(sc->sc_lock));
 
-	mode = mixer->mode;
-	KASSERT(mode == AUMODE_PLAY || mode == AUMODE_RECORD);
+	bufsize = frametobyte(>hwbuf.fmt, mixer->hwbuf.capacity);
 
 	if (mixer->hwbuf.mem != NULL) {
 		if (sc->hw_if->freem) {
-			sc->hw_if->freem(sc->hw_hdl, mixer->hwbuf.mem, mode);
+			sc->hw_if->freem(sc->hw_hdl, mixer->hwbuf.mem, bufsize);
 		} else {
 			kern_free(mixer->hwbuf.mem);
 		}



CVS commit: src/sys/dev/audio

2019-07-07 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sun Jul  7 06:29:15 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Avoid memory reallocation on SET_FORMAT.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.25 src/sys/dev/audio/audio.c:1.26
--- src/sys/dev/audio/audio.c:1.25	Sun Jul  7 06:14:21 2019
+++ src/sys/dev/audio/audio.c	Sun Jul  7 06:29:14 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.25 2019/07/07 06:14:21 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.26 2019/07/07 06:29:14 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.25 2019/07/07 06:14:21 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.26 2019/07/07 06:29:14 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -5967,11 +5967,14 @@ audio_mixers_init(struct audio_softc *sc
 	KASSERT(mutex_owned(sc->sc_lock));
 
 	if ((mode & AUMODE_PLAY)) {
-		if (sc->sc_pmixer) {
+		if (sc->sc_pmixer == NULL) {
+			sc->sc_pmixer = kmem_zalloc(sizeof(*sc->sc_pmixer),
+			KM_SLEEP);
+		} else {
+			/* destroy() doesn't free memory. */
 			audio_mixer_destroy(sc, sc->sc_pmixer);
-			kmem_free(sc->sc_pmixer, sizeof(*sc->sc_pmixer));
+			memset(sc->sc_pmixer, 0, sizeof(*sc->sc_pmixer));
 		}
-		sc->sc_pmixer = kmem_zalloc(sizeof(*sc->sc_pmixer), KM_SLEEP);
 		error = audio_mixer_init(sc, AUMODE_PLAY, phwfmt, pfil);
 		if (error) {
 			aprint_error_dev(sc->sc_dev,
@@ -5982,11 +5985,14 @@ audio_mixers_init(struct audio_softc *sc
 		}
 	}
 	if ((mode & AUMODE_RECORD)) {
-		if (sc->sc_rmixer) {
+		if (sc->sc_rmixer == NULL) {
+			sc->sc_rmixer = kmem_zalloc(sizeof(*sc->sc_rmixer),
+			KM_SLEEP);
+		} else {
+			/* destroy() doesn't free memory. */
 			audio_mixer_destroy(sc, sc->sc_rmixer);
-			kmem_free(sc->sc_rmixer, sizeof(*sc->sc_rmixer));
+			memset(sc->sc_rmixer, 0, sizeof(*sc->sc_rmixer));
 		}
-		sc->sc_rmixer = kmem_zalloc(sizeof(*sc->sc_rmixer), KM_SLEEP);
 		error = audio_mixer_init(sc, AUMODE_RECORD, rhwfmt, rfil);
 		if (error) {
 			aprint_error_dev(sc->sc_dev,



CVS commit: src/sys/dev/audio

2019-07-07 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sun Jul  7 06:29:15 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Avoid memory reallocation on SET_FORMAT.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-07-07 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sun Jul  7 06:14:21 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Rearrange some KASSERT and debug messages, to sync with others.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-07-07 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sun Jul  7 06:14:21 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Rearrange some KASSERT and debug messages, to sync with others.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.24 src/sys/dev/audio/audio.c:1.25
--- src/sys/dev/audio/audio.c:1.24	Sun Jul  7 06:06:46 2019
+++ src/sys/dev/audio/audio.c	Sun Jul  7 06:14:21 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.24 2019/07/07 06:06:46 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.25 2019/07/07 06:14:21 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.24 2019/07/07 06:06:46 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.25 2019/07/07 06:14:21 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2213,6 +2213,8 @@ audio_read(struct audio_softc *sc, struc
 	audio_ring_t *input;
 	int error;
 
+	KASSERT(!mutex_owned(sc->sc_lock));
+
 	/*
 	 * On half-duplex hardware, O_RDWR is treated as O_WRONLY.
 	 * However read() system call itself can be called because it's
@@ -2223,8 +2225,6 @@ audio_read(struct audio_softc *sc, struc
 		return EBADF;
 	}
 
-	KASSERT(!mutex_owned(sc->sc_lock));
-
 	/* I think it's better than EINVAL. */
 	if (track->mmapped)
 		return EPERM;
@@ -2340,18 +2340,19 @@ audio_write(struct audio_softc *sc, stru
 	audio_ring_t *outbuf;
 	int error;
 
+	KASSERT(!mutex_owned(sc->sc_lock));
+
 	track = file->ptrack;
 	KASSERT(track);
-	TRACET(2, track, "%sresid=%zd pid=%d.%d ioflag=0x%x",
-	audiodebug >= 3 ? "begin " : "",
-	uio->uio_resid, (int)curproc->p_pid, (int)curlwp->l_lid, ioflag);
-
-	KASSERT(!mutex_owned(sc->sc_lock));
 
 	/* I think it's better than EINVAL. */
 	if (track->mmapped)
 		return EPERM;
 
+	TRACET(2, track, "%sresid=%zd pid=%d.%d ioflag=0x%x",
+	audiodebug >= 3 ? "begin " : "",
+	uio->uio_resid, (int)curproc->p_pid, (int)curlwp->l_lid, ioflag);
+
 	if (uio->uio_resid == 0) {
 		track->eofcounter++;
 		return 0;



CVS commit: src/sys/dev/audio

2019-07-07 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sun Jul  7 06:06:46 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix to evaluate the validity of this track at earlier on audio_read().
It fixes a panic on read() against descriptor opened as O_RDWR on the
half duplex device.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-07-07 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sun Jul  7 06:06:46 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Fix to evaluate the validity of this track at earlier on audio_read().
It fixes a panic on read() against descriptor opened as O_RDWR on the
half duplex device.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.23 src/sys/dev/audio/audio.c:1.24
--- src/sys/dev/audio/audio.c:1.23	Sat Jul  6 12:58:58 2019
+++ src/sys/dev/audio/audio.c	Sun Jul  7 06:06:46 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.23 2019/07/06 12:58:58 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.24 2019/07/07 06:06:46 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.23 2019/07/06 12:58:58 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.24 2019/07/07 06:06:46 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -2213,9 +2213,15 @@ audio_read(struct audio_softc *sc, struc
 	audio_ring_t *input;
 	int error;
 
+	/*
+	 * On half-duplex hardware, O_RDWR is treated as O_WRONLY.
+	 * However read() system call itself can be called because it's
+	 * opened with O_RDWR.  So in this case, deny this read().
+	 */
 	track = file->rtrack;
-	KASSERT(track);
-	TRACET(2, track, "resid=%zd", uio->uio_resid);
+	if (track == NULL) {
+		return EBADF;
+	}
 
 	KASSERT(!mutex_owned(sc->sc_lock));
 
@@ -2223,6 +2229,8 @@ audio_read(struct audio_softc *sc, struc
 	if (track->mmapped)
 		return EPERM;
 
+	TRACET(2, track, "resid=%zd", uio->uio_resid);
+
 #ifdef AUDIO_PM_IDLE
 	mutex_enter(sc->sc_lock);
 	if (device_is_active(>sc_dev) || sc->sc_idle)
@@ -2230,15 +2238,6 @@ audio_read(struct audio_softc *sc, struc
 	mutex_exit(sc->sc_lock);
 #endif
 
-	/*
-	 * On half-duplex hardware, O_RDWR is treated as O_WRONLY.
-	 * However read() system call itself can be called because it's
-	 * opened with O_RDWR.  So in this case, deny this read().
-	 */
-	if ((file->mode & AUMODE_RECORD) == 0) {
-		return EBADF;
-	}
-
 	usrbuf = >usrbuf;
 	input = track->input;
 



CVS commit: src/sys/dev/audio

2019-07-06 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Jul  6 12:58:58 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
Implement auto recovery of the mixing volume.


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



CVS commit: src/sys/dev/audio

2019-07-06 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Sat Jul  6 12:58:58 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c audiodef.h

Log Message:
Implement auto recovery of the mixing volume.


To generate a diff of this commit:
cvs rdiff -u -r1.22 -r1.23 src/sys/dev/audio/audio.c
cvs rdiff -u -r1.6 -r1.7 src/sys/dev/audio/audiodef.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.22 src/sys/dev/audio/audio.c:1.23
--- src/sys/dev/audio/audio.c:1.22	Wed Jun 26 07:47:25 2019
+++ src/sys/dev/audio/audio.c	Sat Jul  6 12:58:58 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.22 2019/06/26 07:47:25 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.23 2019/07/06 12:58:58 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.22 2019/06/26 07:47:25 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.23 2019/07/06 12:58:58 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -593,6 +593,7 @@ static int audio_mixer_init(struct audio
 static void audio_mixer_destroy(struct audio_softc *, audio_trackmixer_t *);
 static void audio_pmixer_start(struct audio_softc *, bool);
 static void audio_pmixer_process(struct audio_softc *);
+static void audio_pmixer_agc(audio_trackmixer_t *, int);
 static int  audio_pmixer_mix_track(audio_trackmixer_t *, audio_track_t *, int);
 static void audio_pmixer_output(struct audio_softc *);
 static int  audio_pmixer_halt(struct audio_softc *);
@@ -2155,6 +2156,7 @@ audio_close(struct audio_softc *sc, audi
 		if (sc->sc_popens == 0) {
 			mutex_enter(sc->sc_intr_lock);
 			sc->sc_pmixer->volume = 256;
+			sc->sc_pmixer->voltimer = 0;
 			mutex_exit(sc->sc_intr_lock);
 		}
 	}
@@ -4972,60 +4974,33 @@ audio_pmixer_process(struct audio_softc 
 		memset(mixer->mixsample, 0,
 		frametobyte(>mixfmt, frame_count));
 	} else {
-		aint2_t ovf_plus;
-		aint2_t ovf_minus;
-		int vol;
-
-		/* Overflow detection */
-		ovf_plus = AINT_T_MAX;
-		ovf_minus = AINT_T_MIN;
-		m = mixer->mixsample;
-		for (i = 0; i < sample_count; i++) {
-			aint2_t val;
-
-			val = *m++;
-			if (val > ovf_plus)
-ovf_plus = val;
-			else if (val < ovf_minus)
-ovf_minus = val;
-		}
-
-		/* Master Volume Auto Adjust */
-		vol = mixer->volume;
-		if (ovf_plus > (aint2_t)AINT_T_MAX
-		 || ovf_minus < (aint2_t)AINT_T_MIN) {
-			aint2_t ovf;
-			int vol2;
-
-			/* XXX TODO: Check AINT2_T_MIN ? */
-			ovf = ovf_plus;
-			if (ovf < -ovf_minus)
-ovf = -ovf_minus;
-
-			/* Turn down the volume if overflow occured. */
-			vol2 = (int)((aint2_t)AINT_T_MAX * 256 / ovf);
-			if (vol2 < vol)
-vol = vol2;
-
-			if (vol < mixer->volume) {
-/* Turn down gradually to 128. */
-if (mixer->volume > 128) {
-	mixer->volume =
-	(mixer->volume * 95) / 100;
-	TRACE(2,
-	"auto volume adjust: volume %d",
-	mixer->volume);
-}
-			}
+		if (mixed > 1) {
+			/* If there are multiple tracks, do auto gain control */
+			audio_pmixer_agc(mixer, sample_count);
 		}
 
-		/* Apply Master Volume. */
-		if (vol != 256) {
+		/* Apply master volume */
+		if (mixer->volume < 256) {
 			m = mixer->mixsample;
 			for (i = 0; i < sample_count; i++) {
-*m = AUDIO_SCALEDOWN(*m * vol, 8);
+*m = AUDIO_SCALEDOWN(*m * mixer->volume, 8);
 m++;
 			}
+
+			/*
+			 * Recover the volume gradually at the pace of
+			 * several times per second.  If it's too fast, you
+			 * can recognize that the volume changes up and down
+			 * quickly and it's not so comfortable.
+			 */
+			mixer->voltimer += mixer->blktime_n;
+			if (mixer->voltimer * 4 >= mixer->blktime_d) {
+mixer->volume++;
+mixer->voltimer = 0;
+#if defined(AUDIO_DEBUG_AGC)
+TRACE(1, "volume recover: %d", mixer->volume);
+#endif
+			}
 		}
 	}
 
@@ -5069,6 +5044,62 @@ audio_pmixer_process(struct audio_softc 
 }
 
 /*
+ * Do auto gain control.
+ * Must be called sc_intr_lock held.
+ */
+static void
+audio_pmixer_agc(audio_trackmixer_t *mixer, int sample_count)
+{
+	struct audio_softc *sc __unused;
+	aint2_t val;
+	aint2_t maxval;
+	aint2_t minval;
+	aint2_t over_plus;
+	aint2_t over_minus;
+	aint2_t *m;
+	int newvol;
+	int i;
+
+	sc = mixer->sc;
+
+	/* Overflow detection */
+	maxval = AINT_T_MAX;
+	minval = AINT_T_MIN;
+	m = mixer->mixsample;
+	for (i = 0; i < sample_count; i++) {
+		val = *m++;
+		if (val > maxval)
+			maxval = val;
+		else if (val < minval)
+			minval = val;
+	}
+
+	/* Absolute value of overflowed amount */
+	over_plus = maxval - AINT_T_MAX;
+	over_minus = AINT_T_MIN - minval;
+
+	if (over_plus > 0 || over_minus > 0) {
+		if (over_plus > over_minus) {
+			newvol = (int)((aint2_t)AINT_T_MAX * 256 / maxval);
+		} else {
+			newvol = (int)((aint2_t)AINT_T_MIN * 256 / minval);
+		}
+
+		/*
+		 * Change the volume only if new one is smaller.
+		 * Reset the timer 

CVS commit: src/sys/dev/audio

2019-06-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Jun 26 07:47:25 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Improve debug message and remove duplicated one.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/audio/audio.c
diff -u src/sys/dev/audio/audio.c:1.21 src/sys/dev/audio/audio.c:1.22
--- src/sys/dev/audio/audio.c:1.21	Wed Jun 26 06:57:45 2019
+++ src/sys/dev/audio/audio.c	Wed Jun 26 07:47:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: audio.c,v 1.21 2019/06/26 06:57:45 isaki Exp $	*/
+/*	$NetBSD: audio.c,v 1.22 2019/06/26 07:47:25 isaki Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -142,7 +142,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.21 2019/06/26 06:57:45 isaki Exp $");
+__KERNEL_RCSID(0, "$NetBSD: audio.c,v 1.22 2019/06/26 07:47:25 isaki Exp $");
 
 #ifdef _KERNEL_OPT
 #include "audio.h"
@@ -1858,8 +1858,9 @@ audio_open(dev_t dev, struct audio_softc
 	KASSERT(mutex_owned(sc->sc_lock));
 	KASSERT(sc->sc_exlock);
 
-	TRACE(1, "%sflags=0x%x po=%d ro=%d",
+	TRACE(1, "%sdev=%s flags=0x%x po=%d ro=%d",
 	(audiodebug >= 3) ? "start " : "",
+	ISDEVSOUND(dev) ? "sound" : "audio",
 	flags, sc->sc_popens, sc->sc_ropens);
 
 	af = kmem_zalloc(sizeof(audio_file_t), KM_SLEEP);
@@ -2236,8 +2237,6 @@ audio_read(struct audio_softc *sc, struc
 		return EBADF;
 	}
 
-	TRACET(3, track, "resid=%zd", uio->uio_resid);
-
 	usrbuf = >usrbuf;
 	input = track->input;
 



CVS commit: src/sys/dev/audio

2019-06-26 Thread Tetsuya Isaki
Module Name:src
Committed By:   isaki
Date:   Wed Jun 26 07:47:25 UTC 2019

Modified Files:
src/sys/dev/audio: audio.c

Log Message:
Improve debug message and remove duplicated one.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/dev/audio/audio.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.



  1   2   >